| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/27 03:18 | flow | patch-triage |
6mArgs:null Results: {
"EnableConfigs": [
"TRANSPARENT_HUGEPAGE",
"KSM",
"USERFAULTFD",
"HUGETLBFS",
"MEMFD_CREATE",
"PROC_PAGE_MONITOR",
"CMA",
"ZONE_DEVICE",
"NUMA"
],
"FocusSymbols": [
"move_page_tables",
"move_huge_pmd",
"copy_huge_pud",
"try_collapse_pte_mapped_thp",
"ksm_might_need_to_copy",
"do_pagemap_scan",
"memfd_pin_folios",
"userfaultfd_register",
"userfaultfd_unregister",
"deferred_split_scan",
"cma_release"
],
"KMSANReasoning": "The patch series touches several core memory management subsystems (locking, page table manipulation, huge pages, KSM, mremap, and filemap/page cache management). None of these changes introduce or expose risks related to uninitialized memory:\n\n1. Initialization of variables and structures:\n - All newly introduced local variables across the modified functions (such as `range` in mm/userfaultfd.c, `pteval` in mm/rmap.c, `old_pmdval` and `new_pmdval` in mm/mremap.c, `leaked` in mm/cma.c, and `empty` in mm/memory-tiers.c) are fully initialized before use or assigned by functions prior to being evaluated.\n - In `kernel/fork.c`, `dup_mm` now acquires `mmap_write_lock_killable` prior to `memcpy(mm, oldmm, sizeof(*mm))`, ensuring consistent struct state during copy.\n\n2. Kernel-to-user info-leak and bounds handling:\n - No kernel structures with uninitialized fields or padding are copied or exposed to userspace.\n - In `fs/proc/task_mmu.c`, input validation is tightened to prevent 64-bit argument truncation on 32-bit platforms.\n - In `mm/gup.c`, `memfd_pin_folios()` explicitly adds `folio_zero_range()` for non-uptodate folios left behind by `fallocate()`, which resolves a potential leak of uninitialized pages rather than creating one.\n\n3. KASAN vs KMSAN applicability:\n - The primary risks of the modifications in this series involve concurrency, locking hierarchies (e.g., introduction and use of `double_pt_lock`), reference counting, and page table navigation.\n - These failure modes (use-after-free, deadlocks, race conditions, bad pointer dereferences) are effectively detected by KASAN, LOCKDEP, and standard kernel debugging tools. KMSAN provides no additional detection value for these changes.",
"NeedsKMSAN": false,
"Reasoning": "The patch series modifies core memory management (mm) subsystems, including page table manipulation (mremap, khugepaged, huge_memory), KSM copy-on-write and anon_vma checks, userfaultfd register/unregister wakeup flows, memfd pinning, and pagemap scan argument validation. It introduces restructured page table locking (double_pt_lock), new VM_WARN_ON_ONCE_FOLIO assertions in KSM, rmap accounting in copy_huge_pud, and list synchronization in deferred_split_scan. These are fully reachable via user-space syscalls (mremap, madvise, userfaultfd, ioctl, memfd_create) and warrant fuzzing to identify potential deadlocks, race conditions, or assertion violations.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/27 03:18 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 0ac6921d5becf362e53ffa845fb62eeee20cee70\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Tue Oct 27 03:18:52 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c\nindex 7452b695fdaec4..762b6dbb81e4ac 100644\n--- a/fs/proc/task_mmu.c\n+++ b/fs/proc/task_mmu.c\n@@ -3032,6 +3032,12 @@ static int pagemap_scan_get_args(struct pm_scan_arg *arg,\n \t arg-\u003ecategory_anyof_mask | arg-\u003ereturn_mask) \u0026 ~PM_SCAN_CATEGORIES)\n \t\treturn -EINVAL;\n \n+\t/* Don't cut 64-bit numbers down to addresses that happen to work. */\n+\tif (arg-\u003estart != (unsigned long)arg-\u003estart ||\n+\t arg-\u003eend != (unsigned long)arg-\u003eend ||\n+\t arg-\u003evec != (unsigned long)arg-\u003evec)\n+\t\treturn -EFAULT;\n+\n \targ-\u003estart = untagged_addr((unsigned long)arg-\u003estart);\n \targ-\u003eend = untagged_addr((unsigned long)arg-\u003eend);\n \targ-\u003evec = untagged_addr((unsigned long)arg-\u003evec);\ndiff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h\nindex fbe1114e9a443d..3f4a180b48f3c0 100644\n--- a/include/asm-generic/tlb.h\n+++ b/include/asm-generic/tlb.h\n@@ -350,8 +350,7 @@ struct mmu_gather {\n void tlb_flush_mmu(struct mmu_gather *tlb);\n \n static inline void __tlb_adjust_range(struct mmu_gather *tlb,\n-\t\t\t\t unsigned long address,\n-\t\t\t\t unsigned int range_size)\n+\t\tunsigned long address, unsigned long range_size)\n {\n \ttlb-\u003estart = min(tlb-\u003estart, address);\n \ttlb-\u003eend = max(tlb-\u003eend, address + range_size);\ndiff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h\nindex c745f7ad22987f..fac05120ab7744 100644\n--- a/include/linux/huge_mm.h\n+++ b/include/linux/huge_mm.h\n@@ -12,9 +12,8 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n \t\t pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,\n \t\t struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);\n bool huge_pmd_set_accessed(struct vm_fault *vmf);\n-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n-\t\t pud_t *dst_pud, pud_t *src_pud, unsigned long addr,\n-\t\t struct vm_area_struct *vma);\n+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,\n+\t\tpud_t *dst_pud, pud_t *src_pud, unsigned long addr);\n \n #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\n void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud);\ndiff --git a/include/linux/ksm.h b/include/linux/ksm.h\nindex d39d0d5483a23a..a0d358a1feb632 100644\n--- a/include/linux/ksm.h\n+++ b/include/linux/ksm.h\n@@ -91,8 +91,8 @@ static inline void ksm_exit(struct mm_struct *mm)\n * We'd like to make this conditional on vma-\u003evm_flags \u0026 VM_MERGEABLE,\n * but what if the vma was unmerged while the page was swapped out?\n */\n-struct folio *ksm_might_need_to_copy(struct folio *folio,\n-\t\t\tstruct vm_area_struct *vma, unsigned long addr);\n+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,\n+\t\tstruct vm_area_struct *vma, unsigned long addr);\n \n void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);\n void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);\n@@ -145,7 +145,7 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,\n }\n \n static inline struct folio *ksm_might_need_to_copy(struct folio *folio,\n-\t\t\tstruct vm_area_struct *vma, unsigned long addr)\n+\t\tstruct page *page, struct vm_area_struct *vma, unsigned long addr)\n {\n \treturn folio;\n }\ndiff --git a/include/linux/memremap.h b/include/linux/memremap.h\nindex e3c2ccf872a8af..b5e5e626c7a596 100644\n--- a/include/linux/memremap.h\n+++ b/include/linux/memremap.h\n@@ -231,6 +231,7 @@ void memunmap_pages(struct dev_pagemap *pgmap);\n void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);\n void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);\n struct dev_pagemap *get_dev_pagemap(unsigned long pfn);\n+bool pfn_has_dev_pagemap(unsigned long pfn);\n bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);\n \n unsigned long memremap_compat_align(void);\n@@ -284,6 +285,11 @@ static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn)\n \treturn NULL;\n }\n \n+static inline bool pfn_has_dev_pagemap(unsigned long pfn)\n+{\n+\treturn false;\n+}\n+\n static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)\n {\n \treturn false;\ndiff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h\nindex a11a44eef521e9..5db78e5761c5c3 100644\n--- a/include/linux/mmu_notifier.h\n+++ b/include/linux/mmu_notifier.h\n@@ -483,7 +483,8 @@ mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)\n /*\n * This version of mmu_notifier_invalidate_range_start() avoids blocking, but it\n * can return an error if a notifier can't proceed without blocking, in which\n- * case you're not allowed to modify PTEs in the specified range.\n+ * case you're not allowed to modify PTEs in the specified range or call\n+ * mmu_notifier_invalidate_range_end().\n *\n * This is mainly intended for OOM handling.\n */\ndiff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h\nindex a7c7a96a7cbf90..2ceca7993e4f52 100644\n--- a/include/linux/shmem_fs.h\n+++ b/include/linux/shmem_fs.h\n@@ -117,8 +117,14 @@ extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,\n \t\tunsigned long len, unsigned long pgoff, unsigned long flags);\n extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);\n #ifdef CONFIG_SHMEM\n+bool file_inode_is_shmem(const struct inode *inode);\n bool shmem_mapping(const struct address_space *mapping);\n #else\n+static inline bool file_inode_is_shmem(const struct inode *inode)\n+{\n+\treturn false;\n+}\n+\n static inline bool shmem_mapping(const struct address_space *mapping)\n {\n \treturn false;\ndiff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h\nindex a14b8a9ffb7b1c..565e98f342dc83 100644\n--- a/include/linux/userfaultfd_k.h\n+++ b/include/linux/userfaultfd_k.h\n@@ -153,8 +153,6 @@ extern int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,\n \t\t\t unsigned long len, bool enable_rwp);\n \n /* move_pages */\n-void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2);\n-void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2);\n int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,\n \t\t\tstruct vm_area_struct *dst_vma,\n \t\t\tstruct vm_area_struct *src_vma,\ndiff --git a/kernel/fork.c b/kernel/fork.c\nindex 22eaf5fb844d0a..c8948cadccf71d 100644\n--- a/kernel/fork.c\n+++ b/kernel/fork.c\n@@ -1532,12 +1532,19 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,\n \tif (!mm)\n \t\tgoto fail_nomem;\n \n+\tuprobe_start_dup_mmap();\n+\tif (mmap_write_lock_killable(oldmm)) {\n+\t\tfree_mm(mm);\n+\t\tgoto fail_uprobe;\n+\t}\n+\n \tmemcpy(mm, oldmm, sizeof(*mm));\n \n-\tif (!mm_init(mm, tsk))\n-\t\tgoto fail_nomem;\n+\tif (!mm_init(mm, tsk)) {\n+\t\tmmap_write_unlock(oldmm);\n+\t\tgoto fail_uprobe;\n+\t}\n \n-\tuprobe_start_dup_mmap();\n \terr = dup_mmap(mm, oldmm);\n \tif (err)\n \t\tgoto free_pt;\n@@ -1556,9 +1563,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,\n \tmm-\u003ebinfmt = NULL;\n \tmm_init_owner(mm, NULL);\n \tmmput(mm);\n-\tif (err)\n-\t\tuprobe_end_dup_mmap();\n-\n+\tif (!err)\n+\t\treturn NULL;\n+fail_uprobe:\n+\tuprobe_end_dup_mmap();\n fail_nomem:\n \treturn NULL;\n }\ndiff --git a/mm/cma.c b/mm/cma.c\nindex a10ea37a261da3..871f95c9ae9f0e 100644\n--- a/mm/cma.c\n+++ b/mm/cma.c\n@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,\n \t\t unsigned long count)\n {\n \tstruct cma_memrange *cmr;\n-\tunsigned long ret = 0;\n+\tunsigned long leaked = 0;\n \tunsigned long i, pfn;\n \n \tcmr = find_cma_memrange(cma, pages, count);\n@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,\n \n \tpfn = page_to_pfn(pages);\n \tfor (i = 0; i \u003c count; i++, pfn++)\n-\t\tret += !put_page_testzero(pfn_to_page(pfn));\n+\t\tleaked += !page_ref_freeze(pfn_to_page(pfn), 1);\n \n-\tWARN(ret, \"%lu pages are still in use!\\n\", ret);\n+\tif (leaked) {\n+\t\tpr_warn_ratelimited(\"%lu pages are still in use, not freeing CMA region!\\n\",\n+\t\t\t\t leaked);\n+\t\treturn true;\n+\t}\n \n \t__cma_release_frozen(cma, cmr, pages, count);\n \ndiff --git a/mm/filemap.c b/mm/filemap.c\nindex b74bc1e5015c6f..e85a4114d2ecc7 100644\n--- a/mm/filemap.c\n+++ b/mm/filemap.c\n@@ -227,18 +227,6 @@ void __filemap_remove_folio(struct folio *folio, void *shadow)\n \tpage_cache_delete(mapping, folio, shadow);\n }\n \n-static void filemap_free_folio(const struct address_space *mapping,\n-\t\tstruct folio *folio)\n-{\n-\tvoid (*free_folio)(struct folio *);\n-\n-\tfree_folio = mapping-\u003ea_ops-\u003efree_folio;\n-\tif (free_folio)\n-\t\tfree_folio(folio);\n-\n-\tfolio_put_refs(folio, folio_nr_pages(folio));\n-}\n-\n /**\n * filemap_remove_folio - Remove folio from page cache.\n * @folio: The folio.\n@@ -250,6 +238,7 @@ static void filemap_free_folio(const struct address_space *mapping,\n void filemap_remove_folio(struct folio *folio)\n {\n \tstruct address_space *mapping = folio-\u003emapping;\n+\tvoid (*free_folio)(struct folio *folio);\n \n \tBUG_ON(!folio_test_locked(folio));\n \tspin_lock(\u0026mapping-\u003ehost-\u003ei_lock);\n@@ -258,9 +247,12 @@ void filemap_remove_folio(struct folio *folio)\n \txa_unlock_irq(\u0026mapping-\u003ei_pages);\n \tif (mapping_shrinkable(mapping))\n \t\tinode_lru_list_add(mapping-\u003ehost);\n+\tfree_folio = mapping-\u003ea_ops-\u003efree_folio;\n \tspin_unlock(\u0026mapping-\u003ehost-\u003ei_lock);\n \n-\tfilemap_free_folio(mapping, folio);\n+\tif (free_folio)\n+\t\tfree_folio(folio);\n+\tfolio_put_refs(folio, folio_nr_pages(folio));\n }\n \n /*\n@@ -320,6 +312,7 @@ static void page_cache_delete_batch(struct address_space *mapping,\n void delete_from_page_cache_batch(struct address_space *mapping,\n \t\t\t\t struct folio_batch *fbatch)\n {\n+\tvoid (*free_folio)(struct folio *folio);\n \tint i;\n \n \tif (!folio_batch_count(fbatch))\n@@ -337,10 +330,16 @@ void delete_from_page_cache_batch(struct address_space *mapping,\n \txa_unlock_irq(\u0026mapping-\u003ei_pages);\n \tif (mapping_shrinkable(mapping))\n \t\tinode_lru_list_add(mapping-\u003ehost);\n+\tfree_folio = mapping-\u003ea_ops-\u003efree_folio;\n \tspin_unlock(\u0026mapping-\u003ehost-\u003ei_lock);\n \n-\tfor (i = 0; i \u003c folio_batch_count(fbatch); i++)\n-\t\tfilemap_free_folio(mapping, fbatch-\u003efolios[i]);\n+\tfor (i = 0; i \u003c folio_batch_count(fbatch); i++) {\n+\t\tstruct folio *folio = fbatch-\u003efolios[i];\n+\n+\t\tif (free_folio)\n+\t\t\tfree_folio(folio);\n+\t\tfolio_put_refs(folio, folio_nr_pages(folio));\n+\t}\n }\n \n int filemap_check_errors(struct address_space *mapping)\n@@ -1374,18 +1373,30 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,\n \t * return value based on that state without races.\n \t *\n \t * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive\n-\t * waiter, but an exclusive one requires WQ_FLAG_DONE.\n+\t * waiter, but an exclusive one must also acquire the bit.\n \t */\n-\tif (behavior == EXCLUSIVE)\n-\t\treturn wait-\u003eflags \u0026 WQ_FLAG_DONE ? 0 : -EINTR;\n+\tif (behavior == EXCLUSIVE) {\n+\t\tif (wait-\u003eflags \u0026 WQ_FLAG_DONE)\n+\t\t\treturn 0;\n+\t\t/*\n+\t\t * We might have just been woken up and the waker will not\n+\t\t * wake up anybody else. If we ignore that now, the remaining\n+\t\t * waiters may not get woken up: take the bit now if it is\n+\t\t * still free.\n+\t\t */\n+\t\tif ((wait-\u003eflags \u0026 WQ_FLAG_WOKEN) \u0026\u0026\n+\t\t !test_and_set_bit(bit_nr, folio_flags(folio, 0)))\n+\t\t\treturn 0;\n+\t\treturn -EINTR;\n+\t}\n \n \treturn wait-\u003eflags \u0026 WQ_FLAG_WOKEN ? 0 : -EINTR;\n }\n \n #ifdef CONFIG_MIGRATION\n /**\n- * softleaf_entry_wait_on_locked - Wait for a migration entry or\n- * device_private entry to be removed.\n+ * softleaf_entry_wait_on_locked - Wait for a migration entry to be removed\n+ * or a device-private folio in a device-private entry to get unlocked.\n * @entry: migration or device_private swap entry.\n * @ptl: already locked ptl. This function will drop the lock.\n *\n@@ -3297,11 +3308,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,\n * @folio - the folio to lock.\n * @fpin - the pointer to the file we may pin (or is already pinned).\n *\n- * This works similar to lock_folio_or_retry in that it can drop the\n- * mmap_lock. It differs in that it actually returns the folio locked\n- * if it returns 1 and 0 if it couldn't lock the folio. If we did have\n- * to drop the mmap_lock then fpin will point to the pinned file and\n- * needs to be fput()'ed at a later point.\n+ * Returns 1 with the folio locked, or 0 if locking failed.\n+ *\n+ * On both entry and return, a non-NULL *fpin means the fault lock has\n+ * already been dropped and the caller must eventually fput() the file.\n+ * Otherwise the fault lock is still held, including on failure.\n */\n static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,\n \t\t\t\t struct file **fpin)\n@@ -3318,21 +3329,9 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,\n \t\treturn 0;\n \n \t*fpin = maybe_unlock_mmap_for_io(vmf, *fpin);\n-\tif (vmf-\u003eflags \u0026 FAULT_FLAG_KILLABLE) {\n-\t\tif (__folio_lock_killable(folio)) {\n-\t\t\t/*\n-\t\t\t * We didn't have the right flags to drop the\n-\t\t\t * fault lock, but all fault_handlers only check\n-\t\t\t * for fatal signals if we return VM_FAULT_RETRY,\n-\t\t\t * so we need to drop the fault lock here and\n-\t\t\t * return 0 if we don't have a fpin.\n-\t\t\t */\n-\t\t\tif (*fpin == NULL)\n-\t\t\t\trelease_fault_lock(vmf);\n-\t\t\treturn 0;\n-\t\t}\n-\t} else\n-\t\t__folio_lock(folio);\n+\tif (vmf-\u003eflags \u0026 FAULT_FLAG_KILLABLE)\n+\t\treturn !__folio_lock_killable(folio);\n+\t__folio_lock(folio);\n \n \treturn 1;\n }\n@@ -3561,7 +3560,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)\n * vma-\u003evm_mm-\u003emmap_lock must be held on entry.\n *\n * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock\n- * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().\n+ * may be dropped before doing I/O or when aborting an interrupted wait.\n *\n * If our return value does not have VM_FAULT_RETRY set, the mmap_lock\n * has not been released.\n@@ -3717,9 +3716,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)\n \n out_retry:\n \t/*\n-\t * We dropped the mmap_lock, we need to return to the fault handler to\n-\t * re-find the vma and come back and find our hopefully still populated\n-\t * page.\n+\t * With fpin set, the fault lock was already dropped. Otherwise it is\n+\t * still held. Release invalidate_lock before dropping either protection.\n \t */\n \tif (!IS_ERR(folio))\n \t\tfolio_put(folio);\n@@ -3727,6 +3725,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)\n \t\tfilemap_invalidate_unlock_shared(mapping);\n \tif (fpin)\n \t\tfput(fpin);\n+\telse if (!(vmf-\u003eflags \u0026 FAULT_FLAG_RETRY_NOWAIT))\n+\t\trelease_fault_lock(vmf);\n \treturn ret | VM_FAULT_RETRY;\n }\n EXPORT_SYMBOL(filemap_fault);\ndiff --git a/mm/gup.c b/mm/gup.c\nindex f166acf794e308..48e1bfe5ab9a96 100644\n--- a/mm/gup.c\n+++ b/mm/gup.c\n@@ -658,9 +658,6 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,\n \n \tassert_spin_locked(pud_lockptr(mm, pudp));\n \n-\tif (!pud_present(pud))\n-\t\treturn NULL;\n-\n \tif ((flags \u0026 FOLL_WRITE) \u0026\u0026\n \t !can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags))\n \t\treturn NULL;\n@@ -896,15 +893,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,\n }\n \n static struct page *follow_pmd_mask(struct vm_area_struct *vma,\n-\t\t\t\t unsigned long address, pud_t *pudp,\n-\t\t\t\t unsigned int flags,\n-\t\t\t\t unsigned long *page_mask)\n+\t\tunsigned long address, pud_t *pudp, pud_t pud,\n+\t\tunsigned int flags, unsigned long *page_mask)\n {\n \tpmd_t *pmd, pmdval;\n \tspinlock_t *ptl;\n \tstruct page *page;\n \tstruct mm_struct *mm = vma-\u003evm_mm;\n \n+\tif (unlikely(pud_bad(pud)))\n+\t\treturn no_page_table(vma, flags, address);\n+\n \tpmd = pmd_offset(pudp, address);\n \tpmdval = pmdp_get_lockless(pmd);\n \tif (pmd_none(pmdval))\n@@ -953,18 +952,23 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma,\n \tpud = pudp_get(pudp);\n \tif (!pud_present(pud))\n \t\treturn no_page_table(vma, flags, address);\n-\tif (pud_leaf(pud)) {\n-\t\tptl = pud_lock(mm, pudp);\n-\t\tpage = follow_huge_pud(vma, address, pudp, flags, page_mask);\n+\tif (!pud_leaf(pud))\n+\t\treturn follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);\n+\n+\tptl = pud_lock(mm, pudp);\n+\tpud = *pudp;\n+\tif (unlikely(!pud_present(pud))) {\n \t\tspin_unlock(ptl);\n-\t\tif (page)\n-\t\t\treturn page;\n \t\treturn no_page_table(vma, flags, address);\n \t}\n-\tif (unlikely(pud_bad(pud)))\n-\t\treturn no_page_table(vma, flags, address);\n+\tif (unlikely(!pud_leaf(pud))) {\n+\t\tspin_unlock(ptl);\n+\t\treturn follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);\n+\t}\n \n-\treturn follow_pmd_mask(vma, address, pudp, flags, page_mask);\n+\tpage = follow_huge_pud(vma, address, pudp, flags, page_mask);\n+\tspin_unlock(ptl);\n+\treturn page;\n }\n \n static struct page *follow_p4d_mask(struct vm_area_struct *vma,\n@@ -3200,6 +3204,9 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,\n \t\t\t\t FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT)))\n \t\treturn -EINVAL;\n \n+\tif (unlikely(!nr_pages))\n+\t\treturn 0;\n+\n \tif (gup_flags \u0026 FOLL_PIN)\n \t\tmm_set_has_pinned_flag(current-\u003emm);\n \n@@ -3207,8 +3214,8 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,\n \t\tmight_lock_read(\u0026current-\u003emm-\u003emmap_lock);\n \n \tstart = untagged_addr(start) \u0026 PAGE_MASK;\n-\tlen = nr_pages \u003c\u003c PAGE_SHIFT;\n-\tif (check_add_overflow(start, len, \u0026end))\n+\tif (check_shl_overflow(nr_pages, PAGE_SHIFT, \u0026len) ||\n+\t check_add_overflow(start, len, \u0026end))\n \t\treturn -EOVERFLOW;\n \tif (end \u003e TASK_SIZE_MAX)\n \t\treturn -EFAULT;\n@@ -3512,6 +3519,20 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,\n \t\t\t\t\tgoto err;\n \t\t\t\t}\n \n+\t\t\t\t/*\n+\t\t\t\t * fallocate() can leave folios in the\n+\t\t\t\t * page cache that were never cleared.\n+\t\t\t\t */\n+\t\t\t\tif (!folio_test_uptodate(folio)) {\n+\t\t\t\t\tfolio_lock(folio);\n+\t\t\t\t\tif (!folio_test_uptodate(folio)) {\n+\t\t\t\t\t\tfolio_zero_range(folio, 0, folio_size(folio));\n+\t\t\t\t\t\tflush_dcache_folio(folio);\n+\t\t\t\t\t\tfolio_mark_uptodate(folio);\n+\t\t\t\t\t}\n+\t\t\t\t\tfolio_unlock(folio);\n+\t\t\t\t}\n+\n \t\t\t\tif (nr_folios == 0)\n \t\t\t\t\t*offset = offset_in_folio(folio, start);\n \ndiff --git a/mm/huge_memory.c b/mm/huge_memory.c\nindex bd44d58e86845c..d090642ac8c2b6 100644\n--- a/mm/huge_memory.c\n+++ b/mm/huge_memory.c\n@@ -1500,7 +1500,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)\n \t\tfolio_unlock(folio);\n \t\tfolio_put(folio);\n \t} else {\n-\t\tspin_unlock(ptl);\n+\t\tsoftleaf_entry_wait_on_locked(entry, ptl);\n \t}\n \n \treturn ret;\n@@ -2072,11 +2072,13 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr,\n \t\tupdate_mmu_cache_pud(vma, addr, pud);\n }\n \n-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n-\t\t pud_t *dst_pud, pud_t *src_pud, unsigned long addr,\n-\t\t struct vm_area_struct *vma)\n+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,\n+\t\tpud_t *dst_pud, pud_t *src_pud, unsigned long addr)\n {\n+\tstruct mm_struct *dst_mm = dst_vma-\u003evm_mm;\n+\tstruct mm_struct *src_mm = src_vma-\u003evm_mm;\n \tspinlock_t *dst_ptl, *src_ptl;\n+\tstruct page *page;\n \tpud_t pud;\n \tint ret;\n \n@@ -2089,11 +2091,20 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n \tif (unlikely(!pud_trans_huge(pud)))\n \t\tgoto out_unlock;\n \n-\t/*\n-\t * TODO: once we support anonymous pages, use\n-\t * folio_try_dup_anon_rmap_*() and split if duplicating fails.\n-\t */\n-\tif (vma_is_cow_mapping(vma) \u0026\u0026 pud_write(pud)) {\n+\tpage = vm_normal_page_pud(src_vma, addr, pud);\n+\tif (page) {\n+\t\tstruct folio *folio = page_folio(page);\n+\n+\t\tfolio_get(folio);\n+\t\t/*\n+\t\t * TODO: once we support anonymous pages, use\n+\t\t * folio_try_dup_anon_rmap_*() and split if duplicating fails.\n+\t\t */\n+\t\tfolio_add_file_rmap_pud(folio, page, dst_vma);\n+\t\tadd_mm_counter(dst_mm, mm_counter_file(folio), HPAGE_PUD_NR);\n+\t}\n+\n+\tif (vma_is_cow_mapping(src_vma) \u0026\u0026 pud_write(pud)) {\n \t\tpudp_set_wrprotect(src_mm, addr, src_pud);\n \t\tpud = pud_wrprotect(pud);\n \t}\n@@ -2482,7 +2493,15 @@ static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)\n static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,\n \t\tpmd_t pmdval, struct folio *folio, bool is_present)\n {\n-\tconst bool is_device_private = folio_is_device_private(folio);\n+\tconst bool is_device_private = !is_present \u0026\u0026 pmd_is_device_private_entry(pmdval);\n+\n+\t/* Transfer young+dirty while the folio is still mapped. */\n+\tif (is_present \u0026\u0026 !folio_test_anon(folio)) {\n+\t\tif (pmd_dirty(pmdval))\n+\t\t\tfolio_mark_dirty(folio);\n+\t\tif (pmd_young(pmdval) \u0026\u0026 likely(vma_has_recency(vma)))\n+\t\t\tfolio_mark_accessed(folio);\n+\t}\n \n \t/* Present and device private folios are rmappable. */\n \tif (is_present || is_device_private)\n@@ -2493,12 +2512,6 @@ static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,\n \t} else {\n \t\tadd_mm_counter(mm, mm_counter_file(folio),\n \t\t\t -HPAGE_PMD_NR);\n-\n-\t\tif (is_present \u0026\u0026 pmd_dirty(pmdval))\n-\t\t\tfolio_mark_dirty(folio);\n-\t\tif (is_present \u0026\u0026 pmd_young(pmdval) \u0026\u0026\n-\t\t likely(vma_has_recency(vma)))\n-\t\t\tfolio_mark_accessed(folio);\n \t}\n \n \t/* Device private folios are pinned. */\n@@ -2632,15 +2645,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,\n \t\treturn false;\n \t}\n \n-\t/*\n-\t * We don't have to worry about the ordering of src and dst\n-\t * ptlocks because exclusive mmap_lock prevents deadlock.\n-\t */\n-\told_ptl = __pmd_trans_huge_lock(old_pmd, vma);\n-\tif (old_ptl) {\n-\t\tnew_ptl = pmd_lockptr(mm, new_pmd);\n-\t\tif (new_ptl != old_ptl)\n-\t\t\tspin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);\n+\told_ptl = pmd_lockptr(mm, old_pmd);\n+\tnew_ptl = pmd_lockptr(mm, new_pmd);\n+\n+\tdouble_pt_lock(old_ptl, new_ptl);\n+\tif (likely(pmd_is_huge(*old_pmd))) {\n \t\tpmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);\n \t\tif (pmd_present(pmd))\n \t\t\tforce_flush = true;\n@@ -2669,11 +2678,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,\n \t\tset_pmd_at(mm, new_addr, new_pmd, pmd);\n \t\tif (force_flush)\n \t\t\tflush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);\n-\t\tif (new_ptl != old_ptl)\n-\t\t\tspin_unlock(new_ptl);\n-\t\tspin_unlock(old_ptl);\n+\t\tdouble_pt_unlock(old_ptl, new_ptl);\n \t\treturn true;\n \t}\n+\tdouble_pt_unlock(old_ptl, new_ptl);\n \treturn false;\n }\n \n@@ -3495,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,\n \t__split_huge_pmd(vma, pmd, address, freeze);\n }\n \n-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)\n+static bool pud_split_required(const struct vm_area_struct *vma,\n+\t\tunsigned long address)\n {\n+\tif (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))\n+\t\treturn false;\n+\tif (IS_ALIGNED(address, HPAGE_PUD_SIZE))\n+\t\treturn false;\n \t/*\n-\t * If the new address isn't hpage aligned and it could previously\n-\t * contain an hugepage: check if we need to split an huge pmd.\n+\t * Only selected VMAs support mapping PUDs for now. Lift the\n+\t * restriction once this changes.\n \t */\n-\tif (!IS_ALIGNED(address, HPAGE_PMD_SIZE) \u0026\u0026\n-\t range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),\n-\t\t\t ALIGN(address, HPAGE_PMD_SIZE)))\n-\t\tsplit_huge_pmd_address(vma, address, false);\n+\tif (!vma_is_dax(vma) \u0026\u0026 !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))\n+\t\treturn false;\n+\treturn range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),\n+\t\t\t ALIGN(address, HPAGE_PUD_SIZE));\n+}\n+\n+static bool pmd_split_required(const struct vm_area_struct *vma,\n+\t\tunsigned long address)\n+{\n+\tif (IS_ALIGNED(address, HPAGE_PMD_SIZE))\n+\t\treturn false;\n+\n+\treturn range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),\n+\t\t\t ALIGN(address, HPAGE_PMD_SIZE));\n+}\n+\n+static void split_huge_leaf(struct vm_area_struct *vma,\n+\t\tunsigned long address)\n+{\n+\tconst bool split_pud = pud_split_required(vma, address);\n+\tconst bool split_pmd = pmd_split_required(vma, address);\n+\tpgd_t *pgd;\n+\tp4d_t *p4d;\n+\tpud_t *pud;\n+\tpmd_t *pmd;\n+\n+\t/*\n+\t * mmap, VMA and rmap write locks protect the walk; the split helpers\n+\t * take the page table locks.\n+\t */\n+\tif (!split_pud \u0026\u0026 !split_pmd)\n+\t\treturn;\n+\n+\tpgd = pgd_offset(vma-\u003evm_mm, address);\n+\tif (!pgd_present(*pgd))\n+\t\treturn;\n+\n+\tp4d = p4d_offset(pgd, address);\n+\tif (!p4d_present(*p4d))\n+\t\treturn;\n+\n+\tpud = pud_offset(p4d, address);\n+\tif (split_pud \u0026\u0026 pud_leaf(*pud))\n+\t\t__split_huge_pud(vma, pud, address);\n+\tif (!split_pmd || !pud_present(*pud))\n+\t\treturn;\n+\n+\tpmd = pmd_offset(pud, address);\n+\t__split_huge_pmd(vma, pmd, address, false);\n }\n \n void vma_adjust_trans_huge(struct vm_area_struct *vma,\n@@ -3513,14 +3571,14 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,\n \t\t\t struct vm_area_struct *next)\n {\n \t/* Check if we need to split start first. */\n-\tsplit_huge_pmd_if_needed(vma, start);\n+\tsplit_huge_leaf(vma, start);\n \n \t/* Check if we need to split end next. */\n-\tsplit_huge_pmd_if_needed(vma, end);\n+\tsplit_huge_leaf(vma, end);\n \n \t/* If we're incrementing next-\u003evm_start, we might need to split it. */\n \tif (next)\n-\t\tsplit_huge_pmd_if_needed(next, end);\n+\t\tsplit_huge_leaf(next, end);\n }\n \n /*\n@@ -4522,8 +4580,7 @@ int split_folio_to_list(struct folio *folio, struct list_head *list)\n *\n * It is unsafe to call folio_unqueue_deferred_split() until folio refcount is\n * zero: because even when the list_lru lock is held, a non-empty\n- * _deferred_list might be in use on deferred_split_scan()'s unlocked\n- * on-stack list.\n+ * _deferred_list might be in use on deferred_split_scan()'s on-stack list.\n *\n * The list_lru sublist is determined by folio's memcg: it is therefore\n * important to unqueue deferred split before changing folio memcg.\n@@ -4665,10 +4722,22 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,\n \t\t\t\t\t deferred_split_isolate, \u0026dispose);\n \n \tlist_for_each_entry_safe(folio, next, \u0026dispose, _deferred_list) {\n+\t\tstruct mem_cgroup *memcg;\n+\t\tstruct list_lru_one *lru;\n \t\tbool did_split = false;\n \t\tbool underused = false;\n \n+\t\t/*\n+\t\t * Sync with deferred_split_folio(), which can requeue the\n+\t\t * folio once list_empty() is true, and sets PG_partially_mapped.\n+\t\t */\n+\t\trcu_read_lock();\n+\t\tmemcg = folio_memcg(folio);\n+\t\tlru = list_lru_lock_irq(\u0026deferred_split_lru, folio_nid(folio),\n+\t\t\t\t\t\u0026memcg);\n \t\tlist_del_init(\u0026folio-\u003e_deferred_list);\n+\t\tlist_lru_unlock_irq(lru);\n+\t\trcu_read_unlock();\n \n \t\tif (!folio_test_partially_mapped(folio)) {\n \t\t\t/*\ndiff --git a/mm/hugetlb.c b/mm/hugetlb.c\nindex da5d04edf7c108..84141527fe73d3 100644\n--- a/mm/hugetlb.c\n+++ b/mm/hugetlb.c\n@@ -5153,15 +5153,9 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,\n \tspinlock_t *src_ptl, *dst_ptl;\n \tpte_t pte;\n \n-\tdst_ptl = huge_pte_lock(h, mm, dst_pte);\n+\tdst_ptl = huge_pte_lockptr(h, mm, dst_pte);\n \tsrc_ptl = huge_pte_lockptr(h, mm, src_pte);\n-\n-\t/*\n-\t * We don't have to worry about the ordering of src and dst ptlocks\n-\t * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.\n-\t */\n-\tif (src_ptl != dst_ptl)\n-\t\tspin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);\n+\tdouble_pt_lock(dst_ptl, src_ptl);\n \n \tpte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);\n \n@@ -5190,9 +5184,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,\n \t\tset_huge_pte_at(mm, new_addr, dst_pte, pte, sz);\n \t}\n \n-\tif (src_ptl != dst_ptl)\n-\t\tspin_unlock(src_ptl);\n-\tspin_unlock(dst_ptl);\n+\tdouble_pt_unlock(dst_ptl, src_ptl);\n }\n \n int move_hugetlb_page_tables(struct vm_area_struct *vma,\ndiff --git a/mm/internal.h b/mm/internal.h\nindex b1b0f29d2216ee..7e4d88b2fb02c8 100644\n--- a/mm/internal.h\n+++ b/mm/internal.h\n@@ -619,6 +619,34 @@ static inline void force_page_cache_readahead(struct address_space *mapping,\n \tforce_page_cache_ra(\u0026ractl, nr_to_read);\n }\n \n+/*\n+ * Take two page table locks at the same level in address order such that\n+ * we cannot deadlock.\n+ */\n+static inline void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2)\n+\t__acquires(ptl1)\n+\t__acquires(ptl2)\n+{\n+\tif (ptl1 \u003e ptl2)\n+\t\tswap(ptl1, ptl2);\n+\tspin_lock(ptl1);\n+\tif (ptl1 != ptl2)\n+\t\tspin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);\n+\telse\n+\t\t__acquire(ptl2);\n+}\n+\n+static inline void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2)\n+\t__releases(ptl1)\n+\t__releases(ptl2)\n+{\n+\tspin_unlock(ptl1);\n+\tif (ptl1 != ptl2)\n+\t\tspin_unlock(ptl2);\n+\telse\n+\t\t__release(ptl2);\n+}\n+\n unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,\n \t\tpgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);\n unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,\ndiff --git a/mm/khugepaged.c b/mm/khugepaged.c\nindex 4b71d010b2aff5..4313abf9cfcb97 100644\n--- a/mm/khugepaged.c\n+++ b/mm/khugepaged.c\n@@ -10,6 +10,7 @@\n #include \u003clinux/mm_inline.h\u003e\n #include \u003clinux/kthread.h\u003e\n #include \u003clinux/khugepaged.h\u003e\n+#include \u003clinux/hugetlb.h\u003e\n #include \u003clinux/freezer.h\u003e\n #include \u003clinux/mman.h\u003e\n #include \u003clinux/hashtable.h\u003e\n@@ -1832,6 +1833,17 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a\n \treturn SCAN_SUCCEED;\n }\n \n+static bool pte_table_empty(pte_t *pte)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c HPAGE_PMD_NR; i++, pte++) {\n+\t\tif (!pte_none(ptep_get(pte)))\n+\t\t\treturn false;\n+\t}\n+\treturn true;\n+}\n+\n static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,\n \t\tbool install_pmd)\n {\n@@ -1904,7 +1916,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign\n \n \tresult = SCAN_FAIL;\n \tstart_pte = pte_offset_map_lock(mm, pmd, haddr, \u0026ptl);\n-\tif (!start_pte)\t\t/* mmap_lock + page lock should prevent this */\n+\tif (!start_pte)\n \t\tgoto drop_folio;\n \n \t/* step 1: check all mapped PTEs are to the right huge page */\n@@ -1940,26 +1952,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign\n \tmmu_notifier_invalidate_range_start(\u0026range);\n \tnotified = true;\n \n-\t/*\n-\t * pmd_lock covers a wider range than ptl, and (if split from mm's\n-\t * page_table_lock) ptl nests inside pml. The less time we hold pml,\n-\t * the better; but userfaultfd's mfill_atomic_pte() on a private VMA\n-\t * inserts a valid as-if-COWed PTE without even looking up page cache.\n-\t * So page lock of folio does not protect from it, so we must not drop\n-\t * ptl before pgt_pmd is removed, so uffd private needs pml taken now.\n-\t */\n-\tif (userfaultfd_armed(vma) \u0026\u0026 !(vma-\u003evm_flags \u0026 VM_SHARED))\n-\t\tpml = pmd_lock(mm, pmd);\n-\n-\tstart_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, \u0026pgt_pmd, \u0026ptl);\n-\tif (!start_pte)\t\t/* mmap_lock + page lock should prevent this */\n-\t\tgoto abort;\n-\tif (!pml)\n-\t\tspin_lock(ptl);\n-\telse if (ptl != pml)\n-\t\tspin_lock_nested(ptl, SINGLE_DEPTH_NESTING);\n-\n-\tif (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))\n+\tstart_pte = pte_offset_map_lock(mm, pmd, haddr, \u0026ptl);\n+\tif (!start_pte)\n \t\tgoto abort;\n \n \t/* step 2: clear page table and adjust rmap */\n@@ -1991,36 +1985,41 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign\n \n \t\tnr_batch_ptes = folio_pte_batch(folio, pte, ptent, max_nr_batch_ptes);\n \n-\t\t/*\n-\t\t * Must clear entry, or a racing truncate may re-remove it.\n-\t\t * TLB flush can be left until pmdp_collapse_flush() does it.\n-\t\t * PTE dirty? Shmem page is already dirty; file is read-only.\n-\t\t */\n-\t\tclear_ptes(mm, addr, pte, nr_batch_ptes);\n+\t\t/* Must clear entry, or a racing truncate may re-remove it. */\n+\t\tptent = get_and_clear_ptes(mm, addr, pte, nr_batch_ptes);\n+\t\tif (pte_dirty(ptent))\n+\t\t\tfolio_mark_dirty(folio);\n \t\tfolio_remove_rmap_ptes(folio, page, nr_batch_ptes, vma);\n \t\tnr_mapped_ptes += nr_batch_ptes;\n \t}\n \n-\tif (!pml)\n-\t\tspin_unlock(ptl);\n-\n \t/* step 3: set proper refcount and mm_counters. */\n \tif (nr_mapped_ptes) {\n \t\tfolio_ref_sub(folio, nr_mapped_ptes);\n \t\tadd_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);\n \t}\n \n-\t/* step 4: remove empty page table */\n-\tif (!pml) {\n+\t/* step 4: acquire the PMD lock and remove the empty page table */\n+\tpml = pmd_lockptr(mm, pmd);\n+\tif (ptl != pml \u0026\u0026 !spin_trylock(pml)) {\n+\t\t/* We must flush before dropping the PTL. */\n+\t\tif (nr_mapped_ptes)\n+\t\t\tflush_tlb_range(vma, haddr, end);\n+\t\tpte_unmap_unlock(start_pte, ptl);\n+\t\tptl = NULL;\n+\n \t\tpml = pmd_lock(mm, pmd);\n-\t\tif (ptl != pml) {\n+\t\tstart_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, \u0026pgt_pmd, \u0026ptl);\n+\t\tif (!start_pte)\n+\t\t\tgoto unlock;\n+\t\tif (ptl != pml)\n \t\t\tspin_lock_nested(ptl, SINGLE_DEPTH_NESTING);\n-\t\t\tif (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {\n-\t\t\t\tflush_tlb_mm(mm);\n-\t\t\t\tgoto unlock;\n-\t\t\t}\n-\t\t}\n+\n+\t\tif (!pte_table_empty(start_pte))\n+\t\t\tgoto unlock;\n \t}\n+\n+\t/* This also flushes the zapped folio ptes properly. */\n \tpgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);\n \tpmdp_get_lockless_sync();\n \tpte_unmap_unlock(start_pte, ptl);\n@@ -2041,7 +2040,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign\n \tgoto drop_folio;\n abort:\n \tif (nr_mapped_ptes) {\n-\t\tflush_tlb_mm(mm);\n+\t\tflush_tlb_range(vma, haddr, end);\n \t\tfolio_ref_sub(folio, nr_mapped_ptes);\n \t\tadd_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);\n \t}\ndiff --git a/mm/ksm.c b/mm/ksm.c\nindex dbfb8f98b18925..e95f6b67236023 100644\n--- a/mm/ksm.c\n+++ b/mm/ksm.c\n@@ -475,6 +475,7 @@ static void advisor_stop_scan(void)\n #ifdef CONFIG_NUMA\n /* Zeroed when merging across nodes is not allowed */\n static unsigned int ksm_merge_across_nodes = 1;\n+/* Number of allocated stable and unstable tree roots. */\n static int ksm_nr_node_ids = 1;\n #else\n #define ksm_merge_across_nodes\t1U\n@@ -3136,24 +3137,52 @@ void __ksm_exit(struct mm_struct *mm)\n \ttrace_ksm_exit(mm);\n }\n \n-struct folio *ksm_might_need_to_copy(struct folio *folio,\n-\t\t\tstruct vm_area_struct *vma, unsigned long addr)\n+static bool folio_anon_rmap_compatible(const struct folio *folio,\n+\t\tconst struct page *page, struct vm_area_struct *vma,\n+\t\tunsigned long addr)\n {\n-\tstruct page *page = folio_page(folio, 0);\n-\tstruct anon_vma *anon_vma = folio_anon_vma(folio);\n-\tstruct folio *new_folio;\n+\tconst struct anon_vma *anon_vma = folio_anon_vma(folio);\n+\tstruct anon_vma_chain *avc;\n \n-\tif (folio_test_large(folio))\n-\t\treturn folio;\n+\tVM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);\n+\tVM_WARN_ON_ONCE_FOLIO(page_folio(page) != folio, folio);\n+\tVM_WARN_ON_ONCE_FOLIO(folio_test_ksm(folio), folio);\n+\tvma_assert_stabilised(vma);\n+\n+\tif (!anon_vma)\n+\t\treturn true;\n+\tif (page_pgoff(folio, page) != linear_anon_page_index(vma, addr))\n+\t\treturn false;\n+\n+\t/*\n+\t * Careful: anon_vma might have been freed, so we cannot safely\n+\t * dereference it. Add a fast path before we walk the anon_vma chain.\n+\t */\n+\tif (anon_vma == vma-\u003eanon_vma || anon_vma == vma-\u003eanon_vma-\u003eroot)\n+\t\treturn true;\n+\n+\tlist_for_each_entry(avc, \u0026vma-\u003eanon_vma_chain, same_vma) {\n+\t\tif (avc-\u003eanon_vma == anon_vma)\n+\t\t\treturn true;\n+\t}\n+\treturn false;\n+}\n \n+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,\n+\t\tstruct vm_area_struct *vma, unsigned long addr)\n+{\n+\tstruct folio *new_folio;\n+\n+\t/*\n+\t * A KSM folio is small, but the slot that it left behind can be swapped\n+\t * in as a part of a large folio by one of the processes that shared it.\n+\t * So test, and copy, the page of the slot.\n+\t */\n \tif (folio_test_ksm(folio)) {\n \t\tif (folio_stable_node(folio) \u0026\u0026\n \t\t !(ksm_run \u0026 KSM_RUN_UNMERGE))\n \t\t\treturn folio;\t/* no need to copy it */\n-\t} else if (!anon_vma) {\n-\t\treturn folio;\t\t/* no need to copy it */\n-\t} else if (folio-\u003eindex == linear_anon_page_index(vma, addr) \u0026\u0026\n-\t\t\tanon_vma-\u003eroot == vma-\u003eanon_vma-\u003eroot) {\n+\t} else if (folio_anon_rmap_compatible(folio, page, vma, addr)) {\n \t\treturn folio;\t\t/* still no need to copy it */\n \t}\n \tif (PageHWPoison(page))\n@@ -3636,14 +3665,13 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj,\n \t\t\telse {\n \t\t\t\troot_stable_tree = buf;\n \t\t\t\troot_unstable_tree = buf + nr_node_ids;\n+\t\t\t\tksm_nr_node_ids = nr_node_ids;\n \t\t\t\t/* Stable tree is empty but not the unstable */\n \t\t\t\troot_unstable_tree[0] = one_unstable_tree[0];\n \t\t\t}\n \t\t}\n-\t\tif (!err) {\n+\t\tif (!err)\n \t\t\tksm_merge_across_nodes = knob;\n-\t\t\tksm_nr_node_ids = knob ? 1 : nr_node_ids;\n-\t\t}\n \t}\n \tmutex_unlock(\u0026ksm_thread_mutex);\n \ndiff --git a/mm/madvise.c b/mm/madvise.c\nindex 75b409e97dbf24..a16e3cb256b7b0 100644\n--- a/mm/madvise.c\n+++ b/mm/madvise.c\n@@ -756,6 +756,15 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,\n \t\tif (folio_test_swapcache(folio) || folio_test_dirty(folio)) {\n \t\t\tif (!folio_trylock(folio))\n \t\t\t\tcontinue;\n+\t\t\t/*\n+\t\t\t * KSM folios can gain new mappings at any time, so we must\n+\t\t\t * not clear their dirty flag.\n+\t\t\t */\n+\t\t\tif (folio_test_ksm(folio)) {\n+\t\t\t\tfolio_unlock(folio);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\n \t\t\t/*\n \t\t\t * If we have a large folio at this point, we know it is\n \t\t\t * fully mapped so if its mapcount is the same as its\ndiff --git a/mm/memfd.c b/mm/memfd.c\nindex 0f6fff004f5e4c..6f5fbce74f21eb 100644\n--- a/mm/memfd.c\n+++ b/mm/memfd.c\n@@ -211,12 +211,14 @@ static int memfd_wait_for_pins(struct address_space *mapping)\n \n static unsigned int *memfd_file_seals_ptr(struct file *file)\n {\n-\tif (shmem_file(file))\n-\t\treturn \u0026SHMEM_I(file_inode(file))-\u003eseals;\n+\tstruct inode *inode = file_inode(file);\n+\n+\tif (file_inode_is_shmem(inode))\n+\t\treturn \u0026SHMEM_I(inode)-\u003eseals;\n \n #ifdef CONFIG_HUGETLBFS\n \tif (is_file_hugepages(file))\n-\t\treturn \u0026HUGETLBFS_I(file_inode(file))-\u003eseals;\n+\t\treturn \u0026HUGETLBFS_I(inode)-\u003eseals;\n #endif\n \n \treturn NULL;\n@@ -482,6 +484,8 @@ struct file *memfd_alloc_file(const char *name, unsigned int flags)\n \t}\n \n \tfile-\u003ef_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;\n+\t/* a regular file like any other: POSIX.1-2008/SUSv4 Section XSI 2.9.7 */\n+\tfile-\u003ef_mode |= FMODE_ATOMIC_POS;\n \tfile-\u003ef_flags |= O_LARGEFILE;\n \n \tif (flags \u0026 MFD_NOEXEC_SEAL) {\ndiff --git a/mm/memfd_luo.c b/mm/memfd_luo.c\nindex 59de210bee5f9e..c0811407ce3973 100644\n--- a/mm/memfd_luo.c\n+++ b/mm/memfd_luo.c\n@@ -582,7 +582,7 @@ static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler,\n {\n \tstruct inode *inode = file_inode(file);\n \n-\treturn shmem_file(file) \u0026\u0026 !inode-\u003ei_nlink;\n+\treturn file_inode_is_shmem(inode) \u0026\u0026 !inode-\u003ei_nlink;\n }\n \n static unsigned long memfd_luo_get_id(struct file *file)\ndiff --git a/mm/memory-tiers.c b/mm/memory-tiers.c\nindex 25e121851b5862..8eda313cf61925 100644\n--- a/mm/memory-tiers.c\n+++ b/mm/memory-tiers.c\n@@ -572,21 +572,14 @@ static struct memory_tier *set_node_memory_tier(int node)\n \treturn memtier;\n }\n \n-static void destroy_memory_tier(struct memory_tier *memtier)\n+static struct memory_tier *clear_node_memory_tier(int node)\n {\n-\tlist_del(\u0026memtier-\u003elist);\n-\tdevice_unregister(\u0026memtier-\u003edev);\n-}\n-\n-static bool clear_node_memory_tier(int node)\n-{\n-\tbool cleared = false;\n \tpg_data_t *pgdat;\n \tstruct memory_tier *memtier;\n \n \tpgdat = NODE_DATA(node);\n \tif (!pgdat)\n-\t\treturn false;\n+\t\treturn NULL;\n \n \t/*\n \t * Make sure that anybody looking at NODE_DATA who finds\n@@ -604,14 +597,10 @@ static bool clear_node_memory_tier(int node)\n \t\tsynchronize_rcu();\n \t\tmemtype = node_memory_types[node].memtype;\n \t\tnode_clear(node, memtype-\u003enodes);\n-\t\tif (nodes_empty(memtype-\u003enodes)) {\n+\t\tif (nodes_empty(memtype-\u003enodes))\n \t\t\tlist_del_init(\u0026memtype-\u003etier_sibling);\n-\t\t\tif (list_empty(\u0026memtier-\u003ememory_types))\n-\t\t\t\tdestroy_memory_tier(memtier);\n-\t\t}\n-\t\tcleared = true;\n \t}\n-\treturn cleared;\n+\treturn memtier;\n }\n \n static void release_memtype(struct kref *kref)\n@@ -884,13 +873,21 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,\n {\n \tstruct memory_tier *memtier;\n \tstruct node_notify *nn = _arg;\n+\tbool empty = false;\n \n \tswitch (action) {\n \tcase NODE_REMOVED_LAST_MEMORY:\n \t\tmutex_lock(\u0026memory_tier_lock);\n-\t\tif (clear_node_memory_tier(nn-\u003enid))\n+\t\tmemtier = clear_node_memory_tier(nn-\u003enid);\n+\t\tif (memtier) {\n+\t\t\tempty = list_empty(\u0026memtier-\u003ememory_types);\n+\t\t\tif (empty)\n+\t\t\t\tlist_del(\u0026memtier-\u003elist);\n \t\t\testablish_demotion_targets();\n+\t\t}\n \t\tmutex_unlock(\u0026memory_tier_lock);\n+\t\tif (empty)\n+\t\t\tdevice_unregister(\u0026memtier-\u003edev);\n \t\tbreak;\n \tcase NODE_ADDED_FIRST_MEMORY:\n \t\tmutex_lock(\u0026memory_tier_lock);\ndiff --git a/mm/memory.c b/mm/memory.c\nindex 5d6d695675bce4..be4c05db37d33b 100644\n--- a/mm/memory.c\n+++ b/mm/memory.c\n@@ -1463,7 +1463,6 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,\n \t unsigned long end)\n {\n \tstruct mm_struct *dst_mm = dst_vma-\u003evm_mm;\n-\tstruct mm_struct *src_mm = src_vma-\u003evm_mm;\n \tpud_t *src_pud, *dst_pud;\n \tunsigned long next;\n \n@@ -1477,8 +1476,7 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,\n \t\t\tint err;\n \n \t\t\tVM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);\n-\t\t\terr = copy_huge_pud(dst_mm, src_mm,\n-\t\t\t\t\t dst_pud, src_pud, addr, src_vma);\n+\t\t\terr = copy_huge_pud(dst_vma, src_vma, dst_pud, src_pud, addr);\n \t\t\tif (err == -ENOMEM)\n \t\t\t\treturn -ENOMEM;\n \t\t\tif (!err)\n@@ -3763,6 +3761,13 @@ static inline int __wp_page_copy_user(struct page *dst, struct page *src,\n \t\treturn 0;\n \t}\n \n+\t/*\n+\t * __copy_from_user_inatomic() only works as expected for local faults.\n+\t * Reject remote faults.\n+\t */\n+\tif (unlikely(mm != current-\u003emm))\n+\t\treturn -EFAULT;\n+\n \t/*\n \t * If the source page was a PFN mapping, we don't have\n \t * a \"struct page\" for it. We do a best-effort copy by\n@@ -4088,6 +4093,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)\n \t\t\t\tfolio_put(old_folio);\n \n \t\t\tdelayacct_wpcopy_end();\n+\t\t\tif (err == -EFAULT)\n+\t\t\t\treturn VM_FAULT_SIGBUS;\n \t\t\treturn err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;\n \t\t}\n \t\tkmsan_copy_page_meta(\u0026new_folio-\u003epage, vmf-\u003epage);\n@@ -4642,8 +4649,8 @@ EXPORT_SYMBOL_GPL(unmap_mapping_pages);\n void unmap_mapping_range(struct address_space *mapping,\n \t\tloff_t const holebegin, loff_t const holelen, int even_cows)\n {\n-\tpgoff_t hba = (pgoff_t)(holebegin) \u003e\u003e PAGE_SHIFT;\n-\tpgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) \u003e\u003e PAGE_SHIFT;\n+\tpgoff_t hba = (u64)holebegin \u003e\u003e PAGE_SHIFT;\n+\tpgoff_t hlen = ((u64)holelen + PAGE_SIZE - 1) \u003e\u003e PAGE_SHIFT;\n \n \t/* Check for overflow. */\n \tif (sizeof(holelen) \u003e sizeof(hlen)) {\n@@ -5089,7 +5096,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)\n \t * folio-\u003eindex of non-ksm folios would be nonlinear inside the\n \t * anon VMA -- the ksm flag is lost on actual swapout.\n \t */\n-\tfolio = ksm_might_need_to_copy(folio, vma, vmf-\u003eaddress);\n+\tfolio = ksm_might_need_to_copy(folio, page, vma, vmf-\u003eaddress);\n \tif (unlikely(!folio)) {\n \t\tret = VM_FAULT_OOM;\n \t\tfolio = swapcache;\n@@ -5271,7 +5278,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)\n \tif (unlikely(folio != swapcache)) {\n \t\tfolio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE);\n \t\tfolio_add_lru_vma(folio, vma);\n-\t\tfolio_put_swap(swapcache, NULL);\n+\t\tfolio_put_swap(swapcache,\n+\t\t\t folio_file_page(swapcache, swp_offset(entry)));\n \t} else if (!folio_test_anon(folio)) {\n \t\t/*\n \t\t * We currently only expect !anon folios that are fully\n@@ -6950,8 +6958,12 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,\n \n \tlru_gen_exit_fault();\n \n-\t/* If the mapping is droppable, then errors due to OOM aren't fatal. */\n-\tif (is_droppable)\n+\t/*\n+\t * If the mapping is droppable, then errors due to OOM aren't fatal: the\n+\t * task returns to user mode, handles its signals and tries again. A\n+\t * fault from kernel mode has no such return, so it must see the error.\n+\t */\n+\tif (is_droppable \u0026\u0026 (flags \u0026 FAULT_FLAG_USER))\n \t\tret \u0026= ~VM_FAULT_OOM;\n \n \tif (flags \u0026 FAULT_FLAG_USER) {\ndiff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c\nindex 796af1028ee239..5a972d7a9e4787 100644\n--- a/mm/memory_hotplug.c\n+++ b/mm/memory_hotplug.c\n@@ -342,7 +342,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages)\n struct page *pfn_to_online_page(unsigned long pfn)\n {\n \tunsigned long nr = pfn_to_section_nr(pfn);\n-\tstruct dev_pagemap *pgmap;\n \tstruct mem_section *ms;\n \n \tBUILD_BUG_ON(IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID));\n@@ -363,14 +362,12 @@ struct page *pfn_to_online_page(unsigned long pfn)\n \t/*\n \t * Slowpath: when ZONE_DEVICE collides with\n \t * ZONE_{NORMAL,MOVABLE} within the same section some pfns in\n-\t * the section may be 'offline' but 'valid'. Only\n-\t * get_dev_pagemap() can determine sub-section online status.\n+\t * the section may be 'offline' but 'valid'. Only the presence of a\n+\t * pgmap can determine sub-section online status: it indicates a\n+\t * ZONE_DEVICE offline pfn. That includes a pgmap whose reference has\n+\t * been killed, its memmap is about to go away.\n \t */\n-\tpgmap = get_dev_pagemap(pfn);\n-\tput_dev_pagemap(pgmap);\n-\n-\t/* The presence of a pgmap indicates ZONE_DEVICE offline pfn */\n-\tif (pgmap)\n+\tif (pfn_has_dev_pagemap(pfn))\n \t\treturn NULL;\n \n \treturn pfn_to_page(pfn);\ndiff --git a/mm/memremap.c b/mm/memremap.c\nindex accba23aef28cc..a1e99a4c55c561 100644\n--- a/mm/memremap.c\n+++ b/mm/memremap.c\n@@ -413,6 +413,19 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn)\n }\n EXPORT_SYMBOL_GPL(get_dev_pagemap);\n \n+/**\n+ * pfn_has_dev_pagemap() - does @pfn belong to a dev_pagemap, dead or alive\n+ * @pfn: page frame number to lookup page_map\n+ *\n+ * In contrast to get_dev_pagemap() this also finds a dev_pagemap which is on\n+ * its way out: it is registered before its pages are added, and until they\n+ * have been removed.\n+ */\n+bool pfn_has_dev_pagemap(unsigned long pfn)\n+{\n+\treturn xa_load(\u0026pgmap_array, pfn);\n+}\n+\n void free_zone_device_folio(struct folio *folio)\n {\n \tstruct dev_pagemap *pgmap = folio-\u003epgmap;\ndiff --git a/mm/mmap.c b/mm/mmap.c\nindex 148ba01c03739f..7618e72766a522 100644\n--- a/mm/mmap.c\n+++ b/mm/mmap.c\n@@ -1713,8 +1713,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)\n \tLIST_HEAD(uf);\n \tVMA_ITERATOR(vmi, mm, 0);\n \n-\tif (mmap_write_lock_killable(oldmm))\n-\t\treturn -EINTR;\n+\tmmap_assert_write_locked(oldmm);\n \tflush_cache_dup_mm(oldmm);\n \tuprobe_dup_mmap(oldmm, mm);\n \t/*\n@@ -1725,11 +1724,6 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)\n \t/* No ordering required: file already has been exposed. */\n \tdup_mm_exe_file(mm, oldmm);\n \n-\tmm-\u003etotal_vm = oldmm-\u003etotal_vm;\n-\tmm-\u003edata_vm = oldmm-\u003edata_vm;\n-\tmm-\u003eexec_vm = oldmm-\u003eexec_vm;\n-\tmm-\u003estack_vm = oldmm-\u003estack_vm;\n-\n \t/* Use __mt_dup() to efficiently build an identical maple tree. */\n \tretval = __mt_dup(\u0026oldmm-\u003emm_mt, \u0026mm-\u003emm_mt, GFP_KERNEL);\n \tif (unlikely(retval))\ndiff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c\nindex df69ba6e797f76..a7bae5de5fedc1 100644\n--- a/mm/mmu_notifier.c\n+++ b/mm/mmu_notifier.c\n@@ -569,16 +569,25 @@ int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)\n {\n \tstruct mmu_notifier_subscriptions *subscriptions =\n \t\trange-\u003emm-\u003enotifier_subscriptions;\n-\tint ret;\n+\tconst bool has_itree = subscriptions-\u003ehas_itree;\n+\tint ret = 0;\n \n-\tif (subscriptions-\u003ehas_itree) {\n+\tif (has_itree) {\n \t\tret = mn_itree_invalidate(subscriptions, range);\n \t\tif (ret)\n \t\t\treturn ret;\n \t}\n \tif (!hlist_empty(\u0026subscriptions-\u003elist))\n-\t\treturn mn_hlist_invalidate_range_start(subscriptions, range);\n-\treturn 0;\n+\t\tret = mn_hlist_invalidate_range_start(subscriptions, range);\n+\n+\t/*\n+\t * On error, the non-blocking caller is not allowed to call\n+\t * invalidate_range_end(), as in mn_itree_invalidate().\n+\t */\n+\tif (ret \u0026\u0026 has_itree \u0026\u0026 !mmu_notifier_range_blockable(range))\n+\t\tmn_itree_inv_end(subscriptions);\n+\n+\treturn ret;\n }\n \n static void\n@@ -938,7 +947,6 @@ static int __mmu_interval_notifier_insert(\n \tstruct mmu_notifier_subscriptions *subscriptions, unsigned long start,\n \tunsigned long length, const struct mmu_interval_notifier_ops *ops)\n {\n-\tinterval_sub-\u003emm = mm;\n \tinterval_sub-\u003eops = ops;\n \tRB_CLEAR_NODE(\u0026interval_sub-\u003einterval_tree.rb);\n \tinterval_sub-\u003einterval_tree.start = start;\n@@ -955,8 +963,12 @@ static int __mmu_interval_notifier_insert(\n \tif (WARN_ON(atomic_read(\u0026mm-\u003emm_users) \u003c= 0))\n \t\treturn -EINVAL;\n \n-\t/* pairs with mmdrop in mmu_interval_notifier_remove() */\n+\t/*\n+\t * Pairs with mmdrop in mmu_interval_notifier_remove(). Drivers take a\n+\t * non-NULL -\u003emm for \"registered\", so set it only with the reference.\n+\t */\n \tmmgrab(mm);\n+\tinterval_sub-\u003emm = mm;\n \n \t/*\n \t * If some invalidate_range_start/end region is going on in parallel\ndiff --git a/mm/mremap.c b/mm/mremap.c\nindex 0d06d7f37f19a1..70b7264aa6bf5c 100644\n--- a/mm/mremap.c\n+++ b/mm/mremap.c\n@@ -87,7 +87,7 @@ static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)\n \t\treturn NULL;\n \n \tpud = pud_offset(p4d, addr);\n-\tif (pud_none_or_clear_bad(pud))\n+\tif (!pud_trans_huge(*pud) \u0026\u0026 pud_none_or_clear_bad(pud))\n \t\treturn NULL;\n \n \treturn pud;\n@@ -202,7 +202,7 @@ static int move_ptes(struct pagetable_move_control *pmc,\n \tstruct mm_struct *mm = vma-\u003evm_mm;\n \tpte_t *old_ptep, *new_ptep;\n \tpte_t old_pte, pte;\n-\tpmd_t dummy_pmdval;\n+\tpmd_t old_pmdval, new_pmdval;\n \tspinlock_t *old_ptl, *new_ptl;\n \tbool force_flush = false;\n \tunsigned long old_addr = pmc-\u003eold_addr;\n@@ -234,31 +234,30 @@ static int move_ptes(struct pagetable_move_control *pmc,\n \tif (pmc-\u003eneed_rmap_locks)\n \t\ttake_rmap_locks(vma);\n \n-\t/*\n-\t * We don't have to worry about the ordering of src and dst\n-\t * pte locks because exclusive mmap_lock prevents deadlock.\n-\t */\n-\told_ptep = pte_offset_map_lock(mm, old_pmd, old_addr, \u0026old_ptl);\n+\told_ptep = pte_offset_map_rw_nolock(mm, old_pmd, old_addr, \u0026old_pmdval,\n+\t\t\t\t\t \u0026old_ptl);\n \tif (!old_ptep) {\n \t\terr = -EAGAIN;\n \t\tgoto out;\n \t}\n-\t/*\n-\t * Now new_pte is none, so collapse_scan_file() path can not find\n-\t * this by traversing file-\u003ef_mapping, so there is no concurrency with\n-\t * retract_page_tables(). In addition, we already hold the exclusive\n-\t * mmap_lock, so this new_pte page is stable, so there is no need to get\n-\t * pmdval and do pmd_same() check.\n-\t */\n-\tnew_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, \u0026dummy_pmdval,\n-\t\t\t\t\t \u0026new_ptl);\n+\tnew_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, \u0026new_pmdval,\n+\t\t\t\t\t \u0026new_ptl);\n \tif (!new_ptep) {\n-\t\tpte_unmap_unlock(old_ptep, old_ptl);\n+\t\tpte_unmap(old_ptep);\n+\t\terr = -EAGAIN;\n+\t\tgoto out;\n+\t}\n+\n+\tdouble_pt_lock(old_ptl, new_ptl);\n+\tif (unlikely(!pmd_same(old_pmdval, pmdp_get_lockless(old_pmd)) ||\n+\t\t !pmd_same(new_pmdval, pmdp_get_lockless(new_pmd)))) {\n+\t\tdouble_pt_unlock(old_ptl, new_ptl);\n+\t\tpte_unmap(new_ptep);\n+\t\tpte_unmap(old_ptep);\n \t\terr = -EAGAIN;\n \t\tgoto out;\n \t}\n-\tif (new_ptl != old_ptl)\n-\t\tspin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);\n+\n \tflush_tlb_batched_pending(vma-\u003evm_mm);\n \tlazy_mmu_mode_enable();\n \n@@ -317,10 +316,9 @@ static int move_ptes(struct pagetable_move_control *pmc,\n \tlazy_mmu_mode_disable();\n \tif (force_flush)\n \t\tflush_tlb_range(vma, old_end - len, old_end);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_unlock(new_ptl);\n+\tdouble_pt_unlock(old_ptl, new_ptl);\n \tpte_unmap(new_ptep - 1);\n-\tpte_unmap_unlock(old_ptep - 1, old_ptl);\n+\tpte_unmap(old_ptep - 1);\n out:\n \tif (pmc-\u003eneed_rmap_locks)\n \t\tdrop_rmap_locks(vma);\n@@ -395,14 +393,9 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,\n \tif (WARN_ON_ONCE(!pmd_none(*new_pmd)))\n \t\treturn false;\n \n-\t/*\n-\t * We don't have to worry about the ordering of src and dst\n-\t * ptlocks because exclusive mmap_lock prevents deadlock.\n-\t */\n-\told_ptl = pmd_lock(mm, old_pmd);\n+\told_ptl = pmd_lockptr(mm, old_pmd);\n \tnew_ptl = pmd_lockptr(mm, new_pmd);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);\n+\tdouble_pt_lock(old_ptl, new_ptl);\n \n \tpmd = *old_pmd;\n \n@@ -418,9 +411,7 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,\n \tpmd_populate(mm, new_pmd, pmd_pgtable(pmd));\n \tflush_tlb_range(vma, pmc-\u003eold_addr, pmc-\u003eold_addr + PMD_SIZE);\n out_unlock:\n-\tif (new_ptl != old_ptl)\n-\t\tspin_unlock(new_ptl);\n-\tspin_unlock(old_ptl);\n+\tdouble_pt_unlock(old_ptl, new_ptl);\n \n \treturn res;\n }\n@@ -452,26 +443,24 @@ static bool move_normal_pud(struct pagetable_move_control *pmc,\n \tif (WARN_ON_ONCE(!pud_none(*new_pud)))\n \t\treturn false;\n \n-\t/*\n-\t * We don't have to worry about the ordering of src and dst\n-\t * ptlocks because exclusive mmap_lock prevents deadlock.\n-\t */\n-\told_ptl = pud_lock(mm, old_pud);\n+\told_ptl = pud_lockptr(mm, old_pud);\n \tnew_ptl = pud_lockptr(mm, new_pud);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);\n \n-\t/* Clear the pud */\n+\tdouble_pt_lock(old_ptl, new_ptl);\n+\n \tpud = *old_pud;\n+\tif (unlikely(pud_none(pud)))\n+\t\tgoto out_unlock;\n+\n+\t/* Clear the pud */\n \tpud_clear(old_pud);\n \n \tVM_BUG_ON(!pud_none(*new_pud));\n \n \tpud_populate(mm, new_pud, pud_pgtable(pud));\n \tflush_tlb_range(vma, pmc-\u003eold_addr, pmc-\u003eold_addr + PUD_SIZE);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_unlock(new_ptl);\n-\tspin_unlock(old_ptl);\n+out_unlock:\n+\tdouble_pt_unlock(old_ptl, new_ptl);\n \n \treturn true;\n }\n@@ -499,18 +488,17 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,\n \tif (WARN_ON_ONCE(!pud_none(*new_pud)))\n \t\treturn false;\n \n-\t/*\n-\t * We don't have to worry about the ordering of src and dst\n-\t * ptlocks because exclusive mmap_lock prevents deadlock.\n-\t */\n-\told_ptl = pud_lock(mm, old_pud);\n+\told_ptl = pud_lockptr(mm, old_pud);\n \tnew_ptl = pud_lockptr(mm, new_pud);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);\n+\n+\tdouble_pt_lock(old_ptl, new_ptl);\n+\tif (unlikely(!pud_trans_huge(*old_pud))) {\n+\t\tdouble_pt_unlock(old_ptl, new_ptl);\n+\t\treturn false;\n+\t}\n \n \t/* Clear the pud */\n-\tpud = *old_pud;\n-\tpud_clear(old_pud);\n+\tpud = pudp_huge_get_and_clear(mm, pmc-\u003eold_addr, old_pud);\n \n \tVM_BUG_ON(!pud_none(*new_pud));\n \n@@ -518,9 +506,7 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,\n \t/* mark soft_ditry when we add pud level soft dirty support */\n \tset_pud_at(mm, pmc-\u003enew_addr, new_pud, pud);\n \tflush_pud_tlb_range(vma, pmc-\u003eold_addr, pmc-\u003eold_addr + HPAGE_PUD_SIZE);\n-\tif (new_ptl != old_ptl)\n-\t\tspin_unlock(new_ptl);\n-\tspin_unlock(old_ptl);\n+\tdouble_pt_unlock(old_ptl, new_ptl);\n \n \treturn true;\n }\n@@ -842,11 +828,10 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc)\n \t\tif (!new_pud)\n \t\t\tbreak;\n \t\tif (pud_trans_huge(*old_pud)) {\n-\t\t\tif (extent == HPAGE_PUD_SIZE) {\n-\t\t\t\tmove_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud);\n-\t\t\t\t/* We ignore and continue on error? */\n+\t\t\tif (extent == HPAGE_PUD_SIZE \u0026\u0026\n+\t\t\t move_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud))\n \t\t\t\tcontinue;\n-\t\t\t}\n+\t\t\tsplit_huge_pud(pmc-\u003eold, old_pud, pmc-\u003eold_addr);\n \t\t} else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) \u0026\u0026 extent == PUD_SIZE) {\n \t\t\tif (move_pgt_entry(pmc, NORMAL_PUD, old_pud, new_pud))\n \t\t\t\tcontinue;\ndiff --git a/mm/nommu.c b/mm/nommu.c\nindex ed44510e37707d..6e0f9c7da2364b 100644\n--- a/mm/nommu.c\n+++ b/mm/nommu.c\n@@ -1926,7 +1926,7 @@ subsys_initcall(init_admin_reserve);\n \n int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)\n {\n-\tmmap_write_lock(oldmm);\n+\tmmap_assert_write_locked(oldmm);\n \tdup_mm_exe_file(mm, oldmm);\n \tmmap_write_unlock(oldmm);\n \treturn 0;\ndiff --git a/mm/rmap.c b/mm/rmap.c\nindex 1131b76bbbc28b..0faef46806e5ca 100644\n--- a/mm/rmap.c\n+++ b/mm/rmap.c\n@@ -924,6 +924,7 @@ static bool folio_referenced_one(struct folio *folio,\n \tDEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);\n \tint ptes = 0, referenced = 0;\n \tunsigned int nr;\n+\tpte_t pteval;\n \n \twhile (page_vma_mapped_walk(\u0026pvmw)) {\n \t\taddress = pvmw.address;\n@@ -968,10 +969,12 @@ static bool folio_referenced_one(struct folio *folio,\n \t\t\treturn false;\n \t\t}\n \n-\t\tif (pvmw.pte \u0026\u0026 folio_test_large(folio)) {\n+\t\tif (pvmw.pte)\n+\t\t\tpteval = ptep_get(pvmw.pte);\n+\n+\t\tif (pvmw.pte \u0026\u0026 pte_present(pteval) \u0026\u0026 folio_test_large(folio)) {\n \t\t\tconst unsigned long end_addr = pmd_addr_end(address, vma-\u003evm_end);\n \t\t\tconst unsigned int max_nr = (end_addr - address) \u003e\u003e PAGE_SHIFT;\n-\t\t\tpte_t pteval = ptep_get(pvmw.pte);\n \n \t\t\tnr = folio_pte_batch(folio, pvmw.pte, pteval, max_nr);\n \t\t}\n@@ -981,11 +984,23 @@ static bool folio_referenced_one(struct folio *folio,\n \t\t * are. —they could be on active/inactive lists or on MGLRU. So the\n \t\t * simplest approach is to disable this look-around optimization.\n \t\t */\n-\t\tif (lru_gen_enabled() \u0026\u0026 !lru_gen_switching() \u0026\u0026 pvmw.pte) {\n+\t\tif (lru_gen_enabled() \u0026\u0026 !lru_gen_switching() \u0026\u0026 pvmw.pte \u0026\u0026\n+\t\t pte_present(pteval)) {\n \t\t\tif (lru_gen_look_around(\u0026pvmw, nr))\n \t\t\t\treferenced++;\n \t\t} else if (pvmw.pte) {\n-\t\t\tif (clear_flush_young_ptes_notify(vma, address, pvmw.pte, nr))\n+\t\t\tbool young = false;\n+\n+\t\t\t/*\n+\t\t\t * PFN swap PTEs, such as device-exclusive ones, that\n+\t\t\t * actually map pages are \"old\" from a CPU perspective.\n+\t\t\t * The MMU notifier takes care of any device aspects.\n+\t\t\t */\n+\t\t\tif (pte_present(pteval))\n+\t\t\t\tyoung = clear_flush_young_ptes(vma, address, pvmw.pte, nr);\n+\t\t\tyoung |= mmu_notifier_clear_flush_young(vma-\u003evm_mm, address,\n+\t\t\t\t\t\t\t\taddress + nr * PAGE_SIZE);\n+\t\t\tif (young)\n \t\t\t\treferenced++;\n \t\t} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {\n \t\t\tif (pmdp_clear_flush_young_notify(vma, address,\ndiff --git a/mm/shmem.c b/mm/shmem.c\nindex fadf23f9e21edb..0d41aac125df9c 100644\n--- a/mm/shmem.c\n+++ b/mm/shmem.c\n@@ -275,6 +275,11 @@ static const struct vm_operations_struct shmem_vm_ops;\n static const struct vm_operations_struct shmem_anon_vm_ops;\n static struct file_system_type shmem_fs_type;\n \n+bool file_inode_is_shmem(const struct inode *inode)\n+{\n+\treturn inode-\u003ei_fop == \u0026shmem_file_operations;\n+}\n+\n bool shmem_mapping(const struct address_space *mapping)\n {\n \treturn mapping-\u003ea_ops == \u0026shmem_aops;\ndiff --git a/mm/swapfile.c b/mm/swapfile.c\nindex 254ce86fa923e6..71ef9e2efb3a98 100644\n--- a/mm/swapfile.c\n+++ b/mm/swapfile.c\n@@ -2514,7 +2514,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,\n \t\treturn 0;\n \n \tswapcache = folio;\n-\tfolio = ksm_might_need_to_copy(folio, vma, addr);\n+\tfolio = ksm_might_need_to_copy(folio,\n+\t\t\t\t folio_file_page(folio, swp_offset(entry)), vma, addr);\n \tif (unlikely(!folio))\n \t\treturn -ENOMEM;\n \telse if (unlikely(folio == ERR_PTR(-EHWPOISON))) {\ndiff --git a/mm/truncate.c b/mm/truncate.c\nindex 733d2713a875a5..6d8c0ec4623feb 100644\n--- a/mm/truncate.c\n+++ b/mm/truncate.c\n@@ -499,7 +499,15 @@ void truncate_inode_pages_range(struct address_space *mapping,\n \t\t\tfolio_lock(folio);\n \t\t\tVM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);\n \t\t\tfolio_wait_writeback(folio);\n-\t\t\ttruncate_inode_folio(mapping, folio);\n+\t\t\tif (truncate_inode_folio(mapping, folio)) {\n+\t\t\t\tfolio_unlock(folio);\n+\t\t\t\t/*\n+\t\t\t\t * Folio might have been replaced: look it up\n+\t\t\t\t * again.\n+\t\t\t\t */\n+\t\t\t\tindex = indices[i];\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t\tfolio_unlock(folio);\n \t\t}\n \t\ttruncate_folio_batch_exceptionals(mapping, \u0026fbatch, indices);\n@@ -760,7 +768,12 @@ int invalidate_inode_pages2_range(struct address_space *mapping,\n \t\t\tfolio_lock(folio);\n \t\t\tif (unlikely(folio-\u003emapping != mapping)) {\n \t\t\t\tfolio_unlock(folio);\n-\t\t\t\tcontinue;\n+\t\t\t\t/*\n+\t\t\t\t * Folio might have been replaced: look up\n+\t\t\t\t * again.\n+\t\t\t\t */\n+\t\t\t\tindex = indices[i];\n+\t\t\t\tbreak;\n \t\t\t}\n \t\t\tVM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);\n \t\t\tfolio_wait_writeback(folio);\ndiff --git a/mm/userfaultfd.c b/mm/userfaultfd.c\nindex 669f71d485d02e..76de84510340cf 100644\n--- a/mm/userfaultfd.c\n+++ b/mm/userfaultfd.c\n@@ -1206,33 +1206,6 @@ int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,\n \treturn 0;\n }\n \n-void double_pt_lock(spinlock_t *ptl1,\n-\t\t spinlock_t *ptl2)\n-\t__acquires(ptl1)\n-\t__acquires(ptl2)\n-{\n-\tif (ptl1 \u003e ptl2)\n-\t\tswap(ptl1, ptl2);\n-\t/* lock in virtual address order to avoid lock inversion */\n-\tspin_lock(ptl1);\n-\tif (ptl1 != ptl2)\n-\t\tspin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);\n-\telse\n-\t\t__acquire(ptl2);\n-}\n-\n-void double_pt_unlock(spinlock_t *ptl1,\n-\t\t spinlock_t *ptl2)\n-\t__releases(ptl1)\n-\t__releases(ptl2)\n-{\n-\tspin_unlock(ptl1);\n-\tif (ptl1 != ptl2)\n-\t\tspin_unlock(ptl2);\n-\telse\n-\t\t__release(ptl2);\n-}\n-\n static inline bool is_pte_pages_stable(pte_t *dst_pte, pte_t *src_pte,\n \t\t\t\t pte_t orig_dst_pte, pte_t orig_src_pte,\n \t\t\t\t pmd_t *dst_pmd, pmd_t dst_pmdval)\n@@ -2270,6 +2243,9 @@ static struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,\n \treturn ret;\n }\n \n+static void wake_userfault_range(struct userfaultfd_ctx *ctx,\n+\t\tunsigned long start, unsigned long end);\n+\n /* Assumes mmap write lock taken, and mm_struct pinned. */\n static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,\n \t\t\t struct vm_area_struct *vma,\n@@ -2287,6 +2263,8 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,\n \t\tprev = vma;\n \n \tfor_each_vma_range(vmi, vma, end) {\n+\t\tconst bool any_mode_removed = vma-\u003evm_flags \u0026 __VM_UFFD_FLAGS \u0026 ~vm_flags;\n+\n \t\tcond_resched();\n \n \t\tVM_WARN_ON_ONCE(!vma_can_userfault(vma, vm_flags, wp_async));\n@@ -2332,6 +2310,10 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,\n \t\t */\n \t\tuserfaultfd_set_ctx(vma, ctx, vm_flags);\n \n+\t\t/* Retry faults for removed modes while the VMA is write-locked. */\n+\t\tif (any_mode_removed)\n+\t\t\twake_userfault_range(ctx, start, vma_end);\n+\n \t\tif (vma_is_hugetlb(vma) \u0026\u0026 uffd_disable_huge_pmd_share(vma))\n \t\t\thugetlb_unshare_all_pmds(vma);\n \n@@ -3648,6 +3630,17 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,\n \t\t__wake_userfault(ctx, range);\n }\n \n+static void wake_userfault_range(struct userfaultfd_ctx *ctx,\n+\t\tunsigned long start, unsigned long end)\n+{\n+\tstruct userfaultfd_wake_range range = {\n+\t\t.start = start,\n+\t\t.len = end - start,\n+\t};\n+\n+\twake_userfault(ctx, \u0026range);\n+}\n+\n static __always_inline int validate_unaligned_range(\n \tstruct mm_struct *mm, __u64 start, __u64 len)\n {\n@@ -3977,6 +3970,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,\n \n \tret = 0;\n \tfor_each_vma_range(vmi, vma, end) {\n+\t\tstruct userfaultfd_wake_range range;\n+\n \t\tcond_resched();\n \n \t\t/* VMA not registered with userfaultfd. */\n@@ -3991,18 +3986,17 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,\n \t\t\tstart = vma-\u003evm_start;\n \t\tvma_end = min(end, vma-\u003evm_end);\n \n-\t\tif (userfaultfd_missing(vma)) {\n-\t\t\t/*\n-\t\t\t * Wake any concurrent pending userfault while\n-\t\t\t * we unregister, so they will not hang\n-\t\t\t * permanently and it avoids userland to call\n-\t\t\t * UFFDIO_WAKE explicitly.\n-\t\t\t */\n-\t\t\tstruct userfaultfd_wake_range range;\n-\t\t\trange.start = start;\n-\t\t\trange.len = vma_end - start;\n-\t\t\twake_userfault(vma-\u003evm_userfaultfd_ctx.ctx, \u0026range);\n-\t\t}\n+\t\t/* Prevent new userfaults from being queued after the wakeup. */\n+\t\tvma_start_write(vma);\n+\n+\t\t/*\n+\t\t * Wake any concurrent pending userfault while we unregister,\n+\t\t * so they will not hang permanently and it avoids userland to\n+\t\t * call UFFDIO_WAKE explicitly.\n+\t\t */\n+\t\trange.start = start;\n+\t\trange.len = vma_end - start;\n+\t\twake_userfault(ctx, \u0026range);\n \n \t\tvma = userfaultfd_clear_vma(\u0026vmi, prev, vma,\n \t\t\t\t\t start, vma_end);\ndiff --git a/mm/util.c b/mm/util.c\nindex 7f374bb8bf3635..67f8fca3b2056b 100644\n--- a/mm/util.c\n+++ b/mm/util.c\n@@ -697,6 +697,19 @@ void *vcalloc_noprof(size_t n, size_t size)\n }\n EXPORT_SYMBOL(vcalloc_noprof);\n \n+/**\n+ * folio_anon_vma - Get a folio's raw anon_vma pointer.\n+ * @folio: The folio.\n+ *\n+ * The returned pointer may be stale for an unmapped folio. The caller\n+ * must ensure the anon_vma stays alive before dereferencing it. A folio\n+ * reference or lock alone is insufficient; use folio_get_anon_vma() when\n+ * the anon_vma's lifetime is not otherwise protected. Note that\n+ * folio_get_anon_vma() is only useful for folios that are still mapped.\n+ *\n+ * Return: The raw anon_vma pointer for an anonymous, non-KSM folio, or\n+ * %NULL otherwise.\n+ */\n struct anon_vma *folio_anon_vma(const struct folio *folio)\n {\n \tunsigned long mapping = (unsigned long)folio-\u003emapping;\ndiff --git a/mm/vma.c b/mm/vma.c\nindex 1be613044b4a50..695d0be85ac864 100644\n--- a/mm/vma.c\n+++ b/mm/vma.c\n@@ -516,6 +516,15 @@ void unmap_region(struct unmap_desc *unmap)\n \ttlb_finish_mmu(\u0026tlb);\n }\n \n+static int check_split(struct vm_area_struct *vma, unsigned long addr)\n+{\n+\tif (!vma-\u003evm_ops)\n+\t\treturn 0;\n+\tif (!vma-\u003evm_ops-\u003emay_split)\n+\t\treturn 0;\n+\treturn vma-\u003evm_ops-\u003emay_split(vma, addr);\n+}\n+\n /*\n * __split_vma() bypasses sysctl_max_map_count checking. We use this where it\n * has already been checked or doesn't make sense to fail.\n@@ -532,11 +541,9 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,\n \tWARN_ON(vma-\u003evm_start \u003e= addr);\n \tWARN_ON(vma-\u003evm_end \u003c= addr);\n \n-\tif (vma-\u003evm_ops \u0026\u0026 vma-\u003evm_ops-\u003emay_split) {\n-\t\terr = vma-\u003evm_ops-\u003emay_split(vma, addr);\n-\t\tif (err)\n-\t\t\treturn err;\n-\t}\n+\terr = check_split(vma, addr);\n+\tif (err)\n+\t\treturn err;\n \n \tnew = vm_area_dup(vma);\n \tif (!new)\n@@ -948,6 +955,10 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(\n \tif (vmg-\u003e__remove_middle \u0026\u0026 !can_merge_remove_vma(middle))\n \t\treturn NULL;\n \n+\t/* If middle isn't removed then it's resized, i.e. splits. */\n+\tif (!vmg-\u003e__remove_middle \u0026\u0026 check_split(middle, left_side ? end : start))\n+\t\treturn NULL;\n+\n \t/*\n \t * If we merge both VMAs, then next is also deleted. This implies\n \t * merge_will_delete_vma also.\n@@ -3380,7 +3391,8 @@ static int acct_stack_growth(struct vm_area_struct *vma,\n \t * Overcommit.. This must be the final test, as it will\n \t * update security statistics.\n \t */\n-\tif (security_vm_enough_memory_mm(mm, grow))\n+\tif (vma_test(vma, VMA_ACCOUNT_BIT) \u0026\u0026\n+\t security_vm_enough_memory_mm(mm, grow))\n \t\treturn -ENOMEM;\n \n \treturn 0;\ndiff --git a/mm/vmscan.c b/mm/vmscan.c\nindex 9959ca158ce835..6a1af9b92349fb 100644\n--- a/mm/vmscan.c\n+++ b/mm/vmscan.c\n@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n \n \t\twalk-\u003emm_stats[MM_NONLEAF_FOUND]++;\n \n-\t\tif (!walk_pte_range(\u0026val, addr, next, args))\n+\t\tif (!walk_pte_range(pmd + i, addr, next, args))\n \t\t\tcontinue;\n \n \t\twalk-\u003emm_stats[MM_NONLEAF_ADDED]++;\n@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,\n \t\tif (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))\n \t\t\tcontinue;\n \n-\t\twalk_pmd_range(\u0026val, addr, next, args);\n+\t\twalk_pmd_range(pud + i, addr, next, args);\n \n \t\tif (need_resched() || walk-\u003ebatched \u003e= MAX_LRU_BATCH) {\n \t\t\tend = (addr | ~PUD_MASK) + 1;\n"
} |
| 2/1 | 2026/10/27 03:18 | agent | patch-evaluator |
4mModel:core Args: null Results: {
"EnableConfigs": [
"TRANSPARENT_HUGEPAGE",
"KSM",
"USERFAULTFD",
"HUGETLBFS",
"MEMFD_CREATE",
"PROC_PAGE_MONITOR",
"CMA",
"ZONE_DEVICE",
"NUMA"
],
"FocusSymbols": [
"move_page_tables",
"move_huge_pmd",
"copy_huge_pud",
"try_collapse_pte_mapped_thp",
"ksm_might_need_to_copy",
"do_pagemap_scan",
"memfd_pin_folios",
"userfaultfd_register",
"userfaultfd_unregister",
"deferred_split_scan",
"cma_release"
],
"Reasoning": "The patch series modifies core memory management (mm) subsystems, including page table manipulation (mremap, khugepaged, huge_memory), KSM copy-on-write and anon_vma checks, userfaultfd register/unregister wakeup flows, memfd pinning, and pagemap scan argument validation. It introduces restructured page table locking (double_pt_lock), new VM_WARN_ON_ONCE_FOLIO assertions in KSM, rmap accounting in copy_huge_pud, and list synchronization in deferred_split_scan. These are fully reachable via user-space syscalls (mremap, madvise, userfaultfd, ioctl, memfd_create) and warrant fuzzing to identify potential deadlocks, race conditions, or assertion violations.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
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.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 0ac6921d5becf362e53ffa845fb62eeee20cee70
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Oct 27 03:18:52 2026 +0000
syz-cluster: applied patch under review
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 7452b695fdaec4..762b6dbb81e4ac 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3032,6 +3032,12 @@ static int pagemap_scan_get_args(struct pm_scan_arg *arg,
arg->category_anyof_mask | arg->return_mask) & ~PM_SCAN_CATEGORIES)
return -EINVAL;
+ /* Don't cut 64-bit numbers down to addresses that happen to work. */
+ if (arg->start != (unsigned long)arg->start ||
+ arg->end != (unsigned long)arg->end ||
+ arg->vec != (unsigned long)arg->vec)
+ return -EFAULT;
+
arg->start = untagged_addr((unsigned long)arg->start);
arg->end = untagged_addr((unsigned long)arg->end);
arg->vec = untagged_addr((unsigned long)arg->vec);
diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h
index fbe1114e9a443d..3f4a180b48f3c0 100644
--- a/include/asm-generic/tlb.h
+++ b/include/asm-generic/tlb.h
@@ -350,8 +350,7 @@ struct mmu_gather {
void tlb_flush_mmu(struct mmu_gather *tlb);
static inline void __tlb_adjust_range(struct mmu_gather *tlb,
- unsigned long address,
- unsigned int range_size)
+ unsigned long address, unsigned long range_size)
{
tlb->start = min(tlb->start, address);
tlb->end = max(tlb->end, address + range_size);
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index c745f7ad22987f..fac05120ab7744 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -12,9 +12,8 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
bool huge_pmd_set_accessed(struct vm_fault *vmf);
-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
- pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
- struct vm_area_struct *vma);
+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
+ pud_t *dst_pud, pud_t *src_pud, unsigned long addr);
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud);
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index d39d0d5483a23a..a0d358a1feb632 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -91,8 +91,8 @@ static inline void ksm_exit(struct mm_struct *mm)
* We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
* but what if the vma was unmerged while the page was swapped out?
*/
-struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr);
+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,
+ struct vm_area_struct *vma, unsigned long addr);
void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
@@ -145,7 +145,7 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
}
static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr)
+ struct page *page, struct vm_area_struct *vma, unsigned long addr)
{
return folio;
}
diff --git a/include/linux/memremap.h b/include/linux/memremap.h
index e3c2ccf872a8af..b5e5e626c7a596 100644
--- a/include/linux/memremap.h
+++ b/include/linux/memremap.h
@@ -231,6 +231,7 @@ void memunmap_pages(struct dev_pagemap *pgmap);
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
struct dev_pagemap *get_dev_pagemap(unsigned long pfn);
+bool pfn_has_dev_pagemap(unsigned long pfn);
bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);
unsigned long memremap_compat_align(void);
@@ -284,6 +285,11 @@ static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
return NULL;
}
+static inline bool pfn_has_dev_pagemap(unsigned long pfn)
+{
+ return false;
+}
+
static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)
{
return false;
diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h
index a11a44eef521e9..5db78e5761c5c3 100644
--- a/include/linux/mmu_notifier.h
+++ b/include/linux/mmu_notifier.h
@@ -483,7 +483,8 @@ mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
/*
* This version of mmu_notifier_invalidate_range_start() avoids blocking, but it
* can return an error if a notifier can't proceed without blocking, in which
- * case you're not allowed to modify PTEs in the specified range.
+ * case you're not allowed to modify PTEs in the specified range or call
+ * mmu_notifier_invalidate_range_end().
*
* This is mainly intended for OOM handling.
*/
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index a7c7a96a7cbf90..2ceca7993e4f52 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -117,8 +117,14 @@ extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
unsigned long len, unsigned long pgoff, unsigned long flags);
extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
#ifdef CONFIG_SHMEM
+bool file_inode_is_shmem(const struct inode *inode);
bool shmem_mapping(const struct address_space *mapping);
#else
+static inline bool file_inode_is_shmem(const struct inode *inode)
+{
+ return false;
+}
+
static inline bool shmem_mapping(const struct address_space *mapping)
{
return false;
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index a14b8a9ffb7b1c..565e98f342dc83 100644
--- a/include/linux/userfaultfd_k.h
+++ b/include/linux/userfaultfd_k.h
@@ -153,8 +153,6 @@ extern int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
unsigned long len, bool enable_rwp);
/* move_pages */
-void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2);
-void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2);
int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,
struct vm_area_struct *dst_vma,
struct vm_area_struct *src_vma,
diff --git a/kernel/fork.c b/kernel/fork.c
index 22eaf5fb844d0a..c8948cadccf71d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1532,12 +1532,19 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
if (!mm)
goto fail_nomem;
+ uprobe_start_dup_mmap();
+ if (mmap_write_lock_killable(oldmm)) {
+ free_mm(mm);
+ goto fail_uprobe;
+ }
+
memcpy(mm, oldmm, sizeof(*mm));
- if (!mm_init(mm, tsk))
- goto fail_nomem;
+ if (!mm_init(mm, tsk)) {
+ mmap_write_unlock(oldmm);
+ goto fail_uprobe;
+ }
- uprobe_start_dup_mmap();
err = dup_mmap(mm, oldmm);
if (err)
goto free_pt;
@@ -1556,9 +1563,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
mm->binfmt = NULL;
mm_init_owner(mm, NULL);
mmput(mm);
- if (err)
- uprobe_end_dup_mmap();
-
+ if (!err)
+ return NULL;
+fail_uprobe:
+ uprobe_end_dup_mmap();
fail_nomem:
return NULL;
}
diff --git a/mm/cma.c b/mm/cma.c
index a10ea37a261da3..871f95c9ae9f0e 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
unsigned long count)
{
struct cma_memrange *cmr;
- unsigned long ret = 0;
+ unsigned long leaked = 0;
unsigned long i, pfn;
cmr = find_cma_memrange(cma, pages, count);
@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
pfn = page_to_pfn(pages);
for (i = 0; i < count; i++, pfn++)
- ret += !put_page_testzero(pfn_to_page(pfn));
+ leaked += !page_ref_freeze(pfn_to_page(pfn), 1);
- WARN(ret, "%lu pages are still in use!\n", ret);
+ if (leaked) {
+ pr_warn_ratelimited("%lu pages are still in use, not freeing CMA region!\n",
+ leaked);
+ return true;
+ }
__cma_release_frozen(cma, cmr, pages, count);
diff --git a/mm/filemap.c b/mm/filemap.c
index b74bc1e5015c6f..e85a4114d2ecc7 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -227,18 +227,6 @@ void __filemap_remove_folio(struct folio *folio, void *shadow)
page_cache_delete(mapping, folio, shadow);
}
-static void filemap_free_folio(const struct address_space *mapping,
- struct folio *folio)
-{
- void (*free_folio)(struct folio *);
-
- free_folio = mapping->a_ops->free_folio;
- if (free_folio)
- free_folio(folio);
-
- folio_put_refs(folio, folio_nr_pages(folio));
-}
-
/**
* filemap_remove_folio - Remove folio from page cache.
* @folio: The folio.
@@ -250,6 +238,7 @@ static void filemap_free_folio(const struct address_space *mapping,
void filemap_remove_folio(struct folio *folio)
{
struct address_space *mapping = folio->mapping;
+ void (*free_folio)(struct folio *folio);
BUG_ON(!folio_test_locked(folio));
spin_lock(&mapping->host->i_lock);
@@ -258,9 +247,12 @@ void filemap_remove_folio(struct folio *folio)
xa_unlock_irq(&mapping->i_pages);
if (mapping_shrinkable(mapping))
inode_lru_list_add(mapping->host);
+ free_folio = mapping->a_ops->free_folio;
spin_unlock(&mapping->host->i_lock);
- filemap_free_folio(mapping, folio);
+ if (free_folio)
+ free_folio(folio);
+ folio_put_refs(folio, folio_nr_pages(folio));
}
/*
@@ -320,6 +312,7 @@ static void page_cache_delete_batch(struct address_space *mapping,
void delete_from_page_cache_batch(struct address_space *mapping,
struct folio_batch *fbatch)
{
+ void (*free_folio)(struct folio *folio);
int i;
if (!folio_batch_count(fbatch))
@@ -337,10 +330,16 @@ void delete_from_page_cache_batch(struct address_space *mapping,
xa_unlock_irq(&mapping->i_pages);
if (mapping_shrinkable(mapping))
inode_lru_list_add(mapping->host);
+ free_folio = mapping->a_ops->free_folio;
spin_unlock(&mapping->host->i_lock);
- for (i = 0; i < folio_batch_count(fbatch); i++)
- filemap_free_folio(mapping, fbatch->folios[i]);
+ for (i = 0; i < folio_batch_count(fbatch); i++) {
+ struct folio *folio = fbatch->folios[i];
+
+ if (free_folio)
+ free_folio(folio);
+ folio_put_refs(folio, folio_nr_pages(folio));
+ }
}
int filemap_check_errors(struct address_space *mapping)
@@ -1374,18 +1373,30 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
* return value based on that state without races.
*
* Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
- * waiter, but an exclusive one requires WQ_FLAG_DONE.
+ * waiter, but an exclusive one must also acquire the bit.
*/
- if (behavior == EXCLUSIVE)
- return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
+ if (behavior == EXCLUSIVE) {
+ if (wait->flags & WQ_FLAG_DONE)
+ return 0;
+ /*
+ * We might have just been woken up and the waker will not
+ * wake up anybody else. If we ignore that now, the remaining
+ * waiters may not get woken up: take the bit now if it is
+ * still free.
+ */
+ if ((wait->flags & WQ_FLAG_WOKEN) &&
+ !test_and_set_bit(bit_nr, folio_flags(folio, 0)))
+ return 0;
+ return -EINTR;
+ }
return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
}
#ifdef CONFIG_MIGRATION
/**
- * softleaf_entry_wait_on_locked - Wait for a migration entry or
- * device_private entry to be removed.
+ * softleaf_entry_wait_on_locked - Wait for a migration entry to be removed
+ * or a device-private folio in a device-private entry to get unlocked.
* @entry: migration or device_private swap entry.
* @ptl: already locked ptl. This function will drop the lock.
*
@@ -3297,11 +3308,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
* @folio - the folio to lock.
* @fpin - the pointer to the file we may pin (or is already pinned).
*
- * This works similar to lock_folio_or_retry in that it can drop the
- * mmap_lock. It differs in that it actually returns the folio locked
- * if it returns 1 and 0 if it couldn't lock the folio. If we did have
- * to drop the mmap_lock then fpin will point to the pinned file and
- * needs to be fput()'ed at a later point.
+ * Returns 1 with the folio locked, or 0 if locking failed.
+ *
+ * On both entry and return, a non-NULL *fpin means the fault lock has
+ * already been dropped and the caller must eventually fput() the file.
+ * Otherwise the fault lock is still held, including on failure.
*/
static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
struct file **fpin)
@@ -3318,21 +3329,9 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
return 0;
*fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
- if (vmf->flags & FAULT_FLAG_KILLABLE) {
- if (__folio_lock_killable(folio)) {
- /*
- * We didn't have the right flags to drop the
- * fault lock, but all fault_handlers only check
- * for fatal signals if we return VM_FAULT_RETRY,
- * so we need to drop the fault lock here and
- * return 0 if we don't have a fpin.
- */
- if (*fpin == NULL)
- release_fault_lock(vmf);
- return 0;
- }
- } else
- __folio_lock(folio);
+ if (vmf->flags & FAULT_FLAG_KILLABLE)
+ return !__folio_lock_killable(folio);
+ __folio_lock(folio);
return 1;
}
@@ -3561,7 +3560,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
* vma->vm_mm->mmap_lock must be held on entry.
*
* If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
- * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
+ * may be dropped before doing I/O or when aborting an interrupted wait.
*
* If our return value does not have VM_FAULT_RETRY set, the mmap_lock
* has not been released.
@@ -3717,9 +3716,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
out_retry:
/*
- * We dropped the mmap_lock, we need to return to the fault handler to
- * re-find the vma and come back and find our hopefully still populated
- * page.
+ * With fpin set, the fault lock was already dropped. Otherwise it is
+ * still held. Release invalidate_lock before dropping either protection.
*/
if (!IS_ERR(folio))
folio_put(folio);
@@ -3727,6 +3725,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
filemap_invalidate_unlock_shared(mapping);
if (fpin)
fput(fpin);
+ else if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
+ release_fault_lock(vmf);
return ret | VM_FAULT_RETRY;
}
EXPORT_SYMBOL(filemap_fault);
diff --git a/mm/gup.c b/mm/gup.c
index f166acf794e308..48e1bfe5ab9a96 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -658,9 +658,6 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,
assert_spin_locked(pud_lockptr(mm, pudp));
- if (!pud_present(pud))
- return NULL;
-
if ((flags & FOLL_WRITE) &&
!can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags))
return NULL;
@@ -896,15 +893,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
}
static struct page *follow_pmd_mask(struct vm_area_struct *vma,
- unsigned long address, pud_t *pudp,
- unsigned int flags,
- unsigned long *page_mask)
+ unsigned long address, pud_t *pudp, pud_t pud,
+ unsigned int flags, unsigned long *page_mask)
{
pmd_t *pmd, pmdval;
spinlock_t *ptl;
struct page *page;
struct mm_struct *mm = vma->vm_mm;
+ if (unlikely(pud_bad(pud)))
+ return no_page_table(vma, flags, address);
+
pmd = pmd_offset(pudp, address);
pmdval = pmdp_get_lockless(pmd);
if (pmd_none(pmdval))
@@ -953,18 +952,23 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma,
pud = pudp_get(pudp);
if (!pud_present(pud))
return no_page_table(vma, flags, address);
- if (pud_leaf(pud)) {
- ptl = pud_lock(mm, pudp);
- page = follow_huge_pud(vma, address, pudp, flags, page_mask);
+ if (!pud_leaf(pud))
+ return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);
+
+ ptl = pud_lock(mm, pudp);
+ pud = *pudp;
+ if (unlikely(!pud_present(pud))) {
spin_unlock(ptl);
- if (page)
- return page;
return no_page_table(vma, flags, address);
}
- if (unlikely(pud_bad(pud)))
- return no_page_table(vma, flags, address);
+ if (unlikely(!pud_leaf(pud))) {
+ spin_unlock(ptl);
+ return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);
+ }
- return follow_pmd_mask(vma, address, pudp, flags, page_mask);
+ page = follow_huge_pud(vma, address, pudp, flags, page_mask);
+ spin_unlock(ptl);
+ return page;
}
static struct page *follow_p4d_mask(struct vm_area_struct *vma,
@@ -3200,6 +3204,9 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,
FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT)))
return -EINVAL;
+ if (unlikely(!nr_pages))
+ return 0;
+
if (gup_flags & FOLL_PIN)
mm_set_has_pinned_flag(current->mm);
@@ -3207,8 +3214,8 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,
might_lock_read(¤t->mm->mmap_lock);
start = untagged_addr(start) & PAGE_MASK;
- len = nr_pages << PAGE_SHIFT;
- if (check_add_overflow(start, len, &end))
+ if (check_shl_overflow(nr_pages, PAGE_SHIFT, &len) ||
+ check_add_overflow(start, len, &end))
return -EOVERFLOW;
if (end > TASK_SIZE_MAX)
return -EFAULT;
@@ -3512,6 +3519,20 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
goto err;
}
+ /*
+ * fallocate() can leave folios in the
+ * page cache that were never cleared.
+ */
+ if (!folio_test_uptodate(folio)) {
+ folio_lock(folio);
+ if (!folio_test_uptodate(folio)) {
+ folio_zero_range(folio, 0, folio_size(folio));
+ flush_dcache_folio(folio);
+ folio_mark_uptodate(folio);
+ }
+ folio_unlock(folio);
+ }
+
if (nr_folios == 0)
*offset = offset_in_folio(folio, start);
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index bd44d58e86845c..d090642ac8c2b6 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1500,7 +1500,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)
folio_unlock(folio);
folio_put(folio);
} else {
- spin_unlock(ptl);
+ softleaf_entry_wait_on_locked(entry, ptl);
}
return ret;
@@ -2072,11 +2072,13 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr,
update_mmu_cache_pud(vma, addr, pud);
}
-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
- pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
- struct vm_area_struct *vma)
+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
+ pud_t *dst_pud, pud_t *src_pud, unsigned long addr)
{
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
+ struct mm_struct *src_mm = src_vma->vm_mm;
spinlock_t *dst_ptl, *src_ptl;
+ struct page *page;
pud_t pud;
int ret;
@@ -2089,11 +2091,20 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
if (unlikely(!pud_trans_huge(pud)))
goto out_unlock;
- /*
- * TODO: once we support anonymous pages, use
- * folio_try_dup_anon_rmap_*() and split if duplicating fails.
- */
- if (vma_is_cow_mapping(vma) && pud_write(pud)) {
+ page = vm_normal_page_pud(src_vma, addr, pud);
+ if (page) {
+ struct folio *folio = page_folio(page);
+
+ folio_get(folio);
+ /*
+ * TODO: once we support anonymous pages, use
+ * folio_try_dup_anon_rmap_*() and split if duplicating fails.
+ */
+ folio_add_file_rmap_pud(folio, page, dst_vma);
+ add_mm_counter(dst_mm, mm_counter_file(folio), HPAGE_PUD_NR);
+ }
+
+ if (vma_is_cow_mapping(src_vma) && pud_write(pud)) {
pudp_set_wrprotect(src_mm, addr, src_pud);
pud = pud_wrprotect(pud);
}
@@ -2482,7 +2493,15 @@ static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
pmd_t pmdval, struct folio *folio, bool is_present)
{
- const bool is_device_private = folio_is_device_private(folio);
+ const bool is_device_private = !is_present && pmd_is_device_private_entry(pmdval);
+
+ /* Transfer young+dirty while the folio is still mapped. */
+ if (is_present && !folio_test_anon(folio)) {
+ if (pmd_dirty(pmdval))
+ folio_mark_dirty(folio);
+ if (pmd_young(pmdval) && likely(vma_has_recency(vma)))
+ folio_mark_accessed(folio);
+ }
/* Present and device private folios are rmappable. */
if (is_present || is_device_private)
@@ -2493,12 +2512,6 @@ static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
} else {
add_mm_counter(mm, mm_counter_file(folio),
-HPAGE_PMD_NR);
-
- if (is_present && pmd_dirty(pmdval))
- folio_mark_dirty(folio);
- if (is_present && pmd_young(pmdval) &&
- likely(vma_has_recency(vma)))
- folio_mark_accessed(folio);
}
/* Device private folios are pinned. */
@@ -2632,15 +2645,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
return false;
}
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
- if (old_ptl) {
- new_ptl = pmd_lockptr(mm, new_pmd);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+ old_ptl = pmd_lockptr(mm, old_pmd);
+ new_ptl = pmd_lockptr(mm, new_pmd);
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (likely(pmd_is_huge(*old_pmd))) {
pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
if (pmd_present(pmd))
force_flush = true;
@@ -2669,11 +2678,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
set_pmd_at(mm, new_addr, new_pmd, pmd);
if (force_flush)
flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
+ double_pt_unlock(old_ptl, new_ptl);
return false;
}
@@ -3495,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
__split_huge_pmd(vma, pmd, address, freeze);
}
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+ unsigned long address)
{
+ if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+ return false;
+ if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+ return false;
/*
- * If the new address isn't hpage aligned and it could previously
- * contain an hugepage: check if we need to split an huge pmd.
+ * Only selected VMAs support mapping PUDs for now. Lift the
+ * restriction once this changes.
*/
- if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
- range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
- ALIGN(address, HPAGE_PMD_SIZE)))
- split_huge_pmd_address(vma, address, false);
+ if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+ return false;
+ return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+ ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+ unsigned long address)
+{
+ if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+ return false;
+
+ return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+ ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+ unsigned long address)
+{
+ const bool split_pud = pud_split_required(vma, address);
+ const bool split_pmd = pmd_split_required(vma, address);
+ pgd_t *pgd;
+ p4d_t *p4d;
+ pud_t *pud;
+ pmd_t *pmd;
+
+ /*
+ * mmap, VMA and rmap write locks protect the walk; the split helpers
+ * take the page table locks.
+ */
+ if (!split_pud && !split_pmd)
+ return;
+
+ pgd = pgd_offset(vma->vm_mm, address);
+ if (!pgd_present(*pgd))
+ return;
+
+ p4d = p4d_offset(pgd, address);
+ if (!p4d_present(*p4d))
+ return;
+
+ pud = pud_offset(p4d, address);
+ if (split_pud && pud_leaf(*pud))
+ __split_huge_pud(vma, pud, address);
+ if (!split_pmd || !pud_present(*pud))
+ return;
+
+ pmd = pmd_offset(pud, address);
+ __split_huge_pmd(vma, pmd, address, false);
}
void vma_adjust_trans_huge(struct vm_area_struct *vma,
@@ -3513,14 +3571,14 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,
struct vm_area_struct *next)
{
/* Check if we need to split start first. */
- split_huge_pmd_if_needed(vma, start);
+ split_huge_leaf(vma, start);
/* Check if we need to split end next. */
- split_huge_pmd_if_needed(vma, end);
+ split_huge_leaf(vma, end);
/* If we're incrementing next->vm_start, we might need to split it. */
if (next)
- split_huge_pmd_if_needed(next, end);
+ split_huge_leaf(next, end);
}
/*
@@ -4522,8 +4580,7 @@ int split_folio_to_list(struct folio *folio, struct list_head *list)
*
* It is unsafe to call folio_unqueue_deferred_split() until folio refcount is
* zero: because even when the list_lru lock is held, a non-empty
- * _deferred_list might be in use on deferred_split_scan()'s unlocked
- * on-stack list.
+ * _deferred_list might be in use on deferred_split_scan()'s on-stack list.
*
* The list_lru sublist is determined by folio's memcg: it is therefore
* important to unqueue deferred split before changing folio memcg.
@@ -4665,10 +4722,22 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
deferred_split_isolate, &dispose);
list_for_each_entry_safe(folio, next, &dispose, _deferred_list) {
+ struct mem_cgroup *memcg;
+ struct list_lru_one *lru;
bool did_split = false;
bool underused = false;
+ /*
+ * Sync with deferred_split_folio(), which can requeue the
+ * folio once list_empty() is true, and sets PG_partially_mapped.
+ */
+ rcu_read_lock();
+ memcg = folio_memcg(folio);
+ lru = list_lru_lock_irq(&deferred_split_lru, folio_nid(folio),
+ &memcg);
list_del_init(&folio->_deferred_list);
+ list_lru_unlock_irq(lru);
+ rcu_read_unlock();
if (!folio_test_partially_mapped(folio)) {
/*
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index da5d04edf7c108..84141527fe73d3 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5153,15 +5153,9 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
spinlock_t *src_ptl, *dst_ptl;
pte_t pte;
- dst_ptl = huge_pte_lock(h, mm, dst_pte);
+ dst_ptl = huge_pte_lockptr(h, mm, dst_pte);
src_ptl = huge_pte_lockptr(h, mm, src_pte);
-
- /*
- * We don't have to worry about the ordering of src and dst ptlocks
- * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
- */
- if (src_ptl != dst_ptl)
- spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
+ double_pt_lock(dst_ptl, src_ptl);
pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);
@@ -5190,9 +5184,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
}
- if (src_ptl != dst_ptl)
- spin_unlock(src_ptl);
- spin_unlock(dst_ptl);
+ double_pt_unlock(dst_ptl, src_ptl);
}
int move_hugetlb_page_tables(struct vm_area_struct *vma,
diff --git a/mm/internal.h b/mm/internal.h
index b1b0f29d2216ee..7e4d88b2fb02c8 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -619,6 +619,34 @@ static inline void force_page_cache_readahead(struct address_space *mapping,
force_page_cache_ra(&ractl, nr_to_read);
}
+/*
+ * Take two page table locks at the same level in address order such that
+ * we cannot deadlock.
+ */
+static inline void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2)
+ __acquires(ptl1)
+ __acquires(ptl2)
+{
+ if (ptl1 > ptl2)
+ swap(ptl1, ptl2);
+ spin_lock(ptl1);
+ if (ptl1 != ptl2)
+ spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
+ else
+ __acquire(ptl2);
+}
+
+static inline void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2)
+ __releases(ptl1)
+ __releases(ptl2)
+{
+ spin_unlock(ptl1);
+ if (ptl1 != ptl2)
+ spin_unlock(ptl2);
+ else
+ __release(ptl2);
+}
+
unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 4b71d010b2aff5..4313abf9cfcb97 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -10,6 +10,7 @@
#include <linux/mm_inline.h>
#include <linux/kthread.h>
#include <linux/khugepaged.h>
+#include <linux/hugetlb.h>
#include <linux/freezer.h>
#include <linux/mman.h>
#include <linux/hashtable.h>
@@ -1832,6 +1833,17 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
return SCAN_SUCCEED;
}
+static bool pte_table_empty(pte_t *pte)
+{
+ int i;
+
+ for (i = 0; i < HPAGE_PMD_NR; i++, pte++) {
+ if (!pte_none(ptep_get(pte)))
+ return false;
+ }
+ return true;
+}
+
static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
bool install_pmd)
{
@@ -1904,7 +1916,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
result = SCAN_FAIL;
start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
- if (!start_pte) /* mmap_lock + page lock should prevent this */
+ if (!start_pte)
goto drop_folio;
/* step 1: check all mapped PTEs are to the right huge page */
@@ -1940,26 +1952,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
mmu_notifier_invalidate_range_start(&range);
notified = true;
- /*
- * pmd_lock covers a wider range than ptl, and (if split from mm's
- * page_table_lock) ptl nests inside pml. The less time we hold pml,
- * the better; but userfaultfd's mfill_atomic_pte() on a private VMA
- * inserts a valid as-if-COWed PTE without even looking up page cache.
- * So page lock of folio does not protect from it, so we must not drop
- * ptl before pgt_pmd is removed, so uffd private needs pml taken now.
- */
- if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
- pml = pmd_lock(mm, pmd);
-
- start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
- if (!start_pte) /* mmap_lock + page lock should prevent this */
- goto abort;
- if (!pml)
- spin_lock(ptl);
- else if (ptl != pml)
- spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
-
- if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+ start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
+ if (!start_pte)
goto abort;
/* step 2: clear page table and adjust rmap */
@@ -1991,36 +1985,41 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
nr_batch_ptes = folio_pte_batch(folio, pte, ptent, max_nr_batch_ptes);
- /*
- * Must clear entry, or a racing truncate may re-remove it.
- * TLB flush can be left until pmdp_collapse_flush() does it.
- * PTE dirty? Shmem page is already dirty; file is read-only.
- */
- clear_ptes(mm, addr, pte, nr_batch_ptes);
+ /* Must clear entry, or a racing truncate may re-remove it. */
+ ptent = get_and_clear_ptes(mm, addr, pte, nr_batch_ptes);
+ if (pte_dirty(ptent))
+ folio_mark_dirty(folio);
folio_remove_rmap_ptes(folio, page, nr_batch_ptes, vma);
nr_mapped_ptes += nr_batch_ptes;
}
- if (!pml)
- spin_unlock(ptl);
-
/* step 3: set proper refcount and mm_counters. */
if (nr_mapped_ptes) {
folio_ref_sub(folio, nr_mapped_ptes);
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
}
- /* step 4: remove empty page table */
- if (!pml) {
+ /* step 4: acquire the PMD lock and remove the empty page table */
+ pml = pmd_lockptr(mm, pmd);
+ if (ptl != pml && !spin_trylock(pml)) {
+ /* We must flush before dropping the PTL. */
+ if (nr_mapped_ptes)
+ flush_tlb_range(vma, haddr, end);
+ pte_unmap_unlock(start_pte, ptl);
+ ptl = NULL;
+
pml = pmd_lock(mm, pmd);
- if (ptl != pml) {
+ start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
+ if (!start_pte)
+ goto unlock;
+ if (ptl != pml)
spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
- if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
- flush_tlb_mm(mm);
- goto unlock;
- }
- }
+
+ if (!pte_table_empty(start_pte))
+ goto unlock;
}
+
+ /* This also flushes the zapped folio ptes properly. */
pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
pmdp_get_lockless_sync();
pte_unmap_unlock(start_pte, ptl);
@@ -2041,7 +2040,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
goto drop_folio;
abort:
if (nr_mapped_ptes) {
- flush_tlb_mm(mm);
+ flush_tlb_range(vma, haddr, end);
folio_ref_sub(folio, nr_mapped_ptes);
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
}
diff --git a/mm/ksm.c b/mm/ksm.c
index dbfb8f98b18925..e95f6b67236023 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -475,6 +475,7 @@ static void advisor_stop_scan(void)
#ifdef CONFIG_NUMA
/* Zeroed when merging across nodes is not allowed */
static unsigned int ksm_merge_across_nodes = 1;
+/* Number of allocated stable and unstable tree roots. */
static int ksm_nr_node_ids = 1;
#else
#define ksm_merge_across_nodes 1U
@@ -3136,24 +3137,52 @@ void __ksm_exit(struct mm_struct *mm)
trace_ksm_exit(mm);
}
-struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr)
+static bool folio_anon_rmap_compatible(const struct folio *folio,
+ const struct page *page, struct vm_area_struct *vma,
+ unsigned long addr)
{
- struct page *page = folio_page(folio, 0);
- struct anon_vma *anon_vma = folio_anon_vma(folio);
- struct folio *new_folio;
+ const struct anon_vma *anon_vma = folio_anon_vma(folio);
+ struct anon_vma_chain *avc;
- if (folio_test_large(folio))
- return folio;
+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+ VM_WARN_ON_ONCE_FOLIO(page_folio(page) != folio, folio);
+ VM_WARN_ON_ONCE_FOLIO(folio_test_ksm(folio), folio);
+ vma_assert_stabilised(vma);
+
+ if (!anon_vma)
+ return true;
+ if (page_pgoff(folio, page) != linear_anon_page_index(vma, addr))
+ return false;
+
+ /*
+ * Careful: anon_vma might have been freed, so we cannot safely
+ * dereference it. Add a fast path before we walk the anon_vma chain.
+ */
+ if (anon_vma == vma->anon_vma || anon_vma == vma->anon_vma->root)
+ return true;
+
+ list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) {
+ if (avc->anon_vma == anon_vma)
+ return true;
+ }
+ return false;
+}
+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,
+ struct vm_area_struct *vma, unsigned long addr)
+{
+ struct folio *new_folio;
+
+ /*
+ * A KSM folio is small, but the slot that it left behind can be swapped
+ * in as a part of a large folio by one of the processes that shared it.
+ * So test, and copy, the page of the slot.
+ */
if (folio_test_ksm(folio)) {
if (folio_stable_node(folio) &&
!(ksm_run & KSM_RUN_UNMERGE))
return folio; /* no need to copy it */
- } else if (!anon_vma) {
- return folio; /* no need to copy it */
- } else if (folio->index == linear_anon_page_index(vma, addr) &&
- anon_vma->root == vma->anon_vma->root) {
+ } else if (folio_anon_rmap_compatible(folio, page, vma, addr)) {
return folio; /* still no need to copy it */
}
if (PageHWPoison(page))
@@ -3636,14 +3665,13 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj,
else {
root_stable_tree = buf;
root_unstable_tree = buf + nr_node_ids;
+ ksm_nr_node_ids = nr_node_ids;
/* Stable tree is empty but not the unstable */
root_unstable_tree[0] = one_unstable_tree[0];
}
}
- if (!err) {
+ if (!err)
ksm_merge_across_nodes = knob;
- ksm_nr_node_ids = knob ? 1 : nr_node_ids;
- }
}
mutex_unlock(&ksm_thread_mutex);
diff --git a/mm/madvise.c b/mm/madvise.c
index 75b409e97dbf24..a16e3cb256b7b0 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -756,6 +756,15 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
if (folio_test_swapcache(folio) || folio_test_dirty(folio)) {
if (!folio_trylock(folio))
continue;
+ /*
+ * KSM folios can gain new mappings at any time, so we must
+ * not clear their dirty flag.
+ */
+ if (folio_test_ksm(folio)) {
+ folio_unlock(folio);
+ continue;
+ }
+
/*
* If we have a large folio at this point, we know it is
* fully mapped so if its mapcount is the same as its
diff --git a/mm/memfd.c b/mm/memfd.c
index 0f6fff004f5e4c..6f5fbce74f21eb 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -211,12 +211,14 @@ static int memfd_wait_for_pins(struct address_space *mapping)
static unsigned int *memfd_file_seals_ptr(struct file *file)
{
- if (shmem_file(file))
- return &SHMEM_I(file_inode(file))->seals;
+ struct inode *inode = file_inode(file);
+
+ if (file_inode_is_shmem(inode))
+ return &SHMEM_I(inode)->seals;
#ifdef CONFIG_HUGETLBFS
if (is_file_hugepages(file))
- return &HUGETLBFS_I(file_inode(file))->seals;
+ return &HUGETLBFS_I(inode)->seals;
#endif
return NULL;
@@ -482,6 +484,8 @@ struct file *memfd_alloc_file(const char *name, unsigned int flags)
}
file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
+ /* a regular file like any other: POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
+ file->f_mode |= FMODE_ATOMIC_POS;
file->f_flags |= O_LARGEFILE;
if (flags & MFD_NOEXEC_SEAL) {
diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 59de210bee5f9e..c0811407ce3973 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -582,7 +582,7 @@ static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler,
{
struct inode *inode = file_inode(file);
- return shmem_file(file) && !inode->i_nlink;
+ return file_inode_is_shmem(inode) && !inode->i_nlink;
}
static unsigned long memfd_luo_get_id(struct file *file)
diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c
index 25e121851b5862..8eda313cf61925 100644
--- a/mm/memory-tiers.c
+++ b/mm/memory-tiers.c
@@ -572,21 +572,14 @@ static struct memory_tier *set_node_memory_tier(int node)
return memtier;
}
-static void destroy_memory_tier(struct memory_tier *memtier)
+static struct memory_tier *clear_node_memory_tier(int node)
{
- list_del(&memtier->list);
- device_unregister(&memtier->dev);
-}
-
-static bool clear_node_memory_tier(int node)
-{
- bool cleared = false;
pg_data_t *pgdat;
struct memory_tier *memtier;
pgdat = NODE_DATA(node);
if (!pgdat)
- return false;
+ return NULL;
/*
* Make sure that anybody looking at NODE_DATA who finds
@@ -604,14 +597,10 @@ static bool clear_node_memory_tier(int node)
synchronize_rcu();
memtype = node_memory_types[node].memtype;
node_clear(node, memtype->nodes);
- if (nodes_empty(memtype->nodes)) {
+ if (nodes_empty(memtype->nodes))
list_del_init(&memtype->tier_sibling);
- if (list_empty(&memtier->memory_types))
- destroy_memory_tier(memtier);
- }
- cleared = true;
}
- return cleared;
+ return memtier;
}
static void release_memtype(struct kref *kref)
@@ -884,13 +873,21 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
{
struct memory_tier *memtier;
struct node_notify *nn = _arg;
+ bool empty = false;
switch (action) {
case NODE_REMOVED_LAST_MEMORY:
mutex_lock(&memory_tier_lock);
- if (clear_node_memory_tier(nn->nid))
+ memtier = clear_node_memory_tier(nn->nid);
+ if (memtier) {
+ empty = list_empty(&memtier->memory_types);
+ if (empty)
+ list_del(&memtier->list);
establish_demotion_targets();
+ }
mutex_unlock(&memory_tier_lock);
+ if (empty)
+ device_unregister(&memtier->dev);
break;
case NODE_ADDED_FIRST_MEMORY:
mutex_lock(&memory_tier_lock);
diff --git a/mm/memory.c b/mm/memory.c
index 5d6d695675bce4..be4c05db37d33b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1463,7 +1463,6 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
unsigned long end)
{
struct mm_struct *dst_mm = dst_vma->vm_mm;
- struct mm_struct *src_mm = src_vma->vm_mm;
pud_t *src_pud, *dst_pud;
unsigned long next;
@@ -1477,8 +1476,7 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
int err;
VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
- err = copy_huge_pud(dst_mm, src_mm,
- dst_pud, src_pud, addr, src_vma);
+ err = copy_huge_pud(dst_vma, src_vma, dst_pud, src_pud, addr);
if (err == -ENOMEM)
return -ENOMEM;
if (!err)
@@ -3763,6 +3761,13 @@ static inline int __wp_page_copy_user(struct page *dst, struct page *src,
return 0;
}
+ /*
+ * __copy_from_user_inatomic() only works as expected for local faults.
+ * Reject remote faults.
+ */
+ if (unlikely(mm != current->mm))
+ return -EFAULT;
+
/*
* If the source page was a PFN mapping, we don't have
* a "struct page" for it. We do a best-effort copy by
@@ -4088,6 +4093,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
folio_put(old_folio);
delayacct_wpcopy_end();
+ if (err == -EFAULT)
+ return VM_FAULT_SIGBUS;
return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;
}
kmsan_copy_page_meta(&new_folio->page, vmf->page);
@@ -4642,8 +4649,8 @@ EXPORT_SYMBOL_GPL(unmap_mapping_pages);
void unmap_mapping_range(struct address_space *mapping,
loff_t const holebegin, loff_t const holelen, int even_cows)
{
- pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT;
- pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ pgoff_t hba = (u64)holebegin >> PAGE_SHIFT;
+ pgoff_t hlen = ((u64)holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
/* Check for overflow. */
if (sizeof(holelen) > sizeof(hlen)) {
@@ -5089,7 +5096,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
* folio->index of non-ksm folios would be nonlinear inside the
* anon VMA -- the ksm flag is lost on actual swapout.
*/
- folio = ksm_might_need_to_copy(folio, vma, vmf->address);
+ folio = ksm_might_need_to_copy(folio, page, vma, vmf->address);
if (unlikely(!folio)) {
ret = VM_FAULT_OOM;
folio = swapcache;
@@ -5271,7 +5278,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (unlikely(folio != swapcache)) {
folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE);
folio_add_lru_vma(folio, vma);
- folio_put_swap(swapcache, NULL);
+ folio_put_swap(swapcache,
+ folio_file_page(swapcache, swp_offset(entry)));
} else if (!folio_test_anon(folio)) {
/*
* We currently only expect !anon folios that are fully
@@ -6950,8 +6958,12 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
lru_gen_exit_fault();
- /* If the mapping is droppable, then errors due to OOM aren't fatal. */
- if (is_droppable)
+ /*
+ * If the mapping is droppable, then errors due to OOM aren't fatal: the
+ * task returns to user mode, handles its signals and tries again. A
+ * fault from kernel mode has no such return, so it must see the error.
+ */
+ if (is_droppable && (flags & FAULT_FLAG_USER))
ret &= ~VM_FAULT_OOM;
if (flags & FAULT_FLAG_USER) {
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 796af1028ee239..5a972d7a9e4787 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -342,7 +342,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages)
struct page *pfn_to_online_page(unsigned long pfn)
{
unsigned long nr = pfn_to_section_nr(pfn);
- struct dev_pagemap *pgmap;
struct mem_section *ms;
BUILD_BUG_ON(IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID));
@@ -363,14 +362,12 @@ struct page *pfn_to_online_page(unsigned long pfn)
/*
* Slowpath: when ZONE_DEVICE collides with
* ZONE_{NORMAL,MOVABLE} within the same section some pfns in
- * the section may be 'offline' but 'valid'. Only
- * get_dev_pagemap() can determine sub-section online status.
+ * the section may be 'offline' but 'valid'. Only the presence of a
+ * pgmap can determine sub-section online status: it indicates a
+ * ZONE_DEVICE offline pfn. That includes a pgmap whose reference has
+ * been killed, its memmap is about to go away.
*/
- pgmap = get_dev_pagemap(pfn);
- put_dev_pagemap(pgmap);
-
- /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */
- if (pgmap)
+ if (pfn_has_dev_pagemap(pfn))
return NULL;
return pfn_to_page(pfn);
diff --git a/mm/memremap.c b/mm/memremap.c
index accba23aef28cc..a1e99a4c55c561 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -413,6 +413,19 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
}
EXPORT_SYMBOL_GPL(get_dev_pagemap);
+/**
+ * pfn_has_dev_pagemap() - does @pfn belong to a dev_pagemap, dead or alive
+ * @pfn: page frame number to lookup page_map
+ *
+ * In contrast to get_dev_pagemap() this also finds a dev_pagemap which is on
+ * its way out: it is registered before its pages are added, and until they
+ * have been removed.
+ */
+bool pfn_has_dev_pagemap(unsigned long pfn)
+{
+ return xa_load(&pgmap_array, pfn);
+}
+
void free_zone_device_folio(struct folio *folio)
{
struct dev_pagemap *pgmap = folio->pgmap;
diff --git a/mm/mmap.c b/mm/mmap.c
index 148ba01c03739f..7618e72766a522 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1713,8 +1713,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
LIST_HEAD(uf);
VMA_ITERATOR(vmi, mm, 0);
- if (mmap_write_lock_killable(oldmm))
- return -EINTR;
+ mmap_assert_write_locked(oldmm);
flush_cache_dup_mm(oldmm);
uprobe_dup_mmap(oldmm, mm);
/*
@@ -1725,11 +1724,6 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
/* No ordering required: file already has been exposed. */
dup_mm_exe_file(mm, oldmm);
- mm->total_vm = oldmm->total_vm;
- mm->data_vm = oldmm->data_vm;
- mm->exec_vm = oldmm->exec_vm;
- mm->stack_vm = oldmm->stack_vm;
-
/* Use __mt_dup() to efficiently build an identical maple tree. */
retval = __mt_dup(&oldmm->mm_mt, &mm->mm_mt, GFP_KERNEL);
if (unlikely(retval))
diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c
index df69ba6e797f76..a7bae5de5fedc1 100644
--- a/mm/mmu_notifier.c
+++ b/mm/mmu_notifier.c
@@ -569,16 +569,25 @@ int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
{
struct mmu_notifier_subscriptions *subscriptions =
range->mm->notifier_subscriptions;
- int ret;
+ const bool has_itree = subscriptions->has_itree;
+ int ret = 0;
- if (subscriptions->has_itree) {
+ if (has_itree) {
ret = mn_itree_invalidate(subscriptions, range);
if (ret)
return ret;
}
if (!hlist_empty(&subscriptions->list))
- return mn_hlist_invalidate_range_start(subscriptions, range);
- return 0;
+ ret = mn_hlist_invalidate_range_start(subscriptions, range);
+
+ /*
+ * On error, the non-blocking caller is not allowed to call
+ * invalidate_range_end(), as in mn_itree_invalidate().
+ */
+ if (ret && has_itree && !mmu_notifier_range_blockable(range))
+ mn_itree_inv_end(subscriptions);
+
+ return ret;
}
static void
@@ -938,7 +947,6 @@ static int __mmu_interval_notifier_insert(
struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
unsigned long length, const struct mmu_interval_notifier_ops *ops)
{
- interval_sub->mm = mm;
interval_sub->ops = ops;
RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
interval_sub->interval_tree.start = start;
@@ -955,8 +963,12 @@ static int __mmu_interval_notifier_insert(
if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
return -EINVAL;
- /* pairs with mmdrop in mmu_interval_notifier_remove() */
+ /*
+ * Pairs with mmdrop in mmu_interval_notifier_remove(). Drivers take a
+ * non-NULL ->mm for "registered", so set it only with the reference.
+ */
mmgrab(mm);
+ interval_sub->mm = mm;
/*
* If some invalidate_range_start/end region is going on in parallel
diff --git a/mm/mremap.c b/mm/mremap.c
index 0d06d7f37f19a1..70b7264aa6bf5c 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -87,7 +87,7 @@ static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)
return NULL;
pud = pud_offset(p4d, addr);
- if (pud_none_or_clear_bad(pud))
+ if (!pud_trans_huge(*pud) && pud_none_or_clear_bad(pud))
return NULL;
return pud;
@@ -202,7 +202,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
struct mm_struct *mm = vma->vm_mm;
pte_t *old_ptep, *new_ptep;
pte_t old_pte, pte;
- pmd_t dummy_pmdval;
+ pmd_t old_pmdval, new_pmdval;
spinlock_t *old_ptl, *new_ptl;
bool force_flush = false;
unsigned long old_addr = pmc->old_addr;
@@ -234,31 +234,30 @@ static int move_ptes(struct pagetable_move_control *pmc,
if (pmc->need_rmap_locks)
take_rmap_locks(vma);
- /*
- * We don't have to worry about the ordering of src and dst
- * pte locks because exclusive mmap_lock prevents deadlock.
- */
- old_ptep = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
+ old_ptep = pte_offset_map_rw_nolock(mm, old_pmd, old_addr, &old_pmdval,
+ &old_ptl);
if (!old_ptep) {
err = -EAGAIN;
goto out;
}
- /*
- * Now new_pte is none, so collapse_scan_file() path can not find
- * this by traversing file->f_mapping, so there is no concurrency with
- * retract_page_tables(). In addition, we already hold the exclusive
- * mmap_lock, so this new_pte page is stable, so there is no need to get
- * pmdval and do pmd_same() check.
- */
- new_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &dummy_pmdval,
- &new_ptl);
+ new_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &new_pmdval,
+ &new_ptl);
if (!new_ptep) {
- pte_unmap_unlock(old_ptep, old_ptl);
+ pte_unmap(old_ptep);
+ err = -EAGAIN;
+ goto out;
+ }
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (unlikely(!pmd_same(old_pmdval, pmdp_get_lockless(old_pmd)) ||
+ !pmd_same(new_pmdval, pmdp_get_lockless(new_pmd)))) {
+ double_pt_unlock(old_ptl, new_ptl);
+ pte_unmap(new_ptep);
+ pte_unmap(old_ptep);
err = -EAGAIN;
goto out;
}
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+
flush_tlb_batched_pending(vma->vm_mm);
lazy_mmu_mode_enable();
@@ -317,10 +316,9 @@ static int move_ptes(struct pagetable_move_control *pmc,
lazy_mmu_mode_disable();
if (force_flush)
flush_tlb_range(vma, old_end - len, old_end);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
pte_unmap(new_ptep - 1);
- pte_unmap_unlock(old_ptep - 1, old_ptl);
+ pte_unmap(old_ptep - 1);
out:
if (pmc->need_rmap_locks)
drop_rmap_locks(vma);
@@ -395,14 +393,9 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pmd_lock(mm, old_pmd);
+ old_ptl = pmd_lockptr(mm, old_pmd);
new_ptl = pmd_lockptr(mm, new_pmd);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+ double_pt_lock(old_ptl, new_ptl);
pmd = *old_pmd;
@@ -418,9 +411,7 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,
pmd_populate(mm, new_pmd, pmd_pgtable(pmd));
flush_tlb_range(vma, pmc->old_addr, pmc->old_addr + PMD_SIZE);
out_unlock:
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return res;
}
@@ -452,26 +443,24 @@ static bool move_normal_pud(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pud_none(*new_pud)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pud_lock(mm, old_pud);
+ old_ptl = pud_lockptr(mm, old_pud);
new_ptl = pud_lockptr(mm, new_pud);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
- /* Clear the pud */
+ double_pt_lock(old_ptl, new_ptl);
+
pud = *old_pud;
+ if (unlikely(pud_none(pud)))
+ goto out_unlock;
+
+ /* Clear the pud */
pud_clear(old_pud);
VM_BUG_ON(!pud_none(*new_pud));
pud_populate(mm, new_pud, pud_pgtable(pud));
flush_tlb_range(vma, pmc->old_addr, pmc->old_addr + PUD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+out_unlock:
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
@@ -499,18 +488,17 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pud_none(*new_pud)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pud_lock(mm, old_pud);
+ old_ptl = pud_lockptr(mm, old_pud);
new_ptl = pud_lockptr(mm, new_pud);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (unlikely(!pud_trans_huge(*old_pud))) {
+ double_pt_unlock(old_ptl, new_ptl);
+ return false;
+ }
/* Clear the pud */
- pud = *old_pud;
- pud_clear(old_pud);
+ pud = pudp_huge_get_and_clear(mm, pmc->old_addr, old_pud);
VM_BUG_ON(!pud_none(*new_pud));
@@ -518,9 +506,7 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,
/* mark soft_ditry when we add pud level soft dirty support */
set_pud_at(mm, pmc->new_addr, new_pud, pud);
flush_pud_tlb_range(vma, pmc->old_addr, pmc->old_addr + HPAGE_PUD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
@@ -842,11 +828,10 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc)
if (!new_pud)
break;
if (pud_trans_huge(*old_pud)) {
- if (extent == HPAGE_PUD_SIZE) {
- move_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud);
- /* We ignore and continue on error? */
+ if (extent == HPAGE_PUD_SIZE &&
+ move_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud))
continue;
- }
+ split_huge_pud(pmc->old, old_pud, pmc->old_addr);
} else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
if (move_pgt_entry(pmc, NORMAL_PUD, old_pud, new_pud))
continue;
diff --git a/mm/nommu.c b/mm/nommu.c
index ed44510e37707d..6e0f9c7da2364b 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1926,7 +1926,7 @@ subsys_initcall(init_admin_reserve);
int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
{
- mmap_write_lock(oldmm);
+ mmap_assert_write_locked(oldmm);
dup_mm_exe_file(mm, oldmm);
mmap_write_unlock(oldmm);
return 0;
diff --git a/mm/rmap.c b/mm/rmap.c
index 1131b76bbbc28b..0faef46806e5ca 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -924,6 +924,7 @@ static bool folio_referenced_one(struct folio *folio,
DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
int ptes = 0, referenced = 0;
unsigned int nr;
+ pte_t pteval;
while (page_vma_mapped_walk(&pvmw)) {
address = pvmw.address;
@@ -968,10 +969,12 @@ static bool folio_referenced_one(struct folio *folio,
return false;
}
- if (pvmw.pte && folio_test_large(folio)) {
+ if (pvmw.pte)
+ pteval = ptep_get(pvmw.pte);
+
+ if (pvmw.pte && pte_present(pteval) && folio_test_large(folio)) {
const unsigned long end_addr = pmd_addr_end(address, vma->vm_end);
const unsigned int max_nr = (end_addr - address) >> PAGE_SHIFT;
- pte_t pteval = ptep_get(pvmw.pte);
nr = folio_pte_batch(folio, pvmw.pte, pteval, max_nr);
}
@@ -981,11 +984,23 @@ static bool folio_referenced_one(struct folio *folio,
* are. —they could be on active/inactive lists or on MGLRU. So the
* simplest approach is to disable this look-around optimization.
*/
- if (lru_gen_enabled() && !lru_gen_switching() && pvmw.pte) {
+ if (lru_gen_enabled() && !lru_gen_switching() && pvmw.pte &&
+ pte_present(pteval)) {
if (lru_gen_look_around(&pvmw, nr))
referenced++;
} else if (pvmw.pte) {
- if (clear_flush_young_ptes_notify(vma, address, pvmw.pte, nr))
+ bool young = false;
+
+ /*
+ * PFN swap PTEs, such as device-exclusive ones, that
+ * actually map pages are "old" from a CPU perspective.
+ * The MMU notifier takes care of any device aspects.
+ */
+ if (pte_present(pteval))
+ young = clear_flush_young_ptes(vma, address, pvmw.pte, nr);
+ young |= mmu_notifier_clear_flush_young(vma->vm_mm, address,
+ address + nr * PAGE_SIZE);
+ if (young)
referenced++;
} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
if (pmdp_clear_flush_young_notify(vma, address,
diff --git a/mm/shmem.c b/mm/shmem.c
index fadf23f9e21edb..0d41aac125df9c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -275,6 +275,11 @@ static const struct vm_operations_struct shmem_vm_ops;
static const struct vm_operations_struct shmem_anon_vm_ops;
static struct file_system_type shmem_fs_type;
+bool file_inode_is_shmem(const struct inode *inode)
+{
+ return inode->i_fop == &shmem_file_operations;
+}
+
bool shmem_mapping(const struct address_space *mapping)
{
return mapping->a_ops == &shmem_aops;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 254ce86fa923e6..71ef9e2efb3a98 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2514,7 +2514,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
return 0;
swapcache = folio;
- folio = ksm_might_need_to_copy(folio, vma, addr);
+ folio = ksm_might_need_to_copy(folio,
+ folio_file_page(folio, swp_offset(entry)), vma, addr);
if (unlikely(!folio))
return -ENOMEM;
else if (unlikely(folio == ERR_PTR(-EHWPOISON))) {
diff --git a/mm/truncate.c b/mm/truncate.c
index 733d2713a875a5..6d8c0ec4623feb 100644
--- a/mm/truncate.c
+++ b/mm/truncate.c
@@ -499,7 +499,15 @@ void truncate_inode_pages_range(struct address_space *mapping,
folio_lock(folio);
VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
folio_wait_writeback(folio);
- truncate_inode_folio(mapping, folio);
+ if (truncate_inode_folio(mapping, folio)) {
+ folio_unlock(folio);
+ /*
+ * Folio might have been replaced: look it up
+ * again.
+ */
+ index = indices[i];
+ break;
+ }
folio_unlock(folio);
}
truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
@@ -760,7 +768,12 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
folio_lock(folio);
if (unlikely(folio->mapping != mapping)) {
folio_unlock(folio);
- continue;
+ /*
+ * Folio might have been replaced: look up
+ * again.
+ */
+ index = indices[i];
+ break;
}
VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
folio_wait_writeback(folio);
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 669f71d485d02e..76de84510340cf 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -1206,33 +1206,6 @@ int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
return 0;
}
-void double_pt_lock(spinlock_t *ptl1,
- spinlock_t *ptl2)
- __acquires(ptl1)
- __acquires(ptl2)
-{
- if (ptl1 > ptl2)
- swap(ptl1, ptl2);
- /* lock in virtual address order to avoid lock inversion */
- spin_lock(ptl1);
- if (ptl1 != ptl2)
- spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
- else
- __acquire(ptl2);
-}
-
-void double_pt_unlock(spinlock_t *ptl1,
- spinlock_t *ptl2)
- __releases(ptl1)
- __releases(ptl2)
-{
- spin_unlock(ptl1);
- if (ptl1 != ptl2)
- spin_unlock(ptl2);
- else
- __release(ptl2);
-}
-
static inline bool is_pte_pages_stable(pte_t *dst_pte, pte_t *src_pte,
pte_t orig_dst_pte, pte_t orig_src_pte,
pmd_t *dst_pmd, pmd_t dst_pmdval)
@@ -2270,6 +2243,9 @@ static struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
return ret;
}
+static void wake_userfault_range(struct userfaultfd_ctx *ctx,
+ unsigned long start, unsigned long end);
+
/* Assumes mmap write lock taken, and mm_struct pinned. */
static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
struct vm_area_struct *vma,
@@ -2287,6 +2263,8 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
prev = vma;
for_each_vma_range(vmi, vma, end) {
+ const bool any_mode_removed = vma->vm_flags & __VM_UFFD_FLAGS & ~vm_flags;
+
cond_resched();
VM_WARN_ON_ONCE(!vma_can_userfault(vma, vm_flags, wp_async));
@@ -2332,6 +2310,10 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
*/
userfaultfd_set_ctx(vma, ctx, vm_flags);
+ /* Retry faults for removed modes while the VMA is write-locked. */
+ if (any_mode_removed)
+ wake_userfault_range(ctx, start, vma_end);
+
if (vma_is_hugetlb(vma) && uffd_disable_huge_pmd_share(vma))
hugetlb_unshare_all_pmds(vma);
@@ -3648,6 +3630,17 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
__wake_userfault(ctx, range);
}
+static void wake_userfault_range(struct userfaultfd_ctx *ctx,
+ unsigned long start, unsigned long end)
+{
+ struct userfaultfd_wake_range range = {
+ .start = start,
+ .len = end - start,
+ };
+
+ wake_userfault(ctx, &range);
+}
+
static __always_inline int validate_unaligned_range(
struct mm_struct *mm, __u64 start, __u64 len)
{
@@ -3977,6 +3970,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
ret = 0;
for_each_vma_range(vmi, vma, end) {
+ struct userfaultfd_wake_range range;
+
cond_resched();
/* VMA not registered with userfaultfd. */
@@ -3991,18 +3986,17 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
start = vma->vm_start;
vma_end = min(end, vma->vm_end);
- if (userfaultfd_missing(vma)) {
- /*
- * Wake any concurrent pending userfault while
- * we unregister, so they will not hang
- * permanently and it avoids userland to call
- * UFFDIO_WAKE explicitly.
- */
- struct userfaultfd_wake_range range;
- range.start = start;
- range.len = vma_end - start;
- wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
- }
+ /* Prevent new userfaults from being queued after the wakeup. */
+ vma_start_write(vma);
+
+ /*
+ * Wake any concurrent pending userfault while we unregister,
+ * so they will not hang permanently and it avoids userland to
+ * call UFFDIO_WAKE explicitly.
+ */
+ range.start = start;
+ range.len = vma_end - start;
+ wake_userfault(ctx, &range);
vma = userfaultfd_clear_vma(&vmi, prev, vma,
start, vma_end);
diff --git a/mm/util.c b/mm/util.c
index 7f374bb8bf3635..67f8fca3b2056b 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -697,6 +697,19 @@ void *vcalloc_noprof(size_t n, size_t size)
}
EXPORT_SYMBOL(vcalloc_noprof);
+/**
+ * folio_anon_vma - Get a folio's raw anon_vma pointer.
+ * @folio: The folio.
+ *
+ * The returned pointer may be stale for an unmapped folio. The caller
+ * must ensure the anon_vma stays alive before dereferencing it. A folio
+ * reference or lock alone is insufficient; use folio_get_anon_vma() when
+ * the anon_vma's lifetime is not otherwise protected. Note that
+ * folio_get_anon_vma() is only useful for folios that are still mapped.
+ *
+ * Return: The raw anon_vma pointer for an anonymous, non-KSM folio, or
+ * %NULL otherwise.
+ */
struct anon_vma *folio_anon_vma(const struct folio *folio)
{
unsigned long mapping = (unsigned long)folio->mapping;
diff --git a/mm/vma.c b/mm/vma.c
index 1be613044b4a50..695d0be85ac864 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -516,6 +516,15 @@ void unmap_region(struct unmap_desc *unmap)
tlb_finish_mmu(&tlb);
}
+static int check_split(struct vm_area_struct *vma, unsigned long addr)
+{
+ if (!vma->vm_ops)
+ return 0;
+ if (!vma->vm_ops->may_split)
+ return 0;
+ return vma->vm_ops->may_split(vma, addr);
+}
+
/*
* __split_vma() bypasses sysctl_max_map_count checking. We use this where it
* has already been checked or doesn't make sense to fail.
@@ -532,11 +541,9 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
WARN_ON(vma->vm_start >= addr);
WARN_ON(vma->vm_end <= addr);
- if (vma->vm_ops && vma->vm_ops->may_split) {
- err = vma->vm_ops->may_split(vma, addr);
- if (err)
- return err;
- }
+ err = check_split(vma, addr);
+ if (err)
+ return err;
new = vm_area_dup(vma);
if (!new)
@@ -948,6 +955,10 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
if (vmg->__remove_middle && !can_merge_remove_vma(middle))
return NULL;
+ /* If middle isn't removed then it's resized, i.e. splits. */
+ if (!vmg->__remove_middle && check_split(middle, left_side ? end : start))
+ return NULL;
+
/*
* If we merge both VMAs, then next is also deleted. This implies
* merge_will_delete_vma also.
@@ -3380,7 +3391,8 @@ static int acct_stack_growth(struct vm_area_struct *vma,
* Overcommit.. This must be the final test, as it will
* update security statistics.
*/
- if (security_vm_enough_memory_mm(mm, grow))
+ if (vma_test(vma, VMA_ACCOUNT_BIT) &&
+ security_vm_enough_memory_mm(mm, grow))
return -ENOMEM;
return 0;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9959ca158ce835..6a1af9b92349fb 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
walk->mm_stats[MM_NONLEAF_FOUND]++;
- if (!walk_pte_range(&val, addr, next, args))
+ if (!walk_pte_range(pmd + i, addr, next, args))
continue;
walk->mm_stats[MM_NONLEAF_ADDED]++;
@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
continue;
- walk_pmd_range(&val, addr, next, args);
+ walk_pmd_range(pud + i, addr, next, args);
if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
end = (addr | ~PUD_MASK) + 1;
|
| 3/2 | 2026/10/27 03:18 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30462 output: 40 thoughts: 3363 Thoughts: **Patch Series Evaluation: A Deep Dive into Memory Management Enhancements** Alright, let's get down to brass tacks. I've got a patch series here, already committed, that promises to make some pretty significant changes to the kernel's core memory management. My initial impression is that this is high-value stuff, right in my wheelhouse. From a preliminary skim, it looks like this batch is likely a mm tree pull or something similar – it's touching a lot of fundamental components. My Spidey-sense is tingling, this is definitely worth a closer look for fuzzing purposes. Given the positive criteria for fuzzing, which include core subsystem modifications and refactoring of data structures, I'm already leaning towards "WorthFuzzing=TRUE". First things first, I've got to understand these changes. I'm seeing checks in `fs/proc/task_mmu.c` related to the `pagemap_scan_ioctl`, which is interesting. Then there's some range size adjustments in `include/asm-generic/tlb.h`, plus folio rmap tracking in huge page handling! I spot lock refactoring for huge pages, and double-pt-lock being used! I'm seeing changes in `ksm_might_need_to_copy`, and fixes in `merge_across_nodes_store`. I see `xa_load` being used. I see `file_inode_is_shmem`, and atomic positions. I also see locking changes in `dup_mm` and `dup_mmap`, which is essential. There's a leak-handling change in `cma_release`. I'm seeing refactorings of the filemap, with orderings, race fixes and retry logic changes! I'm noticing an interesting change in `gup_fast_fallback`. I'm seeing `move_huge_pte`. There's refactoring in `try_collapse_pte_mapped_thp`, and changes to avoid clearing dirty flags on KSM folios in `madvise_free_pte_range`. Interesting refactorings in `clear_node_memory_tier`. There's also changes in page fault handling, which is ubiquitous (but important to note). I spot overflow checks and casts for unmap ranges and a `folio_file_page` call. I see changes in folio handling, plus rmap, and truncate routines. There are also changes related to userfaultfd. Now, the trick is to zero in on the relevant entry points and feature logic. This means avoiding the ubiquitous functions like page fault handlers, `fork`, general memory allocators/deallocators, and core synchronization primitives. Those are called everywhere and would dilute the fuzzing. I'm making a list of the *specific* functions and entrypoints that are touched here: * **`pagemap_scan_get_args`**: Part of `pagemap_scan_ioctl` or `do_pagemap_scan`. * **`cma_release`** * **`follow_pud_mask`**, **`gup_fast_fallback`**, **`memfd_pin_folios`**. * **`copy_huge_pud`**, **`zap_huge_pmd_folio`**, **`move_huge_pmd`**, **`split_huge_leaf`**, and associated caller **`vma_adjust_trans_huge`**; **`deferred_split_scan`**. * **`move_huge_pte`** (called by `move_hugetlb_page_tables`). * **`try_collapse_pte_mapped_thp`** * **`ksm_might_need_to_copy`**, **`merge_across_nodes_store`**. * **`madvise_free_pte_range`** (through the `madvise_*` family: `madvise_free`, `madvise_free_single_vma`, `madvise_walk_vmas`). * **`memfd_alloc_file`** and `memfd_file_seals_ptr` (accessed via `memfd_fcntl`, e.g. `F_ADD_SEALS`). * **`memfd_luo_can_preserve`** * **`clear_node_memory_tier`** (static), **`memtier_hotplug_callback`**. * **`unmap_mapping_range`**, **`unuse_pte`**. * **`copy_pud_range`** * **`pfn_to_online_page`**. * **`pfn_has |
| 4/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "pagemap_scan_get_args",
"PathPrefixes": [
"fs/proc/task_mmu.c"
]
}Results: {
"Output": "fs/proc/task_mmu.c=3012=static const struct mm_walk_ops pagemap_scan_ops = {\n--\nfs/proc/task_mmu.c-3018-\nfs/proc/task_mmu.c:3019:static int pagemap_scan_get_args(struct pm_scan_arg *arg,\nfs/proc/task_mmu.c-3020-\t\t\t\t unsigned long uarg)\n--\nfs/proc/task_mmu.c=3122=static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)\n--\nfs/proc/task_mmu.c-3131-\nfs/proc/task_mmu.c:3132:\tret = pagemap_scan_get_args(\u0026p.arg, uarg);\nfs/proc/task_mmu.c-3133-\tif (ret)\n"
} |
| 5/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35132 output: 38 thoughts: 37 Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue: **Investigating the `do_pagemap_scan` Function** Alright, I'm diving into this code. It seems like `do_pagemap_scan` is a key function here. The initial step is pretty straightforward; the flow moves from `do_pagemap_scan` to `pagemap_scan_get_args`. That call to `pagemap_scan_get_args` is likely where the real work of getting the scan parameters happens. So the next thing I need to do is quickly work out who, or what, is calling `do_pagemap_scan`. Understanding its callers is vital for figuring out what triggers this function, and what context it operates in. I need to trace back from `do_pagemap_scan` to understand the bigger picture of this memory management process. Knowing the calling points will give me the crucial insight I need into how this scan is initiated and its overall purpose within the system. |
| 6/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "do_pagemap_scan",
"PathPrefixes": [
"fs/proc/task_mmu.c"
]
}Results: {
"Output": "fs/proc/task_mmu.c=3095=static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p)\n--\nfs/proc/task_mmu.c-3121-\nfs/proc/task_mmu.c:3122:static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)\nfs/proc/task_mmu.c-3123-{\n--\nfs/proc/task_mmu.c=3200=static long do_pagemap_cmd(struct file *file, unsigned int cmd,\n--\nfs/proc/task_mmu.c-3206-\tcase PAGEMAP_SCAN:\nfs/proc/task_mmu.c:3207:\t\treturn do_pagemap_scan(mm, arg);\nfs/proc/task_mmu.c-3208-\n"
} |
| 7/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35671 output: 32 thoughts: 61 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into Pagemap Scan Implementation** Okay, so `do_pagemap_cmd` is responsible for handling the `PAGEMAP_SCAN` ioctl call when dealing with `/proc/[pid]/pagemap`. Makes sense. Now, what's controlling the actual enablement or disablement of this `PAGEMAP_SCAN` feature? I'm betting there's a configuration option involved. Let's see... my gut tells me this will be in `fs/proc/task_mmu.c`. Time to dig into that file and see what the guards are for this particular feature. Hopefully it’s well-commented! |
| 8/2 | 2026/10/27 03:19 | tool | grepper |
0mError:no matches Args: {
"Expression": "CONFIG_.*PAGEMAP",
"PathPrefixes": [
"fs/proc/"
]
}Results: {
"error": "no matches"
} |
| 9/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35937 output: 29 thoughts: 20 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating `task_mmu.c` Dependencies** Right, let's dive in. I need to understand the relationship of `task_mmu.c` within this specific kernel configuration. My first move is to check the `fs/proc/Makefile` and `Kconfig` files. That's the obvious starting point. I want to see if this module has any build dependencies or conditional compilation flags that are affecting its inclusion or behavior. I'm looking for things like: * Does it get compiled as a module or built-in, and under what circumstances? * Are there any `#ifdef` blocks or `config` options that directly control its inclusion? * What other modules or kernel features does `task_mmu.c` depend on from a build perspective? * Are there any subtle interactions I should be aware of regarding Kconfig options? Basically, I need to trace the build path, understand the compile-time conditions, and confirm what, if any, impacts this would be having on the function of the code, so I can ensure my understanding is accurate. |
| 10/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "task_mmu",
"PathPrefixes": [
"fs/proc/"
]
}Results: {
"Output": "fs/proc/Makefile=6=obj-y += proc.o\nfs/proc/Makefile-7-\nfs/proc/Makefile:8:CFLAGS_task_mmu.o\t+= -Wno-override-init\nfs/proc/Makefile-9-proc-y\t\t\t:= nommu.o task_nommu.o\nfs/proc/Makefile:10:proc-$(CONFIG_MMU)\t:= task_mmu.o\nfs/proc/Makefile-11-\n"
} |
| 11/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36362 output: 31 thoughts: 67 Thoughts: **Examining Kernel Configuration for Memory Management** Okay, so I'm looking at kernel configurations related to memory management, specifically `CONFIG_PROC_FS` and `CONFIG_MMU`. These are fundamental, so it's a good starting point. I want to understand how changes in these configurations might ripple through the system. Let me take a look for files impacted by the `CONFIG_` flags. I see `fs/proc/task_mmu.c` listed. Right, that makes sense – process-specific memory management and the proc filesystem are intertwined. I'm noting a potential flag here, `CONFIG_PROC_PAGE_MONITOR`. I should investigate that configuration option more thoroughly. It's likely involved in providing monitoring capabilities within the proc filesystem related to page usage. |
| 12/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "PROC_PAGE_MONITOR",
"PathPrefixes": [
"fs/proc/"
]
}Results: {
"Output": "fs/proc/Kconfig=83=config SYSCTL\n--\nfs/proc/Kconfig-100-\nfs/proc/Kconfig:101:config PROC_PAGE_MONITOR\nfs/proc/Kconfig-102- \tdefault y\n--\nfs/proc/Makefile=36=proc-$(CONFIG_PRINTK)\t+= kmsg.o\nfs/proc/Makefile:37:proc-$(CONFIG_PROC_PAGE_MONITOR)\t+= page.o\nfs/proc/Makefile-38-proc-$(CONFIG_BOOT_CONFIG)\t+= bootconfig.o\n--\nfs/proc/base.c=3283=static const struct pid_entry tgid_base_stuff[] = {\n--\nfs/proc/base.c-3321-\tREG(\"mountstats\", S_IRUSR, proc_mountstats_operations),\nfs/proc/base.c:3322:#ifdef CONFIG_PROC_PAGE_MONITOR\nfs/proc/base.c-3323-\tREG(\"clear_refs\", S_IWUSR, proc_clear_refs_operations),\n--\nfs/proc/base.c=3642=static const struct pid_entry tid_base_stuff[] = {\n--\nfs/proc/base.c-3676-\tREG(\"mountinfo\", S_IRUGO, proc_mountinfo_operations),\nfs/proc/base.c:3677:#ifdef CONFIG_PROC_PAGE_MONITOR\nfs/proc/base.c-3678-\tREG(\"clear_refs\", S_IWUSR, proc_clear_refs_operations),\n--\nfs/proc/task_mmu.c=170=static bool fallback_to_mmap_lock(struct proc_maps_private *priv,\n--\nfs/proc/task_mmu.c-186-\nfs/proc/task_mmu.c:187:#if defined(CONFIG_PROC_PAGE_MONITOR) || defined(CONFIG_NUMA)\nfs/proc/task_mmu.c-188-static void drop_rcu(struct proc_maps_private *priv)\n--\nfs/proc/task_mmu.c=776=const struct file_operations proc_pid_maps_operations = {\n--\nfs/proc/task_mmu.c-803-\nfs/proc/task_mmu.c:804:#ifdef CONFIG_PROC_PAGE_MONITOR\nfs/proc/task_mmu.c-805-struct mem_size_stats {\n--\nfs/proc/task_mmu.c=3214=const struct file_operations proc_pagemap_operations = {\n--\nfs/proc/task_mmu.c-3221-};\nfs/proc/task_mmu.c:3222:#endif /* CONFIG_PROC_PAGE_MONITOR */\nfs/proc/task_mmu.c-3223-\n"
} |
| 13/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37344 output: 39 thoughts: 178 Thoughts: **Investigating Transparent Hugepages and Pagemap Operations** Okay, so I'm digging into how the kernel handles page monitoring, specifically around `proc_pagemap_operations` and the `pagemap_scan` function. It's immediately clear that `CONFIG_PROC_PAGE_MONITOR` is the gatekeeper here, that much is straightforward. Now, let's pivot to the relationship with Transparent Hugepages. I remember that impacting memory management significantly. I need to confirm the interactions. Let me start by checking `CONFIG_TRANSPARENT_HUGEPAGE` itself. Ah, yep, a quick look within `mm/huge_memory.c` confirms its active use. Good, good. Alright, I need to see how transparent hugepages tie into this. I see there's a dependency, and I need to check further. I'll need to check `CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD` and of course `CONFIG_TRANSPARENT_HUGEPAGE` itself. Heading over to `include/linux/huge_mm.h` reveals the conditional `#ifdef CONFIG_TRANSPARENT_HUGEPAGE` which is what I suspected. This requires further examination, so I'll check `CONFIG_TRANSPARENT_HUGEPAGE_PUD` and/or `CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD`. I need to figure out which of these configurations is responsible for which interactions and where. I have my work cut out for me. |
| 14/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "TRANSPARENT_HUGEPAGE_PUD",
"PathPrefixes": [
"mm/",
"include/"
]
}Results: {
"Output": "include/linux/huge_mm.h=15=int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,\n--\ninclude/linux/huge_mm.h-17-\ninclude/linux/huge_mm.h:18:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\ninclude/linux/huge_mm.h-19-void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud);\n--\ninclude/linux/huge_mm.h=482=static inline bool pud_is_huge(pud_t pud)\n--\ninclude/linux/huge_mm.h-491-\ninclude/linux/huge_mm.h:492:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\ninclude/linux/huge_mm.h-493-int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\ninclude/linux/mm.h=2862=static inline pmd_t folio_mk_pmd(const struct folio *folio, pgprot_t pgprot)\n--\ninclude/linux/mm.h-2866-\ninclude/linux/mm.h:2867:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\ninclude/linux/mm.h-2868-/**\n--\ninclude/linux/mm.h=2878=static inline pud_t folio_mk_pud(const struct folio *folio, pgprot_t pgprot)\n--\ninclude/linux/mm.h-2881-}\ninclude/linux/mm.h:2882:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\ninclude/linux/mm.h-2883-#endif /* CONFIG_TRANSPARENT_HUGEPAGE */\n--\ninclude/linux/pgtable.h=1225=static inline void pmdp_set_wrprotect(struct mm_struct *mm,\n--\ninclude/linux/pgtable.h-1232-#ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT\ninclude/linux/pgtable.h:1233:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\ninclude/linux/pgtable.h-1234-#ifdef CONFIG_TRANSPARENT_HUGEPAGE\n--\ninclude/linux/pgtable.h=1243=static inline void pudp_set_wrprotect(struct mm_struct *mm,\n--\ninclude/linux/pgtable.h-1248-#endif /* CONFIG_TRANSPARENT_HUGEPAGE */\ninclude/linux/pgtable.h:1249:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\ninclude/linux/pgtable.h-1250-#endif\n--\ninclude/linux/pgtable.h=2082=static inline int pud_write(pud_t pud)\n--\ninclude/linux/pgtable.h-2089-#if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \\\ninclude/linux/pgtable.h:2090:\t!defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\ninclude/linux/pgtable.h-2091-static inline int pud_trans_huge(pud_t pud)\n--\ninclude/linux/pgtable.h=2097=static inline int pud_trans_unstable(pud_t *pud)\n--\ninclude/linux/pgtable.h-2099-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026 \\\ninclude/linux/pgtable.h:2100:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\ninclude/linux/pgtable.h-2101-\tpud_t pudval = pudp_get(pud);\n--\ninclude/linux/pgtable.h=2316=void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size);\n--\ninclude/linux/pgtable.h-2349-#ifndef has_transparent_pud_hugepage\ninclude/linux/pgtable.h:2350:#define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\ninclude/linux/pgtable.h-2351-#endif\n--\ninclude/linux/vm_event_item.h=34=enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,\n--\ninclude/linux/vm_event_item.h-103-\t\tTHP_SCAN_EXCEED_SHARED_PTE,\ninclude/linux/vm_event_item.h:104:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\ninclude/linux/vm_event_item.h-105-\t\tTHP_SPLIT_PUD,\n--\nmm/Kconfig=1008=config HAVE_GIGANTIC_FOLIOS\nmm/Kconfig-1009-\tdef_bool (HUGETLB_PAGE \u0026\u0026 ARCH_HAS_GIGANTIC_PAGE) || \\\nmm/Kconfig:1010:\t\t (ZONE_DEVICE \u0026\u0026 HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/Kconfig-1011-\n--\nmm/Kconfig=1024=config ARCH_SUPPORTS_PUD_PFNMAP\nmm/Kconfig-1025-\tdef_bool y\nmm/Kconfig:1026:\tdepends on ARCH_SUPPORTS_HUGE_PFNMAP \u0026\u0026 HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/Kconfig-1027-\n--\nmm/debug_vm_pgtable.c=282=static void __init pmd_leaf_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c-298-\nmm/debug_vm_pgtable.c:299:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/debug_vm_pgtable.c-300-static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)\n--\nmm/debug_vm_pgtable.c=405=static void __init pud_leaf_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c-419-}\nmm/debug_vm_pgtable.c:420:#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/debug_vm_pgtable.c-421-static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }\n--\nmm/debug_vm_pgtable.c=423=static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }\nmm/debug_vm_pgtable.c:424:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/debug_vm_pgtable.c-425-#else /* !CONFIG_TRANSPARENT_HUGEPAGE */\n--\nmm/debug_vm_pgtable.c=905=static void __init pmd_thp_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c-933-\nmm/debug_vm_pgtable.c:934:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/debug_vm_pgtable.c-935-static void __init pud_thp_tests(struct pgtable_debug_args *args)\n--\nmm/debug_vm_pgtable.c-953-}\nmm/debug_vm_pgtable.c:954:#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/debug_vm_pgtable.c-955-static void __init pud_thp_tests(struct pgtable_debug_args *args) { }\nmm/debug_vm_pgtable.c:956:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/debug_vm_pgtable.c-957-#else /* !CONFIG_TRANSPARENT_HUGEPAGE */\n--\nmm/hmm.c=403=static int hmm_vma_walk_pmd(pmd_t *pmdp,\n--\nmm/hmm.c-479-\nmm/hmm.c:480:#if defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/hmm.c-481-static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,\n--\nmm/huge_memory.c=1748=EXPORT_SYMBOL_GPL(vmf_insert_folio_pmd);\nmm/huge_memory.c-1749-\nmm/huge_memory.c:1750:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-1751-static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)\n--\nmm/huge_memory.c=1864=EXPORT_SYMBOL_GPL(vmf_insert_folio_pud);\nmm/huge_memory.c:1865:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-1866-\n--\nmm/huge_memory.c=1949=int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n--\nmm/huge_memory.c-2060-\nmm/huge_memory.c:2061:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-2062-void touch_pud(struct vm_area_struct *vma, unsigned long addr,\n--\nmm/huge_memory.c=2121=void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)\n--\nmm/huge_memory.c-2132-}\nmm/huge_memory.c:2133:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-2134-\n--\nmm/huge_memory.c=2734=int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c-2841- */\nmm/huge_memory.c:2842:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-2843-int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c=3033=spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)\n--\nmm/huge_memory.c-3043-\nmm/huge_memory.c:3044:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-3045-int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c=3132=void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,\n--\nmm/huge_memory.c-3135-}\nmm/huge_memory.c:3136:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-3137-\n--\nmm/huge_memory.c=3506=static bool pud_split_required(const struct vm_area_struct *vma,\n--\nmm/huge_memory.c-3508-{\nmm/huge_memory.c:3509:\tif (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))\nmm/huge_memory.c-3510-\t\treturn false;\n--\nmm/mapping_dirty_helpers.c=148=static int wp_clean_pud_entry(pud_t *pud, unsigned long addr, unsigned long end,\n--\nmm/mapping_dirty_helpers.c-150-{\nmm/mapping_dirty_helpers.c:151:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/mapping_dirty_helpers.c-152-\tpud_t pudval = pudp_get(pud);\n--\nmm/memory.c=6498=static vm_fault_t create_huge_pud(struct vm_fault *vmf)\n--\nmm/memory.c-6500-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026\t\t\t\\\nmm/memory.c:6501:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/memory.c-6502-\tstruct vm_area_struct *vma = vmf-\u003evma;\n--\nmm/memory.c=6512=static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)\n--\nmm/memory.c-6514-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026\t\t\t\\\nmm/memory.c:6515:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/memory.c-6516-\tstruct vm_area_struct *vma = vmf-\u003evma;\n--\nmm/memory.c-6531-\t__split_huge_pud(vma, vmf-\u003epud, vmf-\u003eaddress);\nmm/memory.c:6532:#endif /* CONFIG_TRANSPARENT_HUGEPAGE \u0026\u0026 CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/memory.c-6533-\treturn VM_FAULT_FALLBACK;\n--\nmm/mremap.c=468=static inline bool move_normal_pud(struct pagetable_move_control *pmc,\n--\nmm/mremap.c-474-\nmm/mremap.c:475:#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/mremap.c-476-static bool move_huge_pud(struct pagetable_move_control *pmc,\n--\nmm/pgtable-generic.c=153=pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,\n--\nmm/pgtable-generic.c-163-\nmm/pgtable-generic.c:164:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/pgtable-generic.c-165-pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,\n--\nmm/rmap.c=1788=void folio_add_file_rmap_pud(struct folio *folio, struct page *page,\n--\nmm/rmap.c-1791-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026 \\\nmm/rmap.c:1792:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/rmap.c-1793-\t__folio_add_file_rmap(folio, page, HPAGE_PUD_NR, vma, PGTABLE_LEVEL_PUD);\n--\nmm/rmap.c=1956=void folio_remove_rmap_pud(struct folio *folio, struct page *page,\n--\nmm/rmap.c-1959-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026 \\\nmm/rmap.c:1960:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\nmm/rmap.c-1961-\t__folio_remove_rmap(folio, page, HPAGE_PUD_NR, vma, PGTABLE_LEVEL_PUD);\n--\nmm/vmstat.c=1199=const char * const vmstat_text[] = {\n--\nmm/vmstat.c-1430-\t[I(THP_SCAN_EXCEED_SHARED_PTE)]\t\t= \"thp_scan_exceed_share_pte\",\nmm/vmstat.c:1431:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/vmstat.c-1432-\t[I(THP_SPLIT_PUD)]\t\t\t= \"thp_split_pud\",\n"
} |
| 15/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42279 output: 43 thoughts: 53 Thoughts: Okay, let's break this down. **Investigating x86_64 Transparent Huge Pages** Right, so I'm wondering about Transparent Huge Pages (THP) on x86_64, specifically if the `HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD` configuration option is enabled. I need to figure out if the architecture supports this at the Page Upper Directory (PUD) level. I'll need to check the kernel's configuration options to be sure. My first port of call will be `arch/x86/Kconfig` – that's where the architecture-specific configuration for x86 systems will be defined. I'll search through that to locate the relevant setting and confirm its status. |
| 16/2 | 2026/10/27 03:19 | tool | grepper |
0mArgs:{
"Expression": "HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD",
"PathPrefixes": [
"arch/x86/"
]
}Results: {
"Output": "arch/x86/Kconfig=57=config X86\n--\narch/x86/Kconfig-210-\tselect HAVE_ARCH_TRANSPARENT_HUGEPAGE\narch/x86/Kconfig:211:\tselect HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64\narch/x86/Kconfig-212-\tselect HAVE_ARCH_USERFAULTFD_WP if X86_64 \u0026\u0026 USERFAULTFD\n--\narch/x86/include/asm/pgtable.h=297=static inline int pmd_trans_huge(pmd_t pmd)\n--\narch/x86/include/asm/pgtable.h-301-\narch/x86/include/asm/pgtable.h:302:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\narch/x86/include/asm/pgtable.h-303-static inline int pud_trans_huge(pud_t pud)\n--\narch/x86/include/asm/pgtable.h=1357=static inline pmd_t pmdp_establish(struct vm_area_struct *vma,\n--\narch/x86/include/asm/pgtable.h-1370-\narch/x86/include/asm/pgtable.h:1371:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\narch/x86/include/asm/pgtable.h-1372-static inline pud_t pudp_establish(struct vm_area_struct *vma,\n--\narch/x86/mm/pgtable.c=509=pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,\n--\narch/x86/mm/pgtable.c-522-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) \u0026\u0026 \\\narch/x86/mm/pgtable.c:523:\tdefined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)\narch/x86/mm/pgtable.c-524-pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,\n"
} |
| 17/2 | 2026/10/27 03:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43117 output: 26 thoughts: 235 Thoughts: **Analyzing Transparent Huge Pages and Related Configurations** Okay, so I'm looking at this code and it seems pretty straightforward on x86_64: if `TRANSPARENT_HUGEPAGE` is enabled, then `HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD` gets selected. Makes sense. `TRANSPARENT_HUGEPAGE` is definitely the critical piece here, the one I need to focus on. Now, let's see what other configurations are relevant, based on those modified files. Let's see... I see `mm/ksm.c` corresponds to `CONFIG_KSM`, which is good to know. Then `mm/memfd.c` links to `CONFIG_MEMFD_CREATE`. `mm/hugetlb.c` is clearly about huge pages, and there are a couple of related options here: `CONFIG_HUGETLB_PAGE` and `CONFIG_HUGETLBFS`. Moving on, `mm/userfaultfd.c` relies on `CONFIG_USERFAULTFD`. `mm/cma.c` is connected to `CONFIG_CMA`, which I recall from other work. Finally, `fs/proc/task_mmu.c` is linked to `CONFIG_PROC_PAGE_MONITOR`, and `mm/memory-tiers.c` is linked to `CONFIG_NUMA` and `CONFIG_MEMORY_TIERS`. Alright, I'll need to double-check that `mm/memory-tiers.c` is appropriately handling both, just to be sure. |
| 18/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "memory-tiers",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=99=obj-$(CONFIG_MIGRATION) += migrate.o\nmm/Makefile:100:obj-$(CONFIG_NUMA) += memory-tiers.o\nmm/Makefile-101-obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o\n--\nmm/damon/paddr.c-12-#include \u003clinux/swap.h\u003e\nmm/damon/paddr.c:13:#include \u003clinux/memory-tiers.h\u003e\nmm/damon/paddr.c-14-#include \u003clinux/mm_inline.h\u003e\n--\nmm/huge_memory.c-38-#include \u003clinux/sched/sysctl.h\u003e\nmm/huge_memory.c:39:#include \u003clinux/memory-tiers.h\u003e\nmm/huge_memory.c-40-#include \u003clinux/compat.h\u003e\n--\nmm/memory-tiers.c-6-#include \u003clinux/memory.h\u003e\nmm/memory-tiers.c:7:#include \u003clinux/memory-tiers.h\u003e\nmm/memory-tiers.c-8-#include \u003clinux/notifier.h\u003e\n--\nmm/memory-tiers.c=736=int mt_set_default_dram_perf(int nid, struct access_coordinate *perf,\n--\nmm/memory-tiers.c-768-\t\tpr_info(\nmm/memory-tiers.c:769:\"memory-tiers: the performance of DRAM node %d mismatches that of the reference\\n\"\nmm/memory-tiers.c-770-\"DRAM node %d.\\n\", nid, default_dram_perf_ref_nid);\n--\nmm/memory.c-68-#include \u003clinux/shmem_fs.h\u003e\nmm/memory.c:69:#include \u003clinux/memory-tiers.h\u003e\nmm/memory.c-70-#include \u003clinux/debugfs.h\u003e\n--\nmm/mempolicy.c-102-#include \u003clinux/proc_fs.h\u003e\nmm/mempolicy.c:103:#include \u003clinux/memory-tiers.h\u003e\nmm/mempolicy.c-104-#include \u003clinux/migrate.h\u003e\n--\nmm/migrate.c-43-#include \u003clinux/sched/sysctl.h\u003e\nmm/migrate.c:44:#include \u003clinux/memory-tiers.h\u003e\nmm/migrate.c-45-#include \u003clinux/pagewalk.h\u003e\n--\nmm/vmscan.c-44-#include \u003clinux/sysctl.h\u003e\nmm/vmscan.c:45:#include \u003clinux/memory-tiers.h\u003e\nmm/vmscan.c-46-#include \u003clinux/oom.h\u003e\n"
} |
| 19/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44376 output: 28 thoughts: 64 Thoughts: Here's my thought process, as it stands: **Navigating Dependencies and Memory Management in the Kernel** Okay, so `memory-tiers.o` has a dependency on `CONFIG_NUMA`. That makes sense, I knew NUMA was relevant, but I need to understand this better. Now, let's see how `ZONE_DEVICE` factors in here. I remember that impacting memory mapping in specific architectures. Let's delve into that with `mm/memremap.c` and its associated `pfn_has_dev_pagemap`. I'll want to see how this all connects. First, I need to figure out what is actually building `mm/memremap.c` to see the whole picture. |
| 20/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "memremap\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=139=obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o\nmm/Makefile:140:obj-$(CONFIG_ZONE_DEVICE) += memremap.o\nmm/Makefile-141-obj-$(CONFIG_HMM_MIRROR) += hmm.o\n"
} |
| 21/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44712 output: 31 thoughts: 85 Thoughts: **Investigating `CONFIG_ZONE_DEVICE` and Device-Private Memory in the Kernel** Okay, so `CONFIG_ZONE_DEVICE` is the key – it looks like that's the trigger that builds `memremap.o`. That's my starting point. Now, I need to understand the connection. Let me jump over to check `CONFIG_DEVICE_PRIVATE`. I'm guessing that's the more relevant flag in this case. My instincts tell me it's related to how device memory interacts with the system. Aha, and in `mm/huge_memory.c`, there's `do_huge_pmd_device_private` and `pmd_is_device_private_entry`. That seems like the right area. Let's delve in and see how `CONFIG_DEVICE_PRIVATE` is actually used within `mm/huge_memory.c`. I need to figure out how these functions and that config option interrelate. I'm going to look at the surrounding code to grasp the larger picture. |
| 22/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "DEVICE_PRIVATE",
"PathPrefixes": [
"mm/",
"include/"
]
}Results: {
"Output": "include/linux/hmm.h=14=struct mmu_interval_notifier;\n--\ninclude/linux/hmm.h-21- * least readable. If dev_private_owner is !NULL then this could\ninclude/linux/hmm.h:22: * point at a DEVICE_PRIVATE page.\ninclude/linux/hmm.h-23- * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)\n--\ninclude/linux/ioport.h=136=enum {\n--\ninclude/linux/ioport.h-142-\tIORES_DESC_PERSISTENT_MEMORY_LEGACY\t= 5,\ninclude/linux/ioport.h:143:\tIORES_DESC_DEVICE_PRIVATE_MEMORY\t= 6,\ninclude/linux/ioport.h-144-\tIORES_DESC_RESERVED\t\t\t= 7,\n--\ninclude/linux/leafops.h=19=enum softleaf_type {\n--\ninclude/linux/leafops.h-27-\t/* Device types. */\ninclude/linux/leafops.h:28:\tSOFTLEAF_DEVICE_PRIVATE_READ,\ninclude/linux/leafops.h:29:\tSOFTLEAF_DEVICE_PRIVATE_WRITE,\ninclude/linux/leafops.h-30-\tSOFTLEAF_DEVICE_EXCLUSIVE,\n--\ninclude/linux/leafops.h=160=static inline enum softleaf_type softleaf_type(softleaf_t entry)\n--\ninclude/linux/leafops.h-180-#endif\ninclude/linux/leafops.h:181:#ifdef CONFIG_DEVICE_PRIVATE\ninclude/linux/leafops.h-182-\tcase SWP_DEVICE_WRITE:\ninclude/linux/leafops.h:183:\t\treturn SOFTLEAF_DEVICE_PRIVATE_WRITE;\ninclude/linux/leafops.h-184-\tcase SWP_DEVICE_READ:\ninclude/linux/leafops.h:185:\t\treturn SOFTLEAF_DEVICE_PRIVATE_READ;\ninclude/linux/leafops.h-186-\tcase SWP_DEVICE_EXCLUSIVE:\n--\ninclude/linux/leafops.h=276=static inline bool softleaf_is_device_private_write(softleaf_t entry)\ninclude/linux/leafops.h-277-{\ninclude/linux/leafops.h:278:\treturn softleaf_type(entry) == SOFTLEAF_DEVICE_PRIVATE_WRITE;\ninclude/linux/leafops.h-279-}\n--\ninclude/linux/leafops.h=287=static inline bool softleaf_is_device_private(softleaf_t entry)\n--\ninclude/linux/leafops.h-289-\tswitch (softleaf_type(entry)) {\ninclude/linux/leafops.h:290:\tcase SOFTLEAF_DEVICE_PRIVATE_WRITE:\ninclude/linux/leafops.h:291:\tcase SOFTLEAF_DEVICE_PRIVATE_READ:\ninclude/linux/leafops.h-292-\t\treturn true;\n--\ninclude/linux/memremap.h=21=struct vmem_altmap {\n--\ninclude/linux/memremap.h-33- *\ninclude/linux/memremap.h:34: * MEMORY_DEVICE_PRIVATE:\ninclude/linux/memremap.h-35- * Device memory that is not directly addressable by the CPU: CPU can neither\n--\ninclude/linux/memremap.h=68=enum memory_type {\ninclude/linux/memremap.h-69-\t/* 0 is reserved to catch uninitialized type fields */\ninclude/linux/memremap.h:70:\tMEMORY_DEVICE_PRIVATE = 1,\ninclude/linux/memremap.h-71-\tMEMORY_DEVICE_COHERENT,\n--\ninclude/linux/memremap.h=166=static inline bool folio_is_device_private(const struct folio *folio)\ninclude/linux/memremap.h-167-{\ninclude/linux/memremap.h:168:\treturn IS_ENABLED(CONFIG_DEVICE_PRIVATE) \u0026\u0026\ninclude/linux/memremap.h-169-\t\tfolio_is_zone_device(folio) \u0026\u0026\ninclude/linux/memremap.h:170:\t\tfolio-\u003epgmap-\u003etype == MEMORY_DEVICE_PRIVATE;\ninclude/linux/memremap.h-171-}\n--\ninclude/linux/memremap.h=173=static inline bool is_device_private_page(const struct page *page)\ninclude/linux/memremap.h-174-{\ninclude/linux/memremap.h:175:\treturn IS_ENABLED(CONFIG_DEVICE_PRIVATE) \u0026\u0026\ninclude/linux/memremap.h-176-\t\tfolio_is_device_private(page_folio(page));\n--\ninclude/linux/migrate.h=153=enum migrate_vma_direction {\ninclude/linux/migrate.h-154-\tMIGRATE_VMA_SELECT_SYSTEM = 1 \u003c\u003c 0,\ninclude/linux/migrate.h:155:\tMIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 \u003c\u003c 1,\ninclude/linux/migrate.h-156-\tMIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 \u003c\u003c 2,\n--\ninclude/linux/migrate.h=160=struct migrate_vma {\n--\ninclude/linux/migrate.h-179-\t * for migrating out of device private memory. The flags also need to\ninclude/linux/migrate.h:180:\t * be set to MIGRATE_VMA_SELECT_DEVICE_PRIVATE.\ninclude/linux/migrate.h-181-\t * The caller should always set this field when using mmu notifier\n--\ninclude/linux/swap.h-64- */\ninclude/linux/swap.h:65:#ifdef CONFIG_DEVICE_PRIVATE\ninclude/linux/swap.h-66-#define SWP_DEVICE_NUM 3\n--\ninclude/linux/swapops.h=131=static inline void *swp_to_radix_entry(swp_entry_t entry)\n--\ninclude/linux/swapops.h-135-\ninclude/linux/swapops.h:136:#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)\ninclude/linux/swapops.h-137-static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)\n--\ninclude/linux/swapops.h=147=static inline swp_entry_t make_device_exclusive_entry(pgoff_t offset)\n--\ninclude/linux/swapops.h-151-\ninclude/linux/swapops.h:152:#else /* CONFIG_DEVICE_PRIVATE */\ninclude/linux/swapops.h-153-static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)\n--\ninclude/linux/swapops.h=163=static inline swp_entry_t make_device_exclusive_entry(pgoff_t offset)\n--\ninclude/linux/swapops.h-167-\ninclude/linux/swapops.h:168:#endif /* CONFIG_DEVICE_PRIVATE */\ninclude/linux/swapops.h-169-\n--\nmm/Kconfig=1224=config ZONE_DEVICE\n--\nmm/Kconfig-1234-\t \"device-physical\" addresses which is needed for DAX, PCI_P2PDMA, and\nmm/Kconfig:1235:\t DEVICE_PRIVATE features among others.\nmm/Kconfig-1236-\n--\nmm/Kconfig=1252=config GET_FREE_REGION\n--\nmm/Kconfig-1254-\nmm/Kconfig:1255:config DEVICE_PRIVATE\nmm/Kconfig-1256-\tbool \"Unaddressable device memory (GPU memory, ...)\"\n--\nmm/memory-failure.c=1761=static int mf_generic_kill_procs(unsigned long long pfn, int flags,\n--\nmm/memory-failure.c-1785-\tswitch (pgmap-\u003etype) {\nmm/memory-failure.c:1786:\tcase MEMORY_DEVICE_PRIVATE:\nmm/memory-failure.c-1787-\tcase MEMORY_DEVICE_COHERENT:\n--\nmm/memremap.c=86=static void pageunmap_range(struct dev_pagemap *pgmap, int range_id)\n--\nmm/memremap.c-97-\t\t\t\t PHYS_PFN(range_len(range)));\nmm/memremap.c:98:\tif (pgmap-\u003etype == MEMORY_DEVICE_PRIVATE) {\nmm/memremap.c-99-\t\t__remove_pages(PHYS_PFN(range-\u003estart),\n--\nmm/memremap.c=112=void memunmap_pages(struct dev_pagemap *pgmap)\n--\nmm/memremap.c-116-\tpercpu_ref_kill(\u0026pgmap-\u003eref);\nmm/memremap.c:117:\tif (pgmap-\u003etype != MEMORY_DEVICE_PRIVATE \u0026\u0026\nmm/memremap.c-118-\t pgmap-\u003etype != MEMORY_DEVICE_COHERENT)\n--\nmm/memremap.c=144=static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,\n--\nmm/memremap.c-146-{\nmm/memremap.c:147:\tconst bool is_private = pgmap-\u003etype == MEMORY_DEVICE_PRIVATE;\nmm/memremap.c-148-\tstruct range *range = \u0026pgmap-\u003eranges[range_id];\n--\nmm/memremap.c-244-\t\t\t\tPHYS_PFN(range_len(range)), pgmap);\nmm/memremap.c:245:\tif (pgmap-\u003etype != MEMORY_DEVICE_PRIVATE \u0026\u0026\nmm/memremap.c-246-\t pgmap-\u003etype != MEMORY_DEVICE_COHERENT)\n--\nmm/memremap.c=266=void *memremap_pages(struct dev_pagemap *pgmap, int nid)\n--\nmm/memremap.c-282-\tswitch (pgmap-\u003etype) {\nmm/memremap.c:283:\tcase MEMORY_DEVICE_PRIVATE:\nmm/memremap.c:284:\t\tif (!IS_ENABLED(CONFIG_DEVICE_PRIVATE)) {\nmm/memremap.c-285-\t\t\tWARN(1, \"Device private memory not supported\\n\");\n--\nmm/memremap.c=429=void free_zone_device_folio(struct folio *folio)\n--\nmm/memremap.c-465-\tswitch (pgmap-\u003etype) {\nmm/memremap.c:466:\tcase MEMORY_DEVICE_PRIVATE:\nmm/memremap.c-467-\tcase MEMORY_DEVICE_COHERENT:\n--\nmm/migrate_device.c=137=static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,\n--\nmm/migrate_device.c-170-\t\tif (!softleaf_is_device_private(entry) ||\nmm/migrate_device.c:171:\t\t !(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_DEVICE_PRIVATE)) {\nmm/migrate_device.c-172-\t\t\tspin_unlock(ptl);\n--\nmm/migrate_device.c=245=static int migrate_vma_collect_pmd(pmd_t *pmdp,\n--\nmm/migrate_device.c-305-\t\t\tif (!(migrate-\u003eflags \u0026\nmm/migrate_device.c:306:\t\t\t\tMIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||\nmm/migrate_device.c-307-\t\t\t pgmap-\u003eowner != migrate-\u003epgmap_owner)\n--\nmm/mm_init.c=1004=static void __ref __init_zone_device_page(struct page *page, unsigned long pfn,\n--\nmm/mm_init.c-1034-\tcase MEMORY_DEVICE_FS_DAX:\nmm/mm_init.c:1035:\tcase MEMORY_DEVICE_PRIVATE:\nmm/mm_init.c-1036-\tcase MEMORY_DEVICE_COHERENT:\n--\nmm/rmap.c=2797=void try_to_migrate(struct folio *folio, enum ttu_flags flags)\n--\nmm/rmap.c-2834-\nmm/rmap.c:2835:#ifdef CONFIG_DEVICE_PRIVATE\nmm/rmap.c-2836-/**\n"
} |
| 23/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48242 output: 33 thoughts: 54 Thoughts: Okay, let's dive into this code. **Investigating `do_huge_pmd_device_private`** Alright, I need to understand this `do_huge_pmd_device_private` function. The first thing I'm going to check is the definition of `CONFIG_DEVICE_PRIVATE`. Is this macro enabled? That's the gatekeeper. I need to know under what circumstances this function is even compiled into the kernel. Where is it used, I need to understand that. I'll need to locate `mm/huge_memory.c`, and specifically that function, which means knowing that the source code can be found at `mm/huge_memory.c`. I'll want to see what conditions control the execution of `do_huge_pmd_device_private`. Are there other macros? Are there checks on the calling context? What is the logic flow around it? Basically, what are the guards around its invocation, and what assumptions does it make about the system? That's where I need to start. |
| 24/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "do_huge_pmd_device_private",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/huge_memory.c=1419=static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf)\n--\nmm/huge_memory.c-1470-\nmm/huge_memory.c:1471:vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)\nmm/huge_memory.c-1472-{\n--\nmm/memory.c=6681=static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,\n--\nmm/memory.c-6755-\t\tif (pmd_is_device_private_entry(vmf.orig_pmd))\nmm/memory.c:6756:\t\t\treturn do_huge_pmd_device_private(\u0026vmf);\nmm/memory.c-6757-\n"
} |
| 25/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48785 output: 31 thoughts: 28 Thoughts: Here's a breakdown of my thought process, as if I were analyzing that function: **Diving into `do_huge_pmd_device_private`** Alright, let's dissect `do_huge_pmd_device_private` within `mm/huge_memory.c`. I need to figure out precisely what this function *does* with respect to huge pages and device-private memory. Given the name, it's pretty clear it's involved with handling the intricacies of allocating and managing huge pages when a device is involved, but I'm looking for the specifics. I'm especially interested in how it interacts with the broader huge page infrastructure and the device driver layers. I'll be keying in on things like: * **Page allocation:** Is it allocating actual physical pages? How? Does it use the standard huge page allocators, or are there special device-specific allocation methods in play? * **Mapping:** How are these huge pages mapped into userspace (or kernel space)? Is there a special device-specific mapping strategy? Are we dealing with IOMMU complexities? I'll need to figure out how it ensures proper address translation. * **Protection:** What are the protection flags set on these huge pages? Are they read-only, read-write, execute-only? Does it handle page table permissions appropriately for device access? I will check whether it is interacting with the page table and the TLB properly. * **Synchronization:** Are there any locking mechanisms involved? Huge page operations are often complex and require careful synchronization to avoid races, so I will need to look for any spinlocks or mutexes. * **Error handling:** Is there any error handling? How does it deal with allocation failures or other issues? This part of the code has to be robust. * **Interaction with device drivers:** This function must be interfacing with the device driver. How does this interaction play out? Does this function call driver-provided callbacks or functions? * **Context:** What context is this function called in? Is it called from a user process context or from the kernel? Is this interruptable? This will significantly affect how the code should function. By focusing on these points, I should be able to get a pretty comprehensive picture of its role in the system. I'll need to remember to trace the callers and callees of this function to understand its broader place in the huge page/device memory landscape. Let's dig in. |
| 26/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "CONFIG_.*",
"PathPrefixes": [
"mm/huge_memory.c"
]
}Results: {
"Output": "mm/huge_memory.c=61=unsigned long transparent_hugepage_flags __read_mostly =\nmm/huge_memory.c:62:#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS\nmm/huge_memory.c-63-\t(1\u003c\u003cTRANSPARENT_HUGEPAGE_FLAG)|\nmm/huge_memory.c-64-#endif\nmm/huge_memory.c:65:#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE\nmm/huge_memory.c-66-\t(1\u003c\u003cTRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|\n--\nmm/huge_memory.c=83=unsigned long huge_zero_pfn __read_mostly = HUGE_ZERO_UNSET_PFN;\nmm/huge_memory.c:84:#ifndef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO\nmm/huge_memory.c-85-static atomic_t huge_zero_refcount;\n--\nmm/huge_memory.c=267=static struct folio *alloc_huge_zero_folio(void)\n--\nmm/huge_memory.c-281-\nmm/huge_memory.c:282:#ifdef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO\nmm/huge_memory.c-283-static int __init huge_zero_init(void)\n--\nmm/huge_memory.c=410=void mm_put_huge_zero_folio(struct mm_struct *mm)\n--\nmm/huge_memory.c-414-}\nmm/huge_memory.c:415:#endif /* CONFIG_PERSISTENT_HUGE_ZERO_FOLIO */\nmm/huge_memory.c-416-\nmm/huge_memory.c:417:#ifdef CONFIG_SYSFS\nmm/huge_memory.c-418-static ssize_t enabled_show(struct kobject *kobj,\n--\nmm/huge_memory.c=655=static struct attribute *hugepage_attr[] = {\n--\nmm/huge_memory.c-659-\t\u0026hpage_pmd_size_attr.attr,\nmm/huge_memory.c:660:#ifdef CONFIG_SHMEM\nmm/huge_memory.c-661-\t\u0026shmem_enabled_attr.attr,\n--\nmm/huge_memory.c=756=static struct attribute *file_ctrl_attrs[] = {\nmm/huge_memory.c:757:#ifdef CONFIG_SHMEM\nmm/huge_memory.c-758-\t\u0026thpsize_shmem_enabled_attr.attr,\n--\nmm/huge_memory.c=816=DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);\nmm/huge_memory.c:817:#ifdef CONFIG_SHMEM\nmm/huge_memory.c-818-DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);\n--\nmm/huge_memory.c=832=static struct attribute *anon_stats_attrs[] = {\n--\nmm/huge_memory.c-835-\t\u0026anon_fault_fallback_charge_attr.attr,\nmm/huge_memory.c:836:#ifndef CONFIG_SHMEM\nmm/huge_memory.c-837-\t\u0026zswpout_attr.attr,\n--\nmm/huge_memory.c=858=static struct attribute *file_stats_attrs[] = {\nmm/huge_memory.c:859:#ifdef CONFIG_SHMEM\nmm/huge_memory.c-860-\t\u0026shmem_alloc_attr.attr,\n--\nmm/huge_memory.c=872=static struct attribute *any_stats_attrs[] = {\nmm/huge_memory.c:873:#ifdef CONFIG_SHMEM\nmm/huge_memory.c-874-\t\u0026zswpout_attr.attr,\n--\nmm/huge_memory.c=1046=static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)\n--\nmm/huge_memory.c-1048-}\nmm/huge_memory.c:1049:#endif /* CONFIG_SYSFS */\nmm/huge_memory.c-1050-\n--\nmm/huge_memory.c=1274=static unsigned long __thp_get_unmapped_area(struct file *filp,\n--\nmm/huge_memory.c-1282-\nmm/huge_memory.c:1283:\tif (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall())\nmm/huge_memory.c-1284-\t\treturn 0;\n--\nmm/huge_memory.c=1748=EXPORT_SYMBOL_GPL(vmf_insert_folio_pmd);\nmm/huge_memory.c-1749-\nmm/huge_memory.c:1750:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-1751-static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)\n--\nmm/huge_memory.c=1864=EXPORT_SYMBOL_GPL(vmf_insert_folio_pud);\nmm/huge_memory.c:1865:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-1866-\n--\nmm/huge_memory.c=1949=int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,\n--\nmm/huge_memory.c-2060-\nmm/huge_memory.c:2061:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-2062-void touch_pud(struct vm_area_struct *vma, unsigned long addr,\n--\nmm/huge_memory.c=2121=void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)\n--\nmm/huge_memory.c-2132-}\nmm/huge_memory.c:2133:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-2134-\n--\nmm/huge_memory.c=2734=int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c-2841- */\nmm/huge_memory.c:2842:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-2843-int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c-2881-\nmm/huge_memory.c:2882:#ifdef CONFIG_USERFAULTFD\nmm/huge_memory.c-2883-/*\n--\nmm/huge_memory.c=2890=int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,\n--\nmm/huge_memory.c-3007-}\nmm/huge_memory.c:3008:#endif /* CONFIG_USERFAULTFD */\nmm/huge_memory.c-3009-\n--\nmm/huge_memory.c=3033=spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)\n--\nmm/huge_memory.c-3043-\nmm/huge_memory.c:3044:#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/huge_memory.c-3045-int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,\n--\nmm/huge_memory.c=3132=void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,\n--\nmm/huge_memory.c-3135-}\nmm/huge_memory.c:3136:#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */\nmm/huge_memory.c-3137-\n--\nmm/huge_memory.c=3506=static bool pud_split_required(const struct vm_area_struct *vma,\n--\nmm/huge_memory.c-3508-{\nmm/huge_memory.c:3509:\tif (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))\nmm/huge_memory.c-3510-\t\treturn false;\n--\nmm/huge_memory.c=3749=static void __split_folio_to_order(struct folio *folio, int old_order,\n--\nmm/huge_memory.c-3801-\t\t\t\t (1L \u003c\u003c PG_unevictable) |\nmm/huge_memory.c:3802:#ifdef CONFIG_ARCH_USES_PG_ARCH_2\nmm/huge_memory.c-3803-\t\t\t\t (1L \u003c\u003c PG_arch_2) |\nmm/huge_memory.c-3804-#endif\nmm/huge_memory.c:3805:#ifdef CONFIG_ARCH_USES_PG_ARCH_3\nmm/huge_memory.c-3806-\t\t\t\t (1L \u003c\u003c PG_arch_3) |\n--\nmm/huge_memory.c-3855-\t\t\tfolio_set_idle(new_folio);\nmm/huge_memory.c:3856:#ifdef CONFIG_MEMCG\nmm/huge_memory.c-3857-\t\tnew_folio-\u003ememcg_data = folio-\u003ememcg_data;\n--\nmm/huge_memory.c=4713=static unsigned long deferred_split_scan(struct shrinker *shrink,\n--\nmm/huge_memory.c-4786-\nmm/huge_memory.c:4787:#ifdef CONFIG_DEBUG_FS\nmm/huge_memory.c-4788-static void split_huge_pages_all(void)\n--\nmm/huge_memory.c=5125=late_initcall(split_huge_pages_debugfs);\n--\nmm/huge_memory.c-5127-\nmm/huge_memory.c:5128:#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES\nmm/huge_memory.c-5129-int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,\n"
} |
| 27/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52102 output: 28 thoughts: 42 Thoughts: Okay, let's break this down. **Analyzing the Compilation Dependencies of `huge_memory.c`** My immediate thought is, "Right, `huge_memory.c` is only brought into the build if `CONFIG_TRANSPARENT_HUGEPAGE` is set." That's the first thing to confirm. I need to make sure I understand the conditional compilation. Since I'm thinking about how the system builds this, I'm going to quickly check `mm/Makefile` to confirm that dependency. I need to verify that `CONFIG_TRANSPARENT_HUGEPAGE` is the sole conditional that dictates the inclusion of `huge_memory.c`. No sense in wasting time down a path that isn't actually relevant to the build context. |
| 28/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "huge_memory\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=101=obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o\nmm/Makefile:102:obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o\nmm/Makefile-103-obj-$(CONFIG_PAGE_COUNTER) += page_counter.o\n"
} |
| 29/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52433 output: 27 thoughts: 73 Thoughts: Here's my take on those thoughts: **Analyzing Kernel Configuration Dependencies** Okay, so I'm looking at this build configuration and seeing that `huge_memory.o` and `khugepaged.o` are both going to be included if `CONFIG_TRANSPARENT_HUGEPAGE` is enabled. Makes perfect sense; those are the core components for transparent huge pages, so they're naturally linked. Now, the next question that pops into my head is, what about the other THP-related code? Specifically, I need to check how `mm/ksm.c` is handled in this scenario. It's important to make sure all related modules are correctly configured based on the same prerequisites. |
| 30/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "ksm\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=90=obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o\nmm/Makefile:91:obj-$(CONFIG_KSM) += ksm.o\nmm/Makefile-92-obj-$(CONFIG_PAGE_POISONING) += page_poison.o\n"
} |
| 31/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52770 output: 27 thoughts: 19 Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, presented in the first person: **Kernel Memory Management Considerations** Right, so I'm thinking about the kernel memory management landscape, and the first thing that pops into my head is `CONFIG_KSM`. Always have to consider KSM's impact, especially in heavily virtualized environments. Is it on, is it off, how aggressive is it? The implications for memory deduplication are huge, and I need to keep that in mind when assessing overall performance and potential bottlenecks. But then, as a more granular follow-up, my mind jumps to `mm/cma.c`. Specifically, how's CMA (Contiguous Memory Allocator) behaving? What's the current state of its allocations, and how much fragmentation are we seeing? Does the configuration affect how it handles direct-mapped memory? Are the allocations successful, and if they are are they being done efficiently? These are all key concerns. I need to understand how CMA is interacting with the system's overall memory pressure and whether its configuration is optimized for the intended workload, including considerations for NUMA architectures. |
| 32/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "cma\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=83=ifdef CONFIG_CMA\nmm/Makefile:84:obj-$(CONFIG_HUGETLBFS)\t+= hugetlb_cma.o\nmm/Makefile-85-endif\n--\nmm/Makefile=121=obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o\nmm/Makefile:122:obj-$(CONFIG_CMA)\t+= cma.o\nmm/Makefile-123-obj-$(CONFIG_NUMA) += numa.o\n"
} |
| 33/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53197 output: 28 thoughts: 18 Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, presented in the first person: **Kernel Memory Management – A Quick Check** Right, let me quickly run through this. I see `CONFIG_CMA`, which immediately makes me think about contiguous memory allocation and its implications for the system. I wonder how it's being configured, if there are any current constraints, what the overall allocation strategy is. Does this have any relationship to current performance problems or optimizations I'm looking at? And then, " `mm/hugetlb.c` ". That's a direct prompt for me to shift gears into HugeTLB land. I need to quickly recall which systems are using hugetlbfs, what the current settings are. Are we experiencing any fragmentation issues, or perhaps contention when huge pages need to be allocated, or reclaimed. It immediately makes me think about potential bottlenecks. Is this relevant to the current work at hand? I need to check the current workload that's running and the relationship between this and the work at hand. |
| 34/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "hugetlb\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=81=obj-$(CONFIG_HAS_DMA)\t+= dmapool.o\nmm/Makefile:82:obj-$(CONFIG_HUGETLBFS)\t+= hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o\nmm/Makefile-83-ifdef CONFIG_CMA\n"
} |
| 35/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53555 output: 28 thoughts: 20 Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, presented in the first person: **Kernel Configuration and Memory Management** Alright, let's see... *CONFIG_HUGETLBFS*... that's a direct prompt for me to consider huge pages and their filesystem implementation. It suggests I'm thinking about large memory allocations and whether I'm working with a system that leverages huge page support for performance gains. I'm likely weighing the benefits of reduced TLB misses and potentially exploring their use in a specific context. I may be looking at configuration options or a codebase related to a system that uses them. And *mm/userfaultfd.c*... Ah, that's interesting. The mention of `mm/userfaultfd.c` immediately calls to mind the userfaultfd mechanism. I'm probably considering its role in handling page faults within userspace, allowing for sophisticated memory management strategies implemented in user space. I may be thinking about the implications of integrating userfaultfd with huge pages, or perhaps optimizing its interaction with other memory management subsystems. It could be related to advanced memory management techniques. I am likely exploring potential applications or problems in a project I am working on. Are these related somehow? |
| 36/2 | 2026/10/27 03:20 | tool | grepper |
0mArgs:{
"Expression": "userfaultfd\\.o",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=132=obj-$(CONFIG_CMA_SYSFS) += cma_sysfs.o\nmm/Makefile:133:obj-$(CONFIG_USERFAULTFD) += userfaultfd.o\nmm/Makefile-134-obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o\n"
} |
| 37/2 | 2026/10/27 03:20 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53954 output: 36 thoughts: 3775 Thoughts: **Memory Management Fixes: A Deep Dive** Okay, so I've got a lot to unpack here. It seems like the core focus is on enhancing and stabilizing a handful of memory management features, and I need to figure out which are the most impactful changes and how to effectively target them for fuzzing. I'm seeing a lot of work related to `USERFAULTFD`, `MMU_NOTIFIER`, `MEMFD_CREATE`, `NUMA`, and `ZONE_DEVICE`, which probably correspond to the `CONFIG` options I saw at the top. Let's make some sense of all these scattered files and the changes within. First, that `fs/proc/task_mmu.c` patch. It appears to be sanitizing input parameters to `pagemap_scan`. I was initially thrown by the `do_pagemap_scan` function but it is the `ioctl` via `/proc/[pid]/pagemap` that is the entrypoint so it is reachable and likely a good focus symbol. Next, `include/asm-generic/tlb.h` is a simple fix. Increasing `range_size` to `unsigned long` in `__tlb_adjust_range` indicates a concern with >4GB TLB operations, which could be interesting for fuzzing. Diving into `mm/huge_memory.c`, things get more involved. There's a flurry of activity around `copy_huge_pud`, `zap_huge_pmd_folio`, `move_huge_pmd`, and `split_huge_leaf`. I see a lot of locking and folio manipulation. This seems like a core area of change, and I need to understand the implications of these changes, especially related to COW handling, and deferred splitting, and how it impacts how we handle large pages and large page faults. It could be significant. In `mm/ksm.c` I'm seeing changes to `ksm_might_need_to_copy`, especially with the addition of folio assertions and vma checks. Also, `merge_across_nodes_store` is setting `ksm_nr_node_ids` under a lock. This feels like a good spot to probe for concurrency issues in KSM. The checks seem important. It may be a good focus to track. `mm/memremap.c` and associated files appear to be refining the interaction with device page mappings. `pfn_has_dev_pagemap` and related changes look like they’re streamlining page table management when dealing with devices and hotplug. `mm/mmu_notifier.c` has changes to the invalidation range. It is less clear if this is good for fuzzing. Then we see a bunch of seemingly minor changes like the `FMODE_ATOMIC_POS` flag, locking in `dup_mm` and the CMA changes. These are probably not good for fuzzing, unless the `cma_release` leaks pages. `mm/filemap.c` is more interesting, especially the changes around `filemap_remove_folio`, `folio_wait_bit_common` and `lock_folio_maybe_drop_mmap`. I need to figure out how these fit into the whole picture of file system page cache interaction. `filemap_fault` is good too. `mm/gup.c` seems to be hardening the GUP (Get User Page) path. The checks in `follow_pud_mask` and `gup_fast_fallback`, and the `memfd_pin_folios` zeroing and uptodate markings are notable, but potentially low value. `mm/hugetlb.c` with the `move_huge_pte` changes are interesting, along with `mm/internal.h` and the double spinlock changes. It is worth investigating if this is a good focus symbol. `mm/madvise.c` looks like we have a KSM folio skip, which would be a bad focus symbol. `mm/memory-tiers.c` defers the `device_unregister` and this sounds ok. `mm/memory.c` has a variety of fixes. The `__wp_page_copy_user`, `wp_page_copy` could be important. `mm/mremap.c` is extensive, with changes to `get_old_pud`, `move_ptes`, and several `move_` functions. It appears to focus on how we handle page table entries during remapping, with lock ordering changes. This is probably a good area to focus on. `mm/rmap.c` is less clear, but checking `pte_present` is interesting. `mm/swapfile.c` with `ksm_might_need_ |
| 38/2 | 2026/10/27 03:21 | tool | grepper |
0mArgs:{
"Expression": "pagemap_operations",
"PathPrefixes": [
"fs/proc/task_mmu.c"
]
}Results: {
"Output": "fs/proc/task_mmu.c=3200=static long do_pagemap_cmd(struct file *file, unsigned int cmd,\n--\nfs/proc/task_mmu.c-3213-\nfs/proc/task_mmu.c:3214:const struct file_operations proc_pagemap_operations = {\nfs/proc/task_mmu.c-3215-\t.llseek\t\t= mem_lseek, /* borrow this */\n"
} |
| 39/2 | 2026/10/27 03:21 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 58920 output: 39 thoughts: 28 Thoughts: Okay, let's dive into this. I'm going to pull up lines 3200-3220 of `fs/proc/task_mmu.c`. Right, I remember this section. I suspect we're looking at something related to memory management, likely within the context of a specific process. Given the file and the likely line range, I'm expecting to find something handling virtual memory accounting, perhaps related to page faults, memory statistics reporting, or even resource limits enforcement at the task level. My intuition says this might be where they're tracking how much memory a process is actually using, perhaps differentiating between resident set size, virtual memory size, and swap usage. Could be a critical section of code for `procfs` memory information updates. Let's get to it. |
| 40/2 | 2026/10/27 03:21 | tool | read-file |
0mArgs:{
"File": "fs/proc/task_mmu.c",
"FirstLine": 3200,
"LineCount": 25
}Results: {
"Contents": "3200:\tstatic long do_pagemap_cmd(struct file *file, unsigned int cmd,\n3201:\t\t\t\t unsigned long arg)\n3202:\t{\n3203:\t\tstruct mm_struct *mm = file-\u003eprivate_data;\n3204:\t\n3205:\t\tswitch (cmd) {\n3206:\t\tcase PAGEMAP_SCAN:\n3207:\t\t\treturn do_pagemap_scan(mm, arg);\n3208:\t\n3209:\t\tdefault:\n3210:\t\t\treturn -EINVAL;\n3211:\t\t}\n3212:\t}\n3213:\t\n3214:\tconst struct file_operations proc_pagemap_operations = {\n3215:\t\t.llseek\t\t= mem_lseek, /* borrow this */\n3216:\t\t.read\t\t= pagemap_read,\n3217:\t\t.open\t\t= pagemap_open,\n3218:\t\t.release\t= pagemap_release,\n3219:\t\t.unlocked_ioctl = do_pagemap_cmd,\n3220:\t\t.compat_ioctl\t= do_pagemap_cmd,\n3221:\t};\n3222:\t#endif /* CONFIG_PROC_PAGE_MONITOR */\n3223:\t\n3224:\t#ifdef CONFIG_NUMA\n"
} |
| 41/2 | 2026/10/27 03:21 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 59518 output: 36 thoughts: 595 Thoughts: **Analyzing Static Functions and Symbol Visibility in the Kernel** Okay, so I'm looking at the `do_pagemap_cmd` function, which handles the ioctl for `/proc/[pid]/pagemap`. I'm also noticing that `pagemap_scan_get_args`, `do_pagemap_scan`, and `do_pagemap_cmd` are all declared static. This makes me consider how static functions are handled with symbol resolution in vmlinux, especially when it comes to tools like mine that use symbol coverage filters. The documentation states that compiler-inlined static functions (those in .h files like `mm/*.h` or `include/linux/*.h`) won't have distinct symbol addresses in the vmlinux image. My tool can't target these directly. But static functions declared in .c files often *do* have unique symbol names. And, of course, exported or global non-static symbols are always visible. This is where I can make use of feature-specific non-static callers that exercise those static inline helpers. So, I need to check the patch for usable symbols. I'll look for functions that are non-static and feature-specific. Here's what I've found that are relevant: 1. `copy_huge_pud`: This function is in `mm/huge_memory.c` and declared in `include/linux/huge_mm.h`. Good, it's a global symbol. 2. `vma_adjust_trans_huge`: Found in `mm/huge_memory.c`. Another global symbol! And it calls `split_huge_leaf`, which has seen some changes. 3. `move_huge_pmd`: Also in `mm/huge_memory.c`. Again, global! It's called from `mremap`, which makes it likely a good target. 4. `try_collapse_pte_mapped_thp`: From `mm/khugepaged.c`. I will check whether `khugepaged_collapse_pte_mapped_thp` or `collapse_pte_mapped_thp` are the callers. These are potential targets for analysis, since they *are* visible symbols and exercise static helper functions that aren't directly addressable. |
| 42/2 | 2026/10/27 03:21 | tool | grepper |
0mArgs:{
"Expression": "collapse_pte_mapped_thp",
"PathPrefixes": [
"mm/",
"include/"
]
}Results: {
"Output": "include/linux/khugepaged.h=19=extern bool current_is_khugepaged(void);\ninclude/linux/khugepaged.h:20:void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,\ninclude/linux/khugepaged.h-21-\t\tbool install_pmd);\n--\ninclude/linux/khugepaged.h=41=static inline void khugepaged_enter_vma(struct vm_area_struct *vma,\n--\ninclude/linux/khugepaged.h-44-}\ninclude/linux/khugepaged.h:45:static inline void collapse_pte_mapped_thp(struct mm_struct *mm,\ninclude/linux/khugepaged.h-46-\t\tunsigned long addr, bool install_pmd)\n--\nmm/khugepaged.c=1836=static bool pte_table_empty(pte_t *pte)\n--\nmm/khugepaged.c-1846-\nmm/khugepaged.c:1847:static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,\nmm/khugepaged.c-1848-\t\tbool install_pmd)\n--\nmm/khugepaged.c-2060-/**\nmm/khugepaged.c:2061: * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at\nmm/khugepaged.c-2062- * address haddr.\n--\nmm/khugepaged.c-2071- */\nmm/khugepaged.c:2072:void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,\nmm/khugepaged.c-2073-\t\tbool install_pmd)\nmm/khugepaged.c-2074-{\nmm/khugepaged.c:2075:\ttry_collapse_pte_mapped_thp(mm, addr, install_pmd);\nmm/khugepaged.c-2076-}\n--\nmm/khugepaged.c=2810=static enum scan_result collapse_pmd(struct mm_struct *mm,\n--\nmm/khugepaged.c-2851-\t\telse\nmm/khugepaged.c:2852:\t\t\tresult = try_collapse_pte_mapped_thp(mm, addr,\nmm/khugepaged.c-2853-\t\t\t\t\t\t\tcc-\u003epolicy.file_install_pmd);\n"
} |
| 43/2 | 2026/10/27 03:21 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 61305 output: 35 thoughts: 208 Thoughts: **My Initial Assessment of Potential Kernel Vulnerabilities** Okay, so I'm looking at these functions as potential areas of interest. The first two that jump out at me are `collapse_pte_mapped_thp` and `try_collapse_pte_mapped_thp`. Both of these are within `mm/khugepaged.c`, the heart of transparent hugepages, and the static function `try_collapse_pte_mapped_thp` is the worker function, so it's probably where the heavy lifting happens. They're both global symbols, making them easily accessible. Good candidates to scrutinize. Next, I see `ksm_might_need_to_copy`. This one's in `mm/ksm.c`, and it's declared in `include/linux/ksm.h`. It's a global symbol, too. The `struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page, struct vm_area_struct *vma, unsigned long addr)` signature already gives me a good idea what's going on, and KSM is always a place that I pay attention to. Finally, I'm noting `userfaultfd_register_range` and `userfaultfd_unregister`. These are in `mm/userfaultfd.c`. Specifically, `userfaultfd_unregister` is called by `userfaultfd_ioctl` when handling UFFDIO_UNREGISTER. I'll definitely want to see how the registration happens, so I'll be checking `userfaultfd_register` too. |
| 44/2 | 2026/10/27 03:21 | tool | grepper |
0mArgs:{
"Expression": "userfaultfd_register_range",
"PathPrefixes": [
"mm/userfaultfd.c"
]
}Results: {
"Output": "mm/userfaultfd.c=2246=static void wake_userfault_range(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-2249-/* Assumes mmap write lock taken, and mm_struct pinned. */\nmm/userfaultfd.c:2250:static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,\nmm/userfaultfd.c-2251-\t\t\t struct vm_area_struct *vma,\n--\nmm/userfaultfd.c=3671=static int userfaultfd_register(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-3846-\nmm/userfaultfd.c:3847:\tret = userfaultfd_register_range(ctx, vma, vm_flags, start, end,\nmm/userfaultfd.c-3848-\t\t\t\t\t wp_async);\n"
} |
| 45/2 | 2026/10/27 03:21 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 62133 output: 28 thoughts: 573 Thoughts: **My Kernel Internals Investigation** Okay, so I'm diving into some kernel code, and here's what's catching my eye. First off, I'm noting that `userfaultfd_register` and `userfaultfd_unregister` are the `ioctl` handlers for `UFFDIO_REGISTER` and `UFFDIO_UNREGISTER` – that's fundamental for userfaultfd. Then I'm seeing a global symbol, `memfd_pin_folios`, in `mm/gup.c`. Its signature is `long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, struct folio **folios, unsigned int max_folios, pgoff_t *offset)`. This is clearly related to pinning memory within a `memfd` and looks like an essential part of page fault handling, which is something I need to keep in mind. Next, there's `pfn_has_dev_pagemap` in `mm/memremap.c`. Its global, so it looks like it can determine whether a given PFN has a device pagemap. This makes sense for managing memory that might be backed by a device, which could be relevant for hot-plugging, and is used to remap or modify memory regions, which might be helpful. I'm seeing `move_hugetlb_page_tables` in `mm/hugetlb.c`, which calls `move_huge_pte`. That's self-explanatory. And similarly `move_page_tables` in `mm/mremap.c` that's exercising functions like `move_ptes`, `move_normal_pmd`, `move_normal_pud`, `move_huge_pud`. Clearly, these are all involved in manipulating page tables, which is very useful for investigating memory migration/management features, which is essential to my research. `unmap_mapping_range` in `mm/memory.c` is also a global symbol, and its signature is `void unmap_mapping_range(struct address_space *mapping, loff_t const holebegin, loff_t const holelen, int even_cows)`. That's used to unmap a region, which is another area of interest for memory operations. `cma_release` in `mm/cma.c`, and its signature `bool cma_release(struct cma *cma, const struct page *pages, unsigned long count)`, looks like a function for releasing CMA (Contiguous Memory Allocator) memory. The way the CMA is implemented can have a significant effect on performance and usability. I'm also noting `madvise_free_pte_range` called during `madvise(..., MADV_FREE)`, which is used for de-allocating memory, and it looks like a low-level operation, possibly for freeing up page table entries during `MADV_FREE`. This will give useful insight into the implementation of memory reclaim. Then there's `truncate_inode_pages_range`, a global symbol in `mm/truncate.c`, which must be for truncating file pages. It would be useful for finding the effect of file truncation on the memory. Also, `file_inode_is_shmem` in `mm/shmem.c` is a global, so probably checks if a file is backed by shared memory. Now, I'm seeing a reference to `memfd_luo.c`. That's new, and I don't recall that file. So I need to go and check what `memfd_luo.c` is all about. What's this Live Update? Time to dive into the code! |
| 46/2 | 2026/10/27 03:21 | tool | grepper |
0mArgs:{
"Expression": "memfd_luo",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Makefile=103=obj-$(CONFIG_PAGE_COUNTER) += page_counter.o\nmm/Makefile:104:obj-$(CONFIG_LIVEUPDATE_MEMFD) += memfd_luo.o\nmm/Makefile-105-obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o\n--\nmm/memfd_luo.c-86-\nmm/memfd_luo.c:87:static int memfd_luo_preserve_folios(struct file *file,\nmm/memfd_luo.c-88-\t\t\t\t struct kho_vmalloc *kho_vmalloc,\nmm/memfd_luo.c:89:\t\t\t\t struct memfd_luo_folio_ser **out_folios_ser,\nmm/memfd_luo.c-90-\t\t\t\t u64 *nr_foliosp)\n--\nmm/memfd_luo.c-92-\tstruct inode *inode = file_inode(file);\nmm/memfd_luo.c:93:\tstruct memfd_luo_folio_ser *folios_ser;\nmm/memfd_luo.c-94-\tunsigned int max_folios;\n--\nmm/memfd_luo.c-148-\tfor (i = 0; i \u003c nr_folios; i++) {\nmm/memfd_luo.c:149:\t\tstruct memfd_luo_folio_ser *pfolio = \u0026folios_ser[i];\nmm/memfd_luo.c-150-\t\tstruct folio *folio = folios[i];\n--\nmm/memfd_luo.c-230-\nmm/memfd_luo.c:231:static void memfd_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc,\nmm/memfd_luo.c:232:\t\t\t\t\tstruct memfd_luo_folio_ser *folios_ser,\nmm/memfd_luo.c-233-\t\t\t\t\tu64 nr_folios)\n--\nmm/memfd_luo.c-242-\tfor (i = 0; i \u003c nr_folios; i++) {\nmm/memfd_luo.c:243:\t\tconst struct memfd_luo_folio_ser *pfolio = \u0026folios_ser[i];\nmm/memfd_luo.c-244-\t\tstruct folio *folio;\n--\nmm/memfd_luo.c-257-\nmm/memfd_luo.c:258:static int memfd_luo_preserve(struct liveupdate_file_op_args *args)\nmm/memfd_luo.c-259-{\nmm/memfd_luo.c-260-\tstruct inode *inode = file_inode(args-\u003efile);\nmm/memfd_luo.c:261:\tstruct memfd_luo_folio_ser *folios_ser;\nmm/memfd_luo.c:262:\tstruct memfd_luo_ser *ser;\nmm/memfd_luo.c-263-\tu64 nr_folios, inode_size;\n--\nmm/memfd_luo.c-302-\nmm/memfd_luo.c:303:\terr = memfd_luo_preserve_folios(args-\u003efile, \u0026ser-\u003efolios,\nmm/memfd_luo.c-304-\t\t\t\t\t\u0026folios_ser, \u0026nr_folios);\n--\nmm/memfd_luo.c-323-\nmm/memfd_luo.c:324:static int memfd_luo_freeze(struct liveupdate_file_op_args *args)\nmm/memfd_luo.c-325-{\nmm/memfd_luo.c:326:\tstruct memfd_luo_ser *ser;\nmm/memfd_luo.c-327-\n--\nmm/memfd_luo.c-341-\nmm/memfd_luo.c:342:static void memfd_luo_unpreserve(struct liveupdate_file_op_args *args)\nmm/memfd_luo.c-343-{\nmm/memfd_luo.c-344-\tstruct inode *inode = file_inode(args-\u003efile);\nmm/memfd_luo.c:345:\tstruct memfd_luo_ser *ser;\nmm/memfd_luo.c-346-\n--\nmm/memfd_luo.c-354-\nmm/memfd_luo.c:355:\tmemfd_luo_unpreserve_folios(\u0026ser-\u003efolios, args-\u003eprivate_data,\nmm/memfd_luo.c-356-\t\t\t\t ser-\u003enr_folios);\n--\nmm/memfd_luo.c-361-\nmm/memfd_luo.c:362:static void memfd_luo_discard_folios(const struct memfd_luo_folio_ser *folios_ser,\nmm/memfd_luo.c-363-\t\t\t\t u64 nr_folios)\n--\nmm/memfd_luo.c-367-\tfor (i = 0; i \u003c nr_folios; i++) {\nmm/memfd_luo.c:368:\t\tconst struct memfd_luo_folio_ser *pfolio = \u0026folios_ser[i];\nmm/memfd_luo.c-369-\t\tstruct folio *folio;\n--\nmm/memfd_luo.c-386-\nmm/memfd_luo.c:387:static void memfd_luo_finish(struct liveupdate_file_op_args *args)\nmm/memfd_luo.c-388-{\nmm/memfd_luo.c:389:\tstruct memfd_luo_folio_ser *folios_ser;\nmm/memfd_luo.c:390:\tstruct memfd_luo_ser *ser;\nmm/memfd_luo.c-391-\n--\nmm/memfd_luo.c-408-\nmm/memfd_luo.c:409:\t\tmemfd_luo_discard_folios(folios_ser, ser-\u003enr_folios);\nmm/memfd_luo.c-410-\t\tvfree(folios_ser);\n--\nmm/memfd_luo.c-416-\nmm/memfd_luo.c:417:static int memfd_luo_retrieve_folios(struct file *file,\nmm/memfd_luo.c:418:\t\t\t\t struct memfd_luo_folio_ser *folios_ser,\nmm/memfd_luo.c-419-\t\t\t\t u64 nr_folios)\n--\nmm/memfd_luo.c-428-\tfor (i = 0; i \u003c nr_folios; i++) {\nmm/memfd_luo.c:429:\t\tconst struct memfd_luo_folio_ser *pfolio = \u0026folios_ser[i];\nmm/memfd_luo.c-430-\t\tphys_addr_t phys;\n--\nmm/memfd_luo.c-500-\tfor (long j = i + 1; j \u003c nr_folios; j++) {\nmm/memfd_luo.c:501:\t\tconst struct memfd_luo_folio_ser *pfolio = \u0026folios_ser[j];\nmm/memfd_luo.c-502-\t\tphys_addr_t phys;\n--\nmm/memfd_luo.c-517-\nmm/memfd_luo.c:518:static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)\nmm/memfd_luo.c-519-{\nmm/memfd_luo.c:520:\tstruct memfd_luo_folio_ser *folios_ser;\nmm/memfd_luo.c:521:\tstruct memfd_luo_ser *ser;\nmm/memfd_luo.c-522-\tstruct file *file;\n--\nmm/memfd_luo.c-561-\nmm/memfd_luo.c:562:\t\terr = memfd_luo_retrieve_folios(file, folios_ser, ser-\u003enr_folios);\nmm/memfd_luo.c-563-\t\tvfree(folios_ser);\n--\nmm/memfd_luo.c-579-\nmm/memfd_luo.c:580:static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler,\nmm/memfd_luo.c-581-\t\t\t\t struct file *file)\n--\nmm/memfd_luo.c-587-\nmm/memfd_luo.c:588:static unsigned long memfd_luo_get_id(struct file *file)\nmm/memfd_luo.c-589-{\n--\nmm/memfd_luo.c-592-\nmm/memfd_luo.c:593:static const struct liveupdate_file_ops memfd_luo_file_ops = {\nmm/memfd_luo.c:594:\t.freeze = memfd_luo_freeze,\nmm/memfd_luo.c:595:\t.finish = memfd_luo_finish,\nmm/memfd_luo.c:596:\t.retrieve = memfd_luo_retrieve,\nmm/memfd_luo.c:597:\t.preserve = memfd_luo_preserve,\nmm/memfd_luo.c:598:\t.unpreserve = memfd_luo_unpreserve,\nmm/memfd_luo.c:599:\t.can_preserve = memfd_luo_can_preserve,\nmm/memfd_luo.c:600:\t.get_id = memfd_luo_get_id,\nmm/memfd_luo.c-601-\t.owner = THIS_MODULE,\n--\nmm/memfd_luo.c-603-\nmm/memfd_luo.c:604:static struct liveupdate_file_handler memfd_luo_handler = {\nmm/memfd_luo.c:605:\t.ops = \u0026memfd_luo_file_ops,\nmm/memfd_luo.c-606-\t.compatible = MEMFD_LUO_FH_COMPATIBLE,\n--\nmm/memfd_luo.c-608-\nmm/memfd_luo.c:609:static int __init memfd_luo_init(void)\nmm/memfd_luo.c-610-{\nmm/memfd_luo.c:611:\tint err = liveupdate_register_file_handler(\u0026memfd_luo_handler);\nmm/memfd_luo.c-612-\n--\nmm/memfd_luo.c-621-}\nmm/memfd_luo.c:622:late_initcall(memfd_luo_init);\n"
} |
| 47/2 | 2026/10/27 03:21 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 66337 output: 38 thoughts: 588 Thoughts: **My Configuration Investigation** Alright, let's get down to brass tacks. I need to figure out the relevant configurations for this `CONFIG_LIVEUPDATE_MEMFD` context. I see that it's another configuration option in the tree, and the task at hand is to identify all the necessary `CONFIG_` options to properly compile and reach the modified code. I'll stick to the instructions: Kconfig inspection and `#ifdef` guards are the name of the game, no assumptions! And remember those "depends on" lines in Kconfig to grab any non-standard parent subsystem configs. Then, strip off the `CONFIG_` prefix for the final output. First, I need a list of the key configurations. Based on the code I'm working with, I've identified the following: * `TRANSPARENT_HUGEPAGE`: Clearly important given its role in `mm/huge_memory.c`, `mm/khugepaged.c`, `copy_huge_pud`, `move_huge_pmd`, `try_collapse_pte_mapped_thp`, and the like. * `PROC_PAGE_MONITOR`: Essential for the pagemap scan in `fs/proc/task_mmu.c`. * `KSM`: Seems integral, given its use in `mm/ksm.c` and functions like `ksm_might_need_to_copy` and `merge_across_nodes_store`. * `USERFAULTFD`: Required for the userfaultfd implementation in `mm/userfaultfd.c`. * `HUGETLBFS` or `HUGETLB_PAGE`: Appears to be needed for the hugetlb functionality in `mm/hugetlb.c`. * `MEMFD_CREATE`: Makes sense, given the focus on `mm/memfd.c`. * `CMA`: I see this used in `mm/cma.c`, so I'll include it. * `ZONE_DEVICE`: Required for `mm/memremap.c` and dev_pagemap logic. * `NUMA`: Necessary for `mm/memory-tiers.c` and NUMA KSM logic. Now, let's verify these in the Kconfig files. 1. `TRANSPARENT_HUGEPAGE`: I'll check `mm/Kconfig`. Yup, there it is: `config TRANSPARENT_HUGEPAGE`, a boolean with a `depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE` dependency. Confirmed. 2. `PROC_PAGE_MONITOR`: I'll head to `fs/proc/Kconfig`. Yep, `config PROC_PAGE_MONITOR` with a default of `y` and a dependency on `PROC_FS`. Sounds good. 3. `KSM`: Back to `mm/Kconfig` for this one. Found it: `config KSM` with `depends on MMU`. Confirmed. 4. `USERFAULTFD`: I need to look in `fs/userfaultfd/Kconfig` or `init/Kconfig` for this one. Let's `grep` for `config USERFAULTFD\b`. Let me see... Alright, I found the config. Verified. Alright, that's the list of configs and the verifications complete. I'm satisfied. |
| 48/2 | 2026/10/27 03:21 | tool | grepper |
0mArgs:{
"Expression": "config USERFAULTFD$",
"PathPrefixes": [
"fs/",
"mm/",
"init/"
]
}Results: {
"Output": "mm/Kconfig=1372=config USERFAULTFD_RWP\n--\nmm/Kconfig-1380-\nmm/Kconfig:1381:menuconfig USERFAULTFD\nmm/Kconfig-1382-\tbool \"Enable userfaultfd() system call\"\n"
} |
| 49/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 67782 output: 42 thoughts: 32 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating Huge Page Configuration Options** Alright, let's dive in. I need to refresh my memory on the specifics of how `HUGETLBFS` and `HUGETLB_PAGE` are configured within the kernel. Time to check those Kconfig files – specifically, `fs/Kconfig` and `mm/Kconfig`. I know the high-level implications, the interaction with other memory management features, and the impact on application performance. But I need to pin down the details. I want to quickly verify the dependencies, defaults, and associated options. Are there any conditional configurations that might be relevant to a specific platform or use case I'm currently working on? I need to be sure I haven't missed any edge cases or new additions. A quick review of the Kconfig files will ensure I have a complete picture. Let's get to it. |
| 50/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config HUGETLBFS$|config HUGETLB_PAGE$",
"PathPrefixes": [
"fs/",
"mm/"
]
}Results: {
"Output": "fs/Kconfig=247=config ARCH_SUPPORTS_HUGETLBFS\n--\nfs/Kconfig-249-\nfs/Kconfig:250:menuconfig HUGETLBFS\nfs/Kconfig-251-\tbool \"HugeTLB file system support\"\n--\nfs/Kconfig=271=endif # HUGETLBFS\nfs/Kconfig-272-\nfs/Kconfig:273:config HUGETLB_PAGE\nfs/Kconfig-274-\tdef_bool HUGETLBFS\n"
} |
| 51/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 68197 output: 34 thoughts: 27 Thoughts: Here's my take on the thought process, as if it were my own: **Investigating Huge Pages and Memory File Descriptors** Okay, so we're dealing with `HUGETLBFS` here, which, as I recall, is the mechanism that essentially *enables* the use of `HUGETLB_PAGE`. That's the core relationship. It's the filesystem that's needed to make huge pages useful. I'm going to want to make sure the build has all the required dependencies to access large pages. Given that, I'm going to pivot and take a look at `CONFIG_MEMFD_CREATE`. It is related to anonymous shared memory and I want to confirm that is available. I'm not entirely clear where this is going, but it seems relevant to how we can allocate and manage these large memory regions if we use it. I'll need to figure out how these concepts are related in the greater scheme of how the system interacts with huge pages, specifically in my current project. |
| 52/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config MEMFD_CREATE$",
"PathPrefixes": [
"init/",
"mm/"
]
}Results: {
"Output": "mm/Kconfig=1333=config KMAP_LOCAL_NON_LINEAR_PTE_ARRAY\n--\nmm/Kconfig-1335-\nmm/Kconfig:1336:config MEMFD_CREATE\nmm/Kconfig-1337-\tbool \"Enable memfd_create() system call\" if EXPERT\n"
} |
| 53/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 68566 output: 28 thoughts: 9 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person: **Initial Assessment: ZONE_DEVICE Verification** Alright, my first thought here is to immediately focus on the `ZONE_DEVICE` element. Given my expertise, this likely represents a crucial component within whatever system or process I'm working with. This could be anything from a hardware configuration to a logical grouping within a software architecture. The implication is that something hinges on the status or behavior of this particular entity. I need to understand its current state. I need to confirm its operational status, or that its configuration adheres to the expected parameters. Essentially, I need to know if it's behaving as expected. |
| 54/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config ZONE_DEVICE$",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Kconfig=1219=config ZONE_DMA32\n--\nmm/Kconfig-1223-\nmm/Kconfig:1224:config ZONE_DEVICE\nmm/Kconfig-1225-\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n"
} |
| 55/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 68839 output: 26 thoughts: 8 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person: **Initial Assessment: CMA Check** Alright, the initial prompt seems to be pointing me towards an analysis or examination. My immediate thought is, "Let's check `CMA`". That's my shorthand, my internal notation. Depending on the context, `CMA` could represent a vast range of things – a specific codebase, a particular dataset, a command-line utility, a regulatory framework like the Competition and Markets Authority, or even a proprietary internal system I'm familiar with. Whatever it is, that's where my focus needs to be directed first. It's the most likely starting point, the essential preliminary step before I can even begin to formulate a comprehensive response or solution. I'm going to quickly parse what `CMA` represents in this specific circumstance and then build from there. |
| 56/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config CMA$",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Kconfig=1052=config HAVE_SETUP_PER_CPU_AREA\n--\nmm/Kconfig-1054-\nmm/Kconfig:1055:config CMA\nmm/Kconfig-1056-\tbool \"Contiguous Memory Allocator\"\n"
} |
| 57/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69146 output: 36 thoughts: 8 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person: **Evaluating NUMA** Right, let's dive in. The core thought here is pretty straightforward: my immediate next step is to examine the implications of `NUMA` (Non-Uniform Memory Access). Given my expertise, I'm already implicitly aware of the potential performance bottlenecks, the memory affinity considerations, and the general complexities that NUMA architecture introduces in modern systems. This "check" implies a deeper dive; I'm not just *thinking* about NUMA, but likely formulating a specific question, running a mental checklist of related issues, or preparing to analyze some data that relates to it. My "check" is more than a casual glance. It's an experienced professional quickly assessing how NUMA factors into the overall problem at hand, its potential impacts, and how to best address those impacts within the constraints of the system. I will not merely check NUMA, but consider NUMA's implications. |
| 58/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config NUMA$",
"PathPrefixes": [
"mm/",
"arch/x86/"
]
}Results: {
"Output": "arch/x86/Kconfig=1489=config AMD_MEM_ENCRYPT\n--\narch/x86/Kconfig-1505-# Common NUMA Features\narch/x86/Kconfig:1506:config NUMA\narch/x86/Kconfig-1507-\tbool \"NUMA Memory Allocation and Scheduler Support\"\n"
} |
| 59/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69504 output: 28 thoughts: 11 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and rendered in the first person: **Analyzing the Configuration - A Quick Scan** Alright, before diving too deep into this problem, I need to do a quick sanity check. The issue likely hinges on device configuration, so my initial thought is, "Let's check `CONFIG_DEVICE_PRIVATE`". This particular configuration option is usually a good indicator of some underlying hardware setup, so it's a logical starting point to see what its current state is, if it's even configured, and what that might imply about the module's behavior. I want to see if this option reveals any immediate clues about the root cause of the behavior I'm witnessing. It’s the usual process to quickly establish a baseline before any deeper diagnosis is attempted, right? |
| 60/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "config DEVICE_PRIVATE$",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/Kconfig=1252=config GET_FREE_REGION\n--\nmm/Kconfig-1254-\nmm/Kconfig:1255:config DEVICE_PRIVATE\nmm/Kconfig-1256-\tbool \"Unaddressable device memory (GPU memory, ...)\"\n"
} |
| 61/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69801 output: 33 thoughts: 1935 Thoughts: **Focus Symbol Selection for Fuzzing** Okay, time to carefully consider which kernel functions from the recent changes are the best candidates for `FocusSymbols`, following Section 4's guidelines. The goal is to identify functions that are specific to the new logic or entry points for the modified subsystem features, *not* ubiquitous functions like memory allocators, page fault handlers, or synchronization primitives – those would just dilute the fuzzing effort. Let's dive into the diff and analyze each function: 1. `do_pagemap_scan` / `pagemap_scan_get_args`: `do_pagemap_scan`, the handler for `/proc/[pid]/pagemap`, is definitely interesting due to its specific nature and new validation logic. 2. `collapse_pte_mapped_thp` / `try_collapse_pte_mapped_thp`: Significant locking changes here, so either of these, particularly `try_collapse_pte_mapped_thp`, looks like a great target. 3. `copy_huge_pud`: A global function with a signature change and rmap tracking, so it could be a focus point. 4. `move_huge_pmd`: A global function modified to use `double_pt_lock`, another potential candidate. 5. `vma_adjust_trans_huge`: Modified to handle PUD and PMD splitting; promising. 6. `deferred_split_scan`: This shrinker callback seems specific to the deferred split queue, with locking for synchronization, so good choice. 7. `ksm_might_need_to_copy`: Changed signature and added checks, potentially relevant. 8. `userfaultfd_register` / `userfaultfd_unregister`: These are good because they are at the entrypoints and are responsible for registering and unregistering userfaultfds. 9. `memfd_pin_folios`: Zeroes out and marks folios uptodate, a possible focus. 10. `move_hugetlb_page_tables`: Uses `double_pt_lock`, an important locking function. 11. `move_page_tables`: Extensive updates and `double_pt_lock`, so it is a good potential symbol. 12. `unmap_mapping_range`: Hole calculation fix, not quite as compelling, but still a possible focus symbol. 13. `cma_release`: Changed ref counting and error handling in CMA – could be relevant. 14. `madvise_free_pte_range`: KSM folio skipping dirty clearing – specific to madvise, so good. 15. `pfn_has_dev_pagemap`: A newly added function, also something that is worth examining. Now, let's *explicitly* exclude: page fault handlers like `handle_mm_fault`, `wp_page_copy`, `do_swap_page`, and `filemap_fault`; `dup_mm`, `dup_mmap` (ubiquitous fork functions); the generic helper `folio_anon_vma` and the static inlines like `double_pt_lock` and `double_pt_unlock`. A final check: the functions that are static in .c files *can* be added as symbols. Therefore, our final symbols are `do_pagemap_scan`, `copy_huge_pud`, `move_huge_pmd`, `vma_adjust_trans_huge`, `deferred_split_scan`, `ksm_might_need_to_copy`, `userfaultfd_unregister`, `userfaultfd_register`, `memfd_pin_folios`, `move_hugetlb_page_tables`, `move_page_tables`, `collapse_pte_mapped_thp`, `cma_release`, `pfn_has_dev_pagemap`, and `madvise_free_pte_range` as these are more feature-specific. |
| 62/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "userfaultfd_ioctl",
"PathPrefixes": [
"mm/",
"fs/"
]
}Results: {
"Output": "mm/khugepaged.c=2119=static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)\n--\nmm/khugepaged.c-2175-\t\t * rmap and userfaultfd_wp() vmas. But since the mmap_lock is not\nmm/khugepaged.c:2176:\t\t * held, it is still possible for a racing userfaultfd_ioctl() or\nmm/khugepaged.c-2177-\t\t * madvise() to have inserted ptes or markers. Now that we hold\n--\nmm/userfaultfd.c=4581=static int userfaultfd_api(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4638-\nmm/userfaultfd.c:4639:static long userfaultfd_ioctl(struct file *file, unsigned cmd,\nmm/userfaultfd.c-4640-\t\t\t unsigned long arg)\n--\nmm/userfaultfd.c=4717=static const struct file_operations userfaultfd_fops = {\n--\nmm/userfaultfd.c-4723-\t.read_iter\t= userfaultfd_read_iter,\nmm/userfaultfd.c:4724:\t.unlocked_ioctl = userfaultfd_ioctl,\nmm/userfaultfd.c-4725-\t.compat_ioctl\t= compat_ptr_ioctl,\n"
} |
| 63/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 73003 output: 29 thoughts: 180 Thoughts: Here's my thought process, as it stands: **Navigating Userfaultfd and THP Collapse** Okay, so I'm diving into this `userfaultfd` code, which, as I recall, lives in `mm/userfaultfd.c`. Right, so `userfaultfd_ioctl` is the key entry point, and within that, I see calls to `userfaultfd_register` and `userfaultfd_unregister`. Seems straightforward enough, `ioctl` handles the setup and teardown, calling those internal functions. Now, let's switch gears a bit. I need to understand how THP collapse is handled in relation to this, so I'm going to look at `mm/khugepaged.c`. I'm specifically curious about the relationship between `try_collapse_pte_mapped_thp` and `collapse_pte_mapped_thp`. I see that `try_collapse_pte_mapped_thp` is static, which implies it's only visible within that file, whereas `collapse_pte_mapped_thp` is global. Both are symbols, but the static nature of the former suggests a more internal, potentially optimization-related role. Finally, I'll switch gears again and check out `move_page_tables` in `mm/mremap.c`. I need to figure out how it's being used. Is it called directly from `mremap_to` or `move_vma`, or are those functions just indirectly using this function? The next step is to examine the callers of `move_page_tables`, which is likely going to reveal its purpose. |
| 64/2 | 2026/10/27 03:22 | tool | grepper |
0mArgs:{
"Expression": "move_page_tables\\(",
"PathPrefixes": [
"mm/"
]
}Results: {
"Output": "mm/huge_memory.c=2630=bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,\n--\nmm/huge_memory.c-2639-\t * The destination pmd shouldn't be established, free_pgtables()\nmm/huge_memory.c:2640:\t * should have released it; but move_page_tables() might have already\nmm/huge_memory.c-2641-\t * inserted a page table, if racing against shmem/file collapse.\n--\nmm/internal.h=1506=extern struct list_lru shadow_nodes;\n--\nmm/internal.h-1514-/* mremap.c */\nmm/internal.h:1515:unsigned long move_page_tables(struct pagetable_move_control *pmc);\nmm/internal.h-1516-\n--\nmm/mremap.c=337=static inline bool uffd_supports_page_table_move(struct pagetable_move_control *pmc)\n--\nmm/mremap.c-342-\t * need to ensure that the uffd-wp state is cleared from all pgtables.\nmm/mremap.c:343:\t * This means recursing into lower page tables in move_page_tables().\nmm/mremap.c-344-\t *\n--\nmm/mremap.c=743=static void try_realign_addr(struct pagetable_move_control *pmc,\n--\nmm/mremap.c-751-\t * Simply align to page table boundaries. Note that we do NOT update the\nmm/mremap.c:752:\t * pmc-\u003eold_end value, and since the move_page_tables() operation spans\nmm/mremap.c-753-\t * from [old_addr, old_end) (offsetting new_addr as it is performed),\n--\nmm/mremap.c=777=static unsigned long pmc_progress(struct pagetable_move_control *pmc)\n--\nmm/mremap.c-783-\t * Prevent negative return values when {old,new}_addr was realigned but\nmm/mremap.c:784:\t * we broke out of the loop in move_page_tables() for the first PMD\nmm/mremap.c-785-\t * itself.\n--\nmm/mremap.c-789-\nmm/mremap.c:790:unsigned long move_page_tables(struct pagetable_move_control *pmc)\nmm/mremap.c-791-{\n--\nmm/mremap.c=1281=static int copy_vma_and_data(struct vma_remap_struct *vrm,\n--\nmm/mremap.c-1308-\nmm/mremap.c:1309:\tmoved_len = move_page_tables(\u0026pmc);\nmm/mremap.c-1310-\tif (moved_len \u003c vrm-\u003eold_len)\n--\nmm/mremap.c-1324-\t\tpmc_revert.need_rmap_locks = true;\nmm/mremap.c:1325:\t\tmove_page_tables(\u0026pmc_revert);\nmm/mremap.c-1326-\n--\nmm/vma_exec.c=23=int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)\n--\nmm/vma_exec.c-70-\tpmc.for_stack = true;\nmm/vma_exec.c:71:\tif (length != move_page_tables(\u0026pmc))\nmm/vma_exec.c-72-\t\treturn -ENOMEM;\n"
} |
| 65/2 | 2026/10/27 03:22 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 74520 output: 354 thoughts: 884 Thoughts: **Analyzing Potential Fuzzing Targets in the Memory Management Subsystem** Alright, let's dive in. `move_page_tables` is definitely a key entry point, the heart of mremap and VMA relocation. It's great to see that it (and its supporting functions like `move_ptes`, `move_normal_pmd`, and so on) has been modified recently, because that signifies a core area with a lot of potential for fuzzing. I'm also seeing `memfd_pin_folios`. This one's very specific, tied directly to memfd pinning, so it warrants a closer look. Similarly, `do_pagemap_scan` is a specific entrypoint for the `PAGEMAP_SCAN` ioctl. It's specialized, which makes it interesting. `ksm_might_need_to_copy` has been updated with a new parameter, anon_vma checks, and multiple `VM_WARN_ON_ONCE_FOLIO`. It's called from swapin (`do_swap_page` and `unuse_pte`), which always brings interesting aspects of memory management into play. Then, there are the `userfaultfd_*` symbols, `userfaultfd_register` and `userfaultfd_unregister`, the entrypoints for those ioctls. And I've got my eye on `copy_huge_pud` given the recent changes involving an added parameter and rmap tracking, which can have important concurrency implications. The `move_huge_pmd` changes related to `double_pt_lock` are very interesting. `try_collapse_pte_mapped_thp` is a major rewrite for page table locking, zapping, and flushing, inside khugepaged, which is a prime target given the complexity. Finally, `deferred_split_scan`, which has modified locking in the deferred split queue shrinker, could be very useful too. Now, to select the most relevant symbols to focus on, I'll go with a focused set: - `move_page_tables` - `move_huge_pmd` - `copy_huge_pud` - `try_collapse_pte_mapped_thp` - `ksm_might_need_to_copy` - `do_pagemap_scan` - `memfd_pin_folios` - `userfaultfd_register` - `userfaultfd_unregister` - `deferred_split_scan` - `cma_release` Next, I need to verify which kernel configurations are essential for exercising these code paths. Based on the involved subsystems, I can identify the following: - `TRANSPARENT_HUGEPAGE` - `KSM` - `USERFAULTFD` - `HUGETLBFS` - `MEMFD_CREATE` - `PROC_PAGE_MONITOR` - `CMA` - `ZONE_DEVICE` - `NUMA` Let me make sure there are no unexpected dependencies. `TRANSPARENT_HUGEPAGE` depends on `HAVE_ARCH_TRANSPARENT_HUGEPAGE`, which is fine. `KSM` requires `MMU`, no problem there. `USERFAULTFD`, `HUGETLBFS` is dependent on `SYSFS` and `ARCH_SUPPORTS_HUGETLBFS`, standard. `MEMFD_CREATE` might prompt for `EXPERT` but that's a non-issue. `PROC_PAGE_MONITOR` needs `PROC_FS`. The others, `CMA` requires `MMU`, `ZONE_DEVICE` needs `PHYS_ADDR_T_64BIT && SPARSEMEM_VMEMMAP`, and `NUMA` needs `SMP && X86_64` are all normal configurations. This is a good list of potential fuzzing targets, considering the modifications to core mm subsystems, the locking and refactoring changes, added assertions, and potential concurrency and state machine regressions. I'm confident in the analysis now. Time to generate the report. |
| 66/2 | 2026/10/27 03:23 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"TRANSPARENT_HUGEPAGE",
"KSM",
"USERFAULTFD",
"HUGETLBFS",
"MEMFD_CREATE",
"PROC_PAGE_MONITOR",
"CMA",
"ZONE_DEVICE",
"NUMA"
],
"FocusSymbols": [
"move_page_tables",
"move_huge_pmd",
"copy_huge_pud",
"try_collapse_pte_mapped_thp",
"ksm_might_need_to_copy",
"do_pagemap_scan",
"memfd_pin_folios",
"userfaultfd_register",
"userfaultfd_unregister",
"deferred_split_scan",
"cma_release"
],
"Reasoning": "The patch series modifies core memory management (mm) subsystems, including page table manipulation (mremap, khugepaged, huge_memory), KSM copy-on-write and anon_vma checks, userfaultfd register/unregister wakeup flows, memfd pinning, and pagemap scan argument validation. It introduces restructured page table locking (double_pt_lock), new VM_WARN_ON_ONCE_FOLIO assertions in KSM, rmap accounting in copy_huge_pud, and list synchronization in deferred_split_scan. These are fully reachable via user-space syscalls (mremap, madvise, userfaultfd, ioctl, memfd_create) and warrant fuzzing to identify potential deadlocks, race conditions, or assertion violations.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"TRANSPARENT_HUGEPAGE",
"KSM",
"USERFAULTFD",
"HUGETLBFS",
"MEMFD_CREATE",
"PROC_PAGE_MONITOR",
"CMA",
"ZONE_DEVICE",
"NUMA"
],
"FocusSymbols": [
"move_page_tables",
"move_huge_pmd",
"copy_huge_pud",
"try_collapse_pte_mapped_thp",
"ksm_might_need_to_copy",
"do_pagemap_scan",
"memfd_pin_folios",
"userfaultfd_register",
"userfaultfd_unregister",
"deferred_split_scan",
"cma_release"
],
"Reasoning": "The patch series modifies core memory management (mm) subsystems, including page table manipulation (mremap, khugepaged, huge_memory), KSM copy-on-write and anon_vma checks, userfaultfd register/unregister wakeup flows, memfd pinning, and pagemap scan argument validation. It introduces restructured page table locking (double_pt_lock), new VM_WARN_ON_ONCE_FOLIO assertions in KSM, rmap accounting in copy_huge_pud, and list synchronization in deferred_split_scan. These are fully reachable via user-space syscalls (mremap, madvise, userfaultfd, ioctl, memfd_create) and warrant fuzzing to identify potential deadlocks, race conditions, or assertion violations.",
"WorthFuzzing": true
} |
| 67/1 | 2026/10/27 03:23 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 68/2 | 2026/10/27 03:23 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series touches several core memory management subsystems (locking, page table manipulation, huge pages, KSM, mremap, and filemap/page cache management). None of these changes introduce or expose risks related to uninitialized memory:\n\n1. Initialization of variables and structures:\n - All newly introduced local variables across the modified functions (such as `range` in mm/userfaultfd.c, `pteval` in mm/rmap.c, `old_pmdval` and `new_pmdval` in mm/mremap.c, `leaked` in mm/cma.c, and `empty` in mm/memory-tiers.c) are fully initialized before use or assigned by functions prior to being evaluated.\n - In `kernel/fork.c`, `dup_mm` now acquires `mmap_write_lock_killable` prior to `memcpy(mm, oldmm, sizeof(*mm))`, ensuring consistent struct state during copy.\n\n2. Kernel-to-user info-leak and bounds handling:\n - No kernel structures with uninitialized fields or padding are copied or exposed to userspace.\n - In `fs/proc/task_mmu.c`, input validation is tightened to prevent 64-bit argument truncation on 32-bit platforms.\n - In `mm/gup.c`, `memfd_pin_folios()` explicitly adds `folio_zero_range()` for non-uptodate folios left behind by `fallocate()`, which resolves a potential leak of uninitialized pages rather than creating one.\n\n3. KASAN vs KMSAN applicability:\n - The primary risks of the modifications in this series involve concurrency, locking hierarchies (e.g., introduction and use of `double_pt_lock`), reference counting, and page table navigation.\n - These failure modes (use-after-free, deadlocks, race conditions, bad pointer dereferences) are effectively detected by KASAN, LOCKDEP, and standard kernel debugging tools. KMSAN provides no additional detection value for these changes.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
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.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 0ac6921d5becf362e53ffa845fb62eeee20cee70
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Oct 27 03:18:52 2026 +0000
syz-cluster: applied patch under review
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 7452b695fdaec4..762b6dbb81e4ac 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3032,6 +3032,12 @@ static int pagemap_scan_get_args(struct pm_scan_arg *arg,
arg->category_anyof_mask | arg->return_mask) & ~PM_SCAN_CATEGORIES)
return -EINVAL;
+ /* Don't cut 64-bit numbers down to addresses that happen to work. */
+ if (arg->start != (unsigned long)arg->start ||
+ arg->end != (unsigned long)arg->end ||
+ arg->vec != (unsigned long)arg->vec)
+ return -EFAULT;
+
arg->start = untagged_addr((unsigned long)arg->start);
arg->end = untagged_addr((unsigned long)arg->end);
arg->vec = untagged_addr((unsigned long)arg->vec);
diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h
index fbe1114e9a443d..3f4a180b48f3c0 100644
--- a/include/asm-generic/tlb.h
+++ b/include/asm-generic/tlb.h
@@ -350,8 +350,7 @@ struct mmu_gather {
void tlb_flush_mmu(struct mmu_gather *tlb);
static inline void __tlb_adjust_range(struct mmu_gather *tlb,
- unsigned long address,
- unsigned int range_size)
+ unsigned long address, unsigned long range_size)
{
tlb->start = min(tlb->start, address);
tlb->end = max(tlb->end, address + range_size);
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index c745f7ad22987f..fac05120ab7744 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -12,9 +12,8 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
bool huge_pmd_set_accessed(struct vm_fault *vmf);
-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
- pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
- struct vm_area_struct *vma);
+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
+ pud_t *dst_pud, pud_t *src_pud, unsigned long addr);
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud);
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index d39d0d5483a23a..a0d358a1feb632 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -91,8 +91,8 @@ static inline void ksm_exit(struct mm_struct *mm)
* We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
* but what if the vma was unmerged while the page was swapped out?
*/
-struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr);
+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,
+ struct vm_area_struct *vma, unsigned long addr);
void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
@@ -145,7 +145,7 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
}
static inline struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr)
+ struct page *page, struct vm_area_struct *vma, unsigned long addr)
{
return folio;
}
diff --git a/include/linux/memremap.h b/include/linux/memremap.h
index e3c2ccf872a8af..b5e5e626c7a596 100644
--- a/include/linux/memremap.h
+++ b/include/linux/memremap.h
@@ -231,6 +231,7 @@ void memunmap_pages(struct dev_pagemap *pgmap);
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
struct dev_pagemap *get_dev_pagemap(unsigned long pfn);
+bool pfn_has_dev_pagemap(unsigned long pfn);
bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);
unsigned long memremap_compat_align(void);
@@ -284,6 +285,11 @@ static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
return NULL;
}
+static inline bool pfn_has_dev_pagemap(unsigned long pfn)
+{
+ return false;
+}
+
static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)
{
return false;
diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h
index a11a44eef521e9..5db78e5761c5c3 100644
--- a/include/linux/mmu_notifier.h
+++ b/include/linux/mmu_notifier.h
@@ -483,7 +483,8 @@ mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
/*
* This version of mmu_notifier_invalidate_range_start() avoids blocking, but it
* can return an error if a notifier can't proceed without blocking, in which
- * case you're not allowed to modify PTEs in the specified range.
+ * case you're not allowed to modify PTEs in the specified range or call
+ * mmu_notifier_invalidate_range_end().
*
* This is mainly intended for OOM handling.
*/
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index a7c7a96a7cbf90..2ceca7993e4f52 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -117,8 +117,14 @@ extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
unsigned long len, unsigned long pgoff, unsigned long flags);
extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
#ifdef CONFIG_SHMEM
+bool file_inode_is_shmem(const struct inode *inode);
bool shmem_mapping(const struct address_space *mapping);
#else
+static inline bool file_inode_is_shmem(const struct inode *inode)
+{
+ return false;
+}
+
static inline bool shmem_mapping(const struct address_space *mapping)
{
return false;
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index a14b8a9ffb7b1c..565e98f342dc83 100644
--- a/include/linux/userfaultfd_k.h
+++ b/include/linux/userfaultfd_k.h
@@ -153,8 +153,6 @@ extern int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
unsigned long len, bool enable_rwp);
/* move_pages */
-void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2);
-void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2);
int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,
struct vm_area_struct *dst_vma,
struct vm_area_struct *src_vma,
diff --git a/kernel/fork.c b/kernel/fork.c
index 22eaf5fb844d0a..c8948cadccf71d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1532,12 +1532,19 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
if (!mm)
goto fail_nomem;
+ uprobe_start_dup_mmap();
+ if (mmap_write_lock_killable(oldmm)) {
+ free_mm(mm);
+ goto fail_uprobe;
+ }
+
memcpy(mm, oldmm, sizeof(*mm));
- if (!mm_init(mm, tsk))
- goto fail_nomem;
+ if (!mm_init(mm, tsk)) {
+ mmap_write_unlock(oldmm);
+ goto fail_uprobe;
+ }
- uprobe_start_dup_mmap();
err = dup_mmap(mm, oldmm);
if (err)
goto free_pt;
@@ -1556,9 +1563,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
mm->binfmt = NULL;
mm_init_owner(mm, NULL);
mmput(mm);
- if (err)
- uprobe_end_dup_mmap();
-
+ if (!err)
+ return NULL;
+fail_uprobe:
+ uprobe_end_dup_mmap();
fail_nomem:
return NULL;
}
diff --git a/mm/cma.c b/mm/cma.c
index a10ea37a261da3..871f95c9ae9f0e 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
unsigned long count)
{
struct cma_memrange *cmr;
- unsigned long ret = 0;
+ unsigned long leaked = 0;
unsigned long i, pfn;
cmr = find_cma_memrange(cma, pages, count);
@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
pfn = page_to_pfn(pages);
for (i = 0; i < count; i++, pfn++)
- ret += !put_page_testzero(pfn_to_page(pfn));
+ leaked += !page_ref_freeze(pfn_to_page(pfn), 1);
- WARN(ret, "%lu pages are still in use!\n", ret);
+ if (leaked) {
+ pr_warn_ratelimited("%lu pages are still in use, not freeing CMA region!\n",
+ leaked);
+ return true;
+ }
__cma_release_frozen(cma, cmr, pages, count);
diff --git a/mm/filemap.c b/mm/filemap.c
index b74bc1e5015c6f..e85a4114d2ecc7 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -227,18 +227,6 @@ void __filemap_remove_folio(struct folio *folio, void *shadow)
page_cache_delete(mapping, folio, shadow);
}
-static void filemap_free_folio(const struct address_space *mapping,
- struct folio *folio)
-{
- void (*free_folio)(struct folio *);
-
- free_folio = mapping->a_ops->free_folio;
- if (free_folio)
- free_folio(folio);
-
- folio_put_refs(folio, folio_nr_pages(folio));
-}
-
/**
* filemap_remove_folio - Remove folio from page cache.
* @folio: The folio.
@@ -250,6 +238,7 @@ static void filemap_free_folio(const struct address_space *mapping,
void filemap_remove_folio(struct folio *folio)
{
struct address_space *mapping = folio->mapping;
+ void (*free_folio)(struct folio *folio);
BUG_ON(!folio_test_locked(folio));
spin_lock(&mapping->host->i_lock);
@@ -258,9 +247,12 @@ void filemap_remove_folio(struct folio *folio)
xa_unlock_irq(&mapping->i_pages);
if (mapping_shrinkable(mapping))
inode_lru_list_add(mapping->host);
+ free_folio = mapping->a_ops->free_folio;
spin_unlock(&mapping->host->i_lock);
- filemap_free_folio(mapping, folio);
+ if (free_folio)
+ free_folio(folio);
+ folio_put_refs(folio, folio_nr_pages(folio));
}
/*
@@ -320,6 +312,7 @@ static void page_cache_delete_batch(struct address_space *mapping,
void delete_from_page_cache_batch(struct address_space *mapping,
struct folio_batch *fbatch)
{
+ void (*free_folio)(struct folio *folio);
int i;
if (!folio_batch_count(fbatch))
@@ -337,10 +330,16 @@ void delete_from_page_cache_batch(struct address_space *mapping,
xa_unlock_irq(&mapping->i_pages);
if (mapping_shrinkable(mapping))
inode_lru_list_add(mapping->host);
+ free_folio = mapping->a_ops->free_folio;
spin_unlock(&mapping->host->i_lock);
- for (i = 0; i < folio_batch_count(fbatch); i++)
- filemap_free_folio(mapping, fbatch->folios[i]);
+ for (i = 0; i < folio_batch_count(fbatch); i++) {
+ struct folio *folio = fbatch->folios[i];
+
+ if (free_folio)
+ free_folio(folio);
+ folio_put_refs(folio, folio_nr_pages(folio));
+ }
}
int filemap_check_errors(struct address_space *mapping)
@@ -1374,18 +1373,30 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
* return value based on that state without races.
*
* Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
- * waiter, but an exclusive one requires WQ_FLAG_DONE.
+ * waiter, but an exclusive one must also acquire the bit.
*/
- if (behavior == EXCLUSIVE)
- return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
+ if (behavior == EXCLUSIVE) {
+ if (wait->flags & WQ_FLAG_DONE)
+ return 0;
+ /*
+ * We might have just been woken up and the waker will not
+ * wake up anybody else. If we ignore that now, the remaining
+ * waiters may not get woken up: take the bit now if it is
+ * still free.
+ */
+ if ((wait->flags & WQ_FLAG_WOKEN) &&
+ !test_and_set_bit(bit_nr, folio_flags(folio, 0)))
+ return 0;
+ return -EINTR;
+ }
return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
}
#ifdef CONFIG_MIGRATION
/**
- * softleaf_entry_wait_on_locked - Wait for a migration entry or
- * device_private entry to be removed.
+ * softleaf_entry_wait_on_locked - Wait for a migration entry to be removed
+ * or a device-private folio in a device-private entry to get unlocked.
* @entry: migration or device_private swap entry.
* @ptl: already locked ptl. This function will drop the lock.
*
@@ -3297,11 +3308,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
* @folio - the folio to lock.
* @fpin - the pointer to the file we may pin (or is already pinned).
*
- * This works similar to lock_folio_or_retry in that it can drop the
- * mmap_lock. It differs in that it actually returns the folio locked
- * if it returns 1 and 0 if it couldn't lock the folio. If we did have
- * to drop the mmap_lock then fpin will point to the pinned file and
- * needs to be fput()'ed at a later point.
+ * Returns 1 with the folio locked, or 0 if locking failed.
+ *
+ * On both entry and return, a non-NULL *fpin means the fault lock has
+ * already been dropped and the caller must eventually fput() the file.
+ * Otherwise the fault lock is still held, including on failure.
*/
static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
struct file **fpin)
@@ -3318,21 +3329,9 @@ static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
return 0;
*fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
- if (vmf->flags & FAULT_FLAG_KILLABLE) {
- if (__folio_lock_killable(folio)) {
- /*
- * We didn't have the right flags to drop the
- * fault lock, but all fault_handlers only check
- * for fatal signals if we return VM_FAULT_RETRY,
- * so we need to drop the fault lock here and
- * return 0 if we don't have a fpin.
- */
- if (*fpin == NULL)
- release_fault_lock(vmf);
- return 0;
- }
- } else
- __folio_lock(folio);
+ if (vmf->flags & FAULT_FLAG_KILLABLE)
+ return !__folio_lock_killable(folio);
+ __folio_lock(folio);
return 1;
}
@@ -3561,7 +3560,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
* vma->vm_mm->mmap_lock must be held on entry.
*
* If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
- * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
+ * may be dropped before doing I/O or when aborting an interrupted wait.
*
* If our return value does not have VM_FAULT_RETRY set, the mmap_lock
* has not been released.
@@ -3717,9 +3716,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
out_retry:
/*
- * We dropped the mmap_lock, we need to return to the fault handler to
- * re-find the vma and come back and find our hopefully still populated
- * page.
+ * With fpin set, the fault lock was already dropped. Otherwise it is
+ * still held. Release invalidate_lock before dropping either protection.
*/
if (!IS_ERR(folio))
folio_put(folio);
@@ -3727,6 +3725,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
filemap_invalidate_unlock_shared(mapping);
if (fpin)
fput(fpin);
+ else if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
+ release_fault_lock(vmf);
return ret | VM_FAULT_RETRY;
}
EXPORT_SYMBOL(filemap_fault);
diff --git a/mm/gup.c b/mm/gup.c
index f166acf794e308..48e1bfe5ab9a96 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -658,9 +658,6 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,
assert_spin_locked(pud_lockptr(mm, pudp));
- if (!pud_present(pud))
- return NULL;
-
if ((flags & FOLL_WRITE) &&
!can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags))
return NULL;
@@ -896,15 +893,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
}
static struct page *follow_pmd_mask(struct vm_area_struct *vma,
- unsigned long address, pud_t *pudp,
- unsigned int flags,
- unsigned long *page_mask)
+ unsigned long address, pud_t *pudp, pud_t pud,
+ unsigned int flags, unsigned long *page_mask)
{
pmd_t *pmd, pmdval;
spinlock_t *ptl;
struct page *page;
struct mm_struct *mm = vma->vm_mm;
+ if (unlikely(pud_bad(pud)))
+ return no_page_table(vma, flags, address);
+
pmd = pmd_offset(pudp, address);
pmdval = pmdp_get_lockless(pmd);
if (pmd_none(pmdval))
@@ -953,18 +952,23 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma,
pud = pudp_get(pudp);
if (!pud_present(pud))
return no_page_table(vma, flags, address);
- if (pud_leaf(pud)) {
- ptl = pud_lock(mm, pudp);
- page = follow_huge_pud(vma, address, pudp, flags, page_mask);
+ if (!pud_leaf(pud))
+ return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);
+
+ ptl = pud_lock(mm, pudp);
+ pud = *pudp;
+ if (unlikely(!pud_present(pud))) {
spin_unlock(ptl);
- if (page)
- return page;
return no_page_table(vma, flags, address);
}
- if (unlikely(pud_bad(pud)))
- return no_page_table(vma, flags, address);
+ if (unlikely(!pud_leaf(pud))) {
+ spin_unlock(ptl);
+ return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask);
+ }
- return follow_pmd_mask(vma, address, pudp, flags, page_mask);
+ page = follow_huge_pud(vma, address, pudp, flags, page_mask);
+ spin_unlock(ptl);
+ return page;
}
static struct page *follow_p4d_mask(struct vm_area_struct *vma,
@@ -3200,6 +3204,9 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,
FOLL_PCI_P2PDMA | FOLL_HONOR_NUMA_FAULT)))
return -EINVAL;
+ if (unlikely(!nr_pages))
+ return 0;
+
if (gup_flags & FOLL_PIN)
mm_set_has_pinned_flag(current->mm);
@@ -3207,8 +3214,8 @@ static int gup_fast_fallback(unsigned long start, unsigned long nr_pages,
might_lock_read(¤t->mm->mmap_lock);
start = untagged_addr(start) & PAGE_MASK;
- len = nr_pages << PAGE_SHIFT;
- if (check_add_overflow(start, len, &end))
+ if (check_shl_overflow(nr_pages, PAGE_SHIFT, &len) ||
+ check_add_overflow(start, len, &end))
return -EOVERFLOW;
if (end > TASK_SIZE_MAX)
return -EFAULT;
@@ -3512,6 +3519,20 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
goto err;
}
+ /*
+ * fallocate() can leave folios in the
+ * page cache that were never cleared.
+ */
+ if (!folio_test_uptodate(folio)) {
+ folio_lock(folio);
+ if (!folio_test_uptodate(folio)) {
+ folio_zero_range(folio, 0, folio_size(folio));
+ flush_dcache_folio(folio);
+ folio_mark_uptodate(folio);
+ }
+ folio_unlock(folio);
+ }
+
if (nr_folios == 0)
*offset = offset_in_folio(folio, start);
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index bd44d58e86845c..d090642ac8c2b6 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1500,7 +1500,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)
folio_unlock(folio);
folio_put(folio);
} else {
- spin_unlock(ptl);
+ softleaf_entry_wait_on_locked(entry, ptl);
}
return ret;
@@ -2072,11 +2072,13 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr,
update_mmu_cache_pud(vma, addr, pud);
}
-int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
- pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
- struct vm_area_struct *vma)
+int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
+ pud_t *dst_pud, pud_t *src_pud, unsigned long addr)
{
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
+ struct mm_struct *src_mm = src_vma->vm_mm;
spinlock_t *dst_ptl, *src_ptl;
+ struct page *page;
pud_t pud;
int ret;
@@ -2089,11 +2091,20 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
if (unlikely(!pud_trans_huge(pud)))
goto out_unlock;
- /*
- * TODO: once we support anonymous pages, use
- * folio_try_dup_anon_rmap_*() and split if duplicating fails.
- */
- if (vma_is_cow_mapping(vma) && pud_write(pud)) {
+ page = vm_normal_page_pud(src_vma, addr, pud);
+ if (page) {
+ struct folio *folio = page_folio(page);
+
+ folio_get(folio);
+ /*
+ * TODO: once we support anonymous pages, use
+ * folio_try_dup_anon_rmap_*() and split if duplicating fails.
+ */
+ folio_add_file_rmap_pud(folio, page, dst_vma);
+ add_mm_counter(dst_mm, mm_counter_file(folio), HPAGE_PUD_NR);
+ }
+
+ if (vma_is_cow_mapping(src_vma) && pud_write(pud)) {
pudp_set_wrprotect(src_mm, addr, src_pud);
pud = pud_wrprotect(pud);
}
@@ -2482,7 +2493,15 @@ static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
pmd_t pmdval, struct folio *folio, bool is_present)
{
- const bool is_device_private = folio_is_device_private(folio);
+ const bool is_device_private = !is_present && pmd_is_device_private_entry(pmdval);
+
+ /* Transfer young+dirty while the folio is still mapped. */
+ if (is_present && !folio_test_anon(folio)) {
+ if (pmd_dirty(pmdval))
+ folio_mark_dirty(folio);
+ if (pmd_young(pmdval) && likely(vma_has_recency(vma)))
+ folio_mark_accessed(folio);
+ }
/* Present and device private folios are rmappable. */
if (is_present || is_device_private)
@@ -2493,12 +2512,6 @@ static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
} else {
add_mm_counter(mm, mm_counter_file(folio),
-HPAGE_PMD_NR);
-
- if (is_present && pmd_dirty(pmdval))
- folio_mark_dirty(folio);
- if (is_present && pmd_young(pmdval) &&
- likely(vma_has_recency(vma)))
- folio_mark_accessed(folio);
}
/* Device private folios are pinned. */
@@ -2632,15 +2645,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
return false;
}
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
- if (old_ptl) {
- new_ptl = pmd_lockptr(mm, new_pmd);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+ old_ptl = pmd_lockptr(mm, old_pmd);
+ new_ptl = pmd_lockptr(mm, new_pmd);
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (likely(pmd_is_huge(*old_pmd))) {
pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
if (pmd_present(pmd))
force_flush = true;
@@ -2669,11 +2678,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
set_pmd_at(mm, new_addr, new_pmd, pmd);
if (force_flush)
flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
+ double_pt_unlock(old_ptl, new_ptl);
return false;
}
@@ -3495,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
__split_huge_pmd(vma, pmd, address, freeze);
}
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+ unsigned long address)
{
+ if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+ return false;
+ if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+ return false;
/*
- * If the new address isn't hpage aligned and it could previously
- * contain an hugepage: check if we need to split an huge pmd.
+ * Only selected VMAs support mapping PUDs for now. Lift the
+ * restriction once this changes.
*/
- if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
- range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
- ALIGN(address, HPAGE_PMD_SIZE)))
- split_huge_pmd_address(vma, address, false);
+ if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+ return false;
+ return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+ ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+ unsigned long address)
+{
+ if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+ return false;
+
+ return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+ ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+ unsigned long address)
+{
+ const bool split_pud = pud_split_required(vma, address);
+ const bool split_pmd = pmd_split_required(vma, address);
+ pgd_t *pgd;
+ p4d_t *p4d;
+ pud_t *pud;
+ pmd_t *pmd;
+
+ /*
+ * mmap, VMA and rmap write locks protect the walk; the split helpers
+ * take the page table locks.
+ */
+ if (!split_pud && !split_pmd)
+ return;
+
+ pgd = pgd_offset(vma->vm_mm, address);
+ if (!pgd_present(*pgd))
+ return;
+
+ p4d = p4d_offset(pgd, address);
+ if (!p4d_present(*p4d))
+ return;
+
+ pud = pud_offset(p4d, address);
+ if (split_pud && pud_leaf(*pud))
+ __split_huge_pud(vma, pud, address);
+ if (!split_pmd || !pud_present(*pud))
+ return;
+
+ pmd = pmd_offset(pud, address);
+ __split_huge_pmd(vma, pmd, address, false);
}
void vma_adjust_trans_huge(struct vm_area_struct *vma,
@@ -3513,14 +3571,14 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,
struct vm_area_struct *next)
{
/* Check if we need to split start first. */
- split_huge_pmd_if_needed(vma, start);
+ split_huge_leaf(vma, start);
/* Check if we need to split end next. */
- split_huge_pmd_if_needed(vma, end);
+ split_huge_leaf(vma, end);
/* If we're incrementing next->vm_start, we might need to split it. */
if (next)
- split_huge_pmd_if_needed(next, end);
+ split_huge_leaf(next, end);
}
/*
@@ -4522,8 +4580,7 @@ int split_folio_to_list(struct folio *folio, struct list_head *list)
*
* It is unsafe to call folio_unqueue_deferred_split() until folio refcount is
* zero: because even when the list_lru lock is held, a non-empty
- * _deferred_list might be in use on deferred_split_scan()'s unlocked
- * on-stack list.
+ * _deferred_list might be in use on deferred_split_scan()'s on-stack list.
*
* The list_lru sublist is determined by folio's memcg: it is therefore
* important to unqueue deferred split before changing folio memcg.
@@ -4665,10 +4722,22 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
deferred_split_isolate, &dispose);
list_for_each_entry_safe(folio, next, &dispose, _deferred_list) {
+ struct mem_cgroup *memcg;
+ struct list_lru_one *lru;
bool did_split = false;
bool underused = false;
+ /*
+ * Sync with deferred_split_folio(), which can requeue the
+ * folio once list_empty() is true, and sets PG_partially_mapped.
+ */
+ rcu_read_lock();
+ memcg = folio_memcg(folio);
+ lru = list_lru_lock_irq(&deferred_split_lru, folio_nid(folio),
+ &memcg);
list_del_init(&folio->_deferred_list);
+ list_lru_unlock_irq(lru);
+ rcu_read_unlock();
if (!folio_test_partially_mapped(folio)) {
/*
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index da5d04edf7c108..84141527fe73d3 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5153,15 +5153,9 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
spinlock_t *src_ptl, *dst_ptl;
pte_t pte;
- dst_ptl = huge_pte_lock(h, mm, dst_pte);
+ dst_ptl = huge_pte_lockptr(h, mm, dst_pte);
src_ptl = huge_pte_lockptr(h, mm, src_pte);
-
- /*
- * We don't have to worry about the ordering of src and dst ptlocks
- * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
- */
- if (src_ptl != dst_ptl)
- spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
+ double_pt_lock(dst_ptl, src_ptl);
pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);
@@ -5190,9 +5184,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
}
- if (src_ptl != dst_ptl)
- spin_unlock(src_ptl);
- spin_unlock(dst_ptl);
+ double_pt_unlock(dst_ptl, src_ptl);
}
int move_hugetlb_page_tables(struct vm_area_struct *vma,
diff --git a/mm/internal.h b/mm/internal.h
index b1b0f29d2216ee..7e4d88b2fb02c8 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -619,6 +619,34 @@ static inline void force_page_cache_readahead(struct address_space *mapping,
force_page_cache_ra(&ractl, nr_to_read);
}
+/*
+ * Take two page table locks at the same level in address order such that
+ * we cannot deadlock.
+ */
+static inline void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2)
+ __acquires(ptl1)
+ __acquires(ptl2)
+{
+ if (ptl1 > ptl2)
+ swap(ptl1, ptl2);
+ spin_lock(ptl1);
+ if (ptl1 != ptl2)
+ spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
+ else
+ __acquire(ptl2);
+}
+
+static inline void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2)
+ __releases(ptl1)
+ __releases(ptl2)
+{
+ spin_unlock(ptl1);
+ if (ptl1 != ptl2)
+ spin_unlock(ptl2);
+ else
+ __release(ptl2);
+}
+
unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 4b71d010b2aff5..4313abf9cfcb97 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -10,6 +10,7 @@
#include <linux/mm_inline.h>
#include <linux/kthread.h>
#include <linux/khugepaged.h>
+#include <linux/hugetlb.h>
#include <linux/freezer.h>
#include <linux/mman.h>
#include <linux/hashtable.h>
@@ -1832,6 +1833,17 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
return SCAN_SUCCEED;
}
+static bool pte_table_empty(pte_t *pte)
+{
+ int i;
+
+ for (i = 0; i < HPAGE_PMD_NR; i++, pte++) {
+ if (!pte_none(ptep_get(pte)))
+ return false;
+ }
+ return true;
+}
+
static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
bool install_pmd)
{
@@ -1904,7 +1916,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
result = SCAN_FAIL;
start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
- if (!start_pte) /* mmap_lock + page lock should prevent this */
+ if (!start_pte)
goto drop_folio;
/* step 1: check all mapped PTEs are to the right huge page */
@@ -1940,26 +1952,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
mmu_notifier_invalidate_range_start(&range);
notified = true;
- /*
- * pmd_lock covers a wider range than ptl, and (if split from mm's
- * page_table_lock) ptl nests inside pml. The less time we hold pml,
- * the better; but userfaultfd's mfill_atomic_pte() on a private VMA
- * inserts a valid as-if-COWed PTE without even looking up page cache.
- * So page lock of folio does not protect from it, so we must not drop
- * ptl before pgt_pmd is removed, so uffd private needs pml taken now.
- */
- if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
- pml = pmd_lock(mm, pmd);
-
- start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
- if (!start_pte) /* mmap_lock + page lock should prevent this */
- goto abort;
- if (!pml)
- spin_lock(ptl);
- else if (ptl != pml)
- spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
-
- if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+ start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
+ if (!start_pte)
goto abort;
/* step 2: clear page table and adjust rmap */
@@ -1991,36 +1985,41 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
nr_batch_ptes = folio_pte_batch(folio, pte, ptent, max_nr_batch_ptes);
- /*
- * Must clear entry, or a racing truncate may re-remove it.
- * TLB flush can be left until pmdp_collapse_flush() does it.
- * PTE dirty? Shmem page is already dirty; file is read-only.
- */
- clear_ptes(mm, addr, pte, nr_batch_ptes);
+ /* Must clear entry, or a racing truncate may re-remove it. */
+ ptent = get_and_clear_ptes(mm, addr, pte, nr_batch_ptes);
+ if (pte_dirty(ptent))
+ folio_mark_dirty(folio);
folio_remove_rmap_ptes(folio, page, nr_batch_ptes, vma);
nr_mapped_ptes += nr_batch_ptes;
}
- if (!pml)
- spin_unlock(ptl);
-
/* step 3: set proper refcount and mm_counters. */
if (nr_mapped_ptes) {
folio_ref_sub(folio, nr_mapped_ptes);
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
}
- /* step 4: remove empty page table */
- if (!pml) {
+ /* step 4: acquire the PMD lock and remove the empty page table */
+ pml = pmd_lockptr(mm, pmd);
+ if (ptl != pml && !spin_trylock(pml)) {
+ /* We must flush before dropping the PTL. */
+ if (nr_mapped_ptes)
+ flush_tlb_range(vma, haddr, end);
+ pte_unmap_unlock(start_pte, ptl);
+ ptl = NULL;
+
pml = pmd_lock(mm, pmd);
- if (ptl != pml) {
+ start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
+ if (!start_pte)
+ goto unlock;
+ if (ptl != pml)
spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
- if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
- flush_tlb_mm(mm);
- goto unlock;
- }
- }
+
+ if (!pte_table_empty(start_pte))
+ goto unlock;
}
+
+ /* This also flushes the zapped folio ptes properly. */
pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
pmdp_get_lockless_sync();
pte_unmap_unlock(start_pte, ptl);
@@ -2041,7 +2040,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
goto drop_folio;
abort:
if (nr_mapped_ptes) {
- flush_tlb_mm(mm);
+ flush_tlb_range(vma, haddr, end);
folio_ref_sub(folio, nr_mapped_ptes);
add_mm_counter(mm, mm_counter_file(folio), -nr_mapped_ptes);
}
diff --git a/mm/ksm.c b/mm/ksm.c
index dbfb8f98b18925..e95f6b67236023 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -475,6 +475,7 @@ static void advisor_stop_scan(void)
#ifdef CONFIG_NUMA
/* Zeroed when merging across nodes is not allowed */
static unsigned int ksm_merge_across_nodes = 1;
+/* Number of allocated stable and unstable tree roots. */
static int ksm_nr_node_ids = 1;
#else
#define ksm_merge_across_nodes 1U
@@ -3136,24 +3137,52 @@ void __ksm_exit(struct mm_struct *mm)
trace_ksm_exit(mm);
}
-struct folio *ksm_might_need_to_copy(struct folio *folio,
- struct vm_area_struct *vma, unsigned long addr)
+static bool folio_anon_rmap_compatible(const struct folio *folio,
+ const struct page *page, struct vm_area_struct *vma,
+ unsigned long addr)
{
- struct page *page = folio_page(folio, 0);
- struct anon_vma *anon_vma = folio_anon_vma(folio);
- struct folio *new_folio;
+ const struct anon_vma *anon_vma = folio_anon_vma(folio);
+ struct anon_vma_chain *avc;
- if (folio_test_large(folio))
- return folio;
+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+ VM_WARN_ON_ONCE_FOLIO(page_folio(page) != folio, folio);
+ VM_WARN_ON_ONCE_FOLIO(folio_test_ksm(folio), folio);
+ vma_assert_stabilised(vma);
+
+ if (!anon_vma)
+ return true;
+ if (page_pgoff(folio, page) != linear_anon_page_index(vma, addr))
+ return false;
+
+ /*
+ * Careful: anon_vma might have been freed, so we cannot safely
+ * dereference it. Add a fast path before we walk the anon_vma chain.
+ */
+ if (anon_vma == vma->anon_vma || anon_vma == vma->anon_vma->root)
+ return true;
+
+ list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) {
+ if (avc->anon_vma == anon_vma)
+ return true;
+ }
+ return false;
+}
+struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page,
+ struct vm_area_struct *vma, unsigned long addr)
+{
+ struct folio *new_folio;
+
+ /*
+ * A KSM folio is small, but the slot that it left behind can be swapped
+ * in as a part of a large folio by one of the processes that shared it.
+ * So test, and copy, the page of the slot.
+ */
if (folio_test_ksm(folio)) {
if (folio_stable_node(folio) &&
!(ksm_run & KSM_RUN_UNMERGE))
return folio; /* no need to copy it */
- } else if (!anon_vma) {
- return folio; /* no need to copy it */
- } else if (folio->index == linear_anon_page_index(vma, addr) &&
- anon_vma->root == vma->anon_vma->root) {
+ } else if (folio_anon_rmap_compatible(folio, page, vma, addr)) {
return folio; /* still no need to copy it */
}
if (PageHWPoison(page))
@@ -3636,14 +3665,13 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj,
else {
root_stable_tree = buf;
root_unstable_tree = buf + nr_node_ids;
+ ksm_nr_node_ids = nr_node_ids;
/* Stable tree is empty but not the unstable */
root_unstable_tree[0] = one_unstable_tree[0];
}
}
- if (!err) {
+ if (!err)
ksm_merge_across_nodes = knob;
- ksm_nr_node_ids = knob ? 1 : nr_node_ids;
- }
}
mutex_unlock(&ksm_thread_mutex);
diff --git a/mm/madvise.c b/mm/madvise.c
index 75b409e97dbf24..a16e3cb256b7b0 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -756,6 +756,15 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
if (folio_test_swapcache(folio) || folio_test_dirty(folio)) {
if (!folio_trylock(folio))
continue;
+ /*
+ * KSM folios can gain new mappings at any time, so we must
+ * not clear their dirty flag.
+ */
+ if (folio_test_ksm(folio)) {
+ folio_unlock(folio);
+ continue;
+ }
+
/*
* If we have a large folio at this point, we know it is
* fully mapped so if its mapcount is the same as its
diff --git a/mm/memfd.c b/mm/memfd.c
index 0f6fff004f5e4c..6f5fbce74f21eb 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -211,12 +211,14 @@ static int memfd_wait_for_pins(struct address_space *mapping)
static unsigned int *memfd_file_seals_ptr(struct file *file)
{
- if (shmem_file(file))
- return &SHMEM_I(file_inode(file))->seals;
+ struct inode *inode = file_inode(file);
+
+ if (file_inode_is_shmem(inode))
+ return &SHMEM_I(inode)->seals;
#ifdef CONFIG_HUGETLBFS
if (is_file_hugepages(file))
- return &HUGETLBFS_I(file_inode(file))->seals;
+ return &HUGETLBFS_I(inode)->seals;
#endif
return NULL;
@@ -482,6 +484,8 @@ struct file *memfd_alloc_file(const char *name, unsigned int flags)
}
file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
+ /* a regular file like any other: POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
+ file->f_mode |= FMODE_ATOMIC_POS;
file->f_flags |= O_LARGEFILE;
if (flags & MFD_NOEXEC_SEAL) {
diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 59de210bee5f9e..c0811407ce3973 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -582,7 +582,7 @@ static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler,
{
struct inode *inode = file_inode(file);
- return shmem_file(file) && !inode->i_nlink;
+ return file_inode_is_shmem(inode) && !inode->i_nlink;
}
static unsigned long memfd_luo_get_id(struct file *file)
diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c
index 25e121851b5862..8eda313cf61925 100644
--- a/mm/memory-tiers.c
+++ b/mm/memory-tiers.c
@@ -572,21 +572,14 @@ static struct memory_tier *set_node_memory_tier(int node)
return memtier;
}
-static void destroy_memory_tier(struct memory_tier *memtier)
+static struct memory_tier *clear_node_memory_tier(int node)
{
- list_del(&memtier->list);
- device_unregister(&memtier->dev);
-}
-
-static bool clear_node_memory_tier(int node)
-{
- bool cleared = false;
pg_data_t *pgdat;
struct memory_tier *memtier;
pgdat = NODE_DATA(node);
if (!pgdat)
- return false;
+ return NULL;
/*
* Make sure that anybody looking at NODE_DATA who finds
@@ -604,14 +597,10 @@ static bool clear_node_memory_tier(int node)
synchronize_rcu();
memtype = node_memory_types[node].memtype;
node_clear(node, memtype->nodes);
- if (nodes_empty(memtype->nodes)) {
+ if (nodes_empty(memtype->nodes))
list_del_init(&memtype->tier_sibling);
- if (list_empty(&memtier->memory_types))
- destroy_memory_tier(memtier);
- }
- cleared = true;
}
- return cleared;
+ return memtier;
}
static void release_memtype(struct kref *kref)
@@ -884,13 +873,21 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
{
struct memory_tier *memtier;
struct node_notify *nn = _arg;
+ bool empty = false;
switch (action) {
case NODE_REMOVED_LAST_MEMORY:
mutex_lock(&memory_tier_lock);
- if (clear_node_memory_tier(nn->nid))
+ memtier = clear_node_memory_tier(nn->nid);
+ if (memtier) {
+ empty = list_empty(&memtier->memory_types);
+ if (empty)
+ list_del(&memtier->list);
establish_demotion_targets();
+ }
mutex_unlock(&memory_tier_lock);
+ if (empty)
+ device_unregister(&memtier->dev);
break;
case NODE_ADDED_FIRST_MEMORY:
mutex_lock(&memory_tier_lock);
diff --git a/mm/memory.c b/mm/memory.c
index 5d6d695675bce4..be4c05db37d33b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1463,7 +1463,6 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
unsigned long end)
{
struct mm_struct *dst_mm = dst_vma->vm_mm;
- struct mm_struct *src_mm = src_vma->vm_mm;
pud_t *src_pud, *dst_pud;
unsigned long next;
@@ -1477,8 +1476,7 @@ copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
int err;
VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
- err = copy_huge_pud(dst_mm, src_mm,
- dst_pud, src_pud, addr, src_vma);
+ err = copy_huge_pud(dst_vma, src_vma, dst_pud, src_pud, addr);
if (err == -ENOMEM)
return -ENOMEM;
if (!err)
@@ -3763,6 +3761,13 @@ static inline int __wp_page_copy_user(struct page *dst, struct page *src,
return 0;
}
+ /*
+ * __copy_from_user_inatomic() only works as expected for local faults.
+ * Reject remote faults.
+ */
+ if (unlikely(mm != current->mm))
+ return -EFAULT;
+
/*
* If the source page was a PFN mapping, we don't have
* a "struct page" for it. We do a best-effort copy by
@@ -4088,6 +4093,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
folio_put(old_folio);
delayacct_wpcopy_end();
+ if (err == -EFAULT)
+ return VM_FAULT_SIGBUS;
return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;
}
kmsan_copy_page_meta(&new_folio->page, vmf->page);
@@ -4642,8 +4649,8 @@ EXPORT_SYMBOL_GPL(unmap_mapping_pages);
void unmap_mapping_range(struct address_space *mapping,
loff_t const holebegin, loff_t const holelen, int even_cows)
{
- pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT;
- pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ pgoff_t hba = (u64)holebegin >> PAGE_SHIFT;
+ pgoff_t hlen = ((u64)holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
/* Check for overflow. */
if (sizeof(holelen) > sizeof(hlen)) {
@@ -5089,7 +5096,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
* folio->index of non-ksm folios would be nonlinear inside the
* anon VMA -- the ksm flag is lost on actual swapout.
*/
- folio = ksm_might_need_to_copy(folio, vma, vmf->address);
+ folio = ksm_might_need_to_copy(folio, page, vma, vmf->address);
if (unlikely(!folio)) {
ret = VM_FAULT_OOM;
folio = swapcache;
@@ -5271,7 +5278,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (unlikely(folio != swapcache)) {
folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE);
folio_add_lru_vma(folio, vma);
- folio_put_swap(swapcache, NULL);
+ folio_put_swap(swapcache,
+ folio_file_page(swapcache, swp_offset(entry)));
} else if (!folio_test_anon(folio)) {
/*
* We currently only expect !anon folios that are fully
@@ -6950,8 +6958,12 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
lru_gen_exit_fault();
- /* If the mapping is droppable, then errors due to OOM aren't fatal. */
- if (is_droppable)
+ /*
+ * If the mapping is droppable, then errors due to OOM aren't fatal: the
+ * task returns to user mode, handles its signals and tries again. A
+ * fault from kernel mode has no such return, so it must see the error.
+ */
+ if (is_droppable && (flags & FAULT_FLAG_USER))
ret &= ~VM_FAULT_OOM;
if (flags & FAULT_FLAG_USER) {
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 796af1028ee239..5a972d7a9e4787 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -342,7 +342,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages)
struct page *pfn_to_online_page(unsigned long pfn)
{
unsigned long nr = pfn_to_section_nr(pfn);
- struct dev_pagemap *pgmap;
struct mem_section *ms;
BUILD_BUG_ON(IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID));
@@ -363,14 +362,12 @@ struct page *pfn_to_online_page(unsigned long pfn)
/*
* Slowpath: when ZONE_DEVICE collides with
* ZONE_{NORMAL,MOVABLE} within the same section some pfns in
- * the section may be 'offline' but 'valid'. Only
- * get_dev_pagemap() can determine sub-section online status.
+ * the section may be 'offline' but 'valid'. Only the presence of a
+ * pgmap can determine sub-section online status: it indicates a
+ * ZONE_DEVICE offline pfn. That includes a pgmap whose reference has
+ * been killed, its memmap is about to go away.
*/
- pgmap = get_dev_pagemap(pfn);
- put_dev_pagemap(pgmap);
-
- /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */
- if (pgmap)
+ if (pfn_has_dev_pagemap(pfn))
return NULL;
return pfn_to_page(pfn);
diff --git a/mm/memremap.c b/mm/memremap.c
index accba23aef28cc..a1e99a4c55c561 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -413,6 +413,19 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
}
EXPORT_SYMBOL_GPL(get_dev_pagemap);
+/**
+ * pfn_has_dev_pagemap() - does @pfn belong to a dev_pagemap, dead or alive
+ * @pfn: page frame number to lookup page_map
+ *
+ * In contrast to get_dev_pagemap() this also finds a dev_pagemap which is on
+ * its way out: it is registered before its pages are added, and until they
+ * have been removed.
+ */
+bool pfn_has_dev_pagemap(unsigned long pfn)
+{
+ return xa_load(&pgmap_array, pfn);
+}
+
void free_zone_device_folio(struct folio *folio)
{
struct dev_pagemap *pgmap = folio->pgmap;
diff --git a/mm/mmap.c b/mm/mmap.c
index 148ba01c03739f..7618e72766a522 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1713,8 +1713,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
LIST_HEAD(uf);
VMA_ITERATOR(vmi, mm, 0);
- if (mmap_write_lock_killable(oldmm))
- return -EINTR;
+ mmap_assert_write_locked(oldmm);
flush_cache_dup_mm(oldmm);
uprobe_dup_mmap(oldmm, mm);
/*
@@ -1725,11 +1724,6 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
/* No ordering required: file already has been exposed. */
dup_mm_exe_file(mm, oldmm);
- mm->total_vm = oldmm->total_vm;
- mm->data_vm = oldmm->data_vm;
- mm->exec_vm = oldmm->exec_vm;
- mm->stack_vm = oldmm->stack_vm;
-
/* Use __mt_dup() to efficiently build an identical maple tree. */
retval = __mt_dup(&oldmm->mm_mt, &mm->mm_mt, GFP_KERNEL);
if (unlikely(retval))
diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c
index df69ba6e797f76..a7bae5de5fedc1 100644
--- a/mm/mmu_notifier.c
+++ b/mm/mmu_notifier.c
@@ -569,16 +569,25 @@ int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
{
struct mmu_notifier_subscriptions *subscriptions =
range->mm->notifier_subscriptions;
- int ret;
+ const bool has_itree = subscriptions->has_itree;
+ int ret = 0;
- if (subscriptions->has_itree) {
+ if (has_itree) {
ret = mn_itree_invalidate(subscriptions, range);
if (ret)
return ret;
}
if (!hlist_empty(&subscriptions->list))
- return mn_hlist_invalidate_range_start(subscriptions, range);
- return 0;
+ ret = mn_hlist_invalidate_range_start(subscriptions, range);
+
+ /*
+ * On error, the non-blocking caller is not allowed to call
+ * invalidate_range_end(), as in mn_itree_invalidate().
+ */
+ if (ret && has_itree && !mmu_notifier_range_blockable(range))
+ mn_itree_inv_end(subscriptions);
+
+ return ret;
}
static void
@@ -938,7 +947,6 @@ static int __mmu_interval_notifier_insert(
struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
unsigned long length, const struct mmu_interval_notifier_ops *ops)
{
- interval_sub->mm = mm;
interval_sub->ops = ops;
RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
interval_sub->interval_tree.start = start;
@@ -955,8 +963,12 @@ static int __mmu_interval_notifier_insert(
if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
return -EINVAL;
- /* pairs with mmdrop in mmu_interval_notifier_remove() */
+ /*
+ * Pairs with mmdrop in mmu_interval_notifier_remove(). Drivers take a
+ * non-NULL ->mm for "registered", so set it only with the reference.
+ */
mmgrab(mm);
+ interval_sub->mm = mm;
/*
* If some invalidate_range_start/end region is going on in parallel
diff --git a/mm/mremap.c b/mm/mremap.c
index 0d06d7f37f19a1..70b7264aa6bf5c 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -87,7 +87,7 @@ static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)
return NULL;
pud = pud_offset(p4d, addr);
- if (pud_none_or_clear_bad(pud))
+ if (!pud_trans_huge(*pud) && pud_none_or_clear_bad(pud))
return NULL;
return pud;
@@ -202,7 +202,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
struct mm_struct *mm = vma->vm_mm;
pte_t *old_ptep, *new_ptep;
pte_t old_pte, pte;
- pmd_t dummy_pmdval;
+ pmd_t old_pmdval, new_pmdval;
spinlock_t *old_ptl, *new_ptl;
bool force_flush = false;
unsigned long old_addr = pmc->old_addr;
@@ -234,31 +234,30 @@ static int move_ptes(struct pagetable_move_control *pmc,
if (pmc->need_rmap_locks)
take_rmap_locks(vma);
- /*
- * We don't have to worry about the ordering of src and dst
- * pte locks because exclusive mmap_lock prevents deadlock.
- */
- old_ptep = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
+ old_ptep = pte_offset_map_rw_nolock(mm, old_pmd, old_addr, &old_pmdval,
+ &old_ptl);
if (!old_ptep) {
err = -EAGAIN;
goto out;
}
- /*
- * Now new_pte is none, so collapse_scan_file() path can not find
- * this by traversing file->f_mapping, so there is no concurrency with
- * retract_page_tables(). In addition, we already hold the exclusive
- * mmap_lock, so this new_pte page is stable, so there is no need to get
- * pmdval and do pmd_same() check.
- */
- new_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &dummy_pmdval,
- &new_ptl);
+ new_ptep = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &new_pmdval,
+ &new_ptl);
if (!new_ptep) {
- pte_unmap_unlock(old_ptep, old_ptl);
+ pte_unmap(old_ptep);
+ err = -EAGAIN;
+ goto out;
+ }
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (unlikely(!pmd_same(old_pmdval, pmdp_get_lockless(old_pmd)) ||
+ !pmd_same(new_pmdval, pmdp_get_lockless(new_pmd)))) {
+ double_pt_unlock(old_ptl, new_ptl);
+ pte_unmap(new_ptep);
+ pte_unmap(old_ptep);
err = -EAGAIN;
goto out;
}
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+
flush_tlb_batched_pending(vma->vm_mm);
lazy_mmu_mode_enable();
@@ -317,10 +316,9 @@ static int move_ptes(struct pagetable_move_control *pmc,
lazy_mmu_mode_disable();
if (force_flush)
flush_tlb_range(vma, old_end - len, old_end);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
pte_unmap(new_ptep - 1);
- pte_unmap_unlock(old_ptep - 1, old_ptl);
+ pte_unmap(old_ptep - 1);
out:
if (pmc->need_rmap_locks)
drop_rmap_locks(vma);
@@ -395,14 +393,9 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pmd_lock(mm, old_pmd);
+ old_ptl = pmd_lockptr(mm, old_pmd);
new_ptl = pmd_lockptr(mm, new_pmd);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+ double_pt_lock(old_ptl, new_ptl);
pmd = *old_pmd;
@@ -418,9 +411,7 @@ static bool move_normal_pmd(struct pagetable_move_control *pmc,
pmd_populate(mm, new_pmd, pmd_pgtable(pmd));
flush_tlb_range(vma, pmc->old_addr, pmc->old_addr + PMD_SIZE);
out_unlock:
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return res;
}
@@ -452,26 +443,24 @@ static bool move_normal_pud(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pud_none(*new_pud)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pud_lock(mm, old_pud);
+ old_ptl = pud_lockptr(mm, old_pud);
new_ptl = pud_lockptr(mm, new_pud);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
- /* Clear the pud */
+ double_pt_lock(old_ptl, new_ptl);
+
pud = *old_pud;
+ if (unlikely(pud_none(pud)))
+ goto out_unlock;
+
+ /* Clear the pud */
pud_clear(old_pud);
VM_BUG_ON(!pud_none(*new_pud));
pud_populate(mm, new_pud, pud_pgtable(pud));
flush_tlb_range(vma, pmc->old_addr, pmc->old_addr + PUD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+out_unlock:
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
@@ -499,18 +488,17 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,
if (WARN_ON_ONCE(!pud_none(*new_pud)))
return false;
- /*
- * We don't have to worry about the ordering of src and dst
- * ptlocks because exclusive mmap_lock prevents deadlock.
- */
- old_ptl = pud_lock(mm, old_pud);
+ old_ptl = pud_lockptr(mm, old_pud);
new_ptl = pud_lockptr(mm, new_pud);
- if (new_ptl != old_ptl)
- spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+
+ double_pt_lock(old_ptl, new_ptl);
+ if (unlikely(!pud_trans_huge(*old_pud))) {
+ double_pt_unlock(old_ptl, new_ptl);
+ return false;
+ }
/* Clear the pud */
- pud = *old_pud;
- pud_clear(old_pud);
+ pud = pudp_huge_get_and_clear(mm, pmc->old_addr, old_pud);
VM_BUG_ON(!pud_none(*new_pud));
@@ -518,9 +506,7 @@ static bool move_huge_pud(struct pagetable_move_control *pmc,
/* mark soft_ditry when we add pud level soft dirty support */
set_pud_at(mm, pmc->new_addr, new_pud, pud);
flush_pud_tlb_range(vma, pmc->old_addr, pmc->old_addr + HPAGE_PUD_SIZE);
- if (new_ptl != old_ptl)
- spin_unlock(new_ptl);
- spin_unlock(old_ptl);
+ double_pt_unlock(old_ptl, new_ptl);
return true;
}
@@ -842,11 +828,10 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc)
if (!new_pud)
break;
if (pud_trans_huge(*old_pud)) {
- if (extent == HPAGE_PUD_SIZE) {
- move_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud);
- /* We ignore and continue on error? */
+ if (extent == HPAGE_PUD_SIZE &&
+ move_pgt_entry(pmc, HPAGE_PUD, old_pud, new_pud))
continue;
- }
+ split_huge_pud(pmc->old, old_pud, pmc->old_addr);
} else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
if (move_pgt_entry(pmc, NORMAL_PUD, old_pud, new_pud))
continue;
diff --git a/mm/nommu.c b/mm/nommu.c
index ed44510e37707d..6e0f9c7da2364b 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1926,7 +1926,7 @@ subsys_initcall(init_admin_reserve);
int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
{
- mmap_write_lock(oldmm);
+ mmap_assert_write_locked(oldmm);
dup_mm_exe_file(mm, oldmm);
mmap_write_unlock(oldmm);
return 0;
diff --git a/mm/rmap.c b/mm/rmap.c
index 1131b76bbbc28b..0faef46806e5ca 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -924,6 +924,7 @@ static bool folio_referenced_one(struct folio *folio,
DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
int ptes = 0, referenced = 0;
unsigned int nr;
+ pte_t pteval;
while (page_vma_mapped_walk(&pvmw)) {
address = pvmw.address;
@@ -968,10 +969,12 @@ static bool folio_referenced_one(struct folio *folio,
return false;
}
- if (pvmw.pte && folio_test_large(folio)) {
+ if (pvmw.pte)
+ pteval = ptep_get(pvmw.pte);
+
+ if (pvmw.pte && pte_present(pteval) && folio_test_large(folio)) {
const unsigned long end_addr = pmd_addr_end(address, vma->vm_end);
const unsigned int max_nr = (end_addr - address) >> PAGE_SHIFT;
- pte_t pteval = ptep_get(pvmw.pte);
nr = folio_pte_batch(folio, pvmw.pte, pteval, max_nr);
}
@@ -981,11 +984,23 @@ static bool folio_referenced_one(struct folio *folio,
* are. —they could be on active/inactive lists or on MGLRU. So the
* simplest approach is to disable this look-around optimization.
*/
- if (lru_gen_enabled() && !lru_gen_switching() && pvmw.pte) {
+ if (lru_gen_enabled() && !lru_gen_switching() && pvmw.pte &&
+ pte_present(pteval)) {
if (lru_gen_look_around(&pvmw, nr))
referenced++;
} else if (pvmw.pte) {
- if (clear_flush_young_ptes_notify(vma, address, pvmw.pte, nr))
+ bool young = false;
+
+ /*
+ * PFN swap PTEs, such as device-exclusive ones, that
+ * actually map pages are "old" from a CPU perspective.
+ * The MMU notifier takes care of any device aspects.
+ */
+ if (pte_present(pteval))
+ young = clear_flush_young_ptes(vma, address, pvmw.pte, nr);
+ young |= mmu_notifier_clear_flush_young(vma->vm_mm, address,
+ address + nr * PAGE_SIZE);
+ if (young)
referenced++;
} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
if (pmdp_clear_flush_young_notify(vma, address,
diff --git a/mm/shmem.c b/mm/shmem.c
index fadf23f9e21edb..0d41aac125df9c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -275,6 +275,11 @@ static const struct vm_operations_struct shmem_vm_ops;
static const struct vm_operations_struct shmem_anon_vm_ops;
static struct file_system_type shmem_fs_type;
+bool file_inode_is_shmem(const struct inode *inode)
+{
+ return inode->i_fop == &shmem_file_operations;
+}
+
bool shmem_mapping(const struct address_space *mapping)
{
return mapping->a_ops == &shmem_aops;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 254ce86fa923e6..71ef9e2efb3a98 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2514,7 +2514,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
return 0;
swapcache = folio;
- folio = ksm_might_need_to_copy(folio, vma, addr);
+ folio = ksm_might_need_to_copy(folio,
+ folio_file_page(folio, swp_offset(entry)), vma, addr);
if (unlikely(!folio))
return -ENOMEM;
else if (unlikely(folio == ERR_PTR(-EHWPOISON))) {
diff --git a/mm/truncate.c b/mm/truncate.c
index 733d2713a875a5..6d8c0ec4623feb 100644
--- a/mm/truncate.c
+++ b/mm/truncate.c
@@ -499,7 +499,15 @@ void truncate_inode_pages_range(struct address_space *mapping,
folio_lock(folio);
VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
folio_wait_writeback(folio);
- truncate_inode_folio(mapping, folio);
+ if (truncate_inode_folio(mapping, folio)) {
+ folio_unlock(folio);
+ /*
+ * Folio might have been replaced: look it up
+ * again.
+ */
+ index = indices[i];
+ break;
+ }
folio_unlock(folio);
}
truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
@@ -760,7 +768,12 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
folio_lock(folio);
if (unlikely(folio->mapping != mapping)) {
folio_unlock(folio);
- continue;
+ /*
+ * Folio might have been replaced: look up
+ * again.
+ */
+ index = indices[i];
+ break;
}
VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
folio_wait_writeback(folio);
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 669f71d485d02e..76de84510340cf 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -1206,33 +1206,6 @@ int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
return 0;
}
-void double_pt_lock(spinlock_t *ptl1,
- spinlock_t *ptl2)
- __acquires(ptl1)
- __acquires(ptl2)
-{
- if (ptl1 > ptl2)
- swap(ptl1, ptl2);
- /* lock in virtual address order to avoid lock inversion */
- spin_lock(ptl1);
- if (ptl1 != ptl2)
- spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
- else
- __acquire(ptl2);
-}
-
-void double_pt_unlock(spinlock_t *ptl1,
- spinlock_t *ptl2)
- __releases(ptl1)
- __releases(ptl2)
-{
- spin_unlock(ptl1);
- if (ptl1 != ptl2)
- spin_unlock(ptl2);
- else
- __release(ptl2);
-}
-
static inline bool is_pte_pages_stable(pte_t *dst_pte, pte_t *src_pte,
pte_t orig_dst_pte, pte_t orig_src_pte,
pmd_t *dst_pmd, pmd_t dst_pmdval)
@@ -2270,6 +2243,9 @@ static struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
return ret;
}
+static void wake_userfault_range(struct userfaultfd_ctx *ctx,
+ unsigned long start, unsigned long end);
+
/* Assumes mmap write lock taken, and mm_struct pinned. */
static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
struct vm_area_struct *vma,
@@ -2287,6 +2263,8 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
prev = vma;
for_each_vma_range(vmi, vma, end) {
+ const bool any_mode_removed = vma->vm_flags & __VM_UFFD_FLAGS & ~vm_flags;
+
cond_resched();
VM_WARN_ON_ONCE(!vma_can_userfault(vma, vm_flags, wp_async));
@@ -2332,6 +2310,10 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
*/
userfaultfd_set_ctx(vma, ctx, vm_flags);
+ /* Retry faults for removed modes while the VMA is write-locked. */
+ if (any_mode_removed)
+ wake_userfault_range(ctx, start, vma_end);
+
if (vma_is_hugetlb(vma) && uffd_disable_huge_pmd_share(vma))
hugetlb_unshare_all_pmds(vma);
@@ -3648,6 +3630,17 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
__wake_userfault(ctx, range);
}
+static void wake_userfault_range(struct userfaultfd_ctx *ctx,
+ unsigned long start, unsigned long end)
+{
+ struct userfaultfd_wake_range range = {
+ .start = start,
+ .len = end - start,
+ };
+
+ wake_userfault(ctx, &range);
+}
+
static __always_inline int validate_unaligned_range(
struct mm_struct *mm, __u64 start, __u64 len)
{
@@ -3977,6 +3970,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
ret = 0;
for_each_vma_range(vmi, vma, end) {
+ struct userfaultfd_wake_range range;
+
cond_resched();
/* VMA not registered with userfaultfd. */
@@ -3991,18 +3986,17 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
start = vma->vm_start;
vma_end = min(end, vma->vm_end);
- if (userfaultfd_missing(vma)) {
- /*
- * Wake any concurrent pending userfault while
- * we unregister, so they will not hang
- * permanently and it avoids userland to call
- * UFFDIO_WAKE explicitly.
- */
- struct userfaultfd_wake_range range;
- range.start = start;
- range.len = vma_end - start;
- wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
- }
+ /* Prevent new userfaults from being queued after the wakeup. */
+ vma_start_write(vma);
+
+ /*
+ * Wake any concurrent pending userfault while we unregister,
+ * so they will not hang permanently and it avoids userland to
+ * call UFFDIO_WAKE explicitly.
+ */
+ range.start = start;
+ range.len = vma_end - start;
+ wake_userfault(ctx, &range);
vma = userfaultfd_clear_vma(&vmi, prev, vma,
start, vma_end);
diff --git a/mm/util.c b/mm/util.c
index 7f374bb8bf3635..67f8fca3b2056b 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -697,6 +697,19 @@ void *vcalloc_noprof(size_t n, size_t size)
}
EXPORT_SYMBOL(vcalloc_noprof);
+/**
+ * folio_anon_vma - Get a folio's raw anon_vma pointer.
+ * @folio: The folio.
+ *
+ * The returned pointer may be stale for an unmapped folio. The caller
+ * must ensure the anon_vma stays alive before dereferencing it. A folio
+ * reference or lock alone is insufficient; use folio_get_anon_vma() when
+ * the anon_vma's lifetime is not otherwise protected. Note that
+ * folio_get_anon_vma() is only useful for folios that are still mapped.
+ *
+ * Return: The raw anon_vma pointer for an anonymous, non-KSM folio, or
+ * %NULL otherwise.
+ */
struct anon_vma *folio_anon_vma(const struct folio *folio)
{
unsigned long mapping = (unsigned long)folio->mapping;
diff --git a/mm/vma.c b/mm/vma.c
index 1be613044b4a50..695d0be85ac864 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -516,6 +516,15 @@ void unmap_region(struct unmap_desc *unmap)
tlb_finish_mmu(&tlb);
}
+static int check_split(struct vm_area_struct *vma, unsigned long addr)
+{
+ if (!vma->vm_ops)
+ return 0;
+ if (!vma->vm_ops->may_split)
+ return 0;
+ return vma->vm_ops->may_split(vma, addr);
+}
+
/*
* __split_vma() bypasses sysctl_max_map_count checking. We use this where it
* has already been checked or doesn't make sense to fail.
@@ -532,11 +541,9 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
WARN_ON(vma->vm_start >= addr);
WARN_ON(vma->vm_end <= addr);
- if (vma->vm_ops && vma->vm_ops->may_split) {
- err = vma->vm_ops->may_split(vma, addr);
- if (err)
- return err;
- }
+ err = check_split(vma, addr);
+ if (err)
+ return err;
new = vm_area_dup(vma);
if (!new)
@@ -948,6 +955,10 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
if (vmg->__remove_middle && !can_merge_remove_vma(middle))
return NULL;
+ /* If middle isn't removed then it's resized, i.e. splits. */
+ if (!vmg->__remove_middle && check_split(middle, left_side ? end : start))
+ return NULL;
+
/*
* If we merge both VMAs, then next is also deleted. This implies
* merge_will_delete_vma also.
@@ -3380,7 +3391,8 @@ static int acct_stack_growth(struct vm_area_struct *vma,
* Overcommit.. This must be the final test, as it will
* update security statistics.
*/
- if (security_vm_enough_memory_mm(mm, grow))
+ if (vma_test(vma, VMA_ACCOUNT_BIT) &&
+ security_vm_enough_memory_mm(mm, grow))
return -ENOMEM;
return 0;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9959ca158ce835..6a1af9b92349fb 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3911,7 +3911,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
walk->mm_stats[MM_NONLEAF_FOUND]++;
- if (!walk_pte_range(&val, addr, next, args))
+ if (!walk_pte_range(pmd + i, addr, next, args))
continue;
walk->mm_stats[MM_NONLEAF_ADDED]++;
@@ -3947,7 +3947,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
continue;
- walk_pmd_range(&val, addr, next, args);
+ walk_pmd_range(pud + i, addr, next, args);
if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
end = (addr | ~PUD_MASK) + 1;
|
| 69/3 | 2026/10/27 03:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29674 output: 34 thoughts: 2178 Thoughts: **KMSAN Evaluation for Proposed Patch Series** Okay, let's break this down. My goal is to determine if this patch series warrants a dedicated KMSAN fuzzing session. Based on the provided guidelines, I need to focus on whether the changes introduce or modify code in a way that risks uninitialized memory usage or info-leaks, specifically those detectable *only* by KMSAN. I'll consider that the standard KASAN checks are already enabled, catching many other memory safety issues. First, I see several changes that seem primarily focused on improving code correctness, handling potential overflows, and refactoring. These include changes to `tlb.h` to use `unsigned long` for range sizes (good for preventing truncation) and parameter modifications in functions like `copy_huge_pud` and `ksm_might_need_to_copy`. Also, some code is being refactored to check arguments, as in the change to `fs/proc/task_mmu.c`, which makes sure that `start`, `end` and `vec` are valid 64-bit addresses, which doesn't seem directly related to uninitialized memory. The introduction of new helper functions like `pfn_has_dev_pagemap` and `file_inode_is_shmem` and the tweaks to mmu notifier error handling don't immediately jump out as KMSAN candidates either. Similarly, moving some locking primitives and adding some rmap/locking changes, like in the huge page code, seem fine, but are not on my priority. Changes in `mm/cma.c`, `mm/filemap.c`, and `mm/gup.c` mostly concern memory management and locking, and the main concern is correct ref counting and handling of possible races, which is not KMSAN's focus. However, I need to check `mm/gup.c` with more scrutiny. The changes in `kernel/fork.c`, involving earlier locking of `oldmm` and using `memcpy` of the `mm` struct, seem like a safe refactoring to minimize potential race conditions, but don't look directly related to KMSAN either. Now, wait... this change in `mm/gup.c` is interesting, specifically in the `memfd_pin_folios` function: The comment mentions that `fallocate()` *can* leave folios in the page cache that were *never cleared*. This is a potential KMSAN issue! The subsequent code checks `!folio_test_uptodate(folio)` and, if true, *zeroes* the folio's range and marks it as uptodate. This is exactly the kind of situation KMSAN is designed to detect: uninitialized memory being exposed. *This could be an info-leak!* If `fallocate()` can leave potentially uninitialized data in these folios, and `memfd_pin_folios` then "fixes" this by zeroing them, this suggests there was a window where userspace could potentially read uninitialized memory. Based on this, I need to investigate the `memfd_pin_folios` code and this particular scenario more closely. This *is* a KMSAN relevant change. *This means NeedsKMSAN = true* for this series. |
| 70/3 | 2026/10/27 03:23 | tool | grepper |
0mArgs:{
"Expression": "long memfd_pin_folios",
"PathPrefixes": [
"mm/gup.c"
]
}Results: {
"Output": "mm/gup.c=3429=EXPORT_SYMBOL(pin_user_pages_unlocked);\n--\nmm/gup.c-3456- */\nmm/gup.c:3457:long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,\nmm/gup.c-3458-\t\t struct folio **folios, unsigned int max_folios,\n"
} |
| 71/3 | 2026/10/27 03:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32698 output: 35 thoughts: 28 Thoughts: Okay, here's my interpretation of that thought process, assuming I'm already deep in kernel memory management. **Delving into `memfd_pin_folios`** Alright, I'm thinking, "Let's zero in on `memfd_pin_folios`". It's a key function in the `mm/gup.c` file. I'm recalling that the line number reference (3512) probably indicates it's a moderately sized function, not something trivial, but also not a monstrous beast. It's likely a critical piece in the overall memory management strategy, specifically related to getting pages and keeping them pinned for a memfd. I want to understand exactly how this function works, its place in the page fault handling flow, and what special scenarios it addresses. I need to refresh my knowledge of the memory management code at that part of the code base. |
| 72/3 | 2026/10/27 03:23 | tool | read-file |
0mArgs:{
"File": "mm/gup.c",
"FirstLine": 3495,
"LineCount": 60
}Results: {
"Contents": "3495:\t\n3496:\t\t\tfolio_batch_init(\u0026fbatch);\n3497:\t\t\twhile (start_idx \u003c= end_idx \u0026\u0026 nr_folios \u003c max_folios) {\n3498:\t\t\t\t/*\n3499:\t\t\t\t * In most cases, we should be able to find the folios\n3500:\t\t\t\t * in the page cache. If we cannot find them for some\n3501:\t\t\t\t * reason, we try to allocate them and add them to the\n3502:\t\t\t\t * page cache.\n3503:\t\t\t\t */\n3504:\t\t\t\tnr_found = filemap_get_folios_contig(memfd-\u003ef_mapping,\n3505:\t\t\t\t\t\t\t\t \u0026start_idx,\n3506:\t\t\t\t\t\t\t\t end_idx,\n3507:\t\t\t\t\t\t\t\t \u0026fbatch);\n3508:\t\t\t\tif (folio) {\n3509:\t\t\t\t\tfolio_put(folio);\n3510:\t\t\t\t\tfolio = NULL;\n3511:\t\t\t\t}\n3512:\t\n3513:\t\t\t\tfor (i = 0; i \u003c nr_found; i++) {\n3514:\t\t\t\t\tfolio = fbatch.folios[i];\n3515:\t\n3516:\t\t\t\t\tif (try_grab_folio(folio, 1, FOLL_PIN)) {\n3517:\t\t\t\t\t\tfolio_batch_release(\u0026fbatch);\n3518:\t\t\t\t\t\tret = -EINVAL;\n3519:\t\t\t\t\t\tgoto err;\n3520:\t\t\t\t\t}\n3521:\t\n3522:\t\t\t\t\t/*\n3523:\t\t\t\t\t * fallocate() can leave folios in the\n3524:\t\t\t\t\t * page cache that were never cleared.\n3525:\t\t\t\t\t */\n3526:\t\t\t\t\tif (!folio_test_uptodate(folio)) {\n3527:\t\t\t\t\t\tfolio_lock(folio);\n3528:\t\t\t\t\t\tif (!folio_test_uptodate(folio)) {\n3529:\t\t\t\t\t\t\tfolio_zero_range(folio, 0, folio_size(folio));\n3530:\t\t\t\t\t\t\tflush_dcache_folio(folio);\n3531:\t\t\t\t\t\t\tfolio_mark_uptodate(folio);\n3532:\t\t\t\t\t\t}\n3533:\t\t\t\t\t\tfolio_unlock(folio);\n3534:\t\t\t\t\t}\n3535:\t\n3536:\t\t\t\t\tif (nr_folios == 0)\n3537:\t\t\t\t\t\t*offset = offset_in_folio(folio, start);\n3538:\t\n3539:\t\t\t\t\tfolios[nr_folios] = folio;\n3540:\t\t\t\t\tif (++nr_folios == max_folios)\n3541:\t\t\t\t\t\tbreak;\n3542:\t\t\t\t}\n3543:\t\n3544:\t\t\t\tfolio = NULL;\n3545:\t\t\t\tfolio_batch_release(\u0026fbatch);\n3546:\t\t\t\tif (!nr_found) {\n3547:\t\t\t\t\tfolio = memfd_alloc_folio(memfd, start_idx);\n3548:\t\t\t\t\tif (IS_ERR(folio)) {\n3549:\t\t\t\t\t\tret = PTR_ERR(folio);\n3550:\t\t\t\t\t\tif (ret != -EEXIST)\n3551:\t\t\t\t\t\t\tgoto err;\n3552:\t\t\t\t\t\tfolio = NULL;\n3553:\t\t\t\t\t}\n3554:\t\t\t\t}\n"
} |
| 73/3 | 2026/10/27 03:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33783 output: 20 thoughts: 14 Thoughts: Here's how I'd approach summarizing that thought process, as a seasoned professional: **Pinpointing the Culprit Behind `memfd_pin_folios`** Alright, so the initial thought is pretty straightforward: I need to investigate the origins of calls to `memfd_pin_folios`. This suggests a specific problem or performance bottleneck related to memory file descriptors and folio pinning, something I'm certainly familiar with. The immediate goal is to identify the code responsible for triggering this system call. I need to understand *why* this is happening and, more importantly, whether it's expected behavior or a potential inefficiency. I'll likely need to trace the execution path from various points in the system, possibly using tools like `perf`, `systemtap`, or even simple `gdb` if I need to dig deep. I'll need to correlate the call to user-space activity, looking at the code, call stacks, and any relevant performance metrics to understand the context and frequency. The fact that I'm specifically thinking about this call implies I'm already somewhat aware that it's worth examining, meaning I have a suspicion that this is worth my time and attention. I will then analyze that, to see if the calls are necessary, and if there are optimization that can be made. |
| 74/3 | 2026/10/27 03:23 | tool | grepper |
0mArgs:{
"Expression": "memfd_pin_folios"
}Results: {
"Output": "drivers/dma-buf/udmabuf.c=304=static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,\n--\ndrivers/dma-buf/udmabuf.c-315-\tend = start + (pgcnt \u003c\u003c PAGE_SHIFT) - 1;\ndrivers/dma-buf/udmabuf.c:316:\tnr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, \u0026pgoff);\ndrivers/dma-buf/udmabuf.c-317-\tif (nr_folios \u003c= 0)\n--\ndrivers/dma-buf/udmabuf.c-337-\t\t * In a given range, only the first subpage of the first folio\ndrivers/dma-buf/udmabuf.c:338:\t\t * has an offset, that is returned by memfd_pin_folios().\ndrivers/dma-buf/udmabuf.c-339-\t\t * The first subpages of other folios (in the range) have an\n--\ndrivers/iommu/iommufd/pages.c=824=static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start,\n--\ndrivers/iommu/iommufd/pages.c-834-\t/*\ndrivers/iommu/iommufd/pages.c:835:\t * todo: memfd_pin_folios should return the last pinned offset so\ndrivers/iommu/iommufd/pages.c-836-\t * we can compute npages pinned, and avoid looping over folios here\n--\ndrivers/iommu/iommufd/pages.c-838-\t */\ndrivers/iommu/iommufd/pages.c:839:\tnfolios = memfd_pin_folios(user-\u003efile, start, end, user-\u003eufolios,\ndrivers/iommu/iommufd/pages.c-840-\t\t\t\t nfolios, \u0026offset);\n--\ninclude/linux/mm.h=3533=long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,\ninclude/linux/mm.h-3534-\t\t struct page **pages, unsigned int gup_flags);\ninclude/linux/mm.h:3535:long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,\ninclude/linux/mm.h-3536-\t\t struct folio **folios, unsigned int max_folios,\n--\nmm/gup.c=190=EXPORT_SYMBOL(unpin_user_page);\n--\nmm/gup.c-195- *\nmm/gup.c:196: * Folios that were pinned via memfd_pin_folios() or other similar routines\nmm/gup.c-197- * must be released either using unpin_folio() or unpin_folios().\n--\nmm/gup.c=3429=EXPORT_SYMBOL(pin_user_pages_unlocked);\n--\nmm/gup.c-3431-/**\nmm/gup.c:3432: * memfd_pin_folios() - pin folios associated with a memfd\nmm/gup.c-3433- * @memfd: the memfd whose folios are to be pinned\n--\nmm/gup.c-3456- */\nmm/gup.c:3457:long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,\nmm/gup.c-3458-\t\t struct folio **folios, unsigned int max_folios,\n--\nmm/gup.c-3567-}\nmm/gup.c:3568:EXPORT_SYMBOL_GPL(memfd_pin_folios);\nmm/gup.c-3569-\n--\nmm/gup.c-3579- * This function is helpful when having obtained a pin on a large folio\nmm/gup.c:3580: * using memfd_pin_folios(), but wanting to logically unpin parts\nmm/gup.c-3581- * (e.g., individual pages) of the folio later, for example, using\n--\nmm/memfd.c=38=static void memfd_tag_pins(struct xa_state *xas)\n--\nmm/memfd.c-64- * It is mainly called to allocate a folio in a memfd when the caller\nmm/memfd.c:65: * (memfd_pin_folios()) cannot find a folio in the page cache at a given\nmm/memfd.c-66- * index in the mapping.\n--\nmm/memfd_luo.c=87=static int memfd_luo_preserve_folios(struct file *file,\n--\nmm/memfd_luo.c-129-\t * that is really a problem in the future, we can have a\nmm/memfd_luo.c:130:\t * memfd_pin_folios() variant that does not allocate a page on empty\nmm/memfd_luo.c-131-\t * slots.\nmm/memfd_luo.c-132-\t */\nmm/memfd_luo.c:133:\tnr_pinned = memfd_pin_folios(file, 0, size - 1, folios, max_folios,\nmm/memfd_luo.c-134-\t\t\t\t \u0026offset);\n--\nmm/memfd_luo.c=258=static int memfd_luo_preserve(struct liveupdate_file_op_args *args)\n--\nmm/memfd_luo.c-291-\t/*\nmm/memfd_luo.c:292:\t * memfd_pin_folios() caps at UINT_MAX folios; refuse larger\nmm/memfd_luo.c-293-\t * files to avoid silently preserving only a prefix.\n"
} |
| 75/3 | 2026/10/27 03:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35587 output: 39 thoughts: 713 Thoughts: **Analyzing a Potential KMSAN Flag** Okay, hold on a minute. This `memfd_pin_folios` patch caught my eye. Specifically, this code block that zeros potentially "un-uptodate" folios. My initial thought is: is this a fix *preventing* uninitialized memory exposure, or is it *introducing* a new risk? That's the critical question here. Let's break down the rules for setting `NeedsKMSAN`. I remember the core principle: KMSAN is for *unique* risks of uninitialized memory. KASAN, LOCKDEP, and standard detectors handle the usual suspects. I need to figure out if this patch falls into the KMSAN-specific category. Let's recap the rules: is the patch introducing or modifying kernel structures sent to userspace with potentially uninitialized fields? Does it involve conditional logic depending on uninitialized variables, or does it allocate and initialize complex structures where uninitialized fields could be read later? Does it mess with bounds checks in a way that risks uninitialized byte exposure? Most importantly, I need to remember the rule: if the bug can be caught by KASAN, LOCKDEP, or standard bug detectors, then KMSAN is not required. So, let's look at the context. The code comments highlight a potential issue with `fallocate()` leaving folios uncleared. The patch explicitly addresses *that* scenario by zeroing them. This suggests we are *mitigating* uninitialized memory. Alright, before I jump to a conclusion, let's be thorough and check all the related changes in the diff. Let's look at every file affected. Okay, let's start with `fs/proc/task_mmu.c`. And let's check what `pagemap_scan_get_args` does in this context. |
| 76/3 | 2026/10/27 03:23 | tool | read-file |
0mArgs:{
"File": "fs/proc/task_mmu.c",
"FirstLine": 3015,
"LineCount": 40
}Results: {
"Contents": "3015:\t\t.pte_hole = pagemap_scan_pte_hole,\n3016:\t\t.hugetlb_entry = pagemap_scan_hugetlb_entry,\n3017:\t};\n3018:\t\n3019:\tstatic int pagemap_scan_get_args(struct pm_scan_arg *arg,\n3020:\t\t\t\t\t unsigned long uarg)\n3021:\t{\n3022:\t\tif (copy_from_user(arg, (void __user *)uarg, sizeof(*arg)))\n3023:\t\t\treturn -EFAULT;\n3024:\t\n3025:\t\tif (arg-\u003esize != sizeof(struct pm_scan_arg))\n3026:\t\t\treturn -EINVAL;\n3027:\t\n3028:\t\t/* Validate requested features */\n3029:\t\tif (arg-\u003eflags \u0026 ~PM_SCAN_FLAGS)\n3030:\t\t\treturn -EINVAL;\n3031:\t\tif ((arg-\u003ecategory_inverted | arg-\u003ecategory_mask |\n3032:\t\t arg-\u003ecategory_anyof_mask | arg-\u003ereturn_mask) \u0026 ~PM_SCAN_CATEGORIES)\n3033:\t\t\treturn -EINVAL;\n3034:\t\n3035:\t\t/* Don't cut 64-bit numbers down to addresses that happen to work. */\n3036:\t\tif (arg-\u003estart != (unsigned long)arg-\u003estart ||\n3037:\t\t arg-\u003eend != (unsigned long)arg-\u003eend ||\n3038:\t\t arg-\u003evec != (unsigned long)arg-\u003evec)\n3039:\t\t\treturn -EFAULT;\n3040:\t\n3041:\t\targ-\u003estart = untagged_addr((unsigned long)arg-\u003estart);\n3042:\t\targ-\u003eend = untagged_addr((unsigned long)arg-\u003eend);\n3043:\t\targ-\u003evec = untagged_addr((unsigned long)arg-\u003evec);\n3044:\t\n3045:\t\t/* Validate memory pointers */\n3046:\t\tif (!IS_ALIGNED(arg-\u003estart, PAGE_SIZE))\n3047:\t\t\treturn -EINVAL;\n3048:\t\tif (!access_ok((void __user *)(long)arg-\u003estart, arg-\u003eend - arg-\u003estart))\n3049:\t\t\treturn -EFAULT;\n3050:\t\tif (!arg-\u003evec \u0026\u0026 arg-\u003evec_len)\n3051:\t\t\treturn -EINVAL;\n3052:\t\tif (UINT_MAX == SIZE_MAX \u0026\u0026 arg-\u003evec_len \u003e SIZE_MAX)\n3053:\t\t\treturn -EINVAL;\n3054:\t\tif (arg-\u003evec \u0026\u0026 !access_ok((void __user *)(long)arg-\u003evec,\n"
} |
| 77/3 | 2026/10/27 03:23 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37425 output: 34 thoughts: 3636 Thoughts: **Unraveling Potential Uninitialized Memory and Memory Management Issues** Okay, let's break down these kernel memory management changes. My initial focus is always on where uninitialized memory *could* be introduced, but I'm also looking for concurrency problems and refcounting errors – the usual suspects in memory management code. In `pagemap_scan_get_args`, it appears to be a no-op, casting a 64-bit value to an unsigned long on 64-bit, and performing a zero-check on 32-bit. It's safe. Moving into `kernel/fork.c`, the `dup_mm` changes with the `mmap_write_lock_killable` is essential. The `memcpy` before the lock was an obvious race. The added locking *before* the `memcpy` and reinitialization of fields via `mm_init` is the correct approach to ensure consistency when duplicating the memory map. The unlock on failure is also correct. The `mm/mmap.c` change removing some `mm->` assignments is a bit deceptive, but the `memcpy` in `dup_mm` already copies these fields, so it's not a problem. Removing the duplicate assignments is a good cleanup. The CMA refcounting fix in `mm/cma.c` is straightforward, ensuring pages are correctly accounted for during freeing. No issues there. Now, in `mm/filemap.c`, the inlining of `filemap_free_folio` and the exclusive bit waiter fix are standard. The lock handling changes in `lock_folio_maybe_drop_mmap` are related to fault handling, specifically the retry path and, `filemap_fault`, which addresses races. The `gup` changes are a series of refinements, checking for underflow and missing presents. However, the `memfd_pin_folios` change is what I was interested in. `fallocate()` potentially creating uninitialized folios is a *vulnerability*. This fix *correctly* zeroes the folio before returning it to the caller, *preventing* a leak of uninitialized memory. This is a very welcome patch. In `mm/huge_memory.c`, the `do_huge_pmd_device_private` fix and signature changes in `copy_huge_pud` look reasonable. The `zap_huge_pmd_folio` is another important fix, ensuring that the young and dirty bits are transferred while the folio is still mapped, addressing potential lost state. `move_huge_pmd` using `double_pt_lock` is correct. The `split_huge_leaf` expansion is a logical progression, and the `deferred_split_scan` locking change is necessary. `mm/hugetlb.c` using `double_pt_lock` is as expected. `mm/internal.h` confirms the expected locking order. `mm/khugepaged.c` handling of clearing PTEs, checks and TLB flushes looks fine, and the pmd_lock ordering is as I would expect. `mm/ksm.c` fixes for the anon_vma checks and `merge_across_nodes_store` look correct. In `mm/madvise.c`, the KSM folio handling in `madvise_free_pte_range` *avoids* a bug by not clearing the dirty flag on KSM folios – good. In `mm/memfd.c` and `mm/memfd_luo.c`, the changes to use `file_inode_is_shmem` and the `FMODE_ATOMIC_POS` flag look reasonable. The check for `!inode->i_nlink` is correct. `mm/memory-tiers.c` is minor but important for resource management. Finally, in `mm/memory.c`, the use of `copy_huge_pud` is as expected. However, the addition of the `mm != current->mm` check in `__wp_page_copy_user` is crucial. The rejection of remote faults in `__wp_page_copy_user` and the EFAULT check in `wp_page_copy` is a safety net, which is the correct approach. Overall, these patches *mostly* address concurrency issues and minor optimizations. The most important one is the `memfd_pin_folios` fix which plugs a potential uninitialized memory leak. They are all necessary. |
| 78/3 | 2026/10/27 03:24 | tool | read-file |
0mArgs:{
"File": "mm/memory.c",
"FirstLine": 3755,
"LineCount": 40
}Results: {
"Contents": "3755:\t\tstruct mm_struct *mm = vma-\u003evm_mm;\n3756:\t\tunsigned long addr = vmf-\u003eaddress;\n3757:\t\n3758:\t\tif (likely(src)) {\n3759:\t\t\tif (copy_mc_user_highpage(dst, src, addr, vma))\n3760:\t\t\t\treturn -EHWPOISON;\n3761:\t\t\treturn 0;\n3762:\t\t}\n3763:\t\n3764:\t\t/*\n3765:\t\t * __copy_from_user_inatomic() only works as expected for local faults.\n3766:\t\t * Reject remote faults.\n3767:\t\t */\n3768:\t\tif (unlikely(mm != current-\u003emm))\n3769:\t\t\treturn -EFAULT;\n3770:\t\n3771:\t\t/*\n3772:\t\t * If the source page was a PFN mapping, we don't have\n3773:\t\t * a \"struct page\" for it. We do a best-effort copy by\n3774:\t\t * just copying from the original user address. If that\n3775:\t\t * fails, we just zero-fill it. Live with it.\n3776:\t\t */\n3777:\t\tkaddr = kmap_local_page(dst);\n3778:\t\tpagefault_disable();\n3779:\t\tuaddr = (void __user *)(addr \u0026 PAGE_MASK);\n3780:\t\n3781:\t\t/*\n3782:\t\t * On architectures with software \"accessed\" bits, we would\n3783:\t\t * take a double page fault, so mark it accessed here.\n3784:\t\t */\n3785:\t\tvmf-\u003epte = NULL;\n3786:\t\tif (!arch_has_hw_pte_young() \u0026\u0026 !pte_young(vmf-\u003eorig_pte)) {\n3787:\t\t\tpte_t entry;\n3788:\t\n3789:\t\t\tvmf-\u003epte = pte_offset_map_lock(mm, vmf-\u003epmd, addr, \u0026vmf-\u003eptl);\n3790:\t\t\tif (unlikely(!vmf-\u003epte || !pte_same(ptep_get(vmf-\u003epte), vmf-\u003eorig_pte))) {\n3791:\t\t\t\t/*\n3792:\t\t\t\t * Other thread has already handled the fault\n3793:\t\t\t\t * and update local tlb only\n3794:\t\t\t\t */\n"
} |
| 79/3 | 2026/10/27 03:24 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42690 output: 34 thoughts: 24 Thoughts: Okay, let's dive into this. Here's how I'd approach this task, as if these were my internal thoughts: **Analyzing `mm/memory.c` - Checking Specific Lines** Alright, I'm thinking we need to take a look at `mm/memory.c` again. I know this file inside and out, but sometimes you just need to re-verify assumptions or confirm a hunch. Specifically, I'm going to focus on lines 4080 through 4105. I've got a specific reason for examining that particular section. Based on the previous data, I have a suspicion about how memory allocation or deallocation might be handling something specific in that block of code. Could be a potential race condition, or perhaps I'm looking at how a specific data structure is being accessed. No point in making any assumptions until I see it on the page. Let's see if those lines hold the key. I expect I will see... [*I would continue with what I expect to see, but the text ends at the lines given, so this is where I would leave it*] |
| 80/3 | 2026/10/27 03:24 | tool | read-file |
0mArgs:{
"File": "mm/memory.c",
"FirstLine": 4080,
"LineCount": 30
}Results: {
"Contents": "4080:\t\t\tint err;\n4081:\t\n4082:\t\t\terr = __wp_page_copy_user(\u0026new_folio-\u003epage, vmf-\u003epage, vmf);\n4083:\t\t\tif (err) {\n4084:\t\t\t\t/*\n4085:\t\t\t\t * COW failed, if the fault was solved by other,\n4086:\t\t\t\t * it's fine. If not, userspace would re-fault on\n4087:\t\t\t\t * the same address and we will handle the fault\n4088:\t\t\t\t * from the second attempt.\n4089:\t\t\t\t * The -EHWPOISON case will not be retried.\n4090:\t\t\t\t */\n4091:\t\t\t\tfolio_put(new_folio);\n4092:\t\t\t\tif (old_folio)\n4093:\t\t\t\t\tfolio_put(old_folio);\n4094:\t\n4095:\t\t\t\tdelayacct_wpcopy_end();\n4096:\t\t\t\tif (err == -EFAULT)\n4097:\t\t\t\t\treturn VM_FAULT_SIGBUS;\n4098:\t\t\t\treturn err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;\n4099:\t\t\t}\n4100:\t\t\tkmsan_copy_page_meta(\u0026new_folio-\u003epage, vmf-\u003epage);\n4101:\t\t}\n4102:\t\n4103:\t\t__folio_mark_uptodate(new_folio);\n4104:\t\n4105:\t\tmmu_notifier_range_init(\u0026range, MMU_NOTIFY_CLEAR, 0, mm,\n4106:\t\t\t\t\tvmf-\u003eaddress \u0026 PAGE_MASK,\n4107:\t\t\t\t\t(vmf-\u003eaddress \u0026 PAGE_MASK) + PAGE_SIZE);\n4108:\t\tmmu_notifier_invalidate_range_start(\u0026range);\n4109:\t\n"
} |
| 81/3 | 2026/10/27 03:24 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43464 output: 22 thoughts: 1365 Thoughts: **Kernel Memory Management Insights** Okay, let's break down these memory management observations. Starting with `__wp_page_copy_user`, the `-EFAULT` return when dealing with a different memory map (`mm != current->mm`) makes sense. It correctly ensures we don't attempt an atomic copy from a different address space, leading to a `VM_FAULT_SIGBUS` and preventing potential security issues or unexpected behavior. Now, looking at `unmap_mapping_range` in `mm/memory.c`, the fix for the cast, particularly on 32-bit systems, is essential. Ensuring correct handling of `pgoff_t` and `loff_t` on different architectures is critical for consistent page offset calculations. This prevents potential truncation or incorrect offsets during unmapping. In `do_swap_page`, the `folio_put_swap` call seems straightforward, correctly handling the specific subpage, which is good. We're putting the swapped page back, and it's nice to see that it's correctly identifying it in the `swapcache`. Moving on to `handle_mm_fault`, the check for `is_droppable` and the `FAULT_FLAG_USER` flag is key. The comment explicitly states that OOM errors on droppable mappings shouldn't be fatal when originating from user space, as the user process has a chance to handle signals and retry. However, a kernel-level fault needs to propagate the OOM error, which seems like a sensible decision. Diving into `mm/mremap.c`, the locking strategy in `move_ptes` and the associated functions is noteworthy. The use of `double_pt_lock` for locking ensures proper synchronization when dealing with page table modifications during remap operations. The logic around splitting huge PUDs is also important, ensuring that large pages are correctly handled and split if necessary, especially when the extent doesn't match `HPAGE_PUD_SIZE` or if the move fails. In `mm/rmap.c`, `folio_referenced_one` has a few updates. The addition of `pteval` and the check for `pte_present` before accessing PTEs (especially for swap and device-exclusive PTEs) is a clear improvement. The handling of "old" PTEs (from a CPU perspective) and the use of `mmu_notifier_clear_flush_young` show that the code is considering how different subsystems interact with memory access. The goal is to set the `young` bit, indicating the page has been recently accessed, if present, avoiding issues with swap or device exclusive pages. The code in `mm/truncate.c`, particularly around `truncate_inode_folio`, is interesting. The retry logic if the folio is replaced is critical, ensuring that we don't operate on stale or invalid page references. It appears the retry loop handles cases where a folio is potentially swapped out or changed during the truncation process. Finally, in `mm/userfaultfd.c`, the `userfaultfd_register_range` behavior with the write lock and waking pending faults is important for managing concurrent operations. The initialization of the `range` struct (`range.start` and `range.len`) in `userfaultfd_unregister` makes sense, as a partially initialized struct would be dangerous. It ensures complete parameters are passed to `wake_userfault` when unregistering. This prevents race conditions and potential hangs. |
| 82/3 | 2026/10/27 03:24 | tool | grepper |
0mArgs:{
"Expression": "struct userfaultfd_wake_range"
}Results: {
"Output": "mm/userfaultfd.c=2410=struct userfaultfd_wait_queue {\n--\nmm/userfaultfd.c-2416-\nmm/userfaultfd.c:2417:struct userfaultfd_wake_range {\nmm/userfaultfd.c-2418-\tunsigned long start;\n--\nmm/userfaultfd.c=2465=static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,\n--\nmm/userfaultfd.c-2467-{\nmm/userfaultfd.c:2468:\tstruct userfaultfd_wake_range *range = key;\nmm/userfaultfd.c-2469-\tint ret;\n--\nmm/userfaultfd.c=3279=static int userfaultfd_release(struct inode *inode, struct file *file)\n--\nmm/userfaultfd.c-3283-\t/* len == 0 means wake all */\nmm/userfaultfd.c:3284:\tstruct userfaultfd_wake_range range = { .len = 0, };\nmm/userfaultfd.c-3285-\n--\nmm/userfaultfd.c=3589=static void __wake_userfault(struct userfaultfd_ctx *ctx,\nmm/userfaultfd.c:3590:\t\t\t struct userfaultfd_wake_range *range)\nmm/userfaultfd.c-3591-{\n--\nmm/userfaultfd.c=3602=static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,\nmm/userfaultfd.c:3603:\t\t\t\t\t struct userfaultfd_wake_range *range)\nmm/userfaultfd.c-3604-{\n--\nmm/userfaultfd.c=3633=static void wake_userfault_range(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-3635-{\nmm/userfaultfd.c:3636:\tstruct userfaultfd_wake_range range = {\nmm/userfaultfd.c-3637-\t\t.start = start,\n--\nmm/userfaultfd.c=3886=static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-3972-\tfor_each_vma_range(vmi, vma, end) {\nmm/userfaultfd.c:3973:\t\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-3974-\n--\nmm/userfaultfd.c=4024=static int userfaultfd_wake(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4028-\tstruct uffdio_range uffdio_wake;\nmm/userfaultfd.c:4029:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4030-\tconst void __user *buf = (void __user *)arg;\n--\nmm/userfaultfd.c=4056=static int userfaultfd_copy(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4061-\tstruct uffdio_copy __user *user_uffdio_copy;\nmm/userfaultfd.c:4062:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4063-\tuffd_flags_t flags = 0;\n--\nmm/userfaultfd.c=4116=static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4121-\tstruct uffdio_zeropage __user *user_uffdio_zeropage;\nmm/userfaultfd.c:4122:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4123-\n--\nmm/userfaultfd.c=4170=static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4175-\tstruct uffdio_writeprotect __user *user_uffdio_wp;\nmm/userfaultfd.c:4176:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4177-\tbool mode_wp, mode_dontwake;\n--\nmm/userfaultfd.c=4222=static int userfaultfd_rwprotect(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4226-\tstruct uffdio_rwprotect uffdio_rwp;\nmm/userfaultfd.c:4227:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4228-\tbool mode_rwp, mode_dontwake;\n--\nmm/userfaultfd.c=4299=static int userfaultfd_set_mode(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4360-\tif (mode.enable \u0026 UFFD_FEATURE_RWP_ASYNC) {\nmm/userfaultfd.c:4361:\t\tstruct userfaultfd_wake_range range = { .len = 0 };\nmm/userfaultfd.c-4362-\n--\nmm/userfaultfd.c=4374=static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg)\n--\nmm/userfaultfd.c-4378-\tstruct uffdio_continue __user *user_uffdio_continue;\nmm/userfaultfd.c:4379:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4380-\tuffd_flags_t flags = 0;\n--\nmm/userfaultfd.c=4435=static inline int userfaultfd_poison(struct userfaultfd_ctx *ctx, unsigned long arg)\n--\nmm/userfaultfd.c-4439-\tstruct uffdio_poison __user *user_uffdio_poison;\nmm/userfaultfd.c:4440:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4441-\n--\nmm/userfaultfd.c=4511=static int userfaultfd_move(struct userfaultfd_ctx *ctx,\n--\nmm/userfaultfd.c-4516-\tstruct uffdio_move __user *user_uffdio_move;\nmm/userfaultfd.c:4517:\tstruct userfaultfd_wake_range range;\nmm/userfaultfd.c-4518-\tstruct mm_struct *mm = ctx-\u003emm;\n"
} |
| 83/3 | 2026/10/27 03:24 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47282 output: 36 thoughts: 17 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, and delivered as a first-person summary: **Digging into `userfaultfd_wake_range`** Alright, let's get down to brass tacks. Time to refresh my understanding of `struct userfaultfd_wake_range`. I know I've worked with userfaultfd before, but it's always worth double-checking the specifics, especially when dealing with wake-up ranges. The nuances here are crucial for performance and correctness, so I need to make sure I'm solid on the definition. Need to ensure I am up to speed. |
| 84/3 | 2026/10/27 03:24 | tool | read-file |
0mArgs:{
"File": "mm/userfaultfd.c",
"FirstLine": 2415,
"LineCount": 10
}Results: {
"Contents": "2415:\t};\n2416:\t\n2417:\tstruct userfaultfd_wake_range {\n2418:\t\tunsigned long start;\n2419:\t\tunsigned long len;\n2420:\t};\n2421:\t\n2422:\t/* internal indication that UFFD_API ioctl was successfully executed */\n2423:\t#define UFFD_FEATURE_INITIALIZED\t\t(1u \u003c\u003c 31)\n2424:\t\n"
} |
| 85/3 | 2026/10/27 03:24 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47608 output: 32 thoughts: 497 Thoughts: **Analyzing Memory Management Wakeup Ranges and Page Table Walks** Okay, let's break this down. My initial focus is on the `struct userfaultfd_wake_range`. I see that it's straightforward; it's got a `start` and `len`, both initialized directly, and then it's used to trigger an internal waitqueue wakeup via `wake_userfault`. Seems simple enough at first glance. Next, I'm diving into `mm/vma.c`. I'm catching the introduction of this `check_split` helper and its use within `__split_vma` and `vma_merge_existing_range`, which seems like a sensible addition for handling VMA splits and merges. Also, the `acct_stack_growth` function now includes a check for `VMA_ACCOUNT_BIT` and the security memory policy. My core interest is around the interaction with the page table traversal logic, so I'm moving on to `mm/vmscan.c`. I immediately notice a change in the `walk_pte_range` and `walk_pmd_range` functions. I'm keen to understand the significance of this. Previously, the code was passing a local copy of the table entry (e.g., `val`) to `walk_pte_range` and `walk_pmd_range`. Now, it seems like we're passing a *pointer* to the table entry itself (e.g., `pmd + i` or `pud + i`). That's the key: it's no longer operating on a local copy but on the actual, live page table entry. I'm going to look into `walk_pte_range` and `walk_pmd_range` themselves to get a better sense of how this change influences their behavior. This shift has implications for how they interact with, and potentially modify, the underlying page table structures. |
| 86/3 | 2026/10/27 03:24 | tool | grepper |
0mArgs:{
"Expression": "walk_pte_range",
"PathPrefixes": [
"mm/vmscan.c"
]
}Results: {
"Output": "mm/vmscan.c=3655=static void walk_update_folio(struct lru_gen_mm_walk *walk, struct vm_area_struct *vma,\n--\nmm/vmscan.c-3680-\nmm/vmscan.c:3681:static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,\nmm/vmscan.c-3682-\t\t\t struct mm_walk *args)\n--\nmm/vmscan.c=3856=static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,\n--\nmm/vmscan.c-3877-restart:\nmm/vmscan.c:3878:\t/* walk_pte_range() may call get_next_vma() */\nmm/vmscan.c-3879-\tvma = args-\u003evma;\n--\nmm/vmscan.c-3913-\nmm/vmscan.c:3914:\t\tif (!walk_pte_range(pmd + i, addr, next, args))\nmm/vmscan.c-3915-\t\t\tcontinue;\n"
} |
| 87/3 | 2026/10/27 03:24 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48850 output: 470 thoughts: 2735 Thoughts: **KMSAN Reasoning: Detailed Analysis and Justification for NeedsKMSAN = false**
Okay, let's break this down. My initial concern regarding `pmd + i` versus `val` was valid; we're dealing with direct pointers to page table entries, so the direct pointer is absolutely critical for functions like `pte_offset_map` to work correctly. The initial check was correct. But that was just a simple example, let's move on.
Now, let's methodically go through this. I have to think like KMSAN is *always* on and *always* running. We need to be absolutely certain we're not introducing any uninitialized memory reads.
First, I need to check for anything exposed to user space, right? Any `copy_to_user`, `put_user`, netlink, ioctls, or BPF buffers where fields or padding might be uninitialized? Okay. `fs/proc/task_mmu.c`? `pagemap_scan_get_args`. Looks like *input* validation, which makes sense. They added checks. Okay, good. No copies there. `mm/userfaultfd.c`? Ah, `struct userfaultfd_wake_range range`. Internal to `wake_userfault`. That's not a direct user exposure. Fine. Next. `mm/gup.c`. `memfd_pin_folios`. Wait... This looks *important*. `fallocate` can leave folios uninitialized. They added a check and a zeroing operation if `folio_test_uptodate` is false, which means it **prevents** uninitialized memory exposure. Excellent.
Now, let's look for conditional logic or branching that might read uninitialized variables or struct fields.
`mm/rmap.c`: `pteval`. Okay, the code says:
```c
- if (pvmw.pte && folio_test_large(folio)) {
+ if (pvmw.pte)
+ pteval = ptep_get(pvmw.pte);
+
+ if (pvmw.pte && pte_present(pteval) && folio_test_large(folio)) {
```
The declaration `pte_t pteval;` is good, it says `pteval` exists, and it is a `pte_t`. Then, we can see the code has unconditionally initialized it to a value using `ptep_get` before it might ever be checked. This is good coding practice. Even if it's not a bug, it's good code. The code checks after the initialization to see if the value is valid.
Then `empty` in `mm/memory-tiers.c`: `bool empty = false;`. Initialized. Good.
`leaked` in `mm/cma.c`: `unsigned long leaked = 0;`. Also initialized.
`free_folio` in `mm/filemap.c`: This is a function pointer. Looks like the function pointer is read, and then assigned using `mapping->a_ops->free_folio;`. This assignment is unconditionally done before the function pointer is dereferenced. `free_folio` is either a real function or `NULL`. No uninitialized reads. Same in `delete_from_page_cache_batch`. All good.
`kernel/fork.c`: `dup_mm`. `mm` is allocated and then initialized before any `memcpy` or usage. Then `mm_init(mm, tsk)` is called. No potential for uninitialized memory.
`mm/mremap.c`: `pmd_t old_pmdval, new_pmdval;`. Both are *output* parameters to `pte_offset_map_rw_nolock`, meaning the function *must* initialize them before returning. Okay. They are initialized by the function and not the caller.
`mm/mmu_notifier.c`: `const bool has_itree = subscriptions->has_itree;`. Initialized. `int ret = 0;`. Initialized.
Alright, the rule is to only run KMSAN when the risk is uninitialized reads, which are *only* caught by KMSAN. I've been meticulous, haven't I? The patch series is largely about locking and concurrency fixes, page table walking optimizations, and memory management synchronization. There's no introduction of *new* structures being sent to user space where fields might be uninitialized. `pagemap_scan_get_args` actually adds *better* bounds checking. `memfd_pin_folios` proactively zeros potentially uninitialized folios. All local variables and struct fields I've checked are either initialized at declaration or are assigned values
|
| 88/3 | 2026/10/27 03:25 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series touches several core memory management subsystems (locking, page table manipulation, huge pages, KSM, mremap, and filemap/page cache management). None of these changes introduce or expose risks related to uninitialized memory:\n\n1. Initialization of variables and structures:\n - All newly introduced local variables across the modified functions (such as `range` in mm/userfaultfd.c, `pteval` in mm/rmap.c, `old_pmdval` and `new_pmdval` in mm/mremap.c, `leaked` in mm/cma.c, and `empty` in mm/memory-tiers.c) are fully initialized before use or assigned by functions prior to being evaluated.\n - In `kernel/fork.c`, `dup_mm` now acquires `mmap_write_lock_killable` prior to `memcpy(mm, oldmm, sizeof(*mm))`, ensuring consistent struct state during copy.\n\n2. Kernel-to-user info-leak and bounds handling:\n - No kernel structures with uninitialized fields or padding are copied or exposed to userspace.\n - In `fs/proc/task_mmu.c`, input validation is tightened to prevent 64-bit argument truncation on 32-bit platforms.\n - In `mm/gup.c`, `memfd_pin_folios()` explicitly adds `folio_zero_range()` for non-uptodate folios left behind by `fallocate()`, which resolves a potential leak of uninitialized pages rather than creating one.\n\n3. KASAN vs KMSAN applicability:\n - The primary risks of the modifications in this series involve concurrency, locking hierarchies (e.g., introduction and use of `double_pt_lock`), reference counting, and page table navigation.\n - These failure modes (use-after-free, deadlocks, race conditions, bad pointer dereferences) are effectively detected by KASAN, LOCKDEP, and standard kernel debugging tools. KMSAN provides no additional detection value for these changes.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series touches several core memory management subsystems (locking, page table manipulation, huge pages, KSM, mremap, and filemap/page cache management). None of these changes introduce or expose risks related to uninitialized memory:\n\n1. Initialization of variables and structures:\n - All newly introduced local variables across the modified functions (such as `range` in mm/userfaultfd.c, `pteval` in mm/rmap.c, `old_pmdval` and `new_pmdval` in mm/mremap.c, `leaked` in mm/cma.c, and `empty` in mm/memory-tiers.c) are fully initialized before use or assigned by functions prior to being evaluated.\n - In `kernel/fork.c`, `dup_mm` now acquires `mmap_write_lock_killable` prior to `memcpy(mm, oldmm, sizeof(*mm))`, ensuring consistent struct state during copy.\n\n2. Kernel-to-user info-leak and bounds handling:\n - No kernel structures with uninitialized fields or padding are copied or exposed to userspace.\n - In `fs/proc/task_mmu.c`, input validation is tightened to prevent 64-bit argument truncation on 32-bit platforms.\n - In `mm/gup.c`, `memfd_pin_folios()` explicitly adds `folio_zero_range()` for non-uptodate folios left behind by `fallocate()`, which resolves a potential leak of uninitialized pages rather than creating one.\n\n3. KASAN vs KMSAN applicability:\n - The primary risks of the modifications in this series involve concurrency, locking hierarchies (e.g., introduction and use of `double_pt_lock`), reference counting, and page table navigation.\n - These failure modes (use-after-free, deadlocks, race conditions, bad pointer dereferences) are effectively detected by KASAN, LOCKDEP, and standard kernel debugging tools. KMSAN provides no additional detection value for these changes.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|