Skip to content

gpiolib: Avoid the hotplug performance reduction #6862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025

Conversation

pelwell
Copy link
Contributor

@pelwell pelwell commented May 19, 2025

The 6.9 kernel introduced a large patchset [1] designed to make gpiochip usage safe in the presence of potential hotplugging events. The changes included protecting every gpiochip access with a claim of an interlock.

Running on a Pi 5 these changes reduce GPIO performance from userspace by around 10%. The penalty would be proportionally higher from kernel, as seen by SPI speed reductions.

Patch the gpiolib implementation to remove the protection of gpiochip accesses. By providing alternative implementations of the relevant macros, the changes are localised and therefore easier to verify.

See: #6854

[1] https://lwn.net/Articles/960024/

The 6.9 kernel introduced a large patchset [1] designed to make gpiochip
usage safe in the presence of potential hotplugging events. The changes
included protecting every gpiochip access with a claim of an interlock.

Running on a Pi 5 these changes reduce GPIO performance from userspace
by around 10%. The penalty would be proportionally higher from kernel,
as seen by SPI speed reductions.

Patch the gpiolib implementation to remove the protection of gpiochip
accesses. By providing alternative implementations of the relevant
macros, the changes are localised and therefore easier to verify.

See: raspberrypi#6854

[1] https://lwn.net/Articles/960024/

Signed-off-by: Phil Elwell <[email protected]>
@pelwell
Copy link
Contributor Author

pelwell commented May 19, 2025

Here are the results of running a performance test on a Pi 5. The test drives a GPIO high and low 1 million times in a tight loop, repeating the process 10 times and calculating the mean and standard deviation of the per-pass execution time (in microseconds):

# The last 6.6 release
Linux raspberrypi 6.6.78-v8+ #1854 SMP PREEMPT Mon Feb 17 16:36:07 GMT 2025 aarch64 GNU/Linux
mean: 1125847, std dev: 4469.304004

# Top-of-tree 6.12
Linux raspberrypi 6.12.28-v8+ #46 SMP PREEMPT Mon May 19 14:35:01 BST 2025 aarch64 GNU/Linux
mean: 1251365, std dev: 19056.459418

# And with this PR applied
Linux raspberrypi 6.12.28-v8+ #50 SMP PREEMPT Mon May 19 15:25:05 BST 2025 aarch64 GNU/Linux
mean: 1133894, std dev: 16224.792260

You can see that there is still a small performance regression with the patched kernel, but it has been reduced from ~11% to less than 1%.

@Roland-Smith-Imperial
Copy link

Many thanks for the patch, downloaded and running with no obvious issues.

Testing with 1E6 dsequential samples running a time-stamp, GPIO -> out, 3uSec delay, SPI Read Back on an AD7606 using Python code, priority set as chrt 99 gives the following sample rates :-

6.6.31 = 37.0 kHz

6.12.25 (unpatched) = 34.9k Hz (~6% performance drop from 6.6.31)

6.12.28 (GPIO Patch applied) = 35.5 kHz (~4% performance drop from 6.6.31)

Patched performance is clearly an improvement over unpached 6.12.25 thanks, not quite the 1% hit for pure GPIO high-low toggles in a loop, though we are alternating GPIO / SPI read so a slightly more complex situation. Histograms to follow.
SPI_AD7606_ADC_Sample_Time_Performance_6 6 31
SPI_AD7606_ADC_Sample_Time_Performance_6 12 25
SPI_AD7606_ADC_Sample_Time_Performance_6 12 28_GPIO_Patch

@pelwell
Copy link
Contributor Author

pelwell commented May 21, 2025

This seems like a step in the right direction for performance.

@pelwell pelwell marked this pull request as ready for review May 21, 2025 15:22
@pelwell pelwell merged commit ae92bb9 into raspberrypi:rpi-6.12.y May 21, 2025
12 checks passed
popcornmix added a commit to raspberrypi/firmware that referenced this pull request May 21, 2025
kernel: usb: xhci: set Input Slot Context bit in xhci_fixup_endpoint
See: raspberrypi/linux#6850

kernel: configs: Enable more MediaTek drivers
See: raspberrypi/linux#6857

kernel: firmware/raspberrypi: raise timeout to 3s
See: raspberrypi/linux#6859

kernel: overlays: sc16is75x: Disable spidev0 first
See: raspberrypi/linux#6858

kernel: gpiolib: Avoid the hotplug performance reduction
See: raspberrypi/linux#6862
popcornmix added a commit to raspberrypi/rpi-firmware that referenced this pull request May 21, 2025
kernel: usb: xhci: set Input Slot Context bit in xhci_fixup_endpoint
See: raspberrypi/linux#6850

kernel: configs: Enable more MediaTek drivers
See: raspberrypi/linux#6857

kernel: firmware/raspberrypi: raise timeout to 3s
See: raspberrypi/linux#6859

kernel: overlays: sc16is75x: Disable spidev0 first
See: raspberrypi/linux#6858

kernel: gpiolib: Avoid the hotplug performance reduction
See: raspberrypi/linux#6862
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants