Skip to contentSkip to conversation

seq_buf: make DECLARE_SEQ_BUF() usable

PATCH In review Nathan Lynch 4 participants 5 messages Jan 16, 2024 – Jan 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seq_buf: make DECLARE_SEQ_BUF() usable

Using the address operator on the array doesn't work:

./include/linux/seq_buf.h:27:27: error: initialization of ‘char *’
from incompatible pointer type ‘char (*)[128]’ [-Werror=incompatible-pointer-types]
27 | .buffer = &__ ## NAME ## _buffer, \
| ^

Apart from fixing that, we can improve DECLARE_SEQ_BUF() by using a compound literal to define the buffer array without attaching a name to it. This makes the macro a single statement, allowing constructs such as:

  static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

to work as intended.

Fixes dcc4e5728eea ("seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()")
---
 include/linux/seq_buf.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/seq_buf.h +1 -2
index 5fb1f12c33f9..c44f4b47b945 100644
@@ -22,9 +22,8 @@ struct seq_buf {
 };
 
 #define DECLARE_SEQ_BUF(NAME, SIZE)			\
-	char __ ## NAME ## _buffer[SIZE] = "";		\
 	struct seq_buf NAME = {				\
-		.buffer = &__ ## NAME ## _buffer,	\
+		.buffer = (char[SIZE]) { 0 },		\
 		.size = SIZE,				\
 	}
 

---
base-commit: 70d201a40823acba23899342d62bc2644051ad2e
change-id: 20240112-declare-seq-buf-fix-9803b7e679bc

Best regards,

Signature · 1 line
Nathan Lynch <[email protected]>
#1 lore raw

Le 16/01/2024 à 15:09, Nathan Lynch via B4 Relay a écrit :

Quoted 30 lines

From: Nathan Lynch <[email protected]>

Using the address operator on the array doesn't work:

/include/linux/seq_buf.h:27:27: error: initialization of ‘char *’
from incompatible pointer type ‘char (*)[128]’ [-Werror=incompatible-pointer-types]

    27 |                 .buffer = &__ ## NAME ## _buffer,       \
       |                           ^

Apart from fixing that, we can improve DECLARE_SEQ_BUF() by using a compound literal to define the buffer array without attaching a name to it. This makes the macro a single statement, allowing constructs such as:

   static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

to work as intended.

Fixes dcc4e5728eea ("seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()")
---
  include/linux/seq_buf.h | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/seq_buf.h +0 -0
index 5fb1f12c33f9..c44f4b47b945 100644

