Skip to contentSkip to conversation

platform/chrome: cros_ec_rpmsg: Fix race with host event.

PATCH In review Pi-Hsun Shih 2 participants 8 messages Feb 14, 2020 – Mar 3, 2020

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

PATCH 1/1 platform/chrome: cros_ec_rpmsg: Fix race with host event.

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_rpmsg.c +15 -1
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
 	struct completion xfer_ack;
 	struct work_struct host_event_work;
 	struct rpmsg_endpoint *ept;
+	bool has_pending_host_event;
+	bool probe_done;
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		memcpy(ec_dev->din, resp->data, len);
 		complete(&ec_rpmsg->xfer_ack);
 	} else if (resp->type == HOST_EVENT_MARK) {
-		schedule_work(&ec_rpmsg->host_event_work);
+		/*
+		 * If the host event is sent before cros_ec_register is
+		 * finished, queue the host event.
+		 */
+		if (ec_rpmsg->probe_done)
+			schedule_work(&ec_rpmsg->host_event_work);
+		else
+			ec_rpmsg->has_pending_host_event = true;
 	} else {
 		dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
 			 resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
 		return ret;
 	}
 
+	ec_rpmsg->probe_done = true;
+
+	if (ec_rpmsg->has_pending_host_event)
+		schedule_work(&ec_rpmsg->host_event_work);
+
 	return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#1 lore raw

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

I don't have the hardware to test but, can't we call first cros_ec_register and then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers itself sounds weird to me.

Thanks,
Enric

Quoted 54 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_rpmsg.c +15 -1
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
 	struct completion xfer_ack;
 	struct work_struct host_event_work;
 	struct rpmsg_endpoint *ept;
+	bool has_pending_host_event;
+	bool probe_done;
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		memcpy(ec_dev->din, resp->data, len);
 		complete(&ec_rpmsg->xfer_ack);
 	} else if (resp->type == HOST_EVENT_MARK) {
-		schedule_work(&ec_rpmsg->host_event_work);
+		/*
+		 * If the host event is sent before cros_ec_register is
+		 * finished, queue the host event.
+		 */
+		if (ec_rpmsg->probe_done)
+			schedule_work(&ec_rpmsg->host_event_work);
+		else
+			ec_rpmsg->has_pending_host_event = true;
 	} else {
 		dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
 			 resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
 		return ret;
 	}
 
+	ec_rpmsg->probe_done = true;
+
+	if (ec_rpmsg->has_pending_host_event)
+		schedule_work(&ec_rpmsg->host_event_work);
+
 	return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#2 lore raw

Hi Enric,

Quoted 10 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Quoted 8 lines

I don't have the hardware to test but, can't we call first cros_ec_register and then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers itself sounds weird to me.

Thanks,
Enric

Since cros_ec_register calls cros_ec_query_all, which sends message to remoteproc using cros_ec_pkt_xfer_rpmsg (to query protocol version), the ec_rpmsg->ept need to be ready before calling cros_ec_register.

Quoted 55 lines
Quoted 55 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_rpmsg.c +15 -1
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
      struct completion xfer_ack;
      struct work_struct host_event_work;
      struct rpmsg_endpoint *ept;
+     bool has_pending_host_event;
+     bool probe_done;
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
              memcpy(ec_dev->din, resp->data, len);
              complete(&ec_rpmsg->xfer_ack);
      } else if (resp->type == HOST_EVENT_MARK) {
-             schedule_work(&ec_rpmsg->host_event_work);
+             /*
+              * If the host event is sent before cros_ec_register is
+              * finished, queue the host event.
+              */
+             if (ec_rpmsg->probe_done)
+                     schedule_work(&ec_rpmsg->host_event_work);
+             else
+                     ec_rpmsg->has_pending_host_event = true;
      } else {
              dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
                       resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
              return ret;
      }
 
+     ec_rpmsg->probe_done = true;
+
+     if (ec_rpmsg->has_pending_host_event)
+             schedule_work(&ec_rpmsg->host_event_work);
+
      return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#3 lore raw

Dear remoteproc experts,

cc'ing you for if we can have your feedback on this change.

Thanks Pi-Hsun, for your quick answer, makes sense but I'm still feeling that I miss something (probably because I'm not a remoteproc expert), so I added the Remoteproc people for if they can comment this patch. We have time as we're in rc2 only, so I'd like to wait a bit in case they can take a look.

If no answer is received I'll take a second look and apply the patch.

Thanks,
Enric

Quoted 82 lines · Pi-Hsun Shih

