On Fri, 3 Jun 2022, Axel Rasmussen wrote: > 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. > > Fixes: 153132571f02 ("userfaultfd/shmem: support UFFDIO_CONTINUE for shmem") > Cc: stable@xxxxxxxxxxxxxxx > Signed-off-by: Axel Rasmussen <axelrasmussen@xxxxxxxxxx> Acked-by: Hugh Dickins <hughd@xxxxxxxxxx> Yes, that looks like a good improvement, and okay to call it a fix for stable. But note that your original choice of SGP_READ was not wrong: there was no SGP_NOALLOC in v5.14, which arrived in v5.15 (and this is not the first time that it's turned out to be useful since then). Since v5.14-stable reached end-of-life some time ago, marking this patch for stable should not result in any untoward build errors. > --- > mm/userfaultfd.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index 4f4892a5f767..c156f7f5b854 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -246,7 +246,7 @@ 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); > if (ret) > goto out; > if (!page) { > -- > 2.36.1.255.ge46751e96f-goog