From patchwork Thu Mar 21 16:36:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13599180 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 315FBC6FD1F for ; Thu, 21 Mar 2024 16:38:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A5986B00B6; Thu, 21 Mar 2024 12:38:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B1E06B00B7; Thu, 21 Mar 2024 12:38:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 104B76B00B8; Thu, 21 Mar 2024 12:38:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E9FE06B00B6 for ; Thu, 21 Mar 2024 12:38:02 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B3934A1FC3 for ; Thu, 21 Mar 2024 16:38:02 +0000 (UTC) X-FDA: 81921603204.12.E4B3891 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf13.hostedemail.com (Postfix) with ESMTP id C6CAB2002D for ; Thu, 21 Mar 2024 16:38:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=trdO50Vm; spf=pass (imf13.hostedemail.com: domain of 3Z2L8ZQYKCFQEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3Z2L8ZQYKCFQEGD09x2AA270.yA8749GJ-886Hwy6.AD2@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=1711039080; 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=qDb8iz8I8QS6d+rOBOAu9Y4ek6cz/wlqKmX9e9H3T5g=; b=hdapQ/3J9U7Mj6Qj+hVvNwqXR0Y6oJCu7eG6hRWn6x1ojpHn56b36WcbCdke6c0dG/wqrw x8pXR3jML0of7FbkQCZNTk2mfHUvFdO+kv2tMSaPNbdl9sKPvS4/lPXNeB9W0LZnJuMC4+ AfKt1tTCkS4xWLmbLQengKqIaWUAJlY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711039080; a=rsa-sha256; cv=none; b=EvY8ykVje+WBEGr9pY26QR0w6U3V6RzgFEX5b8VWF2cAg9zs4CXnbNw/Sof2GGQSPdZRvD vNP6Y4daXk9bPDepTlx+fz9SwrXYIRJejqRSP0FiTt9RrsSmHNFNeTlv0w/28CX/CLVVwX XTMMtMBaGG+HGidRGWsZz81UpBGIfgE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=trdO50Vm; spf=pass (imf13.hostedemail.com: domain of 3Z2L8ZQYKCFQEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3Z2L8ZQYKCFQEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dd0ae66422fso2413457276.0 for ; Thu, 21 Mar 2024 09:38:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039080; x=1711643880; 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=qDb8iz8I8QS6d+rOBOAu9Y4ek6cz/wlqKmX9e9H3T5g=; b=trdO50VmBO7OVgGtubmaDDwQ4RXKdAWS1J9dGjP6aNdzfowW5jgN0PpG0RKxENMYJc XOFTbpT/vM+lTBR8CsHgqj6Cj6czTo8gAlZxCAsliLt/uH/2R93zGG17XcftF94xQGPz Dau81PY4C2E/hI15E28bHgkoNYXSGGK5bH/6QfHbEXXHoAXF+G1pVGdfOzW6OmcHeLWA GpZGrkB1Aq5+32UPXMzycDfI0Hw/nNCB3IyGw+13TuX4DqpBoC9CYbhsgwAlf83RMibm 6n1QZ1LI561FZA2BWzWpKt4d5UaYDZJ/Xof1xlKHZtVSMIC+yPTrRj8LrORBSjlpvn/P RR5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039080; x=1711643880; 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=qDb8iz8I8QS6d+rOBOAu9Y4ek6cz/wlqKmX9e9H3T5g=; b=Qy/ZmiyrkVOlViLH7eL63enOs7ukLgygvDPOiuz9xkd9sq24JwP87bt3eO8PT9GTFO wBQYe3j0TM7QFB1cjbPSN3h1fE2sqMEBEi5t4/oGZpP+8pyJqglWlq7UiGo5JIKKZbKu +RVwZUIOc9TEoc0g/+QVACtAQTWlNRbz/Lugd1adMtFIAMozZKnhNaibNMLoUBGyjT81 huLtO56yJZlJ/VRetLsmxHQlY5uWmhH4YT6OfJHH+hdjBM+AzEP275BHyiKby8sA8ez1 c6R4Nfd4+eyCEiZUzanpXE9sfBoohsQcjrel+DcpdDVv+byiQ/7XBggzh3h+Wmec+sIK tJDA== X-Forwarded-Encrypted: i=1; AJvYcCVPhqBDds8GXUydvq4nkTGrxiT3sbj+TPhgrkUmTRNkAmKGzxh8R/W7PmWOtm100DL1y2E24prZSIJvWZBDP+RbVCY= X-Gm-Message-State: AOJu0YzQlo+ZY+OtZomRe2lvS5h1ho2AuqzH5stblg15+kZ3L9UBX+Vz y+VNJEG6beWev9Xmc9GuDbxx5UPW9cWNM0QeuXbUettBeIpCkQRxDcbc1FA9hBWp/KZW3kRbMYW HrA== X-Google-Smtp-Source: AGHT+IHDeZBFlJ0NtCqkUUNT0gfkHkrQ10cEoA8CJOTWwHwDSwKuqC04Vl0uLsTIeS1kWMwDWt3A7ZdyM4A= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:a489:6433:be5d:e639]) (user=surenb job=sendgmr) by 2002:a05:6902:1604:b0:dd9:2782:d1c6 with SMTP id bw4-20020a056902160400b00dd92782d1c6mr9210ybb.1.1711039079759; Thu, 21 Mar 2024 09:37:59 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:45 -0700 In-Reply-To: <20240321163705.3067592-1-surenb@google.com> Mime-Version: 1.0 References: <20240321163705.3067592-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-24-surenb@google.com> Subject: [PATCH v6 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, 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: C6CAB2002D X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 3in6sjf9t4w8ppoyryiy3abpqcfn7mzq X-HE-Tag: 1711039080-670577 X-HE-Meta: U2FsdGVkX18JkfMoasJUQ1oYet2kvBh3ylYUmVOUpOHMfG3MbyOnKu95sPfjtmoDvqC/+Zoc23ErYMnk9aTCdVxsTwSeKrzkH8bMNmj1DTrUfZ4PkGtiymWOgfXOafekWgaOMiUfAkcXYz1SopLgXOb49DMPwsUnPHixj7Gxo0j93rftr86zlBtYQKySlZek52Amxw8wKawKc1O94jG19J4qF7ApDPSprzUg6RtLh+E7qnop02+ndNVIUk333Id+xVIoC7ua0N8f7XsWu4kk6bjkDRYMMw2Qu5cPU2+azHr9T26t5bcqQohd5zbzm2SG4sGbMO2Y/tSUotPPbC8xm5Ox+U4ufhqPmJZeg8azsEebdRFAwYHLfdYAfi707ta7ymRdXZYuJN+mDctDrvrfD74DJzShJb7koeQx9MPJomyhEizRFv/KvIKJ56nfj1/JzXAkQzxNdAuRPXLES9ZCv1Pr9+YuiQNTBhlhicesY2Lzup8v4YzWm9JUWMVpqyCJTwcPmFTDN9iw9rz5p4YfavpB5VgMTopjs4f6Z2PZm59Y269FE7lVnNyk0vJNRtTcAj1LebDuSnOISmUMaNgxYabVpIVuEjSxoCWl2gzae9xuOJ/dn/NCpg73VISzsrMyAEqqhJJViqOIUnob4jpCMZtGzlkHXR/HpY+Ytu6pO3R8rrHrgFKrWqwFnw3wugmPFyRu0WVCtLfYsql7A5km1KCfjO/sDYZZK95RAUREp0NwyzsB4h85RVjISeN6psz5637jGErfThYjAuwFppNlu5m+StvpJX3CaeH274a7dchejX3eWbnaUTg5xPFOGLvBtd1/ax44HGZGjX1oLKkN7nBXklGCC4DlWziElYUPwZi2n5BMQP/qSP43FEkPaFoK/brrSClYmVnrhWJGnT1jiN2BbLytsx9UB8vobz6iQqyfcOCEM8T0SkRY/SLp/vWn3ck7EeH7XblIV0/l+Z1 mcWe5t1x t4K24zXIOk/leuW5ep5sM68CdhD1F4+ODodt1YZUiOgqTRokx8+oQDldunZSK9ha4CZ02boPBiy196XxbIOwEB9P6nSw4i2upsXSS6kL1tmlIyo+Xw8hWTRNw2vOTmRqI2tasYe1D50xY5Hk1QoM5Lps/pM1rzkNQZqKExPQPvud5XGC1HSCQShYCVyrtpwhmYlgddfI3T2VLDCKlEFcGPUp1FfVGZBv8lg3r9paS5nS+JVtyn4TBAxBmAm4tw6KyTUyeM+6Us3O6zOHGQgoFehYTe1KPHNc0UPZXL7Z7hWFtoYSucY0zFSIv0JlH86eJzDWz2nGgNzv3GJkCoOFaBOhG3crgomTvE88gvwjRneYQzrj5g6Zsy8r2Ooh1HRh6YyNnMBT+hBHoU3I4zkyPZYXUGniz/PoJCHur 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 | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index 666dcc3b8a26..5840ab963319 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1932,7 +1932,68 @@ 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 (!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) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING + 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); + } +#endif +} + #else /* CONFIG_SLAB_OBJ_EXT */ + static int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab) { @@ -1942,6 +2003,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 @@ -2370,7 +2449,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), @@ -3823,6 +3902,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; @@ -3865,6 +3945,18 @@ 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); + if (need_slab_obj_ext()) { + obj_exts = prepare_slab_obj_exts_hook(s, flags, p[i]); +#ifdef CONFIG_MEM_ALLOC_PROFILING + /* + * Currently obj_exts is used only for allocation profiling. + * If other users appear then mem_alloc_profiling_enabled() + * check should be added before alloc_tag_add(). + */ + if (likely(obj_exts)) + alloc_tag_add(&obj_exts->ref, current->alloc_tag, s->size); +#endif + } } memcg_slab_post_alloc_hook(s, objcg, flags, size, p); @@ -4339,6 +4431,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); @@ -4349,6 +4442,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.