* [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration
@ 2026-08-27 10:20 Johan Hovold
2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold
` (3 more replies)
0 siblings, 4 replies; 15+ messages in thread
From: Johan Hovold @ 2026-08-27 10:20 UTC (permalink / raw)
To: Andi Shyti; +Cc: Wolfram Sang, linux-i2c, linux-kernel, Johan Hovold
As part of addressing I2C lifetime issues, this series fixes an issue
which may actually matter to some people, at least if they are playing
around with USB attached I2C controllers, and whose fix does not depend
on any larger rework.
This could possibly go into 7.3, but can also wait for 7.4 as the
impact of the issue (both in terms of number of affected users and
symptoms) should be limited.
Johan
Changes in v2:
- take rwsem also on open()
- clear adap pointer also if chardev registration fails
Johan Hovold (3):
i2c: dev: fix blocked adapter deregistration
i2c: dev: drop unnecessary sysfs device lookup
i2c: dev: clean up registration
drivers/i2c/i2c-dev.c | 162 +++++++++++++++++++++++++++---------------
1 file changed, 103 insertions(+), 59 deletions(-)
--
2.54.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v2 1/3] i2c: dev: fix blocked adapter deregistration 2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold @ 2026-08-27 10:20 ` Johan Hovold 2026-11-09 15:08 ` Bartosz Golaszewski 2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold ` (2 subsequent siblings) 3 siblings, 1 reply; 15+ messages in thread From: Johan Hovold @ 2026-08-27 10:20 UTC (permalink / raw) To: Andi Shyti; +Cc: Wolfram Sang, linux-i2c, linux-kernel, Johan Hovold, stable The I2C subsystem allows controllers to be used by non-child devices that may remain registered after an adapter goes away. To handle this, adapter deregistration blocks until the last reference to the adapter is released. Albeit unorthodox, this is mostly fine for the vast majority of controllers but can cause some trouble when controllers reside on hotpluggable buses. Specifically, userspace can prevent an adapter from being deregistered indefinitely by holding an i2c-dev character device file open. And with USB attached controllers this prevents further hub events from being processed by the parent hub until the file is closed. Fix the i2c-dev implementation by dropping the additional reference taken at open() and using an rwsem to make sure the adapter is only accessed while registered. Note that before commit 611e12ea0f12 ("i2c: core: manage i2c bus device refcount in i2c_[get|put]_adapter") an adapter going away would instead have resulted in a use-after-free. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Johan Hovold <johan@kernel.org> --- drivers/i2c/i2c-dev.c | 95 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index ccaac5e29f90..0940070c0317 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -28,6 +28,7 @@ #include <linux/list.h> #include <linux/module.h> #include <linux/notifier.h> +#include <linux/rwsem.h> #include <linux/slab.h> #include <linux/uaccess.h> @@ -41,11 +42,17 @@ */ struct i2c_dev { struct list_head list; + struct rw_semaphore rwsem; struct i2c_adapter *adap; struct device dev; struct cdev cdev; }; +struct i2c_dev_data { + struct i2c_dev *i2c_dev; + struct i2c_client client; +}; + #define I2C_MINORS (MINORMASK + 1) static LIST_HEAD(i2c_dev_list); static DEFINE_SPINLOCK(i2c_dev_list_lock); @@ -92,6 +99,11 @@ static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev) spin_unlock(&i2c_dev_list_lock); if (del_cdev) cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev); + + scoped_guard(rwsem_write, &i2c_dev->rwsem) { + i2c_dev->adap = NULL; + } + put_device(&i2c_dev->dev); } @@ -134,10 +146,16 @@ ATTRIBUTE_GROUPS(i2c); static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { + struct i2c_dev_data *data = file->private_data; + struct i2c_client *client = &data->client; + struct i2c_dev *i2c_dev = data->i2c_dev; char *tmp; int ret; - struct i2c_client *client = file->private_data; + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; /* Adapter must support I2C transfers */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -163,9 +181,16 @@ static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, static ssize_t i2cdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { + struct i2c_dev_data *data = file->private_data; + struct i2c_client *client = &data->client; + struct i2c_dev *i2c_dev = data->i2c_dev; int ret; char *tmp; - struct i2c_client *client = file->private_data; + + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; /* Adapter must support I2C transfers */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -397,11 +422,15 @@ static noinline int i2cdev_ioctl_smbus(struct i2c_client *client, return res; } -static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +static long i2cdev_ioctl_locked(struct file *file, unsigned int cmd, unsigned long arg) { - struct i2c_client *client = file->private_data; + struct i2c_dev_data *data = file->private_data; + struct i2c_client *client = &data->client; + struct i2c_dev *i2c_dev = data->i2c_dev; unsigned long funcs; + lockdep_assert_held(&i2c_dev->rwsem); + dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n", cmd, arg); @@ -507,6 +536,19 @@ static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return 0; } +static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct i2c_dev_data *data = file->private_data; + struct i2c_dev *i2c_dev = data->i2c_dev; + + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; + + return i2cdev_ioctl_locked(file, cmd, arg); +} + #ifdef CONFIG_COMPAT struct i2c_smbus_ioctl_data32 { @@ -530,8 +572,16 @@ struct i2c_rdwr_ioctl_data32 { static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { - struct i2c_client *client = file->private_data; + struct i2c_dev_data *data = file->private_data; + struct i2c_client *client = &data->client; + struct i2c_dev *i2c_dev = data->i2c_dev; unsigned long funcs; + + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; + switch (cmd) { case I2C_FUNCS: funcs = i2c_get_functionality(client->adapter); @@ -588,7 +638,7 @@ static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lo compat_ptr(data32.data)); } default: - return i2cdev_ioctl(file, cmd, arg); + return i2cdev_ioctl_locked(file, cmd, arg); } } #else @@ -597,14 +647,18 @@ static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lo static int i2cdev_open(struct inode *inode, struct file *file) { - unsigned int minor = iminor(inode); + struct i2c_dev *i2c_dev = container_of(inode->i_cdev, struct i2c_dev, cdev); + struct i2c_dev_data *data; struct i2c_client *client; struct i2c_adapter *adap; - adap = i2c_get_adapter(minor); - if (!adap) + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) return -ENODEV; + adap = i2c_dev->adap; + /* This creates an anonymous i2c_client, which may later be * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE. * @@ -612,26 +666,27 @@ static int i2cdev_open(struct inode *inode, struct file *file) * or I2C core code!! It just holds private copies of addressing * information and maybe a PEC flag. */ - client = kzalloc_obj(*client); - if (!client) { - i2c_put_adapter(adap); + data = kzalloc_obj(*data); + if (!data) return -ENOMEM; - } + + data->i2c_dev = i2c_dev; + + client = &data->client; + snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); client->adapter = adap; - file->private_data = client; + file->private_data = data; return 0; } static int i2cdev_release(struct inode *inode, struct file *file) { - struct i2c_client *client = file->private_data; + struct i2c_dev_data *data = file->private_data; - i2c_put_adapter(client->adapter); - kfree(client); - file->private_data = NULL; + kfree(data); return 0; } @@ -675,8 +730,10 @@ static int i2cdev_attach_adapter(struct device *dev) if (IS_ERR(i2c_dev)) return NOTIFY_DONE; + init_rwsem(&i2c_dev->rwsem); + cdev_init(&i2c_dev->cdev, &i2cdev_fops); - i2c_dev->cdev.owner = THIS_MODULE; + i2c_dev->cdev.owner = adap->owner; device_initialize(&i2c_dev->dev); i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr); -- 2.54.0 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/3] i2c: dev: fix blocked adapter deregistration 2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold @ 2026-11-09 15:08 ` Bartosz Golaszewski 2026-11-10 12:34 ` Johan Hovold 0 siblings, 1 reply; 15+ messages in thread From: Bartosz Golaszewski @ 2026-11-09 15:08 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, Wolfram Sang, linux-i2c, linux-kernel, stable On Thu, Aug 27, 2026 at 12:20:41PM +0200, Johan Hovold wrote: > The I2C subsystem allows controllers to be used by non-child devices > that may remain registered after an adapter goes away. > > To handle this, adapter deregistration blocks until the last reference > to the adapter is released. Albeit unorthodox, this is mostly fine for > the vast majority of controllers but can cause some trouble when > controllers reside on hotpluggable buses. > > Specifically, userspace can prevent an adapter from being deregistered > indefinitely by holding an i2c-dev character device file open. And with > USB attached controllers this prevents further hub events from being > processed by the parent hub until the file is closed. > > Fix the i2c-dev implementation by dropping the additional reference > taken at open() and using an rwsem to make sure the adapter is only > accessed while registered. > > Note that before commit 611e12ea0f12 ("i2c: core: manage i2c bus device > refcount in i2c_[get|put]_adapter") an adapter going away would instead > have resulted in a use-after-free. > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Cc: stable@vger.kernel.org > Signed-off-by: Johan Hovold <johan@kernel.org> > --- > drivers/i2c/i2c-dev.c | 95 ++++++++++++++++++++++++++++++++++--------- > 1 file changed, 76 insertions(+), 19 deletions(-) > > diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c > index ccaac5e29f90..0940070c0317 100644 > --- a/drivers/i2c/i2c-dev.c > +++ b/drivers/i2c/i2c-dev.c > @@ -28,6 +28,7 @@ > #include <linux/list.h> > #include <linux/module.h> > #include <linux/notifier.h> > +#include <linux/rwsem.h> > #include <linux/slab.h> > #include <linux/uaccess.h> > > @@ -41,11 +42,17 @@ > */ > struct i2c_dev { > struct list_head list; > + struct rw_semaphore rwsem; > struct i2c_adapter *adap; > struct device dev; > struct cdev cdev; > }; > > +struct i2c_dev_data { > + struct i2c_dev *i2c_dev; > + struct i2c_client client; > +}; > + > #define I2C_MINORS (MINORMASK + 1) > static LIST_HEAD(i2c_dev_list); > static DEFINE_SPINLOCK(i2c_dev_list_lock); > @@ -92,6 +99,11 @@ static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev) > spin_unlock(&i2c_dev_list_lock); > if (del_cdev) > cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev); > + > + scoped_guard(rwsem_write, &i2c_dev->rwsem) { > + i2c_dev->adap = NULL; > + } > + > put_device(&i2c_dev->dev); > } > > @@ -134,10 +146,16 @@ ATTRIBUTE_GROUPS(i2c); > static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, > loff_t *offset) > { > + struct i2c_dev_data *data = file->private_data; > + struct i2c_client *client = &data->client; > + struct i2c_dev *i2c_dev = data->i2c_dev; > char *tmp; > int ret; > > - struct i2c_client *client = file->private_data; > + guard(rwsem_read)(&i2c_dev->rwsem); I think sashiko is correct in its comment and you should limit the critical section to until right after i2c_master_recv(). Same elsewhere. Looks good otherwise. Thanks, Bartosz [1] https://sashiko.dev/#/patchset/20260827102043.673273-1-johan%40kernel.org?part=1 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/3] i2c: dev: fix blocked adapter deregistration 2026-11-09 15:08 ` Bartosz Golaszewski @ 2026-11-10 12:34 ` Johan Hovold 0 siblings, 0 replies; 15+ messages in thread From: Johan Hovold @ 2026-11-10 12:34 UTC (permalink / raw) To: Bartosz Golaszewski Cc: Andi Shyti, Wolfram Sang, linux-i2c, linux-kernel, stable On Mon, Nov 09, 2026 at 04:08:46PM +0100, Bartosz Golaszewski wrote: > On Thu, Aug 27, 2026 at 12:20:41PM +0200, Johan Hovold wrote: > > The I2C subsystem allows controllers to be used by non-child devices > > that may remain registered after an adapter goes away. > > > > To handle this, adapter deregistration blocks until the last reference > > to the adapter is released. Albeit unorthodox, this is mostly fine for > > the vast majority of controllers but can cause some trouble when > > controllers reside on hotpluggable buses. > > > > Specifically, userspace can prevent an adapter from being deregistered > > indefinitely by holding an i2c-dev character device file open. And with > > USB attached controllers this prevents further hub events from being > > processed by the parent hub until the file is closed. > > > > Fix the i2c-dev implementation by dropping the additional reference > > taken at open() and using an rwsem to make sure the adapter is only > > accessed while registered. > > > > Note that before commit 611e12ea0f12 ("i2c: core: manage i2c bus device > > refcount in i2c_[get|put]_adapter") an adapter going away would instead > > have resulted in a use-after-free. > > > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > > Cc: stable@vger.kernel.org > > Signed-off-by: Johan Hovold <johan@kernel.org> > > --- > > static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, > > loff_t *offset) > > { > > + struct i2c_dev_data *data = file->private_data; > > + struct i2c_client *client = &data->client; > > + struct i2c_dev *i2c_dev = data->i2c_dev; > > char *tmp; > > int ret; > > > > - struct i2c_client *client = file->private_data; > > + guard(rwsem_read)(&i2c_dev->rwsem); > > I think sashiko is correct in its comment and you should limit the > critical section to until right after i2c_master_recv(). Same elsewhere. I don't think that's anything we need to care about. If a malicious actor has physical access to the machine, we have bigger problems than worrying about them going out of their way to cause a USB hub worker thread to hang. :) Johan > [1] https://sashiko.dev/#/patchset/20260827102043.673273-1-johan%40kernel.org?part=1 ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup 2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold 2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold @ 2026-08-27 10:20 ` Johan Hovold 2026-11-09 14:59 ` Bartosz Golaszewski 2026-11-10 11:46 ` Wolfram Sang 2026-08-27 10:20 ` [PATCH v2 3/3] i2c: dev: clean up registration Johan Hovold 2026-11-04 19:42 ` [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Wolfram Sang 3 siblings, 2 replies; 15+ messages in thread From: Johan Hovold @ 2026-08-27 10:20 UTC (permalink / raw) To: Andi Shyti; +Cc: Wolfram Sang, linux-i2c, linux-kernel, Johan Hovold 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@kernel.org> --- 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 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup 2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold @ 2026-11-09 14:59 ` Bartosz Golaszewski 2026-11-10 11:46 ` Wolfram Sang 1 sibling, 0 replies; 15+ messages in thread From: Bartosz Golaszewski @ 2026-11-09 14:59 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, Wolfram Sang, linux-i2c, linux-kernel On Thu, Aug 27, 2026 at 12:20:42PM +0200, Johan Hovold wrote: > 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@kernel.org> > --- Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup 2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold 2026-11-09 14:59 ` Bartosz Golaszewski @ 2026-11-10 11:46 ` Wolfram Sang 1 sibling, 0 replies; 15+ messages in thread From: Wolfram Sang @ 2026-11-10 11:46 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, linux-i2c, linux-kernel On Thu, Aug 27, 2026 at 12:20:42PM +0200, Johan Hovold wrote: > 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@kernel.org> Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com> ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 3/3] i2c: dev: clean up registration 2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold 2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold 2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold @ 2026-08-27 10:20 ` Johan Hovold 2026-11-09 15:01 ` Bartosz Golaszewski 2026-11-10 11:50 ` Wolfram Sang 2026-11-04 19:42 ` [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Wolfram Sang 3 siblings, 2 replies; 15+ messages in thread From: Johan Hovold @ 2026-08-27 10:20 UTC (permalink / raw) To: Andi Shyti; +Cc: Wolfram Sang, linux-i2c, linux-kernel, Johan Hovold Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup and teardown directly in i2cdev_attach_adapter() and i2cdev_detach_adapter() for consistency and to make the logic clearer. Note that the device list is only used at detach so the i2c-dev can be added after registering the class device. Signed-off-by: Johan Hovold <johan@kernel.org> --- drivers/i2c/i2c-dev.c | 69 ++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 49c05f2eb223..9ad145f67fc1 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -73,41 +73,6 @@ static struct i2c_dev *i2c_dev_get_by_minor(unsigned index) return i2c_dev; } -static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) -{ - struct i2c_dev *i2c_dev; - - if (adap->nr >= I2C_MINORS) { - pr_err("Out of device minors (%d)\n", adap->nr); - return ERR_PTR(-ENODEV); - } - - i2c_dev = kzalloc_obj(*i2c_dev); - if (!i2c_dev) - return ERR_PTR(-ENOMEM); - i2c_dev->adap = adap; - - spin_lock(&i2c_dev_list_lock); - list_add_tail(&i2c_dev->list, &i2c_dev_list); - spin_unlock(&i2c_dev_list_lock); - return i2c_dev; -} - -static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev) -{ - spin_lock(&i2c_dev_list_lock); - list_del(&i2c_dev->list); - spin_unlock(&i2c_dev_list_lock); - if (del_cdev) - cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev); - - scoped_guard(rwsem_write, &i2c_dev->rwsem) { - i2c_dev->adap = NULL; - } - - put_device(&i2c_dev->dev); -} - static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -724,11 +689,17 @@ static int i2cdev_attach_adapter(struct device *dev) return NOTIFY_DONE; adap = to_i2c_adapter(dev); - i2c_dev = get_free_i2c_dev(adap); - if (IS_ERR(i2c_dev)) + if (adap->nr >= I2C_MINORS) { + pr_err("Out of device minors (%d)\n", adap->nr); + return NOTIFY_DONE; + } + + i2c_dev = kzalloc_obj(*i2c_dev); + if (!i2c_dev) return NOTIFY_DONE; init_rwsem(&i2c_dev->rwsem); + i2c_dev->adap = adap; cdev_init(&i2c_dev->cdev, &i2cdev_fops); i2c_dev->cdev.owner = adap->owner; @@ -745,13 +716,21 @@ static int i2cdev_attach_adapter(struct device *dev) res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev); if (res) - goto err_put_i2c_dev; + goto err_clear_adap; + + spin_lock(&i2c_dev_list_lock); + list_add_tail(&i2c_dev->list, &i2c_dev_list); + spin_unlock(&i2c_dev_list_lock); pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr); return NOTIFY_OK; +err_clear_adap: + scoped_guard(rwsem_write, &i2c_dev->rwsem) { + i2c_dev->adap = NULL; + } err_put_i2c_dev: - put_i2c_dev(i2c_dev, false); + put_device(&i2c_dev->dev); return NOTIFY_DONE; } @@ -768,7 +747,17 @@ static int i2cdev_detach_adapter(struct device *dev) if (!i2c_dev) /* attach_adapter must have failed */ return NOTIFY_DONE; - put_i2c_dev(i2c_dev, true); + spin_lock(&i2c_dev_list_lock); + list_del(&i2c_dev->list); + spin_unlock(&i2c_dev_list_lock); + + cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev); + + scoped_guard(rwsem_write, &i2c_dev->rwsem) { + i2c_dev->adap = NULL; + } + + put_device(&i2c_dev->dev); pr_debug("adapter [%s] unregistered\n", adap->name); return NOTIFY_OK; -- 2.54.0 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/3] i2c: dev: clean up registration 2026-08-27 10:20 ` [PATCH v2 3/3] i2c: dev: clean up registration Johan Hovold @ 2026-11-09 15:01 ` Bartosz Golaszewski 2026-11-10 11:50 ` Wolfram Sang 1 sibling, 0 replies; 15+ messages in thread From: Bartosz Golaszewski @ 2026-11-09 15:01 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, Wolfram Sang, linux-i2c, linux-kernel On Thu, Aug 27, 2026 at 12:20:43PM +0200, Johan Hovold wrote: > Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup > and teardown directly in i2cdev_attach_adapter() and > i2cdev_detach_adapter() for consistency and to make the logic clearer. > > Note that the device list is only used at detach so the i2c-dev can be > added after registering the class device. > > Signed-off-by: Johan Hovold <johan@kernel.org> > --- I'm not convinced this results in better or cleaner code but I'll let Wolfram be the judge here. FWIW: Acked-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> Bartosz ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/3] i2c: dev: clean up registration 2026-08-27 10:20 ` [PATCH v2 3/3] i2c: dev: clean up registration Johan Hovold 2026-11-09 15:01 ` Bartosz Golaszewski @ 2026-11-10 11:50 ` Wolfram Sang 2026-11-10 12:42 ` Johan Hovold 1 sibling, 1 reply; 15+ messages in thread From: Wolfram Sang @ 2026-11-10 11:50 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, linux-i2c, linux-kernel On Thu, Aug 27, 2026 at 12:20:43PM +0200, Johan Hovold wrote: > Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup > and teardown directly in i2cdev_attach_adapter() and > i2cdev_detach_adapter() for consistency and to make the logic clearer. > > Note that the device list is only used at detach so the i2c-dev can be > added after registering the class device. Can we have the last sentence as a comment in the code, too, please? > Signed-off-by: Johan Hovold <johan@kernel.org> Other than that, I think this patch is an improvement. The logic is more explicit. I didn't like 'put_i2c_dev' having a bool parameter, for example. So, only patch 1 needs a bigger update, it seems. Rest is looking good to me. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/3] i2c: dev: clean up registration 2026-11-10 11:50 ` Wolfram Sang @ 2026-11-10 12:42 ` Johan Hovold 2026-11-10 12:54 ` Bartosz Golaszewski 0 siblings, 1 reply; 15+ messages in thread From: Johan Hovold @ 2026-11-10 12:42 UTC (permalink / raw) To: Wolfram Sang; +Cc: Andi Shyti, linux-i2c, linux-kernel On Tue, Nov 10, 2026 at 12:50:35PM +0100, Wolfram Sang wrote: > On Thu, Aug 27, 2026 at 12:20:43PM +0200, Johan Hovold wrote: > > Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup > > and teardown directly in i2cdev_attach_adapter() and > > i2cdev_detach_adapter() for consistency and to make the logic clearer. > > > > Note that the device list is only used at detach so the i2c-dev can be > > added after registering the class device. > > Can we have the last sentence as a comment in the code, too, please? Sure. > > Signed-off-by: Johan Hovold <johan@kernel.org> > > Other than that, I think this patch is an improvement. The logic is more > explicit. I didn't like 'put_i2c_dev' having a bool parameter, for > example. > > So, only patch 1 needs a bigger update, it seems. Rest is looking good > to me. I had rejected that Sashiko finding as being too outlandish for us to have to worry about. As I just in a reply to Bartosz: If a malicious actor has physical access to the machine, we have bigger problems than worrying about them going out of their way to cause a USB hub worker thread to hang. :) Limiting the critical section can possibly be done as a follow-up, but it may not even be worth the effort doing that. Johan ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/3] i2c: dev: clean up registration 2026-11-10 12:42 ` Johan Hovold @ 2026-11-10 12:54 ` Bartosz Golaszewski 2026-11-10 13:07 ` Johan Hovold 0 siblings, 1 reply; 15+ messages in thread From: Bartosz Golaszewski @ 2026-11-10 12:54 UTC (permalink / raw) To: Johan Hovold; +Cc: Wolfram Sang, Andi Shyti, linux-i2c, linux-kernel On Tue, Nov 10, 2026 at 01:42:29PM +0100, Johan Hovold wrote: > On Tue, Nov 10, 2026 at 12:50:35PM +0100, Wolfram Sang wrote: > > On Thu, Aug 27, 2026 at 12:20:43PM +0200, Johan Hovold wrote: > > > Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup > > > and teardown directly in i2cdev_attach_adapter() and > > > i2cdev_detach_adapter() for consistency and to make the logic clearer. > > > > > > Note that the device list is only used at detach so the i2c-dev can be > > > added after registering the class device. > > > > Can we have the last sentence as a comment in the code, too, please? > > Sure. > > > > Signed-off-by: Johan Hovold <johan@kernel.org> > > > > Other than that, I think this patch is an improvement. The logic is more > > explicit. I didn't like 'put_i2c_dev' having a bool parameter, for > > example. > > > > So, only patch 1 needs a bigger update, it seems. Rest is looking good > > to me. > > I had rejected that Sashiko finding as being too outlandish for us to > have to worry about. As I just in a reply to Bartosz: > > If a malicious actor has physical access to the machine, we have > bigger problems than worrying about them going out of their way > to cause a USB hub worker thread to hang. :) > > Limiting the critical section can possibly be done as a follow-up, but > it may not even be worth the effort doing that. > Why is limiting the scope of the semaphore such a big deal though? It's not a big change, literally just use scoped_guard() instead. Why would we commit incorrect code if we can correct it in a v3 with not much effort? You're fixing a use-case where user-space can hang a kernel thread, why not do it for corner-cases too? Bart ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/3] i2c: dev: clean up registration 2026-11-10 12:54 ` Bartosz Golaszewski @ 2026-11-10 13:07 ` Johan Hovold 0 siblings, 0 replies; 15+ messages in thread From: Johan Hovold @ 2026-11-10 13:07 UTC (permalink / raw) To: Bartosz Golaszewski; +Cc: Wolfram Sang, Andi Shyti, linux-i2c, linux-kernel On Tue, Nov 10, 2026 at 01:54:18PM +0100, Bartosz Golaszewski wrote: > On Tue, Nov 10, 2026 at 01:42:29PM +0100, Johan Hovold wrote: > > I had rejected that Sashiko finding as being too outlandish for us to > > have to worry about. As I just in a reply to Bartosz: > > > > If a malicious actor has physical access to the machine, we have > > bigger problems than worrying about them going out of their way > > to cause a USB hub worker thread to hang. :) > > > > Limiting the critical section can possibly be done as a follow-up, but > > it may not even be worth the effort doing that. > > > > Why is limiting the scope of the semaphore such a big deal though? It's > not a big change, literally just use scoped_guard() instead. Why would > we commit incorrect code if we can correct it in a v3 with not much > effort? You're fixing a use-case where user-space can hang a kernel > thread, why not do it for corner-cases too? A *malicious* actor with physical access (and CAP_SYS_PTRACE) can cause a thread to hang while janking out an USB attached I2C adapter out a of machine if they try really hard. Don't you see how silly that is? I posted this patch months ago, but IIRC it will complicate things somewhat without any relevant benefit. Probably just a larger diff which is unnecessary for a patch marked for stable. If Wolfram insists, I can revisit this, but I don't think it's needed. Johan ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration 2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold ` (2 preceding siblings ...) 2026-08-27 10:20 ` [PATCH v2 3/3] i2c: dev: clean up registration Johan Hovold @ 2026-11-04 19:42 ` Wolfram Sang 2026-11-05 8:18 ` Bartosz Golaszewski 3 siblings, 1 reply; 15+ messages in thread From: Wolfram Sang @ 2026-11-04 19:42 UTC (permalink / raw) To: Johan Hovold; +Cc: Andi Shyti, linux-i2c, linux-kernel, Bartosz Golaszewski [-- Attachment #1: Type: text/plain, Size: 718 bytes --] On Thu, Aug 27, 2026 at 12:20:40PM +0200, Johan Hovold wrote: > As part of addressing I2C lifetime issues, this series fixes an issue > which may actually matter to some people, at least if they are playing > around with USB attached I2C controllers, and whose fix does not depend > on any larger rework. > > This could possibly go into 7.3, but can also wait for 7.4 as the > impact of the issue (both in terms of number of affected users and > symptoms) should be limited. Oh wow, I completely missed this for some reason :( Just discovered it while digging around in patchwork. I will try to find some time soon, so we can have it at least in 7.5. Adding Bart to CC, maybe he is interested. [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 833 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration 2026-11-04 19:42 ` [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Wolfram Sang @ 2026-11-05 8:18 ` Bartosz Golaszewski 0 siblings, 0 replies; 15+ messages in thread From: Bartosz Golaszewski @ 2026-11-05 8:18 UTC (permalink / raw) To: Wolfram Sang; +Cc: Johan Hovold, Andi Shyti, linux-i2c, linux-kernel On Wed, Nov 04, 2026 at 09:42:49PM +0200, Wolfram Sang wrote: > On Thu, Aug 27, 2026 at 12:20:40PM +0200, Johan Hovold wrote: > > As part of addressing I2C lifetime issues, this series fixes an issue > > which may actually matter to some people, at least if they are playing > > around with USB attached I2C controllers, and whose fix does not depend > > on any larger rework. > > > > This could possibly go into 7.3, but can also wait for 7.4 as the > > impact of the issue (both in terms of number of affected users and > > symptoms) should be limited. > > Oh wow, I completely missed this for some reason :( Just discovered it > while digging around in patchwork. > > I will try to find some time soon, so we can have it at least in 7.5. > > Adding Bart to CC, maybe he is interested. > Yeah, I didn't see it either. And yes, I'm obviously interested, this has been my pet-peeve for years. :) I'm adding this to my TODO list for today. Bart ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-11-10 13:07 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold 2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold 2026-11-09 15:08 ` Bartosz Golaszewski 2026-11-10 12:34 ` Johan Hovold 2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold 2026-11-09 14:59 ` Bartosz Golaszewski 2026-11-10 11:46 ` Wolfram Sang 2026-08-27 10:20 ` [PATCH v2 3/3] i2c: dev: clean up registration Johan Hovold 2026-11-09 15:01 ` Bartosz Golaszewski 2026-11-10 11:50 ` Wolfram Sang 2026-11-10 12:42 ` Johan Hovold 2026-11-10 12:54 ` Bartosz Golaszewski 2026-11-10 13:07 ` Johan Hovold 2026-11-04 19:42 ` [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Wolfram Sang 2026-11-05 8:18 ` Bartosz Golaszewski
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®