From: Mathieu Desnoyers <mathieu.desnoyers-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
To: Andy Lutomirski <luto-kltTT9wpgjJwATOyAt5JVQ@public.gmane.org>
Cc: "Paul E. McKenney"
<paulmck-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>,
Ben Maurer <bmaurer-b10kYP2dOMg@public.gmane.org>,
Ingo Molnar <mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
Andrew Morton
<akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
Josh Triplett <josh-iaAMLnmF4UmaiuxdJuQwMA@public.gmane.org>,
Lai Jiangshan <laijs-BthXqXjhjHXQFUHtdCDX3A@public.gmane.org>,
Paul Turner <pjt-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
rostedt <rostedt-nx8X9YLhiw1AfugRpC6u6w@public.gmane.org>,
Andrew Hunter <ahh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
linux-api <linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
Linus Torvalds
<torvalds-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
Peter Zijlstra <peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
Subject: Re: [RFC PATCH] getcpu_cache system call: caching current CPU number (x86)
Date: Mon, 13 Jul 2015 15:27:32 +0000 (UTC) [thread overview]
Message-ID: <1050138282.1065.1436801252018.JavaMail.zimbra@efficios.com> (raw)
In-Reply-To: <CALCETrV1suAbvMgD1jOEFyn3JcDE_hhi6X7+sGs9e3Oqw_6jUw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
----- On Jul 12, 2015, at 11:38 PM, Andy Lutomirski luto-kltTT9wpgjJwATOyAt5JVQ@public.gmane.org wrote:
> On Jul 12, 2015 12:06 PM, "Mathieu Desnoyers"
> <mathieu.desnoyers-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org> wrote:
>>
>> Expose a new system call allowing threads to register a userspace memory
>> area where to store the current CPU number. Scheduler migration sets the
>> TIF_NOTIFY_RESUME flag on the current thread. Upon return to user-space,
>> a notify-resume handler updates the current CPU value within that
>> user-space memory area.
>>
>> This getcpu cache is an alternative to the sched_getcpu() vdso which has
>> a few benefits:
>> - It is faster to do a memory read that to call a vDSO,
>> - This cache value can be read from within an inline assembly, which
>> makes it a useful building block for restartable sequences.
>>
>
> Let's wait and see what the final percpu atomic solution is. If it
> involves percpu segments, then this is unnecessary.
percpu segments will likely not solve everything. I have a use-case
with dynamically allocated per-cpu ring buffer in user-space (lttng-ust)
which can be a challenge for percpu segments. Having a fast getcpu()
is a win in those cases.
>
> Also, this will need to be rebased onto -tip, and that should wait
> until the big exit rewrite is farther along.
I don't really care which thread flag it ends up using, and this is
more or less an internal implementation detail. The important part is
the ABI exposed to user-space, and it's good to start the discussion
on this aspect early.
>
>> This approach is inspired by Paul Turner and Andrew Hunter's work
>> on percpu atomics, which lets the kernel handle restart of critical
>> sections:
>> Ref.:
>> * https://lkml.org/lkml/2015/6/24/665
>> * https://lwn.net/Articles/650333/
>> *
>> http://www.linuxplumbersconf.org/2013/ocw/system/presentations/1695/original/LPC%20-%20PerCpu%20Atomics.pdf
>>
>> Benchmarking sched_getcpu() vs tls cache approach. Getting the
>> current CPU number:
>>
>> - With Linux vdso: 12.7 ns
>
> This is a bit unfair, because the glibc wrapper sucks and the
> __vdso_getcpu interface is overcomplicated. We can fix it with a
> better API. It won't make it *that* much faster, though.
Even if we improve the vDSO function, we are at a point where just
the function call is not that cheap.
>
>>
>> diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c
>> index e504246..157cec0 100644
>> --- a/arch/x86/kernel/signal.c
>> +++ b/arch/x86/kernel/signal.c
>> @@ -750,6 +750,8 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32
>> thread_info_flags)
>> if (thread_info_flags & _TIF_NOTIFY_RESUME) {
>> clear_thread_flag(TIF_NOTIFY_RESUME);
>> tracehook_notify_resume(regs);
>> + if (getcpu_cache_active(current))
>> + getcpu_cache_handle_notify_resume(current);
>
> We need to disentangle this stuff. This is buried way too deeply here.
>
> Fortunately, do_notify_resume is going away. It's already unused on
> 64-bit kernels in -next.
Cool! Of course, I'm willing to rebase this on whichever thread flag
and notification upon resume to userspace makes more sense.
>
>> +/*
>> + * This resume handler should always be executed between a migration
>> + * triggered by preemption and return to user-space.
>> + */
>> +void getcpu_cache_handle_notify_resume(struct task_struct *t)
>> +{
>> + int32_t __user *gcp = t->getcpu_cache;
>> +
>> + if (gcp == NULL)
>> + return;
>> + if (unlikely(t->flags & PF_EXITING))
>> + return;
>> + /*
>> + * access_ok() of gcp_user has already been checked by
>> + * sys_getcpu_cache().
>> + */
>> + if (__put_user(raw_smp_processor_id(), gcp))
>> + force_sig(SIGSEGV, current);
>
> We're preemptible here, although I think it's okay. But I'd at least
> clear the getcpu_cache state if __put_user fails, because otherwise
> it's not entirely obvious to me that we can't infinite loop.
Good point. For safety's sake, I'll set t->getcpu_cache to NULL.
>
>> +/*
>> + * sys_getcpu_cache - setup getcpu cache for caller thread
>> + */
>> +SYSCALL_DEFINE2(getcpu_cache, int32_t __user *, gcp, int, flags)
>> +{
>> + if (flags)
>> + return -EINVAL;
>> + if (gcp != NULL && !access_ok(VERIFY_WRITE, gcp, sizeof(int32_t)))
>> + return -EFAULT;
>> + current->getcpu_cache = gcp;
>> + /* Will update *gcp on resume */
>> + if (gcp)
>> + set_thread_flag(TIF_NOTIFY_RESUME);
>> + return 0;
>> +}
>
> IMO this is impolite. If the pointer is bad, we should return -EFAULT
> rather than sending SIGSEGV.
OK, so I guess you mean we should do the __put_user() in getcpu_cache
too, rather than relying on the one in notify_resume, so we can handle
faults there and return -EFAULT rather than sending SIGSEGV. Yep, it
makes sense, will fix.
Thanks!
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
next prev parent reply other threads:[~2015-07-13 15:27 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-12 18:06 [RFC PATCH] getcpu_cache system call: caching current CPU number (x86) Mathieu Desnoyers
[not found] ` <1436724386-30909-1-git-send-email-mathieu.desnoyers-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-12 18:47 ` Josh Triplett
2015-07-13 3:40 ` Andy Lutomirski
2015-07-13 15:09 ` Mathieu Desnoyers
2015-07-13 3:38 ` Andy Lutomirski
[not found] ` <CALCETrV1suAbvMgD1jOEFyn3JcDE_hhi6X7+sGs9e3Oqw_6jUw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-13 15:27 ` Mathieu Desnoyers [this message]
[not found] ` <1050138282.1065.1436801252018.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-13 15:30 ` Andrew Hunter
[not found] ` <CADroS=7MnUULrjDeQtmscxjkpjCtti9V-HfFXU0sjKhi6PsaAg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-13 16:07 ` Mathieu Desnoyers
2015-07-13 18:36 ` Andy Lutomirski
2015-07-13 11:17 ` Ben Maurer
[not found] ` <5CDDBDF2D36D9F43B9F5E99003F6A0D48D5F39C6-f8hGUhss0nh9TZdEUguypQ2O0Ztt9esIQQ4Iyu8u01E@public.gmane.org>
2015-07-13 17:36 ` Mathieu Desnoyers
2015-07-14 9:34 ` Ben Maurer
[not found] ` <5CDDBDF2D36D9F43B9F5E99003F6A0D48D5F5DA0-f8hGUhss0nh9TZdEUguypQ2O0Ztt9esIQQ4Iyu8u01E@public.gmane.org>
2015-07-16 18:08 ` Mathieu Desnoyers
[not found] ` <549319255.383.1437070088597.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-16 19:27 ` Andy Lutomirski
2015-07-17 10:21 ` Ondřej Bílka
2015-07-17 15:53 ` Andy Lutomirski
[not found] ` <CALCETrWEKE=mow3vVh7C4r8CuGy_d5VOEz7KkpijuR5cpBfFtg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-17 18:48 ` Linus Torvalds
[not found] ` <CA+55aFz-VBnEKh0SPKgu8xV5=Zb+=6odybVUDoOYOknshbcFJA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-17 18:55 ` Andy Lutomirski
[not found] ` <CALCETrVNcLpZVATHOs-gZR9AMUSW_ScvXW_0oY=OnFHXXHLdaA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-17 19:11 ` Linus Torvalds
2015-07-17 23:28 ` Ondřej Bílka
2015-07-17 23:33 ` Andy Lutomirski
2015-07-18 10:35 ` Ondřej Bílka
[not found] ` <CALCETrVY=kjeA_4pazy3BL+ekfcV6WHKw8e3z-LBxx_uP1bw2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-20 8:35 ` Florian Weimer
[not found] ` <55ACB2DC.5010503-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-07-20 15:31 ` Andy Lutomirski
[not found] ` <CALCETrV9Vp5UUOb3e_R5tphyE-urBgTwQR2pFWUOOFnHqWXHKQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-20 15:32 ` Florian Weimer
[not found] ` <55AD14A4.6030101-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-07-20 17:41 ` Andy Lutomirski
[not found] ` <CALCETrUx6wFxmz+9TyW5bNgaMN0q180G8y9YOyq_D41sdhFaRQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-20 20:07 ` josh-iaAMLnmF4UmaiuxdJuQwMA
2015-07-21 7:55 ` Florian Weimer
[not found] ` <CA+55aFzMJkzydXb7uVv1iSUnp=539d43ghQaonGdzMoF7QLZBA@mail.gmail.com>
[not found] ` <CA+55aFzMJkzydXb7uVv1iSUnp=539d43ghQaonGdzMoF7QLZBA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-20 21:09 ` Andy Lutomirski
2015-07-20 22:39 ` Linus Torvalds
[not found] ` <CA+55aFwLZLeeN7UN82dyt=emQcNBc8qZPJAw5iqtAbBwFA7FPQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-21 0:25 ` Mathieu Desnoyers
[not found] ` <2010227315.699.1437438300542.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-21 7:30 ` Ondřej Bílka
2015-07-21 12:58 ` Mathieu Desnoyers
[not found] ` <894137397.137.1437483493715.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-21 15:16 ` Ondřej Bílka
2015-07-21 17:45 ` Mathieu Desnoyers
[not found] ` <1350114812.1035.1437500726799.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-21 18:00 ` Ondřej Bílka
2015-07-21 18:18 ` Mathieu Desnoyers
[not found] ` <2028561497.1088.1437502683664.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-22 7:53 ` Ondřej Bílka
2015-07-21 8:01 ` Florian Weimer
2015-07-20 13:18 ` Konstantin Khlebnikov
2015-07-18 7:34 ` Rich Felker
[not found] ` <20150718073433.GH1173-C3MtFaGISjmo6RMmaWD+6Sb1p8zYI1N1@public.gmane.org>
2015-07-18 10:51 ` Ondřej Bílka
2015-07-18 9:47 ` Florian Weimer
[not found] ` <587954201.31.1436808992876.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-17 10:58 ` Ondřej Bílka
2015-07-17 16:03 ` Mathieu Desnoyers
[not found] ` <626545401.1010.1437149010438.JavaMail.zimbra-vg+e7yoeK/dWk0Htik3J/w@public.gmane.org>
2015-07-17 22:43 ` Ondřej Bílka
2015-07-18 2:43 ` Mathieu Desnoyers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1050138282.1065.1436801252018.JavaMail.zimbra@efficios.com \
--to=mathieu.desnoyers-vg+e7yoek/dwk0htik3j/w@public.gmane.org \
--cc=ahh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
--cc=akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
--cc=bmaurer-b10kYP2dOMg@public.gmane.org \
--cc=josh-iaAMLnmF4UmaiuxdJuQwMA@public.gmane.org \
--cc=laijs-BthXqXjhjHXQFUHtdCDX3A@public.gmane.org \
--cc=linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=luto-kltTT9wpgjJwATOyAt5JVQ@public.gmane.org \
--cc=mingo-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
--cc=paulmck-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org \
--cc=peterz-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org \
--cc=pjt-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
--cc=rostedt-nx8X9YLhiw1AfugRpC6u6w@public.gmane.org \
--cc=torvalds-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.