From patchwork Wed May 6 05:20:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Wu X-Patchwork-Id: 11530217 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E20014B4 for ; Wed, 6 May 2020 05:21:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 30F4A206E6 for ; Wed, 6 May 2020 05:21:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="Z7OBg9xi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 30F4A206E6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 519168E0005; Wed, 6 May 2020 01:21:03 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4A1CE8E0003; Wed, 6 May 2020 01:21:03 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 342E78E0005; Wed, 6 May 2020 01:21:03 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0035.hostedemail.com [216.40.44.35]) by kanga.kvack.org (Postfix) with ESMTP id 168668E0003 for ; Wed, 6 May 2020 01:21:03 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id CB47E381E for ; Wed, 6 May 2020 05:21:02 +0000 (UTC) X-FDA: 76785145164.23.heart71_432d5b6c4114a X-Spam-Summary: 2,0,0,3390b23163b06610,d41d8cd98f00b204,walter-zh.wu@mediatek.com,,RULES_HIT:1:2:41:355:379:541:800:960:966:967:968:973:988:989:1185:1260:1277:1311:1313:1314:1345:1431:1437:1514:1515:1516:1518:1585:1605:1730:1747:1777:1792:1981:2194:2196:2198:2199:2200:2201:2393:2525:2559:2563:2682:2685:2693:2740:2859:2892:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4051:4250:4321:4385:4605:5007:6261:6653:7514:7875:7903:7904:8555:8603:8660:8957:9025:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12521:12555:12683:12698:12737:12895:12986:13141:13146:13148:13230:14394:21080:21212:21451:21627:21772:21990:30003:30054:30056:30070:30075,0,RBL:210.61.82.183:@mediatek.com:.lbl8.mailshell.net-64.100.201.201 62.2.12.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMA RY:none X-HE-Tag: heart71_432d5b6c4114a X-Filterd-Recvd-Size: 13693 Received: from mailgw01.mediatek.com (unknown [210.61.82.183]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Wed, 6 May 2020 05:21:01 +0000 (UTC) X-UUID: 191ea745ffdf44fda16005a881de4ce5-20200506 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=ETHA4HWY0Hog3Z3ytmN4JoSnKcZh6hg7h8BcVESgFcc=; b=Z7OBg9xitJL35hAeqtKSKbKumbZEqXe0Wui4mk4FS94lLngMcOdrsGiLMyalsZ+/fHAti+AosMaV5Vv6Q083XYnHDU7zxXxAQJULXG/hzTaKtbxcI94A3Ob4Rm/mTrA13SHEfJV+Om7rbO4E6VpF7pKmj56x9lLcABAMR75pARc=; X-UUID: 191ea745ffdf44fda16005a881de4ce5-20200506 Received: from mtkcas07.mediatek.inc [(172.21.101.84)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 1905688655; Wed, 06 May 2020 13:20:57 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs01n2.mediatek.inc (172.21.101.79) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 6 May 2020 13:20:54 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 6 May 2020 13:20:54 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , "Paul E . McKenney" , Josh Triplett , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes , Andrew Morton CC: , , , , wsd_upstream , , Walter Wu Subject: [PATCH 1/3] rcu/kasan: record and print call_rcu() call stack Date: Wed, 6 May 2020 13:20:46 +0800 Message-ID: <20200506052046.14451-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-TM-SNTS-SMTP: 207E808D1826991DB0A16DB3C4D0C374BC1D58B8654CEFD3C6BB5AFBE7FBB2F42000:8 X-MTK: N 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: When call_rcu() is called, we store the call_rcu() call stack into slub alloc meta-data, so that KASAN report prints call_rcu() information. We add new KASAN_RCU_STACK_RECORD configuration option. It will record first and last call_rcu() call stack and KASAN report will print two call_rcu() call stack. This option 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 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 | 7 +++++++ kernel/rcu/tree.c | 4 ++++ lib/Kconfig.kasan | 11 +++++++++++ mm/kasan/common.c | 23 +++++++++++++++++++++++ mm/kasan/kasan.h | 12 ++++++++++++ mm/kasan/report.c | 33 +++++++++++++++++++++++++++------ 6 files changed, 84 insertions(+), 6 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 31314ca7c635..5eeece6893cd 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -96,6 +96,12 @@ size_t kasan_metadata_size(struct kmem_cache *cache); bool kasan_save_enable_multi_shot(void); void kasan_restore_multi_shot(bool enabled); +#ifdef CONFIG_KASAN_RCU_STACK_RECORD +void kasan_record_callrcu(void *ptr); +#else +static inline void kasan_record_callrcu(void *ptr) {} +#endif + #else /* CONFIG_KASAN */ static inline void kasan_unpoison_shadow(const void *address, size_t size) {} @@ -165,6 +171,7 @@ static inline void kasan_remove_zero_shadow(void *start, static inline void kasan_unpoison_slab(const void *ptr) { } static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } +static inline void kasan_record_callrcu(void *ptr) {} #endif /* CONFIG_KASAN */ diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 06548e2ebb72..145c79becf7b 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,9 @@ __call_rcu(struct rcu_head *head, rcu_callback_t func) trace_rcu_callback(rcu_state.name, head, rcu_segcblist_n_cbs(&rdp->cblist)); + if (IS_ENABLED(CONFIG_KASAN_RCU_STACK_RECORD)) + kasan_record_callrcu(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..022934049cc2 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -158,6 +158,17 @@ config KASAN_VMALLOC for KASAN to detect more sorts of errors (and to support vmapped stacks), but at the cost of higher memory usage. +config KASAN_RCU_STACK_RECORD + bool "Record and print call_rcu() call stack" + depends on KASAN_GENERIC + help + By default, the KASAN report doesn't print call_rcu() call stack. + It is very difficult to analyze memory issues(e.g., use-after-free). + + Enabling this option will print first and last call_rcu() call stack. + It doesn't enlarge slub alloc meta-data size, so it doesn't increase + the cost of memory consumption. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASAN diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 2906358e42f0..32d422bdf127 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -299,6 +299,29 @@ struct kasan_free_meta *get_free_info(struct kmem_cache *cache, return (void *)object + cache->kasan_info.free_meta_offset; } +#ifdef CONFIG_KASAN_RCU_STACK_RECORD +void kasan_record_callrcu(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_free_stack[0]) + /* record first call_rcu() call stack */ + alloc_info->rcu_free_stack[0] = save_stack(GFP_NOWAIT); + else + /* record last call_rcu() call stack */ + alloc_info->rcu_free_stack[1] = save_stack(GFP_NOWAIT); +} +#endif static void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index e8f37199d885..adc105b9cd07 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -96,15 +96,27 @@ struct kasan_track { depot_stack_handle_t stack; }; +#ifdef CONFIG_KASAN_RCU_STACK_RECORD +#define BYTES_PER_WORD 4 +#define KASAN_NR_RCU_FREE_STACKS 2 +#else /* CONFIG_KASAN_RCU_STACK_RECORD */ #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY #define KASAN_NR_FREE_STACKS 5 #else #define KASAN_NR_FREE_STACKS 1 #endif +#endif /* CONFIG_KASAN_RCU_STACK_RECORD */ struct kasan_alloc_meta { struct kasan_track alloc_track; +#ifdef CONFIG_KASAN_RCU_STACK_RECORD + /* call_rcu() call stack is stored into kasan_alloc_meta. + * free stack is stored into freed object. + */ + depot_stack_handle_t rcu_free_stack[KASAN_NR_RCU_FREE_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; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 80f23c9da6b0..7aaccc70b65b 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; @@ -159,8 +163,22 @@ static void describe_object_addr(struct kmem_cache *cache, void *object, (void *)(object_addr + cache->object_size)); } +#ifdef CONFIG_KASAN_RCU_STACK_RECORD +static void kasan_print_rcu_free_stack(struct kasan_alloc_meta *alloc_info) +{ + struct kasan_track free_track; + + free_track.stack = alloc_info->rcu_free_stack[0]; + print_track(&free_track, "First call_rcu() call stack", true); + pr_err("\n"); + free_track.stack = alloc_info->rcu_free_stack[1]; + print_track(&free_track, "Last call_rcu() call stack", true); + pr_err("\n"); +} +#endif + static struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, - void *object, u8 tag) + void *object, u8 tag, const void *addr) { struct kasan_alloc_meta *alloc_meta; int i = 0; @@ -187,11 +205,14 @@ 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"); + free_track = kasan_get_free_track(cache, object, tag, addr); + print_track(free_track, "Freed", false); pr_err("\n"); +#ifdef CONFIG_KASAN_RCU_STACK_RECORD + kasan_print_rcu_free_stack(alloc_info); +#endif } describe_object_addr(cache, object, addr);