Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2603727ybk; Mon, 18 May 2020 03:24:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWwuy9J9loI+pi3Co6ktMiT0JQCA1BtEuKLaa5Wpl+ZwnjShAKOeecXGLBU5Zv6uJN4YAv X-Received: by 2002:aa7:de0e:: with SMTP id h14mr13414459edv.82.1589797462532; Mon, 18 May 2020 03:24:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589797462; cv=none; d=google.com; s=arc-20160816; b=ovaw+mFhnwcp0c8v8A/kUpKfDpTqhrT6ciQJVFgtvjf7GgWl/dQlnkVJdmAJLvtbRT AauzV9RQC1L1Lr0p0x2Cttxevs6odDBWyqI0YPPXWBMJnmxm67ZPjE+ZVGylzamzbN72 MvrJFQvxDuhmdAMHoaPJ1ZhdiFTR1m15lfUxQlRp6UgbIpYODD5DXP8RkPDa7bf47dgc BK20qiX0XNebzQQkyts5alimxVSY9eHefoj0VWFTyX2E1F9EUZB7sbS1/Wd4jgXOthWP jRpPmfFQdnS1xU9ez8lLbZTiVirFHxuRwe2+pvoLL4EcLKTbRYK43b+RXS+DH7owZH0z 11Pg== 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=4UrcruNgjCGo7KZXCgHiav/SoRCLnqO+sVxO468jQxk=; b=aTxGDVJJMdnl4VEDU//Zu9GxldiaeHObSvwv7ByzDYIdmTRFPKkQVak0rHuhgiEWYw VAcoIRYyZ9fPkuBkul1imSAFL1+l7lttH8vFARaLQuV8J9JAjcqyJWn9sLqTm3fa0kh/ Ox+fFPdTEYi7bSJU2w0d56B1VC6LHXNJcBpdHp1XoPCkWQdXfb6WZlXHqtFzDuXmZfMD 1ey4vUGZosM0gjV1e3xI5uRKv4XtwLG6+PLWuNXkg05qDI90ua4+j9yqyMRzLIUkBfsW sCn4aBSc5fVnvb9HZjM8ifKkbmrWwYhs+FdhXiF2unBlYBM4dFrvCcAlOCrutloM3qe4 xp0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=XrEl9sHF; 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 t13si5911504edi.528.2020.05.18.03.23.59; Mon, 18 May 2020 03:24:22 -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=XrEl9sHF; 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 S1726374AbgERKVf (ORCPT + 99 others); Mon, 18 May 2020 06:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726127AbgERKVc (ORCPT ); Mon, 18 May 2020 06:21:32 -0400 Received: from mail-qv1-xf41.google.com (mail-qv1-xf41.google.com [IPv6:2607:f8b0:4864:20::f41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AB61C061A0C for ; Mon, 18 May 2020 03:21:32 -0700 (PDT) Received: by mail-qv1-xf41.google.com with SMTP id z9so4393535qvi.12 for ; Mon, 18 May 2020 03:21:32 -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=4UrcruNgjCGo7KZXCgHiav/SoRCLnqO+sVxO468jQxk=; b=XrEl9sHFgivtGiowFoKRu5yIVdUWcaZJdjFIXahTi0v6zxZopJjqS+otsRjUQ0uzI0 UaQVSNdIw3+kjD3YpjRPmBQ3TqveF6YF10CWA1br/+YLHZ4FSfqHDm1qF1SqIYC0IqFI ElcZCUsjbZ1RguikKV5X9z2YWiX3+pnPfuj2GvmcKjtUXFaR2VpOseS/LbtDHhpoCAoW dbN+WMRk18FgeQfVgc1G/WSgpNq9Tg4rc5pANHZKRGc2Pz80r1PBGmJ1HsTSr4LOd4/M vwhHbgQaKqW3bkoFhCqMbZygOJJ28YHvnNkBkja+Lhl+p+4jPBJFFk0kTLQthpdoqHHc Ltaw== 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=4UrcruNgjCGo7KZXCgHiav/SoRCLnqO+sVxO468jQxk=; b=P9VkrAUOhE4uA3AcHbbj0+xFV49zssKyfdSMCieUXHITvwvtBHtmDQ9EHr6BuCLTgM 6GbJ+LzaVoqQ3FoS9p0+W8C3luCxETBU3m8ge2o70UILzxijjsZu6V+ribzNTshQ7pp1 KN7fQLOLDZvznR0ho2Z5YwPdXu5IfHVZe4kHoGlHbqwAOiDCmkfkeyW1hfXbT/YF7JqF M4+Go/+u/gjggNM/0qUqbGUhnaEB92mLrUVnuQ0APS63xnqaw2tEM/N8JwPlzRaNf8xX hp1874yjsAXaT2Mkn2GEHZlR9W0ygEXhqnadzP/fSlQGc5AbA2i3+3A4ER+dDLR5s0F6 lSRg== X-Gm-Message-State: AOAM530GrZu8ygXMtXmXAlM9kSYf8jNetZ6U5U1/KaMpQ0nogkL0hpxk 7lKJ858o1tbRGBgcVM20ZSqdzjxmtPkyQCfpoWwqEw== X-Received: by 2002:a0c:db03:: with SMTP id d3mr15524799qvk.80.1589797291392; Mon, 18 May 2020 03:21:31 -0700 (PDT) MIME-Version: 1.0 References: <20200518062603.4570-1-walter-zh.wu@mediatek.com> In-Reply-To: <20200518062603.4570-1-walter-zh.wu@mediatek.com> From: Dmitry Vyukov Date: Mon, 18 May 2020 12:21:19 +0200 Message-ID: Subject: Re: [PATCH v3 1/4] 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 18, 2020 at 8:26 AM Walter Wu wrote: > > This feature will record the last two call_rcu() call stack and > prints up to 2 call_rcu() call stacks in KASAN report. > > When call_rcu() is called, we store the call_rcu() call stack into > slub alloc meta-data, so that the KASAN report can print rcu stack. > > [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 | 2 ++ > lib/Kconfig.kasan | 2 ++ > mm/kasan/common.c | 4 ++-- > mm/kasan/generic.c | 20 ++++++++++++++++++++ > mm/kasan/kasan.h | 10 ++++++++++ > mm/kasan/report.c | 24 ++++++++++++++++++++++++ > 7 files changed, 62 insertions(+), 2 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..36a4ff7f320b 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" > @@ -2668,6 +2669,7 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func) > head->func = func; > head->next = NULL; > local_irq_save(flags); > + kasan_record_aux_stack(head); > rdp = this_cpu_ptr(&rcu_data); > > /* Add the callback to our list. */ > diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan > index 81f5464ea9e1..4e83cf6e3caa 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). > + In generic mode KASAN prints the last two 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..78d8e0a75a8a 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -325,3 +325,23 @@ 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); > + > + /* record last two call_rcu() call stacks */ > + if (alloc_info->rcu_stack[0]) Do we need this if? If we do "alloc_info->rcu_stack[1] = alloc_info->rcu_stack[0]" unconditionally, then we will just move 0 from [0] to [1], which should be 0 at this point anyway. I think it will be more reasonable to rename rcu_stack to aux_stack, the function that stores the stacks is kasan_record_aux_stack. > + alloc_info->rcu_stack[1] = alloc_info->rcu_stack[0]; > + alloc_info->rcu_stack[0] = kasan_save_stack(GFP_NOWAIT); > +} > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index e8f37199d885..870c5dd07756 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -104,7 +104,15 @@ struct kasan_track { > > 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 struct kasan_free_meta. > + */ > + depot_stack_handle_t rcu_stack[2]; > +#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,6 +167,8 @@ 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); > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index 80f23c9da6b0..5ee66cf7e27c 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -179,6 +179,17 @@ static struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, > return &alloc_meta->free_track[i]; > } > > +#ifdef CONFIG_KASAN_GENERIC > +static void print_stack(depot_stack_handle_t stack) > +{ > + unsigned long *entries; > + unsigned int nr_entries; > + > + nr_entries = stack_depot_fetch(stack, &entries); > + stack_trace_print(entries, nr_entries, 0); > +} > +#endif > + > static void describe_object(struct kmem_cache *cache, void *object, > const void *addr, u8 tag) > { > @@ -192,6 +203,19 @@ static void describe_object(struct kmem_cache *cache, void *object, > free_track = kasan_get_free_track(cache, object, tag); > print_track(free_track, "Freed"); > pr_err("\n"); > + > +#ifdef CONFIG_KASAN_GENERIC > + if (alloc_info->rcu_stack[0]) { > + pr_err("Last one call_rcu() call stack:\n"); > + print_stack(alloc_info->rcu_stack[0]); > + pr_err("\n"); > + } > + if (alloc_info->rcu_stack[1]) { > + pr_err("Second to last call_rcu() call stack:\n"); > + print_stack(alloc_info->rcu_stack[1]); > + pr_err("\n"); > + } > +#endif > } > > 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/20200518062603.4570-1-walter-zh.wu%40mediatek.com.