2021-10-20 19:29:16

by Francesco Dolcini

[permalink] [raw]
Subject: [PATCH v2 0/2] serial: imx: fix unregister/register console

Hi,
here a couple of fixes to properly handle imx serial console
unregister/register at runtime.

The patch from Stefan was already posted years ago but never merged [1],
however is still valid and it's pretty easy to trigger the problem. We have
this patch in our kernel tree since 3 years, but for some reason we never
managed to upstream it.

As correctly wrote by Uwe [2] this issue affects multiple drivers, I could
provide a patch for those, but I will not have any way to test it.

The second patch is required to have a balanced clock enable/disable
when doing console register/unregister.

The issue is present at least since
a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")

[1] https://marc.info/?l=linux-serial&m=154221779312036&w=2
[2] https://lore.kernel.org/all/[email protected]/

Changes in v2:
Improved commit message and added fixes tag.

---

Francesco Dolcini (1):
serial: imx: disable console clocks on unregister

Stefan Agner (1):
serial: imx: fix detach/attach of serial console

drivers/tty/serial/imx.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)

--
2.25.1


2021-10-20 19:29:46

by Francesco Dolcini

[permalink] [raw]
Subject: [PATCH v2 1/2] serial: imx: fix detach/attach of serial console

From: Stefan Agner <[email protected]>

If the device used as a serial console gets detached/attached at runtime,
register_console() will try to call imx_uart_setup_console(), but this
is not possible since it is marked as __init.

For instance

# cat /sys/devices/virtual/tty/console/active
tty1 ttymxc0
# echo -n N > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
# echo -n Y > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console

[ 73.166649] 8<--- cut here ---
[ 73.167005] Unable to handle kernel paging request at virtual address c154d928
[ 73.167601] pgd = 55433e84
[ 73.167875] [c154d928] *pgd=8141941e(bad)
[ 73.168304] Internal error: Oops: 8000000d [#1] SMP ARM
[ 73.168429] Modules linked in:
[ 73.168522] CPU: 0 PID: 536 Comm: sh Not tainted 5.15.0-rc6-00056-g3968ddcf05fb #3
[ 73.168675] Hardware name: Freescale i.MX6 Ultralite (Device Tree)
[ 73.168791] PC is at imx_uart_console_setup+0x0/0x238
[ 73.168927] LR is at try_enable_new_console+0x98/0x124
[ 73.169056] pc : [<c154d928>] lr : [<c0196f44>] psr: a0000013
[ 73.169178] sp : c2ef5e70 ip : 00000000 fp : 00000000
[ 73.169281] r10: 00000000 r9 : c02cf970 r8 : 00000000
[ 73.169389] r7 : 00000001 r6 : 00000001 r5 : c1760164 r4 : c1e0fb08
[ 73.169512] r3 : c154d928 r2 : 00000000 r1 : efffcbd1 r0 : c1760164
[ 73.169641] Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none
[ 73.169782] Control: 10c5387d Table: 8345406a DAC: 00000051
[ 73.169895] Register r0 information: non-slab/vmalloc memory
[ 73.170032] Register r1 information: non-slab/vmalloc memory
[ 73.170158] Register r2 information: NULL pointer
[ 73.170273] Register r3 information: non-slab/vmalloc memory
[ 73.170397] Register r4 information: non-slab/vmalloc memory
[ 73.170521] Register r5 information: non-slab/vmalloc memory
[ 73.170647] Register r6 information: non-paged memory
[ 73.170771] Register r7 information: non-paged memory
[ 73.170892] Register r8 information: NULL pointer
[ 73.171009] Register r9 information: non-slab/vmalloc memory
[ 73.171142] Register r10 information: NULL pointer
[ 73.171259] Register r11 information: NULL pointer
[ 73.171375] Register r12 information: NULL pointer
[ 73.171494] Process sh (pid: 536, stack limit = 0xcd1ba82f)
[ 73.171621] Stack: (0xc2ef5e70 to 0xc2ef6000)
[ 73.171731] 5e60: ???????? ???????? ???????? ????????
[ 73.171899] 5e80: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172059] 5ea0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172217] 5ec0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172377] 5ee0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172537] 5f00: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172698] 5f20: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.172856] 5f40: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173016] 5f60: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173177] 5f80: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173336] 5fa0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173496] 5fc0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173654] 5fe0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.173826] [<c0196f44>] (try_enable_new_console) from [<c01984a8>] (register_console+0x10c/0x2ec)
[ 73.174053] [<c01984a8>] (register_console) from [<c06e2c90>] (console_store+0x14c/0x168)
[ 73.174262] [<c06e2c90>] (console_store) from [<c0383718>] (kernfs_fop_write_iter+0x110/0x1cc)
[ 73.174470] [<c0383718>] (kernfs_fop_write_iter) from [<c02cf5f4>] (vfs_write+0x31c/0x548)
[ 73.174679] [<c02cf5f4>] (vfs_write) from [<c02cf970>] (ksys_write+0x60/0xec)
[ 73.174863] [<c02cf970>] (ksys_write) from [<c0100080>] (ret_fast_syscall+0x0/0x1c)
[ 73.175052] Exception stack(0xc2ef5fa8 to 0xc2ef5ff0)
[ 73.175167] 5fa0: ???????? ???????? ???????? ???????? ???????? ????????
[ 73.175327] 5fc0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
[ 73.175486] 5fe0: ???????? ???????? ???????? ????????
[ 73.175608] Code: 00000000 00000000 00000000 00000000 (00000000)
[ 73.175744] ---[ end trace 9b75121265109bf1 ]---

