From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796545058; b=Plj/5jrZplXdEFuCR6nIt8zAzL3ATB0bxO+bOcK8FatLyzDCafOy11XyloZvnBuNn58R rYVH6HvDYM1tftIHMbjL1PIc4eKZ7VwqbHM0fZwu+a+zganhgRmO5jOJgD2IlwZsf7EGt qAYX04Raw5vVxAIhMHf/waL0+KY7u0N8bC10al/1ctfVC+g9RXcRMq7EOzAJMSP07ba4p jPLF+Jp3+egYteAllmrMNnx96ay7zX9eBhPYCsQRibhk1okx4U8Nw4g8HYMdjzOkw5iPK 7W5ZoHlZHacLJhNwxZ8mOWZWCj4FoVf9zCwfgpZPf1zLZhxSJPEHu8Z0CV8ORNkgsDg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796545058; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=l29W2PIJQtdEtHyvjacJczIuen4vuGC8VteT/orpxMg=; b=njS6S/lvQOYCVFEMbsoKhvMMHfcCwcDWI4rS/W5MQRNCnkuQexj6fKIQDHoZMenm6Gr8 pAY+Wiv90jkkLm3685H2nlieBEAwX8YVDpOgZDX3NUXXORCFRT569jGnyar8nxNZssnBv xxUBY2pu1Tc6ZBVUkBmD6WLFRjzqL+tj8xVsc9HFPykFV97W1KAQqNAgn3cTVUckEF/Ie ZrY4Bx1PZDG6DTzBaAjdyjOE1hv5/wnXrV9UvOi84Xb7K6e1DV2C648swg0aOchdUh18k 6/8Rc06sw3dNzoNpxSL6UJ3I4hqR74bOy/j80rPyjPlkQXaLyxP3W+ef+JZ9Jqi9f8w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=d5GxoxJj; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6506A606A31F for ; Sun, 6 Dec 2026 08:17:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6506A606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=d5GxoxJj DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796545057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l29W2PIJQtdEtHyvjacJczIuen4vuGC8VteT/orpxMg=; b=d5GxoxJjbV8/34Gxq/A/9vYczodQcoVrUPwtiHH1Boug/4XCC5E+GbdGxfG5uT9Dv006C6 u+fgBbzFCrW/U8hyIBSCMH+g/GuNCCA5fQmpL25loyTUA6N0c6lzcRpPHJLJaLWf+OQtjv YrNdFOERGd0nTLorx5sZMP7j4uX/vWs= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-640-yV0XnA-QOcue_HlK6dXqGw-1; Sun, 6 Dec 2026 08:17:31 +0000 X-MC-Unique: yV0XnA-QOcue_HlK6dXqGw-1 X-Mimecast-MFC-AGG-ID: yV0XnA-QOcue_HlK6dXqGw_1796545050 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B84871956064; Sun, 6 Dec 2026 08:17:30 +0000 (UTC) Received: from snishika-thinkpadp16vgen1.tokyo.csb (headnet03.pony-001.prod.rdu2.dc.redhat.com [10.4.224.30]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 871C91801A78; Sun, 6 Dec 2026 08:17:28 +0000 (UTC) From: Seiji Nishikawa To: Jan Kara Cc: Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Seiji Nishikawa Subject: [PATCH v2 3/3] fsnotify: annotate lockless accesses to mark masks and flags Date: Sun, 6 Dec 2026 17:15:58 +0900 Message-ID: <20261206081558.1952451-3-snishika@redhat.com> In-Reply-To: <20261206080650.1945355-1-snishika@redhat.com> References: <20261206080650.1945355-1-snishika@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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