Re: Could this become a race condition?

Linus Torvalds (torvalds@cs.helsinki.fi)
Thu, 17 Oct 1996 19:00:06 +0300 (EET DST)


On Wed, 16 Oct 1996, Matti Aarnio wrote:
>
> > Hi,
> >
> > in /usr/src/linux/include/linux/locks.h of kernel 2.0.21, I can see :
> >
> > extern inline void lock_super(struct super_block * sb)
> > {
> > if (sb->s_lock)
> > __wait_on_super(sb);
> > sb->s_lock = 1;
> > }
> ...
> .. is there a deadlock ? ..
> ...
> > Is there any way to avoid that?
>
> Yes. Yes:
>
> while (set_bit(1,&sb->s_lock) != 0)
> __wait_on_super(sb);
>
> While the old value of the s_lock is non-zero, call
> __wait_on_super(), else leave with s_lock set.
> See <asm/bitops.h> for the routines.

Actually, the old code is not a race condition, because "sb_lock" is only
modified in a "process context" and never from interrupts. As such, the
general non-re-entrancy of the kernel will guarantee that you can do
"atomic" operations like the above without using the setbit() etc atomic
stuff

Linus