Hi Enric,

Quoted 10 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Quoted 8 lines

I don't have the hardware to test but, can't we call first cros_ec_register and then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers itself sounds weird to me.

Thanks,
Enric

Since cros_ec_register calls cros_ec_query_all, which sends message to remoteproc using cros_ec_pkt_xfer_rpmsg (to query protocol version), the ec_rpmsg->ept need to be ready before calling cros_ec_register.

Quoted 55 lines
Quoted 55 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_rpmsg.c +15 -1
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
      struct completion xfer_ack;
      struct work_struct host_event_work;
      struct rpmsg_endpoint *ept;
+     bool has_pending_host_event;
+     bool probe_done;
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
              memcpy(ec_dev->din, resp->data, len);
              complete(&ec_rpmsg->xfer_ack);
      } else if (resp->type == HOST_EVENT_MARK) {
-             schedule_work(&ec_rpmsg->host_event_work);
+             /*
+              * If the host event is sent before cros_ec_register is
+              * finished, queue the host event.
+              */
+             if (ec_rpmsg->probe_done)
+                     schedule_work(&ec_rpmsg->host_event_work);
+             else
+                     ec_rpmsg->has_pending_host_event = true;
      } else {
              dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
                       resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
              return ret;
      }
 
+     ec_rpmsg->probe_done = true;
+
+     if (ec_rpmsg->has_pending_host_event)
+             schedule_work(&ec_rpmsg->host_event_work);
+
      return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#4 lore raw

Hi Pi-Hsun,

Quoted 11 lines · Enric Balletbo i Serra

Dear remoteproc experts,

cc'ing you for if we can have your feedback on this change.

Thanks Pi-Hsun, for your quick answer, makes sense but I'm still feeling that I miss something (probably because I'm not a remoteproc expert), so I added the Remoteproc people for if they can comment this patch. We have time as we're in rc2 only, so I'd like to wait a bit in case they can take a look.

If no answer is received I'll take a second look and apply the patch.

I'll pick this patch, just I want to request a minor change.

Quoted 42 lines

Thanks,
Enric

Quoted 38 lines · Pi-Hsun Shih

Hi Enric,

Quoted 10 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Quoted 8 lines

I don't have the hardware to test but, can't we call first cros_ec_register and then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers itself sounds weird to me.

Thanks,
Enric

Since cros_ec_register calls cros_ec_query_all, which sends message to remoteproc using cros_ec_pkt_xfer_rpmsg (to query protocol version), the ec_rpmsg->ept need to be ready before calling cros_ec_register.

Quoted 11 lines
Quoted 11 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/platform/chrome/cros_ec_rpmsg.c struct cros_ec_rpmsg { · line 48

Quoted 10 lines
drivers/platform/chrome/cros_ec_rpmsg.c +2 -0
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
      struct completion xfer_ack;
      struct work_struct host_event_work;
      struct rpmsg_endpoint *ept;
+     bool has_pending_host_event;
+     bool probe_done;

Could you try if just calling driver_probe_done() when needed works, so we don't need to add a new boolean flag for this?

Thanks,
Enric

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/platform/chrome/cros_ec_rpmsg.c +13 -1
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
              memcpy(ec_dev->din, resp->data, len);
              complete(&ec_rpmsg->xfer_ack);
      } else if (resp->type == HOST_EVENT_MARK) {
-             schedule_work(&ec_rpmsg->host_event_work);
+             /*
+              * If the host event is sent before cros_ec_register is
+              * finished, queue the host event.
+              */
+             if (ec_rpmsg->probe_done)
+                     schedule_work(&ec_rpmsg->host_event_work);
+             else
+                     ec_rpmsg->has_pending_host_event = true;
      } else {
              dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
                       resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
              return ret;
      }
 
+     ec_rpmsg->probe_done = true;
+
+     if (ec_rpmsg->has_pending_host_event)
+             schedule_work(&ec_rpmsg->host_event_work);
+
      return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#5 lore raw

Hi Enric,

Quoted 68 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 5 lines · Enric Balletbo i Serra

Dear remoteproc experts,

cc'ing you for if we can have your feedback on this change.

Thanks Pi-Hsun, for your quick answer, makes sense but I'm still feeling

that I

Quoted 1 line

