qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kvm: dirty-ring: Fix race with vcpu creation
@ 2022-09-27 15:46 Peter Xu
  2022-10-14 16:33 ` Peter Xu
  2023-02-05 23:00 ` Peter Xu
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Xu @ 2022-09-27 15:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: Gavin Shan, Paolo Bonzini, peterx, Xiaohui Li

It's possible that we want to reap a dirty ring on a vcpu that is during
creation, because the vcpu is put onto list (CPU_FOREACH visible) before
initialization of the structures.  In this case:

qemu_init_vcpu
    x86_cpu_realizefn
        cpu_exec_realizefn
            cpu_list_add      <---- can be probed by CPU_FOREACH
        qemu_init_vcpu
            cpus_accel->create_vcpu_thread(cpu);
                kvm_init_vcpu
                    map kvm_dirty_gfns  <--- kvm_dirty_gfns valid

Don't try to reap dirty ring on vcpus during creation or it'll crash.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2124756
Reported-by: Xiaohui Li <xiaohli@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 accel/kvm/kvm-all.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 5acab1767f..df5fabd3a8 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -757,6 +757,15 @@ static uint32_t kvm_dirty_ring_reap_one(KVMState *s, CPUState *cpu)
     uint32_t ring_size = s->kvm_dirty_ring_size;
     uint32_t count = 0, fetch = cpu->kvm_fetch_index;
 
+    /*
+     * It's possible that we race with vcpu creation code where the vcpu is
+     * put onto the vcpus list but not yet initialized the dirty ring
+     * structures.  If so, skip it.
+     */
+    if (!cpu->created) {
+        return 0;
+    }
+
     assert(dirty_gfns && ring_size);
     trace_kvm_dirty_ring_reap_vcpu(cpu->cpu_index);
 
-- 
2.37.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: dirty-ring: Fix race with vcpu creation
  2022-09-27 15:46 [PATCH] kvm: dirty-ring: Fix race with vcpu creation Peter Xu
@ 2022-10-14 16:33 ` Peter Xu
  2023-02-05 23:00 ` Peter Xu
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Xu @ 2022-10-14 16:33 UTC (permalink / raw)
  To: qemu-devel; +Cc: Gavin Shan, Paolo Bonzini, Xiaohui Li

Ping?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: dirty-ring: Fix race with vcpu creation
  2022-09-27 15:46 [PATCH] kvm: dirty-ring: Fix race with vcpu creation Peter Xu
  2022-10-14 16:33 ` Peter Xu
@ 2023-02-05 23:00 ` Peter Xu
  2023-02-21 22:39   ` Gavin Shan
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Xu @ 2023-02-05 23:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: Gavin Shan, Paolo Bonzini, Xiaohui Li

Ping

On Tue, Sep 27, 2022 at 11:46:53AM -0400, Peter Xu wrote:
> It's possible that we want to reap a dirty ring on a vcpu that is during
> creation, because the vcpu is put onto list (CPU_FOREACH visible) before
> initialization of the structures.  In this case:
> 
> qemu_init_vcpu
>     x86_cpu_realizefn
>         cpu_exec_realizefn
>             cpu_list_add      <---- can be probed by CPU_FOREACH
>         qemu_init_vcpu
>             cpus_accel->create_vcpu_thread(cpu);
>                 kvm_init_vcpu
>                     map kvm_dirty_gfns  <--- kvm_dirty_gfns valid
> 
> Don't try to reap dirty ring on vcpus during creation or it'll crash.
> 
> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2124756
> Reported-by: Xiaohui Li <xiaohli@redhat.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  accel/kvm/kvm-all.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 5acab1767f..df5fabd3a8 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -757,6 +757,15 @@ static uint32_t kvm_dirty_ring_reap_one(KVMState *s, CPUState *cpu)
>      uint32_t ring_size = s->kvm_dirty_ring_size;
>      uint32_t count = 0, fetch = cpu->kvm_fetch_index;
>  
> +    /*
> +     * It's possible that we race with vcpu creation code where the vcpu is
> +     * put onto the vcpus list but not yet initialized the dirty ring
> +     * structures.  If so, skip it.
> +     */
> +    if (!cpu->created) {
> +        return 0;
> +    }
> +
>      assert(dirty_gfns && ring_size);
>      trace_kvm_dirty_ring_reap_vcpu(cpu->cpu_index);
>  
> -- 
> 2.37.3
> 

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: dirty-ring: Fix race with vcpu creation
  2023-02-05 23:00 ` Peter Xu
@ 2023-02-21 22:39   ` Gavin Shan
  0 siblings, 0 replies; 4+ messages in thread
From: Gavin Shan @ 2023-02-21 22:39 UTC (permalink / raw)
  To: Peter Xu, qemu-devel; +Cc: Paolo Bonzini, Xiaohui Li

On 2/6/23 10:00 AM, Peter Xu wrote:
> On Tue, Sep 27, 2022 at 11:46:53AM -0400, Peter Xu wrote:
>> It's possible that we want to reap a dirty ring on a vcpu that is during
>> creation, because the vcpu is put onto list (CPU_FOREACH visible) before
>> initialization of the structures.  In this case:
>>
>> qemu_init_vcpu
>>      x86_cpu_realizefn
>>          cpu_exec_realizefn
>>              cpu_list_add      <---- can be probed by CPU_FOREACH
>>          qemu_init_vcpu
>>              cpus_accel->create_vcpu_thread(cpu);
>>                  kvm_init_vcpu
>>                      map kvm_dirty_gfns  <--- kvm_dirty_gfns valid
>>
>> Don't try to reap dirty ring on vcpus during creation or it'll crash.
>>
>> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2124756
>> Reported-by: Xiaohui Li <xiaohli@redhat.com>
>> Signed-off-by: Peter Xu <peterx@redhat.com>
>> ---
>>   accel/kvm/kvm-all.c | 9 +++++++++
>>   1 file changed, 9 insertions(+)
>>
>> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
>> index 5acab1767f..df5fabd3a8 100644
>> --- a/accel/kvm/kvm-all.c
>> +++ b/accel/kvm/kvm-all.c
>> @@ -757,6 +757,15 @@ static uint32_t kvm_dirty_ring_reap_one(KVMState *s, CPUState *cpu)
>>       uint32_t ring_size = s->kvm_dirty_ring_size;
>>       uint32_t count = 0, fetch = cpu->kvm_fetch_index;
>>   
>> +    /*
>> +     * It's possible that we race with vcpu creation code where the vcpu is
>> +     * put onto the vcpus list but not yet initialized the dirty ring
>> +     * structures.  If so, skip it.
>> +     */
>> +    if (!cpu->created) {
>> +        return 0;
>> +    }
>> +
>>       assert(dirty_gfns && ring_size);
>>       trace_kvm_dirty_ring_reap_vcpu(cpu->cpu_index);
>>   

Reviewed-by: Gavin Shan <gshan@redhat.com>



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-02-21 22:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-27 15:46 [PATCH] kvm: dirty-ring: Fix race with vcpu creation Peter Xu
2022-10-14 16:33 ` Peter Xu
2023-02-05 23:00 ` Peter Xu
2023-02-21 22:39   ` Gavin Shan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).