public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: wait_event_interruptible() seems non-atomic
@ 2004-11-20 16:16 Manfred Spraul
  2004-11-20 16:26 ` Jan Engelhardt
  0 siblings, 1 reply; 7+ messages in thread
From: Manfred Spraul @ 2004-11-20 16:16 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: linux-kernel

Hi Jan,

>I would like to also lock Buffer_lock around BufRP != BufWP, but don't see a
>way on how to accomplish this.
>
>  
>
This is not a problem: You compare BufRP and BufWP twice: once within 
wait_event_interruptible (without locking) and again a second test in 
your uif_read function with locking.
You are right that the test within wait_event_interruptible is 
optimistic: a concurrent uif_read could read the new data before the 
initial uif_read has a chance to acquire the BufferLock. But it doesn't 
matter: AFAICS the test is optimistic, it can't happen that BufRP and WP 
are actually different and wait_event sleeps. And the external loop 
within uif_read() just loops if the race that you describe happened.

Btw, could you post a link to the complete driver when asking questions? 
For example the use of down_interruptible() looks wrong to me, I'd use 
plain down().

--
    Manfred

^ permalink raw reply	[flat|nested] 7+ messages in thread
* wait_event_interruptible() seems non-atomic
@ 2004-11-20 15:35 Jan Engelhardt
  0 siblings, 0 replies; 7+ messages in thread
From: Jan Engelhardt @ 2004-11-20 15:35 UTC (permalink / raw)
  To: linux-kernel

Hi list,


upon reviewing some of my code for a device driver, I've come across:


static ssize_t uif_read(struct file *filp, char __user *buf,
  size_t count, loff_t *ppos)
{
    // Nothing read, nothing done
    if(count == 0) { return 0; }

    // Must sleep as long as there is no data
    if(down_interruptible(&Buffer_lock)) { return -ERESTARTSYS; }
    while(BufRP == BufWP) {
        up(&Buffer_lock);
        if(filp->f_flags & O_NONBLOCK) { return -EAGAIN; }

        // hm, the condition is not atomic or locked...
        if(wait_event_interruptible(Pull_queue, (BufRP != BufWP))) {
            return -ERESTARTSYS;
        }
        if(down_interruptible(&Buffer_lock)) { return -ERESTARTSYS; }
    }

    // Data is available, so give it to the user
    ...
}

As you can see, I lock Buffer_lock and then check BufRP == BufWP. I do that
because in an SMP environment, either of BufRP or BufWP might have been
updated. (I.e. one CPU currently does "mov BufRP to eax; mov BufWP to ebx; cmp
eax, ebx" while another does "inc BufWP")

I would like to also lock Buffer_lock around BufRP != BufWP, but don't see a
way on how to accomplish this.

Does anybody know a way how this could be achieved?



Jan Engelhardt
-- 
Gesellschaft für Wissenschaftliche Datenverarbeitung
Am Fassberg, 37077 Göttingen, www.gwdg.de

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

end of thread, other threads:[~2004-11-21 10:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-20 16:16 wait_event_interruptible() seems non-atomic Manfred Spraul
2004-11-20 16:26 ` Jan Engelhardt
2004-11-20 18:31   ` Manfred Spraul
2004-11-21  9:40     ` Jan Engelhardt
2004-11-21 10:00       ` Manfred Spraul
2004-11-21 10:21         ` Jan Engelhardt
  -- strict thread matches above, loose matches on Subject: below --
2004-11-20 15:35 Jan Engelhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox