Skip to contentSkip to conversation

fanotify reports the thread id of the event trigger

PATCH In review nixiaoming 2 participants 7 messages Sep 17, 2018 – Sep 19, 2018

Subsystems (3)
lore
Share & tools
lore

nixiaoming

author
#0 lore raw

PATCH v2 1/1 fanotify reports the thread id of the event trigger

In order to identify which thread triggered the event in the multi-threaded program, add the FAN_EVENT_INFO_TID tag in fanotify_init to select whether to report the event creator's thread id information.

---
 fs/notify/fanotify/fanotify.c      | 5 ++++-
 fs/notify/fanotify/fanotify_user.c | 5 +++--
 include/linux/fsnotify_backend.h   | 1 +
 include/uapi/linux/fanotify.h      | 1 +
 4 files changed, 9 insertions(+), 3 deletions(-)
fs/notify/fanotify/fanotify.c +4 -1
index 94b5215..5a3af87 100644
@@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
 		goto out;
 init: __maybe_unused
 	fsnotify_init_event(&event->fse, inode, mask);
-	event->tgid = get_pid(task_tgid(current));
+	if (group->fanotify_data.should_report_tid)
+		event->tgid = get_pid(task_pid(current));
+	else
+		event->tgid = get_pid(task_tgid(current));
 	if (path) {
 		event->path = *path;
 		path_get(&event->path);
fs/notify/fanotify/fanotify_user.c +3 -2
index 6905488..3aa425b 100644
@@ -693,9 +693,9 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 		return -EPERM;
 
 #ifdef CONFIG_AUDITSYSCALL
-	if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
+	if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT | FAN_EVENT_INFO_TID))
 #else
-	if (flags & ~FAN_ALL_INIT_FLAGS)
+	if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_EVENT_INFO_TID))
 #endif
 		return -EINVAL;
 
@@ -731,6 +731,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 	}
 
 	group->fanotify_data.user = user;
+	group->fanotify_data.should_report_tid = (flags & FAN_EVENT_INFO_TID) ? true : false;
 	atomic_inc(&user->fanotify_listeners);
 	group->memcg = get_mem_cgroup_from_mm(current->mm);
 
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
 			unsigned int max_marks;
 			struct user_struct *user;
 			bool audit;
+			bool should_report_tid;
 		} fanotify_data;
 #endif /* CONFIG_FANOTIFY */
 	};
include/uapi/linux/fanotify.h +1 -0
index 7424791..4eba41c 100644
@@ -18,6 +18,7 @@
 
 #define FAN_ONDIR		0x40000000	/* event occurred against dir */
 
+#define FAN_EVENT_INFO_TID	0x02000000	/* reports the thread id of the event trigger */
 #define FAN_EVENT_ON_CHILD	0x08000000	/* interested in child events */
 
 /* helper events */
Signature · 1 line
2.10.1
#1 lore raw
Quoted 3 lines · nixiaoming

In order to identify which thread triggered the event in the multi-threaded program, add the FAN_EVENT_INFO_TID tag in fanotify_init

According to code and man page this is a 'flag' not a 'tag' Please stick to existing terminology.

Quoted 31 lines

to select whether to report the event creator's thread id information.

---
 fs/notify/fanotify/fanotify.c      | 5 ++++-
 fs/notify/fanotify/fanotify_user.c | 5 +++--
 include/linux/fsnotify_backend.h   | 1 +
 include/uapi/linux/fanotify.h      | 1 +
 4 files changed, 9 insertions(+), 3 deletions(-)
fs/notify/fanotify/fanotify.c +4 -1
index 94b5215..5a3af87 100644
@@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
                goto out;
 init: __maybe_unused
        fsnotify_init_event(&event->fse, inode, mask);
-       event->tgid = get_pid(task_tgid(current));
+       if (group->fanotify_data.should_report_tid)
+               event->tgid = get_pid(task_pid(current));
+       else
+               event->tgid = get_pid(task_tgid(current));
        if (path) {
                event->path = *path;
                path_get(&event->path);
fs/notify/fanotify/fanotify_user.c +0 -0
index 6905488..3aa425b 100644

fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 698

Quoted 8 lines
fs/notify/fanotify/fanotify_user.c +2 -2
@@ -693,9 +693,9 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
                return -EPERM;
 
 #ifdef CONFIG_AUDITSYSCALL
-       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
+       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT | FAN_EVENT_INFO_TID))
 #else
