Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754435AbdLNTW1 (ORCPT ); Thu, 14 Dec 2017 14:22:27 -0500 Received: from mail.efficios.com ([167.114.142.141]:59486 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754088AbdLNTW0 (ORCPT ); Thu, 14 Dec 2017 14:22:26 -0500 Date: Thu, 14 Dec 2017 19:24:38 +0000 (UTC) From: Mathieu Desnoyers To: Chris Lameter Cc: Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Andy Lutomirski , Dave Watson , linux-kernel , linux-api , Paul Turner , Andrew Morton , Russell King , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Andrew Hunter , Andi Kleen , Ben Maurer , rostedt , Josh Triplett , Linus Torvalds , Catalin Marinas , Will Deacon , Michael Kerrisk , Alexander Viro Message-ID: <1537392285.34532.1513279478488.JavaMail.zimbra@efficios.com> In-Reply-To: References: <20171214161403.30643-1-mathieu.desnoyers@efficios.com> <20171214161403.30643-3-mathieu.desnoyers@efficios.com> <12046460.34426.1513275177081.JavaMail.zimbra@efficios.com> Subject: Re: [RFC PATCH for 4.16 02/21] rseq: Introduce restartable sequences system call (v12) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.142.141] X-Mailer: Zimbra 8.7.11_GA_1854 (ZimbraWebClient - FF52 (Linux)/8.7.11_GA_1854) Thread-Topic: rseq: Introduce restartable sequences system call (v12) Thread-Index: 3+aT7SZYoK2jDFjjE2S2fPY363+vdA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3450 Lines: 94 ----- On Dec 14, 2017, at 1:50 PM, Chris Lameter cl@linux.com wrote: > On Thu, 14 Dec 2017, Mathieu Desnoyers wrote: > >> > I think the proper way to think about gs and fs on x86 is as base >> > registers. They are essentially values in registers added to the address >> > generated in an instruction. As such the approach is transferable to other >> > processor architecture. Many support base register and base register >> > relative processing. If a processor can do RMV instructions base register >> > relative then you have something similar. >> >> How would you do it on ARM32 ? > > Actually you do not really need RMV instructions. The data is cpu specific > so within a restartable sequence you would have exclusive access right? Yep. > > F.e. a increment would be > > 1. Load base register relative > 2. add 1 > 3. Store base register relative Actually, for the increment case, rseq headers provide a "add" API, which uses a "add" instruction on x86. On arm 32, it does indeed: RSEQ_ASM_STORE_RSEQ_CS(1, 3f, rseq_cs) RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f) "ldr r0, %[v]\n\t" "add r0, %[count]\n\t" /* final store */ "str r0, %[v]\n\t" > > The main overhead would be the registeration of the sequence. Registering a rseq is a single store to a TLS (in user-space), which really isn't that expensive. If we port this concept to kernel-space (as I start to understand would be your wish), then a simple pointer store to the current task_struct would suffice. > > The advantage on x86 is that you do not need a restartable sequence > since a single lockless RMV instruction can do this (this_cpu_inc f.e.) Indeed, on x86, for the specific case of counter increment, the single-instruction "add" or "inc" with a segment-selector prefix can save setting up the rseq (a pointer store), and offsetting from a base using the cpu number. If your wish is to do this at kernel level, where we have full control over the gs segment, this makes sense. I'm worried that applying this to user-space might create conflicts wrt who owns that segment selector register wrt pre-existing applications. > >> One benefit of your proposal is to lessen the number of retired instructions, >> but if we take the IPC into account, it is slower than rseq in my benchmark. >> What >> benefits do you expect from using segment selectors and non-lock-prefixed atomic >> instructions on the fast-path ? > > Ultimately I wish fast increments like done by this_cpu_inc() could be > implemented in an efficient way on non x86 platforms that do not have > cheap instructions like that. My understanding is that your focus is mainly on kernel code, right ? Or is your aim to port this_cpu_inc() to userspace as well ? Indeed, the concepts behind rseq could be ported to kernel code eventually. The immediate gain is much higher by exposing this to user-space though, given that there is no good way to perform per-cpu operations efficiently at all there, whereas kernel code can always disable preemption. > > If cmpxchg local is slower than a group of instructions to do the same > then there is an obvious question to the cpu architects why we would need > the instruction at all (aside from the fact that we do not need a > restartable sequence for these instructions). I'm not a specialist in CPU instruction scheduling, so I won't speculate on this topic. ;-) Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com