diff mbox series

[v10,07/33] mm: Add folio_get

Message ID 20210511214735.1836149-8-willy@infradead.org (mailing list archive)
State New
Headers show
Series Memory folios | expand

Commit Message

Matthew Wilcox May 11, 2021, 9:47 p.m. UTC
If we know we have a folio, we can call folio_get() instead
of get_page() and save the overhead of calling compound_head().

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Jeff Layton <jlayton@kernel.org>
---
 include/linux/mm.h | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

Comments

Vlastimil Babka May 14, 2021, 11:56 a.m. UTC | #1
Nitpick: function names in subject should IMHO also end with (). But not a
reason for resend all patches that don't...

On 5/11/21 11:47 PM, Matthew Wilcox (Oracle) wrote:
> If we know we have a folio, we can call folio_get() instead
> of get_page() and save the overhead of calling compound_head().
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> Reviewed-by: Zi Yan <ziy@nvidia.com>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Acked-by: Jeff Layton <jlayton@kernel.org>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  include/linux/mm.h | 26 +++++++++++++++++---------
>  1 file changed, 17 insertions(+), 9 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 610948f0cb43..feb4645ef4f2 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1219,18 +1219,26 @@ static inline bool is_pci_p2pdma_page(const struct page *page)
>  }
>  
>  /* 127: arbitrary random number, small enough to assemble well */
> -#define page_ref_zero_or_close_to_overflow(page) \
> -	((unsigned int) page_ref_count(page) + 127u <= 127u)
> +#define folio_ref_zero_or_close_to_overflow(folio) \
> +	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
> +
> +/**
> + * folio_get - Increment the reference count on a folio.
> + * @folio: The folio.
> + *
> + * Context: May be called in any context, as long as you know that
> + * you have a refcount on the folio.  If you do not already have one,
> + * folio_try_get() may be the right interface for you to use.
> + */
> +static inline void folio_get(struct folio *folio)
> +{
> +	VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio);
> +	folio_ref_inc(folio);
> +}
>  
>  static inline void get_page(struct page *page)
>  {
> -	page = compound_head(page);
> -	/*
> -	 * Getting a normal page or the head of a compound page
> -	 * requires to already have an elevated page->_refcount.
> -	 */
> -	VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
> -	page_ref_inc(page);
> +	folio_get(page_folio(page));
>  }
>  
>  bool __must_check try_grab_page(struct page *page, unsigned int flags);
>
Matthew Wilcox May 14, 2021, 2:24 p.m. UTC | #2
On Fri, May 14, 2021 at 01:56:46PM +0200, Vlastimil Babka wrote:
> Nitpick: function names in subject should IMHO also end with (). But not a
> reason for resend all patches that don't...

Hm, I thought it was preferred to not do that.  I can fix it
easily enough when I go through and add the R-b.
Vlastimil Babka May 14, 2021, 3:39 p.m. UTC | #3
On 5/14/21 4:24 PM, Matthew Wilcox wrote:
> On Fri, May 14, 2021 at 01:56:46PM +0200, Vlastimil Babka wrote:
>> Nitpick: function names in subject should IMHO also end with (). But not a
>> reason for resend all patches that don't...
> 
> Hm, I thought it was preferred to not do that.

Hm, no idea if there's a concensus on that, actually.

> I can fix it
> easily enough when I go through and add the R-b.

If I was right...
Christoph Hellwig May 27, 2021, 8:10 a.m. UTC | #4
On Fri, May 14, 2021 at 03:24:26PM +0100, Matthew Wilcox wrote:
> On Fri, May 14, 2021 at 01:56:46PM +0200, Vlastimil Babka wrote:
> > Nitpick: function names in subject should IMHO also end with (). But not a
> > reason for resend all patches that don't...
> 
> Hm, I thought it was preferred to not do that.  I can fix it
> easily enough when I go through and add the R-b.

I hate the pointless ().  Some maintainers insist on it.   No matter
what you do you'll make some folks happy and others not.
Andrew Morton May 27, 2021, 10:53 p.m. UTC | #5
On Thu, 27 May 2021 09:10:31 +0100 Christoph Hellwig <hch@infradead.org> wrote:

> On Fri, May 14, 2021 at 03:24:26PM +0100, Matthew Wilcox wrote:
> > On Fri, May 14, 2021 at 01:56:46PM +0200, Vlastimil Babka wrote:
> > > Nitpick: function names in subject should IMHO also end with (). But not a
> > > reason for resend all patches that don't...
> > 
> > Hm, I thought it was preferred to not do that.  I can fix it
> > easily enough when I go through and add the R-b.
> 
> I hate the pointless ().  Some maintainers insist on it.   No matter
> what you do you'll make some folks happy and others not.

I prefer it.  It succinctly says "this identifier is a function" which
is useful info.

I get many changelogs saying "the foo function" or "the function foo". 
"foo()" is better.
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 610948f0cb43..feb4645ef4f2 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1219,18 +1219,26 @@  static inline bool is_pci_p2pdma_page(const struct page *page)
 }
 
 /* 127: arbitrary random number, small enough to assemble well */
-#define page_ref_zero_or_close_to_overflow(page) \
-	((unsigned int) page_ref_count(page) + 127u <= 127u)
+#define folio_ref_zero_or_close_to_overflow(folio) \
+	((unsigned int) folio_ref_count(folio) + 127u <= 127u)
+
+/**
+ * folio_get - Increment the reference count on a folio.
+ * @folio: The folio.
+ *
+ * Context: May be called in any context, as long as you know that
+ * you have a refcount on the folio.  If you do not already have one,
+ * folio_try_get() may be the right interface for you to use.
+ */
+static inline void folio_get(struct folio *folio)
+{
+	VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio);
+	folio_ref_inc(folio);
+}
 
 static inline void get_page(struct page *page)
 {
-	page = compound_head(page);
-	/*
-	 * Getting a normal page or the head of a compound page
-	 * requires to already have an elevated page->_refcount.
-	 */
-	VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
-	page_ref_inc(page);
+	folio_get(page_folio(page));
 }
 
 bool __must_check try_grab_page(struct page *page, unsigned int flags);