Skip to contentSkip to conversation

low-latency zap_page_range()

PATCH In review Robert Love 5 participants 12 messages Aug 29, 2002

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

PATCH 1/1 low-latency zap_page_range()

Andrew,

Attached patch implements a low latency version of "zap_page_range()".

Calls with even moderately large page ranges result in very long lock held times and consequently very long periods of non-preemptibility. This function is in my list of the top 3 worst offenders. It is gross.

This new version reimplements zap_page_range() as a loop over ZAP_BLOCK_SIZE chunks. After each iteration, if a reschedule is pending, we drop page_table_lock and automagically preempt. Note we can not blindly drop the locks and reschedule (e.g. for the non-preempt case) since there is a possibility to enter this codepath holding other locks.

... I am sure you are familar with all this, its the same deal as your low-latency work. This patch implements the "cond_resched_lock()" as we discussed sometime back. I think this solution should be acceptable to you and Linus.

There are other misc. cleanups, too.

This new zap_page_range() yields latency too-low-to-benchmark: <<1ms.

Please, Andrew, add this to your ever-growing list.

Robert Love

include/linux/sched.h +28 -0
diff -urN linux-2.5.32/include/linux/sched.h linux/include/linux/sched.h
@@ -898,6 +898,34 @@
 		__cond_resched();
 }
 
+#ifdef CONFIG_PREEMPT
+
+/*
+ * cond_resched_lock() - if a reschedule is pending, drop the given lock,
+ * call schedule, and on return reacquire the lock.
+ *
+ * Note: this does not assume the given lock is the _only_ lock held.
+ * The kernel preemption counter gives us "free" checking that we are
+ * atomic -- let's use it.
+ */
+static inline void cond_resched_lock(spinlock_t * lock)
+{
+	if (need_resched() && preempt_count() == 1) {
+		_raw_spin_unlock(lock);
+		preempt_enable_no_resched();
+		__cond_resched();
+		spin_lock(lock);
+	}
+}
+
+#else
+
+static inline void cond_resched_lock(spinlock_t * lock)
+{
+}
+
+#endif
+
 /* Reevaluate whether the task has signals pending delivery.
    This is required every time the blocked sigset_t changes.
    Athread cathreaders should have t->sigmask_lock.  */
mm/memory.c +31 -18

69 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Robert Love

Andrew,

Attached patch implements a low latency version of "zap_page_range()".

This doesn't quite do the right thing on SMP.

Note that pages which are to be torn down are buffered in the mmu_gather_t array. The kernel throws away 507 pages at a time - this is to reduce the frequency of global TLB invalidations. (The 507 is, I assume, designed to make the mmu_gather_t be 2048 bytes in size. I recently broke that math, and need to fix it up).

However with your change, we'll only ever put 256 pages into the mmu_gather_t. Half of that thing's buffer is unused and the invalidation rate will be doubled during teardown of large address ranges.

I suggest that you make ZAP_BLOCK_SIZE be equal to FREE_PTE_NR on SMP, and 256 on UP.

(We could get fancier and do something like:

	tlb = tlb_gather_mmu(mm, 0):
	while (size) {
		...
		unmap_page_range(ZAP_BLOCK_SIZE pages);
		tlb_flush_mmu(...);
		cond_resched_lock();
	}
	tlb_finish_mmu(..);
	spin_unlock(page_table_lock);

but I don't think that passes the benefit-versus-complexity test.)

Also, if the kernel is not compiled for preemption then we're doing a little bit of extra work to no advantage, yes? We can avoid doing that by setting ZAP_BLOCK_SIZE to infinity.

How does this altered version look? All I changed was the ZAP_BLOCK_SIZE initialisation.

include/linux/sched.h renamed from include/linux/sched.h~llzpr +28 -0
@@ -907,6 +907,34 @@ static inline void cond_resched(void)
 		__cond_resched();
 }
 
