diff mbox series

[v2,3/3] mm/gup: Remove try_get_page(), call try_get_compound_head() directly

Message ID 20210811070542.3403116-4-jhubbard@nvidia.com (mailing list archive)
State New
Headers show
Series A few gup refactorings and documentation updates | expand

Commit Message

John Hubbard Aug. 11, 2021, 7:05 a.m. UTC
try_get_page() is very similar to try_get_compound_head(), and in fact
try_get_page() has fallen a little behind in terms of maintenance:
try_get_compound_head() handles speculative page references more
thoroughly.

There are only two try_get_page() callsites, so just call
try_get_compound_head() directly from those, and remove try_get_page()
entirely.

Also, seeing as how this changes try_get_compound_head() into a
non-static function, provide some kerneldoc documentation for it.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
 arch/s390/mm/fault.c |  2 +-
 fs/pipe.c            |  2 +-
 include/linux/mm.h   | 10 +---------
 mm/gup.c             | 21 +++++++++++++++++----
 4 files changed, 20 insertions(+), 15 deletions(-)

Comments

William Kucharski Aug. 11, 2021, 8:35 p.m. UTC | #1
I agree that try_get_page() should probably be removed entirely; is there
a reason you didn't in v2 of the patch?

I'm also curious why you changed try_get_compound_head() into a routine
from an inline.

If you want to retain try_get_page() it should be an inline as well, especially
in its current implementation.

    William Kucharski

