From patchwork Wed Mar 6 18:24:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584433 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 19137C5475B for ; Wed, 6 Mar 2024 18:25:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E359C6B00B0; Wed, 6 Mar 2024 13:25:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DBCE46B00B1; Wed, 6 Mar 2024 13:25:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4C686B00B2; Wed, 6 Mar 2024 13:25:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9CB376B00B0 for ; Wed, 6 Mar 2024 13:25:37 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5B20441017 for ; Wed, 6 Mar 2024 18:25:37 +0000 (UTC) X-FDA: 81867442314.29.EFFC373 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf27.hostedemail.com (Postfix) with ESMTP id 5F75040016 for ; Wed, 6 Mar 2024 18:25:35 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hP0D7H4Z; spf=pass (imf27.hostedemail.com: domain of 3HrXoZQYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3HrXoZQYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709749535; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s15jajKF49lJj6S4Z8eIgzY9HreX5MNZRA5AU2XXX3Y=; b=ByyIX4zP2FdquxbcYMNOjdm5er+qyupJ4DJOh9ipWkLToCe1C8i7B+vGP1RT8FaxKcpOCm 8DjZQ35eB7Q8ZgVSZVDxxtcQAMt3uYgTPXeFmmtbqgVdYlAOi/x+EaF+nA7ZWut8LdrWcB XgA1Y1CRuw4ZyAWZ6BTYwESEGo0YeFk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hP0D7H4Z; spf=pass (imf27.hostedemail.com: domain of 3HrXoZQYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3HrXoZQYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749535; a=rsa-sha256; cv=none; b=Nm6TtjR4yVBEbjYJEzoIHSctjcVhkBAHDXPxr0t/8zLqqft8nbuHMyKHw+8gi8WhtKc49A bhXFaeRhV6whNT3cAL2QKTxtgRQBtX6VSIuhX1+hYYgwDoHx1olhIOebIUPR8ysY+6cr58 558nLnTgM1ek2hbZZuzuqkcHAt3ZJEE= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5ee22efe5eeso74657b3.3 for ; Wed, 06 Mar 2024 10:25:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749534; x=1710354334; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=s15jajKF49lJj6S4Z8eIgzY9HreX5MNZRA5AU2XXX3Y=; b=hP0D7H4ZZ8o8J5TE3qGzLrdt5sK89y3w6Lx3+gm9CdXDp7O8McC8brB7LiIerUT6NG hNkHjt+XB1n8FbxkxGcFovtHBgsvXgZlozJnHuV8v7G71XMG2AgZ5nMvtX9GT7qnT4TJ zP15O1pnXw6Fwd5b6ec6jaj9mbh2UO3fCrVgD97ElUoVF2GJmw1AaKizrr6Fsh+zRnLS ygguwySijkfwcF0VHbnlhwf7VaV7l+GX1snJ6VAqi1sRJ42YOsGLU0D9Z2oNfSaNiN9U ac96b3AGK2LkQidzZ8wHU7N/AUIXtYvifDSldvEgcd0G+bt7clXAudNXO7TdqH3WfUbj X46w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749534; x=1710354334; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=s15jajKF49lJj6S4Z8eIgzY9HreX5MNZRA5AU2XXX3Y=; b=dfYHCNf/3TI+ievIHRedVS47YLnz3iTNY4NwykIyhwu8z1YNImNDdTHLQpBHdhxW81 KfToohxgLP7j9ClGU8lY5exv+1C4tLidIpx164awE8WsIiuHLuIZjOTparkUzCAQ/yGv F230+Nzd9ymRX7f9Usgndy6KJBjYgIbxtD3zxmL64UmL3jElrXH0hqYhseyrgaR+1IMq KWmbH2oXMT+rJg5pi3XsKa074LGg3R3lJaA38nnOUwzPKcD0ZBysDrvEcc1fAKQDBq7s lV/JLfrtOYsqN5Sj7E4j20QuDcKtJBpIXVSWiO1iRVNPgFHKBm7koP6eiYCB5adgBLAS SqtA== X-Forwarded-Encrypted: i=1; AJvYcCVEqGdqul+PAvXA8IaSfCPrPszC2KFpSYQsNGgC2NgDGNdODpTGirlFPdIobHcl7LA0NdtsNS2OtcfRRKpW7/iJpXY= X-Gm-Message-State: AOJu0Yzrz85p/R/1+tRCuB97rQS3VFG2rOwboPA+Ek2OkmUD6MdLiDWc 34KtgNzRBjGVSrmK7u9hpnZ5P5Av2XULzaKZM7hbufJXX2LLJDI2g6DftZDvMgbLu9Y6SfFrzAK qnA== X-Google-Smtp-Source: AGHT+IGmcq8HQqCEBWeZhNDcRf4OQCj0ptCPLBS+QbTgQQ1y6EL/L/eLL61CpiC1ccJurAx7LcZUvWTR4g4= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a05:690c:82:b0:609:78d7:4e9 with SMTP id be2-20020a05690c008200b0060978d704e9mr3296801ywb.6.1709749534383; Wed, 06 Mar 2024 10:25:34 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:21 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> Mime-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306182440.2003814-24-surenb@google.com> Subject: [PATCH v5 23/37] mm/slab: add allocation accounting into slab allocation and free paths From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, jhubbard@nvidia.com, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org X-Rspamd-Queue-Id: 5F75040016 X-Rspam-User: X-Stat-Signature: wdifsa9f1tutoyekxm5x9m8oxsezmnq4 X-Rspamd-Server: rspam01 X-HE-Tag: 1709749535-648671 X-HE-Meta: U2FsdGVkX1/6fZ0NDXkvvWVSYfuoRBjTK9rJomkcHpfAdtQTG1AJLE4RSV31YqeLbfV17ZycnkJUB7en4zDCcA16VrPcq8EEhasKYepLPKDf6T0sXVh0NLQsVZq2ASXuAYUGq8uA5XUAbtAgmiNaKXPHFiLBgUbt5eRxybknAnBKW0YKolnNVAZ48EK+HYGDxyoqah1xfNuzzFGuPmfYA6FQYKiMzSJHiQDpRzNneEA4oyuWja8l3IS9dFqBcOEr/muxscEOjeo92A+5fajd6QjG6JwZqUMOjCVXBy0AKFaYUH6QdTidZ+ydmzqjhLsZtQsNe757dKabRpLm5KW3Qod8fhdvCmo8AvyD44CfNTfDZLuCKBLAMNp26gnjL5DozXXMTcZ//ti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AUv02m+f0rdQsII9pgDrNFZU4f6BaYlXDrrgHPF/beTzLfb7B/Trfn4blhboqoZ5/5wzShdArhXLNGwiBHFiReBAPomUaaSvOEVtkPof33KcK1TfJmg14yFq26a++eB5T+cWN7ogvYj2yi+T/z6eYysviG172B/X7ZUlc4OMui1USqM9bFGjKwVTrQ3FDRA1OE+8sgVMUIuQeNlyNGQYvvrXmYHH3HxRXz0BdsSTtQXBJJPaS4/O1jIJREQujMT5OZM4En6cMLUvlKCIJruuSJkPBU= 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: List-Subscribe: List-Unsubscribe: Account slab allocations using codetag reference embedded into slabobj_ext. Signed-off-by: Suren Baghdasaryan Co-developed-by: Kent Overstreet Signed-off-by: Kent Overstreet Reviewed-by: Kees Cook Reviewed-by: Vlastimil Babka --- mm/slub.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index e94d3cc1b270..ea122aeb89fc 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1942,7 +1942,69 @@ static inline void free_slab_obj_exts(struct slab *slab) kfree(obj_exts); slab->obj_exts = 0; } + +static inline bool need_slab_obj_ext(void) +{ + if (mem_alloc_profiling_enabled()) + return true; + + /* + * CONFIG_MEMCG_KMEM creates vector of obj_cgroup objects conditionally + * inside memcg_slab_post_alloc_hook. No other users for now. + */ + return false; +} + +static inline struct slabobj_ext * +prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p) +{ + struct slab *slab; + + if (!need_slab_obj_ext()) + return NULL; + + if (!p) + return NULL; + + if (s->flags & SLAB_NO_OBJ_EXT) + return NULL; + + if (flags & __GFP_NO_OBJ_EXT) + return NULL; + + slab = virt_to_slab(p); + if (!slab_obj_exts(slab) && + WARN(alloc_slab_obj_exts(slab, s, flags, false), + "%s, %s: Failed to create slab extension vector!\n", + __func__, s->name)) + return NULL; + + return slab_obj_exts(slab) + obj_to_index(s, slab, p); +} + +static inline void +alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, + int objects) +{ + struct slabobj_ext *obj_exts; + int i; + + if (!mem_alloc_profiling_enabled()) + return; + + obj_exts = slab_obj_exts(slab); + if (!obj_exts) + return; + + for (i = 0; i < objects; i++) { + unsigned int off = obj_to_index(s, slab, p[i]); + + alloc_tag_sub(&obj_exts[off].ref, s->size); + } +} + #else /* CONFIG_SLAB_OBJ_EXT */ + static int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab) { @@ -1952,6 +2014,24 @@ static int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, static inline void free_slab_obj_exts(struct slab *slab) { } + +static inline bool need_slab_obj_ext(void) +{ + return false; +} + +static inline struct slabobj_ext * +prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p) +{ + return NULL; +} + +static inline void +alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, + int objects) +{ +} + #endif /* CONFIG_SLAB_OBJ_EXT */ #ifdef CONFIG_MEMCG_KMEM @@ -2381,7 +2461,7 @@ static __always_inline void account_slab(struct slab *slab, int order, static __always_inline void unaccount_slab(struct slab *slab, int order, struct kmem_cache *s) { - if (memcg_kmem_online()) + if (memcg_kmem_online() || need_slab_obj_ext()) free_slab_obj_exts(slab); mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s), @@ -3833,6 +3913,7 @@ void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, unsigned int orig_size) { unsigned int zero_size = s->object_size; + struct slabobj_ext *obj_exts; bool kasan_init = init; size_t i; gfp_t init_flags = flags & gfp_allowed_mask; @@ -3875,6 +3956,12 @@ void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, init_flags); kmsan_slab_alloc(s, p[i], init_flags); + obj_exts = prepare_slab_obj_exts_hook(s, flags, p[i]); +#ifdef CONFIG_MEM_ALLOC_PROFILING + /* obj_exts can be allocated for other reasons */ + if (likely(obj_exts) && mem_alloc_profiling_enabled()) + alloc_tag_add(&obj_exts->ref, current->alloc_tag, s->size); +#endif } memcg_slab_post_alloc_hook(s, objcg, flags, size, p); @@ -4353,6 +4440,7 @@ void slab_free(struct kmem_cache *s, struct slab *slab, void *object, unsigned long addr) { memcg_slab_free_hook(s, slab, &object, 1); + alloc_tagging_slab_free_hook(s, slab, &object, 1); if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) do_slab_free(s, slab, object, object, 1, addr); @@ -4363,6 +4451,7 @@ void slab_free_bulk(struct kmem_cache *s, struct slab *slab, void *head, void *tail, void **p, int cnt, unsigned long addr) { memcg_slab_free_hook(s, slab, p, cnt); + alloc_tagging_slab_free_hook(s, slab, p, cnt); /* * With KASAN enabled slab_free_freelist_hook modifies the freelist * to remove objects, whose reuse must be delayed.