Skip to contentSkip to conversation

sched/core: silence a warning in sched_init()

PATCH Superseded Qian Cai 2 participants 8 messages Jun 25, 2019 – Jul 18, 2019 Scheduler

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH v2 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 previous 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]/

---

v2: Incorporate the feedback from Valentin.

 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +2 -1
index 874c427742a9..12b9b69c8a66 100644
@@ -5903,7 +5903,8 @@ 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;
+	unsigned long __maybe_unused ptr;
 
 	wait_bit_init();
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 27 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]

  unsigned long alloc_size = 0, ptr;
                                ^~~

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

---

v2: Incorporate the feedback from Valentin.

 kernel/sched/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 874c427742a9..12b9b69c8a66 100644

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

Quoted 7 lines
kernel/sched/core.c +2 -1
@@ -5903,7 +5903,8 @@ 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;
+	unsigned long __maybe_unused ptr;
 

That still isn't particularly pretty.

Why do we care about W=1 build noise? Some of that seems rather silly, like that -Wmissing-prototype nonsense.

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

kernel/sched/core.c +2 -2
index fa43ce3962e7..cb652e165570 100644
@@ -6369,7 +6369,7 @@ DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);
 
 void __init sched_init(void)
 {
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;
 	int i;
 
 	wait_bit_init();
@@ -6381,7 +6381,7 @@ void __init sched_init(void)
 	alloc_size += 2 * nr_cpu_ids * sizeof(void **);
 #endif
 	if (alloc_size) {
-		ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
+		unsigned long ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
 		root_task_group.se = (struct sched_entity **)ptr;
author
#2 lore raw
Quoted 38 lines · Peter Zijlstra
Quoted 33 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]
  unsigned long alloc_size = 0, ptr;
                                ^~~

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

---

v2: Incorporate the feedback from Valentin.

 kernel/sched/core.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +2 -1
index 874c427742a9..12b9b69c8a66 100644
@@ -5903,7 +5903,8 @@ 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;
+	unsigned long __maybe_unused ptr;
Quoted 1 line

That still isn't particularly pretty.

Quoted 2 lines

Why do we care about W=1 build noise? Some of that seems rather silly, like that -Wmissing-prototype nonsense.

Yes, -Wmissing-prototype makes no sense, but "-Wunused-but-set-variable" is pretty valid to catch certain developer errors. For example,

https://lists.linuxfoundation.org/pipermail/iommu/2019-May/035680.html

Quoted 3 lines

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

Pretty sure that won't work, as the compiler will complain something like,

ISO C90 forbids mixed declarations and code

Quoted 24 lines
kernel/sched/core.c +0 -0
index fa43ce3962e7..cb652e165570 100644
@@ -6369,7 +6369,7 @@ DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);


 void __init sched_init(void)
 {

kernel/sched/core.c +1 -1
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;

  int i;

  wait_bit_init();

kernel/sched/core.c +1 -1
@@ -6381,7 +6381,7 @@ void __init sched_init(void)
  	alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  #endif
  	if (alloc_size) {
-		ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
+		unsigned long ptr = (unsigned long)kzalloc(alloc_size,
 GFP_NOWAIT);


 #ifdef CONFIG_FAIR_GROUP_SCHED
  root_task_group.se = (struct sched_entity **)ptr;

maintainer
#3 lore raw
Quoted 8 lines · Qian Cai

On Tue, 2019-06-25 at 15:52 +0200, Peter Zijlstra wrote:

Yes, -Wmissing-prototype makes no sense, but "-Wunused-but-set-variable" is pretty valid to catch certain developer errors. For example,

https://lists.linuxfoundation.org/pipermail/iommu/2019-May/035680.html

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

Pretty sure that won't work, as the compiler will complain something like,

ISO C90 forbids mixed declarations and code

No, it builds just fine, it's a new block and C allows new variables at every block start -- with the scope of that block.

And for our config, alloc_size is an unconditional 0, so it should DCE the whole block and with that our variable. But clearly the passes are the other way around :/

Quoted 23 lines
Quoted 23 lines
kernel/sched/core.c +0 -0
index fa43ce3962e7..cb652e165570 100644
@@ -6369,7 +6369,7 @@ DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);


 void __init sched_init(void)
 {

kernel/sched/core.c +1 -1
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;

  int i;

  wait_bit_init();

kernel/sched/core.c +1 -1
@@ -6381,7 +6381,7 @@ void __init sched_init(void)
  	alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  #endif
  	if (alloc_size) {
-		ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
+		unsigned long ptr = (unsigned long)kzalloc(alloc_size,
 GFP_NOWAIT);


 #ifdef CONFIG_FAIR_GROUP_SCHED
  root_task_group.se = (struct sched_entity **)ptr;

author
#4 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Qian Cai

On Tue, 2019-06-25 at 15:52 +0200, Peter Zijlstra wrote:
Yes, -Wmissing-prototype makes no sense, but "-Wunused-but-set-variable" is pretty valid to catch certain developer errors. For example,

https://lists.linuxfoundation.org/pipermail/iommu/2019-May/035680.html

Quoted 3 lines

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

Quoted 6 lines
Quoted 3 lines

Pretty sure that won't work, as the compiler will complain something like,

ISO C90 forbids mixed declarations and code

No, it builds just fine, it's a new block and C allows new variables at every block start -- with the scope of that block.

I remember I tried that before but recalled the error code wrong. Here it is,

kernel/sched/core.c:5940:17: warning: unused variable 'ptr' [-Wunused-variable] unsigned long ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);

Quoted 28 lines

And for our config, alloc_size is an unconditional 0, so it should DCE the whole block and with that our variable. But clearly the passes are the other way around :/

Quoted 23 lines
Quoted 23 lines
kernel/sched/core.c +0 -0
index fa43ce3962e7..cb652e165570 100644
@@ -6369,7 +6369,7 @@ DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);


 void __init sched_init(void)
 {

kernel/sched/core.c +1 -1
-	unsigned long alloc_size = 0, ptr;
+	unsigned long alloc_size = 0;

  int i;

  wait_bit_init();

kernel/sched/core.c +1 -1
@@ -6381,7 +6381,7 @@ void __init sched_init(void)
  	alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  #endif
  	if (alloc_size) {
-		ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
+		unsigned long ptr = (unsigned long)kzalloc(alloc_size,
 GFP_NOWAIT);


 #ifdef CONFIG_FAIR_GROUP_SCHED
  root_task_group.se = (struct sched_entity **)ptr;

maintainer
#5 lore raw
Quoted 16 lines · Qian Cai
Quoted 15 lines · Peter Zijlstra
Quoted 13 lines · Qian Cai

On Tue, 2019-06-25 at 15:52 +0200, Peter Zijlstra wrote:
Yes, -Wmissing-prototype makes no sense, but "-Wunused-but-set-variable" is pretty valid to catch certain developer errors. For example,

https://lists.linuxfoundation.org/pipermail/iommu/2019-May/035680.html

Quoted 3 lines

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

Pretty sure that won't work, as the compiler will complain something like,

ISO C90 forbids mixed declarations and code

Quoted 8 lines
Quoted 2 lines

No, it builds just fine, it's a new block and C allows new variables at every block start -- with the scope of that block.

I remember I tried that before but recalled the error code wrong. Here it is,

kernel/sched/core.c:5940:17: warning: unused variable 'ptr' [-Wunused-variable] unsigned long ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);

Yes, I know, I tried. And GCC is a moron because of it.

author
#6 lore raw
Quoted 24 lines · Peter Zijlstra
Quoted 19 lines · Qian Cai
Quoted 17 lines · Peter Zijlstra
Quoted 7 lines · Qian Cai

On Tue, 2019-06-25 at 15:52 +0200, Peter Zijlstra wrote:
Yes, -Wmissing-prototype makes no sense, but "-Wunused-but-set-variable" is pretty valid to catch certain developer errors. For example,

https://lists.linuxfoundation.org/pipermail/iommu/2019-May/035680.html

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

As to this one, ideally the compiler would not be stupid, and understand the below, but alas.

Pretty sure that won't work, as the compiler will complain something like,

ISO C90 forbids mixed declarations and code

No, it builds just fine, it's a new block and C allows new variables at every block start -- with the scope of that block.

Quoted 7 lines
Quoted 5 lines

I remember I tried that before but recalled the error code wrong. Here it is,

kernel/sched/core.c:5940:17: warning: unused variable 'ptr' [-Wunused-variable]
unsigned long ptr = (unsigned long)kzalloc(alloc_size,
GFP_NOWAIT);

Yes, I know, I tried. And GCC is a moron because of it.

Actually, not only GCC but clang also don’t understand your patch.

# make CC=clang W=1 kernel/sched/core.o

kernel/sched/core.c:6384:17: warning: unused variable 'ptr' [-Wunused-variable]

                unsigned long ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);

Maybe adding a “__maybe_unused” until the day that compilers are getting smarter.

author
#7 lore raw
In reply to Qian Cai

Ping. Per discussion, sounds like this is the best thing to do in order to avoid compilation warnings.

Quoted 38 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]
  unsigned long alloc_size = 0, ptr;
                                ^~~

It apparently the maintainers don't like the previous 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]/

---

v2: Incorporate the feedback from Valentin.

 kernel/sched/core.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/core.c +2 -1
index 874c427742a9..12b9b69c8a66 100644
@@ -5903,7 +5903,8 @@ 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;
+	unsigned long __maybe_unused ptr;


  wait_bit_init();