Skip to contentSkip to conversation

interruptible_sleep_on, interrupts and device drivers

DISCUSSION Gabriele Brugnoni 3 participants 3 messages Oct 15, 2005

lore
Share & tools
lore
#0 lore raw
Quoted 11 lines · Arjan van de Ven

don't.

interruptible_sleep_on() is a broken interface (see the comments in the header) and should not be used in any new code (where "new" is "since the year 2000" :)

Just use the wait_event() interfaces .... or just a simple semaphore even if what you want to do is simple and performance isn't too critical.

OK, i'll not use, but the kernel has a lot of device drivers using it, that may present the problem explained in my message.

In my code i've try the following:

		save_flags(flags); cli();
		if( !rs.txdone ) {
			if( arg < 0 ) arg = rs.ttimeout;
			if( arg > 0 )
				interruptible_sleep_on_timeout ( &rs.txwait, arg );
			else
				interruptible_sleep_on ( &rs.txwait );
		}
		restore_flags(flags);
		return rs.txdone;

Before testing the flag, interrupt are closed and if the TX is not terminated, the interruptible_sleep_on_timeout will be called with interrupt DISABLED. This is not a problem, because the scheduler will switch to another process, and the new process will enable again the IRQ. When the scheduler will give back the control to my process, it will continue with interrupt disabled, and will be reenabled before exit.
This seems to work very fine.

Calling the waiting function with IRQ enabled may expose the device driver to the risk of infinite wait (until a signal, obvious).

Thanks
Gabriele

#1 lore raw
Quoted 14 lines · Gabriele Brugnoni
Quoted 11 lines · Arjan van de Ven

don't.

interruptible_sleep_on() is a broken interface (see the comments in the header) and should not be used in any new code (where "new" is "since the year 2000" :)

Just use the wait_event() interfaces .... or just a simple semaphore even if what you want to do is simple and performance isn't too critical.

Quoted 6 lines

OK, i'll not use, but the kernel has a lot of device drivers using it, that may present the problem explained in my message.

In my code i've try the following:

		save_flags(flags); cli();

this is broken code; cli() cannot and should not be used. (and isn't even available on SMP kernels anymore)

Quoted 8 lines
		if( !rs.txdone ) {
			if( arg < 0 ) arg = rs.ttimeout;
			if( arg > 0 )
				interruptible_sleep_on_timeout ( &rs.txwait, arg );
			else
				interruptible_sleep_on ( &rs.txwait );
		}
		restore_flags(flags);

and this is missing a sti()

Quoted 1 line
		return rs.txdone;

your code is just wrong. Yes I know many really old drivers do this, but those are getting extinct fast..

#2 lore raw
Quoted 15 lines · Arjan van de Ven
Quoted 1 line · Gabriele Brugnoni
		save_flags(flags); cli();

this is broken code; cli() cannot and should not be used. (and isn't even available on SMP kernels anymore)

Quoted 8 lines
		if( !rs.txdone ) {
			if( arg < 0 ) arg = rs.ttimeout;
			if( arg > 0 )
				interruptible_sleep_on_timeout ( &rs.txwait, arg );
			else
				interruptible_sleep_on ( &rs.txwait );
		}
		restore_flags(flags);
Quoted 1 line

and this is missing a sti()

Err, no, that's wrong. sti() unconditionally enables interrupts - if an sti() was placed here, it's pointless using save_flags() (which saves the old interrupt enable state) and restore_flags() (which restores the interrupt enable state).

Also remember that interruptible_sleep_* is only safe on UP machines provided it's called with interrupts disabled.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core