From patchwork Tue Nov 19 11:20:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Tesarik X-Patchwork-Id: 13879742 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 401DED44142 for ; Tue, 19 Nov 2024 11:20:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5846B6B009B; Tue, 19 Nov 2024 06:20:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 533616B009C; Tue, 19 Nov 2024 06:20:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FBA66B009D; Tue, 19 Nov 2024 06:20:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1C88D6B009B for ; Tue, 19 Nov 2024 06:20:25 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C57D31C5C0F for ; Tue, 19 Nov 2024 11:20:24 +0000 (UTC) X-FDA: 82802599236.25.F57C5C9 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by imf22.hostedemail.com (Postfix) with ESMTP id 3FD1AC0024 for ; Tue, 19 Nov 2024 11:19:20 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=HOPfAEky; spf=pass (imf22.hostedemail.com: domain of ptesarik@suse.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=ptesarik@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732015131; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=fVb/aba7kAugbNKh4JU+Sc2Vx3uCwvVw0Yr+o1yHZGI=; b=j87Iwsr7fEdecOOs0wPk/XcQoz+/ekqkMEKIQPrPU5B8F/AA6WRyDlB+SZO99QkjxRVUwl l5Z4cMUBq7AtiYN7t6jQJ3ehAIYSCXFDh/7LIDX9xDByyG7G/ZNr4hPOgpiSu9wDzHZ+DK dH6AbYDHfGCRgRn9V1eqQ7HxD1WZ7Fs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732015131; a=rsa-sha256; cv=none; b=6cVRS0vPs5OEEWEC3ElR3vdkzO8CsMXfroof3fCxOC380M6cWxFd7b9EaSHclNyxtxelVG jxhWjT7H96dLvY5L6lfM4zsJzUjCJuciqgu8KCDEEPHqr2wEOpEiBenGBlhqTtYu0XzCzy 2YyKGAKplf/8WBRzZDZ1TrPZfUvWc4k= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=HOPfAEky; spf=pass (imf22.hostedemail.com: domain of ptesarik@suse.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=ptesarik@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-43152fa76aaso5106445e9.1 for ; Tue, 19 Nov 2024 03:20:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1732015221; x=1732620021; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=fVb/aba7kAugbNKh4JU+Sc2Vx3uCwvVw0Yr+o1yHZGI=; b=HOPfAEkyuQ037L9B8ZiiLAODJWQL69ypV9wt0AA7k5lHExlb88alskzZ8MbF9CDBSD grn8X5j5Pbz4MIPg48+F0pGbzgsGigzItoN1fog8NOF6dzp5rbdkjTFj0T4b5S86I5+D KlFCHSnjdOuJk7yFImA1950ebnixLjL2sesgNqh0N3H1bFNB07E0BCAAz+VPRRtsW2bJ 6yhlDFRkJ/86NsGuwnXAjR0pq/HAhP7s2i5AOHjzL99vps/SSBzWeZPpJJI/vaGRXC4u OENC5cSr21PngAx9fidFlX44YZIn5IjZF59z4h6YLX9IjcsNRmy0kAN+X8gXXiX2s+Te b6fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732015221; x=1732620021; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fVb/aba7kAugbNKh4JU+Sc2Vx3uCwvVw0Yr+o1yHZGI=; b=Ux2E9ZQU00FP19ScbNSLeNJwtfFAyyoONohfyBu966ALpRp48t3jyHdTuz2gabEf1d bHBo0s6Mvu5o4bcMnceAUcn7Slny2zC7BLm92AYI3C5Y8QDIRoDcjKcj3mSUKLrmjnVr rL3EDAzc6SFpNf3s5D6wIgv+uK0Bstdm7LsUlAbK9PL+04WJiZyAeve91yDtyPFvgwyn PTSwthDKHuBGwnw4xv703an1uOoQxYOa5attDFf1T+foe4wsOweCZQHrGQ6Wv1Jx7eqK CG2y2ZiMA06i67o06LElpmbAfoAEyR+75s4WoijpIbzTa1Lq/bqxiG+fxC7KE6RIKZo1 dUfg== X-Gm-Message-State: AOJu0YzWP0kThhNhv6kIGJ9jQgpkBKeXow75qPWag3Pw97wfQOoEvKUV N6B8KHpCIORn2e4VmBl300lUguQor0yO0bjDzn2+8OzK0RN5EWhliN5OckYhnsM= X-Gm-Gg: ASbGncvHwVMqbqQhKLmf6nNVwXmJ5ezC4dTSoWzsr+q1Zc+tRTOSVaDlxJpmD1pHmkl kvH/qQf/LdYjyssWYdNcfmN0DbaVSCF7xo2tGnvW2TcVwzElyB/7pJtywrfPks47M3XuXnIHLFp lp9Y9d1juAsu6cOJMFEm/Ot/wpN1FKQk634QVqgnJ7CN8PnNItIyVW7Wgjbl4eZ6tCWlnJ8cDfX uUa/V8xJnvG/6ayD6fn0hn1nv0INNF1gMYQC7UCkLk= X-Google-Smtp-Source: AGHT+IE0CgkwbVrPJgyuN/v334z6ESlddBE+ZSqCtlOylTILZqnBOeT5xGKgLIHKCmpm3UVVW48BsQ== X-Received: by 2002:a05:600c:1c29:b0:42c:aeee:80a with SMTP id 5b1f17b1804b1-432df791c54mr58031795e9.7.1732015221222; Tue, 19 Nov 2024 03:20:21 -0800 (PST) Received: from localhost ([193.86.92.181]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432dab80a28sm189428165e9.24.2024.11.19.03.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 03:20:20 -0800 (PST) From: Petr Tesarik To: Andrew Morton , Kees Cook , Jinbum Park Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Petr Tesarik Subject: [PATCH 1/1] mm/rodata_test: Use READ_ONCE() to read const variable Date: Tue, 19 Nov 2024 12:20:09 +0100 Message-ID: <20241119112009.1286675-1-ptesarik@suse.com> X-Mailer: git-send-email 2.46.1 MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Stat-Signature: ig16wxanxs38xrjgew3fdafidjitt4kn X-Rspamd-Queue-Id: 3FD1AC0024 X-Rspam-User: X-HE-Tag: 1732015160-245637 X-HE-Meta: U2FsdGVkX19QnkqS1c3gi5gcYvIfqN4WqOp4qmqyD35YBPqWQzk7zQZR9yN0YanF12CRdWYXdSNcqbxA7hX5RKWXQ3FmEHA2tn5Cqs46v/fCe9mgch5WOArzN86LoLl91o9m36ZnIkMBQEngq8FhRsCCYMPdtQ99gy0Sf0xXuid5rk3g6eYdsNXMV4olxNqpvaFswzEuvZQ6dV0bRFl9fvO98eKSMLjC+R5CNtzl6sSR7ioX0IoOD4yXdqJU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cPZOzGM6WsWIJMJTf6csrSgkUJjZoBkOhrmJBdm+3rkETvZ174amn+flm6h/HltgRd7JDaMpnhOZgDU8UgrzmLJmBVQoA3FyBTF0RtJKIZFy 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: List-Subscribe: List-Unsubscribe: The C compiler may optimize away the memory read of a const variable if its value is known at compile time. In particular, GCC14 with -O2 generates no code at all for test 1, and it generates the following x86_64 instructions for test 3: cmpl $195, 4(%rsp) je .L14 That is, it replaces the read of rodata_test_data with an immediate value and compares it to the value of the local variable "zero". Use READ_ONCE() to undo any such compiler optimizations and enforce a memory read. Fixes: 2959a5f726f6 ("mm: add arch-independent testcases for RODATA") Signed-off-by: Petr Tesarik --- mm/rodata_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/rodata_test.c b/mm/rodata_test.c index 6d783436951f..3b60425d80fe 100644 --- a/mm/rodata_test.c +++ b/mm/rodata_test.c @@ -20,7 +20,7 @@ void rodata_test(void) /* test 1: read the value */ /* If this test fails, some previous testrun has clobbered the state */ - if (!rodata_test_data) { + if (!READ_ONCE(rodata_test_data)) { pr_err("test 1 fails (start data)\n"); return; } @@ -33,7 +33,7 @@ void rodata_test(void) } /* test 3: check the value hasn't changed */ - if (rodata_test_data == zero) { + if (READ_ONCE(rodata_test_data) == zero) { pr_err("test data was changed\n"); return; }