diff mbox series

[RFC,3/4] perf: Use vmalloc_to_folio()

Message ID 20230821202016.2910321-4-willy@infradead.org (mailing list archive)
State New
Headers show
Series Convert perf ringbuffer to folios | expand

Commit Message

Matthew Wilcox Aug. 21, 2023, 8:20 p.m. UTC
Eliminate a use of page->mapping by using vmalloc_to_folio() instead.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/mm.h          | 5 +++++
 kernel/events/ring_buffer.c | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

Comments

Zhu Yanjun Aug. 22, 2023, 7:54 a.m. UTC | #1
在 2023/8/22 4:20, Matthew Wilcox (Oracle) 写道:
> Eliminate a use of page->mapping by using vmalloc_to_folio() instead.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>   include/linux/mm.h          | 5 +++++
>   kernel/events/ring_buffer.c | 4 ++--
>   2 files changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 840bae5f23b6..7d84a2843193 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1134,6 +1134,11 @@ int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
>   struct page *vmalloc_to_page(const void *addr);
>   unsigned long vmalloc_to_pfn(const void *addr);
>   
> +static inline struct folio *vmalloc_to_folio(const void *addr)
> +{
> +	return page_folio(vmalloc_to_page(addr));
> +}

Thanks a lot. This function is very wonderful. It is also what I need.

Zhu Yanjun

> +
>   /*
>    * Determine if an address is within the vmalloc range
>    *
> diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
> index c73add132618..56939dc3bf33 100644
> --- a/kernel/events/ring_buffer.c
> +++ b/kernel/events/ring_buffer.c
> @@ -873,9 +873,9 @@ __perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff)
>   
>   static void perf_mmap_unmark_page(void *addr)
>   {
> -	struct page *page = vmalloc_to_page(addr);
> +	struct folio *folio = vmalloc_to_folio(addr);
>   
> -	page->mapping = NULL;
> +	folio->mapping = NULL;
>   }
>   
>   static void rb_free_work(struct work_struct *work)
Yin Fengwei Aug. 23, 2023, 7:30 a.m. UTC | #2
On 8/22/23 04:20, Matthew Wilcox (Oracle) wrote:
> Eliminate a use of page->mapping by using vmalloc_to_folio() instead.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  include/linux/mm.h          | 5 +++++
>  kernel/events/ring_buffer.c | 4 ++--
>  2 files changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 840bae5f23b6..7d84a2843193 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1134,6 +1134,11 @@ int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
>  struct page *vmalloc_to_page(const void *addr);
>  unsigned long vmalloc_to_pfn(const void *addr);
>  
> +static inline struct folio *vmalloc_to_folio(const void *addr)
> +{
> +	return page_folio(vmalloc_to_page(addr));
I am wondering whether we should check the return value of vmalloc_to_page()?

> +}
> +
>  /*
>   * Determine if an address is within the vmalloc range
>   *
> diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
> index c73add132618..56939dc3bf33 100644
> --- a/kernel/events/ring_buffer.c
> +++ b/kernel/events/ring_buffer.c
> @@ -873,9 +873,9 @@ __perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff)
>  
>  static void perf_mmap_unmark_page(void *addr)
>  {
> -	struct page *page = vmalloc_to_page(addr);
> +	struct folio *folio = vmalloc_to_folio(addr);
>  
> -	page->mapping = NULL;
> +	folio->mapping = NULL;
>  }
>  
>  static void rb_free_work(struct work_struct *work)
Matthew Wilcox Aug. 23, 2023, 12:16 p.m. UTC | #3
On Wed, Aug 23, 2023 at 03:30:22PM +0800, Yin Fengwei wrote:
> On 8/22/23 04:20, Matthew Wilcox (Oracle) wrote:
> > +static inline struct folio *vmalloc_to_folio(const void *addr)
> > +{
> > +	return page_folio(vmalloc_to_page(addr));
> I am wondering whether we should check the return value of vmalloc_to_page()?

That's a good question.  Almost every user of vmalloc_to_page() just
assumes it works.  But it'll cost very little to check for NULL, so
I'll put that into the next version.  Thanks!
Lorenzo Stoakes Aug. 27, 2023, 7:22 a.m. UTC | #4
On Mon, Aug 21, 2023 at 09:20:15PM +0100, Matthew Wilcox (Oracle) wrote:
> Eliminate a use of page->mapping by using vmalloc_to_folio() instead.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  include/linux/mm.h          | 5 +++++
>  kernel/events/ring_buffer.c | 4 ++--
>  2 files changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 840bae5f23b6..7d84a2843193 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1134,6 +1134,11 @@ int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
>  struct page *vmalloc_to_page(const void *addr);
>  unsigned long vmalloc_to_pfn(const void *addr);
>
> +static inline struct folio *vmalloc_to_folio(const void *addr)
> +{
> +	return page_folio(vmalloc_to_page(addr));
> +}
> +

Total nit, and quite possibly unnecessary, but it might be nice to have a
comment pointing out that vmalloc_to_page can and will return tail pages hence
the need for page_folio() and thus _compound_head().

This makes me wonder about a typedef or wrapper type which explicitly indicates
that a function is _intentionally_ returning something that might be a head or a
tail page. But perhaps once foliofication is complete, this is what struct page
or its memdesc replacement will become?

>  /*
>   * Determine if an address is within the vmalloc range
>   *
> diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
> index c73add132618..56939dc3bf33 100644
> --- a/kernel/events/ring_buffer.c
> +++ b/kernel/events/ring_buffer.c
> @@ -873,9 +873,9 @@ __perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff)
>
>  static void perf_mmap_unmark_page(void *addr)
>  {
> -	struct page *page = vmalloc_to_page(addr);
> +	struct folio *folio = vmalloc_to_folio(addr);
>
> -	page->mapping = NULL;
> +	folio->mapping = NULL;
>  }
>
>  static void rb_free_work(struct work_struct *work)
> --
> 2.40.1
>

Reviewed-by: Lorenzo Stoakes <lstoakes@gmail.com>
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 840bae5f23b6..7d84a2843193 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1134,6 +1134,11 @@  int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
 struct page *vmalloc_to_page(const void *addr);
 unsigned long vmalloc_to_pfn(const void *addr);
 
+static inline struct folio *vmalloc_to_folio(const void *addr)
+{
+	return page_folio(vmalloc_to_page(addr));
+}
+
 /*
  * Determine if an address is within the vmalloc range
  *
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index c73add132618..56939dc3bf33 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -873,9 +873,9 @@  __perf_mmap_to_page(struct perf_buffer *rb, unsigned long pgoff)
 
 static void perf_mmap_unmark_page(void *addr)
 {
-	struct page *page = vmalloc_to_page(addr);
+	struct folio *folio = vmalloc_to_folio(addr);
 
-	page->mapping = NULL;
+	folio->mapping = NULL;
 }
 
 static void rb_free_work(struct work_struct *work)