+#ifdef CONFIG_PREEMPT
+
+/*
+ * cond_resched_lock() - if a reschedule is pending, drop the given lock,
+ * call schedule, and on return reacquire the lock.
+ *
+ * Note: this does not assume the given lock is the _only_ lock held.
+ * The kernel preemption counter gives us "free" checking that we are
+ * atomic -- let's use it.
+ */
+static inline void cond_resched_lock(spinlock_t * lock)
+{
+	if (need_resched() && preempt_count() == 1) {
+		_raw_spin_unlock(lock);
+		preempt_enable_no_resched();
+		__cond_resched();
+		spin_lock(lock);
+	}
+}
+
+#else
+
+static inline void cond_resched_lock(spinlock_t * lock)
+{
+}
+
+#endif
+
 /* Reevaluate whether the task has signals pending delivery.
    This is required every time the blocked sigset_t changes.
    Athread cathreaders should have t->sigmask_lock.  */
mm/memory.c renamed from mm/memory.c~llzpr +41 -18

79 lines not on this page · raw message

.

#2 lore raw
Quoted 4 lines · Andrew Morton

However with your change, we'll only ever put 256 pages into the mmu_gather_t. Half of that thing's buffer is unused and the invalidation rate will be doubled during teardown of large address ranges.

Agreed. Go for it.

Hm, unless, since 507 vs 256 is not the end of the world and latency is already low, we want to just make it always (FREE_PTE_NR*PAGE_SIZE)...

As long as the "cond_resched_lock()" is a preempt only thing, I also agree with making ZAP_BLOCK_SIZE ~0 on !CONFIG_PREEMPT - unless we wanted to unconditionally drop the locks and let preempt just do the right thing and also reduce SMP lock contention in the SMP case.

Robert Love

#3 lore raw
Quoted 8 lines · Robert Love
Quoted 4 lines · Andrew Morton

However with your change, we'll only ever put 256 pages into the mmu_gather_t. Half of that thing's buffer is unused and the invalidation rate will be doubled during teardown of large address ranges.

Agreed. Go for it.

Oh and put a comment in there explaining what you just said to me :)

Robert Love

#4 lore raw
In reply to Robert Love
Quoted 5 lines · Robert Love

...
unless we
wanted to unconditionally drop the locks and let preempt just do the right thing and also reduce SMP lock contention in the SMP case.

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.

