Skip to contentSkip to conversation

The Art Of Locking

parisc-linux Matthew Wilcox 2 participants 3 messages Jan 18, 2001 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 4 lines · Grant Grundler

Log message:
First cut of CONFIG_SMP support. Default is disabled and my C3k still boots/works that way. With CONFIG_SMP=y, hangs after kswapd msg. Willy tells me that's the first place semaphores are used.

I'm reviewing this code for Grant. He's kindly allowed me to point out the flaws publically for the benefit of all.

First, Rusty has written an excellent document on locking in Documentation/Docbook/kernel-locking.tmpl. You can turn this into a pdf with `make pdfdocs' (other formats are also available).

Here's the code grant wrote in irq.c:

spinlock_t irq_lock = SPIN_LOCK_UNLOCKED; /* protect IRQ regions */ [...]

        for (regnr = 0; regnr < NR_IRQ_REGS; regnr++) {
           spin_lock_irqsave(&irq_lock, flags);
            region = irq_region[regnr];
           if (!region || !region->action) {
               spin_unlock_irqrestore(&irq_lock, flags);
                continue;
           }
[...]
           spin_unlock_irqrestore(&irq_lock, flags);
       }

The first thing is the use of spin_lock_irqsave. This disables local interrupts and takes the spinlock. However, this code can't be called from interrupt context, and nor can any of the other places which take this lock. So there's no need to disable local irqs. We _are_ messing with the irq tables at this point (though read-only), but that either means that we have to do a _global_ interrupt disable (which is bad and slow) or it was ok anyway. Since this is read only, I assert that there's no need to disable local irqs, so we can use the spin_lock() function instead of spin_lock_irqsave.

Next, the lock is taken and released at each iteration. Why not take it outside the loop body? Well, one reason might be to reduce latency -- if other people are sleeping on that lock, they should be given a chance. But the other people who use this lock are free_irq and request_irq -- hardly important latency-wise sincethey're almost always called at device driver initialisation (bootup or module load/unload).

Finally, it's not declared `static', yet it's only used within this file.

Signature · 1 line
Revolutions do not require corporate support.
#2 lore raw

Matthew Wilcox wrote:
...

Quoted 2 lines

I'm reviewing this code for Grant. He's kindly allowed me to point out the flaws publically for the benefit of all.

yup...overall willy pointed out some good things.
Let me correct a few nits though.

Quoted 4 lines

The first thing is the use of spin_lock_irqsave. This disables local interrupts and takes the spinlock. However, this code can't be called from interrupt context, and nor can any of the other places which take this lock. So there's no need to disable local irqs.

Agreed. I've fixed that and added a test to warn us in case request_irq() or free_irq() are called from the interrupt context.

Quoted 3 lines

Since this is read only, I assert that there's no
need to disable local irqs, so we can use the spin_lock() function instead of spin_lock_irqsave.

Not quite correct. The lock is needed to protect the read-only traversing of a linked list. We can leave local interrupts enabled though for above reasons though.

...

Quoted 1 line

Finally, it's not declared `static', yet it's only used within this file.

It's used in:

    fs/proc/proc_misc.c:extern int get_irq_list(char *);
    fs/proc/proc_misc.c:  int len = get_irq_list(page);

Each arch has to provide this entry point.

thanks though for the other good points!
grant

Grant Grundler
Unix Systems Enablement Lab
+1.408.447.7253

#3 lore raw
Quoted 5 lines · Grant Grundler
Quoted 1 line

Finally, it's not declared `static', yet it's only used within this file.

It's used in:

    fs/proc/proc_misc.c:extern int get_irq_list(char *);
    fs/proc/proc_misc.c:  int len = get_irq_list(page);

I'm sorry for being ambiguous. I meant that the spin_lock should be static, not the function.

Signature · 1 line
Revolutions do not require corporate support.