Skip to contentSkip to conversation

kbuild: remove gcc's -Wtype-limits

PATCH Superseded Vincent Mailhol 7 participants 23 messages Dec 20, 2025 – Jan 5

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover kbuild: remove gcc's -Wtype-limits

I often read on the mailing list people saying "who cares about W=2 builds anyway?". At least I do. Not that I want to fix all of them, but on some occasions, such as new driver submissions, I have often found a couple valid diagnostics in the W=2 output.

That said, the annoying thing is that W=2 is heavily polluted by one
warning: -Wtype-limits. Try a gcc W=2 build on any file and see the results for yourself. I suspect this to be the reason why so few people are using W=2.

This series removes gcc's -Wtype-limits in an attempt to make W=2 more useful. Those who do not use W=2 can continue to not use it if they want. Those who, like me, use it for time to time will get an improved experience from the reduced spam.

Patch #1 deactivates -Wtype-limits. Extra details on statistics, past attempts and alternatives are given in the description.

Patch #2 clean-ups the local kbuild -Wno-type-limits exceptions, while patch #3 undoes a local workaround which silenced that warning by uglifying the code.

---
Changes in v3:

- Remove patch #4.

Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:

- Add two more patches to clean up some -Wtype-limits workarounds
- Collect the Reviewed-by tags.

Link to v1: https://lore.kernel.org/r/[email protected]

---
Vincent Mailhol (3):
kbuild: remove gcc's -Wtype-limits
kbuild: cleanup local -Wno-type-limits exceptions
overflow: Remove is_non_negative() and is_negative()

 drivers/gpu/drm/Makefile |  1 -
 fs/btrfs/Makefile        |  1 -
 include/linux/overflow.h | 10 ++--------
 scripts/Makefile.warn    |  4 +++-
 4 files changed, 5 insertions(+), 11 deletions(-)

---
base-commit: 3e7f562e20ee87a25e104ef4fce557d39d62fa85
change-id: 20251205-remove_wtype-limits-c77eb46d09c2

Best regards,

Signature · 1 line
Vincent Mailhol <[email protected]>
#1 lore raw

PATCH v3 1/3 kbuild: remove gcc's -Wtype-limits

W=2 builds are heavily polluted by the -Wtype-limits warning.

Here are some W=12 statistics on Linux v6.19-rc1 for an x86_64 defconfig (with just CONFIG_WERROR set to "n") using gcc 14.3.1:

	 Warning name			count	percent
	-------------------------------------------------
	 -Wlogical-op			    2	  0.00 %
	 -Wmaybe-uninitialized		  138	  0.20 %
	 -Wunused-macros		  869	  1.24 %
	 -Wmissing-field-initializers	 1418	  2.02 %
	 -Wshadow			 2234	  3.19 %
	 -Wtype-limits			65378	 93.35 %
	-------------------------------------------------
	 Total				70039	100.00 %

As we can see, -Wtype-limits represents the vast majority of all warnings. The reason behind this is that these warnings appear in some common header files, meaning that some unique warnings are repeated tens of thousands of times (once per header inclusion).

Add to this the fact that each warning is coupled with a dozen lines detailing some macro expansion. The end result is that the W=2 output is just too bloated and painful to use.

Three years ago, I proposed in [1] modifying one such header to silence that noise. Because the code was not faulty, Linus rejected the idea and instead suggested simply removing that warning.

At that time, I could not bring myself to send such a patch because, despite its problems, -Wtype-limits would still catch the below bug:

	unsigned int ret;

	ret = check();
	if (ret < 0)
		error();

Meanwhile, based on another suggestion from Linus, I added a new check to sparse [2] that would catch the above bug without the useless spam.

With this, remove gcc's -Wtype-limits. People who still want to catch incorrect comparisons between unsigned integers and zero can now use sparse instead.

On a side note, clang also has a -Wtype-limits warning but:

* it is not enabled in the kernel at the moment because, contrary to gcc, clang did not include it under -Wextra.

* it does not warn if the code results from a macro expansion. So, if activated, it would not cause as much spam as gcc does.

* -Wtype-limits is split into four sub-warnings [3] meaning that if it were to be activated, we could select which one to keep.

So there is no present need to explicitly disable -Wtype-limits in clang.

[1] linux/bits.h: GENMASK_INPUT_CHECK: reduce W=2 noise by 31% treewide

[2] Warn about "unsigned value that used to be signed against zero"

[3] clang's -Wtype-limits

---
 scripts/Makefile.warn | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/Makefile.warn +3 -1
index 68e6fafcb80c..c593ab1257de 100644
@@ -55,6 +55,9 @@ else
 KBUILD_CFLAGS += -Wno-main
 endif
 
+# Too noisy on range checks and in macros handling both signed and unsigned.
+KBUILD_CFLAGS += -Wno-type-limits
+
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
 
@@ -174,7 +177,6 @@ else
 
 # The following turn off the warnings enabled by -Wextra
 KBUILD_CFLAGS += -Wno-missing-field-initializers
-KBUILD_CFLAGS += -Wno-type-limits
 KBUILD_CFLAGS += -Wno-shift-negative-value
 
 ifdef CONFIG_CC_IS_CLANG
Signature · 1 line
2.51.2
#2 lore raw

PATCH v3 2/3 kbuild: cleanup local -Wno-type-limits exceptions

In reply to Vincent Mailhol

Now that -Wtype-limits is globally deactivated, there is no need for local exceptions anymore.

---
Changelog:

