Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4894521rdb; Fri, 15 Sep 2023 16:10:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFRBbC3ty6P+Y3p49hO7FRbffP4z0z6LyxADuP1zspFoGYkLfuMFoTScldeuQW+hoBhoDIH X-Received: by 2002:a05:6a20:7d87:b0:14c:c511:387d with SMTP id v7-20020a056a207d8700b0014cc511387dmr2689615pzj.9.1694819455879; Fri, 15 Sep 2023 16:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694819455; cv=none; d=google.com; s=arc-20160816; b=JPb0zqUg9gdX+V2Hh+OfjYWNoBzfNauCkE55gNtFedGZsN0B5WJN1FmA54kXUTSfhq l6177x/8QaaC06pN6Yf0b+N2sbE4xUCGfwazJv69DbF8NWGiQjc9KWF5jmSzdbCKjONS JlLAjpltl1778pBs26ngzo/SBX+d6ezZ39ogxiHOdcGcGP2LcJa5IrvSuyt89z+JWa7A tTanB7rJf0DevSJIKNxCp4nndMFCHU9ZidUulJ6YvAO03zNcmwaP5bBMJUFHKuOuxrB1 nE4m6Hl9W7BNPMqOKejCF2zhLKVSVitb/vXODhLRycWPepK4a/WPiezB0eqT9/VfJJyP 8rHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=6aCgnXJdDdEagEQtDfeKQJl/I/7HuJOZio3xGVS/z3M=; fh=3EgVQPQxbxv+XfZ2sWOggyb2K8Rxmj6Eg9mP1jVgo94=; b=mX9OB+1yK9LkhFPvR96ZVIPqTu5KfQXdT28BnKnLpA5cITN/8KglHI+2gRZkGUVmSI RCPTxmPStoWmtfkFBtcY/6fCuoK5vQHYZt0gLPuIYNDkKBK7Y7xi9lpR/BYGMkk+1WuM sEf4fM+5r58QA7qUPt1qva0pM3jXNGevKotc0jI5i4ZF/gSJ+4TPNVvFmhO2p6l8jyB4 wJ6NCFmRv97wJpaYNHnqlJFQEZZR5ZiT7LHa+PtRTsqEnkYDeyiXNFB/hqOKKDlrs5uR uvzQTpJhYAQvTRW/KaxYKEXp/HRBCpy1kveiisVqw2FTqfxFZJD1/AOUFR4LGBkPcDPO mliQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yU2GPzTM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id fo24-20020a056a00601800b00690158064aasi3910300pfb.118.2023.09.15.16.10.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 16:10:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yU2GPzTM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 36B398258CBA; Fri, 15 Sep 2023 13:33:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237273AbjIOUcV (ORCPT + 99 others); Fri, 15 Sep 2023 16:32:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237476AbjIOUcK (ORCPT ); Fri, 15 Sep 2023 16:32:10 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDB7D10E for ; Fri, 15 Sep 2023 13:32:02 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-403012f27e3so29300465e9.3 for ; Fri, 15 Sep 2023 13:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694809921; x=1695414721; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6aCgnXJdDdEagEQtDfeKQJl/I/7HuJOZio3xGVS/z3M=; b=yU2GPzTMy/FZ8zgkN1X4CJg88Xfg/6DlgHIIp9FnFo/pNjDIdHvsiIfdHhlRlChJo+ J7SmqAWEnRVz9EHRclHPbu5OVwwL5LZQzfPRYumltXN1AXSLJZCDGkWbWceLXZwvQTt6 W61o/KL2JXS3lyc+IqeJU3r999wkSJ6xmB+1ggBfvOysagqnLS9zvZ3GGCXjLTU7v7gw zBK6S0k9KJ+6LbzKgql+K+1lc8AuIDU0owkvg+tp9khYeFADZ0m/twoH4udlqc6lvas9 od1tiDf2caoEk30rmnAnI1HYeS2PXl78SvVdHq7eJde0wFrzWSuothAN4zyRdlJEiww3 f0Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694809921; x=1695414721; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6aCgnXJdDdEagEQtDfeKQJl/I/7HuJOZio3xGVS/z3M=; b=drqb5aLIHpHr1NB+iJCe7MQHD9fNL56rR5vjUrik3qPNLV4ddHxVAtlx9yirLstsaJ TC3gj/BzekbWg4L7kS/3NWfRjRFOE3Shm+hinoK7de4xmo3aWzop9AB90FQElHYqKhSs k2r7b2M0kcdDbAp9K6WBqxgykalOMngtZMn3yOoxfMRTRASjVzXldlS7mwjL164RJga9 DVgcUDnT7uq5rACoUq4qJs7C/jXhqE7hqlxJrxNdjpXji8JbHXWsaALJAVgpdcfiz2q9 +Sh22h4wHlYhkaEaMhSWG85XkuwjugWsZ94sFxp68yIpRJgA0fYLBJFSU1zd/AMki7/A 336w== X-Gm-Message-State: AOJu0Yy+66FCVU0lY0b0KLlH0MPgbgRG4rIFCFMtOM+GeWBU/Lc3VSlW sepgANvLfhOsoH/ZN4V3EZ8dxSXdKiHYlctQegkdsA== X-Received: by 2002:a05:600c:3641:b0:401:b652:b6cf with SMTP id y1-20020a05600c364100b00401b652b6cfmr2697790wmq.13.1694809921161; Fri, 15 Sep 2023 13:32:01 -0700 (PDT) MIME-Version: 1.0 References: <2a161c99c47a45f8e9f7a21a732c60f0cd674a66.1694625260.git.andreyknvl@google.com> In-Reply-To: <2a161c99c47a45f8e9f7a21a732c60f0cd674a66.1694625260.git.andreyknvl@google.com> From: Marco Elver Date: Fri, 15 Sep 2023 22:31:21 +0200 Message-ID: Subject: Re: [PATCH v2 14/19] lib/stackdepot, kasan: add flags to __stack_depot_save and rename To: andrey.konovalov@linux.dev Cc: Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 15 Sep 2023 13:33:10 -0700 (PDT) On Wed, 13 Sept 2023 at 19:17, wrote: > > From: Andrey Konovalov > > Change the bool can_alloc argument of __stack_depot_save to a > u32 argument that accepts a set of flags. > > The following patch will add another flag to stack_depot_save_flags > besides the existing STACK_DEPOT_FLAG_CAN_ALLOC. > > Also rename the function to stack_depot_save_flags, as __stack_depot_save > is a cryptic name, > > Signed-off-by: Andrey Konovalov > > --- > > Changes v1->v2: > - This is a new patch. > --- > include/linux/stackdepot.h | 36 +++++++++++++++++++++++++----------- > lib/stackdepot.c | 16 +++++++++++----- > mm/kasan/common.c | 7 ++++--- > mm/kasan/generic.c | 9 +++++---- > mm/kasan/kasan.h | 2 +- > mm/kasan/tags.c | 3 ++- > 6 files changed, 48 insertions(+), 25 deletions(-) > > diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h > index e58306783d8e..0b262e14144e 100644 > --- a/include/linux/stackdepot.h > +++ b/include/linux/stackdepot.h > @@ -32,6 +32,17 @@ typedef u32 depot_stack_handle_t; > */ > #define STACK_DEPOT_EXTRA_BITS 5 > > +typedef u32 depot_flags_t; > + > +/* > + * Flags that can be passed to stack_depot_save_flags(); see the comment next > + * to its declaration for more details. > + */ > +#define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) > + > +#define STACK_DEPOT_FLAGS_NUM 1 > +#define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) > + > /* > * Using stack depot requires its initialization, which can be done in 3 ways: > * > @@ -69,31 +80,34 @@ static inline int stack_depot_early_init(void) { return 0; } > #endif > > /** > - * __stack_depot_save - Save a stack trace to stack depot > + * stack_depot_save_flags - Save a stack trace to stack depot > * > * @entries: Pointer to the stack trace > * @nr_entries: Number of frames in the stack > * @alloc_flags: Allocation GFP flags > - * @can_alloc: Allocate stack pools (increased chance of failure if false) > + * @depot_flags: Stack depot flags > + * > + * Saves a stack trace from @entries array of size @nr_entries. > * > - * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is > - * %true, stack depot can replenish the stack pools in case no space is left > - * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids > - * any allocations and fails if no space is left to store the stack trace. > + * If STACK_DEPOT_FLAG_CAN_ALLOC is set in @depot_flags, stack depot can > + * replenish the stack pools in case no space is left (allocates using GFP > + * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and > + * fails if no space is left to store the stack trace. > * > * If the provided stack trace comes from the interrupt context, only the part > * up to the interrupt entry is saved. > * > - * Context: Any context, but setting @can_alloc to %false is required if > + * Context: Any context, but setting STACK_DEPOT_FLAG_CAN_ALLOC is required if > * alloc_pages() cannot be used from the current context. Currently > * this is the case for contexts where neither %GFP_ATOMIC nor > * %GFP_NOWAIT can be used (NMI, raw_spin_lock). > * > * Return: 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 gfp_flags, bool can_alloc); > +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, > + unsigned int nr_entries, > + gfp_t gfp_flags, > + depot_flags_t depot_flags); > > /** > * stack_depot_save - Save a stack trace to stack depot > @@ -103,7 +117,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, > * @alloc_flags: Allocation GFP flags > * > * Context: Contexts where allocations via alloc_pages() are allowed. > - * See __stack_depot_save() for more details. > + * See stack_depot_save_flags() for more details. > * > * Return: Handle of the stack trace stored in depot, 0 on failure > */ > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > index 1b08897ebd2b..e5121225f124 100644 > --- a/lib/stackdepot.c > +++ b/lib/stackdepot.c > @@ -438,19 +438,24 @@ static inline struct stack_record *find_stack(struct list_head *bucket, > return NULL; > } > > -depot_stack_handle_t __stack_depot_save(unsigned long *entries, > - unsigned int nr_entries, > - gfp_t alloc_flags, bool can_alloc) > +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, > + unsigned int nr_entries, > + gfp_t alloc_flags, > + depot_flags_t depot_flags) > { > struct list_head *bucket; > struct stack_record *found = NULL; > depot_stack_handle_t handle = 0; > struct page *page = NULL; > void *prealloc = NULL; > + bool can_alloc = depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC; > bool need_alloc = false; > unsigned long flags; > u32 hash; > > + if (depot_flags & ~STACK_DEPOT_FLAGS_MASK) > + return 0; > + Shouldn't this be a WARN due to invalid flags? > /* > * If this stack trace is from an interrupt, including anything before > * interrupt entry usually leads to unbounded stack depot growth. > @@ -529,13 +534,14 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, > handle = found->handle.handle; > return handle; > } > -EXPORT_SYMBOL_GPL(__stack_depot_save); > +EXPORT_SYMBOL_GPL(stack_depot_save_flags); > > 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); > + return stack_depot_save_flags(entries, nr_entries, alloc_flags, > + STACK_DEPOT_FLAG_CAN_ALLOC); > } > EXPORT_SYMBOL_GPL(stack_depot_save); > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 256930da578a..825a0240ec02 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -22,6 +22,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -37,19 +38,19 @@ struct slab *kasan_addr_to_slab(const void *addr) > return NULL; > } > > -depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) > +depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags) > { > unsigned long entries[KASAN_STACK_DEPTH]; > unsigned int nr_entries; > > nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); > - return __stack_depot_save(entries, nr_entries, flags, can_alloc); > + return stack_depot_save_flags(entries, nr_entries, flags, depot_flags); > } > > void kasan_set_track(struct kasan_track *track, gfp_t flags) > { > track->pid = current->pid; > - track->stack = kasan_save_stack(flags, true); > + track->stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); > } > > #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) > diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c > index 4d837ab83f08..5d168c9afb32 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -472,7 +473,7 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object) > sizeof(struct kasan_free_meta) : 0); > } > > -static void __kasan_record_aux_stack(void *addr, bool can_alloc) > +static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) > { > struct slab *slab = kasan_addr_to_slab(addr); > struct kmem_cache *cache; > @@ -489,17 +490,17 @@ static void __kasan_record_aux_stack(void *addr, bool can_alloc) > return; > > alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; > - alloc_meta->aux_stack[0] = kasan_save_stack(0, can_alloc); > + alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); > } > > void kasan_record_aux_stack(void *addr) > { > - return __kasan_record_aux_stack(addr, true); > + return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); > } > > void kasan_record_aux_stack_noalloc(void *addr) > { > - return __kasan_record_aux_stack(addr, false); > + return __kasan_record_aux_stack(addr, 0); > } > > void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index f70e3d7a602e..de3206e11888 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -370,7 +370,7 @@ static inline void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int > static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { } > #endif > > -depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); > +depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags); > void kasan_set_track(struct kasan_track *track, gfp_t flags); > void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); > void kasan_save_free_info(struct kmem_cache *cache, void *object); > diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c > index 7dcfe341d48e..4fd32121b0fd 100644 > --- a/mm/kasan/tags.c > +++ b/mm/kasan/tags.c > @@ -13,6 +13,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -101,7 +102,7 @@ static void save_stack_info(struct kmem_cache *cache, void *object, > struct kasan_stack_ring_entry *entry; > void *old_ptr; > > - stack = kasan_save_stack(gfp_flags, true); > + stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); > > /* > * Prevent save_stack_info() from modifying stack ring > -- > 2.25.1 >