Message ID | 20230324172427.3416342-1-harperchen1110@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp799288vqo; Fri, 24 Mar 2023 10:35:57 -0700 (PDT) X-Google-Smtp-Source: AKy350YlUZnmLS1M8t/tQgutNOi5lMVatcXhXRD+qdk0zr9RuTn9mbWixi8ICwa4bwg6xaM831LI X-Received: by 2002:a17:90a:b397:b0:23f:7ff6:eba with SMTP id e23-20020a17090ab39700b0023f7ff60ebamr3885136pjr.0.1679679357021; Fri, 24 Mar 2023 10:35:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679679357; cv=none; d=google.com; s=arc-20160816; b=B9WoDkC1u97iAvmKr5Rw4khzoSDjW5OfDXw5AHvT89Il/ebwdl/1fG8TfB7zv5gn0V NOu5sN0japc51GAE9P77nyGD91DTDl9acbBR9HWrdRhj0zi7jjnRW5dc0x/EejBECSM6 7wyR3LGlduTonbTNOsE1aZ/dJCabygAj1qmeu0oR7s1QMjXM86to4KsTLvUce9Jm4RjQ Z7bRXDKEG0xWTrf/jYMAltiIZVLWdaFkTBJcutTrIwSVPnI2SdWgT4bGwgEeUvG/0JrG LT8xXgQHsAtIpRYApOWd7cvEqAO7sEZu7NivKtJUI38RYGu1xgsvYcOlWRFwCLudBo+x dmMA== 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=6kvgynts9pGunaYTqyxyW8b1uVo2ebWpPqOLNHUAp5s=; b=TJUKW0pgTJpj6U6ECk6DC77+1TsbTN88i4TKRezEl+Si+bZEuB5lGMTICbf0cCm5vu xy7DpL9j5JRia0fYoZX5nTcefYX7xACJ1SkIERl1nZgsvCqR0G4XAzY/6cRuAYXS8wP2 EeGgBhmx1/xS0FzBxbrtKljngu2quwnmJDPJqXcKzulKgfWLeTZ4Qzttjxuf/f5CXzEF uMu/Lc/0PbzKbrlS6vrVfxSP0jw/dAz1dqnBNcvxaCrrix0xRCHtkf5gvHezFxqxIkc5 2HjDOuJ4P7LDRRya84wlv3GHqDGYUDOTkHonNOFvq1o1GqwDLX+BqmJN4Ka2LUOMsgE2 ULOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=oB+95YCd; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hg3-20020a17090b300300b002343565e8b4si234771pjb.122.2023.03.24.10.35.44; Fri, 24 Mar 2023 10:35:56 -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=@gmail.com header.s=20210112 header.b=oB+95YCd; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231794AbjCXRYp (ORCPT <rfc822;makky5685@gmail.com> + 99 others); Fri, 24 Mar 2023 13:24:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231718AbjCXRYm (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 24 Mar 2023 13:24:42 -0400 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FE7118B0F; Fri, 24 Mar 2023 10:24:35 -0700 (PDT) Received: by mail-ot1-x332.google.com with SMTP id m20-20020a9d6094000000b0069caf591747so1258056otj.2; Fri, 24 Mar 2023 10:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679678674; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6kvgynts9pGunaYTqyxyW8b1uVo2ebWpPqOLNHUAp5s=; b=oB+95YCdnFSnDNUAsmd3akZYut4IbwPvelIZLYP3hiX0NQCJJ6AgqdHo4hFOQiQGCS hzzPfqMKlfTr9PXTDc6vufgbr2Yilw7biRtdlDfZxHSwo6WEh+dcKorVWSfYOLy+WnEE pOPfH8KZARyRRpnR2xUDAw00GgMeD5Bed74rCQWBw6pBpYkFYsyHiz6GtuH5tBQ9DKA6 SWPjr6iwRZqDbLPXiI5uWvO51wT26c7hMu9Hz1jzcfwi3gkD32W8hMwVcxhv2vXrKtod 7QB8AQukpAPhoD6nMFqx9xaOEOoVzeXN/2Cj/xICn3LkQmr1WBOLEqlJ9krwe8q7/qJL jggA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679678674; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6kvgynts9pGunaYTqyxyW8b1uVo2ebWpPqOLNHUAp5s=; b=a/w3FDLS1RtrfbiVeD+MW4bXa/ZpYwGsMQiUXVTjRt6ZKqPdeMFuTTku1cR0sHrql+ TnwV0+2f/kYZSkTZHts9+Up+zqZEfvEkI5Km6o4KCarpWh3tFhWXEoW/ZH2uyexDFUaD Cb6kzfQAGoVOiEBKDk5bCbTasBDK96eN3JudXC2AowDpRrTj1mViSEkIbCWkOVQ+6DF2 8rMQkNQRCsoZGilBnhg0OX1CAn4Hn2+Un+YivR5nxOC/UP4AzvjfTu/C4ppVSYDzcbh5 kuL8FpIbxjzzYgATpxB1uYH6j1ePs4lw2aFT9Fq6M9iUmtCtr9AcokSvoePwpXIt3krk EMKA== X-Gm-Message-State: AO0yUKXiJNj3WWCloQs549Wd9iEBzIvzJkMBgFGDCvwH29AsrtihcBIJ mHI0eoOvMoMXb4wtjqHBPS0= X-Received: by 2002:a05:6830:1da6:b0:69d:8f5e:b24b with SMTP id z6-20020a0568301da600b0069d8f5eb24bmr2223161oti.25.1679678674500; Fri, 24 Mar 2023 10:24:34 -0700 (PDT) Received: from chcpu13.cse.ust.hk (191host119.mobilenet.cse.ust.hk. [143.89.191.119]) by smtp.gmail.com with ESMTPSA id w16-20020a9d77d0000000b006a11dd6d2c1sm102045otl.29.2023.03.24.10.24.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 10:24:34 -0700 (PDT) From: Wei Chen <harperchen1110@gmail.com> To: martink@posteo.de Cc: mchehab@kernel.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Chen <harperchen1110@gmail.com> Subject: [PATCH] media: hi846: Fix memleak in hi846_init_controls() Date: Fri, 24 Mar 2023 17:24:27 +0000 Message-Id: <20230324172427.3416342-1-harperchen1110@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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?1761271461502378734?= X-GMAIL-MSGID: =?utf-8?q?1761271461502378734?= |
Series |
media: hi846: Fix memleak in hi846_init_controls()
|
|
Commit Message
Wei Chen
March 24, 2023, 5:24 p.m. UTC
hi846_init_controls doesn't clean the allocated ctrl_hdlr
in case there is a failure, which causes memleak. Add
v4l2_ctrl_handler_free to free the resource properly.
Signed-off-by: Wei Chen <harperchen1110@gmail.com>
---
drivers/media/i2c/hi846.c | 1 +
1 file changed, 1 insertion(+)
Comments
Am Freitag, dem 24.03.2023 um 17:24 +0000 schrieb Wei Chen: > hi846_init_controls doesn't clean the allocated ctrl_hdlr > in case there is a failure, which causes memleak. Add > v4l2_ctrl_handler_free to free the resource properly. > > Signed-off-by: Wei Chen <harperchen1110@gmail.com> > --- > drivers/media/i2c/hi846.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c > index 7c61873b7198..c45a6511d2c1 100644 > --- a/drivers/media/i2c/hi846.c > +++ b/drivers/media/i2c/hi846.c > @@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846 > *hi846) > if (ctrl_hdlr->error) { > dev_err(&client->dev, "v4l ctrl handler error: %d\n", > ctrl_hdlr->error); > + v4l2_ctrl_handler_free(ctrl_hdlr); > return ctrl_hdlr->error; > } > hi Wei, thanks for the patch. It looks like I forgot that indeed, but to me it looks like the subsequent error paths of v4l2_fwnode_device_parse() and v4l2_ctrl_new_fwnode_properties() would leak the same thing. And since we only assign it to the subdev's ctrl_handler in the success case later, you could simply add an error label: @@ -1663,21 +1663,26 @@ static int hi846_init_controls(struct hi846 *hi846) if (ctrl_hdlr->error) { dev_err(&client->dev, "v4l ctrl handler error: %d\n", ctrl_hdlr->error); - return ctrl_hdlr->error; + ret = ctrl_hdlr->error; + goto error; } ret = v4l2_fwnode_device_parse(&client->dev, &props); if (ret) - return ret; + goto error; ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &hi846_ctrl_ops, &props); if (ret) - return ret; + goto error; hi846->sd.ctrl_handler = ctrl_hdlr; return 0; + +error: + v4l2_ctrl_handler_free(ctrl_hdlr); + return ret; } what do you think? thanks! martin
Dear Martin, Thanks for pointing out my mistake. I cross-checked other implementations of drivers under media/i2c. I agree with you. Let me send the second version of my patch. Thanks, Wei On Sun, 26 Mar 2023 at 16:50, Martin Kepplinger <martink@posteo.de> wrote: > > Am Freitag, dem 24.03.2023 um 17:24 +0000 schrieb Wei Chen: > > hi846_init_controls doesn't clean the allocated ctrl_hdlr > > in case there is a failure, which causes memleak. Add > > v4l2_ctrl_handler_free to free the resource properly. > > > > Signed-off-by: Wei Chen <harperchen1110@gmail.com> > > --- > > drivers/media/i2c/hi846.c | 1 + > > 1 file changed, 1 insertion(+) > > > > diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c > > index 7c61873b7198..c45a6511d2c1 100644 > > --- a/drivers/media/i2c/hi846.c > > +++ b/drivers/media/i2c/hi846.c > > @@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846 > > *hi846) > > if (ctrl_hdlr->error) { > > dev_err(&client->dev, "v4l ctrl handler error: %d\n", > > ctrl_hdlr->error); > > + v4l2_ctrl_handler_free(ctrl_hdlr); > > return ctrl_hdlr->error; > > } > > > > hi Wei, thanks for the patch. It looks like I forgot that indeed, but > to me it looks like the subsequent error paths of > v4l2_fwnode_device_parse() and v4l2_ctrl_new_fwnode_properties() would > leak the same thing. > > And since we only assign it to the subdev's ctrl_handler in the success > case later, you could simply add an error label: > > > @@ -1663,21 +1663,26 @@ static int hi846_init_controls(struct hi846 > *hi846) > if (ctrl_hdlr->error) { > dev_err(&client->dev, "v4l ctrl handler error: %d\n", > ctrl_hdlr->error); > - return ctrl_hdlr->error; > + ret = ctrl_hdlr->error; > + goto error; > } > > ret = v4l2_fwnode_device_parse(&client->dev, &props); > if (ret) > - return ret; > + goto error; > > ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, > &hi846_ctrl_ops, > &props); > if (ret) > - return ret; > + goto error; > > hi846->sd.ctrl_handler = ctrl_hdlr; > > return 0; > + > +error: > + v4l2_ctrl_handler_free(ctrl_hdlr); > + return ret; > } > > > what do you think? thanks! > > martin > >
diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c index 7c61873b7198..c45a6511d2c1 100644 --- a/drivers/media/i2c/hi846.c +++ b/drivers/media/i2c/hi846.c @@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846 *hi846) if (ctrl_hdlr->error) { dev_err(&client->dev, "v4l ctrl handler error: %d\n", ctrl_hdlr->error); + v4l2_ctrl_handler_free(ctrl_hdlr); return ctrl_hdlr->error; }