Skip to contentSkip to conversation

x86: coco: mark cc_mask as __maybe_unused

PATCH In review Arnd Bergmann 7 participants 11 messages Mar 4, 2025 – Mar 10, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: coco: mark cc_mask as __maybe_unused

From: Arnd Bergmann <[email protected]>

When extra warnings are enabled, the cc_mask definition in asm/coco.h causes a build failure with gcc:

arch/x86/include/asm/coco.h:28:18: error: 'cc_mask' defined but not used [-Werror=unused-const-variable=]
28 | static const u64 cc_mask = 0;

Mark this one as __maybe_unused.

Fixes a0a8d15a798b ("x86/tdx: Preserve shared bit on mprotect()")
---
 arch/x86/include/asm/coco.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/coco.h +1 -1
index aa6c8f8ca958..9e9204cfca6f 100644
@@ -25,7 +25,7 @@ u64 cc_mkdec(u64 val);
 void cc_random_init(void);
 #else
 #define cc_vendor (CC_VENDOR_NONE)
-static const u64 cc_mask = 0;
+static const __maybe_unused u64 cc_mask = 0;
 
 static inline u64 cc_mkenc(u64 val)
 {
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When extra warnings are enabled, the cc_mask definition in asm/coco.h causes a build failure with gcc:

Quoted 8 lines

arch/x86/include/asm/coco.h:28:18: error: 'cc_mask' defined but not used [-Werror=unused-const-variable=]

    28 | static const u64 cc_mask = 0;

Mark this one as __maybe_unused.

Fixes a0a8d15a798b ("x86/tdx: Preserve shared bit on mprotect()")

---

Looks good to me.

Quoted 16 lines
  arch/x86/include/asm/coco.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/coco.h +1 -1
index aa6c8f8ca958..9e9204cfca6f 100644
@@ -25,7 +25,7 @@ u64 cc_mkdec(u64 val);
  void cc_random_init(void);
  #else
  #define cc_vendor (CC_VENDOR_NONE)
-static const u64 cc_mask = 0;
+static const __maybe_unused u64 cc_mask = 0;
 
  static inline u64 cc_mkenc(u64 val)
  {
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When extra warnings are enabled, the cc_mask definition in asm/coco.h causes a build failure with gcc:

Quoted 7 lines

arch/x86/include/asm/coco.h:28:18: error: 'cc_mask' defined but not used [-Werror=unused-const-variable=]
28 | static const u64 cc_mask = 0;

Mark this one as __maybe_unused.

Fixes a0a8d15a798b ("x86/tdx: Preserve shared bit on mprotect()")
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#3 lore raw
In reply to Arnd Bergmann
Quoted 21 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When extra warnings are enabled, the cc_mask definition in asm/coco.h causes a build failure with gcc:

arch/x86/include/asm/coco.h:28:18: error: 'cc_mask' defined but not used [-Werror=unused-const-variable=]
28 | static const u64 cc_mask = 0;

Mark this one as __maybe_unused.

Fixes a0a8d15a798b ("x86/tdx: Preserve shared bit on mprotect()")
---
 arch/x86/include/asm/coco.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/coco.h +0 -0
index aa6c8f8ca958..9e9204cfca6f 100644

arch/x86/include/asm/coco.h u64 cc_mkdec(u64 val); · line 28

Quoted 5 lines
arch/x86/include/asm/coco.h +1 -1
@@ -25,7 +25,7 @@ u64 cc_mkdec(u64 val);
 void cc_random_init(void);
 #else
 #define cc_vendor (CC_VENDOR_NONE)
-static const u64 cc_mask = 0;
+static const __maybe_unused u64 cc_mask = 0;

So I detest __maybe_unused with a vengeance: the 'maybe' unnecessarily inserts uncertainty & probability language into the text, while there's nothing uncertain about this interface or the code. Why cannot the compiler figure it out?

Anyway, I'd suggest we change direct usage of cc_mask to a get_cc_mask() inline function instead, this will resolve the warning, plus it avoids some messy looking variable shadowing in tdx.c AFAICS:

arch/x86/coco/tdx/tdx.c:static void tdx_setup(u64 *cc_mask)

Thanks,

Ingo

maintainer
#4 lore raw
Quoted 35 lines · Ingo Molnar
Quoted 21 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When extra warnings are enabled, the cc_mask definition in asm/coco.h causes a build failure with gcc:

arch/x86/include/asm/coco.h:28:18: error: 'cc_mask' defined but not used [-Werror=unused-const-variable=]
28 | static const u64 cc_mask = 0;

Mark this one as __maybe_unused.

Fixes a0a8d15a798b ("x86/tdx: Preserve shared bit on mprotect()")
---
 arch/x86/include/asm/coco.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/coco.h +0 -0
index aa6c8f8ca958..9e9204cfca6f 100644

arch/x86/include/asm/coco.h u64 cc_mkdec(u64 val); · line 28

Quoted 5 lines
arch/x86/include/asm/coco.h +1 -1
@@ -25,7 +25,7 @@ u64 cc_mkdec(u64 val);
 void cc_random_init(void);
 #else
 #define cc_vendor (CC_VENDOR_NONE)
-static const u64 cc_mask = 0;
+static const __maybe_unused u64 cc_mask = 0;

So I detest __maybe_unused with a vengeance: the 'maybe' unnecessarily inserts uncertainty & probability language into the text, while there's nothing uncertain about this interface or the code. Why cannot the compiler figure it out?

Quoted 5 lines

Anyway, I'd suggest we change direct usage of cc_mask to a get_cc_mask() inline function instead, this will resolve the warning, plus it avoids some messy looking variable shadowing in tdx.c AFAICS:

arch/x86/coco/tdx/tdx.c:static void tdx_setup(u64 *cc_mask)

Touch ~17 spots:

$ git grep -w cc_mask arch/x86 | wc -l
17

just because of some stupid gcc extra warning switch?

I say disable the gcc warning. Pfft.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#5 lore raw
Quoted 1 line · Borislav Petkov

just because of some stupid gcc extra warning switch?

This warning has been kicked out into W1 once already for too many false positives:

c9c6837d3931 ("kbuild: move -Wunused-const-variable to W=1 warning level")

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#6 lore raw
Quoted 8 lines · Borislav Petkov
Quoted 7 lines · Borislav Petkov

Touch ~17 spots:

$ git grep -w cc_mask arch/x86 | wc -l
17

just because of some stupid gcc extra warning switch?

It's only one outside of CONFIG_ARCH_HAS_CC_PLATFORM, the other ones always see the 'extern' declaration.

Quoted 4 lines

This warning has been kicked out into W1 once already for too many false positives:

c9c6837d3931 ("kbuild: move -Wunused-const-variable to W=1 warning level")

Yes, that was me. We have now come to the point where only about a dozen instances are left and I resubmitted the remaining patches for most of them.

There is a twist here: clang by default warns about unused const variables in .c files but not in headers, while gcc doesn't warn about them at all unless it's explictly enabled, and then it warns about both of them. Newer gcc versions have a distinct -Wunused-const-variable=1 for the clang behavior and -Wunused-const-variable=2 that warns for both, so we could reasonably decide to enable the =1 version by default and leave the =2 version for W=2.

On the other hand, most of the users of 'static const' variables in headers are rather dumb and should just be moved into the file that uses them, or they can be replaced with a #define or an enum.

In this case, the only user is a macro:
#define _PAGE_CC (_AT(pteval_t, cc_mask))

so maybe '#define cc_mask 0' would be appropriate.

Arnd

maintainer
#7 lore raw
Quoted 2 lines · Arnd Bergmann

There is a twist here: clang by default warns about unused const variables in .c files but not in headers, while gcc doesn't

What is the point of this warning, do you know?

Someone defines a const, forgets to use it and? Oh big deal. This should be a -Wunused anyway, no?

I must be missing something here...

Quoted 12 lines

warn about them at all unless it's explictly enabled, and then it warns about both of them. Newer gcc versions have a distinct

(quoted diff) +0 -2
-Wunused-const-variable=1 for the clang behavior and
-Wunused-const-variable=2 that warns for both, so we could

reasonably decide to enable the =1 version by default and leave the =2 version for W=2.

On the other hand, most of the users of 'static const' variables in headers are rather dumb and should just be moved into the file that uses them, or they can be replaced with a #define or an enum.

Quoted 4 lines

In this case, the only user is a macro:
#define _PAGE_CC (_AT(pteval_t, cc_mask))

so maybe '#define cc_mask 0' would be appropriate.

Sounds a lot better to me.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw
Quoted 14 lines
Quoted 14 lines

warn about them at all unless it's explictly enabled, and then it warns about both of them. Newer gcc versions have a distinct

(quoted diff) +0 -2
-Wunused-const-variable=1 for the clang behavior and
-Wunused-const-variable=2 that warns for both, so we could

reasonably decide to enable the =1 version by default and leave the =2 version for W=2.

On the other hand, most of the users of 'static const' variables in headers are rather dumb and should just be moved into the file that uses them, or they can be replaced with a #define or an enum.

Quoted 7 lines
Quoted 4 lines

In this case, the only user is a macro:
#define _PAGE_CC (_AT(pteval_t, cc_mask))

so maybe '#define cc_mask 0' would be appropriate.

Sounds a lot better to me.

I actually tried this with CONFIG_ARCH_HAS_CC_PLATFORM off yesterday but got below error:

$ make -j$(nproc)
mkdir -p /work/enabling/src/linux/tools/objtool && make
O=/work/enabling/src/linux subdir=tools/objtool --no-print-directory -C objtool 
  CALL    scripts/checksyscalls.sh
  INSTALL libsubcmd_headers
  CC      drivers/char/tpm/tpm2-cmd.o
  CC      drivers/char/tpm/tpmrm-dev.o
  CC      drivers/char/tpm/tpm2-space.o
  CC      drivers/char/tpm/tpm-sysfs.o
In file included from ./arch/x86/include/asm/pgtable.h:23,
                 from ./arch/x86/include/asm/tlbflush.h:16,
                 from ./arch/x86/include/asm/uaccess.h:17,
                 from ./include/linux/uaccess.h:12,
                 from ./include/linux/sched/task.h:13,
                 from ./include/linux/sched/signal.h:9,
                 from ./include/linux/rcuwait.h:6,
                 from ./include/linux/percpu-rwsem.h:7,
                 from ./include/linux/fs.h:33,
                 from ./include/linux/tpm.h:23,
                 from drivers/char/tpm/tpm.h:27,
                 from drivers/char/tpm/tpm2-cmd.c:14:
drivers/char/tpm/tpm2-cmd.c: In function ‘tpm2_find_cc’:
./arch/x86/include/asm/coco.h:28:17: error: expected identifier or ‘(’ before
numeric constant
   28 | #define cc_mask 0
      |                 ^
drivers/char/tpm/tpm2-cmd.c:813:13: note: in expansion of macro ‘cc_mask’
  813 |         u32 cc_mask;
      |             ^~~~~~~
drivers/char/tpm/tpm2-cmd.c:816:17: error: lvalue required as left operand of
assignment
  816 |         cc_mask = 1 << TPM2_CC_ATTR_VENDOR | GENMASK(15, 0);
      |                 ^

Rename the local variable 'cc_mask' in drivers/char/tpm/tpm2-cmd.c fix the build, but I think the real issue is the 'cc_mask' in asm/coco.h is too common to be a global visible variable/macro.

#9 lore raw
In reply to Borislav Petkov
Quoted 10 lines · Borislav Petkov
Quoted 2 lines · Arnd Bergmann

There is a twist here: clang by default warns about unused const variables in .c files but not in headers, while gcc doesn't

What is the point of this warning, do you know?

Someone defines a const, forgets to use it and? Oh big deal. This should be a -Wunused anyway, no?

I must be missing something here...

We turned on -Wunused a while ago for default builds after all the -Wunused-variable warnings got addressed, but instead turned off -Wunused-const-variable and -Wunused-but-set-variable unless W=1 is set while there are still existing warnings.

In my opinion, there is little difference between unused const and non-const variables, the reason that gcc treats them differently seems to be from common c++ coding style advocating for them to be used in place of macros. This is the case here, but most of the warnings it actually shows are for mistakes where some variable is in the wrong #ifdef block or the only user got removed.

Quoted 6 lines
Quoted 4 lines

In this case, the only user is a macro:
#define _PAGE_CC (_AT(pteval_t, cc_mask))

so maybe '#define cc_mask 0' would be appropriate.

Sounds a lot better to me.

Too bad that did not work. This version is also a bit ugly:

arch/x86/include/asm/pgtable_types.h +4 -0
index c90e9c51edb7..f31c1a31742d 100644
@@ -179,7 +179,11 @@ enum page_cache_mode {
 };
 #endif
 
+#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
 #define _PAGE_CC               (_AT(pteval_t, cc_mask))
+#else
+#define _PAGE_CC               (_AT(pteval_t, 0))
+#endif
 #define _PAGE_ENC              (_AT(pteval_t, sme_me_mask))
 
 #define _PAGE_CACHE_MASK       (_PAGE_PWT | _PAGE_PCD | _PAGE_PAT)

so I'll just follow Ingo's earlier suggestion for the v2 patch.

Arnd

#10 lore raw
In reply to Ingo Molnar
Quoted 13 lines · Ingo Molnar

arch/x86/include/asm/coco.h u64 cc_mkdec(u64 val); · line 28

Quoted 6 lines · Arnd Bergmann
arch/x86/include/asm/coco.h +1 -1
@@ -25,7 +25,7 @@ u64 cc_mkdec(u64 val);
 void cc_random_init(void);
 #else
 #define cc_vendor (CC_VENDOR_NONE)
-static const u64 cc_mask = 0;
+static const __maybe_unused u64 cc_mask = 0;

So I detest __maybe_unused with a vengeance: the 'maybe' unnecessarily inserts uncertainty & probability language into the text, while there's nothing uncertain about this interface or the code. Why cannot the compiler figure it out?

Right, I'm also trying to remove a lot of the __maybe_unused annotations in places like power management and device_id tables where we now have better solutions in place.

Quoted 5 lines

Anyway, I'd suggest we change direct usage of cc_mask to a get_cc_mask() inline function instead, this will resolve the warning, plus it avoids some messy looking variable shadowing in tdx.c AFAICS:

arch/x86/coco/tdx/tdx.c:static void tdx_setup(u64 *cc_mask)

I've sent a v2 now.

Arnd