Linux Security Modules development
 help / color / mirror / Atom feed
From: Huang Shijie <huangsj@hygon.cn>
To: <viro@zeniv.linux.org.uk>, <brauner@kernel.org>, <jack@suse.cz>,
	<hch@infradead.org>, <agruenba@redhat.com>, <mic@digikod.net>,
	<axboe@kernel.dk>
Cc: <gnoack@google.com>, <paul@paul-moore.com>, <jmorris@namei.org>,
	<serge@hallyn.com>, <legion@kernel.org>, <aleksa@amutable.com>,
	<libaokun@linux.alibaba.com>, <ebiggers@kernel.org>,
	<djwong@kernel.org>, <sandeen@redhat.com>, <zhongyuan@hygon.cn>,
	<fangbaoshun@hygon.cn>, <yingzhiwei@hygon.cn>,
	<wujianyong@hygon.cn>, <shijie8@gmail.com>,
	<linux-fsdevel@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-block@vger.kernel.org>, <gfs2@lists.linux.dev>,
	<linux-security-module@vger.kernel.org>,
	Huang Shijie <huangsj@hygon.cn>
Subject: [PATCH v3 10/11] landlock: convert hook_sb_delete() to super_iter_inodes()
Date: Fri, 20 Nov 2026 11:13:04 +0800	[thread overview]
Message-ID: <20261120031305.3561045-11-huangsj@hygon.cn> (raw)
In-Reply-To: <20261120031305.3561045-1-huangsj@hygon.cn>

Convert hook_sb_delete() to super_iter_inodes().
No functional change.

Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
 security/landlock/fs.c | 138 ++++++++++++++++-------------------------
 1 file changed, 52 insertions(+), 86 deletions(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 2ab67bcad198..dae606892d96 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -1380,105 +1380,71 @@ static void hook_inode_free_security_rcu(void *inode_security)
  *
  * Cf. fsnotify_unmount_inodes() and evict_inodes()
  */
-static void hook_sb_delete(struct super_block *const sb)
+static bool release_inode_eligible(struct inode *inode, void *priv)
 {
-	struct inode *inode, *prev_inode = NULL;
+	bool ret;
 
-	if (!landlock_initialized)
-		return;
+	rcu_read_lock();
+	ret = !!rcu_dereference(landlock_inode(inode)->object);
+	rcu_read_unlock();
 
-	spin_lock(&sb->s_inode_list_lock);
-	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
-		struct landlock_object *object;
+	return ret;
+}
+
+static int release_inode_fn(struct inode *inode, void *priv)
+{
+	struct landlock_object *object;
+
+	rcu_read_lock();
+	object = rcu_dereference(landlock_inode(inode)->object);
+	if (!object) {
+		rcu_read_unlock();
+		return INO_ITER_RET_NEXT;
+	}
 
-		/* Only handles referenced inodes. */
-		if (!icount_read_once(inode))
-			continue;
+	/*
+	 * If there is no concurrent release_inode() ongoing, then we
+	 * are in charge of calling iput() on this inode, otherwise we
+	 * will just wait for it to finish.
+	 */
+	spin_lock(&object->lock);
+	if (object->underobj == inode) {
+		object->underobj = NULL;
+		spin_unlock(&object->lock);
+		rcu_read_unlock();
 
 		/*
-		 * Protects against concurrent modification of inode (e.g.
-		 * from get_inode_object()).
+		 * Because object->underobj was not NULL,
+		 * release_inode() and get_inode_object() guarantee
+		 * that it is safe to reset
+		 * landlock_inode(inode)->object while it is not NULL.
+		 * It is therefore not necessary to lock inode->i_lock.
 		 */
-		spin_lock(&inode->i_lock);
+		rcu_assign_pointer(landlock_inode(inode)->object, NULL);
 		/*
-		 * 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.
+		 * At this point, we own the ihold() reference that was
+		 * originally set up by get_inode_object() and the
+		 * __iget() reference that we just set in this loop
+		 * walk.  Therefore there are at least two references
+		 * on the inode.
 		 */
-		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);
+		iput_not_last(inode);
+	} else {
+		spin_unlock(&object->lock);
+		rcu_read_unlock();
+	}
 
-		/*
-		 * If there is no concurrent release_inode() ongoing, then we
-		 * are in charge of calling iput() on this inode, otherwise we
-		 * will just wait for it to finish.
-		 */
-		spin_lock(&object->lock);
-		if (object->underobj == inode) {
-			object->underobj = NULL;
-			spin_unlock(&object->lock);
-			rcu_read_unlock();
+	return INO_ITER_RET_NEXT;
+}
 
-			/*
-			 * Because object->underobj was not NULL,
-			 * release_inode() and get_inode_object() guarantee
-			 * that it is safe to reset
-			 * landlock_inode(inode)->object while it is not NULL.
-			 * It is therefore not necessary to lock inode->i_lock.
-			 */
-			rcu_assign_pointer(landlock_inode(inode)->object, NULL);
-			/*
-			 * At this point, we own the ihold() reference that was
-			 * originally set up by get_inode_object() and the
-			 * __iget() reference that we just set in this loop
-			 * walk.  Therefore there are at least two references
-			 * on the inode.
-			 */
-			iput_not_last(inode);
-		} else {
-			spin_unlock(&object->lock);
-			rcu_read_unlock();
-		}
+static void hook_sb_delete(struct super_block *const sb)
+{
+	if (!landlock_initialized)
+		return;
 
-		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;
-	}
-	spin_unlock(&sb->s_inode_list_lock);
+	super_iter_inodes(sb, release_inode_fn, release_inode_eligible,
+			  NULL, INO_ITER_F_REFERENCED);
 
-	/* 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



  parent reply	other threads:[~2026-11-20  3:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
2026-11-20  3:12 ` [PATCH v3 01/11] vfs: add super_iter_inodes() Huang Shijie
2026-11-20 14:56   ` Andreas Gruenbacher
2026-11-20  3:12 ` [PATCH v3 02/11] vfs: add super_iter_inodes_locked() Huang Shijie
2026-11-20  3:12 ` [PATCH v3 03/11] fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked() Huang Shijie
2026-11-20  3:12 ` [PATCH v3 04/11] block: convert nr_blockdev_pages() " Huang Shijie
2026-11-20  3:12 ` [PATCH v3 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
2026-11-20  3:13 ` [PATCH v3 06/11] fs/inode: convert evict_inodes() to super_iter_inodes() Huang Shijie
2026-11-20  3:13 ` [PATCH v3 07/11] block: convert sync_bdevs() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
2026-11-20  3:13 ` Huang Shijie [this message]
2026-11-20  3:13 ` [PATCH v3 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
2026-11-20 14:59 ` [PATCH v3 0/11] vfs: add inode iteration methods Andreas Gruenbacher
2026-11-23  2:31   ` Huang Shijie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120031305.3561045-11-huangsj@hygon.cn \
    --to=huangsj@hygon.cn \
    --cc=agruenba@redhat.com \
    --cc=aleksa@amutable.com \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=djwong@kernel.org \
    --cc=ebiggers@kernel.org \
    --cc=fangbaoshun@hygon.cn \
    --cc=gfs2@lists.linux.dev \
    --cc=gnoack@google.com \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=jmorris@namei.org \
    --cc=legion@kernel.org \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=paul@paul-moore.com \
    --cc=sandeen@redhat.com \
    --cc=serge@hallyn.com \
    --cc=shijie8@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wujianyong@hygon.cn \
    --cc=yingzhiwei@hygon.cn \
    --cc=zhongyuan@hygon.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox