[PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup

From: Johan Hovold

Date: Thu Aug 27 2026 - 06:23:12 EST


The sysfs lifetime rules guarantees that the i2c-dev class device is
still valid while its attribute callbacks are executing so drop the
unnecessary reverse lookup.

Signed-off-by: Johan Hovold <johan@xxxxxxxxxx>
---
drivers/i2c/i2c-dev.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 0940070c0317..49c05f2eb223 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -47,6 +47,7 @@ struct i2c_dev {
struct device dev;
struct cdev cdev;
};
+#define to_i2c_dev(d) container_of((d), struct i2c_dev, dev)

struct i2c_dev_data {
struct i2c_dev *i2c_dev;
@@ -110,10 +111,8 @@ static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
static ssize_t name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
+ struct i2c_dev *i2c_dev = to_i2c_dev(dev);

- if (!i2c_dev)
- return -ENODEV;
return sysfs_emit(buf, "%s\n", i2c_dev->adap->name);
}
static DEVICE_ATTR_RO(name);
@@ -710,9 +709,8 @@ static const struct class i2c_dev_class = {

static void i2cdev_dev_release(struct device *dev)
{
- struct i2c_dev *i2c_dev;
+ struct i2c_dev *i2c_dev = to_i2c_dev(dev);

- i2c_dev = container_of(dev, struct i2c_dev, dev);
kfree(i2c_dev);
}

--
2.54.0