mark->mask, mark->flags and mark->ignore_mask are written under mark->lock, but they are also read without it. Add READ_ONCE() to these reads and WRITE_ONCE() to the writes. Also write the mask once in fsnotify_modify_mark_mask(), and read a field once into a local variable where a function reads it more than once without mark->lock. Signed-off-by: Seiji Nishikawa --- fs/notify/dnotify/dnotify.c | 2 +- fs/notify/fanotify/fanotify.c | 12 +++++++---- fs/notify/fanotify/fanotify.h | 7 ++++--- fs/notify/fanotify/fanotify_user.c | 32 +++++++++++++++++++----------- fs/notify/fdinfo.c | 10 ++++++---- fs/notify/inotify/inotify.h | 7 ++++--- fs/notify/mark.c | 32 +++++++++++++++++------------- include/linux/fsnotify_backend.h | 13 ++++++------ 8 files changed, 68 insertions(+), 47 deletions(-) diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c index 9fb73bafd..073cc7537 100644 --- a/fs/notify/dnotify/dnotify.c +++ b/fs/notify/dnotify/dnotify.c @@ -73,7 +73,7 @@ static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark) new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT); if (fsn_mark->mask == new_mask) return; - fsn_mark->mask = new_mask; + WRITE_ONCE(fsn_mark->mask, new_mask); fsnotify_recalc_mask(fsn_mark->connector); } diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 71b3a2361..5cff9423a 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -307,6 +307,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); struct fsnotify_mark *mark; bool ondir = event_mask & FAN_ONDIR; + __u32 mark_mask; int type; pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n", @@ -338,10 +339,11 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, /* * Send the event depending on event flags in mark mask. */ - if (!fsnotify_mask_applicable(mark->mask, ondir, type)) + mark_mask = READ_ONCE(mark->mask); + if (!fsnotify_mask_applicable(mark_mask, ondir, type)) continue; - marks_mask |= mark->mask; + marks_mask |= mark_mask; /* Record the mark types of this group that matched the event */ *match_mask |= 1U << type; @@ -894,10 +896,12 @@ static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info) __kernel_fsid_t fsid = {}; fsnotify_foreach_iter_mark_type(iter_info, mark, type) { - if (!(mark->flags & FSNOTIFY_MARK_FLAG_HAS_FSID)) + unsigned int flags = READ_ONCE(mark->flags); + + if (!(flags & FSNOTIFY_MARK_FLAG_HAS_FSID)) continue; fsid = FANOTIFY_MARK(mark)->fsid; - if (!(mark->flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) && + if (!(flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) && WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1])) continue; return fsid; diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 87ba78857..ad1764070 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -542,13 +542,14 @@ static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1, static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) { + unsigned int flags = READ_ONCE(mark->flags); unsigned int mflags = 0; - if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) + if (flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) mflags |= FAN_MARK_IGNORED_SURV_MODIFY; - if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF) + if (flags & FSNOTIFY_MARK_FLAG_NO_IREF) mflags |= FAN_MARK_EVICTABLE; - if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) + if (flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) mflags |= FAN_MARK_IGNORE; return mflags; diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 63c9759fc..172761890 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1229,9 +1229,10 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, spin_lock(&fsn_mark->lock); oldmask = fsnotify_calc_mask(fsn_mark); if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) { - fsn_mark->mask &= ~mask; + WRITE_ONCE(fsn_mark->mask, fsn_mark->mask & ~mask); } else { - fsn_mark->ignore_mask &= ~mask; + WRITE_ONCE(fsn_mark->ignore_mask, + fsn_mark->ignore_mask & ~mask); } newmask = fsnotify_calc_mask(fsn_mark); /* @@ -1290,7 +1291,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, * will result in EEXIST error. */ if (ignore == FAN_MARK_IGNORE) - fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS; + WRITE_ONCE(fsn_mark->flags, fsn_mark->flags | + FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS); /* * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to @@ -1299,7 +1301,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, */ if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) && !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) { - fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; + WRITE_ONCE(fsn_mark->flags, fsn_mark->flags | + FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY); if (!(fsn_mark->mask & FS_MODIFY)) recalc = true; } @@ -1313,7 +1316,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, * When removed, fsnotify_recalc_mask() will take the inode ref. */ WARN_ON_ONCE(!want_iref); - fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF; + WRITE_ONCE(fsn_mark->flags, + fsn_mark->flags & ~FSNOTIFY_MARK_FLAG_NO_IREF); return true; } @@ -1327,10 +1331,11 @@ static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, spin_lock(&fsn_mark->lock); if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS)) { old_mask = fsn_mark->mask; - fsn_mark->mask |= mask; + WRITE_ONCE(fsn_mark->mask, old_mask | mask); recalc = old_mask != fsn_mark->mask; } else { - fsn_mark->ignore_mask |= mask; + WRITE_ONCE(fsn_mark->ignore_mask, + fsn_mark->ignore_mask | mask); recalc = true; } @@ -1378,7 +1383,8 @@ static int fanotify_set_mark_fsid(struct fsnotify_group *group, return 0; /* Do not allow mixing of marks with weak and strong fsid */ - if ((mark->flags ^ old->flags) & FSNOTIFY_MARK_FLAG_WEAK_FSID) + if ((mark->flags ^ READ_ONCE(old->flags)) & + FSNOTIFY_MARK_FLAG_WEAK_FSID) return -EXDEV; /* Allow mixing of marks with strong fsid from different fs */ @@ -1465,18 +1471,20 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group) static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark, __u32 mask, unsigned int fan_flags) { + unsigned int flags = READ_ONCE(fsn_mark->flags); + /* * Non evictable mark cannot be downgraded to evictable mark. */ if (fan_flags & FAN_MARK_EVICTABLE && - !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) + !(flags & FSNOTIFY_MARK_FLAG_NO_IREF)) return -EEXIST; /* * New ignore mask semantics cannot be downgraded to old semantics. */ if (fan_flags & FAN_MARK_IGNORED_MASK && - fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) + flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) return -EEXIST; /* @@ -1487,11 +1495,11 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark, */ if (fan_flags & FAN_MARK_IGNORE && !(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) && - fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) + flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) return -EEXIST; /* For now pre-content events are not generated for directories */ - mask |= fsn_mark->mask; + mask |= READ_ONCE(fsn_mark->mask); if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) return -EEXIST; diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c index 0f731edde..63dd90e59 100644 --- a/fs/notify/fdinfo.c +++ b/fs/notify/fdinfo.c @@ -105,6 +105,8 @@ void inotify_show_fdinfo(struct seq_file *m, struct file *f) static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) { unsigned int mflags = fanotify_mark_user_flags(mark); + __u32 mask = READ_ONCE(mark->mask); + __u32 ignore_mask = READ_ONCE(mark->ignore_mask); struct inode *inode; if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) { @@ -113,7 +115,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) return; seq_printf(m, "fanotify ino:%llx sdev:%x mflags:%x mask:%x ignored_mask:%x ", inode->i_ino, inode->i_sb->s_dev, - mflags, mark->mask, mark->ignore_mask); + mflags, mask, ignore_mask); show_mark_fhandle(m, inode); seq_putc(m, '\n'); iput(inode); @@ -121,17 +123,17 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) struct mount *mnt = fsnotify_conn_mount(mark->connector); seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n", - mnt->mnt_id, mflags, mark->mask, mark->ignore_mask); + mnt->mnt_id, mflags, mask, ignore_mask); } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) { struct super_block *sb = fsnotify_conn_sb(mark->connector); seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n", - sb->s_dev, mflags, mark->mask, mark->ignore_mask); + sb->s_dev, mflags, mask, ignore_mask); } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_MNTNS) { struct mnt_namespace *mnt_ns = fsnotify_conn_mntns(mark->connector); seq_printf(m, "fanotify mnt_ns:%u mflags:%x mask:%x ignored_mask:%x\n", - mnt_ns->ns.inum, mflags, mark->mask, mark->ignore_mask); + mnt_ns->ns.inum, mflags, mask, ignore_mask); } } diff --git a/fs/notify/inotify/inotify.h b/fs/notify/inotify/inotify.h index 7d5df7a21..992e223b6 100644 --- a/fs/notify/inotify/inotify.h +++ b/fs/notify/inotify/inotify.h @@ -31,11 +31,12 @@ static inline struct inotify_event_info *INOTIFY_E(struct fsnotify_event *fse) static inline __u32 inotify_mark_user_mask(struct fsnotify_mark *fsn_mark) { - __u32 mask = fsn_mark->mask & INOTIFY_USER_MASK; + __u32 mask = READ_ONCE(fsn_mark->mask) & INOTIFY_USER_MASK; + unsigned int flags = READ_ONCE(fsn_mark->flags); - if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) + if (flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) mask |= IN_EXCL_UNLINK; - if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT) + if (flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT) mask |= IN_ONESHOT; return mask; diff --git a/fs/notify/mark.c b/fs/notify/mark.c index cb07f42d3..c7c9c2903 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -254,11 +254,13 @@ static bool __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) if (!fsnotify_valid_obj_type(conn->type)) return NULL; hlist_for_each_entry(mark, &conn->list, obj_list) { - if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) + unsigned int flags = READ_ONCE(mark->flags); + + if (!(flags & FSNOTIFY_MARK_FLAG_ATTACHED)) continue; new_mask |= fsnotify_calc_mask(mark); if (conn->type == FSNOTIFY_OBJ_TYPE_INODE && - !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) + !(flags & FSNOTIFY_MARK_FLAG_NO_IREF)) want_iref = true; } /* @@ -360,11 +362,11 @@ void fsnotify_modify_mark_mask(struct fsnotify_mark *mark, u32 set, u32 clear) WARN_ON_ONCE(clear & set); spin_lock(&mark->lock); - mask = mark->mask; - mark->mask |= set; - mark->mask &= ~clear; - if (mark->mask != mask) + mask = (mark->mask | set) & ~clear; + if (mark->mask != mask) { + WRITE_ONCE(mark->mask, mask); recalc = true; + } spin_unlock(&mark->lock); if (recalc) @@ -613,7 +615,8 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark) fsnotify_group_assert_locked(mark->group); WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) && refcount_read(&mark->refcnt) < 1 + - !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); + !!(READ_ONCE(mark->flags) & + FSNOTIFY_MARK_FLAG_ATTACHED)); spin_lock(&mark->lock); /* something else already called this function on this mark */ @@ -621,7 +624,7 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark) spin_unlock(&mark->lock); return; } - mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; + WRITE_ONCE(mark->flags, mark->flags & ~FSNOTIFY_MARK_FLAG_ATTACHED); list_del_init(&mark->g_list); spin_unlock(&mark->lock); @@ -647,7 +650,7 @@ void fsnotify_free_mark(struct fsnotify_mark *mark) spin_unlock(&mark->lock); return; } - mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; + WRITE_ONCE(mark->flags, mark->flags & ~FSNOTIFY_MARK_FLAG_ALIVE); spin_unlock(&mark->lock); /* @@ -945,7 +948,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj, last = lmark; if ((lmark->group == mark->group) && - (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) && + (READ_ONCE(lmark->flags) & FSNOTIFY_MARK_FLAG_ATTACHED) && !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) { err = -EEXIST; goto out_err; @@ -997,7 +1000,8 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, * mark->connector->lock */ spin_lock(&mark->lock); - mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; + WRITE_ONCE(mark->flags, mark->flags | FSNOTIFY_MARK_FLAG_ALIVE | + FSNOTIFY_MARK_FLAG_ATTACHED); list_add(&mark->g_list, &group->marks_list); fsnotify_get_mark(mark); /* for g_list */ @@ -1012,8 +1016,8 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, return ret; err: spin_lock(&mark->lock); - mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE | - FSNOTIFY_MARK_FLAG_ATTACHED); + WRITE_ONCE(mark->flags, mark->flags & ~(FSNOTIFY_MARK_FLAG_ALIVE | + FSNOTIFY_MARK_FLAG_ATTACHED)); list_del_init(&mark->g_list); spin_unlock(&mark->lock); @@ -1054,7 +1058,7 @@ struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type, hlist_for_each_entry(mark, &conn->list, obj_list) { if (mark->group == group && - (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { + (READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_ATTACHED)) { fsnotify_get_mark(mark); spin_unlock(&conn->lock); return mark; diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 5f0b94bfd..58ef5ba6d 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -799,7 +799,7 @@ extern void fsnotify_remove_queued_event(struct fsnotify_group *group, */ static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark) { - __u32 ignore_mask = mark->ignore_mask; + __u32 ignore_mask = READ_ONCE(mark->ignore_mask); /* The event flags in ignore mask take effect */ if (READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) @@ -820,7 +820,7 @@ static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark) /* Legacy ignored_mask - only event types to ignore */ static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark) { - return mark->ignore_mask & ALL_FSNOTIFY_EVENTS; + return READ_ONCE(mark->ignore_mask) & ALL_FSNOTIFY_EVENTS; } /* @@ -868,20 +868,21 @@ static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark, /* Get mask for calculating object interest taking ignore mask into account */ static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark) { - __u32 mask = mark->mask; + __u32 mask = READ_ONCE(mark->mask); + __u32 ignore_mask = READ_ONCE(mark->ignore_mask); - if (!fsnotify_ignored_events(mark)) + if (!(ignore_mask & ALL_FSNOTIFY_EVENTS)) return mask; /* Interest in FS_MODIFY may be needed for clearing ignore mask */ - if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) + if (!(READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) mask |= FS_MODIFY; /* * If mark is interested in ignoring events on children, the object must * show interest in those events for fsnotify_parent() to notice it. */ - return mask | mark->ignore_mask; + return mask | ignore_mask; } /* Get mask of events for a list of marks */ -- 2.43.0