Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp3772332pxb; Tue, 7 Sep 2021 07:17:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUOU2RNQ3OfN7mGckfBUuRGF0sYhst06bMZcrtmEqE+dhh/pIr6a/7N3tLvQwrqKps9AX4 X-Received: by 2002:a92:c9cc:: with SMTP id k12mr12429442ilq.316.1631024236527; Tue, 07 Sep 2021 07:17:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631024236; cv=none; d=google.com; s=arc-20160816; b=jFnXH9M8VLo7FNGPSeeSoxEte7BiqXSprpylIwz70w5W4K8guPETt8C12tPLxahiZJ 2+JSmAW5WUdAfcAu/fEWqDhVTPrCbGAnv+GbiXW4V0aB+63Brh/b2Xc8RqMXR7CY6ukD t5nIPKorOf3liDW1CrNY9LoKt0yfcV2CtMPEd6odGdQmh9xJxtdLLcq64N1HoYthIEiA 69imVxR0iejS/qVSwpLZChfNS7lxjJe4cYMiPEvmH5jl/vv2DYVcDs0o0aXx+9x9PZhv o/3TRXKNjHjgRskOzHZbqVcF2RSbD9lKVq6bUgiSMfxNcpvOMF4hX6y/SMv6R+GfSCzq oVgg== 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=ClauTO0DAc/W2CE5FaWJtasGRNKjOeQhVVw0fZ55opA=; b=Lpx8LAKiUwsNcoDj1U9VA5LGBsaBAzzuous4JlqlYMuZNG+BXzhoMrZAJdG7bKtysh S94h8lgs1s3454UevocwTtJwHxitxzaREPr27J/tWHIq1HtvPpxuf+e44YhtPmh9PttO vVkm1lv6NtMhy+tC/4qY2sWspNkDzJAWnzRvZqIUqjcNF/r/AR/+4YzEMA17V+C95i4s uIiOUbIs0j+rEswvsfNSDGwDOX0jd095uG7wscdBl/rqVJeTIR8K49LCOKUSQtF77ALd FdNjSWRa0BRl7aP7VChwQZwg1Q/1llPdWBFxYtcyJHSoalvcE99jlJJCmlL14ESR8htb khNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ICdgop9B; 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 i12si11517694ilu.9.2021.09.07.07.17.02; Tue, 07 Sep 2021 07:17:16 -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=ICdgop9B; 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 S236655AbhIGOPq (ORCPT + 99 others); Tue, 7 Sep 2021 10:15:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237150AbhIGOPR (ORCPT ); Tue, 7 Sep 2021 10:15:17 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3F42C0613C1 for ; Tue, 7 Sep 2021 07:14:08 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id b8-20020a0562141148b02902f1474ce8b7so15177742qvt.20 for ; Tue, 07 Sep 2021 07:14:08 -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=ClauTO0DAc/W2CE5FaWJtasGRNKjOeQhVVw0fZ55opA=; b=ICdgop9Bb/UlNIicMIQWpjefBvU62X7yD2tARPBACjMMeNeFKwL6hpa4za3Q+GViph 59yYezR+3aidPQ4onhqjPPPGR4f/IJ0F0Lq2blaciGq800jLMrwOi6qn9Tr39ECNIEHI wpHPULsvIva0eZTmQLyc2Xu/kI8dANICLgD99thQF9/xwBQM453Fcpo01BqwkjaiJEc7 VW6igdwjJSIfWLdmnBMhQ/2AmmT/YbgKiSok59dlxnsL7bOq1G9Xf8FpiQS0SGrGT4sc IDimGSf/TGzEFVj2tZta1r7d+1QalLw3e4KnH938MLWkG9cgJiBntBlEk2MNKDx+r5/O XgnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ClauTO0DAc/W2CE5FaWJtasGRNKjOeQhVVw0fZ55opA=; b=GzP1WynBcHltzXwffZY7MMReSESt6HzCXtX39C9mewmYodwdse9c7rMg8NcexQkDrO f8AiOkyZhV06E5LnB6vNYAnQqFd7ugKmp9QQJQnT5XdAQrdQRTn5JcOMwExJj0VDMygi 2PIXKnH5Pzsr7tVtF00pGcbIEC6+rpITPThRUC/SNmqE0/3QfLAgu2VWDoxuVJLqgonp yWii5/94P35QsKNr7T4ODBl/5TlnRngYRu11tbAqWsERZlaHsLdzQcdp7zk+dR8nAMxi iJWnT6Iszv8YYqgb9nTDzpFMY58TdzT8mLEWhAoAyIo9QjbYAHnkCNM0poTAsegL0zv7 n44w== X-Gm-Message-State: AOAM531gtlGBNlp3ygvuoBt+gXF9r56SXeN8aUOgkpf5T2PhdbcQuUfZ ojoIWvZsjBndAk0mtMpOlRkELIhTnw== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:6800:c1ea:4271:5898]) (user=elver job=sendgmr) by 2002:a05:6214:14f2:: with SMTP id k18mr17256135qvw.19.1631024048156; Tue, 07 Sep 2021 07:14:08 -0700 (PDT) Date: Tue, 7 Sep 2021 16:13:04 +0200 In-Reply-To: <20210907141307.1437816-1-elver@google.com> Message-Id: <20210907141307.1437816-4-elver@google.com> Mime-Version: 1.0 References: <20210907141307.1437816-1-elver@google.com> X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog Subject: [PATCH 3/6] lib/stackdepot: introduce __stack_depot_save() From: Marco Elver To: elver@google.com Cc: Andrew Morton , Shuah Khan , Tejun Heo , Lai Jiangshan , Andrey Konovalov , Walter Wu , 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 --- include/linux/stackdepot.h | 4 ++++ lib/stackdepot.c | 42 ++++++++++++++++++++++++++++++++------ 2 files changed, 40 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..cab6cf117290 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,25 @@ 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. + * + * 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.153.gba50c8fa24-goog