Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3414985pxm; Mon, 28 Feb 2022 20:00:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRJKmMi+Q1ej8bS/uuMaN9L3YSV3n6GAB6UH5idRRf9GDwrG86c00FR+N1a4HLgaQ79yCh X-Received: by 2002:a17:906:d8ae:b0:6b7:98d6:6139 with SMTP id qc14-20020a170906d8ae00b006b798d66139mr17320851ejb.498.1646107242354; Mon, 28 Feb 2022 20:00:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646107242; cv=none; d=google.com; s=arc-20160816; b=Aw+6Pop+gu5us8ZGvKRDa+srcqb4ZNqv6lZKiw1Pyq5vijm1gDXyy+3x9+l0G1THFa xpDKyVoPNUzfBadsE3LLGMzsoJyqIYPbc0aJcM5qouceJGZs55ar6VCM/vqiyOvoSuDC EUcXiLcBPcBv8XRU2xRb7pPGs+wDlO27PaoNYyE3DEeahxeikb0pu/1jQc1HpH3odUNq 69rD92nL4aKFyGRDKKFre5uSx1os3r3V3TAuc2TEs6cyTCQ16vjz1b8J5bxXHx5Fvkoa kUAj68mLC/D9NM3vzg8RwdpAiLXiW1YDVoDZnu1MyIxK7YN+edyTM9PdArKLiVeVL5QU s4gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=wvHyMWb4Vup6fp70fcnYjPwaQlO4R7c0Csa1i6hXi9M=; b=0bkvvfrAEa1mz0t/g5cBoohrbwqVDQiRe/sIwyJRkv0jOTXdT6XZXyiYo0hKCebkRJ gwe617h1c9a60DnJSYAO/rPcdivHMmd12AiQnCPDjGW0gTAi4+WY92NpYvwus2YZOPNA uDcExvlDVrwtas8eZLCIkeknDK+OWLDhSv58DJ4YA+Sx1h/NYLZKA71XYzXaVNIPEb9u TOvZQ/PQCwFT4gbPJXomT8qDa1V8niBtFgFBLI6PVY+uoXGar+U9U2LWSYMM0ghZ0BBC GEPRyhvxdnzVuVrsuVTVFzGkykhCQzwPNHWyFIad03JZQl3ddXaz09bX9nCNVkL49J3U QB4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=EBvBprQQ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w11-20020a056402070b00b00410b7f0c260si7893104edx.540.2022.02.28.20.00.20; Mon, 28 Feb 2022 20:00:42 -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=@gmail.com header.s=20210112 header.b=EBvBprQQ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232091AbiCADha (ORCPT + 99 others); Mon, 28 Feb 2022 22:37:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229625AbiCADh0 (ORCPT ); Mon, 28 Feb 2022 22:37:26 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6749870312 for ; Mon, 28 Feb 2022 19:36:46 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id bc27so4426756pgb.4 for ; Mon, 28 Feb 2022 19:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=wvHyMWb4Vup6fp70fcnYjPwaQlO4R7c0Csa1i6hXi9M=; b=EBvBprQQ//9P3T/BVK9MgCeHo1ctnzByGpuO39blALooJrEPY4SOSpFa8BBXfnBEBI ENckznChvtoDSYrxUSfH6FmKbXAdEZ+TCLtss/48tk32k5Z/IpBHE7XCXiRkm6cti8yD /cADisIYld/bdrsJhdHi0AbX0lkGTj5kHUVxOkg/wITDIhgLd1g31B4ohCpIdk+vcKta sbSbptc4OYc6JkXsyvQG8oqHi7I/kJlco8oZctdPPjZlWo+yJelu8QIFYfZ554YUFnd4 CbSSmU+YEdUVujYBt1Xg+HsW+nveNueZB/9ofo8BLovB1o0rMGZhCfdmXSlz499TYUuq /O9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=wvHyMWb4Vup6fp70fcnYjPwaQlO4R7c0Csa1i6hXi9M=; b=wmx0s3th6ZHFP0OlSh62d9nxmWIep2AQmi4crWajeUkEXrDeTZj72Sg2HNXCNczdb/ 0Efl5lcrdW0wRXnon+12vfqVkjAsTVKUQzGdK4AuwYcrV+FZ5nw3+4dgFwT5rwccuo2X nUfrUamg2/tjtr3NUuG+yPFBCCD15mAMRj9fYJD1ku6SwTu2W4R9m2IA5QXR2xWPuHsw gxRjsFfHCD0VM/TrIdP6bsoRmNAubEgaXGwFUrYihB0vS29vPVW7ChHMXgCjNNfj4tes YSO8IEbikZg3LRpN/Jjx2vAsGkI82GXR3aso62amC84D2u7fTnnJUBdK7sYP7CA5Yywf Nw/w== X-Gm-Message-State: AOAM532Q5uKtcKaHLlJWJEuRwTPsRpUmDxS/lV5fGw77oRlsqSkjKNeb 32s3T+fIgsF4RO/cSM3PSlA= X-Received: by 2002:a63:1b5e:0:b0:36f:e756:c118 with SMTP id b30-20020a631b5e000000b0036fe756c118mr19840678pgm.562.1646105805849; Mon, 28 Feb 2022 19:36:45 -0800 (PST) Received: from ip-172-31-19-208.ap-northeast-1.compute.internal (ec2-18-181-137-102.ap-northeast-1.compute.amazonaws.com. [18.181.137.102]) by smtp.gmail.com with ESMTPSA id 17-20020a056a00071100b004f0f941d1e8sm14258564pfl.24.2022.02.28.19.36.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 19:36:45 -0800 (PST) Date: Tue, 1 Mar 2022 03:36:38 +0000 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Vlastimil Babka 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 , Marco Elver , Andrey Konovalov , Imran Khan , Zqiang Subject: [PATCH v2] mm/slub: initialize stack depot in boot process Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 commit ba10d4b46655 ("mm/slub: use stackdepot to save stack trace in objects") initializes stack depot in cache creation if SLAB_STORE_USER flag is set. This can make kernel crash because a cache can be crashed 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 for at least one cache. [ elver@google.com: initialize stack depot depending on slub_debug parameter instead of allowing stack_depot_init() to be called during kmem_cache_init() for simplicity. ] [ vbabka@suse.cz: parse slub_debug parameter in setup_slub_debug() and initialize stack depot in stack_depot_early_init(). ] Link: https://lore.kernel.org/all/YhyeaP8lrzKgKm5A@ip-172-31-19-208.ap-northeast-1.compute.internal/ Fixes: ba10d4b46655 ("mm/slub: use stackdepot to save stack trace in objects") Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slab.h | 6 ++++++ include/linux/stackdepot.h | 3 ++- mm/slub.c | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 37bde99b74af..062128e0db10 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -763,6 +763,12 @@ extern void kvfree_sensitive(const void *addr, size_t len); unsigned int kmem_cache_size(struct kmem_cache *s); void __init kmem_cache_init_late(void); +#if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_STACKDEPOT) +int slab_stack_depot_init(void); +#else +int slab_stack_depot_init(void) { return 0; } +#endif + #if defined(CONFIG_SMP) && defined(CONFIG_SLAB) int slab_prepare_cpu(unsigned int cpu); int slab_dead_cpu(unsigned int cpu); diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 17f992fe6355..a813a2673c48 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -12,6 +12,7 @@ #define _LINUX_STACKDEPOT_H #include +#include typedef u32 depot_stack_handle_t; @@ -32,7 +33,7 @@ int stack_depot_init(void); #ifdef CONFIG_STACKDEPOT_ALWAYS_INIT static inline int stack_depot_early_init(void) { return stack_depot_init(); } #else -static inline int stack_depot_early_init(void) { return 0; } +static inline int stack_depot_early_init(void) { return slab_stack_depot_init(); } #endif depot_stack_handle_t stack_depot_save(unsigned long *entries, diff --git a/mm/slub.c b/mm/slub.c index a74afe59a403..2419fc3cc9f3 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -646,6 +646,14 @@ static slab_flags_t slub_debug; static char *slub_debug_string; static int disable_higher_order_debug; +static bool init_stack_depot; + +int slab_stack_depot_init(void) +{ + if (init_stack_depot) + stack_depot_init(); + return 0; +} /* * slub is about to manipulate internal object metadata. This memory lies @@ -1531,6 +1539,8 @@ static int __init setup_slub_debug(char *str) global_slub_debug_changed = true; } else { slab_list_specified = true; + if (flags & SLAB_STORE_USER) + init_stack_depot = true; } } @@ -1546,6 +1556,10 @@ static int __init setup_slub_debug(char *str) global_flags = slub_debug; slub_debug_string = saved_str; } + + if (global_flags & SLAB_STORE_USER) + init_stack_depot = true; + out: slub_debug = global_flags; if (slub_debug != 0 || slub_debug_string) @@ -1556,6 +1570,7 @@ static int __init setup_slub_debug(char *str) static_branch_unlikely(&init_on_free)) && (slub_debug & SLAB_POISON)) pr_info("mem auto-init: SLAB_POISON will take precedence over init_on_alloc/init_on_free\n"); + return 1; } @@ -4221,9 +4236,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)) -- 2.33.1