From patchwork Thu Nov 10 01:40:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony DeRossi X-Patchwork-Id: 13038206 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 07B73C433FE for ; Thu, 10 Nov 2022 01:41:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232312AbiKJBlC (ORCPT ); Wed, 9 Nov 2022 20:41:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232356AbiKJBlB (ORCPT ); Wed, 9 Nov 2022 20:41:01 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97BFB275C2 for ; Wed, 9 Nov 2022 17:41:00 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id k15so470136pfg.2 for ; Wed, 09 Nov 2022 17:41:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o+09jyLU6lDhuNs5pMZSMI1gUeLpq4TEKRbF8Z7vfAE=; b=quGu/dIyNoNYfoUaLOnhPpC6Drh6MzlOQVIRdx+/tN9n66ne2Y2jhdye0zFUICK7Os L+ggQFPVgGPzF0xL53MmKLhsT24S5NwvyPVRXeu3zeQdUsNQ+T5FVrid56SVQRqej6JU ugxRe8Cpp30PV0h7NJfND2HeYo/UhyY+TLVyjzeCoHi3+hzOzg2O/qa7yf8PoAAYFUMj /QSiFaYXnnf5GaoCxd3K9IY4Rn7eoWBCbTzQHkh+xqnoQIbQHqcLnq2zt0qBpZiQWX+s 5ZLdDiQg35w0yltAiV3gitf/aYAunI0+QZKrfCmD3w5jYbH0l/Lk4Q+SEnWrBMZ6qHTr 1UlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o+09jyLU6lDhuNs5pMZSMI1gUeLpq4TEKRbF8Z7vfAE=; b=j7njg8mlvbDDRYVfvYGWbYmX7wxSY+IdBSBnQlAg1gVkQun+/05/1Q56f0drF+1Nje YHKXca6e7wxZBSqRhpmIH1gMiabeh2u5BCUn/9QeY0Dp0b4ZIdzKQQArEDrTU+xyjnKS 5yomDwmRzwz+QckoT4Qv04hANV+fynevrmgz4rqhEdNpfbA5NQ4XJ11q2cFtuzDm3oW7 7U7Rp7dr3WFszyUBt3KZgZNJ8BdU9hh/N3qF8ujOp0hmEjXQEtKiPhAAfzGLOQX6k5Wn zu1Td9rqLsRT4e+8tQVHOGU41uuI4wZ4ZkN45LkvVUFBLNjaNbFtMvauGyd07P/o5F95 T80w== X-Gm-Message-State: ACrzQf0d2WMu9jIP0nJYQvgdF3xXhBjCZ0IpcKQOrcmr5LgvjWAvU9cg EbDqgE4l3xRjjHcb0XRGnGz7c5M3qkUfnw== X-Google-Smtp-Source: AMsMyM5GBPV0dHnu0qo+eAe5l/VAmWFRds6l8diPV/W5VpWGrDHyAmKIM0csPNEnbAEMrJSaxfCuZg== X-Received: by 2002:aa7:9145:0:b0:556:d001:b830 with SMTP id 5-20020aa79145000000b00556d001b830mr1570995pfi.62.1668044459914; Wed, 09 Nov 2022 17:40:59 -0800 (PST) Received: from crazyhorse.local ([174.127.229.57]) by smtp.googlemail.com with ESMTPSA id pj4-20020a17090b4f4400b00212cf2fe8c3sm3091836pjb.1.2022.11.09.17.40.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 17:40:59 -0800 (PST) From: Anthony DeRossi To: kvm@vger.kernel.org Cc: alex.williamson@redhat.com, cohuck@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, abhsahu@nvidia.com, yishaih@nvidia.com Subject: [PATCH v6 2/3] vfio: Export the device set open count Date: Wed, 9 Nov 2022 17:40:26 -0800 Message-Id: <20221110014027.28780-3-ajderossi@gmail.com> X-Mailer: git-send-email 2.37.4 In-Reply-To: <20221110014027.28780-1-ajderossi@gmail.com> References: <20221110014027.28780-1-ajderossi@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value to determine whether shared resources are in use without tracking them manually or accessing the private open_count in vfio_device. Signed-off-by: Anthony DeRossi Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Yi Liu --- drivers/vfio/vfio_main.c | 13 +++++++++++++ include/linux/vfio.h | 1 + 2 files changed, 14 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 9a4af880e941..6e8804fe0095 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -125,6 +125,19 @@ static void vfio_release_device_set(struct vfio_device *device) xa_unlock(&vfio_device_set_xa); } +unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set) +{ + struct vfio_device *cur; + unsigned int open_count = 0; + + lockdep_assert_held(&dev_set->lock); + + list_for_each_entry(cur, &dev_set->device_list, dev_set_list) + open_count += cur->open_count; + return open_count; +} +EXPORT_SYMBOL_GPL(vfio_device_set_open_count); + /* * Group objects - create, release, get, put, search */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e7cebeb875dd..fdd393f70b19 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -189,6 +189,7 @@ int vfio_register_emulated_iommu_dev(struct vfio_device *device); void vfio_unregister_group_dev(struct vfio_device *device); int vfio_assign_device_set(struct vfio_device *device, void *set_id); +unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set); int vfio_mig_get_next_state(struct vfio_device *device, enum vfio_device_mig_state cur_fsm,