From nobody Sat Feb 7 10:47:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618260014; cv=none; d=zohomail.com; s=zohoarc; b=ANye2eqOitJpTVdjlvbpx6jIaUo6/O4cPP0xdRLl2DIxUmJ7gUC697GSpt+PLIhDRKBLCCR5EKsSpkBNgSrl/0rh7DIYDNSCHCGcoEqUWDaJoj1/1R6V6Wd2dh1L1DTwkus296PPTO1ySRGIrQ4yJLi7IADOzKoTzolB2LX4ErE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618260014; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=EVXsoxgV7gtRDRd1xGcoA3SJlJULSZViZqSq5i93vdo=; b=Yg/KPlL9JnxauFAVPQd571qlj+GA+GgLUnRoLFlPekas3YNYuChyX96zR/bAtaFtxu9WQo1sbwGFjFG9BHXIrsY12w5zfM9AIy8CiVaKUX+BqkjeOHh66YQRl0aCFtOOy+6uoLu22qcmskQJYwq3SvISfiii5tdyKnlD8M7zVBE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1618260014542275.9670011098474; Mon, 12 Apr 2021 13:40:14 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-120-ioVimrxXNHa25gMvJFhbzw-1; Mon, 12 Apr 2021 16:40:11 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DE2A7107ACE3; Mon, 12 Apr 2021 20:40:06 +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 B13D960C04; Mon, 12 Apr 2021 20:40:06 +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 5E60C1806D0E; Mon, 12 Apr 2021 20:40:06 +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 13CKc7rF003749 for ; Mon, 12 Apr 2021 16:38:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 07BCF5D719; Mon, 12 Apr 2021 20:38:07 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77D9C5D6D1 for ; Mon, 12 Apr 2021 20:38:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618260013; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=EVXsoxgV7gtRDRd1xGcoA3SJlJULSZViZqSq5i93vdo=; b=RdEpXjzCsHIoGbDecTpAzjy8MFGJJcd0HIe4bKEgiISWxrm/x0UhAmDhKO8aSftGY6fsgE 1dcst6ivbOrOFwEmZLgv/S+PWYQPISryAuvIqfzHfLJ/P7oMzy8G1qY4agR0zMJdxL9ubD i7eoQJfIs3dEz9uIWa4lKdN0q4Qtc2M= X-MC-Unique: ioVimrxXNHa25gMvJFhbzw-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 1/2] vircgroupv2: properly free BPF prog and map FDs Date: Mon, 12 Apr 2021 22:38:00 +0200 Message-Id: <994de7fa1902331813b4ea2937adeff2c2aedc0f.1618259787.git.phrdina@redhat.com> 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 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" When nested cgroup was introduced it did not properly free file descriptors for BPF prog and map. With nested cgroups we create the BPF bits in the nested cgroup instead of the VM root cgroup. This would leak the FDs which would be the last reference to the prog and map so kernel would not remove the resources as well. It would only happen once libvirtd process exits. Fixes: 184245f53b94fc84f727eb6e8a2aa52df02d69c0 Reported-by: Eric Farman Signed-off-by: Pavel Hrdina Reviewed-by: Eric Farman Reviewed-by: Michal Privoznik Tested-by: Eric Farman --- src/util/vircgroupv2.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 248d4047e5..4664492c34 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -523,6 +523,7 @@ static int virCgroupV2Remove(virCgroupPtr group) { g_autofree char *grppath =3D NULL; + virCgroupPtr parent =3D virCgroupGetNested(group); int controller; =20 /* Don't delete the root group, if we accidentally @@ -534,7 +535,7 @@ virCgroupV2Remove(virCgroupPtr group) if (virCgroupV2PathOfController(group, controller, "", &grppath) < 0) return 0; =20 - if (virCgroupV2DevicesRemoveProg(group) < 0) + if (virCgroupV2DevicesRemoveProg(parent) < 0) return -1; =20 return virCgroupRemoveRecursively(grppath); --=20 2.30.2 From nobody Sat Feb 7 10:47:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618260003; cv=none; d=zohomail.com; s=zohoarc; b=PHp3HLq6hoNB4OFYNmsthV+qIzSZuk5VNmXMvo4he9C73QsThBjtJKjDqWRme1RU5RMko2c7iNryevXcdJ9Y/+GVFuAzN+y0KYMczVF2Z9QEJKtKAggBSFiwMHx0GjZWI5Enzw5sNhnAM9ZaRhBQPnCFRJ4Luz7QjXQkqINAaQY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618260003; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zmcuRT2jX/yXLfR6YIvsNOAfz3LdH4nvw15WMc/srOA=; b=UqsdSz20tbBuzQmZgpDKZCuH8zZflK0IoevbhmSTJo8KcF5gzjqlZsSQKMJC0DVpqYnRjuQ6jjbdb1qWtHW1ctPNMak8/k8fm0WueRylLFeXLFutDUA3qn5FWrf5u7ca+2ZGrMWKoJvYKugVKuR9p4UPvs+jsnU+kRNFB6f5kAA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1618260003625219.30118627694117; Mon, 12 Apr 2021 13:40:03 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-210-zEximCibN9ShyrF7cD4xtA-1; Mon, 12 Apr 2021 16:40:00 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 130088189CB; Mon, 12 Apr 2021 20:39:55 +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 DA30C60C04; Mon, 12 Apr 2021 20:39:54 +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 181B844A60; Mon, 12 Apr 2021 20:39:54 +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 13CKc7Tu003754 for ; Mon, 12 Apr 2021 16:38:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id DE8285D6D1; Mon, 12 Apr 2021 20:38:07 +0000 (UTC) Received: from antique-work.redhat.com (unknown [10.40.195.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5BA5C5D71D for ; Mon, 12 Apr 2021 20:38:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618260002; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=zmcuRT2jX/yXLfR6YIvsNOAfz3LdH4nvw15WMc/srOA=; b=isd7iGccTQxudJXVthPtCFc4GNd2a8O3RuydCveyn/+Bjrh0koOLbwue01tK4DKVlI7pFf aEg6Qpi4GWEIzPmJ1+fdrx0dOAT4x5Xi1+LBcqP+RIdFy+4JaBWvZMW7II94v02oy5swbY 2FCGYrVvFboD/EUAyxr2x+9d423ZGu0= X-MC-Unique: zEximCibN9ShyrF7cD4xtA-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 2/2] vircgroupv2devices: refactor virCgroupV2DevicesRemoveProg Date: Mon, 12 Apr 2021 22:38:01 +0200 Message-Id: <83bae4da3569e66c8a4fccceeb483bb79f631969.1618259787.git.phrdina@redhat.com> 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 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" When running on systemd host the cgroup itself is removed by machined so when we reach this code the directory no longer exist. If libvirtd was running the whole time between starting and destroying VM the detection is skipped because we still have both FD in memory. But if libvirtd was restarted and no operation requiring cgroup devices executed the FDs would be 0 and libvirt would try to detect them using the cgroup directory. This results in reporting following errors: libvirtd[955]: unable to open '/sys/fs/cgroup/machine.slice/machine-qem= u\x2d1\x2dguest.scope/': No such file or directory libvirtd[955]: Failed to remove cgroup for guest When running on non-systemd host where we handle cgroups manually this would not happen. When destroying VM it is not necessary to detect the BPF prog and map because the following code only closes the FDs without doing anything else. We could run code that would try to detach the BPF prog from the cgroup but that is not necessary as well. If the cgroup is removed and there is no other FD open to the prog kernel will cleanup the prog and map eventually. Reported-by: Eric Farman Signed-off-by: Pavel Hrdina Reviewed-by: Eric Farman Reviewed-by: Michal Privoznik Tested-by: Eric Farman --- src/libvirt_private.syms | 2 +- src/util/vircgroupv2.c | 2 +- src/util/vircgroupv2devices.c | 14 ++++---------- src/util/vircgroupv2devices.h | 2 +- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 119768496b..0ccde7e1b4 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1963,12 +1963,12 @@ virCgroupV2Register; =20 # util/vircgroupv2devices.h virCgroupV2DevicesAvailable; +virCgroupV2DevicesCloseProg; virCgroupV2DevicesCreateProg; virCgroupV2DevicesDetectProg; virCgroupV2DevicesGetKey; virCgroupV2DevicesGetPerms; virCgroupV2DevicesPrepareProg; -virCgroupV2DevicesRemoveProg; =20 # util/vircommand.h virCommandAbort; diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c index 4664492c34..5bf087b39b 100644 --- a/src/util/vircgroupv2.c +++ b/src/util/vircgroupv2.c @@ -535,7 +535,7 @@ virCgroupV2Remove(virCgroupPtr group) if (virCgroupV2PathOfController(group, controller, "", &grppath) < 0) return 0; =20 - if (virCgroupV2DevicesRemoveProg(parent) < 0) + if (virCgroupV2DevicesCloseProg(parent) < 0) return -1; =20 return virCgroupRemoveRecursively(grppath); diff --git a/src/util/vircgroupv2devices.c b/src/util/vircgroupv2devices.c index 4bcc1d52fe..98ab6e8f5a 100644 --- a/src/util/vircgroupv2devices.c +++ b/src/util/vircgroupv2devices.c @@ -548,18 +548,12 @@ virCgroupV2DevicesPrepareProg(virCgroupPtr group) =20 =20 int -virCgroupV2DevicesRemoveProg(virCgroupPtr group) +virCgroupV2DevicesCloseProg(virCgroupPtr group) { - if (virCgroupV2DevicesDetectProg(group) < 0) - return -1; - - if (group->unified.devices.progfd <=3D 0 && group->unified.devices.map= fd <=3D 0) - return 0; - - if (group->unified.devices.mapfd >=3D 0) + if (group->unified.devices.mapfd > 0) VIR_FORCE_CLOSE(group->unified.devices.mapfd); =20 - if (group->unified.devices.progfd >=3D 0) + if (group->unified.devices.progfd > 0) VIR_FORCE_CLOSE(group->unified.devices.progfd); =20 return 0; @@ -629,7 +623,7 @@ virCgroupV2DevicesPrepareProg(virCgroupPtr group G_GNUC= _UNUSED) =20 =20 int -virCgroupV2DevicesRemoveProg(virCgroupPtr group G_GNUC_UNUSED) +virCgroupV2DevicesCloseProg(virCgroupPtr group G_GNUC_UNUSED) { return 0; } diff --git a/src/util/vircgroupv2devices.h b/src/util/vircgroupv2devices.h index 9d91256120..17ab07afa0 100644 --- a/src/util/vircgroupv2devices.h +++ b/src/util/vircgroupv2devices.h @@ -38,7 +38,7 @@ int virCgroupV2DevicesPrepareProg(virCgroupPtr group); =20 int -virCgroupV2DevicesRemoveProg(virCgroupPtr group); +virCgroupV2DevicesCloseProg(virCgroupPtr group); =20 uint32_t virCgroupV2DevicesGetPerms(int perms, --=20 2.30.2