Linux kernel crash report:
pgoff 20000 file 0000000000000000 private_data 0000000000000000
flags: 0x8102172(write|mayread|maywrite|mayexec|growsdown|locked|account|softdirty)
------------[ cut here ]------------
kernel BUG at mm/mmap.c:1108!
invalid opcode: 0000 [#1] PREEMPT SMP KASAN
CPU: 1 PID: 24456 Comm: syz-executor216 Not tainted 6.2.0-syzkaller-13467-g0988a0ea7919 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/02/2023
RIP: 0010:find_mergeable_anon_vma+0x6f0/0x840 mm/mmap.c:1108
Code: df 48 89 fa 48 c1 ea 03 80 3c 02 00 75 6b 48 3b 6b 50 0f 85 08 fb ff ff e9 06 fb ff ff e8 58 ad bf ff 48 89 ef e8 70 a5 fa ff <0f> 0b 4c 89 f7 e8 06 29 11 00 e9 d2 f9 ff ff e8 fc 28 11 00 e9 a0
RSP: 0018:ffffc9000b61f888 EFLAGS: 00010286
RAX: 0000000000000130 RBX: 0000000000000000 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff816931bc RDI: 0000000000000005
RBP: ffff8880783f97e0 R08: 0000000000000005 R09: 0000000000000000
R10: 0000000080000000 R11: 0000000000000000 R12: ffffc9000b61f8b0
R13: 1ffff920016c3f12 R14: ffff8880783f97e8 R15: fff0000000000fff
FS:  00007fe0f1f58700(0000) GS:ffff8880b9900000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffc5b9a8960 CR3: 000000002bd88000 CR4: 00000000003506e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 <TASK>
 __anon_vma_prepare+0x7d/0x580 mm/rmap.c:199
 anon_vma_prepare include/linux/rmap.h:159 [inline]
 do_anonymous_page mm/memory.c:4050 [inline]
 handle_pte_fault mm/memory.c:4907 [inline]
 __handle_mm_fault+0x3a8f/0x3e60 mm/memory.c:5051
 handle_mm_fault+0x2ba/0x9c0 mm/memory.c:5197
 faultin_page mm/gup.c:925 [inline]
 __get_user_pages+0x4da/0xf30 mm/gup.c:1147
 populate_vma_page_range+0x2df/0x420 mm/gup.c:1543
 __mm_populate+0x105/0x3b0 mm/gup.c:1652
 mm_populate include/linux/mm.h:3026 [inline]
 vm_mmap_pgoff+0x201/0x280 mm/util.c:547
 ksys_mmap_pgoff+0x7d/0x5a0 mm/mmap.c:1410
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe0f1fa6959
Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 11 15 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fe0f1f582f8 EFLAGS: 00000246 ORIG_RAX: 0000000000000009
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe0f1fa6959
RDX: 0000000000000002 RSI: 0000000000001000 RDI: 0000000020002000
RBP: 00007fe0f202e3e8 R08: 00000000ffffffff R09: 0000000000000000
R10: 840000000000a132 R11: 0000000000000246 R12: 840000000000a132
R13: 00007fe0f202e3e0 R14: 00007fe0f202e3ec R15: 0000000000022000
 </TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:find_mergeable_anon_vma+0x6f0/0x840 mm/mmap.c:1108
Code: df 48 89 fa 48 c1 ea 03 80 3c 02 00 75 6b 48 3b 6b 50 0f 85 08 fb ff ff e9 06 fb ff ff e8 58 ad bf ff 48 89 ef e8 70 a5 fa ff <0f> 0b 4c 89 f7 e8 06 29 11 00 e9 d2 f9 ff ff e8 fc 28 11 00 e9 a0
RSP: 0018:ffffc9000b61f888 EFLAGS: 00010286
RAX: 0000000000000130 RBX: 0000000000000000 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff816931bc RDI: 0000000000000005
RBP: ffff8880783f97e0 R08: 0000000000000005 R09: 0000000000000000
R10: 0000000080000000 R11: 0000000000000000 R12: ffffc9000b61f8b0
R13: 1ffff920016c3f12 R14: ffff8880783f97e8 R15: fff0000000000fff
FS:  00007fe0f1f58700(0000) GS:ffff8880b9900000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fe0f1ffc01d CR3: 000000002bd88000 CR4: 00000000003506e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400


Fix patch:
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 0722859c3647..a57e6ae78e65 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -774,7 +774,8 @@ struct mm_struct {
 	unsigned long cpu_bitmap[];
 };
 
-#define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN)
+#define MM_MT_FLAGS	(MT_FLAGS_ALLOC_RANGE | MT_FLAGS_LOCK_EXTERN | \
+			 MT_FLAGS_USE_RCU)
 extern struct mm_struct init_mm;
 
 /* Pointer magic because the dynamic array size confuses some compilers. */
diff --git a/kernel/fork.c b/kernel/fork.c
index c0257cbee093..0c92f224c68c 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -617,6 +617,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
 	if (retval)
 		goto out;
 
+	mt_clear_in_rcu(vmi.mas.tree);
 	for_each_vma(old_vmi, mpnt) {
 		struct file *file;
 
@@ -700,6 +701,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
 	retval = arch_dup_mmap(oldmm, mm);
 loop_out:
 	vma_iter_free(&vmi);
+	if (!retval)
+		mt_set_in_rcu(vmi.mas.tree);
 out:
 	mmap_write_unlock(mm);
 	flush_tlb_mm(oldmm);
diff --git a/mm/mmap.c b/mm/mmap.c
index ad499f7b767f..ff68a67a2a7c 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2277,7 +2277,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	int count = 0;
 	int error = -ENOMEM;
 	MA_STATE(mas_detach, &mt_detach, 0, 0);
-	mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN);
+	mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
 	mt_set_external_lock(&mt_detach, &mm->mmap_lock);
 
 	/*
@@ -3037,6 +3037,7 @@ void exit_mmap(struct mm_struct *mm)
 	 */
 	set_bit(MMF_OOM_SKIP, &mm->flags);
 	mmap_write_lock(mm);
+	mt_clear_in_rcu(&mm->mm_mt);
 	free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS,
 		      USER_PGTABLES_CEILING);
 	tlb_finish_mmu(&tlb);