mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: alexandre.belloni@bootlin.com
To: Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Frank Li <Frank.Li@nxp.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Boris Brezillon <bbrezillon@kernel.org>,
	Arnd Bergmann <arnd@arndb.de>
Cc: linux-i3c@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] i3c: master: Give I3C devices their own fwnode reference
Date: Wed,  2 Dec 2026 23:30:13 +0100	[thread overview]
Message-ID: <20261202223014.696672-2-alexandre.belloni@bootlin.com> (raw)
In-Reply-To: <20261202223014.696672-1-alexandre.belloni@bootlin.com>

From: Alexandre Belloni <alexandre.belloni@bootlin.com>

i3c_dev_boardinfo entries take a reference on their fwnode, which is
transferred to the I3C device created from the boardinfo by a plain
pointer assignment in i3c_master_register_new_i3c_devs().  This leaks a
reference for boardinfo entries that never match a device on the bus
(the boardinfo is devm-freed with the master without dropping it), and
if two device descriptors ever match the same boardinfo, both devices
would release the single transferred reference, underflowing the fwnode
refcount.

Let each holder own its own reference: devices take a fresh reference
in i3c_master_register_new_i3c_devs(), balanced by the existing
fwnode_handle_put() in i3c_device_release(), while the boardinfo drops
its own reference through a devm action when it is released.  Devices
are unregistered before the devm resources are released at master
unregistration, but each reference is independent so the order does not
matter.

Fixes: 3a379bbcea0a ("i3c: Add core I3C infrastructure")
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
---
 drivers/i3c/master.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 677d547bc12f..409545a6eda3 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -2242,8 +2242,13 @@ i3c_master_register_new_i3c_devs(struct i3c_master_controller *master)
 			dev_set_name(&desc->dev->dev, "%d-%02x", master->bus.id,
 				     desc->info.static_addr);
 
+		/*
+		 * The device takes its own reference on the fwnode, which
+		 * is dropped by i3c_device_release().
+		 */
 		if (desc->boardinfo)
-			device_set_node(&desc->dev->dev, desc->boardinfo->fwnode);
+			device_set_node(&desc->dev->dev,
+					fwnode_handle_get(desc->boardinfo->fwnode));
 
 		/* If the device has IBI capability, set as wakeup capable */
 		if (master->ibi_wakeup && (desc->info.bcr & I3C_BCR_IBI_REQ_CAP))
@@ -3197,6 +3202,13 @@ i3c_master_add_i2c_boardinfo(struct i3c_master_controller *master,
 	return 0;
 }
 
+static void i3c_master_i3c_boardinfo_fwnode_put(void *data)
+{
+	struct i3c_dev_boardinfo *boardinfo = data;
+
+	fwnode_handle_put(boardinfo->fwnode);
+}
+
 static int
 i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
 			     struct fwnode_handle *fwnode, u32 *reg)
@@ -3205,6 +3217,7 @@ i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
 	struct device *dev = &master->dev;
 	enum i3c_addr_slot_status addrstatus;
 	u32 init_dyn_addr = 0, static_addr_method = 0;
+	int ret;
 
 	boardinfo = devm_kzalloc(dev, sizeof(*boardinfo), GFP_KERNEL);
 	if (!boardinfo)
@@ -3266,7 +3279,13 @@ i3c_master_add_i3c_boardinfo(struct i3c_master_controller *master,
 	}
 
 	boardinfo->init_dyn_addr = init_dyn_addr;
+
 	boardinfo->fwnode = fwnode_handle_get(fwnode);
+	ret = devm_add_action_or_reset(dev, i3c_master_i3c_boardinfo_fwnode_put,
+				       boardinfo);
+	if (ret)
+		return ret;
+
 	list_add_tail(&boardinfo->node, &master->boardinfo.i3c);
 
 	return 0;
-- 
2.56.0


      reply	other threads:[~2026-12-02 22:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 22:30 [PATCH 1/2] i3c: master: Release fwnode reference of I2C boardinfo at unregistration alexandre.belloni
2026-12-02 22:30 ` alexandre.belloni [this message]

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=20261202223014.696672-2-alexandre.belloni@bootlin.com \
    --to=alexandre.belloni@bootlin.com \
    --cc=Frank.Li@nxp.com \
    --cc=arnd@arndb.de \
    --cc=bbrezillon@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-i3c@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®