Skip to content

Commit 9048c73

Browse files
yiliu1765awilliam
authored andcommitted
vfio-iommufd: Add detach_ioas support for physical VFIO devices
This prepares for adding DETACH ioctl for physical VFIO devices. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Tested-by: Terrence Xu <[email protected]> Tested-by: Nicolin Chen <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yanting Jiang <[email protected]> Tested-by: Shameer Kolothum <[email protected]> Tested-by: Zhenzhong Duan <[email protected]> Signed-off-by: Yi Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 31014ae commit 9048c73

File tree

10 files changed

+41
-5
lines changed

10 files changed

+41
-5
lines changed

Documentation/driver-api/vfio.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ similar to a file operations structure::
279279
struct iommufd_ctx *ictx, u32 *out_device_id);
280280
void (*unbind_iommufd)(struct vfio_device *vdev);
281281
int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
282+
void (*detach_ioas)(struct vfio_device *vdev);
282283
int (*open_device)(struct vfio_device *vdev);
283284
void (*close_device)(struct vfio_device *vdev);
284285
ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
@@ -315,9 +316,10 @@ container_of().
315316
- The [un]bind_iommufd callbacks are issued when the device is bound to
316317
and unbound from iommufd.
317318

318-
- The attach_ioas callback is issued when the device is attached to an
319-
IOAS managed by the bound iommufd. The attached IOAS is automatically
320-
detached when the device is unbound from iommufd.
319+
- The [de]attach_ioas callback is issued when the device is attached to
320+
and detached from an IOAS managed by the bound iommufd. However, the
321+
attached IOAS can also be automatically detached when the device is
322+
unbound from iommufd.
321323

322324
- The read/write/mmap callbacks implement the device region access defined
323325
by the device's own VFIO_DEVICE_GET_REGION_INFO ioctl.

drivers/vfio/fsl-mc/vfio_fsl_mc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ static const struct vfio_device_ops vfio_fsl_mc_ops = {
593593
.bind_iommufd = vfio_iommufd_physical_bind,
594594
.unbind_iommufd = vfio_iommufd_physical_unbind,
595595
.attach_ioas = vfio_iommufd_physical_attach_ioas,
596+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
596597
};
597598

598599
static struct fsl_mc_driver vfio_fsl_mc_driver = {

drivers/vfio/iommufd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
140140
{
141141
int rc;
142142

143+
lockdep_assert_held(&vdev->dev_set->lock);
144+
145+
if (WARN_ON(!vdev->iommufd_device))
146+
return -EINVAL;
147+
148+
if (vdev->iommufd_attached)
149+
return -EBUSY;
150+
143151
rc = iommufd_device_attach(vdev->iommufd_device, pt_id);
144152
if (rc)
145153
return rc;
@@ -148,6 +156,18 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
148156
}
149157
EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas);
150158

159+
void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev)
160+
{
161+
lockdep_assert_held(&vdev->dev_set->lock);
162+
163+
if (WARN_ON(!vdev->iommufd_device) || !vdev->iommufd_attached)
164+
return;
165+
166+
iommufd_device_detach(vdev->iommufd_device);
167+
vdev->iommufd_attached = false;
168+
}
169+
EXPORT_SYMBOL_GPL(vfio_iommufd_physical_detach_ioas);
170+
151171
/*
152172
* The emulated standard ops mean that vfio_device is going to use the
153173
* "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = {
13731373
.bind_iommufd = vfio_iommufd_physical_bind,
13741374
.unbind_iommufd = vfio_iommufd_physical_unbind,
13751375
.attach_ioas = vfio_iommufd_physical_attach_ioas,
1376+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
13761377
};
13771378

13781379
static const struct vfio_device_ops hisi_acc_vfio_pci_ops = {
@@ -1391,6 +1392,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_ops = {
13911392
.bind_iommufd = vfio_iommufd_physical_bind,
13921393
.unbind_iommufd = vfio_iommufd_physical_unbind,
13931394
.attach_ioas = vfio_iommufd_physical_attach_ioas,
1395+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
13941396
};
13951397

13961398
static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)

drivers/vfio/pci/mlx5/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,7 @@ static const struct vfio_device_ops mlx5vf_pci_ops = {
13201320
.bind_iommufd = vfio_iommufd_physical_bind,
13211321
.unbind_iommufd = vfio_iommufd_physical_unbind,
13221322
.attach_ioas = vfio_iommufd_physical_attach_ioas,
1323+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
13231324
};
13241325

13251326
static int mlx5vf_pci_probe(struct pci_dev *pdev,

drivers/vfio/pci/vfio_pci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ static const struct vfio_device_ops vfio_pci_ops = {
141141
.bind_iommufd = vfio_iommufd_physical_bind,
142142
.unbind_iommufd = vfio_iommufd_physical_unbind,
143143
.attach_ioas = vfio_iommufd_physical_attach_ioas,
144+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
144145
};
145146

146147
static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)

drivers/vfio/platform/vfio_amba.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static const struct vfio_device_ops vfio_amba_ops = {
119119
.bind_iommufd = vfio_iommufd_physical_bind,
120120
.unbind_iommufd = vfio_iommufd_physical_unbind,
121121
.attach_ioas = vfio_iommufd_physical_attach_ioas,
122+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
122123
};
123124

124125
static const struct amba_id pl330_ids[] = {

drivers/vfio/platform/vfio_platform.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static const struct vfio_device_ops vfio_platform_ops = {
108108
.bind_iommufd = vfio_iommufd_physical_bind,
109109
.unbind_iommufd = vfio_iommufd_physical_unbind,
110110
.attach_ioas = vfio_iommufd_physical_attach_ioas,
111+
.detach_ioas = vfio_iommufd_physical_detach_ioas,
111112
};
112113

113114
static struct platform_driver vfio_platform_driver = {

drivers/vfio/vfio_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ static int __vfio_register_dev(struct vfio_device *device,
273273
if (WARN_ON(IS_ENABLED(CONFIG_IOMMUFD) &&
274274
(!device->ops->bind_iommufd ||
275275
!device->ops->unbind_iommufd ||
276-
!device->ops->attach_ioas)))
276+
!device->ops->attach_ioas ||
277+
!device->ops->detach_ioas)))
277278
return -EINVAL;
278279

279280
/*

include/linux/vfio.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ struct vfio_device {
7373
* @bind_iommufd: Called when binding the device to an iommufd
7474
* @unbind_iommufd: Opposite of bind_iommufd
7575
* @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the
76-
* bound iommufd. Undo in unbind_iommufd.
76+
* bound iommufd. Undo in unbind_iommufd if @detach_ioas is not
77+
* called.
78+
* @detach_ioas: Opposite of attach_ioas
7779
* @open_device: Called when the first file descriptor is opened for this device
7880
* @close_device: Opposite of open_device
7981
* @read: Perform read(2) on device file descriptor
@@ -97,6 +99,7 @@ struct vfio_device_ops {
9799
struct iommufd_ctx *ictx, u32 *out_device_id);
98100
void (*unbind_iommufd)(struct vfio_device *vdev);
99101
int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
102+
void (*detach_ioas)(struct vfio_device *vdev);
100103
int (*open_device)(struct vfio_device *vdev);
101104
void (*close_device)(struct vfio_device *vdev);
102105
ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
@@ -120,6 +123,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev,
120123
struct iommufd_ctx *ictx, u32 *out_device_id);
121124
void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
122125
int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
126+
void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev);
123127
int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
124128
struct iommufd_ctx *ictx, u32 *out_device_id);
125129
void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
@@ -144,6 +148,8 @@ vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
144148
((void (*)(struct vfio_device *vdev)) NULL)
145149
#define vfio_iommufd_physical_attach_ioas \
146150
((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
151+
#define vfio_iommufd_physical_detach_ioas \
152+
((void (*)(struct vfio_device *vdev)) NULL)
147153
#define vfio_iommufd_emulated_bind \
148154
((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \
149155
u32 *out_device_id)) NULL)

0 commit comments

Comments
 (0)