* [PATCH] staging: greybus: uart: replace IDR with XArray
@ 2026-11-11 18:23 Aviv Levinson
2026-11-12 12:37 ` Greg KH
0 siblings, 1 reply; 4+ messages in thread
From: Aviv Levinson @ 2026-11-11 18:23 UTC (permalink / raw)
To: dtwlin, johan, elder, gregkh
Cc: greybus-dev, linux-staging, linux-kernel, Aviv Levinson
Migrate the minor number allocation from the legacy IDR API to the
modern XArray API to take advantage of its built-in thread safety.
As part of this migration, the global `table_lock` mutex has been
removed. Mutating operations now rely on XArray's internal spinlocks,
and the lookup logic in get_gb_by_minor() has been updated to use
rcu_read_lock() combined with kref_get_unless_zero() to safely
pin the device reference without blocking.
Signed-off-by: Aviv Levinson <levinson.aviv@gmail.com>
---
drivers/staging/greybus/uart.c | 37 ++++++++++++++++++----------------
1 file changed, 20 insertions(+), 17 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index e1b73db6a17c..2b1a5d4671dd 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,8 +66,7 @@ struct gb_tty {
};
static struct tty_driver *gb_tty_driver;
-static DEFINE_IDR(tty_minors);
-static DEFINE_MUTEX(table_lock);
+static DEFINE_XARRAY_ALLOC(tty_minors);
static int gb_uart_receive_data_handler(struct gb_operation *op)
{
@@ -340,31 +339,37 @@ 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);
+ rcu_read_lock();
+ gb_tty = xa_load(&tty_minors, minor);
+ if (gb_tty) {
+ if (!kref_get_unless_zero(&gb_tty->port.kref))
+ gb_tty = NULL;
+ }
+ rcu_read_unlock();
+
if (gb_tty) {
mutex_lock(&gb_tty->mutex);
if (gb_tty->disconnected) {
mutex_unlock(&gb_tty->mutex);
+ tty_port_put(&gb_tty->port);
gb_tty = NULL;
} else {
- tty_port_get(&gb_tty->port);
mutex_unlock(&gb_tty->mutex);
}
}
- mutex_unlock(&table_lock);
+
return gb_tty;
}
static int alloc_minor(struct gb_tty *gb_tty)
{
- int minor;
+ unsigned int minor;
+ int ret;
- mutex_lock(&table_lock);
- minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
- mutex_unlock(&table_lock);
- if (minor >= 0)
- gb_tty->minor = minor;
+ ret = xa_alloc(&tty_minors, &minor, gb_tty, XA_LIMIT(0, GB_NUM_MINORS - 1), GFP_KERNEL);
+ if (ret)
+ return ret;
+ gb_tty->minor = minor;
return minor;
}
@@ -373,9 +378,7 @@ static void release_minor(struct gb_tty *gb_tty)
int minor = gb_tty->minor;
gb_tty->minor = 0; /* Maybe should use an invalid value instead */
- mutex_lock(&table_lock);
- idr_remove(&tty_minors, minor);
- mutex_unlock(&table_lock);
+ xa_erase(&tty_minors, minor);
}
static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
@@ -991,7 +994,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] 4+ messages in thread* Re: [PATCH] staging: greybus: uart: replace IDR with XArray
2026-11-11 18:23 [PATCH] staging: greybus: uart: replace IDR with XArray Aviv Levinson
@ 2026-11-12 12:37 ` Greg KH
2026-11-12 16:05 ` Aviv Levinson
2026-11-12 16:05 ` [PATCH v2] staging: greybus: uart: convert idr to xarray Aviv Levinson
0 siblings, 2 replies; 4+ messages in thread
From: Greg KH @ 2026-11-12 12:37 UTC (permalink / raw)
To: Aviv Levinson
Cc: dtwlin, johan, elder, greybus-dev, linux-staging, linux-kernel
On Wed, Nov 11, 2026 at 08:23:30PM +0200, Aviv Levinson wrote:
> Migrate the minor number allocation from the legacy IDR API to the
> modern XArray API to take advantage of its built-in thread safety.
But why?
> As part of this migration, the global `table_lock` mutex has been
> removed. Mutating operations now rely on XArray's internal spinlocks,
> and the lookup logic in get_gb_by_minor() has been updated to use
> rcu_read_lock() combined with kref_get_unless_zero() to safely
> pin the device reference without blocking.
How was this found and generated and tested?
And kref_get_unless_zero() is almost always a call you never want to
make.
> Signed-off-by: Aviv Levinson <levinson.aviv@gmail.com>
> ---
> drivers/staging/greybus/uart.c | 37 ++++++++++++++++++----------------
> 1 file changed, 20 insertions(+), 17 deletions(-)
So now there is more code? That feels like a step backwards, don't you
think?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] staging: greybus: uart: replace IDR with XArray
2026-11-12 12:37 ` Greg KH
@ 2026-11-12 16:05 ` Aviv Levinson
2026-11-12 16:05 ` [PATCH v2] staging: greybus: uart: convert idr to xarray Aviv Levinson
1 sibling, 0 replies; 4+ messages in thread
From: Aviv Levinson @ 2026-11-12 16:05 UTC (permalink / raw)
To: Greg KH; +Cc: dtwlin, johan, elder, greybus-dev, linux-staging, linux-kernel
Hi Greg,
Thank you for the review.
I am looking to start contributing to the kernel and thought
drivers/staging/ was a good place to start.
I used the checkpatch.pl tool that flagged the use of the deprecated IDR.
I wrote the patch manually and it was only compile tested.
I understand that the use of kref_get_unless_zero() is not a good
practice, so checked current drivers that implement the use of XArray
(drivers/gpu/drm/drm_drv.c) and tried using similar methods that I am
sending in the v2 patch.
בתאריך יום ה׳, 12 בנוב׳ 2026 ב-14:37 מאת Greg KH
<gregkh@linuxfoundation.org>:
>
> On Wed, Nov 11, 2026 at 08:23:30PM +0200, Aviv Levinson wrote:
> > Migrate the minor number allocation from the legacy IDR API to the
> > modern XArray API to take advantage of its built-in thread safety.
>
> But why?
>
> > As part of this migration, the global `table_lock` mutex has been
> > removed. Mutating operations now rely on XArray's internal spinlocks,
> > and the lookup logic in get_gb_by_minor() has been updated to use
> > rcu_read_lock() combined with kref_get_unless_zero() to safely
> > pin the device reference without blocking.
>
> How was this found and generated and tested?
>
> And kref_get_unless_zero() is almost always a call you never want to
> make.
>
> > Signed-off-by: Aviv Levinson <levinson.aviv@gmail.com>
> > ---
> > drivers/staging/greybus/uart.c | 37 ++++++++++++++++++----------------
> > 1 file changed, 20 insertions(+), 17 deletions(-)
>
> So now there is more code? That feels like a step backwards, don't you
> think?
>
> thanks,
>
> greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] staging: greybus: uart: convert idr to xarray
2026-11-12 12:37 ` Greg KH
2026-11-12 16:05 ` Aviv Levinson
@ 2026-11-12 16:05 ` Aviv Levinson
1 sibling, 0 replies; 4+ messages in thread
From: Aviv Levinson @ 2026-11-12 16:05 UTC (permalink / raw)
To: gregkh
Cc: dtwlin, johan, elder, greybus-dev, linux-staging, linux-kernel,
Aviv Levinson
The legacy IDR API is deprecated in favor of the modern XArray API.
Convert the tty_minors IDR to an XArray.
To prevent locking the entire gb_tty object while loading the minor,
switch IDR to XArray and use the internal XArray's spinlock (xa_lock).
To respect TTY encapsulation, use tty_port_get()
instead of direct kref_get_unless_zero().
Signed-off-by: Aviv Levinson <levinson.aviv@gmail.com>
---
drivers/staging/greybus/uart.c | 27 ++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index 2b1a5d4671dd..054bdc232942 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -339,24 +339,21 @@ static struct gb_tty *get_gb_by_minor(unsigned int minor)
{
struct gb_tty *gb_tty;
- rcu_read_lock();
+ xa_lock(&tty_minors);
gb_tty = xa_load(&tty_minors, minor);
- if (gb_tty) {
- if (!kref_get_unless_zero(&gb_tty->port.kref))
- gb_tty = NULL;
- }
- rcu_read_unlock();
+ if (gb_tty)
+ tty_port_get(&gb_tty->port);
+ xa_unlock(&tty_minors);
- if (gb_tty) {
- mutex_lock(&gb_tty->mutex);
- if (gb_tty->disconnected) {
- mutex_unlock(&gb_tty->mutex);
- tty_port_put(&gb_tty->port);
- gb_tty = NULL;
- } else {
- mutex_unlock(&gb_tty->mutex);
- }
+ if (!gb_tty)
+ return NULL;
+ mutex_lock(&gb_tty->mutex);
+ if (gb_tty->disconnected) {
+ mutex_unlock(&gb_tty->mutex);
+ tty_port_put(&gb_tty->port);
+ return NULL;
}
+ mutex_unlock(&gb_tty->mutex);
return gb_tty;
}
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-12 16:05 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-11 18:23 [PATCH] staging: greybus: uart: replace IDR with XArray Aviv Levinson
2026-11-12 12:37 ` Greg KH
2026-11-12 16:05 ` Aviv Levinson
2026-11-12 16:05 ` [PATCH v2] staging: greybus: uart: convert idr to xarray Aviv Levinson
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®