Skip to contentSkip to conversation

Snapshot of shared page tables

PATCH In review Dave McCracken 3 participants 6 messages Oct 2, 2002

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

PATCH 1/1 Snapshot of shared page tables

Ok, here it is. This patch works for my simple tests, both under UP and SMP, including under memory pressure. I'd appreciate anyone who'd like to take it and beat on it. Please let me know of any problems you find.

The patch is against this morning's 2.5 BK tree.

Dave McCracken

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

# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.674 -> 1.675
# include/linux/mm.h 1.83.1.2 -> 1.86
# include/asm-i386/pgalloc.h 1.16.1.2 -> 1.19
# kernel/fork.c 1.70.1.15 -> 1.76
# Makefile 1.293.1.22 -> 1.302
# include/linux/rmap-locking.h 1.1 -> 1.2
# init/main.c 1.64.1.6 -> 1.69
# mm/swapfile.c 1.54.1.2 -> 1.58
# fs/exec.c 1.45.1.4 -> 1.49
# mm/memory.c 1.84.1.6 -> 1.92
# include/asm-generic/rmap.h 1.2.1.1 -> 1.6
# include/linux/page-flags.h 1.24.1.1 -> 1.27
# include/asm-i386/pgtable.h 1.17.1.3 -> 1.20
# mm/rmap.c 1.12.1.4 -> 1.20
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/10/02 [email protected] 1.663.2.2
# Check in working snapshot
# --------------------------------------------
# 02/10/02 [email protected] 1.675
# Merge baldur.austin.ibm.com:/home/dmc/linux/bk/linux-2.5

# into baldur.austin.ibm.com:/home/dmc/linux/bk/linux-2.5-shpte
# --------------------------------------------
#
diff -Nru a/Makefile b/Makefile
--- a/Makefile	Wed Oct  2 09:47:17 2002
+++ b/Makefile	Wed Oct  2 09:47:17 2002
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 5
 SUBLEVEL = 40
-EXTRAVERSION =
+EXTRAVERSION =-shpte

# *DOCUMENTATION*
# Too see a list of typical targets execute "make help"

fs/exec.c +1 -1
diff -Nru a/fs/exec.c b/fs/exec.c
@@ -308,8 +308,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(virt_to_page(pte));
 	pte_unmap(pte);
-	tsk->mm->rss++;
 	spin_unlock(&tsk->mm->page_table_lock);
 
 	/* no need for flush_tlb */
include/asm-generic/rmap.h +6 -27
diff -Nru a/include/asm-generic/rmap.h b/include/asm-generic/rmap.h
@@ -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);
@@ -86,5 +59,11 @@
 	return;
 }
 #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/pgalloc.h +7 -0
diff -Nru a/include/asm-i386/pgalloc.h b/include/asm-i386/pgalloc.h
@@ -16,6 +16,13 @@
 		((unsigned long long)page_to_pfn(pte) <<
 			(unsigned long long) PAGE_SHIFT)));
 }
+
+static inline void pmd_populate_rdonly(struct mm_struct *mm, pmd_t *pmd, struct page *page)
+{
+	set_pmd(pmd, __pmd(_PAGE_TABLE_RDONLY +
+		((unsigned long long)page_to_pfn(page) <<
+			(unsigned long long) PAGE_SHIFT)));
+}
 /*
  * Allocate and free page tables.
  */
include/asm-i386/pgtable.h +9 -2
diff -Nru a/include/asm-i386/pgtable.h b/include/asm-i386/pgtable.h
@@ -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)
 
include/linux/mm.h +2 -0
diff -Nru a/include/linux/mm.h b/include/linux/mm.h
@@ -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 */
 
include/linux/rmap-locking.h +3 -0
diff -Nru a/include/linux/rmap-locking.h b/include/linux/rmap-locking.h
@@ -31,3 +31,6 @@
 #endif
 	preempt_enable();
 }
