linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: Marco Elver <elver@google.com>
Cc: dvyukov@google.com, glider@google.com, andreyknvl@google.com,
	kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] kcsan: Add atomic builtin test case
Date: Mon, 6 Jul 2020 16:45:11 -0700	[thread overview]
Message-ID: <20200706234510.GA20540@paulmck-ThinkPad-P72> (raw)
In-Reply-To: <20200703134031.3298135-3-elver@google.com>

On Fri, Jul 03, 2020 at 03:40:31PM +0200, Marco Elver wrote:
> Adds test case to kcsan-test module, to test atomic builtin
> instrumentation works.
> 
> Signed-off-by: Marco Elver <elver@google.com>

Applied all three, thank you!!!

							Thanx, Paul

> ---
>  kernel/kcsan/kcsan-test.c | 63 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 63 insertions(+)
> 
> diff --git a/kernel/kcsan/kcsan-test.c b/kernel/kcsan/kcsan-test.c
> index fed6fcb5768c..721180cbbab1 100644
> --- a/kernel/kcsan/kcsan-test.c
> +++ b/kernel/kcsan/kcsan-test.c
> @@ -390,6 +390,15 @@ static noinline void test_kernel_seqlock_writer(void)
>  	write_sequnlock_irqrestore(&test_seqlock, flags);
>  }
>  
> +static noinline void test_kernel_atomic_builtins(void)
> +{
> +	/*
> +	 * Generate concurrent accesses, expecting no reports, ensuring KCSAN
> +	 * treats builtin atomics as actually atomic.
> +	 */
> +	__atomic_load_n(&test_var, __ATOMIC_RELAXED);
> +}
> +
>  /* ===== Test cases ===== */
>  
>  /* Simple test with normal data race. */
> @@ -852,6 +861,59 @@ static void test_seqlock_noreport(struct kunit *test)
>  	KUNIT_EXPECT_FALSE(test, match_never);
>  }
>  
> +/*
> + * Test atomic builtins work and required instrumentation functions exist. We
> + * also test that KCSAN understands they're atomic by racing with them via
> + * test_kernel_atomic_builtins(), and expect no reports.
> + *
> + * The atomic builtins _SHOULD NOT_ be used in normal kernel code!
> + */
> +static void test_atomic_builtins(struct kunit *test)
> +{
> +	bool match_never = false;
> +
> +	begin_test_checks(test_kernel_atomic_builtins, test_kernel_atomic_builtins);
> +	do {
> +		long tmp;
> +
> +		kcsan_enable_current();
> +
> +		__atomic_store_n(&test_var, 42L, __ATOMIC_RELAXED);
> +		KUNIT_EXPECT_EQ(test, 42L, __atomic_load_n(&test_var, __ATOMIC_RELAXED));
> +
> +		KUNIT_EXPECT_EQ(test, 42L, __atomic_exchange_n(&test_var, 20, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 20L, test_var);
> +
> +		tmp = 20L;
> +		KUNIT_EXPECT_TRUE(test, __atomic_compare_exchange_n(&test_var, &tmp, 30L,
> +								    0, __ATOMIC_RELAXED,
> +								    __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, tmp, 20L);
> +		KUNIT_EXPECT_EQ(test, test_var, 30L);
> +		KUNIT_EXPECT_FALSE(test, __atomic_compare_exchange_n(&test_var, &tmp, 40L,
> +								     1, __ATOMIC_RELAXED,
> +								     __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, tmp, 30L);
> +		KUNIT_EXPECT_EQ(test, test_var, 30L);
> +
> +		KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 31L, __atomic_fetch_sub(&test_var, 1, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_and(&test_var, 0xf, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 14L, __atomic_fetch_xor(&test_var, 0xf, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 1L, __atomic_fetch_or(&test_var, 0xf0, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, 241L, __atomic_fetch_nand(&test_var, 0xf, __ATOMIC_RELAXED));
> +		KUNIT_EXPECT_EQ(test, -2L, test_var);
> +
> +		__atomic_thread_fence(__ATOMIC_SEQ_CST);
> +		__atomic_signal_fence(__ATOMIC_SEQ_CST);
> +
> +		kcsan_disable_current();
> +
> +		match_never = report_available();
> +	} while (!end_test_checks(match_never));
> +	KUNIT_EXPECT_FALSE(test, match_never);
> +}
> +
>  /*
>   * Each test case is run with different numbers of threads. Until KUnit supports
>   * passing arguments for each test case, we encode #threads in the test case
> @@ -891,6 +953,7 @@ static struct kunit_case kcsan_test_cases[] = {
>  	KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
>  	KCSAN_KUNIT_CASE(test_jiffies_noreport),
>  	KCSAN_KUNIT_CASE(test_seqlock_noreport),
> +	KCSAN_KUNIT_CASE(test_atomic_builtins),
>  	{},
>  };
>  
> -- 
> 2.27.0.212.ge8ba1cc988-goog
> 

  reply	other threads:[~2020-07-06 23:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-03 13:40 [PATCH 1/3] kcsan: Add support for atomic builtins Marco Elver
2020-07-03 13:40 ` [PATCH 2/3] objtool: Add atomic builtin TSAN instrumentation to uaccess whitelist Marco Elver
2020-07-03 13:40 ` [PATCH 3/3] kcsan: Add atomic builtin test case Marco Elver
2020-07-06 23:45   ` Paul E. McKenney [this message]
2020-07-03 14:36 ` [PATCH 1/3] kcsan: Add support for atomic builtins Dmitry Vyukov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200706234510.GA20540@paulmck-ThinkPad-P72 \
    --to=paulmck@kernel.org \
    --cc=andreyknvl@google.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).