Skip to content

Commit 0f5bb0c

Browse files
David Laightbrauner
authored andcommitted
fs: use min() or umin() instead of min_t()
min_t(unsigned int, a, b) casts an 'unsigned long' to 'unsigned int'. Use min(a, b) instead as it promotes any 'unsigned int' to 'unsigned long' and so cannot discard significant bits. A couple of places need umin() because of loops like: nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE); for (i = 0; i < nfolios; i++) { struct folio *folio = page_folio(pages[i]); ... unsigned int len = umin(ret, PAGE_SIZE - start); ... ret -= len; ... } where the compiler doesn't track things well enough to know that 'ret' is never negative. The alternate loop: for (i = 0; ret > 0; i++) { struct folio *folio = page_folio(pages[i]); ... unsigned int len = min(ret, PAGE_SIZE - start); ... ret -= len; ... } would be equivalent and doesn't need 'nfolios'. Most of the 'unsigned long' actually come from PAGE_SIZE. Detected by an extra check added to min_t(). Signed-off-by: David Laight <david.laight.linux@gmail.com> Link: https://patch.msgid.link/20251119224140.8616-31-david.laight.linux@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 8f0b4cc commit 0f5bb0c

10 files changed

Lines changed: 13 additions & 17 deletions

File tree

fs/buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ bool block_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
23542354
if (!head)
23552355
return false;
23562356
blocksize = head->b_size;
2357-
to = min_t(unsigned, folio_size(folio) - from, count);
2357+
to = min(folio_size(folio) - from, count);
23582358
to = from + to;
23592359
if (from < blocksize && to > folio_size(folio) - blocksize)
23602360
return false;

fs/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ int copy_string_kernel(const char *arg, struct linux_binprm *bprm)
555555
return -E2BIG;
556556

557557
while (len > 0) {
558-
unsigned int bytes_to_copy = min_t(unsigned int, len,
558+
unsigned int bytes_to_copy = min(len,
559559
min_not_zero(offset_in_page(pos), PAGE_SIZE));
560560
struct page *page;
561561

fs/ext4/mballoc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4276,8 +4276,7 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
42764276
* get the corresponding group metadata to work with.
42774277
* For this we have goto again loop.
42784278
*/
4279-
thisgrp_len = min_t(unsigned int, (unsigned int)len,
4280-
EXT4_BLOCKS_PER_GROUP(sb) - EXT4_C2B(sbi, blkoff));
4279+
thisgrp_len = min(len, EXT4_BLOCKS_PER_GROUP(sb) - EXT4_C2B(sbi, blkoff));
42814280
clen = EXT4_NUM_B2C(sbi, thisgrp_len);
42824281

42834282
if (!ext4_sb_block_valid(sb, NULL, block, thisgrp_len)) {

fs/ext4/resize.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ static void ext4_update_super(struct super_block *sb,
14791479

14801480
/* Update the global fs size fields */
14811481
sbi->s_groups_count += flex_gd->count;
1482-
sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
1482+
sbi->s_blockfile_groups = min(sbi->s_groups_count,
14831483
(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
14841484

14851485
/* Update the reserved block counts only once the new group is

fs/ext4/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4832,7 +4832,7 @@ static int ext4_check_geometry(struct super_block *sb,
48324832
return -EINVAL;
48334833
}
48344834
sbi->s_groups_count = blocks_count;
4835-
sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4835+
sbi->s_blockfile_groups = min(sbi->s_groups_count,
48364836
(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
48374837
if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
48384838
le32_to_cpu(es->s_inodes_count)) {

fs/fat/dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
13531353

13541354
/* Fill the long name slots. */
13551355
for (i = 0; i < long_bhs; i++) {
1356-
int copy = min_t(int, sb->s_blocksize - offset, size);
1356+
int copy = umin(sb->s_blocksize - offset, size);
13571357
memcpy(bhs[i]->b_data + offset, slots, copy);
13581358
mark_buffer_dirty_inode(bhs[i], dir);
13591359
offset = 0;
@@ -1364,7 +1364,7 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
13641364
err = fat_sync_bhs(bhs, long_bhs);
13651365
if (!err && i < nr_bhs) {
13661366
/* Fill the short name slot. */
1367-
int copy = min_t(int, sb->s_blocksize - offset, size);
1367+
int copy = umin(sb->s_blocksize - offset, size);
13681368
memcpy(bhs[i]->b_data + offset, slots, copy);
13691369
mark_buffer_dirty_inode(bhs[i], dir);
13701370
if (IS_DIRSYNC(dir))

fs/fat/file.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
140140
if (copy_from_user(&range, user_range, sizeof(range)))
141141
return -EFAULT;
142142

143-
range.minlen = max_t(unsigned int, range.minlen,
144-
bdev_discard_granularity(sb->s_bdev));
143+
range.minlen = max(range.minlen, bdev_discard_granularity(sb->s_bdev));
145144

146145
err = fat_trim_fs(inode, &range);
147146
if (err < 0)

fs/fuse/dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
18131813
goto out_iput;
18141814

18151815
folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
1816-
nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
1816+
nr_bytes = min(num, folio_size(folio) - folio_offset);
18171817
nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
18181818

18191819
err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);

fs/fuse/file.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,8 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
13231323
static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
13241324
unsigned int max_pages)
13251325
{
1326-
return min_t(unsigned int,
1327-
((pos + len - 1) >> PAGE_SHIFT) -
1328-
(pos >> PAGE_SHIFT) + 1,
1329-
max_pages);
1326+
return min(((pos + len - 1) >> PAGE_SHIFT) - (pos >> PAGE_SHIFT) + 1,
1327+
max_pages);
13301328
}
13311329

13321330
static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii)
@@ -1607,7 +1605,7 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
16071605
struct folio *folio = page_folio(pages[i]);
16081606
unsigned int offset = start +
16091607
(folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
1610-
unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
1608+
unsigned int len = umin(ret, PAGE_SIZE - start);
16111609

16121610
ap->descs[ap->num_folios].offset = offset;
16131611
ap->descs[ap->num_folios].length = len;

fs/splice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static ssize_t iter_to_pipe(struct iov_iter *from,
14671467

14681468
n = DIV_ROUND_UP(left + start, PAGE_SIZE);
14691469
for (i = 0; i < n; i++) {
1470-
int size = min_t(int, left, PAGE_SIZE - start);
1470+
int size = umin(left, PAGE_SIZE - start);
14711471

14721472
buf.page = pages[i];
14731473
buf.offset = start;

0 commit comments

Comments
 (0)