All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ayush Singh" <ayush@beagleboard.org>,
	"Jason Kridner" <jkridner@beagleboard.org>,
	robertcnelson@gmail.com, "Johan Hovold" <johan@kernel.org>,
	"Alex Elder" <elder@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Ayush Singh" <ayush@beagleboard.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Markus Probst" <markus.probst@posteo.de>
Cc: linux-crypto@vger.kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v6 1/9] greybus: operation: keep request active across message_send
Date: Fri, 04 Dec 2026 10:55:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-249644@kernel.org> (raw)
In-Reply-To: <20261204-gb-uart-transport-v6-1-668b333ae71d@beagleboard.org>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] greybus: double put on gb_message_send() error races with
  cancellation
- [Medium] greybus: gb_operation_cancel() skips gb_message_cancel() on
  set result
- [Medium] greybus: commit message and greybus_message_sent() comment
  are stale
- [Low] greybus: commit message is missing Fixes trailer for teardown
  bug fix
- [Low] greybus: open-coded connection->lock in
  gb_operation_request_send()
- [Low] greybus: missing Fixes trailer for teardown active-count race
  fix
--

commit 2a2ce0ef899bb0644246f2ca7d0a08a37a9ea029
Author: Ayush Singh <ayush@beagleboard.org>

> greybus: operation: keep request active across message_send
>
> 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@beagleboard.org>

[Severity: Medium]
Does the third paragraph of the commit message match the actual code?

It says the patch drops the second reference and active count once
gb_message_send() returns and returns 0 if the connection starts tearing down
between the two calls.  The implementation instead acquires both active counts
atomically under one connection->lock via __gb_operation_get_active() and
drops the second count in greybus_message_sent(), which runs asynchronously
for drivers such as es2.

The final paragraph also misspells greybus_message_sent() as gb_message_sent()
and has a stray period in "gb_message_send. in core.".  In addition, the
comment in greybus_message_sent() keeps the obsolete response-only paragraph
directly above the new unified description.

[Severity: Low]
This fixes a connection teardown active-count race introduced when
per-connection active-operation tracking was added.  Should this include:

  Fixes: 008974cb528d ("greybus: operation: fix connection tear down")

> diff --git a/drivers/greybus/operation.c b/drivers/greybus/operation.c
> index 7e12ffb2dd60d..a9eada39ec793 100644
> --- a/drivers/greybus/operation.c
> +++ b/drivers/greybus/operation.c

[ ... ]

> @@ -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;

[Severity: Low]
This isn't a bug, but __gb_operation_get_active() is only called from
gb_operation_get_active() and here, and no other work is done under
connection->lock.  Having a locked helper take the count argument would avoid
open-coding spin_lock_irqsave()/spin_unlock_irqrestore() in
gb_operation_request_send().

[ ... ]

> @@ -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;
>  }

[Severity: High]
Can err_put_active underflow operation->active and double-put operation if
gb_operation_cancel() runs while a failing gb_message_send() is in progress?

While gb_message_send() is sleeping (for example in hdlc_tx_frames() in
drivers/greybus/gb-beagleplay.c before returning an error), connection
teardown can cancel the operation and queue gb_operation_work():

CPU0                      CPU1                        kworker
gb_operation_request_send()
  __gb_operation_get_active(..., 2)
  gb_message_send()
                            gb_operation_cancel()
                              result_set(-ESHUTDOWN)
                              queue_work(&op->work)
                              wait_event(!is_active)
                                                      gb_operation_work()
                                                        op->callback(op)
                                                        put_active() [2->1]
                                                        put()
  /* returns error */
  err_put_active:
    put_active() [1->0]
    put_active() [0->-1]  <-- active underflow
    put()
    put()                 <-- extra put / use-after-free

Because gb_operation_is_active() converts signed int operation->active to
bool, -1 stays true permanently and gb_operation_cancel() hangs in
wait_event() while holding connection->mutex.  The extra gb_operation_put()
causes a use-after-free on operation, and returning non-zero after
operation->callback() has already run causes async callers such as
gb_loopback_async_operation() to double free op_async and underflow
outstanding_operations.

Even if cancellation does not race during gb_message_send(), calling
gb_operation_put_active() twice in separate lock sections leaves operation on
connection->operations with active == 1 in between.  If gb_operation_work()
runs after err_put_active instead of before, operation can also be freed while
operation->work is still queued.

On gb_message_send() failure, calling gb_operation_result_set(operation, ret)
can arbitrate completion ownership: if it returns true, drop both active
counts under one connection->lock and both krefs and return ret; otherwise
drop only the host driver's active count and kref and return 0.

[ ... ]

> @@ -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);
>  }

[Severity: Medium]
Now that greybus_message_sent() drops the second active count for outgoing
requests, can gb_operation_cancel() hang if operation->errno was already set
before teardown?

With an async host driver such as es2, if gb_connection_recv_response() sets a
non-ETIMEDOUT result before the bulk-OUT URB completes, gb_operation_work()
does not cancel the request message and drops operation->active from 2 to 1.
A subsequent gb_operation_cancel() then skips gb_message_cancel():

drivers/greybus/operation.c:gb_operation_cancel() {
	...
	if (gb_operation_result_set(operation, errno)) {
		gb_message_cancel(operation->request);
		queue_work(gb_operation_completion_wq, &operation->work);
	}
	trace_gb_message_cancel_outgoing(operation->request);

	atomic_inc(&operation->waiters);
	wait_event(gb_operation_cancellation_queue,
		   !gb_operation_is_active(operation));
	...
}

If the bulk-OUT URB is stalled, greybus_message_sent() never runs and
teardown blocks indefinitely in wait_event() while holding connection->mutex,
unlike gb_operation_cancel_incoming() which cancels the message even when
gb_operation_result_set() returns false.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-gb-uart-transport-v6-0-668b333ae71d@beagleboard.org?part=1

  reply	other threads:[~2026-12-04 10:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 10:30 [PATCH v6 0/9] Add Greybus Sotfsvc and UART Node drivers Ayush Singh
2026-12-04 10:30 ` [PATCH v6 1/9] greybus: operation: keep request active across message_send Ayush Singh
2026-12-04 10:55   ` sashiko-bot [this message]
2026-12-04 10:30 ` [PATCH v6 2/9] greybus: operation: arm request timeout before sending Ayush Singh
2026-12-04 10:52   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 3/9] greybus: svc: add gb_svc_flush() Ayush Singh
2026-12-04 10:43   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 4/9] greybus: connection: Add greybus_data_rcvd_by_intf() Ayush Singh
2026-12-04 10:52   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 5/9] rust: crc_ccitt: add CRC-CCITT abstraction Ayush Singh
2026-12-04 10:30 ` [PATCH v6 6/9] rust: kernel: Add greybus abstractions Ayush Singh
2026-12-04 11:06   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 7/9] drivers: greybus: Add software SVC implementation Ayush Singh
2026-12-04 11:07   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 8/9] dt-bindings: beagle: Add BeagleConnect Freedom Ayush Singh
2026-12-04 11:03   ` sashiko-bot
2026-12-04 10:30 ` [PATCH v6 9/9] greybus: Add Rust UART node driver Ayush Singh
2026-12-04 11:18   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249644@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=ardb@kernel.org \
    --cc=ayush@beagleboard.com \
    --cc=ayush@beagleboard.org \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=devicetree@vger.kernel.org \
    --cc=ebiggers@kernel.org \
    --cc=elder@kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jkridner@beagleboard.org \
    --cc=johan@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=robertcnelson@gmail.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@kernel.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.