2023-04-27 18:28:45

by Doug Berger

[permalink] [raw]
Subject: [PATCH 0/2] serial: 8250_bcm7271: Fix clock handling

XuDong Liu offered a patch to correct some clock issues detected
in the probe function and Christophe JAILLET pointed out that
there was a similar issue in remove.

Since no V2 has appeared, I am providing this pair of commits to
address both cases. This set chooses to use a managed clock to
avoid the need to explicitly call clk_put from the driver.

Doug Berger (2):
serial: 8250_bcm7271: balance clk_enable calls
serial: 8250_bcm7271: fix leak in `brcmuart_probe`

drivers/tty/serial/8250/8250_bcm7271.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

--
2.34.1


2023-04-27 18:28:56

by Doug Berger

[permalink] [raw]
Subject: [PATCH 2/2] serial: 8250_bcm7271: fix leak in `brcmuart_probe`

Smatch reports:
drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.

The issue is fixed by using a managed clock.

Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom STB driver")
Reported-by: XuDong Liu <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Doug Berger <[email protected]>
---
drivers/tty/serial/8250/8250_bcm7271.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
index 90ee7bc12f77..af0e1c070187 100644
--- a/drivers/tty/serial/8250/8250_bcm7271.c
+++ b/drivers/tty/serial/8250/8250_bcm7271.c
@@ -1012,7 +1012,7 @@ static int brcmuart_probe(struct platform_device *pdev)
of_property_read_u32(np, "clock-frequency", &clk_rate);

/* See if a Baud clock has been specified */
- baud_mux_clk = of_clk_get_by_name(np, "sw_baud");
+ baud_mux_clk = devm_clk_get(dev, "sw_baud");
if (IS_ERR(baud_mux_clk)) {
if (PTR_ERR(baud_mux_clk) == -EPROBE_DEFER) {
ret = -EPROBE_DEFER;
--
2.34.1

2023-04-27 18:29:01

by Doug Berger

[permalink] [raw]
Subject: [PATCH 1/2] serial: 8250_bcm7271: balance clk_enable calls

The sw_baud clock must be disabled when the device driver is not
connected to the device. This now occurs when probe fails and
upon remove.

Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom STB driver")
Reported-by: XuDong Liu <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Doug Berger <[email protected]>
---
drivers/tty/serial/8250/8250_bcm7271.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
index f801b1f5b46c..90ee7bc12f77 100644
--- a/drivers/tty/serial/8250/8250_bcm7271.c
+++ b/drivers/tty/serial/8250/8250_bcm7271.c
@@ -1032,7 +1032,7 @@ static int brcmuart_probe(struct platform_device *pdev)
if (clk_rate == 0) {
dev_err(dev, "clock-frequency or clk not defined\n");
ret = -EINVAL;
- goto release_dma;
+ goto err_clk_disable;
}

dev_dbg(dev, "DMA is %senabled\n", priv->dma_enabled ? "" : "not ");
@@ -1119,6 +1119,8 @@ static int brcmuart_probe(struct platform_device *pdev)
serial8250_unregister_port(priv->line);
err:
brcmuart_free_bufs(dev, priv);
+err_clk_disable:
+ clk_disable_unprepare(baud_mux_clk);
release_dma:
if (priv->dma_enabled)
brcmuart_arbitration(priv, 0);
@@ -1133,6 +1135,7 @@ static int brcmuart_remove(struct platform_device *pdev)
hrtimer_cancel(&priv->hrt);
serial8250_unregister_port(priv->line);
brcmuart_free_bufs(&pdev->dev, priv);
+ clk_disable_unprepare(priv->baud_mux_clk);
if (priv->dma_enabled)
brcmuart_arbitration(priv, 0);
return 0;
--
2.34.1

2023-04-27 20:35:49

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH 2/2] serial: 8250_bcm7271: fix leak in `brcmuart_probe`

Le 27/04/2023 à 20:19, Doug Berger a écrit :
> Smatch reports:
> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
>
> The issue is fixed by using a managed clock.
>
> Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom STB driver")
> Reported-by: XuDong Liu <[email protected]>
> Link: https://lore.kernel.org/lkml/[email protected]/
> Signed-off-by: Doug Berger <[email protected]>
> ---
> drivers/tty/serial/8250/8250_bcm7271.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/tty/serial/8250/8250_bcm7271.c b/drivers/tty/serial/8250/8250_bcm7271.c
> index 90ee7bc12f77..af0e1c070187 100644
> --- a/drivers/tty/serial/8250/8250_bcm7271.c
> +++ b/drivers/tty/serial/8250/8250_bcm7271.c
> @@ -1012,7 +1012,7 @@ static int brcmuart_probe(struct platform_device *pdev)
> of_property_read_u32(np, "clock-frequency", &clk_rate);
>
> /* See if a Baud clock has been specified */
> - baud_mux_clk = of_clk_get_by_name(np, "sw_baud");
> + baud_mux_clk = devm_clk_get(dev, "sw_baud");

If switching to devm_clk_get(), maybe devm_clk_get_enabled() could also
be an option to fix both issues and avoid adding some LoC.

The order of operation in the remove function would then be different. I
don't know if it can be an issue.

Just my 2c.

CJ

> if (IS_ERR(baud_mux_clk)) {
> if (PTR_ERR(baud_mux_clk) == -EPROBE_DEFER) {
> ret = -EPROBE_DEFER;

2023-04-27 23:31:40

by Doug Berger

[permalink] [raw]
Subject: Re: [PATCH 2/2] serial: 8250_bcm7271: fix leak in `brcmuart_probe`

On 4/27/2023 1:29 PM, Christophe JAILLET wrote:
> Le 27/04/2023 à 20:19, Doug Berger a écrit :
>> Smatch reports:
>> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
>> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
>>
>> The issue is fixed by using a managed clock.
>>
>> Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom
>> STB driver")
>> Reported-by: XuDong Liu <[email protected]>
>> Link:
>> https://lore.kernel.org/lkml/[email protected]/
>> Signed-off-by: Doug Berger <[email protected]>
>> ---
>>   drivers/tty/serial/8250/8250_bcm7271.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/tty/serial/8250/8250_bcm7271.c
>> b/drivers/tty/serial/8250/8250_bcm7271.c
>> index 90ee7bc12f77..af0e1c070187 100644
>> --- a/drivers/tty/serial/8250/8250_bcm7271.c
>> +++ b/drivers/tty/serial/8250/8250_bcm7271.c
>> @@ -1012,7 +1012,7 @@ static int brcmuart_probe(struct platform_device
>> *pdev)
>>       of_property_read_u32(np, "clock-frequency", &clk_rate);
>>       /* See if a Baud clock has been specified */
>> -    baud_mux_clk = of_clk_get_by_name(np, "sw_baud");
>> +    baud_mux_clk = devm_clk_get(dev, "sw_baud");
>
> If switching to devm_clk_get(), maybe devm_clk_get_enabled() could also
> be an option to fix both issues and avoid adding some LoC.
>
> The order of operation in the remove function would then be different. I
> don't know if it can be an issue.
I like the idea, but it doesn't backport to the source of the error.
I'll try to remember to submit something after the merge closes.

>
> Just my 2c.
>
> CJ
Thanks!
Doug

2023-04-28 17:49:35

by Florian Fainelli

[permalink] [raw]
Subject: Re: [PATCH 1/2] serial: 8250_bcm7271: balance clk_enable calls

On 4/27/23 11:19, Doug Berger wrote:
> The sw_baud clock must be disabled when the device driver is not
> connected to the device. This now occurs when probe fails and
> upon remove.
>
> Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom STB driver")
> Reported-by: XuDong Liu <[email protected]>
> Link: https://lore.kernel.org/lkml/[email protected]/
> Signed-off-by: Doug Berger <[email protected]>

Acked-by: Florian Fainelli <[email protected]>
--
Florian

2023-04-28 17:49:44

by Florian Fainelli

[permalink] [raw]
Subject: Re: [PATCH 2/2] serial: 8250_bcm7271: fix leak in `brcmuart_probe`

On 4/27/23 11:19, Doug Berger wrote:
> Smatch reports:
> drivers/tty/serial/8250/8250_bcm7271.c:1120 brcmuart_probe() warn:
> 'baud_mux_clk' from clk_prepare_enable() not released on lines: 1032.
>
> The issue is fixed by using a managed clock.
>
> Fixes: 41a469482de2 ("serial: 8250: Add new 8250-core based Broadcom STB driver")
> Reported-by: XuDong Liu <[email protected]>
> Link: https://lore.kernel.org/lkml/[email protected]/
> Signed-off-by: Doug Berger <[email protected]>

Acked-by: Florian Fainelli <[email protected]>
--
Florian