diff mbox series

[v3] ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time

Message ID 20190226023413.19531-1-okuno.kohji@jp.panasonic.com
State New
Headers show
Series [v3] ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time | expand

Commit Message

Kohji Okuno Feb. 26, 2019, 2:34 a.m. UTC
In the current cpuidle implementation for i.MX6q, the CPU that sets
'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
"WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
istead of "WAIT", this CPU can not wake up at expired time.
 Because, in the case of "WFI", the CPU must be waked up by the local
timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
is stopped, when all CPUs execute "wfi" instruction. As a result, the
local timer interrupt is not fired.
 In this situation, this CPU will wake up by IRQ different from local
timer. (e.g. broacast tiemr)

So, this fix changes CPU to return to 'WAIT_CLOCKED'.

Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
---

Changes for v3:
 - remove the braces for a single statement inside the if block.

Changes for v2:
 - change "master" variable name.

 arch/arm/mach-imx/cpuidle-imx6q.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

Comments

Shawn Guo March 1, 2019, 9:23 a.m. UTC | #1
Hi Kohji,

On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
>  In the current cpuidle implementation for i.MX6q, the CPU that sets
> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
> the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
> "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
> istead of "WAIT", this CPU can not wake up at expired time.
>  Because, in the case of "WFI", the CPU must be waked up by the local
> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
> is stopped, when all CPUs execute "wfi" instruction. As a result, the
> local timer interrupt is not fired.
>  In this situation, this CPU will wake up by IRQ different from local
> timer. (e.g. broacast tiemr)
> 
> So, this fix changes CPU to return to 'WAIT_CLOCKED'.

Just out of curiosity, how did you observe the bug?  I'm trying to
understand the impact of the bug.

Shawn

> 
> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
> ---
> 
> Changes for v3:
>  - remove the braces for a single statement inside the if block.
> 
> Changes for v2:
>  - change "master" variable name.
> 
>  arch/arm/mach-imx/cpuidle-imx6q.c | 27 ++++++++++-----------------
>  1 file changed, 10 insertions(+), 17 deletions(-)
> 
> diff --git a/arch/arm/mach-imx/cpuidle-imx6q.c b/arch/arm/mach-imx/cpuidle-imx6q.c
> index bfeb25aaf9a2..326e870d7123 100644
> --- a/arch/arm/mach-imx/cpuidle-imx6q.c
> +++ b/arch/arm/mach-imx/cpuidle-imx6q.c
> @@ -16,30 +16,23 @@
>  #include "cpuidle.h"
>  #include "hardware.h"
>  
> -static atomic_t master = ATOMIC_INIT(0);
> -static DEFINE_SPINLOCK(master_lock);
> +static int num_idle_cpus = 0;
> +static DEFINE_SPINLOCK(cpuidle_lock);
>  
>  static int imx6q_enter_wait(struct cpuidle_device *dev,
>  			    struct cpuidle_driver *drv, int index)
>  {
> -	if (atomic_inc_return(&master) == num_online_cpus()) {
> -		/*
> -		 * With this lock, we prevent other cpu to exit and enter
> -		 * this function again and become the master.
> -		 */
> -		if (!spin_trylock(&master_lock))
> -			goto idle;
> +	spin_lock(&cpuidle_lock);
> +	if (++num_idle_cpus == num_online_cpus())
>  		imx6_set_lpm(WAIT_UNCLOCKED);
> -		cpu_do_idle();
> -		imx6_set_lpm(WAIT_CLOCKED);
> -		spin_unlock(&master_lock);
> -		goto done;
> -	}
> +	spin_unlock(&cpuidle_lock);
>  
> -idle:
>  	cpu_do_idle();
> -done:
> -	atomic_dec(&master);
> +
> +	spin_lock(&cpuidle_lock);
> +	if (num_idle_cpus-- == num_online_cpus())
> +		imx6_set_lpm(WAIT_CLOCKED);
> +	spin_unlock(&cpuidle_lock);
>  
>  	return index;
>  }
> -- 
> 2.17.1
>
Kohji Okuno March 4, 2019, 1:28 a.m. UTC | #2
Hi Shawn,

I found this bug by delaying periodic communication. For example, it
is the source codes as below.

	while (1) {
		sleep(2);
		write(fd, "Hello", strlen("Hello"));
	}

Of course, it dows not occur frequently. However, in the worst case,
it stopped for more than 1 minute.

