qemu_cgroup.c 38.4 KB
Newer Older
1 2 3
/*
 * qemu_cgroup.c: QEMU cgroup management
 *
4
 * Copyright (C) 2006-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "qemu_cgroup.h"
27
#include "qemu_domain.h"
28
#include "qemu_process.h"
29
#include "vircgroup.h"
30
#include "virlog.h"
31
#include "viralloc.h"
32
#include "virerror.h"
33
#include "domain_audit.h"
34
#include "virscsi.h"
35
#include "virstring.h"
36
#include "virfile.h"
37
#include "virtypedparam.h"
38
#include "virnuma.h"
39
#include "virsystemd.h"
40 41 42

#define VIR_FROM_THIS VIR_FROM_QEMU

43 44
VIR_LOG_INIT("qemu.qemu_cgroup");

45
const char *const defaultDeviceACL[] = {
46 47 48
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
49
    "/dev/rtc", "/dev/hpet", "/dev/vfio/vfio",
50 51 52 53 54
    NULL,
};
#define DEVICE_PTY_MAJOR 136
#define DEVICE_SND_MAJOR 116

55

56
static int
57 58 59
qemuSetupImagePathCgroup(virDomainObjPtr vm,
                         const char *path,
                         bool readonly)
60
{
61
    qemuDomainObjPrivatePtr priv = vm->privateData;
62
    int perms = VIR_CGROUP_DEVICE_READ;
63
    int ret;
64

65
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
66 67
        return 0;

68
    if (!readonly)
69
        perms |= VIR_CGROUP_DEVICE_WRITE;
70

71
    VIR_DEBUG("Allow path %s, perms: %s",
72
              path, virCgroupGetDevicePermsString(perms));
73

74
    ret = virCgroupAllowDevicePath(priv->cgroup, path, perms, true);
75

76
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
77 78
                             virCgroupGetDevicePermsString(perms),
                             ret == 0);
79 80

    return ret;
81 82 83
}


84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static int
qemuSetupImageCgroupInternal(virDomainObjPtr vm,
                             virStorageSourcePtr src,
                             bool forceReadonly)
{
    if (!src->path || !virStorageSourceIsLocalStorage(src)) {
        VIR_DEBUG("Not updating cgroups for disk path '%s', type: %s",
                  NULLSTR(src->path), virStorageTypeToString(src->type));
        return 0;
    }

    return qemuSetupImagePathCgroup(vm, src->path, src->readonly || forceReadonly);
}


99
int
100 101
qemuSetupImageCgroup(virDomainObjPtr vm,
                     virStorageSourcePtr src)
102
{
103
    return qemuSetupImageCgroupInternal(vm, src, false);
104 105 106 107 108 109 110
}


int
qemuTeardownImageCgroup(virDomainObjPtr vm,
                        virStorageSourcePtr src)
{
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int perms = VIR_CGROUP_DEVICE_READ |
                VIR_CGROUP_DEVICE_WRITE |
                VIR_CGROUP_DEVICE_MKNOD;
    int ret;

    if (!virCgroupHasController(priv->cgroup,
                                VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    if (!src->path || !virStorageSourceIsLocalStorage(src)) {
        VIR_DEBUG("Not updating cgroups for disk path '%s', type: %s",
                  NULLSTR(src->path), virStorageTypeToString(src->type));
        return 0;
    }

    VIR_DEBUG("Deny path %s", src->path);

    ret = virCgroupDenyDevicePath(priv->cgroup, src->path, perms, true);

    virDomainAuditCgroupPath(vm, priv->cgroup, "deny", src->path,
                             virCgroupGetDevicePermsString(perms), ret == 0);

    return ret;
135 136 137
}


138 139 140
int
qemuSetupDiskCgroup(virDomainObjPtr vm,
                    virDomainDiskDefPtr disk)
141
{
142
    virStorageSourcePtr next;
143
    bool forceReadonly = false;
144

145
    for (next = disk->src; next; next = next->backingStore) {
146
        if (qemuSetupImageCgroupInternal(vm, next, forceReadonly) < 0)
147
            return -1;
148 149 150

        /* setup only the top level image for read-write */
        forceReadonly = true;
151
    }
152 153

    return 0;
154 155 156
}


