linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: buffer: use raw page_memcg() on locked page
@ 2021-02-09 19:01 Johannes Weiner
  2021-02-09 19:12 ` Shakeel Butt
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Johannes Weiner @ 2021-02-09 19:01 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Muchun Song, Shakeel Butt, Michal Hocko, linux-mm, cgroups,
	linux-kernel, kernel-team

alloc_page_buffers() currently uses get_mem_cgroup_from_page() for
charging the buffers to the page owner, which does an rcu-protected
page->memcg lookup and acquires a reference. But buffer allocation has
the page lock held throughout, which pins the page to the memcg and
thereby the memcg - neither rcu nor holding an extra reference during
the allocation are necessary. Use a raw page_memcg() instead.

This was the last user of get_mem_cgroup_from_page(), delete it.

Reported-by: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
---
 fs/buffer.c                |  4 ++--
 include/linux/memcontrol.h |  7 -------
 mm/memcontrol.c            | 23 -----------------------
 3 files changed, 2 insertions(+), 32 deletions(-)

diff --git a/fs/buffer.c b/fs/buffer.c
index 96c7604f69b3..dc5d3e22c493 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -847,7 +847,8 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
 	if (retry)
 		gfp |= __GFP_NOFAIL;
 
-	memcg = get_mem_cgroup_from_page(page);
+	/* The page lock pins the memcg */
+	memcg = page_memcg(page);
 	old_memcg = set_active_memcg(memcg);
 
 	head = NULL;
@@ -868,7 +869,6 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
 	}
 out:
 	set_active_memcg(old_memcg);
-	mem_cgroup_put(memcg);
 	return head;
 /*
  * In case anything failed, we just free everything we got.
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index a8c7a0ccc759..a44b2d51aecc 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -687,8 +687,6 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
 
 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
 
-struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
-
 struct lruvec *lock_page_lruvec(struct page *page);
 struct lruvec *lock_page_lruvec_irq(struct page *page);
 struct lruvec *lock_page_lruvec_irqsave(struct page *page,
@@ -1169,11 +1167,6 @@ static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
 	return NULL;
 }
 
-static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
-{
-	return NULL;
-}
-
 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
 {
 }
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 51778fa9b462..9e455815fb7a 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1048,29 +1048,6 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
 }
 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
 
-/**
- * get_mem_cgroup_from_page: Obtain a reference on given page's memcg.
- * @page: page from which memcg should be extracted.
- *
- * Obtain a reference on page->memcg and returns it if successful. Otherwise
- * root_mem_cgroup is returned.
- */
-struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
-{
-	struct mem_cgroup *memcg = page_memcg(page);
-
-	if (mem_cgroup_disabled())
-		return NULL;
-
-	rcu_read_lock();
-	/* Page should not get uncharged and freed memcg under us. */
-	if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css)))
-		memcg = root_mem_cgroup;
-	rcu_read_unlock();
-	return memcg;
-}
-EXPORT_SYMBOL(get_mem_cgroup_from_page);
-
 static __always_inline struct mem_cgroup *active_memcg(void)
 {
 	if (in_interrupt())
-- 
2.30.0



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

* Re: [PATCH] fs: buffer: use raw page_memcg() on locked page
  2021-02-09 19:01 [PATCH] fs: buffer: use raw page_memcg() on locked page Johannes Weiner
@ 2021-02-09 19:12 ` Shakeel Butt
  2021-02-09 19:20 ` Roman Gushchin
  2021-02-10  9:18 ` Michal Hocko
  2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2021-02-09 19:12 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: Andrew Morton, Muchun Song, Michal Hocko, Linux MM, Cgroups,
	LKML, Kernel Team

On Tue, Feb 9, 2021 at 11:01 AM Johannes Weiner <hannes@cmpxchg.org> wrote:
>
> alloc_page_buffers() currently uses get_mem_cgroup_from_page() for
> charging the buffers to the page owner, which does an rcu-protected
> page->memcg lookup and acquires a reference. But buffer allocation has
> the page lock held throughout, which pins the page to the memcg and
> thereby the memcg - neither rcu nor holding an extra reference during
> the allocation are necessary. Use a raw page_memcg() instead.
>
> This was the last user of get_mem_cgroup_from_page(), delete it.
>
> Reported-by: Muchun Song <songmuchun@bytedance.com>
> Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>

Reviewed-by: Shakeel Butt <shakeelb@google.com>


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

