Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796135737; b=4FY9dq/B47M+tZQig3QciUz9/avM2SpfcrxqUuW3R2EE0ZCVugGl+jsNn8jm/XzI0FKJ gEbf2rvntqnqHkSbPx2G1zsYN1zDPZq2QK6Jmk8TFkhEr+UgBAmh139fcfI3P3xciBQ4u +hUtHOOLEulWyFlp3q5Gk7JINTSijSPKTUgJHAqHdq1g6ISAMQvOzb2mi6EmOfW4OmVJx 34jm2LQL8THXVKzHcGJHyTIz2g7fcP4I7XxfqCDJciRnxpiBA5IY/F0aQPz1SQUJB/SZs uKoSZwC3VIDSwtuLWtwB0IO13KH4qh+tN9j3S/l5+H+ML9HeyAlG0nqemdRQTaOY8xQ== 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: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-Spam-Flag:X-Spam-Level:X-Spamd-Result:X-Spam-Score; bh=BirWYkTehAUqeERTOxeGbr/7Oy0d4FXIgyhMXDXyzb4=; b=KmQ6nZOW4XtwTWWdcaoaMtTmvvbH5Hfq4S3SyQqv/nYmMgVoRctgeavvGkfvkZxIx3sh AWr2Yd9sb4METEqBvLthwF5unLPVJpXMfJN2TIEFXm6U1uouSbL0ut9BxjvKpiqqBTDEA LgJsbOp3gjf4DUAgkdKHMLCANhXFDTlGQMFBXXiGJgfb1o2kEa0Ru8+sjNPKYuayeVi0V /uxnMxWbiT7HBytXYIB0CqZOMAuS/eBMda2MUdgK/5Yo4t5s1ahY82T4g1mfXjMqoenwI yVwGpEfjG+V37c/1maZBgN8usPSdlmk7RRx8KJAuGjHTzp2+PdOc65miB7i+tuIabng== 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=QKAuY6OR; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Yv2s9RJm; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=QKAuY6OR; dkim=neutral header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Yv2s9RJm; arc=none smtp.remote-ip=195.135.223.131 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 21501606A33D for ; Tue, 1 Dec 2026 14:35:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 21501606A33D 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=QKAuY6OR; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Yv2s9RJm; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=QKAuY6OR; dkim=neutral header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Yv2s9RJm Received: from imap1.dmz-prg2.suse.org (unknown [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-out2.suse.de (Postfix) with ESMTPS id 4C8421F78D; Tue, 1 Dec 2026 14:35:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1796135735; 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=BirWYkTehAUqeERTOxeGbr/7Oy0d4FXIgyhMXDXyzb4=; b=QKAuY6ORf+xgYLWOlVc+iAyJ1bohQmEROmexhT4IXvDiw130hJ74Jkz1qYidBYnE+vEzAw To1ka5ra09UE3Ckrsm89RBNdHT5hrrYqVn3oGpd7PL54LOZGFviA7gr4aOy9N+LDtckSDm oZu5aEx8KZupi4+VpLmfMrwnuQgne3w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1796135735; 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=BirWYkTehAUqeERTOxeGbr/7Oy0d4FXIgyhMXDXyzb4=; b=Yv2s9RJmxN8LzA62gIijSwSIGZFl/TkZDH0FHqMCQFrDnNsVRM6ZvvhxCGVSJ7QwHPFgk8 HbwZ1ihfxurYaWAw== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1796135735; 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=BirWYkTehAUqeERTOxeGbr/7Oy0d4FXIgyhMXDXyzb4=; b=QKAuY6ORf+xgYLWOlVc+iAyJ1bohQmEROmexhT4IXvDiw130hJ74Jkz1qYidBYnE+vEzAw To1ka5ra09UE3Ckrsm89RBNdHT5hrrYqVn3oGpd7PL54LOZGFviA7gr4aOy9N+LDtckSDm oZu5aEx8KZupi4+VpLmfMrwnuQgne3w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1796135735; 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=BirWYkTehAUqeERTOxeGbr/7Oy0d4FXIgyhMXDXyzb4=; b=Yv2s9RJmxN8LzA62gIijSwSIGZFl/TkZDH0FHqMCQFrDnNsVRM6ZvvhxCGVSJ7QwHPFgk8 HbwZ1ihfxurYaWAw== 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 13D2D1375D; Tue, 1 Dec 2026 14:35:34 +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 KmQYNzXbDmsFVQAAD6G6ig (envelope-from ); Tue, 01 Dec 2026 14:35:34 +0000 Received: by quack.suse.cz (Postfix, from userid 1000) id 28BBC51549; 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 7/8] fs: Add throttling to deferred inode reclaim Date: Tue, 1 Dec 2026 15:35:09 +0100 Message-ID: <20261201143522.483168-15-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=9859; i=jack@suse.cz; h=from:subject; bh=rkFGZhS8FcyppHz1mzzyWEAcFFGT8imYlGYhoRQxHi8=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBrDtsvPoTvAqb4PFfOa4h5hsdjQF9Hs0cIiNhL2 s5fe1mRyfaJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCaw7bLwAKCRCcnaoHP2RA 2f84B/9CzAQ3mwdVmCYCnqq/8MXYi055RUr3OTUHh/p8V/Bo2j6Ep5AFce75etBaYkSBkQuFNWP PYX9ioC3+cSMhdKPRI+w87ndnoJ6roDKQU4ac8GX6ctBmjTfUlhuuFVmkHjr5Dagg73haaULnWZ 8424s6yDvEpKvVDTYsYJwTPb6hFOweum3/53oejTdE4YZj9ojDOhh+YFyws92DIenWAMKuiy5Z/ l839vqQ02LEFa25u0CYLxzZd3IyIAwV6RYaDLVVTeZSsCSpimVjWFpLJ8P/8XC12qRFyE5AjBDH fxESML4xVPepsaXBz5Oi9f86oSk8VMAUGXkuvW1XwyxEX8jD X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Content-Transfer-Encoding: 8bit X-Spam-Flag: NO X-Spam-Level: X-Spamd-Result: default: False [-15.80 / 50.00]; SUSE_INTERNAL_BYPASS(-9.00)[]; REPLY(-4.00)[]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_LAST(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.cz:email] X-Spam-Score: -15.80 Deferring difficult inode reclaim from prune_icache_sb() to a workqueue removes the natural feedback loop of blocking tasks in direct reclaim until they make space for new allocations. This can result in the list of deferred inodes to grow beyond any bounds and possibly push the machine to a reclaim storm or OOM. Add a throttling mechanism that makes tasks calling mark_inode_reclaim_deferred() wait on exit to userspace if the number of deferred inodes to reclaim for a superblock grows over limit. We measure average time it takes to reclaim inode on deferred list for a superblock and block tasks proportionally to that. The mechanism to block tasks on exit to userspace is a bit simplistic but I don't expect more than one inode to be deferred per syscall and I expect throttling to be rare so it should be fine. If we wanted something more advanced, we could store the delay in task_struct and just wait for it when returning to userspace similarly as blkcg throttling does it but so far I consider the space in task_struct a waste for this purpose. Signed-off-by: Jan Kara --- fs/inode.c | 150 ++++++++++++++++++++++++++++--- include/linux/fs/super_types.h | 2 + include/trace/events/writeback.h | 51 +++++++++++ 3 files changed, 189 insertions(+), 14 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index 5cc36e3df2e8..6b9c63f21307 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #define CREATE_TRACE_POINTS #include @@ -1143,23 +1144,55 @@ static int get_inode_reclaim_batch(int *id, struct list_head *list) return count; } +static void inode_reclaim_update_stat(struct super_block *sb, unsigned int n, + u64 start) +{ + u64 delay; + + delay = div_u64(ktime_get_ns() - start, n); + /* + * Smooth delay updates with exponential moving average. Updates can + * get lost if workers race but we don't really care. + */ + WRITE_ONCE(sb->s_deferred_reclaim_delay, + (63 * READ_ONCE(sb->s_deferred_reclaim_delay) + delay) / 64); + + trace_inode_reclaim_update_stat(sb, n, delay, + READ_ONCE(sb->s_deferred_reclaim_delay)); +} + +static void sb_deferred_reclaim_put(struct super_block *sb, int n) +{ + /* + * The elevated s_deferred_reclaim_count keeps sb alive until we drop + * it. Use RCU to make sure sb isn't freed before the wakeup. + */ + rcu_read_lock(); + if (!atomic_sub_return(n, &sb->s_deferred_reclaim_count)) + wake_up_var(&sb->s_deferred_reclaim_count); + rcu_read_unlock(); +} static void inode_reclaim_deferred(struct work_struct *work) { struct inode *inode; LIST_HEAD(inode_batch); + u64 start; + int id, count; + struct super_block *sb; + /* * We start with the list corresponding to the work but rolling a dice * would work as well */ - int id = work - deferred_reclaim.work; + 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; + sb = NULL; + while (!list_empty(&inode_batch)) { /* * inode_batch list is private and I_FREEING flags * protect us from anybody else trying to remove the @@ -1167,20 +1200,27 @@ static void inode_reclaim_deferred(struct work_struct *work) */ inode = list_first_entry(&inode_batch, struct inode, i_lru); + if (inode->i_sb != sb) { + if (sb) { + inode_reclaim_update_stat(sb, count, + start); + sb_deferred_reclaim_put(sb, count); + } + + sb = inode->i_sb; + count = 0; + start = ktime_get_ns(); + } + count++; 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(); } + + if (sb) { + inode_reclaim_update_stat(sb, count, start); + sb_deferred_reclaim_put(sb, count); + } } } @@ -1216,6 +1256,80 @@ static void __init deferred_reclaim_init(void) } } +struct deferred_reclaim_delay { + struct callback_head task_work; + u64 delay; +}; + +static void deferred_delay_work(struct callback_head *work) +{ + struct deferred_reclaim_delay *dd = + container_of(work, struct deferred_reclaim_delay, task_work); + + schedule_timeout_killable(nsecs_to_jiffies(dd->delay)); + kfree(dd); +} + +/* + * Size of deferred reclaim list from which we start throttling tasks creating + * inodes marked for deferred reclaim. + */ +#define INODE_DEFERRED_RECLAIM_LIMIT 8192 + +static void throttle_inode_deferred_reclaim(struct inode *inode) +{ + struct super_block *sb = inode->i_sb; + unsigned int len; + + /* + * Don't throttle kthreads. They are doing work for somebody else + * anyway so throttling is counterproductive. + */ + if (current->flags & PF_KTHREAD) + return; + + /* + * If inodes with deferred reclaim are accumulating too much, slow down + * tasks creating them. This doesn't provide any kind of guarantee on + * the length of the deferred list since lots of inodes with + * I_DEFER_RECLAIM can be already present in the inode cache and we + * have no control when they reach the deferred list. But if the + * pressure on the deferred list is sustained, the balance should + * eventually be established. + */ + len = atomic_read(&sb->s_deferred_reclaim_count); + if (len > INODE_DEFERRED_RECLAIM_LIMIT) { + u64 delay = READ_ONCE(sb->s_deferred_reclaim_delay); + struct deferred_reclaim_delay *dd; + + /* + * Scale the delay based on how much we exceed the limit. Wait + * at most 4x as long as estimated time to reclaim the inode. + */ + len = min(len, 5 * INODE_DEFERRED_RECLAIM_LIMIT); + delay = div_u64(delay * (len - INODE_DEFERRED_RECLAIM_LIMIT), + INODE_DEFERRED_RECLAIM_LIMIT); + if (!delay) + return; + + dd = kmalloc_obj(*dd, GFP_NOFS | __GFP_NORETRY); + /* + * If the allocation failed, don't bother. Throttling is + * approximate anyway and next time we'll throttle more. + */ + if (!dd) + return; + + trace_mark_inode_reclaim_deferred_throttle(inode, len, delay); + + dd->delay = delay; + init_task_work(&dd->task_work, deferred_delay_work); + /* This shouldn't fail. If it does, just cleanly bail. */ + if (task_work_add(current, &dd->task_work, TWA_RESUME) < 0) + kfree(dd); + } +} + /* * 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 @@ -1232,12 +1346,20 @@ static void __init deferred_reclaim_init(void) */ void mark_inode_reclaim_deferred(struct inode *inode) { + bool throttle = false; + if (inode_state_read_once(inode) & I_DEFER_RECLAIM) return; spin_lock(&inode->i_lock); - inode_state_set(inode, I_DEFER_RECLAIM); + if (!(inode_state_read(inode) & I_DEFER_RECLAIM)) { + inode_state_set(inode, I_DEFER_RECLAIM); + throttle = true; + } spin_unlock(&inode->i_lock); + + if (throttle) + throttle_inode_deferred_reclaim(inode); } EXPORT_SYMBOL_GPL(mark_inode_reclaim_deferred); diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h index fc8f7f2261e2..fe952ef02b96 100644 --- a/include/linux/fs/super_types.h +++ b/include/linux/fs/super_types.h @@ -241,6 +241,8 @@ struct super_block { /* Number of inodes queued for deferred reclaim */ atomic_t s_deferred_reclaim_count; + /* Average time to reclaim one deferred inode */ + u64 s_deferred_reclaim_delay; /* 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 c2a5b492a71a..d371d1112843 100644 --- a/include/trace/events/writeback.h +++ b/include/trace/events/writeback.h @@ -885,6 +885,57 @@ DEFINE_EVENT(writeback_inode_template, sb_clear_inode_writeback, TP_ARGS(inode) ); +TRACE_EVENT(inode_reclaim_update_stat, + TP_PROTO( + struct super_block *sb, + unsigned int n, + u64 batch_delay, + u64 avg_delay + ), + TP_ARGS(sb, n, batch_delay, avg_delay), + + TP_STRUCT__entry( + __field(dev_t, dev) + __field(unsigned int, n) + __field(u64, batch_delay) + __field(u64, avg_delay) + ), + + TP_fast_assign( + __entry->dev = sb->s_dev; + __entry->n = n; + __entry->batch_delay = batch_delay; + __entry->avg_delay = avg_delay; + ), + + TP_printk("dev %d,%d batch size %u batch delay %llu ns avg delay %llu ns", + MAJOR(__entry->dev), MINOR(__entry->dev), __entry->n, + __entry->batch_delay, __entry->avg_delay) +); + +TRACE_EVENT(mark_inode_reclaim_deferred_throttle, + TP_PROTO(struct inode *inode, unsigned int len, u64 delay), + TP_ARGS(inode, len, delay), + + TP_STRUCT__entry( + __field(u64, ino) + __field(dev_t, dev) + __field(unsigned int, len) + __field(u64, delay) + ), + + TP_fast_assign( + __entry->ino = inode->i_ino; + __entry->dev = inode->i_sb->s_dev; + __entry->len = len; + __entry->delay = delay; + ), + + TP_printk("dev %d,%d ino %llu deferred list len %u delay %llu ns", + MAJOR(__entry->dev), MINOR(__entry->dev), + __entry->ino, __entry->len, __entry->delay) +); + #endif /* _TRACE_WRITEBACK_H */ /* This part must be outside protection */ -- 2.53.0