Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 15 min read

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

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), 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

  1. The spikes hidden inside the average
  2. A win, and an unexpected bill
  3. Chasing the slowdown into a lab
  4. Methodology
  5. No single winner
  6. Same reads, different cost
  7. The Genoa result: a security mitigation changed the answer
    • Why zstd and not gzip?
    • Turin: mitigated without Safe RET
  8. Choose for your workload
  9. 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 with three lanes on one time axis. Compression CPU sampled every second shows tall, short bursts about 35 seconds apart. The same metric averaged, as a dashboard shows it, is a flat low line. Database stop-the-world pauses line up under each burst. Schematic, no measured values.

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.

Four Google Cloud VMs in us-east4-a, 4 vCPUs each: c3-standard-4 (Intel Sapphire Rapids), n4-standard-4 (Intel Emerald Rapids), c3d-standard-4 (AMD Genoa, Mitigation: Safe RET), c4d-standard-4 (AMD Turin, Mitigation: IBPB on VMEXIT only). Same Ubuntu 24.04 image, kernel 7.0.0-1014-gcp and ZFS module 2.4.1 with bundled zstd 1.4.5. Kernel states: default on all four; spec_rstack_overflow=off on c3d; mitigations=off on c3, n4 and c4d in run 1. Pool on a 4 GiB RAM disk: ashift 12, recordsize 128K, fletcher4 checksums, ARC capped at 3 GiB.

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).

One test cell on a fresh dataset: W writes 512 MiB and syncs, measuring compression CPU. R-cold exports and imports the pool and reads 512 MiB, checked so that at least 90% of records miss the ARC. R-warm reads the same 512 MiB three times from the ARC, checked so that ARC data misses are at most 1% and the dbuf cache evicted at least 90% of records. R-rand does 10,000 random 4 KiB reads from the ARC (50,000 in run 1). Around every cell: a CPU calibration per kernel state, exclusion rules, medians of valid repetitions, and extra untimed cells for decompression counts and profiles.

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=off tests, 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.

CPUΒ cost=GiBΒ processedCPU-seconds,Β summedΒ overΒ allΒ vCPUs​

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=off always after the defaults.
  • The guest reports microcode 0xffffffff, hiding the host's.
  • Not recorded: the RAM-disk type (likely brd, inferred from brd functions 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.

Four panels of compression ratio by codec. events: lz4 2.11, gzip-9 3.01, zstd-1 3.20, zstd-9 3.21. json: lz4 2.35, and gzip-9, zstd-1 and zstd-9 all 4.56. silesia: lz4 2.01, gzip-9 2.97, zstd-1 2.73, zstd-9 3.06. text (run 1): lz4 1.68, gzip-9 2.56, zstd-1 2.32, zstd-9 2.66.

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.

Two panels of CPU-seconds per GiB on silesia, lower is better. Write: gzip-9 116.3, 107.0, 77.2 on c3, n4, c4d against zstd-1 13.8, 12.8, 10.1; Genoa default gzip-9 107.5, zstd-1 50.7; Genoa with SRSO off gzip-9 104.6, zstd-1 13.5. ARC read: gzip-9 4.29, 3.98, 2.57 against zstd-1 3.71, 3.16, 2.84; Genoa default gzip-9 3.15, zstd-1 17.1; Genoa SRSO off gzip-9 3.00, zstd-1 3.99.

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.

Two lanes. Normal return: the caller calls a function, the function ends with ret, and the CPU's guess of where to return is right, so the caller continues. With Safe RET, the default on Zen 1 to 4: the function's return is compiled as a jump to the return thunk, which runs the safe-return sequence; the CPU's guess goes to a controlled location, a forced misprediction, and then the real return reaches the caller. Footer: cost should grow with kernel returns more than with bytes, and an untraining sequence also runs on each kernel entry.

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).

Bar chart of speed-up with SRSO off divided by default, on the same c3d VM, run 2, with bars starting at 1Γ— (no change). ARC read: zstd-1 4.52, 4.37, 4.38 on silesia, json, events, with silesia going from 60 to 272 MiB/s; gzip-9 1.04, 1.01, 1.00; compression off 1.19, 1.27, 1.25. Write: zstd-1 3.66, 3.64, 3.67; gzip-9 1.03, 1.04, 1.05; compression off 1.26, 1.33, 1.29.

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).

Decision flow, Linux v7.0 default path, simplified. Is the CPU affected by SRSO? No: not affected (c3, n4, per their reported status). Yes: does the CPU report SRSO_USER_KERNEL_NO? No: Safe RET, every kernel return mispredicts and untraining runs on kernel entry (c3d, Genoa). Yes: does it also report BpSpecReduce? Yes: Reduced Speculation. No: IBPB on VM exit, which protects the host from its VMs and adds nothing to a guest that runs no VMs (c4d, Turin, our inference).

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.

Heat grid of median CPU-seconds per GiB on silesia, run 2, coloured on a log scale per panel. Colour key: light cells use less CPU (better), dark cells use more CPU (worse). Columns: c3 Sapphire Rapids, n4 Emerald Rapids, c4d Turin, c3d Genoa default (Safe RET on, outlined), and c3d Genoa with SRSO off (diagnostic). Write: off 1.43, 1.77, 0.69, 1.30, 1.01; lz4 3.82, 3.80, 2.25, 3.66, not run; zstd-1 13.80, 12.83, 10.15, 50.72, 13.51; gzip-9 116.31, 107.03, 77.23, 107.50, 104.57; zstd-9 49.89, 46.86, 36.16, 123.65, not run. ARC read: off 0.21, 0.27, 0.08, 0.16, 0.13; lz4 0.67, 0.72, 0.47, 2.16, not run; zstd-1 3.71, 3.16, 2.84, 17.08, 3.99; gzip-9 4.29, 3.98, 2.57, 3.15, 3.00; zstd-9 3.81, 3.34, 3.08, 18.83, not run. Ratios: off 1.00, lz4 2.01, zstd-1 2.73, gzip-9 2.97, zstd-9 3.06. Footer: zstd-1 writes are cheap everywhere, but on Genoa's default kernel its ARC reads cost 5 times gzip-9's, and lz4 reads cost 3 to 5 times more than on the other CPUs.

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

  1. What matters most: capacity, write latency, read latency or CPU headroom?
  2. Which reads dominate: disk, ARC, or uncompressed copies in RAM (usually the dbuf cache)?
  3. What are your real request sizes and data? Small reads from big records decompress whole records, unless cached uncompressed.
  4. Measure lz4, zstd-1 and your current codec on production-like hosts and kernel settings; record each host type's spec_rstack_overflow status (cat /sys/devices/system/cpu/vulnerabilities/spec_rstack_overflow).
  5. If capacity matters, test whether a higher level pays for itself.
  6. 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.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.