Skip to contentSkip to conversation

Speed up task exit

PATCH In review Dave McCracken 2 participants 3 messages Jan 28, 2003 – Feb 3, 2003

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

PATCH 1/1 Speed up task exit

Andrew, this builds on my first patch eliminating the page_table_lock during page table cleanup on exit. I took a good hard look at clear_page_tables, and realized that it's using up a lot of time to run. It walks through a lot of empty slots looking for pte pages to free.

I came to the realization that if we could just keep a count of mapped pages and swap entries, we'd know right away if a pte page is freeable. This patch tracks the count for pte pages and removes them as soon as they're unused, eliminating the need for clear_page_tables entirely.

Doing this gained another 5% in my fork/exit timing tests, so the combined patch gives me a 10% improvement in fork/exit.

Tracking the reference counts was the last straw in overloading struct page with pte page info, so I created a 'struct ptpage' to use when the struct page describes a page table page. It's a bit of a hack, but I think in the long run will make it more understandable.

Dave McCracken

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

./include/asm-generic/tlb.h +0 -7
@@ -84,13 +84,6 @@
 static inline void
 tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
 {
-	int freed = tlb->freed;
-	struct mm_struct *mm = tlb->mm;
-	int rss = mm->rss;
-
-	if (rss < freed)
-		freed = rss;
-	mm->rss = rss - freed;
 	tlb_flush_mmu(tlb, start, end);
 
 	/* keep the page table cache within bounds */
./include/asm-generic/rmap.h +12 -10
@@ -26,7 +26,8 @@
  */
 #include <linux/mm.h>
 
-static inline void pgtable_add_rmap(struct page * page, struct mm_struct * mm, unsigned long address)
+static inline void
+pgtable_add_rmap(struct ptpage * 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 ... ;( */
@@ -35,30 +36,31 @@
 	if (!mem_init_done)
 		return;
 #endif
-	page->mapping = (void *)mm;
-	page->index = address & ~((PTRS_PER_PTE * PAGE_SIZE) - 1);
+	page->mm = mm;
+	page->virtual = address & ~((PTRS_PER_PTE * PAGE_SIZE) - 1);
 	inc_page_state(nr_page_table_pages);
 }
 
-static inline void pgtable_remove_rmap(struct page * page)
+static inline void
+pgtable_remove_rmap(struct ptpage * page)
 {
-	page->mapping = NULL;
-	page->index = 0;
+	page->mm = NULL;
+	page->virtual = 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;
+	struct ptpage * page = (struct ptpage *)kmap_atomic_to_page(ptep);
+	return page->mm;
 }
 
 static inline unsigned long ptep_to_address(pte_t * ptep)
 {
-	struct page * page = kmap_atomic_to_page(ptep);
+	struct ptpage * page = (struct ptpage *)kmap_atomic_to_page(ptep);
 	unsigned long low_bits;
 	low_bits = ((unsigned long)ptep & ~PAGE_MASK) * PTRS_PER_PTE;
-	return page->index + low_bits;
+	return page->virtual + low_bits;
 }
 
 #if CONFIG_HIGHPTE
./include/linux/mm.h +15 -1
@@ -196,6 +196,16 @@
  */
 #include <linux/page-flags.h>
 
+struct ptpage {
+	unsigned long flags;		/* atomic flags, some possibly
+					   updated asynchronously */
+	atomic_t count;			/* Usage count, see below. */
+	struct mm_struct *mm;		/* mm_struct this page belongs to */
+	unsigned long virtual;		/* virtual address this page maps */
+	unsigned long mapcount;		/* Number of pages mapped to this page */
+	unsigned long swapcount; 	/* Number of swap pages in this page */
+};
+
 /*
  * Methods to modify the page usage count.
  *
@@ -365,6 +375,11 @@
 void shmem_lock(struct file * file, int lock);
 int shmem_zero_setup(struct vm_area_struct *);
 
+void increment_rss(struct ptpage *ptpage);
+void decrement_rss(struct ptpage *ptpage);
+void increment_swapcount(struct ptpage *ptpage);
+void decrement_swapcount(struct ptpage *ptpage);
+
 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
 			unsigned long size);
 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
@@ -372,7 +387,6 @@
 		unsigned long end_addr, unsigned long *nr_accounted);
 void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			unsigned long address, unsigned long size);
-void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr);
 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
 			struct vm_area_struct *vma);
 int remap_page_range(struct vm_area_struct *vma, unsigned long from,
./include/asm-i386/pgalloc.h +6 -6
@@ -10,10 +10,10 @@
 #define pmd_populate_kernel(mm, pmd, pte) \
 		set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)))
 
-static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte)
+static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct ptpage *pte)
 {
 	set_pmd(pmd, __pmd(_PAGE_TABLE +
-		((unsigned long long)page_to_pfn(pte) <<
+		((unsigned long long)page_to_pfn((struct page *)pte) <<
 			(unsigned long long) PAGE_SHIFT)));
 }
 /*
@@ -24,20 +24,20 @@
 void pgd_free(pgd_t *pgd);
 
 pte_t *pte_alloc_one_kernel(struct mm_struct *, unsigned long);
-struct page *pte_alloc_one(struct mm_struct *, unsigned long);
+struct ptpage *pte_alloc_one(struct mm_struct *, unsigned long);
 
 static inline void pte_free_kernel(pte_t *pte)
 {
 	free_page((unsigned long)pte);
 }
 
-static inline void pte_free(struct page *pte)
+static inline void pte_free(struct ptpage *pte)
 {
-	__free_page(pte);
+	__free_page((struct page *)pte);
 }
 
 
-#define __pte_free_tlb(tlb,pte) tlb_remove_page((tlb),(pte))
+#define __pte_free_tlb(tlb,pte) tlb_remove_page((tlb),((struct page *)pte))
 
 /*
  * allocating and freeing a pmd is trivial: the 1-entry pmd is
./include/asm-i386/pgtable.h +2 -0
@@ -229,6 +229,8 @@
 #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
 #endif /* !CONFIG_DISCONTIGMEM */
 
+#define	pmd_ptpage(pmd) ((struct ptpage *)pmd_page(pmd))
+
 #define pmd_large(pmd) \
 	((pmd_val(pmd) & (_PAGE_PSE|_PAGE_PRESENT)) == (_PAGE_PSE|_PAGE_PRESENT))
 
./arch/i386/mm/pgtable.c +10 -8
@@ -145,24 +145,26 @@
 	return pte;
 }
 
-struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
+struct ptpage *pte_alloc_one(struct mm_struct *mm, unsigned long address)
 {
 	int count = 0;
-	struct page *pte;
+	struct ptpage *pte;
    
    	do {
 #if CONFIG_HIGHPTE
-		pte = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, 0);
+		pte = (struct ptpage *)alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, 0);
 #else
-		pte = alloc_pages(GFP_KERNEL, 0);
+		pte = (struct ptpage *)alloc_pages(GFP_KERNEL, 0);
 #endif
-		if (pte)
-			clear_highpage(pte);
-		else {
+		if (pte) {
+			clear_highpage((struct page *)pte);
+			pte->mapcount = pte->swapcount= 0;
+			break;
+		} else {
 			current->state = TASK_UNINTERRUPTIBLE;
 			schedule_timeout(HZ);
 		}
-	} while (!pte && (count++ < 10));
+	} while (count++ < 10);
 	return pte;
 }
 
./fs/exec.c +1 -1
@@ -317,7 +317,7 @@
 	set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
 	pte_chain = page_add_rmap(page, pte, pte_chain);
 	pte_unmap(pte);
-	tsk->mm->rss++;
+	increment_rss(pmd_ptpage(*pmd));
 	spin_unlock(&tsk->mm->page_table_lock);
 
 	/* no need for flush_tlb */
./mm/fremap.c +5 -2
@@ -19,9 +19,11 @@
 static inline void zap_pte(struct mm_struct *mm, pte_t *ptep)
 {
 	pte_t pte = *ptep;
+	struct ptpage *ptpage;
 
 	if (pte_none(pte))
 		return;
+	ptpage = (struct ptpage *)kmap_atomic_to_page((void *)ptep);
 	if (pte_present(pte)) {
 		unsigned long pfn = pte_pfn(pte);
 
@@ -33,12 +35,13 @@
 					set_page_dirty(page);
 				page_remove_rmap(page, ptep);
 				page_cache_release(page);
-				mm->rss--;
+				decrement_rss(ptpage);
 			}
 		}
 	} else {
 		free_swap_and_cache(pte_to_swp_entry(pte));
 		pte_clear(ptep);
+		decrement_swapcount(ptpage);
 	}
 }
 
@@ -69,7 +72,6 @@
 
 	zap_pte(mm, pte);
 
-	mm->rss++;
 	flush_page_to_ram(page);
 	flush_icache_page(vma, page);
 	entry = mk_pte(page, protection_map[prot]);
@@ -78,6 +80,7 @@
 	set_pte(pte, entry);
 	pte_chain = page_add_rmap(page, pte, pte_chain);
 	pte_unmap(pte);
+	increment_rss(pmd_ptpage(*pmd));
 	flush_tlb_page(vma, addr);
 
 	spin_unlock(&mm->page_table_lock);
./mm/swapfile.c +6 -4
@@ -379,20 +379,23 @@
  */
 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
 static void
-unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
+unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, pte_t *dir,
 	swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
 {
 	pte_t pte = *dir;
+	struct ptpage *ptpage;
 
 	if (likely(pte_to_swp_entry(pte).val != entry.val))
 		return;
 	if (unlikely(pte_none(pte) || pte_present(pte)))
 		return;
+	ptpage = pmd_ptpage(*pmd);
 	get_page(page);
 	set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
 	*pte_chainp = page_add_rmap(page, dir, *pte_chainp);
+	increment_rss(ptpage);
+	decrement_swapcount(ptpage);
 	swap_free(entry);
-	++vma->vm_mm->rss;
 }
 
 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
@@ -423,8 +426,7 @@
 		 */
 		if (pte_chain == NULL)
 			pte_chain = pte_chain_alloc(GFP_ATOMIC);
-		unuse_pte(vma, offset+address-vma->vm_start,
-				pte, entry, page, &pte_chain);
+		unuse_pte(vma, dir, pte, entry, page, &pte_chain);
 		address += PAGE_SIZE;
 		pte++;
 	} while (address && (address < end));
./mm/memory.c +206 -64

403 lines not on this page · raw message

./mm/mremap.c +6 -2
@@ -94,8 +94,10 @@
 		page = pte_page(*src);
 
 	if (!pte_none(*src)) {
-		if (page)
+		if (page) {
 			page_remove_rmap(page, src);
+			decrement_rss((struct ptpage *)kmap_atomic_to_page((void *)src));
+		}
 		pte = ptep_get_and_clear(src);
 		if (!dst) {
 			/* No dest?  We must put it back. */
@@ -103,8 +105,10 @@
 			error++;
 		}
 		set_pte(dst, pte);
-		if (page)
+		if (page) {
 			*pte_chainp = page_add_rmap(page, dst, *pte_chainp);
+			increment_rss((struct ptpage *)kmap_atomic_to_page((void *)dst));
+		}
 	}
 	return error;
 }
./mm/mmap.c +13 -78

134 lines not on this page · raw message

./mm/rmap.c +12 -1
@@ -328,6 +328,7 @@
 static int try_to_unmap_one(struct page * page, pte_addr_t paddr)
 {
 	pte_t *ptep = rmap_ptep_map(paddr);
+	struct ptpage *ptpage = (struct ptpage *)kmap_atomic_to_page((void *)ptep);
 	unsigned long address = ptep_to_address(ptep);
 	struct mm_struct * mm = ptep_to_mm(ptep);
 	struct vm_area_struct * vma;
@@ -338,6 +339,15 @@
 		BUG();
 
 	/*
+	 * If this mm is in the process of exiting, skip this page
+	 * for now to let the exit finish.
+	 */
+	if (atomic_read(&mm->mm_users) == 0) {
+		rmap_ptep_unmap(ptep);
+		return SWAP_AGAIN;
+	}
+
+	/*
 	 * We need the page_table_lock to protect us from page faults,
 	 * munmap, fork, etc...
 	 */
@@ -364,19 +374,20 @@
 	flush_cache_page(vma, address);
 	pte = ptep_get_and_clear(ptep);
 	flush_tlb_page(vma, address);
+	decrement_rss(ptpage);
 
 	/* Store the swap location in the pte. See handle_pte_fault() ... */
 	if (PageSwapCache(page)) {
 		swp_entry_t entry = { .val = page->index };
 		swap_duplicate(entry);
 		set_pte(ptep, swp_entry_to_pte(entry));
+		increment_swapcount(ptpage);
 	}
 
 	/* Move the dirty bit to the physical page now the pte is gone. */
 	if (pte_dirty(pte))
 		set_page_dirty(page);
 
-	mm->rss--;
 	page_cache_release(page);
 	ret = SWAP_SUCCESS;
 
#1 lore raw
Quoted 4 lines · Dave McCracken

Andrew, this builds on my first patch eliminating the page_table_lock during page table cleanup on exit.

Sorry David, I just haven't had time to play with this. I did some quick testing on uniprocessor shell-script-intensive loads and saw no bottom-line change at all.

What load did you test with?
--
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 Monday, February 03, 2003 13:47:19 -0800 Andrew Morton

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

Sorry David, I just haven't had time to play with this. I did some quick testing on uniprocessor shell-script-intensive loads and saw no bottom-line change at all.

What load did you test with?

I used a simple test program that forks a null child and waits for it to exit. It does it multiple times (default 100) and times the aggregate time, then computes an average.

Like I said, I saw roughly 10% improvement in that test with my patch.

I'm surprised that shell scripts wouldn't show an improvement. I expected they'd be more sensitive to exit performance, given how they highlighted the performance issues with shared page tables.

My original reason for attacking clear_all_pages was because it kept showing up in my profiling as significant, and a quick examination of it looked like it had significant overhead that could be eliminated by keeping a few reference counts.

Dave

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

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