mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: Kees Cook <kees@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	stable@vger.kernel.org,  kernel test robot <lkp@intel.com>,
	Nathan Chancellor <nathan@kernel.org>
Subject: [PATCH] lkdtm/bugs: Remove volatile from element_count
Date: Sat, 14 Nov 2026 21:35:33 -0700	[thread overview]
Message-ID: <20261114-lkdtm-bugs-wunsequenced-v1-1-b24f15ce1328@kernel.org> (raw)

After a recent improvement in clang to warn on unsequenced accesses to
volatile variables [1], an instance appears in lkdtm_FAM_BOUNDS(), which
can break the build with CONFIG_WERROR=y / W=e:

  drivers/misc/lkdtm/bugs.c:493:31: error: unsequenced accesses to volatile qualified 'element_count' [-Werror,-Wunsequenced]
    493 |         inst->array[element_count] = element_count;
        |                     ~~~~~~~~~~~~~    ^

Per C11 5.1.2.3.2, accessing a volatile object is a side effect. Per C11
6.5.2, unsequenced side effects on the same scalar object are undefined
behavior. Per C11 6.5.16.3, the value computations of the left and
right operands of the assignment operator are unsequenced.

This variable is marked volatile to hide its value from the compiler at
compile time. The same thing can be accomplished with
OPTIMIZER_HIDE_VAR. Drop volatile from element_count to clear up the
warning. The tests still pass and there are no warnings at compile time.

Cc: stable@vger.kernel.org
Fixes: 5d207e83ca41 ("lkdtm: Add FAM_BOUNDS test for __counted_by")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202611140405.wMowxoPE-lkp@intel.com/
Closes: https://github.com/ClangBuiltLinux/linux/issues/2194
Link: https://github.com/llvm/llvm-project/commit/955715460eaf9188c241c74750637fc82dfcc423 [1]
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 drivers/misc/lkdtm/bugs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/lkdtm/bugs.c b/drivers/misc/lkdtm/bugs.c
index 2761685cf4f8..7c799fef89a8 100644
--- a/drivers/misc/lkdtm/bugs.c
+++ b/drivers/misc/lkdtm/bugs.c
@@ -472,12 +472,14 @@ struct lkdtm_cb_fam {
 	int array[] __counted_by(count);
 };
 
-static volatile int element_count = 4;
+static int element_count = 4;
 
 static void lkdtm_FAM_BOUNDS(void)
 {
 	struct lkdtm_cb_fam *inst;
 
+	OPTIMIZER_HIDE_VAR(element_count);
+
 	inst = kzalloc_flex(*inst, array, element_count + 1);
 	if (!inst) {
 		pr_err("FAIL: could not allocate test struct!\n");
@@ -534,6 +536,8 @@ static void lkdtm_PTR_BOUNDS(void)
 {
 	struct lkdtm_cb_ptr *inst;
 
+	OPTIMIZER_HIDE_VAR(element_count);
+
 	inst = kzalloc_obj(*inst);
 	if (!inst) {
 		pr_err("FAIL: could not allocate struct lkdtm_cb_ptr!\n");

---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
change-id: 20261114-lkdtm-bugs-wunsequenced-7c18499c2771

Best regards,
--  
Cheers,
Nathan


                 reply	other threads:[~2026-11-15  4:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261114-lkdtm-bugs-wunsequenced-v1-1-b24f15ce1328@kernel.org \
    --to=nathan@kernel.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=llvm@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®