common.c (7afee874f1b27abc998b8b747d16b77cb6398716) common.c (aff92b828647839b956dfa647a18b3ce10058e6a)
1/*
2 * generic functions used by VFIO devices
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Alex Williamson <alex.williamson@redhat.com>
8 *

--- 19 unchanged lines hidden (view full) ---

28#include "hw/vfio/vfio-common.h"
29#include "hw/vfio/vfio.h"
30#include "exec/address-spaces.h"
31#include "exec/memory.h"
32#include "hw/hw.h"
33#include "qemu/error-report.h"
34#include "qemu/main-loop.h"
35#include "qemu/range.h"
1/*
2 * generic functions used by VFIO devices
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Alex Williamson <alex.williamson@redhat.com>
8 *

--- 19 unchanged lines hidden (view full) ---

28#include "hw/vfio/vfio-common.h"
29#include "hw/vfio/vfio.h"
30#include "exec/address-spaces.h"
31#include "exec/memory.h"
32#include "hw/hw.h"
33#include "qemu/error-report.h"
34#include "qemu/main-loop.h"
35#include "qemu/range.h"
36#include "sysemu/balloon.h"
37#include "sysemu/kvm.h"
38#include "sysemu/reset.h"
39#include "trace.h"
40#include "qapi/error.h"
41
42VFIOGroupList vfio_group_list =
43 QLIST_HEAD_INITIALIZER(vfio_group_list);
44static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =

--- 1165 unchanged lines hidden (view full) ---

1210{
1211 VFIOContainer *container;
1212 int ret, fd;
1213 VFIOAddressSpace *space;
1214
1215 space = vfio_get_address_space(as);
1216
1217 /*
36#include "sysemu/kvm.h"
37#include "sysemu/reset.h"
38#include "trace.h"
39#include "qapi/error.h"
40
41VFIOGroupList vfio_group_list =
42 QLIST_HEAD_INITIALIZER(vfio_group_list);
43static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =

--- 1165 unchanged lines hidden (view full) ---

1209{
1210 VFIOContainer *container;
1211 int ret, fd;
1212 VFIOAddressSpace *space;
1213
1214 space = vfio_get_address_space(as);
1215
1216 /*
1218 * VFIO is currently incompatible with memory ballooning insofar as the
1217 * VFIO is currently incompatible with discarding of RAM insofar as the
1219 * madvise to purge (zap) the page from QEMU's address space does not
1220 * interact with the memory API and therefore leaves stale virtual to
1221 * physical mappings in the IOMMU if the page was previously pinned. We
1218 * madvise to purge (zap) the page from QEMU's address space does not
1219 * interact with the memory API and therefore leaves stale virtual to
1220 * physical mappings in the IOMMU if the page was previously pinned. We
1222 * therefore add a balloon inhibit for each group added to a container,
1221 * therefore set discarding broken for each group added to a container,
1223 * whether the container is used individually or shared. This provides
1224 * us with options to allow devices within a group to opt-in and allow
1222 * whether the container is used individually or shared. This provides
1223 * us with options to allow devices within a group to opt-in and allow
1225 * ballooning, so long as it is done consistently for a group (for instance
1224 * discarding, so long as it is done consistently for a group (for instance
1226 * if the device is an mdev device where it is known that the host vendor
1227 * driver will never pin pages outside of the working set of the guest
1225 * if the device is an mdev device where it is known that the host vendor
1226 * driver will never pin pages outside of the working set of the guest
1228 * driver, which would thus not be ballooning candidates).
1227 * driver, which would thus not be discarding candidates).
1229 *
1230 * The first opportunity to induce pinning occurs here where we attempt to
1231 * attach the group to existing containers within the AddressSpace. If any
1228 *
1229 * The first opportunity to induce pinning occurs here where we attempt to
1230 * attach the group to existing containers within the AddressSpace. If any
1232 * pages are already zapped from the virtual address space, such as from a
1233 * previous ballooning opt-in, new pinning will cause valid mappings to be
1231 * pages are already zapped from the virtual address space, such as from
1232 * previous discards, new pinning will cause valid mappings to be
1234 * re-established. Likewise, when the overall MemoryListener for a new
1235 * container is registered, a replay of mappings within the AddressSpace
1236 * will occur, re-establishing any previously zapped pages as well.
1237 *
1233 * re-established. Likewise, when the overall MemoryListener for a new
1234 * container is registered, a replay of mappings within the AddressSpace
1235 * will occur, re-establishing any previously zapped pages as well.
1236 *
1238 * NB. Balloon inhibiting does not currently block operation of the
1239 * balloon driver or revoke previously pinned pages, it only prevents
1240 * calling madvise to modify the virtual mapping of ballooned pages.
1237 * Especially virtio-balloon is currently only prevented from discarding
1238 * new memory, it will not yet set ram_block_discard_set_required() and
1239 * therefore, neither stops us here or deals with the sudden memory
1240 * consumption of inflated memory.
1241 */
1241 */
1242 qemu_balloon_inhibit(true);
1242 ret = ram_block_discard_disable(true);
1243 if (ret) {
1244 error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken");
1245 return ret;
1246 }
1243
1244 QLIST_FOREACH(container, &space->containers, next) {
1245 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
1246 group->container = container;
1247 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1248 vfio_kvm_device_add_group(group);
1249 return 0;
1250 }

--- 149 unchanged lines hidden (view full) ---

1400
1401free_container_exit:
1402 g_free(container);
1403
1404close_fd_exit:
1405 close(fd);
1406
1407put_space_exit:
1247
1248 QLIST_FOREACH(container, &space->containers, next) {
1249 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
1250 group->container = container;
1251 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1252 vfio_kvm_device_add_group(group);
1253 return 0;
1254 }

--- 149 unchanged lines hidden (view full) ---

1404
1405free_container_exit:
1406 g_free(container);
1407
1408close_fd_exit:
1409 close(fd);
1410
1411put_space_exit:
1408 qemu_balloon_inhibit(false);
1412 ram_block_discard_disable(false);
1409 vfio_put_address_space(space);
1410
1411 return ret;
1412}
1413
1414static void vfio_disconnect_container(VFIOGroup *group)
1415{
1416 VFIOContainer *container = group->container;

--- 104 unchanged lines hidden (view full) ---

1521}
1522
1523void vfio_put_group(VFIOGroup *group)
1524{
1525 if (!group || !QLIST_EMPTY(&group->device_list)) {
1526 return;
1527 }
1528
1413 vfio_put_address_space(space);
1414
1415 return ret;
1416}
1417
1418static void vfio_disconnect_container(VFIOGroup *group)
1419{
1420 VFIOContainer *container = group->container;

--- 104 unchanged lines hidden (view full) ---

1525}
1526
1527void vfio_put_group(VFIOGroup *group)
1528{
1529 if (!group || !QLIST_EMPTY(&group->device_list)) {
1530 return;
1531 }
1532
1529 if (!group->balloon_allowed) {
1530 qemu_balloon_inhibit(false);
1533 if (!group->ram_block_discard_allowed) {
1534 ram_block_discard_disable(false);
1531 }
1532 vfio_kvm_device_del_group(group);
1533 vfio_disconnect_container(group);
1534 QLIST_REMOVE(group, next);
1535 trace_vfio_put_group(group->fd);
1536 close(group->fd);
1537 g_free(group);
1538

--- 21 unchanged lines hidden (view full) ---

1560 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
1561 if (ret) {
1562 error_setg_errno(errp, errno, "error getting device info");
1563 close(fd);
1564 return ret;
1565 }
1566
1567 /*
1535 }
1536 vfio_kvm_device_del_group(group);
1537 vfio_disconnect_container(group);
1538 QLIST_REMOVE(group, next);
1539 trace_vfio_put_group(group->fd);
1540 close(group->fd);
1541 g_free(group);
1542

--- 21 unchanged lines hidden (view full) ---

1564 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
1565 if (ret) {
1566 error_setg_errno(errp, errno, "error getting device info");
1567 close(fd);
1568 return ret;
1569 }
1570
1571 /*
1568 * Clear the balloon inhibitor for this group if the driver knows the
1569 * device operates compatibly with ballooning. Setting must be consistent
1570 * per group, but since compatibility is really only possible with mdev
1571 * currently, we expect singleton groups.
1572 * Set discarding of RAM as not broken for this group if the driver knows
1573 * the device operates compatibly with discarding. Setting must be
1574 * consistent per group, but since compatibility is really only possible
1575 * with mdev currently, we expect singleton groups.
1572 */
1576 */
1573 if (vbasedev->balloon_allowed != group->balloon_allowed) {
1577 if (vbasedev->ram_block_discard_allowed !=
1578 group->ram_block_discard_allowed) {
1574 if (!QLIST_EMPTY(&group->device_list)) {
1579 if (!QLIST_EMPTY(&group->device_list)) {
1575 error_setg(errp,
1576 "Inconsistent device balloon setting within group");
1580 error_setg(errp, "Inconsistent setting of support for discarding "
1581 "RAM (e.g., balloon) within group");
1577 close(fd);
1578 return -1;
1579 }
1580
1582 close(fd);
1583 return -1;
1584 }
1585
1581 if (!group->balloon_allowed) {
1582 group->balloon_allowed = true;
1583 qemu_balloon_inhibit(false);
1586 if (!group->ram_block_discard_allowed) {
1587 group->ram_block_discard_allowed = true;
1588 ram_block_discard_disable(false);
1584 }
1585 }
1586
1587 vbasedev->fd = fd;
1588 vbasedev->group = group;
1589 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
1590
1591 vbasedev->num_irqs = dev_info.num_irqs;

--- 192 unchanged lines hidden ---
1589 }
1590 }
1591
1592 vbasedev->fd = fd;
1593 vbasedev->group = group;
1594 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
1595
1596 vbasedev->num_irqs = dev_info.num_irqs;

--- 192 unchanged lines hidden ---