[RFC net-next 10/15] net: cpc: make disconnect blocking

Damien Riégel posted 15 patches 7 months, 1 week ago
[RFC net-next 10/15] net: cpc: make disconnect blocking
Posted by Damien Riégel 7 months, 1 week ago
In order to make life easier for consumer drivers, make
cpc_endpoint_disconnect() blocking. Once the call returns, it guarantees
that endpoint's rx callback won't be called anymore, making driver's
teardown simpler to implement.

Signed-off-by: Damien Riégel <damien.riegel@silabs.com>
---
 drivers/net/cpc/cpc.h       |  1 +
 drivers/net/cpc/endpoint.c  | 19 ++++++++++++++++++-
 drivers/net/cpc/interface.c |  4 ++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/net/cpc/cpc.h b/drivers/net/cpc/cpc.h
index 34ee519d907..8a761856deb 100644
--- a/drivers/net/cpc/cpc.h
+++ b/drivers/net/cpc/cpc.h
@@ -21,6 +21,7 @@ extern const struct bus_type cpc_bus;
 /* CPC endpoint flags */
 enum {
 	CPC_ENDPOINT_UP,	/* Connection is established with remote counterpart. */
+	CPC_ENDPOINT_RECEIVING,	/* Interface RX work is processing a frame for this endpoint. */
 };
 
 /**
diff --git a/drivers/net/cpc/endpoint.c b/drivers/net/cpc/endpoint.c
index 7e2f623fb8e..f953e4cb7ab 100644
--- a/drivers/net/cpc/endpoint.c
+++ b/drivers/net/cpc/endpoint.c
@@ -260,8 +260,25 @@ void __cpc_endpoint_disconnect(struct cpc_endpoint *ep, bool send_rst)
 
 	cpc_interface_remove_rx_endpoint(ep);
 
-	if (send_rst)
+	if (send_rst) {
+		/*
+		 * It makes sense to wait on the RECEIVING bit only when send_rst is true as this
+		 * means the operation was initiated by the user and can happen concurrently with
+		 * the RX work function. If a RST is received from the remote and
+		 * __cpc_endpoint_disconnect from the RX work function, then it's safe to assume
+		 * that this frame won't trigger a call to ep->ops->rx function.
+		 */
+		int err;
+
+		err = wait_on_bit_timeout(&ep->flags,
+					  CPC_ENDPOINT_RECEIVING,
+					  TASK_INTERRUPTIBLE,
+					  msecs_to_jiffies(1000));
+		if (!err)
+			dev_warn(&ep->dev, "Timeout when disconnecting.\n");
+
 		cpc_protocol_send_rst(ep->intf, ep->id);
+	}
 }
 
 /**
diff --git a/drivers/net/cpc/interface.c b/drivers/net/cpc/interface.c
index 30e7976355c..13b52cdc357 100644
--- a/drivers/net/cpc/interface.c
+++ b/drivers/net/cpc/interface.c
@@ -36,6 +36,8 @@ static void cpc_interface_rx_work(struct work_struct *work)
 			continue;
 		}
 
+		set_bit(CPC_ENDPOINT_RECEIVING, &ep->flags);
+
 		switch (type) {
 		case CPC_FRAME_TYPE_DATA:
 			cpc_protocol_on_data(ep, skb);
@@ -50,6 +52,8 @@ static void cpc_interface_rx_work(struct work_struct *work)
 			break;
 		}
 
+		clear_and_wake_up_bit(CPC_ENDPOINT_RECEIVING, &ep->flags);
+
 		cpc_endpoint_put(ep);
 	}
 }
-- 
2.49.0