Re: [PATCH 1/5] Split wait_noreap_copyout()

Ingo Molnar
2009-05-11T21:05:27Z

* Vitaly Mayatskikh <v.mayatskih@gmail.com> wrote:

> At Mon, 11 May 2009 14:17:08 +0200, Ingo Molnar wrote:
>
> > > if (put_user(signal, &infop->si_signo) ||
> > > put_user(0, &infop->si_errno) ||
> > > put_user((short)why, &infop->si_code) ||
> > > put_user(pid, &infop->si_pid) ||
> > > put_user(uid, &infop->si_uid) ||
> > > put_user(status, &infop->si_status))
> > > return -EFAULT;
> >
> > For best assembly code this should generally be written as a series
> > of:
> >
> > __uaccess_err |= __put_user(x, ptr);
> > __uaccess_err |= __put_user(y, ptr);
> > __uaccess_err |= __put_user(z, ptr);
> >
> > As this generates non-dependent, compressed, branch-less code.
>
> Yeah, my first intention was to eliminate a lot of branches in one
> place. It's terrible for CPU pipeline, I bet.
>
> > See the (new) put_user_try / put_user_ex() / put_user_catch()
> > abstraction in arch/x86/include/asm/uaccess.h, and how all the
> > x86 signal code makes use of that to optimize such patterns of
> > per field user copies.
>
> So, there's catch block to handle GPF and the code inside of `try'
> block is still branch-less, right? I was thinking of minimized
> version of struct siginfo (up to si_uid) and copying it with
> single copy_to_user(), but the idea with try/catch is definitely
> much better.

It creates really nice assembly code. Hiroshi-san experimented with
it a lot until he found this form.

Regarding potentially generalizing that facility into generic code,
it relies on the exception code filling in
current_thread_info()->uaccess_err with -EFAULT. So it needs
architecture level support. It also kind of relies on
current_thread_info()->uaccess_err being super-optimal - which it is
on x86. (the assembler can optimize it)

But a compatible wrapper could be added, for architectures that dont
support, or that dont need support.

Ingo