linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Nicolin Chen <nicolinc@nvidia.com>
To: <jgg@nvidia.com>, <kevin.tian@intel.com>
Cc: <will@kernel.org>, <robin.murphy@arm.com>, <joro@8bytes.org>,
	<praan@google.com>, <yi.l.liu@intel.com>, <peterz@infradead.org>,
	<jsnitsel@redhat.com>, <linux-arm-kernel@lists.infradead.org>,
	<iommu@lists.linux.dev>, <linux-kernel@vger.kernel.org>,
	<patches@lists.linux.dev>, <baolu.lu@linux.intel.com>
Subject: [PATCH v2 13/14] iommufd: Introduce iommufd_object_alloc_ucmd helper
Date: Fri, 13 Jun 2025 23:35:25 -0700	[thread overview]
Message-ID: <e7206d4227844887cc8dbf0cc7b0242580fafd9d.1749882255.git.nicolinc@nvidia.com> (raw)
In-Reply-To: <cover.1749882255.git.nicolinc@nvidia.com>

An object allocator needs to call either iommufd_object_finalize() upon a
success or iommufd_object_abort_and_destroy() upon an error code.

To reduce duplication, store a new_obj in the struct iommufd_ucmd and call
iommufd_object_finalize/iommufd_object_abort_and_destroy() accordingly in
the main function.

Similar to iommufd_object_alloc() and __iommufd_object_alloc(), add a pair
of helpers: __iommufd_object_alloc_ucmd() and iommufd_object_alloc_ucmd().

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 drivers/iommu/iommufd/iommufd_private.h | 26 +++++++++++++++++++++++++
 drivers/iommu/iommufd/main.c            | 25 ++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index ec5b499d139c..4f5e8cd99c96 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -135,6 +135,7 @@ struct iommufd_ucmd {
 	void __user *ubuffer;
 	u32 user_size;
 	void *cmd;
+	struct iommufd_object *new_obj;
 };
 
 int iommufd_vfio_ioctl(struct iommufd_ctx *ictx, unsigned int cmd,
@@ -230,6 +231,11 @@ iommufd_object_put_and_try_destroy(struct iommufd_ctx *ictx,
 	iommufd_object_remove(ictx, obj, obj->id, 0);
 }
 
+/*
+ * Callers of these normal object allocators must call iommufd_object_finalize()
+ * to finalize the object, or call iommufd_object_abort_and_destroy() to revert
+ * the allocation.
+ */
 struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 					     size_t size,
 					     enum iommufd_object_type type);
@@ -246,6 +252,26 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 #define iommufd_object_alloc(ictx, ptr, type) \
 	__iommufd_object_alloc(ictx, ptr, type, obj)
 
+/*
+ * Callers of these _ucmd allocators should not call iommufd_object_finalize()
+ * or iommufd_object_abort_and_destroy(), as the core automatically does that.
+ */
+struct iommufd_object *
+_iommufd_object_alloc_ucmd(struct iommufd_ucmd *ucmd, size_t size,
+			   enum iommufd_object_type type);
+
+#define __iommufd_object_alloc_ucmd(ucmd, ptr, type, obj)                      \
+	container_of(_iommufd_object_alloc_ucmd(                               \
+			     ucmd,                                             \
+			     sizeof(*(ptr)) + BUILD_BUG_ON_ZERO(               \
+						      offsetof(typeof(*(ptr)), \
+							       obj) != 0),     \
+			     type),                                            \
+		     typeof(*(ptr)), obj)
+
+#define iommufd_object_alloc_ucmd(ucmd, ptr, type) \
+	__iommufd_object_alloc_ucmd(ucmd, ptr, type, obj)
+
 /*
  * The IO Address Space (IOAS) pagetable is a virtual page table backed by the
  * io_pagetable object. It is a user controlled mapping of IOVA -> PFNs. The
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 85ad2853da0b..778694d7c207 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -61,6 +61,24 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 	return ERR_PTR(rc);
 }
 
+struct iommufd_object *_iommufd_object_alloc_ucmd(struct iommufd_ucmd *ucmd,
+						  size_t size,
+						  enum iommufd_object_type type)
+{
+	struct iommufd_object *new_obj;
+
+	/* Something is coded wrong if this is hit */
+	if (WARN_ON(ucmd->new_obj))
+		return ERR_PTR(-EBUSY);
+
+	new_obj = _iommufd_object_alloc(ucmd->ictx, size, type);
+	if (IS_ERR(new_obj))
+		return new_obj;
+
+	ucmd->new_obj = new_obj;
+	return new_obj;
+}
+
 /*
  * Allow concurrent access to the object.
  *
@@ -448,6 +466,13 @@ static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
 	if (ret)
 		return ret;
 	ret = op->execute(&ucmd);
+
+	if (ucmd.new_obj) {
+		if (ret)
+			iommufd_object_abort_and_destroy(ictx, ucmd.new_obj);
+		else
+			iommufd_object_finalize(ictx, ucmd.new_obj);
+	}
 	return ret;
 }
 
-- 
2.43.0



  parent reply	other threads:[~2025-06-14  7:10 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-14  6:35 [PATCH v2 00/14] iommufd: Prepare for IOMMUFD_OBJ_HW_QUEUE Nicolin Chen
2025-06-14  6:35 ` [PATCH v2 01/14] iommufd: Apply obvious cosmetic fixes Nicolin Chen
2025-06-16  3:23   ` Baolu Lu
2025-06-14  6:35 ` [PATCH v2 02/14] iommufd: Drop unused ictx in struct iommufd_vdevice Nicolin Chen
2025-06-16  3:23   ` Baolu Lu
2025-06-14  6:35 ` [PATCH v2 03/14] iommufd: Use enum iommu_viommu_type for type in struct iommufd_viommu Nicolin Chen
2025-06-16  3:24   ` Baolu Lu
2025-06-16 21:45   ` Pranjal Shrivastava
2025-06-14  6:35 ` [PATCH v2 04/14] iommufd: Use enum iommu_veventq_type for type in struct iommufd_veventq Nicolin Chen
2025-06-16  3:24   ` Baolu Lu
2025-06-16 21:47   ` Pranjal Shrivastava
2025-06-14  6:35 ` [PATCH v2 05/14] iommufd: Return EOPNOTSUPP for failures due to driver bugs Nicolin Chen
2025-06-16  3:25   ` Baolu Lu
2025-06-16 12:28   ` Jason Gunthorpe
2025-06-16 21:49   ` Pranjal Shrivastava
2025-06-19  5:40   ` Tian, Kevin
2025-06-14  6:35 ` [PATCH v2 06/14] iommu: Introduce get_viommu_size and viommu_init ops Nicolin Chen
2025-06-16  3:25   ` Baolu Lu
2025-06-16 12:43   ` Jason Gunthorpe
2025-06-16 22:10   ` Pranjal Shrivastava
2025-06-19  5:42   ` Tian, Kevin
2025-06-14  6:35 ` [PATCH v2 07/14] iommufd/viommu: Support " Nicolin Chen
2025-06-16  3:26   ` Baolu Lu
2025-06-16 12:45   ` Jason Gunthorpe
2025-06-19  5:45   ` Tian, Kevin
2025-06-14  6:35 ` [PATCH v2 08/14] iommufd/selftest: Drop parent domain from mock_iommu_domain_nested Nicolin Chen
2025-06-16 12:46   ` Jason Gunthorpe
2025-06-19  5:46   ` Tian, Kevin
2025-06-14  6:35 ` [PATCH v2 09/14] iommufd/selftest: Replace mock_viommu_alloc with mock_viommu_init Nicolin Chen
2025-06-16 12:46   ` Jason Gunthorpe
2025-06-14  6:35 ` [PATCH v2 10/14] iommu/arm-smmu-v3: Replace arm_vsmmu_alloc with arm_vsmmu_init Nicolin Chen
2025-06-16 10:03   ` Will Deacon
2025-06-16 12:49   ` Jason Gunthorpe
2025-06-16 22:43   ` Pranjal Shrivastava
2025-06-17  2:15     ` Nicolin Chen
2025-06-17  5:43       ` Pranjal Shrivastava
2025-06-14  6:35 ` [PATCH v2 11/14] iommu: Deprecate viommu_alloc op Nicolin Chen
2025-06-16  3:26   ` Baolu Lu
2025-06-16 12:49   ` Jason Gunthorpe
2025-06-16 22:46   ` Pranjal Shrivastava
2025-06-14  6:35 ` [PATCH v2 12/14] iommufd: Move _iommufd_object_alloc out of driver.c Nicolin Chen
2025-06-16  3:26   ` Baolu Lu
2025-06-14  6:35 ` Nicolin Chen [this message]
2025-06-16  3:27   ` [PATCH v2 13/14] iommufd: Introduce iommufd_object_alloc_ucmd helper Baolu Lu
2025-06-16 22:52   ` [PATCH v2 13/14] iommufd: Introduce iommufd_object_alloc_ucmd helpery Pranjal Shrivastava
2025-07-09  5:31   ` [PATCH v2 13/14] iommufd: Introduce iommufd_object_alloc_ucmd helper Xu Yilun
2025-07-10  5:32     ` Tian, Kevin
2025-07-10 18:21       ` Nicolin Chen
2025-06-14  6:35 ` [PATCH v2 14/14] iommufd: Apply the new " Nicolin Chen
2025-06-16  3:27   ` Baolu Lu
2025-06-19  5:49   ` Tian, Kevin
2025-06-19 18:46 ` [PATCH v2 00/14] iommufd: Prepare for IOMMUFD_OBJ_HW_QUEUE 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=e7206d4227844887cc8dbf0cc7b0242580fafd9d.1749882255.git.nicolinc@nvidia.com \
    --to=nicolinc@nvidia.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=jsnitsel@redhat.com \
    --cc=kevin.tian@intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=peterz@infradead.org \
    --cc=praan@google.com \
    --cc=robin.murphy@arm.com \
    --cc=will@kernel.org \
    --cc=yi.l.liu@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).