Skip to contentSkip to conversation

fsnotify: Rework inode tracking

RFC PATCH In review Jan Kara 2 participants 18 messages Nov 27, 2025 – Dec 1, 2025

Subsystems (4)
lore
Share & tools
lore

Jan Kara

author maintainer
#0 lore raw

cover fsnotify: Rework inode tracking

Hello!

This patch set reworks how fsnotify subsystem tracks inodes. So far we have held inode references from notification marks (connectors of list of notification marks to be more precise). This has three issues:

1) Placing a notification mark pins inode in memory. Since inodes are relatively big objects, users could pin significant amount of kernel memory by this. This then requires administrators to configure suitable limits on maximum amount of notification marks each user can place which is cumbersome.

2) During filesystem unmount we have walk a list of all inodes for the superblock to drop these inode references. This is either slow (when we do it before evicting all other inodes) or opens nasty races (when fsnotify() can run after dcache for the superblock has been evicted).

3) Since sb global inode list is a noticeable contention point we are trying to transition its users to something else so that we can eventually get rid of it.

In this patch set, fsnotify subsystem tracks notification marks attached to inodes in a specialrhashtable indexed by inode identifier. This way we can stop holding inode references and instead disconnect notification marks from the inode on inode eviction (and reconnect them when inode gets loaded into memory again). Credit for the original idea of this tracking actually goes to Dave Chinner.

The patches are so far incomplete - I still need to implement proper handling for filesystems where inode number isn't enough to identify the inode. isofs is provided as a sample how such support will look like for these filesystems.

Also we need to decide what to do with evictable notification marks. With the patches as is they are now never evicted. This makes sense because the main reason behind evictable marks was to avoid pinning the inode. On the other hand this *might* surprise some userspace - definitely it breaks couple of LTP tests we have for evictable marks.

Overall the patches passed some basic testing with LTP so they shouldn't be completely wrong but there could be bugs lurking so handle with care ;). I'm sending the patches for comments to the approach and whether people find this approach acceptable.

Honza

author maintainer
#1 lore raw

PATCH 1/13 fsnotify: Make detached connectors point to the superblock

Currently detached connectors have no association with the superblock for which they were created. This will get inconvenient when the detached connectors will be kept in a hash for later reconnection. So make detached connectors point to the superblock and fixup the two places using conn->obj for detecting detached connectors instead of conn->type.

---
 fs/notify/fsnotify.h | 2 ++
 fs/notify/mark.c     | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)
fs/notify/fsnotify.h +2 -0
index 5950c7a67f41..860a07ada7fd 100644
@@ -49,6 +49,8 @@ static inline struct super_block *fsnotify_object_sb(void *obj,
 		return ((struct vfsmount *)obj)->mnt_sb;
 	case FSNOTIFY_OBJ_TYPE_SB:
 		return (struct super_block *)obj;
+	case FSNOTIFY_OBJ_TYPE_DETACHED:
+		return (struct super_block *)obj;
 	default:
 		return NULL;
 	}
