22c213
From 4cc435b3a8a9a419cc85ee883d5184f810f91e52 Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:34 +0100
22c213
Subject: [PATCH 063/116] virtiofsd: set maximum RLIMIT_NOFILE limit
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200127190227.40942-60-dgilbert@redhat.com>
22c213
Patchwork-id: 93516
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 059/112] virtiofsd: set maximum RLIMIT_NOFILE limit
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: Stefan Hajnoczi <stefanha@redhat.com>
22c213
22c213
virtiofsd can exceed the default open file descriptor limit easily on
22c213
most systems.  Take advantage of the fact that it runs as root to raise
22c213
the limit.
22c213
22c213
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
22c213
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 01a6dc95ec7f71eeff9963fe3cb03d85225fba3e)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/passthrough_ll.c | 32 ++++++++++++++++++++++++++++++++
22c213
 1 file changed, 32 insertions(+)
22c213
22c213
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
22c213
index d53cb1e..c281d81 100644
22c213
--- a/tools/virtiofsd/passthrough_ll.c
22c213
+++ b/tools/virtiofsd/passthrough_ll.c
22c213
@@ -53,6 +53,7 @@
22c213
 #include <sys/file.h>
22c213
 #include <sys/mount.h>
22c213
 #include <sys/prctl.h>
22c213
+#include <sys/resource.h>
22c213
 #include <sys/syscall.h>
22c213
 #include <sys/types.h>
22c213
 #include <sys/wait.h>
22c213
@@ -2268,6 +2269,35 @@ static void setup_sandbox(struct lo_data *lo, struct fuse_session *se)
22c213
     setup_seccomp();
22c213
 }
22c213
 
22c213
+/* Raise the maximum number of open file descriptors */
22c213
+static void setup_nofile_rlimit(void)
22c213
+{
22c213
+    const rlim_t max_fds = 1000000;
22c213
+    struct rlimit rlim;
22c213
+
22c213
+    if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
22c213
+        fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n");
22c213
+        exit(1);
22c213
+    }
22c213
+
22c213
+    if (rlim.rlim_cur >= max_fds) {
22c213
+        return; /* nothing to do */
22c213
+    }
22c213
+
22c213
+    rlim.rlim_cur = max_fds;
22c213
+    rlim.rlim_max = max_fds;
22c213
+
22c213
+    if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
22c213
+        /* Ignore SELinux denials */
22c213
+        if (errno == EPERM) {
22c213
+            return;
22c213
+        }
22c213
+
22c213
+        fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n");
22c213
+        exit(1);
22c213
+    }
22c213
+}
22c213
+
22c213
 int main(int argc, char *argv[])
22c213
 {
22c213
     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
22c213
@@ -2389,6 +2419,8 @@ int main(int argc, char *argv[])
22c213
 
22c213
     fuse_daemonize(opts.foreground);
22c213
 
22c213
+    setup_nofile_rlimit();
22c213
+
22c213
     /* Must be before sandbox since it wants /proc */
22c213
     setup_capng();
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213