[PATCH v4 10/11] landlock: convert hook_sb_delete() to sb_inode_iter

From: Huang Shijie

Date: Wed Nov 25 2026 - 04:13:32 EST


Convert hook_sb_delete() to sb_inode_iter.
No functional change.

Signed-off-by: Huang Shijie <huangsj@xxxxxxxx>
---
security/landlock/fs.c | 67 ++++++++++--------------------------------
1 file changed, 16 insertions(+), 51 deletions(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 2ab67bcad198..36e22be99acc 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -1380,49 +1380,35 @@ static void hook_inode_free_security_rcu(void *inode_security)
*
* Cf. fsnotify_unmount_inodes() and evict_inodes()
*/
+static bool release_inode_eligible(struct inode *inode, void *priv)
+{
+ bool ret;
+
+ rcu_read_lock();
+ ret = !!rcu_dereference(landlock_inode(inode)->object);
+ rcu_read_unlock();
+
+ return ret;
+}
+
static void hook_sb_delete(struct super_block *const sb)
{
- struct inode *inode, *prev_inode = NULL;
+ struct inode *inode;

if (!landlock_initialized)
return;

- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ CLASS(sb_inode_iter, iter)(sb, release_inode_eligible, NULL,
+ INO_ITER_F_REFERENCED);
+ while ((inode = sb_inode_iter_next(&iter))) {
struct landlock_object *object;

- /* Only handles referenced inodes. */
- if (!icount_read_once(inode))
- continue;
-
- /*
- * Protects against concurrent modification of inode (e.g.
- * from get_inode_object()).
- */
- spin_lock(&inode->i_lock);
- /*
- * Checks I_FREEING and I_WILL_FREE to protect against a race
- * condition when release_inode() just called iput(), which
- * could lead to a NULL dereference of inode->security or a
- * second call to iput() for the same Landlock object. Also
- * checks I_NEW because such inode cannot be tied to an object.
- */
- if (inode_state_read(inode) &
- (I_FREEING | I_WILL_FREE | I_NEW)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
-
rcu_read_lock();
object = rcu_dereference(landlock_inode(inode)->object);
if (!object) {
rcu_read_unlock();
- spin_unlock(&inode->i_lock);
continue;
}
- /* Keeps a reference to this inode until the next loop walk. */
- __iget(inode);
- spin_unlock(&inode->i_lock);

/*
* If there is no concurrent release_inode() ongoing, then we
@@ -1455,30 +1441,9 @@ static void hook_sb_delete(struct super_block *const sb)
spin_unlock(&object->lock);
rcu_read_unlock();
}
-
- if (prev_inode) {
- /*
- * At this point, we still own the __iget() reference
- * that we just set in this loop walk. Therefore we
- * can drop the list lock and know that the inode won't
- * disappear from under us until the next loop walk.
- */
- spin_unlock(&sb->s_inode_list_lock);
- /*
- * We can now actually put the inode reference from the
- * previous loop walk, which is not needed anymore.
- */
- iput(prev_inode);
- cond_resched();
- spin_lock(&sb->s_inode_list_lock);
- }
- prev_inode = inode;
+ cond_resched();
}
- spin_unlock(&sb->s_inode_list_lock);

- /* Puts the inode reference from the last loop walk, if any. */
- if (prev_inode)
- iput(prev_inode);
/* Waits for pending iput() in release_inode(). */
wait_var_event(&landlock_superblock(sb)->inode_refs,
!atomic_long_read(&landlock_superblock(sb)->inode_refs));
--
2.53.0