[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20210414091839.mvm6ffmfdoy44e3l@kamzik.brq.redhat.com>
Date: Wed, 14 Apr 2021 11:18:39 +0200
From: Andrew Jones <drjones@...hat.com>
To: Peter Xu <peterx@...hat.com>
Cc: kvm@...r.kernel.org, linux-kernel@...r.kernel.org,
Paolo Bonzini <pbonzini@...hat.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Sean Christopherson <seanjc@...gle.com>
Subject: Re: [PATCH v2] kvm/selftests: Fix race condition with dirty_log_test
On Tue, Apr 13, 2021 at 05:36:41PM -0400, Peter Xu wrote:
> This fixes a bug that can trigger with e.g. "taskset -c 0 ./dirty_log_test" or
> when the testing host is very busy.
>
> The issue is when the vcpu thread got the dirty bit set but got preempted by
> other threads _before_ the data is written, we won't be able to see the latest
> data only until the vcpu threads do VMENTER. IOW, the guest write operation and
> dirty bit set cannot guarantee atomicity. The race could look like:
>
> main thread vcpu thread
> =========== ===========
> iteration=X
> *addr = X
> (so latest data is X)
> iteration=X+1
> ...
> iteration=X+N
> guest executes "*addr = X+N"
> reg=READ_ONCE(iteration)=X+N
> host page fault
> set dirty bit for page "addr"
> (_before_ VMENTER happens...
> so *addr is still X!)
> vcpu thread got preempted
> get dirty log
> verify data
> detected dirty bit set, data is X
> not X+N nor X+N-1, data too old!
>
> This patch closes this race by allowing the main thread to give the vcpu thread
> chance to do a VMENTER to complete that write operation. It's done by adding a
> vcpu loop counter (must be defined as volatile as main thread will do read
> loop), then the main thread can guarantee the vcpu got at least another VMENTER
> by making sure the guest_vcpu_loops increases by 2.
>
> Dirty ring does not need this since dirty_ring_last_page would already help
> avoid this specific race condition.
>
> Cc: Andrew Jones <drjones@...hat.com>
> Cc: Paolo Bonzini <pbonzini@...hat.com>
> Cc: Vitaly Kuznetsov <vkuznets@...hat.com>
> Cc: Sean Christopherson <seanjc@...gle.com>
> Signed-off-by: Peter Xu <peterx@...hat.com>
> ---
> v2:
> - drop one unnecessary check on "!matched"
> ---
> tools/testing/selftests/kvm/dirty_log_test.c | 53 +++++++++++++++++++-
> 1 file changed, 52 insertions(+), 1 deletion(-)
>
Reviewed-by: Andrew Jones <drjones@...hat.com>
Powered by blists - more mailing lists