mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xi Wang <xi.wang@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Eric Dumazet <eric.dumazet@gmail.com>, Xi Wang <xi.wang@gmail.com>
Subject: [PATCH] fs: limit file-max
Date: Sat, 25 Aug 2012 12:42:40 -0400	[thread overview]
Message-ID: <1345912960-6419-1-git-send-email-xi.wang@gmail.com> (raw)

Setting /proc/sys/fs/file-max larger than 2^63 on a 64-bit system is
wrong since the kernel cannot allocate that many structures (similarly
2^31 for a 32-bit system).

This will also cause an integer overflow in the check in unix_create1():

	atomic_long_read(&unix_nr_socks) > 2 * get_max_files()

which prevents the kernel from creating a unix domain socket when the
sysadmin sets a large file-max.

Suggested-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
What's the best min value for file-max?  This patch uses NR_FILE.
Zero seems weird.  BITS_PER_LONG as sysctl_nr_open_min?
---
 fs/file_table.c |    8 ++++++--
 kernel/sysctl.c |    3 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/file_table.c b/fs/file_table.c
index 701985e..9b65e59 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -36,6 +36,9 @@ struct files_stat_struct files_stat = {
 	.max_files = NR_FILE
 };
 
+unsigned long sysctl_file_max_min = NR_FILE;
+unsigned long sysctl_file_max_max = NR_FILE; /* raised later */
+
 DEFINE_LGLOCK(files_lglock);
 
 /* SLAB cache for file structures */
@@ -582,8 +585,9 @@ void __init files_init(unsigned long mempages)
 	 * Per default don't use more than 10% of our memory for files. 
 	 */ 
 
-	n = (mempages * (PAGE_SIZE / 1024)) / 10;
-	files_stat.max_files = max_t(unsigned long, n, NR_FILE);
+	n = mempages * (PAGE_SIZE / 1024);
+	files_stat.max_files = max_t(unsigned long, n / 10, NR_FILE);
+	sysctl_file_max_max = max_t(unsigned long, n, NR_FILE);
 	files_defer_init();
 	lg_lock_init(&files_lglock, "files_lglock");
 	percpu_counter_init(&nr_files, 0);
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 87174ef..a21e22a 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -108,6 +108,7 @@ extern int sysctl_drop_caches;
 extern int percpu_pagelist_fraction;
 extern int compat_log;
 extern int latencytop_enabled;
+extern int sysctl_file_max_min, sysctl_file_max_max;
 extern int sysctl_nr_open_min, sysctl_nr_open_max;
 #ifndef CONFIG_MMU
 extern int sysctl_nr_trim_pages;
@@ -1403,6 +1404,8 @@ static struct ctl_table fs_table[] = {
 		.maxlen		= sizeof(files_stat.max_files),
 		.mode		= 0644,
 		.proc_handler	= proc_doulongvec_minmax,
+		.extra1		= &sysctl_file_max_min,
+		.extra2		= &sysctl_file_max_max,
 	},
 	{
 		.procname	= "nr_open",
-- 
1.7.9.5


                 reply	other threads:[~2012-08-25 16:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1345912960-6419-1-git-send-email-xi.wang@gmail.com \
    --to=xi.wang@gmail.com \
    --cc=eric.dumazet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome