From patchwork Fri Nov 11 10:36:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 18671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp668692wru; Fri, 11 Nov 2022 02:53:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf7PFQnyn+52NTX0LobBJXvoC8b1pwY3JN3n+cuVqmtjWPvmQLwoJEWgFA9xWhBkteV7igti X-Received: by 2002:a17:902:8343:b0:188:52df:769e with SMTP id z3-20020a170902834300b0018852df769emr1776260pln.30.1668163993651; Fri, 11 Nov 2022 02:53:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668163993; cv=none; d=google.com; s=arc-20160816; b=D5l/r9U8NUlLt64CoqehW3jGgAW9GJbq+ZYz3c4yGTIyx68ENoOJ5FMuxwnW7yX/PI JqerQbs93G2v3XolLOUI9Yh/l2ruQvBlLaEUca7xBt9buBLtQQzKtzfJ6Unh5MUHYGuh AzZIGCiMFJzLzOq67jzpi2EMqPJxDV7KSfef1f74HwEO7eG1DoGzl6Q1ZzNH6Z78AD/R Xzafng6eHlfD/3lPOQffUo2lQt/uAzN0hqxjFfT+gXJhchmWg3eNo/s47qRTXm00H47+ yHXrBxi6UZV5Ii3TRBhRQJkZ6KSOZcFenKKqXGxbZeXAIp5iTCMbG0HN8ZAC0oLfiqZS MIcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=XcDBWiofYspeSyblkks2qROriwr7wzg9VeoILpBiBbE=; b=MW8Jw8LBv0vPUffN74qwxX9b1k/nzK8fisUu16wQfSlxaI/XFQ7oYQFpNqzCEcgJLH ti10mm2C909d9JJFSFiAsf+UtpNe1xRSNVkMmqqweLXwe56wCUmV5WEB5POYExOCztuc hSJiBBU+g2BGzJX04Ihzad7iodXs5xtNYAMWd0kObu+PacNAgmLYTvkzCUZaejExc7Q5 eGG7g5Nx7Q4kpAdYsIwc43k5FpWBTTTR9l5D6y+QFEOOQaKeTPe/9F1lwQZbcu0pMdeF TjTEe7SaS3VJPAJTblyx3WtLv8q0QOdey4d/uH3iL+uApjIXjJO7L0kpQcFImAvHjh0v k1Lw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u12-20020a63df0c000000b0043c0b452d3esi2097351pgg.292.2022.11.11.02.52.59; Fri, 11 Nov 2022 02:53:13 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233451AbiKKKkM (ORCPT + 99 others); Fri, 11 Nov 2022 05:40:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233915AbiKKKkH (ORCPT ); Fri, 11 Nov 2022 05:40:07 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31803654E5; Fri, 11 Nov 2022 02:40:06 -0800 (PST) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N7wFN1BB9zRp1W; Fri, 11 Nov 2022 18:39:52 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 11 Nov 2022 18:40:04 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 11 Nov 2022 18:40:04 +0800 From: Chen Zhongjin To: , CC: , , , , , , , Subject: [PATCH] perf: Fix possible memleak in pmu_dev_alloc() Date: Fri, 11 Nov 2022 18:36:53 +0800 Message-ID: <20221111103653.91058-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749196727836577529?= X-GMAIL-MSGID: =?utf-8?q?1749196727836577529?= In pmu_dev_alloc(), when dev_set_name() failed, it will goto free_dev and call put_device(pmu->dev) to release it. However pmu->dev->release is assigned after this, which makes warning and memleak. Call dev_set_name() after pmu->dev->release = pmu_dev_release to fix it. Device '(null)' does not have a release() function... WARNING: CPU: 2 PID: 441 at drivers/base/core.c:2332 device_release+0x1b9/0x240 ... Call Trace: kobject_put+0x17f/0x460 put_device+0x20/0x30 pmu_dev_alloc+0x152/0x400 perf_pmu_register+0x96b/0xee0 ... kmemleak: 1 new suspected memory leaks (see /sys/kernel/debug/kmemleak) unreferenced object 0xffff888014759000 (size 2048): comm "modprobe", pid 441, jiffies 4294931444 (age 38.332s) backtrace: [<0000000005aed3b4>] kmalloc_trace+0x27/0x110 [<000000006b38f9b8>] pmu_dev_alloc+0x50/0x400 [<00000000735f17be>] perf_pmu_register+0x96b/0xee0 [<00000000e38477f1>] 0xffffffffc0ad8603 [<000000004e162216>] do_one_initcall+0xd0/0x4e0 ... Fixes: abe43400579d ("perf: Sysfs enumeration") Signed-off-by: Chen Zhongjin --- kernel/events/core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4ec3717003d5..baf0f33b06af 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -11148,13 +11148,15 @@ static int pmu_dev_alloc(struct pmu *pmu) pmu->dev->groups = pmu->attr_groups; device_initialize(pmu->dev); - ret = dev_set_name(pmu->dev, "%s", pmu->name); - if (ret) - goto free_dev; dev_set_drvdata(pmu->dev, pmu); pmu->dev->bus = &pmu_bus; pmu->dev->release = pmu_dev_release; + + ret = dev_set_name(pmu->dev, "%s", pmu->name); + if (ret) + goto free_dev; + ret = device_add(pmu->dev); if (ret) goto free_dev;