Skip to contentSkip to conversation

page fault fastpath patch v2: fix race conditions, stats for 8,32 and 512 cpu SMP

DISCUSSION Rajesh Venkatasubramanian 3 participants 10 messages Aug 18, 2004 – Aug 23, 2004 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 14 lines · William Lee Irwin III
Quoted 4 lines · Hugh Dickins

That's interesting, and disappointing.
The main lesson I took from your patch (I think wli was hinting at the same) is that we ought now to question page_table_lock usage, should be possible to cut it a lot.

Quoted 8 lines

I recall from exchanges with Dave McCracken 18 months ago that the page_table_lock is _almost_ unnecessary in rmap.c, should be possible to get avoid it there and in some other places.
We take page_table_lock when making absent present and when making present absent: I like your observation that those are exclusive cases. But you've found that narrowing the width of the page_table_lock in a particular path does not help. You sound surprised, me too. Did you find out why that was?

Quoted 3 lines

It also protects against vma tree modifications in mainline, but rmap.c shouldn't need it for vmas anymore, as the vma is rooted to the spot by mapping->i_shared_lock for file pages and anon_vma->lock for anonymous.

If I am reading the code correctly, then without page_table_lock in page_referenced_one(), we can race with exit_mmap() and page table pages can be freed under us.

Quoted 10 lines · William Lee Irwin III
Quoted 2 lines · Hugh Dickins

I do think this will be a more fruitful direction than pte locking:
just looking through the arches for spare bits puts me off pte locking.

Fortunately, spare bits aren't strictly necessary, and neither is cmpxchg. A single invalid value can serve in place of a bitflag. When using such an invalid value, just xchg()'ing it and looping when the invalid value is seen should suffice. This holds more generally for all radix trees, not just pagetables, and happily xchg() or emulation thereof is required by core code for all arches.

Good point.

Another solution may be to use the unused bytes (->lru or ->private) in page table "struct page" as bit_spin_locks. We can use a single bit to protect a small set of ptes (8, 16, or 32).

Rajesh

#3 lore raw
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

It also protects against vma tree modifications in mainline, but rmap.c shouldn't need it for vmas anymore, as the vma is rooted to the spot by mapping->i_shared_lock for file pages and anon_vma->lock for anonymous.

Quoted 3 lines · Rajesh Venkatasubramanian

If I am reading the code correctly, then without page_table_lock in page_referenced_one(), we can race with exit_mmap() and page table pages can be freed under us.

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

Quoted 6 lines · William Lee Irwin III
Quoted 6 lines

Fortunately, spare bits aren't strictly necessary, and neither is cmpxchg. A single invalid value can serve in place of a bitflag. When using such an invalid value, just xchg()'ing it and looping when the invalid value is seen should suffice. This holds more generally for all radix trees, not just pagetables, and happily xchg() or emulation thereof is required by core code for all arches.

Quoted 4 lines · Rajesh Venkatasubramanian

Good point.
Another solution may be to use the unused bytes (->lru or ->private) in page table "struct page" as bit_spin_locks. We can use a single bit to protect a small set of ptes (8, 16, or 32).

In general the bitwise operations are more expensive than ordinary spinlocks, and a separately-allocated spinlock (not necessarily kmalloc()'d, sitting in struct page also counts, that is, separate from the pte) introduces another cacheline to be touched where with in-place locking of the pte only the pte's cacheline is needed.

-- wli

#4 lore raw
Quoted 5 lines · William Lee Irwin III
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

It also protects against vma tree modifications in mainline, but rmap.c shouldn't need it for vmas anymore, as the vma is rooted to the spot by mapping->i_shared_lock for file pages and anon_vma->lock for anonymous.

Quoted 8 lines
Quoted 3 lines · Rajesh Venkatasubramanian

If I am reading the code correctly, then without page_table_lock in page_referenced_one(), we can race with exit_mmap() and page table pages can be freed under us.

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

No. Unfortunately exit_mmap() removes vmas from ->i_mmap after removing page table pages. Maybe we can reverse this, though.

Rajesh

#5 lore raw
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

Quoted 2 lines · Rajesh Venkatasubramanian

No. Unfortunately exit_mmap() removes vmas from ->i_mmap after removing page table pages. Maybe we can reverse this, though.

Something like this?

Index: mm1-2.6.8.1/mm/mmap.c
=================================--- mm1-2.6.8.1.orig/mm/mmap.c 2004-08-16 23:47:16.000000000 -0700
+++ mm1-2.6.8.1/mm/mmap.c 2004-08-18 17:18:26.513559632 -0700
@@ -1810,6 +1810,14 @@
mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
~0UL, &nr_accounted, NULL);
vm_unacct_memory(nr_accounted);
+ /*
+ * Walk the list again, actually closing and freeing it.
+ */
+ while (vma) {
+ struct vm_area_struct *next = vma->vm_next;
+ remove_vm_struct(vma);
+ vma = next;
+ }
BUG_ON(mm->map_count); /* This is just debugging */
clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD); tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
@@ -1822,16 +1830,6 @@
mm->locked_vm = 0;

spin_unlock(&mm->page_table_lock);
-
- /*
- * Walk the list again, actually closing and freeing it
- * without holding any MM locks.
- */
- while (vma) {
- struct vm_area_struct *next = vma->vm_next;
- remove_vm_struct(vma);
- vma = next;
- }
}

/* Insert vm structure into process list sorted by address

#6 lore raw
Quoted 10 lines · William Lee Irwin III
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

Quoted 2 lines · Rajesh Venkatasubramanian

No. Unfortunately exit_mmap() removes vmas from ->i_mmap after removing page table pages. Maybe we can reverse this, though.

Something like this?

Yeah, something similar... A small nitpick: page_table_lock nests within i_mmap_lock and anon_lock.

do_unmap() also frees page tables first and then removes vmas from i_mmap (and/or) anon_vma list. Is there a reason to preserve this ordering ?

Rajesh

Quoted 37 lines

Index: mm1-2.6.8.1/mm/mmap.c
=================================> --- mm1-2.6.8.1.orig/mm/mmap.c 2004-08-16 23:47:16.000000000 -0700
+++ mm1-2.6.8.1/mm/mmap.c 2004-08-18 17:18:26.513559632 -0700

(quoted diff) +8 -10
@@ -1810,6 +1810,14 @@
 	mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
 					~0UL, &nr_accounted, NULL);
 	vm_unacct_memory(nr_accounted);
+	/*
+	 * Walk the list again, actually closing and freeing it.
+	 */
+	while (vma) {
+		struct vm_area_struct *next = vma->vm_next;
+		remove_vm_struct(vma);
+		vma = next;
+	}
 	BUG_ON(mm->map_count);	/* This is just debugging */
 	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
 	tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
@@ -1822,16 +1830,6 @@
 	mm->locked_vm = 0;
 
 	spin_unlock(&mm->page_table_lock);
-
-	/*
-	 * Walk the list again, actually closing and freeing it
-	 * without holding any MM locks.
-	 */
-	while (vma) {
-		struct vm_area_struct *next = vma->vm_next;
-		remove_vm_struct(vma);
-		vma = next;
-	}
 }
 
 /* Insert vm structure into process list sorted by address
#7 lore raw
Quoted 1 line · William Lee Irwin III
Quoted 1 line

Something like this?

Quoted 5 lines · Rajesh Venkatasubramanian

Yeah, something similar... A small nitpick: page_table_lock nests within i_mmap_lock and anon_lock.
do_unmap() also frees page tables first and then removes vmas from i_mmap (and/or) anon_vma list. Is there a reason to preserve this ordering ?

I don't see a reason to rearrange it. We can just as easily drop and reacquire ->page_table_lock.

-- wli

#8 lore raw
Quoted 1 line · William Lee Irwin III
Quoted 1 line
Quoted 1 line

Something like this?

Quoted 5 lines · Rajesh Venkatasubramanian
Quoted 5 lines

Yeah, something similar... A small nitpick: page_table_lock nests within i_mmap_lock and anon_lock.
do_unmap() also frees page tables first and then removes vmas from i_mmap (and/or) anon_vma list. Is there a reason to preserve this ordering ?

Quoted 2 lines · William Lee Irwin III

I don't see a reason to rearrange it. We can just as easily drop and reacquire ->page_table_lock.

On closer examination it's not that simple.

-- wli

#9 lore raw
In reply to William Lee Irwin III

Would it not be better if exit_mmap would hold a writelock on mm->mmap_sem and the page_table_lock? This would insure that page faults would not change pte's. exit_mmap changes the memory map so it seems that the wrong lock is used here.

Quoted 53 lines · William Lee Irwin III
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

Quoted 2 lines · Rajesh Venkatasubramanian

No. Unfortunately exit_mmap() removes vmas from ->i_mmap after removing page table pages. Maybe we can reverse this, though.

Something like this?

Index: mm1-2.6.8.1/mm/mmap.c
=================================> --- mm1-2.6.8.1.orig/mm/mmap.c 2004-08-16 23:47:16.000000000 -0700
+++ mm1-2.6.8.1/mm/mmap.c 2004-08-18 17:18:26.513559632 -0700

(quoted diff) +8 -11
@@ -1810,6 +1810,14 @@
 	mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
 					~0UL, &nr_accounted, NULL);
 	vm_unacct_memory(nr_accounted);
+	/*
+	 * Walk the list again, actually closing and freeing it.
+	 */
+	while (vma) {
+		struct vm_area_struct *next = vma->vm_next;
+		remove_vm_struct(vma);
+		vma = next;
+	}
 	BUG_ON(mm->map_count);	/* This is just debugging */
 	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
 	tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
