qemu_cgroup.c 36.3 KB
Newer Older
1 2 3
/*
 * qemu_cgroup.c: QEMU cgroup management
 *
4
 * Copyright (C) 2006-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "qemu_cgroup.h"
27
#include "qemu_domain.h"
28
#include "qemu_process.h"
29
#include "vircgroup.h"
30
#include "virlog.h"
31
#include "viralloc.h"
32
#include "virerror.h"
33
#include "domain_audit.h"
34
#include "virscsi.h"
35
#include "virstring.h"
36
#include "virfile.h"
37
#include "virtypedparam.h"
38
#include "virnuma.h"
39
#include "virsystemd.h"
40
#include "virdevmapper.h"
41 42 43

#define VIR_FROM_THIS VIR_FROM_QEMU

44 45
VIR_LOG_INIT("qemu.qemu_cgroup");

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

56

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

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

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

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

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

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

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


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

    return qemuSetupImagePathCgroup(vm, src->path, src->readonly || forceReadonly);
}


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


int
qemuTeardownImageCgroup(virDomainObjPtr vm,
                        virStorageSourcePtr src)
{
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int perms = VIR_CGROUP_DEVICE_READ |
                VIR_CGROUP_DEVICE_WRITE |
                VIR_CGROUP_DEVICE_MKNOD;
    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;
    }

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

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

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

167 168 169 170 171 172 173
    /* 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. */

174
    return ret;
175 176 177
}


178 179 180
int
qemuSetupDiskCgroup(virDomainObjPtr vm,
                    virDomainDiskDefPtr disk)
181
{
182
    virStorageSourcePtr next;
183
    bool forceReadonly = false;
184

185
    for (next = disk->src; virStorageSourceIsBacking(next); next = next->backingStore) {
186
        if (qemuSetupImageCgroupInternal(vm, next, forceReadonly) < 0)
187
            return -1;
188 189 190

        /* setup only the top level image for read-write */
        forceReadonly = true;
191
    }
192 193

    return 0;
194 195 196
}


197 198 199
int
qemuTeardownDiskCgroup(virDomainObjPtr vm,
                       virDomainDiskDefPtr disk)
200
{
201
    virStorageSourcePtr next;
202

203
    for (next = disk->src; virStorageSourceIsBacking(next); next = next->backingStore) {
204
        if (qemuTeardownImageCgroup(vm, next) < 0)
205 206
            return -1;
    }
207

208
    return 0;
209 210
}

211

212
static int
213
qemuSetupChrSourceCgroup(virDomainObjPtr vm,
214
                         virDomainChrSourceDefPtr source)
215
{
216
    qemuDomainObjPrivatePtr priv = vm->privateData;
217
    int ret;
218

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

222
    if (source->type != VIR_DOMAIN_CHR_TYPE_DEV)
223 224
        return 0;

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

227
    ret = virCgroupAllowDevicePath(priv->cgroup, source->data.file.path,
228
                                   VIR_CGROUP_DEVICE_RW, false);
229
    virDomainAuditCgroupPath(vm, priv->cgroup, "allow",
230
                             source->data.file.path, "rw", ret);
231

232
    return ret;
233 234
}

235 236 237 238 239 240 241 242

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

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

246 247 248 249 250 251 252 253
    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",
254
                             source->data.file.path, "rw", ret);
255 256 257 258 259

    return ret;
}


260
static int
261 262 263
qemuSetupChardevCgroupCB(virDomainDefPtr def ATTRIBUTE_UNUSED,
                         virDomainChrDefPtr dev,
                         void *opaque)
264
{
265 266
    virDomainObjPtr vm = opaque;

267
    return qemuSetupChrSourceCgroup(vm, dev->source);
268 269 270 271
}


static int
272
qemuSetupTPMCgroup(virDomainObjPtr vm)
273
{
274
    int ret = 0;
275
    virDomainTPMDefPtr dev = vm->def->tpm;
276 277 278

    switch (dev->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
279
        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
280 281 282 283 284
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

285
    return ret;
286 287
}

288

289
int
290 291 292 293 294 295
qemuSetupInputCgroup(virDomainObjPtr vm,
                     virDomainInputDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int ret = 0;

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

299 300 301 302
    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,
303
                                       VIR_CGROUP_DEVICE_RW, false);
304
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", dev->source.evdev, "rw", ret);
305 306 307 308 309 310 311
        break;
    }

    return ret;
}


312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
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);
327
        virDomainAuditCgroupPath(vm, priv->cgroup, "deny", dev->source.evdev, "rwm", ret);
328 329 330 331 332 333 334
        break;
    }

    return ret;
}


335
int
336
qemuSetupHostdevCgroup(virDomainObjPtr vm,
337 338 339
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
340 341 342 343
    char **path = NULL;
    int *perms = NULL;
    size_t i, npaths = 0;
    int rv, ret = -1;
344

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

348
    if (qemuDomainGetHostdevPath(NULL, dev, false, &npaths, &path, &perms) < 0)
349
        goto cleanup;
350

351 352 353 354 355
    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]),
356
                                 rv);
357 358
        if (rv < 0)
            goto cleanup;
359 360
    }

361
    ret = 0;
362

363
 cleanup:
364 365
    for (i = 0; i < npaths; i++)
        VIR_FREE(path[i]);
366
    VIR_FREE(path);
367
    VIR_FREE(perms);
368 369 370 371 372 373 374 375
    return ret;
}

int
qemuTeardownHostdevCgroup(virDomainObjPtr vm,
                       virDomainHostdevDefPtr dev)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
376 377 378
    char **path = NULL;
    size_t i, npaths = 0;
    int rv, ret = -1;
379 380 381 382 383 384 385 386 387

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

388 389 390 391 392 393 394 395 396 397 398 399
    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,
400
                                 "deny", path[i], "rwm", rv);
401 402
        if (rv < 0)
            goto cleanup;
403 404 405
    }

    ret = 0;
406
 cleanup:
407 408
    for (i = 0; i < npaths; i++)
        VIR_FREE(path[i]);
409 410 411 412
    VIR_FREE(path);
    return ret;
}

413

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
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",
431
                             mem->nvdimmPath, "rw", rv);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

    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,
453
                             "deny", mem->nvdimmPath, "rwm", rv);
454 455 456 457
    return rv;
}


458 459 460 461 462 463 464 465
static int
qemuSetupGraphicsCgroup(virDomainObjPtr vm,
                        virDomainGraphicsDefPtr gfx)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    const char *rendernode = gfx->data.spice.rendernode;
    int ret;

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

469 470 471 472 473 474 475 476
    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,
477
                             "rw", ret);
478 479 480 481
    return ret;
}


482 483 484 485
static int
qemuSetupBlkioCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
486
    size_t i;
487 488 489 490 491 492 493 494 495 496 497 498

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

499 500 501
    if (vm->def->blkio.weight != 0 &&
        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
        return -1;
502 503 504

    if (vm->def->blkio.ndevices) {
        for (i = 0; i < vm->def->blkio.ndevices; i++) {
505
            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
506 507
            if (dev->weight &&
                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
508 509 510
                                               dev->weight) < 0 ||
                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
                                               &dev->weight) < 0))
511 512 513 514
                return -1;

            if (dev->riops &&
                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
515 516 517
                                                 dev->riops) < 0 ||
                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
                                                 &dev->riops) < 0))
518 519 520 521
                return -1;

            if (dev->wiops &&
                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
522 523 524
                                                  dev->wiops) < 0 ||
                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
                                                  &dev->wiops) < 0))
525 526 527 528
                return -1;

            if (dev->rbps &&
                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
529 530 531
                                                dev->rbps) < 0 ||
                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
                                                &dev->rbps) < 0))
532 533 534 535
                return -1;

            if (dev->wbps &&
                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
536 537 538
                                                 dev->wbps) < 0 ||
                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
                                                 &dev->wbps) < 0))
539 540 541 542 543 544 545
                return -1;
        }
    }

    return 0;
}

546

547 548 549 550 551
static int
qemuSetupMemoryCgroup(virDomainObjPtr vm)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

