From nobody Wed Sep 17 08:03:25 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E18AFC3DA7A for ; Tue, 20 Dec 2022 18:28:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233478AbiLTS2V (ORCPT ); Tue, 20 Dec 2022 13:28:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230026AbiLTS2F (ORCPT ); Tue, 20 Dec 2022 13:28:05 -0500 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB0011AD8C; Tue, 20 Dec 2022 10:28:04 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1671560883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1c+efmi7i1PNMb6NmmQf0Ro/sE7SMscFhY/KBS3yOLk=; b=Wx7bQ5ioJAD+g491R7nSJvJ8QTYMx+OPD/+3NQf2xDyjGmSAIeS5xvA6vaaypoUUowSvau T38EBM/MyoOs0D9shUHdyPMcOMxI88n+BDtB5+ZeMySUKoKja9i+0tgo8ESPYw+WmPupoG JdpUiHXhDx7tLpgzdmFRX6vTZ0dwRYM= From: Roman Gushchin To: linux-mm@kvack.org Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Shakeel Butt , Johannes Weiner , Michal Hocko , Muchun Song , Andrew Morton , Roman Gushchin Subject: [PATCH RFC 1/2] mm: kmem: optimize get_obj_cgroup_from_current() Date: Tue, 20 Dec 2022 10:27:44 -0800 Message-Id: <20221220182745.1903540-2-roman.gushchin@linux.dev> In-Reply-To: <20221220182745.1903540-1-roman.gushchin@linux.dev> References: <20221220182745.1903540-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Manually inline memcg_kmem_bypass() and active_memcg() to speed up get_obj_cgroup_from_current() by avoiding duplicate in_task() checks and active_memcg() readings. Also add a likely() macro to __get_obj_cgroup_from_memcg(): obj_cgroup_tryget() should succeed at almost all times except a very unlikely race with the memcg deletion path. Signed-off-by: Roman Gushchin Acked-by: Shakeel Butt --- mm/memcontrol.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index bafd3cde4507..82828c51d2ea 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1047,19 +1047,6 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_= struct *mm) } EXPORT_SYMBOL(get_mem_cgroup_from_mm); =20 -static __always_inline bool memcg_kmem_bypass(void) -{ - /* Allow remote memcg charging from any context. */ - if (unlikely(active_memcg())) - return false; - - /* Memcg to charge can't be determined. */ - if (!in_task() || !current->mm || (current->flags & PF_KTHREAD)) - return true; - - return false; -} - /** * mem_cgroup_iter - iterate over memory cgroup hierarchy * @root: hierarchy root @@ -3004,7 +2991,7 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg= (struct mem_cgroup *memcg) =20 for (; !mem_cgroup_is_root(memcg); memcg =3D parent_mem_cgroup(memcg)) { objcg =3D rcu_dereference(memcg->objcg); - if (objcg && obj_cgroup_tryget(objcg)) + if (likely(objcg && obj_cgroup_tryget(objcg))) break; objcg =3D NULL; } @@ -3013,16 +3000,23 @@ static struct obj_cgroup *__get_obj_cgroup_from_mem= cg(struct mem_cgroup *memcg) =20 __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) { - struct obj_cgroup *objcg =3D NULL; struct mem_cgroup *memcg; + struct obj_cgroup *objcg; =20 - if (memcg_kmem_bypass()) - return NULL; + if (in_task()) { + memcg =3D current->active_memcg; + + /* Memcg to charge can't be determined. */ + if (likely(!memcg) && (!current->mm || (current->flags & PF_KTHREAD))) + return NULL; + } else { + memcg =3D this_cpu_read(int_active_memcg); + if (likely(!memcg)) + return NULL; + } =20 rcu_read_lock(); - if (unlikely(active_memcg())) - memcg =3D active_memcg(); - else + if (!memcg) memcg =3D mem_cgroup_from_task(current); objcg =3D __get_obj_cgroup_from_memcg(memcg); rcu_read_unlock(); --=20 2.39.0 From nobody Wed Sep 17 08:03:25 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7108AC4332F for ; Tue, 20 Dec 2022 18:28:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234015AbiLTS22 (ORCPT ); Tue, 20 Dec 2022 13:28:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233985AbiLTS2I (ORCPT ); Tue, 20 Dec 2022 13:28:08 -0500 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8524CE07; Tue, 20 Dec 2022 10:28:06 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1671560885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YesO5scRwOOin7uW7PZmPoS44XmivgS0ElHSpIPIVmE=; b=uj/7yvtKjoxajolPjtroGBuMqcW9xd/ppyLXn1op6xg0jwxNhV/i7tWBgzXE7hMxU7ZOg2 5YnAteiyU3LPromj1Y2rl9NJzDsH3ZSZvNaCXoL5YyWtmpdEj5VNLkeKsdpOM0ZXg9BxQ1 nwtLRs6SLSSxrFSJHvJFeux6PLl778Y= From: Roman Gushchin To: linux-mm@kvack.org Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Shakeel Butt , Johannes Weiner , Michal Hocko , Muchun Song , Andrew Morton , Roman Gushchin Subject: [PATCH RFC 2/2] mm: kmem: add direct objcg pointer to task_struct Date: Tue, 20 Dec 2022 10:27:45 -0800 Message-Id: <20221220182745.1903540-3-roman.gushchin@linux.dev> In-Reply-To: <20221220182745.1903540-1-roman.gushchin@linux.dev> References: <20221220182745.1903540-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To charge a freshly allocated kernel object to a memory cgroup, the kernel needs to obtain an objcg pointer. Currently it does it indirectly by obtaining the memcg pointer first and then calling to __get_obj_cgroup_from_memcg(). Usually tasks spend their entire life belonging to the same object cgroup. So it makes sense to save the objcg pointer on task_struct directly, so it can be obtained faster. It requires some work on fork, exit and cgroup migrate paths, but these paths are way colder. The old indirect way is still used for remote memcg charging. Signed-off-by: Roman Gushchin --- include/linux/sched.h | 4 +++ mm/memcontrol.c | 84 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 853d08f7562b..e17be609cbcb 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1435,6 +1435,10 @@ struct task_struct { struct mem_cgroup *active_memcg; #endif =20 +#ifdef CONFIG_MEMCG_KMEM + struct obj_cgroup *objcg; +#endif + #ifdef CONFIG_BLK_CGROUP struct request_queue *throttle_queue; #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 82828c51d2ea..e0547b224f40 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3001,23 +3001,29 @@ static struct obj_cgroup *__get_obj_cgroup_from_mem= cg(struct mem_cgroup *memcg) __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) { struct mem_cgroup *memcg; - struct obj_cgroup *objcg; + struct obj_cgroup *objcg =3D NULL; =20 if (in_task()) { memcg =3D current->active_memcg; - - /* Memcg to charge can't be determined. */ - if (likely(!memcg) && (!current->mm || (current->flags & PF_KTHREAD))) - return NULL; + if (unlikely(memcg)) + goto from_memcg; + + if (current->objcg) { + rcu_read_lock(); + do { + objcg =3D READ_ONCE(current->objcg); + } while (objcg && !obj_cgroup_tryget(objcg)); + rcu_read_unlock(); + } } else { memcg =3D this_cpu_read(int_active_memcg); - if (likely(!memcg)) - return NULL; + if (unlikely(memcg)) + goto from_memcg; } + return objcg; =20 +from_memcg: rcu_read_lock(); - if (!memcg) - memcg =3D mem_cgroup_from_task(current); objcg =3D __get_obj_cgroup_from_memcg(memcg); rcu_read_unlock(); return objcg; @@ -6303,6 +6309,28 @@ static void mem_cgroup_move_task(void) mem_cgroup_clear_mc(); } } + +#ifdef CONFIG_MEMCG_KMEM +static void mem_cgroup_fork(struct task_struct *task) +{ + struct mem_cgroup *memcg; + + rcu_read_lock(); + memcg =3D mem_cgroup_from_task(task); + if (!memcg || mem_cgroup_is_root(memcg)) + task->objcg =3D NULL; + else + task->objcg =3D __get_obj_cgroup_from_memcg(memcg); + rcu_read_unlock(); +} + +static void mem_cgroup_exit(struct task_struct *task) +{ + if (task->objcg) + obj_cgroup_put(task->objcg); +} +#endif + #else /* !CONFIG_MMU */ static int mem_cgroup_can_attach(struct cgroup_taskset *tset) { @@ -6317,7 +6345,7 @@ static void mem_cgroup_move_task(void) #endif =20 #ifdef CONFIG_LRU_GEN -static void mem_cgroup_attach(struct cgroup_taskset *tset) +static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) { struct task_struct *task; struct cgroup_subsys_state *css; @@ -6335,10 +6363,38 @@ static void mem_cgroup_attach(struct cgroup_taskset= *tset) task_unlock(task); } #else +static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {} +#endif /* CONFIG_LRU_GEN */ + +#ifdef CONFIG_MEMCG_KMEM +static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) +{ + struct task_struct *task; + struct cgroup_subsys_state *css; + + cgroup_taskset_for_each(task, css, tset) { + struct mem_cgroup *memcg; + + if (task->objcg) + obj_cgroup_put(task->objcg); + + rcu_read_lock(); + memcg =3D container_of(css, struct mem_cgroup, css); + task->objcg =3D __get_obj_cgroup_from_memcg(memcg); + rcu_read_unlock(); + } +} +#else +static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) {} +#endif /* CONFIG_MEMCG_KMEM */ + +#if defined(CONFIG_MEMCG_KMEM) || defined(CONFIG_MEMCG_KMEM) static void mem_cgroup_attach(struct cgroup_taskset *tset) { + mem_cgroup_lru_gen_attach(tset); + mem_cgroup_kmem_attach(tset); } -#endif /* CONFIG_LRU_GEN */ +#endif =20 static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value) { @@ -6816,9 +6872,15 @@ struct cgroup_subsys memory_cgrp_subsys =3D { .css_reset =3D mem_cgroup_css_reset, .css_rstat_flush =3D mem_cgroup_css_rstat_flush, .can_attach =3D mem_cgroup_can_attach, +#if defined(CONFIG_MEMCG_KMEM) || defined(CONFIG_MEMCG_KMEM) .attach =3D mem_cgroup_attach, +#endif .cancel_attach =3D mem_cgroup_cancel_attach, .post_attach =3D mem_cgroup_move_task, +#ifdef CONFIG_MEMCG_KMEM + .fork =3D mem_cgroup_fork, + .exit =3D mem_cgroup_exit, +#endif .dfl_cftypes =3D memory_files, .legacy_cftypes =3D mem_cgroup_legacy_files, .early_init =3D 0, --=20 2.39.0