Skip to contentSkip to conversation

mm: fix used but uninitialized variable

PATCH In review Zhouyi Zhou 2 participants 5 messages Aug 24, 2022

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

PATCH 1/1 mm: fix used but uninitialized variable

In function walk_hugetlb_range, the local variable err may be used uninitialzed when:
ops->pte_hole in side of "else if (ops->pte_hole)" is false.

---
Dear mm Developers:

When I build kernel using "make CC=clang-14"
the compiler complains following:

CC mm/pagewalk.o
mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

                else if (ops->pte_hole)
                         ^~~~~~~~~~~~~
mm/pagewalk.c:321:7: note: uninitialized use occurs here
                if (err)
                    ^~~
mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always true
                else if (ops->pte_hole)
                     ^~~~~~~~~~~~~~~~~~
mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence this warning
                int err;
                       ^
                        = 0

1 error generated.
make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1
make: *** [Makefile:2006: mm] Error 2

I initialize that variable outside of the for loop because we can assign 0 to err only once in this function.

After my fix, I can compile the kernel.

Many Thanks
Zhouyi
--

 mm/pagewalk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/pagewalk.c +1 -1
index 54b2a1beeeb3..b6eb330e8ecd 100644
@@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
 	unsigned long hmask = huge_page_mask(h);
 	unsigned long sz = huge_page_size(h);
 	const struct mm_walk_ops *ops = walk->ops;
