serial: mxs-auart: fix probe error paths and clock handling
Subsystems (2)
PATCH v2 1/1 serial: mxs-auart: fix probe error paths and clock handling
Sashiko reported three pre-existing bugs in the mxs-auart driver:
- For non-ASM9260 variants, mxs_get_clks() obtained the clock but never prepared/enabled it, leaving register accesses in probe at risk of faulting if the bootloader had gated the clock.
- The error path and remove function used pdev->id instead of s->port.line to clear the auart_port[] slot. For DT-probed devices pdev->id is -1, causing an out-of-bounds write and leaving a dangling pointer in the array.
- The probe error path called iounmap() while the IRQ was still registered. An interrupt during that window would dereference the unmapped membase.
---
v2: reduce overly zealous devm conversions
drivers/tty/serial/mxs-auart.c | 127 +++++++++++++-------------------- 1 file changed, 49 insertions(+), 78 deletions(-)
drivers/tty/serial/mxs-auart.c +49 -78
@@ -1470,34 +1470,22 @@ static int mxs_get_clks(struct mxs_auart_port *s,return PTR_ERR (s->clk); } - s->clk_ahb =devm_clk_get (s->dev,"ahb" ); + s->clk_ahb =devm_clk_get_enabled (s->dev,"ahb" );if (IS_ERR (s->clk_ahb)) {dev_err (s->dev,"Failed to get \"ahb\" clk\n" );return PTR_ERR (s->clk_ahb); } - err =clk_prepare_enable (s->clk_ahb); -if (err) { -dev_err (s->dev,"Failed to enable ahb_clk!\n" ); -return err; - } - +/* +* Set mod clock rate while it is still disabled so +* CLK_SET_RATE_GATE does not cause clk_set_rate to fail. +* The mod clock will be enabled in mxs_auart_startup() +* and in probe after mxs_get_clks returns. +*/ err =clk_set_rate (s->clk,clk_get_rate (s->clk_ahb)); -if (err) { +if (err)dev_err (s->dev,"Failed to set rate!\n" ); - goto disable_clk_ahb; - } - - err =clk_prepare_enable (s->clk); -if (err) { -dev_err (s->dev,"Failed to enable clk!\n" ); - goto disable_clk_ahb; - } - -return 0 ; -disable_clk_ahb: -clk_disable_unprepare (s->clk_ahb);return err; }
@@ -1529,15 +1517,6 @@ static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)return 0 ; } -static void mxs_auart_free_gpio_irq (struct mxs_auart_port *s) -{ -enum mctrl_gpio_idx i; - -for (i =0 ; i <UART_GPIO_MAX ; i++) -if (s->gpio_irq[i] >=0 ) -free_irq (s->gpio_irq[i], s); -} -static int mxs_auart_request_gpio_irq (struct mxs_auart_port *s) {int *irq = s->gpio_irq;
@@ -1549,21 +1528,13 @@ static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)continue ;irq_set_status_flags (irq[i],IRQ_NOAUTOEN ); - err =request_irq (irq[i], mxs_auart_irq_handle, -IRQ_TYPE_EDGE_BOTH ,dev_name (s->dev), s); + err =devm_request_irq (s->dev, irq[i], mxs_auart_irq_handle,IRQ_TYPE_EDGE_BOTH , +dev_name (s->dev), s);if (err)dev_err (s->dev,"%s - Can't get %d irq\n" , __func__, irq[i]); } -/* -* If something went wrong, rollback. -* Be careful: i may be unsigned. -*/ -while (err && (i-- >0 )) -if (irq[i] >=0 ) -free_irq (irq[i], s); -return err; }
@@ -1598,24 +1569,22 @@ static int mxs_auart_probe(struct platform_device *pdev)return -EINVAL ; } - s->devtype = (enum mxs_auart_type )of_device_get_match_data (&pdev->dev); + s->devtype = (unsigned long )of_device_get_match_data (&pdev->dev); ret =mxs_get_clks (s, pdev);if (ret)return ret; - r =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (!r) { - ret = -ENXIO ; - goto out_disable_clks; - } + ret =clk_prepare_enable (s->clk); +if (ret) +return ret; - s->port.mapbase = r->start; - s->port.membase =ioremap (r->start,resource_size (r)); -if (!s->port.membase) { - ret = -ENOMEM ; - goto out_disable_clks; + s->port.membase =devm_platform_get_and_ioremap_resource (pdev,0 , &r); +if (IS_ERR (s->port.membase)) { + ret =PTR_ERR (s->port.membase); + goto out_disable_clk; } + s->port.mapbase = r->start; s->port.ops = &mxs_auart_ops; s->port.iotype =UPIO_MEM ; s->port.fifosize =MXS_AUART_FIFO_SIZE ;
@@ -1630,21 +1599,17 @@ static int mxs_auart_probe(struct platform_device *pdev) irq =platform_get_irq (pdev,0 );if (irq <0 ) { ret = irq; - goto out_iounmap; + goto out_disable_clk; } s->port.irq = irq; - ret =devm_request_irq (&pdev->dev, irq, mxs_auart_irq_handle,0 , -dev_name (&pdev->dev), s); -if (ret) - goto out_iounmap;platform_set_drvdata (pdev, s); ret =mxs_auart_init_gpios (s, &pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to initialize GPIOs.\n" ); - goto out_iounmap; + goto out_disable_clk; }/*
@@ -1652,15 +1617,34 @@ static int mxs_auart_probe(struct platform_device *pdev)*/ ret =mxs_auart_request_gpio_irq (s);if (ret) - goto out_iounmap; + goto out_disable_clk; auart_port[s->port.line] = s;mxs_auart_reset_deassert (s); +/* Mask all UART interrupts to prevent spurious IRQs from bootloader */ +mxs_write (0 , s,REG_INTR ); + ret =uart_add_one_port (&auart_driver, &s->port); -if (ret) - goto out_free_qpio_irq; +if (ret) { + auart_port[s->port.line] =NULL ; + goto out_disable_clk; + } + +/* +* Request the main IRQ after uart_add_one_port so that +* s->port.state and s->port.lock are initialized before +* the handler can run in response to a bootloader-left +* interrupt. +*/ + ret =devm_request_irq (&pdev->dev, irq, mxs_auart_irq_handle,0 , +dev_name (&pdev->dev), s); +if (ret) { +uart_remove_one_port (&auart_driver, &s->port); + auart_port[s->port.line] =NULL ; + goto out_disable_clk; + }/* ASM9260 don't have version reg */ if (is_asm9260_auart (s)) {
@@ -1672,20 +1656,13 @@ static int mxs_auart_probe(struct platform_device *pdev) (version >> 16) & 0xff, version & 0xffff); } -return 0 ; - -out_free_qpio_irq: -mxs_auart_free_gpio_irq (s); - auart_port[pdev->id] =NULL ; +/* Disable clock – startup will re-enable when the port is opened */ +clk_disable_unprepare (s->clk); -out_iounmap: -iounmap (s->port.membase); +return 0 ; -out_disable_clks: -if (is_asm9260_auart (s)) { -clk_disable_unprepare (s->clk); -clk_disable_unprepare (s->clk_ahb); - } +out_disable_clk: +clk_disable_unprepare (s->clk);return ret; }
@@ -1694,13 +1671,7 @@ static void mxs_auart_remove(struct platform_device *pdev)struct mxs_auart_port *s =platform_get_drvdata (pdev);uart_remove_one_port (&auart_driver, &s->port); - auart_port[pdev->id] =NULL ; -mxs_auart_free_gpio_irq (s); -iounmap (s->port.membase); -if (is_asm9260_auart (s)) { -clk_disable_unprepare (s->clk); -clk_disable_unprepare (s->clk_ahb); - } + auart_port[s->port.line] =NULL ; }static struct platform_driver mxs_auart_driver = {
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The unconditional `clk_disable_unprepare(s->clk)` at the end of `probe()` breaks the serial…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The unconditional clk_disable_unprepare(s->clk) at the end of probe() breaks the serial console output. --
commit 09d89989b2eafe8aa07311d7fa3bb051e4b94c5c
Author: Rosen Penev <[email protected]>
serial: mxs-auart: fix probe error paths and clock handling
This commit fixes probe error paths and clock handling in the mxs-auart driver. It resolves unenabled clock accesses in probe, fixes port array bounds writes on DT devices, and corrects out-of-order unmapping during error handling.
Quoted 4 lines
drivers/tty/serial/mxs-auart.c +0
-0
[ ... ]
Quoted 1 line
drivers/tty/serial/mxs-auart.c +0
-0@@ -1652,15 +1617,34 @@ static int mxs_auart_probe(struct platform_device *pdev)
[ ... ]
drivers/tty/serial/mxs-auart.c static int mxs_auart_probe(struct platform_device *pdev) · line 1660
Quoted 13 lines
drivers/tty/serial/mxs-auart.c +2
-5/* ASM9260 don't have version reg */ if (is_asm9260_auart (s)) {@@ -1672,20 +1656,13 @@ static int mxs_auart_probe(struct platform_device *pdev) (version >> 16) & 0xff, version & 0xffff); } -return 0 ; - -out_free_qpio_irq: -mxs_auart_free_gpio_irq (s); - auart_port[pdev->id] =NULL ; +/* Disable clock – startup will re-enable when the port is opened */ +clk_disable_unprepare (s->clk);
[Severity: High]
Will this unconditional clk_disable_unprepare() break the serial console?
If the UART is configured as the console, printk() can asynchronously invoke auart_console_write() from atomic context at any time. auart_console_write() relies on clk_enable(s->clk) to temporarily ungate the clock.
Since the Common Clock Framework enforces that clocks must be prepared before they are enabled, calling clk_enable() on an unprepared clock returns an error and triggers a kernel warning. As a result, auart_console_write() will abort and drop all console messages between the end of mxs_auart_probe() and when userspace explicitly opens the port.
Should this check !uart_console(&s->port) before unpreparing the clock?
Quoted 3 lines
drivers/tty/serial/mxs-auart.c +0
-2-out_iounmap: -iounmap (s->port.membase);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg Kroah-Hartman
maintainerQuoted 12 lines · Rosen Penev
Sashiko reported three pre-existing bugs in the mxs-auart driver:
- For non-ASM9260 variants,
mxs_get_clks()obtained the clock but never prepared/enabled it, leaving register accesses in probe at risk of faulting if the bootloader had gated the clock.
- The error path and remove function usedpdev->idinstead ofs->port.lineto clear the auart_port[] slot. For DT-probed devicespdev->idis -1, causing an out-of-bounds write and leaving a dangling pointer in the array.
- The probe error path callediounmap()while the IRQ was still registered. An interrupt during that window would dereference the unmapped membase.
This should still be broken up into smaller pieces, don't you think?
Whenever you have to list the different things you do in a patch, that's a huge hint to do so.
thanks,
greg k-h
Quoted 15 lines · Greg Kroah-Hartman
Quoted 12 lines · Rosen Penev
Sashiko reported three pre-existing bugs in the mxs-auart driver:
- For non-ASM9260 variants,
mxs_get_clks()obtained the clock but never prepared/enabled it, leaving register accesses in probe at risk of faulting if the bootloader had gated the clock.
- The error path and remove function usedpdev->idinstead ofs->port.lineto clear the auart_port[] slot. For DT-probed devicespdev->idis -1, causing an out-of-bounds write and leaving a dangling pointer in the array.
- The probe error path callediounmap()while the IRQ was still registered. An interrupt during that window would dereference the unmapped membase.
Quoted 4 lines
This should still be broken up into smaller pieces, don't you think?
Whenever you have to list the different things you do in a patch, that's a huge hint to do so.
I'll do so. Although I doubt the bot will be happy either way.
Quoted 4 lines
thanks,
greg k-h
Greg Kroah-Hartman
maintainerQuoted 17 lines · Rosen Penev
Quoted 15 lines · Greg Kroah-Hartman
Quoted 12 lines · Rosen Penev
Sashiko reported three pre-existing bugs in the mxs-auart driver:
- For non-ASM9260 variants,
mxs_get_clks()obtained the clock but never prepared/enabled it, leaving register accesses in probe at risk of faulting if the bootloader had gated the clock.
- The error path and remove function usedpdev->idinstead ofs->port.lineto clear the auart_port[] slot. For DT-probed devicespdev->idis -1, causing an out-of-bounds write and leaving a dangling pointer in the array.
- The probe error path callediounmap()while the IRQ was still registered. An interrupt during that window would dereference the unmapped membase.
Quoted 5 lines
Quoted 4 lines
This should still be broken up into smaller pieces, don't you think?
Whenever you have to list the different things you do in a patch, that's a huge hint to do so.
I'll do so. Although I doubt the bot will be happy either way.
What bot? Yours? If so, please don't use it.