Skip to contentSkip to conversation

IIO: Runtime PM related cleanups.

PATCH RESEND Superseded Jonathan Cameron 11 participants 99 messages May 9, 2021 – May 17, 2021

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

cover IIO: Runtime PM related cleanups.

From: Jonathan Cameron <[email protected]>

Resend because I managed to misstype kernel.org in the first set of email addresses. Sorry all who hence get this twice.

Two basic types of tidying up in here.
1) Cases where pm_runtime_put_noidle() is called in remove() path but not matched with any form of get
2) Using pm_runtime_resume_and_get() to either replace existing error handling or to introduce it where errors were previously ignored.

Note the unbalanced put is not a bug as such, because the runtime pm core protects against the reference count going negative. However, it is a bad pattern to have copied into new drivers (as it confuses me) so I'd like to clear it out.

Inspired by Mauro's work on similar issues in media and Julia's coccicheck script.

There will be at least one more set of these once this first set have flushed out any mistakes I may have made.

I checked my assumptions around the excess pm_runtime_put_noidle calls in QEMU but may well be missing something even so.

Cc Andreas Klinger <[email protected]>
Cc Brian Masney <[email protected]>
Cc Fabrice Gasnier <[email protected]>
Cc Hans de Goede <[email protected]>
Cc Jean-Baptiste Maneyrol <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Marek Vasut <[email protected]>
Cc Matt Ranostay <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Mike Looijmans <[email protected]>
Cc Mikko Koivunen <[email protected]>
Cc Parthiban Nallathambi <[email protected]>
Cc Rui Miguel Silva <[email protected]>
Cc Sean Nyekjaer <[email protected]>
Cc Sebastian Reichel <[email protected]>
Cc Srinivas Pandruvada <[email protected]>

Jonathan Cameron (28):
iio: gyro: fxa210002c: Balance runtime pm + use
pm_runtime_resume_and_get().
iio: light: isl29028: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: light: tsl2583: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: accel: bmc150-accel: Balanced runtime pm + use
pm_runtime_resume_and_get()
iio: accel: mma8452: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: accel: mma9551/3: Balance untime pm + use
pm_runtime_resume_and_get()
iio: accel: kxcjk-1013: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: hid: trigger: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: imu: kmx61: Balance runtime pm + use pm_runtime_resume_and_get()
iio: light: rpr0521: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: magn: bmc150: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: accel: bmi088: Balance runtime pm + use
pm_runtime_resume_and_get()
iio: imu: mpu6050: Use pm_runtime_resume_and_get() to replace open
coding.
iio: adc: stm32-adc: Use pm_runtime_resume_and_get() to replace open
coding.
iio: adc: stm32-dfsdm: Use pm_runtime_resume_and_get() to replace open
coding.
iio: dac: stm32-dac: Use pm_runtime_resume_and_get() to replace open
coding.
iio: adc: ads1015: Use pm_runtime_resume_and_get() to replace open
coding.
iio: chemical: atlas-sensor: Use pm_runtime_resume_and_get() to
replace open coding.
iio: light: pa12203001: Use pm_runtime_resume_and_get() to replace
open coding.
iio: light: us5182: Use pm_runtime_resume_and_get() to replace open
coding.
iio: light: vcnl4000: Use pm_runtime_resume_and_get() to replace open
coding.
iio: light: vcnl4035: Use pm_runtime_resume_and_get() to replace open
coding.
iio: pressure: zpa2326: fix potential extra call of runtime suspend.
iio: temp: mlx90614: Handle failure in pm_runtime_resume_and_get()
iio: proximity: srf04: Use pm_runtime_resume_and_get() and handle
error
iio: prox: pulsed-light-v2: Switch pm_runtime_resume_and_get()
iio: pressure: icp10100: Use pm_runtime_resume_and_get() + handle
errors
iio: adc: rcar-gyroadc: Use pm_runtime_resume_and_get() and check in

    probe()
 drivers/iio/accel/bmc150-accel-core.c         |  6 +----
 drivers/iio/accel/bmi088-accel-core.c         | 26 ++++++++++++++-----
 drivers/iio/accel/kxcjk-1013.c                |  5 +---
 drivers/iio/accel/mma8452.c                   |  5 +---
 drivers/iio/accel/mma9551.c                   |  1 -
 drivers/iio/accel/mma9551_core.c              |  4 +--
 drivers/iio/accel/mma9553.c                   |  1 -
 drivers/iio/adc/rcar-gyroadc.c                | 15 +++++------
 drivers/iio/adc/stm32-adc.c                   | 24 ++++++-----------
 drivers/iio/adc/stm32-dfsdm-core.c            |  6 ++---
 drivers/iio/adc/ti-ads1015.c                  |  4 +--
 drivers/iio/chemical/atlas-sensor.c           | 12 +++------
 .../common/hid-sensors/hid-sensor-trigger.c   |  8 ++----
 drivers/iio/dac/stm32-dac.c                   |  3 +--
 drivers/iio/gyro/fxas21002c_core.c            | 11 +-------
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    | 18 +++++--------
 drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c |  6 ++---
 drivers/iio/imu/kmx61.c                       |  5 +---
 drivers/iio/light/isl29028.c                  |  5 +---
 drivers/iio/light/pa12203001.c                |  4 +--
 drivers/iio/light/rpr0521.c                   |  7 +----
 drivers/iio/light/tsl2583.c                   | 13 +++++-----
 drivers/iio/light/us5182d.c                   |  4 +--
 drivers/iio/light/vcnl4000.c                  |  4 +--
 drivers/iio/light/vcnl4035.c                  |  4 +--
 drivers/iio/magnetometer/bmc150_magn.c        | 10 +++----
 drivers/iio/pressure/icp10100.c               |  4 ++-
 drivers/iio/pressure/zpa2326.c                |  6 ++---
 .../iio/proximity/pulsedlight-lidar-lite-v2.c |  4 ++-
 drivers/iio/proximity/srf04.c                 |  8 +++---
 drivers/iio/temperature/mlx90614.c            | 25 ++++++++++++++----
 31 files changed, 109 insertions(+), 149 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/28 iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

---
 drivers/iio/gyro/fxas21002c_core.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/iio/gyro/fxas21002c_core.c +1 -10
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }
 
 static int  fxas21002c_pm_put(struct fxas21002c_data *data)
@@ -1002,7 +995,6 @@ int fxas21002c_core_probe(struct device *dev, struct regmap *regmap, int irq,
 pm_disable:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	return ret;
 }
@@ -1016,7 +1008,6 @@ void fxas21002c_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 }
 EXPORT_SYMBOL_GPL(fxas21002c_core_remove);
 
Signature · 1 line
2.31.1
author maintainer
#8 lore raw

PATCH 2/28 iio: light: isl29028: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

In remove this driver called pm_runtime_put_noidle() but there is no matching get operation. This does not cause any problems because the reference counter will not change if already zero, but it does make the code harder to reason about so should be dropped.

Whilst we are here, use pm_runtime_resume_and_get() to replace open coded version.
Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Cc Sebastian Reichel <[email protected]>
---
 drivers/iio/light/isl29028.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/light/isl29028.c +1 -4
index 2f8b494f3e08..9de3262aa688 100644
@@ -339,9 +339,7 @@ static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
@@ -647,7 +645,6 @@ static int isl29028_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	return isl29028_clear_configure_reg(chip);
 }
Signature · 1 line
2.31.1
author maintainer
#12 lore raw

PATCH 3/28 iio: light: tsl2583: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Error paths in read_raw() and write_raw() callbacks failed to perform and type of runtime pm put(). Remove called pm_runtime_put_noidle() but there is no equivalent get (this is safe because the reference count is protected against going below zero, but it is misleading.

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

---
 drivers/iio/light/tsl2583.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/light/tsl2583.c +7 -6
index 0f787bfc88fc..1f4ad4047a86 100644
@@ -636,9 +636,7 @@ static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&chip->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&chip->client->dev);
+		ret = pm_runtime_resume_and_get(&chip->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&chip->client->dev);
 		ret = pm_runtime_put_autosuspend(&chip->client->dev);
@@ -721,8 +719,10 @@ static int tsl2583_read_raw(struct iio_dev *indio_dev,
 read_done:
 	mutex_unlock(&chip->als_mutex);
 
-	if (ret < 0)
+	if (ret < 0) {
+		tsl2583_set_pm_runtime_busy(chip, false);
 		return ret;
+	}
 
 	/*
 	 * Preserve the ret variable if the call to
@@ -783,8 +783,10 @@ static int tsl2583_write_raw(struct iio_dev *indio_dev,
 
 	mutex_unlock(&chip->als_mutex);
 
-	if (ret < 0)
+	if (ret < 0) {
+		tsl2583_set_pm_runtime_busy(chip, false);
 		return ret;
+	}
 
 	ret = tsl2583_set_pm_runtime_busy(chip, false);
 	if (ret < 0)
@@ -872,7 +874,6 @@ static int tsl2583_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
 }
Signature · 1 line
2.31.1
author maintainer
#16 lore raw

PATCH 4/28 iio: accel: bmc150-accel: Balanced runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

A call to pm_runtime_put_noidle() doesn't match any call that would result in a get(). It is safe because runtime pm core protects against the reference counter going 0, but it makes it harder to understand the code.

Whilst here use pm_runtime_resume_and_get() to tidy things up. The Coccinelle script didn't get this one due to more complex code structure.

---
 drivers/iio/accel/bmc150-accel-core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +1 -5
index 62a164a7b852..43cfadf8f6b7 100644
@@ -389,7 +389,7 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
@@ -398,9 +398,6 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	if (ret < 0) {
 		dev_err(dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(dev);
-
 		return ret;
 	}
 
@@ -1836,7 +1833,6 @@ int bmc150_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
 
Signature · 1 line
2.31.1
author maintainer
#20 lore raw

PATCH 5/28 iio: accel: mma8452: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Remove() callback calls pm_runtime_put_noidle() but there it is not balancing a get. No actual affect because the runtime pm core prevents the reference count going negative.

Whilst here use pm_runtime_resume_and_get() rather than open coded version. Again, coccinelle script missed this one due to more complex code structure.

---
 drivers/iio/accel/mma8452.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/mma8452.c +1 -4
index 464a6bfe6746..715b8138fb71 100644
@@ -221,7 +221,7 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -230,8 +230,6 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}
@@ -1711,7 +1709,6 @@ static int mma8452_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 	mma8452_trigger_cleanup(indio_dev);
Signature · 1 line
2.31.1
author maintainer
#23 lore raw

PATCH 6/28 iio: accel: mma9551/3: Balance untime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Both these drivers call pm_runtime_put_no_idle() when the reference count should already be zero as there is no matching get()

Whilst here use pm_runtime_resume_and_get() rather than open coding.

---
 drivers/iio/accel/mma9551.c      | 1 -
 drivers/iio/accel/mma9551_core.c | 4 +---
 drivers/iio/accel/mma9553.c      | 1 -
 3 files changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/mma9551.c +0 -1
index 08a2303cc9df..4c359fb05480 100644
@@ -515,7 +515,6 @@ static int mma9551_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);
drivers/iio/accel/mma9551_core.c +1 -3
index 666e7a04a7d7..fbf2e2c45678 100644
@@ -664,7 +664,7 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -673,8 +673,6 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}
drivers/iio/accel/mma9553.c +0 -1
index c15908faa381..ba3ecb3b57dc 100644
@@ -1154,7 +1154,6 @@ static int mma9553_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);
Signature · 1 line
2.31.1
author maintainer
#26 lore raw

PATCH 7/28 iio: accel: kxcjk-1013: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This driver alls pm_runtime_put_noidle() in it's remove function, but there is no matching get call. This isn't a bug as runtime pm will not allow the reference counter to go negative, but it is missleading so lets remove it.

Whilst here use pm_runtime_resume_and_get() to tidy up some boilerplate. The coccicheck script didn't get this one due to the less obvious structure. Found by inspection.

---
 drivers/iio/accel/kxcjk-1013.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/kxcjk-1013.c +1 -4
index 283e6a3feffc..a9e3dae43ac5 100644
@@ -478,7 +478,7 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -486,8 +486,6 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	if (ret < 0) {
 		dev_err(&data->client->dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 		return ret;
 	}
 #endif
@@ -1485,7 +1483,6 @@ static int kxcjk1013_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (data->dready_trig) {
 		iio_triggered_buffer_cleanup(indio_dev);
Signature · 1 line
2.31.1
author maintainer
#29 lore raw

PATCH 8/28 iio: hid: trigger: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() callback is not balanced by any gets

Note this doesn't cause any problems beyond reader confusion as the runtime pm core protects against the reference counter going negative.

Whilst here, use pm_runtiem_resume_and_get() to simplify code a little.

Cc Srinivas Pandruvada <[email protected]>
---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -6
index 5a7b3e253e58..c06537e106e9 100644
@@ -163,18 +163,15 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 
 	if (state) {
 		atomic_inc(&st->user_requested_state);
-		ret = pm_runtime_get_sync(&st->pdev->dev);
+		ret = pm_runtime_resume_and_get(&st->pdev->dev);
 	} 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);
 	}
-	if (ret < 0) {
-		if (state)
-			pm_runtime_put_noidle(&st->pdev->dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	return 0;
 #else
@@ -222,7 +219,6 @@ void hid_sensor_remove_trigger(struct iio_dev *indio_dev,
 		pm_runtime_disable(&attrb->pdev->dev);
 
 	pm_runtime_set_suspended(&attrb->pdev->dev);
-	pm_runtime_put_noidle(&attrb->pdev->dev);
 
 	cancel_work_sync(&attrb->work);
 	iio_trigger_unregister(attrb->trigger);
Signature · 1 line
2.31.1
author maintainer
#34 lore raw

PATCH 9/28 iio: imu: kmx61: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

No point in calling pm_runtime_put_noidle() that isn't balancing a get. Note no actual impact because the runtime pm core protects against a negative reference counter.

For the pm_runtime_resume_and_get() main interest is in clearing out this old pattern to avoid it getting coppied into new submissions.

---
 drivers/iio/imu/kmx61.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/imu/kmx61.c +1 -4
index d3e06ce99c1e..1dabfd615dab 100644
@@ -750,7 +750,7 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -759,8 +759,6 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 		dev_err(&data->client->dev,
 			"Failed: kmx61_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 
 		return ret;
 	}
@@ -1426,7 +1424,6 @@ static int kmx61_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (client->irq > 0) {
 		iio_triggered_buffer_cleanup(data->acc_indio_dev);
Signature · 1 line
2.31.1
author maintainer
#37 lore raw

PATCH 10/28 iio: light: rpr0521: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Calls to pm_runtime_put_noidle in probe() error path and remove() are not match to any get() calls.

The runtime pm core protects against negative reference counts, so this doesn't have any visible impact beyond confusing the reader.

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

---
 drivers/iio/light/rpr0521.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/iio/light/rpr0521.c +1 -6
index 7e332de0e6a5..c2dd8a3d4217 100644
@@ -360,7 +360,7 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 	 * both stay enabled until _suspend().
 	 */
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -369,9 +369,6 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 		dev_err(&data->client->dev,
 			"Failed: rpr0521_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
-
 		return ret;
 	}
 
@@ -1038,7 +1035,6 @@ static int rpr0521_probe(struct i2c_client *client,
 err_pm_disable:
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 err_poweroff:
 	rpr0521_poweroff(data);
 
@@ -1053,7 +1049,6 @@ static int rpr0521_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	rpr0521_poweroff(iio_priv(indio_dev));
 
Signature · 1 line
2.31.1
author maintainer
#40 lore raw

PATCH 11/28 iio: magn: bmc150: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

probe() error paths after runtime pm is enabled, should disable it. remove() should not call pm_runtime_put_noidle() as there is no matching get() to have raised the reference count. This case has no affect a the runtime pm core protects against going negative.

Whilst here use pm_runtime_resume_and_get() to tidy things up a little. coccicheck script didn't get this one due to complex code structure so found by inspection.

---
 drivers/iio/magnetometer/bmc150_magn.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +4 -6
index d75b437a43f2..39920cb764bf 100644
@@ -262,7 +262,7 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(data->dev);
+		ret = pm_runtime_resume_and_get(data->dev);
 	} else {
 		pm_runtime_mark_last_busy(data->dev);
 		ret = pm_runtime_put_autosuspend(data->dev);
@@ -271,9 +271,6 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	if (ret < 0) {
 		dev_err(data->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(data->dev);
-
 		return ret;
 	}
 #endif
@@ -963,12 +960,14 @@ int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
 	ret = iio_device_register(indio_dev);
 	if (ret < 0) {
 		dev_err(dev, "unable to register iio device\n");
-		goto err_buffer_cleanup;
+		goto err_disable_runtime_pm;
 	}
 
 	dev_dbg(dev, "Registered device %s\n", name);
 	return 0;
 
+err_disable_runtime_pm:
+	pm_runtime_disable(dev);
 err_buffer_cleanup:
 	iio_triggered_buffer_cleanup(indio_dev);
 err_free_irq:
@@ -992,7 +991,6 @@ int bmc150_magn_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 
Signature · 1 line
2.31.1
author maintainer
#44 lore raw

PATCH 12/28 iio: accel: bmi088: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() is not balanced by a get so drop it.

Using pm_runtime_resume_and_get() allows for simple introduction of error handling to allow for runtime resume failing.

---
 drivers/iio/accel/bmi088-accel-core.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/iio/accel/bmi088-accel-core.c +20 -6
index 61aaaf48c040..a06dae5c971d 100644
@@ -285,11 +285,17 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW:
 		switch (chan->type) {
 		case IIO_TEMP:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = bmi088_accel_get_temp(data, val);
 			goto out_read_raw_pm_put;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = iio_device_claim_direct_mode(indio_dev);
 			if (ret)
 				goto out_read_raw_pm_put;
@@ -319,7 +325,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			*val = BMI088_ACCEL_TEMP_UNIT;
 			return IIO_VAL_INT;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = regmap_read(data->regmap,
 					  BMI088_ACCEL_REG_ACC_RANGE, val);
 			if (ret)
@@ -334,7 +343,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_get_sample_freq(data, val, val2);
 		goto out_read_raw_pm_put;
 	default:
@@ -376,7 +388,10 @@ static int bmi088_accel_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_set_sample_freq(data, val);
 		pm_runtime_mark_last_busy(dev);
 		pm_runtime_put_autosuspend(dev);
@@ -530,7 +545,6 @@ int bmi088_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 	bmi088_accel_power_down(data);
 
 	return 0;
Signature · 1 line
2.31.1
author maintainer
#47 lore raw

PATCH 13/28 iio: imu: mpu6050: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

pm_runtime_resume_and_get() returns <= 0 only so simplify related checks to bring this more inline with nearby calls.

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    | 18 ++++++------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c |  6 ++----
 2 files changed, 8 insertions(+), 16 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +6 -12
index 6244a07048df..06b9c84f6bfb 100644
@@ -570,11 +570,9 @@ static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
 	freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
 	period_us = 1000000 / freq_hz;
 
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		return result;
-	}
 
 	switch (chan->type) {
 	case IIO_ANGL_VEL:
@@ -812,11 +810,9 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 		return result;
 
 	mutex_lock(&st->lock);
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto error_write_raw_unlock;
-	}
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -930,11 +926,9 @@ inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
 		result = 0;
 		goto fifo_rate_fail_unlock;
 	}
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto fifo_rate_fail_unlock;
-	}
 
 	result = regmap_write(st->map, st->reg->sample_rate_div, d);
 	if (result)
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c +2 -4
index e21ba778595a..2d0e8cdd4848 100644
@@ -173,11 +173,9 @@ static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
 
 	if (enable) {
 		scan = inv_scan_query(indio_dev);
-		result = pm_runtime_get_sync(pdev);
-		if (result < 0) {
-			pm_runtime_put_noidle(pdev);
+		result = pm_runtime_resume_and_get(pdev);
+		if (result)
 			return result;
-		}
 		/*
 		 * In case autosuspend didn't trigger, turn off first not
 		 * required sensors.
Signature · 1 line
2.31.1
author maintainer
#50 lore raw

PATCH 14/28 iio: adc: stm32-adc: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-adc.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
drivers/iio/adc/stm32-adc.c +8 -16
index c9d36c88ee6a..5088de835bb1 100644
@@ -1158,11 +1158,9 @@ static int stm32_adc_single_conv(struct iio_dev *indio_dev,
 
 	adc->bufi = 0;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	/* Apply sampling time settings */
 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
@@ -1364,11 +1362,9 @@ static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
 
@@ -1413,11 +1409,9 @@ static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	if (!readval)
 		stm32_adc_writel(adc, reg, writeval);
@@ -1537,11 +1531,9 @@ static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
 	if (ret) {
Signature · 1 line
2.31.1
author maintainer
#54 lore raw

PATCH 15/28 iio: adc: stm32-dfsdm: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-dfsdm-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/stm32-dfsdm-core.c +2 -4
index bb925a11c8ae..a627af9a825e 100644
@@ -135,11 +135,9 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
 	int ret;
 
 	if (atomic_inc_return(&priv->n_active_ch) == 1) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret < 0)
 			goto error_ret;
-		}
 
 		/* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
 		clk_src = priv->aclk ? 1 : 0;
Signature · 1 line
2.31.1
author maintainer
#58 lore raw

PATCH 16/28 iio: dac: stm32-dac: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general

---
 drivers/iio/dac/stm32-dac.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/dac/stm32-dac.c +1 -2
index a5b0a52bf86e..dd2e306824e7 100644
@@ -69,9 +69,8 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 	}
 
 	if (enable) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
 			mutex_unlock(&dac->lock);
 			return ret;
 		}
Signature · 1 line
2.31.1
author maintainer
#62 lore raw

PATCH 17/28 iio: adc: ads1015: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/ti-ads1015.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/ti-ads1015.c +1 -3
index 9fef39bcf997..4b61ee3d2501 100644
@@ -323,9 +323,7 @@ static int ads1015_set_power_state(struct ads1015_data *data, bool on)
 	struct device *dev = regmap_get_device(data->regmap);
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
Signature · 1 line
2.31.1
author maintainer
#65 lore raw

PATCH 18/28 iio: chemical: atlas-sensor: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

As pm_runtime_resume_and_get() returns <= 0 take advantage of that to change the error checking to if (ret) which is more in keeping with the rest of this driver.

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/chemical/atlas-sensor.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/iio/chemical/atlas-sensor.c +4 -8
index d10f921b233a..90bb3f5bff19 100644
@@ -410,11 +410,9 @@ static int atlas_buffer_postenable(struct iio_dev *indio_dev)
 	struct atlas_data *data = iio_priv(indio_dev);
 	int ret;
 
-	ret = pm_runtime_get_sync(&data->client->dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret)
 		return ret;
-	}
 
 	return atlas_set_interrupt(data, true);
 }
@@ -487,11 +485,9 @@ static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
 	int suspended = pm_runtime_suspended(dev);
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
 		return ret;
-	}
 
 	if (suspended)
 		msleep(data->chip->delay);
Signature · 1 line
2.31.1
author maintainer
#69 lore raw

PATCH 19/28 iio: light: pa12203001: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/light/pa12203001.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/pa12203001.c +1 -3
index bfade6577a38..a52b2c788540 100644
@@ -186,9 +186,7 @@ static int pa12203001_set_power_state(struct pa12203001_data *data, bool on,
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
Signature · 1 line
2.31.1
author maintainer
#72 lore raw

PATCH 20/28 iio: light: us5182: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/light/us5182d.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/us5182d.c +1 -3
index 393f27b75c75..96e4a66ddf28 100644
@@ -367,9 +367,7 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on)
 		return 0;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
Signature · 1 line
2.31.1
author maintainer
#75 lore raw

PATCH 21/28 iio: light: vcnl4000: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/light/vcnl4000.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4000.c +1 -3
index 4a61865d01cd..01772327a947 100644
@@ -413,9 +413,7 @@ static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
Signature · 1 line
2.31.1
author maintainer
#78 lore raw

PATCH 22/28 iio: light: vcnl4035: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Cc Parthiban Nallathambi <[email protected]>
---
 drivers/iio/light/vcnl4035.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4035.c +1 -3
index 691a54b763e1..fd2f181b16db 100644
@@ -144,9 +144,7 @@ static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on)
 	struct device *dev = &data->client->dev;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
Signature · 1 line
2.31.1
author maintainer
#81 lore raw

PATCH 23/28 iio: pressure: zpa2326: fix potential extra call of runtime suspend.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This case illustrates why the new pm_runtime_sync_and_get() is good in that it makes clear the correct way to handle errors.

Calling pm_runtime_put() on failure of the _get() in pm_runtime_get_sync() will potentially result in powering down an already powered down device (as we never successfully powered it up. Unlikely to cause any problems in reality.

---
 drivers/iio/pressure/zpa2326.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/pressure/zpa2326.c +2 -4
index 89295c90f801..97ac3ba399f7 100644
@@ -664,11 +664,9 @@ static int zpa2326_resume(const struct iio_dev *indio_dev)
 {
 	int err;
 
-	err = pm_runtime_get_sync(indio_dev->dev.parent);
-	if (err < 0) {
-		pm_runtime_put(indio_dev->dev.parent);
+	err = pm_runtime_resume_and_get(indio_dev->dev.parent);
+	if (err < 0)
 		return err;
-	}
 
 	if (err > 0) {
 		/*
Signature · 1 line
2.31.1
author maintainer
#84 lore raw

PATCH 24/28 iio: temp: mlx90614: Handle failure in pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Converts from using pm_runtime_get_sync() with no error handling over to pm_runtime_resume_and_get() which will ensure we don't end up holding a reference. Ensure this error return is then handled at calls to mlx90614_power_get(). These are all direct returns.

---
 drivers/iio/temperature/mlx90614.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/iio/temperature/mlx90614.c +20 -5
index ef0fec94d269..afcb10ea7c44 100644
@@ -176,11 +176,14 @@ static inline s32 mlx90614_iir_search(const struct i2c_client *client,
 static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
 {
 	unsigned long now;
+	int ret;
 
 	if (!data->wakeup_gpio)
 		return 0;
 
-	pm_runtime_get_sync(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret < 0)
+		return ret;
 
 	if (startup) {
 		now = jiffies;
@@ -267,7 +270,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		*val = MLX90614_CONST_SCALE;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client,
 					       MLX90614_EMISSIVITY);
@@ -287,7 +293,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT_PLUS_NANO;
 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR setting with
 							     FIR = 1024 */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
 		mutex_unlock(&data->lock);
@@ -319,7 +328,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX +
 			val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
 					  val);
@@ -331,7 +343,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		if (val < 0 || val2 < 0)
 			return -EINVAL;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_iir_search(data->client,
 					  val * 100 + val2 / 10000);
Signature · 1 line
2.31.1
author maintainer
#87 lore raw

PATCH 25/28 iio: proximity: srf04: Use pm_runtime_resume_and_get() and handle error

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Previously this driver used pm_runtime_sync_get() and did not handle any errors that occurred. Moving to the new pm_runtime_resume_and_get() + handle errors.

---
 drivers/iio/proximity/srf04.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/proximity/srf04.c +5 -3
index 420c37c72de4..fe88b2bb60bc 100644
@@ -100,9 +100,11 @@ static int srf04_read(struct srf04_data *data)
 	u64 dt_ns;
 	u32 time_ns, distance_mm;
 
-	if (data->gpiod_power)
-		pm_runtime_get_sync(data->dev);
-
+	if (data->gpiod_power) {
+		ret = pm_runtime_resume_and_get(data->dev);
+		if (ret < 0)
+			return ret;
+	}
 	/*
 	 * just one read-echo-cycle can take place at a time
 	 * ==> lock against concurrent reading calls
Signature · 1 line
2.31.1
author maintainer
#90 lore raw

PATCH 26/28 iio: prox: pulsed-light-v2: Switch pm_runtime_resume_and_get()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle any errors as a result of runtime resume as it exits without leaving the reference count elevated.

---
 drivers/iio/proximity/pulsedlight-lidar-lite-v2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +3 -1
index c685f10b5ae4..ca8dc5cc209a 100644
@@ -154,7 +154,9 @@ static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
 	int tries = 10;
 	int ret;
 
-	pm_runtime_get_sync(&client->dev);
+	ret = pm_runtime_resume_and_get(&client->dev);
+	if (ret < 0)
+		return ret;
 
 	/* start sample */
 	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
Signature · 1 line
2.31.1
author maintainer
#93 lore raw

PATCH 27/28 iio: pressure: icp10100: Use pm_runtime_resume_and_get() + handle errors

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle failures in resume as it doesn't hold a reference count if it exits with an error.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 drivers/iio/pressure/icp10100.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/pressure/icp10100.c +3 -1
index 48759fc4bf18..80cd66688731 100644
@@ -250,7 +250,9 @@ static int icp10100_get_measures(struct icp10100_state *st,
 	__be16 measures[3];
 	int ret;
 
-	pm_runtime_get_sync(&st->client->dev);
+	ret = pm_runtime_resume_and_get(&st->client->dev);
+	if (ret < 0)
+		return ret;
 
 	mutex_lock(&st->lock);
 	cmd = &icp10100_cmd_measure[st->mode];
Signature · 1 line
2.31.1
author maintainer
#96 lore raw

PATCH 28/28 iio: adc: rcar-gyroadc: Use pm_runtime_resume_and_get() and check in probe()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

1 instance found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/
The other instance changed did not check for failure of the pm_runtime_get_sync() so that is added.

Note the remaining pm_runtime_get_sync() call is left alone because it is not obvious what to do on failure to power up in remove()

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/rcar-gyroadc.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iio/adc/rcar-gyroadc.c +7 -8
index 9f38cf3c7dc2..a48895046408 100644
@@ -162,18 +162,13 @@ static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
 static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
 {
 	struct device *dev = priv->dev;
-	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		return pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
-		ret = pm_runtime_put_autosuspend(dev);
+		return pm_runtime_put_autosuspend(dev);
 	}
-
-	return ret;
 }
 
 static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
@@ -535,7 +530,10 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_enable(dev);
 
-	pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
+		goto err_power_up;
+
 	rcar_gyroadc_hw_init(priv);
 	rcar_gyroadc_hw_start(priv);
 
@@ -552,6 +550,7 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 err_iio_device_register:
 	rcar_gyroadc_hw_stop(priv);
 	pm_runtime_put_sync(dev);
+err_power_up:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
 	clk_disable_unprepare(priv->clk);
Signature · 1 line
2.31.1
Reviewed-by on v1 4/28
#17 lore raw

Re: [PATCH 04/28] iio: accel: bmc150-accel: Balanced runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Hi,

Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

A call to pm_runtime_put_noidle() doesn't match any call that would result in a get(). It is safe because runtime pm core protects against the reference counter going 0, but it makes it harder to understand the code.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy things up. The Coccinelle script didn't get this one due to more complex code structure.

Thanks, patch looks good to me:

Regards,

Hans

Quoted 36 lines
---
 drivers/iio/accel/bmc150-accel-core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +1 -5
index 62a164a7b852..43cfadf8f6b7 100644
@@ -389,7 +389,7 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
@@ -398,9 +398,6 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	if (ret < 0) {
 		dev_err(dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(dev);
-
 		return ret;
 	}
 
@@ -1836,7 +1833,6 @@ int bmc150_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
 
Reviewed-by on v1 7/28
#27 lore raw

Re: [PATCH 07/28] iio: accel: kxcjk-1013: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Hi,

Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This driver alls pm_runtime_put_noidle() in it's remove function, but there is no matching get call. This isn't a bug as runtime pm will not allow the reference counter to go negative, but it is missleading so lets remove it.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy up some boilerplate. The coccicheck script didn't get this one due to the less obvious structure. Found by inspection.

Thanks, patch looks good to me:

Regards,

Hans

Quoted 35 lines
---
 drivers/iio/accel/kxcjk-1013.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/kxcjk-1013.c +1 -4
index 283e6a3feffc..a9e3dae43ac5 100644
@@ -478,7 +478,7 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -486,8 +486,6 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	if (ret < 0) {
 		dev_err(&data->client->dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 		return ret;
 	}
 #endif
@@ -1485,7 +1483,6 @@ static int kxcjk1013_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (data->dready_trig) {
 		iio_triggered_buffer_cleanup(indio_dev);
Reviewed-by on v1 1/28
#2 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

In reply to Jonathan Cameron

Hi Jonathan,
thanks for the patch.

Quoted 11 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Quoted 6 lines

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

With s/fxa210002c:/fxa21002c:/ in the patch subject:

Cheers,
Rui

Quoted 42 lines
---
 drivers/iio/gyro/fxas21002c_core.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/iio/gyro/fxas21002c_core.c +1 -10
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }
 
 static int  fxas21002c_pm_put(struct fxas21002c_data *data)
@@ -1002,7 +995,6 @@ int fxas21002c_core_probe(struct device *dev, struct regmap *regmap, int irq,
 pm_disable:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	return ret;
 }
@@ -1016,7 +1008,6 @@ void fxas21002c_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 }
 EXPORT_SYMBOL_GPL(fxas21002c_core_remove);
 
Signature · 1 line
2.31.1
Acked-by on v1 18/28
#66 lore raw

Re: [PATCH 18/28] iio: chemical: atlas-sensor: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron
Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

As pm_runtime_resume_and_get() returns <= 0 take advantage of that to change the error checking to if (ret) which is more in keeping with the rest of this driver.

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Quoted 41 lines
---
 drivers/iio/chemical/atlas-sensor.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/iio/chemical/atlas-sensor.c +4 -9
index d10f921b233a..90bb3f5bff19 100644
@@ -410,11 +410,9 @@ static int atlas_buffer_postenable(struct iio_dev *indio_dev)
        struct atlas_data *data = iio_priv(indio_dev);
        int ret;
 
-       ret = pm_runtime_get_sync(&data->client->dev);
-       if (ret < 0) {
-               pm_runtime_put_noidle(&data->client->dev);
+       ret = pm_runtime_resume_and_get(&data->client->dev);
+       if (ret)
                return ret;
-       }
 
        return atlas_set_interrupt(data, true);
 }
@@ -487,11 +485,9 @@ static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
        int suspended = pm_runtime_suspended(dev);
        int ret;
 
-       ret = pm_runtime_get_sync(dev);
-       if (ret < 0) {
-               pm_runtime_put_noidle(dev);
+       ret = pm_runtime_resume_and_get(dev);
+       if (ret)
                return ret;
-       }
 
        if (suspended)
                msleep(data->chip->delay);
--

2.31.1

Reviewed-by on v1 3/28
#13 lore raw

Re: [PATCH 03/28] iio: light: tsl2583: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron
Quoted 11 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Error paths in read_raw() and write_raw() callbacks failed to perform and type of runtime pm put(). Remove called pm_runtime_put_noidle() but there is no equivalent get (this is safe because the reference count is protected against going below zero, but it is misleading.

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

Reviewed-by on v1 1/28
#3 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:27 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 11 lines

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Quoted 6 lines

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

LGTM.

Quoted 33 lines
---
 drivers/iio/gyro/fxas21002c_core.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/iio/gyro/fxas21002c_core.c +1 -9
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }
 
 static int  fxas21002c_pm_put(struct fxas21002c_data *data)
@@ -1002,7 +995,6 @@ int fxas21002c_core_probe(struct device *dev, struct regmap *regmap, int irq,
 pm_disable:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	return ret;
 }

drivers/iio/gyro/fxas21002c_core.c void fxas21002c_core_remove(struct device *dev) · line 1013

Quoted 7 lines
drivers/iio/gyro/fxas21002c_core.c +0 -1
@@ -1016,7 +1008,6 @@ void fxas21002c_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 }
 EXPORT_SYMBOL_GPL(fxas21002c_core_remove);
 

Thanks,
Mauro

#4 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

Em Wed, 12 May 2021 15:22:43 +0200
Mauro Carvalho Chehab <[email protected]> escreveu:

Quoted 18 lines

Em Sun, 9 May 2021 12:33:27 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 15 lines

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines

LGTM.

Hmm...not quite...

Quoted 6 lines
Quoted 6 lines
drivers/iio/gyro/fxas21002c_core.c +0 -0
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
Quoted 12 lines

drivers/iio/gyro/fxas21002c_core.c static int fxas21002c_write(struct fxas21002c_data *data, · line 370

Quoted 12 lines
drivers/iio/gyro/fxas21002c_core.c +1 -8
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }

fxas21002c_temp_get() and fxas21002c_axis_get() seem to be missing a pm_runtime_put*() if something gets wrong at regmap_field_read(), e. g.:

static int fxas21002c_axis_get(struct fxas21002c_data *data,
int index, int *val)

{
        struct device *dev = regmap_get_device(data->regmap);
        __be16 axis_be;
        int ret;

        mutex_lock(&data->lock);
        ret = fxas21002c_pm_get(data);
        if (ret < 0)
                goto data_unlock;

        ret = regmap_bulk_read(data->regmap, FXAS21002C_AXIS_TO_REG(index),
                               &axis_be, sizeof(axis_be));
        if (ret < 0) {
                dev_err(dev, "failed to read axis: %d: %d\n", index, ret);
+		fxas21002c_pm_put(data);
                goto data_unlock;
        }

	*val = sign_extend32(be16_to_cpu(axis_be), 15);

        ret = fxas21002c_pm_put(data);
        if (ret < 0)
                goto data_unlock;

        ret = IIO_VAL_INT;

data_unlock:

	mutex_unlock(&data->lock);

        return ret;
}

Thanks,
Mauro

Reviewed-by on v1 14/28
#51 lore raw

Re: [PATCH 14/28] iio: adc: stm32-adc: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron
Quoted 5 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-adc.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

Hi Jonathan,

Thanks for the patch,
Regards,
Fabrice

Quoted 62 lines
drivers/iio/adc/stm32-adc.c +8 -16
index c9d36c88ee6a..5088de835bb1 100644
@@ -1158,11 +1158,9 @@ static int stm32_adc_single_conv(struct iio_dev *indio_dev,
 
 	adc->bufi = 0;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	/* Apply sampling time settings */
 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
@@ -1364,11 +1362,9 @@ static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
 
@@ -1413,11 +1409,9 @@ static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	if (!readval)
 		stm32_adc_writel(adc, reg, writeval);
@@ -1537,11 +1531,9 @@ static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
 	if (ret) {
Reviewed-by on v1 15/28
#55 lore raw

Re: [PATCH 15/28] iio: adc: stm32-dfsdm: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron
Quoted 5 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-dfsdm-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Hi Jonathan,

Thanks for the patch,
Regards,
Fabrice

Quoted 20 lines
drivers/iio/adc/stm32-dfsdm-core.c +2 -4
index bb925a11c8ae..a627af9a825e 100644
@@ -135,11 +135,9 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
 	int ret;
 
 	if (atomic_inc_return(&priv->n_active_ch) == 1) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret < 0)
 			goto error_ret;
-		}
 
 		/* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
 		clk_src = priv->aclk ? 1 : 0;
Reviewed-by on v1 16/28
#59 lore raw

Re: [PATCH 16/28] iio: dac: stm32-dac: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron
Quoted 5 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general

---
 drivers/iio/dac/stm32-dac.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Hi Jonathan,

Thanks for the patch,
Regards,
Fabrice

Quoted 17 lines
drivers/iio/dac/stm32-dac.c +1 -2
index a5b0a52bf86e..dd2e306824e7 100644
@@ -69,9 +69,8 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 	}
 
 	if (enable) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
 			mutex_unlock(&dac->lock);
 			return ret;
 		}
Reviewed-by on v1 2/28
#9 lore raw

Re: [PATCH 02/28] iio: light: isl29028: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:28 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

In remove this driver called pm_runtime_put_noidle() but there is no matching get operation. This does not cause any problems because the reference counter will not change if already zero, but it does make the code harder to reason about so should be dropped.

Quoted 7 lines

Whilst we are here, use pm_runtime_resume_and_get() to replace open coded version.
Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Cc Sebastian Reichel <[email protected]>

LGTM.

Quoted 20 lines
---
 drivers/iio/light/isl29028.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/light/isl29028.c +1 -3
index 2f8b494f3e08..9de3262aa688 100644
@@ -339,9 +339,7 @@ static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);

drivers/iio/light/isl29028.c static int isl29028_remove(struct i2c_client *client) · line 650

Quoted 7 lines
drivers/iio/light/isl29028.c +0 -1
@@ -647,7 +645,6 @@ static int isl29028_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	return isl29028_clear_configure_reg(chip);
 }

Thanks,
Mauro

Reviewed-by on v1 3/28
#15 lore raw

Re: [PATCH 03/28] iio: light: tsl2583: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:29 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 11 lines

From: Jonathan Cameron <[email protected]>

Error paths in read_raw() and write_raw() callbacks failed to perform and type of runtime pm put(). Remove called pm_runtime_put_noidle() but there is no equivalent get (this is safe because the reference count is protected against going below zero, but it is misleading.

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

LGTM.

Quoted 44 lines
---
 drivers/iio/light/tsl2583.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/light/tsl2583.c +7 -5
index 0f787bfc88fc..1f4ad4047a86 100644
@@ -636,9 +636,7 @@ static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&chip->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&chip->client->dev);
+		ret = pm_runtime_resume_and_get(&chip->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&chip->client->dev);
 		ret = pm_runtime_put_autosuspend(&chip->client->dev);
@@ -721,8 +719,10 @@ static int tsl2583_read_raw(struct iio_dev *indio_dev,
 read_done:
 	mutex_unlock(&chip->als_mutex);
 
-	if (ret < 0)
+	if (ret < 0) {
+		tsl2583_set_pm_runtime_busy(chip, false);
 		return ret;
+	}
 
 	/*
 	 * Preserve the ret variable if the call to
@@ -783,8 +783,10 @@ static int tsl2583_write_raw(struct iio_dev *indio_dev,
 
 	mutex_unlock(&chip->als_mutex);
 
-	if (ret < 0)
+	if (ret < 0) {
+		tsl2583_set_pm_runtime_busy(chip, false);
 		return ret;
+	}
 
 	ret = tsl2583_set_pm_runtime_busy(chip, false);
 	if (ret < 0)

drivers/iio/light/tsl2583.c static int tsl2583_remove(struct i2c_client *client) · line 879

Quoted 7 lines
drivers/iio/light/tsl2583.c +0 -1
@@ -872,7 +874,6 @@ static int tsl2583_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
 }

Thanks,
Mauro

Reviewed-by on v1 4/28
#19 lore raw

Re: [PATCH 04/28] iio: accel: bmc150-accel: Balanced runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:30 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

A call to pm_runtime_put_noidle() doesn't match any call that would result in a get(). It is safe because runtime pm core protects against the reference counter going 0, but it makes it harder to understand the code.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy things up. The Coccinelle script didn't get this one due to more complex code structure.

LGTM.

Quoted 28 lines
---
 drivers/iio/accel/bmc150-accel-core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +1 -4
index 62a164a7b852..43cfadf8f6b7 100644
@@ -389,7 +389,7 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
@@ -398,9 +398,6 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	if (ret < 0) {
 		dev_err(dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(dev);
-
 		return ret;
 	}
 

drivers/iio/accel/bmc150-accel-core.c int bmc150_accel_core_remove(struct device *dev) · line 1838

Quoted 7 lines
drivers/iio/accel/bmc150-accel-core.c +0 -1
@@ -1836,7 +1833,6 @@ int bmc150_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
 

Thanks,
Mauro

Reviewed-by on v1 5/28
#21 lore raw

Re: [PATCH 05/28] iio: accel: mma8452: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:31 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 6 lines

From: Jonathan Cameron <[email protected]>

Remove() callback calls pm_runtime_put_noidle() but there it is not balancing a get. No actual affect because the runtime pm core prevents the reference count going negative.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() rather than open coded version. Again, coccinelle script missed this one due to more complex code structure.

LGTM.

Quoted 27 lines
---
 drivers/iio/accel/mma8452.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/mma8452.c +1 -3
index 464a6bfe6746..715b8138fb71 100644
@@ -221,7 +221,7 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -230,8 +230,6 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}

drivers/iio/accel/mma8452.c static int mma8452_remove(struct i2c_client *client) · line 1714

Quoted 7 lines
drivers/iio/accel/mma8452.c +0 -1
@@ -1711,7 +1709,6 @@ static int mma8452_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 	mma8452_trigger_cleanup(indio_dev);

Thanks,
Mauro

Reviewed-by on v1 6/28
#24 lore raw

Re: [PATCH 06/28] iio: accel: mma9551/3: Balance untime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:32 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 6 lines

From: Jonathan Cameron <[email protected]>

Both these drivers call pm_runtime_put_no_idle() when the reference count should already be zero as there is no matching get()

Whilst here use pm_runtime_resume_and_get() rather than open coding.

LGTM.

Quoted 46 lines
---
 drivers/iio/accel/mma9551.c      | 1 -
 drivers/iio/accel/mma9551_core.c | 4 +---
 drivers/iio/accel/mma9553.c      | 1 -
 3 files changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/mma9551.c +0 -1
index 08a2303cc9df..4c359fb05480 100644
@@ -515,7 +515,6 @@ static int mma9551_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);
drivers/iio/accel/mma9551_core.c +1 -3
index 666e7a04a7d7..fbf2e2c45678 100644
@@ -664,7 +664,7 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -673,8 +673,6 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}
drivers/iio/accel/mma9553.c +0 -0
index c15908faa381..ba3ecb3b57dc 100644

drivers/iio/accel/mma9553.c static int mma9553_remove(struct i2c_client *client) · line 1159

Quoted 7 lines
drivers/iio/accel/mma9553.c +0 -1
@@ -1154,7 +1154,6 @@ static int mma9553_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);

Thanks,
Mauro

Reviewed-by on v1 7/28
#28 lore raw

Re: [PATCH 07/28] iio: accel: kxcjk-1013: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:33 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

This driver alls pm_runtime_put_noidle() in it's remove function, but there is no matching get call. This isn't a bug as runtime pm will not allow the reference counter to go negative, but it is missleading so lets remove it.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy up some boilerplate. The coccicheck script didn't get this one due to the less obvious structure. Found by inspection.

LGTM.

Quoted 27 lines
---
 drivers/iio/accel/kxcjk-1013.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/kxcjk-1013.c +1 -3
index 283e6a3feffc..a9e3dae43ac5 100644
@@ -478,7 +478,7 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -486,8 +486,6 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
 	if (ret < 0) {
 		dev_err(&data->client->dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 		return ret;
 	}
 #endif

drivers/iio/accel/kxcjk-1013.c static int kxcjk1013_remove(struct i2c_client *client) · line 1488

Quoted 7 lines
drivers/iio/accel/kxcjk-1013.c +0 -1
@@ -1485,7 +1483,6 @@ static int kxcjk1013_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (data->dready_trig) {
 		iio_triggered_buffer_cleanup(indio_dev);

Thanks,
Mauro

Reviewed-by on v1 8/28
#30 lore raw

Re: [PATCH 08/28] iio: hid: trigger: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:34 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 5 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() callback is not balanced by any gets

Quoted 7 lines

Note this doesn't cause any problems beyond reader confusion as the runtime pm core protects against the reference counter going negative.

Whilst here, use pm_runtiem_resume_and_get() to simplify code a little.

Cc Srinivas Pandruvada <[email protected]>

RPM get/put logic LGTM.

Quoted 30 lines
---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -5
index 5a7b3e253e58..c06537e106e9 100644
@@ -163,18 +163,15 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 
 	if (state) {
 		atomic_inc(&st->user_requested_state);
-		ret = pm_runtime_get_sync(&st->pdev->dev);
+		ret = pm_runtime_resume_and_get(&st->pdev->dev);
 	} 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);
 	}
-	if (ret < 0) {
-		if (state)
-			pm_runtime_put_noidle(&st->pdev->dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	return 0;
 #else

drivers/iio/common/hid-sensors/hid-sensor-trigger.c void hid_sensor_remove_trigger(struct iio_dev *indio_dev, · line 224

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -1
@@ -222,7 +219,6 @@ void hid_sensor_remove_trigger(struct iio_dev *indio_dev,
 		pm_runtime_disable(&attrb->pdev->dev);
 
 	pm_runtime_set_suspended(&attrb->pdev->dev);
-	pm_runtime_put_noidle(&attrb->pdev->dev);
 
 	cancel_work_sync(&attrb->work);
 	iio_trigger_unregister(attrb->trigger);

Thanks,
Mauro

Reviewed-by on v1 9/28
#35 lore raw

Re: [PATCH 09/28] iio: imu: kmx61: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:35 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

No point in calling pm_runtime_put_noidle() that isn't balancing a get. Note no actual impact because the runtime pm core protects against a negative reference counter.

For the pm_runtime_resume_and_get() main interest is in clearing out this old pattern to avoid it getting coppied into new submissions.

LGTM.

Quoted 27 lines
---
 drivers/iio/imu/kmx61.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/imu/kmx61.c +1 -3
index d3e06ce99c1e..1dabfd615dab 100644
@@ -750,7 +750,7 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -759,8 +759,6 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 		dev_err(&data->client->dev,
 			"Failed: kmx61_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 
 		return ret;
 	}

drivers/iio/imu/kmx61.c static int kmx61_remove(struct i2c_client *client) · line 1429

Quoted 7 lines
drivers/iio/imu/kmx61.c +0 -1
@@ -1426,7 +1424,6 @@ static int kmx61_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (client->irq > 0) {
 		iio_triggered_buffer_cleanup(data->acc_indio_dev);

Thanks,
Mauro

Reviewed-by on v1 10/28
#38 lore raw

Re: [PATCH 10/28] iio: light: rpr0521: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:36 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 5 lines

From: Jonathan Cameron <[email protected]>

Calls to pm_runtime_put_noidle in probe() error path and remove() are not match to any get() calls.

Quoted 7 lines

The runtime pm core protects against negative reference counts, so this doesn't have any visible impact beyond confusing the reader.

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

LGTM.

Quoted 36 lines
---
 drivers/iio/light/rpr0521.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/iio/light/rpr0521.c +1 -5
index 7e332de0e6a5..c2dd8a3d4217 100644
@@ -360,7 +360,7 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 	 * both stay enabled until _suspend().
 	 */
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -369,9 +369,6 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 		dev_err(&data->client->dev,
 			"Failed: rpr0521_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
-
 		return ret;
 	}
 
@@ -1038,7 +1035,6 @@ static int rpr0521_probe(struct i2c_client *client,
 err_pm_disable:
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 err_poweroff:
 	rpr0521_poweroff(data);
 

drivers/iio/light/rpr0521.c static int rpr0521_remove(struct i2c_client *client) · line 1054

Quoted 7 lines
drivers/iio/light/rpr0521.c +0 -1
@@ -1053,7 +1049,6 @@ static int rpr0521_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	rpr0521_poweroff(iio_priv(indio_dev));
 

Thanks,
Mauro

Reviewed-by on v1 11/28
#41 lore raw

Re: [PATCH 11/28] iio: magn: bmc150: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:37 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

probe() error paths after runtime pm is enabled, should disable it. remove() should not call pm_runtime_put_noidle() as there is no matching get() to have raised the reference count. This case has no affect a the runtime pm core protects against going negative.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy things up a little. coccicheck script didn't get this one due to complex code structure so found by inspection.

LGTM.

Quoted 44 lines
---
 drivers/iio/magnetometer/bmc150_magn.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +4 -5
index d75b437a43f2..39920cb764bf 100644
@@ -262,7 +262,7 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(data->dev);
+		ret = pm_runtime_resume_and_get(data->dev);
 	} else {
 		pm_runtime_mark_last_busy(data->dev);
 		ret = pm_runtime_put_autosuspend(data->dev);
@@ -271,9 +271,6 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	if (ret < 0) {
 		dev_err(data->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(data->dev);
-
 		return ret;
 	}
 #endif
@@ -963,12 +960,14 @@ int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
 	ret = iio_device_register(indio_dev);
 	if (ret < 0) {
 		dev_err(dev, "unable to register iio device\n");
-		goto err_buffer_cleanup;
+		goto err_disable_runtime_pm;
 	}
 
 	dev_dbg(dev, "Registered device %s\n", name);
 	return 0;
 
+err_disable_runtime_pm:
+	pm_runtime_disable(dev);
 err_buffer_cleanup:
 	iio_triggered_buffer_cleanup(indio_dev);
 err_free_irq:

drivers/iio/magnetometer/bmc150_magn.c int bmc150_magn_remove(struct device *dev) · line 996

Quoted 7 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -1
@@ -992,7 +991,6 @@ int bmc150_magn_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 

Thanks,
Mauro

Reviewed-by on v1 12/28
#45 lore raw

Re: [PATCH 12/28] iio: accel: bmi088: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:38 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() is not balanced by a get so drop it.

Using pm_runtime_resume_and_get() allows for simple introduction of error handling to allow for runtime resume failing.

LGTM.

Quoted 65 lines
---
 drivers/iio/accel/bmi088-accel-core.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/iio/accel/bmi088-accel-core.c +20 -5
index 61aaaf48c040..a06dae5c971d 100644
@@ -285,11 +285,17 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW:
 		switch (chan->type) {
 		case IIO_TEMP:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = bmi088_accel_get_temp(data, val);
 			goto out_read_raw_pm_put;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = iio_device_claim_direct_mode(indio_dev);
 			if (ret)
 				goto out_read_raw_pm_put;
@@ -319,7 +325,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			*val = BMI088_ACCEL_TEMP_UNIT;
 			return IIO_VAL_INT;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = regmap_read(data->regmap,
 					  BMI088_ACCEL_REG_ACC_RANGE, val);
 			if (ret)
@@ -334,7 +343,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_get_sample_freq(data, val, val2);
 		goto out_read_raw_pm_put;
 	default:
@@ -376,7 +388,10 @@ static int bmi088_accel_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_set_sample_freq(data, val);
 		pm_runtime_mark_last_busy(dev);
 		pm_runtime_put_autosuspend(dev);

drivers/iio/accel/bmi088-accel-core.c int bmi088_accel_core_remove(struct device *dev) · line 550

Quoted 7 lines
drivers/iio/accel/bmi088-accel-core.c +0 -1
@@ -530,7 +545,6 @@ int bmi088_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 	bmi088_accel_power_down(data);
 
 	return 0;

Thanks,
Mauro

#48 lore raw

Re: [PATCH 13/28] iio: imu: mpu6050: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:39 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

pm_runtime_resume_and_get() returns <= 0 only so simplify related checks to bring this more inline with nearby calls.

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Hmm... I guess there are some weird things happening there at inv_mpu_core_probe(). The code there does:

...

        pm_runtime_put(dev); // Already mentioned above
        result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
        if (result)
                return result;

and inv_mpu_pm_disable, in turn, calls:

	pm_runtime_put_sync_suspend(dev);

I failed to see where the corresponding get for both pm_runtime_put() and pm_runtime_put_sync_suspend(), as the remaining RPM get/put logic seemed balanced.

Quoted 61 lines
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    | 18 ++++++------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c |  6 ++----
 2 files changed, 8 insertions(+), 16 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +6 -12
index 6244a07048df..06b9c84f6bfb 100644
@@ -570,11 +570,9 @@ static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
 	freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
 	period_us = 1000000 / freq_hz;
 
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		return result;
-	}
 
 	switch (chan->type) {
 	case IIO_ANGL_VEL:
@@ -812,11 +810,9 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 		return result;
 
 	mutex_lock(&st->lock);
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto error_write_raw_unlock;
-	}
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -930,11 +926,9 @@ inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
 		result = 0;
 		goto fifo_rate_fail_unlock;
 	}
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto fifo_rate_fail_unlock;
-	}
 
 	result = regmap_write(st->map, st->reg->sample_rate_div, d);
 	if (result)
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c +0 -0
index e21ba778595a..2d0e8cdd4848 100644
@@ -173,11 +173,9 @@ static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
 
 	if (enable) {
 		scan = inv_scan_query(indio_dev);

drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable) · line 181

Quoted 10 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c +2 -4
-		result = pm_runtime_get_sync(pdev);
-		if (result < 0) {
-			pm_runtime_put_noidle(pdev);
+		result = pm_runtime_resume_and_get(pdev);
+		if (result)
 			return result;
-		}
 		/*
 		 * In case autosuspend didn't trigger, turn off first not
 		 * required sensors.

Thanks,
Mauro

Reviewed-by on v1 14/28
#53 lore raw

Re: [PATCH 14/28] iio: adc: stm32-adc: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:40 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 54 lines
---
 drivers/iio/adc/stm32-adc.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
drivers/iio/adc/stm32-adc.c +6 -12
index c9d36c88ee6a..5088de835bb1 100644
@@ -1158,11 +1158,9 @@ static int stm32_adc_single_conv(struct iio_dev *indio_dev,
 
 	adc->bufi = 0;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	/* Apply sampling time settings */
 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
@@ -1364,11 +1362,9 @@ static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
 
@@ -1413,11 +1409,9 @@ static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	if (!readval)
 		stm32_adc_writel(adc, reg, writeval);
@@ -1537,11 +1531,9 @@ static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 

drivers/iio/adc/stm32-adc.c static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev) · line 1539

Quoted 10 lines
drivers/iio/adc/stm32-adc.c +2 -4
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
 	if (ret) {

Thanks,
Mauro

Reviewed-by on v1 15/28
#57 lore raw

Re: [PATCH 15/28] iio: adc: stm32-dfsdm: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:41 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 12 lines
---
 drivers/iio/adc/stm32-dfsdm-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/stm32-dfsdm-core.c +0 -0
index bb925a11c8ae..a627af9a825e 100644
@@ -135,11 +135,9 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
 	int ret;
 
 	if (atomic_inc_return(&priv->n_active_ch) == 1) {

drivers/iio/adc/stm32-dfsdm-core.c int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm) · line 143

Quoted 10 lines
drivers/iio/adc/stm32-dfsdm-core.c +2 -4
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret < 0)
 			goto error_ret;
-		}
 
 		/* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
 		clk_src = priv->aclk ? 1 : 0;

Thanks,
Mauro

Reviewed-by on v1 16/28
#60 lore raw

Re: [PATCH 16/28] iio: dac: stm32-dac: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:42 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general

LGTM.

Quoted 9 lines
---
 drivers/iio/dac/stm32-dac.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/dac/stm32-dac.c +0 -0
index a5b0a52bf86e..dd2e306824e7 100644

drivers/iio/dac/stm32-dac.c static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch, · line 76

Quoted 10 lines
drivers/iio/dac/stm32-dac.c +1 -2
@@ -69,9 +69,8 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 	}
 
 	if (enable) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
 			mutex_unlock(&dac->lock);
 			return ret;
 		}

Thanks,
Mauro

#63 lore raw

Re: [PATCH 17/28] iio: adc: ads1015: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:43 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 9 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

This driver has an extra (unbalanced?) call to pm_runtime_put_noidle() at ads1015_remove().

Quoted 9 lines
---
 drivers/iio/adc/ti-ads1015.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/ti-ads1015.c +0 -0
index 9fef39bcf997..4b61ee3d2501 100644

drivers/iio/adc/ti-ads1015.c static int ads1015_set_power_state(struct ads1015_data *data, bool on) · line 329

Quoted 10 lines
drivers/iio/adc/ti-ads1015.c +1 -3
@@ -323,9 +323,7 @@ static int ads1015_set_power_state(struct ads1015_data *data, bool on)
 	struct device *dev = regmap_get_device(data->regmap);
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);

Thanks,
Mauro

#67 lore raw

Re: [PATCH 18/28] iio: chemical: atlas-sensor: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:44 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 9 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

As pm_runtime_resume_and_get() returns <= 0 take advantage of that to change the error checking to if (ret) which is more in keeping with the rest of this driver.

Quoted 5 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Same case as patch 17: this driver also seems to have an extra (unbalanced?) call to pm_runtime_disable() at remove().

Quoted 26 lines
---
 drivers/iio/chemical/atlas-sensor.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/iio/chemical/atlas-sensor.c +2 -4
index d10f921b233a..90bb3f5bff19 100644
@@ -410,11 +410,9 @@ static int atlas_buffer_postenable(struct iio_dev *indio_dev)
 	struct atlas_data *data = iio_priv(indio_dev);
 	int ret;
 
-	ret = pm_runtime_get_sync(&data->client->dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret)
 		return ret;
-	}
 
 	return atlas_set_interrupt(data, true);
 }
@@ -487,11 +485,9 @@ static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
 	int suspended = pm_runtime_suspended(dev);
 	int ret;
 

drivers/iio/chemical/atlas-sensor.c static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val) · line 493

Quoted 10 lines
drivers/iio/chemical/atlas-sensor.c +2 -4
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
 		return ret;
-	}
 
 	if (suspended)
 		msleep(data->chip->delay);

Thanks,
Mauro

Reviewed-by on v1 20/28
#73 lore raw

Re: [PATCH 20/28] iio: light: us5182: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:46 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 9 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 9 lines
---
 drivers/iio/light/us5182d.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/us5182d.c +0 -0
index 393f27b75c75..96e4a66ddf28 100644

drivers/iio/light/us5182d.c static int us5182d_set_power_state(struct us5182d_data *data, bool on) · line 373

Quoted 10 lines
drivers/iio/light/us5182d.c +1 -3
@@ -367,9 +367,7 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on)
 		return 0;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);

Thanks,
Mauro

Reviewed-by on v1 21/28
#76 lore raw

Re: [PATCH 21/28] iio: light: vcnl4000: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:47 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 9 lines
---
 drivers/iio/light/vcnl4000.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4000.c +0 -0
index 4a61865d01cd..01772327a947 100644

drivers/iio/light/vcnl4000.c static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on) · line 419

Quoted 10 lines
drivers/iio/light/vcnl4000.c +1 -3
@@ -413,9 +413,7 @@ static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);

Thanks,
Mauro

Reviewed-by on v1 22/28
#79 lore raw

Re: [PATCH 22/28] iio: light: vcnl4035: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:48 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Cc Parthiban Nallathambi <[email protected]>

LGTM.

Quoted 9 lines
---
 drivers/iio/light/vcnl4035.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4035.c +0 -0
index 691a54b763e1..fd2f181b16db 100644

drivers/iio/light/vcnl4035.c static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on) · line 150

Quoted 10 lines
drivers/iio/light/vcnl4035.c +1 -3
@@ -144,9 +144,7 @@ static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on)
 	struct device *dev = &data->client->dev;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);

Thanks,
Mauro

#82 lore raw

Re: [PATCH 23/28] iio: pressure: zpa2326: fix potential extra call of runtime suspend.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:49 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 11 lines

From: Jonathan Cameron <[email protected]>

This case illustrates why the new pm_runtime_sync_and_get() is good in that it makes clear the correct way to handle errors.

Calling pm_runtime_put() on failure of the _get() in pm_runtime_get_sync() will potentially result in powering down an already powered down device (as we never successfully powered it up. Unlikely to cause any problems in reality.

This one seems a little odd on my eyes, although I don't know much about IIO, as it calls RPM get only at:

	zpa2326_init_runtime()

on a balanced way, as the routine starts with RPM get() ends with RPM put().

Then it does a put at zpa2326_suspend() and a get at zpa2326_resume().

Can RPM usage_count be zero at suspend (or are there some other part of IIO core that increments it?). Because, if after resume, usage_count would be equal to 1, as I guess RPM core prevent negative values.

Regards,
Mauro

Quoted 12 lines
---
 drivers/iio/pressure/zpa2326.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/pressure/zpa2326.c +0 -0
index 89295c90f801..97ac3ba399f7 100644
@@ -664,11 +664,9 @@ static int zpa2326_resume(const struct iio_dev *indio_dev)
 {
 	int err;
 

drivers/iio/pressure/zpa2326.c static int zpa2326_resume(const struct iio_dev *indio_dev) · line 672

Quoted 10 lines
drivers/iio/pressure/zpa2326.c +2 -4
-	err = pm_runtime_get_sync(indio_dev->dev.parent);
-	if (err < 0) {
-		pm_runtime_put(indio_dev->dev.parent);
+	err = pm_runtime_resume_and_get(indio_dev->dev.parent);
+	if (err < 0)
 		return err;
-	}
 
 	if (err > 0) {
 		/*

Thanks,
Mauro

Reviewed-by on v1 24/28
#85 lore raw

Re: [PATCH 24/28] iio: temp: mlx90614: Handle failure in pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:50 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Converts from using pm_runtime_get_sync() with no error handling over to pm_runtime_resume_and_get() which will ensure we don't end up holding a reference. Ensure this error return is then handled at calls to mlx90614_power_get(). These are all direct returns.

LGTM.

Quoted 62 lines
---
 drivers/iio/temperature/mlx90614.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/iio/temperature/mlx90614.c +16 -4
index ef0fec94d269..afcb10ea7c44 100644
@@ -176,11 +176,14 @@ static inline s32 mlx90614_iir_search(const struct i2c_client *client,
 static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
 {
 	unsigned long now;
+	int ret;
 
 	if (!data->wakeup_gpio)
 		return 0;
 
-	pm_runtime_get_sync(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret < 0)
+		return ret;
 
 	if (startup) {
 		now = jiffies;
@@ -267,7 +270,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		*val = MLX90614_CONST_SCALE;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client,
 					       MLX90614_EMISSIVITY);
@@ -287,7 +293,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT_PLUS_NANO;
 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR setting with
 							     FIR = 1024 */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
 		mutex_unlock(&data->lock);
@@ -319,7 +328,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX +
 			val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
 					  val);
@@ -331,7 +343,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		if (val < 0 || val2 < 0)

drivers/iio/temperature/mlx90614.c static int mlx90614_write_raw(struct iio_dev *indio_dev, · line 352

Quoted 10 lines
drivers/iio/temperature/mlx90614.c +4 -1
 			return -EINVAL;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_iir_search(data->client,
 					  val * 100 + val2 / 10000);

Thanks,
Mauro

Reviewed-by on v1 25/28
#88 lore raw

Re: [PATCH 25/28] iio: proximity: srf04: Use pm_runtime_resume_and_get() and handle error

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:51 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Previously this driver used pm_runtime_sync_get() and did not handle any errors that occurred. Moving to the new pm_runtime_resume_and_get() + handle errors.

LGTM.

Quoted 13 lines
---
 drivers/iio/proximity/srf04.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/proximity/srf04.c +0 -1
index 420c37c72de4..fe88b2bb60bc 100644
@@ -100,9 +100,11 @@ static int srf04_read(struct srf04_data *data)
 	u64 dt_ns;
 	u32 time_ns, distance_mm;
 
-	if (data->gpiod_power)

drivers/iio/proximity/srf04.c static int srf04_read(struct srf04_data *data) · line 110

Quoted 10 lines
drivers/iio/proximity/srf04.c +5 -2
-		pm_runtime_get_sync(data->dev);
-
+	if (data->gpiod_power) {
+		ret = pm_runtime_resume_and_get(data->dev);
+		if (ret < 0)
+			return ret;
+	}
 	/*
 	 * just one read-echo-cycle can take place at a time
 	 * ==> lock against concurrent reading calls

Thanks,
Mauro

#91 lore raw

Re: [PATCH 26/28] iio: prox: pulsed-light-v2: Switch pm_runtime_resume_and_get()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:52 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 16 lines

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle any errors as a result of runtime resume as it exits without leaving the reference count elevated.

---
 drivers/iio/proximity/pulsedlight-lidar-lite-v2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +0 -0
index c685f10b5ae4..ca8dc5cc209a 100644

drivers/iio/proximity/pulsedlight-lidar-lite-v2.c static int lidar_get_measurement(struct lidar_data *data, u16 *reg) · line 162

Quoted 10 lines
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +3 -1
@@ -154,7 +154,9 @@ static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
 	int tries = 10;
 	int ret;
 
-	pm_runtime_get_sync(&client->dev);
+	ret = pm_runtime_resume_and_get(&client->dev);
+	if (ret < 0)
+		return ret;
 
 	/* start sample */
 	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);

There's a return just after this that it is missing a call to pm_runtime_put_autosuspend():

	if (ret < 0) {
                dev_err(&client->dev, "cannot send start measurement command");
                return ret;
        }

Perhaps the best here would be to add a goto and a label before the return logic.

Thanks,
Mauro

#94 lore raw

Re: [PATCH 27/28] iio: pressure: icp10100: Use pm_runtime_resume_and_get() + handle errors

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:53 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 16 lines

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle failures in resume as it doesn't hold a reference count if it exits with an error.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 drivers/iio/pressure/icp10100.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/pressure/icp10100.c +0 -0
index 48759fc4bf18..80cd66688731 100644

drivers/iio/pressure/icp10100.c static int icp10100_get_measures(struct icp10100_state *st, · line 258

Quoted 10 lines
drivers/iio/pressure/icp10100.c +3 -1
@@ -250,7 +250,9 @@ static int icp10100_get_measures(struct icp10100_state *st,
 	__be16 measures[3];
 	int ret;
 
-	pm_runtime_get_sync(&st->client->dev);
+	ret = pm_runtime_resume_and_get(&st->client->dev);
+	if (ret < 0)
+		return ret;
 
 	mutex_lock(&st->lock);
 	cmd = &icp10100_cmd_measure[st->mode];

This one has a logic similar to a patch I commented earlier in this series. At probing time, it does:

        pm_runtime_get_noresume(&client->dev);
        pm_runtime_set_active(&client->dev);
        pm_runtime_enable(&client->dev);
        pm_runtime_set_autosuspend_delay(&client->dev, 2000);
        pm_runtime_use_autosuspend(&client->dev);
        pm_runtime_put(&client->dev);
        ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
                                       &client->dev);

Here, get/put are balanced, but the icp10100_pm_disable logic is:

static void icp10100_pm_disable(void *data)

{
        struct device *dev = data;

        pm_runtime_put_sync_suspend(dev);
        pm_runtime_disable(dev);
}

Not sure what RPM get() it is trying to balance there.

Thanks,
Mauro

Reviewed-by on v1 28/28
#97 lore raw

Re: [PATCH 28/28] iio: adc: rcar-gyroadc: Use pm_runtime_resume_and_get() and check in probe()

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:54 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

1 instance found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/
The other instance changed did not check for failure of the pm_runtime_get_sync() so that is added.

Quoted 8 lines

Note the remaining pm_runtime_get_sync() call is left alone because it is not obvious what to do on failure to power up in remove()

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 42 lines
---
 drivers/iio/adc/rcar-gyroadc.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iio/adc/rcar-gyroadc.c +6 -8
index 9f38cf3c7dc2..a48895046408 100644
@@ -162,18 +162,13 @@ static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
 static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
 {
 	struct device *dev = priv->dev;
-	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		return pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
-		ret = pm_runtime_put_autosuspend(dev);
+		return pm_runtime_put_autosuspend(dev);
 	}
-
-	return ret;
 }
 
 static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
@@ -535,7 +530,10 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_enable(dev);
 
-	pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
+		goto err_power_up;
+
 	rcar_gyroadc_hw_init(priv);
 	rcar_gyroadc_hw_start(priv);
 

drivers/iio/adc/rcar-gyroadc.c static int rcar_gyroadc_probe(struct platform_device *pdev) · line 556

Quoted 7 lines
drivers/iio/adc/rcar-gyroadc.c +1 -0
@@ -552,6 +550,7 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 err_iio_device_register:
 	rcar_gyroadc_hw_stop(priv);
 	pm_runtime_put_sync(dev);
+err_power_up:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
 	clk_disable_unprepare(priv->clk);

Thanks,
Mauro

#5 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

In reply to Mauro Carvalho Chehab

Hi Mauro,

Quoted 36 lines · Mauro Carvalho Chehab

Em Wed, 12 May 2021 15:22:43 +0200
Mauro Carvalho Chehab <[email protected]> escreveu:

Quoted 18 lines

Em Sun, 9 May 2021 12:33:27 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 15 lines

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines

LGTM.

Hmm...not quite...

Quoted 6 lines
Quoted 6 lines
drivers/iio/gyro/fxas21002c_core.c +0 -0
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/gyro/fxas21002c_core.c +0 -2
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
Quoted 8 lines

drivers/iio/gyro/fxas21002c_core.c static int fxas21002c_write(struct fxas21002c_data *data, · line 370

Quoted 8 lines
drivers/iio/gyro/fxas21002c_core.c +1 -6
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }

fxas21002c_temp_get() and fxas21002c_axis_get() seem to be missing a pm_runtime_put*() if something gets wrong at regmap_field_read(), e. g.:

Thanks for catching this, you are right. However I think it should have is own patch, since it is a real fix and this one is not.

Jonathan, bellow goes a proper patch, please feel free to squash this on top of yours, or just apply it separately, your call.

Cheers,
Rui

8<-------------------------------------------------------------------

From 314a56247515c05c0c5504c1e305127878af0c9b Mon Sep 17 00:00:00 2001
From: Rui Miguel Silva <[email protected]>
Date: Wed, 12 May 2021 23:39:29 +0100
Subject: [PATCH] iio: gyro: fxas21002c: balance runtime power in error path

If we fail to read temperature or axis we need to decrement the runtime pm reference count to trigger autosuspend.

Add the call to pm_put to do that in case of error.

Fixes a0701b6263ae ("iio: gyro: add core driver for fxas21002c")
---
 drivers/iio/gyro/fxas21002c_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/gyro/fxas21002c_core.c +2 -0
index 1a20c6b88e7d..645461c70454 100644
@@ -399,6 +399,7 @@ static int fxas21002c_temp_get(struct fxas21002c_data *data, int *val)
 	ret = regmap_field_read(data->regmap_fields[F_TEMP], &temp);
 	if (ret < 0) {
 		dev_err(dev, "failed to read temp: %d\n", ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
@@ -432,6 +433,7 @@ static int fxas21002c_axis_get(struct fxas21002c_data *data,
 			       &axis_be, sizeof(axis_be));
 	if (ret < 0) {
 		dev_err(dev, "failed to read axis: %d: %d\n", index, ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

Quoted 72 lines · On Thu, 13 May 2021 09:34:52 +0100 "Rui Miguel Silva" <[email protected]>…

Hi Mauro,

Quoted 36 lines · Mauro Carvalho Chehab

Em Wed, 12 May 2021 15:22:43 +0200
Mauro Carvalho Chehab <[email protected]> escreveu:

Quoted 18 lines

Em Sun, 9 May 2021 12:33:27 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 15 lines

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines

LGTM.

Hmm...not quite...

Quoted 6 lines
Quoted 6 lines
drivers/iio/gyro/fxas21002c_core.c +0 -0
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/gyro/fxas21002c_core.c +0 -2
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
Quoted 8 lines

drivers/iio/gyro/fxas21002c_core.c static int fxas21002c_write(struct fxas21002c_data *data, · line 370

Quoted 8 lines
drivers/iio/gyro/fxas21002c_core.c +1 -6
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }  

fxas21002c_temp_get() and fxas21002c_axis_get() seem to be missing a pm_runtime_put*() if something gets wrong at regmap_field_read(), e. g.:

Thanks for catching this, you are right. However I think it should have is own patch, since it is a real fix and this one is not.

Jonathan, bellow goes a proper patch, please feel free to squash this on top of yours, or just apply it separately, your call.

Cheers,
Rui

8<-------------------------------------------------------------------

From 314a56247515c05c0c5504c1e305127878af0c9b Mon Sep 17 00:00:00 2001
From: Rui Miguel Silva <[email protected]>
Date: Wed, 12 May 2021 23:39:29 +0100
Subject: [PATCH] iio: gyro: fxas21002c: balance runtime power in error path

Quoted 8 lines

If we fail to read temperature or axis we need to decrement the runtime pm reference count to trigger autosuspend.

Add the call to pm_put to do that in case of error.

Fixes a0701b6263ae ("iio: gyro: add core driver for fxas21002c")

I've queued this as a fix in my fixes-togreg branch of iio.git and marked it for stable.

Thanks,

Jonathan

Quoted 24 lines
---
 drivers/iio/gyro/fxas21002c_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/gyro/fxas21002c_core.c +2 -0
index 1a20c6b88e7d..645461c70454 100644
@@ -399,6 +399,7 @@ static int fxas21002c_temp_get(struct fxas21002c_data *data, int *val)
 	ret = regmap_field_read(data->regmap_fields[F_TEMP], &temp);
 	if (ret < 0) {
 		dev_err(dev, "failed to read temp: %d\n", ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
@@ -432,6 +433,7 @@ static int fxas21002c_axis_get(struct fxas21002c_data *data,
 			       &axis_be, sizeof(axis_be));
 	if (ret < 0) {
 		dev_err(dev, "failed to read axis: %d: %d\n", index, ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
author maintainer
#7 lore raw

Re: [PATCH 01/28] iio: gyro: fxa210002c: Balance runtime pm + use pm_runtime_resume_and_get().

Quoted 27 lines · On Thu, 13 May 2021 17:31:31 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 24 lines · On Thu, 13 May 2021 09:34:52 +0100 "Rui Miguel Silva" <[email protected]>…

Hi Mauro,

Quoted 21 lines · Mauro Carvalho Chehab

Em Wed, 12 May 2021 15:22:43 +0200
Mauro Carvalho Chehab <[email protected]> escreveu:

Quoted 18 lines

Em Sun, 9 May 2021 12:33:27 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 15 lines

From: Jonathan Cameron <[email protected]>

In both the probe() error path and remove() pm_runtime_put_noidle() is called which will decrement the runtime pm reference count. However, there is no matching function to have raised the reference count. Not this isn't a fix as the runtime pm core will stop the reference count going negative anyway.

An alternative would have been to raise the count in these paths, but it is not clear why that would be necessary.

Whilst we are here replace some boilerplate with pm_runtime_resume_and_get() Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

LGTM.

Hmm...not quite...

Because the fix and the cleanup are in substantially different places in the driver I'm going to assume git will sort it out and apply the original patch to my togreg branch - initially pushed out as testing for bots to poke at.

Thanks,

Jonathan

Quoted 83 lines
Quoted 40 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/gyro/fxas21002c_core.c +0 -0
index 5af7b48ff01a..539585b0d300 100644
@@ -366,14 +366,7 @@ static int fxas21002c_write(struct fxas21002c_data *data,
 
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/gyro/fxas21002c_core.c +0 -2
 static int  fxas21002c_pm_get(struct fxas21002c_data *data)
 {
-	struct device *dev = regmap_get_device(data->regmap);
-	int ret;
Quoted 8 lines

drivers/iio/gyro/fxas21002c_core.c static int fxas21002c_write(struct fxas21002c_data *data, · line 370

Quoted 8 lines
drivers/iio/gyro/fxas21002c_core.c +1 -6
-
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0)
-		pm_runtime_put_noidle(dev);
-
-	return ret;
+	return pm_runtime_resume_and_get(regmap_get_device(data->regmap));
 }    

fxas21002c_temp_get() and fxas21002c_axis_get() seem to be missing a pm_runtime_put*() if something gets wrong at regmap_field_read(), e. g.:

Thanks for catching this, you are right. However I think it should have is own patch, since it is a real fix and this one is not.

Jonathan, bellow goes a proper patch, please feel free to squash this on top of yours, or just apply it separately, your call.

Cheers,
Rui

8<-------------------------------------------------------------------

From 314a56247515c05c0c5504c1e305127878af0c9b Mon Sep 17 00:00:00 2001
From: Rui Miguel Silva <[email protected]>
Date: Wed, 12 May 2021 23:39:29 +0100
Subject: [PATCH] iio: gyro: fxas21002c: balance runtime power in error path

Quoted 8 lines

If we fail to read temperature or axis we need to decrement the runtime pm reference count to trigger autosuspend.

Add the call to pm_put to do that in case of error.

Fixes a0701b6263ae ("iio: gyro: add core driver for fxas21002c")

I've queued this as a fix in my fixes-togreg branch of iio.git and marked it for stable.

Thanks,

Jonathan

Quoted 24 lines
---
 drivers/iio/gyro/fxas21002c_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/gyro/fxas21002c_core.c +2 -0
index 1a20c6b88e7d..645461c70454 100644
@@ -399,6 +399,7 @@ static int fxas21002c_temp_get(struct fxas21002c_data *data, int *val)
 	ret = regmap_field_read(data->regmap_fields[F_TEMP], &temp);
 	if (ret < 0) {
 		dev_err(dev, "failed to read temp: %d\n", ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
@@ -432,6 +433,7 @@ static int fxas21002c_axis_get(struct fxas21002c_data *data,
 			       &axis_be, sizeof(axis_be));
 	if (ret < 0) {
 		dev_err(dev, "failed to read axis: %d: %d\n", index, ret);
+		fxas21002c_pm_put(data);
 		goto data_unlock;
 	}
 
author maintainer Applied
#10 lore raw

Re: [PATCH 02/28] iio: light: isl29028: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 15 lines · On Wed, 12 May 2021 15:33:35 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:28 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 12 lines

From: Jonathan Cameron <[email protected]>

In remove this driver called pm_runtime_put_noidle() but there is no matching get operation. This does not cause any problems because the reference counter will not change if already zero, but it does make the code harder to reason about so should be dropped.

Whilst we are here, use pm_runtime_resume_and_get() to replace open coded version.
Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines
Cc Sebastian Reichel <[email protected]>

LGTM.

Applied to the togreg branch of iio.git

Thanks,

Jonathan

Quoted 33 lines
Quoted 21 lines
---
 drivers/iio/light/isl29028.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/light/isl29028.c +1 -3
index 2f8b494f3e08..9de3262aa688 100644
@@ -339,9 +339,7 @@ static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);

drivers/iio/light/isl29028.c static int isl29028_remove(struct i2c_client *client) · line 650

Quoted 7 lines
drivers/iio/light/isl29028.c +0 -1
@@ -647,7 +645,6 @@ static int isl29028_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	return isl29028_clear_configure_reg(chip);
 }  

Thanks,
Mauro

author maintainer Applied
#14 lore raw

Re: [PATCH 03/28] iio: light: tsl2583: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Brian Masney
Quoted 8 lines · On Mon, 10 May 2021 06:46:01 -0400 Brian Masney <[email protected]> wrote:
Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Error paths in read_raw() and write_raw() callbacks failed to perform and type of runtime pm put(). Remove called pm_runtime_put_noidle() but there is no equivalent get (this is safe because the reference count is protected against going below zero, but it is misleading.

Quoted 7 lines
Quoted 4 lines

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

Applied. Thanks,

Jonathan

author maintainer
#18 lore raw

Re: [PATCH 04/28] iio: accel: bmc150-accel: Balanced runtime pm + use pm_runtime_resume_and_get()

In reply to Hans de Goede
Quoted 14 lines · On Sun, 9 May 2021 15:52:44 +0200 Hans de Goede <[email protected]> wrote:

Hi,

Quoted 11 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

A call to pm_runtime_put_noidle() doesn't match any call that would result in a get(). It is safe because runtime pm core protects against the reference counter going 0, but it makes it harder to understand the code.

Whilst here use pm_runtime_resume_and_get() to tidy things up. The Coccinelle script didn't get this one due to more complex code structure.

Quoted 6 lines
Quoted 2 lines

Thanks, patch looks good to me:

Applied to the togreg branch of iio.git, pushed out as testing for all the normal reasons.

Thanks,

Jonathan

Quoted 43 lines

Regards,

Hans

Quoted 36 lines
---
 drivers/iio/accel/bmc150-accel-core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +1 -5
index 62a164a7b852..43cfadf8f6b7 100644
@@ -389,7 +389,7 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);
@@ -398,9 +398,6 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
 	if (ret < 0) {
 		dev_err(dev,
 			"Failed: %s for %d\n", __func__, on);
-		if (on)
-			pm_runtime_put_noidle(dev);
-
 		return ret;
 	}
 
@@ -1836,7 +1833,6 @@ int bmc150_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
 
author maintainer Applied
#25 lore raw

Re: [PATCH 06/28] iio: accel: mma9551/3: Balance untime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 5 lines · On Wed, 12 May 2021 15:37:57 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:32 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 2 lines

From: Jonathan Cameron <[email protected]>

Quoted 10 lines
Quoted 4 lines

Both these drivers call pm_runtime_put_no_idle() when the reference count should already be zero as there is no matching get()

Whilst here use pm_runtime_resume_and_get() rather than open coding.

LGTM.

Quoted 1 line

Applied. Thanks,

Quoted 57 lines
Quoted 45 lines
---
 drivers/iio/accel/mma9551.c      | 1 -
 drivers/iio/accel/mma9551_core.c | 4 +---
 drivers/iio/accel/mma9553.c      | 1 -
 3 files changed, 1 insertion(+), 5 deletions(-)
drivers/iio/accel/mma9551.c +0 -1
index 08a2303cc9df..4c359fb05480 100644
@@ -515,7 +515,6 @@ static int mma9551_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);
drivers/iio/accel/mma9551_core.c +1 -3
index 666e7a04a7d7..fbf2e2c45678 100644
@@ -664,7 +664,7 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on)
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -673,8 +673,6 @@ int mma9551_set_power_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}
drivers/iio/accel/mma9553.c +0 -0
index c15908faa381..ba3ecb3b57dc 100644

drivers/iio/accel/mma9553.c static int mma9553_remove(struct i2c_client *client) · line 1159

Quoted 7 lines
drivers/iio/accel/mma9553.c +0 -1
@@ -1154,7 +1154,6 @@ static int mma9553_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	mutex_lock(&data->mutex);
 	mma9551_set_device_state(data->client, false);  

Thanks,
Mauro

author maintainer Applied
#36 lore raw

Re: [PATCH 09/28] iio: imu: kmx61: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 9 lines · On Wed, 12 May 2021 15:45:11 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:35 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 6 lines

From: Jonathan Cameron <[email protected]>

No point in calling pm_runtime_put_noidle() that isn't balancing a get. Note no actual impact because the runtime pm core protects against a negative reference counter.

Quoted 8 lines
Quoted 4 lines

For the pm_runtime_resume_and_get() main interest is in clearing out this old pattern to avoid it getting coppied into new submissions.

LGTM.

Applied. Thanks, J

Quoted 40 lines
Quoted 28 lines
---
 drivers/iio/imu/kmx61.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/imu/kmx61.c +1 -3
index d3e06ce99c1e..1dabfd615dab 100644
@@ -750,7 +750,7 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -759,8 +759,6 @@ static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
 		dev_err(&data->client->dev,
 			"Failed: kmx61_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
 
 		return ret;
 	}

drivers/iio/imu/kmx61.c static int kmx61_remove(struct i2c_client *client) · line 1429

Quoted 7 lines
drivers/iio/imu/kmx61.c +0 -1
@@ -1426,7 +1424,6 @@ static int kmx61_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	if (client->irq > 0) {
 		iio_triggered_buffer_cleanup(data->acc_indio_dev);  

Thanks,
Mauro

author maintainer
#52 lore raw

Re: [PATCH 14/28] iio: adc: stm32-adc: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Fabrice Gasnier
Quoted 15 lines · On Wed, 12 May 2021 15:30:14 +0200 Fabrice Gasnier…
Quoted 5 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-adc.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)  
Quoted 3 lines

Hi Jonathan,

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to see if they can find anything we missed.

Thanks,

Jonathan

Quoted 67 lines

Thanks for the patch,
Regards,
Fabrice

Quoted 62 lines
drivers/iio/adc/stm32-adc.c +8 -16
index c9d36c88ee6a..5088de835bb1 100644
@@ -1158,11 +1158,9 @@ static int stm32_adc_single_conv(struct iio_dev *indio_dev,
 
 	adc->bufi = 0;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	/* Apply sampling time settings */
 	stm32_adc_writel(adc, regs->smpr[0], adc->smpr_val[0]);
@@ -1364,11 +1362,9 @@ static int stm32_adc_update_scan_mode(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength);
 
@@ -1413,11 +1409,9 @@ static int stm32_adc_debugfs_reg_access(struct iio_dev *indio_dev,
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	if (!readval)
 		stm32_adc_writel(adc, reg, writeval);
@@ -1537,11 +1531,9 @@ static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev)
 	struct device *dev = indio_dev->dev.parent;
 	int ret;
 
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = stm32_adc_set_trig(indio_dev, indio_dev->trig);
 	if (ret) {
author maintainer Applied
#56 lore raw

Re: [PATCH 15/28] iio: adc: stm32-dfsdm: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Fabrice Gasnier
Quoted 16 lines · On Wed, 12 May 2021 15:31:33 +0200 Fabrice Gasnier…
Quoted 5 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

---
 drivers/iio/adc/stm32-dfsdm-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)  
Quoted 3 lines

Hi Jonathan,

Applied, thanks

Quoted 25 lines

Thanks for the patch,
Regards,
Fabrice

Quoted 20 lines
drivers/iio/adc/stm32-dfsdm-core.c +2 -4
index bb925a11c8ae..a627af9a825e 100644
@@ -135,11 +135,9 @@ int stm32_dfsdm_start_dfsdm(struct stm32_dfsdm *dfsdm)
 	int ret;
 
 	if (atomic_inc_return(&priv->n_active_ch) == 1) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret < 0)
 			goto error_ret;
-		}
 
 		/* select clock source, e.g. 0 for "dfsdm" or 1 for "audio" */
 		clk_src = priv->aclk ? 1 : 0;
author maintainer Applied
#61 lore raw

Re: [PATCH 16/28] iio: dac: stm32-dac: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 16:20:26 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:42 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied. Thanks

Jonathan

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/dac/stm32-dac.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/dac/stm32-dac.c +0 -0
index a5b0a52bf86e..dd2e306824e7 100644

drivers/iio/dac/stm32-dac.c static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch, · line 76

Quoted 10 lines
drivers/iio/dac/stm32-dac.c +1 -2
@@ -69,9 +69,8 @@ static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
 	}
 
 	if (enable) {
-		ret = pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
 		if (ret < 0) {
-			pm_runtime_put_noidle(dev);
 			mutex_unlock(&dac->lock);
 			return ret;
 		}  

Thanks,
Mauro

author maintainer Applied
#74 lore raw

Re: [PATCH 20/28] iio: light: us5182: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 8 lines · On Wed, 12 May 2021 16:35:33 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:46 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 5 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 9 lines
Quoted 4 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Applied.

Thanks,

Jonathan

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/light/us5182d.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/us5182d.c +0 -0
index 393f27b75c75..96e4a66ddf28 100644

drivers/iio/light/us5182d.c static int us5182d_set_power_state(struct us5182d_data *data, bool on) · line 373

Quoted 10 lines
drivers/iio/light/us5182d.c +1 -3
@@ -367,9 +367,7 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on)
 		return 0;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);  

Thanks,
Mauro

author maintainer Applied
#86 lore raw

Re: [PATCH 24/28] iio: temp: mlx90614: Handle failure in pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 10 lines · On Wed, 12 May 2021 16:51:57 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:50 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

Converts from using pm_runtime_get_sync() with no error handling over to pm_runtime_resume_and_get() which will ensure we don't end up holding a reference. Ensure this error return is then handled at calls to mlx90614_power_get(). These are all direct returns.

Quoted 5 lines
Quoted 1 line

LGTM.

Applied,

Thanks,

Jonathan

Quoted 78 lines
Quoted 63 lines
---
 drivers/iio/temperature/mlx90614.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/iio/temperature/mlx90614.c +16 -4
index ef0fec94d269..afcb10ea7c44 100644
@@ -176,11 +176,14 @@ static inline s32 mlx90614_iir_search(const struct i2c_client *client,
 static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
 {
 	unsigned long now;
+	int ret;
 
 	if (!data->wakeup_gpio)
 		return 0;
 
-	pm_runtime_get_sync(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret < 0)
+		return ret;
 
 	if (startup) {
 		now = jiffies;
@@ -267,7 +270,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		*val = MLX90614_CONST_SCALE;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client,
 					       MLX90614_EMISSIVITY);
@@ -287,7 +293,10 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT_PLUS_NANO;
 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR setting with
 							     FIR = 1024 */
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
 		mutex_unlock(&data->lock);
@@ -319,7 +328,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX +
 			val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
 					  val);
@@ -331,7 +343,10 @@ static int mlx90614_write_raw(struct iio_dev *indio_dev,
 		if (val < 0 || val2 < 0)

drivers/iio/temperature/mlx90614.c static int mlx90614_write_raw(struct iio_dev *indio_dev, · line 352

Quoted 10 lines
drivers/iio/temperature/mlx90614.c +4 -1
 			return -EINVAL;
 
-		mlx90614_power_get(data, false);
+		ret = mlx90614_power_get(data, false);
+		if (ret < 0)
+			return ret;
+
 		mutex_lock(&data->lock);
 		ret = mlx90614_iir_search(data->client,
 					  val * 100 + val2 / 10000);  

Thanks,
Mauro

author maintainer Applied
#98 lore raw

Re: [PATCH 28/28] iio: adc: rcar-gyroadc: Use pm_runtime_resume_and_get() and check in probe()

In reply to Mauro Carvalho Chehab
Quoted 16 lines · On Wed, 12 May 2021 17:01:40 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:54 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 13 lines

From: Jonathan Cameron <[email protected]>

1 instance found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/
The other instance changed did not check for failure of the pm_runtime_get_sync() so that is added.

Note the remaining pm_runtime_get_sync() call is left alone because it is not obvious what to do on failure to power up in remove()

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied. Thanks,

Jonathan

Quoted 55 lines
Quoted 43 lines
---
 drivers/iio/adc/rcar-gyroadc.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iio/adc/rcar-gyroadc.c +6 -8
index 9f38cf3c7dc2..a48895046408 100644
@@ -162,18 +162,13 @@ static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
 static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
 {
 	struct device *dev = priv->dev;
-	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		return pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
-		ret = pm_runtime_put_autosuspend(dev);
+		return pm_runtime_put_autosuspend(dev);
 	}
-
-	return ret;
 }
 
 static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
@@ -535,7 +530,10 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_enable(dev);
 
-	pm_runtime_get_sync(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
+		goto err_power_up;
+
 	rcar_gyroadc_hw_init(priv);
 	rcar_gyroadc_hw_start(priv);
 

drivers/iio/adc/rcar-gyroadc.c static int rcar_gyroadc_probe(struct platform_device *pdev) · line 556

Quoted 7 lines
drivers/iio/adc/rcar-gyroadc.c +1 -0
@@ -552,6 +550,7 @@ static int rcar_gyroadc_probe(struct platform_device *pdev)
 err_iio_device_register:
 	rcar_gyroadc_hw_stop(priv);
 	pm_runtime_put_sync(dev);
+err_power_up:
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
 	clk_disable_unprepare(priv->clk);  

Thanks,
Mauro

author maintainer
#89 lore raw

Re: [PATCH 25/28] iio: proximity: srf04: Use pm_runtime_resume_and_get() and handle error

In reply to Mauro Carvalho Chehab
Quoted 9 lines · On Wed, 12 May 2021 16:53:26 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:51 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 6 lines

From: Jonathan Cameron <[email protected]>

Previously this driver used pm_runtime_sync_get() and did not handle any errors that occurred. Moving to the new pm_runtime_resume_and_get() + handle errors.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to see if we missed anything.

Thanks,

Jonathan

Quoted 29 lines
Quoted 14 lines
---
 drivers/iio/proximity/srf04.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/proximity/srf04.c +0 -1
index 420c37c72de4..fe88b2bb60bc 100644
@@ -100,9 +100,11 @@ static int srf04_read(struct srf04_data *data)
 	u64 dt_ns;
 	u32 time_ns, distance_mm;
 
-	if (data->gpiod_power)

drivers/iio/proximity/srf04.c static int srf04_read(struct srf04_data *data) · line 110

Quoted 10 lines
drivers/iio/proximity/srf04.c +5 -2
-		pm_runtime_get_sync(data->dev);
-
+	if (data->gpiod_power) {
+		ret = pm_runtime_resume_and_get(data->dev);
+		if (ret < 0)
+			return ret;
+	}
 	/*
 	 * just one read-echo-cycle can take place at a time
 	 * ==> lock against concurrent reading calls  

Thanks,
Mauro

author maintainer
#77 lore raw

Re: [PATCH 21/28] iio: light: vcnl4000: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 16:36:36 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:47 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to poke at.

Thanks,

Jonathan

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/light/vcnl4000.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4000.c +0 -0
index 4a61865d01cd..01772327a947 100644

drivers/iio/light/vcnl4000.c static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on) · line 419

Quoted 10 lines
drivers/iio/light/vcnl4000.c +1 -3
@@ -413,9 +413,7 @@ static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);  

Thanks,
Mauro

author maintainer
#80 lore raw

Re: [PATCH 22/28] iio: light: vcnl4035: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 16:39:54 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:48 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Quoted 7 lines
Quoted 2 lines
Cc Parthiban Nallathambi <[email protected]>

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to see if they can find anything we missed.

Thanks,

Jonathan

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/light/vcnl4035.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4035.c +0 -0
index 691a54b763e1..fd2f181b16db 100644

drivers/iio/light/vcnl4035.c static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on) · line 150

Quoted 10 lines
drivers/iio/light/vcnl4035.c +1 -3
@@ -144,9 +144,7 @@ static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on)
 	struct device *dev = &data->client->dev;
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);  

Thanks,
Mauro

author maintainer
#46 lore raw

Re: [PATCH 12/28] iio: accel: bmi088: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 15:51:39 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:38 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() is not balanced by a get so drop it.

Using pm_runtime_resume_and_get() allows for simple introduction of error handling to allow for runtime resume failing.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to see if they can find anything we missed.

Thanks,

Jonathan

Quoted 78 lines
Quoted 66 lines
---
 drivers/iio/accel/bmi088-accel-core.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/iio/accel/bmi088-accel-core.c +20 -5
index 61aaaf48c040..a06dae5c971d 100644
@@ -285,11 +285,17 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW:
 		switch (chan->type) {
 		case IIO_TEMP:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = bmi088_accel_get_temp(data, val);
 			goto out_read_raw_pm_put;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = iio_device_claim_direct_mode(indio_dev);
 			if (ret)
 				goto out_read_raw_pm_put;
@@ -319,7 +325,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			*val = BMI088_ACCEL_TEMP_UNIT;
 			return IIO_VAL_INT;
 		case IIO_ACCEL:
-			pm_runtime_get_sync(dev);
+			ret = pm_runtime_resume_and_get(dev);
+			if (ret)
+				return ret;
+
 			ret = regmap_read(data->regmap,
 					  BMI088_ACCEL_REG_ACC_RANGE, val);
 			if (ret)
@@ -334,7 +343,10 @@ static int bmi088_accel_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_get_sample_freq(data, val, val2);
 		goto out_read_raw_pm_put;
 	default:
@@ -376,7 +388,10 @@ static int bmi088_accel_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		pm_runtime_get_sync(dev);
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+
 		ret = bmi088_accel_set_sample_freq(data, val);
 		pm_runtime_mark_last_busy(dev);
 		pm_runtime_put_autosuspend(dev);

drivers/iio/accel/bmi088-accel-core.c int bmi088_accel_core_remove(struct device *dev) · line 550

Quoted 7 lines
drivers/iio/accel/bmi088-accel-core.c +0 -1
@@ -530,7 +545,6 @@ int bmi088_accel_core_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 	bmi088_accel_power_down(data);
 
 	return 0;  

Thanks,
Mauro

author maintainer
#42 lore raw

Re: [PATCH 11/28] iio: magn: bmc150: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 14 lines · On Wed, 12 May 2021 15:48:57 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:37 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 11 lines

From: Jonathan Cameron <[email protected]>

probe() error paths after runtime pm is enabled, should disable it. remove() should not call pm_runtime_put_noidle() as there is no matching get() to have raised the reference count. This case has no affect a the runtime pm core protects against going negative.

Whilst here use pm_runtime_resume_and_get() to tidy things up a little. coccicheck script didn't get this one due to complex code structure so found by inspection.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to take a poke at it.

Still time for additional reviews as this won't go out in a non rebasing tree for a few days yet.

Thanks,

Jonathan

Quoted 57 lines
Quoted 45 lines
---
 drivers/iio/magnetometer/bmc150_magn.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +4 -5
index d75b437a43f2..39920cb764bf 100644
@@ -262,7 +262,7 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(data->dev);
+		ret = pm_runtime_resume_and_get(data->dev);
 	} else {
 		pm_runtime_mark_last_busy(data->dev);
 		ret = pm_runtime_put_autosuspend(data->dev);
@@ -271,9 +271,6 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
 	if (ret < 0) {
 		dev_err(data->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(data->dev);
-
 		return ret;
 	}
 #endif
@@ -963,12 +960,14 @@ int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
 	ret = iio_device_register(indio_dev);
 	if (ret < 0) {
 		dev_err(dev, "unable to register iio device\n");
-		goto err_buffer_cleanup;
+		goto err_disable_runtime_pm;
 	}
 
 	dev_dbg(dev, "Registered device %s\n", name);
 	return 0;
 
+err_disable_runtime_pm:
+	pm_runtime_disable(dev);
 err_buffer_cleanup:
 	iio_triggered_buffer_cleanup(indio_dev);
 err_free_irq:

drivers/iio/magnetometer/bmc150_magn.c int bmc150_magn_remove(struct device *dev) · line 996

Quoted 7 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -1
@@ -992,7 +991,6 @@ int bmc150_magn_remove(struct device *dev)
 
 	pm_runtime_disable(dev);
 	pm_runtime_set_suspended(dev);
-	pm_runtime_put_noidle(dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 

Thanks,
Mauro

author maintainer
#39 lore raw

Re: [PATCH 10/28] iio: light: rpr0521: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 15:46:32 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:36 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Calls to pm_runtime_put_noidle in probe() error path and remove() are not match to any get() calls.

The runtime pm core protects against negative reference counts, so this doesn't have any visible impact beyond confusing the reader.

Quoted 8 lines
Quoted 4 lines

Whilst here use pm_runtime_resume_and_get() to replace boilerplate.

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to take a swing at it before I push it in something linux-next
will pick up. Note still time for additional reviews as I'm happy to rebase
the tree for now.

Thanks,

Jonathan

Quoted 49 lines
Quoted 37 lines
---
 drivers/iio/light/rpr0521.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/iio/light/rpr0521.c +1 -5
index 7e332de0e6a5..c2dd8a3d4217 100644
@@ -360,7 +360,7 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 	 * both stay enabled until _suspend().
 	 */
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);
 		ret = pm_runtime_put_autosuspend(&data->client->dev);
@@ -369,9 +369,6 @@ static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
 		dev_err(&data->client->dev,
 			"Failed: rpr0521_set_power_state for %d, ret %d\n",
 			on, ret);
-		if (on)
-			pm_runtime_put_noidle(&data->client->dev);
-
 		return ret;
 	}
 
@@ -1038,7 +1035,6 @@ static int rpr0521_probe(struct i2c_client *client,
 err_pm_disable:
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 err_poweroff:
 	rpr0521_poweroff(data);
 

drivers/iio/light/rpr0521.c static int rpr0521_remove(struct i2c_client *client) · line 1054

Quoted 7 lines
drivers/iio/light/rpr0521.c +0 -1
@@ -1053,7 +1049,6 @@ static int rpr0521_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	rpr0521_poweroff(iio_priv(indio_dev));
 

Thanks,
Mauro

author maintainer
#22 lore raw

Re: [PATCH 05/28] iio: accel: mma8452: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 13 lines · On Wed, 12 May 2021 15:37:10 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:31 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 10 lines

From: Jonathan Cameron <[email protected]>

Remove() callback calls pm_runtime_put_noidle() but there it is not balancing a get. No actual affect because the runtime pm core prevents the reference count going negative.

Whilst here use pm_runtime_resume_and_get() rather than open coded version. Again, coccinelle script missed this one due to more complex code structure.

Quoted 6 lines
Quoted 2 lines

LGTM.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Note additional reviews / tags are find for now as I'm happy to rebase this tree as needed until it goes out as togreg (when I try not to!)

Thanks,

Jonathan

Quoted 40 lines
Quoted 28 lines
---
 drivers/iio/accel/mma8452.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/accel/mma8452.c +1 -3
index 464a6bfe6746..715b8138fb71 100644
@@ -221,7 +221,7 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	int ret;
 
 	if (on) {
-		ret = pm_runtime_get_sync(&client->dev);
+		ret = pm_runtime_resume_and_get(&client->dev);
 	} else {
 		pm_runtime_mark_last_busy(&client->dev);
 		ret = pm_runtime_put_autosuspend(&client->dev);
@@ -230,8 +230,6 @@ static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
 	if (ret < 0) {
 		dev_err(&client->dev,
 			"failed to change power state to %d\n", on);
-		if (on)
-			pm_runtime_put_noidle(&client->dev);
 
 		return ret;
 	}

drivers/iio/accel/mma8452.c static int mma8452_remove(struct i2c_client *client) · line 1714

Quoted 7 lines
drivers/iio/accel/mma8452.c +0 -1
@@ -1711,7 +1709,6 @@ static int mma8452_remove(struct i2c_client *client)
 
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
 	mma8452_trigger_cleanup(indio_dev);  

Thanks,
Mauro

Reviewed-by on v1 19/28
#70 lore raw

Re: [PATCH 19/28] iio: light: pa12203001: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Jonathan Cameron

Em Sun, 9 May 2021 12:33:45 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 9 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Quoted 9 lines
---
 drivers/iio/light/pa12203001.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/pa12203001.c +0 -0
index bfade6577a38..a52b2c788540 100644

drivers/iio/light/pa12203001.c static int pa12203001_set_power_state(struct pa12203001_data *data, bool on, · line 192

Quoted 10 lines
drivers/iio/light/pa12203001.c +1 -3
@@ -186,9 +186,7 @@ static int pa12203001_set_power_state(struct pa12203001_data *data, bool on,
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);

Thanks,
Mauro

author maintainer Applied
#71 lore raw

Re: [PATCH 19/28] iio: light: pa12203001: Use pm_runtime_resume_and_get() to replace open coding.

Quoted 8 lines · On Fri, 14 May 2021 09:35:13 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:45 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 5 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines
Quoted 4 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

LGTM.

Thanks,

Applied to the togreg branch of iio.git.

Jonathan

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/light/pa12203001.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/pa12203001.c +0 -0
index bfade6577a38..a52b2c788540 100644

drivers/iio/light/pa12203001.c static int pa12203001_set_power_state(struct pa12203001_data *data, bool on, · line 192

Quoted 10 lines
drivers/iio/light/pa12203001.c +1 -3
@@ -186,9 +186,7 @@ static int pa12203001_set_power_state(struct pa12203001_data *data, bool on,
 	}
 
 	if (on) {
-		ret = pm_runtime_get_sync(&data->client->dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(&data->client->dev);
+		ret = pm_runtime_resume_and_get(&data->client->dev);
 
 	} else {
 		pm_runtime_mark_last_busy(&data->client->dev);  

Thanks,
Mauro

author maintainer
#31 lore raw

Re: [PATCH 08/28] iio: hid: trigger: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 15:44:30 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:34 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() callback is not balanced by any gets

Note this doesn't cause any problems beyond reader confusion as the runtime pm core protects against the reference counter going negative.

Quoted 8 lines
Quoted 4 lines

Whilst here, use pm_runtiem_resume_and_get() to simplify code a little.

Cc Srinivas Pandruvada <[email protected]>

RPM get/put logic LGTM.

After our side discussion on this one I wanted to take another look. I'm not sure I follow the logic behind the runtime_enable() path and why we don't want to enable runtime_pm until first use. However it's not directly related to this patch and as far as I can tell it's just unsuual rather than broken. As such, applied this one to the togreg branch of iio.git and pushed out as testing.

Thanks,

Jonathan

Quoted 43 lines
Quoted 31 lines
---
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -5
index 5a7b3e253e58..c06537e106e9 100644
@@ -163,18 +163,15 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 
 	if (state) {
 		atomic_inc(&st->user_requested_state);
-		ret = pm_runtime_get_sync(&st->pdev->dev);
+		ret = pm_runtime_resume_and_get(&st->pdev->dev);
 	} 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);
 	}
-	if (ret < 0) {
-		if (state)
-			pm_runtime_put_noidle(&st->pdev->dev);
+	if (ret < 0)
 		return ret;
-	}
 
 	return 0;
 #else

drivers/iio/common/hid-sensors/hid-sensor-trigger.c void hid_sensor_remove_trigger(struct iio_dev *indio_dev, · line 224

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -1
@@ -222,7 +219,6 @@ void hid_sensor_remove_trigger(struct iio_dev *indio_dev,
 		pm_runtime_disable(&attrb->pdev->dev);
 
 	pm_runtime_set_suspended(&attrb->pdev->dev);
-	pm_runtime_put_noidle(&attrb->pdev->dev);
 
 	cancel_work_sync(&attrb->work);
 	iio_trigger_unregister(attrb->trigger);  

Thanks,
Mauro

author maintainer
#49 lore raw

Re: [PATCH 13/28] iio: imu: mpu6050: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 11 lines · On Wed, 12 May 2021 16:11:02 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:39 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 8 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

pm_runtime_resume_and_get() returns <= 0 only so simplify related checks to bring this more inline with nearby calls.

Quoted 8 lines
Quoted 2 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Hmm... I guess there are some weird things happening there at inv_mpu_core_probe(). The code there does:

...
pm_runtime_put(dev); // Already mentioned above

That one is ok I think as it matches the pm_runtime_get_noresume() a couple of lines further up.

That will increment the counter, but not carry out a resume (which make sense as the device is already powered up). The pm_runtime_put() will cause the device to suspend which is fine and somewhat logical.

Quoted 7 lines
        result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
        if (result)
                return result;

and inv_mpu_pm_disable, in turn, calls:

	pm_runtime_put_sync_suspend(dev);

Good spot on that one. I'll drop it in v2.

Quoted 84 lines

I failed to see where the corresponding get for both pm_runtime_put() and pm_runtime_put_sync_suspend(), as the remaining RPM get/put logic seemed balanced.

Quoted 61 lines
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    | 18 ++++++------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c |  6 ++----
 2 files changed, 8 insertions(+), 16 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +6 -12
index 6244a07048df..06b9c84f6bfb 100644
@@ -570,11 +570,9 @@ static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
 	freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
 	period_us = 1000000 / freq_hz;
 
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		return result;
-	}
 
 	switch (chan->type) {
 	case IIO_ANGL_VEL:
@@ -812,11 +810,9 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 		return result;
 
 	mutex_lock(&st->lock);
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto error_write_raw_unlock;
-	}
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -930,11 +926,9 @@ inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
 		result = 0;
 		goto fifo_rate_fail_unlock;
 	}
-	result = pm_runtime_get_sync(pdev);
-	if (result < 0) {
-		pm_runtime_put_noidle(pdev);
+	result = pm_runtime_resume_and_get(pdev);
+	if (result)
 		goto fifo_rate_fail_unlock;
-	}
 
 	result = regmap_write(st->map, st->reg->sample_rate_div, d);
 	if (result)
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c +0 -0
index e21ba778595a..2d0e8cdd4848 100644
@@ -173,11 +173,9 @@ static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
 
 	if (enable) {
 		scan = inv_scan_query(indio_dev);

drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable) · line 181

Quoted 10 lines
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c +2 -4
-		result = pm_runtime_get_sync(pdev);
-		if (result < 0) {
-			pm_runtime_put_noidle(pdev);
+		result = pm_runtime_resume_and_get(pdev);
+		if (result)
 			return result;
-		}
 		/*
 		 * In case autosuspend didn't trigger, turn off first not
 		 * required sensors.  

Thanks,
Mauro

author maintainer
#64 lore raw

Re: [PATCH 17/28] iio: adc: ads1015: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 8 lines · On Wed, 12 May 2021 16:22:12 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:43 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 5 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines
Quoted 4 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

This driver has an extra (unbalanced?) call to pm_runtime_put_noidle() at ads1015_remove().

Good point. Dropped that as well for v2.

Quoted 25 lines
Quoted 10 lines
---
 drivers/iio/adc/ti-ads1015.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/ti-ads1015.c +0 -0
index 9fef39bcf997..4b61ee3d2501 100644

drivers/iio/adc/ti-ads1015.c static int ads1015_set_power_state(struct ads1015_data *data, bool on) · line 329

Quoted 10 lines
drivers/iio/adc/ti-ads1015.c +1 -3
@@ -323,9 +323,7 @@ static int ads1015_set_power_state(struct ads1015_data *data, bool on)
 	struct device *dev = regmap_get_device(data->regmap);
 
 	if (on) {
-		ret = pm_runtime_get_sync(dev);
-		if (ret < 0)
-			pm_runtime_put_noidle(dev);
+		ret = pm_runtime_resume_and_get(dev);
 	} else {
 		pm_runtime_mark_last_busy(dev);
 		ret = pm_runtime_put_autosuspend(dev);  

Thanks,
Mauro

author maintainer
#68 lore raw

Re: [PATCH 18/28] iio: chemical: atlas-sensor: Use pm_runtime_resume_and_get() to replace open coding.

In reply to Mauro Carvalho Chehab
Quoted 12 lines · On Wed, 12 May 2021 16:25:41 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:44 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 9 lines

From: Jonathan Cameron <[email protected]>

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

As pm_runtime_resume_and_get() returns <= 0 take advantage of that to change the error checking to if (ret) which is more in keeping with the rest of this driver.

Quoted 8 lines
Quoted 5 lines

This is a prequel to taking a closer look at the runtime pm in IIO drivers in general.

Same case as patch 17: this driver also seems to have an extra (unbalanced?) call to pm_runtime_disable() at remove().

Good point again. Matt, given this is a material change I'm going to drop your tag, please take another look.

Thanks,

Jonathan

Quoted 42 lines
Quoted 27 lines
---
 drivers/iio/chemical/atlas-sensor.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/iio/chemical/atlas-sensor.c +2 -4
index d10f921b233a..90bb3f5bff19 100644
@@ -410,11 +410,9 @@ static int atlas_buffer_postenable(struct iio_dev *indio_dev)
 	struct atlas_data *data = iio_priv(indio_dev);
 	int ret;
 
-	ret = pm_runtime_get_sync(&data->client->dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(&data->client->dev);
+	ret = pm_runtime_resume_and_get(&data->client->dev);
+	if (ret)
 		return ret;
-	}
 
 	return atlas_set_interrupt(data, true);
 }
@@ -487,11 +485,9 @@ static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val)
 	int suspended = pm_runtime_suspended(dev);
 	int ret;
 

drivers/iio/chemical/atlas-sensor.c static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val) · line 493

Quoted 10 lines
drivers/iio/chemical/atlas-sensor.c +2 -4
-	ret = pm_runtime_get_sync(dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(dev);
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret)
 		return ret;
-	}
 
 	if (suspended)
 		msleep(data->chip->delay);  

Thanks,
Mauro

author maintainer
#92 lore raw

Re: [PATCH 26/28] iio: prox: pulsed-light-v2: Switch pm_runtime_resume_and_get()

In reply to Mauro Carvalho Chehab
Quoted 33 lines · On Wed, 12 May 2021 16:56:57 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:52 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 16 lines

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle any errors as a result of runtime resume as it exits without leaving the reference count elevated.

---
 drivers/iio/proximity/pulsedlight-lidar-lite-v2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +0 -0
index c685f10b5ae4..ca8dc5cc209a 100644

drivers/iio/proximity/pulsedlight-lidar-lite-v2.c static int lidar_get_measurement(struct lidar_data *data, u16 *reg) · line 162

Quoted 10 lines
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +3 -1
@@ -154,7 +154,9 @@ static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
 	int tries = 10;
 	int ret;
 
-	pm_runtime_get_sync(&client->dev);
+	ret = pm_runtime_resume_and_get(&client->dev);
+	if (ret < 0)
+		return ret;
 
 	/* start sample */
 	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);  

There's a return just after this that it is missing a call to pm_runtime_put_autosuspend():

Quoted 7 lines
	if (ret < 0) {
                dev_err(&client->dev, "cannot send start measurement command");
                return ret;
        }

Perhaps the best here would be to add a goto and a label before the return logic.

Agreed. I've added a precursor fix patch that does exactly that.

Quoted 4 lines

Thanks,
Mauro

author maintainer
#83 lore raw

Re: [PATCH 23/28] iio: pressure: zpa2326: fix potential extra call of runtime suspend.

In reply to Mauro Carvalho Chehab
Quoted 20 lines · On Wed, 12 May 2021 16:49:58 +0200 Mauro Carvalho Chehab…

Em Sun, 9 May 2021 12:33:49 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 11 lines

From: Jonathan Cameron <[email protected]>

This case illustrates why the new pm_runtime_sync_and_get() is good in that it makes clear the correct way to handle errors.

Calling pm_runtime_put() on failure of the _get() in pm_runtime_get_sync() will potentially result in powering down an already powered down device (as we never successfully powered it up. Unlikely to cause any problems in reality.

This one seems a little odd on my eyes, although I don't know much about IIO, as it calls RPM get only at:

	zpa2326_init_runtime()
Quoted 7 lines

on a balanced way, as the routine starts with RPM get() ends with RPM put().

Then it does a put at zpa2326_suspend() and a get at zpa2326_resume().

Can RPM usage_count be zero at suspend (or are there some other part of IIO core that increments it?). Because, if after resume, usage_count would be equal to 1, as I guess RPM core prevent negative values.

I've stared at this for a few minutes and gotten nowhere in figuring out
the intent. As such I'm going to drop the patch for now and revisit
at a later date.

Jonathan

Quoted 33 lines

Regards,
Mauro

Quoted 12 lines
---
 drivers/iio/pressure/zpa2326.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/pressure/zpa2326.c +0 -0
index 89295c90f801..97ac3ba399f7 100644
@@ -664,11 +664,9 @@ static int zpa2326_resume(const struct iio_dev *indio_dev)
 {
 	int err;
 

drivers/iio/pressure/zpa2326.c static int zpa2326_resume(const struct iio_dev *indio_dev) · line 672

Quoted 10 lines
drivers/iio/pressure/zpa2326.c +2 -4
-	err = pm_runtime_get_sync(indio_dev->dev.parent);
-	if (err < 0) {
-		pm_runtime_put(indio_dev->dev.parent);
+	err = pm_runtime_resume_and_get(indio_dev->dev.parent);
+	if (err < 0)
 		return err;
-	}
 
 	if (err > 0) {
 		/*  

Thanks,
Mauro

author maintainer
#95 lore raw

Re: [PATCH 27/28] iio: pressure: icp10100: Use pm_runtime_resume_and_get() + handle errors

In reply to Mauro Carvalho Chehab
Quoted 44 lines · On Wed, 12 May 2021 17:00:04 +0200 Mauro Carvalho Chehab <[email protected]>…

Em Sun, 9 May 2021 12:33:53 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 16 lines

From: Jonathan Cameron <[email protected]>

Using this new call makes it easy to handle failures in resume as it doesn't hold a reference count if it exits with an error.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 drivers/iio/pressure/icp10100.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/pressure/icp10100.c +0 -0
index 48759fc4bf18..80cd66688731 100644

drivers/iio/pressure/icp10100.c static int icp10100_get_measures(struct icp10100_state *st, · line 258

Quoted 10 lines
drivers/iio/pressure/icp10100.c +3 -1
@@ -250,7 +250,9 @@ static int icp10100_get_measures(struct icp10100_state *st,
 	__be16 measures[3];
 	int ret;
 
-	pm_runtime_get_sync(&st->client->dev);
+	ret = pm_runtime_resume_and_get(&st->client->dev);
+	if (ret < 0)
+		return ret;
 
 	mutex_lock(&st->lock);
 	cmd = &icp10100_cmd_measure[st->mode];  

This one has a logic similar to a patch I commented earlier in this series. At probing time, it does:

        pm_runtime_get_noresume(&client->dev);
        pm_runtime_set_active(&client->dev);
        pm_runtime_enable(&client->dev);
        pm_runtime_set_autosuspend_delay(&client->dev, 2000);
        pm_runtime_use_autosuspend(&client->dev);
        pm_runtime_put(&client->dev);
        ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
                                       &client->dev);
Quoted 7 lines

Here, get/put are balanced, but the icp10100_pm_disable logic is:

static void icp10100_pm_disable(void *data)

{
        struct device *dev = data;

        pm_runtime_put_sync_suspend(dev);

I'll drop this in v2.

Thanks,

Jonathan

Quoted 7 lines
        pm_runtime_disable(dev);
}

Not sure what RPM get() it is trying to balance there.

Thanks,
Mauro

#32 lore raw

Re: [PATCH 08/28] iio: hid: trigger: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron
Quoted 25 lines · Jonathan Cameron
Quoted 15 lines · On Wed, 12 May 2021 15:44:30 +0200 Mauro Carvalho Chehab…

Em Sun,  9 May 2021 12:33:34 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 12 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() callback is not balanced by any gets

Note this doesn't cause any problems beyond reader confusion as the runtime
pm core protects against the reference counter going negative.

Whilst here, use pm_runtiem_resume_and_get() to simplify code a little.

Quoted 6 lines
Quoted 2 lines
Cc Srinivas Pandruvada <[email protected]>

RPM get/put logic LGTM.

Quoted 8 lines

After our side discussion on this one I wanted to take another look. I'm not sure I follow the logic behind the runtime_enable() path and why we don't want to enable runtime_pm until first use.  However it's not directly related to this patch and as far as I can tell it's just unsuual rather than broken.  As such, applied this one to the
togreg branch of iio.git and pushed out as testing.

This was enabled in init path but caused issue with USB sensor hub in Thinkpasd Yoga S1, where we can never power off sensor and it wakes up device immediately after suspend.

commit
ad7532cefd11d11a0814a75fb814c205ee3d9d4c

Thanks,
Srinivas

Quoted 51 lines

Thanks,

Jonathan

Quoted 46 lines
Quoted 35 lines

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

 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -0
index 5a7b3e253e58..c06537e106e9 100644
@@ -163,18 +163,15 @@ int hid_sensor_power_state(struct
 hid_sensor_common *st, bool state)


        if (state) { atomic_inc(&st->user_requested_state); -               ret = pm_runtime_get_sync(&st->pdev->dev); +               ret = pm_runtime_resume_and_get(&st->pdev->dev); } 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); }
-       if (ret < 0) {
-               if (state)
-                       pm_runtime_put_noidle(&st->pdev->dev); +       if (ret < 0)
                return ret;
-       }

        return 0;
 #else

drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -0
@@ -222,7 +219,6 @@ void hid_sensor_remove_trigger(struct iio_dev
 *indio_dev,
                 pm_runtime_disable(&attrb->pdev->dev);
Quoted 6 lines

pm_runtime_set_suspended(&attrb->pdev->dev); -       pm_runtime_put_noidle(&attrb->pdev->dev);
        cancel_work_sync(&attrb->work); iio_trigger_unregister(attrb->trigger);

Thanks,
Mauro

#33 lore raw

Re: [PATCH 08/28] iio: hid: trigger: Balance runtime pm + use pm_runtime_resume_and_get()

Quoted 35 lines · On Sun, 16 May 2021 19:19:40 -0700 Srinivas Pandruvada…
Quoted 25 lines · Jonathan Cameron
Quoted 15 lines · On Wed, 12 May 2021 15:44:30 +0200 Mauro Carvalho Chehab…

Em Sun,  9 May 2021 12:33:34 +0100
Jonathan Cameron <[email protected]> escreveu:

Quoted 12 lines

From: Jonathan Cameron <[email protected]>

The call to pm_runtime_put_noidle() in remove() callback is not balanced by any gets

Note this doesn't cause any problems beyond reader confusion as the runtime
pm core protects against the reference counter going negative.

Whilst here, use pm_runtiem_resume_and_get() to simplify code a little.

Quoted 6 lines
Quoted 2 lines
Cc Srinivas Pandruvada <[email protected]>

RPM get/put logic LGTM.

Quoted 8 lines

After our side discussion on this one I wanted to take another look. I'm not sure I follow the logic behind the runtime_enable() path and why we don't want to enable runtime_pm until first use.  However it's not directly related to this patch and as far as I can tell it's just unsuual rather than broken.  As such, applied this one to the
togreg branch of iio.git and pushed out as testing.

Quoted 6 lines

This was enabled in init path but caused issue with USB sensor hub in Thinkpasd Yoga S1, where we can never power off sensor and it wakes up device immediately after suspend.

commit
ad7532cefd11d11a0814a75fb814c205ee3d9d4c

Ah. Thanks for the history. Thanks makes a horrible kind of sense ;)

Jonathan

Quoted 58 lines

Thanks,
Srinivas

Quoted 51 lines

Thanks,

Jonathan

Quoted 46 lines
Quoted 35 lines

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

 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -0
index 5a7b3e253e58..c06537e106e9 100644
@@ -163,18 +163,15 @@ int hid_sensor_power_state(struct
 hid_sensor_common *st, bool state)


        if (state) { atomic_inc(&st->user_requested_state); -               ret = pm_runtime_get_sync(&st->pdev->dev); +               ret = pm_runtime_resume_and_get(&st->pdev->dev); } 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); }
-       if (ret < 0) {
-               if (state)
-                       pm_runtime_put_noidle(&st->pdev->dev); +       if (ret < 0)
                return ret;
-       }

        return 0;
 #else

drivers/iio/common/hid-sensors/hid-sensor-trigger.c +0 -0
@@ -222,7 +219,6 @@ void hid_sensor_remove_trigger(struct iio_dev
 *indio_dev,
                 pm_runtime_disable(&attrb->pdev->dev);
Quoted 6 lines

pm_runtime_set_suspended(&attrb->pdev->dev); -       pm_runtime_put_noidle(&attrb->pdev->dev);
        cancel_work_sync(&attrb->work); iio_trigger_unregister(attrb->trigger);

Thanks,
Mauro

Reviewed-by on v1 2/28
#11 lore raw

Re: [PATCH 02/28] iio: light: isl29028: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron
Quoted 2 lines · Jonathan Cameron

Whilst we are here, use pm_runtime_resume_and_get() to replace open coded version.

Wow that is a new API. Why didn't I know about it... Oh it is only from november last year that is why. I guess we should switch to this everywhere.

Quoted 5 lines

Found using coccicheck script under review at:
https://lore.kernel.org/lkml/[email protected]/

Cc Sebastian Reichel <[email protected]>

Yours,
Linus Walleij

Reviewed-by on v1 11/28
#43 lore raw

Re: [PATCH 11/28] iio: magn: bmc150: Balance runtime pm + use pm_runtime_resume_and_get()

In reply to Jonathan Cameron
Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

probe() error paths after runtime pm is enabled, should disable it. remove() should not call pm_runtime_put_noidle() as there is no matching get() to have raised the reference count. This case has no affect a the runtime pm core protects against going negative.

Quoted 6 lines

Whilst here use pm_runtime_resume_and_get() to tidy things up a little. coccicheck script didn't get this one due to complex code structure so found by inspection.

Yours,
Linus Walleij