Re: A general question on SMP-safe driver code.

From: Linus Torvalds
Date: Fri Dec 24 2004 - 18:29:47 EST




On Fri, 24 Dec 2004, Jim Nelson wrote:
>
> work if all other areas of the driver that send commands to the board also try for
> the semaphore?

The most common reason _not_ to use a semaphore, but a single simple
spinlock is:
- spinlocks are generally faster.
- you can't use semaphores to protect against interrupts, as interrupts
cannot take semaphores.

> Is there an easier way of doing this?

The simplest approach tends to be to just have a single spinlock per
driver (or, if the driver can drive multiple independent ports, one per
port).

The only advantage of semaphores is that you can do user accesses and you
can sleep during them, but if you're looking at converting a driver that
used to just depend on the global interrupt lock, that shouldn't be an
issue anyway. Generally, the semaphores are more useful at a higher level
(ie there is almost never any reason to protect actual _IO_ accesses with
a semaphore).

The biggest problem with converting old-style irq locks into spinlocks
tends to be that the irq locking allowed nesting (though the use of
save_flags/restore_flags), and normal spinlocks don't.

You can make your own nesting spinlocks, of course, but the reason there
aren't any standard nesting locks in the kernel is that in pretty much all
cases you can trivially avoid the nesting by just moving the lock
sufficiently far out, or just re-organizing the source a bit.

Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/