diff mbox series

[net-next,1/5] net: ipa: use atomic exchange for suspend reference

Message ID 20200909002127.21089-2-elder@linaro.org
State New
Headers show
Series net: ipa: wake up system on RX available | expand

Commit Message

Alex Elder Sept. 9, 2020, 12:21 a.m. UTC
We take a single IPA clock reference to keep the clock running
until we get a system suspend operation.  When a system suspend
request arrives, we drop that reference, and if that's the last
reference (likely) we'll proceed with suspending endpoints and
disabling the IPA core clock and interconnects.

In most places we simply set the reference count to 0 or 1
atomically.  Instead--primarily to catch coding errors--use an
atomic exchange to update the reference count value, and report
an error in the event the previous value was unexpected.

In a few cases it's not hard to see that the error message should
never be reported.  Report them anyway, but add some excitement
to the message by ending it with an exclamation point.

Signed-off-by: Alex Elder <elder@linaro.org>

---
 drivers/net/ipa/ipa_main.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

-- 
2.20.1

Comments

David Miller Sept. 9, 2020, 3:27 a.m. UTC | #1
From: Alex Elder <elder@linaro.org>

Date: Tue,  8 Sep 2020 19:21:23 -0500

> We take a single IPA clock reference to keep the clock running

> until we get a system suspend operation.  When a system suspend

> request arrives, we drop that reference, and if that's the last

> reference (likely) we'll proceed with suspending endpoints and

> disabling the IPA core clock and interconnects.

> 

> In most places we simply set the reference count to 0 or 1

> atomically.  Instead--primarily to catch coding errors--use an

> atomic exchange to update the reference count value, and report

> an error in the event the previous value was unexpected.

> 

> In a few cases it's not hard to see that the error message should

> never be reported.  Report them anyway, but add some excitement

> to the message by ending it with an exclamation point.

> 

> Signed-off-by: Alex Elder <elder@linaro.org>


Please use refcount_t if you're wanting to validate things like
this.

Thank you.
Alex Elder Sept. 9, 2020, 1:43 p.m. UTC | #2
On 9/8/20 10:27 PM, David Miller wrote:
> From: Alex Elder <elder@linaro.org>

> Date: Tue,  8 Sep 2020 19:21:23 -0500

> 

>> We take a single IPA clock reference to keep the clock running

>> until we get a system suspend operation.  When a system suspend

>> request arrives, we drop that reference, and if that's the last

>> reference (likely) we'll proceed with suspending endpoints and

>> disabling the IPA core clock and interconnects.

>>

>> In most places we simply set the reference count to 0 or 1

>> atomically.  Instead--primarily to catch coding errors--use an

>> atomic exchange to update the reference count value, and report

>> an error in the event the previous value was unexpected.

>>

>> In a few cases it's not hard to see that the error message should

>> never be reported.  Report them anyway, but add some excitement

>> to the message by ending it with an exclamation point.

>>

>> Signed-off-by: Alex Elder <elder@linaro.org>

> 

> Please use refcount_t if you're wanting to validate things like

> this.


There is exactly one reference here; the "reference" is
essentially a Boolean flag.  So the value is always either
0 or 1.

I can use refcount_dec_if_one() for the 1->0 transition,
but I'm not sure how I can do the 0->1 transition with
refcount_t.  I admit I might be missing something.

Would you like me to add refcount_inc_if_zero()?

Otherwise would you prefer a different naming convention
to use for this Boolean "reference count"?

Thanks.

					-Alex

> 

> Thank you.

>
David Miller Sept. 9, 2020, 9:14 p.m. UTC | #3
From: Alex Elder <elder@linaro.org>

Date: Wed, 9 Sep 2020 08:43:44 -0500

> There is exactly one reference here; the "reference" is

> essentially a Boolean flag.  So the value is always either

> 0 or 1.


Aha, then why not use a bitmask and test_and_set_bit() et al.?
Alex Elder Sept. 9, 2020, 9:23 p.m. UTC | #4
On 9/9/20 4:14 PM, David Miller wrote:
> From: Alex Elder <elder@linaro.org>

> Date: Wed, 9 Sep 2020 08:43:44 -0500

> 

>> There is exactly one reference here; the "reference" is

>> essentially a Boolean flag.  So the value is always either

>> 0 or 1.

> 

> Aha, then why not use a bitmask and test_and_set_bit() et al.?


OK I'll go take a look at that option.  It's overkill for
bits used but it makes it more obvious it's a single bit,
so it's probably a better idea.

I'll try to turn this around again by tomorrow.  Thank you.

					-Alex
diff mbox series

Patch

diff --git a/drivers/net/ipa/ipa_main.c b/drivers/net/ipa/ipa_main.c
index 1fdfec41e4421..6b843fc989122 100644
--- a/drivers/net/ipa/ipa_main.c
+++ b/drivers/net/ipa/ipa_main.c
@@ -83,6 +83,7 @@  static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
 	/* Take a a single clock reference to prevent suspend.  All
 	 * endpoints will be resumed as a result.  This reference will
 	 * be dropped when we get a power management suspend request.
+	 * The first call activates the clock; ignore any others.
 	 */
 	if (!atomic_xchg(&ipa->suspend_ref, 1))
 		ipa_clock_get(ipa);
@@ -502,13 +503,15 @@  static void ipa_resource_deconfig(struct ipa *ipa)
  */
 static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
 {
+	struct device *dev = &ipa->pdev->dev;
 	int ret;
 
 	/* Get a clock reference to allow initialization.  This reference
 	 * is held after initialization completes, and won't get dropped
 	 * unless/until a system suspend request arrives.
 	 */
-	atomic_set(&ipa->suspend_ref, 1);
+	if (atomic_xchg(&ipa->suspend_ref, 1))
+		dev_err(dev, "suspend clock reference already taken!\n");
 	ipa_clock_get(ipa);
 
 	ipa_hardware_config(ipa);
@@ -544,7 +547,8 @@  static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
 err_hardware_deconfig:
 	ipa_hardware_deconfig(ipa);
 	ipa_clock_put(ipa);
-	atomic_set(&ipa->suspend_ref, 0);
+	if (!atomic_xchg(&ipa->suspend_ref, 0))
+		dev_err(dev, "suspend clock reference already dropped!\n");
 
 	return ret;
 }
@@ -562,7 +566,8 @@  static void ipa_deconfig(struct ipa *ipa)
 	ipa_endpoint_deconfig(ipa);
 	ipa_hardware_deconfig(ipa);
 	ipa_clock_put(ipa);
-	atomic_set(&ipa->suspend_ref, 0);
+	if (!atomic_xchg(&ipa->suspend_ref, 0))
+		dev_err(&ipa->pdev->dev, "no suspend clock reference\n");
 }
 
 static int ipa_firmware_load(struct device *dev)
@@ -913,7 +918,8 @@  static int ipa_suspend(struct device *dev)
 	struct ipa *ipa = dev_get_drvdata(dev);
 
 	ipa_clock_put(ipa);
-	atomic_set(&ipa->suspend_ref, 0);
+	if (!atomic_xchg(&ipa->suspend_ref, 0))
+		dev_err(dev, "suspend: missing suspend clock reference\n");
 
 	return 0;
 }
@@ -933,7 +939,8 @@  static int ipa_resume(struct device *dev)
 	/* This clock reference will keep the IPA out of suspend
 	 * until we get a power management suspend request.
 	 */
-	atomic_set(&ipa->suspend_ref, 1);
+	if (atomic_xchg(&ipa->suspend_ref, 1))
+		dev_err(dev, "resume: duplicate suspend clock reference\n");
 	ipa_clock_get(ipa);
 
 	return 0;