Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp44125lfe; Fri, 15 Apr 2022 18:36:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzO43gbtlqZ5+zwnP8d7YuzZdVkpd244wJBPthwEx4wbb8KdPaNJJFpkI9umInrfL78hdPe X-Received: by 2002:a17:90a:4417:b0:1ca:a861:3fbf with SMTP id s23-20020a17090a441700b001caa8613fbfmr7060392pjg.80.1650073009131; Fri, 15 Apr 2022 18:36:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650073009; cv=none; d=google.com; s=arc-20160816; b=nueV1gwEOY82UMSQRz/fHaGDOfkcKNet8mk/Jt5ujVs/rvC7OMUsKnf2vT9STag8d5 MvcFaU9wDvZg7wJVKyktx/3SQ9v+EiYSjIX2/a7UajZ2FhGP8oUjKf8N9ylOA4gIuXGm VG7dTCmk1QAHrgnZOH1qJ/JvWVNYmIinmO1eBgMyR7ZMEI3LVbv3D4cNHi49jOoGeK4Y iPNRqtgYY9K1bwhAe+5tOCvefjXicCCddMt4hGBf27Kipzn2DM9KDZ2vDDaFu9LlbAmv HZks6pp+5RrVwOF2VVdOvnRWI1NaZYBVIrK69DEWL0m2ie/E7eMbLNuOo6otbGdHTA7g 3A9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=6KFOwKoPEIsZwPb2uiiRkr1OqP8mZh6A0ZzLiM9xZxc=; b=HceAsjvhxc/Z5/lQC0aB4Humrvz3ylrd4h+89yEgk/JegpSoqNOPRqlo9WXdF3/iV6 6YOnPm3HSpvSVmamYUxn8tMtWVNBtHLltbtBEfKmk5aVug/+BExmg3Qrd5VaG6cPhAPP JlC9IORoJ80pC79mRe32xm50uNBA1NxfBLlwfS/ZWWZjU70qINraf0MUYMNx3swZIOTq 0f1fZjI+j+fV/lFC56fujjtA4ByC96o8U+SMcKQ1GZKCXVTNyoWgeiLP2t1kro9ow6Qp xzbmWRKuG2apaBfnXyN5pMUDGeAAbJR2QvXE3uU3YQHAx2UterPtvsue6m0apxVbgqa/ r/tA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 134-20020a63028c000000b0039c8da2dd8fsi2715184pgc.430.2022.04.15.18.36.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 18:36:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E1A3123BE0; Fri, 15 Apr 2022 18:02:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236944AbiDNMsk (ORCPT + 99 others); Thu, 14 Apr 2022 08:48:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234116AbiDNMsb (ORCPT ); Thu, 14 Apr 2022 08:48:31 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9BB2191362 for ; Thu, 14 Apr 2022 05:46:06 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 62BBC1424; Thu, 14 Apr 2022 05:46:06 -0700 (PDT) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 133923F70D; Thu, 14 Apr 2022 05:46:03 -0700 (PDT) Date: Thu, 14 Apr 2022 13:46:01 +0100 From: Mark Rutland To: andrey.konovalov@linux.dev Cc: Marco Elver , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Catalin Marinas , Will Deacon , Vincenzo Frascino , Sami Tolvanen , linux-arm-kernel@lists.infradead.org, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: Re: [PATCH v3 2/3] kasan, arm64: implement stack_trace_save_shadow Message-ID: References: <78cd352296ceb14da1d0136ff7d0a6818e594ab7.1649877511.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <78cd352296ceb14da1d0136ff7d0a6818e594ab7.1649877511.git.andreyknvl@google.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Wed, Apr 13, 2022 at 09:26:45PM +0200, andrey.konovalov@linux.dev wrote: > From: Andrey Konovalov > > Implement stack_trace_save_shadow() that collects stack traces based on > the Shadow Call Stack (SCS) for arm64 by copiing the frames from SCS. > > The implementation is best-effort and thus has limitations. > > stack_trace_save_shadow() fully handles task and softirq contexts, which > are both processed on the per-task SCS. > > For hardirqs, the support is limited: stack_trace_save_shadow() does not > collect the task part of the stack trace. For KASAN, this is not a problem, > as stack depot only saves the interrupt part of the stack anyway. > > Otherwise, stack_trace_save_shadow() also takes a best-effort approach > with a focus on performance. Thus, it: > > - Does not try to collect stack traces from other exceptions like SDEI. > - Does not try to recover frames modified by KRETPROBES or by FTRACE. > > However, stack_trace_save_shadow() does strip PTR_AUTH tags to avoid > leaking them in stack traces. > > The -ENOSYS return value is deliberatly used to match > stack_trace_save_tsk_reliable(). > > Signed-off-by: Andrey Konovalov > --- > mm/kasan/common.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 62 insertions(+) As things stand, NAK to this patch, for the reasons I have laid out in my replies to earlier postings and to my reply to the cover letter of this posting. To be clear, that NAK applies regardless of where this is placed within the kernel tree. If we *really* need to have a special unwinder, that should live under arch/arm64/, but my first objection is that it is not necessary. I am more than happy to extend the existing unwinder with some options to minimize overhead (e.g. to stop dumping at an exception boundary), since that sounds useful to you, and I know is relatively simple to implement. Thanks, Mark. > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index d9079ec11f31..23b30fa6e270 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -30,6 +30,68 @@ > #include "kasan.h" > #include "../slab.h" > > +#ifdef CONFIG_SHADOW_CALL_STACK > +#include > +#include > + > +/* > + * Collect the stack trace from the Shadow Call Stack in a best-effort manner: > + * > + * - Do not collect the task part of the stack trace when in a hardirq. > + * - Do not collect stack traces from other exception levels like SDEI. > + * - Do not recover frames modified by KRETPROBES or by FTRACE. > + * > + * Note that marking the function with __noscs leads to unnacceptable > + * performance impact, as helper functions stop being inlined. > + */ > +static inline int stack_trace_save_shadow(unsigned long *store, > + unsigned int size) > +{ > + unsigned long *scs_top, *scs_base, *frame; > + unsigned int len = 0; > + > + /* Get the SCS base. */ > + if (in_task() || in_serving_softirq()) { > + /* Softirqs reuse the task SCS area. */ > + scs_base = task_scs(current); > + } else if (in_hardirq()) { > + /* Hardirqs use a per-CPU SCS area. */ > + scs_base = *this_cpu_ptr(&irq_shadow_call_stack_ptr); > + } else { > + /* Ignore other exception levels. */ > + return 0; > + } > + > + /* > + * Get the SCS pointer. > + * > + * Note that this assembly might be placed before the function's > + * prologue. In this case, the last stack frame will be lost. This is > + * acceptable: the lost frame will correspond to an internal KASAN > + * function, which is not relevant to identify the external call site. > + */ > + asm volatile("mov %0, x18" : "=&r" (scs_top)); > + > + /* The top SCS slot is empty. */ > + scs_top -= 1; > + > + for (frame = scs_top; frame >= scs_base; frame--) { > + if (len >= size) > + break; > + /* Do not leak PTR_AUTH tags in stack traces. */ > + store[len++] = ptrauth_strip_insn_pac(*frame); > + } > + > + return len; > +} > +#else /* CONFIG_SHADOW_CALL_STACK */ > +static inline int stack_trace_save_shadow(unsigned long *store, > + unsigned int size) > +{ > + return -ENOSYS; > +} > +#endif /* CONFIG_SHADOW_CALL_STACK */ > + > depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) > { > unsigned long entries[KASAN_STACK_DEPTH]; > -- > 2.25.1 >