qemu_cgroup.c 37.1 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
 */

#include <config.h>

#include "qemu_cgroup.h"
25
#include "qemu_domain.h"
26
#include "qemu_process.h"
27
#include "qemu_extdevice.h"
28
#include "qemu_hostdev.h"
29
#include "virlog.h"
30
#include "viralloc.h"
31
#include "virerror.h"
32
#include "domain_audit.h"
33
#include "domain_cgroup.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
#include "virdevmapper.h"
J
Ján Tomko 已提交
41
#include "virutil.h"
42 43 44

#define VIR_FROM_THIS VIR_FROM_QEMU

45 46
VIR_LOG_INIT("qemu.qemu_cgroup");

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

57

58
static int
59 60 61
qemuSetupImagePathCgroup(virDomainObjPtr vm,
                         const char *path,
                         bool readonly)
62
{
63
    qemuDomainObjPrivatePtr priv = vm->privateData;
64
    int perms = VIR_CGROUP_DEVICE_READ;
65 66 67 68
    char **targetPaths = NULL;
    size_t i;
    int rv;
    int ret = -1;
69

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

73
    if (!readonly)
74
        perms |= VIR_CGROUP_DEVICE_WRITE;
75

76
    VIR_DEBUG("Allow path %s, perms: %s",
77
              path, virCgroupGetDevicePermsString(perms));
78

79
    rv = virCgroupAllowDevicePath(priv->cgroup, path, perms, true);
80

81
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
82
                             virCgroupGetDevicePermsString(perms),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                             rv);
    if (rv < 0)
        goto cleanup;

    if (rv > 0) {
        /* @path is neither character device nor block device. */
        ret = 0;
        goto cleanup;
    }

    if (virDevMapperGetTargets(path, &targetPaths) < 0 &&
        errno != ENOSYS && errno != EBADF) {
        virReportSystemError(errno,
                             _("Unable to get devmapper targets for %s"),
                             path);
        goto cleanup;
    }
100

101 102 103 104 105 106 107 108 109 110 111 112 113
    for (i = 0; targetPaths && targetPaths[i]; i++) {
        rv = virCgroupAllowDevicePath(priv->cgroup, targetPaths[i], perms, false);

        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", targetPaths[i],
                                 virCgroupGetDevicePermsString(perms),
                                 rv);
        if (rv < 0)
            goto cleanup;
    }

    ret = 0;
 cleanup:
    virStringListFree(targetPaths);
114
    return ret;
115 116 117
}


118 119 120 121 122
static int
qemuSetupImageCgroupInternal(virDomainObjPtr vm,
                             virStorageSourcePtr src,
                             bool forceReadonly)
{
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    g_autofree char *path = NULL;
    bool readonly = src->readonly || forceReadonly;

    if (src->type == VIR_STORAGE_TYPE_NVME) {
        /* Even though disk is R/O we can't make it so in
         * CGroups. QEMU will try to do some ioctl()-s over the
         * device and such operations are considered R/W by the
         * kernel */
        readonly = false;

        if (!(path = virPCIDeviceAddressGetIOMMUGroupDev(&src->nvme->pciAddr)))
            return -1;

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

        path = g_strdup(src->path);
146 147
    }

148
    if (virStoragePRDefIsManaged(src->pr) &&
149 150
        virFileExists(QEMU_DEVICE_MAPPER_CONTROL_PATH) &&
        qemuSetupImagePathCgroup(vm, QEMU_DEVICE_MAPPER_CONTROL_PATH, false) < 0)
151 152
        return -1;

153
    return qemuSetupImagePathCgroup(vm, path, readonly);
154 155 156
}


157
int
158 159
qemuSetupImageCgroup(virDomainObjPtr vm,
                     virStorageSourcePtr src)
160
{
161
    return qemuSetupImageCgroupInternal(vm, src, false);
162 163 164 165 166 167 168
}


int
qemuTeardownImageCgroup(virDomainObjPtr vm,
                        virStorageSourcePtr src)
{
169
    qemuDomainObjPrivatePtr priv = vm->privateData;
170
    g_autofree char *path = NULL;
171
    int perms = VIR_CGROUP_DEVICE_RWM;
172 173
    bool hasPR = false;
    bool hasNVMe = false;
174
    size_t i;
175 176 177 178 179 180
    int ret;

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

181 182
    for (i = 0; i < vm->def->ndisks; i++) {
        virStorageSourcePtr diskSrc = vm->def->disks[i]->src;
183

184 185
        if (src == diskSrc)
            continue;
186

187 188
        if (virStoragePRDefIsManaged(diskSrc->pr))
            hasPR = true;
189

190 191 192 193 194 195 196
        if (virStorageSourceChainHasNVMe(diskSrc))
            hasNVMe = true;
    }

    if (src->type == VIR_STORAGE_TYPE_NVME) {
        if (!(path = virPCIDeviceAddressGetIOMMUGroupDev(&src->nvme->pciAddr)))
            return -1;
197

198 199 200
        if (!hasNVMe &&
            !qemuDomainNeedsVFIO(vm->def)) {
            ret = virCgroupDenyDevicePath(priv->cgroup, QEMU_DEV_VFIO, perms, true);
201
            virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
202
                                     QEMU_DEV_VFIO,
203 204
                                     virCgroupGetDevicePermsString(perms), ret);
            if (ret < 0)
205 206 207 208 209 210 211
                return -1;
        }
    } else {
        if (!src->path || !virStorageSourceIsLocalStorage(src)) {
            VIR_DEBUG("Not updating cgroups for disk path '%s', type: %s",
                      NULLSTR(src->path), virStorageTypeToString(src->type));
            return 0;
212
        }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

        path = g_strdup(src->path);
    }

    if (!hasPR &&
        virFileExists(QEMU_DEVICE_MAPPER_CONTROL_PATH)) {
        VIR_DEBUG("Disabling device mapper control");
        ret = virCgroupDenyDevicePath(priv->cgroup,
                                      QEMU_DEVICE_MAPPER_CONTROL_PATH,
                                      perms, true);
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
                                 QEMU_DEVICE_MAPPER_CONTROL_PATH,
                                 virCgroupGetDevicePermsString(perms), ret);
        if (ret < 0)
            return ret;
228 229
    }

230
    VIR_DEBUG("Deny path %s", path);
231

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

234
    virDomainAuditCgroupPath(vm, priv->cgroup, "deny", path,
235
                             virCgroupGetDevicePermsString(perms), ret);
236

237 238 239 240 241 242 243
    /* If you're looking for a counter part to
     * qemuSetupImagePathCgroup you're at the right place.
     * However, we can't just blindly deny all the device mapper
     * targets of src->path because they might still be used by
     * another disk in domain. Just like we are not removing
     * disks from namespace. */

244
    return ret;
245 246 247
}


248
int
249 250
qemuSetupImageChainCgroup(virDomainObjPtr vm,
                          virStorageSourcePtr src)
251
{
252
    virStorageSourcePtr next;
253
    bool forceReadonly = false;
254

255
    for (next = src; virStorageSourceIsBacking(next); next = next->backingStore) {
256
        if (qemuSetupImageCgroupInternal(vm, next, forceReadonly) < 0)
257
            return -1;
258 259 260

        /* setup only the top level image for read-write */
        forceReadonly = true;
261
    }
262 263

    return 0;
264 265 266
}


267
int
268 269
qemuTeardownImageChainCgroup(virDomainObjPtr vm,
                             virStorageSourcePtr src)
270
{
271
    virStorageSourcePtr next;
272

273
    for (next = src; virStorageSourceIsBacking(next); next = next->backingStore) {
274
        if (qemuTeardownImageCgroup(vm, next) < 0)
275 276
            return -1;
    }
277

278
    return 0;
279 280
}

281

282
static int
283
qemuSetupChrSourceCgroup(virDomainObjPtr vm,
284
                         virDomainChrSourceDefPtr source)
285
{
286
    qemuDomainObjPrivatePtr priv = vm->privateData;
287
    int ret;
288

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

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

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

297
    ret = virCgroupAllowDevicePath(priv->cgroup, source->data.file.path,
298
                                   VIR_CGROUP_DEVICE_RW, false);
299
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
300
                             source->data.file.path, "rw", ret);
301

302
    return ret;
303 304
}

305 306 307 308 309 310 311 312

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

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

316 317 318 319 320 321 322 323
    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",
324
                             source->data.file.path, "rw", ret);
325 326 327 328 329

    return ret;
}


330
static int
J
Ján Tomko 已提交
331
qemuSetupChardevCgroupCB(virDomainDefPtr def G_GNUC_UNUSED,
332 333
                         virDomainChrDefPtr dev,
                         void *opaque)
334
{
335 336
    virDomainObjPtr vm = opaque;

337
    return qemuSetupChrSourceCgroup(vm, dev->source);
338 339 340 341
}


static int
342
qemuSetupTPMCgroup(virDomainObjPtr vm)
343
{
344
    int ret = 0;
345
    virDomainTPMDefPtr dev = vm->def->tpm;
346 347 348

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
349
        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
350
        break;
351
    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
352 353 354 355
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

356
    return ret;
357 358
}

359

360
int
361 362 363 364 365 366
qemuSetupInputCgroup(virDomainObjPtr vm,
                     virDomainInputDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = 0;

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

370 371 372 373
    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,
374
                                       VIR_CGROUP_DEVICE_RW, false);
375
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", dev->source.evdev, "rw", ret);
376 377 378 379 380 381 382
        break;
    }

    return ret;
}


383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
int
qemuTeardownInputCgroup(virDomainObjPtr vm,
                        virDomainInputDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = 0;

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

    switch (dev->type) {
    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
        VIR_DEBUG("Process path '%s' for input device", dev->source.evdev);
        ret = virCgroupDenyDevicePath(priv->cgroup, dev->source.evdev,
                                      VIR_CGROUP_DEVICE_RWM, false);
398
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny", dev->source.evdev, "rwm", ret);
399 400 401 402 403 404 405
        break;
    }

    return ret;
}


406 407 408 409 410 411 412 413 414 415
/**
 * qemuSetupHostdevCgroup:
 * vm: domain object
 * @dev: device to allow
 *
 * For given host device @dev allow access to in Cgroups.
 *
 * Returns: 0 on success,
 *         -1 otherwise.
 */
416
int
417
qemuSetupHostdevCgroup(virDomainObjPtr vm,
418 419 420
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
421 422
    g_autofree char *path = NULL;
    int perms;
423
    int rv;
424

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

428
    if (qemuDomainGetHostdevPath(dev, &path, &perms) < 0)
429
        return -1;
430

431 432 433 434 435 436 437 438 439
    if (path) {
        VIR_DEBUG("Cgroup allow %s perms=%d", path, perms);
        rv = virCgroupAllowDevicePath(priv->cgroup, path, perms, false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
                                 virCgroupGetDevicePermsString(perms),
                                 rv);
        if (rv < 0)
            return -1;
    }
440

441 442 443 444 445 446 447
    if (qemuHostdevNeedsVFIO(dev)) {
        VIR_DEBUG("Cgroup allow %s perms=%d", QEMU_DEV_VFIO, VIR_CGROUP_DEVICE_RW);
        rv = virCgroupAllowDevicePath(priv->cgroup, QEMU_DEV_VFIO,
                                      VIR_CGROUP_DEVICE_RW, false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
                                 QEMU_DEV_VFIO, "rw", rv);
        if (rv < 0)
448
            return -1;
449 450
    }

451
    return 0;
452 453
}

454 455 456 457 458 459 460 461 462 463 464 465

/**
 * qemuTeardownHostdevCgroup:
 * @vm: doamin object
 * @dev: device to tear down
 *
 * For given host device @dev deny access to it in CGroups.
 * Note, @dev must not be in @vm's definition.
 *
 * Returns: 0 on success,
 *         -1 otherwise.
 */
466 467
int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
468
                          virDomainHostdevDefPtr dev)
469 470
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
471
    g_autofree char *path = NULL;
472
    int rv;
473 474 475 476

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

477
    if (qemuDomainGetHostdevPath(dev, &path, NULL) < 0)
478
        return -1;
479

480 481 482 483 484 485 486 487 488
    if (path) {
        VIR_DEBUG("Cgroup deny %s", path);
        rv = virCgroupDenyDevicePath(priv->cgroup, path,
                                     VIR_CGROUP_DEVICE_RWM, false);
        virDomainAuditCgroupPath(vm, priv->cgroup,
                                 "deny", path, "rwm", rv);
        if (rv < 0)
            return -1;
    }
489

490 491 492 493 494 495 496 497
    if (qemuHostdevNeedsVFIO(dev) &&
        !qemuDomainNeedsVFIO(vm->def)) {
        VIR_DEBUG("Cgroup deny " QEMU_DEV_VFIO);
        rv = virCgroupDenyDevicePath(priv->cgroup, QEMU_DEV_VFIO,
                                     VIR_CGROUP_DEVICE_RWM, false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
                                 QEMU_DEV_VFIO, "rwm", rv);
        if (rv < 0)
498
            return -1;
499 500
    }

501
    return 0;
502 503
}

504

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
int
qemuSetupMemoryDevicesCgroup(virDomainObjPtr vm,
                             virDomainMemoryDefPtr mem)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

    if (mem->model != VIR_DOMAIN_MEMORY_MODEL_NVDIMM)
        return 0;

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

    VIR_DEBUG("Setting devices Cgroup for NVDIMM device: %s", mem->nvdimmPath);
    rv = virCgroupAllowDevicePath(priv->cgroup, mem->nvdimmPath,
                                  VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
522
                             mem->nvdimmPath, "rw", rv);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

    return rv;
}


int
qemuTeardownMemoryDevicesCgroup(virDomainObjPtr vm,
                                virDomainMemoryDefPtr mem)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

    if (mem->model != VIR_DOMAIN_MEMORY_MODEL_NVDIMM)
        return 0;

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

    rv = virCgroupDenyDevicePath(priv->cgroup, mem->nvdimmPath,
                                 VIR_CGROUP_DEVICE_RWM, false);
    virDomainAuditCgroupPath(vm, priv->cgroup,
544
                             "deny", mem->nvdimmPath, "rwm", rv);
545 546 547 548
    return rv;
}


549 550 551 552 553
static int
qemuSetupGraphicsCgroup(virDomainObjPtr vm,
                        virDomainGraphicsDefPtr gfx)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
554
    const char *rendernode = virDomainGraphicsGetRenderNode(gfx);
555 556
    int ret;

557 558
    if (!rendernode ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
559 560 561 562 563
        return 0;

    ret = virCgroupAllowDevicePath(priv->cgroup, rendernode,
                                   VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", rendernode,
564
                             "rw", ret);
565 566 567 568
    return ret;
}


569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
static int
qemuSetupVideoCgroup(virDomainObjPtr vm,
                     virDomainVideoDefPtr def)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virDomainVideoAccelDefPtr accel = def->accel;
    int ret;

    if (!accel)
        return 0;

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

    ret = virCgroupAllowDevicePath(priv->cgroup, accel->rendernode,
                                   VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", accel->rendernode,
                             "rw", ret);
    return ret;
}


592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

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

608
    return virDomainCgroupSetupBlkio(priv->cgroup, vm->def->blkio);
609 610
}

611

612 613 614 615 616
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
617
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
618 619 620
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
621 622 623
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
624 625
        } else {
            return 0;
626 627 628
        }
    }

629
    return virDomainCgroupSetupMemtune(priv->cgroup, vm->def->mem);
630 631 632
}


633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
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;
}


652 653 654 655 656 657 658
int
qemuSetupRNGCgroup(virDomainObjPtr vm,
                   virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

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

662 663 664 665 666 667 668
    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,
669
                                 "rw", rv);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


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

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

689 690 691 692 693 694 695
    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,
696
                                 "rw", rv);
697 698 699 700 701 702 703 704 705
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
int
qemuSetupChardevCgroup(virDomainObjPtr vm,
                       virDomainChrDefPtr dev)
{
    return qemuSetupChrSourceCgroup(vm, dev->source);
}


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


722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
static int
qemuSetupSEVCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret;

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

    ret = virCgroupAllowDevicePath(priv->cgroup, "/dev/sev",
                                   VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", "/dev/sev",
                             "rw", ret);
    return ret;
}

738
static int
739
qemuSetupDevicesCgroup(virDomainObjPtr vm)
740 741 742 743
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
744
    int rv = -1;
745
    int ret = -1;
746
    size_t i;
747 748 749 750

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

751 752 753 754 755
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
756 757 758 759 760 761 762
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

763 764 765
    if (qemuSetupFirmwareCgroup(vm) < 0)
        goto cleanup;

766
    for (i = 0; i < vm->def->ndisks; i++) {
767
        if (qemuSetupImageChainCgroup(vm, vm->def->disks[i]->src) < 0)
768 769 770
            goto cleanup;
    }

771 772
    rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_PTY_MAJOR, -1,
                              VIR_CGROUP_DEVICE_RW);
773
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
774 775
                              "pty", "rw", rv == 0);
    if (rv < 0)
776 777
        goto cleanup;

778
    cfg = virQEMUDriverGetConfig(priv->driver);
779 780 781 782 783
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
784
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
785 786
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
787
           cfg->vncAllowHostAudio) ||
788
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
789 790
        rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_SND_MAJOR, -1,
                                  VIR_CGROUP_DEVICE_RW);
791
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
792 793
                                  "sound", "rw", rv == 0);
        if (rv < 0)
794 795 796
            goto cleanup;
    }

797
    for (i = 0; deviceACL[i] != NULL; i++) {
798
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
799
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
800 801 802
            continue;
        }

803
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
804
                                      VIR_CGROUP_DEVICE_RW, false);
805
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv);
806 807
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
808 809 810 811 812
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
813
                               qemuSetupChardevCgroupCB,
814 815 816
                               vm) < 0)
        goto cleanup;

817
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
818 819 820
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
821 822
        /* This may allow /dev/vfio/vfio multiple times, but that
         * is not a problem. Kernel will have only one record. */
823
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
824 825 826
            goto cleanup;
    }

827 828 829 830 831
    for (i = 0; i < vm->def->nmems; i++) {
        if (qemuSetupMemoryDevicesCgroup(vm, vm->def->mems[i]) < 0)
            goto cleanup;
    }

832 833 834
    for (i = 0; i < vm->def->ngraphics; i++) {
        if (qemuSetupGraphicsCgroup(vm, vm->def->graphics[i]) < 0)
            goto cleanup;
835 836 837 838 839
    }

    for (i = 0; i < vm->def->nvideos; i++) {
        if (qemuSetupVideoCgroup(vm, vm->def->videos[i]) < 0)
            goto cleanup;
840 841
    }

842 843 844 845 846
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

847
    for (i = 0; i < vm->def->nrngs; i++) {
848 849
        if (qemuSetupRNGCgroup(vm, vm->def->rngs[i]) < 0)
            goto cleanup;
850 851
    }

852 853 854
    if (vm->def->sev && qemuSetupSEVCgroup(vm) < 0)
        goto cleanup;

855
    ret = 0;
856
 cleanup:
857 858 859 860 861
    virObjectUnref(cfg);
    return ret;
}


862
static int
863
qemuSetupCpusetCgroup(virDomainObjPtr vm)
864 865 866 867 868 869
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

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

870 871 872
    if (virCgroupSetCpusetMemoryMigrate(priv->cgroup, true) < 0)
        return -1;

873
    return 0;
874 875 876
}


877
static int
878
qemuSetupCpuCgroup(virDomainObjPtr vm)
879 880
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
881 882 883 884
    virObjectEventPtr event = NULL;
    virTypedParameterPtr eventParams = NULL;
    int eventNparams = 0;
    int eventMaxparams = 0;
885 886

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
887
       if (vm->def->cputune.sharesSpecified) {
888 889 890 891 892 893 894 895
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

896 897
    if (vm->def->cputune.sharesSpecified) {
        unsigned long long val;
898 899
        if (virCgroupSetupCpuShares(priv->cgroup, vm->def->cputune.shares,
                                    &val) < 0)
900 901
            return -1;

902 903 904 905
        if (vm->def->cputune.shares != val) {
            vm->def->cputune.shares = val;
            if (virTypedParamsAddULLong(&eventParams, &eventNparams,
                                        &eventMaxparams,
906
                                        VIR_DOMAIN_TUNABLE_CPU_CPU_SHARES,
907 908 909 910 911 912
                                        val) < 0)
                return -1;

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

913
        virObjectEventStateQueue(priv->driver->domainEventState, event);
914
    }
915 916 917 918 919

    return 0;
}


920
static int
921
qemuInitCgroup(virDomainObjPtr vm,
922 923
               size_t nnicindexes,
               int *nicindexes)
924
{
925
    int ret = -1;
926
    qemuDomainObjPrivatePtr priv = vm->privateData;
927
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
928

929
    if (!virQEMUDriverIsPrivileged(priv->driver))
930 931
        goto done;

932 933 934
    if (!virCgroupAvailable())
        goto done;

935
    virCgroupFree(&priv->cgroup);
936

937
    if (!vm->def->resource) {
938 939
        virDomainResourceDefPtr res;

940
        if (VIR_ALLOC(res) < 0)
941
            goto cleanup;
942

943
        res->partition = g_strdup("/machine");
944 945

        vm->def->resource = res;
946 947
    }

948 949 950 951 952 953
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
954

955
    if (virCgroupNewMachine(priv->machineName,
956 957 958 959 960
                            "qemu",
                            vm->def->uuid,
                            NULL,
                            vm->pid,
                            false,
961
                            nnicindexes, nicindexes,
962 963
                            vm->def->resource->partition,
                            cfg->cgroupControllers,
964
                            cfg->maxThreadsPerProc,
965
                            &priv->cgroup) < 0) {
966 967
        if (virCgroupNewIgnoreError())
            goto done;
968

969 970
        goto cleanup;
    }
971

972
 done:
973
    ret = 0;
974
 cleanup:
975 976 977
    virObjectUnref(cfg);
    return ret;
}
978

979 980 981
static void
qemuRestoreCgroupState(virDomainObjPtr vm)
{
982
    char *mem_mask = NULL;
983
    char *nodeset = NULL;
984 985
    int empty = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
986
    size_t i = 0;
987
    virBitmapPtr all_nodes;
988
    virCgroupPtr cgroup_temp = NULL;
989

990 991 992 993
    if (!virNumaIsAvailable() ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return;

994
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
995 996 997 998 999 1000
        goto error;

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

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
1001
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
1002 1003 1004 1005 1006
        goto error;

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

1007
    for (i = 0; i < virDomainDefGetVcpusMax(vm->def); i++) {
1008
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(vm->def, i);
1009 1010 1011 1012

        if (!vcpu->online)
            continue;

J
John Ferlan 已提交
1013 1014
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               false, &cgroup_temp) < 0 ||
1015 1016 1017 1018 1019
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

1020
        VIR_FREE(nodeset);
1021
        virCgroupFree(&cgroup_temp);
1022 1023
    }

1024 1025 1026
    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 已提交
1027
                               false, &cgroup_temp) < 0 ||
1028 1029 1030 1031 1032
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

1033
        VIR_FREE(nodeset);
1034
        virCgroupFree(&cgroup_temp);
1035 1036
    }

J
John Ferlan 已提交
1037 1038
    if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &cgroup_temp) < 0 ||
1039 1040 1041 1042 1043
        virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
        virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
        virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
        goto cleanup;

1044 1045
 cleanup:
    VIR_FREE(mem_mask);
1046
    VIR_FREE(nodeset);
1047
    virBitmapFree(all_nodes);
1048
    virCgroupFree(&cgroup_temp);
1049 1050 1051 1052 1053 1054 1055
    return;

 error:
    virResetLastError();
    VIR_DEBUG("Couldn't restore cgroups to meaningful state");
    goto cleanup;
}
1056 1057

int
1058
qemuConnectCgroup(virDomainObjPtr vm)
1059 1060
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1061
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
1062 1063
    int ret = -1;

1064
    if (!virQEMUDriverIsPrivileged(priv->driver))
1065 1066 1067 1068 1069
        goto done;

    if (!virCgroupAvailable())
        goto done;