A similar issue could be triggered unbinding/binding the serial console
device [*].

Drop __init so that imx_uart_setup_console() can be safely called at
runtime.

[*] https://lore.kernel.org/all/[email protected]/

Fixes: a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")
Signed-off-by: Stefan Agner <[email protected]>
Signed-off-by: Francesco Dolcini <[email protected]>
---
drivers/tty/serial/imx.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 8b121cd869e9..51a9f9423b1a 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -2017,7 +2017,7 @@ imx_uart_console_write(struct console *co, const char *s, unsigned int count)
* If the port was already initialised (eg, by a boot loader),
* try to determine the current setup.
*/
-static void __init
+static void
imx_uart_console_get_options(struct imx_port *sport, int *baud,
int *parity, int *bits)
{
@@ -2076,7 +2076,7 @@ imx_uart_console_get_options(struct imx_port *sport, int *baud,
}
}

-static int __init
+static int
imx_uart_console_setup(struct console *co, char *options)
{
struct imx_port *sport;
--
2.25.1

2021-10-20 19:31:22

by Francesco Dolcini

[permalink] [raw]
Subject: [PATCH v2 2/2] serial: imx: disable console clocks on unregister

During console setup imx_uart_console_setup() enables clocks, but they
are never disabled when the console is unregistered, this leads to
clk_prepare_enable() being called multiple times without a matching
clk_disable_unprepare() in case of console unregister.

Ensure that clock enable/disable are balanced adding
clk_disable_unprepare() in the console exit callback.

Signed-off-by: Francesco Dolcini <[email protected]>
---
drivers/tty/serial/imx.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 51a9f9423b1a..90f82e6c54e4 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -2124,12 +2124,24 @@ imx_uart_console_setup(struct console *co, char *options)
return retval;
}

+static int
+imx_uart_console_exit(struct console *co)
+{
+ struct imx_port *sport = imx_uart_ports[co->index];
+
+ clk_disable_unprepare(sport->clk_per);
+ clk_disable_unprepare(sport->clk_ipg);
+
+ return 0;
+}
+
static struct uart_driver imx_uart_uart_driver;
static struct console imx_uart_console = {
.name = DEV_NAME,
.write = imx_uart_console_write,
.device = uart_console_device,
.setup = imx_uart_console_setup,
+ .exit = imx_uart_console_exit,
.flags = CON_PRINTBUFFER,
.index = -1,
.data = &imx_uart_uart_driver,
--
2.25.1

2021-10-20 19:41:07

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] serial: imx: fix detach/attach of serial console

