Welcome to Soft32 Linux Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

why kernel implement "udelay" by cpu instructions?

 
   Soft32 Home -> Linux -> Kernel RSS
Next:  Accepted emacs23 23.1+1-5 (source all i386)  
Author Message
loody

External


Since: Nov 02, 2009
Posts: 2



(Msg. 1) Posted: Sun Nov 01, 2009 9:20 pm
Post subject: why kernel implement "udelay" by cpu instructions?
Archived from groups: linux>kernel (more info?)

Dear all:
I find the kernel use cpu instruction to implement the udelay function
as keeping decrease a big counter by 1.

If I search the right place in kernel, why kernel does so?
the precision will be different if cpu runs faster or slower, right?
appreciate your help,
miloody
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo.RemoveThis@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Rik van Riel

External


Since: Sep 12, 2004
Posts: 205



(Msg. 2) Posted: Sun Nov 01, 2009 11:20 pm
Post subject: Re: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 11/01/2009 10:13 PM, loody wrote:
> Dear all:
> I find the kernel use cpu instruction to implement the udelay function
> as keeping decrease a big counter by 1.
>
> If I search the right place in kernel, why kernel does so?

Because udelay is used in places where the kernel cannot
use other mechanisms, eg. because interrupts are blocked
or the current process cannot be scheduled out.

> the precision will be different if cpu runs faster or slower, right?

At bootup the kernel measures the delay loop speed of
each CPU. CPU frequency scaling might make the loop
run slower at times, but that is okay because udelay
simply specifies a *minimum* delay.

This is true even on systems without frequency scaling,
because the udelay loop could be interrupted by an
interrupt, an NMI or by having the CPU trap into SMM
mode and execute code there.

--
All rights reversed.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo DeleteThis @vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
loody

External


Since: Nov 02, 2009
Posts: 2



(Msg. 3) Posted: Tue Nov 03, 2009 9:20 pm
Post subject: Re: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi

2009/11/2 Rik van Riel <riel.TakeThisOut@redhat.com>:
> On 11/01/2009 10:13 PM, loody wrote:
>>
>> Dear all:
>> I find the kernel use cpu instruction to implement the udelay function
>> as keeping decrease a big counter by 1.
>>
>> If I search the right place in kernel, why kernel does so?
>
> Because udelay is used in places where the kernel cannot
> use other mechanisms, eg. because interrupts are blocked
> or the current process cannot be scheduled out.

Or the only way to support udelay is using CPU instruction to do the counting?
I find something interesting; kernel has msleep, but it doesn't have usleep.
Does that mean the minimum time kernel can react is msecond instead of usecond?
so if users want to count useconds, they have to do the busy waiting,
execute some looping assembly instructions?

If my consumption is correct, where I can find the evidence?
BTW, does Hz has anything related to kernel timing?
From the comment in the kernel, it says
Hz: clock ticks generated per second
Does that mean the kernel will get #Hz timer interrupts per second?
Whz the value of Hz is 100?
if the minimum reaction time of kernel is msecond, the value of Hz
should be 1000, right?


>
>> the precision will be different if cpu runs faster or slower, right?
>
> At bootup the kernel measures the delay loop speed of
> each CPU.  CPU frequency scaling might make the loop

would you please let me know where the source code is?
(measuring loop speed of cpu and scale cpu frequency)

> run slower at times, but that is okay because udelay
> simply specifies a *minimum* delay.
>
> This is true even on systems without frequency scaling,
> because the udelay loop could be interrupted by an
> interrupt, an NMI or by having the CPU trap into SMM
> mode and execute code there.
appreciate your kind help Smile
miloody
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo.TakeThisOut@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Rajat Jain

External


Since: Nov 04, 2009
Posts: 2



(Msg. 4) Posted: Tue Nov 03, 2009 11:20 pm
Post subject: RE: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi,

> I find something interesting; kernel has msleep, but it
> doesn't have usleep.
> Does that mean the minimum time kernel can react is msecond
> instead of usecond?
> so if users want to count useconds, they have to do the busy waiting,
> execute some looping assembly instructions?

You are roughly right. If you don't want to busy loop (udelay / mdelay), then you will have to sleep. The granularity of this sleep depends on how frequently the timer interrupt ticks (HZ). Thus if HZ is 1000, then you cannot sleep for a period less than 1 msec.

>
> If my consumption is correct, where I can find the evidence?
> BTW, does Hz has anything related to kernel timing?
> From the comment in the kernel, it says
> Hz: clock ticks generated per second
> Does that mean the kernel will get #Hz timer interrupts per second?

Yes.

> Whz the value of Hz is 100? if the minimum reaction time of kernel is
> msecond, the value of Hz should be 1000, right?

Default value of HZ depends on the architecture - and you can change it as well. If HZ is 100, then minimum sleep is 10 ms. If you call msleep(1), you will still sleep for 10 msec atleast - msleep() only guarantees that you will sleep for ATLEAST the time you specified - you may obviously sleep for longer.

>>
>> At bootup the kernel measures the delay loop speed of
>> each CPU.  CPU frequency scaling might make the loop
>
> would you please let me know where the source code is?
> (measuring loop speed of cpu and scale cpu frequency)

calibrate_delay()


Thanks,

Rajat
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo RemoveThis @vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Bryan Donlan

External


Since: Aug 26, 2005
Posts: 16



(Msg. 5) Posted: Wed Nov 04, 2009 3:20 am
Post subject: Re: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, Nov 4, 2009 at 12:01 AM, Rajat Jain <Rajat.Jain.DeleteThis@infogain.com> wrote:
> Hi,
>
>> I find something interesting; kernel has msleep, but it
>> doesn't have usleep.
>> Does that mean the minimum time kernel can react is msecond
>> instead of usecond?
>> so if  users want to count useconds, they have to do the busy waiting,
>> execute some looping assembly instructions?
>
> You are roughly right. If you don't want to busy loop (udelay / mdelay), then you will have to sleep. The granularity of this sleep depends on how frequently the timer interrupt ticks (HZ). Thus if HZ is 1000, then you cannot sleep for a period less than 1 msec.

I thought hrtimers allow higher-precision wakeups these days?
Of course, if you only want to sleep for a few microseconds, the
context switch might take longer than you want to sleep...
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo.DeleteThis@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Rajat Jain

External


Since: Nov 04, 2009
Posts: 2



(Msg. 6) Posted: Wed Nov 04, 2009 3:20 am
Post subject: RE: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi,

>
> I thought hrtimers allow higher-precision wakeups these days?
> Of course, if you only want to sleep for a few microseconds, the
> context switch might take longer than you want to sleep...

Yes, that is right. Sorry, I missed that. But that is purely HW
dependent - if your HW providea a high precision timer (Such as HPET on
latest Intel machines), that can be used for quicker sleeps.

Thanks,

Rajat
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo.DeleteThis@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Rik van Riel

External


Since: Sep 12, 2004
Posts: 205



(Msg. 7) Posted: Wed Nov 04, 2009 9:20 am
Post subject: Re: why kernel implement "udelay" by cpu instructions? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 11/04/2009 12:36 AM, Bryan Donlan wrote:

> I thought hrtimers allow higher-precision wakeups these days?
> Of course, if you only want to sleep for a few microseconds, the
> context switch might take longer than you want to sleep...

Also, you may not be in a context where you can schedule.

Sometimes drivers need to implement a small delay (to wait
for something on the device) while holding a spinlock or
while interrupts are disabled.

--
All rights reversed.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo RemoveThis @vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Back to top
Login to vote
Display posts from previous:   
Related Topics:
RFC: implement daemon() in the kernel - [please CC me on replies] Hi, I'm working with Linux on MMUless systems, and one of the biggest issues in porting..

[PATCH 5/9] Paravirt drop udelay op - Not respecting udelay causes problems with any virtual hardware that is passed through to real hardware. This can be..

[PATCH][2.6.21] uml: fix unreasonably long udelay - Currently we have a confused udelay implementation. * __const_udelay does not accept usecs but xloops in i386 and..

[PATCH] KVM: AMD SVM: Avoid three more new instructions - The clgi, stgi, and invlpga instructions are all too new to unleash on the world's assemblers. Replace them with the..

[PATCH] Lguest32, use guest page tables to find paddr for .. - [Bug that was found by my previous patch] This patch allows things like modules, which don't have a direct __pa(EIP)..

[PATCH] KVM: x86 emulator: fix debug reg mov instructions - More fallout from the writeback fixes: debug register transfer instructions do their own writeback and thus need to..
       Soft32 Home -> Linux -> Kernel All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Categories:
 Windows
  Linux
 Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]