From: 徐晗翔 <xuhx@hust.edu.cn>
To: dsterba@suse.com
Cc: linux-kernel@vger.kernel.org, mason@kernel.org,
linux-btrfs@vger.kernel.org
Subject: btrfs: compression: data race on compr_pool.count between shrinker scan and folio alloc/free (KCSAN data race)
Date: Tue, 17 Nov 2026 20:38:20 +0800 (GMT+08:00) [thread overview]
Message-ID: <4cbd7a1f.28fd9.1a1e983f1b1.Coremail.xuhx@hust.edu.cn> (raw)
In-Reply-To: <2026102727-data-sequel-ce41@gregkh>
[-- Attachment #1: Type: text/plain, Size: 7459 bytes --]
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@hust.edu.cn
[-- Attachment #2: btrfs_mr01_race.c --]
[-- Type: text/plain, Size: 3610 bytes --]
// 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");
next prev parent reply other threads:[~2026-11-17 12:38 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 12:35 usb: devio: usbdev_release() has no barrier against an in-flight async_completed() callback (use-after-free window on struct usb_dev_state)gregkh@linuxfoundation.org 徐晗翔
2026-10-27 14:12 ` Greg KH
2026-11-03 11:21 ` mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN blind spot: slub.o is uninstrumented) 徐晗翔
2026-11-04 9:41 ` Qing Wang
2026-11-04 10:07 ` Qing Wang
2026-11-03 11:30 ` futex/hrtimer: hrtimer_sleeper::task is read unsynchronized in 16 places on v7.3 -- the accessor series that fixes this is not in 7.3; please make sure it reaches the affected trees (backport nudge + stock-kernel KCSAN evidence) 徐晗翔
2026-11-03 11:48 ` cgroup: fork vs cgroup.procs write race in favordynmods mode -- v7.3 is affected; please make sure bf6fbf46b5ff reaches the stable trees (repro incl. uninstrumented natural hit) 徐晗翔
2026-11-03 14:51 ` Sasha Levin
2026-11-03 11:57 ` net: rtnetlink: rtnl_link_unregister() vs in-flight RTM_GETLINK dump readers -- v7.3 lacks the synchronize_rcu() fix (Fixes: 6b57ff21a310 family); A/B reproduction on stock 7.3 徐晗翔
2026-11-03 15:49 ` Eric Dumazet
2026-11-04 15:03 ` Sasha Levin
2026-11-03 12:07 ` wifi: wilc1000: SDIO/SPI remove destroys the srcu_struct before wiphy_unregister() -- late cfg80211 callbacks write through dangling srcu state (unfixed upstream; controlled + natural Oops) 徐晗翔
2026-11-03 12:10 ` gpio: gpiolib: final put of the gpio_device happens inside the SRCU read critical section on the setup-failure path -- deterministic use-after-free read at guard release (unfixed upstream) 徐晗翔
2026-11-05 8:52 ` Linus Walleij
2026-11-04 12:54 ` soc: qcom: pmic_glink_altmode: fix use-after-free on unbind (no cancel_work_sync for port work and enable_work) 徐晗翔
2026-11-04 13:04 ` Greg KH
2026-11-04 12:57 ` usb: typec: anx7411: unregister partner/altmodes before stopping the IRQ work -- use-after-free, double unregister, orphan partner on remove 徐晗翔
2026-11-04 13:28 ` Greg KH
2026-11-04 12:58 ` usb: typec: ucsi: deadlock on cros_ec unbind -- connector lock held across cancel_delayed_work_sync vs the mode-selection work's unbounded trylock polling 徐晗翔
2026-11-04 13:00 ` usb: typec: stusb160x/rt1719/hd3ss3220: IRQ thread vs remove -- double unregister / use-after-free on the partner and port 徐晗翔
2026-11-04 13:01 ` usb: typec: ucsi: displayport: data race on dp->header/ vdo_data/vdo_size -- work reads them unlocked while writers hold con->lock (VDM mismatch) 徐晗翔
2026-11-04 13:02 ` usb: typec: class: three lifetime/locking gaps around the mux/switch handles and mode_selection (drvdata UAF after provider unbind; no concurrent protocol for set/put; unlocked state_update vs delete) 徐晗翔
2026-11-10 12:56 ` driver core: uevent: netns teardown frees uevent_sock while a tagged uevent is still being broadcast (UAF) 徐晗翔
2026-11-10 13:00 ` 徐晗翔
2026-11-10 18:48 ` Greg KH
2026-11-10 13:05 ` ACPI: CPPC: per-CPU cpc_desc is freed on processor unbind without clearing the published pointer, causing UAF in cpufreq accessors 徐晗翔
2026-11-10 13:07 ` ACPI: CPPC: shared PCC subspace data can be freed while another CPU probes, reads, or tears it down 徐晗翔
2026-11-10 14:47 ` Christian Loehle
2026-11-10 13:11 ` ACPI: ACPICA: Reset can delete an event semaphore while another thread is blocked in Wait 徐晗翔
2026-11-10 13:14 ` power: supply: bq25890: pump-express work can call power_supply_changed after the psy is unregistered 徐晗翔
2026-11-10 13:17 ` power: supply: core: get/set property callbacks can run after unregister and read freed driver data 徐晗翔
2026-11-10 13:18 ` power: supply: core: system-battery enumeration can return an unregistered battery 徐晗翔
2026-11-10 13:21 ` power: supply: core: changed work can run after unregister and use freed LED triggers 徐晗翔
2026-11-10 13:23 ` power: supply: bq24190: IRQ handler can call power_supply_changed after the psy is unregistered 徐晗翔
2026-11-10 13:34 ` 徐晗翔
2026-11-11 7:17 ` genirq: NMI dispatch reads struct irqaction with no quiescence -- use-after-free across free_nmi()'s kfree (x86 verification on the arch-independent core path) 徐晗翔
2026-11-11 7:26 ` genirq: NMI API lacks the request_mutex / dev_id matching / idempotency semantics that free_irq() has -- double free_nmi releases chip PM and module references twice (verified) 徐晗翔
2026-11-11 7:30 ` kernfs: kernfs_drain()'s fast-path decision reads the active count without acquire ordering -- on weakly-ordered architectures the forced ->release()/unmap can be skipped 徐晗翔
2026-11-11 13:41 ` Greg KH
2026-11-13 11:58 ` ALSA: ctxfi: free atimer only after the timer IRQ is quiesced 徐晗翔
2026-11-13 16:30 ` Takashi Iwai
2026-11-13 12:03 ` ALSA: ctxfi: do not free a running system timer when timer_delete_sync_try() returns -1 (UAF) 徐晗翔
2026-11-13 12:04 ` ALSA: asihpi: free IRQ before snd_card_free()/HPI delete on HPI6205 removal (UAF) 徐晗翔
2026-11-13 12:06 ` ALSA: asihpi: quiesce PCM timer and IRQ before freeing dpcm on close (UAF) 徐晗翔
2026-11-13 12:15 ` ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF) 徐晗翔
2026-11-13 12:28 ` ALSA: usb-audio: us144mkii: PCM close frees runtime while playback/feedback URB completion and capture work still use it (UAF) 徐晗翔
2026-11-13 12:29 ` ALSA: usb-audio: us144mkii: queued stop_pcm_work calls snd_pcm_stop() after PCM close and dereferences NULL runtime 徐晗翔
2026-11-13 12:33 ` sched/topology: old domain chains are queued for RCU freeing before the per-CPU pointers are updated, so a late reader can still dereference a freed sched_domain and sched_domain_shared (UAF) 徐晗翔
2026-11-13 12:47 ` xfs: use-after-free in stats_clear sysctl handler during module unload 徐晗翔
2026-11-13 12:48 ` xfs: data races on xfs_params and xfs_globals tunables 徐晗翔
2026-11-17 12:32 ` jbd2: fix unsynchronized read of j_average_commit_time in procfs stats (KCSAN data race) 徐晗翔
2026-11-17 12:33 ` jbd2: read j_stats.ts_tid under j_history_lock in fast commit path " 徐晗翔
2026-11-17 12:35 ` jbd2: fix unlocked read/write of j_last_sync_writer in jbd2_journal_stop " 徐晗翔
2026-11-17 12:38 ` 徐晗翔 [this message]
2026-11-17 12:47 ` fs/ceph: debugfs mdsmap_show() can dereference a freed MDS map after a concurrent map update (UAF) 徐晗翔
2026-11-17 12:48 ` fs/ceph: ceph_statfs() can dereference a freed MDS map (double evaluation of mdsc->mdsmap) (UAF) 徐晗翔
2026-11-17 12:49 ` fs/ceph: ceph_get_tree() out_splat path walks mdsc->mdsmap without mdsc->mutex (UAF) 徐晗翔
2026-11-17 12:50 ` fs/ceph: __ceph_setxattr() reads mdsmap->m_max_xattr_size " 徐晗翔
2026-11-17 12:51 ` fs/ceph: MDS reconnect paths read mdsc->mdsmap " 徐晗翔
2026-11-17 12:52 ` fs/ceph: caps renew paths read mdsmap->m_session_timeout " 徐晗翔
2026-11-17 12:53 ` fs/ceph: MDS connections can deliver CEPH_MSG_MDS_MAP and re-enter ceph_mdsc_handle_mdsmap() (UAF) 徐晗翔
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4cbd7a1f.28fd9.1a1e983f1b1.Coremail.xuhx@hust.edu.cn \
--to=xuhx@hust.edu.cn \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®