On Wed, Oct 20, 2021 at 09:26:42PM +0200, Francesco Dolcini wrote:
> From: Stefan Agner <[email protected]>
>
> If the device used as a serial console gets detached/attached at runtime,
> register_console() will try to call imx_uart_setup_console(), but this
> is not possible since it is marked as __init.
>
> For instance
>
> # cat /sys/devices/virtual/tty/console/active
> tty1 ttymxc0
> # echo -n N > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
> # echo -n Y > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
>
> [ 73.166649] 8<--- cut here ---
> [ 73.167005] Unable to handle kernel paging request at virtual address c154d928
> [ 73.167601] pgd = 55433e84
> [ 73.167875] [c154d928] *pgd=8141941e(bad)
> [ 73.168304] Internal error: Oops: 8000000d [#1] SMP ARM
> [ 73.168429] Modules linked in:
> [ 73.168522] CPU: 0 PID: 536 Comm: sh Not tainted 5.15.0-rc6-00056-g3968ddcf05fb #3
> [ 73.168675] Hardware name: Freescale i.MX6 Ultralite (Device Tree)
> [ 73.168791] PC is at imx_uart_console_setup+0x0/0x238
> [ 73.168927] LR is at try_enable_new_console+0x98/0x124
> [ 73.169056] pc : [<c154d928>] lr : [<c0196f44>] psr: a0000013
> [ 73.169178] sp : c2ef5e70 ip : 00000000 fp : 00000000
> [ 73.169281] r10: 00000000 r9 : c02cf970 r8 : 00000000
> [ 73.169389] r7 : 00000001 r6 : 00000001 r5 : c1760164 r4 : c1e0fb08
> [ 73.169512] r3 : c154d928 r2 : 00000000 r1 : efffcbd1 r0 : c1760164
> [ 73.169641] Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none
> [ 73.169782] Control: 10c5387d Table: 8345406a DAC: 00000051
> [ 73.169895] Register r0 information: non-slab/vmalloc memory
> [ 73.170032] Register r1 information: non-slab/vmalloc memory
> [ 73.170158] Register r2 information: NULL pointer
> [ 73.170273] Register r3 information: non-slab/vmalloc memory
> [ 73.170397] Register r4 information: non-slab/vmalloc memory
> [ 73.170521] Register r5 information: non-slab/vmalloc memory
> [ 73.170647] Register r6 information: non-paged memory
> [ 73.170771] Register r7 information: non-paged memory
> [ 73.170892] Register r8 information: NULL pointer
> [ 73.171009] Register r9 information: non-slab/vmalloc memory
> [ 73.171142] Register r10 information: NULL pointer
> [ 73.171259] Register r11 information: NULL pointer
> [ 73.171375] Register r12 information: NULL pointer
> [ 73.171494] Process sh (pid: 536, stack limit = 0xcd1ba82f)
> [ 73.171621] Stack: (0xc2ef5e70 to 0xc2ef6000)
> [ 73.171731] 5e60: ???????? ???????? ???????? ????????
> [ 73.171899] 5e80: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172059] 5ea0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172217] 5ec0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172377] 5ee0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172537] 5f00: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172698] 5f20: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.172856] 5f40: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173016] 5f60: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173177] 5f80: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173336] 5fa0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173496] 5fc0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173654] 5fe0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.173826] [<c0196f44>] (try_enable_new_console) from [<c01984a8>] (register_console+0x10c/0x2ec)
> [ 73.174053] [<c01984a8>] (register_console) from [<c06e2c90>] (console_store+0x14c/0x168)
> [ 73.174262] [<c06e2c90>] (console_store) from [<c0383718>] (kernfs_fop_write_iter+0x110/0x1cc)
> [ 73.174470] [<c0383718>] (kernfs_fop_write_iter) from [<c02cf5f4>] (vfs_write+0x31c/0x548)
> [ 73.174679] [<c02cf5f4>] (vfs_write) from [<c02cf970>] (ksys_write+0x60/0xec)
> [ 73.174863] [<c02cf970>] (ksys_write) from [<c0100080>] (ret_fast_syscall+0x0/0x1c)
> [ 73.175052] Exception stack(0xc2ef5fa8 to 0xc2ef5ff0)
> [ 73.175167] 5fa0: ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.175327] 5fc0: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ????????
> [ 73.175486] 5fe0: ???????? ???????? ???????? ????????
> [ 73.175608] Code: 00000000 00000000 00000000 00000000 (00000000)
> [ 73.175744] ---[ end trace 9b75121265109bf1 ]---
>
> A similar issue could be triggered unbinding/binding the serial console
> device [*].
>
> Drop __init so that imx_uart_setup_console() can be safely called at
> runtime.
>
> [*] https://lore.kernel.org/all/[email protected]/
>
> Fixes: a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")
> Signed-off-by: Stefan Agner <[email protected]>
> Signed-off-by: Francesco Dolcini <[email protected]>
Acked-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (5.41 kB)
signature.asc (499.00 B)
Download all attachments

2021-10-20 19:58:26

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] serial: imx: fix detach/attach of serial console

On Wed, Oct 20, 2021 at 10:27 PM Francesco Dolcini
<[email protected]> wrote:
>
> From: Stefan Agner <[email protected]>
>
> If the device used as a serial console gets detached/attached at runtime,
> register_console() will try to call imx_uart_setup_console(), but this
> is not possible since it is marked as __init.

Thank you for fixing this!

> For instance
>
> # cat /sys/devices/virtual/tty/console/active
> tty1 ttymxc0
> # echo -n N > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
> # echo -n Y > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console

