* [PATCH] gpio: virtuser: refuse the atomic attributes for lines that can sleep
@ 2026-12-01 14:49 Lin Tzu Yang
0 siblings, 0 replies; only message in thread
From: Lin Tzu Yang @ 2026-12-01 14:49 UTC (permalink / raw)
To: Linus Walleij, Bartosz Golaszewski; +Cc: linux-gpio, linux-kernel
The *_atomic debugfs attributes run the non-sleeping GPIO calls from a
hard irq_work, but they are created for every line, also for lines on
chips that can sleep. Using them on such a line, for example one of
gpio-sim, makes gpiolib warn and the chip driver sleep in hard
interrupt context. Reading value_atomic of a gpio-sim line gives:
WARNING: drivers/gpio/gpiolib.c:3689 at gpiod_get_value+0xb4/0x128
BUG: sleeping function called from invalid context at kernel/locking/mutex.c:623
[ BUG: Invalid wait context ]
cat/108 is trying to lock:
(mutex_){+.+.}-{4:4}, at: gpio_sim_get+0x30/0xb4
...
gpio_sim_get+0x30/0xb4
gpiochip_get+0x78/0x150
gpiod_get_raw_value_commit+0x10c/0x2ac
gpiod_get_value+0x74/0x128
gpio_virtuser_get_value_atomic+0x34/0x9c
irq_work_single+0xb0/0x1e0
and value_atomic writes and values_atomic reads and writes hit the
matching warnings in gpiod_set_value() and the array helpers.
Return -EOPNOTSUPP from the atomic variants when a line can sleep,
before queueing the irq_work. Lines on chips that don't sleep behave
as before.
Fixes: 91581c4b3f29 ("gpio: virtuser: new virtual testing driver for the GPIO API")
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
Found while writing the gpio-virtuser selftest I sent earlier today,
which leaves the atomic attributes out for this reason. Returning an
error seemed less surprising than not creating the files for such
lines, but I can change it if you prefer the latter.
Tested in a QEMU/KVM arm64 guest (configfs-next 2e0fad9d2eae with my
two other gpio-virtuser fixes from today, KASAN and lockdep), with a
consumer of two gpio-sim lines and one of two lines of the PL061 of
the virt machine, which doesn't sleep:
- gpio-sim: before, value_atomic and values_atomic gave 9 warnings and
lockdep reports; after, all six atomic reads and writes fail with
EOPNOTSUPP and nothing is logged. The non-atomic attributes still
work.
- PL061: value_atomic and values_atomic work the same before and
after, without reports.
- the gpio-virtuser selftest still passes.
Not addressed here: writing direction_atomic of a PL061 line also
gives a lockdep "Invalid wait context" report, with or without this
patch, because gpiod_direction_output() takes gdev->line_state_lock,
an rwlock_t, from the hard irq_work. I did not look into whether the
direction calls are meant to be usable from atomic context at all.
This patch was written with the help of an AI coding assistant
(Claude). I ran the tests above and reviewed the change.
drivers/gpio/gpio-virtuser.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/drivers/gpio/gpio-virtuser.c b/drivers/gpio/gpio-virtuser.c
index 5bf5d891c4..0a988825db 100644
--- a/drivers/gpio/gpio-virtuser.c
+++ b/drivers/gpio/gpio-virtuser.c
@@ -115,6 +115,22 @@ static void gpio_virtuser_dbgfs_emit_value_array(char *buf,
buf[i++] = '\n';
}
+/*
+ * The *_atomic attributes call the GPIO API from hard interrupt context,
+ * which only works for lines whose chip doesn't sleep.
+ */
+static bool gpio_virtuser_descs_cansleep(struct gpio_descs *descs)
+{
+ unsigned int i;
+
+ for (i = 0; i < descs->ndescs; i++) {
+ if (gpiod_cansleep(descs->desc[i]))
+ return true;
+ }
+
+ return false;
+}
+
static void gpio_virtuser_get_value_array_atomic(struct irq_work *work)
{
struct gpio_virtuser_irq_work_context *ctx =
@@ -136,6 +152,9 @@ static int gpio_virtuser_get_array_value(struct gpio_descs *descs,
descs->desc,
descs->info, values);
+ if (gpio_virtuser_descs_cansleep(descs))
+ return -EOPNOTSUPP;
+
gpio_virtuser_init_irq_work_context(&ctx);
ctx.work = IRQ_WORK_INIT_HARD(gpio_virtuser_get_value_array_atomic);
ctx.descs = descs;
@@ -214,6 +233,9 @@ static int gpio_virtuser_set_array_value(struct gpio_descs *descs,
if (!atomic)
return gpiod_multi_set_value_cansleep(descs, values);
+ if (gpio_virtuser_descs_cansleep(descs))
+ return -EOPNOTSUPP;
+
gpio_virtuser_init_irq_work_context(&ctx);
ctx.work = IRQ_WORK_INIT_HARD(gpio_virtuser_set_value_array_atomic);
ctx.descs = descs;
@@ -343,6 +365,8 @@ static ssize_t gpio_virtuser_direction_do_read(struct file *file,
if (!atomic)
dir = gpiod_get_direction(desc);
+ else if (gpiod_cansleep(desc))
+ return -EOPNOTSUPP;
else
dir = gpio_virtuser_get_direction_atomic(desc);
if (dir < 0)
@@ -375,6 +399,9 @@ static int gpio_virtuser_set_direction_atomic(struct gpio_desc *desc,
{
struct gpio_virtuser_irq_work_context ctx;
+ if (gpiod_cansleep(desc))
+ return -EOPNOTSUPP;
+
gpio_virtuser_init_irq_work_context(&ctx);
ctx.work = IRQ_WORK_INIT_HARD(gpio_virtuser_do_set_direction_atomic);
ctx.desc = desc;
@@ -519,6 +546,9 @@ static int gpio_virtuser_value_atomic_get(void *data, u64 *val)
struct gpio_virtuser_line_data *ld = data;
struct gpio_virtuser_irq_work_context ctx;
+ if (gpiod_cansleep(ld->ad.desc))
+ return -EOPNOTSUPP;
+
gpio_virtuser_init_irq_work_context(&ctx);
ctx.work = IRQ_WORK_INIT_HARD(gpio_virtuser_get_value_atomic);
ctx.desc = ld->ad.desc;
@@ -550,6 +580,9 @@ static int gpio_virtuser_value_atomic_set(void *data, u64 val)
if (val > 1)
return -EINVAL;
+ if (gpiod_cansleep(ld->ad.desc))
+ return -EOPNOTSUPP;
+
gpio_virtuser_init_irq_work_context(&ctx);
ctx.work = IRQ_WORK_INIT_HARD(gpio_virtuser_set_value_atomic);
ctx.desc = ld->ad.desc;
--
2.47.3
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-01 14:49 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 14:49 [PATCH] gpio: virtuser: refuse the atomic attributes for lines that can sleep Lin Tzu Yang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox