Shared page table core patch
Subsystems (8)
RFC PATCH 2/2 Shared page table core patch
This patch adds functionality to share page tables between processes when possible.
Signed off by: Dave McCracken <[email protected]>
---
arch/i386/Kconfig | 12 + arch/s390/Kconfig | 22 + arch/x86_64/Kconfig | 22 + include/asm-generic/pgtable.h | 31 ++ include/linux/mm.h | 10 include/linux/ptshare.h | 189 +++++++++++++++ include/linux/rmap.h | 2 include/linux/sched.h | 11 mm/Makefile | 1 mm/filemap_xip.c | 3 mm/fremap.c | 4 mm/hugetlb.c | 70 ++++- mm/memory.c | 37 ++- mm/mmap.c | 1 mm/mprotect.c | 9 mm/mremap.c | 5 mm/ptshare.c | 503 ++++++++++++++++++++++++++++++++++++++++++ mm/rmap.c | 21 + 18 files changed, 912 insertions(+), 41 deletions(-)
./arch/i386/Kconfig +12 -0
@@ -512,6 +512,18 @@ config X86_PAE depends on HIGHMEM64G default y +config PTSHARE + bool "Share page tables" + default y + help + Turn on sharing of page tables between processes for large shared + memory regions. + +config PTSHARE_PTE + bool + depends on PTSHARE + default y + # Common NUMA Features config NUMA bool "Numa Memory Allocation and Scheduler Support"
./arch/s390/Kconfig +22 -0
@@ -218,6 +218,28 @@ config WARN_STACK_SIZE source "mm/Kconfig" +config PTSHARE + bool "Share page tables" + default y + help + Turn on sharing of page tables between processes for large shared + memory regions. + +menu "Page table levels to share" + depends on PTSHARE + +config PTSHARE_PTE + bool "Bottom level table (PTE)" + depends on PTSHARE + default y + +config PTSHARE_PMD + bool "Middle level table (PMD)" + depends on PTSHARE && 64BIT + default y + +endmenu + comment "I/O subsystem configuration" config MACHCHK_WARNING
./arch/x86_64/Kconfig +22 -0
@@ -312,6 +312,28 @@ config NUMA_EMU into virtual nodes when booted with "numa=fake=N", where N is the number of nodes. This is only useful for debugging. +config PTSHARE + bool "Share page tables" + default y + help + Turn on sharing of page tables between processes for large shared + memory regions. + +config PTSHARE_PTE + bool + depends on PTSHARE + default y + +config PTSHARE_PMD + bool + depends on PTSHARE + default y + +config PTSHARE_HUGEPAGE + bool + depends on PTSHARE && PTSHARE_PMD + default y + config ARCH_DISCONTIGMEM_ENABLE bool depends on NUMA
./include/asm-generic/pgtable.h +31 -0
@@ -127,6 +127,16 @@ do { \ })#endif +#ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH_ALL +#define ptep_clear_flush_all (__vma, __address, __ptep) \ +({ \ + pte_t __pte; \ + __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \ + flush_tlb_all(); \ + __pte; \ +}) +#endif +#ifndef __HAVE_ARCH_PTEP_SET_WRPROTECTstruct mm_struct ;static inline void ptep_set_wrprotect (struct mm_struct *mm,unsigned long address,pte_t *ptep)
@@ -173,6 +183,27 @@ static inline void ptep_set_wrprotect(st#endif /* +* Some architectures might need flushes when higher levels of page table +* are unshared. +*/ + +#ifndef __HAVE_ARCH_PMD_CLEAR_FLUSH +#define pmd_clear_flush (__mm, __addr, __pmd) \ +({ \ + pmd_clear(__pmd); \ + flush_tlb_all(); \ +}) +#endif + +#ifndef __HAVE_ARCH_PUD_CLEAR_FLUSH +#define pud_clear_flush (__mm, __addr, __pud) \ +({ \ + pud_clear(__pud); \ + flush_tlb_all(); \ +}) +#endif + +/* * When walking page tables, get the address of the next boundary, * or the end address of the range if that comes earlier. Although no * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
./include/linux/mm.h +5 -5
@@ -241,7 +241,7 @@ struct page {* see PAGE_MAPPING_ANON below. */ }; -#if NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS +#if (NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS ) || defined(CONFIG_PTSHARE )spinlock_t ptl;#endif };
@@ -814,19 +814,19 @@ static inline pmd_t *pmd_alloc(struct mm }#endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */ -#if NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS +#if (NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS ) || defined(CONFIG_PTSHARE )/* * We tuck a spinlock to guard each pagetable page into its struct page, * at page->private, with BUILD_BUG_ON to make sure that this will not * overflow into the next struct page (as it might with DEBUG_SPINLOCK). * When freeing, reset page->mapping so free_pages_check won't complain. */ -#define __pte_lockptr (page) &((page)->ptl) +#define __pt_lockptr (page) &((page)->ptl)#define pte_lock_init (_page) do { \ - spin_lock_init(__pte_lockptr(_page)); \ + spin_lock_init(__pt_lockptr(_page)); \ } while (0)#define pte_lock_deinit (page) ((page)->mapping = NULL) -#define pte_lockptr (mm, pmd) ({(void)(mm); __pte_lockptr(pmd_page(*(pmd)));}) +#define pte_lockptr (mm, pmd) ({(void)(mm); __pt_lockptr(pmd_page(*(pmd)));})#else /* * We use mm->page_table_lock to guard all pagetable pages of the mm.
./include/linux/rmap.h +1 -1
@@ -98,7 +98,7 @@ void remove_from_swap(struct page *page)* Called from mm/filemap_xip.c to unmap empty zero page */ pte_t *page_check_address (struct page *,struct mm_struct *, -unsigned long ,spinlock_t **); +unsigned long ,spinlock_t **,int *);/* * Used by swapoff to help locate where page is expected in vma.
./include/linux/sched.h +10 -1
@@ -254,7 +254,7 @@ arch_get_unmapped_area_topdown(struct fiextern void arch_unmap_area (struct mm_struct *,unsigned long );extern void arch_unmap_area_topdown (struct mm_struct *,unsigned long ); -#if NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS +#if (NR_CPUS >=CONFIG_SPLIT_PTLOCK_CPUS ) || defined(CONFIG_PTSHARE )/* * The mm counters are not protected by its page_table_lock, * so must be incremented atomically.
@@ -316,6 +316,15 @@ struct mm_struct {* by mmlist_lock */ +/* +* This address range is set during operations that modify memory +* regions. It protects against page tables being shared in this +* range while the vma is in transition. Only one range is needed +* because it will only be set while mmap_sem is held for write. +*/ +unsigned long pt_trans_start; +unsigned long pt_trans_end; +/* Special counters, in some configurations protected by the * page_table_lock, in other configurations by being atomic. */
./include/linux/ptshare.h +189 -0
189 lines not on this page · raw message
./mm/Makefile +1 -0
@@ -23,4 +23,5 @@ obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_FS_XIP) += filemap_xip.o obj-$(CONFIG_MIGRATION) += migrate.o +obj-$(CONFIG_PTSHARE) += ptshare.o
./mm/filemap_xip.c +2 -1
@@ -174,6 +174,7 @@ __xip_unmap (struct address_space * mappunsigned long address;pte_t *pte;pte_t pteval; +int shared;spinlock_t *ptl;struct page *page;
@@ -184,7 +185,7 @@ __xip_unmap (struct address_space * mapp ((pgoff - vma->vm_pgoff) <<PAGE_SHIFT );BUG_ON (address < vma->vm_start || address >= vma->vm_end); page =ZERO_PAGE (address); - pte =page_check_address (page, mm, address, &ptl); + pte =page_check_address (page, mm, address, &ptl, &shared);if (pte) {/* Nuke the page table entry. */ flush_cache_page (vma, address,pte_pfn (*pte));
./mm/fremap.c +4 -0
@@ -15,6 +15,7 @@#include <linux/rmap.h> #include <linux/module.h> #include <linux/syscalls.h> +#include <linux/ptshare.h> #include <asm/mmu_context.h> #include <asm/cacheflush.h>
@@ -193,6 +194,8 @@ asmlinkage long sys_remap_file_pages(uns has_write_lock =1 ; goto retry; } +pt_unshare_lock_range (mm, vma->vm_start, vma->vm_end); + mapping = vma->vm_file->f_mapping;spin_lock (&mapping->i_mmap_lock);flush_dcache_mmap_lock (mapping);
@@ -201,6 +204,7 @@ asmlinkage long sys_remap_file_pages(unsvma_nonlinear_insert (vma, &mapping->i_mmap_nonlinear);flush_dcache_mmap_unlock (mapping);spin_unlock (&mapping->i_mmap_lock); +pt_unlock_range (mm); } err = vma->vm_ops->populate(vma, start, size,
./mm/hugetlb.c +50 -20
173 lines not on this page · raw message
./mm/memory.c +30 -7
@@ -48,6 +48,7 @@#include <linux/rmap.h> #include <linux/module.h> #include <linux/init.h> +#include <linux/ptshare.h> #include <asm/pgalloc.h> #include <asm/uaccess.h>
@@ -144,6 +145,10 @@ static inline void free_pmd_range(struct next =pmd_addr_end (addr, end);if (pmd_none_or_clear_bad (pmd))continue ; +if (pt_is_shared_pte (*pmd)) { +if (pt_check_unshare_pte (tlb->mm, addr, pmd)) +continue ; + }free_pte_range (tlb, pmd); }while (pmd++, addr = next, addr != end);
@@ -160,6 +165,7 @@ static inline void free_pmd_range(struct pmd =pmd_offset (pud, start);pud_clear (pud); +pmd_lock_deinit (pmd);pmd_free_tlb (tlb, pmd); }
@@ -177,6 +183,10 @@ static inline void free_pud_range(struct next =pud_addr_end (addr, end);if (pud_none_or_clear_bad (pud))continue ; +if (pt_is_shared_pmd (*pud)) { +if (pt_check_unshare_pmd (tlb->mm, addr, pud)) +continue ; + }free_pmd_range (tlb, pud, addr, next, floor, ceiling); }while (pud++, addr = next, addr != end);
@@ -301,20 +311,21 @@ void free_pgtables(struct mmu_gather **tint __pte_alloc (struct mm_struct *mm,pmd_t *pmd,unsigned long address) {struct page *new =pte_alloc_one (mm, address); +spinlock_t *ptl =pmd_lockptr (mm, pmd); +if (!new)return -ENOMEM ;pte_lock_init (new); -spin_lock (&mm->page_table_lock); +spin_lock (ptl);if (pmd_present (*pmd)) {/* Another has populated it */ pte_lock_deinit (new);pte_free (new); }else { - mm->nr_ptes++;inc_page_state (nr_page_table_pages);pmd_populate (mm, pmd, new); } -spin_unlock (&mm->page_table_lock); +spin_unlock (ptl);return 0 ; }
@@ -613,6 +624,10 @@ static unsigned long zap_pte_range(strucint file_rss =0 ;int anon_rss =0 ; +if (pt_is_shared_pte (*pmd)) { +if (pt_check_unshare_pte (mm, addr, pmd)) +return end; + } pte =pte_offset_map_lock (mm, pmd, addr, &ptl); do { pte_t ptent = *pte;
@@ -694,6 +709,10 @@ static inline unsigned long zap_pmd_rangunsigned long next; pmd =pmd_offset (pud, addr); +if (pt_is_shared_pmd (*pud)) { +if (pt_check_unshare_pmd (tlb->mm, addr, pud)) +return end; + } do { next = pmd_addr_end(addr, end); if (pmd_none_or_clear_bad(pmd)) {
@@ -2272,10 +2291,10 @@ int __handle_mm_fault(struct mm_struct * pud =pud_alloc (mm, pgd, address);if (!pud)return VM_FAULT_OOM ; - pmd =pmd_alloc (mm, pud, address); + pmd =pt_share_pmd (vma, address, pud);if (!pmd)return VM_FAULT_OOM ; - pte =pte_alloc_map (mm, pmd, address); + pte =pt_share_pte (vma, address, pmd);if (!pte)return VM_FAULT_OOM ;
@@ -2326,13 +2345,17 @@ int __pmd_alloc(struct mm_struct *mm, pu#ifndef __ARCH_HAS_4LEVEL_HACKif (pud_present (*pud))/* Another has populated it */ pmd_free (new); -else +else { +pmd_lock_init (new);pud_populate (mm, pud, new); + }#else if (pgd_present (*pud))/* Another has populated it */ pmd_free (new); -else +else { +pmd_lock_init (new);pgd_populate (mm, pud, new); + }#endif /* __ARCH_HAS_4LEVEL_HACK */ spin_unlock (&mm->page_table_lock);return 0 ;
./mm/mmap.c +0 -1
@@ -1959,7 +1959,6 @@ void exit_mmap(struct mm_struct *mm)while (vma) vma =remove_vma (vma); -BUG_ON (mm->nr_ptes > (FIRST_USER_ADDRESS +PMD_SIZE -1 )>>PMD_SHIFT ); }/* Insert vm structure into process list sorted by address
./mm/mprotect.c +8 -1
@@ -19,6 +19,7 @@#include <linux/mempolicy.h> #include <linux/personality.h> #include <linux/syscalls.h> +#include <linux/ptshare.h> #include <asm/uaccess.h> #include <asm/pgtable.h>
@@ -115,6 +116,8 @@ mprotect_fixup(struct vm_area_struct *vmreturn 0 ; } +pt_unshare_lock_range (mm, start, end); +/* * If we make a private mapping writable we increase our commit; * but (without finer accounting) cannot reduce our commit if we
@@ -126,8 +129,10 @@ mprotect_fixup(struct vm_area_struct *vmif (newflags &VM_WRITE ) {if (!(oldflags & (VM_ACCOUNT |VM_WRITE |VM_SHARED ))) { charged = nrpages; -if (security_vm_enough_memory (charged)) +if (security_vm_enough_memory (charged)) { +pt_unlock_range (mm);return -ENOMEM ; + } newflags |=VM_ACCOUNT ; } }
@@ -172,9 +177,11 @@ success:change_protection (vma, start, end, newprot);vm_stat_account (mm, oldflags, vma->vm_file, -nrpages);vm_stat_account (mm, newflags, vma->vm_file, nrpages); +pt_unlock_range (mm);return 0 ; fail: +pt_unlock_range (mm);vm_unacct_memory (charged);return error; }
./mm/mremap.c +5 -0
@@ -18,6 +18,7 @@#include <linux/highmem.h> #include <linux/security.h> #include <linux/syscalls.h> +#include <linux/ptshare.h> #include <asm/uaccess.h> #include <asm/cacheflush.h>
@@ -178,6 +179,8 @@ static unsigned long move_vma(struct vm_if (!new_vma)return -ENOMEM ; +pt_unshare_lock_range (mm, old_addr, old_addr + old_len); + moved_len =move_page_tables (vma, old_addr, new_vma, new_addr, old_len);if (moved_len < old_len) {/*
@@ -221,6 +224,8 @@ static unsigned long move_vma(struct vm_ } mm->hiwater_vm = hiwater_vm; +pt_unlock_range (mm); +/* Restore VM_ACCOUNT if one or two pieces of vma left */ if (excess) { vma->vm_flags |=VM_ACCOUNT ;
./mm/rmap.c +17 -4
@@ -53,6 +53,7 @@#include <linux/rmap.h> #include <linux/rcupdate.h> #include <linux/module.h> +#include <linux/ptshare.h> #include <asm/tlbflush.h>
@@ -286,7 +287,8 @@ unsigned long page_address_in_vma(struct* On success returns with pte mapped and locked. */ pte_t *page_check_address (struct page *page,struct mm_struct *mm, -unsigned long address,spinlock_t **ptlp) +unsigned long address,spinlock_t **ptlp, +int *shared) {pgd_t *pgd;pud_t *pud;
@@ -306,6 +308,9 @@ pte_t *page_check_address(struct page *pif (!pmd_present (*pmd))return NULL ; +if (pt_is_shared_pmd (*pud)) + *shared++; + pte =pte_offset_map (pmd, address);/* Make a quick check before getting the lock */ if (!pte_present (*pte)) {
@@ -313,6 +318,9 @@ pte_t *page_check_address(struct page *preturn NULL ; } +if (pt_is_shared_pte (*pmd)) + *shared++; + ptl =pte_lockptr (mm, pmd);spin_lock (ptl);if (pte_present (*pte) &&page_to_pfn (page) ==pte_pfn (*pte)) {
@@ -333,6 +341,7 @@ static int page_referenced_one(struct pastruct mm_struct *mm = vma->vm_mm;unsigned long address;pte_t *pte; +int shared;spinlock_t *ptl;int referenced =0 ;
@@ -340,7 +349,7 @@ static int page_referenced_one(struct paif (address == -EFAULT ) goto out; - pte =page_check_address (page, mm, address, &ptl); + pte =page_check_address (page, mm, address, &ptl, &shared);if (!pte) goto out;
@@ -584,6 +593,7 @@ static int try_to_unmap_one(struct pageunsigned long address;pte_t *pte;pte_t pteval; +int shared =0 ;spinlock_t *ptl;int ret =SWAP_AGAIN ;
@@ -591,7 +601,7 @@ static int try_to_unmap_one(struct pageif (address == -EFAULT ) goto out; - pte =page_check_address (page, mm, address, &ptl); + pte =page_check_address (page, mm, address, &ptl, &shared);if (!pte) goto out;
@@ -609,7 +619,10 @@ static int try_to_unmap_one(struct page/* Nuke the page table entry. */ flush_cache_page (vma, address,page_to_pfn (page)); - pteval =ptep_clear_flush (vma, address, pte); +if (shared) + pteval =ptep_clear_flush_all (vma, address, pte); +else + pteval =ptep_clear_flush (vma, address, pte);/* Move the dirty bit to the physical page now the pte is gone. */ if (pte_dirty (pteval))
./mm/ptshare.c +503 -0
503 lines not on this page · raw message
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>