[2/2] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk
Message ID | 93bf8f574310256fcea50e5c5a62b5c37e20bb14.1686285892.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp714039vqr; Thu, 8 Jun 2023 21:57:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6UpRr8XugtWZOhNDMX/5juPZ3hYUgGvpnUANuQ3f/q+l1BZuwq2CGwZVC4Z+19nBF+vLKo X-Received: by 2002:a05:6830:104d:b0:6b1:5fa8:77ee with SMTP id b13-20020a056830104d00b006b15fa877eemr479969otp.10.1686286625059; Thu, 08 Jun 2023 21:57:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686286625; cv=none; d=google.com; s=arc-20160816; b=zNX5OJXIrudQkQbqcI9E1ZLdqsYtg3x8fKeQzxwnxMjHwswho2m7/PvQaerwR66Z1d tv8dmmn2pjK2/jPiFwPn29SzXnI1O0I3eUyWjcbQvas6GrDF/iARFUxrsbK6SI37n0Yh NyMp3g2+23PaOTNTakrPJn4OQiFiK7ca94r5CCF1T9p0+pSZ8wt8mqyc4yPslJiwoI3g 52WY8jJ7J17rpkpqZFXwBM5yB55cJyn+ECb7Kz75n7mJ1Hvni46EQDVTuU7ZauQJNgu6 BToWFB11Ty9y9+j62AH/2K5bJ9lK3Rj/mwLA3BM5yZ73XTV38ac9rKt7N3nBBYybQnqm uX9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PEZXQDap3pTmCCLCcgCzrjYNlvmhAuSx1kWCIhd8mYU=; b=xcftvBJGg23sfRagOQ2Q+SwSNCsvRpz8/O4xk/VQC7rDFvnMOajUur9m9Ylgdi0ii4 jXqVnDWsp4t2LbpI8eS5b4wWJYmJTefUAd4aN78kQ01nQHsAVgmw19RcvbpZZN8v1sVy WDcdlXC/jIwNQWyQHkPu9hbIAQ5FZVqvOSlgd8Jj4b44VMrQ8BpT5p0Z6fzaN5HZ5WFl NtpdFqPYr/30GZcP5tUHhpoEzZtfraaZ/DlhiO7LKt0mfKwqJIBRFWzVegGHHua5O4+h N6U8hbZ290YqFm9LrOPFvqWNiUOFK+7iZTuSXM4RbyYJCYzCttzI6qK4iMYm5vTap/EN DLBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b="MNXndMF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z28-20020aa7959c000000b0066356d0888csi1810324pfj.113.2023.06.08.21.56.49; Thu, 08 Jun 2023 21:57:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b="MNXndMF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237142AbjFIEp6 (ORCPT <rfc822;literming00@gmail.com> + 99 others); Fri, 9 Jun 2023 00:45:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236735AbjFIEpy (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 9 Jun 2023 00:45:54 -0400 Received: from smtp.smtpout.orange.fr (smtp-19.smtpout.orange.fr [80.12.242.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3CE630EC for <linux-kernel@vger.kernel.org>; Thu, 8 Jun 2023 21:45:53 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id 7U0KqPBjCDGHG7U0Wq4YHb; Fri, 09 Jun 2023 06:45:52 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1686285952; bh=PEZXQDap3pTmCCLCcgCzrjYNlvmhAuSx1kWCIhd8mYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MNXndMF/jixUheplVtvmjAeKNdqfUNPyGiPl/BQL6RB8FmYTuDSD0XJmujKY+yCAI K8mm0h/Hajyw1zqBxN2m49rlL2K7GdkZHpirX8vjjktGce6QqrHJ2urq8Kbw1oFoI9 d2BG20yCDVqYhP+vEZa1utxMtwu9T7VINxnej2aA2xyqPiIjc0Df7W0qYLbgw211M0 6SmE2i5hucStjfdLlyJN2J+2o2udu9+UeBaAQOD3JcgLWLUWXt8cnfb29x4oS3XqIs cqUPy0HgV5AHSz+16w13cITmb50wgy8gu8KfTMBW3Reosssbtncwh9BThkQOggKGQi 38sMb7mq0Pp+w== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Fri, 09 Jun 2023 06:45:52 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>, Alim Akhtar <alim.akhtar@samsung.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Jiri Slaby <jirislaby@kernel.org>, Thomas Abraham <thomas.abraham@linaro.org>, Kukjin Kim <kgene.kim@samsung.com> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH 2/2] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() when iterating clk Date: Fri, 9 Jun 2023 06:45:39 +0200 Message-Id: <93bf8f574310256fcea50e5c5a62b5c37e20bb14.1686285892.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <e4359d5ef206f5b349c1d15a515a1205e78dda55.1686285892.git.christophe.jaillet@wanadoo.fr> References: <e4359d5ef206f5b349c1d15a515a1205e78dda55.1686285892.git.christophe.jaillet@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768199683981639536?= X-GMAIL-MSGID: =?utf-8?q?1768199683981639536?= |
Series |
[1/2] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error
|
|
Commit Message
Christophe JAILLET
June 9, 2023, 4:45 a.m. UTC
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.
Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch is speculative. Review with care.
I think that some clk_put() are also missing somewhere else in the driver
but won't be able to investigate further.
---
drivers/tty/serial/samsung_tty.c | 8 ++++++++
1 file changed, 8 insertions(+)
Comments
On 09/06/2023 06:45, 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. > > Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > This patch is speculative. Review with care. > Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Best regards, Krzysztof
On 09/06/2023 10:57, Walter Harms wrote: > > while we are here .... > > perhaps INT_MAX from kernel.h ? > > int deviation = (1 << 30) - 1; > > the part before looks a bit strange > > if (ourport->info->has_divslot) { > unsigned long div = rate / req_baud; > > /* The UDIVSLOT register on the newer UARTs allows us to > * get a divisor adjustment of 1/16th on the baud clock. > * > * We don't keep the UDIVSLOT value (the 16ths we > * calculated by not multiplying the baud by 16) as it > * is easy enough to recalculate. > */ > > quot = div / 16; > baud = rate / div; > because > baud=rate/rate/req_baud = req_baud > can this be simplyfied ? (or is the numeric required ?) > > > Homebrew abs() kernel.h has a abs() can we use it here ? > > if (calc_deviation < 0) > calc_deviation = -calc_deviation; > > to the patch: > > + /* > + * If we find a better clk, release the previous one, if > + * any. > + */ > + if (!IS_ERR(*best_clk)) > + clk_put(*best_clk); > > the intentions are good. *best_clk is user supplied (and should be NULL) > filled & released in the next round but IMHO must be valid (is clk). > so no need to check. (ntl clk_put seems to handle NULL and ERR ) > if (!clk || WARN_ON_ONCE(IS_ERR(clk))) > return; Don't top-post. Anyway, I don't understand what you want to say here. Best regards, Krzysztof
Hi Christophe, On Fri, Jun 09, 2023 at 06:45:39AM +0200, 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. > > Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > This patch is speculative. Review with care. > > I think that some clk_put() are also missing somewhere else in the driver > but won't be able to investigate further. > --- > 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 dd751e7010e3..c07877dd25fa 100644 > --- a/drivers/tty/serial/samsung_tty.c > +++ b/drivers/tty/serial/samsung_tty.c > @@ -1488,10 +1488,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)) what is the case when *best_clk has an error in it? Andi > + clk_put(*best_clk); > *best_clk = clk; > best_quot = quot; > *clk_num = cnt; > deviation = calc_deviation; > + } else { > + clk_put(clk); > } > } > > -- > 2.34.1 >
On 10/06/2023 12:39, Andi Shyti wrote: > Hi Christophe, > > On Fri, Jun 09, 2023 at 06:45:39AM +0200, 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. >> >> Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") >> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> >> --- >> This patch is speculative. Review with care. >> >> I think that some clk_put() are also missing somewhere else in the driver >> but won't be able to investigate further. >> --- >> 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 dd751e7010e3..c07877dd25fa 100644 >> --- a/drivers/tty/serial/samsung_tty.c >> +++ b/drivers/tty/serial/samsung_tty.c >> @@ -1488,10 +1488,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)) > > what is the case when *best_clk has an error in it? The initial one? Open the place where the function is being called. Best regards, Krzysztof
Hi Krzysztof, > On Sat, Jun 10, 2023 at 12:45:53PM +0200, Krzysztof Kozlowski wrote: > On 10/06/2023 12:39, Andi Shyti wrote: > > Hi Christophe, > > > > On Fri, Jun 09, 2023 at 06:45:39AM +0200, 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. > >> > >> Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") > >> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > >> --- > >> This patch is speculative. Review with care. > >> > >> I think that some clk_put() are also missing somewhere else in the driver > >> but won't be able to investigate further. > >> --- > >> 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 dd751e7010e3..c07877dd25fa 100644 > >> --- a/drivers/tty/serial/samsung_tty.c > >> +++ b/drivers/tty/serial/samsung_tty.c > >> @@ -1488,10 +1488,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)) > > > > what is the case when *best_clk has an error in it? > > The initial one? Open the place where the function is being called. Right! Reviewed-by: Andi Shyti <andi.shyti@kernel.org> Andi > > Best regards, > Krzysztof >
diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c index dd751e7010e3..c07877dd25fa 100644 --- a/drivers/tty/serial/samsung_tty.c +++ b/drivers/tty/serial/samsung_tty.c @@ -1488,10 +1488,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); } }