Skip to content

Commit 2cd8b14

Browse files
yishaihAlex Williamson
authored andcommitted
vfio/pci: Move to the device set infrastructure
PCI wants to have the usual open/close_device() logic with the slight twist that the open/close_device() must be done under a singelton lock shared by all of the vfio_devices that are in the PCI "reset group". The reset group, and thus the device set, is determined by what devices pci_reset_bus() touches, which is either the entire bus or only the slot. Rely on the core code to do everything reflck was doing and delete reflck entirely. Signed-off-by: Yishai Hadas <yishaih@nvidia.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Cornelia Huck <cohuck@redhat.com> Link: https://lore.kernel.org/r/8-v4-9ea22c5e6afb+1adf-vfio_reflck_jgg@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent ab7e5e3 commit 2cd8b14

2 files changed

Lines changed: 37 additions & 132 deletions

File tree

drivers/vfio/pci/vfio_pci.c

Lines changed: 37 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -530,53 +530,40 @@ static void vfio_pci_vf_token_user_add(struct vfio_pci_device *vdev, int val)
530530
vfio_device_put(&pf_vdev->vdev);
531531
}
532532

533-
static void vfio_pci_release(struct vfio_device *core_vdev)
533+
static void vfio_pci_close_device(struct vfio_device *core_vdev)
534534
{
535535
struct vfio_pci_device *vdev =
536536
container_of(core_vdev, struct vfio_pci_device, vdev);
537537

538-
mutex_lock(&vdev->reflck->lock);
539-
540-
if (!(--vdev->refcnt)) {
541-
vfio_pci_vf_token_user_add(vdev, -1);
542-
vfio_spapr_pci_eeh_release(vdev->pdev);
543-
vfio_pci_disable(vdev);
538+
vfio_pci_vf_token_user_add(vdev, -1);
539+
vfio_spapr_pci_eeh_release(vdev->pdev);
540+
vfio_pci_disable(vdev);
544541

545-
mutex_lock(&vdev->igate);
546-
if (vdev->err_trigger) {
547-
eventfd_ctx_put(vdev->err_trigger);
548-
vdev->err_trigger = NULL;
549-
}
550-
if (vdev->req_trigger) {
551-
eventfd_ctx_put(vdev->req_trigger);
552-
vdev->req_trigger = NULL;
553-
}
554-
mutex_unlock(&vdev->igate);
542+
mutex_lock(&vdev->igate);
543+
if (vdev->err_trigger) {
544+
eventfd_ctx_put(vdev->err_trigger);
545+
vdev->err_trigger = NULL;
555546
}
556-
557-
mutex_unlock(&vdev->reflck->lock);
547+
if (vdev->req_trigger) {
548+
eventfd_ctx_put(vdev->req_trigger);
549+
vdev->req_trigger = NULL;
550+
}
551+
mutex_unlock(&vdev->igate);
558552
}
559553

560-
static int vfio_pci_open(struct vfio_device *core_vdev)
554+
static int vfio_pci_open_device(struct vfio_device *core_vdev)
561555
{
562556
struct vfio_pci_device *vdev =
563557
container_of(core_vdev, struct vfio_pci_device, vdev);
564558
int ret = 0;
565559

566-
mutex_lock(&vdev->reflck->lock);
567-
568-
if (!vdev->refcnt) {
569-
ret = vfio_pci_enable(vdev);
570-
if (ret)
571-
goto error;
560+
ret = vfio_pci_enable(vdev);
561+
if (ret)
562+
return ret;
572563

573-
vfio_spapr_pci_eeh_open(vdev->pdev);
574-
vfio_pci_vf_token_user_add(vdev, 1);
575-
}
576-
vdev->refcnt++;
577-
error:
578-
mutex_unlock(&vdev->reflck->lock);
579-
return ret;
564+
vfio_spapr_pci_eeh_open(vdev->pdev);
565+
vfio_pci_vf_token_user_add(vdev, 1);
566+
return 0;
580567
}
581568

582569
static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
@@ -1870,8 +1857,8 @@ static int vfio_pci_match(struct vfio_device *core_vdev, char *buf)
18701857

18711858
static const struct vfio_device_ops vfio_pci_ops = {
18721859
.name = "vfio-pci",
1873-
.open = vfio_pci_open,
1874-
.release = vfio_pci_release,
1860+
.open_device = vfio_pci_open_device,
1861+
.close_device = vfio_pci_close_device,
18751862
.ioctl = vfio_pci_ioctl,
18761863
.read = vfio_pci_read,
18771864
.write = vfio_pci_write,
@@ -1880,9 +1867,6 @@ static const struct vfio_device_ops vfio_pci_ops = {
18801867
.match = vfio_pci_match,
18811868
};
18821869

1883-
static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev);
1884-
static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck);
1885-
18861870
static int vfio_pci_bus_notifier(struct notifier_block *nb,
18871871
unsigned long action, void *data)
18881872
{
@@ -2020,12 +2004,23 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
20202004
INIT_LIST_HEAD(&vdev->vma_list);
20212005
init_rwsem(&vdev->memory_lock);
20222006

2023-
ret = vfio_pci_reflck_attach(vdev);
2007+
if (pci_is_root_bus(pdev->bus)) {
2008+
ret = vfio_assign_device_set(&vdev->vdev, vdev);
2009+
} else if (!pci_probe_reset_slot(pdev->slot)) {
2010+
ret = vfio_assign_device_set(&vdev->vdev, pdev->slot);
2011+
} else {
2012+
/*
2013+
* If there is no slot reset support for this device, the whole
2014+
* bus needs to be grouped together to support bus-wide resets.
2015+
*/
2016+
ret = vfio_assign_device_set(&vdev->vdev, pdev->bus);
2017+
}
2018+
20242019
if (ret)
20252020
goto out_uninit;
20262021
ret = vfio_pci_vf_init(vdev);
20272022
if (ret)
2028-
goto out_reflck;
2023+
goto out_uninit;
20292024
ret = vfio_pci_vga_init(vdev);
20302025
if (ret)
20312026
goto out_vf;
@@ -2057,8 +2052,6 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
20572052
vfio_pci_set_power_state(vdev, PCI_D0);
20582053
out_vf:
20592054
vfio_pci_vf_uninit(vdev);
2060-
out_reflck:
2061-
vfio_pci_reflck_put(vdev->reflck);
20622055
out_uninit:
20632056
vfio_uninit_group_dev(&vdev->vdev);
20642057
kfree(vdev->pm_save);
@@ -2077,7 +2070,6 @@ static void vfio_pci_remove(struct pci_dev *pdev)
20772070
vfio_unregister_group_dev(&vdev->vdev);
20782071

20792072
vfio_pci_vf_uninit(vdev);
2080-
vfio_pci_reflck_put(vdev->reflck);
20812073
vfio_uninit_group_dev(&vdev->vdev);
20822074
vfio_pci_vga_uninit(vdev);
20832075

@@ -2153,86 +2145,6 @@ static struct pci_driver vfio_pci_driver = {
21532145
.err_handler = &vfio_err_handlers,
21542146
};
21552147

2156-
static DEFINE_MUTEX(reflck_lock);
2157-
2158-
static struct vfio_pci_reflck *vfio_pci_reflck_alloc(void)
2159-
{
2160-
struct vfio_pci_reflck *reflck;
2161-
2162-
reflck = kzalloc(sizeof(*reflck), GFP_KERNEL);
2163-
if (!reflck)
2164-
return ERR_PTR(-ENOMEM);
2165-
2166-
kref_init(&reflck->kref);
2167-
mutex_init(&reflck->lock);
2168-
2169-
return reflck;
2170-
}
2171-
2172-
static void vfio_pci_reflck_get(struct vfio_pci_reflck *reflck)
2173-
{
2174-
kref_get(&reflck->kref);
2175-
}
2176-
2177-
static int vfio_pci_reflck_find(struct pci_dev *pdev, void *data)
2178-
{
2179-
struct vfio_pci_reflck **preflck = data;
2180-
struct vfio_device *device;
2181-
struct vfio_pci_device *vdev;
2182-
2183-
device = vfio_device_get_from_dev(&pdev->dev);
2184-
if (!device)
2185-
return 0;
2186-
2187-
if (pci_dev_driver(pdev) != &vfio_pci_driver) {
2188-
vfio_device_put(device);
2189-
return 0;
2190-
}
2191-
2192-
vdev = container_of(device, struct vfio_pci_device, vdev);
2193-
2194-
if (vdev->reflck) {
2195-
vfio_pci_reflck_get(vdev->reflck);
2196-
*preflck = vdev->reflck;
2197-
vfio_device_put(device);
2198-
return 1;
2199-
}
2200-
2201-
vfio_device_put(device);
2202-
return 0;
2203-
}
2204-
2205-
static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev)
2206-
{
2207-
bool slot = !pci_probe_reset_slot(vdev->pdev->slot);
2208-
2209-
mutex_lock(&reflck_lock);
2210-
2211-
if (pci_is_root_bus(vdev->pdev->bus) ||
2212-
vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_reflck_find,
2213-
&vdev->reflck, slot) <= 0)
2214-
vdev->reflck = vfio_pci_reflck_alloc();
2215-
2216-
mutex_unlock(&reflck_lock);
2217-
2218-
return PTR_ERR_OR_ZERO(vdev->reflck);
2219-
}
2220-
2221-
static void vfio_pci_reflck_release(struct kref *kref)
2222-
{
2223-
struct vfio_pci_reflck *reflck = container_of(kref,
2224-
struct vfio_pci_reflck,
2225-
kref);
2226-
2227-
kfree(reflck);
2228-
mutex_unlock(&reflck_lock);
2229-
}
2230-
2231-
static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck)
2232-
{
2233-
kref_put_mutex(&reflck->kref, vfio_pci_reflck_release, &reflck_lock);
2234-
}
2235-
22362148
static int vfio_pci_get_unused_devs(struct pci_dev *pdev, void *data)
22372149
{
22382150
struct vfio_devices *devs = data;
@@ -2254,7 +2166,7 @@ static int vfio_pci_get_unused_devs(struct pci_dev *pdev, void *data)
22542166
vdev = container_of(device, struct vfio_pci_device, vdev);
22552167

22562168
/* Fault if the device is not unused */
2257-
if (vdev->refcnt) {
2169+
if (device->open_count) {
22582170
vfio_device_put(device);
22592171
return -EBUSY;
22602172
}
@@ -2303,7 +2215,7 @@ static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data)
23032215
* - At least one of the affected devices is marked dirty via
23042216
* needs_reset (such as by lack of FLR support)
23052217
* Then attempt to perform that bus or slot reset. Callers are required
2306-
* to hold vdev->reflck->lock, protecting the bus/slot reset group from
2218+
* to hold vdev->dev_set->lock, protecting the bus/slot reset group from
23072219
* concurrent opens. A vfio_device reference is acquired for each device
23082220
* to prevent unbinds during the reset operation.
23092221
*

drivers/vfio/pci/vfio_pci_private.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ struct vfio_pci_dummy_resource {
8383
struct list_head res_next;
8484
};
8585

86-
struct vfio_pci_reflck {
87-
struct kref kref;
88-
struct mutex lock;
89-
};
90-
9186
struct vfio_pci_vf_token {
9287
struct mutex lock;
9388
uuid_t uuid;
@@ -130,8 +125,6 @@ struct vfio_pci_device {
130125
bool needs_pm_restore;
131126
struct pci_saved_state *pci_saved_state;
132127
struct pci_saved_state *pm_save;
133-
struct vfio_pci_reflck *reflck;
134-
int refcnt;
135128
int ioeventfds_nr;
136129
struct eventfd_ctx *err_trigger;
137130
struct eventfd_ctx *req_trigger;

0 commit comments

Comments
 (0)