From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 75119C38A2D for ; Mon, 24 Oct 2022 14:54:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D1A0410E801; Mon, 24 Oct 2022 14:54:04 +0000 (UTC) Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by gabe.freedesktop.org (Postfix) with ESMTPS id 22B8210E801 for ; Mon, 24 Oct 2022 14:52:35 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 62D49B816A2; Mon, 24 Oct 2022 14:52:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EFBC433C1; Mon, 24 Oct 2022 14:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 From: Greg KH To: Yang Yingliang Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> X-Mailman-Approved-At: Mon, 24 Oct 2022 14:54:03 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: rafael@kernel.org, qemu-devel@nongnu.org, liushixin2@huawei.com, joseph.qi@linux.alibaba.com, linux-mtd@lists.infradead.org, huangjianan@oppo.com, richard@nod.at, mark@fasheh.com, mst@redhat.com, amd-gfx@lists.freedesktop.org, luben.tuikov@amd.com, hsiangkao@linux.alibaba.com, somlo@cmu.edu, chao@kernel.org, jlbec@evilplan.org, jaegeuk@kernel.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, alexander.deucher@amd.com, akpm@linux-foundation.org, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE88EC38A2D for ; Mon, 24 Oct 2022 14:52:48 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MwyjW07fkz3c1K for ; Tue, 25 Oct 2022 01:52:47 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=0R1tizG1; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linuxfoundation.org (client-ip=145.40.68.75; helo=ams.source.kernel.org; envelope-from=gregkh@linuxfoundation.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=0R1tizG1; dkim-atps=neutral Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MwyjM6q7Lz2xl5 for ; Tue, 25 Oct 2022 01:52:39 +1100 (AEDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 62D49B816A2; Mon, 24 Oct 2022 14:52:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EFBC433C1; Mon, 24 Oct 2022 14:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 From: Greg KH To: Yang Yingliang Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> X-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: rafael@kernel.org, qemu-devel@nongnu.org, liushixin2@huawei.com, joseph.qi@linux.alibaba.com, linux-mtd@lists.infradead.org, huangjianan@oppo.com, richard@nod.at, mark@fasheh.com, mst@redhat.com, amd-gfx@lists.freedesktop.org, luben.tuikov@amd.com, hsiangkao@linux.alibaba.com, somlo@cmu.edu, jlbec@evilplan.org, jaegeuk@kernel.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, alexander.deucher@amd.com, akpm@linux-foundation.org, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 93C4EC38A2D for ; Mon, 24 Oct 2022 14:52:53 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1omyou-00037M-BP; Mon, 24 Oct 2022 14:52:52 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1omyom-00037F-MY for linux-f2fs-devel@lists.sourceforge.net; Mon, 24 Oct 2022 14:52:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=In-Reply-To:Content-Type:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=taryaQqhDU8DO2nlD8gYIfcALNfWHO/93rLVKCPhD8g=; b=cX2tqGqc7lEV40Or3PdSlMqUpv 7PyqQzg9sp2MXThmgipasFooHMXpwzRFBzRrXhHlnGbKco+cKQhkxZ8e6bEIBxyRh+KYib5mMv7eK g3Tt5LhYKrLh5RmwG1IrQoYhViHzKro55YhqYSH9c6bZoA4qlUfnJdSAsuEauRy6PT3M=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To :From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=taryaQqhDU8DO2nlD8gYIfcALNfWHO/93rLVKCPhD8g=; b=b1POzU4vLbvQq7leatsUAV3GDI uFdzFb7jH1FgpImREz51xlhJdD7qhs024qrKZzXDEDbh6/JPg105wRwUFJFA2CTEXDFEQ690ElPl7 l0PBVSmhTQIUb/cEPlr+qN7OkSpcFq2PBN9Z9t8XdBk9DMW1vg06sMZpiDeKDilTF9L4=; Received: from ams.source.kernel.org ([145.40.68.75]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1omyoi-00FqFu-PV for linux-f2fs-devel@lists.sourceforge.net; Mon, 24 Oct 2022 14:52:44 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 62D49B816A2; Mon, 24 Oct 2022 14:52:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EFBC433C1; Mon, 24 Oct 2022 14:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 From: Greg KH To: Yang Yingliang Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> X-Headers-End: 1omyoi-00FqFu-PV Subject: Re: [f2fs-dev] [PATCH v2] kset: fix memory leak when kset_register() returns error X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: rafael@kernel.org, qemu-devel@nongnu.org, liushixin2@huawei.com, joseph.qi@linux.alibaba.com, linux-mtd@lists.infradead.org, richard@nod.at, mark@fasheh.com, mst@redhat.com, amd-gfx@lists.freedesktop.org, luben.tuikov@amd.com, hsiangkao@linux.alibaba.com, somlo@cmu.edu, jlbec@evilplan.org, jaegeuk@kernel.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, alexander.deucher@amd.com, akpm@linux-foundation.org, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 29F3DECAAA1 for ; Mon, 24 Oct 2022 14:52:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=G5G+rgo+hbkIMx9UY6tl7o41GD50QdYUQmn3j9gy4DI=; b=pjMqpy24mfo7T6 zwRMDJAeN01JI5zdXrZrgc/USngdFjLCdwuVQCAKrQVTHionPWyhVLek6g+r0VoM6tKZqpR0cAlp+ y+uQ80OtYo54ggu3jmBUCszaSMmTNh/K24/lSI5DVeOyn2ovlkOsrfzIKMnbxx6sEtiUPUr614uBh zLtb+xSUKlkeFvn+Ayi45bSD2YVCvLRHVRj9cg2IKJ5moH0GEIgogHjv5wxHnyxvQKDUHX9YBF1BF dAzoXj1miyLO4O0oudDqRX6cZ3+dRTvuLbaP/jE5XNP/T15+HmHnviRX7XFBW/krFHG+ylAcxolLq CitHcH6OmPMAyIHVdLLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1omyoh-001zcz-40; Mon, 24 Oct 2022 14:52:39 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1omyoe-001zbg-26 for linux-mtd@lists.infradead.org; Mon, 24 Oct 2022 14:52:37 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 62D49B816A2; Mon, 24 Oct 2022 14:52:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EFBC433C1; Mon, 24 Oct 2022 14:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 From: Greg KH To: Yang Yingliang Cc: linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, linux-f2fs-devel@lists.sourceforge.net, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com, linux-mtd@lists.infradead.org, amd-gfx@lists.freedesktop.org, rafael@kernel.org, somlo@cmu.edu, mst@redhat.com, jaegeuk@kernel.org, chao@kernel.org, hsiangkao@linux.alibaba.com, huangjianan@oppo.com, mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, akpm@linux-foundation.org, alexander.deucher@amd.com, luben.tuikov@amd.com, richard@nod.at, liushixin2@huawei.com Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221024_075236_410270_64EFA87F X-CRM114-Status: GOOD ( 35.84 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aib29ajc253.phx1.oracleemaildelivery.com (aib29ajc253.phx1.oracleemaildelivery.com [192.29.103.253]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2DE15ECAAA1 for ; Mon, 24 Oct 2022 14:52:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=oss-phx-1109; d=oss.oracle.com; h=Date:To:From:Subject:Message-Id:MIME-Version:Sender; bh=EtTChZP17fRR9Xqu1bCqnJ+4WaojBukPEnqQho/9nfQ=; b=k0RSDpq2UvF+kfSayyo8qlZecmhLNuZTfs6HSFoqKvGQJfYtzTeZgs3BuT/SC7Co7ANH3ynMAURf CJGJZ/FED4ir1Q57iydeKwg19sDkMiVHab3Ogo2vdC3RqskvRl/J6084sVNAKiisZMVQp7NcM2Kf w1rQ0z0Y/VBu3a3bPFS3rSYpcK481rKc3b+1piuG2QWxviwBVouSBSft0OXCWzOgRVwAuGzApH8x 22418QbFAcpi90rGMr5zDi97O9yu3FAc1XD2HIyO8/FYGoalCdo3LJQTW/CgU+Gik/YqDtmv2qsa cc4NIYfoQ1k5RpP//y1eVF3gY1tjybLHhjGXQA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=prod-phx-20191217; d=phx1.rp.oracleemaildelivery.com; h=Date:To:From:Subject:Message-Id:MIME-Version:Sender; bh=EtTChZP17fRR9Xqu1bCqnJ+4WaojBukPEnqQho/9nfQ=; b=RFtTQtc4trcktpyaOdLho0TssZ5tfgTD5ZfbxuoG3YHAsYhCzv9yDyHWJLtebOShOit92ipkfPBP V1Ex3jE0NRNmTnnOEYNRi+MDo8oCsBfJPEk7xB17iIG+YeoLzhRSVaakgnG6029W+d/2xu2QP7pT NJJU/REEzyRZthRyTEYwoUI6yNQ3KrleLz4pLJVX8VlPPP2kAcr66nL7+2Z6IOHAGNaYtQ+u4zZD zm2VubFG+ltY3AsjEqXNQX35eHqqOSQlmxeqlxT5iDtRWJn0jHLisLl72/Z1VSho2rANVx3U2T48 AourDmS61Of8+pbwzJZSMwxiwcdBe6TJHcPN+A== Received: by omta-ad3-fd1-302-us-phoenix-1.omtaad3.vcndpphx.oraclevcn.com (Oracle Communications Messaging Server 8.1.0.1.20220914 64bit (built Sep 14 2022)) with ESMTPS id <0RK900241INZLF80@omta-ad3-fd1-302-us-phoenix-1.omtaad3.vcndpphx.oraclevcn.com> for ocfs2-devel@archiver.kernel.org; Mon, 24 Oct 2022 14:52:47 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 To: Yang Yingliang Message-id: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-version: 1.0 Content-disposition: inline In-reply-to: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> X-Source-IP: 145.40.68.75 X-Proofpoint-Virus-Version: vendor=nai engine=6500 definitions=10510 signatures=596816 X-Proofpoint-Spam-Details: rule=tap_notspam policy=tap score=0 mlxscore=0 lowpriorityscore=0 malwarescore=0 bulkscore=0 phishscore=0 priorityscore=639 adultscore=0 clxscore=224 suspectscore=0 spamscore=0 mlxlogscore=999 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210240091 Cc: rafael@kernel.org, qemu-devel@nongnu.org, liushixin2@huawei.com, linux-mtd@lists.infradead.org, huangjianan@oppo.com, richard@nod.at, mst@redhat.com, amd-gfx@lists.freedesktop.org, luben.tuikov@amd.com, hsiangkao@linux.alibaba.com, somlo@cmu.edu, chao@kernel.org, jaegeuk@kernel.org, linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, alexander.deucher@amd.com, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com Subject: Re: [Ocfs2-devel] [PATCH v2] kset: fix memory leak when kset_register() returns error X-BeenThere: ocfs2-devel@oss.oracle.com X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Greg KH via Ocfs2-devel Reply-to: Greg KH Content-type: text/plain; charset="us-ascii" Content-transfer-encoding: 7bit Errors-to: ocfs2-devel-bounces@oss.oracle.com X-ServerName: ams.source.kernel.org X-Proofpoint-SPF-Result: pass X-Proofpoint-SPF-Record: v=spf1 ip4:72.55.140.81 ip4:52.25.139.140 ip4:139.178.84.217 ip6:2604:1380:4641:c500::1 ip4:145.40.68.75 ip6:2604:1380:4601:e00::1 ip4:145.40.73.55 ip6:2604:1380:40e1:4800::1 include:_spf.google.com include:amazonses.com include:_spf.salesforce.com -all X-Spam: Clean X-Proofpoint-GUID: f1DLm87A-6n-IL2j7VFvIZGxiWNZU2gZ X-Proofpoint-ORIG-GUID: f1DLm87A-6n-IL2j7VFvIZGxiWNZU2gZ Reporting-Meta: AAE6cxYTIoozDZcuy6sg4SFwdM4qoPKmxIRcU7EJpaJhZzlou10r9sgOizjg8OUL IklgzdAW/2el2R/BHwhMH0cqA0lA+C+m8NCSoY/R6kPTsb1CM2La/gf9WcktG90l 69cSmmS1Pev4WvCQJxg2oqc6Ig7qwhWagv1FtJU2YAMeWUogSih9IUxCQKzFLcgh qvxL6kLF1QcETOyeWfH6HmyyCtrBKH2Y7LHeJdC0B+BRh2sE+qDKDBJK2AhKez5P 2Gl8HMhvO+rf7glcqhL1jBAgiNmdBH7sf9JKHtNpeQpcFk4yI1BrtuH65h83mL11 VgZC7eBZ6Bay5RIp+8+3pdAgTs/l0i2nOw9//VTiHC5276l6sSabb+LwZ35qpb4p 59j6boTRgeQIeaHSlca5Xd4vMDKyNrX/4OnhotyDC6N2J+KB+TXP26fBLYxCsjfT FTpkiVA24hIq9mbiihDRwwNY1gEsQErWIFhdFDR1MLGFL61uDd4xc9pZpA+D7oUx VOSXs6JF+/iaTDN6DOpEmhImci9Jsr/TZmybI3jM On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h _______________________________________________ Ocfs2-devel mailing list Ocfs2-devel@oss.oracle.com https://oss.oracle.com/mailman/listinfo/ocfs2-devel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47CF3ECAAA1 for ; Mon, 24 Oct 2022 16:13:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234052AbiJXQNE (ORCPT ); Mon, 24 Oct 2022 12:13:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233782AbiJXQJL (ORCPT ); Mon, 24 Oct 2022 12:09:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EBA13470A for ; Mon, 24 Oct 2022 07:59:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id ABF2661425 for ; Mon, 24 Oct 2022 14:52:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EFBC433C1; Mon, 24 Oct 2022 14:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666623153; bh=iIf7CBp4DRtS4RK5w27tn1+7LfTAdJA8NimWQF9sSvg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0R1tizG1JTD1VZMfei9eZUwBceJMo27F3IPhrwxgizVbdIXR/VebztKIjUAfm+TJ2 cpiPAbSdylVk4YR/PmDNZIIZzoMbWnp7XnOykqA3Po0XqNtgjvxOXuMeqXyiEX1VR+ i+cMHZA9GYInHS3HEvmT7UEBSkL+YkXi0BemMqzI= Date: Mon, 24 Oct 2022 16:53:22 +0200 From: Greg KH To: Yang Yingliang Cc: linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, linux-f2fs-devel@lists.sourceforge.net, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com, linux-mtd@lists.infradead.org, amd-gfx@lists.freedesktop.org, rafael@kernel.org, somlo@cmu.edu, mst@redhat.com, jaegeuk@kernel.org, chao@kernel.org, hsiangkao@linux.alibaba.com, huangjianan@oppo.com, mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, akpm@linux-foundation.org, alexander.deucher@amd.com, luben.tuikov@amd.com, richard@nod.at, liushixin2@huawei.com Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8281fc72-948a-162d-6e5f-a9fe29d8ee46@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 24, 2022 at 10:39:44PM +0800, Yang Yingliang wrote: > > On 2022/10/24 21:52, Greg KH wrote: > > On Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > > > Inject fault while loading module, kset_register() may fail. > > > If it fails, the name allocated by kobject_set_name() which > > > is called before kset_register() is leaked, because refcount > > > of kobject is hold in kset_init(). > > > > > > As a kset may be embedded in a larger structure which needs > > > be freed in release() function or error path in callers, we > > > can not call kset_put() in kset_register(), or it will cause > > > double free, so just call kfree_const() to free the name and > > > set it to NULL. > > > > > > With this fix, the callers don't need to care about the name > > > freeing and call an extra kset_put() if kset_register() fails. > > > > > > Suggested-by: Luben Tuikov > > > Signed-off-by: Yang Yingliang > > > --- > > > v1 -> v2: > > > Free name inside of kset_register() instead of calling kset_put() > > > in drivers. > > > --- > > > lib/kobject.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index a0b2dbfcfa23..3409a89c81e5 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > > > /** > > > * kset_register() - Initialize and add a kset. > > > * @k: kset. > > > + * > > > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > > > + * which is called before kset_register() in caller need be freed. > > This comment doesn't make any sense anymore. No caller needs to worry > > about this, right? > With this fix, the name is freed inside of kset_register(), it can not be > accessed, Agreed. > if it allocated dynamically, but callers don't know this if no comment here, > they may use it in error path (something like to print error message with > it), > so how about comment like this to tell callers not to use the name: > > NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > is freed, it can not be used any more. Sure, that's a better way to word it. > > > */ > > > int kset_register(struct kset *k) > > > { > > > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > > kset_init(k); > > > err = kobject_add_internal(&k->kobj); > > > - if (err) > > > + if (err) { > > > + kfree_const(k->kobj.name); > > > + k->kobj.name = NULL; > > Why are you setting the name here to NULL? > I set it to NULL to avoid accessing bad pointer in callers, > if callers use it in error path, current callers won't use this > name pointer in error path, so we can remove this assignment? Ah, I didn't think about using it on error paths. Ideally that would never happen, but that's good to set just to make it obvious. How about adding a small comment here saying why you are setting it so we all remember it in 5 years when we look at the code again. thanks, greg k-h