v1 -> v2: small change in patch description

---
 drivers/gpu/drm/Makefile | 1 -
 fs/btrfs/Makefile        | 1 -
 2 files changed, 2 deletions(-)
drivers/gpu/drm/Makefile +0 -1
index 0e1c668b46d2..b879a60ca79a 100644
@@ -22,7 +22,6 @@ subdir-ccflags-y += $(call cc-option, -Wstringop-truncation)
 # The following turn off the warnings enabled by -Wextra
 ifeq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
 subdir-ccflags-y += -Wno-missing-field-initializers
-subdir-ccflags-y += -Wno-type-limits
 subdir-ccflags-y += -Wno-shift-negative-value
 endif
 ifeq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
fs/btrfs/Makefile +0 -1
index 743d7677b175..40bc2f7e6f6b 100644
@@ -17,7 +17,6 @@ subdir-ccflags-y += $(condflags)
 # The following turn off the warnings enabled by -Wextra
 subdir-ccflags-y += -Wno-missing-field-initializers
 subdir-ccflags-y += -Wno-sign-compare
-subdir-ccflags-y += -Wno-type-limits
 subdir-ccflags-y += -Wno-shift-negative-value
 
 obj-$(CONFIG_BTRFS_FS) := btrfs.o
Signature · 1 line
2.51.2
#4 lore raw

PATCH v3 3/3 overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol

The is_non_negative() and is_negative() function-like macros just exist as a workaround to silence the -Wtype-limits warning. Now that this warning is disabled, those two macros have lost their raison d'être. Remove them.

This reverts commit dc7fe518b049 ("overflow: Fix -Wtype-limits compilation warnings").

---
Changelog:

v1 -> v2: new patch

---
 include/linux/overflow.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
include/linux/overflow.h +2 -8
index 736f633b2d5f..ab142d60c6b5 100644
@@ -36,12 +36,6 @@
 #define __type_min(T) ((T)((T)-type_max(T)-(T)1))
 #define type_min(t)	__type_min(typeof(t))
 
-/*
- * Avoids triggering -Wtype-limits compilation warning,
- * while using unsigned data types to check a < 0.
- */
-#define is_non_negative(a) ((a) > 0 || (a) == 0)
-#define is_negative(a) (!(is_non_negative(a)))
 
 /*
  * Allows for effectively applying __must_check to a macro so we can have
@@ -201,9 +195,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
 	typeof(d) _d = d;						\
 	unsigned long long _a_full = _a;				\
 	unsigned int _to_shift =					\
-		is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0;	\
+		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
 	*_d = (_a_full << _to_shift);					\
-	(_to_shift != _s || is_negative(*_d) || is_negative(_a) ||	\
+	(_to_shift != _s || *_d < 0 || _a < 0 ||			\
 	(*_d >> _to_shift) != _a);					\
 }))
 
Signature · 1 line
2.51.2
maintainer Reviewed-by on v3 3/3
#5 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

Quoted 11 lines · Vincent Mailhol

The is_non_negative() and is_negative() function-like macros just exist as a workaround to silence the -Wtype-limits warning. Now that this warning is disabled, those two macros have lost their raison d'être. Remove them.

This reverts commit dc7fe518b049 ("overflow: Fix -Wtype-limits compilation warnings").

Quoted 8 lines

---
Changelog:

v1 -> v2: new patch

---
 include/linux/overflow.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

Thanks!

maintainer Reviewed-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] kbuild: cleanup local -Wno-type-limits exceptions

In reply to Vincent Mailhol
Quoted 5 lines · Vincent Mailhol

Now that -Wtype-limits is globally deactivated, there is no need for local exceptions anymore.

Quoted 9 lines

---
Changelog:

v1 -> v2: small change in patch description

---
 drivers/gpu/drm/Makefile | 1 -
 fs/btrfs/Makefile        | 1 -
 2 files changed, 2 deletions(-)
#6 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
kernel test robot noticed the following build warnings:

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: x86_64-randconfig-161-20251222 (https://download.01.org/0day-ci/archive/20251222/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

smatch warnings:
fs/libfs.c:1628 generic_check_addressable() warn: unsigned '*_d' is never less than zero. fs/libfs.c:1628 generic_check_addressable() warn: unsigned '_a' is never less than zero. mm/vmalloc.c:4708 remap_vmalloc_range_partial() warn: unsigned '*_d' is never less than zero. mm/vmalloc.c:4708 remap_vmalloc_range_partial() warn: unsigned '_a' is never less than zero.

vim +1628 fs/libfs.c

1b061d9247f71c Christoph Hellwig   2010-05-26  1613  
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1614  /**
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1615   * generic_check_addressable - Check addressability of file system
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1616   * @blocksize_bits:	log of file system block size
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1617   * @num_blocks:		number of blocks in file system
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1618   *
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1619   * Determine whether a file system with @num_blocks blocks (and a
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1620   * block size of 2**@blocksize_bits) is addressable by the sector_t
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1621   * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1622   */
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1623  int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1624  {
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1625  	u64 last_fs_block = num_blocks - 1;
25050181b61aa0 Pankaj Raghav       2025-06-30  1626  	u64 last_fs_page, max_bytes;
25050181b61aa0 Pankaj Raghav       2025-06-30  1627  
25050181b61aa0 Pankaj Raghav       2025-06-30 @1628  	if (check_shl_overflow(num_blocks, blocksize_bits, &max_bytes))
25050181b61aa0 Pankaj Raghav       2025-06-30  1629  		return -EFBIG;
25050181b61aa0 Pankaj Raghav       2025-06-30  1630  
25050181b61aa0 Pankaj Raghav       2025-06-30  1631  	last_fs_page = (max_bytes >> PAGE_SHIFT) - 1;
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1632  
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1633  	if (unlikely(num_blocks == 0))
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1634  		return 0;
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1635  
25050181b61aa0 Pankaj Raghav       2025-06-30  1636  	if (blocksize_bits < 9)
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1637  		return -EINVAL;
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1638  
a33f13efe05192 Joel Becker         2010-08-16  1639  	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
a33f13efe05192 Joel Becker         2010-08-16  1640  	    (last_fs_page > (pgoff_t)(~0ULL))) {
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1641  		return -EFBIG;
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1642  	}
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1643  	return 0;
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1644  }
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1645  EXPORT_SYMBOL(generic_check_addressable);
30ca22c70e3ef0 Patrick J. LoPresti 2010-07-22  1646  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

Quoted 18 lines · kernel test robot

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: x86_64-randconfig-161-20251222 (https://download.01.org/0day-ci/archive/20251222/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 5 lines

smatch warnings:
fs/libfs.c:1628 generic_check_addressable() warn: unsigned '*_d' is never less than zero. fs/libfs.c:1628 generic_check_addressable() warn: unsigned '_a' is never less than zero. mm/vmalloc.c:4708 remap_vmalloc_range_partial() warn: unsigned '*_d' is never less than zero. mm/vmalloc.c:4708 remap_vmalloc_range_partial() warn: unsigned '_a' is never less than zero.

So smatch is not able to distinguish when the comparison comes from a macro expansion.

Can this warning be ignored? Or should I remove this 3rd patch from the series (and go back to v1)? My choice would be to ignore this warning.

Yours sincerely,
Vincent Mailhol

#8 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
kernel test robot noticed the following build warnings:

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: i386-randconfig-141-20251222 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

New smatch warnings:
block/blk-settings.c:702 blk_stack_atomic_writes_chunk_sectors() warn: unsigned '*_d' is never less than zero. block/blk-settings.c:702 blk_stack_atomic_writes_chunk_sectors() warn: unsigned '_a' is never less than zero. drivers/nvme/host/core.c:3353 nvme_mps_to_sectors() warn: unsigned '*_d' is never less than zero. drivers/nvme/host/core.c:3353 nvme_mps_to_sectors() warn: unsigned '_a' is never less than zero.

Old smatch warnings:
drivers/nvme/host/core.c:5032 nvme_free_cels() warn: iterator 'i' not incremented

vim +702 block/blk-settings.c

d7f36dc446e894 John Garry 2024-11-18  689  
63d092d1c1b1f7 John Garry 2025-07-11  690  static void blk_stack_atomic_writes_chunk_sectors(struct queue_limits *t)
d7f36dc446e894 John Garry 2024-11-18  691  {
63d092d1c1b1f7 John Garry 2025-07-11  692  	unsigned int chunk_bytes;
d7f36dc446e894 John Garry 2024-11-18  693  
63d092d1c1b1f7 John Garry 2025-07-11  694  	if (!t->chunk_sectors)
63d092d1c1b1f7 John Garry 2025-07-11  695  		return;
63d092d1c1b1f7 John Garry 2025-07-11  696  
63d092d1c1b1f7 John Garry 2025-07-11  697  	/*
63d092d1c1b1f7 John Garry 2025-07-11  698  	 * If chunk sectors is so large that its value in bytes overflows
63d092d1c1b1f7 John Garry 2025-07-11  699  	 * UINT_MAX, then just shift it down so it definitely will fit.
63d092d1c1b1f7 John Garry 2025-07-11  700  	 * We don't support atomic writes of such a large size anyway.
63d092d1c1b1f7 John Garry 2025-07-11  701  	 */
63d092d1c1b1f7 John Garry 2025-07-11 @702  	if (check_shl_overflow(t->chunk_sectors, SECTOR_SHIFT, &chunk_bytes))
63d092d1c1b1f7 John Garry 2025-07-11  703  		chunk_bytes = t->chunk_sectors;
d7f36dc446e894 John Garry 2024-11-18  704  
d7f36dc446e894 John Garry 2024-11-18  705  	/*
d7f36dc446e894 John Garry 2024-11-18  706  	 * Find values for limits which work for chunk size.
d7f36dc446e894 John Garry 2024-11-18  707  	 * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk
63d092d1c1b1f7 John Garry 2025-07-11  708  	 * size, as the chunk size is not restricted to a power-of-2.
d7f36dc446e894 John Garry 2024-11-18  709  	 * So we need to find highest power-of-2 which works for the chunk
d7f36dc446e894 John Garry 2024-11-18  710  	 * size.
63d092d1c1b1f7 John Garry 2025-07-11  711  	 * As an example scenario, we could have t->unit_max = 16K and
63d092d1c1b1f7 John Garry 2025-07-11  712  	 * t->chunk_sectors = 24KB. For this case, reduce t->unit_max to a
63d092d1c1b1f7 John Garry 2025-07-11  713  	 * value aligned with both limits, i.e. 8K in this example.
d7f36dc446e894 John Garry 2024-11-18  714  	 */
63d092d1c1b1f7 John Garry 2025-07-11  715  	t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max,
63d092d1c1b1f7 John Garry 2025-07-11  716  					max_pow_of_two_factor(chunk_bytes));
d7f36dc446e894 John Garry 2024-11-18  717  
63d092d1c1b1f7 John Garry 2025-07-11  718  	t->atomic_write_hw_unit_min = min(t->atomic_write_hw_unit_min,
d7f36dc446e894 John Garry 2024-11-18  719  					  t->atomic_write_hw_unit_max);
63d092d1c1b1f7 John Garry 2025-07-11  720  	t->atomic_write_hw_max = min(t->atomic_write_hw_max, chunk_bytes);
63d092d1c1b1f7 John Garry 2025-07-11  721  }
d7f36dc446e894 John Garry 2024-11-18  722  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
kernel test robot noticed the following build warnings:

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: i386-randconfig-141-20251225 (https://download.01.org/0day-ci/archive/20251225/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

smatch warnings:
drivers/md/dm-stripe.c:463 stripe_io_hints() warn: unsigned '*_d' is never less than zero. drivers/md/dm-stripe.c:463 stripe_io_hints() warn: unsigned '_a' is never less than zero.

vim +463 drivers/md/dm-stripe.c

af4874e03ed82f Mike Snitzer    2009-06-22  454  
40bea431274c24 Mike Snitzer    2009-09-04  455  static void stripe_io_hints(struct dm_target *ti,
40bea431274c24 Mike Snitzer    2009-09-04  456  			    struct queue_limits *limits)
40bea431274c24 Mike Snitzer    2009-09-04  457  {
40bea431274c24 Mike Snitzer    2009-09-04  458  	struct stripe_c *sc = ti->private;
1071d560afb4c2 Mikulas Patocka 2025-08-11  459  	unsigned int io_min, io_opt;
40bea431274c24 Mike Snitzer    2009-09-04  460  
5fb9d4341b782a John Garry      2025-07-11  461  	limits->chunk_sectors = sc->chunk_size;
1071d560afb4c2 Mikulas Patocka 2025-08-11  462  
1071d560afb4c2 Mikulas Patocka 2025-08-11 @463  	if (!check_shl_overflow(sc->chunk_size, SECTOR_SHIFT, &io_min) &&
1071d560afb4c2 Mikulas Patocka 2025-08-11  464  	    !check_mul_overflow(io_min, sc->stripes, &io_opt)) {
1071d560afb4c2 Mikulas Patocka 2025-08-11  465  		limits->io_min = io_min;
1071d560afb4c2 Mikulas Patocka 2025-08-11  466  		limits->io_opt = io_opt;
1071d560afb4c2 Mikulas Patocka 2025-08-11  467  	}
40bea431274c24 Mike Snitzer    2009-09-04  468  }

40bea431274c24 Mike Snitzer 2009-09-04 469

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#10 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
kernel test robot noticed the following build warnings:

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: sparc-randconfig-r072-20251227 (https://download.01.org/0day-ci/archive/20251227/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 15.1.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

smatch warnings:
drivers/block/nbd.c:1612 __nbd_ioctl() warn: unsigned '_a' is never less than zero.

vim +/_a +1612 drivers/block/nbd.c

55313e92bd17a87 Mike Christie     2019-08-13  1591  
9442b739207aab6 Josef Bacik       2017-02-07  1592  /* Must be called with config_lock held */
9442b739207aab6 Josef Bacik       2017-02-07  1593  static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
9442b739207aab6 Josef Bacik       2017-02-07  1594  		       unsigned int cmd, unsigned long arg)
9442b739207aab6 Josef Bacik       2017-02-07  1595  {
5ea8d10802ec4c1 Josef Bacik       2017-04-06  1596  	struct nbd_config *config = nbd->config;
fad7cd3310db309 Baokun Li         2021-08-04  1597  	loff_t bytesize;
5ea8d10802ec4c1 Josef Bacik       2017-04-06  1598  
9442b739207aab6 Josef Bacik       2017-02-07  1599  	switch (cmd) {
9442b739207aab6 Josef Bacik       2017-02-07  1600  	case NBD_DISCONNECT:
29eaadc0364943b Josef Bacik       2017-04-06  1601  		return nbd_disconnect(nbd);
9442b739207aab6 Josef Bacik       2017-02-07  1602  	case NBD_CLEAR_SOCK:
0c1c9a27ce909e3 Christoph Hellwig 2023-08-11  1603  		nbd_clear_sock_ioctl(nbd);
29eaadc0364943b Josef Bacik       2017-04-06  1604  		return 0;
9442b739207aab6 Josef Bacik       2017-02-07  1605  	case NBD_SET_SOCK:
e46c7287b1c2768 Josef Bacik       2017-04-06  1606  		return nbd_add_socket(nbd, arg, false);
9442b739207aab6 Josef Bacik       2017-02-07  1607  	case NBD_SET_BLKSIZE:
dcbddf541f18e36 Christoph Hellwig 2020-11-16  1608  		return nbd_set_size(nbd, config->bytesize, arg);
9442b739207aab6 Josef Bacik       2017-02-07  1609  	case NBD_SET_SIZE:
41e76c6a3c83c85 Nick Desaulniers  2021-09-20  1610  		return nbd_set_size(nbd, arg, nbd_blksize(config));
9442b739207aab6 Josef Bacik       2017-02-07  1611  	case NBD_SET_SIZE_BLOCKS:
41e76c6a3c83c85 Nick Desaulniers  2021-09-20 @1612  		if (check_shl_overflow(arg, config->blksize_bits, &bytesize))
fad7cd3310db309 Baokun Li         2021-08-04  1613  			return -EINVAL;
41e76c6a3c83c85 Nick Desaulniers  2021-09-20  1614  		return nbd_set_size(nbd, bytesize, nbd_blksize(config));
9442b739207aab6 Josef Bacik       2017-02-07  1615  	case NBD_SET_TIMEOUT:
55313e92bd17a87 Mike Christie     2019-08-13  1616  		nbd_set_cmd_timeout(nbd, arg);
9442b739207aab6 Josef Bacik       2017-02-07  1617  		return 0;
9442b739207aab6 Josef Bacik       2017-02-07  1618  
9442b739207aab6 Josef Bacik       2017-02-07  1619  	case NBD_SET_FLAGS:
5ea8d10802ec4c1 Josef Bacik       2017-04-06  1620  		config->flags = arg;
9442b739207aab6 Josef Bacik       2017-02-07  1621  		return 0;
9442b739207aab6 Josef Bacik       2017-02-07  1622  	case NBD_DO_IT:
2a852a693f8839b Christoph Hellwig 2022-03-30  1623  		return nbd_start_device_ioctl(nbd);
^1da177e4c3f415 Linus Torvalds    2005-04-16  1624  	case NBD_CLEAR_QUE:
4b2f0260c74324a Herbert Xu        2006-01-06  1625  		/*
4b2f0260c74324a Herbert Xu        2006-01-06  1626  		 * This is for compatibility only.  The queue is always cleared
4b2f0260c74324a Herbert Xu        2006-01-06  1627  		 * by NBD_DO_IT or NBD_CLEAR_SOCK.
4b2f0260c74324a Herbert Xu        2006-01-06  1628  		 */
^1da177e4c3f415 Linus Torvalds    2005-04-16  1629  		return 0;
^1da177e4c3f415 Linus Torvalds    2005-04-16  1630  	case NBD_PRINT_DEBUG:
fd8383fd88a2fd8 Josef Bacik       2016-09-08  1631  		/*
fd8383fd88a2fd8 Josef Bacik       2016-09-08  1632  		 * For compatibility only, we no longer keep a list of
fd8383fd88a2fd8 Josef Bacik       2016-09-08  1633  		 * outstanding requests.
fd8383fd88a2fd8 Josef Bacik       2016-09-08  1634  		 */
^1da177e4c3f415 Linus Torvalds    2005-04-16  1635  		return 0;
^1da177e4c3f415 Linus Torvalds    2005-04-16  1636  	}
1a2ad21128bb4eb Pavel Machek      2009-04-02  1637  	return -ENOTTY;
1a2ad21128bb4eb Pavel Machek      2009-04-02  1638  }
1a2ad21128bb4eb Pavel Machek      2009-04-02  1639  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
kernel test robot noticed the following build warnings:

Hi Vincent,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 3e7f562e20ee87a25e104ef4fce557d39d62fa85]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/kbuild-remove-gcc-s-Wtype-limits/20251220-190509
base:   3e7f562e20ee87a25e104ef4fce557d39d62fa85
patch link:    https://lore.kernel.org/r/20251220-remove_wtype-limits-v3-3-24b170af700e%40kernel.org
patch subject: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()
config: arm-randconfig-r071-20251224 (https://download.01.org/0day-ci/archive/20251228/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 8.5.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

smatch warnings:
fs/xfs/xfs_mount.c:144 xfs_sb_validate_fsb_count() warn: unsigned '*_d' is never less than zero. fs/xfs/xfs_mount.c:144 xfs_sb_validate_fsb_count() warn: unsigned '_a' is never less than zero.

vim +144 fs/xfs/xfs_mount.c

27174203f570b9 Christoph Hellwig 2009-03-30  130  
4cc929ee305c69 Nathan Scott      2007-05-14  131  /*
4cc929ee305c69 Nathan Scott      2007-05-14  132   * Check size of device based on the (data/realtime) block count.
4cc929ee305c69 Nathan Scott      2007-05-14  133   * Note: this check is used by the growfs code as well as mount.
4cc929ee305c69 Nathan Scott      2007-05-14  134   */
4cc929ee305c69 Nathan Scott      2007-05-14  135  int
4cc929ee305c69 Nathan Scott      2007-05-14  136  xfs_sb_validate_fsb_count(
4cc929ee305c69 Nathan Scott      2007-05-14  137  	xfs_sb_t	*sbp,
c8ce540db5f67d Darrick J. Wong   2017-06-16  138  	uint64_t	nblocks)
4cc929ee305c69 Nathan Scott      2007-05-14  139  {
cebf9dacd5c3ce Pankaj Raghav     2024-08-22  140  	uint64_t		max_bytes;
cebf9dacd5c3ce Pankaj Raghav     2024-08-22  141  
4cc929ee305c69 Nathan Scott      2007-05-14  142  	ASSERT(sbp->sb_blocklog >= BBSHIFT);
4cc929ee305c69 Nathan Scott      2007-05-14  143  
cebf9dacd5c3ce Pankaj Raghav     2024-08-22 @144  	if (check_shl_overflow(nblocks, sbp->sb_blocklog, &max_bytes))
cebf9dacd5c3ce Pankaj Raghav     2024-08-22  145  		return -EFBIG;
cebf9dacd5c3ce Pankaj Raghav     2024-08-22  146  
d5cf09baced0ef Christoph Hellwig 2014-07-30  147  	/* Limited by ULONG_MAX of page cache index */
cebf9dacd5c3ce Pankaj Raghav     2024-08-22  148  	if (max_bytes >> PAGE_SHIFT > ULONG_MAX)
2451337dd04390 Dave Chinner      2014-06-25  149  		return -EFBIG;
4cc929ee305c69 Nathan Scott      2007-05-14  150  	return 0;
4cc929ee305c69 Nathan Scott      2007-05-14  151  }
^1da177e4c3f41 Linus Torvalds    2005-04-16  152  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#12 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

In reply to Vincent Mailhol
Quoted 11 lines · Vincent Mailhol

The is_non_negative() and is_negative() function-like macros just exist as a workaround to silence the -Wtype-limits warning. Now that this warning is disabled, those two macros have lost their raison d'être. Remove them.

This reverts commit dc7fe518b049 ("overflow: Fix -Wtype-limits compilation warnings").

So at the end, this patch got five kernel test robot reports:

https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

All these are the same smatch warning just triggered from a different place. I think it is still too early to undo that workaround in include/linux/overflow.h, otherwise developers would be getting that smatch report too often.

I will send a v4 in which I will drop this patch. This basically means that the v4 is a revert to v1...

Quoted 28 lines

---
Changelog:

v1 -> v2: new patch

---
 include/linux/overflow.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
include/linux/overflow.h +0 -6
index 736f633b2d5f..ab142d60c6b5 100644
@@ -36,12 +36,6 @@
 #define __type_min(T) ((T)((T)-type_max(T)-(T)1))
 #define type_min(t)	__type_min(typeof(t))
 
-/*
- * Avoids triggering -Wtype-limits compilation warning,
- * while using unsigned data types to check a < 0.
- */
-#define is_non_negative(a) ((a) > 0 || (a) == 0)
-#define is_negative(a) (!(is_non_negative(a)))
 
 /*
  * Allows for effectively applying __must_check to a macro so we can have
@@ -201,9 +195,9 @@ static inline bool __must_check __must_check_overflow(bool overflow)
 	typeof(d) _d = d;						\

include/linux/overflow.h static inline bool __must_check __must_check_overflow(bool overflow) · line 203

Quoted 10 lines
include/linux/overflow.h +2 -2
 	unsigned long long _a_full = _a;				\
 	unsigned int _to_shift =					\
-		is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0;	\
+		_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0;		\
 	*_d = (_a_full << _to_shift);					\
-	(_to_shift != _s || is_negative(*_d) || is_negative(_a) ||	\
+	(_to_shift != _s || *_d < 0 || _a < 0 ||			\
 	(*_d >> _to_shift) != _a);					\
 }))
 

Yours sincerely,
Vincent Mailhol

#13 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

Quoted 21 lines · Vincent Mailhol
Quoted 11 lines · Vincent Mailhol

The is_non_negative() and is_negative() function-like macros just exist as a workaround to silence the -Wtype-limits warning. Now that this warning is disabled, those two macros have lost their raison d'être. Remove them.

This reverts commit dc7fe518b049 ("overflow: Fix -Wtype-limits compilation warnings").

So at the end, this patch got five kernel test robot reports:

https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

All these are the same smatch warning just triggered from a different place. I think it is still too early to undo that workaround in include/linux/overflow.h, otherwise developers would be getting that smatch report too often.

I will send a v4 in which I will drop this patch. This basically means that the v4 is a revert to v1...

thanks! I think it's a bit sad to keep code only to make some checker tooling happy, but for now it seems to be the right thing to do.

Kind Regards,
Nicolas

#14 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

Quoted 3 lines · Nicolas Schier

thanks! I think it's a bit sad to keep code only to make some checker tooling happy, but for now it seems to be the right thing to do.

Perhaps a patch to add a comment explaining Vincent's findings would be a good outcome, i.e. explaining the reason it needs to remain in place for the moment (even a link to lore.kernel.org to this thread would help).

Cheers,
Miguel

#15 lore raw

Re: [PATCH v3 3/3] overflow: Remove is_non_negative() and is_negative()

Quoted 8 lines · Miguel Ojeda
Quoted 3 lines · Nicolas Schier

thanks! I think it's a bit sad to keep code only to make some checker tooling happy, but for now it seems to be the right thing to do.

Perhaps a patch to add a comment explaining Vincent's findings would be a good outcome, i.e. explaining the reason it needs to remain in place for the moment

OK. But I will send this as a separate patch as a reply to this thread so that this can be discussed separately without having to respin the main series again and again. I will add it back to the main series only if it get a decent level of Acked-by tags.

Quoted 1 line

(even a link to lore.kernel.org to this thread would help).

It is rather uncommon to add lore.kernel.org links in the code comment. But I am not against. I will do as you suggested so and see what people think of it.

Yours sincerely,
Vincent Mailhol

#16 lore raw

PATCH 1/1 overflow: Update is_non_negative() and is_negative() comment

The is_non_negative() and is_negative() function-like macros initially exist as a workaround to silence the -Wtype-limits warning. Now that this warning is disabled, those two macros have lost their raison d'être. Or so we thought.

In reality, smatch still produces a similar warning and so, it is unfortunately still too early to undo this workaround.

Update the comment to point to smatch instead of GCC's -Wtype-limits. Add a link to the thread in which this was discovered.

---
 include/linux/overflow.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/overflow.h +2 -2
index 736f633b2d5f..d84194fc783b 100644
@@ -37,8 +37,8 @@
 #define type_min(t)	__type_min(typeof(t))
 
 /*
- * Avoids triggering -Wtype-limits compilation warning,
- * while using unsigned data types to check a < 0.
+ * Avoids triggering "unsigned 'a' is never less than zero" smatch warning,
+ * Link: https://lore.kernel.org/all/[email protected]
  */
 #define is_non_negative(a) ((a) > 0 || (a) == 0)
 #define is_negative(a) (!(is_non_negative(a)))
Signature · 1 line
2.52.0
#17 lore raw

Re: [PATCH] overflow: Update is_non_negative() and is_negative() comment

Thanks Randy, for sending this to me. I'm on the sparse list, but I've been on vacation and haven't caught up with my email. I can easily silence this in Smatch.

regards,
dan carpenter

check_unsigned_lt_zero.c +2 -1
index bfeb3261f91d..ac3e650704ce 100644
@@ -105,7 +105,8 @@ static bool is_allowed_zero(struct expression *expr)
 	    strcmp(macro, "STRTO_H") == 0 ||
 	    strcmp(macro, "SUB_EXTEND_USTAT") == 0 ||
 	    strcmp(macro, "TEST_CASTABLE_TO_TYPE_VAR") == 0 ||
-	    strcmp(macro, "TEST_ONE_SHIFT") == 0)
+	    strcmp(macro, "TEST_ONE_SHIFT") == 0 ||
+	    strcmp(macro, "check_shl_overflow") == 0)
 		return true;
 	return false;
 }
