efivarfs: avoid slow QueryVariableInfo() in statfs()
Subsystems (3)
PATCH 1/1 efivarfs: avoid slow QueryVariableInfo() in statfs()
QueryVariableInfo() is an EFI runtime service that, on some platforms, takes tens of milliseconds and runs with preemption disabled, freezing the CPU that services it for the whole call. efivarfs_statfs() issued this call on every statfs(2) (e.g. every "df"), which produced large latency spikes for unrelated latency-sensitive workloads pinned to the same CPU.
Commit b2326338dc68 ("efivarfs: Rate limit statfs() handler") already bounds the firmware call to twice per second. That helps against a statfs() flood, but on a real-time / dataplane system even the residual 2 calls/s x ~40ms = up to ~80ms/s of preempt-disabled CPU time is
unacceptable: any unprivileged process (or a periodic monitoring "df") can still inject ~40ms stalls into a co-located latency-sensitive task.
Cache the reported capacity instead of querying firmware from the statfs() hot path:
- The cache is primed once at mount time so df has valid numbers.
- When fs.efivarfs.statfs_refresh is set (default 1, preserving the current rate-limited behaviour), statfs() refreshes the cache in the same rate-limited way.
- Latency-sensitive systems can set fs.efivarfs.statfs_refresh to 0 so statfs() never calls firmware; the cache is then kept up to date only by mount and explicit fs.efivarfs.force_refresh writes.
- fs.efivarfs.force_refresh is a write-only knob that forces an immediate synchronous re-query on demand.
On a hard query failure (a status other than EFI_SUCCESS or EFI_UNSUPPORTED) the last known-good cached value is retained rather than overwritten, so a transient firmware error does not zero out the reported capacity.
Measured on an Intel Xeon E5-2628L v4 (6.12 kernel), per statfs(2) on the efivarfs mount:
# fs.efivarfs.statfs_refresh = 1 (firmware call on the hot path)
$ strace -T -e trace=statfs df 2>&1 | grep -i efivars
statfs("/sys/firmware/efi/efivars", {...}) = 0 <0.062993>
statfs("/sys/firmware/efi/efivars", {...}) = 0 <0.064343># after: sysctl fs.efivarfs.statfs_refresh=0 (served from cache)
$ strace -T -e trace=statfs df 2>&1 | grep -i efivars
statfs("/sys/firmware/efi/efivars", {...}) = 0 <0.000009># in a tight df loop with refresh=1, rate-limited hits show the cache # (sub-ms) interleaved with the ~63ms firmware calls: statfs(...) = 0 <0.063705> statfs(...) = 0 <0.000429> statfs(...) = 0 <0.000079>
force_refresh picks up an intervening variable write on demand without re-enabling the hot-path call:
$ df -h | grep efivars efivarfs 88K 54K 30K 65% /sys/firmware/efi/efivars $ sysctl -w fs.efivarfs.force_refresh=1 $ df -h | grep efivars efivarfs 88K 55K 29K 66% /sys/firmware/efi/efivars
---
An alternative would be to expose statfs_refresh as an efivarfs mount option instead of a global sysctl (per-mount, no new sysctl ABI, and a remount could re-prime the cache in place of force_refresh). I went with the sysctl for a live runtime toggle and because the variable store / cache are inherently global, but I'm happy to respin as a mount option if that's preferred.
Documentation/admin-guide/sysctl/fs.rst | 24 +++++ fs/efivarfs/super.c | 132 +++++++++++++++++++----- 2 files changed, 128 insertions(+), 28 deletions(-)
Documentation/admin-guide/sysctl/fs.rst +24 -0
@@ -76,6 +76,30 @@ they help speeding up rejection of non-existing files provided by the users. +efivarfs +-------- + +These entries appear under ``/proc/sys/fs/efivarfs`` and control how +``statfs(2)`` on an efivarfs mount reports the EFI variable-store +capacity. The capacity is obtained from the ``QueryVariableInfo()`` EFI +runtime service, which on some x86 platforms can take tens of +milliseconds. To avoid this cost on the ``statfs(2)`` hot path the +reported capacity is cached; these knobs control how the cache is +refreshed. + +``statfs_refresh`` (default 1) + When set to 1, ``statfs(2)`` refreshes the cached capacity by calling + the firmware, rate-limited to a few calls every couple of seconds. + When set to 0, ``statfs(2)`` never calls firmware and always serves the + cached value; the cache is then updated only at mount time and by + ``force_refresh``. Set this to 0 on latency-sensitive systems. + +``force_refresh`` (write-only) + Writing any value forces an immediate, synchronous re-query of the + firmware to update the cache. Useful to pick up capacity changes when + ``statfs_refresh`` is 0. The written value is ignored. + + file-max & file-nr ------------------
fs/efivarfs/super.c +104 -28
173 lines not on this page · raw message
Signature · 3 lines
2.34.1 base-commit: 9b87fdc9af2fbfcdb5c24a64139685ef80f6573f
Ard Biesheuvel
maintainerHello Prashant,
Quoted 7 lines · Prashant Singh
QueryVariableInfo()is an EFI runtime service that, on some platforms, takes tens of milliseconds and runs with preemption disabled, freezing the CPU that services it for the whole call.efivarfs_statfs()issued this call on every statfs(2) (e.g. every "df"), which produced large latency spikes for unrelated latency-sensitive workloads pinned to the same CPU.
Quoted 7 lines
Commit
b2326338dc68("efivarfs: Rate limitstatfs()handler") already bounds the firmware call to twice per second. That helps against astatfs()flood, but on a real-time / dataplane system even the residual 2 calls/s x ~40ms = up to ~80ms/s of preempt-disabled CPU time is
unacceptable: any unprivileged process (or a periodic monitoring "df") can still inject ~40ms stalls into a co-located latency-sensitive task.
Could we instead introduce a mount option that disables used/available reporting entirely? Feel free to set the default to 'disabled' when CONFIG_PREEMPT_RT is set.
Quoted 3 lines · Ard Biesheuvel
Could we instead introduce a mount option that disables used/available reporting entirely? Feel free to set the default to 'disabled' when
CONFIG_PREEMPT_RTis set.
Will do. I'll add a bool mount option (e.g. statfs_capacity) that, when disabled, skips QueryVariableInfo() in statfs() and reports 0 used/available, defaulting to disabled under CONFIG_PREEMPT_RT.
If you'd rather df still show numbers without the per-statfs firmware call, I could instead prime the value once at mount and serve that (stale but non-zero) as a "cached" mode -- i.e. a tri-state option:
statfs_capacity=live|cached|off
live - current behaviour: rate-limited QueryVariableInfo() on statfs()
cached - prime once at mount, serve the static value (no per-statfs call)
off - report 0 used/available (no firmware call at all)
default: live on non-RT, off on CONFIG_PREEMPT_RT
Happy either way.
Thanks,
Prashant
Ard Biesheuvel
maintainerQuoted 9 lines · Prashant Singh
Quoted 3 lines · Ard Biesheuvel
Could we instead introduce a mount option that disables used/available reporting entirely? Feel free to set the default to 'disabled' when
CONFIG_PREEMPT_RTis set.Will do. I'll add a bool mount option (e.g.
statfs_capacity) that, when disabled, skipsQueryVariableInfo()instatfs()and reports 0 used/available, defaulting to disabled underCONFIG_PREEMPT_RT.
Let's call it nostatfs.
Quoted 4 lines
If you'd rather df still show numbers without the per-statfs firmware call, I could instead prime the value once at mount and serve that (stale but non-zero) as a "cached" mode -- i.e. a tri-state option:
Quoted 9 lines
statfs_capacity=live|cached|off
live - current behaviour: rate-limitedQueryVariableInfo()onstatfs()
cached - prime once at mount, serve the static value (no per-statfs call)
off - report 0 used/available (no firmware call at all)default: live on non-RT, off on
CONFIG_PREEMPT_RTHappy either way.
No, let's keep it simple. Thanks.