fs/notify/mark.c +4 -3
index 798340db69d7..26faa2f640b9 100644
@@ -176,7 +176,7 @@ static void fsnotify_update_sb_watchers(struct super_block *sb,
 	struct fsnotify_mark *first_mark = NULL;
 	unsigned int highest_prio = 0;
 
-	if (conn->obj)
+	if (conn->type != FSNOTIFY_OBJ_TYPE_DETACHED)
 		first_mark = hlist_entry_safe(conn->list.first,
 					      struct fsnotify_mark, obj_list);
 	if (first_mark)
@@ -355,7 +355,8 @@ static void *fsnotify_detach_connector_from_object(
 	}
 
 	rcu_assign_pointer(*connp, NULL);
-	conn->obj = NULL;
+	/* We make detached connectors point to the superblock */
+	conn->obj = sb;
 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
 	if (sb)
 		fsnotify_update_sb_watchers(sb, conn);
@@ -413,7 +414,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
 		struct super_block *sb = fsnotify_connector_sb(conn);
 
 		/* Update watched objects after detaching mark */
-		if (sb)
+		if (conn->type != FSNOTIFY_OBJ_TYPE_DETACHED)
 			fsnotify_update_sb_watchers(sb, conn);
 		objp = __fsnotify_recalc_mask(conn);
 		type = conn->type;
Signature · 1 line
2.51.0
author maintainer
#2 lore raw

PATCH 2/13 fsnotify: Remove sb argument from fsnotify_update_sb_watchers()

In reply to Jan Kara

Now that connector is always associated with the superblock, there's no need for the argument.

---
 fs/notify/mark.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
fs/notify/mark.c +5 -9
index 26faa2f640b9..ecd2c3944051 100644
@@ -168,9 +168,9 @@ static void fsnotify_put_inode_ref(struct inode *inode)
  * Grab or drop watched objects reference depending on whether the connector
  * is attached and has any marks attached.
  */
-static void fsnotify_update_sb_watchers(struct super_block *sb,
-					struct fsnotify_mark_connector *conn)
+static void fsnotify_update_sb_watchers(struct fsnotify_mark_connector *conn)
 {
+	struct super_block *sb = fsnotify_connector_sb(conn);
 	struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
 	bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED;
 	struct fsnotify_mark *first_mark = NULL;
@@ -358,8 +358,7 @@ static void *fsnotify_detach_connector_from_object(
 	/* We make detached connectors point to the superblock */
 	conn->obj = sb;
 	conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
-	if (sb)
-		fsnotify_update_sb_watchers(sb, conn);
+	fsnotify_update_sb_watchers(conn);
 
 	return inode;
 }
@@ -411,11 +410,9 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
 		objp = fsnotify_detach_connector_from_object(conn, &type);
 		free_conn = true;
 	} else {
-		struct super_block *sb = fsnotify_connector_sb(conn);
-
 		/* Update watched objects after detaching mark */
 		if (conn->type != FSNOTIFY_OBJ_TYPE_DETACHED)
-			fsnotify_update_sb_watchers(sb, conn);
+			fsnotify_update_sb_watchers(conn);
 		objp = __fsnotify_recalc_mask(conn);
 		type = conn->type;
 	}
@@ -778,8 +775,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,
 	/* mark should be the last entry.  last is the current last entry */
 	hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
 added:
-	if (sb)
-		fsnotify_update_sb_watchers(sb, conn);
+	fsnotify_update_sb_watchers(conn);
 	/*
 	 * Since connector is attached to object using cmpxchg() we are
 	 * guaranteed that connector initialization is fully visible by anyone
Signature · 1 line
2.51.0
author maintainer
#3 lore raw

PATCH 3/13 fsnotify: Introduce inode connector hash

In reply to Jan Kara

Introduce rhashtable that will contain all inode connectors for a superblock. As for some filesystems inode number is not enough to identify inode, provide enough flexibility for such filesystems to provide their own keys for the hash. Eventually we will use this hash table to track all inode connectors (and thus marks) for the superblock and also to track inode marks for inodes that were evicted from memory (so that inode marks don't have to pin inodes in memory anymore).

---
 fs/notify/fsnotify.c             |  12 ++-
 fs/notify/fsnotify.h             |   4 +-
 fs/notify/mark.c                 | 176 +++++++++++++++++++++++++++----
 include/linux/fs.h               |   3 +
 include/linux/fsnotify.h         |   9 ++
 include/linux/fsnotify_backend.h |   6 +-
 6 files changed, 187 insertions(+), 23 deletions(-)
fs/notify/fsnotify.c +9 -3
index 079b868552c2..46db712c83ec 100644
@@ -110,9 +110,16 @@ void fsnotify_sb_delete(struct super_block *sb)
 						  FSNOTIFY_PRIO_PRE_CONTENT));
 }
 
+void fsnotify_free_sb_info(struct fsnotify_sb_info *sbinfo)
+{
+	rhashtable_destroy(&sbinfo->inode_conn_hash);
+	kfree(sbinfo);
+}
+
 void fsnotify_sb_free(struct super_block *sb)
 {
-	kfree(sb->s_fsnotify_info);
+	if (sb->s_fsnotify_info)
+		fsnotify_free_sb_info(sb->s_fsnotify_info);
 }
 
 /*
@@ -770,8 +777,7 @@ static __init int fsnotify_init(void)
 	if (ret)
 		panic("initializing fsnotify_mark_srcu");
 
-	fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
-						    SLAB_PANIC);
+	fsnotify_init_connector_caches();
 
 	return 0;
 }
fs/notify/fsnotify.h +3 -1
index 860a07ada7fd..e9160c0e1a70 100644
@@ -108,6 +108,8 @@ static inline void fsnotify_clear_marks_by_mntns(struct mnt_namespace *mntns)
  */
 extern void fsnotify_set_children_dentry_flags(struct inode *inode);
 
-extern struct kmem_cache *fsnotify_mark_connector_cachep;
+void fsnotify_free_sb_info(struct fsnotify_sb_info *sbinfo);
+
+void fsnotify_init_connector_caches(void);
 
 #endif	/* __FS_NOTIFY_FSNOTIFY_H_ */
fs/notify/mark.c +158 -18

250 lines not on this page · raw message

include/linux/fs.h +3 -0
index 601d036a6c78..98890bb1592a 100644
@@ -1325,6 +1325,8 @@ struct sb_writers {
 	struct percpu_rw_semaphore	rw_sem[SB_FREEZE_LEVELS];
 };
 
+struct fsnotify_sb_operations;
+
 struct super_block {
 	struct list_head	s_list;		/* Keep this first */
 	dev_t			s_dev;		/* search index; _not_ kdev_t */
@@ -1385,6 +1387,7 @@ struct super_block {
 #ifdef CONFIG_FSNOTIFY
 	u32			s_fsnotify_mask;
 	struct fsnotify_sb_info	*s_fsnotify_info;
+	const struct fsnotify_sb_operations *s_fsnotify_op;
 #endif
 
 	/*
include/linux/fsnotify.h +9 -0
index 28a9cb13fbfa..2f94809cca2a 100644
@@ -17,6 +17,15 @@
 #include <linux/slab.h>
 #include <linux/bug.h>
 
+struct fsnotify_sb_operations {
+	struct fsnotify_mark_connector *(*alloc_inode_connector)(struct inode *inode);
+
+	const struct rhashtable_params inode_conn_hash_params;
+};
+
+int fsnotify_init_inode_connector(struct fsnotify_mark_connector *conn,
+				  struct inode *inode);
+
 /* Are there any inode/mount/sb objects watched with priority prio or above? */
 static inline bool fsnotify_sb_has_priority_watchers(struct super_block *sb,
 						     int prio)
include/linux/fsnotify_backend.h +5 -1
index d4034ddaf392..0a163c10b5e2 100644
@@ -21,6 +21,7 @@
 #include <linux/refcount.h>
 #include <linux/mempool.h>
 #include <linux/sched/mm.h>
+#include <linux/rhashtable.h>
 
 /*
  * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily
@@ -544,6 +545,7 @@ struct fsnotify_mark_connector {
 	unsigned char prio;	/* Highest priority group */
 #define FSNOTIFY_CONN_FLAG_IS_WATCHED	0x01
 #define FSNOTIFY_CONN_FLAG_HAS_IREF	0x02
+#define FSNOTIFY_CONN_FLAG_HASHED	0x04
 	unsigned short flags;	/* flags [lock] */
 	union {
 		/* Object pointer [lock] */
@@ -551,7 +553,7 @@ struct fsnotify_mark_connector {
 		/* Used listing heads to free after srcu period expires */
 		struct fsnotify_mark_connector *destroy_next;
 	};
-	struct hlist_head list;
+	struct hlist_head list;	/* List of marks */
 };
 
 /*
@@ -560,6 +562,8 @@ struct fsnotify_mark_connector {
  */
 struct fsnotify_sb_info {
 	struct fsnotify_mark_connector __rcu *sb_marks;
+	/* Hash of connectors for inode marks */
+	struct rhashtable inode_conn_hash;
 	/*
 	 * Number of inode/mount/sb objects that are being watched in this sb.
 	 * Note that inodes objects are currently double-accounted.
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH 03/13] fsnotify: Introduce inode connector hash

Quoted 140 lines · Jan Kara

Introduce rhashtable that will contain all inode connectors for a superblock. As for some filesystems inode number is not enough to identify inode, provide enough flexibility for such filesystems to provide their own keys for the hash. Eventually we will use this hash table to track all inode connectors (and thus marks) for the superblock and also to track inode marks for inodes that were evicted from memory (so that inode marks don't have to pin inodes in memory anymore).

---
 fs/notify/fsnotify.c             |  12 ++-
 fs/notify/fsnotify.h             |   4 +-
 fs/notify/mark.c                 | 176 +++++++++++++++++++++++++++----
 include/linux/fs.h               |   3 +
 include/linux/fsnotify.h         |   9 ++
 include/linux/fsnotify_backend.h |   6 +-
 6 files changed, 187 insertions(+), 23 deletions(-)
fs/notify/fsnotify.c +9 -3
index 079b868552c2..46db712c83ec 100644
@@ -110,9 +110,16 @@ void fsnotify_sb_delete(struct super_block *sb)
                                                  FSNOTIFY_PRIO_PRE_CONTENT));
 }
 
+void fsnotify_free_sb_info(struct fsnotify_sb_info *sbinfo)
+{
+       rhashtable_destroy(&sbinfo->inode_conn_hash);
+       kfree(sbinfo);
+}
+
 void fsnotify_sb_free(struct super_block *sb)
 {
-       kfree(sb->s_fsnotify_info);
+       if (sb->s_fsnotify_info)
+               fsnotify_free_sb_info(sb->s_fsnotify_info);
 }
 
 /*
@@ -770,8 +777,7 @@ static __init int fsnotify_init(void)
        if (ret)
                panic("initializing fsnotify_mark_srcu");
 
-       fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector,
-                                                   SLAB_PANIC);
+       fsnotify_init_connector_caches();
 
        return 0;
 }
fs/notify/fsnotify.h +3 -1
index 860a07ada7fd..e9160c0e1a70 100644
@@ -108,6 +108,8 @@ static inline void fsnotify_clear_marks_by_mntns(struct mnt_namespace *mntns)
  */
 extern void fsnotify_set_children_dentry_flags(struct inode *inode);
 
-extern struct kmem_cache *fsnotify_mark_connector_cachep;
+void fsnotify_free_sb_info(struct fsnotify_sb_info *sbinfo);
+
+void fsnotify_init_connector_caches(void);
 
 #endif /* __FS_NOTIFY_FSNOTIFY_H_ */
fs/notify/mark.c +17 -9
index ecd2c3944051..fd1fe8d37c36 100644
@@ -79,7 +79,8 @@
 #define FSNOTIFY_REAPER_DELAY  (1)     /* 1 jiffy */
 
 struct srcu_struct fsnotify_mark_srcu;
-struct kmem_cache *fsnotify_mark_connector_cachep;
+static struct kmem_cache *fsnotify_mark_connector_cachep;
+static struct kmem_cache *fsnotify_inode_mark_connector_cachep;
 
 static DEFINE_SPINLOCK(destroy_lock);
 static LIST_HEAD(destroy_list);
@@ -91,6 +92,8 @@ static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
 static void fsnotify_connector_destroy_workfn(struct work_struct *work);
 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
 
+static void fsnotify_unhash_connector(struct fsnotify_mark_connector *conn);
+
 void fsnotify_get_mark(struct fsnotify_mark *mark)
 {
        WARN_ON_ONCE(!refcount_read(&mark->refcnt));
@@ -323,7 +326,7 @@ static void fsnotify_connector_destroy_workfn(struct work_struct *work)
        while (conn) {
                free = conn;
                conn = conn->destroy_next;
-               kmem_cache_free(fsnotify_mark_connector_cachep, free);
+               kfree(free);
        }
 }
 
@@ -342,6 +345,7 @@ static void *fsnotify_detach_connector_from_object(
        if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
                inode = fsnotify_conn_inode(conn);
                inode->i_fsnotify_mask = 0;
+               fsnotify_unhash_connector(conn);
 
                /* Unpin inode when detaching from connector */
                if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
@@ -384,6 +388,15 @@ static void fsnotify_drop_object(unsigned int type, void *objp)
        fsnotify_put_inode_ref(objp);
 }
 
+static void fsnotify_free_connector(struct fsnotify_mark_connector *conn)
+{
+       spin_lock(&destroy_lock);
+       conn->destroy_next = connector_destroy_list;
+       connector_destroy_list = conn;
+       spin_unlock(&destroy_lock);
+       queue_work(system_unbound_wq, &connector_reaper_work);
+}
+
 void fsnotify_put_mark(struct fsnotify_mark *mark)
 {
        struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
@@ -421,13 +434,8 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
 
        fsnotify_drop_object(type, objp);
 
-       if (free_conn) {
-               spin_lock(&destroy_lock);
-               conn->destroy_next = connector_destroy_list;
-               connector_destroy_list = conn;
-               spin_unlock(&destroy_lock);
-               queue_work(system_unbound_wq, &connector_reaper_work);
-       }
+       if (free_conn)
+               fsnotify_free_connector(conn);
        /*
         * Note that we didn't update flags telling whether inode cares about
         * what's happening with children. We update these flags from
@@ -633,22 +641,136 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
        return -1;
 }

fs/notify/mark.c int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) · line 652

Quoted 10 lines
fs/notify/mark.c +9 -0
 
+/*
+ * Inode connector for filesystems where inode->i_ino uniquely identifies the
+ * inode.
+ */
+struct fsnotify_inode_mark_connector {
+       struct fsnotify_mark_connector common;
+       ino_t ino;
+       struct rhash_head hash_list;
+};

If only adding a list_head and no fs specific stuff,
I don't think we will need a dedicated fsnotify_inode_mark_connector

I think the whole thing could be done in a single not so complicated patch. I did not see any patch in the series other than the iteration patch and parts of this one that would be needed if we go for inode connectors list before going to the hash table.

Thanks,
Amir.

author maintainer
#5 lore raw

PATCH 4/13 fsnotify: Track number of connectors for superblock separately

In reply to Jan Kara

Currently number of watched objects is equal to the number of existing connectors. However in the future when connectors will no longer pin inodes these counts can differ. Introduce a separate counter of the number of existing connectors and use it what waiting for all marks and connectors to be freed.

---
 fs/notify/fsnotify.c             |  5 +++--
 fs/notify/mark.c                 | 13 ++++++++-----
 include/linux/fsnotify_backend.h |  2 ++
 3 files changed, 13 insertions(+), 7 deletions(-)
fs/notify/fsnotify.c +3 -2
index 46db712c83ec..f7f1d9ff3e38 100644
@@ -103,8 +103,9 @@ void fsnotify_sb_delete(struct super_block *sb)
 	fsnotify_unmount_inodes(sb);
 	fsnotify_clear_marks_by_sb(sb);
 	/* Wait for outstanding object references from connectors */
-	wait_var_event(fsnotify_sb_watched_objects(sb),
-		       !atomic_long_read(fsnotify_sb_watched_objects(sb)));
+	wait_var_event(&sbinfo->connector_count,
+		       !atomic_long_read(&sbinfo->connector_count));
+	WARN_ON(fsnotify_sb_has_watchers(sb));
 	WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_CONTENT));
 	WARN_ON(fsnotify_sb_has_priority_watchers(sb,
 						  FSNOTIFY_PRIO_PRE_CONTENT));
fs/notify/mark.c +8 -5
index fd1fe8d37c36..3df230e218fb 100644
@@ -145,11 +145,7 @@ static void fsnotify_get_sb_watched_objects(struct super_block *sb)
 
 static void fsnotify_put_sb_watched_objects(struct super_block *sb)
 {
-	atomic_long_t *watched_objects = fsnotify_sb_watched_objects(sb);
-
-	/* the superblock can go away after this decrement */
-	if (atomic_long_dec_and_test(watched_objects))
-		wake_up_var(watched_objects);
+	atomic_long_dec(fsnotify_sb_watched_objects(sb));
 }
 
 static void fsnotify_get_inode_ref(struct inode *inode)
@@ -390,6 +386,11 @@ static void fsnotify_drop_object(unsigned int type, void *objp)
 
 static void fsnotify_free_connector(struct fsnotify_mark_connector *conn)
 {
+	struct fsnotify_sb_info *sbinfo =
+				fsnotify_sb_info(fsnotify_connector_sb(conn));
+
+	if (atomic_long_dec_and_test(&sbinfo->connector_count))
+		wake_up_var(&sbinfo->connector_count);
 	spin_lock(&destroy_lock);
 	conn->destroy_next = connector_destroy_list;
 	connector_destroy_list = conn;
@@ -798,6 +799,8 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
 	if (!conn)
 		return -ENOMEM;
 
+	atomic_long_inc(
+		&fsnotify_sb_info(fsnotify_connector_sb(conn))->connector_count);
 	/*
 	 * cmpxchg() provides the barrier so that readers of *connp can see
 	 * only initialized structure
include/linux/fsnotify_backend.h +2 -0
index 0a163c10b5e2..41d913c328fa 100644
@@ -564,6 +564,8 @@ struct fsnotify_sb_info {
 	struct fsnotify_mark_connector __rcu *sb_marks;
 	/* Hash of connectors for inode marks */
 	struct rhashtable inode_conn_hash;
+	/* Number of connectors to objects in the superblock */
+	atomic_long_t connector_count;
 	/*
 	 * Number of inode/mount/sb objects that are being watched in this sb.
 	 * Note that inodes objects are currently double-accounted.
Signature · 1 line
2.51.0
author maintainer
#6 lore raw

PATCH 5/13 fsnotify: Remove fsnotify_sb_watched_objects()

In reply to Jan Kara

Remove fsnotify_sb_watched_objects() helper since it's now used only in fsnotify_get_sb_watched_objects() and fsnotify_put_sb_watched_objects(). Also make those two functions take sbinfo as that will be more common in the future.

---
 fs/notify/mark.c                 | 20 ++++++++++----------
 include/linux/fsnotify_backend.h |  5 -----
 2 files changed, 10 insertions(+), 15 deletions(-)
fs/notify/mark.c +10 -10
index 3df230e218fb..f6a197c63c1d 100644
@@ -138,20 +138,20 @@ __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
 	return READ_ONCE(*fsnotify_conn_mask_p(conn));
 }
 
-static void fsnotify_get_sb_watched_objects(struct super_block *sb)
+static void fsnotify_get_sb_watched_objects(struct fsnotify_sb_info *sbinfo)
 {
-	atomic_long_inc(fsnotify_sb_watched_objects(sb));
+	atomic_long_inc(&sbinfo->watched_objects[0]);
 }
 
-static void fsnotify_put_sb_watched_objects(struct super_block *sb)
+static void fsnotify_put_sb_watched_objects(struct fsnotify_sb_info *sbinfo)
 {
-	atomic_long_dec(fsnotify_sb_watched_objects(sb));
+	atomic_long_dec(&sbinfo->watched_objects[0]);
 }
 
 static void fsnotify_get_inode_ref(struct inode *inode)
 {
 	ihold(inode);
-	fsnotify_get_sb_watched_objects(inode->i_sb);
+	fsnotify_get_sb_watched_objects(fsnotify_sb_info(inode->i_sb));
 }
 
 static void fsnotify_put_inode_ref(struct inode *inode)
@@ -160,7 +160,7 @@ static void fsnotify_put_inode_ref(struct inode *inode)
 	struct super_block *sb = inode->i_sb;
 
 	iput(inode);
-	fsnotify_put_sb_watched_objects(sb);
+	fsnotify_put_sb_watched_objects(fsnotify_sb_info(sb));
 }
 
 /*
@@ -169,8 +169,8 @@ static void fsnotify_put_inode_ref(struct inode *inode)
  */
 static void fsnotify_update_sb_watchers(struct fsnotify_mark_connector *conn)
 {
-	struct super_block *sb = fsnotify_connector_sb(conn);
-	struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
+	struct fsnotify_sb_info *sbinfo =
+				fsnotify_sb_info(fsnotify_connector_sb(conn));
 	bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED;
 	struct fsnotify_mark *first_mark = NULL;
 	unsigned int highest_prio = 0;
@@ -198,10 +198,10 @@ static void fsnotify_update_sb_watchers(struct fsnotify_mark_connector *conn)
 	BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0);
 	if (first_mark && !is_watched) {
 		conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED;
-		fsnotify_get_sb_watched_objects(sb);
+		fsnotify_get_sb_watched_objects(sbinfo);
 	} else if (!first_mark && is_watched) {
 		conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED;
-		fsnotify_put_sb_watched_objects(sb);
+		fsnotify_put_sb_watched_objects(sbinfo);
 	}
 }
 
include/linux/fsnotify_backend.h +0 -5
index 41d913c328fa..bf6796be7561 100644
@@ -586,11 +586,6 @@ static inline struct fsnotify_sb_info *fsnotify_sb_info(struct super_block *sb)
 #endif
 }
 
-static inline atomic_long_t *fsnotify_sb_watched_objects(struct super_block *sb)
-{
-	return &fsnotify_sb_info(sb)->watched_objects[0];
-}
-
 /*
  * A mark is simply an object attached to an in core inode which allows an
  * fsnotify listener to indicate they are either no longer interested in events
Signature · 1 line
2.51.0
author maintainer
#7 lore raw

PATCH 6/13 isofs: Provide fsnotify inode mark connector

In reply to Jan Kara

For isofs ino_t needn't be enough to identify an inode on 32-bit platforms. Implement isofs variant of fsnotify inode mark connector so that we can reliably match the connector to the inode.

---
 fs/isofs/inode.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
fs/isofs/inode.c +41 -0
index 6f0e6b19383c..73ae9adf4161 100644
@@ -27,6 +27,7 @@
 #include <linux/blkdev.h>
 #include <linux/fs_context.h>
 #include <linux/fs_parser.h>
+#include <linux/fsnotify.h>
 
 #include "isofs.h"
 #include "zisofs.h"
@@ -288,6 +289,45 @@ isofs_dentry_cmpi_ms(const struct dentry *dentry,
 }
 #endif
 
+/* Structure for connecting fsnotify marks to isofs inodes */
+struct isofs_inode_fsnotify_connector {
+	struct fsnotify_mark_connector common;
+	struct rhash_head hash_list;
+	u32 block;
+	u32 offset;
+};
+
+static struct fsnotify_mark_connector *isofs_alloc_inode_connector(
+							struct inode *inode)
+{
+	struct isofs_inode_fsnotify_connector *iconn;
+	struct iso_inode_info *ei = ISOFS_I(inode);
+
+	iconn = kmalloc(sizeof(*iconn), GFP_KERNEL);
+	if (!iconn)
+		return NULL;
+
+	iconn->block = ei->i_iget5_block;
+	iconn->offset = ei->i_iget5_offset;
+	if (fsnotify_init_inode_connector(&iconn->common, inode)) {
+		kfree(iconn);
+		return NULL;
+	}
+
+	return &iconn->common;
+}
+
+static const struct rhashtable_params isofs_inode_conn_hash_params = {
+	.key_len = 8,
+	.key_offset = offsetof(struct isofs_inode_fsnotify_connector, block),
+	.head_offset = offsetof(struct isofs_inode_fsnotify_connector, hash_list),
+};
+
+static const struct fsnotify_sb_operations isofs_fsnotify_ops = {
+	.alloc_inode_connector = isofs_alloc_inode_connector,
+	.inode_conn_hash_params = isofs_inode_conn_hash_params,
+};
+
 enum {
 	Opt_block, Opt_check, Opt_cruft, Opt_gid, Opt_ignore, Opt_iocharset,
 	Opt_map, Opt_mode, Opt_nojoliet, Opt_norock, Opt_sb, Opt_session,
@@ -830,6 +870,7 @@ static int isofs_fill_super(struct super_block *s, struct fs_context *fc)
 #endif
 	s->s_op = &isofs_sops;
 	s->s_export_op = &isofs_export_ops;
+	s->s_fsnotify_op = &isofs_fsnotify_ops;
 	sbi->s_mapping = opt->map;
 	sbi->s_rock = (opt->rock ? 2 : 0);
 	sbi->s_rock_offset = -1; /* initial offset, will guess until SP is found*/
Signature · 1 line
2.51.0
author maintainer
#8 lore raw

PATCH 7/13 fsnotify: Use connector hash for destroying inode marks

In reply to Jan Kara

Instead of iterating all inodes belonging to a superblock to find inode marks and remove them on umount, iterate all inode connectors for the superblock. This may be substantially faster since there are generally much less inodes with fsnotify marks than all inodes. It also removes one use of sb->s_inodes list which we strive to ultimately remove.

---
 fs/notify/fsnotify.c | 78 ++++++++++++++++++--------------------------
 1 file changed, 32 insertions(+), 46 deletions(-)
fs/notify/fsnotify.c +32 -46
index f7f1d9ff3e38..6e4da46c10ad 100644
@@ -34,62 +34,48 @@ void __fsnotify_mntns_delete(struct mnt_namespace *mntns)
 }
 
 /**
- * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
- * @sb: superblock being unmounted.
+ * fsnotify_unmount_inodes - an sb is unmounting. Handle any watched inodes.
+ * @sbinfo: fsnotify info for superblock being unmounted.
  *
- * Called during unmount with no locks held, so needs to be safe against
- * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
+ * Walk all inode connectors for the superblock and free all associated marks.
  */
-static void fsnotify_unmount_inodes(struct super_block *sb)
+static void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo)
 {
-	struct inode *inode, *iput_inode = NULL;
+	int idx;
+	struct rhashtable_iter iter;
+	struct fsnotify_mark_connector *conn;
+	struct inode *inode;
 
-	spin_lock(&sb->s_inode_list_lock);
-	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
-		/*
-		 * We cannot __iget() an inode in state I_FREEING,
-		 * I_WILL_FREE, or I_NEW which is fine because by that point
-		 * the inode cannot have any associated watches.
-		 */
-		spin_lock(&inode->i_lock);
-		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
-			spin_unlock(&inode->i_lock);
+	/*
+	 * We hold srcu over the iteration so that returned connectors stay
+	 * allocated until we can grab them in fsnotify_destroy_conn_marks()
+	 */
+	idx = srcu_read_lock(&fsnotify_mark_srcu);
+	rhashtable_walk_enter(&sbinfo->inode_conn_hash, &iter);
+	rhashtable_walk_start(&iter);
+	while ((conn = rhashtable_walk_next(&iter)) != NULL) {
+		/* Table resized - we don't care... */
+		if (IS_ERR(conn))
 			continue;
-		}
-
-		/*
-		 * If i_count is zero, the inode cannot have any watches and
-		 * doing an __iget/iput with SB_ACTIVE clear would actually
-		 * evict all inodes with zero i_count from icache which is
-		 * unnecessarily violent and may in fact be illegal to do.
-		 * However, we should have been called /after/ evict_inodes
-		 * removed all zero refcount inodes, in any case.  Test to
-		 * be sure.
-		 */
-		if (!atomic_read(&inode->i_count)) {
-			spin_unlock(&inode->i_lock);
+		spin_lock(&conn->lock);
+		/* Connector got detached before we grabbed conn->lock? */
+		if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
+			spin_unlock(&conn->lock);
 			continue;
 		}
-
+		inode = conn->obj;
 		__iget(inode);
-		spin_unlock(&inode->i_lock);
-		spin_unlock(&sb->s_inode_list_lock);
-
-		iput(iput_inode);
-
-		/* for each watch, send FS_UNMOUNT and then remove it */
+		spin_unlock(&conn->lock);
+		rhashtable_walk_stop(&iter);
 		fsnotify_inode(inode, FS_UNMOUNT);
-
-		fsnotify_inode_delete(inode);
-
-		iput_inode = inode;
-
+		fsnotify_destroy_marks(&inode->i_fsnotify_marks);
+		iput(inode);
 		cond_resched();
-		spin_lock(&sb->s_inode_list_lock);
+		rhashtable_walk_start(&iter);
 	}
-	spin_unlock(&sb->s_inode_list_lock);
-
-	iput(iput_inode);
+	rhashtable_walk_stop(&iter);
+	rhashtable_walk_exit(&iter);
+	srcu_read_unlock(&fsnotify_mark_srcu, idx);
 }
 
 void fsnotify_sb_delete(struct super_block *sb)
