Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp80354lqi; Wed, 6 Mar 2024 10:32:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW8FvMRXLVMnwxZQ5LjjxuXeVgbYntPirG1MbBUfZksfXwHyq8pjW/7CxuIvudbPuENu+aIAp/ZkJA+aJoH7reW9ICqo3GnJjJ1YtGFdw== X-Google-Smtp-Source: AGHT+IHw/CR0ED1iBsTbJqyTqjV1oirjTB/oMnkuU9BzUoTF9srz/4Np+yGIWT2jFpSUgY59sRTi X-Received: by 2002:a50:f61c:0:b0:565:f51:3b26 with SMTP id c28-20020a50f61c000000b005650f513b26mr10589645edn.30.1709749972958; Wed, 06 Mar 2024 10:32:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709749972; cv=pass; d=google.com; s=arc-20160816; b=XYH3nkxoWbjSb6IyF7QqYodoB5W6mUnsQoAihWv7Ub/VqSaaDBk/ymEr10vRSlIINC Gdmv6SFwobBY9tvjZU5rU8jNHc6Jpqt6ShpGAM/XsW7ZOyjvRSgIaSpzbd8WcrCQbO4d 7TG5GWgVrntiwrBYHa8GrRagDQPz+bW7XCWEn4scNw01vHOpnYRerqSxDqPOYep/i3Ji hMDGtTq87DFzMMbgmMs3sS2iWNFOs6NCjFDs2BnDoqBT3MTvzCkNS8VYJqT2t2xF76XV Ya9H+8nRMuEEJOQlFNzP+fLdreKteOS7xTYRu6prB7lWAxseOcBS7A1nTPaPu+kLiNnq 0Dew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=s15jajKF49lJj6S4Z8eIgzY9HreX5MNZRA5AU2XXX3Y=; fh=6PfY4S9QUU/GYitr3yNMMMTmsVMw5rG3BIeEQNXbA9k=; b=0BcVoVtthLR66oR3kSAeYdvpuZmiNThSEosAUn+o8+VlY97HUtsXoL4f1tqfmG8ki9 J4kIET/vPzwuz7ywmzb3pNIw0ViEfBwOJCx/CDhWwF14fhvrLxdSuvs5Z1pyNxnwjw9G +Bk79C29WWSluSsk4LQlVKaYkc5u+4769T2zkqSL6e9VsISZV5WZRhTsf3MsgAkE6HOX Pfoahh5keOohIa8Ud5xpyCBuXnc7LWLIarBpfftnVhX5CxsXi8iaFJIW2ijbb7MspSWx Pp0heVv1pNaVqNrPVKn2+bMw0Ox47j4/XjpPfU5VkKv58ahduEFPQOKijLi8jCy2SyzH KJnA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oqjpJoGW; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94415-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94415-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i16-20020a05640242d000b00568130dcc40si94027edc.564.2024.03.06.10.32.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 10:32:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94415-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oqjpJoGW; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94415-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94415-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 763641F22C55 for ; Wed, 6 Mar 2024 18:32:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F1AED14C59E; Wed, 6 Mar 2024 18:25:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oqjpJoGW" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 401DA13E7D6 for ; Wed, 6 Mar 2024 18:25:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749537; cv=none; b=Bi3pt57FyQJsriMsCRSnaWjgn/V7v6071PQ3PWkLj06W3E1SaNfSVVR30IO19hI8tOQPC3R01jsRbGZTCAF2FE7W4YRQ4JJijzRd6xG/3nvM6gtUrdGB+osxD0YO2TfVjnjhPIiZX/IOIZtVqe9FlCTEpc+0QW7604wnB5QN5kw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749537; c=relaxed/simple; bh=Z86ROT7Zdn1ciAfGWq9DAKpw0B6kxc7ufPJJCqX4DBg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z7lDLBDZCeOoBdzVUZTiI/xwB5eAcsKbVeC+AZI+ERjUFKpCh6BHsznR4JWsyNNl9upKIwhTtHzJJ2Dln3lts7lI4S+KMP5VT7T53o0XCbmEoQRdz6IR4wAWuZbEe+aLbiXBCod3NK2rYn0PcVJsVA/WqPAHvZP9hKgsSqpq6Pw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oqjpJoGW; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-609a8fc232bso684297b3.0 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=vger.kernel.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=oqjpJoGWdxnVNBJ85rFBUqk/zaz71D/r7R8+6Gifgd6JHc1LlCSaxjZksXvJkNjZYs 8wlNfBA8VB/xxTvixtCsXu3x5OD3rYKf2ffcJB2eE9w4UAZ2HsbV8fdg8caDYxrm6KTH vGvaTYNWCdt7W3ESQ+XxBCI8c2w/NXw0teeNVoJvq0+1yksDpdv2zFQKn20KiqCWIkKi /feIYzH/vslrU1Qw5rTSqBN6L4QHzdrT9NPRP42CHDLifqDwHiLiPvQcrfZ0TxLcIZyV IQFYmaZUeEk8T8derrmHX2TzLQhKdDVLTIh0Mq8OYNMLAVomkyyaDJ9Mlkhgruf9uHUP xIxw== 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=flaTTKJPtzhz/8fa8ksKvYSlTeYc9K3r9ZrI8rJxa0r83VAf8CTn4SGntCpwgVN1oV C7BTsA/wgaSz7F9Iv2ONtx3J+wT/SNvwJ0kj7W5r7DKrUbt5LNfqrO4h9f2ZwbCGM9Sq Jofmj70/fjqV+h1zt/O5ase/gpcQZ5YDRq3QIzFfb6MeDz7jayUx43FHKi/swXJwSRmm AgDwPb+tRJw2fsC0FzR7Pe7RulYd7qNNsZn/bhBrQhTJY/bACwixno3Me33uRh+OzxA+ VmAuA5O8iivOfpt/pp0b//94sCbG4x+LNvjUehVF+4RVys2BkABdH2t4PrHoxvnQXlPO 7NKA== X-Forwarded-Encrypted: i=1; AJvYcCWD0fyaS/ED/bu4jAaPYO0/1x/+6XjqWfPPwYxcGdisrCW3iRn0hGNes3vYz+DblyIW6EXPbOWT6cvHdDvv7oEV1sZkXCjq2yhmyh1C X-Gm-Message-State: AOJu0YyN/Q8BAjFx7QdeurVMuP+QOl+hX3modBKWHQLohclryA9JqUM0 lOAVulHefIChdiivZEVUrJmSo6FdGQgwXewH4PqGSWGk8xy7DqOuf8O6Jk+IBX8VUF4ARPJHQRu H1Q== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Type: text/plain; charset="UTF-8" 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/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. -- 2.44.0.278.ge034bb2e1d-goog