Skip to contentSkip to conversation

gpiolib: acpi: use BIT_ULL() for u64 mask in address space handler

PATCH Applied Denis Sergeev 5 participants 5 messages Jan 26 – Jan 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: acpi: use BIT_ULL() for u64 mask in address space handler

The BIT() macro uses unsigned long, which is 32 bits on 32-bit architectures. When iterating over GPIO pins with index >= 32, the expression (*value & BIT(i)) causes undefined behavior due to shifting by a value >= type width.

Since 'value' is a pointer to u64, use BIT_ULL() to ensure correct 64-bit mask on all architectures.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 2c4d00cb8fc5 ("gpiolib: acpi: Use BIT() macro to increase readability")

---
The ACPI specification does not define a strict upper bound for the number of GPIO pins in the Pin Table. The value is derived from 16-bit offsets inside the resource descriptor, which theoretically allows far more than 64 pins.

However, the current Linux GPIO ACPI OpRegion handler represents the pin state as a single u64 value, which inherently limits the number of addressable pins to 64. Thus, even though the specification permits larger tables, the existing implementation already assumes a <= 64 pin mask.

This patch fixes undefined behavior in the valid range [32, 63] on 32-bit architectures. Extending support beyond 64 pins would require a different representation (e.g. bitmap) and is outside the scope of this fix.

 drivers/gpio/gpiolib-acpi-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-acpi-core.c +1 -1
index 83dd227dbbec..d42f769eeb11 100644
@@ -1159,7 +1159,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		mutex_unlock(&achip->conn_lock);
 
 		if (function == ACPI_WRITE)
-			gpiod_set_raw_value_cansleep(desc, !!(*value & BIT(i)));
+			gpiod_set_raw_value_cansleep(desc, !!(*value & BIT_ULL(i)));
 		else
 			*value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
 	}
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Denis Sergeev

The BIT() macro uses unsigned long, which is 32 bits on 32-bit architectures. When iterating over GPIO pins with index >= 32, the expression (*value & BIT(i)) causes undefined behavior due to shifting by a value >= type width.

Quoted 7 lines

Since 'value' is a pointer to u64, use BIT_ULL() to ensure correct 64-bit mask on all architectures.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 2c4d00cb8fc5 ("gpiolib: acpi: Use BIT() macro to increase readability")
#2 lore raw
Quoted 10 lines · Mika Westerberg
Quoted 8 lines · Denis Sergeev

The BIT() macro uses unsigned long, which is 32 bits on 32-bit architectures. When iterating over GPIO pins with index >= 32, the expression (*value & BIT(i)) causes undefined behavior due to shifting by a value >= type width.

Since 'value' is a pointer to u64, use BIT_ULL() to ensure correct 64-bit mask on all architectures.

Quoted 6 lines
Quoted 4 lines

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 2c4d00cb8fc5 ("gpiolib: acpi: Use BIT() macro to increase readability")

I'll queue this for v6.19-rc8.

Bart

maintainer
#3 lore raw
Quoted 12 lines · Bartosz Golaszewski
Quoted 10 lines · Mika Westerberg
Quoted 8 lines · Denis Sergeev

The BIT() macro uses unsigned long, which is 32 bits on 32-bit architectures. When iterating over GPIO pins with index >= 32, the expression (*value & BIT(i)) causes undefined behavior due to shifting by a value >= type width.

Since 'value' is a pointer to u64, use BIT_ULL() to ensure correct 64-bit mask on all architectures.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 2c4d00cb8fc5 ("gpiolib: acpi: Use BIT() macro to increase readability")

I'll queue this for v6.19-rc8.

This is half-baked solution... But okay, let's shut up at least the stream of this type of "fixes" in this file.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Denis Sergeev
Quoted 9 lines · Denis Sergeev

The BIT() macro uses unsigned long, which is 32 bits on 32-bit architectures. When iterating over GPIO pins with index >= 32, the expression (*value & BIT(i)) causes undefined behavior due to shifting by a value >= type width.

Since 'value' is a pointer to u64, use BIT_ULL() to ensure correct 64-bit mask on all architectures.

[...]

Applied, thanks!

[1/1] gpiolib: acpi: use BIT_ULL() for u64 mask in address space handler

      commit: 59084c564c412b1d537f90bd70fa1d6bfb584e82

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>