linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
@ 2022-06-03 20:57 Axel Rasmussen
  2022-06-05  1:37 ` Hugh Dickins
  2022-06-09 21:43 ` Peter Xu
  0 siblings, 2 replies; 4+ messages in thread
From: Axel Rasmussen @ 2022-06-03 20:57 UTC (permalink / raw)
  To: Andrew Morton, Peter Xu, Hugh Dickins
  Cc: Axel Rasmussen, linux-mm, linux-kernel, stable

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@vger.kernel.org
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
  2022-06-03 20:57 [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages Axel Rasmussen
@ 2022-06-05  1:37 ` Hugh Dickins
  2022-06-09 21:43 ` Peter Xu
  1 sibling, 0 replies; 4+ messages in thread
From: Hugh Dickins @ 2022-06-05  1:37 UTC (permalink / raw)
  To: Axel Rasmussen
  Cc: Andrew Morton, Peter Xu, Hugh Dickins, linux-mm, linux-kernel

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@vger.kernel.org
> Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>

Acked-by: Hugh Dickins <hughd@google.com>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
  2022-06-03 20:57 [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages Axel Rasmussen
  2022-06-05  1:37 ` Hugh Dickins
@ 2022-06-09 21:43 ` Peter Xu
  2022-06-10 17:31   ` Axel Rasmussen
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Xu @ 2022-06-09 21:43 UTC (permalink / raw)
  To: Axel Rasmussen
  Cc: Andrew Morton, Hugh Dickins, linux-mm, linux-kernel, stable

Hi, Axel,

Sorry to read this late.

On Fri, Jun 03, 2022 at 01:57:41PM -0700, 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@vger.kernel.org
> Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
> ---
>  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) {

It all looks sane if the page is !uptodate as you described.  Though I've a
question on what'll happen if the page is actually missing rather than just
!PageUptodate().

My reading is previously it'll keep returning 0 on shmem_getpage_gfp() for
both cases, but now for the missing page shmem_getpage_gfp() will return
-ENOENT instead.

This reminded me on whether this will errornously let __mcopy_atomic() go
into the special path to copy the page without mmap lock, please see this
commit:

b6ebaedb4cb1 ("userfaultfd: avoid mmap_sem read recursion in mcopy_atomic", 2015-09-04)

Would that be a problem?  Or could I read it wrong?

This also reminded me that whether we'd better need some protection in the
-ENOENT handling in __mcopy_atomic() to be always safe.

Thanks,

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages
  2022-06-09 21:43 ` Peter Xu
@ 2022-06-10 17:31   ` Axel Rasmussen
  0 siblings, 0 replies; 4+ messages in thread
From: Axel Rasmussen @ 2022-06-10 17:31 UTC (permalink / raw)
  To: Peter Xu; +Cc: Andrew Morton, Hugh Dickins, Linux MM, LKML, stable

Ah, thanks for pointing this out Peter, it is definitely something I missed.

You're right that elsewhere in mm/userfaultfd.c we use -ENOENT for
some special case. I think for mcontinue_atomic_pte, we don't want to
change the status quo - if we fail to lookup an existing page, we
should just return -EFAULT just like we were doing before. We
certainly shouldn't return -ENOENT, as that causes us to take a wrong,
unrelated code path a couple of callers up, as you mentioned.

I'll send a v2 with this small modification.

On Thu, Jun 9, 2022 at 2:44 PM Peter Xu <peterx@redhat.com> wrote:
>
> Hi, Axel,
>
> Sorry to read this late.
>
> On Fri, Jun 03, 2022 at 01:57:41PM -0700, 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@vger.kernel.org
> > Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
> > ---
> >  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) {
>
> It all looks sane if the page is !uptodate as you described.  Though I've a
> question on what'll happen if the page is actually missing rather than just
> !PageUptodate().
>
> My reading is previously it'll keep returning 0 on shmem_getpage_gfp() for
> both cases, but now for the missing page shmem_getpage_gfp() will return
> -ENOENT instead.
>
> This reminded me on whether this will errornously let __mcopy_atomic() go
> into the special path to copy the page without mmap lock, please see this
> commit:
>
> b6ebaedb4cb1 ("userfaultfd: avoid mmap_sem read recursion in mcopy_atomic", 2015-09-04)
>
> Would that be a problem?  Or could I read it wrong?
>
> This also reminded me that whether we'd better need some protection in the
> -ENOENT handling in __mcopy_atomic() to be always safe.
>
> Thanks,
>
> --
> Peter Xu
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-10 17:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03 20:57 [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages Axel Rasmussen
2022-06-05  1:37 ` Hugh Dickins
2022-06-09 21:43 ` Peter Xu
2022-06-10 17:31   ` Axel Rasmussen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).