bcachefs (loop4): recovering from clean shutdown, journal seq 13
bcachefs (loop4): Doing compatible version upgrade from 1.7: mi_btree_bitmap to 1.9: disk_accounting_v2
  running recovery passes: check_allocations
==================================================================
BUG: KASAN: slab-use-after-free in bch2_fs_recovery+0xe85/0x38c0 fs/bcachefs/recovery.c:699
Read of size 8 at addr ffff88805b7e8f98 by task syz-executor.4/8467

CPU: 0 PID: 8467 Comm: syz-executor.4 Not tainted 6.10.0-rc3-next-20240611-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 06/07/2024
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:91 [inline]
 dump_stack_lvl+0x241/0x360 lib/dump_stack.c:117
 print_address_description mm/kasan/report.c:377 [inline]
 print_report+0x169/0x550 mm/kasan/report.c:488
 kasan_report+0x143/0x180 mm/kasan/report.c:601
 bch2_fs_recovery+0xe85/0x38c0 fs/bcachefs/recovery.c:699
 bch2_fs_start+0x356/0x5b0 fs/bcachefs/super.c:1029
 bch2_fs_get_tree+0xce4/0x1620 fs/bcachefs/fs.c:1922
 vfs_get_tree+0x90/0x2a0 fs/super.c:1780
 do_new_mount+0x2be/0xb40 fs/namespace.c:3379
 do_mount fs/namespace.c:3719 [inline]
 __do_sys_mount fs/namespace.c:3925 [inline]
 __se_sys_mount+0x2d6/0x3c0 fs/namespace.c:3902
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fae02e7e5aa
Code: d8 64 89 02 48 c7 c0 ff ff ff ff eb a6 e8 de 09 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fae03c94ef8 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007fae03c94f80 RCX: 00007fae02e7e5aa
RDX: 000000002000f640 RSI: 000000002000f680 RDI: 00007fae03c94f40
RBP: 000000002000f640 R08: 00007fae03c94f80 R09: 0000000000200012
R10: 0000000000200012 R11: 0000000000000202 R12: 000000002000f680
R13: 00007fae03c94f40 R14: 000000000000f629 R15: 00000000200000c0
 </TASK>

Allocated by task 8467:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3f/0x80 mm/kasan/common.c:68
 poison_kmalloc_redzone mm/kasan/common.c:370 [inline]
 __kasan_kmalloc+0x98/0xb0 mm/kasan/common.c:387
 kasan_kmalloc include/linux/kasan.h:211 [inline]
 __do_kmalloc_node mm/slub.c:4140 [inline]
 kmalloc_node_track_caller_noprof+0x225/0x440 mm/slub.c:4160
 __do_krealloc mm/slab_common.c:1183 [inline]
 krealloc_noprof+0x7d/0x120 mm/slab_common.c:1216
 bch2_sb_realloc+0x2f5/0x660 fs/bcachefs/super-io.c:189
 __copy_super+0x5dc/0xe70 fs/bcachefs/super-io.c:579
 bch2_sb_to_fs+0xab/0x150 fs/bcachefs/super-io.c:606
 bch2_fs_alloc+0xd36/0x2040 fs/bcachefs/super.c:823
 bch2_fs_open+0x8cc/0xdf0 fs/bcachefs/super.c:2104
 bch2_fs_get_tree+0x750/0x1620 fs/bcachefs/fs.c:1909
 vfs_get_tree+0x90/0x2a0 fs/super.c:1780
 do_new_mount+0x2be/0xb40 fs/namespace.c:3379
 do_mount fs/namespace.c:3719 [inline]
 __do_sys_mount fs/namespace.c:3925 [inline]
 __se_sys_mount+0x2d6/0x3c0 fs/namespace.c:3902
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 8467:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3f/0x80 mm/kasan/common.c:68
 kasan_save_free_info+0x40/0x50 mm/kasan/generic.c:579
 poison_slab_object+0xe0/0x150 mm/kasan/common.c:240
 __kasan_slab_free+0x37/0x60 mm/kasan/common.c:256
 kasan_slab_free include/linux/kasan.h:184 [inline]
 slab_free_hook mm/slub.c:2214 [inline]
 slab_free mm/slub.c:4455 [inline]
 kfree+0x149/0x360 mm/slub.c:4576
 krealloc_noprof+0xcf/0x120
 bch2_sb_realloc+0x2f5/0x660 fs/bcachefs/super-io.c:189
 bch2_sb_field_resize_id+0x140/0x7c0 fs/bcachefs/super-io.c:221
 bch2_sb_counters_from_cpu+0xac/0x300 fs/bcachefs/sb-counters.c:67
 bch2_write_super+0xe80/0x3c80 fs/bcachefs/super-io.c:966
 bch2_fs_recovery+0xe6c/0x38c0 fs/bcachefs/recovery.c:697
 bch2_fs_start+0x356/0x5b0 fs/bcachefs/super.c:1029
 bch2_fs_get_tree+0xce4/0x1620 fs/bcachefs/fs.c:1922
 vfs_get_tree+0x90/0x2a0 fs/super.c:1780
 do_new_mount+0x2be/0xb40 fs/namespace.c:3379
 do_mount fs/namespace.c:3719 [inline]
 __do_sys_mount fs/namespace.c:3925 [inline]
 __se_sys_mount+0x2d6/0x3c0 fs/namespace.c:3902
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

