* [PATCH] staging: greybus: uart: convert from IDR to XArray
@ 2026-11-18 16:01 Philipp Prell
0 siblings, 0 replies; 3+ messages in thread
From: Philipp Prell @ 2026-11-18 16:01 UTC (permalink / raw)
To: dtwlin, johan, elder, gregkh
Cc: greybus-dev, linux-staging, linux-kernel, Philipp Prell
The IDR API is deprecated in favour of XArray, see
Documentation/core-api/idr.rst. Convert the
minor table accordingly.
XA_LIMIT is inclusive, so the upper bound becomes
GB_NUM_MINORS - 1 in alloc_minor(). xa_alloc()
returns -EBUSY instead of -ENOSPC when full;
adjust the check in gb_uart_probe().
table_lock is kept: it prevents the entry from
being freed, while get_gb_by_minor() uses it,
whereas the XArray lock only protects the array
itself.
Signed-off-by: Philipp Prell <philipp.prell@icloud.com>
---
Compile-tested only (W=1, C=1); I do not have the Greybus hardware.
An AI assistant (Claude) was used to review my changes and to point
out bugs in earlier drafts and to help with the wording of the commit message.
The code changes were written by me.
drivers/staging/greybus/uart.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index e1b73db6a17c..ff33991562f5 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -22,7 +22,7 @@
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
-#include <linux/idr.h>
+#include <linux/xarray.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/kfifo.h>
@@ -66,7 +66,7 @@ struct gb_tty {
};
static struct tty_driver *gb_tty_driver;
-static DEFINE_IDR(tty_minors);
+static DEFINE_XARRAY_ALLOC(tty_minors);
static DEFINE_MUTEX(table_lock);
static int gb_uart_receive_data_handler(struct gb_operation *op)
@@ -341,7 +341,7 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
struct gb_tty *gb_tty;
mutex_lock(&table_lock);
- gb_tty = idr_find(&tty_minors, minor);
+ gb_tty = xa_load(&tty_minors, minor);
if (gb_tty) {
mutex_lock(&gb_tty->mutex);
if (gb_tty->disconnected) {
@@ -358,13 +358,18 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
static int alloc_minor(struct gb_tty *gb_tty)
{
- int minor;
+ u32 minor;
+ int ret;
mutex_lock(&table_lock);
- minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
+ ret = xa_alloc(&tty_minors, &minor, gb_tty,
+ XA_LIMIT(0, GB_NUM_MINORS - 1), GFP_KERNEL);
mutex_unlock(&table_lock);
- if (minor >= 0)
- gb_tty->minor = minor;
+
+ if (ret)
+ return ret;
+
+ gb_tty->minor = minor;
return minor;
}
@@ -374,7 +379,7 @@ static void release_minor(struct gb_tty *gb_tty)
gb_tty->minor = 0; /* Maybe should use an invalid value instead */
mutex_lock(&table_lock);
- idr_remove(&tty_minors, minor);
+ xa_erase(&tty_minors, minor);
mutex_unlock(&table_lock);
}
@@ -860,7 +865,7 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
minor = alloc_minor(gb_tty);
if (minor < 0) {
- if (minor == -ENOSPC) {
+ if (minor == -EBUSY) {
dev_err(&gbphy_dev->dev,
"no more free minor numbers\n");
retval = -ENODEV;
@@ -991,7 +996,7 @@ static void gb_tty_exit(void)
{
tty_unregister_driver(gb_tty_driver);
tty_driver_kref_put(gb_tty_driver);
- idr_destroy(&tty_minors);
+ xa_destroy(&tty_minors);
}
static const struct gbphy_device_id gb_uart_id_table[] = {
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.56.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH] staging: greybus: uart: convert from IDR to XArray
@ 2026-08-10 14:10 Dmitry Priz
2026-08-25 15:35 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Priz @ 2026-08-10 14:10 UTC (permalink / raw)
To: dtwlin, johan, elder, gregkh
Cc: greybus-dev, linux-staging, linux-kernel, Dmitry Priz
IDR is deprecated in favor of XArray, which manages its own
internal locking and provides a more consistent allocation API.
Convert tty_minors from an IDR to an allocating XArray. Update
the error check in gb_uart_probe() from -ENOSPC to -EBUSY, since
that is the error xa_alloc() returns when the allocation range
is exhausted, unlike idr_alloc() which returned -ENOSPC.
Build-tested and verified via boot in QEMU: the module loads,
registers with the greybus core, and unloads cleanly.
Signed-off-by: Dmitry Priz <dimahtms@gmail.com>
---
drivers/staging/greybus/uart.c | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index 7d060b4cd33d..edd6b0e25c8c 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -22,7 +22,7 @@
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
-#include <linux/idr.h>
+#include <linux/xarray.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/kfifo.h>
@@ -67,7 +67,7 @@ struct gb_tty {
};
static struct tty_driver *gb_tty_driver;
-static DEFINE_IDR(tty_minors);
+static DEFINE_XARRAY_ALLOC(tty_minors);
static DEFINE_MUTEX(table_lock);
static int gb_uart_receive_data_handler(struct gb_operation *op)
@@ -342,7 +342,7 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
struct gb_tty *gb_tty;
mutex_lock(&table_lock);
- gb_tty = idr_find(&tty_minors, minor);
+ gb_tty = xa_load(&tty_minors, minor);
if (gb_tty) {
mutex_lock(&gb_tty->mutex);
if (gb_tty->disconnected) {
@@ -359,14 +359,19 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
static int alloc_minor(struct gb_tty *gb_tty)
{
- int minor;
+ u32 id;
+ int ret;
mutex_lock(&table_lock);
- minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
+ ret = xa_alloc(&tty_minors, &id, gb_tty, XA_LIMIT(0, GB_NUM_MINORS - 1), GFP_KERNEL);
mutex_unlock(&table_lock);
- if (minor >= 0)
- gb_tty->minor = minor;
- return minor;
+
+ if (ret < 0)
+ return ret;
+
+ gb_tty->minor = id;
+
+ return id;
}
static void release_minor(struct gb_tty *gb_tty)
@@ -375,7 +380,7 @@ static void release_minor(struct gb_tty *gb_tty)
gb_tty->minor = 0; /* Maybe should use an invalid value instead */
mutex_lock(&table_lock);
- idr_remove(&tty_minors, minor);
+ xa_erase(&tty_minors, minor);
mutex_unlock(&table_lock);
}
@@ -854,7 +859,7 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
minor = alloc_minor(gb_tty);
if (minor < 0) {
- if (minor == -ENOSPC) {
+ if (minor == -EBUSY) {
dev_err(&gbphy_dev->dev,
"no more free minor numbers\n");
retval = -ENODEV;
@@ -984,7 +989,7 @@ static void gb_tty_exit(void)
{
tty_unregister_driver(gb_tty_driver);
tty_driver_kref_put(gb_tty_driver);
- idr_destroy(&tty_minors);
+ xa_destroy(&tty_minors);
}
static const struct gbphy_device_id gb_uart_id_table[] = {
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] staging: greybus: uart: convert from IDR to XArray
2026-08-10 14:10 Dmitry Priz
@ 2026-08-25 15:35 ` Greg KH
0 siblings, 0 replies; 3+ messages in thread
From: Greg KH @ 2026-08-25 15:35 UTC (permalink / raw)
To: Dmitry Priz
Cc: dtwlin, johan, elder, greybus-dev, linux-staging, linux-kernel
On Mon, Aug 10, 2026 at 05:10:43PM +0300, Dmitry Priz wrote:
> IDR is deprecated in favor of XArray, which manages its own
> internal locking and provides a more consistent allocation API.
Which is fine, but:
> Convert tty_minors from an IDR to an allocating XArray. Update
> the error check in gb_uart_probe() from -ENOSPC to -EBUSY, since
> that is the error xa_alloc() returns when the allocation range
> is exhausted, unlike idr_alloc() which returned -ENOSPC.
This isn't needed at all. idr is just fine for a minor number
allocation, an xarray is total overkill.
Sorry, but this keeps coming up on the list, what tool made you want to
do this conversion?
> Build-tested and verified via boot in QEMU: the module loads,
> registers with the greybus core, and unloads cleanly.
But you don't actually test it such that this codepath gets used, right?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-18 16:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 16:01 [PATCH] staging: greybus: uart: convert from IDR to XArray Philipp Prell
-- strict thread matches above, loose matches on Subject: below --
2026-08-10 14:10 Dmitry Priz
2026-08-25 15:35 ` Greg KH
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®