Skip to contentSkip to conversation

platform/chrome: cros_ec: Avoid -Wflex-array-member-not-at-end warning

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Mar 28, 2025 – Apr 1, 2025

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

PATCH 1/1 platform/chrome: cros_ec: Avoid -Wflex-array-member-not-at-end warning

-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.

Use the DEFINE_RAW_FLEX() helper for an on-stack definition of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.

So, with these changes, fix the following warning:

drivers/platform/chrome/cros_ec.c:106:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
 drivers/platform/chrome/cros_ec.c | 47 +++++++++++++++++--------------
 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/platform/chrome/cros_ec.c +26 -21
index 110771a8645e..cd15aa0cb082 100644
@@ -99,51 +99,56 @@ irqreturn_t cros_ec_irq_thread(int irq, void *data)
 }
 EXPORT_SYMBOL(cros_ec_irq_thread);
 
+union cros_ec_sleep_data {
+	struct ec_params_host_sleep_event req0;
+	struct ec_params_host_sleep_event_v1 req1;
+	struct ec_response_host_sleep_event_v1 resp1;
+} __packed;
+
 static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
 {
 	int ret;
-	struct {
-		struct cros_ec_command msg;
-		union {
-			struct ec_params_host_sleep_event req0;
-			struct ec_params_host_sleep_event_v1 req1;
-			struct ec_response_host_sleep_event_v1 resp1;
-		} u;
-	} __packed buf;
+	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
+			sizeof(union cros_ec_sleep_data));
+	struct ec_params_host_sleep_event *req0 =
+			(struct ec_params_host_sleep_event *)msg->data;
+	struct ec_params_host_sleep_event_v1 *req1 =
+			(struct ec_params_host_sleep_event_v1 *)msg->data;
+	struct ec_response_host_sleep_event_v1 *resp1 =
+			(struct ec_response_host_sleep_event_v1 *)msg->data;
 
-	memset(&buf, 0, sizeof(buf));
 
 	if (ec_dev->host_sleep_v1) {
-		buf.u.req1.sleep_event = sleep_event;
-		buf.u.req1.suspend_params.sleep_timeout_ms =
+		req1->sleep_event = sleep_event;
+		req1->suspend_params.sleep_timeout_ms =
 				ec_dev->suspend_timeout_ms;
 
-		buf.msg.outsize = sizeof(buf.u.req1);
+		msg->outsize = sizeof(*req1);
 		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
 		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
-			buf.msg.insize = sizeof(buf.u.resp1);
+			msg->insize = sizeof(*resp1);
 
-		buf.msg.version = 1;
+		msg->version = 1;
 
 	} else {
-		buf.u.req0.sleep_event = sleep_event;
-		buf.msg.outsize = sizeof(buf.u.req0);
+		req0->sleep_event = sleep_event;
+		msg->outsize = sizeof(*req0);
 	}
 
-	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
+	msg->command = EC_CMD_HOST_SLEEP_EVENT;
 
-	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
+	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
 	/* Report failure to transition to system wide suspend with a warning. */
 	if (ret >= 0 && ec_dev->host_sleep_v1 &&
 	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME ||
 	     sleep_event == HOST_SLEEP_EVENT_S3_RESUME)) {
 		ec_dev->last_resume_result =
-			buf.u.resp1.resume_response.sleep_transitions;
+			resp1->resume_response.sleep_transitions;
 
-		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
+		WARN_ONCE(resp1->resume_response.sleep_transitions &
 			  EC_HOST_RESUME_SLEEP_TIMEOUT,
 			  "EC detected sleep transition timeout. Total sleep transitions: %d",
-			  buf.u.resp1.resume_response.sleep_transitions &
+			  resp1->resume_response.sleep_transitions &
 			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
 	}
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw

drivers/platform/chrome/cros_ec.c irqreturn_t cros_ec_irq_thread(int irq, void *data) · line 106

Quoted 5 lines · Gustavo A. R. Silva
drivers/platform/chrome/cros_ec.c +5 -0
+union cros_ec_sleep_data {
+	struct ec_params_host_sleep_event req0;
+	struct ec_params_host_sleep_event_v1 req1;
+	struct ec_response_host_sleep_event_v1 resp1;
+} __packed;

The __packed modifier seems redundant.

drivers/platform/chrome/cros_ec.c irqreturn_t cros_ec_irq_thread(int irq, void *data) · line 112

Quoted 13 lines

static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event) {

drivers/platform/chrome/cros_ec.c +2 -8
 	int ret;
-	struct {
-		struct cros_ec_command msg;
-		union {
-			struct ec_params_host_sleep_event req0;
-			struct ec_params_host_sleep_event_v1 req1;
-			struct ec_response_host_sleep_event_v1 resp1;
-		} u;
-	} __packed buf;
+	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
+			sizeof(union cros_ec_sleep_data));

Is it possible to use something similar to:

MAX(MAX(sizeof(A), sizeof(B)),

    sizeof(C))

so that the union doesn't need to be defined?