From patchwork Fri Jan 15 17:52:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12023537 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 EA495C433DB for ; Fri, 15 Jan 2021 17:56:09 +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 A0ED322262 for ; Fri, 15 Jan 2021 17:56:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A0ED322262 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=vBbbWjCD7Hf4bpCaXd0R4Fqy9JetpwCbEnGF6aCsUes=; b=kMduCUK7nyZUMWliegkVz6KYg rOH5zDT8wttAja709qAfoVsnvX9kWkVTkwSugt5+6Rpt9XUwzHD9weS7T4t6ly4Jdw1wJZ7xpYnUA EtXbERPww+E5WRZVautZNdd2OGsXhjLvXmrCwHJseoLecNEEYg505PtWOoFYwTM17E27pfHtvPvLR a2QV3KGcSv1dAjyOSjCnvbhwBn59T9ksp2XZva1//K6xwhgpfXT0nkaxUenK3hMKJlxJzTnTuZETm ErY+acdDFQJcbcxvJpXVRC73xMPJcfH64Or2NJKzwH7IvS8XZqOQ2/D0MogHSlPVIv9ApcOtQO+Ea tAFLZQ7CA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l0TIx-0003tK-Fo; Fri, 15 Jan 2021 17:54:35 +0000 Received: from mail-lj1-x249.google.com ([2a00:1450:4864:20::249]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l0THn-0003V3-7M for linux-arm-kernel@lists.infradead.org; Fri, 15 Jan 2021 17:53:30 +0000 Received: by mail-lj1-x249.google.com with SMTP id z26so3370831ljm.18 for ; Fri, 15 Jan 2021 09:53:20 -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=ncx/ADq8yORoljqCdVSCBm5uc5T6Epz7JI4WnQIQm9Y=; b=JyTBPemy3BOiuiPnGHThX969keSgEJXqagqLcGeIwrltiAduseqR9VsulSa+UB561H p9ca1sq4Tqxv6yqpKs/Hzbdl7HMOvCk9HD750NHtTPXCT38/tJ6mIPowusxMc4DrtZ74 rvIa2RankgFhhQ2w0Ax4fJdf1SeCsWdYOYa+z8ZxOGppkDMvFKqaq8dLDsMPu4iu38m3 HgS+q4AdwDawo9HtCCeE3OWWunv7dNe9QF4nZjILkmQ8tIIj2A8aq7zup/5kuZjNBsNH /RshVRfNEtOEPJ0GVqNhWT2HTHv5hhEF1YwOk3UtqlD2k98xBtxF64puSAW+GAopsKjP JQVA== 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=ncx/ADq8yORoljqCdVSCBm5uc5T6Epz7JI4WnQIQm9Y=; b=C7XQKkgCZMt9B40FpeK6zZZNB96x5cQWZWnIws3NrRyI/1EwtpgjrBBMmp2fqTeazM DWVYmXSIuw7rWB98J/ufOLIhWNzDpGJWMZtoLyjHRjJAmjJK8XyHLuSELZb/GEaBwVod reRa4rgDrpPUCcuJzl6fADLGcD1WH8zwrLQ1I2c5rzrQ/EDunDCL1qeOL53GScLywOw2 ooMWzmDaSLRk5/dkHuAYVcPJPmypZ11ESd/Yw3UptOvgUgPf4mhQSEKwKzdSQ2SC8Ijq 5+eGtDBI734Bq7/WeeaInZM8nd1hGM7Iup+FGNzWD9EmgtFSqceMAgDTb3TZkJRsjeQP 1VNw== X-Gm-Message-State: AOAM530OaVaMLvV/gUORjdgOMh1A8U5+fEjRjMrWTiKcNaoVoAOpX3Lj XSs+58v4EAVXvyHImANGTA/xX1vJsv/XgrqL X-Google-Smtp-Source: ABdhPJxkcEq3dTkx3/QICT5fD5ln+9/s4jmGD3uu27CAmlbv7p6OvUWn27f9nqaT6qfXvVOQ8P5WJRnaqf1ZJ63q X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a19:40d7:: with SMTP id n206mr5901814lfa.27.1610733198842; Fri, 15 Jan 2021 09:53:18 -0800 (PST) Date: Fri, 15 Jan 2021 18:52:45 +0100 In-Reply-To: Message-Id: <6f11596f367d8ae8f71d800351e9a5d91eda19f6.1610733117.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v4 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-20210115_125323_422313_E853B57D X-CRM114-Status: GOOD ( 15.43 ) 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 Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- 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) */