diff mbox series

mm/memcg: warn on missing memcg on mem_cgroup_page_lruvec()

Message ID 20201125112202.387009-1-lstoakes@gmail.com (mailing list archive)
State New, archived
Headers show
Series mm/memcg: warn on missing memcg on mem_cgroup_page_lruvec() | expand

Commit Message

Lorenzo Stoakes Nov. 25, 2020, 11:22 a.m. UTC
Move memcg check to mem_cgroup_page_lruvec() as there are callers which
may invoke this with !memcg in mem_cgroup_lruvec(), whereas they should
not in mem_cgroup_page_lruvec().

We expect that we have always charged a page to the memcg before
mem_cgroup_page_lruvec() is invoked, so add a warning to assert that this
is the case.

Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
Reported-by: syzbot+ce635500093181f39c1c@syzkaller.appspotmail.com
---
 include/linux/memcontrol.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Alex Shi Nov. 25, 2020, 12:15 p.m. UTC | #1
Acked-by: Alex Shi <alex.shi@linux.alibaba.com>


在 2020/11/25 下午7:22, Lorenzo Stoakes 写道:
> Move memcg check to mem_cgroup_page_lruvec() as there are callers which
> may invoke this with !memcg in mem_cgroup_lruvec(), whereas they should
> not in mem_cgroup_page_lruvec().
> 
> We expect that we have always charged a page to the memcg before
> mem_cgroup_page_lruvec() is invoked, so add a warning to assert that this
> is the case.
> 
> Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
> Reported-by: syzbot+ce635500093181f39c1c@syzkaller.appspotmail.com
> ---
>  include/linux/memcontrol.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index 87ed56dc75f9..3e6a1df3bdb9 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -618,7 +618,6 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
>  		goto out;
>  	}
>  
> -	VM_WARN_ON_ONCE(!memcg);
>  	if (!memcg)
>  		memcg = root_mem_cgroup;
>  
> @@ -645,7 +644,10 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
>  static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
>  						struct pglist_data *pgdat)
>  {
> -	return mem_cgroup_lruvec(page_memcg(page), pgdat);
> +	struct mem_cgroup *memcg = page_memcg(page);
> +
> +	VM_WARN_ON_ONCE_PAGE(!memcg, page);
> +	return mem_cgroup_lruvec(memcg, pgdat);
>  }
>  
>  static inline bool lruvec_holds_page_lru_lock(struct page *page,
>
Stephen Rothwell Nov. 25, 2020, 10:27 p.m. UTC | #2
Hi all,

On Wed, 25 Nov 2020 20:15:11 +0800 Alex Shi <alex.shi@linux.alibaba.com> wrote:
>
> Acked-by: Alex Shi <alex.shi@linux.alibaba.com>
> 
> 
> 在 2020/11/25 下午7:22, Lorenzo Stoakes 写道:
> > Move memcg check to mem_cgroup_page_lruvec() as there are callers which
> > may invoke this with !memcg in mem_cgroup_lruvec(), whereas they should
> > not in mem_cgroup_page_lruvec().
> > 
> > We expect that we have always charged a page to the memcg before
> > mem_cgroup_page_lruvec() is invoked, so add a warning to assert that this
> > is the case.
> > 
> > Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
> > Reported-by: syzbot+ce635500093181f39c1c@syzkaller.appspotmail.com
> > ---
> >  include/linux/memcontrol.h | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> > index 87ed56dc75f9..3e6a1df3bdb9 100644
> > --- a/include/linux/memcontrol.h
> > +++ b/include/linux/memcontrol.h
> > @@ -618,7 +618,6 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
> >  		goto out;
> >  	}
> >  
> > -	VM_WARN_ON_ONCE(!memcg);
> >  	if (!memcg)
> >  		memcg = root_mem_cgroup;
> >  
> > @@ -645,7 +644,10 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
> >  static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
> >  						struct pglist_data *pgdat)
> >  {
> > -	return mem_cgroup_lruvec(page_memcg(page), pgdat);
> > +	struct mem_cgroup *memcg = page_memcg(page);
> > +
> > +	VM_WARN_ON_ONCE_PAGE(!memcg, page);
> > +	return mem_cgroup_lruvec(memcg, pgdat);
> >  }
> >  
> >  static inline bool lruvec_holds_page_lru_lock(struct page *page,
> >   

I have added that patch to the akpm tree in linux-next today as a fix
for "mm/memcg: add missed warning in mem_cgroup_lruvec".

Andrew: the original patch is here:
https://lore.kernel.org/lkml/20201125112202.387009-1-lstoakes@gmail.com/
diff mbox series

Patch

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 87ed56dc75f9..3e6a1df3bdb9 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -618,7 +618,6 @@  static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
 		goto out;
 	}
 
-	VM_WARN_ON_ONCE(!memcg);
 	if (!memcg)
 		memcg = root_mem_cgroup;
 
@@ -645,7 +644,10 @@  static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
 						struct pglist_data *pgdat)
 {
-	return mem_cgroup_lruvec(page_memcg(page), pgdat);
+	struct mem_cgroup *memcg = page_memcg(page);
+
+	VM_WARN_ON_ONCE_PAGE(!memcg, page);
+	return mem_cgroup_lruvec(memcg, pgdat);
 }
 
 static inline bool lruvec_holds_page_lru_lock(struct page *page,