mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
Subsystems (3)
PATCH v2 1/1 mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
When fallocate() is used on a shmem file, the pages we allocate can end up with !PageUptodate.
Since UFFDIO_CONTINUE tries to find the existing page the user wants to map with SGP_READ, we would fail to find such a page, since shmem_getpage_gfp returns with a "NULL" pagep for SGP_READ if it discovers !PageUptodate. As a result, UFFDIO_CONTINUE returns -EFAULT, as it would do if the page wasn't found in the page cache at all.
This isn't the intended behavior. UFFDIO_CONTINUE is just trying to find if a page exists, and doesn't care whether it still needs to be cleared or not. So, instead of SGP_READ, pass in SGP_NOALLOC. This is the same, except for one critical difference: in the !PageUptodate case, SGP_NOALLOC will clear the page and then return it. With this change, UFFDIO_CONTINUE works properly (succeeds) on a shmem file which has been fallocated, but otherwise not modified.
--- mm/userfaultfd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/userfaultfd.c +4 -1
@@ -246,7 +246,10 @@ static int mcontinue_atomic_pte(struct mm_struct *dst_mm,struct page *page;int ret; - ret =shmem_getpage (inode, pgoff, &page,SGP_READ ); + ret =shmem_getpage (inode, pgoff, &page,SGP_NOALLOC ); +/* Our caller expects us to return -EFAULT if we failed to find page. */ +if (ret == -ENOENT ) + ret = -EFAULT ;if (ret) goto out;if (!page) {
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Quoted 29 lines · Axel Rasmussen
When
fallocate()is used on a shmem file, the pages we allocate can end up with !PageUptodate.Since
UFFDIO_CONTINUEtries to find the existing page the user wants to map withSGP_READ, we would fail to find such a page, sinceshmem_getpage_gfpreturns with a "NULL" pagep forSGP_READif it discovers !PageUptodate. As a result,UFFDIO_CONTINUEreturns-EFAULT, as it would do if the page wasn't found in the page cache at all.This isn't the intended behavior.
UFFDIO_CONTINUEis just trying to find if a page exists, and doesn't care whether it still needs to be cleared or not. So, instead ofSGP_READ, pass inSGP_NOALLOC. This is the same, except for one critical difference: in the !PageUptodate case,SGP_NOALLOCwill clear the page and then return it. With this change,UFFDIO_CONTINUEworks properly (succeeds) on a shmem file which has been fallocated, but otherwise not modified.Fixes 153132571f02 ("userfaultfd/shmem: support UFFDIO_CONTINUE for shmem")Signed-off-by Axel Rasmussen <[email protected]>--- mm/userfaultfd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/userfaultfd.c +0
-0
mm/userfaultfd.c static int mcontinue_atomic_pte(struct mm_struct *dst_mm, · line 252
Quoted 8 lines
mm/userfaultfd.c +4
-1@@ -246,7 +246,10 @@ static int mcontinue_atomic_pte(struct mm_struct *dst_mm,struct page *page;int ret; - ret =shmem_getpage (inode, pgoff, &page,SGP_READ ); + ret =shmem_getpage (inode, pgoff, &page,SGP_NOALLOC ); +/* Our caller expects us to return -EFAULT if we failed to find page. */ +if (ret == -ENOENT ) + ret = -EFAULT ;
I checked shmem_getpage_gfp() and it only returns -ENOENT in one place where is the missing case for SGP_NOALLOC, then this looks correct to me.
Thanks,
Signature · 1 line
Peter Xu