Re: Can I use semaphore inside spinlock?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/11/05, Tetsuo Handa <from-kernelnewbies@xxxxxxxxxxxxxxxxxxx> wrote:
> Hello.

> I posted this question 3 months ago, but I post again, for
> I'm worried that this will cause a potential deadlock bug.

Always worth asking this question because it provides an opportunity
to recall why everything is ok.

> Can a process do semaphore operations while the process is
> holding a spinlock?

No. Well, you might wake something up later.

> As far as I know, a process must not call semaphore operations
> nor functions that may sleep (such as kmalloc(GFP_KERNEL))
> when the process is holding a spinlock.

That's right. To do so would be a really bad idea.

> But I can find the following sequence.
>
> Step 1: lock_kernel() is called from sys_mount().
> ( at http://lxr.linux.no/source/fs/namespace.c?v=2.4.28#L855 )

Yes. The BKL is a magical super dooper uber ultra lock which violates
*all* of the usual behaviours and can be called in contexts when you
would normally not use a spinlock. It's an ugly hack that's slowly
going away.

> Why it is allowed to do semaphore operations (or kmalloc(GFP_KERNEL))
> while the kernel lock is held.

Because the BKL is very special.

> This will sleep and cause deadlock, won't this?

No. The BKL gets silently released and reacquired without you noticing
it is happening.

Jon.

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux