From patchwork Tue Sep 7 14:13:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12478821 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5ADE3C433EF for ; Tue, 7 Sep 2021 14:14:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 03D4661103 for ; Tue, 7 Sep 2021 14:14:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 03D4661103 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 9B2A1940009; Tue, 7 Sep 2021 10:14:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 93ABA940007; Tue, 7 Sep 2021 10:14:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DCB1940009; Tue, 7 Sep 2021 10:14:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by kanga.kvack.org (Postfix) with ESMTP id 690FF940007 for ; Tue, 7 Sep 2021 10:14:09 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 215DF181B0493 for ; Tue, 7 Sep 2021 14:14:09 +0000 (UTC) X-FDA: 78560971818.31.1C8F43D Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf18.hostedemail.com (Postfix) with ESMTP id E2039400209E for ; Tue, 7 Sep 2021 14:14:08 +0000 (UTC) Received: by mail-qk1-f202.google.com with SMTP id 23-20020a05620a071700b00426392c0e6eso14643517qkc.4 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=KOq+gDZUsCM3Lk8KuSxd9hxv698qQFPOfv3kJo5Kcv5cmq/9T3ZveTq92zn8NDH70t kV4PEsjGHokfeuNHW8aJ38CBGdnbxIe2zOU/pFE42TVXGL8LjhkzRIvYs9V+S9+bb4cI f/ZBSxe5gh0VzD/c0crOCfJwEzBn8VO3GoOMF1sMGnIZrDYVgV9rzaaQMMnx5jwW8Wx0 OvfCM+GKjtshpvk3mHI1a/EBA/8SO7yuvKAmXyL0VAACzWlFSv/9FCG3bF6rZwMvwMhP qvf2F3KQ+DhkGC43TGrQs+QeGUs5UZSqXFwNZfBnQyEhztyuSaq2bYIXTAvl/DpQHZig PeMA== X-Gm-Message-State: AOAM533Mt7a+OTHD1Lx7+INM3yOlEJDutVle18LYhp0XQOtjX3oujLAC WNXY/njMjKbzBE9sOhcWVH/K3DeMJw== X-Google-Smtp-Source: ABdhPJw87QSuzKKPiaQWZq+ibYy/Wvgp6rb9RM4pXrZmwrnDnpMuaZQjS0f+oetPSFS2lDFVXSeJ2ZruVQ== 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 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ICdgop9B; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3sHM3YQUKCIUnu4n0pxxpun.lxvurw36-vvt4jlt.x0p@flex--elver.bounces.google.com designates 209.85.222.202 as permitted sender) smtp.mailfrom=3sHM3YQUKCIUnu4n0pxxpun.lxvurw36-vvt4jlt.x0p@flex--elver.bounces.google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E2039400209E X-Stat-Signature: ssqg9uk75a4cwbxk5yqbige7h8tgdw7j X-HE-Tag: 1631024048-635541 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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)