qemu_cgroup.c 38.7 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 40 41

#define VIR_FROM_THIS VIR_FROM_QEMU

42 43
VIR_LOG_INIT("qemu.qemu_cgroup");

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

54 55 56 57 58
static int
qemuSetImageCgroupInternal(virDomainObjPtr vm,
                           virStorageSourcePtr src,
                           bool deny,
                           bool forceReadonly)
59
{
60
    qemuDomainObjPrivatePtr priv = vm->privateData;
61
    int perms = VIR_CGROUP_DEVICE_READ;
62
    int ret;
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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;
    }

    if (deny) {
        perms |= VIR_CGROUP_DEVICE_WRITE | VIR_CGROUP_DEVICE_MKNOD;

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

        ret = virCgroupDenyDevicePath(priv->cgroup, src->path, perms);
    } else {
81
        if (!src->readonly && !forceReadonly)
82 83 84 85 86 87 88 89 90 91 92 93 94
            perms |= VIR_CGROUP_DEVICE_WRITE;

        VIR_DEBUG("Allow path %s, perms: %s",
                  src->path, virCgroupGetDevicePermsString(perms));

        ret = virCgroupAllowDevicePath(priv->cgroup, src->path, perms);
    }

    virDomainAuditCgroupPath(vm, priv->cgroup,
                             deny ? "deny" : "allow",
                             src->path,
                             virCgroupGetDevicePermsString(perms),
                             ret == 0);
95 96 97 98

    /* Get this for root squash NFS */
    if (ret < 0 &&
        virLastErrorIsSystemErrno(EACCES)) {
99
        VIR_DEBUG("Ignoring EACCES for %s", src->path);
100 101
        virResetLastError();
        ret = 0;
102
    }
103

104
    return ret;
105 106 107
}


108 109 110 111 112 113 114 115 116
int
qemuSetImageCgroup(virDomainObjPtr vm,
                   virStorageSourcePtr src,
                   bool deny)
{
    return qemuSetImageCgroupInternal(vm, src, deny, false);
}


117 118 119
int
qemuSetupDiskCgroup(virDomainObjPtr vm,
                    virDomainDiskDefPtr disk)
120
{
121
    virStorageSourcePtr next;
122
    bool forceReadonly = false;
123

124
    for (next = disk->src; next; next = next->backingStore) {
125
        if (qemuSetImageCgroupInternal(vm, next, false, forceReadonly) < 0)
126
            return -1;
127 128 129

        /* setup only the top level image for read-write */
        forceReadonly = true;
130
    }
131 132

    return 0;
133 134 135
}


136 137 138
int
qemuTeardownDiskCgroup(virDomainObjPtr vm,
                       virDomainDiskDefPtr disk)
139
{
140
    virStorageSourcePtr next;
141

142 143 144 145
    for (next = disk->src; next; next = next->backingStore) {
        if (qemuSetImageCgroup(vm, next, true) < 0)
            return -1;
    }
146

147
    return 0;
148 149
}

150

151
static int
152
qemuSetupChrSourceCgroup(virDomainObjPtr vm,
153
                         virDomainChrSourceDefPtr source)
154
{
155
    qemuDomainObjPrivatePtr priv = vm->privateData;
156
    int ret;
157

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

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

163
    ret = virCgroupAllowDevicePath(priv->cgroup, source->data.file.path,
164
                                   VIR_CGROUP_DEVICE_RW);
165
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
166
                             source->data.file.path, "rw", ret == 0);
167

168
    return ret;
169 170
}

171
static int
172
qemuSetupChardevCgroup(virDomainDefPtr def ATTRIBUTE_UNUSED,
173 174 175
                       virDomainChrDefPtr dev,
                       void *opaque)
{
176 177 178
    virDomainObjPtr vm = opaque;

    return qemuSetupChrSourceCgroup(vm, &dev->source);
179 180 181 182
}


static int
183
qemuSetupTPMCgroup(virDomainObjPtr vm)
184
{
185
    int ret = 0;
186
    virDomainTPMDefPtr dev = vm->def->tpm;
187 188 189

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
190
        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
191 192 193 194 195
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

196
    return ret;
197 198
}

199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
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,
                                       VIR_CGROUP_DEVICE_RW);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", dev->source.evdev, "rw", ret == 0);
        break;
    }

    return ret;
}


220
static int
221
qemuSetupHostUSBDeviceCgroup(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
222 223
                             const char *path,
                             void *opaque)
224
{
225 226
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
227
    int ret;
228 229

    VIR_DEBUG("Process path '%s' for USB device", path);
230 231 232
    ret = virCgroupAllowDevicePath(priv->cgroup, path,
                                   VIR_CGROUP_DEVICE_RW);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path, "rw", ret == 0);
233

234
    return ret;
235 236
}

237
static int
238
qemuSetupHostSCSIDeviceCgroup(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
239 240 241 242 243
                              const char *path,
                              void *opaque)
{
    virDomainObjPtr vm = opaque;
    qemuDomainObjPrivatePtr priv = vm->privateData;
244
    int ret;
245 246 247

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

248 249 250 251
    ret = virCgroupAllowDevicePath(priv->cgroup, path,
                                   virSCSIDeviceGetReadonly(dev) ?
                                   VIR_CGROUP_DEVICE_READ :
                                   VIR_CGROUP_DEVICE_RW);
252 253

    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
254
                             virSCSIDeviceGetReadonly(dev) ? "r" : "rw", ret == 0);
255

256
    return ret;
257
}
258

259
int
260
qemuSetupHostdevCgroup(virDomainObjPtr vm,
261 262 263 264
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
265
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
266
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
267
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
268
    virPCIDevicePtr pci = NULL;
269
    virUSBDevicePtr usb = NULL;
270
    virSCSIDevicePtr scsi = NULL;
271 272 273 274 275 276 277 278 279 280 281 282 283 284
    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) {

        switch (dev->source.subsys.type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
285
            if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
286
                int rv;
287

288 289 290 291
                pci = virPCIDeviceNew(pcisrc->addr.domain,
                                      pcisrc->addr.bus,
                                      pcisrc->addr.slot,
                                      pcisrc->addr.function);
292 293 294
                if (!pci)
                    goto cleanup;

295
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
296 297 298
                    goto cleanup;

                VIR_DEBUG("Cgroup allow %s for PCI device assignment", path);
299
                rv = virCgroupAllowDevicePath(priv->cgroup, path,
300 301
                                              VIR_CGROUP_DEVICE_RW);
                virDomainAuditCgroupPath(vm, priv->cgroup,
302 303
                                         "allow", path, "rw", rv == 0);
                if (rv < 0)
304 305 306
                    goto cleanup;
            }
            break;
307 308 309 310 311 312 313 314

        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;
315
            if ((usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device,
316 317 318 319
                                       NULL)) == NULL) {
                goto cleanup;
            }

320
            /* oddly, qemuSetupHostUSBDeviceCgroup doesn't ever
321 322
             * reference the usb object we just created
             */
323
            if (virUSBDeviceFileIterate(usb, qemuSetupHostUSBDeviceCgroup,
324 325 326 327
                                        vm) < 0) {
                goto cleanup;
            }
            break;
328

329
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
            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;
349

350 351 352 353 354
                if (virSCSIDeviceFileIterate(scsi,
                                             qemuSetupHostSCSIDeviceCgroup,
                                             vm) < 0)
                    goto cleanup;
            }
355 356
            break;
        }
357

358 359 360 361 362 363
        default:
            break;
        }
    }

    ret = 0;
364
 cleanup:
365
    virPCIDeviceFree(pci);
366
    virUSBDeviceFree(usb);
367
    virSCSIDeviceFree(scsi);
368 369 370 371 372 373 374 375 376 377
    VIR_FREE(path);
    return ret;
}

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    int ret = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
378
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    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) {

        switch (dev->source.subsys.type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
394
            if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
395
                int rv;
396

397 398 399 400
                pci = virPCIDeviceNew(pcisrc->addr.domain,
                                      pcisrc->addr.bus,
                                      pcisrc->addr.slot,
                                      pcisrc->addr.function);
401 402 403
                if (!pci)
                    goto cleanup;

404
                if (!(path = virPCIDeviceGetIOMMUGroupDev(pci)))
405 406 407
                    goto cleanup;

                VIR_DEBUG("Cgroup deny %s for PCI device assignment", path);
408
                rv = virCgroupDenyDevicePath(priv->cgroup, path,
409 410
                                             VIR_CGROUP_DEVICE_RWM);
                virDomainAuditCgroupPath(vm, priv->cgroup,
411 412
                                         "deny", path, "rwm", rv == 0);
                if (rv < 0)
413 414 415
                    goto cleanup;
            }
            break;
416 417 418
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
            /* nothing to tear down for USB */
            break;
419 420 421 422 423 424
        default:
            break;
        }
    }

    ret = 0;
425
 cleanup:
426 427 428 429 430
    virPCIDeviceFree(pci);
    VIR_FREE(path);
    return ret;
}

431 432 433 434
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
435
    size_t i;
436 437 438 439 440 441 442 443 444 445 446 447

    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;
        }
    }

448 449 450
    if (vm->def->blkio.weight != 0 &&
        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
        return -1;
451 452 453

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
454
            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
455 456
            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
457 458 459
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
                                               &dev->weight) < 0))
460 461 462 463
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
464 465 466
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
                                                 &dev->riops) < 0))
467 468 469 470
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
471 472 473
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
                                                  &dev->wiops) < 0))
474 475 476 477
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
478 479 480
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
                                                &dev->rbps) < 0))
481 482 483 484
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
485 486 487
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
                                                 &dev->wbps) < 0))
488 489 490 491 492 493 494
                return -1;
        }
    }

    return 0;
}

495

496 497 498 499 500
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
501
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
502 503 504
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
505 506 507
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
508 509
        } else {
            return 0;
510 511 512
        }
    }

513 514 515
    if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0)
            return -1;
516

517 518 519
    if (virMemoryLimitIsSet(vm->def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0)
            return -1;
520

521 522 523
    if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0)
            return -1;
524 525 526 527 528

    return 0;
}


529 530 531 532 533 534 535
static int
qemuSetupDevicesCgroup(virQEMUDriverPtr driver,
                       virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
536
    int rv = -1;
537
    int ret = -1;
538
    size_t i;
539 540 541 542

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

543 544 545 546 547
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
548 549 550 551 552 553 554
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

555
    for (i = 0; i < vm->def->ndisks; i++) {
556 557 558 559
        if (qemuSetupDiskCgroup(vm, vm->def->disks[i]) < 0)
            goto cleanup;
    }

560
    rv = virCgroupAllowDeviceMajor(priv->cgroup, 'c', DEVICE_PTY_MAJOR,
561 562
                                   VIR_CGROUP_DEVICE_RW);
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
563 564
                              "pty", "rw", rv == 0);
    if (rv < 0)
565 566 567 568 569 570 571 572
        goto cleanup;

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

    if (vm->def->nsounds &&
573
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
574 575
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
576
           cfg->vncAllowHostAudio) ||
577
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
578
        rv = virCgroupAllowDeviceMajor(priv->cgroup, 'c', DEVICE_SND_MAJOR,
579 580
                                       VIR_CGROUP_DEVICE_RW);
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
581 582
                                  "sound", "rw", rv == 0);
        if (rv < 0)
583 584 585
            goto cleanup;
    }

586
    for (i = 0; deviceACL[i] != NULL; i++) {
587
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
588
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
589 590 591
            continue;
        }

592
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
593
                                      VIR_CGROUP_DEVICE_RW);
594 595 596
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv == 0);
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
597 598 599 600 601 602 603 604 605
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
                               qemuSetupChardevCgroup,
                               vm) < 0)
        goto cleanup;

606
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
607 608 609
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
610
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
611 612 613
            goto cleanup;
    }

614 615 616 617 618
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

619 620 621
    for (i = 0; i < vm->def->nrngs; i++) {
        if (vm->def->rngs[i]->backend == VIR_DOMAIN_RNG_BACKEND_RANDOM) {
            VIR_DEBUG("Setting Cgroup ACL for RNG device");
622 623
            rv = virCgroupAllowDevicePath(priv->cgroup,
                                          vm->def->rngs[i]->source.file,
624 625
                                          VIR_CGROUP_DEVICE_RW);
            virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
626 627
                                     vm->def->rngs[i]->source.file,
                                     "rw", rv == 0);
628 629 630 631
            if (rv < 0 &&
                !virLastErrorIsSystemErrno(ENOENT))
                goto cleanup;
        }
632 633
    }

634
    ret = 0;
635
 cleanup:
636 637 638 639 640
    virObjectUnref(cfg);
    return ret;
}


641
int
642
qemuSetupCpusetMems(virDomainObjPtr vm)
643
{
644
    virCgroupPtr cgroup_temp = NULL;
645
    qemuDomainObjPrivatePtr priv = vm->privateData;
646
    virDomainNumatuneMemMode mode;
647
    char *mem_mask = NULL;
648 649 650 651 652
    int ret = -1;

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

653 654
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mode) < 0 ||
        mode != VIR_DOMAIN_NUMATUNE_MEM_STRICT)
