All of lore.kernel.org
 help / color / mirror / Atom feed
* preemptible spinlock?
@ 2021-04-22 15:50 유형곤
  2021-04-23  6:27 ` Wonhyuk Yang
  0 siblings, 1 reply; 4+ messages in thread
From: 유형곤 @ 2021-04-22 15:50 UTC (permalink / raw)
  To: kernelnewbies

I'm learning how spinlock works.

I think the main idea spinlock disables preemption is,
the other process that's spinning on the lock can acquire lock.

but in some implementations of spinlock, like qspinlock in x86 (or mcs lock),
I think there's no need to disable preemption. because processes
waiting for lock cannot acquire the lock before the lock holder hand
over to other process.

is enabling preemption in such spinlock a bad idea?

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

* RE: preemptible spinlock?
  2021-04-22 15:50 preemptible spinlock? 유형곤
@ 2021-04-23  6:27 ` Wonhyuk Yang
  2021-04-23  6:45   ` Max Filippov
  0 siblings, 1 reply; 4+ messages in thread
From: Wonhyuk Yang @ 2021-04-23  6:27 UTC (permalink / raw)
  To: 유형곤, kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 619 bytes --]

Hi,

> I think the main idea spinlock disables preemption is,

> the other process that's spinning on the lock can acquire lock.



> but in some implementations of spinlock, like qspinlock in x86 (or mcs
lock),
> I think there's no need to disable preemption. because processes
> waiting for lock cannot acquire the lock before the lock holder hand
> over to other process.


Are you talking about disabling local irq(ex. spin_lock_irqsave)?

If so, think about the situation that a process holding the lock is
preempted

by interrupt. And that interrupt handler tries to grab the spinlock.

It will lead to deadlock.

[-- Attachment #1.2: Type: text/html, Size: 1160 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

* Re: preemptible spinlock?
  2021-04-23  6:27 ` Wonhyuk Yang
@ 2021-04-23  6:45   ` Max Filippov
  2021-04-24  5:51     ` Hyeonggon Yoo
  0 siblings, 1 reply; 4+ messages in thread
From: Max Filippov @ 2021-04-23  6:45 UTC (permalink / raw)
  To: Wonhyuk Yang; +Cc: 유형곤, kernelnewbies

On Thu, Apr 22, 2021 at 11:28 PM Wonhyuk Yang <vvghjk1234@gmail.com> wrote:
> > I think the main idea spinlock disables preemption is,
>
> > the other process that's spinning on the lock can acquire lock.
>
> > but in some implementations of spinlock, like qspinlock in x86 (or mcs lock),
> > I think there's no need to disable preemption. because processes
> > waiting for lock cannot acquire the lock before the lock holder hand
> > over to other process.
>
> Are you talking about disabling local irq(ex. spin_lock_irqsave)?
>
> If so, think about the situation that a process holding the lock is preempted
> by interrupt. And that interrupt handler tries to grab the spinlock.
> It will lead to deadlock.

Usually the word "preemption" is used when a task switch occurs and one task
preempts another task. Interrupts and tasklets are not usually described with
this word.

Now if an interrupt handler may need to acquire a lock that may also be
acquired by a task then the task must use spin_lock_irq or
spin_lock_irqsave to avoid the possibility of a deadlock. This is documented
in Documentation/kernel-hacking/locking.rst

Going back to the original question:

> > but in some implementations of spinlock, like qspinlock in x86 (or mcs lock),
> > I think there's no need to disable preemption. because processes
> > waiting for lock cannot acquire the lock before the lock holder hand
> > over to other process.

Imagine what happens if a task acquires a spinlock, gets interrupted,
task switch happens in the interrupt (remember, preemption is enabled),
and the new task tries to acquire the same spinlock on the same CPU?

-- 
Thanks.
-- Max

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

* Re: preemptible spinlock?
  2021-04-23  6:45   ` Max Filippov
@ 2021-04-24  5:51     ` Hyeonggon Yoo
  0 siblings, 0 replies; 4+ messages in thread
From: Hyeonggon Yoo @ 2021-04-24  5:51 UTC (permalink / raw)
  To: Max Filippov; +Cc: kernelnewbies, Wonhyuk Yang

Thank you so much for kindly explaining the problem!
as Wonhyuk Yang and Max Filippov said, I didn't consider the case that
interrupt handler holds same lock.

there can be deadlock as you said.

2021년 4월 23일 (금) 오후 3:45, Max Filippov <jcmvbkbc@gmail.com>님이 작성:
>
> On Thu, Apr 22, 2021 at 11:28 PM Wonhyuk Yang <vvghjk1234@gmail.com> wrote:
> > > I think the main idea spinlock disables preemption is,
> >
> > > the other process that's spinning on the lock can acquire lock.
> >
> > > but in some implementations of spinlock, like qspinlock in x86 (or mcs lock),
> > > I think there's no need to disable preemption. because processes
> > > waiting for lock cannot acquire the lock before the lock holder hand
> > > over to other process.
> >
> > Are you talking about disabling local irq(ex. spin_lock_irqsave)?
> >
> > If so, think about the situation that a process holding the lock is preempted
> > by interrupt. And that interrupt handler tries to grab the spinlock.
> > It will lead to deadlock.
>
> Usually the word "preemption" is used when a task switch occurs and one task
> preempts another task. Interrupts and tasklets are not usually described with
> this word.
>
> Now if an interrupt handler may need to acquire a lock that may also be
> acquired by a task then the task must use spin_lock_irq or
> spin_lock_irqsave to avoid the possibility of a deadlock. This is documented
> in Documentation/kernel-hacking/locking.rst
>
> Going back to the original question:
>
> > > but in some implementations of spinlock, like qspinlock in x86 (or mcs lock),
> > > I think there's no need to disable preemption. because processes
> > > waiting for lock cannot acquire the lock before the lock holder hand
> > > over to other process.
>
> Imagine what happens if a task acquires a spinlock, gets interrupted,
> task switch happens in the interrupt (remember, preemption is enabled),
> and the new task tries to acquire the same spinlock on the same CPU?
>
> --
> Thanks.
> -- Max

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

end of thread, other threads:[~2021-04-24  5:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 15:50 preemptible spinlock? 유형곤
2021-04-23  6:27 ` Wonhyuk Yang
2021-04-23  6:45   ` Max Filippov
2021-04-24  5:51     ` Hyeonggon Yoo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.