From nobody Fri Dec 19 17:35:18 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0029ECAAA1 for ; Thu, 15 Sep 2022 15:04:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230249AbiIOPE4 (ORCPT ); Thu, 15 Sep 2022 11:04:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230083AbiIOPEq (ORCPT ); Thu, 15 Sep 2022 11:04:46 -0400 Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7146E9FF5 for ; Thu, 15 Sep 2022 08:04:44 -0700 (PDT) Received: by mail-ej1-x64a.google.com with SMTP id du20-20020a17090772d400b0077df99c5ab3so5450990ejc.3 for ; Thu, 15 Sep 2022 08:04:44 -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=gS6j99y09BMp9BDjMRS1RUcbMakKgpTeqeZX6PzPrFgNC6JUyxdSvKRCjzIAswJzFY RACNurD1UwAw++C5CP/8IVwFLhwKh6jwIU1EimPsdHz7lSOgoWBUNH7rd2WrZNpDm2OE SKlmmmiGBzBNa7fDj8PHiGO2iT7RaZ12zTCZWYSTNArLj8SV2RvSCJhna5aBD06OlEsK MYVW63jPXWNru9dTAIlZZzh9S7iv3j2jcZqjzbtOPGMy+DXVJjDMb9AzkTghV1DwnKI/ qubMAz4mMwtItFPbaU7zU0DcS9Lc1NXUE92xo2vdruTx6cGeJ7VV8XEa8cy4Yoam+vkP l4cQ== 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=4Pt6jsXykdUHNgTs6A6uCOZP2qJv/l9URGA8f1MYE9mXh1Bi+e5MDS27gam2LZ5bBf 9Xr71GvBCRQpue3+dT3cLHo1otUT4RTBlqV62t9LHpIlklyQI1ccVNmQQdSlnub3HWTe fRjQiRwDa6cItZWZRg9qdJspoTG1X1F5qJLKAEz32Hkhs1ih3icqkWnUZ2PwU8YiRBuU bP95MNHywFmz797VjNlaa60/4igBczmldgKe1eKBy9Qb9taL0ydeoAlz+2qjnxhwBQ0e VkrusIBVFHwN/Zc83JcGleO9f9DQjzaUsx2MrL1RIIMdiFyL4xJpFK03U2/J8KByQVJ0 vrDQ== X-Gm-Message-State: ACrzQf20GcaKm4W352gme0zeq1/0dy0xLm3CX51Jwh4kpQJcsxuQE4/J R3k3AXzFPSX+6jaTjzDqjEqygprQT44= X-Google-Smtp-Source: AMsMyM5cIaAUAU3AA+LWPRVGPrLc1LHdkwZUDXgPvqFuYO3BlghU78yjZTOSc74hno8rab0+nl/oMOdeVBQ= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:43cc:b0:451:129e:1b1e with SMTP id p12-20020a05640243cc00b00451129e1b1emr273421edc.258.1663254282917; Thu, 15 Sep 2022 08:04:42 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:36 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-3-glider@google.com> Subject: [PATCH v7 02/43] 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 Biggers , 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 , Stephen Rothwell , 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D0, 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/I0587f6c777667864768daf07821= d594bce6d8ff9 --- 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 =20 typedef u32 depot_stack_handle_t; +/* + * Number of bits in the handle that stack depot doesn't use. Users may st= ore + * information in them. + */ +#define STACK_DEPOT_EXTRA_BITS 5 =20 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); =20 /* @@ -59,6 +65,8 @@ depot_stack_handle_t stack_depot_save(unsigned long *entr= ies, unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries); =20 +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 siz= e, int spaces); =20 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; }; }; =20 @@ -77,6 +79,14 @@ static int next_slab_inited; static size_t depot_offset; static DEFINE_RAW_SPINLOCK(depot_lock); =20 +unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) +{ + union handle_parts parts =3D { .handle =3D 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 =3D depot_index; stack->handle.offset =3D depot_offset >> STACK_ALLOC_ALIGN; stack->handle.valid =3D 1; + stack->handle.extra =3D 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); depot_offset +=3D required_size; =20 @@ -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 u= p to * interrupt entry is saved. * + * Additional opaque flags can be passed in @extra_bits, stored in the unu= sed + * bits of the stack handle, and retrieved using stack_depot_get_extra_bit= s() + * 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. Current= ly * 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 =3D NULL, **bucket; - depot_stack_handle_t retval =3D 0; + union handle_parts retval =3D { .handle =3D 0 }; struct page *page =3D NULL; void *prealloc =3D 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 =3D found->handle.handle; + retval.handle =3D found->handle.handle; fast_exit: - return retval; + retval.extra =3D extra_bits; + + return retval.handle; } EXPORT_SYMBOL_GPL(__stack_depot_save); =20 @@ -511,6 +530,6 @@ depot_stack_handle_t stack_depot_save(unsigned long *en= tries, 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 c= an_alloc) unsigned int nr_entries; =20 nr_entries =3D 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); } =20 void kasan_set_track(struct kasan_track *track, gfp_t flags) --=20 2.37.2.789.g6183377224-goog