We Fixed Our ZFS Write Spikes, Then Reads Got 4 Slower!
One of our databases was getting hit by long garbage-collection pauses. The trigger turned out to be ZFS: compressing writes with gzip-9 took heavy bursts of CPU, and whenever a burst collided with GC (Garbage Collection
One of our databases was getting hit by long garbage-collection pauses. The trigger turned out to be ZFS: compressing writes with gzip-9 took heavy bursts of CPU, and whenever a burst collided with GC (Garbage Collection), the stop-the-world pause ran far longer than it should.
We switched to zstd-1 to cut write CPU, and it worked: the long pauses vanished. Then reads on some of our environments got about 4Γ slower, with nothing in ZFS to explain why.
The answer wasn't in ZFS. It was Safe RET, a Linux kernel security mitigation active on our AMD Genoa VMs. In the lab, turning it off just to confirm the cause (not as advice) made zstd-1 reads 4.5Γ faster on the exact same machine.
Contents
- The spikes hidden inside the average
- A win, and an unexpected bill
- Chasing the slowdown into a lab
- Methodology
- No single winner
- Same reads, different cost
-
The Genoa result: a security mitigation changed the answer
- Why zstd and not gzip?
- Turin: mitigated without Safe RET
- Choose for your workload
- Sources
The spikes hidden inside the average
It started with pauses. One of our databases regularly stopped all its work for garbage collection (a stop-the-world pause, while its runtime frees unused memory), and the long pauses hurt. Our dashboards showed nothing unusual around them, because they plotted averages.
When we sampled every second instead, a pattern appeared: the pauses lined up with short bursts of ZFS compression CPU that the averages had smoothed away.
Schematic: 1 s samples show compression bursts at the pauses; averages hide them.
That pointed at writes. The bursts follow from how ZFS writes. It doesn't compress a buffered write when the application calls write(). It compresses when it syncs a transaction group, the batch of changes it flushes to disk. That happens "at least every" zfs_txg_timeout seconds, 5 by default, or sooner when dirty data (data waiting in memory to be written) builds up (zfs(4), zfs(4)).
So compression CPU arrives in batches. Ours came about every 35 seconds, a cadence we observed but didn't explain.
And each batch was expensive, because we were compressing with gzip-9, gzip's strongest level (zfsprops(7)). Our lab later put a number on it: on three of our four test CPUs, gzip-9 used 4.7β8.4Γ the write CPU of zstd-1.
A win, and an unexpected bill
zstd-1 compresses far faster, so we switched. At first it looked like a free lunch: fleet-wide ZFS compression CPU dropped by about 83% (comparing roughly 5 weeks after the change against before it), and the long pauses mostly disappeared.
Then reads on some environments got about 4Γ slower. That was the unexpected bill.
Compression isn't only paid on writes. When a read finds a compressed record in the ARC (an ARC hit), ZFS decompresses the whole record, unless an uncompressed copy is still in RAM (arc.c). So a slower decoder costs CPU on every such read, with no disk involved.
But a slower decoder didn't explain why only some environments slowed down, and at first we couldn't explain it.
Chasing the slowdown into a lab
We started by profiling the slow production hosts. The profiles pointed toward the processors, but production hosts differ from each other in too many ways, such as hardware, load, data and configuration, to prove a cause.
So we built a controlled benchmark: the same software and data on four Google Cloud virtual machine (VM) types, with the pool in RAM so that disks couldn't blur the result. If one VM type stood out, the cause would have to come with that machine type. Everything from here on is lab data, not production.
Methodology
We ran the same tests on four VM types with identical software. Each VM had 4 vCPUs (virtual CPUs). The ZFS pool sat on a RAM disk, a block device that lives in memory, so no real disk took part: the results reflect CPU and memory work, not disk speed.
Same software and pool on all four VMs; only the machine type differs. The bold line is each kernel's SRSO status.
GCP documents the CPU behind each series (machine families, CPU platforms). ZFS bundles zstd 1.4.5 (zstd.h); its gzip calls the kernel's zlib (gzip.c, spl-zlib.c).
Each operation targets one cost; the checks confirm where each read was served from.
A test cell is one codec on one data set, one repetition. Each starts on a fresh dataset and runs four operations in order:
- W: write 512 MiB and sync. Measures compression and write CPU.
-
R-cold: export and import the pool (detach and reattach it; export flushes the pool's blocks from the ARC (
dsl_pool.c)), then read 512 MiB. Most records come from the RAM disk, not the ARC. - R-warm: read the same 512 MiB three times from the ARC. Measures ARC-hit decompression.
- R-rand: 10,000 random 4 KiB reads from the ARC (50,000 in run 1). Measures small reads, most of which decompress a whole 128 KiB record.
A kernel state is how the VM was booted: default, SRSO off (c3d), or mitigations=off (c3, n4, c4d; run 1). mitigations=off is a boot option documented as "Disable all optional CPU mitigations" (kernel-parameters.txt).
The R-warm check matters most, because it confirms that the reads really decompressed. The dbuf cache, which holds uncompressed copies of recently used records, targets 1/32 of the ARC's target size (zfs(4)), at most 96 MiB here. That is too small to hold a 512 MiB pass. So most records' uncompressed copies were evicted, and each pass decompressed them again from their compressed copies in the ARC.
| Data set | What it is | In run |
|---|---|---|
| silesia | 12 mixed files: text, XML, executables, databases, images | 1, 2 |
| text | enwik8, the first 100 MB of an English Wikipedia dump | 1 |
| json | Generated log lines, one JSON object per line; 64 MiB unique, repeated to 512 MiB | 2 |
| events | Generated append-only event log: small binary-framed JSON events plus periodic snapshots; all unique | 2 |
| random | Generated incompressible data, a control | 1, 2 |
Each data set is 16 files of 32 MiB, from deterministic generators whose output hashes were fixed before the runs.
Runs. We ran the benchmark twice. Numbers are run 2 unless marked.
- Run 2 (main): 8 codecs (off, lz4, gzip-1/6/9, zstd-1/3/9), 3 repetitions (2 for Genoa with SRSO off).
-
Run 1: adds zstd-19 and the
mitigations=offtests, with 5 repetitions (2 or 3 on Genoa, whose SRSO test used a separate SRSO-on boot).
Order was shuffled per repetition.
CPU is CPU-seconds per GiB: the CPU time spent across all vCPUs of the VM (the whole guest), divided by the GiB processed.
The CPU time comes from /proc/schedstat, a file where the Linux kernel reports scheduler statistics; it sums the time tasks spent running on each CPU (sched-stats.rst), so ZFS kernel threads count. We cross-checked it with /proc/stat. Speed is MiB/s.
Counting and profiling. Extra untimed cells did two jobs. In some, the Linux tracing tool bpftrace counted zio_decompress_data calls, which the ARC makes to decompress a block (arc.c). It counted only full 128 KiB records of the codec under test. In others, the Linux profiler perf record sampled all CPUs to show which functions used the time.
Limits.
- One VM per type: a difference includes memory, host, SMT layout (how vCPUs map to cores and threads) and clock, not only the core. Four VMs, not "AMD vs Intel".
- Few repetitions (3, or 2), but tight: slowest and fastest differed by under 0.8% in half of run 2's write and ARC-read cells, at most 1.3% in the headline Genoa zstd-1 cells, 19% in the noisiest.
- One reader thread (no parallel reads), one ZFS build, one zstd version.
- json and events are generated, log- and event-store-shaped, not real workloads, and not described well enough to regenerate.
- A RAM pool has no device I/O; real disks may differ.
- SRSO off always ran after SRSO on, with a reboot between;
mitigations=offalways after the defaults. - The guest reports microcode
0xffffffff, hiding the host's. - Not recorded: the RAM-disk type (likely
brd, inferred frombrdfunctions in the profiles), the zlib version, and how silesia and enwik8 were extended to 512 MiB. - Scripts, commands and raw data aren't published.
No single winner
No codec won everywhere. Which codec is best depends on the data for size, and on the CPU for speed.
zstd-1 beats gzip-9 on events, ties on json, loses on silesia and text.
Ratios were identical on every CPU. The json tie may come from rounding: ZFS stores each record in whole 4 KiB sectors, so records that compress to slightly different sizes can take the same space (zio.c). We didn't inspect block sizes.
zstd-1 writes cost less everywhere; its ARC reads cost far more on default Genoa.
Writes. zstd-1 writes were cheaper than gzip-9 everywhere, but far less so on default Genoa. Across silesia, json and events, gzip-9 used 4.7β8.4Γ zstd-1's write CPU on c3, n4 and c4d, but only 1.4β2.1Γ on default Genoa. There, zstd-1 got more expensive; gzip-9 didn't get cheaper.
zstd-9 was hit too. On silesia, zstd-9 beat gzip-9's ratio (3.06Γ vs 2.97Γ) at 43β47% of gzip-9's write CPU on c3, n4 and c4d, but 115% on default Genoa.
ARC reads. The table divides gzip-9's ARC-read CPU by zstd-1's. Above 1, zstd-1 is cheaper; below 1, it is more expensive. Ranges cover silesia, json and events. "SRSO off" means Safe RET disabled; the Genoa section below explains it.
| CPU | gzip-9 Γ· zstd-1, ARC-read CPU | Meaning |
|---|---|---|
| n4, Emerald Rapids | 1.26β1.62 | zstd-1 cheaper |
| c3, Sapphire Rapids | 1.16β1.47 | zstd-1 cheaper |
| c4d, Turin | 0.91β1.18 | about even |
| c3d, Genoa, default | 0.18β0.24 | zstd-1 much more expensive |
| c3d, Genoa, SRSO off (diagnostic) | 0.75β1.05 | zstd-1 more expensive, except on events |
Data that doesn't compress still costs. On the random set every codec reported 1.00Γ: ZFS stored records raw, so reads skip decompression, but writes paid for the attempt. Across both runs, gzip-9 spent 20.2β38.2 CPU-s/GiB and zstd-1 1.4β4.3. lz4 cost within 0.1 CPU-s/GiB of compression off, except one noisy cell (n4, run 2, 16% above compression off).
Same reads, different cost
zstd-1 on Genoa wasn't decompressing more often. On json and events, gzip-9 and zstd-1 decompressed exactly the same number of records on every CPU, so the Genoa gap is in the cost of each decompression, not the count.
one 512 MiB pass = 4,096 records of 128 KiB
decompressions per pass, every CPU, SRSO on or off:
gzip-9 zstd-1
json 4,096 4,096
events 4,096 4,096
silesia 4,021 3,987 (zstd-1 stored more records raw)
Random 4 KiB reads (R-rand) matched. On json and events, 96β97% decompressed a whole record for both codecs, and the rest found an uncompressed copy in RAM. On silesia, 94β95% did.
The Genoa result: a security mitigation changed the answer
The lab showed the same kind of slowdown on one VM out of four, the Genoa one. Besides the CPU model, one line in the setup diagram set it apart: its kernel reported Mitigation: Safe RET.
On Genoa, the kernel's default mitigation for a CPU flaw called SRSO made zstd-1 slow; turning it off on the same VM made zstd-1 read about as fast as on c3 (272 vs 277 MiB/s).
First, some background. The kernel is the core of the operating system; applications ask it for work through system calls. ZFS runs inside the kernel as a module, so every compression and decompression is kernel code. That matters, because Safe RET changes how kernel code runs.
Four steps explain it: what a return is, how the CPU predicts it, what SRSO attacks, and what Safe RET does about it.
1. A return jumps back to the caller. Programs are built from functions that call each other. When a function finishes, it runs a return instruction (ret on x86) that jumps back to the code that called it. The kernel, including ZFS and its codecs, does this constantly.
2. The CPU guesses where each return goes. CPUs run ahead before they know for sure where the program goes next. This is speculative execution, used "to improve performance" (spectre.rst). For returns, "AMD CPUs predict RET instructions using a Return Address Predictor" (srso.rst). A wrong guess is a misprediction.
3. SRSO lets an attacker steer that guess. SRSO (Inception, CVE-2023-20569) lets an attacker mis-train an AMD CPU to "control the speculative target of a subsequent kernel RET, potentially leading to information disclosure" (srso.rst). Put simply, when a kernel function returns, the CPU can be tricked into speculatively running attacker-chosen code, which can leak data (AMD-SB-7005).
4. Safe RET makes every return mispredict, on purpose. Safe RET is the kernel's default defence on affected CPUs, Zen 1 to Zen 4 (srso.rst, srso.rst). It "forces the CPU to mispredict every function return using a 'safe return' sequence" (srso.rst). The guess always goes to a location the kernel controls, which is meant to keep an attacker from steering it. That closes the hole, but every kernel return now pays for a misprediction.
Safe RET sends every kernel return's guess to a controlled location, so each return mispredicts on purpose.
How the kernel applies it. In kernels built with return-thunk support, as distro kernels are, every kernel function return is compiled as a jump to a return thunk, one shared piece of code that performs the return (Makefile, L215). At boot and when a module such as ZFS loads, the kernel rewrites those jumps to the Safe RET sequence, or to a plain ret when no return mitigation is needed (alternative.c, boot, module.c).
So the cost should follow returns more than bytes. Most of Safe RET's cost should grow with the number of kernel function returns more than with bytes processed. The docs say only that it "does come at the cost of some performance depending on the workload" (srso.rst).
The test. Our c3d VM reported Mitigation: Safe RET. We rebooted it with only spec_rstack_overflow=off (kernel-parameters.txt), changing only its SRSO status, and reran the same test cells. We call this state "SRSO off": Safe RET disabled.
A diagnostic experiment, not advice. We turned SRSO off only to find the cause: the kernel docs frame it as an option for those who trust their userspace (srso.rst), and AMD "recommends users evaluate their risk environment" (AMD-SB-7005).
With SRSO off (Safe RET disabled), zstd-1 ran 3.6β4.5Γ faster on Genoa; gzip-9 barely moved.
In run 2, zstd-1 ARC reads ran 4.37β4.52Γ faster and writes 3.64β3.67Γ; gzip-9 changed by 0β5%.
Compression off, with no codec work, still gained 19β33%. Safe RET has a second cost that probably explains this. Each time code enters the kernel, for example with a system call, Safe RET runs an untraining sequence that evicts "a potentially poisoned" entry from the branch predictor, the part of the CPU that guesses where branches and returns go (bugs.c, srso.rst, nospec-branch.h, entry_64.S). That is a cost per system call, not per byte.
Run 1 (silesia, 2 repetitions per state) agreed for zstd-1 reads (4.47Γ), and showed lz4's ARC reads 3.25Γ faster with SRSO off, so lz4 is exposed too.
Why zstd and not gzip?
The profiles fit a cost paid per function return, but don't prove it.
gzip-9: one long loop. On Genoa's ARC reads, gzip-9 spent 69β81% of busy CPU in one function, inflate_fast (the low end includes unrelated samples). The kernel's zlib says it takes "more than 95% of the inflate execution time" with large buffers (inffast.c). One long loop means few returns.
zstd-1: many small functions. zstd-1's time was spread out. ZSTD_decompressSequences (the _bmi2 variant) took 20β25%. Small helpers such as BIT_readBits, BIT_lookBits and BIT_skipBits appeared in the profile as separate functions (symbols), at 4β11% each. That suggests some hot helpers aren't inlined in this build. Inlining means the compiler copies a small function's code into its caller, so there is no call and no return. Without it, each call ends in a return. We didn't disassemble the module to confirm it.
More returns, more forced mispredictions. More returns per byte would mean more forced mispredictions. A kernel commit saw the same effect: inlining one function in a BPF lookup cut its latency 7β8% on AMD, which the author attributed to SRSO (commit 1a4a0cb7985f).
What we didn't check. The thunk itself takes under 1% of samples, likely because a misprediction's cost lands on the code it returns to. Counting returns and mispredicted returns with perf stat (srso.rst) would test this; we didn't, nor did we build zstd with more inlining.
Turin: mitigated without Safe RET
The kernel treats Turin as affected too, but picked a mitigation that adds nothing to our VM's own code paths. c4d (Turin; Zen 5 per AMD) reported Mitigation: IBPB on VMEXIT only, and ran the fastest zstd-1 reads in our test, 363 MiB/s on silesia.
1. The kernel marks Zen 5 as affected. The v7.0 code (not yet the docs) marks Zen 5 as affected by SRSO (common.c).
2. The CPU can say the user/kernel boundary is safe. If the CPU reports SRSO_USER_KERNEL_NO (not vulnerable across the user/kernel boundary), the kernel skips Safe RET. Instead it flushes the branch predictor on VM exit, when a guest (the VM) hands control back to its host (the hypervisor on the physical machine). The flush is IBPB, an "indirect branch prediction barrier" (srso.rst). If the CPU also reports BpSpecReduce, the kernel uses "Reduced Speculation" instead (bugs.c, commit 877818802c3e); ours didn't.
3. That mode protects the host, not the guest. The docs say the IBPB mode covers "the Guest->Host transitions only" (srso.rst): it protects a host from its VMs. Our c4d kernel is itself a guest and runs no VMs, so the mode adds nothing to its own code paths (bugs.c).
Turin skips Safe RET because its CPU reports the user/kernel boundary as safe; Genoa's doesn't.
That the guest saw SRSO_USER_KERNEL_NO is our inference from its status string; other kernels or hypervisors may choose differently.
The other three VMs: all mitigations off barely moved codec throughput. On c3, n4 and c4d, mitigations=off (run 1, silesia) moved ARC-read and write throughput for every codec by β0.7% to +1.8%, and with compression off by β7.3% to +6.9%.
Choose for your workload
Test the codec on the hardware and kernel you run, with the reads and writes you actually have. This grid sums up why: the same codec can be cheap on one CPU and expensive on another.
Darker cells cost more CPU, so lighter is better; a higher ratio means smaller on disk. Read across a row: one column breaks the pattern, Genoa with Safe RET on.
Decision aid
- What matters most: capacity, write latency, read latency or CPU headroom?
- Which reads dominate: disk, ARC, or uncompressed copies in RAM (usually the dbuf cache)?
- What are your real request sizes and data? Small reads from big records decompress whole records, unless cached uncompressed.
-
Measure lz4, zstd-1 and your current codec on production-like hosts and kernel settings; record each host type's
spec_rstack_overflowstatus (cat /sys/devices/system/cpu/vulnerabilities/spec_rstack_overflow). - If capacity matters, test whether a higher level pays for itself.
- Watch 1 s peaks, tail latency and application pauses, not averages.
Rolling it out. Canary on a few hosts of each type first. A new compression value "affects only newly-written data" (zfsprops(7)), so read costs shift only as records are rewritten.
Sources
Linux sources are pinned to tag v7.0; the test kernel was Ubuntu's 7.0.0-1014-gcp, whose patches we did not diff. OpenZFS is pinned to upstream zfs-2.4.1.
- Linux kernel docs: SRSO (srso.rst), Spectre (spectre.rst), sched-stats.rst, kernel-parameters.txt
- Linux kernel source:
arch/x86/kernel/cpu/bugs.c,arch/x86/kernel/cpu/common.c,arch/x86/Makefile,arch/x86/kernel/alternative.c,arch/x86/kernel/module.c,arch/x86/include/asm/nospec-branch.h,arch/x86/entry/entry_64.S,lib/zlib_inflate/inffast.c - Linux commits: 877818802c3e, "x86/bugs: Add SRSO_USER_KERNEL_NO support", 1a4a0cb7985f, "bpf/lpm_trie: Inline longest_prefix_match for fastpath"
- AMD, AMD-SB-7005: Return Address Security Bulletin, 5th Gen EPYC (Turin, Zen 5) launch
- Google Cloud: General-purpose machine family, CPU platforms
- OpenZFS: zfs(4), zfsprops(7),
arc.c,zio.c,gzip.c,spl-zlib.c,dsl_pool.c,module/zstd/lib/zstd.h
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.







