Re: [patch] cpufreq: mark cpufreq_tsc() as core_initcall_sync

From: Alan Stern
Date: Sat Nov 25 2006 - 17:09:21 EST


On Sat, 25 Nov 2006, Oleg Nesterov wrote:

> > void xxx_read_unlock(struct xxx_struct *sp, int idx)
> > {
> > spin_lock(&sp->lock);
>
> It is possible that the memory ops that occur before spin_lock() is not yet
> completed,
>
> > if (--sp->ctr[idx] == 0)
>
> suppose that synchronize_xxx() just unlocked sp->lock. It sees sp->ctr[idx] == 0
> and returns.
>
> > wake_up(&sp->wq);
> > spin_unlock(&sp->lock);
>
> This is a one-way barrier, yes. But it is too late.

Yes, you are right. The corrected routine (including your little
optimization) looks like this:

void synchronize_xxx(struct xxx_struct *sp)
{
int idx;

mutex_lock(&sp->mutex);
spin_lock(&sp->lock);
idx = sp->completed & 0x1;
if (sp->ctr[idx] == 1)
goto done;

++sp->completed;
--sp->ctr[idx];
sp->ctr[idx ^ 1] = 1;

spin_unlock(&sp->lock);
__wait_event(sp->wq, sp->ctr[idx] == 0);
spin_lock(&sp->lock);

done:
spin_unlock(&sp->lock);
mutex_unlock(&sp->mutex);
}

> Actually, synchronize_xxx() may sleep on sp->wq and we still have a race.
> synchronize_xxx() can return before ->wake_up() unlocks sp->wq.lock (finish_wait()
> doesn't take sp->wq.lock due to autoremove_wake_function()).

The version above doesn't suffer from that race.

> This is more or less equivalent to
>
> void synchronize_xxx(struct xxx_struct *sp)
> {
> int idx;
>
> mutex_lock(&sp->mutex);
>
> idx = sp->completed & 0x1;
> atomic_dec(sp->ctr + idx);
> smp_mb__before_atomic_inc();
> atomic_inc(sp->ctr + (idx ^ 0x1));
> sp->completed++;
>
> wait_event(sp->wq, !atomic_read(sp->ctr + idx));
> mutex_unlock(&sp->mutex);
> }

It may indeed be equivalent. But _proving_ it is equivalent is certainly
not easy. The advantage of spinlocks is that they remove the necessity
for outrageous mental contortions to verify that all possible execution
paths will work correctly.

> Honestly, I don't see why it is better, but may be this is just me.
> In any case, spinlock based implementation shouldn't be faster, yes?

It will generally be somewhat slower. In addition to cache-line
contention it suffers from lock contention. The difference shouldn't be
enough to matter unless a lot of threads are trying to acquire a read lock
simultaneously.

Alan Stern

-
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/