From patchwork Fri Sep 29 18:00:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13404693 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 E615DE728D6 for ; Fri, 29 Sep 2023 18:01:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D2C18D00FB; Fri, 29 Sep 2023 14:01:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25BAE8D002B; Fri, 29 Sep 2023 14:01:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 030458D00FB; Fri, 29 Sep 2023 14:01:30 -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 E67138D002B for ; Fri, 29 Sep 2023 14:01:30 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B3FA9C0E9C for ; Fri, 29 Sep 2023 18:01:30 +0000 (UTC) X-FDA: 81290402340.17.8B72F94 Received: from out-201.mta0.migadu.com (out-201.mta0.migadu.com [91.218.175.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 89784120014 for ; Fri, 29 Sep 2023 18:01:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=reulyu8B; spf=pass (imf29.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.201 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=1696010488; 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=O/HghY6af2rzSMfJ7xP4DH1sDr9DxTKbSixxOwxk1Wc=; b=QdyZfEDeWIIKQUH3noORAoJ0/1zPCx0AIhB3DCTK0VIPrIM4ZOf9yPVeU+J0snhYBiPV+1 YMzgUOlpJ7iQ5S11uRRVY/D5UMDa3ELGT17coxLwxJJbjZ3B9MsRat/MNjho1aCz1DLWJT cE8OLhtb6BPLXd6XiopACZ8Osl+vSmM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696010488; a=rsa-sha256; cv=none; b=VVwLcOXOQhrmRQZfkou6PziMCDZeIqMUvvYTmxhoFdy09FzB/XWNYPS/OUX/winsHN73Hv v8bY8QOUSvNRMJyhyB+tLTtQBn16670vfaeeCnVu2d6HFDyN1vMAVyNLfKTblvy1PmgKXV o6i1MgCwZtjUmFAy979l5gMZRb9OTAg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=reulyu8B; spf=pass (imf29.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.201 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1696010485; 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=O/HghY6af2rzSMfJ7xP4DH1sDr9DxTKbSixxOwxk1Wc=; b=reulyu8BPJ63WGorYfZOnZ+y3TQQyJvBgiYOWl05pCLFalHnzIPEPs/r+T2JEd8zTAmV+5 xq4tlzloY5TTOTNFcVw0n7ImC8bEtRN2wTux41sKvpuWQw+ZJwVV9YgZkFzFehUsdj59fc /cKiihLvc8ycwOyaCRkSf2htJ6JeWMI= 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 , Roman Gushchin Subject: [PATCH v1 4/5] mm: kmem: scoped objcg protection Date: Fri, 29 Sep 2023 11:00:54 -0700 Message-ID: <20230929180056.1122002-5-roman.gushchin@linux.dev> In-Reply-To: <20230929180056.1122002-1-roman.gushchin@linux.dev> References: <20230929180056.1122002-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 89784120014 X-Rspam-User: X-Stat-Signature: hkdqstgfarzmccf69g7rg5edmzdwuxby X-Rspamd-Server: rspam03 X-HE-Tag: 1696010488-449391 X-HE-Meta: U2FsdGVkX1+aeVO93f2wyiM9jnj//9XU5g2KdHdA98dCQcR3K4JuDKnMOoWtLHus4HzdG1xb9nJrAkAB5oMLEzkaxfNvvTh0iTD5zQ7gMhRsfBML+PKoUnVIr4bSRPWnClpkgtPwcguxI2bYJN52Neab5+g4AnQdirht2jltt5Imz8/ePWltM4++AAuJBIloQ45uHWALmLF8/TZLXCII9ZesGibZ9APeV4TJeMkk94o61zqSMzfUjSnLln91REnGixhYmmPsUKlBBm+01KGAYmkxSXLm+K1FmitDEQrMPLTzWDYywmBBwvg0nrJcpwg7txsVphT2tengAJkkY32Hths00ajw5Jyeb9GoYqVaIJPkUj2lheAgIlS3yOFDATIx8OJgySWxbEs527z44WbVyRFor2vHroLN5KxhDPa1v5QsLoukg0TAWcOnqCgX9+ORTlc4EReA+EwNO4ZzRdBm4OCUeojubTSN7QGcBrzWH9EAM31zmTuezptvnKrYgQSuhLAdBT8h5EoAkFH8YWzRpy7BDvrSezz7bBEBNA8nVvgvdJUmkK3785jiKbecymsvZqitVJ7b1W+J2g6jQj5ebgk+pO8jCAJ1M1TG9QHWk4DoXM4t63b76vrVtzOy1UTeNnw7m/56i463peoacFw+ZCaXInueewOwEqBfWQRvJnT8zS4XI4DYFo1cEdBBgVsyRd311w6Tsi1LHyPqg8VHMvOrZdoUaJRngJ+EfHO3+lvCzy8G1JUNnxT9Kwa0kbEj0J+zqxUFC/dgXwGI/vEA/wbnczIjc6LlpM36kX+5Oj9x6TSwyLtdievfRzh5NHN9OXFl9FPHkpuJQsPEuDyg/cxkm4FL8na80DU9mm9SoZ0cOpO1eQ2EZPkbaLBrvsYs1fP6+8hl7ij4p4SPHdy2LpcZ6WeCfZ/ylCfHsmOa2lZwStrqPq1+rssmzDNYU5S1XL0MXE5xjV+tRZlG8mr FLKqcfkR qtcdRUYmWKWHOim7A+VSvA+iEYnrhDzPsqWGihfXt21Nkyl84CZ8M4tiovyvJFT/mZC3fe3xK1GtTFlBUbKPjO6KkSA== 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) --- 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 e59dea9d8666..5a52327ab09a 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 e9890f6e4da7..78ab36b5899f 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,