Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ext4: remove unnecessary parameter "needed" in ext4_discard_preallocations

The "needed" controls the number of ext4_prealloc_space to discard in
ext4_discard_preallocations. Function ext4_discard_preallocations is
supposed to discard all non-used preallocated blocks when "needed"
is 0 and now ext4_discard_preallocations is always called with "needed"
= 0. Remove unnecessary parameter "needed" and remove all non-used
preallocated spaces in ext4_discard_preallocations to simplify the
code.

Note: If count of non-used preallocated spaces could be more than
UINT_MAX, there was a memory leak as some non-used preallocated
spaces are left ununsed and this commit will fix it. Otherwise,
there is no behavior change.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20240105092102.496631-9-shikemeng@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>

authored by

Kemeng Shi and committed by
Theodore Ts'o
2ffd2a6a 20427949

+18 -22
+1 -1
fs/ext4/ext4.h
··· 2915 2915 extern void ext4_mb_release(struct super_block *); 2916 2916 extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *, 2917 2917 struct ext4_allocation_request *, int *); 2918 - extern void ext4_discard_preallocations(struct inode *, unsigned int); 2918 + extern void ext4_discard_preallocations(struct inode *); 2919 2919 extern int __init ext4_init_mballoc(void); 2920 2920 extern void ext4_exit_mballoc(void); 2921 2921 extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
+5 -5
fs/ext4/extents.c
··· 100 100 * i_rwsem. So we can safely drop the i_data_sem here. 101 101 */ 102 102 BUG_ON(EXT4_JOURNAL(inode) == NULL); 103 - ext4_discard_preallocations(inode, 0); 103 + ext4_discard_preallocations(inode); 104 104 up_write(&EXT4_I(inode)->i_data_sem); 105 105 *dropped = 1; 106 106 return 0; ··· 4313 4313 * not a good idea to call discard here directly, 4314 4314 * but otherwise we'd need to call it every free(). 4315 4315 */ 4316 - ext4_discard_preallocations(inode, 0); 4316 + ext4_discard_preallocations(inode); 4317 4317 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 4318 4318 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; 4319 4319 ext4_free_blocks(handle, inode, NULL, newblock, ··· 5357 5357 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); 5358 5358 5359 5359 down_write(&EXT4_I(inode)->i_data_sem); 5360 - ext4_discard_preallocations(inode, 0); 5360 + ext4_discard_preallocations(inode); 5361 5361 ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start); 5362 5362 5363 5363 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); ··· 5365 5365 up_write(&EXT4_I(inode)->i_data_sem); 5366 5366 goto out_stop; 5367 5367 } 5368 - ext4_discard_preallocations(inode, 0); 5368 + ext4_discard_preallocations(inode); 5369 5369 5370 5370 ret = ext4_ext_shift_extents(inode, handle, punch_stop, 5371 5371 punch_stop - punch_start, SHIFT_LEFT); ··· 5497 5497 goto out_stop; 5498 5498 5499 5499 down_write(&EXT4_I(inode)->i_data_sem); 5500 - ext4_discard_preallocations(inode, 0); 5500 + ext4_discard_preallocations(inode); 5501 5501 5502 5502 path = ext4_find_extent(inode, offset_lblk, NULL, 0); 5503 5503 if (IS_ERR(path)) {
+1 -1
fs/ext4/file.c
··· 174 174 (atomic_read(&inode->i_writecount) == 1) && 175 175 !EXT4_I(inode)->i_reserved_data_blocks) { 176 176 down_write(&EXT4_I(inode)->i_data_sem); 177 - ext4_discard_preallocations(inode, 0); 177 + ext4_discard_preallocations(inode); 178 178 up_write(&EXT4_I(inode)->i_data_sem); 179 179 } 180 180 if (is_dx(inode) && filp->private_data)
+1 -1
fs/ext4/indirect.c
··· 714 714 * i_rwsem. So we can safely drop the i_data_sem here. 715 715 */ 716 716 BUG_ON(EXT4_JOURNAL(inode) == NULL); 717 - ext4_discard_preallocations(inode, 0); 717 + ext4_discard_preallocations(inode); 718 718 up_write(&EXT4_I(inode)->i_data_sem); 719 719 *dropped = 1; 720 720 return 0;
+3 -3
fs/ext4/inode.c
··· 371 371 */ 372 372 if ((ei->i_reserved_data_blocks == 0) && 373 373 !inode_is_open_for_write(inode)) 374 - ext4_discard_preallocations(inode, 0); 374 + ext4_discard_preallocations(inode); 375 375 } 376 376 377 377 static int __check_block_validity(struct inode *inode, const char *func, ··· 4017 4017 if (stop_block > first_block) { 4018 4018 4019 4019 down_write(&EXT4_I(inode)->i_data_sem); 4020 - ext4_discard_preallocations(inode, 0); 4020 + ext4_discard_preallocations(inode); 4021 4021 4022 4022 ext4_es_remove_extent(inode, first_block, 4023 4023 stop_block - first_block); ··· 4170 4170 4171 4171 down_write(&EXT4_I(inode)->i_data_sem); 4172 4172 4173 - ext4_discard_preallocations(inode, 0); 4173 + ext4_discard_preallocations(inode); 4174 4174 4175 4175 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 4176 4176 err = ext4_ext_truncate(handle, inode);
+1 -1
fs/ext4/ioctl.c
··· 467 467 ext4_reset_inode_seed(inode); 468 468 ext4_reset_inode_seed(inode_bl); 469 469 470 - ext4_discard_preallocations(inode, 0); 470 + ext4_discard_preallocations(inode); 471 471 472 472 err = ext4_mark_inode_dirty(handle, inode); 473 473 if (err < 0) {
+3 -7
fs/ext4/mballoc.c
··· 5498 5498 * 5499 5499 * FIXME!! Make sure it is valid at all the call sites 5500 5500 */ 5501 - void ext4_discard_preallocations(struct inode *inode, unsigned int needed) 5501 + void ext4_discard_preallocations(struct inode *inode) 5502 5502 { 5503 5503 struct ext4_inode_info *ei = EXT4_I(inode); 5504 5504 struct super_block *sb = inode->i_sb; ··· 5520 5520 mb_debug(sb, "discard preallocation for inode %lu\n", 5521 5521 inode->i_ino); 5522 5522 trace_ext4_discard_preallocations(inode, 5523 - atomic_read(&ei->i_prealloc_active), needed); 5524 - 5525 - if (needed == 0) 5526 - needed = UINT_MAX; 5523 + atomic_read(&ei->i_prealloc_active), 0); 5527 5524 5528 5525 repeat: 5529 5526 /* first, collect all pa's in the inode */ 5530 5527 write_lock(&ei->i_prealloc_lock); 5531 - for (iter = rb_first(&ei->i_prealloc_node); iter && needed; 5528 + for (iter = rb_first(&ei->i_prealloc_node); iter; 5532 5529 iter = rb_next(iter)) { 5533 5530 pa = rb_entry(iter, struct ext4_prealloc_space, 5534 5531 pa_node.inode_node); ··· 5549 5552 spin_unlock(&pa->pa_lock); 5550 5553 rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node); 5551 5554 list_add(&pa->u.pa_tmp_list, &list); 5552 - needed--; 5553 5555 continue; 5554 5556 } 5555 5557
+2 -2
fs/ext4/move_extent.c
··· 686 686 687 687 out: 688 688 if (*moved_len) { 689 - ext4_discard_preallocations(orig_inode, 0); 690 - ext4_discard_preallocations(donor_inode, 0); 689 + ext4_discard_preallocations(orig_inode); 690 + ext4_discard_preallocations(donor_inode); 691 691 } 692 692 693 693 ext4_free_ext_path(path);
+1 -1
fs/ext4/super.c
··· 1525 1525 ext4_fc_del(inode); 1526 1526 invalidate_inode_buffers(inode); 1527 1527 clear_inode(inode); 1528 - ext4_discard_preallocations(inode, 0); 1528 + ext4_discard_preallocations(inode); 1529 1529 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); 1530 1530 dquot_drop(inode); 1531 1531 if (EXT4_I(inode)->jinode) {