1070
    virCgroupFree(&priv->cgroup);
1071

1072 1073 1074
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
                                  vm->pid,
1075
                                  cfg->cgroupControllers,
1076
                                  priv->machineName,
1077
                                  &priv->cgroup) < 0)
1078
        goto cleanup;
1079

1080 1081
    qemuRestoreCgroupState(vm);

1082
 done:
1083
    ret = 0;
1084
 cleanup:
1085
    virObjectUnref(cfg);
1086
    return ret;
1087 1088
}

1089
int
1090
qemuSetupCgroup(virDomainObjPtr vm,
1091 1092
                size_t nnicindexes,
                int *nicindexes)
1093
{
1094
    qemuDomainObjPrivatePtr priv = vm->privateData;
1095

1096 1097 1098 1099 1100 1101
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

1102
    if (qemuInitCgroup(vm, nnicindexes, nicindexes) < 0)
1103
        return -1;
1104

1105
    if (!priv->cgroup)
1106
        return 0;
1107

1108
    if (qemuSetupDevicesCgroup(vm) < 0)
1109
        return -1;
1110

1111
    if (qemuSetupBlkioCgroup(vm) < 0)
1112
        return -1;
1113

1114
    if (qemuSetupMemoryCgroup(vm) < 0)
1115
        return -1;
1116

1117
    if (qemuSetupCpuCgroup(vm) < 0)
1118
        return -1;
1119

1120
    if (qemuSetupCpusetCgroup(vm) < 0)
1121
        return -1;
1122

1123
    return 0;
1124 1125
}

1126 1127 1128 1129
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
1130
{
1131
    return virCgroupSetupCpuPeriodQuota(cgroup, period, quota);
1132 1133
}

1134

1135
int
1136 1137
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
1138
{
1139
    return virCgroupSetupCpusetCpus(cgroup, cpumask);
1140 1141
}

1142

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
int
qemuSetupCgroupForExtDevices(virDomainObjPtr vm,
                             virQEMUDriverPtr driver)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virCgroupPtr cgroup_temp = NULL;
    int ret = -1;

    if (!qemuExtDevicesHasDevice(vm->def) ||
        priv->cgroup == NULL)
        return 0; /* Not supported, so claim success */

    /*
     * 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 (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &cgroup_temp) < 0)
        goto cleanup;

    ret = qemuExtDevicesSetupCgroup(driver, vm->def, cgroup_temp);

 cleanup:
1171
    virCgroupFree(&cgroup_temp);
1172 1173 1174 1175 1176

    return ret;
}


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 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
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;
}


1226
int
1227
qemuRemoveCgroup(virDomainObjPtr vm)
1228
{
1229
    qemuDomainObjPrivatePtr priv = vm->privateData;
1230

1231
    if (priv->cgroup == NULL)
1232 1233
        return 0; /* Not supported, so claim success */

1234
    if (virCgroupTerminateMachine(priv->machineName) < 0) {
1235 1236 1237 1238
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

1239
    return virCgroupRemove(priv->cgroup);
1240
}
1241 1242 1243 1244 1245 1246 1247 1248


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

1249
    virCgroupFree(&data->emulatorCgroup);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
    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
1263
 * qemuCgroupEmulatorAllNodesRestore.
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
 *
 * 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;

1280
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
        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;

1297
    *retData = g_steal_pointer(&data);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    ret = 0;

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

    return ret;
}


/**
1310
 * qemuCgroupEmulatorAllNodesRestore:
1311 1312 1313 1314 1315 1316
 * @data: data structure created by qemuCgroupEmulatorAllNodesAllow
 *
 * Rolls back the setting done by qemuCgroupEmulatorAllNodesAllow and frees the
 * associated data.
 */
void
1317
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
1318 1319 1320 1321 1322 1323
{
    virErrorPtr err;

    if (!data)
        return;

1324
    virErrorPreserveLast(&err);
1325
    virCgroupSetCpusetMems(data->emulatorCgroup, data->emulatorMemMask);
1326
    virErrorRestore(&err);
1327 1328 1329

    qemuCgroupEmulatorAllNodesDataFree(data);
}