E
Eric Blake 已提交
552
    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_MEMORY)) {
553 554 555
        if (virMemoryLimitIsSet(vm->def->mem.hard_limit) ||
            virMemoryLimitIsSet(vm->def->mem.soft_limit) ||
            virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) {
556 557 558
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Memory cgroup is not available on this host"));
            return -1;
O
Osier Yang 已提交
559 560
        } else {
            return 0;
561 562 563
        }
    }

564 565 566
    if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
        if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0)
            return -1;
567

568 569 570
    if (virMemoryLimitIsSet(vm->def->mem.soft_limit))
        if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0)
            return -1;
571

572 573 574
    if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit))
        if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0)
            return -1;
575 576 577 578 579

    return 0;
}


580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
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;
}


599 600 601 602 603 604 605
int
qemuSetupRNGCgroup(virDomainObjPtr vm,
                   virDomainRNGDefPtr rng)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    int rv;

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

609 610 611 612 613 614 615
    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,
616
                                 "rw", rv);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


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

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

636 637 638 639 640 641 642
    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,
643
                                 "rw", rv);
644 645 646 647 648 649 650 651 652
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
            return -1;
    }

    return 0;
}


653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
int
qemuSetupChardevCgroup(virDomainObjPtr vm,
                       virDomainChrDefPtr dev)
{
    return qemuSetupChrSourceCgroup(vm, dev->source);
}


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


669
static int
670
qemuSetupDevicesCgroup(virDomainObjPtr vm)
671 672 673 674
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    virQEMUDriverConfigPtr cfg = NULL;
    const char *const *deviceACL = NULL;
675
    int rv = -1;
676
    int ret = -1;
677
    size_t i;
678 679 680 681

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

682 683 684 685 686
    rv = virCgroupDenyAllDevices(priv->cgroup);
    virDomainAuditCgroup(vm, priv->cgroup, "deny", "all", rv == 0);
    if (rv < 0) {
        if (virLastErrorIsSystemErrno(EPERM)) {
            virResetLastError();
687 688 689 690 691 692 693
            VIR_WARN("Group devices ACL is not accessible, disabling whitelisting");
            return 0;
        }

        goto cleanup;
    }

694 695 696
    if (qemuSetupFirmwareCgroup(vm) < 0)
        goto cleanup;

697
    for (i = 0; i < vm->def->ndisks; i++) {
698 699 700 701
        if (qemuSetupDiskCgroup(vm, vm->def->disks[i]) < 0)
            goto cleanup;
    }

702 703
    rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_PTY_MAJOR, -1,
                              VIR_CGROUP_DEVICE_RW);
704
    virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_PTY_MAJOR,
705 706
                              "pty", "rw", rv == 0);
    if (rv < 0)
707 708
        goto cleanup;

709
    cfg = virQEMUDriverGetConfig(priv->driver);
710 711 712 713 714
    deviceACL = cfg->cgroupDeviceACL ?
                (const char *const *)cfg->cgroupDeviceACL :
                defaultDeviceACL;

    if (vm->def->nsounds &&
715
        ((!vm->def->ngraphics && cfg->nogfxAllowHostAudio) ||
716 717
         (vm->def->graphics &&
          ((vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
718
           cfg->vncAllowHostAudio) ||
719
           (vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL))))) {
720 721
        rv = virCgroupAllowDevice(priv->cgroup, 'c', DEVICE_SND_MAJOR, -1,
                                  VIR_CGROUP_DEVICE_RW);
722
        virDomainAuditCgroupMajor(vm, priv->cgroup, "allow", DEVICE_SND_MAJOR,
723 724
                                  "sound", "rw", rv == 0);
        if (rv < 0)
725 726 727
            goto cleanup;
    }

728
    for (i = 0; deviceACL[i] != NULL; i++) {
729
        if (!virFileExists(deviceACL[i])) {
N
Nehal J Wani 已提交
730
            VIR_DEBUG("Ignoring non-existent device %s", deviceACL[i]);
731 732 733
            continue;
        }

734
        rv = virCgroupAllowDevicePath(priv->cgroup, deviceACL[i],
735
                                      VIR_CGROUP_DEVICE_RW, false);
736
        virDomainAuditCgroupPath(vm, priv->cgroup, "allow", deviceACL[i], "rw", rv);
737 738
        if (rv < 0 &&
            !virLastErrorIsSystemErrno(ENOENT))
739 740 741 742 743
            goto cleanup;
    }

    if (virDomainChrDefForeach(vm->def,
                               true,
744
                               qemuSetupChardevCgroupCB,
745 746 747
                               vm) < 0)
        goto cleanup;

