[PATCH v3 2/2] soc: fsl: dpio: Use scope-based resource management in dpaa2_io_create()

Markus Elfring <[email protected]> Wed, 29 Jul 2026 13:44:06 +0200
Newsgroups org.kernel.vger.kernel-janitors,org.infradead.lists.linux-arm-kernel,org.kernel.vger.linux-kernel,org.ozlabs.lists.linuxppc-dev
Message-ID <[email protected]>
From: Markus Elfring <[email protected]>
Date: Wed, 29 Jul 2026 12:46:56 +0200

Scope-based resource management became supported for some
programming interfaces by contributions of Peter Zijlstra on 2023-05-26.
See also the commit 54da6a0924311c7cf5015533991e44fb8eb12773 ("locking:
Introduce __cleanup() based infrastructure").

* Move some source code from the implementation of the function
  =E2=80=9Cdpaa2_io_create=E2=80=9D to a new function.

* Use the attribute =E2=80=9C__free(kfree)=E2=80=9D for the local variable=
 =E2=80=9Cobj=E2=80=9D.

* Omit two kfree() calls accordingly.

Signed-off-by: Markus Elfring <[email protected]>
=2D--
 drivers/soc/fsl/dpio/dpio-service.c | 46 +++++++++++++++--------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/drivers/soc/fsl/dpio/dpio-service.c b/drivers/soc/fsl/dpio/dp=
io-service.c
index b621755d8988..4ff99928e9c6 100644
=2D-- a/drivers/soc/fsl/dpio/dpio-service.c
+++ b/drivers/soc/fsl/dpio/dpio-service.c
@@ -121,31 +121,15 @@ static void dpaa2_io_dim_work(struct work_struct *w)
 	dim->state =3D DIM_START_MEASURE;
 }
=20
-/**
- * dpaa2_io_create() - create a dpaa2_io object.
- * @desc: the dpaa2_io descriptor
- * @dev: the actual DPIO device
- *
- * Activates a "struct dpaa2_io" corresponding to the given config of an =
actual
- * DPIO object.
- *
- * Return a valid dpaa2_io object for success, or NULL for failure.
- */
-struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc,
-				 struct device *dev)
+static inline
+struct dpaa2_io *dpaa2_io_create_impl(const struct dpaa2_io_desc *desc, s=
truct device *dev)
 {
-	struct dpaa2_io *obj =3D kmalloc_obj(*obj);
 	u32 qman_256_cycles_per_ns;
+	struct dpaa2_io *obj __free(kfree) =3D kmalloc_obj(*obj);
=20
 	if (!obj)
 		return NULL;
=20
-	/* check if CPU is out of range (-1 means any cpu) */
-	if (desc->cpu !=3D DPAA2_IO_ANY_CPU && desc->cpu >=3D num_possible_cpus(=
)) {
-		kfree(obj);
-		return NULL;
-	}
-
 	obj->dpio_desc =3D *desc;
 	obj->swp_desc.cena_bar =3D obj->dpio_desc.regs_cena;
 	obj->swp_desc.cinh_bar =3D obj->dpio_desc.regs_cinh;
@@ -160,10 +144,8 @@ struct dpaa2_io *dpaa2_io_create(const struct dpaa2_i=
o_desc *desc,
 	obj->swp_desc.qman_256_cycles_per_ns =3D qman_256_cycles_per_ns;
 	obj->swp =3D qbman_swp_init(&obj->swp_desc);
=20
-	if (!obj->swp) {
-		kfree(obj);
+	if (!obj->swp)
 		return NULL;
-	}
=20
 	INIT_LIST_HEAD(&obj->node);
 	spin_lock_init(&obj->lock_mgmt_cmd);
@@ -195,6 +177,26 @@ struct dpaa2_io *dpaa2_io_create(const struct dpaa2_i=
o_desc *desc,
 	return obj;
 }
=20
+/**
+ * dpaa2_io_create() - create a dpaa2_io object.
+ * @desc: the dpaa2_io descriptor
+ * @dev: the actual DPIO device
+ *
+ * Activates a "struct dpaa2_io" corresponding to the given config of an =
actual
+ * DPIO object.
+ *
+ * Return a valid dpaa2_io object for success, or NULL for failure.
+ */
+struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc,
+				 struct device *dev)
+{
+	/* check if CPU is out of range (-1 means any cpu) */
+	if (desc->cpu !=3D DPAA2_IO_ANY_CPU && desc->cpu >=3D num_possible_cpus(=
))
+		return NULL;
+
+	return dpaa2_io_create_impl(desc, dev);
+}
+
 /**
  * dpaa2_io_down() - release the dpaa2_io object.
  * @d: the dpaa2_io object to be released.
=2D-=20
2.55.0