The buggy address belongs to the object at ffff88805b7e8000
 which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 3992 bytes inside of
 freed 4096-byte region [ffff88805b7e8000, ffff88805b7e9000)

The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x5b7e8
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000000040(head|node=0|zone=1|lastcpupid=0x7ff)
page_type: 0xfdffffff(slab)
raw: 00fff00000000040 ffff888015042140 dead000000000122 0000000000000000
raw: 0000000000000000 0000000000040004 00000001fdffffff 0000000000000000
head: 00fff00000000040 ffff888015042140 dead000000000122 0000000000000000
head: 0000000000000000 0000000000040004 00000001fdffffff 0000000000000000
head: 00fff00000000003 ffffea00016dfa01 ffffffffffffffff 0000000000000000
head: 0000000000000008 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0x1d2040(__GFP_IO|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC|__GFP_HARDWALL), pid 8467, tgid 8466 (syz-executor.4), ts 364896318783, free_ts 364672690724
 set_page_owner include/linux/page_owner.h:32 [inline]
 post_alloc_hook+0x1f3/0x230 mm/page_alloc.c:1470
 prep_new_page mm/page_alloc.c:1478 [inline]
 get_page_from_freelist+0x2cbd/0x2d70 mm/page_alloc.c:3457
 __alloc_pages_noprof+0x256/0x6c0 mm/page_alloc.c:4715
 __alloc_pages_node_noprof include/linux/gfp.h:269 [inline]
 alloc_pages_node_noprof include/linux/gfp.h:296 [inline]
 alloc_slab_page+0x5f/0x120 mm/slub.c:2283
 allocate_slab+0x5a/0x2f0 mm/slub.c:2446
 new_slab mm/slub.c:2499 [inline]
 ___slab_alloc+0xcd1/0x14b0 mm/slub.c:3685
 __slab_alloc+0x58/0xa0 mm/slub.c:3775
 __slab_alloc_node mm/slub.c:3828 [inline]
 slab_alloc_node mm/slub.c:4007 [inline]
 __do_kmalloc_node mm/slub.c:4139 [inline]
 kmalloc_node_track_caller_noprof+0x281/0x440 mm/slub.c:4160
 __do_krealloc mm/slab_common.c:1183 [inline]
 krealloc_noprof+0x7d/0x120 mm/slab_common.c:1216
 bch2_sb_realloc+0x2f5/0x660 fs/bcachefs/super-io.c:189
 __copy_super+0x5dc/0xe70 fs/bcachefs/super-io.c:579
 bch2_sb_to_fs+0xab/0x150 fs/bcachefs/super-io.c:606
 bch2_fs_alloc+0xd36/0x2040 fs/bcachefs/super.c:823
 bch2_fs_open+0x8cc/0xdf0 fs/bcachefs/super.c:2104
 bch2_fs_get_tree+0x750/0x1620 fs/bcachefs/fs.c:1909
 vfs_get_tree+0x90/0x2a0 fs/super.c:1780
page last free pid 5108 tgid 5090 stack trace:
 reset_page_owner include/linux/page_owner.h:25 [inline]
 free_pages_prepare mm/page_alloc.c:1089 [inline]
 free_unref_page+0xd22/0xea0 mm/page_alloc.c:2621
 skb_free_frag include/linux/skbuff.h:3360 [inline]
 skb_free_head net/core/skbuff.c:1079 [inline]
 skb_release_data+0x6b2/0x880 net/core/skbuff.c:1108
 skb_release_all net/core/skbuff.c:1173 [inline]
 __napi_kfree_skb net/core/skbuff.c:1451 [inline]
 kfree_skb_napi_cache net/core/skbuff.c:7010 [inline]
 skb_attempt_defer_free+0x42f/0x5c0 net/core/skbuff.c:7032
 tcp_eat_recv_skb net/ipv4/tcp.c:1522 [inline]
 tcp_recvmsg_locked+0x1500/0x2640 net/ipv4/tcp.c:2546
 tcp_recvmsg+0x25d/0x920 net/ipv4/tcp.c:2592
 inet_recvmsg+0x150/0x2d0 net/ipv4/af_inet.c:885
 sock_recvmsg_nosec net/socket.c:1046 [inline]
 sock_recvmsg+0x1ae/0x280 net/socket.c:1068
 sock_read_iter+0x2ca/0x3e0 net/socket.c:1138
 new_sync_read fs/read_write.c:395 [inline]
 vfs_read+0x9c4/0xbd0 fs/read_write.c:476
 ksys_read+0x1a0/0x2c0 fs/read_write.c:619
 do_syscall_x64 arch/x86/entry/common.c:52 [inline]
 do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
 ffff88805b7e8e80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff88805b7e8f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff88805b7e8f80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                            ^
 ffff88805b7e9000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff88805b7e9080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================