vfs: add non-blocking mode for function find_inode_nowait()
Subsystems (4)
PATCH v2 1/2 vfs: add non-blocking mode for function find_inode_nowait()
Currently concurrent inode lookup by number does not scale well because inode hash table is protected with single spinlock. Someday inode hash will become searchable under RCU (see linked patchset by David Howells).
For now main user of this function: ext4_update_other_inodes_time() could live with optimistic non-blocking try-lock/try-find semantics.
--- fs/ext4/inode.c | 2 +- fs/f2fs/node.c | 2 +- fs/inode.c | 9 +++++++-- include/linux/fs.h | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-)
fs/ext4/inode.c +1 -1
@@ -4865,7 +4865,7 @@ static void ext4_update_other_inodes_time(struct super_block *sb,if (ino == orig_ino)continue ; oi.raw_inode = (struct ext4_inode *) buf; - (void )find_inode_nowait (sb, ino, other_inode_match, &oi); + (void )find_inode_nowait (sb, ino, other_inode_match, &oi, false); } }
fs/f2fs/node.c +1 -1
@@ -1793,7 +1793,7 @@ static bool flush_dirty_inode(struct page *page)struct inode *inode;nid_t ino =ino_of_node (page); - inode =find_inode_nowait (sbi->sb, ino, f2fs_match_ino,NULL ); + inode =find_inode_nowait (sbi->sb, ino, f2fs_match_ino,NULL , false);if (!inode)return false;
fs/inode.c +7 -2
@@ -1411,6 +1411,7 @@ EXPORT_SYMBOL(ilookup);* @hashval: hash value (usually inode number) to search for * @match: callback used for comparisons between inodes * @data: opaque data pointer to pass to @match +* @nonblock: if true do not wait for lock and fail with EAGAIN * * Search for the inode specified by @hashval and @data in the inode * cache, where the helper function @match will return 0 if the inode
@@ -1432,13 +1433,17 @@ struct inode *find_inode_nowait(struct super_block *sb,unsigned long hashval, int (*match)(struct inode *, unsigned long,void *), - void *data) + void *data, bool nonblock) { struct hlist_head *head = inode_hashtable + hash(sb, hashval);struct inode *inode, *ret_inode =NULL ;int mval; -spin_lock (&inode_hash_lock); +if (!nonblock) +spin_lock (&inode_hash_lock); +else if (!spin_trylock (&inode_hash_lock)) +return ERR_PTR (-EAGAIN ); +hlist_for_each_entry (inode, head, i_hash) {if (inode->i_sb != sb)continue ;
include/linux/fs.h +1 -1
@@ -3039,7 +3039,7 @@ extern struct inode *find_inode_nowait(struct super_block *, unsigned long,int (*match)(struct inode *,unsigned long ,void *), - void *data); + void *data, bool nonblock);extern int insert_inode_locked4 (struct inode *,unsigned long ,int (*test)(struct inode *,void *),void *);extern int insert_inode_locked (struct inode *);#ifdef CONFIG_DEBUG_LOCK_ALLOC
PATCH v2 2/2 ext4: avoid spinlock congestion in lazytime optimization
Function ext4_update_other_inodes_time() implements optimization which opportunistically updates times for inodes within same inode table block.
For now concurrent inode lookup by number does not scale well because inode hash table is protected with single spinlock. It could become very hot at concurrent writes to fast nvme when inode cache has enough inodes.
Let's use here non-blocking variant of function find_inode_nowait() and skip opportunistic inode updates if spinlock is congested.
Synthetic testcase by Dmitry Monakhov:
modprobe brd rd_size=10240000 rd_nr=1
mkfs.ext4 -F -I 256 -b 4096 -q /dev/ram0
mkdir -p m
mount /dev/ram0 m -o lazytime,noload,barrier=0
mkdir m/{0..31}
fio --ioengine=ftruncate --direct=1 --bs=4k --filesize=16m --time_based=1 \
--runtime=10 --numjobs=32 --group_reporting --norandommap --name=write-32 \
--rw=randwrite --directory=m --filename_format='$jobnum/t' umount m
rmdir m
rmmod brd
Before patch: 50k op/s
After patch: 3000k op/s
--- fs/ext4/inode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext4/inode.c +5 -1
@@ -4853,6 +4853,7 @@ static void ext4_update_other_inodes_time(struct super_block *sb,unsigned long ino;int i, inodes_per_block =EXT4_SB (sb)->s_inodes_per_block;int inode_size =EXT4_INODE_SIZE (sb); +struct inode *res; oi.orig_ino = orig_ino;/*
@@ -4865,7 +4866,10 @@ static void ext4_update_other_inodes_time(struct super_block *sb,if (ino == orig_ino)continue ; oi.raw_inode = (struct ext4_inode *) buf; - (void )find_inode_nowait (sb, ino, other_inode_match, &oi, false); +/* Try to find inode, stop if inode_hash_lock is congested. */ + res =find_inode_nowait (sb, ino, other_inode_match, &oi, true); +if (res ==ERR_PTR (-EAGAIN )) +break ; } }
David Howells
maintainerQuoted 3 lines · Konstantin Khlebnikov
Currently concurrent inode lookup by number does not scale well because inode hash table is protected with single spinlock. Someday inode hash will become searchable under RCU (see linked patchset by David Howells).
Something like this?
David
---
vfs: Make the inode hash table RCU searchable
Make the inode hash table RCU searchable so that searches that want to access or modify an inode without taking a ref on that inode can do so without taking the inode hash table lock.
The main thing this requires is some RCU annotation on the list manipulation operations. Inodes are already freed by RCU in most cases.
Users of this interface must take care as the inode may be still under construction or may be being torn down around them.
There are at least two instances where this can be of use:
(1) Ext4 date stamp updating.
(2) AFS callback breaking.
--- fs/afs/callback.c | 12 ++- fs/ext4/inode.c | 44 +++++------ fs/inode.c | 209 ++++++++++++++++++++++++++++++++++------------------- include/linux/fs.h | 8 -- 4 files changed, 172 insertions(+), 101 deletions(-)
fs/afs/callback.c +9 -3
@@ -252,6 +252,7 @@ static void afs_break_one_callback(struct afs_server *server,struct afs_vnode *vnode;struct inode *inode; +rcu_read_lock ();read_lock (&server->cb_break_lock);hlist_for_each_entry (vi, &server->cb_volumes, srv_link) {if (vi->vid < fid->vid)
@@ -287,12 +288,16 @@ static void afs_break_one_callback(struct afs_server *server, }else { data.volume =NULL ; data.fid = *fid; - inode =ilookup5_nowait (cbi->sb, fid->vnode, - afs_iget5_test, &data); + +/* See if we can find a matching inode - even an I_NEW +* inode needs to be marked as it can have its callback +* broken before we finish setting up the local inode. +*/ + inode =find_inode_rcu (cbi->sb, fid->vnode, + afs_iget5_test, &data);if (inode) { vnode =AFS_FS_I (inode);afs_break_callback (vnode, afs_cb_break_for_callback); -iput (inode); }else {trace_afs_cb_miss (fid, afs_cb_break_for_callback); }
@@ -301,6 +306,7 @@ static void afs_break_one_callback(struct afs_server *server, out:read_unlock (&server->cb_break_lock); +rcu_read_unlock (); }/*
fs/ext4/inode.c +22 -22
83 lines not on this page · raw message
fs/inode.c +138 -71
306 lines not on this page · raw message
include/linux/fs.h +3 -5
@@ -3057,11 +3057,9 @@ extern struct inode *inode_insert5(struct inode *inode, unsigned long hashval, void *data);extern struct inode *iget5_locked (struct super_block *,unsigned long ,int (*test)(struct inode *,void *),int (*set)(struct inode *,void *),void *);extern struct inode *iget_locked (struct super_block *,unsigned long ); -extern struct inode *find_inode_nowait (struct super_block *, -unsigned long , -int (*match)(struct inode *, -unsigned long ,void *), -void *data); +extern struct inode *find_inode_rcu (struct super_block *,unsigned long , +int (*)(struct inode *,void *),void *); +extern struct inode *find_inode_by_ino_rcu (struct super_block *,unsigned long );extern int insert_inode_locked4 (struct inode *,unsigned long ,int (*test)(struct inode *,void *),void *);extern int insert_inode_locked (struct inode *);#ifdef CONFIG_DEBUG_LOCK_ALLOC