Re: [PATCH 1/3] i2c: dev: fix blocked adapter deregistration
From: Johan Hovold
Date: Thu Aug 27 2026 - 05:45:31 EST
On Thu, Aug 27, 2026 at 11:08:02AM +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@xxxxxxxxxxxxxxx
> Signed-off-by: Johan Hovold <johan@xxxxxxxxxx>
> 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_adapter *adap = i2c_dev->adap;
Bah, I of course need to take the rwsem here as well as pointed out by
Sashiko.
Let me respin.
Johan