Skip to content

Commit 9289012

Browse files
Waiman-Longjankara
authored andcommitted
inotify: Increase default inotify.max_user_watches limit to 1048576
The default value of inotify.max_user_watches sysctl parameter was set to 8192 since the introduction of the inotify feature in 2005 by commit 0eeca28 ("[PATCH] inotify"). Today this value is just too small for many modern usage. As a result, users have to explicitly set it to a larger value to make it work. After some searching around the web, these are the inotify.max_user_watches values used by some projects: - vscode: 524288 - dropbox support: 100000 - users on stackexchange: 12228 - lsyncd user: 2000000 - code42 support: 1048576 - monodevelop: 16384 - tectonic: 524288 - openshift origin: 65536 Each watch point adds an inotify_inode_mark structure to an inode to be watched. It also pins the watched inode. Modeled after the epoll.max_user_watches behavior to adjust the default value according to the amount of addressable memory available, make inotify.max_user_watches behave in a similar way to make it use no more than 1% of addressable memory within the range [8192, 1048576]. We estimate the amount of memory used by inotify mark to size of inotify_inode_mark plus two times the size of struct inode (we double the inode size to cover the additional filesystem private inode part). That means that a 64-bit system with 128GB or more memory will likely have the maximum value of 1048576 for inotify.max_user_watches. This default should be big enough for most use cases. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Amir Goldstein <[email protected]> Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent 7372e79 commit 9289012

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

fs/notify/inotify/inotify_user.c

+22-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737

3838
#include <asm/ioctls.h>
3939

40+
/*
41+
* An inotify watch requires allocating an inotify_inode_mark structure as
42+
* well as pinning the watched inode. Doubling the size of a VFS inode
43+
* should be more than enough to cover the additional filesystem inode
44+
* size increase.
45+
*/
46+
#define INOTIFY_WATCH_COST (sizeof(struct inotify_inode_mark) + \
47+
2 * sizeof(struct inode))
48+
4049
/* configurable via /proc/sys/fs/inotify/ */
4150
static int inotify_max_queued_events __read_mostly;
4251

@@ -801,6 +810,18 @@ SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
801810
*/
802811
static int __init inotify_user_setup(void)
803812
{
813+
unsigned long watches_max;
814+
struct sysinfo si;
815+
816+
si_meminfo(&si);
817+
/*
818+
* Allow up to 1% of addressable memory to be allocated for inotify
819+
* watches (per user) limited to the range [8192, 1048576].
820+
*/
821+
watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
822+
INOTIFY_WATCH_COST;
823+
watches_max = clamp(watches_max, 8192UL, 1048576UL);
824+
804825
BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
805826
BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
806827
BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
@@ -827,7 +848,7 @@ static int __init inotify_user_setup(void)
827848

828849
inotify_max_queued_events = 16384;
829850
init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
830-
init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
851+
init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
831852

832853
return 0;
833854
}

0 commit comments

Comments
 (0)