Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1740012pxb; Mon, 13 Sep 2021 04:37:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzpknp2YWqNVyoRn4aNz/+iEKgt46PLidREr9xfKME5EmFX/1MFh6mTCEglUPiwT73kEED X-Received: by 2002:a17:906:26c4:: with SMTP id u4mr11888067ejc.511.1631533031730; Mon, 13 Sep 2021 04:37:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631533031; cv=none; d=google.com; s=arc-20160816; b=yXFWjjH9nBxTl5AdrPBwGUr8LNkmdnjgRybGU4IiIV4gPlC0tAlMOBE35R0DnDlhNN mTv5SCKWiGPfx2xA0Pj2TkWEe9QkBtdsaA/7JZ91mPrGrSaKxTPCqyZLozLaCByoKxSV olXdOrdKgJDyb0f3raPKd69NAkqwgOWLMeUATgW9Ui4jlUnYQbtU5fbHsPAAnnFYrJTh F9CPVUQ59ujTOS8XKlNRXRQI7pkjnF01dWan1TOTZ5RlUbB4iYtRntHExQCjhYD2rHi+ WF6y8uRke0OsPYUUTs4ZooYvunk5LS0gfy3LQ5+rObFQe66xnUCKf205YaMlKYDq3HQ0 Tjaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=9lBuIl0uPI1ZQBCrnGIbYZD+uvnKuUY9gwGZHXFGUeM=; b=D3MlvsG99EEHCVBM+bU1cfJfy6EOM9oyAhsTVqEqChTTqjIo41Q2hqqd2kOi1qiWW5 y0xU6U6SOXg9NHRQULfEAzPjg+CVKn9HyH7aQhONDDDbqHtN2K1yKCgeVLb5qtBWK9IU gIUT9wNNVWDgQMQV2hUgrgajBlWeGl8BK9GSMaUOgaeamuJWQ3kV53+WT09aKbYsvCIf sDijzT1votl6JCeCvkQ2QTTzaDZbMi1XYfuzpYIoxKlyi3t+Bz6LohK7KCXDayZKniAf MGVr1U3uLngFf1hK5KuQ0yHs86ZtuAH3VRwDJOVOtT/qgPD1yeZYrzTx20Vy0a+BVGrv 2djA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rfFKZJW7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p15si9293237edj.97.2021.09.13.04.36.48; Mon, 13 Sep 2021 04:37:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rfFKZJW7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239575AbhIML1z (ORCPT + 99 others); Mon, 13 Sep 2021 07:27:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239569AbhIML1v (ORCPT ); Mon, 13 Sep 2021 07:27:51 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59FF7C061574 for ; Mon, 13 Sep 2021 04:26:35 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id p23-20020a05620a22f700b003d5ac11ac5cso40823143qki.15 for ; Mon, 13 Sep 2021 04:26:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=9lBuIl0uPI1ZQBCrnGIbYZD+uvnKuUY9gwGZHXFGUeM=; b=rfFKZJW7387I8bY6bZwe3tpbSrQulDsb6V7Xwc91XTAZqQ2rVHjixGGJPtC/N1ZUqQ yhGp8ziZ7XDZv++dd0r1xKomcebuFt3fXxSrL14JZIjrz/g8w0QFsopRiUg/0B2fCOH6 sHz903yPsr4JP3JTMVutIG/T3ajRAjA349fLtSXz14Z+WkEdiriM41DBAbg0wTdPuxG9 Lcx2B0ZuQeG1z+UazCNJm6rxW3utVeBQf7qnv1h3JL+uElYgkA03GRabboaMPc2S9oPD U5e58FCuQ2G0kN3WoUCCgjUslqVlfBfYUQAAwIp32EZpc3ctNvzIoyOFTt9EUg88uK6t XegA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=9lBuIl0uPI1ZQBCrnGIbYZD+uvnKuUY9gwGZHXFGUeM=; b=TbDdaX0Vo86rWpnZtthjJUcL4jIoW5IPwqwTZh0Xx5ch9lDveL9ccYi1lgdpyPSNv7 fKymvp8S48JTXndgB+DDHbBjcLb1IG2cyJN3jPU+olAi992X7JX4rXe0EkircGEnTc6+ rC8lE7bC/wKhKQ/oNuOHmkbNQj8fesvqB6ikPSvFHhBrT4c0ToA3KINn6T6M/LDPMlt+ qV5Odpm4O43auL3EMplQVvf+CEYgVcdiLPJx9KMJraWpoUdeggQStgdy7iRlWiWoPOJm dbDajjS1m9XiYLqDO0lJ5UsRG5Rlxn2rHFlxxI4NhTOci9ZW/aIrophp40rjYtrTSoVN +OzQ== X-Gm-Message-State: AOAM531O/xFdokte6c5VMvz0JFtQEU22a1Wiz3t6GsAFNnJBSEnlecWD 8Ms5OpVYk2MbLN/zj/7feKU734py9g== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:1f19:d46:38c8:7e48]) (user=elver job=sendgmr) by 2002:a0c:ffc3:: with SMTP id h3mr1804390qvv.22.1631532394351; Mon, 13 Sep 2021 04:26:34 -0700 (PDT) Date: Mon, 13 Sep 2021 13:26:06 +0200 In-Reply-To: <20210913112609.2651084-1-elver@google.com> Message-Id: <20210913112609.2651084-4-elver@google.com> Mime-Version: 1.0 References: <20210913112609.2651084-1-elver@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH v2 3/6] lib/stackdepot: introduce __stack_depot_save() From: Marco Elver To: elver@google.com, Andrew Morton Cc: Shuah Khan , Tejun Heo , Lai Jiangshan , Andrey Konovalov , Walter Wu , Sebastian Andrzej Siewior , Thomas Gleixner , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Vijayanand Jitta , Vinayak Menon , "Gustavo A. R. Silva" , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Aleksandr Nogikh , Taras Madan Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add __stack_depot_save(), which provides more fine-grained control over stackdepot's memory allocation behaviour, in case stackdepot runs out of "stack slabs". Normally stackdepot uses alloc_pages() in case it runs out of space; passing can_alloc==false to __stack_depot_save() prohibits this, at the cost of more likely failure to record a stack trace. Signed-off-by: Marco Elver Tested-by: Shuah Khan Acked-by: Sebastian Andrzej Siewior --- v2: * Refer to __stack_depot_save() in comment of stack_depot_save(). --- include/linux/stackdepot.h | 4 ++++ lib/stackdepot.c | 43 ++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 97b36dc53301..b2f7e7c6ba54 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -15,6 +15,10 @@ typedef u32 depot_stack_handle_t; +depot_stack_handle_t __stack_depot_save(unsigned long *entries, + unsigned int nr_entries, + gfp_t gfp_flags, bool can_alloc); + depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t gfp_flags); diff --git a/lib/stackdepot.c b/lib/stackdepot.c index c80a9f734253..bda58597e375 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -248,17 +248,28 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, EXPORT_SYMBOL_GPL(stack_depot_fetch); /** - * stack_depot_save - Save a stack trace from an array + * __stack_depot_save - Save a stack trace from an array * * @entries: Pointer to storage array * @nr_entries: Size of the storage array * @alloc_flags: Allocation gfp flags + * @can_alloc: Allocate stack slabs (increased chance of failure if false) + * + * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is + * %true, is allowed to replenish the stack slab pool in case no space is left + * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids + * any allocations and will fail if no space is left to store the stack trace. + * + * Context: Any context, but setting @can_alloc to %false is required if + * alloc_pages() cannot be used from the current context. Currently + * this is the case from contexts where neither %GFP_ATOMIC nor + * %GFP_NOWAIT can be used (NMI, raw_spin_lock). * - * Return: The handle of the stack struct stored in depot + * Return: The handle of the stack struct stored in depot, 0 on failure. */ -depot_stack_handle_t stack_depot_save(unsigned long *entries, - unsigned int nr_entries, - gfp_t alloc_flags) +depot_stack_handle_t __stack_depot_save(unsigned long *entries, + unsigned int nr_entries, + gfp_t alloc_flags, bool can_alloc) { struct stack_record *found = NULL, **bucket; depot_stack_handle_t retval = 0; @@ -291,7 +302,7 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, * The smp_load_acquire() here pairs with smp_store_release() to * |next_slab_inited| in depot_alloc_stack() and init_stack_slab(). */ - if (unlikely(!smp_load_acquire(&next_slab_inited))) { + if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -339,6 +350,26 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, fast_exit: return retval; } +EXPORT_SYMBOL_GPL(__stack_depot_save); + +/** + * stack_depot_save - Save a stack trace from an array + * + * @entries: Pointer to storage array + * @nr_entries: Size of the storage array + * @alloc_flags: Allocation gfp flags + * + * Context: Contexts where allocations via alloc_pages() are allowed. + * See __stack_depot_save() for more details. + * + * Return: The handle of the stack struct stored in depot, 0 on failure. + */ +depot_stack_handle_t stack_depot_save(unsigned long *entries, + unsigned int nr_entries, + gfp_t alloc_flags) +{ + return __stack_depot_save(entries, nr_entries, alloc_flags, true); +} EXPORT_SYMBOL_GPL(stack_depot_save); static inline int in_irqentry_text(unsigned long ptr) -- 2.33.0.309.g3052b89438-goog