mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error
@ 2023-06-10 15:59 Christophe JAILLET
  2023-06-10 15:59 ` [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Christophe JAILLET
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Christophe JAILLET @ 2023-06-10 15:59 UTC (permalink / raw)
  To: Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby,
	Thomas Abraham, Kukjin Kim
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, stable,
	Andi Shyti, linux-arm-kernel, linux-samsung-soc, linux-serial

If clk_get_rate() fails, the clk that has just been allocated needs to be
freed.

Cc: <stable@vger.kernel.org> # v3.3+
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Reviewed-by: Andi Shyti <andi.shyti@kernel.org>
Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
v2: Add an error message    [Andi Shyti]
    Add Cc: stable          [Andi Shyti]
    Add R-b tags

v1: https://lore.kernel.org/all/e4359d5ef206f5b349c1d15a515a1205e78dda55.1686285892.git.christophe.jaillet@wanadoo.fr/

Slightly unsure if Krzysztof's R-b should be kept or not.
v2 is not the same as v1, but the change looks small.
Sorry if I did wrong.
---
 drivers/tty/serial/samsung_tty.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c
index 2a7520ad3abd..a92a23e1964e 100644
--- a/drivers/tty/serial/samsung_tty.c
+++ b/drivers/tty/serial/samsung_tty.c
@@ -1459,8 +1459,12 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
 			continue;
 
 		rate = clk_get_rate(clk);
-		if (!rate)
+		if (!rate) {
+			dev_err(ourport->port.dev,
+				"Failed to get clock rate for %s.\n", clkname);
+			clk_put(clk);
 			continue;
+		}
 
 		if (ourport->info->has_divslot) {
 			unsigned long div = rate / req_baud;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk
  2023-06-10 15:59 [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Christophe JAILLET
@ 2023-06-10 15:59 ` Christophe JAILLET
  2023-06-12  5:27   ` Jiri Slaby
  2023-06-10 15:59 ` [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code Christophe JAILLET
  2023-06-12  5:27 ` [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Jiri Slaby
  2 siblings, 1 reply; 8+ messages in thread
From: Christophe JAILLET @ 2023-06-10 15:59 UTC (permalink / raw)
  To: Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby,
	Kukjin Kim, Thomas Abraham
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, stable,
	Andi Shyti, linux-arm-kernel, linux-samsung-soc, linux-serial

When the best clk is searched, we iterate over all possible clk.

If we find a better match, the previous one, if any, needs to be freed.
If a better match has already been found, we still need to free the new
one, otherwise it leaks.

Cc: <stable@vger.kernel.org> # v3.3+
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Reviewed-by: Andi Shyti <andi.shyti@kernel.org>
Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
I think that some clk_put() are also missing somewhere else in the driver
but won't be able to investigate further.

v2: No code change
    Add Cc: stable          [Andi Shyti, as suggested for patch 1/2]
    Add R-b tags

v1: https://lore.kernel.org/all/93bf8f574310256fcea50e5c5a62b5c37e20bb14.1686285892.git.christophe.jaillet@wanadoo.fr/
---
 drivers/tty/serial/samsung_tty.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c
index a92a23e1964e..0b37019820b4 100644
--- a/drivers/tty/serial/samsung_tty.c
+++ b/drivers/tty/serial/samsung_tty.c
@@ -1490,10 +1490,18 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
 			calc_deviation = -calc_deviation;
 
 		if (calc_deviation < deviation) {
+			/*
+			 * If we find a better clk, release the previous one, if
+			 * any.
+			 */
+			if (!IS_ERR(*best_clk))
+				clk_put(*best_clk);
 			*best_clk = clk;
 			best_quot = quot;
 			*clk_num = cnt;
 			deviation = calc_deviation;
+		} else {
+			clk_put(clk);
 		}
 	}
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code
  2023-06-10 15:59 [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Christophe JAILLET
  2023-06-10 15:59 ` [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Christophe JAILLET
@ 2023-06-10 15:59 ` Christophe JAILLET
  2023-06-10 16:12   ` Andi Shyti
  2023-06-12  5:13   ` Jiri Slaby
  2023-06-12  5:27 ` [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Jiri Slaby
  2 siblings, 2 replies; 8+ messages in thread
From: Christophe JAILLET @ 2023-06-10 15:59 UTC (permalink / raw)
  To: Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, Walter Harms,
	linux-arm-kernel, linux-samsung-soc, linux-serial

Use abs() instead of hand-writing it.

Suggested-by: Walter Harms <wharms@bfs.de>
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
v2: new patch
---
 drivers/tty/serial/samsung_tty.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c
index 0b37019820b4..b29e9dfd81a6 100644
--- a/drivers/tty/serial/samsung_tty.c
+++ b/drivers/tty/serial/samsung_tty.c
@@ -24,6 +24,7 @@
 #include <linux/dmaengine.h>
 #include <linux/dma-mapping.h>
 #include <linux/slab.h>
+#include <linux/math.h>
 #include <linux/module.h>
 #include <linux/ioport.h>
 #include <linux/io.h>
@@ -1485,9 +1486,7 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
 		}
 		quot--;
 
-		calc_deviation = req_baud - baud;
-		if (calc_deviation < 0)
-			calc_deviation = -calc_deviation;
+		calc_deviation = abs(req_baud - baud);
 
 		if (calc_deviation < deviation) {
 			/*
-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code
  2023-06-10 15:59 ` [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code Christophe JAILLET
@ 2023-06-10 16:12   ` Andi Shyti
  2023-06-12  5:13   ` Jiri Slaby
  1 sibling, 0 replies; 8+ messages in thread
From: Andi Shyti @ 2023-06-10 16:12 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby,
	linux-kernel, kernel-janitors, Walter Harms, linux-arm-kernel,
	linux-samsung-soc, linux-serial

Hi Christophe,

On Sat, Jun 10, 2023 at 05:59:27PM +0200, Christophe JAILLET wrote:
> Use abs() instead of hand-writing it.
> 
> Suggested-by: Walter Harms <wharms@bfs.de>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Reviewed-by: Andi Shyti <andi.shyti@kernel.org> 

Andi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code
  2023-06-10 15:59 ` [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code Christophe JAILLET
  2023-06-10 16:12   ` Andi Shyti
@ 2023-06-12  5:13   ` Jiri Slaby
  2023-06-12  5:31     ` Jiri Slaby
  1 sibling, 1 reply; 8+ messages in thread
From: Jiri Slaby @ 2023-06-12  5:13 UTC (permalink / raw)
  To: Christophe JAILLET, Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, Walter Harms, linux-arm-kernel,
	linux-samsung-soc, linux-serial

On 10. 06. 23, 17:59, Christophe JAILLET wrote:
> Use abs() instead of hand-writing it.
> 
> Suggested-by: Walter Harms <wharms@bfs.de>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> v2: new patch
> ---
>   drivers/tty/serial/samsung_tty.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c
> index 0b37019820b4..b29e9dfd81a6 100644
> --- a/drivers/tty/serial/samsung_tty.c
> +++ b/drivers/tty/serial/samsung_tty.c
> @@ -24,6 +24,7 @@
>   #include <linux/dmaengine.h>
>   #include <linux/dma-mapping.h>
>   #include <linux/slab.h>
> +#include <linux/math.h>
>   #include <linux/module.h>
>   #include <linux/ioport.h>
>   #include <linux/io.h>
> @@ -1485,9 +1486,7 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
>   		}
>   		quot--;
>   
> -		calc_deviation = req_baud - baud;
> -		if (calc_deviation < 0)
> -			calc_deviation = -calc_deviation;
> +		calc_deviation = abs(req_baud - baud);

Does this work provided req_baud and baud are unsigned?

thanks,
-- 
js
suse labs


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk
  2023-06-10 15:59 ` [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Christophe JAILLET
@ 2023-06-12  5:27   ` Jiri Slaby
  0 siblings, 0 replies; 8+ messages in thread
From: Jiri Slaby @ 2023-06-12  5:27 UTC (permalink / raw)
  To: Christophe JAILLET, Krzysztof Kozlowski, Alim Akhtar,
	Greg Kroah-Hartman, Kukjin Kim, Thomas Abraham
  Cc: linux-kernel, kernel-janitors, stable, Andi Shyti,
	linux-arm-kernel, linux-samsung-soc, linux-serial

On 10. 06. 23, 17:59, Christophe JAILLET wrote:
> When the best clk is searched, we iterate over all possible clk.
> 
> If we find a better match, the previous one, if any, needs to be freed.
> If a better match has already been found, we still need to free the new
> one, otherwise it leaks.
> 
> Cc: <stable@vger.kernel.org> # v3.3+
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> Reviewed-by: Andi Shyti <andi.shyti@kernel.org>
> Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Reviewed-by: Jiri Slaby <jirislaby@kernel.org>

thanks,
-- 
js
suse labs


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error
  2023-06-10 15:59 [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Christophe JAILLET
  2023-06-10 15:59 ` [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Christophe JAILLET
  2023-06-10 15:59 ` [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code Christophe JAILLET
@ 2023-06-12  5:27 ` Jiri Slaby
  2 siblings, 0 replies; 8+ messages in thread
From: Jiri Slaby @ 2023-06-12  5:27 UTC (permalink / raw)
  To: Christophe JAILLET, Krzysztof Kozlowski, Alim Akhtar,
	Greg Kroah-Hartman, Thomas Abraham, Kukjin Kim
  Cc: linux-kernel, kernel-janitors, stable, Andi Shyti,
	linux-arm-kernel, linux-samsung-soc, linux-serial

On 10. 06. 23, 17:59, Christophe JAILLET wrote:
> If clk_get_rate() fails, the clk that has just been allocated needs to be
> freed.
> 
> Cc: <stable@vger.kernel.org> # v3.3+
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> Reviewed-by: Andi Shyti <andi.shyti@kernel.org>
> Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Reviewed-by: Jiri Slaby <jirislaby@kernel.org>

thanks,
-- 
js
suse labs


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code
  2023-06-12  5:13   ` Jiri Slaby
@ 2023-06-12  5:31     ` Jiri Slaby
  0 siblings, 0 replies; 8+ messages in thread
From: Jiri Slaby @ 2023-06-12  5:31 UTC (permalink / raw)
  To: Christophe JAILLET, Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, Walter Harms, linux-arm-kernel,
	linux-samsung-soc, linux-serial

On 12. 06. 23, 7:13, Jiri Slaby wrote:
> On 10. 06. 23, 17:59, Christophe JAILLET wrote:
>> Use abs() instead of hand-writing it.
>>
>> Suggested-by: Walter Harms <wharms@bfs.de>
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>> v2: new patch
>> ---
>>   drivers/tty/serial/samsung_tty.c | 5 ++---
>>   1 file changed, 2 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/tty/serial/samsung_tty.c 
>> b/drivers/tty/serial/samsung_tty.c
>> index 0b37019820b4..b29e9dfd81a6 100644
>> --- a/drivers/tty/serial/samsung_tty.c
>> +++ b/drivers/tty/serial/samsung_tty.c
>> @@ -24,6 +24,7 @@
>>   #include <linux/dmaengine.h>
>>   #include <linux/dma-mapping.h>
>>   #include <linux/slab.h>
>> +#include <linux/math.h>
>>   #include <linux/module.h>
>>   #include <linux/ioport.h>
>>   #include <linux/io.h>
>> @@ -1485,9 +1486,7 @@ static unsigned int s3c24xx_serial_getclk(struct 
>> s3c24xx_uart_port *ourport,
>>           }
>>           quot--;
>> -        calc_deviation = req_baud - baud;
>> -        if (calc_deviation < 0)
>> -            calc_deviation = -calc_deviation;
>> +        calc_deviation = abs(req_baud - baud);
> 
> Does this work provided req_baud and baud are unsigned?

Oh, yes, it does, _hopefully_. A bit unexpectedly for me, but:
  * abs - return absolute value of an argument
  * @x: the value.  If it is unsigned type, it is converted to signed 
type first.

So:

Reviewed-by: Jiri Slaby <jirislaby@kernel.org>

It would help noting this in the commit log.

> thanks,-- 
js
suse labs


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-06-12  5:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-10 15:59 [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Christophe JAILLET
2023-06-10 15:59 ` [PATCH v2 2/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Christophe JAILLET
2023-06-12  5:27   ` Jiri Slaby
2023-06-10 15:59 ` [PATCH v2 3/3] tty: serial: samsung_tty: Use abs() to simplify some code Christophe JAILLET
2023-06-10 16:12   ` Andi Shyti
2023-06-12  5:13   ` Jiri Slaby
2023-06-12  5:31     ` Jiri Slaby
2023-06-12  5:27 ` [PATCH v2 1/3] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Jiri Slaby

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

all inboxes | Powered by JetHome®