All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev, iommu@lists.linux.dev,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	Alexey Kardashevskiy <aik@amd.com>,
	Bjorn Helgaas <helgaas@kernel.org>,
	Joerg Roedel <joro@8bytes.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Kevin Tian <kevin.tian@intel.com>,
	Nicolin Chen <nicolinc@nvidia.com>,
	Samuel Ortiz <sameo@rivosinc.com>,
	Steven Price <steven.price@arm.com>,
	Suzuki K Poulose <Suzuki.Poulose@arm.com>,
	Will Deacon <will@kernel.org>,
	Xu Yilun <yilun.xu@linux.intel.com>,
	Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [RFC PATCH v6 07/11] coco: tsm: Expose active-user lifetime references
Date: Thu, 17 Sep 2026 19:31:55 +0530	[thread overview]
Message-ID: <20260917140159.1163281-8-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20260917140159.1163281-1-aneesh.kumar@kernel.org>

Add separate active-user references for consumers that require both the
TSM device and its PCI/TSM resources.

Initialize the users count to one when allocating the TSM device. This
initial reference represents the registration and keeps the PCI/TSM
resources registered until tsm_unregister() drops it.

tsm_get() takes both an active-user reference and a device reference.
tsm_put() drops the active-user reference first, allowing the last
active user to tear down PCI/TSM resources while struct tsm_dev and the
driver operations remain valid, and then drops the device reference.

tsm_unregister() drops the initial registration reference and
unregisters the device. Existing active users retain the PCI/TSM
resources, while their device references keep struct tsm_dev alive. The
final active-user reference tears down the PCI/TSM resources,
independently of the final device reference releasing struct tsm_dev.

Restructure PCI/TSM registration error handling so tsm_register()
removes the device and lets its scoped device reference perform the
final release.

Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
 drivers/virt/coco/tsm-core.c | 55 ++++++++++++++++++++++++++++++------
 include/linux/tsm.h          | 13 +++++++++
 2 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
index 0843b77c6549..90d304d55b59 100644
--- a/drivers/virt/coco/tsm-core.c
+++ b/drivers/virt/coco/tsm-core.c
@@ -57,6 +57,39 @@ static const struct class tsm_class = {
 	.dev_groups	= tsm_pci_groups,
 };
 static DEFINE_IDA(tsm_ida);
+static void tsm_put_active(struct tsm_dev *tsm_dev)
+{
+	if (!refcount_dec_and_test(&tsm_dev->users))
+		return;
+	if (tsm_dev->pci_ops)
+		pci_tsm_unregister(tsm_dev);
+}
+
+/**
+ * tsm_get() - Take an active reference to a TSM
+ * @tsm_dev: registered TSM or TSM already held by an active reference
+ *
+ * Keeps both the device and its PCI/TSM resources alive.
+ */
+void tsm_get(struct tsm_dev *tsm_dev)
+{
+	get_device(&tsm_dev->dev);
+	refcount_inc(&tsm_dev->users);
+}
+EXPORT_SYMBOL_GPL(tsm_get);
+
+/**
+ * tsm_put() - Release an active TSM reference
+ * @tsm_dev: TSM acquired with tsm_get()
+ *
+ * The last active reference tears down PCI/TSM resources after unregister.
+ */
+void tsm_put(struct tsm_dev *tsm_dev)
+{
+	tsm_put_active(tsm_dev);
+	put_device(&tsm_dev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_put);
 
 static int match_id(struct device *dev, const void *data)
 {
@@ -90,6 +123,7 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
 		return ERR_PTR(id);
 
 	tsm_dev->id = id;
+	refcount_set(&tsm_dev->users, 1);
 	dev = &tsm_dev->dev;
 	dev->parent = parent;
 	dev->class = &tsm_class;
@@ -98,27 +132,26 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
 	return no_free_ptr(tsm_dev);
 }
 
-static struct tsm_dev *tsm_register_pci_or_reset(struct tsm_dev *tsm_dev,
-						 struct pci_tsm_ops *pci_ops)
+static int tsm_register_pci(struct tsm_dev *tsm_dev, struct pci_tsm_ops *pci_ops)
 {
 	int rc;
 
 	if (!pci_ops)
-		return tsm_dev;
+		return 0;
 
 	tsm_dev->pci_ops = pci_ops;
 	rc = pci_tsm_register(tsm_dev);
 	if (rc) {
+		tsm_dev->pci_ops = NULL;
 		dev_err(tsm_dev->dev.parent,
 			"PCI/TSM registration failure: %d\n", rc);
-		device_unregister(&tsm_dev->dev);
-		return ERR_PTR(rc);
+		return rc;
 	}
 	sysfs_update_group(&tsm_dev->dev.kobj, &tsm_pci_group);
 
 	/* Notify TSM userspace that PCI/TSM operations are now possible */
 	kobject_uevent(&tsm_dev->dev.kobj, KOBJ_CHANGE);
-	return tsm_dev;
+	return 0;
 }
 
 struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
@@ -139,14 +172,18 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
 	if (rc)
 		return ERR_PTR(rc);
 
-	return tsm_register_pci_or_reset(no_free_ptr(tsm_dev), pci_ops);
+	rc = tsm_register_pci(tsm_dev, pci_ops);
+	if (rc) {
+		device_del(dev);
+		return ERR_PTR(rc);
+	}
+	return no_free_ptr(tsm_dev);
 }
 EXPORT_SYMBOL_GPL(tsm_register);
 
 void tsm_unregister(struct tsm_dev *tsm_dev)
 {
-	if (tsm_dev->pci_ops)
-		pci_tsm_unregister(tsm_dev);
+	tsm_put_active(tsm_dev);
 	device_unregister(&tsm_dev->dev);
 }
 EXPORT_SYMBOL_GPL(tsm_unregister);
