Message ID | 20220810151840.16394-14-laoar.shao@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | bpf: Introduce selectable memcg for bpf map | expand |
On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > a specific cgroup. > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > --- > include/linux/memcontrol.h | 1 + > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 42 insertions(+) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index 2f0a611..901a921 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > void __memcg_kmem_uncharge_page(struct page *page, int order); > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > struct obj_cgroup *get_obj_cgroup_from_current(void); > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 618c366..762cffa 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > return objcg; > } > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > +{ > + struct obj_cgroup *objcg; > + > + if (memcg_kmem_bypass()) > + return NULL; > + > + rcu_read_lock(); > + objcg = __get_obj_cgroup_from_memcg(memcg); > + rcu_read_unlock(); > + return objcg; This code doesn't make sense to me. What does rcu read lock protect here?
On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > a specific cgroup. > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > --- > > include/linux/memcontrol.h | 1 + > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 42 insertions(+) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index 2f0a611..901a921 100644 > > --- a/include/linux/memcontrol.h > > +++ b/include/linux/memcontrol.h > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index 618c366..762cffa 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > return objcg; > > } > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > +{ > > + struct obj_cgroup *objcg; > > + > > + if (memcg_kmem_bypass()) > > + return NULL; > > + > > + rcu_read_lock(); > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > + rcu_read_unlock(); > > + return objcg; > > This code doesn't make sense to me. What does rcu read lock protect here? To protect rcu_dereference(memcg->objcg);. Doesn't it need the read rcu lock ?
On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > a specific cgroup. Can you please add couple of lines on why you need objcg? > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > --- > include/linux/memcontrol.h | 1 + > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 42 insertions(+) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index 2f0a611..901a921 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > void __memcg_kmem_uncharge_page(struct page *page, int order); > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > struct obj_cgroup *get_obj_cgroup_from_current(void); > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 618c366..762cffa 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > return objcg; > } > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > +{ > + struct obj_cgroup *objcg; > + > + if (memcg_kmem_bypass()) > + return NULL; > + > + rcu_read_lock(); > + objcg = __get_obj_cgroup_from_memcg(memcg); > + rcu_read_unlock(); > + return objcg; > +} > + > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp) > +{ > + struct cgroup_subsys_state *css; > + struct mem_cgroup *memcg; > + struct obj_cgroup *objcg; > + > + rcu_read_lock(); > + css = rcu_dereference(cgrp->subsys[memory_cgrp_id]); > + if (!css || !css_tryget_online(css)) { > + rcu_read_unlock(); > + cgroup_put(cgrp); > + return ERR_PTR(-EINVAL); > + } > + rcu_read_unlock(); > + cgroup_put(cgrp); The above put seems out of place and buggy. > + > + memcg = mem_cgroup_from_css(css); > + if (!memcg) { > + css_put(css); > + return ERR_PTR(-EINVAL); > + } > + > + objcg = get_obj_cgroup_from_memcg(memcg); > + css_put(css); > + > + return objcg; > +} > + > __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) > { > struct obj_cgroup *objcg = NULL; > -- > 1.8.3.1 >
On Fri, Aug 12, 2022 at 08:35:19AM +0800, Yafang Shao wrote: > On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin > <roman.gushchin@linux.dev> wrote: > > > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > > a specific cgroup. > > > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > --- > > > include/linux/memcontrol.h | 1 + > > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > 2 files changed, 42 insertions(+) > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > index 2f0a611..901a921 100644 > > > --- a/include/linux/memcontrol.h > > > +++ b/include/linux/memcontrol.h > > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > index 618c366..762cffa 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > return objcg; > > > } > > > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > +{ > > > + struct obj_cgroup *objcg; > > > + > > > + if (memcg_kmem_bypass()) > > > + return NULL; > > > + > > > + rcu_read_lock(); > > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > > + rcu_read_unlock(); > > > + return objcg; > > > > This code doesn't make sense to me. What does rcu read lock protect here? > > To protect rcu_dereference(memcg->objcg);. > Doesn't it need the read rcu lock ? No, it's not how rcu works. Please, take a look at the docs here: https://docs.kernel.org/RCU/whatisRCU.html#whatisrcu . In particular, it describes this specific case very well. In 2 words, you don't protect the rcu_dereference() call, you protect the pointer you get, cause it's valid only inside the rcu read section. After rcu_read_unlock() it might point at a random data, because the protected object can be already freed. Thanks!
On Sat, Aug 13, 2022 at 1:40 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Fri, Aug 12, 2022 at 08:35:19AM +0800, Yafang Shao wrote: > > On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin > > <roman.gushchin@linux.dev> wrote: > > > > > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > > > a specific cgroup. > > > > > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > > --- > > > > include/linux/memcontrol.h | 1 + > > > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > > 2 files changed, 42 insertions(+) > > > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > > index 2f0a611..901a921 100644 > > > > --- a/include/linux/memcontrol.h > > > > +++ b/include/linux/memcontrol.h > > > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > > index 618c366..762cffa 100644 > > > > --- a/mm/memcontrol.c > > > > +++ b/mm/memcontrol.c > > > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > return objcg; > > > > } > > > > > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > +{ > > > > + struct obj_cgroup *objcg; > > > > + > > > > + if (memcg_kmem_bypass()) > > > > + return NULL; > > > > + > > > > + rcu_read_lock(); > > > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > > > + rcu_read_unlock(); > > > > + return objcg; > > > > > > This code doesn't make sense to me. What does rcu read lock protect here? > > > > To protect rcu_dereference(memcg->objcg);. > > Doesn't it need the read rcu lock ? > > No, it's not how rcu works. Please, take a look at the docs here: > https://docs.kernel.org/RCU/whatisRCU.html#whatisrcu . > In particular, it describes this specific case very well. > > In 2 words, you don't protect the rcu_dereference() call, you protect the pointer I just copied and pasted rcu_dereference(memcg->objcg) there to make it clear. Actually it protects memcg->objcg, doesn't it ? > you get, cause it's valid only inside the rcu read section. After rcu_read_unlock() > it might point at a random data, because the protected object can be already freed. > Are you sure? Can't the obj_cgroup_tryget(objcg) prevent it from being freed ?
On Sat, Aug 13, 2022 at 12:57 AM Shakeel Butt <shakeelb@google.com> wrote: > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > a specific cgroup. > > Can you please add couple of lines on why you need objcg? > Sure. will update in the next version. > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > --- > > include/linux/memcontrol.h | 1 + > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 42 insertions(+) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index 2f0a611..901a921 100644 > > --- a/include/linux/memcontrol.h > > +++ b/include/linux/memcontrol.h > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index 618c366..762cffa 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > return objcg; > > } > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > +{ > > + struct obj_cgroup *objcg; > > + > > + if (memcg_kmem_bypass()) > > + return NULL; > > + > > + rcu_read_lock(); > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > + rcu_read_unlock(); > > + return objcg; > > +} > > + > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp) > > +{ > > + struct cgroup_subsys_state *css; > > + struct mem_cgroup *memcg; > > + struct obj_cgroup *objcg; > > + > > + rcu_read_lock(); > > + css = rcu_dereference(cgrp->subsys[memory_cgrp_id]); > > + if (!css || !css_tryget_online(css)) { > > + rcu_read_unlock(); > > + cgroup_put(cgrp); > > + return ERR_PTR(-EINVAL); > > + } > > + rcu_read_unlock(); > > + cgroup_put(cgrp); > > The above put seems out of place and buggy. > Thanks for pointing it out. The cgroup_put should be used in bpf_map_save_memcg(). I will update it.
On Sat, Aug 13, 2022 at 07:56:54AM +0800, Yafang Shao wrote: > On Sat, Aug 13, 2022 at 1:40 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > On Fri, Aug 12, 2022 at 08:35:19AM +0800, Yafang Shao wrote: > > > On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin > > > <roman.gushchin@linux.dev> wrote: > > > > > > > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > > > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > > > > a specific cgroup. > > > > > > > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > > > --- > > > > > include/linux/memcontrol.h | 1 + > > > > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > > > 2 files changed, 42 insertions(+) > > > > > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > > > index 2f0a611..901a921 100644 > > > > > --- a/include/linux/memcontrol.h > > > > > +++ b/include/linux/memcontrol.h > > > > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > > > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > > > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > > > > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > > > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > > > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > > > index 618c366..762cffa 100644 > > > > > --- a/mm/memcontrol.c > > > > > +++ b/mm/memcontrol.c > > > > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > return objcg; > > > > > } > > > > > > > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > +{ > > > > > + struct obj_cgroup *objcg; > > > > > + > > > > > + if (memcg_kmem_bypass()) > > > > > + return NULL; > > > > > + > > > > > + rcu_read_lock(); > > > > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > > > > + rcu_read_unlock(); > > > > > + return objcg; > > > > > > > > This code doesn't make sense to me. What does rcu read lock protect here? > > > > > > To protect rcu_dereference(memcg->objcg);. > > > Doesn't it need the read rcu lock ? > > > > No, it's not how rcu works. Please, take a look at the docs here: > > https://docs.kernel.org/RCU/whatisRCU.html#whatisrcu . > > In particular, it describes this specific case very well. > > > > In 2 words, you don't protect the rcu_dereference() call, you protect the pointer > > I just copied and pasted rcu_dereference(memcg->objcg) there to make it clear. > Actually it protects memcg->objcg, doesn't it ? > > > you get, cause it's valid only inside the rcu read section. After rcu_read_unlock() > > it might point at a random data, because the protected object can be already freed. > > > > Are you sure? > Can't the obj_cgroup_tryget(objcg) prevent it from being freed ? Ok, now I see where it comes from. You copy-pasted it from get_obj_cgroup_from_current()? There rcu read lock section protects memcg, not objcg. In your case you don't need it, because memcg is passed as a parameter to the function, so it's the duty of the caller to ensure the lifetime of memcg. Thanks!
On Sun, Aug 14, 2022 at 2:30 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Sat, Aug 13, 2022 at 07:56:54AM +0800, Yafang Shao wrote: > > On Sat, Aug 13, 2022 at 1:40 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > > > On Fri, Aug 12, 2022 at 08:35:19AM +0800, Yafang Shao wrote: > > > > On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin > > > > <roman.gushchin@linux.dev> wrote: > > > > > > > > > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > > > > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > > > > > a specific cgroup. > > > > > > > > > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > > > > --- > > > > > > include/linux/memcontrol.h | 1 + > > > > > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > > > > 2 files changed, 42 insertions(+) > > > > > > > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > > > > index 2f0a611..901a921 100644 > > > > > > --- a/include/linux/memcontrol.h > > > > > > +++ b/include/linux/memcontrol.h > > > > > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > > > > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > > > > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > > > > > > > > > +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > > > > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > > > > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > > > > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > > > > index 618c366..762cffa 100644 > > > > > > --- a/mm/memcontrol.c > > > > > > +++ b/mm/memcontrol.c > > > > > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > > return objcg; > > > > > > } > > > > > > > > > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > > +{ > > > > > > + struct obj_cgroup *objcg; > > > > > > + > > > > > > + if (memcg_kmem_bypass()) > > > > > > + return NULL; > > > > > > + > > > > > > + rcu_read_lock(); > > > > > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > > > > > + rcu_read_unlock(); > > > > > > + return objcg; > > > > > > > > > > This code doesn't make sense to me. What does rcu read lock protect here? > > > > > > > > To protect rcu_dereference(memcg->objcg);. > > > > Doesn't it need the read rcu lock ? > > > > > > No, it's not how rcu works. Please, take a look at the docs here: > > > https://docs.kernel.org/RCU/whatisRCU.html#whatisrcu . > > > In particular, it describes this specific case very well. > > > > > > In 2 words, you don't protect the rcu_dereference() call, you protect the pointer > > > > I just copied and pasted rcu_dereference(memcg->objcg) there to make it clear. > > Actually it protects memcg->objcg, doesn't it ? > > > > > you get, cause it's valid only inside the rcu read section. After rcu_read_unlock() > > > it might point at a random data, because the protected object can be already freed. > > > > > > > Are you sure? > > Can't the obj_cgroup_tryget(objcg) prevent it from being freed ? > > Ok, now I see where it comes from. You copy-pasted it from get_obj_cgroup_from_current()? > There rcu read lock section protects memcg, not objcg. Could you pls explain in detail why we should protect memcg instead of objcg ? Why does the memcg need the read rcu lock ? > In your case you don't need it, because memcg is passed as a parameter to the function, > so it's the duty of the caller to ensure the lifetime of memcg. > I'm still a bit confused. See below, objcg = rcu_dereference(memcg->objcg); percpu_ref_tryget(&objcg->refcnt); <<<< what if the objcg is freed before this operation ??
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 2f0a611..901a921 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); void __memcg_kmem_uncharge_page(struct page *page, int order); +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); struct obj_cgroup *get_obj_cgroup_from_current(void); struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 618c366..762cffa 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) return objcg; } +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) +{ + struct obj_cgroup *objcg; + + if (memcg_kmem_bypass()) + return NULL; + + rcu_read_lock(); + objcg = __get_obj_cgroup_from_memcg(memcg); + rcu_read_unlock(); + return objcg; +} + +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp) +{ + struct cgroup_subsys_state *css; + struct mem_cgroup *memcg; + struct obj_cgroup *objcg; + + rcu_read_lock(); + css = rcu_dereference(cgrp->subsys[memory_cgrp_id]); + if (!css || !css_tryget_online(css)) { + rcu_read_unlock(); + cgroup_put(cgrp); + return ERR_PTR(-EINVAL); + } + rcu_read_unlock(); + cgroup_put(cgrp); + + memcg = mem_cgroup_from_css(css); + if (!memcg) { + css_put(css); + return ERR_PTR(-EINVAL); + } + + objcg = get_obj_cgroup_from_memcg(memcg); + css_put(css); + + return objcg; +} + __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) { struct obj_cgroup *objcg = NULL;
Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from a specific cgroup. Signed-off-by: Yafang Shao <laoar.shao@gmail.com> --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)