Skip to content

Commit dfc4d77

Browse files
rpptakpm00
authored andcommitted
shmem, userfaultfd: use a VMA callback to handle UFFDIO_CONTINUE
When userspace resolves a page fault in a shmem VMA with UFFDIO_CONTINUE it needs to get a folio that already exists in the pagecache backing that VMA. Instead of using shmem_get_folio() for that, add a get_folio_noalloc() method to 'struct vm_uffd_ops' that will return a folio if it exists in the VMA's pagecache at given pgoff. Implement get_folio_noalloc() method for shmem and slightly refactor userfaultfd's mfill_get_vma() and mfill_atomic_pte_continue() to support this new API. Link: https://lore.kernel.org/20260402041156.1377214-9-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Reviewed-by: James Houghton <jthoughton@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Andrei Vagin <avagin@google.com> Cc: Axel Rasmussen <axelrasmussen@google.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: David Hildenbrand (Arm) <david@kernel.org> Cc: Harry Yoo <harry.yoo@oracle.com> Cc: Harry Yoo (Oracle) <harry@kernel.org> Cc: Hugh Dickins <hughd@google.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes (Oracle) <ljs@kernel.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Muchun Song <muchun.song@linux.dev> Cc: Nikita Kalyazin <kalyazin@amazon.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Peter Xu <peterx@redhat.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: David Carlier <devnexen@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 0f48947 commit dfc4d77

3 files changed

Lines changed: 39 additions & 17 deletions

File tree

include/linux/userfaultfd_k.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
8787
struct vm_uffd_ops {
8888
/* Checks if a VMA can support userfaultfd */
8989
bool (*can_userfault)(struct vm_area_struct *vma, vm_flags_t vm_flags);
90+
/*
91+
* Called to resolve UFFDIO_CONTINUE request.
92+
* Should return the folio found at pgoff in the VMA's pagecache if it
93+
* exists or ERR_PTR otherwise.
94+
* The returned folio is locked and with reference held.
95+
*/
96+
struct folio *(*get_folio_noalloc)(struct inode *inode, pgoff_t pgoff);
9097
};
9198

9299
/* A combined operation mode + behavior flags. */

mm/shmem.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3289,13 +3289,26 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd,
32893289
return ret;
32903290
}
32913291

3292+
static struct folio *shmem_get_folio_noalloc(struct inode *inode, pgoff_t pgoff)
3293+
{
3294+
struct folio *folio;
3295+
int err;
3296+
3297+
err = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
3298+
if (err)
3299+
return ERR_PTR(err);
3300+
3301+
return folio;
3302+
}
3303+
32923304
static bool shmem_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags)
32933305
{
32943306
return true;
32953307
}
32963308

32973309
static const struct vm_uffd_ops shmem_uffd_ops = {
3298-
.can_userfault = shmem_can_userfault,
3310+
.can_userfault = shmem_can_userfault,
3311+
.get_folio_noalloc = shmem_get_folio_noalloc,
32993312
};
33003313
#endif /* CONFIG_USERFAULTFD */
33013314

mm/userfaultfd.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static int mfill_get_vma(struct mfill_state *state)
191191
struct userfaultfd_ctx *ctx = state->ctx;
192192
uffd_flags_t flags = state->flags;
193193
struct vm_area_struct *dst_vma;
194+
const struct vm_uffd_ops *ops;
194195
int err;
195196

196197
/*
@@ -232,10 +233,12 @@ static int mfill_get_vma(struct mfill_state *state)
232233
if (is_vm_hugetlb_page(dst_vma))
233234
return 0;
234235

235-
if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
236+
ops = vma_uffd_ops(dst_vma);
237+
if (!ops)
236238
goto out_unlock;
237-
if (!vma_is_shmem(dst_vma) &&
238-
uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE))
239+
240+
if (uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE) &&
241+
!ops->get_folio_noalloc)
239242
goto out_unlock;
240243

241244
return 0;
@@ -575,6 +578,7 @@ static int mfill_atomic_pte_zeropage(struct mfill_state *state)
575578
static int mfill_atomic_pte_continue(struct mfill_state *state)
576579
{
577580
struct vm_area_struct *dst_vma = state->vma;
581+
const struct vm_uffd_ops *ops = vma_uffd_ops(dst_vma);
578582
unsigned long dst_addr = state->dst_addr;
579583
pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
580584
struct inode *inode = file_inode(dst_vma->vm_file);
@@ -584,17 +588,16 @@ static int mfill_atomic_pte_continue(struct mfill_state *state)
584588
struct page *page;
585589
int ret;
586590

587-
ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
588-
/* Our caller expects us to return -EFAULT if we failed to find folio */
589-
if (ret == -ENOENT)
590-
ret = -EFAULT;
591-
if (ret)
592-
goto out;
593-
if (!folio) {
594-
ret = -EFAULT;
595-
goto out;
591+
if (!ops) {
592+
VM_WARN_ONCE(1, "UFFDIO_CONTINUE for unsupported VMA");
593+
return -EOPNOTSUPP;
596594
}
597595

596+
folio = ops->get_folio_noalloc(inode, pgoff);
597+
/* Our caller expects us to return -EFAULT if we failed to find folio */
598+
if (IS_ERR_OR_NULL(folio))
599+
return -EFAULT;
600+
598601
page = folio_file_page(folio, pgoff);
599602
if (PageHWPoison(page)) {
600603
ret = -EIO;
@@ -607,13 +610,12 @@ static int mfill_atomic_pte_continue(struct mfill_state *state)
607610
goto out_release;
608611

609612
folio_unlock(folio);
610-
ret = 0;
611-
out:
612-
return ret;
613+
return 0;
614+
613615
out_release:
614616
folio_unlock(folio);
615617
folio_put(folio);
616-
goto out;
618+
return ret;
617619
}
618620

619621
/* Handles UFFDIO_POISON for all non-hugetlb VMAs. */

0 commit comments

Comments
 (0)