platform/chrome: cros_ec_proto: Fix deferred response
Subsystems (2)
PATCH v5 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.
---
v5:
- In KUnit tests, set ec_dev->max_response before transfer and drop msg->outsize in payload_4bytes test (Tzung-Bi).
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
@@ -138,7 +138,8 @@ static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_coreturn 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_coint 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.966.g6673acef38-goog
Tzung-Bi Shih
maintainerQuoted 10 lines · Rob Barnes
When
cros_ec_wait_until_complete()succeeds after anEC_RES_IN_PROGRESSstatus, it previously returned the 4-byte transfer size ofEC_CMD_GET_COMMS_STATUS. For 0-byte payload commands (such asEC_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 issuingEC_CMD_RESEND_RESPONSE, leaving response buffers uninitialized.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git for-next
[1/1] platform/chrome: cros_ec_proto: Fix deferred response
commit: c55749415623a58eba3eb527da023d29b43fbf5d
Thanks!