From: Natalie Vock <nat@pixelcluster.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
linux-mm@kvack.org, dri-devel@lists.freedesktop.org
Subject: userfaultfd wp-async support for (GPU) special mappings?
Date: Wed, 12 Aug 2026 23:01:55 +0200 [thread overview]
Message-ID: <d5ea3344-24e7-4834-b743-4dec812ada70@pixelcluster.dev> (raw)
Hi all,
lately I've been investigating some ways to efficiently query for
whether particular memory has been written to or not. The functionality
I'm looking for is pretty much exactly what userfaultfd's wp-async mode
exposes, but with a twist: The memory I'm interested in is GPU memory
mapped into users' address spaces.
The broader context here is writing a "capture/replay" tool for the
Vulkan graphics API: The tool lives in a .so that is injected into some
app at runtime (LD_PRELOAD style). All graphics API calls (rendering
commands etc.) are then intercepted, making a copy of any call
parameters and writing ("capturing") them to disk. Later, these calls
can be read back from the file and "replayed", reproducing the exact
same sequence of rendering commands again (hopefully leading to the same
rendering output, too).
However, one of the commands is a simple wrapper over mmap(), where the
input is a GPU resource and the output is a mapped pointer for free use
by applications. To correctly reproduce the behavior of apps using this
command, the capture/replay tool needs some side-channel to know which
parts of this mapped memory have been overwritten by the CPU, so that it
can perform the same modifications when replaying API calls.
The only part I'm interested here are writes done by the CPU. The GPU
may also write to the mapped memory itself, but there's no need to track
where it wrote.
userfaultfd wp-async tracking would be a pretty great match for this, if
only it could be made to work with GPU mappings, too. I've been hacking
around in the kernel and I did get my use case working fairly well with
only a few modifications:
First, I mostly-reverted commit 3c58f641e81 ("userfaultfd: prevent
registration of special VMAs") for rather obvious reasons :)
Then, all I had to change to get things to work was add handling for
encountering uffd-wp marker PTEs on a read fault inside insert_pfn(),
and allow the PM_SCAN ioctl for /proc/<pid>/pagemap to process vmas
marked with VM_PFNMAP if ioctl only does uffd wp-async bookkeeping.
I included a complete diff of these changes at the end of this email,
but their quality is very much proof-of-concept only; it's not remotely
in an upstreamable state.
Is this something upstream would consider supporting at all? I'm not
familiar enough with memory management to judge whether there are
fundamental pitfalls making this whole idea impossible (but for what
it's worth, it worked really well on every program I tried
capturing/replaying :P)
Best,
Natalie
---
Here's the diff for my dirty hacks making uffd work with GPU mappings,
based on commit f5098b6bae ("Linux 7.2-rc5"):
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index d32408f7cd5ed..7ac75ac0a7794 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -2426,6 +2426,19 @@ struct pagemap_scan_private {
struct page_region __user *vec_out;
};
+static bool pagemap_exclusively_mark_wp(struct pagemap_scan_private *p)
+{
+ return (p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out;
+}
+
+static bool
+pagemap_exclusively_mark_and_query_wp(struct pagemap_scan_private *p)
+{
+ return !p->arg.category_anyof_mask && !p->arg.category_inverted &&
+ p->arg.category_mask == PAGE_IS_WRITTEN &&
+ p->arg.return_mask == PAGE_IS_WRITTEN;
+}
+
static unsigned long pagemap_page_category(struct pagemap_scan_private *p,
struct vm_area_struct *vma,
unsigned long addr, pte_t pte)
@@ -2689,7 +2702,9 @@ static int pagemap_scan_test_walk(unsigned long
start, unsigned long end,
*/
}
- if (vma->vm_flags & VM_PFNMAP)
+ if ((vma->vm_flags & VM_PFNMAP) &&
+ !(pagemap_exclusively_mark_wp(p) ||
+ pagemap_exclusively_mark_and_query_wp(p)))
return 1;
if (wp_allowed)
@@ -2844,7 +2859,7 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd,
unsigned long start,
lazy_mmu_mode_enable();
- if ((p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out) {
+ if (pagemap_exclusively_mark_wp(p)) {
/* Fast path for performing exclusive WP */
for (addr = start; addr != end; pte++, addr += PAGE_SIZE) {
pte_t ptent = ptep_get(pte);
@@ -2860,9 +2875,7 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd,
unsigned long start,
goto flush_and_return;
}
- if (!p->arg.category_anyof_mask && !p->arg.category_inverted &&
- p->arg.category_mask == PAGE_IS_WRITTEN &&
- p->arg.return_mask == PAGE_IS_WRITTEN) {
+ if (pagemap_exclusively_mark_and_query_wp(p)) {
for (addr = start; addr < end; pte++, addr += PAGE_SIZE) {
unsigned long next = addr + PAGE_SIZE;
pte_t ptent = ptep_get(pte);
diff --git a/mm/memory.c b/mm/memory.c
index ff338c2abe923..a06a31f7f45cc 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2698,6 +2698,10 @@ static vm_fault_t insert_pfn(struct
vm_area_struct *vma, unsigned long addr,
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
if (ptep_set_access_flags(vma, addr, pte, entry, 1))
update_mmu_cache(vma, addr, pte);
+ } else if (pte_uffd_wp(entry)) {
+ entry = pte_mkspecial(pfn_pte(pfn, prot));
+ entry = pte_mkuffd_wp(entry);
+ goto out_set_pte;
}
goto out_unlock;
}
@@ -2710,6 +2714,7 @@ static vm_fault_t insert_pfn(struct vm_area_struct
*vma, unsigned long addr,
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
}
+out_set_pte:
set_pte_at(mm, addr, pte, entry);
update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index c3adedaaf7d54..aa1553ec5a3bb 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2114,8 +2114,8 @@ static bool vma_can_userfault(struct
vm_area_struct *vma, vm_flags_t vm_flags,
if (vma->vm_flags & (VM_DROPPABLE | VM_SHADOW_STACK))
return false;
- if (!is_vm_hugetlb_page(vma) && (vma->vm_flags & VM_SPECIAL))
- return false;
+ //if (!is_vm_hugetlb_page(vma) && (vma->vm_flags & VM_SPECIAL))
+ // return false;
vm_flags &= __VM_UFFD_FLAGS;
--
2.55.0
reply other threads:[~2026-08-12 21:02 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d5ea3344-24e7-4834-b743-4dec812ada70@pixelcluster.dev \
--to=nat@pixelcluster.dev \
--cc=akpm@linux-foundation.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-mm@kvack.org \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox