Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2637946rdh; Wed, 27 Sep 2023 08:22:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFURre6bayIR39X5nPggl6ZUjZ67wWvTV9KMKGfhlqyYdT4EaVP+mEQCkNRKJqbtwiXOx/W X-Received: by 2002:a17:90b:4f85:b0:274:bdcc:85b1 with SMTP id qe5-20020a17090b4f8500b00274bdcc85b1mr9454315pjb.12.1695828143594; Wed, 27 Sep 2023 08:22:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695828143; cv=none; d=google.com; s=arc-20160816; b=aluNHhzkFyf9AZTiELi+f6EiLcqSLUxVCSyPPy0iUGhuOJqwh8bRLBGs9PUaXF1Hzm 5XiqeM34OUc+kP4MU7LudJk+aFvFc75Hs+cyHJjsqFrzjReE/Dg6uD2P5ZvNf3MPXfkW TjAAQ0sVnN4Z8ToUNouRj6xlxo12F9oXee1zmj7TVlEgn8stIuJGpigaqvrM5C3jezhj oNQqiKQjoCNuE1leTJqsktOZYeOaGGUy7eozEA1K3OEIc6yvReTLN65gq4TTH4PrUsDk wp2Cu6q3M0+CkVxtt86Dlv7sxpIbjGljPfogJ0I+tJUHHyc+hwMko7LbibuGgJda4U7Z m1Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jeQJ/eIJXU0AYA9yk+s4aO7OKAgQk2qdJmGkoMOLMDk=; fh=J+ThODwbvpIaijJ/a/gIBgC20RizoNlQ1sjxEf1nY4o=; b=KloBBxiO7mtfiZbrsL4KyqXRw4gktv39030MYNkVDOed50JK6OfotrNUl6sjEsnJph frpp6RqqUbmVsVnKN9Q1OlvrZo9p/QtYX54hJdXFnw7GHmk+ExK9X7gcSJXjonoEfReG hZwoBqKlhZxi5rKCQhtW14ec8B5bcn3S1JR0G8Min+6H3U8VdmulDjNYfVlNHlp2xncZ pB/sWDyiX2UhZJ1Q6w0PoEXWeTxANQWOaim9SC4YFznxNboleVNwjPogcloswU+Q4l5M FreSQzOwrNNJh+GyCyzWl3R8ZYw7/Giya6aUAlwz94XuMS1Jl+ZrPQbH0I8jSbAN25BJ YDDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=c6ZuccPL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id 2-20020a17090a0e8200b002681fea6d14si871875pjx.79.2023.09.27.08.22.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 08:22:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=c6ZuccPL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2A2B38028049; Wed, 27 Sep 2023 08:09:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232373AbjI0PJQ (ORCPT + 99 others); Wed, 27 Sep 2023 11:09:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232336AbjI0PJE (ORCPT ); Wed, 27 Sep 2023 11:09:04 -0400 Received: from out-201.mta1.migadu.com (out-201.mta1.migadu.com [IPv6:2001:41d0:203:375::c9]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4779A1B1 for ; Wed, 27 Sep 2023 08:09:03 -0700 (PDT) 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=1695827341; 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=jeQJ/eIJXU0AYA9yk+s4aO7OKAgQk2qdJmGkoMOLMDk=; b=c6ZuccPLxLC+Ce4wWZ9aCvoGVbip0UFScbqDK/VCUHTzImAhP4V7Ea1dW8CIa6yy8rjLZJ 0Sp2j/SlVWnSus6gz0MlC0eiKozxqUtiaYSQ8bVhbIgA/srrcTqQNsoM1ecUX491WcPVhw WEyQ3zRkmTvotbuHgAt2bGqQFn9TnMI= 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 , Roman Gushchin Subject: [PATCH rfc 4/5] mm: kmem: scoped objcg protection Date: Wed, 27 Sep 2023 08:08:31 -0700 Message-ID: <20230927150832.335132-5-roman.gushchin@linux.dev> In-Reply-To: <20230927150832.335132-1-roman.gushchin@linux.dev> References: <20230927150832.335132-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:09:39 -0700 (PDT) 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) --- include/linux/memcontrol.h | 6 +++++ mm/memcontrol.c | 46 ++++++++++++++++++++++++++++++++++++-- mm/slab.h | 10 +++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 412ff0e8694d..9a5212d3b9d8 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1779,6 +1779,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 4815f897758c..76557370f212 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3074,6 +3074,48 @@ __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(current_objcg_needs_update(objcg))) + objcg = current_objcg_update(objcg); + /* + * Objcg reference is kept by the task, so it's safe + * to use the objcg by the current task. + */ + return objcg; + } else { + 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; @@ -3168,15 +3210,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, -- 2.42.0