AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/07/02 17:26 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "KVM"
  ],
  "FocusSymbols": [
    "kvm_gmem_change_flags",
    "kvm_gmem_check_no_change",
    "__kvm_gmem_invalidate_start",
    "__kvm_gmem_invalidate_end",
    "kvm_gmem_error_folio"
  ],
  "Reasoning": "The patch introduces support for dirty page logging for `guest_memfd` backed memslots in KVM. It modifies the KVM memory region configuration logic and the invalidation paths for guest memory. These are core functional changes in KVM that can be reached via KVM ioctls (e.g., `KVM_SET_USER_MEMORY_REGION2`) and memory invalidation events, making them highly relevant for fuzzing.",
  "WorthFuzzing": true
}

1/1 2026/07/02 17:26 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 1f3c7ac55717899c4c91ac49fde364885d88ee69\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Thu Jul 2 17:26:53 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst\nindex a5f9ee92f43e8d..5012afe6a9b559 100644\n--- a/Documentation/virt/kvm/api.rst\n+++ b/Documentation/virt/kvm/api.rst\n@@ -9493,6 +9493,15 @@ take care to differentiate between these cases.\n The presence of this capability indicates that the nested KVM guest can\n start in ESA mode.\n \n+8.48 KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES\n+---------------------------------------------\n+\n+:Architectures: all\n+\n+The presence of this capability indicates that memslots backed by a guest_memfd\n+file descriptor created with the GUEST_MEMFD_FLAG_MMAP flag can have dirty\n+page logging enabled.\n+\n 9. Known KVM API problems\n =========================\n \ndiff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c\nindex 50adfff75be82e..e92e5d7b533ada 100644\n--- a/arch/arm64/kvm/arm.c\n+++ b/arch/arm64/kvm/arm.c\n@@ -393,6 +393,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)\n \tcase KVM_CAP_COUNTER_OFFSET:\n \tcase KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:\n \tcase KVM_CAP_ARM_SEA_TO_USER:\n+#ifdef CONFIG_KVM_GUEST_MEMFD\n+\tcase KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES:\n+#endif\n \t\tr = 1;\n \t\tbreak;\n \tcase KVM_CAP_SET_GUEST_DEBUG2:\n@@ -493,6 +496,25 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)\n \treturn r;\n }\n \n+#ifdef CONFIG_KVM_GUEST_MEMFD\n+bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)\n+{\n+\t/*\n+\t * Protected pKVM VMs don't allow dirty page logging, fail early here\n+\t * instead of in kvm_arch_prepare_memory_region().\n+\t *\n+\t * KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES is not available for a\n+\t * protected pKVM VM, and returning false means that\n+\t * KVM_SET_USER_MEMORY_REGION2 fails with EINVAL, which is consistent\n+\t * with unsupported memslot flags.\n+\t */\n+\tif (!is_protected_kvm_enabled())\n+\t\treturn true;\n+\n+\treturn kvm_pkvm_ext_allowed(kvm, KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES);\n+}\n+#endif\n+\n long kvm_arch_dev_ioctl(struct file *filp,\n \t\t\tunsigned int ioctl, unsigned long arg)\n {\ndiff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c\nindex 6c941aaa10c639..f6e6de153ce253 100644\n--- a/arch/arm64/kvm/mmu.c\n+++ b/arch/arm64/kvm/mmu.c\n@@ -1648,7 +1648,8 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)\n \t\treturn ret;\n \t}\n \n-\tif (!(s2fd-\u003ememslot-\u003eflags \u0026 KVM_MEM_READONLY))\n+\tif (!(s2fd-\u003ememslot-\u003eflags \u0026 KVM_MEM_READONLY) \u0026\u0026\n+\t    (!kvm_slot_dirty_track_enabled(s2fd-\u003ememslot) || write_fault))\n \t\tprot |= KVM_PGTABLE_PROT_W;\n \n \tif (s2fd-\u003enested)\ndiff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h\nindex ab8cfaec82d31e..c44e9253eb400a 100644\n--- a/include/linux/kvm_host.h\n+++ b/include/linux/kvm_host.h\n@@ -56,6 +56,7 @@\n  */\n #define KVM_MEMSLOT_INVALID\t\t\t(1UL \u003c\u003c 16)\n #define KVM_MEMSLOT_GMEM_ONLY\t\t\t(1UL \u003c\u003c 17)\n+#define MEMSLOT_USER_FLAGS_MASK\t\t\t0xffff\n \n /*\n  * Bit 63 of the memslot generation number is an \"update in-progress flag\",\n@@ -731,6 +732,9 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)\n \n #ifdef CONFIG_KVM_GUEST_MEMFD\n bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm);\n+bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm);\n+int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *slot,\n+\t\t\t     unsigned int fd, loff_t offset);\n \n static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)\n {\n@@ -741,6 +745,17 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)\n \n \treturn flags;\n }\n+#else\n+static inline bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)\n+{\n+\treturn false;\n+}\n+static inline int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *slot,\n+\t\t\t\t\t   unsigned int fd, loff_t offset)\n+{\n+\tWARN_ON_ONCE(1);\n+\treturn -EIO;\n+}\n #endif\n \n #ifndef kvm_arch_has_readonly_mem\ndiff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h\nindex 419011097fa8e2..5a53e2e19b2fb2 100644\n--- a/include/uapi/linux/kvm.h\n+++ b/include/uapi/linux/kvm.h\n@@ -997,6 +997,7 @@ struct kvm_enable_cap {\n #define KVM_CAP_S390_KEYOP 247\n #define KVM_CAP_S390_VSIE_ESAMODE 248\n #define KVM_CAP_S390_HPAGE_2G 249\n+#define KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES 250\n \n struct kvm_irq_routing_irqchip {\n \t__u32 irqchip;\ndiff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c\nindex db57c5766ab604..210bdd76f0aad7 100644\n--- a/virt/kvm/guest_memfd.c\n+++ b/virt/kvm/guest_memfd.c\n@@ -25,6 +25,7 @@ struct gmem_file {\n \tstruct kvm *kvm;\n \tstruct xarray bindings;\n \tstruct list_head entry;\n+\tbool found_memslot;\t/* Used for balancing invalidations when punching a hole */\n };\n \n struct gmem_inode {\n@@ -43,6 +44,29 @@ static __always_inline struct gmem_inode *GMEM_I(struct inode *inode)\n #define kvm_gmem_for_each_file(f, inode) \\\n \tlist_for_each_entry(f, \u0026GMEM_I(inode)-\u003egmem_file_list, entry)\n \n+static void *memslot_to_xa_value(struct kvm_memory_slot *slot)\n+{\n+\tWARN_ON_ONCE(sizeof(slot-\u003eas_id) \u003e 16);\n+\tWARN_ON_ONCE(sizeof(slot-\u003eid) \u003e 16);\n+\tWARN_ON_ONCE(sizeof(slot-\u003eas_id) + sizeof(slot-\u003eid) \u003e sizeof(unsigned long));\n+\n+\treturn xa_mk_value(((unsigned long)slot-\u003eas_id) \u003c\u003c 16 | (unsigned long)slot-\u003eid);\n+}\n+\n+static struct kvm_memory_slot *xa_value_to_memslot(struct kvm *kvm, const void *entry)\n+{\n+\tunsigned long full_id = xa_to_value(entry);\n+\tu16 as_id = (full_id \u003e\u003e 16) \u0026 U16_MAX;\n+\tshort id = full_id \u0026 U16_MAX;\n+\n+\t/*\n+\t * Do not ignore KVM_MEMSLOT_INVALID memslots, as we want\n+\t * -\u003eerror_remove_folio(), when it races with memslot deletion, to have\n+\t *  unmapped the memory upon completion.\n+\t */\n+\treturn id_to_memslot(__kvm_memslots(kvm, as_id), id);\n+}\n+\n /**\n  * folio_file_pfn - like folio_file_page, but return a pfn.\n  * @folio: The folio which contains this index.\n@@ -157,7 +181,7 @@ static enum kvm_gfn_range_filter kvm_gmem_get_invalidate_filter(struct inode *in\n \treturn KVM_FILTER_PRIVATE;\n }\n \n-static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,\n+static bool __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,\n \t\t\t\t\tpgoff_t end,\n \t\t\t\t\tenum kvm_gfn_range_filter attr_filter)\n {\n@@ -165,9 +189,15 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,\n \tstruct kvm_memory_slot *slot;\n \tstruct kvm *kvm = f-\u003ekvm;\n \tunsigned long index;\n+\tvoid *entry;\n \n-\txa_for_each_range(\u0026f-\u003ebindings, index, slot, start, end - 1) {\n-\t\tpgoff_t pgoff = slot-\u003egmem.pgoff;\n+\txa_for_each_range(\u0026f-\u003ebindings, index, entry, start, end - 1) {\n+\t\tpgoff_t pgoff;\n+\n+\t\tslot = xa_value_to_memslot(kvm, entry);\n+\t\tif (!slot)\n+\t\t\tcontinue;\n+\t\tpgoff = slot-\u003egmem.pgoff;\n \n \t\tstruct kvm_gfn_range gfn_range = {\n \t\t\t.start = slot-\u003ebase_gfn + max(pgoff, start) - pgoff,\n@@ -192,6 +222,8 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,\n \n \tif (found_memslot)\n \t\tKVM_MMU_UNLOCK(kvm);\n+\n+\treturn found_memslot;\n }\n \n static void kvm_gmem_invalidate_start(struct inode *inode, pgoff_t start,\n@@ -199,11 +231,22 @@ static void kvm_gmem_invalidate_start(struct inode *inode, pgoff_t start,\n {\n \tenum kvm_gfn_range_filter attr_filter;\n \tstruct gmem_file *f;\n+\tstruct kvm *kvm;\n+\tint idx;\n \n \tattr_filter = kvm_gmem_get_invalidate_filter(inode);\n \n-\tkvm_gmem_for_each_file(f, inode)\n-\t\t__kvm_gmem_invalidate_start(f, start, end, attr_filter);\n+\tkvm_gmem_for_each_file(f, inode) {\n+\t\tkvm = f-\u003ekvm;\n+\t\tidx = srcu_read_lock(\u0026kvm-\u003esrcu);\n+\t\t/*\n+\t\t * This is safe to do because calls to\n+\t\t * kvm_gmem_invalidate_start() are serialized by\n+\t\t * filemap_invalidate_lock().\n+\t\t */\n+\t\tf-\u003efound_memslot = __kvm_gmem_invalidate_start(f, start, end, attr_filter);\n+\t\tsrcu_read_unlock(\u0026kvm-\u003esrcu, idx);\n+\t}\n }\n \n static void __kvm_gmem_invalidate_end(struct gmem_file *f, pgoff_t start,\n@@ -223,8 +266,11 @@ static void kvm_gmem_invalidate_end(struct inode *inode, pgoff_t start,\n {\n \tstruct gmem_file *f;\n \n-\tkvm_gmem_for_each_file(f, inode)\n+\tkvm_gmem_for_each_file(f, inode) {\n+\t\tif (!f-\u003efound_memslot)\n+\t\t\tcontinue;\n \t\t__kvm_gmem_invalidate_end(f, start, end);\n+\t}\n }\n \n static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len)\n@@ -326,6 +372,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)\n \tstruct kvm_memory_slot *slot;\n \tstruct kvm *kvm = f-\u003ekvm;\n \tunsigned long index;\n+\tvoid *entry;\n \n \t/*\n \t * Prevent concurrent attempts to *unbind* a memslot.  This is the last\n@@ -344,17 +391,18 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)\n \n \tfilemap_invalidate_lock(inode-\u003ei_mapping);\n \n-\txa_for_each(\u0026f-\u003ebindings, index, slot)\n+\txa_for_each(\u0026f-\u003ebindings, index, entry) {\n+\t\tslot = xa_value_to_memslot(kvm, entry);\n \t\tWRITE_ONCE(slot-\u003egmem.file, NULL);\n+\t}\n \n \t/*\n \t * All in-flight operations are gone and new bindings can be created.\n \t * Zap all SPTEs pointed at by this file.  Do not free the backing\n \t * memory, as its lifetime is associated with the inode, not the file.\n \t */\n-\t__kvm_gmem_invalidate_start(f, 0, -1ul,\n-\t\t\t\t    kvm_gmem_get_invalidate_filter(inode));\n-\t__kvm_gmem_invalidate_end(f, 0, -1ul);\n+\tif (__kvm_gmem_invalidate_start(f, 0, -1ul, kvm_gmem_get_invalidate_filter(inode)))\n+\t\t__kvm_gmem_invalidate_end(f, 0, -1ul);\n \n \tlist_del(\u0026f-\u003eentry);\n \n@@ -498,14 +546,20 @@ static int kvm_gmem_migrate_folio(struct address_space *mapping,\n \n static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folio)\n {\n+\tenum kvm_gfn_range_filter attr_filter;\n+\tstruct inode *inode = mapping-\u003ehost;\n+\tstruct gmem_file *f;\n \tpgoff_t start, end;\n+\tbool found_memslot;\n+\tstruct kvm *kvm;\n+\tint idx;\n \n \tfilemap_invalidate_lock_shared(mapping);\n \n \tstart = folio-\u003eindex;\n \tend = start + folio_nr_pages(folio);\n \n-\tkvm_gmem_invalidate_start(mapping-\u003ehost, start, end);\n+\tattr_filter = kvm_gmem_get_invalidate_filter(inode);\n \n \t/*\n \t * Do not truncate the range, what action is taken in response to the\n@@ -515,8 +569,19 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol\n \t * at which point KVM can either terminate the VM or propagate the\n \t * error to userspace.\n \t */\n+\tkvm_gmem_for_each_file(f, inode) {\n+\t\tkvm = f-\u003ekvm;\n \n-\tkvm_gmem_invalidate_end(mapping-\u003ehost, start, end);\n+\t\tidx = srcu_read_lock(\u0026kvm-\u003esrcu);\n+\t\tfound_memslot = __kvm_gmem_invalidate_start(f, start, end, attr_filter);\n+\t\tsrcu_read_unlock(\u0026kvm-\u003esrcu, idx);\n+\n+\t\tif (found_memslot) {\n+\t\t\tKVM_MMU_LOCK(kvm);\n+\t\t\tkvm_mmu_invalidate_end(kvm);\n+\t\t\tKVM_MMU_UNLOCK(kvm);\n+\t\t}\n+\t}\n \n \tfilemap_invalidate_unlock_shared(mapping);\n \n@@ -557,6 +622,11 @@ bool __weak kvm_arch_supports_gmem_init_shared(struct kvm *kvm)\n \treturn true;\n }\n \n+bool __weak kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)\n+{\n+\treturn false;\n+}\n+\n static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)\n {\n \tstatic const char *name = \"[kvm-gmem]\";\n@@ -640,6 +710,66 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)\n \treturn __kvm_gmem_create(kvm, size, flags);\n }\n \n+static int __kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *old,\n+\t\t\t\t      struct file *old_file, unsigned int fd,\n+\t\t\t\t      loff_t offset)\n+{\n+\tstruct file *new_file;\n+\n+\tnew_file = fget(fd);\n+\tif (!new_file)\n+\t\treturn -EBADF;\n+\tif (new_file != old_file) {\n+\t\tfput(new_file);\n+\t\treturn -EBADF;\n+\t}\n+\tfput(new_file);\n+\n+\tif (old-\u003egmem.pgoff != offset \u003e\u003e PAGE_SHIFT)\n+\t\treturn -EINVAL;\n+\n+\treturn 0;\n+}\n+\n+int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *old,\n+\t\t\t     unsigned int fd, loff_t offset)\n+{\n+\tCLASS(gmem_get_file, old_file)(old);\n+\n+\treturn __kvm_gmem_check_no_change(kvm, old, old_file, fd, offset);\n+}\n+\n+int kvm_gmem_change_flags(struct kvm *kvm, struct kvm_memory_slot *old,\n+\t\t\t  struct kvm_memory_slot *new, unsigned int fd,\n+\t\t\t  loff_t offset)\n+{\n+\tstruct gmem_file *old_f;\n+\tint ret;\n+\n+\tlockdep_assert_held(\u0026kvm-\u003eslots_lock);\n+\n+\tif (!kvm_memslot_is_gmem_only(old))\n+\t\treturn -EINVAL;\n+\n+\tCLASS(gmem_get_file, old_file)(old);\n+\n+\tret = __kvm_gmem_check_no_change(kvm, old, old_file, fd, offset);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\told_f = old_file-\u003eprivate_data;\n+\tif (xa_load(\u0026old_f-\u003ebindings, old-\u003egmem.pgoff) != memslot_to_xa_value(new)) {\n+\t\tWARN_ON_ONCE(xa_to_value(xa_load(\u0026old_f-\u003ebindings, old-\u003egmem.pgoff)));\n+\t\treturn -EIO;\n+\t}\n+\n+\tnew-\u003egmem.file = old-\u003egmem.file;\n+\tnew-\u003egmem.pgoff = old-\u003egmem.pgoff;\n+\tnew-\u003eflags |= KVM_MEMSLOT_GMEM_ONLY;\n+\n+\treturn 0;\n+}\n+\n int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,\n \t\t  unsigned int fd, uoff_t offset)\n {\n@@ -669,6 +799,11 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,\n \tif (!PAGE_ALIGNED(offset) || offset + size \u003e i_size_read(inode))\n \t\tgoto err;\n \n+\tif (slot-\u003eflags \u0026 KVM_MEM_LOG_DIRTY_PAGES \u0026\u0026\n+\t    (!kvm_gmem_supports_mmap(inode) ||\n+\t     !kvm_arch_supports_gmem_mmap_dirty_logging(kvm)))\n+\t\tgoto err;\n+\n \tfilemap_invalidate_lock(inode-\u003ei_mapping);\n \n \tstart = offset \u003e\u003e PAGE_SHIFT;\n@@ -691,7 +826,7 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,\n \tif (kvm_gmem_supports_mmap(inode))\n \t\tslot-\u003eflags |= KVM_MEMSLOT_GMEM_ONLY;\n \n-\txa_store_range(\u0026f-\u003ebindings, start, end - 1, slot, GFP_KERNEL);\n+\txa_store_range(\u0026f-\u003ebindings, start, end - 1, memslot_to_xa_value(slot), GFP_KERNEL);\n \tfilemap_invalidate_unlock(inode-\u003ei_mapping);\n \n \t/*\n@@ -765,8 +900,8 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,\n \t\treturn ERR_PTR(-EFAULT);\n \t}\n \n-\tif (xa_load(\u0026f-\u003ebindings, index) != slot) {\n-\t\tWARN_ON_ONCE(xa_load(\u0026f-\u003ebindings, index));\n+\tif (xa_load(\u0026f-\u003ebindings, index) != memslot_to_xa_value(slot)) {\n+\t\tWARN_ON_ONCE(xa_to_value(xa_load(\u0026f-\u003ebindings, index)));\n \t\treturn ERR_PTR(-EIO);\n \t}\n \ndiff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c\nindex e44c20c049610d..af380e8f2b68d4 100644\n--- a/virt/kvm/kvm_main.c\n+++ b/virt/kvm/kvm_main.c\n@@ -1573,14 +1573,14 @@ static void kvm_replace_memslot(struct kvm *kvm,\n static int check_memory_region_flags(struct kvm *kvm,\n \t\t\t\t     const struct kvm_userspace_memory_region2 *mem)\n {\n-\tu32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;\n+\tu32 valid_flags = 0;\n \n \tif (IS_ENABLED(CONFIG_KVM_GUEST_MEMFD))\n \t\tvalid_flags |= KVM_MEM_GUEST_MEMFD;\n \n-\t/* Dirty logging private memory is not currently supported. */\n-\tif (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD)\n-\t\tvalid_flags \u0026= ~KVM_MEM_LOG_DIRTY_PAGES;\n+\tif (!(mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD) ||\n+\t    kvm_arch_supports_gmem_mmap_dirty_logging(kvm))\n+\t\tvalid_flags |= KVM_MEM_LOG_DIRTY_PAGES;\n \n \t/*\n \t * GUEST_MEMFD is incompatible with read-only memslots, as writes to\n@@ -1739,16 +1739,6 @@ static void kvm_commit_memory_region(struct kvm *kvm,\n \t\t */\n \t\tif (old-\u003edirty_bitmap \u0026\u0026 !new-\u003edirty_bitmap)\n \t\t\tkvm_destroy_dirty_bitmap(old);\n-\n-\t\t/*\n-\t\t * Unbind the guest_memfd instance as needed; the @new slot has\n-\t\t * already created its own binding.  TODO: Drop the WARN when\n-\t\t * dirty logging guest_memfd memslots is supported.  Until then,\n-\t\t * flags-only changes on guest_memfd slots should be impossible.\n-\t\t */\n-\t\tif (WARN_ON_ONCE(old-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD))\n-\t\t\tkvm_gmem_unbind(old);\n-\n \t\t/*\n \t\t * The final quirk.  Free the detached, old slot, but only its\n \t\t * memory, not any metadata.  Metadata, including arch specific\n@@ -2073,22 +2063,27 @@ static int kvm_set_memory_region(struct kvm *kvm,\n \t\tif ((kvm-\u003enr_memslot_pages + npages) \u003c kvm-\u003enr_memslot_pages)\n \t\t\treturn -EINVAL;\n \t} else { /* Modify an existing slot. */\n-\t\t/* Private memslots are immutable, they can only be deleted. */\n-\t\tif (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD)\n-\t\t\treturn -EINVAL;\n \t\tif ((mem-\u003euserspace_addr != old-\u003euserspace_addr) ||\n \t\t    (npages != old-\u003enpages) ||\n \t\t    ((mem-\u003eflags ^ old-\u003eflags) \u0026 (KVM_MEM_READONLY | KVM_MEM_GUEST_MEMFD)))\n \t\t\treturn -EINVAL;\n \n-\t\tif (base_gfn != old-\u003ebase_gfn)\n+\t\tif (base_gfn != old-\u003ebase_gfn) {\n \t\t\tchange = KVM_MR_MOVE;\n-\t\telse if (mem-\u003eflags != old-\u003eflags)\n+\t\t} else if (mem-\u003eflags != (old-\u003eflags \u0026 MEMSLOT_USER_FLAGS_MASK)) {\n \t\t\tchange = KVM_MR_FLAGS_ONLY;\n-\t\telse /* Nothing to change. */\n+\t\t} else if (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD) {\n+\t\t\treturn kvm_gmem_check_no_change(kvm, old, mem-\u003eguest_memfd,\n+\t\t\t\t\t\t\tmem-\u003eguest_memfd_offset);\n+\t\t} else {\n \t\t\treturn 0;\n+\t\t}\n \t}\n \n+\tif (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD \u0026\u0026\n+\t    change != KVM_MR_CREATE \u0026\u0026 change != KVM_MR_FLAGS_ONLY)\n+\t\treturn -EINVAL;\n+\n \tif ((change == KVM_MR_CREATE || change == KVM_MR_MOVE) \u0026\u0026\n \t    kvm_check_memslot_overlap(slots, id, base_gfn, base_gfn + npages))\n \t\treturn -EEXIST;\n@@ -2105,7 +2100,12 @@ static int kvm_set_memory_region(struct kvm *kvm,\n \tnew-\u003eflags = mem-\u003eflags;\n \tnew-\u003euserspace_addr = mem-\u003euserspace_addr;\n \tif (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD) {\n-\t\tr = kvm_gmem_bind(kvm, new, mem-\u003eguest_memfd, mem-\u003eguest_memfd_offset);\n+\t\tif (change == KVM_MR_CREATE) {\n+\t\t\tr = kvm_gmem_bind(kvm, new, mem-\u003eguest_memfd, mem-\u003eguest_memfd_offset);\n+\t\t} else if (change == KVM_MR_FLAGS_ONLY) {\n+\t\t\tr = kvm_gmem_change_flags(kvm, old, new, mem-\u003eguest_memfd,\n+\t\t\t\t\t\t  mem-\u003eguest_memfd_offset);\n+\t\t}\n \t\tif (r)\n \t\t\tgoto out;\n \t}\n@@ -2117,7 +2117,7 @@ static int kvm_set_memory_region(struct kvm *kvm,\n \treturn 0;\n \n out_unbind:\n-\tif (mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD)\n+\tif ((mem-\u003eflags \u0026 KVM_MEM_GUEST_MEMFD) \u0026\u0026 change == KVM_MR_CREATE)\n \t\tkvm_gmem_unbind(new);\n out:\n \tkfree(new);\ndiff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h\nindex 7510ca915dd1aa..c58dfeb0f3df55 100644\n--- a/virt/kvm/kvm_mm.h\n+++ b/virt/kvm/kvm_mm.h\n@@ -77,6 +77,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);\n int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,\n \t\t  unsigned int fd, uoff_t offset);\n void kvm_gmem_unbind(struct kvm_memory_slot *slot);\n+int kvm_gmem_change_flags(struct kvm *kvm, struct kvm_memory_slot *old,\n+\t\t\t  struct kvm_memory_slot *new, unsigned int fd,\n+\t\t\t  loff_t offset);\n #else\n static inline int kvm_gmem_init(struct module *module)\n {\n@@ -95,6 +98,14 @@ static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot)\n {\n \tWARN_ON_ONCE(1);\n }\n+static inline int kvm_gmem_change_flags(struct kvm *kvm,\n+\t\t\t\t\tstruct kvm_memory_slot *old,\n+\t\t\t\t\tstruct kvm_memory_slot *new,\n+\t\t\t\t\tunsigned int fd, loff_t offset)\n+{\n+\tWARN_ON_ONCE(1);\n+\treturn -EIO;\n+}\n #endif /* CONFIG_KVM_GUEST_MEMFD */\n \n #endif /* __KVM_MM_H__ */\n"
}

2/1 2026/07/02 17:26 agent patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
{
  "EnableConfigs": [
    "KVM"
  ],
  "FocusSymbols": [
    "kvm_gmem_change_flags",
    "kvm_gmem_check_no_change",
    "__kvm_gmem_invalidate_start",
    "__kvm_gmem_invalidate_end",
    "kvm_gmem_error_folio"
  ],
  "Reasoning": "The patch introduces support for dirty page logging for `guest_memfd` backed memslots in KVM. It modifies the KVM memory region configuration logic and the invalidation paths for guest memory. These are core functional changes in KVM that can be reached via KVM ioctls (e.g., `KVM_SET_USER_MEMORY_REGION2`) and memory invalidation events, making them highly relevant for fuzzing.",
  "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).

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.
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.
   Do not list too generic configs (we already have them enabled). Only list those that
   specifically cover the modified code. 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 1f3c7ac55717899c4c91ac49fde364885d88ee69
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Jul 2 17:26:53 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index a5f9ee92f43e8d..5012afe6a9b559 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -9493,6 +9493,15 @@ take care to differentiate between these cases.
 The presence of this capability indicates that the nested KVM guest can
 start in ESA mode.
 
+8.48 KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES
+---------------------------------------------
+
+:Architectures: all
+
+The presence of this capability indicates that memslots backed by a guest_memfd
+file descriptor created with the GUEST_MEMFD_FLAG_MMAP flag can have dirty
+page logging enabled.
+
 9. Known KVM API problems
 =========================
 
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 50adfff75be82e..e92e5d7b533ada 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -393,6 +393,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_COUNTER_OFFSET:
 	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
 	case KVM_CAP_ARM_SEA_TO_USER:
+#ifdef CONFIG_KVM_GUEST_MEMFD
+	case KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES:
+#endif
 		r = 1;
 		break;
 	case KVM_CAP_SET_GUEST_DEBUG2:
@@ -493,6 +496,25 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	return r;
 }
 