> On Aug 11, 2021, at 1:05 AM, John Hubbard <jhubbard@nvidia.com> wrote:
> 
> try_get_page() is very similar to try_get_compound_head(), and in fact
> try_get_page() has fallen a little behind in terms of maintenance:
> try_get_compound_head() handles speculative page references more
> thoroughly.
> 
> There are only two try_get_page() callsites, so just call
> try_get_compound_head() directly from those, and remove try_get_page()
> entirely.
> 
> Also, seeing as how this changes try_get_compound_head() into a
> non-static function, provide some kerneldoc documentation for it.
> 
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Matthew Wilcox <willy@infradead.org>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
> ---
> arch/s390/mm/fault.c |  2 +-
> fs/pipe.c            |  2 +-
> include/linux/mm.h   | 10 +---------
> mm/gup.c             | 21 +++++++++++++++++----
> 4 files changed, 20 insertions(+), 15 deletions(-)
> 
> diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c
> index 212632d57db9..fe1d2c1dbe3b 100644
> --- a/arch/s390/mm/fault.c
> +++ b/arch/s390/mm/fault.c
> @@ -822,7 +822,7 @@ void do_secure_storage_access(struct pt_regs *regs)
> 		break;
> 	case KERNEL_FAULT:
> 		page = phys_to_page(addr);
> -		if (unlikely(!try_get_page(page)))
> +		if (unlikely(try_get_compound_head(page, 1) == NULL))
> 			break;
> 		rc = arch_make_page_accessible(page);
> 		put_page(page);
> diff --git a/fs/pipe.c b/fs/pipe.c
> index 8e6ef62aeb1c..06ba9df37410 100644
> --- a/fs/pipe.c
> +++ b/fs/pipe.c
> @@ -191,7 +191,7 @@ EXPORT_SYMBOL(generic_pipe_buf_try_steal);
>  */
> bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
> {
> -	return try_get_page(buf->page);
> +	return try_get_compound_head(buf->page, 1) != NULL;
> }
> EXPORT_SYMBOL(generic_pipe_buf_get);
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index ce8fc0fd6d6e..cd00d1222235 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1207,15 +1207,7 @@ bool __must_check try_grab_page(struct page *page, unsigned int flags);
> __maybe_unused struct page *try_grab_compound_head(struct page *page, int refs,
> 						   unsigned int flags);
> 
> -
> -static inline __must_check bool try_get_page(struct page *page)
> -{
> -	page = compound_head(page);
> -	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
> -		return false;
> -	page_ref_inc(page);
> -	return true;
> -}
> +struct page *try_get_compound_head(struct page *page, int refs);
> 
> /**
>  * folio_put - Decrement the reference count on a folio.
> diff --git a/mm/gup.c b/mm/gup.c
> index 64798d6b5043..c2d19d370c99 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -62,11 +62,24 @@ static void put_page_refs(struct page *page, int refs)
> 	put_page(page);
> }
> 
> -/*
> - * Return the compound head page with ref appropriately incremented,
> - * or NULL if that failed.
> +/**
> + * try_get_compound_head() - return the compound head page with refcount
> + * appropriately incremented, or NULL if that failed.
> + *
> + * This handles potential refcount overflow correctly. It also works correctly
> + * for various lockless get_user_pages()-related callers, due to the use of
> + * page_cache_add_speculative().
> + *
> + * Even though the name includes "compound_head", this function is still
> + * appropriate for callers that have a non-compound @page to get.
> + *
> + * @page:  pointer to page to be gotten
> + * @refs:  the value to add to the page's refcount
> + *
> + * Return: head page (with refcount appropriately incremented) for success, or
> + * NULL upon failure.
>  */
> -static inline struct page *try_get_compound_head(struct page *page, int refs)
> +struct page *try_get_compound_head(struct page *page, int refs)
> {
> 	struct page *head = compound_head(page);
> 
> -- 
> 2.32.0
> 
>
John Hubbard Aug. 11, 2021, 10:07 p.m. UTC | #2
On 8/11/21 1:35 PM, William Kucharski wrote:
> I agree that try_get_page() should probably be removed entirely; is there
> a reason you didn't in v2 of the patch?

Hi William,

This patch *does* remove try_get_page() entirely! Look below. I'll reply
inline, below, to show where that happens.

> 
> I'm also curious why you changed try_get_compound_head() into a routine
> from an inline.

It was part of the change to make it available to callers outside of
gup.c. try_get_compound_head() is slightly messy and doesn't like to
live in mm.h, because it calls page_cache_add_speculative(), which lives
in linux-pagemap.h, which, in turn, has its own set of different headers
that it pulls in.

So, leaving it in gup.c, and exposing it to the other callers as a
non-static function, seemed appropriate here.

> 
> If you want to retain try_get_page() it should be an inline as well, especially
> in its current implementation.
> 
>      William Kucharski
> 
>> On Aug 11, 2021, at 1:05 AM, John Hubbard <jhubbard@nvidia.com> wrote:
>>
>> try_get_page() is very similar to try_get_compound_head(), and in fact
>> try_get_page() has fallen a little behind in terms of maintenance:
>> try_get_compound_head() handles speculative page references more
>> thoroughly.
>>
>> There are only two try_get_page() callsites, so just call
>> try_get_compound_head() directly from those, and remove try_get_page()
>> entirely.
>>
>> Also, seeing as how this changes try_get_compound_head() into a
>> non-static function, provide some kerneldoc documentation for it.
>>
>> Cc: Christoph Hellwig <hch@lst.de>
>> Cc: Matthew Wilcox <willy@infradead.org>
>> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
>> ---
>> arch/s390/mm/fault.c |  2 +-
>> fs/pipe.c            |  2 +-
>> include/linux/mm.h   | 10 +---------
>> mm/gup.c             | 21 +++++++++++++++++----
>> 4 files changed, 20 insertions(+), 15 deletions(-)
>>
>> diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c
>> index 212632d57db9..fe1d2c1dbe3b 100644
>> --- a/arch/s390/mm/fault.c
>> +++ b/arch/s390/mm/fault.c
>> @@ -822,7 +822,7 @@ void do_secure_storage_access(struct pt_regs *regs)
>> 		break;
>> 	case KERNEL_FAULT:
>> 		page = phys_to_page(addr);
>> -		if (unlikely(!try_get_page(page)))
>> +		if (unlikely(try_get_compound_head(page, 1) == NULL))
>> 			break;
>> 		rc = arch_make_page_accessible(page);
>> 		put_page(page);
>> diff --git a/fs/pipe.c b/fs/pipe.c
>> index 8e6ef62aeb1c..06ba9df37410 100644
>> --- a/fs/pipe.c
>> +++ b/fs/pipe.c
>> @@ -191,7 +191,7 @@ EXPORT_SYMBOL(generic_pipe_buf_try_steal);
>>   */
>> bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
>> {
>> -	return try_get_page(buf->page);
>> +	return try_get_compound_head(buf->page, 1) != NULL;
>> }
>> EXPORT_SYMBOL(generic_pipe_buf_get);
>>
>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>> index ce8fc0fd6d6e..cd00d1222235 100644
>> --- a/include/linux/mm.h
>> +++ b/include/linux/mm.h
>> @@ -1207,15 +1207,7 @@ bool __must_check try_grab_page(struct page *page, unsigned int flags);
>> __maybe_unused struct page *try_grab_compound_head(struct page *page, int refs,
>> 						   unsigned int flags);
>>
>> -
>> -static inline __must_check bool try_get_page(struct page *page)
>> -{
>> -	page = compound_head(page);
>> -	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
>> -		return false;
>> -	page_ref_inc(page);
>> -	return true;
>> -}

This is where try_get_page() is removed entirely.

thanks,
William Kucharski Aug. 12, 2021, 2:17 a.m. UTC | #3
> On Aug 11, 2021, at 4:07 PM, John Hubbard <jhubbard@nvidia.com> wrote:
> 
> On 8/11/21 1:35 PM, William Kucharski wrote:
>> I agree that try_get_page() should probably be removed entirely; is there
>> a reason you didn't in v2 of the patch?
> 
> Hi William,
> 
> This patch *does* remove try_get_page() entirely! Look below. I'll reply
> inline, below, to show where that happens.

Ah, my bad.  I was conflating it with try_grab_page() in patch 2/3, which
also seems like it should be an inline, but given your explanation re:
try_get_compound_head() it makes perfect sense.
 
For the series:

Reviewed-by: William Kucharski <william.kucharski@oracle.com>
Christoph Hellwig Aug. 12, 2021, 9:22 a.m. UTC | #4
On Wed, Aug 11, 2021 at 12:05:42AM -0700, John Hubbard wrote:
> -		if (unlikely(!try_get_page(page)))
> +		if (unlikely(try_get_compound_head(page, 1) == NULL))

Why not a simple ! instead of the == NULL?

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
John Hubbard Aug. 12, 2021, 6:19 p.m. UTC | #5
On 8/12/21 2:22 AM, Christoph Hellwig wrote:
> On Wed, Aug 11, 2021 at 12:05:42AM -0700, John Hubbard wrote:
>> -		if (unlikely(!try_get_page(page)))
>> +		if (unlikely(try_get_compound_head(page, 1) == NULL))
> 
> Why not a simple ! instead of the == NULL?

I'll fix those up and post a v4.

> 
> Otherwise looks good:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 

Thanks for the reviews!


thanks,
diff mbox series

Patch

diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c
index 212632d57db9..fe1d2c1dbe3b 100644
--- a/arch/s390/mm/fault.c
+++ b/arch/s390/mm/fault.c
@@ -822,7 +822,7 @@  void do_secure_storage_access(struct pt_regs *regs)
 		break;
 	case KERNEL_FAULT:
 		page = phys_to_page(addr);
-		if (unlikely(!try_get_page(page)))
+		if (unlikely(try_get_compound_head(page, 1) == NULL))
 			break;
 		rc = arch_make_page_accessible(page);
 		put_page(page);
diff --git a/fs/pipe.c b/fs/pipe.c
index 8e6ef62aeb1c..06ba9df37410 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -191,7 +191,7 @@  EXPORT_SYMBOL(generic_pipe_buf_try_steal);
  */
 bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
 {
-	return try_get_page(buf->page);
+	return try_get_compound_head(buf->page, 1) != NULL;
 }
 EXPORT_SYMBOL(generic_pipe_buf_get);
 
diff --git a/include/linux/mm.h b/include/linux/mm.h
index ce8fc0fd6d6e..cd00d1222235 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1207,15 +1207,7 @@  bool __must_check try_grab_page(struct page *page, unsigned int flags);
 __maybe_unused struct page *try_grab_compound_head(struct page *page, int refs,
 						   unsigned int flags);
 
-
-static inline __must_check bool try_get_page(struct page *page)
-{
-	page = compound_head(page);
-	if (WARN_ON_ONCE(page_ref_count(page) <= 0))
-		return false;
-	page_ref_inc(page);
-	return true;
-}
+struct page *try_get_compound_head(struct page *page, int refs);
 
 /**
  * folio_put - Decrement the reference count on a folio.
diff --git a/mm/gup.c b/mm/gup.c
index 64798d6b5043..c2d19d370c99 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -62,11 +62,24 @@  static void put_page_refs(struct page *page, int refs)
 	put_page(page);
 }
 
-/*
- * Return the compound head page with ref appropriately incremented,
- * or NULL if that failed.
+/**
+ * try_get_compound_head() - return the compound head page with refcount
+ * appropriately incremented, or NULL if that failed.
+ *
+ * This handles potential refcount overflow correctly. It also works correctly
+ * for various lockless get_user_pages()-related callers, due to the use of
+ * page_cache_add_speculative().
+ *
+ * Even though the name includes "compound_head", this function is still
+ * appropriate for callers that have a non-compound @page to get.
+ *
+ * @page:  pointer to page to be gotten
+ * @refs:  the value to add to the page's refcount
+ *
+ * Return: head page (with refcount appropriately incremented) for success, or
+ * NULL upon failure.
  */
-static inline struct page *try_get_compound_head(struct page *page, int refs)
+struct page *try_get_compound_head(struct page *page, int refs)
 {
 	struct page *head = compound_head(page);