Commit df7bf834 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'vfio-v7.1-rc1-pt2' of https://github.com/awilliam/linux-vfio

Pull more VFIO updates from Alex Williamson:

 - Fix ordering of dma-buf cleanup versus device disabling in vfio-pci
   (Matt Evans)

 - Resolve an inconsistent and incorrect use of spinlock-irq in the
   virtio vfio-pci variant by conversion to mutex and proceed to
   modernize and simplify driver with use of guards (Alex Williamson)

 - Resurrect the removal of the remaining class_create() call in vfio,
   replacing with const struct class and class_register() (Jori
   Koolstra, Alex Williamson)

 - Fix NULL pointer dereference, properly serialize interrupt setup, and
   cleanup interrupt state tracking in the cdx vfio bus driver (Prasanna
   Kumar T S M, Alex Williamson)

* tag 'vfio-v7.1-rc1-pt2' of https://github.com/awilliam/linux-vfio:
  vfio/cdx: Consolidate MSI configured state onto cdx_irqs
  vfio/cdx: Serialize VFIO_DEVICE_SET_IRQS with a per-device mutex
  vfio/cdx: Fix NULL pointer dereference in interrupt trigger path
  vfio: replace vfio->device_class with a const struct class
  vfio/virtio: Use guard() for bar_mutex in legacy I/O
  vfio/virtio: Use guard() for migf->lock where applicable
  vfio/virtio: Use guard() for list_lock where applicable
  vfio/virtio: Convert list_lock from spinlock to mutex
  vfio/pci: Clean up DMABUFs before disabling function
parents 429e6c7f 30471982
Loading
Loading
Loading
Loading
+18 −20
Original line number Diff line number Diff line
@@ -32,26 +32,27 @@ static int vfio_cdx_msi_enable(struct vfio_cdx_device *vdev, int nvec)
		return -ENOMEM;

	ret = cdx_enable_msi(cdx_dev);
	if (ret) {
		kfree(vdev->cdx_irqs);
		return ret;
	}
	if (ret)
		goto err_free;

	/* Allocate cdx MSIs */
	ret = msi_domain_alloc_irqs(dev, MSI_DEFAULT_DOMAIN, nvec);
	if (ret) {
		cdx_disable_msi(cdx_dev);
		kfree(vdev->cdx_irqs);
		return ret;
	}
	if (ret)
		goto err_disable;

	for (msi_idx = 0; msi_idx < nvec; msi_idx++)
		vdev->cdx_irqs[msi_idx].irq_no = msi_get_virq(dev, msi_idx);

	vdev->msi_count = nvec;
	vdev->config_msi = 1;

	return 0;

err_disable:
	cdx_disable_msi(cdx_dev);
err_free:
	kfree(vdev->cdx_irqs);
	vdev->cdx_irqs = NULL;
	return ret;
}

static int vfio_cdx_msi_set_vector_signal(struct vfio_cdx_device *vdev,
@@ -129,7 +130,7 @@ static void vfio_cdx_msi_disable(struct vfio_cdx_device *vdev)

	vfio_cdx_msi_set_block(vdev, 0, vdev->msi_count, NULL);

	if (!vdev->config_msi)
	if (!vdev->cdx_irqs)
		return;

	msi_domain_free_irqs_all(dev, MSI_DEFAULT_DOMAIN);
@@ -138,7 +139,6 @@ static void vfio_cdx_msi_disable(struct vfio_cdx_device *vdev)

	vdev->cdx_irqs = NULL;
	vdev->msi_count = 0;
	vdev->config_msi = 0;
}

static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
@@ -152,6 +152,8 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
	if (start + count > cdx_dev->num_msi)
		return -EINVAL;

	guard(mutex)(&vdev->cdx_irqs_lock);

	if (!count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
		vfio_cdx_msi_disable(vdev);
		return 0;
@@ -161,7 +163,7 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
		s32 *fds = data;
		int ret;

		if (vdev->config_msi)
		if (vdev->cdx_irqs)
			return vfio_cdx_msi_set_block(vdev, start, count,
						  fds);
		ret = vfio_cdx_msi_enable(vdev, cdx_dev->num_msi);
@@ -175,6 +177,9 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
		return ret;
	}

	if (!vdev->cdx_irqs)
		return -EINVAL;

	for (i = start; i < start + count; i++) {
		if (!vdev->cdx_irqs[i].trigger)
			continue;
@@ -206,12 +211,5 @@ int vfio_cdx_set_irqs_ioctl(struct vfio_cdx_device *vdev,
/* Free All IRQs for the given device */
void vfio_cdx_irqs_cleanup(struct vfio_cdx_device *vdev)
{
	/*
	 * Device does not support any interrupt or the interrupts
	 * were not configured
	 */
	if (!vdev->cdx_irqs)
		return;

	vfio_cdx_set_msi_trigger(vdev, 0, 0, 0, VFIO_IRQ_SET_DATA_NONE, NULL);
}
+19 −0
Original line number Diff line number Diff line
@@ -8,6 +8,23 @@

#include "private.h"

static int vfio_cdx_init_dev(struct vfio_device *core_vdev)
{
	struct vfio_cdx_device *vdev =
		container_of(core_vdev, struct vfio_cdx_device, vdev);

	mutex_init(&vdev->cdx_irqs_lock);
	return 0;
}

static void vfio_cdx_release_dev(struct vfio_device *core_vdev)
{
	struct vfio_cdx_device *vdev =
		container_of(core_vdev, struct vfio_cdx_device, vdev);

	mutex_destroy(&vdev->cdx_irqs_lock);
}

static int vfio_cdx_open_device(struct vfio_device *core_vdev)
{
	struct vfio_cdx_device *vdev =
@@ -273,6 +290,8 @@ static int vfio_cdx_mmap(struct vfio_device *core_vdev,

static const struct vfio_device_ops vfio_cdx_ops = {
	.name		= "vfio-cdx",
	.init		= vfio_cdx_init_dev,
	.release	= vfio_cdx_release_dev,
	.open_device	= vfio_cdx_open_device,
	.close_device	= vfio_cdx_close_device,
	.ioctl		= vfio_cdx_ioctl,
+3 −1
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
#ifndef VFIO_CDX_PRIVATE_H
#define VFIO_CDX_PRIVATE_H

#include <linux/mutex.h>

#define VFIO_CDX_OFFSET_SHIFT    40

static inline u64 vfio_cdx_index_to_offset(u32 index)
@@ -31,11 +33,11 @@ struct vfio_cdx_region {
struct vfio_cdx_device {
	struct vfio_device	vdev;
	struct vfio_cdx_region	*regions;
	struct mutex		cdx_irqs_lock;
	struct vfio_cdx_irq	*cdx_irqs;
	u32			flags;
#define BME_SUPPORT BIT(0)
	u32			msi_count;
	u8			config_msi;
};

#ifdef CONFIG_GENERIC_MSI_IRQ
+1 −7
Original line number Diff line number Diff line
@@ -293,14 +293,8 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df,
	return 0;
}

static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
int vfio_cdev_init(void)
{
	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
}

int vfio_cdev_init(struct class *device_class)
{
	device_class->devnode = vfio_device_devnode;
	return alloc_chrdev_region(&device_devt, 0,
				   MINORMASK + 1, "vfio-dev");
}
+2 −2
Original line number Diff line number Diff line
@@ -734,10 +734,10 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
#if IS_ENABLED(CONFIG_EEH)
	eeh_dev_release(vdev->pdev);
#endif
	vfio_pci_core_disable(vdev);

	vfio_pci_dma_buf_cleanup(vdev);

	vfio_pci_core_disable(vdev);

	mutex_lock(&vdev->igate);
	vfio_pci_eventfd_replace_locked(vdev, &vdev->err_trigger, NULL);
	vfio_pci_eventfd_replace_locked(vdev, &vdev->req_trigger, NULL);
Loading