+#ifdef CONFIG_KVM_GUEST_MEMFD
+bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)
+{
+	/*
+	 * Protected pKVM VMs don't allow dirty page logging, fail early here
+	 * instead of in kvm_arch_prepare_memory_region().
+	 *
+	 * KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES is not available for a
+	 * protected pKVM VM, and returning false means that
+	 * KVM_SET_USER_MEMORY_REGION2 fails with EINVAL, which is consistent
+	 * with unsupported memslot flags.
+	 */
+	if (!is_protected_kvm_enabled())
+		return true;
+
+	return kvm_pkvm_ext_allowed(kvm, KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES);
+}
+#endif
+
 long kvm_arch_dev_ioctl(struct file *filp,
 			unsigned int ioctl, unsigned long arg)
 {
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 6c941aaa10c639..f6e6de153ce253 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1648,7 +1648,8 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)
 		return ret;
 	}
 
-	if (!(s2fd->memslot->flags & KVM_MEM_READONLY))
+	if (!(s2fd->memslot->flags & KVM_MEM_READONLY) &&
+	    (!kvm_slot_dirty_track_enabled(s2fd->memslot) || write_fault))
 		prot |= KVM_PGTABLE_PROT_W;
 
 	if (s2fd->nested)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index ab8cfaec82d31e..c44e9253eb400a 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -56,6 +56,7 @@
  */
 #define KVM_MEMSLOT_INVALID			(1UL << 16)
 #define KVM_MEMSLOT_GMEM_ONLY			(1UL << 17)
