[libvirt] [PATCH 08/19] vircgroup: introduce virCgroupV2DevicePrepareProg

Pavel Hrdina phrdina at redhat.com
Wed Jan 2 14:08:40 UTC 2019


This function will be called for every virCgroup(Allow|Deny)* API in
order to prepare BPF program for guest.  Since libvirtd can be restarted
at any point we will first try to detect existing progam, if there is
none we will create a new empty BPF program and lastly if we don't have
any space left in the existing BPF map we will create a new copy of the
BPF map with more space and attach a new program with that map into the
guest cgroup.

This solution allows us to start with reasonably small BPF map consuming
only small amount of memory and if needed we can easily extend the BPF
map if there is a lot of host devices used in guest or if user wants to
hot-plug a lot of devices once the guest is running.

This overcomes all the limitations in BPF:

    - map used in program has to be created before the program is loaded
      into kernel

    - once map is created you cannot change its size

    - you cannot replace map in existing program

    - you cannot use an array of maps because it can store FD to maps
      of one specific size so we would not be able to use it to overcome
      the second issue

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/util/vircgroupv2.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index d70eefd92f..aea094a328 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -1914,6 +1914,33 @@ virCgroupV2DeviceCreateProg(virCgroupPtr group)
 }
 
 
+static int
+virCgroupV2DevicePrepareProg(virCgroupPtr group)
+{
+    if (virCgroupV2DeviceDetectProg(group) < 0)
+        return -1;
+
+    if (virCgroupV2DeviceCreateProg(group) < 0)
+        return -1;
+
+    if (group->unified.devices.count >= group->unified.devices.max) {
+        size_t max = group->unified.devices.max * 2;
+        int newmapfd = virCgroupV2DeviceReallocMap(group->unified.devices.mapfd,
+                                                   max);
+
+        if (newmapfd < 0)
+            return -1;
+
+        if (virCgroupV2DeviceAttachProg(group, newmapfd, max) < 0) {
+            VIR_FORCE_CLOSE(newmapfd);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV2Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
 
-- 
2.20.1




More information about the libvir-list mailing list