qemu_cgroup.c 24.2 KB
Newer Older
1 2 3
/*
 * qemu_cgroup.c: QEMU cgroup management
 *
4
 * Copyright (C) 2006-2012 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
O
Osier Yang 已提交
18 19
 * License along with this library;  If not, see
 * <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 29 30 31 32
#include "cgroup.h"
#include "logging.h"
#include "memory.h"
#include "virterror_internal.h"
#include "util.h"
33
#include "domain_audit.h"
34 35 36 37 38 39 40

#define VIR_FROM_THIS VIR_FROM_QEMU

static const char *const defaultDeviceACL[] = {
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
41
    "/dev/rtc", "/dev/hpet",
42 43 44 45 46
    NULL,
};
#define DEVICE_PTY_MAJOR 136
#define DEVICE_SND_MAJOR 116

47 48
bool qemuCgroupControllerActive(struct qemud_driver *driver,
                                int controller)
49 50
{
    if (driver->cgroup == NULL)
51 52 53
        return false;
    if (controller < 0 || controller >= VIR_CGROUP_CONTROLLER_LAST)
        return false;
E
Eric Blake 已提交
54 55
    if (!virCgroupMounted(driver->cgroup, controller))
        return false;
56
    if (driver->cgroupControllers & (1 << controller))
57 58
        return true;
    return false;
59 60
}

61
static int
62
qemuSetupDiskPathAllow(virDomainDiskDefPtr disk,
63 64 65
                       const char *path,
                       size_t depth ATTRIBUTE_UNUSED,
                       void *opaque)
66
{
67
    qemuCgroupData *data = opaque;
68 69 70
    int rc;

    VIR_DEBUG("Process path %s for disk", path);
71 72 73
    rc = virCgroupAllowDevicePath(data->cgroup, path,
                                  (disk->readonly ? VIR_CGROUP_DEVICE_READ
                                   : VIR_CGROUP_DEVICE_RW));
74 75
    virDomainAuditCgroupPath(data->vm, data->cgroup, "allow", path,
                             disk->readonly ? "r" : "rw", rc);
76 77
    if (rc < 0) {
        if (rc == -EACCES) { /* Get this for root squash NFS */
78 79 80 81 82 83 84 85 86 87 88 89 90
            VIR_DEBUG("Ignoring EACCES for %s", path);
        } else {
            virReportSystemError(-rc,
                                 _("Unable to allow access for disk path %s"),
                                 path);
            return -1;
        }
    }
    return 0;
}


int qemuSetupDiskCgroup(struct qemud_driver *driver,
91
                        virDomainObjPtr vm,
92 93 94
                        virCgroupPtr cgroup,
                        virDomainDiskDefPtr disk)
{
95
    qemuCgroupData data = { vm, cgroup };
96 97 98
    return virDomainDiskDefForeachPath(disk,
                                       driver->allowDiskFormatProbing,
                                       true,
99
                                       driver->user, driver->group,
100
                                       qemuSetupDiskPathAllow,
101
                                       &data);
102 103 104
}


105 106 107 108 109
static int
qemuTeardownDiskPathDeny(virDomainDiskDefPtr disk ATTRIBUTE_UNUSED,
                         const char *path,
                         size_t depth ATTRIBUTE_UNUSED,
                         void *opaque)
110
{
111
    qemuCgroupData *data = opaque;
112 113 114
    int rc;

    VIR_DEBUG("Process path %s for disk", path);
115 116
    rc = virCgroupDenyDevicePath(data->cgroup, path,
                                 VIR_CGROUP_DEVICE_RWM);
117
    virDomainAuditCgroupPath(data->vm, data->cgroup, "deny", path, "rwm", rc);
118 119
    if (rc < 0) {
        if (rc == -EACCES) { /* Get this for root squash NFS */
120 121 122 123 124 125 126 127 128 129 130 131 132
            VIR_DEBUG("Ignoring EACCES for %s", path);
        } else {
            virReportSystemError(-rc,
                                 _("Unable to deny access for disk path %s"),
                                 path);
            return -1;
        }
    }
    return 0;
}


int qemuTeardownDiskCgroup(struct qemud_driver *driver,
133
                           virDomainObjPtr vm,
134 135 136
                           virCgroupPtr cgroup,
                           virDomainDiskDefPtr disk)
{
137
    qemuCgroupData data = { vm, cgroup };
138 139 140
    return virDomainDiskDefForeachPath(disk,
                                       driver->allowDiskFormatProbing,
                                       true,
141
                                       driver->user, driver->group,
142
                                       qemuTeardownDiskPathDeny,
143
                                       &data);
144 145 146
}


147 148 149 150
static int
qemuSetupChardevCgroup(virDomainDefPtr def,
                       virDomainChrDefPtr dev,
                       void *opaque)
151
{
152
    qemuCgroupData *data = opaque;
153 154
    int rc;

155
    if (dev->source.type != VIR_DOMAIN_CHR_TYPE_DEV)
156 157 158
        return 0;


159
    VIR_DEBUG("Process path '%s' for disk", dev->source.data.file.path);
160 161
    rc = virCgroupAllowDevicePath(data->cgroup, dev->source.data.file.path,
                                  VIR_CGROUP_DEVICE_RW);
162 163
    virDomainAuditCgroupPath(data->vm, data->cgroup, "allow",
                             dev->source.data.file.path, "rw", rc);
164
    if (rc < 0) {
165 166
        virReportSystemError(-rc,
                             _("Unable to allow device %s for %s"),
167
                             dev->source.data.file.path, def->name);
168 169 170 171 172 173 174 175 176 177 178
        return -1;
    }

    return 0;
}


int qemuSetupHostUsbDeviceCgroup(usbDevice *dev ATTRIBUTE_UNUSED,
                                 const char *path,
                                 void *opaque)
{
179
    qemuCgroupData *data = opaque;
180 181 182
    int rc;

    VIR_DEBUG("Process path '%s' for USB device", path);
183 184
    rc = virCgroupAllowDevicePath(data->cgroup, path,
                                  VIR_CGROUP_DEVICE_RW);
185
    virDomainAuditCgroupPath(data->vm, data->cgroup, "allow", path, "rw", rc);
186
    if (rc < 0) {
187 188 189 190 191 192 193 194 195 196
        virReportSystemError(-rc,
                             _("Unable to allow device %s"),
                             path);
        return -1;
    }

    return 0;
}

int qemuSetupCgroup(struct qemud_driver *driver,
197 198
                    virDomainObjPtr vm,
                    char *nodemask)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
{
    virCgroupPtr cgroup = NULL;
    int rc;
    unsigned int i;
    const char *const *deviceACL =
        driver->cgroupDeviceACL ?
        (const char *const *)driver->cgroupDeviceACL :
        defaultDeviceACL;

    if (driver->cgroup == NULL)
        return 0; /* Not supported, so claim success */

    rc = virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 1);
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("Unable to create cgroup for %s"),
                             vm->def->name);
        goto cleanup;
    }

    if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_DEVICES)) {
220
        qemuCgroupData data = { vm, cgroup };
221
        rc = virCgroupDenyAllDevices(cgroup);
222
        virDomainAuditCgroup(vm, cgroup, "deny", "all", rc == 0);
223 224
        if (rc != 0) {
            if (rc == -EPERM) {
225
                VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
226 227 228 229 230 231 232 233 234
                goto done;
            }

            virReportSystemError(-rc,
                                 _("Unable to deny all devices for %s"), vm->def->name);
            goto cleanup;
        }

        for (i = 0; i < vm->def->ndisks ; i++) {
235
            if (qemuSetupDiskCgroup(driver, vm, cgroup, vm->def->disks[i]) < 0)
236 237 238
                goto cleanup;
        }

239 240
        rc = virCgroupAllowDeviceMajor(cgroup, 'c', DEVICE_PTY_MAJOR,
                                       VIR_CGROUP_DEVICE_RW);
241 242
        virDomainAuditCgroupMajor(vm, cgroup, "allow", DEVICE_PTY_MAJOR,
                                  "pty", "rw", rc == 0);
243 244 245 246 247 248
        if (rc != 0) {
            virReportSystemError(-rc, "%s",
                                 _("unable to allow /dev/pts/ devices"));
            goto cleanup;
        }

249 250 251 252 253
        if (vm->def->nsounds &&
            (!vm->def->ngraphics ||
             ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
               driver->vncAllowHostAudio) ||
              (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL)))) {
254 255
            rc = virCgroupAllowDeviceMajor(cgroup, 'c', DEVICE_SND_MAJOR,
                                           VIR_CGROUP_DEVICE_RW);
256 257
            virDomainAuditCgroupMajor(vm, cgroup, "allow", DEVICE_SND_MAJOR,
                                      "sound", "rw", rc == 0);
258 259 260 261 262 263 264 265
            if (rc != 0) {
                virReportSystemError(-rc, "%s",
                                     _("unable to allow /dev/snd/ devices"));
                goto cleanup;
            }
        }

        for (i = 0; deviceACL[i] != NULL ; i++) {
266 267
            rc = virCgroupAllowDevicePath(cgroup, deviceACL[i],
                                          VIR_CGROUP_DEVICE_RW);
268
            virDomainAuditCgroupPath(vm, cgroup, "allow", deviceACL[i], "rw", rc);
269 270 271 272 273 274 275 276 277 278 279 280
            if (rc < 0 &&
                rc != -ENOENT) {
                virReportSystemError(-rc,
                                     _("unable to allow device %s"),
                                     deviceACL[i]);
                goto cleanup;
            }
        }

        if (virDomainChrDefForeach(vm->def,
                                   true,
                                   qemuSetupChardevCgroup,
281
                                   &data) < 0)
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
            goto cleanup;

        for (i = 0; i < vm->def->nhostdevs; i++) {
            virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
            usbDevice *usb;

            if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
                continue;
            if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
                continue;

            if ((usb = usbGetDevice(hostdev->source.subsys.u.usb.bus,
                                    hostdev->source.subsys.u.usb.device)) == NULL)
                goto cleanup;

297 298
            if (usbDeviceFileIterate(usb, qemuSetupHostUsbDeviceCgroup,
                                     &data) < 0)
299 300 301 302
                goto cleanup;
        }
    }

303 304
    if (vm->def->blkio.weight != 0) {
        if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_BLKIO)) {
305 306 307 308 309 310 311
            rc = virCgroupSetBlkioWeight(cgroup, vm->def->blkio.weight);
            if(rc != 0) {
                virReportSystemError(-rc,
                                     _("Unable to set io weight for domain %s"),
                                     vm->def->name);
                goto cleanup;
            }
312
        } else {
313 314
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Block I/O tuning is not available on this host"));
315 316 317 318 319 320 321 322
            goto cleanup;
        }
    }

    if (vm->def->blkio.ndevices) {
        if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_BLKIO)) {
            for (i = 0; i < vm->def->blkio.ndevices; i++) {
                virBlkioDeviceWeightPtr dw = &vm->def->blkio.devices[i];
323 324
                if (!dw->weight)
                    continue;
325 326 327 328 329 330 331 332 333 334 335
                rc = virCgroupSetBlkioDeviceWeight(cgroup, dw->path,
                                                   dw->weight);
                if (rc != 0) {
                    virReportSystemError(-rc,
                                         _("Unable to set io device weight "
                                           "for domain %s"),
                                         vm->def->name);
                    goto cleanup;
                }
            }
        } else {
336
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
337
                           _("Block I/O tuning is not available on this host"));
338
            goto cleanup;
339 340 341
        }
    }

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_MEMORY)) {
        unsigned long long hard_limit = vm->def->mem.hard_limit;

        if (!hard_limit) {
            /* If there is no hard_limit set, set a reasonable
             * one to avoid system trashing caused by exploited qemu.
             * As 'reasonable limit' has been chosen:
             *     (1 + k) * (domain memory + total video memory) + F
             * where k = 0.02 and F = 200MB. */
            hard_limit = vm->def->mem.max_balloon;
            for (i = 0; i < vm->def->nvideos; i++)
                hard_limit += vm->def->videos[i]->vram;
            hard_limit = hard_limit * 1.02 + 204800;
        }

        rc = virCgroupSetMemoryHardLimit(cgroup, hard_limit);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set memory hard limit for domain %s"),
                                 vm->def->name);
            goto cleanup;
        }
        if (vm->def->mem.soft_limit != 0) {
            rc = virCgroupSetMemorySoftLimit(cgroup, vm->def->mem.soft_limit);
            if (rc != 0) {
                virReportSystemError(-rc,
                                     _("Unable to set memory soft limit for domain %s"),
                                     vm->def->name);
                goto cleanup;
371
            }
372
        }
