Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1783661ybk; Mon, 11 May 2020 04:16:31 -0700 (PDT) X-Google-Smtp-Source: APiQypKJnC8bzpW4hvKYK0ljtKlDcgru1Xq/WCy5iKxjb35J91W6cbL27yubAiL9idF+fpMjeFIi X-Received: by 2002:a17:907:214f:: with SMTP id rk15mr13096907ejb.301.1589195791737; Mon, 11 May 2020 04:16:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589195791; cv=none; d=google.com; s=arc-20160816; b=nBxgCjboB41HC0XlYzgmxJLBcxEhBcLkk9l0vN3N1JmLEEnf7xJhkZYvjcfrpQ189n zlsVabFFFgweq2obT/6vVqlzJFfC9Gz3T1jGYoOU6oU9WNJA0/bFT5TcP7j+bShog3Rt 9OPjlfkmBOoBdseEH+p6l7cEAg0GA9JvIyx7UBPuCVZYa0lK6ITVXKts8EL1OpFUfVLr QBRqeejtZBh2OC6LwZCs1pMGPDdXqFbc6f3eJbXAIl6dFC4ePPwuv5fecyi7bg3aFxFA WLEPgyA+ssrj+5j61qEQHrUq9gSpz659wrWUuCaUdPRyFUsVBCeWiiSc2VGUXGMnwwGC Kedg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=0Yiq17KeeoPAL2ALedPiCre2LYd91TsHJmWVoUnhISo=; b=jM6kmSJrR2u+G3seP+LER3zUV69VsbOMfV16IlXfrGFaf6IKdqEaElhgyfynYr49FQ au6y5u/R6lUhTFmknEkb34BqP+D0rgXrYUUXKul2A7oWyGSa5Qm/86dwSptiHdeavffp sQPg5Qb9fhy92fYrNdiCYvEljsZtgRUGv0Xj7fgBCWrB5HJ2V7dsLcdHkxhBcjU5JaLh edOLi6bK64xepZVhYAqX+Jtkufjt/S0xv6a7hEq2kOaQUX4V2C8ifuk6qSUp6KTzl2q+ PQG9khAjfU8XcT6rEsLHNbM+pA5bamUJ8rvTb7sPy8WolCM0aE7RPQsJ/9v8UCBnPRJ2 1lgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mXiDmvSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y11si5817262edt.286.2020.05.11.04.16.08; Mon, 11 May 2020 04:16:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mXiDmvSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729476AbgEKLOp (ORCPT + 99 others); Mon, 11 May 2020 07:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725993AbgEKLOo (ORCPT ); Mon, 11 May 2020 07:14:44 -0400 Received: from mail-qv1-xf43.google.com (mail-qv1-xf43.google.com [IPv6:2607:f8b0:4864:20::f43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D27BC061A0C for ; Mon, 11 May 2020 04:14:44 -0700 (PDT) Received: by mail-qv1-xf43.google.com with SMTP id c4so3842046qvi.6 for ; Mon, 11 May 2020 04:14:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0Yiq17KeeoPAL2ALedPiCre2LYd91TsHJmWVoUnhISo=; b=mXiDmvSvTFmjTNGHJW5HppQE/j79m3K1ScMXBDhJDly96MT5S03eU40+S3/A3euASl iUwCx5a83xlJZIJgIUhV2mvAHSkzYre2HwzxM0mg8uwK9WEQvmCtoIa9tOi6EDAxMb9o QbyRE2xHy+xtV6+QjkeW8Xz8Eou2O0qIKxe7UGxNTjJgwk5IaihN1cRpr8pFebgAcn3E 5EYDFlGGnXiKKz96JuYb+cNzxEbgly2ShCZQo0WS13CeIRLWD4CFlrVuOMAKIy0mEE/Q znzuLMzo+UKM7HMfTYr7W2Dg6UEZDranxbv0LL/nkxmuhnjAlelj/evOxXPqTaFw9SBJ 9W0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0Yiq17KeeoPAL2ALedPiCre2LYd91TsHJmWVoUnhISo=; b=rJuy1BIWORHYyTYHY1O3RiEXPE0qiT5ZWo/+OjtjCtHfVG2OcoJnzoRmV+4z5X85Vf VRBULU4zBt88bUYuagwd6Xcm9pA3+YMphRRxjh0o2XBX6Jh6qV0zfhD+V/j31+2eYP8+ 0eG0fHy5VFO29DILMiTW/qmQexmUNRs0NMDD00hbJ5bF1ghAjyDlQZxgpkxgmYR/kyNY Y1YzimraCK29FEbhJz2wxP5EahD4TUiuXRmJ6/HPTKke4i536ZABuVZ93fQ+V+gOeYpz 8c1lG8eYFqPtO42gN88ZhTVPE5zsgIkchiG4jgXpZfWB2kOUFRueV8O8tzNICb4hLXFi VCgQ== X-Gm-Message-State: AGi0Puab/ixRTXUemTC5s/+DX0XnZCWxOXF4bJcVKIoyfjPBKFRiMC9S zKNI0iKRpHxVcQlXiNnLj8cGRB4gpZZSTbgjrm0lxA== X-Received: by 2002:a0c:db03:: with SMTP id d3mr5012984qvk.80.1589195683252; Mon, 11 May 2020 04:14:43 -0700 (PDT) MIME-Version: 1.0 References: <20200511023111.15310-1-walter-zh.wu@mediatek.com> In-Reply-To: <20200511023111.15310-1-walter-zh.wu@mediatek.com> From: Dmitry Vyukov Date: Mon, 11 May 2020 13:14:32 +0200 Message-ID: Subject: Re: [PATCH v2 1/3] rcu/kasan: record and print call_rcu() call stack To: Walter Wu Cc: Andrey Ryabinin , Alexander Potapenko , Matthias Brugger , "Paul E . McKenney" , Josh Triplett , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes , Andrew Morton , kasan-dev , Linux-MM , LKML , Linux ARM , wsd_upstream , linux-mediatek@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 11, 2020 at 4:31 AM Walter Wu wrote: > > This feature will record first and last call_rcu() call stack and > print two call_rcu() call stack in KASAN report. > > When call_rcu() is called, we store the call_rcu() call stack into > slub alloc meta-data, so that KASAN report can print rcu stack. > > It doesn't increase the cost of memory consumption. Because we don't > enlarge struct kasan_alloc_meta size. > - add two call_rcu() call stack into kasan_alloc_meta, size is 8 bytes. > - remove free track from kasan_alloc_meta, size is 8 bytes. > > [1]https://bugzilla.kernel.org/show_bug.cgi?id=198437 > [2]https://groups.google.com/forum/#!searchin/kasan-dev/better$20stack$20traces$20for$20rcu%7Csort:date/kasan-dev/KQsjT_88hDE/7rNUZprRBgAJ > > Signed-off-by: Walter Wu > Suggested-by: Dmitry Vyukov > Cc: Andrey Ryabinin > Cc: Dmitry Vyukov > Cc: Alexander Potapenko > Cc: Andrew Morton > Cc: Paul E. McKenney > Cc: Josh Triplett > Cc: Mathieu Desnoyers > Cc: Lai Jiangshan > Cc: Joel Fernandes > --- > include/linux/kasan.h | 2 ++ > kernel/rcu/tree.c | 3 +++ > lib/Kconfig.kasan | 2 ++ > mm/kasan/common.c | 4 ++-- > mm/kasan/generic.c | 29 +++++++++++++++++++++++++++++ > mm/kasan/kasan.h | 19 +++++++++++++++++++ > mm/kasan/report.c | 21 +++++++++++++++++---- > 7 files changed, 74 insertions(+), 6 deletions(-) > > diff --git a/include/linux/kasan.h b/include/linux/kasan.h > index 31314ca7c635..23b7ee00572d 100644 > --- a/include/linux/kasan.h > +++ b/include/linux/kasan.h > @@ -174,11 +174,13 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } > > void kasan_cache_shrink(struct kmem_cache *cache); > void kasan_cache_shutdown(struct kmem_cache *cache); > +void kasan_record_aux_stack(void *ptr); > > #else /* CONFIG_KASAN_GENERIC */ > > static inline void kasan_cache_shrink(struct kmem_cache *cache) {} > static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} > +static inline void kasan_record_aux_stack(void *ptr) {} > > #endif /* CONFIG_KASAN_GENERIC */ > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > index 06548e2ebb72..de872b6cc261 100644 > --- a/kernel/rcu/tree.c > +++ b/kernel/rcu/tree.c > @@ -57,6 +57,7 @@ > #include > #include > #include > +#include > #include "../time/tick-internal.h" > > #include "tree.h" > @@ -2694,6 +2695,8 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func) > trace_rcu_callback(rcu_state.name, head, > rcu_segcblist_n_cbs(&rdp->cblist)); > > + kasan_record_aux_stack(head); > + > /* Go handle any RCU core processing required. */ > if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) && > unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) { > diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan > index 81f5464ea9e1..56a89291f1cc 100644 > --- a/lib/Kconfig.kasan > +++ b/lib/Kconfig.kasan > @@ -58,6 +58,8 @@ config KASAN_GENERIC > For better error detection enable CONFIG_STACKTRACE. > Currently CONFIG_KASAN_GENERIC doesn't work with CONFIG_DEBUG_SLAB > (the resulting kernel does not boot). > + Currently CONFIG_KASAN_GENERIC will print first and last call_rcu() > + call stack. It doesn't increase the cost of memory consumption. We don't plan to change this and this is not a bug, right? So I think using "Currently" is confusing. What's changing in future? s/will print/prints/ Simple present tense is the default for documentation, we are just stating facts. The remark about not increasing memory consumption is both false and not useful (we don't give an option to change this). I would just say: "In generic mode KASAN prints first and last call_rcu() call stacks in reports." > config KASAN_SW_TAGS > bool "Software tag-based mode" > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 2906358e42f0..8bc618289bb1 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -41,7 +41,7 @@ > #include "kasan.h" > #include "../slab.h" > > -static inline depot_stack_handle_t save_stack(gfp_t flags) > +depot_stack_handle_t kasan_save_stack(gfp_t flags) > { > unsigned long entries[KASAN_STACK_DEPTH]; > unsigned int nr_entries; > @@ -54,7 +54,7 @@ static inline depot_stack_handle_t save_stack(gfp_t flags) > static inline void set_track(struct kasan_track *track, gfp_t flags) > { > track->pid = current->pid; > - track->stack = save_stack(flags); > + track->stack = kasan_save_stack(flags); > } > > void kasan_enable_current(void) > diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c > index 56ff8885fe2e..b86880c338e2 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -325,3 +325,32 @@ DEFINE_ASAN_SET_SHADOW(f2); > DEFINE_ASAN_SET_SHADOW(f3); > DEFINE_ASAN_SET_SHADOW(f5); > DEFINE_ASAN_SET_SHADOW(f8); > + > +void kasan_record_aux_stack(void *addr) > +{ > + struct page *page = kasan_addr_to_page(addr); > + struct kmem_cache *cache; > + struct kasan_alloc_meta *alloc_info; > + void *object; > + > + if (!(page && PageSlab(page))) > + return; > + > + cache = page->slab_cache; > + object = nearest_obj(cache, page, addr); > + alloc_info = get_alloc_info(cache, object); > + > + if (!alloc_info->rcu_stack[0]) > + /* record first call_rcu() call stack */ > + alloc_info->rcu_stack[0] = kasan_save_stack(GFP_NOWAIT); > + else > + /* record last call_rcu() call stack */ > + alloc_info->rcu_stack[1] = kasan_save_stack(GFP_NOWAIT); > +} > + > +struct kasan_track *kasan_get_aux_stack(struct kasan_alloc_meta *alloc_info, > + u8 idx) > +{ > + return container_of(&alloc_info->rcu_stack[idx], > + struct kasan_track, stack); > +} > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index e8f37199d885..1cc1fb7b0de3 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -96,15 +96,28 @@ struct kasan_track { > depot_stack_handle_t stack; > }; > > +#ifdef CONFIG_KASAN_GENERIC > +#define SIZEOF_PTR sizeof(void *) > +#define KASAN_NR_RCU_CALL_STACKS 2 > +#else /* CONFIG_KASAN_GENERIC */ > #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY > #define KASAN_NR_FREE_STACKS 5 > #else > #define KASAN_NR_FREE_STACKS 1 > #endif > +#endif /* CONFIG_KASAN_GENERIC */ > > struct kasan_alloc_meta { > struct kasan_track alloc_track; > +#ifdef CONFIG_KASAN_GENERIC > + /* > + * call_rcu() call stack is stored into struct kasan_alloc_meta. > + * The free stack is stored into freed object. > + */ > + depot_stack_handle_t rcu_stack[KASAN_NR_RCU_CALL_STACKS]; > +#else > struct kasan_track free_track[KASAN_NR_FREE_STACKS]; > +#endif > #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY > u8 free_pointer_tag[KASAN_NR_FREE_STACKS]; > u8 free_track_idx; > @@ -159,16 +172,22 @@ void kasan_report_invalid_free(void *object, unsigned long ip); > > struct page *kasan_addr_to_page(const void *addr); > > +depot_stack_handle_t kasan_save_stack(gfp_t flags); > + > #if defined(CONFIG_KASAN_GENERIC) && \ > (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) > void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); > void quarantine_reduce(void); > void quarantine_remove_cache(struct kmem_cache *cache); > +struct kasan_track *kasan_get_aux_stack(struct kasan_alloc_meta *alloc_info, > + u8 idx); > #else > static inline void quarantine_put(struct kasan_free_meta *info, > struct kmem_cache *cache) { } > static inline void quarantine_reduce(void) { } > static inline void quarantine_remove_cache(struct kmem_cache *cache) { } > +static inline struct kasan_track *kasan_get_aux_stack( > + struct kasan_alloc_meta *alloc_info, u8 idx) { return NULL; } > #endif > > #ifdef CONFIG_KASAN_SW_TAGS > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index 80f23c9da6b0..f16a1a210815 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -105,9 +105,13 @@ static void end_report(unsigned long *flags) > kasan_enable_current(); > } > > -static void print_track(struct kasan_track *track, const char *prefix) > +static void print_track(struct kasan_track *track, const char *prefix, > + bool is_callrcu) > { > - pr_err("%s by task %u:\n", prefix, track->pid); > + if (is_callrcu) > + pr_err("%s:\n", prefix); > + else > + pr_err("%s by task %u:\n", prefix, track->pid); > if (track->stack) { > unsigned long *entries; > unsigned int nr_entries; > @@ -187,11 +191,20 @@ static void describe_object(struct kmem_cache *cache, void *object, > if (cache->flags & SLAB_KASAN) { > struct kasan_track *free_track; > > - print_track(&alloc_info->alloc_track, "Allocated"); > + print_track(&alloc_info->alloc_track, "Allocated", false); > pr_err("\n"); > free_track = kasan_get_free_track(cache, object, tag); > - print_track(free_track, "Freed"); > + print_track(free_track, "Freed", false); > pr_err("\n"); > + > + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { > + free_track = kasan_get_aux_stack(alloc_info, 0); > + print_track(free_track, "First call_rcu() call stack", true); > + pr_err("\n"); > + free_track = kasan_get_aux_stack(alloc_info, 1); > + print_track(free_track, "Last call_rcu() call stack", true); > + pr_err("\n"); > + } > } > > describe_object_addr(cache, object, addr); > -- > 2.18.0 > > -- > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20200511023111.15310-1-walter-zh.wu%40mediatek.com.