Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp3055926rwb; Mon, 5 Sep 2022 05:58:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR7GAXbdlw97w+HCD2kfIsPMOD2PwiM5/6WenSvvqRF5pcNv1rU+dDpWE6jn6DAK/M6mH/dA X-Received: by 2002:a17:90b:1e4d:b0:1fd:e8b3:6699 with SMTP id pi13-20020a17090b1e4d00b001fde8b36699mr19959396pjb.172.1662382699175; Mon, 05 Sep 2022 05:58:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662382699; cv=none; d=google.com; s=arc-20160816; b=dUZNWfpHMphOzyC3g1oOt0XPf5rVygBKAxBZ59cmCL68BDD0qlHJJnsGr+q1HPynrP cFvRet8oAKGPHVgK5cPbJlr80MaeoNWCd8njGWB/PjfMakoZnpK5Py178Zo/vGBBuLFT vKSnYhJi/wuhHNAMruKqoamreYq3J15+2yKwmA/NaQA2BYJ6wdf//CE5/uiH3+NkbYXE 9+4gtDMfGH9FHydD4IcqXJIIcYwC7KztJcAdftYPHY6HpK287bcge2FbxkLWpA6Xku7m A5lTZVwlFTlV6cHVeVYHTeGNEYkVqyO1BKCu6l5YIp3rLO/pZtQzlYtOjfMZYXAQ3KmD b8Zg== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=TmxPk0FyKDkT0y+xNsyA55BiCleSLQcpK862DZSm9iy4s3AjqSSTZIfr+0DGmF5gY3 sYJZgnIOGmJeyZWsLfi/DiRZZRwa175yWX8SyPfnISfO90UAJhrlnAsH/ryGTzJkUC+r Df4TIpMcKQsRUN7jgMZjT4IHHhQWup4uGFvJQhFxzsIDIF7zJ+scQWQLvTbZfjB4BelH A708qGMVoYa4nNQrZ72NK5amu9XAbF82VGlFDayypvIwiErexg+CwONAw0mTyf3pVnAS CYf67PMI82OUcJJ11pebJqyn5fC7KKYLMOP7gu1epoOXWRRxTdQE2WMzne18ltL4MBvq hxow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IBpKp0yu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h193-20020a636cca000000b0041d66279902si10475326pgc.865.2022.09.05.05.58.07; Mon, 05 Sep 2022 05:58:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IBpKp0yu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237484AbiIEMZU (ORCPT + 99 others); Mon, 5 Sep 2022 08:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236540AbiIEMZG (ORCPT ); Mon, 5 Sep 2022 08:25:06 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C8BC1BEAE for ; Mon, 5 Sep 2022 05:25:04 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id w17-20020a056402269100b0043da2189b71so5639770edd.6 for ; Mon, 05 Sep 2022 05:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=IBpKp0yu7hath3Co70M/j4bs2EU3HCIGiKtB6PMhhOq7WSR2S+sr7K7xvybqX5tiPf KMPu6eRVtR9bJt9vg1t7nwWY7WgX8rjB7vSwu+xkDC2COoLN7bfIujnQGccb9tiuNN2I +tvcy8vtAqgC8/rOhasI+jB3iaWQ09gj5u+PZXUGsOUzskQ4vR5VS2PzQP0rNKu3m/o/ HuvftnQpFDuleOZ6lltnEfpy11C4VbBbXBRUCGafqrf5OjRzUIgq9PKL1SA1LkvboCaB 03BkpVi0fEzItGaGnZXDzs3yF73raYOzYiFhGt55TzT/HGwTN2ogG055IZb40fN68SOe uVhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=myl6nRKQ/2lBkAfAPAxtf2MFDBMrGN4kiGAEDqUjhHA=; b=41GOYGMyETp8ENCKo2P4pL1qeZMguhJLC2Clc6+IgyXWMvTWPO40GXTOz0LiOWZbsM vMXYmbgZAE2VWU4VeJgu66VEgW5fQ/4vSfaHs7KCssgygzIlQepUuieocbQBpwjbtjSE gA+5NuoBTZhyNXhGwOCWfQ8BE+BKqKAbfSgjfA4qkp1pz4jWeInmx9CU1UHx7LtSIC7e t1cK/bm8E01tDF0vJXEr6Wpi81TRxvO39DWgzHQgpRQuHDdUmRbAsSnizh8V74S60/Qr FGh27jQjq03RO7b8kYQbkNtvYvcyg4m8Zan7gESLg9rWUwANbjEEQsQBX8KwWqT6gHke Fy6w== X-Gm-Message-State: ACgBeo1Hees3JeXRhtNRR/xMy5q4rLLUTacChOh5jk/GGVS259KarapH IljHmz6fsv2hj9+hmZBq6JifdAM8yMg= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:b808:8d07:ab4a:554c]) (user=glider job=sendgmr) by 2002:a17:906:7310:b0:741:85de:ead0 with SMTP id di16-20020a170906731000b0074185deead0mr26364424ejc.441.1662380702571; Mon, 05 Sep 2022 05:25:02 -0700 (PDT) Date: Mon, 5 Sep 2022 14:24:10 +0200 In-Reply-To: <20220905122452.2258262-1-glider@google.com> Mime-Version: 1.0 References: <20220905122452.2258262-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220905122452.2258262-3-glider@google.com> Subject: [PATCH v6 02/44] stackdepot: reserve 5 extra bits in depot_stack_handle_t From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some users (currently only KMSAN) may want to use spare bits in depot_stack_handle_t. Let them do so by adding @extra_bits to __stack_depot_save() to store arbitrary flags, and providing stack_depot_get_extra_bits() to retrieve those flags. Also adapt KASAN to the new prototype by passing extra_bits=0, as KASAN does not intend to store additional information in the stack handle. Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v4: -- per Marco Elver's request, fold "kasan: common: adapt to the new prototype of __stack_depot_save()" into this patch to prevent bisection breakages. Link: https://linux-review.googlesource.com/id/I0587f6c777667864768daf07821d594bce6d8ff9 --- include/linux/stackdepot.h | 8 ++++++++ lib/stackdepot.c | 29 ++++++++++++++++++++++++----- mm/kasan/common.c | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index bc2797955de90..9ca7798d7a318 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -14,9 +14,15 @@ #include typedef u32 depot_stack_handle_t; +/* + * Number of bits in the handle that stack depot doesn't use. Users may store + * information in them. + */ +#define STACK_DEPOT_EXTRA_BITS 5 depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, + unsigned int extra_bits, gfp_t gfp_flags, bool can_alloc); /* @@ -59,6 +65,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries); +unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle); + int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e73fda23388d8..79e894cf84064 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -43,7 +43,8 @@ #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \ STACK_ALLOC_ALIGN) #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \ - STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS) + STACK_ALLOC_NULL_PROTECTION_BITS - \ + STACK_ALLOC_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS) #define STACK_ALLOC_SLABS_CAP 8192 #define STACK_ALLOC_MAX_SLABS \ (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \ @@ -56,6 +57,7 @@ union handle_parts { u32 slabindex : STACK_ALLOC_INDEX_BITS; u32 offset : STACK_ALLOC_OFFSET_BITS; u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS; + u32 extra : STACK_DEPOT_EXTRA_BITS; }; }; @@ -77,6 +79,14 @@ static int next_slab_inited; static size_t depot_offset; static DEFINE_RAW_SPINLOCK(depot_lock); +unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) +{ + union handle_parts parts = { .handle = handle }; + + return parts.extra; +} +EXPORT_SYMBOL(stack_depot_get_extra_bits); + static bool init_stack_slab(void **prealloc) { if (!*prealloc) @@ -140,6 +150,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->handle.slabindex = depot_index; stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN; stack->handle.valid = 1; + stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); depot_offset += required_size; @@ -382,6 +393,7 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); * * @entries: Pointer to storage array * @nr_entries: Size of the storage array + * @extra_bits: Flags to store in unused bits of depot_stack_handle_t * @alloc_flags: Allocation gfp flags * @can_alloc: Allocate stack slabs (increased chance of failure if false) * @@ -393,6 +405,10 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); * If the stack trace in @entries is from an interrupt, only the portion up to * interrupt entry is saved. * + * Additional opaque flags can be passed in @extra_bits, stored in the unused + * bits of the stack handle, and retrieved using stack_depot_get_extra_bits() + * without calling stack_depot_fetch(). + * * 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 @@ -402,10 +418,11 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); */ depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, + unsigned int extra_bits, gfp_t alloc_flags, bool can_alloc) { struct stack_record *found = NULL, **bucket; - depot_stack_handle_t retval = 0; + union handle_parts retval = { .handle = 0 }; struct page *page = NULL; void *prealloc = NULL; unsigned long flags; @@ -489,9 +506,11 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER); } if (found) - retval = found->handle.handle; + retval.handle = found->handle.handle; fast_exit: - return retval; + retval.extra = extra_bits; + + return retval.handle; } EXPORT_SYMBOL_GPL(__stack_depot_save); @@ -511,6 +530,6 @@ 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(entries, nr_entries, 0, alloc_flags, true); } EXPORT_SYMBOL_GPL(stack_depot_save); diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 69f583855c8be..94caa2d46a327 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -36,7 +36,7 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) 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(entries, nr_entries, 0, flags, can_alloc); } void kasan_set_track(struct kasan_track *track, gfp_t flags) -- 2.37.2.789.g6183377224-goog