linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: core: Switch adapter number assignment from idr to xarray
@ 2025-09-14 20:10 Heiner Kallweit
  0 siblings, 0 replies; only message in thread
From: Heiner Kallweit @ 2025-09-14 20:10 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c@vger.kernel.org

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2025-09-14 20:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-14 20:10 [PATCH] i2c: core: Switch adapter number assignment from idr to xarray Heiner Kallweit

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).