655 656
        return 0;

657
    if (virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
658
                                            priv->autoNodeset,
659
                                            &mem_mask, -1) < 0)
660
        goto cleanup;
661

662
    if (mem_mask)
J
John Ferlan 已提交
663 664
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                               false, &cgroup_temp) < 0 ||
665
            virCgroupSetCpusetMems(cgroup_temp, mem_mask) < 0)
666
            goto cleanup;
667

668 669 670
    ret = 0;
 cleanup:
    VIR_FREE(mem_mask);
671
    virCgroupFree(&cgroup_temp);
672 673 674 675 676
    return ret;
}


static int
677
qemuSetupCpusetCgroup(virDomainObjPtr vm)
678 679 680 681 682 683
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

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

684 685 686
    if (virCgroupSetCpusetMemoryMigrate(priv->cgroup, true) < 0)
        return -1;

687
    return 0;
688 689 690
}


691
static int
692 693
qemuSetupCpuCgroup(virQEMUDriverPtr driver,
                   virDomainObjPtr vm)
694 695
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
696 697 698 699
    virObjectEventPtr event = NULL;
    virTypedParameterPtr eventParams = NULL;
    int eventNparams = 0;
    int eventMaxparams = 0;
700 701

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
702
       if (vm->def->cputune.sharesSpecified) {
703 704 705 706 707 708 709 710
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

711 712 713 714 715 716 717
    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;
718 719 720 721
        if (vm->def->cputune.shares != val) {
            vm->def->cputune.shares = val;
            if (virTypedParamsAddULLong(&eventParams, &eventNparams,
                                        &eventMaxparams,
722
                                        VIR_DOMAIN_TUNABLE_CPU_CPU_SHARES,
723 724 725 726 727 728
                                        val) < 0)
                return -1;

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

729
        qemuDomainEventQueue(driver, event);
730
    }
731 732 733 734 735

    return 0;
}


736
static int
737
qemuInitCgroup(virQEMUDriverPtr driver,
738 739 740
               virDomainObjPtr vm,
               size_t nnicindexes,
               int *nicindexes)
741
{
742
    int ret = -1;
743 744 745
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);

746
    if (!virQEMUDriverIsPrivileged(driver))
747 748
        goto done;

749 750 751
    if (!virCgroupAvailable())
        goto done;

752 753
    virCgroupFree(&priv->cgroup);

754
    if (!vm->def->resource) {
755 756
        virDomainResourceDefPtr res;

757
        if (VIR_ALLOC(res) < 0)
758
            goto cleanup;
759

760
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
761 762 763 764 765
            VIR_FREE(res);
            goto cleanup;
        }

        vm->def->resource = res;
766 767
    }

768 769 770 771 772 773
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
774 775 776

    if (virCgroupNewMachine(vm->def->name,
                            "qemu",
777
                            true,
778 779 780 781
                            vm->def->uuid,
                            NULL,
                            vm->pid,
                            false,
782
                            nnicindexes, nicindexes,
783 784 785
                            vm->def->resource->partition,
                            cfg->cgroupControllers,
                            &priv->cgroup) < 0) {
786 787
        if (virCgroupNewIgnoreError())
            goto done;
788

789 790
        goto cleanup;
    }
791

792
 done:
793
    ret = 0;
794
 cleanup:
795 796 797
    virObjectUnref(cfg);
    return ret;
}
798

799 800 801
static void
qemuRestoreCgroupState(virDomainObjPtr vm)
{
802
    char *mem_mask = NULL;
803
    char *nodeset = NULL;
804 805
    int empty = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
806
    size_t i = 0;
807
    virBitmapPtr all_nodes;
808
    virCgroupPtr cgroup_temp = NULL;
809 810 811 812 813 814 815 816

    if (!(all_nodes = virNumaGetHostNodeset()))
        goto error;

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

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
817
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
818 819 820 821 822
        goto error;

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

823
    for (i = 0; i < priv->nvcpupids; i++) {
J
John Ferlan 已提交
824 825
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               false, &cgroup_temp) < 0 ||
826 827 828 829 830
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

831
        VIR_FREE(nodeset);
832 833 834
        virCgroupFree(&cgroup_temp);
    }

835 836 837
    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 已提交
838
                               false, &cgroup_temp) < 0 ||
839 840 841 842 843
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

844
        VIR_FREE(nodeset);
845 846 847
        virCgroupFree(&cgroup_temp);
    }

J
John Ferlan 已提交
848 849
    if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &cgroup_temp) < 0 ||
850 851 852 853 854
        virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
        virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
        virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
        goto cleanup;

855 856
 cleanup:
    VIR_FREE(mem_mask);
857
    VIR_FREE(nodeset);
858
    virBitmapFree(all_nodes);
859
    virCgroupFree(&cgroup_temp);
860 861 862 863 864 865 866
    return;

 error:
    virResetLastError();
    VIR_DEBUG("Couldn't restore cgroups to meaningful state");
    goto cleanup;
}
867 868 869 870 871 872 873 874 875

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

876
    if (!virQEMUDriverIsPrivileged(driver))
877 878 879 880 881 882 883
        goto done;

    if (!virCgroupAvailable())
        goto done;

    virCgroupFree(&priv->cgroup);

884 885 886
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
                                  vm->pid,
887
                                  cfg->cgroupControllers,
888
                                  &priv->cgroup) < 0)
889
        goto cleanup;
890

891 892
    qemuRestoreCgroupState(vm);

893
 done:
894
    ret = 0;
895
 cleanup:
896
    virObjectUnref(cfg);
897
    return ret;
898 899
}

900 901
int
qemuSetupCgroup(virQEMUDriverPtr driver,
902 903 904
                virDomainObjPtr vm,
                size_t nnicindexes,
                int *nicindexes)
905
{
906
    qemuDomainObjPrivatePtr priv = vm->privateData;
907
    int ret = -1;
908

909 910 911 912 913 914
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

915
    if (qemuInitCgroup(driver, vm, nnicindexes, nicindexes) < 0)
916
        return -1;
917

918
    if (!priv->cgroup)
919
        return 0;
920

921 922
    if (qemuSetupDevicesCgroup(driver, vm) < 0)
        goto cleanup;
923

924 925
    if (qemuSetupBlkioCgroup(vm) < 0)
        goto cleanup;
926

927 928
    if (qemuSetupMemoryCgroup(vm) < 0)
        goto cleanup;
929

930
    if (qemuSetupCpuCgroup(driver, vm) < 0)
931
        goto cleanup;
932

933
    if (qemuSetupCpusetCgroup(vm) < 0)
934
        goto cleanup;
935

936
    ret = 0;
937
 cleanup:
938
    return ret;
939 940
}

941 942 943 944
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
945 946 947 948 949 950 951 952
{
    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 */
953
        if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
954 955
            return -1;

956
        if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
957 958 959
            return -1;
    }

960 961 962
    if (quota &&
        virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
        goto error;
963 964 965

    return 0;

966
 error:
967
    if (period) {
968 969 970 971 972 973
        virErrorPtr saved = virSaveLastError();
        ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
        if (saved) {
            virSetError(saved);
            virFreeError(saved);
        }
974 975 976 977 978
    }

    return -1;
}

979

980
int
981 982
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
983
{
984
    int ret = -1;
985 986
    char *new_cpus = NULL;

987
    if (!(new_cpus = virBitmapFormat(cpumask)))
988 989
        goto cleanup;

990
    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
991 992
        goto cleanup;

993
    ret = 0;
994
 cleanup:
995
    VIR_FREE(new_cpus);
996
    return ret;
997 998
}

999 1000
int
qemuSetupCgroupForVcpu(virDomainObjPtr vm)
1001 1002 1003
{
    virCgroupPtr cgroup_vcpu = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
1004
    virDomainDefPtr def = vm->def;
1005
    size_t i, j;
1006 1007
    unsigned long long period = vm->def->cputune.period;
    long long quota = vm->def->cputune.quota;
1008
    char *mem_mask = NULL;
1009
    virDomainNumatuneMemMode mem_mode;
1010

1011
    if ((period || quota) &&
1012
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
1013 1014
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("cgroup cpu is required for scheduler tuning"));
H
Hu Tao 已提交
1015 1016 1017
        return -1;
    }

1018 1019 1020
    /*
     * If CPU cgroup controller is not initialized here, then we need
     * neither period nor quota settings.  And if CPUSET controller is
1021 1022
     * not initialized either, then there's nothing to do anyway. CPU pinning
     * will be set via virProcessSetAffinity.
1023 1024 1025 1026 1027
     */
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU) &&
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return 0;

1028
    if (priv->nvcpupids == 0 || priv->vcpupids[0] == vm->pid) {
1029
        /* If we don't know VCPU<->PID mapping or all vcpu runs in the same
W
Wen Congyang 已提交
1030
         * thread, we cannot control each vcpu.
1031
         */
1032
        return 0;
1033 1034
    }

1035 1036
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mem_mode) == 0 &&
        mem_mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT &&
1037
        virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
1038 1039 1040 1041
                                            priv->autoNodeset,
                                            &mem_mask, -1) < 0)
        goto cleanup;

1042
    for (i = 0; i < priv->nvcpupids; i++) {
1043
        virCgroupFree(&cgroup_vcpu);
J
John Ferlan 已提交
1044 1045
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               true, &cgroup_vcpu) < 0)
1046 1047 1048
            goto cleanup;

        /* move the thread for vcpu to sub dir */
1049
        if (virCgroupAddTask(cgroup_vcpu, priv->vcpupids[i]) < 0)
1050 1051 1052
            goto cleanup;

        if (period || quota) {
H
Hu Tao 已提交
1053 1054
            if (qemuSetupCgroupVcpuBW(cgroup_vcpu, period, quota) < 0)
                goto cleanup;
1055 1056
        }

1057
        /* Set vcpupin in cgroup if vcpupin xml is provided */
1058
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET)) {
1059
            virBitmapPtr cpumap = NULL;
M
Martin Kletzander 已提交
1060

1061 1062 1063 1064
            if (mem_mask &&
                virCgroupSetCpusetMems(cgroup_vcpu, mem_mask) < 0)
                goto cleanup;

1065 1066 1067 1068 1069
            /* try to use the default cpu maps */
            if (vm->def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO)
                cpumap = priv->autoCpuset;
            else
                cpumap = vm->def->cpumask;
M
Martin Kletzander 已提交
1070

1071 1072 1073 1074 1075 1076
            /* lookup a more specific pinning info */
            for (j = 0; j < def->cputune.nvcpupin; j++) {
                if (def->cputune.vcpupin[j]->id == i) {
                    cpumap = def->cputune.vcpupin[j]->cpumask;
                    break;
                }
M
Martin Kletzander 已提交
1077
            }
1078 1079 1080 1081

            if (!cpumap)
                continue;

1082
            if (qemuSetupCgroupCpusetCpus(cgroup_vcpu, cpumap) < 0)
1083
                goto cleanup;
M
Martin Kletzander 已提交
1084
        }
1085
    }
1086
    virCgroupFree(&cgroup_vcpu);
1087
    VIR_FREE(mem_mask);
1088 1089 1090

    return 0;

1091
 cleanup:
1092 1093 1094 1095
    if (cgroup_vcpu) {
        virCgroupRemove(cgroup_vcpu);
        virCgroupFree(&cgroup_vcpu);
    }
1096
    VIR_FREE(mem_mask);
1097

1098 1099 1100
    return -1;
}

