ddf19c
From 555ec3463b3dbfd6e08eac7840419d176f113e46 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Tue, 5 May 2020 16:35:55 +0100
ddf19c
Subject: [PATCH 4/9] virtiofsd: add --rlimit-nofile=NUM option
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: <20200505163600.22956-3-dgilbert@redhat.com>
ddf19c
Patchwork-id: 96270
ddf19c
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 2/7] virtiofsd: add --rlimit-nofile=NUM option
ddf19c
Bugzilla: 1817445
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ddf19c
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
Make it possible to specify the RLIMIT_NOFILE on the command-line.
ddf19c
Users running multiple virtiofsd processes should allocate a certain
ddf19c
number to each process so that the system-wide limit can never be
ddf19c
exhausted.
ddf19c
ddf19c
When this option is set to 0 the rlimit is left at its current value.
ddf19c
This is useful when a management tool wants to configure the rlimit
ddf19c
itself.
ddf19c
ddf19c
The default behavior remains unchanged: try to set the limit to
ddf19c
1,000,000 file descriptors if the current rlimit is lower.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-Id: <20200501140644.220940-2-stefanha@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 6dbb716877728ce4eb51619885ef6ef4ada9565f)
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_lowlevel.h  |  1 +
ddf19c
 tools/virtiofsd/helper.c         | 23 +++++++++++++++++++++++
ddf19c
 tools/virtiofsd/passthrough_ll.c | 22 ++++++++--------------
ddf19c
 3 files changed, 32 insertions(+), 14 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.h b/tools/virtiofsd/fuse_lowlevel.h
ddf19c
index 8f6d705..562fd52 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.h
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.h
ddf19c
@@ -1777,6 +1777,7 @@ struct fuse_cmdline_opts {
ddf19c
     int syslog;
ddf19c
     int log_level;
ddf19c
     unsigned int max_idle_threads;
ddf19c
+    unsigned long rlimit_nofile;
ddf19c
 };
ddf19c
 
ddf19c
 /**
ddf19c
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
ddf19c
index 0801cf7..9b3eddc 100644
ddf19c
--- a/tools/virtiofsd/helper.c
ddf19c
+++ b/tools/virtiofsd/helper.c
ddf19c
@@ -23,6 +23,8 @@
ddf19c
 #include <stdlib.h>
ddf19c
 #include <string.h>
ddf19c
 #include <sys/param.h>
ddf19c
+#include <sys/time.h>
ddf19c
+#include <sys/resource.h>
ddf19c
 #include <unistd.h>
ddf19c
 
ddf19c
 #define FUSE_HELPER_OPT(t, p)                       \
ddf19c
@@ -53,6 +55,7 @@ static const struct fuse_opt fuse_helper_opts[] = {
ddf19c
     FUSE_HELPER_OPT("subtype=", nodefault_subtype),
ddf19c
     FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
ddf19c
     FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
ddf19c
+    FUSE_HELPER_OPT("--rlimit-nofile=%lu", rlimit_nofile),
ddf19c
     FUSE_HELPER_OPT("--syslog", syslog),
ddf19c
     FUSE_HELPER_OPT_VALUE("log_level=debug", log_level, FUSE_LOG_DEBUG),
ddf19c
     FUSE_HELPER_OPT_VALUE("log_level=info", log_level, FUSE_LOG_INFO),
ddf19c
@@ -171,6 +174,9 @@ void fuse_cmdline_help(void)
ddf19c
            "                               default: no_writeback\n"
ddf19c
            "    -o xattr|no_xattr          enable/disable xattr\n"
ddf19c
            "                               default: no_xattr\n"
ddf19c
+           "    --rlimit-nofile=<num>      set maximum number of file descriptors\n"
ddf19c
+           "                               (0 leaves rlimit unchanged)\n"
ddf19c
+           "                               default: 1,000,000 if the current rlimit is lower\n"
ddf19c
            );
ddf19c
 }
ddf19c
 
ddf19c
@@ -191,11 +197,28 @@ static int fuse_helper_opt_proc(void *data, const char *arg, int key,
ddf19c
     }
ddf19c
 }
ddf19c
 
ddf19c
+static unsigned long get_default_rlimit_nofile(void)
ddf19c
+{
ddf19c
+    rlim_t max_fds = 1000000; /* our default RLIMIT_NOFILE target */
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 0; /* we have more fds available than required! */
ddf19c
+    }
ddf19c
+    return max_fds;
ddf19c
+}
ddf19c
+
ddf19c
 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
ddf19c
 {
ddf19c
     memset(opts, 0, sizeof(struct fuse_cmdline_opts));
ddf19c
 
ddf19c
     opts->max_idle_threads = 10;
ddf19c
+    opts->rlimit_nofile = get_default_rlimit_nofile();
ddf19c
     opts->foreground = 1;
ddf19c
 
ddf19c
     if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc) ==
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index 50ff672..184ad0f 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -2711,24 +2711,18 @@ static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
ddf19c
     setup_seccomp(enable_syslog);
ddf19c
 }
ddf19c
 
ddf19c
-/* Raise the maximum number of open file descriptors */
ddf19c
-static void setup_nofile_rlimit(void)
ddf19c
+/* Set the maximum number of open file descriptors */
ddf19c
+static void setup_nofile_rlimit(unsigned long rlimit_nofile)
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
+    struct rlimit rlim = {
ddf19c
+        .rlim_cur = rlimit_nofile,
ddf19c
+        .rlim_max = rlimit_nofile,
ddf19c
+    };
ddf19c
 
ddf19c
-    if (rlim.rlim_cur >= max_fds) {
ddf19c
+    if (rlimit_nofile == 0) {
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
@@ -2981,7 +2975,7 @@ int main(int argc, char *argv[])
ddf19c
 
ddf19c
     fuse_daemonize(opts.foreground);
ddf19c
 
ddf19c
-    setup_nofile_rlimit();
ddf19c
+    setup_nofile_rlimit(opts.rlimit_nofile);
ddf19c
 
ddf19c
     /* Must be before sandbox since it wants /proc */
ddf19c
     setup_capng();
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c