[PATCH 10/10] iio: light: tsl2591: Prevent infinite recursion on threshold boundaries

From: Lee Jones

Date: Thu Nov 19 2026 - 10:33:45 EST


tsl2591_set_als_lower_threshold() adjusts the upper threshold to
als_lower_threshold + 1 whenever the new lower threshold is greater than
or equal to the current upper threshold, and
tsl2591_set_als_upper_threshold() symmetrically adjusts the lower
threshold to als_upper_threshold - 1.

Writing 65535 (TSL2591_ALS_MAX_VALUE) to the falling threshold wraps the
16-bit addition to 0 and calls tsl2591_set_als_upper_threshold(chip, 0),
which then wraps 0 - 1 back to 65535 and calls
tsl2591_set_als_lower_threshold(chip, 65535) in an infinite mutual
recursion until the kernel stack overflows. Writing 0 to the rising
threshold triggers the same cycle whenever the lower threshold is
non-zero.

Reject als_lower_threshold == TSL2591_ALS_MAX_VALUE and
als_upper_threshold == 0 with -EINVAL, propagate errors from the
cross-calls, and only update chip->als_settings once the I2C writes
succeed.

Fixes: 2335f0d7c790 ("iio: light: Added AMS tsl2591 driver implementation")
Closes: https://sashiko.dev/#/bug/linux-29f0f993-c799-4717-9daa-d0ad367f4e6d
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/iio/light/tsl2591.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/light/tsl2591.c b/drivers/iio/light/tsl2591.c
index 8cfce720efb9..e0c2b67c7dd4 100644
--- a/drivers/iio/light/tsl2591.c
+++ b/drivers/iio/light/tsl2591.c
@@ -466,23 +466,25 @@ static int tsl2591_set_als_gain_int_time(struct tsl2591_chip *chip)
static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
u16 als_lower_threshold)
{
- struct tsl2591_als_settings als_settings = chip->als_settings;
struct i2c_client *client = chip->client;
u16 als_upper_threshold;
u8 als_lower_l;
u8 als_lower_h;
int ret;

- chip->als_settings.als_lower_thresh = als_lower_threshold;
+ if (als_lower_threshold == TSL2591_ALS_MAX_VALUE)
+ return -EINVAL;

/*
* Lower threshold should not be greater or equal to upper.
* If this is the case, then assert upper threshold to new lower
* threshold + 1 to avoid ordering issues when setting thresholds.
*/
- if (als_lower_threshold >= als_settings.als_upper_thresh) {
+ if (als_lower_threshold >= chip->als_settings.als_upper_thresh) {
als_upper_threshold = als_lower_threshold + 1;
- tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
+ ret = tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
+ if (ret)
+ return ret;
}

als_lower_l = als_lower_threshold;
@@ -504,32 +506,33 @@ static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
return ret;
}

+ chip->als_settings.als_lower_thresh = als_lower_threshold;
+
return 0;
}

static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
u16 als_upper_threshold)
{
- struct tsl2591_als_settings als_settings = chip->als_settings;
struct i2c_client *client = chip->client;
u16 als_lower_threshold;
u8 als_upper_l;
u8 als_upper_h;
int ret;

- if (als_upper_threshold > TSL2591_ALS_MAX_VALUE)
+ if (!als_upper_threshold)
return -EINVAL;

- chip->als_settings.als_upper_thresh = als_upper_threshold;
-
/*
* Upper threshold should not be less than lower. If this
* is the case, then assert lower threshold to new upper
* threshold - 1 to avoid ordering issues when setting thresholds.
*/
- if (als_upper_threshold < als_settings.als_lower_thresh) {
+ if (als_upper_threshold < chip->als_settings.als_lower_thresh) {
als_lower_threshold = als_upper_threshold - 1;
- tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
+ ret = tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
+ if (ret)
+ return ret;
}

als_upper_l = als_upper_threshold;
@@ -551,6 +554,8 @@ static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
return ret;
}

+ chip->als_settings.als_upper_thresh = als_upper_threshold;
+
return 0;
}

--
2.56.1.708.g77570a6a1d-goog