Skip to content

Commit 5543d3c

Browse files
xzpeterakpm00
authored andcommitted
mm/uffd: allow vma to merge as much as possible
We used to not pass in the pgoff correctly when register/unregister uffd regions, it caused incorrect behavior on vma merging and can cause mergeable vmas being separate after ioctls return. For example, when we have: vma1(range 0-9, with uffd), vma2(range 10-19, no uffd) Then someone unregisters uffd on range (5-9), it should logically become: vma1(range 0-4, with uffd), vma2(range 5-19, no uffd) But with current code we'll have: vma1(range 0-4, with uffd), vma3(range 5-9, no uffd), vma2(range 10-19, no uffd) This patch allows such merge to happen correctly before ioctl returns. This behavior seems to have existed since the 1st day of uffd. Since pgoff for vma_merge() is only used to identify the possibility of vma merging, meanwhile here what we did was always passing in a pgoff smaller than what we should, so there should have no other side effect besides not merging it. Let's still tentatively copy stable for this, even though I don't see anything will go wrong besides vma being split (which is mostly not user visible). Link: https://lkml.kernel.org/r/20230517190916.3429499-3-peterx@redhat.com Fixes: 86039bd ("userfaultfd: add new syscall to provide memory externalization") Signed-off-by: Peter Xu <peterx@redhat.com> Reported-by: Lorenzo Stoakes <lstoakes@gmail.com> Acked-by: Lorenzo Stoakes <lstoakes@gmail.com> Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Mike Rapoport (IBM) <rppt@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 270aa01 commit 5543d3c

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

fs/userfaultfd.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
13321332
bool basic_ioctls;
13331333
unsigned long start, end, vma_end;
13341334
struct vma_iterator vmi;
1335+
pgoff_t pgoff;
13351336

13361337
user_uffdio_register = (struct uffdio_register __user *) arg;
13371338

@@ -1484,8 +1485,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
14841485
vma_end = min(end, vma->vm_end);
14851486

14861487
new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
1488+
pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
14871489
prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags,
1488-
vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1490+
vma->anon_vma, vma->vm_file, pgoff,
14891491
vma_policy(vma),
14901492
((struct vm_userfaultfd_ctx){ ctx }),
14911493
anon_vma_name(vma));
@@ -1565,6 +1567,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
15651567
unsigned long start, end, vma_end;
15661568
const void __user *buf = (void __user *)arg;
15671569
struct vma_iterator vmi;
1570+
pgoff_t pgoff;
15681571

15691572
ret = -EFAULT;
15701573
if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
@@ -1667,8 +1670,9 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
16671670
uffd_wp_range(vma, start, vma_end - start, false);
16681671

16691672
new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS;
1673+
pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
16701674
prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags,
1671-
vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1675+
vma->anon_vma, vma->vm_file, pgoff,
16721676
vma_policy(vma),
16731677
NULL_VM_UFFD_CTX, anon_vma_name(vma));
16741678
if (prev) {

0 commit comments

Comments
 (0)