]> git.baikalelectronics.ru Git - kernel.git/commitdiff
inotify, memcg: account inotify instances to kmemcg
authorShakeel Butt <shakeelb@google.com>
Sun, 20 Dec 2020 04:46:08 +0000 (20:46 -0800)
committerJan Kara <jack@suse.cz>
Tue, 5 Jan 2021 13:42:54 +0000 (14:42 +0100)
Currently the fs sysctl inotify/max_user_instances is used to limit the
number of inotify instances on the system. For systems running multiple
workloads, the per-user namespace sysctl max_inotify_instances can be
used to further partition inotify instances. However there is no easy
way to set a sensible system level max limit on inotify instances and
further partition it between the workloads. It is much easier to charge
the underlying resource (i.e. memory) behind the inotify instances to
the memcg of the workload and let their memory limits limit the number
of inotify instances they can create.

With inotify instances charged to memcg, the admin can simply set
max_user_instances to INT_MAX and let the memcg limits of the jobs limit
their inotify instances.

Link: https://lore.kernel.org/r/20201220044608.1258123-1-shakeelb@google.com
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Signed-off-by: Jan Kara <jack@suse.cz>
fs/notify/fanotify/fanotify_user.c
fs/notify/group.c
fs/notify/inotify/inotify_user.c
include/linux/fsnotify_backend.h

index 3e01d8f2ab9061faa43f865e1f9f3dc2868e5dbc..7e7afc2b62e19673e6ad48188e4049ee29dbae4e 100644 (file)
@@ -976,7 +976,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
                f_flags |= O_NONBLOCK;
 
        /* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
-       group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
+       group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
        if (IS_ERR(group)) {
                free_uid(user);
                return PTR_ERR(group);
index a4a4b1c64d32a6dea443209c2e61c472d383ec45..ffd723ffe46de31b99f089d284a925b22a978781 100644 (file)
@@ -111,14 +111,12 @@ void fsnotify_put_group(struct fsnotify_group *group)
 }
 EXPORT_SYMBOL_GPL(fsnotify_put_group);
 
-/*
- * Create a new fsnotify_group and hold a reference for the group returned.
- */
-struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
+static struct fsnotify_group *__fsnotify_alloc_group(
+                               const struct fsnotify_ops *ops, gfp_t gfp)
 {
        struct fsnotify_group *group;
 
-       group = kzalloc(sizeof(struct fsnotify_group), GFP_KERNEL);
+       group = kzalloc(sizeof(struct fsnotify_group), gfp);
        if (!group)
                return ERR_PTR(-ENOMEM);
 
@@ -139,8 +137,25 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
 
        return group;
 }
+
+/*
+ * Create a new fsnotify_group and hold a reference for the group returned.
+ */
+struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
+{
+       return __fsnotify_alloc_group(ops, GFP_KERNEL);
+}
 EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
 
+/*
+ * Create a new fsnotify_group and hold a reference for the group returned.
+ */
+struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops)
+{
+       return __fsnotify_alloc_group(ops, GFP_KERNEL_ACCOUNT);
+}
+EXPORT_SYMBOL_GPL(fsnotify_alloc_user_group);
+
 int fsnotify_fasync(int fd, struct file *file, int on)
 {
        struct fsnotify_group *group = file->private_data;
index 59c177011a0f9aca7d7560d13d270c948d3f788b..266d17e8ecb9824b25df1eba5410d70b6581d9a5 100644 (file)
@@ -632,11 +632,11 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)
        struct fsnotify_group *group;
        struct inotify_event_info *oevent;
 
-       group = fsnotify_alloc_group(&inotify_fsnotify_ops);
+       group = fsnotify_alloc_user_group(&inotify_fsnotify_ops);
        if (IS_ERR(group))
                return group;
 
-       oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
+       oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT);
        if (unlikely(!oevent)) {
                fsnotify_destroy_group(group);
                return ERR_PTR(-ENOMEM);
index a2e42d3cd87cfaf94bc2884cacc2fa740c3896e6..e5409b83e7313a4b49938a0c2de458693579dbf7 100644 (file)
@@ -470,6 +470,7 @@ static inline void fsnotify_update_flags(struct dentry *dentry)
 
 /* create a new group */
 extern struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops);
+extern struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops);
 /* get reference to a group */
 extern void fsnotify_get_group(struct fsnotify_group *group);
 /* drop reference on a group from fsnotify_alloc_group */