Linux CAN drivers development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: mailhol@kernel.org, pfink@christ-es.de, eric@evenchick.com,
	max@schneidersoft.net, wg@grandegger.com, kernel@pengutronix.de,
	linux-can@vger.kernel.org, stable@vger.kernel.org,
	kuba@kernel.org
Subject: Re: [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc
Date: Tue, 24 Nov 2026 09:30:55 +0000	[thread overview]
Message-ID: <179551265523.3131.7740892094400767439@kernel.org> (raw)
In-Reply-To: <20261122-gs_usb-fixes-v10-4-7c165c35d04a@pengutronix.de>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch adds `unsigned long tc_lock_flags` to struct gs_usb
  and creates a locking contract that is not written down anywhere.
- [Low] The commit message has typos and does not state the user-visible
  impact.

--

can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc

This commit replaces spin_lock_bh() with spin_lock_irqsave() for
struct gs_usb::tc_lock. The reason is that gs_usb_skb_set_timestamp() can
be called from the URB completion handler in hardirq context when the HCD
does not set HCD_BH. A new tc_lock_flags field in struct gs_usb carries
the saved IRQ state into the cyclecounter read callback, which has to drop
and re-take the lock.

Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-gs_usb-fixes-v8-0-f615502b005b%40pengutronix.de?part=7

> gs_usb_skb_set_timestamp() is called from gs_usb_receive_bulk_callback(),
> the URB completion handle. usb_hcd_giveback_urb() calls the URB completion
> handler directly when the HCD does not set HCD_BH.

[ ... ]

> This issue has not been observed on read hardware, but found by LLM review.

[Severity: Low]
This isn't a bug, but there are two typos here. "completion handle" should
be "completion handler", and "read hardware" should be "real hardware".

The premise is correct. usb_hcd_giveback_urb() calls the handler directly:

drivers/usb/core/hcd.c:usb_hcd_giveback_urb() {
    ...
	if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
		__usb_hcd_giveback_urb(urb);
		return;
	}
    ...
}

So gs_usb_receive_bulk_callback()->gs_usb_set_timestamp()->
gs_usb_skb_set_timestamp() can run in hardirq context.

Could the commit message also say what goes wrong without this fix? With
spin_lock_bh(), gs_usb_timestamp_work() can hold tc_lock while hardirqs
are enabled. A URB completion on the same CPU would then spin on tc_lock
forever. Lockdep would also report an inconsistent {IN-HARDIRQ-W} vs
{HARDIRQ-ON-W} lock state.

The patch is tagged Cc: stable, and the stable team needs that impact
statement to evaluate it.

> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index c9306e7155529..470dd52d97dfe 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -333,6 +333,7 @@ struct gs_usb {
>  	struct cyclecounter cc;
>  	struct timecounter tc;
>  	spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
> +	unsigned long tc_lock_flags;

[Severity: Low]
This isn't a bug in the current callers, but should this new field have a
comment that describes the locking contract it creates?

gs_usb_timestamp_read() is the cyclecounter read callback. It drops and
re-takes tc_lock using the flags stored in the struct:

	spin_unlock_irqrestore(&parent->tc_lock, parent->tc_lock_flags);
	err = gs_usb_get_timestamp(parent, &timestamp);
	spin_lock_irqsave(&parent->tc_lock, parent->tc_lock_flags);

The cc->read() signature cannot carry the caller's flags. Any path that
reaches cc->read therefore has to follow two rules that are not written
down. This covers timecounter_read() in gs_usb_timestamp_work() and
timecounter_init() in gs_usb_timestamp_init():

  - the caller must save its IRQ state in parent->tc_lock_flags, not in a
    local variable
  - the caller must be in sleepable context, because of the GFP_KERNEL
    control transfer in gs_usb_get_timestamp()

The patch also uses two conventions for the same lock.
gs_usb_skb_set_timestamp() keeps its flags in a local variable. That is
safe only because timecounter_cyc2time() never calls cc->read.

The commit message does not mention the new field either.

Two related documentation problems are fixed later in the series by "can:
gs_usb: add support for context analysis":

  - the stale tc_lock comment ("guard access tc->cycle_last")
  - the __must_hold(&dev->tc_lock) annotation on gs_usb_timestamp_read(),
    which names a dev that does not exist in that function

tc_lock_flags still has no comment at the end of the series.

The contract is also harder to see later in the series.
gs_usb_timestamp_start() calls timecounter_init() under
scoped_guard(spinlock_irqsave) with guard-local flags. That is safe only
because cc->read is temporarily swapped to
gs_usb_timestamp_read_initial().

A future caller that takes tc_lock with local flags and then reaches
gs_usb_timestamp_read() would restore a stale IRQ state from
parent->tc_lock_flags, and nothing in the code warns against that.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de

  reply	other threads:[~2026-11-24  9:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko [this message]
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko

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=179551265523.3131.7740892094400767439@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=eric@evenchick.com \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=max@schneidersoft.net \
    --cc=mkl@pengutronix.de \
    --cc=pfink@christ-es.de \
    --cc=stable@vger.kernel.org \
    --cc=wg@grandegger.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox