Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751891AbdLYIEB (ORCPT ); Mon, 25 Dec 2017 03:04:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54980 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750814AbdLYID6 (ORCPT ); Mon, 25 Dec 2017 03:03:58 -0500 Date: Mon, 25 Dec 2017 16:03:51 +0800 From: Peter Xu To: Paolo Bonzini Cc: David Hildenbrand , Lan Tianyu , rkrcmar@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dvyukov@google.com, kernellwp@gmail.com Subject: Re: [PATCH] KVM/Eventfd: Avoid crash when assign and deassign same eventfd in parallel. Message-ID: <20171225080351.GN2443@xz-mi> References: <1513554007-12302-1-git-send-email-tianyu.lan@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.1 (2017-09-22) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 25 Dec 2017 08:03:58 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1386 Lines: 41 On Mon, Dec 18, 2017 at 09:50:04AM +0100, Paolo Bonzini wrote: > On 18/12/2017 09:30, David Hildenbrand wrote: > > The ugly thing in kvm_irqfd_assign() is that we access irqfd without > > holding a lock. I think that should rather be fixed than working around > > that issue. (e.g. lock() -> lookup again -> verify still in list -> > > unlock()) > > I wonder if it's even simpler: > > diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c > index f2ac53ab8243..17ed298bd66f 100644 > --- a/virt/kvm/eventfd.c > +++ b/virt/kvm/eventfd.c > @@ -387,7 +387,6 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) > > idx = srcu_read_lock(&kvm->irq_srcu); > irqfd_update(kvm, irqfd); > - srcu_read_unlock(&kvm->irq_srcu, idx); > > list_add_tail(&irqfd->list, &kvm->irqfds.items); > > @@ -420,10 +419,12 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) > irqfd->consumer.token, ret); > } > #endif > + srcu_read_unlock(&kvm->irq_srcu, idx); > > return 0; > > fail: > + /* irq_srcu is *not* held here. */ > if (irqfd->resampler) > irqfd_resampler_shutdown(irqfd); Sorry if I miss anything important, but... should we extend the unlock of kvm->irqfds.lock instead of kvm->irq_srcu here? AFAIU kvm->irq_srcu is protecting accesses to kvm->irq_routing, while kvm->irqfds.lock is the one that really protects kvm->irqfds? Thanks, -- Peter Xu