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