Skip to contentSkip to conversation

drivers: net: wireless: ath: ath9: dfs: remove VLA usage

PATCH In review Andreas Christoforou 9 participants 11 messages Mar 9, 2018 – Mar 14, 2018

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

PATCH 1/1 drivers: net: wireless: ath: ath9: dfs: remove VLA usage

The kernel would like to have all stack VLA usage removed.

---
 drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +1 -2
index 6fee9a4..cfb0f84 100644
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX		= 10;
 
 /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
 #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES	= (NUM_DIFFS + 1);
 
 /* Threshold for difference of delta peaks */
 static const int MAX_DIFF		= 2;
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
 				 int datalen, bool is_ctl, bool is_ext)
 {
 	int i;
-	int max_bin[FFT_NUM_SAMPLES];
+	int max_bin[NUM_DIFFS + 1];
 	struct ath_hw *ah = sc->sc_ah;
 	struct ath_common *common = ath9k_hw_common(ah);
 	int prev_delta;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 12 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed.

---
 drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
index 6fee9a4..cfb0f84 100644

drivers/net/wireless/ath/ath9k/dfs.c static const int BIN_DELTA_MAX = 10; · line 44

Quoted 4 lines
drivers/net/wireless/ath/ath9k/dfs.c +0 -1
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX		= 10;
 
 /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
 #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES	= (NUM_DIFFS + 1);

Are you sure it is correct ?
Look for other users of "FFT_NUM_SAMPLES".

drivers/net/wireless/ath/ath9k/dfs.c static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data, · line 106

Quoted 11 lines
drivers/net/wireless/ath/ath9k/dfs.c +1 -1
 /* Threshold for difference of delta peaks */
 static const int MAX_DIFF		= 2;
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
 				 int datalen, bool is_ctl, bool is_ext)
 {
 	int i;
-	int max_bin[FFT_NUM_SAMPLES];
+	int max_bin[NUM_DIFFS + 1];
 	struct ath_hw *ah = sc->sc_ah;
 	struct ath_common *common = ath9k_hw_common(ah);
 	int prev_delta;

Always compile test the driver before sending a patch. Also, patch title seems incorrect *ath9k*

himanshu@himanshu-Vostro-3559:~/linux-next$ git log --oneline drivers/net/wireless/ath/ath9k/dfs.c
626ab67 ath9k: dfs: use swap macro in ath9k_check_chirping
50c8cd4 ath9k: remove cast to void pointer
8fc2b61 ath9k: DFS - add pulse chirp detection for FCC
....
Signature · 2 lines
Thanks
Himanshu Jha
#2 lore raw
Quoted 37 lines · Himanshu Jha
Quoted 13 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed.

---
 drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
index 6fee9a4..cfb0f84 100644

drivers/net/wireless/ath/ath9k/dfs.c static const int BIN_DELTA_MAX = 10; · line 44

Quoted 4 lines
drivers/net/wireless/ath/ath9k/dfs.c +0 -1
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX		= 10;
 
 /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
 #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES	= (NUM_DIFFS + 1);

Are you sure it is correct ?
Look for other users of "FFT_NUM_SAMPLES".

drivers/net/wireless/ath/ath9k/dfs.c static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data, · line 106

Quoted 11 lines
drivers/net/wireless/ath/ath9k/dfs.c +1 -1
 /* Threshold for difference of delta peaks */
 static const int MAX_DIFF		= 2;
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
 				 int datalen, bool is_ctl, bool is_ext)
 {
 	int i;
-	int max_bin[FFT_NUM_SAMPLES];
+	int max_bin[NUM_DIFFS + 1];
 	struct ath_hw *ah = sc->sc_ah;
 	struct ath_common *common = ath9k_hw_common(ah);
 	int prev_delta;

Always compile test the driver before sending a patch. Also, patch title seems incorrect *ath9k*

Quoted 6 lines
himanshu@himanshu-Vostro-3559:~/linux-next$ git log --oneline
drivers/net/wireless/ath/ath9k/dfs.c
626ab67 ath9k: dfs: use swap macro in ath9k_check_chirping
50c8cd4 ath9k: remove cast to void pointer
8fc2b61 ath9k: DFS - add pulse chirp detection for FCC
....

Yeah, just "ath9k:" is enough as the prefix, no need to have full directory path in the title.

Signature · 1 line
Kalle Valo
#3 lore raw
In reply to Andreas Christoforou
Quoted 1 line · Andreas Christoforou

The kernel would like to have all stack VLA usage removed.

I think there was a remark made earlier to give more explanation here. It should explain why we want "VLA on stack" removed.

Quoted 18 lines
---
  drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +0 -1
index 6fee9a4..cfb0f84 100644
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX		= 10;
 
  /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
  #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES	= (NUM_DIFFS + 1);
 
  /* Threshold for difference of delta peaks */
  static const int MAX_DIFF		= 2;

drivers/net/wireless/ath/ath9k/dfs.c static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data, · line 103

Quoted 5 lines
drivers/net/wireless/ath/ath9k/dfs.c +1 -1
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
  				 int datalen, bool is_ctl, bool is_ext)
  {
  	int i;
-	int max_bin[FFT_NUM_SAMPLES];
+	int max_bin[NUM_DIFFS + 1];

Just wondering. Is this actually a VLA. FFT_NUM_SAMPLES was static const so not really going to show a lot of variation. This array will always have the same size on the stack.

Regards,
Arend

#4 lore raw
Quoted 25 lines · Arend van Spriel
Quoted 2 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed.

I think there was a remark made earlier to give more explanation here. It should explain why we want "VLA on stack" removed.

Quoted 17 lines
---
  drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +0 -1
index 6fee9a4..cfb0f84 100644
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX                = 10;
 
  /* we need at least 3 deltas / 4 samples for a reliable chirp detection
 */
  #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES       = (NUM_DIFFS + 1);
 
Quoted 14 lines

drivers/net/wireless/ath/ath9k/dfs.c static bool ath9k_check_chirping(struct ath_softc *sc, · line 104

Quoted 9 lines
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
  /* Threshold for difference of delta peaks */
  static const int MAX_DIFF             = 2;
drivers/net/wireless/ath/ath9k/dfs.c +1 -1
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc,
 u8 *data,
                                 int datalen, bool is_ctl, bool is_ext)
  {
        int i;
-       int max_bin[FFT_NUM_SAMPLES];
+       int max_bin[NUM_DIFFS + 1];

Just wondering. Is this actually a VLA. FFT_NUM_SAMPLES was static const so not really going to show a lot of variation. This array will always have the same size on the stack.

The problem is that it's not a "constant expression", so the compiler frontend still yells about it under -Wvla. I would characterize this mainly as a fix for "accidental VLA" or "misdetected VLA" or something like that. AIUI, there really isn't a functional change here.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
Quoted 22 lines · Kees Cook
Quoted 20 lines · Arend van Spriel
Quoted 2 lines · Andreas Christoforou

The kernel would like to have all stack VLA usage removed.

I think there was a remark made earlier to give more explanation here. It should explain why we want "VLA on stack" removed.

Quoted 12 lines
---
   drivers/net/wireless/ath/ath9k/dfs.c | 3 +--
   1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
index 6fee9a4..cfb0f84 100644
@@ -41,7 +41,6 @@ static const int BIN_DELTA_MAX                = 10;
 
Quoted 5 lines
Quoted 5 lines

drivers/net/wireless/ath/ath9k/dfs.c static const int BIN_DELTA_MAX = 10; · line 44

Quoted 5 lines
drivers/net/wireless/ath/ath9k/dfs.c +0 -1
   /* we need at least 3 deltas / 4 samples for a reliable chirp detection
 */
   #define NUM_DIFFS 3
-static const int FFT_NUM_SAMPLES       = (NUM_DIFFS + 1);
 

What about this instead?

#define FFT_NUM_SAMPLES (NUM_DIFFS + 1)

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
   /* Threshold for difference of delta peaks */
   static const int MAX_DIFF             = 2;
drivers/net/wireless/ath/ath9k/dfs.c +0 -0
@@ -101,7 +100,7 @@ static bool ath9k_check_chirping(struct ath_softc *sc,
 u8 *data,
                                  int datalen, bool is_ctl, bool is_ext)
   {
Quoted 8 lines

drivers/net/wireless/ath/ath9k/dfs.c static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data, · line 103

Quoted 3 lines
drivers/net/wireless/ath/ath9k/dfs.c +1 -1
         int i;
-       int max_bin[FFT_NUM_SAMPLES];
+       int max_bin[NUM_DIFFS + 1];

Just wondering. Is this actually a VLA. FFT_NUM_SAMPLES was static const so not really going to show a lot of variation. This array will always have the same size on the stack.

Quoted 7 lines

The problem is that it's not a "constant expression", so the compiler frontend still yells about it under -Wvla. I would characterize this mainly as a fix for "accidental VLA" or "misdetected VLA" or something like that. AIUI, there really isn't a functional change here.

-Kees

--
Gustavo

#6 lore raw
In reply to Kees Cook
Quoted 5 lines · Kees Cook

The problem is that it's not a "constant expression", so the compiler frontend still yells about it under -Wvla. I would characterize this mainly as a fix for "accidental VLA" or "misdetected VLA" or something like that. AIUI, there really isn't a functional change here.

C++11's constexpr variables would be nice to have.

Cheers,
Miguel

#7 lore raw
In reply to Arend van Spriel
Quoted 3 lines

Just wondering. Is this actually a VLA. FFT_NUM_SAMPLES was static const so not really going to show a lot of variation. This array will always have the same size on the stack.

The issue is that unlike in C++, a static const can't be used in a constant expression in C. It's unclear why C is defined that way but it's how it is and there isn't currently a GCC extension making more things into constant expressions like C++.

#8 lore raw

From: Daniel Micay

Quoted 10 lines

Sent: 10 March 2018 23:45

Quoted 3 lines

Just wondering. Is this actually a VLA. FFT_NUM_SAMPLES was static const so not really going to show a lot of variation. This array will always have the same size on the stack.

The issue is that unlike in C++, a static const can't be used in a constant expression in C. It's unclear why C is defined that way but it's how it is and there isn't currently a GCC extension making more things into constant expressions like C++.

'static' and 'const' are both just qualifiers to a 'variable' You can still take it's address.
The language allows you to cast away the 'const' and write to the variable - the effect is probably 'implementation defined'.

It is probably required to be valid for 'static const' items to be patchable.

David

#9 lore raw

No, it's undefined behavior to write to a const variable. The static and const on the variable both change the code generation in the real world as permitted / encouraged by the standard. It's placed in read-only memory. Trying to write to it will break. It's not "implemented defined" to write to it, it's "undefined behavior" i.e. it's considered incorrect. There a clear distinction between those in the standard.

You're confusing having a real const for a variable with having it applied to a pointer. It's well-defined to cast away const from a pointer and write to what it points at if it's not actually const. If it is const, that's broken.

There's nothing implementation defined about either case.

The C standard could have considered static const variables to work as constant expressions just like the C++ standard. They borrowed it from there but made it less useful than const in what became the C++ standard. They also used stricter rules for the permitted implicit conversions of const pointers which made those much less usable, i.e. converting int ** to const int *const * wasn't permitted like C++. I don't think the difference between C and C++ const pointer conversions, it's a quirk of them being standardized on different timelines and ending up with different versions of the same thing. On the other hand, they might have left out having ever so slightly more useful constant expressions on purpose since people can use #define.

#10 lore raw
Quoted 2 lines

I don't think the difference between C and C++ const pointer conversions

I mean I don't think the difference between them was intended.