Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp717483ybk; Wed, 20 May 2020 10:10:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzt56SG5UMcxFyhdVRrIC1NNPwNcYx1HhE/bRe6ceOiOdDNwzlHPCpTOontFt5AS0nKoTfa X-Received: by 2002:a17:906:90d4:: with SMTP id v20mr65558ejw.476.1589994644966; Wed, 20 May 2020 10:10:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589994644; cv=none; d=google.com; s=arc-20160816; b=I4u8SKYHl7gSpxVGnJ0ZAP81jQZ7HqDCVdLICO3jA5Lmwxt81tXpJ5dtKQmMjMC8Ge rOsSLthrDelxQj9RDuHCadw0Gi70qDq8tJVDKg/n2N/OC7Uvqw5hpydp8PA7IgOr/PGn KUqfenIrhAVh6ymeD8/KUbd+gXbjq8g7Mj1z6Z+6HKan3IPLJ86yTGfCA0W6OUpneWV0 sjHbY0eUlRwVwJYyXaLkWOm+syP2uB8UsaiC84yKESxNNn5MSCpBPsMix9qtuxhh38+u vLngwal0mux7ove12/YMw90UssMiavK6p6oSq6lkFkYVQrKwcUytdhbhSPrEy5BWx2YH xi5g== 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=Qv8ZHds2eTWQ0EUWvVRBqR2tiVx11jw6rlo5/Ez4dVU=; b=G6WQ4BxxKcS3zZgtjUViFN1Yqb30HdWeyc6cDvb4x9hSdYv3NLMImKJ6xYhzrXqM4W Wrp+8LTmVLuJ7ZgpwDCYyPk2Cc6Aq7onKnahXevEj8HzRzXA0CKHB4b/aULgouSS1ZgN /xrxaN6QANiAqgbJEOZWDKqzR2rUBV1BgH8qJeDdtgmUoHpI8Ddf6elyF08+vRvzgOX6 AD4j5woZQH8Ip4SecZzxc6opaQkNQORsdcOtDGLEGIk3IL9v44nPBUeZwGbXSB4/JNIs jConCwHznIZzDQh86A6R6H14sQ04vFa9Q+PSh4kZ04pHtne3svVuKog5Jo1r2iFh0RqB a4fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ekEr6ftz; 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 i14si1933666ejp.246.2020.05.20.10.10.22; Wed, 20 May 2020 10:10:44 -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=ekEr6ftz; 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 S1727890AbgETRIv (ORCPT + 99 others); Wed, 20 May 2020 13:08:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726826AbgETRIs (ORCPT ); Wed, 20 May 2020 13:08:48 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F9D5C061A0E for ; Wed, 20 May 2020 10:08:48 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id t7so1600911plr.0 for ; Wed, 20 May 2020 10:08:48 -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=Qv8ZHds2eTWQ0EUWvVRBqR2tiVx11jw6rlo5/Ez4dVU=; b=ekEr6ftzVAUjjcsBED4SnDlqh9A4pHKPU+xFnRxYBzmTT21fgJmCYvrpZ1IAaFkfzf pLjfT7sRtGLVd3urJCmlFiOKYirE8S0ZWGEPWb70W975pj8242keMrKUw3krxh9ghg7o o65AOnXNsIwTE9sonu1p0DKFwJo6+Kk9Zcm8vTrkYz0iaDiBzwGsB+DMuHFmArLQRc4D 7q4QQfD+VNNF9kAtD7n7fYLHHFBKMZaQ1AOWQ7DbGYl6ZHIgY7BtQpdp3c6aCwtvDEuZ Gi0Hl5loTwhjUHqBYb4dZRbGmm9gRCnBqb+YfFQjbuGuiWiouCdVvYs6NR5U22c019tC sEnQ== 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=Qv8ZHds2eTWQ0EUWvVRBqR2tiVx11jw6rlo5/Ez4dVU=; b=MNKU6RY6qrlAqEj969Fng9Sbn6sFRrCqLEkgAMiIzJrOx+J8FpkiYTOuf/33BPEizt ThajYMXztGWhp91VrSNT/v4/QrHwPRyywrJx7WEIPfFu0pEoEclFO582Tj+leKJJOOyr 1uzSS33nduIo3Ncjjym262nVgd/PIE02IXvLVixwlKhX+lS4+TZx/V2gF3fXaK+5XiZJ UvAV/JA84n+3+YxC1Z9yHBiQylGAzoqZjNFNs0sEsjFRljgwhD+brjfAUIhA1ENVLh28 HTrHG44Yhhq1jDCguhmCW9XG3+etB34+j3Jh44M1OckY8nBOUqz+b3pjsBXadEQOrtLC 0B5g== X-Gm-Message-State: AOAM533bu2hNwxo6CmQlLPmTubiXT4CILDgsoE74i4VmoxRbblgBFZ69 VbM/lqmvMTUyJCGzzr0peho7xRMgn70+YTVnsG+a9g== X-Received: by 2002:a17:90a:21c9:: with SMTP id q67mr6204249pjc.166.1589994527943; Wed, 20 May 2020 10:08:47 -0700 (PDT) MIME-Version: 1.0 References: <20200520123434.3888-1-walter-zh.wu@mediatek.com> In-Reply-To: <20200520123434.3888-1-walter-zh.wu@mediatek.com> From: Andrey Konovalov Date: Wed, 20 May 2020 19:08:36 +0200 Message-ID: Subject: Re: [PATCH v5 1/4] rcu/kasan: record and print call_rcu() call stack To: Walter Wu Cc: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , "Paul E . McKenney" , Josh Triplett , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes , Andrew Morton , kasan-dev , Linux Memory Management List , 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 Wed, May 20, 2020 at 2:34 PM Walter Wu wrote: > > This feature will record the last two call_rcu() call stacks 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 > Acked-by: Paul E. McKenney > Cc: Andrey Ryabinin > Cc: Dmitry Vyukov > Cc: Alexander Potapenko > Cc: Andrew Morton > Cc: Josh Triplett > Cc: Mathieu Desnoyers > Cc: Lai Jiangshan > Cc: Joel Fernandes > Cc: Andrey Konovalov > --- > include/linux/kasan.h | 2 ++ > kernel/rcu/tree.c | 2 ++ > mm/kasan/common.c | 4 ++-- > mm/kasan/generic.c | 21 +++++++++++++++++++++ > mm/kasan/kasan.h | 10 ++++++++++ > mm/kasan/report.c | 24 ++++++++++++++++++++++++ > 6 files changed, 61 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/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..8acf48882ba2 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -325,3 +325,24 @@ 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 the last two call_rcu() call stacks. > + */ > + alloc_info->aux_stack[1] = alloc_info->aux_stack[0]; > + alloc_info->aux_stack[0] = kasan_save_stack(GFP_NOWAIT); > +} > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index e8f37199d885..a7391bc83070 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 aux_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..29a801d5cd74 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -105,6 +105,17 @@ static void end_report(unsigned long *flags) > kasan_enable_current(); > } > > +#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 The idea of moving it here was to reuse print_stack() in print_track() :) > + > static void print_track(struct kasan_track *track, const char *prefix) > { > pr_err("%s by task %u:\n", prefix, track->pid); > @@ -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->aux_stack[0]) { > + pr_err("Last call_rcu():\n"); > + print_stack(alloc_info->aux_stack[0]); > + pr_err("\n"); > + } > + if (alloc_info->aux_stack[1]) { > + pr_err("Second to last call_rcu():\n"); > + print_stack(alloc_info->aux_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/20200520123434.3888-1-walter-zh.wu%40mediatek.com.