@@ -100,7 +86,7 @@ void fsnotify_sb_delete(struct super_block *sb)
 	if (!sbinfo)
 		return;
 
-	fsnotify_unmount_inodes(sb);
+	fsnotify_unmount_inodes(sbinfo);
 	fsnotify_clear_marks_by_sb(sb);
 	/* Wait for outstanding object references from connectors */
 	wait_var_event(&sbinfo->connector_count,
Signature · 1 line
2.51.0
#9 lore raw

Re: [PATCH 07/13] fsnotify: Use connector hash for destroying inode marks

Quoted 6 lines · Jan Kara

Instead of iterating all inodes belonging to a superblock to find inode marks and remove them on umount, iterate all inode connectors for the superblock. This may be substantially faster since there are generally much less inodes with fsnotify marks than all inodes. It also removes one use of sb->s_inodes list which we strive to ultimately remove.

It should be simple to iterate an sb->s_inode_connectors list without the need of any of the patches until this point. Right?

Thanks,
Amir.

Quoted 116 lines
---
 fs/notify/fsnotify.c | 78 ++++++++++++++++++--------------------------
 1 file changed, 32 insertions(+), 46 deletions(-)
fs/notify/fsnotify.c +32 -47
index f7f1d9ff3e38..6e4da46c10ad 100644
@@ -34,62 +34,48 @@ void __fsnotify_mntns_delete(struct mnt_namespace *mntns)
 }
 
 /**
- * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
- * @sb: superblock being unmounted.
+ * fsnotify_unmount_inodes - an sb is unmounting. Handle any watched inodes.
+ * @sbinfo: fsnotify info for superblock being unmounted.
  *
- * Called during unmount with no locks held, so needs to be safe against
- * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
+ * Walk all inode connectors for the superblock and free all associated marks.
  */
