Skip to contentSkip to conversation

sysctl: move local variable in proc_do_large_bitmap() to proper scope

PATCH In review Lukas Bulwahn 4 participants 4 messages Nov 9, 2020 – Nov 13, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysctl: move local variable in proc_do_large_bitmap() to proper scope

make clang-analyzer caught my attention with:

  kernel/sysctl.c:1511:4: warning: Value stored to 'first' is never read \
  [clang-analyzer-deadcode.DeadStores]
                          first = 0;
                          ^

Commit 9f977fb7ae9d ("sysctl: add proc_do_large_bitmap") introduced proc_do_large_bitmap(), where the variable first is only effectively used when write is false; when write is true, the variable first is only used in a dead assignment.

So, simply remove this dead assignment and put the variable in local scope.

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.

---
applies cleanly on v5.10-rc3 and next-20201106

Luis, Kees, Iurii, please pick this minor non-urgent clean-up patch.

 kernel/sysctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sysctl.c +1 -2
index ce75c67572b9..cc274a431d91 100644
@@ -1423,7 +1423,6 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
 	int err = 0;
-	bool first = 1;
 	size_t left = *lenp;
 	unsigned long bitmap_len = table->maxlen;
 	unsigned long *bitmap = *(unsigned long **) table->data;
@@ -1508,12 +1507,12 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			}
 
 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
-			first = 0;
 			proc_skip_char(&p, &left, '\n');
 		}
 		left += skipped;
 	} else {
 		unsigned long bit_a, bit_b = 0;
+		bool first = 1;
 
 		while (left) {
 			bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Lukas Bulwahn

make clang-analyzer caught my attention with:

  kernel/sysctl.c:1511:4: warning: Value stored to 'first' is never read \
  [clang-analyzer-deadcode.DeadStores]
                          first = 0;
                          ^

Commit 9f977fb7ae9d ("sysctl: add proc_do_large_bitmap") introduced proc_do_large_bitmap(), where the variable first is only effectively used when write is false; when write is true, the variable first is only used in a dead assignment.

Quoted 8 lines

So, simply remove this dead assignment and put the variable in local scope.

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.

Quoted 37 lines

---
applies cleanly on v5.10-rc3 and next-20201106

Luis, Kees, Iurii, please pick this minor non-urgent clean-up patch.

 kernel/sysctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sysctl.c +1 -2
index ce75c67572b9..cc274a431d91 100644
@@ -1423,7 +1423,6 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
 	int err = 0;
-	bool first = 1;
 	size_t left = *lenp;
 	unsigned long bitmap_len = table->maxlen;
 	unsigned long *bitmap = *(unsigned long **) table->data;
@@ -1508,12 +1507,12 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			}
 
 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
-			first = 0;
 			proc_skip_char(&p, &left, '\n');
 		}
 		left += skipped;
 	} else {
 		unsigned long bit_a, bit_b = 0;
+		bool first = 1;
 
 		while (left) {
 			bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
Signature · 1 line
2.17.1
#2 lore raw
In reply to Lukas Bulwahn
Quoted 41 lines · Lukas Bulwahn

make clang-analyzer caught my attention with:

  kernel/sysctl.c:1511:4: warning: Value stored to 'first' is never read \
  [clang-analyzer-deadcode.DeadStores]
                          first = 0;
                          ^

Commit 9f977fb7ae9d ("sysctl: add proc_do_large_bitmap") introduced proc_do_large_bitmap(), where the variable first is only effectively used when write is false; when write is true, the variable first is only used in a dead assignment.

So, simply remove this dead assignment and put the variable in local scope.

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.

---
applies cleanly on v5.10-rc3 and next-20201106

Luis, Kees, Iurii, please pick this minor non-urgent clean-up patch.

 kernel/sysctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sysctl.c +0 -1
index ce75c67572b9..cc274a431d91 100644
@@ -1423,7 +1423,6 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
 	int err = 0;
-	bool first = 1;
 	size_t left = *lenp;
 	unsigned long bitmap_len = table->maxlen;
 	unsigned long *bitmap = *(unsigned long **) table->data;

kernel/sysctl.c int proc_do_large_bitmap(struct ctl_table *table, int write, · line 1515

Quoted 10 lines
kernel/sysctl.c +1 -1
@@ -1508,12 +1507,12 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			}
 
 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
-			first = 0;
 			proc_skip_char(&p, &left, '\n');
 		}
 		left += skipped;
 	} else {
 		unsigned long bit_a, bit_b = 0;
+		bool first = 1;

This looks fine, but while you are here how about setting, to match the type

first = true

And then only clearing first once

if (!first)
  proc_put_char(&buffer, &left, ',');

else

  first = false

Instead of at every loop iteraction

Tom

Quoted 3 lines
kernel/sysctl.c +0 -0
 		while (left) {
 			bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
#3 lore raw
Quoted 60 lines · Tom Rix
Quoted 41 lines · Lukas Bulwahn

make clang-analyzer caught my attention with:

  kernel/sysctl.c:1511:4: warning: Value stored to 'first' is never read \
  [clang-analyzer-deadcode.DeadStores]
                          first = 0;
                          ^

Commit 9f977fb7ae9d ("sysctl: add proc_do_large_bitmap") introduced proc_do_large_bitmap(), where the variable first is only effectively used when write is false; when write is true, the variable first is only used in a dead assignment.

So, simply remove this dead assignment and put the variable in local scope.

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.

---
applies cleanly on v5.10-rc3 and next-20201106

Luis, Kees, Iurii, please pick this minor non-urgent clean-up patch.

 kernel/sysctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sysctl.c +0 -1
index ce75c67572b9..cc274a431d91 100644
@@ -1423,7 +1423,6 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
 	int err = 0;
-	bool first = 1;
 	size_t left = *lenp;
 	unsigned long bitmap_len = table->maxlen;
 	unsigned long *bitmap = *(unsigned long **) table->data;

kernel/sysctl.c int proc_do_large_bitmap(struct ctl_table *table, int write, · line 1515

Quoted 10 lines
kernel/sysctl.c +1 -1
@@ -1508,12 +1507,12 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			}
 
 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
-			first = 0;
 			proc_skip_char(&p, &left, '\n');
 		}
 		left += skipped;
 	} else {
 		unsigned long bit_a, bit_b = 0;
+		bool first = 1;

This looks fine, but while you are here how about setting, to match the type

first = true

And then only clearing first once

Quoted 8 lines

if (!first)
  proc_put_char(&buffer, &left, ',');

else

  first = false

Instead of at every loop iteraction

Thanks for your patch Lukas!

Agreed, please resend with that change as requested by Tom. And also please add Andrew Morton <[email protected]> to your To address.

Luis