Skip to contentSkip to conversation

platform/chrome: cros_ec_proto: Fix deferred response

PATCH Superseded Rob Barnes 2 participants 2 messages Aug 27 – Aug 28

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Rob Barnes

author
#0 lore raw

PATCH v4 1/1 platform/chrome: cros_ec_proto: Fix deferred response

When cros_ec_wait_until_complete() succeeds after an EC_RES_IN_PROGRESS status, it previously returned the 4-byte transfer size of EC_CMD_GET_COMMS_STATUS. For 0-byte payload commands (such as EC_CMD_FLASH_ERASE), userspace received 4 bytes instead of 0, causing response size validation errors. For commands expecting non-zero response payloads, the kernel exited without issuing EC_CMD_RESEND_RESPONSE, leaving response buffers uninitialized.

Refactor cros_ec_wait_until_complete() to pass orig_msg, issue EC_CMD_RESEND_RESPONSE into orig_msg->data when insize > 0, and return 0 for 0-byte response commands.

---
v4:
- Rebase against upstream chrome-platform/for-next branch (Tzung-Bi).
- Send patch using git-send-email via Google Mail Relay to prevent MTA line-wrapping (Tzung-Bi).

v3:
- Rebase against upstream chrome-platform tree (Tzung-Bi).

v2:
- Drop redundant commit description sentences (Tzung-Bi).
- Align function signature parameters to open parenthesis (Tzung-Bi).
- Check orig_msg->insize == 0 and add comments explaining the logic (Tzung-Bi / Rob).
- Use u32 instead of uint32_t (Tzung-Bi).
- Rename 0bytes test to 0byte (Tzung-Bi).
- Check header immutability for insize in test cases (Tzung-Bi).

---
 drivers/platform/chrome/cros_ec_proto.c      |  32 +++-
 drivers/platform/chrome/cros_ec_proto_test.c | 158 ++++++++++++++++++-
 2 files changed, 183 insertions(+), 7 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +28 -4
index 1d8d9168ec1a..6d7c57e381e5 100644
@@ -138,7 +138,8 @@ static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_co
 	return ret;
 }
 
-static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
+static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev,
+				       struct cros_ec_command *orig_msg)
 {
 	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
 			sizeof(struct ec_response_get_comms_status));
@@ -161,7 +162,7 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *
 		if (ret < 0)
 			return ret;
 
-		*result = msg->result;
+		orig_msg->result = msg->result;
 		if (msg->result != EC_RES_SUCCESS)
 			return ret;
 
@@ -170,8 +171,31 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *
 			break;
 		}
 
-		if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
+		if (!(status->flags & EC_COMMS_STATUS_PROCESSING)) {
+			u32 orig_cmd, orig_outsize, orig_version;
+
+			/* If no response payload is expected, return 0. */
+			if (orig_msg->insize == 0)
+				return 0;
+
+			/*
+			 * Request the response using EC_CMD_RESEND_RESPONSE.
+			 * Restore the original message fields so it appears
+			 * to be a direct response to the original command.
+			 */
+			orig_cmd = orig_msg->command;
+			orig_outsize = orig_msg->outsize;
+			orig_version = orig_msg->version;
+
+			orig_msg->command = EC_CMD_RESEND_RESPONSE;
+			orig_msg->outsize = 0;
+			orig_msg->version = 0;
+			ret = cros_ec_xfer_command(ec_dev, orig_msg);
+			orig_msg->command = orig_cmd;
+			orig_msg->outsize = orig_outsize;
+			orig_msg->version = orig_version;
 			return ret;
+		}
 	}
 
 	if (i >= EC_COMMAND_RETRIES)
@@ -185,7 +209,7 @@ static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_co
 	int ret = cros_ec_xfer_command(ec_dev, msg);
 
 	if (msg->result == EC_RES_IN_PROGRESS)
-		ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
+		ret = cros_ec_wait_until_complete(ec_dev, msg);
 
 	return ret;
 }
drivers/platform/chrome/cros_ec_proto_test.c +155 -3

