2009-05-22 05:02:01 +08:00
|
|
|
/*
|
|
|
|
* fs/inotify_user.c - inotify support for userspace
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* John McCutchan <ttb@tentacle.dhs.org>
|
|
|
|
* Robert Love <rml@novell.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 John McCutchan
|
|
|
|
* Copyright 2006 Hewlett-Packard Development Company, L.P.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Eric Paris <Red Hat Inc>
|
|
|
|
* inotify was largely rewriten to make use of the fsnotify infrastructure
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2010-07-28 22:18:37 +08:00
|
|
|
#include <linux/dcache.h> /* d_unlinked */
|
2009-05-22 05:02:01 +08:00
|
|
|
#include <linux/fs.h> /* struct inode */
|
|
|
|
#include <linux/fsnotify_backend.h>
|
|
|
|
#include <linux/inotify.h>
|
|
|
|
#include <linux/path.h> /* struct path */
|
|
|
|
#include <linux/slab.h> /* kmem_* */
|
|
|
|
#include <linux/types.h>
|
2010-05-13 06:34:07 +08:00
|
|
|
#include <linux/sched.h>
|
2017-02-03 00:54:15 +08:00
|
|
|
#include <linux/sched/user.h>
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
#include <linux/sched/mm.h>
|
2009-05-22 05:02:01 +08:00
|
|
|
|
|
|
|
#include "inotify.h"
|
|
|
|
|
2009-12-18 10:24:21 +08:00
|
|
|
/*
|
2014-01-22 07:48:14 +08:00
|
|
|
* Check if 2 events contain the same information.
|
2009-12-18 10:24:21 +08:00
|
|
|
*/
|
2014-01-22 07:48:14 +08:00
|
|
|
static bool event_compare(struct fsnotify_event *old_fsn,
|
|
|
|
struct fsnotify_event *new_fsn)
|
2009-12-18 10:24:21 +08:00
|
|
|
{
|
2014-01-22 07:48:14 +08:00
|
|
|
struct inotify_event_info *old, *new;
|
|
|
|
|
|
|
|
old = INOTIFY_E(old_fsn);
|
|
|
|
new = INOTIFY_E(new_fsn);
|
2019-01-11 01:04:31 +08:00
|
|
|
if (old->mask & FS_IN_IGNORED)
|
|
|
|
return false;
|
|
|
|
if ((old->mask == new->mask) &&
|
2014-01-22 07:48:14 +08:00
|
|
|
(old_fsn->inode == new_fsn->inode) &&
|
|
|
|
(old->name_len == new->name_len) &&
|
|
|
|
(!old->name_len || !strcmp(old->name, new->name)))
|
|
|
|
return true;
|
2009-12-18 10:24:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:53:22 +08:00
|
|
|
static int inotify_merge(struct list_head *list,
|
|
|
|
struct fsnotify_event *event)
|
2009-12-18 10:24:21 +08:00
|
|
|
{
|
|
|
|
struct fsnotify_event *last_event;
|
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
last_event = list_entry(list->prev, struct fsnotify_event, list);
|
2014-01-29 01:53:22 +08:00
|
|
|
return event_compare(last_event, event);
|
2009-12-18 10:24:21 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
int inotify_handle_event(struct fsnotify_group *group,
|
|
|
|
struct inode *inode,
|
2016-11-21 09:19:09 +08:00
|
|
|
u32 mask, const void *data, int data_type,
|
2019-04-27 01:51:03 +08:00
|
|
|
const struct qstr *file_name, u32 cookie,
|
2016-11-11 00:51:50 +08:00
|
|
|
struct fsnotify_iter_info *iter_info)
|
2009-05-22 05:02:01 +08:00
|
|
|
{
|
2018-04-21 07:10:50 +08:00
|
|
|
struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
|
2009-12-18 10:24:24 +08:00
|
|
|
struct inotify_inode_mark *i_mark;
|
2014-01-22 07:48:14 +08:00
|
|
|
struct inotify_event_info *event;
|
|
|
|
struct fsnotify_event *fsn_event;
|
2014-01-29 01:53:22 +08:00
|
|
|
int ret;
|
2014-01-22 07:48:14 +08:00
|
|
|
int len = 0;
|
|
|
|
int alloc_len = sizeof(struct inotify_event_info);
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2018-04-21 07:10:50 +08:00
|
|
|
if (WARN_ON(fsnotify_iter_vfsmount_mark(iter_info)))
|
|
|
|
return 0;
|
2010-07-28 22:18:39 +08:00
|
|
|
|
2014-01-22 07:48:15 +08:00
|
|
|
if ((inode_mark->mask & FS_EXCL_UNLINK) &&
|
|
|
|
(data_type == FSNOTIFY_EVENT_PATH)) {
|
2016-11-21 09:19:09 +08:00
|
|
|
const struct path *path = data;
|
2014-01-22 07:48:15 +08:00
|
|
|
|
|
|
|
if (d_unlinked(path->dentry))
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-22 07:48:14 +08:00
|
|
|
if (file_name) {
|
2019-04-27 01:55:21 +08:00
|
|
|
len = file_name->len;
|
2014-01-22 07:48:14 +08:00
|
|
|
alloc_len += len + 1;
|
|
|
|
}
|
2010-07-28 22:18:37 +08:00
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
|
|
|
|
mask);
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2010-07-28 22:18:39 +08:00
|
|
|
i_mark = container_of(inode_mark, struct inotify_inode_mark,
|
2009-12-18 10:24:24 +08:00
|
|
|
fsn_mark);
|
2009-05-22 05:02:01 +08:00
|
|
|
|
fs: fsnotify: account fsnotify metadata to kmemcg
Patch series "Directed kmem charging", v8.
The Linux kernel's memory cgroup allows limiting the memory usage of the
jobs running on the system to provide isolation between the jobs. All
the kernel memory allocated in the context of the job and marked with
__GFP_ACCOUNT will also be included in the memory usage and be limited
by the job's limit.
The kernel memory can only be charged to the memcg of the process in
whose context kernel memory was allocated. However there are cases
where the allocated kernel memory should be charged to the memcg
different from the current processes's memcg. This patch series
contains two such concrete use-cases i.e. fsnotify and buffer_head.
The fsnotify event objects can consume a lot of system memory for large
or unlimited queues if there is either no or slow listener. The events
are allocated in the context of the event producer. However they should
be charged to the event consumer. Similarly the buffer_head objects can
be allocated in a memcg different from the memcg of the page for which
buffer_head objects are being allocated.
To solve this issue, this patch series introduces mechanism to charge
kernel memory to a given memcg. In case of fsnotify events, the memcg
of the consumer can be used for charging and for buffer_head, the memcg
of the page can be charged. For directed charging, the caller can use
the scope API memalloc_[un]use_memcg() to specify the memcg to charge
for all the __GFP_ACCOUNT allocations within the scope.
This patch (of 2):
A lot of memory can be consumed by the events generated for the huge or
unlimited queues if there is either no or slow listener. This can cause
system level memory pressure or OOMs. So, it's better to account the
fsnotify kmem caches to the memcg of the listener.
However the listener can be in a different memcg than the memcg of the
producer and these allocations happen in the context of the event
producer. This patch introduces remote memcg charging API which the
producer can use to charge the allocations to the memcg of the listener.
There are seven fsnotify kmem caches and among them allocations from
dnotify_struct_cache, dnotify_mark_cache, fanotify_mark_cache and
inotify_inode_mark_cachep happens in the context of syscall from the
listener. So, SLAB_ACCOUNT is enough for these caches.
The objects from fsnotify_mark_connector_cachep are not accounted as
they are small compared to the notification mark or events and it is
unclear whom to account connector to since it is shared by all events
attached to the inode.
The allocations from the event caches happen in the context of the event
producer. For such caches we will need to remote charge the allocations
to the listener's memcg. Thus we save the memcg reference in the
fsnotify_group structure of the listener.
This patch has also moved the members of fsnotify_group to keep the size
same, at least for 64 bit build, even with additional member by filling
the holes.
[shakeelb@google.com: use GFP_KERNEL_ACCOUNT rather than open-coding it]
Link: http://lkml.kernel.org/r/20180702215439.211597-1-shakeelb@google.com
Link: http://lkml.kernel.org/r/20180627191250.209150-2-shakeelb@google.com
Signed-off-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-08-18 06:46:39 +08:00
|
|
|
/* Whoever is interested in the event, pays for the allocation. */
|
|
|
|
memalloc_use_memcg(group->memcg);
|
|
|
|
event = kmalloc(alloc_len, GFP_KERNEL_ACCOUNT);
|
|
|
|
memalloc_unuse_memcg();
|
|
|
|
|
2018-02-21 22:07:52 +08:00
|
|
|
if (unlikely(!event)) {
|
|
|
|
/*
|
|
|
|
* Treat lost event due to ENOMEM the same way as queue
|
|
|
|
* overflow to let userspace know event was lost.
|
|
|
|
*/
|
|
|
|
fsnotify_queue_overflow(group);
|
2009-05-22 05:02:01 +08:00
|
|
|
return -ENOMEM;
|
2018-02-21 22:07:52 +08:00
|
|
|
}
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2019-01-11 01:04:40 +08:00
|
|
|
/*
|
|
|
|
* We now report FS_ISDIR flag with MOVE_SELF and DELETE_SELF events
|
|
|
|
* for fanotify. inotify never reported IN_ISDIR with those events.
|
|
|
|
* It looks like an oversight, but to avoid the risk of breaking
|
|
|
|
* existing inotify programs, mask the flag out from those events.
|
|
|
|
*/
|
|
|
|
if (mask & (IN_MOVE_SELF | IN_DELETE_SELF))
|
|
|
|
mask &= ~IN_ISDIR;
|
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
fsn_event = &event->fse;
|
2019-01-11 01:04:31 +08:00
|
|
|
fsnotify_init_event(fsn_event, inode);
|
|
|
|
event->mask = mask;
|
2014-01-22 07:48:14 +08:00
|
|
|
event->wd = i_mark->wd;
|
2014-02-17 20:09:50 +08:00
|
|
|
event->sync_cookie = cookie;
|
2014-01-22 07:48:14 +08:00
|
|
|
event->name_len = len;
|
|
|
|
if (len)
|
2019-04-27 01:51:03 +08:00
|
|
|
strcpy(event->name, file_name->name);
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2014-08-07 07:03:26 +08:00
|
|
|
ret = fsnotify_add_event(group, fsn_event, inotify_merge);
|
2014-01-29 01:53:22 +08:00
|
|
|
if (ret) {
|
2014-01-22 07:48:14 +08:00
|
|
|
/* Our event wasn't used in the end. Free it. */
|
|
|
|
fsnotify_destroy_event(group, fsn_event);
|
2009-08-17 09:51:44 +08:00
|
|
|
}
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2010-07-28 22:18:39 +08:00
|
|
|
if (inode_mark->mask & IN_ONESHOT)
|
2011-06-14 23:29:51 +08:00
|
|
|
fsnotify_destroy_mark(inode_mark, group);
|
2009-05-22 05:02:01 +08:00
|
|
|
|
2014-01-29 01:53:22 +08:00
|
|
|
return 0;
|
2009-05-22 05:02:01 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:24 +08:00
|
|
|
static void inotify_freeing_mark(struct fsnotify_mark *fsn_mark, struct fsnotify_group *group)
|
2009-05-22 05:02:01 +08:00
|
|
|
{
|
2009-12-18 10:24:24 +08:00
|
|
|
inotify_ignored_and_remove_idr(fsn_mark, group);
|
2009-05-22 05:02:01 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 04:03:35 +08:00
|
|
|
/*
|
|
|
|
* This is NEVER supposed to be called. Inotify marks should either have been
|
|
|
|
* removed from the idr when the watch was removed or in the
|
|
|
|
* fsnotify_destroy_mark_by_group() call when the inotify instance was being
|
|
|
|
* torn down. This is only called if the idr is about to be freed but there
|
|
|
|
* are still marks in it.
|
|
|
|
*/
|
2009-05-22 05:02:01 +08:00
|
|
|
static int idr_callback(int id, void *p, void *data)
|
|
|
|
{
|
2009-12-18 10:24:24 +08:00
|
|
|
struct fsnotify_mark *fsn_mark;
|
|
|
|
struct inotify_inode_mark *i_mark;
|
2009-08-25 04:03:35 +08:00
|
|
|
static bool warned = false;
|
|
|
|
|
|
|
|
if (warned)
|
|
|
|
return 0;
|
|
|
|
|
2010-01-16 01:12:25 +08:00
|
|
|
warned = true;
|
2009-12-18 10:24:24 +08:00
|
|
|
fsn_mark = p;
|
|
|
|
i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
|
2009-08-25 04:03:35 +08:00
|
|
|
|
2009-12-18 10:24:24 +08:00
|
|
|
WARN(1, "inotify closing but id=%d for fsn_mark=%p in group=%p still in "
|
2009-08-25 04:03:35 +08:00
|
|
|
"idr. Probably leaking memory\n", id, p, data);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I'm taking the liberty of assuming that the mark in question is a
|
|
|
|
* valid address and I'm dereferencing it. This might help to figure
|
|
|
|
* out why we got here and the panic is no worse than the original
|
|
|
|
* BUG() that was here.
|
|
|
|
*/
|
2009-12-18 10:24:24 +08:00
|
|
|
if (fsn_mark)
|
2016-12-09 16:38:55 +08:00
|
|
|
printk(KERN_WARNING "fsn_mark->group=%p wd=%d\n",
|
|
|
|
fsn_mark->group, i_mark->wd);
|
2009-05-22 05:02:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inotify_free_group_priv(struct fsnotify_group *group)
|
|
|
|
{
|
2011-03-31 09:57:33 +08:00
|
|
|
/* ideally the idr is empty and we won't hit the BUG in the callback */
|
2009-08-25 04:03:35 +08:00
|
|
|
idr_for_each(&group->inotify_data.idr, idr_callback, group);
|
2009-05-22 05:02:01 +08:00
|
|
|
idr_destroy(&group->inotify_data.idr);
|
2016-12-14 21:56:33 +08:00
|
|
|
if (group->inotify_data.ucounts)
|
|
|
|
dec_inotify_instances(group->inotify_data.ucounts);
|
2009-05-22 05:02:01 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
static void inotify_free_event(struct fsnotify_event *fsn_event)
|
2009-05-22 05:02:01 +08:00
|
|
|
{
|
2014-01-22 07:48:14 +08:00
|
|
|
kfree(INOTIFY_E(fsn_event));
|
2009-05-22 05:02:01 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 01:06:12 +08:00
|
|
|
/* ding dong the mark is dead */
|
|
|
|
static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
|
|
|
|
{
|
|
|
|
struct inotify_inode_mark *i_mark;
|
|
|
|
|
|
|
|
i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
|
|
|
|
|
|
|
|
kmem_cache_free(inotify_inode_mark_cachep, i_mark);
|
|
|
|
}
|
|
|
|
|
2009-05-22 05:02:01 +08:00
|
|
|
const struct fsnotify_ops inotify_fsnotify_ops = {
|
|
|
|
.handle_event = inotify_handle_event,
|
|
|
|
.free_group_priv = inotify_free_group_priv,
|
2014-01-22 07:48:14 +08:00
|
|
|
.free_event = inotify_free_event,
|
2009-05-22 05:02:01 +08:00
|
|
|
.freeing_mark = inotify_freeing_mark,
|
2016-12-22 01:06:12 +08:00
|
|
|
.free_mark = inotify_free_mark,
|
2009-05-22 05:02:01 +08:00
|
|
|
};
|