From patchwork Sat May 13 13:28:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 13240230 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC8E8C77B7D for ; Sat, 13 May 2023 13:29:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239144AbjEMN3m (ORCPT ); Sat, 13 May 2023 09:29:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239160AbjEMN3j (ORCPT ); Sat, 13 May 2023 09:29:39 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9397C40DA; Sat, 13 May 2023 06:29:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683984558; x=1715520558; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TWV894DTZrYaN7h4aj49YReKCmOsfie/espg2ZPGgRk=; b=T40ULdWK1bHD6E8pGoqgSu2FO2ZNrbWb+2hYPByDV8Q3nH6zR4NLRvYg coOrHa3ytc9VGsBbz4lNbaoxQnbXHPA/VRwHpC/eJr2UuUTSBIhEiY4/f +cyUkXnKjl8EMoN3sWzUUx0T+dwimGsqbcxr3q8v8Wkh3gaFYg91ir1xP KzTduo3lfSGlpjJXNrrCj3TgHucgAGWa37Mz3Lgk5DyteEY79K9/p/tLa mW/F00daQJodz/3M95fBaUQ+JxamkzdvxBWmct3AW4m1b0y9xkUcQNNF/ zHRZIJqAzrFJnVjf+GXAPk4lGIAaglRaXXQSeqRJmKV8WnePsOwqmtcYH A==; X-IronPort-AV: E=McAfee;i="6600,9927,10708"; a="354100807" X-IronPort-AV: E=Sophos;i="5.99,272,1677571200"; d="scan'208";a="354100807" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 May 2023 06:28:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10708"; a="703459516" X-IronPort-AV: E=Sophos;i="5.99,272,1677571200"; d="scan'208";a="703459516" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga007.fm.intel.com with ESMTP; 13 May 2023 06:28:53 -0700 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: joro@8bytes.org, robin.murphy@arm.com, cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-s390@vger.kernel.org, xudong.hao@intel.com, yan.y.zhao@intel.com, terrence.xu@intel.com, yanting.jiang@intel.com, zhenzhong.duan@intel.com, clegoate@redhat.com Subject: [PATCH v11 17/23] vfio: Move vfio_device_group_unregister() to be the first operation in unregister Date: Sat, 13 May 2023 06:28:21 -0700 Message-Id: <20230513132827.39066-18-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230513132827.39066-1-yi.l.liu@intel.com> References: <20230513132827.39066-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This can avoid endless vfio_device refcount increasement by userspace, which would keep blocking the vfio_unregister_group_dev(). Tested-by: Yanting Jiang Tested-by: Shameer Kolothum Signed-off-by: Yi Liu --- drivers/vfio/vfio_main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 89720b73fa30..541bfd9ce004 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -333,6 +333,12 @@ void vfio_unregister_group_dev(struct vfio_device *device) bool interrupted = false; long rc; + /* + * Prevent new device opened by userspace via the + * VFIO_GROUP_GET_DEVICE_FD in the group path. + */ + vfio_device_group_unregister(device); + vfio_device_put_registration(device); rc = try_wait_for_completion(&device->comp); while (rc <= 0) { @@ -356,8 +362,6 @@ void vfio_unregister_group_dev(struct vfio_device *device) } } - vfio_device_group_unregister(device); - /* Balances device_add in register path */ device_del(&device->device);