LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 08-03-2011, 08:14 AM   #1
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Rep: Reputation: 0
spinlocks


why spinlocks are disabled on a uniprocessor system?
 
Old 08-03-2011, 08:38 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
You should try a little research before asking questions, read the second paragraph here: http://en.wikipedia.org/wiki/Spinlock
 
Old 08-03-2011, 09:14 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
"Silly rabbit ... what's a spinlock for?" And, if you know that, "what does a spinlock have to guard against if there is only one CPU?"
 
1 members found this post helpful.
Old 08-03-2011, 11:29 PM   #4
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Smile

see i know but had a little confusion.I was thinking of a scenario

i have a uniprocessor machine

now one of my kernel threads say A has aquired a spinlock on particular data structure say DS .There can be a situation that it gets scheduled and another thread say B gets scheduled which is operating on the same data structure say DS now since spinlocks are disabled thread B will start operating on DS which can lead to race conditions .Please correct me if I am wrong .I am very new to linux kernel .


Thanx in advance
 
Old 08-04-2011, 12:35 AM   #5
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
The scenario you have mentioned can not occur with spinlocks.

If Process A holds a spinlock, it can not be taken out of the processor because kernel preemption gets disabled while holding a spinlock.
Even if did get preempted while holding the lock, and process B tries to acquire the lock it will fail as process A would be holding it and Process B would spin endlessly on the lock. Because CPU will be kept busy by the Process B and there is no way for process A to come back and release the lock, leading to a perfect situation of a deadlock.
 
Old 08-04-2011, 11:21 PM   #6
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Thumbs up to bsat

hi

the scenario of which I am talking about is on uniprocessor system where spinlocks are disabled so at first A will not be able to lock.
only kernel preemption gets disabled .As kernel preemtion is disabled so a process in user space cannot be scheduled but what if a kernel thread B gets scheduled .what you say.(A and B both are kthreads so will be in kernel context)
 
Old 08-05-2011, 07:25 AM   #7
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
I am not really clear what exactly you are trying to say.
Quote:
spinlocks are disabled so at first A will not be able to lock
when spinlock is disabled, what are you trying to lock ?

If multiple threads have to access the same data you could protect it using semaphore or use atomic operations. Failing to do so is poor programming and there is nothing that kernel can do for that.
 
Old 08-05-2011, 11:48 PM   #8
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Thumbs up to bsat

hi
what I am trying to say is that spinlocks are disabled on uniprocessor so if I want to use spinlocks in two of my kthreads I will not be ale to .

scenario- global is shared


kthread a //kernel thread
spinlock()//not actual syntax
global++;
spinunlock()//not actual syntax

kthread b //kernel thread
spinlock()//not actual syntax
global++;
spinunlock()//not actual syntax

providing it is uniprocessor now so i cannot use spinlock cause they are disabled .Correct me I am wrong so in such scenararios I am supposed to use other locking schemes.

Last edited by ashish anand; 08-05-2011 at 11:54 PM.
 
Old 08-06-2011, 01:25 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Correct me I am wrong
OK. You're wrong

1. Sundialsvcs said it best:
Quote:
"Silly rabbit ... what's a spinlock for?" And, if you know that, "what does a spinlock have to guard against if there is only one CPU?"
2.Maybe the source of your confusion is that perhaps you're thinking "if spinlocks are disabled on a UP kernel" (and they are), "then I can't use them in my code." But you CAN!

3. If you have a critical section (for example, in a kernel driver), then use spinlocks.
If this code is executed on a UP, the code will effectively be a no-op.

This article might explain a bit better:

http://www.linuxjournal.com/article/5833

In particular:
Quote:
The basic use of the spinlock is:
Code:
spinlock_t mr_lock = SPIN_LOCK_UNLOCKED;
unsigned long flags;
spin_lock_irqsave(&mr_lock, flags);
/* critical section ... */
spin_unlock_irqrestore(&mr_lock, flags);
The use of spin_lock_irqsave() will disable interrupts locally and provide the spinlock on SMP. This covers both interrupt and SMP concurrency issues. With a call to spin_unlock_irqrestore(), interrupts are restored to the state when the lock was acquired. With a UP kernel, the above code compiles to the same as:
Code:
unsigned long flags;
save_flags(flags);
cli();
/* critical section ... */
restore_flags(flags);
which will provide the needed interrupt concurrency protection without unneeded SMP protection.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphores and Spinlocks lrios Linux - Kernel 3 07-29-2008 08:13 AM
reg Kernal Locking - Spinlocks deep123 Linux - Newbie 0 02-09-2005 05:59 AM
spinlocks on SMP only? rgiggs Programming 3 06-07-2004 12:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration