From patchwork Tue Feb 7 12:37:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 53865 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2825468wrn; Tue, 7 Feb 2023 04:42:01 -0800 (PST) X-Google-Smtp-Source: AK7set+V34xhBNFuBf8umqKCEIHy1710rS2h/kKfMifub67QtZLuvJz97AnLRbDzqRFuIobXDDqF X-Received: by 2002:a50:f615:0:b0:4a8:5f58:7670 with SMTP id c21-20020a50f615000000b004a85f587670mr3402069edn.17.1675773721209; Tue, 07 Feb 2023 04:42:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675773721; cv=none; d=google.com; s=arc-20160816; b=UFEl81YIhSAYOChFiq3HtJNSJq5iY8JYCwh6Fnc+nPw0P66n/yOvzhr6DbCVU3tEFp PYvvoAzxaHfHbd5LNTz780f2IxuMaLhz3FujW1+X8XSbm8kFt5z9PiYSh0ozypAtuhVD 5mPXcBEBY9uss3vPUPURqutCl1F0pNXGtFPbXVnQt0t0i90lmntww+g6mcOdOFJ0z0E3 csDEnhVyz6v1C9qYOhOM5HXwjdIqICNQpVDVpSOkqg5QhoUo176pP3tDmDh0qxV0k1OQ 5r+rQlvXy085iTaohy7SnGmfEVYgPbW5OKFfjfWzBOLMAUzsHVl1D3/Oc4nJ7K6/5j0w m7xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Uyn75jmjozHty97qCvmAsDBkz3xSdLZsTwtZtzMgPTk=; b=hRruD8xHoyrmz86iPYtgjcp108yqnEeZ0BtOdMIHttmPMWfn6qqhYXICnOfZ4j+sQY gCFabDy8Yj8Bo+0AKFM2GLCC+X104UDrc4k143usRk4pNARMnUT0xdsqqbg3IiM4yJxg yjeTsFpWsYkMGiJkHbfMJHPXHHDn7vL3lx8Os4Aii0GyJ+HSGDknsj7SSUQA2rxEeidM FVcLRhhzSN6DU/HvSTKmXOKCokU8vC95zqq+Dmk7a0hopgitwdZZVcJ9yLt3qpbOsqjn nK4/FH2DppdEC7VupZ73yupIBfhZTL2rIJaOP7vMTYntqKnbDnNrILZeO70GczoRNv5d qnpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aY4C1MVB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cn21-20020a0564020cb500b004a2449b0c0esi13411605edb.347.2023.02.07.04.41.37; Tue, 07 Feb 2023 04:42:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aY4C1MVB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231822AbjBGMh1 (ORCPT + 99 others); Tue, 7 Feb 2023 07:37:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231795AbjBGMhX (ORCPT ); Tue, 7 Feb 2023 07:37:23 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F24D31E5EA; Tue, 7 Feb 2023 04:37:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675773441; x=1707309441; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xBpTrUqylWiKPxKvun97ZAjRbNqQZ6n5ryRa2OISY5U=; b=aY4C1MVB9TthPTPwXni1IVx+qumO4wfTi1+6rapVQkTrjEDw4wjQKCFt msDr2Iop/dZkQV3ju6RXWy1/IybpWa/VY9S7cjmYs4ALo5eYhuUMWLM0q 9a+Ud6p7VbTmS82EZ/m8YerG/JWewrw5440iDjXzTIbxnnXiLtnH+IXUO Oi/2PuZKTkOof++fsXkUBAELpPLv6L45O9hFa5SGlzdKhaYt/Cr3+Hz4t 0Hka8P55Ash9MZguJo0RauvYJws/PTjwJyWPBz5Jf5NiXPu89aQVtygQa 3OFL0Nb81tQx4id+ki93hveNYTAxSHG8fAF+2FSO7jCHiCOX2lwRFJ/7I w==; X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="309140920" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="309140920" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 04:37:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="697267994" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="697267994" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by orsmga008.jf.intel.com with ESMTP; 07 Feb 2023 04:37:19 -0800 From: Wei Wang To: pbonzini@redhat.com, seanjc@google.com, mhal@rbox.co Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v2 1/2] KVM: destruct kvm_io_device while unregistering it from kvm_io_bus Date: Tue, 7 Feb 2023 20:37:12 +0800 Message-Id: <20230207123713.3905-2-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230207123713.3905-1-wei.w.wang@intel.com> References: <20230207123713.3905-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757176105825595561?= X-GMAIL-MSGID: =?utf-8?q?1757176105825595561?= Current usage of kvm_io_device requires users to destruct it with an extra call of kvm_iodevice_destructor after the device gets unregistered from kvm_io_bus. This is not necessary and can cause errors if a user forgot to make the extra call. Simplify the usage by combining kvm_iodevice_destructor into kvm_io_bus_unregister_dev. This reduces LOCs a bit for users and can avoid the leakage of destructing the device explicitly. Signed-off-by: Wei Wang Reviewed-by: Sean Christopherson --- include/kvm/iodev.h | 6 ------ virt/kvm/coalesced_mmio.c | 9 ++------- virt/kvm/eventfd.c | 1 - virt/kvm/kvm_main.c | 23 +++++++++++++++-------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/include/kvm/iodev.h b/include/kvm/iodev.h index d75fc4365746..56619e33251e 100644 --- a/include/kvm/iodev.h +++ b/include/kvm/iodev.h @@ -55,10 +55,4 @@ static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu, : -EOPNOTSUPP; } -static inline void kvm_iodevice_destructor(struct kvm_io_device *dev) -{ - if (dev->ops->destructor) - dev->ops->destructor(dev); -} - #endif /* __KVM_IODEV_H__ */ diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c index 5ef88f5a0864..1b90acb6e3fe 100644 --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -186,15 +186,10 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, coalesced_mmio_in_range(dev, zone->addr, zone->size)) { r = kvm_io_bus_unregister_dev(kvm, zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev); - - kvm_iodevice_destructor(&dev->dev); - /* * On failure, unregister destroys all devices on the - * bus _except_ the target device, i.e. coalesced_zones - * has been modified. Bail after destroying the target - * device, there's no need to restart the walk as there - * aren't any zones left. + * bus, including the target device. There's no need + * to restart the walk as there aren't any zones left. */ if (r) break; diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 2a3ed401ce46..1b277afb545b 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -898,7 +898,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, bus = kvm_get_bus(kvm, bus_idx); if (bus) bus->ioeventfd_count--; - ioeventfd_release(p); ret = 0; break; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 71cc63640173..ce1f2c5b7c87 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5275,6 +5275,12 @@ static void hardware_disable_all(void) } #endif /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */ +static void kvm_iodevice_destructor(struct kvm_io_device *dev) +{ + if (dev->ops->destructor) + dev->ops->destructor(dev); +} + static void kvm_io_bus_destroy(struct kvm_io_bus *bus) { int i; @@ -5498,7 +5504,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_io_device *dev) { - int i, j; + int i; struct kvm_io_bus *new_bus, *bus; lockdep_assert_held(&kvm->slots_lock); @@ -5528,18 +5534,19 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, rcu_assign_pointer(kvm->buses[bus_idx], new_bus); synchronize_srcu_expedited(&kvm->srcu); - /* Destroy the old bus _after_ installing the (null) bus. */ + /* + * If NULL bus is installed, destroy the old bus, including all the + * attached devices. Otherwise, destroy the caller's device only. + */ if (!new_bus) { pr_err("kvm: failed to shrink bus, removing it completely\n"); - for (j = 0; j < bus->dev_count; j++) { - if (j == i) - continue; - kvm_iodevice_destructor(bus->range[j].dev); - } + kvm_io_bus_destroy(bus); + return -ENOMEM; } + kvm_iodevice_destructor(dev); kfree(bus); - return new_bus ? 0 : -ENOMEM; + return 0; } struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, From patchwork Tue Feb 7 12:37:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 53864 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2825429wrn; Tue, 7 Feb 2023 04:41:56 -0800 (PST) X-Google-Smtp-Source: AK7set8jyDH9w6x5dVpUWLv4GklTaI4EL3xRYAd6Ih0cbdC034dZjvBpJHSkFZnsQBNJRvplw3xC X-Received: by 2002:a17:902:ec8e:b0:198:f574:1d2c with SMTP id x14-20020a170902ec8e00b00198f5741d2cmr2717197plg.24.1675773716490; Tue, 07 Feb 2023 04:41:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675773716; cv=none; d=google.com; s=arc-20160816; b=CF0iVB9WgdvdvQU8jsRChaSkm381Tr1IxTe+SFqYi1JSmsnKTR0em2PBFqXYMhImZ6 tYZs0JUNQayuNfxrpV4hreh12oQNPmbpt4LRtm3TbRgcR2yPTpZDRjk72sZv5llZMdUp aOIpH10WdLndrZ5TX8WsXk26ray31EANshFWXobOGiC11E4uaJALdS5aehz1JCq++Zr9 9CK17Ku6kNqwtkiqmCElMkFl2XoM+EpJbwOiayVcSjYT7hode04LZZIozCcppHCf9o4G aryCQRHbXJEIp1FC3l0qQkAlHyOtQvm2i5/o6Vk9Voi3NskDIzbKUrttIXAWjZ3Eh5mr g7eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=U05d1FEwEV4kFx0/0h+z779sxBqZgM/TYt1Z/WbUp1A=; b=n4FFxlHno524qh4eJURbbu6UFvwXChbAXTM+ycpLJLM9FQFPTh7zyfKnyhCW7Yqvp3 8bQ3DZGBiRT8gkao2FHXuCLH061sIkGuYR9zoL8xN3loqqj3zYWdhSTzMEDNHh1kAQga StIl/Gkp9xaBnvrdQNQt4JnHUT99DVXRYQvEx00uM0h7fTGGJ48LINZsg6q76ZY0fbPd PO1UncbiTODAFk06djbQMPKrhcnDeZ/OjEytCZJDHyJws8enNI3O9lrF9x1to9jHmNpy 4V+aIIdd3kD8iHySLz4IQibxjptKBT9dc1dk/8wMyRqD6N6SELvMY+k3gRxxhlk/gFwU 9P1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Bg6A8O/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020a170902700700b001978f72aee3si13205907plk.502.2023.02.07.04.41.44; Tue, 07 Feb 2023 04:41:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Bg6A8O/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230132AbjBGMhb (ORCPT + 99 others); Tue, 7 Feb 2023 07:37:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230501AbjBGMhZ (ORCPT ); Tue, 7 Feb 2023 07:37:25 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C78761E2B9; Tue, 7 Feb 2023 04:37:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675773444; x=1707309444; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LiSxcagqNB+5kdb9BVVbcrT1QJr/8pqeefoLK3ITSQA=; b=Bg6A8O/9PBpkWNomYDrP164OIOQjpvRzyp0WemoXr22Fo9b38t2NBSjQ WGkDnVP/Lpl91hxUCxU8KX/Yr0b+ZHMnj9suPYkf27Jw04QCQ3y1XPZhi EUmX6Ae2GQlZRY1U83M+6u6quaj69b8ifNUZdTWShGdr7SRrHlStutATr CS5iQtcMsOvKOwLP0NpkWkycjYkTIwyDT+x4SDvODSyqEwZSrXGy5WCwg BrX1dAo4eZFGwKadKS2Okhzgv7LuuVZ01tIQb6s2zrkiKQvlfJCb+pspA EAVO4yKxga2sCnsL775hQxllTK3WB0l0kryXBXsURqH/nMO3C65pVJBpC g==; X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="309140933" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="309140933" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 04:37:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="697268000" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="697268000" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by orsmga008.jf.intel.com with ESMTP; 07 Feb 2023 04:37:22 -0800 From: Wei Wang To: pbonzini@redhat.com, seanjc@google.com, mhal@rbox.co Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v2 2/2] kvm/eventfd: use list_for_each_entry when deassign ioeventfd Date: Tue, 7 Feb 2023 20:37:13 +0800 Message-Id: <20230207123713.3905-3-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230207123713.3905-1-wei.w.wang@intel.com> References: <20230207123713.3905-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757176100381135385?= X-GMAIL-MSGID: =?utf-8?q?1757176100381135385?= Simpify kvm_deassign_ioeventfd_idx to use list_for_each_entry as the loop just ends at the entry that's founded and deleted. Signed-off-by: Wei Wang Reviewed-by: Sean Christopherson --- virt/kvm/eventfd.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 1b277afb545b..2b56a3c81957 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -868,7 +868,7 @@ static int kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_ioeventfd *args) { - struct _ioeventfd *p, *tmp; + struct _ioeventfd *p; struct eventfd_ctx *eventfd; struct kvm_io_bus *bus; int ret = -ENOENT; @@ -882,8 +882,7 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, mutex_lock(&kvm->slots_lock); - list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) { - + list_for_each_entry(p, &kvm->ioeventfds, list) { if (p->bus_idx != bus_idx || p->eventfd != eventfd || p->addr != args->addr ||