gpiolib: clear the event clock flags when a line is released
PATCH 1/1 gpiolib: clear the event clock flags when a line is released
gpiod_free_commit() clears the flags that a request sets on a line, but not GPIOD_FLAG_EVENT_CLOCK_REALTIME and GPIOD_FLAG_EVENT_CLOCK_HTE, which gpio_v2_line_config_flags_to_desc_flags() sets from the uAPI v2 line config. Once a line requested with GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME or GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE is released, GPIO_V2_GET_LINEINFO_IOCTL and the line info changed events, the "released" one included, keep reporting that event clock for the free line:
GPIO_V2_GET_LINE_IOCTL, line 1, input, event clock realtime: ok close()
GPIO_V2_GET_LINEINFO_IOCTL, line 1: input, event clock realtime
The flags are set before edge detection is set up, so a request that fails in hte_ts_get(), for instance because no HTE provider handles the line, leaves GPIOD_FLAG_EVENT_CLOCK_HTE behind as well. The next uAPI v2 request rewrites both flags, but requests through uAPI v1, sysfs exports and in-kernel consumers do not, and the line info then shows the event clock of an earlier user for them too.
Clear both flags together with the other flags of the request.
Reproduced under QEMU with a test GPIO chip: after a released request with the realtime event clock, and on a kernel with CONFIG_HTE=y but no HTE provider after a failed request with the HTE event clock, the line info of the free line still reports that event clock. With this change it reports none.
--- drivers/gpio/gpiolib.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpio/gpiolib.c +2 -0
@@ -2679,6 +2679,8 @@ void gpiod_free_commit(struct gpio_desc *desc)clear_bit (GPIOD_FLAG_BIAS_DISABLE , &flags);clear_bit (GPIOD_FLAG_EDGE_RISING , &flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &flags); +clear_bit (GPIOD_FLAG_EVENT_CLOCK_REALTIME , &flags); +clear_bit (GPIOD_FLAG_EVENT_CLOCK_HTE , &flags);clear_bit (GPIOD_FLAG_IS_HOGGED , &flags);#ifdef CONFIG_OF_DYNAMIC WRITE_ONCE (desc->hog,NULL );
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] gpio: data race in gpiod_free_commit() causes debounce corruption --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] gpio: data race in gpiod_free_commit() causes debounce corruption --
commit 9516d6cf1c2a072b1bec8e797ac66548d0323b06
Author: Adrian Ambrozewicz <[email protected]>
gpiolib: clear the event clock flags when a line is released
This commit ensures that the GPIOD_FLAG_EVENT_CLOCK_REALTIME and GPIOD_FLAG_EVENT_CLOCK_HTE flags are cleared when a GPIO line is released. It modifies gpiod_free_commit() to clear these flags alongside others so that they do not leak into the line info for a freed line.
drivers/gpio/gpiolib.c void gpiod_free_commit(struct gpio_desc *desc) · line 2686
Quoted 13 lines
drivers/gpio/gpiolib.c +2
-0@@ -2679,6 +2679,8 @@ void gpiod_free_commit(struct gpio_desc *desc)clear_bit (GPIOD_FLAG_BIAS_DISABLE , &flags);clear_bit (GPIOD_FLAG_EDGE_RISING , &flags);clear_bit (GPIOD_FLAG_EDGE_FALLING , &flags); +clear_bit (GPIOD_FLAG_EVENT_CLOCK_REALTIME , &flags); +clear_bit (GPIOD_FLAG_EVENT_CLOCK_HTE , &flags);clear_bit (GPIOD_FLAG_IS_HOGGED , &flags);#ifdef CONFIG_OF_DYNAMIC WRITE_ONCE (desc->hog,NULL );
[Severity: High]
Does this code cause debounce corruption by clearing ownership too early?
In gpiod_free_commit(), the ownership synchronization token (GPIOD_FLAG_REQUESTED) is cleared in desc->flags via a WRITE_ONCE() before the function finishes cleaning up ancillary state (debounce_period_us) and emitting the GPIO_V2_LINE_CHANGED_RELEASED notification:
drivers/gpio/gpiolib.c:gpiod_free_commit() {
...
WRITE_ONCE(desc->flags, flags);
#ifdef CONFIG_GPIO_CDEV
WRITE_ONCE(desc->debounce_period_us, 0);
#endif
gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_RELEASED);
...
}This allows a concurrent request (e.g., from GPIO_V2_GET_LINE_IOCTL via linereq_create()) to successfully acquire the GPIO line:
drivers/gpio/gpiolib.c:gpiod_request_commit() {
...
if (test_and_set_bit(GPIOD_FLAG_REQUESTED, &desc->flags))
return -EBUSY;
...
}It can then configure a new debounce_period_us:
drivers/gpio/gpiolib-cdev.c:edge_detector_setup() {
...
WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
...
}The original freeing thread then resumes and overwrites the newly requested line's debounce_period_us with 0. This completely breaks software debouncing, causing a 0-delay software debounce that will trigger its delayed work immediately on every hardware edge. This could lead to an interrupt storm and CPU soft lockup.
Additionally, this sends an out-of-order RELEASED notification to userspace for a line that is currently fully requested and active.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · On Mon, Oct 12, 2026 at 02:05:49PM +0000, [email protected] wrote:
In
gpiod_free_commit(), the ownership synchronization token (GPIOD_FLAG_REQUESTED) is cleared indesc->flagsvia aWRITE_ONCE()before the function finishes cleaning up ancillary state (debounce_period_us) and emitting theGPIO_V2_LINE_CHANGED_RELEASEDnotification:
This is real, and older than this patch: once the cleared GPIOD_FLAG_REQUESTED is visible, a new request can set desc->debounce_period_us in edge_detector_setup() before the old one resets it to 0, and the debounce code reads the period from there.
I'll send a fix with v2 of the chip-removal series, which touches the same function: reset the period first, then write the flags with release ordering, pairing with the test_and_set_bit() in gpiod_request_commit(), so that a new owner sees the reset. The RELEASED notification can still arrive after the new request's REQUESTED one. Moving it before the flags update would report the line as still requested in the RELEASED event, so the fix will describe that rather than reorder it.
Adrian
Bartosz Golaszewski
maintainerQuoted 11 lines · Adrian Ambrozewicz
Quoted 4 lines · On Mon, Oct 12, 2026 at 02:05:49PM +0000, [email protected] wrote:
In
gpiod_free_commit(), the ownership synchronization token (GPIOD_FLAG_REQUESTED) is cleared indesc->flagsvia aWRITE_ONCE()before the function finishes cleaning up ancillary state (debounce_period_us) and emitting theGPIO_V2_LINE_CHANGED_RELEASEDnotification:This is real, and older than this patch: once the cleared
GPIOD_FLAG_REQUESTEDis visible, a new request can setdesc->debounce_period_usinedge_detector_setup()before the old one resets it to 0, and the debounce code reads the period from there.
Quoted 9 lines
I'll send a fix with v2 of the chip-removal series, which touches the same function: reset the period first, then write the flags with release ordering, pairing with the
test_and_set_bit()ingpiod_request_commit(), so that a new owner sees the reset. The RELEASED notification can still arrive after the new request's REQUESTED one. Moving it before the flags update would report the line as still requested in the RELEASED event, so the fix will describe that rather than reorder it.
Will you resend this together with that v2? Or should I take it independently?
Bartosz
Quoted 1 line · Bartosz Golaszewski
Will you resend this together with that v2? Or should I take it independently?
Please take it independently. It doesn't depend on the removal series and still applies to gpio/for-current and gpio/for-next.
The debounce fix didn't go into v2 in the end: Kent asked to keep fixes for existing problems out of the removal series, and the fix builds on the gpiod_free_commit() rework in patch 2. I'll send it once the series is applied.
Adrian
Bartosz Golaszewski
AppliedQuoted 10 lines · Adrian Ambrozewicz
gpiod_free_commit()clears the flags that a request sets on a line, but notGPIOD_FLAG_EVENT_CLOCK_REALTIMEandGPIOD_FLAG_EVENT_CLOCK_HTE, whichgpio_v2_line_config_flags_to_desc_flags()sets from the uAPI v2 line config. Once a line requested withGPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIMEorGPIO_V2_LINE_FLAG_EVENT_CLOCK_HTEis released,GPIO_V2_GET_LINEINFO_IOCTLand the line info changed events, the "released" one included, keep reporting that event clock for the free line:[...]
Applied, thanks!
[1/1] gpiolib: clear the event clock flags when a line is released
https://git.kernel.org/brgl/c/d35f61581ee67f8d4d58c325bac813cea528b6b9
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>