From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED2DFC636D3 for ; Tue, 31 Jan 2023 08:54:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62F3B6B0072; Tue, 31 Jan 2023 03:54:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DFED6B0093; Tue, 31 Jan 2023 03:54:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A7756B0095; Tue, 31 Jan 2023 03:54:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 37D8C6B0072 for ; Tue, 31 Jan 2023 03:54:17 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 097EAA01B5 for ; Tue, 31 Jan 2023 08:54:17 +0000 (UTC) X-FDA: 80414482554.01.8028BBC Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by imf08.hostedemail.com (Postfix) with ESMTP id 241B0160015 for ; Tue, 31 Jan 2023 08:54:14 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rAIko5S6; spf=pass (imf08.hostedemail.com: domain of elver@google.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675155255; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=q1SrdDNUlC8jwW9ILBpZQKsxZB0Kk6bmk1dzxOugPqw=; b=7gmlBNvDOj0NgXqNoPJ3tVjCgGf5vC1Vru7BEWjuNbXq0ZjRbTphMwVKL3FQ1/knH0ulMS kx///2GLPxw5kBvTzielduX8HdPHgSrPYbsRCcZSoACfLJjfqdUuDBm+6WEA0vCpgokJmN UiHM/K+NprQct64euEyNLP45mc0Q00k= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rAIko5S6; spf=pass (imf08.hostedemail.com: domain of elver@google.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675155255; a=rsa-sha256; cv=none; b=BN1lv3CrWzCVSg2c0rRB15PFIlXiVQZBuswRP2gDBnUxDMWvbgynOi9VMV0WdpWy/vpJfN cRbAmxRFoUxJ501mA5gVKkawLlCvbFrL1LfdKnrc9c1ynQI/e1KDX7G9JgsFNF1TMBh7Gw SGydsaK1Dd1cD46FgGWMWnNMtLENtb0= Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-4c24993965eso193906507b3.12 for ; Tue, 31 Jan 2023 00:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=q1SrdDNUlC8jwW9ILBpZQKsxZB0Kk6bmk1dzxOugPqw=; b=rAIko5S656uXYnMEPKcMcp9zgAKiuhyYIHq+CtfD0P4YTXB5pDrenATm8Z9aEUO5SS xiSUxuAQOxSGKKFaWmNCMvrwiBWQRZlRPztdqGRL/gHUxdJRfz6BVxqJqI+5v7mWXxcr Kx/nStRUuqMsFlHJ51LQUwmAozVWOmKptPg+kuOthAqQReD57P/bZZS6Sg9Fm1HqJDkV 8XnwiErjgZ+BxdBmZxCjiTwxsFkNF7Aek9A/O091/dZPgvxSbR86nkOxqL4CL2EBrc7u q/D5Fy53PmD3E5mfIL3lgADH0brpnWjGHmdVFr05+fXPACY7AlWbsKluf+YZQcA1r77x UDLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=q1SrdDNUlC8jwW9ILBpZQKsxZB0Kk6bmk1dzxOugPqw=; b=EkOmbZ5ICy9PdQTJOrvW4HqDzEJ4IqsXNOOSSv2BYdLqx+GCEgRHdG1LuEdBShxkeY 8xxUw/fUckBiJbpUzjRE8qFde3pxMHIf+jucJZlRVIVQcr5PCkHibzHVmcwLc1TQXeJh SxQhuQfolmgFgZkbixqBKpJwXb4ZnZHIQHOzQnENyhZxFM0YvQdU+ufWkKa+IxIxNMte u3jjqnGxH+xf6nZ0/7cqUVY6+I+o9bUraM6UcxPdYuJS+zEzqt6oEGeQe2MwjkyvQpbr AEYj2PeoEYanwd7Z5XtDmE+rlMP0+b3ERsQfKPXzfm/ytBonWv8BJyN/jdDondRHujBB 0lhg== X-Gm-Message-State: AFqh2kp7zi2hRrHuGV9cTnu4giMC8mEBLpTEPOVKT0aqz6UaOkPR+AZt jXUmBLSuExv7zSBbv/BN6zAbC7+SSGX/uPomhfXSHA== X-Google-Smtp-Source: AMrXdXuFN4s20Kx9T5tmNLJ44z59TdEQU7GqKqHCpApJ5dnP9JbjylkFb83+PoHMtS5f4TPercZLAZZI9kkdd+3E1Wo= X-Received: by 2002:a81:6d4:0:b0:468:5fe3:7771 with SMTP id 203-20020a8106d4000000b004685fe37771mr6562874ywg.267.1675155254079; Tue, 31 Jan 2023 00:54:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Marco Elver Date: Tue, 31 Jan 2023 09:53:37 +0100 Message-ID: Subject: Re: [PATCH 15/18] lib/stacktrace, kasan, kmsan: rework extra_bits interface To: andrey.konovalov@linux.dev Cc: Alexander Potapenko , Andrey Konovalov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: u8fezm8icgzgw3x5isupy3ztx3s9ttn6 X-Rspam-User: X-Rspamd-Queue-Id: 241B0160015 X-Rspamd-Server: rspam06 X-HE-Tag: 1675155254-414453 X-HE-Meta: U2FsdGVkX19/T4LHEb9iP7VFKDpaNCXS0jj/lnpsL5zHFVL0tWUpD8KEan4RnI9pV2TYpn51s6iKfsrZTq3mtKXnrUlUPh+lgJy/qydbdWfbg2+T1crAn1sFRHc+mNGbI+yc3TIAbQUDyyGaT2refRFS0T0kwxs6d4wz5cjs5viLjXw9JzCiEaDE/MWk3KXun8qCL55UT9mtbdKrobQE4QMFo9j1TT2rrSH2v5k8Fi+4FdH6tjbZqZGZo9bH9VkdQdmXFmRJZwEjFEHE4wLEoaY1D65COHemLax94WZLk8ZprPW5RvK5vjZYfllKkeKBu9f2tyHfUXZxlizrqR2QqOUmZpWSWUDuFhf8pgwRzWgT1XrNoYjHTY2KuIDjbKLNW3CkbpPiKxzSLXnD/KHgg61/AqwJp4lUY86PoDhEQZpYANnOYHDe+38/7UAvLm/xaknqIQTU9KzlIcSMWl0a3zF0lTQ3s5gPaiU3UNJS3F03aa3U8BifHLnFBQw+F2qraGcSVb5e02FXAniwWnLFDXBf+wPZTwSP69GyKNXQwikyQDqyOh2GsRz9O4neL7JjQfY3epFEsjPW5MJnPYNgXiVJ9DinHgxl+p0bFwjzxJPWTTXdDRIOWrY8z+NSQFj3vf6cJi8G82ON07F1T189KQWfQdO7vgYfX46coFBioisQFMABXpJWmXaHewnkwSdNbaOVx8ADQgBNCk7sC2whH9Z/2kWfi88WiqNHX8O4LlZPzWPMuNmguhV1hcRjOx9ysE3Ee1YFFiVK+EJaQJUEr1fObDzcsdm7uEIslQaO71FUrpYdpfYUOvX63SrQP6EM8HPS+MW8HIeKGxif4g7CVkVpvwwYW48A6tL14SUR1ARFags7+Much4wXqElY379dlBIinzbfeXnS8S45VgCIXMdEWqvgm67S8a26wWz1QVHquEjH3GcK0Y27HvpE2W6c+zRdSqj5VP7l9KBR5BN kZDYo+sO Wqx2QEQd6j5OYn5QvlN0+ziox8AuOxzwEIIlfFJ3N5LmfNB5zydcKqQqJUK6+3TOehvUbEqhuvhYVObCnnxXKbNqvYKJBNXf0p6YLfcc44t5DzwaAXVIqKuSd33cqTl5rIxx6MD8BT1H7yxrvNkMw5P85fcugG6RC19SKId/xI6T+d6EMcRj6jH5pL5ID7v9F8unuqLhDKQYDxIr1yKNKgZ/S9ZAXpxvNRwCjOiO7b0XKGaWvYEcpHqTymRvhSJcSWRSdrP2RcYOb1BkIaxtple77BlIPQRvJhzolHKLtNNs2dnDOhKdk4L4/yibgex0gB1/u 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: On Mon, 30 Jan 2023 at 21:51, wrote: > > From: Andrey Konovalov > > The current implementation of the extra_bits interface is confusing: > passing extra_bits to __stack_depot_save makes it seem that the extra > bits are somehow stored in stack depot. In reality, they are only > embedded into a stack depot handle and are not used within stack depot. > > Drop the extra_bits argument from __stack_depot_save and instead provide > a new stack_depot_set_extra_bits function (similar to the exsiting > stack_depot_get_extra_bits) that saves extra bits into a stack depot > handle. > > Update the callers of __stack_depot_save to use the new interace. > > This change also fixes a minor issue in the old code: __stack_depot_save > does not return NULL if saving stack trace fails and extra_bits is used. > > Signed-off-by: Andrey Konovalov > --- > include/linux/stackdepot.h | 4 +++- > lib/stackdepot.c | 38 +++++++++++++++++++++++++++++--------- > mm/kasan/common.c | 2 +- > mm/kmsan/core.c | 10 +++++++--- > 4 files changed, 40 insertions(+), 14 deletions(-) > > diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h > index c4e3abc16b16..f999811c66d7 100644 > --- a/include/linux/stackdepot.h > +++ b/include/linux/stackdepot.h > @@ -57,7 +57,6 @@ static inline int stack_depot_early_init(void) { return 0; } > > 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); > > depot_stack_handle_t stack_depot_save(unsigned long *entries, > @@ -71,6 +70,9 @@ void stack_depot_print(depot_stack_handle_t stack); > int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, > int spaces); > > +depot_stack_handle_t stack_depot_set_extra_bits(depot_stack_handle_t handle, > + unsigned int extra_bits); Can you add __must_check to this function? Either that or making handle an in/out param, as otherwise it might be easy to think that it doesn't return anything ("set_foo()" seems like it sets the information in the handle-associated data but not handle itself ... in case someone missed the documentation). > unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle); > > #endif > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > index 7282565722f2..f291ad6a4e72 100644 > --- a/lib/stackdepot.c > +++ b/lib/stackdepot.c > @@ -346,7 +346,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket, > * > * @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) > * > @@ -358,10 +357,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket, > * 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 > @@ -371,7 +366,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket, > */ > 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; > @@ -461,8 +455,6 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, > if (found) > retval.handle = found->handle.handle; > fast_exit: > - retval.extra = extra_bits; > - > return retval.handle; > } > EXPORT_SYMBOL_GPL(__stack_depot_save); > @@ -483,7 +475,7 @@ 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, 0, alloc_flags, true); > + return __stack_depot_save(entries, nr_entries, alloc_flags, true); > } > EXPORT_SYMBOL_GPL(stack_depot_save); > > @@ -566,6 +558,34 @@ int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, > } > EXPORT_SYMBOL_GPL(stack_depot_snprint); > > +/** > + * stack_depot_set_extra_bits - Set extra bits in a stack depot handle > + * > + * @handle: Stack depot handle > + * @extra_bits: Value to set the extra bits > + * > + * Return: Stack depot handle with extra bits set > + * > + * Stack depot handles have a few unused bits, which can be used for storing > + * user-specific information. These bits are transparent to the stack depot. > + */ > +depot_stack_handle_t stack_depot_set_extra_bits(depot_stack_handle_t handle, > + unsigned int extra_bits) > +{ > + union handle_parts parts = { .handle = handle }; > + > + parts.extra = extra_bits; > + return parts.handle; > +} > +EXPORT_SYMBOL(stack_depot_set_extra_bits); > + > +/** > + * stack_depot_get_extra_bits - Retrieve extra bits from a stack depot handle > + * > + * @handle: Stack depot handle with extra bits saved > + * > + * Return: Extra bits retrieved from the stack depot handle > + */ > unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) > { > union handle_parts parts = { .handle = handle }; > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 833bf2cfd2a3..50f4338b477f 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -43,7 +43,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, 0, flags, can_alloc); > + return __stack_depot_save(entries, nr_entries, flags, can_alloc); > } > > void kasan_set_track(struct kasan_track *track, gfp_t flags) > diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c > index 112dce135c7f..f710257d6867 100644 > --- a/mm/kmsan/core.c > +++ b/mm/kmsan/core.c > @@ -69,13 +69,15 @@ depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags, > { > unsigned long entries[KMSAN_STACK_DEPTH]; > unsigned int nr_entries; > + depot_stack_handle_t handle; > > nr_entries = stack_trace_save(entries, KMSAN_STACK_DEPTH, 0); > > /* Don't sleep (see might_sleep_if() in __alloc_pages_nodemask()). */ > flags &= ~__GFP_DIRECT_RECLAIM; > > - return __stack_depot_save(entries, nr_entries, extra, flags, true); > + handle = __stack_depot_save(entries, nr_entries, flags, true); > + return stack_depot_set_extra_bits(handle, extra); > } > > /* Copy the metadata following the memmove() behavior. */ > @@ -215,6 +217,7 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id) > u32 extra_bits; > int depth; > bool uaf; > + depot_stack_handle_t handle; > > if (!id) > return id; > @@ -250,8 +253,9 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id) > * positives when __stack_depot_save() passes it to instrumented code. > */ > kmsan_internal_unpoison_memory(entries, sizeof(entries), false); > - return __stack_depot_save(entries, ARRAY_SIZE(entries), extra_bits, > - GFP_ATOMIC, true); > + handle = __stack_depot_save(entries, ARRAY_SIZE(entries), GFP_ATOMIC, > + true); > + return stack_depot_set_extra_bits(handle, extra_bits); > } > > void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b, > -- > 2.25.1 >