From nobody Sun Feb 8 12:14:21 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 1546438144116373.2193393576897; Wed, 2 Jan 2019 06:09:04 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F21B9C05680E; Wed, 2 Jan 2019 14:09:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 24328101963D; Wed, 2 Jan 2019 14:09:01 +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 75E9D3F604; Wed, 2 Jan 2019 14:09:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x02E90Xt000886 for ; Wed, 2 Jan 2019 09:09:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id 154BF608DC; Wed, 2 Jan 2019 14:09:00 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F5E1608D9 for ; Wed, 2 Jan 2019 14:08:59 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Wed, 2 Jan 2019 15:08:37 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 05/19] vircgroup: introduce virCgroupV2DeviceDetectProg 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.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 02 Jan 2019 14:09:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This function will be called if libvirtd was restarted while some domains were running. It will try to detect existing programs attached to the guest cgroup. Signed-off-by: Pavel Hrdina --- src/util/vircgroupv2.c | 105 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 7a8cc040eb..86adbc4ff9 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -1723,6 +1723,111 @@ virCgroupV2DeviceAttachProg(virCgroupPtr group, } =20 =20 +static int +virCgroupV2DeviceCountMapEntries(int mapfd) +{ + int ret =3D 0; + int rc; + __u64 key =3D 0; + __u64 prevKey =3D 0; + + while ((rc =3D virBPFGetNextElem(mapfd, &prevKey, &key)) =3D=3D 0) { + ret++; + prevKey =3D key; + } + + if (rc < 0) + return -1; + + return ret; +} + + +# define MAX_PROG_IDS 10 + +static int +virCgroupV2DeviceDetectProg(virCgroupPtr group) +{ + int ret =3D -1; + int cgroupfd =3D -1; + unsigned int progcnt =3D 0; + unsigned int progids[MAX_PROG_IDS] =3D { 0 }; + VIR_AUTOFREE(char *) path =3D NULL; + + if (group->unified.devices.progfd > 0 && group->unified.devices.mapfd = > 0) + return 0; + + if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_DEVICES, + NULL, &path) < 0) { + return -1; + } + + cgroupfd =3D open(path, O_RDONLY); + if (cgroupfd < 0) { + virReportSystemError(errno, _("unable to open '%s'"), path); + goto cleanup; + } + + if (virBPFQueryProg(cgroupfd, MAX_PROG_IDS, BPF_CGROUP_DEVICE, + &progcnt, progids) < 0) { + virReportSystemError(errno, "%s", _("unable to query cgroup BPF pr= ogs")); + goto cleanup; + } + + if (progcnt > 0) { + int progfd =3D virBPFGetProg(progids[0]); + int mapfd =3D -1; + int nitems =3D -1; + struct bpf_prog_info progInfo =3D { 0 }; + struct bpf_map_info mapInfo =3D { 0 }; + VIR_AUTOFREE(unsigned int *) mapIDs =3D NULL; + + if (progfd < 0) { + virReportSystemError(errno, "%s", _("failed to get cgroup BPF = prog FD")); + goto cleanup; + } + + if (virBPFGetProgInfo(progfd, &progInfo, &mapIDs) < 0) { + virReportSystemError(errno, "%s", _("failed to get cgroup BPF = prog info")); + goto cleanup; + } + + if (progInfo.nr_map_ids =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("no map for cgroup BPF prog")); + goto cleanup; + } + + mapfd =3D virBPFGetMap(mapIDs[0]); + if (mapfd < 0) { + virReportSystemError(errno, "%s", _("failed to get cgroup BPF = map FD")); + goto cleanup; + } + + if (virBPFGetMapInfo(mapfd, &mapInfo) < 0) { + virReportSystemError(errno, "%s", _("failed to get cgroup BPF = map info")); + goto cleanup; + } + + nitems =3D virCgroupV2DeviceCountMapEntries(mapfd); + if (nitems < 0) { + virReportSystemError(errno, "%s", _("failed to count cgroup BP= F map items")); + goto cleanup; + } + + group->unified.devices.progfd =3D progfd; + group->unified.devices.mapfd =3D mapfd; + group->unified.devices.max =3D mapInfo.max_entries; + group->unified.devices.count =3D nitems; + } + + ret =3D 0; + cleanup: + VIR_FORCE_CLOSE(cgroupfd); + return ret; +} + + virCgroupBackend virCgroupV2Backend =3D { .type =3D VIR_CGROUP_BACKEND_TYPE_V2, =20 --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list