1101
int
1102
qemuSetupCgroupForEmulator(virDomainObjPtr vm)
1103
{
1104
    virBitmapPtr cpumask = NULL;
1105
    virCgroupPtr cgroup_emulator = NULL;
1106
    virDomainDefPtr def = vm->def;
1107
    qemuDomainObjPrivatePtr priv = vm->privateData;
1108 1109
    unsigned long long period = vm->def->cputune.emulator_period;
    long long quota = vm->def->cputune.emulator_quota;
1110

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

1118 1119 1120 1121 1122 1123 1124 1125 1126
    /*
     * 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;

J
John Ferlan 已提交
1127 1128
    if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           true, &cgroup_emulator) < 0)
1129 1130
        goto cleanup;

1131
    if (virCgroupMoveTask(priv->cgroup, cgroup_emulator) < 0)
1132
        goto cleanup;
1133

1134
    if (def->cputune.emulatorpin)
1135
        cpumask = def->cputune.emulatorpin;
1136 1137
    else if (def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO)
        cpumask = priv->autoCpuset;
1138
    else if (def->cpumask)
1139 1140 1141
        cpumask = def->cpumask;

    if (cpumask) {
1142
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET) &&
1143
            qemuSetupCgroupCpusetCpus(cgroup_emulator, cpumask) < 0)
1144
            goto cleanup;
H
Hu Tao 已提交
1145
    }
1146

1147
    if (period || quota) {
1148 1149 1150 1151
        if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU) &&
            qemuSetupCgroupVcpuBW(cgroup_emulator, period,
                                  quota) < 0)
            goto cleanup;
1152 1153
    }

1154 1155 1156
    virCgroupFree(&cgroup_emulator);
    return 0;

1157
 cleanup:
1158 1159 1160 1161 1162
    if (cgroup_emulator) {
        virCgroupRemove(cgroup_emulator);
        virCgroupFree(&cgroup_emulator);
    }

1163
    return -1;
1164
}
1165

1166 1167 1168 1169 1170 1171
int
qemuSetupCgroupForIOThreads(virDomainObjPtr vm)
{
    virCgroupPtr cgroup_iothread = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virDomainDefPtr def = vm->def;
1172
    size_t i;
1173 1174
    unsigned long long period = vm->def->cputune.period;
    long long quota = vm->def->cputune.quota;
1175
    char *mem_mask = NULL;
1176
    virDomainNumatuneMemMode mem_mode;
1177

1178 1179 1180
    if (def->niothreadids == 0)
        return 0;

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    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;

1197 1198
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mem_mode) == 0 &&
        mem_mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT &&
1199
        virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
1200 1201 1202 1203
                                            priv->autoNodeset,
                                            &mem_mask, -1) < 0)
        goto cleanup;

1204
    for (i = 0; i < def->niothreadids; i++) {
1205 1206 1207
        /* IOThreads are numbered 1..n, although the array is 0..n-1,
         * so we will account for that here
         */
1208 1209
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_IOTHREAD,
                               def->iothreadids[i]->iothread_id,
J
John Ferlan 已提交
1210
                               true, &cgroup_iothread) < 0)
1211 1212 1213
            goto cleanup;

        /* move the thread for iothread to sub dir */
1214 1215
        if (virCgroupAddTask(cgroup_iothread,
                             def->iothreadids[i]->thread_id) < 0)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
            goto cleanup;

        if (period || quota) {
            if (qemuSetupCgroupVcpuBW(cgroup_iothread, period, quota) < 0)
                goto cleanup;
        }

        /* Set iothreadpin in cgroup if iothreadpin xml is provided */
        if (virCgroupHasController(priv->cgroup,
                                   VIR_CGROUP_CONTROLLER_CPUSET)) {
1226
            virBitmapPtr cpumask = NULL;
1227

1228 1229 1230 1231
            if (mem_mask &&
                virCgroupSetCpusetMems(cgroup_iothread, mem_mask) < 0)
                goto cleanup;

1232 1233 1234
            if (def->iothreadids[i]->cpumask)
                cpumask = def->iothreadids[i]->cpumask;
            else if (def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO)
1235 1236 1237
                cpumask = priv->autoCpuset;
            else
                cpumask = def->cpumask;
1238

1239
            if (cpumask &&
1240
                qemuSetupCgroupCpusetCpus(cgroup_iothread, cpumask) < 0)
1241
                goto cleanup;
1242 1243 1244 1245
        }

        virCgroupFree(&cgroup_iothread);
    }
1246
    VIR_FREE(mem_mask);
1247 1248 1249 1250 1251 1252 1253 1254

    return 0;

 cleanup:
    if (cgroup_iothread) {
        virCgroupRemove(cgroup_iothread);
        virCgroupFree(&cgroup_iothread);
    }
1255
    VIR_FREE(mem_mask);
1256 1257 1258 1259

    return -1;
}

1260
int
1261 1262
qemuRemoveCgroup(virQEMUDriverPtr driver,
                 virDomainObjPtr vm)
1263
{
1264
    qemuDomainObjPrivatePtr priv = vm->privateData;
1265

1266
    if (priv->cgroup == NULL)
1267 1268
        return 0; /* Not supported, so claim success */

1269 1270
    if (virCgroupTerminateMachine(vm->def->name,
                                  "qemu",
1271
                                  virQEMUDriverIsPrivileged(driver)) < 0) {
1272 1273 1274 1275
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

1276
    return virCgroupRemove(priv->cgroup);
1277 1278
}

1279 1280
int
qemuAddToCgroup(virDomainObjPtr vm)
1281
{
1282
    qemuDomainObjPrivatePtr priv = vm->privateData;
1283

1284
    if (priv->cgroup == NULL)
1285 1286
        return 0; /* Not supported, so claim success */

1287
    return 0;
1288
}