2021-03-31 08:05:14

by Yang Li

[permalink] [raw]
Subject: [PATCH] x86/kernel: remove unneeded dead-store initialization

make clang-analyzer on x86_64 defconfig caught my attention with:

arch/x86/kernel/cpu/cacheinfo.c:880:24: warning: Value stored to
'this_cpu_ci' during its initialization is never read
[clang-analyzer-deadcode.DeadStores]
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
^

So, simply remove this unneeded dead-store initialization to make
clang-analyzer happy.

As compilers will detect this unneeded assignment and optimize this anyway,
the resulting object code is identical before and after this change.

No functional change. No change to object code.

Reported-by: Abaci Robot <[email protected]>
Signed-off-by: Yang Li <[email protected]>
---
arch/x86/kernel/cpu/cacheinfo.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
index 3ca9be4..d66af29 100644
--- a/arch/x86/kernel/cpu/cacheinfo.c
+++ b/arch/x86/kernel/cpu/cacheinfo.c
@@ -877,7 +877,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)
static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
struct _cpuid4_info_regs *base)
{
- struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
+ struct cpu_cacheinfo *this_cpu_ci;
struct cacheinfo *this_leaf;
int i, sibling;

--
1.8.3.1


2021-03-31 17:50:58

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Mar 31, 2021 at 1:00 AM Yang Li <[email protected]> wrote:
>
> make clang-analyzer on x86_64 defconfig caught my attention with:
>
> arch/x86/kernel/cpu/cacheinfo.c:880:24: warning: Value stored to
> 'this_cpu_ci' during its initialization is never read
> [clang-analyzer-deadcode.DeadStores]
> struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
> ^
>
> So, simply remove this unneeded dead-store initialization to make
> clang-analyzer happy.
>
> As compilers will detect this unneeded assignment and optimize this anyway,
> the resulting object code is identical before and after this change.
>
> No functional change. No change to object code.

Reviewed-by: Nick Desaulniers <[email protected]>

Looks like this is from when this code was introduced in
commit 0d55ba46bfbe ("x86/cacheinfo: Move cacheinfo sysfs code to
generic infrastructure")
though this file was moved from arch/x86/kernel/cpu/intel_cacheinfo.c
to arch/x86/kernel/cpu/cacheinfo.c in
commit 1d200c078d0e ("x86/CPU: Rename intel_cacheinfo.c to cacheinfo.c")
(So I don't think a Fixes tag for 0d55ba46bfbe would be appropriate).

Thanks for the patch!

>
> Reported-by: Abaci Robot <[email protected]>
> Signed-off-by: Yang Li <[email protected]>
> ---
> arch/x86/kernel/cpu/cacheinfo.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
> index 3ca9be4..d66af29 100644
> --- a/arch/x86/kernel/cpu/cacheinfo.c
> +++ b/arch/x86/kernel/cpu/cacheinfo.c
> @@ -877,7 +877,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)
> static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
> struct _cpuid4_info_regs *base)
> {
> - struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
> + struct cpu_cacheinfo *this_cpu_ci;
> struct cacheinfo *this_leaf;
> int i, sibling;
>
> --
> 1.8.3.1
>


--
Thanks,
~Nick Desaulniers

2021-04-07 20:57:05

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Mar 31, 2021 at 04:00:24PM +0800, Yang Li wrote:
> make clang-analyzer on x86_64 defconfig caught my attention with:

I can't trigger this here using:

make CC=clang-11 -j16 clang-analyzer

I get all kinds of missing python scripts:

multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.9/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/usr/lib/python3.9/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
File "/mnt/kernel/kernel/linux/./scripts/clang-tools/run-clang-tools.py", line 54, in run_analysis
p = subprocess.run(["clang-tidy", "-p", args.path, checks, entry["file"]],
File "/usr/lib/python3.9/subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.9/subprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.9/subprocess.py", line 1819, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'clang-tidy'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/mnt/kernel/kernel/linux/./scripts/clang-tools/run-clang-tools.py", line 74, in <module>
main()
File "/mnt/kernel/kernel/linux/./scripts/clang-tools/run-clang-tools.py", line 70, in main
pool.map(run_analysis, datastore)
File "/usr/lib/python3.9/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.9/multiprocessing/pool.py", line 771, in get
raise self._value
FileNotFoundError: [Errno 2] No such file or directory: 'clang-tidy'
make: *** [Makefile:1914: clang-analyzer] Error 1

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-04-07 21:46:32

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Apr 7, 2021 at 5:02 AM Borislav Petkov <[email protected]> wrote:
>
> On Wed, Mar 31, 2021 at 04:00:24PM +0800, Yang Li wrote:
> > make clang-analyzer on x86_64 defconfig caught my attention with:
>
> I can't trigger this here using:
>
> make CC=clang-11 -j16 clang-analyzer
>
> I get all kinds of missing python scripts:

<snip>

> FileNotFoundError: [Errno 2] No such file or directory: 'clang-tidy'

You do have clang-tidy installed right? `which clang-tidy`?
--
Thanks,
~Nick Desaulniers

2021-04-07 21:55:38

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Apr 07, 2021 at 10:41:26AM -0700, Nick Desaulniers wrote:
> You do have clang-tidy installed right? `which clang-tidy`?

Yah, installed that and was able to repro:

arch/x86/kernel/cpu/cacheinfo.c:880:24: warning: Value stored to 'this_cpu_ci' during its initialization is never read [clang-analyzer-deadcode.DeadStores]
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
arch/x86/kernel/cpu/cacheinfo.c:880:24: note: Value stored to 'this_cpu_ci' during its initialization is never read

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2021-04-07 21:56:13

by tip-bot2 for Haifeng Xu

[permalink] [raw]
Subject: [tip: x86/cleanups] x86/cacheinfo: Remove unneeded dead-store initialization

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID: dda451f391eee5d68db3ca87fd8b2a42c8c2b507
Gitweb: https://git.kernel.org/tip/dda451f391eee5d68db3ca87fd8b2a42c8c2b507
Author: Yang Li <[email protected]>
AuthorDate: Wed, 31 Mar 2021 16:00:24 +08:00
Committer: Borislav Petkov <[email protected]>
CommitterDate: Wed, 07 Apr 2021 21:12:12 +02:00

x86/cacheinfo: Remove unneeded dead-store initialization

$ make CC=clang clang-analyzer

(needs clang-tidy installed on the system too)

on x86_64 defconfig triggers:

arch/x86/kernel/cpu/cacheinfo.c:880:24: warning: Value stored to 'this_cpu_ci' \
during its initialization is never read [clang-analyzer-deadcode.DeadStores]
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
^
arch/x86/kernel/cpu/cacheinfo.c:880:24: note: Value stored to 'this_cpu_ci' \
during its initialization is never read

So simply remove this unneeded dead-store initialization.

As compilers will detect this unneeded assignment and optimize this
anyway the resulting object code is identical before and after this
change.

No functional change. No change to object code.

[ bp: Massage commit message. ]

Reported-by: Abaci Robot <[email protected]>
Signed-off-by: Yang Li <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Reviewed-by: Nick Desaulniers <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
arch/x86/kernel/cpu/cacheinfo.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
index 3ca9be4..d66af29 100644
--- a/arch/x86/kernel/cpu/cacheinfo.c
+++ b/arch/x86/kernel/cpu/cacheinfo.c
@@ -877,7 +877,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)
static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
struct _cpuid4_info_regs *base)
{
- struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
+ struct cpu_cacheinfo *this_cpu_ci;
struct cacheinfo *this_leaf;
int i, sibling;

2021-04-07 22:06:36

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Apr 7, 2021 at 12:07 PM Borislav Petkov <[email protected]> wrote:
>
> On Wed, Apr 07, 2021 at 09:03:28PM +0200, Borislav Petkov wrote:
> > On Wed, Apr 07, 2021 at 10:41:26AM -0700, Nick Desaulniers wrote:
> > > You do have clang-tidy installed right? `which clang-tidy`?
>
> Btw, for user convenience, that "clang-analyzer" Makefile target could
> check for the presence of clang-tidy and fail if it is not there, with
> an informative error message. Methinks.

Yes, that's a good idea; we had a similar discussion recently about
what happens if you enable CONFIG_DEBUG_INFO_BTF and don't have pahole
installed. This is very much in the same vein. I've filed
https://github.com/ClangBuiltLinux/linux/issues/1342
to follow up on. Should be a good beginner bug for folks looking to
get started contributing.

--
Thanks,
~Nick Desaulniers

2021-04-07 22:43:23

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH] x86/kernel: remove unneeded dead-store initialization

On Wed, Apr 07, 2021 at 09:03:28PM +0200, Borislav Petkov wrote:
> On Wed, Apr 07, 2021 at 10:41:26AM -0700, Nick Desaulniers wrote:
> > You do have clang-tidy installed right? `which clang-tidy`?

Btw, for user convenience, that "clang-analyzer" Makefile target could
check for the presence of clang-tidy and fail if it is not there, with
an informative error message. Methinks.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette