Linux IIO development
 help / color / mirror / Atom feed
From: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
To: Shan Rizvi <shan@rizvi.nu>, Jonathan Cameron <jic23@kernel.org>,
	Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-iio@vger.kernel.org, linux-input@vger.kernel.org
Subject: Re: iio: hid-sensor-trigger: unclamped msleep_interruptible(poll_value * 2) stalls runtime PM for hours on Chicony 04F2:B872 secondary proximity sensor
Date: Sun, 29 Nov 2026 09:54:19 -0800	[thread overview]
Message-ID: <b9ac1fb0e7bf68c915b03f0fca4a56a4412e2191.camel@linux.intel.com> (raw)
In-Reply-To: <179597320466.39.13133294134105261462@rizvi.nu>

[-- Attachment #1: Type: text/plain, Size: 3976 bytes --]

On Sun, 2026-11-29 at 12:26 -0500, Shan Rizvi wrote:
> On Sun, 2026-11-29 at 06:37 -0800, Srinivas Pandruvada wrote:
> > Try this:
> > 
> > Replace:
> >     if (state && poll_value)
> >         msleep_interruptible(poll_value * 2);
> > 
> > with
> > 
> > if (state && poll_value > 0) {
> > 	unsigned long max_sleep_ms = 120 * MSEC_PER_SEC;
> > 	unsigned long sleep_ms;
> > 
> > 	if (IS_ENABLED(CONFIG_DETECT_HUNG_TASK) &&
> > 	    CONFIG_DEFAULT_HUNG_TASK_TIMEOUT > 0)
> > 		max_sleep_ms = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT *
> > MSEC_PER_SEC;
> > 
> > 	if (poll_value * 2 > max_sleep_ms)
> > 		return -EIO;
> > 
> > 	sleep_ms = min_t(u64, (u64)poll_value * 2, max_sleep_ms);
> > 	msleep_interruptible(sleep_ms);
> > }
> 
> Thanks for the quick reply!
> 
> I built and tested this on the HP 8EA2 laptop (with (u64)poll_value *
> 2
> in the if condition as well, since poll_value is s32 and REPORT_ID 17
> declares max=2147483647, so s32 * 2 could overflow).
> 
> Good news:
> - The multi-hour stall and rpm_resume() D-state hang on
>   HID-SENSOR-200011.6.auto are gone, and the working 10 Hz proximity
>   sensor (HID-SENSOR-200011.3.auto) and ALS (HID-SENSOR-
> 200041.4.auto)
>   continue to work normally.
> 
> However, testing turned up a side effect from returning -EIO inside
> _hid_sensor_power_state():
> 
> Every read of HID-SENSOR-200011.6.auto now triggers a runtime PM
> underflow in dmesg:
> 
>   hid_sensor_prox HID-SENSOR-200011.6.auto: Runtime PM usage count
> underflow!
> 
> Tracing why:
> 1. drivers/iio/light/hid-sensor-prox.c:prox_read_raw() (lines 93,
> 103)
>    calls hid_sensor_power_state(..., true) without checking the
> return
>    value, proceeds into sensor_hub_input_attr_get_raw_value() (~5s
> sync
>    timeout -> returns 0), and then unconditionally calls
>    hid_sensor_power_state(..., false). Because
>    pm_runtime_resume_and_get() already dropped the usage count on -
> EIO,
>    the paired pm_runtime_put_autosuspend() underflows on every read.
> 2. Returning -EIO at line 146 also runs after
> sensor_hub_device_open(),
>    atomic_inc(&st->data_ready), powering the sensor to D0/ALL_EVENTS,
>    and atomic_inc(&st->user_requested_state) in
> hid_sensor_power_state()
>    without unwinding them on the error path (affecting callers like
>    buffer_postenable() that do check the return value), and latches
>    dev->power.runtime_error = -EIO.
> 
> Two small additional notes:
> - Capping sleep_ms to CONFIG_DEFAULT_HUNG_TASK_TIMEOUT (120s) could
>   still let a concurrent reader in rpm_resume() trip the 120s hung-
> task
>   warning if a device reports e.g. poll_value = 60000 ms; capping
> lower
>   (or returning 0 after a smaller clamped sleep, e.g. a few seconds)
>   would avoid both the hung-task warning and the -EIO
> unwind/underflow
>   issues above.
> - While unloading the unpatched stock module before this test,
>   modprobe -r hid_sensor_prox also hit the same multi-hour
>   msleep_interruptible() stall via __device_release_driver() ->
>   pm_runtime_get_sync() -> hid_sensor_runtime_resume() once runtime
> PM
>   had previously been enabled on HID-SENSOR-200011.6.auto (woken by
>   SIGINT).
> 
> Would you prefer:
> (a) clamping the sleep in _hid_sensor_power_state() and returning 0
>     (no runtime PM error/underflow, e.g. capping at a few seconds or
>     half of hung_task timeout), or



> (b) keeping the -EIO rejection, checking poll_value before
>     sensor_hub_device_open() (and decrementing user_requested_state
> on
>     failure), plus checking hid_sensor_power_state() return values in
>     hid-sensor-prox (and other hid-sensor-* callers)?
> 
This may be better. Try the attached:

Thanks,
Srinivas

> Happy to test either approach on this hardware and send a patch if
> you'd
> like.
> 
> Thanks,
> Shan Rizvi

[-- Attachment #2: invalid_sensor.diff --]
[-- Type: text/x-patch, Size: 982 bytes --]

diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index cb7713646e21..b995bbe0811f 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -518,6 +518,18 @@ int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
 	int i;
 
 	hid_sensor_get_reporting_interval(hsdev, usage_id, st);
+	value =	hid_sensor_read_poll_value(st);
+	if (value > 0) {
+		unsigned long max_sleep_ms = 120 * MSEC_PER_SEC; /* default hung task timeout */
+
+		if (IS_ENABLED(CONFIG_DETECT_HUNG_TASK) && CONFIG_DEFAULT_HUNG_TASK_TIMEOUT > 0)
+			max_sleep_ms = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT * MSEC_PER_SEC;
+
+		if (value * 2 >= max_sleep_ms) {
+			hid_err(hsdev->hdev, "Invalid sampling interval, will cause task hangs\n");
+			return -EINVAL;
+		}
+	}
 
 	sensor_hub_input_get_attribute_info(hsdev,
 					HID_FEATURE_REPORT, usage_id,

      reply	other threads:[~2026-11-29 17:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  2:03 iio: hid-sensor-trigger: unclamped msleep_interruptible(poll_value * 2) stalls runtime PM for hours on Chicony 04F2:B872 secondary proximity sensor Shan Rizvi
2026-11-29 14:37 ` Srinivas Pandruvada
2026-11-29 17:26   ` Shan Rizvi
2026-11-29 17:54     ` Srinivas Pandruvada [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b9ac1fb0e7bf68c915b03f0fca4a56a4412e2191.camel@linux.intel.com \
    --to=srinivas.pandruvada@linux.intel.com \
    --cc=bentiss@kernel.org \
    --cc=jic23@kernel.org \
    --cc=jikos@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=shan@rizvi.nu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox