From nobody Sat Jul 25 20:47:10 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3F872E62AC for ; Tue, 14 Jul 2026 01:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783991699; cv=none; b=o+Ujkp0nx4LBCP6jlYx8b+d9NuMkFdaLiZrSYXNqJ2i4p2/kR2f6hXVOQNgTdN8WS4b/tePexV3/2X8XQbG14FYsXE1wlDiN06NIRMHWKudeSjdE4e4PQLTcSZkeE1hS2DqivU+aBNpJFT6ep4NiJSn4LjTWZDwSGFR4dlVeeH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783991699; c=relaxed/simple; bh=1XJxmXwfqQafTIytUX2Z6Lul7wZpxa519y93Qpuhxxk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=XRUvON7L32vbodKf/HV9gFc8QesKn9OBCDf3Daf4KZ7ZpyKAmYiisTbqZ6iybZN+hkgamwm+vtC1MZLJIsPm5ZwuQZbWim68p6kbcdt90c+Qvgcp6vjgIjzi/yVxJYXa8Fyc7Rj5KYWLJWdpHIVDb8cP4jumdCBjOESdwPsnIJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=oAelxqIG; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oAelxqIG" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2cc73e322dbso42974115ad.1 for ; Mon, 13 Jul 2026 18:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783991697; x=1784596497; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=W/hLy+UxJhCgbMBZlTiYLrb+eLGW2reQNpf7vQdSCic=; b=oAelxqIGk2wcP16+36JVIhIZ8dIU5t2qMKYq1SZ7Lr4ZqjwSK5tt7YtFw6/IvFWRiG TaXuJjLV9HkuyeJwF3woANKdTDo2vcuI5b+do+6E+OUvCvhoZSD3DK8Bne2Y8qCmnegs qSl1sz9dq4ZgqXfwSkGh6trm8oYTVm+ORDA9AIGtpWl5aOnVR4lc0rSqAxUTgvuH9Nz0 ieJuyNOVpSprQyt704sIQBDArrKawfdgaNmOe8FLH7TPqHMwJhV3mcgUKJBlajRflthr YZVAK/vE1eeC7me8aga80bzi5BddxjF61FvW9Y4XNupThBnmI5wBUgfPCN3q92SWSP/q 2S3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783991697; x=1784596497; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=W/hLy+UxJhCgbMBZlTiYLrb+eLGW2reQNpf7vQdSCic=; b=jhbIjdFvYCUlJFc3NkYhpM+HAzy7X6uV2co1fRaqXL9rHSfkt9nh7oRyNH5YROs9Xz lKRcRofIQ5vF+NW4foHleiW68aknjx/5nJxj0UqR7qonXI+xm4Khg+vSQWT/WDs5jipQ rSNt22rvhLU8HOBGPJLY/jECoqDx3JLj7ngxJs9uVFeSvUA+fK+nH4fXLabSY4a2eZwj CQQnX22EOyW5rN0pyx9ACHFy6ImVewPxnU+gKJwSU+IxyAUZgJi8oxceWQVfwBw6htzg yuap08FCDZtA9YDIPbodslXNa8QOc1zk06M44u6NBNrvWAGryBVl7Pkol8qyrWgu9A3z YHzg== X-Forwarded-Encrypted: i=1; AHgh+RqBgrheqhpq5YC66UgLgX8wmyWA9JkN2kwjMn8eaB6L5qt+/yUWQ2OAiq1o3HseC4K3ga/Cq0QuRAdWnsM=@vger.kernel.org X-Gm-Message-State: AOJu0Yxyn7bkGruSMccJafLTMaUBq2oF1XCJBkxy3ZI3EPyPW0PUyrmv xjvvM82TunfRaHnTVJvR+dwYdCUeH/370dzzsNktp+WZXWEekZgZ0WSL X-Gm-Gg: AfdE7cnZeRUU+x8I0SztxtTi91nc/qSuoSqgDPXq3DNx/lZOF2C2MtkXjAXsNheH/0o mo6YRkrDFWX0bxi0leaaCfA5EaplsGJL2tZ/T/+pYjVptrvsAxTOj/PE1iJK97rqz5JsYZO1iQr j4GGwynQmK8ZDDYEqy3OdHpj0l5H3NMD6YehVTpTqSRYQvBVyv6/d1cHSpVro7keQi5BDiUBsxD DXXeEVDg2dNPYeRYEQqlQzrtNkAUFtqoWnlhffwfca7545d2BuMJ2nBQj2VXDyrXb4ymGZUvUdu qK6hGpG7hLyqrSbbbzK3UvcNGstssYB/FDQJVmsodWvBDsUcdoCTY4ThNdhog/vv6oLhMUc9DdO SBJv/uqT+44nyn5E7SV8fL05bVb/EEshBmClNNVh6nKDQcHatzqyxLndRoTc5XO5q3YA/EJMV27 5+9+5MsYHzQu1S221H1hn7qw== X-Received: by 2002:a17:902:c403:b0:2c0:e5ee:f55e with SMTP id d9443c01a7336-2ce9e7aa136mr108076075ad.7.1783991697251; Mon, 13 Jul 2026 18:14:57 -0700 (PDT) Received: from [127.0.1.1] ([138.199.21.246]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2cee5ccb56esm8315355ad.84.2026.07.13.18.14.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 18:14:56 -0700 (PDT) From: Jing Wu Date: Tue, 14 Jul 2026 09:14:54 +0800 Subject: [PATCH] fs/inode: replace goto-again rescan with cursor in evict_inodes() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260714-feat-fs-evict-inodes-cursor-v1-1-64818f31020e@gmail.com> X-B4-Tracking: v=1; b=H4sIAI2NVWoC/x3MwQqDMAyA4VeRnA3YFjb0VYYHl6Yul3YkVYTiu 6/s+MHP38BYhQ2WoYHyKSYld7hxAPpseWeU2A1+8o/p6QIm3iomw95SRcklsiEdakWRfKD5HQM 5l6AfvspJrv/9td73D0kPkpZtAAAA To: Alexander Viro , Christian Brauner , Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Qiliang Yuan , Jing Wu X-Mailer: b4 0.13.0 evict_inodes() traverses sb->s_inodes to reclaim all zero-refcount inodes at unmount time. When the traversal yields to the scheduler via cond_resched(), the current code jumps back to a "goto again" label and restarts the list_for_each_entry loop from the head. Inodes already marked I_FREEING are skipped cheaply, but the repeated scans make the total work O(n * r) where r is the number of reschedule events -- proportional to O(n^2) in the worst case on systems with many inodes and memory pressure. Replace the goto-again pattern with a sentinel cursor node embedded in sb->s_inodes. list_move() advances the cursor past each visited inode in O(1). Dropping and reacquiring s_inode_list_lock around cond_resched() leaves the cursor in place, so the loop resumes from exactly the current position rather than the list head, giving O(n) total traversal. Co-developed-by: Qiliang Yuan Signed-off-by: Qiliang Yuan Signed-off-by: Jing Wu --- evict_inodes() is called at unmount time to reclaim all zero-refcount inodes on a superblock. Its current implementation uses a "goto again" pattern: after dropping s_inode_list_lock for cond_resched() and dispose_list(), it restarts list_for_each_entry from the list head. In the common case (clean unmount, all files closed) the goto loop terminates quickly because dispose_list() removes evicted inodes before the restart. However, when many inodes have a positive refcount and cannot be evicted -- as happens with a force-unmount while file descriptors are still open -- the restarts scan O(n) skippable inodes each time, making total work O(n * r) proportional to O(n^2) in the worst case. This series replaces the goto-again pattern with a sentinel cursor node embedded in sb->s_inodes, allowing the traversal to resume from its current position after each lock drop. --- fs/inode.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index acf206beb2e03..19197368c6a67 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -880,15 +880,31 @@ static void dispose_list(struct list_head *head) * called by superblock shutdown after having SB_ACTIVE flag removed, * so any inode reaching zero refcount during or after that call will * be immediately evicted. + * + * Use a cursor node embedded in sb->s_inodes to resume traversal after + * dropping s_inode_list_lock for cond_resched() + dispose_list(). This + * avoids restarting the scan from the list head on each reschedule, giving + * O(n) total traversal instead of O(n * r) where r is the reschedule coun= t. */ void evict_inodes(struct super_block *sb) { struct inode *inode; LIST_HEAD(dispose); + /* + * Embed a cursor node directly in sb->s_inodes. list_move() advances + * it past each visited inode in O(1), so the loop resumes from exactly + * the current position after lock drop rather than from the list head. + */ + struct list_head cursor; =20 -again: spin_lock(&sb->s_inode_list_lock); - list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { + list_add(&cursor, &sb->s_inodes); + + while (cursor.next !=3D &sb->s_inodes) { + inode =3D list_entry(cursor.next, struct inode, i_sb_list); + /* Leave the cursor immediately after the current inode. */ + list_move(&cursor, &inode->i_sb_list); + if (icount_read_once(inode)) continue; =20 @@ -916,9 +932,11 @@ void evict_inodes(struct super_block *sb) spin_unlock(&sb->s_inode_list_lock); cond_resched(); dispose_list(&dispose); - goto again; + spin_lock(&sb->s_inode_list_lock); } } + + list_del(&cursor); spin_unlock(&sb->s_inode_list_lock); =20 dispose_list(&dispose); --- base-commit: 502d801f0ab03e4f32f9a33d203154ce84887921 change-id: 20260713-feat-fs-evict-inodes-cursor-c23c9bd3c11f Best regards, --=20 Jing Wu