* [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled()
@ 2026-12-02 16:28 Hugo Villeneuve
2026-12-02 16:40 ` sashiko-bot
2026-12-06 7:39 ` Tapio Reijonen
0 siblings, 2 replies; 3+ messages in thread
From: Hugo Villeneuve @ 2026-12-02 16:28 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Hugo Villeneuve, linux-kernel, linux-serial
Use devm_clk_get_optional_enabled() helper instead of calling
devm_clk_get_optional() and then clk_prepare_enable(). This simplifies
cleanup code on probe failure or driver detach and allows to remove
out_clk label and associated gotos.
Signed-off-by: Hugo Villeneuve <hugo@hugovil.com>
---
drivers/tty/serial/max310x.c | 27 +++++++--------------------
1 file changed, 7 insertions(+), 20 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index de8a1f05cf30e..8a71127c29210 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1880,23 +1880,17 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
xtal = device_property_match_string(dev, "clock-names", "osc") < 0;
if (xtal)
- s->clk = devm_clk_get_optional(dev, "xtal");
+ s->clk = devm_clk_get_optional_enabled(dev, "xtal");
else
- s->clk = devm_clk_get_optional(dev, "osc");
+ s->clk = devm_clk_get_optional_enabled(dev, "osc");
if (IS_ERR(s->clk))
return PTR_ERR(s->clk);
- ret = clk_prepare_enable(s->clk);
- if (ret)
- return ret;
-
freq = clk_get_rate(s->clk);
if (freq == 0)
freq = uartclk;
- if (freq == 0) {
- ret = dev_err_probe(dev, -EINVAL, "Cannot get clock rate\n");
- goto out_clk;
- }
+ if (freq == 0)
+ return dev_err_probe(dev, -EINVAL, "Cannot get clock rate\n");
if (xtal) {
fmin = 1000000;
@@ -1907,10 +1901,8 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
}
/* Check frequency limits */
- if (freq < fmin || freq > fmax) {
- ret = -ERANGE;
- goto out_clk;
- }
+ if (freq < fmin || freq > fmax)
+ return -ERANGE;
s->regmap = regmaps[0];
s->devtype = devtype;
@@ -1920,7 +1912,7 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
/* Check device to ensure we are talking to what we expect */
ret = max310x_detect(dev);
if (ret)
- goto out_clk;
+ return ret;
for (i = 0; i < devtype->nr; i++) {
unsigned int val;
@@ -2082,9 +2074,6 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
uart_remove_one_port(&max310x_uart, &s->p[i].port);
}
-out_clk:
- clk_disable_unprepare(s->clk);
-
return ret;
}
@@ -2105,8 +2094,6 @@ static void max310x_remove(struct device *dev)
max310x_power(&s->p[i].port, 0);
}
-
- clk_disable_unprepare(s->clk);
}
static const struct of_device_id __maybe_unused max310x_dt_ids[] = {
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled()
2026-12-02 16:28 [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled() Hugo Villeneuve
@ 2026-12-02 16:40 ` sashiko-bot
2026-12-06 7:39 ` Tapio Reijonen
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 16:40 UTC (permalink / raw)
To: Hugo Villeneuve; +Cc: linux-serial
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] serial: max310x: use-after-free in max310x_remove() [1]
--
commit b2a072021bb98e40adfb8c4e05317de478392670
Author: Hugo Villeneuve <hugo@hugovil.com>
serial: max310x: simplify clock management with devm_clk_get_optional_enabled()
This commit replaces devm_clk_get_optional() and manual clock enable and
disable calls with devm_clk_get_optional_enabled().
> diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
> index de8a1f05cf30e..8a71127c29210 100644
> --- a/drivers/tty/serial/max310x.c
> +++ b/drivers/tty/serial/max310x.c
[ ... ]
> @@ -2105,8 +2094,6 @@ static void max310x_remove(struct device *dev)
>
> max310x_power(&s->p[i].port, 0);
> }
> -
> - clk_disable_unprepare(s->clk);
> }
[Severity: Critical]
This problem wasn't introduced by this patch, but can max310x_remove()
cause a use-after-free on struct max310x_port [1] by cancelling the
port work items before calling uart_remove_one_port()?
drivers/tty/serial/max310x.c:max310x_remove() {
for (i = 0; i < s->devtype->nr; i++) {
hrtimer_cancel(&s->p[i].tx_delay_tmr);
cancel_work_sync(&s->p[i].rts_work);
cancel_work_sync(&s->p[i].tx_work);
cancel_work_sync(&s->p[i].md_work);
cancel_work_sync(&s->p[i].rs_work);
if (test_and_clear_bit(s->p[i].port.line, max310x_lines))
uart_remove_one_port(&max310x_uart, &s->p[i].port);
...
}
Because the UART port is still registered and the devm-managed IRQ is
still active when cancel_work_sync() runs, concurrent tty operations or
interrupts can requeue the work items before uart_remove_one_port()
completes:
CPU0 CPU1
max310x_remove()
cancel_work_sync(&s->p[i].tx_work)
max310x_start_tx()
schedule_work(&one->tx_work)
uart_remove_one_port()
devres_release_all()
kfree(s)
max310x_tx_proc() <-- use-after-free
[1] https://sashiko.dev/#/bug/linux-5f8166f2-80f6-4ce1-9c42-cff293ba7fbc
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202162824.34779-1-hugo@hugovil.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled()
2026-12-02 16:28 [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled() Hugo Villeneuve
2026-12-02 16:40 ` sashiko-bot
@ 2026-12-06 7:39 ` Tapio Reijonen
1 sibling, 0 replies; 3+ messages in thread
From: Tapio Reijonen @ 2026-12-06 7:39 UTC (permalink / raw)
To: Hugo Villeneuve, Greg Kroah-Hartman, Jiri Slaby
Cc: linux-kernel, linux-serial
On 12/2/26 18:28, Hugo Villeneuve wrote:
> -out_clk:
> - clk_disable_unprepare(s->clk);
> -
> return ret;
> }
Hi Hugo,
This overlaps with patches of mine that are on the list, so two notes.
The use-after-free in max310x_remove() that the Sashiko reply to this
patch points at is what patch 1/3 of this series fixes:
[PATCH 0/3] serial: max310x: teardown ordering and CTS locking fixes
<20261120-max310x-teardown-fixes-v1-0-eb52c8b612df@vaisala.com>
Patches 1/3 and 2/3 of it change the same lines as this patch: the end
of max310x_remove() and the error exits of max310x_probe(), where 2/3
sends the exits that come before any port is registered to out_clk.
Patch 1/7 of
[PATCH 0/7] serial: core: Power the line transceiver with the port
<20261111-serial-transceiver-supply-v1-0-40bad0f912f2@vaisala.com>
adds two more jumps to out_clk. It applies on top of this patch, but
does not build then, as the label is gone.
With the clock managed by devm those jumps become plain returns, so
the rebase is small in either order. If this patch is applied first I
rebase both series on it.
The change itself looks right to me: the clock is now released by
devres after remove() has returned, and after the gpiochip and the
interrupt, which were requested later in probe.
Reviewed-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
If you have time to look at the teardown series, a review would be
very welcome.
Best regards,
Tapio
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-06 7:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:28 [PATCH] serial: max310x: simplify clock management with devm_clk_get_optional_enabled() Hugo Villeneuve
2026-12-02 16:40 ` sashiko-bot
2026-12-06 7:39 ` Tapio Reijonen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox