Skip to contentSkip to conversation

new snapshot of shared page tables

PATCH In review Dave McCracken 4 participants 8 messages Oct 9, 2002 – Oct 12, 2002

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 new snapshot of shared page tables

Here's the latest shared page table patch. Changes are mostly cleanups, with the added feature that shared page tables are now a config option. This means the patch should build on other architectures (this is untested).

At Andrew Morton's request, I've moved my development base to the -mm line.

Dave McCracken

======================================================================
Dave McCracken IBM Linux Base Kernel Team 1-512-838-3059
[email protected] T/L 678-3059

./include/linux/mm.h +3 -0
@@ -163,6 +163,8 @@
 		struct pte_chain *chain;/* Reverse pte mapping pointer.
 					 * protected by PG_chainlock */
 		pte_addr_t direct;
+		struct mm_chain *mmchain;/* Reverse mm_struct mapping pointer */
+		struct mm_struct *mmdirect;
 	} pte;
 	unsigned long private;		/* mapping-private opaque data */
 
@@ -358,6 +360,7 @@
 
 extern void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size);
 extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
+extern int share_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma, pmd_t **prev_pmd);
 extern int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long to, unsigned long size, pgprot_t prot);
 extern int zeromap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long size, pgprot_t prot);
 
./include/linux/rmap-locking.h +3 -0
@@ -31,3 +31,6 @@
 #endif
 	preempt_enable();
 }
+
+#define	pte_page_lock	pte_chain_lock
+#define	pte_page_unlock	pte_chain_unlock
./include/asm-generic/rmap.h +6 -27
@@ -26,33 +26,6 @@
  */
 #include <linux/mm.h>
 
-static inline void pgtable_add_rmap(struct page * page, struct mm_struct * mm, unsigned long address)
-{
-#ifdef BROKEN_PPC_PTE_ALLOC_ONE
-	/* OK, so PPC calls pte_alloc() before mem_map[] is setup ... ;( */
-	extern int mem_init_done;
-
-	if (!mem_init_done)
-		return;
-#endif
-	page->mapping = (void *)mm;
-	page->index = address & ~((PTRS_PER_PTE * PAGE_SIZE) - 1);
-	inc_page_state(nr_page_table_pages);
-}
-
-static inline void pgtable_remove_rmap(struct page * page)
-{
-	page->mapping = NULL;
-	page->index = 0;
-	dec_page_state(nr_page_table_pages);
-}
-
-static inline struct mm_struct * ptep_to_mm(pte_t * ptep)
-{
-	struct page * page = kmap_atomic_to_page(ptep);
-	return (struct mm_struct *) page->mapping;
-}
-
 static inline unsigned long ptep_to_address(pte_t * ptep)
 {
 	struct page * page = kmap_atomic_to_page(ptep);
@@ -87,4 +60,10 @@
 }
 #endif
 
+extern void pgtable_add_rmap(struct page * page, struct mm_struct * mm, unsigned long address);
+extern void pgtable_add_rmap_locked(struct page * page, struct mm_struct * mm, unsigned long address);
+extern void pgtable_remove_rmap(struct page * page, struct mm_struct *mm);
+extern void pgtable_remove_rmap_locked(struct page * page, struct mm_struct *mm);
+extern void increment_rss(struct page *ptepage);
+
 #endif /* _GENERIC_RMAP_H */
./include/asm-i386/pgtable.h +9 -2
@@ -124,6 +124,7 @@
 #define _PAGE_PROTNONE	0x080	/* If not present */
 
 #define _PAGE_TABLE	(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
+#define _PAGE_TABLE_RDONLY	(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
 #define _KERNPG_TABLE	(_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
 #define _PAGE_CHG_MASK	(PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
 
@@ -184,8 +185,8 @@
 #define pmd_none(x)	(!pmd_val(x))
 #define pmd_present(x)	(pmd_val(x) & _PAGE_PRESENT)
 #define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
-#define	pmd_bad(x)	((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
-
+#define	pmd_bad(x)	((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER & ~_PAGE_RW)) != \
+			(_KERNPG_TABLE & ~_PAGE_RW))
 
 #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))
 
@@ -209,6 +210,8 @@
 static inline pte_t pte_mkdirty(pte_t pte)	{ (pte).pte_low |= _PAGE_DIRTY; return pte; }
 static inline pte_t pte_mkyoung(pte_t pte)	{ (pte).pte_low |= _PAGE_ACCESSED; return pte; }
 static inline pte_t pte_mkwrite(pte_t pte)	{ (pte).pte_low |= _PAGE_RW; return pte; }
+static inline int pmd_write(pmd_t pmd)		{ return (pmd).pmd & _PAGE_RW; }
+static inline pmd_t pmd_wrprotect(pmd_t pmd)	{ (pmd).pmd &= ~_PAGE_RW; return pmd; }
 
 static inline  int ptep_test_and_clear_dirty(pte_t *ptep)	{ return test_and_clear_bit(_PAGE_BIT_DIRTY, &ptep->pte_low); }
 static inline  int ptep_test_and_clear_young(pte_t *ptep)	{ return test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte_low); }
@@ -263,6 +266,10 @@
 	((pte_t *)kmap_atomic(pmd_page(*(dir)),KM_PTE0) + __pte_offset(address))
 #define pte_offset_map_nested(dir, address) \
 	((pte_t *)kmap_atomic(pmd_page(*(dir)),KM_PTE1) + __pte_offset(address))
+#define pte_page_map(__page, address) \
+	((pte_t *)kmap_atomic(__page,KM_PTE0) + __pte_offset(address))
+#define pte_page_map_nested(__page, address) \
+	((pte_t *)kmap_atomic(__page,KM_PTE1) + __pte_offset(address))
 #define pte_unmap(pte) kunmap_atomic(pte, KM_PTE0)
 #define pte_unmap_nested(pte) kunmap_atomic(pte, KM_PTE1)
 
./arch/i386/Config.help +7 -0
@@ -143,6 +143,13 @@
   low memory.  Setting this option will put user-space page table
   entries in high memory.
 
+CONFIG_SHAREPTE
+  Normally each address space has its own complete page table for all
+  its mappings.  This can mean many mappings of a set of shared data
+  pages.  With this option, the VM will attempt to share the bottom
+  level of the page table between address spaces that are sharing data
+  pages.
+
 CONFIG_HIGHMEM4G
   Select this if you have a 32-bit processor and between 1 and 4
   gigabytes of physical RAM.
./arch/i386/config.in +1 -0
@@ -232,6 +232,7 @@
 if [ "$CONFIG_HIGHMEM4G" = "y" -o "$CONFIG_HIGHMEM64G" = "y" ]; then
    bool 'Allocate 3rd-level pagetables from highmem' CONFIG_HIGHPTE
 fi
+bool 'Share 3rd-level pagetables' CONFIG_SHAREPTE y
 
 bool 'Math emulation' CONFIG_MATH_EMULATION
 bool 'MTRR (Memory Type Range Register) support' CONFIG_MTRR
./fs/exec.c +2 -1
@@ -46,6 +46,7 @@
 #include <asm/uaccess.h>
 #include <asm/pgalloc.h>
 #include <asm/mmu_context.h>
+#include <asm/rmap.h>
 
 #ifdef CONFIG_KMOD
 #include <linux/kmod.h>
@@ -308,8 +309,8 @@
 	flush_page_to_ram(page);
 	set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
 	page_add_rmap(page, pte);
+	increment_rss(kmap_atomic_to_page(pte));
 	pte_unmap(pte);
-	tsk->mm->rss++;
 	spin_unlock(&tsk->mm->page_table_lock);
 
 	/* no need for flush_tlb */
./kernel/fork.c +7 -0
@@ -208,6 +208,9 @@
 	struct vm_area_struct * mpnt, *tmp, **pprev;
 	int retval;
 	unsigned long charge = 0;
+#ifdef CONFIG_SHAREPTE
+	pmd_t *prev_pmd = 0;
+#endif
 
 	flush_cache_mm(current->mm);
 	mm->locked_vm = 0;
@@ -270,7 +273,11 @@
 		*pprev = tmp;
 		pprev = &tmp->vm_next;
 		mm->map_count++;
+#ifdef CONFIG_SHAREPTE
+		retval = share_page_range(mm, current->mm, tmp, &prev_pmd);
+#else
 		retval = copy_page_range(mm, current->mm, tmp);
+#endif
 		spin_unlock(&mm->page_table_lock);
 
 		if (tmp->vm_ops && tmp->vm_ops->open)
./mm/mmap.c +18 -3
@@ -23,6 +23,7 @@
 #include <asm/tlb.h>
 
 extern void unmap_page_range(mmu_gather_t *,struct vm_area_struct *vma, unsigned long address, unsigned long size);
