We Fixed Our ZFS Write Spikes, Then Reads Got 4 Slower!
DEV Community

We Fixed Our ZFS Write Spikes, Then Reads Got 4 Slower!

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=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 processed CPU-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. 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,0964,096 events 4,0964,096 silesia4,0213,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

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.