| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/04 04:55 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"TEST_HMM",
"TRANSPARENT_HUGEPAGE",
"DEVICE_PRIVATE",
"ZONE_DEVICE",
"MEMORY_HOTPLUG",
"MEMORY_HOTREMOVE",
"SPARSEMEM_VMEMMAP",
"MIGRATION",
"DEVICE_MIGRATION",
"HMM_MIRROR"
],
"FocusSymbols": [
"hmm_range_fault",
"migrate_vma_setup",
"migrate_hmm_range_setup"
],
"Reasoning": "The patch introduces a new feature to migrate memory via HMM during a page fault. It modifies core HMM and memory migration logic, which is reachable via the test_hmm driver or other HMM users. It adds new flags and modifies the behavior of hmm_range_fault and migrate_vma_setup. These are functional changes to core kernel memory management and should be fuzzed.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/04 04:55 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 3a66202d0dc29fc656a6f451bf16a7b6faa27f33\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Tue Aug 4 04:55:13 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/hmm.h b/include/linux/hmm.h\nindex db75ffc949a7a..fbc9bb74d1608 100644\n--- a/include/linux/hmm.h\n+++ b/include/linux/hmm.h\n@@ -13,6 +13,8 @@\n \n struct mmu_interval_notifier;\n \n+struct migrate_vma;\n+\n /*\n * On output:\n * 0 - The page is faultable and a future call with \n@@ -27,6 +29,13 @@ struct mmu_interval_notifier;\n * HMM_PFN_P2PDMA_BUS - Bus mapped P2P transfer\n * HMM_PFN_DMA_MAPPED - Flag preserved on input-to-output transformation\n * to mark that page is already DMA mapped\n+ * HMM_PFN_MIGRATE - The entry is to be migrated. Note, HMM_PFN_MIGRATE\n+ * alone without HMM_PFN_VALID denotes the\n+ * empty page.\n+ * This flag together with HMM_PFN_COMPOUND are\n+ * indicators for migrate_hmm_range_setup() to\n+ * setup the migrate pfns.\n+ * HMM_PFN_COMPOUND - The entry is represents a \u003e 0 order page\n *\n * On input:\n * 0 - Return the current state of the page, do not fault it.\n@@ -34,6 +43,8 @@ struct mmu_interval_notifier;\n * will fail\n * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()\n * will fail. Must be combined with HMM_PFN_REQ_FAULT.\n+ * HMM_PFN_REQ_MIGRATE - For default_flags, request to migrate, according to\n+ * hmm_range.migrate.flags\n */\n enum hmm_pfn_flags {\n \t/* Output fields and flags */\n@@ -48,11 +59,15 @@ enum hmm_pfn_flags {\n \tHMM_PFN_P2PDMA = 1UL \u003c\u003c (BITS_PER_LONG - 5),\n \tHMM_PFN_P2PDMA_BUS = 1UL \u003c\u003c (BITS_PER_LONG - 6),\n \n-\tHMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 11),\n+\t/* Migrate request */\n+\tHMM_PFN_MIGRATE = 1UL \u003c\u003c (BITS_PER_LONG - 7),\n+\tHMM_PFN_COMPOUND = 1UL \u003c\u003c (BITS_PER_LONG - 8),\n+\tHMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 13),\n \n \t/* Input flags */\n \tHMM_PFN_REQ_FAULT = HMM_PFN_VALID,\n \tHMM_PFN_REQ_WRITE = HMM_PFN_WRITE,\n+\tHMM_PFN_REQ_MIGRATE = HMM_PFN_MIGRATE,\n \n \tHMM_PFN_FLAGS = ~((1UL \u003c\u003c HMM_PFN_ORDER_SHIFT) - 1),\n };\n@@ -96,6 +111,28 @@ static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)\n \treturn (hmm_pfn \u003e\u003e HMM_PFN_ORDER_SHIFT) \u0026 0x1F;\n }\n \n+/*\n+ * hmm_pfn_collected() - is this pfn entry prepared for migration ?\n+ * If collected the folio's refcount is increased and the folio\n+ * is locked.\n+ */\n+static inline bool hmm_pfn_collected(unsigned long hmm_pfn)\n+{\n+\treturn (hmm_pfn \u0026 (HMM_PFN_VALID | HMM_PFN_MIGRATE)) ==\n+\t\t(HMM_PFN_VALID | HMM_PFN_MIGRATE);\n+}\n+\n+/*\n+ * hmm_pfn_rollback() - undoes the collecction of hmm_pfn\n+ *\n+ * Note for total rollback the folio's refcount has to be put\n+ * and folio has to be unlocked.\n+ */\n+static inline unsigned long hmm_pfn_rollback_collected(unsigned long hmm_pfn)\n+{\n+\treturn hmm_pfn \u0026 ~(HMM_PFN_VALID | HMM_PFN_MIGRATE | HMM_PFN_COMPOUND);\n+}\n+\n /*\n * struct hmm_range - track invalidation lock on virtual address range\n *\n@@ -107,6 +144,7 @@ static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)\n * @default_flags: default flags for the range (write, read, ... see hmm doc)\n * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter\n * @dev_private_owner: owner of device private pages\n+ * @migrate: structure for migrating a range of a VMA\n */\n struct hmm_range {\n \tstruct mmu_interval_notifier *notifier;\n@@ -117,6 +155,7 @@ struct hmm_range {\n \tunsigned long\t\tdefault_flags;\n \tunsigned long\t\tpfn_flags_mask;\n \tvoid\t\t\t*dev_private_owner;\n+\tstruct migrate_vma *migrate;\n };\n \n /*\ndiff --git a/include/linux/migrate.h b/include/linux/migrate.h\nindex d5af2b7f577b3..2b137e6a56d0f 100644\n--- a/include/linux/migrate.h\n+++ b/include/linux/migrate.h\n@@ -3,6 +3,7 @@\n #define _LINUX_MIGRATE_H\n \n #include \u003clinux/mm.h\u003e\n+#include \u003clinux/hmm.h\u003e\n #include \u003clinux/mempolicy.h\u003e\n #include \u003clinux/migrate_mode.h\u003e\n #include \u003clinux/hugetlb.h\u003e\n@@ -105,6 +106,28 @@ static inline void softleaf_entry_wait_on_locked(softleaf_t entry, spinlock_t *p\n \tspin_unlock(ptl);\n }\n \n+enum migrate_vma_info {\n+\tMIGRATE_VMA_SELECT_NONE = 0,\n+\tMIGRATE_VMA_SELECT_COMPOUND = MIGRATE_VMA_SELECT_NONE,\n+};\n+\n+static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range)\n+{\n+\treturn MIGRATE_VMA_SELECT_NONE;\n+}\n+\n+static inline void hmm_fill_migrate_vma(struct hmm_range *range,\n+\t\t\t\t\tstruct vm_area_struct *vma,\n+\t\t\t\t\tunsigned long start,\n+\t\t\t\t\tunsigned long end)\n+{\n+}\n+\n+static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range)\n+{\n+\treturn range-\u003enotifier-\u003emm;\n+}\n+\n #endif /* CONFIG_MIGRATION */\n \n #ifdef CONFIG_NUMA_BALANCING\n@@ -148,11 +171,13 @@ static inline unsigned long migrate_pfn(unsigned long pfn)\n \treturn (pfn \u003c\u003c MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID;\n }\n \n-enum migrate_vma_direction {\n+enum migrate_vma_info {\n \tMIGRATE_VMA_SELECT_SYSTEM = 1 \u003c\u003c 0,\n \tMIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 \u003c\u003c 1,\n \tMIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 \u003c\u003c 2,\n \tMIGRATE_VMA_SELECT_COMPOUND = 1 \u003c\u003c 3,\n+\tMIGRATE_VMA_FAULT = 1 \u003c\u003c 4,\n+\tMIGRATE_VMA_WRITE = 1 \u003c\u003c 5,\n };\n \n struct migrate_vma {\n@@ -190,6 +215,31 @@ struct migrate_vma {\n \tstruct page\t\t*fault_page;\n };\n \n+static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range)\n+{\n+\tenum migrate_vma_info minfo;\n+\n+\tminfo = (range-\u003edefault_flags \u0026 HMM_PFN_REQ_MIGRATE) ?\n+\t\trange-\u003emigrate-\u003eflags : 0;\n+\n+\treturn minfo;\n+}\n+\n+static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range)\n+{\n+\treturn range-\u003enotifier ? range-\u003enotifier-\u003emm : range-\u003emigrate-\u003evma-\u003evm_mm;\n+}\n+\n+static inline void hmm_fill_migrate_vma(struct hmm_range *range,\n+\t\t\t\t\tstruct vm_area_struct *vma,\n+\t\t\t\t\tunsigned long start,\n+\t\t\t\t\tunsigned long end)\n+{\n+\trange-\u003emigrate-\u003evma = vma;\n+\trange-\u003emigrate-\u003estart = start;\n+\trange-\u003emigrate-\u003eend = end;\n+}\n+\n int migrate_vma_setup(struct migrate_vma *args);\n void migrate_vma_pages(struct migrate_vma *migrate);\n void migrate_vma_finalize(struct migrate_vma *migrate);\n@@ -200,7 +250,7 @@ void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,\n \t\t\tunsigned long npages);\n void migrate_device_finalize(unsigned long *src_pfns,\n \t\t\tunsigned long *dst_pfns, unsigned long npages);\n-\n+void migrate_hmm_range_setup(struct hmm_range *range);\n #endif /* CONFIG_MIGRATION */\n \n #endif /* _LINUX_MIGRATE_H */\ndiff --git a/lib/test_hmm.c b/lib/test_hmm.c\nindex c4adbf98fac79..46a83e0cb7091 100644\n--- a/lib/test_hmm.c\n+++ b/lib/test_hmm.c\n@@ -36,6 +36,7 @@\n #define DMIRROR_RANGE_FAULT_TIMEOUT\t1000\n #define DEVMEM_CHUNK_SIZE\t\t(256 * 1024 * 1024U)\n #define DEVMEM_CHUNKS_RESERVE\t\t16\n+#define PFNS_ARRAY_SIZE\t\t\t64\n \n /*\n * For device_private pages, dpage is just a dummy struct page\n@@ -145,7 +146,7 @@ static bool dmirror_is_private_zone(struct dmirror_device *mdevice)\n \t\tHMM_DMIRROR_MEMORY_DEVICE_PRIVATE);\n }\n \n-static enum migrate_vma_direction\n+static enum migrate_vma_info\n dmirror_select_device(struct dmirror *dmirror)\n {\n \treturn (dmirror-\u003emdevice-\u003ezone_device_type ==\n@@ -355,6 +356,7 @@ static int dmirror_range_fault(struct dmirror *dmirror,\n \tstruct mm_struct *mm = dmirror-\u003enotifier.mm;\n \tunsigned long timeout =\n \t\tjiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);\n+\tbool migrate = range-\u003edefault_flags \u0026 HMM_PFN_REQ_MIGRATE;\n \tint ret;\n \n \twhile (true) {\n@@ -364,9 +366,15 @@ static int dmirror_range_fault(struct dmirror *dmirror,\n \t\t}\n \n \t\trange-\u003enotifier_seq = mmu_interval_read_begin(range-\u003enotifier);\n-\t\tmmap_read_lock(mm);\n-\t\tret = hmm_range_fault(range);\n-\t\tmmap_read_unlock(mm);\n+\n+\t\t/* mmap lock held for whole migrate on fault operation */\n+\t\tif (!migrate) {\n+\t\t\tmmap_read_lock(mm);\n+\t\t\tret = hmm_range_fault(range);\n+\t\t\tmmap_read_unlock(mm);\n+\t\t} else {\n+\t\t\tret = hmm_range_fault(range);\n+\t\t}\n \t\tif (ret) {\n \t\t\tif (ret == -EBUSY)\n \t\t\t\tcontinue;\n@@ -382,7 +390,9 @@ static int dmirror_range_fault(struct dmirror *dmirror,\n \t\tbreak;\n \t}\n \n-\tret = dmirror_do_fault(dmirror, range);\n+\t/* update device page table after migration */\n+\tif (!migrate)\n+\t\tret = dmirror_do_fault(dmirror, range);\n \n \tmutex_unlock(\u0026dmirror-\u003emutex);\n out:\n@@ -1281,6 +1291,114 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,\n \treturn ret;\n }\n \n+static int do_fault_and_migrate(struct dmirror *dmirror, struct hmm_range *range)\n+{\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\tint ret;\n+\n+\tmmap_read_lock(dmirror-\u003enotifier.mm);\n+\n+\t/* Fault-in pages for migration */\n+\tret = dmirror_range_fault(dmirror, range);\n+\t/*\n+\t * Set this up even on error: hmm_range_fault() may have collected\n+\t * part of the range before failing.\n+\t */\n+\tmigrate_hmm_range_setup(range);\n+\tif (ret) {\n+\t\t/*\n+\t\t * dst[] entries are empty, so this marks every collected migration\n+\t\t * as failed. finalize then restores the source mappings and drops\n+\t\t * the associated locks/references.\n+\t\t */\n+\t\tmigrate_vma_pages(migrate);\n+\t\tmigrate_vma_finalize(migrate);\n+\t\tgoto out;\n+\t}\n+\n+\tpr_debug(\"Migrating from sys mem to device mem\\n\");\n+\n+\tdmirror_migrate_alloc_and_copy(migrate, dmirror);\n+\tmigrate_vma_pages(migrate);\n+\tdmirror_migrate_finalize_and_map(migrate, dmirror);\n+\tmigrate_vma_finalize(migrate);\n+out:\n+\tmmap_read_unlock(dmirror-\u003enotifier.mm);\n+\treturn ret;\n+}\n+\n+static int dmirror_fault_and_migrate_to_device(struct dmirror *dmirror,\n+\t\t\t\t\t struct hmm_dmirror_cmd *cmd)\n+{\n+\tunsigned long start, size, end, next;\n+\tunsigned long src_pfns[PFNS_ARRAY_SIZE] = { 0 };\n+\tunsigned long dst_pfns[PFNS_ARRAY_SIZE] = { 0 };\n+\tstruct migrate_vma migrate = { 0 };\n+\tstruct hmm_range range = { 0 };\n+\tstruct dmirror_bounce bounce;\n+\tint ret = 0;\n+\n+\t/* Whole range */\n+\tstart = cmd-\u003eaddr;\n+\tsize = cmd-\u003enpages \u003c\u003c PAGE_SHIFT;\n+\tend = start + size;\n+\n+\tif (!mmget_not_zero(dmirror-\u003enotifier.mm)) {\n+\t\tret = -EFAULT;\n+\t\tgoto out;\n+\t}\n+\n+\tmigrate.pgmap_owner = dmirror-\u003emdevice;\n+\tmigrate.src = src_pfns;\n+\tmigrate.dst = dst_pfns;\n+\tmigrate.flags = MIGRATE_VMA_SELECT_SYSTEM;\n+\n+\trange.migrate = \u0026migrate;\n+\trange.hmm_pfns = src_pfns;\n+\trange.pfn_flags_mask = 0;\n+\trange.default_flags = HMM_PFN_REQ_FAULT | HMM_PFN_REQ_MIGRATE;\n+\trange.dev_private_owner = dmirror-\u003emdevice;\n+\trange.notifier = \u0026dmirror-\u003enotifier;\n+\n+\tfor (next = start; next \u003c end; next = range.end) {\n+\t\trange.start = next;\n+\t\trange.end = min(end, next + (PFNS_ARRAY_SIZE \u003c\u003c PAGE_SHIFT));\n+\n+\t\tpr_debug(\"Fault and migrate range start:%#lx end:%#lx\\n\",\n+\t\t\t range.start, range.end);\n+\n+\t\tret = do_fault_and_migrate(dmirror, \u0026range);\n+\t\tif (ret)\n+\t\t\tgoto out_mmput;\n+\t}\n+\n+\t/*\n+\t * Return the migrated data for verification.\n+\t * Only for pages in device zone\n+\t */\n+\tret = dmirror_bounce_init(\u0026bounce, start, size);\n+\tif (ret)\n+\t\tgoto out_mmput;\n+\n+\tmutex_lock(\u0026dmirror-\u003emutex);\n+\tret = dmirror_do_read(dmirror, start, end, \u0026bounce);\n+\tmutex_unlock(\u0026dmirror-\u003emutex);\n+\tif (ret == 0) {\n+\t\tret = copy_to_user(u64_to_user_ptr(cmd-\u003eptr), bounce.ptr, bounce.size);\n+\t\tif (ret)\n+\t\t\tret = -EFAULT;\n+\t}\n+\n+\tcmd-\u003ecpages = bounce.cpages;\n+\tdmirror_bounce_fini(\u0026bounce);\n+\n+\n+out_mmput:\n+\tmmput(dmirror-\u003enotifier.mm);\n+out:\n+\treturn ret;\n+}\n+\n static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range,\n \t\t\t unsigned char *perm, unsigned long entry)\n {\n@@ -1547,6 +1665,10 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,\n \t\tret = dmirror_migrate_to_device(dmirror, \u0026cmd);\n \t\tbreak;\n \n+\tcase HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV:\n+\t\tret = dmirror_fault_and_migrate_to_device(dmirror, \u0026cmd);\n+\t\tbreak;\n+\n \tcase HMM_DMIRROR_MIGRATE_TO_SYS:\n \t\tret = dmirror_migrate_to_system(dmirror, \u0026cmd);\n \t\tbreak;\ndiff --git a/lib/test_hmm_uapi.h b/lib/test_hmm_uapi.h\nindex f94c6d4573382..0b6e7a419e365 100644\n--- a/lib/test_hmm_uapi.h\n+++ b/lib/test_hmm_uapi.h\n@@ -29,15 +29,16 @@ struct hmm_dmirror_cmd {\n };\n \n /* Expose the address space of the calling process through hmm device file */\n-#define HMM_DMIRROR_READ\t\t_IOWR('H', 0x00, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_WRITE\t\t_IOWR('H', 0x01, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_MIGRATE_TO_DEV\t_IOWR('H', 0x02, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_MIGRATE_TO_SYS\t_IOWR('H', 0x03, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_SNAPSHOT\t\t_IOWR('H', 0x04, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_EXCLUSIVE\t\t_IOWR('H', 0x05, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_CHECK_EXCLUSIVE\t_IOWR('H', 0x06, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_RELEASE\t\t_IOWR('H', 0x07, struct hmm_dmirror_cmd)\n-#define HMM_DMIRROR_FLAGS\t\t_IOWR('H', 0x08, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_READ\t\t\t_IOWR('H', 0x00, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_WRITE\t\t\t_IOWR('H', 0x01, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_MIGRATE_TO_DEV\t\t_IOWR('H', 0x02, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV\t_IOWR('H', 0x03, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_MIGRATE_TO_SYS\t\t_IOWR('H', 0x04, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_SNAPSHOT\t\t\t_IOWR('H', 0x05, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_EXCLUSIVE\t\t\t_IOWR('H', 0x06, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_CHECK_EXCLUSIVE\t\t_IOWR('H', 0x07, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_RELEASE\t\t\t_IOWR('H', 0x08, struct hmm_dmirror_cmd)\n+#define HMM_DMIRROR_FLAGS\t\t\t_IOWR('H', 0x09, struct hmm_dmirror_cmd)\n \n #define HMM_DMIRROR_FLAG_FAIL_ALLOC\t(1ULL \u003c\u003c 0)\n \ndiff --git a/mm/Kconfig b/mm/Kconfig\nindex 9e0ca48249054..7647671f37f09 100644\n--- a/mm/Kconfig\n+++ b/mm/Kconfig\n@@ -699,6 +699,7 @@ config MIGRATION\n \n config DEVICE_MIGRATION\n \tdef_bool MIGRATION \u0026\u0026 ZONE_DEVICE\n+\tselect HMM_MIRROR\n \n config ARCH_ENABLE_HUGEPAGE_MIGRATION\n \tbool\n@@ -1260,6 +1261,7 @@ config ZONE_DEVICE\n config HMM_MIRROR\n \tbool\n \tdepends on MMU\n+\tselect MMU_NOTIFIER\n \n config GET_FREE_REGION\n \tbool\ndiff --git a/mm/hmm.c b/mm/hmm.c\nindex c72c9ddfdb2f6..53df67bbd87ef 100644\n--- a/mm/hmm.c\n+++ b/mm/hmm.c\n@@ -20,6 +20,7 @@\n #include \u003clinux/pagemap.h\u003e\n #include \u003clinux/leafops.h\u003e\n #include \u003clinux/hugetlb.h\u003e\n+#include \u003clinux/migrate.h\u003e\n #include \u003clinux/memremap.h\u003e\n #include \u003clinux/sched/mm.h\u003e\n #include \u003clinux/jump_label.h\u003e\n@@ -27,14 +28,44 @@\n #include \u003clinux/pci-p2pdma.h\u003e\n #include \u003clinux/mmu_notifier.h\u003e\n #include \u003clinux/memory_hotplug.h\u003e\n+#include \u003casm/tlbflush.h\u003e\n \n #include \"internal.h\"\n \n struct hmm_vma_walk {\n-\tstruct hmm_range\t*range;\n-\tunsigned long\t\tlast;\n+\tstruct mmu_notifier_range\tmmu_range;\n+\tstruct vm_area_struct\t\t*vma;\n+\tstruct hmm_range\t\t*range;\n+\tunsigned long\t\t\tstart;\n+\tunsigned long\t\t\tend;\n+\tunsigned long\t\t\tlast;\n+\t/*\n+\t * For migration we need pte/pmd\n+\t * locked for the handle_* and\n+\t * prepare_* regions. While faulting\n+\t * we have to drop the locks and\n+\t * start again.\n+\t * ptelocked and pmdlocked\n+\t * hold the state and tells if need\n+\t * to drop locks before faulting.\n+\t * ptl is the lock held for pte or pmd.\n+\t *\n+\t */\n+\tbool\t\t\t\tptelocked;\n+\tbool\t\t\t\tpmdlocked;\n+\tspinlock_t\t\t\t*ptl;\n };\n \n+#define HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, locked)\t\t\\\n+\tWARN_ON_ONCE((hmm_vma_walk)-\u003eptelocked != locked)\n+\n+#define HMM_ASSERT_PMD_LOCKED(hmm_vma_walk, locked)\t\t\\\n+\tWARN_ON_ONCE((hmm_vma_walk)-\u003epmdlocked != locked)\n+\n+#define HMM_ASSERT_UNLOCKED(hmm_vma_walk)\t\t\t\\\n+\tWARN_ON_ONCE((hmm_vma_walk)-\u003eptelocked ||\t\t\\\n+\t\t (hmm_vma_walk)-\u003epmdlocked)\n+\n enum {\n \tHMM_NEED_FAULT = 1 \u003c\u003c 0,\n \tHMM_NEED_WRITE_FAULT = 1 \u003c\u003c 1,\n@@ -47,15 +78,46 @@ enum {\n \t\t\t HMM_PFN_P2PDMA_BUS,\n };\n \n+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,\n+\t\t\t\t\t\t unsigned long start,\n+\t\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t\t unsigned long *hmm_pfn);\n+\n static int hmm_pfns_fill(unsigned long addr, unsigned long end,\n-\t\t\t struct hmm_range *range, unsigned long cpu_flags)\n+\t\t\t struct hmm_vma_walk *hmm_vma_walk, unsigned long cpu_flags)\n {\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n \tunsigned long i = (addr - range-\u003estart) \u003e\u003e PAGE_SHIFT;\n+\tenum migrate_vma_info minfo;\n+\tbool migrate = false;\n+\n+\tminfo = hmm_select_migrate(range);\n+\tif (cpu_flags != HMM_PFN_ERROR) {\n+\t\tif (minfo \u0026\u0026 (vma_is_anonymous(hmm_vma_walk-\u003evma))) {\n+\t\t\tcpu_flags |= HMM_PFN_MIGRATE;\n+\t\t\tmigrate = true;\n+\t\t}\n+\t}\n+\n+\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr, end, \u0026range-\u003ehmm_pfns[i]);\n+\n+\tif (migrate \u0026\u0026 thp_migration_supported() \u0026\u0026\n+\t (minfo \u0026 MIGRATE_VMA_SELECT_COMPOUND) \u0026\u0026\n+\t IS_ALIGNED(addr, HPAGE_PMD_SIZE) \u0026\u0026\n+\t IS_ALIGNED(end, HPAGE_PMD_SIZE) \u0026\u0026\n+\t\tend-addr == HPAGE_PMD_SIZE) {\n+\t\trange-\u003ehmm_pfns[i] \u0026= HMM_PFN_INOUT_FLAGS;\n+\t\trange-\u003ehmm_pfns[i] |= cpu_flags | HMM_PFN_COMPOUND;\n+\t\taddr += PAGE_SIZE;\n+\t\ti++;\n+\t\tcpu_flags = 0;\n+\t}\n \n \tfor (; addr \u003c end; addr += PAGE_SIZE, i++) {\n \t\trange-\u003ehmm_pfns[i] \u0026= HMM_PFN_INOUT_FLAGS;\n \t\trange-\u003ehmm_pfns[i] |= cpu_flags;\n \t}\n+\n \treturn 0;\n }\n \n@@ -78,6 +140,7 @@ static int hmm_vma_fault(unsigned long addr, unsigned long end,\n \tunsigned int fault_flags = FAULT_FLAG_REMOTE;\n \n \tWARN_ON_ONCE(!required_fault);\n+\tHMM_ASSERT_UNLOCKED(hmm_vma_walk);\n \thmm_vma_walk-\u003elast = addr;\n \n \tif (required_fault \u0026 HMM_NEED_WRITE_FAULT) {\n@@ -171,11 +234,16 @@ static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,\n \tif (!walk-\u003evma) {\n \t\tif (required_fault)\n \t\t\treturn -EFAULT;\n-\t\treturn hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);\n+\t\treturn hmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR);\n \t}\n-\tif (required_fault)\n+\tif (required_fault) {\n+\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t}\n \t\treturn hmm_vma_fault(addr, end, required_fault, walk);\n-\treturn hmm_pfns_fill(addr, end, range, 0);\n+\t}\n+\treturn hmm_pfns_fill(addr, end, hmm_vma_walk, 0);\n }\n \n static inline unsigned long hmm_pfn_flags_order(unsigned long order)\n@@ -208,9 +276,16 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,\n \tcpu_flags = pmd_to_hmm_pfn_flags(range, pmd);\n \trequired_fault =\n \t\thmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);\n-\tif (required_fault)\n+\tif (required_fault) {\n+\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t}\n \t\treturn hmm_vma_fault(addr, end, required_fault, walk);\n+\t}\n \n+\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr,\n+\t\t\t\t\t\tend, hmm_pfns);\n \tpfn = pmd_pfn(pmd) + ((addr \u0026 ~PMD_MASK) \u003e\u003e PAGE_SHIFT);\n \tfor (i = 0; addr \u003c end; addr += PAGE_SIZE, i++, pfn++) {\n \t\thmm_pfns[i] \u0026= HMM_PFN_INOUT_FLAGS;\n@@ -289,14 +364,26 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,\n \t\t\tgoto fault;\n \n \t\tif (softleaf_is_migration(entry)) {\n-\t\t\tpte_unmap(ptep);\n-\t\t\thmm_vma_walk-\u003elast = addr;\n-\t\t\tmigration_entry_wait(walk-\u003emm, pmdp, addr);\n-\t\t\treturn -EBUSY;\n+\t\t\tif (!hmm_select_migrate(range)) {\n+\t\t\t\tHMM_ASSERT_UNLOCKED(hmm_vma_walk);\n+\t\t\t\tpte_unmap(ptep);\n+\t\t\t\thmm_vma_walk-\u003elast = addr;\n+\t\t\t\tmigration_entry_wait(walk-\u003emm, pmdp, addr);\n+\t\t\t\treturn -EBUSY;\n+\t\t\t}\n+\t\t\treturn 0;\n \t\t}\n \n \t\t/* Report error for everything else */\n-\t\tpte_unmap(ptep);\n+\n+\t\tif (hmm_vma_walk-\u003eptelocked) {\n+\t\t\tlazy_mmu_mode_disable();\n+\t\t\tpte_unmap_unlock(ptep, hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003eptelocked = false;\n+\t\t} else {\n+\t\t\tpte_unmap(ptep);\n+\t\t}\n+\n \t\treturn -EFAULT;\n \t}\n \n@@ -313,7 +400,14 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,\n \tif (!vm_normal_page(walk-\u003evma, addr, pte) \u0026\u0026\n \t !is_zero_pfn(pte_pfn(pte))) {\n \t\tif (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {\n-\t\t\tpte_unmap(ptep);\n+\t\t\tif (hmm_vma_walk-\u003eptelocked) {\n+\t\t\t\tlazy_mmu_mode_disable();\n+\t\t\t\tpte_unmap_unlock(ptep, hmm_vma_walk-\u003eptl);\n+\t\t\t\thmm_vma_walk-\u003eptelocked = false;\n+\t\t\t} else {\n+\t\t\t\tpte_unmap(ptep);\n+\t\t\t}\n+\n \t\t\treturn -EFAULT;\n \t\t}\n \t\tnew_pfn_flags = HMM_PFN_ERROR;\n@@ -322,11 +416,20 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,\n \n \tnew_pfn_flags = pte_pfn(pte) | cpu_flags;\n out:\n+\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr,\n+\t\t\t\t\t\taddr + PAGE_SIZE,\n+\t\t\t\t\t\thmm_pfn);\n \t*hmm_pfn = (*hmm_pfn \u0026 HMM_PFN_INOUT_FLAGS) | new_pfn_flags;\n \treturn 0;\n \n fault:\n-\tpte_unmap(ptep);\n+\tif (hmm_vma_walk-\u003eptelocked) {\n+\t\tlazy_mmu_mode_disable();\n+\t\tpte_unmap_unlock(ptep, hmm_vma_walk-\u003eptl);\n+\t\thmm_vma_walk-\u003eptelocked = false;\n+\t} else {\n+\t\tpte_unmap(ptep);\n+\t}\n \t/* Fault any virtual address we were asked to fault */\n \treturn hmm_vma_fault(addr, end, required_fault, walk);\n }\n@@ -354,6 +457,9 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,\n \t\tif (softleaf_is_device_private_write(entry))\n \t\t\tcpu_flags |= HMM_PFN_WRITE;\n \n+\t\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,\n+\t\t\t\t\t\t\tstart, end,\n+\t\t\t\t\t\t\thmm_pfns);\n \t\t/*\n \t\t * Fully populate the PFN list though subsequent PFNs could be\n \t\t * inferred, because drivers which are not yet aware of large\n@@ -370,13 +476,18 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,\n \trequired_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,\n \t\t\t\t\t npages, 0);\n \tif (required_fault) {\n-\t\tif (softleaf_is_device_private(entry))\n+\t\tif (softleaf_is_device_private(entry)) {\n+\t\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t\t}\n \t\t\treturn hmm_vma_fault(addr, end, required_fault, walk);\n+\t\t}\n \t\telse\n \t\t\treturn -EFAULT;\n \t}\n \n-\treturn hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);\n+\treturn hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n }\n #else\n static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,\n@@ -384,15 +495,575 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,\n \t\t\t\t pmd_t pmd)\n {\n \tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n-\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n \tunsigned long npages = (end - start) \u003e\u003e PAGE_SHIFT;\n \n \tif (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))\n \t\treturn -EFAULT;\n-\treturn hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);\n+\treturn hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n }\n #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */\n \n+#ifdef CONFIG_DEVICE_MIGRATION\n+/**\n+ * migrate_vma_split_folio() - Helper function to split a THP folio\n+ * @folio: the folio to split\n+ * @fault_page: struct page associated with the fault if any\n+ * @hmm_vma_walk: walk in progress\n+ * @ptep: pte_t * for unmap and unlock ptl\n+ *\n+ * Returns 0 on success\n+ */\n+static int migrate_vma_split_folio(struct folio *folio,\n+\t\t\t\t struct page *fault_page,\n+\t\t\t\t struct hmm_vma_walk *hmm_vma_walk,\n+\t\t\t\t pte_t *ptep)\n+{\n+\tint ret;\n+\tstruct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;\n+\tstruct folio *new_fault_folio = NULL;\n+\n+\tif (folio != fault_folio)\n+\t\tfolio_get(folio);\n+\n+\tlazy_mmu_mode_disable();\n+\tpte_unmap_unlock(ptep, hmm_vma_walk-\u003eptl);\n+\thmm_vma_walk-\u003eptelocked = false;\n+\n+\tif (folio != fault_folio)\n+\t\tfolio_lock(folio);\n+\n+\tret = split_folio(folio);\n+\tif (ret) {\n+\t\tif (folio != fault_folio) {\n+\t\t\tfolio_unlock(folio);\n+\t\t\tfolio_put(folio);\n+\t\t}\n+\t\treturn ret;\n+\t}\n+\n+\tnew_fault_folio = fault_page ? page_folio(fault_page) : NULL;\n+\n+\t/*\n+\t * Ensure the lock is held on the correct\n+\t * folio after the split\n+\t */\n+\tif (!new_fault_folio) {\n+\t\tfolio_unlock(folio);\n+\t\tfolio_put(folio);\n+\t} else if (folio != new_fault_folio) {\n+\t\tif (new_fault_folio != fault_folio) {\n+\t\t\tfolio_get(new_fault_folio);\n+\t\t\tfolio_lock(new_fault_folio);\n+\t\t}\n+\t\tfolio_unlock(folio);\n+\t\tfolio_put(folio);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * Due to dropping ptl locks for splitting for instance, would we\n+ * overwrite already collected pfns? This could happen when pmd\n+ * pointing to a page table has vanished and been replaced\n+ * with a leaf pmd, or another page table.\n+ * In that case unref and unlock the folios,\n+ * the pfns of which were collected from the disappeared\n+ * page tables.\n+ */\n+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,\n+\t\t\t\t\t\t unsigned long start,\n+\t\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t\t unsigned long *hmm_pfn)\n+{\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\tstruct folio *fault_folio = NULL;\n+\tenum migrate_vma_info minfo;\n+\tstruct folio *folio;\n+\tunsigned long i;\n+\n+\tminfo = hmm_select_migrate(range);\n+\tif (!minfo)\n+\t\treturn;\n+\n+\tWARN_ON_ONCE(!migrate);\n+\n+\tfault_folio = migrate-\u003efault_page ?\n+\t\tpage_folio(migrate-\u003efault_page) : NULL;\n+\n+\tfor (i = 0; start \u003c end; start += PAGE_SIZE, i++) {\n+\t\tif (hmm_pfn_collected(hmm_pfn[i])) {\n+\t\t\tfolio = page_folio(hmm_pfn_to_page(hmm_pfn[i]));\n+\t\t\tif (folio != fault_folio)\n+\t\t\t\tfolio_unlock(folio);\n+\t\t\tfolio_put(folio);\n+\t\t\thmm_pfn[i] = hmm_pfn_rollback_collected(hmm_pfn[i]);\n+\n+\t\t}\n+\t}\n+}\n+\n+static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk,\n+\t\t\t\t\t pmd_t *pmdp,\n+\t\t\t\t\t unsigned long start,\n+\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t unsigned long *hmm_pfn)\n+{\n+\tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\tstruct folio *fault_folio = NULL;\n+\tenum migrate_vma_info minfo;\n+\tstruct folio *folio;\n+\tunsigned long i;\n+\tint r = 0;\n+\n+\t// Do we want to migrate at all?\n+\tminfo = hmm_select_migrate(range);\n+\tif (!minfo)\n+\t\treturn r;\n+\n+\tWARN_ON_ONCE(!migrate);\n+\tHMM_ASSERT_PMD_LOCKED(hmm_vma_walk, true);\n+\n+\tfault_folio = migrate-\u003efault_page ?\n+\t\tpage_folio(migrate-\u003efault_page) : NULL;\n+\n+\tif (pmd_none(*pmdp))\n+\t\treturn hmm_pfns_fill(start, end, hmm_vma_walk, 0);\n+\n+\tif (!(hmm_pfn[0] \u0026 HMM_PFN_VALID))\n+\t\tgoto out;\n+\n+\tif (pmd_trans_huge(*pmdp)) {\n+\t\tif (!(minfo \u0026 MIGRATE_VMA_SELECT_SYSTEM))\n+\t\t\tgoto out;\n+\n+\t\tfolio = pmd_folio(*pmdp);\n+\t\tif (is_huge_zero_folio(folio))\n+\t\t\treturn hmm_pfns_fill(start, end, hmm_vma_walk, 0);\n+\n+\t} else if (!pmd_present(*pmdp)) {\n+\t\tconst softleaf_t entry = softleaf_from_pmd(*pmdp);\n+\n+\t\tif (!softleaf_is_device_private(entry))\n+\t\t\tgoto out;\n+\n+\t\tif (!(minfo \u0026 MIGRATE_VMA_SELECT_DEVICE_PRIVATE))\n+\t\t\tgoto out;\n+\n+\t\tfolio = softleaf_to_folio(entry);\n+\t\tif (folio-\u003epgmap-\u003eowner != migrate-\u003epgmap_owner)\n+\t\t\tgoto out;\n+\t} else {\n+\t\thmm_vma_walk-\u003elast = start;\n+\t\treturn -EBUSY;\n+\t}\n+\n+\tfolio_get(folio);\n+\n+\tif (folio != fault_folio \u0026\u0026 unlikely(!folio_trylock(folio))) {\n+\t\tfolio_put(folio);\n+\t\thmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n+\t\treturn 0;\n+\t}\n+\n+\tif (thp_migration_supported() \u0026\u0026\n+\t (migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_COMPOUND) \u0026\u0026\n+\t (IS_ALIGNED(start, HPAGE_PMD_SIZE) \u0026\u0026\n+\t IS_ALIGNED(end, HPAGE_PMD_SIZE))) {\n+\t\tstruct page_vma_mapped_walk pvmw = {\n+\t\t\t.ptl = hmm_vma_walk-\u003eptl,\n+\t\t\t.address = start,\n+\t\t\t.pmd = pmdp,\n+\t\t\t.vma = walk-\u003evma,\n+\t\t};\n+\n+\t\thmm_pfn[0] |= HMM_PFN_MIGRATE | HMM_PFN_COMPOUND;\n+\n+\t\tr = set_pmd_migration_entry(\u0026pvmw, folio_page(folio, 0));\n+\t\tif (r) {\n+\t\t\thmm_pfn[0] \u0026= ~(HMM_PFN_MIGRATE | HMM_PFN_COMPOUND);\n+\t\t\tr = -ENOENT; // fallback\n+\t\t\tgoto unlock_out;\n+\t\t}\n+\t\tfor (i = 1, start += PAGE_SIZE; start \u003c end; start += PAGE_SIZE, i++)\n+\t\t\thmm_pfn[i] \u0026= HMM_PFN_INOUT_FLAGS;\n+\n+\t} else {\n+\t\tr = -ENOENT; // fallback\n+\t\tgoto unlock_out;\n+\t}\n+\n+out:\n+\treturn r;\n+\n+unlock_out:\n+\tif (folio != fault_folio)\n+\t\tfolio_unlock(folio);\n+\tfolio_put(folio);\n+\tgoto out;\n+}\n+\n+/*\n+ * Install migration entries if migration requested, either from fault\n+ * or migrate paths.\n+ *\n+ */\n+static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk,\n+\t\t\t\t\t pmd_t *pmdp,\n+\t\t\t\t\t pte_t *ptep,\n+\t\t\t\t\t unsigned long addr,\n+\t\t\t\t\t unsigned long *hmm_pfn,\n+\t\t\t\t\t bool *unmapped)\n+{\n+\tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\tstruct mm_struct *mm = walk-\u003evma-\u003evm_mm;\n+\tstruct folio *fault_folio = NULL;\n+\tenum migrate_vma_info minfo;\n+\tstruct dev_pagemap *pgmap;\n+\tbool anon_exclusive;\n+\tstruct folio *folio;\n+\tunsigned long pfn;\n+\tstruct page *page;\n+\tsoftleaf_t entry;\n+\tpte_t pte, swp_pte;\n+\tbool writable = false;\n+\n+\t// Do we want to migrate at all?\n+\tminfo = hmm_select_migrate(range);\n+\tif (!minfo)\n+\t\treturn 0;\n+\n+\tWARN_ON_ONCE(!migrate);\n+\tHMM_ASSERT_PTE_LOCKED(hmm_vma_walk, true);\n+\n+\tfault_folio = migrate-\u003efault_page ?\n+\t\tpage_folio(migrate-\u003efault_page) : NULL;\n+\n+\tpte = ptep_get(ptep);\n+\n+\tif (pte_none(pte)) {\n+\t\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,\n+\t\t\t\t\t\t\taddr,\n+\t\t\t\t\t\t\taddr + PAGE_SIZE,\n+\t\t\t\t\t\t\thmm_pfn);\n+\n+\t\tif (vma_is_anonymous(walk-\u003evma)) {\n+\t\t\t*hmm_pfn \u0026= HMM_PFN_INOUT_FLAGS;\n+\t\t\t*hmm_pfn |= HMM_PFN_MIGRATE;\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (!(hmm_pfn[0] \u0026 HMM_PFN_VALID))\n+\t\tgoto out;\n+\n+\tif (!pte_present(pte)) {\n+\t\t/*\n+\t\t * Only care about unaddressable device page special\n+\t\t * page table entry. Other special swap entries are not\n+\t\t * migratable, and we ignore regular swapped page.\n+\t\t */\n+\t\tentry = softleaf_from_pte(pte);\n+\t\tif (!softleaf_is_device_private(entry))\n+\t\t\tgoto out;\n+\n+\t\tif (!(minfo \u0026 MIGRATE_VMA_SELECT_DEVICE_PRIVATE))\n+\t\t\tgoto out;\n+\n+\t\tpage = softleaf_to_page(entry);\n+\t\tfolio = page_folio(page);\n+\t\tif (folio-\u003epgmap-\u003eowner != migrate-\u003epgmap_owner)\n+\t\t\tgoto out;\n+\n+\t\tif (folio_test_large(folio)) {\n+\t\t\tint ret;\n+\n+\t\t\tret = migrate_vma_split_folio(folio,\n+\t\t\t\t\t\t migrate-\u003efault_page,\n+\t\t\t\t\t\t hmm_vma_walk,\n+\t\t\t\t\t\t ptep);\n+\t\t\tif (ret)\n+\t\t\t\tgoto out_error;\n+\t\t\treturn -EAGAIN;\n+\t\t}\n+\n+\t\tpfn = page_to_pfn(page);\n+\t\tif (softleaf_is_device_private_write(entry))\n+\t\t\twritable = true;\n+\t} else {\n+\t\tpfn = pte_pfn(pte);\n+\t\tif (is_zero_pfn(pfn) \u0026\u0026\n+\t\t (minfo \u0026 MIGRATE_VMA_SELECT_SYSTEM)) {\n+\t\t\thmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,\n+\t\t\t\t\t\t\t\taddr,\n+\t\t\t\t\t\t\t\taddr + PAGE_SIZE,\n+\t\t\t\t\t\t\t\thmm_pfn);\n+\t\t\t*hmm_pfn = HMM_PFN_MIGRATE;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tpage = vm_normal_page(walk-\u003evma, addr, pte);\n+\t\tif (page \u0026\u0026 !is_zone_device_page(page) \u0026\u0026\n+\t\t !(minfo \u0026 MIGRATE_VMA_SELECT_SYSTEM)) {\n+\t\t\tgoto out;\n+\t\t} else if (page \u0026\u0026 is_device_coherent_page(page)) {\n+\t\t\tpgmap = page_pgmap(page);\n+\n+\t\t\tif (!(minfo \u0026\n+\t\t\t MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||\n+\t\t\t pgmap-\u003eowner != migrate-\u003epgmap_owner)\n+\t\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfolio = page ? page_folio(page) : NULL;\n+\t\tif (folio \u0026\u0026 folio_test_large(folio)) {\n+\t\t\tint ret;\n+\n+\t\t\tret = migrate_vma_split_folio(folio,\n+\t\t\t\t\t\t migrate-\u003efault_page,\n+\t\t\t\t\t\t hmm_vma_walk,\n+\t\t\t\t\t\t ptep);\n+\t\t\tif (ret)\n+\t\t\t\tgoto out_error;\n+\t\t\treturn -EAGAIN;\n+\t\t}\n+\n+\t\twritable = pte_write(pte);\n+\t}\n+\n+\tif (!page || !page-\u003emapping)\n+\t\tgoto out;\n+\n+\t/*\n+\t * By getting a reference on the folio we pin it and that blocks\n+\t * any kind of migration. Side effect is that it \"freezes\" the\n+\t * pte.\n+\t *\n+\t * We drop this reference after isolating the folio from the lru\n+\t * for non device folio (device folio are not on the lru and thus\n+\t * can't be dropped from it).\n+\t */\n+\tfolio = page_folio(page);\n+\tfolio_get(folio);\n+\n+\t/*\n+\t * We rely on folio_trylock() to avoid deadlock between\n+\t * concurrent migrations where each is waiting on the others\n+\t * folio lock. If we can't immediately lock the folio we fail this\n+\t * migration as it is only best effort anyway.\n+\t *\n+\t * If we can lock the folio it's safe to set up a migration entry\n+\t * now. In the common case where the folio is mapped once in a\n+\t * single process setting up the migration entry now is an\n+\t * optimisation to avoid walking the rmap later with\n+\t * try_to_migrate().\n+\t */\n+\n+\tif (fault_folio == folio || folio_trylock(folio)) {\n+\t\tanon_exclusive = folio_test_anon(folio) \u0026\u0026\n+\t\t\tPageAnonExclusive(page);\n+\n+\t\tif (pte_present(pte))\n+\t\t\tflush_cache_page(walk-\u003evma, addr, pfn);\n+\n+\t\tif (anon_exclusive) {\n+\t\t\tpte = ptep_clear_flush(walk-\u003evma, addr, ptep);\n+\n+\t\t\tif (folio_try_share_anon_rmap_pte(folio, page)) {\n+\t\t\t\tset_pte_at(mm, addr, ptep, pte);\n+\t\t\t\tfolio_unlock(folio);\n+\t\t\t\tfolio_put(folio);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tpte = ptep_get_and_clear(mm, addr, ptep);\n+\t\t}\n+\n+\t\tif (pte_present(pte) \u0026\u0026 pte_dirty(pte))\n+\t\t\tfolio_mark_dirty(folio);\n+\n+\t\t/* Setup special migration page table entry */\n+\t\tif (writable)\n+\t\t\tentry = make_writable_migration_entry(pfn);\n+\t\telse if (anon_exclusive)\n+\t\t\tentry = make_readable_exclusive_migration_entry(pfn);\n+\t\telse\n+\t\t\tentry = make_readable_migration_entry(pfn);\n+\n+\t\tif (pte_present(pte)) {\n+\t\t\tif (pte_young(pte))\n+\t\t\t\tentry = make_migration_entry_young(entry);\n+\t\t\tif (pte_dirty(pte))\n+\t\t\t\tentry = make_migration_entry_dirty(entry);\n+\t\t}\n+\n+\t\tswp_pte = swp_entry_to_pte(entry);\n+\t\tif (pte_present(pte)) {\n+\t\t\tif (pte_soft_dirty(pte))\n+\t\t\t\tswp_pte = pte_swp_mksoft_dirty(swp_pte);\n+\t\t\tif (pte_uffd_wp(pte))\n+\t\t\t\tswp_pte = pte_swp_mkuffd_wp(swp_pte);\n+\t\t} else {\n+\t\t\tif (pte_swp_soft_dirty(pte))\n+\t\t\t\tswp_pte = pte_swp_mksoft_dirty(swp_pte);\n+\t\t\tif (pte_swp_uffd_wp(pte))\n+\t\t\t\tswp_pte = pte_swp_mkuffd_wp(swp_pte);\n+\t\t}\n+\n+\t\tset_pte_at(mm, addr, ptep, swp_pte);\n+\t\tfolio_remove_rmap_pte(folio, page, walk-\u003evma);\n+\t\tfolio_put(folio);\n+\t\t*hmm_pfn |= HMM_PFN_MIGRATE;\n+\t\tif (pte_present(pte))\n+\t\t\t*unmapped = true;\n+\t} else {\n+\t\tfolio_put(folio);\n+\t}\n+out:\n+\treturn 0;\n+out_error:\n+\treturn -EFAULT;\n+}\n+\n+static int hmm_vma_walk_split(pmd_t *pmdp,\n+\t\t\t unsigned long addr,\n+\t\t\t struct mm_walk *walk)\n+{\n+\tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\tstruct folio *folio, *fault_folio;\n+\tspinlock_t *ptl;\n+\tint ret = 0;\n+\n+\tHMM_ASSERT_UNLOCKED(hmm_vma_walk);\n+\n+\tfault_folio = (migrate \u0026\u0026 migrate-\u003efault_page) ?\n+\t\tpage_folio(migrate-\u003efault_page) : NULL;\n+\n+\tptl = pmd_lock(walk-\u003emm, pmdp);\n+\tif (unlikely(!pmd_trans_huge(*pmdp))) {\n+\t\tspin_unlock(ptl);\n+\t\tgoto out;\n+\t}\n+\n+\tfolio = pmd_folio(*pmdp);\n+\tif (is_huge_zero_folio(folio)) {\n+\t\tspin_unlock(ptl);\n+\t\tsplit_huge_pmd(walk-\u003evma, pmdp, addr);\n+\t} else {\n+\t\tfolio_get(folio);\n+\t\tspin_unlock(ptl);\n+\n+\t\tif (folio != fault_folio) {\n+\t\t\tif (unlikely(!folio_trylock(folio))) {\n+\t\t\t\tfolio_put(folio);\n+\t\t\t\tret = -EBUSY;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tfolio_put(folio);\n+\t\t}\n+\n+\t\tret = split_folio(folio);\n+\t\tif (fault_folio != folio) {\n+\t\t\tfolio_unlock(folio);\n+\t\t\tfolio_put(folio);\n+\t\t}\n+\t}\n+out:\n+\treturn ret;\n+}\n+#else\n+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,\n+\t\t\t\t\t\t unsigned long start,\n+\t\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t\t unsigned long *hmm_pfn)\n+{\n+}\n+\n+static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk,\n+\t\t\t\t\t pmd_t *pmdp,\n+\t\t\t\t\t unsigned long start,\n+\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t unsigned long *hmm_pfn)\n+{\n+\treturn 0;\n+}\n+\n+static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk,\n+\t\t\t\t\t pmd_t *pmdp,\n+\t\t\t\t\t pte_t *ptep,\n+\t\t\t\t\t unsigned long addr,\n+\t\t\t\t\t unsigned long *hmm_pfn,\n+\t\t\t\t\t bool *unmapped)\n+{\n+\treturn 0;\n+}\n+\n+static int hmm_vma_walk_split(pmd_t *pmdp,\n+\t\t\t unsigned long addr,\n+\t\t\t struct mm_walk *walk)\n+{\n+\treturn 0;\n+}\n+#endif\n+\n+static int hmm_vma_capture_migrate_range(unsigned long start,\n+\t\t\t\t\t unsigned long end,\n+\t\t\t\t\t struct mm_walk *walk)\n+{\n+\tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\n+\tif (!hmm_select_migrate(range))\n+\t\treturn 0;\n+\n+\tif (hmm_vma_walk-\u003evma \u0026\u0026 (hmm_vma_walk-\u003evma != walk-\u003evma))\n+\t\treturn -ERANGE;\n+\n+\thmm_vma_walk-\u003evma = walk-\u003evma;\n+\thmm_vma_walk-\u003estart = start;\n+\thmm_vma_walk-\u003eend = end;\n+\n+\tif (end - start \u003e range-\u003eend - range-\u003estart)\n+\t\treturn -ERANGE;\n+\n+\tif (!hmm_vma_walk-\u003emmu_range.owner) {\n+\t\tmmu_notifier_range_init_owner(\u0026hmm_vma_walk-\u003emmu_range, MMU_NOTIFY_MIGRATE, 0,\n+\t\t\t\t\t walk-\u003evma-\u003evm_mm, start, end,\n+\t\t\t\t\t range-\u003edev_private_owner);\n+\t\tmmu_notifier_invalidate_range_start(\u0026hmm_vma_walk-\u003emmu_range);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static void hmm_vma_post_range_fault(struct hmm_vma_walk *hmm_vma_walk)\n+{\n+\n+\tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n+\n+\tif (hmm_select_migrate(range) \u0026\u0026\n+\t hmm_vma_walk-\u003emmu_range.owner) {\n+\t\t/*\n+\t\t * The migrate_vma path has the following initialized,\n+\t\t * so take care of fault path below.\n+\t\t */\n+\t\tif (range-\u003enotifier) {\n+\t\t\thmm_fill_migrate_vma(range,\n+\t\t\t\t\t hmm_vma_walk-\u003evma,\n+\t\t\t\t\t hmm_vma_walk-\u003estart,\n+\t\t\t\t\t hmm_vma_walk-\u003eend);\n+\t\t}\n+\t\tmmu_notifier_invalidate_range_end(\u0026hmm_vma_walk-\u003emmu_range);\n+\t}\n+}\n+\n static int hmm_vma_walk_pmd(pmd_t *pmdp,\n \t\t\t unsigned long start,\n \t\t\t unsigned long end,\n@@ -400,46 +1071,128 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp,\n {\n \tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n \tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n-\tunsigned long *hmm_pfns =\n-\t\t\u0026range-\u003ehmm_pfns[(start - range-\u003estart) \u003e\u003e PAGE_SHIFT];\n \tunsigned long npages = (end - start) \u003e\u003e PAGE_SHIFT;\n+\tstruct mm_struct *mm = walk-\u003evma-\u003evm_mm;\n+\tunsigned long *hmm_pfns, *hmm_pfns_start;\n+\tenum migrate_vma_info minfo;\n \tunsigned long addr = start;\n+\tbool unmapped = false;\n+\tunsigned long i;\n \tpte_t *ptep;\n \tpmd_t pmd;\n+\tint r = 0;\n \n+\tminfo = hmm_select_migrate(range);\n+\thmm_pfns_start = \u0026range-\u003ehmm_pfns[(start - range-\u003estart) \u003e\u003e PAGE_SHIFT];\n again:\n-\tpmd = pmdp_get_lockless(pmdp);\n-\tif (pmd_none(pmd))\n-\t\treturn hmm_vma_walk_hole(start, end, -1, walk);\n+\thmm_pfns = \u0026range-\u003ehmm_pfns[(addr - range-\u003estart) \u003e\u003e PAGE_SHIFT];\n+\thmm_vma_walk-\u003eptelocked = false;\n+\thmm_vma_walk-\u003epmdlocked = false;\n+\n+\tif (minfo) {\n+\t\thmm_vma_walk-\u003eptl = pmd_lock(mm, pmdp);\n+\t\thmm_vma_walk-\u003epmdlocked = true;\n+\t\tpmd = pmdp_get(pmdp);\n+\t} else\n+\t\tpmd = pmdp_get_lockless(pmdp);\n+\n+\tif (pmd_none(pmd)) {\n+\t\tr = hmm_vma_walk_hole(start, end, -1, walk);\n+\n+\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t}\n+\t\treturn r;\n+\t}\n \n \tif (thp_migration_supported() \u0026\u0026 pmd_is_migration_entry(pmd)) {\n-\t\tif (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {\n-\t\t\thmm_vma_walk-\u003elast = addr;\n-\t\t\tpmd_migration_entry_wait(walk-\u003emm, pmdp);\n-\t\t\treturn -EBUSY;\n+\t\tif (!minfo) {\n+\t\t\tif (hmm_range_need_fault(hmm_vma_walk, hmm_pfns_start, npages, 0)) {\n+\t\t\t\thmm_vma_walk-\u003elast = addr;\n+\t\t\t\tpmd_migration_entry_wait(walk-\u003emm, pmdp);\n+\t\t\t\treturn -EBUSY;\n+\t\t\t}\n+\t\t\tfor (i = 0; start \u003c end; start += PAGE_SIZE, i++)\n+\t\t\t\thmm_pfns_start[i] \u0026= HMM_PFN_INOUT_FLAGS;\n+\t\t}\n+\n+\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003epmdlocked = false;\n \t\t}\n-\t\treturn hmm_pfns_fill(start, end, range, 0);\n+\n+\t\treturn 0;\n \t}\n \n-\tif (!pmd_present(pmd))\n-\t\treturn hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns,\n-\t\t\t\t\t\t pmd);\n+\tif (pmd_trans_huge(pmd) || !pmd_present(pmd)) {\n+\t\tif (!pmd_present(pmd)) {\n+\t\t\tr = hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns_start,\n+\t\t\t\t\t\t pmd);\n+\t\t\t// If not migrating we are done\n+\t\t\tif (r || !minfo) {\n+\t\t\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t\t\t}\n+\t\t\t\treturn r;\n+\t\t\t}\n+\t\t}\n \n-\tif (pmd_trans_huge(pmd)) {\n-\t\t/*\n-\t\t * No need to take pmd_lock here, even if some other thread\n-\t\t * is splitting the huge pmd we will get that event through\n-\t\t * mmu_notifier callback.\n-\t\t *\n-\t\t * So just read pmd value and check again it's a transparent\n-\t\t * huge or device mapping one and compute corresponding pfn\n-\t\t * values.\n-\t\t */\n-\t\tpmd = pmdp_get_lockless(pmdp);\n-\t\tif (!pmd_trans_huge(pmd))\n-\t\t\tgoto again;\n+\t\tif (pmd_trans_huge(pmd)) {\n+\t\t\t/*\n+\t\t\t * No need to take pmd_lock here if not migrating,\n+\t\t\t * even if some other thread is splitting the huge\n+\t\t\t * pmd we will get that event through mmu_notifier callback.\n+\t\t\t *\n+\t\t\t * So just read pmd value and check again it's a transparent\n+\t\t\t * huge or device mapping one and compute corresponding pfn\n+\t\t\t * values.\n+\t\t\t */\n+\n+\t\t\tif (!minfo) {\n+\t\t\t\tpmd = pmdp_get_lockless(pmdp);\n+\t\t\t\tif (!pmd_trans_huge(pmd))\n+\t\t\t\t\tgoto again;\n+\t\t\t}\n+\n+\t\t\tr = hmm_vma_handle_pmd(walk, start, end, hmm_pfns_start, pmd);\n+\n+\t\t\t// If not migrating we are done\n+\t\t\tif (r || !minfo) {\n+\t\t\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t\t\t}\n+\t\t\t\treturn r;\n+\t\t\t}\n+\t\t}\n+\n+\t\tr = hmm_vma_handle_migrate_prepare_pmd(walk, pmdp, start, end, hmm_pfns_start);\n+\n+\t\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\t\thmm_vma_walk-\u003epmdlocked = false;\n+\t\t}\n+\n+\t\tif (r == -ENOENT) {\n+\t\t\tr = hmm_vma_walk_split(pmdp, addr, walk);\n+\t\t\tif (r) {\n+\t\t\t\t/* Split not successful, skip */\n+\t\t\t\treturn hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n+\t\t\t}\n+\n+\t\t\t/* Split successful, reloop */\n+\t\t\thmm_vma_walk-\u003elast = addr;\n+\t\t\treturn -EBUSY;\n+\t\t}\n+\t\treturn r;\n+\n+\t}\n \n-\t\treturn hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);\n+\tif (hmm_vma_walk-\u003epmdlocked) {\n+\t\tspin_unlock(hmm_vma_walk-\u003eptl);\n+\t\thmm_vma_walk-\u003epmdlocked = false;\n \t}\n \n \t/*\n@@ -449,24 +1202,62 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp,\n \t * recover.\n \t */\n \tif (pmd_bad(pmd)) {\n-\t\tif (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))\n+\t\tif (hmm_range_need_fault(hmm_vma_walk, hmm_pfns_start, npages, 0))\n \t\t\treturn -EFAULT;\n-\t\treturn hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);\n+\t\treturn hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n \t}\n \n-\tptep = pte_offset_map(pmdp, addr);\n-\tif (!ptep)\n+\tif (minfo) {\n+\t\tptep = pte_offset_map_lock(mm, pmdp, addr, \u0026hmm_vma_walk-\u003eptl);\n+\t\tif (ptep) {\n+\t\t\tlazy_mmu_mode_enable();\n+\t\t\thmm_vma_walk-\u003eptelocked = true;\n+\t\t} else {\n+\t\t\t/* The pte table is gone */\n+\t\t\thmm_vma_handle_migrate_prepare_rollback(walk-\u003eprivate, addr, end, hmm_pfns);\n+\t\t}\n+\t} else {\n+\t\tptep = pte_offset_map(pmdp, addr);\n+\t}\n+\tif (!ptep) {\n+\t\taddr = start;\n \t\tgoto again;\n+\t}\n+\n \tfor (; addr \u003c end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {\n-\t\tint r;\n \n \t\tr = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);\n \t\tif (r) {\n-\t\t\t/* hmm_vma_handle_pte() did pte_unmap() */\n+\t\t\t/* hmm_vma_handle_pte() did pte_unmap() / pte_unmap_unlock */\n \t\t\treturn r;\n \t\t}\n+\n+\t\tr = hmm_vma_handle_migrate_prepare(walk, pmdp, ptep, addr, hmm_pfns, \u0026unmapped);\n+\t\tif (r == -EAGAIN) {\n+\t\t\tHMM_ASSERT_UNLOCKED(hmm_vma_walk);\n+\t\t\tif (unmapped) {\n+\t\t\t\tflush_tlb_range(walk-\u003evma, start, addr);\n+\t\t\t\tunmapped = false;\n+\t\t\t}\n+\t\t\tgoto again;\n+\t\t}\n+\t\tif (r) {\n+\t\t\thmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (unmapped)\n+\t\tflush_tlb_range(walk-\u003evma, start, addr);\n+\n+\tif (hmm_vma_walk-\u003eptelocked) {\n+\t\tlazy_mmu_mode_disable();\n+\t\tpte_unmap_unlock(ptep - 1, hmm_vma_walk-\u003eptl);\n+\t\thmm_vma_walk-\u003eptelocked = false;\n+\t} else {\n+\t\tpte_unmap(ptep - 1);\n \t}\n-\tpte_unmap(ptep - 1);\n+\n \treturn 0;\n }\n \n@@ -600,6 +1391,11 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,\n \tstruct hmm_vma_walk *hmm_vma_walk = walk-\u003eprivate;\n \tstruct hmm_range *range = hmm_vma_walk-\u003erange;\n \tstruct vm_area_struct *vma = walk-\u003evma;\n+\tint r;\n+\n+\tr = hmm_vma_capture_migrate_range(start, end, walk);\n+\tif (r)\n+\t\treturn r;\n \n \tif (!(vma-\u003evm_flags \u0026 (VM_IO | VM_PFNMAP)) \u0026\u0026\n \t vma-\u003evm_flags \u0026 VM_READ)\n@@ -622,7 +1418,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,\n \t\t\t\t (end - start) \u003e\u003e PAGE_SHIFT, 0))\n \t\treturn -EFAULT;\n \n-\thmm_pfns_fill(start, end, range, HMM_PFN_ERROR);\n+\thmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);\n \n \t/* Skip this vma and continue processing the next vma. */\n \treturn 1;\n@@ -652,9 +1448,37 @@ static const struct mm_walk_ops hmm_walk_ops = {\n *\t\tthe invalidation to finish.\n * -EFAULT: A page was requested to be valid and could not be made valid\n * ie it has no backing VMA or it is illegal to access\n+ * -ERANGE: The range crosses multiple VMAs, or space for hmm_pfns array\n+ * is too low.\n *\n * This is similar to get_user_pages(), except that it can read the page tables\n * without mutating them (ie causing faults).\n+ *\n+ * If want to do migration after faulting, call hmm_range_fault() with\n+ * range.default_flags of HMM_PFN_REQ_MIGRATE, and optionally\n+ * HMM_PFN_REQ_FAULT|HMM_PFN_REQ_WRITE, and initialize range-\u003emigrate field.\n+ * range-\u003emigrate-\u003evma will be populated during the call,\n+ * and must be stable across the whole migrate process, which is\n+ * why mmap_lock must be held around this call.\n+ *\n+ * When HMM_PFN_REQ_MIGRATE is set, migration collection may be partial on\n+ * return and the caller takes responsibility for completing or aborting it.\n+ *\n+ * On success, the caller must call migrate_hmm_range_setup() and may then\n+ * proceed with the normal migrate_vma sequence: prepare destination pages,\n+ * call migrate_vma_pages(), update device mappings as needed, and finally\n+ * call migrate_vma_finalize().\n+ *\n+ * On -EBUSY, the caller may retry hmm_range_fault() using the same range and\n+ * PFN array without undoing entries collected by the previous attempt. If\n+ * the caller stops retrying, it must abort the partial migration.\n+ *\n+ * On any other error, or when abandoning a retry, the caller must call\n+ * migrate_hmm_range_setup(), migrate_vma_pages() with no valid destination\n+ * entries, and migrate_vma_finalize() to abort the partial migration.\n+ *\n+ * The mmap read lock must remain held until the migration has either been\n+ * completed or aborted.\n */\n int hmm_range_fault(struct hmm_range *range)\n {\n@@ -662,16 +1486,25 @@ int hmm_range_fault(struct hmm_range *range)\n \t\t.range = range,\n \t\t.last = range-\u003estart,\n \t};\n-\tstruct mm_struct *mm = range-\u003enotifier-\u003emm;\n+\t/*\n+\t * Could be serving a device fault or come from migrate\n+\t * entry point. For the former we have not resolved the vma\n+\t * yet, and the latter we don't have a notifier (but have a vma).\n+\t *\n+\t */\n+\tstruct mm_struct *mm = hmm_range_fault_mm(range);\n \tint ret;\n \n \tmmap_assert_locked(mm);\n \n \tdo {\n \t\t/* If range is no longer valid force retry. */\n-\t\tif (mmu_interval_check_retry(range-\u003enotifier,\n-\t\t\t\t\t range-\u003enotifier_seq))\n-\t\t\treturn -EBUSY;\n+\t\tif (range-\u003enotifier \u0026\u0026 mmu_interval_check_retry(range-\u003enotifier,\n+\t\t\t\t\t\t\t\trange-\u003enotifier_seq)) {\n+\t\t\tret = -EBUSY;\n+\t\t\tbreak;\n+\t\t}\n+\n \t\tret = walk_page_range(mm, hmm_vma_walk.last, range-\u003eend,\n \t\t\t\t \u0026hmm_walk_ops, \u0026hmm_vma_walk);\n \t\t/*\n@@ -681,6 +1514,9 @@ int hmm_range_fault(struct hmm_range *range)\n \t\t * output, and all \u003e= are still at their input values.\n \t\t */\n \t} while (ret == -EBUSY);\n+\n+\thmm_vma_post_range_fault(\u0026hmm_vma_walk);\n+\n \treturn ret;\n }\n EXPORT_SYMBOL(hmm_range_fault);\ndiff --git a/mm/migrate_device.c b/mm/migrate_device.c\nindex 908d2d4ec43ac..3d021b2e81f0c 100644\n--- a/mm/migrate_device.c\n+++ b/mm/migrate_device.c\n@@ -18,509 +18,6 @@\n #include \u003casm/tlbflush.h\u003e\n #include \"internal.h\"\n \n-static int migrate_vma_collect_skip(unsigned long start,\n-\t\t\t\t unsigned long end,\n-\t\t\t\t struct mm_walk *walk)\n-{\n-\tstruct migrate_vma *migrate = walk-\u003eprivate;\n-\tunsigned long addr;\n-\n-\tfor (addr = start; addr \u003c end; addr += PAGE_SIZE) {\n-\t\tmigrate-\u003edst[migrate-\u003enpages] = 0;\n-\t\tmigrate-\u003esrc[migrate-\u003enpages++] = 0;\n-\t}\n-\n-\treturn 0;\n-}\n-\n-static int migrate_vma_collect_hole(unsigned long start,\n-\t\t\t\t unsigned long end,\n-\t\t\t\t __always_unused int depth,\n-\t\t\t\t struct mm_walk *walk)\n-{\n-\tstruct migrate_vma *migrate = walk-\u003eprivate;\n-\tunsigned long addr;\n-\n-\t/* Only allow populating anonymous memory. */\n-\tif (!vma_is_anonymous(walk-\u003evma))\n-\t\treturn migrate_vma_collect_skip(start, end, walk);\n-\n-\tif (thp_migration_supported() \u0026\u0026\n-\t\t(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_COMPOUND) \u0026\u0026\n-\t\t(IS_ALIGNED(start, HPAGE_PMD_SIZE) \u0026\u0026\n-\t\t IS_ALIGNED(end, HPAGE_PMD_SIZE))) {\n-\t\tmigrate-\u003esrc[migrate-\u003enpages] = MIGRATE_PFN_MIGRATE |\n-\t\t\t\t\t\tMIGRATE_PFN_COMPOUND;\n-\t\tmigrate-\u003edst[migrate-\u003enpages] = 0;\n-\t\tmigrate-\u003enpages++;\n-\t\tmigrate-\u003ecpages++;\n-\n-\t\t/*\n-\t\t * Collect the remaining entries as holes, in case we\n-\t\t * need to split later\n-\t\t */\n-\t\treturn migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);\n-\t}\n-\n-\tfor (addr = start; addr \u003c end; addr += PAGE_SIZE) {\n-\t\tmigrate-\u003esrc[migrate-\u003enpages] = MIGRATE_PFN_MIGRATE;\n-\t\tmigrate-\u003edst[migrate-\u003enpages] = 0;\n-\t\tmigrate-\u003enpages++;\n-\t\tmigrate-\u003ecpages++;\n-\t}\n-\n-\treturn 0;\n-}\n-\n-/**\n- * migrate_vma_split_folio() - Helper function to split a THP folio\n- * @folio: the folio to split\n- * @fault_page: struct page associated with the fault if any\n- *\n- * Returns 0 on success\n- */\n-static int migrate_vma_split_folio(struct folio *folio,\n-\t\t\t\t struct page *fault_page)\n-{\n-\tint ret;\n-\tstruct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;\n-\tstruct folio *new_fault_folio = NULL;\n-\n-\tif (folio != fault_folio) {\n-\t\tfolio_get(folio);\n-\t\tfolio_lock(folio);\n-\t}\n-\n-\tret = split_folio(folio);\n-\tif (ret) {\n-\t\tif (folio != fault_folio) {\n-\t\t\tfolio_unlock(folio);\n-\t\t\tfolio_put(folio);\n-\t\t}\n-\t\treturn ret;\n-\t}\n-\n-\tnew_fault_folio = fault_page ? page_folio(fault_page) : NULL;\n-\n-\t/*\n-\t * Ensure the lock is held on the correct\n-\t * folio after the split\n-\t */\n-\tif (!new_fault_folio) {\n-\t\tfolio_unlock(folio);\n-\t\tfolio_put(folio);\n-\t} else if (folio != new_fault_folio) {\n-\t\tif (new_fault_folio != fault_folio) {\n-\t\t\tfolio_get(new_fault_folio);\n-\t\t\tfolio_lock(new_fault_folio);\n-\t\t}\n-\t\tfolio_unlock(folio);\n-\t\tfolio_put(folio);\n-\t}\n-\n-\treturn 0;\n-}\n-\n-/** migrate_vma_collect_huge_pmd - collect THP pages without splitting the\n- * folio for device private pages.\n- * @pmdp: pointer to pmd entry\n- * @start: start address of the range for migration\n- * @end: end address of the range for migration\n- * @walk: mm_walk callback structure\n- * @fault_folio: folio associated with the fault if any\n- *\n- * Collect the huge pmd entry at @pmdp for migration and set the\n- * MIGRATE_PFN_COMPOUND flag in the migrate src entry to indicate that\n- * migration will occur at HPAGE_PMD granularity\n- */\n-static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,\n-\t\t\t\t\tunsigned long end, struct mm_walk *walk,\n-\t\t\t\t\tstruct folio *fault_folio)\n-{\n-\tstruct mm_struct *mm = walk-\u003emm;\n-\tstruct folio *folio;\n-\tstruct migrate_vma *migrate = walk-\u003eprivate;\n-\tspinlock_t *ptl;\n-\tint ret;\n-\tunsigned long write = 0;\n-\n-\tptl = pmd_lock(mm, pmdp);\n-\tif (pmd_none(*pmdp)) {\n-\t\tspin_unlock(ptl);\n-\t\treturn migrate_vma_collect_hole(start, end, -1, walk);\n-\t}\n-\n-\tif (pmd_trans_huge(*pmdp)) {\n-\t\tif (!(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_SYSTEM)) {\n-\t\t\tspin_unlock(ptl);\n-\t\t\treturn migrate_vma_collect_skip(start, end, walk);\n-\t\t}\n-\n-\t\tfolio = pmd_folio(*pmdp);\n-\t\tif (is_huge_zero_folio(folio)) {\n-\t\t\tspin_unlock(ptl);\n-\t\t\treturn migrate_vma_collect_hole(start, end, -1, walk);\n-\t\t}\n-\t\tif (pmd_write(*pmdp))\n-\t\t\twrite = MIGRATE_PFN_WRITE;\n-\t} else if (!pmd_present(*pmdp)) {\n-\t\tconst softleaf_t entry = softleaf_from_pmd(*pmdp);\n-\n-\t\tfolio = softleaf_to_folio(entry);\n-\n-\t\tif (!softleaf_is_device_private(entry) ||\n-\t\t\t!(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||\n-\t\t\t(folio-\u003epgmap-\u003eowner != migrate-\u003epgmap_owner)) {\n-\t\t\tspin_unlock(ptl);\n-\t\t\treturn migrate_vma_collect_skip(start, end, walk);\n-\t\t}\n-\n-\t\tif (softleaf_is_device_private_write(entry))\n-\t\t\twrite = MIGRATE_PFN_WRITE;\n-\t} else {\n-\t\tspin_unlock(ptl);\n-\t\treturn -EAGAIN;\n-\t}\n-\n-\tfolio_get(folio);\n-\tif (folio != fault_folio \u0026\u0026 unlikely(!folio_trylock(folio))) {\n-\t\tspin_unlock(ptl);\n-\t\tfolio_put(folio);\n-\t\treturn migrate_vma_collect_skip(start, end, walk);\n-\t}\n-\n-\tif (thp_migration_supported() \u0026\u0026\n-\t\t(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_COMPOUND) \u0026\u0026\n-\t\t(IS_ALIGNED(start, HPAGE_PMD_SIZE) \u0026\u0026\n-\t\t IS_ALIGNED(end, HPAGE_PMD_SIZE))) {\n-\n-\t\tstruct page_vma_mapped_walk pvmw = {\n-\t\t\t.ptl = ptl,\n-\t\t\t.address = start,\n-\t\t\t.pmd = pmdp,\n-\t\t\t.vma = walk-\u003evma,\n-\t\t};\n-\n-\t\tunsigned long pfn = page_to_pfn(folio_page(folio, 0));\n-\n-\t\tmigrate-\u003esrc[migrate-\u003enpages] = migrate_pfn(pfn) | write\n-\t\t\t\t\t\t| MIGRATE_PFN_MIGRATE\n-\t\t\t\t\t\t| MIGRATE_PFN_COMPOUND;\n-\t\tmigrate-\u003edst[migrate-\u003enpages++] = 0;\n-\t\tmigrate-\u003ecpages++;\n-\t\tret = set_pmd_migration_entry(\u0026pvmw, folio_page(folio, 0));\n-\t\tif (ret) {\n-\t\t\tmigrate-\u003enpages--;\n-\t\t\tmigrate-\u003ecpages--;\n-\t\t\tmigrate-\u003esrc[migrate-\u003enpages] = 0;\n-\t\t\tmigrate-\u003edst[migrate-\u003enpages] = 0;\n-\t\t\tgoto fallback;\n-\t\t}\n-\t\tmigrate_vma_collect_skip(start + PAGE_SIZE, end, walk);\n-\t\tspin_unlock(ptl);\n-\t\treturn 0;\n-\t}\n-\n-fallback:\n-\tspin_unlock(ptl);\n-\tif (!folio_test_large(folio))\n-\t\tgoto done;\n-\tret = split_folio(folio);\n-\tif (fault_folio != folio)\n-\t\tfolio_unlock(folio);\n-\tfolio_put(folio);\n-\tif (ret)\n-\t\treturn migrate_vma_collect_skip(start, end, walk);\n-\tif (pmd_none(pmdp_get_lockless(pmdp)))\n-\t\treturn migrate_vma_collect_hole(start, end, -1, walk);\n-\n-done:\n-\treturn -ENOENT;\n-}\n-\n-static int migrate_vma_collect_pmd(pmd_t *pmdp,\n-\t\t\t\t unsigned long start,\n-\t\t\t\t unsigned long end,\n-\t\t\t\t struct mm_walk *walk)\n-{\n-\tstruct migrate_vma *migrate = walk-\u003eprivate;\n-\tstruct vm_area_struct *vma = walk-\u003evma;\n-\tstruct mm_struct *mm = vma-\u003evm_mm;\n-\tunsigned long addr = start, unmapped = 0;\n-\tspinlock_t *ptl;\n-\tstruct folio *fault_folio = migrate-\u003efault_page ?\n-\t\tpage_folio(migrate-\u003efault_page) : NULL;\n-\tpte_t *ptep;\n-\n-again:\n-\tif (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) {\n-\t\tint ret = migrate_vma_collect_huge_pmd(pmdp, start, end, walk, fault_folio);\n-\n-\t\tif (ret == -EAGAIN)\n-\t\t\tgoto again;\n-\t\tif (ret == 0)\n-\t\t\treturn 0;\n-\t}\n-\n-\tptep = pte_offset_map_lock(mm, pmdp, start, \u0026ptl);\n-\tif (!ptep)\n-\t\tgoto again;\n-\tlazy_mmu_mode_enable();\n-\tptep += (addr - start) / PAGE_SIZE;\n-\n-\tfor (; addr \u003c end; addr += PAGE_SIZE, ptep++) {\n-\t\tstruct dev_pagemap *pgmap;\n-\t\tunsigned long mpfn = 0, pfn;\n-\t\tstruct folio *folio;\n-\t\tstruct page *page;\n-\t\tsoftleaf_t entry;\n-\t\tpte_t pte;\n-\n-\t\tpte = ptep_get(ptep);\n-\n-\t\tif (pte_none(pte)) {\n-\t\t\tif (vma_is_anonymous(vma)) {\n-\t\t\t\tmpfn = MIGRATE_PFN_MIGRATE;\n-\t\t\t\tmigrate-\u003ecpages++;\n-\t\t\t}\n-\t\t\tgoto next;\n-\t\t}\n-\n-\t\tif (!pte_present(pte)) {\n-\t\t\t/*\n-\t\t\t * Only care about unaddressable device page special\n-\t\t\t * page table entry. Other special swap entries are not\n-\t\t\t * migratable, and we ignore regular swapped page.\n-\t\t\t */\n-\t\t\tentry = softleaf_from_pte(pte);\n-\t\t\tif (!softleaf_is_device_private(entry))\n-\t\t\t\tgoto next;\n-\n-\t\t\tpage = softleaf_to_page(entry);\n-\t\t\tpgmap = page_pgmap(page);\n-\t\t\tif (!(migrate-\u003eflags \u0026\n-\t\t\t\tMIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||\n-\t\t\t pgmap-\u003eowner != migrate-\u003epgmap_owner)\n-\t\t\t\tgoto next;\n-\n-\t\t\tfolio = page_folio(page);\n-\t\t\tif (folio_test_large(folio)) {\n-\t\t\t\tint ret;\n-\n-\t\t\t\tlazy_mmu_mode_disable();\n-\t\t\t\tpte_unmap_unlock(ptep, ptl);\n-\t\t\t\tret = migrate_vma_split_folio(folio,\n-\t\t\t\t\t\t\t migrate-\u003efault_page);\n-\n-\t\t\t\tif (ret) {\n-\t\t\t\t\tif (unmapped)\n-\t\t\t\t\t\tflush_tlb_range(walk-\u003evma, start, end);\n-\n-\t\t\t\t\treturn migrate_vma_collect_skip(addr, end, walk);\n-\t\t\t\t}\n-\n-\t\t\t\tgoto again;\n-\t\t\t}\n-\n-\t\t\tmpfn = migrate_pfn(page_to_pfn(page)) |\n-\t\t\t\t\tMIGRATE_PFN_MIGRATE;\n-\t\t\tif (softleaf_is_device_private_write(entry))\n-\t\t\t\tmpfn |= MIGRATE_PFN_WRITE;\n-\t\t} else {\n-\t\t\tpfn = pte_pfn(pte);\n-\t\t\tif (is_zero_pfn(pfn) \u0026\u0026\n-\t\t\t (migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_SYSTEM)) {\n-\t\t\t\tmpfn = MIGRATE_PFN_MIGRATE;\n-\t\t\t\tmigrate-\u003ecpages++;\n-\t\t\t\tgoto next;\n-\t\t\t}\n-\t\t\tpage = vm_normal_page(migrate-\u003evma, addr, pte);\n-\t\t\tif (page \u0026\u0026 !is_zone_device_page(page) \u0026\u0026\n-\t\t\t !(migrate-\u003eflags \u0026 MIGRATE_VMA_SELECT_SYSTEM)) {\n-\t\t\t\tgoto next;\n-\t\t\t} else if (page \u0026\u0026 is_device_coherent_page(page)) {\n-\t\t\t\tpgmap = page_pgmap(page);\n-\n-\t\t\t\tif (!(migrate-\u003eflags \u0026\n-\t\t\t\t\tMIGRATE_VMA_SELECT_DEVICE_COHERENT) ||\n-\t\t\t\t\tpgmap-\u003eowner != migrate-\u003epgmap_owner)\n-\t\t\t\t\tgoto next;\n-\t\t\t}\n-\t\t\tfolio = page ? page_folio(page) : NULL;\n-\t\t\tif (folio \u0026\u0026 folio_test_large(folio)) {\n-\t\t\t\tint ret;\n-\n-\t\t\t\tlazy_mmu_mode_disable();\n-\t\t\t\tpte_unmap_unlock(ptep, ptl);\n-\t\t\t\tret = migrate_vma_split_folio(folio,\n-\t\t\t\t\t\t\t migrate-\u003efault_page);\n-\n-\t\t\t\tif (ret) {\n-\t\t\t\t\tif (unmapped)\n-\t\t\t\t\t\tflush_tlb_range(walk-\u003evma, start, end);\n-\n-\t\t\t\t\treturn migrate_vma_collect_skip(addr, end, walk);\n-\t\t\t\t}\n-\n-\t\t\t\tgoto again;\n-\t\t\t}\n-\t\t\tmpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;\n-\t\t\tmpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;\n-\t\t}\n-\n-\t\tif (!page || !page-\u003emapping) {\n-\t\t\tmpfn = 0;\n-\t\t\tgoto next;\n-\t\t}\n-\n-\t\t/*\n-\t\t * By getting a reference on the folio we pin it and that blocks\n-\t\t * any kind of migration. Side effect is that it \"freezes\" the\n-\t\t * pte.\n-\t\t *\n-\t\t * We drop this reference after isolating the folio from the lru\n-\t\t * for non device folio (device folio are not on the lru and thus\n-\t\t * can't be dropped from it).\n-\t\t */\n-\t\tfolio = page_folio(page);\n-\t\tfolio_get(folio);\n-\n-\t\t/*\n-\t\t * We rely on folio_trylock() to avoid deadlock between\n-\t\t * concurrent migrations where each is waiting on the others\n-\t\t * folio lock. If we can't immediately lock the folio we fail this\n-\t\t * migration as it is only best effort anyway.\n-\t\t *\n-\t\t * If we can lock the folio it's safe to set up a migration entry\n-\t\t * now. In the common case where the folio is mapped once in a\n-\t\t * single process setting up the migration entry now is an\n-\t\t * optimisation to avoid walking the rmap later with\n-\t\t * try_to_migrate().\n-\t\t */\n-\t\tif (fault_folio == folio || folio_trylock(folio)) {\n-\t\t\tbool anon_exclusive;\n-\t\t\tpte_t swp_pte;\n-\n-\t\t\tif (pte_present(pte))\n-\t\t\t\tflush_cache_page(vma, addr, pte_pfn(pte));\n-\t\t\tanon_exclusive = folio_test_anon(folio) \u0026\u0026\n-\t\t\t\t\t PageAnonExclusive(page);\n-\t\t\tif (anon_exclusive) {\n-\t\t\t\tpte = ptep_clear_flush(vma, addr, ptep);\n-\n-\t\t\t\tif (folio_try_share_anon_rmap_pte(folio, page)) {\n-\t\t\t\t\tset_pte_at(mm, addr, ptep, pte);\n-\t\t\t\t\tif (fault_folio != folio)\n-\t\t\t\t\t\tfolio_unlock(folio);\n-\t\t\t\t\tfolio_put(folio);\n-\t\t\t\t\tmpfn = 0;\n-\t\t\t\t\tgoto next;\n-\t\t\t\t}\n-\t\t\t} else {\n-\t\t\t\tpte = ptep_get_and_clear(mm, addr, ptep);\n-\t\t\t}\n-\n-\t\t\tmigrate-\u003ecpages++;\n-\n-\t\t\t/* Set the dirty flag on the folio now the pte is gone. */\n-\t\t\tif (pte_present(pte) \u0026\u0026 pte_dirty(pte))\n-\t\t\t\tfolio_mark_dirty(folio);\n-\n-\t\t\t/* Setup special migration page table entry */\n-\t\t\tif (mpfn \u0026 MIGRATE_PFN_WRITE)\n-\t\t\t\tentry = make_writable_migration_entry(\n-\t\t\t\t\t\t\tpage_to_pfn(page));\n-\t\t\telse if (anon_exclusive)\n-\t\t\t\tentry = make_readable_exclusive_migration_entry(\n-\t\t\t\t\t\t\tpage_to_pfn(page));\n-\t\t\telse\n-\t\t\t\tentry = make_readable_migration_entry(\n-\t\t\t\t\t\t\tpage_to_pfn(page));\n-\t\t\tif (pte_present(pte)) {\n-\t\t\t\tif (pte_young(pte))\n-\t\t\t\t\tentry = make_migration_entry_young(entry);\n-\t\t\t\tif (pte_dirty(pte))\n-\t\t\t\t\tentry = make_migration_entry_dirty(entry);\n-\t\t\t}\n-\t\t\tswp_pte = swp_entry_to_pte(entry);\n-\t\t\tif (pte_present(pte)) {\n-\t\t\t\tif (pte_soft_dirty(pte))\n-\t\t\t\t\tswp_pte = pte_swp_mksoft_dirty(swp_pte);\n-\t\t\t\tif (pte_uffd_wp(pte))\n-\t\t\t\t\tswp_pte = pte_swp_mkuffd_wp(swp_pte);\n-\t\t\t} else {\n-\t\t\t\tif (pte_swp_soft_dirty(pte))\n-\t\t\t\t\tswp_pte = pte_swp_mksoft_dirty(swp_pte);\n-\t\t\t\tif (pte_swp_uffd_wp(pte))\n-\t\t\t\t\tswp_pte = pte_swp_mkuffd_wp(swp_pte);\n-\t\t\t}\n-\t\t\tset_pte_at(mm, addr, ptep, swp_pte);\n-\n-\t\t\t/*\n-\t\t\t * This is like regular unmap: we remove the rmap and\n-\t\t\t * drop the folio refcount. The folio won't be freed, as\n-\t\t\t * we took a reference just above.\n-\t\t\t */\n-\t\t\tfolio_remove_rmap_pte(folio, page, vma);\n-\t\t\tfolio_put(folio);\n-\n-\t\t\tif (pte_present(pte))\n-\t\t\t\tunmapped++;\n-\t\t} else {\n-\t\t\tfolio_put(folio);\n-\t\t\tmpfn = 0;\n-\t\t}\n-\n-next:\n-\t\tmigrate-\u003edst[migrate-\u003enpages] = 0;\n-\t\tmigrate-\u003esrc[migrate-\u003enpages++] = mpfn;\n-\t}\n-\n-\t/* Only flush the TLB if we actually modified any entries */\n-\tif (unmapped)\n-\t\tflush_tlb_range(walk-\u003evma, start, end);\n-\n-\tlazy_mmu_mode_disable();\n-\tpte_unmap_unlock(ptep - 1, ptl);\n-\n-\treturn 0;\n-}\n-\n-static const struct mm_walk_ops migrate_vma_walk_ops = {\n-\t.pmd_entry\t\t= migrate_vma_collect_pmd,\n-\t.pte_hole\t\t= migrate_vma_collect_hole,\n-\t.walk_lock\t\t= PGWALK_RDLOCK,\n-};\n-\n-/*\n- * migrate_vma_collect() - collect pages over a range of virtual addresses\n- * @migrate: migrate struct containing all migration information\n- *\n- * This will walk the CPU page table. For each virtual address backed by a\n- * valid page, it updates the src array and takes a reference on the page, in\n- * order to pin the page until we lock it and unmap it.\n- */\n-static void migrate_vma_collect(struct migrate_vma *migrate)\n-{\n-\tstruct mmu_notifier_range range;\n-\n-\t/*\n-\t * Note that the pgmap_owner is passed to the mmu notifier callback so\n-\t * that the registered device driver can skip invalidating device\n-\t * private page mappings that won't be migrated.\n-\t */\n-\tmmu_notifier_range_init_owner(\u0026range, MMU_NOTIFY_MIGRATE, 0,\n-\t\tmigrate-\u003evma-\u003evm_mm, migrate-\u003estart, migrate-\u003eend,\n-\t\tmigrate-\u003epgmap_owner);\n-\tmmu_notifier_invalidate_range_start(\u0026range);\n-\n-\twalk_page_range(migrate-\u003evma-\u003evm_mm, migrate-\u003estart, migrate-\u003eend,\n-\t\t\t\u0026migrate_vma_walk_ops, migrate);\n-\n-\tmmu_notifier_invalidate_range_end(\u0026range);\n-\tmigrate-\u003eend = migrate-\u003estart + (migrate-\u003enpages \u003c\u003c PAGE_SHIFT);\n-}\n-\n /*\n * migrate_vma_check_page() - check if page is pinned or not\n * @page: struct page to check\n@@ -729,10 +226,20 @@ static void migrate_vma_unmap(struct migrate_vma *migrate)\n */\n int migrate_vma_setup(struct migrate_vma *args)\n {\n+\tint ret;\n \tlong nr_pages = (args-\u003eend - args-\u003estart) \u003e\u003e PAGE_SHIFT;\n+\tstruct hmm_range range = {\n+\t\t.notifier = NULL,\n+\t\t.hmm_pfns = args-\u003esrc,\n+\t\t.dev_private_owner = args-\u003epgmap_owner,\n+\t\t.migrate = args,\n+\t\t.default_flags = HMM_PFN_REQ_MIGRATE\n+\t};\n \n \targs-\u003estart \u0026= PAGE_MASK;\n \targs-\u003eend \u0026= PAGE_MASK;\n+\trange.start = args-\u003estart;\n+\trange.end = args-\u003eend;\n \tif (!args-\u003evma || is_vm_hugetlb_page(args-\u003evma) ||\n \t (args-\u003evma-\u003evm_flags \u0026 VM_SPECIAL) || vma_is_dax(args-\u003evma))\n \t\treturn -EINVAL;\n@@ -754,10 +261,24 @@ int migrate_vma_setup(struct migrate_vma *args)\n \targs-\u003ecpages = 0;\n \targs-\u003enpages = 0;\n \n-\tmigrate_vma_collect(args);\n+\tif (args-\u003eflags \u0026 MIGRATE_VMA_FAULT)\n+\t\trange.default_flags |= HMM_PFN_REQ_FAULT;\n \n-\tif (args-\u003ecpages)\n-\t\tmigrate_vma_unmap(args);\n+\tif (args-\u003eflags \u0026 MIGRATE_VMA_WRITE)\n+\t\trange.default_flags |= HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE;\n+\n+\tret = hmm_range_fault(\u0026range);\n+\n+\tmigrate_hmm_range_setup(\u0026range);\n+\n+\t/* Remove migration PTEs */\n+\tif (ret) {\n+\t\tmigrate_vma_pages(args);\n+\t\tmigrate_vma_finalize(args);\n+\t\tmemset(args-\u003esrc, 0, sizeof(*args-\u003esrc) * nr_pages);\n+\t\targs-\u003ecpages = 0;\n+\t\targs-\u003enpages = 0;\n+\t}\n \n \t/*\n \t * At this point pages are locked and unmapped, and thus they have\n@@ -1487,3 +1008,58 @@ int migrate_device_coherent_folio(struct folio *folio)\n \t\treturn 0;\n \treturn -EBUSY;\n }\n+\n+/**\n+ * migrate_hmm_range_setup() - prepare to migrate a range of memory\n+ * @range: contains pointer to struct migrate_vma to be set up.\n+ *\n+ * When collecting has been done with hmm_range_fault(), this\n+ * should be called next, and completes range-\u003emigrate by\n+ * populating migrate-\u003esrc[] and migrate-\u003edst[]\n+ * using range-\u003ehmm_pfns[].\n+ * Also, migrate-\u003ecpages and migrate-\u003enpages get initialized.\n+ * After migrate_hmm_range_setup(), range-\u003emigrate is good\n+ * for the rest of the migrate_vma_* flow.\n+ */\n+void migrate_hmm_range_setup(struct hmm_range *range)\n+{\n+\tstruct migrate_vma *migrate = range-\u003emigrate;\n+\n+\tif (!migrate)\n+\t\treturn;\n+\n+\tmigrate-\u003enpages = (migrate-\u003eend - migrate-\u003estart) \u003e\u003e PAGE_SHIFT;\n+\tmigrate-\u003ecpages = 0;\n+\n+\tfor (unsigned long i = 0; i \u003c migrate-\u003enpages; i++) {\n+\t\tunsigned long pfn = range-\u003ehmm_pfns[i];\n+\n+\t\t/*\n+\t\t * We are only interested in entries to be\n+\t\t * migrated.\n+\t\t */\n+\t\tif (!(pfn \u0026 HMM_PFN_MIGRATE)) {\n+\t\t\tmigrate-\u003esrc[i] = 0;\n+\t\t\tmigrate-\u003edst[i] = 0;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tmigrate-\u003ecpages++;\n+\n+\t\t/* HMM_PFN_MIGRATE without HMM_PFN_VALID denotes the special zero page */\n+\t\tif (pfn \u0026 HMM_PFN_VALID)\n+\t\t\tmigrate-\u003esrc[i] = migrate_pfn(page_to_pfn(hmm_pfn_to_page(pfn)));\n+\t\telse\n+\t\t\tmigrate-\u003esrc[i] = 0;\n+\n+\t\tmigrate-\u003esrc[i] |= MIGRATE_PFN_MIGRATE;\n+\t\tmigrate-\u003esrc[i] |= (pfn \u0026 HMM_PFN_WRITE) ? MIGRATE_PFN_WRITE : 0;\n+\t\tmigrate-\u003esrc[i] |= (pfn \u0026 HMM_PFN_COMPOUND) ? MIGRATE_PFN_COMPOUND : 0;\n+\t\tmigrate-\u003edst[i] = 0;\n+\t}\n+\n+\tif (migrate-\u003ecpages)\n+\t\tmigrate_vma_unmap(migrate);\n+\n+}\n+EXPORT_SYMBOL(migrate_hmm_range_setup);\ndiff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c\nindex 2f2b9879d1005..4ac17cf6f8663 100644\n--- a/tools/testing/selftests/mm/hmm-tests.c\n+++ b/tools/testing/selftests/mm/hmm-tests.c\n@@ -283,6 +283,13 @@ static int hmm_migrate_sys_to_dev(int fd,\n \treturn hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_DEV, buffer, npages);\n }\n \n+static int hmm_migrate_on_fault_sys_to_dev(int fd,\n+\t\t\t\t\t struct hmm_buffer *buffer,\n+\t\t\t\t\t unsigned long npages)\n+{\n+\treturn hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV, buffer, npages);\n+}\n+\n static int hmm_migrate_dev_to_sys(int fd,\n \t\t\t\t struct hmm_buffer *buffer,\n \t\t\t\t unsigned long npages)\n@@ -990,6 +997,53 @@ TEST_F(hmm, migrate)\n \thmm_buffer_free(buffer);\n }\n \n+\n+/*\n+ * Fault and migrate anonymous memory to device private memory.\n+ */\n+TEST_F(hmm, migrate_on_fault)\n+{\n+\tstruct hmm_buffer *buffer;\n+\tunsigned long npages;\n+\tunsigned long size;\n+\tunsigned long i;\n+\tint *ptr;\n+\tint ret;\n+\n+\tnpages = ALIGN(HMM_BUFFER_SIZE, self-\u003epage_size) \u003e\u003e self-\u003epage_shift;\n+\tASSERT_NE(npages, 0);\n+\tsize = npages \u003c\u003c self-\u003epage_shift;\n+\n+\tbuffer = malloc(sizeof(*buffer));\n+\tASSERT_NE(buffer, NULL);\n+\n+\tbuffer-\u003efd = -1;\n+\tbuffer-\u003esize = size;\n+\tbuffer-\u003emirror = malloc(size);\n+\tASSERT_NE(buffer-\u003emirror, NULL);\n+\n+\tbuffer-\u003eptr = mmap(NULL, size,\n+\t\t\t PROT_READ | PROT_WRITE,\n+\t\t\t MAP_PRIVATE | MAP_ANONYMOUS,\n+\t\t\t buffer-\u003efd, 0);\n+\tASSERT_NE(buffer-\u003eptr, MAP_FAILED);\n+\n+\t/* Initialize buffer in system memory. */\n+\tfor (i = 0, ptr = buffer-\u003eptr; i \u003c size / sizeof(*ptr); ++i)\n+\t\tptr[i] = i;\n+\n+\t/* Fault and migrate memory to device. */\n+\tret = hmm_migrate_on_fault_sys_to_dev(self-\u003efd, buffer, npages);\n+\tASSERT_EQ(ret, 0);\n+\tASSERT_EQ(buffer-\u003ecpages, npages);\n+\n+\t/* Check what the device read. */\n+\tfor (i = 0, ptr = buffer-\u003emirror; i \u003c size / sizeof(*ptr); ++i)\n+\t\tASSERT_EQ(ptr[i], i);\n+\n+\thmm_buffer_free(buffer);\n+}\n+\n /*\n * Migrate private file memory to device private memory.\n */\n"
} |
| 2/1 | 2026/08/04 04:55 | agent | patch-evaluator |
2mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"TEST_HMM",
"TRANSPARENT_HUGEPAGE",
"DEVICE_PRIVATE",
"ZONE_DEVICE",
"MEMORY_HOTPLUG",
"MEMORY_HOTREMOVE",
"SPARSEMEM_VMEMMAP",
"MIGRATION",
"DEVICE_MIGRATION",
"HMM_MIRROR"
],
"FocusSymbols": [
"hmm_range_fault",
"migrate_vma_setup",
"migrate_hmm_range_setup"
],
"Reasoning": "The patch introduces a new feature to migrate memory via HMM during a page fault. It modifies core HMM and memory migration logic, which is reachable via the test_hmm driver or other HMM users. It adds new flags and modifies the behavior of hmm_range_fault and migrate_vma_setup. These are functional changes to core kernel memory management and should be fuzzed.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a '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: For your convenience, here is the diff of the changes:
commit 3a66202d0dc29fc656a6f451bf16a7b6faa27f33
Author: syz-cluster <triage@syzkaller.com>
Date: Tue Aug 4 04:55:13 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/hmm.h b/include/linux/hmm.h
index db75ffc949a7a..fbc9bb74d1608 100644
--- a/include/linux/hmm.h
+++ b/include/linux/hmm.h
@@ -13,6 +13,8 @@
struct mmu_interval_notifier;
+struct migrate_vma;
+
/*
* On output:
* 0 - The page is faultable and a future call with
@@ -27,6 +29,13 @@ struct mmu_interval_notifier;
* HMM_PFN_P2PDMA_BUS - Bus mapped P2P transfer
* HMM_PFN_DMA_MAPPED - Flag preserved on input-to-output transformation
* to mark that page is already DMA mapped
+ * HMM_PFN_MIGRATE - The entry is to be migrated. Note, HMM_PFN_MIGRATE
+ * alone without HMM_PFN_VALID denotes the
+ * empty page.
+ * This flag together with HMM_PFN_COMPOUND are
+ * indicators for migrate_hmm_range_setup() to
+ * setup the migrate pfns.
+ * HMM_PFN_COMPOUND - The entry is represents a > 0 order page
*
* On input:
* 0 - Return the current state of the page, do not fault it.
@@ -34,6 +43,8 @@ struct mmu_interval_notifier;
* will fail
* HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
* will fail. Must be combined with HMM_PFN_REQ_FAULT.
+ * HMM_PFN_REQ_MIGRATE - For default_flags, request to migrate, according to
+ * hmm_range.migrate.flags
*/
enum hmm_pfn_flags {
/* Output fields and flags */
@@ -48,11 +59,15 @@ enum hmm_pfn_flags {
HMM_PFN_P2PDMA = 1UL << (BITS_PER_LONG - 5),
HMM_PFN_P2PDMA_BUS = 1UL << (BITS_PER_LONG - 6),
- HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 11),
+ /* Migrate request */
+ HMM_PFN_MIGRATE = 1UL << (BITS_PER_LONG - 7),
+ HMM_PFN_COMPOUND = 1UL << (BITS_PER_LONG - 8),
+ HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 13),
/* Input flags */
HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
+ HMM_PFN_REQ_MIGRATE = HMM_PFN_MIGRATE,
HMM_PFN_FLAGS = ~((1UL << HMM_PFN_ORDER_SHIFT) - 1),
};
@@ -96,6 +111,28 @@ static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
}
+/*
+ * hmm_pfn_collected() - is this pfn entry prepared for migration ?
+ * If collected the folio's refcount is increased and the folio
+ * is locked.
+ */
+static inline bool hmm_pfn_collected(unsigned long hmm_pfn)
+{
+ return (hmm_pfn & (HMM_PFN_VALID | HMM_PFN_MIGRATE)) ==
+ (HMM_PFN_VALID | HMM_PFN_MIGRATE);
+}
+
+/*
+ * hmm_pfn_rollback() - undoes the collecction of hmm_pfn
+ *
+ * Note for total rollback the folio's refcount has to be put
+ * and folio has to be unlocked.
+ */
+static inline unsigned long hmm_pfn_rollback_collected(unsigned long hmm_pfn)
+{
+ return hmm_pfn & ~(HMM_PFN_VALID | HMM_PFN_MIGRATE | HMM_PFN_COMPOUND);
+}
+
/*
* struct hmm_range - track invalidation lock on virtual address range
*
@@ -107,6 +144,7 @@ static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
* @default_flags: default flags for the range (write, read, ... see hmm doc)
* @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
* @dev_private_owner: owner of device private pages
+ * @migrate: structure for migrating a range of a VMA
*/
struct hmm_range {
struct mmu_interval_notifier *notifier;
@@ -117,6 +155,7 @@ struct hmm_range {
unsigned long default_flags;
unsigned long pfn_flags_mask;
void *dev_private_owner;
+ struct migrate_vma *migrate;
};
/*
diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index d5af2b7f577b3..2b137e6a56d0f 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -3,6 +3,7 @@
#define _LINUX_MIGRATE_H
#include <linux/mm.h>
+#include <linux/hmm.h>
#include <linux/mempolicy.h>
#include <linux/migrate_mode.h>
#include <linux/hugetlb.h>
@@ -105,6 +106,28 @@ static inline void softleaf_entry_wait_on_locked(softleaf_t entry, spinlock_t *p
spin_unlock(ptl);
}
+enum migrate_vma_info {
+ MIGRATE_VMA_SELECT_NONE = 0,
+ MIGRATE_VMA_SELECT_COMPOUND = MIGRATE_VMA_SELECT_NONE,
+};
+
+static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range)
+{
+ return MIGRATE_VMA_SELECT_NONE;
+}
+
+static inline void hmm_fill_migrate_vma(struct hmm_range *range,
+ struct vm_area_struct *vma,
+ unsigned long start,
+ unsigned long end)
+{
+}
+
+static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range)
+{
+ return range->notifier->mm;
+}
+
#endif /* CONFIG_MIGRATION */
#ifdef CONFIG_NUMA_BALANCING
@@ -148,11 +171,13 @@ static inline unsigned long migrate_pfn(unsigned long pfn)
return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID;
}
-enum migrate_vma_direction {
+enum migrate_vma_info {
MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2,
MIGRATE_VMA_SELECT_COMPOUND = 1 << 3,
+ MIGRATE_VMA_FAULT = 1 << 4,
+ MIGRATE_VMA_WRITE = 1 << 5,
};
struct migrate_vma {
@@ -190,6 +215,31 @@ struct migrate_vma {
struct page *fault_page;
};
+static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range)
+{
+ enum migrate_vma_info minfo;
+
+ minfo = (range->default_flags & HMM_PFN_REQ_MIGRATE) ?
+ range->migrate->flags : 0;
+
+ return minfo;
+}
+
+static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range)
+{
+ return range->notifier ? range->notifier->mm : range->migrate->vma->vm_mm;
+}
+
+static inline void hmm_fill_migrate_vma(struct hmm_range *range,
+ struct vm_area_struct *vma,
+ unsigned long start,
+ unsigned long end)
+{
+ range->migrate->vma = vma;
+ range->migrate->start = start;
+ range->migrate->end = end;
+}
+
int migrate_vma_setup(struct migrate_vma *args);
void migrate_vma_pages(struct migrate_vma *migrate);
void migrate_vma_finalize(struct migrate_vma *migrate);
@@ -200,7 +250,7 @@ void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
unsigned long npages);
void migrate_device_finalize(unsigned long *src_pfns,
unsigned long *dst_pfns, unsigned long npages);
-
+void migrate_hmm_range_setup(struct hmm_range *range);
#endif /* CONFIG_MIGRATION */
#endif /* _LINUX_MIGRATE_H */
diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index c4adbf98fac79..46a83e0cb7091 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -36,6 +36,7 @@
#define DMIRROR_RANGE_FAULT_TIMEOUT 1000
#define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U)
#define DEVMEM_CHUNKS_RESERVE 16
+#define PFNS_ARRAY_SIZE 64
/*
* For device_private pages, dpage is just a dummy struct page
@@ -145,7 +146,7 @@ static bool dmirror_is_private_zone(struct dmirror_device *mdevice)
HMM_DMIRROR_MEMORY_DEVICE_PRIVATE);
}
-static enum migrate_vma_direction
+static enum migrate_vma_info
dmirror_select_device(struct dmirror *dmirror)
{
return (dmirror->mdevice->zone_device_type ==
@@ -355,6 +356,7 @@ static int dmirror_range_fault(struct dmirror *dmirror,
struct mm_struct *mm = dmirror->notifier.mm;
unsigned long timeout =
jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
+ bool migrate = range->default_flags & HMM_PFN_REQ_MIGRATE;
int ret;
while (true) {
@@ -364,9 +366,15 @@ static int dmirror_range_fault(struct dmirror *dmirror,
}
range->notifier_seq = mmu_interval_read_begin(range->notifier);
- mmap_read_lock(mm);
- ret = hmm_range_fault(range);
- mmap_read_unlock(mm);
+
+ /* mmap lock held for whole migrate on fault operation */
+ if (!migrate) {
+ mmap_read_lock(mm);
+ ret = hmm_range_fault(range);
+ mmap_read_unlock(mm);
+ } else {
+ ret = hmm_range_fault(range);
+ }
if (ret) {
if (ret == -EBUSY)
continue;
@@ -382,7 +390,9 @@ static int dmirror_range_fault(struct dmirror *dmirror,
break;
}
- ret = dmirror_do_fault(dmirror, range);
+ /* update device page table after migration */
+ if (!migrate)
+ ret = dmirror_do_fault(dmirror, range);
mutex_unlock(&dmirror->mutex);
out:
@@ -1281,6 +1291,114 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,
return ret;
}
+static int do_fault_and_migrate(struct dmirror *dmirror, struct hmm_range *range)
+{
+ struct migrate_vma *migrate = range->migrate;
+ int ret;
+
+ mmap_read_lock(dmirror->notifier.mm);
+
+ /* Fault-in pages for migration */
+ ret = dmirror_range_fault(dmirror, range);
+ /*
+ * Set this up even on error: hmm_range_fault() may have collected
+ * part of the range before failing.
+ */
+ migrate_hmm_range_setup(range);
+ if (ret) {
+ /*
+ * dst[] entries are empty, so this marks every collected migration
+ * as failed. finalize then restores the source mappings and drops
+ * the associated locks/references.
+ */
+ migrate_vma_pages(migrate);
+ migrate_vma_finalize(migrate);
+ goto out;
+ }
+
+ pr_debug("Migrating from sys mem to device mem\n");
+
+ dmirror_migrate_alloc_and_copy(migrate, dmirror);
+ migrate_vma_pages(migrate);
+ dmirror_migrate_finalize_and_map(migrate, dmirror);
+ migrate_vma_finalize(migrate);
+out:
+ mmap_read_unlock(dmirror->notifier.mm);
+ return ret;
+}
+
+static int dmirror_fault_and_migrate_to_device(struct dmirror *dmirror,
+ struct hmm_dmirror_cmd *cmd)
+{
+ unsigned long start, size, end, next;
+ unsigned long src_pfns[PFNS_ARRAY_SIZE] = { 0 };
+ unsigned long dst_pfns[PFNS_ARRAY_SIZE] = { 0 };
+ struct migrate_vma migrate = { 0 };
+ struct hmm_range range = { 0 };
+ struct dmirror_bounce bounce;
+ int ret = 0;
+
+ /* Whole range */
+ start = cmd->addr;
+ size = cmd->npages << PAGE_SHIFT;
+ end = start + size;
+
+ if (!mmget_not_zero(dmirror->notifier.mm)) {
+ ret = -EFAULT;
+ goto out;
+ }
+
+ migrate.pgmap_owner = dmirror->mdevice;
+ migrate.src = src_pfns;
+ migrate.dst = dst_pfns;
+ migrate.flags = MIGRATE_VMA_SELECT_SYSTEM;
+
+ range.migrate = &migrate;
+ range.hmm_pfns = src_pfns;
+ range.pfn_flags_mask = 0;
+ range.default_flags = HMM_PFN_REQ_FAULT | HMM_PFN_REQ_MIGRATE;
+ range.dev_private_owner = dmirror->mdevice;
+ range.notifier = &dmirror->notifier;
+
+ for (next = start; next < end; next = range.end) {
+ range.start = next;
+ range.end = min(end, next + (PFNS_ARRAY_SIZE << PAGE_SHIFT));
+
+ pr_debug("Fault and migrate range start:%#lx end:%#lx\n",
+ range.start, range.end);
+
+ ret = do_fault_and_migrate(dmirror, &range);
+ if (ret)
+ goto out_mmput;
+ }
+
+ /*
+ * Return the migrated data for verification.
+ * Only for pages in device zone
+ */
+ ret = dmirror_bounce_init(&bounce, start, size);
+ if (ret)
+ goto out_mmput;
+
+ mutex_lock(&dmirror->mutex);
+ ret = dmirror_do_read(dmirror, start, end, &bounce);
+ mutex_unlock(&dmirror->mutex);
+ if (ret == 0) {
+ ret = copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, bounce.size);
+ if (ret)
+ ret = -EFAULT;
+ }
+
+ cmd->cpages = bounce.cpages;
+ dmirror_bounce_fini(&bounce);
+
+
+out_mmput:
+ mmput(dmirror->notifier.mm);
+out:
+ return ret;
+}
+
static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range,
unsigned char *perm, unsigned long entry)
{
@@ -1547,6 +1665,10 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,
ret = dmirror_migrate_to_device(dmirror, &cmd);
break;
+ case HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV:
+ ret = dmirror_fault_and_migrate_to_device(dmirror, &cmd);
+ break;
+
case HMM_DMIRROR_MIGRATE_TO_SYS:
ret = dmirror_migrate_to_system(dmirror, &cmd);
break;
diff --git a/lib/test_hmm_uapi.h b/lib/test_hmm_uapi.h
index f94c6d4573382..0b6e7a419e365 100644
--- a/lib/test_hmm_uapi.h
+++ b/lib/test_hmm_uapi.h
@@ -29,15 +29,16 @@ struct hmm_dmirror_cmd {
};
/* Expose the address space of the calling process through hmm device file */
-#define HMM_DMIRROR_READ _IOWR('H', 0x00, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_WRITE _IOWR('H', 0x01, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_MIGRATE_TO_DEV _IOWR('H', 0x02, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_MIGRATE_TO_SYS _IOWR('H', 0x03, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x04, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x05, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x06, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_RELEASE _IOWR('H', 0x07, struct hmm_dmirror_cmd)
-#define HMM_DMIRROR_FLAGS _IOWR('H', 0x08, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_READ _IOWR('H', 0x00, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_WRITE _IOWR('H', 0x01, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_MIGRATE_TO_DEV _IOWR('H', 0x02, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV _IOWR('H', 0x03, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_MIGRATE_TO_SYS _IOWR('H', 0x04, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x05, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x06, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x07, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_RELEASE _IOWR('H', 0x08, struct hmm_dmirror_cmd)
+#define HMM_DMIRROR_FLAGS _IOWR('H', 0x09, struct hmm_dmirror_cmd)
#define HMM_DMIRROR_FLAG_FAIL_ALLOC (1ULL << 0)
diff --git a/mm/Kconfig b/mm/Kconfig
index 9e0ca48249054..7647671f37f09 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -699,6 +699,7 @@ config MIGRATION
config DEVICE_MIGRATION
def_bool MIGRATION && ZONE_DEVICE
+ select HMM_MIRROR
config ARCH_ENABLE_HUGEPAGE_MIGRATION
bool
@@ -1260,6 +1261,7 @@ config ZONE_DEVICE
config HMM_MIRROR
bool
depends on MMU
+ select MMU_NOTIFIER
config GET_FREE_REGION
bool
diff --git a/mm/hmm.c b/mm/hmm.c
index c72c9ddfdb2f6..53df67bbd87ef 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -20,6 +20,7 @@
#include <linux/pagemap.h>
#include <linux/leafops.h>
#include <linux/hugetlb.h>
+#include <linux/migrate.h>
#include <linux/memremap.h>
#include <linux/sched/mm.h>
#include <linux/jump_label.h>
@@ -27,14 +28,44 @@
#include <linux/pci-p2pdma.h>
#include <linux/mmu_notifier.h>
#include <linux/memory_hotplug.h>
+#include <asm/tlbflush.h>
#include "internal.h"
struct hmm_vma_walk {
- struct hmm_range *range;
- unsigned long last;
+ struct mmu_notifier_range mmu_range;
+ struct vm_area_struct *vma;
+ struct hmm_range *range;
+ unsigned long start;
+ unsigned long end;
+ unsigned long last;
+ /*
+ * For migration we need pte/pmd
+ * locked for the handle_* and
+ * prepare_* regions. While faulting
+ * we have to drop the locks and
+ * start again.
+ * ptelocked and pmdlocked
+ * hold the state and tells if need
+ * to drop locks before faulting.
+ * ptl is the lock held for pte or pmd.
+ *
+ */
+ bool ptelocked;
+ bool pmdlocked;
+ spinlock_t *ptl;
};
+#define HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, locked) \
+ WARN_ON_ONCE((hmm_vma_walk)->ptelocked != locked)
+
+#define HMM_ASSERT_PMD_LOCKED(hmm_vma_walk, locked) \
+ WARN_ON_ONCE((hmm_vma_walk)->pmdlocked != locked)
+
+#define HMM_ASSERT_UNLOCKED(hmm_vma_walk) \
+ WARN_ON_ONCE((hmm_vma_walk)->ptelocked || \
+ (hmm_vma_walk)->pmdlocked)
+
enum {
HMM_NEED_FAULT = 1 << 0,
HMM_NEED_WRITE_FAULT = 1 << 1,
@@ -47,15 +78,46 @@ enum {
HMM_PFN_P2PDMA_BUS,
};
+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,
+ unsigned long start,
+ unsigned long end,
+ unsigned long *hmm_pfn);
+
static int hmm_pfns_fill(unsigned long addr, unsigned long end,
- struct hmm_range *range, unsigned long cpu_flags)
+ struct hmm_vma_walk *hmm_vma_walk, unsigned long cpu_flags)
{
+ struct hmm_range *range = hmm_vma_walk->range;
unsigned long i = (addr - range->start) >> PAGE_SHIFT;
+ enum migrate_vma_info minfo;
+ bool migrate = false;
+
+ minfo = hmm_select_migrate(range);
+ if (cpu_flags != HMM_PFN_ERROR) {
+ if (minfo && (vma_is_anonymous(hmm_vma_walk->vma))) {
+ cpu_flags |= HMM_PFN_MIGRATE;
+ migrate = true;
+ }
+ }
+
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr, end, &range->hmm_pfns[i]);
+
+ if (migrate && thp_migration_supported() &&
+ (minfo & MIGRATE_VMA_SELECT_COMPOUND) &&
+ IS_ALIGNED(addr, HPAGE_PMD_SIZE) &&
+ IS_ALIGNED(end, HPAGE_PMD_SIZE) &&
+ end-addr == HPAGE_PMD_SIZE) {
+ range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
+ range->hmm_pfns[i] |= cpu_flags | HMM_PFN_COMPOUND;
+ addr += PAGE_SIZE;
+ i++;
+ cpu_flags = 0;
+ }
for (; addr < end; addr += PAGE_SIZE, i++) {
range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
range->hmm_pfns[i] |= cpu_flags;
}
+
return 0;
}
@@ -78,6 +140,7 @@ static int hmm_vma_fault(unsigned long addr, unsigned long end,
unsigned int fault_flags = FAULT_FLAG_REMOTE;
WARN_ON_ONCE(!required_fault);
+ HMM_ASSERT_UNLOCKED(hmm_vma_walk);
hmm_vma_walk->last = addr;
if (required_fault & HMM_NEED_WRITE_FAULT) {
@@ -171,11 +234,16 @@ static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
if (!walk->vma) {
if (required_fault)
return -EFAULT;
- return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
+ return hmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR);
}
- if (required_fault)
+ if (required_fault) {
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
return hmm_vma_fault(addr, end, required_fault, walk);
- return hmm_pfns_fill(addr, end, range, 0);
+ }
+ return hmm_pfns_fill(addr, end, hmm_vma_walk, 0);
}
static inline unsigned long hmm_pfn_flags_order(unsigned long order)
@@ -208,9 +276,16 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
required_fault =
hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
- if (required_fault)
+ if (required_fault) {
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
return hmm_vma_fault(addr, end, required_fault, walk);
+ }
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr,
+ end, hmm_pfns);
pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
@@ -289,14 +364,26 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
goto fault;
if (softleaf_is_migration(entry)) {
- pte_unmap(ptep);
- hmm_vma_walk->last = addr;
- migration_entry_wait(walk->mm, pmdp, addr);
- return -EBUSY;
+ if (!hmm_select_migrate(range)) {
+ HMM_ASSERT_UNLOCKED(hmm_vma_walk);
+ pte_unmap(ptep);
+ hmm_vma_walk->last = addr;
+ migration_entry_wait(walk->mm, pmdp, addr);
+ return -EBUSY;
+ }
+ return 0;
}
/* Report error for everything else */
- pte_unmap(ptep);
+
+ if (hmm_vma_walk->ptelocked) {
+ lazy_mmu_mode_disable();
+ pte_unmap_unlock(ptep, hmm_vma_walk->ptl);
+ hmm_vma_walk->ptelocked = false;
+ } else {
+ pte_unmap(ptep);
+ }
+
return -EFAULT;
}
@@ -313,7 +400,14 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
if (!vm_normal_page(walk->vma, addr, pte) &&
!is_zero_pfn(pte_pfn(pte))) {
if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
- pte_unmap(ptep);
+ if (hmm_vma_walk->ptelocked) {
+ lazy_mmu_mode_disable();
+ pte_unmap_unlock(ptep, hmm_vma_walk->ptl);
+ hmm_vma_walk->ptelocked = false;
+ } else {
+ pte_unmap(ptep);
+ }
+
return -EFAULT;
}
new_pfn_flags = HMM_PFN_ERROR;
@@ -322,11 +416,20 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
new_pfn_flags = pte_pfn(pte) | cpu_flags;
out:
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk, addr,
+ addr + PAGE_SIZE,
+ hmm_pfn);
*hmm_pfn = (*hmm_pfn & HMM_PFN_INOUT_FLAGS) | new_pfn_flags;
return 0;
fault:
- pte_unmap(ptep);
+ if (hmm_vma_walk->ptelocked) {
+ lazy_mmu_mode_disable();
+ pte_unmap_unlock(ptep, hmm_vma_walk->ptl);
+ hmm_vma_walk->ptelocked = false;
+ } else {
+ pte_unmap(ptep);
+ }
/* Fault any virtual address we were asked to fault */
return hmm_vma_fault(addr, end, required_fault, walk);
}
@@ -354,6 +457,9 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
if (softleaf_is_device_private_write(entry))
cpu_flags |= HMM_PFN_WRITE;
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,
+ start, end,
+ hmm_pfns);
/*
* Fully populate the PFN list though subsequent PFNs could be
* inferred, because drivers which are not yet aware of large
@@ -370,13 +476,18 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
npages, 0);
if (required_fault) {
- if (softleaf_is_device_private(entry))
+ if (softleaf_is_device_private(entry)) {
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
return hmm_vma_fault(addr, end, required_fault, walk);
+ }
else
return -EFAULT;
}
- return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
+ return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
}
#else
static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
@@ -384,15 +495,575 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
pmd_t pmd)
{
struct hmm_vma_walk *hmm_vma_walk = walk->private;
- struct hmm_range *range = hmm_vma_walk->range;
unsigned long npages = (end - start) >> PAGE_SHIFT;
if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
return -EFAULT;
- return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
+ return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
}
#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
+#ifdef CONFIG_DEVICE_MIGRATION
+/**
+ * migrate_vma_split_folio() - Helper function to split a THP folio
+ * @folio: the folio to split
+ * @fault_page: struct page associated with the fault if any
+ * @hmm_vma_walk: walk in progress
+ * @ptep: pte_t * for unmap and unlock ptl
+ *
+ * Returns 0 on success
+ */
+static int migrate_vma_split_folio(struct folio *folio,
+ struct page *fault_page,
+ struct hmm_vma_walk *hmm_vma_walk,
+ pte_t *ptep)
+{
+ int ret;
+ struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;
+ struct folio *new_fault_folio = NULL;
+
+ if (folio != fault_folio)
+ folio_get(folio);
+
+ lazy_mmu_mode_disable();
+ pte_unmap_unlock(ptep, hmm_vma_walk->ptl);
+ hmm_vma_walk->ptelocked = false;
+
+ if (folio != fault_folio)
+ folio_lock(folio);
+
+ ret = split_folio(folio);
+ if (ret) {
+ if (folio != fault_folio) {
+ folio_unlock(folio);
+ folio_put(folio);
+ }
+ return ret;
+ }
+
+ new_fault_folio = fault_page ? page_folio(fault_page) : NULL;
+
+ /*
+ * Ensure the lock is held on the correct
+ * folio after the split
+ */
+ if (!new_fault_folio) {
+ folio_unlock(folio);
+ folio_put(folio);
+ } else if (folio != new_fault_folio) {
+ if (new_fault_folio != fault_folio) {
+ folio_get(new_fault_folio);
+ folio_lock(new_fault_folio);
+ }
+ folio_unlock(folio);
+ folio_put(folio);
+ }
+
+ return 0;
+}
+
+/*
+ * Due to dropping ptl locks for splitting for instance, would we
+ * overwrite already collected pfns? This could happen when pmd
+ * pointing to a page table has vanished and been replaced
+ * with a leaf pmd, or another page table.
+ * In that case unref and unlock the folios,
+ * the pfns of which were collected from the disappeared
+ * page tables.
+ */
+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,
+ unsigned long start,
+ unsigned long end,
+ unsigned long *hmm_pfn)
+{
+ struct hmm_range *range = hmm_vma_walk->range;
+ struct migrate_vma *migrate = range->migrate;
+ struct folio *fault_folio = NULL;
+ enum migrate_vma_info minfo;
+ struct folio *folio;
+ unsigned long i;
+
+ minfo = hmm_select_migrate(range);
+ if (!minfo)
+ return;
+
+ WARN_ON_ONCE(!migrate);
+
+ fault_folio = migrate->fault_page ?
+ page_folio(migrate->fault_page) : NULL;
+
+ for (i = 0; start < end; start += PAGE_SIZE, i++) {
+ if (hmm_pfn_collected(hmm_pfn[i])) {
+ folio = page_folio(hmm_pfn_to_page(hmm_pfn[i]));
+ if (folio != fault_folio)
+ folio_unlock(folio);
+ folio_put(folio);
+ hmm_pfn[i] = hmm_pfn_rollback_collected(hmm_pfn[i]);
+
+ }
+ }
+}
+
+static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk,
+ pmd_t *pmdp,
+ unsigned long start,
+ unsigned long end,
+ unsigned long *hmm_pfn)
+{
+ struct hmm_vma_walk *hmm_vma_walk = walk->private;
+ struct hmm_range *range = hmm_vma_walk->range;
+ struct migrate_vma *migrate = range->migrate;
+ struct folio *fault_folio = NULL;
+ enum migrate_vma_info minfo;
+ struct folio *folio;
+ unsigned long i;
+ int r = 0;
+
+ // Do we want to migrate at all?
+ minfo = hmm_select_migrate(range);
+ if (!minfo)
+ return r;
+
+ WARN_ON_ONCE(!migrate);
+ HMM_ASSERT_PMD_LOCKED(hmm_vma_walk, true);
+
+ fault_folio = migrate->fault_page ?
+ page_folio(migrate->fault_page) : NULL;
+
+ if (pmd_none(*pmdp))
+ return hmm_pfns_fill(start, end, hmm_vma_walk, 0);
+
+ if (!(hmm_pfn[0] & HMM_PFN_VALID))
+ goto out;
+
+ if (pmd_trans_huge(*pmdp)) {
+ if (!(minfo & MIGRATE_VMA_SELECT_SYSTEM))
+ goto out;
+
+ folio = pmd_folio(*pmdp);
+ if (is_huge_zero_folio(folio))
+ return hmm_pfns_fill(start, end, hmm_vma_walk, 0);
+
+ } else if (!pmd_present(*pmdp)) {
+ const softleaf_t entry = softleaf_from_pmd(*pmdp);
+
+ if (!softleaf_is_device_private(entry))
+ goto out;
+
+ if (!(minfo & MIGRATE_VMA_SELECT_DEVICE_PRIVATE))
+ goto out;
+
+ folio = softleaf_to_folio(entry);
+ if (folio->pgmap->owner != migrate->pgmap_owner)
+ goto out;
+ } else {
+ hmm_vma_walk->last = start;
+ return -EBUSY;
+ }
+
+ folio_get(folio);
+
+ if (folio != fault_folio && unlikely(!folio_trylock(folio))) {
+ folio_put(folio);
+ hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
+ return 0;
+ }
+
+ if (thp_migration_supported() &&
+ (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
+ (IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
+ IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
+ struct page_vma_mapped_walk pvmw = {
+ .ptl = hmm_vma_walk->ptl,
+ .address = start,
+ .pmd = pmdp,
+ .vma = walk->vma,
+ };
+
+ hmm_pfn[0] |= HMM_PFN_MIGRATE | HMM_PFN_COMPOUND;
+
+ r = set_pmd_migration_entry(&pvmw, folio_page(folio, 0));
+ if (r) {
+ hmm_pfn[0] &= ~(HMM_PFN_MIGRATE | HMM_PFN_COMPOUND);
+ r = -ENOENT; // fallback
+ goto unlock_out;
+ }
+ for (i = 1, start += PAGE_SIZE; start < end; start += PAGE_SIZE, i++)
+ hmm_pfn[i] &= HMM_PFN_INOUT_FLAGS;
+
+ } else {
+ r = -ENOENT; // fallback
+ goto unlock_out;
+ }
+
+out:
+ return r;
+
+unlock_out:
+ if (folio != fault_folio)
+ folio_unlock(folio);
+ folio_put(folio);
+ goto out;
+}
+
+/*
+ * Install migration entries if migration requested, either from fault
+ * or migrate paths.
+ *
+ */
+static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk,
+ pmd_t *pmdp,
+ pte_t *ptep,
+ unsigned long addr,
+ unsigned long *hmm_pfn,
+ bool *unmapped)
+{
+ struct hmm_vma_walk *hmm_vma_walk = walk->private;
+ struct hmm_range *range = hmm_vma_walk->range;
+ struct migrate_vma *migrate = range->migrate;
+ struct mm_struct *mm = walk->vma->vm_mm;
+ struct folio *fault_folio = NULL;
+ enum migrate_vma_info minfo;
+ struct dev_pagemap *pgmap;
+ bool anon_exclusive;
+ struct folio *folio;
+ unsigned long pfn;
+ struct page *page;
+ softleaf_t entry;
+ pte_t pte, swp_pte;
+ bool writable = false;
+
+ // Do we want to migrate at all?
+ minfo = hmm_select_migrate(range);
+ if (!minfo)
+ return 0;
+
+ WARN_ON_ONCE(!migrate);
+ HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, true);
+
+ fault_folio = migrate->fault_page ?
+ page_folio(migrate->fault_page) : NULL;
+
+ pte = ptep_get(ptep);
+
+ if (pte_none(pte)) {
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,
+ addr,
+ addr + PAGE_SIZE,
+ hmm_pfn);
+
+ if (vma_is_anonymous(walk->vma)) {
+ *hmm_pfn &= HMM_PFN_INOUT_FLAGS;
+ *hmm_pfn |= HMM_PFN_MIGRATE;
+ goto out;
+ }
+ }
+
+ if (!(hmm_pfn[0] & HMM_PFN_VALID))
+ goto out;
+
+ if (!pte_present(pte)) {
+ /*
+ * Only care about unaddressable device page special
+ * page table entry. Other special swap entries are not
+ * migratable, and we ignore regular swapped page.
+ */
+ entry = softleaf_from_pte(pte);
+ if (!softleaf_is_device_private(entry))
+ goto out;
+
+ if (!(minfo & MIGRATE_VMA_SELECT_DEVICE_PRIVATE))
+ goto out;
+
+ page = softleaf_to_page(entry);
+ folio = page_folio(page);
+ if (folio->pgmap->owner != migrate->pgmap_owner)
+ goto out;
+
+ if (folio_test_large(folio)) {
+ int ret;
+
+ ret = migrate_vma_split_folio(folio,
+ migrate->fault_page,
+ hmm_vma_walk,
+ ptep);
+ if (ret)
+ goto out_error;
+ return -EAGAIN;
+ }
+
+ pfn = page_to_pfn(page);
+ if (softleaf_is_device_private_write(entry))
+ writable = true;
+ } else {
+ pfn = pte_pfn(pte);
+ if (is_zero_pfn(pfn) &&
+ (minfo & MIGRATE_VMA_SELECT_SYSTEM)) {
+ hmm_vma_handle_migrate_prepare_rollback(hmm_vma_walk,
+ addr,
+ addr + PAGE_SIZE,
+ hmm_pfn);
+ *hmm_pfn = HMM_PFN_MIGRATE;
+ goto out;
+ }
+ page = vm_normal_page(walk->vma, addr, pte);
+ if (page && !is_zone_device_page(page) &&
+ !(minfo & MIGRATE_VMA_SELECT_SYSTEM)) {
+ goto out;
+ } else if (page && is_device_coherent_page(page)) {
+ pgmap = page_pgmap(page);
+
+ if (!(minfo &
+ MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
+ pgmap->owner != migrate->pgmap_owner)
+ goto out;
+ }
+
+ folio = page ? page_folio(page) : NULL;
+ if (folio && folio_test_large(folio)) {
+ int ret;
+
+ ret = migrate_vma_split_folio(folio,
+ migrate->fault_page,
+ hmm_vma_walk,
+ ptep);
+ if (ret)
+ goto out_error;
+ return -EAGAIN;
+ }
+
+ writable = pte_write(pte);
+ }
+
+ if (!page || !page->mapping)
+ goto out;
+
+ /*
+ * By getting a reference on the folio we pin it and that blocks
+ * any kind of migration. Side effect is that it "freezes" the
+ * pte.
+ *
+ * We drop this reference after isolating the folio from the lru
+ * for non device folio (device folio are not on the lru and thus
+ * can't be dropped from it).
+ */
+ folio = page_folio(page);
+ folio_get(folio);
+
+ /*
+ * We rely on folio_trylock() to avoid deadlock between
+ * concurrent migrations where each is waiting on the others
+ * folio lock. If we can't immediately lock the folio we fail this
+ * migration as it is only best effort anyway.
+ *
+ * If we can lock the folio it's safe to set up a migration entry
+ * now. In the common case where the folio is mapped once in a
+ * single process setting up the migration entry now is an
+ * optimisation to avoid walking the rmap later with
+ * try_to_migrate().
+ */
+
+ if (fault_folio == folio || folio_trylock(folio)) {
+ anon_exclusive = folio_test_anon(folio) &&
+ PageAnonExclusive(page);
+
+ if (pte_present(pte))
+ flush_cache_page(walk->vma, addr, pfn);
+
+ if (anon_exclusive) {
+ pte = ptep_clear_flush(walk->vma, addr, ptep);
+
+ if (folio_try_share_anon_rmap_pte(folio, page)) {
+ set_pte_at(mm, addr, ptep, pte);
+ folio_unlock(folio);
+ folio_put(folio);
+ goto out;
+ }
+ } else {
+ pte = ptep_get_and_clear(mm, addr, ptep);
+ }
+
+ if (pte_present(pte) && pte_dirty(pte))
+ folio_mark_dirty(folio);
+
+ /* Setup special migration page table entry */
+ if (writable)
+ entry = make_writable_migration_entry(pfn);
+ else if (anon_exclusive)
+ entry = make_readable_exclusive_migration_entry(pfn);
+ else
+ entry = make_readable_migration_entry(pfn);
+
+ if (pte_present(pte)) {
+ if (pte_young(pte))
+ entry = make_migration_entry_young(entry);
+ if (pte_dirty(pte))
+ entry = make_migration_entry_dirty(entry);
+ }
+
+ swp_pte = swp_entry_to_pte(entry);
+ if (pte_present(pte)) {
+ if (pte_soft_dirty(pte))
+ swp_pte = pte_swp_mksoft_dirty(swp_pte);
+ if (pte_uffd_wp(pte))
+ swp_pte = pte_swp_mkuffd_wp(swp_pte);
+ } else {
+ if (pte_swp_soft_dirty(pte))
+ swp_pte = pte_swp_mksoft_dirty(swp_pte);
+ if (pte_swp_uffd_wp(pte))
+ swp_pte = pte_swp_mkuffd_wp(swp_pte);
+ }
+
+ set_pte_at(mm, addr, ptep, swp_pte);
+ folio_remove_rmap_pte(folio, page, walk->vma);
+ folio_put(folio);
+ *hmm_pfn |= HMM_PFN_MIGRATE;
+ if (pte_present(pte))
+ *unmapped = true;
+ } else {
+ folio_put(folio);
+ }
+out:
+ return 0;
+out_error:
+ return -EFAULT;
+}
+
+static int hmm_vma_walk_split(pmd_t *pmdp,
+ unsigned long addr,
+ struct mm_walk *walk)
+{
+ struct hmm_vma_walk *hmm_vma_walk = walk->private;
+ struct hmm_range *range = hmm_vma_walk->range;
+ struct migrate_vma *migrate = range->migrate;
+ struct folio *folio, *fault_folio;
+ spinlock_t *ptl;
+ int ret = 0;
+
+ HMM_ASSERT_UNLOCKED(hmm_vma_walk);
+
+ fault_folio = (migrate && migrate->fault_page) ?
+ page_folio(migrate->fault_page) : NULL;
+
+ ptl = pmd_lock(walk->mm, pmdp);
+ if (unlikely(!pmd_trans_huge(*pmdp))) {
+ spin_unlock(ptl);
+ goto out;
+ }
+
+ folio = pmd_folio(*pmdp);
+ if (is_huge_zero_folio(folio)) {
+ spin_unlock(ptl);
+ split_huge_pmd(walk->vma, pmdp, addr);
+ } else {
+ folio_get(folio);
+ spin_unlock(ptl);
+
+ if (folio != fault_folio) {
+ if (unlikely(!folio_trylock(folio))) {
+ folio_put(folio);
+ ret = -EBUSY;
+ goto out;
+ }
+ } else {
+ folio_put(folio);
+ }
+
+ ret = split_folio(folio);
+ if (fault_folio != folio) {
+ folio_unlock(folio);
+ folio_put(folio);
+ }
+ }
+out:
+ return ret;
+}
+#else
+static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *hmm_vma_walk,
+ unsigned long start,
+ unsigned long end,
+ unsigned long *hmm_pfn)
+{
+}
+
+static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk,
+ pmd_t *pmdp,
+ unsigned long start,
+ unsigned long end,
+ unsigned long *hmm_pfn)
+{
+ return 0;
+}
+
+static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk,
+ pmd_t *pmdp,
+ pte_t *ptep,
+ unsigned long addr,
+ unsigned long *hmm_pfn,
+ bool *unmapped)
+{
+ return 0;
+}
+
+static int hmm_vma_walk_split(pmd_t *pmdp,
+ unsigned long addr,
+ struct mm_walk *walk)
+{
+ return 0;
+}
+#endif
+
+static int hmm_vma_capture_migrate_range(unsigned long start,
+ unsigned long end,
+ struct mm_walk *walk)
+{
+ struct hmm_vma_walk *hmm_vma_walk = walk->private;
+ struct hmm_range *range = hmm_vma_walk->range;
+
+ if (!hmm_select_migrate(range))
+ return 0;
+
+ if (hmm_vma_walk->vma && (hmm_vma_walk->vma != walk->vma))
+ return -ERANGE;
+
+ hmm_vma_walk->vma = walk->vma;
+ hmm_vma_walk->start = start;
+ hmm_vma_walk->end = end;
+
+ if (end - start > range->end - range->start)
+ return -ERANGE;
+
+ if (!hmm_vma_walk->mmu_range.owner) {
+ mmu_notifier_range_init_owner(&hmm_vma_walk->mmu_range, MMU_NOTIFY_MIGRATE, 0,
+ walk->vma->vm_mm, start, end,
+ range->dev_private_owner);
+ mmu_notifier_invalidate_range_start(&hmm_vma_walk->mmu_range);
+ }
+
+ return 0;
+}
+
+static void hmm_vma_post_range_fault(struct hmm_vma_walk *hmm_vma_walk)
+{
+
+ struct hmm_range *range = hmm_vma_walk->range;
+
+ if (hmm_select_migrate(range) &&
+ hmm_vma_walk->mmu_range.owner) {
+ /*
+ * The migrate_vma path has the following initialized,
+ * so take care of fault path below.
+ */
+ if (range->notifier) {
+ hmm_fill_migrate_vma(range,
+ hmm_vma_walk->vma,
+ hmm_vma_walk->start,
+ hmm_vma_walk->end);
+ }
+ mmu_notifier_invalidate_range_end(&hmm_vma_walk->mmu_range);
+ }
+}
+
static int hmm_vma_walk_pmd(pmd_t *pmdp,
unsigned long start,
unsigned long end,
@@ -400,46 +1071,128 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp,
{
struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct hmm_range *range = hmm_vma_walk->range;
- unsigned long *hmm_pfns =
- &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
unsigned long npages = (end - start) >> PAGE_SHIFT;
+ struct mm_struct *mm = walk->vma->vm_mm;
+ unsigned long *hmm_pfns, *hmm_pfns_start;
+ enum migrate_vma_info minfo;
unsigned long addr = start;
+ bool unmapped = false;
+ unsigned long i;
pte_t *ptep;
pmd_t pmd;
+ int r = 0;
+ minfo = hmm_select_migrate(range);
+ hmm_pfns_start = &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
again:
- pmd = pmdp_get_lockless(pmdp);
- if (pmd_none(pmd))
- return hmm_vma_walk_hole(start, end, -1, walk);
+ hmm_pfns = &range->hmm_pfns[(addr - range->start) >> PAGE_SHIFT];
+ hmm_vma_walk->ptelocked = false;
+ hmm_vma_walk->pmdlocked = false;
+
+ if (minfo) {
+ hmm_vma_walk->ptl = pmd_lock(mm, pmdp);
+ hmm_vma_walk->pmdlocked = true;
+ pmd = pmdp_get(pmdp);
+ } else
+ pmd = pmdp_get_lockless(pmdp);
+
+ if (pmd_none(pmd)) {
+ r = hmm_vma_walk_hole(start, end, -1, walk);
+
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
+ return r;
+ }
if (thp_migration_supported() && pmd_is_migration_entry(pmd)) {
- if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
- hmm_vma_walk->last = addr;
- pmd_migration_entry_wait(walk->mm, pmdp);
- return -EBUSY;
+ if (!minfo) {
+ if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns_start, npages, 0)) {
+ hmm_vma_walk->last = addr;
+ pmd_migration_entry_wait(walk->mm, pmdp);
+ return -EBUSY;
+ }
+ for (i = 0; start < end; start += PAGE_SIZE, i++)
+ hmm_pfns_start[i] &= HMM_PFN_INOUT_FLAGS;
+ }
+
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
}
- return hmm_pfns_fill(start, end, range, 0);
+
+ return 0;
}
- if (!pmd_present(pmd))
- return hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns,
- pmd);
+ if (pmd_trans_huge(pmd) || !pmd_present(pmd)) {
+ if (!pmd_present(pmd)) {
+ r = hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns_start,
+ pmd);
+ // If not migrating we are done
+ if (r || !minfo) {
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
+ return r;
+ }
+ }
- if (pmd_trans_huge(pmd)) {
- /*
- * No need to take pmd_lock here, even if some other thread
- * is splitting the huge pmd we will get that event through
- * mmu_notifier callback.
- *
- * So just read pmd value and check again it's a transparent
- * huge or device mapping one and compute corresponding pfn
- * values.
- */
- pmd = pmdp_get_lockless(pmdp);
- if (!pmd_trans_huge(pmd))
- goto again;
+ if (pmd_trans_huge(pmd)) {
+ /*
+ * No need to take pmd_lock here if not migrating,
+ * even if some other thread is splitting the huge
+ * pmd we will get that event through mmu_notifier callback.
+ *
+ * So just read pmd value and check again it's a transparent
+ * huge or device mapping one and compute corresponding pfn
+ * values.
+ */
+
+ if (!minfo) {
+ pmd = pmdp_get_lockless(pmdp);
+ if (!pmd_trans_huge(pmd))
+ goto again;
+ }
+
+ r = hmm_vma_handle_pmd(walk, start, end, hmm_pfns_start, pmd);
+
+ // If not migrating we are done
+ if (r || !minfo) {
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
+ return r;
+ }
+ }
+
+ r = hmm_vma_handle_migrate_prepare_pmd(walk, pmdp, start, end, hmm_pfns_start);
+
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
+ }
+
+ if (r == -ENOENT) {
+ r = hmm_vma_walk_split(pmdp, addr, walk);
+ if (r) {
+ /* Split not successful, skip */
+ return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
+ }
+
+ /* Split successful, reloop */
+ hmm_vma_walk->last = addr;
+ return -EBUSY;
+ }
+ return r;
+
+ }
- return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
+ if (hmm_vma_walk->pmdlocked) {
+ spin_unlock(hmm_vma_walk->ptl);
+ hmm_vma_walk->pmdlocked = false;
}
/*
@@ -449,24 +1202,62 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp,
* recover.
*/
if (pmd_bad(pmd)) {
- if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
+ if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns_start, npages, 0))
return -EFAULT;
- return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
+ return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
}
- ptep = pte_offset_map(pmdp, addr);
- if (!ptep)
+ if (minfo) {
+ ptep = pte_offset_map_lock(mm, pmdp, addr, &hmm_vma_walk->ptl);
+ if (ptep) {
+ lazy_mmu_mode_enable();
+ hmm_vma_walk->ptelocked = true;
+ } else {
+ /* The pte table is gone */
+ hmm_vma_handle_migrate_prepare_rollback(walk->private, addr, end, hmm_pfns);
+ }
+ } else {
+ ptep = pte_offset_map(pmdp, addr);
+ }
+ if (!ptep) {
+ addr = start;
goto again;
+ }
+
for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
- int r;
r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
if (r) {
- /* hmm_vma_handle_pte() did pte_unmap() */
+ /* hmm_vma_handle_pte() did pte_unmap() / pte_unmap_unlock */
return r;
}
+
+ r = hmm_vma_handle_migrate_prepare(walk, pmdp, ptep, addr, hmm_pfns, &unmapped);
+ if (r == -EAGAIN) {
+ HMM_ASSERT_UNLOCKED(hmm_vma_walk);
+ if (unmapped) {
+ flush_tlb_range(walk->vma, start, addr);
+ unmapped = false;
+ }
+ goto again;
+ }
+ if (r) {
+ hmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR);
+ break;
+ }
+ }
+
+ if (unmapped)
+ flush_tlb_range(walk->vma, start, addr);
+
+ if (hmm_vma_walk->ptelocked) {
+ lazy_mmu_mode_disable();
+ pte_unmap_unlock(ptep - 1, hmm_vma_walk->ptl);
+ hmm_vma_walk->ptelocked = false;
+ } else {
+ pte_unmap(ptep - 1);
}
- pte_unmap(ptep - 1);
+
return 0;
}
@@ -600,6 +1391,11 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,
struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct hmm_range *range = hmm_vma_walk->range;
struct vm_area_struct *vma = walk->vma;
+ int r;
+
+ r = hmm_vma_capture_migrate_range(start, end, walk);
+ if (r)
+ return r;
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
vma->vm_flags & VM_READ)
@@ -622,7 +1418,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end,
(end - start) >> PAGE_SHIFT, 0))
return -EFAULT;
- hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
+ hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR);
/* Skip this vma and continue processing the next vma. */
return 1;
@@ -652,9 +1448,37 @@ static const struct mm_walk_ops hmm_walk_ops = {
* the invalidation to finish.
* -EFAULT: A page was requested to be valid and could not be made valid
* ie it has no backing VMA or it is illegal to access
+ * -ERANGE: The range crosses multiple VMAs, or space for hmm_pfns array
+ * is too low.
*
* This is similar to get_user_pages(), except that it can read the page tables
* without mutating them (ie causing faults).
+ *
+ * If want to do migration after faulting, call hmm_range_fault() with
+ * range.default_flags of HMM_PFN_REQ_MIGRATE, and optionally
+ * HMM_PFN_REQ_FAULT|HMM_PFN_REQ_WRITE, and initialize range->migrate field.
+ * range->migrate->vma will be populated during the call,
+ * and must be stable across the whole migrate process, which is
+ * why mmap_lock must be held around this call.
+ *
+ * When HMM_PFN_REQ_MIGRATE is set, migration collection may be partial on
+ * return and the caller takes responsibility for completing or aborting it.
+ *
+ * On success, the caller must call migrate_hmm_range_setup() and may then
+ * proceed with the normal migrate_vma sequence: prepare destination pages,
+ * call migrate_vma_pages(), update device mappings as needed, and finally
+ * call migrate_vma_finalize().
+ *
+ * On -EBUSY, the caller may retry hmm_range_fault() using the same range and
+ * PFN array without undoing entries collected by the previous attempt. If
+ * the caller stops retrying, it must abort the partial migration.
+ *
+ * On any other error, or when abandoning a retry, the caller must call
+ * migrate_hmm_range_setup(), migrate_vma_pages() with no valid destination
+ * entries, and migrate_vma_finalize() to abort the partial migration.
+ *
+ * The mmap read lock must remain held until the migration has either been
+ * completed or aborted.
*/
int hmm_range_fault(struct hmm_range *range)
{
@@ -662,16 +1486,25 @@ int hmm_range_fault(struct hmm_range *range)
.range = range,
.last = range->start,
};
- struct mm_struct *mm = range->notifier->mm;
+ /*
+ * Could be serving a device fault or come from migrate
+ * entry point. For the former we have not resolved the vma
+ * yet, and the latter we don't have a notifier (but have a vma).
+ *
+ */
+ struct mm_struct *mm = hmm_range_fault_mm(range);
int ret;
mmap_assert_locked(mm);
do {
/* If range is no longer valid force retry. */
- if (mmu_interval_check_retry(range->notifier,
- range->notifier_seq))
- return -EBUSY;
+ if (range->notifier && mmu_interval_check_retry(range->notifier,
+ range->notifier_seq)) {
+ ret = -EBUSY;
+ break;
+ }
+
ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
&hmm_walk_ops, &hmm_vma_walk);
/*
@@ -681,6 +1514,9 @@ int hmm_range_fault(struct hmm_range *range)
* output, and all >= are still at their input values.
*/
} while (ret == -EBUSY);
+
+ hmm_vma_post_range_fault(&hmm_vma_walk);
+
return ret;
}
EXPORT_SYMBOL(hmm_range_fault);
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 908d2d4ec43ac..3d021b2e81f0c 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -18,509 +18,6 @@
#include <asm/tlbflush.h>
#include "internal.h"
-static int migrate_vma_collect_skip(unsigned long start,
- unsigned long end,
- struct mm_walk *walk)
-{
- struct migrate_vma *migrate = walk->private;
- unsigned long addr;
-
- for (addr = start; addr < end; addr += PAGE_SIZE) {
- migrate->dst[migrate->npages] = 0;
- migrate->src[migrate->npages++] = 0;
- }
-
- return 0;
-}
-
-static int migrate_vma_collect_hole(unsigned long start,
- unsigned long end,
- __always_unused int depth,
- struct mm_walk *walk)
-{
- struct migrate_vma *migrate = walk->private;
- unsigned long addr;
-
- /* Only allow populating anonymous memory. */
- if (!vma_is_anonymous(walk->vma))
- return migrate_vma_collect_skip(start, end, walk);
-
- if (thp_migration_supported() &&
- (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
- (IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
- IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
- migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE |
- MIGRATE_PFN_COMPOUND;
- migrate->dst[migrate->npages] = 0;
- migrate->npages++;
- migrate->cpages++;
-
- /*
- * Collect the remaining entries as holes, in case we
- * need to split later
- */
- return migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
- }
-
- for (addr = start; addr < end; addr += PAGE_SIZE) {
- migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
- migrate->dst[migrate->npages] = 0;
- migrate->npages++;
- migrate->cpages++;
- }
-
- return 0;
-}
-
-/**
- * migrate_vma_split_folio() - Helper function to split a THP folio
- * @folio: the folio to split
- * @fault_page: struct page associated with the fault if any
- *
- * Returns 0 on success
- */
-static int migrate_vma_split_folio(struct folio *folio,
- struct page *fault_page)
-{
- int ret;
- struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;
- struct folio *new_fault_folio = NULL;
-
- if (folio != fault_folio) {
- folio_get(folio);
- folio_lock(folio);
- }
-
- ret = split_folio(folio);
- if (ret) {
- if (folio != fault_folio) {
- folio_unlock(folio);
- folio_put(folio);
- }
- return ret;
- }
-
- new_fault_folio = fault_page ? page_folio(fault_page) : NULL;
-
- /*
- * Ensure the lock is held on the correct
- * folio after the split
- */
- if (!new_fault_folio) {
- folio_unlock(folio);
- folio_put(folio);
- } else if (folio != new_fault_folio) {
- if (new_fault_folio != fault_folio) {
- folio_get(new_fault_folio);
- folio_lock(new_fault_folio);
- }
- folio_unlock(folio);
- folio_put(folio);
- }
-
- return 0;
-}
-
-/** migrate_vma_collect_huge_pmd - collect THP pages without splitting the
- * folio for device private pages.
- * @pmdp: pointer to pmd entry
- * @start: start address of the range for migration
- * @end: end address of the range for migration
- * @walk: mm_walk callback structure
- * @fault_folio: folio associated with the fault if any
- *
- * Collect the huge pmd entry at @pmdp for migration and set the
- * MIGRATE_PFN_COMPOUND flag in the migrate src entry to indicate that
- * migration will occur at HPAGE_PMD granularity
- */
-static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
- unsigned long end, struct mm_walk *walk,
- struct folio *fault_folio)
-{
- struct mm_struct *mm = walk->mm;
- struct folio *folio;
- struct migrate_vma *migrate = walk->private;
- spinlock_t *ptl;
- int ret;
- unsigned long write = 0;
-
- ptl = pmd_lock(mm, pmdp);
- if (pmd_none(*pmdp)) {
- spin_unlock(ptl);
- return migrate_vma_collect_hole(start, end, -1, walk);
- }
-
- if (pmd_trans_huge(*pmdp)) {
- if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
- spin_unlock(ptl);
- return migrate_vma_collect_skip(start, end, walk);
- }
-
- folio = pmd_folio(*pmdp);
- if (is_huge_zero_folio(folio)) {
- spin_unlock(ptl);
- return migrate_vma_collect_hole(start, end, -1, walk);
- }
- if (pmd_write(*pmdp))
- write = MIGRATE_PFN_WRITE;
- } else if (!pmd_present(*pmdp)) {
- const softleaf_t entry = softleaf_from_pmd(*pmdp);
-
- folio = softleaf_to_folio(entry);
-
- if (!softleaf_is_device_private(entry) ||
- !(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
- (folio->pgmap->owner != migrate->pgmap_owner)) {
- spin_unlock(ptl);
- return migrate_vma_collect_skip(start, end, walk);
- }
-
- if (softleaf_is_device_private_write(entry))
- write = MIGRATE_PFN_WRITE;
- } else {
- spin_unlock(ptl);
- return -EAGAIN;
- }
-
- folio_get(folio);
- if (folio != fault_folio && unlikely(!folio_trylock(folio))) {
- spin_unlock(ptl);
- folio_put(folio);
- return migrate_vma_collect_skip(start, end, walk);
- }
-
- if (thp_migration_supported() &&
- (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
- (IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
- IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
-
- struct page_vma_mapped_walk pvmw = {
- .ptl = ptl,
- .address = start,
- .pmd = pmdp,
- .vma = walk->vma,
- };
-
- unsigned long pfn = page_to_pfn(folio_page(folio, 0));
-
- migrate->src[migrate->npages] = migrate_pfn(pfn) | write
- | MIGRATE_PFN_MIGRATE
- | MIGRATE_PFN_COMPOUND;
- migrate->dst[migrate->npages++] = 0;
- migrate->cpages++;
- ret = set_pmd_migration_entry(&pvmw, folio_page(folio, 0));
- if (ret) {
- migrate->npages--;
- migrate->cpages--;
- migrate->src[migrate->npages] = 0;
- migrate->dst[migrate->npages] = 0;
- goto fallback;
- }
- migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
- spin_unlock(ptl);
- return 0;
- }
-
-fallback:
- spin_unlock(ptl);
- if (!folio_test_large(folio))
- goto done;
- ret = split_folio(folio);
- if (fault_folio != folio)
- folio_unlock(folio);
- folio_put(folio);
- if (ret)
- return migrate_vma_collect_skip(start, end, walk);
- if (pmd_none(pmdp_get_lockless(pmdp)))
- return migrate_vma_collect_hole(start, end, -1, walk);
-
-done:
- return -ENOENT;
-}
-
-static int migrate_vma_collect_pmd(pmd_t *pmdp,
- unsigned long start,
- unsigned long end,
- struct mm_walk *walk)
-{
- struct migrate_vma *migrate = walk->private;
- struct vm_area_struct *vma = walk->vma;
- struct mm_struct *mm = vma->vm_mm;
- unsigned long addr = start, unmapped = 0;
- spinlock_t *ptl;
- struct folio *fault_folio = migrate->fault_page ?
- page_folio(migrate->fault_page) : NULL;
- pte_t *ptep;
-
-again:
- if (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) {
- int ret = migrate_vma_collect_huge_pmd(pmdp, start, end, walk, fault_folio);
-
- if (ret == -EAGAIN)
- goto again;
- if (ret == 0)
- return 0;
- }
-
- ptep = pte_offset_map_lock(mm, pmdp, start, &ptl);
- if (!ptep)
- goto again;
- lazy_mmu_mode_enable();
- ptep += (addr - start) / PAGE_SIZE;
-
- for (; addr < end; addr += PAGE_SIZE, ptep++) {
- struct dev_pagemap *pgmap;
- unsigned long mpfn = 0, pfn;
- struct folio *folio;
- struct page *page;
- softleaf_t entry;
- pte_t pte;
-
- pte = ptep_get(ptep);
-
- if (pte_none(pte)) {
- if (vma_is_anonymous(vma)) {
- mpfn = MIGRATE_PFN_MIGRATE;
- migrate->cpages++;
- }
- goto next;
- }
-
- if (!pte_present(pte)) {
- /*
- * Only care about unaddressable device page special
- * page table entry. Other special swap entries are not
- * migratable, and we ignore regular swapped page.
- */
- entry = softleaf_from_pte(pte);
- if (!softleaf_is_device_private(entry))
- goto next;
-
- page = softleaf_to_page(entry);
- pgmap = page_pgmap(page);
- if (!(migrate->flags &
- MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
- pgmap->owner != migrate->pgmap_owner)
- goto next;
-
- folio = page_folio(page);
- if (folio_test_large(folio)) {
- int ret;
-
- lazy_mmu_mode_disable();
- pte_unmap_unlock(ptep, ptl);
- ret = migrate_vma_split_folio(folio,
- migrate->fault_page);
-
- if (ret) {
- if (unmapped)
- flush_tlb_range(walk->vma, start, end);
-
- return migrate_vma_collect_skip(addr, end, walk);
- }
-
- goto again;
- }
-
- mpfn = migrate_pfn(page_to_pfn(page)) |
- MIGRATE_PFN_MIGRATE;
- if (softleaf_is_device_private_write(entry))
- mpfn |= MIGRATE_PFN_WRITE;
- } else {
- pfn = pte_pfn(pte);
- if (is_zero_pfn(pfn) &&
- (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
- mpfn = MIGRATE_PFN_MIGRATE;
- migrate->cpages++;
- goto next;
- }
- page = vm_normal_page(migrate->vma, addr, pte);
- if (page && !is_zone_device_page(page) &&
- !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
- goto next;
- } else if (page && is_device_coherent_page(page)) {
- pgmap = page_pgmap(page);
-
- if (!(migrate->flags &
- MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
- pgmap->owner != migrate->pgmap_owner)
- goto next;
- }
- folio = page ? page_folio(page) : NULL;
- if (folio && folio_test_large(folio)) {
- int ret;
-
- lazy_mmu_mode_disable();
- pte_unmap_unlock(ptep, ptl);
- ret = migrate_vma_split_folio(folio,
- migrate->fault_page);
-
- if (ret) {
- if (unmapped)
- flush_tlb_range(walk->vma, start, end);
-
- return migrate_vma_collect_skip(addr, end, walk);
- }
-
- goto again;
- }
- mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
- mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
- }
-
- if (!page || !page->mapping) {
- mpfn = 0;
- goto next;
- }
-
- /*
- * By getting a reference on the folio we pin it and that blocks
- * any kind of migration. Side effect is that it "freezes" the
- * pte.
- *
- * We drop this reference after isolating the folio from the lru
- * for non device folio (device folio are not on the lru and thus
- * can't be dropped from it).
- */
- folio = page_folio(page);
- folio_get(folio);
-
- /*
- * We rely on folio_trylock() to avoid deadlock between
- * concurrent migrations where each is waiting on the others
- * folio lock. If we can't immediately lock the folio we fail this
- * migration as it is only best effort anyway.
- *
- * If we can lock the folio it's safe to set up a migration entry
- * now. In the common case where the folio is mapped once in a
- * single process setting up the migration entry now is an
- * optimisation to avoid walking the rmap later with
- * try_to_migrate().
- */
- if (fault_folio == folio || folio_trylock(folio)) {
- bool anon_exclusive;
- pte_t swp_pte;
-
- if (pte_present(pte))
- flush_cache_page(vma, addr, pte_pfn(pte));
- anon_exclusive = folio_test_anon(folio) &&
- PageAnonExclusive(page);
- if (anon_exclusive) {
- pte = ptep_clear_flush(vma, addr, ptep);
-
- if (folio_try_share_anon_rmap_pte(folio, page)) {
- set_pte_at(mm, addr, ptep, pte);
- if (fault_folio != folio)
- folio_unlock(folio);
- folio_put(folio);
- mpfn = 0;
- goto next;
- }
- } else {
- pte = ptep_get_and_clear(mm, addr, ptep);
- }
-
- migrate->cpages++;
-
- /* Set the dirty flag on the folio now the pte is gone. */
- if (pte_present(pte) && pte_dirty(pte))
- folio_mark_dirty(folio);
-
- /* Setup special migration page table entry */
- if (mpfn & MIGRATE_PFN_WRITE)
- entry = make_writable_migration_entry(
- page_to_pfn(page));
- else if (anon_exclusive)
- entry = make_readable_exclusive_migration_entry(
- page_to_pfn(page));
- else
- entry = make_readable_migration_entry(
- page_to_pfn(page));
- if (pte_present(pte)) {
- if (pte_young(pte))
- entry = make_migration_entry_young(entry);
- if (pte_dirty(pte))
- entry = make_migration_entry_dirty(entry);
- }
- swp_pte = swp_entry_to_pte(entry);
- if (pte_present(pte)) {
- if (pte_soft_dirty(pte))
- swp_pte = pte_swp_mksoft_dirty(swp_pte);
- if (pte_uffd_wp(pte))
- swp_pte = pte_swp_mkuffd_wp(swp_pte);
- } else {
- if (pte_swp_soft_dirty(pte))
- swp_pte = pte_swp_mksoft_dirty(swp_pte);
- if (pte_swp_uffd_wp(pte))
- swp_pte = pte_swp_mkuffd_wp(swp_pte);
- }
- set_pte_at(mm, addr, ptep, swp_pte);
-
- /*
- * This is like regular unmap: we remove the rmap and
- * drop the folio refcount. The folio won't be freed, as
- * we took a reference just above.
- */
- folio_remove_rmap_pte(folio, page, vma);
- folio_put(folio);
-
- if (pte_present(pte))
- unmapped++;
- } else {
- folio_put(folio);
- mpfn = 0;
- }
-
-next:
- migrate->dst[migrate->npages] = 0;
- migrate->src[migrate->npages++] = mpfn;
- }
-
- /* Only flush the TLB if we actually modified any entries */
- if (unmapped)
- flush_tlb_range(walk->vma, start, end);
-
- lazy_mmu_mode_disable();
- pte_unmap_unlock(ptep - 1, ptl);
-
- return 0;
-}
-
-static const struct mm_walk_ops migrate_vma_walk_ops = {
- .pmd_entry = migrate_vma_collect_pmd,
- .pte_hole = migrate_vma_collect_hole,
- .walk_lock = PGWALK_RDLOCK,
-};
-
-/*
- * migrate_vma_collect() - collect pages over a range of virtual addresses
- * @migrate: migrate struct containing all migration information
- *
- * This will walk the CPU page table. For each virtual address backed by a
- * valid page, it updates the src array and takes a reference on the page, in
- * order to pin the page until we lock it and unmap it.
- */
-static void migrate_vma_collect(struct migrate_vma *migrate)
-{
- struct mmu_notifier_range range;
-
- /*
- * Note that the pgmap_owner is passed to the mmu notifier callback so
- * that the registered device driver can skip invalidating device
- * private page mappings that won't be migrated.
- */
- mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
- migrate->vma->vm_mm, migrate->start, migrate->end,
- migrate->pgmap_owner);
- mmu_notifier_invalidate_range_start(&range);
-
- walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
- &migrate_vma_walk_ops, migrate);
-
- mmu_notifier_invalidate_range_end(&range);
- migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
-}
-
/*
* migrate_vma_check_page() - check if page is pinned or not
* @page: struct page to check
@@ -729,10 +226,20 @@ static void migrate_vma_unmap(struct migrate_vma *migrate)
*/
int migrate_vma_setup(struct migrate_vma *args)
{
+ int ret;
long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
+ struct hmm_range range = {
+ .notifier = NULL,
+ .hmm_pfns = args->src,
+ .dev_private_owner = args->pgmap_owner,
+ .migrate = args,
+ .default_flags = HMM_PFN_REQ_MIGRATE
+ };
args->start &= PAGE_MASK;
args->end &= PAGE_MASK;
+ range.start = args->start;
+ range.end = args->end;
if (!args->vma || is_vm_hugetlb_page(args->vma) ||
(args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
return -EINVAL;
@@ -754,10 +261,24 @@ int migrate_vma_setup(struct migrate_vma *args)
args->cpages = 0;
args->npages = 0;
- migrate_vma_collect(args);
+ if (args->flags & MIGRATE_VMA_FAULT)
+ range.default_flags |= HMM_PFN_REQ_FAULT;
- if (args->cpages)
- migrate_vma_unmap(args);
+ if (args->flags & MIGRATE_VMA_WRITE)
+ range.default_flags |= HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE;
+
+ ret = hmm_range_fault(&range);
+
+ migrate_hmm_range_setup(&range);
+
+ /* Remove migration PTEs */
+ if (ret) {
+ migrate_vma_pages(args);
+ migrate_vma_finalize(args);
+ memset(args->src, 0, sizeof(*args->src) * nr_pages);
+ args->cpages = 0;
+ args->npages = 0;
+ }
/*
* At this point pages are locked and unmapped, and thus they have
@@ -1487,3 +1008,58 @@ int migrate_device_coherent_folio(struct folio *folio)
return 0;
return -EBUSY;
}
+
+/**
+ * migrate_hmm_range_setup() - prepare to migrate a range of memory
+ * @range: contains pointer to struct migrate_vma to be set up.
+ *
+ * When collecting has been done with hmm_range_fault(), this
+ * should be called next, and completes range->migrate by
+ * populating migrate->src[] and migrate->dst[]
+ * using range->hmm_pfns[].
+ * Also, migrate->cpages and migrate->npages get initialized.
+ * After migrate_hmm_range_setup(), range->migrate is good
+ * for the rest of the migrate_vma_* flow.
+ */
+void migrate_hmm_range_setup(struct hmm_range *range)
+{
+ struct migrate_vma *migrate = range->migrate;
+
+ if (!migrate)
+ return;
+
+ migrate->npages = (migrate->end - migrate->start) >> PAGE_SHIFT;
+ migrate->cpages = 0;
+
+ for (unsigned long i = 0; i < migrate->npages; i++) {
+ unsigned long pfn = range->hmm_pfns[i];
+
+ /*
+ * We are only interested in entries to be
+ * migrated.
+ */
+ if (!(pfn & HMM_PFN_MIGRATE)) {
+ migrate->src[i] = 0;
+ migrate->dst[i] = 0;
+ continue;
+ }
+
+ migrate->cpages++;
+
+ /* HMM_PFN_MIGRATE without HMM_PFN_VALID denotes the special zero page */
+ if (pfn & HMM_PFN_VALID)
+ migrate->src[i] = migrate_pfn(page_to_pfn(hmm_pfn_to_page(pfn)));
+ else
+ migrate->src[i] = 0;
+
+ migrate->src[i] |= MIGRATE_PFN_MIGRATE;
+ migrate->src[i] |= (pfn & HMM_PFN_WRITE) ? MIGRATE_PFN_WRITE : 0;
+ migrate->src[i] |= (pfn & HMM_PFN_COMPOUND) ? MIGRATE_PFN_COMPOUND : 0;
+ migrate->dst[i] = 0;
+ }
+
+ if (migrate->cpages)
+ migrate_vma_unmap(migrate);
+
+}
+EXPORT_SYMBOL(migrate_hmm_range_setup);
diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
index 2f2b9879d1005..4ac17cf6f8663 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -283,6 +283,13 @@ static int hmm_migrate_sys_to_dev(int fd,
return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_DEV, buffer, npages);
}
+static int hmm_migrate_on_fault_sys_to_dev(int fd,
+ struct hmm_buffer *buffer,
+ unsigned long npages)
+{
+ return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV, buffer, npages);
+}
+
static int hmm_migrate_dev_to_sys(int fd,
struct hmm_buffer *buffer,
unsigned long npages)
@@ -990,6 +997,53 @@ TEST_F(hmm, migrate)
hmm_buffer_free(buffer);
}
+
+/*
+ * Fault and migrate anonymous memory to device private memory.
+ */
+TEST_F(hmm, migrate_on_fault)
+{
+ struct hmm_buffer *buffer;
+ unsigned long npages;
+ unsigned long size;
+ unsigned long i;
+ int *ptr;
+ int ret;
+
+ npages = ALIGN(HMM_BUFFER_SIZE, self->page_size) >> self->page_shift;
+ ASSERT_NE(npages, 0);
+ size = npages << self->page_shift;
+
+ buffer = malloc(sizeof(*buffer));
+ ASSERT_NE(buffer, NULL);
+
+ buffer->fd = -1;
+ buffer->size = size;
+ buffer->mirror = malloc(size);
+ ASSERT_NE(buffer->mirror, NULL);
+
+ buffer->ptr = mmap(NULL, size,
+ PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS,
+ buffer->fd, 0);
+ ASSERT_NE(buffer->ptr, MAP_FAILED);
+
+ /* Initialize buffer in system memory. */
+ for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i)
+ ptr[i] = i;
+
+ /* Fault and migrate memory to device. */
+ ret = hmm_migrate_on_fault_sys_to_dev(self->fd, buffer, npages);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(buffer->cpages, npages);
+
+ /* Check what the device read. */
+ for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i)
+ ASSERT_EQ(ptr[i], i);
+
+ hmm_buffer_free(buffer);
+}
+
/*
* Migrate private file memory to device private memory.
*/
|
| 3/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 28553 output: 21 thoughts: 535 Thoughts: **My Assessment of the HMM Migration Patch** Okay, so we have a significant patch here, introducing a new feature: migrating memory using HMM during a page fault. This is a core memory management change, touching `mm/hmm.c`, `mm/migrate_device.c`, and relevant headers, along with some testing components in `lib/test_hmm.c` and `tools/testing/selftests/mm/hmm-tests.c`. The patch introduces new flags, `HMM_PFN_MIGRATE`, `HMM_PFN_COMPOUND`, and `HMM_PFN_REQ_MIGRATE`, indicating a deeper integration with the HMM framework. The core of the change appears to center around modifying `hmm_range_fault()` to handle the migration itself, and it adds `migrate_hmm_range_setup()` and integrates `hmm_range_fault()` into `migrate_vma_setup()`. The addition of `HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV` suggests a focus on migration to a device upon a fault. Given the scope of the changes and the integration with core memory management code, this patch is a prime candidate for thorough fuzzing. My immediate focus is going to be on identifying the most critical functions to target. Looking at the specific functions added or modified, here's my list of high-priority targets: `hmm_range_fault`, `migrate_vma_setup`, `migrate_hmm_range_setup`, `hmm_vma_handle_migrate_prepare`, `hmm_vma_handle_migrate_prepare_pmd`, `hmm_vma_handle_migrate_prepare_rollback`, `hmm_vma_walk_split`, `hmm_vma_capture_migrate_range`, `hmm_vma_post_range_fault`, and `migrate_vma_split_folio`. Now, to refine my fuzzing strategy, I need to understand which of these functions are actually accessible from outside their respective modules. A quick scan of `mm/hmm.c` and `mm/migrate_device.c` reveals that `hmm_range_fault`, `migrate_vma_setup`, and `migrate_hmm_range_setup` are all exported, making them accessible from the outside which will ease their fuzzability. Finally, I should also verify the relevant Kconfig options are enabled to include: `CONFIG_MIGRATION`, `CONFIG_DEVICE_MIGRATION`, `CONFIG_HMM_MIRROR`, and `CONFIG_TEST_HMM`. Now, let's use `default_api:grepper` to locate the definition and export of `migrate_hmm_range_setup` so I can fully understand how it is used. |
| 4/2 | 2026/08/04 04:55 | tool | grepper |
0mArgs:{
"Expression": "migrate_hmm_range_setup"
}Results: {
"Output": "include/linux/hmm.h=16=struct migrate_vma;\n--\ninclude/linux/hmm.h-35- * This flag together with HMM_PFN_COMPOUND are\ninclude/linux/hmm.h:36: * indicators for migrate_hmm_range_setup() to\ninclude/linux/hmm.h-37- * setup the migrate pfns.\n--\ninclude/linux/migrate.h=251=void migrate_device_finalize(unsigned long *src_pfns,\ninclude/linux/migrate.h-252-\t\t\tunsigned long *dst_pfns, unsigned long npages);\ninclude/linux/migrate.h:253:void migrate_hmm_range_setup(struct hmm_range *range);\ninclude/linux/migrate.h-254-#endif /* CONFIG_MIGRATION */\n--\nlib/test_hmm.c=1294=static int do_fault_and_migrate(struct dmirror *dmirror, struct hmm_range *range)\n--\nlib/test_hmm.c-1306-\t */\nlib/test_hmm.c:1307:\tmigrate_hmm_range_setup(range);\nlib/test_hmm.c-1308-\tif (ret) {\n--\nmm/hmm.c=1427=static const struct mm_walk_ops hmm_walk_ops = {\n--\nmm/hmm.c-1466- *\nmm/hmm.c:1467: * On success, the caller must call migrate_hmm_range_setup() and may then\nmm/hmm.c-1468- * proceed with the normal migrate_vma sequence: prepare destination pages,\n--\nmm/hmm.c-1476- * On any other error, or when abandoning a retry, the caller must call\nmm/hmm.c:1477: * migrate_hmm_range_setup(), migrate_vma_pages() with no valid destination\nmm/hmm.c-1478- * entries, and migrate_vma_finalize() to abort the partial migration.\n--\nmm/migrate_device.c=227=int migrate_vma_setup(struct migrate_vma *args)\n--\nmm/migrate_device.c-271-\nmm/migrate_device.c:272:\tmigrate_hmm_range_setup(\u0026range);\nmm/migrate_device.c-273-\n--\nmm/migrate_device.c=977=int migrate_device_coherent_folio(struct folio *folio)\n--\nmm/migrate_device.c-1012-/**\nmm/migrate_device.c:1013: * migrate_hmm_range_setup() - prepare to migrate a range of memory\nmm/migrate_device.c-1014- * @range: contains pointer to struct migrate_vma to be set up.\n--\nmm/migrate_device.c-1020- * Also, migrate-\u003ecpages and migrate-\u003enpages get initialized.\nmm/migrate_device.c:1021: * After migrate_hmm_range_setup(), range-\u003emigrate is good\nmm/migrate_device.c-1022- * for the rest of the migrate_vma_* flow.\nmm/migrate_device.c-1023- */\nmm/migrate_device.c:1024:void migrate_hmm_range_setup(struct hmm_range *range)\nmm/migrate_device.c-1025-{\n--\nmm/migrate_device.c-1064-}\nmm/migrate_device.c:1065:EXPORT_SYMBOL(migrate_hmm_range_setup);\n"
} |
| 5/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 30595 output: 20 thoughts: 53 Thoughts: Okay, here's my interpretation of those thoughts, considering my expertise: **Checking the Migration Mechanics** Right, so we're looking at some core changes concerning Heterogeneous Memory Management (HMM) and migration. The key modifications are hitting three key areas: `hmm_range_fault`, `migrate_vma_setup`, and the addition of `migrate_hmm_range_setup`. That immediately flags a potential shift in how we handle page faults, virtual memory area (VMA) setup, and importantly, the specific setup for migrating HMM ranges. This means we're likely dealing with the mechanics of moving memory between different memory domains or devices, possibly with a focus on HMM. Before I dig deeper into the code itself, I need to understand the configuration context. Specifically, I need to verify how `HMM_MIRROR` and `DEVICE_MIGRATION` are configured. Are we mirroring memory? If so, what is the strategy? Is the device migration component enabled? The answers to these questions are essential. They'll tell me if this patch is focusing on a specific migration use case, such as GPU memory to host memory, or something more general, or specific HMM features. Depending on those settings, I can infer whether this is a full migration implementation or part of a bigger scheme. I need to establish the system's baseline configuration so I can understand the context of the migration process. |
| 6/2 | 2026/08/04 04:55 | tool | grepper |
0mArgs:{
"Expression": "config DEVICE_MIGRATION"
}Results: {
"Output": "mm/Kconfig=696=config MIGRATION\n--\nmm/Kconfig-699-\nmm/Kconfig:700:config DEVICE_MIGRATION\nmm/Kconfig-701-\tdef_bool MIGRATION \u0026\u0026 ZONE_DEVICE\n"
} |
| 7/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 31039 output: 19 thoughts: 13 Thoughts: Okay, here's my interpretation of those thoughts, crafted with the understanding that I'm speaking to a fellow expert: **Investigating the Kconfig for `TEST_HMM`** Alright, so the immediate thought is to delve into the Kconfig files. The context isn't fully clear from this snippet, but I'm assuming we're dealing with a kernel configuration, or something similarly structured using the Kconfig system. My internal thought process goes directly to checking the relevant configuration options. The goal is to see how `TEST_HMM` is defined: its dependencies, default value, if it's tristate or boolean, and of course, where it's located within the configuration hierarchy. Understanding its placement is vital for troubleshooting or modifying its behavior. I'm practically itching to get my hands dirty with the `Kconfig` files and start parsing the dependencies and locations. |
| 8/2 | 2026/08/04 04:55 | tool | grepper |
0mArgs:{
"Expression": "config TEST_HMM"
}Results: {
"Output": "lib/Kconfig.debug=3346=config TEST_MEMINIT\n--\nlib/Kconfig.debug-3353-\nlib/Kconfig.debug:3354:config TEST_HMM\nlib/Kconfig.debug-3355-\ttristate \"Test HMM (Heterogeneous Memory Management)\"\n"
} |
| 9/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 31347 output: 18 thoughts: 84 Thoughts: **Configuration Dependencies and Enabling Key Features** Okay, so I need to enable a series of configurations: `MIGRATION`, `ZONE_DEVICE`, `DEVICE_MIGRATION`, `HMM_MIRROR`, and `TEST_HMM`. Now, immediately I see a dependency: `DEVICE_MIGRATION` is defined as a boolean that requires both `MIGRATION` and `ZONE_DEVICE` to be active. So, I have to ensure those two are enabled first. Given that `DEVICE_MIGRATION` is dependent on the other two, I'll need to double-check `ZONE_DEVICE`'s dependencies to make sure I'm not missing anything crucial there. It's best to take a moment to understand each one, step-by-step. |
| 10/2 | 2026/08/04 04:55 | tool | grepper |
0mArgs:{
"Expression": "config ZONE_DEVICE"
}Results: {
"Output": "mm/Kconfig=1232=config ZONE_DMA32\n--\nmm/Kconfig-1236-\nmm/Kconfig:1237:config ZONE_DEVICE\nmm/Kconfig-1238-\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n"
} |
| 11/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 31701 output: 33 thoughts: 17 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into the `mm/Kconfig` Dependencies** Right, let's get down to it. My first instinct here is to delve straight into `mm/Kconfig`. We need to scrutinize the dependencies surrounding `ZONE_DEVICE`. This isn't just a casual glance; we need a thorough examination of *every* possible dependency that might influence the inclusion or behavior of `ZONE_DEVICE`. I'm thinking about the implications for memory management – are there related configurations, perhaps around NUMA, memory tiers, or even specific hardware features that trigger or restrict `ZONE_DEVICE`? We can't afford to overlook any potential conflicts or interactions. I'm visualizing the `Kconfig` structure now, tracing the conditional logic. Time to get my hands dirty. This is where the real work begins. |
| 12/2 | 2026/08/04 04:55 | tool | read-file |
0mArgs:{
"File": "mm/Kconfig",
"FirstLine": 1237,
"LineCount": 20
}Results: {
"Contents": "1237:\tconfig ZONE_DEVICE\n1238:\t\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\n1239:\t\tdepends on MEMORY_HOTPLUG\n1240:\t\tdepends on MEMORY_HOTREMOVE\n1241:\t\tdepends on SPARSEMEM_VMEMMAP\n1242:\t\tselect XARRAY_MULTI\n1243:\t\n1244:\t\thelp\n1245:\t\t Device memory hotplug support allows for establishing pmem,\n1246:\t\t or other device driver discovered memory regions, in the\n1247:\t\t memmap. This allows pfn_to_page() lookups of otherwise\n1248:\t\t \"device-physical\" addresses which is needed for DAX, PCI_P2PDMA, and\n1249:\t\t DEVICE_PRIVATE features among others.\n1250:\t\n1251:\t\t Enabling this option will reduce the entropy of x86 KASLR memory\n1252:\t\t regions. For example - on a 46 bit system, the entropy goes down\n1253:\t\t from 16 bits to 15 bits. The actual reduction in entropy depends\n1254:\t\t on the physical address bits, on processor features, kernel config\n1255:\t\t (5 level page table) and physical memory present on the system.\n1256:\t\n"
} |
| 13/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 32294 output: 35 thoughts: 39 Thoughts: Okay, here's my take on those considerations, from my perspective as someone well-versed in memory management: **Memory Hotplug, Hotremove, and Sparse Memory Considerations** Alright, looking at the requirements, the first thing that jumps out is that we absolutely need to ensure `MEMORY_HOTPLUG`, `MEMORY_HOTREMOVE`, and `SPARSEMEM_VMEMMAP` are enabled. These are foundational for handling dynamic memory adjustments and managing large, potentially non-contiguous physical memory spaces. Without them, we're severely handicapped. Before we go too deep, let me just quickly run through the dependencies for `TEST_HMM`, I think there might be some overlap there we should note to avoid reinventing any wheels. I'd like to check if they've already got some code we could leverage. This could be useful to save some time, and ensure that the code is more robust. |
| 14/2 | 2026/08/04 04:55 | tool | read-file |
0mArgs:{
"File": "lib/Kconfig.debug",
"FirstLine": 3354,
"LineCount": 20
}Results: {
"Contents": "3354:\tconfig TEST_HMM\n3355:\t\ttristate \"Test HMM (Heterogeneous Memory Management)\"\n3356:\t\tdepends on TRANSPARENT_HUGEPAGE\n3357:\t\tdepends on DEVICE_PRIVATE\n3358:\t\tselect HMM_MIRROR\n3359:\t\tselect MMU_NOTIFIER\n3360:\t\thelp\n3361:\t\t This is a pseudo device driver solely for testing HMM.\n3362:\t\t Say M here if you want to build the HMM test module.\n3363:\t\t Doing so will allow you to run tools/testing/selftest/vm/hmm-tests.\n3364:\t\n3365:\t\t If unsure, say N.\n3366:\t\n3367:\tconfig TEST_FREE_PAGES\n3368:\t\ttristate \"Test freeing pages\"\n3369:\t\thelp\n3370:\t\t Test that a memory leak does not occur due to a race between\n3371:\t\t freeing a block of pages and a speculative page reference.\n3372:\t\t Loading this module is safe if your kernel has the bug fixed.\n3373:\t\t If the bug is not fixed, it will leak gigabytes of memory and\n"
} |
| 15/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 32886 output: 18 thoughts: 35 Thoughts: Okay, here's my take on those thoughts, framed as an expert's inner monologue: **Dependency Deep Dive: TEST_HMM and DEVICE_PRIVATE** Alright, so `TEST_HMM` has a couple of dependencies, namely `TRANSPARENT_HUGEPAGE` and `DEVICE_PRIVATE`. Given my experience, the connection with `TRANSPARENT_HUGEPAGE` makes sense; performance often hinges on memory management and that's a common pairing. But `DEVICE_PRIVATE`... that's the more intriguing one in this case. Let's dig into that bit first, since it's less obvious why that is a dependency. I need to understand what role `DEVICE_PRIVATE` plays here and why its presence or absence would have a direct impact on the execution or testability of `TEST_HMM`. My immediate instinct is to check the relevant documentation or the code directly to confirm. This'll require me to investigate the details of `DEVICE_PRIVATE`. |
| 16/2 | 2026/08/04 04:55 | tool | grepper |
0mArgs:{
"Expression": "config DEVICE_PRIVATE"
}Results: {
"Output": "mm/Kconfig=1266=config GET_FREE_REGION\n--\nmm/Kconfig-1268-\nmm/Kconfig:1269:config DEVICE_PRIVATE\nmm/Kconfig-1270-\tbool \"Unaddressable device memory (GPU memory, ...)\"\n"
} |
| 17/2 | 2026/08/04 04:55 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 33231 output: 33 thoughts: 11 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing `DEVICE_PRIVATE` Dependencies** Alright, time to dive in. My initial thought process is straightforward: I need to get a clear picture of the dependencies within the `DEVICE_PRIVATE` space. It's a standard practice for me; understanding the interconnectedness is paramount for any kind of impact analysis, refactoring, or security audit. The level of detail I'll require depends on the context, of course – is this for performance optimization, a vulnerability assessment, or simply a code review? But the fundamental question remains: "What relies on this, and what does *it* rely on?" I’ll need to trace the web of connections meticulously. This likely involves diving into build configurations, header files, and potentially even runtime configurations, depending on the architecture. It's time to build a mental, and potentially physical, map. |
| 18/2 | 2026/08/04 04:56 | tool | read-file |
0mArgs:{
"File": "mm/Kconfig",
"FirstLine": 1269,
"LineCount": 10
}Results: {
"Contents": "1269:\tconfig DEVICE_PRIVATE\n1270:\t\tbool \"Unaddressable device memory (GPU memory, ...)\"\n1271:\t\tdepends on ZONE_DEVICE\n1272:\t\tselect GET_FREE_REGION\n1273:\t\n1274:\t\thelp\n1275:\t\t Allows creation of struct pages to represent unaddressable device\n1276:\t\t memory; i.e., memory that is only accessible from the device (or\n1277:\t\t group of devices). You likely also want to select HMM_MIRROR.\n1278:\t\n"
} |
| 19/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 33626 output: 26 thoughts: 453 Thoughts: **Fuzzing Focus: HMM and Migration** Okay, so I'm thinking we need a specific set of kernel configurations enabled for this fuzzing effort to be effective. Let's see, we'll definitely need `TRANSPARENT_HUGEPAGE`, `DEVICE_PRIVATE`, `ZONE_DEVICE`, `MEMORY_HOTPLUG`, and `MEMORY_HOTREMOVE`. `SPARSEMEM_VMEMMAP` is a given, and, of course, `TEST_HMM`. Now, let's concretely list those configurations to make sure we don't miss anything: * `TEST_HMM` * `TRANSPARENT_HUGEPAGE` * `DEVICE_PRIVATE` * `ZONE_DEVICE` * `MEMORY_HOTPLUG` * `MEMORY_HOTREMOVE` * `SPARSEMEM_VMEMMAP` * `MIGRATION` * `DEVICE_MIGRATION` * `HMM_MIRROR` Alright, with the configs in place, I need to identify the key functions to focus the fuzzing on. Based on the HMM and migration context, I'm thinking the primary entry points we want to hit are: `hmm_range_fault`, `migrate_vma_setup`, and `migrate_hmm_range_setup`. I've also checked and confirmed that several related helper functions like `hmm_vma_handle_migrate_prepare`, `hmm_vma_handle_migrate_prepare_pmd`, `hmm_vma_handle_migrate_prepare_rollback`, `hmm_vma_walk_split`, `hmm_vma_capture_migrate_range`, `hmm_vma_post_range_fault`, and `migrate_vma_split_folio` are all defined as static which means they're likely not exposed externally and won't be entry points for fuzzing. Going deeper, I need to consider ioctls in `test_hmm.c`. I recall the `dmirror_fops_unlocked_ioctl` function handles the `HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV` request. The important function is `dmirror_fault_and_migrate_to_device` itself. So, I should definitely add this function to my list of focus symbols. Now, let's take a closer look at `dmirror_fault_and_migrate_to_device` in `lib/test_hmm.c` to see how it fits in to the overall picture. |
| 20/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "dmirror_fault_and_migrate_to_device"
}Results: {
"Output": "lib/test_hmm.c=1294=static int do_fault_and_migrate(struct dmirror *dmirror, struct hmm_range *range)\n--\nlib/test_hmm.c-1329-\nlib/test_hmm.c:1330:static int dmirror_fault_and_migrate_to_device(struct dmirror *dmirror,\nlib/test_hmm.c-1331-\t\t\t\t\t struct hmm_dmirror_cmd *cmd)\n--\nlib/test_hmm.c=1631=static long dmirror_fops_unlocked_ioctl(struct file *filp,\n--\nlib/test_hmm.c-1668-\tcase HMM_DMIRROR_MIGRATE_ON_FAULT_TO_DEV:\nlib/test_hmm.c:1669:\t\tret = dmirror_fault_and_migrate_to_device(dmirror, \u0026cmd);\nlib/test_hmm.c-1670-\t\tbreak;\n"
} |
| 21/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 34920 output: 24 thoughts: 92 Thoughts: **Considering Migration Strategy** Okay, so `dmirror_fault_and_migrate_to_device` is static, which isn't ideal, but it's a backup plan. I'd really prefer something non-static if possible. Wait a minute... I think I see some better candidates here. `hmm_range_fault`, `migrate_vma_setup`, and `migrate_hmm_range_setup` - all exported and non-static! Excellent. Now, I'll quickly need to verify that `migrate_vma_setup` is indeed exported and suitable for this use. I'll get that checked. |
| 22/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "EXPORT_SYMBOL.*migrate_vma_setup"
}Results: {
"Output": "mm/migrate_device.c=227=int migrate_vma_setup(struct migrate_vma *args)\n--\nmm/migrate_device.c-290-}\nmm/migrate_device.c:291:EXPORT_SYMBOL(migrate_vma_setup);\nmm/migrate_device.c-292-\n"
} |
| 23/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 35264 output: 23 thoughts: 21 Thoughts: Okay, here's my summary, taking the perspective of someone knowledgeable in the field: **Checking for Exported Functions** Right, that's definitely the case. Now, let's just confirm something crucial. I need to quickly verify that the function, specifically `hmm_range_fault`, is actually exported. This is a common point of oversight, and I need to be sure the function is accessible before I move on. This is a critical prerequisite for whatever I'm doing next. I can't proceed without confirming it. |
| 24/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "EXPORT_SYMBOL.*hmm_range_fault"
}Results: {
"Output": "mm/hmm.c=1483=int hmm_range_fault(struct hmm_range *range)\n--\nmm/hmm.c-1521-}\nmm/hmm.c:1522:EXPORT_SYMBOL(hmm_range_fault);\nmm/hmm.c-1523-\n"
} |
| 25/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 35508 output: 20 thoughts: 136 Thoughts: **My Assessment of the HMM Migration Strategy** Right, that's correct, the direction I was leaning towards is the proper one. So, when we're talking about focus symbols for HMM, we are going to focus on: `hmm_range_fault`, `migrate_vma_setup`, and `migrate_hmm_range_setup`. That feels right in the current context. Now, let's nail down the relevant configs. We'll be working with `TEST_HMM`, `TRANSPARENT_HUGEPAGE`, `DEVICE_PRIVATE`, `ZONE_DEVICE`, `MEMORY_HOTPLUG`, `MEMORY_HOTREMOVE`, `SPARSEMEM_VMEMMAP`, `MIGRATION`, `DEVICE_MIGRATION`, and `HMM_MIRROR`. Just to make absolutely sure, I'll quickly verify that `MEMORY_HOTREMOVE` is actually present in this build environment. Always good to double-check these assumptions before making any big moves. |
| 26/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "config MEMORY_HOTREMOVE"
}Results: {
"Output": "mm/Kconfig=589=endchoice\nmm/Kconfig-590-\nmm/Kconfig:591:config MEMORY_HOTREMOVE\nmm/Kconfig-592-\tbool \"Allow for memory hot remove\"\n"
} |
| 27/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 35946 output: 22 thoughts: 24 Thoughts: Okay, here's my interpretation of those thoughts, tailored for an expert audience, presented in the first person: **Confirming and Validating a Key Configuration** Right, that seems to be the case. I'm satisfied with the initial assessment. Now, let's just make absolutely sure, for the sake of thoroughness and to avoid any potential assumptions down the line, that we've got the `SPARSEMEM_VMEMMAP` configuration option defined and enabled. It's crucial for the memory management scheme we're employing, so a quick check is warranted before proceeding with further analysis. We should confirm that. |
| 28/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "config SPARSEMEM_VMEMMAP"
}Results: {
"Output": "mm/Kconfig=451=config SPARSEMEM_EXTREME\n--\nmm/Kconfig-454-\nmm/Kconfig:455:config SPARSEMEM_VMEMMAP_ENABLE\nmm/Kconfig-456-\tbool\nmm/Kconfig-457-\nmm/Kconfig:458:config SPARSEMEM_VMEMMAP\nmm/Kconfig-459-\tdef_bool y\n--\nmm/Kconfig-465-\nmm/Kconfig:466:config SPARSEMEM_VMEMMAP_PREINIT\nmm/Kconfig-467-\tbool\n"
} |
| 29/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 36286 output: 22 thoughts: 56 Thoughts: Okay, here's my take on those thoughts, framed as my own mental processing: **Vmemmap and Transparent Hugepages - A Quick Assessment** Alright, so I'm seeing this `def_bool y` conditional on `SPARSEMEM_VMEMMAP_ENABLE`. Basically, if that's enabled, then we've got the `def_bool y` for the functionality. Seems straightforward enough – if `SPARSEMEM_VMEMMAP_ENABLE` is set, the thing is enabled. So, the implication is that we can just include `SPARSEMEM_VMEMMAP` and be done with it. That's good, minimizes complexity. No need to overthink that part. Now, let's just make a quick check on `TRANSPARENT_HUGEPAGE`. I want to make sure the interactions between these features is something I'm aware of. Always good to refresh the mental model there. Make sure there are no obvious conflicts or unexpected behavior changes that need to be addressed. |
| 30/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "config TRANSPARENT_HUGEPAGE"
}Results: {
"Output": "mm/Kconfig=844=config MM_ID\n--\nmm/Kconfig-846-\nmm/Kconfig:847:menuconfig TRANSPARENT_HUGEPAGE\nmm/Kconfig-848-\tbool \"Transparent Hugepage Support\"\n--\nmm/Kconfig=865=choice\n--\nmm/Kconfig-870-\nmm/Kconfig:871:\tconfig TRANSPARENT_HUGEPAGE_ALWAYS\nmm/Kconfig-872-\t\tbool \"always\"\n--\nmm/Kconfig-877-\nmm/Kconfig:878:\tconfig TRANSPARENT_HUGEPAGE_MADVISE\nmm/Kconfig-879-\t\tbool \"madvise\"\n--\nmm/Kconfig-886-\nmm/Kconfig:887:\tconfig TRANSPARENT_HUGEPAGE_NEVER\nmm/Kconfig-888-\t\tbool \"never\"\n--\nmm/Kconfig=894=choice\n--\nmm/Kconfig-903-\nmm/Kconfig:904:\tconfig TRANSPARENT_HUGEPAGE_SHMEM_HUGE_NEVER\nmm/Kconfig-905-\t\tbool \"never\"\n--\nmm/Kconfig-913-\nmm/Kconfig:914:\tconfig TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ALWAYS\nmm/Kconfig-915-\t\tbool \"always\"\n--\nmm/Kconfig-921-\nmm/Kconfig:922:\tconfig TRANSPARENT_HUGEPAGE_SHMEM_HUGE_WITHIN_SIZE\nmm/Kconfig-923-\t\tbool \"within_size\"\n--\nmm/Kconfig-929-\nmm/Kconfig:930:\tconfig TRANSPARENT_HUGEPAGE_SHMEM_HUGE_ADVISE\nmm/Kconfig-931-\t\tbool \"advise\"\n--\nmm/Kconfig=939=choice\n--\nmm/Kconfig-948-\nmm/Kconfig:949:\tconfig TRANSPARENT_HUGEPAGE_TMPFS_HUGE_NEVER\nmm/Kconfig-950-\t\tbool \"never\"\n--\nmm/Kconfig-957-\nmm/Kconfig:958:\tconfig TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ALWAYS\nmm/Kconfig-959-\t\tbool \"always\"\n--\nmm/Kconfig-965-\nmm/Kconfig:966:\tconfig TRANSPARENT_HUGEPAGE_TMPFS_HUGE_WITHIN_SIZE\nmm/Kconfig-967-\t\tbool \"within_size\"\n--\nmm/Kconfig-973-\nmm/Kconfig:974:\tconfig TRANSPARENT_HUGEPAGE_TMPFS_HUGE_ADVISE\nmm/Kconfig-975-\t\tbool \"advise\"\n"
} |
| 31/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37296 output: 18 thoughts: 24 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Kernel Memory Management Deep Dive** Right, so we're looking at something specific here. My mind immediately flags the existence of `TRANSPARENT_HUGEPAGE`. No surprise there – that's a cornerstone of modern memory management, right? Implicitly, we're considering the implications of this feature, which makes us think about its impact on the rest of the memory subsystem. Now, with `TRANSPARENT_HUGEPAGE` confirmed, the immediate follow-up is to consider its interaction with another kernel-level aspect. I need to now check the state of `DEVICE_PRIVATE`, and how that might influence the use of hugepages. There could be an important relationship to consider between the two, which is what I need to understand for this particular problem or investigation. |
| 32/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "config DEVICE_PRIVATE"
}Results: {
"Output": "mm/Kconfig=1266=config GET_FREE_REGION\n--\nmm/Kconfig-1268-\nmm/Kconfig:1269:config DEVICE_PRIVATE\nmm/Kconfig-1270-\tbool \"Unaddressable device memory (GPU memory, ...)\"\n"
} |
| 33/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37603 output: 20 thoughts: 14 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Internal Validation Check** Okay, affirmative. Got it. Now, my immediate next step is to perform a direct validation check using `HMM_MIRROR`. This is a crucial diagnostic for verifying the current state of affairs and ruling out any immediate red flags. I need a clear baseline established before proceeding. |
| 34/2 | 2026/08/04 04:56 | tool | grepper |
0mArgs:{
"Expression": "config HMM_MIRROR"
}Results: {
"Output": "mm/Kconfig=1237=config ZONE_DEVICE\n--\nmm/Kconfig-1260-#\nmm/Kconfig:1261:config HMM_MIRROR\nmm/Kconfig-1262-\tbool\n"
} |
| 35/2 | 2026/08/04 04:56 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37789 output: 20 thoughts: 14 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Quick Confirmation & Targeted Inquiry** Yes, that’s where my head's at too. I immediately want to drill down on the `DEVICE_MIGRATION` aspect. It's the lynchpin, isn't it? Gotta make sure that's solid, and precisely what part of the whole picture are we checking? I can already picture a few scenarios, but let's see what’s the focus here. |
| 36/2 | 2026/08/04 04:57 | tool | grepper |
0mArgs:{
"Expression": "config DEVICE_MIGRATION"
}Results: {
"Output": "mm/Kconfig=696=config MIGRATION\n--\nmm/Kconfig-699-\nmm/Kconfig:700:config DEVICE_MIGRATION\nmm/Kconfig-701-\tdef_bool MIGRATION \u0026\u0026 ZONE_DEVICE\n"
} |
| 37/2 | 2026/08/04 04:57 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38008 output: 18 thoughts: 12 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Confirming & Zeroing In** Okay, yes. That’s the starting point. But, what’s the immediate next step? I need to focus on the key area of concern. It seems obvious that the next thing to investigate is `MIGRATION`. My intuition suggests that this will be the crux of the matter. Let's see what's involved with that. |
| 38/2 | 2026/08/04 04:57 | tool | grepper |
0mArgs:{
"Expression": "config MIGRATION"
}Results: {
"Output": "mm/Kconfig=685=config NUMA_MIGRATION\n--\nmm/Kconfig-695-\nmm/Kconfig:696:config MIGRATION\nmm/Kconfig-697-\tbool\n"
} |
| 39/2 | 2026/08/04 04:57 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38204 output: 21 thoughts: 14 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Memory Hotplug - A Quick Check** Yes, that's right. Time to quickly verify the status of `MEMORY_HOTPLUG`. I need to ensure that the necessary infrastructure for dynamic memory addition and removal is properly configured and functioning as expected. This is a critical component for [**mention your field-specific implication, e.g., cloud environments, embedded systems, high-performance computing**]. I have a hunch that [**mention your specific area of concern, e.g., a recent kernel update, a new hardware configuration, a reported error**] requires a check on the configuration. Making sure this functionality is online will confirm the system's ability to handle resource changes without downtime. |
| 40/2 | 2026/08/04 04:57 | tool | grepper |
0mArgs:{
"Expression": "config MEMORY_HOTPLUG"
}Results: {
"Output": "mm/Kconfig=519=config ARCH_ENABLE_MEMORY_HOTPLUG\n--\nmm/Kconfig-522-# eventually, we can have this option just 'select SPARSEMEM'\nmm/Kconfig:523:menuconfig MEMORY_HOTPLUG\nmm/Kconfig-524-\tbool \"Memory hotplug\"\n"
} |
| 41/2 | 2026/08/04 04:57 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38495 output: 214 thoughts: 216 Thoughts: **My Assessment of the HMM Memory Migration Patch** Okay, so I've got this new patch introducing memory migration via HMM during a page fault, which sounds... interesting. Let's see, the configurations it touches are: `TEST_HMM`, `TRANSPARENT_HUGEPAGE`, `DEVICE_PRIVATE`, `ZONE_DEVICE`, `MEMORY_HOTPLUG`, `MEMORY_HOTREMOVE`, `SPARSEMEM_VMEMMAP`, `MIGRATION`, `DEVICE_MIGRATION`, and `HMM_MIRROR`. That's a pretty wide net of potentially affected areas, which isn't entirely unexpected given the nature of the beast. The key symbols I need to focus on are `hmm_range_fault`, `migrate_vma_setup`, and `migrate_hmm_range_setup`. Looks like it's tweaking the behavior and adding new flags to those functions. This is where the rubber meets the road, isn't it? We're talking about fundamental changes to how the kernel handles memory management, especially in the context of HMM and memory migration, which, if poorly handled, could have pretty dire consequences. Given the potential impact, particularly as it intersects with other HMM users and the test driver, I'm thinking we need to give this a proper going-over. That `test_hmm` driver is clearly a pathway here. It seems like a decent place to start poking and prodding to see what breaks, or *if* anything breaks. And those functional changes... yeah, that's screaming "fuzzing target" to me. Let's set up a fuzzing campaign to really put this through its paces. I'm going to call that "set-results" - we need to see what comes out of the fuzzing. |
| 42/2 | 2026/08/04 04:57 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"TEST_HMM",
"TRANSPARENT_HUGEPAGE",
"DEVICE_PRIVATE",
"ZONE_DEVICE",
"MEMORY_HOTPLUG",
"MEMORY_HOTREMOVE",
"SPARSEMEM_VMEMMAP",
"MIGRATION",
"DEVICE_MIGRATION",
"HMM_MIRROR"
],
"FocusSymbols": [
"hmm_range_fault",
"migrate_vma_setup",
"migrate_hmm_range_setup"
],
"Reasoning": "The patch introduces a new feature to migrate memory via HMM during a page fault. It modifies core HMM and memory migration logic, which is reachable via the test_hmm driver or other HMM users. It adds new flags and modifies the behavior of hmm_range_fault and migrate_vma_setup. These are functional changes to core kernel memory management and should be fuzzed.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"TEST_HMM",
"TRANSPARENT_HUGEPAGE",
"DEVICE_PRIVATE",
"ZONE_DEVICE",
"MEMORY_HOTPLUG",
"MEMORY_HOTREMOVE",
"SPARSEMEM_VMEMMAP",
"MIGRATION",
"DEVICE_MIGRATION",
"HMM_MIRROR"
],
"FocusSymbols": [
"hmm_range_fault",
"migrate_vma_setup",
"migrate_hmm_range_setup"
],
"Reasoning": "The patch introduces a new feature to migrate memory via HMM during a page fault. It modifies core HMM and memory migration logic, which is reachable via the test_hmm driver or other HMM users. It adds new flags and modifies the behavior of hmm_range_fault and migrate_vma_setup. These are functional changes to core kernel memory management and should be fuzzed.",
"WorthFuzzing": true
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|