[PATCH v6 1/9] greybus: operation: keep request active across message_send
From: Ayush Singh
Date: Fri Dec 04 2026 - 05:31:51 EST
gb_operation_request_send() takes a single active count, and completion
drops it. This would be fine normally, but in case of cancellation (in
gb_softsvc context, this would happen in module_removed), completion
(gb_operation_work), can drop the active count, while message_send is
running.
The core does call message_cancel before dropping the active count,
but in most normal circumstances, cancelling an operation that is
already writing over a protocol (UART) is not really possible. So we
are only left with making the core wait for the said write to complete
instead of making active count 0 while message_send is in progress.
The patch takes a second reference and active count before the send
and drop them once it returns. If the connection starts tearing down
between the two calls, return 0 and let the cancel complete the
operation.
Since it took at least me a while to truely understand what the initial
sashiko-bot report meant, here is an example:
Thread A Thread B + worker active
------------------------ ------------------------------ ------
1 get_active() 1
2 gb_message_send()
3 GbNode::write() ...
4 ... blocked state = DISCONNECTING
5 ... blocked gb_operation_cancel()
6 ... blocked result_set(-ESHUTDOWN)
7 ... blocked queue_work(completion)
8 ... blocked wait_event(!active) [sleeps]
9 ... blocked [worker] gb_operation_work()
10 ... blocked [worker] put_active() [:292] 0
11 ... blocked wait_event wakes, returns <- too early
12 ... blocked gb_module_del() returns
13 ... blocked [softsvc] Module::drop finishes
14 ... blocked serdev port closed, unbind done <- bad
15 write() returns
16 return to caller
This patch simply takes an extra active count when calling
gb_message_send. in core. It also treats both requests and response
the same, i.e. clear an active count when gb_message_sent() is
called. Earlier, this was only being done for responses.
Signed-off-by: Ayush Singh <ayush@xxxxxxxxxxxxxxx>
---
drivers/greybus/operation.c | 61 ++++++++++++++++++++++++++++++++-------------
1 file changed, 43 insertions(+), 18 deletions(-)
diff --git a/drivers/greybus/operation.c b/drivers/greybus/operation.c
index 7e12ffb2dd60..a9eada39ec79 100644
--- a/drivers/greybus/operation.c
+++ b/drivers/greybus/operation.c
@@ -37,42 +37,55 @@ static int gb_operation_response_send(struct gb_operation *operation,
* Increment operation active count and add to connection list unless the
* connection is going away.
*
- * Caller holds operation reference.
+ * Caller holds operation reference and connection->lock
*/
-static int gb_operation_get_active(struct gb_operation *operation)
+static int __gb_operation_get_active(struct gb_operation *operation, int count)
+ __must_hold(&operation->connection->lock)
{
struct gb_connection *connection = operation->connection;
- unsigned long flags;
- spin_lock_irqsave(&connection->lock, flags);
switch (connection->state) {
case GB_CONNECTION_STATE_ENABLED:
break;
case GB_CONNECTION_STATE_ENABLED_TX:
if (gb_operation_is_incoming(operation))
- goto err_unlock;
+ return -ENOTCONN;
break;
case GB_CONNECTION_STATE_DISCONNECTING:
if (!gb_operation_is_core(operation))
- goto err_unlock;
+ return -ENOTCONN;
break;
default:
- goto err_unlock;
+ return -ENOTCONN;
}
- if (operation->active++ == 0)
+ if (operation->active == 0)
list_add_tail(&operation->links, &connection->operations);
- trace_gb_operation_get_active(operation);
+ operation->active += count;
- spin_unlock_irqrestore(&connection->lock, flags);
+ trace_gb_operation_get_active(operation);
return 0;
+}
+
+/*
+ * Increment operation active count and add to connection list unless the
+ * connection is going away.
+ *
+ * Caller holds operation reference.
+ */
+static int gb_operation_get_active(struct gb_operation *operation)
+{
+ struct gb_connection *connection = operation->connection;
+ unsigned long flags;
+ int ret;
-err_unlock:
+ spin_lock_irqsave(&connection->lock, flags);
+ ret = __gb_operation_get_active(operation, 1);
spin_unlock_irqrestore(&connection->lock, flags);
- return -ENOTCONN;
+ return ret;
}
/* Caller holds operation reference. */
@@ -726,6 +739,7 @@ int gb_operation_request_send(struct gb_operation *operation,
struct gb_connection *connection = operation->connection;
struct gb_operation_msg_hdr *header;
unsigned int cycle;
+ unsigned long flags;
int ret;
if (gb_connection_is_offloaded(connection))
@@ -758,11 +772,17 @@ int gb_operation_request_send(struct gb_operation *operation,
gb_operation_result_set(operation, -EINPROGRESS);
/*
- * Get an extra reference on the operation. It'll be dropped when the
- * operation completes.
+ * Take two references and active counts. One is dropped when the
+ * operation completes, the other when the host driver is done with the
+ * request, so that cancelling the operation waits for both. Take both
+ * counts under one lock, so the operation can't be cancelled in between.
*/
gb_operation_get(operation);
- ret = gb_operation_get_active(operation);
+ gb_operation_get(operation);
+
+ spin_lock_irqsave(&connection->lock, flags);
+ ret = __gb_operation_get_active(operation, 2);
+ spin_unlock_irqrestore(&connection->lock, flags);
if (ret)
goto err_put;
@@ -779,8 +799,10 @@ int gb_operation_request_send(struct gb_operation *operation,
err_put_active:
gb_operation_put_active(operation);
+ gb_operation_put_active(operation);
err_put:
gb_operation_put(operation);
+ gb_operation_put(operation);
return ret;
}
@@ -880,6 +902,9 @@ void greybus_message_sent(struct gb_host_device *hd,
* reference to the operation. If an error occurred, report
* it.
*
+ * The host driver is done with the message, so drop the reference and
+ * active count taken when it was sent. If a response failed, report it.
+ *
* For requests, if there's no error and the operation in not
* unidirectional, there's nothing more to do until the response
* arrives. If an error occurred attempting to send it, or if the
@@ -892,15 +917,15 @@ void greybus_message_sent(struct gb_host_device *hd,
"%s: error sending response 0x%02x: %d\n",
connection->name, operation->type, status);
}
-
- gb_operation_put_active(operation);
- gb_operation_put(operation);
} else if (status || gb_operation_is_unidirectional(operation)) {
if (gb_operation_result_set(operation, status)) {
queue_work(gb_operation_completion_wq,
&operation->work);
}
}
+
+ gb_operation_put_active(operation);
+ gb_operation_put(operation);
}
EXPORT_SYMBOL_GPL(greybus_message_sent);
--
2.56.1