| 0/0 |
2026/11/26 16:09 |
flow |
bisect-cause |
46m
Results: map[BisectReasoning:The bug was introduced by commit `bf4916922c60f43efaa329744b3eef539aa6a2b2` ("hugetlbfs: extend hugetlb_vma_lock to private VMAs").
### Root Cause
In `fs/hugetlbfs/inode.c:hugetlb_unmap_file_folio()`, when `hugetlb_vma_trylock_write(vma)` fails, the code assumes `vma->vm_private_data` is a `struct hugetlb_vma_lock *`:
```c
if (!hugetlb_vma_trylock_write(vma)) {
vma_lock = vma->vm_private_data;
kref_get(&vma_lock->refs);
break;
}
```
and subsequently executes `down_write(&vma_lock->rw_sema)`, `vma = vma_lock->vma`, and `kref_put(&vma_lock->refs, hugetlb_vma_lock_release)`.
Originally, `hugetlb_vma_trylock_write(vma)` (added in `40549ba8f8e0`) returned `1` unconditionally whenever `!__vma_shareable_lock(vma)`, so the `!hugetlb_vma_trylock_write(vma)` branch in `hugetlb_unmap_file_folio()` could only be taken for shareable VMAs whose `vma->vm_private_data` pointed to a `struct hugetlb_vma_lock`.
Commit `bf4916922c60f43efaa329744b3eef539aa6a2b2` extended `hugetlb_vma_trylock_write(vma)` (in `mm/hugetlb.c`) to also attempt `down_write_trylock(&resv_map->rw_sema)` for private VMAs satisfying `__vma_private_lock(vma)`. However, for private hugetlbfs VMAs owning a reservation map, `vma->vm_private_data` holds a `struct resv_map *` with low-bit flags (`HPAGE_RESV_OWNER = 1UL << 0`, `HPAGE_RESV_UNMAPPED = 1UL << 1`) ORed in, not a `struct hugetlb_vma_lock *`.
When `ftruncate()` (`hugetlbfs_setattr()` -> `hugetlb_vmtruncate()` -> `remove_inode_hugepages()` -> `remove_inode_single_folio()` -> `hugetlb_unmap_file_folio()`) encounters a private VMA mapping a pagecache folio while another thread holds `resv_map->rw_sema` (e.g. during `hugetlb_fault()`, `MADV_DONTNEED`, or unmap):
1. `hugetlb_vma_trylock_write(vma)` calls `down_write_trylock(&resv_map->rw_sema)` and returns `0`.
2. `hugetlb_unmap_file_folio()` assigns `vma_lock = vma->vm_private_data` (a `resv_map *` tagged with `HPAGE_RESV_OWNER`, matching `R14: ffff888060cd6801` in the crash registers).
3. `kref_get(&vma_lock->refs)` reads a 32-bit `refcount_t` at offset `+1` into `struct resv_map` (spanning the upper 3 bytes of `resv_map->refs` and the 4-byte padding before `resv_map->lock`), reading `0` and triggering `refcount_t: addition on 0; use-after-free` at `lib/refcount.c:25`.
### Git History Traced
- `40549ba8f8e0` ("hugetlb: use new vma_lock for pmd sharing synchronization"): Introduced `hugetlb_vma_lock` and the `if (!hugetlb_vma_trylock_write(vma))` path in `hugetlb_unmap_file_folio()`. Ruled out because at that time `hugetlb_vma_trylock_write()` always returned `1` for non-shareable VMAs.
- `bf4916922c60` ("hugetlbfs: extend hugetlb_vma_lock to private VMAs"): Changed `hugetlb_vma_trylock_write()` to try locking `resv_map->rw_sema` on private VMAs without updating `hugetlb_unmap_file_folio()`, which continued to treat `vma->vm_private_data` as `struct hugetlb_vma_lock *`.
- `2820b0f09be9` ("hugetlbfs: close race between MADV_DONTNEED and page fault"): Follow-up commit in the same series moving `hugetlb_vma_lock_write()` to `hugetlb_zap_begin()`; ruled out as the primary cause because `hugetlb_fault()` already held `hugetlb_vma_lock_read()` on private VMAs as of `bf4916922c60`.
- `187da0f8250a` ("hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write"): Fixed `__vma_private_lock()` to mask off `HPAGE_RESV_MASK` and check `HPAGE_RESV_OWNER` for child VMAs without a `resv_map`, leaving the `hugetlb_unmap_file_folio()` bug for `HPAGE_RESV_OWNER` private VMAs intact.
- `dd83609b8898` ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list") and `cec944dd329f` ("hugetlbfs: move lock assertions after early returns in huge_pmd_unshare()"): Related fixes in `hugetlb_vmdelete_list()`/`huge_pmd_unshare()` (where `cec944dd329f` reverted `dd83609b8898`); neither introduced the bug in `hugetlb_unmap_file_folio()`. Commits:[map[AuthorEmail:riel@surriel.com AuthorName:Rik van Riel Date:2023-10-05T23:59:07-04:00 Hash:bf4916922c60f43efaa329744b3eef539aa6a2b2 Recipients:[map[Email:akpm@linux-foundation.org Name: To:true] map[Email:mike.kravetz@oracle.com Name: To:true] map[Email:riel@surriel.com Name: To:true]] Title:hugetlbfs: extend hugetlb_vma_lock to private VMAs]] IsRelease:false KernelCommit:0fcb05b72344f40806cbe810de8872f1698bf053]
|
| 1/1 |
2026/11/26 16:09 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:0fcb05b72344f40806cbe810de8872f1698bf053]
|
| 2/1 |
2026/11/26 16:09 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 3/1 |
2026/11/26 16:09 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/55f68612361916f88521406e90445113548fb766/data]
|
| 4/1 |
2026/11/26 16:10 |
action |
kernel-builder |
28m
Results: map[KernelObj:/app/workdir/cache/build/51cb445cb27ab95d0dbb2c9976901d39eab78d71/data]
|
| 5/1 |
2026/11/26 16:38 |
action |
codesearch-prepare |
13m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/26 16:52 |
agent |
cause-finder |
3m
Model:
Results: map[CandidateCommits:[bf4916922c60f43efaa329744b3eef539aa6a2b2]]
Instruction:
You are an experienced Linux kernel developer tasked with identifying the commit
that introduced a kernel bug (cause bisection).
Your investigation strategy:
1. Establish the root cause first.
Analyze the crash report and the code it points to (stack traces, allocation/free stacks,
the involved functions and structs) until you understand the exact invariant that is
violated and the exact execution sequence that leads to the crash.
Identify the specific functions, structs, invariants, or lines of code where the buggy
logic resides.
2. Use git tools (git-blame, git-log, git-show) to trace the history
of the affected functions and lines.
3. Trace past pure refactorings, code movements, formatting changes, or variable renames
to find the commit that actually introduced the buggy logic or broke an existing invariant.
4. Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON,
KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is
incorrect and is the bug.
5. Do NOT return a merge commit unless the bug was introduced by the merge conflict
resolution itself; otherwise identify the underlying non-merge commit.
6. Verify your hypothesis by inspecting the full commit diff with git-show and
confirming how the change introduced the exact condition described in the root cause.
Output rules:
- Returning a SINGLE commit in CandidateCommits (a 1-element list) is the expected norm
and is strongly preferred. A single commit gives maintainers a clear, directly actionable
result, whereas returning extra commits dilutes the report and notifies additional commit
authors unnecessarily.
- Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that
commit in CandidateCommits. Do NOT include secondary or "contributing" commits such as
earlier background/foundation commits, preparatory or follow-up commits in the same
series, later refactorings, incomplete fix attempts, or commits that merely exposed or
enabled the code path (e.g. new callers, default config changes, or sanitizer/debug checks).
Mention those related commits in your text reply for context instead.
- Returning more than one commit (at most 3) is the rare exception, not the norm. Only return
multiple commits when:
1. There is genuine, unresolvable ambiguity between 2-3 equally plausible candidates after
thorough investigation, OR
2. The bug was strictly and inseparably introduced (or fixed) by 2-3 commits together, with
no single primary commit.
In those rare cases, order CandidateCommits by relevance with the most important/likely
commit FIRST.
- If you cannot determine the commit with confidence, return an empty CandidateCommits list
rather than guessing.
- Your text reply must provide a clear bisection log explaining the root cause of the bug,
the git history you traced, the commits you examined (with their hashes and titles), why
you ruled out other commits, and how you reached your conclusion.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
During execution, you may receive periodic guidance in <supervisor_advice> tags;
use it to refine your investigation or wrap up once you have sufficient evidence.
Prompt:
The crash is:
------------[ cut here ]------------
refcount_t: addition on 0; use-after-free.
WARNING: lib/refcount.c:25 at refcount_warn_saturate+0x9f/0x110 lib/refcount.c:25, CPU#0: syz.9.2312/14401
Modules linked in:
CPU: 0 UID: 0 PID: 14401 Comm: syz.9.2312 Tainted: G L syzkaller #0 PREEMPT_{RT,(full)}
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/16/2026
RIP: 0010:refcount_warn_saturate+0x9f/0x110 lib/refcount.c:25
Code: eb 66 85 db 74 3e 83 fb 01 75 4c e8 5b ae f7 fc 48 8d 3d 24 9b ed 0b 67 48 0f b9 3a eb 4a e8 48 ae f7 fc 48 8d 3d 21 9b ed 0b <67> 48 0f b9 3a eb 37 e8 35 ae f7 fc 48 8d 3d 1e 9b ed 0b 67 48 0f
RSP: 0018:ffffc9001448f928 EFLAGS: 00010287
RAX: ffffffff84cf2b08 RBX: 0000000000000002 RCX: 0000000000080000
RDX: ffffc90021f14000 RSI: 00000000000006dc RDI: ffffffff90bcc630
RBP: ffffc9001448fbe8 R08: ffff88805d22ddc0 R09: 0000000000000005
R10: 0000000000000100 R11: 0000000000000004 R12: ffffea0001f58000
R13: dffffc0000000000 R14: ffff888060cd6801 R15: ffff88801bfae128
FS: 00007f1644f766c0(0000) GS:ffff8881249bb000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fff7546e068 CR3: 00000000515a4000 CR4: 00000000003526f0
Call Trace:
<TASK>
__refcount_add include/linux/refcount.h:-1 [inline]
__refcount_inc include/linux/refcount.h:366 [inline]
refcount_inc include/linux/refcount.h:383 [inline]
kref_get include/linux/kref.h:45 [inline]
hugetlb_unmap_file_folio fs/hugetlbfs/inode.c:413 [inline]
remove_inode_single_folio fs/hugetlbfs/inode.c:512 [inline]
remove_inode_hugepages+0xbe0/0x10f0 fs/hugetlbfs/inode.c:577
hugetlb_vmtruncate fs/hugetlbfs/inode.c:621 [inline]
hugetlbfs_setattr+0x4f0/0x6d0 fs/hugetlbfs/inode.c:856
notify_change+0xbb8/0xec0 fs/attr.c:556
do_truncate+0x1c2/0x250 fs/open.c:68
do_ftruncate+0x4e0/0x590 fs/open.c:194
ksys_ftruncate fs/open.c:206 [inline]
__do_sys_ftruncate fs/open.c:211 [inline]
__se_sys_ftruncate fs/open.c:209 [inline]
__x64_sys_ftruncate+0x8f/0xe0 fs/open.c:209
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1646d1e559
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 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 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f1644f76028 EFLAGS: 00000246 ORIG_RAX: 000000000000004d
RAX: ffffffffffffffda RBX: 00007f1646fa5fa0 RCX: 00007f1646d1e559
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000008
RBP: 00007f1646db58b0 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f1646fa6038 R14: 00007f1646fa5fa0 R15: 00007fff5a5cefe8
</TASK>
----------------
Code disassembly (best guess):
0: eb 66 jmp 0x68
2: 85 db test %ebx,%ebx
4: 74 3e je 0x44
6: 83 fb 01 cmp $0x1,%ebx
9: 75 4c jne 0x57
b: e8 5b ae f7 fc call 0xfcf7ae6b
10: 48 8d 3d 24 9b ed 0b lea 0xbed9b24(%rip),%rdi # 0xbed9b3b
17: 67 48 0f b9 3a ud1 (%edx),%rdi
1c: eb 4a jmp 0x68
1e: e8 48 ae f7 fc call 0xfcf7ae6b
23: 48 8d 3d 21 9b ed 0b lea 0xbed9b21(%rip),%rdi # 0xbed9b4b
* 2a: 67 48 0f b9 3a ud1 (%edx),%rdi <-- trapping instruction
2f: eb 37 jmp 0x68
31: e8 35 ae f7 fc call 0xfcf7ae6b
36: 48 8d 3d 1e 9b ed 0b lea 0xbed9b1e(%rip),%rdi # 0xbed9b5b
3d: 67 addr32
3e: 48 rex.W
3f: 0f .byte 0xf
Search for the commit that introduced this bug.
Reply: The bug was introduced by commit `bf4916922c60f43efaa329744b3eef539aa6a2b2` ("hugetlbfs: extend hugetlb_vma_lock to private VMAs").
### Root Cause
In `fs/hugetlbfs/inode.c:hugetlb_unmap_file_folio()`, when `hugetlb_vma_trylock_write(vma)` fails, the code assumes `vma->vm_private_data` is a `struct hugetlb_vma_lock *`:
```c
if (!hugetlb_vma_trylock_write(vma)) {
vma_lock = vma->vm_private_data;
kref_get(&vma_lock->refs);
break;
}
```
and subsequently executes `down_write(&vma_lock->rw_sema)`, `vma = vma_lock->vma`, and `kref_put(&vma_lock->refs, hugetlb_vma_lock_release)`.
Originally, `hugetlb_vma_trylock_write(vma)` (added in `40549ba8f8e0`) returned `1` unconditionally whenever `!__vma_shareable_lock(vma)`, so the `!hugetlb_vma_trylock_write(vma)` branch in `hugetlb_unmap_file_folio()` could only be taken for shareable VMAs whose `vma->vm_private_data` pointed to a `struct hugetlb_vma_lock`.
Commit `bf4916922c60f43efaa329744b3eef539aa6a2b2` extended `hugetlb_vma_trylock_write(vma)` (in `mm/hugetlb.c`) to also attempt `down_write_trylock(&resv_map->rw_sema)` for private VMAs satisfying `__vma_private_lock(vma)`. However, for private hugetlbfs VMAs owning a reservation map, `vma->vm_private_data` holds a `struct resv_map *` with low-bit flags (`HPAGE_RESV_OWNER = 1UL << 0`, `HPAGE_RESV_UNMAPPED = 1UL << 1`) ORed in, not a `struct hugetlb_vma_lock *`.
When `ftruncate()` (`hugetlbfs_setattr()` -> `hugetlb_vmtruncate()` -> `remove_inode_hugepages()` -> `remove_inode_single_folio()` -> `hugetlb_unmap_file_folio()`) encounters a private VMA mapping a pagecache folio while another thread holds `resv_map->rw_sema` (e.g. during `hugetlb_fault()`, `MADV_DONTNEED`, or unmap):
1. `hugetlb_vma_trylock_write(vma)` calls `down_write_trylock(&resv_map->rw_sema)` and returns `0`.
2. `hugetlb_unmap_file_folio()` assigns `vma_lock = vma->vm_private_data` (a `resv_map *` tagged with `HPAGE_RESV_OWNER`, matching `R14: ffff888060cd6801` in the crash registers).
3. `kref_get(&vma_lock->refs)` reads a 32-bit `refcount_t` at offset `+1` into `struct resv_map` (spanning the upper 3 bytes of `resv_map->refs` and the 4-byte padding before `resv_map->lock`), reading `0` and triggering `refcount_t: addition on 0; use-after-free` at `lib/refcount.c:25`.
### Git History Traced
- `40549ba8f8e0` ("hugetlb: use new vma_lock for pmd sharing synchronization"): Introduced `hugetlb_vma_lock` and the `if (!hugetlb_vma_trylock_write(vma))` path in `hugetlb_unmap_file_folio()`. Ruled out because at that time `hugetlb_vma_trylock_write()` always returned `1` for non-shareable VMAs.
- `bf4916922c60` ("hugetlbfs: extend hugetlb_vma_lock to private VMAs"): Changed `hugetlb_vma_trylock_write()` to try locking `resv_map->rw_sema` on private VMAs without updating `hugetlb_unmap_file_folio()`, which continued to treat `vma->vm_private_data` as `struct hugetlb_vma_lock *`.
- `2820b0f09be9` ("hugetlbfs: close race between MADV_DONTNEED and page fault"): Follow-up commit in the same series moving `hugetlb_vma_lock_write()` to `hugetlb_zap_begin()`; ruled out as the primary cause because `hugetlb_fault()` already held `hugetlb_vma_lock_read()` on private VMAs as of `bf4916922c60`.
- `187da0f8250a` ("hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write"): Fixed `__vma_private_lock()` to mask off `HPAGE_RESV_MASK` and check `HPAGE_RESV_OWNER` for child VMAs without a `resv_map`, leaving the `hugetlb_unmap_file_folio()` bug for `HPAGE_RESV_OWNER` private VMAs intact.
- `dd83609b8898` ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list") and `cec944dd329f` ("hugetlbfs: move lock assertions after early returns in huge_pmd_unshare()"): Related fixes in `hugetlb_vmdelete_list()`/`huge_pmd_unshare()` (where `cec944dd329f` reverted `dd83609b8898`); neither introduced the bug in `hugetlb_unmap_file_folio()`.
|
| 7/2 |
2026/11/26 16:52 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 6952
output: 171
thoughts: 304
Thoughts: **My Debugging Journey into the Hugetlb `use-after-free`**
Here I am, diving into this crash report, trying to get to the bottom of this `refcount_t: addition on 0; use-after-free.` warning. The first thing that jumps out is the location: `lib/refcount.c:25`. That's a pretty fundamental place, so this could be tricky. Looking at the stack trace, the action seems to originate from `hugetlbfs_setattr`, leading down through `hugetlb_vmtruncate` and `remove_inode_hugepages`, eventually hitting `remove_inode_single_folio` and `hugetlb_unmap_file_folio`. The crucial part seems to be the `kref_get` call right before the `use-after-free`.
My immediate plan is to meticulously review the source code around the reported functions, specifically in `fs/hugetlbfs/inode.c` from lines 400 to 630. I need to understand the flow of `hugetlb_unmap_file_folio` and its callers, `remove_inode_single_folio` and `remove_inode_hugepages`. To gain historical context and understand the evolution of this code, I'll also be using `git-blame` and `git-log` on these functions. This will help me see who made changes, when, and why, potentially revealing the root cause or prior related issues.
While I'm doing that, to save time and maintain momentum, I'll run `read-file` for the source code inspection and `git-log` in parallel. The goal is to get a comprehensive understanding of the code's current state and its past, which should illuminate this use-after-free issue.
|
| 8/2 |
2026/11/26 16:52 |
tool |
read-file |
0m
Args: map[File:fs/hugetlbfs/inode.c FirstLine:370 LineCount:100]
Results: map[Contents: 370: t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
371: if (t_end > vma->vm_end)
372: t_end = vma->vm_end;
373: return t_end;
374: }
375:
376: /*
377: * Called with hugetlb fault mutex held. Therefore, no more mappings to
378: * this folio can be created while executing the routine.
379: */
380: static void hugetlb_unmap_file_folio(struct hstate *h,
381: struct address_space *mapping,
382: struct folio *folio, pgoff_t index)
383: {
384: struct hugetlb_vma_lock *vma_lock;
385: unsigned long pfn = folio_pfn(folio);
386: struct vm_area_struct *vma;
387: unsigned long v_start;
388: unsigned long v_end;
389: pgoff_t start, end;
390:
391: start = index * pages_per_huge_page(h);
392: end = (index + 1) * pages_per_huge_page(h);
393:
394: i_mmap_lock_write(mapping);
395: retry:
396: vma_lock = NULL;
397: mapping_rmap_tree_foreach(vma, mapping, start, end - 1) {
398: v_start = vma_offset_start(vma, start);
399: v_end = vma_offset_end(vma, end);
400:
401: if (!hugetlb_vma_maps_pfn(vma, v_start, pfn))
402: continue;
403:
404: if (!hugetlb_vma_trylock_write(vma)) {
405: vma_lock = vma->vm_private_data;
406: /*
407: * If we can not get vma lock, we need to drop
408: * immap_sema and take locks in order. First,
409: * take a ref on the vma_lock structure so that
410: * we can be guaranteed it will not go away when
411: * dropping immap_sema.
412: */
413: kref_get(&vma_lock->refs);
414: break;
415: }
416:
417: unmap_hugepage_range(vma, v_start, v_end, NULL,
418: ZAP_FLAG_DROP_MARKER);
419: hugetlb_vma_unlock_write(vma);
420: }
421:
422: i_mmap_unlock_write(mapping);
423:
424: if (vma_lock) {
425: /*
426: * Wait on vma_lock. We know it is still valid as we have
427: * a reference. We must 'open code' vma locking as we do
428: * not know if vma_lock is still attached to vma.
429: */
430: down_write(&vma_lock->rw_sema);
431: i_mmap_lock_write(mapping);
432:
433: vma = vma_lock->vma;
434: if (!vma) {
435: /*
436: * If lock is no longer attached to vma, then just
437: * unlock, drop our reference and retry looking for
438: * other vmas.
439: */
440: up_write(&vma_lock->rw_sema);
441: kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
442: goto retry;
443: }
444:
445: /*
446: * vma_lock is still attached to vma. Check to see if vma
447: * still maps page and if so, unmap.
448: */
449: v_start = vma_offset_start(vma, start);
450: v_end = vma_offset_end(vma, end);
451: if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
452: unmap_hugepage_range(vma, v_start, v_end, NULL,
453: ZAP_FLAG_DROP_MARKER);
454:
455: kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
456: hugetlb_vma_unlock_write(vma);
457:
458: goto retry;
459: }
460: }
461:
462: static void
463: hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
464: pgoff_t end, zap_flags_t zap_flags)
465: {
466: struct vm_area_struct *vma;
467:
468: /*
469: * end == 0 indicates that the entire range after start should be
]
|
| 9/2 |
2026/11/26 16:52 |
tool |
read-file |
0m
Args: map[File:fs/hugetlbfs/inode.c FirstLine:470 LineCount:130]
Results: map[Contents: 470: * unmapped. Note, end is exclusive, whereas the interval tree takes
471: * an inclusive "last".
472: */
473: mapping_rmap_tree_foreach(vma, mapping, start,
474: end ? end - 1 : ULONG_MAX) {
475: unsigned long v_start;
476: unsigned long v_end;
477:
478: if (!hugetlb_vma_trylock_write(vma))
479: continue;
480:
481: v_start = vma_offset_start(vma, start);
482: v_end = vma_offset_end(vma, end);
483:
484: unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
485:
486: /*
487: * Note that vma lock only exists for shared/non-private
488: * vmas. Therefore, lock is not held when calling
489: * unmap_hugepage_range for private vmas.
490: */
491: hugetlb_vma_unlock_write(vma);
492: }
493: }
494:
495: /*
496: * Called with hugetlb fault mutex held.
497: */
498: static void remove_inode_single_folio(struct hstate *h, struct inode *inode,
499: struct address_space *mapping, struct folio *folio,
500: pgoff_t index, bool truncate_op)
501: {
502: /*
503: * If folio is mapped, it was faulted in after being
504: * unmapped in caller or hugetlb_vmdelete_list() skips
505: * unmapping it due to fail to grab lock. Unmap (again)
506: * while holding the fault mutex. The mutex will prevent
507: * faults until we finish removing the folio. Hold folio
508: * lock to guarantee no concurrent migration.
509: */
510: folio_lock(folio);
511: if (unlikely(folio_mapped(folio)))
512: hugetlb_unmap_file_folio(h, mapping, folio, index);
513:
514: /*
515: * We must remove the folio from page cache before removing
516: * the region/ reserve map (hugetlb_unreserve_pages). In
517: * rare out of memory conditions, removal of the region/reserve
518: * map could fail. Correspondingly, the subpool and global
519: * reserve usage count can need to be adjusted.
520: */
521: VM_BUG_ON_FOLIO(folio_test_hugetlb_restore_reserve(folio), folio);
522: hugetlb_delete_from_page_cache(folio);
523: if (!truncate_op) {
524: if (unlikely(hugetlb_unreserve_pages(inode, index,
525: index + 1, 1)))
526: hugetlb_fix_reserve_counts(inode);
527: }
528:
529: folio_unlock(folio);
530: }
531:
532: /*
533: * remove_inode_hugepages handles two distinct cases: truncation and hole
534: * punch. There are subtle differences in operation for each case.
535: *
536: * truncation is indicated by end of range being LLONG_MAX
537: * In this case, we first scan the range and release found pages.
538: * After releasing pages, hugetlb_unreserve_pages cleans up region/reserve
539: * maps and global counts. Page faults can race with truncation.
540: * During faults, hugetlb_no_page() checks i_size before page allocation,
541: * and again after obtaining page table lock. It will 'back out'
542: * allocations in the truncated range.
543: * hole punch is indicated if end is not LLONG_MAX
544: * In the hole punch case we scan the range and release found pages.
545: * Only when releasing a page is the associated region/reserve map
546: * deleted. The region/reserve map for ranges without associated
547: * pages are not modified. Page faults can race with hole punch.
548: * This is indicated if we find a mapped page.
549: * Note: If the passed end of range value is beyond the end of file, but
550: * not LLONG_MAX this routine still performs a hole punch operation.
551: */
552: static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
553: loff_t lend)
554: {
555: struct hstate *h = hstate_inode(inode);
556: struct address_space *mapping = &inode->i_data;
557: const pgoff_t end = lend >> PAGE_SHIFT;
558: struct folio_batch fbatch;
559: pgoff_t next, index;
560: int i, freed = 0;
561: bool truncate_op = (lend == LLONG_MAX);
562:
563: folio_batch_init(&fbatch);
564: next = lstart >> PAGE_SHIFT;
565: while (filemap_get_folios(mapping, &next, end - 1, &fbatch)) {
566: for (i = 0; i < folio_batch_count(&fbatch); ++i) {
567: struct folio *folio = fbatch.folios[i];
568: u32 hash = 0;
569:
]
|
| 10/2 |
2026/11/26 16:52 |
tool |
git-log |
0m
Args: map[SourcePath:fs/hugetlbfs/inode.c SymbolName:hugetlb_unmap_file_folio]
Results: map[Output:4b7a90a48b99 mm/rmap: rename vma_interval_tree_*() to mapping_rmap_tree_*()
7a67b96af069 mm/rmap: parameterise vma_interval_tree_*() by address_space
fa17ad58f832 hugetlb: convert hugetlb_vma_maps_page() to hugetlb_vma_maps_pfn()
243b1f2d3b09 mm/hugetlb: let vma_offset_start() to return start
40549ba8f8e0 hugetlb: use new vma_lock for pmd sharing synchronization
378397ccb8e5 hugetlb: create hugetlb_unmap_file_folio to unmap single file folio
]
|
| 11/2 |
2026/11/26 16:52 |
tool |
git-log |
0m
Args: map[SourcePath:fs/hugetlbfs/inode.c SymbolName:remove_inode_single_folio]
Results: map[Output:34ca46cc6fc9 fs: hugetlb: simplify remove_inode_hugepages() return type
7b7387650dcf mm/hugetlb: fix folio is still mapped when deleted
ece62684dcfb hugetlbfs: convert hugetlb_delete_from_page_cache() to use folios
fa27759af4a6 hugetlb: clean up code checking for fault/truncation races
378397ccb8e5 hugetlb: create hugetlb_unmap_file_folio to unmap single file folio
c86272287bc6 hugetlb: create remove_inode_single_folio to remove single file folio
188a39725ad7 hugetlbfs: revert use i_mmap_rwsem to address page fault/truncate race
1935ebd3cf6c hugetlbfs: fix some comment typos
87bf91d39bb5 hugetlbfs: Use i_mmap_rwsem to address page fault/truncate race
e7c58097793e hugetlbfs: revert "Use i_mmap_rwsem to fix page fault/truncate race"
]
|
| 12/2 |
2026/11/26 16:52 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 11236
output: 168
thoughts: 314
Thoughts: **Unpacking a Hugetlb Unmapping Race Condition Hypothesis**
Okay, so I'm deep-diving into the `hugetlb_unmap_file_folio` function, specifically looking at lines 404-415. I've just encountered a conditional block that immediately caught my eye:
My immediate reaction is a pretty strong "WHOA!". This section is dealing with a situation where the write lock for the VMA (`vma`) cannot be acquired directly via `hugetlb_vma_trylock_write`. The comment explains that in this scenario, the `immap_sema` needs to be dropped, and locks must be acquired in a specific order. Crucially, before dropping `immap_sema`, a reference count (`kref_get`) is taken on `vma->vm_private_data` to ensure its stability.
This smells like a potential race condition, or at least a very carefully choreographed locking dance to *avoid* one. The fact that `vma->vm_private_data` is being used to store a lock (`vma_lock`) and that its reference count needs to be managed so explicitly when the primary VMA lock fails, is highly suggestive.
My next logical step, driven by this initial suspicion, is to really dig into the mechanics of these components. I need to understand:
* **`hugetlb_vma_trylock_write(vma)`**: What exactly does this function do? What are the conditions under which it might fail? Does its failure imply an immediate race, or is it just a performance optimization that leads to a more complex locking path?
* **`vma->vm_private_data`**: What is the typical usage of `vm_private_data` in the context of hugetlb? How is it being utilized here to hold `vma_lock`? What is the lifecycle of this `vm_private_data` structure, and why is `kref_get` necessary?
* **`vma_lock`**: What is the structure of `vma_lock`? What specific locks does it contain or manage? How does its reference counting (`refs`) work?
* **The overall locking scheme**: How does this `vma_lock` interact with other locks, particularly `immap_sema` and potentially other VMA-related locks, during unmapping operations? The comment mentions "take locks in order," which implies a strict hierarchy is being enforced.
To get a comprehensive understanding, I'm going to leverage my tools. I plan to use `codesearch-find-references` and `codesearch-definition-source` on `hugetlb_vma_trylock_write`, `hugetlb_vma_lock` (assuming this is the underlying locking mechanism `trylock_write` uses), `hugetlb_vma_lock_release`, and `vm_private_data` specifically within the hugetlb subsystem. This will allow me to trace the definitions, usages, and the interconnectedness of these critical data structures and functions, which should illuminate how this particular locking strategy is designed to maintain data integrity during concurrent hugetlb unmapping.
|
| 13/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args: map[Name:hugetlb_vma_trylock_write]
Results: map[SourceCode: 344: int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
345: {
346:
347: if (__vma_shareable_lock(vma)) {
348: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
349:
350: return down_write_trylock(&vma_lock->rw_sema);
351: } else if (__vma_private_lock(vma)) {
352: struct resv_map *resv_map = vma_resv_map(vma);
353:
354: return down_write_trylock(&resv_map->rw_sema);
355: }
356:
357: return 1;
358: }
SourceFile:mm/hugetlb.c]
|
| 14/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args: map[Name:hugetlb_vma_lock_release]
Results: map[SourceCode: 373: void hugetlb_vma_lock_release(struct kref *kref)
374: {
375: struct hugetlb_vma_lock *vma_lock = container_of(kref,
376: struct hugetlb_vma_lock, refs);
377:
378: kfree(vma_lock);
379: }
SourceFile:mm/hugetlb.c]
|
| 15/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args: map[Name:hugetlb_vma_lock]
Results: map[SourceCode: 104: struct hugetlb_vma_lock {
105: struct kref refs;
106: struct rw_semaphore rw_sema;
107: struct vm_area_struct *vma;
108: };
SourceFile:include/linux/hugetlb.h]
|
| 16/2 |
2026/11/26 16:52 |
tool |
codesearch-find-references |
0m
Args: map[Name:hugetlb_vma_lock SourceTreePrefix:mm/]
Results: map[References:[map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:__hugetlb_vma_unlock_write_free SourceFile:mm/hugetlb.c SourceLine:399] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:__hugetlb_vma_unlock_write_put SourceFile:mm/hugetlb.c SourceLine:381] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_dup_vma_private SourceFile:mm/hugetlb.c SourceLine:1199] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vm_op_open SourceFile:mm/hugetlb.c SourceLine:4705] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_assert_locked SourceFile:mm/hugetlb.c SourceLine:363] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_alloc SourceFile:mm/hugetlb.c SourceLine:428] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_free SourceFile:mm/hugetlb.c SourceLine:419] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_read SourceFile:mm/hugetlb.c SourceLine:295] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_release SourceFile:mm/hugetlb.c SourceLine:375] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_release SourceFile:mm/hugetlb.c SourceLine:375] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_release SourceFile:mm/hugetlb.c SourceLine:375] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_release SourceFile:mm/hugetlb.c SourceLine:375] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_lock_write SourceFile:mm/hugetlb.c SourceLine:321] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_trylock_write SourceFile:mm/hugetlb.c SourceLine:348] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_unlock_read SourceFile:mm/hugetlb.c SourceLine:308] map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_vma_unlock_write SourceFile:mm/hugetlb.c SourceLine:334]] TruncatedOutput:false]
|
| 17/2 |
2026/11/26 16:52 |
tool |
codesearch-find-references |
0m
Args: map[Name:hugetlb_vma_lock SourceTreePrefix:fs/]
Results: map[References:[map[ReferenceKind:uses ReferencingEntityKind:function ReferencingEntityName:hugetlb_unmap_file_folio SourceFile:fs/hugetlbfs/inode.c SourceLine:384]] TruncatedOutput:false]
|
| 18/2 |
2026/11/26 16:52 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 13228
output: 121
thoughts: 523
Thoughts: **Rethinking Hugetlb VMA Lock Handling and Potential Race Conditions**
Okay, I've been scrutinizing the `hugetlb_vma_trylock_write` function, and a rather unsettling thought has just popped into my head. I'm looking at the logic there, specifically:
if (__vma_shareable_lock(vma)) {
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
return down_write_trylock(&vma_lock->rw_sema);
} else if (__vma_private_lock(vma)) {
struct resv_map *resv_map = vma_resv_map(vma);
return down_write_trylock(&resv_map->rw_sema);
}
And then, my gaze falls upon `hugetlb_unmap_file_folio`:
if (!hugetlb_vma_trylock_write(vma)) {
vma_lock = vma->vm_private_data;
kref_get(&vma_lock->refs);
break;
}
This is where the alarm bells start ringing. I'm immediately thinking, "Wait a minute! What happens if `__vma_private_lock(vma)` is true in `hugetlb_vma_trylock_write`? Or, more broadly, what is the *exact* nature of `vma->vm_private_data` when we're dealing with a private VMA? And what if that `vma_lock->refs` in `hugetlb_unmap_file_folio` could potentially be zero while the `vma` is still supposedly part of the memory map tree?"
I need to explore both scenarios.
First, let's consider the **private VMA case (`__vma_private_lock(vma)` is true)**. I'm digging into the definitions, and it seems `vma->vm_private_data` should resolve to a `struct resv_map *`. Now, within `struct resv_map`, the very first field is `struct kref refs;`. The question is, could `resv_map->refs` ever be zero in this context? I'm not entirely convinced it couldn't be.
Second, let's look at the **shared VMA case (`__vma_shareable_lock(vma)` is true)**. The more pressing concern here is how `vma_lock->refs` could possibly reach zero *while* the `vma` is still registered within the rmap (Reverse Map) tree. This seems like a potential window for a race condition.
To get to the bottom of this, I absolutely must dive deeper into `mm/hugetlb.c`, specifically around lines 270-460. I also need to thoroughly examine the structure and lifecycle of `resv_map`, and importantly, trace the unlock and free paths, particularly `__hugetlb_vma_unlock_write_free` and `hugetlb_vma_lock_free`. My gut feeling is there's a subtle interaction here I'm not fully grasping yet, and a potential for a critical race.
|
| 19/2 |
2026/11/26 16:52 |
tool |
read-file |
0m
Args: map[File:mm/hugetlb.c FirstLine:260 LineCount:180]
Results: map[Contents: 260: if (spool->max_hpages != -1) /* maximum size accounting */
261: spool->used_hpages -= delta;
262:
263: /* minimum size accounting */
264: if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
265: if (spool->rsv_hpages + delta <= spool->min_hpages)
266: ret = 0;
267: else
268: ret = spool->rsv_hpages + delta - spool->min_hpages;
269:
270: spool->rsv_hpages += delta;
271: if (spool->rsv_hpages > spool->min_hpages)
272: spool->rsv_hpages = spool->min_hpages;
273: }
274:
275: /*
276: * If hugetlbfs_put_super couldn't free spool due to an outstanding
277: * quota reference, free it now.
278: */
279: unlock_or_release_subpool(spool, flags);
280:
281: return ret;
282: }
283:
284: static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
285: {
286: return subpool_inode(file_inode(vma->vm_file));
287: }
288:
289: /*
290: * hugetlb vma_lock helper routines
291: */
292: void hugetlb_vma_lock_read(struct vm_area_struct *vma)
293: {
294: if (__vma_shareable_lock(vma)) {
295: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
296:
297: down_read(&vma_lock->rw_sema);
298: } else if (__vma_private_lock(vma)) {
299: struct resv_map *resv_map = vma_resv_map(vma);
300:
301: down_read(&resv_map->rw_sema);
302: }
303: }
304:
305: void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
306: {
307: if (__vma_shareable_lock(vma)) {
308: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
309:
310: up_read(&vma_lock->rw_sema);
311: } else if (__vma_private_lock(vma)) {
312: struct resv_map *resv_map = vma_resv_map(vma);
313:
314: up_read(&resv_map->rw_sema);
315: }
316: }
317:
318: void hugetlb_vma_lock_write(struct vm_area_struct *vma)
319: {
320: if (__vma_shareable_lock(vma)) {
321: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
322:
323: down_write(&vma_lock->rw_sema);
324: } else if (__vma_private_lock(vma)) {
325: struct resv_map *resv_map = vma_resv_map(vma);
326:
327: down_write(&resv_map->rw_sema);
328: }
329: }
330:
331: void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
332: {
333: if (__vma_shareable_lock(vma)) {
334: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
335:
336: up_write(&vma_lock->rw_sema);
337: } else if (__vma_private_lock(vma)) {
338: struct resv_map *resv_map = vma_resv_map(vma);
339:
340: up_write(&resv_map->rw_sema);
341: }
342: }
343:
344: int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
345: {
346:
347: if (__vma_shareable_lock(vma)) {
348: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
349:
350: return down_write_trylock(&vma_lock->rw_sema);
351: } else if (__vma_private_lock(vma)) {
352: struct resv_map *resv_map = vma_resv_map(vma);
353:
354: return down_write_trylock(&resv_map->rw_sema);
355: }
356:
357: return 1;
358: }
359:
]
|
| 20/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args: map[Name:__vma_shareable_lock]
Results: map[SourceCode:1353: static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
1354: {
1355: return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
1356: }
SourceFile:include/linux/hugetlb.h]
|
| 21/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args: map[Name:__vma_private_lock]
Results: map[SourceCode:1176: bool __vma_private_lock(struct vm_area_struct *vma)
1177: {
1178: return !(vma->vm_flags & VM_MAYSHARE) &&
1179: get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
1180: is_vma_resv_set(vma, HPAGE_RESV_OWNER);
1181: }
SourceFile:mm/hugetlb.c]
|
| 22/2 |
2026/11/26 16:52 |
tool |
codesearch-definition-source |
0m
Args:
Results: map[SourceCode: 50: struct resv_map {
51: struct kref refs;
52: spinlock_t lock;
53: struct list_head regions;
54: long adds_in_progress;
55: struct list_head region_cache;
56: long region_cache_count;
57: struct rw_semaphore rw_sema;
58: #ifdef CONFIG_CGROUP_HUGETLB
59: /*
60: * On private mappings, the counter to uncharge reservations is stored
61: * here. If these fields are 0, then either the mapping is shared, or
62: * cgroup accounting is disabled for this resv_map.
63: */
64: struct page_counter *reservation_counter;
65: unsigned long pages_per_hpage;
66: struct cgroup_subsys_state *css;
67: #endif
68: };
SourceFile:include/linux/hugetlb.h]
|
| 23/2 |
2026/11/26 16:52 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 16042
output: 161
thoughts: 55
Thoughts: **My Deep Dive into Hugetlb Locking Mechanisms**
Alright, I've just finished a deep dive into the Hugetlb subsystem in the kernel, specifically focusing on the locking mechanisms around `__vma_private_lock` and `hugetlb_vma_lock`. My approach was to dissect the relevant code within `mm/hugetlb.c`, targeting lines 360 to 460, and then cross-reference that with the commit history using `git log` for those specific lock functions. I was really trying to get to the bottom of how these locks are acquired and released, what their scope is, and what potential race conditions or deadlocks might arise, especially in concurrent scenarios involving huge page management. By examining the source code directly and then tracing its evolution, I'm building a robust understanding of the underlying synchronization primitives that protect critical sections related to VMA manipulation and huge page states.
|
| 24/2 |
2026/11/26 16:52 |
tool |
read-file |
0m
Args: map[File:mm/hugetlb.c FirstLine:360 LineCount:100]
Results: map[Contents: 360: void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
361: {
362: if (__vma_shareable_lock(vma)) {
363: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
364:
365: lockdep_assert_held(&vma_lock->rw_sema);
366: } else if (__vma_private_lock(vma)) {
367: struct resv_map *resv_map = vma_resv_map(vma);
368:
369: lockdep_assert_held(&resv_map->rw_sema);
370: }
371: }
372:
373: void hugetlb_vma_lock_release(struct kref *kref)
374: {
375: struct hugetlb_vma_lock *vma_lock = container_of(kref,
376: struct hugetlb_vma_lock, refs);
377:
378: kfree(vma_lock);
379: }
380:
381: static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
382: {
383: struct vm_area_struct *vma = vma_lock->vma;
384:
385: /*
386: * vma_lock structure may or not be released as a result of put,
387: * it certainly will no longer be attached to vma so clear pointer.
388: * Semaphore synchronizes access to vma_lock->vma field.
389: */
390: vma_lock->vma = NULL;
391: vma->vm_private_data = NULL;
392: up_write(&vma_lock->rw_sema);
393: kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
394: }
395:
396: static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
397: {
398: if (__vma_shareable_lock(vma)) {
399: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
400:
401: __hugetlb_vma_unlock_write_put(vma_lock);
402: } else if (__vma_private_lock(vma)) {
403: struct resv_map *resv_map = vma_resv_map(vma);
404:
405: /* no free for anon vmas, but still need to unlock */
406: up_write(&resv_map->rw_sema);
407: }
408: }
409:
410: static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
411: {
412: /*
413: * Only present in sharable vmas.
414: */
415: if (!vma || !__vma_shareable_lock(vma))
416: return;
417:
418: if (vma->vm_private_data) {
419: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
420:
421: down_write(&vma_lock->rw_sema);
422: __hugetlb_vma_unlock_write_put(vma_lock);
423: }
424: }
425:
426: static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
427: {
428: struct hugetlb_vma_lock *vma_lock;
429:
430: /* Only establish in (flags) sharable vmas */
431: if (!vma || !(vma->vm_flags & VM_MAYSHARE))
432: return;
433:
434: /* Should never get here with non-NULL vm_private_data */
435: if (vma->vm_private_data)
436: return;
437:
438: vma_lock = kmalloc_obj(*vma_lock);
439: if (!vma_lock) {
440: /*
441: * If we can not allocate structure, then vma can not
442: * participate in pmd sharing. This is only a possible
443: * performance enhancement and memory saving issue.
444: * However, the lock is also used to synchronize page
445: * faults with truncation. If the lock is not present,
446: * unlikely races could leave pages in a file past i_size
447: * until the file is removed. Warn in the unlikely case of
448: * allocation failure.
449: */
450: pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
451: return;
452: }
453:
454: kref_init(&vma_lock->refs);
455: init_rwsem(&vma_lock->rw_sema);
456: vma_lock->vma = vma;
457: vma->vm_private_data = vma_lock;
458: }
459:
]
|
| 25/2 |
2026/11/26 16:52 |
tool |
read-file |
0m
Args: map[File:mm/hugetlb.c FirstLine:1160 LineCount:80]
Results: map[Contents:1160:
1161: static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
1162: {
1163: VM_WARN_ON_ONCE_VMA(!is_vm_hugetlb_page(vma), vma);
1164: VM_WARN_ON_ONCE_VMA(vma_test(vma, VMA_MAYSHARE_BIT), vma);
1165:
1166: set_vma_private_data(vma, get_vma_private_data(vma) | flags);
1167: }
1168:
1169: static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1170: {
1171: VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1172:
1173: return (get_vma_private_data(vma) & flag) != 0;
1174: }
1175:
1176: bool __vma_private_lock(struct vm_area_struct *vma)
1177: {
1178: return !(vma->vm_flags & VM_MAYSHARE) &&
1179: get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
1180: is_vma_resv_set(vma, HPAGE_RESV_OWNER);
1181: }
1182:
1183: void hugetlb_dup_vma_private(struct vm_area_struct *vma)
1184: {
1185: VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1186: /*
1187: * Clear vm_private_data
1188: * - For shared mappings this is a per-vma semaphore that may be
1189: * allocated in a subsequent call to hugetlb_vm_op_open.
1190: * Before clearing, make sure pointer is not associated with vma
1191: * as this will leak the structure. This is the case when called
1192: * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already
1193: * been called to allocate a new structure.
1194: * - For MAP_PRIVATE mappings, this is the reserve map which does
1195: * not apply to children. Faults generated by the children are
1196: * not guaranteed to succeed, even if read-only.
1197: */
1198: if (vma->vm_flags & VM_MAYSHARE) {
1199: struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1200:
1201: if (vma_lock && vma_lock->vma != vma)
1202: vma->vm_private_data = NULL;
1203: } else {
1204: vma->vm_private_data = NULL;
1205: }
1206: }
1207:
1208: /*
1209: * Reset and decrement one ref on hugepage private reservation.
1210: * Called with mm->mmap_lock writer semaphore held.
1211: * This function should be only used by mremap and operate on
1212: * same sized vma. It should never come here with last ref on the
1213: * reservation.
1214: */
1215: void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1216: {
1217: /*
1218: * Clear the old hugetlb private page reservation.
1219: * It has already been transferred to new_vma.
1220: *
1221: * During a mremap() operation of a hugetlb vma we call move_vma()
1222: * which copies vma into new_vma and unmaps vma. After the copy
1223: * operation both new_vma and vma share a reference to the resv_map
1224: * struct, and at that point vma is about to be unmapped. We don't
1225: * want to return the reservation to the pool at unmap of vma because
1226: * the reservation still lives on in new_vma, so simply decrement the
1227: * ref here and remove the resv_map reference from this vma.
1228: */
1229: struct resv_map *reservations = vma_resv_map(vma);
1230:
1231: if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1232: resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
1233: kref_put(&reservations->refs, resv_map_release);
1234: }
1235:
1236: hugetlb_dup_vma_private(vma);
1237: }
1238:
1239: static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio)
]
|
| 26/2 |
2026/11/26 16:52 |
tool |
git-log |
0m
Args: map[SourcePath:mm/hugetlb.c SymbolName:hugetlb_vma_trylock_write]
Results: map[Output:bf4916922c60 hugetlbfs: extend hugetlb_vma_lock to private VMAs
e700898fa075 hugetlb: really allocate vma lock for all sharable vmas
]
|
| 27/2 |
2026/11/26 16:53 |
tool |
git-log |
0m
Args: map[SourcePath:mm/hugetlb.c SymbolName:__vma_private_lock]
Results: map[Output:187da0f8250a hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write
]
|
| 28/2 |
2026/11/26 16:53 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 19495
output: 87
thoughts: 103
Thoughts: **Okay, so I'm looking at these commits, `bf4916922c60` and `187da0f8250a`, and my immediate thought is "what's going on here with hugetlbfs and VMA locks?" The first one, `bf4916922c60`, seems to be extending the `hugetlb_vma_lock` to cover private VMAs. That's a significant change, suggesting a need for more robust locking in that area. The second one, `187da0f8250a`, is titled "fix null-ptr-deref in hugetlb_vma_lock_write". That's a red flag – a null pointer dereference is never good, and in a locking function, it could be particularly nasty. This immediately makes me want to dive deeper. My next step is to use `git show` on both these commits to get the full context: the commit message, the diff, the author, the date, everything. I need to understand *why* the lock was extended and *why* that null pointer dereference was happening. And to fully grasp the implications of `hugetlb_vma_lock_write` and its potential issues, I should also examine `hugetlb_unmap_file_folio`. This function likely interacts with the VMA and the unmapping process, and understanding its behavior in conjunction with the changes in these commits will be crucial for a complete picture of the situation.**
|
| 29/2 |
2026/11/26 16:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit bf4916922c60f43efaa329744b3eef539aa6a2b2
Author: Rik van Riel <riel@surriel.com>
Date: Thu Oct 5 23:59:07 2023 -0400
hugetlbfs: extend hugetlb_vma_lock to private VMAs
Extend the locking scheme used to protect shared hugetlb mappings from
truncate vs page fault races, in order to protect private hugetlb mappings
(with resv_map) against MADV_DONTNEED.
Add a read-write semaphore to the resv_map data structure, and use that
from the hugetlb_vma_(un)lock_* functions, in preparation for closing the
race between MADV_DONTNEED and page faults.
Link: https://lkml.kernel.org/r/20231006040020.3677377-3-riel@surriel.com
Fixes: 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing")
Signed-off-by: Rik van Riel <riel@surriel.com>
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index a30686e649f7..065ec022cbb0 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -60,6 +60,7 @@ struct resv_map {
long adds_in_progress;
struct list_head region_cache;
long region_cache_count;
+ struct rw_semaphore rw_sema;
#ifdef CONFIG_CGROUP_HUGETLB
/*
* On private mappings, the counter to uncharge reservations is stored
@@ -1233,6 +1234,11 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
}
+static inline bool __vma_private_lock(struct vm_area_struct *vma)
+{
+ return (!(vma->vm_flags & VM_MAYSHARE)) && vma->vm_private_data;
+}
+
/*
* Safe version of huge_pte_offset() to check the locks. See comments
* above huge_pte_offset().
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 467fedd42453..d218ee2b6a99 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -97,6 +97,7 @@ static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
unsigned long start, unsigned long end);
+static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
static inline bool subpool_is_free(struct hugepage_subpool *spool)
{
@@ -267,6 +268,10 @@ void hugetlb_vma_lock_read(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
down_read(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ down_read(&resv_map->rw_sema);
}
}
@@ -276,6 +281,10 @@ void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
up_read(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ up_read(&resv_map->rw_sema);
}
}
@@ -285,6 +294,10 @@ void hugetlb_vma_lock_write(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
down_write(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ down_write(&resv_map->rw_sema);
}
}
@@ -294,17 +307,27 @@ void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
up_write(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ up_write(&resv_map->rw_sema);
}
}
int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
{
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
- if (!__vma_shareable_lock(vma))
- return 1;
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
- return down_write_trylock(&vma_lock->rw_sema);
+ return down_write_trylock(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ return down_write_trylock(&resv_map->rw_sema);
+ }
+
+ return 1;
}
void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
@@ -313,6 +336,10 @@ void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
lockdep_assert_held(&vma_lock->rw_sema);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ lockdep_assert_held(&resv_map->rw_sema);
}
}
@@ -345,6 +372,11 @@ static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
__hugetlb_vma_unlock_write_put(vma_lock);
+ } else if (__vma_private_lock(vma)) {
+ struct resv_map *resv_map = vma_resv_map(vma);
+
+ /* no free for anon vmas, but still need to unlock */
+ up_write(&resv_map->rw_sema);
}
}
@@ -1068,6 +1100,7 @@ struct resv_map *resv_map_alloc(void)
kref_init(&resv_map->refs);
spin_lock_init(&resv_map->lock);
INIT_LIST_HEAD(&resv_map->regions);
+ init_rwsem(&resv_map->rw_sema);
resv_map->adds_in_progress = 0;
/*
]
|
| 30/2 |
2026/11/26 16:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 187da0f8250aa94bd96266096aef6f694e0b4cd2
Author: Mike Kravetz <mike.kravetz@oracle.com>
Date: Mon Nov 13 17:20:33 2023 -0800
hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write
The routine __vma_private_lock tests for the existence of a reserve map
associated with a private hugetlb mapping. A pointer to the reserve map
is in vma->vm_private_data. __vma_private_lock was checking the pointer
for NULL. However, it is possible that the low bits of the pointer could
be used as flags. In such instances, vm_private_data is not NULL and not
a valid pointer. This results in the null-ptr-deref reported by syzbot:
general protection fault, probably for non-canonical address 0xdffffc000000001d:
0000 [#1] PREEMPT SMP KASAN
KASAN: null-ptr-deref in range [0x00000000000000e8-0x00000000000000ef]
CPU: 0 PID: 5048 Comm: syz-executor139 Not tainted 6.6.0-rc7-syzkaller-00142-g88
8cf78c29e2 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 1
0/09/2023
RIP: 0010:__lock_acquire+0x109/0x5de0 kernel/locking/lockdep.c:5004
...
Call Trace:
<TASK>
lock_acquire kernel/locking/lockdep.c:5753 [inline]
lock_acquire+0x1ae/0x510 kernel/locking/lockdep.c:5718
down_write+0x93/0x200 kernel/locking/rwsem.c:1573
hugetlb_vma_lock_write mm/hugetlb.c:300 [inline]
hugetlb_vma_lock_write+0xae/0x100 mm/hugetlb.c:291
__hugetlb_zap_begin+0x1e9/0x2b0 mm/hugetlb.c:5447
hugetlb_zap_begin include/linux/hugetlb.h:258 [inline]
unmap_vmas+0x2f4/0x470 mm/memory.c:1733
exit_mmap+0x1ad/0xa60 mm/mmap.c:3230
__mmput+0x12a/0x4d0 kernel/fork.c:1349
mmput+0x62/0x70 kernel/fork.c:1371
exit_mm kernel/exit.c:567 [inline]
do_exit+0x9ad/0x2a20 kernel/exit.c:861
__do_sys_exit kernel/exit.c:991 [inline]
__se_sys_exit kernel/exit.c:989 [inline]
__x64_sys_exit+0x42/0x50 kernel/exit.c:989
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
Mask off low bit flags before checking for NULL pointer. In addition, the
reserve map only 'belongs' to the OWNER (parent in parent/child
relationships) so also check for the OWNER flag.
Link: https://lkml.kernel.org/r/20231114012033.259600-1-mike.kravetz@oracle.com
Reported-by: syzbot+6ada951e7c0f7bc8a71e@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/linux-mm/00000000000078d1e00608d7878b@google.com/
Fixes: bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: Rik van Riel <riel@surriel.com>
Cc: Edward Adam Davis <eadavis@qq.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Tom Rix <trix@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index d3acecc5db4b..236ec7b63c54 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -1268,10 +1268,7 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
}
-static inline bool __vma_private_lock(struct vm_area_struct *vma)
-{
- return (!(vma->vm_flags & VM_MAYSHARE)) && vma->vm_private_data;
-}
+bool __vma_private_lock(struct vm_area_struct *vma);
/*
* Safe version of huge_pte_offset() to check the locks. See comments
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 1169ef2f2176..6feb3e0630d1 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1182,6 +1182,13 @@ static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
return (get_vma_private_data(vma) & flag) != 0;
}
+bool __vma_private_lock(struct vm_area_struct *vma)
+{
+ return !(vma->vm_flags & VM_MAYSHARE) &&
+ get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
+ is_vma_resv_set(vma, HPAGE_RESV_OWNER);
+}
+
void hugetlb_dup_vma_private(struct vm_area_struct *vma)
{
VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
]
|
| 31/2 |
2026/11/26 16:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 378397ccb8e5a695a42e819df545ccd28641b683
Author: Mike Kravetz <mike.kravetz@oracle.com>
Date: Wed Sep 14 15:18:08 2022 -0700
hugetlb: create hugetlb_unmap_file_folio to unmap single file folio
Create the new routine hugetlb_unmap_file_folio that will unmap a single
file folio. This is refactored code from hugetlb_vmdelete_list. It is
modified to do locking within the routine itself and check whether the
page is mapped within a specific vma before unmapping.
This refactoring will be put to use and expanded upon in a subsequent
patch adding vma specific locking.
Link: https://lkml.kernel.org/r/20220914221810.95771-8-mike.kravetz@oracle.com
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: James Houghton <jthoughton@google.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mina Almasry <almasrymina@google.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Prakash Sangappa <prakash.sangappa@oracle.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 7112a9a9f54d..3bb1772fce2f 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -371,6 +371,94 @@ static void hugetlb_delete_from_page_cache(struct page *page)
delete_from_page_cache(page);
}
+/*
+ * Called with i_mmap_rwsem held for inode based vma maps. This makes
+ * sure vma (and vm_mm) will not go away. We also hold the hugetlb fault
+ * mutex for the page in the mapping. So, we can not race with page being
+ * faulted into the vma.
+ */
+static bool hugetlb_vma_maps_page(struct vm_area_struct *vma,
+ unsigned long addr, struct page *page)
+{
+ pte_t *ptep, pte;
+
+ ptep = huge_pte_offset(vma->vm_mm, addr,
+ huge_page_size(hstate_vma(vma)));
+
+ if (!ptep)
+ return false;
+
+ pte = huge_ptep_get(ptep);
+ if (huge_pte_none(pte) || !pte_present(pte))
+ return false;
+
+ if (pte_page(pte) == page)
+ return true;
+
+ return false;
+}
+
+/*
+ * Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
+ * No, because the interval tree returns us only those vmas
+ * which overlap the truncated area starting at pgoff,
+ * and no vma on a 32-bit arch can span beyond the 4GB.
+ */
+static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
+{
+ if (vma->vm_pgoff < start)
+ return (start - vma->vm_pgoff) << PAGE_SHIFT;
+ else
+ return 0;
+}
+
+static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end)
+{
+ unsigned long t_end;
+
+ if (!end)
+ return vma->vm_end;
+
+ t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
+ if (t_end > vma->vm_end)
+ t_end = vma->vm_end;
+ return t_end;
+}
+
+/*
+ * Called with hugetlb fault mutex held. Therefore, no more mappings to
+ * this folio can be created while executing the routine.
+ */
+static void hugetlb_unmap_file_folio(struct hstate *h,
+ struct address_space *mapping,
+ struct folio *folio, pgoff_t index)
+{
+ struct rb_root_cached *root = &mapping->i_mmap;
+ struct page *page = &folio->page;
+ struct vm_area_struct *vma;
+ unsigned long v_start;
+ unsigned long v_end;
+ pgoff_t start, end;
+
+ start = index * pages_per_huge_page(h);
+ end = (index + 1) * pages_per_huge_page(h);
+
+ i_mmap_lock_write(mapping);
+
+ vma_interval_tree_foreach(vma, root, start, end - 1) {
+ v_start = vma_offset_start(vma, start);
+ v_end = vma_offset_end(vma, end);
+
+ if (!hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page))
+ continue;
+
+ unmap_hugepage_range(vma, vma->vm_start + v_start, v_end,
+ NULL, ZAP_FLAG_DROP_MARKER);
+ }
+
+ i_mmap_unlock_write(mapping);
+}
+
static void
hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
zap_flags_t zap_flags)
@@ -383,30 +471,13 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
* an inclusive "last".
*/
vma_interval_tree_foreach(vma, root, start, end ? end - 1 : ULONG_MAX) {
- unsigned long v_offset;
+ unsigned long v_start;
unsigned long v_end;
- /*
- * Can the expression below overflow on 32-bit arches?
- * No, because the interval tree returns us only those vmas
- * which overlap the truncated area starting at pgoff,
- * and no vma on a 32-bit arch can span beyond the 4GB.
- */
- if (vma->vm_pgoff < start)
- v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
- else
- v_offset = 0;
-
- if (!end)
- v_end = vma->vm_end;
- else {
- v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
- + vma->vm_start;
- if (v_end > vma->vm_end)
- v_end = vma->vm_end;
- }
+ v_start = vma_offset_start(vma, start);
+ v_end = vma_offset_end(vma, end);
- unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
+ unmap_hugepage_range(vma, vma->vm_start + v_start, v_end,
NULL, zap_flags);
}
}
@@ -428,14 +499,8 @@ static bool remove_inode_single_folio(struct hstate *h, struct inode *inode,
* the fault mutex. The mutex will prevent faults
* until we finish removing the folio.
*/
- if (unlikely(folio_mapped(folio))) {
- i_mmap_lock_write(mapping);
- hugetlb_vmdelete_list(&mapping->i_mmap,
- index * pages_per_huge_page(h),
- (index + 1) * pages_per_huge_page(h),
- ZAP_FLAG_DROP_MARKER);
- i_mmap_unlock_write(mapping);
- }
+ if (unlikely(folio_mapped(folio)))
+ hugetlb_unmap_file_folio(h, mapping, folio, index);
folio_lock(folio);
/*
]
|
| 32/2 |
2026/11/26 16:53 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 26353
output: 32
thoughts: 53
Thoughts: **My Thought Process on `40549ba8f8e0` and `vma_lock`**
Alright, so I've just encountered this commit, `40549ba8f8e0`, titled "hugetlb: use new vma_lock for pmd sharing synchronization". My immediate reaction is, "Okay, I need to dig into this." It's not just about *that* commit, though; the key here is understanding the *mechanism* it's introducing or leveraging: `vma_lock`. I need to get a handle on exactly how `vma_lock` operates. My primary focus will be on its internal workings, its synchronization properties, and what makes it suitable for use in the context of pmd sharing within the hugetlb subsystem. This isn't a superficial read; I need to go deep into the implementation details of `vma_lock` to fully grasp the implications of its adoption in this specific area.
|
| 33/2 |
2026/11/26 16:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 40549ba8f8e0ed1f8b235979563f619e9aa34fdf
Author: Mike Kravetz <mike.kravetz@oracle.com>
Date: Wed Sep 14 15:18:09 2022 -0700
hugetlb: use new vma_lock for pmd sharing synchronization
The new hugetlb vma lock is used to address this race:
Faulting thread Unsharing thread
... ...
ptep = huge_pte_offset()
or
ptep = huge_pte_alloc()
...
i_mmap_lock_write
lock page table
ptep invalid <------------------------ huge_pmd_unshare()
Could be in a previously unlock_page_table
sharing process or worse i_mmap_unlock_write
...
The vma_lock is used as follows:
- During fault processing. The lock is acquired in read mode before
doing a page table lock and allocation (huge_pte_alloc). The lock is
held until code is finished with the page table entry (ptep).
- The lock must be held in write mode whenever huge_pmd_unshare is
called.
Lock ordering issues come into play when unmapping a page from all
vmas mapping the page. The i_mmap_rwsem must be held to search for the
vmas, and the vma lock must be held before calling unmap which will
call huge_pmd_unshare. This is done today in:
- try_to_migrate_one and try_to_unmap_ for page migration and memory
error handling. In these routines we 'try' to obtain the vma lock and
fail to unmap if unsuccessful. Calling routines already deal with the
failure of unmapping.
- hugetlb_vmdelete_list for truncation and hole punch. This routine
also tries to acquire the vma lock. If it fails, it skips the
unmapping. However, we can not have file truncation or hole punch
fail because of contention. After hugetlb_vmdelete_list, truncation
and hole punch call remove_inode_hugepages. remove_inode_hugepages
checks for mapped pages and call hugetlb_unmap_file_page to unmap them.
hugetlb_unmap_file_page is designed to drop locks and reacquire in the
correct order to guarantee unmap success.
Link: https://lkml.kernel.org/r/20220914221810.95771-9-mike.kravetz@oracle.com
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: James Houghton <jthoughton@google.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mina Almasry <almasrymina@google.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Prakash Sangappa <prakash.sangappa@oracle.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 3bb1772fce2f..009ae539b9b2 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -434,6 +434,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
struct folio *folio, pgoff_t index)
{
struct rb_root_cached *root = &mapping->i_mmap;
+ struct hugetlb_vma_lock *vma_lock;
struct page *page = &folio->page;
struct vm_area_struct *vma;
unsigned long v_start;
@@ -444,7 +445,8 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
end = (index + 1) * pages_per_huge_page(h);
i_mmap_lock_write(mapping);
-
+retry:
+ vma_lock = NULL;
vma_interval_tree_foreach(vma, root, start, end - 1) {
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
@@ -452,11 +454,63 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
if (!hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page))
continue;
+ if (!hugetlb_vma_trylock_write(vma)) {
+ vma_lock = vma->vm_private_data;
+ /*
+ * If we can not get vma lock, we need to drop
+ * immap_sema and take locks in order. First,
+ * take a ref on the vma_lock structure so that
+ * we can be guaranteed it will not go away when
+ * dropping immap_sema.
+ */
+ kref_get(&vma_lock->refs);
+ break;
+ }
+
unmap_hugepage_range(vma, vma->vm_start + v_start, v_end,
NULL, ZAP_FLAG_DROP_MARKER);
+ hugetlb_vma_unlock_write(vma);
}
i_mmap_unlock_write(mapping);
+
+ if (vma_lock) {
+ /*
+ * Wait on vma_lock. We know it is still valid as we have
+ * a reference. We must 'open code' vma locking as we do
+ * not know if vma_lock is still attached to vma.
+ */
+ down_write(&vma_lock->rw_sema);
+ i_mmap_lock_write(mapping);
+
+ vma = vma_lock->vma;
+ if (!vma) {
+ /*
+ * If lock is no longer attached to vma, then just
+ * unlock, drop our reference and retry looking for
+ * other vmas.
+ */
+ up_write(&vma_lock->rw_sema);
+ kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
+ goto retry;
+ }
+
+ /*
+ * vma_lock is still attached to vma. Check to see if vma
+ * still maps page and if so, unmap.
+ */
+ v_start = vma_offset_start(vma, start);
+ v_end = vma_offset_end(vma, end);
+ if (hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page))
+ unmap_hugepage_range(vma, vma->vm_start + v_start,
+ v_end, NULL,
+ ZAP_FLAG_DROP_MARKER);
+
+ kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
+ hugetlb_vma_unlock_write(vma);
+
+ goto retry;
+ }
}
static void
@@ -474,11 +528,21 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
unsigned long v_start;
unsigned long v_end;
+ if (!hugetlb_vma_trylock_write(vma))
+ continue;
+
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
unmap_hugepage_range(vma, vma->vm_start + v_start, v_end,
NULL, zap_flags);
+
+ /*
+ * Note that vma lock only exists for shared/non-private
+ * vmas. Therefore, lock is not held when calling
+ * unmap_hugepage_range for private vmas.
+ */
+ hugetlb_vma_unlock_write(vma);
}
}
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index f44b79998ac2..d78504959df7 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4796,6 +4796,14 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
mmu_notifier_invalidate_range_start(&range);
mmap_assert_write_locked(src);
raw_write_seqcount_begin(&src->write_protect_seq);
+ } else {
+ /*
+ * For shared mappings the vma lock must be held before
+ * calling huge_pte_offset in the src vma. Otherwise, the
+ * returned ptep could go away if part of a shared pmd and
+ * another thread calls huge_pmd_unshare.
+ */
+ hugetlb_vma_lock_read(src_vma);
}
last_addr_mask = hugetlb_mask_last_page(h);
@@ -4942,6 +4950,8 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
if (cow) {
raw_write_seqcount_end(&src->write_protect_seq);
mmu_notifier_invalidate_range_end(&range);
+ } else {
+ hugetlb_vma_unlock_read(src_vma);
}
return ret;
@@ -5000,6 +5010,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
mmu_notifier_invalidate_range_start(&range);
last_addr_mask = hugetlb_mask_last_page(h);
/* Prevent race with file truncation */
+ hugetlb_vma_lock_write(vma);
i_mmap_lock_write(mapping);
for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
src_pte = huge_pte_offset(mm, old_addr, sz);
@@ -5031,6 +5042,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
flush_tlb_range(vma, old_end - len, old_end);
mmu_notifier_invalidate_range_end(&range);
i_mmap_unlock_write(mapping);
+ hugetlb_vma_unlock_write(vma);
return len + old_addr - old_end;
}
@@ -5350,8 +5362,29 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
* may get SIGKILLed if it later faults.
*/
if (outside_reserve) {
+ struct address_space *mapping = vma->vm_file->f_mapping;
+ pgoff_t idx;
+ u32 hash;
+
put_page(old_page);
+ /*
+ * Drop hugetlb_fault_mutex and vma_lock before
+ * unmapping. unmapping needs to hold vma_lock
+ * in write mode. Dropping vma_lock in read mode
+ * here is OK as COW mappings do not interact with
+ * PMD sharing.
+ *
+ * Reacquire both after unmap operation.
+ */
+ idx = vma_hugecache_offset(h, vma, haddr);
+ hash = hugetlb_fault_mutex_hash(mapping, idx);
+ hugetlb_vma_unlock_read(vma);
+ mutex_unlock(&hugetlb_fault_mutex_table[hash]);
+
unmap_ref_private(mm, vma, old_page, haddr);
+
+ mutex_lock(&hugetlb_fault_mutex_table[hash]);
+ hugetlb_vma_lock_read(vma);
spin_lock(ptl);
ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
if (likely(ptep &&
@@ -5500,14 +5533,16 @@ static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma,
};
/*
- * hugetlb_fault_mutex and i_mmap_rwsem must be
+ * vma_lock and hugetlb_fault_mutex must be
* dropped before handling userfault. Reacquire
* after handling fault to make calling code simpler.
*/
+ hugetlb_vma_unlock_read(vma);
hash = hugetlb_fault_mutex_hash(mapping, idx);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
ret = handle_userfault(&vmf, reason);
mutex_lock(&hugetlb_fault_mutex_table[hash]);
+ hugetlb_vma_lock_read(vma);
return ret;
}
@@ -5741,6 +5776,11 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
if (ptep) {
+ /*
+ * Since we hold no locks, ptep could be stale. That is
+ * OK as we are only making decisions based on content and
+ * not actually modifying content here.
+ */
entry = huge_ptep_get(ptep);
if (unlikely(is_hugetlb_entry_migration(entry))) {
migration_entry_wait_huge(vma, ptep);
@@ -5748,23 +5788,35 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
return VM_FAULT_HWPOISON_LARGE |
VM_FAULT_SET_HINDEX(hstate_index(h));
- } else {
- ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h));
- if (!ptep)
- return VM_FAULT_OOM;
}
- mapping = vma->vm_file->f_mapping;
- idx = vma_hugecache_offset(h, vma, haddr);
-
/*
* Serialize hugepage allocation and instantiation, so that we don't
* get spurious allocation failures if two CPUs race to instantiate
* the same page in the page cache.
*/
+ mapping = vma->vm_file->f_mapping;
+ idx = vma_hugecache_offset(h, vma, haddr);
hash = hugetlb_fault_mutex_hash(mapping, idx);
mutex_lock(&hugetlb_fault_mutex_table[hash]);
+ /*
+ * Acquire vma lock before calling huge_pte_alloc and hold
+ * until finished with ptep. This prevents huge_pmd_unshare from
+ * being called elsewhere and making the ptep no longer valid.
+ *
+ * ptep could have already be assigned via huge_pte_offset. That
+ * is OK, as huge_pte_alloc will return the same value unless
+ * something has changed.
+ */
+ hugetlb_vma_lock_read(vma);
+ ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h));
+ if (!ptep) {
+ hugetlb_vma_unlock_read(vma);
+ mutex_unlock(&hugetlb_fault_mutex_table[hash]);
+ return VM_FAULT_OOM;
+ }
+
entry = huge_ptep_get(ptep);
/* PTE markers should be handled the same way as none pte */
if (huge_pte_none_mostly(entry)) {
@@ -5825,6 +5877,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
unlock_page(pagecache_page);
put_page(pagecache_page);
}
+ hugetlb_vma_unlock_read(vma);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
return handle_userfault(&vmf, VM_UFFD_WP);
}
@@ -5868,6 +5921,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
put_page(pagecache_page);
}
out_mutex:
+ hugetlb_vma_unlock_read(vma);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
/*
* Generally it's safe to hold refcount during waiting page lock. But
@@ -6330,8 +6384,9 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
flush_cache_range(vma, range.start, range.end);
mmu_notifier_invalidate_range_start(&range);
- last_addr_mask = hugetlb_mask_last_page(h);
+ hugetlb_vma_lock_write(vma);
i_mmap_lock_write(vma->vm_file->f_mapping);
+ last_addr_mask = hugetlb_mask_last_page(h);
for (; address < end; address += psize) {
spinlock_t *ptl;
ptep = huge_pte_offset(mm, address, psize);
@@ -6430,6 +6485,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
* See Documentation/mm/mmu_notifier.rst
*/
i_mmap_unlock_write(vma->vm_file->f_mapping);
+ hugetlb_vma_unlock_write(vma);
mmu_notifier_invalidate_range_end(&range);
return pages << h->order;
@@ -6931,6 +6987,7 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
pud_t *pud = pud_offset(p4d, addr);
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ hugetlb_vma_assert_locked(vma);
BUG_ON(page_count(virt_to_page(ptep)) == 0);
if (page_count(virt_to_page(ptep)) == 1)
return 0;
@@ -6943,6 +7000,31 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
+void hugetlb_vma_lock_read(struct vm_area_struct *vma)
+{
+}
+
+void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
+{
+}
+
+void hugetlb_vma_lock_write(struct vm_area_struct *vma)
+{
+}
+
+void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
+{
+}
+
+int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
+{
+ return 1;
+}
+
+void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
+{
+}
+
void hugetlb_vma_lock_release(struct kref *kref)
{
}
@@ -7325,6 +7407,7 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
start, end);
mmu_notifier_invalidate_range_start(&range);
+ hugetlb_vma_lock_write(vma);
i_mmap_lock_write(vma->vm_file->f_mapping);
for (address = start; address < end; address += PUD_SIZE) {
ptep = huge_pte_offset(mm, address, sz);
@@ -7336,6 +7419,7 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
}
flush_hugetlb_tlb_range(vma, start, end);
i_mmap_unlock_write(vma->vm_file->f_mapping);
+ hugetlb_vma_unlock_write(vma);
/*
* No need to call mmu_notifier_invalidate_range(), see
* Documentation/mm/mmu_notifier.rst.
diff --git a/mm/memory.c b/mm/memory.c
index c01c12500169..b3ed17219d77 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1684,10 +1684,12 @@ static void unmap_single_vma(struct mmu_gather *tlb,
if (vma->vm_file) {
zap_flags_t zap_flags = details ?
details->zap_flags : 0;
+ hugetlb_vma_lock_write(vma);
i_mmap_lock_write(vma->vm_file->f_mapping);
__unmap_hugepage_range_final(tlb, vma, start, end,
NULL, zap_flags);
i_mmap_unlock_write(vma->vm_file->f_mapping);
+ hugetlb_vma_unlock_write(vma);
}
} else
unmap_page_range(tlb, vma, start, end, details);
diff --git a/mm/rmap.c b/mm/rmap.c
index 0e179c823e0a..b6743c2b8b5f 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1551,24 +1551,39 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
* To call huge_pmd_unshare, i_mmap_rwsem must be
* held in write mode. Caller needs to explicitly
* do this outside rmap routines.
+ *
+ * We also must hold hugetlb vma_lock in write mode.
+ * Lock order dictates acquiring vma_lock BEFORE
+ * i_mmap_rwsem. We can only try lock here and fail
+ * if unsuccessful.
*/
- VM_BUG_ON(!anon && !(flags & TTU_RMAP_LOCKED));
- if (!anon && huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
- flush_tlb_range(vma, range.start, range.end);
- mmu_notifier_invalidate_range(mm, range.start,
- range.end);
-
- /*
- * The ref count of the PMD page was dropped
- * which is part of the way map counting
- * is done for shared PMDs. Return 'true'
- * here. When there is no other sharing,
- * huge_pmd_unshare returns false and we will
- * unmap the actual page and drop map count
- * to zero.
- */
- page_vma_mapped_walk_done(&pvmw);
- break;
+ if (!anon) {
+ VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
+ if (!hugetlb_vma_trylock_write(vma)) {
+ page_vma_mapped_walk_done(&pvmw);
+ ret = false;
+ break;
+ }
+ if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
+ hugetlb_vma_unlock_write(vma);
+ flush_tlb_range(vma,
+ range.start, range.end);
+ mmu_notifier_invalidate_range(mm,
+ range.start, range.end);
+ /*
+ * The ref count of the PMD page was
+ * dropped which is part of the way map
+ * counting is done for shared PMDs.
+ * Return 'true' here. When there is
+ * no other sharing, huge_pmd_unshare
+ * returns false and we will unmap the
+ * actual page and drop map count
+ * to zero.
+ */
+ page_vma_mapped_walk_done(&pvmw);
+ break;
+ }
+ hugetlb_vma_unlock_write(vma);
}
pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
} else {
@@ -1926,26 +1941,41 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
* To call huge_pmd_unshare, i_mmap_rwsem must be
* held in write mode. Caller needs to explicitly
* do this outside rmap routines.
+ *
+ * We also must hold hugetlb vma_lock in write mode.
+ * Lock order dictates acquiring vma_lock BEFORE
+ * i_mmap_rwsem. We can only try lock here and
+ * fail if unsuccessful.
*/
- VM_BUG_ON(!anon && !(flags & TTU_RMAP_LOCKED));
- if (!anon && huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
- flush_tlb_range(vma, range.start, range.end);
- mmu_notifier_invalidate_range(mm, range.start,
- range.end);
-
- /*
- * The ref count of the PMD page was dropped
- * which is part of the way map counting
- * is done for shared PMDs. Return 'true'
- * here. When there is no other sharing,
- * huge_pmd_unshare returns false and we will
- * unmap the actual page and drop map count
- * to zero.
- */
- page_vma_mapped_walk_done(&pvmw);
- break;
+ if (!anon) {
+ VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
+ if (!hugetlb_vma_trylock_write(vma)) {
+ page_vma_mapped_walk_done(&pvmw);
+ ret = false;
+ break;
+ }
+ if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
+ hugetlb_vma_unlock_write(vma);
+ flush_tlb_range(vma,
+ range.start, range.end);
+ mmu_notifier_invalidate_range(mm,
+ range.start, range.end);
+
+ /*
+ * The ref count of the PMD page was
+ * dropped which is part of the way map
+ * counting is done for shared PMDs.
+ * Return 'true' here. When there is
+ * no other sharing, huge_pmd_unshare
+ * returns false and we will unmap the
+ * actual page and drop map count
+ * to zero.
+ */
+ page_vma_mapped_walk_done(&pvmw);
+ break;
+ }
+ hugetlb_vma_unlock_write(vma);
}
-
/* Nuke the hugetlb page table entry */
pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
} else {
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 0fdbd2c05587..e24e8a47ce8a 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -379,16 +379,21 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
BUG_ON(dst_addr >= dst_start + len);
/*
- * Serialize via hugetlb_fault_mutex.
+ * Serialize via vma_lock and hugetlb_fault_mutex.
+ * vma_lock ensures the dst_pte remains valid even
+ * in the case of shared pmds. fault mutex prevents
+ * races with other faulting threads.
*/
idx = linear_page_index(dst_vma, dst_addr);
mapping = dst_vma->vm_file->f_mapping;
hash = hugetlb_fault_mutex_hash(mapping, idx);
mutex_lock(&hugetlb_fault_mutex_table[hash]);
+ hugetlb_vma_lock_read(dst_vma);
err = -ENOMEM;
dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
if (!dst_pte) {
+ hugetlb_vma_unlock_read(dst_vma);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
goto out_unlock;
}
@@ -396,6 +401,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
if (mode != MCOPY_ATOMIC_CONTINUE &&
!huge_pte_none_mostly(huge_ptep_get(dst_pte))) {
err = -EEXIST;
+ hugetlb_vma_unlock_read(dst_vma);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
goto out_unlock;
}
@@ -404,6 +410,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,
dst_addr, src_addr, mode, &page,
wp_copy);
+ hugetlb_vma_unlock_read(dst_vma);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
cond_resched();
]
|
| 34/2 |
2026/11/26 16:53 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 34808
output: 144
thoughts: 1367
Thoughts: **Initial Diagnostic Pause and Hypothesis Formulation**
Alright, let's hit the brakes and really scrutinize *both* scenarios we're looking at. I need to understand precisely what's happening. My immediate attention is drawn to the crash registers and the code within `hugetlb_unmap_file_folio`. I'm seeing `R14` holding `ffff888060cd6801`. The `01` at the end there is what's catching my eye. Could `R14` actually be `vma->vm_private_data`, and if so, is it potentially pointing to something related to `HPAGE_RESV_OWNER` or `HPAGE_RESV_UNMAPPED`? Or is there another explanation for this value? I absolutely must avoid jumping to conclusions; I need to explore both of these possibilities thoroughly.
**Hypothesis A: Private VMA Data within `hugetlb_unmap_file_folio`**
Let's dive into Hypothesis A first: the idea that `vma->vm_private_data` is involved in a significant way within `hugetlb_unmap_file_folio`. I'm looking at the `fs/hugetlbfs/inode.c` implementation of `hugetlb_unmap_file_folio`. The relevant section iterates through VMAs in the `mapping_rmap_tree`:
mapping_rmap_tree_foreach(vma, mapping, start, end - 1) {
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
if (!hugetlb_vma_maps_pfn(vma, v_start, pfn))
continue;
if (!hugetlb_vma_trylock_write(vma)) {
vma_lock = vma->vm_private_data;
/*
* If we can not get vma lock, we need to drop
* immap_sema and take locks in order. First,
* take a ref on the vma_lock structure so that
* we can be guaranteed it will not go away when
* dropping immap_sema.
*/
kref_get(&vma_lock->refs);
break;
}
Now, this is where things get interesting. What if `vma` represents a *private* hugetlb mapping (i.e., `MAP_PRIVATE` on a hugetlbfs file)? In such a case, `!(vma->vm_flags & VM_MAYSHARE)` is true, *and* the VMA is still present in `mapping->i_mmap`. I need to confirm if private hugetlbfs file mappings are indeed registered in `mapping->i_mmap`. My understanding is that `vma_link` and `mapping_rmap_tree_insert` will insert *any* file-backed VMA, whether shared or private, into `vma->vm_file->f_mapping->i_mmap`.
Now, let's consider the condition for `__vma_private_lock(vma)` being true in a private mapping:
This implies that if `__vma_private_lock(vma)` is true, `vma->vm_private_data` will have the `HPAGE_RESV_OWNER` bit set. What exactly is stored in `vma->vm_private_data` when `__vma_private_lock(vma)` is true? It's a combination: `(void *)((unsigned long)resv_map | flags)`, where `flags` includes `HPAGE_RESV_OWNER` (which is bit 1). I need to go and check the definitions of `HPAGE_RESV_*` bits in `mm/hugetlb.c` to be absolutely sure.
This brings me back to the value in `R14` – `ffff888060cd6801`. If `vma->vm_private_data` is a `struct resv_map *` *ORed* with `HPAGE_RESV_OWNER` (which is `0x2`), or even `HPAGE_RESV_UNMAPPED` (`0x1`), the value would end in `01` or `02` or `03`. This would mean `vma_lock` is an *unaligned pointer*!
And if `vma_lock` is an unaligned pointer, then the subsequent `kref_get(&vma_lock->refs)` would be attempting to read unaligned memory. This is precisely what could trigger a KASAN error related to an alignment or unaligned reference count read. The error message mentioning `refcount_t: addition on 0; use-after-free` is peculiar, though – why not a more direct KASAN OOB error? This difference warrants further investigation.
So, to recap this path: If `vma->vm_private_data` contains `HPAGE_RESV_OWNER` or `HPAGE_RESV_UNMAPPED` flags, it means `vma->vm_private_data` is not a direct pointer to a `struct hugetlb_vma_lock` (as it might be for a shared VMA), but rather a masked value. The `hugetlb_vma_trylock_write(vma)` call, which uses `vma_resv_map(vma)` (masking out `HPAGE_RESV_MASK`), would fail if another thread holds a lock (like `hugetlb_vma_lock_read(vma)` or `hugetlb_vma_lock_write(vma)`) on that private VMA, perhaps due to actions like `MADV_DONTNEED`, `hugetlb_fault`, or `unmap_vmas`. When this lock acquisition fails, `hugetlb_unmap_file_folio` proceeds to `vma_lock = vma->vm_private_data; kref_get(&vma_lock->refs);`. If `vma->vm_private_data` is indeed an unaligned pointer due to the flag, this `kref_get` is the culprit.
**Cross-Checking and Further Investigation**
This brings me to the critical question: what *is* `vma->vm_private_data` when `__vma_private_lock(vma)` is true? Is it a `struct hugetlb_vma_lock *` in the shared VMA case, or a `struct resv_map *` in the private VMA case? I need to verify this by examining the logic in `hugetlb_vma_lock_free`, `hugetlb_vma_lock_alloc`, and `reset_vma_resv_huge_pages`, as well as the uses of `vma_lock` throughout the hugetlb code. My goal is to ensure I fully understand the types and values being manipulated when `vma->vm_private_data` ends up in `R14`. I must investigate both possibilities – the shared VMA `struct hugetlb_vma_lock` and the private VMA `struct resv_map *` – to definitively understand the cause of the observed behavior.
|
| 35/2 |
2026/11/26 16:53 |
tool |
grepper |
0m
Args: map[Expression:HPAGE_RESV_ PathPrefixes:[mm/hugetlb.c include/linux/hugetlb.h]]
Results: map[Output:mm/hugetlb.c=1017=static pgoff_t vma_hugecache_offset(struct hstate *h,
--
mm/hugetlb.c-1027- */
mm/hugetlb.c:1028:#define HPAGE_RESV_OWNER (1UL << 0)
mm/hugetlb.c:1029:#define HPAGE_RESV_UNMAPPED (1UL << 1)
mm/hugetlb.c:1030:#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
mm/hugetlb.c-1031-
--
mm/hugetlb.c=1138=static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
--
mm/hugetlb.c-1148- return (struct resv_map *)(get_vma_private_data(vma) &
mm/hugetlb.c:1149: ~HPAGE_RESV_MASK);
mm/hugetlb.c-1150- }
--
mm/hugetlb.c=1176=bool __vma_private_lock(struct vm_area_struct *vma)
--
mm/hugetlb.c-1178- return !(vma->vm_flags & VM_MAYSHARE) &&
mm/hugetlb.c:1179: get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
mm/hugetlb.c:1180: is_vma_resv_set(vma, HPAGE_RESV_OWNER);
mm/hugetlb.c-1181-}
--
mm/hugetlb.c=1215=void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
--
mm/hugetlb.c-1230-
mm/hugetlb.c:1231: if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
mm/hugetlb.c-1232- resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
--
mm/hugetlb.c=2427=static long __vma_reservation_common(struct hstate *h,
--
mm/hugetlb.c-2485- /*
mm/hugetlb.c:2486: * We know private mapping must have HPAGE_RESV_OWNER set.
mm/hugetlb.c-2487- *
--
mm/hugetlb.c=4680=static void hugetlb_vm_op_open(struct vm_area_struct *vma)
--
mm/hugetlb.c-4684- /*
mm/hugetlb.c:4685: * HPAGE_RESV_OWNER indicates a private mapping.
mm/hugetlb.c-4686- * This new VMA should share its siblings reservation map if present.
--
mm/hugetlb.c-4692- */
mm/hugetlb.c:4693: if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
mm/hugetlb.c-4694- resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
--
mm/hugetlb.c=4720=static void hugetlb_vm_op_close(struct vm_area_struct *vma)
--
mm/hugetlb.c-4730- resv = vma_resv_map(vma);
mm/hugetlb.c:4731: if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
mm/hugetlb.c-4732- return;
--
mm/hugetlb.c=5210=void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
--
mm/hugetlb.c-5295- */
mm/hugetlb.c:5296: set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
mm/hugetlb.c-5297- } else {
--
mm/hugetlb.c=5435=static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
--
mm/hugetlb.c-5476- */
mm/hugetlb.c:5477: if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
mm/hugetlb.c-5478- unmap_hugepage_range(iter_vma, address,
--
mm/hugetlb.c=5491=static vm_fault_t hugetlb_wp(struct vm_fault *vmf)
--
mm/hugetlb.c-5561- */
mm/hugetlb.c:5562: if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
mm/hugetlb.c-5563- folio_test_anon(old_folio))
--
mm/hugetlb.c=5763=static vm_fault_t hugetlb_no_page(struct address_space *mapping,
--
mm/hugetlb.c-5782- */
mm/hugetlb.c:5783: if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
mm/hugetlb.c-5784- pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
--
mm/hugetlb.c=6673=long hugetlb_reserve_pages(struct inode *inode,
--
mm/hugetlb.c-6731- set_vma_resv_map(vma, resv_map);
mm/hugetlb.c:6732: set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
mm/hugetlb.c-6733- }
--
mm/hugetlb.c-6853- region_abort(resv_map, from, to, regions_needed);
mm/hugetlb.c:6854: if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
mm/hugetlb.c-6855- kref_put(&resv_map->refs, resv_map_release);
]
|
| 36/2 |
2026/11/26 16:53 |
tool |
codesearch-find-references |
0m
Args: map[Name:hugetlb_vma_lock_free]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_reserve_pages SourceFile:mm/hugetlb.c SourceLine:6847] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_vm_op_close SourceFile:mm/hugetlb.c SourceLine:4728]] TruncatedOutput:false]
|
| 37/2 |
2026/11/26 16:53 |
tool |
codesearch-find-references |
0m
Args: map[Name:hugetlb_vma_lock_alloc]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_reserve_pages SourceFile:mm/hugetlb.c SourceLine:6696] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_vm_op_open SourceFile:mm/hugetlb.c SourceLine:4710] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_vm_op_open SourceFile:mm/hugetlb.c SourceLine:4715]] TruncatedOutput:false]
|
| 38/2 |
2026/11/26 16:53 |
tool |
codesearch-find-references |
0m
Args: map[Name:__hugetlb_vma_unlock_write_free]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__hugetlb_zap_end SourceFile:mm/hugetlb.c SourceLine:5400]] TruncatedOutput:false]
|
| 39/2 |
2026/11/26 16:53 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 38454
output: 96
thoughts: 519
Thoughts: **The Revelation in the Crash Report**
I'm staring at this crash report, and something just clicked. I need to look at `R14` again. There it is: `R14: ffff888060cd6801`. This value... it feels familiar. I pull up the definition for `HPAGE_RESV_OWNER`: `#define HPAGE_RESV_OWNER (1UL << 0)`, which is `0x1`.
Suddenly, it dawns on me: `ffff888060cd6801` is a slab pointer, specifically `0xffff888060cd6800`, bitwise OR-ed with `HPAGE_RESV_OWNER` (`0x1`).
Now, I need to dig into the `struct resv_map`. I know it has a `struct kref refs` field at offset 0. So, if `vma_lock` is `0xffff888060cd6801`, then `&vma_lock->refs` should be pointing to `0xffff888060cd6801`.
The question then becomes: what exactly is at `0xffff888060cd6800`? It's a `struct resv_map`. Let's break down its layout. Offset 0-3 is `refs`. If the initial value of `refs` is 1, in little-endian, that's `01 00 00 00`. Offset 4-7 is `spinlock_t lock`. If it's unlocked, that's `00 00 00 00`.
Now, consider what happens when we read a 32-bit integer at offset 1 through 4. That's `0xffff888060cd6801`. Looking at the bytes, we take `00` from `refs` (bytes 1, 2, 3) and the first byte of `lock` (`00`). This gives us `0x00000000`!
This is it! This is *why* `refcount_inc` at `0xffff888060cd6801` saw a value of `0` and printed the error: `refcount_t: addition on 0; use-after-free.` It was misinterpreting the address due to the `HPAGE_RESV_OWNER` flag being set and the specific offset being used for the refcount increment.
I need to verify this immediately. I'll check the exact struct layouts for `resv_map` and `hugetlb_vma_lock`. Then, I'll trace the precise assembly instructions and register values in `hugetlb_unmap_file_folio` to confirm this critical insight. I'll use `codesearch-struct-layout` to get those struct definitions.
|
| 40/2 |
2026/11/26 16:53 |
tool |
codesearch-struct-layout |
0m
Args:
Results: map[Fields:[map[Name:refs OffsetBits:0 SizeBits:32] map[Name:lock OffsetBits:64 SizeBits:1024] map[Name:regions OffsetBits:1088 SizeBits:128] map[Name:adds_in_progress OffsetBits:1216 SizeBits:64] map[Name:region_cache OffsetBits:1280 SizeBits:128] map[Name:region_cache_count OffsetBits:1408 SizeBits:64] map[Name:rw_sema OffsetBits:1472 SizeBits:1088] map[Name:reservation_counter OffsetBits:2560 SizeBits:64] map[Name:pages_per_hpage OffsetBits:2624 SizeBits:64] map[Name:css OffsetBits:2688 SizeBits:64]]]
|
| 41/2 |
2026/11/26 16:53 |
tool |
codesearch-struct-layout |
0m
Args: map[Name:hugetlb_vma_lock]
Results: map[Fields:[map[Name:refs OffsetBits:0 SizeBits:32] map[Name:rw_sema OffsetBits:64 SizeBits:1088] map[Name:vma OffsetBits:1152 SizeBits:64]]]
|
| 42/2 |
2026/11/26 16:53 |
tool |
read-file |
0m
Args: map[File:fs/hugetlbfs/inode.c FirstLine:375 LineCount:90]
Results: map[Contents: 375:
376: /*
377: * Called with hugetlb fault mutex held. Therefore, no more mappings to
378: * this folio can be created while executing the routine.
379: */
380: static void hugetlb_unmap_file_folio(struct hstate *h,
381: struct address_space *mapping,
382: struct folio *folio, pgoff_t index)
383: {
384: struct hugetlb_vma_lock *vma_lock;
385: unsigned long pfn = folio_pfn(folio);
386: struct vm_area_struct *vma;
387: unsigned long v_start;
388: unsigned long v_end;
389: pgoff_t start, end;
390:
391: start = index * pages_per_huge_page(h);
392: end = (index + 1) * pages_per_huge_page(h);
393:
394: i_mmap_lock_write(mapping);
395: retry:
396: vma_lock = NULL;
397: mapping_rmap_tree_foreach(vma, mapping, start, end - 1) {
398: v_start = vma_offset_start(vma, start);
399: v_end = vma_offset_end(vma, end);
400:
401: if (!hugetlb_vma_maps_pfn(vma, v_start, pfn))
402: continue;
403:
404: if (!hugetlb_vma_trylock_write(vma)) {
405: vma_lock = vma->vm_private_data;
406: /*
407: * If we can not get vma lock, we need to drop
408: * immap_sema and take locks in order. First,
409: * take a ref on the vma_lock structure so that
410: * we can be guaranteed it will not go away when
411: * dropping immap_sema.
412: */
413: kref_get(&vma_lock->refs);
414: break;
415: }
416:
417: unmap_hugepage_range(vma, v_start, v_end, NULL,
418: ZAP_FLAG_DROP_MARKER);
419: hugetlb_vma_unlock_write(vma);
420: }
421:
422: i_mmap_unlock_write(mapping);
423:
424: if (vma_lock) {
425: /*
426: * Wait on vma_lock. We know it is still valid as we have
427: * a reference. We must 'open code' vma locking as we do
428: * not know if vma_lock is still attached to vma.
429: */
430: down_write(&vma_lock->rw_sema);
431: i_mmap_lock_write(mapping);
432:
433: vma = vma_lock->vma;
434: if (!vma) {
435: /*
436: * If lock is no longer attached to vma, then just
437: * unlock, drop our reference and retry looking for
438: * other vmas.
439: */
440: up_write(&vma_lock->rw_sema);
441: kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
442: goto retry;
443: }
444:
445: /*
446: * vma_lock is still attached to vma. Check to see if vma
447: * still maps page and if so, unmap.
448: */
449: v_start = vma_offset_start(vma, start);
450: v_end = vma_offset_end(vma, end);
451: if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
452: unmap_hugepage_range(vma, v_start, v_end, NULL,
453: ZAP_FLAG_DROP_MARKER);
454:
455: kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
456: hugetlb_vma_unlock_write(vma);
457:
458: goto retry;
459: }
460: }
461:
462: static void
463: hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
464: pgoff_t end, zap_flags_t zap_flags)
]
|
| 43/2 |
2026/11/26 16:53 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 40829
output: 135
thoughts: 1207
Thoughts: **Analysis of `hugetlb_unmap_file_folio` and `vma->vm_private_data` Misinterpretation**
My initial reaction upon examining the `struct resv_map` was one of pure astonishment. I observed that `refs` was situated at offset 0, immediately followed by 4 bytes of padding before the `lock` (a `spinlock_t`). This means that if `vma->vm_private_data` held a value like `(void *)((unsigned long)resv_map | HPAGE_RESV_OWNER)`, for instance `0xffff888060cd6801`, then the bytes at `0xffff888060cd6800` would be interpreted as `01 00 00 00 00 00 00 00`. Critically, reading 4 bytes from `0xffff888060cd6801` yields `00 00 00 00`, which is zero. This is already a red flag.
The situation becomes even more concerning when I look at the crash register dump, specifically at `R14: ffff888060cd6801`. This register is directly assigned to `vma_lock = vma->vm_private_data`. Even if the `refs` field wasn't zero at offset +1 (which it is in this scenario), the `hugetlb_unmap_file_folio` function *also* makes a faulty assumption: it treats `vma_lock` as a `struct hugetlb_vma_lock *`. I can see this in lines 430-456 of the code:
down_write(&vma_lock->rw_sema);
i_mmap_lock_write(mapping);
vma = vma_lock->vma;
Now, let's consider the structure layouts. In `struct hugetlb_vma_lock`, `rw_sema` is at offset 8 bytes (64 bits), and `vma` is at offset 144 bytes (1152 bits). However, in `struct resv_map`, `rw_sema` is at offset 184 bytes (1472 bits), and at offset 8 bytes, we find `spinlock_t lock`. This stark difference in offsets means that `hugetlb_unmap_file_folio` is fundamentally misinterpreting the data. It's *assuming* that if `!hugetlb_vma_trylock_write(vma)` returns true (meaning `hugetlb_vma_trylock_write(vma)` failed), then `vma->vm_private_data` *must* be a `struct hugetlb_vma_lock *`.
This is where I need to pause and re-evaluate. I must systematically check *all* callers of `hugetlb_vma_trylock_write` and *all* places where `vma_lock` is used within the kernel. Furthermore, a deep dive into the git history is absolutely essential to understand how this assumption came to be and how it might have been broken.
My initial investigation into the git history reveals two critical points:
1. **Original Intent (Commit `40549ba8f8e0` - "hugetlb: use new vma_lock for pmd sharing synchronization"):** When `vma_lock` was first introduced, the comment in `hugetlb_vmdelete_list` clearly stated: "Note that vma lock only exists for shared/non-private vmas. Therefore, lock is not held when calling unmap_hugepage_range for private vmas." At this time, `hugetlb_vma_trylock_write(vma)` checked `if (!__vma_shareable_lock(vma)) return 1;`. This means that for private VMAs (where `!(vma->vm_flags & VM_MAYSHARE)`), `hugetlb_vma_trylock_write(vma)` *always* returned `1`. Consequently, the `if (!hugetlb_vma_trylock_write(vma))` branch in `hugetlb_unmap_file_folio` could *only* be entered for a shared VMA where `__vma_shareable_lock(vma)` was true. In such cases, `vma->vm_private_data` was *guaranteed* to be a `struct hugetlb_vma_lock *`.
2. **The Shift (Commit `bf4916922c60` - "hugetlbfs: extend hugetlb_vma_lock to private VMAs"):** This commit significantly altered the behavior of `hugetlb_vma_trylock_write` and its related functions, including `lock_read`, `lock_write`, `unlock_read`, `unlock_write`, `assert_locked`, and `__hugetlb_vma_unlock_write_free`. The intention was to extend `hugetlb_vma_lock` to private VMAs as well. This change is the likely culprit for the current misinterpretation, as it appears to have broken the previous guarantee that `vma->vm_private_data` would only point to a `struct hugetlb_vma_lock *` under specific conditions.
My next step is to meticulously trace the commit history surrounding `bf4916922c60`, paying close attention to commits by Rik van Riel and any others related to `hugetlb_vma_lock` or `MADV_DONTNEED`. I need to understand the precise modifications made and how they inadvertently allow a `struct resv_map *` to be incorrectly treated as a `struct hugetlb_vma_lock *` within `hugetlb_unmap_file_folio`.
|
| 44/2 |
2026/11/26 16:53 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:mm/hugetlb.c Since:4 years]
Results: map[Output:9bdad082d44b mm/hugetlb: preserve mremap address delta when skipping page tables
a363c62a653c mm/hugetlb: do not dissolve gigantic pages without runtime support
8ee1ef0f2f8c mm/hugetlb: fix missing migratable flag on same-node hugetlb migration
267bede12d3b mm/hugetlb: keep max_huge_pages when dissolving surplus folios
5120b1e048d4 hugetlb: only adjust reservation during unmapping if mapcount is 0
51943a18ad4b mm: provide vma_[flags_]is_cow_mapping() and remove is_cow_mapping()
47f3cecd720f hugetlb: evaluate subpool free state while locked
60191388cad9 hugetlb: make hugepage_put_subpool() tolerate NULL
5872168de5d5 mm/vma: use vma_start_pgoff(), linear_page_index() in mm code
4b7a90a48b99 mm/rmap: rename vma_interval_tree_*() to mapping_rmap_tree_*()
7a67b96af069 mm/rmap: parameterise vma_interval_tree_*() by address_space
55ed40abb2cd mm: split out mm_init and memblock declarations from internal.h
45347e3d32c1 userfaultfd: add UFFD_FEATURE_RWP_ASYNC for async fault resolution
2d427bb01639 mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP
763f43865fc4 mm: preserve RWP marker across PTE rewrites
9cf3c554acbd mm: add MM_CP_UFFD_RWP change_protection() flag
c0ffe79eedc1 mm: rename uffd-wp PTE accessors to uffd
7d163a75f821 memblock: make HugeTLB bootmem allocation work with KHO
2fc4c1d51d87 mm/page_alloc: unify __alloc_frozen_pages[_nolock]_noprof()
d0850de699b0 mm: split out internal page_alloc.h
]
|
| 45/2 |
2026/11/26 16:53 |
tool |
git-log |
0m
Args: map[MessageRegexps:[extend hugetlb_vma_lock]]
Results: map[Output:187da0f8250a hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write
bf4916922c60 hugetlbfs: extend hugetlb_vma_lock to private VMAs
]
|
| 46/2 |
2026/11/26 16:54 |
tool |
git-log |
0m
Args: map[Count:10 MessageRegexps:[hugetlb vma_lock]]
Results: map[Output:121170831228 mm/hmm: add hmm_range_fault_unlocked_timeout() for mmap lock-drop support
1ef12d3b3068 mm/hmm: move page fault handling out of walk callbacks
e92d92bbafb2 fs/proc/task_mmu: fix hugetlb self-deadlock in pagemap_scan_pte_hole()
b4aea43cd37a mm/hugetlb: avoid false positive lockdep assertion
dd83609b8898 hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list
2ae1ab9934c7 mm,hugetlb: change mechanism to detect a COW on private mapping
35e351780fa9 fork: defer linking file vma until vma is fully initialized
187da0f8250a hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write
2820b0f09be9 hugetlbfs: close race between MADV_DONTNEED and page fault
bf4916922c60 hugetlbfs: extend hugetlb_vma_lock to private VMAs
]
|
| 47/2 |
2026/11/26 16:54 |
tool |
codesearch-find-references |
0m
Args: map[Name:hugetlb_vma_trylock_write]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_unmap_file_folio SourceFile:fs/hugetlbfs/inode.c SourceLine:404] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hugetlb_vmdelete_list SourceFile:fs/hugetlbfs/inode.c SourceLine:478] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:try_to_migrate_one SourceFile:mm/rmap.c SourceLine:2606] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:try_to_unmap_poisoned_hugetlb_one SourceFile:mm/rmap.c SourceLine:2042]] TruncatedOutput:false]
|
| 48/2 |
2026/11/26 16:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 43395
output: 86
thoughts: 98
Thoughts: **Hold on, I need to re-evaluate something.**
I just spotted a commit in the log: `dd83609b8898 hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list`. This immediately caught my attention, and I'm thinking, "Wait a second! This looks significant." My immediate instinct is to dive deeper into the context. So, I'm going to run `git show` on this commit, `dd83609b8898`, to understand its precise changes. But that's not enough on its own. To truly grasp the implications and the evolution of this functionality, I need to see the commits that led to it and perhaps any subsequent related changes. Therefore, I'm also going to examine `2820b0f09be9` and `b4aea43cd37a`. This comparative analysis will allow me to build a comprehensive picture of the problem being addressed and the solution implemented, which is crucial for my understanding.
|
| 49/2 |
2026/11/26 16:54 |
tool |
git-show |
0m
Args:
Results: map[Output:commit dd83609b88986f4add37c0871c3434310652ebd5
Author: Deepanshu Kartikey <kartikey406@gmail.com>
Date: Fri Sep 26 09:02:54 2025 +0530
hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list
hugetlb_vmdelete_list() uses trylock to acquire VMA locks during truncate
operations. As per the original design in commit 40549ba8f8e0 ("hugetlb:
use new vma_lock for pmd sharing synchronization"), if the trylock fails
or the VMA has no lock, it should skip that VMA. Any remaining mapped
pages are handled by remove_inode_hugepages() which is called after
hugetlb_vmdelete_list() and uses proper lock ordering to guarantee
unmapping success.
Currently, when hugetlb_vma_trylock_write() returns success (1) for VMAs
without shareable locks, the code proceeds to call unmap_hugepage_range().
This causes assertion failures in huge_pmd_unshare() →
hugetlb_vma_assert_locked() because no lock is actually held:
WARNING: CPU: 1 PID: 6594 Comm: syz.0.28 Not tainted
Call Trace:
hugetlb_vma_assert_locked+0x1dd/0x250
huge_pmd_unshare+0x2c8/0x540
__unmap_hugepage_range+0x6e3/0x1aa0
unmap_hugepage_range+0x32e/0x410
hugetlb_vmdelete_list+0x189/0x1f0
Fix by using goto to ensure locks acquired by trylock are always released,
even when skipping VMAs without shareable locks.
Link: https://lkml.kernel.org/r/20250926033255.10930-1-kartikey406@gmail.com
Fixes: 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
Reported-by: syzbot+f26d7c75c26ec19790e7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f26d7c75c26ec19790e7
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 3cfdf4091001..94b4d854429e 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -478,6 +478,14 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
if (!hugetlb_vma_trylock_write(vma))
continue;
+ /*
+ * Skip VMAs without shareable locks. Per the design in commit
+ * 40549ba8f8e0, these will be handled by remove_inode_hugepages()
+ * called after this function with proper locking.
+ */
+ if (!__vma_shareable_lock(vma))
+ goto skip;
+
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
@@ -488,6 +496,7 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
* vmas. Therefore, lock is not held when calling
* unmap_hugepage_range for private vmas.
*/
+skip:
hugetlb_vma_unlock_write(vma);
}
}
]
|
| 50/2 |
2026/11/26 16:54 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 2820b0f09be99f6406784b03a22dfc83e858449d
Author: Rik van Riel <riel@surriel.com>
Date: Thu Oct 5 23:59:08 2023 -0400
hugetlbfs: close race between MADV_DONTNEED and page fault
Malloc libraries, like jemalloc and tcalloc, take decisions on when to
call madvise independently from the code in the main application.
This sometimes results in the application page faulting on an address,
right after the malloc library has shot down the backing memory with
MADV_DONTNEED.
Usually this is harmless, because we always have some 4kB pages sitting
around to satisfy a page fault. However, with hugetlbfs systems often
allocate only the exact number of huge pages that the application wants.
Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of
any lock taken on the page fault path, which can open up the following
race condition:
CPU 1 CPU 2
MADV_DONTNEED
unmap page
shoot down TLB entry
page fault
fail to allocate a huge page
killed with SIGBUS
free page
Fix that race by pulling the locking from __unmap_hugepage_final_range
into helper functions called from zap_page_range_single. This ensures
page faults stay locked out of the MADV_DONTNEED VMA until the huge pages
have actually been freed.
Link: https://lkml.kernel.org/r/20231006040020.3677377-4-riel@surriel.com
Fixes: 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing")
Signed-off-by: Rik van Riel <riel@surriel.com>
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 065ec022cbb0..47d25a5e1933 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -139,7 +139,7 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
void unmap_hugepage_range(struct vm_area_struct *,
unsigned long, unsigned long, struct page *,
zap_flags_t);
-void __unmap_hugepage_range_final(struct mmu_gather *tlb,
+void __unmap_hugepage_range(struct mmu_gather *tlb,
struct vm_area_struct *vma,
unsigned long start, unsigned long end,
struct page *ref_page, zap_flags_t zap_flags);
@@ -246,6 +246,25 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
unsigned long *start, unsigned long *end);
+extern void __hugetlb_zap_begin(struct vm_area_struct *vma,
+ unsigned long *begin, unsigned long *end);
+extern void __hugetlb_zap_end(struct vm_area_struct *vma,
+ struct zap_details *details);
+
+static inline void hugetlb_zap_begin(struct vm_area_struct *vma,
+ unsigned long *start, unsigned long *end)
+{
+ if (is_vm_hugetlb_page(vma))
+ __hugetlb_zap_begin(vma, start, end);
+}
+
+static inline void hugetlb_zap_end(struct vm_area_struct *vma,
+ struct zap_details *details)
+{
+ if (is_vm_hugetlb_page(vma))
+ __hugetlb_zap_end(vma, details);
+}
+
void hugetlb_vma_lock_read(struct vm_area_struct *vma);
void hugetlb_vma_unlock_read(struct vm_area_struct *vma);
void hugetlb_vma_lock_write(struct vm_area_struct *vma);
@@ -297,6 +316,18 @@ static inline void adjust_range_if_pmd_sharing_possible(
{
}
+static inline void hugetlb_zap_begin(
+ struct vm_area_struct *vma,
+ unsigned long *start, unsigned long *end)
+{
+}
+
+static inline void hugetlb_zap_end(
+ struct vm_area_struct *vma,
+ struct zap_details *details)
+{
+}
+
static inline struct page *hugetlb_follow_page_mask(
struct vm_area_struct *vma, unsigned long address, unsigned int flags,
unsigned int *page_mask)
@@ -442,7 +473,7 @@ static inline long hugetlb_change_protection(
return 0;
}
-static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
+static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
struct vm_area_struct *vma, unsigned long start,
unsigned long end, struct page *ref_page,
zap_flags_t zap_flags)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d218ee2b6a99..1301ba7b2c9a 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5306,9 +5306,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
return len + old_addr - old_end;
}
-static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
- unsigned long start, unsigned long end,
- struct page *ref_page, zap_flags_t zap_flags)
+void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
+ unsigned long start, unsigned long end,
+ struct page *ref_page, zap_flags_t zap_flags)
{
struct mm_struct *mm = vma->vm_mm;
unsigned long address;
@@ -5437,16 +5437,25 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct
tlb_flush_mmu_tlbonly(tlb);
}
-void __unmap_hugepage_range_final(struct mmu_gather *tlb,
- struct vm_area_struct *vma, unsigned long start,
- unsigned long end, struct page *ref_page,
- zap_flags_t zap_flags)
+void __hugetlb_zap_begin(struct vm_area_struct *vma,
+ unsigned long *start, unsigned long *end)
{
+ if (!vma->vm_file) /* hugetlbfs_file_mmap error */
+ return;
+
+ adjust_range_if_pmd_sharing_possible(vma, start, end);
hugetlb_vma_lock_write(vma);
- i_mmap_lock_write(vma->vm_file->f_mapping);
+ if (vma->vm_file)
+ i_mmap_lock_write(vma->vm_file->f_mapping);
+}
- /* mmu notification performed in caller */
- __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags);
+void __hugetlb_zap_end(struct vm_area_struct *vma,
+ struct zap_details *details)
+{
+ zap_flags_t zap_flags = details ? details->zap_flags : 0;
+
+ if (!vma->vm_file) /* hugetlbfs_file_mmap error */
+ return;
if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */
/*
@@ -5459,11 +5468,12 @@ void __unmap_hugepage_range_final(struct mmu_gather *tlb,
* someone else.
*/
__hugetlb_vma_unlock_write_free(vma);
- i_mmap_unlock_write(vma->vm_file->f_mapping);
} else {
- i_mmap_unlock_write(vma->vm_file->f_mapping);
hugetlb_vma_unlock_write(vma);
}
+
+ if (vma->vm_file)
+ i_mmap_unlock_write(vma->vm_file->f_mapping);
}
void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
diff --git a/mm/memory.c b/mm/memory.c
index 6c264d2f969c..517221f01303 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1683,7 +1683,7 @@ static void unmap_single_vma(struct mmu_gather *tlb,
if (vma->vm_file) {
zap_flags_t zap_flags = details ?
details->zap_flags : 0;
- __unmap_hugepage_range_final(tlb, vma, start, end,
+ __unmap_hugepage_range(tlb, vma, start, end,
NULL, zap_flags);
}
} else
@@ -1728,8 +1728,12 @@ void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas,
start_addr, end_addr);
mmu_notifier_invalidate_range_start(&range);
do {
- unmap_single_vma(tlb, vma, start_addr, end_addr, &details,
+ unsigned long start = start_addr;
+ unsigned long end = end_addr;
+ hugetlb_zap_begin(vma, &start, &end);
+ unmap_single_vma(tlb, vma, start, end, &details,
mm_wr_locked);
+ hugetlb_zap_end(vma, &details);
} while ((vma = mas_find(mas, tree_end - 1)) != NULL);
mmu_notifier_invalidate_range_end(&range);
}
@@ -1753,9 +1757,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
lru_add_drain();
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
address, end);
- if (is_vm_hugetlb_page(vma))
- adjust_range_if_pmd_sharing_possible(vma, &range.start,
- &range.end);
+ hugetlb_zap_begin(vma, &range.start, &range.end);
tlb_gather_mmu(&tlb, vma->vm_mm);
update_hiwater_rss(vma->vm_mm);
mmu_notifier_invalidate_range_start(&range);
@@ -1766,6 +1768,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
unmap_single_vma(&tlb, vma, address, end, details, false);
mmu_notifier_invalidate_range_end(&range);
tlb_finish_mmu(&tlb);
+ hugetlb_zap_end(vma, details);
}
/**
]
|
| 51/2 |
2026/11/26 16:54 |
tool |
git-show |
0m
Args:
Results: map[Output:commit b4aea43cd37afad714b5684fe9fdfcb0e78dba26
Author: Lorenzo Stoakes <ljs@kernel.org>
Date: Wed May 13 09:56:58 2026 +0100
mm/hugetlb: avoid false positive lockdep assertion
Commit 081056dc00a2 ("mm/hugetlb: unshare page tables during VMA split,
not before") changed the locking model around hugetlbfs PMD unsharing on
VMA split, but did not update the function which asserts the locks,
hugetlb_vma_assert_locked().
This function asserts that either the hugetlb VMA lock is held (if a
shared mapping) or that the reservation map lock is held (if private).
If you get an unfortunate race between something which results in one of
these locks being released and a hugetlb VMA split and you have
CONFIG_LOCKDEP enabled, you can therefore see a false positive assertion
arise when there is in fact no issue.
Since this change introduced a new take_locks parameter to
hugetlb_unshare_pmds(), which, when set to false, indicates that locking
is sufficient, simply pass this to the unsharing logic and predicate the
lock assertions on this.
This is safe, as we already asserted the file rmap lock and the VMA write
lock prior to this (implying exclusive mmap write lock), so we cannot be
raced by either rmap or page fault page table walkers which the asserted
locks are intended to protect against (we don't mind GUP-fast).
Separate out huge_pmd_unshare() into __huge_pmd_unshare() to add a
check_locks parameter, and update hugetlb_unshare_pmds() to pass this
parameter to it.
This leaves all other callers of huge_pmd_unshare() still correctly
asserting the locks.
The below reproducer will trigger the assert in a kernel with
CONFIG_LOCKDEP enabled by racing process teardown (which will release the
hugetlb lock) against a hugetlb split.
void execute_one(void)
{
void *ptr;
pid_t pid;
/*
* Create a hugetlb mapping spanning a PUD entry.
*
* We force the hugetlb page allocation with populate and
* noreserve.
*
* |---------------------|
* | |
* |---------------------|
* 0 PUD boundary
*/
ptr = mmap(0, PUD_SIZE, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED | MAP_ANON |
MAP_NORESERVE | MAP_HUGETLB | MAP_POPULATE,
-1, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
/*
* Fork but with a bogus stack pointer so we try to execute code in
* a non-VM_EXEC VMA, causing segfault + teardown via exit_mmap().
*
* The clone will cause PMD page table sharing between the
* processes first via:
* copy_process() -> ... -> huge_pte_alloc() -> huge_pmd_share()
*
* Then tear down and release the hugetlb 'VMA' lock via:
* exit_mmap() -> ... -> vma_close() -> hugetlb_vma_lock_free()
*/
pid = syscall(__NR_clone, 0, 2 * PMD_SIZE, 0, 0, 0);
if (pid < 0) {
perror("clone");
exit(EXIT_FAILURE);
} if (pid == 0) {
/* Pop stack... */
return;
}
/*
* We are the parent process.
*
* Race the child process's teardown with a PMD unshare.
*
* We do this by triggering:
*
* __split_vma() -> hugetlb_split() -> hugetlb_unshare_pmds()
*
* Which, importantly, doesn't hold the hugetlb VMA lock (nor can
* it), meaning we assert in hugetlb_vma_assert_locked().
*
* .
* |----------.----------|
* | . |
* |----------.----------|
* 0 . PUD boundary
*/
mmap(0, PUD_SIZE / 2, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
}
int main(void)
{
int i;
/* Kick off fork children. */
for (i = 0; i < NUM_FORKS; i++) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
/* Fork children do their work and exit. */
if (!pid) {
int j;
for (j = 0; j < NUM_ITERS; j++)
execute_one();
return EXIT_SUCCESS;
}
}
/* If we succeeded, wait on children. */
for (i = 0; i < NUM_FORKS; i++)
wait(NULL);
return EXIT_SUCCESS;
}
[ljs@kernel.org: account for the !CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING case]
Link: https://lore.kernel.org/agWZsPGYid08uU6O@lucifer
Link: https://lore.kernel.org/20260513085658.45264-1-ljs@kernel.org
Fixes: 081056dc00a2 ("mm/hugetlb: unshare page tables during VMA split, not before")
Signed-off-by: Lorenzo Stoakes <ljs@kernel.org>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Oscar Salvador <osalvador@suse.de>
Cc: Jann Horn <jannh@google.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 4b80b167cc9c..ece86d9339ce 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -118,6 +118,9 @@ static int hugetlb_acct_memory(struct hstate *h, long delta);
static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
+static int __huge_pmd_unshare(struct mmu_gather *tlb,
+ struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
+ bool check_locks);
static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
unsigned long start, unsigned long end, bool take_locks);
static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
@@ -6891,6 +6894,31 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
return pte;
}
+static int __huge_pmd_unshare(struct mmu_gather *tlb,
+ struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
+ bool check_locks)
+{
+ unsigned long sz = huge_page_size(hstate_vma(vma));
+ struct mm_struct *mm = vma->vm_mm;
+ pgd_t *pgd = pgd_offset(mm, addr);
+ p4d_t *p4d = p4d_offset(pgd, addr);
+ pud_t *pud = pud_offset(p4d, addr);
+
+ if (sz != PMD_SIZE)
+ return 0;
+ if (!ptdesc_pmd_is_shared(virt_to_ptdesc(ptep)))
+ return 0;
+ i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ if (check_locks)
+ hugetlb_vma_assert_locked(vma);
+ pud_clear(pud);
+
+ tlb_unshare_pmd_ptdesc(tlb, virt_to_ptdesc(ptep), addr);
+
+ mm_dec_nr_pmds(mm);
+ return 1;
+}
+
/**
* huge_pmd_unshare - Unmap a pmd table if it is shared by multiple users
* @tlb: the current mmu_gather.
@@ -6910,24 +6938,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
int huge_pmd_unshare(struct mmu_gather *tlb, struct vm_area_struct *vma,
unsigned long addr, pte_t *ptep)
{
- unsigned long sz = huge_page_size(hstate_vma(vma));
- struct mm_struct *mm = vma->vm_mm;
- pgd_t *pgd = pgd_offset(mm, addr);
- p4d_t *p4d = p4d_offset(pgd, addr);
- pud_t *pud = pud_offset(p4d, addr);
-
- if (sz != PMD_SIZE)
- return 0;
- if (!ptdesc_pmd_is_shared(virt_to_ptdesc(ptep)))
- return 0;
- i_mmap_assert_write_locked(vma->vm_file->f_mapping);
- hugetlb_vma_assert_locked(vma);
- pud_clear(pud);
-
- tlb_unshare_pmd_ptdesc(tlb, virt_to_ptdesc(ptep), addr);
-
- mm_dec_nr_pmds(mm);
- return 1;
+ return __huge_pmd_unshare(tlb, vma, addr, ptep, /*check_locks=*/true);
}
/*
@@ -6961,6 +6972,13 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
return NULL;
}
+static int __huge_pmd_unshare(struct mmu_gather *tlb,
+ struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
+ bool check_locks)
+{
+ return 0;
+}
+
int huge_pmd_unshare(struct mmu_gather *tlb, struct vm_area_struct *vma,
unsigned long addr, pte_t *ptep)
{
@@ -7269,7 +7287,7 @@ static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
if (!ptep)
continue;
ptl = huge_pte_lock(h, mm, ptep);
- huge_pmd_unshare(&tlb, vma, address, ptep);
+ __huge_pmd_unshare(&tlb, vma, address, ptep, take_locks);
spin_unlock(ptl);
}
huge_pmd_unshare_flush(&tlb, vma);
]
|
| 52/2 |
2026/11/26 16:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 51092
output: 38
thoughts: 2547
Thoughts: **It appears I've stumbled upon a race condition involving hugetlbfs and MAP_PRIVATE mappings, leading to a use-after-free (UAF) scenario and a crash.**
My thought process started by examining two specific commits: `dd83609b8898` and `bf4916922c60`. I needed to understand how they altered the locking mechanisms within hugetlb.
Specifically, I focused on changes to `hugetlb_vma_trylock_write`. Before `bf4916922c60`, it only attempted to acquire a lock on `vma->vm_private_data` if `__vma_shareable_lock(vma)` succeeded. After `bf4916922c60` (and a fix in `187da0f8250a`), it gained the ability to also try and acquire a lock on `resv_map->rw_sema` if `__vma_private_lock(vma)` was true. This second path is crucial for private VMAs, as their `vm_private_data` is set up by `hugetlb_reserve_pages` to contain the `resv_map` with the `HPAGE_RESV_OWNER` bit set.
Then, I dove into `hugetlb_unmap_file_folio()`. I observed the loop iterating through VMAs and the check `if (!hugetlb_vma_trylock_write(vma))`. The logic here is that if the lock acquisition fails, the code needs to take a reference on the `vma_lock` structure to prevent it from disappearing while dropping another lock (`immap_sema`). This is where the problem seems to lie.
I started hypothesizing a scenario where a `MAP_PRIVATE` hugetlbfs mapping is created. In this case, `vma->vm_private_data` is set to the `resv_map` pointer with the `HPAGE_RESV_OWNER` bit set (indicated by a non-zero lower byte, like `0x1`). Crucially, such a VMA *is* present in the `mapping->i_mmap` tree.
Next, I considered a situation where a folio is faulted into this private VMA. The `hugetlb_no_page` function for `MAP_PRIVATE` hugetlbfs will map the file's pagecache folio into the VMA's page table. This means `hugetlb_vma_maps_pfn(vma, v_start, pfn)` would return true when `hugetlb_unmap_file_folio` iterates over this VMA.
Now, the critical part: a thread calls `ftruncate()` on the hugetlbfs file. This eventually leads to `hugetlb_vmdelete_list` and then `hugetlb_unmap_file_folio()`. The reason it reaches `hugetlb_unmap_file_folio` is twofold:
1. `hugetlb_vmdelete_list` might skip the private VMA due to commit `dd83609b8898`'s check for `__vma_shareable_lock(vma)`.
2. Even if it didn't skip, a race condition could occur where a page fault or `MADV_DONTNEED` on another part of the VMA holds a lock, causing `hugetlb_vma_trylock_write` within `hugetlb_vmdelete_list` to fail, and the VMA is then handled by `hugetlb_unmap_file_folio`.
3. Or, `folio_mapped(folio)` being true in `remove_inode_single_folio` triggers the call.
So, inside `hugetlb_unmap_file_folio`, we're iterating through the `i_mmap` tree, find our private VMA, `hugetlb_vma_maps_pfn` is true. We then call `hugetlb_vma_trylock_write(vma)`. Because it's a private VMA, `__vma_private_lock(vma)` is true, and it attempts `down_write_trylock(&resv_map->rw_sema)`.
The problem arises if another thread is *already* holding `resv_map->rw_sema`. This could happen during operations like:
* A concurrent page fault in `hugetlb_fault()` holding `hugetlb_vma_lock_read()`.
* `zap_page_range_single()` or `MADV_DONTNEED` holding `hugetlb_vma_lock_write()`.
* `munmap()`, `exit_mmap()`, or `copy_hugetlb_page_range()` holding `hugetlb_vma_lock_write()` via `hugetlb_zap_begin()`.
If `down_write_trylock(&resv_map->rw_sema)` fails (returns 0), we enter the `if (!hugetlb_vma_trylock_write(vma))` block. The code then retrieves `vma_lock = vma->vm_private_data` and tries to call `kref_get(&vma_lock->refs)`.
Here's the critical flaw: `vma_lock` is *not* a `struct hugetlb_vma_lock *`. It's actually the `struct resv_map *` with the `HPAGE_RESV_OWNER` bit set. The kernel then attempts to interpret the lower bytes of this pointer as a `struct hugetlb_vma_lock`, specifically trying to access its `refs` member.
The address `ffff888060cd6801` in the crash report matches this perfectly. The lower bit `0x1` signifies `HPAGE_RESV_OWNER`. When `kref_get` attempts to read the `refcount_t` at offset +1 from this address (effectively treating `ffff888060cd6801` as the start of the `hugetlb_vma_lock` structure), it's reading from the `resv_map->refs` field. If `resv_map->refs` happens to be 0, `refcount_inc` will see 0, trigger `refcount_warn_saturate`, and lead to the UAF. The value `0x00000000` found in the crash report at this location confirms this.
The register `R14` holding `ffff888060cd6801` is consistent with `vma_lock` being in a callee-saved register, and `RBX` being `2` aligns with `REFCOUNT_ADD_UAF` from `lib/refcount.c`.
Essentially, the logic in `hugetlb_unmap_file_folio` assumes that if `hugetlb_vma_trylock_write` fails and `__vma_private_lock` was true, then `vma->vm_private_data` *must* be a `struct hugetlb_vma_lock *` with valid `refs`. However, for private VMAs, it's a `struct resv_map *`, and this assumption leads to the use-after-free.
|
| 53/2 |
2026/11/26 16:54 |
tool |
read-file |
0m
Args: map[File:lib/refcount.c FirstLine:1 LineCount:45]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0
2: /*
3: * Out-of-line refcount functions.
4: */
5:
6: #include <linux/mutex.h>
7: #include <linux/refcount.h>
8: #include <linux/spinlock.h>
9: #include <linux/bug.h>
10:
11: #define REFCOUNT_WARN(str) WARN_ONCE(1, "refcount_t: " str ".\n")
12:
13: void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
14: {
15: refcount_set(r, REFCOUNT_SATURATED);
16:
17: switch (t) {
18: case REFCOUNT_ADD_NOT_ZERO_OVF:
19: REFCOUNT_WARN("saturated; leaking memory");
20: break;
21: case REFCOUNT_ADD_OVF:
22: REFCOUNT_WARN("saturated; leaking memory");
23: break;
24: case REFCOUNT_ADD_UAF:
25: REFCOUNT_WARN("addition on 0; use-after-free");
26: break;
27: case REFCOUNT_SUB_UAF:
28: REFCOUNT_WARN("underflow; use-after-free");
29: break;
30: case REFCOUNT_DEC_LEAK:
31: REFCOUNT_WARN("decrement hit 0; leaking memory");
32: break;
33: default:
34: REFCOUNT_WARN("unknown saturation event!?");
35: }
36: }
37: EXPORT_SYMBOL(refcount_warn_saturate);
38:
39: /**
40: * refcount_dec_if_one - decrement a refcount if it is 1
41: * @r: the refcount
42: *
43: * No atomic_t counterpart, it attempts a 1 -> 0 transition and returns the
44: * success thereof.
45: *
]
|
| 54/2 |
2026/11/26 16:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 54289
output: 87
thoughts: 3548
Thoughts: **Deconstructing the Hugetlb VMA Lock Bug**
Okay, I've got it. The key pieces of information that clued me in were these constants: `REFCOUNT_ADD_NOT_ZERO_OVF = 0`, `REFCOUNT_ADD_OVF = 1`, and `REFCOUNT_ADD_UAF = 2`. And then, the specific register value `R14: ffff888060cd6801`. These pointed me directly to a reference counting overflow/use-after-free scenario within the hugetlb subsystem, specifically triggered by an operation involving `vma->vm_private_data`. The low bits of the `R14` address (`01`) were particularly telling, strongly suggesting that `vma->vm_private_data` was not pointing to a `struct hugetlb_vma_lock`, but rather to a `struct resv_map *` with the `HPAGE_RESV_OWNER` flag set. This combination immediately told me the crash was occurring when `hugetlb_unmap_file_folio()` was attempting to use a `resv_map` pointer as if it were a `hugetlb_vma_lock`.
My primary goal now is to pinpoint the exact commit that introduced this bug. I've been meticulously sifting through the commit history, examining each candidate with a fine-tooth comb.
The commit `40549ba8f8e0` ("hugetlb: use new vma_lock for pmd sharing synchronization") is where I started. This commit introduced `hugetlb_vma_lock` and added the `if (!hugetlb_vma_trylock_write(vma))` block in `hugetlb_unmap_file_folio()`. At that time, `hugetlb_vma_trylock_write(vma)` was designed such that it would only return `0` (indicating success) for *shareable* VMAs that actually possessed a `hugetlb_vma_lock` (checked by `__vma_shareable_lock(vma)`). If a VMA was private or didn't have the `hugetlb_vma_lock` initialized, `hugetlb_vma_trylock_write` would always return `1`, meaning the `if (!hugetlb_vma_trylock_write(vma))` condition would *never* be met for those cases. This was crucial: it meant that within that `if` block, `vma->vm_private_data` was *guaranteed* to be a valid `struct hugetlb_vma_lock *`. I then spent some time verifying the lifecycle of `vma_lock` and its `refs` count, making sure no other bug could lead to a zero `refs` count on a shared VMA. My conclusion here is that `40549ba8f8e0` *itself* didn't introduce the bug but laid the groundwork by creating the problematic `if` condition.
Now, I'm comparing the subsequent commits that modified the locking mechanisms: `bf4916922c60` ("hugetlbfs: extend hugetlb_vma_lock to private VMAs"), `187da0f8250a` ("hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write"), and `dd83609b8898` ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list").
Let's look at `bf4916922c60`. This commit significantly altered `hugetlb_vma_trylock_write(vma)` to *also* attempt to lock the `resv_map->rw_sema` for private VMAs (via `__vma_private_lock(vma)`). Crucially, it would return `0` if this trylock succeeded. This is the pivotal point: before `bf4916922c60`, the `if (!hugetlb_vma_trylock_write(vma))` block in `hugetlb_unmap_file_folio()` was only reachable for shared VMAs with valid `hugetlb_vma_lock`s. However, *after* `bf4916922c60`, this condition could now be met for private VMAs where `vma->vm_private_data` was actually a `resv_map` pointer with flags set. The bug is that the code within that `if` block still assumed `vma->vm_private_data` was a `struct hugetlb_vma_lock *`, leading to the `kref_get` dereferencing a `resv_map` (or rather, a `resv_map | HPAGE_RESV_OWNER` value). This commit, `bf4916922c60`, is therefore the one that *introduced* the vulnerability. It failed to account for the fact that the `if` condition could now be true for private VMAs, where `vma->vm_private_data` pointed to something entirely different (a `resv_map` with a different structure and locking semaphore).
I then examined `187da0f8250a`. This commit primarily addressed a null-pointer dereference that could occur in `__vma_private_lock(vma)` when `vma->vm_private_data` had certain flags set but wasn't a valid `resv_map` pointer. It didn't introduce the bug in `hugetlb_unmap_file_folio()`; it was a subsequent fix for a related but distinct issue.
Finally, I analyzed `dd83609b8898` ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list"). Before this commit, `hugetlb_vmdelete_list()` would attempt to trylock the `resv_map->rw_sema` for private VMAs. If successful, it would unmap the private VMA directly. `dd83609b8898` changed this behavior: it now *always* skips private VMAs in `hugetlb_vmdelete_list()` (using `if (!__vma_shareable_lock(vma)) goto skip;`). This means that private VMAs are now *guaranteed* to be processed later by `remove_inode_hugepages()` -> `remove_inode_single_folio()` -> `hugetlb_unmap_file_folio()`. This commit didn't introduce the fundamental problem in `hugetlb_unmap_file_folio()`, but it *did* make it significantly more likely for that function to be called with a private VMA that was still mapped and potentially held by another thread. The comment in `dd83609b8898` also points to a misunderstanding of how the locking was supposed to work after `40549ba8f8e0`.
To summarize my detailed thought process: the bug originates from `bf4916922c60`, which enabled `hugetlb_vma_trylock_write()` to succeed for private VMAs holding `resv_map`s. The `if (!hugetlb_vma_trylock_write(vma))` block in `hugetlb_unmap_file_folio()` then incorrectly treated these `resv_map` pointers as `struct hugetlb_vma_lock *`, leading to the crash. The commit `dd83609b8898` exacerbated the issue by ensuring private VMAs are more reliably funneled through `hugetlb_unmap_file_folio()` where the bug can manifest. I've been double-checking the interaction of `hugetlb_fault()` and `ftruncate()` as a race condition scenario that would definitely trigger this, with `hugetlb_fault()` holding the `resv_map->rw_sema` and `ftruncate()` (via `hugetlb_vmdelete_list()` and then `hugetlb_unmap_file_folio()`) failing its trylock and falling into the buggy code path.
I'm now focusing on any other commits that might have touched `hugetlb_unmap_file_folio`, `hugetlb_vma_trylock_write`, `resv_map`, or `hugetlb_vmdelete_list` to ensure I haven't missed any subtle interactions, and I've performed quick `git-log` checks on related functions to confirm my focus is correct.
|
| 55/2 |
2026/11/26 16:55 |
tool |
git-log |
0m
Args: map[SourcePath:fs/hugetlbfs/inode.c SymbolName:hugetlb_vmdelete_list]
Results: map[Output:4b7a90a48b99 mm/rmap: rename vma_interval_tree_*() to mapping_rmap_tree_*()
7a67b96af069 mm/rmap: parameterise vma_interval_tree_*() by address_space
34ca46cc6fc9 fs: hugetlb: simplify remove_inode_hugepages() return type
cec944dd329f hugetlbfs: move lock assertions after early returns in huge_pmd_unshare()
dd83609b8898 hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list
243b1f2d3b09 mm/hugetlb: let vma_offset_start() to return start
40549ba8f8e0 hugetlb: use new vma_lock for pmd sharing synchronization
378397ccb8e5 hugetlb: create hugetlb_unmap_file_folio to unmap single file folio
c86272287bc6 hugetlb: create remove_inode_single_folio to remove single file folio
05e90bd05eea mm/hugetlb: only drop uffd-wp special pte if required
]
|
| 56/2 |
2026/11/26 16:55 |
tool |
git-log |
0m
Args: map[SourcePath:mm/hugetlb.c SymbolName:hugetlb_vma_lock_release]
Results: map[Output:e700898fa075 hugetlb: really allocate vma lock for all sharable vmas
]
|
| 57/2 |
2026/11/26 16:55 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 58285
output: 58
thoughts: 45
Thoughts: **Verifying Critical Data Points**
My current priority is to ensure absolute certainty regarding the data encompassed by specific identifiers: `cec944dd329f` and `e700898fa075`. I'm conducting this review with the explicit goal of eliminating any possibility of oversight or missed information. This meticulous double-check is a crucial step in my process to guarantee the integrity and completeness of the information I'm working with.
|
| 58/2 |
2026/11/26 16:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit cec944dd329fbefee907da95c298719d900d4787
Author: Deepanshu Kartikey <kartikey406@gmail.com>
Date: Tue Oct 14 17:03:44 2025 +0530
hugetlbfs: move lock assertions after early returns in huge_pmd_unshare()
When hugetlb_vmdelete_list() processes VMAs during truncate operations, it
may encounter VMAs where huge_pmd_unshare() is called without the required
shareable lock. This triggers an assertion failure in
hugetlb_vma_assert_locked().
The previous fix in commit dd83609b8898 ("hugetlbfs: skip VMAs without
shareable locks in hugetlb_vmdelete_list") skipped entire VMAs without
shareable locks to avoid the assertion. However, this prevented pages
from being unmapped and freed, causing a regression in
fallocate(PUNCH_HOLE) operations where pages were not freed immediately,
as reported by Mark Brown.
Instead of checking locks in the caller or skipping VMAs, move the lock
assertions in huge_pmd_unshare() to after the early return checks. The
assertions are only needed when actual PMD unsharing work will be
performed. If the function returns early because sz != PMD_SIZE or the
PMD is not shared, no locks are required and assertions should not fire.
This approach reverts the VMA skipping logic from commit dd83609b8898
("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list")
while moving the assertions to avoid the assertion failure, keeping all
the logic within huge_pmd_unshare() itself and allowing page unmapping and
freeing to proceed for all VMAs.
Link: https://lkml.kernel.org/r/20251014113344.21194-1-kartikey406@gmail.com
Fixes: dd83609b8898 ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list")
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
Reported-by: <syzbot+f26d7c75c26ec19790e7@syzkaller.appspotmail.com>
Reported-by: Mark Brown <broonie@kernel.org>
Closes: https://syzkaller.appspot.com/bug?extid=f26d7c75c26ec19790e7
Suggested-by: David Hildenbrand <david@redhat.com>
Suggested-by: Oscar Salvador <osalvador@suse.de>
Tested-by: <syzbot+f26d7c75c26ec19790e7@syzkaller.appspotmail.com>
Acked-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 9c94ed8c3ab0..f42548ee9083 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -478,14 +478,6 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
if (!hugetlb_vma_trylock_write(vma))
continue;
- /*
- * Skip VMAs without shareable locks. Per the design in commit
- * 40549ba8f8e0, these will be handled by remove_inode_hugepages()
- * called after this function with proper locking.
- */
- if (!__vma_shareable_lock(vma))
- goto skip;
-
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
@@ -496,7 +488,6 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,
* vmas. Therefore, lock is not held when calling
* unmap_hugepage_range for private vmas.
*/
-skip:
hugetlb_vma_unlock_write(vma);
}
}
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 795ee393eac0..0455119716ec 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -7614,13 +7614,12 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
p4d_t *p4d = p4d_offset(pgd, addr);
pud_t *pud = pud_offset(p4d, addr);
- i_mmap_assert_write_locked(vma->vm_file->f_mapping);
- hugetlb_vma_assert_locked(vma);
if (sz != PMD_SIZE)
return 0;
if (!ptdesc_pmd_is_shared(virt_to_ptdesc(ptep)))
return 0;
-
+ i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ hugetlb_vma_assert_locked(vma);
pud_clear(pud);
/*
* Once our caller drops the rmap lock, some other process might be
]
|
| 59/2 |
2026/11/26 16:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit e700898fa075c69b3ae02b702ab57fb75e1a82ec
Author: Mike Kravetz <mike.kravetz@oracle.com>
Date: Mon Dec 12 15:50:41 2022 -0800
hugetlb: really allocate vma lock for all sharable vmas
Commit bbff39cc6cbc ("hugetlb: allocate vma lock for all sharable vmas")
removed the pmd sharable checks in the vma lock helper routines. However,
it left the functional version of helper routines behind #ifdef
CONFIG_ARCH_WANT_HUGE_PMD_SHARE. Therefore, the vma lock is not being
used for sharable vmas on architectures that do not support pmd sharing.
On these architectures, a potential fault/truncation race is exposed that
could leave pages in a hugetlb file past i_size until the file is removed.
Move the functional vma lock helpers outside the ifdef, and remove the
non-functional stubs. Since the vma lock is not just for pmd sharing,
rename the routine __vma_shareable_flags_pmd.
Link: https://lkml.kernel.org/r/20221212235042.178355-1-mike.kravetz@oracle.com
Fixes: bbff39cc6cbc ("hugetlb: allocate vma lock for all sharable vmas")
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: James Houghton <jthoughton@google.com>
Cc: Mina Almasry <almasrymina@google.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev>
Cc: Peter Xu <peterx@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 77f36e3681e3..db895230ee7e 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -255,6 +255,152 @@ static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
return subpool_inode(file_inode(vma->vm_file));
}
+/*
+ * hugetlb vma_lock helper routines
+ */
+static bool __vma_shareable_lock(struct vm_area_struct *vma)
+{
+ return vma->vm_flags & (VM_MAYSHARE | VM_SHARED) &&
+ vma->vm_private_data;
+}
+
+void hugetlb_vma_lock_read(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ down_read(&vma_lock->rw_sema);
+ }
+}
+
+void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ up_read(&vma_lock->rw_sema);
+ }
+}
+
+void hugetlb_vma_lock_write(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ down_write(&vma_lock->rw_sema);
+ }
+}
+
+void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ up_write(&vma_lock->rw_sema);
+ }
+}
+
+int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
+{
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ if (!__vma_shareable_lock(vma))
+ return 1;
+
+ return down_write_trylock(&vma_lock->rw_sema);
+}
+
+void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ lockdep_assert_held(&vma_lock->rw_sema);
+ }
+}
+
+void hugetlb_vma_lock_release(struct kref *kref)
+{
+ struct hugetlb_vma_lock *vma_lock = container_of(kref,
+ struct hugetlb_vma_lock, refs);
+
+ kfree(vma_lock);
+}
+
+static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
+{
+ struct vm_area_struct *vma = vma_lock->vma;
+
+ /*
+ * vma_lock structure may or not be released as a result of put,
+ * it certainly will no longer be attached to vma so clear pointer.
+ * Semaphore synchronizes access to vma_lock->vma field.
+ */
+ vma_lock->vma = NULL;
+ vma->vm_private_data = NULL;
+ up_write(&vma_lock->rw_sema);
+ kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
+}
+
+static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
+{
+ if (__vma_shareable_lock(vma)) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ __hugetlb_vma_unlock_write_put(vma_lock);
+ }
+}
+
+static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
+{
+ /*
+ * Only present in sharable vmas.
+ */
+ if (!vma || !__vma_shareable_lock(vma))
+ return;
+
+ if (vma->vm_private_data) {
+ struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
+
+ down_write(&vma_lock->rw_sema);
+ __hugetlb_vma_unlock_write_put(vma_lock);
+ }
+}
+
+static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
+{
+ struct hugetlb_vma_lock *vma_lock;
+
+ /* Only establish in (flags) sharable vmas */
+ if (!vma || !(vma->vm_flags & VM_MAYSHARE))
+ return;
+
+ /* Should never get here with non-NULL vm_private_data */
+ if (vma->vm_private_data)
+ return;
+
+ vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
+ if (!vma_lock) {
+ /*
+ * If we can not allocate structure, then vma can not
+ * participate in pmd sharing. This is only a possible
+ * performance enhancement and memory saving issue.
+ * However, the lock is also used to synchronize page
+ * faults with truncation. If the lock is not present,
+ * unlikely races could leave pages in a file past i_size
+ * until the file is removed. Warn in the unlikely case of
+ * allocation failure.
+ */
+ pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
+ return;
+ }
+
+ kref_init(&vma_lock->refs);
+ init_rwsem(&vma_lock->rw_sema);
+ vma_lock->vma = vma;
+ vma->vm_private_data = vma_lock;
+}
+
/* Helper that removes a struct file_region from the resv_map cache and returns
* it for use.
*/
@@ -6613,7 +6759,8 @@ bool hugetlb_reserve_pages(struct inode *inode,
}
/*
- * vma specific semaphore used for pmd sharing synchronization
+ * vma specific semaphore used for pmd sharing and fault/truncation
+ * synchronization
*/
hugetlb_vma_lock_alloc(vma);
@@ -6869,149 +7016,6 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
*end = ALIGN(*end, PUD_SIZE);
}
-static bool __vma_shareable_flags_pmd(struct vm_area_struct *vma)
-{
- return vma->vm_flags & (VM_MAYSHARE | VM_SHARED) &&
- vma->vm_private_data;
-}
-
-void hugetlb_vma_lock_read(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- down_read(&vma_lock->rw_sema);
- }
-}
-
-void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- up_read(&vma_lock->rw_sema);
- }
-}
-
-void hugetlb_vma_lock_write(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- down_write(&vma_lock->rw_sema);
- }
-}
-
-void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- up_write(&vma_lock->rw_sema);
- }
-}
-
-int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
-{
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- if (!__vma_shareable_flags_pmd(vma))
- return 1;
-
- return down_write_trylock(&vma_lock->rw_sema);
-}
-
-void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- lockdep_assert_held(&vma_lock->rw_sema);
- }
-}
-
-void hugetlb_vma_lock_release(struct kref *kref)
-{
- struct hugetlb_vma_lock *vma_lock = container_of(kref,
- struct hugetlb_vma_lock, refs);
-
- kfree(vma_lock);
-}
-
-static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
-{
- struct vm_area_struct *vma = vma_lock->vma;
-
- /*
- * vma_lock structure may or not be released as a result of put,
- * it certainly will no longer be attached to vma so clear pointer.
- * Semaphore synchronizes access to vma_lock->vma field.
- */
- vma_lock->vma = NULL;
- vma->vm_private_data = NULL;
- up_write(&vma_lock->rw_sema);
- kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
-}
-
-static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
-{
- if (__vma_shareable_flags_pmd(vma)) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- __hugetlb_vma_unlock_write_put(vma_lock);
- }
-}
-
-static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
-{
- /*
- * Only present in sharable vmas.
- */
- if (!vma || !__vma_shareable_flags_pmd(vma))
- return;
-
- if (vma->vm_private_data) {
- struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
-
- down_write(&vma_lock->rw_sema);
- __hugetlb_vma_unlock_write_put(vma_lock);
- }
-}
-
-static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
-{
- struct hugetlb_vma_lock *vma_lock;
-
- /* Only establish in (flags) sharable vmas */
- if (!vma || !(vma->vm_flags & VM_MAYSHARE))
- return;
-
- /* Should never get here with non-NULL vm_private_data */
- if (vma->vm_private_data)
- return;
-
- vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
- if (!vma_lock) {
- /*
- * If we can not allocate structure, then vma can not
- * participate in pmd sharing. This is only a possible
- * performance enhancement and memory saving issue.
- * However, the lock is also used to synchronize page
- * faults with truncation. If the lock is not present,
- * unlikely races could leave pages in a file past i_size
- * until the file is removed. Warn in the unlikely case of
- * allocation failure.
- */
- pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
- return;
- }
-
- kref_init(&vma_lock->refs);
- init_rwsem(&vma_lock->rw_sema);
- vma_lock->vma = vma;
- vma->vm_private_data = vma_lock;
-}
-
/*
* Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
* and returns the corresponding pte. While this is not necessary for the
@@ -7100,47 +7104,6 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
-void hugetlb_vma_lock_read(struct vm_area_struct *vma)
-{
-}
-
-void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
-{
-}
-
-void hugetlb_vma_lock_write(struct vm_area_struct *vma)
-{
-}
-
-void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
-{
-}
-
-int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
-{
- return 1;
-}
-
-void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
-{
-}
-
-void hugetlb_vma_lock_release(struct kref *kref)
-{
-}
-
-static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
-{
-}
-
-static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
-{
-}
-
-static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
-{
-}
-
pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
unsigned long addr, pud_t *pud)
{
]
|
| 60/2 |
2026/11/26 16:55 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 64508
output: 56
thoughts: 1599
Thoughts: **My Analysis of the Hugetlbfs Inode Locking Bug**
It's all perfectly clear now. Let me walk through my thought process to arrive at this understanding.
My investigation points to a very specific root cause within the `hugetlbfs` module, primarily in the `fs/hugetlbfs/inode.c` file, specifically within the `hugetlb_unmap_file_folio()` function. The critical section I'm looking at is this:
if (!hugetlb_vma_trylock_write(vma)) {
vma_lock = vma->vm_private_data;
kref_get(&vma_lock->refs);
break;
}
And then, subsequently, the code block from lines 424 to 459 *assumes* that if `hugetlb_vma_trylock_write(vma)` returns `0` (indicating a failure to acquire the write lock), then `vma->vm_private_data` *must* be a pointer to a `struct hugetlb_vma_lock`. This assumption is the core of the problem.
Now, how did this invariant get broken?
Initially, commit `40549ba8f8e0` (titled "hugetlb: use new vma_lock for pmd sharing synchronization") introduced the `hugetlb_vma_lock` structure, but it was *only* intended for shareable VMAs, hence the call to `__vma_shareable_lock(vma)`. At that time, `hugetlb_vma_trylock_write(vma)` would return `1` unconditionally whenever `!__vma_shareable_lock(vma)` was true. This meant that the failure path in `hugetlb_unmap_file_folio()` could *only* be reached for shareable VMAs, and for those VMAs, `vma->vm_private_data` was indeed guaranteed to be a `struct hugetlb_vma_lock *`.
The problem arose with commit `bf4916922c60` (titled "hugetlbfs: extend hugetlb_vma_lock to private VMAs"). This commit modified `hugetlb_vma_trylock_write(vma)` (and its related locking helpers) to also handle private VMAs. For these private VMAs, it calls `down_write_trylock(&resv_map->rw_sema)` on `vma_resv_map(vma)`. Here's the crucial deviation: for *private* hugetlbfs VMAs that actually possess a reservation map (`resv_map`), `vma->vm_private_data` is *not* a `struct hugetlb_vma_lock *`. Instead, it's a `struct resv_map *` that has its lower bits tagged. Specifically, it's marked with `HPAGE_RESV_OWNER` (which is `1UL << 0`) and potentially `HPAGE_RESV_UNMAPPED` (which is `1UL << 1`).
The scenario where this bug manifests occurs during operations like `ftruncate()` (which eventually calls into `hugetlb_unmap_file_folio()` via `hugetlbfs_setattr()` -> `hugetlb_vmtruncate()` -> `remove_inode_hugepages()` -> `remove_inode_single_folio()`). If this operation encounters a private VMA that maps the file folio, *and* another thread is concurrently holding `resv_map->rw_sema` (this could happen during a `hugetlb_fault()`, `MADV_DONTNEED`, or `munmap`/`exit_mmap` processing), then `hugetlb_vma_trylock_write(vma)` will return `0`.
At this point, `hugetlb_unmap_file_folio()` proceeds to read `vma_lock = vma->vm_private_data`. As we've established, this value is actually a tagged `struct resv_map *` (e.g., the crash report shows it as `R14: ffff888060cd6801`, which is `(resv_map | HPAGE_RESV_OWNER)`). The function then attempts to call `kref_get(&vma_lock->refs)`. Because `vma_lock` is pointing to a `struct resv_map *` and not a `struct hugetlb_vma_lock *`, and due to the tagging, this `kref_get` operates on an *unaligned* address. It's essentially trying to access `refs` which is at offset `+1` byte into the `struct resv_map` (because the `HPAGE_RESV_OWNER` bit is set in the lowest byte). On a little-endian system, reading bytes 1 through 4 (which would correspond to `refs`) might yield `0x00000000` if `refs` itself happens to be 0 and the padding before `lock` is zeroed out. This triggers the `refcount_t: addition on 0; use-after-free.` error at `lib/refcount.c:25`.
Even if `refs` wasn't initially 0, the subsequent operations in that block – `down_write(&vma_lock->rw_sema)`, `vma = vma_lock->vma`, and `kref_put(&vma_lock->refs, hugetlb_vma_lock_release)` – would all be incorrectly treating a `struct resv_map *` (with the `HPAGE_RESV_OWNER` bit set) as if it were a `struct hugetlb_vma_lock *`, leading to undefined behavior and likely crashes.
I also reviewed other related commits to ensure I wasn't missing anything:
* `40549ba8f8e0`: This is the commit that *introduced* the `hugetlb_vma_lock` and the problematic `if (!hugetlb_vma_trylock_write(vma))` block. However, as noted, at that point, the failure path was only reachable for shareable VMAs, where the assumption held.
* `187da0f8250a` ("hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write"): This commit addressed a different issue where `__vma_private_lock()` could dereference a NULL pointer for child VMAs without a `resv_map`. It masked off `HPAGE_RESV_MASK` and enforced `HPAGE_RESV_OWNER`. Importantly, the bug I've identified in `hugetlb_unmap_file_folio()` for `HPAGE_RESV_OWNER` private VMAs was already present and unaffected by this fix.
* `2820b0f09be9` ("hugetlbfs: close race between MADV_DONTNEED and page fault"): This was a follow-up in the same series, moving `hugetlb_vma_lock_write()` to `hugetlb_zap_begin()`. While this increased opportunities for contention on `resv_map->rw_sema`, the actual bug was introduced earlier by `bf4916922c60`.
* `dd83609b8898` ("hugetlbfs: skip VMAs without shareable locks in hugetlb_vmdelete_list") and `cec944dd329f` ("hugetlbfs: move lock assertions after early returns in huge_pmd_unshare()`): The first of these temporarily skipped non-shareable VMAs, and the second reverted that behavior. They don't directly cause or fix the `vm_private_data` interpretation issue.
Therefore, my conclusion is that commit `bf4916922c60f43efaa329744b3eef539aa6a2b2` is the single commit that introduced this specific bug. I will proceed to set the results accordingly.
|
| 61/2 |
2026/11/26 16:55 |
tool |
set-results |
0m
Args: map[CandidateCommits:[bf4916922c60f43efaa329744b3eef539aa6a2b2]]
Results: map[CandidateCommits:[bf4916922c60f43efaa329744b3eef539aa6a2b2]]
|
| 62/2 |
2026/11/26 16:55 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 66215
output: 1498
thoughts: 70
Thoughts: **My Thought Process for Pinpointing the Introducing Commit**
Alright, let's crystallize this. My objective here is to construct a definitive, expert-level explanation that leaves no room for doubt about the root cause of the issue, how I arrived at it through Git history analysis, which specific commits I scrutinized, and precisely why `bf4916922c60f43efaa329744b3eef539aa6a2b2` stands out as the culprit commit.
First and foremost, I need to clearly articulate the **root cause** itself. This isn't just about symptoms; it's about the fundamental flaw, the underlying architectural or logical error that triggered the observed behavior. I'll ensure this is described with the precision and technical depth expected by someone who understands the intricacies of our domain.
Next, the **Git history trace** is crucial. This is my roadmap. I'm not just going to say "it's in this commit." I'm going to detail the *process* of tracing. This involves identifying the first known "good" state or behavior (if possible) and the first known "bad" state or behavior, and then systematically navigating the commit history between them. I'll explain the strategy used – perhaps binary search on commits, or following a specific feature branch's evolution, or looking for commits related to particular modules or functional areas. The narrative needs to demonstrate a methodical approach, not a lucky find.
This tracing naturally leads to the **commits examined**. I need to highlight that this wasn't a cursory glance. I will have dug into the code changes within key commits. What was I looking for? Significant refactoring, new feature introductions, bug fixes (ironically, or especially), changes to dependencies, or modifications in critical logic paths. I'll convey that I didn't just read commit messages; I actually inspected the diffs, understood the implications of the code modifications, and perhaps even tested them in isolation if necessary.
Finally, the **definitive identification of `bf4916922c60f43efaa329744b3eef539aa6a2b2` as the introducing commit**. This is the culmination. I need to explain *why* this specific commit is the one. It's not just because it's the first one where the bug appears; it's because the code *within that commit* demonstrably introduced the logical flaw, altered a crucial dependency, or initiated a behavior change that directly led to the root cause. I will likely tie this back to the specific lines of code or architectural decision made in that commit, explaining how it creates the problematic scenario. This is where the "why" becomes unequivocally clear.
Essentially, I'm laying out a rigorous, scientific investigation process within the Git framework, culminating in irrefutable evidence pointing to `bf4916922c60f43efaa329744b3eef539aa6a2b2` as the origin of the problem.
|
| 63/1 |
2026/11/26 16:55 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:riel@surriel.com AuthorName:Rik van Riel Date:2023-10-05T23:59:07-04:00 Hash:bf4916922c60f43efaa329744b3eef539aa6a2b2 Recipients:[map[Email:akpm@linux-foundation.org Name: To:true] map[Email:mike.kravetz@oracle.com Name: To:true] map[Email:riel@surriel.com Name: To:true]] Title:hugetlbfs: extend hugetlb_vma_lock to private VMAs]] IsRelease:false]
|