1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
4 * Author: Alex Williamson <alex.williamson@redhat.com>
5 *
6 * Derived from original vfio:
7 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
8 * Author: Tom Lyon, pugs@cisco.com
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/device.h>
14 #include <linux/eventfd.h>
15 #include <linux/file.h>
16 #include <linux/interrupt.h>
17 #include <linux/iommu.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/notifier.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/uaccess.h>
26 #include <linux/vgaarb.h>
27 #include <linux/nospec.h>
28 #include <linux/sched/mm.h>
29
30 #include <linux/vfio_pci_core.h>
31
32 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
33 #define DRIVER_DESC "core driver for VFIO based PCI devices"
34
35 static bool nointxmask;
36 static bool disable_vga;
37 static bool disable_idle_d3;
38
39 /* List of PF's that vfio_pci_core_sriov_configure() has been called on */
40 static DEFINE_MUTEX(vfio_pci_sriov_pfs_mutex);
41 static LIST_HEAD(vfio_pci_sriov_pfs);
42
vfio_vga_disabled(void)43 static inline bool vfio_vga_disabled(void)
44 {
45 #ifdef CONFIG_VFIO_PCI_VGA
46 return disable_vga;
47 #else
48 return true;
49 #endif
50 }
51
52 /*
53 * Our VGA arbiter participation is limited since we don't know anything
54 * about the device itself. However, if the device is the only VGA device
55 * downstream of a bridge and VFIO VGA support is disabled, then we can
56 * safely return legacy VGA IO and memory as not decoded since the user
57 * has no way to get to it and routing can be disabled externally at the
58 * bridge.
59 */
vfio_pci_set_decode(struct pci_dev * pdev,bool single_vga)60 static unsigned int vfio_pci_set_decode(struct pci_dev *pdev, bool single_vga)
61 {
62 struct pci_dev *tmp = NULL;
63 unsigned char max_busnr;
64 unsigned int decodes;
65
66 if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
67 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
68 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
69
70 max_busnr = pci_bus_max_busnr(pdev->bus);
71 decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
72
73 while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
74 if (tmp == pdev ||
75 pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
76 pci_is_root_bus(tmp->bus))
77 continue;
78
79 if (tmp->bus->number >= pdev->bus->number &&
80 tmp->bus->number <= max_busnr) {
81 pci_dev_put(tmp);
82 decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
83 break;
84 }
85 }
86
87 return decodes;
88 }
89
vfio_pci_probe_mmaps(struct vfio_pci_core_device * vdev)90 static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev)
91 {
92 struct resource *res;
93 int i;
94 struct vfio_pci_dummy_resource *dummy_res;
95
96 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
97 int bar = i + PCI_STD_RESOURCES;
98
99 res = &vdev->pdev->resource[bar];
100
101 if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
102 goto no_mmap;
103
104 if (!(res->flags & IORESOURCE_MEM))
105 goto no_mmap;
106
107 /*
108 * The PCI core shouldn't set up a resource with a
109 * type but zero size. But there may be bugs that
110 * cause us to do that.
111 */
112 if (!resource_size(res))
113 goto no_mmap;
114
115 if (resource_size(res) >= PAGE_SIZE) {
116 vdev->bar_mmap_supported[bar] = true;
117 continue;
118 }
119
120 if (!(res->start & ~PAGE_MASK)) {
121 /*
122 * Add a dummy resource to reserve the remainder
123 * of the exclusive page in case that hot-add
124 * device's bar is assigned into it.
125 */
126 dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
127 if (dummy_res == NULL)
128 goto no_mmap;
129
130 dummy_res->resource.name = "vfio sub-page reserved";
131 dummy_res->resource.start = res->end + 1;
132 dummy_res->resource.end = res->start + PAGE_SIZE - 1;
133 dummy_res->resource.flags = res->flags;
134 if (request_resource(res->parent,
135 &dummy_res->resource)) {
136 kfree(dummy_res);
137 goto no_mmap;
138 }
139 dummy_res->index = bar;
140 list_add(&dummy_res->res_next,
141 &vdev->dummy_resources_list);
142 vdev->bar_mmap_supported[bar] = true;
143 continue;
144 }
145 /*
146 * Here we don't handle the case when the BAR is not page
147 * aligned because we can't expect the BAR will be
148 * assigned into the same location in a page in guest
149 * when we passthrough the BAR. And it's hard to access
150 * this BAR in userspace because we have no way to get
151 * the BAR's location in a page.
152 */
153 no_mmap:
154 vdev->bar_mmap_supported[bar] = false;
155 }
156 }
157
158 struct vfio_pci_group_info;
159 static bool vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set);
160 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,
161 struct vfio_pci_group_info *groups);
162
163 /*
164 * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
165 * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
166 * If a device implements the former but not the latter we would typically
167 * expect broken_intx_masking be set and require an exclusive interrupt.
168 * However since we do have control of the device's ability to assert INTx,
169 * we can instead pretend that the device does not implement INTx, virtualizing
170 * the pin register to report zero and maintaining DisINTx set on the host.
171 */
vfio_pci_nointx(struct pci_dev * pdev)172 static bool vfio_pci_nointx(struct pci_dev *pdev)
173 {
174 switch (pdev->vendor) {
175 case PCI_VENDOR_ID_INTEL:
176 switch (pdev->device) {
177 /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */
178 case 0x1572:
179 case 0x1574:
180 case 0x1580 ... 0x1581:
181 case 0x1583 ... 0x158b:
182 case 0x37d0 ... 0x37d2:
183 /* X550 */
184 case 0x1563:
185 return true;
186 default:
187 return false;
188 }
189 }
190
191 return false;
192 }
193
vfio_pci_probe_power_state(struct vfio_pci_core_device * vdev)194 static void vfio_pci_probe_power_state(struct vfio_pci_core_device *vdev)
195 {
196 struct pci_dev *pdev = vdev->pdev;
197 u16 pmcsr;
198
199 if (!pdev->pm_cap)
200 return;
201
202 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &pmcsr);
203
204 vdev->needs_pm_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET);
205 }
206
207 /*
208 * pci_set_power_state() wrapper handling devices which perform a soft reset on
209 * D3->D0 transition. Save state prior to D0/1/2->D3, stash it on the vdev,
210 * restore when returned to D0. Saved separately from pci_saved_state for use
211 * by PM capability emulation and separately from pci_dev internal saved state
212 * to avoid it being overwritten and consumed around other resets.
213 */
vfio_pci_set_power_state(struct vfio_pci_core_device * vdev,pci_power_t state)214 int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state)
215 {
216 struct pci_dev *pdev = vdev->pdev;
217 bool needs_restore = false, needs_save = false;
218 int ret;
219
220 if (vdev->needs_pm_restore) {
221 if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) {
222 pci_save_state(pdev);
223 needs_save = true;
224 }
225
226 if (pdev->current_state >= PCI_D3hot && state <= PCI_D0)
227 needs_restore = true;
228 }
229
230 ret = pci_set_power_state(pdev, state);
231
232 if (!ret) {
233 /* D3 might be unsupported via quirk, skip unless in D3 */
234 if (needs_save && pdev->current_state >= PCI_D3hot) {
235 /*
236 * The current PCI state will be saved locally in
237 * 'pm_save' during the D3hot transition. When the
238 * device state is changed to D0 again with the current
239 * function, then pci_store_saved_state() will restore
240 * the state and will free the memory pointed by
241 * 'pm_save'. There are few cases where the PCI power
242 * state can be changed to D0 without the involvement
243 * of the driver. For these cases, free the earlier
244 * allocated memory first before overwriting 'pm_save'
245 * to prevent the memory leak.
246 */
247 kfree(vdev->pm_save);
248 vdev->pm_save = pci_store_saved_state(pdev);
249 } else if (needs_restore) {
250 pci_load_and_free_saved_state(pdev, &vdev->pm_save);
251 pci_restore_state(pdev);
252 }
253 }
254
255 return ret;
256 }
257
vfio_pci_core_enable(struct vfio_pci_core_device * vdev)258 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
259 {
260 struct pci_dev *pdev = vdev->pdev;
261 int ret;
262 u16 cmd;
263 u8 msix_pos;
264
265 vfio_pci_set_power_state(vdev, PCI_D0);
266
267 /* Don't allow our initial saved state to include busmaster */
268 pci_clear_master(pdev);
269
270 ret = pci_enable_device(pdev);
271 if (ret)
272 return ret;
273
274 /* If reset fails because of the device lock, fail this path entirely */
275 ret = pci_try_reset_function(pdev);
276 if (ret == -EAGAIN) {
277 pci_disable_device(pdev);
278 return ret;
279 }
280
281 vdev->reset_works = !ret;
282 pci_save_state(pdev);
283 vdev->pci_saved_state = pci_store_saved_state(pdev);
284 if (!vdev->pci_saved_state)
285 pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__);
286
287 if (likely(!nointxmask)) {
288 if (vfio_pci_nointx(pdev)) {
289 pci_info(pdev, "Masking broken INTx support\n");
290 vdev->nointx = true;
291 pci_intx(pdev, 0);
292 } else
293 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
294 }
295
296 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
297 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
298 cmd &= ~PCI_COMMAND_INTX_DISABLE;
299 pci_write_config_word(pdev, PCI_COMMAND, cmd);
300 }
301
302 ret = vfio_config_init(vdev);
303 if (ret) {
304 kfree(vdev->pci_saved_state);
305 vdev->pci_saved_state = NULL;
306 pci_disable_device(pdev);
307 return ret;
308 }
309
310 msix_pos = pdev->msix_cap;
311 if (msix_pos) {
312 u16 flags;
313 u32 table;
314
315 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
316 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
317
318 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
319 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
320 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
321 } else
322 vdev->msix_bar = 0xFF;
323
324 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
325 vdev->has_vga = true;
326
327
328 return 0;
329 }
330 EXPORT_SYMBOL_GPL(vfio_pci_core_enable);
331
vfio_pci_core_disable(struct vfio_pci_core_device * vdev)332 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
333 {
334 struct pci_dev *pdev = vdev->pdev;
335 struct vfio_pci_dummy_resource *dummy_res, *tmp;
336 struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp;
337 int i, bar;
338
339 /* For needs_reset */
340 lockdep_assert_held(&vdev->vdev.dev_set->lock);
341
342 /*
343 * This function can be invoked while the power state is non-D0.
344 * This function calls __pci_reset_function_locked() which internally
345 * can use pci_pm_reset() for the function reset. pci_pm_reset() will
346 * fail if the power state is non-D0. Also, for the devices which
347 * have NoSoftRst-, the reset function can cause the PCI config space
348 * reset without restoring the original state (saved locally in
349 * 'vdev->pm_save').
350 */
351 vfio_pci_set_power_state(vdev, PCI_D0);
352
353 /* Stop the device from further DMA */
354 pci_clear_master(pdev);
355
356 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
357 VFIO_IRQ_SET_ACTION_TRIGGER,
358 vdev->irq_type, 0, 0, NULL);
359
360 /* Device closed, don't need mutex here */
361 list_for_each_entry_safe(ioeventfd, ioeventfd_tmp,
362 &vdev->ioeventfds_list, next) {
363 vfio_virqfd_disable(&ioeventfd->virqfd);
364 list_del(&ioeventfd->next);
365 kfree(ioeventfd);
366 }
367 vdev->ioeventfds_nr = 0;
368
369 vdev->virq_disabled = false;
370
371 for (i = 0; i < vdev->num_regions; i++)
372 vdev->region[i].ops->release(vdev, &vdev->region[i]);
373
374 vdev->num_regions = 0;
375 kfree(vdev->region);
376 vdev->region = NULL; /* don't krealloc a freed pointer */
377
378 vfio_config_free(vdev);
379
380 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
381 bar = i + PCI_STD_RESOURCES;
382 if (!vdev->barmap[bar])
383 continue;
384 pci_iounmap(pdev, vdev->barmap[bar]);
385 pci_release_selected_regions(pdev, 1 << bar);
386 vdev->barmap[bar] = NULL;
387 }
388
389 list_for_each_entry_safe(dummy_res, tmp,
390 &vdev->dummy_resources_list, res_next) {
391 list_del(&dummy_res->res_next);
392 release_resource(&dummy_res->resource);
393 kfree(dummy_res);
394 }
395
396 vdev->needs_reset = true;
397
398 /*
399 * If we have saved state, restore it. If we can reset the device,
400 * even better. Resetting with current state seems better than
401 * nothing, but saving and restoring current state without reset
402 * is just busy work.
403 */
404 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
405 pci_info(pdev, "%s: Couldn't reload saved state\n", __func__);
406
407 if (!vdev->reset_works)
408 goto out;
409
410 pci_save_state(pdev);
411 }
412
413 /*
414 * Disable INTx and MSI, presumably to avoid spurious interrupts
415 * during reset. Stolen from pci_reset_function()
416 */
417 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
418
419 /*
420 * Try to get the locks ourselves to prevent a deadlock. The
421 * success of this is dependent on being able to lock the device,
422 * which is not always possible.
423 * We can not use the "try" reset interface here, which will
424 * overwrite the previously restored configuration information.
425 */
426 if (vdev->reset_works && pci_dev_trylock(pdev)) {
427 if (!__pci_reset_function_locked(pdev))
428 vdev->needs_reset = false;
429 pci_dev_unlock(pdev);
430 }
431
432 pci_restore_state(pdev);
433 out:
434 pci_disable_device(pdev);
435
436 if (!vfio_pci_dev_set_try_reset(vdev->vdev.dev_set) && !disable_idle_d3)
437 vfio_pci_set_power_state(vdev, PCI_D3hot);
438 }
439 EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
440
vfio_pci_core_close_device(struct vfio_device * core_vdev)441 void vfio_pci_core_close_device(struct vfio_device *core_vdev)
442 {
443 struct vfio_pci_core_device *vdev =
444 container_of(core_vdev, struct vfio_pci_core_device, vdev);
445
446 if (vdev->sriov_pf_core_dev) {
447 mutex_lock(&vdev->sriov_pf_core_dev->vf_token->lock);
448 WARN_ON(!vdev->sriov_pf_core_dev->vf_token->users);
449 vdev->sriov_pf_core_dev->vf_token->users--;
450 mutex_unlock(&vdev->sriov_pf_core_dev->vf_token->lock);
451 }
452 vfio_spapr_pci_eeh_release(vdev->pdev);
453 vfio_pci_core_disable(vdev);
454
455 mutex_lock(&vdev->igate);
456 if (vdev->err_trigger) {
457 eventfd_ctx_put(vdev->err_trigger);
458 vdev->err_trigger = NULL;
459 }
460 if (vdev->req_trigger) {
461 eventfd_ctx_put(vdev->req_trigger);
462 vdev->req_trigger = NULL;
463 }
464 mutex_unlock(&vdev->igate);
465 }
466 EXPORT_SYMBOL_GPL(vfio_pci_core_close_device);
467
vfio_pci_core_finish_enable(struct vfio_pci_core_device * vdev)468 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
469 {
470 vfio_pci_probe_mmaps(vdev);
471 vfio_spapr_pci_eeh_open(vdev->pdev);
472
473 if (vdev->sriov_pf_core_dev) {
474 mutex_lock(&vdev->sriov_pf_core_dev->vf_token->lock);
475 vdev->sriov_pf_core_dev->vf_token->users++;
476 mutex_unlock(&vdev->sriov_pf_core_dev->vf_token->lock);
477 }
478 }
479 EXPORT_SYMBOL_GPL(vfio_pci_core_finish_enable);
480
vfio_pci_get_irq_count(struct vfio_pci_core_device * vdev,int irq_type)481 static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_type)
482 {
483 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
484 u8 pin;
485
486 if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) ||
487 vdev->nointx || vdev->pdev->is_virtfn)
488 return 0;
489
490 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
491
492 return pin ? 1 : 0;
493 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
494 u8 pos;
495 u16 flags;
496
497 pos = vdev->pdev->msi_cap;
498 if (pos) {
499 pci_read_config_word(vdev->pdev,
500 pos + PCI_MSI_FLAGS, &flags);
501 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
502 }
503 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
504 u8 pos;
505 u16 flags;
506
507 pos = vdev->pdev->msix_cap;
508 if (pos) {
509 pci_read_config_word(vdev->pdev,
510 pos + PCI_MSIX_FLAGS, &flags);
511
512 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
513 }
514 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
515 if (pci_is_pcie(vdev->pdev))
516 return 1;
517 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
518 return 1;
519 }
520
521 return 0;
522 }
523
vfio_pci_count_devs(struct pci_dev * pdev,void * data)524 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
525 {
526 (*(int *)data)++;
527 return 0;
528 }
529
530 struct vfio_pci_fill_info {
531 int max;
532 int cur;
533 struct vfio_pci_dependent_device *devices;
534 };
535
vfio_pci_fill_devs(struct pci_dev * pdev,void * data)536 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
537 {
538 struct vfio_pci_fill_info *fill = data;
539 struct iommu_group *iommu_group;
540
541 if (fill->cur == fill->max)
542 return -EAGAIN; /* Something changed, try again */
543
544 iommu_group = iommu_group_get(&pdev->dev);
545 if (!iommu_group)
546 return -EPERM; /* Cannot reset non-isolated devices */
547
548 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
549 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
550 fill->devices[fill->cur].bus = pdev->bus->number;
551 fill->devices[fill->cur].devfn = pdev->devfn;
552 fill->cur++;
553 iommu_group_put(iommu_group);
554 return 0;
555 }
556
557 struct vfio_pci_group_info {
558 int count;
559 struct vfio_group **groups;
560 };
561
vfio_pci_dev_below_slot(struct pci_dev * pdev,struct pci_slot * slot)562 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
563 {
564 for (; pdev; pdev = pdev->bus->self)
565 if (pdev->bus == slot->bus)
566 return (pdev->slot == slot);
567 return false;
568 }
569
570 struct vfio_pci_walk_info {
571 int (*fn)(struct pci_dev *pdev, void *data);
572 void *data;
573 struct pci_dev *pdev;
574 bool slot;
575 int ret;
576 };
577
vfio_pci_walk_wrapper(struct pci_dev * pdev,void * data)578 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
579 {
580 struct vfio_pci_walk_info *walk = data;
581
582 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
583 walk->ret = walk->fn(pdev, walk->data);
584
585 return walk->ret;
586 }
587
vfio_pci_for_each_slot_or_bus(struct pci_dev * pdev,int (* fn)(struct pci_dev *,void * data),void * data,bool slot)588 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
589 int (*fn)(struct pci_dev *,
590 void *data), void *data,
591 bool slot)
592 {
593 struct vfio_pci_walk_info walk = {
594 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
595 };
596
597 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
598
599 return walk.ret;
600 }
601
msix_mmappable_cap(struct vfio_pci_core_device * vdev,struct vfio_info_cap * caps)602 static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
603 struct vfio_info_cap *caps)
604 {
605 struct vfio_info_cap_header header = {
606 .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE,
607 .version = 1
608 };
609
610 return vfio_info_add_capability(caps, &header, sizeof(header));
611 }
612
vfio_pci_register_dev_region(struct vfio_pci_core_device * vdev,unsigned int type,unsigned int subtype,const struct vfio_pci_regops * ops,size_t size,u32 flags,void * data)613 int vfio_pci_register_dev_region(struct vfio_pci_core_device *vdev,
614 unsigned int type, unsigned int subtype,
615 const struct vfio_pci_regops *ops,
616 size_t size, u32 flags, void *data)
617 {
618 struct vfio_pci_region *region;
619
620 region = krealloc(vdev->region,
621 (vdev->num_regions + 1) * sizeof(*region),
622 GFP_KERNEL);
623 if (!region)
624 return -ENOMEM;
625
626 vdev->region = region;
627 vdev->region[vdev->num_regions].type = type;
628 vdev->region[vdev->num_regions].subtype = subtype;
629 vdev->region[vdev->num_regions].ops = ops;
630 vdev->region[vdev->num_regions].size = size;
631 vdev->region[vdev->num_regions].flags = flags;
632 vdev->region[vdev->num_regions].data = data;
633
634 vdev->num_regions++;
635
636 return 0;
637 }
638 EXPORT_SYMBOL_GPL(vfio_pci_register_dev_region);
639
vfio_pci_core_ioctl(struct vfio_device * core_vdev,unsigned int cmd,unsigned long arg)640 long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
641 unsigned long arg)
642 {
643 struct vfio_pci_core_device *vdev =
644 container_of(core_vdev, struct vfio_pci_core_device, vdev);
645 unsigned long minsz;
646
647 if (cmd == VFIO_DEVICE_GET_INFO) {
648 struct vfio_device_info info;
649 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
650 unsigned long capsz;
651 int ret;
652
653 minsz = offsetofend(struct vfio_device_info, num_irqs);
654
655 /* For backward compatibility, cannot require this */
656 capsz = offsetofend(struct vfio_iommu_type1_info, cap_offset);
657
658 if (copy_from_user(&info, (void __user *)arg, minsz))
659 return -EFAULT;
660
661 if (info.argsz < minsz)
662 return -EINVAL;
663
664 if (info.argsz >= capsz) {
665 minsz = capsz;
666 info.cap_offset = 0;
667 }
668
669 info.flags = VFIO_DEVICE_FLAGS_PCI;
670
671 if (vdev->reset_works)
672 info.flags |= VFIO_DEVICE_FLAGS_RESET;
673
674 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
675 info.num_irqs = VFIO_PCI_NUM_IRQS;
676
677 ret = vfio_pci_info_zdev_add_caps(vdev, &caps);
678 if (ret && ret != -ENODEV) {
679 pci_warn(vdev->pdev, "Failed to setup zPCI info capabilities\n");
680 return ret;
681 }
682
683 if (caps.size) {
684 info.flags |= VFIO_DEVICE_FLAGS_CAPS;
685 if (info.argsz < sizeof(info) + caps.size) {
686 info.argsz = sizeof(info) + caps.size;
687 } else {
688 vfio_info_cap_shift(&caps, sizeof(info));
689 if (copy_to_user((void __user *)arg +
690 sizeof(info), caps.buf,
691 caps.size)) {
692 kfree(caps.buf);
693 return -EFAULT;
694 }
695 info.cap_offset = sizeof(info);
696 }
697
698 kfree(caps.buf);
699 }
700
701 return copy_to_user((void __user *)arg, &info, minsz) ?
702 -EFAULT : 0;
703
704 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
705 struct pci_dev *pdev = vdev->pdev;
706 struct vfio_region_info info;
707 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
708 int i, ret;
709
710 minsz = offsetofend(struct vfio_region_info, offset);
711
712 if (copy_from_user(&info, (void __user *)arg, minsz))
713 return -EFAULT;
714
715 if (info.argsz < minsz)
716 return -EINVAL;
717
718 switch (info.index) {
719 case VFIO_PCI_CONFIG_REGION_INDEX:
720 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
721 info.size = pdev->cfg_size;
722 info.flags = VFIO_REGION_INFO_FLAG_READ |
723 VFIO_REGION_INFO_FLAG_WRITE;
724 break;
725 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
726 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
727 info.size = pci_resource_len(pdev, info.index);
728 if (!info.size) {
729 info.flags = 0;
730 break;
731 }
732
733 info.flags = VFIO_REGION_INFO_FLAG_READ |
734 VFIO_REGION_INFO_FLAG_WRITE;
735 if (vdev->bar_mmap_supported[info.index]) {
736 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
737 if (info.index == vdev->msix_bar) {
738 ret = msix_mmappable_cap(vdev, &caps);
739 if (ret)
740 return ret;
741 }
742 }
743
744 break;
745 case VFIO_PCI_ROM_REGION_INDEX:
746 {
747 void __iomem *io;
748 size_t size;
749 u16 cmd;
750
751 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
752 info.flags = 0;
753
754 /* Report the BAR size, not the ROM size */
755 info.size = pci_resource_len(pdev, info.index);
756 if (!info.size) {
757 /* Shadow ROMs appear as PCI option ROMs */
758 if (pdev->resource[PCI_ROM_RESOURCE].flags &
759 IORESOURCE_ROM_SHADOW)
760 info.size = 0x20000;
761 else
762 break;
763 }
764
765 /*
766 * Is it really there? Enable memory decode for
767 * implicit access in pci_map_rom().
768 */
769 cmd = vfio_pci_memory_lock_and_enable(vdev);
770 io = pci_map_rom(pdev, &size);
771 if (io) {
772 info.flags = VFIO_REGION_INFO_FLAG_READ;
773 pci_unmap_rom(pdev, io);
774 } else {
775 info.size = 0;
776 }
777 vfio_pci_memory_unlock_and_restore(vdev, cmd);
778
779 break;
780 }
781 case VFIO_PCI_VGA_REGION_INDEX:
782 if (!vdev->has_vga)
783 return -EINVAL;
784
785 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
786 info.size = 0xc0000;
787 info.flags = VFIO_REGION_INFO_FLAG_READ |
788 VFIO_REGION_INFO_FLAG_WRITE;
789
790 break;
791 default:
792 {
793 struct vfio_region_info_cap_type cap_type = {
794 .header.id = VFIO_REGION_INFO_CAP_TYPE,
795 .header.version = 1 };
796
797 if (info.index >=
798 VFIO_PCI_NUM_REGIONS + vdev->num_regions)
799 return -EINVAL;
800 info.index = array_index_nospec(info.index,
801 VFIO_PCI_NUM_REGIONS +
802 vdev->num_regions);
803
804 i = info.index - VFIO_PCI_NUM_REGIONS;
805
806 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
807 info.size = vdev->region[i].size;
808 info.flags = vdev->region[i].flags;
809
810 cap_type.type = vdev->region[i].type;
811 cap_type.subtype = vdev->region[i].subtype;
812
813 ret = vfio_info_add_capability(&caps, &cap_type.header,
814 sizeof(cap_type));
815 if (ret)
816 return ret;
817
818 if (vdev->region[i].ops->add_capability) {
819 ret = vdev->region[i].ops->add_capability(vdev,
820 &vdev->region[i], &caps);
821 if (ret)
822 return ret;
823 }
824 }
825 }
826
827 if (caps.size) {
828 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
829 if (info.argsz < sizeof(info) + caps.size) {
830 info.argsz = sizeof(info) + caps.size;
831 info.cap_offset = 0;
832 } else {
833 vfio_info_cap_shift(&caps, sizeof(info));
834 if (copy_to_user((void __user *)arg +
835 sizeof(info), caps.buf,
836 caps.size)) {
837 kfree(caps.buf);
838 return -EFAULT;
839 }
840 info.cap_offset = sizeof(info);
841 }
842
843 kfree(caps.buf);
844 }
845
846 return copy_to_user((void __user *)arg, &info, minsz) ?
847 -EFAULT : 0;
848
849 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
850 struct vfio_irq_info info;
851
852 minsz = offsetofend(struct vfio_irq_info, count);
853
854 if (copy_from_user(&info, (void __user *)arg, minsz))
855 return -EFAULT;
856
857 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
858 return -EINVAL;
859
860 switch (info.index) {
861 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
862 case VFIO_PCI_REQ_IRQ_INDEX:
863 break;
864 case VFIO_PCI_ERR_IRQ_INDEX:
865 if (pci_is_pcie(vdev->pdev))
866 break;
867 fallthrough;
868 default:
869 return -EINVAL;
870 }
871
872 info.flags = VFIO_IRQ_INFO_EVENTFD;
873
874 info.count = vfio_pci_get_irq_count(vdev, info.index);
875
876 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
877 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
878 VFIO_IRQ_INFO_AUTOMASKED);
879 else
880 info.flags |= VFIO_IRQ_INFO_NORESIZE;
881
882 return copy_to_user((void __user *)arg, &info, minsz) ?
883 -EFAULT : 0;
884
885 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
886 struct vfio_irq_set hdr;
887 u8 *data = NULL;
888 int max, ret = 0;
889 size_t data_size = 0;
890
891 minsz = offsetofend(struct vfio_irq_set, count);
892
893 if (copy_from_user(&hdr, (void __user *)arg, minsz))
894 return -EFAULT;
895
896 max = vfio_pci_get_irq_count(vdev, hdr.index);
897
898 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
899 VFIO_PCI_NUM_IRQS, &data_size);
900 if (ret)
901 return ret;
902
903 if (data_size) {
904 data = memdup_user((void __user *)(arg + minsz),
905 data_size);
906 if (IS_ERR(data))
907 return PTR_ERR(data);
908 }
909
910 mutex_lock(&vdev->igate);
911
912 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
913 hdr.start, hdr.count, data);
914
915 mutex_unlock(&vdev->igate);
916 kfree(data);
917
918 return ret;
919
920 } else if (cmd == VFIO_DEVICE_RESET) {
921 int ret;
922
923 if (!vdev->reset_works)
924 return -EINVAL;
925
926 vfio_pci_zap_and_down_write_memory_lock(vdev);
927
928 /*
929 * This function can be invoked while the power state is non-D0.
930 * If pci_try_reset_function() has been called while the power
931 * state is non-D0, then pci_try_reset_function() will
932 * internally set the power state to D0 without vfio driver
933 * involvement. For the devices which have NoSoftRst-, the
934 * reset function can cause the PCI config space reset without
935 * restoring the original state (saved locally in
936 * 'vdev->pm_save').
937 */
938 vfio_pci_set_power_state(vdev, PCI_D0);
939
940 ret = pci_try_reset_function(vdev->pdev);
941 up_write(&vdev->memory_lock);
942
943 return ret;
944
945 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
946 struct vfio_pci_hot_reset_info hdr;
947 struct vfio_pci_fill_info fill = { 0 };
948 struct vfio_pci_dependent_device *devices = NULL;
949 bool slot = false;
950 int ret = 0;
951
952 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
953
954 if (copy_from_user(&hdr, (void __user *)arg, minsz))
955 return -EFAULT;
956
957 if (hdr.argsz < minsz)
958 return -EINVAL;
959
960 hdr.flags = 0;
961
962 /* Can we do a slot or bus reset or neither? */
963 if (!pci_probe_reset_slot(vdev->pdev->slot))
964 slot = true;
965 else if (pci_probe_reset_bus(vdev->pdev->bus))
966 return -ENODEV;
967
968 /* How many devices are affected? */
969 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
970 vfio_pci_count_devs,
971 &fill.max, slot);
972 if (ret)
973 return ret;
974
975 WARN_ON(!fill.max); /* Should always be at least one */
976
977 /*
978 * If there's enough space, fill it now, otherwise return
979 * -ENOSPC and the number of devices affected.
980 */
981 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
982 ret = -ENOSPC;
983 hdr.count = fill.max;
984 goto reset_info_exit;
985 }
986
987 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
988 if (!devices)
989 return -ENOMEM;
990
991 fill.devices = devices;
992
993 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
994 vfio_pci_fill_devs,
995 &fill, slot);
996
997 /*
998 * If a device was removed between counting and filling,
999 * we may come up short of fill.max. If a device was
1000 * added, we'll have a return of -EAGAIN above.
1001 */
1002 if (!ret)
1003 hdr.count = fill.cur;
1004
1005 reset_info_exit:
1006 if (copy_to_user((void __user *)arg, &hdr, minsz))
1007 ret = -EFAULT;
1008
1009 if (!ret) {
1010 if (copy_to_user((void __user *)(arg + minsz), devices,
1011 hdr.count * sizeof(*devices)))
1012 ret = -EFAULT;
1013 }
1014
1015 kfree(devices);
1016 return ret;
1017
1018 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
1019 struct vfio_pci_hot_reset hdr;
1020 int32_t *group_fds;
1021 struct vfio_group **groups;
1022 struct vfio_pci_group_info info;
1023 bool slot = false;
1024 int group_idx, count = 0, ret = 0;
1025
1026 minsz = offsetofend(struct vfio_pci_hot_reset, count);
1027
1028 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1029 return -EFAULT;
1030
1031 if (hdr.argsz < minsz || hdr.flags)
1032 return -EINVAL;
1033
1034 /* Can we do a slot or bus reset or neither? */
1035 if (!pci_probe_reset_slot(vdev->pdev->slot))
1036 slot = true;
1037 else if (pci_probe_reset_bus(vdev->pdev->bus))
1038 return -ENODEV;
1039
1040 /*
1041 * We can't let userspace give us an arbitrarily large
1042 * buffer to copy, so verify how many we think there
1043 * could be. Note groups can have multiple devices so
1044 * one group per device is the max.
1045 */
1046 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1047 vfio_pci_count_devs,
1048 &count, slot);
1049 if (ret)
1050 return ret;
1051
1052 /* Somewhere between 1 and count is OK */
1053 if (!hdr.count || hdr.count > count)
1054 return -EINVAL;
1055
1056 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
1057 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
1058 if (!group_fds || !groups) {
1059 kfree(group_fds);
1060 kfree(groups);
1061 return -ENOMEM;
1062 }
1063
1064 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
1065 hdr.count * sizeof(*group_fds))) {
1066 kfree(group_fds);
1067 kfree(groups);
1068 return -EFAULT;
1069 }
1070
1071 /*
1072 * For each group_fd, get the group through the vfio external
1073 * user interface and store the group and iommu ID. This
1074 * ensures the group is held across the reset.
1075 */
1076 for (group_idx = 0; group_idx < hdr.count; group_idx++) {
1077 struct vfio_group *group;
1078 struct fd f = fdget(group_fds[group_idx]);
1079 if (!f.file) {
1080 ret = -EBADF;
1081 break;
1082 }
1083
1084 group = vfio_group_get_external_user(f.file);
1085 fdput(f);
1086 if (IS_ERR(group)) {
1087 ret = PTR_ERR(group);
1088 break;
1089 }
1090
1091 groups[group_idx] = group;
1092 }
1093
1094 kfree(group_fds);
1095
1096 /* release reference to groups on error */
1097 if (ret)
1098 goto hot_reset_release;
1099
1100 info.count = hdr.count;
1101 info.groups = groups;
1102
1103 ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info);
1104
1105 hot_reset_release:
1106 for (group_idx--; group_idx >= 0; group_idx--)
1107 vfio_group_put_external_user(groups[group_idx]);
1108
1109 kfree(groups);
1110 return ret;
1111 } else if (cmd == VFIO_DEVICE_IOEVENTFD) {
1112 struct vfio_device_ioeventfd ioeventfd;
1113 int count;
1114
1115 minsz = offsetofend(struct vfio_device_ioeventfd, fd);
1116
1117 if (copy_from_user(&ioeventfd, (void __user *)arg, minsz))
1118 return -EFAULT;
1119
1120 if (ioeventfd.argsz < minsz)
1121 return -EINVAL;
1122
1123 if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK)
1124 return -EINVAL;
1125
1126 count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK;
1127
1128 if (hweight8(count) != 1 || ioeventfd.fd < -1)
1129 return -EINVAL;
1130
1131 return vfio_pci_ioeventfd(vdev, ioeventfd.offset,
1132 ioeventfd.data, count, ioeventfd.fd);
1133 } else if (cmd == VFIO_DEVICE_FEATURE) {
1134 struct vfio_device_feature feature;
1135 uuid_t uuid;
1136
1137 minsz = offsetofend(struct vfio_device_feature, flags);
1138
1139 if (copy_from_user(&feature, (void __user *)arg, minsz))
1140 return -EFAULT;
1141
1142 if (feature.argsz < minsz)
1143 return -EINVAL;
1144
1145 /* Check unknown flags */
1146 if (feature.flags & ~(VFIO_DEVICE_FEATURE_MASK |
1147 VFIO_DEVICE_FEATURE_SET |
1148 VFIO_DEVICE_FEATURE_GET |
1149 VFIO_DEVICE_FEATURE_PROBE))
1150 return -EINVAL;
1151
1152 /* GET & SET are mutually exclusive except with PROBE */
1153 if (!(feature.flags & VFIO_DEVICE_FEATURE_PROBE) &&
1154 (feature.flags & VFIO_DEVICE_FEATURE_SET) &&
1155 (feature.flags & VFIO_DEVICE_FEATURE_GET))
1156 return -EINVAL;
1157
1158 switch (feature.flags & VFIO_DEVICE_FEATURE_MASK) {
1159 case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN:
1160 if (!vdev->vf_token)
1161 return -ENOTTY;
1162
1163 /*
1164 * We do not support GET of the VF Token UUID as this
1165 * could expose the token of the previous device user.
1166 */
1167 if (feature.flags & VFIO_DEVICE_FEATURE_GET)
1168 return -EINVAL;
1169
1170 if (feature.flags & VFIO_DEVICE_FEATURE_PROBE)
1171 return 0;
1172
1173 /* Don't SET unless told to do so */
1174 if (!(feature.flags & VFIO_DEVICE_FEATURE_SET))
1175 return -EINVAL;
1176
1177 if (feature.argsz < minsz + sizeof(uuid))
1178 return -EINVAL;
1179
1180 if (copy_from_user(&uuid, (void __user *)(arg + minsz),
1181 sizeof(uuid)))
1182 return -EFAULT;
1183
1184 mutex_lock(&vdev->vf_token->lock);
1185 uuid_copy(&vdev->vf_token->uuid, &uuid);
1186 mutex_unlock(&vdev->vf_token->lock);
1187
1188 return 0;
1189 default:
1190 return -ENOTTY;
1191 }
1192 }
1193
1194 return -ENOTTY;
1195 }
1196 EXPORT_SYMBOL_GPL(vfio_pci_core_ioctl);
1197
vfio_pci_rw(struct vfio_pci_core_device * vdev,char __user * buf,size_t count,loff_t * ppos,bool iswrite)1198 static ssize_t vfio_pci_rw(struct vfio_pci_core_device *vdev, char __user *buf,
1199 size_t count, loff_t *ppos, bool iswrite)
1200 {
1201 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1202
1203 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
1204 return -EINVAL;
1205
1206 switch (index) {
1207 case VFIO_PCI_CONFIG_REGION_INDEX:
1208 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
1209
1210 case VFIO_PCI_ROM_REGION_INDEX:
1211 if (iswrite)
1212 return -EINVAL;
1213 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
1214
1215 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1216 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
1217
1218 case VFIO_PCI_VGA_REGION_INDEX:
1219 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
1220 default:
1221 index -= VFIO_PCI_NUM_REGIONS;
1222 return vdev->region[index].ops->rw(vdev, buf,
1223 count, ppos, iswrite);
1224 }
1225
1226 return -EINVAL;
1227 }
1228
vfio_pci_core_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)1229 ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
1230 size_t count, loff_t *ppos)
1231 {
1232 struct vfio_pci_core_device *vdev =
1233 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1234
1235 if (!count)
1236 return 0;
1237
1238 return vfio_pci_rw(vdev, buf, count, ppos, false);
1239 }
1240 EXPORT_SYMBOL_GPL(vfio_pci_core_read);
1241
vfio_pci_core_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)1242 ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
1243 size_t count, loff_t *ppos)
1244 {
1245 struct vfio_pci_core_device *vdev =
1246 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1247
1248 if (!count)
1249 return 0;
1250
1251 return vfio_pci_rw(vdev, (char __user *)buf, count, ppos, true);
1252 }
1253 EXPORT_SYMBOL_GPL(vfio_pci_core_write);
1254
1255 /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */
vfio_pci_zap_and_vma_lock(struct vfio_pci_core_device * vdev,bool try)1256 static int vfio_pci_zap_and_vma_lock(struct vfio_pci_core_device *vdev, bool try)
1257 {
1258 struct vfio_pci_mmap_vma *mmap_vma, *tmp;
1259
1260 /*
1261 * Lock ordering:
1262 * vma_lock is nested under mmap_lock for vm_ops callback paths.
1263 * The memory_lock semaphore is used by both code paths calling
1264 * into this function to zap vmas and the vm_ops.fault callback
1265 * to protect the memory enable state of the device.
1266 *
1267 * When zapping vmas we need to maintain the mmap_lock => vma_lock
1268 * ordering, which requires using vma_lock to walk vma_list to
1269 * acquire an mm, then dropping vma_lock to get the mmap_lock and
1270 * reacquiring vma_lock. This logic is derived from similar
1271 * requirements in uverbs_user_mmap_disassociate().
1272 *
1273 * mmap_lock must always be the top-level lock when it is taken.
1274 * Therefore we can only hold the memory_lock write lock when
1275 * vma_list is empty, as we'd need to take mmap_lock to clear
1276 * entries. vma_list can only be guaranteed empty when holding
1277 * vma_lock, thus memory_lock is nested under vma_lock.
1278 *
1279 * This enables the vm_ops.fault callback to acquire vma_lock,
1280 * followed by memory_lock read lock, while already holding
1281 * mmap_lock without risk of deadlock.
1282 */
1283 while (1) {
1284 struct mm_struct *mm = NULL;
1285
1286 if (try) {
1287 if (!mutex_trylock(&vdev->vma_lock))
1288 return 0;
1289 } else {
1290 mutex_lock(&vdev->vma_lock);
1291 }
1292 while (!list_empty(&vdev->vma_list)) {
1293 mmap_vma = list_first_entry(&vdev->vma_list,
1294 struct vfio_pci_mmap_vma,
1295 vma_next);
1296 mm = mmap_vma->vma->vm_mm;
1297 if (mmget_not_zero(mm))
1298 break;
1299
1300 list_del(&mmap_vma->vma_next);
1301 kfree(mmap_vma);
1302 mm = NULL;
1303 }
1304 if (!mm)
1305 return 1;
1306 mutex_unlock(&vdev->vma_lock);
1307
1308 if (try) {
1309 if (!mmap_read_trylock(mm)) {
1310 mmput(mm);
1311 return 0;
1312 }
1313 } else {
1314 mmap_read_lock(mm);
1315 }
1316 if (try) {
1317 if (!mutex_trylock(&vdev->vma_lock)) {
1318 mmap_read_unlock(mm);
1319 mmput(mm);
1320 return 0;
1321 }
1322 } else {
1323 mutex_lock(&vdev->vma_lock);
1324 }
1325 list_for_each_entry_safe(mmap_vma, tmp,
1326 &vdev->vma_list, vma_next) {
1327 struct vm_area_struct *vma = mmap_vma->vma;
1328
1329 if (vma->vm_mm != mm)
1330 continue;
1331
1332 list_del(&mmap_vma->vma_next);
1333 kfree(mmap_vma);
1334
1335 zap_vma_ptes(vma, vma->vm_start,
1336 vma->vm_end - vma->vm_start);
1337 }
1338 mutex_unlock(&vdev->vma_lock);
1339 mmap_read_unlock(mm);
1340 mmput(mm);
1341 }
1342 }
1343
vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device * vdev)1344 void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev)
1345 {
1346 vfio_pci_zap_and_vma_lock(vdev, false);
1347 down_write(&vdev->memory_lock);
1348 mutex_unlock(&vdev->vma_lock);
1349 }
1350
vfio_pci_memory_lock_and_enable(struct vfio_pci_core_device * vdev)1351 u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_core_device *vdev)
1352 {
1353 u16 cmd;
1354
1355 down_write(&vdev->memory_lock);
1356 pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd);
1357 if (!(cmd & PCI_COMMAND_MEMORY))
1358 pci_write_config_word(vdev->pdev, PCI_COMMAND,
1359 cmd | PCI_COMMAND_MEMORY);
1360
1361 return cmd;
1362 }
1363
vfio_pci_memory_unlock_and_restore(struct vfio_pci_core_device * vdev,u16 cmd)1364 void vfio_pci_memory_unlock_and_restore(struct vfio_pci_core_device *vdev, u16 cmd)
1365 {
1366 pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd);
1367 up_write(&vdev->memory_lock);
1368 }
1369
1370 /* Caller holds vma_lock */
__vfio_pci_add_vma(struct vfio_pci_core_device * vdev,struct vm_area_struct * vma)1371 static int __vfio_pci_add_vma(struct vfio_pci_core_device *vdev,
1372 struct vm_area_struct *vma)
1373 {
1374 struct vfio_pci_mmap_vma *mmap_vma;
1375
1376 mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL);
1377 if (!mmap_vma)
1378 return -ENOMEM;
1379
1380 mmap_vma->vma = vma;
1381 list_add(&mmap_vma->vma_next, &vdev->vma_list);
1382
1383 return 0;
1384 }
1385
1386 /*
1387 * Zap mmaps on open so that we can fault them in on access and therefore
1388 * our vma_list only tracks mappings accessed since last zap.
1389 */
vfio_pci_mmap_open(struct vm_area_struct * vma)1390 static void vfio_pci_mmap_open(struct vm_area_struct *vma)
1391 {
1392 zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
1393 }
1394
vfio_pci_mmap_close(struct vm_area_struct * vma)1395 static void vfio_pci_mmap_close(struct vm_area_struct *vma)
1396 {
1397 struct vfio_pci_core_device *vdev = vma->vm_private_data;
1398 struct vfio_pci_mmap_vma *mmap_vma;
1399
1400 mutex_lock(&vdev->vma_lock);
1401 list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
1402 if (mmap_vma->vma == vma) {
1403 list_del(&mmap_vma->vma_next);
1404 kfree(mmap_vma);
1405 break;
1406 }
1407 }
1408 mutex_unlock(&vdev->vma_lock);
1409 }
1410
vfio_pci_mmap_fault(struct vm_fault * vmf)1411 static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
1412 {
1413 struct vm_area_struct *vma = vmf->vma;
1414 struct vfio_pci_core_device *vdev = vma->vm_private_data;
1415 struct vfio_pci_mmap_vma *mmap_vma;
1416 vm_fault_t ret = VM_FAULT_NOPAGE;
1417
1418 mutex_lock(&vdev->vma_lock);
1419 down_read(&vdev->memory_lock);
1420
1421 if (!__vfio_pci_memory_enabled(vdev)) {
1422 ret = VM_FAULT_SIGBUS;
1423 goto up_out;
1424 }
1425
1426 /*
1427 * We populate the whole vma on fault, so we need to test whether
1428 * the vma has already been mapped, such as for concurrent faults
1429 * to the same vma. io_remap_pfn_range() will trigger a BUG_ON if
1430 * we ask it to fill the same range again.
1431 */
1432 list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
1433 if (mmap_vma->vma == vma)
1434 goto up_out;
1435 }
1436
1437 if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1438 vma->vm_end - vma->vm_start,
1439 vma->vm_page_prot)) {
1440 ret = VM_FAULT_SIGBUS;
1441 zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
1442 goto up_out;
1443 }
1444
1445 if (__vfio_pci_add_vma(vdev, vma)) {
1446 ret = VM_FAULT_OOM;
1447 zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
1448 }
1449
1450 up_out:
1451 up_read(&vdev->memory_lock);
1452 mutex_unlock(&vdev->vma_lock);
1453 return ret;
1454 }
1455
1456 static const struct vm_operations_struct vfio_pci_mmap_ops = {
1457 .open = vfio_pci_mmap_open,
1458 .close = vfio_pci_mmap_close,
1459 .fault = vfio_pci_mmap_fault,
1460 };
1461
vfio_pci_core_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)1462 int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
1463 {
1464 struct vfio_pci_core_device *vdev =
1465 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1466 struct pci_dev *pdev = vdev->pdev;
1467 unsigned int index;
1468 u64 phys_len, req_len, pgoff, req_start;
1469 int ret;
1470
1471 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1472
1473 if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
1474 return -EINVAL;
1475 if (vma->vm_end < vma->vm_start)
1476 return -EINVAL;
1477 if ((vma->vm_flags & VM_SHARED) == 0)
1478 return -EINVAL;
1479 if (index >= VFIO_PCI_NUM_REGIONS) {
1480 int regnum = index - VFIO_PCI_NUM_REGIONS;
1481 struct vfio_pci_region *region = vdev->region + regnum;
1482
1483 if (region->ops && region->ops->mmap &&
1484 (region->flags & VFIO_REGION_INFO_FLAG_MMAP))
1485 return region->ops->mmap(vdev, region, vma);
1486 return -EINVAL;
1487 }
1488 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1489 return -EINVAL;
1490 if (!vdev->bar_mmap_supported[index])
1491 return -EINVAL;
1492
1493 phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
1494 req_len = vma->vm_end - vma->vm_start;
1495 pgoff = vma->vm_pgoff &
1496 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1497 req_start = pgoff << PAGE_SHIFT;
1498
1499 if (req_start + req_len > phys_len)
1500 return -EINVAL;
1501
1502 /*
1503 * Even though we don't make use of the barmap for the mmap,
1504 * we need to request the region and the barmap tracks that.
1505 */
1506 if (!vdev->barmap[index]) {
1507 ret = pci_request_selected_regions(pdev,
1508 1 << index, "vfio-pci");
1509 if (ret)
1510 return ret;
1511
1512 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1513 if (!vdev->barmap[index]) {
1514 pci_release_selected_regions(pdev, 1 << index);
1515 return -ENOMEM;
1516 }
1517 }
1518
1519 vma->vm_private_data = vdev;
1520 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1521 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
1522
1523 /*
1524 * See remap_pfn_range(), called from vfio_pci_fault() but we can't
1525 * change vm_flags within the fault handler. Set them now.
1526 */
1527 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1528 vma->vm_ops = &vfio_pci_mmap_ops;
1529
1530 return 0;
1531 }
1532 EXPORT_SYMBOL_GPL(vfio_pci_core_mmap);
1533
vfio_pci_core_request(struct vfio_device * core_vdev,unsigned int count)1534 void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)
1535 {
1536 struct vfio_pci_core_device *vdev =
1537 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1538 struct pci_dev *pdev = vdev->pdev;
1539
1540 mutex_lock(&vdev->igate);
1541
1542 if (vdev->req_trigger) {
1543 if (!(count % 10))
1544 pci_notice_ratelimited(pdev,
1545 "Relaying device request to user (#%u)\n",
1546 count);
1547 eventfd_signal(vdev->req_trigger, 1);
1548 } else if (count == 0) {
1549 pci_warn(pdev,
1550 "No device request channel registered, blocked until released by user\n");
1551 }
1552
1553 mutex_unlock(&vdev->igate);
1554 }
1555 EXPORT_SYMBOL_GPL(vfio_pci_core_request);
1556
vfio_pci_validate_vf_token(struct vfio_pci_core_device * vdev,bool vf_token,uuid_t * uuid)1557 static int vfio_pci_validate_vf_token(struct vfio_pci_core_device *vdev,
1558 bool vf_token, uuid_t *uuid)
1559 {
1560 /*
1561 * There's always some degree of trust or collaboration between SR-IOV
1562 * PF and VFs, even if just that the PF hosts the SR-IOV capability and
1563 * can disrupt VFs with a reset, but often the PF has more explicit
1564 * access to deny service to the VF or access data passed through the
1565 * VF. We therefore require an opt-in via a shared VF token (UUID) to
1566 * represent this trust. This both prevents that a VF driver might
1567 * assume the PF driver is a trusted, in-kernel driver, and also that
1568 * a PF driver might be replaced with a rogue driver, unknown to in-use
1569 * VF drivers.
1570 *
1571 * Therefore when presented with a VF, if the PF is a vfio device and
1572 * it is bound to the vfio-pci driver, the user needs to provide a VF
1573 * token to access the device, in the form of appending a vf_token to
1574 * the device name, for example:
1575 *
1576 * "0000:04:10.0 vf_token=bd8d9d2b-5a5f-4f5a-a211-f591514ba1f3"
1577 *
1578 * When presented with a PF which has VFs in use, the user must also
1579 * provide the current VF token to prove collaboration with existing
1580 * VF users. If VFs are not in use, the VF token provided for the PF
1581 * device will act to set the VF token.
1582 *
1583 * If the VF token is provided but unused, an error is generated.
1584 */
1585 if (vdev->pdev->is_virtfn) {
1586 struct vfio_pci_core_device *pf_vdev = vdev->sriov_pf_core_dev;
1587 bool match;
1588
1589 if (!pf_vdev) {
1590 if (!vf_token)
1591 return 0; /* PF is not vfio-pci, no VF token */
1592
1593 pci_info_ratelimited(vdev->pdev,
1594 "VF token incorrectly provided, PF not bound to vfio-pci\n");
1595 return -EINVAL;
1596 }
1597
1598 if (!vf_token) {
1599 pci_info_ratelimited(vdev->pdev,
1600 "VF token required to access device\n");
1601 return -EACCES;
1602 }
1603
1604 mutex_lock(&pf_vdev->vf_token->lock);
1605 match = uuid_equal(uuid, &pf_vdev->vf_token->uuid);
1606 mutex_unlock(&pf_vdev->vf_token->lock);
1607
1608 if (!match) {
1609 pci_info_ratelimited(vdev->pdev,
1610 "Incorrect VF token provided for device\n");
1611 return -EACCES;
1612 }
1613 } else if (vdev->vf_token) {
1614 mutex_lock(&vdev->vf_token->lock);
1615 if (vdev->vf_token->users) {
1616 if (!vf_token) {
1617 mutex_unlock(&vdev->vf_token->lock);
1618 pci_info_ratelimited(vdev->pdev,
1619 "VF token required to access device\n");
1620 return -EACCES;
1621 }
1622
1623 if (!uuid_equal(uuid, &vdev->vf_token->uuid)) {
1624 mutex_unlock(&vdev->vf_token->lock);
1625 pci_info_ratelimited(vdev->pdev,
1626 "Incorrect VF token provided for device\n");
1627 return -EACCES;
1628 }
1629 } else if (vf_token) {
1630 uuid_copy(&vdev->vf_token->uuid, uuid);
1631 }
1632
1633 mutex_unlock(&vdev->vf_token->lock);
1634 } else if (vf_token) {
1635 pci_info_ratelimited(vdev->pdev,
1636 "VF token incorrectly provided, not a PF or VF\n");
1637 return -EINVAL;
1638 }
1639
1640 return 0;
1641 }
1642
1643 #define VF_TOKEN_ARG "vf_token="
1644
vfio_pci_core_match(struct vfio_device * core_vdev,char * buf)1645 int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf)
1646 {
1647 struct vfio_pci_core_device *vdev =
1648 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1649 bool vf_token = false;
1650 uuid_t uuid;
1651 int ret;
1652
1653 if (strncmp(pci_name(vdev->pdev), buf, strlen(pci_name(vdev->pdev))))
1654 return 0; /* No match */
1655
1656 if (strlen(buf) > strlen(pci_name(vdev->pdev))) {
1657 buf += strlen(pci_name(vdev->pdev));
1658
1659 if (*buf != ' ')
1660 return 0; /* No match: non-whitespace after name */
1661
1662 while (*buf) {
1663 if (*buf == ' ') {
1664 buf++;
1665 continue;
1666 }
1667
1668 if (!vf_token && !strncmp(buf, VF_TOKEN_ARG,
1669 strlen(VF_TOKEN_ARG))) {
1670 buf += strlen(VF_TOKEN_ARG);
1671
1672 if (strlen(buf) < UUID_STRING_LEN)
1673 return -EINVAL;
1674
1675 ret = uuid_parse(buf, &uuid);
1676 if (ret)
1677 return ret;
1678
1679 vf_token = true;
1680 buf += UUID_STRING_LEN;
1681 } else {
1682 /* Unknown/duplicate option */
1683 return -EINVAL;
1684 }
1685 }
1686 }
1687
1688 ret = vfio_pci_validate_vf_token(vdev, vf_token, &uuid);
1689 if (ret)
1690 return ret;
1691
1692 return 1; /* Match */
1693 }
1694 EXPORT_SYMBOL_GPL(vfio_pci_core_match);
1695
vfio_pci_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1696 static int vfio_pci_bus_notifier(struct notifier_block *nb,
1697 unsigned long action, void *data)
1698 {
1699 struct vfio_pci_core_device *vdev = container_of(nb,
1700 struct vfio_pci_core_device, nb);
1701 struct device *dev = data;
1702 struct pci_dev *pdev = to_pci_dev(dev);
1703 struct pci_dev *physfn = pci_physfn(pdev);
1704
1705 if (action == BUS_NOTIFY_ADD_DEVICE &&
1706 pdev->is_virtfn && physfn == vdev->pdev) {
1707 pci_info(vdev->pdev, "Captured SR-IOV VF %s driver_override\n",
1708 pci_name(pdev));
1709 pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
1710 vdev->vdev.ops->name);
1711 } else if (action == BUS_NOTIFY_BOUND_DRIVER &&
1712 pdev->is_virtfn && physfn == vdev->pdev) {
1713 struct pci_driver *drv = pci_dev_driver(pdev);
1714
1715 if (drv && drv != pci_dev_driver(vdev->pdev))
1716 pci_warn(vdev->pdev,
1717 "VF %s bound to driver %s while PF bound to driver %s\n",
1718 pci_name(pdev), drv->name,
1719 pci_dev_driver(vdev->pdev)->name);
1720 }
1721
1722 return 0;
1723 }
1724
vfio_pci_vf_init(struct vfio_pci_core_device * vdev)1725 static int vfio_pci_vf_init(struct vfio_pci_core_device *vdev)
1726 {
1727 struct pci_dev *pdev = vdev->pdev;
1728 struct vfio_pci_core_device *cur;
1729 struct pci_dev *physfn;
1730 int ret;
1731
1732 if (pdev->is_virtfn) {
1733 /*
1734 * If this VF was created by our vfio_pci_core_sriov_configure()
1735 * then we can find the PF vfio_pci_core_device now, and due to
1736 * the locking in pci_disable_sriov() it cannot change until
1737 * this VF device driver is removed.
1738 */
1739 physfn = pci_physfn(vdev->pdev);
1740 mutex_lock(&vfio_pci_sriov_pfs_mutex);
1741 list_for_each_entry(cur, &vfio_pci_sriov_pfs, sriov_pfs_item) {
1742 if (cur->pdev == physfn) {
1743 vdev->sriov_pf_core_dev = cur;
1744 break;
1745 }
1746 }
1747 mutex_unlock(&vfio_pci_sriov_pfs_mutex);
1748 return 0;
1749 }
1750
1751 /* Not a SRIOV PF */
1752 if (!pdev->is_physfn)
1753 return 0;
1754
1755 vdev->vf_token = kzalloc(sizeof(*vdev->vf_token), GFP_KERNEL);
1756 if (!vdev->vf_token)
1757 return -ENOMEM;
1758
1759 mutex_init(&vdev->vf_token->lock);
1760 uuid_gen(&vdev->vf_token->uuid);
1761
1762 vdev->nb.notifier_call = vfio_pci_bus_notifier;
1763 ret = bus_register_notifier(&pci_bus_type, &vdev->nb);
1764 if (ret) {
1765 kfree(vdev->vf_token);
1766 return ret;
1767 }
1768 return 0;
1769 }
1770
vfio_pci_vf_uninit(struct vfio_pci_core_device * vdev)1771 static void vfio_pci_vf_uninit(struct vfio_pci_core_device *vdev)
1772 {
1773 if (!vdev->vf_token)
1774 return;
1775
1776 bus_unregister_notifier(&pci_bus_type, &vdev->nb);
1777 WARN_ON(vdev->vf_token->users);
1778 mutex_destroy(&vdev->vf_token->lock);
1779 kfree(vdev->vf_token);
1780 }
1781
vfio_pci_vga_init(struct vfio_pci_core_device * vdev)1782 static int vfio_pci_vga_init(struct vfio_pci_core_device *vdev)
1783 {
1784 struct pci_dev *pdev = vdev->pdev;
1785 int ret;
1786
1787 if (!vfio_pci_is_vga(pdev))
1788 return 0;
1789
1790 ret = vga_client_register(pdev, vfio_pci_set_decode);
1791 if (ret)
1792 return ret;
1793 vga_set_legacy_decoding(pdev, vfio_pci_set_decode(pdev, false));
1794 return 0;
1795 }
1796
vfio_pci_vga_uninit(struct vfio_pci_core_device * vdev)1797 static void vfio_pci_vga_uninit(struct vfio_pci_core_device *vdev)
1798 {
1799 struct pci_dev *pdev = vdev->pdev;
1800
1801 if (!vfio_pci_is_vga(pdev))
1802 return;
1803 vga_client_unregister(pdev);
1804 vga_set_legacy_decoding(pdev, VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1805 VGA_RSRC_LEGACY_IO |
1806 VGA_RSRC_LEGACY_MEM);
1807 }
1808
vfio_pci_core_init_device(struct vfio_pci_core_device * vdev,struct pci_dev * pdev,const struct vfio_device_ops * vfio_pci_ops)1809 void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev,
1810 struct pci_dev *pdev,
1811 const struct vfio_device_ops *vfio_pci_ops)
1812 {
1813 vfio_init_group_dev(&vdev->vdev, &pdev->dev, vfio_pci_ops);
1814 vdev->pdev = pdev;
1815 vdev->irq_type = VFIO_PCI_NUM_IRQS;
1816 mutex_init(&vdev->igate);
1817 spin_lock_init(&vdev->irqlock);
1818 mutex_init(&vdev->ioeventfds_lock);
1819 INIT_LIST_HEAD(&vdev->dummy_resources_list);
1820 INIT_LIST_HEAD(&vdev->ioeventfds_list);
1821 mutex_init(&vdev->vma_lock);
1822 INIT_LIST_HEAD(&vdev->vma_list);
1823 INIT_LIST_HEAD(&vdev->sriov_pfs_item);
1824 init_rwsem(&vdev->memory_lock);
1825 }
1826 EXPORT_SYMBOL_GPL(vfio_pci_core_init_device);
1827
vfio_pci_core_uninit_device(struct vfio_pci_core_device * vdev)1828 void vfio_pci_core_uninit_device(struct vfio_pci_core_device *vdev)
1829 {
1830 mutex_destroy(&vdev->igate);
1831 mutex_destroy(&vdev->ioeventfds_lock);
1832 mutex_destroy(&vdev->vma_lock);
1833 vfio_uninit_group_dev(&vdev->vdev);
1834 kfree(vdev->region);
1835 kfree(vdev->pm_save);
1836 }
1837 EXPORT_SYMBOL_GPL(vfio_pci_core_uninit_device);
1838
vfio_pci_core_register_device(struct vfio_pci_core_device * vdev)1839 int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
1840 {
1841 struct pci_dev *pdev = vdev->pdev;
1842 struct iommu_group *group;
1843 int ret;
1844
1845 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1846 return -EINVAL;
1847
1848 /*
1849 * Prevent binding to PFs with VFs enabled, the VFs might be in use
1850 * by the host or other users. We cannot capture the VFs if they
1851 * already exist, nor can we track VF users. Disabling SR-IOV here
1852 * would initiate removing the VFs, which would unbind the driver,
1853 * which is prone to blocking if that VF is also in use by vfio-pci.
1854 * Just reject these PFs and let the user sort it out.
1855 */
1856 if (pci_num_vf(pdev)) {
1857 pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n");
1858 return -EBUSY;
1859 }
1860
1861 group = vfio_iommu_group_get(&pdev->dev);
1862 if (!group)
1863 return -EINVAL;
1864
1865 if (pci_is_root_bus(pdev->bus)) {
1866 ret = vfio_assign_device_set(&vdev->vdev, vdev);
1867 } else if (!pci_probe_reset_slot(pdev->slot)) {
1868 ret = vfio_assign_device_set(&vdev->vdev, pdev->slot);
1869 } else {
1870 /*
1871 * If there is no slot reset support for this device, the whole
1872 * bus needs to be grouped together to support bus-wide resets.
1873 */
1874 ret = vfio_assign_device_set(&vdev->vdev, pdev->bus);
1875 }
1876
1877 if (ret)
1878 goto out_group_put;
1879 ret = vfio_pci_vf_init(vdev);
1880 if (ret)
1881 goto out_group_put;
1882 ret = vfio_pci_vga_init(vdev);
1883 if (ret)
1884 goto out_vf;
1885
1886 vfio_pci_probe_power_state(vdev);
1887
1888 if (!disable_idle_d3) {
1889 /*
1890 * pci-core sets the device power state to an unknown value at
1891 * bootup and after being removed from a driver. The only
1892 * transition it allows from this unknown state is to D0, which
1893 * typically happens when a driver calls pci_enable_device().
1894 * We're not ready to enable the device yet, but we do want to
1895 * be able to get to D3. Therefore first do a D0 transition
1896 * before going to D3.
1897 */
1898 vfio_pci_set_power_state(vdev, PCI_D0);
1899 vfio_pci_set_power_state(vdev, PCI_D3hot);
1900 }
1901
1902 ret = vfio_register_group_dev(&vdev->vdev);
1903 if (ret)
1904 goto out_power;
1905 return 0;
1906
1907 out_power:
1908 if (!disable_idle_d3)
1909 vfio_pci_set_power_state(vdev, PCI_D0);
1910 out_vf:
1911 vfio_pci_vf_uninit(vdev);
1912 out_group_put:
1913 vfio_iommu_group_put(group, &pdev->dev);
1914 return ret;
1915 }
1916 EXPORT_SYMBOL_GPL(vfio_pci_core_register_device);
1917
vfio_pci_core_unregister_device(struct vfio_pci_core_device * vdev)1918 void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev)
1919 {
1920 struct pci_dev *pdev = vdev->pdev;
1921
1922 vfio_pci_core_sriov_configure(pdev, 0);
1923
1924 vfio_unregister_group_dev(&vdev->vdev);
1925
1926 vfio_pci_vf_uninit(vdev);
1927 vfio_pci_vga_uninit(vdev);
1928
1929 vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
1930
1931 if (!disable_idle_d3)
1932 vfio_pci_set_power_state(vdev, PCI_D0);
1933 }
1934 EXPORT_SYMBOL_GPL(vfio_pci_core_unregister_device);
1935
vfio_pci_aer_err_detected(struct pci_dev * pdev,pci_channel_state_t state)1936 static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1937 pci_channel_state_t state)
1938 {
1939 struct vfio_pci_core_device *vdev;
1940 struct vfio_device *device;
1941
1942 device = vfio_device_get_from_dev(&pdev->dev);
1943 if (device == NULL)
1944 return PCI_ERS_RESULT_DISCONNECT;
1945
1946 vdev = container_of(device, struct vfio_pci_core_device, vdev);
1947
1948 mutex_lock(&vdev->igate);
1949
1950 if (vdev->err_trigger)
1951 eventfd_signal(vdev->err_trigger, 1);
1952
1953 mutex_unlock(&vdev->igate);
1954
1955 vfio_device_put(device);
1956
1957 return PCI_ERS_RESULT_CAN_RECOVER;
1958 }
1959
vfio_pci_core_sriov_configure(struct pci_dev * pdev,int nr_virtfn)1960 int vfio_pci_core_sriov_configure(struct pci_dev *pdev, int nr_virtfn)
1961 {
1962 struct vfio_pci_core_device *vdev;
1963 struct vfio_device *device;
1964 int ret = 0;
1965
1966 device_lock_assert(&pdev->dev);
1967
1968 device = vfio_device_get_from_dev(&pdev->dev);
1969 if (!device)
1970 return -ENODEV;
1971
1972 vdev = container_of(device, struct vfio_pci_core_device, vdev);
1973
1974 if (nr_virtfn) {
1975 mutex_lock(&vfio_pci_sriov_pfs_mutex);
1976 /*
1977 * The thread that adds the vdev to the list is the only thread
1978 * that gets to call pci_enable_sriov() and we will only allow
1979 * it to be called once without going through
1980 * pci_disable_sriov()
1981 */
1982 if (!list_empty(&vdev->sriov_pfs_item)) {
1983 ret = -EINVAL;
1984 goto out_unlock;
1985 }
1986 list_add_tail(&vdev->sriov_pfs_item, &vfio_pci_sriov_pfs);
1987 mutex_unlock(&vfio_pci_sriov_pfs_mutex);
1988 ret = pci_enable_sriov(pdev, nr_virtfn);
1989 if (ret)
1990 goto out_del;
1991 ret = nr_virtfn;
1992 goto out_put;
1993 }
1994
1995 pci_disable_sriov(pdev);
1996
1997 out_del:
1998 mutex_lock(&vfio_pci_sriov_pfs_mutex);
1999 list_del_init(&vdev->sriov_pfs_item);
2000 out_unlock:
2001 mutex_unlock(&vfio_pci_sriov_pfs_mutex);
2002 out_put:
2003 vfio_device_put(device);
2004 return ret;
2005 }
2006 EXPORT_SYMBOL_GPL(vfio_pci_core_sriov_configure);
2007
2008 const struct pci_error_handlers vfio_pci_core_err_handlers = {
2009 .error_detected = vfio_pci_aer_err_detected,
2010 };
2011 EXPORT_SYMBOL_GPL(vfio_pci_core_err_handlers);
2012
vfio_dev_in_groups(struct vfio_pci_core_device * vdev,struct vfio_pci_group_info * groups)2013 static bool vfio_dev_in_groups(struct vfio_pci_core_device *vdev,
2014 struct vfio_pci_group_info *groups)
2015 {
2016 unsigned int i;
2017
2018 for (i = 0; i < groups->count; i++)
2019 if (groups->groups[i] == vdev->vdev.group)
2020 return true;
2021 return false;
2022 }
2023
vfio_pci_is_device_in_set(struct pci_dev * pdev,void * data)2024 static int vfio_pci_is_device_in_set(struct pci_dev *pdev, void *data)
2025 {
2026 struct vfio_device_set *dev_set = data;
2027 struct vfio_device *cur;
2028
2029 list_for_each_entry(cur, &dev_set->device_list, dev_set_list)
2030 if (cur->dev == &pdev->dev)
2031 return 0;
2032 return -EBUSY;
2033 }
2034
2035 /*
2036 * vfio-core considers a group to be viable and will create a vfio_device even
2037 * if some devices are bound to drivers like pci-stub or pcieport. Here we
2038 * require all PCI devices to be inside our dev_set since that ensures they stay
2039 * put and that every driver controlling the device can co-ordinate with the
2040 * device reset.
2041 *
2042 * Returns the pci_dev to pass to pci_reset_bus() if every PCI device to be
2043 * reset is inside the dev_set, and pci_reset_bus() can succeed. NULL otherwise.
2044 */
2045 static struct pci_dev *
vfio_pci_dev_set_resettable(struct vfio_device_set * dev_set)2046 vfio_pci_dev_set_resettable(struct vfio_device_set *dev_set)
2047 {
2048 struct pci_dev *pdev;
2049
2050 lockdep_assert_held(&dev_set->lock);
2051
2052 /*
2053 * By definition all PCI devices in the dev_set share the same PCI
2054 * reset, so any pci_dev will have the same outcomes for
2055 * pci_probe_reset_*() and pci_reset_bus().
2056 */
2057 pdev = list_first_entry(&dev_set->device_list,
2058 struct vfio_pci_core_device,
2059 vdev.dev_set_list)->pdev;
2060
2061 /* pci_reset_bus() is supported */
2062 if (pci_probe_reset_slot(pdev->slot) && pci_probe_reset_bus(pdev->bus))
2063 return NULL;
2064
2065 if (vfio_pci_for_each_slot_or_bus(pdev, vfio_pci_is_device_in_set,
2066 dev_set,
2067 !pci_probe_reset_slot(pdev->slot)))
2068 return NULL;
2069 return pdev;
2070 }
2071
2072 /*
2073 * We need to get memory_lock for each device, but devices can share mmap_lock,
2074 * therefore we need to zap and hold the vma_lock for each device, and only then
2075 * get each memory_lock.
2076 */
vfio_pci_dev_set_hot_reset(struct vfio_device_set * dev_set,struct vfio_pci_group_info * groups)2077 static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,
2078 struct vfio_pci_group_info *groups)
2079 {
2080 struct vfio_pci_core_device *cur_mem;
2081 struct vfio_pci_core_device *cur_vma;
2082 struct vfio_pci_core_device *cur;
2083 struct pci_dev *pdev;
2084 bool is_mem = true;
2085 int ret;
2086
2087 mutex_lock(&dev_set->lock);
2088 cur_mem = list_first_entry(&dev_set->device_list,
2089 struct vfio_pci_core_device,
2090 vdev.dev_set_list);
2091
2092 pdev = vfio_pci_dev_set_resettable(dev_set);
2093 if (!pdev) {
2094 ret = -EINVAL;
2095 goto err_unlock;
2096 }
2097
2098 list_for_each_entry(cur_vma, &dev_set->device_list, vdev.dev_set_list) {
2099 /*
2100 * Test whether all the affected devices are contained by the
2101 * set of groups provided by the user.
2102 */
2103 if (!vfio_dev_in_groups(cur_vma, groups)) {
2104 ret = -EINVAL;
2105 goto err_undo;
2106 }
2107
2108 /*
2109 * Locking multiple devices is prone to deadlock, runaway and
2110 * unwind if we hit contention.
2111 */
2112 if (!vfio_pci_zap_and_vma_lock(cur_vma, true)) {
2113 ret = -EBUSY;
2114 goto err_undo;
2115 }
2116 }
2117 cur_vma = NULL;
2118
2119 list_for_each_entry(cur_mem, &dev_set->device_list, vdev.dev_set_list) {
2120 if (!down_write_trylock(&cur_mem->memory_lock)) {
2121 ret = -EBUSY;
2122 goto err_undo;
2123 }
2124 mutex_unlock(&cur_mem->vma_lock);
2125 }
2126 cur_mem = NULL;
2127
2128 /*
2129 * The pci_reset_bus() will reset all the devices in the bus.
2130 * The power state can be non-D0 for some of the devices in the bus.
2131 * For these devices, the pci_reset_bus() will internally set
2132 * the power state to D0 without vfio driver involvement.
2133 * For the devices which have NoSoftRst-, the reset function can
2134 * cause the PCI config space reset without restoring the original
2135 * state (saved locally in 'vdev->pm_save').
2136 */
2137 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list)
2138 vfio_pci_set_power_state(cur, PCI_D0);
2139
2140 ret = pci_reset_bus(pdev);
2141
2142 err_undo:
2143 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) {
2144 if (cur == cur_mem)
2145 is_mem = false;
2146 if (cur == cur_vma)
2147 break;
2148 if (is_mem)
2149 up_write(&cur->memory_lock);
2150 else
2151 mutex_unlock(&cur->vma_lock);
2152 }
2153 err_unlock:
2154 mutex_unlock(&dev_set->lock);
2155 return ret;
2156 }
2157
vfio_pci_dev_set_needs_reset(struct vfio_device_set * dev_set)2158 static bool vfio_pci_dev_set_needs_reset(struct vfio_device_set *dev_set)
2159 {
2160 struct vfio_pci_core_device *cur;
2161 bool needs_reset = false;
2162
2163 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) {
2164 /* No VFIO device in the set can have an open device FD */
2165 if (cur->vdev.open_count)
2166 return false;
2167 needs_reset |= cur->needs_reset;
2168 }
2169 return needs_reset;
2170 }
2171
2172 /*
2173 * If a bus or slot reset is available for the provided dev_set and:
2174 * - All of the devices affected by that bus or slot reset are unused
2175 * - At least one of the affected devices is marked dirty via
2176 * needs_reset (such as by lack of FLR support)
2177 * Then attempt to perform that bus or slot reset.
2178 * Returns true if the dev_set was reset.
2179 */
vfio_pci_dev_set_try_reset(struct vfio_device_set * dev_set)2180 static bool vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set)
2181 {
2182 struct vfio_pci_core_device *cur;
2183 struct pci_dev *pdev;
2184 int ret;
2185
2186 if (!vfio_pci_dev_set_needs_reset(dev_set))
2187 return false;
2188
2189 pdev = vfio_pci_dev_set_resettable(dev_set);
2190 if (!pdev)
2191 return false;
2192
2193 /*
2194 * The pci_reset_bus() will reset all the devices in the bus.
2195 * The power state can be non-D0 for some of the devices in the bus.
2196 * For these devices, the pci_reset_bus() will internally set
2197 * the power state to D0 without vfio driver involvement.
2198 * For the devices which have NoSoftRst-, the reset function can
2199 * cause the PCI config space reset without restoring the original
2200 * state (saved locally in 'vdev->pm_save').
2201 */
2202 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list)
2203 vfio_pci_set_power_state(cur, PCI_D0);
2204
2205 ret = pci_reset_bus(pdev);
2206 if (ret)
2207 return false;
2208
2209 list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) {
2210 cur->needs_reset = false;
2211 if (!disable_idle_d3)
2212 vfio_pci_set_power_state(cur, PCI_D3hot);
2213 }
2214 return true;
2215 }
2216
vfio_pci_core_set_params(bool is_nointxmask,bool is_disable_vga,bool is_disable_idle_d3)2217 void vfio_pci_core_set_params(bool is_nointxmask, bool is_disable_vga,
2218 bool is_disable_idle_d3)
2219 {
2220 nointxmask = is_nointxmask;
2221 disable_vga = is_disable_vga;
2222 disable_idle_d3 = is_disable_idle_d3;
2223 }
2224 EXPORT_SYMBOL_GPL(vfio_pci_core_set_params);
2225
vfio_pci_core_cleanup(void)2226 static void vfio_pci_core_cleanup(void)
2227 {
2228 vfio_pci_uninit_perm_bits();
2229 }
2230
vfio_pci_core_init(void)2231 static int __init vfio_pci_core_init(void)
2232 {
2233 /* Allocate shared config space permission data used by all devices */
2234 return vfio_pci_init_perm_bits();
2235 }
2236
2237 module_init(vfio_pci_core_init);
2238 module_exit(vfio_pci_core_cleanup);
2239
2240 MODULE_LICENSE("GPL v2");
2241 MODULE_AUTHOR(DRIVER_AUTHOR);
2242 MODULE_DESCRIPTION(DRIVER_DESC);
2243