qemu_cgroup.c 38.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 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 "qemu_extdevice.h"
30
#include "vircgroup.h"
31
#include "virlog.h"
32
#include "viralloc.h"
33
#include "virerror.h"
34
#include "domain_audit.h"
35
#include "virscsi.h"
36
#include "virstring.h"
37
#include "virfile.h"
38
#include "virtypedparam.h"
39
#include "virnuma.h"
40
#include "virsystemd.h"
41
#include "virdevmapper.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 50
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
51
    "/dev/rtc", "/dev/hpet", "/dev/sev",
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
#define DEVICE_MAPPER_CONTROL_PATH "/dev/mapper/control"

120 121 122 123 124 125 126 127 128 129 130
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;
    }

131 132 133 134
    if (virStoragePRDefIsManaged(src->pr) &&
        qemuSetupImagePathCgroup(vm, DEVICE_MAPPER_CONTROL_PATH, false) < 0)
        return -1;

135 136 137 138
    return qemuSetupImagePathCgroup(vm, src->path, src->readonly || forceReadonly);
}


139
int
140 141
qemuSetupImageCgroup(virDomainObjPtr vm,
                     virStorageSourcePtr src)
142
{
143
    return qemuSetupImageCgroupInternal(vm, src, false);
144 145 146 147 148 149 150
}


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

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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,
                                      DEVICE_MAPPER_CONTROL_PATH, perms, true);
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny",
                                 DEVICE_MAPPER_CONTROL_PATH,
                                 virCgroupGetDevicePermsString(perms), ret);
        if (ret < 0)
            return ret;
    }


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

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

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

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

202
    return ret;
203 204 205
}


206 207 208
int
qemuSetupDiskCgroup(virDomainObjPtr vm,
                    virDomainDiskDefPtr disk)
209
{
210
    virStorageSourcePtr next;
211
    bool forceReadonly = false;
212

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

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

    return 0;
222 223 224
}


225 226 227
int
qemuTeardownDiskCgroup(virDomainObjPtr vm,
                       virDomainDiskDefPtr disk)
228
{
229
    virStorageSourcePtr next;
230

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

236
    return 0;
237 238
}

239

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

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

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

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

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

260
    return ret;
261 262
}

263 264 265 266 267 268 269 270

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

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

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

    return ret;
}


288
static int
289 290 291
qemuSetupChardevCgroupCB(virDomainDefPtr def ATTRIBUTE_UNUSED,
                         virDomainChrDefPtr dev,
                         void *opaque)
292
{
293 294
    virDomainObjPtr vm = opaque;

295
    return qemuSetupChrSourceCgroup(vm, dev->source);
296 297 298 299
}


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

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

314
    return ret;
315 316
}

317

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

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

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

    return ret;
}


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

    return ret;
}


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

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

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

380 381 382 383 384
    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]),
385
                                 rv);
386 387
        if (rv < 0)
            goto cleanup;
388 389
    }

390
    ret = 0;
391

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

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
405 406 407
    char **path = NULL;
    size_t i, npaths = 0;
    int rv, ret = -1;
