Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp224925rdh; Thu, 23 Nov 2023 02:06:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0wqS/JtwLq9JWl4WOvBoHw7NvIreknsfW1YxEsnH0OTjZ2Gi5KlDOya89ixLVgHfsOKsE X-Received: by 2002:a05:6a21:8cc6:b0:187:3765:1795 with SMTP id ta6-20020a056a218cc600b0018737651795mr4791622pzb.40.1700734007075; Thu, 23 Nov 2023 02:06:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700734007; cv=pass; d=google.com; s=arc-20160816; b=JroFPLMQ0ImoEDpeEgxQ14oWq02kQ8dIYBjBLBN6xQ0RsaM/fS8IV0uTswuHwGAK2M gQmfnHz93GO0nuxEyu0FU39MozTUBjRDxVJdIZPM8p2X6DvjDN1E6qauMdegtH4UdPLI Za5m91Un3jyQIy1eBw4hY+chQBTC7BTuqpd/T5sDk8NHb/1lA+1KPJ98QvCTt2Q2hLWc gP9TzNov0dcv4TGkkXm9w//UdUKL/8WrFOlkcDUiDl34F1N0WBu7CkQ3EcIWccydltU+ htIMav8cBaQ9hzv9dxotdBAcsvreBRx/Uf+RuMPANjGcv209a+6QlXodhcwWpj3CQKSh SYHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:from:references:cc:to:subject:user-agent:date :message-id:dkim-signature; bh=B6bR/IFOw82mgJYNFjjPT5zeJmdfBxerdImFv9Qm7Z0=; fh=jbVJpa6y1kFfVaEzIZIbFuVdgktfCZfUyP5Q8nXqkFQ=; b=nke/7P1ZOfC83h07pDWiccDk9Uf9vnmnVcVCYMweyciyFi/IZEV309SfhqmoChf2ib GbG70uHzkOlW0AOUut5EaT79C/sQxvW0yYbADlXu3+ONbGlP96/oDq5tnaCoRQY5WaB3 XDqgwnxpwVCGziXqharaCrHeZmeYmKITlxo+0AdHAg2T0PJBeSZXnnzHfYn8O9joUZIw m2fCZeJIBgpQB5UUjPzZ3vD0DD7N3JcBDMyxzcEGdXabpI9z2LlPOMePF2okcOvRk2r7 VWiVBGzxTRrcgny8BhHLuMC912rn8naNpufzWg5r51ZINF7FtuQonlI+5Z73M5moIq6N owwg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=Sc4CJFDY; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id p3-20020a17090adf8300b0028076e86368si1032452pjv.144.2023.11.23.02.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 02:06:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=Sc4CJFDY; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 5AECB8083492; Thu, 23 Nov 2023 02:06:43 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343878AbjKWKGS (ORCPT + 99 others); Thu, 23 Nov 2023 05:06:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbjKWKGQ (ORCPT ); Thu, 23 Nov 2023 05:06:16 -0500 Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01olkn2096.outbound.protection.outlook.com [40.92.64.96]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EA6E9F for ; Thu, 23 Nov 2023 02:06:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oOn0C3EIc1ubWUe7E+P/uuW7BGe+z0ziVY0wOp4lq4B1Ki2Q+8fkee0cmziSgHFfdXjvA/3L92qh58ohRCEEVxuzhv07ibsL87ymzYKuKlilE4dkR7qpJpGzKaooHY3i7lc8p8nowyhCs7bEgZaPL5einzL1vQSN8doH3qLeQx5mjYMeREX0IozcyqYmvylebPIu/16PeYiOqYJBJHlA8RrlRyYmLGmf8FhWJdIpHUnDE7DX0cLdtK/hcT3GMr3FUMPrXHM7crCDcO9TH9YZxmL1xeR8cWuUlnU9fcm2b/EchFLCm9w8dtZJRopdcCyGVOjTY/O548QemeN4xhY0+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=B6bR/IFOw82mgJYNFjjPT5zeJmdfBxerdImFv9Qm7Z0=; b=fOvfph1dndMEiOMbnbYBbirm+jNlMHGqbyan5BHzabRx/C6WbR56PJPobOxk98g19KOg5x8x5xL9WaQFH3ee874tfgsrAkX1eIm/ypIIux6u0TZ4kzXv3aRBUAp/pYiPofO95p4YcrXmiR3ulXCyxvlylnw23IArCIEt0qotgln8GGFtRbeov2hzCZzjOkFjwg+RvjtG1QwnU327sNgfjk/JeFH2cxL/3XK0VXqMDVaFBnVJGBOarnISRMiswd9gS8B2UEBpeGWun9j+ImarxDPF6KKepUteI6fqzZFaXOc2w+oJXkNg9cMFdVixLDqoNVNv6GroLAPR245hLMXRJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=B6bR/IFOw82mgJYNFjjPT5zeJmdfBxerdImFv9Qm7Z0=; b=Sc4CJFDYJ4IuQpFafga5HylCW9wJZxx94FAjRXvyLQsoAQxgDAggZ6+Iv0rOJ44JMYkfbNv+MJEmNaw7U+rAKdPNtj8p12EiGDoOtxW5XNEW8Nzqtlaq3j7HUoRgbaKMujZUJ25f3qsB4EquioaaIe3ZI9eao6LYU4hy9JjmAhutrChJkOBR4YvawqJtBEhtj0Wm7Yb1zjhHBglOr0EUYO6LFhuMhcrWgZP4sFoklsiZ4he4gEUxb8TcW3PK8xLpf0PCiGYuVgmKgBzSL/6H1/ZQoCzaVYgaezdqzFSpjAxRBSTUGFZBucdTOI3BOX+UrQEiyY2tbRXKRjk7sQ1tCw== Received: from VI1P193MB0752.EURP193.PROD.OUTLOOK.COM (2603:10a6:800:32::19) by DB8P193MB0757.EURP193.PROD.OUTLOOK.COM (2603:10a6:10:145::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.20; Thu, 23 Nov 2023 10:06:18 +0000 Received: from VI1P193MB0752.EURP193.PROD.OUTLOOK.COM ([fe80::fdd2:7dbf:e16c:f4a4]) by VI1P193MB0752.EURP193.PROD.OUTLOOK.COM ([fe80::fdd2:7dbf:e16c:f4a4%5]) with mapi id 15.20.7025.020; Thu, 23 Nov 2023 10:06:18 +0000 Message-ID: Date: Thu, 23 Nov 2023 18:06:18 +0800 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] kasan: Record and report more information To: Dmitry Vyukov Cc: ryabinin.a.a@gmail.com, glider@google.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, akpm@linux-foundation.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org References: From: Juntong Deng In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-TMN: [4kCbHZwt9ARI4aFKbKUlgoB8+hukrqur] X-ClientProxiedBy: LO4P123CA0513.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:272::23) To VI1P193MB0752.EURP193.PROD.OUTLOOK.COM (2603:10a6:800:32::19) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI1P193MB0752:EE_|DB8P193MB0757:EE_ X-MS-Office365-Filtering-Correlation-Id: a7691dfb-4934-43c7-9f92-08dbec0bd59b X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tPSEWYlGUTxoTLGi7jqe/H9GbvZnH27BKIkoA7dGwlqaDjS+2KaBFOhEEdBgtGFdl2BsrZ7WXBc7wQblcdznywAPrNcdxTkmN+W1SKgrvy3wBAGINhhQ8OKPZaUWhrDyO0Owl68lp0JONiQocXYPYC9gGGbKpgrxKe0yp9u9sR7dBfpKLr33tKfMtI9fUeZ/uIQ/kfCX+V1PI5+irlXl2p0rOU9NkcicsiQ088vIAXM2uYX443qONVaVwlkhHcvNYdyTQJWwnS6T7CxdM+KctLObBbttczR0ZW+q82IrGxG2SuCYOY2DBjS/ANax8yvmQCRKXPjm79+cRkzhM2ly8z7uy1Aduc9LVOUM98TzsZxbsc021z8URFEQTLT0N9jjtYxYWFtfORWXRH8JUd0/A27Sozh226gSNgG4ySdj27zeOkZDb2vYS+Bxnc/wHf+EazZcjCyTv0lEWnQk1OQADQhxwDWkNlF61TZIBpKDi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obG5YMDZL?= =?utf-8?Q?Pu5tBXLk0JpwERK0SA1/RDOMrfQ3rkXWuGN5X6IZE2qA=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a7691dfb-4934-43c7-9f92-08dbec0bd59b X-MS-Exchange-CrossTenant-AuthSource: VI1P193MB0752.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Nov 2023 10:06:17.9955 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8P193MB0757 X-Spam-Status: No, score=1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FORGED_MUA_MOZILLA,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,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 pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 23 Nov 2023 02:06:43 -0800 (PST) On 2023/11/20 19:06, Dmitry Vyukov wrote: > On Wed, 15 Nov 2023 at 22:53, Juntong Deng wrote: >> >> On 2023/11/14 15:27, Dmitry Vyukov wrote: >>> On Mon, 13 Nov 2023 at 22:17, Juntong Deng wrote: >>>> >>>> Record and report more information to help us find the cause of >>>> the bug (for example, bugs caused by subtle race condition). >>>> >>>> This patch adds recording and showing CPU number and timestamp at >>>> allocation and free (controlled by CONFIG_KASAN_EXTRA_INFO), and >>>> adds recording and showing timestamp at error occurrence (CPU number >>>> is already shown by dump_stack_lvl). The timestamps in the report use >>>> the same format and source as printk. >>>> >>>> In order to record CPU number and timestamp at allocation and free, >>>> corresponding members need to be added to the relevant data structures, >>>> which may lead to increased memory consumption. >>>> >>>> In Generic KASAN, members are added to struct kasan_track. Since in >>>> most cases, alloc meta is stored in the redzone and free meta is >>>> stored in the object or the redzone, memory consumption will not >>>> increase much. >>>> >>>> In SW_TAGS KASAN and HW_TAGS KASAN, members are added to >>>> struct kasan_stack_ring_entry. Memory consumption increases as the >>>> size of struct kasan_stack_ring_entry increases (this part of the >>>> memory is allocated by memblock), but since this is configurable, >>>> it is up to the user to choose. >>>> >>>> Signed-off-by: Juntong Deng >>>> --- >>>> lib/Kconfig.kasan | 11 +++++++++++ >>>> mm/kasan/common.c | 5 +++++ >>>> mm/kasan/kasan.h | 9 +++++++++ >>>> mm/kasan/report.c | 28 ++++++++++++++++++++++------ >>>> mm/kasan/report_tags.c | 18 ++++++++++++++++++ >>>> mm/kasan/tags.c | 15 +++++++++++++++ >>>> 6 files changed, 80 insertions(+), 6 deletions(-) >>>> >>>> diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan >>>> index fdca89c05745..d9611564b339 100644 >>>> --- a/lib/Kconfig.kasan >>>> +++ b/lib/Kconfig.kasan >>>> @@ -207,4 +207,15 @@ config KASAN_MODULE_TEST >>>> A part of the KASAN test suite that is not integrated with KUnit. >>>> Incompatible with Hardware Tag-Based KASAN. >>>> >>>> +config KASAN_EXTRA_INFO >>>> + bool "Record and report more information" >>>> + depends on KASAN >>>> + help >>>> + Record and report more information to help us find the cause of >>>> + the bug. The trade-off is potentially increased memory consumption >>>> + (to record more information). >>>> + >>>> + Currently the CPU number and timestamp are additionally recorded >>>> + at allocation and free. >>> >>> Hi Juntong, >>> >>> Thanks for working on this. >>> >> >> >> Thanks for your reply! >> >> >>> As a KASAN developer I understand what this is doing, but I am trying >>> to think from a position of a user that does not know details of KASAN >>> implementation. From this position it may be useful to say somewhere >>> that information is recorded "per heap allocation". Perhaps something >>> like: >>> >>> "Currently the CPU number and timestamp are additionally recorded for >>> each heap block at allocation and free time". >> >> >> Yes, I agree, that is a better expression. >> >> >>> >>> Also it's unclear what the memory consumption increase is. You say >>> "potentially|, so may it not increase at all? If it increases, by how >>> much? I obviously want more information, if I can afford it, but I >>> can't understand if I can or not based on this description. I would >>> assume that this may be a problem only for small/embedded devices. >>> Can we provide some ballpark estimation of the memory consumption >>> increase? And somehow say that's probably not an issue for larger >>> machines? >>> >> >> >> How about this expression? >> >> Currently, in order to record CPU number and timestamp, the data >> structure to record allocation and free information will increase >> by 12 bytes. >> >> In Generic KASAN, this affects all allocations less than 32 bytes. >> In SW_TAGS KASAN and HW_TAGS KASAN, depending on the stack_ring_size >> boot parameter increases the memory consumption by >> 12 * stack_ring_size bytes. > > Let's go with this version. > > >>>> endif # KASAN >>>> diff --git a/mm/kasan/common.c b/mm/kasan/common.c >>>> index 256930da578a..7a81566d9d66 100644 >>>> --- a/mm/kasan/common.c >>>> +++ b/mm/kasan/common.c >>>> @@ -20,6 +20,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #include >>>> #include >>>> #include >>>> @@ -50,6 +51,10 @@ void kasan_set_track(struct kasan_track *track, gfp_t flags) >>>> { >>>> track->pid = current->pid; >>>> track->stack = kasan_save_stack(flags, true); >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + track->cpu = raw_smp_processor_id(); >>>> + track->ts_nsec = local_clock(); >>> >>> What does "local_" mean? Is this clock value comparable across CPUs? >>> >> >> >> No, local_clock is the local CPU clock and cannot be used for comparison >> across CPUs, I made a mistake here. >> >> I delved into the clock subsystem of the Linux kernel today and I found >> that we have two choices. >> >> - sched_clock(): based on jiffies, high performance, but on some >> hardware, it will drift between CPUs. >> >> - ktime_get_boot_fast_ns(): based on clocksource, highly accurate, >> can be compared between CPUs, but performance is worse (seqlock). >> >> I tested 100000 calls respectively on my laptop, the average of >> sched_clock() is 17ns and the average of ktime_get_boot_fast_ns() >> is 25ns. >> >> ktime_get_boot_fast_ns() takes about 1.5 times as long as sched_clock(). >> >> With Generic KASAN enabled, the average of one memory allocation is >> 3512ns on my laptop. >> >> Personally, I prefer ktime_get_boot_fast_ns() because it is more >> accurate and the extra time is insignificant for the time required for >> one memory allocation with Generic KASAN enabled. >> >> But maybe using ktime_get_boot_fast_ns() would have a more serious >> impact on small/embedded devices. >> >> Which do you think is the better choice? > > I don't have a strong preference. > > Re drift of sched_clock(), do you mean unsynchronized RDTSC on > different cores? I had the impression that RDTSC is synchronized > across cores on all recent CPUs/systems. > After discussions with Marco Elver, I now think that perhaps continuing to use local_clock() is a better option. The full discussion with Marco Elver can be found at https://groups.google.com/g/kasan-dev/c/zmxwYv8wZTg Because local_clock() is the clock source used by printk and is the default clock source for ftrace. Using local_clock() as the clock source for KASAN reports makes it easier to correlate the timestamps of allocations, frees, and errors with the timestamps of other events in the system. This is perhaps more important than being able to accurately compare across CPUs. What do you think? > >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> } >>>> >>>> #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) >>>> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h >>>> index 8b06bab5c406..b3899a255aca 100644 >>>> --- a/mm/kasan/kasan.h >>>> +++ b/mm/kasan/kasan.h >>>> @@ -187,6 +187,10 @@ static inline bool kasan_requires_meta(void) >>>> struct kasan_track { >>>> u32 pid; >>>> depot_stack_handle_t stack; >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + u32 cpu; >>>> + u64 ts_nsec; >>> >>> This increases the size of meta from 8 to 24 bytes. >>> Can we somehow store the timestamp as 32 bits? Maybe compress the CPU >>> number (it shouldn't be larger than 20 bits?)? >>> I see below we report on microseconds, so we don't low bits of the >>> timestamp as well. >>> >> >> >> Maybe we can use bit field. >> >> struct kasan_track { >> u32 pid; >> depot_stack_handle_t stack; >> #ifdef CONFIG_KASAN_EXTRA_INFO >> u64 cpu:20; >> u64 ts_sec:22; >> u64 ts_usec:22; >> #endif /* CONFIG_KASAN_EXTRA_INFO */ >> }; >> >> For example, 20-bit cpu, 22-bit ts_sec, 22-bit ts_usec, 64 bits >> (8 bytes), and the data structure is 16 bytes in total. > > This looks better. > Can't we have a single field for time instead of ts_sec/usec? > Both sched_clock() and ktime_get_boot_fast_ns() return just u64. > >> If the data structure becomes 16 bytes, it will not affect objects >> larger than 16 bytes. >> >> But the bit field can only be used in struct kasan_track (Generic), >> and cannot be used in struct kasan_stack_ring_entry (SW_TAGS KASAN >> and HW_TAGS KASAN). >> >> Because we need to use READ_ONCE and WRITE_ONCE to read and write >> struct kasan_stack_ring_entry, but READ_ONCE and WRITE_ONCE cannot >> read or write bit field. > > If that's necessary, we could store both values as a single u64 and > the manually pack/unpack. > > >>> I see there is a deficiency in kasan_cache_create(): >>> https://elixir.bootlin.com/linux/latest/source/mm/kasan/generic.c#L412 >>> >>> If free_meta does not fit into the object, we add it after the object. >>> But we could overlap it with the object. >>> For example if the object size is 16 bytes and free_meta size is 24 >>> bytes, we could increase object size to 24, while currently we >>> increase it to 16+24 = 40. >>> We need to place it after the object only if we have these other cases >>> "(cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor". >>> >>> Currently it affects only kmalloc-8 slab. >>> But with this change it will affect at least kmalloc-16 slab as well. >>> >> >> >> I completely agree that we can use both object space and redzone >> space to store free meta, thereby further reducing the extra memory >> consumption caused by KASAN. >> >> Of course, in this case we need to readjust the offset of the >> alloc meta. >> >> If you agree I can make this change in a separate patch. > > This would be good. Thanks. > >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> }; >>>> >>>> enum kasan_report_type { >>>> @@ -202,6 +206,7 @@ struct kasan_report_info { >>>> size_t access_size; >>>> bool is_write; >>>> unsigned long ip; >>>> + u64 ts_nsec; >>>> >>>> /* Filled in by the common reporting code. */ >>>> const void *first_bad_addr; >>>> @@ -278,6 +283,10 @@ struct kasan_stack_ring_entry { >>>> u32 pid; >>>> depot_stack_handle_t stack; >>>> bool is_free; >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + u32 cpu; >>>> + u64 ts_nsec; >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> }; >>>> >>>> struct kasan_stack_ring { >>>> diff --git a/mm/kasan/report.c b/mm/kasan/report.c >>>> index e77facb62900..b6feaf807c08 100644 >>>> --- a/mm/kasan/report.c >>>> +++ b/mm/kasan/report.c >>>> @@ -25,6 +25,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #include >>>> #include >>>> #include >>>> @@ -242,27 +243,40 @@ static void end_report(unsigned long *flags, const void *addr, bool is_write) >>>> >>>> static void print_error_description(struct kasan_report_info *info) >>>> { >>>> + unsigned long rem_usec = do_div(info->ts_nsec, NSEC_PER_SEC) / 1000; >>>> + >>>> pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip); >>>> >>>> if (info->type != KASAN_REPORT_ACCESS) { >>>> - pr_err("Free of addr %px by task %s/%d\n", >>>> - info->access_addr, current->comm, task_pid_nr(current)); >>>> + pr_err("Free of addr %px by task %s/%d at %lu.%06lus\n", >>>> + info->access_addr, current->comm, task_pid_nr(current), >>>> + (unsigned long)info->ts_nsec, rem_usec); >>>> return; >>>> } >>>> >>>> if (info->access_size) >>>> - pr_err("%s of size %zu at addr %px by task %s/%d\n", >>>> + pr_err("%s of size %zu at addr %px by task %s/%d at %lu.%06lus\n", >>>> info->is_write ? "Write" : "Read", info->access_size, >>>> - info->access_addr, current->comm, task_pid_nr(current)); >>>> + info->access_addr, current->comm, task_pid_nr(current), >>>> + (unsigned long)info->ts_nsec, rem_usec); >>>> else >>>> - pr_err("%s at addr %px by task %s/%d\n", >>>> + pr_err("%s at addr %px by task %s/%d at %lu.%06lus\n", >>>> info->is_write ? "Write" : "Read", >>>> - info->access_addr, current->comm, task_pid_nr(current)); >>>> + info->access_addr, current->comm, task_pid_nr(current), >>>> + (unsigned long)info->ts_nsec, rem_usec); >>>> } >>>> >>>> static void print_track(struct kasan_track *track, const char *prefix) >>>> { >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + unsigned long rem_usec = do_div(track->ts_nsec, NSEC_PER_SEC) / 1000; >>>> + >>>> + pr_err("%s by task %u on cpu %d at %lu.%06lus:\n", >>>> + prefix, track->pid, track->cpu, >>>> + (unsigned long)track->ts_nsec, rem_usec); >>>> +#else >>>> pr_err("%s by task %u:\n", prefix, track->pid); >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> if (track->stack) >>>> stack_depot_print(track->stack); >>>> else >>>> @@ -544,6 +558,7 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty >>>> info.access_size = 0; >>>> info.is_write = false; >>>> info.ip = ip; >>>> + info.ts_nsec = local_clock(); >>>> >>>> complete_report_info(&info); >>>> >>>> @@ -582,6 +597,7 @@ bool kasan_report(const void *addr, size_t size, bool is_write, >>>> info.access_size = size; >>>> info.is_write = is_write; >>>> info.ip = ip; >>>> + info.ts_nsec = local_clock(); >>>> >>>> complete_report_info(&info); >>>> >>>> diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c >>>> index 8b8bfdb3cfdb..4d62f1b3e11d 100644 >>>> --- a/mm/kasan/report_tags.c >>>> +++ b/mm/kasan/report_tags.c >>>> @@ -26,6 +26,18 @@ static const char *get_common_bug_type(struct kasan_report_info *info) >>>> return "invalid-access"; >>>> } >>>> >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> +static void kasan_complete_extra_report_info(struct kasan_track *track, >>>> + struct kasan_stack_ring_entry *entry) >>>> +{ >>>> + u32 cpu = READ_ONCE(entry->cpu); >>>> + u64 ts_nsec = READ_ONCE(entry->ts_nsec); >>>> + >>>> + track->cpu = cpu; >>>> + track->ts_nsec = ts_nsec; >>>> +} >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> + >>>> void kasan_complete_mode_report_info(struct kasan_report_info *info) >>>> { >>>> unsigned long flags; >>>> @@ -82,6 +94,9 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) >>>> >>>> info->free_track.pid = pid; >>>> info->free_track.stack = stack; >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + kasan_complete_extra_report_info(&info->free_track, entry); >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> free_found = true; >>>> >>>> /* >>>> @@ -97,6 +112,9 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) >>>> >>>> info->alloc_track.pid = pid; >>>> info->alloc_track.stack = stack; >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + kasan_complete_extra_report_info(&info->alloc_track, entry); >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> alloc_found = true; >>>> >>>> /* >>>> diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c >>>> index 7dcfe341d48e..474ce7e8be8b 100644 >>>> --- a/mm/kasan/tags.c >>>> +++ b/mm/kasan/tags.c >>>> @@ -13,6 +13,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #include >>>> #include >>>> #include >>>> @@ -92,6 +93,17 @@ void __init kasan_init_tags(void) >>>> } >>>> } >>>> >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> +static void save_extra_info(struct kasan_stack_ring_entry *entry) >>>> +{ >>>> + u32 cpu = raw_smp_processor_id(); >>>> + u64 ts_nsec = local_clock(); >>>> + >>>> + WRITE_ONCE(entry->cpu, cpu); >>>> + WRITE_ONCE(entry->ts_nsec, ts_nsec); >>>> +} >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> + >>>> static void save_stack_info(struct kmem_cache *cache, void *object, >>>> gfp_t gfp_flags, bool is_free) >>>> { >>>> @@ -124,6 +136,9 @@ static void save_stack_info(struct kmem_cache *cache, void *object, >>>> WRITE_ONCE(entry->pid, current->pid); >>>> WRITE_ONCE(entry->stack, stack); >>>> WRITE_ONCE(entry->is_free, is_free); >>>> +#ifdef CONFIG_KASAN_EXTRA_INFO >>>> + save_extra_info(entry); >>>> +#endif /* CONFIG_KASAN_EXTRA_INFO */ >>>> >>>> /* >>>> * Paired with smp_load_acquire() in kasan_complete_mode_report_info(). >>>> -- >>>> 2.39.2 >>>> >>