From nobody Sun Sep 27 02:07:38 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 211AF3E835E; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; cv=none; b=lpzUiYw4VbRVVqMuLfZNwpTfOE2mfnnozz4E6IYSemwWpCDbLa6il6h2Zzn7avqAYkuh+SIQruY3StmiQs1Bh41nOOLwpV/T3cUsGwtIszWW6tYxHnd8hiYRWgB7vtBLrxMaaK25sZAl+we+OpGJHakiqGLyRF0tChZIXD7TP74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; c=relaxed/simple; bh=4787ZnHZ6YTIRSvWSls19SZyHGiiVuZ7+8lwIGiAPrc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TLf0gp5BeSNECtCxCm1CTwBBMoJeBM6nxsl3uufaC2dJtaHJ8fK3mU/t0IuePAa5b0KyiNcaSXuZAvw+SYpNHUvnnUiDvGtIiBH23p5hCsZZYiZQ6dxFr1B5MJ8a5LOcwIike2FszI5XkFvnHteYPhiLp7s/dJnTHd8tbjrd3ek= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N5x9DIpr; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N5x9DIpr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73C111F00A3A; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787821722; bh=uMuQFE4jZ84ox+X7P6Q1JZqLqanxrdsEZvNGyQngblQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=N5x9DIprcsYiYO4FPi6WR3MV6s6vR1SA7mH4S+8xtCB70v+Esztdh+PIoa3x4Npfw OwgFxxzRFbNY4AS1jGm0QILtYR7qiPec+EjZQ4TFH7DgA2wQpC8o30O0MtvNf44VWj MEyzP3sk9SfHxy+TscEgOqnF9YfLWWwX7DPqhKeky3wIjk2hT4sAz/RNsKFLjQdRKv BTusmhkGvFmpPYamYgbP0NBlCiqdjNrSosyXd+SKOM+X4yrsaPHT0knSUCwvZr3i/k BYb5VHjVepM+pO2zBcEWzOik9hy2s9hxmQRXLqQ4lG4E+0pLgyMvEpZ7NL7OycD3Vm 5PBdttd7Ztopg== Received: from johan by xi.lan with local (Exim 4.99.4) (envelope-from ) id 1wzW6C-00000002g8F-0qh4; Thu, 27 Aug 2026 11:08:40 +0200 From: Johan Hovold To: Andi Shyti Cc: Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold , stable@vger.kernel.org Subject: [PATCH 1/3] i2c: dev: fix blocked adapter deregistration Date: Thu, 27 Aug 2026 11:08:02 +0200 Message-ID: <20260827090804.638565-2-johan@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260827090804.638565-1-johan@kernel.org> References: <20260827090804.638565-1-johan@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- drivers/i2c/i2c-dev.c | 97 +++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index ccaac5e29f90..05616249f61b 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include =20 @@ -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; }; =20 +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); @@ -90,8 +97,14 @@ static void put_i2c_dev(struct i2c_dev *i2c_dev, bool de= l_cdev) spin_lock(&i2c_dev_list_lock); list_del(&i2c_dev->list); spin_unlock(&i2c_dev_list_lock); - if (del_cdev) + if (del_cdev) { cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev); + + scoped_guard(rwsem_write, &i2c_dev->rwsem) { + i2c_dev->adap =3D NULL; + } + } + put_device(&i2c_dev->dev); } =20 @@ -134,10 +147,16 @@ ATTRIBUTE_GROUPS(i2c); static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t cou= nt, loff_t *offset) { + struct i2c_dev_data *data =3D file->private_data; + struct i2c_client *client =3D &data->client; + struct i2c_dev *i2c_dev =3D data->i2c_dev; char *tmp; int ret; =20 - struct i2c_client *client =3D file->private_data; + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; =20 /* Adapter must support I2C transfers */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -163,9 +182,16 @@ static ssize_t i2cdev_read(struct file *file, char __u= ser *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 =3D file->private_data; + struct i2c_client *client =3D &data->client; + struct i2c_dev *i2c_dev =3D data->i2c_dev; int ret; char *tmp; - struct i2c_client *client =3D file->private_data; + + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; =20 /* Adapter must support I2C transfers */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -397,11 +423,15 @@ static noinline int i2cdev_ioctl_smbus(struct i2c_cli= ent *client, return res; } =20 -static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lon= g arg) +static long i2cdev_ioctl_locked(struct file *file, unsigned int cmd, unsig= ned long arg) { - struct i2c_client *client =3D file->private_data; + struct i2c_dev_data *data =3D file->private_data; + struct i2c_client *client =3D &data->client; + struct i2c_dev *i2c_dev =3D data->i2c_dev; unsigned long funcs; =20 + lockdep_assert_held(&i2c_dev->rwsem); + dev_dbg(&client->adapter->dev, "ioctl, cmd=3D0x%02x, arg=3D0x%02lx\n", cmd, arg); =20 @@ -507,6 +537,19 @@ static long i2cdev_ioctl(struct file *file, unsigned i= nt cmd, unsigned long arg) return 0; } =20 +static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lon= g arg) +{ + struct i2c_dev_data *data =3D file->private_data; + struct i2c_dev *i2c_dev =3D 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 =20 struct i2c_smbus_ioctl_data32 { @@ -530,8 +573,16 @@ struct i2c_rdwr_ioctl_data32 { =20 static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsig= ned long arg) { - struct i2c_client *client =3D file->private_data; + struct i2c_dev_data *data =3D file->private_data; + struct i2c_client *client =3D &data->client; + struct i2c_dev *i2c_dev =3D data->i2c_dev; unsigned long funcs; + + guard(rwsem_read)(&i2c_dev->rwsem); + + if (!i2c_dev->adap) + return -ENODEV; + switch (cmd) { case I2C_FUNCS: funcs =3D i2c_get_functionality(client->adapter); @@ -588,7 +639,7 @@ static long compat_i2cdev_ioctl(struct file *file, unsi= gned int cmd, unsigned lo compat_ptr(data32.data)); } default: - return i2cdev_ioctl(file, cmd, arg); + return i2cdev_ioctl_locked(file, cmd, arg); } } #else @@ -597,13 +648,10 @@ static long compat_i2cdev_ioctl(struct file *file, un= signed int cmd, unsigned lo =20 static int i2cdev_open(struct inode *inode, struct file *file) { - unsigned int minor =3D iminor(inode); + struct i2c_dev *i2c_dev =3D container_of(inode->i_cdev, struct i2c_dev, c= dev); + struct i2c_adapter *adap =3D i2c_dev->adap; + struct i2c_dev_data *data; struct i2c_client *client; - struct i2c_adapter *adap; - - adap =3D i2c_get_adapter(minor); - if (!adap) - return -ENODEV; =20 /* This creates an anonymous i2c_client, which may later be * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE. @@ -612,26 +660,27 @@ static int i2cdev_open(struct inode *inode, struct fi= le *file) * or I2C core code!! It just holds private copies of addressing * information and maybe a PEC flag. */ - client =3D kzalloc_obj(*client); - if (!client) { - i2c_put_adapter(adap); + data =3D kzalloc_obj(*data); + if (!data) return -ENOMEM; - } + + data->i2c_dev =3D i2c_dev; + + client =3D &data->client; + snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); =20 client->adapter =3D adap; - file->private_data =3D client; + file->private_data =3D data; =20 return 0; } =20 static int i2cdev_release(struct inode *inode, struct file *file) { - struct i2c_client *client =3D file->private_data; + struct i2c_dev_data *data =3D file->private_data; =20 - i2c_put_adapter(client->adapter); - kfree(client); - file->private_data =3D NULL; + kfree(data); =20 return 0; } @@ -675,8 +724,10 @@ static int i2cdev_attach_adapter(struct device *dev) if (IS_ERR(i2c_dev)) return NOTIFY_DONE; =20 + init_rwsem(&i2c_dev->rwsem); + cdev_init(&i2c_dev->cdev, &i2cdev_fops); - i2c_dev->cdev.owner =3D THIS_MODULE; + i2c_dev->cdev.owner =3D adap->owner; =20 device_initialize(&i2c_dev->dev); i2c_dev->dev.devt =3D MKDEV(I2C_MAJOR, adap->nr); --=20 2.54.0 From nobody Sun Sep 27 02:07:38 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2025336B912; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; cv=none; b=WHTciljMBkXTn1t/CqWN/Q20YF/nnqKD1Rg9L8MGEzJSDP7cwJn/QTFdwN+Wd5KPwFGYx9+DmPpevaRnlSNGwDzfrCjFK4VPJAjfdoTzqKYIGYuccjbUSb8MoZI4bc471GOki5v4q5pR8Q/MFpau0yMXxGjG9b698F9kp6ohxUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; c=relaxed/simple; bh=/GksnhYNesP45HiePsqhiIN1kWJNEvj5aSBFPP2lGBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dYDJyruBGF8EYzDO0BU7YWn+4uAV3j2CIUuaFNCecOUrTMFyPofx7e9mtQphAnGb/ulHqoJfIqQdEWrZWe8u9iYKvhiVdWQBvnpL1Ui8lSSVabooLV7Ye6h3DBIjNM/wVCSgQ8rQfTFjLycGlkywLblmTRQj9AYETYp/Pyjk6Xc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OeYlCQWj; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OeYlCQWj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7957A1F00A3F; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787821722; bh=fNDsmY25d2+79qMBcXoyfYGND5DKyfIxnRNdUo9Ozfo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OeYlCQWjIrCdRDmJ4bC6t2t/RLpw6rPNDSZSWqFMC85zg+rTamRwUxSwqYBXQoJfg 3aAR16zllatXxKBGfMJl1WSV/wWwHTl7tZmbB+zMAcBqbyk4yJgcwauyQIi7XgeLv1 RNnz0AGmU43Ga3n5e/q+HRY4NFy2yXPSAekDsjz7YRIHKz+xTazAMqufw2Qh0wG6Bz RCrurFLM7bXCaR0GCsMlvHQEPXKhlqSzkZDKMrYgwb9wG5tR0H+UvI19C14Dy6tjhU tk40mNkm0WK3Wj1smVqwWDo+nMCRX9IW/QvKGFmO8k0N5aJ++NZNSSxHG0/su9T/k4 KmerCa1YaqMZg== Received: from johan by xi.lan with local (Exim 4.99.4) (envelope-from ) id 1wzW6C-00000002g8H-0tDw; Thu, 27 Aug 2026 11:08:40 +0200 From: Johan Hovold To: Andi Shyti Cc: Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold Subject: [PATCH 2/3] i2c: dev: drop unnecessary sysfs device lookup Date: Thu, 27 Aug 2026 11:08:03 +0200 Message-ID: <20260827090804.638565-3-johan@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260827090804.638565-1-johan@kernel.org> References: <20260827090804.638565-1-johan@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 05616249f61b..319bc46cc207 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) =20 struct i2c_dev_data { struct i2c_dev *i2c_dev; @@ -111,10 +112,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 =3D i2c_dev_get_by_minor(MINOR(dev->devt)); + struct i2c_dev *i2c_dev =3D to_i2c_dev(dev); =20 - if (!i2c_dev) - return -ENODEV; return sysfs_emit(buf, "%s\n", i2c_dev->adap->name); } static DEVICE_ATTR_RO(name); @@ -704,9 +703,8 @@ static const struct class i2c_dev_class =3D { =20 static void i2cdev_dev_release(struct device *dev) { - struct i2c_dev *i2c_dev; + struct i2c_dev *i2c_dev =3D to_i2c_dev(dev); =20 - i2c_dev =3D container_of(dev, struct i2c_dev, dev); kfree(i2c_dev); } =20 --=20 2.54.0 From nobody Sun Sep 27 02:07:38 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2B933FDC05; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; cv=none; b=a/26Nb1CsuA4L1K0x+snJbAjWSENM02EssxNhStdUT01pjz+w8KAST1C+VqW8BB0S9DmoOCzhCjzLld9mVrRIhqD5KUN4Cnz6iNY5JFCxV4mSXKbQ/WCNa0Po6/rLJmhaVKRJm5sby2uRKgHkzwArY5QTiVgPpSBQ9pVi4L1Lj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787821726; c=relaxed/simple; bh=Xxv7LmKVMXjjYZv8NFNSnSakxsax5lAclsdAXTnQ2l8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ItyCHQX2YORjnHZMDu59c3tYRo27Hrma2PhBtvLArtTJh6bHNtZF7Benq7aiVNIHTIqYK3ZeVb1GvXqb1AMb4vwl94LEpMHCjxOjIMF5WtNvXSYLvqz7kuAFEesgoLaYeptgWrflG5/8Paknv2cQn/8QtLm+R+8+9yVuhDCStDE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lAhJr41G; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lAhJr41G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75DF61F00A3D; Thu, 27 Aug 2026 09:08:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787821722; bh=F71mUxpCC9lB/TEKxDCkjypZdTsveKGITFGaS7b2D3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lAhJr41GxJEmPP3oyjG45cL2Vbh5L1ZjsdRdEei2nqSzlLO5AUj+WoboC0Ig52pub dgVurvYvDnaicJJfKRumb6NCSnqDlC/pSSOsBNcVmFo1qC01eHiyd59zCim/Bp7GIy FuIXCzsSlZ4kIXqzyGXwQsRpYNu/JcIcf5HHybhKDiZGVTlzSy6YQorILeJkVfgsdV i8ly9vl+4WmYHtXYSmD0NizTg0bVmpvM4f9UZZb2NBH6xVHAOox3OI1IT9j91bdM0O /VfAyX79/ltgfSrLiY1EVNKbVk7HILv5neb2wcK086/rI4uDL+bHlOccgA6gRh4QmB Qv3g+3Whf/MDQ== Received: from johan by xi.lan with local (Exim 4.99.4) (envelope-from ) id 1wzW6C-00000002g8J-0vxR; Thu, 27 Aug 2026 11:08:40 +0200 From: Johan Hovold To: Andi Shyti Cc: Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold Subject: [PATCH 3/3] i2c: dev: clean up registration Date: Thu, 27 Aug 2026 11:08:04 +0200 Message-ID: <20260827090804.638565-4-johan@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260827090804.638565-1-johan@kernel.org> References: <20260827090804.638565-1-johan@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all set up and tear down 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 --- drivers/i2c/i2c-dev.c | 64 ++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 319bc46cc207..1aa798b87dcd 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -73,42 +73,6 @@ static struct i2c_dev *i2c_dev_get_by_minor(unsigned ind= ex) return i2c_dev; } =20 -static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) -{ - struct i2c_dev *i2c_dev; - - if (adap->nr >=3D I2C_MINORS) { - pr_err("Out of device minors (%d)\n", adap->nr); - return ERR_PTR(-ENODEV); - } - - i2c_dev =3D kzalloc_obj(*i2c_dev); - if (!i2c_dev) - return ERR_PTR(-ENOMEM); - i2c_dev->adap =3D 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 =3D NULL; - } - } - - put_device(&i2c_dev->dev); -} - static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -718,11 +682,17 @@ static int i2cdev_attach_adapter(struct device *dev) return NOTIFY_DONE; adap =3D to_i2c_adapter(dev); =20 - i2c_dev =3D get_free_i2c_dev(adap); - if (IS_ERR(i2c_dev)) + if (adap->nr >=3D I2C_MINORS) { + pr_err("Out of device minors (%d)\n", adap->nr); + return NOTIFY_DONE; + } + + i2c_dev =3D kzalloc_obj(*i2c_dev); + if (!i2c_dev) return NOTIFY_DONE; =20 init_rwsem(&i2c_dev->rwsem); + i2c_dev->adap =3D adap; =20 cdev_init(&i2c_dev->cdev, &i2cdev_fops); i2c_dev->cdev.owner =3D adap->owner; @@ -741,11 +711,15 @@ static int i2cdev_attach_adapter(struct device *dev) if (res) goto err_put_i2c_dev; =20 + 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; =20 err_put_i2c_dev: - put_i2c_dev(i2c_dev, false); + put_device(&i2c_dev->dev); return NOTIFY_DONE; } =20 @@ -762,7 +736,17 @@ static int i2cdev_detach_adapter(struct device *dev) if (!i2c_dev) /* attach_adapter must have failed */ return NOTIFY_DONE; =20 - 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 =3D NULL; + } + + put_device(&i2c_dev->dev); =20 pr_debug("adapter [%s] unregistered\n", adap->name); return NOTIFY_OK; --=20 2.54.0