All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Troy Mitchell" <troy.mitchell@linux.spacemit.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 2/5] clocksource/drivers/spacemit: Add K1 timer support
Date: Fri, 04 Dec 2026 06:24:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-249207@kernel.org> (raw)
In-Reply-To: <20261204-spacemit-soc-timer-v5-2-c863b6ffc645@linux.spacemit.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] clocksource/spacemit: devm-managed clkevt freed on CCU supplier
  unbind
- [Medium] clocksource/drivers/spacemit: missing
  set_state_oneshot_stopped callback
- [Medium] clocksource: redundant quiesce udelay in
  spacemit_timer_next_event()
- [Low] clocksource/drivers/spacemit: missing RESET_CONTROLLER Kconfig
  dependency
--

commit ad7135b3687fa52d25d983234e9577a502e58903
Author: Troy Mitchell <troy.mitchell@linux.spacemit.com>

clocksource/drivers/spacemit: Add K1 timer support

This commit adds a platform clockevent driver for the SpacemiT K1/K3 APB
timers, registering counter 0 and its first match comparator as a shared
oneshot device for tick broadcast.

> diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
> index da83483296d5a..e14bb40f9a314 100644
> --- a/drivers/clocksource/Kconfig
> +++ b/drivers/clocksource/Kconfig
> @@ -163,6 +163,17 @@ config RZN1_TIMER
>  	    - 2 32-bit timers
>  	  Two timers blocks are available in RZ/N1 SoCs.
>  
> +config SPACEMIT_TIMER
> +	bool "SpacemiT K1/K3 timer"
> +	depends on ARCH_SPACEMIT || COMPILE_TEST
> +	depends on OF && COMMON_CLK && HAS_IOMEM
> +	default y if ARCH_SPACEMIT

[Severity: Low]
Should SPACEMIT_TIMER also depend on RESET_CONTROLLER?

spacemit_timer_probe() requests a mandatory reset via
devm_reset_control_get_exclusive(), which fails with -ENOTSUPP when
CONFIG_RESET_CONTROLLER is disabled.  Neither ARCH_SPACEMIT nor
SPACEMIT_TIMER selects RESET_CONTROLLER, so the driver can be built in on
ARCH_SPACEMIT and then unconditionally fail to probe.

> diff --git a/drivers/clocksource/timer-spacemit.c b/drivers/clocksource/timer-spacemit.c
> new file mode 100644
> index 0000000000000..e0f24b133a216
> --- /dev/null
> +++ b/drivers/clocksource/timer-spacemit.c

[ ... ]

> +static int spacemit_timer_next_event(unsigned long delta,
> +				     struct clock_event_device *evt)
> +{
> +	struct spacemit_timer *timer = to_spacemit_timer(evt);
> +	unsigned long flags;
> +	u32 val;
> +	int ret;
> +
> +	raw_spin_lock_irqsave(&timer->lock, flags);
> +
> +	ret = spacemit_timer_quiesce(timer);
> +	if (ret)
> +		goto out;

[Severity: Medium]
Can spacemit_timer_next_event() skip spacemit_timer_quiesce() when the
counter is already stopped and synchronized?

spacemit_timer_stop() always runs udelay(timer->sync_us) at least once.
When a broadcast expiry schedules a next wakeup, spacemit_timer_interrupt()
has already quiesced the timer before event_handler() re-enters
spacemit_timer_next_event() under tick_broadcast_lock with IRQs disabled.

Since clockevents_program_event() computes delta beforehand, the redundant
busy-wait holds tick_broadcast_lock with IRQs off and delays the wakeup by
at least sync_us (3 us at 1 MHz, 92 us at 32.768 kHz).  Tracking whether
the timer is already quiesced under timer->lock would avoid this.

[ ... ]

> +	timer->clkevt.set_next_event = spacemit_timer_next_event;
> +	timer->clkevt.set_state_shutdown = spacemit_timer_shutdown;
> +	timer->clkevt.set_state_oneshot = spacemit_timer_shutdown;
> +	timer->clkevt.tick_resume = spacemit_timer_resume;

[Severity: Medium]
Should timer->clkevt.set_state_oneshot_stopped also be set to
spacemit_timer_shutdown?

When NOHZ idle cancels an event with tick_program_event(KTIME_MAX),
__clockevents_switch_state() returns -ENOSYS when set_state_oneshot_stopped
is NULL.  On UP systems without Sstc (where rating 200 beats the RISC-V
timer's 100), counter 0 stays armed and fires a spurious wakeup interrupt.

[ ... ]

> +	ret = devm_request_irq(dev, irq, spacemit_timer_interrupt, IRQF_TIMER,
> +			       dev_name(dev), timer);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "Failed to request timer interrupt\n");
> +
> +	clockevents_config_and_register(&timer->clkevt, rate, TMR_MIN_DELTA,
> +					U32_MAX - 1);
> +	return 0;
> +}
> +
> +static const struct of_device_id spacemit_timer_match[] = {
> +	{ .compatible = "spacemit,k1-timer" },
> +	{ }
> +};
> +
> +static struct platform_driver spacemit_timer_driver = {
> +	.probe = spacemit_timer_probe,
> +	.driver = {
> +		.name = "spacemit-timer",
> +		.of_match_table = spacemit_timer_match,
> +		.suppress_bind_attrs = true,
> +	},
> +};

[Severity: High]
Can unbinding the CCU supplier free struct spacemit_timer and its devm
resources while &timer->clkevt is still registered?

suppress_bind_attrs only hides this driver's own sysfs unbind file, not
forced consumer unbind in device_links_unbind_consumers().  Because
k1_ccu_driver and k3_ccu_driver expose sysfs unbind, unbinding syscon_apbc
runs devres_release_all() on spacemit-timer.

That frees struct spacemit_timer, unmaps timer->base, disables the clocks,
and frees the IRQ while &timer->clkevt stays live in clockevent_devices
and tick_broadcast_device.evtdev, causing a use-after-free on the next
broadcast event.

Using non-devm allocations and resource acquisitions with explicit probe
error cleanup avoids releasing permanent clockevent state.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-spacemit-soc-timer-v5-0-c863b6ffc645@linux.spacemit.com?part=2

  reply	other threads:[~2026-12-04  6:24 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:01 [PATCH v5 0/5] Add SpacemiT K1/K3 SoC timer support Troy Mitchell
2026-12-04  6:01 ` Troy Mitchell
2026-12-04  6:01 ` [PATCH v5 1/5] dt-bindings: timer: Add SpacemiT K1 timer Troy Mitchell
2026-12-04  6:01   ` Troy Mitchell
2026-12-04  6:13   ` sashiko-bot
2026-12-04  6:01 ` [PATCH v5 2/5] clocksource/drivers/spacemit: Add K1 timer support Troy Mitchell
2026-12-04  6:01   ` Troy Mitchell
2026-12-04  6:24   ` sashiko-bot [this message]
2026-12-04  6:01 ` [PATCH v5 3/5] riscv: dts: spacemit: Add the K1 APB timer Troy Mitchell
2026-12-04  6:01   ` Troy Mitchell
2026-12-04  6:01 ` [PATCH v5 4/5] riscv: dts: spacemit: Add the K3 " Troy Mitchell
2026-12-04  6:01   ` Troy Mitchell
2026-12-04  6:01 ` [PATCH v5 5/5] MAINTAINERS: Add entry for SpacemiT timer Troy Mitchell
2026-12-04  6:01   ` Troy Mitchell

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-249207@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=troy.mitchell@linux.spacemit.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 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.