Message ID | 20220619155032.32515-4-laoar.shao@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | bpf, mm: Recharge pages when reuse bpf map | expand |
On Sun, Jun 19, 2022 at 03:50:25PM +0000, Yafang Shao wrote: > The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() > is that the later one doesn't add objcg's refcnt. > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > --- > include/linux/memcontrol.h | 1 + > mm/memcontrol.c | 24 ++++++++++++++++++++++++ > 2 files changed, 25 insertions(+) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index cf074156c6ac..402b42670bcd 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void); > 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 *obj_cgroup_from_current(void); > 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 abec50f31fe6..350a7849dac3 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) > return objcg; > } > > +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) > +{ > + struct obj_cgroup *objcg = NULL; > + struct mem_cgroup *memcg; > + > + if (memcg_kmem_bypass()) > + return NULL; > + > + rcu_read_lock(); > + if (unlikely(active_memcg())) > + memcg = active_memcg(); > + else > + memcg = mem_cgroup_from_task(current); > + > + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { > + objcg = rcu_dereference(memcg->objcg); > + if (objcg) > + break; > + } > + rcu_read_unlock(); Hm, what prevents the objcg from being released here? Under which conditions it's safe to call it?
On Thu, Jun 23, 2022 at 11:01 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Sun, Jun 19, 2022 at 03:50:25PM +0000, Yafang Shao wrote: > > The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() > > is that the later one doesn't add objcg's refcnt. > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > --- > > include/linux/memcontrol.h | 1 + > > mm/memcontrol.c | 24 ++++++++++++++++++++++++ > > 2 files changed, 25 insertions(+) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index cf074156c6ac..402b42670bcd 100644 > > --- a/include/linux/memcontrol.h > > +++ b/include/linux/memcontrol.h > > @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void); > > 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 *obj_cgroup_from_current(void); > > 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 abec50f31fe6..350a7849dac3 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) > > return objcg; > > } > > > > +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) > > +{ > > + struct obj_cgroup *objcg = NULL; > > + struct mem_cgroup *memcg; > > + > > + if (memcg_kmem_bypass()) > > + return NULL; > > + > > + rcu_read_lock(); > > + if (unlikely(active_memcg())) > > + memcg = active_memcg(); > > + else > > + memcg = mem_cgroup_from_task(current); > > + > > + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { > > + objcg = rcu_dereference(memcg->objcg); > > + if (objcg) > > + break; > > + } > > + rcu_read_unlock(); > > Hm, what prevents the objcg from being released here? Under which conditions > it's safe to call it? obj_cgroup_from_current() is used when we know the objcg's refcnt has already been incremented. For example in my case, it is called after we have already call get_ parent_mem_cgroup(). I should add a comment or a WARN_ON() in this function.
On Sat, Jun 25, 2022 at 09:54:17PM +0800, Yafang Shao wrote: > On Thu, Jun 23, 2022 at 11:01 AM Roman Gushchin > <roman.gushchin@linux.dev> wrote: > > > > On Sun, Jun 19, 2022 at 03:50:25PM +0000, Yafang Shao wrote: > > > The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() > > > is that the later one doesn't add objcg's refcnt. > > > > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > --- > > > include/linux/memcontrol.h | 1 + > > > mm/memcontrol.c | 24 ++++++++++++++++++++++++ > > > 2 files changed, 25 insertions(+) > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > index cf074156c6ac..402b42670bcd 100644 > > > --- a/include/linux/memcontrol.h > > > +++ b/include/linux/memcontrol.h > > > @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void); > > > 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 *obj_cgroup_from_current(void); > > > 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 abec50f31fe6..350a7849dac3 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) > > > return objcg; > > > } > > > > > > +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) > > > +{ > > > + struct obj_cgroup *objcg = NULL; > > > + struct mem_cgroup *memcg; > > > + > > > + if (memcg_kmem_bypass()) > > > + return NULL; > > > + > > > + rcu_read_lock(); > > > + if (unlikely(active_memcg())) > > > + memcg = active_memcg(); > > > + else > > > + memcg = mem_cgroup_from_task(current); > > > + > > > + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { > > > + objcg = rcu_dereference(memcg->objcg); > > > + if (objcg) > > > + break; > > > + } > > > + rcu_read_unlock(); > > > > Hm, what prevents the objcg from being released here? Under which conditions > > it's safe to call it? > > obj_cgroup_from_current() is used when we know the objcg's refcnt has > already been incremented. > For example in my case, it is called after we have already call get_ > parent_mem_cgroup(). > I should add a comment or a WARN_ON() in this function. Yes, it's very confusing, please add a big comment explaining under which conditions it's safe to call it. Thanks!
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index cf074156c6ac..402b42670bcd 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void); 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 *obj_cgroup_from_current(void); 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 abec50f31fe6..350a7849dac3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) return objcg; } +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) +{ + struct obj_cgroup *objcg = NULL; + struct mem_cgroup *memcg; + + if (memcg_kmem_bypass()) + return NULL; + + rcu_read_lock(); + if (unlikely(active_memcg())) + memcg = active_memcg(); + else + memcg = mem_cgroup_from_task(current); + + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { + objcg = rcu_dereference(memcg->objcg); + if (objcg) + break; + } + rcu_read_unlock(); + + return objcg; +} + static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) { mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);
The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() is that the later one doesn't add objcg's refcnt. Signed-off-by: Yafang Shao <laoar.shao@gmail.com> --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)