Sixty-Four CPUs Fighting Over One Refcount: A Page Cache Scalability Post-Mortem
Sixty-four threads, sixteen bytes each, same file, one file descriptor per thread. On ext4 the unpatched kernel does 15.0–15.7 million reads per second....
Sixty-four threads, sixteen bytes each, same file, one file descriptor per thread. On ext4 the unpatched kernel does 15.0–15.7 million reads per second. With the series Kiryl Shutsemau posted on 19 October 2026 it does 68.9–70.1 million.
Run the same benchmark with one thread and the numbers are 1.6–1.9M before, 1.7–2.0M after. The patch is a 4.6x win and a 5% win depending on a variable most benchmarks never sweep.
That gap is the interesting artifact: a pathology that only exists at scale, fixed by deleting a reference count instead of making the reference count cheaper.
The Benchmark That Hides the Bug: Flat at One Thread, 4.6x at Sixty-Four
Torvalds's original RFC was ten patches for read()-heavy workloads doing small reads of one file across many threads. Shutsemau (Meta) reworked it into eighteen, titled mm: read the page cache without folio references, and published numbers from a 64-vCPU KVM guest on a Threadripper 7975WX, 2G of RAM, no debug options, against v7.3-rc7.
The shape of the win matters more than its size:
- ext4, 64 threads, 16-byte
pread(): 15.0–15.7 → 68.9–70.1 M reads/s - tmpfs, 64 threads: 1.6–1.7 → 71–72 M reads/s
- XFS, 64 threads: 11.3–11.4 → 20.4–20.5 M reads/s
- raw block device, 64 threads: 14.7 → 28.8–28.9 M reads/s
- any of them, 1 thread: 1.6–1.9 → 1.7–2.0 M reads/s
tmpfs is the tell. Its unpatched 64-thread figure is indistinguishable from its single-thread figure — sixty-four CPUs reading as fast as one. That is not a bandwidth ceiling. That is serialization.
A second workload reads 64K at offset 0 with 64 threads, from a file backed by a single large folio. tmpfs goes 96–100 → 1370–1390 MiB/s. ext4 goes 756–775 → 1355–1373 MiB/s. A 14x and a 1.8x from the same patch.
Third workload, the honest one: large sequential reads of a 256M file are unchanged — within 1% of base single-threaded, equal at 16 threads. This is not a faster read path. It is a fix for one access shape: small reads, hot data, many CPUs, shared folio.
Reproducing the shape takes thirty lines. The detail that decides whether you measure anything is one descriptor per thread — share it and f_pos contention replaces the refcount contention:
#define _GNU_SOURCE
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static const char *path;
static long iters;
static void *reader(void *arg)
{
char buf[16];
int fd = open(path, O_RDONLY);
if (fd < 0) {
perror("open");
return NULL;
}
for (long i = 0; i < iters; i++) {
ssize_t n = pread(fd, buf, sizeof buf, 0);
if (n != (ssize_t)sizeof buf) {
perror("pread");
break;
}
}
close(fd);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t th[256];
if (argc != 4) {
fprintf(stderr, "usage: %s FILE THREADS ITERS\n", argv[0]);
return 2;
}
path = argv[1];
int nthreads = atoi(argv[2]);
iters = atol(argv[3]);
for (int i = 0; i < nthreads; i++)
if (pthread_create(&th[i], NULL, reader, NULL)) {
perror("pthread_create");
return 1;
}
for (int i = 0; i < nthreads; i++)
pthread_join(th[i], NULL);
return 0;
}
If your harness opens one descriptor and hands it to every thread, this class of bug is invisible by construction — and so is every fix for it.
A Refcount Is a Cacheline, Not a Counter
filemap_read() has to guarantee the folio stays alive across copy_page_to_iter(). It does that the standard way: take a reference before the copy, drop it after.
folio = filemap_get_folio(mapping, index);
if (IS_ERR(folio))
return PTR_ERR(folio);
copied = copy_page_to_iter(&folio->page, offset, bytes, iter);
folio_put(folio);
return copied;
Two atomic read-modify-writes per sixteen bytes of payload. The arithmetic is free; the cacheline is not. A refcount lives in the folio's struct page, so every folio_get() invalidates that line in the caches of sixty-three other CPUs, and every folio_put() does it again. The read touches no shared state except to announce that it is touching shared state.
Making the counter cheaper does not fix this. A per-CPU delta scheme still needs a shared notion of liveness for a folio that sixty-four threads are reading, so an aggregation point returns. The refcount was never the cost. The requirement that the folio be provably alive during the copy was.
Delete the Reference Instead of Optimizing It
The series removes that requirement for the folios that need it least: hot ones. A folio a reader may copy from without a reference is freed only after an RCU grace period, tracked by a new page flag PG_rcu_free, on 64-bit kernels only.
Selectivity is the design. folio_mark_accessed() already tells the LRU which folios are being touched, and the patch reuses that signal to decide which folios get deferred. Streaming reads and data that never comes back are freed immediately, so memory held back tracks the hot working set rather than the page cache.
The reader side is an xarray lookup with a discipline that looks redundant until you see why it is not:
folio = xas_load(&xas);
if (!folio_test_rcu_free(folio))
goto take_reference;
isize = i_size_read(inode);
if (xas_reload(&xas) != folio)
goto retry;
copied = copy_page_to_iter(&folio->page, offset, bytes, iter);
xas_reload() is not defensive programming. It is the correctness argument: the folio can leave the page cache between the load and the copy, and a grace period guarantees the memory stays mapped, not that the mapping still refers to that folio.
Nine hours after Torvalds posted the RFC, Shutsemau replied with the interleaving that breaks a version of this without the reload:
- CPU0 loads folio A, sees
PG_rcu_free, confirms the slot still holds A. - CPU1 calls
fadvise(FADV_DONTNEED). A leaves the page cache, but its free is deferred, so the memory stays valid. - CPU1
write()s to the same offset, repopulating the page cache at that index with a new folio, and extendsi_size. - CPU0 reads
i_size, now larger, and copies sixteen bytes out of the old folio — data the user never wrote.
The fix is the reload. The failure is not a use-after-free, which is why no sanitizer would have caught it: the memory is legal, the contents are stale, and the read succeeds with the wrong bytes.
That is the part of the story worth extracting. A model produced a coherent mechanism — deferred frees, hot-folio flagging, xarray lookups — in a form that compiles and passes single-threaded tests. What it did not produce was the counterexample. Kernel maintainers deprecated the Assisted-by tag at the Prague Maintainers Summit in October 2026 because disclosure was being used as a proxy for competence; Torvalds's position was that the tag answers the wrong question. The interleaving above is the right one.
The same instinct that makes When WFI Zeroes x0–x31 worth reading — vendor microarchitecture silently violating a documented register contract — is what finds this race: assume any invariant can break between any two lines.
XFS Gained 1.8x While ext4 Gained 4.5x
Look at the spread again. ext4 went 15.7 → 70. XFS went 11.4 → 20.5. Same patch, same guest, same threads. The difference is a second shared counter nobody removed.
XFS and raw block devices take i_rwsem shared on every read — XFS_IOLOCK_SHARED, inode_lock_shared() in blkdev_read_iter(). A read-write semaphore's counter bounces between CPUs exactly the way a refcount does. The patch deleted one bouncing cacheline and left the other, so the ceiling moved from the folio refcount to the inode lock.
The same reasoning explains the second axis. With default mitigations a single read pays roughly 400 ns at the user/kernel boundary — about 170 ns for TSA and 230 ns for SRSO — capping one thread near 1.8M reads/s. Disable mitigations and that thread does 7.1–8.8M, and the 64-thread ext4 gain grows from 4.5x to 13x.
So the post-patch bottleneck is no longer contention. It is a fixed per-call cost that lock elision cannot touch, and it is the one you cannot patch away without changing what the CPU does on entry. Deleting a shared cacheline moves you to the next bottleneck; it does not remove bottlenecks.
The mitigation rows also reframe the headline. Torvalds measured 13.3M → 304M reads/s across 64 threads on a Threadripper 3970X, and that figure is the kernel with the security tax switched off. The honest row for a production deployment is the mitigated one, which is 4.6x, not 22x.
The decomposition argument in Why Multi-Threaded Shared-Nothing Conquered Redis applies here at a finer granularity: you do not scale a shared counter by making it faster, you scale it by removing the sharing. The folio refcount was removable because liveness can be expressed as a grace period instead of a count. The inode lock is not removable the same way, which is why XFS stayed at 1.8x.
The Bill Arrives as a Grace Period
Deferred frees are memory the allocator cannot hand out. The series pays for that in five places, and the patch list is unusually candid about each one.
- Accounting. A deferred folio is uncharged from its memcg before it waits, as every page cache folio is. For the length of a grace period it counts toward no cgroup at all.
- Compaction. A migrated flagged folio is freed after the grace period, so a pageblock that compaction just emptied becomes free only then.
defrag_modeleans on free pageblocks, and the series states this is not measured yet. - Page flags. The flag is a page flag, 64-bit only, and on large configs it costs MGLRU one refs bit.
- Architectures. On writably mapped files, architectures that implement
flush_dcache_page()take the slow path. - Throughput under pressure. A load that keeps refaulting loses 4–6%. A load that drops 1G of hot data with
FADV_DONTNEEDevery round lands between 9% below and 1% above base.
The pressure path is explicit rather than emergent. A counter NR_FREE_RCU_PAGES tracks pages waiting on a grace period. An expedited grace period runs when a CPU's deferred frees pile up and memory runs short. Those pages are freed before the allocator gives up, before alloc_contig_range() gives up, and before memory offline gives up. A process touching 1400M in a loop alongside the benchmark does not get OOM-killed.
Then the admission that the trade is real. CONFIG_PAGE_RCU_FREE is enabled in every 64-bit kernel, so a workload that loses on the trade — or a bug in the lockless read — has no other way out. Patch 18 adds lockless_page_cache_read to debugfs. Writing zero stops filemap_read() and shmem_file_read_iter() from setting PG_rcu_free and from copying without a reference.
Two unresolved items in the cover letter are worth reading as scope statements rather than TODOs. Patch 12 depends on mapping_exiting() meaning that no file holds the inode open, and f2fs called truncate_inode_pages_final() on open inodes for atomic writes, so a fix for that filesystem is inbound. Patch 17 drops the folio lock from tmpfs reads, where shmem_get_folio() held it only for the lookup — meaning a read was never atomic against a write or a hole punch, and now also does not wait for one that holds the lock, matching filemap_read().
A 4.6x benchmark number is a claim about one access shape. A debugfs switch that turns the optimization off is a statement that the access shape is not universal, and that whoever operates the machine gets to decide. Both were written by the same series, in the same week, and only one of them survives contact with a workload that refaults.