From nobody Sun Feb 8 09:41:06 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1537285609412849.837326990881; Tue, 18 Sep 2018 08:46:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2276A750D5; Tue, 18 Sep 2018 15:46:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 626942CFBC; Tue, 18 Sep 2018 15:46:46 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CB1BC1800533; Tue, 18 Sep 2018 15:46:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8IFkQMJ012150 for ; Tue, 18 Sep 2018 11:46:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7B10E82216; Tue, 18 Sep 2018 15:46:26 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id CC1FB5D9C7 for ; Tue, 18 Sep 2018 15:46:25 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Tue, 18 Sep 2018 17:45:36 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 15/47] vircgroup: extract virCgroupV1MakeGroup X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 18 Sep 2018 15:46:47 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Fabiano Fid=C3=AAncio Reviewed-by: J=EF=BF=BDn Tomko --- src/util/vircgroup.c | 141 ++---------------------------------- src/util/vircgroupbackend.h | 15 ++++ src/util/vircgrouppriv.h | 20 +++++ src/util/vircgroupv1.c | 132 +++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 134 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 55122a5ab9..8083e4596d 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -80,14 +80,6 @@ VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER= _LAST, "freezer", "blkio", "net_cls", "perf_event", "name=3Dsystemd"); =20 -typedef enum { - VIR_CGROUP_NONE =3D 0, /* create subdir under each cgroup if possible.= */ - VIR_CGROUP_MEM_HIERACHY =3D 1 << 0, /* call virCgroupSetMemoryUseHiera= rchy - * before creating subcgroups and - * attaching tasks - */ -} virCgroupFlags; - =20 /** * virCgroupGetDevicePermsString: @@ -448,7 +440,7 @@ virCgroupGetBlockDevString(const char *path) } =20 =20 -static int +int virCgroupSetValueStr(virCgroupPtr group, int controller, const char *key, @@ -478,7 +470,7 @@ virCgroupSetValueStr(virCgroupPtr group, } =20 =20 -static int +int virCgroupGetValueStr(virCgroupPtr group, int controller, const char *key, @@ -539,7 +531,7 @@ virCgroupGetValueForBlkDev(virCgroupPtr group, } =20 =20 -static int +int virCgroupSetValueU64(virCgroupPtr group, int controller, const char *key, @@ -591,7 +583,7 @@ virCgroupGetValueI64(virCgroupPtr group, } =20 =20 -static int +int virCgroupGetValueU64(virCgroupPtr group, int controller, const char *key, @@ -613,137 +605,18 @@ virCgroupGetValueU64(virCgroupPtr group, } =20 =20 -static int -virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group) -{ - size_t i; - const char *inherit_values[] =3D { - "cpuset.cpus", - "cpuset.mems", - "cpuset.memory_migrate", - }; - - VIR_DEBUG("Setting up inheritance %s -> %s", parent->path, group->path= ); - for (i =3D 0; i < ARRAY_CARDINALITY(inherit_values); i++) { - VIR_AUTOFREE(char *) value =3D NULL; - - if (virCgroupGetValueStr(parent, - VIR_CGROUP_CONTROLLER_CPUSET, - inherit_values[i], - &value) < 0) - return -1; - - VIR_DEBUG("Inherit %s =3D %s", inherit_values[i], value); - - if (virCgroupSetValueStr(group, - VIR_CGROUP_CONTROLLER_CPUSET, - inherit_values[i], - value) < 0) - return -1; - } - - return 0; -} - - -static int -virCgroupSetMemoryUseHierarchy(virCgroupPtr group) -{ - unsigned long long value; - const char *filename =3D "memory.use_hierarchy"; - - if (virCgroupGetValueU64(group, - VIR_CGROUP_CONTROLLER_MEMORY, - filename, &value) < 0) - return -1; - - /* Setting twice causes error, so if already enabled, skip setting */ - if (value =3D=3D 1) - return 0; - - VIR_DEBUG("Setting up %s/%s", group->path, filename); - if (virCgroupSetValueU64(group, - VIR_CGROUP_CONTROLLER_MEMORY, - filename, 1) < 0) - return -1; - - return 0; -} - - static int virCgroupMakeGroup(virCgroupPtr parent, virCgroupPtr group, bool create, unsigned int flags) { - size_t i; - - VIR_DEBUG("Make group %s", group->path); - for (i =3D 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - VIR_AUTOFREE(char *) path =3D NULL; - - /* We must never mkdir() in systemd's hierarchy */ - if (i =3D=3D VIR_CGROUP_CONTROLLER_SYSTEMD) { - VIR_DEBUG("Not creating systemd controller group"); - continue; - } - - /* Skip over controllers that aren't mounted */ - if (!group->controllers[i].mountPoint) { - VIR_DEBUG("Skipping unmounted controller %s", - virCgroupControllerTypeToString(i)); - continue; - } - - if (virCgroupPathOfController(group, i, "", &path) < 0) - goto error; - - VIR_DEBUG("Make controller %s", path); - if (!virFileExists(path)) { - if (!create || - mkdir(path, 0755) < 0) { - if (errno =3D=3D EEXIST) - continue; - /* With a kernel that doesn't support multi-level directory - * for blkio controller, libvirt will fail and disable all - * other controllers even though they are available. So - * treat blkio as unmounted if mkdir fails. */ - if (i =3D=3D VIR_CGROUP_CONTROLLER_BLKIO) { - VIR_DEBUG("Ignoring mkdir failure with blkio controlle= r. Kernel probably too old"); - VIR_FREE(group->controllers[i].mountPoint); - continue; - } else { - virReportSystemError(errno, - _("Failed to create controller %s= for group"), - virCgroupControllerTypeToString(i= )); - goto error; - } - } - if (i =3D=3D VIR_CGROUP_CONTROLLER_CPUSET && - group->controllers[i].mountPoint !=3D NULL && - virCgroupCpuSetInherit(parent, group) < 0) { - goto error; - } - /* - * Note that virCgroupSetMemoryUseHierarchy should always be - * called prior to creating subcgroups and attaching tasks. - */ - if ((flags & VIR_CGROUP_MEM_HIERACHY) && - i =3D=3D VIR_CGROUP_CONTROLLER_MEMORY && - group->controllers[i].mountPoint !=3D NULL && - virCgroupSetMemoryUseHierarchy(group) < 0) { - goto error; - } - } + if (group->backend->makeGroup(parent, group, create, flags) < 0) { + virCgroupRemove(group); + return -1; } =20 - VIR_DEBUG("Done making controllers for group"); return 0; - - error: - virCgroupRemove(group); - return -1; } =20 =20 diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index cf000529c4..214c9f0726 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -27,6 +27,14 @@ =20 # define CGROUP_MAX_VAL 512 =20 +typedef enum { + VIR_CGROUP_NONE =3D 0, /* create subdir under each cgroup if possible.= */ + VIR_CGROUP_MEM_HIERACHY =3D 1 << 0, /* call virCgroupSetMemoryUseHiera= rchy + * before creating subcgroups and + * attaching tasks + */ +} virCgroupBackendFlags; + typedef enum { VIR_CGROUP_BACKEND_TYPE_V1 =3D 0, VIR_CGROUP_BACKEND_TYPE_LAST, @@ -86,6 +94,12 @@ typedef int const char *key, char **path); =20 +typedef int +(*virCgroupMakeGroupCB)(virCgroupPtr parent, + virCgroupPtr group, + bool create, + unsigned int flags); + struct _virCgroupBackend { virCgroupBackendType type; =20 @@ -102,6 +116,7 @@ struct _virCgroupBackend { virCgroupHasControllerCB hasController; virCgroupGetAnyControllerCB getAnyController; virCgroupPathOfControllerCB pathOfController; + virCgroupMakeGroupCB makeGroup; }; typedef struct _virCgroupBackend virCgroupBackend; typedef virCgroupBackend *virCgroupBackendPtr; diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h index e7f4a1f0fc..2e731458d5 100644 --- a/src/util/vircgrouppriv.h +++ b/src/util/vircgrouppriv.h @@ -53,6 +53,26 @@ struct _virCgroup { virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST]; }; =20 +int virCgroupSetValueStr(virCgroupPtr group, + int controller, + const char *key, + const char *value); + +int virCgroupGetValueStr(virCgroupPtr group, + int controller, + const char *key, + char **value); + +int virCgroupSetValueU64(virCgroupPtr group, + int controller, + const char *key, + unsigned long long int value); + +int virCgroupGetValueU64(virCgroupPtr group, + int controller, + const char *key, + unsigned long long int *value); + int virCgroupPartitionEscape(char **path); =20 int virCgroupNewPartition(const char *path, diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index 7d92150dc3..cf484216cc 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -536,6 +536,137 @@ virCgroupV1PathOfController(virCgroupPtr group, } =20 =20 +static int +virCgroupV1CpuSetInherit(virCgroupPtr parent, + virCgroupPtr group) +{ + size_t i; + const char *inherit_values[] =3D { + "cpuset.cpus", + "cpuset.mems", + "cpuset.memory_migrate", + }; + + VIR_DEBUG("Setting up inheritance %s -> %s", parent->path, group->path= ); + for (i =3D 0; i < ARRAY_CARDINALITY(inherit_values); i++) { + VIR_AUTOFREE(char *) value =3D NULL; + + if (virCgroupGetValueStr(parent, + VIR_CGROUP_CONTROLLER_CPUSET, + inherit_values[i], + &value) < 0) + return -1; + + VIR_DEBUG("Inherit %s =3D %s", inherit_values[i], value); + + if (virCgroupSetValueStr(group, + VIR_CGROUP_CONTROLLER_CPUSET, + inherit_values[i], + value) < 0) + return -1; + } + + return 0; +} + + +static int +virCgroupV1SetMemoryUseHierarchy(virCgroupPtr group) +{ + unsigned long long value; + const char *filename =3D "memory.use_hierarchy"; + + if (virCgroupGetValueU64(group, + VIR_CGROUP_CONTROLLER_MEMORY, + filename, &value) < 0) + return -1; + + /* Setting twice causes error, so if already enabled, skip setting */ + if (value =3D=3D 1) + return 0; + + VIR_DEBUG("Setting up %s/%s", group->path, filename); + if (virCgroupSetValueU64(group, + VIR_CGROUP_CONTROLLER_MEMORY, + filename, 1) < 0) + return -1; + + return 0; +} + + +static int +virCgroupV1MakeGroup(virCgroupPtr parent, + virCgroupPtr group, + bool create, + unsigned int flags) +{ + size_t i; + + VIR_DEBUG("Make group %s", group->path); + for (i =3D 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + VIR_AUTOFREE(char *) path =3D NULL; + + /* We must never mkdir() in systemd's hierarchy */ + if (i =3D=3D VIR_CGROUP_CONTROLLER_SYSTEMD) { + VIR_DEBUG("Not creating systemd controller group"); + continue; + } + + /* Skip over controllers that aren't mounted */ + if (!group->controllers[i].mountPoint) { + VIR_DEBUG("Skipping unmounted controller %s", + virCgroupV1ControllerTypeToString(i)); + continue; + } + + if (virCgroupV1PathOfController(group, i, "", &path) < 0) + return -1; + + VIR_DEBUG("Make controller %s", path); + if (!virFileExists(path)) { + if (!create || + mkdir(path, 0755) < 0) { + if (errno =3D=3D EEXIST) + continue; + /* With a kernel that doesn't support multi-level directory + * for blkio controller, libvirt will fail and disable all + * other controllers even though they are available. So + * treat blkio as unmounted if mkdir fails. */ + if (i =3D=3D VIR_CGROUP_CONTROLLER_BLKIO) { + VIR_DEBUG("Ignoring mkdir failure with blkio controlle= r. Kernel probably too old"); + VIR_FREE(group->controllers[i].mountPoint); + continue; + } else { + virReportSystemError(errno, + _("Failed to create v1 controller= %s for group"), + virCgroupV1ControllerTypeToString= (i)); + return -1; + } + } + if (i =3D=3D VIR_CGROUP_CONTROLLER_CPUSET && + group->controllers[i].mountPoint !=3D NULL && + virCgroupV1CpuSetInherit(parent, group) < 0) { + return -1; + } + /* + * Note that virCgroupV1SetMemoryUseHierarchy should always be + * called prior to creating subcgroups and attaching tasks. + */ + if ((flags & VIR_CGROUP_MEM_HIERACHY) && + i =3D=3D VIR_CGROUP_CONTROLLER_MEMORY && + group->controllers[i].mountPoint !=3D NULL && + virCgroupV1SetMemoryUseHierarchy(group) < 0) { + return -1; + } + } + } + + VIR_DEBUG("Done making controllers for group"); + return 0; +} + + virCgroupBackend virCgroupV1Backend =3D { .type =3D VIR_CGROUP_BACKEND_TYPE_V1, =20 @@ -551,6 +682,7 @@ virCgroupBackend virCgroupV1Backend =3D { .hasController =3D virCgroupV1HasController, .getAnyController =3D virCgroupV1GetAnyController, .pathOfController =3D virCgroupV1PathOfController, + .makeGroup =3D virCgroupV1MakeGroup, }; =20 =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list