[1/2] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error
Message ID | e4359d5ef206f5b349c1d15a515a1205e78dda55.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 k13csp714736vqr; Thu, 8 Jun 2023 21:59:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4/wnyjag+kW9dv8DfzFbdDzSVRGT7bY8Wtdj/OGrPvv0P+8QGT1fCTWZU9AL4MV2ho1iWy X-Received: by 2002:a17:90b:34c:b0:255:614a:7fae with SMTP id fh12-20020a17090b034c00b00255614a7faemr391591pjb.20.1686286787241; Thu, 08 Jun 2023 21:59:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686286787; cv=none; d=google.com; s=arc-20160816; b=p8wJP0zSa/UrqRlUbNpvd+/yW9Y2JFgvjlmlIa0DTEXmsXWYcSdU3/ZrviL5CZgU2s L0HD4c/Q980Jq7lvG5boXf7eP+iLPnHsgh6QOAOaUxfrf2jj4guLSw9/sh3wxv0ualjT Lidx9O4biZFR/RjgGZwz88LvzLUx4e7LcQrqHT8ZvyI6GQwyiH8ZXyyqvlG43WSo5I19 oIArP0zXr6NUMI1+BlqpkoLVHK/2SyvXuUIdN+n5203WcMIjd39UvdvLAx2yv2f14IT0 vOkBvb3mrPreajhFHKakBS1mD6PlcrydE1zL55tX1UrroVm4APOpiQsKal7un3+IiT4I zr2Q== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=UkPuf1cEggfUhF63kQO/8xolTLGbzwLNxFq6qhE7R0c=; b=xmVVnooyRw1VpjNFPtgCau9+mOLAZpECRpPbfypMnLpzoZIhbzCMJwBr24AaY/A56q IOfURU5xGcpflNEKsHaBiZjrt6yi1T+GWZVE3BWcIFYLvt5yoTrZBkHg9YvN4sHaB97h aCrwJtgXdUQcr4r7+4JT275bgWpukLyRVFDKiMlg5OfTIQI6l0T95T560OtL4HfUDjTe F7jCqWwt4GkU7itk+Twssuj+CzWKzS96IJ8/m5qT2oTIM2KQpoLdlMVcpgiSv2R/jNoU wJcifr3y32jtOuArGbF0W2uUUXdZmY0m3bMFDN06ZYkVJX0LFlR3d0RmuAnnEkWqnsK4 tS/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=ID8zcjZT; 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 h16-20020a17090adb9000b002507cbb009bsi3822232pjv.112.2023.06.08.21.59.32; Thu, 08 Jun 2023 21:59:47 -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=ID8zcjZT; 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 S236492AbjFIEpw (ORCPT <rfc822;literming00@gmail.com> + 99 others); Fri, 9 Jun 2023 00:45:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234505AbjFIEpt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 9 Jun 2023 00:45:49 -0400 Received: from smtp.smtpout.orange.fr (smtp-19.smtpout.orange.fr [80.12.242.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 934D830ED for <linux-kernel@vger.kernel.org>; Thu, 8 Jun 2023 21:45:45 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id 7U0KqPBjCDGHG7U0Kq4YGn; Fri, 09 Jun 2023 06:45:43 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1686285943; bh=UkPuf1cEggfUhF63kQO/8xolTLGbzwLNxFq6qhE7R0c=; h=From:To:Cc:Subject:Date; b=ID8zcjZT9VNLSLnTBfTRFr7XdAJhLgGQQTZmuTkb8ZRx1I4puHUr3FQjEy9dizrt1 +7adlgaC23iIECotO7NMgpk6j7KCOEfm3jRftQprT2A53sWY5bt3RaOketL6bUfAD5 VOxq1Q2AFB7OXRMufd0PVN3LlKc8Txd1YBW/wQFCGRaf4qVdgn5ZhNXIF/jJ+50DOs ghNrWe8JWisDPa66b89R+/ZFNK6uBEpAARxgn5xwkfxV0+Ap9z+ileXfnpMkWSPSOs x8NPJdeXGHRNnDwW6n8G+sRF44HeyZkdQwJDEnTF16cnPXLuz922jTX7m+hoJxBYOs Cc0p82GxdZrEQ== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Fri, 09 Jun 2023 06:45:43 +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 1/2] tty: serial: samsung_tty: Fix a memory leak in s3c24xx_serial_getclk() in case of error Date: Fri, 9 Jun 2023 06:45:38 +0200 Message-Id: <e4359d5ef206f5b349c1d15a515a1205e78dda55.1686285892.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 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=unavailable 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?1768199854329953782?= X-GMAIL-MSGID: =?utf-8?q?1768199854329953782?= |
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
If clk_get_rate() fails, the clk that has just been allocated needs to be
freed.
Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
drivers/tty/serial/samsung_tty.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
Comments
On 09/06/2023 06:45, Christophe JAILLET wrote: > If clk_get_rate() fails, the clk that has just been allocated needs to be > freed. > > Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Best regards, Krzysztof
Hi Christophe, On Fri, Jun 09, 2023 at 06:45:38AM +0200, Christophe JAILLET wrote: > If clk_get_rate() fails, the clk that has just been allocated needs to be > freed. > > Fixes: 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup") > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> As this is a fix, can you cc the stable kernel Cc: <stable@vger.kernel.org> # v3.3+ > --- > drivers/tty/serial/samsung_tty.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c > index 2a7520ad3abd..dd751e7010e3 100644 > --- a/drivers/tty/serial/samsung_tty.c > +++ b/drivers/tty/serial/samsung_tty.c > @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, > continue; > > rate = clk_get_rate(clk); > - if (!rate) > + if (!rate) { > + clk_put(clk); > continue; could you also print an error here? In any case: Reviewed-by: Andi Shyti <andi.shyti@kernel.org> Andi > + } > > if (ourport->info->has_divslot) { > unsigned long div = rate / req_baud; > -- > 2.34.1 >
Le 10/06/2023 à 12:26, Andi Shyti a écrit : >> @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, >> continue; >> >> rate = clk_get_rate(clk); >> - if (!rate) >> + if (!rate) { >> + clk_put(clk); >> continue; > > could you also print an error here? > Is: dev_err(ourport->port.dev, "Failed to get clock rate for %s.\n", clkname); fine for you? CJ
On Sat, Jun 10, 2023 at 04:07:51PM +0200, Christophe JAILLET wrote: > Le 10/06/2023 à 12:26, Andi Shyti a écrit : > > > @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, > > > continue; > > > rate = clk_get_rate(clk); > > > - if (!rate) > > > + if (!rate) { > > > + clk_put(clk); > > > continue; > > > > could you also print an error here? > > > > Is: > dev_err(ourport->port.dev, > "Failed to get clock rate for %s.\n", clkname); Fantastic! Thanks! Andi
On 10/06/2023 16:54, Andi Shyti wrote: > On Sat, Jun 10, 2023 at 04:07:51PM +0200, Christophe JAILLET wrote: >> Le 10/06/2023 à 12:26, Andi Shyti a écrit : >>>> @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, >>>> continue; >>>> rate = clk_get_rate(clk); >>>> - if (!rate) >>>> + if (!rate) { >>>> + clk_put(clk); >>>> continue; >>> >>> could you also print an error here? >>> >> >> Is: >> dev_err(ourport->port.dev, >> "Failed to get clock rate for %s.\n", clkname); Why do we need it? Most of other users of clk_get_rate() don't print. Probably because such condition is highly unlikely if not impossible. This makes simple function unnecessarily bigger... Best regards, Krzysztof
On Sat, Jun 10, 2023 at 06:23:58PM +0200, Krzysztof Kozlowski wrote: > On 10/06/2023 16:54, Andi Shyti wrote: > > On Sat, Jun 10, 2023 at 04:07:51PM +0200, Christophe JAILLET wrote: > >> Le 10/06/2023 à 12:26, Andi Shyti a écrit : > >>>> @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, > >>>> continue; > >>>> rate = clk_get_rate(clk); > >>>> - if (!rate) > >>>> + if (!rate) { > >>>> + clk_put(clk); > >>>> continue; > >>> > >>> could you also print an error here? > >>> > >> > >> Is: > >> dev_err(ourport->port.dev, > >> "Failed to get clock rate for %s.\n", clkname); > > Why do we need it? Most of other users of clk_get_rate() don't print. that's not a reason not to print it. > Probably because such condition is highly unlikely if not impossible. still... that's not a reason not to print it. All errors are unlikely and if it's unlikely, why there is no unlikely(!rate)? Which doesn't improve the reason not to print it. The more unlikely, the lauder you need to be: WARN_ON(!rate)... maybe too much! BUG_ON(!rate)... way too much! But these are inversely proportional to the likeliness of the error. > This makes simple function unnecessarily bigger... and... that's not a reason not to print it :) If it's needed, it's needed. If we are considering the error, then we need to treat it as an error. In any case, I'm not strong with it, indeed, I r-b it anyway. I personally prefer and suggested printing the error. Up to Christophe. Thanks, Andi
On 10/06/2023 19:10, Andi Shyti wrote: > On Sat, Jun 10, 2023 at 06:23:58PM +0200, Krzysztof Kozlowski wrote: >> On 10/06/2023 16:54, Andi Shyti wrote: >>> On Sat, Jun 10, 2023 at 04:07:51PM +0200, Christophe JAILLET wrote: >>>> Le 10/06/2023 à 12:26, Andi Shyti a écrit : >>>>>> @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, >>>>>> continue; >>>>>> rate = clk_get_rate(clk); >>>>>> - if (!rate) >>>>>> + if (!rate) { >>>>>> + clk_put(clk); >>>>>> continue; >>>>> >>>>> could you also print an error here? >>>>> >>>> >>>> Is: >>>> dev_err(ourport->port.dev, >>>> "Failed to get clock rate for %s.\n", clkname); >> >> Why do we need it? Most of other users of clk_get_rate() don't print. > > that's not a reason not to print it. This is the reason, because it was the conscious choice - not to print, otherwise drivers are unreadable. > >> Probably because such condition is highly unlikely if not impossible. > > still... that's not a reason not to print it. It is a reason not to print it in the driver. Code readability is more important than adding error messages for every possible case in the driver. > > All errors are unlikely and if it's unlikely, why there is no > unlikely(!rate)? Which doesn't improve the reason not to print > it. > > The more unlikely, the lauder you need to be: > > WARN_ON(!rate)... maybe too much! > BUG_ON(!rate)... way too much! > > But these are inversely proportional to the likeliness of the > error. > >> This makes simple function unnecessarily bigger... > > and... that's not a reason not to print it :) This is the reason not to print it in the driver, because it makes the code less maintainable. Such unlikely errors should be handled by core, not by every driver. If this error message here is reasonable, I would argue that it is reasonable to add it to other places... try doing it. You will see to what silly code it leads. It's like adding dev_err to regmap_mmio read/write failures - code will be difficult to read. Best regards, Krzysztof
Le 10/06/2023 à 19:10, Andi Shyti a écrit : > On Sat, Jun 10, 2023 at 06:23:58PM +0200, Krzysztof Kozlowski wrote: >> On 10/06/2023 16:54, Andi Shyti wrote: >>> On Sat, Jun 10, 2023 at 04:07:51PM +0200, Christophe JAILLET wrote: >>>> Le 10/06/2023 à 12:26, Andi Shyti a écrit : >>>>>> @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, >>>>>> continue; >>>>>> rate = clk_get_rate(clk); >>>>>> - if (!rate) >>>>>> + if (!rate) { >>>>>> + clk_put(clk); >>>>>> continue; >>>>> >>>>> could you also print an error here? >>>>> >>>> >>>> Is: >>>> dev_err(ourport->port.dev, >>>> "Failed to get clock rate for %s.\n", clkname); >> >> Why do we need it? Most of other users of clk_get_rate() don't print. > > that's not a reason not to print it. > >> Probably because such condition is highly unlikely if not impossible. > > still... that's not a reason not to print it. > > All errors are unlikely and if it's unlikely, why there is no > unlikely(!rate)? Which doesn't improve the reason not to print > it. > > The more unlikely, the lauder you need to be: > > WARN_ON(!rate)... maybe too much! > BUG_ON(!rate)... way too much! > > But these are inversely proportional to the likeliness of the > error. > >> This makes simple function unnecessarily bigger... > > and... that's not a reason not to print it :) > > If it's needed, it's needed. If we are considering the error, > then we need to treat it as an error. > > In any case, I'm not strong with it, indeed, I r-b it anyway. I > personally prefer and suggested printing the error. Up to > Christophe. git grep -A5 clk_get_rate | grep dev_err | wc -l 173 git grep clk_get_rate | wc -l 1464 (+ Krzysztof's argumentation) So lets go for v1. Can v1 be taken as is? (knowing that I don't really care about the new 3/3 related to abs()) Or should I send a v3 to ease the process? CJ > > Thanks, > Andi >
On Sat, Jun 10, 2023 at 07:10:15PM +0200, Andi Shyti wrote: > All errors are unlikely and if it's unlikely, why there is no > unlikely(!rate)? The likely/unlikely() annotations help performance at the expense of readability. If they improved readability every if statement would have them. They should only be used if it makes a difference in a benchmark. I think I have heard other people say the rule is that they shouldn't be used in the drivers/ directory. Also the other thing to consider is that quite often GCC is clever enough to figure out which paths are success paths and which are failure paths. So sometimes adding the annotation is redundant. regards, dan carpenter
diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c index 2a7520ad3abd..dd751e7010e3 100644 --- a/drivers/tty/serial/samsung_tty.c +++ b/drivers/tty/serial/samsung_tty.c @@ -1459,8 +1459,10 @@ static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, continue; rate = clk_get_rate(clk); - if (!rate) + if (!rate) { + clk_put(clk); continue; + } if (ourport->info->has_divslot) { unsigned long div = rate / req_baud;