-       if (flags & ~FAN_ALL_INIT_FLAGS)
+       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_EVENT_INFO_TID))

Not this way. You need to add FAN_EVENT_INFO_TID to FAN_ALL_INIT_FLAGS.

fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 734

Quoted 8 lines
fs/notify/fanotify/fanotify_user.c +1 -0
 #endif
                return -EINVAL;
 
@@ -731,6 +731,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
        }
 
        group->fanotify_data.user = user;
+       group->fanotify_data.should_report_tid = (flags & FAN_EVENT_INFO_TID) ? true : false;

Please drop "? true : false" its not needed.

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +0 -0
        atomic_inc(&user->fanotify_listeners);
        group->memcg = get_mem_cgroup_from_mm(current->mm);
 
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

include/uapi/linux/fanotify.h line 21

Quoted 12 lines
include/linux/fsnotify_backend.h +0 -0
                } fanotify_data;
 #endif /* CONFIG_FANOTIFY */
        };
include/uapi/linux/fanotify.h +1 -0
index 7424791..4eba41c 100644
@@ -18,6 +18,7 @@
 
 #define FAN_ONDIR              0x40000000      /* event occurred against dir */
 
+#define FAN_EVENT_INFO_TID     0x02000000      /* reports the thread id of the event trigger */

That is not the right place nor the sensible value for this flag, but this:

#define FAN_UNLIMITED_QUEUE     0x00000010
#define FAN_UNLIMITED_MARKS     0x00000020
#define FAN_ENABLE_AUDIT        0x00000040
+#define FAN_EVENT_INFO_TID     0x00000080

Thanks,
Amir.

author
#2 lore raw
Quoted 8 lines · Amir Goldstein
Quoted 3 lines · nixiaoming

In order to identify which thread triggered the event in the multi-threaded program, add the FAN_EVENT_INFO_TID tag in fanotify_init

According to code and man page this is a 'flag' not a 'tag' Please stick to existing terminology.

Thank you for your guidance, I will correct it in a later patch.

Quoted 3 lines
Quoted 3 lines

to select whether to report the event creator's thread id information.

.......

Quoted 11 lines

fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 698

Quoted 9 lines
fs/notify/fanotify/fanotify_user.c +2 -2
@@ -693,9 +693,9 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
                return -EPERM;
 
 #ifdef CONFIG_AUDITSYSCALL
-       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
+       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT | FAN_EVENT_INFO_TID))
 #else
-       if (flags & ~FAN_ALL_INIT_FLAGS)
+       if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_EVENT_INFO_TID))

Not this way. You need to add FAN_EVENT_INFO_TID to FAN_ALL_INIT_FLAGS.

Thank you for your guidance, I will correct it in a later patch.

Quoted 15 lines

fs/notify/fanotify/fanotify_user.c SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) · line 734

Quoted 9 lines
fs/notify/fanotify/fanotify_user.c +1 -0
 #endif
                return -EINVAL;
 
@@ -731,6 +731,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
        }
 
        group->fanotify_data.user = user;
+       group->fanotify_data.should_report_tid = (flags & FAN_EVENT_INFO_TID) ? true : false;

Please drop "? true : false" its not needed.

Quoted 3 lines
fs/notify/fanotify/fanotify_user.c +0 -0
        atomic_inc(&user->fanotify_listeners);
        group->memcg = get_mem_cgroup_from_mm(current->mm);
 
Quoted 12 lines

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 9 lines
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

Whether it is better to change to "unsigned int flags"
Save "group->fanotify_data.flags=flags;" in "fanotify_init"
Determine whether "group->fanotify_data.flags" contains "FAN_EVENT_INFO_TID" in "fanotify_alloc_event";

At the same time, if there are other flags that need to be used later, there is no need to add new members.

By the way, whether or not "bool audit" can also be included by flags

thanks

Quoted 23 lines

include/uapi/linux/fanotify.h line 21

Quoted 12 lines
include/linux/fsnotify_backend.h +0 -0
                } fanotify_data;
 #endif /* CONFIG_FANOTIFY */
        };
include/uapi/linux/fanotify.h +1 -0
index 7424791..4eba41c 100644
@@ -18,6 +18,7 @@
 
 #define FAN_ONDIR              0x40000000      /* event occurred against dir */
 
+#define FAN_EVENT_INFO_TID     0x02000000      /* reports the thread id of the event trigger */

That is not the right place nor the sensible value for this flag, but this:

#define FAN_UNLIMITED_QUEUE     0x00000010
#define FAN_UNLIMITED_MARKS     0x00000020
#define FAN_ENABLE_AUDIT        0x00000040
+#define FAN_EVENT_INFO_TID     0x00000080

Thanks,
Amir.

#3 lore raw
Quoted 3 lines · Nixiaoming
Quoted 1 line · Amir Goldstein

On Mon, Sep 17, 2018 at 6:05 PM nixiaoming <[email protected]> wrote:

...

Quoted 13 lines
Quoted 12 lines

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 9 lines
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

Quoted 8 lines

Whether it is better to change to "unsigned int flags"
Save "group->fanotify_data.flags=flags;" in "fanotify_init"
Determine whether "group->fanotify_data.flags" contains "FAN_EVENT_INFO_TID" in "fanotify_alloc_event";

At the same time, if there are other flags that need to be used later, there is no need to add new members.

By the way, whether or not "bool audit" can also be included by flags

I strongly agree. Didn't want to impose this change on you, but in fact, I already did
that in my own patch set, so you can use my patches as reference:
https://github.com/amir73il/linux/commit/5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620
https://github.com/amir73il/linux/commit/0d1a226f5c18012f19ed6eabfab46f0f125ec190

If you do this you need to separate your change into 2 patches. First make the fanotify_data.flags change collecting the pieces from both my patches.
Then make your TID change using fanotify_data.flags.

Thanks,
Amir.

author
#4 lore raw

On Tue, Sep 18, 2018 3:07 PM Amir Goldstein <[email protected]>

Quoted 33 lines · Nixiaoming
Quoted 3 lines · Amir Goldstein
Quoted 1 line · nixiaoming

...

Quoted 13 lines
Quoted 12 lines

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 9 lines
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

Quoted 8 lines

Whether it is better to change to "unsigned int flags"
Save "group->fanotify_data.flags=flags;" in "fanotify_init"
Determine whether "group->fanotify_data.flags" contains "FAN_EVENT_INFO_TID" in "fanotify_alloc_event";

At the same time, if there are other flags that need to be used later, there is no need to add new members.

By the way, whether or not "bool audit" can also be included by flags

I strongly agree. Didn't want to impose this change on you, but in fact, I already did
that in my own patch set, so you can use my patches as reference:
https://github.com/amir73il/linux/commit/5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620
https://github.com/amir73il/linux/commit/0d1a226f5c18012f19ed6eabfab46f0f125ec190

Quoted 8 lines

If you do this you need to separate your change into 2 patches. First make the fanotify_data.flags change collecting the pieces from both my patches.
Then make your TID change using fanotify_data.flags.

Thanks,
Amir.

Should I do this:
1 git cherry-pick 5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620 Git cherry-pick 0d1a226f5c18012f19ed6eabfab46f0f125ec190 Manually handle patch conflicts
2 Add a new flag "FAN_EVENT_INFO_TID" into fanotify_data.flags 3 Incorporate "bool audit" into fanotify_data.flags 4 Submit 4 patches

thanks

#5 lore raw
Quoted 43 lines · Nixiaoming

On Tue, Sep 18, 2018 3:07 PM Amir Goldstein <[email protected]>

Quoted 33 lines · Nixiaoming
Quoted 3 lines · Amir Goldstein
Quoted 1 line · nixiaoming

...

Quoted 13 lines
Quoted 12 lines

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 9 lines
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

Quoted 8 lines

Whether it is better to change to "unsigned int flags"
Save "group->fanotify_data.flags=flags;" in "fanotify_init"
Determine whether "group->fanotify_data.flags" contains "FAN_EVENT_INFO_TID" in "fanotify_alloc_event";

At the same time, if there are other flags that need to be used later, there is no need to add new members.

By the way, whether or not "bool audit" can also be included by flags

I strongly agree. Didn't want to impose this change on you, but in fact, I already did
that in my own patch set, so you can use my patches as reference:
https://github.com/amir73il/linux/commit/5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620
https://github.com/amir73il/linux/commit/0d1a226f5c18012f19ed6eabfab46f0f125ec190

Quoted 8 lines

If you do this you need to separate your change into 2 patches. First make the fanotify_data.flags change collecting the pieces from both my patches.
Then make your TID change using fanotify_data.flags.

Thanks,
Amir.

Quoted 8 lines

Should I do this:
1 git cherry-pick 5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620

    Git cherry-pick 0d1a226f5c18012f19ed6eabfab46f0f125ec190

Manually handle patch conflicts
2 Add a new flag "FAN_EVENT_INFO_TID" into fanotify_data.flags 3 Incorporate "bool audit" into fanotify_data.flags 4 Submit 4 patches

No there is stuff in my patches that is irrelevant.
Let me prepare a clean patch for fanotify_data.flags and you can work on top of that.

I'll try to post it later today.

Thanks,
Amir.

#6 lore raw
Quoted 54 lines · Amir Goldstein
Quoted 43 lines · Nixiaoming

On Tue, Sep 18, 2018 3:07 PM Amir Goldstein <[email protected]>

Quoted 33 lines · Nixiaoming
Quoted 3 lines · Amir Goldstein
Quoted 1 line · nixiaoming

...

Quoted 13 lines
Quoted 12 lines

include/linux/fsnotify_backend.h struct fsnotify_group { · line 196

Quoted 9 lines
include/linux/fsnotify_backend.h +1 -0
index b8f4182..44c659f 100644
@@ -193,6 +193,7 @@ struct fsnotify_group {
                        unsigned int max_marks;
                        struct user_struct *user;
                        bool audit;
+                       bool should_report_tid;

For brevity I would call that report_tid, but not insisting.

Quoted 8 lines

Whether it is better to change to "unsigned int flags"
Save "group->fanotify_data.flags=flags;" in "fanotify_init"
Determine whether "group->fanotify_data.flags" contains "FAN_EVENT_INFO_TID" in "fanotify_alloc_event";

At the same time, if there are other flags that need to be used later, there is no need to add new members.

By the way, whether or not "bool audit" can also be included by flags

I strongly agree. Didn't want to impose this change on you, but in fact, I already did
that in my own patch set, so you can use my patches as reference:
https://github.com/amir73il/linux/commit/5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620
https://github.com/amir73il/linux/commit/0d1a226f5c18012f19ed6eabfab46f0f125ec190

Quoted 8 lines

If you do this you need to separate your change into 2 patches. First make the fanotify_data.flags change collecting the pieces from both my patches.
Then make your TID change using fanotify_data.flags.

Thanks,
Amir.

Quoted 8 lines

Should I do this:
1 git cherry-pick 5225fe1e19c74f4d7a4a4cc98ff6ef5872c8e620

    Git cherry-pick 0d1a226f5c18012f19ed6eabfab46f0f125ec190

Manually handle patch conflicts
2 Add a new flag "FAN_EVENT_INFO_TID" into fanotify_data.flags 3 Incorporate "bool audit" into fanotify_data.flags 4 Submit 4 patches

Quoted 4 lines

No there is stuff in my patches that is irrelevant.
Let me prepare a clean patch for fanotify_data.flags and you can work on top of that.

Nixiaoming,

I posted the prep patch earlier today. You can also find a slightly modified version of
the patch it on this branch with example for adding a new flag (FAN_UNPRIVILEGED):
https://github.com/amir73il/linux/commits/fanotify_unpriv

Note that my FAN_UNPRIVILEGED adds the flag at 0x80.
Not that it matters much, but it would be nice if you add your flag at 0x100 because I have more FAN_EVENT_INFO_ flags in the works, so its nice if we can group them all on byte 3 (at least for start).

For your next posting please don't forget to CC [email protected] and specify the branch your patch applies to and mention the required prep patch unless you re-send it as a series.

Thanks,
Amir.