Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2220270rwi; Fri, 21 Oct 2022 00:54:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6+RZkZV5porVw0vOIEQin7XcF+0IXIMgEuXxpsCKDf5DCEZEJDrmOGiJFqp0ptXSa8fXm5 X-Received: by 2002:a17:907:3e87:b0:78d:bb06:9066 with SMTP id hs7-20020a1709073e8700b0078dbb069066mr14302161ejc.387.1666338849811; Fri, 21 Oct 2022 00:54:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666338849; cv=none; d=google.com; s=arc-20160816; b=uQp7Rag++mZc+hqiP4RyjEL98bxlke1vDj7kKyWwKAYSsFTgtXys4k32dPHaKctbTc o19guvFV6NIZ1QK8R/r5mA+J8Mu0x7sAmWvA44u1ndH35ZPOjAvEFCXQaNYi7RZ4Siu/ v7f6hvt1ncCcgmY8Kk+biJrTgSgdC6cyBAeb+oa/SOu5mSTDK36MXxy0OgqL1dRfUZ1I khmC0+Z8/3za9LyquomSmiRVxfXzSoEEZSvH0sv0jDljoU08pc53hIHuqVuoAX4s/ezD sgL2yIiFPZoq+fWKaS09BnuclGNuz0Hvmgnw7YjBZClGqz9PyYDimou0YV56Aechwx3M mbHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=JQluvds8xOLIlfimsEnEw6zA5CGTTtIL1qDb7SHY87g=; b=PcPG9nPwzoUSovJWb7tZxinC+mzbbCDGl7amxOselXDd8SXXsOL54QdRXzEqblqrxN wqx+wirPTbyLTBVAWuhSNZPPHeBHH+x3NYmh3ngklOG2R6f828x1F3H3TTG5gIMBBDaf g2GH4EQmG6oxvgJrgyoNzBG+ExGcXg5ZlW2DmGn76kwPnUWWC0i9fm0VPAV46sHkmN4/ E1Zczy6Q/JQK56xvnWcvq4fJzchvioKUSJ181bT3ofF6CFbAaB52J/ihpcygOScbZqxm tn5bwXTMC/gA9O6I39ebIRsFr+roNMrF8EHuhabcPU+kWJovlWZMYj8WWzNt+05H4PY9 6cnw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kx10-20020a170907774a00b007832270e88fsi17288972ejc.681.2022.10.21.00.53.44; Fri, 21 Oct 2022 00:54:09 -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; 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 S230055AbiJUH0K (ORCPT + 99 others); Fri, 21 Oct 2022 03:26:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230040AbiJUH0I (ORCPT ); Fri, 21 Oct 2022 03:26:08 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84039FF3 for ; Fri, 21 Oct 2022 00:26:05 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Mtwr34fC2zVj5k; Fri, 21 Oct 2022 15:21:23 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 15:25:54 +0800 Received: from [10.174.178.174] (10.174.178.174) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 15:25:53 +0800 Subject: Re: [PATCH 00/11] fix memory leak while kset_register() fails To: Luben Tuikov , , , , , , , CC: , , , , , , , , , , , , , , References: <20221021022102.2231464-1-yangyingliang@huawei.com> From: Yang Yingliang Message-ID: <2a99c52c-d29c-5f5c-57a8-9851018e7420@huawei.com> Date: Fri, 21 Oct 2022 15:25:51 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.174.178.174] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 Hi, On 2022/10/21 13:29, Luben Tuikov wrote: > On 2022-10-20 22:20, Yang Yingliang wrote: >> The previous discussion link: >> https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194@huawei.com/T/ > The very first discussion on this was here: > > https://www.spinics.net/lists/dri-devel/msg368077.html > > Please use this link, and not the that one up there you which quoted above, > and whose commit description is taken verbatim from the this link. I found this leaks in bus_register()/class_register()/kset_create_and_add() at first, and describe the reason in these patches which is using kobject_set_name() description, here is the patches: https://lore.kernel.org/lkml/20221017014957.156645-1-yangyingliang@huawei.com/T/ https://lore.kernel.org/lkml/20221017031335.1845383-1-yangyingliang@huawei.com/ https://lore.kernel.org/lkml/Y0zfPKAgQSrYZg5o@kroah.com/T/ And then I found other subsystem also have this problem, so posted the fix patches for them (including qemu_fw_cfg/f2fs/erofs/ocfs2/amdgpu_discovery): https://www.mail-archive.com/qemu-devel@nongnu.org/msg915553.html https://lore.kernel.org/linux-f2fs-devel/7908686b-9a7c-b754-d312-d689fc28366e@kernel.org/T/#t https://lore.kernel.org/linux-erofs/20221018073947.693206-1-yangyingliang@huawei.com/ https://lore.kernel.org/lkml/0db486eb-6927-927e-3629-958f8f211194@huawei.com/T/ https://www.spinics.net/lists/dri-devel/msg368092.html In the amdgpu_discovery patch, I sent a old one which using wrong description and you pointer out, and then I send a v2. And then the maintainer of ocfs2 has different thought about this, so we had a discussion in the link that I gave out, and Greg suggested me to update kset_register() documentation and then put the fix patches together in one series, so I sent this patchset and use the link. Thanks, Yang > >> kset_register() is currently used in some places without calling >> kset_put() in error path, because the callers think it should be >> kset internal thing to do, but the driver core can not know what >> caller doing with that memory at times. The memory could be freed >> both in kset_put() and error path of caller, if it is called in >> kset_register(). > As I explained in the link above, the reason there's > a memory leak is that one cannot call kset_register() without > the kset->kobj.name being set--kobj_add_internal() returns -EINVAL, > in this case, i.e. kset_register() fails with -EINVAL. > > Thus, the most common usage is something like this: > > kobj_set_name(&kset->kobj, format, ...); > kset->kobj.kset = parent_kset; > kset->kobj.ktype = ktype; > res = kset_register(kset); > > So, what is being leaked, is the memory allocated in kobj_set_name(), > by the common idiom shown above. This needs to be mentioned in > the documentation, at least, in case, in the future this is absolved > in kset_register() redesign, etc. > > Regards, > Luben > >> So make the function documentation more explicit about calling >> kset_put() in the error path of caller first, so that people >> have a chance to know what to do here, then fixes this leaks >> by calling kset_put() from callers. >> >> Liu Shixin (1): >> ubifs: Fix memory leak in ubifs_sysfs_init() >> >> Yang Yingliang (10): >> kset: fix documentation for kset_register() >> kset: add null pointer check in kset_put() >> bus: fix possible memory leak in bus_register() >> kobject: fix possible memory leak in kset_create_and_add() >> class: fix possible memory leak in __class_register() >> firmware: qemu_fw_cfg: fix possible memory leak in >> fw_cfg_build_symlink() >> f2fs: fix possible memory leak in f2fs_init_sysfs() >> erofs: fix possible memory leak in erofs_init_sysfs() >> ocfs2: possible memory leak in mlog_sys_init() >> drm/amdgpu/discovery: fix possible memory leak >> >> drivers/base/bus.c | 4 +++- >> drivers/base/class.c | 6 ++++++ >> drivers/firmware/qemu_fw_cfg.c | 2 +- >> drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++-- >> fs/erofs/sysfs.c | 4 +++- >> fs/f2fs/sysfs.c | 4 +++- >> fs/ocfs2/cluster/masklog.c | 7 ++++++- >> fs/ubifs/sysfs.c | 2 ++ >> include/linux/kobject.h | 3 ++- >> lib/kobject.c | 5 ++++- >> 10 files changed, 33 insertions(+), 9 deletions(-) >> > .