linux-arm-msm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] regulator: core: fix a possible race in disable_work handling
@ 2017-07-12  9:29 Tirupathi Reddy
  2017-07-12 10:02 ` Mark Brown
  2017-07-20 12:24 ` Applied "regulator: core: fix a possible race in disable_work handling" to the regulator tree Mark Brown
  0 siblings, 2 replies; 4+ messages in thread
From: Tirupathi Reddy @ 2017-07-12  9:29 UTC (permalink / raw)
  To: broonie, lgirdwood; +Cc: linux-arm-msm, linux-kernel, Tirupathi Reddy

A race condition between queueing and processing the disable_work
instances results in having a work instance in the queue and the
deferred_disables variable of regulator device structure having a
value '0'. If no new regulator_disable_deferred() call later from
clients, the deferred_disables variable value remains '0' and hits
BUG() in regulator_disable_work() when the queued instance scheduled
for processing the work.

The race occurs as below:

	Core-0					     Core-1
	.....	       /* deferred_disables = 2 */   .....
	.....	       /* disable_work is queued */  .....
	.....					     .....
regulator_disable_deferred: 		regulator_disable_work:
   mutex_lock(&rdev->mutex);			     .....
   rdev->deferred_disables++;		    mutex_lock(&rdev->mutex);
   mutex_unlock(&rdev->mutex);			     .....
   queue_delayed_work(...)		    count =rdev->deferred_disables;
	.....				    rdev->deferred_disables = 0;
	.....					     .....
	.....				    mutex_unlock(&rdev->mutex);
	.....					     .....
	.....				    return;
	.....					     .....
	/* No new regulator_disable_deferred() calls from clients */
	/* The newly queued instance is scheduled for processing */
	.....					     .....
regulator_disable_work:
	.....
   mutex_lock(&rdev->mutex);
   BUG_ON(!rdev->deferred_disables); /* deferred_disables = 0 */

The race is fixed by removing the work instance that is queued while
processing the previous queued instance. Cancel the newly queued instance
from disable_work() handler just after reset the deferred_disables variable
to value '0'. Also move the work queueing step before mutex_unlock in
regulator_disable_deferred().

Also use mod_delayed_work() in the pace of queue_delayed_work() as
queue_delayed_work() always uses the delay requested in the first call
when multiple consumers call regulator_disable_deferred() close in time
and does not guarantee the semantics of regulator_disable_deferred().

Signed-off-by: Tirupathi Reddy <tirupath@codeaurora.org>
---
 drivers/regulator/core.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index e567fa5..9f4d484 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -2396,6 +2396,14 @@ static void regulator_disable_work(struct work_struct *work)
 	count = rdev->deferred_disables;
 	rdev->deferred_disables = 0;
 
+	/*
+	 * Workqueue functions queue the new work instance while the previous
+	 * work instance is being processed. Cancel the queued work instance
+	 * as the work instance under processing does the job of the queued
+	 * work instance.
+	 */
+	cancel_delayed_work(&rdev->disable_work);
+
 	for (i = 0; i < count; i++) {
 		ret = _regulator_disable(rdev);
 		if (ret != 0)
@@ -2439,10 +2447,10 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
 
 	mutex_lock(&rdev->mutex);
 	rdev->deferred_disables++;
+	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
+			 msecs_to_jiffies(ms));
 	mutex_unlock(&rdev->mutex);
 
-	queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
-			   msecs_to_jiffies(ms));
 	return 0;
 }
 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
-- 
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] regulator: core: fix a possible race in disable_work handling
  2017-07-12  9:29 [PATCH 1/1] regulator: core: fix a possible race in disable_work handling Tirupathi Reddy
@ 2017-07-12 10:02 ` Mark Brown
  2017-07-12 11:46   ` Tirupathi Reddy T
  2017-07-20 12:24 ` Applied "regulator: core: fix a possible race in disable_work handling" to the regulator tree Mark Brown
  1 sibling, 1 reply; 4+ messages in thread
From: Mark Brown @ 2017-07-12 10:02 UTC (permalink / raw)
  To: Tirupathi Reddy; +Cc: lgirdwood, linux-arm-msm, linux-kernel

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

On Wed, Jul 12, 2017 at 02:59:47PM +0530, Tirupathi Reddy wrote:

> regulator_disable_deferred: 		regulator_disable_work:
>    mutex_lock(&rdev->mutex);			     .....
>    rdev->deferred_disables++;		    mutex_lock(&rdev->mutex);
>    mutex_unlock(&rdev->mutex);			     .....
>    queue_delayed_work(...)		    count =rdev->deferred_disables;
> 	.....				    rdev->deferred_disables = 0;

This shows two things simultaneously holding rdev->mutex...

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] regulator: core: fix a possible race in disable_work handling
  2017-07-12 10:02 ` Mark Brown
