From: Vlastimil Babka <vbabka@suse.cz>
To: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: David Rientjes <rientjes@google.com>,
Christoph Lameter <cl@linux.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Pekka Enberg <penberg@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, patches@lists.linux.dev,
linux-kernel@vger.kernel.org, Oliver Glitta <glittao@gmail.com>,
Faiyaz Mohammed <faiyazm@codeaurora.org>,
Marco Elver <elver@google.com>,
Mike Rapoport <rppt@linux.ibm.com>,
Imran Khan <imran.f.khan@oracle.com>
Subject: Re: [PATCH v2 3/6] mm/slub: use stackdepot to save stack trace in objects
Date: Fri, 4 Mar 2022 13:10:28 +0100 [thread overview]
Message-ID: <a4811cfa-8178-1b0b-866d-141221144c79@suse.cz> (raw)
In-Reply-To: <YiH3QAsrnBFc1Nrj@ip-172-31-19-208.ap-northeast-1.compute.internal>
On 3/4/22 12:25, Hyeonggon Yoo wrote:
> On Wed, Mar 02, 2022 at 06:31:19PM +0100, Vlastimil Babka wrote:
>> From: Oliver Glitta <glittao@gmail.com>
>>
>> Many stack traces are similar so there are many similar arrays.
>> Stackdepot saves each unique stack only once.
>>
>> Replace field addrs in struct track with depot_stack_handle_t handle. Use
>> stackdepot to save stack trace.
>>
>> The benefits are smaller memory overhead and possibility to aggregate
>> per-cache statistics in the following patch using the stackdepot handle
>> instead of matching stacks manually.
>>
>> [ vbabka@suse.cz: rebase to 5.17-rc1 and adjust accordingly ]
>>
>> This was initially merged as commit 788691464c29 and reverted by commit
>> ae14c63a9f20 due to several issues, that should now be fixed.
>> The problem of unconditional memory overhead by stackdepot has been
>> addressed by commit 2dba5eb1c73b ("lib/stackdepot: allow optional init
>> and stack_table allocation by kvmalloc()"), so the dependency on
>> stackdepot will result in extra memory usage only when a slab cache
>> tracking is actually enabled, and not for all CONFIG_SLUB_DEBUG builds.
>> The build failures on some architectures were also addressed, and the
>> reported issue with xfs/433 test did not reproduce on 5.17-rc1 with this
>> patch.
>>
>> Signed-off-by: Oliver Glitta <glittao@gmail.com>
>> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
>> Cc: David Rientjes <rientjes@google.com>
>> Cc: Christoph Lameter <cl@linux.com>
>> Cc: Pekka Enberg <penberg@kernel.org>
>> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
...
>> @@ -314,9 +315,13 @@ kmem_cache_create_usercopy(const char *name,
>> * If no slub_debug was enabled globally, the static key is not yet
>> * enabled by setup_slub_debug(). Enable it if the cache is being
>> * created with any of the debugging flags passed explicitly.
>> + * It's also possible that this is the first cache created with
>> + * SLAB_STORE_USER and we should init stack_depot for it.
>> */
>> if (flags & SLAB_DEBUG_FLAGS)
>> static_branch_enable(&slub_debug_enabled);
>> + if (flags & SLAB_STORE_USER && IS_ENABLED(CONFIG_STACKDEPOT))
>> + stack_depot_init();
>> #endif
>
> Is this comment and code still valid in v3?
The comment is still valid, as there can be a kmem_cache_create() call with
SLAB_STORE_USER (in fact there's one in kernel/rcu/rcutorture.c) that's not
covered by the slub_debug parsing.
The code in v3 is just without IS_ENABLED(CONFIG_STACKDEPOT).
>> mutex_lock(&slab_mutex);
>> diff --git a/mm/slub.c b/mm/slub.c
>> index 1fc451f4fe62..42cb79af70a0 100644
>> --- a/mm/slub.c
>> +++ b/mm/slub.c
>> @@ -26,6 +26,7 @@
>> #include <linux/cpuset.h>
>> #include <linux/mempolicy.h>
>> #include <linux/ctype.h>
>> +#include <linux/stackdepot.h>
>> #include <linux/debugobjects.h>
>> #include <linux/kallsyms.h>
>> #include <linux/kfence.h>
>> @@ -264,8 +265,8 @@ static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
>> #define TRACK_ADDRS_COUNT 16
>> struct track {
>> unsigned long addr; /* Called from address */
>> -#ifdef CONFIG_STACKTRACE
>> - unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
>> +#ifdef CONFIG_STACKDEPOT
>> + depot_stack_handle_t handle;
>> #endif
>> int cpu; /* Was running on cpu */
>> int pid; /* Pid context */
>> @@ -724,22 +725,19 @@ static struct track *get_track(struct kmem_cache *s, void *object,
>> return kasan_reset_tag(p + alloc);
>> }
>>
>> -static void set_track(struct kmem_cache *s, void *object,
>> +static void noinline set_track(struct kmem_cache *s, void *object,
>> enum track_item alloc, unsigned long addr)
>> {
>
> noinline for debugging purpose?
> I think it's okay. just a question.
These noinlines make sure that the amount of stack entries are stable and
not subject to inline decisions of compiler...
>> struct track *p = get_track(s, object, alloc);
>>
>> -#ifdef CONFIG_STACKTRACE
>> +#ifdef CONFIG_STACKDEPOT
>> + unsigned long entries[TRACK_ADDRS_COUNT];
>> unsigned int nr_entries;
>>
>> - metadata_access_enable();
>> - nr_entries = stack_trace_save(kasan_reset_tag(p->addrs),
>> - TRACK_ADDRS_COUNT, 3);
>> - metadata_access_disable();
>> -
>> - if (nr_entries < TRACK_ADDRS_COUNT)
>> - p->addrs[nr_entries] = 0;
>> + nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
... so that here '3' removes the correct count of 'internal' stack trace
entries that are not interesting for us.
>> + p->handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT);
>> #endif
>> +
>> p->addr = addr;
>> p->cpu = smp_processor_id();
>> p->pid = current->pid;
>> @@ -759,20 +757,19 @@ static void init_tracking(struct kmem_cache *s, void *object)
>>
>> static void print_track(const char *s, struct track *t, unsigned long pr_time)
>> {
>> + depot_stack_handle_t handle __maybe_unused;
>> +
>> if (!t->addr)
>> return;
>>
>> pr_err("%s in %pS age=%lu cpu=%u pid=%d\n",
>> s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid);
>> -#ifdef CONFIG_STACKTRACE
>> - {
>> - int i;
>> - for (i = 0; i < TRACK_ADDRS_COUNT; i++)
>> - if (t->addrs[i])
>> - pr_err("\t%pS\n", (void *)t->addrs[i]);
>> - else
>> - break;
>> - }
>> +#ifdef CONFIG_STACKDEPOT
>> + handle = READ_ONCE(t->handle);
>> + if (handle)
>> + stack_depot_print(handle);
>> + else
>> + pr_err("object allocation/free stack trace missing\n");
>> #endif
>> }
>>
>> @@ -1532,6 +1529,8 @@ static int __init setup_slub_debug(char *str)
>> global_slub_debug_changed = true;
>> } else {
>> slab_list_specified = true;
>> + if (flags & SLAB_STORE_USER)
>> + stack_depot_want_early_init = true;
>
> This is updated to stack_depot_want_early_init() in v3.
Yes.
>> }
>> }
>>
>> @@ -1549,6 +1548,8 @@ static int __init setup_slub_debug(char *str)
>> }
>> out:
>> slub_debug = global_flags;
>> + if (slub_debug & SLAB_STORE_USER)
>> + stack_depot_want_early_init = true;
>
> This too.
Yes.
>> if (slub_debug != 0 || slub_debug_string)
>> static_branch_enable(&slub_debug_enabled);
>> else
>> @@ -4352,18 +4353,26 @@ void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
>> objp = fixup_red_left(s, objp);
>> trackp = get_track(s, objp, TRACK_ALLOC);
>> kpp->kp_ret = (void *)trackp->addr;
>> -#ifdef CONFIG_STACKTRACE
>> - for (i = 0; i < KS_ADDRS_COUNT && i < TRACK_ADDRS_COUNT; i++) {
>> - kpp->kp_stack[i] = (void *)trackp->addrs[i];
>> - if (!kpp->kp_stack[i])
>> - break;
>> - }
>> +#ifdef CONFIG_STACKDEPOT
>> + {
>> + depot_stack_handle_t handle;
>> + unsigned long *entries;
>> + unsigned int nr_entries;
>> +
>> + handle = READ_ONCE(trackp->handle);
>> + if (handle) {
>> + nr_entries = stack_depot_fetch(handle, &entries);
>> + for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
>> + kpp->kp_stack[i] = (void *)entries[i];
>> + }
>>
>> - trackp = get_track(s, objp, TRACK_FREE);
>> - for (i = 0; i < KS_ADDRS_COUNT && i < TRACK_ADDRS_COUNT; i++) {
>> - kpp->kp_free_stack[i] = (void *)trackp->addrs[i];
>> - if (!kpp->kp_free_stack[i])
>> - break;
>> + trackp = get_track(s, objp, TRACK_FREE);
>> + handle = READ_ONCE(trackp->handle);
>> + if (handle) {
>> + nr_entries = stack_depot_fetch(handle, &entries);
>> + for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
>> + kpp->kp_free_stack[i] = (void *)entries[i];
>> + }
>> }
>> #endif
>> #endif
>> --
>> 2.35.1
>
> Otherwise looks good.
> Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
>
> kmem_dump_obj() and slab error report functionality works fine.
> Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Thanks!
next prev parent reply other threads:[~2022-03-04 12:10 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-02 17:31 [PATCH v2 0/6] SLUB debugfs improvements based on stackdepot Vlastimil Babka
2022-03-02 17:31 ` [PATCH v2 1/6] lib/stackdepot: allow requesting early initialization dynamically Vlastimil Babka
2022-03-02 17:47 ` Marco Elver
2022-03-02 18:02 ` Vlastimil Babka
2022-03-02 18:15 ` Marco Elver
2022-03-02 18:01 ` Mike Rapoport
2022-03-03 19:19 ` [PATCH v3r0 " Vlastimil Babka
2022-03-04 9:18 ` Marco Elver
2022-03-04 10:47 ` Hyeonggon Yoo
2022-03-04 11:02 ` Mike Rapoport
2022-03-02 17:31 ` [PATCH v2 2/6] mm/slub: move struct track init out of set_track() Vlastimil Babka
2022-03-02 17:31 ` [PATCH v2 3/6] mm/slub: use stackdepot to save stack trace in objects Vlastimil Babka
2022-03-04 11:25 ` Hyeonggon Yoo
2022-03-04 12:10 ` Vlastimil Babka [this message]
2022-03-02 17:31 ` [PATCH v2 4/6] mm/slub: distinguish and print stack traces in debugfs files Vlastimil Babka
2022-03-02 17:31 ` [PATCH v2 5/6] mm/slub: sort debugfs output by frequency of stack traces Vlastimil Babka
2022-03-02 17:31 ` [PATCH v2 6/6] slab, documentation: add description of debugfs files for SLUB caches Vlastimil Babka
2022-03-03 8:14 ` Hyeonggon Yoo
2022-03-03 9:33 ` Mike Rapoport
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a4811cfa-8178-1b0b-866d-141221144c79@suse.cz \
--to=vbabka@suse.cz \
--cc=42.hyeyoo@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=elver@google.com \
--cc=faiyazm@codeaurora.org \
--cc=glittao@gmail.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=imran.f.khan@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=patches@lists.linux.dev \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rppt@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox