From nobody Fri Oct 2 03:50:55 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 4989F3F6C50 for ; Wed, 5 Aug 2026 10:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785927285; cv=none; b=CuY1PvHjt91KTbvZndG73W0Ew16j28VgRTCl5cCR1uyXfDWxdEqTlmVO9jKUeDW27/2IT1DjrM0+MF9gxCbqNarnF+5Xyxi+lZSpDGyUQaWrPbzQ0+DJS5eivYaLw81FUpWczqqPE3LRVUeGciFdOy1/ZpvnZNv7Fik4YyiU1FA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785927285; c=relaxed/simple; bh=z5CtL0tvG/N6SW3r5QghRl4ULIot3ag0FmgDNUWAZOc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=TnwQRbGxg8PMn/xjJWl9XPSupV2eYdSMXBQyJMuLbFMiWRWyJhiPpkEXWfP8+f45HvLpfwfUen86msxJA5jKdp/8pm+aXcqctUKrKeAjddFnhXHPALZlMQqfjPGeDQqNDinYqMkpY+K0/iANUgdwOb0P0ZRpHc2lf/IjG2DUOSw= 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=ZvwlJwLM; arc=none smtp.client-ip=209.85.216.54 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="ZvwlJwLM" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-38f620399a0so725554a91.2 for ; Wed, 05 Aug 2026 03:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785927283; x=1786532083; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=CcRPCvVBu/zePR4R+dFekkxEB52r0HskV68lJfnp8ZU=; b=ZvwlJwLMKcrXlOBOaGAznAd5qly8CzYSr+arXhnbnxpiBrgS8NWbc8f9XFbMhOHBEm P4pdwZITKU5NNPUlRnBklZDVzAWEaU4MU5gWDXuioAH+HSbUmbcwDsUSY9cwJWZrt17e NYrG41Q5NsDTIx9EY/ZKZc7EBPjV7U7aTfZsqnsl7me4vWGCdqGIva2s+NPmY1Vpwg+S hfglWf6IOwh/IRYS+Liu7B8J78tKAg4mINSK3jTXpxPhlOfShCKwdV7T8s+8PVA+XFCQ /VVLhm9zWVfwxbrI3gsbvQZkPmyOGXhlCTXLIIY0ENcrzbQt1QU2FejvcYZERPD3Tcj2 lkbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785927283; x=1786532083; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CcRPCvVBu/zePR4R+dFekkxEB52r0HskV68lJfnp8ZU=; b=MAdrUv9jHgr9u4qDnP8yhi9pq/bhDPGTmdpU9FnDEaFnRnoCU+HxYkxxtKm9fs1HRZ p2wGhJ2Hosnv/43mi9PN5M7yNvXjdidesl11J1Ty+Ec/lKNXRB1N9TLorl3fTFeC0ghX xqMlkZi2oB5/zuKmKJp+UoIWlfRcEdcZVzGgylpTILmhb+pdaA7n0UTMrWwcK3J8p7Vb G3VIPoGk9qEweZCq/Va/ejZAZx1XPsoK+Mh08Gmr2un8K1qSPqgPUjIbanz+t34+tm/M b+YaoWIMAGlJvClKKkRj7bHjFwox83eUt8qAC8+atkQT7U+qR/c9aOwr33UXTmdbg9gi 1PBw== X-Gm-Message-State: AOJu0Yw4yog5HVhfC8V1Y3V96ojglI+H+n7hq94Z8Rya9fdf1vKqeC3O uPk2B3ZBGAqnG6pNNh+mLrhq3eL0TWMqHVE5+0HWzILX4FZNsO5QEc2ubKLK8zzS X-Gm-Gg: AR+sD13OSPq8knK017Wv6Z9eUPPgHRrmr6C9MB/5UsgIYzLwSHIqr+3r5se0VCth9Wt oaMYAp1eaAjw4OdLSOTogXs82/HN0ZdzGmWr4UPM628aObcsuQaWurTAItYYQuYvMer00s2lPjR HpC3BDhcxkPgfrVCorgGX9h+afUnlyoWlXS7UtKASTsS3VSy7r5896znCczYPa6XgzEj73IWI9D 3H3J/VwpYzEDQ01pyME2xkeC8e6z9OUotFvXZQRHLhNoQDu6Nve7vK1/BhorBn0tDjJx+mVtq/O 2WmGgIqhe7HfixsjHTwx8Rm9N1qvSO+gsbjD0MqrZGQ2myPqOJKctCQvaG86cK6pL0vDVVIv+6x wc1Qaohep6kBRjn/C169RqIsEa601VLQ0zMQg+TP6K+QJ7/VcoWyBRq8MdBufi3wLvg1QKm7AWg 3uJc2MQSuIY9TjDv5iUrB3YV6+FX3rQzYyRWvWPDYv7fhQGJ0DbdfVfnT1QRUS749rdiryxgXvR YXID2aw/924 X-Received: by 2002:a17:90a:d445:b0:37e:b6a:6cdf with SMTP id 98e67ed59e1d1-3903c66ba04mr6169363a91.20.1785927283397; Wed, 05 Aug 2026 03:54:43 -0700 (PDT) Received: from localhost.localdomain ([47.100.192.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3903f5fdfe5sm1529929a91.4.2026.08.05.03.54.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2026 03:54:43 -0700 (PDT) From: Yang Wen To: hirofumi@mail.parknet.co.jp Cc: linux-kernel@vger.kernel.org, Yang Wen Subject: [PATCH] fat: reclaim unused KEEP_SIZE clusters on last writer release Date: Wed, 5 Aug 2026 18:54:26 +0800 Message-Id: <20260805105426.443353-1-anmuxixixi@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" FAT releases unused clusters allocated by FALLOC_FL_KEEP_SIZE when the inode is evicted. As a result, inode reclaim can enter the FAT cluster freeing path directly from kswapd. If another thread holds the FAT lock for a long time, kswapd can block while reclaiming an inode which still has unused KEEP_SIZE clusters: Thread-B kswapd -------- ------ lock_fat() mutex_lock(&sbi->fat_lock) shrink_node() shrink_slab() super_cache_scan() prune_icache_sb() evict() fat_evict_inode() fat_free_eofblocks() fat_truncate_blocks() fat_free_clusters() lock_fat() mutex_lock( &sbi->fat_loc= k) fat_lock for a long time> This leaves kswapd blocked until Thread-B releases fat_lock and can delay memory reclaim for an extended period. KEEP_SIZE preallocation on FAT is not persistent. The unused clusters only need to remain allocated while a writable file description can still consume them. Track writable file descriptions with a counter in the FAT private inode. When the counter reaches zero, acquire inode->i_rwsem and reclaim the unused clusters if no writer has appeared in the meantime. A writer opened after that check serializes subsequent write and fallocate operations with the cleanup through inode->i_rwsem and, as documented, must re-establish any reservation after reopening the file. On the last writer release, take internal freeze protection before inode->i_rwsem, wait for direct I/O and run the existing EOF-block cleanup under truncate_lock. This keeps the metadata update outside a frozen filesystem without requiring a separate workqueue. This can move a fat_lock wait to the final writable file release, but prevents kswapd from executing the potentially blocking cluster-chain update directly. Keep the cleanup in fat_evict_inode() as a fallback for any inode which still reaches eviction with unused preallocated clusters. Signed-off-by: Yang Wen --- fs/fat/fat.h | 5 +++++ fs/fat/file.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ fs/fat/inode.c | 22 ++++++++++++++++------ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/fs/fat/fat.h b/fs/fat/fat.h index 61338413d9f3..c8bdbbaa5a0b 100644 --- a/fs/fat/fat.h +++ b/fs/fat/fat.h @@ -121,6 +121,9 @@ struct msdos_inode_info { /* for avoiding the race between fat_free() and fat_get_cluster() */ unsigned int cache_valid_id; =20 + /* Number of writable struct file instances. */ + atomic_t write_open_count; + /* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */ loff_t mmu_private; /* physically allocated size */ =20 @@ -407,6 +410,8 @@ extern const struct inode_operations fat_file_inode_ope= rations; extern int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *attr); extern void fat_truncate_blocks(struct inode *inode, loff_t offset); +extern bool fat_has_eofblocks(struct inode *inode); +extern void fat_free_eofblocks(struct inode *inode); extern int fat_getattr(struct mnt_idmap *idmap, const struct path *path, struct kstat *stat, u32 request_mask, unsigned int flags); diff --git a/fs/fat/file.c b/fs/fat/file.c index 1c835ca5f21a..935aa8e90eea 100644 --- a/fs/fat/file.c +++ b/fs/fat/file.c @@ -173,8 +173,53 @@ long fat_generic_ioctl(struct file *filp, unsigned int= cmd, unsigned long arg) } } =20 +static int fat_file_open(struct inode *inode, struct file *filp) +{ + if (filp->f_mode & FMODE_WRITER) + atomic_inc(&MSDOS_I(inode)->write_open_count); + + return 0; +} + static int fat_file_release(struct inode *inode, struct file *filp) { + struct msdos_inode_info *ei =3D MSDOS_I(inode); + struct super_block *sb =3D inode->i_sb; + + if (filp->f_mode & FMODE_WRITER) { + int writers =3D atomic_dec_return(&ei->write_open_count); + + if (WARN_ON_ONCE(writers < 0)) { + atomic_inc(&ei->write_open_count); + goto flush; + } + + if (!writers) { + sb_start_intwrite(sb); + inode_lock(inode); + + /* + * A writer which appeared before i_rwsem was acquired + * keeps the reservation alive. A writer opened after + * this check has its write and fallocate operations + * serialized with the cleanup by i_rwsem and must + * re-establish the reservation after reopening. + */ + if (!atomic_read(&ei->write_open_count) && + inode->i_nlink && fat_has_eofblocks(inode)) { + inode_dio_wait(inode); + + down_write(&ei->truncate_lock); + mmb_sync(&ei->i_metadata_bhs); + fat_free_eofblocks(inode); + up_write(&ei->truncate_lock); + } + + inode_unlock(inode); + sb_end_intwrite(sb); + } + } +flush: if ((filp->f_mode & FMODE_WRITE) && MSDOS_SB(inode->i_sb)->options.flush) { fat_flush_inodes(inode->i_sb, inode, NULL); @@ -207,6 +252,7 @@ const struct file_operations fat_file_operations =3D { .read_iter =3D generic_file_read_iter, .write_iter =3D generic_file_write_iter, .mmap_prepare =3D generic_file_mmap_prepare, + .open =3D fat_file_open, .release =3D fat_file_release, .unlocked_ioctl =3D fat_generic_ioctl, .compat_ioctl =3D compat_ptr_ioctl, diff --git a/fs/fat/inode.c b/fs/fat/inode.c index f775a004cae1..c01de4263bf9 100644 --- a/fs/fat/inode.c +++ b/fs/fat/inode.c @@ -659,22 +659,31 @@ static int fat_sync_inode_metadata(struct inode *inod= e, return mmb_sync(&MSDOS_I(inode)->i_metadata_bhs); } =20 -static void fat_free_eofblocks(struct inode *inode) +bool fat_has_eofblocks(struct inode *inode) +{ + struct msdos_inode_info *ei =3D MSDOS_I(inode); + struct msdos_sb_info *sbi =3D MSDOS_SB(inode->i_sb); + u64 allocated_bytes =3D (u64)inode->i_blocks << 9; + u64 valid_bytes =3D round_up((u64)ei->mmu_private, + (u64)sbi->cluster_size); + + return allocated_bytes > valid_bytes; +} + +void fat_free_eofblocks(struct inode *inode) { /* Release unwritten fallocated blocks on inode eviction. */ - if ((inode->i_blocks << 9) > - round_up(MSDOS_I(inode)->mmu_private, - MSDOS_SB(inode->i_sb)->cluster_size)) { + if (fat_has_eofblocks(inode)) { int err; =20 fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private); /* Fallocate results in updating the i_start/iogstart * for the zero byte file. So, make it return to - * original state during evict and commit it to avoid + * * original state during cleanup and commit it to avoid * any corruption on the next access to the cluster * chain for the file. */ - err =3D sync_inode_metadata(inode, inode_needs_sync(inode)); + err =3D __fat_write_inode(inode, inode_needs_sync(inode)); if (err) { fat_msg(inode->i_sb, KERN_WARNING, "Failed to " "update on disk inode for unused " @@ -789,6 +798,7 @@ static struct inode *fat_alloc_inode(struct super_block= *sb) return NULL; =20 init_rwsem(&ei->truncate_lock); + atomic_set(&ei->write_open_count, 0); /* Zeroing to allow iput() even if partial initialized inode. */ ei->mmu_private =3D 0; ei->i_start =3D 0; --=20 2.34.1