[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <DS0PR11MB752976AA5F3F5FF6A1CC246FC3DB9@DS0PR11MB7529.namprd11.prod.outlook.com>
Date: Tue, 7 Feb 2023 07:50:40 +0000
From: "Liu, Yi L" <yi.l.liu@...el.com>
To: "Zhao, Yan Y" <yan.y.zhao@...el.com>
CC: "alex.williamson@...hat.com" <alex.williamson@...hat.com>,
"jgg@...dia.com" <jgg@...dia.com>,
"Tian, Kevin" <kevin.tian@...el.com>,
"linux-s390@...r.kernel.org" <linux-s390@...r.kernel.org>,
"yi.y.sun@...ux.intel.com" <yi.y.sun@...ux.intel.com>,
"kvm@...r.kernel.org" <kvm@...r.kernel.org>,
"mjrosato@...ux.ibm.com" <mjrosato@...ux.ibm.com>,
"jasowang@...hat.com" <jasowang@...hat.com>,
"cohuck@...hat.com" <cohuck@...hat.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"peterx@...hat.com" <peterx@...hat.com>,
"eric.auger@...hat.com" <eric.auger@...hat.com>,
"nicolinc@...dia.com" <nicolinc@...dia.com>,
"shameerali.kolothum.thodi@...wei.com"
<shameerali.kolothum.thodi@...wei.com>,
"suravee.suthikulpanit@....com" <suravee.suthikulpanit@....com>,
"chao.p.peng@...ux.intel.com" <chao.p.peng@...ux.intel.com>,
"lulu@...hat.com" <lulu@...hat.com>,
"intel-gvt-dev@...ts.freedesktop.org"
<intel-gvt-dev@...ts.freedesktop.org>,
"intel-gfx@...ts.freedesktop.org" <intel-gfx@...ts.freedesktop.org>
Subject: RE: [PATCH v2 13/14] vfio: Add ioctls for device cdev using iommufd
> From: Yan Zhao <yan.y.zhao@...el.com>
> Sent: Tuesday, February 7, 2023 11:41 AM
>
> On Mon, Feb 06, 2023 at 01:05:31AM -0800, Yi Liu wrote:
> ...
>
> > +void vfio_device_cdev_close(struct vfio_device_file *df)
> > +{
> > + mutex_lock(&df->device->dev_set->lock);
> > + vfio_device_close(df);
> > + vfio_device_put_kvm(df->device);
> > + mutex_unlock(&df->device->dev_set->lock);
> > +}
> > +
>
> ...
>
> > +long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df,
> > + unsigned long arg)
> > +{
> > + struct vfio_device *device = df->device;
> > + struct vfio_device_bind_iommufd bind;
> > + struct iommufd_ctx *iommufd = NULL;
> > + unsigned long minsz;
> > + struct fd f;
> > + int ret;
> > +
> > + minsz = offsetofend(struct vfio_device_bind_iommufd, iommufd);
> > +
> > + if (copy_from_user(&bind, (void __user *)arg, minsz))
> > + return -EFAULT;
> > +
> > + if (bind.argsz < minsz || bind.flags)
> > + return -EINVAL;
> > +
> > + if (!device->ops->bind_iommufd)
> > + return -ENODEV;
> > +
> > + mutex_lock(&device->dev_set->lock);
> > + /*
> > + * If already been bound to an iommufd, or already set noiommu
> > + * then fail it.
> > + */
> > + if (df->iommufd || df->noiommu) {
> > + ret = -EINVAL;
> > + goto out_unlock;
> > + }
> > +
> > + /* iommufd < 0 means noiommu mode */
> > + if (bind.iommufd < 0) {
> > + if (!capable(CAP_SYS_RAWIO)) {
> > + ret = -EPERM;
> > + goto out_unlock;
> > + }
> > + df->noiommu = true;
> > + } else {
> > + f = fdget(bind.iommufd);
> > + if (!f.file) {
> > + ret = -EBADF;
> > + goto out_unlock;
> > + }
> > + iommufd = iommufd_ctx_from_file(f.file);
> > + if (IS_ERR(iommufd)) {
> > + ret = PTR_ERR(iommufd);
> > + goto out_put_file;
> > + }
> > + }
> > +
> > + /*
> > + * Before the first device open, get the KVM pointer currently
> > + * associated with the device file (if there is) and obtain a
> > + * reference. This reference is held until device closed. Save
> > + * the pointer in the device for use by drivers.
> > + */
> > + vfio_device_get_kvm_safe(df);
> > +
> > + df->iommufd = iommufd;
> > + ret = vfio_device_open(df, &bind.out_devid, NULL);
> > + if (ret)
> > + goto out_put_kvm;
> > +
> > + ret = copy_to_user((void __user *)arg + minsz,
> > + &bind.out_devid,
> > + sizeof(bind.out_devid)) ? -EFAULT : 0;
> > + if (ret)
> > + goto out_close_device;
> > +
> > + if (iommufd)
> > + fdput(f);
> > + else if (df->noiommu)
> > + dev_warn(device->dev, "vfio-noiommu device used by user
> "
> > + "(%s:%d)\n", current->comm,
> task_pid_nr(current));
> > + mutex_unlock(&device->dev_set->lock);
> > + return 0;
> > +
> > +out_close_device:
> > + vfio_device_close(df);
> vfio_device_close() is called here if any error after vfio_device_open().
> But it will also be called unconditionally in vfio_device_cdev_close() and
> cause a wrong value of device->open_count.
Oh, yes yes. Good catch. Vfio_device_cdev_close() should check either the
open_count or access_granted.
> df->access_granted in patch 07 can also be of wrong true value after
> this vfio_device_close().
access_granted will surely be wrong if open_count is not correctly
counted.
Regards,
Yi Liu
Powered by blists - more mailing lists