[v5,6/6] mm: kmem: reimplement get_obj_cgroup_from_current()

Message ID 20231019225346.1822282-7-roman.gushchin@linux.dev
State New
Headers
Series mm: improve performance of accounted kernel memory allocations |

Commit Message

Roman Gushchin Oct. 19, 2023, 10:53 p.m. UTC
  Reimplement get_obj_cgroup_from_current() using current_obj_cgroup().
get_obj_cgroup_from_current() and current_obj_cgroup() share 80% of
the code, so the new implementation is almost trivial.

get_obj_cgroup_from_current() is a convenient function used by the
bpf subsystem, so there is no reason to get rid of it completely.

Signed-off-by: Roman Gushchin (Cruise) <roman.gushchin@linux.dev>
---
 include/linux/memcontrol.h | 11 ++++++++++-
 mm/memcontrol.c            | 32 --------------------------------
 2 files changed, 10 insertions(+), 33 deletions(-)
  

Comments

Vlastimil Babka Oct. 20, 2023, 5:41 a.m. UTC | #1
On 10/20/23 00:53, Roman Gushchin wrote:
> Reimplement get_obj_cgroup_from_current() using current_obj_cgroup().
> get_obj_cgroup_from_current() and current_obj_cgroup() share 80% of
> the code, so the new implementation is almost trivial.

Great.

> get_obj_cgroup_from_current() is a convenient function used by the
> bpf subsystem, so there is no reason to get rid of it completely.
> 
> Signed-off-by: Roman Gushchin (Cruise) <roman.gushchin@linux.dev>

Reviwed-by: Vlastimil Babka <vbabka@suse.cz>

Thanks!
  
Shakeel Butt Oct. 20, 2023, 6:31 a.m. UTC | #2
On Thu, Oct 19, 2023 at 3:54 PM Roman Gushchin <roman.gushchin@linux.dev> wrote:
>
> Reimplement get_obj_cgroup_from_current() using current_obj_cgroup().
> get_obj_cgroup_from_current() and current_obj_cgroup() share 80% of
> the code, so the new implementation is almost trivial.
>
> get_obj_cgroup_from_current() is a convenient function used by the
> bpf subsystem, so there is no reason to get rid of it completely.
>
> Signed-off-by: Roman Gushchin (Cruise) <roman.gushchin@linux.dev>

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

Patch

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index a89df289144d..ef26551a633f 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1778,9 +1778,18 @@  void __memcg_kmem_uncharge_page(struct page *page, int order);
  * needs to be used outside of the local scope.
  */
 struct obj_cgroup *current_obj_cgroup(void);
-struct obj_cgroup *get_obj_cgroup_from_current(void);
 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio);
 
+static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
+{
+	struct obj_cgroup *objcg = current_obj_cgroup();
+
+	if (objcg)
+		obj_cgroup_get(objcg);
+
+	return objcg;
+}
+
 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e3d4b7fabb7d..e13c10912c16 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3056,38 +3056,6 @@  static struct obj_cgroup *current_objcg_update(void)
 	return objcg;
 }
 
-__always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
-{
-	struct mem_cgroup *memcg;
-	struct obj_cgroup *objcg;
-
-	if (in_task()) {
-		memcg = current->active_memcg;
-		if (unlikely(memcg))
-			goto from_memcg;
-
-		objcg = READ_ONCE(current->objcg);
-		if (unlikely((unsigned long)objcg & CURRENT_OBJCG_UPDATE_FLAG))
-			objcg = current_objcg_update();
-
-		if (objcg) {
-			obj_cgroup_get(objcg);
-			return objcg;
-		}
-	} else {
-		memcg = this_cpu_read(int_active_memcg);
-		if (unlikely(memcg))
-			goto from_memcg;
-	}
-	return NULL;
-
-from_memcg:
-	rcu_read_lock();
-	objcg = __get_obj_cgroup_from_memcg(memcg);
-	rcu_read_unlock();
-	return objcg;
-}
-
 __always_inline struct obj_cgroup *current_obj_cgroup(void)
 {
 	struct mem_cgroup *memcg;