btrfs: Fix BTRFS arm64 tagged KASAN false-positive
Subsystems (2)
PATCH v3 1/1 btrfs: Fix BTRFS arm64 tagged KASAN false-positive
When booting Linux 7.0-rc5 on a Qualcomm Snapdragon X1 with KASAN software tagging with a BTRFS filesystem, we see:
BUG: KASAN: invalid-access in xxh64_update (lib/xxhash.c:143 lib/xxhash.c:283) Read of size 8 at addr 7bff000804fe1000 by task kworker/u49:2/138
Pointer tag: [7b], memory tag: [b2]
CPU: 0 UID: 0 PID: 138 Comm: kworker/u49:2 Not tainted 7.0.0-rc4+ #34 PREEMPTLAZY Hardware name: LENOVO 83ED/LNVNB161216, BIOS NHCN60WW 09/11/2025 Workqueue: btrfs-endio-meta simple_end_io_work Call trace:
show_stack (arch/arm64/kernel/stacktrace.c:501) (C) dump_stack_lvl (lib/dump_stack.c:122)print_report (mm/kasan/report.c:379 mm/kasan/report.c:482) kasan_report (mm/kasan/report.c:597)kasan_check_range (mm/kasan/sw_tags.c:86 (discriminator 1)) __hwasan_loadN_noabort (mm/kasan/sw_tags.c:158) xxh64_update (lib/xxhash.c:143 lib/xxhash.c:283) btrfs_csum_update (fs/btrfs/fs.c:106)csum_tree_block (fs/btrfs/disk-io.c:103 (discriminator 3)) btrfs_validate_extent_buffer (fs/btrfs/disk-io.c:389) end_bbio_meta_read (fs/btrfs/extent_io.c:3853 (discriminator 1)) btrfs_bio_end_io (fs/btrfs/bio.c:152) simple_end_io_work (fs/btrfs/bio.c:388)
process_one_work (./arch/arm64/include/asm/jump_label.h:36 ./include/trace/events/workqueue.h:110 kernel/workqueue.c:3281) worker_thread (kernel/workqueue.c:3353 (discriminator 2) kernel/workqueue.c:3440 (discriminator 2))
kthread (kernel/kthread.c:436)ret_from_fork (arch/arm64/kernel/entry.S:861)
The buggy address belongs to the physical page: page: refcount:3 mapcount:0 mapping:f1ff00080055dee8 index:0x2467bd pfn:0x884fe1 memcg:51ff000800e68ec0 aops:btree_aops ino:1 flags: 0x9340000000004000(private|zone=2|kasantag=0x4d) raw: 9340000000004000 0000000000000000 dead000000000122 f1ff00080055dee8 raw: 00000000002467bd 43ff00081d0cc6f0 00000003ffffffff 51ff000800e68ec0 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff000804fe0e00: 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b ffff000804fe0f00: 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b >ffff000804fe1000: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2
^ffff000804fe1100: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2ffff000804fe1200: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2
This occurs as allocation in btrfs_alloc_page_array is from multiple discrete pages thus different KASAN tags by design, leading to a tag mismatch when linear access is used where the pages are physically contiguous.
Fix this by retagging all the EB pages with the same KASAN tag.
Changelog:
v3: Retag only when contiguous; fix build failure when generic KASAN configured
v2: https://lore.kernel.org/lkml/[email protected]/
- Retag pages rather than bypass linear access optimisation
v1: https://lore.kernel.org/lkml/[email protected]/
--- fs/btrfs/extent_io.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
fs/btrfs/extent_io.c +16 -1
@@ -3533,8 +3533,23 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,if (uptodate)set_bit (EXTENT_BUFFER_UPTODATE , &eb->bflags);/* All pages are physically contiguous, can skip cross page handling. */ -if (page_contig) +if (page_contig) { +#if defined(CONFIG_KASAN_SW_TAGS ) || defined(CONFIG_KASAN_HW_TAGS ) +struct page *page =folio_page (eb->folios[0 ],0 ); +u8 tag =page_kasan_tag (page); + +/* +* Since pages are from multiple allocations and physically +* contiguous allowing linear access, prevent KASAN warnings +* by retagging with the first tag +*/ +for (int i =1 ; i <num_extent_pages (eb); i++) { +page_kasan_tag_set (page + i, tag); +kasan_unpoison_range (page_address (page + i),PAGE_SIZE ); + } +#endif eb->addr =folio_address (eb->folios[0 ]) +offset_in_page (eb->start); + } again:xa_lock_irq (&fs_info->buffer_tree); existing_eb = __xa_cmpxchg(&fs_info->buffer_tree,
Signature · 1 line
2.53.0
Quoted 52 lines · Daniel J Blueman
When booting Linux 7.0-rc5 on a Qualcomm Snapdragon X1 with KASAN software tagging with a BTRFS filesystem, we see:
BUG: KASAN: invalid-access in xxh64_update (lib/xxhash.c:143 lib/xxhash.c:283) Read of size 8 at addr 7bff000804fe1000 by task kworker/u49:2/138Pointer tag: [7b], memory tag: [b2]
CPU: 0 UID: 0 PID: 138 Comm: kworker/u49:2 Not tainted 7.0.0-rc4+ #34 PREEMPTLAZY Hardware name: LENOVO 83ED/LNVNB161216, BIOS NHCN60WW 09/11/2025 Workqueue: btrfs-endio-meta simple_end_io_work Call trace:
show_stack(arch/arm64/kernel/stacktrace.c:501) (C)dump_stack_lvl(lib/dump_stack.c:122)print_report(mm/kasan/report.c:379mm/kasan/report.c:482)kasan_report(mm/kasan/report.c:597)kasan_check_range(mm/kasan/sw_tags.c:86(discriminator 1))__hwasan_loadN_noabort(mm/kasan/sw_tags.c:158)xxh64_update(lib/xxhash.c:143lib/xxhash.c:283)btrfs_csum_update(fs/btrfs/fs.c:106)csum_tree_block(fs/btrfs/disk-io.c:103(discriminator 3))btrfs_validate_extent_buffer(fs/btrfs/disk-io.c:389)end_bbio_meta_read(fs/btrfs/extent_io.c:3853(discriminator 1))btrfs_bio_end_io(fs/btrfs/bio.c:152)simple_end_io_work(fs/btrfs/bio.c:388)process_one_work (./arch/arm64/include/asm/jump_label.h:36 ./include/trace/events/workqueue.h:110 kernel/workqueue.c:3281) worker_thread (kernel/workqueue.c:3353 (discriminator 2) kernel/workqueue.c:3440 (discriminator 2))kthread (
kernel/kthread.c:436)ret_from_fork(arch/arm64/kernel/entry.S:861)The buggy address belongs to the physical page: page: refcount:3 mapcount:0 mapping:f1ff00080055dee8 index:0x2467bd pfn:0x884fe1 memcg:51ff000800e68ec0 aops:btree_aops ino:1 flags: 0x9340000000004000(private|zone=2|kasantag=0x4d) raw: 9340000000004000 0000000000000000 dead000000000122 f1ff00080055dee8 raw: 00000000002467bd 43ff00081d0cc6f0 00000003ffffffff 51ff000800e68ec0 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff000804fe0e00: 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b ffff000804fe0f00: 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7bQuoted 1 line
ffff000804fe1000: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2^
ffff000804fe1100: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2ffff000804fe1200: b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2 b2This occurs as allocation in
btrfs_alloc_page_arrayis from multiple discrete pages thus different KASAN tags by design, leading to a tag mismatch when linear access is used where the pages are physically contiguous.Fix this by retagging all the EB pages with the same KASAN tag.
Quoted 8 lines
Signed-off-by Daniel J Blueman <[email protected]>Fixes 397239ed6a6c ("btrfs: allow extent buffer helpers to skip cross-page handling")Changelog:
v3: Retag only when contiguous; fix build failure when generic KASAN configured
v2: https://lore.kernel.org/lkml/[email protected]/
- Retag pages rather than bypass linear access optimisation
v1: https://lore.kernel.org/lkml/[email protected]/
This one looks good to me for btrfs.
But still a question related to kasan_unpoison_range().
Quoted 16 lines
--- fs/btrfs/extent_io.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)fs/btrfs/extent_io.c +3
-1@@ -3533,8 +3533,23 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,if (uptodate)set_bit (EXTENT_BUFFER_UPTODATE , &eb->bflags);/* All pages are physically contiguous, can skip cross page handling. */ -if (page_contig) +if (page_contig) { +#if defined(CONFIG_KASAN_SW_TAGS ) || defined(CONFIG_KASAN_HW_TAGS ) +struct page *page =folio_page (eb->folios[0 ],0 );
fs/btrfs/extent_io.c struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · line 3548
Quoted 10 lines
fs/btrfs/extent_io.c +10
-0+u8 tag =page_kasan_tag (page); + +/* +* Since pages are from multiple allocations and physically +* contiguous allowing linear access, prevent KASAN warnings +* by retagging with the first tag +*/ +for (int i =1 ; i <num_extent_pages (eb); i++) { +page_kasan_tag_set (page + i, tag); +kasan_unpoison_range (page_address (page + i),PAGE_SIZE );
There is a LKP report that __kasan_unpoison_range() is not properly exported thus if btrfs is compiled as a module, we can not properly link the module.
https://lore.kernel.org/linux-btrfs/[email protected]/
You may need to export __kasan_unpoison_range() to fix it, or explain why kasan_unpoison_range() is needed here.
AFAIK those pages are already unpoisoned at allocation, thus I'm not sure why we need to call kasan_unpoison_range() here again.
Thanks,
Qu
Quoted 7 lines
fs/btrfs/extent_io.c +3
-0+ } +#endif eb->addr =folio_address (eb->folios[0 ]) +offset_in_page (eb->start); + } again:xa_lock_irq (&fs_info->buffer_tree); existing_eb = __xa_cmpxchg(&fs_info->buffer_tree,