All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Filippov <jcmvbkbc@gmail.com>
To: Wonhyuk Yang <vvghjk1234@gmail.com>
Cc: 유형곤 <42.hyeyoo@gmail.com>,
	"kernelnewbies@kernelnewbies.org"
	<kernelnewbies@kernelnewbies.org>
Subject: Re: preemptible spinlock?
Date: Thu, 22 Apr 2021 23:45:36 -0700	[thread overview]
Message-ID: <CAMo8BfKtbu3KmauzxUfkVxDA154yNNEQCXXwH04uPDyCW7g1oQ@mail.gmail.com> (raw)
In-Reply-To: <CAEcHRTpexNvxZuHpYqXEjvnNbwHaBAX_VYbzm+8C9YycEgc26w@mail.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

  reply	other threads:[~2021-04-23  6:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22 15:50 preemptible spinlock? 유형곤
2021-04-23  6:27 ` Wonhyuk Yang
2021-04-23  6:45   ` Max Filippov [this message]
2021-04-24  5:51     ` Hyeonggon Yoo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMo8BfKtbu3KmauzxUfkVxDA154yNNEQCXXwH04uPDyCW7g1oQ@mail.gmail.com \
    --to=jcmvbkbc@gmail.com \
    --cc=42.hyeyoo@gmail.com \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=vvghjk1234@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.