All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Cornelia Huck <cohuck@redhat.com>
Cc: Greg Kroah-Hartman <gregkh@suse.de>,
	weiping zhang <zhangweiping@didichuxing.com>,
	virtualization@lists.linux-foundation.org
Subject: Re: [PATCH v2 1/3] virtio_pci: use put_device instead of kfree
Date: Sun, 17 Dec 2017 00:13:38 +0200	[thread overview]
Message-ID: <20171215170147-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20171215132127.71c33352.cohuck@redhat.com>

On Fri, Dec 15, 2017 at 01:21:27PM +0100, Cornelia Huck wrote:
> On Thu, 14 Dec 2017 21:13:28 +0200
> "Michael S. Tsirkin" <mst@redhat.com> wrote:
> 
> > On Tue, Dec 12, 2017 at 09:24:02PM +0800, weiping zhang wrote:
> > > As mentioned at drivers/base/core.c:
> > > /*
> > >  * NOTE: _Never_ directly free @dev after calling this function, even
> > >  * if it returned an error! Always use put_device() to give up the
> > >  * reference initialized in this function instead.
> > >  */
> > > so we don't free vp_dev until vp_dev->vdev.dev.release be called.  
> > 
> > seeing as 5739411acbaa63a6c22c91e340fdcdbcc7d82a51 adding these
> > annotations went to stable, should this go there too?
> > 
> > > Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
> > > Reviewed-by: Cornelia Huck <cohuck@redhat.com>  
> > 
> > OK but this relies on users knowing that register_virtio_device
> > calls device_register. I think we want to add a comment
> > to register_virtio_device.
> > 
> > Also the cleanup is uglified.
> > 
> > I really think the right thing would be to change device_register making
> > it safe to kfree. People have the right to expect register on failure to
> > have no effect.
> > 
> > That just might be too hard to implement though.
> 
> Yes. The main problem is that device_register() at some point makes the
> structure visible to others, at which point they may obtain a
> reference. If that happened, you cannot clean up unless that other
> party gave up their reference -- which means your only chance to get
> this right is the current put_device() approach.
> 
> It *is* problematic if all of that stuff is hidden behind too many
> calling layers. If you have the device_initialize() -> device_add()
> calling sequence, having to do a put_device() on failure is much more
> obvious. But as you usually don't pass in a pure struct device but
> something embedding it, the put_device() needs to be done on the
> outermost level.
> 
> Commenting can help here, as would probably a static checker for that
> code pattern.

A semantic patch is probably the best we can do here.

-- 
MST

  reply	other threads:[~2017-12-16 22:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-12 13:13 [PATCH v2 0/3] fix cleanup for fail to register_virtio_device weiping zhang
2017-12-12 13:24 ` [PATCH v2 1/3] virtio_pci: use put_device instead of kfree weiping zhang
2017-12-14 19:13   ` Michael S. Tsirkin
2017-12-15  1:38     ` weiping zhang
2017-12-15  1:48       ` Michael S. Tsirkin
2017-12-15 12:32         ` Cornelia Huck
2017-12-15 12:21     ` Cornelia Huck
2017-12-16 22:13       ` Michael S. Tsirkin [this message]
2017-12-12 13:24 ` [PATCH v2 2/3] virtio: " weiping zhang
2017-12-13  8:27   ` Cornelia Huck
2017-12-12 13:25 ` [PATCH v2 3/3] virtio: put reference count of virtio_device.dev weiping zhang

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=20171215170147-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=gregkh@suse.de \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=zhangweiping@didichuxing.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.