From: Jiangshan Yi <[email protected]>
Fix the following coccicheck warning:
fs/ext4/inline.c:183: WARNING opportunity for min().
fs/ext4/extents.c:2631: WARNING opportunity for max().
fs/ext4/extents.c:2632: WARNING opportunity for min().
fs/ext4/extents.c:5559: WARNING opportunity for max().
fs/ext4/super.c:6908: WARNING opportunity for min().
min()/max() and min_t() macro is defined in include/linux/minmax.h.
It avoids multiple evaluations of the arguments when non-constant and
performs strict type-checking.
Suggested-by: Lukas Czerner <[email protected]>
Signed-off-by: Jiangshan Yi <[email protected]>
Reviewed-by: Lukas Czerner <[email protected]>
---
fs/ext4/extents.c | 8 +++-----
fs/ext4/inline.c | 3 +--
fs/ext4/super.c | 3 +--
3 files changed, 5 insertions(+), 9 deletions(-)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index c148bb97b527..37321aecd878 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -2628,9 +2628,8 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
unwritten, ex_ee_len);
path[depth].p_ext = ex;
- a = ex_ee_block > start ? ex_ee_block : start;
- b = ex_ee_block+ex_ee_len - 1 < end ?
- ex_ee_block+ex_ee_len - 1 : end;
+ a = max(ex_ee_block, start);
+ b = min(ex_ee_block + ex_ee_len - 1, end);
ext_debug(inode, " border %u:%u\n", a, b);
@@ -5557,8 +5556,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
* ee_start_lblk to shift extents
*/
ret = ext4_ext_shift_extents(inode, handle,
- ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
- len_lblk, SHIFT_RIGHT);
+ max(ee_start_lblk, offset_lblk), len_lblk, SHIFT_RIGHT);
up_write(&EXT4_I(inode)->i_data_sem);
if (IS_SYNC(inode))
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index a4fbe825694b..2b42ececa46d 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -180,8 +180,7 @@ static int ext4_read_inline_data(struct inode *inode, void *buffer,
BUG_ON(len > EXT4_I(inode)->i_inline_size);
- cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
- len : EXT4_MIN_INLINE_DATA_SIZE;
+ cp_len = min_t(unsigned int, len, EXT4_MIN_INLINE_DATA_SIZE);
raw_inode = ext4_raw_inode(iloc);
memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 9a66abcca1a8..5615bf7ab15d 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -6905,8 +6905,7 @@ static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
len = i_size-off;
toread = len;
while (toread > 0) {
- tocopy = sb->s_blocksize - offset < toread ?
- sb->s_blocksize - offset : toread;
+ tocopy = min(sb->s_blocksize - offset, toread);
bh = ext4_bread(NULL, inode, blk, 0);
if (IS_ERR(bh))
return PTR_ERR(bh);
--
2.25.1
Hi Jiangshan,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on tytso-ext4/dev]
[also build test WARNING on linus/master v6.0-rc1 next-20220816]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jiangshan-Yi/fs-ext4-replace-ternary-operator-with-min-max-and-min_t/20220816-144454
base: https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: arm-randconfig-r034-20220815
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project aed5e3bea138ce581d682158eb61c27b3cfdd6ec)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/intel-lab-lkp/linux/commit/cdc8d157495f1a1cbf921569e2babf14446058cf
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jiangshan-Yi/fs-ext4-replace-ternary-operator-with-min-max-and-min_t/20220816-144454
git checkout cdc8d157495f1a1cbf921569e2babf14446058cf
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash fs/ext4/
If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>
All warnings (new ones prefixed by >>):
>> fs/ext4/super.c:6907:12: warning: comparison of distinct pointer types ('typeof (sb->s_blocksize - offset) *' (aka 'unsigned long *') and 'typeof (toread) *' (aka 'unsigned int *')) [-Wcompare-distinct-pointer-types]
tocopy = min(sb->s_blocksize - offset, toread);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/minmax.h:45:19: note: expanded from macro 'min'
#define min(x, y) __careful_cmp(x, y, <)
^~~~~~~~~~~~~~~~~~~~~~
include/linux/minmax.h:36:24: note: expanded from macro '__careful_cmp'
__builtin_choose_expr(__safe_cmp(x, y), \
^~~~~~~~~~~~~~~~
include/linux/minmax.h:26:4: note: expanded from macro '__safe_cmp'
(__typecheck(x, y) && __no_side_effects(x, y))
^~~~~~~~~~~~~~~~~
include/linux/minmax.h:20:28: note: expanded from macro '__typecheck'
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~
1 warning generated.
vim +6907 fs/ext4/super.c
6885
6886 /* Read data from quotafile - avoid pagecache and such because we cannot afford
6887 * acquiring the locks... As quota files are never truncated and quota code
6888 * itself serializes the operations (and no one else should touch the files)
6889 * we don't have to be afraid of races */
6890 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6891 size_t len, loff_t off)
6892 {
6893 struct inode *inode = sb_dqopt(sb)->files[type];
6894 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6895 int offset = off & (sb->s_blocksize - 1);
6896 int tocopy;
6897 size_t toread;
6898 struct buffer_head *bh;
6899 loff_t i_size = i_size_read(inode);
6900
6901 if (off > i_size)
6902 return 0;
6903 if (off+len > i_size)
6904 len = i_size-off;
6905 toread = len;
6906 while (toread > 0) {
> 6907 tocopy = min(sb->s_blocksize - offset, toread);
6908 bh = ext4_bread(NULL, inode, blk, 0);
6909 if (IS_ERR(bh))
6910 return PTR_ERR(bh);
6911 if (!bh) /* A hole? */
6912 memset(data, 0, tocopy);
6913 else
6914 memcpy(data, bh->b_data+offset, tocopy);
6915 brelse(bh);
6916 offset = 0;
6917 toread -= tocopy;
6918 data += tocopy;
6919 blk++;
6920 }
6921 return len;
6922 }
6923
--
0-DAY CI Kernel Test Service
https://01.org/lkp
Hi Jiangshan,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on tytso-ext4/dev]
[also build test WARNING on linus/master v6.0-rc1 next-20220819]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jiangshan-Yi/fs-ext4-replace-ternary-operator-with-min-max-and-min_t/20220816-144454
base: https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: parisc-randconfig-s041-20220821 (https://download.01.org/0day-ci/archive/20220821/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 12.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/cdc8d157495f1a1cbf921569e2babf14446058cf
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jiangshan-Yi/fs-ext4-replace-ternary-operator-with-min-max-and-min_t/20220816-144454
git checkout cdc8d157495f1a1cbf921569e2babf14446058cf
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=parisc SHELL=/bin/bash fs/ext4/
If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>
sparse warnings: (new ones prefixed by >>)
>> fs/ext4/super.c:6907:26: sparse: sparse: incompatible types in comparison expression (different type sizes):
>> fs/ext4/super.c:6907:26: sparse: unsigned long *
>> fs/ext4/super.c:6907:26: sparse: unsigned int *
fs/ext4/super.c:992:6: sparse: sparse: context imbalance in '__ext4_grp_locked_error' - different lock contexts for basic block
fs/ext4/super.c:3266:9: sparse: sparse: context imbalance in 'ext4_check_descriptors' - different lock contexts for basic block
vim +6907 fs/ext4/super.c
6885
6886 /* Read data from quotafile - avoid pagecache and such because we cannot afford
6887 * acquiring the locks... As quota files are never truncated and quota code
6888 * itself serializes the operations (and no one else should touch the files)
6889 * we don't have to be afraid of races */
6890 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6891 size_t len, loff_t off)
6892 {
6893 struct inode *inode = sb_dqopt(sb)->files[type];
6894 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6895 int offset = off & (sb->s_blocksize - 1);
6896 int tocopy;
6897 size_t toread;
6898 struct buffer_head *bh;
6899 loff_t i_size = i_size_read(inode);
6900
6901 if (off > i_size)
6902 return 0;
6903 if (off+len > i_size)
6904 len = i_size-off;
6905 toread = len;
6906 while (toread > 0) {
> 6907 tocopy = min(sb->s_blocksize - offset, toread);
6908 bh = ext4_bread(NULL, inode, blk, 0);
6909 if (IS_ERR(bh))
6910 return PTR_ERR(bh);
6911 if (!bh) /* A hole? */
6912 memset(data, 0, tocopy);
6913 else
6914 memcpy(data, bh->b_data+offset, tocopy);
6915 brelse(bh);
6916 offset = 0;
6917 toread -= tocopy;
6918 data += tocopy;
6919 blk++;
6920 }
6921 return len;
6922 }
6923
--
0-DAY CI Kernel Test Service
https://01.org/lkp