Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796135737; b=BUJ5vxj5Zwdhu/fl1YsRtEKgJWptLSp8Iq49aHutz223mdQwb/NIjKiWy+04LpsAei6H 2Nc5IVKfZacTgB7o43zzxe62jPiI9Vii66ulzChgammwj1km4/stbhh2TyYUviIQWF5yk bHiIfmu+unKIVovqcN8XLPHTnYno+6eyZEk9Png04HHN8lpa6T6qDlngwAAMKZGT4XY75 4Jq+zMBAJm3KWwCMku7LSek1a+xacj5yDfEC/7ubmJZpm5HtcOTVc7Da+sGuKa6NWF3z5 sD1tRL6peJoeBh8PpEzQ5OoPFybI7Ndehgh3mFWIOVi5VaLIGwrkmxGmpyncSc76h3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796135737; h=DMARC-Filter:Received:DKIM-Signature:DKIM-Signature:Received: Received:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-Developer-Signature: X-Developer-Key:Content-Transfer-Encoding:X-Rspamd-Queue-Id: X-Rspamd-Pre-Result:X-Rspamd-Action:X-Rspamd-Server:X-Spam-Level: X-Spamd-Result:X-Spam-Flag:X-Spam-Score; bh=lCniuLzwPt0jugtptKEV2OTpeHYjyJTq6UEP2KgLqFc=; b=Dcs/rICOew+99StzQS+I0mp7sHLsy5p8hw+y1ireKzXDWOxp0ZH3xsy70op8sqBP59X6 jgNvoctUqqZU9rtAe7Sj3mmxPWASHUuawRoFGdaRXW6+FQV/Zv1S84wnLazFxepwGtkan futFqujX1PxpLLCbAwoeQlVK10HiQcgy877MGia2cAG+rULO+nxtCnczpVZ0hL8g0WFIy 0vRcUFsuP8D2A77taGoXL7jARPjdTX5tHKBeDiEZhfU2mSvLK68mFyWzJq64SYKf2Y54o CuwP464sP3xi12vQxK1ArnVkzcSYspHBhgntpq4nfEM/b8aykmw9oHaq9o+LW5nm8fQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=S6FFh9yv; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b="Iw/83ThU"; arc=none smtp.remote-ip=195.135.223.130 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B26F9606A33A for ; Tue, 1 Dec 2026 14:35:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B26F9606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=S6FFh9yv; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Iw/83ThU Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 688C0222AC; Tue, 1 Dec 2026 14:35:34 +0000 (UTC) Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1796135734; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lCniuLzwPt0jugtptKEV2OTpeHYjyJTq6UEP2KgLqFc=; b=S6FFh9yviKHl3+fg+TKm3OPnlwSh0CX5y2NcsQ4vfmpML/Cj2czsx7LiKEPR3H8XBkDWJM HRhX7z8rOm6YQyImSuIR+fU3lR9mTp5pDJpfJo9vObSLrnS7mpv8wYTXnvIffZuPbZFs/3 ufwExJI7oG0BSS/7eQ4HN6a4/SwaFL8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1796135734; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lCniuLzwPt0jugtptKEV2OTpeHYjyJTq6UEP2KgLqFc=; b=Iw/83ThUFzBdSS7M1L0mvrnojjL13O7dciYGfaw9ZhcfZoNol1LwJ/jGGc8tZgWU1RDDiZ jnZR4XRwklVrB5Ag== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id CF7FE136E4; Tue, 1 Dec 2026 14:35:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id xoM3LDXbDmv8VAAAD6G6ig (envelope-from ); Tue, 01 Dec 2026 14:35:33 +0000 Received: by quack.suse.cz (Postfix, from userid 1000) id 1866951543; Tue, 01 Dec 2026 15:35:33 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , Christoph Hellwig , , Mikhail Rudenko , Andreas Gruenbacher , Jan Kara Subject: [PATCH v5 4/8] fs: Basic infrastructure for offloading inode reclaim Date: Tue, 1 Dec 2026 15:35:06 +0100 Message-ID: <20261201143522.483168-12-jack@suse.cz> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201105733.18997-1-jack@suse.cz> References: <20261201105733.18997-1-jack@suse.cz> X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11416; i=jack@suse.cz; h=from:subject; bh=ga/176AYJYs4reL3U2O2+qmrIxOUIUrDjhVGLBI96gc=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBrDtsteIPdEBtxHGqniMmPIlJrK+072ru0naX/K 4wY04ar1vuJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCaw7bLQAKCRCcnaoHP2RA 2SYTCADWALSzNkLs5KsFhE33uWOrOUJb9nnRu0tP7mFsYhgxvZpJ6YLcs5Djbc2/cKKNm7zgbxT DtxQupPAbcONTXUXmSX94NG+qcf8efRc3Sj0kDuKvdkla87hgek+LOdzE7A+dK5fPGYSMt41DhA SZLGaE5EOnCBJSBh5wGDBuGMkWzr4xvHMa3CZXsgCyKCKAO8cwd9KDyrZ1B3fTWHyN45pROWILI TpcVbRjZBLEYtfQnwXVec5DMyz856HmZ+cEdPL8xstX/2ThHT5Pa3Mo17+5+HgiBhZFlzhyDLi7 qmxs3htgSYZrIvrnUERPZKk2bQL7JZvjIfatrd22e/f4zITq X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 688C0222AC X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Flag: NO X-Spam-Score: -4.00 Reclaim of some inodes is rather complex requiring running transactions or doing other IO. Consequently filesystems end up doing GFP_NOFAIL allocations from kswapd or even direct reclaim which is problematic because forward progress of these allocations isn't guaranteed. Add infrastructure for marking inodes whose reclaim is difficult and offload reclaim of such inodes into a workqueue to not block kswapd with difficult inode reclaim. The processing of the queued inodes happens in parallel from several work items so that the reclaim has higher chances of keeping up with the inflow of inodes if reclaim of some inodes progresses slowly. Signed-off-by: Jan Kara --- fs/inode.c | 216 ++++++++++++++++++++++++++++++- fs/super.c | 2 + include/linux/fs.h | 3 + include/linux/fs/super_types.h | 3 + include/trace/events/writeback.h | 3 +- 5 files changed, 223 insertions(+), 4 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index a9f757fba23b..5713e18229e0 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -74,6 +74,25 @@ static DEFINE_PER_CPU(unsigned long, nr_unused); static struct kmem_cache *inode_cachep __ro_after_init; +struct deferred_reclaim_queue { + struct list_head list; + spinlock_t lock; +}; + +/* Inodes for deferred reclaim */ +struct inode_deferred_reclaim { + int workers; /* Number of parallel workers */ + struct workqueue_struct *wq; /* Workqueue for workers */ + struct work_struct *work; /* Workers */ + /* + * Queue of deferred inodes. The number of queues is the same number + * as the number of workers + */ + struct deferred_reclaim_queue *queues; +}; + +static struct inode_deferred_reclaim deferred_reclaim; + static long get_nr_inodes(void) { int i; @@ -965,7 +984,7 @@ static enum lru_status inode_lru_isolate(struct list_head *item, */ if (icount_read(inode) || (inode_state_read(inode) & - ~(I_REFERENCED | I_METADATA_WRITEBACK)) || + ~(I_REFERENCED | I_METADATA_WRITEBACK | I_DEFER_RECLAIM)) || !mapping_shrinkable(&inode->i_data)) { list_lru_isolate(lru, &inode->i_lru); spin_unlock(&inode->i_lock); @@ -1016,6 +1035,39 @@ static enum lru_status inode_lru_isolate(struct list_head *item, return LRU_REMOVED; } +/* Number of inodes to process in one reclaim batch */ +#define INODE_RECLAIM_BATCH_SIZE 16 + +static int get_deferred_sb_id(struct super_block *sb) +{ + return hash_ptr(sb, 32) % deferred_reclaim.workers; +} + +static void queue_deferred_reclaim(struct super_block *sb, int count, + struct list_head *list) +{ + int id = get_deferred_sb_id(sb); + struct deferred_reclaim_queue *queue = + &deferred_reclaim.queues[id]; + int i, wake_count; + + spin_lock(&queue->lock); + list_splice_tail(list, &queue->list); + atomic_add(count, &sb->s_deferred_reclaim_count); + spin_unlock(&queue->lock); + + /* Make sure enough workers are woken for queued inodes of this sb */ + wake_count = DIV_ROUND_UP(atomic_read(&sb->s_deferred_reclaim_count), + INODE_RECLAIM_BATCH_SIZE); + if (wake_count > deferred_reclaim.workers) + wake_count = deferred_reclaim.workers; + for (i = 0; i < wake_count; i++) { + queue_work(deferred_reclaim.wq, + &deferred_reclaim.work[id]); + id = (id + 1) % deferred_reclaim.workers; + } +} + /* * Walk the superblock inode LRU for freeable inodes and attempt to free them. * This is called from the superblock shrinker function with a number of inodes @@ -1025,14 +1077,169 @@ static enum lru_status inode_lru_isolate(struct list_head *item, long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) { LIST_HEAD(freeable); - long freed; + long freed, deferred = 0; + struct inode *inode, *tmp; freed = list_lru_shrink_walk(&sb->s_inode_lru, sc, inode_lru_isolate, &freeable); - dispose_list(&freeable); + + list_for_each_entry_safe(inode, tmp, &freeable, i_lru) { + if (inode_state_read_once(inode) & I_DEFER_RECLAIM) { + deferred++; + continue; + } + list_del_init(&inode->i_lru); + evict(inode); + cond_resched(); + } + + if (!list_empty(&freeable)) + queue_deferred_reclaim(sb, deferred, &freeable); return freed; } +static void __get_inode_reclaim_batch(int *start, int end, + struct list_head *list, int *count) +{ + int i; + + for (i = *start; i < end; i++) { + struct deferred_reclaim_queue *queue = + &deferred_reclaim.queues[i]; + struct list_head *entry, tmp; + + if (list_empty_careful(&queue->list)) + continue; + spin_lock(&queue->lock); + list_for_each(entry, &queue->list) { + if (*count >= INODE_RECLAIM_BATCH_SIZE) + break; + (*count)++; + } + list_cut_before(&tmp, &queue->list, entry); + list_splice_tail(&tmp, list); + spin_unlock(&queue->lock); + if (*count >= INODE_RECLAIM_BATCH_SIZE) + break; + } + *start = i; +} + +/* Move a batch of inodes from queued lists to our private list */ +static int get_inode_reclaim_batch(int *id, struct list_head *list) +{ + int count = 0; + int orig_id = *id; + + __get_inode_reclaim_batch(id, deferred_reclaim.workers, list, &count); + if (count >= INODE_RECLAIM_BATCH_SIZE) + return count; + /* Wrap around */ + if (orig_id > 0) { + *id = 0; + __get_inode_reclaim_batch(id, orig_id, list, &count); + } + return count; +} + + +static void inode_reclaim_deferred(struct work_struct *work) +{ + struct inode *inode; + LIST_HEAD(inode_batch); + /* + * We start with the list corresponding to the work but rolling a dice + * would work as well + */ + int id = work - deferred_reclaim.work; + + while (1) { + if (!get_inode_reclaim_batch(&id, &inode_batch)) + break; + while (!list_empty(&inode_batch)) { + struct super_block *sb; + + /* + * inode_batch list is private and I_FREEING flags + * protect us from anybody else trying to remove the + * inode from the LRU list. No locking needed. + */ + inode = list_first_entry(&inode_batch, struct inode, + i_lru); + list_del_init(&inode->i_lru); + sb = inode->i_sb; + evict(inode); + /* + * The elevated s_deferred_reclaim_count keeps sb alive + * until we drop it. Use RCU to keep sb alive until + * the wakeup is done. + */ + rcu_read_lock(); + if (atomic_dec_and_test(&sb->s_deferred_reclaim_count)) + wake_up_var(&sb->s_deferred_reclaim_count); + rcu_read_unlock(); + cond_resched(); + } + } +} + +/* Maximum number of inode reclaim workers */ +#define MAX_RECLAIM_WORKERS 16 + +static void __init deferred_reclaim_init(void) +{ + int workers = MAX_RECLAIM_WORKERS; + int i; + + /* Scale down the number of workers for small systems */ + if (workers > num_possible_cpus()) + workers = num_possible_cpus(); + + deferred_reclaim.workers = workers; + deferred_reclaim.work = kmalloc_objs(struct work_struct, workers); + deferred_reclaim.queues = + kmalloc_objs(struct deferred_reclaim_queue, workers); + if (!deferred_reclaim.work || !deferred_reclaim.queues) + panic("Failed to allocate deferred inode reclaim queues"); + + deferred_reclaim.wq = alloc_workqueue("deferred-inode-reclaim", + WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND, + workers); + if (!deferred_reclaim.wq) + panic("Failed to allocate deferred inode reclaim workqueue"); + + for (i = 0; i < workers; i++) { + INIT_WORK(&deferred_reclaim.work[i], inode_reclaim_deferred); + spin_lock_init(&deferred_reclaim.queues[i].lock); + INIT_LIST_HEAD(&deferred_reclaim.queues[i].list); + } +} + +/* + * Mark inode for deferred reclaim. When slab reclaim path will be freeing the + * inode, it will not call evict() directly but rather offload it to a worker + * in a workqueue. This is useful when inode reclaim is expected to take a + * long time (e.g. because it needs to do IO) so that it doesn't block kswapd + * or because it needs allocating memory (which cannot be guaranteed from + * reclaim path). + * + * Note that we currently don't provide a way to unmark inode for deferred + * reclaim. It could be done but the benefit is unclear. The rationale is that + * to make inode hard to reclaim, relatively expensive things like writing data + * to the file have to be done and in such case the additional CPU overhead and + * latency of offloading the inode reclaim is negligible in the overall load. + */ +void mark_inode_reclaim_deferred(struct inode *inode) +{ + if (inode_state_read_once(inode) & I_DEFER_RECLAIM) + return; + + spin_lock(&inode->i_lock); + inode_state_set(inode, I_DEFER_RECLAIM); + spin_unlock(&inode->i_lock); +} +EXPORT_SYMBOL_GPL(mark_inode_reclaim_deferred); + static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked, bool rcu_locked); static bool igrab_from_hash(struct inode *inode); @@ -2680,6 +2887,9 @@ void __init inode_init(void) SLAB_ACCOUNT), init_once); + /* Deferred inode reclaim infrastructure */ + deferred_reclaim_init(); + /* Hash may have been set up in inode_init_early */ if (!hashdist) return; diff --git a/fs/super.c b/fs/super.c index 4e68e7cac342..fa345d5027bf 100644 --- a/fs/super.c +++ b/fs/super.c @@ -652,6 +652,8 @@ void deactivate_locked_super(struct super_block *s) struct file_system_type *fs = s->s_type; if (atomic_dec_and_test(&s->s_active)) { shrinker_free(s->s_shrink); + wait_var_event(&s->s_deferred_reclaim_count, + !atomic_read(&s->s_deferred_reclaim_count)); fs->kill_sb(s); kill_super_notify(s); diff --git a/include/linux/fs.h b/include/linux/fs.h index 92a2841184f3..fc1a2cc9eb21 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -744,6 +744,7 @@ enum inode_state_flags_enum { I_SYNC_QUEUED = (1U << 17), I_PINNING_NETFS_WB = (1U << 18), I_METADATA_WRITEBACK = (1U << 19), + I_DEFER_RECLAIM = (1U << 20), }; #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC) @@ -2229,6 +2230,8 @@ static inline void set_inode_metadata_writeback(struct inode *inode) spin_unlock(&inode->i_lock); } +void mark_inode_reclaim_deferred(struct inode *inode); + /* * returns the refcount on the inode. it can change arbitrarily. */ diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h index 4454e3574b2c..fc8f7f2261e2 100644 --- a/include/linux/fs/super_types.h +++ b/include/linux/fs/super_types.h @@ -239,6 +239,9 @@ struct super_block { struct shrinker *s_shrink; /* per-sb shrinker handle */ + /* Number of inodes queued for deferred reclaim */ + atomic_t s_deferred_reclaim_count; + /* Number of inodes with nlink == 0 but still referenced */ atomic_long_t s_remove_count; diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h index b7dfc9f7bb61..c2a5b492a71a 100644 --- a/include/trace/events/writeback.h +++ b/include/trace/events/writeback.h @@ -28,7 +28,8 @@ {I_DONTCACHE, "I_DONTCACHE"}, \ {I_SYNC_QUEUED, "I_SYNC_QUEUED"}, \ {I_PINNING_NETFS_WB, "I_PINNING_NETFS_WB"}, \ - {I_LRU_ISOLATING, "I_LRU_ISOLATING"} \ + {I_LRU_ISOLATING, "I_LRU_ISOLATING"}, \ + {I_DEFER_RECLAIM, "I_DEFER_RECLAIM"} \ ) /* enums need to be exported to user space */ -- 2.53.0