From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796136595; b=fdpOhQ5rNNyQEIHVps1hlQvb7LnFxvKJ5Jl50EYUZo6no/AOI81hEOh+xW73DA+y25Vu qb5fmfL/GqhM982zpyAsBh1v0cavMFOJWa4ZtvCbf4AIlychcQ8gBGimqsw93TDmX9/gX HG0bGmZE5T79HyPiLl2mzJOuWgNqrI3KiCwCfgz7kqlFwW0/iq/edE6DAqKYaodzXuIbR 2j7aeFssignLSKW+ABEX5U+EQZiWMvSfVudQF0n4eiyuxRBUjTf0QtxdpSMU2Z8MTe/Q8 ZhdBYe2QYEA3flpEcqmtEbHeQLsXljR9QmDJbTKHmO9tMiHX/CaAlwlNToS/oXZD05g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796136595; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=Bh8CEmaTntRewG9HN4//rh4E//6UsoIt5+MF67yhRaP3SUHJlWAYU2Hkvc4S0DbPz7my oN9Jr0xSBuMOG5gtB8kcO78KoT1tJP+vZ3ZJkgey1NNsNMb3HC8iEY1YKSwwYu+x3g9T0 6DwpW10zeXv/henvKP3hpasDymfZqdvTSei/wMBuMxoyg18AfXUN/3GNpN6KRb5Wxicqr VvOHlZZ9FTJdbkY/6BuqcxaVtgGKUUadMdEHu7LB+HQo+b69P2ql5zPFY0W61Ni0YfbgM ANpIR3zGsi5wQzGJM34cK33pmZDoHSFwJar5g9w/VM1+x1vUIRuzJ8h3lyhaSiQxr2Q== 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.173 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 5E0C7606A32D for ; Tue, 1 Dec 2026 14:49:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E0C7606A32D 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-f173.google.com with SMTP id d2e1a72fcca58-8a9f9473029so8880277b3a.2 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=U8OIsd3CGNtHesPFh44uIl3Al67JdabiIYdgasaGToL4oyF8Zq1dYRWOj+6iJRtuOf tZTFY7625/F4gzlAUtN+tPqLJHI2dxr8axyHiw+LUwLhgMlZiOI5/UuvRgo0DQWQXpRw mO4aHLkh/KYbyLAsiwc1dPLLh5M2XIVbH4GRv7wcGEPh9TQx1ocfkZfr90hXVQrAF912 MXfCW4WBU155wbtpXOiVpd1e4hssGljq2ZQ8l1kPpyBehuX1avTAK7Z5cWWnS40PVsxQ y2JrCCCvShQP7CdSg32FmoZR0CmXkIzwPcvYo16xiVF/Q+20r7nAq6uV7TCP3e7GVbjV JwMQ== X-Forwarded-Encrypted: i=1; AL+hErEHVjzKYiQq+RUxIEQz5cmZRmIeJnN/kq+dqwUeIrk03a5TGZuW8dUBb7lKKnLTq3O4zV6ecXXALzH0l2Q=@vger.kernel.org X-Gm-Message-State: APuU4Z9TD/bBt1bxvlfqIwxnlb7uBy2PJzAqIP+r1+UayrXSThqa5vNt qmXEEr4WPXQo5GeqJ73INarOikCjNvygT4u6vpplvulRmFJFexcfmd75 X-Gm-Gg: AZFdTZeDdo5MIeE2W2vPgk1Sh2oheCaV7kxpCeABZy2uZXFucWeyp848jRy+y4AyZFJ bRfwbBv21ij2WDItEUzP6+J0wvaUmdFDCyPmwiXoVxkueMJycq9c1XNhF3cY38fmT9TuK1RAmry VJUa6Wy5IhDDhSBYB2z+d0GiuWk7FfcaUBMth/qLb/hfd81DBUbcx7C4EravcsEUr72QYWXK7Pa Xr9hxTzPlOhGKMDj4QRvi3BAXwC0A5qEh4L9hHg4+lbCsCx3ASB2Ac4qCu3JVc2UBWpcsczya04 OTVwREuPAWiLF29XDGGot5zaMzWnx2gDMiKFvHgTD8ZDWw+cJJwO7VueLEyYi7MQSOMbxtq7D6s db9nceFj8ApOIZAz4y9gF+/6etfAwZW4BCYVmE0DQUhZ2+PMRIs/ddQDQHWEAah4vwIlFe7AxyA eYJFHyfWE3c+jwkbED+gEEAoTDWJ09nvooDnltugREWJheBMcRKm+CU477iryID2RcBQ148UAY0 fnuPe0qhRYpq5Da51u9GeBsuuAfpkPTn42DPvmpn/yxnQ2QNTL5Tjd5oL2sp8X6Yzq35rmFf7Dl 8gSU1ucQr1Hn9jwTDvGa5QNwE6Z+Cu5wCX6aQSCUIa0LafNroONdANC80Kx9b/j7wcPOWYBzsUp 0Ek5Cb6A= 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-kernel@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