From patchwork Tue Oct 10 00:09:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13414698 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 520C2E95A9C for ; Tue, 10 Oct 2023 00:10:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BAE18D00A1; Mon, 9 Oct 2023 20:10:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 312648D0089; Mon, 9 Oct 2023 20:10:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 163498D00A1; Mon, 9 Oct 2023 20:10:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EE6798D0089 for ; Mon, 9 Oct 2023 20:10:10 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C2ACB1CAA94 for ; Tue, 10 Oct 2023 00:10:10 +0000 (UTC) X-FDA: 81327619380.26.88D1808 Received: from out-207.mta0.migadu.com (out-207.mta0.migadu.com [91.218.175.207]) by imf06.hostedemail.com (Postfix) with ESMTP id EFFBB180014 for ; Tue, 10 Oct 2023 00:10:08 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="M/HNb3YK"; spf=pass (imf06.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.207 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696896609; a=rsa-sha256; cv=none; b=hF214hcgT08069fL9SUOqnT+O5hI1w7nKg70xI3Vr/or52KlvriZkM082UcIA7Jk66KqbF trdbb1Za+0a3LOtT0lwHLUh/Zk28TmCPWxuv26M2DeQT07t5+gX3hHkfkTriCGOG+Po5OI uBt/yGPvwltbGjCnrlnJhBrdM8gFpHI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="M/HNb3YK"; spf=pass (imf06.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.207 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696896609; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vaNtRxcBkqs+qNsGw0HnKynEFDwRPPwwrEaTCT/4xFw=; b=WKZ4+3JC9Uae6voc+pqT1veiD3EqptuC/oNxeanhnAk2SJVN7dtOzDdhFXqbYnr7ODJDID 8/4vcchVB27Jc15eH17XxdmXRDuLkVMC32b6iDjuZdMPCJa3wAwuursZYwtF0lNd7zk9PX 9n39I7eXr8tpyk7ApnFBdBhLiUeKD8c= 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=1696896607; 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=vaNtRxcBkqs+qNsGw0HnKynEFDwRPPwwrEaTCT/4xFw=; b=M/HNb3YKc2bGW5FqYd/GesbtjL4WMaYzcJf13vLo0uLcpBE3PKKz+0p+JT6Cdmcu7vY5Yn RNsW9yYFROw7J8lJJuMjKiT3ExifQsuvx6IMfw/EZFZy1RZTpeDFncxOFPz8hXFU61Wa2S ta2O/F0zqFj/J+i3jJVN9WgsTLIfc8Q= From: Roman Gushchin To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Dennis Zhou , Andrew Morton , David Rientjes , Vlastimil Babka , Naresh Kamboju , Roman Gushchin Subject: [PATCH v2 4/5] mm: kmem: scoped objcg protection Date: Mon, 9 Oct 2023 17:09:28 -0700 Message-ID: <20231010000929.450702-5-roman.gushchin@linux.dev> In-Reply-To: <20231010000929.450702-1-roman.gushchin@linux.dev> References: <20231010000929.450702-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: EFFBB180014 X-Stat-Signature: yd3erdds9sz5y1f6xw7ewgpb9hqrcbwt X-Rspam-User: X-HE-Tag: 1696896608-156837 X-HE-Meta: U2FsdGVkX18QoCWqS9Ky1CyRLoqJzfIKmG275OjXa/eqloV2kjs7d/Bbz8n5KkC0YOEiS3+pabrhdww18yr1HuBSIWCqBLs6L6w9UeIAIkMEHI3/MHp0KXN6JObRN2MD5VO6na718d+FNIaZxJUxCI88vRomHmp5ZftojdEHnmFWcDqAZ6XfH97tL82Db6lNifpry82AeibWTLxh/x9uv5gstde4IXoKYz9pkgS7tOKNhMSixJ/YmsRllA8r01KxR2wckrtbQSRYhXDYdIONhnbVUh1kt9hExWiMa9IlmYIy5voaJEyAjjcSRhBaPtIBPwDVALE4n9hQDrxL4pEC5uoC0HZgFpOEKeVC2VvmN/GDVM4U/sF4u6tX8HAXXB5Rntbtc9yM8d9A9/r5StgvJqlm6rHN9HBIH7RR1TbtOB2dR4Ym0QzvTvZju+gLmElZeHTnQTB+0Imrm8LSfXvTeROa9mYP9AE02oSsP20L8vhW4C0OHkiNBXZdqhtkno/CLX20hGJsO+/kwX8EhE2IrY31qzIlKhLV8i04V69SDIEI/bwBZpBdXcqs78dBw8s7sfAd4ukjYNzb2erTQM8BKXIAf1n8hwoVgsFGLZCrJ5vR34VF7Aqr5RzC0+daoirEJw6y0gjj4gjdckzyXtnbSkCnWv5HymE62ljsxo0DedRBwink83I9mchHJtjhyiHGmutnFOguwNbLQFIPboYfAh0k0xiz310bt6lUpOfNRJ0SiuodeyDgIKodmRZxFuBQaHWZSJcD8OgdR46f39pDebiSYLvcN8cmw9P9epsNH8c86uMSjfziIy/r2hjp0+gAP5CxXIUIgFGZLn21t64IYePIq47MPFPMhFAbPi6GGcH5PCQooX0BYKmr1lpvCjIGoAeutonhIz9iqJn2OowL4gReGvzq81GazRKCEd9OExaUxTGLcKdpEHyBkEJPp7gKdTmjJ1GHtKqGuhI6PqL n2TEvPNW qo8oaKRs6L+dPJFSHk6QzwZqN8kwt3DdsomM5WBhJKfCvsL4S1fEay2tliN0CLhKfOVmZ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Switch to a scope-based protection of the objcg pointer on slab/kmem allocation paths. Instead of using the get_() semantics in the pre-allocation hook and put the reference afterwards, let's rely on the fact that objcg is pinned by the scope. It's possible because: 1) if the objcg is received from the current task struct, the task is keeping a reference to the objcg. 2) if the objcg is received from an active memcg (remote charging), the memcg is pinned by the scope and has a reference to the corresponding objcg. Signed-off-by: Roman Gushchin (Cruise) Acked-by: Shakeel Butt --- include/linux/memcontrol.h | 6 +++++ mm/memcontrol.c | 47 ++++++++++++++++++++++++++++++++++++-- mm/slab.h | 10 +++----- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 277690af383d..0e53b890f063 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1769,6 +1769,12 @@ 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); +/* + * The returned objcg pointer is safe to use without additional + * protection within a scope, refer to the implementation for the + * additional details. + */ +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); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 490c5f67be79..84724fb875ce 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3079,6 +3079,49 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) return objcg; } +__always_inline struct obj_cgroup *current_obj_cgroup(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(); + /* + * Objcg reference is kept by the task, so it's safe + * to use the objcg by the current task. + */ + return objcg; + } + + memcg = this_cpu_read(int_active_memcg); + if (unlikely(memcg)) + goto from_memcg; + + return NULL; + +from_memcg: + for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { + /* + * Memcg pointer is protected by scope (see set_active_memcg()) + * and is pinning the corresponding objcg, so objcg can't go + * away and can be used within the scope without any additional + * protection. + */ + objcg = rcu_dereference_check(memcg->objcg, 1); + if (likely(objcg)) + break; + objcg = NULL; + } + + return objcg; +} + struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio) { struct obj_cgroup *objcg; @@ -3173,15 +3216,15 @@ int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order) struct obj_cgroup *objcg; int ret = 0; - objcg = get_obj_cgroup_from_current(); + objcg = current_obj_cgroup(); if (objcg) { ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order); if (!ret) { + obj_cgroup_get(objcg); page->memcg_data = (unsigned long)objcg | MEMCG_DATA_KMEM; return 0; } - obj_cgroup_put(objcg); } return ret; } diff --git a/mm/slab.h b/mm/slab.h index 799a315695c6..8cd3294fedf5 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -484,7 +484,7 @@ static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s, if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT)) return true; - objcg = get_obj_cgroup_from_current(); + objcg = current_obj_cgroup(); if (!objcg) return true; @@ -497,17 +497,14 @@ static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s, css_put(&memcg->css); if (ret) - goto out; + return false; } if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) - goto out; + return false; *objcgp = objcg; return true; -out: - obj_cgroup_put(objcg); - return false; } static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, @@ -542,7 +539,6 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, obj_cgroup_uncharge(objcg, obj_full_size(s)); } } - obj_cgroup_put(objcg); } static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,