From patchwork Tue Nov 19 11:37:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Tesarik X-Patchwork-Id: 13879759 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 76934D44148 for ; Tue, 19 Nov 2024 11:39:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16CE76B009B; Tue, 19 Nov 2024 06:39:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11CB66B009E; Tue, 19 Nov 2024 06:39:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F27006B009F; Tue, 19 Nov 2024 06:39:04 -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 D85566B009B for ; Tue, 19 Nov 2024 06:39:04 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8C781403A5 for ; Tue, 19 Nov 2024 11:39:04 +0000 (UTC) X-FDA: 82802646612.21.8B7015B Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by imf11.hostedemail.com (Postfix) with ESMTP id 6846F4000A for ; Tue, 19 Nov 2024 11:38:00 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=aEuE9+86; spf=pass (imf11.hostedemail.com: domain of ptesarik@suse.com designates 209.85.128.51 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=1732016251; 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:in-reply-to:references:references:dkim-signature; bh=fVb/aba7kAugbNKh4JU+Sc2Vx3uCwvVw0Yr+o1yHZGI=; b=wC2N5AVMvLtupAHErIRXAo4L2TDqzxLX1yfhlLlMIzTLkO43LR1RzyxmD8YgkEHhVpwOdV Ob1k3pseKbeXEXeDYFQR3XelguC21zyMkUcVrtXMRCOCN9JKkOzEOslkACNmuzImzSGkkl ubr0UydppKsWU9VpSaBrQmSWM/mXrRQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732016251; a=rsa-sha256; cv=none; b=5OLOKaixlCkauIWrmX/ZwomgyZJTI8XBFeZIPpnyM7gGSiVluCBbeJCDZmLedoiW/1sVwb NHdJVdus4MRl0X+tbqtI9VORItIxJUecLfNB8dA9wMEKTN5WQsXYCyFI8rWuXx8AUTmt9V FZvatcv8grSyu4IJ4gqakiteuO8eP/k= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=aEuE9+86; spf=pass (imf11.hostedemail.com: domain of ptesarik@suse.com designates 209.85.128.51 as permitted sender) smtp.mailfrom=ptesarik@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-431506d4f3bso3046975e9.0 for ; Tue, 19 Nov 2024 03:39:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1732016341; x=1732621141; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fVb/aba7kAugbNKh4JU+Sc2Vx3uCwvVw0Yr+o1yHZGI=; b=aEuE9+86nWzgD0RyDqJG9Fa5tEPFfugJtXJ2/qRAmToMU/bBJkOmekz85UxVyiyHqq 4X7t3rdc1I6FTVMrM42/4+d+MR2DKXEgkDpcmukoEq+VocVddxvUNLMuFfw9lUt5fGw1 NN6BJJrNwoGVQy6G7fmddM+R8AXilFT8ol3+/D/a3lQQMeUh7p1n5Nu9+t1y7xx7C0AE LX1YFiGy5/9cUolo89zA8C0dnHIBbqCOBvDerifYTwDY00kmCKS7nrvlleStuASEJlHK 3ebya5NDYgzi5skLrvnVCiFRI6/AjKCjpBWos4Sfgfkxq977d6Wl+jnK996PXR9ARfrE tLgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732016341; x=1732621141; h=content-transfer-encoding:mime-version:references:in-reply-to :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=Y2GPbvVbXZi+b5G0REQ51+I9QpE45tpWYM729Hz1cygXY/sgtbLmI93oBtEVauWWQ2 XvbNZRJkm83lktd2vSCedeVEaP4FS37FScruXaG8F8Qru2RPGciEGLl1yNPUW7YINqHp zN886eoVRBWiVMWgW5f5BQqlEoQ8tOMj7Ykps2eeqA3ysXC0hSC6RIrFVu+d1wRi6mey omaEOG3FlsVh75dt3cJwPmdSgbMXVoLnXjmKrto3TVzzVcC1Wut3IkEnMNEPh6Kx0bkG J2QL05xpevrcBvoM1/syU/deEuD43FBU5ob0c07AC8wtXEFN0ybNrf1AGQvyogddnrUh IA4Q== X-Gm-Message-State: AOJu0Yz2/krUD+6i4N/qNuC5pGRYMqKrqxz8zn7/Y78EXHhIHKzqMndo g10J9KIhLsGQ2o1kvmehTyku92mM2ldo6xkX3O0hovMA/KuMuau3sfIAy4PcUi8= X-Google-Smtp-Source: AGHT+IFr45o9PycleaXFLiTzqXvscquGYb+Mn9zBNm7BOq5qHffbKvS1FuCE87Z0Yz6aN3Qi7Bsc1w== X-Received: by 2002:a05:600c:4eca:b0:431:5632:448d with SMTP id 5b1f17b1804b1-432df795cafmr57581345e9.9.1732016341354; Tue, 19 Nov 2024 03:39:01 -0800 (PST) Received: from localhost ([193.86.92.181]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432dab7878esm192965345e9.14.2024.11.19.03.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 03:39:00 -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 v2 1/2] mm/rodata_test: use READ_ONCE() to read const variable Date: Tue, 19 Nov 2024 12:37:38 +0100 Message-ID: <2a66dee010151b25cb143efb39091ef7530aa00a.1732016064.git.ptesarik@suse.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Stat-Signature: 5phh38hb46g1zsia4j3e7y761ig6kq31 X-Rspamd-Queue-Id: 6846F4000A X-Rspam-User: X-HE-Tag: 1732016280-372909 X-HE-Meta: U2FsdGVkX19anrLo4/e5xK4gfwWaexlY8VmZv52XBp4hzxrZ6Jf4kkk5sDfq4XyAu74xpsu+ab1czC7w9eI5SuXEOC+fW4d+fjNxUvQHuUyVBM/vJDcVH0KOO5FzXjBsaJd3UxO0wPucmQe2nI19NecnLztp1Jg917KvMyr/CkmBJ+4n2wQeOgV6b51f+OF7+rG4Y0mEQs2HFtrXCFiRXcwFZad0N1cATYlWuidzxGPQTMT5yLOQB82OQdvwABlp768HCz4NsQ/XE5fVzvoFifjFGQJC7Vg/P8nHaSdHdTCNB1piSOy7yJZc0/qjG6vFQEKer2VxwYGxu1Z/4k6EeP/8XYFh0py/ZArfi9voN50LTZDhEZfdPyocWuXayG9JI7QjtMODJ+CmjxOrH14hqYU7L60029+aH9FN1+ehZLpwuhDO4rgdgdXe2aJru7bQXNV601V9Gt8GBiVMEuAvi1Obr/s74Ncz0uWiVZhRSYThue0cJn9DkAWM2yk8v+P8WA2kR768apk/819no8AgBTZUW+TFjmBjkb5FbJTYEPodR3YOwwO0KItWZrbPOX61u7OMvgAubDxZm1iQmHjE/c2UvbdOap31YlxOoJZpHWmtBt7UtpwkgujSQPFrgMnHExqho0M1b9LNgckkuKFZ1U92s+J0HeTnW/asIzK7QasTMM3UpDwKSJGZBnaRkIdGmLjHkQPn9IWiKMVYGXdSinjl3t7L6M6IeyqwvLxt0w605MM/kjyGVDpV9vCudoa0DMxv/1sDE2P9NiSvpSPjkoFQrp9thaHdHYK2K9DF5s3YUi38iXe5uXuK7PmEZdRhdNjL4yzzjcOUftgTge2m6+Od+F5/ZuiLbLLCnGaOvPdpQu/YFqbHO/g969zlzU0OJrBDcfxgixv2WsIWnkyrx4y9xtVGxFlAbMZGm9165tRIv0FFGokUBOJRPE61f+xsVYR3O5Dhby9xAkTizZb M8Em6Jlq Zfpa4P1SL0xMW6yYca+M3rsF7awAefPlEybjvnldAncR/9tONCi8v7TSdV6y20T1EtxQpvqlrFsmks/uTmqShJC+szjKgG2eQ936F872FfhMnTOIId+H50n2bxtf8pftjQkk7OubOZOBhZgwE/rMb08Sea5tOh8FAFYmKETpDfJVc91InGXaj2ww/Y+PZACUwJEIeYII7HJ8zvGFqTgliYFrzTJf3PI9ME+cmnzshOwnAGP3asV3f7BOadvx4GmFMpK8Ajf9583Cvr2ZH0KJmIPkN+SqsDzct6ylBUuvaxxLzJ2QJdvd7ZWbvBhx2HEMLgpGOQrfn6By4h0Olmw7PycaXTwOqT7EIuIHsl4alFLWSL5zpnfWaz6DFPPbPo8rRCdcs3oqsDWyHNEI+O1E1V/IhCCnpljsm6HGCCYBkHDZW0gDTdToEKkHTxc2N26osly1k 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; }