Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp184475ybz; Thu, 30 Apr 2020 19:16:39 -0700 (PDT) X-Google-Smtp-Source: APiQypK0gDdtZTJaDAVruUySAI4iS/G1sDfTlVJvguKS1TsymOEmDEe7Nu1WcMamdi+CdkxM3rhS X-Received: by 2002:a17:906:f74e:: with SMTP id jp14mr1357224ejb.15.1588299398798; Thu, 30 Apr 2020 19:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588299398; cv=none; d=google.com; s=arc-20160816; b=gPeeNMCw2rz7QZ3gK0cuxWJYXidswDAg0V7WEcTtzpDTdfMu9DkE1pOumOusTpUF2P hhvwCVjdzFawtReRFYuxgKBGKBaa2EHn9QK8JF6mil7PR4wcx00BpuEg6PZ/i6QlQpyq NVRK1NCzPytlVgGUJSxk5T7vvs9kzdpVMSHGLuxWhcQdL24HbpMLtfeYvw0VAjZ5DCBu L3x7PdhFlCu2T4amA4ImbNpO2fjQi9cLf9BB/nonJa+mZlxVtHAtcVvYjWyRtOuKuSM3 BjqHg/+RkOtf8ulkCTCEQdnpFMdV1Mlx2GZ5N3NK8xuM9PhHj9rQjhrOxGgSWaGaP5po R7qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=0smAkb6cp0p+RuLi71ihxVXx+NcGlOLwapAddILSwQ4=; b=gT3lkfemm0uPnR4T0XfLCozWl8Yn1hVEDJnCqFjS3HNeEoXQA8aA82HyvK4cvFK4du F3aGukN8tAQ9B1uCLVhF1wrcY318fQwUSeJaa5PshoXLpcHScsfP9Ql1rwCJGTjH5GZ0 3bczOE2efQsq4vhMi4FpSR1ovsinnUR9FRJYHGx0oqvrH/bkzjZnLHxaKvOB5BgJCTiY 1hgTMmyaFTC++/mVHEZX6vTto+ASQqZ1rh4tnDEQN8RzpD9mnbNzcYT2appXP+8nKJo/ cV32e9+5Ki7m1RkcKMGGCeKm8viENBnBGdNdiMHhm9a4/F77VFLNfnxb2RLaeTK5wnJi LZvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=uiYrwNaL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rh26si939975ejb.81.2020.04.30.19.16.15; Thu, 30 Apr 2020 19:16:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=uiYrwNaL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728072AbgEACOx (ORCPT + 99 others); Thu, 30 Apr 2020 22:14:53 -0400 Received: from mail.efficios.com ([167.114.26.124]:37534 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728029AbgEACOx (ORCPT ); Thu, 30 Apr 2020 22:14:53 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id A11D0294168; Thu, 30 Apr 2020 22:14:51 -0400 (EDT) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id pfpxW8W8kRsn; Thu, 30 Apr 2020 22:14:51 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 3D69A294595; Thu, 30 Apr 2020 22:14:51 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 3D69A294595 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1588299291; bh=0smAkb6cp0p+RuLi71ihxVXx+NcGlOLwapAddILSwQ4=; h=From:To:Date:Message-Id; b=uiYrwNaLaQSi0yvBcpghTE8u0RKlmi9zj2hl+AbLfNKVr3ROEErLC4GvCmY6yd/YP 0RHkDQKgl5PKy3TFakb0sn0FTUhTQ5lzwVeE19PW2Zc3Hyhe0SzvFiyJwkkIiNPxME GT1NSygDlfIEASsCZi0bvXmFh2A2ZIFi74onh/XN+JGc4pZgroKE47mIpGsiVO+cca K46lOKJ2r5JyHK00794VOEJ8RJ1eSwim5ixHZ4sqslzSHD5RPkr7cY42jxJCsjLadq AVXMdh0uCMTxyaxlwydthovF8KDGrAX0zC9Zf2TgJdtxXr/wf0FGa7K6rJd15Sxpor N9iXBolxuPRpQ== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id DGHr5DzkxDIs; Thu, 30 Apr 2020 22:14:51 -0400 (EDT) Received: from localhost.localdomain (192-222-181-218.qc.cable.ebox.net [192.222.181.218]) by mail.efficios.com (Postfix) with ESMTPSA id F0D44294518; Thu, 30 Apr 2020 22:14:50 -0400 (EDT) From: Mathieu Desnoyers To: Florian Weimer Cc: Carlos O'Donell , Joseph Myers , Szabolcs Nagy , libc-alpha@sourceware.org, Mathieu Desnoyers , Thomas Gleixner , Ben Maurer , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Will Deacon , Paul Turner , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH glibc 2/3] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux (v7) Date: Thu, 30 Apr 2020 22:14:38 -0400 Message-Id: <20200501021439.2456-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200501021439.2456-1-mathieu.desnoyers@efficios.com> References: <20200501021439.2456-1-mathieu.desnoyers@efficios.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When available, use the cpu_id field from __rseq_abi on Linux to implement sched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable. Benchmarks: x86-64: Intel E5-2630 v3@2.40GHz, 16-core, hyperthreading glibc sched_getcpu(): 13.7 ns (baseline) glibc sched_getcpu() using rseq: 2.5 ns (speedup: 5.5x) inline load cpuid from __rseq_abi TLS: 0.8 ns (speedup: 17.1x) CC: Carlos O'Donell CC: Florian Weimer CC: Joseph Myers CC: Szabolcs Nagy CC: Thomas Gleixner CC: Ben Maurer CC: Peter Zijlstra CC: "Paul E. McKenney" CC: Boqun Feng CC: Will Deacon CC: Paul Turner CC: libc-alpha@sourceware.org CC: linux-kernel@vger.kernel.org CC: linux-api@vger.kernel.org --- Changes since v1: - rseq is only used if both __NR_rseq and RSEQ_SIG are defined. Changes since v2: - remove duplicated __rseq_abi extern declaration. Changes since v3: - update ChangeLog. Changes since v4: - Use atomic_load_relaxed to load the __rseq_abi.cpu_id field, a consequence of the fact that __rseq_abi is not volatile anymore. - Include atomic.h which provides atomic_load_relaxed. Changes since v5: - Use __ASSUME_RSEQ to detect rseq availability. Changes since v6: - Remove use of __ASSUME_RSEQ. --- sysdeps/unix/sysv/linux/sched_getcpu.c | 27 ++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index c019cfb3cf..2269c4f2bd 100644 --- a/sysdeps/unix/sysv/linux/sched_getcpu.c +++ b/sysdeps/unix/sysv/linux/sched_getcpu.c @@ -18,10 +18,15 @@ #include #include #include +#include #include -int -sched_getcpu (void) +#ifdef HAVE_GETCPU_VSYSCALL +# define HAVE_VSYSCALL +#endif + +static int +vsyscall_sched_getcpu (void) { unsigned int cpu; int r = -1; @@ -32,3 +37,21 @@ sched_getcpu (void) #endif return r == -1 ? r : cpu; } + +#include + +#ifdef RSEQ_SIG +int +sched_getcpu (void) +{ + int cpu_id = atomic_load_relaxed (&__rseq_abi.cpu_id); + + return cpu_id >= 0 ? cpu_id : vsyscall_sched_getcpu (); +} +#else +int +sched_getcpu (void) +{ + return vsyscall_sched_getcpu (); +} +#endif -- 2.17.1