373

374 375 376 377 378 379 380
        if (vm->def->mem.swap_hard_limit != 0) {
            rc = virCgroupSetMemSwapHardLimit(cgroup, vm->def->mem.swap_hard_limit);
            if (rc != 0) {
                virReportSystemError(-rc,
                                     _("Unable to set swap hard limit for domain %s"),
                                     vm->def->name);
                goto cleanup;
381 382
            }
        }
383 384 385 386 387 388 389
    } else if (vm->def->mem.hard_limit != 0 ||
               vm->def->mem.soft_limit != 0 ||
               vm->def->mem.swap_hard_limit != 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Memory cgroup is not available on this host"));
    } else {
        VIR_WARN("Could not autoset a RSS limit for domain %s", vm->def->name);
390 391
    }

392 393
    if (vm->def->cputune.shares != 0) {
        if (qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
394 395 396 397 398 399 400
            rc = virCgroupSetCpuShares(cgroup, vm->def->cputune.shares);
            if(rc != 0) {
                virReportSystemError(-rc,
                                     _("Unable to set io cpu shares for domain %s"),
                                     vm->def->name);
                goto cleanup;
            }
401
        } else {
402 403
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("CPU tuning is not available on this host"));
404 405 406
        }
    }

407 408 409
    if ((vm->def->numatune.memory.nodemask ||
         (vm->def->numatune.memory.placement_mode ==
          VIR_DOMAIN_NUMATUNE_MEM_PLACEMENT_MODE_AUTO)) &&
H
Hu Tao 已提交
410 411
        vm->def->numatune.memory.mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT &&
        qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPUSET)) {
412 413 414 415 416 417 418
        char *mask = NULL;
        if (vm->def->numatune.memory.placement_mode ==
            VIR_DOMAIN_NUMATUNE_MEM_PLACEMENT_MODE_AUTO)
            mask = virDomainCpuSetFormat(nodemask, VIR_DOMAIN_CPUMASK_LEN);
        else
            mask = virDomainCpuSetFormat(vm->def->numatune.memory.nodemask,
                                         VIR_DOMAIN_CPUMASK_LEN);
H
Hu Tao 已提交
419
        if (!mask) {
420 421
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to convert memory nodemask"));
H
Hu Tao 已提交
422 423 424 425 426 427 428 429 430 431 432 433
            goto cleanup;
        }

        rc = virCgroupSetCpusetMems(cgroup, mask);
        VIR_FREE(mask);
        if (rc != 0) {
            virReportSystemError(-rc,
                                 _("Unable to set cpuset.mems for domain %s"),
                                 vm->def->name);
            goto cleanup;
        }
    }
434 435 436 437 438 439 440 441 442 443 444 445
done:
    virCgroupFree(&cgroup);
    return 0;

cleanup:
    if (cgroup) {
        virCgroupRemove(cgroup);
        virCgroupFree(&cgroup);
    }
    return -1;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459
int qemuSetupCgroupVcpuBW(virCgroupPtr cgroup, unsigned long long period,
                          long long quota)
{
    int rc;
    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 */
        rc = virCgroupGetCpuCfsPeriod(cgroup, &old_period);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
460
                                 "%s", _("Unable to get cpu bandwidth period"));
461 462 463 464 465 466
            return -1;
        }

        rc = virCgroupSetCpuCfsPeriod(cgroup, period);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
467
                                 "%s", _("Unable to set cpu bandwidth period"));
468 469 470 471 472 473 474 475
            return -1;
        }
    }

    if (quota) {
        rc = virCgroupSetCpuCfsQuota(cgroup, quota);
        if (rc < 0) {
            virReportSystemError(-rc,
W
Wen Congyang 已提交
476
                                 "%s", _("Unable to set cpu bandwidth quota"));
477 478 479 480 481 482 483 484 485 486
            goto cleanup;
        }
    }

    return 0;

cleanup:
    if (period) {
        rc = virCgroupSetCpuCfsPeriod(cgroup, old_period);
        if (rc < 0)
487
            virReportSystemError(-rc, "%s",
488
                                 _("Unable to rollback cpu bandwidth period"));
489 490 491 492 493
    }

    return -1;
}

494 495 496 497 498
int qemuSetupCgroupVcpuPin(virCgroupPtr cgroup,
                           virDomainVcpuPinDefPtr *vcpupin,
                           int nvcpupin,
                           int vcpuid)
{
499
    int i;
500 501 502

    for (i = 0; i < nvcpupin; i++) {
        if (vcpuid == vcpupin[i]->vcpuid) {
503
            return qemuSetupCgroupEmulatorPin(cgroup, vcpupin[i]);
504 505 506
        }
    }

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    return -1;
}

int qemuSetupCgroupEmulatorPin(virCgroupPtr cgroup,
                               virDomainVcpuPinDefPtr vcpupin)
{
    int rc = 0;
    char *new_cpus = NULL;

    new_cpus = virDomainCpuSetFormat(vcpupin->cpumask,
                                     VIR_DOMAIN_CPUMASK_LEN);
    if (!new_cpus) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to convert cpu mask"));
        rc = -1;
        goto cleanup;
    }

    rc = virCgroupSetCpusetCpus(cgroup, new_cpus);
    if (rc < 0) {
        virReportSystemError(-rc,
                             "%s",
                             _("Unable to set cpuset.cpus"));
        goto cleanup;
    }

533 534 535 536 537
cleanup:
    VIR_FREE(new_cpus);
    return rc;
}

538 539 540 541 542
int qemuSetupCgroupForVcpu(struct qemud_driver *driver, virDomainObjPtr vm)
{
    virCgroupPtr cgroup = NULL;
    virCgroupPtr cgroup_vcpu = NULL;
    qemuDomainObjPrivatePtr priv = vm->privateData;
543
    virDomainDefPtr def = vm->def;
544 545 546 547 548 549 550 551
    int rc;
    unsigned int i;
    unsigned long long period = vm->def->cputune.period;
    long long quota = vm->def->cputune.quota;

    if (driver->cgroup == NULL)
        return 0; /* Not supported, so claim success */

H
Hu Tao 已提交
552 553 554 555 556 557
    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPU)) {
        virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
                       _("cgroup cpu is not active"));
        return -1;
    }

558 559 560 561 562 563 564 565 566
    rc = virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0);
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("Unable to find cgroup for %s"),
                             vm->def->name);
        goto cleanup;
    }

    if (priv->nvcpupids == 0 || priv->vcpupids[0] == vm->pid) {
567
        /* If we does not know VCPU<->PID mapping or all vcpus run in the same
W
Wen Congyang 已提交
568
         * thread, we cannot control each vcpu.
569
         */
570 571 572
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to get vcpus' pids."));
        goto cleanup;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    }

    for (i = 0; i < priv->nvcpupids; i++) {
        rc = virCgroupForVcpu(cgroup, i, &cgroup_vcpu, 1);
        if (rc < 0) {
            virReportSystemError(-rc,
                                 _("Unable to create vcpu cgroup for %s(vcpu:"
                                   " %d)"),
                                 vm->def->name, i);
            goto cleanup;
        }

        /* move the thread for vcpu to sub dir */
        rc = virCgroupAddTask(cgroup_vcpu, priv->vcpupids[i]);
        if (rc < 0) {
            virReportSystemError(-rc,
                                 _("unable to add vcpu %d task %d to cgroup"),
                                 i, priv->vcpupids[i]);
            goto cleanup;
        }

        if (period || quota) {
H
Hu Tao 已提交
595 596
            if (qemuSetupCgroupVcpuBW(cgroup_vcpu, period, quota) < 0)
                goto cleanup;
597 598
        }

599 600 601 602 603 604 605 606 607
        /* Set vcpupin in cgroup if vcpupin xml is provided */
        if (def->cputune.nvcpupin &&
            qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPUSET) &&
            qemuSetupCgroupVcpuPin(cgroup_vcpu,
                                   def->cputune.vcpupin,
                                   def->cputune.nvcpupin,
                                   i) < 0)
            goto cleanup;

608 609 610 611 612 613 614
        virCgroupFree(&cgroup_vcpu);
    }

    virCgroupFree(&cgroup);
    return 0;

cleanup:
615 616 617 618 619
    if (cgroup_vcpu) {
        virCgroupRemove(cgroup_vcpu);
        virCgroupFree(&cgroup_vcpu);
    }

620 621 622 623 624 625 626 627
    if (cgroup) {
        virCgroupRemove(cgroup);
        virCgroupFree(&cgroup);
    }

    return -1;
}

628 629 630 631 632
int qemuSetupCgroupForEmulator(struct qemud_driver *driver,
                               virDomainObjPtr vm)
{
    virCgroupPtr cgroup = NULL;
    virCgroupPtr cgroup_emulator = NULL;
633
    virDomainDefPtr def = vm->def;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    int rc, i;

    if (driver->cgroup == NULL)
        return 0; /* Not supported, so claim success */

    rc = virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0);
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("Unable to find cgroup for %s"),
                             vm->def->name);
        goto cleanup;
    }

    rc = virCgroupForEmulator(cgroup, &cgroup_emulator, 1);
    if (rc < 0) {
        virReportSystemError(-rc,
                             _("Unable to create emulator cgroup for %s"),
                             vm->def->name);
        goto cleanup;
    }

    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
        if (!qemuCgroupControllerActive(driver, i)) {
            VIR_WARN("cgroup %d is not active", i);
            continue;
        }
        rc = virCgroupMoveTask(cgroup, cgroup_emulator, i);
        if (rc < 0) {
            virReportSystemError(-rc,
                                 _("Unable to move tasks from domain cgroup to "
                                   "emulator cgroup in controller %d for %s"),
                                 i, vm->def->name);
            goto cleanup;
        }
    }

670 671 672 673 674
    if (def->cputune.emulatorpin &&
        qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPUSET) &&
        qemuSetupCgroupEmulatorPin(cgroup_emulator, def->cputune.emulatorpin) < 0)
        goto cleanup;

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    virCgroupFree(&cgroup_emulator);
    virCgroupFree(&cgroup);
    return 0;

cleanup:
    if (cgroup_emulator) {
        virCgroupRemove(cgroup_emulator);
        virCgroupFree(&cgroup_emulator);
    }

    if (cgroup) {
        virCgroupRemove(cgroup);
        virCgroupFree(&cgroup);
    }

    return rc;
}
692 693 694 695 696 697 698 699 700 701 702 703 704 705

int qemuRemoveCgroup(struct qemud_driver *driver,
                     virDomainObjPtr vm,
                     int quiet)
{
    virCgroupPtr cgroup;
    int rc;

    if (driver->cgroup == NULL)
        return 0; /* Not supported, so claim success */

    rc = virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0);
    if (rc != 0) {
        if (!quiet)
706 707 708
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unable to find cgroup for %s"),
                           vm->def->name);
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
        return rc;
    }

    rc = virCgroupRemove(cgroup);
    virCgroupFree(&cgroup);
    return rc;
}

int qemuAddToCgroup(struct qemud_driver *driver,
                    virDomainDefPtr def)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rc;

    if (driver->cgroup == NULL)
        return 0; /* Not supported, so claim success */

    rc = virCgroupForDomain(driver->cgroup, def->name, &cgroup, 0);
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("unable to find cgroup for domain %s"),
                             def->name);
        goto cleanup;
    }

    rc = virCgroupAddTask(cgroup, getpid());
    if (rc != 0) {
        virReportSystemError(-rc,
                             _("unable to add domain %s task %d to cgroup"),
                             def->name, getpid());
        goto cleanup;
    }

    ret = 0;

cleanup:
    virCgroupFree(&cgroup);
    return ret;
}