Skip to contentSkip to conversation

mei: Avoid the use of one-element arrays

PATCH Superseded Gustavo A. R. Silva 2 participants 5 messages Jul 14, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mei: Avoid the use of one-element arrays

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure. Kernel code should always use “flexible array members”[1] for these cases or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment. The older style of one-element or zero-length arrays should no longer be used[2].

Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the variable type is changed but the corresponding sizeof that is passed as argument is not.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

---
 drivers/misc/mei/hbm.c | 4 ++--
 drivers/misc/mei/hw.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/misc/mei/hbm.c +2 -2
index a44094cdbc36..f020d5594154 100644
@@ -408,14 +408,14 @@ static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
 {
 	struct mei_msg_hdr mei_hdr;
 	struct hbm_add_client_response resp;
-	const size_t len = sizeof(struct hbm_add_client_response);
+	const size_t len = sizeof(resp);
 	int ret;
 
 	dev_dbg(dev->dev, "adding client response\n");
 
 	mei_hbm_hdr(&mei_hdr, len);
 
-	memset(&resp, 0, sizeof(struct hbm_add_client_response));
+	memset(&resp, 0, len);
 	resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
 	resp.me_addr = addr;
 	resp.status  = status;
drivers/misc/mei/hw.h +3 -3
index b1a8d5ec88b3..8c0297f0e7f3 100644
@@ -346,13 +346,13 @@ struct hbm_add_client_request {
  * @hbm_cmd: bus message command header
  * @me_addr: address of the client in ME
  * @status: if HBMS_SUCCESS then the client can now accept connections.
- * @reserved: reserved
+ * @reserved: reserved for alignment.
  */
 struct hbm_add_client_response {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 status;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
@@ -461,7 +461,7 @@ struct hbm_notification {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 host_addr;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
Signature · 1 line
2.27.0
#1 lore raw
Quoted 13 lines

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure.
Kernel code should always use “flexible array members”[1] for these cases or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment. The older style of one-element or zero-length arrays should no longer be used[2].

Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the variable type is changed but the corresponding sizeof that is passed as argument is not.

Quoted 4 lines

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

I'm okay with the patch but in this case the description is a bit off. In this case there was no intention for a flexible arrays its just a reserved field.

Quoted 57 lines
---
 drivers/misc/mei/hbm.c | 4 ++--
 drivers/misc/mei/hw.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/misc/mei/hbm.c index renamed from drivers/misc/mei/hbm.c +0 -0

a44094cdbc36..f020d5594154 100644

drivers/misc/mei/hbm.c +2 -2
@@ -408,14 +408,14 @@ static int mei_hbm_add_cl_resp(struct mei_device
 *dev, u8 addr, u8 status)  {
 	struct mei_msg_hdr mei_hdr;
 	struct hbm_add_client_response resp;
-	const size_t len = sizeof(struct hbm_add_client_response);
+	const size_t len = sizeof(resp);
 	int ret;
 
 	dev_dbg(dev->dev, "adding client response\n");
 
 	mei_hbm_hdr(&mei_hdr, len);
 
-	memset(&resp, 0, sizeof(struct hbm_add_client_response));
+	memset(&resp, 0, len);
 	resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
 	resp.me_addr = addr;
 	resp.status  = status;
drivers/misc/mei/hw.h index renamed from drivers/misc/mei/hw.h +0 -0

b1a8d5ec88b3..8c0297f0e7f3 100644

drivers/misc/mei/hw.h +3 -4
@@ -346,13 +346,13 @@ struct hbm_add_client_request {
  * @hbm_cmd: bus message command header
  * @me_addr: address of the client in ME
  * @status: if HBMS_SUCCESS then the client can now accept connections.
- * @reserved: reserved
+ * @reserved: reserved for alignment.
  */
 struct hbm_add_client_response {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 status;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
@@ -461,7 +461,7 @@ struct hbm_notification {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 host_addr;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
--

2.27.0

#2 lore raw
Quoted 13 lines · Tomas
Quoted 13 lines

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure.
Kernel code should always use “flexible array members”[1] for these cases or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment. The older style of one-element or zero-length arrays should no longer be used[2].

Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the variable type is changed but the corresponding sizeof that is passed as argument is not.

Quoted 7 lines
Quoted 4 lines

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

I'm okay with the patch but in this case the description is a bit off. In this case there was no intention for a flexible arrays its just a reserved field.

The reserved field is actually mentioned in the description:

"... or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment."

Thanks
--
Gustavo

Quoted 58 lines
Quoted 57 lines
---
 drivers/misc/mei/hbm.c | 4 ++--
 drivers/misc/mei/hw.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/misc/mei/hbm.c index renamed from drivers/misc/mei/hbm.c +0 -0

a44094cdbc36..f020d5594154 100644

drivers/misc/mei/hbm.c +2 -2
@@ -408,14 +408,14 @@ static int mei_hbm_add_cl_resp(struct mei_device
 *dev, u8 addr, u8 status)  {
 	struct mei_msg_hdr mei_hdr;
 	struct hbm_add_client_response resp;
-	const size_t len = sizeof(struct hbm_add_client_response);
+	const size_t len = sizeof(resp);
 	int ret;
 
 	dev_dbg(dev->dev, "adding client response\n");
 
 	mei_hbm_hdr(&mei_hdr, len);
 
-	memset(&resp, 0, sizeof(struct hbm_add_client_response));
+	memset(&resp, 0, len);
 	resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
 	resp.me_addr = addr;
 	resp.status  = status;
drivers/misc/mei/hw.h index renamed from drivers/misc/mei/hw.h +0 -0

b1a8d5ec88b3..8c0297f0e7f3 100644

drivers/misc/mei/hw.h +3 -4
@@ -346,13 +346,13 @@ struct hbm_add_client_request {
  * @hbm_cmd: bus message command header
  * @me_addr: address of the client in ME
  * @status: if HBMS_SUCCESS then the client can now accept connections.
- * @reserved: reserved
+ * @reserved: reserved for alignment.
  */
 struct hbm_add_client_response {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 status;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
@@ -461,7 +461,7 @@ struct hbm_notification {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 host_addr;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
--

2.27.0

#3 lore raw
Quoted 23 lines · Tomas
Quoted 7 lines
Quoted 7 lines

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure. Kernel code should always use “flexible array members”[1] for these cases or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment. The older style of one-element or zero-length arrays

should no longer be used[2].

Quoted 5 lines
Quoted 5 lines

Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the variable type is changed but the corresponding sizeof that is passed as

argument is not.

Quoted 7 lines
Quoted 5 lines

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

I'm okay with the patch but in this case the description is a bit off. In this case there was no intention for a flexible arrays its just a reserved

Quoted 7 lines

field.

Quoted 1 line

The reserved field is actually mentioned in the description:

"... or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment."

Right, but it looks not connected to overall context, it looks like not very clean reuse of a commit message. I would say that this reserved[1] rather had confused the detection scripts you are using for the cleanup you are doing. Again, I'm okay with the patch, but if you can reword the commit message it would be even more okay.

Quoted 64 lines

Thanks
--
Gustavo

Quoted 21 lines
Quoted 21 lines
---
 drivers/misc/mei/hbm.c | 4 ++--
 drivers/misc/mei/hw.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/misc/mei/hbm.c index renamed from drivers/misc/mei/hbm.c +0 -0

a44094cdbc36..f020d5594154 100644

drivers/misc/mei/hbm.c +1 -1
@@ -408,14 +408,14 @@ static int mei_hbm_add_cl_resp(struct
 mei_device *dev, u8 addr, u8 status)  {
 	struct mei_msg_hdr mei_hdr;
 	struct hbm_add_client_response resp;
-	const size_t len = sizeof(struct hbm_add_client_response);
+	const size_t len = sizeof(resp);
 	int ret;
 
 	dev_dbg(dev->dev, "adding client response\n");
 
 	mei_hbm_hdr(&mei_hdr, len);
 
Quoted 13 lines

drivers/misc/mei/hw.h struct hbm_add_client_request { · line 348

Quoted 13 lines
drivers/misc/mei/hbm.c +1 -1
-	memset(&resp, 0, sizeof(struct hbm_add_client_response));
+	memset(&resp, 0, len);
 	resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
 	resp.me_addr = addr;
 	resp.status  = status;
drivers/misc/mei/hw.h index renamed from drivers/misc/mei/hw.h +0 -0

b1a8d5ec88b3..8c0297f0e7f3 100644

drivers/misc/mei/hw.h +0 -0
@@ -346,13 +346,13 @@ struct hbm_add_client_request {
  * @hbm_cmd: bus message command header
  * @me_addr: address of the client in ME
  * @status: if HBMS_SUCCESS then the client can now accept

connections.

Quoted 24 lines
Quoted 23 lines
drivers/misc/mei/hw.h +3 -4
- * @reserved: reserved
+ * @reserved: reserved for alignment.
  */
 struct hbm_add_client_response {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 status;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
@@ -461,7 +461,7 @@ struct hbm_notification {
 	u8 hbm_cmd;
 	u8 me_addr;
 	u8 host_addr;
-	u8 reserved[1];
+	u8 reserved;
 } __packed;
 
 /**
--

2.27.0

#4 lore raw
Quoted 26 lines · Tomas
Quoted 26 lines · Tomas
Quoted 7 lines
Quoted 7 lines

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure. Kernel code should always use “flexible array members”[1] for these cases or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment. The older style of one-element or zero-length arrays

should no longer be used[2].

Quoted 5 lines
Quoted 5 lines

Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the variable type is changed but the corresponding sizeof that is passed as

argument is not.

Quoted 7 lines
Quoted 5 lines

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

I'm okay with the patch but in this case the description is a bit off. In this case there was no intention for a flexible arrays its just a reserved

field.

Quoted 1 line
Quoted 9 lines
Quoted 4 lines

The reserved field is actually mentioned in the description:

"... or, as in this particular case, replace the one-element array with a simple value type u8 reserved once this is just a placeholder for alignment."

Right, but it looks not connected to overall context, it looks like not very clean reuse of a commit message. I would say that this reserved[1] rather had confused the detection scripts you are using for the cleanup you are doing. Again, I'm okay with the patch, but if you can reword the commit message it would be even more okay.

Yep; I've come up with a more concise text for these sorts of cases:

"One-element arrays are being deprecated[1]. Replace the one-element array with a simple value type u8 reserved, once this is just a placeholder for alignment.

[1] https://github.com/KSPP/linux/issues/79"

I'll send v2 with the text above, shortly.

Thanks for the feedback.
--
Gustavo