kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: kai@gnukai.com (Kai Meyer)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Spinlocks and interrupts
Date: Thu, 10 Nov 2011 11:14:21 -0700	[thread overview]
Message-ID: <4EBC147D.1010604@gnukai.com> (raw)
In-Reply-To: <4EBC11C5.9070507@gnukai.com>

I think I get it. I'm hitting the scheduling while atomic because I'm 
calling my function from a struct bio's endio function, which is 
probably running with a lock held somewhere else, and then my mutex 
sleeps, while the spin_lock functions do not sleep.

Perhaps I need to learn more about the context in which my endio 
function is being called.

On 11/10/2011 11:02 AM, Kai Meyer wrote:
> Well, I changed my code to use a mutex instead of a spinlock, and now I get:
> BUG: scheduling while atomic: swapper/0/0x10010000
> All I changed was the spinlock_t to a struct mutex, and call mutex_init,
> mutex_lock, and mutex_unlock where I was previously calling the
> spin_lock variations. I'm confused. What does mutex_lock do besides set
> values in an atomic_t?
>
> -Kai Meyer
>
> On 11/10/2011 10:02 AM, Kai Meyer wrote:
>> On 11/09/2011 08:38 PM, Dave Hylands wrote:
>>> Hi Kai,
>>>
>>> On Wed, Nov 9, 2011 at 3:12 PM, Kai Meyer<kai@gnukai.com>    wrote:
>>>> Ok, I need mutual exclusion on a data structure regardless of interrupts
>>>> and core. It sounds like it can be done by using a spinlock and
>>>> disabling interrupts, but you mention that "spinlocks are intended to
>>>> provide mutual exclsion between interrupt context and non-interrupt
>>>> context." Should I be using a semaphore (mutex) instead?
>>> It depends. If the function is only called from thread context, then
>>> you probably want to use a mutex. If there is a possibility that it
>>> might be called from interrupt context, then you can't use a mutex.
>>>
>>> Also, remember that spin-locks are no-ops on a single processor
>>> machine, so as coded, you have no protection on a single-processor
>>> machine if you're calling from thread context.
>>>
>> To make sure I understand you, it sounds like there's two contexts I
>> need to be concerned about, thread context and interrupt context. As far
>> as I can be sure, this code will only run in thread context. If you
>> could verify for me that a block device's make request function is only
>> reached in thread context, then that would make me doubly sure.
>>>> Perhaps I could explain my problem with some code:
>>>> struct my_struct *get_data(spinlock_t *mylock, int ALLOC_DATA)
>>>> {
>>>>        struct my_struct *mydata = NULL;
>>>>        spin_lock(mylock);
>>>>        if (test_bit(index, mybitmap))
>>>>                mydata = retrieve_data();
>>>>        if (!mydata&&    ALLOC_DATA) {
>>>>                mydata = alloc_data();
>>>>                set_bit(index, mybitmap);
>>>>        }
>>>>        spin_unlock(mylock);
>>>>        return mydata;
>>>> }
>>>>
>>>> I need to prevent retrieve_data from being called if the index bit is
>>>> set in mybitmap and alloc_data has not completed, so I use a bitmap to
>>>> indicate that alloc_data has completed. I also need to protect
>>>> alloc_data from being run multiple times, so I use the spin_lock to
>>>> ensure that test_bit (and possibly retrieve_data) is not run while
>>>> alloc_data is being run (because it runs while the bit is cleared).
>>> If alloc_data might block, then you can't disable interrupts and you
>>> definitely shouldn't be using spinlocks.
>>>
>> alloc_data will call kmalloc(size, GFP_KERNEL), which I think may block,
>> so disabling irqs is out.
>>
>> Between thread context and kmalloc with GFP_KERNEL, it sounds like your
>> suggestion would be to use a mutex. Is that correct?
>>
>> -Kai Meyer
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  reply	other threads:[~2011-11-10 18:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-09 21:07 Spinlocks and interrupts Kai Meyer
2011-11-09 21:40 ` Dave Hylands
2011-11-09 23:12   ` Kai Meyer
2011-11-10  0:03     ` Jeff Haran
2011-11-10  3:38     ` Dave Hylands
2011-11-10 17:02       ` Kai Meyer
2011-11-10 18:02         ` Kai Meyer
2011-11-10 18:14           ` Kai Meyer [this message]
2011-11-10 19:07             ` Dave Hylands
2011-11-10 19:19               ` Jeff Haran
2011-11-10 21:55                 ` Kai Meyer
2011-11-10 23:00                   ` Jeff Haran
2011-11-10 23:20                     ` Kai Meyer
2011-11-11  6:58                       ` Rajat Sharma
2011-11-14 19:05                         ` Kai Meyer
2011-11-10  4:47   ` rohan puri
2011-11-10  5:47     ` Rajat Sharma
2011-11-10 10:05       ` santhosh kumars

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=4EBC147D.1010604@gnukai.com \
    --to=kai@gnukai.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).