Skip to contentSkip to conversation

wait_event_interruptible() seems non-atomic

DISCUSSION Manfred Spraul 2 participants 6 messages Nov 20, 2004 – Nov 21, 2004

lore
Share & tools
lore
#0 lore raw

Hi Jan,

Quoted 5 lines

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

#1 lore raw
Quoted 5 lines

Hi Jan,

Quoted 2 lines

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

Quoted 9 lines

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.

Yay, I see now. It takes a fair amount of looking behind the scene to get the idea. (It's just copy-and-paste from an O'reilly book with a few modifications.)
Yes, there is always only one way from any insn you look at, which does not require a lock.

Quoted 1 line

Btw, could you post a link to the complete driver when asking questions?

http://ttyrpld.sf.net/ -- "kernel-2.6/rpldev.c" in the tarball.

Quoted 2 lines

For example the use of down_interruptible() looks wrong to me, I'd use plain down().

I'd like to be able to hit Ctrl+C (in the userspace application) whenever possible. If that's not a reason, blame the book
http://www.xml.com/ldd/chapter/book/ch03.html#t8 ("the read method" a further down below)

BTW, the complete book is at http://www.xml.com/ldd/chapter/book/index.html

Thanks,
Jan Engelhardt

Signature · 2 lines
Gesellschaft für Wissenschaftliche Datenverarbeitung
Am Fassberg, 37077 Göttingen, www.gwdg.de
#2 lore raw
Quoted 5 lines · Jan Engelhardt
Quoted 4 lines

For example the use of down_interruptible() looks wrong to me, I'd use plain down().

Quoted 7 lines

I'd like to be able to hit Ctrl+C (in the userspace application) whenever possible. If that's not a reason, blame the book
http://www.xml.com/ldd/chapter/book/ch03.html#t8 ("the read method" a further down below)

As far as I can see BufferLock is only held for tiny sections - the longest thing is a copy_to_user(), i.e. at worst a swap in. I my opinion the delay for handling Ctrl+C is therefore negligible and not worth the added code for handling down_interruptible(). You have already written the code, so I'd leave it as it is and I'll blame the book. They probably started from an older version of fs/pipe.c, which contained _interruptible calls. There are gone now, this allowed some cleanup.

--
Manfred

#3 lore raw
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

For example the use of down_interruptible() looks wrong to me, I'd use plain down().

I'd like to be able to hit Ctrl+C (in the userspace application) whenever possible. If that's not a reason, blame the book
http://www.xml.com/ldd/chapter/book/ch03.html#t8 ("the read method" a further down below)

Quoted 4 lines

You have already written the code, so I'd leave it as it is and I'll blame the book. They probably started from an older version of fs/pipe.c, which contained _interruptible calls. There are gone now, this allowed some cleanup.

Well, it's just one line so I would not care, and I'm also open for suggestions. Does down_interruptible() cost so much more in CPU cycles than down()?

Jan Engelhardt

Signature · 2 lines
Gesellschaft für Wissenschaftliche Datenverarbeitung
Am Fassberg, 37077 Göttingen, www.gwdg.de
#4 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 6 lines

You have already written the code, so I'd leave it as it is and I'll blame the book. They probably started from an older version of fs/pipe.c, which contained _interruptible calls. There are gone now, this allowed some cleanup.

Quoted 6 lines

Well, it's just one line so I would not care, and I'm also open for suggestions. Does down_interruptible() cost so much more in CPU cycles than down()?

It's more about code complexity than performance. down_interruptible() means that you must handle failures - double check that you free all temporary allocations, decrease all reference counts (make the reference counts atomic_t), etc.

--
Manfred

#5 lore raw
Quoted 8 lines
Quoted 3 lines

Well, it's just one line so I would not care, and I'm also open for suggestions. Does down_interruptible() cost so much more in CPU cycles than down()?

It's more about code complexity than performance. down_interruptible() means that you must handle failures - double check that you free all temporary allocations, decrease all reference counts (make the reference counts atomic_t), etc.

All considered. rpldev.c only has 4 occurrences of down_interruptible, all which are near the start of the function body. There's nothing to deallocate at the time down_interruptible() is due ;-)

Jan Engelhardt

Signature · 2 lines
Gesellschaft für Wissenschaftliche Datenverarbeitung
Am Fassberg, 37077 Göttingen, www.gwdg.de