mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: "Tobin C. Harding" <me@tobin.cc>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tyrel Datwyler <tyreld@linux.vnet.ibm.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Miroslav Benes <mbenes@suse.cz>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: kobject_init_and_add() confusion
Date: Fri, 3 May 2019 09:56:28 +0200	[thread overview]
Message-ID: <20190503075628.kw6h2coyoft2w6o5@pathway.suse.cz> (raw)
In-Reply-To: <20190503011626.GA7416@eros.localdomain>

On Fri 2019-05-03 11:16:26, Tobin C. Harding wrote:
> On Thu, May 02, 2019 at 10:34:12AM +0200, Petr Mladek wrote:
> > On Wed 2019-05-01 09:38:03, Tobin C. Harding wrote:
> > I guess that we need two examples. I currently understand
> > it the following way:
> > 
> > 1. sysfs interface and the structure can be freed anytime:
> > 
> >    	struct A
> > 	{
> > 		struct kobject kobj;
> > 		...
> > 	};
> > 
> > 	void fn(void)
> > 	{
> > 		struct A *a;
> > 		int ret;
> > 
> > 		a = kzalloc(sizeof(*a), GFP_KERNEL);
> > 		if (!a)
> > 			return;
> > 
> > 		/*
> > 		 * Initialize structure before we make it accessible via
> > 		 * sysfs.
> > 		 */
> > 		ret = some_init_fn();
> > 		if (ret) {
> > 			goto init_err;
> > 		}
> > 
> > 		ret = kobject_init_and_add(&a->kobj, ktype, NULL, "foo");
> > 		if (ret)
> > 			goto kobj_err;
> > 
> > 		return 0;
> > 
> > 	kobj_err:
> > 		/* kobject_init() always succeds and take reference. */
> > 		kobject_put(kobj);
> > 		return ret;
> > 
> > 	init_err:
> > 		/* kobject was not initialized, simple free is enough */
> > 		kfree(a);
> > 		return ret;
> > 	}
> > 
> > 
> > 2. Structure must be registered into the subsystem before
> >    it can be made visible via sysfs:
> > 
> >    	struct A
> > 	{
> > 		struct kobject kobj;
> > 		...
> > 	};
> > 
> > 	void fn(void)
> > 	{
> > 		struct A *a;
> > 		int ret;
> > 
> > 		a = kzalloc(sizeof(*a), GFP_KERNEL);
> > 		if (!a)
> > 			return;
> > 
> > 		ret = some_init_fn();
> > 		if (ret) {
> > 			goto init_err;
> > 		}
> > 
> > 		/*
> > 		 * Structure is in a reasonable state and can be freed
> > 		 * via the kobject release callback.
> > 		 */
> > 		kobject_init(&a->kobj);
> > 
> > 		/*
> > 		 * Register the structure so that it can cooperate
> > 		 * with the rest of the system.
> > 		 */
> > 		ret = register_fn(a);
> > `		if (ret)
> > 			goto register_err;
> > 
> > 
> > 		/* Make it visible via sysfs */
> > 		ret = kobject_add(&a->kobj, ktype, NULL, "foo");
> > 		if (ret) {
> > 			goto kobj_add_err;
> > 		}
> > 
> > 		/* Manipulate the structure somehow */
> > 		ret = action_fn(a);
> > 		if (ret)
> > 			goto action_err;
> > 
> > 		mutex_unlock(&my_mutex);
> > 		return 0;
> > 
> > 	action_err:
> > 		/*
> > 		 * Destroy sysfs interface but the structure
> > 		 * is still needed.
> > 		 */
> > 		kobject_del(&a->kboject);
> > 	kobject_add_err:
> > 		/* Make it invisible to the system. */
> > 		unregister_fn(a);
> > 	register_err:
> > 		/* Release the structure unsing the kobject callback */
> > 		kobject_put(&a->kobj);
> > 		return;
> > 
> > 	init_err:
> > 		/*
> > 		 * Custom init failed. Kobject release callback would do
> > 		 * a double free or so. Simple free is enough.
> > 		 */
> > 		 kfree(a);
> > 	}
> > 
> > I would really prefer if we clearly understand where each variant makes
> > sense before we start modifying the code and documentation.
> 
> Hi Petr,
> 
> Shall we work these two examples into samples/kobject/.  I'm AFK now for
> the rest of the week but I can do it on Monday if you don't mind me
> doing it?

Sounds good to me. The current samples/kobject/kobject-example shows
the most simple case when the kobject is standalone. While the
above samples shows how to have it bundled in a bigger structure.

Thanks,
Petr

      reply	other threads:[~2019-05-03  7:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-30 23:38 Tobin C. Harding
2019-05-01  7:54 ` Rafael J. Wysocki
2019-05-01 21:44   ` Tobin C. Harding
2019-05-10  2:35   ` Tobin C. Harding
2019-05-10  7:52     ` Rafael J. Wysocki
2019-05-10  9:40     ` Petr Mladek
2019-05-11  6:32       ` Tobin C. Harding
2019-05-01 11:10 ` Greg Kroah-Hartman
2019-05-01 21:58   ` Tobin C. Harding
2019-05-02  7:19     ` Greg Kroah-Hartman
2019-05-02  8:34 ` Petr Mladek
2019-05-02  9:06   ` Greg Kroah-Hartman
2019-05-03  1:16   ` Tobin C. Harding
2019-05-03  7:56     ` Petr Mladek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190503075628.kw6h2coyoft2w6o5@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=me@tobin.cc \
    --cc=rafael@kernel.org \
    --cc=tyreld@linux.vnet.ibm.com \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®