qemu_cgroup.c 38.0 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 "virlog.h"
29
#include "viralloc.h"
30
#include "virerror.h"
31
#include "domain_audit.h"
32
#include "virscsi.h"
33
#include "virstring.h"
34
#include "virfile.h"
35
#include "virtypedparam.h"
36
#include "virnuma.h"
37
#include "virsystemd.h"
38
#include "virdevmapper.h"
39 40 41

#define VIR_FROM_THIS VIR_FROM_QEMU

42 43
VIR_LOG_INIT("qemu.qemu_cgroup");

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

54

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

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

70
    if (!readonly)
71
        perms |= VIR_CGROUP_DEVICE_WRITE;
72

73
    VIR_DEBUG("Allow path %s, perms: %s",
74
              path, virCgroupGetDevicePermsString(perms));
75

76
    rv = virCgroupAllowDevicePath(priv->cgroup, path, perms, true);
77

78
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path,
79
                             virCgroupGetDevicePermsString(perms),
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                             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;
    }
97

98 99 100 101 102 103 104 105 106 107 108 109 110
    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);
111
    return ret;
112 113 114
}


115 116 117 118 119 120 121 122 123 124 125
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;
    }

126
    if (virStoragePRDefIsManaged(src->pr) &&
127 128
        virFileExists(QEMU_DEVICE_MAPPER_CONTROL_PATH) &&
        qemuSetupImagePathCgroup(vm, QEMU_DEVICE_MAPPER_CONTROL_PATH, false) < 0)
129 130
        return -1;

131 132 133 134
    return qemuSetupImagePathCgroup(vm, src->path, src->readonly || forceReadonly);
}


135
int
136 137
qemuSetupImageCgroup(virDomainObjPtr vm,
                     virStorageSourcePtr src)
138
{
139
    return qemuSetupImageCgroupInternal(vm, src, false);
140 141 142 143 144 145 146
}


int
qemuTeardownImageCgroup(virDomainObjPtr vm,
                        virStorageSourcePtr src)
{
147
    qemuDomainObjPrivatePtr priv = vm->privateData;
148 149
    int perms = VIR_CGROUP_DEVICE_RWM;
    size_t i;
150 151 152 153 154 155 156 157 158 159 160 161
    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;
    }

162
    if (virFileExists(QEMU_DEVICE_MAPPER_CONTROL_PATH)) {
163 164 165 166 167 168 169 170 171 172 173 174 175
        for (i = 0; i < vm->def->ndisks; i++) {
            virStorageSourcePtr diskSrc = vm->def->disks[i]->src;

            if (src == diskSrc)
                continue;

            if (virStoragePRDefIsManaged(diskSrc->pr))
                break;
        }

        if (i == vm->def->ndisks) {
            VIR_DEBUG("Disabling device mapper control");
            ret = virCgroupDenyDevicePath(priv->cgroup,
176 177
                                          QEMU_DEVICE_MAPPER_CONTROL_PATH,
                                          perms, true);
178
            virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
179
                                     QEMU_DEVICE_MAPPER_CONTROL_PATH,
180 181 182 183
                                     virCgroupGetDevicePermsString(perms), ret);
            if (ret < 0)
                return ret;
        }
184 185
    }

186 187 188 189 190
    VIR_DEBUG("Deny path %s", src->path);

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

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

193 194 195 196 197 198 199
    /* 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. */

200
    return ret;
201 202 203
}


204
int
205 206
qemuSetupImageChainCgroup(virDomainObjPtr vm,
                          virStorageSourcePtr src)
207
{
208
    virStorageSourcePtr next;
209
    bool forceReadonly = false;
210

211
    for (next = src; virStorageSourceIsBacking(next); next = next->backingStore) {
212
        if (qemuSetupImageCgroupInternal(vm, next, forceReadonly) < 0)
213
            return -1;
214 215 216

        /* setup only the top level image for read-write */
        forceReadonly = true;
217
    }
218 219

    return 0;
220 221 222
}


223
int
224 225
qemuTeardownImageChainCgroup(virDomainObjPtr vm,
                             virStorageSourcePtr src)
226
{
227
    virStorageSourcePtr next;
228

229
    for (next = src; virStorageSourceIsBacking(next); next = next->backingStore) {
230
        if (qemuTeardownImageCgroup(vm, next) < 0)
231 232
            return -1;
    }
233

234
    return 0;
235 236
}

237

238
static int
239
qemuSetupChrSourceCgroup(virDomainObjPtr vm,
240
                         virDomainChrSourceDefPtr source)
241
{
242
    qemuDomainObjPrivatePtr priv = vm->privateData;
243
    int ret;
244

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

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

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

253
    ret = virCgroupAllowDevicePath(priv->cgroup, source->data.file.path,
254
                                   VIR_CGROUP_DEVICE_RW, false);
255
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
256
                             source->data.file.path, "rw", ret);
257

258
    return ret;
259 260
}

261 262 263 264 265 266 267 268

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

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

272 273 274 275 276 277 278 279
    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",
280
                             source->data.file.path, "rw", ret);
281 282 283 284 285

    return ret;
}


286
static int
J
Ján Tomko 已提交
287
qemuSetupChardevCgroupCB(virDomainDefPtr def G_GNUC_UNUSED,
288 289
                         virDomainChrDefPtr dev,
                         void *opaque)
290
{
291 292
    virDomainObjPtr vm = opaque;

293
    return qemuSetupChrSourceCgroup(vm, dev->source);
294 295 296 297
}


static int
298
qemuSetupTPMCgroup(virDomainObjPtr vm)
299
{
300
    int ret = 0;
301
    virDomainTPMDefPtr dev = vm->def->tpm;
302 303 304

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
305
        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
306
        break;
307
    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
308 309 310 311
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

312
    return ret;
313 314
}

315

316
int
317 318 319 320 321 322
qemuSetupInputCgroup(virDomainObjPtr vm,
                     virDomainInputDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = 0;

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

326 327 328 329
    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,
330
                                       VIR_CGROUP_DEVICE_RW, false);
331
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", dev->source.evdev, "rw", ret);
332 333 334 335 336 337 338
        break;
    }

    return ret;
}


339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
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);
354
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny", dev->source.evdev, "rwm", ret);
355 356 357 358 359 360 361
        break;
    }

    return ret;
}


362
int
363
qemuSetupHostdevCgroup(virDomainObjPtr vm,
364 365 366
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
367 368 369 370
    char **path = NULL;
    int *perms = NULL;
    size_t i, npaths = 0;
    int rv, ret = -1;
371

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

375
    if (qemuDomainGetHostdevPath(NULL, dev, false, &npaths, &path, &perms) < 0)
376
        goto cleanup;
377

378 379 380 381 382
    for (i = 0; i < npaths; i++) {
        VIR_DEBUG("Cgroup allow %s perms=%d", path[i], perms[i]);
        rv = virCgroupAllowDevicePath(priv->cgroup, path[i], perms[i], false);
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", path[i],
                                 virCgroupGetDevicePermsString(perms[i]),
383
                                 rv);
384 385
        if (rv < 0)
            goto cleanup;
386 387
    }

388
    ret = 0;
389

390
 cleanup:
391 392
    for (i = 0; i < npaths; i++)
        VIR_FREE(path[i]);
393
    VIR_FREE(path);
394
    VIR_FREE(perms);
395 396 397 398 399 400 401 402
    return ret;
}

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
403 404 405
    char **path = NULL;
    size_t i, npaths = 0;
    int rv, ret = -1;
406 407 408 409

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

410
    if (qemuDomainGetHostdevPath(vm->def, dev, true,
411 412 413 414 415 416 417 418
                                 &npaths, &path, NULL) < 0)
        goto cleanup;

    for (i = 0; i < npaths; i++) {
        VIR_DEBUG("Cgroup deny %s", path[i]);
        rv = virCgroupDenyDevicePath(priv->cgroup, path[i],
                                     VIR_CGROUP_DEVICE_RWM, false);
        virDomainAuditCgroupPath(vm, priv->cgroup,
419
                                 "deny", path[i], "rwm", rv);
420 421
        if (rv < 0)
            goto cleanup;
422 423 424
    }

    ret = 0;
425
 cleanup:
426 427
    for (i = 0; i < npaths; i++)
        VIR_FREE(path[i]);
428 429 430 431
    VIR_FREE(path);
    return ret;
}

432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
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",
450
                             mem->nvdimmPath, "rw", rv);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

    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,
472
                             "deny", mem->nvdimmPath, "rwm", rv);
473 474 475 476
    return rv;
}


477 478 479 480 481
static int
qemuSetupGraphicsCgroup(virDomainObjPtr vm,
                        virDomainGraphicsDefPtr gfx)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
482
    const char *rendernode = virDomainGraphicsGetRenderNode(gfx);
483 484
    int ret;

485 486
    if (!rendernode ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
487 488 489 490 491
        return 0;

    ret = virCgroupAllowDevicePath(priv->cgroup, rendernode,
                                   VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", rendernode,
492
                             "rw", ret);
493 494 495 496
    return ret;
}


497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
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;
}


520 521 522 523
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
524
    size_t i;
525 526 527 528 529 530 531 532 533 534 535 536

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

537 538 539
    if (vm->def->blkio.weight != 0 &&
        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
        return -1;
540 541 542

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
543
            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
544 545
            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
546 547 548
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
                                               &dev->weight) < 0))
549 550 551 552
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
553 554 555
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
                                                 &dev->riops) < 0))
556 557 558 559
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
560 561 562
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
                                                  &dev->wiops) < 0))
563 564 565 566
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
567 568 569
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
                                                &dev->rbps) < 0))
570 571 572 573
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
574 575 576
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
                                                 &dev->wbps) < 0))
577 578 579 580 581 582 583
                return -1;
        }
    }

    return 0;
}

584

585 586 587 588 589
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
590
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
591 592 593
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
594 595 596
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
597 598
        } else {
            return 0;
599 600 601
        }
    }

602 603 604
    if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0)
            return -1;
605

606 607 608
    if (virMemoryLimitIsSet(vm->def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0)
            return -1;
609

610 611 612
    if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0)
            return -1;
613 614 615 616 617

    return 0;
}


618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
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;
}


637 638 639 640 641 642 643
int
qemuSetupRNGCgroup(virDomainObjPtr vm,
                   virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

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

647 648 649 650 651 652 653
    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,
654
                                 "rw", rv);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


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

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

674 675 676 677 678 679 680
    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,
681
                                 "rw", rv);
682 683 684 685 686 687 688 689 690
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
int
qemuSetupChardevCgroup(virDomainObjPtr vm,
                       virDomainChrDefPtr dev)
{
    return qemuSetupChrSourceCgroup(vm, dev->source);
}


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


707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
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;
}

723
static int
724
qemuSetupDevicesCgroup(virDomainObjPtr vm)
725 726 727 728
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
729
    int rv = -1;
730
    int ret = -1;
731
    size_t i;
732 733 734 735

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

736 737 738 739 740
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
741 742 743 744 745 746 747
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

748 749 750
    if (qemuSetupFirmwareCgroup(vm) < 0)
        goto cleanup;

751
    for (i = 0; i < vm->def->ndisks; i++) {
752
        if (qemuSetupImageChainCgroup(vm, vm->def->disks[i]->src) < 0)
753 754 755
            goto cleanup;
    }

756 757
    rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_PTY_MAJOR, -1,
                              VIR_CGROUP_DEVICE_RW);
758
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
759 760
                              "pty", "rw", rv == 0);
    if (rv < 0)
761 762
        goto cleanup;

763
    cfg = virQEMUDriverGetConfig(priv->driver);
764 765 766 767 768
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
769
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
770 771
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
772
           cfg->vncAllowHostAudio) ||
773
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
774 775
        rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_SND_MAJOR, -1,
                                  VIR_CGROUP_DEVICE_RW);
776
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
777 778
                                  "sound", "rw", rv == 0);
        if (rv < 0)
779 780 781
            goto cleanup;
    }

782
    for (i = 0; deviceACL[i] != NULL; i++) {
783
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
784
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
785 786 787
            continue;
        }

788
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
789
                                      VIR_CGROUP_DEVICE_RW, false);
790
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv);
791 792
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
793 794 795 796 797
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
798
                               qemuSetupChardevCgroupCB,
799 800 801
                               vm) < 0)
        goto cleanup;

802
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
803 804 805
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
806
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
807 808 809
            goto cleanup;
    }

810 811 812 813 814
    for (i = 0; i < vm->def->nmems; i++) {
        if (qemuSetupMemoryDevicesCgroup(vm, vm->def->mems[i]) < 0)
            goto cleanup;
    }

815 816 817
    for (i = 0; i < vm->def->ngraphics; i++) {
        if (qemuSetupGraphicsCgroup(vm, vm->def->graphics[i]) < 0)
            goto cleanup;
818 819 820 821 822
    }

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

825 826 827 828 829
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

830
    for (i = 0; i < vm->def->nrngs; i++) {
831 832
        if (qemuSetupRNGCgroup(vm, vm->def->rngs[i]) < 0)
            goto cleanup;
833 834
    }

835 836 837
    if (vm->def->sev && qemuSetupSEVCgroup(vm) < 0)
        goto cleanup;

838
    ret = 0;
839
 cleanup:
840 841 842 843 844
    virObjectUnref(cfg);
    return ret;
}


