Skip to contentSkip to conversation

cpu_relax in busy loops question

DISCUSSION Daniel Lezcano 2 participants 3 messages Feb 1, 2012 – Feb 2, 2012

lore
Share & tools
lore
#0 lore raw

Hi Linus,

While looking at the drivers/mfd/db8500-prcmu.c file I noticed:

  595         /* Grab the HW semaphore. */
  596         while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
  597                 cpu_relax();

I was wondering why is cpu_relax needed here as readl does a memory barrier ? I thought the cpu_relax function was related to the x86 to consume less power with an optimization of the "rep nop" loops or/and do a memory barrier.

Should a busy-loop be always with cpu_relax ?

Thanks
-- Daniel

Signature · 5 lines
  <http://www.linaro.org/> Linaro.org ? Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#1 lore raw
Quoted 11 lines · Daniel Lezcano

Hi Linus,

While looking at the drivers/mfd/db8500-prcmu.c file I noticed:

?595 ? ? ? ? /* Grab the HW semaphore. */
?596 ? ? ? ? while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0) ?597 ? ? ? ? ? ? ? ? cpu_relax();

Quoted 6 lines

I was wondering why is cpu_relax needed here as readl does a memory barrier ? I thought the cpu_relax function was related to the x86 to consume less power with an optimization of the "rep nop" loops or/and do a memory barrier.

Should a busy-loop be always with cpu_relax ?

Last time we discussed it ar LAKML I think we came to the conclusion that while cpu_relax() has very little semantic meaning on ARM, it is still nice to have from a readability and maintenance point of view, since it is pretty clear what we're trying to do.

But I will stand corrected...

Yours,
Linus Walleij

#2 lore raw
Quoted 18 lines · Linus Walleij
Quoted 16 lines · Daniel Lezcano

Hi Linus,

While looking at the drivers/mfd/db8500-prcmu.c file I noticed:

  595         /* Grab the HW semaphore. */
  596         while ((readl(PRCM_SEM)&  PRCM_SEM_PRCM_SEM) != 0)
  597                 cpu_relax();

I was wondering why is cpu_relax needed here as readl does a memory barrier ? I thought the cpu_relax function was related to the x86 to consume less power with an optimization of the "rep nop" loops or/and do a memory barrier.

Quoted 6 lines
Quoted 1 line

Should a busy-loop be always with cpu_relax ?

Last time we discussed it ar LAKML I think we came to the conclusion that while cpu_relax() has very little semantic meaning on ARM, it is still nice to have from a readability and maintenance point of view, since it is pretty clear what we're trying to do.

Ok, thanks Linus.

-- Daniel

Signature · 5 lines
  <http://www.linaro.org/> Linaro.org ? Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog