|
| 1 | +From d0afcfeb9e3810ec89d1ffde1a0e36621bb75dca Mon Sep 17 00:00:00 2001 |
| 2 | +From: Nathan Chancellor < [email protected]> |
| 3 | +Date: Tue, 6 May 2025 14:02:01 -0700 |
| 4 | +Subject: kbuild: Disable -Wdefault-const-init-unsafe |
| 5 | + |
| 6 | +A new on by default warning in clang [1] aims to flags instances where |
| 7 | +const variables without static or thread local storage or const members |
| 8 | +in aggregate types are not initialized because it can lead to an |
| 9 | +indeterminate value. This is quite noisy for the kernel due to |
| 10 | +instances originating from header files such as: |
| 11 | + |
| 12 | + drivers/gpu/drm/i915/gt/intel_ring.h:62:2: error: default initialization of an object of type 'typeof (ring->size)' (aka 'const unsigned int') leaves the object uninitialized [-Werror,-Wdefault-const-init-var-unsafe] |
| 13 | + 62 | typecheck(typeof(ring->size), next); |
| 14 | + | ^ |
| 15 | + include/linux/typecheck.h:10:9: note: expanded from macro 'typecheck' |
| 16 | + 10 | ({ type __dummy; \ |
| 17 | + | ^ |
| 18 | + |
| 19 | + include/net/ip.h:478:14: error: default initialization of an object of type 'typeof (rt->dst.expires)' (aka 'const unsigned long') leaves the object uninitialized [-Werror,-Wdefault-const-init-var-unsafe] |
| 20 | + 478 | if (mtu && time_before(jiffies, rt->dst.expires)) |
| 21 | + | ^ |
| 22 | + include/linux/jiffies.h:138:26: note: expanded from macro 'time_before' |
| 23 | + 138 | #define time_before(a,b) time_after(b,a) |
| 24 | + | ^ |
| 25 | + include/linux/jiffies.h:128:3: note: expanded from macro 'time_after' |
| 26 | + 128 | (typecheck(unsigned long, a) && \ |
| 27 | + | ^ |
| 28 | + include/linux/typecheck.h:11:12: note: expanded from macro 'typecheck' |
| 29 | + 11 | typeof(x) __dummy2; \ |
| 30 | + | ^ |
| 31 | + |
| 32 | + include/linux/list.h:409:27: warning: default initialization of an object of type 'union (unnamed union at include/linux/list.h:409:27)' with const member leaves the object uninitialized [-Wdefault-const-init-field-unsafe] |
| 33 | + 409 | struct list_head *next = smp_load_acquire(&head->next); |
| 34 | + | ^ |
| 35 | + include/asm-generic/barrier.h:176:29: note: expanded from macro 'smp_load_acquire' |
| 36 | + 176 | #define smp_load_acquire(p) __smp_load_acquire(p) |
| 37 | + | ^ |
| 38 | + arch/arm64/include/asm/barrier.h:164:59: note: expanded from macro '__smp_load_acquire' |
| 39 | + 164 | union { __unqual_scalar_typeof(*p) __val; char __c[1]; } __u; \ |
| 40 | + | ^ |
| 41 | + include/linux/list.h:409:27: note: member '__val' declared 'const' here |
| 42 | + |
| 43 | + crypto/scatterwalk.c:66:22: error: default initialization of an object of type 'struct scatter_walk' with const member leaves the object uninitialized [-Werror,-Wdefault-const-init-field-unsafe] |
| 44 | + 66 | struct scatter_walk walk; |
| 45 | + | ^ |
| 46 | + include/crypto/algapi.h:112:15: note: member 'addr' declared 'const' here |
| 47 | + 112 | void *const addr; |
| 48 | + | ^ |
| 49 | + |
| 50 | + fs/hugetlbfs/inode.c:733:24: error: default initialization of an object of type 'struct vm_area_struct' with const member leaves the object uninitialized [-Werror,-Wdefault-const-init-field-unsafe] |
| 51 | + 733 | struct vm_area_struct pseudo_vma; |
| 52 | + | ^ |
| 53 | + include/linux/mm_types.h:803:20: note: member 'vm_flags' declared 'const' here |
| 54 | + 803 | const vm_flags_t vm_flags; |
| 55 | + | ^ |
| 56 | + |
| 57 | +Silencing the instances from typecheck.h is difficult because '= {}' is |
| 58 | +not available in older but supported compilers and '= {0}' would cause |
| 59 | +warnings about a literal 0 being treated as NULL. While it might be |
| 60 | +possible to come up with a local hack to silence the warning for |
| 61 | +clang-21+, it may not be worth it since -Wuninitialized will still |
| 62 | +trigger if an uninitialized const variable is actually used. |
| 63 | + |
| 64 | +In all audited cases of the "field" variant of the warning, the members |
| 65 | +are either not used in the particular call path, modified through other |
| 66 | +means such as memset() / memcpy() because the containing object is not |
| 67 | +const, or are within a union with other non-const members. |
| 68 | + |
| 69 | +Since this warning does not appear to have a high signal to noise ratio, |
| 70 | +just disable it. |
| 71 | + |
| 72 | + |
| 73 | +Link: https://github.com/llvm/llvm-project/commit/576161cb6069e2c7656a8ef530727a0f4aefff30 [1] |
| 74 | +Reported-by: Linux Kernel Functional Testing < [email protected]> |
| 75 | +Closes: https://lore.kernel.org/CA+G9fYuNjKcxFKS_MKPRuga32XbndkLGcY-PVuoSwzv6VWbY=w@mail.gmail.com/ |
| 76 | +Reported-by: Marcus Seyfarth < [email protected]> |
| 77 | +Closes: https://github.com/ClangBuiltLinux/linux/issues/2088 |
| 78 | +Signed-off-by: Nathan Chancellor < [email protected]> |
| 79 | +Signed-off-by: Masahiro Yamada < [email protected]> |
| 80 | +--- |
| 81 | +Link: https://git.kernel.org/linus/d0afcfeb9e3810ec89d1ffde1a0e36621bb75dca |
| 82 | +--- |
| 83 | + Makefile | 11 +++++++++++ |
| 84 | + 1 file changed, 11 insertions(+) |
| 85 | + |
| 86 | +diff --git a/Makefile b/Makefile |
| 87 | +index 09de195b86f2..f63509602c2d 100644 |
| 88 | +--- a/Makefile |
| 89 | ++++ b/Makefile |
| 90 | +@@ -814,6 +814,17 @@ KBUILD_CFLAGS += -Wno-gnu |
| 91 | + # source of a reference will be _MergedGlobals and not on of the whitelisted names. |
| 92 | + # See modpost pattern 2 |
| 93 | + KBUILD_CFLAGS += -mno-global-merge |
| 94 | ++# Clang may emit a warning when a const variable, such as the dummy variables |
| 95 | ++# in typecheck(), or const member of an aggregate type are not initialized, |
| 96 | ++# which can result in unexpected behavior. However, in many audited cases of |
| 97 | ++# the "field" variant of the warning, this is intentional because the field is |
| 98 | ++# never used within a particular call path, the field is within a union with |
| 99 | ++# other non-const members, or the containing object is not const so the field |
| 100 | ++# can be modified via memcpy() / memset(). While the variable warning also gets |
| 101 | ++# disabled with this same switch, there should not be too much coverage lost |
| 102 | ++# because -Wuninitialized will still flag when an uninitialized const variable |
| 103 | ++# is used. |
| 104 | ++KBUILD_CFLAGS += $(call cc-disable-warning, default-const-init-unsafe) |
| 105 | + else |
| 106 | + |
| 107 | + # Warn about unmarked fall-throughs in switch statement. |
| 108 | +-- |
| 109 | +cgit 1.2.3-korg |
| 110 | + |
0 commit comments