+
+#define	pte_page_lock	pte_chain_lock
+#define	pte_page_unlock	pte_chain_unlock
init/main.c +1 -3
diff -Nru a/init/main.c b/init/main.c
@@ -511,9 +511,7 @@
 static void do_pre_smp_initcalls(void)
 {
 	extern int spawn_ksoftirqd(void);
-#ifdef CONFIG_SMP
-	extern int migration_init(void);
-
+#if CONFIG_SMP
 	migration_init();
 #endif
 	spawn_ksoftirqd();
kernel/fork.c +2 -1
diff -Nru a/kernel/fork.c b/kernel/fork.c
@@ -205,6 +205,7 @@
 	struct vm_area_struct * mpnt, *tmp, **pprev;
 	int retval;
 	unsigned long charge = 0;
+	pmd_t *prev_pmd = 0;
 
 	flush_cache_mm(current->mm);
 	mm->locked_vm = 0;
@@ -267,7 +268,7 @@
 		*pprev = tmp;
 		pprev = &tmp->vm_next;
 		mm->map_count++;
-		retval = copy_page_range(mm, current->mm, tmp);
+		retval = share_page_range(mm, current->mm, tmp, &prev_pmd);
 		spin_unlock(&mm->page_table_lock);
 
 		if (tmp->vm_ops && tmp->vm_ops->open)
mm/memory.c +319 -122

714 lines not on this page · raw message

mm/rmap.c +235 -34

345 lines not on this page · raw message

mm/swapfile.c +3 -3
diff -Nru a/mm/swapfile.c b/mm/swapfile.c
@@ -371,7 +371,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 +383,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 +408,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));
#1 lore raw
Quoted 6 lines · Dave McCracken

Ok, here it is. This patch works for my simple tests, both under UP and SMP, including under memory pressure. I'd appreciate anyone who'd like to take it and beat on it. Please let me know of any problems you find.

The patch is against this morning's 2.5 BK tree.

Interesting, you substituted pte_page_lock(ptepage) for mm->page_table_lock. Could you wax poetic about that, please?

Signature · 1 line
Daniel
#2 lore raw

--On Wednesday, October 02, 2002 18:51:41 +0200 Daniel Phillips

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

Interesting, you substituted pte_page_lock(ptepage) for mm->page_table_lock. Could you wax poetic about that, please?

Sure. If a pte page is shared, the mm->page_table_lock is not sufficient to protect the rest of the page fault. Therefore we need a lock at the pte page level. The mm->page_table_lock is held during the page fault until we have a valid and locked pte page we're working on, then it's dropped for the rest of the fault.

Feel free to poke holes in my logic, but I think it's the right locking model for shared pte pages.

Dave McCracken

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

#3 lore raw
In reply to Daniel Phillips
Quoted 10 lines · Daniel Phillips
Quoted 6 lines · Dave McCracken

Ok, here it is. This patch works for my simple tests, both under UP and SMP, including under memory pressure. I'd appreciate anyone who'd like to take it and beat on it. Please let me know of any problems you find.

The patch is against this morning's 2.5 BK tree.

Interesting, you substituted pte_page_lock(ptepage) for mm->page_table_lock. Could you wax poetic about that, please?

Never mind, I see the logic. This reflects the fact that page_table_lock is insufficient protection when pte pages are shared. So you solved that problem and at the same time improved the scalability for the general case immensely, without adding any new overhead. Very nice!

Signature · 1 line
Daniel
#4 lore raw
In reply to Dave McCracken
Quoted 3 lines · Dave McCracken

Ok, here it is. This patch works for my simple tests, both under UP and SMP, including under memory pressure. I'd appreciate anyone who'd like to take it and beat on it. Please let me know of any problems you find.

Interesting. I notice that you are using the _PAGE_RW bit in the PMDs. Are you relying on the hardware to do anything with that bit, or is it only used by software?

(If you are relying on the hardware to do something different when _PAGE_RW is clear in the PMD, then your approach isn't portable.)

Paul.

#5 lore raw

--On Thursday, October 03, 2002 08:39:20 +1000 Paul Mackerras

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

Interesting. I notice that you are using the _PAGE_RW bit in the PMDs. Are you relying on the hardware to do anything with that bit, or is it only used by software?

(If you are relying on the hardware to do something different when _PAGE_RW is clear in the PMD, then your approach isn't portable.)

Yes, I am relying on the hardware. I was under the impression that it was pretty much universal that making the pmd read-only would make the hardware treat all ptes under it as read-only. This came out of a discussion on lkml last winter where this assertion was made.

Do you know of a page table-based architecture that doesn't have and honor read-only protections at the pmd level?

Dave McCracken

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