182 lines not on this page · raw message

Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
maintainer
#1 lore raw
Quoted 1 line · Rob Barnes

- Send patch using git-send-email via Google Mail Relay to prevent MTA line-wrapping (Tzung-Bi).

The patch can be applied via git am after the fixing. However, KUnit test emits errors after applying the patch:

$ ./tools/testing/kunit/kunit.py run \
        --arch=x86_64 \
        --kconfig_add CONFIG_CHROME_PLATFORMS=y \
        --kconfig_add CONFIG_CROS_EC=y \
        cros_ec*
Quoted 1 line
drivers/platform/chrome/cros_ec_proto_test.c +0 -0

...

Quoted 7 lines
drivers/platform/chrome/cros_ec_proto_test.c +7 -0
+static void cros_ec_proto_test_cmd_xfer_in_progress_payload_4bytes(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	u8 resp_data[4] = {0x11, 0x22, 0x33, 0x44};
+	struct {

drivers/platform/chrome/cros_ec_proto_test.c static void cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit *test) · line 1818

Quoted 10 lines
drivers/platform/chrome/cros_ec_proto_test.c +10 -0
+		struct cros_ec_command msg;
+		u8 data[4];
+	} buf;
+	struct cros_ec_command *msg = &buf.msg;
+	int ret;
+
+	memset(&buf, 0, sizeof(buf));
+	msg->version = 1;
+	msg->command = 0x1234;
+	msg->outsize = 2;

It needs to set ec_dev->max_request before it can send the payload. Given the test doesn't verify the output payload, just removing the assignment of msg->outsize and using default value 0 could be the simplest fix. Otherwise:

    cros_ec_proto_test: request of size 2 is too big (max: 0)
       # cros_ec_proto_test_cmd_xfer_in_progress_payload_4bytes:
       Expected ret == 4, but
           ret == -90 (0xffffffffffffffa6)

drivers/platform/chrome/cros_ec_proto_test.c

Quoted 1 line
drivers/platform/chrome/cros_ec_proto_test.c +1 -0
+	msg->insize = sizeof(buf.data);

Similar here, it needs to set ec_dev->max_response before it can receive payload (cros_ec_cmd_xfer() clamps it). Otherwise:

# cros_ec_proto_test_cmd_xfer_in_progress_payload_4bytes:
Expected msg->insize == sizeof(buf.data), but

       msg->insize == 0 (0x0)
       sizeof(buf.data) == 4 (0x4)

drivers/platform/chrome/cros_ec_proto_test.c static void cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit *test) · line 1851

Quoted 5 lines
drivers/platform/chrome/cros_ec_proto_test.c +5 -0
+	ret = cros_ec_cmd_xfer(ec_dev, msg);
+	KUNIT_EXPECT_EQ(test, ret, 4);
+	KUNIT_EXPECT_EQ(test, msg->result, EC_RES_SUCCESS);
+	KUNIT_EXPECT_EQ(test, msg->command, 0x1234);
+	KUNIT_EXPECT_EQ(test, msg->outsize, 2);

Following the above suggestion, this needs to be dropped.

Quoted 12 lines
drivers/platform/chrome/cros_ec_proto_test.c +12 -0
+static void cros_ec_proto_test_cmd_xfer_in_progress_payload_gt4bytes(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	u8 resp_data[16];
+	struct {
+		struct cros_ec_command msg;
+		u8 data[16];
+	} buf;
+	struct cros_ec_command *msg = &buf.msg;
+	int ret, i;

drivers/platform/chrome/cros_ec_proto_test.c static void cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit *test) · line 1897

Quoted 10 lines
drivers/platform/chrome/cros_ec_proto_test.c +10 -0
+
+	for (i = 0; i < sizeof(resp_data); ++i)
+		resp_data[i] = (u8)(i + 1);
+
+	memset(&buf, 0, sizeof(buf));
+	msg->version = 0;
+	msg->command = 0x5678;
+	msg->insize = sizeof(buf.data);
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;

Same here:

    ec_dev->max_response = sizeof(buf.data);