Skip to contentSkip to conversation

powerpc: mm: radix_tlb: rearrange the if-else block

PATCH Superseded Anders Roxell 4 participants 5 messages Nov 25, 2021 – Nov 26, 2021 PowerPC

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: mm: radix_tlb: rearrange the if-else block

Clang warns:

arch/powerpc/mm/book3s64/radix_tlb.c:1191:23: error: variable 'hstart' is uninitialized when used here [-Werror,-Wuninitialized]
__tlbiel_va_range(hstart, hend, pid,
^~~~~~
arch/powerpc/mm/book3s64/radix_tlb.c:1175:23: note: initialize the variable 'hstart' to silence this warning

                unsigned long hstart, hend;
                                    ^
                                     = 0

arch/powerpc/mm/book3s64/radix_tlb.c:1191:31: error: variable 'hend' is uninitialized when used here [-Werror,-Wuninitialized]
__tlbiel_va_range(hstart, hend, pid,
^~~~
arch/powerpc/mm/book3s64/radix_tlb.c:1175:29: note: initialize the variable 'hend' to silence this warning

                unsigned long hstart, hend;
                                          ^
                                           = 0

2 errors generated.

Rework the if-else to pull the 'IS_ENABLE(CONFIG_TRANSPARENT_HUGEPAGE)' check one level up, this will silent the warnings. That will also simplify the 'else' path. Clang is getting confused with these warnings, but the warnings is a false-positive.

---
 arch/powerpc/mm/book3s64/radix_tlb.c | 31 +++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
arch/powerpc/mm/book3s64/radix_tlb.c +24 -7
index 7724af19ed7e..e494a45ce1b4 100644
@@ -1170,16 +1170,14 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
 				_tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
 			}
 		}
-	} else {
+	} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
 		bool hflush = false;
 		unsigned long hstart, hend;
 
-		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
-			hstart = (start + PMD_SIZE - 1) & PMD_MASK;
-			hend = end & PMD_MASK;
-			if (hstart < hend)
-				hflush = true;
-		}
+		hstart = (start + PMD_SIZE - 1) & PMD_MASK;
+		hend = end & PMD_MASK;
+		if (hstart < hend)
+			hflush = true;
 
 		if (type == FLUSH_TYPE_LOCAL) {
 			asm volatile("ptesync": : :"memory");
@@ -1207,6 +1205,25 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
 				_tlbiel_va_range_multicast(mm,
 					hstart, hend, pid, PMD_SIZE, MMU_PAGE_2M, flush_pwc);
 		}
+	} else {
+
+		if (type == FLUSH_TYPE_LOCAL) {
+			asm volatile("ptesync" : : : "memory");
+			if (flush_pwc)
+				/* For PWC, only one flush is needed */
+				__tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
+			__tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
+			ppc_after_tlbiel_barrier();
+		} else if (cputlb_use_tlbie()) {
+			asm volatile("ptesync" : : : "memory");
+			if (flush_pwc)
+				__tlbie_pid(pid, RIC_FLUSH_PWC);
+			__tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
+			asm volatile("eieio; tlbsync; ptesync" : : : "memory");
+		} else {
+			_tlbiel_va_range_multicast(mm,
+					start, end, pid, page_size, mmu_virtual_psize, flush_pwc);
+		}
 	}
 out:
 	preempt_enable();
Signature · 1 line
2.33.0
#1 lore raw

Le 25/11/2021 à 16:44, Anders Roxell a écrit :

Quoted 16 lines

Clang warns:

arch/powerpc/mm/book3s64/radix_tlb.c:1191:23: error: variable 'hstart' is uninitialized when used here [-Werror,-Wuninitialized]
__tlbiel_va_range(hstart, hend, pid,
^~~~~~
arch/powerpc/mm/book3s64/radix_tlb.c:1175:23: note: initialize the variable 'hstart' to silence this warning

                 unsigned long hstart, hend;
                                     ^
                                      = 0

arch/powerpc/mm/book3s64/radix_tlb.c:1191:31: error: variable 'hend' is uninitialized when used here [-Werror,-Wuninitialized]
__tlbiel_va_range(hstart, hend, pid,
^~~~
arch/powerpc/mm/book3s64/radix_tlb.c:1175:29: note: initialize the variable 'hend' to silence this warning

                 unsigned long hstart, hend;
                                           ^
                                            = 0
Quoted 6 lines

2 errors generated.

Rework the if-else to pull the 'IS_ENABLE(CONFIG_TRANSPARENT_HUGEPAGE)' check one level up, this will silent the warnings. That will also simplify the 'else' path. Clang is getting confused with these warnings, but the warnings is a false-positive.

But you are duplicating a significant part of the code by doing that, and duplicated code generaly leads to bugs.

And we already have redundant stuff between FLUSH_TYPE_LOCAL leg and cputlb_use_tlbie() leg.

Can't you fix CLANG instead :) ?

Or just add an else to the IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) that sets hstart and hend to 0 ?

Or just put hstart and hend calculation outside the IS_ENABLED() ? After all GCC should drop the calculation when not used.

Quoted 60 lines
---
  arch/powerpc/mm/book3s64/radix_tlb.c | 31 +++++++++++++++++++++-------
  1 file changed, 24 insertions(+), 7 deletions(-)
arch/powerpc/mm/book3s64/radix_tlb.c +24 -7
index 7724af19ed7e..e494a45ce1b4 100644
@@ -1170,16 +1170,14 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
  				_tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
  			}
  		}
-	} else {
+	} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
  		bool hflush = false;
  		unsigned long hstart, hend;
 
-		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
-			hstart = (start + PMD_SIZE - 1) & PMD_MASK;
-			hend = end & PMD_MASK;
-			if (hstart < hend)
-				hflush = true;
-		}
+		hstart = (start + PMD_SIZE - 1) & PMD_MASK;
+		hend = end & PMD_MASK;
+		if (hstart < hend)
+			hflush = true;
 
  		if (type == FLUSH_TYPE_LOCAL) {
  			asm volatile("ptesync": : :"memory");
@@ -1207,6 +1205,25 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
  				_tlbiel_va_range_multicast(mm,
  					hstart, hend, pid, PMD_SIZE, MMU_PAGE_2M, flush_pwc);
  		}
+	} else {
+
+		if (type == FLUSH_TYPE_LOCAL) {
+			asm volatile("ptesync" : : : "memory");
+			if (flush_pwc)
+				/* For PWC, only one flush is needed */
+				__tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
+			__tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
+			ppc_after_tlbiel_barrier();
+		} else if (cputlb_use_tlbie()) {
+			asm volatile("ptesync" : : : "memory");
+			if (flush_pwc)
+				__tlbie_pid(pid, RIC_FLUSH_PWC);
+			__tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
+			asm volatile("eieio; tlbsync; ptesync" : : : "memory");
+		} else {
+			_tlbiel_va_range_multicast(mm,
+					start, end, pid, page_size, mmu_virtual_psize, flush_pwc);
+		}
  	}
  out:
  	preempt_enable();
#2 lore raw
Quoted 5 lines · Christophe Leroy

Le 25/11/2021 à 16:44, Anders Roxell a écrit :
Can't you fix CLANG instead :) ?

Or just add an else to the IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) that sets hstart and hend to 0 ?

That doesn't sound any less risky than duplicating the code, it can lead to incorrect changes just as easily if a patch ends up actually flushing at the wrong address, and the compiler fails to complain because of the bogus initialization.

Quoted 2 lines

Or just put hstart and hend calculation outside the IS_ENABLED() ? After all GCC should drop the calculation when not used.

I like this one. I'm still unsure how clang can get so confused about whether the variables are initialized or not, usually it handles this much better than gcc. My best guess is that one of the memory clobbers makes it conclude that 'hflush' can be true when it gets written to by an inline asm.

Arnd

#3 lore raw
Quoted 13 lines · Arnd Bergmann
Quoted 5 lines · Christophe Leroy

Le 25/11/2021 à 16:44, Anders Roxell a écrit :
Can't you fix CLANG instead :) ?

Or just add an else to the IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) that sets hstart and hend to 0 ?

That doesn't sound any less risky than duplicating the code, it can lead to incorrect changes just as easily if a patch ends up actually flushing at the wrong address, and the compiler fails to complain because of the bogus initialization.

Quoted 7 lines
Quoted 2 lines

Or just put hstart and hend calculation outside the IS_ENABLED() ? After all GCC should drop the calculation when not used.

I like this one. I'm still unsure how clang can get so confused about whether the variables are initialized or not, usually it handles this much better than gcc. My best guess is that one of the memory clobbers makes it conclude that 'hflush' can be true when it gets written to by an inline asm.

As far as I am aware, clang's analysis does not evaluate variables when generating a control flow graph and using that for static analysis:

https://godbolt.org/z/PdGxoq9j7

Based on the control flow graph, it knows that hstart and hend are uninitialized because IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) gets expanded to 0 by the preprocessor but it does not seem like it can piece together that hflush's value of false is only changed to true under the now 'if (0) {' branch, meaning that all the calls to __tlbiel_va_range() never get evaluated. That may or may not be easy to fix in clang but we run into issues like this so infrequently.

At any rate, the below diff works for me.

Cheers,
Nathan

arch/powerpc/mm/book3s64/radix_tlb.c +4 -6
index 7724af19ed7e..156a631df976 100644
@@ -1174,12 +1174,10 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
 		bool hflush = false;
 		unsigned long hstart, hend;
 
-		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
-			hstart = (start + PMD_SIZE - 1) & PMD_MASK;
-			hend = end & PMD_MASK;
-			if (hstart < hend)
-				hflush = true;
-		}
+		hstart = (start + PMD_SIZE - 1) & PMD_MASK;
+		hend = end & PMD_MASK;
+		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend)
+			hflush = true;
 
 		if (type == FLUSH_TYPE_LOCAL) {
 			asm volatile("ptesync": : :"memory");
#4 lore raw

Le 26/11/2021 à 16:46, Nathan Chancellor a écrit :

Quoted 48 lines · Arnd Bergmann
Quoted 13 lines
Quoted 5 lines · Christophe Leroy

Le 25/11/2021 à 16:44, Anders Roxell a écrit :
Can't you fix CLANG instead :) ?

Or just add an else to the IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) that sets hstart and hend to 0 ?

That doesn't sound any less risky than duplicating the code, it can lead to incorrect changes just as easily if a patch ends up actually flushing at the wrong address, and the compiler fails to complain because of the bogus initialization.

Quoted 7 lines
Quoted 2 lines

Or just put hstart and hend calculation outside the IS_ENABLED() ? After all GCC should drop the calculation when not used.

I like this one. I'm still unsure how clang can get so confused about whether the variables are initialized or not, usually it handles this much better than gcc. My best guess is that one of the memory clobbers makes it conclude that 'hflush' can be true when it gets written to by an inline asm.

As far as I am aware, clang's analysis does not evaluate variables when generating a control flow graph and using that for static analysis:

https://godbolt.org/z/PdGxoq9j7

Based on the control flow graph, it knows that hstart and hend are uninitialized because IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) gets expanded to 0 by the preprocessor but it does not seem like it can piece together that hflush's value of false is only changed to true under the now 'if (0) {' branch, meaning that all the calls to __tlbiel_va_range() never get evaluated. That may or may not be easy to fix in clang but we run into issues like this so infrequently.

At any rate, the below diff works for me.

Cheers,
Nathan

arch/powerpc/mm/book3s64/radix_tlb.c +0 -0
index 7724af19ed7e..156a631df976 100644
@@ -1174,12 +1174,10 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm,
  		bool hflush = false;
  		unsigned long hstart, hend;
 

arch/powerpc/mm/book3s64/radix_tlb.c static inline void __radix__flush_tlb_range(struct mm_struct *mm, · line 1180

Quoted 10 lines
arch/powerpc/mm/book3s64/radix_tlb.c +4 -6
-		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
-			hstart = (start + PMD_SIZE - 1) & PMD_MASK;
-			hend = end & PMD_MASK;
-			if (hstart < hend)
-				hflush = true;
-		}
+		hstart = (start + PMD_SIZE - 1) & PMD_MASK;
+		hend = end & PMD_MASK;
+		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend)
+			hflush = true;

Yes I like that much better.

Maybe even better with

	hflush = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend;

(And remove default false value at declaration).

Quoted 4 lines
arch/powerpc/mm/book3s64/radix_tlb.c +0 -0
  		if (type == FLUSH_TYPE_LOCAL) {
  			asm volatile("ptesync": : :"memory");