author Tested-by
#18 lore raw

Re: [PATCH] overflow: Update is_non_negative() and is_negative() comment

Quoted 2 lines · Dan Carpenter

Thanks Randy, for sending this to me. I'm on the sparse list, but I've been on vacation and haven't caught up with my email.

Welcome back, hope you enjoyed your holidays!

Quoted 1 line

I can easily silence this in Smatch.

Thanks. I ran this locally, I can confirm that this silences the warning. So:

check_unsigned_lt_zero.c static bool is_allowed_zero(struct expression *expr) · line 109

Quoted 11 lines
check_unsigned_lt_zero.c +2 -1
index bfeb3261f91d..ac3e650704ce 100644
@@ -105,7 +105,8 @@ static bool is_allowed_zero(struct expression *expr)
 	    strcmp(macro, "STRTO_H") == 0 ||
 	    strcmp(macro, "SUB_EXTEND_USTAT") == 0 ||
 	    strcmp(macro, "TEST_CASTABLE_TO_TYPE_VAR") == 0 ||
-	    strcmp(macro, "TEST_ONE_SHIFT") == 0)
+	    strcmp(macro, "TEST_ONE_SHIFT") == 0 ||
+	    strcmp(macro, "check_shl_overflow") == 0)

But, for the long term, wouldn't it better to just ignore all the code coming from macro extensions instead of maintaining this allow-list?

