From patchwork Thu Jan 14 19:36:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12020759 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, URIBL_BLOCKED,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 D7B45C433DB for ; Thu, 14 Jan 2021 19:36:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7AA042399C for ; Thu, 14 Jan 2021 19:36:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7AA042399C 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 C46378D0126; Thu, 14 Jan 2021 14:36:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BCE938D0125; Thu, 14 Jan 2021 14:36:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6FE38D0126; Thu, 14 Jan 2021 14:36:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id 808D58D0125 for ; Thu, 14 Jan 2021 14:36:54 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4A82C2C06 for ; Thu, 14 Jan 2021 19:36:54 +0000 (UTC) X-FDA: 77705388348.28.hair78_23101c827529 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id 180D56D6F for ; Thu, 14 Jan 2021 19:36:54 +0000 (UTC) X-HE-Tag: hair78_23101c827529 X-Filterd-Recvd-Size: 6152 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Thu, 14 Jan 2021 19:36:53 +0000 (UTC) Received: by mail-qv1-f74.google.com with SMTP id cc1so5472646qvb.3 for ; Thu, 14 Jan 2021 11:36:53 -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=IeCg6Q6ax6zf9VJ+vRzVtCBhXruC0quH/SAPFfzsvs4=; b=eTZNHRFwfoCF3Fl7H/HMfcHuwyTyfhV52ed+NJ58jBt3yxDyJcqD2BMacpWkq7y2RZ JeMFiKc4vkrxluqmQgLoLhGnVgH3jbfS/EhKMYIHxbRS8cwqVyRwoB4MlN56VxGqSjkn N5XaSiResQQqO6HC6l1fUhmVps7XNA6phTQeeMo2VWh3I9Kjb5T3oNEaK2hAV6UsBwoB rre26sZU5XJxzHLWKInioZ9+zByEt+QVei87qGtDX7XfdwMBxay5paiLav7OJnHNR/Qx YLPTjaN23CuZ90WIjmOX+wro+H5XZuSkLOpwxxmJaTcrJlOXiXWf25ozjg7ke+q0RhHP AhUw== 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=IeCg6Q6ax6zf9VJ+vRzVtCBhXruC0quH/SAPFfzsvs4=; b=awXt5wSwSX9B/Ndv/fZd4ZFx5Ldq2ZFABdniHE19Yjl36ZBVIWdTDcdwKZG9esd5Ke 0dUhjn0YbwE41wNXX2Ge0mZJM10YDuyLOvZl7XEXZqn9y0IxQwW6QeFIYzLyOHslb0XJ QuykZ0kkyq07+kJaNwGjLWeRmNtjUUUdl0mCo8kcbJ8J2v3f4JZvANeVu2m8y8yT6LKR T//Ft+KZ0tv/2RUnRY3d5tiYIAXPdrKGsVmMMr+4VQmWrrgfguKLx2FRLXb3q7rmJjga rH4jlT29hfhYXEvTL4cKY8JKh3tu9VxGZd1rZ6ZT+nVJyKIMyIaAD0M8UfaICKxqmu3s h9gw== X-Gm-Message-State: AOAM532sdSR9LrfHVaaYOfhqrAWICAncpIYf/i0XTzAKPRo7bXEGTN68 cP8UR2BvxKApUB1p+9OHiYwMSwqOXuXFreMb X-Google-Smtp-Source: ABdhPJxfE6NK6yqr9G1+T+44ye3s9ZpD3mdA+ec+MYX3OXmVMzoUVOm3cRqC6z06PsWVassdHCODx3/XP0e2Q/xE X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:f08b:: with SMTP id g11mr8742547qvk.7.1610653012977; Thu, 14 Jan 2021 11:36:52 -0800 (PST) Date: Thu, 14 Jan 2021 20:36:24 +0100 In-Reply-To: Message-Id: <73283ddcceed173966041f9ce1734f50ea3e9a41.1610652890.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v3 08/15] kasan: add compiler barriers to KUNIT_EXPECT_KASAN_FAIL From: Andrey Konovalov To: Andrew Morton , Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: 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 Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 ef663bcf83e5..2419e36e117b 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) */