public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kobject: Improve docs for kobject_add/del
@ 2019-04-28  0:48 Tobin C. Harding
  2019-04-28 16:16 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Tobin C. Harding @ 2019-04-28  0:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Tobin C. Harding, linux-kernel

There is currently some confusion on how to wind back
kobject_init_and_add() during the error paths in code that uses this
function.

Add documentation to kobject_add() and kobject_del() to help clarify the
usage.

Signed-off-by: Tobin C. Harding <tobin@kernel.org>
---

The assumption is that this is the correct usage, and that's what I've
tried to document.  Is this correct?

void fn(void)
{
	int ret;

	ret = kobject_init_and_add(kobj, ktype, NULL, "foo");
	if (ret) {
		kobject_put(kobj);
		return -1;
	}

	ret = some_init_fn();
	if (ret)
		goto err;

	ret = some_other_init_fn();
	if (ret)
		goto other_err;

	kobject_uevent(kobj, KOBJ_ADD);
	return 0;

other_err:
	other_clean_up_fn();
err:
	kobject_del(kobj);
	return ret;
}

thanks,
Tobin.

 lib/kobject.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/lib/kobject.c b/lib/kobject.c
index aa89edcd2b63..b2670671977b 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -397,15 +397,19 @@ static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
  * is assigned to the kobject, then the kobject will be located in the
  * root of the sysfs tree.
  *
- * If this function returns an error, kobject_put() must be called to
- * properly clean up the memory associated with the object.
- * Under no instance should the kobject that is passed to this function
- * be directly freed with a call to kfree(), that can leak memory.
- *
  * Note, no "add" uevent will be created with this call, the caller should set
  * up all of the necessary sysfs files for the object and then call
  * kobject_uevent() with the UEVENT_ADD parameter to ensure that
  * userspace is properly notified of this kobject's creation.
+ *
+ * Return: If this function returns an error, kobject_put() must be
+ *         called to properly clean up the memory associated with the
+ *         object.  Under no instance should the kobject that is passed
+ *         to this function be directly freed with a call to kfree(),
+ *         that can leak memory.
+ *
+ *         If this call returns successfully and you later need to unwind
+ *         kobject_add() for the error path you should call kobject_del().
  */
 int kobject_add(struct kobject *kobj, struct kobject *parent,
 		const char *fmt, ...)
@@ -580,6 +584,9 @@ EXPORT_SYMBOL_GPL(kobject_move);
 /**
  * kobject_del - unlink kobject from hierarchy.
  * @kobj: object.
+ *
+ * This is the function that should be called to delete an object
+ * successfully added via kobject_add().
  */
 void kobject_del(struct kobject *kobj)
 {
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] kobject: Improve docs for kobject_add/del
  2019-04-28  0:48 [PATCH] kobject: Improve docs for kobject_add/del Tobin C. Harding
@ 2019-04-28 16:16 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2019-04-28 16:16 UTC (permalink / raw)
  To: Tobin C. Harding; +Cc: Rafael J. Wysocki, linux-kernel

On Sun, Apr 28, 2019 at 10:48:10AM +1000, Tobin C. Harding wrote:
> There is currently some confusion on how to wind back
> kobject_init_and_add() during the error paths in code that uses this
> function.
> 
> Add documentation to kobject_add() and kobject_del() to help clarify the
> usage.
> 
> Signed-off-by: Tobin C. Harding <tobin@kernel.org>
> ---
> 
> The assumption is that this is the correct usage, and that's what I've
> tried to document.  Is this correct?
> 
> void fn(void)
> {
> 	int ret;
> 
> 	ret = kobject_init_and_add(kobj, ktype, NULL, "foo");
> 	if (ret) {
> 		kobject_put(kobj);
> 		return -1;

Don't make up error numbers, use a real one :)

> 	}
> 
> 	ret = some_init_fn();
> 	if (ret)
> 		goto err;
> 
> 	ret = some_other_init_fn();
> 	if (ret)
> 		goto other_err;
> 
> 	kobject_uevent(kobj, KOBJ_ADD);
> 	return 0;
> 
> other_err:
> 	other_clean_up_fn();
> err:
> 	kobject_del(kobj);
> 	return ret;
> }

Yes, this looks correct.

I've queued both of these patches up now, thanks!

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-28 16:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-28  0:48 [PATCH] kobject: Improve docs for kobject_add/del Tobin C. Harding
2019-04-28 16:16 ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox