btrfs: compression: data race on compr_pool.count between shrinker scan and folio alloc/free (KCSAN data race)

From: 徐晗翔

Date: Tue Nov 17 2026 - 07:38:38 EST


btrfs_compr_pool_scan() reads compr_pool.count at line 160 before
taking compr_pool.lock, while btrfs_alloc_compr_folio() (line 195) and
btrfs_free_compr_folio() (line 219) read-modify-write the same int
under that lock. KCSAN reports the race. The practical effect is
limited: a shrinker pass can observe 0 at line 160 before a concurrent
free increments the count, return SHRINK_STOP, and leave a just-freed
folio in the pool until the next pass. The pool has a hard upper bound
(thresh + 1), and I have not found a path to list corruption, double
free, or use-after-free. This is a low-risk data race, not a memory
safety bug.

The detailed description:

* Test environment / affected version

Linux version 7.3.0 (Ubuntu clang 18.1.3, GNU ld 2.42) #1 SMP
PREEMPT_DYNAMIC, built from the unmodified kernel.org 7.3 tarball
(verification/linux-7.3), QEMU/KVM x86_64 guest, 16 vCPU, 4 GiB RAM.
The kernel is a KCSAN build with BTRFS enabled; relevant config:

CONFIG_KCSAN=y
CONFIG_KCSAN_EARLY_ENABLE=y
CONFIG_KCSAN_UDELAY_TASK=80
CONFIG_KCSAN_SKIP_WATCH=4000
CONFIG_KCSAN_REPORT_ONCE_IN_MS=3000
CONFIG_BTRFS_FS=y
CONFIG_ZLIB_DEFLATE=y
CONFIG_LZO_COMPRESS=y

The test filesystem is a 2 GiB btrfs image made with btrfs-progs
6.6.3 (mkfs.btrfs -f -d single -m single -L btrfsrace), mounted as

mount -t btrfs -o compress-force=zlib /dev/vda /mnt

The source under test is unmodified. All line numbers are from 7.3.

* Mechanism

The pool is a single global object in fs/btrfs/compression.c:

:132 static struct btrfs_compr_pool {
:133 struct shrinker *shrinker;
:134 spinlock_t lock;
:135 struct list_head list;
:136 int count;
:137 int thresh;
:138 } compr_pool;

The shrinker's scan callback checks the count without the lock:

:154 static unsigned long btrfs_compr_pool_scan(...)
:160 if (compr_pool.count == 0)
:161 return SHRINK_STOP;
:164 spin_lock(&compr_pool.lock);
:165 list_splice_init(&compr_pool.list, &remove);
:166 freed = compr_pool.count;
:167 compr_pool.count = 0;
:168 spin_unlock(&compr_pool.lock);

Allocation and free update count only while holding compr_pool.lock:

:191 spin_lock(&compr_pool.lock);
:192 if (compr_pool.count > 0) {
:193 folio = list_first_entry(...);
:194 list_del_init(&folio->lru);
:195 compr_pool.count--;
:196 }
:197 spin_unlock(&compr_pool.lock);

:214 spin_lock(&compr_pool.lock);
:215 if (compr_pool.count > thresh)
:216 do_free = true;
:217 else {
:218 list_add(&folio->lru, &compr_pool.list);
:219 compr_pool.count++;
:220 }
:221 spin_unlock(&compr_pool.lock);

btrfs_compr_pool_count(), the shrinker's count_objects callback,
returns count - thresh when that is positive. thresh is set to
BTRFS_MAX_COMPRESSED / PAGE_SIZE * 8, i.e. 256 for 4 KiB pages, so the
callback returns 1 exactly when the pool has reached its upper bound
of 257 folios. do_shrink_slab() then calls btrfs_compr_pool_scan()
with freeable == 1. The scan callback's line 160 read is outside
compr_pool.lock, so it can run concurrently with the line 195/219
read-modify-write.

After the count callback has returned 1 at count == 257, concurrent
allocators can drain the pool before line 160 is executed. If line
160 then reads 0, and a free increments the count immediately
afterwards, the scan returns SHRINK_STOP without taking the lock; the
folio that the free adds stays in the pool until a later reclaim pass.
If line 160 reads a positive value, the scan takes the lock, splices
the whole list and resets count, so the count/list invariant is
preserved. The race is therefore a missed-reclaim window, not a
corrupt-list window.

* KCSAN evidence

The controlled run produced 7 scan reports, 49 count-callback reports,
and 56 btrfs reports in total (113 KCSAN reports overall). Two
representative headers are:

BUG: KCSAN: data-race in btrfs_alloc_compr_folio / btrfs_compr_pool_scan
read at btrfs_compr_pool_scan+0x3f/0x...
read-write at btrfs_alloc_compr_folio+0xe5/0x...

BUG: KCSAN: data-race in btrfs_compr_pool_scan / btrfs_free_compr_folio
read at btrfs_compr_pool_scan+0x3f/0x...
read-write at btrfs_free_compr_folio+0x109/0x...

Disassembly mapping:

btrfs_compr_pool_scan+0x3f cmpl $0x0, compr_pool.count :160
btrfs_alloc_compr_folio+0xe5 mov ..., compr_pool.count :195
btrfs_free_compr_folio+0x109 mov ..., compr_pool.count :219

The same run also reports btrfs_compr_pool_count+0x11 (the marked
READ_ONCE at line 149) against the line 195/219 writes. READ_ONCE
prevents compiler merging but does not provide cross-CPU ordering;
the line 160 plain load is the access this report is about.

* Trigger / reproducer

The attached btrfs_mr01_race.c is an out-of-tree trigger. It resolves
the real, unexported btrfs_alloc_compr_folio() and
btrfs_free_compr_folio() with kprobes, takes fs_info from a mounted
btrfs at /mnt, fills the pool with 300 folios and frees them (the
first 256 are cached; the count reaches the 257 upper bound), then
keeps alloc/free activity on the pool. A user-space loop runs
drop_caches to invoke the shrinker. The module does not modify
kernel source or the btrfs on-disk format.

Build (against the KCSAN+Btrfs build tree):

make -C <kernel-src> O=<build-dir> M=$PWD modules
insmod btrfs_mr01_race.ko

Then, with /mnt mounted as above:

i=0
while [ $i -lt 300 ]; do
echo 2 > /proc/sys/vm/drop_caches
usleep 20000
i=$((i+1))
done
rmmod btrfs_mr01_race

KCSAN must be enabled (debugfs mounted; echo on > /sys/kernel/debug/kcsan).
The run used skip_watch=30 and udelay_task=500 to widen the window.
In this controlled setup the module performed 415500 alloc/free pairs
in 831 fill/shake loops; the full serial log is 4052 lines.

This is a controlled trigger, not a claim about production frequency:
a 75-second natural workload with 32 writers under compress-force=zlib
did not hit the count==257 window, and the earlier 5-second kprobe run
saw scan=0, alloc=32, count=102. The trigger proves the line 160 read
and the line 195/219 writes can overlap; it does not prove a
production-rate failure.



* Suggested direction

Read the count with READ_ONCE() at line 160:

if (READ_ONCE(compr_pool.count) == 0)
return SHRINK_STOP;

or move the zero check inside compr_pool.lock if an exact decision is
preferred. READ_ONCE is sufficient to silence the data race and to
make the lockless snapshot intentional; it does not by itself make
the reclaim decision atomic, which is acceptable for this best-effort
shrinker. Report-first; no patch attached.

A separate report covers the free_ws race in btrfs_get_workspace();
please do not fold the two into one patch.

Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
/*
* Controlled trigger for the btrfs compression folio-pool race (MR-01):
* btrfs_compr_pool_scan()'s lockless count read vs the ws_lock-protected
* count updates in btrfs_alloc_compr_folio()/btrfs_free_compr_folio().
*
* The module only calls the real, unmodified btrfs functions. It fills the
* global compr_pool to its upper bound (thresh + 1), then keeps alloc/free
* activity on the pool while the user triggers the shrinker via drop_caches.
*/
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/namei.h>
#include <linux/magic.h>
#include <linux/kprobes.h>
#include <linux/atomic.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>

#define NTHREADS 4
#define FILL 300
#define SHAKE 200

typedef void *(*alloc_fn_t)(void *fs_info, gfp_t gfp);
typedef void (*free_fn_t)(void *folio);

static unsigned long alloc_addr, free_addr;
static void *fs_info;
static struct task_struct *threads[NTHREADS];
static atomic_t stop;
static atomic64_t allocs, frees, loops;

static int lookup_symbol(const char *name, unsigned long *addr)
{
struct kprobe kp = { .symbol_name = name };
int ret;

ret = register_kprobe(&kp);
if (ret)
return ret;
*addr = (unsigned long)kp.addr;
unregister_kprobe(&kp);
return 0;
}

static int fill_once(alloc_fn_t alloc, free_fn_t free)
{
void **v;
int i, n = 0;

v = kvmalloc_array(FILL, sizeof(void *), GFP_KERNEL);
if (!v)
return -ENOMEM;
for (i = 0; i < FILL; i++) {
v[i] = alloc(fs_info, GFP_NOFS);
if (!v[i])
break;
n++;
}
for (i = 0; i < n; i++)
free(v[i]);
kvfree(v);
atomic64_add(n, &allocs);
atomic64_add(n, &frees);
return n;
}

static int worker(void *arg)
{
alloc_fn_t alloc = (alloc_fn_t)alloc_addr;
free_fn_t free = (free_fn_t)free_addr;
int i;

while (!kthread_should_stop() && !atomic_read(&stop)) {
fill_once(alloc, free);
/* Keep the pool at 257 and create 195/219 writes. */
for (i = 0; i < SHAKE; i++) {
void *f = alloc(fs_info, GFP_NOFS);

if (!f)
break;
atomic64_inc(&allocs);
free(f);
atomic64_inc(&frees);
}
atomic64_inc(&loops);
cond_resched();
}
return 0;
}

static int __init btrfs_mr01_race_init(void)
{
struct path path;
struct super_block *sb;
int i, ret;

if (kern_path("/mnt", LOOKUP_FOLLOW, &path))
return -ENOENT;
sb = path.dentry->d_sb;
if (sb->s_magic != BTRFS_SUPER_MAGIC) {
path_put(&path);
return -EINVAL;
}
fs_info = sb->s_fs_info;
path_put(&path);

ret = lookup_symbol("btrfs_alloc_compr_folio", &alloc_addr);
if (ret)
return ret;
ret = lookup_symbol("btrfs_free_compr_folio", &free_addr);
if (ret)
return ret;

atomic_set(&stop, 0);
atomic64_set(&allocs, 0);
atomic64_set(&frees, 0);
atomic64_set(&loops, 0);
for (i = 0; i < NTHREADS; i++) {
threads[i] = kthread_run(worker, NULL, "btrfs_mr01/%d", i);
if (IS_ERR(threads[i])) {
pr_err("BTRFSMR01: kthread_run failed at %d\n", i);
threads[i] = NULL;
break;
}
}
pr_info("BTRFSMR01: started %d workers fs_info=%px alloc=%px free=%px\n",
i, fs_info, (void *)alloc_addr, (void *)free_addr);
return 0;
}

static void __exit btrfs_mr01_race_exit(void)
{
int i;

atomic_set(&stop, 1);
for (i = 0; i < NTHREADS; i++) {
if (threads[i])
kthread_stop(threads[i]);
}
pr_info("BTRFSMR01: stopped allocs=%lld frees=%lld loops=%lld\n",
atomic64_read(&allocs), atomic64_read(&frees),
atomic64_read(&loops));
}

module_init(btrfs_mr01_race_init);
module_exit(btrfs_mr01_race_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("btrfs compression folio-pool race trigger");