From: Amir Goldstein <amir73il@gmail.com>
To: Jan Kara <jack@suse.cz>
Cc: Christian Brauner <brauner@kernel.org>, linux-fsdevel@vger.kernel.org
Subject: [PATCH v2 06/10] fanotify: gate fs event classification by group type
Date: Fri, 24 Apr 2026 19:04:59 +0200 [thread overview]
Message-ID: <20260424170503.2096847-7-amir73il@gmail.com> (raw)
In-Reply-To: <20260424170503.2096847-1-amir73il@gmail.com>
fanotify decides on how to allocate events based on the event mask.
The event mask bits that correspond to filesystem watchers are only
meaningful in the context of filesystem group type.
Hence, before checking if event is a specific fs event (e.g. FS_RENAME)
need to first check the group type is filesystem.
Separate fanotify_alloc_event() into an allocator per group type, so
that all the fs events check inside fanotify_alloc_fs_watcher_event()
are already properly gated.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
fs/notify/fanotify/fanotify.c | 73 ++++++++++++++++++++++++++++-------
1 file changed, 58 insertions(+), 15 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 80116026d3ae0..987092c38789b 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -744,11 +744,12 @@ static struct fanotify_event *fanotify_alloc_error_event(
return &fee->fae;
}
-static struct fanotify_event *fanotify_alloc_event(
+static struct fanotify_event *fanotify_alloc_fs_watcher_event(
struct fsnotify_group *group,
u32 mask, const void *data, int data_type,
struct inode *dir, const struct qstr *file_name,
- __kernel_fsid_t *fsid, u32 match_mask)
+ __kernel_fsid_t *fsid, u32 match_mask,
+ unsigned int *hash)
{
struct fanotify_event *event = NULL;
gfp_t gfp = GFP_KERNEL_ACCOUNT;
@@ -759,14 +760,11 @@ static struct fanotify_event *fanotify_alloc_event(
const struct path *path = fsnotify_data_path(data, data_type);
struct fs_error_report *fs_error =
fsnotify_data_error_report(data, data_type);
- u64 mnt_id = fsnotify_data_mnt_id(data, data_type);
struct mem_cgroup *old_memcg;
struct dentry *moved = NULL;
struct inode *child = NULL;
bool name_event = false;
- unsigned int hash = 0;
bool ondir = mask & FAN_ONDIR;
- struct pid *pid;
if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
/*
@@ -848,22 +846,69 @@ static struct fanotify_event *fanotify_alloc_event(
if (fanotify_is_fs_perm_event(group, mask)) {
event = fanotify_alloc_perm_event(data, data_type, gfp);
} else if (fs_error) {
- event = fanotify_alloc_error_event(group, fsid, fs_error, &hash);
+ event = fanotify_alloc_error_event(group, fsid, fs_error, hash);
} else if (name_event && (file_name || moved || child)) {
event = fanotify_alloc_name_event(dirid, fsid, file_name, child,
- moved, &hash, gfp);
+ moved, hash, gfp);
} else if (fid_mode) {
- event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
+ event = fanotify_alloc_fid_event(id, fsid, hash, gfp);
} else if (path) {
- event = fanotify_alloc_path_event(path, &hash, gfp);
- } else if (mnt_id) {
+ event = fanotify_alloc_path_event(path, hash, gfp);
+ } else {
+ WARN_ON_ONCE(1);
+ }
+
+ set_active_memcg(old_memcg);
+ return event;
+}
+
+static struct fanotify_event *fanotify_alloc_ns_watcher_event(
+ struct fsnotify_group *group, u64 mask,
+ const void *data, int data_type)
+{
+ u64 mnt_id = fsnotify_data_mnt_id(data, data_type);
+ struct mem_cgroup *old_memcg;
+ struct fanotify_event *event = NULL;
+ gfp_t gfp = GFP_KERNEL_ACCOUNT;
+
+ if (group->max_events == UINT_MAX)
+ gfp |= __GFP_NOFAIL;
+ else
+ gfp |= __GFP_RETRY_MAYFAIL;
+
+ old_memcg = set_active_memcg(group->memcg);
+
+ if (mnt_id) {
event = fanotify_alloc_mnt_event(mnt_id, gfp);
} else {
WARN_ON_ONCE(1);
}
+ set_active_memcg(old_memcg);
+ return event;
+}
+
+static struct fanotify_event *fanotify_alloc_event(
+ struct fsnotify_group *group, u64 mask,
+ const void *data, int data_type,
+ struct inode *dir, const struct qstr *name,
+ __kernel_fsid_t *fsid, u32 match_mask)
+{
+ struct fanotify_event *event;
+ unsigned int hash = 0;
+ bool ondir = mask & FAN_ONDIR;
+ struct pid *pid;
+
+ if (fsnotify_is_ns_watcher(group)) {
+ event = fanotify_alloc_ns_watcher_event(group, mask, data,
+ data_type);
+ } else {
+ event = fanotify_alloc_fs_watcher_event(group, mask, data,
+ data_type, dir, name, fsid,
+ match_mask, &hash);
+ }
if (!event)
- goto out;
+ return NULL;
if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
pid = get_pid(task_pid(current));
@@ -875,8 +920,6 @@ static struct fanotify_event *fanotify_alloc_event(
fanotify_init_event(event, hash, mask);
event->pid = pid;
-out:
- set_active_memcg(old_memcg);
return event;
}
@@ -966,8 +1009,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
if (!mask)
return 0;
- pr_debug("%s: group=%p mask=%x report_mask=%x\n", __func__,
- group, mask, match_mask);
+ pr_debug("%s: group=%p type=%d mask=%x report_mask=%x\n", __func__,
+ group, group->type, mask, match_mask);
bool is_perm = fanotify_is_fs_perm_event(group, mask);
if (is_perm) {
--
2.54.0
next prev parent reply other threads:[~2026-04-24 17:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-24 17:04 [PATCH v2 00/10] fanotify namespace monitoring Amir Goldstein
2026-04-24 17:04 ` [PATCH v2 01/10] fsnotify: rename fsnotify group flag macros Amir Goldstein
2026-04-24 17:04 ` [PATCH v2 02/10] fsnotify: introduce fsnotify group types Amir Goldstein
2026-04-24 17:04 ` [PATCH v2 03/10] fsnotify: separate the events bitmask macros by group type Amir Goldstein
2026-04-24 17:04 ` [PATCH v2 04/10] fanotify: test event->type instead of event mask when possible Amir Goldstein
2026-04-24 17:04 ` [PATCH v2 05/10] fsnotify: do not report mount events with fsnotify() Amir Goldstein
2026-04-24 17:04 ` Amir Goldstein [this message]
2026-04-24 17:05 ` [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type Amir Goldstein
2026-04-24 17:05 ` [PATCH v2 08/10] fanotify: add support for watching the namespaces tree Amir Goldstein
2026-04-24 17:05 ` [PATCH v2 09/10] selftests/filesystems: create fanotify test dir Amir Goldstein
2026-04-24 17:05 ` [PATCH v2 10/10] selftests/filesystems: add fanotify namespace notifications test Amir Goldstein
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=20260424170503.2096847-7-amir73il@gmail.com \
--to=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@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