If there are few running processes and few hrtimer events, this issue
will occur.

For example:
(*) time is abstracting.
    WFI idle : the CPU will be waked up by local timer.
    WAIT idle: the CPU will be waked up by broad cast(bc) timer.
    
[time]
	  CPU#0				  CPU#1
----------------------------------------------------------------------
	enter "WAIT" idle
	[next event=10]
					enter "WAIT" idle
					[next event=60]	
			all cpus are IDLE = > CPU#1 sets WAIT_UNCLOCKED

10:	bc timer fires
	exit idle
	
	enter "WFI" idle
	[next event=12]
	
12:     ** local timer is not fired **
	CPU#0 will not be waked up
	until the next bc timer interrupt.

			>> all processes stop <<

60:	bc timer fires
	exit idle			exit idle
					CPU#1 sets WAIT_CLOCKED
	local timer fires

If CPU#0 sets WAIT_CLOCKED at time=10, this issue will not occur.

Does that make any sense?

Best regards,
 Kohji Okuno


Shawn Guo <shawnguo@kernel.org> wrote:
> Hi Kohji,
> 
> On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
>>  In the current cpuidle implementation for i.MX6q, the CPU that sets
>> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
>> the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
>> "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
>> istead of "WAIT", this CPU can not wake up at expired time.
>>  Because, in the case of "WFI", the CPU must be waked up by the local
>> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
>> is stopped, when all CPUs execute "wfi" instruction. As a result, the
>> local timer interrupt is not fired.
>>  In this situation, this CPU will wake up by IRQ different from local
>> timer. (e.g. broacast tiemr)
>> 
>> So, this fix changes CPU to return to 'WAIT_CLOCKED'.
> 
> Just out of curiosity, how did you observe the bug?  I'm trying to
> understand the impact of the bug.
> 
> Shawn
> 
>> 
>> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
>> ---
>> 
>> Changes for v3:
>>  - remove the braces for a single statement inside the if block.
>> 
>> Changes for v2:
>>  - change "master" variable name.
>> 
>>  arch/arm/mach-imx/cpuidle-imx6q.c | 27 ++++++++++-----------------
>>  1 file changed, 10 insertions(+), 17 deletions(-)
>> 
>> diff --git a/arch/arm/mach-imx/cpuidle-imx6q.c b/arch/arm/mach-imx/cpuidle-imx6q.c
>> index bfeb25aaf9a2..326e870d7123 100644
>> --- a/arch/arm/mach-imx/cpuidle-imx6q.c
>> +++ b/arch/arm/mach-imx/cpuidle-imx6q.c
>> @@ -16,30 +16,23 @@
>>  #include "cpuidle.h"
>>  #include "hardware.h"
>>  
>> -static atomic_t master = ATOMIC_INIT(0);
>> -static DEFINE_SPINLOCK(master_lock);
>> +static int num_idle_cpus = 0;
>> +static DEFINE_SPINLOCK(cpuidle_lock);
>>  
>>  static int imx6q_enter_wait(struct cpuidle_device *dev,
>>  			    struct cpuidle_driver *drv, int index)
>>  {
>> -	if (atomic_inc_return(&master) == num_online_cpus()) {
>> -		/*
>> -		 * With this lock, we prevent other cpu to exit and enter
>> -		 * this function again and become the master.
>> -		 */
>> -		if (!spin_trylock(&master_lock))
>> -			goto idle;
>> +	spin_lock(&cpuidle_lock);
>> +	if (++num_idle_cpus == num_online_cpus())
>>  		imx6_set_lpm(WAIT_UNCLOCKED);
>> -		cpu_do_idle();
>> -		imx6_set_lpm(WAIT_CLOCKED);
>> -		spin_unlock(&master_lock);
>> -		goto done;
>> -	}
>> +	spin_unlock(&cpuidle_lock);
>>  
>> -idle:
>>  	cpu_do_idle();
>> -done:
>> -	atomic_dec(&master);
>> +
>> +	spin_lock(&cpuidle_lock);
>> +	if (num_idle_cpus-- == num_online_cpus())
>> +		imx6_set_lpm(WAIT_CLOCKED);
>> +	spin_unlock(&cpuidle_lock);
>>  
>>  	return index;
>>  }
>> -- 
>> 2.17.1
>>
Shawn Guo March 4, 2019, 7 a.m. UTC | #3
On Mon, Mar 04, 2019 at 10:28:14AM +0900, Kohji Okuno wrote:
> Hi Shawn,
> 
> I found this bug by delaying periodic communication. For example, it
> is the source codes as below.
> 
> 	while (1) {
> 		sleep(2);
> 		write(fd, "Hello", strlen("Hello"));
> 	}
> 
> Of course, it dows not occur frequently. However, in the worst case,
> it stopped for more than 1 minute.
> 
> If there are few running processes and few hrtimer events, this issue
> will occur.
> 
> For example:
> (*) time is abstracting.
>     WFI idle : the CPU will be waked up by local timer.
>     WAIT idle: the CPU will be waked up by broad cast(bc) timer.
>     
> [time]
> 	  CPU#0				  CPU#1
> ----------------------------------------------------------------------
> 	enter "WAIT" idle
> 	[next event=10]
> 					enter "WAIT" idle
> 					[next event=60]	
> 			all cpus are IDLE = > CPU#1 sets WAIT_UNCLOCKED
> 
> 10:	bc timer fires
> 	exit idle
> 	
> 	enter "WFI" idle
> 	[next event=12]
> 	
> 12:     ** local timer is not fired **
> 	CPU#0 will not be waked up
> 	until the next bc timer interrupt.
> 
> 			>> all processes stop <<
> 
> 60:	bc timer fires
> 	exit idle			exit idle
> 					CPU#1 sets WAIT_CLOCKED
> 	local timer fires
> 
> If CPU#0 sets WAIT_CLOCKED at time=10, this issue will not occur.
> 
> Does that make any sense?

