Linux Serial subsystem development
 help / color / mirror / Atom feed
* [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
  0 siblings, 1 reply; 2+ 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] 2+ 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
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-12-02 16:40 UTC | newest]

Thread overview: 2+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox