Skip to contentSkip to conversation

Fixes for fanotify parent dir ignore mask logic

PATCH Superseded Amir Goldstein 2 participants 12 messages May 11, 2022 – May 13, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amir Goldstein

author reviewer
#0 lore raw

cover Fixes for fanotify parent dir ignore mask logic

Jan,

The following two patches are a prelude to FAN_MARK_IGNORE patch set [1]. I have written tests [2] and man page draft [3] for FAN_MARK_IGNORE, but not proposing it for next, because one big UAPI change is enough and it is too late in the cycle anyway.

However, I though you may want to consider these two patches for next. The test fanotify09 on [2] has two new test cases for the fixes in these patches.

Thanks,
Amir.

[1] https://github.com/amir73il/linux/commits/fan_mark_ignore
[2] https://github.com/amir73il/ltp/commits/fan_mark_ignore
[3] https://github.com/amir73il/man-pages/commits/fan_mark_ignore

Amir Goldstein (2):
fsnotify: introduce mark type iterator
fsnotify: consistent behavior for parent not watching children

 fs/notify/fanotify/fanotify.c    | 24 ++-------
 fs/notify/fsnotify.c             | 88 +++++++++++++++++---------------
 include/linux/fsnotify_backend.h | 17 +++---
 3 files changed, 61 insertions(+), 68 deletions(-)
Signature · 1 line
2.25.1
author reviewer
#1 lore raw

PATCH 1/2 fsnotify: introduce mark type iterator

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")
---
 fs/notify/fanotify/fanotify.c    | 14 +++-------
 fs/notify/fsnotify.c             | 48 ++++++++++++++------------------
 include/linux/fsnotify_backend.h | 17 ++++++-----
 3 files changed, 35 insertions(+), 44 deletions(-)
fs/notify/fanotify/fanotify.c +4 -10
index 985e995d2a39..263d303d8f8f 100644
@@ -319,11 +319,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
 			return 0;
 	}
 
-	fsnotify_foreach_iter_type(type) {
-		if (!fsnotify_iter_should_report_type(iter_info, type))
-			continue;
-		mark = iter_info->marks[type];
-
+	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
 		/* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
 		marks_ignored_mask |= mark->ignored_mask;
 
@@ -849,16 +845,14 @@ static struct fanotify_event *fanotify_alloc_event(
  */
 static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
 {
+	struct fsnotify_mark *mark;
 	int type;
 	__kernel_fsid_t fsid = {};
 
-	fsnotify_foreach_iter_type(type) {
+	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
 		struct fsnotify_mark_connector *conn;
 
-		if (!fsnotify_iter_should_report_type(iter_info, type))
-			continue;
-
-		conn = READ_ONCE(iter_info->marks[type]->connector);
+		conn = READ_ONCE(mark->connector);
 		/* Mark is just getting destroyed or created? */
 		if (!conn)
 			continue;
fs/notify/fsnotify.c +21 -27

93 lines not on this page · raw message

include/linux/fsnotify_backend.h +10 -7
index 9a1a9e78f69f..a21dccc255ee 100644
@@ -399,6 +399,7 @@ static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
 
 struct fsnotify_iter_info {
 	struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
+	struct fsnotify_group *current_group;
 	unsigned int report_mask;
 	int srcu_idx;
 };
@@ -415,20 +416,19 @@ static inline void fsnotify_iter_set_report_type(
 	iter_info->report_mask |= (1U << iter_type);
 }
 
-static inline void fsnotify_iter_set_report_type_mark(
-		struct fsnotify_iter_info *iter_info, int iter_type,
-		struct fsnotify_mark *mark)
+static inline struct fsnotify_mark *fsnotify_iter_mark(
+		struct fsnotify_iter_info *iter_info, int iter_type)
 {
-	iter_info->marks[iter_type] = mark;
-	iter_info->report_mask |= (1U << iter_type);
+	if (fsnotify_iter_should_report_type(iter_info, iter_type))
+		return iter_info->marks[iter_type];
+	return NULL;
 }
 
 #define FSNOTIFY_ITER_FUNCS(name, NAME) \
 static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
 		struct fsnotify_iter_info *iter_info) \
 { \
-	return (iter_info->report_mask & (1U << FSNOTIFY_ITER_TYPE_##NAME)) ? \
-		iter_info->marks[FSNOTIFY_ITER_TYPE_##NAME] : NULL; \
+	return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
 }
 
 FSNOTIFY_ITER_FUNCS(inode, INODE)
@@ -438,6 +438,9 @@ FSNOTIFY_ITER_FUNCS(sb, SB)
 
 #define fsnotify_foreach_iter_type(type) \
 	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
+#define fsnotify_foreach_iter_mark_type(iter, mark, type) \
+	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++) \
+		if (!(mark = fsnotify_iter_mark(iter, type))) {} else
 
 /*
  * fsnotify_connp_t is what we embed in objects which connector can be attached
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+	struct fsnotify_mark *mark;
 	int type;
 
 	fsnotify_foreach_iter_type(type) {
-		if (fsnotify_iter_should_report_type(iter_info, type))
+		mark = iter_info->marks[type];
+		if (mark && mark->group == iter_info->current_group)
 			iter_info->marks[type] =
 				fsnotify_next_mark(iter_info->marks[type]);

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

include/linux/fsnotify_backend.h FSNOTIFY_ITER_FUNCS(sb, SB) · line 443

Quoted 7 lines
include/linux/fsnotify_backend.h +3 -0
@@ -438,6 +438,9 @@ FSNOTIFY_ITER_FUNCS(sb, SB)
 
 #define fsnotify_foreach_iter_type(type) \
 	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
+#define fsnotify_foreach_iter_mark_type(iter, mark, type) \
+	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++) \
+		if (!(mark = fsnotify_iter_mark(iter, type))) {} else

Hum, you're really inventive here ;) I'd rather go for something a bit more conservative and readable like:

static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
struct fsnotify_mark **markp)

{
	while (type < FSNOTIFY_ITER_TYPE_COUNT) {
		*markp = fsnotify_iter_mark(iter, type);
		if (*markp)
			break;
		type++;
	}
	return type;
}
#define fsnotify_foreach_iter_mark_type(iter, mark, type) \
	for (type = 0; \
	     (type = fsnotify_iter_step(iter, type, &mark)) < FSNOTIFY_ITER_TYPE_COUNT; \
	     type++)


								Honza
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#3 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 38 lines · Jan Kara
Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+     struct fsnotify_mark *mark;
      int type;
 
      fsnotify_foreach_iter_type(type) {
-             if (fsnotify_iter_should_report_type(iter_info, type))
+             mark = iter_info->marks[type];
+             if (mark && mark->group == iter_info->current_group)
                      iter_info->marks[type] =
                              fsnotify_next_mark(iter_info->marks[type]);
Quoted 3 lines

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

Took me an embarrassing amount of time to figure out why this would be wrong and I must have known this a few weeks ago when I wrote the patch, so a comment is in order:

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to check if next group has a mark of type X even if current
* group did not have a mark of type X.
*/

Quoted 11 lines

include/linux/fsnotify_backend.h FSNOTIFY_ITER_FUNCS(sb, SB) · line 443

Quoted 8 lines
include/linux/fsnotify_backend.h +3 -0
@@ -438,6 +438,9 @@ FSNOTIFY_ITER_FUNCS(sb, SB)
 
 #define fsnotify_foreach_iter_type(type) \
      for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
+#define fsnotify_foreach_iter_mark_type(iter, mark, type) \
+     for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++) \
+             if (!(mark = fsnotify_iter_mark(iter, type))) {} else

Hum, you're really inventive here ;) I'd rather go for something a bit more conservative and readable like:

It's good that you are here to restrain me ;-)

Quoted 13 lines

static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
struct fsnotify_mark **markp)

{
        while (type < FSNOTIFY_ITER_TYPE_COUNT) {
                *markp = fsnotify_iter_mark(iter, type);
                if (*markp)
                        break;
                type++;
        }
        return type;
}
Quoted 6 lines
#define fsnotify_foreach_iter_mark_type(iter, mark, type) \
        for (type = 0; \
             (type = fsnotify_iter_step(iter, type, &mark)) < FSNOTIFY_ITER_TYPE_COUNT; \
             type++)

That looks nicer.

Thanks,
Amir.

maintainer
#4 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 47 lines · Amir Goldstein
Quoted 38 lines · Jan Kara
Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+     struct fsnotify_mark *mark;
      int type;
 
      fsnotify_foreach_iter_type(type) {
-             if (fsnotify_iter_should_report_type(iter_info, type))
+             mark = iter_info->marks[type];
+             if (mark && mark->group == iter_info->current_group)
                      iter_info->marks[type] =
                              fsnotify_next_mark(iter_info->marks[type]);
Quoted 3 lines

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

Took me an embarrassing amount of time to figure out why this would be wrong and I must have known this a few weeks ago when I wrote the patch, so a comment is in order:

Quoted 5 lines

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to check if next group has a mark of type X even if current
* group did not have a mark of type X.
*/

Well, but this function is just advancing the lists for marks we have already processed. And processed marks are exactly those set in report_mask. So your code should be equivalent to the old one but using fsnotify_foreach_iter_mark_type() should work as well AFAICT.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#5 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 55 lines · Jan Kara
Quoted 47 lines · Amir Goldstein
Quoted 38 lines · Jan Kara
Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+     struct fsnotify_mark *mark;
      int type;
 
      fsnotify_foreach_iter_type(type) {
-             if (fsnotify_iter_should_report_type(iter_info, type))
+             mark = iter_info->marks[type];
+             if (mark && mark->group == iter_info->current_group)
                      iter_info->marks[type] =
                              fsnotify_next_mark(iter_info->marks[type]);
Quoted 3 lines

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

Took me an embarrassing amount of time to figure out why this would be wrong and I must have known this a few weeks ago when I wrote the patch, so a comment is in order:

Quoted 5 lines

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to check if next group has a mark of type X even if current
* group did not have a mark of type X.
*/

Quoted 4 lines

Well, but this function is just advancing the lists for marks we have already processed. And processed marks are exactly those set in report_mask. So your code should be equivalent to the old one but using fsnotify_foreach_iter_mark_type() should work as well AFAICT.

Yes, you are right about that. But,
With the 2 patches applied, fanotify09 test gets into livelock here. Fixing the livelock requires that fsnotify_iter_should_report_type() condition is removed.

I am assuming that it is the next patch that introduces the livelock, but I cannot figure out why the comment I have written above is not true in the code in master.

Could it be that the livelock already exists but we do not have a test case to trigger it and the test case in fanotify09 which checks the next patch is just a private case?

In any case, even if the logic change here is not needed before the next patch, it would make no sense to convert this function to fsnotify_foreach_iter_mark_type() and change it back.
The most that could make sense is to add this comment and remove fsnotify_iter_should_report_type() together with the next patch.

Can you wrap your head around this? Because I can't.

Thanks,
Amir.

author reviewer
#6 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 62 lines · Amir Goldstein
Quoted 55 lines · Jan Kara
Quoted 47 lines · Amir Goldstein
Quoted 38 lines · Jan Kara
Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+     struct fsnotify_mark *mark;
      int type;
 
      fsnotify_foreach_iter_type(type) {
-             if (fsnotify_iter_should_report_type(iter_info, type))
+             mark = iter_info->marks[type];
+             if (mark && mark->group == iter_info->current_group)
                      iter_info->marks[type] =
                              fsnotify_next_mark(iter_info->marks[type]);
Quoted 3 lines

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

Took me an embarrassing amount of time to figure out why this would be wrong and I must have known this a few weeks ago when I wrote the patch, so a comment is in order:

Quoted 5 lines

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to check if next group has a mark of type X even if current
* group did not have a mark of type X.
*/

Quoted 4 lines

Well, but this function is just advancing the lists for marks we have already processed. And processed marks are exactly those set in report_mask. So your code should be equivalent to the old one but using fsnotify_foreach_iter_mark_type() should work as well AFAICT.

Quoted 8 lines

Yes, you are right about that. But,
With the 2 patches applied, fanotify09 test gets into livelock here. Fixing the livelock requires that fsnotify_iter_should_report_type() condition is removed.

I am assuming that it is the next patch that introduces the livelock, but I cannot figure out why the comment I have written above is not true in the code in master.

Nevermind. I got it.

Quoted 5 lines

Could it be that the livelock already exists but we do not have a test case to trigger it and the test case in fanotify09 which checks the next patch is just a private case?

No. This is because of the change of behavior in the next patch.

Quoted 5 lines

In any case, even if the logic change here is not needed before the next patch, it would make no sense to convert this function to fsnotify_foreach_iter_mark_type() and change it back.
The most that could make sense is to add this comment and remove fsnotify_iter_should_report_type() together with the next patch.

Yes. I will do that and better explain in the comment.

Thanks,
Amir.

author reviewer
#7 lore raw

Re: [PATCH 1/2] fsnotify: introduce mark type iterator

Quoted 76 lines · Amir Goldstein
Quoted 62 lines · Amir Goldstein
Quoted 55 lines · Jan Kara
Quoted 47 lines · Amir Goldstein
Quoted 38 lines · Jan Kara
Quoted 8 lines · Amir Goldstein

fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code of iteratating all marks of a specific group interested in an event by consulting the iterator report_mask.

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

Quoted 8 lines

At the moment, the two iterator variants are the same, but this decoupling will allow us to exclude some of the group's marks from reporting the event, for example for event on child and inode marks on parent did not request to watch events on children.

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Mostly looks good. Two nits below.

Quoted 7 lines

/*
- * Pop from iter_info multi head queue, the marks that were iterated in the
+ * Pop from iter_info multi head queue, the marks that belong to the group of
* current iteration step.
*/
static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) {

fs/notify/fsnotify.c static unsigned int fsnotify_iter_select_report_types( · line 445

Quoted 9 lines
fs/notify/fsnotify.c +3 -1
+     struct fsnotify_mark *mark;
      int type;
 
      fsnotify_foreach_iter_type(type) {
-             if (fsnotify_iter_should_report_type(iter_info, type))
+             mark = iter_info->marks[type];
+             if (mark && mark->group == iter_info->current_group)
                      iter_info->marks[type] =
                              fsnotify_next_mark(iter_info->marks[type]);
Quoted 3 lines

Wouldn't it be more natural here to use the new helper fsnotify_foreach_iter_mark_type()? In principle we want to advance mark types which were already reported...

Took me an embarrassing amount of time to figure out why this would be wrong and I must have known this a few weeks ago when I wrote the patch, so a comment is in order:

Quoted 5 lines

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to check if next group has a mark of type X even if current
* group did not have a mark of type X.
*/

Quoted 4 lines

Well, but this function is just advancing the lists for marks we have already processed. And processed marks are exactly those set in report_mask. So your code should be equivalent to the old one but using fsnotify_foreach_iter_mark_type() should work as well AFAICT.

Quoted 8 lines

Yes, you are right about that. But,
With the 2 patches applied, fanotify09 test gets into livelock here. Fixing the livelock requires that fsnotify_iter_should_report_type() condition is removed.

I am assuming that it is the next patch that introduces the livelock, but I cannot figure out why the comment I have written above is not true in the code in master.

Nevermind. I got it.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

Could it be that the livelock already exists but we do not have a test case to trigger it and the test case in fanotify09 which checks the next patch is just a private case?

No. This is because of the change of behavior in the next patch.

Bah, no it is not.
Emailing before coffee is not a good idea...

It is *this* patch that changes the behavior:
fsnotify_iter_select_report_types() can return true with 0 report_mask and it is valid.
This is documented in the updated comments above fsnotify_iter_next() and fsnotify_iter_select_report_types() and also in commit message:

Use an open coded version of that iterator in fsnotify_iter_next() that collects all marks of the current iteration group without consulting the iterator report_mask.

The comment I added to v2 could be better phrased as:

/*
* We cannot use fsnotify_foreach_iter_mark_type() here because we
* may need to advance a mark of type X that belongs to current_group
* but was not selected for reporting.
*/

Is that better? Do you want me to re-post or can you fix that on commit? Maybe even phrase it better than I did.

Thanks,
Amir.

author reviewer
#8 lore raw

PATCH 2/2 fsnotify: consistent behavior for parent not watching children

In reply to Amir Goldstein

The logic for handling events on child in groups that have a mark on the parent inode, but without FS_EVENT_ON_CHILD flag in the mask is duplicated in several places and inconsistent.

Move the logic into the preparation of mark type iterator, so that the parent mark type will be excluded from all mark type iterations in that case.

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignore mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignore mask of group A, but before this change it does

And if group A ignore mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")
---
 fs/notify/fanotify/fanotify.c | 10 +--------
 fs/notify/fsnotify.c          | 40 ++++++++++++++++++++++-------------
 2 files changed, 26 insertions(+), 24 deletions(-)
fs/notify/fanotify/fanotify.c +1 -9
index 263d303d8f8f..4f897e109547 100644
@@ -320,7 +320,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
 	}
 
 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
-		/* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
+		/* Apply ignore mask regardless of mark's ISDIR flag */
 		marks_ignored_mask |= mark->ignored_mask;
 
 		/*
@@ -330,14 +330,6 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
 		if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
 			continue;
 
-		/*
-		 * If the event is on a child and this mark is on a parent not
-		 * watching children, don't send it!
-		 */
-		if (type == FSNOTIFY_ITER_TYPE_PARENT &&
-		    !(mark->mask & FS_EVENT_ON_CHILD))
-			continue;
-
 		marks_mask |= mark->mask;
 
 		/* Record the mark types of this group that matched the event */
fs/notify/fsnotify.c +25 -15
index 00f11ef4c0ed..985cc56f4c7d 100644
@@ -290,22 +290,15 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
 	}
 
 	if (parent_mark) {
-		/*
-		 * parent_mark indicates that the parent inode is watching
-		 * children and interested in this event, which is an event
-		 * possible on child. But is *this mark* watching children and
-		 * interested in this event?
-		 */
-		if (parent_mark->mask & FS_EVENT_ON_CHILD) {
-			ret = fsnotify_handle_inode_event(group, parent_mark, mask,
-							  data, data_type, dir, name, 0);
-			if (ret)
-				return ret;
-		}
-		if (!inode_mark)
-			return 0;
+		ret = fsnotify_handle_inode_event(group, parent_mark, mask,
+						  data, data_type, dir, name, 0);
+		if (ret)
+			return ret;
 	}
 
+	if (!inode_mark)
+		return 0;
+
 	if (mask & FS_EVENT_ON_CHILD) {
 		/*
 		 * Some events can be sent on both parent dir and child marks
@@ -393,6 +386,23 @@ static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
 }
 
+static void fsnotify_iter_set_report_mark_type(
+		struct fsnotify_iter_info *iter_info, int iter_type)
+{
+	struct fsnotify_mark *mark = iter_info->marks[iter_type];
+
+	/*
+	 * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode is watching
+	 * children and interested in this event, which is an event
+	 * possible on child. But is *this mark* watching children?
+	 */
+	if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
+	    !(mark->mask & FS_EVENT_ON_CHILD))
+		return;
+
+	fsnotify_iter_set_report_type(iter_info, iter_type);
+}
+
 /*
  * iter_info is a multi head priority queue of marks.
  * Pick a subset of marks from queue heads, all with the same group
@@ -423,7 +433,7 @@ static bool fsnotify_iter_select_report_types(
 	fsnotify_foreach_iter_type(type) {
 		mark = iter_info->marks[type];
 		if (mark && mark->group == iter_info->current_group)
-			fsnotify_iter_set_report_type(iter_info, type);
+			fsnotify_iter_set_report_mark_type(iter_info, type);
 	}
 
 	return true;
Signature · 1 line
2.25.1
maintainer
#9 lore raw

Re: [PATCH 2/2] fsnotify: consistent behavior for parent not watching children

Quoted 11 lines · Amir Goldstein

The logic for handling events on child in groups that have a mark on the parent inode, but without FS_EVENT_ON_CHILD flag in the mask is duplicated in several places and inconsistent.

Move the logic into the preparation of mark type iterator, so that the parent mark type will be excluded from all mark type iterations in that case.

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

Quoted 7 lines

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignore mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignore mask of group A, but before this change it does

Since FS_MODIFY of directory never happens I guess the ignore mask is never cleared? Am I missing something?

Quoted 8 lines

And if group A ignore mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Just one nit below.

Quoted 25 lines
fs/notify/fsnotify.c +17 -0
@@ -393,6 +386,23 @@ static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
 	return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
 }
 
+static void fsnotify_iter_set_report_mark_type(
+		struct fsnotify_iter_info *iter_info, int iter_type)
+{
+	struct fsnotify_mark *mark = iter_info->marks[iter_type];
+
+	/*
+	 * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode is watching
+	 * children and interested in this event, which is an event
+	 * possible on child. But is *this mark* watching children?
+	 */
+	if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
+	    !(mark->mask & FS_EVENT_ON_CHILD))
+		return;
+
+	fsnotify_iter_set_report_type(iter_info, iter_type);
+}
+
 /*
  * iter_info is a multi head priority queue of marks.
  * Pick a subset of marks from queue heads, all with the same group

fs/notify/fsnotify.c static bool fsnotify_iter_select_report_types( · line 437

Quoted 6 lines
fs/notify/fsnotify.c +1 -1
@@ -423,7 +433,7 @@ static bool fsnotify_iter_select_report_types(
 	fsnotify_foreach_iter_type(type) {
 		mark = iter_info->marks[type];
 		if (mark && mark->group == iter_info->current_group)
-			fsnotify_iter_set_report_type(iter_info, type);
+			fsnotify_iter_set_report_mark_type(iter_info, type);
 	}

I think it is confusing to hide another condition in fsnotify_iter_set_report_mark_type() I'd rather have it explicitely here in fsnotify_iter_select_report_types().

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#10 lore raw

Re: [PATCH 2/2] fsnotify: consistent behavior for parent not watching children

Quoted 21 lines · Jan Kara
Quoted 11 lines · Amir Goldstein

The logic for handling events on child in groups that have a mark on the parent inode, but without FS_EVENT_ON_CHILD flag in the mask is duplicated in several places and inconsistent.

Move the logic into the preparation of mark type iterator, so that the parent mark type will be excluded from all mark type iterations in that case.

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

Quoted 7 lines

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignore mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignore mask of group A, but before this change it does

Quoted 2 lines

Since FS_MODIFY of directory never happens I guess the ignore mask is never cleared? Am I missing something?

According to the code in send_to_group()
If D has FS_EVENT_ON_CHILD in mask then
The the inode mask on D would get events on D/foo
therefore
The ignore mask on D should ignore events (e.g. from mount mark) on D/foo therefore
A MODIFY event on D/foo should clear the ignore mask on D

This is expected. The bug is that the ignore mask is cleared also when D does not have FS_EVENT_ON_CHILD in the mask.

Quoted 39 lines
Quoted 9 lines

And if group A ignore mask was set to survive FS_MODIFY:
- FS_MODIFY event on file D/foo should be reported to group A on account of the mount mark, but before this change it is wrongly ignored

Fixes 2f02fd3fa13e ("fanotify: fix ignore mask logic for events on child and on dir")

Just one nit below.

Quoted 27 lines
fs/notify/fsnotify.c +17 -0
@@ -393,6 +386,23 @@ static struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark)
      return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
 }
 
+static void fsnotify_iter_set_report_mark_type(
+             struct fsnotify_iter_info *iter_info, int iter_type)
+{
+     struct fsnotify_mark *mark = iter_info->marks[iter_type];
+
+     /*
+      * FSNOTIFY_ITER_TYPE_PARENT indicates that this inode is watching
+      * children and interested in this event, which is an event
+      * possible on child. But is *this mark* watching children?
+      */
+     if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
+         !(mark->mask & FS_EVENT_ON_CHILD))
+             return;
+
+     fsnotify_iter_set_report_type(iter_info, iter_type);
+}
+
 /*
  * iter_info is a multi head priority queue of marks.
  * Pick a subset of marks from queue heads, all with the same group
@@ -423,7 +433,7 @@ static bool fsnotify_iter_select_report_types(
      fsnotify_foreach_iter_type(type) {
              mark = iter_info->marks[type];
Quoted 8 lines

fs/notify/fsnotify.c static bool fsnotify_iter_select_report_types( · line 437

Quoted 4 lines
fs/notify/fsnotify.c +1 -1
              if (mark && mark->group == iter_info->current_group)
-                     fsnotify_iter_set_report_type(iter_info, type);
+                     fsnotify_iter_set_report_mark_type(iter_info, type);
      }

I think it is confusing to hide another condition in fsnotify_iter_set_report_mark_type() I'd rather have it explicitely here in fsnotify_iter_select_report_types().

OK.

Thanks,
Amir.

maintainer
#11 lore raw

Re: [PATCH 2/2] fsnotify: consistent behavior for parent not watching children

Quoted 33 lines · Amir Goldstein
Quoted 21 lines · Jan Kara
Quoted 11 lines · Amir Goldstein

The logic for handling events on child in groups that have a mark on the parent inode, but without FS_EVENT_ON_CHILD flag in the mask is duplicated in several places and inconsistent.

Move the logic into the preparation of mark type iterator, so that the parent mark type will be excluded from all mark type iterations in that case.

This results in several subtle changes of behavior, hopefully all desired changes of behavior, for example:

Quoted 7 lines

- Group A has a mount mark with FS_MODIFY in mask
- Group A has a mark with ignore mask that does not survive FS_MODIFY and does not watch children on directory D.
- Group B has a mark with FS_MODIFY in mask that does watch children on directory D.
- FS_MODIFY event on file D/foo should not clear the ignore mask of group A, but before this change it does

Quoted 2 lines

Since FS_MODIFY of directory never happens I guess the ignore mask is never cleared? Am I missing something?

According to the code in send_to_group()
If D has FS_EVENT_ON_CHILD in mask then
The the inode mask on D would get events on D/foo
therefore
The ignore mask on D should ignore events (e.g. from mount mark) on D/foo therefore
A MODIFY event on D/foo should clear the ignore mask on D

Quoted 2 lines

This is expected. The bug is that the ignore mask is cleared also when D does not have FS_EVENT_ON_CHILD in the mask.

Ah, now I understand. Thanks for explanation.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR