Message ID | 20230619112253.v2.1.I13f060c10549ef181603e921291bdea95f83033c@changeid |
---|---|
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 k13csp2755073vqr; Sun, 18 Jun 2023 20:37:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4uWr3wSuw3WByO1SQALAdkLcCVxMcPGSjJbrtPARzigNA0XIgHsdd6a+O1caz9EQ75li5H X-Received: by 2002:a05:6a00:c83:b0:666:617a:c3b5 with SMTP id a3-20020a056a000c8300b00666617ac3b5mr9225366pfv.21.1687145869645; Sun, 18 Jun 2023 20:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687145869; cv=none; d=google.com; s=arc-20160816; b=vpqdsAWl5ueX5WZSkBlYviTXFePNsX5jGl9SZC9RCb4BF4ybtE+53mrZ7ka4O4cGVw he0ss7kACbPpGtnw/W6zCqPqt2E3T1bkDyT+OcPKpN/7GjcdkscvyAYoMp24q2Ph2v/4 QLERM2/4BskxxuM2YnO6fDSY4ZIMfQ0MpDB0UJKGxfiu50Az0VUEvyQ2A27QPlzVKkJm Fcje62Hgx0qKnrmDCKx7mKSHW6B/67N7T/3veFjwEZzcAw0IssE3kxicVkYlhLRDAPde 6Ad4CCKO7BIEaEXAKt4Dy9ZXIWH+e4D5BdrkPOm36awHW9wOJk0E9pIpPvMlttLLJ1Eb q65w== 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=Gqt51Wedx4m69k4BYNrm+CYeb0BA4dcpBkRvlKi17a4=; b=ftvuSmMYYvA8Xo4xHxbyworYZD6qYNTr5z7OKxjqP8QZ5i8YAKPYuZEU3e2BOCRzdH l9nXpVDhU2oUi3psYJn0RGQz0u8kx9zP+HEaqW6bYiAOBf6/7SH7gCh0ZfWlaycl6tOs /54baLw+pr5B1M5hWD6XEnwywQ7pMQru/M55Jgsdz43opQzBwJgXcpV5/MIZ5xwibTt9 splw887PjL8NbNswCBF1Q52U5T0h2pv8xdfQSjAhYtMx20Hnsarfe2lGCpn6v6o500kp HBu5fayXR5PziWcRJQcIb4kufpLDqodb/4sGJq04nD27PcQ929S8VQpkX+nKCtCCOaLE ttdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ittZM8Gx; 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=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c9-20020aa79529000000b00666b6710dcfsi1884720pfp.55.2023.06.18.20.37.37; Sun, 18 Jun 2023 20:37:49 -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=@chromium.org header.s=google header.b=ittZM8Gx; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229647AbjFSDZY (ORCPT <rfc822;maxin.john@gmail.com> + 99 others); Sun, 18 Jun 2023 23:25:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229699AbjFSDZK (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 18 Jun 2023 23:25:10 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CEE110C0 for <linux-kernel@vger.kernel.org>; Sun, 18 Jun 2023 20:24:47 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-66869feb7d1so708916b3a.3 for <linux-kernel@vger.kernel.org>; Sun, 18 Jun 2023 20:24:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1687145087; x=1689737087; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Gqt51Wedx4m69k4BYNrm+CYeb0BA4dcpBkRvlKi17a4=; b=ittZM8GxPcQopWHNt7Ly2wAXQ4tNpBnKAkizLpPXp4OHioLjELl4DIwex/nk7L38wM 4qqn2iA+OIiguh8n3cYxkILH6t+fd8ERPsG1Io/00BEboH7WgB8KQNw1THCSLa2U7yuE BSqX+Iu5uHc4hpfAPFXUXlJCi1WqZlXernAjg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687145087; x=1689737087; 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=Gqt51Wedx4m69k4BYNrm+CYeb0BA4dcpBkRvlKi17a4=; b=f93AIymgxrlwo6kjNQWg5kg2HFCLasgaihVo9txgBuTwOUTeR+lhh/wxQNY1MxO915 jjQpSW+qh/g5/yNasEOPFk3P7NjNcGt985ybP1TOjW8tV8ZFBbr9BgPkqdaCMTIohaXm Ehlv1c02oK7maphtew7HgitWCkNaG2hpq9fnUweCMEEgc1leN2xdnhCbKOicPXUBe7aC HkXw6FONbkAcjUUfjtF19FWhT1FosFaNxi5SNlSmhTm3wFZowNYwmvYjRE3tdgNT1+DY TsnYuldvmTfDSceCUUnfbSh/Am5BImdUbVNXhTtUp0xpagbn/UzxYpjhwJkK2FWzjX61 1M5Q== X-Gm-Message-State: AC+VfDyPKtrbmiM+E1rHK5kylvGR7Hk/7IbRzxwjUtHqu3hNqhrJPlr2 6gKHhIQ/0IQq+u8g7skh/l4tXD9QDSFXg4IwQY4= X-Received: by 2002:a05:6a00:3a10:b0:668:8596:7524 with SMTP id fj16-20020a056a003a1000b0066885967524mr83131pfb.20.1687145086953; Sun, 18 Jun 2023 20:24:46 -0700 (PDT) Received: from fshao-p620.tpe.corp.google.com ([2401:fa00:1:10:59e8:aa0b:2092:f8fc]) by smtp.gmail.com with ESMTPSA id j9-20020aa78d09000000b0064b0326494asm11464995pfe.150.2023.06.18.20.24.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Jun 2023 20:24:46 -0700 (PDT) From: Fei Shao <fshao@chromium.org> To: Stephen Boyd <sboyd@kernel.org> Cc: Fei Shao <fshao@chromium.org>, Jerome Brunet <jbrunet@baylibre.com>, Michael Turquette <mturquette@baylibre.com>, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] clk: Fix memory leak in devm_clk_notifier_register() Date: Mon, 19 Jun 2023 11:22:53 +0800 Message-ID: <20230619112253.v2.1.I13f060c10549ef181603e921291bdea95f83033c@changeid> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,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?1769100667192818720?= X-GMAIL-MSGID: =?utf-8?q?1769100667192818720?= |
Series |
[v2] clk: Fix memory leak in devm_clk_notifier_register()
|
|
Commit Message
Fei Shao
June 19, 2023, 3:22 a.m. UTC
devm_clk_notifier_register() allocates a devres resource for clk
notifier but didn't register that to the device, so the notifier didn't
get unregistered on device detach and the allocated resource was leaked.
Fix the issue by registering the resource through devres_add().
Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register")
Signed-off-by: Fei Shao <fshao@chromium.org>
---
Changes in v2:
- Revise commit message
drivers/clk/clk.c | 1 +
1 file changed, 1 insertion(+)
Comments
On Mon, Jun 19, 2023 at 11:22:53AM +0800, Fei Shao wrote: > devm_clk_notifier_register() allocates a devres resource for clk > notifier but didn't register that to the device, so the notifier didn't > get unregistered on device detach and the allocated resource was leaked. > > Fix the issue by registering the resource through devres_add(). > > Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register") > Signed-off-by: Fei Shao <fshao@chromium.org> > --- > Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> How did you find this bug? I can think of some ways to find this bug with static analysis. KTODO: static analysis: look at unused parameters Both GCC and Clang have a warning for unused parameters. I think the last time I looked at GCC it had a lot of false positives for functions which were called as pointers but hopefully that has been fixed now? Smatch does not have a check for this. If someone were to write it, I would probably the check under the --pedantic flag so it would be turned off by default. regards, dan carpenter
On Mon, Jun 19, 2023 at 4:48 PM Dan Carpenter <dan.carpenter@linaro.org> wrote: > > On Mon, Jun 19, 2023 at 11:22:53AM +0800, Fei Shao wrote: > > devm_clk_notifier_register() allocates a devres resource for clk > > notifier but didn't register that to the device, so the notifier didn't > > get unregistered on device detach and the allocated resource was leaked. > > > > Fix the issue by registering the resource through devres_add(). > > > > Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register") > > Signed-off-by: Fei Shao <fshao@chromium.org> > > --- > > > > Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> > > How did you find this bug? > > I can think of some ways to find this bug with static analysis. > It was actually detected by kmemleak on an unreleased Chromebook device. I added the trace snippet in the message at first but removed that before sending this. Maybe I shouldn't have. I can resend a v3 to add that back if that's preferable. What do you think? Regards, Fei > KTODO: static analysis: look at unused parameters > > Both GCC and Clang have a warning for unused parameters. I think the > last time I looked at GCC it had a lot of false positives for functions > which were called as pointers but hopefully that has been fixed now? > Smatch does not have a check for this. If someone were to write it, > I would probably the check under the --pedantic flag so it would be > turned off by default. > > regards, > dan carpenter >
On Mon, Jun 19, 2023 at 05:05:47PM +0800, Fei Shao wrote: > On Mon, Jun 19, 2023 at 4:48 PM Dan Carpenter <dan.carpenter@linaro.org> wrote: > > > > On Mon, Jun 19, 2023 at 11:22:53AM +0800, Fei Shao wrote: > > > devm_clk_notifier_register() allocates a devres resource for clk > > > notifier but didn't register that to the device, so the notifier didn't > > > get unregistered on device detach and the allocated resource was leaked. > > > > > > Fix the issue by registering the resource through devres_add(). > > > > > > Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register") > > > Signed-off-by: Fei Shao <fshao@chromium.org> > > > --- > > > > > > > Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> > > > > How did you find this bug? > > > > I can think of some ways to find this bug with static analysis. > > > > It was actually detected by kmemleak on an unreleased Chromebook device. > I added the trace snippet in the message at first but removed that > before sending this. Maybe I shouldn't have. > > I can resend a v3 to add that back if that's preferable. What do you think? I'm not a clk maintainer, but let's not go overboard resending patches, especially when they're as straight forward as this one. This is good information though so I would include that kind of stuff in future patches. I don't really need to see the kmemleak warning itself because I know what those look like already. But to me it says a lot that actually this was detected at runtime. It says good things about your test infrastructure and makes me feel more confident that your patch is correct. So maybe just a comment that "This leak was detected by kmemleak". regards, dan carpenter
On Mon, Jun 19, 2023 at 5:24 PM Dan Carpenter <dan.carpenter@linaro.org> wrote: > > On Mon, Jun 19, 2023 at 05:05:47PM +0800, Fei Shao wrote: > > On Mon, Jun 19, 2023 at 4:48 PM Dan Carpenter <dan.carpenter@linaro.org> wrote: > > > > > > On Mon, Jun 19, 2023 at 11:22:53AM +0800, Fei Shao wrote: > > > > devm_clk_notifier_register() allocates a devres resource for clk > > > > notifier but didn't register that to the device, so the notifier didn't > > > > get unregistered on device detach and the allocated resource was leaked. > > > > > > > > Fix the issue by registering the resource through devres_add(). > > > > > > > > Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register") > > > > Signed-off-by: Fei Shao <fshao@chromium.org> > > > > --- > > > > > > > > > > Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> > > > > > > How did you find this bug? > > > > > > I can think of some ways to find this bug with static analysis. > > > > > > > It was actually detected by kmemleak on an unreleased Chromebook device. > > I added the trace snippet in the message at first but removed that > > before sending this. Maybe I shouldn't have. > > > > I can resend a v3 to add that back if that's preferable. What do you think? > > I'm not a clk maintainer, but let's not go overboard resending patches, > especially when they're as straight forward as this one. > > This is good information though so I would include that kind of stuff in > future patches. I don't really need to see the kmemleak warning itself > because I know what those look like already. But to me it says a lot > that actually this was detected at runtime. It says good things about > your test infrastructure and makes me feel more confident that your > patch is correct. So maybe just a comment that "This leak was detected > by kmemleak". That makes sense. Acknowledged and noted. Thanks, Fei > > regards, > dan carpenter >
On Mon, Jun 19, 2023 at 12:24:41PM +0300, Dan Carpenter wrote: > > It was actually detected by kmemleak on an unreleased Chromebook device. > > I added the trace snippet in the message at first but removed that > > before sending this. Maybe I shouldn't have. > > > > I can resend a v3 to add that back if that's preferable. What do you think? The other reason to include stack traces is so that if someone else runs into the same bug they can find your patch by googling their stack trace. Normal users aren't going to be running kmemleak. And people doing testing work for companies are hopefully going to pull this fix in via the stable tree so they'll get this patch automatically that way so they won't see it either. But if the stack trace is like a NULL dereference bug, then users absolutely do notice that kind of thing. You should always include those kind of stack traces. regards, dan carpenter
On Mon, Jun 19, 2023 at 5:57 PM Dan Carpenter <dan.carpenter@linaro.org> wrote: > The other reason to include stack traces is so that if someone else > runs into the same bug they can find your patch by googling their stack > trace. > > Normal users aren't going to be running kmemleak. And people doing > testing work for companies are hopefully going to pull this fix in via > the stable tree so they'll get this patch automatically that way so > they won't see it either. > > But if the stack trace is like a NULL dereference bug, then users > absolutely do notice that kind of thing. You should always include > those kind of stack traces. If that's the case, I can leave a retrospective trace record here: unreferenced object 0xffffff80c4e34a00 (size 256): comm "swapper/0", pid 1, jiffies 4294667967 (age 288.740s) hex dump (first 32 bytes): 00 4a e3 c4 80 ff ff ff 00 4a e3 c4 80 ff ff ff .J.......J...... 1c 2a 7a ae d8 ff ff ff a0 b0 af af d8 ff ff ff .*z............. backtrace: [<000000007d72e65c>] __kmem_cache_alloc_node+0x198/0x240 [<00000000dfce47ef>] __kmalloc_node_track_caller+0x6c/0x1b8 [<00000000b6c409fe>] __devres_alloc_node+0x60/0x104 [<0000000081112baf>] devm_clk_notifier_register+0x44/0xc8 [<0000000070bfe318>] devm_mtk_clk_mux_notifier_register+0x60/0x74 [<000000000242235f>] clk_mt8188_reg_mfg_mux_notifier+0x84/0xb4 [<00000000f67ce424>] clk_mt8188_topck_probe+0x1b8/0x2e4 [<0000000006eef8cd>] platform_probe+0x12c/0x17c [<00000000eacf783c>] really_probe+0x1f0/0x4d8 [<00000000f321a3f0>] __driver_probe_device+0x160/0x230 [<00000000bbeed898>] driver_probe_device+0x6c/0x148 [<000000007d5af62b>] __driver_attach+0x164/0x20c [<00000000c5c25e77>] bus_for_each_dev+0xf4/0x144 [<00000000e2c0100f>] driver_attach+0x50/0x60 [<00000000cc421ec0>] bus_add_driver+0x2a8/0x458 [<000000007814168a>] driver_register+0x16c/0x29c It's up to the maintainers for the next step and I'll follow the call. Regards, Fei
Quoting Fei Shao (2023-06-18 20:22:53) > devm_clk_notifier_register() allocates a devres resource for clk > notifier but didn't register that to the device, so the notifier didn't > get unregistered on device detach and the allocated resource was leaked. > > Fix the issue by registering the resource through devres_add(). > > Fixes: 6d30d50d037d ("clk: add devm variant of clk_notifier_register") > Signed-off-by: Fei Shao <fshao@chromium.org> > --- Applied to clk-next It would be nice to also add a test or two for this.
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 7ac9f7a8cb84..c249f9791ae8 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4741,6 +4741,7 @@ int devm_clk_notifier_register(struct device *dev, struct clk *clk, if (!ret) { devres->clk = clk; devres->nb = nb; + devres_add(dev, devres); } else { devres_free(devres); }