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: 12020735 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=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 03807C433E9 for ; Thu, 14 Jan 2021 19:40:23 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3D711239E4 for ; Thu, 14 Jan 2021 19:40:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3D711239E4 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=241u7RYeNbFYDO2rMuHJ+Lll8Bndlhva9wFjLMseE18=; b=vEkqi5DCdzV+WK5WSl83bVuUL tvHa3KUEFrAkJOuhQ7u/9sW2whqCuqw/bazd1eaXMo8y7q3j5JpxjjznYiy1Njc3WGW2tBVhistaT 1XgAYYnnEY2mbjhtW8agFIUaR/9xc5Y8cTBrrudy2+iq4HrOqgWQ84apFduGERAFtgKWZoV7OX/60 doJbwGMLPkhgkkTRtqamKP+DWMbBJ8Of1pygZpD17us2VR4UJLNly6vGLky8zj++qAYgBir+6E/DD qCxqtHxozEoNfQ+dRorgtExXdABU1RLYlaVRjWTJSxxjSKmyBkGWQ1qu8c15RCX5FmxnFigmwjI7M IbnroyK5g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08Rr-0004od-Df; Thu, 14 Jan 2021 19:38:23 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08QS-0004Ek-T5 for linux-arm-kernel@lists.infradead.org; Thu, 14 Jan 2021 19:37:06 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id j5so5433236qvu.22 for ; Thu, 14 Jan 2021 11:36:54 -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=lcCcLZX6WH9qFxbkAXc7T3tbjX7xF6AhTFJqXb040RMDXCh64TqSNwmUGg5b4nHmNR hT63A76SgWEOzykw6QMrUmhOAMeE2mv3n2wq7cdc2dFEmsM9vB7VSTCcTFhWES9lJKri epeKkqiWNL1cKrmGScoNRpzOt9j7/Yw6lsOEDcQLKpX4b1nrlH0A4LjFmyuK0gq24TYm XMTQmDi5cMW4okeMkIlhtyU1RPS1JqZTSUhgp7RIIS9u1i/j1z0SfGauwi/Bb7KPKV2u JWuwu8DDha4XYUNG83jLeAL2qf5D8OB3XrdetJ+DeV5pLHVc9l3xuXYVC3aqRzd8hoaR jPcg== X-Gm-Message-State: AOAM530Fahn1IggQ/HXnCw5DHXPKm7CGwf2nyvcBw3cYSE0L9AC2ROko cqwudsu7G0lzrP2YPfFOYqYRaj0jKzmBLPI/ 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210114_143657_036523_FAD8B9A1 X-CRM114-Status: GOOD ( 15.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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) */