Can we leave only something like below in the commit message?

> [ 73.167005] Unable to handle kernel paging request at virtual address c154d928
> [ 73.168304] Internal error: Oops: 8000000d [#1] SMP ARM
> [ 73.168522] CPU: 0 PID: 536 Comm: sh Not tainted 5.15.0-rc6-00056-g3968ddcf05fb #3
...
> [ 73.168791] PC is at imx_uart_console_setup+0x0/0x238
> [ 73.168927] LR is at try_enable_new_console+0x98/0x124
...
> [ 73.173826] [<c0196f44>] (try_enable_new_console) from [<c01984a8>] (register_console+0x10c/0x2ec)
> [ 73.174053] [<c01984a8>] (register_console) from [<c06e2c90>] (console_store+0x14c/0x168)
> [ 73.174262] [<c06e2c90>] (console_store) from [<c0383718>] (kernfs_fop_write_iter+0x110/0x1cc)

> A similar issue could be triggered unbinding/binding the serial console

on unbinding/binding

> device [*].
>
> Drop __init so that imx_uart_setup_console() can be safely called at
> runtime.
>
> [*] https://lore.kernel.org/all/[email protected]/

Make it Link: tag?

> Fixes: a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")
> Signed-off-by: Stefan Agner <[email protected]>
> Signed-off-by: Francesco Dolcini <[email protected]>

With above nit-picks addressed, FWIW,
Reviewed-by: Andy Shevchenko <[email protected]>

--
With Best Regards,
Andy Shevchenko

2021-10-21 08:30:10

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] serial: imx: fix detach/attach of serial console

On Wed, Oct 20, 2021 at 10:54:26PM +0300, Andy Shevchenko wrote:
> On Wed, Oct 20, 2021 at 10:27 PM Francesco Dolcini
> <[email protected]> wrote:
> >
> > From: Stefan Agner <[email protected]>
> >
> > If the device used as a serial console gets detached/attached at runtime,
> > register_console() will try to call imx_uart_setup_console(), but this
> > is not possible since it is marked as __init.
>
> Thank you for fixing this!
>
> > For instance
> >
> > # cat /sys/devices/virtual/tty/console/active
> > tty1 ttymxc0
> > # echo -n N > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
> > # echo -n Y > /sys/devices/virtual/tty/console/subsystem/ttymxc0/console
>
> Can we leave only something like below in the commit message?
>
> > [ 73.167005] Unable to handle kernel paging request at virtual address c154d928
> > [ 73.168304] Internal error: Oops: 8000000d [#1] SMP ARM
> > [ 73.168522] CPU: 0 PID: 536 Comm: sh Not tainted 5.15.0-rc6-00056-g3968ddcf05fb #3
> ...
> > [ 73.168791] PC is at imx_uart_console_setup+0x0/0x238
> > [ 73.168927] LR is at try_enable_new_console+0x98/0x124
> ...
> > [ 73.173826] [<c0196f44>] (try_enable_new_console) from [<c01984a8>] (register_console+0x10c/0x2ec)
> > [ 73.174053] [<c01984a8>] (register_console) from [<c06e2c90>] (console_store+0x14c/0x168)
> > [ 73.174262] [<c06e2c90>] (console_store) from [<c0383718>] (kernfs_fop_write_iter+0x110/0x1cc)
>
> > A similar issue could be triggered unbinding/binding the serial console
>
> on unbinding/binding
>
> > device [*].
> >
> > Drop __init so that imx_uart_setup_console() can be safely called at
> > runtime.
> >
> > [*] https://lore.kernel.org/all/[email protected]/
>
> Make it Link: tag?
>
> > Fixes: a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")
> > Signed-off-by: Stefan Agner <[email protected]>
> > Signed-off-by: Francesco Dolcini <[email protected]>
>
> With above nit-picks addressed, FWIW,

Those are not a big deal, I'll edit the changelog text...

thanks,

greg k-h

2021-10-21 08:41:11

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] serial: imx: disable console clocks on unregister

On Wed, Oct 20, 2021 at 09:26:43PM +0200, Francesco Dolcini wrote:
> During console setup imx_uart_console_setup() enables clocks, but they
> are never disabled when the console is unregistered, this leads to
> clk_prepare_enable() being called multiple times without a matching
> clk_disable_unprepare() in case of console unregister.
>
> Ensure that clock enable/disable are balanced adding
> clk_disable_unprepare() in the console exit callback.
>
> Signed-off-by: Francesco Dolcini <[email protected]>
Acked-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (765.00 B)
signature.asc (499.00 B)
Download all attachments