From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796136596; b=tipl08/h8B8yjVZwMjrd7tV+ZPVxDYMUFHtsgvukOGzWwG0xdwoy5tDZF43mITxZQAqv 1B1kfL2mTJfSR5CP9VbpgbZeTFCel36iZZBAEdeYy1+RCfLXLEEvTO3O6xKB5twsZ/H6n KK7sbZxoo1tev3gMX/LUywl6wJEw1qh0GiOMs3j8Kqtg299uqPd4UQWMYU+CP9l0HNmsl 4l09V1Kfz4gr+3Gcjptdt3o1JMWRxdrOuQk6wroN8J5OVqr4pZyxZ3q26E3hhFcmpMk8X 7FkDAlFU+upA+JjB7hR/ANHdhQuzCxp9R9lhbjpIajg1UOwr4dVzRX5uD8oH9WS4b1g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796136596; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=QPrgx2F/gs9CmZEGRwsmz4Wz1W6IWfIt+rdR4i2o92U=; b=BxQJ1wg7n0CXfiAiAAK560D/1lDTYs4jXzFnMl7PdgxaACvutU51QdpECrKxRcGwyXMn YskK9Ciy/ukEFsGZhd2p8xzitOrrh+evrEhDiqCin4d3GeFh53QNNYxEJ3f0AXosr7ObA NKopvAVzRXBBqeSMm7PP5VCzB7HHjwybMGkUWWkGkr7Uk6L9GBQtl0oPIhFOOsqQ+nxkg w52hkYdt8oSvTs9/jLHrtJoaSQZSnZMtEppIk1Jn+cGqpzXyg4ZsMBN0OZoxMVJcK5mFd CPi7oQErhl6nbKKfiMxIDiQRocW4oEjsDHAhvokHdomP16HyQLCbU4F82AgyHqYd5YA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=m3r54KHu; arc=none smtp.remote-ip=209.85.210.182 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6108A606A32F for ; Tue, 1 Dec 2026 14:49:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6108A606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=m3r54KHu Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-8a052aff9e9so8451249b3a.0 for ; Tue, 01 Dec 2026 06:49:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796136594; x=1796741394; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=QPrgx2F/gs9CmZEGRwsmz4Wz1W6IWfIt+rdR4i2o92U=; b=m3r54KHuE1V6Dhi/LauJ0sGPpaIBMWHmVEt7LQN5T9ScaW0u0/hObHwhHhwvh6JqM+ XGtIsXp3n4KipHVfs6TfxJz/SoYG88xGnfDNNoyVFrpnmmnET0pCgHbDcmVn9RrWvH3k lntgeeX9aSdYICiMvRoCHB2GSBlJ+3IrszeYZYz/qne4mrY/5LtVMTj+twtFhrsNhb99 qxOq/UTU1/peesW88TPeiQKjaClUvdt+eAVD7G99DQvBLRL07KGKKwKn0yFvhrEZf5fa W6Y72wQ0NaRzOKmk3hVWxVtjpWag1D3/Bxuf7JWtbBD4vb8L2+quIyxpJ/JFIqyg+4UG mwDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796136594; x=1796741394; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QPrgx2F/gs9CmZEGRwsmz4Wz1W6IWfIt+rdR4i2o92U=; b=D3eQtvEc32hwMqPy6EHBE1Oktdxj7l+ul6QwpNOjeYutUx36J8cEORh2mTOCb+06cT d+V9ypHt3GoyHiQWULlACYIhBe9B85rCF5sm1kU35EcGOetbuBbBpfWS1o517K5OPJC9 WapJrNU/60qe7ugErOuzzPo+AkGa03qoQDKdpB63QNCSJGFQMTjI9z2miUgCd48+bQxM 2pVrRBFxsOAdLEwSIl6oNfAH5hhCfnNJeF52WrJOfbBXtRH7KVZhJ7UVXClpwX3BKQVc cmyV+UvZxUx2fxZWZakB3G83Jnth9bl0LOCcviNBbx+ZvbrrpYTkNxBoXpN1K6YDoQBa Mf8Q== X-Gm-Message-State: APuU4Z8keizRCYNvAIOHunwbgBldAAek6UoL+jUmITdzRQf/YkF9YDI3 YE1aI2xkcJA5tMuR8lQEbIQCpiwMKDIK3Xk0ug+G47llkGmC76ivvzSifVn2qJU3 X-Gm-Gg: AZFdTZex4a2R0a2Arx73wO9BY1bs1zwnUBxi38TZkNd9xXdUFTYfEektwZ3sbaStM1I +mwNP6Eld5IWb8jwtCmZ0H1uEGs4Ak/eDGhSc/mJfITE3GdDbk4arnOCjwnYwi9zfC52yNc0G1v U+H4mAOSd2Jt0wV33VmXYyH14Ie0+9GmuLjJeQUbjMhXNpXOYWs8knZYL7wTT/M1IOgYdFBrwWZ JAGqRjNU2Gk8sEJ9O+477h47YQH8Jr6HYB3HNmPwWl3/Ya10+akqLX+Jhx1jpYpqnG52OgtCLAP RPOAyyL3vqDBlHGbOwUoLxjoxdO7HOspYwSP1jdq789ARXPnmOf5nUVjesRVKo8eoBVyVD/fOJs 0/R9TWaEewJGChekwo2ScGZ4gnufwbsVziO5FgM3m6nGJmT4VdfKEsejz49qMBkeymJUOKLt4C8 SyCm+aEiUFv3vRLq0cpr8S/6QC3AQ97afqxBrugzjUziAiCmqHYaL8fn2UOeT4FBVBSY+AUvAhG GUqvHBKVrRlq4fXqI8VpZ7KdtJWo0EsUXtHbHTzy6v0nStkIAWNijnliub7O0Txg0fHi08TMB/K WNLEXU3ZOq9Wbc3VLPLXE12j7tNAWQNmM7Zi/2Ws7Q+GOfl640JGyJZhmEatrV1udX9JiQhszf7 aenRgWQs= X-Received: by 2002:a05:6a21:7e83:b0:3e2:b561:2b23 with SMTP id adf61e73a8af0-4014c250045mr32488436637.18.1796136594116; Tue, 01 Dec 2026 06:49:54 -0800 (PST) Received: from DESKTOP-TPPBPA1 (36-226-254-86.dynamic-ip.hinet.net. [36.226.254.86]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde9bb05981sm14625717a12.26.2026.12.01.06.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:49:53 -0800 (PST) From: Lin Tzu Yang To: Linus Walleij , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] gpio: virtuser: refuse the atomic attributes for lines that can sleep Date: Tue, 1 Dec 2026 22:49:50 +0800 Message-ID: <20261201144950.901806-1-rongh7426@gmail.com> X-Mailer: git-send-email 2.51.1.windows.1 X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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