diff --git a/include/linux/tsm.h b/include/linux/tsm.h
index 7f72a154b6b2..f38d6fcf9cc9 100644
--- a/include/linux/tsm.h
+++ b/include/linux/tsm.h
@@ -2,6 +2,7 @@
 #ifndef __TSM_H
 #define __TSM_H
 
+#include <linux/refcount.h>
 #include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/uuid.h>
@@ -109,8 +110,18 @@ struct tsm_report_ops {
 };
 
 struct pci_tsm_ops;
+
+/**
+ * struct tsm_dev - TEE Security Manager device
+ * @dev: device-model representation of the TSM
+ * @users: registration reference plus active references that retain the
+ *	PCI/TSM resources; reaching zero tears down those resources
+ * @id: instance identifier
+ * @pci_ops: PCI/TSM operations, or %NULL when PCI/TSM is unsupported
+ */
 struct tsm_dev {
 	struct device dev;
+	refcount_t users;
 	int id;
 	const struct pci_tsm_ops *pci_ops;
 };
@@ -122,6 +133,8 @@ int tsm_report_register(const struct tsm_report_ops *ops, void *priv);
 int tsm_report_unregister(const struct tsm_report_ops *ops);
 struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *ops);
 void tsm_unregister(struct tsm_dev *tsm_dev);
+void tsm_get(struct tsm_dev *tsm_dev);
+void tsm_put(struct tsm_dev *tsm_dev);
 struct tsm_dev *find_tsm_dev(int id);
 struct pci_ide;
 int tsm_ide_stream_register(struct pci_ide *ide);
-- 
2.43.0


  parent reply	other threads:[~2026-09-17 14:03 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17 14:01 [RFC PATCH v6 00/11] iommufd: Infrastructure for vIOMMU creation for confidential guests and guest TSM requests Aneesh Kumar K.V (Arm)
2026-09-17 14:01 ` [RFC PATCH v6 01/11] vfio: cache KVM VM file references instead of raw struct kvm pointers Aneesh Kumar K.V (Arm)
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-30  7:19     ` Aneesh Kumar K.V
2026-09-17 14:01 ` [RFC PATCH v6 02/11] vfio: cdev: Reject duplicate bind before updating KVM file Aneesh Kumar K.V (Arm)
2026-09-24  7:53   ` Tian, Kevin
2026-09-25  5:49     ` Aneesh Kumar K.V
2026-09-17 14:01 ` [RFC PATCH v6 03/11] iommufd/device: Associate KVM file pointer with iommufd_device Aneesh Kumar K.V (Arm)
2026-09-17 14:01 ` [RFC PATCH v6 04/11] iommufd/viommu: Keep a reference to the KVM file Aneesh Kumar K.V (Arm)
2026-09-30 13:28   ` Vasant Hegde
2026-10-02  5:26     ` Aneesh Kumar K.V
2026-09-17 14:01 ` [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent Aneesh Kumar K.V (Arm)
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-25  5:48     ` Aneesh Kumar K.V
2026-09-25 12:23       ` Jason Gunthorpe
2026-09-28 10:36         ` Aneesh Kumar K.V
2026-09-28 12:11           ` Jason Gunthorpe
2026-09-28 15:39             ` Aneesh Kumar K.V
2026-09-28 16:17               ` Jason Gunthorpe
2026-09-28 18:08                 ` Jacob Pan
2026-09-28 18:20                   ` Jason Gunthorpe
2026-09-28 22:24                     ` Jacob Pan
2026-09-28 23:03                       ` Jason Gunthorpe
2026-09-29  5:55                         ` Jacob Pan
2026-09-29 12:30                           ` Jason Gunthorpe
2026-09-29 23:15                             ` Jacob Pan
2026-09-29 23:30                               ` Jason Gunthorpe
2026-09-17 14:01 ` [RFC PATCH v6 06/11] iommu: Add a helper to query vIOMMU hardware parameters Aneesh Kumar K.V (Arm)
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-25  5:59     ` Aneesh Kumar K.V
2026-09-25 12:29       ` Jason Gunthorpe
2026-09-17 14:01 ` Aneesh Kumar K.V (Arm) [this message]
2026-09-17 14:01 ` [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support Aneesh Kumar K.V (Arm)
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-25  6:08     ` Aneesh Kumar K.V
2026-09-25 12:39       ` Jason Gunthorpe
2026-09-28  3:41         ` Tian, Kevin
2026-09-29  6:14           ` Aneesh Kumar K.V
2026-09-29 12:17             ` Jason Gunthorpe
2026-09-29 12:45               ` Aneesh Kumar K.V
2026-09-29 13:06                 ` Jason Gunthorpe
2026-09-29 15:58                   ` Aneesh Kumar K.V
2026-09-29 19:10                     ` Jason Gunthorpe
2026-09-28 10:51         ` Aneesh Kumar K.V
2026-09-17 14:01 ` [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl Aneesh Kumar K.V (Arm)
2026-09-18 13:09   ` Alexey Kardashevskiy
2026-09-18 13:13     ` Jason Gunthorpe
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-30  8:03     ` Aneesh Kumar K.V
2026-09-30 13:26       ` Vasant Hegde
2026-09-30 14:03         ` Jason Gunthorpe
2026-09-30 14:08       ` Jason Gunthorpe
2026-09-17 14:01 ` [RFC PATCH v6 10/11] PCI/TSM: Remove the legacy guest request interface Aneesh Kumar K.V (Arm)
2026-09-17 14:01 ` [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers Aneesh Kumar K.V (Arm)
2026-09-24  8:17   ` Tian, Kevin
2026-09-24 19:41   ` Jason Gunthorpe
2026-09-25  8:15     ` Aneesh Kumar K.V
2026-09-28 18:47   ` Sonang Patel
2026-09-28 23:08     ` Jason Gunthorpe
2026-10-02  6:14       ` Aneesh Kumar K.V
2026-10-02 13:06         ` Jason Gunthorpe
2026-09-17 14:17 ` [RFC PATCH v6 00/11] iommufd: Infrastructure for vIOMMU creation for confidential guests and guest TSM requests Aneesh Kumar K.V
2026-09-24  7:48 ` Tian, Kevin
2026-09-24 19:20   ` Jason Gunthorpe
2026-09-28  3:35     ` Tian, Kevin
2026-09-28 13:08       ` Jason Gunthorpe
2026-09-25  8:29   ` Aneesh Kumar K.V
2026-09-28  3:41     ` Tian, Kevin
2026-09-28  3:55       ` Tian, Kevin
2026-09-24 19:09 ` Jason Gunthorpe
2026-09-25  6:46   ` Aneesh Kumar K.V
2026-09-25 12:45     ` Jason Gunthorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260917140159.1163281-8-aneesh.kumar@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=Suzuki.Poulose@arm.com \
    --cc=aik@amd.com \
    --cc=helgaas@kernel.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jic23@kernel.org \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=pbonzini@redhat.com \
    --cc=sameo@rivosinc.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=steven.price@arm.com \
    --cc=will@kernel.org \
    --cc=yilun.xu@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.