| 1 | =====================
|
| 2 | Split page table lock
|
| 3 | =====================
|
| 4 |
|
| 5 | Originally, mm->page_table_lock spinlock protected all page tables of the
|
| 6 | mm_struct. But this approach leads to poor page fault scalability of
|
| 7 | multi-threaded applications due to high contention on the lock. To improve
|
| 8 | scalability, split page table lock was introduced.
|
| 9 |
|
| 10 | With split page table lock we have separate per-table lock to serialize
|
| 11 | access to the table. At the moment we use split lock for PTE and PMD
|
| 12 | tables. Access to higher level tables protected by mm->page_table_lock.
|
| 13 |
|
| 14 | There are helpers to lock/unlock a table and other accessor functions:
|
| 15 |
|
| 16 | - pte_offset_map_lock()
|
| 17 | maps PTE and takes PTE table lock, returns pointer to PTE with
|
| 18 | pointer to its PTE table lock, or returns NULL if no PTE table;
|
| 19 | - pte_offset_map_ro_nolock()
|
| 20 | maps PTE, returns pointer to PTE with pointer to its PTE table
|
| 21 | lock (not taken), or returns NULL if no PTE table;
|
| 22 | - pte_offset_map_rw_nolock()
|
| 23 | maps PTE, returns pointer to PTE with pointer to its PTE table
|
| 24 | lock (not taken) and the value of its pmd entry, or returns NULL
|
| 25 | if no PTE table;
|
| 26 | - pte_offset_map()
|
| 27 | maps PTE, returns pointer to PTE, or returns NULL if no PTE table;
|
| 28 | - pte_unmap()
|
| 29 | unmaps PTE table;
|
| 30 | - pte_unmap_unlock()
|
| 31 | unlocks and unmaps PTE table;
|
| 32 | - pte_alloc_map_lock()
|
| 33 | allocates PTE table if needed and takes its lock, returns pointer to
|
| 34 | PTE with pointer to its lock, or returns NULL if allocation failed;
|
| 35 | - pmd_lock()
|
| 36 | takes PMD table lock, returns pointer to taken lock;
|
| 37 | - pmd_lockptr()
|
| 38 | returns pointer to PMD table lock;
|
| 39 |
|
| 40 | Split page table lock for PTE tables is enabled compile-time if
|
| 41 | CONFIG_SPLIT_PTLOCK_CPUS (usually 4) is less or equal to NR_CPUS.
|
| 42 | If split lock is disabled, all tables are guarded by mm->page_table_lock.
|
| 43 |
|
| 44 | Split page table lock for PMD tables is enabled, if it's enabled for PTE
|
| 45 | tables and the architecture supports it (see below).
|
| 46 |
|
| 47 | Hugetlb and split page table lock
|
| 48 | =================================
|
| 49 |
|
| 50 | Hugetlb can support several page sizes. We use split lock only for PMD
|
| 51 | level, but not for PUD.
|
| 52 |
|
| 53 | Hugetlb-specific helpers:
|
| 54 |
|
| 55 | - huge_pte_lock()
|
| 56 | takes pmd split lock for PMD_SIZE page, mm->page_table_lock
|
| 57 | otherwise;
|
| 58 | - huge_pte_lockptr()
|
| 59 | returns pointer to table lock;
|
| 60 |
|
| 61 | Support of split page table lock by an architecture
|
| 62 | ===================================================
|
| 63 |
|
| 64 | There's no need in special enabling of PTE split page table lock: everything
|
| 65 | required is done by pagetable_pte_ctor() and pagetable_dtor(), which
|
| 66 | must be called on PTE table allocation / freeing.
|
| 67 |
|
| 68 | Make sure the architecture doesn't use slab allocator for page table
|
| 69 | allocation: slab uses page->slab_cache for its pages.
|
| 70 | This field shares storage with page->ptl.
|
| 71 |
|
| 72 | PMD split lock only makes sense if you have more than two page table
|
| 73 | levels.
|
| 74 |
|
| 75 | PMD split lock enabling requires pagetable_pmd_ctor() call on PMD table
|
| 76 | allocation and pagetable_dtor() on freeing.
|
| 77 |
|
| 78 | Allocation usually happens in pmd_alloc_one(), freeing in pmd_free() and
|
| 79 | pmd_free_tlb(), but make sure you cover all PMD table allocation / freeing
|
| 80 | paths: i.e X86_PAE preallocate few PMDs on pgd_alloc().
|
| 81 |
|
| 82 | With everything in place you can set CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK.
|
| 83 |
|
| 84 | NOTE: pagetable_pte_ctor() and pagetable_pmd_ctor() can fail -- it must
|
| 85 | be handled properly.
|
| 86 |
|
| 87 | page->ptl
|
| 88 | =========
|
| 89 |
|
| 90 | page->ptl is used to access split page table lock, where 'page' is struct
|
| 91 | page of page containing the table. It shares storage with page->private
|
| 92 | (and few other fields in union).
|
| 93 |
|
| 94 | To avoid increasing size of struct page and have best performance, we use a
|
| 95 | trick:
|
| 96 |
|
| 97 | - if spinlock_t fits into long, we use page->ptr as spinlock, so we
|
| 98 | can avoid indirect access and save a cache line.
|
| 99 | - if size of spinlock_t is bigger then size of long, we use page->ptl as
|
| 100 | pointer to spinlock_t and allocate it dynamically. This allows to use
|
| 101 | split lock with enabled DEBUG_SPINLOCK or DEBUG_LOCK_ALLOC, but costs
|
| 102 | one more cache line for indirect access;
|
| 103 |
|
| 104 | The spinlock_t allocated in pagetable_pte_ctor() for PTE table and in
|
| 105 | pagetable_pmd_ctor() for PMD table.
|
| 106 |
|
| 107 | Please, never access page->ptl directly -- use appropriate helper.
|