From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754732AbdLTI7a (ORCPT ); Wed, 20 Dec 2017 03:59:30 -0500 Received: from mga14.intel.com ([192.55.52.115]:13147 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753269AbdLTI73 (ORCPT ); Wed, 20 Dec 2017 03:59:29 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,431,1508828400"; d="scan'208";a="17547517" Subject: Re: [PATCH] KVM/Eventfd: Avoid crash when assign and deassign same eventfd in parallel. To: Paolo Bonzini , David Hildenbrand References: <1513554007-12302-1-git-send-email-tianyu.lan@intel.com> Cc: rkrcmar@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dvyukov@google.com, kernellwp@gmail.com From: Lan Tianyu Message-ID: <90056b6f-cb82-4828-f7c2-f6fde41e7087@intel.com> Date: Wed, 20 Dec 2017 16:48:59 +0800 User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2017年12月19日 18:41, Paolo Bonzini wrote: > On 19/12/2017 07:35, Lan Tianyu wrote: >> On 2017年12月18日 16:50, 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); >>> >>> >> >> Hi Paolo: >> This patch still can't prevent from freeing struct irqfd in >> irq_shutdown() during assign irqfd. Once irqfd is added to >> kvm->irqfds.items list, deassign path can get irqfd and free it. > > You're right, that also has to be protected by SRCU. So a new bool is > needed (probably "active" more than "initialized") in order to replace > list_del_init with list_del_rcu. > Adding new flag is sufficient to fix the crash issue. All list operations for kvm->irqfds.items list are already under protection kvm->irqfds.lock. Do you mean we should use list_add/del_rcu for kvm->irqfds.items? -- Best regards Tianyu Lan