platform/chrome: skip old cros_ec responses
Subsystems (2)
PATCH 1/2 platform/chrome: skip old cros_ec responses
From: Jett Rink <[email protected]>
The ISHTP layer can give us old responses that we already gave up on. We do not want to interpret these old responses as the current response we are waiting for.
--- drivers/platform/chrome/cros_ec_ishtp.c | 32 ++++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/platform/chrome/cros_ec_ishtp.c +23 -9
@@ -48,7 +48,8 @@ static const guid_t cros_ish_guid =struct header {u8 channel;u8 status; -u8 reserved[2 ]; +u8 id; +u8 reserved; } __packed;struct cros_ish_out_msg {
@@ -90,6 +91,7 @@ static DECLARE_RWSEM(init_lock);* data exceeds this value, we log an error. * @size: Actual size of data received from firmware. * @error: 0 for success, negative error code for a failure in process_recv(). +* @expected_id: Expected id for response that we are waiting on. * @received: Set to true on receiving a valid firmware response to host command * @wait_queue: Wait queue for host to wait for firmware response. */
@@ -98,6 +100,7 @@ struct response_info {size_t max_size;size_t size;int error; +u8 expected_id;bool received;wait_queue_head_t wait_queue; };
@@ -162,6 +165,7 @@ static int ish_send(struct ishtp_cl_data *client_data, u8 *out_msg, size_t out_size, u8 *in_msg, size_t in_size) { + static u8 current_id;int rv;struct header *out_hdr = (struct header *)out_msg;struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
@@ -174,8 +178,11 @@ static int ish_send(struct ishtp_cl_data *client_data, client_data->response.data = in_msg; client_data->response.max_size = in_size; client_data->response.error =0 ; + client_data->response.expected_id = ++current_id; client_data->response.received = false; + out_hdr->id = client_data->response.expected_id; + rv =ishtp_cl_send (cros_ish_cl, out_msg, out_size);if (rv) { dev_err(cl_data_to_dev(client_data),
@@ -249,17 +256,23 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,switch (in_msg->hdr.channel) {case CROS_EC_COMMAND : -/* Sanity check */ -if (!client_data->response.data) { +if (client_data->response.received) {dev_err (dev, -"Receiving buffer is null. Should be allocated by calling function\n" ); - client_data->response.error = -EINVAL ; - goto error_wake_up; +"Previous firmware message not yet processed\n" ); + goto end_error; } -if (client_data->response.received) { +if (client_data->response.expected_id != in_msg->hdr.id) {dev_err (dev, -"Previous firmware message not yet processed\n" ); +"Dropping old response id %d\n" , + in_msg->hdr.id); + goto end_error; + } + +/* Sanity check */ +if (!client_data->response.data) { +dev_err (dev, +"Receiving buffer is null. Should be allocated by calling function\n" ); client_data->response.error = -EINVAL ; goto error_wake_up; }
@@ -289,9 +302,10 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,memcpy (client_data->response.data, rb_in_proc->buffer.data, data_len); +error_wake_up:/* Set flag before waking up the caller */ client_data->response.received = true; -error_wake_up: +/* Wake the calling thread */ wake_up_interruptible (&client_data->response.wait_queue);
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
PATCH 2/2 platform/chrome: free ishtp buffer before sending event
From: Jett Rink <[email protected]>
Free the buffer as soon as we can to ensure we are using the least amount of buffers as possible.
--- drivers/platform/chrome/cros_ec_ishtp.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/platform/chrome/cros_ec_ishtp.c +10 -3
@@ -303,6 +303,10 @@ static void process_recv(struct ishtp_cl *cros_ish_cl, rb_in_proc->buffer.data, data_len); error_wake_up: +/* Free the buffer since we copied data or didn't need it */ +ishtp_cl_io_rb_recycle (rb_in_proc); + rb_in_proc =NULL ; +/* Set flag before waking up the caller */ client_data->response.received = true;
@@ -312,12 +316,14 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,break ;case CROS_MKBP_EVENT : +/* Free the buffer. This is just an event without data */ +ishtp_cl_io_rb_recycle (rb_in_proc); + rb_in_proc =NULL ;/* * Set timestamp from beginning of function since we actually * got an incoming MKBP event */ client_data->ec_dev->last_event_time = timestamp; -/* The event system doesn't send any data in buffer */ schedule_work (&client_data->work_ec_evt);break ;
@@ -327,8 +333,9 @@ static void process_recv(struct ishtp_cl *cros_ish_cl, } end_error: -/* Free the buffer */ -ishtp_cl_io_rb_recycle (rb_in_proc); +/* Free the buffer if we already haven't */ +if (rb_in_proc) +ishtp_cl_io_rb_recycle (rb_in_proc);up_read (&init_lock); }
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Hi Mathew,
Thanks for your patch.
Quoted 6 lines · Mathew King
From: Jett Rink <[email protected]>
The ISHTP layer can give us old responses that we already gave up on. We do not want to interpret these old responses as the current response we are waiting for.
Looking at the code and with the above explanation I am not sure I get what is doing this patch, could you explain a bit more, thanks.
Quoted 45 lines
Signed-off-by Jett Rink <[email protected]>Signed-off-by Mathew King <[email protected]>--- drivers/platform/chrome/cros_ec_ishtp.c | 32 ++++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-)drivers/platform/chrome/cros_ec_ishtp.c +5
-1@@ -48,7 +48,8 @@ static const guid_t cros_ish_guid =struct header {u8 channel;u8 status; -u8 reserved[2 ]; +u8 id; +u8 reserved; } __packed;struct cros_ish_out_msg {@@ -90,6 +91,7 @@ static DECLARE_RWSEM(init_lock);* data exceeds this value, we log an error. * @size: Actual size of data received from firmware. * @error: 0 for success, negative error code for a failure in process_recv(). +* @expected_id: Expected id for response that we are waiting on. * @received: Set to true on receiving a valid firmware response to host command * @wait_queue: Wait queue for host to wait for firmware response. */ @@ -98,6 +100,7 @@ struct response_info {size_t max_size;size_t size;int error; +u8 expected_id;bool received;wait_queue_head_t wait_queue; };@@ -162,6 +165,7 @@ static int ish_send(struct ishtp_cl_data *client_data, u8 *out_msg, size_t out_size, u8 *in_msg, size_t in_size) { + static u8 current_id;int rv;struct header *out_hdr = (struct header *)out_msg;struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
drivers/platform/chrome/cros_ec_ishtp.c static int ish_send(struct ishtp_cl_data *client_data, · line 181
Quoted 4 lines
drivers/platform/chrome/cros_ec_ishtp.c +1
-0@@ -174,8 +178,11 @@ static int ish_send(struct ishtp_cl_data *client_data, client_data->response.data = in_msg; client_data->response.max_size = in_size; client_data->response.error =0 ; + client_data->response.expected_id = ++current_id;
So on every ish_send call this variable is increased in a range 1 to 255 first, then overflows and goes from 0 to 255. Is this what you want to do?
Quoted 14 lines
drivers/platform/chrome/cros_ec_ishtp.c +3
-2client_data->response.received = false; + out_hdr->id = client_data->response.expected_id; + rv =ishtp_cl_send (cros_ish_cl, out_msg, out_size);if (rv) { dev_err(cl_data_to_dev(client_data),@@ -249,17 +256,23 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,switch (in_msg->hdr.channel) {case CROS_EC_COMMAND : -/* Sanity check */ -if (!client_data->response.data) { +if (client_data->response.received) {
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 265
Quoted 10 lines
drivers/platform/chrome/cros_ec_ishtp.c +3
-4dev_err (dev, -"Receiving buffer is null. Should be allocated by calling function\n" ); - client_data->response.error = -EINVAL ; - goto error_wake_up; +"Previous firmware message not yet processed\n" ); + goto end_error; } -if (client_data->response.received) { +if (client_data->response.expected_id != in_msg->hdr.id) {
And here you compare that the response received matches with the message id. I assume the ISH is sending a sequential id on every message?
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 268
Quoted 4 lines
drivers/platform/chrome/cros_ec_ishtp.c +2
-1dev_err (dev, -"Previous firmware message not yet processed\n" ); +"Dropping old response id %d\n" , + in_msg->hdr.id);
How often this message appears?
Quoted 23 lines
drivers/platform/chrome/cros_ec_ishtp.c +9
-1+ goto end_error; + } + +/* Sanity check */ +if (!client_data->response.data) { +dev_err (dev, +"Receiving buffer is null. Should be allocated by calling function\n" ); client_data->response.error = -EINVAL ; goto error_wake_up; }@@ -289,9 +302,10 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,memcpy (client_data->response.data, rb_in_proc->buffer.data, data_len); +error_wake_up:/* Set flag before waking up the caller */ client_data->response.received = true; -error_wake_up: +/* Wake the calling thread */ wake_up_interruptible (&client_data->response.wait_queue);
Quoted 8 lines · Enric Balletbo i Serra
Hi Mathew,
Thanks for your patch.
Quoted 2 lines · Mathew King
From: Jett Rink <[email protected]>
Quoted 7 lines
Quoted 4 lines
The ISHTP layer can give us old responses that we already gave up on. We do not want to interpret these old responses as the current response we are waiting for.
Looking at the code and with the above explanation I am not sure I get what is doing this patch, could you explain a bit more, thanks.
The cros_ish should only have one request in flight at a time, we send the request and wait for the response from the ISH. If the ISH is too slow to respond we give up on that request and we can send a new request. The ISH may still send the response to the request that timed out and without this patch we would treat the old response as the response to the current command. This is a condition that should not normally happen but we have seen it with a bad ISH image. So we just add an Id to the request header which is copied into the response header when the ISH processes the message to ensure that response is for the current request.
Quoted 45 lines
Quoted 45 lines
Signed-off-by Jett Rink <[email protected]>Signed-off-by Mathew King <[email protected]>--- drivers/platform/chrome/cros_ec_ishtp.c | 32 ++++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-)drivers/platform/chrome/cros_ec_ishtp.c +5
-1@@ -48,7 +48,8 @@ static const guid_t cros_ish_guid =struct header {u8 channel;u8 status; -u8 reserved[2 ]; +u8 id; +u8 reserved; } __packed;struct cros_ish_out_msg {@@ -90,6 +91,7 @@ static DECLARE_RWSEM(init_lock);* data exceeds this value, we log an error. * @size: Actual size of data received from firmware. * @error: 0 for success, negative error code for a failure in process_recv(). +* @expected_id: Expected id for response that we are waiting on. * @received: Set to true on receiving a valid firmware response to host command * @wait_queue: Wait queue for host to wait for firmware response. */ @@ -98,6 +100,7 @@ struct response_info {size_t max_size;size_t size;int error; +u8 expected_id;bool received;wait_queue_head_t wait_queue; };@@ -162,6 +165,7 @@ static int ish_send(struct ishtp_cl_data *client_data, u8 *out_msg, size_t out_size, u8 *in_msg, size_t in_size) { + static u8 current_id;int rv;struct header *out_hdr = (struct header *)out_msg;struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
Quoted 8 lines
drivers/platform/chrome/cros_ec_ishtp.c static int ish_send(struct ishtp_cl_data *client_data, · line 181
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c +1
-0@@ -174,8 +178,11 @@ static int ish_send(struct ishtp_cl_data *client_data, client_data->response.data = in_msg; client_data->response.max_size = in_size; client_data->response.error =0 ; + client_data->response.expected_id = ++current_id;So on every
ish_sendcall this variable is increased in a range 1 to 255 first, then overflows and goes from 0 to 255. Is this what you want to do?
On our internal code review this was switched from a post increment to a pre increment so that the variable name made sense. The id itself doesn't really matter as long as it is changed every response. We could also call it next_id and do a post increment and then we would start at 0. Do you have preference?
Quoted 23 lines
Quoted 23 lines
drivers/platform/chrome/cros_ec_ishtp.c +5
-5client_data->response.received = false; + out_hdr->id = client_data->response.expected_id; + rv =ishtp_cl_send (cros_ish_cl, out_msg, out_size);if (rv) { dev_err(cl_data_to_dev(client_data),@@ -249,17 +256,23 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,switch (in_msg->hdr.channel) {case CROS_EC_COMMAND : -/* Sanity check */ -if (!client_data->response.data) { +if (client_data->response.received) {dev_err (dev, -"Receiving buffer is null. Should be allocated by calling function\n" ); - client_data->response.error = -EINVAL ; - goto error_wake_up; +"Previous firmware message not yet processed\n" ); + goto end_error; }
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 265
Quoted 2 lines
drivers/platform/chrome/cros_ec_ishtp.c +1
-1-if (client_data->response.received) { +if (client_data->response.expected_id != in_msg->hdr.id) {And here you compare that the response received matches with the message id. I assume the ISH is sending a sequential id on every message?
The ISH will copy the id from the request to the id of the response
Quoted 8 lines
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 268
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c +2
-1dev_err (dev, -"Previous firmware message not yet processed\n" ); +"Dropping old response id %d\n" , + in_msg->hdr.id);How often this message appears?
Since we have stabilized our ISH code it rarely happens if ever. If it does happen it indicates an error in the ISH code or the driver code but we still want to attempt to recover gracefully if possible.
Quoted 24 lines
Quoted 24 lines
drivers/platform/chrome/cros_ec_ishtp.c +9
-1+ goto end_error; + } + +/* Sanity check */ +if (!client_data->response.data) { +dev_err (dev, +"Receiving buffer is null. Should be allocated by calling function\n" ); client_data->response.error = -EINVAL ; goto error_wake_up; }@@ -289,9 +302,10 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,memcpy (client_data->response.data, rb_in_proc->buffer.data, data_len); +error_wake_up:/* Set flag before waking up the caller */ client_data->response.received = true; -error_wake_up: +/* Wake the calling thread */ wake_up_interruptible (&client_data->response.wait_queue);
Hi Mat,
Quoted 18 lines · Mat King
Quoted 8 lines · Enric Balletbo i Serra
Hi Mathew,
Thanks for your patch.
Quoted 2 lines · Mathew King
From: Jett Rink <[email protected]>
Quoted 7 lines
Quoted 4 lines
The ISHTP layer can give us old responses that we already gave up on. We do not want to interpret these old responses as the current response we are waiting for.
Looking at the code and with the above explanation I am not sure I get what is doing this patch, could you explain a bit more, thanks.
Quoted 11 lines
The
cros_ishshould only have one request in flight at a time, we send the request and wait for the response from the ISH. If the ISH is too slow to respond we give up on that request and we can send a new request. The ISH may still send the response to the request that timed out and without this patch we would treat the old response as the response to the current command. This is a condition that should not normally happen but we have seen it with a bad ISH image. So we just add an Id to the request header which is copied into the response header when the ISH processes the message to ensure that response is for the current request.
Could you include that in the commit message. Thanks.
Quoted 54 lines
Quoted 45 lines
Quoted 45 lines
Signed-off-by Jett Rink <[email protected]>Signed-off-by Mathew King <[email protected]>--- drivers/platform/chrome/cros_ec_ishtp.c | 32 ++++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-)drivers/platform/chrome/cros_ec_ishtp.c +5
-1@@ -48,7 +48,8 @@ static const guid_t cros_ish_guid =struct header {u8 channel;u8 status; -u8 reserved[2 ]; +u8 id; +u8 reserved; } __packed;struct cros_ish_out_msg {@@ -90,6 +91,7 @@ static DECLARE_RWSEM(init_lock);* data exceeds this value, we log an error. * @size: Actual size of data received from firmware. * @error: 0 for success, negative error code for a failure in process_recv(). +* @expected_id: Expected id for response that we are waiting on. * @received: Set to true on receiving a valid firmware response to host command * @wait_queue: Wait queue for host to wait for firmware response. */ @@ -98,6 +100,7 @@ struct response_info {size_t max_size;size_t size;int error; +u8 expected_id;bool received;wait_queue_head_t wait_queue; };@@ -162,6 +165,7 @@ static int ish_send(struct ishtp_cl_data *client_data, u8 *out_msg, size_t out_size, u8 *in_msg, size_t in_size) { + static u8 current_id;int rv;struct header *out_hdr = (struct header *)out_msg;struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;Quoted 8 lines
drivers/platform/chrome/cros_ec_ishtp.c static int ish_send(struct ishtp_cl_data *client_data, · line 181
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c +1
-0@@ -174,8 +178,11 @@ static int ish_send(struct ishtp_cl_data *client_data, client_data->response.data = in_msg; client_data->response.max_size = in_size; client_data->response.error =0 ; + client_data->response.expected_id = ++current_id;So on every
ish_sendcall this variable is increased in a range 1 to 255 first, then overflows and goes from 0 to 255. Is this what you want to do?
Quoted 6 lines
On our internal code review this was switched from a post increment to a pre increment so that the variable name made sense. The id itself doesn't really matter as long as it is changed every response. We could also call it
next_idand do a post increment and then we would start at 0. Do you have preference?
So is like a token (actually a ring of tokens) to make sure the response matches with the question.
if (response.token != in_msg->hdr.token)
ignore the response
This id dancing (expected_id, current_id, id) confused me a bit. IMO the code will be more readable simply with token or id for both, the response and the question, and have a next_token/id. But I don't have a strong preference, so I will let you the final decision.
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/platform/chrome/cros_ec_ishtp.c +5
-5client_data->response.received = false; + out_hdr->id = client_data->response.expected_id; + rv =ishtp_cl_send (cros_ish_cl, out_msg, out_size);if (rv) { dev_err(cl_data_to_dev(client_data),@@ -249,17 +256,23 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,switch (in_msg->hdr.channel) {case CROS_EC_COMMAND : -/* Sanity check */ -if (!client_data->response.data) { +if (client_data->response.received) {dev_err (dev, -"Receiving buffer is null. Should be allocated by calling function\n" ); - client_data->response.error = -EINVAL ; - goto error_wake_up; +"Previous firmware message not yet processed\n" ); + goto end_error; }
Quoted 8 lines
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 265
Quoted 2 lines
drivers/platform/chrome/cros_ec_ishtp.c +1
-1-if (client_data->response.received) { +if (client_data->response.expected_id != in_msg->hdr.id) {And here you compare that the response received matches with the message id. I assume the ISH is sending a sequential id on every message?
The ISH will copy the id from the request to the id of the response
Ok.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
drivers/platform/chrome/cros_ec_ishtp.c +2
-1dev_err (dev, -"Previous firmware message not yet processed\n" ); +"Dropping old response id %d\n" , + in_msg->hdr.id);
Quoted 6 lines
Quoted 1 line
How often this message appears?
Since we have stabilized our ISH code it rarely happens if ever. If it does happen it indicates an error in the ISH code or the driver code but we still want to attempt to recover gracefully if possible.
Still, I am wondering if is a good idea to have this message rate limited (dev_err_ratelimited) If for some reason the ISH code is bad or losts sync the kernel can be flooded by this message and being annoying.
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/platform/chrome/cros_ec_ishtp.c +7
-0+ goto end_error; + } + +/* Sanity check */ +if (!client_data->response.data) { +dev_err (dev, +"Receiving buffer is null. Should be allocated by calling function\n" ); client_data->response.error = -EINVAL ; goto error_wake_up; }@@ -289,9 +302,10 @@ static void process_recv(struct ishtp_cl *cros_ish_cl,memcpy (client_data->response.data, rb_in_proc->buffer.data, data_len);
Quoted 9 lines
Quoted 9 lines
drivers/platform/chrome/cros_ec_ishtp.c static void process_recv(struct ishtp_cl *cros_ish_cl, · line 311
Quoted 9 lines
drivers/platform/chrome/cros_ec_ishtp.c +2
-1+error_wake_up:/* Set flag before waking up the caller */ client_data->response.received = true; -error_wake_up: +/* Wake the calling thread */ wake_up_interruptible (&client_data->response.wait_queue);
Please resend the patch modifying the commit message and using
'platform/chrome: cros_ec_ishtp: ...'
as title prefix.
Thanks,
Enric