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: 12017315 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 7A7C5C433E6 for ; Wed, 13 Jan 2021 16:25:46 +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 1420D2339D for ; Wed, 13 Jan 2021 16:25:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1420D2339D 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=R4r6h+CwOAOtmmZeeolvR2DniDjb3WbNCtutD+n9EfQ=; b=EV5lXlXI+v8+YBK33uk8pjovZ UV2iwoXV/JAFy70Q1l+sg75Km/xNSjWod/j5MPryDDw+rbK9L9SpW3B2VCF+FauOHOiEtrgT87JBW XERfEd9hYsfWwzvKfGwwJlqQYnv8DTQNQZC5LF0ubP4BDP74TqczKDunFZTa2YKBqMSOeiM7xM0ER LrjoMy93Lt75jlRsTHKis5ZaC0X7gRA483lIzanMtcIXeAPrjg/ke2XpAVm7Zq6qcBotO34Aijlqe vJcEvrNmvWqEkc0gHmhiJD2FDgxOpBuEBNzwtxMtkD849KjjByRWBM8ToSs8TObCpv20jXBcorPvA bpzHGKiOA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kzivS-0007cw-Tl; Wed, 13 Jan 2021 16:23:14 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kziuP-0007G0-5D for linux-arm-kernel@lists.infradead.org; Wed, 13 Jan 2021 16:22:25 +0000 Received: by mail-qk1-x74a.google.com with SMTP id i82so1704126qke.19 for ; Wed, 13 Jan 2021 08:22:06 -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=ONT5wvfyLbq2T280dqpoGQLFjAfUo45I/hnLoY0ewU7JAUM+InEm+2elDQQ9RiC1mH dR7Ka5o/L2wi8R0L4EY3TUKMwPIdSdk2LfQpjrEUKs3/BC8Utoi3cH56xDLJTE7Dh4cA WLzpWzjMaal+7Hv6FCM5fkJbkRaEb7Y2Ja+ecLz+wBdK7lHt8zW5c+O2fzoEZ1JaQ9gy Vg83tlQzkPytDVH1xt6XuSV4bE/OFBLti/1Fy8xBg9Z4UDpS1jyIzj62f4aCmwHkiOgw Mpv2qkCFwS+ZalA6Hw8Bq8nwirgjYLZ3crXEG98JMmm5QUwUSv5tqbytCGQiKls4oGjX Y87w== X-Gm-Message-State: AOAM533F8zo5K7YSsusuL51t5gUhTHjJ1Orm1r53VT2lAbxT+BIQ1C3y WAyvkJzTf/fY41YO2tPo59dLN1Qv02mshAFO 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210113_112209_342014_F16CFAF6 X-CRM114-Status: GOOD ( 15.65 ) 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 , Andrew Morton , 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 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) */