Skip to contentSkip to conversation

iio: hid-sensor-trigger: Fix sometimes not powering up the sensor after resume

PATCH Applied Hans de Goede 3 participants 5 messages Apr 14, 2018 – Apr 21, 2018 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: hid-sensor-trigger: Fix sometimes not powering up the sensor after resume

hid_sensor_set_power_work() powers the sensors back up after a resume based on the user_requested_state atomic_t.

But hid_sensor_power_state() treats this as a boolean flag, leading to the following problematic scenario:

1) Some app starts using the iio-sensor in buffered / triggered mode, hid_sensor_data_rdy_trigger_set_state(true) gets called, setting user_requested_state to 1.
2) Something directly accesses a _raw value through sysfs, leading to a call to hid_sensor_power_state(true) followed by hid_sensor_power_state(false) call, this sets user_requested_state to 1 followed by setting it to 0.
3) Suspend/resume the machine, hid_sensor_set_power_work() now does NOT power the sensor back up because user_requested_state (wrongly) is 0. Which stops the app using the sensor in buffered mode from receiving any new values.

This commit changes user_requested_state to a counter tracking how many times hid_sensor_power_state(true) was called instead, fixing this.

Cc Bastien Nocera <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +4 -4
index cfb6588565ba..4905a997a7ec 100644
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 #ifdef CONFIG_PM
 	int ret;
 
-	atomic_set(&st->user_requested_state, state);
-
 	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
 		pm_runtime_enable(&st->pdev->dev);
 
-	if (state)
+	if (state) {
+		atomic_inc(&st->user_requested_state);
 		ret = pm_runtime_get_sync(&st->pdev->dev);
-	else {
+	} else {
+		atomic_dec(&st->user_requested_state);
 		pm_runtime_mark_last_busy(&st->pdev->dev);
 		pm_runtime_use_autosuspend(&st->pdev->dev);
 		ret = pm_runtime_put_autosuspend(&st->pdev->dev);
Signature · 1 line
2.17.0
maintainer
#1 lore raw
Quoted 18 lines · On Sat, 14 Apr 2018 17:09:09 +0200 Hans de Goede <[email protected]> wrote:

hid_sensor_set_power_work() powers the sensors back up after a resume based on the user_requested_state atomic_t.

But hid_sensor_power_state() treats this as a boolean flag, leading to the following problematic scenario:

1) Some app starts using the iio-sensor in buffered / triggered mode, hid_sensor_data_rdy_trigger_set_state(true) gets called, setting user_requested_state to 1.
2) Something directly accesses a _raw value through sysfs, leading to a call to hid_sensor_power_state(true) followed by hid_sensor_power_state(false) call, this sets user_requested_state to 1 followed by setting it to 0.
3) Suspend/resume the machine, hid_sensor_set_power_work() now does NOT power the sensor back up because user_requested_state (wrongly) is 0. Which stops the app using the sensor in buffered mode from receiving any new values.

Quoted 6 lines

This commit changes user_requested_state to a counter tracking how many times hid_sensor_power_state(true) was called instead, fixing this.

Cc Bastien Nocera <[email protected]>
Cc Srinivas Pandruvada <[email protected]>

Looks sensible to me.

I'll give it a few days at least though for others to comment.

Thanks,

Jonathan

Quoted 27 lines
---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +4 -4
index cfb6588565ba..4905a997a7ec 100644
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 #ifdef CONFIG_PM
 	int ret;
 
-	atomic_set(&st->user_requested_state, state);
-
 	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
 		pm_runtime_enable(&st->pdev->dev);
 
-	if (state)
+	if (state) {
+		atomic_inc(&st->user_requested_state);
 		ret = pm_runtime_get_sync(&st->pdev->dev);
-	else {
+	} else {
+		atomic_dec(&st->user_requested_state);
 		pm_runtime_mark_last_busy(&st->pdev->dev);
 		pm_runtime_use_autosuspend(&st->pdev->dev);
 		ret = pm_runtime_put_autosuspend(&st->pdev->dev);
#2 lore raw
Quoted 26 lines · Jonathan Cameron
Quoted 23 lines · On Sat, 14 Apr 2018 17:09:09 +0200 Hans de Goede <[email protected]> wrote:

hid_sensor_set_power_work() powers the sensors back up after a resume
based on the user_requested_state atomic_t.

But hid_sensor_power_state() treats this as a boolean flag, leading to
the following problematic scenario:

1) Some app starts using the iio-sensor in buffered / triggered mode,
   hid_sensor_data_rdy_trigger_set_state(true) gets called, setting user_requested_state to 1.
2) Something directly accesses a _raw value through sysfs, leading to a call to hid_sensor_power_state(true) followed by hid_sensor_power_state(false) call, this sets user_requested_state
   to 1 followed by setting it to 0.
3) Suspend/resume the machine, hid_sensor_set_power_work() now does NOT power the sensor back up because user_requested_state (wrongly)
   is 0. Which stops the app using the sensor in buffered mode from receiving any new values.

Quoted 7 lines
Quoted 7 lines

This commit changes user_requested_state to a counter tracking how many
times hid_sensor_power_state(true) was called instead, fixing this.

Cc Bastien Nocera <[email protected]>
Cc Srinivas Pandruvada <[email protected]>

Which App is doing like this?

Thanks,
Srinivas

Quoted 40 lines

Looks sensible to me.

I'll give it a few days at least though for others to comment.

Thanks,

Jonathan

Quoted 29 lines

---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -0
index cfb6588565ba..4905a997a7ec 100644
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct
 hid_sensor_common *st, bool state)
  #ifdef CONFIG_PM
  	int ret;

drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -2
-	atomic_set(&st->user_requested_state, state);
-

  if (atomic_add_unless(&st->runtime_pm_enable, 1, 1)) pm_runtime_enable(&st->pdev->dev);

drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -1
-	if (state)
+	if (state) {
+		atomic_inc(&st->user_requested_state);

  ret = pm_runtime_get_sync(&st->pdev->dev);

drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -1
-	else {
+	} else {
+		atomic_dec(&st->user_requested_state);

  pm_runtime_mark_last_busy(&st->pdev->dev); pm_runtime_use_autosuspend(&st->pdev->dev); ret = pm_runtime_put_autosuspend(&st->pdev->dev);

#3 lore raw

Hi,

Quoted 31 lines · Srinivas Pandruvada
Quoted 30 lines · Jonathan Cameron
Quoted 27 lines · On Sat, 14 Apr 2018 17:09:09 +0200 Hans de Goede <[email protected]> wrote:

hid_sensor_set_power_work() powers the sensors back up after a resume
based on the user_requested_state atomic_t.

But hid_sensor_power_state() treats this as a boolean flag, leading to
the following problematic scenario:

1) Some app starts using the iio-sensor in buffered / triggered mode,
   hid_sensor_data_rdy_trigger_set_state(true) gets called, setting user_requested_state to 1.
2) Something directly accesses a _raw value through sysfs, leading to a call to hid_sensor_power_state(true) followed by hid_sensor_power_state(false) call, this sets
user_requested_state
   to 1 followed by setting it to 0.
3) Suspend/resume the machine, hid_sensor_set_power_work() now does NOT power the sensor back up because user_requested_state
(wrongly)
   is 0. Which stops the app using the sensor in buffered mode from receiving any new values.

This commit changes user_requested_state to a counter tracking how many
times hid_sensor_power_state(true) was called instead, fixing this.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Cc Bastien Nocera <[email protected]>
Cc Srinivas Pandruvada <[email protected]>

Which App is doing like this?

No app, just something I noticed while manually testing the accelerometer while iio-sensor-proxy was also active.

Regards,

Hans

Quoted 45 lines

Thanks,
Srinivas

Quoted 40 lines

Looks sensible to me.

I'll give it a few days at least though for others to comment.

Thanks,

Jonathan

Quoted 29 lines
---
  drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +4 -4
index cfb6588565ba..4905a997a7ec 100644
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct
 hid_sensor_common *st, bool state)
  #ifdef CONFIG_PM
  	int ret;
 
-	atomic_set(&st->user_requested_state, state);
-
  	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
  		pm_runtime_enable(&st->pdev->dev);
 
-	if (state)
+	if (state) {
+		atomic_inc(&st->user_requested_state);
  		ret = pm_runtime_get_sync(&st->pdev->dev);
-	else {
+	} else {
+		atomic_dec(&st->user_requested_state);
  		pm_runtime_mark_last_busy(&st->pdev->dev);
  		pm_runtime_use_autosuspend(&st->pdev->dev);

  ret = pm_runtime_put_autosuspend(&st->pdev->dev);

maintainer Applied
#4 lore raw
Quoted 38 lines · On Mon, 16 Apr 2018 07:22:50 +0200 Hans de Goede <[email protected]> wrote:

Hi,

Quoted 35 lines · Srinivas Pandruvada
Quoted 33 lines · Jonathan Cameron
Quoted 30 lines · On Sat, 14 Apr 2018 17:09:09 +0200 Hans de Goede <[email protected]> wrote:

hid_sensor_set_power_work() powers the sensors back up after a resume
based on the user_requested_state atomic_t.

But hid_sensor_power_state() treats this as a boolean flag, leading to
the following problematic scenario:

1) Some app starts using the iio-sensor in buffered / triggered mode,
   hid_sensor_data_rdy_trigger_set_state(true) gets called, setting user_requested_state to 1.
2) Something directly accesses a _raw value through sysfs, leading to a call to hid_sensor_power_state(true) followed by hid_sensor_power_state(false) call, this sets
user_requested_state
   to 1 followed by setting it to 0.
3) Suspend/resume the machine, hid_sensor_set_power_work() now does NOT power the sensor back up because user_requested_state
(wrongly)
   is 0. Which stops the app using the sensor in buffered mode from receiving any new values.

This commit changes user_requested_state to a counter tracking how many
times hid_sensor_power_state(true) was called instead, fixing this.

Cc Bastien Nocera <[email protected]>
Cc Srinivas Pandruvada <[email protected]>
Quoted 7 lines
Quoted 3 lines

Which App is doing like this?

No app, just something I noticed while manually testing the accelerometer while iio-sensor-proxy was also active.

Applied to the fixes-togreg branch of iio.git and marked for stable. Thanks,

Jonathan

Quoted 51 lines

Regards,

Hans

Quoted 45 lines

Thanks,
Srinivas

Quoted 40 lines

Looks sensible to me.

I'll give it a few days at least though for others to comment.

Thanks,

Jonathan

Quoted 29 lines
---
  drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +4 -4
index cfb6588565ba..4905a997a7ec 100644
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct
 hid_sensor_common *st, bool state)
  #ifdef CONFIG_PM
  	int ret;
 
-	atomic_set(&st->user_requested_state, state);
-
  	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
  		pm_runtime_enable(&st->pdev->dev);
 
-	if (state)
+	if (state) {
+		atomic_inc(&st->user_requested_state);
  		ret = pm_runtime_get_sync(&st->pdev->dev);
-	else {
+	} else {
+		atomic_dec(&st->user_requested_state);
  		pm_runtime_mark_last_busy(&st->pdev->dev);
  		pm_runtime_use_autosuspend(&st->pdev->dev);

  ret = pm_runtime_put_autosuspend(&st->pdev->dev);