Yes, makes sense, and thanks for the details.  So the fix should be
a material for stable kernel, even though the bug doesn't happen often.
Thanks for the patch.

Shawn
Shawn Guo March 4, 2019, 7:06 a.m. UTC | #4
On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
>  In the current cpuidle implementation for i.MX6q, the CPU that sets
> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
> the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
> "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
> istead of "WAIT", this CPU can not wake up at expired time.
>  Because, in the case of "WFI", the CPU must be waked up by the local
> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
> is stopped, when all CPUs execute "wfi" instruction. As a result, the
> local timer interrupt is not fired.
>  In this situation, this CPU will wake up by IRQ different from local
> timer. (e.g. broacast tiemr)

s/tiemr/timer

> 
> So, this fix changes CPU to return to 'WAIT_CLOCKED'.
> 
> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>

Applied the patch with following Fixes tag.

Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")

Thanks for the fixing.

Shawn
Kohji Okuno March 4, 2019, 7:38 a.m. UTC | #5
Hi Shawn,

Thank you for your work. I confirmed about your commit.

Best regards,
 Kohji Okuno

Shawn Guo <shawnguo@kernel.org> wrote:
> Applied the patch with following Fixes tag.
> 
> Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")
> 
> Thanks for the fixing.
> 
> Shawn
Kohji Okuno March 5, 2019, 10:38 a.m. UTC | #6
Hi Shawn,

I found the fix of the same issue on linux-imx repository. Could you
refer to the following?

https://source.codeaurora.org/external/imx/linux-imx/commit/?h=imx_4.1.y_brillo&id=f739483488ef76979c06efea397237756bec45b9

Regarding the fix of cpuidle-imx6q.c, my fix is better, I think.
But, we may also need to modify pm-imx6.c.

# I cannot understand why they doesn't release their modifications.

Best regards,
 Kohji Okuno

Shawn Guo <shawnguo@kernel.org> wrote:
> On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
>>  In the current cpuidle implementation for i.MX6q, the CPU that sets
>> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
>> the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
>> "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
>> istead of "WAIT", this CPU can not wake up at expired time.
>>  Because, in the case of "WFI", the CPU must be waked up by the local
>> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
>> is stopped, when all CPUs execute "wfi" instruction. As a result, the
>> local timer interrupt is not fired.
>>  In this situation, this CPU will wake up by IRQ different from local
>> timer. (e.g. broacast tiemr)
> 
> s/tiemr/timer
> 
>> 
>> So, this fix changes CPU to return to 'WAIT_CLOCKED'.
>> 
>> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
> 
> Applied the patch with following Fixes tag.
> 
> Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")
> 
> Thanks for the fixing.
> 
> Shawn
Shawn Guo March 6, 2019, 3:21 a.m. UTC | #7
On Tue, Mar 05, 2019 at 07:38:05PM +0900, Kohji Okuno wrote:
> Hi Shawn,
> 
> I found the fix of the same issue on linux-imx repository. Could you
> refer to the following?

Thanks for the pointer.

> 
> https://source.codeaurora.org/external/imx/linux-imx/commit/?h=imx_4.1.y_brillo&id=f739483488ef76979c06efea397237756bec45b9
> 
> Regarding the fix of cpuidle-imx6q.c, my fix is better, I think.

I agree.

> But, we may also need to modify pm-imx6.c.

You can send another patch if you think the change is really necessary.

> 
> # I cannot understand why they doesn't release their modifications.

My wild guess is that they shifted the focus to new platforms like
i.MX8.

Shawn
Peng Fan March 6, 2019, 2:36 p.m. UTC | #8
> -----Original Message-----
> From: Kohji Okuno [mailto:okuno.kohji@jp.panasonic.com]
> Sent: 2019年3月5日 18:38
> To: shawnguo@kernel.org
> Cc: linux-arm-kernel@lists.infradead.org; s.hauer@pengutronix.de;
> kernel@pengutronix.de; Fabio Estevam <fabio.estevam@nxp.com>;
> dl-linux-imx <linux-imx@nxp.com>; okuno.kohji@jp.panasonic.com
> Subject: Re: [PATCH v3] ARM: imx6q: cpuidle: fix bug that CPU might not wake
> up at expected time
> 
> Hi Shawn,
> 
> I found the fix of the same issue on linux-imx repository. Could you refer to
> the following?
> 
> https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsour
> ce.codeaurora.org%2Fexternal%2Fimx%2Flinux-imx%2Fcommit%2F%3Fh%3D
> imx_4.1.y_brillo%26id%3Df739483488ef76979c06efea397237756bec45b9&a
> mp;data=02%7C01%7Cpeng.fan%40nxp.com%7Cf02b29e33b2c4245a60d08d
> 6a156ad72%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636873
> 790983230223&amp;sdata=QM21UkcAUjjZq3kPdZEgrkCxvFTneGRH7%2BlQg
> GJNHoU%3D&amp;reserved=0
> 
> Regarding the fix of cpuidle-imx6q.c, my fix is better, I think.
> But, we may also need to modify pm-imx6.c.
> 
> # I cannot understand why they doesn't release their modifications.

This patch has been tried to upstream long time ago, https://lkml.org/lkml/2017/12/30/65

Regards,
Peng

> 
> Best regards,
>  Kohji Okuno
> 
> Shawn Guo <shawnguo@kernel.org> wrote:
> > On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
> >>  In the current cpuidle implementation for i.MX6q, the CPU that sets
> >> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are
> >> always the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE
> >> state of "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
> >> istead of "WAIT", this CPU can not wake up at expired time.
> >>  Because, in the case of "WFI", the CPU must be waked up by the local
> >> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
> >> is stopped, when all CPUs execute "wfi" instruction. As a result, the
> >> local timer interrupt is not fired.
> >>  In this situation, this CPU will wake up by IRQ different from local
> >> timer. (e.g. broacast tiemr)
> >
> > s/tiemr/timer
> >
> >>
> >> So, this fix changes CPU to return to 'WAIT_CLOCKED'.
> >>
> >> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
> >
> > Applied the patch with following Fixes tag.
> >
> > Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")
> >
> > Thanks for the fixing.
> >
> > Shawn
Kohji Okuno March 7, 2019, 12:06 a.m. UTC | #9
Hi Peng and Shawn,

Peng, thank you for sharing information and I'm very sorry about my
comment.

Shawn and Peng,
Could you confirm about the addition patch for pm-imx6.c?

Best regards,
 Kohji Okuno

Peng Fan <peng.fan@nxp.com> wrote:
> 
> 
>> -----Original Message-----
>> From: Kohji Okuno [mailto:okuno.kohji@jp.panasonic.com]
>> Sent: 2019年3月5日 18:38
>> To: shawnguo@kernel.org
>> Cc: linux-arm-kernel@lists.infradead.org; s.hauer@pengutronix.de;
>> kernel@pengutronix.de; Fabio Estevam <fabio.estevam@nxp.com>;
>> dl-linux-imx <linux-imx@nxp.com>; okuno.kohji@jp.panasonic.com
>> Subject: Re: [PATCH v3] ARM: imx6q: cpuidle: fix bug that CPU might not wake
>> up at expected time
>> 
>> Hi Shawn,
>> 
>> I found the fix of the same issue on linux-imx repository. Could you refer to
>> the following?
>> 
>> https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsour
>> ce.codeaurora.org%2Fexternal%2Fimx%2Flinux-imx%2Fcommit%2F%3Fh%3D
>> imx_4.1.y_brillo%26id%3Df739483488ef76979c06efea397237756bec45b9&a
>> mp;data=02%7C01%7Cpeng.fan%40nxp.com%7Cf02b29e33b2c4245a60d08d
>> 6a156ad72%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636873
>> 790983230223&amp;sdata=QM21UkcAUjjZq3kPdZEgrkCxvFTneGRH7%2BlQg
>> GJNHoU%3D&amp;reserved=0
>> 
>> Regarding the fix of cpuidle-imx6q.c, my fix is better, I think.
>> But, we may also need to modify pm-imx6.c.
>> 
>> # I cannot understand why they doesn't release their modifications.
> 
> This patch has been tried to upstream long time ago, https://lkml.org/lkml/2017/12/30/65
> 
> Regards,
> Peng
> 
>> 
>> Best regards,
>>  Kohji Okuno
>> 
>> Shawn Guo <shawnguo@kernel.org> wrote:
>> > On Tue, Feb 26, 2019 at 11:34:13AM +0900, Kohji Okuno wrote:
>> >>  In the current cpuidle implementation for i.MX6q, the CPU that sets
>> >> 'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are
>> >> always the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE
>> >> state of "WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
>> >> istead of "WAIT", this CPU can not wake up at expired time.
>> >>  Because, in the case of "WFI", the CPU must be waked up by the local
>> >> timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
>> >> is stopped, when all CPUs execute "wfi" instruction. As a result, the
>> >> local timer interrupt is not fired.
>> >>  In this situation, this CPU will wake up by IRQ different from local
>> >> timer. (e.g. broacast tiemr)
>> >
>> > s/tiemr/timer
>> >
>> >>
>> >> So, this fix changes CPU to return to 'WAIT_CLOCKED'.
>> >>
>> >> Signed-off-by: Kohji Okuno <okuno.kohji@jp.panasonic.com>
>> >
>> > Applied the patch with following Fixes tag.
>> >
>> > Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")
>> >
>> > Thanks for the fixing.
>> >
>> > Shawn
diff mbox series

Patch

diff --git a/arch/arm/mach-imx/cpuidle-imx6q.c b/arch/arm/mach-imx/cpuidle-imx6q.c
index bfeb25aaf9a2..326e870d7123 100644
--- a/arch/arm/mach-imx/cpuidle-imx6q.c
+++ b/arch/arm/mach-imx/cpuidle-imx6q.c
@@ -16,30 +16,23 @@ 
 #include "cpuidle.h"
 #include "hardware.h"
 
-static atomic_t master = ATOMIC_INIT(0);
-static DEFINE_SPINLOCK(master_lock);
+static int num_idle_cpus = 0;
+static DEFINE_SPINLOCK(cpuidle_lock);
 
 static int imx6q_enter_wait(struct cpuidle_device *dev,
 			    struct cpuidle_driver *drv, int index)
 {
-	if (atomic_inc_return(&master) == num_online_cpus()) {
-		/*
-		 * With this lock, we prevent other cpu to exit and enter
-		 * this function again and become the master.
-		 */
-		if (!spin_trylock(&master_lock))
-			goto idle;
+	spin_lock(&cpuidle_lock);
+	if (++num_idle_cpus == num_online_cpus())
 		imx6_set_lpm(WAIT_UNCLOCKED);
-		cpu_do_idle();
-		imx6_set_lpm(WAIT_CLOCKED);
-		spin_unlock(&master_lock);
-		goto done;
-	}
+	spin_unlock(&cpuidle_lock);
 
-idle:
 	cpu_do_idle();
-done:
-	atomic_dec(&master);
+
+	spin_lock(&cpuidle_lock);
+	if (num_idle_cpus-- == num_online_cpus())
+		imx6_set_lpm(WAIT_CLOCKED);
+	spin_unlock(&cpuidle_lock);
 
 	return index;
 }