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