@ 2017-07-12 11:46   ` Tirupathi Reddy T
  0 siblings, 0 replies; 4+ messages in thread
From: Tirupathi Reddy T @ 2017-07-12 11:46 UTC (permalink / raw)
  To: Mark Brown; +Cc: lgirdwood, linux-arm-msm, linux-kernel



On 7/12/2017 3:32 PM, Mark Brown wrote:
> On Wed, Jul 12, 2017 at 02:59:47PM +0530, Tirupathi Reddy wrote:
>
>> regulator_disable_deferred: 		regulator_disable_work:
>>     mutex_lock(&rdev->mutex);			     .....
>>     rdev->deferred_disables++;		    mutex_lock(&rdev->mutex);
>>     mutex_unlock(&rdev->mutex);			     .....
>>     queue_delayed_work(...)		    count =rdev->deferred_disables;
>> 	.....				    rdev->deferred_disables = 0;
> This shows two things simultaneously holding rdev->mutex...
Fixed this in the new patch version(V2).
https://patchwork.kernel.org/patch/9836529/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Applied "regulator: core: fix a possible race in disable_work handling" to the regulator tree
  2017-07-12  9:29 [PATCH 1/1] regulator: core: fix a possible race in disable_work handling Tirupathi Reddy
  2017-07-12 10:02 ` Mark Brown
@ 2017-07-20 12:24 ` Mark Brown
  1 sibling, 0 replies; 4+ messages in thread
From: Mark Brown @ 2017-07-20 12:24 UTC (permalink / raw)
  To: Tirupathi Reddy; +Cc: Mark Brown

The patch

   regulator: core: fix a possible race in disable_work handling

has been applied to the regulator tree at

   git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

>From c9ccaa0cac3fc8e7d17a668aabfdf632c7c0517a Mon Sep 17 00:00:00 2001
From: Tirupathi Reddy <tirupath@codeaurora.org>
Date: Wed, 12 Jul 2017 17:08:13 +0530
Subject: [PATCH] regulator: core: fix a possible race in disable_work handling

A race condition between queueing and processing the disable_work
instances results in having a work instance in the queue and the
deferred_disables variable of regulator device structure having a
value '0'. If no new regulator_disable_deferred() call later from
clients, the deferred_disables variable value remains '0' and hits
BUG() in regulator_disable_work() when the queued instance scheduled
for processing the work.

The race occurs as below:

	Core-0					     Core-1
	.....	       /* deferred_disables = 2 */   .....
	.....	       /* disable_work is queued */  .....
	.....					     .....
regulator_disable_deferred: 		regulator_disable_work:
   mutex_lock(&rdev->mutex);			     .....
   rdev->deferred_disables++;		             .....
   mutex_unlock(&rdev->mutex);			     .....
   queue_delayed_work(...)		    mutex_lock(&rdev->mutex);
	.....				    count =rdev->deferred_disables;
	.....				    rdev->deferred_disables = 0;
	.....					     .....
	.....				    mutex_unlock(&rdev->mutex);
	.....					     .....
	.....				    return;
	.....					     .....
	/* No new regulator_disable_deferred() calls from clients */
	/* The newly queued instance is scheduled for processing */
	.....					     .....
regulator_disable_work:
	.....
   mutex_lock(&rdev->mutex);
   BUG_ON(!rdev->deferred_disables); /* deferred_disables = 0 */

The race is fixed by removing the work instance that is queued while
processing the previous queued instance. Cancel the newly queued instance
from disable_work() handler just after reset the deferred_disables variable
to value '0'. Also move the work queueing step before mutex_unlock in
regulator_disable_deferred().

Also use mod_delayed_work() in the pace of queue_delayed_work() as
queue_delayed_work() always uses the delay requested in the first call
when multiple consumers call regulator_disable_deferred() close in time
and does not guarantee the semantics of regulator_disable_deferred().

Signed-off-by: Tirupathi Reddy <tirupath@codeaurora.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/regulator/core.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index e567fa54980b..9f4d484eb25d 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -2396,6 +2396,14 @@ static void regulator_disable_work(struct work_struct *work)
 	count = rdev->deferred_disables;
 	rdev->deferred_disables = 0;
 
+	/*
+	 * Workqueue functions queue the new work instance while the previous
+	 * work instance is being processed. Cancel the queued work instance
+	 * as the work instance under processing does the job of the queued
+	 * work instance.
+	 */
+	cancel_delayed_work(&rdev->disable_work);
+
 	for (i = 0; i < count; i++) {
 		ret = _regulator_disable(rdev);
 		if (ret != 0)
@@ -2439,10 +2447,10 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
 
 	mutex_lock(&rdev->mutex);
 	rdev->deferred_disables++;
+	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
+			 msecs_to_jiffies(ms));
 	mutex_unlock(&rdev->mutex);
 
-	queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
-			   msecs_to_jiffies(ms));
 	return 0;
 }
 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
-- 
2.13.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-07-20 12:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-12  9:29 [PATCH 1/1] regulator: core: fix a possible race in disable_work handling Tirupathi Reddy
2017-07-12 10:02 ` Mark Brown
2017-07-12 11:46   ` Tirupathi Reddy T
2017-07-20 12:24 ` Applied "regulator: core: fix a possible race in disable_work handling" to the regulator tree Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).