Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3296276pxm; Mon, 28 Feb 2022 16:46:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZtN1FS039E3vJt6Jc4nfJCcSpsu73HN5yC6lUZ5iIyx3NcIosUOxLQDHygXaX6IEn8wlF X-Received: by 2002:a17:90a:bb8d:b0:1bc:72a9:a07d with SMTP id v13-20020a17090abb8d00b001bc72a9a07dmr19089170pjr.9.1646095585590; Mon, 28 Feb 2022 16:46:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646095585; cv=none; d=google.com; s=arc-20160816; b=CuiHjChBeVds+/yWpuntYE8xeNDco66WkSlE7gdsfJExO4gXYDKHcwun8ZHp08oPzu kXrjbfa174kqAest4Bal7iOEWGFzF1L5zOYOTxRdV1mzyRiOMCTMrrgMkjPHADuo/RF7 0PFri4/k0wXiir3m/eHftZ6zV55334POtGgeIehIDAQtTqGXqZf/1wUqxmrZ1+/6DPm9 2u3wXFUDlstxBqGoGEneWZ1JrSigs4WUVtWZ63cCBgAL7Mh7P2CX779NOgkckTgy4pjU 2PHfAZlyq0RRZ6LaWoWBzeVBoBZPBIaah1L5xIdLhCeXr+QpfxUIX45/IU7UrFoiUtjr U8Tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=ipnEaaVXGjuPjbjYUko5rS7uGNP4HYCYa0ctgBOY/t0=; b=05ehAM/d1F19jWI3Fhz+tGnQK0wssqzv6fShmk4duHDeKL2UQ9hFSaTK1z3S5WERLY qUCVBYt2pr+CN/R3ICTWEz82qUjp402ExDpXYj7qs4d8FVRoGEKxpZSRUR2cEqTMUt0a 6WGC8GS/FUIWVeksyucTbnbkhaOdmlHVxX2eYMDYFCt2E2jfIs9eDFQwRszZBPCMuI9Q yh4+JXbHkVCxUWQxQV0T8uSJV0JIYUx+PWMJZOohhRzGW2ML4hva1sevX71RFMH/Uwa2 ctDYZ/9p8KGoZJrmFOMQQYDuKyjDN0kixgRoQjrGqQrHK98/wHerDhJD70CYQPwMnJQ7 dCHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=CI80451Y; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y73-20020a638a4c000000b0034ba6ea10a0si10686483pgd.661.2022.02.28.16.46.09; Mon, 28 Feb 2022 16:46:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=CI80451Y; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231758AbiCAA3Q (ORCPT + 99 others); Mon, 28 Feb 2022 19:29:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231236AbiCAA3O (ORCPT ); Mon, 28 Feb 2022 19:29:14 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B95053B70 for ; Mon, 28 Feb 2022 16:28:34 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9D04B219A5; Tue, 1 Mar 2022 00:28:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1646094512; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ipnEaaVXGjuPjbjYUko5rS7uGNP4HYCYa0ctgBOY/t0=; b=CI80451YJ3vFV81nQZrQtSXk3JDBcHNPYUgd/GcfK/Tc6m/F1WBK72vu6tVTbuis3mIMRg osAcsfU18GgV0S81cgmIJXxocy7Kc1h7cqhwf/esP3R+A1WIo4v5wx5x8uT3vn+BQqAD46 xtnCKclHnfrCXUwlirNspZYryuHCiQ0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1646094512; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ipnEaaVXGjuPjbjYUko5rS7uGNP4HYCYa0ctgBOY/t0=; b=qePXbAq5X41KUF0JSLYLQgJjKxepd3IPmR+7c4WH8VqdqJDOORa2/MgJ8rvjHrrXrD2POT Dh3O7dPB9KSH8HDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 41A20139FF; Tue, 1 Mar 2022 00:28:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id iPFCD7BoHWIMPQAAMHmgww (envelope-from ); Tue, 01 Mar 2022 00:28:32 +0000 Message-ID: Date: Tue, 1 Mar 2022 01:28:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: [PATCH] mm/slub: initialize stack depot in boot process Content-Language: en-US To: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Marco Elver Cc: David Rientjes , Christoph Lameter , Joonsoo Kim , Pekka Enberg , Roman Gushchin , Andrew Morton , linux-mm@kvack.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org, Oliver Glitta , Faiyaz Mohammed , Dmitry Vyukov , Eric Dumazet , Jarkko Sakkinen , Johannes Berg , Yury Norov , Arnd Bergmann , James Bottomley , Matteo Croce , Andrey Konovalov , Imran Khan , Zqiang References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/28/22 16:09, Hyeonggon Yoo wrote: > commit ba10d4b46655 ("mm/slub: use stackdepot to save stack trace in > objects") initializes stack depot while creating cache if SLAB_STORE_USER > flag is set. > > This can make kernel crash because a cache can be created in various > contexts. For example if user sets slub_debug=U, kernel crashes > because create_boot_cache() calls stack_depot_init(), which tries to > allocate hash table using memblock_alloc() if slab is not available. > But memblock is also not available at that time. > > This patch solves the problem by initializing stack depot early > in boot process if SLAB_STORE_USER debug flag is set globally > or the flag is set to at least one cache. > > [ elver@google.com: initialize stack depot depending on slub_debug > parameter instead of allowing stack_depot_init() can be called > in kmem_cache_init() for simplicity. ] > > Link: https://lkml.org/lkml/2022/2/28/238 > Fixes: ba10d4b46655 ("mm/slub: use stackdepot to save stack trace in objects") > Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> I think a much easier approach would be to do this checking in setup_slub_debug(). There we may either detect SLAB_STORE_USER in global_flags, or check the flags returned by parse_slub_debug_flags() in the while (str) cycle, in the 'else' case where slab_list is present. Both cases would just set some variable that stack_depot_early_init() (the !CONFIG_STACKDEPOT_ALWAYS_INIT version, or a newly consolidated one) would check. So that would be another way to request the stack_depot_init() at a well-defined point of boot, similar to CONFIG_STACKDEPOT_ALWAYS_INIT. Because setup_slub_debug() is called by __setup, which is processed from start_kernel() -> parse_args() before mm_init() -> stack_depot_early_init(). > --- > include/linux/slab.h | 1 + > init/main.c | 1 + > mm/slab.c | 4 ++++ > mm/slob.c | 4 ++++ > mm/slub.c | 28 +++++++++++++++++++++++++--- > 5 files changed, 35 insertions(+), 3 deletions(-) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 37bde99b74af..023f3f71ae35 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -139,6 +139,7 @@ struct mem_cgroup; > /* > * struct kmem_cache related prototypes > */ > +void __init kmem_cache_init_early(void); > void __init kmem_cache_init(void); > bool slab_is_available(void); > > diff --git a/init/main.c b/init/main.c > index 65fa2e41a9c0..4fdb7975a085 100644 > --- a/init/main.c > +++ b/init/main.c > @@ -835,6 +835,7 @@ static void __init mm_init(void) > kfence_alloc_pool(); > report_meminit(); > stack_depot_early_init(); > + kmem_cache_init_early(); > mem_init(); > mem_init_print_info(); > kmem_cache_init(); > diff --git a/mm/slab.c b/mm/slab.c > index ddf5737c63d9..80a6d01aab06 100644 > --- a/mm/slab.c > +++ b/mm/slab.c > @@ -1196,6 +1196,10 @@ static void __init set_up_node(struct kmem_cache *cachep, int index) > } > } > > +void __init kmem_cache_init_early(void) > +{ > +} > + > /* > * Initialisation. Called after the page allocator have been initialised and > * before smp_init(). > diff --git a/mm/slob.c b/mm/slob.c > index 60c5842215f1..00e323af8be4 100644 > --- a/mm/slob.c > +++ b/mm/slob.c > @@ -715,6 +715,10 @@ struct kmem_cache kmem_cache_boot = { > .align = ARCH_KMALLOC_MINALIGN, > }; > > +void __init kmem_cache_init_early(void) > +{ > +} > + > void __init kmem_cache_init(void) > { > kmem_cache = &kmem_cache_boot; > diff --git a/mm/slub.c b/mm/slub.c > index a74afe59a403..40bcd18143b6 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -4221,9 +4221,6 @@ static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags) > s->remote_node_defrag_ratio = 1000; > #endif > > - if (s->flags & SLAB_STORE_USER && IS_ENABLED(CONFIG_STACKDEPOT)) > - stack_depot_init(); > - > /* Initialize the pre-computed randomized freelist if slab is up */ > if (slab_state >= UP) { > if (init_cache_random_seq(s)) > @@ -4810,6 +4807,31 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache) > return s; > } > > +/* Initialize stack depot if needed */ > +void __init kmem_cache_init_early(void) > +{ > +#ifdef CONFIG_STACKDEPOT > + slab_flags_t block_flags; > + char *next_block; > + char *slab_list; > + > + if (slub_debug & SLAB_STORE_USER) > + goto init_stack_depot; > + > + next_block = slub_debug_string; > + while (next_block) { > + next_block = parse_slub_debug_flags(next_block, &block_flags, &slab_list, false); > + if (block_flags & SLAB_STORE_USER) > + goto init_stack_depot; > + } > + > + return; > + > +init_stack_depot: > + stack_depot_init(); > +#endif > +} > + > void __init kmem_cache_init(void) > { > static __initdata struct kmem_cache boot_kmem_cache,