check_unsigned_lt_zero.c static bool is_allowed_zero(struct expression *expr) · line 112

Quoted 3 lines
check_unsigned_lt_zero.c +0 -0
 		return true;
 	return false;
 }

Yours sincerely,
Vincent Mailhol

#19 lore raw

Re: [PATCH] overflow: Update is_non_negative() and is_negative() comment

Quoted 13 lines · Vincent Mailhol
Quoted 2 lines · Dan Carpenter

Thanks Randy, for sending this to me. I'm on the sparse list, but I've been on vacation and haven't caught up with my email.

Welcome back, hope you enjoyed your holidays!

Quoted 1 line

I can easily silence this in Smatch.

Thanks. I ran this locally, I can confirm that this silences the warning. So:

Quoted 15 lines

check_unsigned_lt_zero.c static bool is_allowed_zero(struct expression *expr) · line 109

Quoted 11 lines
check_unsigned_lt_zero.c +2 -1
index bfeb3261f91d..ac3e650704ce 100644
@@ -105,7 +105,8 @@ static bool is_allowed_zero(struct expression *expr)
 	    strcmp(macro, "STRTO_H") == 0 ||
 	    strcmp(macro, "SUB_EXTEND_USTAT") == 0 ||
 	    strcmp(macro, "TEST_CASTABLE_TO_TYPE_VAR") == 0 ||
-	    strcmp(macro, "TEST_ONE_SHIFT") == 0)
+	    strcmp(macro, "TEST_ONE_SHIFT") == 0 ||
+	    strcmp(macro, "check_shl_overflow") == 0)

But, for the long term, wouldn't it better to just ignore all the code coming from macro extensions instead of maintaining this allow-list?

Of course, that idea occured to me, but so far the allow list is not very burdensome to maintain. I maybe should disable it for all macros unless the --spammy option is used...

regards,
dan carpenter

#20 lore raw

Re: [PATCH] overflow: Update is_non_negative() and is_negative() comment

Quoted 30 lines · Dan Carpenter
Quoted 13 lines · Vincent Mailhol
Quoted 2 lines · Dan Carpenter

Thanks Randy, for sending this to me. I'm on the sparse list, but I've been on vacation and haven't caught up with my email.

Welcome back, hope you enjoyed your holidays!

Quoted 1 line

I can easily silence this in Smatch.

Thanks. I ran this locally, I can confirm that this silences the warning. So:

Quoted 15 lines

check_unsigned_lt_zero.c static bool is_allowed_zero(struct expression *expr) · line 109

Quoted 11 lines
check_unsigned_lt_zero.c +2 -1
index bfeb3261f91d..ac3e650704ce 100644
@@ -105,7 +105,8 @@ static bool is_allowed_zero(struct expression *expr)
 	    strcmp(macro, "STRTO_H") == 0 ||
 	    strcmp(macro, "SUB_EXTEND_USTAT") == 0 ||
 	    strcmp(macro, "TEST_CASTABLE_TO_TYPE_VAR") == 0 ||