include/linux/seq_buf.h struct seq_buf { · line 28

Quoted 9 lines
include/linux/seq_buf.h +1 -2
@@ -22,9 +22,8 @@ struct seq_buf {
  };
 
  #define DECLARE_SEQ_BUF(NAME, SIZE)			\
-	char __ ## NAME ## _buffer[SIZE] = "";		\
  	struct seq_buf NAME = {				\
-		.buffer = &__ ## NAME ## _buffer,	\
+		.buffer = (char[SIZE]) { 0 },		\
  		.size = SIZE,				\
  	}

Hi,

just removing the & in ".buffer = __ ## NAME ## _buffer, \" also works IIRC.

See [1], which unfortunately has been unnoticed.

CJ

[1]: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
include/linux/seq_buf.h +0 -1
---

base-commit: 70d201a40823acba23899342d62bc2644051ad2e
change-id: 20240112-declare-seq-buf-fix-9803b7e679bc

Best regards,

#2 lore raw
Quoted 45 lines · On Tue, 16 Jan 2024 20:40:29 +0100 Christophe JAILLET…

Le 16/01/2024 à 15:09, Nathan Lynch via B4 Relay a écrit :

Quoted 30 lines

From: Nathan Lynch <[email protected]>

Using the address operator on the array doesn't work:

/include/linux/seq_buf.h:27:27: error: initialization of ‘char *’
from incompatible pointer type ‘char (*)[128]’ [-Werror=incompatible-pointer-types]

    27 |                 .buffer = &__ ## NAME ## _buffer,       \
       |                           ^

Apart from fixing that, we can improve DECLARE_SEQ_BUF() by using a compound literal to define the buffer array without attaching a name to it. This makes the macro a single statement, allowing constructs such as:

   static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

to work as intended.

Fixes dcc4e5728eea ("seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()")
---
  include/linux/seq_buf.h | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/seq_buf.h +0 -0
index 5fb1f12c33f9..c44f4b47b945 100644

include/linux/seq_buf.h struct seq_buf { · line 28

Quoted 9 lines
include/linux/seq_buf.h +1 -2
@@ -22,9 +22,8 @@ struct seq_buf {
  };
 
  #define DECLARE_SEQ_BUF(NAME, SIZE)			\
-	char __ ## NAME ## _buffer[SIZE] = "";		\
  	struct seq_buf NAME = {				\
-		.buffer = &__ ## NAME ## _buffer,	\
+		.buffer = (char[SIZE]) { 0 },		\
  		.size = SIZE,				\
  	}  

Hi,

just removing the & in ".buffer = __ ## NAME ## _buffer, \" also works IIRC.

Quoted 7 lines

See [1], which unfortunately has been unnoticed.

CJ

[1]:
https://lore.kernel.org/all/[email protected]/

I guess I missed that.

But it still doesn't fix this case:

static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

Which this patch does.

-- Steve

#3 lore raw
In reply to Nathan Lynch

Kees,

Are you OK with this change? I ran it through my tests and have another pull request ready to go that includes this. But I don't want to send it without an Acked-by from you.

Luckily, Linus is on non-voluntary vacation so we may still have time ;-)

-- Steve

Quoted 46 lines · On Tue, 16 Jan 2024 08:09:25 -0600 Nathan Lynch via B4 Relay…

From: Nathan Lynch <[email protected]>

Using the address operator on the array doesn't work:

./include/linux/seq_buf.h:27:27: error: initialization of ‘char *’
from incompatible pointer type ‘char (*)[128]’ [-Werror=incompatible-pointer-types]
27 | .buffer = &__ ## NAME ## _buffer, \
| ^

Apart from fixing that, we can improve DECLARE_SEQ_BUF() by using a compound literal to define the buffer array without attaching a name to it. This makes the macro a single statement, allowing constructs such as:

  static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

to work as intended.

Fixes dcc4e5728eea ("seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()")
---
 include/linux/seq_buf.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/seq_buf.h +1 -3
index 5fb1f12c33f9..c44f4b47b945 100644
@@ -22,9 +22,8 @@ struct seq_buf {
 };
 
 #define DECLARE_SEQ_BUF(NAME, SIZE)			\
-	char __ ## NAME ## _buffer[SIZE] = "";		\
 	struct seq_buf NAME = {				\
-		.buffer = &__ ## NAME ## _buffer,	\
+		.buffer = (char[SIZE]) { 0 },		\
 		.size = SIZE,				\
 	}
 
 
---

base-commit: 70d201a40823acba23899342d62bc2644051ad2e
change-id: 20240112-declare-seq-buf-fix-9803b7e679bc

Best regards,

Acked-by
#4 lore raw
Quoted 6 lines · Steven Rostedt

Kees,

Are you OK with this change? I ran it through my tests and have another pull request ready to go that includes this. But I don't want to send it without an Acked-by from you.

Yeah! This cleanly solves the lack of being able to add the "static", etc.

-Kees

Quoted 56 lines

Luckily, Linus is on non-voluntary vacation so we may still have time ;-)

-- Steve

Quoted 46 lines · On Tue, 16 Jan 2024 08:09:25 -0600 Nathan Lynch via B4 Relay…

From: Nathan Lynch <[email protected]>

Using the address operator on the array doesn't work:

./include/linux/seq_buf.h:27:27: error: initialization of ‘char *’
from incompatible pointer type ‘char (*)[128]’ [-Werror=incompatible-pointer-types]
27 | .buffer = &__ ## NAME ## _buffer, \
| ^

Apart from fixing that, we can improve DECLARE_SEQ_BUF() by using a compound literal to define the buffer array without attaching a name to it. This makes the macro a single statement, allowing constructs such as:

  static DECLARE_SEQ_BUF(my_seq_buf, MYSB_SIZE);

to work as intended.

Fixes dcc4e5728eea ("seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()")
---
 include/linux/seq_buf.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/seq_buf.h +1 -3
index 5fb1f12c33f9..c44f4b47b945 100644
@@ -22,9 +22,8 @@ struct seq_buf {
 };
 
 #define DECLARE_SEQ_BUF(NAME, SIZE)			\
-	char __ ## NAME ## _buffer[SIZE] = "";		\
 	struct seq_buf NAME = {				\
-		.buffer = &__ ## NAME ## _buffer,	\
+		.buffer = (char[SIZE]) { 0 },		\
 		.size = SIZE,				\
 	}
 
 
---

base-commit: 70d201a40823acba23899342d62bc2644051ad2e
change-id: 20240112-declare-seq-buf-fix-9803b7e679bc

Best regards,

Signature · 1 line
Kees Cook