408 409 410 411 412 413 414 415 416

    /* 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;

417 418 419 420 421 422 423 424 425 426 427 428
    if (dev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
        dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
        dev->source.subsys.u.pci.backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO &&
        qemuDomainGetHostdevPath(vm->def, dev, true,
                                 &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,
429
                                 "deny", path[i], "rwm", rv);
430 431
        if (rv < 0)
            goto cleanup;
432 433 434
    }

    ret = 0;
435
 cleanup:
436 437
    for (i = 0; i < npaths; i++)
        VIR_FREE(path[i]);
438 439 440 441
    VIR_FREE(path);
    return ret;
}

442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
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",
460
                             mem->nvdimmPath, "rw", rv);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

    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,
482
                             "deny", mem->nvdimmPath, "rwm", rv);
483 484 485 486
    return rv;
}


487 488 489 490 491 492 493 494
static int
qemuSetupGraphicsCgroup(virDomainObjPtr vm,
                        virDomainGraphicsDefPtr gfx)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    const char *rendernode = gfx->data.spice.rendernode;
    int ret;

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

498 499 500 501 502 503 504 505
    if (gfx->type != VIR_DOMAIN_GRAPHICS_TYPE_SPICE ||
        gfx->data.spice.gl != VIR_TRISTATE_BOOL_YES ||
        !rendernode)
        return 0;

    ret = virCgroupAllowDevicePath(priv->cgroup, rendernode,
                                   VIR_CGROUP_DEVICE_RW, false);
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow", rendernode,
506
                             "rw", ret);
507 508 509 510
    return ret;
}


511 512 513 514
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
515
    size_t i;
516 517 518 519 520 521 522 523 524 525 526 527

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

528 529 530
    if (vm->def->blkio.weight != 0 &&
        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
        return -1;
531 532 533

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
534
            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
535 536
            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
537 538 539
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
                                               &dev->weight) < 0))
540 541 542 543
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
544 545 546
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
                                                 &dev->riops) < 0))
547 548 549 550
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
551 552 553
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
                                                  &dev->wiops) < 0))
554 555 556 557
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
558 559 560
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
                                                &dev->rbps) < 0))
561 562 563 564
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
565 566 567
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
                                                 &dev->wbps) < 0))
568 569 570 571 572 573 574
                return -1;
        }
    }

    return 0;
}

575

576 577 578 579 580
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
581
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
582 583 584
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
585 586 587
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
588 589
        } else {
            return 0;
590 591 592
        }
    }

593 594 595
    if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0)
            return -1;
596

597 598 599
    if (virMemoryLimitIsSet(vm->def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0)
            return -1;
600

601 602 603
    if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0)
            return -1;
604 605 606 607 608

    return 0;
}


609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
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;
}


628 629 630 631 632 633 634
int
qemuSetupRNGCgroup(virDomainObjPtr vm,
                   virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

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

638 639 640 641 642 643 644
    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,
645
                                 "rw", rv);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


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

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

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

    return 0;
}


682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
int
qemuSetupChardevCgroup(virDomainObjPtr vm,
                       virDomainChrDefPtr dev)
{
    return qemuSetupChrSourceCgroup(vm, dev->source);
}


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


698
static int
699
qemuSetupDevicesCgroup(virDomainObjPtr vm)
700 701 702 703
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
704
    int rv = -1;
705
    int ret = -1;
706
    size_t i;
707 708 709 710

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

711 712 713 714 715
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
716 717 718 719 720 721 722
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

723 724 725
    if (qemuSetupFirmwareCgroup(vm) < 0)
        goto cleanup;

726
    for (i = 0; i < vm->def->ndisks; i++) {
727 728 729 730
        if (qemuSetupDiskCgroup(vm, vm->def->disks[i]) < 0)
            goto cleanup;
    }

731 732
    rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_PTY_MAJOR, -1,
                              VIR_CGROUP_DEVICE_RW);
733
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
734 735
                              "pty", "rw", rv == 0);
    if (rv < 0)
736 737
        goto cleanup;

738
    cfg = virQEMUDriverGetConfig(priv->driver);
739 740 741 742 743
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
744
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
745 746
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
747
           cfg->vncAllowHostAudio) ||
748
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
749 750
        rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_SND_MAJOR, -1,
                                  VIR_CGROUP_DEVICE_RW);
751
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
752 753
                                  "sound", "rw", rv == 0);
        if (rv < 0)
754 755 756
            goto cleanup;
    }

757
    for (i = 0; deviceACL[i] != NULL; i++) {
758
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
759
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
760 761 762
            continue;
        }

763
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
764
                                      VIR_CGROUP_DEVICE_RW, false);
765
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv);
766 767
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
768 769 770 771 772
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
773
                               qemuSetupChardevCgroupCB,
774 775 776
                               vm) < 0)
        goto cleanup;

777
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
778 779 780
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
781
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
782 783 784
            goto cleanup;
    }

785 786 787 788 789
    for (i = 0; i < vm->def->nmems; i++) {
        if (qemuSetupMemoryDevicesCgroup(vm, vm->def->mems[i]) < 0)
            goto cleanup;
    }

790 791 792 793 794
    for (i = 0; i < vm->def->ngraphics; i++) {
        if (qemuSetupGraphicsCgroup(vm, vm->def->graphics[i]) < 0)
            goto cleanup;
    }

795 796 797 798 799
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

800
    for (i = 0; i < vm->def->nrngs; i++) {
801 802
        if (qemuSetupRNGCgroup(vm, vm->def->rngs[i]) < 0)
            goto cleanup;
803 804
    }

805
    ret = 0;
806
 cleanup:
807 808 809 810 811
    virObjectUnref(cfg);
    return ret;
}


812
int
813
qemuSetupCpusetMems(virDomainObjPtr vm)
814
{
815
    virCgroupPtr cgroup_temp = NULL;
816
    qemuDomainObjPrivatePtr priv = vm->privateData;
817
    virDomainNumatuneMemMode mode;
818
    char *mem_mask = NULL;
819 820 821 822 823
    int ret = -1;

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

824 825
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mode) < 0 ||
        mode != VIR_DOMAIN_NUMATUNE_MEM_STRICT)
826 827
        return 0;

828
    if (virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
829
                                            priv->autoNodeset,
830
                                            &mem_mask, -1) < 0)
831
        goto cleanup;
832

833
    if (mem_mask)
J
John Ferlan 已提交
834 835
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                               false, &cgroup_temp) < 0 ||
836
            virCgroupSetCpusetMems(cgroup_temp, mem_mask) < 0)
837
            goto cleanup;
838

839 840 841
    ret = 0;
 cleanup:
    VIR_FREE(mem_mask);
842
    virCgroupFree(&cgroup_temp);
843 844 845 846 847
    return ret;
}


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

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

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

858
    return 0;
859 860 861
}


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

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

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

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

899
        qemuDomainEventQueue(priv->driver, event);
900
    }
901 902 903 904 905

    return 0;
}


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

915
    if (!virQEMUDriverIsPrivileged(priv->driver))
916 917
        goto done;

918 919 920
    if (!virCgroupAvailable())
        goto done;

921 922
    virCgroupFree(&priv->cgroup);

923
    if (!vm->def->resource) {
924 925
        virDomainResourceDefPtr res;

926
        if (VIR_ALLOC(res) < 0)
927
            goto cleanup;
928

929
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
930 931 932 933 934
            VIR_FREE(res);
            goto cleanup;
        }

        vm->def->resource = res;
935 936
    }

937 938 939 940 941 942
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
943

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

957 958
        goto cleanup;
    }
959

960
 done:
961
    ret = 0;
962
 cleanup:
963 964 965
    virObjectUnref(cfg);
    return ret;
}
966

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

978 979 980 981
    if (!virNumaIsAvailable() ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return;

982
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
983 984 985 986 987 988
        goto error;

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

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
989
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
990 991 992 993 994
        goto error;

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

995
    for (i = 0; i < virDomainDefGetVcpusMax(vm->def); i++) {
996
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(vm->def, i);
997 998 999 1000

        if (!vcpu->online)
            continue;

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

1008
        VIR_FREE(nodeset);
1009 1010 1011
        virCgroupFree(&cgroup_temp);
    }

1012 1013 1014
    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 已提交
1015
                               false, &cgroup_temp) < 0 ||
1016 1017 1018 1019 1020
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

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

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

1032 1033
 cleanup:
    VIR_FREE(mem_mask);
1034
    VIR_FREE(nodeset);
1035
    virBitmapFree(all_nodes);
1036
    virCgroupFree(&cgroup_temp);
1037 1038 1039 1040 1041 1042 1043
    return;

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

int
1046
qemuConnectCgroup(virDomainObjPtr vm)
1047 1048
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1049
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
1050 1051
    int ret = -1;

1052
    if (!virQEMUDriverIsPrivileged(priv->driver))
1053 1054 1055 1056 1057 1058 1059
        goto done;

    if (!virCgroupAvailable())
        goto done;

    virCgroupFree(&priv->cgroup);

1060 1061 1062
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
                                  vm->pid,
1063
                                  cfg->cgroupControllers,
1064
                                  priv->machineName,
1065
                                  &priv->cgroup) < 0)
1066
        goto cleanup;
1067

1068 1069
    qemuRestoreCgroupState(vm);

1070
 done:
1071
    ret = 0;
1072
 cleanup:
1073
    virObjectUnref(cfg);
1074
    return ret;
1075 1076
}

1077
int
1078
qemuSetupCgroup(virDomainObjPtr vm,
1079 1080
                size_t nnicindexes,
                int *nicindexes)
1081
{
1082
    qemuDomainObjPrivatePtr priv = vm->privateData;
1083
    int ret = -1;
1084

1085 1086 1087 1088 1089 1090
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

1091
    if (qemuInitCgroup(vm, nnicindexes, nicindexes) < 0)
1092
        return -1;
1093

1094
    if (!priv->cgroup)
1095
        return 0;
1096

1097
    if (qemuSetupDevicesCgroup(vm) < 0)
1098
        goto cleanup;
1099

1100 1101
    if (qemuSetupBlkioCgroup(vm) < 0)
        goto cleanup;
1102

1103 1104
    if (qemuSetupMemoryCgroup(vm) < 0)
        goto cleanup;
1105

1106
    if (qemuSetupCpuCgroup(vm) < 0)
1107
        goto cleanup;
1108

1109
    if (qemuSetupCpusetCgroup(vm) < 0)
1110
        goto cleanup;
1111

1112
    ret = 0;
1113
 cleanup:
1114
    return ret;
1115 1116
}

1117 1118 1119 1120
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
1121 1122 1123 1124 1125 1126 1127 1128
{
    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 */
