wait_event_interruptible() seems non-atomic
Hi Jan,
Quoted 5 lines
I would like to also lock
Buffer_lockaround 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
Quoted 5 lines
Hi Jan,
Quoted 2 lines
I would like to also lock
Buffer_lockaround 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 youruif_readfunction with locking.
You are right that the test withinwait_event_interruptibleis
optimistic: a concurrentuif_readcould read the new data before the initialuif_readhas 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 andwait_eventsleeps. And the external loop withinuif_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 plaindown().
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
Quoted 5 lines · Jan Engelhardt
Quoted 4 lines
For example the use of
down_interruptible()looks wrong to me, I'd use plaindown().
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
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
For example the use of
down_interruptible()looks wrong to me, I'd use plaindown().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_interruptiblecalls. 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
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_interruptiblecalls. 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 thandown()?
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
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 thandown()?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 countsatomic_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