qemu/hw/virtio/virtio-balloon.c
<<
>>
Prefs
   1/*
   2 * Virtio Balloon Device
   3 *
   4 * Copyright IBM, Corp. 2008
   5 * Copyright (C) 2011 Red Hat, Inc.
   6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
   7 *
   8 * Authors:
   9 *  Anthony Liguori   <aliguori@us.ibm.com>
  10 *
  11 * This work is licensed under the terms of the GNU GPL, version 2.  See
  12 * the COPYING file in the top-level directory.
  13 *
  14 */
  15
  16#include "qemu/osdep.h"
  17#include "qemu/iov.h"
  18#include "qemu/timer.h"
  19#include "qemu-common.h"
  20#include "hw/virtio/virtio.h"
  21#include "hw/i386/pc.h"
  22#include "sysemu/balloon.h"
  23#include "hw/virtio/virtio-balloon.h"
  24#include "sysemu/kvm.h"
  25#include "exec/address-spaces.h"
  26#include "qapi/visitor.h"
  27#include "qapi-event.h"
  28#include "trace.h"
  29
  30#if defined(__linux__)
  31#include <sys/mman.h>
  32#endif
  33
  34#include "hw/virtio/virtio-bus.h"
  35#include "hw/virtio/virtio-access.h"
  36
  37#define BALLOON_PAGE_SIZE  (1 << VIRTIO_BALLOON_PFN_SHIFT)
  38
  39static void balloon_page(void *addr, int deflate)
  40{
  41#if defined(__linux__)
  42    if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
  43                                         kvm_has_sync_mmu())) {
  44        qemu_madvise(addr, BALLOON_PAGE_SIZE,
  45                deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
  46    }
  47#endif
  48}
  49
  50static const char *balloon_stat_names[] = {
  51   [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
  52   [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
  53   [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
  54   [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
  55   [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
  56   [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
  57   [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
  58   [VIRTIO_BALLOON_S_NR] = NULL
  59};
  60
  61/*
  62 * reset_stats - Mark all items in the stats array as unset
  63 *
  64 * This function needs to be called at device initialization and before
  65 * updating to a set of newly-generated stats.  This will ensure that no
  66 * stale values stick around in case the guest reports a subset of the supported
  67 * statistics.
  68 */
  69static inline void reset_stats(VirtIOBalloon *dev)
  70{
  71    int i;
  72    for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
  73}
  74
  75static bool balloon_stats_supported(const VirtIOBalloon *s)
  76{
  77    VirtIODevice *vdev = VIRTIO_DEVICE(s);
  78    return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
  79}
  80
  81static bool balloon_stats_enabled(const VirtIOBalloon *s)
  82{
  83    return s->stats_poll_interval > 0;
  84}
  85
  86static void balloon_stats_destroy_timer(VirtIOBalloon *s)
  87{
  88    if (balloon_stats_enabled(s)) {
  89        timer_del(s->stats_timer);
  90        timer_free(s->stats_timer);
  91        s->stats_timer = NULL;
  92        s->stats_poll_interval = 0;
  93    }
  94}
  95
  96static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
  97{
  98    timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
  99}
 100
 101static void balloon_stats_poll_cb(void *opaque)
 102{
 103    VirtIOBalloon *s = opaque;
 104    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 105
 106    if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
 107        /* re-schedule */
 108        balloon_stats_change_timer(s, s->stats_poll_interval);
 109        return;
 110    }
 111
 112    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
 113    virtio_notify(vdev, s->svq);
 114    g_free(s->stats_vq_elem);
 115    s->stats_vq_elem = NULL;
 116}
 117
 118static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
 119                                  void *opaque, Error **errp)
 120{
 121    Error *err = NULL;
 122    VirtIOBalloon *s = opaque;
 123    int i;
 124
 125    visit_start_struct(v, name, NULL, 0, &err);
 126    if (err) {
 127        goto out;
 128    }
 129    visit_type_int(v, "last-update", &s->stats_last_update, &err);
 130    if (err) {
 131        goto out_end;
 132    }
 133
 134    visit_start_struct(v, "stats", NULL, 0, &err);
 135    if (err) {
 136        goto out_end;
 137    }
 138    for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
 139        visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
 140        if (err) {
 141            break;
 142        }
 143    }
 144    error_propagate(errp, err);
 145    err = NULL;
 146    visit_end_struct(v, &err);
 147
 148out_end:
 149    error_propagate(errp, err);
 150    err = NULL;
 151    visit_end_struct(v, &err);
 152out:
 153    error_propagate(errp, err);
 154}
 155
 156static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
 157                                            const char *name, void *opaque,
 158                                            Error **errp)
 159{
 160    VirtIOBalloon *s = opaque;
 161    visit_type_int(v, name, &s->stats_poll_interval, errp);
 162}
 163
 164static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
 165                                            const char *name, void *opaque,
 166                                            Error **errp)
 167{
 168    VirtIOBalloon *s = opaque;
 169    Error *local_err = NULL;
 170    int64_t value;
 171
 172    visit_type_int(v, name, &value, &local_err);
 173    if (local_err) {
 174        error_propagate(errp, local_err);
 175        return;
 176    }
 177
 178    if (value < 0) {
 179        error_setg(errp, "timer value must be greater than zero");
 180        return;
 181    }
 182
 183    if (value > UINT32_MAX) {
 184        error_setg(errp, "timer value is too big");
 185        return;
 186    }
 187
 188    if (value == s->stats_poll_interval) {
 189        return;
 190    }
 191
 192    if (value == 0) {
 193        /* timer=0 disables the timer */
 194        balloon_stats_destroy_timer(s);
 195        return;
 196    }
 197
 198    if (balloon_stats_enabled(s)) {
 199        /* timer interval change */
 200        s->stats_poll_interval = value;
 201        balloon_stats_change_timer(s, value);
 202        return;
 203    }
 204
 205    /* create a new timer */
 206    g_assert(s->stats_timer == NULL);
 207    s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
 208    s->stats_poll_interval = value;
 209    balloon_stats_change_timer(s, 0);
 210}
 211
 212static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 213{
 214    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
 215    VirtQueueElement *elem;
 216    MemoryRegionSection section;
 217
 218    for (;;) {
 219        size_t offset = 0;
 220        uint32_t pfn;
 221        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
 222        if (!elem) {
 223            return;
 224        }
 225
 226        while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
 227            ram_addr_t pa;
 228            ram_addr_t addr;
 229            int p = virtio_ldl_p(vdev, &pfn);
 230
 231            pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
 232            offset += 4;
 233
 234            /* FIXME: remove get_system_memory(), but how? */
 235            section = memory_region_find(get_system_memory(), pa, 1);
 236            if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
 237                continue;
 238
 239            trace_virtio_balloon_handle_output(memory_region_name(section.mr),
 240                                               pa);
 241            /* Using memory_region_get_ram_ptr is bending the rules a bit, but
 242               should be OK because we only want a single page.  */
 243            addr = section.offset_within_region;
 244            balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
 245                         !!(vq == s->dvq));
 246            memory_region_unref(section.mr);
 247        }
 248
 249        virtqueue_push(vq, elem, offset);
 250        virtio_notify(vdev, vq);
 251        g_free(elem);
 252    }
 253}
 254
 255static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
 256{
 257    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
 258    VirtQueueElement *elem;
 259    VirtIOBalloonStat stat;
 260    size_t offset = 0;
 261    qemu_timeval tv;
 262
 263    elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
 264    if (!elem) {
 265        goto out;
 266    }
 267
 268    if (s->stats_vq_elem != NULL) {
 269        /* This should never happen if the driver follows the spec. */
 270        virtqueue_push(vq, s->stats_vq_elem, 0);
 271        virtio_notify(vdev, vq);
 272        g_free(s->stats_vq_elem);
 273    }
 274
 275    s->stats_vq_elem = elem;
 276
 277    /* Initialize the stats to get rid of any stale values.  This is only
 278     * needed to handle the case where a guest supports fewer stats than it
 279     * used to (ie. it has booted into an old kernel).
 280     */
 281    reset_stats(s);
 282
 283    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
 284           == sizeof(stat)) {
 285        uint16_t tag = virtio_tswap16(vdev, stat.tag);
 286        uint64_t val = virtio_tswap64(vdev, stat.val);
 287
 288        offset += sizeof(stat);
 289        if (tag < VIRTIO_BALLOON_S_NR)
 290            s->stats[tag] = val;
 291    }
 292    s->stats_vq_offset = offset;
 293
 294    if (qemu_gettimeofday(&tv) < 0) {
 295        fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
 296        goto out;
 297    }
 298
 299    s->stats_last_update = tv.tv_sec;
 300
 301out:
 302    if (balloon_stats_enabled(s)) {
 303        balloon_stats_change_timer(s, s->stats_poll_interval);
 304    }
 305}
 306
 307static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
 308{
 309    VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
 310    struct virtio_balloon_config config;
 311
 312    config.num_pages = cpu_to_le32(dev->num_pages);
 313    config.actual = cpu_to_le32(dev->actual);
 314
 315    trace_virtio_balloon_get_config(config.num_pages, config.actual);
 316    memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
 317}
 318
 319static int build_dimm_list(Object *obj, void *opaque)
 320{
 321    GSList **list = opaque;
 322
 323    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
 324        DeviceState *dev = DEVICE(obj);
 325        if (dev->realized) { /* only realized DIMMs matter */
 326            *list = g_slist_prepend(*list, dev);
 327        }
 328    }
 329
 330    object_child_foreach(obj, build_dimm_list, opaque);
 331    return 0;
 332}
 333
 334static ram_addr_t get_current_ram_size(void)
 335{
 336    GSList *list = NULL, *item;
 337    ram_addr_t size = ram_size;
 338
 339    build_dimm_list(qdev_get_machine(), &list);
 340    for (item = list; item; item = g_slist_next(item)) {
 341        Object *obj = OBJECT(item->data);
 342        if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
 343            size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
 344                                            &error_abort);
 345        }
 346    }
 347    g_slist_free(list);
 348
 349    return size;
 350}
 351
 352static void virtio_balloon_set_config(VirtIODevice *vdev,
 353                                      const uint8_t *config_data)
 354{
 355    VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
 356    struct virtio_balloon_config config;
 357    uint32_t oldactual = dev->actual;
 358    ram_addr_t vm_ram_size = get_current_ram_size();
 359
 360    memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
 361    dev->actual = le32_to_cpu(config.actual);
 362    if (dev->actual != oldactual) {
 363        qapi_event_send_balloon_change(vm_ram_size -
 364                        ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
 365                        &error_abort);
 366    }
 367    trace_virtio_balloon_set_config(dev->actual, oldactual);
 368}
 369
 370static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
 371                                            Error **errp)
 372{
 373    VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
 374    f |= dev->host_features;
 375    virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
 376    return f;
 377}
 378
 379static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
 380{
 381    VirtIOBalloon *dev = opaque;
 382    info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
 383                                             VIRTIO_BALLOON_PFN_SHIFT);
 384}
 385
 386static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
 387{
 388    VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
 389    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 390    ram_addr_t vm_ram_size = get_current_ram_size();
 391
 392    if (target > vm_ram_size) {
 393        target = vm_ram_size;
 394    }
 395    if (target) {
 396        dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
 397        virtio_notify_config(vdev);
 398    }
 399    trace_virtio_balloon_to_target(target, dev->num_pages);
 400}
 401
 402static void virtio_balloon_save(QEMUFile *f, void *opaque)
 403{
 404    virtio_save(VIRTIO_DEVICE(opaque), f);
 405}
 406
 407static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
 408{
 409    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
 410
 411    qemu_put_be32(f, s->num_pages);
 412    qemu_put_be32(f, s->actual);
 413}
 414
 415static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
 416{
 417    if (version_id != 1)
 418        return -EINVAL;
 419
 420    return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
 421}
 422
 423static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
 424                                      int version_id)
 425{
 426    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
 427
 428    s->num_pages = qemu_get_be32(f);
 429    s->actual = qemu_get_be32(f);
 430
 431    if (balloon_stats_enabled(s)) {
 432        balloon_stats_change_timer(s, s->stats_poll_interval);
 433    }
 434    return 0;
 435}
 436
 437static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 438{
 439    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 440    VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 441    int ret;
 442
 443    virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
 444                sizeof(struct virtio_balloon_config));
 445
 446    ret = qemu_add_balloon_handler(virtio_balloon_to_target,
 447                                   virtio_balloon_stat, s);
 448
 449    if (ret < 0) {
 450        error_setg(errp, "Only one balloon device is supported");
 451        virtio_cleanup(vdev);
 452        return;
 453    }
 454
 455    s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
 456    s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
 457    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
 458
 459    reset_stats(s);
 460
 461    register_savevm(dev, "virtio-balloon", -1, 1,
 462                    virtio_balloon_save, virtio_balloon_load, s);
 463}
 464
 465static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
 466{
 467    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 468    VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 469
 470    balloon_stats_destroy_timer(s);
 471    qemu_remove_balloon_handler(s);
 472    unregister_savevm(dev, "virtio-balloon", s);
 473    virtio_cleanup(vdev);
 474}
 475
 476static void virtio_balloon_device_reset(VirtIODevice *vdev)
 477{
 478    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
 479
 480    if (s->stats_vq_elem != NULL) {
 481        g_free(s->stats_vq_elem);
 482        s->stats_vq_elem = NULL;
 483    }
 484}
 485
 486static void virtio_balloon_instance_init(Object *obj)
 487{
 488    VirtIOBalloon *s = VIRTIO_BALLOON(obj);
 489
 490    object_property_add(obj, "guest-stats", "guest statistics",
 491                        balloon_stats_get_all, NULL, NULL, s, NULL);
 492
 493    object_property_add(obj, "guest-stats-polling-interval", "int",
 494                        balloon_stats_get_poll_interval,
 495                        balloon_stats_set_poll_interval,
 496                        NULL, s, NULL);
 497}
 498
 499static Property virtio_balloon_properties[] = {
 500    DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
 501                    VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
 502    DEFINE_PROP_END_OF_LIST(),
 503};
 504
 505static void virtio_balloon_class_init(ObjectClass *klass, void *data)
 506{
 507    DeviceClass *dc = DEVICE_CLASS(klass);
 508    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
 509
 510    dc->props = virtio_balloon_properties;
 511    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 512    vdc->realize = virtio_balloon_device_realize;
 513    vdc->unrealize = virtio_balloon_device_unrealize;
 514    vdc->reset = virtio_balloon_device_reset;
 515    vdc->get_config = virtio_balloon_get_config;
 516    vdc->set_config = virtio_balloon_set_config;
 517    vdc->get_features = virtio_balloon_get_features;
 518    vdc->save = virtio_balloon_save_device;
 519    vdc->load = virtio_balloon_load_device;
 520}
 521
 522static const TypeInfo virtio_balloon_info = {
 523    .name = TYPE_VIRTIO_BALLOON,
 524    .parent = TYPE_VIRTIO_DEVICE,
 525    .instance_size = sizeof(VirtIOBalloon),
 526    .instance_init = virtio_balloon_instance_init,
 527    .class_init = virtio_balloon_class_init,
 528};
 529
 530static void virtio_register_types(void)
 531{
 532    type_register_static(&virtio_balloon_info);
 533}
 534
 535type_init(virtio_register_types)
 536