public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: "Tian, Kevin" <kevin.tian@intel.com>
Cc: Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	Xiao Guangrong <guangrong.xiao@linux.intel.com>,
	Jike Song <jike.song@intel.com>,
	Kirti Wankhede <kwankhede@nvidia.com>,
	Nicolin Chen <nicolinc@nvidia.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH 4/6] vfio: Fully lock struct vfio_group::container
Date: Fri, 13 May 2022 10:29:10 -0300	[thread overview]
Message-ID: <20220513132910.GB1457796@nvidia.com> (raw)
In-Reply-To: <BN9PR11MB5276CE4C4C01A3D34F8DC48F8CCA9@BN9PR11MB5276.namprd11.prod.outlook.com>

On Fri, May 13, 2022 at 09:53:05AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Friday, May 6, 2022 8:25 AM
> > 
> > This is necessary to avoid various user triggerable races, for instance
> > racing SET_CONTAINER/UNSET_CONTAINER:
> > 
> >                                   ioctl(VFIO_GROUP_SET_CONTAINER)
> > ioctl(VFIO_GROUP_UNSET_CONTAINER)
> >  vfio_group_unset_container
> >     int users = atomic_cmpxchg(&group->container_users, 1, 0);
> >     // users == 1 container_users == 0
> >     __vfio_group_unset_container(group);
> > 
> >                                     vfio_group_set_container()
> > 	                              if (atomic_read(&group->container_users))
> 
> 	                       if (!atomic_read(...))
> 
> > 				        down_write(&container->group_lock);
> > 				        group->container = container;
> > 				        up_write(&container->group_lock);
> > 
> 
> It'd be clearer to add below step here:
> 
>        container = group->container;
> 
> otherwise if this assignment is done before above race then the
> original container is not leaked.

Not quite, it is the assignment to NULL that is trouble:

> >       down_write(&container->group_lock);
> >       group->container = NULL;
> >       up_write(&container->group_lock);

Either we leaked the original container or we leaked the new
container, and now the atomics are out of sync with the state of
group->container.

But sure, it is a touch clearer to focus only one scenario, I picked
the one where group->container is read early and we leak the new
container.

Thanks,
Jason

  reply	other threads:[~2022-05-13 13:50 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-06  0:25 [PATCH 0/6] Fully lock the container members of struct vfio_group Jason Gunthorpe
2022-05-06  0:25 ` [PATCH 1/6] vfio: Add missing locking for struct vfio_group::kvm Jason Gunthorpe
2022-05-13  9:08   ` Tian, Kevin
2022-05-13 13:16     ` Jason Gunthorpe
2022-05-06  0:25 ` [PATCH 2/6] vfio: Change struct vfio_group::opened from an atomic to bool Jason Gunthorpe
2022-05-13  9:10   ` Tian, Kevin
2022-05-06  0:25 ` [PATCH 3/6] vfio: Split up vfio_group_get_device_fd() Jason Gunthorpe
2022-05-10 19:59   ` Alex Williamson
2022-05-10 23:48     ` Jason Gunthorpe
2022-05-13  9:40   ` Tian, Kevin
2022-05-06  0:25 ` [PATCH 4/6] vfio: Fully lock struct vfio_group::container Jason Gunthorpe
2022-05-13  9:53   ` Tian, Kevin
2022-05-13 13:29     ` Jason Gunthorpe [this message]
2022-05-06  0:25 ` [PATCH 5/6] vfio: Simplify the life cycle of the group FD Jason Gunthorpe
2022-05-10 19:59   ` Alex Williamson
2022-05-12 23:35     ` Jason Gunthorpe
2022-05-13  9:57   ` Tian, Kevin
2022-05-06  0:25 ` [PATCH 6/6] vfio: Change struct vfio_group::container_users to a non-atomic int Jason Gunthorpe
2022-05-13 10:01   ` Tian, Kevin

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=20220513132910.GB1457796@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=guangrong.xiao@linux.intel.com \
    --cc=jike.song@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=nicolinc@nvidia.com \
    --cc=pbonzini@redhat.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