From patchwork Wed Feb 21 19:40:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13566202 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 A810AC5478A for ; Wed, 21 Feb 2024 19:42:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A70646B00B1; Wed, 21 Feb 2024 14:41:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FA236B00B2; Wed, 21 Feb 2024 14:41:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84B7D6B00B3; Wed, 21 Feb 2024 14:41:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6D5396B00B1 for ; Wed, 21 Feb 2024 14:41:47 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4363840B60 for ; Wed, 21 Feb 2024 19:41:47 +0000 (UTC) X-FDA: 81816831054.22.1E44AF9 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf13.hostedemail.com (Postfix) with ESMTP id A56DA2001A for ; Wed, 21 Feb 2024 19:41:45 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Bds0vIaU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3-FHWZQYKCCsZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3-FHWZQYKCCsZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708544505; 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=SFqjsc0WslhSBLDjO+3ojiIHoGv8W+Erk8fAgLgypEk=; b=B12v/0xg/+BroZ9UNgydbSpHxOuzbBqom1hNU6z/vMowm6OP0vRYTumZwjEaDSYouYore+ vy8qKihhLG67jEgNDfBpvY0nCMtKXFldrNgbrlw+foKdk5MLMsfZp+ysQXbLSLjLm86rez gXZ3zXRL60XALaIvGei28H3+pZD/ryw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Bds0vIaU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3-FHWZQYKCCsZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3-FHWZQYKCCsZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708544505; a=rsa-sha256; cv=none; b=hxoDPX3ptjdw0hBpNb0Z+jNoDQfzDbWu5vFPQ8go5eoCMjuoGerCE+szZNl4isUe31OtnM Lrwk1ZeGXGNtSVa6BImt+kwi5CUYUed97oL2HSFJ7Zd/tzLQs/kLxw5r6AW8jJq05QCCMe hpCk6FQtH1lM0nQXhLWlk+5z5u6Ck9k= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-607c9677a91so17929847b3.2 for ; Wed, 21 Feb 2024 11:41:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708544505; x=1709149305; 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=SFqjsc0WslhSBLDjO+3ojiIHoGv8W+Erk8fAgLgypEk=; b=Bds0vIaUC1JWTe1BxpC6ST/ywEXBP8Uiy0BEMxZwECWSgTR5jbcWqgUQYanXCICNvp nD5DBu1mIVYz3v6kiZPVFo5x8qAgN6Ux8XR/ypRujEAOXtZyzI+tEvX6p84IACjMmMP7 h5Jft9S76WoSIwEIrzjvdThSlabH9060NAgCxefn4/9rjhbBuK64HK1Kij4ijY6zN+Um cEDyj6NQFOSApG3SArE092TrzLEuaTfMSrElaf95VBOpoEZnUfwgacdHHIxTZk9AAjRO leOtgRa22vjobe9+DojeEDp+frVP15hZYjvxYbfHAQVESyQp3goBrhVsFPuawHnGo4u3 vU1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708544505; x=1709149305; 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=SFqjsc0WslhSBLDjO+3ojiIHoGv8W+Erk8fAgLgypEk=; b=CgkYbAS6m0PJu/v3BIiRYVZWQH4eqI+ugjOz4q5qV3TZZ69RqoO8Yx15j8kU9JmULx dsxTcXILsyVAgq61OGU5FsS01SYMcbQlG2ksbeJhuE90SsDnxSCk1OHDUATB1ELO1pLh 9CKC6uDMRQc66/0TxknFJVH1FG4muchRqBOGPBooXJCfK4grr1DKNKIVMazhYabvJ3pB ssDiMTmCsoBrDbyQAUGFYO5l6+4oC9o12SflmWbm6HS51qsTMXwHU5lOo8YjKoM5yuS+ 5raQ/qi1INBDQq65yGhkLh0EIhLP9q2XKIxxL2WIgLiooyKGowRrsiuP5oaLBh1qcc9E TQ/A== X-Forwarded-Encrypted: i=1; AJvYcCWOiXFA0F7KNYtMHZc1wZHe9leWIex2ABSL5GEvxpRLdtcGLFVZdP71Knk1bmcsVEGb79CMLeC/jVP9vS9XfC+olGU= X-Gm-Message-State: AOJu0YyrVMzltU/pqY7FzsHU6ihvW1FFM9coEDmAW304qM/BTMi3kjxu uK4dB68vl9je1VAXW+n5cISHmqce+tAT7ARlVH0M4ZFqiXK24P/U/Nldgsw1aqwx3gP5xoBzGw+ y0g== X-Google-Smtp-Source: AGHT+IFEtEW0GI6YMD3iPDE2fE3Szl49cxeT7/Q/vgrbPAzl796e5Nz4XS4xSM5+xHy4B42fIJc5lCjZ/j8= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:953b:9a4e:1e10:3f07]) (user=surenb job=sendgmr) by 2002:a0d:e692:0:b0:608:6894:120 with SMTP id p140-20020a0de692000000b0060868940120mr1109228ywe.4.1708544504629; Wed, 21 Feb 2024 11:41:44 -0800 (PST) Date: Wed, 21 Feb 2024 11:40:35 -0800 In-Reply-To: <20240221194052.927623-1-surenb@google.com> Mime-Version: 1.0 References: <20240221194052.927623-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221194052.927623-23-surenb@google.com> Subject: [PATCH v4 22/36] 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, 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, 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-Server: rspam09 X-Rspamd-Queue-Id: A56DA2001A X-Stat-Signature: 914u1wxtmc5s8fwscy8sctha1o3ah7c7 X-Rspam-User: X-HE-Tag: 1708544505-128283 X-HE-Meta: U2FsdGVkX1/bQ7YPEdNHZ5eGS3hjJg3XLrUESa13QpLTcsHF5JXCSl5QDxcXdC9+3mWKUmKyhYgvVcT15Fh/qR4y1ckTZRnLCAbxcwsDFLgrWsZbqPdyXEn7f9qC2VCAyVXWCErcqOu4/M9BZsA0U1M9jH8g8hsg7E1k7RXuaIOHJul1iLCJR18bTkAaHObpst2kcFgkfvRDZeXk++01EUQ9uySSrgS1nr7lTUw8VEIzkDH8rL5Y6a8OvfAH/45LyOYPLupznKkqQp1oN8XWhZrcyYdpPui9AX4JHqbWyUPjOKT4HYs6k1UoWIrp4oajro7L5t/fBNZvWDtZPGlLQCU0Crf937QEVIzPssE5xLkJENTXonf8yJE8zS0ijSSPQSev934uR1Ly0OHQ7zplZKFZZY2MmItgCjhEiYqI2RbJXIVG8wHRNtjFYLJITb2zlWGpvpKAmJ3SjOl/1C0LE2S7yIWMdanK2L0pGFC/RqrFuIhuZh6yOpeMu0LovTzy4H54akxL+yVMFksUPzD/vOgPbJT4J0oRh4CH0kxLgz+nfqmVJzmfX4k1pX3BJcpZeyuRWe1LciT/G+yEcnD78JtCIsoo1zC5Vp2pnFZZOwLeuB1DagIOdW3fQKtGCNQe+k9EeCPTzmpjKmJ3IHXGAtS12Ko6+PzXYHCrZTdr+uuFfEOd7QPas2fSu597M4yXCgvTHQyWK7wL52EBOurTchQa+xyZTwa+TNUvZbPWcSTqHFMyyr8NP09lMQHGO8asKSnwQTkVYuMDwb2t8F6xU/Wqd3TZP8mOvbsXcMI9KZ6psq10YoSwV5n/JJLWJ0i3E6jAq56v22rFJ3HsWnJ8cXwjeObSpyDnzraWK5tZnt6fgW6RK9MhBvcFlUfwlKzIVHIh7rXWJFFT9YEY9M+0ZxtQaCf3ueAJ2rt0ekKYZn8dbhSjZW9M5p8vF72RqVUG6o5XVlhmQ8KtkT55JEi CweLWuAo /pWHLe5RS5OmEHxVLRvQGokjsdAvfqb6SNMbeXG5KGDHC8lHw2vpJzWIVfPtRaumdEvhLqvqVE0WXaapSBYaknVmiR86FgST6ccICV9dMgjDKntYVAe0gfc44n2Jz1mqMwh+/m9zeI0ZdzeLe07df9YoQTO8IpGOzzjGLnTMlcSqrMk1D0u/P7LnVj7WkC10Yi56O9J9izeSwRAB/zRAzygx33W9U81fKg5JykAEc8yJy4zxoU8FM8KvlVfjSu7ooPdHB/tkW2yGe3cHROQvjLbMtTSUPWF9Yja4Lx8GzfwCquHH+Zda6MwZDuF42HtYtRGqFWtZrqYf0FKkquCFp7YiKZ0rHxXhYcoeOnSO9JHwMRBwRQbPkEpn4K0amTKITTb55PCQCCpRwgtdUHo4HWRBCryegBfMy4xf8 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 --- mm/slab.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/slub.c | 9 ++++++++ 2 files changed, 75 insertions(+) diff --git a/mm/slab.h b/mm/slab.h index 13b6ba2abd74..c4bd0d5348cb 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -567,6 +567,46 @@ static inline struct slabobj_ext *slab_obj_exts(struct slab *slab) int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab); +static inline bool need_slab_obj_ext(void) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING + if (mem_alloc_profiling_enabled()) + return true; +#endif + /* + * 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 (!p) + return NULL; + + if (!need_slab_obj_ext()) + 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); +} + #else /* CONFIG_SLAB_OBJ_EXT */ static inline struct slabobj_ext *slab_obj_exts(struct slab *slab) @@ -589,6 +629,32 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p) #endif /* CONFIG_SLAB_OBJ_EXT */ +#ifdef CONFIG_MEM_ALLOC_PROFILING + +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; + + 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 + +static inline void alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, + void **p, int objects) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING */ + #ifdef CONFIG_MEMCG_KMEM void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, enum node_stat_item idx, int nr); diff --git a/mm/slub.c b/mm/slub.c index 5dc7beda6c0d..a69b6b4c8df6 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3826,6 +3826,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; @@ -3868,6 +3869,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); @@ -4346,6 +4353,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); @@ -4356,6 +4364,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.