miss something (probably because I'm not a remoteproc expert), so I

added the

Quoted 1 line

Remoteproc people for if they can comment this patch. We have time as

we're in

Quoted 4 lines

rc2 only, so I'd like to wait a bit in case they can take a look.

If no answer is received I'll take a second look and apply the patch.

I'll pick this patch, just I want to request a minor change.

Quoted 15 lines

Thanks,
Enric

Quoted 11 lines · Pi-Hsun Shih

Hi Enric,

Quoted 7 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 3 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host

event

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

handler to use cros_ec_device that are not initialized properly yet.

I don't have the hardware to test but, can't we call first

cros_ec_register and

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers

itself

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

sounds weird to me.

Thanks,
Enric

Since cros_ec_register calls cros_ec_query_all, which sends message to remoteproc using cros_ec_pkt_xfer_rpmsg (to query protocol version), the ec_rpmsg->ept need to be ready before calling cros_ec_register.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with

host command when probe failed.")

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/chrome/cros_ec_rpmsg.c

Quoted 15 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/platform/chrome/cros_ec_rpmsg.c struct cros_ec_rpmsg { · line 48

Quoted 9 lines

index dbc3f5523b83..7e8629e3db74 100644

drivers/platform/chrome/cros_ec_rpmsg.c +2 -0
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
      struct completion xfer_ack;
      struct work_struct host_event_work;
      struct rpmsg_endpoint *ept;
+     bool has_pending_host_event;
+     bool probe_done;

Could you try if just calling driver_probe_done() when needed works, so we don't
need to add a new boolean flag for this?

Changing from "if (ec_rpmsg->probe_done)" to "if (driver_probe_done() == 0)" works in my testing.

But since driver_probe_done() returns 0 after all driver probes are done, not after the probe of cros_ec_rpmsg driver, I think it's possible that we got a host event after the cros_ec_rpmsg driver probe is done (ec_rpmsg->probe_done is true), but before all driver probe done (driver_probe_done() is still -EBUSY). In this case the host event would be lost since we would set the has_pending_host_event flag but no one would be processing it.

Quoted 42 lines

Thanks,
Enric

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/platform/chrome/cros_ec_rpmsg.c static int cros_ec_rpmsg_callback(struct

Quoted 4 lines
drivers/platform/chrome/cros_ec_rpmsg.c +0 -0
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct

rpmsg_device *rpdev, void *data,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/platform/chrome/cros_ec_rpmsg.c +2 -1
              memcpy(ec_dev->din, resp->data, len);
              complete(&ec_rpmsg->xfer_ack);
      } else if (resp->type == HOST_EVENT_MARK) {
-             schedule_work(&ec_rpmsg->host_event_work);
+             /*
+              * If the host event is sent before cros_ec_register is
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/platform/chrome/cros_ec_rpmsg.c static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,

Quoted 8 lines
drivers/platform/chrome/cros_ec_rpmsg.c +6 -0
+              * finished, queue the host event.
+              */
+             if (ec_rpmsg->probe_done)
+                     schedule_work(&ec_rpmsg->host_event_work);
+             else
+                     ec_rpmsg->has_pending_host_event = true;
      } else {
              dev_warn(ec_dev->dev, "rpmsg received invalid type =

%d",

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/platform/chrome/cros_ec_rpmsg.c static int cros_ec_rpmsg_probe(struct

Quoted 2 lines
drivers/platform/chrome/cros_ec_rpmsg.c +0 -0
                       resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct

rpmsg_device *rpdev)

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/platform/chrome/cros_ec_rpmsg.c +5 -0
              return ret;
      }
 
+     ec_rpmsg->probe_done = true;
+
+     if (ec_rpmsg->has_pending_host_event)
+             schedule_work(&ec_rpmsg->host_event_work);
+
      return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#6 lore raw
In reply to Enric Balletbo i Serra

(Resending since I forgot to use plain text mode in the previous mail, and got blocked by mailing lists. Sorry for the duplicate email.)

Hi Enric,

Quoted 60 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 11 lines · Enric Balletbo i Serra

Dear remoteproc experts,

cc'ing you for if we can have your feedback on this change.

Thanks Pi-Hsun, for your quick answer, makes sense but I'm still feeling that I miss something (probably because I'm not a remoteproc expert), so I added the Remoteproc people for if they can comment this patch. We have time as we're in rc2 only, so I'd like to wait a bit in case they can take a look.

If no answer is received I'll take a second look and apply the patch.

I'll pick this patch, just I want to request a minor change.

Quoted 42 lines

Thanks,
Enric

Quoted 38 lines · Pi-Hsun Shih

Hi Enric,

Quoted 10 lines · Enric Balletbo i Serra

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Quoted 8 lines

I don't have the hardware to test but, can't we call first cros_ec_register and then cros_ec_rpmsg_create_ept?

Start receiving driver callbacks before finishing to probe the drivers itself sounds weird to me.

Thanks,
Enric

Since cros_ec_register calls cros_ec_query_all, which sends message to remoteproc using cros_ec_pkt_xfer_rpmsg (to query protocol version), the ec_rpmsg->ept need to be ready before calling cros_ec_register.

Quoted 11 lines
Quoted 11 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")
---
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/platform/chrome/cros_ec_rpmsg.c struct cros_ec_rpmsg { · line 48

Quoted 10 lines
drivers/platform/chrome/cros_ec_rpmsg.c +2 -0
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
      struct completion xfer_ack;
      struct work_struct host_event_work;
      struct rpmsg_endpoint *ept;
+     bool has_pending_host_event;
+     bool probe_done;

Could you try if just calling driver_probe_done() when needed works, so we don't need to add a new boolean flag for this?

Changing from "if (ec_rpmsg->probe_done)" to "if (driver_probe_done() == 0)" works in my testing.

But since driver_probe_done() returns 0 after all driver probes are done, not after the probe of cros_ec_rpmsg driver, I think it's possible that we got a host event after the cros_ec_rpmsg driver probe is done (ec_rpmsg->probe_done is true), but before all driver probe done (driver_probe_done() is still -EBUSY). In this case the host event would be lost since we would set the has_pending_host_event flag but no one would be processing it.

Quoted 38 lines

Thanks,
Enric

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/platform/chrome/cros_ec_rpmsg.c +13 -1
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
              memcpy(ec_dev->din, resp->data, len);
              complete(&ec_rpmsg->xfer_ack);
      } else if (resp->type == HOST_EVENT_MARK) {
-             schedule_work(&ec_rpmsg->host_event_work);
+             /*
+              * If the host event is sent before cros_ec_register is
+              * finished, queue the host event.
+              */
+             if (ec_rpmsg->probe_done)
+                     schedule_work(&ec_rpmsg->host_event_work);
+             else
+                     ec_rpmsg->has_pending_host_event = true;
      } else {
              dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
                       resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
              return ret;
      }
 
+     ec_rpmsg->probe_done = true;
+
+     if (ec_rpmsg->has_pending_host_event)
+             schedule_work(&ec_rpmsg->host_event_work);
+
      return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402

#7 lore raw
In reply to Pi-Hsun Shih

Hi Pi-Hsun,

Quoted 5 lines · Pi-Hsun Shih

Host event can be sent by remoteproc by any time, and cros_ec_rpmsg_callback would be called after cros_ec_rpmsg_create_ept. But the cros_ec_device is initialized after that, which cause host event handler to use cros_ec_device that are not initialized properly yet.

Quoted 7 lines

Fix this by don't schedule host event handler before cros_ec_register returns. Instead, remember that we have a pending host event, and schedule host event handler after cros_ec_register.

Fixes 71cddb7097e2 ("platform/chrome: cros_ec_rpmsg: Fix race with host command when probe failed.")

---

Applied for 5.7

Quoted 47 lines
 drivers/platform/chrome/cros_ec_rpmsg.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/platform/chrome/cros_ec_rpmsg.c +15 -1
index dbc3f5523b83..7e8629e3db74 100644
@@ -44,6 +44,8 @@ struct cros_ec_rpmsg {
 	struct completion xfer_ack;
 	struct work_struct host_event_work;
 	struct rpmsg_endpoint *ept;
+	bool has_pending_host_event;
+	bool probe_done;
 };
 
 /**
@@ -177,7 +179,14 @@ static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		memcpy(ec_dev->din, resp->data, len);
 		complete(&ec_rpmsg->xfer_ack);
 	} else if (resp->type == HOST_EVENT_MARK) {
-		schedule_work(&ec_rpmsg->host_event_work);
+		/*
+		 * If the host event is sent before cros_ec_register is
+		 * finished, queue the host event.
+		 */
+		if (ec_rpmsg->probe_done)
+			schedule_work(&ec_rpmsg->host_event_work);
+		else
+			ec_rpmsg->has_pending_host_event = true;
 	} else {
 		dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
 			 resp->type);
@@ -240,6 +249,11 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
 		return ret;
 	}
 
+	ec_rpmsg->probe_done = true;
+
+	if (ec_rpmsg->has_pending_host_event)
+		schedule_work(&ec_rpmsg->host_event_work);
+
 	return 0;
 }
 

base-commit: b19e8c68470385dd2c5440876591fddb02c8c402