From patchwork Tue Oct 24 13:46:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13434592 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 1DFD5C07545 for ; Tue, 24 Oct 2023 13:47:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21E086B0281; Tue, 24 Oct 2023 09:47:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 135836B0282; Tue, 24 Oct 2023 09:47:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEDDB6B0283; Tue, 24 Oct 2023 09:47:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C91976B0281 for ; Tue, 24 Oct 2023 09:47:35 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8E86C1205F5 for ; Tue, 24 Oct 2023 13:47:35 +0000 (UTC) X-FDA: 81380482470.16.51524F6 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf28.hostedemail.com (Postfix) with ESMTP id D03B5C0004 for ; Tue, 24 Oct 2023 13:47:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OBcROUbK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 39Mo3ZQYKCJkLNK7G49HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=39Mo3ZQYKCJkLNK7G49HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698155253; 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=jxcZ43bmgT/q64Ul96+yH7BdLzW8wJzWTibIuRMuFN0=; b=1F6CCmJQy2p9UNCjiJECWw+hnyUjncNk/xdSedIxdIqYoxRg1a2JhCS0okTUW113ZV6hTM 828sm+NPBIArwadZjUHdGhpe1H8IndfPAJXGEIepVgHqL7jQMPHVe5G64Ps0vy6RopNW+N 9StGOJ1DzzjPkkLe832SLL/vVsIt4VI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OBcROUbK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 39Mo3ZQYKCJkLNK7G49HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=39Mo3ZQYKCJkLNK7G49HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698155253; a=rsa-sha256; cv=none; b=PC4X5gQn2nfCsYk+luYbNSG4TRu2CwWAXyYaZY8nJ1ig9h516fW7hHAxxx/XvNvbVtpc2l +/c4qHSPJWD+29SJxYW2SsGnRBohwm4KWY1BybQxmlsxNorG/PcD1FNTjn8mrTt4H4EpF/ otgsn6rb9u/VWUNPtG9RRBGlnaiArfg= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-da03390793fso1013537276.3 for ; Tue, 24 Oct 2023 06:47:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698155253; x=1698760053; 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=jxcZ43bmgT/q64Ul96+yH7BdLzW8wJzWTibIuRMuFN0=; b=OBcROUbKNdAqdzkrkQOwPJni4l3cOJwTjho2FddfM4hpGjHOIsUL2sxxylcPh2gjt/ TF0ikWjuVHc4l0n+ZzmsYURptMinpo7IILCz7GcaLjOAfZXP5MWa9RHAa56JCw//M6P7 q6vanf3tCkZcpWu0loGQ/XrUK4IFDbUak6nfUS+3tC2/Hhe5/fPMEIXWAQroY8GGaQfX HdehAvDCRld8Aqlr2d+fAc4IEIfe+YS9Pqy8axYEUca39z0qZi/OnYpcyzNywCQn9jBJ mQF8KdkGpeqQq5NM5VCoKhE2Z6mnBzjqLsZn8Rpli/7glDDQ69zNRwlgZL2Y3Atn962O JfOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698155253; x=1698760053; 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=jxcZ43bmgT/q64Ul96+yH7BdLzW8wJzWTibIuRMuFN0=; b=rCkHULOUmKpgxXapKWmGJSccy6wwh1LypvDlpN3neUrienmxeTk2h97b16b7gdOIlo EIP8PWkub2fhxhK0Saqxzj1Iy5WW3l1x3TnUBsy1wQvkfk+NvXKJJlJ9QW6TkhUhRKv9 mSfNadlt0JtMo7EqK28HkUME4yenGyX1gomQshQHCvSJU+J9jzkNXGvWxmouOtoJFsZU HcN2o8pk5b8uJXb/+9076SiATRKDxgTTrMfj+s1Bi6CVo/7ha1C+jmbq82Kxuo/Gqh6k jiTzZXtZMUar1ipM1Eihtkeyy4YgEu2JKxiMKHCU5Mx4ZWm6a9taPP5gyeu34Cs/mpxY C4eg== X-Gm-Message-State: AOJu0Yzlsl8xqkfwwKX7nJbFvpU37cKn6tE/TYpWCW42L+7BKjsnsthq u2r4+b1avBfwjCPaeGU6VhRxP22Qft0= X-Google-Smtp-Source: AGHT+IHLiWJmtuPzRqvJDSScUKUh0y8YokFyWjfHx2rxdH2c6l653IiepUePcxPp47iOlZTgh6t0s/pZmOY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:45ba:3318:d7a5:336a]) (user=surenb job=sendgmr) by 2002:a25:d05:0:b0:d9a:5b63:a682 with SMTP id 5-20020a250d05000000b00d9a5b63a682mr216305ybn.13.1698155252840; Tue, 24 Oct 2023 06:47:32 -0700 (PDT) Date: Tue, 24 Oct 2023 06:46:20 -0700 In-Reply-To: <20231024134637.3120277-1-surenb@google.com> Mime-Version: 1.0 References: <20231024134637.3120277-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231024134637.3120277-24-surenb@google.com> Subject: [PATCH v2 23/39] 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, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, ldufour@linux.ibm.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-Queue-Id: D03B5C0004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 5znkmu3s5ypyuq3ssh8sarbhcxp4e99i X-HE-Tag: 1698155253-593740 X-HE-Meta: U2FsdGVkX1+D77zX6fv090eCksxcWaniApENkNyjuSem2QqtvfA13UdQlJgIskomsspIVJgecMSaSBEuVHimtVb+D68OJL0rMAVSW5wIQK7GbTBqq4x80Z34LBrUspBRfYRfS9X4BY5blR2rI+vHdPlFXluompy17rt7ajmZhxWtIvc2xfhmNrvQBk3OxXTRTjhtknLmgm6GNVqXlMbLgJ3cMlIRMhZIvBvDYw4xdnEE32QriHeX3ydPwA/0kiZtCQd/3LXd+kbE6xGRU1oB1qgr4RJjHDp3RpYFbEZszdeUNi5WMxwO/SNKs9hQAMhD3XQkKrhbatPR0OQlKx8TxNS7k1OzSz8OZCuKwqC1VOM4rb6KAhs2Itcp+taS6fxoD07XqLNX/v2iKThgmHTfbHiTc9zeNgv2pa+w/AD9Si+twfQRh8G/+FPt/edht/VIWM3j1j7bz6t/lErV+0WIm2AgvdEp1O9gyqaNYWpAuKkijyQYoq3LOt1U+lPujteiIkPQoA45aPUFWDlkSlASEw4JRxNKPcyYpz8m5U57A7DWkODh6sbti3YGzGvx1Mjoi8cCN63gKaBL7l5XCy+Kzr7q8U3WXTcRBCk6UqL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VSlmtAKZJwCYI= 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 --- include/linux/slab_def.h | 2 +- include/linux/slub_def.h | 4 ++-- mm/slab.c | 4 +++- mm/slab.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h index a61e7d55d0d3..23f14dcb8d5b 100644 --- a/include/linux/slab_def.h +++ b/include/linux/slab_def.h @@ -107,7 +107,7 @@ static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *sla * reciprocal_divide(offset, cache->reciprocal_buffer_size) */ static inline unsigned int obj_to_index(const struct kmem_cache *cache, - const struct slab *slab, void *obj) + const struct slab *slab, const void *obj) { u32 offset = (obj - slab->s_mem); return reciprocal_divide(offset, cache->reciprocal_buffer_size); diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h index deb90cf4bffb..43fda4a5f23a 100644 --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -182,14 +182,14 @@ static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *sla /* Determine object index from a given position */ static inline unsigned int __obj_to_index(const struct kmem_cache *cache, - void *addr, void *obj) + void *addr, const void *obj) { return reciprocal_divide(kasan_reset_tag(obj) - addr, cache->reciprocal_size); } static inline unsigned int obj_to_index(const struct kmem_cache *cache, - const struct slab *slab, void *obj) + const struct slab *slab, const void *obj) { if (is_kfence_address(obj)) return 0; diff --git a/mm/slab.c b/mm/slab.c index cefcb7499b6c..18923f5f05b5 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3348,9 +3348,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp, unsigned long caller) { + struct slab *slab = virt_to_slab(objp); bool init; - memcg_slab_free_hook(cachep, virt_to_slab(objp), &objp, 1); + memcg_slab_free_hook(cachep, slab, &objp, 1); + alloc_tagging_slab_free_hook(cachep, slab, &objp, 1); if (is_kfence_address(objp)) { kmemleak_free_recursive(objp, cachep->flags); diff --git a/mm/slab.h b/mm/slab.h index 293210ed10a9..4859ce1f8808 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -533,6 +533,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); @@ -827,6 +853,12 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, s->flags, flags); kmsan_slab_alloc(s, p[i], 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);