From patchwork Fri Sep 29 09:19:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 146486 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3928217vqu; Fri, 29 Sep 2023 03:43:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHc7peEvmw6QIo93gnpRRO7Afcck6D0KtbeN7gJV7sceO+rJxiwP/U1SLfPjTzBjdyrdnep X-Received: by 2002:a05:6e02:20cb:b0:349:36e1:10fb with SMTP id 11-20020a056e0220cb00b0034936e110fbmr4167507ilq.18.1695984196951; Fri, 29 Sep 2023 03:43:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695984196; cv=none; d=google.com; s=arc-20160816; b=mTMWCUtqGgOZL/9S6IMaMp+xDN6Azzn8k51Tw2xI4dv835WWUjs47IY9QGtrMsIg5S ttb3bd0wVSCnm34ryxIdYgZHclb964mhUCNAagoGlrHjZi6gZPkxq9y368uIxgwD9Snh VatwNioNGB2zJpjL2MNeuJav0boeyQHnD10Byh2w2tDPLtD76MAKkoM75W946xQ8wUqE 59S5/OUXD3sa7MHQz8nO/DoAPAg+Ox50/FVCSdn+gcKVYGXHMtNOTpCuLwLIaFYg+oW4 rLr//E/F4FHCmdOAUOwl2vkMIfoDKr6Wh3Jlr6FP3vd1iTwOijFn+ydohXdYLrFbLj62 GuuQ== 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=CPCOX8Pk808b+bp768FKhaTdrGjFDu5eVaZMfKQbRpU=; fh=XBuHK3IJ1Ei0gfafkHS9yTkx0Pa9cT/3h2lpxnizSiI=; b=jV2XtvQqybc0Llw8x0EmpZ7IG2B+Trq8HexMg8AnccXDkC5XGkaGsjxH9wxzf7fAM/ 7F+qc3MUP7JZA+eqjHGg8mYT9SBkizgpinJc7PxbVMP/mJEiFvEkMD8BJt21RC9NwT42 aIviXy6GiKK4+aQHmY6NK7QAA0WHPCCVm8lB3Z+OT7vv173WZahfn8xNwnKvuceJulOl bj92HCge7UawfPbMLnfyvzswbZn2n2Y2ODpgA634DYqjAnENx4s+aZUmwKqHd4aG1U8/ 896aM2OeHtZcN166ebo2KKDoXKRTVEBdfGcHw+Hxta4ZPeSBsWr9jgSyDaEnge/FT1bI qqmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sang-engineering.com header.s=k1 header.b=TNnoYa3i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id i7-20020a636d07000000b0057745b2d018si18119795pgc.390.2023.09.29.03.43.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 03:43:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@sang-engineering.com header.s=k1 header.b=TNnoYa3i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id F185680FC095; Fri, 29 Sep 2023 02:20:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232919AbjI2JUE (ORCPT + 20 others); Fri, 29 Sep 2023 05:20:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231774AbjI2JUD (ORCPT ); Fri, 29 Sep 2023 05:20:03 -0400 Received: from mail.zeus03.de (www.zeus03.de [194.117.254.33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 773721AB for ; Fri, 29 Sep 2023 02:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; s=k1; bh=CPCOX8Pk808b+b p768FKhaTdrGjFDu5eVaZMfKQbRpU=; b=TNnoYa3iv05zXfEWTwp4/pi40Du+IF 8heVnVxeXg0mIFyQeyzUJiBXjACBBOtgORflQmm6zwzViPMpb/bF5He6jTefXSpn gA3AqiR9pqbMoTBL6iqtBWkdxuAD/CSjjjz24cIXu1vwme9aYB2S8dWmG4cQyRIv oz45OCfOuYaGdQAHAcCo2PbDdDeE/scvWvao3bY6Hdv87S9gFIqPHooOuPgP6VRl uZNoAl80ATsBZEnRc3cjsyN9NY0dGmLUMxMFqRbn4E452pfc5O9vloqOcaUF10SU j8jyTjd465dXaZ0mDSfZ53dMzt2LAi1BhhGKcgaWLxDKFevh75MdFpDA== Received: (qmail 3780117 invoked from network); 29 Sep 2023 11:19:56 +0200 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 29 Sep 2023 11:19:56 +0200 X-UD-Smtp-Session: l3s3148p1@sfOD7XsGGL0ujntX From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: Yang Yingliang , Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] i2c: fix memleak in i2c_new_client_device() Date: Fri, 29 Sep 2023 11:19:52 +0200 Message-Id: <20230929091952.19957-1-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 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_BLOCKED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Fri, 29 Sep 2023 02:20:20 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778368325098084299 X-GMAIL-MSGID: 1778368325098084299 Yang Yingliang reported a memleak: === I got memory leak as follows when doing fault injection test: unreferenced object 0xffff888014aec078 (size 8): comm "xrun", pid 356, jiffies 4294910619 (age 16.332s) hex dump (first 8 bytes): 31 2d 30 30 31 63 00 00 1-001c.. backtrace: [<00000000eb56c0a9>] __kmalloc_track_caller+0x1a6/0x300 [<000000000b220ea3>] kvasprintf+0xad/0x140 [<00000000b83203e5>] kvasprintf_const+0x62/0x190 [<000000002a5eab37>] kobject_set_name_vargs+0x56/0x140 [<00000000300ac279>] dev_set_name+0xb0/0xe0 [<00000000b66ebd6f>] i2c_new_client_device+0x7e4/0x9a0 If device_register() returns error in i2c_new_client_device(), the name allocated by i2c_dev_set_name() need be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. === I think this solution is less intrusive and more robust than he originally proposed solutions, though. Reported-by: Yang Yingliang Closes: http://patchwork.ozlabs.org/project/linux-i2c/patch/20221124085448.3620240-1-yangyingliang@huawei.com/ Signed-off-by: Wolfram Sang --- Build tested only. @Yang Yingliang: I'd be happy if you could test it/comment on it. drivers/i2c/i2c-core-base.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index cc4a20465456..0d6172d6b808 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -931,8 +931,9 @@ int i2c_dev_irq_from_resources(const struct resource *resources, struct i2c_client * i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info) { - struct i2c_client *client; - int status; + struct i2c_client *client; + bool need_put = false; + int status; client = kzalloc(sizeof *client, GFP_KERNEL); if (!client) @@ -970,7 +971,6 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf client->dev.fwnode = info->fwnode; device_enable_async_suspend(&client->dev); - i2c_dev_set_name(adap, client, info); if (info->swnode) { status = device_add_software_node(&client->dev, info->swnode); @@ -982,6 +982,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf } } + i2c_dev_set_name(adap, client, info); status = device_register(&client->dev); if (status) goto out_remove_swnode; @@ -993,6 +994,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf out_remove_swnode: device_remove_software_node(&client->dev); + need_put = true; out_err_put_of_node: of_node_put(info->of_node); out_err: @@ -1000,7 +1002,10 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf "Failed to register i2c client %s at 0x%02x (%d)\n", client->name, client->addr, status); out_err_silent: - kfree(client); + if (need_put) + put_device(&client->dev); + else + kfree(client); return ERR_PTR(status); } EXPORT_SYMBOL_GPL(i2c_new_client_device);