Re: [PATCH RESEND v2 1/1] percpu_rw_semaphore: reimplement to notblock the readers unnecessarily

From: Oleg Nesterov
Date: Sun Nov 11 2012 - 10:44:35 EST


On 11/09, Paul E. McKenney wrote:
>
> On Fri, Nov 09, 2012 at 07:10:48PM +0100, Oleg Nesterov wrote:
> >
> > static bool xxx(brw)
> > {
> > down_write(&brw->rw_sem);
>
> down_write_trylock()
>
> As you noted in your later email. Presumably you return false if
> the attempt to acquire it fails.

Yes, yes, thanks.

> > But first we should do other changes, I think. IMHO we should not do
> > synchronize_sched() under mutex_lock() and this will add (a bit) more
> > complications. We will see.
>
> Indeed, that does put considerable delay on the writers. There is always
> synchronize_sched_expedited(), I suppose.

I am not sure about synchronize_sched_expedited() (at least unconditionally),
but: only the 1st down_write() needs synchronize_, and up_write() do not
need to sleep in synchronize_ at all.

To simplify, lets ignore the fact that the writers need to serialize with
each other. IOW, the pseudo-code below is obviously deadly wrong and racy,
just to illustrate the idea.

1. We remove brw->writer_mutex and add "atomic_t writers_ctr".

update_fast_ctr() uses atomic_read(brw->writers_ctr) == 0 instead
of !mutex_is_locked().

2. down_write() does

if (atomic_add_return(brw->writers_ctr) == 1) {
// first writer
synchronize_sched();
...
} else {
... XXX: wait for percpu_up_write() from the first writer ...
}

3. up_write() does

if (atomic_dec_unless_one(brw->writers_ctr)) {
... wake up XXX writers above ...
return;
} else {
// the last writer
call_rcu_sched( func => { atomic_dec(brw->writers_ctr) } );
}

Once again, this all is racy, but hopefully the idea is clear:

- down_write(brw) sleeps in synchronize_sched() only if brw
has already switched back to fast-path-mode

- up_write() never sleeps in synchronize_sched(), it uses
call_rcu_sched() or wakes up the next writer.

Of course I am not sure this all worth the trouble, this should be discussed.
(and, cough, I'd like to add the multi-writers mode which I'm afraid nobody
will like) But I am not going to even try to do this until the current patch
is applied, I need it to fix the bug in uprobes and I think the current code
is "good enough". These changes can't help to speedup the readers, and the
writers are slow/rare anyway.

Thanks!

Oleg.

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