From patchwork Wed Jan 13 16:21:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12017285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 667C1C433E0 for ; Wed, 13 Jan 2021 16:22:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 146EB2313C for ; Wed, 13 Jan 2021 16:22:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 146EB2313C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 83B4E8D0073; Wed, 13 Jan 2021 11:22:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8141D8D0076; Wed, 13 Jan 2021 11:22:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 728CE8D0073; Wed, 13 Jan 2021 11:22:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0006.hostedemail.com [216.40.44.6]) by kanga.kvack.org (Postfix) with ESMTP id 5633E8D0076 for ; Wed, 13 Jan 2021 11:22:07 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1705D1EFD for ; Wed, 13 Jan 2021 16:22:07 +0000 (UTC) X-FDA: 77701268694.07.ducks65_2816fd82751f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin07.hostedemail.com (Postfix) with ESMTP id E52421803F496 for ; Wed, 13 Jan 2021 16:22:06 +0000 (UTC) X-HE-Tag: ducks65_2816fd82751f X-Filterd-Recvd-Size: 6102 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Wed, 13 Jan 2021 16:22:05 +0000 (UTC) Received: by mail-qv1-f74.google.com with SMTP id m1so1843928qvp.0 for ; Wed, 13 Jan 2021 08:22:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=1ElsZsJrO3cOsYp/vFzZufoQKn9+sd3vP1RO0nASQZ0=; b=vLdGASrWREAy+wj9sfLUQB8j+yuVmoSbMdRaRJXHZRPSwkP8jB8knKlgUr+3AQR9Ve +r4vu4nQaMgAPKzfyhmAnJD0GwU6xEzluvKdeR9lktNoMnhyjqR2SbxVWzWiTKW3yd8A dxzNhIdB5+2+uFtg8x0VySNNDiMNMTODEvZz/GwlMWaIXxmHrfBNWrul1YExWcwZe8dq ErFPQ5XWd+oRZYvm3QY1ynahqe5O0lzPJ4CWD5ZbJ02KeB2Jlc94Cg++JguL9yPQXGhb Dnm7IxJww7M1CbKv0pids9Wc5g1KRkXybcD1YhSQUrQhlRSOZpo13kWx8rJLimwmCi4/ gOhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1ElsZsJrO3cOsYp/vFzZufoQKn9+sd3vP1RO0nASQZ0=; b=dkrUZO/xGxf9SxAJOkNZX6+dGY74gcU7IawzaiQ0S3YAZKAyYIY/QOz0XJtF+eQxBL vyc6mJ5kM0UYMyKN+ZljJwQimrCcUD1i2gGzngsUEFZmTLDJCrE8bTMn7ow6YbWG+MrZ ZqGcdaLnh+/0glAord6AeKLa83VVDVMvIqIkYm6gVwCXoOxKZMcXKy6B177uj0Zx3sh0 BKDSLiNVlcJYAiBIBgKTPoinI1H3B6klc+csR0IfKw7GDMVDPKzaKm6Upih5dAK9OH8V dlvduBbuSnqADXGJe/yD4u32SSE7THivQoKrUbIbzakSu82pd+xYaLy1zIYhL/zLshBi sZ0Q== X-Gm-Message-State: AOAM531fTa3HUCAXYIoyRJAOoznk7akXeoOt0s8pjmjH+v0/OVp7KPkY uo2akHKc5ZWB+8U5Nvjxx1zsvRCqkDdOELp3 X-Google-Smtp-Source: ABdhPJzRVpbzs6eSZ4YpS+Iv4DdXN1T6DSp2/3sND1u+wQPzWYQWt3sUVkNPSbg16ucwUAEsnMF3mPkHaU9ACJ77 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:fd89:: with SMTP id p9mr3153133qvr.8.1610554925220; Wed, 13 Jan 2021 08:22:05 -0800 (PST) Date: Wed, 13 Jan 2021 17:21:35 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 08/14] kasan: add compiler barriers to KUNIT_EXPECT_KASAN_FAIL From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: Andrew Morton , Will Deacon , Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov 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: It might not be obvious to the compiler that the expression must be executed between writing and reading to fail_data. In this case, the compiler might reorder or optimize away some of the accesses, and the tests will fail. Add compiler barriers around the expression in KUNIT_EXPECT_KASAN_FAIL and use READ/WRITE_ONCE() for accessing fail_data fields. Link: https://linux-review.googlesource.com/id/I046079f48641a1d36fe627fc8827a9249102fd50 Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- lib/test_kasan.c | 17 ++++++++++++----- mm/kasan/report.c | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 5c8aa3a5ce93..283feda9882a 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -68,23 +68,30 @@ static void kasan_test_exit(struct kunit *test) * normally auto-disabled. When this happens, this test handler reenables * tag checking. As tag checking can be only disabled or enabled per CPU, this * handler disables migration (preemption). + * + * Since the compiler doesn't see that the expression can change the fail_data + * fields, it can reorder or optimize away the accesses to those fields. + * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the + * expression to prevent that. */ #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \ if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \ migrate_disable(); \ - fail_data.report_expected = true; \ - fail_data.report_found = false; \ + WRITE_ONCE(fail_data.report_expected, true); \ + WRITE_ONCE(fail_data.report_found, false); \ kunit_add_named_resource(test, \ NULL, \ NULL, \ &resource, \ "kasan_data", &fail_data); \ + barrier(); \ expression; \ + barrier(); \ KUNIT_EXPECT_EQ(test, \ - fail_data.report_expected, \ - fail_data.report_found); \ + READ_ONCE(fail_data.report_expected), \ + READ_ONCE(fail_data.report_found)); \ if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \ - if (fail_data.report_found) \ + if (READ_ONCE(fail_data.report_found)) \ hw_enable_tagging(); \ migrate_enable(); \ } \ diff --git a/mm/kasan/report.c b/mm/kasan/report.c index e93d7973792e..234f35a84f19 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -331,7 +331,7 @@ static void kasan_update_kunit_status(struct kunit *cur_test) } kasan_data = (struct kunit_kasan_expectation *)resource->data; - kasan_data->report_found = true; + WRITE_ONCE(kasan_data->report_found, true); kunit_put_resource(resource); } #endif /* IS_ENABLED(CONFIG_KUNIT) */