-	    strcmp(macro, "TEST_ONE_SHIFT") == 0)
+	    strcmp(macro, "TEST_ONE_SHIFT") == 0 ||
+	    strcmp(macro, "check_shl_overflow") == 0)

But, for the long term, wouldn't it better to just ignore all the code coming from macro extensions instead of maintaining this allow-list?

Quoted 2 lines

Of course, that idea occured to me, but so far the allow list is not very burdensome to maintain.

Indeed, but my concern was more on how people would treat such smatch warnings coming from the kernel test robot. It is very uncommon to have an allow-list hard coded into the static analyzer. Actually, this is the first time I see this. My fear here is that people will just uglify the code rather than sending a patch to extend the allow list in smatch.

Quoted 1 line

I maybe should disable it for all macros unless the --spammy option is used...

IMHO, that would be an even better approach. That said, I am happy enough with your previous patch which resolves my issue and which is way better than updating the is_non_negative() and is_negative() comments as I did in my patch!

Yours sincerely,
Vincent Mailhol

#21 lore raw

Re: [PATCH] overflow: Update is_non_negative() and is_negative() comment

Quoted 31 lines · Vincent Mailhol
Quoted 30 lines · Dan Carpenter
Quoted 28 lines · Vincent Mailhol
Quoted 2 lines · Dan Carpenter

Thanks Randy, for sending this to me. I'm on the sparse list, but I've been on vacation and haven't caught up with my email.

Welcome back, hope you enjoyed your holidays!

Quoted 1 line

I can easily silence this in Smatch.

Thanks. I ran this locally, I can confirm that this silences the warning. So:

check_unsigned_lt_zero.c static bool is_allowed_zero(struct expression *expr) · line 109

Quoted 11 lines
check_unsigned_lt_zero.c +2 -1
index bfeb3261f91d..ac3e650704ce 100644
@@ -105,7 +105,8 @@ static bool is_allowed_zero(struct expression *expr)
 	    strcmp(macro, "STRTO_H") == 0 ||
 	    strcmp(macro, "SUB_EXTEND_USTAT") == 0 ||
 	    strcmp(macro, "TEST_CASTABLE_TO_TYPE_VAR") == 0 ||
-	    strcmp(macro, "TEST_ONE_SHIFT") == 0)
+	    strcmp(macro, "TEST_ONE_SHIFT") == 0 ||
+	    strcmp(macro, "check_shl_overflow") == 0)

But, for the long term, wouldn't it better to just ignore all the code coming from macro extensions instead of maintaining this allow-list?

Quoted 9 lines
Quoted 2 lines

Of course, that idea occured to me, but so far the allow list is not very burdensome to maintain.

Indeed, but my concern was more on how people would treat such smatch warnings coming from the kernel test robot. It is very uncommon to have an allow-list hard coded into the static analyzer. Actually, this is the first time I see this. My fear here is that people will just uglify the code rather than sending a patch to extend the allow list in smatch.

People need to learn to ignore false positives. The zero day bot sends a one time email and if you just delete it, then it's gone forever. Forget about it.

It's really limitting to try be totally static checker clean. Things like passing a zero to ERR_PTR() for example. It's a perfectly valid way to return NULL and the fs/ subsystem uses this a lot. But 80% when you see it in new code, then it's a bug where they returned the wrong variable or something. It's a high quality warning. The solution is to look at it one time, when the code is fresh and then never look at it again.

In this case when I disable the unsigned < 0 checking for macros then it means that if someone does if (WARN_ON(u32_var < 0)) { it's silenced. I don't want that.

I could create a much better way to silence false positives like this if I passed an expression pointer to the sm_warning()... It's a bit of a re-work.

regards,
dan carpenter

It's really limitting
if we can only check

Quoted 10 lines
Quoted 1 line

I maybe should disable it for all macros unless the --spammy option is used...

IMHO, that would be an even better approach. That said, I am happy enough with your previous patch which resolves my issue and which is way better than updating the is_non_negative() and is_negative() comments as I did in my patch!

Yours sincerely,
Vincent Mailhol

#22 lore raw
In reply to Vincent Mailhol
Quoted 11 lines · Vincent Mailhol

I often read on the mailing list people saying "who cares about W=2 builds anyway?". At least I do. Not that I want to fix all of them, but on some occasions, such as new driver submissions, I have often found a couple valid diagnostics in the W=2 output.

That said, the annoying thing is that W=2 is heavily polluted by one
warning: -Wtype-limits. Try a gcc W=2 build on any file and see the results for yourself. I suspect this to be the reason why so few people are using W=2.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next

Thanks!

[1/3] kbuild: remove gcc's -Wtype-limits

      https://git.kernel.org/kbuild/c/660e899103e29
[2/3] kbuild: cleanup local -Wno-type-limits exceptions
      https://git.kernel.org/kbuild/c/34a1bd0b6b2c0
[3/3] overflow: Remove is_non_negative() and is_negative()
      https://git.kernel.org/kbuild/c/5ce3218d4f102

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted.

Best regards,

Signature · 1 line
Nathan Chancellor <[email protected]>