]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircgroup: introduce virCgroupV2AllowDevice
authorPavel Hrdina <phrdina@redhat.com>
Wed, 24 Apr 2019 10:10:08 +0000 (12:10 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Fri, 15 Nov 2019 11:58:39 +0000 (12:58 +0100)
In order to allow device we need to create key and value which will be
used to update BPF map.  virBPFUpdateElem() can override existing
entries in BPF map so we need to check if that entry exists in order to
track number of entries in our map.

This can add rule for specific device but major and minor can be both
-1 which follows the same behavior as in cgroup v1.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/vircgroupv2.c

index 6bce9012ae3885bafb367069ab8c34db76fed26b..5396972d80c0f84e568ebfb18410b0f58eb8e835 100644 (file)
@@ -30,6 +30,7 @@
 #include "vircgrouppriv.h"
 
 #include "viralloc.h"
+#include "virbpf.h"
 #include "vircgroup.h"
 #include "vircgroupbackend.h"
 #include "vircgroupv2.h"
@@ -1737,6 +1738,35 @@ virCgroupV2GetCpusetCpus(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV2AllowDevice(virCgroupPtr group,
+                       char type,
+                       int major,
+                       int minor,
+                       int perms)
+{
+    uint64_t key = virCgroupV2DevicesGetKey(major, minor);
+    uint32_t val = virCgroupV2DevicesGetPerms(perms, type);
+    int rc;
+
+    if (virCgroupV2DevicesPrepareProg(group) < 0)
+        return -1;
+
+    rc = virBPFLookupElem(group->unified.devices.mapfd, &key, NULL);
+
+    if (virBPFUpdateElem(group->unified.devices.mapfd, &key, &val) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("failed to update device in BPF cgroup map"));
+        return -1;
+    }
+
+    if (rc < 0)
+        group->unified.devices.count++;
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV2Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
 
@@ -1786,6 +1816,8 @@ virCgroupBackend virCgroupV2Backend = {
     .getMemSwapHardLimit = virCgroupV2GetMemSwapHardLimit,
     .getMemSwapUsage = virCgroupV2GetMemSwapUsage,
 
+    .allowDevice = virCgroupV2AllowDevice,
+
     .setCpuShares = virCgroupV2SetCpuShares,
     .getCpuShares = virCgroupV2GetCpuShares,
     .setCpuCfsPeriod = virCgroupV2SetCpuCfsPeriod,