+extern void unmap_all_pages(mmu_gather_t *tlb, struct mm_struct *mm, unsigned long address, unsigned long end);
 extern void clear_page_tables(mmu_gather_t *tlb, unsigned long first, int nr);
 
 /*
@@ -1248,11 +1249,22 @@
 	}
 }
 
+/*
+ * For small tasks, it's most efficient to unmap the pages for each
+ * vma.  For larger tasks, it's better to just walk the entire address
+ * space in one pass, particularly with shared pte pages.  This
+ * threshold determines the size where we switch from one method to
+ * the other.
+ */
+
+#define	UNMAP_THRESHOLD		500
+
 /* Release all mmaps. */
 void exit_mmap(struct mm_struct * mm)
 {
 	mmu_gather_t *tlb;
 	struct vm_area_struct * mpnt;
+	int unmap_vma = mm->total_vm < UNMAP_THRESHOLD;
 
 	release_segments(mm);
 	spin_lock(&mm->page_table_lock);
@@ -1273,10 +1285,10 @@
 			vm_unacct_memory((end - start) >> PAGE_SHIFT);
 
 		mm->map_count--;
-		if (!(is_vm_hugetlb_page(mpnt)))
-			unmap_page_range(tlb, mpnt, start, end);
-		else
+		if (is_vm_hugetlb_page(mpnt))
 			mpnt->vm_ops->close(mpnt);
+		else if (unmap_vma)
+			unmap_page_range(tlb, mpnt, start, end);
 		mpnt = mpnt->vm_next;
 	}
 
@@ -1284,6 +1296,9 @@
 	if (mm->map_count)
 		BUG();
 
+	if (!unmap_vma)
+		unmap_all_pages(tlb, mm, 0, TASK_SIZE);
+
 	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
 	tlb_finish_mmu(tlb, 0, TASK_SIZE);
 
./mm/swapfile.c +4 -3
@@ -17,6 +17,7 @@
 #include <linux/buffer_head.h>
 
 #include <asm/pgtable.h>
+#include <asm/rmap.h>
 #include <linux/swapops.h>
 
 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
@@ -371,7 +372,7 @@
  */
 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
 static inline void unuse_pte(struct vm_area_struct * vma, unsigned long address,
-	pte_t *dir, swp_entry_t entry, struct page* page)
+	pte_t *dir, swp_entry_t entry, struct page* page, pmd_t *pmd)
 {
 	pte_t pte = *dir;
 
@@ -383,7 +384,7 @@
 	set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
 	page_add_rmap(page, dir);
 	swap_free(entry);
-	++vma->vm_mm->rss;
+	increment_rss(pmd_page(*pmd));
 }
 
 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
@@ -408,7 +409,7 @@
 	if (end > PMD_SIZE)
 		end = PMD_SIZE;
 	do {
-		unuse_pte(vma, offset+address-vma->vm_start, pte, entry, page);
+		unuse_pte(vma, offset+address-vma->vm_start, pte, entry, page, dir);
 		address += PAGE_SIZE;
 		pte++;
 	} while (address && (address < end));
./mm/memory.c +409 -35

684 lines not on this page · raw message

./mm/rmap.c +235 -34

345 lines not on this page · raw message

#1 lore raw
Quoted 2 lines · Dave McCracken

with the added feature that shared page tables are now a config option.

Good idea, that. The ppc64 guys (guy) don't actually want the feature,
and it significantly de-risks things. Although it's one more datapoint to be gathered when pondering oops reports.

Stylistic trivia: When stubbing out a function it's cleaner (and faster) to do:

#ifdef CONFIG_FOO
int my_function(arg1, arg2)

{
	...
}
#else
static inline int my_function(arg1, arg2)
{
	return 0;
}

#endif

Do we have any performance numbers on this yet? Both for speed and space?
--
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/

#2 lore raw

--On Wednesday, October 09, 2002 15:51:15 -0700 Andrew Morton

Quoted 14 lines · <[email protected]> wrote:

Stylistic trivia: When stubbing out a function it's cleaner (and faster) to do:

# ifdef CONFIG_FOO
int my_function(arg1, arg2)
{
	...
}
# else
static inline int my_function(arg1, arg2)
{
	return 0;
}
# endif

I'll go one better and remove the function and the references to it, if that's cleaner. It feels cleaner to me. Here's a patch that does it, on top of 2.5.41-mm2.

Dave

======================================================================
Dave McCracken IBM Linux Base Kernel Team 1-512-838-3059
[email protected] T/L 678-3059

./include/linux/mm.h +4 -1
@@ -359,8 +359,11 @@
 extern int shmem_zero_setup(struct vm_area_struct *);
 
 extern void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size);
-extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
+#ifdef CONFIG_SHAREPTE
 extern int share_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma, pmd_t **prev_pmd);
+#else
+extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
+#endif
 extern int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long to, unsigned long size, pgprot_t prot);
 extern int zeromap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long size, pgprot_t prot);
 
./mm/memory.c +14 -12
@@ -153,6 +153,7 @@
 	} while (--nr);
 }
 
+#ifdef CONFIG_SHAREPTE
 /*
  * This function makes the decision whether a pte page needs to be unshared
  * or not.  Note that page_count() == 1 isn't even tested here.  The assumption
@@ -166,7 +167,6 @@
 static inline int pte_needs_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
 				    pmd_t *pmd, unsigned long address, int write_access)
 {
-#ifdef CONFIG_SHAREPTE
 	struct page *ptepage;
 
 	/* It's not even there, nothing to unshare. */
@@ -198,9 +198,6 @@
 	 * Ok, we have to unshare.
 	 */
 	return 1;
-#else
-	return 0;
-#endif
 }
 
 /*
@@ -223,7 +220,6 @@
 
 static pte_t *pte_unshare(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
 {
-#ifdef CONFIG_SHAREPTE
 	pte_t	*src_ptb, *dst_ptb;
 	struct page *oldpage, *newpage, *tmppage;
 	struct vm_area_struct *vma;
@@ -359,9 +355,9 @@
 	return dst_ptb + __pte_offset(address);
 
 out_map:
-#endif
 	return pte_offset_map(pmd, address);
 }
+#endif
 
 pte_t * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
 {
@@ -417,10 +413,10 @@
 #define PTE_TABLE_MASK	((PTRS_PER_PTE-1) * sizeof(pte_t))
 #define PMD_TABLE_MASK	((PTRS_PER_PMD-1) * sizeof(pmd_t))
 
+#ifdef CONFIG_SHAREPTE
 int share_page_range(struct mm_struct *dst, struct mm_struct *src,
 	struct vm_area_struct *vma, pmd_t **prev_pmd)
 {
-#ifdef CONFIG_SHAREPTE
 	pgd_t *src_pgd, *dst_pgd;
 	unsigned long address = vma->vm_start;
 	unsigned long end = vma->vm_end;
@@ -505,10 +501,9 @@
 out:
 	return 0;
 nomem:
-#endif
 	return -ENOMEM;
 }
-
+#else
 /*
  * copy one vm_area from one task to the other. Assumes the page tables
  * already present in the new task to be cleared in the whole range
@@ -640,6 +635,7 @@
 nomem:
 	return -ENOMEM;
 }
+#endif
 
 static void zap_pte_range(mmu_gather_t *tlb, pmd_t * pmd, unsigned long address, unsigned long size)
 {
@@ -668,6 +664,7 @@
 	 */
 	ptepage = pmd_page(*pmd);
 	pte_page_lock(ptepage);
+#ifdef CONFIG_SHAREPTE
 	if (page_count(ptepage) > 1) {
 		if ((offset == 0) && (size == PMD_SIZE)) {
 			pmd_clear(pmd);
@@ -679,9 +676,10 @@
 		}
 		ptep = pte_unshare(tlb->mm, pmd, address);
 		ptepage = pmd_page(*pmd);
-	} else {
+	} else
+#endif
 		ptep = pte_offset_map(pmd, address);
-	}
+
 	for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
 		pte_t pte = *ptep;
 		if (pte_none(pte))