845
static int
846
qemuSetupCpusetCgroup(virDomainObjPtr vm)
847 848 849 850 851 852
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

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

853 854 855
    if (virCgroupSetCpusetMemoryMigrate(priv->cgroup, true) < 0)
        return -1;

856
    return 0;
857 858 859
}


860
static int
861
qemuSetupCpuCgroup(virDomainObjPtr vm)
862 863
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
864 865 866 867
    virObjectEventPtr event = NULL;
    virTypedParameterPtr eventParams = NULL;
    int eventNparams = 0;
    int eventMaxparams = 0;
868 869

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
870
       if (vm->def->cputune.sharesSpecified) {
871 872 873 874 875 876 877 878
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

879 880 881 882 883 884 885
    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;
886 887 888 889
        if (vm->def->cputune.shares != val) {
            vm->def->cputune.shares = val;
            if (virTypedParamsAddULLong(&eventParams, &eventNparams,
                                        &eventMaxparams,
890
                                        VIR_DOMAIN_TUNABLE_CPU_CPU_SHARES,
891 892 893 894 895 896
                                        val) < 0)
                return -1;

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

897
        virObjectEventStateQueue(priv->driver->domainEventState, event);
898
    }
899 900 901 902 903

    return 0;
}


904
static int
905
qemuInitCgroup(virDomainObjPtr vm,
906 907
               size_t nnicindexes,
               int *nicindexes)
908
{
909
    int ret = -1;
910
    qemuDomainObjPrivatePtr priv = vm->privateData;
911
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
912

913
    if (!virQEMUDriverIsPrivileged(priv->driver))
914 915
        goto done;

916 917 918
    if (!virCgroupAvailable())
        goto done;

919
    virCgroupFree(&priv->cgroup);
920

921
    if (!vm->def->resource) {
922 923
        virDomainResourceDefPtr res;

924
        if (VIR_ALLOC(res) < 0)
925
            goto cleanup;
926

927
        res->partition = g_strdup("/machine");
928 929

        vm->def->resource = res;
930 931
    }

932 933 934 935 936 937
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
938

939
    if (virCgroupNewMachine(priv->machineName,
940 941 942 943 944
                            "qemu",
                            vm->def->uuid,
                            NULL,
                            vm->pid,
                            false,
945
                            nnicindexes, nicindexes,
946 947
                            vm->def->resource->partition,
                            cfg->cgroupControllers,
948
                            cfg->maxThreadsPerProc,
949
                            &priv->cgroup) < 0) {
950 951
        if (virCgroupNewIgnoreError())
            goto done;
952

953 954
        goto cleanup;
    }
955

956
 done:
957
    ret = 0;
958
 cleanup:
959 960 961
    virObjectUnref(cfg);
    return ret;
}
962

963 964 965
static void
qemuRestoreCgroupState(virDomainObjPtr vm)
{
966
    char *mem_mask = NULL;
967
    char *nodeset = NULL;
968 969
    int empty = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
970
    size_t i = 0;
971
    virBitmapPtr all_nodes;
972
    virCgroupPtr cgroup_temp = NULL;
973

974 975 976 977
    if (!virNumaIsAvailable() ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return;

978
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
979 980 981 982 983 984
        goto error;

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

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
985
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
986 987 988 989 990
        goto error;

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

991
    for (i = 0; i < virDomainDefGetVcpusMax(vm->def); i++) {
992
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(vm->def, i);
993 994 995 996

        if (!vcpu->online)
            continue;

J
John Ferlan 已提交
997 998
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               false, &cgroup_temp) < 0 ||
999 1000 1001 1002 1003
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

1004
        VIR_FREE(nodeset);
1005
        virCgroupFree(&cgroup_temp);
1006 1007
    }

1008 1009 1010
    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 已提交
1011
                               false, &cgroup_temp) < 0 ||
1012 1013 1014 1015 1016
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

1017
        VIR_FREE(nodeset);
1018
        virCgroupFree(&cgroup_temp);
1019 1020
    }

J
John Ferlan 已提交
1021 1022
    if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                           false, &cgroup_temp) < 0 ||
1023 1024 1025 1026 1027
        virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
        virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
        virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
        goto cleanup;

1028 1029
 cleanup:
    VIR_FREE(mem_mask);
1030
    VIR_FREE(nodeset);
1031
    virBitmapFree(all_nodes);
1032
    virCgroupFree(&cgroup_temp);
1033 1034 1035 1036 1037 1038 1039
    return;

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

int
1042
qemuConnectCgroup(virDomainObjPtr vm)
1043 1044
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1045
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
1046 1047
    int ret = -1;

1048
    if (!virQEMUDriverIsPrivileged(priv->driver))
1049 1050 1051 1052 1053
        goto done;

    if (!virCgroupAvailable())
        goto done;

1054
    virCgroupFree(&priv->cgroup);
1055

1056 1057 1058
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
                                  vm->pid,
1059
                                  cfg->cgroupControllers,
1060
                                  priv->machineName,
1061
                                  &priv->cgroup) < 0)
1062
        goto cleanup;
1063

1064 1065
    qemuRestoreCgroupState(vm);

1066
 done:
1067
    ret = 0;
1068
 cleanup:
1069
    virObjectUnref(cfg);
1070
    return ret;
1071 1072
}

1073
int
1074
qemuSetupCgroup(virDomainObjPtr vm,
1075 1076
                size_t nnicindexes,
                int *nicindexes)
1077
{
1078
    qemuDomainObjPrivatePtr priv = vm->privateData;
1079

1080 1081 1082 1083 1084 1085
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

1086
    if (qemuInitCgroup(vm, nnicindexes, nicindexes) < 0)
1087
        return -1;
1088

1089
    if (!priv->cgroup)
1090
        return 0;
1091

1092
    if (qemuSetupDevicesCgroup(vm) < 0)
1093
        return -1;
1094

1095
    if (qemuSetupBlkioCgroup(vm) < 0)
1096
        return -1;
1097

1098
    if (qemuSetupMemoryCgroup(vm) < 0)
1099
        return -1;
1100

1101
    if (qemuSetupCpuCgroup(vm) < 0)
1102
        return -1;
1103

1104
    if (qemuSetupCpusetCgroup(vm) < 0)
1105
        return -1;
1106

1107
    return 0;
1108 1109
}

1110 1111 1112 1113
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
1114 1115 1116 1117 1118 1119 1120 1121
{
    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 */
1122
        if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
1123 1124
            return -1;

1125
        if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
1126 1127 1128
            return -1;
    }

1129 1130 1131
    if (quota &&
        virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
        goto error;
1132 1133 1134

    return 0;

1135
 error:
1136
    if (period) {
1137 1138 1139
        virErrorPtr saved;

        virErrorPreserveLast(&saved);
1140
        ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
1141
        virErrorRestore(&saved);
1142 1143 1144 1145 1146
    }

    return -1;
}

1147

1148
int
1149 1150
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
1151
{
1152
    int ret = -1;
1153 1154
    char *new_cpus = NULL;

1155
    if (!(new_cpus = virBitmapFormat(cpumask)))
1156 1157
        goto cleanup;

1158
    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
1159 1160
        goto cleanup;

1161
    ret = 0;
1162
 cleanup:
1163
    VIR_FREE(new_cpus);
1164
    return ret;
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
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:
1196
    virCgroupFree(&cgroup_temp);
1197 1198 1199 1200 1201

    return ret;
}


1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
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;
}


1251
int
1252
qemuRemoveCgroup(virDomainObjPtr vm)
1253
{
1254
    qemuDomainObjPrivatePtr priv = vm->privateData;
1255

1256
    if (priv->cgroup == NULL)
1257 1258
        return 0; /* Not supported, so claim success */

1259
    if (virCgroupTerminateMachine(priv->machineName) < 0) {
1260 1261 1262 1263
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

1264
    return virCgroupRemove(priv->cgroup);
1265
}
1266 1267 1268 1269 1270 1271 1272 1273


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

1274
    virCgroupFree(&data->emulatorCgroup);
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    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
1288
 * qemuCgroupEmulatorAllNodesRestore.
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
 *
 * 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;

1305
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
        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;

1322
    *retData = g_steal_pointer(&data);
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    ret = 0;

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

    return ret;
}


/**
1335
 * qemuCgroupEmulatorAllNodesRestore:
1336 1337 1338 1339 1340 1341
 * @data: data structure created by qemuCgroupEmulatorAllNodesAllow
 *
 * Rolls back the setting done by qemuCgroupEmulatorAllNodesAllow and frees the
 * associated data.
 */
void
1342
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
1343 1344 1345 1346 1347 1348
{
    virErrorPtr err;

    if (!data)
        return;

1349
    virErrorPreserveLast(&err);
1350
    virCgroupSetCpusetMems(data->emulatorCgroup, data->emulatorMemMask);
1351
    virErrorRestore(&err);
1352 1353 1354

    qemuCgroupEmulatorAllNodesDataFree(data);
}