157 158 159
int
qemuTeardownDiskCgroup(virDomainObjPtr vm,
                       virDomainDiskDefPtr disk)
160
{
161
    virStorageSourcePtr next;
162

163
    for (next = disk->src; next; next = next->backingStore) {
164
        if (qemuTeardownImageCgroup(vm, next) < 0)
165 166
            return -1;
    }
167

168
    return 0;
169 170
}

171

172
static int
173
qemuSetupChrSourceCgroup(virDomainObjPtr vm,
174
                         virDomainChrSourceDefPtr source)
175
{
176
    qemuDomainObjPrivatePtr priv = vm->privateData;
177
    int ret;
178

179
    if (source->type != VIR_DOMAIN_CHR_TYPE_DEV)
180 181
        return 0;

182
    VIR_DEBUG("Process path '%s' for device", source->data.file.path);
183

184
    ret = virCgroupAllowDevicePath(priv->cgroup, source->data.file.path,
185
                                   VIR_CGROUP_DEVICE_RW, false);
186
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
187
                             source->data.file.path, "rw", ret == 0);
188

189
    return ret;
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

static int
qemuTeardownChrSourceCgroup(virDomainObjPtr vm,
                            virDomainChrSourceDefPtr source)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret;

    if (source->type != VIR_DOMAIN_CHR_TYPE_DEV)
        return 0;

    VIR_DEBUG("Process path '%s' for device", source->data.file.path);

    ret = virCgroupDenyDevicePath(priv->cgroup, source->data.file.path,
                                  VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
                             source->data.file.path, "rw", ret == 0);

    return ret;
}


214
static int
215 216 217
qemuSetupChardevCgroupCB(virDomainDefPtr def ATTRIBUTE_UNUSED,
                         virDomainChrDefPtr dev,
                         void *opaque)
218
{
219 220
    virDomainObjPtr vm = opaque;

221
    return qemuSetupChrSourceCgroup(vm, dev->source);
222 223 224 225
}


static int
226
qemuSetupTPMCgroup(virDomainObjPtr vm)
227
{
228
    int ret = 0;
229
    virDomainTPMDefPtr dev = vm->def->tpm;
230 231 232

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
233
        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
234 235 236 237 238
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

239
    return ret;
240 241
}

242

243 244 245 246 247 248 249 250 251 252 253
static int
qemuSetupInputCgroup(virDomainObjPtr vm,
                     virDomainInputDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = 0;

    switch (dev->type) {
    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
        VIR_DEBUG("Process path '%s' for input device", dev->source.evdev);
        ret = virCgroupAllowDevicePath(priv->cgroup, dev->source.evdev,
254
                                       VIR_CGROUP_DEVICE_RW, false);
255 256 257 258 259 260 261 262
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", dev->source.evdev, "rw", ret == 0);
        break;
    }

    return ret;
}


263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int
qemuSetupHostSCSIVHostDeviceCgroup(virSCSIVHostDevicePtr dev ATTRIBUTE_UNUSED,
                                   const char *path,
                                   void *opaque)
{
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret;

    VIR_DEBUG("Process path '%s' for scsi_host device", path);

    ret = virCgroupAllowDevicePath(priv->cgroup, path,
                                   VIR_CGROUP_DEVICE_RW, false);

    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path, "rw", ret == 0);

    return ret;
}

282
int
283
qemuSetupHostdevCgroup(virDomainObjPtr vm,
284 285 286 287
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
288
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
289
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
290
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
291
    virDomainHostdevSubsysSCSIVHostPtr hostsrc = &dev->source.subsys.u.scsi_host;
292
    virPCIDevicePtr pci = NULL;
293
    virUSBDevicePtr usb = NULL;
294
    virSCSIDevicePtr scsi = NULL;
295
    virSCSIVHostDevicePtr host = NULL;
296
    char *path = NULL;
297
    int rv;
298 299 300 301 302 303 304 305 306 307 308

    /* currently this only does something for PCI devices using vfio
     * for device assignment, but it is called for *all* hostdev
     * devices.
     */

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {

309
        switch ((virDomainHostdevSubsysType) dev->source.subsys.type) {
310
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
311 312 313 314 315
            if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
                pci = virPCIDeviceNew(pcisrc->addr.domain,
                                      pcisrc->addr.bus,
                                      pcisrc->addr.slot,
                                      pcisrc->addr.function);
316 317 318
                if (!pci)
                    goto cleanup;

319
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
320 321 322
                    goto cleanup;

                VIR_DEBUG("Cgroup allow %s for PCI device assignment", path);
323
                rv = virCgroupAllowDevicePath(priv->cgroup, path,
324
                                              VIR_CGROUP_DEVICE_RW, false);
325
                virDomainAuditCgroupPath(vm, priv->cgroup,
326 327
                                         "allow", path, "rw", rv == 0);
                if (rv < 0)
328 329 330
                    goto cleanup;
            }
            break;
331 332 333 334 335 336 337 338

        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
            /* NB: hostdev->missing wasn't previously checked in the
             * case of hotplug, only when starting a domain. Now it is
             * always checked, and the cgroup setup skipped if true.
             */
            if (dev->missing)
                break;
339
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
340 341 342 343
                                       NULL)) == NULL) {
                goto cleanup;
            }