I suspect that yes, voluntarily popping the lock during the long holdtimes will allow other CPUs to get on with stuff, and will provide efficiency increases. (It's a pretty lame way of doing that though).

But I don't recall seeing nasty page_table_lock spintimes on anyone's lockmeter reports, so...

#5 lore raw
Quoted 3 lines · Robert Love
Quoted 3 lines

unless we
wanted to unconditionally drop the locks and let preempt just do the right thing and also reduce SMP lock contention in the SMP case.

Quoted 8 lines · Andrew Morton

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.
I suspect that yes, voluntarily popping the lock during the long holdtimes will allow other CPUs to get on with stuff, and will provide efficiency increases. (It's a pretty lame way of doing that though).
But I don't recall seeing nasty page_table_lock spintimes on anyone's lockmeter reports, so...

You will. There are just bigger fish to fry at the moment.

Cheers,
Bill

#6 lore raw

In article <[email protected]>,

Quoted 15 lines · William Lee Irwin III
Quoted 3 lines · Robert Love
Quoted 3 lines

unless we
wanted to unconditionally drop the locks and let preempt just do the right thing and also reduce SMP lock contention in the SMP case.

Quoted 8 lines · Andrew Morton

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.
I suspect that yes, voluntarily popping the lock during the long holdtimes will allow other CPUs to get on with stuff, and will provide efficiency increases. (It's a pretty lame way of doing that though).
But I don't recall seeing nasty page_table_lock spintimes on anyone's lockmeter reports, so...

Quoted 1 line

You will. There are just bigger fish to fry at the moment.

You will NOT.

The page_table_lock protects against page stealing of the VM and concurrent page-faults, nothing else. There is no way you can get contention on it under any reasonable load that doesn't involve heavy out-of-memory behaviour, simply because

- the lock is per-mm
- all "regular" paths that care about this also get the mmap semaphore

In short, that spinlock has _zero_ scalability impact. You can theoretically get contention on it without memory pressure only by having hundreds of threads page-faulting at the same time (getting a read-lock on the mmap semaphore), but by then your performance has nothing to do with the spinlock, and everything to do with the page faults themselves.

(In fact, I can almost guarantee that most of the long hold-times are for exit(), not for munmap(). And in that case the spinlock cannot get any non-pagestealer contention at all, since nobody else is using the MM)

Linus

#7 lore raw
Quoted 9 lines · Linus Torvalds

You will NOT.
The page_table_lock protects against page stealing of the VM and concurrent page-faults, nothing else. There is no way you can get contention on it under any reasonable load that doesn't involve heavy out-of-memory behaviour, simply because
- the lock is per-mm
- all "regular" paths that care about this also get the mmap semaphore
In short, that spinlock has _zero_ scalability impact. You can theoretically get contention on it without memory pressure only by

Quoted 8 lines

having hundreds of threads page-faulting at the same time (getting a read-lock on the mmap semaphore), but by then your performance has nothing to do with the spinlock, and everything to do with the page faults themselves.
(In fact, I can almost guarantee that most of the long hold-times are for exit(), not for munmap(). And in that case the spinlock cannot get any non-pagestealer contention at all, since nobody else is using the MM)

All I have to go on is a report this has happened and a low-priority task to investigate it at some point in the future. I'll send you data either demonstrating it or exonerating it when I eventually get to it.

Cheers,
Bill

#8 lore raw
In reply to Robert Love
Quoted 5 lines · Robert Love

...
unless we
wanted to unconditionally drop the locks and let preempt just do the right thing and also reduce SMP lock contention in the SMP case.

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.

I suspect that yes, voluntarily popping the lock during the long holdtimes will allow other CPUs to get on with stuff, and will provide efficiency increases. (It's a pretty lame way of doing that though).

But I don't recall seeing nasty page_table_lock spintimes on anyone's lockmeter reports, so we can leave it as-is for now.

#9 lore raw
Quoted 3 lines · Andrew Morton

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.

Since latency is a direct function of lock held times in the preemptible kernel, and I am seeing disgusting zap_page_range() latencies, the lock is held a long time.

So we know it is held forever and a day... but is there contention?

Quoted 2 lines

But I don't recall seeing nasty page_table_lock spintimes on anyone's lockmeter reports, so we can leave it as-is for now.

I do not recall seeing this either and I have not done my own tests.

Personally, I would love to rip out the "cond_resched_lock()" and just do

	spin_unlock();
	spin_lock();

and be done with it. This gives automatic preemption support and the SMP benefit. Preemption being an "automatic" consequence of improved locking was always my selling point (albeit, this is a gross example of improving the locking, but it gets the job done).

But, the current implementation was more palatable to you and Linus when I first posted this, and that counts for something.

Robert Love

#10 lore raw
Quoted 7 lines · Robert Love
Quoted 3 lines · Andrew Morton

That's an interesting point. page_table_lock is one of those locks which is occasionally held for ages, and frequently held for a short time.

Quoted 5 lines

Since latency is a direct function of lock held times in the preemptible kernel, and I am seeing disgusting zap_page_range() latencies, the lock is held a long time.

So we know it is held forever and a day... but is there contention?

I'm sure there is, but nobody has measured the right workload.

Two CLONE_MM threads, one running mmap()/munmap(), the other trying to fault in some pages. I'm sure someone has some vital application which does exactly this. They always do :(

#11 lore raw
Quoted 7 lines · Andrew Morton
Quoted 1 line

So we know it is held forever and a day... but is there contention?

I'm sure there is, but nobody has measured the right workload.

Two CLONE_MM threads, one running mmap()/munmap(), the other trying to fault in some pages. I'm sure someone has some vital application which does exactly this. They always do :(

Can't fix this one. The mmap()/munmap() needs to have the mmap_sem for writing as long as its setting up or tearing down a VMA while the pagefault path takes the mmap_sem for reading.

It might be fixable in some dirty way, but I doubt that'll ever be worth it.

regards,

Rik

Signature · 3 lines
Bravely reimplemented by the knights who say "NIH".

http://www.surriel.com/		http://distro.conectiva.com/