Received: by 2002:a05:7208:20d2:b0:82:bbfa:f723 with SMTP id z18csp39977rbz; Tue, 7 May 2024 09:57:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWuYLsz0SsLm7+cmLEX8hEBVil3OLV3p1P34k5NoGBnOKiIpL+Jbz82V/O5r2uEM3P6nS0yrK1qgE02amtAtiUQdycFB2y+yW1dHYyXUw== X-Google-Smtp-Source: AGHT+IF045Cbf3hLD510BeVzmqTgeaai4TWWc0J1ep8CY6jVrvsrxrau2/kwMTxuch7vOjJXkhxU X-Received: by 2002:aca:1113:0:b0:3c3:de8e:5411 with SMTP id 5614622812f47-3c98530627dmr115445b6e.38.1715101042736; Tue, 07 May 2024 09:57:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715101042; cv=pass; d=google.com; s=arc-20160816; b=N4p5oyBo2mYvuB9PBMqw0WPzqklCjp/iyaEMDWpodlYAVzB+p6MC8D5uR6I756P50A ehjNPb75XZv85pkg4eHSPd6qw1ppHGzia6AfrUOzwT0GlXOkpfEawSSVdalBth/+etWS uODhpLVO8qqM9emX4dQcnLoGJ4GQjXY1g6/tsTzIPDj5Q2xOlnTvlwTD5LnVZDq5I6Rb xVaulqR6XtWbTPCNr7n777+Lha3EQHtY02BXnJqx6PSw5G1NvutNyvZm8OBcx/K2bNjA ks+N5H7h50hxau3iwB3dGhlVgyxqWPx0RXqHrjGWnPNXn6Kk0u8X3ucPFzP4Foyp544x LFdQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=+EMAYasugQJlF/eIJC9doRqtMdUsWD58uq5OF+PT44M=; fh=da2oyvUOV2BW9gyf5zGIr+iw3AXc8Nd7+4aoYUaZj+s=; b=a9pz1p6ajrsrxCyqLOTcAbuKiDZIj2Hs17eV5Gv/vMpFw4RFv0Dpj5h9nr6TrO+o8Z 2CBoFwmnPap0g1uLrye9PfC1TZ6IJJmsbbegfIALLfut8cirEULzge8tP+tHe+rx9WgD 0Bopk0n+9K2rKw32p+4kn3IzRTkusuRDpHqnao+CbdqjngNkGsyNYWBAfYAr4924in1n DcUWZRR0g7gAqf+uoUPW6kxRlhvBKN0da77yeZPXu3j3aJNFTJqHzlKp+kF5dexbSWHf Hntm7NDiiiLVT/uuWhVTz4DXSGSBd/pZPtvdSEsB1jItrQZj5XPGOnwXA7L1FKkLusNa zwVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="E/G+nh7M"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171885-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171885-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id pt4-20020a056214048400b006993cf9de84si9886331qvb.343.2024.05.07.09.57.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 09:57:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171885-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="E/G+nh7M"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-171885-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171885-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 858121C22101 for ; Tue, 7 May 2024 16:57:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F274354BEA; Tue, 7 May 2024 16:56:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E/G+nh7M" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06B8916C443 for ; Tue, 7 May 2024 16:56:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715101006; cv=none; b=CGEzzxkBZBeJ71Jbh4XcHF4CF0spKXMjfzpp25+K1MUdkQATfRl9zlu7eAxVWVr+3iACqs9mOAyJUHsw0mBQlyjtFklsRCcEehlJQg006NJlnvKb1rEFWO4ZCDFAu5RyVC9dQz3Q0+di6S1pzsWSbFEjew9KTIV3cGRY8muold8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715101006; c=relaxed/simple; bh=cpSnBOlxoobeW93e7dJEkEHD7EOpMpJthLmloLNdINo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AED3SmmgcN6YX2nGxn9girPsPh5It6tPcEyq5IxCXHQ0rjiP6wW1d4o9vnX3K7+3pLTgs6dLIYtfEW8Ve2ewfW6xQQoyn434+LJYCR0M6/yLsX+PuClMZQ5EZv1PgJh40ePVgL2gFKT1UIHiYCjwA8F7WFWLn/0lbZzLgA3PvGk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E/G+nh7M; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAF1CC4DDE1; Tue, 7 May 2024 16:56:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715101005; bh=cpSnBOlxoobeW93e7dJEkEHD7EOpMpJthLmloLNdINo=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=E/G+nh7MycQtNscnJTuv0PuvL1b+CIRXIRXtEJYIdUxxCuB8qLiRlLSYx8GFul1sM 5Sl79y1bvolz5SfuGBoV+/jdcYBxmvQSXhU8zGcYgf+GTrXMM2Ws5UbO2NKLeGpaAj 6pS2RaTP17vUcOjLvaOCmJvGA4X6ricYA4nR+2TnIt8q59pv3nnk3bTL1Uj1dLyzby wWTiCajH3dWEUlZIMvsz8horcyw08ZT6D96TLfblS7RPznZvyI2+P7EMlDH2lLavDj HS70QamYgMSx5berMmBEkUJ1LaRq3/F/sG+kyS15mqKbekaZOo4TR0QO8ZGxyDs4MC xY+PJAPEJAtuA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 839E3CE14CC; Tue, 7 May 2024 09:56:45 -0700 (PDT) Date: Tue, 7 May 2024 09:56:45 -0700 From: "Paul E. McKenney" To: Marco Elver Cc: Linus Torvalds , Mark Rutland , Dmitry Vyukov , Alexander Potapenko , Boqun Feng , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Tetsuo Handa Subject: Re: [PATCH] kcsan, compiler_types: Introduce __data_racy type qualifier Message-ID: Reply-To: paulmck@kernel.org References: <20240502141242.2765090-1-elver@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240502141242.2765090-1-elver@google.com> On Thu, May 02, 2024 at 04:12:17PM +0200, Marco Elver wrote: > Based on the discussion at [1], it would be helpful to mark certain > variables as explicitly "data racy", which would result in KCSAN not > reporting data races involving any accesses on such variables. To do > that, introduce the __data_racy type qualifier: > > struct foo { > ... > int __data_racy bar; > ... > }; > > In KCSAN-kernels, __data_racy turns into volatile, which KCSAN already > treats specially by considering them "marked". In non-KCSAN kernels the > type qualifier turns into no-op. > > The generated code between KCSAN-instrumented kernels and non-KCSAN > kernels is already huge (inserted calls into runtime for every memory > access), so the extra generated code (if any) due to volatile for few > such __data_racy variables are unlikely to have measurable impact on > performance. > > Link: https://lore.kernel.org/all/CAHk-=wi3iondeh_9V2g3Qz5oHTRjLsOpoy83hb58MVh=nRZe0A@mail.gmail.com/ [1] > Suggested-by: Linus Torvalds > Signed-off-by: Marco Elver > Cc: Paul E. McKenney > Cc: Tetsuo Handa I have queued and pushed this, thank you! I have started testing, and if all goes well I will rebase this on top of v6.9-rc2 (same base as the rest of my commits for next merge window), merge it in and push it out. With a little luck, this will get it into tomorrow's -next. With more luck than anyone deserves, today's. Thanx, Paul > --- > Documentation/dev-tools/kcsan.rst | 10 ++++++++++ > include/linux/compiler_types.h | 7 +++++++ > kernel/kcsan/kcsan_test.c | 17 +++++++++++++++++ > 3 files changed, 34 insertions(+) > > diff --git a/Documentation/dev-tools/kcsan.rst b/Documentation/dev-tools/kcsan.rst > index 94b6802ab0ab..02143f060b22 100644 > --- a/Documentation/dev-tools/kcsan.rst > +++ b/Documentation/dev-tools/kcsan.rst > @@ -91,6 +91,16 @@ the below options are available: > behaviour when encountering a data race is deemed safe. Please see > `"Marking Shared-Memory Accesses" in the LKMM`_ for more information. > > +* Similar to ``data_race(...)``, the type qualifier ``__data_racy`` can be used > + to document that all data races due to accesses to a variable are intended > + and should be ignored by KCSAN:: > + > + struct foo { > + ... > + int __data_racy stats_counter; > + ... > + }; > + > * Disabling data race detection for entire functions can be accomplished by > using the function attribute ``__no_kcsan``:: > > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h > index 2abaa3a825a9..a38162a8590d 100644 > --- a/include/linux/compiler_types.h > +++ b/include/linux/compiler_types.h > @@ -273,9 +273,16 @@ struct ftrace_likely_data { > * disable all instrumentation. See Kconfig.kcsan where this is mandatory. > */ > # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation > +/* > + * Type qualifier to mark variables where all data-racy accesses should be > + * ignored by KCSAN. Note, the implementation simply marks these variables as > + * volatile, since KCSAN will treat such accesses as "marked". > + */ > +# define __data_racy volatile > # define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused > #else > # define __no_kcsan > +# define __data_racy > #endif > > #ifndef __no_sanitize_or_inline > diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c > index 015586217875..0c17b4c83e1c 100644 > --- a/kernel/kcsan/kcsan_test.c > +++ b/kernel/kcsan/kcsan_test.c > @@ -304,6 +304,7 @@ static long test_array[3 * PAGE_SIZE / sizeof(long)]; > static struct { > long val[8]; > } test_struct; > +static long __data_racy test_data_racy; > static DEFINE_SEQLOCK(test_seqlock); > static DEFINE_SPINLOCK(test_spinlock); > static DEFINE_MUTEX(test_mutex); > @@ -358,6 +359,8 @@ static noinline void test_kernel_write_uninstrumented(void) { test_var++; } > > static noinline void test_kernel_data_race(void) { data_race(test_var++); } > > +static noinline void test_kernel_data_racy_qualifier(void) { test_data_racy++; } > + > static noinline void test_kernel_assert_writer(void) > { > ASSERT_EXCLUSIVE_WRITER(test_var); > @@ -1009,6 +1012,19 @@ static void test_data_race(struct kunit *test) > KUNIT_EXPECT_FALSE(test, match_never); > } > > +/* Test the __data_racy type qualifier. */ > +__no_kcsan > +static void test_data_racy_qualifier(struct kunit *test) > +{ > + bool match_never = false; > + > + begin_test_checks(test_kernel_data_racy_qualifier, test_kernel_data_racy_qualifier); > + do { > + match_never = report_available(); > + } while (!end_test_checks(match_never)); > + KUNIT_EXPECT_FALSE(test, match_never); > +} > + > __no_kcsan > static void test_assert_exclusive_writer(struct kunit *test) > { > @@ -1424,6 +1440,7 @@ static struct kunit_case kcsan_test_cases[] = { > KCSAN_KUNIT_CASE(test_read_plain_atomic_rmw), > KCSAN_KUNIT_CASE(test_zero_size_access), > KCSAN_KUNIT_CASE(test_data_race), > + KCSAN_KUNIT_CASE(test_data_racy_qualifier), > KCSAN_KUNIT_CASE(test_assert_exclusive_writer), > KCSAN_KUNIT_CASE(test_assert_exclusive_access), > KCSAN_KUNIT_CASE(test_assert_exclusive_access_writer), > -- > 2.45.0.rc1.225.g2a3ae87e7f-goog >