344 345 346 347 348 349 350 351
            if (VIR_STRDUP(path, virUSBDeviceGetPath(usb)) < 0)
                goto cleanup;

            VIR_DEBUG("Process path '%s' for USB device", path);
            rv = virCgroupAllowDevicePath(priv->cgroup, path,
                                          VIR_CGROUP_DEVICE_RW, false);
            virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path, "rw", rv == 0);
            if (rv < 0)
352 353
                goto cleanup;
            break;
354

355
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            if (scsisrc->protocol ==
                VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
                virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &scsisrc->u.iscsi;
                /* Follow qemuSetupDiskCgroup() and qemuSetImageCgroupInternal()
                 * which does nothing for non local storage
                 */
                VIR_DEBUG("Not updating cgroups for hostdev iSCSI path '%s'",
                          iscsisrc->path);
            } else {
                virDomainHostdevSubsysSCSIHostPtr scsihostsrc =
                    &scsisrc->u.host;
                if ((scsi = virSCSIDeviceNew(NULL,
                                             scsihostsrc->adapter,
                                             scsihostsrc->bus,
                                             scsihostsrc->target,
                                             scsihostsrc->unit,
                                             dev->readonly,
                                             dev->shareable)) == NULL)
                    goto cleanup;
375

376 377 378 379 380 381 382 383 384 385 386 387 388
                if (VIR_STRDUP(path, virSCSIDeviceGetPath(scsi)) < 0)
                    goto cleanup;

                VIR_DEBUG("Process path '%s' for SCSI device", path);
                rv = virCgroupAllowDevicePath(priv->cgroup, path,
                                              virSCSIDeviceGetReadonly(scsi) ?
                                              VIR_CGROUP_DEVICE_READ :
                                              VIR_CGROUP_DEVICE_RW, false);

                virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
                                         virSCSIDeviceGetReadonly(scsi) ? "r" : "rw",
                                         rv == 0);
                if (rv < 0)
389 390
                    goto cleanup;
            }
391 392
            break;
        }
393

394
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
395 396 397 398 399 400 401 402 403 404
            if (hostsrc->protocol ==
                VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST) {
                if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn)))
                    goto cleanup;

                if (virSCSIVHostDeviceFileIterate(host,
                                             qemuSetupHostSCSIVHostDeviceCgroup,
                                             vm) < 0)
                    goto cleanup;
            }
405 406 407
            break;
        }

408
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
409 410 411 412 413
            break;
        }
    }

    ret = 0;
414
 cleanup:
415
    virPCIDeviceFree(pci);
416
    virUSBDeviceFree(usb);
417
    virSCSIDeviceFree(scsi);
418
    virSCSIVHostDeviceFree(host);
419 420 421 422 423 424 425 426 427 428
    VIR_FREE(path);
    return ret;
}

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
429
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
430 431 432 433 434 435 436 437 438 439 440 441 442
    virPCIDevicePtr pci = NULL;
    char *path = NULL;

    /* currently this only does something for PCI devices using vfio
     * for device assignment, but it is called for *all* hostdev
     * devices.
     */

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {

443
        switch ((virDomainHostdevSubsysType) dev->source.subsys.type) {
444
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
445
            if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
446
                int rv;
447

448 449 450 451
                pci = virPCIDeviceNew(pcisrc->addr.domain,
                                      pcisrc->addr.bus,
                                      pcisrc->addr.slot,
                                      pcisrc->addr.function);
452 453 454
                if (!pci)
                    goto cleanup;

455
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
456 457 458
                    goto cleanup;

                VIR_DEBUG("Cgroup deny %s for PCI device assignment", path);
459
                rv = virCgroupDenyDevicePath(priv->cgroup, path,
460
                                             VIR_CGROUP_DEVICE_RWM, false);
461
                virDomainAuditCgroupPath(vm, priv->cgroup,
462 463
                                         "deny", path, "rwm", rv == 0);
                if (rv < 0)
464 465 466
                    goto cleanup;
            }
            break;
467 468 469
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
            /* nothing to tear down for USB */
            break;
470 471 472
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
            /* nothing to tear down for SCSI */
            break;
473 474 475
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
            /* nothing to tear down for scsi_host */
            break;
476
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
477 478 479 480 481
            break;
        }
    }

    ret = 0;
482
 cleanup:
483 484 485 486 487
    virPCIDeviceFree(pci);
    VIR_FREE(path);
    return ret;
}

488 489 490 491
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
492
    size_t i;
493 494 495 496 497 498 499 500 501 502 503 504

    if (!virCgroupHasController(priv->cgroup,
                                VIR_CGROUP_CONTROLLER_BLKIO)) {
        if (vm->def->blkio.weight || vm->def->blkio.ndevices) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Block I/O tuning is not available on this host"));
            return -1;
        } else {
            return 0;
        }
    }

505 506 507
    if (vm->def->blkio.weight != 0 &&
        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
        return -1;
508 509 510

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
511
            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
512 513
            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
514 515 516
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
                                               &dev->weight) < 0))
517 518 519 520
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
521 522 523
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
                                                 &dev->riops) < 0))
524 525 526 527
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
528 529 530
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
                                                  &dev->wiops) < 0))
531 532 533 534
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
535 536 537
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
                                                &dev->rbps) < 0))
538 539 540 541
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
542 543 544
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
                                                 &dev->wbps) < 0))
545 546 547 548 549 550 551
                return -1;
        }
    }

    return 0;
}

552

553 554 555 556 557
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
558
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
559 560 561
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
562 563 564
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
565 566
        } else {
            return 0;
567 568 569
        }
    }

570 571 572
    if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0)
            return -1;
573

574 575 576
    if (virMemoryLimitIsSet(vm->def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0)
            return -1;
577

578 579 580
    if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0)
            return -1;
581 582 583 584 585

    return 0;
}


586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
static int
qemuSetupFirmwareCgroup(virDomainObjPtr vm)
{
    if (!vm->def->os.loader)
        return 0;

    if (vm->def->os.loader->path &&
        qemuSetupImagePathCgroup(vm, vm->def->os.loader->path,
                                 vm->def->os.loader->readonly == VIR_TRISTATE_BOOL_YES) < 0)
        return -1;

    if (vm->def->os.loader->nvram &&
        qemuSetupImagePathCgroup(vm, vm->def->os.loader->nvram, false) < 0)
        return -1;

    return 0;
}


605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
int
qemuSetupRNGCgroup(virDomainObjPtr vm,
                   virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

    if (rng->backend == VIR_DOMAIN_RNG_BACKEND_RANDOM) {
        VIR_DEBUG("Setting Cgroup ACL for RNG device");
        rv = virCgroupAllowDevicePath(priv->cgroup,
                                      rng->source.file,
                                      VIR_CGROUP_DEVICE_RW, false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
                                 rng->source.file,
                                 "rw", rv == 0);
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


int
qemuTeardownRNGCgroup(virDomainObjPtr vm,
                      virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

    if (rng->backend == VIR_DOMAIN_RNG_BACKEND_RANDOM) {
        VIR_DEBUG("Tearing down Cgroup ACL for RNG device");
        rv = virCgroupDenyDevicePath(priv->cgroup,
                                     rng->source.file,
                                     VIR_CGROUP_DEVICE_RW, false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
                                 rng->source.file,
                                 "rw", rv == 0);
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
int
qemuSetupChardevCgroup(virDomainObjPtr vm,
                       virDomainChrDefPtr dev)
{
    return qemuSetupChrSourceCgroup(vm, dev->source);
}


int
qemuTeardownChardevCgroup(virDomainObjPtr vm,
                          virDomainChrDefPtr dev)
{
    return qemuTeardownChrSourceCgroup(vm, dev->source);
}


669 670 671 672 673 674 675
static int
qemuSetupDevicesCgroup(virQEMUDriverPtr driver,
                       virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
676
    int rv = -1;
677
    int ret = -1;
678
    size_t i;
679 680 681 682

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
        return 0;

683 684 685 686 687
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
688 689 690 691 692 693 694
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

695 696 697
    if (qemuSetupFirmwareCgroup(vm) < 0)
        goto cleanup;

698
    for (i = 0; i < vm->def->ndisks; i++) {
699 700 701 702
        if (qemuSetupDiskCgroup(vm, vm->def->disks[i]) < 0)
            goto cleanup;
    }

703 704
    rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_PTY_MAJOR, -1,
                              VIR_CGROUP_DEVICE_RW);
705
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
706 707
                              "pty", "rw", rv == 0);
    if (rv < 0)
708 709 710 711 712 713 714 715
        goto cleanup;

    cfg = virQEMUDriverGetConfig(driver);
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
716
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
717 718
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
719
           cfg->vncAllowHostAudio) ||
720
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
721 722
        rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_SND_MAJOR, -1,
                                  VIR_CGROUP_DEVICE_RW);
723
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
724 725
                                  "sound", "rw", rv == 0);
        if (rv < 0)
726 727 728
            goto cleanup;
    }

729
    for (i = 0; deviceACL[i] != NULL; i++) {
730
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
731
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
732 733 734
            continue;
        }

735
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
736
                                      VIR_CGROUP_DEVICE_RW, false);
737 738 739
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv == 0);
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
740 741 742 743 744
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
745
                               qemuSetupChardevCgroupCB,
746 747 748
                               vm) < 0)
        goto cleanup;

749
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
750 751 752
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
753
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
754 755 756
            goto cleanup;
    }

757 758 759 760 761
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

762
    for (i = 0; i < vm->def->nrngs; i++) {
763 764
        if (qemuSetupRNGCgroup(vm, vm->def->rngs[i]) < 0)
            goto cleanup;
765 766
    }

767
    ret = 0;
768
 cleanup:
769 770 771 772 773
    virObjectUnref(cfg);
    return ret;
}


774
int
775
qemuSetupCpusetMems(virDomainObjPtr vm)
776
{
777
    virCgroupPtr cgroup_temp = NULL;
778
    qemuDomainObjPrivatePtr priv = vm->privateData;
779
    virDomainNumatuneMemMode mode;
780
    char *mem_mask = NULL;
781 782 783 784 785
    int ret = -1;

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;

786 787
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mode) < 0 ||
        mode != VIR_DOMAIN_NUMATUNE_MEM_STRICT)
788 789
        return 0;

790
    if (virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
791
                                            priv->autoNodeset,
792
                                            &mem_mask, -1) < 0)
793
        goto cleanup;
794

795
    if (mem_mask)
J
John Ferlan 已提交
796 797
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                               false, &cgroup_temp) < 0 ||
798
            virCgroupSetCpusetMems(cgroup_temp, mem_mask) < 0)
799
            goto cleanup;
800

801 802 803
    ret = 0;
 cleanup:
    VIR_FREE(mem_mask);
804
    virCgroupFree(&cgroup_temp);
805 806 807 808 809
    return ret;
}


static int
810
qemuSetupCpusetCgroup(virDomainObjPtr vm)
811 812 813 814 815 816
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;

817 818 819
    if (virCgroupSetCpusetMemoryMigrate(priv->cgroup, true) < 0)
        return -1;

820
    return 0;
821 822 823
}


824
static int
825 826
qemuSetupCpuCgroup(virQEMUDriverPtr driver,
                   virDomainObjPtr vm)
827 828
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
829 830 831 832
    virObjectEventPtr event = NULL;
    virTypedParameterPtr eventParams = NULL;
    int eventNparams = 0;
    int eventMaxparams = 0;
