From nobody Tue Jun 23 20:24:35 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A341C433F5 for ; Sun, 27 Feb 2022 09:23:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229574AbiB0JYL (ORCPT ); Sun, 27 Feb 2022 04:24:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbiB0JYH (ORCPT ); Sun, 27 Feb 2022 04:24:07 -0500 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE1F013E8F for ; Sun, 27 Feb 2022 01:23:30 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id p8so8441702pfh.8 for ; Sun, 27 Feb 2022 01:23:30 -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 :in-reply-to; bh=YKnxtZE2J4v12iS0fkHm9fdiEXM2m35F8Fk6VzqHDJs=; b=gsFQBqioXHjAw6hD6Yd7BIthDv0o6558Jjlxjdj60UHr/TUw5mbmVDNW3Mw4mfLBqJ y18TDRuBREtjEciK7pvIJk+Yml0exrMOuIfFfSGsRBS+kO5NQ3bLZGXbwIRKKX9yezdw vtInvg6o/fRAGW9EWIg42jh/4u0Oh1R+AQwYlFa6K6rTTc5I9KGnXseZqdTm5iLCam4O hhtPsxlQFlhr5MYHGq90OLuZG+6dQtA8oLuXPZps1CdB3WSZIuye4w7Hu27jYq/4Q0X1 ikiQUqdnK6BgGLuP7/XO07U9YE1oM8VlBgzcm6Ou2/fMdpgodE1Z8/1/qvdHX78t5YLe eX8A== 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:in-reply-to; bh=YKnxtZE2J4v12iS0fkHm9fdiEXM2m35F8Fk6VzqHDJs=; b=Ia25TCA0GPYVHbqwybSKPxv7RsCs7JdL6BXnzWKZNgTO5cfOxXcFecCIYnevtGIMm4 atPYVRoIo2yAQjMhkba1lTgzxswz7n/L5s/3HFF8GOIJ7SLHYEKXYyvDeq+ZsdtzDPGB Kdb843KRNdXZpVxf/8kdso+/rWvNKpJEjam9vpodclPlqKKEcjJ9ucT7M6/AvtY6nnOg Pg0Euk3uYzFatlnlPkyrfj1cjtUShhWYpatj0QytSSc0gHKoOqrJgTYGWirnfQq91ZU8 dUIRzivC7wez1MvlQ972ds/8PIRIGIGrqmR88bjqzTvWak3h+QBM/UFH9A2yqDkp2Nq8 h92Q== X-Gm-Message-State: AOAM531j2l/TyHhH1rz8S5B6Mb8IZcDFWvPCWuo5JbZ0+k9fHxelYSKk 5YOEtWyBxokAt8Y+0rriGM0= X-Google-Smtp-Source: ABdhPJzMawut+N0Hibcl7C82p1CHJqf3vz7xVupBicFzhSKFRDvmHb0hc3h1/wky4JSeuPTlAy9Mrw== X-Received: by 2002:a05:6a00:24ca:b0:4e1:cb76:32da with SMTP id d10-20020a056a0024ca00b004e1cb7632damr15794574pfv.81.1645953810461; Sun, 27 Feb 2022 01:23:30 -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 e14-20020a056a001a8e00b004e136d54a15sm9584444pfv.105.2022.02.27.01.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Feb 2022 01:23:30 -0800 (PST) Date: Sun, 27 Feb 2022 09:23:22 +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] lib/stackdepot: Use page allocator if both slab and memblock is unavailable Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After commit 2dba5eb1c73b ("lib/stackdepot: allow optional init and stack_table allocation by kvmalloc()"), stack_depot_init() is called later if CONFIG_STACKDEPOT_ALWAYS_INIT=3Dn to remove unnecessary memory usage. It allocates stack_table using memblock_alloc() or kvmalloc() depending on availability of slab allocator. But when stack_depot_init() is called while creating boot slab caches, both slab allocator and memblock is not available. So kernel crashes. Allocate stack_table from page allocator when both slab allocator and memblock is unavailable. Limit size of stack_table when using page allocator because vmalloc() is also unavailable in kmem_cache_init(). It must not be larger than (PAGE_SIZE << (MAX_ORDER - 1)). This patch was tested on both CONFIG_STACKDEPOT_ALWAYS_INIT=3Dy and n. [ lkp@intel.com: Fix W=3D1 build warning ] Fixes: 2dba5eb1c73b ("lib/stackdepot: allow optional init and stack_table a= llocation by kvmalloc()") Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- lib/stackdepot.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index bf5ba9af0500..a96f8fd78c42 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -73,6 +73,14 @@ static int next_slab_inited; static size_t depot_offset; static DEFINE_RAW_SPINLOCK(depot_lock); =20 +static size_t stack_hash_size =3D (1 << CONFIG_STACK_HASH_ORDER); +static inline size_t stack_hash_mask(void) +{ + return stack_hash_size - 1; +} + +#define STACK_HASH_SEED 0x9747b28c + static bool init_stack_slab(void **prealloc) { if (!*prealloc) @@ -142,10 +150,6 @@ depot_alloc_stack(unsigned long *entries, int size, u3= 2 hash, void **prealloc) return stack; } =20 -#define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER) -#define STACK_HASH_MASK (STACK_HASH_SIZE - 1) -#define STACK_HASH_SEED 0x9747b28c - static bool stack_depot_disable; static struct stack_record **stack_table; =20 @@ -172,18 +176,28 @@ __ref int stack_depot_init(void) =20 mutex_lock(&stack_depot_init_mutex); if (!stack_depot_disable && !stack_table) { - size_t size =3D (STACK_HASH_SIZE * sizeof(struct stack_record *)); + size_t size =3D (stack_hash_size * sizeof(struct stack_record *)); int i; =20 if (slab_is_available()) { pr_info("Stack Depot allocating hash table with kvmalloc\n"); stack_table =3D kvmalloc(size, GFP_KERNEL); + } else if (totalram_pages() > 0) { + /* Reduce size because vmalloc may be unavailable */ + size =3D min_t(size_t, size, PAGE_SIZE << (MAX_ORDER - 1)); + stack_hash_size =3D size / sizeof(struct stack_record *); + + pr_info("Stack Depot allocating hash table with __get_free_pages\n"); + stack_table =3D (struct stack_record **) + __get_free_pages(GFP_KERNEL, get_order(size)); } else { pr_info("Stack Depot allocating hash table with memblock_alloc\n"); stack_table =3D memblock_alloc(size, SMP_CACHE_BYTES); } + if (stack_table) { - for (i =3D 0; i < STACK_HASH_SIZE; i++) + pr_info("Stack Depot hash table size=3D%zu\n", stack_hash_size); + for (i =3D 0; i < stack_hash_size; i++) stack_table[i] =3D NULL; } else { pr_err("Stack Depot hash table allocation failed, disabling\n"); @@ -363,7 +377,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *= entries, goto fast_exit; =20 hash =3D hash_stack(entries, nr_entries); - bucket =3D &stack_table[hash & STACK_HASH_MASK]; + bucket =3D &stack_table[hash & stack_hash_mask()]; =20 /* * Fast path: look the stack trace up without locking. --=20 2.33.1