@@ -1839,6 +1837,7 @@
 	if (pmd) {
 		pte_t * pte;
 
+#ifdef CONFIG_SHAREPTE
 		if (pte_needs_unshare(mm, vma, pmd, address, write_access)) {
 			pte_page_lock(pmd_page(*pmd));
 			pte = pte_unshare(mm, pmd, address);
@@ -1846,7 +1845,10 @@
 			pte = pte_alloc_map(mm, pmd, address);
 			pte_page_lock(pmd_page(*pmd));
 		}
-
+#else
+		pte = pte_alloc_map(mm, pmd, address);
+		pte_page_lock(pmd_page(*pmd));
+#endif
 		if (pte) {
 			spin_unlock(&mm->page_table_lock);
 			return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
#3 lore raw
In reply to Dave McCracken
Quoted 6 lines · Dave McCracken

Here's the latest shared page table patch. Changes are mostly cleanups, with the added feature that shared page tables are now a config option. This means the patch should build on other architectures (this is untested).

At Andrew Morton's request, I've moved my development base to the -mm line.

After realizing (thanks Dave) that kmail 3.03 has a bug saving multipart/mixed mime messages, I was able to use uudeview to extract a clean patch, and build kernel which boot fine. Thats the good news.

When I try to start kde 3.03 on an up to date debian sid (X 4.2 etc) kde fails to start. It complains that ksmserver cannot be started. Same setup works with 41-mm1.

Know this is not a meaty report. With X4.2 I have not yet figgered out how to get more debug messages (the log from xstart is anemic) nor is there anything in messages, kern.log or on the serial console. The box is a K6-III 400 on a via MVP3 chipset.

What other info can I gather?

Ed Tomlinson
--
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/

#4 lore raw
Quoted 4 lines · Ed Tomlinson

After realizing (thanks Dave) that kmail 3.03 has a bug saving multipart/mixed mime messages, I was able to use uudeview to extract a clean patch, and build kernel which boot fine. Thats the good news. When I try to start kde 3.03 on an up to date debian sid (X 4.2 etc)

Quoted 8 lines

kde fails to start. It complains that ksmserver cannot be started. Same setup works with 41-mm1.
Know this is not a meaty report. With X4.2 I have not yet figgered out how to get more debug messages (the log from xstart is anemic) nor is there anything in messages, kern.log or on the serial console. The box is a K6-III 400 on a via MVP3 chipset.
What other info can I gather?
Ed Tomlinson

Could you strace ksmserver on a working and non-working console and (privately) send (probably large) logs to dmc & me? Please use strace -f -ff or some equivalent that follows children.

I'll try to reproduce something locally.

Thanks,
Bill
--
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/

#5 lore raw
Quoted 14 lines · William Lee Irwin III
Quoted 4 lines · Ed Tomlinson

After realizing (thanks Dave) that kmail 3.03 has a bug saving multipart/mixed mime messages, I was able to use uudeview to extract a clean patch, and build kernel which boot fine. Thats the good news. When I try to start kde 3.03 on an up to date debian sid (X 4.2 etc)

Quoted 8 lines

kde fails to start. It complains that ksmserver cannot be started. Same setup works with 41-mm1.
Know this is not a meaty report. With X4.2 I have not yet figgered out how to get more debug messages (the log from xstart is anemic) nor is there anything in messages, kern.log or on the serial console. The box is a K6-III 400 on a via MVP3 chipset.
What other info can I gather?
Ed Tomlinson

Quoted 3 lines

Could you strace ksmserver on a working and non-working console and (privately) send (probably large) logs to dmc & me? Please use strace -f -ff or some equivalent that follows children.

Hope the straces helped...

I tried again this evening with mm2 plus shpte-2.5.41-mm2-1.diff and shpte-2.5.41-mm2-2.diff and still get the same error.

Ed
--
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/

#6 lore raw
Quoted 15 lines · Ed Tomlinson
Quoted 14 lines · William Lee Irwin III
Quoted 12 lines · Ed Tomlinson

After realizing (thanks Dave) that kmail 3.03 has a bug saving multipart/mixed mime messages, I was able to use uudeview to extract a clean patch, and build kernel which boot fine. Thats the good news. When I try to start kde 3.03 on an up to date debian sid (X 4.2 etc) kde fails to start. It complains that ksmserver cannot be started. Same setup works with 41-mm1.
Know this is not a meaty report. With X4.2 I have not yet figgered out how to get more debug messages (the log from xstart is anemic) nor is there anything in messages, kern.log or on the serial console. The box is a K6-III 400 on a via MVP3 chipset.
What other info can I gather?
Ed Tomlinson

Quoted 8 lines
Quoted 3 lines

Could you strace ksmserver on a working and non-working console and (privately) send (probably large) logs to dmc & me? Please use strace -f -ff or some equivalent that follows children.

Hope the straces helped...

I tried again this evening with mm2 plus shpte-2.5.41-mm2-1.diff and shpte-2.5.41-mm2-2.diff and still get the same error.

And again with 2.5.42-mm2 - still no joy. Errror looks the same here. Do you want another set of traces?

Ed

--
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/