* Re: [PATCH] fs: buffer: use raw page_memcg() on locked page
  2021-02-09 19:01 [PATCH] fs: buffer: use raw page_memcg() on locked page Johannes Weiner
  2021-02-09 19:12 ` Shakeel Butt
@ 2021-02-09 19:20 ` Roman Gushchin
  2021-02-10  9:18 ` Michal Hocko
  2 siblings, 0 replies; 4+ messages in thread
From: Roman Gushchin @ 2021-02-09 19:20 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: Andrew Morton, Muchun Song, Shakeel Butt, Michal Hocko, linux-mm,
	cgroups, linux-kernel, kernel-team

On Tue, Feb 09, 2021 at 02:01:26PM -0500, Johannes Weiner wrote:
> alloc_page_buffers() currently uses get_mem_cgroup_from_page() for
> charging the buffers to the page owner, which does an rcu-protected
> page->memcg lookup and acquires a reference. But buffer allocation has
> the page lock held throughout, which pins the page to the memcg and
> thereby the memcg - neither rcu nor holding an extra reference during
> the allocation are necessary. Use a raw page_memcg() instead.
> 
> This was the last user of get_mem_cgroup_from_page(), delete it.
> 
> Reported-by: Muchun Song <songmuchun@bytedance.com>
> Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>

Acked-by: Roman Gushchin <guro@fb.com>

Thanks!


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

* Re: [PATCH] fs: buffer: use raw page_memcg() on locked page
  2021-02-09 19:01 [PATCH] fs: buffer: use raw page_memcg() on locked page Johannes Weiner
  2021-02-09 19:12 ` Shakeel Butt
  2021-02-09 19:20 ` Roman Gushchin
@ 2021-02-10  9:18 ` Michal Hocko
  2 siblings, 0 replies; 4+ messages in thread
From: Michal Hocko @ 2021-02-10  9:18 UTC (permalink / raw)
  To: Johannes Weiner
  Cc: Andrew Morton, Muchun Song, Shakeel Butt, linux-mm, cgroups,
	linux-kernel, kernel-team

On Tue 09-02-21 14:01:26, Johannes Weiner wrote:
> alloc_page_buffers() currently uses get_mem_cgroup_from_page() for
> charging the buffers to the page owner, which does an rcu-protected
> page->memcg lookup and acquires a reference. But buffer allocation has
> the page lock held throughout, which pins the page to the memcg and
> thereby the memcg - neither rcu nor holding an extra reference during
> the allocation are necessary. Use a raw page_memcg() instead.
> 
> This was the last user of get_mem_cgroup_from_page(), delete it.
> 
> Reported-by: Muchun Song <songmuchun@bytedance.com>
> Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>

Acked-by: Michal Hocko <mhocko@suse.com>

Thanks!

> ---
>  fs/buffer.c                |  4 ++--
>  include/linux/memcontrol.h |  7 -------
>  mm/memcontrol.c            | 23 -----------------------
>  3 files changed, 2 insertions(+), 32 deletions(-)
> 
> diff --git a/fs/buffer.c b/fs/buffer.c
> index 96c7604f69b3..dc5d3e22c493 100644
> --- a/fs/buffer.c
> +++ b/fs/buffer.c
> @@ -847,7 +847,8 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
>  	if (retry)
>  		gfp |= __GFP_NOFAIL;
>  
> -	memcg = get_mem_cgroup_from_page(page);
> +	/* The page lock pins the memcg */
> +	memcg = page_memcg(page);
>  	old_memcg = set_active_memcg(memcg);
>  
>  	head = NULL;
> @@ -868,7 +869,6 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
>  	}
>  out:
>  	set_active_memcg(old_memcg);
> -	mem_cgroup_put(memcg);
>  	return head;
>  /*
>   * In case anything failed, we just free everything we got.
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index a8c7a0ccc759..a44b2d51aecc 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -687,8 +687,6 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
>  
>  struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
>  
> -struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
> -
>  struct lruvec *lock_page_lruvec(struct page *page);
>  struct lruvec *lock_page_lruvec_irq(struct page *page);
>  struct lruvec *lock_page_lruvec_irqsave(struct page *page,
> @@ -1169,11 +1167,6 @@ static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
>  	return NULL;
>  }
>  
> -static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
> -{
> -	return NULL;
> -}
> -
>  static inline void mem_cgroup_put(struct mem_cgroup *memcg)
>  {
>  }
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 51778fa9b462..9e455815fb7a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1048,29 +1048,6 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
>  }
>  EXPORT_SYMBOL(get_mem_cgroup_from_mm);
>  
> -/**
> - * get_mem_cgroup_from_page: Obtain a reference on given page's memcg.
> - * @page: page from which memcg should be extracted.
> - *
> - * Obtain a reference on page->memcg and returns it if successful. Otherwise
> - * root_mem_cgroup is returned.
> - */
> -struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
> -{
> -	struct mem_cgroup *memcg = page_memcg(page);
> -
> -	if (mem_cgroup_disabled())
> -		return NULL;
> -
> -	rcu_read_lock();
> -	/* Page should not get uncharged and freed memcg under us. */
> -	if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css)))
> -		memcg = root_mem_cgroup;
> -	rcu_read_unlock();
> -	return memcg;
> -}
> -EXPORT_SYMBOL(get_mem_cgroup_from_page);
> -
>  static __always_inline struct mem_cgroup *active_memcg(void)
>  {
>  	if (in_interrupt())
> -- 
> 2.30.0

-- 
Michal Hocko
SUSE Labs


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

end of thread, other threads:[~2021-02-10  9:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-09 19:01 [PATCH] fs: buffer: use raw page_memcg() on locked page Johannes Weiner
2021-02-09 19:12 ` Shakeel Butt
2021-02-09 19:20 ` Roman Gushchin
2021-02-10  9:18 ` Michal Hocko

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).