mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vladislav Bolkhovitin <vst@vlnb.net>
To: Greg KH <greg@kroah.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	scst-devel <scst-devel@lists.sourceforge.net>,
	James Bottomley <James.Bottomley@HansenPartnership.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>,
	Mike Christie <michaelc@cs.wisc.edu>,
	Vu Pham <vuhuong@mellanox.com>,
	Bart Van Assche <bart.vanassche@gmail.com>,
	James Smart <James.Smart@Emulex.Com>,
	Joe Eykholt <jeykholt@cisco.com>, Andy Yan <ayan@marvell.com>,
	Chetan Loke <generationgnu@yahoo.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Hannes Reinecke <hare@suse.de>,
	Richard Sharpe <realrichardsharpe@gmail.com>,
	Daniel Henrique Debonzi <debonzi@linux.vnet.ibm.com>
Subject: Re: [PATCH 8/19]: SCST SYSFS interface implementation
Date: Thu, 14 Oct 2010 23:48:17 +0400	[thread overview]
Message-ID: <4CB75E81.7000208@vlnb.net> (raw)
In-Reply-To: <20101012190345.GA25737@kroah.com>

Greg KH, on 10/12/2010 11:03 PM wrote:
> On Tue, Oct 12, 2010 at 10:53:45PM +0400, Vladislav Bolkhovitin 
> wrote:
>>> Seriously, you CAN NOT DO THIS!  If you embed a kobject in a 
>>> different structure, then you have to rely on the kobject to 
>>> handle the reference counting for that larger structure.  To do 
>>> ANYTHING else is a bug and wrong.
>>> 
>>> Please read the kobject documentation and fix this code up
>>> before submitting it again.
>> 
>> Sure, I have read it and we rely on the kobject to handle the 
>> reference counting for the larger structure. It's only done not in
>>  a straightforward way, because the way it is implemented is 
>> simpler for us + for some other reasons.
> 
> Sorry, but I don't buy it.
> 
>> For instance, for structure scst_tgt it is done using 
>> tgt_kobj_release_cmpl completion. When a target driver calls 
>> scst_unregister_target(), scst_unregister_target() in the end
>> calls scst_tgt_sysfs_del(), which calls kobject_put(&tgt->tgt_kobj)
>> and wait for tgt_kobj_release_cmpl to complete.
> 
> Wait, why shouldn't the release then free the memory?
> 
>> At this point tgt_kobj can be taken only by the SYSFS. 
>> Scst_tgt_sysfs_del() can wait as much as needed until the SYSFS 
>> code released it. As far as I can see, it can't be forever, so it's
>> OK.
> 
> I don't understand, why can't you just free the memory, what are you 
> having to wait for?
> 
> You are only having one kobject for your structure, right?  If so, 
> then free the memory in the release, to wait for something else to 
> free the memory is wrong.
> 
>> Then, after scst_tgt_sysfs_del() returned,
>> scst_unregister_target() will free scst_tgt together with embedded
>> tgt_kobj.
> 
> As no other kernel code is like this, I don't think it's valid to be 
> doing so, sorry.
> 
> Please fix this.

I'm sorry, but after I started implementing it I'm confused..

Originally I thought you are asking to make tgt_kobj be not embedded in
struct scst_tgt, but a pointer in it, so scst_tgtt_release() will
kfree() tgt_kobj. Hence, all the above I wrote about why we have
tgt_kobj embedded.

But now I feel like you are asking that scst_tgtt_release() should
kfree() tgt, not tgt_kobj.

Is it correct?

If yes, we did it this way to make errors processing uniform and
straightforward. In the code (simplified) our current errors recovery
looks like:

void scst_tgt_sysfs_del(struct scst_tgt *tgt)
{
...
	kobject_put(&tgt->tgt_kobj);
	wait_for_completion(&tgt->tgt_kobj_release_cmpl);
}

int scst_tgt_sysfs_create(struct scst_tgt *tgt)
{
	init_completion(&tgt->tgt_kobj_release_cmpl);

	res = kobject_init_and_add(&tgt->tgt_kobj, &tgt_ktype,
			&tgt->tgtt->tgtt_kobj, tgt->tgt_name);
	if (res != 0)
		goto out;

	res = sysfs_create_file(&tgt->tgt_kobj,
			&tgt_enable_attr.attr);
	if (res != 0)
		goto out_err;
...

out:
	return res;

out_err:
	scst_tgt_sysfs_del(tgt);
	goto out;
}

struct scst_tgt *scst_register_target()
{
	struct scst_tgt *tgt;
	int rc = 0;

	TRACE_ENTRY();

	rc = scst_alloc_tgt();
	if (rc != 0)
		goto out;
...
	tgt->tgt_name = kmalloc(strlen(target_name) + 1, GFP_KERNEL);
	if (tgt->tgt_name == NULL)
		goto out_free_tgt;
...
	mutex_lock();

	rc = scst_tgt_sysfs_create(tgt);
	if (rc < 0)
		goto out_unlock;

	tgt->default_acg = scst_alloc_add_acg();
	if (tgt->default_acg == NULL)
		goto out_sysfs_del;

...

out:
	return tgt;

out_sysfs_del:
	mutex_unlock();
	scst_tgt_sysfs_del(tgt)
	goto out_free_tgt;

out_unlock:
	mutex_unlock();

out_free_tgt:
	scst_free_tgt(tgt);
}

We have a simple and straightforward errors recovery semantic: if
scst_tgt_sysfs_create() failed, then tgt_kobj returned deinited as if
scst_tgt_sysfs_create() has never been called. This is a regular
practice in the kernel: don't return half-initialized objects.

If we implement freeing tgt in scst_tgtt_release() as you requesting, we
will need to add in the error recovery path additional recovery code to
track and delete half-initialized tgt_kobj. Particularly, we will need
to add additional flag to track that tgt_kobj initialized, hence needs
deleting. Similar flag and code will be added in all similar to scst_tgt
SCST objects.

This code will be quite errors prone as you can see on the example of
device_register() which on failure requires device_put() to be called
(http://lkml.org/lkml/2010/9/19/93). (I'm not questioning
device_register() implementation, there might be very good reasons to
implement it this way (I don't know), I mean, it is too easy to forget
to do the needed recovery of the half-created objects as this case
demonstrating.)

Could you confirm if I understand you correctly and need to implement
freeing tgt in the kobject release() function, please?

Thanks you very much,
Vlad

  reply	other threads:[~2010-10-14 19:49 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-01 21:34 [PATCHv4 0/19]: New SCSI target framework (SCST) with dev handlers and 2 target drivers Vladislav Bolkhovitin
2010-10-01 21:36 ` [PATCH 1/19]: Integration of SCST into the Linux kernel tree Vladislav Bolkhovitin
2010-10-01 21:36 ` [PATCH 2/19]: SCST core's Makefile and Kconfig Vladislav Bolkhovitin
2010-10-01 21:38 ` [PATCH 3/19]: SCST public headers Vladislav Bolkhovitin
2010-10-01 21:39 ` [PATCH 4/19]: SCST main management files and private headers Vladislav Bolkhovitin
2010-10-01 21:42 ` [PATCH 5/19]: SCST implementation of the SCSI target state machine Vladislav Bolkhovitin
2010-10-01 21:43 ` [PATCH 6/19]: SCST internal library functions Vladislav Bolkhovitin
2010-10-01 21:44 ` [PATCH 7/19]: SCST Persistent Reservations implementation Vladislav Bolkhovitin
2010-10-01 21:46 ` [PATCH 8/19]: SCST SYSFS interface implementation Vladislav Bolkhovitin
2010-10-09 21:20   ` Greg KH
2010-10-11 19:29     ` Vladislav Bolkhovitin
2010-10-11 21:32       ` Greg KH
2010-10-12 18:53         ` Vladislav Bolkhovitin
2010-10-12 19:03           ` Greg KH
2010-10-14 19:48             ` Vladislav Bolkhovitin [this message]
2010-10-14 20:04               ` Greg KH
2010-10-22 17:30                 ` Vladislav Bolkhovitin
2010-10-22 17:56                   ` Greg KH
2010-10-22 18:40                     ` Vladislav Bolkhovitin
2010-10-22 18:54                       ` Greg KH
2010-11-08 19:58                         ` Vladislav Bolkhovitin
2010-11-09  0:28                           ` Greg KH
2010-11-09 20:06                             ` Vladislav Bolkhovitin
2010-11-10  9:58                               ` Boaz Harrosh
2010-11-10 20:19                                 ` Vladislav Bolkhovitin
2010-11-10 20:29                                   ` Joe Eykholt
2010-11-10 20:38                                     ` Vladislav Bolkhovitin
2010-11-10 20:42                                     ` Joe Eykholt
2010-11-11  9:59                                   ` Boaz Harrosh
2010-11-11 12:04                                     ` Greg KH
2010-11-11 14:05                                       ` Boaz Harrosh
2010-11-11 14:16                                         ` Greg KH
2010-11-11 14:19                                           ` Boaz Harrosh
2010-11-11 20:50                                     ` Vladislav Bolkhovitin
2010-11-12  1:23                                       ` Dmitry Torokhov
2010-11-12 12:09                                         ` Bart Van Assche
2010-11-12 18:44                                           ` Dmitry Torokhov
2010-11-13 10:52                                             ` Bart Van Assche
2010-11-13 17:20                                         ` Vladislav Bolkhovitin
2010-11-13 23:59                                           ` Greg KH
2010-11-15  6:59                                             ` Dmitry Torokhov
2010-11-15 17:53                                               ` Bart Van Assche
2010-11-15 20:36                                               ` Vladislav Bolkhovitin
2010-11-15  9:46                                             ` Boaz Harrosh
2010-11-15 16:16                                               ` Greg KH
2010-11-15 17:19                                                 ` Boaz Harrosh
2010-11-15 17:49                                                   ` Bart Van Assche
2010-11-15 20:19                                                     ` Nicholas A. Bellinger
2010-11-16 13:12                                                       ` Vladislav Bolkhovitin
2010-11-16 11:59                                                     ` [Scst-devel] " Richard Williams
2010-11-16 13:17                                                       ` Vladislav Bolkhovitin
2010-11-18 21:02                                                         ` Vladislav Bolkhovitin
2010-11-18 21:46                                                           ` Greg KH
2010-11-19 18:00                                                             ` Vladislav Bolkhovitin
2010-11-19 20:22                                                               ` Dmitry Torokhov
2010-11-19 20:50                                                                 ` Vladislav Bolkhovitin
2010-11-19 21:16                                                                   ` Greg KH
2010-11-24 20:35                                                                     ` Vladislav Bolkhovitin
2010-11-19 21:19                                                               ` Greg KH
2010-12-10 12:06                                                                 ` Bart Van Assche
2010-12-10 19:36                                                                   ` Greg KH
2010-12-14 14:10                                                                     ` Bart Van Assche
2010-11-19 18:01                                                             ` Bart Van Assche
2010-11-15 20:39                                                   ` Vladislav Bolkhovitin
2010-11-15 20:39                                                 ` Vladislav Bolkhovitin
2010-11-15 17:45                                             ` Bart Van Assche
2010-11-15 18:44                                               ` Greg KH
2010-11-15 20:39                                                 ` Vladislav Bolkhovitin
2010-11-15 22:13                                                   ` Greg KH
2010-11-16  5:04                                                     ` Joe Eykholt
2010-11-16  6:03                                                       ` Nicholas A. Bellinger
2010-11-16  8:49                                                       ` Florian Mickler
2010-11-16 13:18                                                       ` Vladislav Bolkhovitin
2010-11-16  7:15                                                     ` Bart Van Assche
2010-11-16 13:19                                                     ` Vladislav Bolkhovitin
2010-11-15 20:36                                             ` Vladislav Bolkhovitin
2010-11-15  7:04                                           ` Dmitry Torokhov
2010-11-15 20:37                                             ` Vladislav Bolkhovitin
2010-11-15 21:14                                               ` Dmitry Torokhov
2010-11-16 13:13                                                 ` Vladislav Bolkhovitin
2010-10-01 21:46 ` [PATCH 9/19]: SCST debugging support routines Vladislav Bolkhovitin
2010-10-01 21:48 ` [PATCH 10/19]: SCST SGV cache Vladislav Bolkhovitin
2010-10-01 21:48 ` [PATCH 11/19]: SCST core's docs Vladislav Bolkhovitin
2010-10-01 21:49 ` [PATCH 12/19]: SCST dev handlers' Makefile Vladislav Bolkhovitin
2010-10-01 21:50 ` [PATCH 13/19]: SCST vdisk dev handler Vladislav Bolkhovitin
2010-10-01 21:51 ` [PATCH 14/19]: SCST pass-through dev handlers Vladislav Bolkhovitin
2010-10-01 21:53 ` [PATCH 15/19]: Implementation of blk_rq_map_kern_sg() Vladislav Bolkhovitin
2010-10-01 21:57 ` [PATCH 16/19]: scst_local target driver Vladislav Bolkhovitin
2010-10-01 21:58 ` [PATCH 17/19]: SCST InfiniBand SRP " Vladislav Bolkhovitin
2010-10-01 22:04 ` [PATCH 18/19]: ibmvstgt: Port from tgt to SCST Vladislav Bolkhovitin
2010-10-01 22:05 ` [PATCH 19/19]: tgt: Removal Vladislav Bolkhovitin
2010-10-02  7:40 ` [PATCHv4 0/19]: New SCSI target framework (SCST) with dev handlers and 2 target drivers Bart Van Assche
2010-10-06 20:21 ` [Scst-devel] " Steve Modica

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=4CB75E81.7000208@vlnb.net \
    --to=vst@vlnb.net \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=James.Smart@Emulex.Com \
    --cc=akpm@linux-foundation.org \
    --cc=ayan@marvell.com \
    --cc=bart.vanassche@gmail.com \
    --cc=debonzi@linux.vnet.ibm.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=fujita.tomonori@lab.ntt.co.jp \
    --cc=generationgnu@yahoo.com \
    --cc=greg@kroah.com \
    --cc=hare@suse.de \
    --cc=jeykholt@cisco.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=michaelc@cs.wisc.edu \
    --cc=realrichardsharpe@gmail.com \
    --cc=scst-devel@lists.sourceforge.net \
    --cc=vuhuong@mellanox.com \
    /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

Powered by JetHome