From: Heiner Kallweit <hkallweit1@gmail.com>
To: Wolfram Sang <wsa+renesas@sang-engineering.com>
Cc: "linux-i2c@vger.kernel.org" <linux-i2c@vger.kernel.org>
Subject: [PATCH] i2c: core: Switch adapter number assignment from idr to xarray
Date: Sun, 14 Sep 2025 22:10:38 +0200 [thread overview]
Message-ID: <09c81521-1444-4c6d-8ded-4318c95d6bdd@gmail.com> (raw)
IDR API is deprecated, see 85656ec193e9 ("IDR: Note that the IDR API
is deprecated"). So replace it with an xarray here. One benefit is
that xarray includes locking, therefore we can remove taking core_lock
for id operations.
In my tests there was no change in assigned adapter numbers.
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
drivers/i2c/i2c-core-base.c | 62 ++++++++++++++-----------------------
1 file changed, 24 insertions(+), 38 deletions(-)
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index ecca8c006..8c3a30082 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -23,7 +23,6 @@
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/i2c-smbus.h>
-#include <linux/idr.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
@@ -42,6 +41,7 @@
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <linux/string_choices.h>
+#include <linux/xarray.h>
#include "i2c-core.h"
@@ -56,12 +56,10 @@
#define I2C_ADDR_DEVICE_ID 0x7c
-/*
- * core_lock protects i2c_adapter_idr, and guarantees that device detection,
- * deletion of detected devices are serialized
- */
+/* core_lock guarantees that device detection, deletion of detected devices are serialized */
static DEFINE_MUTEX(core_lock);
-static DEFINE_IDR(i2c_adapter_idr);
+
+static DEFINE_XARRAY_ALLOC(i2c_adapter_xa);
static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
@@ -1613,9 +1611,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
device_unregister(&adap->dev);
wait_for_completion(&adap->dev_released);
out_list:
- mutex_lock(&core_lock);
- idr_remove(&i2c_adapter_idr, adap->nr);
- mutex_unlock(&core_lock);
+ xa_erase(&i2c_adapter_xa, adap->nr);
return res;
}
@@ -1628,13 +1624,12 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
*/
static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
{
- int id;
+ int ret;
+ u32 id;
- mutex_lock(&core_lock);
- id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
- mutex_unlock(&core_lock);
- if (WARN(id < 0, "couldn't get idr"))
- return id == -ENOSPC ? -EBUSY : id;
+ ret = xa_alloc(&i2c_adapter_xa, &id, adap, XA_LIMIT(adap->nr, adap->nr), GFP_KERNEL);
+ if (WARN(ret < 0, "couldn't get xa: error %d", ret))
+ return ret;
return i2c_register_adapter(adap);
}
@@ -1656,20 +1651,19 @@ static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
int i2c_add_adapter(struct i2c_adapter *adapter)
{
struct device *dev = &adapter->dev;
- int id;
+ int ret;
+ u32 id;
- id = of_alias_get_id(dev->of_node, "i2c");
- if (id >= 0) {
- adapter->nr = id;
+ ret = of_alias_get_id(dev->of_node, "i2c");
+ if (ret >= 0) {
+ adapter->nr = ret;
return __i2c_add_numbered_adapter(adapter);
}
- mutex_lock(&core_lock);
- id = idr_alloc(&i2c_adapter_idr, adapter,
- __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
- mutex_unlock(&core_lock);
- if (WARN(id < 0, "couldn't get idr"))
- return id;
+ ret = xa_alloc(&i2c_adapter_xa, &id, adapter,
+ XA_LIMIT(__i2c_first_dynamic_bus_num, UINT_MAX), GFP_KERNEL);
+ if (WARN(ret < 0, "couldn't get xa, error %d", ret))
+ return ret;
adapter->nr = id;
@@ -1761,9 +1755,7 @@ void i2c_del_adapter(struct i2c_adapter *adap)
struct i2c_client *client, *next;
/* First make sure that this adapter was ever added */
- mutex_lock(&core_lock);
- found = idr_find(&i2c_adapter_idr, adap->nr);
- mutex_unlock(&core_lock);
+ found = xa_load(&i2c_adapter_xa, adap->nr);
if (found != adap) {
pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
return;
@@ -1817,9 +1809,7 @@ void i2c_del_adapter(struct i2c_adapter *adap)
wait_for_completion(&adap->dev_released);
/* free bus id */
- mutex_lock(&core_lock);
- idr_remove(&i2c_adapter_idr, adap->nr);
- mutex_unlock(&core_lock);
+ xa_erase(&i2c_adapter_xa, adap->nr);
/* Clear the device structure in case this adapter is ever going to be
added again */
@@ -2607,17 +2597,13 @@ struct i2c_adapter *i2c_get_adapter(int nr)
struct i2c_adapter *adapter;
mutex_lock(&core_lock);
- adapter = idr_find(&i2c_adapter_idr, nr);
- if (!adapter)
- goto exit;
-
- if (try_module_get(adapter->owner))
+ adapter = xa_load(&i2c_adapter_xa, nr);
+ if (adapter && try_module_get(adapter->owner))
get_device(&adapter->dev);
else
adapter = NULL;
-
- exit:
mutex_unlock(&core_lock);
+
return adapter;
}
EXPORT_SYMBOL(i2c_get_adapter);
--
2.51.0
reply other threads:[~2025-09-14 20:10 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=09c81521-1444-4c6d-8ded-4318c95d6bdd@gmail.com \
--to=hkallweit1@gmail.com \
--cc=linux-i2c@vger.kernel.org \
--cc=wsa+renesas@sang-engineering.com \
/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;
as well as URLs for NNTP newsgroup(s).