+#define MEMSLOT_USER_FLAGS_MASK			0xffff
 
 /*
  * Bit 63 of the memslot generation number is an "update in-progress flag",
@@ -731,6 +732,9 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm)
 
 #ifdef CONFIG_KVM_GUEST_MEMFD
 bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm);
+bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm);
+int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *slot,
+			     unsigned int fd, loff_t offset);
 
 static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)
 {
@@ -741,6 +745,17 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)
 
 	return flags;
 }
+#else
+static inline bool kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)
+{
+	return false;
+}
+static inline int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *slot,
+					   unsigned int fd, loff_t offset)
+{
+	WARN_ON_ONCE(1);
+	return -EIO;
+}
 #endif
 
 #ifndef kvm_arch_has_readonly_mem
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 419011097fa8e2..5a53e2e19b2fb2 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -997,6 +997,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
 #define KVM_CAP_S390_HPAGE_2G 249
+#define KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES 250
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index db57c5766ab604..210bdd76f0aad7 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -25,6 +25,7 @@ struct gmem_file {
 	struct kvm *kvm;
 	struct xarray bindings;
 	struct list_head entry;
+	bool found_memslot;	/* Used for balancing invalidations when punching a hole */
 };
 
 struct gmem_inode {
@@ -43,6 +44,29 @@ static __always_inline struct gmem_inode *GMEM_I(struct inode *inode)
 #define kvm_gmem_for_each_file(f, inode) \
 	list_for_each_entry(f, &GMEM_I(inode)->gmem_file_list, entry)
 
+static void *memslot_to_xa_value(struct kvm_memory_slot *slot)
+{
+	WARN_ON_ONCE(sizeof(slot->as_id) > 16);
+	WARN_ON_ONCE(sizeof(slot->id) > 16);
+	WARN_ON_ONCE(sizeof(slot->as_id) + sizeof(slot->id) > sizeof(unsigned long));
+
+	return xa_mk_value(((unsigned long)slot->as_id) << 16 | (unsigned long)slot->id);
+}
+
+static struct kvm_memory_slot *xa_value_to_memslot(struct kvm *kvm, const void *entry)
+{
+	unsigned long full_id = xa_to_value(entry);
+	u16 as_id = (full_id >> 16) & U16_MAX;
+	short id = full_id & U16_MAX;
+
+	/*
+	 * Do not ignore KVM_MEMSLOT_INVALID memslots, as we want
+	 * ->error_remove_folio(), when it races with memslot deletion, to have
+	 *  unmapped the memory upon completion.
+	 */
+	return id_to_memslot(__kvm_memslots(kvm, as_id), id);
+}
+
 /**
  * folio_file_pfn - like folio_file_page, but return a pfn.
  * @folio: The folio which contains this index.
@@ -157,7 +181,7 @@ static enum kvm_gfn_range_filter kvm_gmem_get_invalidate_filter(struct inode *in
 	return KVM_FILTER_PRIVATE;
 }
 
-static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
+static bool __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
 					pgoff_t end,
 					enum kvm_gfn_range_filter attr_filter)
 {
@@ -165,9 +189,15 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
 	struct kvm_memory_slot *slot;
 	struct kvm *kvm = f->kvm;
 	unsigned long index;
+	void *entry;
 
-	xa_for_each_range(&f->bindings, index, slot, start, end - 1) {
-		pgoff_t pgoff = slot->gmem.pgoff;
+	xa_for_each_range(&f->bindings, index, entry, start, end - 1) {
+		pgoff_t pgoff;
+
+		slot = xa_value_to_memslot(kvm, entry);
+		if (!slot)
+			continue;
+		pgoff = slot->gmem.pgoff;
 
 		struct kvm_gfn_range gfn_range = {
 			.start = slot->base_gfn + max(pgoff, start) - pgoff,
@@ -192,6 +222,8 @@ static void __kvm_gmem_invalidate_start(struct gmem_file *f, pgoff_t start,
 
 	if (found_memslot)
 		KVM_MMU_UNLOCK(kvm);
+
+	return found_memslot;
 }
 
 static void kvm_gmem_invalidate_start(struct inode *inode, pgoff_t start,
@@ -199,11 +231,22 @@ static void kvm_gmem_invalidate_start(struct inode *inode, pgoff_t start,
 {
 	enum kvm_gfn_range_filter attr_filter;
 	struct gmem_file *f;
+	struct kvm *kvm;
+	int idx;
 
 	attr_filter = kvm_gmem_get_invalidate_filter(inode);
 
-	kvm_gmem_for_each_file(f, inode)
-		__kvm_gmem_invalidate_start(f, start, end, attr_filter);
+	kvm_gmem_for_each_file(f, inode) {
+		kvm = f->kvm;
+		idx = srcu_read_lock(&kvm->srcu);
+		/*
+		 * This is safe to do because calls to
+		 * kvm_gmem_invalidate_start() are serialized by
+		 * filemap_invalidate_lock().
+		 */
+		f->found_memslot = __kvm_gmem_invalidate_start(f, start, end, attr_filter);
+		srcu_read_unlock(&kvm->srcu, idx);
+	}
 }
 
 static void __kvm_gmem_invalidate_end(struct gmem_file *f, pgoff_t start,
@@ -223,8 +266,11 @@ static void kvm_gmem_invalidate_end(struct inode *inode, pgoff_t start,
 {
 	struct gmem_file *f;
 
-	kvm_gmem_for_each_file(f, inode)
+	kvm_gmem_for_each_file(f, inode) {
+		if (!f->found_memslot)
+			continue;
 		__kvm_gmem_invalidate_end(f, start, end);
+	}
 }
 
 static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len)
@@ -326,6 +372,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
 	struct kvm_memory_slot *slot;
 	struct kvm *kvm = f->kvm;
 	unsigned long index;
+	void *entry;
 
 	/*
 	 * Prevent concurrent attempts to *unbind* a memslot.  This is the last
@@ -344,17 +391,18 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
 
 	filemap_invalidate_lock(inode->i_mapping);
 
-	xa_for_each(&f->bindings, index, slot)
+	xa_for_each(&f->bindings, index, entry) {
+		slot = xa_value_to_memslot(kvm, entry);
 		WRITE_ONCE(slot->gmem.file, NULL);
+	}
 
 	/*
 	 * All in-flight operations are gone and new bindings can be created.
 	 * Zap all SPTEs pointed at by this file.  Do not free the backing
 	 * memory, as its lifetime is associated with the inode, not the file.
 	 */
-	__kvm_gmem_invalidate_start(f, 0, -1ul,
-				    kvm_gmem_get_invalidate_filter(inode));
-	__kvm_gmem_invalidate_end(f, 0, -1ul);
+	if (__kvm_gmem_invalidate_start(f, 0, -1ul, kvm_gmem_get_invalidate_filter(inode)))
+		__kvm_gmem_invalidate_end(f, 0, -1ul);
 
 	list_del(&f->entry);
 
@@ -498,14 +546,20 @@ static int kvm_gmem_migrate_folio(struct address_space *mapping,
 
 static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folio)
 {
+	enum kvm_gfn_range_filter attr_filter;
+	struct inode *inode = mapping->host;
+	struct gmem_file *f;
 	pgoff_t start, end;
+	bool found_memslot;
+	struct kvm *kvm;
+	int idx;
 
 	filemap_invalidate_lock_shared(mapping);
 
 	start = folio->index;
 	end = start + folio_nr_pages(folio);
 
-	kvm_gmem_invalidate_start(mapping->host, start, end);
+	attr_filter = kvm_gmem_get_invalidate_filter(inode);
 
 	/*
 	 * Do not truncate the range, what action is taken in response to the
@@ -515,8 +569,19 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol
 	 * at which point KVM can either terminate the VM or propagate the
 	 * error to userspace.
 	 */
+	kvm_gmem_for_each_file(f, inode) {
+		kvm = f->kvm;
 
-	kvm_gmem_invalidate_end(mapping->host, start, end);
+		idx = srcu_read_lock(&kvm->srcu);
+		found_memslot = __kvm_gmem_invalidate_start(f, start, end, attr_filter);
+		srcu_read_unlock(&kvm->srcu, idx);
+
+		if (found_memslot) {
+			KVM_MMU_LOCK(kvm);
+			kvm_mmu_invalidate_end(kvm);
+			KVM_MMU_UNLOCK(kvm);
+		}
+	}
 
 	filemap_invalidate_unlock_shared(mapping);
 
@@ -557,6 +622,11 @@ bool __weak kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
 	return true;
 }
 
+bool __weak kvm_arch_supports_gmem_mmap_dirty_logging(struct kvm *kvm)
+{
+	return false;
+}
+
 static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
 {
 	static const char *name = "[kvm-gmem]";
@@ -640,6 +710,66 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
 	return __kvm_gmem_create(kvm, size, flags);
 }
 
+static int __kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *old,
+				      struct file *old_file, unsigned int fd,
+				      loff_t offset)
+{
+	struct file *new_file;
+
+	new_file = fget(fd);
+	if (!new_file)
+		return -EBADF;
+	if (new_file != old_file) {
+		fput(new_file);
+		return -EBADF;
+	}
+	fput(new_file);
+
+	if (old->gmem.pgoff != offset >> PAGE_SHIFT)
+		return -EINVAL;
+
+	return 0;
+}
+
+int kvm_gmem_check_no_change(struct kvm *kvm, struct kvm_memory_slot *old,
+			     unsigned int fd, loff_t offset)
+{
+	CLASS(gmem_get_file, old_file)(old);
+
+	return __kvm_gmem_check_no_change(kvm, old, old_file, fd, offset);
+}
+
+int kvm_gmem_change_flags(struct kvm *kvm, struct kvm_memory_slot *old,
+			  struct kvm_memory_slot *new, unsigned int fd,
+			  loff_t offset)
+{
+	struct gmem_file *old_f;
+	int ret;
+
+	lockdep_assert_held(&kvm->slots_lock);
+
+	if (!kvm_memslot_is_gmem_only(old))
+		return -EINVAL;
+
+	CLASS(gmem_get_file, old_file)(old);
+
+	ret = __kvm_gmem_check_no_change(kvm, old, old_file, fd, offset);
+	if (ret)
+		return ret;
+
+	old_f = old_file->private_data;
+	if (xa_load(&old_f->bindings, old->gmem.pgoff) != memslot_to_xa_value(new)) {
+		WARN_ON_ONCE(xa_to_value(xa_load(&old_f->bindings, old->gmem.pgoff)));
+		return -EIO;
+	}
+
+	new->gmem.file = old->gmem.file;
+	new->gmem.pgoff = old->gmem.pgoff;
+	new->flags |= KVM_MEMSLOT_GMEM_ONLY;
+
+	return 0;
+}
+
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 		  unsigned int fd, uoff_t offset)
 {
@@ -669,6 +799,11 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 	if (!PAGE_ALIGNED(offset) || offset + size > i_size_read(inode))
 		goto err;
 
+	if (slot->flags & KVM_MEM_LOG_DIRTY_PAGES &&
+	    (!kvm_gmem_supports_mmap(inode) ||
+	     !kvm_arch_supports_gmem_mmap_dirty_logging(kvm)))
+		goto err;
+
 	filemap_invalidate_lock(inode->i_mapping);
 
 	start = offset >> PAGE_SHIFT;
@@ -691,7 +826,7 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 	if (kvm_gmem_supports_mmap(inode))
 		slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
 
-	xa_store_range(&f->bindings, start, end - 1, slot, GFP_KERNEL);
+	xa_store_range(&f->bindings, start, end - 1, memslot_to_xa_value(slot), GFP_KERNEL);
 	filemap_invalidate_unlock(inode->i_mapping);
 
 	/*
@@ -765,8 +900,8 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 		return ERR_PTR(-EFAULT);
 	}
 
-	if (xa_load(&f->bindings, index) != slot) {
-		WARN_ON_ONCE(xa_load(&f->bindings, index));
+	if (xa_load(&f->bindings, index) != memslot_to_xa_value(slot)) {
+		WARN_ON_ONCE(xa_to_value(xa_load(&f->bindings, index)));
 		return ERR_PTR(-EIO);
 	}
 
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e44c20c049610d..af380e8f2b68d4 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1573,14 +1573,14 @@ static void kvm_replace_memslot(struct kvm *kvm,
 static int check_memory_region_flags(struct kvm *kvm,
 				     const struct kvm_userspace_memory_region2 *mem)
 {
-	u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
+	u32 valid_flags = 0;
 
 	if (IS_ENABLED(CONFIG_KVM_GUEST_MEMFD))
 		valid_flags |= KVM_MEM_GUEST_MEMFD;
 
-	/* Dirty logging private memory is not currently supported. */
-	if (mem->flags & KVM_MEM_GUEST_MEMFD)
-		valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
+	if (!(mem->flags & KVM_MEM_GUEST_MEMFD) ||
+	    kvm_arch_supports_gmem_mmap_dirty_logging(kvm))
+		valid_flags |= KVM_MEM_LOG_DIRTY_PAGES;
 
 	/*
 	 * GUEST_MEMFD is incompatible with read-only memslots, as writes to
@@ -1739,16 +1739,6 @@ static void kvm_commit_memory_region(struct kvm *kvm,
 		 */
 		if (old->dirty_bitmap && !new->dirty_bitmap)
 			kvm_destroy_dirty_bitmap(old);
-
-		/*
-		 * Unbind the guest_memfd instance as needed; the @new slot has
-		 * already created its own binding.  TODO: Drop the WARN when
-		 * dirty logging guest_memfd memslots is supported.  Until then,
-		 * flags-only changes on guest_memfd slots should be impossible.
-		 */
-		if (WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD))
-			kvm_gmem_unbind(old);
-
 		/*
 		 * The final quirk.  Free the detached, old slot, but only its
 		 * memory, not any metadata.  Metadata, including arch specific
@@ -2073,22 +2063,27 @@ static int kvm_set_memory_region(struct kvm *kvm,
 		if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages)
 			return -EINVAL;
 	} else { /* Modify an existing slot. */
