From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B9BBC33C8C for ; Mon, 6 Jan 2020 15:57:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6E632207FD for ; Mon, 6 Jan 2020 15:57:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b="uG6SHO5p" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726591AbgAFP5b (ORCPT ); Mon, 6 Jan 2020 10:57:31 -0500 Received: from mail.efficios.com ([167.114.142.138]:33170 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726296AbgAFP5a (ORCPT ); Mon, 6 Jan 2020 10:57:30 -0500 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id DEDE11EA206; Mon, 6 Jan 2020 10:57:28 -0500 (EST) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id KLvUN4v0cISR; Mon, 6 Jan 2020 10:57:27 -0500 (EST) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 983511EA1D4; Mon, 6 Jan 2020 10:57:21 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 983511EA1D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1578326241; bh=V01sGTkdCO7SqIi8D+mEDAjTS4ndQ4TnKwFDOpxXqBA=; h=From:To:Date:Message-Id; b=uG6SHO5pZWLZyzrvJ+miQCWNOw3+othcNc8D+G11B4x+s8a/NnE40NAwnCnPV9a1a nMf4dVo1cgWqrNkk2Ty+wUL0fsuLyYCrFPPOybJbz83WicNS/dAqIxx9W43qbX2Ly6 4LfJwkED7+3LD8PyOtRtVs73tO2dX1CNsir7zitNaoOWXcOH8cwoMY5Q8H3VFb2Bgp zIwjMW2T8C2G3csESuMWfzfArC9UFprrDr1lKmxmI0Gw/PfOQgRRF6n9LjH3gtxx2v s1xlFESfthQEnrMzF+E43fzxYaAeLY5kP+hqP9eQ/CE13r00TjfkoetsarAzHnVSQw GTHE4UZALHTsg== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id hQpQtE0KI3-I; Mon, 6 Jan 2020 10:57:21 -0500 (EST) Received: from localhost.localdomain (192-222-181-218.qc.cable.ebox.net [192.222.181.218]) by mail.efficios.com (Postfix) with ESMTPSA id 4D07A1EA188; Mon, 6 Jan 2020 10:57:19 -0500 (EST) From: Mathieu Desnoyers To: Carlos O'Donell Cc: Florian Weimer , Joseph Myers , Szabolcs Nagy , libc-alpha@sourceware.org, Mathieu Desnoyers , Thomas Gleixner , Ben Maurer , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Will Deacon , Dave Watson , Paul Turner , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Subject: [RFC PATCH glibc 10/13] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux (v5) Date: Mon, 6 Jan 2020 10:57:10 -0500 Message-Id: <20200106155713.397-11-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200106155713.397-1-mathieu.desnoyers@efficios.com> References: <20200106155713.397-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) * sysdeps/unix/sysv/linux/sched_getcpu.c: use rseq cpu_id TLS on Linux. Signed-off-by: Mathieu Desnoyers 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: Dave Watson 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. --- sysdeps/unix/sysv/linux/sched_getcpu.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index 65dd9fdda7..6f24c1db99 100644 --- a/sysdeps/unix/sysv/linux/sched_getcpu.c +++ b/sysdeps/unix/sysv/linux/sched_getcpu.c @@ -18,14 +18,15 @@ #include #include #include +#include #ifdef HAVE_GETCPU_VSYSCALL # define HAVE_VSYSCALL #endif #include -int -sched_getcpu (void) +static int +vsyscall_sched_getcpu (void) { #ifdef __NR_getcpu unsigned int cpu; @@ -37,3 +38,23 @@ sched_getcpu (void) return -1; #endif } + +#ifdef __NR_rseq +#include +#endif + +#if defined __NR_rseq && defined 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