833 834

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
835
       if (vm->def->cputune.sharesSpecified) {
836 837 838 839 840 841 842 843
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

844 845 846 847 848 849 850
    if (vm->def->cputune.sharesSpecified) {
        unsigned long long val;
        if (virCgroupSetCpuShares(priv->cgroup, vm->def->cputune.shares) < 0)
            return -1;

        if (virCgroupGetCpuShares(priv->cgroup, &val) < 0)
            return -1;
851 852 853 854
        if (vm->def->cputune.shares != val) {
            vm->def->cputune.shares = val;
            if (virTypedParamsAddULLong(&eventParams, &eventNparams,
                                        &eventMaxparams,
855
                                        VIR_DOMAIN_TUNABLE_CPU_CPU_SHARES,
856 857 858 859 860 861
                                        val) < 0)
                return -1;

            event = virDomainEventTunableNewFromObj(vm, eventParams, eventNparams);
        }

862
        qemuDomainEventQueue(driver, event);
863
    }
864 865 866 867 868

    return 0;
}


869
static int
870
qemuInitCgroup(virQEMUDriverPtr driver,
871 872 873
               virDomainObjPtr vm,
               size_t nnicindexes,
               int *nicindexes)
874
{
875
    int ret = -1;
876 877 878
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);

879
    if (!virQEMUDriverIsPrivileged(driver))
880 881
        goto done;

882 883 884
    if (!virCgroupAvailable())
        goto done;

885 886
    virCgroupFree(&priv->cgroup);

887
    if (!vm->def->resource) {
888 889
        virDomainResourceDefPtr res;

890
        if (VIR_ALLOC(res) < 0)
891
            goto cleanup;
892

893
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
894 895 896 897 898
            VIR_FREE(res);
            goto cleanup;
        }

        vm->def->resource = res;
899 900
    }

901 902 903 904 905 906
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
907

908 909 910 911 912 913 914 915 916 917 918 919
    /*
     * We need to do this because of systemd-machined, because
     * CreateMachine requires the name to be a valid hostname.
     */
    priv->machineName = virSystemdMakeMachineName("qemu",
                                                  vm->def->id,
                                                  vm->def->name,
                                                  virQEMUDriverIsPrivileged(driver));
    if (!priv->machineName)
        goto cleanup;

    if (virCgroupNewMachine(priv->machineName,
920 921 922 923 924
                            "qemu",
                            vm->def->uuid,
                            NULL,
                            vm->pid,
                            false,
925
                            nnicindexes, nicindexes,
926 927 928
                            vm->def->resource->partition,
                            cfg->cgroupControllers,
                            &priv->cgroup) < 0) {
929 930
        if (virCgroupNewIgnoreError())
            goto done;
931

932 933
        goto cleanup;
    }
934

935
 done:
936
    ret = 0;
937
 cleanup:
938 939 940
    virObjectUnref(cfg);
    return ret;
}
941

942 943 944
static void
qemuRestoreCgroupState(virDomainObjPtr vm)
{
945
    char *mem_mask = NULL;
946
    char *nodeset = NULL;
947 948
    int empty = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
949
    size_t i = 0;
950
    virBitmapPtr all_nodes;
951
    virCgroupPtr cgroup_temp = NULL;
952

953 954 955 956
    if (!virNumaIsAvailable() ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return;

957
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
958 959 960 961 962 963
        goto error;

    if (!(mem_mask = virBitmapFormat(all_nodes)))
        goto error;

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
964
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
965 966 967 968 969
        goto error;

    if (virCgroupSetCpusetMems(priv->cgroup, mem_mask) < 0)
        goto error;

970
    for (i = 0; i < virDomainDefGetVcpusMax(vm->def); i++) {
971
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(vm->def, i);
972 973 974 975

        if (!vcpu->online)
            continue;

J
John Ferlan 已提交
976 977
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               false, &cgroup_temp) < 0 ||
978 979 980 981 982
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

983
        VIR_FREE(nodeset);
984 985 986
        virCgroupFree(&cgroup_temp);
    }

987 988 989
    for (i = 0; i < vm->def->niothreadids; i++) {
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_IOTHREAD,
                               vm->def->iothreadids[i]->iothread_id,
J
John Ferlan 已提交
990
                               false, &cgroup_temp) < 0 ||
991 992 993 994 995
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

996
        VIR_FREE(nodeset);
997 998 999
        virCgroupFree(&cgroup_temp);
    }

J
John Ferlan 已提交
1000 1001
    if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &cgroup_temp) < 0 ||
1002 1003 1004 1005 1006
        virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
        virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
        virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
        goto cleanup;

1007 1008
 cleanup:
    VIR_FREE(mem_mask);
1009
    VIR_FREE(nodeset);
1010
    virBitmapFree(all_nodes);
1011
    virCgroupFree(&cgroup_temp);
1012 1013 1014 1015 1016 1017 1018
    return;

 error:
    virResetLastError();
    VIR_DEBUG("Couldn't restore cgroups to meaningful state");
    goto cleanup;
}
1019 1020 1021 1022 1023 1024 1025 1026 1027

int
qemuConnectCgroup(virQEMUDriverPtr driver,
                  virDomainObjPtr vm)
{
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = -1;

1028
    if (!virQEMUDriverIsPrivileged(driver))
1029 1030 1031 1032 1033 1034 1035
        goto done;

    if (!virCgroupAvailable())
        goto done;

    virCgroupFree(&priv->cgroup);

1036 1037
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
1038 1039
                                  vm->def->id,
                                  virQEMUDriverIsPrivileged(driver),
1040
                                  vm->pid,
1041
                                  cfg->cgroupControllers,
1042
                                  &priv->cgroup) < 0)
1043
        goto cleanup;
1044

1045 1046 1047 1048
    priv->machineName = virSystemdGetMachineNameByPID(vm->pid);
    if (!priv->machineName)
        virResetLastError();

1049 1050
    qemuRestoreCgroupState(vm);

1051
 done:
1052
    ret = 0;
1053
 cleanup:
1054
    virObjectUnref(cfg);
1055
    return ret;
1056 1057
}

1058 1059
int
qemuSetupCgroup(virQEMUDriverPtr driver,
1060 1061 1062
                virDomainObjPtr vm,
                size_t nnicindexes,
                int *nicindexes)
1063
{
1064
    qemuDomainObjPrivatePtr priv = vm->privateData;
1065
    int ret = -1;
1066

1067 1068 1069 1070 1071 1072
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

1073
    if (qemuInitCgroup(driver, vm, nnicindexes, nicindexes) < 0)
1074
        return -1;
1075

1076
    if (!priv->cgroup)
1077
        return 0;
1078

1079 1080
    if (qemuSetupDevicesCgroup(driver, vm) < 0)
        goto cleanup;
1081

1082 1083
    if (qemuSetupBlkioCgroup(vm) < 0)
        goto cleanup;
1084

1085 1086
    if (qemuSetupMemoryCgroup(vm) < 0)
        goto cleanup;
1087

1088
    if (qemuSetupCpuCgroup(driver, vm) < 0)
1089
        goto cleanup;
1090

1091
    if (qemuSetupCpusetCgroup(vm) < 0)
1092
        goto cleanup;
1093

1094
    ret = 0;
1095
 cleanup:
1096
    return ret;
1097 1098
}

1099 1100 1101 1102
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
1103 1104 1105 1106 1107 1108 1109 1110
{
    unsigned long long old_period;

    if (period == 0 && quota == 0)
        return 0;

    if (period) {
        /* get old period, and we can rollback if set quota failed */
1111
        if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
1112 1113
            return -1;

1114
        if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
1115 1116 1117
            return -1;
    }

1118 1119 1120
    if (quota &&
        virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
        goto error;
1121 1122 1123

    return 0;

1124
 error:
1125
    if (period) {
1126 1127 1128 1129 1130 1131
        virErrorPtr saved = virSaveLastError();
        ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
        if (saved) {
            virSetError(saved);
            virFreeError(saved);
        }
1132 1133 1134 1135 1136
    }

    return -1;
}

1137

1138
int
1139 1140
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
1141
{
1142
    int ret = -1;
1143 1144
    char *new_cpus = NULL;

1145
    if (!(new_cpus = virBitmapFormat(cpumask)))
1146 1147
        goto cleanup;

1148
    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
1149 1150
        goto cleanup;

1151
    ret = 0;
1152
 cleanup:
1153
    VIR_FREE(new_cpus);
1154
    return ret;
1155 1156
}

1157

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
int
qemuSetupGlobalCpuCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    unsigned long long period = vm->def->cputune.global_period;
    long long quota = vm->def->cputune.global_quota;
    char *mem_mask = NULL;
    virDomainNumatuneMemMode mem_mode;

    if ((period || quota) &&
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("cgroup cpu is required for scheduler tuning"));
        return -1;
    }

    /*
     * If CPU cgroup controller is not initialized here, then we need
     * neither period nor quota settings.  And if CPUSET controller is
     * not initialized either, then there's nothing to do anyway.
     */
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU) &&
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;


    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mem_mode) == 0 &&
        mem_mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT &&
        virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
                                            priv->autoNodeset,
                                            &mem_mask, -1) < 0)
        goto cleanup;

    if (period || quota) {
        if (qemuSetupCgroupVcpuBW(priv->cgroup, period, quota) < 0)
            goto cleanup;
    }

    VIR_FREE(mem_mask);

    return 0;

 cleanup:
    VIR_FREE(mem_mask);

    return -1;
}


1207
int
1208
qemuRemoveCgroup(virDomainObjPtr vm)
1209
{
1210
    qemuDomainObjPrivatePtr priv = vm->privateData;
1211

1212
    if (priv->cgroup == NULL)
1213 1214
        return 0; /* Not supported, so claim success */

1215
    if (virCgroupTerminateMachine(priv->machineName) < 0) {
1216 1217 1218 1219
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

M
Martin Kletzander 已提交
1220 1221
    VIR_FREE(priv->machineName);

1222
    return virCgroupRemove(priv->cgroup);
1223
}
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245


static void
qemuCgroupEmulatorAllNodesDataFree(qemuCgroupEmulatorAllNodesDataPtr data)
{
    if (!data)
        return;

    virCgroupFree(&data->emulatorCgroup);
    VIR_FREE(data->emulatorMemMask);
    VIR_FREE(data);
}


/**
 * qemuCgroupEmulatorAllNodesAllow:
 * @cgroup: domain cgroup pointer
 * @retData: filled with structure used to roll back the operation
 *
 * Allows all NUMA nodes for the qemu emulator thread temporarily. This is
 * necessary when hotplugging cpus since it requires memory allocated in the
 * DMA region. Afterwards the operation can be reverted by
1246
 * qemuCgroupEmulatorAllNodesRestore.
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
 *
 * Returns 0 on success -1 on error
 */
int
qemuCgroupEmulatorAllNodesAllow(virCgroupPtr cgroup,
                                qemuCgroupEmulatorAllNodesDataPtr *retData)
{
    qemuCgroupEmulatorAllNodesDataPtr data = NULL;
    char *all_nodes_str = NULL;
    virBitmapPtr all_nodes = NULL;
    int ret = -1;

    if (!virNumaIsAvailable() ||
        !virCgroupHasController(cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;

1263
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
        goto cleanup;

    if (!(all_nodes_str = virBitmapFormat(all_nodes)))
        goto cleanup;

    if (VIR_ALLOC(data) < 0)
        goto cleanup;

    if (virCgroupNewThread(cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &data->emulatorCgroup) < 0)
        goto cleanup;

    if (virCgroupGetCpusetMems(data->emulatorCgroup, &data->emulatorMemMask) < 0 ||
        virCgroupSetCpusetMems(data->emulatorCgroup, all_nodes_str) < 0)
        goto cleanup;

    VIR_STEAL_PTR(*retData, data);
    ret = 0;

 cleanup:
    VIR_FREE(all_nodes_str);
    virBitmapFree(all_nodes);
    qemuCgroupEmulatorAllNodesDataFree(data);

    return ret;
}


/**
1293
 * qemuCgroupEmulatorAllNodesRestore:
1294 1295 1296 1297 1298 1299
 * @data: data structure created by qemuCgroupEmulatorAllNodesAllow
 *
 * Rolls back the setting done by qemuCgroupEmulatorAllNodesAllow and frees the
 * associated data.
 */
void
1300
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
{
    virErrorPtr err;

    if (!data)
        return;

    err = virSaveLastError();
    virCgroupSetCpusetMems(data->emulatorCgroup, data->emulatorMemMask);
    virSetError(err);
    virFreeError(err);

    qemuCgroupEmulatorAllNodesDataFree(data);
}