748
    if (vm->def->tpm && qemuSetupTPMCgroup(vm) < 0)
749 750 751
        goto cleanup;

    for (i = 0; i < vm->def->nhostdevs; i++) {
752
        if (qemuSetupHostdevCgroup(vm, vm->def->hostdevs[i]) < 0)
753 754 755
            goto cleanup;
    }

756 757 758 759 760
    for (i = 0; i < vm->def->nmems; i++) {
        if (qemuSetupMemoryDevicesCgroup(vm, vm->def->mems[i]) < 0)
            goto cleanup;
    }

761 762 763 764 765
    for (i = 0; i < vm->def->ngraphics; i++) {
        if (qemuSetupGraphicsCgroup(vm, vm->def->graphics[i]) < 0)
            goto cleanup;
    }

766 767 768 769 770
    for (i = 0; i < vm->def->ninputs; i++) {
        if (qemuSetupInputCgroup(vm, vm->def->inputs[i]) < 0)
            goto cleanup;
    }

771
    for (i = 0; i < vm->def->nrngs; i++) {
772 773
        if (qemuSetupRNGCgroup(vm, vm->def->rngs[i]) < 0)
            goto cleanup;
774 775
    }

776
    ret = 0;
777
 cleanup:
778 779 780 781 782
    virObjectUnref(cfg);
    return ret;
}


783
int
784
qemuSetupCpusetMems(virDomainObjPtr vm)
785
{
786
    virCgroupPtr cgroup_temp = NULL;
787
    qemuDomainObjPrivatePtr priv = vm->privateData;
788
    virDomainNumatuneMemMode mode;
789
    char *mem_mask = NULL;
790 791 792 793 794
    int ret = -1;

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

795 796
    if (virDomainNumatuneGetMode(vm->def->numa, -1, &mode) < 0 ||
        mode != VIR_DOMAIN_NUMATUNE_MEM_STRICT)
797 798
        return 0;

799
    if (virDomainNumatuneMaybeFormatNodeset(vm->def->numa,
800
                                            priv->autoNodeset,
801
                                            &mem_mask, -1) < 0)
802
        goto cleanup;
803

804
    if (mem_mask)
J
John Ferlan 已提交
805 806
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                               false, &cgroup_temp) < 0 ||
807
            virCgroupSetCpusetMems(cgroup_temp, mem_mask) < 0)
808
            goto cleanup;
809

810 811 812
    ret = 0;
 cleanup:
    VIR_FREE(mem_mask);
813
    virCgroupFree(&cgroup_temp);
814 815 816 817 818
    return ret;
}


static int
819
qemuSetupCpusetCgroup(virDomainObjPtr vm)
820 821 822 823 824 825
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

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

826 827 828
    if (virCgroupSetCpusetMemoryMigrate(priv->cgroup, true) < 0)
        return -1;

829
    return 0;
830 831 832
}


833
static int
834
qemuSetupCpuCgroup(virDomainObjPtr vm)
835 836
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
837 838 839 840
    virObjectEventPtr event = NULL;
    virTypedParameterPtr eventParams = NULL;
    int eventNparams = 0;
    int eventMaxparams = 0;
841 842

    if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU)) {
843
       if (vm->def->cputune.sharesSpecified) {
844 845 846 847 848 849 850 851
           virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                          _("CPU tuning is not available on this host"));
           return -1;
       } else {
           return 0;
       }
    }

852 853 854 855 856 857 858
    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;
859 860 861 862
        if (vm->def->cputune.shares != val) {
            vm->def->cputune.shares = val;
            if (virTypedParamsAddULLong(&eventParams, &eventNparams,
                                        &eventMaxparams,
863
                                        VIR_DOMAIN_TUNABLE_CPU_CPU_SHARES,
864 865 866 867 868 869
                                        val) < 0)
                return -1;

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

870
        qemuDomainEventQueue(priv->driver, event);
871
    }
872 873 874 875 876

    return 0;
}


877
static int
878
qemuInitCgroup(virDomainObjPtr vm,
879 880
               size_t nnicindexes,
               int *nicindexes)
881
{
882
    int ret = -1;
883
    qemuDomainObjPrivatePtr priv = vm->privateData;
884
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
885

886
    if (!virQEMUDriverIsPrivileged(priv->driver))
887 888
        goto done;

889 890 891
    if (!virCgroupAvailable())
        goto done;

892 893
    virCgroupFree(&priv->cgroup);

894
    if (!vm->def->resource) {
895 896
        virDomainResourceDefPtr res;

897
        if (VIR_ALLOC(res) < 0)
898
            goto cleanup;
899

900
        if (VIR_STRDUP(res->partition, "/machine") < 0) {
901 902 903 904 905
            VIR_FREE(res);
            goto cleanup;
        }

        vm->def->resource = res;
906 907
    }

908 909 910 911 912 913
    if (vm->def->resource->partition[0] != '/') {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Resource partition '%s' must start with '/'"),
                       vm->def->resource->partition);
        goto cleanup;
    }
914

915
    if (virCgroupNewMachine(priv->machineName,
916 917 918 919 920
                            "qemu",
                            vm->def->uuid,
                            NULL,
                            vm->pid,
                            false,
921
                            nnicindexes, nicindexes,
922 923 924
                            vm->def->resource->partition,
                            cfg->cgroupControllers,
                            &priv->cgroup) < 0) {
925 926
        if (virCgroupNewIgnoreError())
            goto done;
927

928 929
        goto cleanup;
    }
930

931
 done:
932
    ret = 0;
933
 cleanup:
934 935 936
    virObjectUnref(cfg);
    return ret;
}
937

938 939 940
static void
qemuRestoreCgroupState(virDomainObjPtr vm)
{
941
    char *mem_mask = NULL;
942
    char *nodeset = NULL;
943 944
    int empty = -1;
    qemuDomainObjPrivatePtr priv = vm->privateData;
945
    size_t i = 0;
946
    virBitmapPtr all_nodes;
947
    virCgroupPtr cgroup_temp = NULL;
948

949 950 951 952
    if (!virNumaIsAvailable() ||
        !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET))
        return;

953
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
954 955 956 957 958 959
        goto error;

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

    if ((empty = virCgroupHasEmptyTasks(priv->cgroup,
960
                                        VIR_CGROUP_CONTROLLER_CPUSET)) <= 0)
961 962 963 964 965
        goto error;

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

966
    for (i = 0; i < virDomainDefGetVcpusMax(vm->def); i++) {
967
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(vm->def, i);
968 969 970 971

        if (!vcpu->online)
            continue;

J
John Ferlan 已提交
972 973
        if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_VCPU, i,
                               false, &cgroup_temp) < 0 ||
974 975 976 977 978
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

979
        VIR_FREE(nodeset);
980 981 982
        virCgroupFree(&cgroup_temp);
    }

983 984 985
    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 已提交
986
                               false, &cgroup_temp) < 0 ||
987 988 989 990 991
            virCgroupSetCpusetMemoryMigrate(cgroup_temp, true) < 0 ||
            virCgroupGetCpusetMems(cgroup_temp, &nodeset) < 0 ||
            virCgroupSetCpusetMems(cgroup_temp, nodeset) < 0)
            goto cleanup;

992
        VIR_FREE(nodeset);
993 994 995
        virCgroupFree(&cgroup_temp);
    }

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

1003 1004
 cleanup:
    VIR_FREE(mem_mask);
1005
    VIR_FREE(nodeset);
1006
    virBitmapFree(all_nodes);
1007
    virCgroupFree(&cgroup_temp);
1008 1009 1010 1011 1012 1013 1014
    return;

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

int
1017
qemuConnectCgroup(virDomainObjPtr vm)
1018 1019
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1020
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(priv->driver);
1021 1022
    int ret = -1;

1023
    if (!virQEMUDriverIsPrivileged(priv->driver))
1024 1025 1026 1027 1028 1029 1030
        goto done;

    if (!virCgroupAvailable())
        goto done;

    virCgroupFree(&priv->cgroup);

1031 1032 1033
    if (virCgroupNewDetectMachine(vm->def->name,
                                  "qemu",
                                  vm->pid,
1034
                                  cfg->cgroupControllers,
1035
                                  priv->machineName,
1036
                                  &priv->cgroup) < 0)
1037
        goto cleanup;
1038

1039 1040
    qemuRestoreCgroupState(vm);

1041
 done:
1042
    ret = 0;
1043
 cleanup:
1044
    virObjectUnref(cfg);
1045
    return ret;
1046 1047
}

1048
int
1049
qemuSetupCgroup(virDomainObjPtr vm,
1050 1051
                size_t nnicindexes,
                int *nicindexes)
1052
{
1053
    qemuDomainObjPrivatePtr priv = vm->privateData;
1054
    int ret = -1;
1055

1056 1057 1058 1059 1060 1061
    if (!vm->pid) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot setup cgroups until process is started"));
        return -1;
    }

1062
    if (qemuInitCgroup(vm, nnicindexes, nicindexes) < 0)
1063
        return -1;
1064

1065
    if (!priv->cgroup)
1066
        return 0;
1067

1068
    if (qemuSetupDevicesCgroup(vm) < 0)
1069
        goto cleanup;
1070

1071 1072
    if (qemuSetupBlkioCgroup(vm) < 0)
        goto cleanup;
1073

1074 1075
    if (qemuSetupMemoryCgroup(vm) < 0)
        goto cleanup;
1076

1077
    if (qemuSetupCpuCgroup(vm) < 0)
1078
        goto cleanup;
1079

1080
    if (qemuSetupCpusetCgroup(vm) < 0)
1081
        goto cleanup;
1082

1083
    ret = 0;
1084
 cleanup:
1085
    return ret;
1086 1087
}

1088 1089 1090 1091
int
qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                      unsigned long long period,
                      long long quota)
1092 1093 1094 1095 1096 1097 1098 1099
{
    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 */
1100
        if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
1101 1102
            return -1;

1103
        if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
1104 1105 1106
            return -1;
    }

1107 1108 1109
    if (quota &&
        virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
        goto error;
1110 1111 1112

    return 0;

1113
 error:
1114
    if (period) {
1115 1116 1117 1118 1119 1120
        virErrorPtr saved = virSaveLastError();
        ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
        if (saved) {
            virSetError(saved);
            virFreeError(saved);
        }
1121 1122 1123 1124 1125
    }

    return -1;
}

1126

1127
int
1128 1129
qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup,
                          virBitmapPtr cpumask)
1130
{
1131
    int ret = -1;
1132 1133
    char *new_cpus = NULL;

1134
    if (!(new_cpus = virBitmapFormat(cpumask)))
1135 1136
        goto cleanup;

1137
    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
1138 1139
        goto cleanup;

1140
    ret = 0;
1141
 cleanup:
1142
    VIR_FREE(new_cpus);
1143
    return ret;
1144 1145
}

1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 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
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;
}


1196
int
1197
qemuRemoveCgroup(virDomainObjPtr vm)
1198
{
1199
    qemuDomainObjPrivatePtr priv = vm->privateData;
1200

1201
    if (priv->cgroup == NULL)
1202 1203
        return 0; /* Not supported, so claim success */

1204
    if (virCgroupTerminateMachine(priv->machineName) < 0) {
1205 1206 1207 1208
        if (!virCgroupNewIgnoreError())
            VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
    }

1209
    return virCgroupRemove(priv->cgroup);
1210
}
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232


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
1233
 * qemuCgroupEmulatorAllNodesRestore.
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
 *
 * 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;

1250
    if (!(all_nodes = virNumaGetHostMemoryNodeset()))
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        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;
}


/**
1280
 * qemuCgroupEmulatorAllNodesRestore:
1281 1282 1283 1284 1285 1286
 * @data: data structure created by qemuCgroupEmulatorAllNodesAllow
 *
 * Rolls back the setting done by qemuCgroupEmulatorAllNodesAllow and frees the
 * associated data.
 */
void
1287
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
{
    virErrorPtr err;

    if (!data)
        return;

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

    qemuCgroupEmulatorAllNodesDataFree(data);
}