From nobody Sun Feb 8 14:56:24 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 1544609849387434.20088693214825; Wed, 12 Dec 2018 02:17:29 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B5DDE3082194; Wed, 12 Dec 2018 10:17:27 +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 ED5155D738; Wed, 12 Dec 2018 10:17:26 +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 19767181B9E5; Wed, 12 Dec 2018 10:17:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id wBCAHNOO025919 for ; Wed, 12 Dec 2018 05:17:23 -0500 Received: by smtp.corp.redhat.com (Postfix) id 58E165D738; Wed, 12 Dec 2018 10:17:23 +0000 (UTC) Received: from antique-laptop.redhat.com (ovpn-12-137.pek2.redhat.com [10.72.12.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id 67B585D70A for ; Wed, 12 Dec 2018 10:17:22 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Wed, 12 Dec 2018 18:17:16 +0800 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/2] vircgroup: introduce virCgroupKillRecursiveCB 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: , 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 12 Dec 2018 10:17:28 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The rewrite to support cgroup v2 missed this function. In cgroup v2 we have different files to track tasks. We would fail to remove cgroup on non-systemd OSes if there is any extra process assigned to guest cgroup because we would not kill any process form the guest cgroup. Signed-off-by: Pavel Hrdina --- src/util/vircgroup.c | 69 ++++++++++++++++++++----------------- src/util/vircgroupbackend.h | 7 ++++ src/util/vircgrouppriv.h | 8 +++++ src/util/vircgroupv1.c | 16 +++++++++ src/util/vircgroupv2.c | 16 +++++++++ 5 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index e20df3ea05..afb12a6436 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -2427,33 +2427,15 @@ virCgroupRemove(virCgroupPtr group) } =20 =20 -static int -virCgroupPathOfAnyController(virCgroupPtr group, - const char *name, - char **keypath) -{ - size_t i; - int controller; - - for (i =3D 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) { - if (group->backends[i]) { - controller =3D group->backends[i]->getAnyController(group); - if (controller >=3D 0) - return virCgroupPathOfController(group, controller, name, = keypath); - } - } - - virReportSystemError(ENOSYS, "%s", - _("No controllers are mounted")); - return -1; -} - - /* * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error */ static int -virCgroupKillInternal(virCgroupPtr group, int signum, virHashTablePtr pids) +virCgroupKillInternal(virCgroupPtr group, + int signum, + virHashTablePtr pids, + int controller, + const char *taskFile) { int ret =3D -1; bool killedAny =3D false; @@ -2463,7 +2445,7 @@ virCgroupKillInternal(virCgroupPtr group, int signum,= virHashTablePtr pids) VIR_DEBUG("group=3D%p path=3D%s signum=3D%d pids=3D%p", group, group->path, signum, pids); =20 - if (virCgroupPathOfAnyController(group, "tasks", &keypath) < 0) + if (virCgroupPathOfController(group, controller, taskFile, &keypath) <= 0) return -1; =20 /* PIDs may be forking as we kill them, so loop @@ -2549,10 +2531,12 @@ virCgroupPidCopy(const void *name) } =20 =20 -static int +int virCgroupKillRecursiveInternal(virCgroupPtr group, int signum, virHashTablePtr pids, + int controller, + const char *taskFile, bool dormdir) { int ret =3D -1; @@ -2566,11 +2550,13 @@ virCgroupKillRecursiveInternal(virCgroupPtr group, VIR_DEBUG("group=3D%p path=3D%s signum=3D%d pids=3D%p", group, group->path, signum, pids); =20 - if (virCgroupPathOfAnyController(group, "", &keypath) < 0) + if (virCgroupPathOfController(group, controller, "", &keypath) < 0) return -1; =20 - if ((rc =3D virCgroupKillInternal(group, signum, pids)) < 0) + if ((rc =3D virCgroupKillInternal(group, signum, pids, + controller, taskFile)) < 0) { goto cleanup; + } if (rc =3D=3D 1) killedAny =3D true; =20 @@ -2594,7 +2580,7 @@ virCgroupKillRecursiveInternal(virCgroupPtr group, goto cleanup; =20 if ((rc =3D virCgroupKillRecursiveInternal(subgroup, signum, pids, - true)) < 0) + controller, taskFile, tru= e)) < 0) goto cleanup; if (rc =3D=3D 1) killedAny =3D true; @@ -2620,8 +2606,10 @@ virCgroupKillRecursiveInternal(virCgroupPtr group, int virCgroupKillRecursive(virCgroupPtr group, int signum) { - int ret; - VIR_DEBUG("group=3D%p path=3D%s signum=3D%d", group, group->path, sign= um); + int ret =3D 0; + int rc; + size_t i; + virCgroupBackendPtr *backends =3D virCgroupBackendGetAll(); virHashTablePtr pids =3D virHashCreateFull(100, NULL, virCgroupPidCode, @@ -2629,10 +2617,27 @@ virCgroupKillRecursive(virCgroupPtr group, int sign= um) virCgroupPidCopy, NULL); =20 - ret =3D virCgroupKillRecursiveInternal(group, signum, pids, false); + VIR_DEBUG("group=3D%p path=3D%s signum=3D%d", group, group->path, sign= um); =20 + if (!backends) { + ret =3D -1; + goto cleanup; + } + + for (i =3D 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) { + if (backends[i]) { + rc =3D backends[i]->killRecursive(group, signum, pids); + if (rc < 0) { + ret =3D -1; + goto cleanup; + } + if (rc > 0) + ret =3D rc; + } + } + + cleanup: virHashFree(pids); - return ret; } =20 diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index bc60b44643..a825dc4be7 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -24,6 +24,7 @@ # include "internal.h" =20 # include "vircgroup.h" +# include "virhash.h" =20 # define CGROUP_MAX_VAL 512 =20 @@ -128,6 +129,11 @@ typedef int (*virCgroupHasEmptyTasksCB)(virCgroupPtr cgroup, int controller); =20 +typedef int +(*virCgroupKillRecursiveCB)(virCgroupPtr group, + int signum, + virHashTablePtr pids); + typedef int (*virCgroupBindMountCB)(virCgroupPtr group, const char *oldroot, @@ -370,6 +376,7 @@ struct _virCgroupBackend { virCgroupRemoveCB remove; virCgroupAddTaskCB addTask; virCgroupHasEmptyTasksCB hasEmptyTasks; + virCgroupKillRecursiveCB killRecursive; virCgroupBindMountCB bindMount; virCgroupSetOwnerCB setOwner; =20 diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h index 8f24b0891e..6067f5cdc8 100644 --- a/src/util/vircgrouppriv.h +++ b/src/util/vircgrouppriv.h @@ -123,4 +123,12 @@ int virCgroupNewDomainPartition(virCgroupPtr partition, =20 int virCgroupRemoveRecursively(char *grppath); =20 + +int virCgroupKillRecursiveInternal(virCgroupPtr group, + int signum, + virHashTablePtr pids, + int controller, + const char *taskFile, + bool dormdir); + #endif /* __VIR_CGROUP_PRIV_H__ */ diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index ab1a2870a3..45378be34b 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -757,6 +757,21 @@ virCgroupV1HasEmptyTasks(virCgroupPtr cgroup, } =20 =20 +static int +virCgroupV1KillRecursive(virCgroupPtr group, + int signum, + virHashTablePtr pids) +{ + int controller =3D virCgroupV1GetAnyController(group); + + if (controller < 0) + return -1; + + return virCgroupKillRecursiveInternal(group, signum, pids, controller, + "tasks", false); +} + + static char * virCgroupV1IdentifyRoot(virCgroupPtr group) { @@ -2041,6 +2056,7 @@ virCgroupBackend virCgroupV1Backend =3D { .remove =3D virCgroupV1Remove, .addTask =3D virCgroupV1AddTask, .hasEmptyTasks =3D virCgroupV1HasEmptyTasks, + .killRecursive =3D virCgroupV1KillRecursive, .bindMount =3D virCgroupV1BindMount, .setOwner =3D virCgroupV1SetOwner, =20 diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 85aa62bd4c..dc2b2a65bc 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -462,6 +462,21 @@ virCgroupV2HasEmptyTasks(virCgroupPtr cgroup, } =20 =20 +static int +virCgroupV2KillRecursive(virCgroupPtr group, + int signum, + virHashTablePtr pids) +{ + int controller =3D virCgroupV2GetAnyController(group); + + if (controller < 0) + return -1; + + return virCgroupKillRecursiveInternal(group, signum, pids, controller, + "cgroup.threads", false); +} + + static int virCgroupV2BindMount(virCgroupPtr group, const char *oldroot, @@ -1558,6 +1573,7 @@ virCgroupBackend virCgroupV2Backend =3D { .remove =3D virCgroupV2Remove, .addTask =3D virCgroupV2AddTask, .hasEmptyTasks =3D virCgroupV2HasEmptyTasks, + .killRecursive =3D virCgroupV2KillRecursive, .bindMount =3D virCgroupV2BindMount, .setOwner =3D virCgroupV2SetOwner, =20 --=20 2.19.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list