-static void fsnotify_unmount_inodes(struct super_block *sb)
+static void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo)
 {
-       struct inode *inode, *iput_inode = NULL;
+       int idx;
+       struct rhashtable_iter iter;
+       struct fsnotify_mark_connector *conn;
+       struct inode *inode;
 
-       spin_lock(&sb->s_inode_list_lock);
-       list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
-               /*
-                * We cannot __iget() an inode in state I_FREEING,
-                * I_WILL_FREE, or I_NEW which is fine because by that point
-                * the inode cannot have any associated watches.
-                */
-               spin_lock(&inode->i_lock);
-               if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
-                       spin_unlock(&inode->i_lock);
+       /*
+        * We hold srcu over the iteration so that returned connectors stay
+        * allocated until we can grab them in fsnotify_destroy_conn_marks()
+        */
+       idx = srcu_read_lock(&fsnotify_mark_srcu);
+       rhashtable_walk_enter(&sbinfo->inode_conn_hash, &iter);
+       rhashtable_walk_start(&iter);
+       while ((conn = rhashtable_walk_next(&iter)) != NULL) {
+               /* Table resized - we don't care... */
+               if (IS_ERR(conn))
                        continue;
-               }
-
-               /*
-                * If i_count is zero, the inode cannot have any watches and
-                * doing an __iget/iput with SB_ACTIVE clear would actually
-                * evict all inodes with zero i_count from icache which is
-                * unnecessarily violent and may in fact be illegal to do.
-                * However, we should have been called /after/ evict_inodes
-                * removed all zero refcount inodes, in any case.  Test to
-                * be sure.
-                */
-               if (!atomic_read(&inode->i_count)) {
-                       spin_unlock(&inode->i_lock);
+               spin_lock(&conn->lock);
+               /* Connector got detached before we grabbed conn->lock? */
+               if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
+                       spin_unlock(&conn->lock);
                        continue;
                }
-
+               inode = conn->obj;
                __iget(inode);
-               spin_unlock(&inode->i_lock);
-               spin_unlock(&sb->s_inode_list_lock);
-
-               iput(iput_inode);
-
-               /* for each watch, send FS_UNMOUNT and then remove it */
+               spin_unlock(&conn->lock);
+               rhashtable_walk_stop(&iter);
                fsnotify_inode(inode, FS_UNMOUNT);
-
-               fsnotify_inode_delete(inode);
-
-               iput_inode = inode;
-
+               fsnotify_destroy_marks(&inode->i_fsnotify_marks);
+               iput(inode);
                cond_resched();
-               spin_lock(&sb->s_inode_list_lock);
+               rhashtable_walk_start(&iter);
        }
-       spin_unlock(&sb->s_inode_list_lock);
-
-       iput(iput_inode);
+       rhashtable_walk_stop(&iter);
+       rhashtable_walk_exit(&iter);
+       srcu_read_unlock(&fsnotify_mark_srcu, idx);
 }
 
 void fsnotify_sb_delete(struct super_block *sb)
@@ -100,7 +86,7 @@ void fsnotify_sb_delete(struct super_block *sb)
        if (!sbinfo)
                return;
 
-       fsnotify_unmount_inodes(sb);
+       fsnotify_unmount_inodes(sbinfo);
        fsnotify_clear_marks_by_sb(sb);
        /* Wait for outstanding object references from connectors */
        wait_var_event(&sbinfo->connector_count,
--

2.51.0

author maintainer
#10 lore raw

PATCH 8/13 fsnotify: Shutdown fsnotify before destroying sb's dcache

In reply to Jan Kara

Currently fsnotify_sb_delete() was called after we have evicted superblock's dcache and inode cache. This was done mainly so that we iterate as few inodes as possible when removing inode marks. However, as Jakub reported, this is problematic because for some filesystems encoding of file handles uses sb->s_root which gets cleared as part of dcache eviction. And either delayed fsnotify events or reading fdinfo for fsnotify group with marks on fs being unmounted may trigger encoding of file handles during unmount. So move shutdown of fsnotify subsystem before shrinking of dcache.

---
 fs/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/super.c +2 -2
index 7f876f32343a..d1045bce0741 100644
@@ -618,6 +618,7 @@ void generic_shutdown_super(struct super_block *sb)
 	const struct super_operations *sop = sb->s_op;
 
 	if (sb->s_root) {
+		fsnotify_sb_delete(sb);
 		shrink_dcache_for_umount(sb);
 		sync_filesystem(sb);
 		sb->s_flags &= ~SB_ACTIVE;
@@ -629,9 +630,8 @@ void generic_shutdown_super(struct super_block *sb)
 
 		/*
 		 * Clean up and evict any inodes that still have references due
-		 * to fsnotify or the security policy.
+		 * to the security policy.
 		 */
-		fsnotify_sb_delete(sb);
 		security_sb_delete(sb);
 
 		if (sb->s_dio_done_wq) {
Signature · 1 line
2.51.0
author maintainer
#11 lore raw

PATCH 9/13 fsnotify: Reconnect inode marks to inode on lookup

In reply to Jan Kara

When inode is being inserted into inode cache, lookup whether there are some notification marks for this inode and reconnect them to it.

---
 fs/inode.c                       |  1 +
 fs/notify/mark.c                 | 37 ++++++++++++++++++++++++++++++++
 include/linux/fsnotify_backend.h | 13 +++++++++++
 3 files changed, 51 insertions(+)
fs/inode.c +1 -0
index 01ebdc40021e..9f9507eea645 100644
@@ -1450,6 +1450,7 @@ struct inode *iget_locked(struct super_block *sb, unsigned long ino)
 			spin_unlock(&inode->i_lock);
 			spin_unlock(&inode_hash_lock);
 			inode_sb_list_add(inode);
+			fsnotify_generic_reconnect_inode_marks(inode);
 
 			/* Return the locked inode with I_NEW set, the
 			 * caller is responsible for filling in the contents
fs/notify/mark.c +37 -0
index f6a197c63c1d..8fe7128f4122 100644
@@ -731,6 +731,43 @@ fsnotify_alloc_inode_connector(struct inode *inode)
 	return &iconn->common;
 }
 
+/*
+ * If there are existing notification marks for the inode, attach them
+ * to it.
+ */
+void fsnotify_reconnect_inode_marks(struct inode *inode, void *key)
+{
+	struct fsnotify_sb_info *info = fsnotify_sb_info(inode->i_sb);
+	const struct rhashtable_params *params =
+			fsnotify_get_conn_hash_params(inode->i_sb);
+	struct fsnotify_inode_mark_connector *iconn;
+	struct fsnotify_mark_connector *conn;
+	int idx;
+
+	if (!info)
+		return;
+
+	idx = srcu_read_lock(&fsnotify_mark_srcu);
+	iconn = rhashtable_lookup_fast(&info->inode_conn_hash, key, *params);
+	if (!iconn)
+		goto out_srcu;
+	conn = &iconn->common;
+	spin_lock(&conn->lock);
+	/* The connector is just undergoing destruction? */
+	if (hlist_empty(&conn->list))
+		goto out_lock;
+	conn->type = FSNOTIFY_OBJ_TYPE_INODE;
+	conn->obj = inode;
+	rcu_assign_pointer(inode->i_fsnotify_marks, conn);
+	__fsnotify_recalc_mask(conn);
+	fsnotify_update_sb_watchers(conn);
+out_lock:
+	spin_unlock(&conn->lock);
+out_srcu:
+	srcu_read_unlock(&fsnotify_mark_srcu, idx);
+}
+EXPORT_SYMBOL(fsnotify_reconnect_inode_marks);
+
 static void fsnotify_unhash_connector(struct fsnotify_mark_connector *conn)
 {
 	if (!(conn->flags & FSNOTIFY_CONN_FLAG_HASHED))
include/linux/fsnotify_backend.h +13 -0
index bf6796be7561..267c6587af97 100644
@@ -921,6 +921,13 @@ static inline void fsnotify_init_event(struct fsnotify_event *event)
 int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
 			 size_t count);
 
+void fsnotify_reconnect_inode_marks(struct inode *inode, void *key);
+/* Reconnection function for filesystems using fsnotify_inode_mark_connector */
+static inline void fsnotify_generic_reconnect_inode_marks(struct inode *inode)
+{
+	fsnotify_reconnect_inode_marks(inode, &inode->i_ino);
+}
+
 #else
 
 static inline int fsnotify_pre_content(const struct path *path,
@@ -971,6 +978,12 @@ static inline void fsnotify_unmount_inodes(struct super_block *sb)
 static inline void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)
 {}
 
+static inline void fsnotify_reconnect_inode_marks(struct inode *inode, void *key)
+{}
+
+static inline void fsnotify_generic_reconnect_inode_marks(struct inode *inode)
+{}
+
 #endif	/* CONFIG_FSNOTIFY */
 
 #endif	/* __KERNEL __ */
Signature · 1 line
2.51.0
author maintainer
#12 lore raw

PATCH 10/13 fsnotify: Stop holding inode references from inode mark connectors

In reply to Jan Kara

Currently inode mark connectors hold reference to inode thus keeping it pinned in memory. Now that we have infrastructure to keep inode marks in a separate hash and reconnect them to inode on lookup there's no need for this pinning anymore. Remove it so that reclaim can evict inodes with notification marks thus significantly reducing effective memory footprint of inode notification marks.

---
 fs/inode.c                       |   2 +-
 fs/notify/fsnotify.c             |  20 +++-
 fs/notify/fsnotify.h             |   5 +
 fs/notify/mark.c                 | 157 +++++++++----------------------
 include/linux/fsnotify.h         |   6 +-
 include/linux/fsnotify_backend.h |   7 +-
 6 files changed, 76 insertions(+), 121 deletions(-)
fs/inode.c +1 -1
index 9f9507eea645..196c727365db 100644
@@ -369,7 +369,7 @@ void __destroy_inode(struct inode *inode)
 	BUG_ON(inode_has_buffers(inode));
 	inode_detach_wb(inode);
 	security_inode_free(inode);
-	fsnotify_inode_delete(inode);
+	fsnotify_inode_evict(inode);
 	locks_free_lock_context(inode);
 	if (!inode->i_nlink) {
 		WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
fs/notify/fsnotify.c +17 -3
index 6e4da46c10ad..5ee94a6d9422 100644
@@ -14,8 +14,13 @@
 #include <linux/fsnotify_backend.h>
 #include "fsnotify.h"
 
+void __fsnotify_inode_evict(struct inode *inode)
+{
+	fsnotify_detach_marks(&inode->i_fsnotify_marks);
+}
+
 /*
- * Clear all of the marks on an inode when it is being evicted from core
+ * Clear all of the marks on an inode when it is being deleted
  */
 void __fsnotify_inode_delete(struct inode *inode)
 {
@@ -60,7 +65,8 @@ static void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo)
 		spin_lock(&conn->lock);
 		/* Connector got detached before we grabbed conn->lock? */
 		if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
-			spin_unlock(&conn->lock);
+			/* drops conn->lock... */
+			__fsnotify_destroy_marks(conn);
 			continue;
 		}
 		inode = conn->obj;
@@ -68,7 +74,15 @@ static void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo)
 		spin_unlock(&conn->lock);
 		rhashtable_walk_stop(&iter);
 		fsnotify_inode(inode, FS_UNMOUNT);
-		fsnotify_destroy_marks(&inode->i_fsnotify_marks);
+		/*
+		 * Now when notifications are sent, we can destroy marks.
+		 * The connector can be detached by now but since we held
+		 * fsnotify_mark_srcu all the time, the connector still has to
+		 * be allocated even though we could be racing with other
+		 * processes trying to destroy it.
+		 */
+		spin_lock(&conn->lock);
+		__fsnotify_destroy_marks(conn);
 		iput(inode);
 		cond_resched();
 		rhashtable_walk_start(&iter);
fs/notify/fsnotify.h +5 -0
index e9160c0e1a70..7e0f89a64516 100644
@@ -79,8 +79,13 @@ extern struct srcu_struct fsnotify_mark_srcu;
 extern int fsnotify_compare_groups(struct fsnotify_group *a,
 				   struct fsnotify_group *b);
 
+extern void __fsnotify_destroy_marks(struct fsnotify_mark_connector *conn);
 /* Destroy all marks attached to an object via connector */
 extern void fsnotify_destroy_marks(fsnotify_connp_t *connp);
+
+/* Detach marks from object but keep them alive (and connector hashed) */
+extern void fsnotify_detach_marks(fsnotify_connp_t *connp);
+
 /* run the list of all marks associated with inode and destroy them */
 static inline void fsnotify_clear_marks_by_inode(struct inode *inode)
 {
fs/notify/mark.c +45 -112

255 lines not on this page · raw message

include/linux/fsnotify.h +3 -3
index 2f94809cca2a..b3ab48079987 100644
@@ -289,11 +289,11 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
 }
 
 /*
- * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
+ * fsnotify_inode_evict - an inode is being evicted from cache, clean up is needed
  */
-static inline void fsnotify_inode_delete(struct inode *inode)
+static inline void fsnotify_inode_evict(struct inode *inode)
 {
-	__fsnotify_inode_delete(inode);
+	__fsnotify_inode_evict(inode);
 }
 
 /*
include/linux/fsnotify_backend.h +5 -2
index 267c6587af97..c7ca848f576c 100644
@@ -544,8 +544,7 @@ struct fsnotify_mark_connector {
 	unsigned char type;	/* Type of object [lock] */
 	unsigned char prio;	/* Highest priority group */
 #define FSNOTIFY_CONN_FLAG_IS_WATCHED	0x01
-#define FSNOTIFY_CONN_FLAG_HAS_IREF	0x02
-#define FSNOTIFY_CONN_FLAG_HASHED	0x04
+#define FSNOTIFY_CONN_FLAG_HASHED	0x02
 	unsigned short flags;	/* flags [lock] */
 	union {
 		/* Object pointer [lock] */
@@ -647,6 +646,7 @@ extern int fsnotify(__u32 mask, const void *data, int data_type,
 extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
 			   int data_type);
 extern void __fsnotify_inode_delete(struct inode *inode);
+extern void __fsnotify_inode_evict(struct inode *inode);
 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
 extern void fsnotify_sb_delete(struct super_block *sb);
 extern void __fsnotify_mntns_delete(struct mnt_namespace *mntns);
@@ -952,6 +952,9 @@ static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
 static inline void __fsnotify_inode_delete(struct inode *inode)
 {}
 
+static inline void __fsnotify_inode_evict(struct inode *inode)
+{}
+
 static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
 {}
 
Signature · 1 line
2.51.0
author maintainer
#13 lore raw

PATCH 11/13 fsnotify: Drop fsnotify_{get,put}_sb_watched_objects()

In reply to Jan Kara

These are used only in a single place now. Inline them to the call site.

---
 fs/notify/mark.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
fs/notify/mark.c +2 -12
index c01c38244f30..c9b5ca198f3b 100644
@@ -138,16 +138,6 @@ __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
 	return READ_ONCE(*fsnotify_conn_mask_p(conn));
 }
 
-static void fsnotify_get_sb_watched_objects(struct fsnotify_sb_info *sbinfo)
-{
-	atomic_long_inc(&sbinfo->watched_objects[0]);
-}
-
-static void fsnotify_put_sb_watched_objects(struct fsnotify_sb_info *sbinfo)
-{
-	atomic_long_dec(&sbinfo->watched_objects[0]);
-}
-
 /*
  * Grab or drop watched objects reference depending on whether the connector
  * is attached and has any marks attached.
@@ -183,10 +173,10 @@ static void fsnotify_update_sb_watchers(struct fsnotify_mark_connector *conn)
 	BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0);
 	if (first_mark && !is_watched) {
 		conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED;
-		fsnotify_get_sb_watched_objects(sbinfo);
+		atomic_long_inc(&sbinfo->watched_objects[0]);
 	} else if (!first_mark && is_watched) {
 		conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED;
-		fsnotify_put_sb_watched_objects(sbinfo);
+		atomic_long_dec(&sbinfo->watched_objects[0]);
 	}
 }
 
Signature · 1 line
2.51.0
author maintainer
#14 lore raw

PATCH 12/13 fanotify: Don't recalculate mask when mark evictable flag changes

In reply to Jan Kara

Since connectors no longer hold inode references, there's no need to call fsnotify_recalc_mask() only because FSNOTIFY_MARK_FLAG_NO_IREF flag changed.

---
 fs/notify/fanotify/fanotify_user.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
fs/notify/fanotify/fanotify_user.c +6 -15
index b192ee068a7a..2c440ea96521 100644
@@ -1202,9 +1202,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
 static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
 				       unsigned int fan_flags)
 {
-	bool want_iref = !(fan_flags & FAN_MARK_EVICTABLE);
 	unsigned int ignore = fan_flags & FANOTIFY_MARK_IGNORE_BITS;
-	bool recalc = false;
 
 	/*
 	 * When using FAN_MARK_IGNORE for the first time, mark starts using
@@ -1215,6 +1213,10 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
 	if (ignore == FAN_MARK_IGNORE)
 		fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
 
+	/* NO_IREF may be removed from a mark, but not added */
+	if (!(fan_flags & FAN_MARK_EVICTABLE))
+		fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
+
 	/*
 	 * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
 	 * the removal of the FS_MODIFY bit in calculated mask if it was set
@@ -1224,21 +1226,10 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
 	    !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
 		fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
 		if (!(fsn_mark->mask & FS_MODIFY))
-			recalc = true;
+			return true;
 	}
 
-	if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE ||
-	    want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
-		return recalc;
-
-	/*
-	 * NO_IREF may be removed from a mark, but not added.
-	 * When removed, fsnotify_recalc_mask() will take the inode ref.
-	 */
-	WARN_ON_ONCE(!want_iref);
-	fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
-
-	return true;
+	return false;
 }
 
 static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
Signature · 1 line
2.51.0
author maintainer
#15 lore raw

PATCH 13/13 fsnotify: Rename FSNOTIFY_MARK_FLAG_NO_IREF

In reply to Jan Kara

Since notification marks no longer hold inode references the name of flag FSNOTIFY_MARK_FLAG_NO_IREF doesn't make sense anymore. Rename it to FSNOTIFY_MARK_FLAG_EVICTABLE.

---
 fs/notify/fanotify/fanotify.h      | 2 +-
 fs/notify/fanotify/fanotify_user.c | 8 ++++----
 include/linux/fsnotify_backend.h   | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
fs/notify/fanotify/fanotify.h +1 -1
index b78308975082..1521a5bf2b9e 100644
@@ -538,7 +538,7 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
 
 	if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
 		mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
-	if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)
+	if (mark->flags & FSNOTIFY_MARK_FLAG_EVICTABLE)
 		mflags |= FAN_MARK_EVICTABLE;
 	if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
 		mflags |= FAN_MARK_IGNORE;