1129
        if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
1130 1131
            return -1;

1132
        if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
1133 1134 1135
            return -1;
    }

1136 1137 1138
    if (quota &&
        virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
        goto error;
1139 1140 1141

    return 0;

1142
 error:
1143
    if (period) {
1144 1145 1146 1147 1148 1149
        virErrorPtr saved = virSaveLastError();
        ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
        if (saved) {
            virSetError(saved);
            virFreeError(saved);
        }
1150 1151 1152 1153 1154
    }

    return -1;
}

1155

1156
int
1157 1158
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
1159
{
1160
    int ret = -1;
1161 1162
    char *new_cpus = NULL;

1163
    if (!(new_cpus = virBitmapFormat(cpumask)))
1164 1165
        goto cleanup;

1166
    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
1167 1168
        goto cleanup;

1169
    ret = 0;
1170
 cleanup:
1171
    VIR_FREE(new_cpus);
1172
    return ret;
1173 1174
}

1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
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:
    virCgroupFree(&cgroup_temp);

    return ret;
}


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 1251 1252 1253 1254 1255 1256 1257 1258
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;
}


1259
int
1260
qemuRemoveCgroup(virDomainObjPtr vm)
1261
{
1262
    qemuDomainObjPrivatePtr priv = vm->privateData;
1263

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

1267
    if (virCgroupTerminateMachine(priv->machineName) < 0) {
1268 1269 1270 1271
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

1272
    return virCgroupRemove(priv->cgroup);
1273
}
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295


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

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


/**
 * qemuCgroupEmulatorAllNodesAllow:
 * @cgroup: domain cgroup pointer
 * @retData: filled with structure used to roll back the operation
 *
 * Allows all NUMA nodes for the qemu emulator thread temporarily. This is
 * necessary when hotplugging cpus since it requires memory allocated in the
 * DMA region. Afterwards the operation can be reverted by
1296
 * qemuCgroupEmulatorAllNodesRestore.
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
 *
 * 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;

1313
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
        goto cleanup;

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

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

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

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

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

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

    return ret;
}


/**
1343
 * qemuCgroupEmulatorAllNodesRestore:
1344 1345 1346 1347 1348 1349
 * @data: data structure created by qemuCgroupEmulatorAllNodesAllow
 *
 * Rolls back the setting done by qemuCgroupEmulatorAllNodesAllow and frees the
 * associated data.
 */
void
1350
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
{
    virErrorPtr err;

    if (!data)
        return;

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

    qemuCgroupEmulatorAllNodesDataFree(data);
}