@@ -1822,16 +1830,6 @@
 	mm->locked_vm = 0;
 
 	spin_unlock(&mm->page_table_lock);
-
-	/*
-	 * Walk the list again, actually closing and freeing it
-	 * without holding any MM locks.
-	 */
-	while (vma) {
-		struct vm_area_struct *next = vma->vm_next;
-		remove_vm_struct(vma);
-		vma = next;
-	}
 }
 
 /* Insert vm structure into process list sorted by address
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#10 lore raw
Quoted 4 lines · Christoph Lameter

Would it not be better if exit_mmap would hold a writelock on mm->mmap_sem and the page_table_lock? This would insure that page faults would not change pte's. exit_mmap changes the memory map so it seems that the wrong lock is used here.

exit_mmap() is only called from kernel/fork.c/mmput(). We call exit_mmap() only if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)).

So there are no other active thread (mm_user) other than the current exit_mmap() thread. This gives thread exclusion. So we don't need mm->mmap_sem.

However, we have to lock out truncate()->zap_pmd_range(), rmap.c functions, and other places from walking the page tables while we are freeing the page tables in exit_mmap(). The page_table_lock in exit_mmap() provides that exclusion.

That's my understanding. Correct me if I am wrong.

Thanks,
Rajesh

Quoted 56 lines · William Lee Irwin III
Quoted 54 lines · William Lee Irwin III
Quoted 3 lines
Quoted 3 lines

exit_mmap() has removed the vma from ->i_mmap and ->mmap prior to unmapping the pages, so this should be safe unless that operation can be caught while it's in progress.

Quoted 2 lines · Rajesh Venkatasubramanian

No. Unfortunately exit_mmap() removes vmas from ->i_mmap after removing page table pages. Maybe we can reverse this, though.

Something like this?

Index: mm1-2.6.8.1/mm/mmap.c
=================================> > --- mm1-2.6.8.1.orig/mm/mmap.c 2004-08-16 23:47:16.000000000 -0700
+++ mm1-2.6.8.1/mm/mmap.c 2004-08-18 17:18:26.513559632 -0700

(quoted diff) +8 -11
@@ -1810,6 +1810,14 @@
 	mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
 					~0UL, &nr_accounted, NULL);
 	vm_unacct_memory(nr_accounted);
+	/*
+	 * Walk the list again, actually closing and freeing it.
+	 */
+	while (vma) {
+		struct vm_area_struct *next = vma->vm_next;
+		remove_vm_struct(vma);
+		vma = next;
+	}
 	BUG_ON(mm->map_count);	/* This is just debugging */
 	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
 	tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
@@ -1822,16 +1830,6 @@
 	mm->locked_vm = 0;
 
 	spin_unlock(&mm->page_table_lock);
-
-	/*
-	 * Walk the list again, actually closing and freeing it
-	 * without holding any MM locks.
-	 */
-	while (vma) {
-		struct vm_area_struct *next = vma->vm_next;
-		remove_vm_struct(vma);
-		vma = next;
-	}
 }
 
 /* Insert vm structure into process list sorted by address
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#11 lore raw
Quoted 10 lines · Rajesh Venkatasubramanian

So there are no other active thread (mm_user) other than the current exit_mmap() thread. This gives thread exclusion. So we don't need mm->mmap_sem.

However, we have to lock out truncate()->zap_pmd_range(), rmap.c functions, and other places from walking the page tables while we are freeing the page tables in exit_mmap(). The page_table_lock in exit_mmap() provides that exclusion.

That's my understanding. Correct me if I am wrong.

Correct. I just looked through the function and it unlinks the pgd before unlocking page_table_lock. It frees the pgd tree content later.

Since no mm_user is active anymore no pte ops occur and therefore also atomic pte operations do not need protection.