File d5572f62-qemu-support-override-max-thread.patch of Package libvirt.22293

commit d5572f62e329930d0533f5221839724e4fdeeb33
Author: Jim Fehlig <jfehlig@suse.com>
Date:   Wed May 22 17:12:14 2019 -0600

    qemu: Add support for overriding max threads per process limit
    
    Some VM configurations may result in a large number of threads created by
    the associated qemu process which can exceed the system default limit. The
    maximum number of threads allowed per process is controlled by the pids
    cgroup controller and is set to 16k when creating VMs with systemd's
    machined service. The maximum number of threads per process is recorded
    in the pids.max file under the machine's pids controller cgroup hierarchy,
    e.g.
    
    $cgrp-mnt/pids/machine.slice/machine-qemu\\x2d1\\x2dtest.scope/pids.max
    
    Maximum threads per process is controlled with the TasksMax property of
    the systemd scope for the machine. This patch adds an option to qemu.conf
    which can be used to override the maximum number of threads allowed per
    qemu process. If the value of option is greater than zero, it will be set
    in the TasksMax property of the machine's scope after creating the machine.
    
    Signed-off-by: Jim Fehlig <jfehlig@suse.com>
    Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

Index: libvirt-4.0.0/src/lxc/lxc_cgroup.c
===================================================================
--- libvirt-4.0.0.orig/src/lxc/lxc_cgroup.c
+++ libvirt-4.0.0/src/lxc/lxc_cgroup.c
@@ -506,6 +506,7 @@ virCgroupPtr virLXCCgroupCreate(virDomai
                             nnicindexes, nicindexes,
                             def->resource->partition,
                             -1,
+                            0,
                             &cgroup) < 0)
         goto cleanup;
 
Index: libvirt-4.0.0/src/qemu/libvirtd_qemu.aug
===================================================================
--- libvirt-4.0.0.orig/src/qemu/libvirtd_qemu.aug
+++ libvirt-4.0.0/src/qemu/libvirtd_qemu.aug
@@ -92,6 +92,7 @@ module Libvirtd_qemu =
                  | limits_entry "max_core"
                  | bool_entry "dump_guest_core"
                  | str_entry "stdio_handler"
+                 | int_entry "max_threads_per_process"
 
    let device_entry = bool_entry "mac_filter"
                  | bool_entry "relaxed_acs_check"
Index: libvirt-4.0.0/src/qemu/qemu.conf
===================================================================
--- libvirt-4.0.0.orig/src/qemu/qemu.conf
+++ libvirt-4.0.0/src/qemu/qemu.conf
@@ -569,6 +569,16 @@
 #max_processes = 0
 #max_files = 0
 
+# If max_threads_per_process is set to a positive integer, libvirt
+# will use it to set the maximum number of threads that can be
+# created by a qemu process. Some VM configurations can result in
+# qemu processes with tens of thousands of threads. systemd-based
+# systems typically limit the number of threads per process to
+# 16k. max_threads_per_process can be used to override default
+# limits in the host OS.
+#
+#max_threads_per_process = 0
+
 # If max_core is set to a non-zero integer, then QEMU will be
 # permitted to create core dumps when it crashes, provided its
 # RAM size is smaller than the limit set.
Index: libvirt-4.0.0/src/qemu/qemu_cgroup.c
===================================================================
--- libvirt-4.0.0.orig/src/qemu/qemu_cgroup.c
+++ libvirt-4.0.0/src/qemu/qemu_cgroup.c
@@ -900,6 +900,7 @@ qemuInitCgroup(virDomainObjPtr vm,
                             nnicindexes, nicindexes,
                             vm->def->resource->partition,
                             cfg->cgroupControllers,
+                            cfg->maxThreadsPerProc,
                             &priv->cgroup) < 0) {
         if (virCgroupNewIgnoreError())
             goto done;
Index: libvirt-4.0.0/src/qemu/qemu_conf.c
===================================================================
--- libvirt-4.0.0.orig/src/qemu/qemu_conf.c
+++ libvirt-4.0.0/src/qemu/qemu_conf.c
@@ -774,6 +774,8 @@ int virQEMUDriverConfigLoadFile(virQEMUD
         goto cleanup;
     if (virConfGetValueUInt(conf, "max_files", &cfg->maxFiles) < 0)
         goto cleanup;
+    if (virConfGetValueUInt(conf, "max_threads_per_process", &cfg->maxThreadsPerProc) < 0)
+        goto cleanup;
 
     if (virConfGetValueType(conf, "max_core") == VIR_CONF_STRING) {
         if (virConfGetValueString(conf, "max_core", &corestr) < 0)
Index: libvirt-4.0.0/src/qemu/qemu_conf.h
===================================================================
--- libvirt-4.0.0.orig/src/qemu/qemu_conf.h
+++ libvirt-4.0.0/src/qemu/qemu_conf.h
@@ -165,6 +165,7 @@ struct _virQEMUDriverConfig {
 
     unsigned int maxProcesses;
     unsigned int maxFiles;
+    unsigned int maxThreadsPerProc;
     unsigned long long maxCore;
     bool dumpGuestCore;
 
Index: libvirt-4.0.0/src/qemu/test_libvirtd_qemu.aug.in
===================================================================
--- libvirt-4.0.0.orig/src/qemu/test_libvirtd_qemu.aug.in
+++ libvirt-4.0.0/src/qemu/test_libvirtd_qemu.aug.in
@@ -73,6 +73,7 @@ module Test_libvirtd_qemu =
 { "set_process_name" = "1" }
 { "max_processes" = "0" }
 { "max_files" = "0" }
+{ "max_threads_per_process" = "0" }
 { "max_core" = "unlimited" }
 { "dump_guest_core" = "1" }
 { "mac_filter" = "1" }
Index: libvirt-4.0.0/src/util/vircgroup.c
===================================================================
--- libvirt-4.0.0.orig/src/util/vircgroup.c
+++ libvirt-4.0.0/src/util/vircgroup.c
@@ -1572,6 +1572,7 @@ virCgroupNewMachineSystemd(const char *n
                            int *nicindexes,
                            const char *partition,
                            int controllers,
+                           unsigned int maxthreads,
                            virCgroupPtr *group)
 {
     int ret = -1;
@@ -1589,7 +1590,8 @@ virCgroupNewMachineSystemd(const char *n
                                       isContainer,
                                       nnicindexes,
                                       nicindexes,
-                                      partition)) < 0)
+                                      partition,
+                                      maxthreads)) < 0)
         return rv;
 
     if (controllers != -1)
@@ -1735,6 +1737,7 @@ virCgroupNewMachine(const char *name,
                     int *nicindexes,
                     const char *partition,
                     int controllers,
+                    unsigned int maxthreads,
                     virCgroupPtr *group)
 {
     int rv;
@@ -1751,6 +1754,7 @@ virCgroupNewMachine(const char *name,
                                          nicindexes,
                                          partition,
                                          controllers,
+                                         maxthreads,
                                          group)) == 0)
         return 0;
 
Index: libvirt-4.0.0/src/util/vircgroup.h
===================================================================
--- libvirt-4.0.0.orig/src/util/vircgroup.h
+++ libvirt-4.0.0/src/util/vircgroup.h
@@ -113,6 +113,7 @@ int virCgroupNewMachine(const char *name
                         int *nicindexes,
                         const char *partition,
                         int controllers,
+                        unsigned int maxthreads,
                         virCgroupPtr *group)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
     ATTRIBUTE_NONNULL(3);
Index: libvirt-4.0.0/src/util/virsystemd.c
===================================================================
--- libvirt-4.0.0.orig/src/util/virsystemd.c
+++ libvirt-4.0.0/src/util/virsystemd.c
@@ -238,12 +238,14 @@ int virSystemdCreateMachine(const char *
                             bool iscontainer,
                             size_t nnicindexes,
                             int *nicindexes,
-                            const char *partition)
+                            const char *partition,
+                            unsigned int maxthreads)
 {
     int ret;
     DBusConnection *conn;
     char *creatorname = NULL;
     char *slicename = NULL;
+    char *scopename = NULL;
     static int hasCreateWithNetwork = 1;
 
     if ((ret = virSystemdHasMachined()) < 0)
@@ -389,11 +391,31 @@ int virSystemdCreateMachine(const char *
             goto cleanup;
     }
 
+    if (maxthreads > 0) {
+        if (!(scopename = virSystemdMakeScopeName(name, drivername, false)))
+            goto cleanup;
+
+        if (virDBusCallMethod(conn,
+                              NULL,
+                              NULL,
+                              "org.freedesktop.systemd1",
+                              "/org/freedesktop/systemd1",
+                              "org.freedesktop.systemd1.Manager",
+                              "SetUnitProperties",
+                              "sba(sv)",
+                              scopename,
+                              true,
+                              1,
+                              "TasksMax", "t", (uint64_t)maxthreads) < 0)
+            goto cleanup;
+    }
+
     ret = 0;
 
  cleanup:
     VIR_FREE(creatorname);
     VIR_FREE(slicename);
+    VIR_FREE(scopename);
     return ret;
 }
 
Index: libvirt-4.0.0/src/util/virsystemd.h
===================================================================
--- libvirt-4.0.0.orig/src/util/virsystemd.h
+++ libvirt-4.0.0/src/util/virsystemd.h
@@ -37,7 +37,8 @@ int virSystemdCreateMachine(const char *
                             bool iscontainer,
                             size_t nnicindexes,
                             int *nicindexes,
-                            const char *partition);
+                            const char *partition,
+                            unsigned int maxthreads);
 
 int virSystemdTerminateMachine(const char *name);
 
Index: libvirt-4.0.0/tests/virsystemdtest.c
===================================================================
--- libvirt-4.0.0.orig/tests/virsystemdtest.c
+++ libvirt-4.0.0/tests/virsystemdtest.c
@@ -175,7 +175,7 @@ static int testCreateContainer(const voi
                                 123,
                                 true,
                                 0, NULL,
-                                "highpriority.slice") < 0) {
+                                "highpriority.slice", 0) < 0) {
         fprintf(stderr, "%s", "Failed to create LXC machine\n");
         return -1;
     }
@@ -208,7 +208,7 @@ static int testCreateMachine(const void
                                 123,
                                 false,
                                 0, NULL,
-                                NULL) < 0) {
+                                NULL, 0) < 0) {
         fprintf(stderr, "%s", "Failed to create KVM machine\n");
         return -1;
     }
@@ -245,7 +245,7 @@ static int testCreateNoSystemd(const voi
                                       123,
                                       false,
                                       0, NULL,
-                                      NULL)) == 0) {
+                                      NULL, 0)) == 0) {
         unsetenv("FAIL_NO_SERVICE");
         fprintf(stderr, "%s", "Unexpected create machine success\n");
         return -1;
@@ -279,7 +279,7 @@ static int testCreateSystemdNotRunning(c
                                       123,
                                       false,
                                       0, NULL,
-                                      NULL)) == 0) {
+                                      NULL, 0)) == 0) {
         unsetenv("FAIL_NOT_REGISTERED");
         fprintf(stderr, "%s", "Unexpected create machine success\n");
         return -1;
@@ -313,7 +313,7 @@ static int testCreateBadSystemd(const vo
                                       123,
                                       false,
                                       0, NULL,
-                                      NULL)) == 0) {
+                                      NULL, 0)) == 0) {
         unsetenv("FAIL_BAD_SERVICE");
         fprintf(stderr, "%s", "Unexpected create machine success\n");
         return -1;
@@ -348,7 +348,7 @@ static int testCreateNetwork(const void
                                 123,
                                 true,
                                 nnicindexes, nicindexes,
-                                "highpriority.slice") < 0) {
+                                "highpriority.slice", 0) < 0) {
         fprintf(stderr, "%s", "Failed to create LXC machine\n");
         return -1;
     }
openSUSE Build Service is sponsored by