-		/* Private memslots are immutable, they can only be deleted. */
-		if (mem->flags & KVM_MEM_GUEST_MEMFD)
-			return -EINVAL;
 		if ((mem->userspace_addr != old->userspace_addr) ||
 		    (npages != old->npages) ||
 		    ((mem->flags ^ old->flags) & (KVM_MEM_READONLY | KVM_MEM_GUEST_MEMFD)))
 			return -EINVAL;
 
-		if (base_gfn != old->base_gfn)
+		if (base_gfn != old->base_gfn) {
 			change = KVM_MR_MOVE;
-		else if (mem->flags != old->flags)
+		} else if (mem->flags != (old->flags & MEMSLOT_USER_FLAGS_MASK)) {
 			change = KVM_MR_FLAGS_ONLY;
-		else /* Nothing to change. */
+		} else if (mem->flags & KVM_MEM_GUEST_MEMFD) {
+			return kvm_gmem_check_no_change(kvm, old, mem->guest_memfd,
+							mem->guest_memfd_offset);
+		} else {
 			return 0;
+		}
 	}
 
+	if (mem->flags & KVM_MEM_GUEST_MEMFD &&
+	    change != KVM_MR_CREATE && change != KVM_MR_FLAGS_ONLY)
+		return -EINVAL;
+
 	if ((change == KVM_MR_CREATE || change == KVM_MR_MOVE) &&
 	    kvm_check_memslot_overlap(slots, id, base_gfn, base_gfn + npages))
 		return -EEXIST;
@@ -2105,7 +2100,12 @@ static int kvm_set_memory_region(struct kvm *kvm,
 	new->flags = mem->flags;
 	new->userspace_addr = mem->userspace_addr;
 	if (mem->flags & KVM_MEM_GUEST_MEMFD) {
-		r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset);
+		if (change == KVM_MR_CREATE) {
+			r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset);
+		} else if (change == KVM_MR_FLAGS_ONLY) {
+			r = kvm_gmem_change_flags(kvm, old, new, mem->guest_memfd,
+						  mem->guest_memfd_offset);
+		}
 		if (r)
 			goto out;
 	}
@@ -2117,7 +2117,7 @@ static int kvm_set_memory_region(struct kvm *kvm,
 	return 0;
 
 out_unbind:
-	if (mem->flags & KVM_MEM_GUEST_MEMFD)
+	if ((mem->flags & KVM_MEM_GUEST_MEMFD) && change == KVM_MR_CREATE)
 		kvm_gmem_unbind(new);
 out:
 	kfree(new);
diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h
index 7510ca915dd1aa..c58dfeb0f3df55 100644
--- a/virt/kvm/kvm_mm.h
+++ b/virt/kvm/kvm_mm.h
@@ -77,6 +77,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);
 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
 		  unsigned int fd, uoff_t offset);
 void kvm_gmem_unbind(struct kvm_memory_slot *slot);
+int kvm_gmem_change_flags(struct kvm *kvm, struct kvm_memory_slot *old,
+			  struct kvm_memory_slot *new, unsigned int fd,
+			  loff_t offset);
 #else
 static inline int kvm_gmem_init(struct module *module)
 {
@@ -95,6 +98,14 @@ static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot)
 {
 	WARN_ON_ONCE(1);
 }
+static inline int kvm_gmem_change_flags(struct kvm *kvm,
+					struct kvm_memory_slot *old,
+					struct kvm_memory_slot *new,
+					unsigned int fd, loff_t offset)
+{
+	WARN_ON_ONCE(1);
+	return -EIO;
+}
 #endif /* CONFIG_KVM_GUEST_MEMFD */
 
 #endif /* __KVM_MM_H__ */

3/2 2026/07/02 17:26 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 9515
							output: 23
							thoughts: 411
						

Thoughts:
**Fuzzing Target Identification: KVM Guest Memfd Dirty Page Logging**

Okay, let's break this down from a fuzzing perspective. My initial thought is to identify whether this patch series introduces any functional changes to the kernel that warrant fuzzing. From what I see, it adds support for dirty page logging specifically for `guest_memfd` backed memslots within KVM. This looks interesting.

The patch introduces `KVM_CAP_GUEST_MEMFD_MMAP_LOG_DIRTY_PAGES`. So this is something new that can be enabled, and that's usually where the bugs hide. Looking at the code modifications, it touches several KVM functions. It seems to have a lot of changes related to `kvm_vm_ioctl_check_extension`, `gmem_abort` (arm64), `kvm_gmem_invalidate_start`, `kvm_gmem_invalidate_end`, `kvm_gmem_release`, `kvm_gmem_error_folio`, `kvm_gmem_bind`, `kvm_set_memory_region`, and others. This indicates a significant shift in how memory is managed within the guest context.

This patch series is allowing the `KVM_MEM_LOG_DIRTY_PAGES` flag to be set for memslots using `KVM_MEM_GUEST_MEMFD` if the architecture supports it. Currently, it seems that `arm64` supports it, provided it's not a protected pKVM VM. This architecture-specific condition is always something to take note of during fuzzing.

My next step is to nail down the necessary configurations. I see that `CONFIG_KVM_GUEST_MEMFD` and `CONFIG_KVM` are required. I'll need to confirm these are indeed the right configuration flags to enable this functionality for fuzzing.

Now, let's identify the key focus symbols to target in the fuzzing harness. I'm focusing on functions related to the core logic, which are: `kvm_gmem_change_flags`, `kvm_gmem_check_no_change`, `kvm_gmem_bind`, `kvm_set_memory_region`, `__kvm_gmem_invalidate_start`, `__kvm_gmem_invalidate_end`, `kvm_gmem_error_folio`, and `kvm_gmem_release`. These look like the entry points for the new feature or significantly modified ones, based on the patch.

I will use `grepper` to confirm that `CONFIG_KVM_GUEST_MEMFD` is the correct config, and I'll see where this is defined and used.



4/2 2026/07/02 17:27 tool grepper
0m Args:
{
  "Expression": "config KVM_GUEST_MEMFD"
}

Results:
{
  "Output": "virt/kvm/Kconfig=103=config KVM_GENERIC_MEMORY_ATTRIBUTES\n--\nvirt/kvm/Kconfig-105-\nvirt/kvm/Kconfig:106:config KVM_GUEST_MEMFD\nvirt/kvm/Kconfig-107-       select XARRAY_MULTI\n"
}

5/2 2026/07/02 17:27 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 10626
							output: 36
							thoughts: 25
						

Thoughts:
Okay, here's my take on those thoughts, framed as an expert's internal monologue:

**Investigating KVM_GUEST_MEMFD Configuration**

Alright, `KVM_GUEST_MEMFD`... that's a config option. Right. Before I dive too deep into its specifics, my first instinct is always to understand its dependencies. It's crucial, especially in kernel configurations, to see if enabling or disabling this feature has cascading effects. Does it hinge on other specific options? Is it tied to a particular hardware architecture, or a specific kernel version? Understanding these dependencies will give me a better understanding of its overall impact and portability. Let's see what the config system tells us.



6/2 2026/07/02 17:27 tool grepper
0m Args:
{
  "Expression": "config KVM_GUEST_MEMFD",
  "PathPrefix": "virt/kvm/Kconfig"
}

Results:
{
  "Output": "virt/kvm/Kconfig=103=config KVM_GENERIC_MEMORY_ATTRIBUTES\n--\nvirt/kvm/Kconfig-105-\nvirt/kvm/Kconfig:106:config KVM_GUEST_MEMFD\nvirt/kvm/Kconfig-107-       select XARRAY_MULTI\n"
}

7/2 2026/07/02 17:27 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 10928
							output: 35
							thoughts: 20
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Analyzing the KVM Configuration Options**

Okay, let's dive in. My immediate focus is on `virt/kvm/Kconfig` around line 106. I'm assuming we're dealing with the core KVM configuration here, probably the top-level options that dictate whether or not KVM itself is built into the kernel, or as a module, and possibly some fundamental architectural choices. At line 106, I expect to encounter the foundational toggles, the base `bool` or `tristate` options that enable or disable KVM support at a very broad level. I'm looking for things like `CONFIG_KVM`, and maybe its dependency on things like processor virtualization extensions (VT-x or AMD-V). I'll also be scanning for how this base configuration is then refined into more granular controls. The goal is to immediately understand the scope of KVM's build configuration, what the initial settings do, and their relationship to various hardware and architecture choices. I am particularly interested in conditional selections.



8/2 2026/07/02 17:27 tool read-file
0m Args:
{
  "File": "virt/kvm/Kconfig",
  "FirstLine": 100,
  "LineCount": 20
}

Results:
{
  "Contents": " 100:\tconfig KVM_MMU_LOCKLESS_AGING\n 101:\t       bool\n 102:\t\n 103:\tconfig KVM_GENERIC_MEMORY_ATTRIBUTES\n 104:\t       bool\n 105:\t\n 106:\tconfig KVM_GUEST_MEMFD\n 107:\t       select XARRAY_MULTI\n 108:\t       bool\n 109:\t\n 110:\tconfig HAVE_KVM_ARCH_GMEM_PREPARE\n 111:\t       bool\n 112:\t       depends on KVM_GUEST_MEMFD\n 113:\t\n 114:\tconfig HAVE_KVM_ARCH_GMEM_INVALIDATE\n 115:\t       bool\n 116:\t       depends on KVM_GUEST_MEMFD\n 117:\t\n 118:\tconfig HAVE_KVM_ARCH_GMEM_POPULATE\n 119:\t       bool\n"
}

9/2 2026/07/02 17:27 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 11485
							output: 23
							thoughts: 30
						

Thoughts:
Here's my take on those thoughts:

**Investigating the KVM_GUEST_MEMFD Configuration Option**

Okay, so there's this `KVM_GUEST_MEMFD` setting, tucked away as a boolean configuration option. It's marked as hidden, which means it's probably not something end-users are meant to directly manipulate, but rather something selected by the architecture. My immediate thought is, "Alright, let's figure out *who* exactly is making use of this thing." I need to understand which architectures consider this option relevant. Diving into the code to find the select statements is the next logical step. From there, I can begin to unpack *why* they need it. I am keen to learn the implications this might have on the virtualisation landscape.



10/2 2026/07/02 17:27 tool grepper
0m Args:
{
  "Expression": "select KVM_GUEST_MEMFD"
}

Results:
{
  "Output": "arch/arm64/kvm/Kconfig=20=menuconfig KVM\n--\narch/arm64/kvm/Kconfig-38-\tselect GUEST_PERF_EVENTS if PERF_EVENTS\narch/arm64/kvm/Kconfig:39:\tselect KVM_GUEST_MEMFD\narch/arm64/kvm/Kconfig-40-\thelp\n--\narch/x86/kvm/Kconfig=20=config KVM_X86\n--\narch/x86/kvm/Kconfig-49-\tselect KVM_WERROR if WERROR\narch/x86/kvm/Kconfig:50:\tselect KVM_GUEST_MEMFD if X86_64\narch/x86/kvm/Kconfig-51-\n"
}

11/2 2026/07/02 17:27 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 11915
							output: 188
							thoughts: 538
						

Thoughts:
**My Assessment of the KVM Guest Memory Management Patch**

Okay, here's what I'm thinking about this patch. From a high level, it looks like this patch introduces changes to KVM's guest memory management, specifically concerning dirty page logging for `guest_memfd` based memslots. On arm64 and x86_64, the relevant configurations are selected by `KVM`. The main configuration option to enable is `KVM`.

Given the core functional changes to memory management, this is *definitely* worth fuzzing. The patch is touching KVM memory region configuration and invalidation, which immediately makes me think of KVM ioctls like `KVM_SET_USER_MEMORY_REGION2` and potential memory invalidation events. It's a prime target.

My initial thought was to check whether `kvm_set_memory_region` was a hot path. But now, after reviewing the other functions, I think it is less interesting. I also took a look at other function, such as `kvm_gmem_change_flags` and `kvm_gmem_check_no_change` which seem specific to this patch. I also saw that `__kvm_gmem_invalidate_start` and `__kvm_gmem_invalidate_end` have also been modified.

So, let's refine the symbol list that should be the focus of the fuzzing efforts. I'll include the following:

*   `kvm_gmem_change_flags`
*   `kvm_gmem_check_no_change`
*   `__kvm_gmem_invalidate_start`
*   `__kvm_gmem_invalidate_end`
*   `kvm_gmem_error_folio`

I've also confirmed that `kvm_gmem_change_flags` is exported to `virt/kvm/kvm_mm.h`, which is important for understanding how it's being used. Similarly, `kvm_gmem_check_no_change` is also exported, which confirms its importance in the broader KVM architecture.

My reasoning boils down to this: the patch fundamentally alters memory region management and invalidation within KVM. This has direct implications for how guests interact with memory and, therefore, is a prime target for vulnerability research. Specifically, ioctl calls related to memory region configuration and anything that can trigger memory invalidation would be primary areas for fuzzing and audit. The configs will be `KVM`.



12/2 2026/07/02 17:27 tool set-results
0m Args:
{
  "EnableConfigs": [
    "KVM"
  ],
  "FocusSymbols": [
    "kvm_gmem_change_flags",
    "kvm_gmem_check_no_change",
    "__kvm_gmem_invalidate_start",
    "__kvm_gmem_invalidate_end",
    "kvm_gmem_error_folio"
  ],
  "Reasoning": "The patch introduces support for dirty page logging for `guest_memfd` backed memslots in KVM. It modifies the KVM memory region configuration logic and the invalidation paths for guest memory. These are core functional changes in KVM that can be reached via KVM ioctls (e.g., `KVM_SET_USER_MEMORY_REGION2`) and memory invalidation events, making them highly relevant for fuzzing.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "KVM"
  ],
  "FocusSymbols": [
    "kvm_gmem_change_flags",
    "kvm_gmem_check_no_change",
    "__kvm_gmem_invalidate_start",
    "__kvm_gmem_invalidate_end",
    "kvm_gmem_error_folio"
  ],
  "Reasoning": "The patch introduces support for dirty page logging for `guest_memfd` backed memslots in KVM. It modifies the KVM memory region configuration logic and the invalidation paths for guest memory. These are core functional changes in KVM that can be reached via KVM ioctls (e.g., `KVM_SET_USER_MEMORY_REGION2`) and memory invalidation events, making them highly relevant for fuzzing.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)