+	int err = 0;
 
 	for (; addr < end; addr = next) {
-		int err;
 		pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
 
 		next = hugetlb_entry_end(h, addr, end);
Signature · 1 line
2.34.1
#1 lore raw

Le 24/08/2022 à 07:56, Zhouyi Zhou a écrit :

Quoted 45 lines

In function walk_hugetlb_range, the local variable err may be used uninitialzed when:
ops->pte_hole in side of "else if (ops->pte_hole)" is false.

---
Dear mm Developers:

When I build kernel using "make CC=clang-14"
the compiler complains following:

CC mm/pagewalk.o
mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

                 else if (ops->pte_hole)
                          ^~~~~~~~~~~~~
mm/pagewalk.c:321:7: note: uninitialized use occurs here
                 if (err)
                     ^~~
mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always true
                 else if (ops->pte_hole)
                      ^~~~~~~~~~~~~~~~~~
mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence this warning
                 int err;
                        ^
                         = 0

1 error generated.
make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1
make: *** [Makefile:2006: mm] Error 2

I initialize that variable outside of the for loop because we can assign 0 to err only once in this function.

After my fix, I can compile the kernel.

Many Thanks
Zhouyi
--

  mm/pagewalk.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/pagewalk.c +0 -0
index 54b2a1beeeb3..b6eb330e8ecd 100644

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 309

Quoted 4 lines
mm/pagewalk.c +1 -0
@@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
  	unsigned long hmask = huge_page_mask(h);
  	unsigned long sz = huge_page_size(h);
  	const struct mm_walk_ops *ops = walk->ops;
+	int err = 0;

Why do you move it back outside of the for loop allthough it is exclusively used inside the loop ?

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 311

Quoted 3 lines
mm/pagewalk.c +0 -1
  	for (; addr < end; addr = next) {
-		int err;

Another solution would be to add an explicit else, setting err = 0 in the if/else if sequence.

Quoted 3 lines
mm/pagewalk.c +0 -0
  		pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
 
  		next = hugetlb_entry_end(h, addr, end);
#2 lore raw

Thank Christophe for reviewing my patch

Quoted 44 lines · Christophe Leroy
Quoted 40 lines · Zhouyi Zhou

In function walk_hugetlb_range, the local variable err may be used uninitialzed when:
ops->pte_hole in side of "else if (ops->pte_hole)" is false.

---
Dear mm Developers:

When I build kernel using "make CC=clang-14"
the compiler complains following:

CC mm/pagewalk.o
mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

                 else if (ops->pte_hole)
                          ^~~~~~~~~~~~~
mm/pagewalk.c:321:7: note: uninitialized use occurs here
                 if (err)
                     ^~~
mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always true
                 else if (ops->pte_hole)
                      ^~~~~~~~~~~~~~~~~~
mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence this warning
                 int err;
                        ^
                         = 0

1 error generated.
make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1
make: *** [Makefile:2006: mm] Error 2

I initialize that variable outside of the for loop because we can assign 0 to err only once in this function.

After my fix, I can compile the kernel.

Many Thanks
Zhouyi
--

  mm/pagewalk.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 309

Quoted 9 lines
mm/pagewalk.c +1 -0
index 54b2a1beeeb3..b6eb330e8ecd 100644
@@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
      unsigned long hmask = huge_page_mask(h);
      unsigned long sz = huge_page_size(h);
      const struct mm_walk_ops *ops = walk->ops;
+     int err = 0;

Why do you move it back outside of the for loop allthough it is exclusively used inside the loop ?

I move it outside of the for loop for performance consideration. Because if we initialize err inside, there will be an assignment statement every iteration.

Quoted 7 lines

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 311

Quoted 4 lines
mm/pagewalk.c +0 -1
      for (; addr < end; addr = next) {
-             int err;

Another solution would be to add an explicit else, setting err = 0 in the if/else if sequence.

Thank Christophe for your valuable advice, I am going to prepare a 2nd version

Thanks
Zhouyi

Quoted 4 lines
Quoted 4 lines
mm/pagewalk.c +0 -0
              pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
 
              next = hugetlb_entry_end(h, addr, end);
#3 lore raw

Le 24/08/2022 à 08:41, Zhouyi Zhou a écrit :

Quoted 60 lines

Thank Christophe for reviewing my patch

Quoted 44 lines · Christophe Leroy
Quoted 40 lines · Zhouyi Zhou

In function walk_hugetlb_range, the local variable err may be used uninitialzed when:
ops->pte_hole in side of "else if (ops->pte_hole)" is false.

---
Dear mm Developers:

When I build kernel using "make CC=clang-14"
the compiler complains following:

CC mm/pagewalk.o
mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

                  else if (ops->pte_hole)
                           ^~~~~~~~~~~~~
mm/pagewalk.c:321:7: note: uninitialized use occurs here
                  if (err)
                      ^~~
mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always true
                  else if (ops->pte_hole)
                       ^~~~~~~~~~~~~~~~~~
mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence this warning
                  int err;
                         ^
                          = 0

1 error generated.
make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1
make: *** [Makefile:2006: mm] Error 2

I initialize that variable outside of the for loop because we can assign 0 to err only once in this function.

After my fix, I can compile the kernel.

Many Thanks
Zhouyi
--

   mm/pagewalk.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 309

Quoted 9 lines
mm/pagewalk.c +1 -0
index 54b2a1beeeb3..b6eb330e8ecd 100644
@@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
       unsigned long hmask = huge_page_mask(h);
       unsigned long sz = huge_page_size(h);
       const struct mm_walk_ops *ops = walk->ops;
+     int err = 0;

Why do you move it back outside of the for loop allthough it is exclusively used inside the loop ?

Quoted 3 lines

I move it outside of the for loop for performance consideration. Because if we initialize err inside, there will be an assignment statement every iteration.

I think GCC is smart enough to do the assignment only when necessary, maybe have a look at the generated assembly in order to confirm.

Quoted 8 lines
Quoted 7 lines

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 311

Quoted 4 lines
mm/pagewalk.c +0 -1
       for (; addr < end; addr = next) {
-             int err;

Another solution would be to add an explicit else, setting err = 0 in the if/else if sequence.

Thank Christophe for your valuable advice, I am going to prepare a 2nd version

Don't spend too much time on that, there are already other people looking at it, see
https://lore.kernel.org/linux-mm/[email protected]/T/

Quoted 7 lines

Thanks
Zhouyi

Quoted 4 lines
Quoted 4 lines
mm/pagewalk.c +0 -0
               pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
 
               next = hugetlb_entry_end(h, addr, end);
#4 lore raw
Quoted 64 lines · Christophe Leroy
Quoted 60 lines · Zhouyi Zhou

Thank Christophe for reviewing my patch

Quoted 56 lines · Christophe Leroy
Quoted 45 lines · Zhouyi Zhou

In function walk_hugetlb_range, the local variable err may be used uninitialzed when:
ops->pte_hole in side of "else if (ops->pte_hole)" is false.

---
Dear mm Developers:

When I build kernel using "make CC=clang-14"
the compiler complains following:

CC mm/pagewalk.o
mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

                  else if (ops->pte_hole)
                           ^~~~~~~~~~~~~
mm/pagewalk.c:321:7: note: uninitialized use occurs here
                  if (err)
                      ^~~
mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always true
                  else if (ops->pte_hole)
                       ^~~~~~~~~~~~~~~~~~
mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence this warning
                  int err;
                         ^
                          = 0

1 error generated.
make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1
make: *** [Makefile:2006: mm] Error 2

I initialize that variable outside of the for loop because we can assign 0 to err only once in this function.

After my fix, I can compile the kernel.

Many Thanks
Zhouyi
--

   mm/pagewalk.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
mm/pagewalk.c +0 -0
index 54b2a1beeeb3..b6eb330e8ecd 100644

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 309

Quoted 4 lines
mm/pagewalk.c +1 -0
@@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
       unsigned long hmask = huge_page_mask(h);
       unsigned long sz = huge_page_size(h);
       const struct mm_walk_ops *ops = walk->ops;
+     int err = 0;

Why do you move it back outside of the for loop allthough it is exclusively used inside the loop ?

Quoted 6 lines
Quoted 3 lines

I move it outside of the for loop for performance consideration. Because if we initialize err inside, there will be an assignment statement every iteration.

I think GCC is smart enough to do the assignment only when necessary, maybe have a look at the generated assembly in order to confirm.

Thank Christophe for your guidance, I disassembled the GCC generated code, it optimized away int err = 0 on both X86 and PowerPC platforms. The disassembled instructions are quite long, so I prefer not to paste in my email unless someone asks me to do so ;-)

Quoted 10 lines
Quoted 9 lines
Quoted 8 lines

mm/pagewalk.c static int walk_hugetlb_range(unsigned long addr, unsigned long end, · line 311

Quoted 5 lines
mm/pagewalk.c +0 -1
       for (; addr < end; addr = next) {
-             int err;

Another solution would be to add an explicit else, setting err = 0 in the if/else if sequence.

Thank Christophe for your valuable advice, I am going to prepare a 2nd version

Thank Christophe for your reminder, I only subscribed to the RCU mailing list currently, so I submitted duplicated work. I should be more devoted to the Linux community. I am sorry.

Quoted 7 lines

Don't spend too much time on that, there are already other people looking at it, see
https://lore.kernel.org/linux-mm/[email protected]/T/

Quoted 3 lines

Thanks
Zhouyi

Many thanks
Zhouyi

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
mm/pagewalk.c +0 -0
               pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
 
               next = hugetlb_entry_end(h, addr, end);