[PATCH v3 0/3] Introduce in_range_incl() inclusive range check macro

From: Guru Das Srinagesh

Date: Mon Aug 31 2026 - 16:38:54 EST


Extend the existing in_range() API to provide a user-friendly inclusive
range check and add one illustrative in-tree example of its use.

The in_range() API lends itself easily to callers who care about a
half-open range, but has no straightforward equivalent for callers that
want to check for an inclusive range. Such callers have to resort to
this pattern:

in_range(val, start, (end - start + 1))

which is quite clunky and easy to mess up. Examples of such callers:

- fs/btrfs/extent-io-tree.c hand-computes the inclusive check by
passing "state->end - state->start + 1" as in_range()'s len argument.
- drivers/md/dm-raid.c has its own file-local __within_range(v, min, max)
helper, independently invented, with almost a dozen call sites in
that one file.
- drivers/iio/imu/bmi270/bmi270_core.c has three "in_range(val, 0,
MAX + 1)" checks in bmi270_write_event_value(), computing the +1 by
hand for the same reason.

This series converts only bmi270_core.c as a first step, as an exemplar
for the usage of the new API. Note that similar conversions in other
call sites are not drop-in replacements and have to be reviewed
carefully to ensure the behaviour of the check does not change.

Signed-off-by: Guru Das Srinagesh <linux@xxxxxxxxxxx>
---
Changes in v3:
- (Matthew) Go back to in_range() approach which was earlier incorrectly
abandoned in v2 to avoid the overflow problem with v1's plain wrapper of
in_range() with len = (max - min + 1)
- (Andy) Add Kunit test for in_range_inclusive() covering both u32 and
u64 test cases. Tested with:
./tools/testing/kunit/kunit.py run --arch=x86_64 "in_range*"
- (Andy) Added return value kernel-doc description
- in_range_inclusive() deliberately does not add a BUILD_BUG_ON for
@min > @max nor a signedness check in keeping with the in_range() example.
- AI assistance (Claude, sashiko using Claude CLI as backend) was used
to improve precision in kernel-doc description, refining test cases
and adding more test case classes.
- Rebased to v7.2
- Link to v2: https://patch.msgid.link/20260816-minmax-in-range-incl-v2-0-766f737dd6bf@xxxxxxxxxxx

Changes in v2:
- (Andrew) Rename in_range_incl() to in_range_inclusive()
- Wholly incorrect approach abandoning in_range()

Link to v1: https://patch.msgid.link/20260815-minmax-in-range-incl-v1-0-a75f7d9ae92e@xxxxxxxxxxx

To: Alex Lanzano <lanzano.alex@xxxxxxxxx>
To: Jonathan Cameron <jic23@xxxxxxxxxx>
To: David Lechner <dlechner@xxxxxxxxxxxx>
To: Nuno Sá <nuno.sa@xxxxxxxxxx>
To: Andy Shevchenko <andy@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: linux-iio@xxxxxxxxxxxxxxx

---
Guru Das Srinagesh (3):
minmax: Add in_range_inclusive() for inclusive range checks
lib/tests: Add in_range_inclusive() KUnit test
iio: imu: bmi270: Use in_range_inclusive() in bmi270_write_event_value()

drivers/iio/imu/bmi270/bmi270_core.c | 6 +-
include/linux/minmax.h | 39 ++++++
lib/Kconfig.debug | 18 +++
lib/tests/Makefile | 1 +
lib/tests/in_range_inclusive_kunit.c | 249 +++++++++++++++++++++++++++++++++++
5 files changed, 310 insertions(+), 3 deletions(-)
---
base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260815-minmax-in-range-incl-a25c242be676

Best regards,
--
Guru Das Srinagesh <linux@xxxxxxxxxxx>