Skip to contentSkip to conversation

srcu: Implement call_srcu()

RFC PATCH In review Peter Zijlstra 6 participants 35 messages Jan 31, 2012 – Feb 14, 2012

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

RFC PATCH 1/1 srcu: Implement call_srcu()

Subject: srcu: Implement call_srcu()
From: Peter Zijlstra <[email protected]>
Date: Mon Jan 30 23:20:49 CET 2012

Implement call_srcu() by using a state machine driven by call_rcu_sched() and timer callbacks.

The state machine is a direct derivation of the existing synchronize_srcu() code and replaces synchronize_sched() calls with a call_rcu_sched() callback and the schedule_timeout() calls with simple timer callbacks.

It then re-implements synchronize_srcu() using a completion where we send the complete through call_srcu().

It completely wrecks synchronize_srcu_expedited() which is only used by KVM. 3 of the 5 use cases look like they really want to use call_srcu() instead, the remaining 2 I don't know but hope they can, which would let us remove it.

Compile tested only!! :-)

---
 include/linux/srcu.h |   23 +++
 kernel/srcu.c        |  304 +++++++++++++++++++++++++++++----------------------
 2 files changed, 196 insertions(+), 131 deletions(-)
include/linux/srcu.h +21 -2
@@ -27,17 +27,35 @@
 #ifndef _LINUX_SRCU_H
 #define _LINUX_SRCU_H
 
-#include <linux/mutex.h>
+#include <linux/spinlock.h>
 #include <linux/rcupdate.h>
+#include <linux/timer.h>
 
 struct srcu_struct_array {
 	int c[2];
 };
 
+enum srcu_state {
+	srcu_idle,
+	srcu_sync_1,
+	srcu_sync_2,
+	srcu_sync_2b,
+	srcu_wait,
+	srcu_wait_b,
+	srcu_sync_3,
+	srcu_sync_3b,
+};
+
 struct srcu_struct {
 	int completed;
 	struct srcu_struct_array __percpu *per_cpu_ref;
-	struct mutex mutex;
+	raw_spinlock_t lock;
+	enum srcu_state state;
+	union {
+		struct rcu_head head;
+		struct timer_list timer;
+	};
+	struct rcu_head *pending[2];
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	struct lockdep_map dep_map;
 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
@@ -73,6 +91,7 @@ void __srcu_read_unlock(struct srcu_stru
 void synchronize_srcu(struct srcu_struct *sp);
 void synchronize_srcu_expedited(struct srcu_struct *sp);
 long srcu_batches_completed(struct srcu_struct *sp);
+void call_srcu(struct srcu_struct *sp, struct rcu_head *head, void (*func)(struct rcu_head *));
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 
kernel/srcu.c +175 -129

352 lines not on this page · raw message

#1 lore raw
Quoted 12 lines · Peter Zijlstra

Subject: srcu: Implement call_srcu()
From: Peter Zijlstra <[email protected]>
Date: Mon Jan 30 23:20:49 CET 2012

Implement call_srcu() by using a state machine driven by call_rcu_sched() and timer callbacks.

The state machine is a direct derivation of the existing synchronize_srcu() code and replaces synchronize_sched() calls with a call_rcu_sched() callback and the schedule_timeout() calls with simple timer callbacks.

Quoted 5 lines

It then re-implements synchronize_srcu() using a completion where we send the complete through call_srcu().

It completely wrecks synchronize_srcu_expedited() which is only used by KVM. 3 of the 5

2 of the 5

Quoted 3 lines

use cases look like they really want to use
call_srcu() instead, the remaining 2 I don't know but hope they can, which would let us remove it.

They really need to return quickly to userspace, and they really need to perform some operation between rcu_assign_pointer() and returning, so no.

Quoted 3 lines

Compile tested only!! :-)

How much did synchronize_srcu_expedited() regress? Presumably your compiler didn't tell you that.

Can we get it back to speed by scheduling a work function on all cpus? wouldn't that force a quiescent state and allow call_srcu() to fire?

In kvm's use case synchronize_srcu_expedited() is usually called when no thread is in a critical section, so we don't have to wait for anything except the srcu machinery.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 2 lines · Avi Kivity

They really need to return quickly to userspace, and they really need to perform some operation between rcu_assign_pointer() and returning, so no.

Bugger :/

Quoted 6 lines
Quoted 3 lines

Compile tested only!! :-)

How much did synchronize_srcu_expedited() regress? Presumably your compiler didn't tell you that.

Nope, quite a lot I figure.

Quoted 6 lines

Can we get it back to speed by scheduling a work function on all cpus? wouldn't that force a quiescent state and allow call_srcu() to fire?

In kvm's use case synchronize_srcu_expedited() is usually called when no thread is in a critical section, so we don't have to wait for anything except the srcu machinery.

OK, I'll try and come up with means of making it go fast again ;-)

#3 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 2 lines · Avi Kivity

They really need to return quickly to userspace, and they really need to perform some operation between rcu_assign_pointer() and returning, so no.

Bugger :/

Quoted 6 lines
Quoted 3 lines

Compile tested only!! :-)

How much did synchronize_srcu_expedited() regress? Presumably your compiler didn't tell you that.

Nope, quite a lot I figure.

Quoted 8 lines
Quoted 6 lines

Can we get it back to speed by scheduling a work function on all cpus? wouldn't that force a quiescent state and allow call_srcu() to fire?

In kvm's use case synchronize_srcu_expedited() is usually called when no thread is in a critical section, so we don't have to wait for anything except the srcu machinery.

OK, I'll try and come up with means of making it go fast again ;-)

I cannot resist suggesting a kthread to do the call_srcu(), which would allow synchronize_srcu_expedited() to proceed with its current brute-force speed.

Thanx, Paul

#4 lore raw
Quoted 7 lines · Paul E. McKenney
Quoted 7 lines
Quoted 6 lines

Can we get it back to speed by scheduling a work function on all cpus? wouldn't that force a quiescent state and allow call_srcu() to fire?

In kvm's use case synchronize_srcu_expedited() is usually called when no thread is in a critical section, so we don't have to wait for anything except the srcu machinery.

Quoted 5 lines
Quoted 1 line

OK, I'll try and come up with means of making it go fast again ;-)

I cannot resist suggesting a kthread to do the call_srcu(), which would allow synchronize_srcu_expedited() to proceed with its current brute-force speed.

Right, so I really don't like to add a kthread per srcu instance. Sharing a kthread between all SRCUs will be problematic since these sync things can take forever and so the thread will become a bottlneck.

Also, I'd really like to come up with a better means of sync for SRCU and not hammer the entire machine (3 times).

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

  unsigned int seq1 = 0, seq2 = 0, count = 0;
  int cpu, idx;

  idx = ACCESS_ONCE(sp->completions) & 1;

for_each_possible_cpu(cpu)

	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

for_each_possible_cpu(cpu)

	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

for_each_possible_cpu(cpu)

	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

/*
* there's no active references and no activity, we pass */
if (seq1 == seq2 && count == 0)

	return;
  synchronize_srcu_slow();

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

The other thing I was hoping to be able to pull off is add a copy of idx into the same cacheline as c[] and abuse cache-coherency to avoid some of the sync_sched() calls, but that's currently hurting my brain.

#5 lore raw
Quoted 21 lines · Peter Zijlstra

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

  unsigned int seq1 = 0, seq2 = 0, count = 0;
  int cpu, idx;

  idx = ACCESS_ONCE(sp->completions) & 1;

for_each_possible_cpu(cpu)

	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

for_each_possible_cpu(cpu)

	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

for_each_possible_cpu(cpu)

	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

/*
* there's no active references and no activity, we pass */
if (seq1 == seq2 && count == 0)

Quoted 7 lines
	return;
  synchronize_srcu_slow();

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

Sorry, I was inaccurate. In two of the cases indeed we don't expect guest activity, and we're okay with waiting a bit if there is guest activity - when we're altering the guest physical memory map. But the third case does have concurrent guest activity with synchronize_srcu_expedited() and we still need it fast - that's when userspace reads the dirty bitmap log of a running guest and replaces it with a new bitmap.

There may be a way to convert it to call_srcu() though. Without synchronize_srcu_expedited(), kvm sees both the old and the new bitmaps, but that's fine, since the dirty bits will go *somewhere*, and we can pick them up later in call_srcu(). The only problem is if this is the very last call to kvm_vm_ioctl_get_dirty_log(), and the callback triggers after it returns - we end up with a bag of bits with not one to return them to. Maybe we can detect this conditions (all vcpus ought to be stopped), and do something like:

   if (all vcpus stopped) {
        /* no activity, this should be fast */
        synchronize_srcu()
        /* collect and return bits */
   } else {
        call_srcu(collect bits)
   }

still a snag - we can't reliably detect that all vcpus are stopped, they may be just resting in userspace, and restart while synchronize_srcu() is running.

Marcelo?

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 56 lines · Avi Kivity
Quoted 21 lines · Peter Zijlstra

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

  unsigned int seq1 = 0, seq2 = 0, count = 0;
  int cpu, idx;

  idx = ACCESS_ONCE(sp->completions) & 1;

for_each_possible_cpu(cpu)

	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

for_each_possible_cpu(cpu)

	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

for_each_possible_cpu(cpu)

	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

/*
* there's no active references and no activity, we pass */
if (seq1 == seq2 && count == 0)

Quoted 7 lines
	return;
  synchronize_srcu_slow();

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

Sorry, I was inaccurate. In two of the cases indeed we don't expect guest activity, and we're okay with waiting a bit if there is guest activity - when we're altering the guest physical memory map. But the third case does have concurrent guest activity with synchronize_srcu_expedited() and we still need it fast - that's when userspace reads the dirty bitmap log of a running guest and replaces it with a new bitmap.

There may be a way to convert it to call_srcu() though. Without synchronize_srcu_expedited(), kvm sees both the old and the new bitmaps, but that's fine, since the dirty bits will go *somewhere*, and we can pick them up later in call_srcu(). The only problem is if this is the very last call to kvm_vm_ioctl_get_dirty_log(), and the callback triggers after it returns - we end up with a bag of bits with not one to return them to. Maybe we can detect this conditions (all vcpus ought to be stopped), and do something like:

   if (all vcpus stopped) {
        /* no activity, this should be fast */
        synchronize_srcu()
        /* collect and return bits */
   } else {
        call_srcu(collect bits)
   }
Quoted 6 lines

still a snag - we can't reliably detect that all vcpus are stopped, they may be just resting in userspace, and restart while synchronize_srcu() is running.

Marcelo?

Or something completely different - we can remove srcu from the equation completely in this case. Use just one bitmap (so no rcu_assign_pointer), and use atomic operations to copy and clear:

word = bitmap[i]
put_user(word)
atomic_and(&bitmap[i], ~word)

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 61 lines · Avi Kivity
Quoted 60 lines · Avi Kivity
Quoted 26 lines · Peter Zijlstra

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

   unsigned int seq1 = 0, seq2 = 0, count = 0;
   int cpu, idx;

   idx = ACCESS_ONCE(sp->completions)&  1;
   for_each_possible_cpu(cpu)
	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

   for_each_possible_cpu(cpu)
	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

   for_each_possible_cpu(cpu)
	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

   /*
    * there's no active references and no activity, we pass
    */
   if (seq1 == seq2&&  count == 0)
	return;

   synchronize_srcu_slow();
Quoted 2 lines

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

Sorry, I was inaccurate. In two of the cases indeed we don't expect guest activity, and we're okay with waiting a bit if there is guest activity - when we're altering the guest physical memory map. But the third case does have concurrent guest activity with synchronize_srcu_expedited() and we still need it fast - that's when userspace reads the dirty bitmap log of a running guest and replaces it with a new bitmap.

There may be a way to convert it to call_srcu() though. Without synchronize_srcu_expedited(), kvm sees both the old and the new bitmaps, but that's fine, since the dirty bits will go *somewhere*, and we can pick them up later in call_srcu(). The only problem is if this is the very last call to kvm_vm_ioctl_get_dirty_log(), and the callback triggers after it returns - we end up with a bag of bits with not one to return them to. Maybe we can detect this conditions (all vcpus ought to be stopped), and do something like:

    if (all vcpus stopped) {
         /* no activity, this should be fast */
         synchronize_srcu()
         /* collect and return bits */
    } else {
         call_srcu(collect bits)
    }

still a snag - we can't reliably detect that all vcpus are stopped, they may be just resting in userspace, and restart while synchronize_srcu() is running.

Quoted 5 lines
Quoted 2 lines

Marcelo?

Or something completely different - we can remove srcu from the equation completely in this case. Use just one bitmap (so no

I am already testing various possibilities like this. For VGA, using clear_bit() (+ rmap write protect) works well!

Quoted 7 lines

rcu_assign_pointer), and use atomic operations to copy and clear:

word = bitmap[i]
put_user(word)
atomic_and(&bitmap[i], ~word)

This kind of this was really slow IIRC.

How about just doing:

take a spin_lock
copy the entire (or some portions of) bitmap locally clear the bitmap
unlock

write protect the dirty pages based on the copied dirty data

copy_to_user

I can show you some performance numbers, this weekend, if you like.

Takuya

#8 lore raw
Quoted 12 lines · Takuya Yoshikawa
Quoted 8 lines

rcu_assign_pointer), and use atomic operations to copy and clear:

word = bitmap[i]
put_user(word)
atomic_and(&bitmap[i], ~word)

This kind of this was really slow IIRC.

Quoted 7 lines

How about just doing:

take a spin_lock
copy the entire (or some portions of) bitmap locally clear the bitmap
unlock

That means that vcpus dirtying memory also have to take that lock, and spin while the bitmap is being copied. So kvm_vm_ioctl_get_dirty_log() will become faster, at the expense of vcpus, which I think is a bad tradeoff.

Quoted 7 lines

write protect the dirty pages based on the copied dirty data

copy_to_user

I can show you some performance numbers, this weekend, if you like.

That'll be great, numbers are better than speculation.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Takuya Yoshikawa

How about just doing:

take a spin_lock
copy the entire (or some portions of) bitmap locally clear the bitmap
unlock

Quoted 4 lines

That means that vcpus dirtying memory also have to take that lock, and spin while the bitmap is being copied. So kvm_vm_ioctl_get_dirty_log() will become faster, at the expense of vcpus, which I think is a bad tradeoff.

Almost every caller is already holding the mmu_lock.

Isn't it possible to make others hold the lock only for the case of marking to the bitmap? (I will check myself too.)

Quoted 10 lines
Quoted 7 lines

write protect the dirty pages based on the copied dirty data

copy_to_user

I can show you some performance numbers, this weekend, if you like.

That'll be great, numbers are better than speculation.

Yes, I already have some good numbers to show (and some patches).

Takuya

#10 lore raw
Quoted 11 lines · Takuya Yoshikawa
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Takuya Yoshikawa

How about just doing:

take a spin_lock
copy the entire (or some portions of) bitmap locally clear the bitmap
unlock

Quoted 6 lines
Quoted 4 lines

That means that vcpus dirtying memory also have to take that lock, and spin while the bitmap is being copied. So kvm_vm_ioctl_get_dirty_log() will become faster, at the expense of vcpus, which I think is a bad tradeoff.

Almost every caller is already holding the mmu_lock.

True. But let's not add more locks.

Quoted 3 lines

Isn't it possible to make others hold the lock only for the case of marking to the bitmap? (I will check myself too.)

Yes, in walk_addr(), while setting accessed and dirty bits in guest PTEs, or while emulating instructions and writing to guest memory.

Quoted 5 lines
Quoted 2 lines

That'll be great, numbers are better than speculation.

Yes, I already have some good numbers to show (and some patches).

Looking forward.

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw
In reply to Avi Kivity
Quoted 28 lines · Avi Kivity
Quoted 12 lines · Takuya Yoshikawa
Quoted 8 lines

rcu_assign_pointer), and use atomic operations to copy and clear:

word = bitmap[i]
put_user(word)
atomic_and(&bitmap[i], ~word)

This kind of this was really slow IIRC.

Quoted 7 lines

How about just doing:

take a spin_lock
copy the entire (or some portions of) bitmap locally clear the bitmap
unlock

That means that vcpus dirtying memory also have to take that lock, and spin while the bitmap is being copied. So kvm_vm_ioctl_get_dirty_log() will become faster, at the expense of vcpus, which I think is a bad tradeoff.

Quoted 2 lines

write protect the dirty pages based on the copied dirty data

Quoted 7 lines
Quoted 5 lines

copy_to_user

I can show you some performance numbers, this weekend, if you like.

That'll be great, numbers are better than speculation.

get dirty log: 5634134 ns for 262144 dirty pages

5ms (for the entire operation).

#21 lore raw
In reply to Avi Kivity
Quoted 57 lines · Avi Kivity
Quoted 56 lines · Avi Kivity
Quoted 21 lines · Peter Zijlstra

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

  unsigned int seq1 = 0, seq2 = 0, count = 0;
  int cpu, idx;

  idx = ACCESS_ONCE(sp->completions) & 1;

for_each_possible_cpu(cpu)

	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

for_each_possible_cpu(cpu)

	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

for_each_possible_cpu(cpu)

	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

/*
* there's no active references and no activity, we pass */
if (seq1 == seq2 && count == 0)

Quoted 7 lines
	return;
  synchronize_srcu_slow();

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

Sorry, I was inaccurate. In two of the cases indeed we don't expect guest activity, and we're okay with waiting a bit if there is guest activity - when we're altering the guest physical memory map. But the third case does have concurrent guest activity with synchronize_srcu_expedited() and we still need it fast - that's when userspace reads the dirty bitmap log of a running guest and replaces it with a new bitmap.

There may be a way to convert it to call_srcu() though. Without synchronize_srcu_expedited(), kvm sees both the old and the new bitmaps, but that's fine, since the dirty bits will go *somewhere*, and we can pick them up later in call_srcu(). The only problem is if this is the very last call to kvm_vm_ioctl_get_dirty_log(), and the callback triggers after it returns - we end up with a bag of bits with not one to return them to. Maybe we can detect this conditions (all vcpus ought to be stopped), and do something like:

   if (all vcpus stopped) {
        /* no activity, this should be fast */
        synchronize_srcu()
        /* collect and return bits */
   } else {
        call_srcu(collect bits)
   }
Quoted 6 lines
Quoted 6 lines

still a snag - we can't reliably detect that all vcpus are stopped, they may be just resting in userspace, and restart while synchronize_srcu() is running.

Marcelo?

VGABIOS mode constantly destroys and creates 0xa0000 slot, so performance is required for KVM_SET_MEM too (it can probably be fixed in qemu, but older qemu's must be supported).

For KVM it would be better for synchronize_srcu_expedited() to remain relatively fast (think heavy users of GET_DIRTY_LOG such as Kemari), even if slower than it is today.

synchronize_srcu() takes 10ms or so.

Quoted 11 lines

Or something completely different - we can remove srcu from the equation completely in this case. Use just one bitmap (so no rcu_assign_pointer), and use atomic operations to copy and clear:

word = bitmap[i]
put_user(word)
atomic_and(&bitmap[i], ~word)

Signature · 1 line
error compiling committee.c: too many arguments to function
#34 lore raw
In reply to Peter Zijlstra
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Paul E. McKenney
Quoted 8 lines
Quoted 6 lines

Can we get it back to speed by scheduling a work function on all cpus? wouldn't that force a quiescent state and allow call_srcu() to fire?

In kvm's use case synchronize_srcu_expedited() is usually called when no thread is in a critical section, so we don't have to wait for anything except the srcu machinery.

OK, I'll try and come up with means of making it go fast again ;-)

Quoted 7 lines
Quoted 3 lines

I cannot resist suggesting a kthread to do the call_srcu(), which would allow synchronize_srcu_expedited() to proceed with its current brute-force speed.

Right, so I really don't like to add a kthread per srcu instance. Sharing a kthread between all SRCUs will be problematic since these sync things can take forever and so the thread will become a bottlneck.

It should be possible to substitute call_rcu_sched() for synchronize_sched() and use a per-SRCU-instance state machine to avoid a given instance from blocking progress on the other instances.

Quoted 2 lines

Also, I'd really like to come up with a better means of sync for SRCU and not hammer the entire machine (3 times).

We could introduce read-side smp_mb()s and reduce to one machine-hammer straightforwardly. It should be possible to make it so that if a given srcu_struct ever uses synchronize_srcu_expedited(), it is required to use the slower version of the read-side primitives.

Hmmm.. I should go back and take a look at the 8 or 9 variants that I posted to LKML when I first worked on this.

Quoted 21 lines

One of the things I was thinking of is adding a sequence counter in the per-cpu data. Using that we could do something like:

  unsigned int seq1 = 0, seq2 = 0, count = 0;
  int cpu, idx;

  idx = ACCESS_ONCE(sp->completions) & 1;

for_each_possible_cpu(cpu)

	seq1 += per_cpu(sp->per_cpu_ref, cpu)->seq;

for_each_possible_cpu(cpu)

	count += per_cpu(sp->per_cpu_ref, cpu)->c[idx];

for_each_possible_cpu(cpu)

	seq2 += per_cpu(sp->per_cpu_ref, cpu)->seq;

/*
* there's no active references and no activity, we pass */
if (seq1 == seq2 && count == 0)

Quoted 7 lines
	return;
  synchronize_srcu_slow();

This would add a fast-path which should catch the case Avi outlined where we call sync_srcu() when there's no other SRCU activity.

Overflow could give us false positives for workloads with frequent readers. Yes, I know you don't like 32-bit systems, but SRCU still has to work on them. ;-)

We also need some smp_mb() calls for readers and in the above sequence for this to have half a chance of working.

Quoted 3 lines

The other thing I was hoping to be able to pull off is add a copy of idx into the same cacheline as c[] and abuse cache-coherency to avoid some of the sync_sched() calls, but that's currently hurting my brain.

I will check with some hardware architects. Last I knew, this was not officially supported, but it cannot hurt to ask again.

I suppose that another approach would be to use bitfields. Fifteen bits should be enough for each of the counters, but ouch! Atomic instructions would be required to make incrementing all the idx bits safe.

Thanx, Paul

#20 lore raw
In reply to Marcelo Tosatti
Quoted 8 lines · On Wed, 1 Feb 2012 11:43:47 -0200 Marcelo Tosatti <[email protected]> wrote:
Quoted 3 lines
Quoted 1 line

I can show you some performance numbers, this weekend, if you like.

That'll be great, numbers are better than speculation.

get dirty log: 5634134 ns for 262144 dirty pages

5ms (for the entire operation).

I have mixed my personal patch series and Avi's comments, and ...

I am now getting really good improvements for every number of dirty pages. (might be a dream, so checking now ... will soon report)

Takuya

#11 lore raw

[test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 5 lines
Quoted 2 lines

That'll be great, numbers are better than speculation.

Yes, I already have some good numbers to show (and some patches).

Looking forward.

I made a patch to see if Avi's suggestion of getting rid of srcu update for dirty logging is practical; tested with my unit-test.

(I used a function to write protect a range of pages using rmap,
which is itself useful for optimizing the current code.)

1. test result

on 32bit host (core i3 box) // just for the unit-test ...
slot size: 256K pages (1GB memory)

Measured by dirty-log-perf (executed only once for each case)

Note: dirty pages are completely distributed
(no locality: worst case for my patch?)

=========================================================

# of dirty pages:  kvm.git (ns),  with this patch (ns)
1:         102,077 ns      10,105 ns
2:          47,197 ns       9,395 ns
4:          43,563 ns       9,938 ns
8:          41,239 ns      10,618 ns
16:         42,988 ns      12,299 ns
32:         45,503 ns      14,298 ns
64:         50,915 ns      19,895 ns
128:        61,087 ns      29,260 ns
256:        81,007 ns      49,023 ns
512:       132,776 ns      86,670 ns
1024:      939,299 ns     131,496 ns
2048:      992,209 ns     250,429 ns
4096:      891,809 ns     479,280 ns
8192:    1,027,280 ns     906,971 ns
(until now pretty good)

(ah, for every 32-bit atomic clear mask ...)

16384:   1,270,972 ns   6,661,741 ns    //  1  1  1 ...  1
32768:   1,581,335 ns   9,673,985 ns    //  ...
65536:   2,161,604 ns  11,466,134 ns    //  ...
131072:  3,253,027 ns  13,412,954 ns    //  ...
262144:  5,663,002 ns  16,309,924 ns    // 31 31 31 ... 31

=========================================================

According to a 2005 usenix paper, WWS with a 8sec window was about 50,000 pages for a high dirtying rate program.

Taking into acount of another possible gains from the WWS locality of real workloads, these numbers are not so bad IMO.

Furthermore the code has been made for initial test only and I did not do any optimization: I know what I should try.

So this seems worth more testing.

The new code also makes it possible to do find-grained get dirty log. Live migration can be done like this ??? (not sure yet):

until the dirty rate becomes enough low
get dirty log for the first 32K pages (partial return is OK) while sending
get dirty log for the next 32K pages (partial return is OK) while sending
...
get dirty log for the last 32K pages (partial return is OK)

stop the guest and get dirty log (but no need to write protect now) send the remaining pages

New API is needed for this as discussed before!

Thanks,
Takuya

2. test patch

[PATCH for test only] KVM: dirty logging: avoid srcu and ...

VGA works normally but not tested enough yet.

---
 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/mmu.c              |   48 ++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |   55 ++++++++++++++++----------------------
 include/linux/kvm_host.h        |    7 +++++
 virt/kvm/kvm_main.c             |    7 +----
 5 files changed, 79 insertions(+), 40 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 4610166..e9bbef1 100644
@@ -684,6 +684,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
 
 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
+void kvm_mmu_write_protect_range(struct kvm *kvm, struct kvm_memory_slot *slot,
+				gfn_t start, gfn_t end);
 int kvm_mmu_rmap_write_protect(struct kvm *kvm, u64 gfn,
 			       struct kvm_memory_slot *slot);
 void kvm_mmu_zap_all(struct kvm *kvm);
arch/x86/kvm/mmu.c +46 -2

60 lines not on this page · raw message

arch/x86/kvm/x86.c +23 -32

74 lines not on this page · raw message

include/linux/kvm_host.h +7 -0
index eada8e6..06d4e41 100644
@@ -656,6 +656,13 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
 	return gfn_to_memslot(kvm, gfn)->id;
 }
 
+static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
+{
+	/* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
+	return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
+		(base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
+}
+
 static inline unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
 					       gfn_t gfn)
 {
virt/kvm/kvm_main.c +1 -6
index 9f32bff..e483ae4 100644
@@ -797,15 +797,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		int lpages;
 		int level = i + 2;
 
-		/* Avoid unused variable warning if no large pages */
-		(void)level;
-
 		if (new.lpage_info[i])
 			continue;
 
-		lpages = 1 + ((base_gfn + npages - 1)
-			     >> KVM_HPAGE_GFN_SHIFT(level));
-		lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
+		lpages = gfn_to_index(base_gfn + npages - 1, base_gfn, level) + 1;
 
 		new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
 
Signature · 1 line
1.7.5.4
#12 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 45 lines · Takuya Yoshikawa
Quoted 7 lines · Avi Kivity
Quoted 5 lines
Quoted 2 lines

That'll be great, numbers are better than speculation.

Yes, I already have some good numbers to show (and some patches).

Looking forward.

I made a patch to see if Avi's suggestion of getting rid of srcu update for dirty logging is practical; tested with my unit-test.

(I used a function to write protect a range of pages using rmap,
which is itself useful for optimizing the current code.)

1. test result

on 32bit host (core i3 box) // just for the unit-test ...
slot size: 256K pages (1GB memory)

Measured by dirty-log-perf (executed only once for each case)

Note: dirty pages are completely distributed
(no locality: worst case for my patch?)

=========================================================

# of dirty pages:  kvm.git (ns),  with this patch (ns)
1:         102,077 ns      10,105 ns
2:          47,197 ns       9,395 ns
4:          43,563 ns       9,938 ns
8:          41,239 ns      10,618 ns
16:         42,988 ns      12,299 ns
32:         45,503 ns      14,298 ns
64:         50,915 ns      19,895 ns
128:        61,087 ns      29,260 ns
256:        81,007 ns      49,023 ns
512:       132,776 ns      86,670 ns
1024:      939,299 ns     131,496 ns
2048:      992,209 ns     250,429 ns
4096:      891,809 ns     479,280 ns
8192:    1,027,280 ns     906,971 ns
(until now pretty good)
Quoted 7 lines

(ah, for every 32-bit atomic clear mask ...)

16384:   1,270,972 ns   6,661,741 ns    //  1  1  1 ...  1
32768:   1,581,335 ns   9,673,985 ns    //  ...
65536:   2,161,604 ns  11,466,134 ns    //  ...
131072:  3,253,027 ns  13,412,954 ns    //  ...
262144:  5,663,002 ns  16,309,924 ns    // 31 31 31 ... 31

=========================================================

On a 64-bit host, this will be twice as fast. Or if we use cmpxchg16b, and there are no surprises, four times as fast. It will still be slower than the original, but by a smaller margin.

Quoted 5 lines

According to a 2005 usenix paper, WWS with a 8sec window was about 50,000 pages for a high dirtying rate program.

Taking into acount of another possible gains from the WWS locality of real workloads, these numbers are not so bad IMO.

I agree.

Quoted 18 lines

Furthermore the code has been made for initial test only and I did not do any optimization: I know what I should try.

So this seems worth more testing.

The new code also makes it possible to do find-grained get dirty log. Live migration can be done like this ??? (not sure yet):

until the dirty rate becomes enough low
get dirty log for the first 32K pages (partial return is OK) while sending
get dirty log for the next 32K pages (partial return is OK) while sending
...
get dirty log for the last 32K pages (partial return is OK)

Quoted 4 lines

stop the guest and get dirty log (but no need to write protect now) send the remaining pages

New API is needed for this as discussed before!

Yeah. But I think we should switch to srcu-less dirty logs regardless. Here are you numbers, but normalized by the number of dirty pages.

dirty pages old (ns/page) new (ns/page)

1    102077    10105
2    23599    4698
4    10891    2485
8    5155    1327
16    2687    769
32    1422    447
64    796    311
128    477    229
256    316    191
512    259    169
1024    917    128
2048    484    122
4096    218    117
8192    125    111
16384    78    407
32768    48    295
65536    33    175
131072    25    102
262144    22    62

Your worst case, when considering a reasonable number of dirty pages, is 407ns/page, which is still lower than what userspace will actually do to process the page, so it's reasonable. The old method is often a lot worse than your worst case, by this metric.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 27 lines · Avi Kivity
Quoted 19 lines

=========================================================

# of dirty pages:  kvm.git (ns),  with this patch (ns)
1:         102,077 ns      10,105 ns
2:          47,197 ns       9,395 ns
4:          43,563 ns       9,938 ns
8:          41,239 ns      10,618 ns
16:         42,988 ns      12,299 ns
32:         45,503 ns      14,298 ns
64:         50,915 ns      19,895 ns
128:        61,087 ns      29,260 ns
256:        81,007 ns      49,023 ns
512:       132,776 ns      86,670 ns
1024:      939,299 ns     131,496 ns
2048:      992,209 ns     250,429 ns
4096:      891,809 ns     479,280 ns
8192:    1,027,280 ns     906,971 ns
(until now pretty good)
Quoted 7 lines

(ah, for every 32-bit atomic clear mask ...)

16384:   1,270,972 ns   6,661,741 ns    //  1  1  1 ...  1
32768:   1,581,335 ns   9,673,985 ns    //  ...
65536:   2,161,604 ns  11,466,134 ns    //  ...
131072:  3,253,027 ns  13,412,954 ns    //  ...
262144:  5,663,002 ns  16,309,924 ns    // 31 31 31 ... 31

=========================================================

Quoted 3 lines

On a 64-bit host, this will be twice as fast. Or if we use cmpxchg16b, and there are no surprises, four times as fast. It will still be slower than the original, but by a smaller margin.

Yes.

I used "unsigned int" just because I wanted to use the current atomic_clear_mask() as is.

We need to implement atomic_clear_mask_long() or use ...

Quoted 3 lines

Yeah. But I think we should switch to srcu-less dirty logs regardless. Here are you numbers, but normalized by the number of dirty pages.

Thanks,

I can prepare the official patch series then, of course with more test.

Takuya

Quoted 30 lines

dirty pages old (ns/page) new (ns/page)

1    102077    10105
2    23599    4698
4    10891    2485
8    5155    1327
16    2687    769
32    1422    447
64    796    311
128    477    229
256    316    191
512    259    169
1024    917    128
2048    484    122
4096    218    117
8192    125    111
16384    78    407
32768    48    295
65536    33    175
131072    25    102
262144    22    62

Your worst case, when considering a reasonable number of dirty pages, is 407ns/page, which is still lower than what userspace will actually do to process the page, so it's reasonable. The old method is often a lot worse than your worst case, by this metric.

#14 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 33 lines · Takuya Yoshikawa
Quoted 27 lines · Avi Kivity
Quoted 19 lines

=========================================================

# of dirty pages:  kvm.git (ns),  with this patch (ns)
1:         102,077 ns      10,105 ns
2:          47,197 ns       9,395 ns
4:          43,563 ns       9,938 ns
8:          41,239 ns      10,618 ns
16:         42,988 ns      12,299 ns
32:         45,503 ns      14,298 ns
64:         50,915 ns      19,895 ns
128:        61,087 ns      29,260 ns
256:        81,007 ns      49,023 ns
512:       132,776 ns      86,670 ns
1024:      939,299 ns     131,496 ns
2048:      992,209 ns     250,429 ns
4096:      891,809 ns     479,280 ns
8192:    1,027,280 ns     906,971 ns
(until now pretty good)
Quoted 7 lines

(ah, for every 32-bit atomic clear mask ...)

16384:   1,270,972 ns   6,661,741 ns    //  1  1  1 ...  1
32768:   1,581,335 ns   9,673,985 ns    //  ...
65536:   2,161,604 ns  11,466,134 ns    //  ...
131072:  3,253,027 ns  13,412,954 ns    //  ...
262144:  5,663,002 ns  16,309,924 ns    // 31 31 31 ... 31

=========================================================

Quoted 3 lines

On a 64-bit host, this will be twice as fast. Or if we use cmpxchg16b, and there are no surprises, four times as fast. It will still be slower than the original, but by a smaller margin.

Quoted 6 lines

Yes.

I used "unsigned int" just because I wanted to use the current atomic_clear_mask() as is.

We need to implement atomic_clear_mask_long() or use ...

If we use cmpxchg8b/cmpxchg16b then this won't fit with the atomic_*_long family.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 9 lines · Avi Kivity
Quoted 5 lines

I used "unsigned int" just because I wanted to use the current atomic_clear_mask() as is.

We need to implement atomic_clear_mask_long() or use ...

If we use cmpxchg8b/cmpxchg16b then this won't fit with the atomic_*_long family.

OK, I will try.

I have one concern about correctness issue though:

concurrent rmap write protection may not be safe due to delayed tlb flush ... cannot happen?

Takuya

#16 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 5 lines · Takuya Yoshikawa

I have one concern about correctness issue though:

concurrent rmap write protection may not be safe due to delayed tlb flush ... cannot happen?

What do you mean by concurrent rmap write protection?

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 7 lines · Avi Kivity
Quoted 4 lines

I have one concern about correctness issue though:

concurrent rmap write protection may not be safe due to delayed tlb flush ... cannot happen?

What do you mean by concurrent rmap write protection?

Not sure, but other codes like:

- mmu_sync_children()

	for_each_sp(pages, sp, parents, i)
		protected |= rmap_write_protect(vcpu->kvm, sp->gfn);

		if (protected)
			kvm_flush_remote_tlbs(vcpu->kvm);

- kvm_mmu_get_page()

	if (rmap_write_protect(vcpu->kvm, gfn))
		kvm_flush_remote_tlbs(vcpu->kvm);

I just wondered what can happen if GET_DIRTY_LOG is being processed behind these processing?

They may find nothing to write protect and won't do kvm_flush_remote_tlbs() if the gfn has been already protected by GET_DIRTY_LOG.

But GET_DIRTY_LOG may still be busy write protecting other pages and others can return before. (My code releases mmu_lock to not include __put_user() in the critical section.)

I am not still enough familier with these code yet. (maybe empty concern)

Takuya

#18 lore raw

Re: [test result] dirty logging without srcu update -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 19 lines · Takuya Yoshikawa
Quoted 7 lines · Avi Kivity
Quoted 4 lines

I have one concern about correctness issue though:

concurrent rmap write protection may not be safe due to delayed tlb flush ... cannot happen?

What do you mean by concurrent rmap write protection?

Not sure, but other codes like:

- mmu_sync_children()

	for_each_sp(pages, sp, parents, i)
		protected |= rmap_write_protect(vcpu->kvm, sp->gfn);

		if (protected)
			kvm_flush_remote_tlbs(vcpu->kvm);
Quoted 6 lines

- kvm_mmu_get_page()

	if (rmap_write_protect(vcpu->kvm, gfn))
		kvm_flush_remote_tlbs(vcpu->kvm);

I just wondered what can happen if GET_DIRTY_LOG is being processed behind these processing?

It's a bug. If the flush happens outside the spinlock, then one of the callers can return before it is assured the tlb is flushed.

A B

spin_lock
clear pte.w
spin_unlock
spin_lock
pte.w already clear
spin_unlock
skip flush
return
flush

Quoted 10 lines

They may find nothing to write protect and won't do kvm_flush_remote_tlbs() if the gfn has been already protected by GET_DIRTY_LOG.

But GET_DIRTY_LOG may still be busy write protecting other pages and others can return before. (My code releases mmu_lock to not include __put_user() in the critical section.)

I am not still enough familier with these code yet.

It's actually an advantage, since you don't have any assumptions on how the code works.

Quoted 1 line

(maybe empty concern)

Nope, good catch of this subtle bug.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw

[RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Marcelo Tosatti

[Dropped non-kvm members from cc]

Quoted 3 lines · Marcelo Tosatti

VGABIOS mode constantly destroys and creates 0xa0000 slot, so performance is required for KVM_SET_MEM too (it can probably be fixed in qemu, but older qemu's must be supported).

Apart from srcu, I see some problems concerning slot creation/destruction:
heavy shadow flush (and extra write protection).

Look at __kvm_set_memory_region().

1. When we invalidate a memory slot, we call kvm_arch_flush_shadow() and zap all shadow pages, not restricted to that slot.

	/* From this point no new shadow pages pointing to a deleted
	 * memslot will be created.
	 *
	 * validation of sp->gfn happens in:
	 *	- gfn_to_hva (kvm_read_guest, gfn_to_pfn)
	 *	- kvm_is_visible_gfn (mmu_check_roots)
	 */
	kvm_arch_flush_shadow(kvm);

2. When we create(and shift?) a memory slot, we call kvm_arch_flush_shadow() to clear all mmio sptes, again not restricted to that slot.

	/*
	 * If the new memory slot is created, we need to clear all
	 * mmio sptes.
	 */
	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
		kvm_arch_flush_shadow(kvm);

3. In addition to these, we do write-protect all pages in that slot in kvm_arch_commit_memory_region() every time.

For 1: I made a patch to restrict the flush to that slot by using sp->slot_bitmap. (seems working here)

For 2: I think we can do the same: not 100% sure yet because I am still struggling with the "mmio spte optimization" code. (really hacky ...)

For 3: I think doing both "write protection" and "shadow flush" is unnecessary.

BTW do we really need fast slot creation/destruction?

Takuya

#23 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 40 lines · Takuya Yoshikawa

[Dropped non-kvm members from cc]

Quoted 3 lines · Marcelo Tosatti

VGABIOS mode constantly destroys and creates 0xa0000 slot, so performance is required for KVM_SET_MEM too (it can probably be fixed in qemu, but older qemu's must be supported).

Apart from srcu, I see some problems concerning slot creation/destruction:
heavy shadow flush (and extra write protection).

Look at __kvm_set_memory_region().

1. When we invalidate a memory slot, we call kvm_arch_flush_shadow() and zap all shadow pages, not restricted to that slot.

	/* From this point no new shadow pages pointing to a deleted
	 * memslot will be created.
	 *
	 * validation of sp->gfn happens in:
	 *	- gfn_to_hva (kvm_read_guest, gfn_to_pfn)
	 *	- kvm_is_visible_gfn (mmu_check_roots)
	 */
	kvm_arch_flush_shadow(kvm);

2. When we create(and shift?) a memory slot, we call kvm_arch_flush_shadow() to clear all mmio sptes, again not restricted to that slot.

	/*
	 * If the new memory slot is created, we need to clear all
	 * mmio sptes.
	 */
	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
		kvm_arch_flush_shadow(kvm);

3. In addition to these, we do write-protect all pages in that slot in kvm_arch_commit_memory_region() every time.

Quoted 5 lines

For 1: I made a patch to restrict the flush to that slot by using sp->slot_bitmap. (seems working here)

For 2: I think we can do the same: not 100% sure yet because I am still struggling with the "mmio spte optimization" code. (really hacky ...)

Yes, you can. The flush is there because a new slot invalidates any mmio sptes in that region, but in the region covered by the new slot only.

Quoted 1 line

For 3: I think doing both "write protection" and "shadow flush" is unnecessary.

If you enable dirty logging on a slot, certainly you have to write protect?

Quoted 1 line

BTW do we really need fast slot creation/destruction?

At the moment yes. Boot a RHEL/Fedora installation disk (or any other guest which uses SYSLINUX splash screen) and you will see. That particular case is a limitation of cirrus in QEMU, ideally it should be optimized there.

#24 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 4 lines · On Wed, 8 Feb 2012 16:45:31 -0200 Marcelo Tosatti <[email protected]> wrote:
Quoted 1 line

For 3: I think doing both "write protection" and "shadow flush" is unnecessary.

If you enable dirty logging on a slot, certainly you have to write protect?

When we enable dirty logging, yes.

Quoted 8 lines
Quoted 2 lines

BTW do we really need fast slot creation/destruction?

At the moment yes. Boot a RHEL/Fedora installation disk (or any other guest which uses SYSLINUX splash screen) and you will see. That particular case is a limitation of cirrus in QEMU, ideally it should be optimized there.

I've checked; limiting flush to that slot seems to be good. I will send a patch soon.

Takuya

#28 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Takuya Yoshikawa
Quoted 17 lines · Takuya Yoshikawa

[Dropped non-kvm members from cc]

Quoted 3 lines · Marcelo Tosatti

VGABIOS mode constantly destroys and creates 0xa0000 slot, so performance is required for KVM_SET_MEM too (it can probably be fixed in qemu, but older qemu's must be supported).

Apart from srcu, I see some problems concerning slot creation/destruction:
heavy shadow flush (and extra write protection).

Look at __kvm_set_memory_region().

1. When we invalidate a memory slot, we call kvm_arch_flush_shadow() and zap all shadow pages, not restricted to that slot.

Quoted 8 lines
	/* From this point no new shadow pages pointing to a deleted
	 * memslot will be created.
	 *
	 * validation of sp->gfn happens in:
	 *	- gfn_to_hva (kvm_read_guest, gfn_to_pfn)
	 *	- kvm_is_visible_gfn (mmu_check_roots)
	 */
	kvm_arch_flush_shadow(kvm);

The workloads that exercise slot removal heavily usually do so in a tight loop, so flushing all shadow is not too bad (not much to flush).

Quoted 10 lines

2. When we create(and shift?) a memory slot, we call kvm_arch_flush_shadow() to clear all mmio sptes, again not restricted to that slot.

	/*
	 * If the new memory slot is created, we need to clear all
	 * mmio sptes.
	 */
	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
		kvm_arch_flush_shadow(kvm);

This is pretty rare outside the previous scenario (memory/pci hotplug).

Quoted 5 lines

3. In addition to these, we do write-protect all pages in that slot in kvm_arch_commit_memory_region() every time.

Quoted 7 lines

For 1: I made a patch to restrict the flush to that slot by using sp->slot_bitmap. (seems working here)

For 2: I think we can do the same: not 100% sure yet because I am still struggling with the "mmio spte optimization" code. (really hacky ...)

For 3: I think doing both "write protection" and "shadow flush" is unnecessary.

Maybe it only needs to be done if the only change is enabling dirty logging.

Quoted 1 line

BTW do we really need fast slot creation/destruction?

Not really, but it's good to have infrastructure that copes with different workloads. If the patches keep the code simple I think it's a good thing to have.

Signature · 1 line
error compiling committee.c: too many arguments to function
#29 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

Quoted 5 lines · Avi Kivity
Quoted 1 line

BTW do we really need fast slot creation/destruction?

Not really, but it's good to have infrastructure that copes with different workloads. If the patches keep the code simple I think it's a good thing to have.

To qualify - taking several tens of milliseconds is out of the question as some workloads grind to a halt. But it doesn't need to be incredibly fast.

Signature · 1 line
error compiling committee.c: too many arguments to function
#25 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Marcelo Tosatti
Quoted 4 lines · Marcelo Tosatti
Quoted 1 line

BTW do we really need fast slot creation/destruction?

At the moment yes. Boot a RHEL/Fedora installation disk (or any other guest which uses SYSLINUX splash screen) and you will see.

Another workload that suffers is Windows XP clearing the screen during boot.

Quoted 3 lines

That
particular case is a limitation of cirrus in QEMU, ideally it should be optimized there.

Why do you say that?

Signature · 1 line
error compiling committee.c: too many arguments to function
#30 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Avi Kivity
Quoted 1 line

BTW do we really need fast slot creation/destruction?

Not really, but it's good to have infrastructure that copes with different workloads. If the patches keep the code simple I think it's a good thing to have.

My patch is pretty simple.

Quoted 3 lines

To qualify - taking several tens of milliseconds is out of the question as some workloads grind to a halt. But it doesn't need to be incredibly fast.

I think the problem is not how long it takes to create/invalidate a slot but following faults and shadow pages reconstruction.

Similar to mmu shrinker problem we discussed before.

E.g. users will not expect sudden latency induced by pci hotplug.

Takuya

#32 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Avi Kivity
Quoted 11 lines · Avi Kivity
Quoted 9 lines

2. When we create(and shift?) a memory slot, we call kvm_arch_flush_shadow() to clear all mmio sptes, again not restricted to that slot.

	/*
	 * If the new memory slot is created, we need to clear all
	 * mmio sptes.
	 */
	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
		kvm_arch_flush_shadow(kvm);

This is pretty rare outside the previous scenario (memory/pci hotplug).

Is this condition correct?

When npages != 0 and old.npages == 0, the slot is being newly created, do we really need to flush shadow pages?

This should be

	if (npages && old.npages && (old.base_gfn != base_gfn))

No?

Takuya

#26 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Marcelo Tosatti
Quoted 1 line

BTW do we really need fast slot creation/destruction?

At the moment yes. Boot a RHEL/Fedora installation disk (or any other guest which uses SYSLINUX splash screen) and you will see.

Quoted 7 lines

Another workload that suffers is Windows XP clearing the screen during boot.

Quoted 3 lines

That
particular case is a limitation of cirrus in QEMU, ideally it should be optimized there.

Why do you say that?

There is no fundamental need to create/destroy the 0xa0000 VGA memory slot repeatedly.

But you are right that the aim should be decent performance nevertheless.

#31 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Takuya Yoshikawa
Quoted 9 lines · Takuya Yoshikawa
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Avi Kivity
Quoted 1 line

BTW do we really need fast slot creation/destruction?

Not really, but it's good to have infrastructure that copes with different workloads. If the patches keep the code simple I think it's a good thing to have.

Quoted 8 lines

My patch is pretty simple.

Quoted 3 lines

To qualify - taking several tens of milliseconds is out of the question as some workloads grind to a halt. But it doesn't need to be incredibly fast.

I think the problem is not how long it takes to create/invalidate a slot but following faults and shadow pages reconstruction.

The problem which is immediatelly visible is kvm_set_mem performance.

Quoted 5 lines

Similar to mmu shrinker problem we discussed before.

E.g. users will not expect sudden latency induced by pci hotplug.

Takuya

#33 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Takuya Yoshikawa
Quoted 14 lines · Takuya Yoshikawa
Quoted 11 lines · Avi Kivity
Quoted 9 lines

2. When we create(and shift?) a memory slot, we call kvm_arch_flush_shadow() to clear all mmio sptes, again not restricted to that slot.

	/*
	 * If the new memory slot is created, we need to clear all
	 * mmio sptes.
	 */
	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
		kvm_arch_flush_shadow(kvm);

This is pretty rare outside the previous scenario (memory/pci hotplug).

Quoted 8 lines

Is this condition correct?

When npages != 0 and old.npages == 0, the slot is being newly created, do we really need to flush shadow pages?

This should be

	if (npages && old.npages && (old.base_gfn != base_gfn))

Your condition is more correct, but in practice there's no difference. If old.npages == 0, then old.base_gfn will be 0, and the condition will fail, except for the first slot created (when the shadow cache is empty anyway).

Signature · 1 line
error compiling committee.c: too many arguments to function
#27 lore raw

Re: [RFC] need to improve slot creation/destruction? -- Re: [RFC][PATCH] srcu: Implement call_srcu()

In reply to Marcelo Tosatti
Quoted 9 lines · Marcelo Tosatti
Quoted 8 lines · Avi Kivity
Quoted 4 lines · Marcelo Tosatti
Quoted 1 line

BTW do we really need fast slot creation/destruction?

At the moment yes. Boot a RHEL/Fedora installation disk (or any other guest which uses SYSLINUX splash screen) and you will see.

Another workload that suffers is Windows XP clearing the screen during boot.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

That
particular case is a limitation of cirrus in QEMU, ideally it should be optimized there.

Why do you say that?

There is no fundamental need to create/destroy the 0xa0000 VGA memory slot repeatedly.

If the guest writes to it, then the need exists.

Quoted 2 lines

But you are right that the aim should be decent performance nevertheless.

Signature · 1 line
error compiling committee.c: too many arguments to function