fs/notify/fanotify/fanotify_user.c +4 -4
index 2c440ea96521..c820fe9aee20 100644
@@ -1213,9 +1213,9 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
 	if (ignore == FAN_MARK_IGNORE)
 		fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
 
-	/* NO_IREF may be removed from a mark, but not added */
+	/* EVICTABLE may be removed from a mark, but not added */
 	if (!(fan_flags & FAN_MARK_EVICTABLE))
-		fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
+		fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_EVICTABLE;
 
 	/*
 	 * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
@@ -1339,7 +1339,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
 	mark = &fan_mark->fsn_mark;
 	fsnotify_init_mark(mark, group);
 	if (fan_flags & FAN_MARK_EVICTABLE)
-		mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF;
+		mark->flags |= FSNOTIFY_MARK_FLAG_EVICTABLE;
 
 	/* Cache fsid of filesystem containing the marked object */
 	if (fsid) {
@@ -1381,7 +1381,7 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
 	 * Non evictable mark cannot be downgraded to evictable mark.
 	 */
 	if (fan_flags & FAN_MARK_EVICTABLE &&
-	    !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
+	    !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_EVICTABLE))
 		return -EEXIST;
 
 	/*
include/linux/fsnotify_backend.h +1 -1
index c7ca848f576c..4b7bd48dd9aa 100644
@@ -628,7 +628,7 @@ struct fsnotify_mark {
 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT		0x0020
 	/* fanotify mark flags */
 #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY	0x0100
-#define FSNOTIFY_MARK_FLAG_NO_IREF		0x0200
+#define FSNOTIFY_MARK_FLAG_EVICTABLE		0x0200
 #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS	0x0400
 #define FSNOTIFY_MARK_FLAG_HAS_FSID		0x0800
 #define FSNOTIFY_MARK_FLAG_WEAK_FSID		0x1000
Signature · 1 line
2.51.0
#16 lore raw
In reply to Jan Kara
Quoted 4 lines · Jan Kara

Hello!

This patch set reworks how fsnotify subsystem tracks inodes.

I like the vision, but I think we need to break the problem into smaller pieces and solve them one at a time.

The first and foremost problem is the nasty races like [1] and more that are possible in upstream and stable kernels.

[1] https://lore.kernel.org/linux-unionfs/[email protected]/

I think it is a worthy objective to be able to solve the race in stable kernels, so I am suggesting a more conservative approach...

Quoted 9 lines

So far we have
held inode references from notification marks (connectors of list of notification marks to be more precise). This has three issues:

1) Placing a notification mark pins inode in memory. Since inodes are relatively big objects, users could pin significant amount of kernel memory by this. This then requires administrators to configure suitable limits on maximum amount of notification marks each user can place which is cumbersome.

Quoted 5 lines

2) During filesystem unmount we have walk a list of all inodes for the superblock to drop these inode references. This is either slow (when we do it before evicting all other inodes) or opens nasty races (when fsnotify() can run after dcache for the superblock has been evicted).

This can be solved by putting the inode connector on a list. AFAICT, the hash table code (which is ~90% of this pathset) is not needed to solve the race vs. inode walk problem.

Quoted 11 lines

3) Since sb global inode list is a noticeable contention point we are trying to transition its users to something else so that we can eventually get rid of it.

In this patch set, fsnotify subsystem tracks notification marks attached to inodes in a specialrhashtable indexed by inode identifier. This way we can stop holding inode references and instead disconnect notification marks from the inode on inode eviction (and reconnect them when inode gets loaded into memory again). Credit for the original idea of this tracking actually goes to Dave Chinner.

Quoted 4 lines

The patches are so far incomplete - I still need to implement proper handling for filesystems where inode number isn't enough to identify the inode. isofs is provided as a sample how such support will look like for these filesystems.

If you accept the POV that the hash table is an optimization to solve the pinned inodes situation, then when adding hash table code, I think it is safer to have opt-in sb flag to participate in "independent inode marks" and test the code first without having to implement all special filesystems fsnotify operations.

Heck, if only for memory optimization and not for the end goal of getting rid of global sb inode list, then there may be no reason at all for optimizing those special filesystems for recursive inotify memory usage - a problem that existed forever.

Quoted 5 lines

Also we need to decide what to do with evictable notification marks. With the patches as is they are now never evicted. This makes sense because the main reason behind evictable marks was to avoid pinning the inode. On the other hand this *might* surprise some userspace - definitely it breaks couple of LTP tests we have for evictable marks.

Those tests were never super reliable, because as in real workloads, users can't usually know, short of unmount, if inode was really evicted.

What we can do going forward is use this currently inval flag combo #define FAN_MARK_EVICTABLE_INODE \
(FAN_MARK_EVICTABLE | FAN_MARK_MOUNT)

to opt-in for not pinning the inode without loosing the mark
as an explicit test for a kernel that supports this improvement, rather than users doing recursive inotify without knowing the consequences.

Thanks,
Amir.

Quoted 7 lines

Overall the patches passed some basic testing with LTP so they shouldn't be completely wrong but there could be bugs lurking so handle with care ;). I'm sending the patches for comments to the approach and whether people find this approach acceptable.

Honza

author maintainer
#17 lore raw
Quoted 9 lines · Amir Goldstein
Quoted 4 lines · Jan Kara

Hello!

This patch set reworks how fsnotify subsystem tracks inodes.

I like the vision, but I think we need to break the problem into smaller pieces and solve them one at a time.

Quoted 7 lines

The first and foremost problem is the nasty races like [1] and more that are possible in upstream and stable kernels.

[1] https://lore.kernel.org/linux-unionfs/[email protected]/

I think it is a worthy objective to be able to solve the race in stable kernels, so I am suggesting a more conservative approach...

So far nobody was able to hit these races in practice (even syzbot ;)) so I've let my creativity go wild but perhaps you're right and we could make the patch set so that the initial part of it is not very intrusive and deals with the races. I did the patches the way they are because I was most interested to see how the rhashtable idea works out and what we can simplify when using it. Fixing the races was just a nice side effect :).

Quoted 15 lines
Quoted 9 lines

So far we have
held inode references from notification marks (connectors of list of notification marks to be more precise). This has three issues:

1) Placing a notification mark pins inode in memory. Since inodes are relatively big objects, users could pin significant amount of kernel memory by this. This then requires administrators to configure suitable limits on maximum amount of notification marks each user can place which is cumbersome.

Quoted 5 lines

2) During filesystem unmount we have walk a list of all inodes for the superblock to drop these inode references. This is either slow (when we do it before evicting all other inodes) or opens nasty races (when fsnotify() can run after dcache for the superblock has been evicted).

Quoted 3 lines

This can be solved by putting the inode connector on a list. AFAICT, the hash table code (which is ~90% of this pathset) is not needed to solve the race vs. inode walk problem.

Correct, the race-handling part could be made less intrusive by using a list.

Quoted 21 lines
Quoted 11 lines

3) Since sb global inode list is a noticeable contention point we are trying to transition its users to something else so that we can eventually get rid of it.

In this patch set, fsnotify subsystem tracks notification marks attached to inodes in a specialrhashtable indexed by inode identifier. This way we can stop holding inode references and instead disconnect notification marks from the inode on inode eviction (and reconnect them when inode gets loaded into memory again). Credit for the original idea of this tracking actually goes to Dave Chinner.

Quoted 3 lines

The patches are so far incomplete - I still need to implement proper handling for filesystems where inode number isn't enough to identify the inode. isofs is provided as a sample how such support will look like for these filesystems.

If you accept the POV that the hash table is an optimization to solve the pinned inodes situation, then when adding hash table code, I think it is safer to have opt-in sb flag to participate in "independent inode marks" and test the code first without having to implement all special filesystems fsnotify operations.

Quoted 4 lines

Heck, if only for memory optimization and not for the end goal of getting rid of global sb inode list, then there may be no reason at all for optimizing those special filesystems for recursive inotify memory usage - a problem that existed forever.

I'll consider this. So far I think it is beneficial for the lifetime rules on all filesystems to be the same and it also allows us to get rid of the inode pinning code which has its complexity. As I went through the filesystems there aren't that many that need special treatment (like 19) and for each of them the adaptation is fairly simple so it isn't that huge task.

Quoted 9 lines
Quoted 5 lines

Also we need to decide what to do with evictable notification marks. With the patches as is they are now never evicted. This makes sense because the main reason behind evictable marks was to avoid pinning the inode. On the other hand this *might* surprise some userspace - definitely it breaks couple of LTP tests we have for evictable marks.

Those tests were never super reliable, because as in real workloads, users can't usually know, short of unmount, if inode was really evicted.

Quoted 7 lines

What we can do going forward is use this currently inval flag combo #define FAN_MARK_EVICTABLE_INODE \
(FAN_MARK_EVICTABLE | FAN_MARK_MOUNT)

to opt-in for not pinning the inode without loosing the mark
as an explicit test for a kernel that supports this improvement, rather than users doing recursive inotify without knowing the consequences.

OK, I agree that if we have marks that no longer pin the inode, then adding a way for userspace to discover this capability is sensible. But let's solve that API question after we have the functionality merged.

Honza

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