mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aviv Levinson <levinson.aviv@gmail.com>
To: dtwlin@gmail.com, johan@kernel.org, elder@kernel.org,
	gregkh@linuxfoundation.org
Cc: greybus-dev@lists.linaro.org, linux-staging@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	Aviv Levinson <levinson.aviv@gmail.com>
Subject: [PATCH] staging: greybus: uart: replace IDR with XArray
Date: Wed, 11 Nov 2026 20:23:30 +0200	[thread overview]
Message-ID: <20261111182330.77824-1-levinson.aviv@gmail.com> (raw)

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


             reply	other threads:[~2026-11-11 18:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 18:23 Aviv Levinson [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-06-16 19:07 Jack Lee
2026-06-17  1:26 ` Greg KH

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=20261111182330.77824-1-levinson.aviv@gmail.com \
    --to=levinson.aviv@gmail.com \
    --cc=dtwlin@gmail.com \
    --cc=elder@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=greybus-dev@lists.linaro.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    /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

all inboxes | Powered by JetHome®