Skip to contentSkip to conversation

sched/core: silence a warning in sched_init()

PATCH Superseded Qian Cai 2 participants 2 messages Jun 24, 2019 Scheduler

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH 1/1 sched/core: silence a warning in sched_init()

Compiling a kernel with both FAIR_GROUP_SCHED=n and RT_GROUP_SCHED=n will generate a warning using W=1,

kernel/sched/core.c: In function 'sched_init':
kernel/sched/core.c:5906:32: warning: variable 'ptr' set but not used [-Wunused-but-set-variable]

  unsigned long alloc_size = 0, ptr;
                                ^~~

It apparently the maintainers don't like the proper fix [1] which contains ugly idefs, so silence it by appending the __maybe_unused attribute for it instead.

[1] https://lore.kernel.org/lkml/[email protected]/

---
 kernel/sched/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/core.c +4 -1
index 874c427742a9..9bbad91b3f01 100644
@@ -5903,7 +5903,10 @@ int in_sched_functions(unsigned long addr)
 void __init sched_init(void)
 {
 	int i, j;
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;
+
+	/* To silence a -Wunused-but-set-variable warning. */
+	unsigned long ptr __maybe_unused;
 
 	wait_bit_init();
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Qian Cai

Compiling a kernel with both FAIR_GROUP_SCHED=n and RT_GROUP_SCHED=n will generate a warning using W=1,

kernel/sched/core.c: In function 'sched_init':
kernel/sched/core.c:5906:32: warning: variable 'ptr' set but not used [-Wunused-but-set-variable]

Quoted 7 lines
  unsigned long alloc_size = 0, ptr;
                                ^~~

It apparently the maintainers don't like the proper fix [1] which contains ugly idefs, so silence it by appending the __maybe_unused attribute for it instead.

As you say, ifdefs are ugly. I try to stay away from them as much as possible (as you may have noticed with the NOHZ stuff). They just make code harder to read (I know someone who'd say "it's free code obfuscation"), and that is especially true in the patch you reference where it conditions variable declaration.

So it may not have been the "proper" fix after all...

Quoted 12 lines

[1] https://lore.kernel.org/lkml/[email protected]/

---
 kernel/sched/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 874c427742a9..9bbad91b3f01 100644

kernel/sched/core.c int in_sched_functions(unsigned long addr) · line 5910

Quoted 9 lines
kernel/sched/core.c +4 -1
@@ -5903,7 +5903,10 @@ int in_sched_functions(unsigned long addr)
 void __init sched_init(void)
 {
 	int i, j;
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;
+
+	/* To silence a -Wunused-but-set-variable warning. */
+	unsigned long ptr __maybe_unused;
 

To be super nitpicky, I'd argue the __maybe_unused attribute should be placed before the variable name - the kernel/sched/* code goes with prefixing rather than postfixing (there is *one* exception in deadline.c), and I personally prefer prefixing as well (it just reads better).

To be (still, but slightly less) nitpicky, I'd remove the added newline (keep the variable declaration as a single block) and even remove the comment - it's redundant with the attribute.

With those changes:

Quoted 3 lines
kernel/sched/core.c +0 -0
 	wait_bit_init();