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=-7.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,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 0FFC5C433DF for ; Mon, 22 Jun 2020 18:08:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB71520656 for ; Mon, 22 Jun 2020 18:08:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b="N3ALkBp9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729910AbgFVSIM (ORCPT ); Mon, 22 Jun 2020 14:08:12 -0400 Received: from mail.efficios.com ([167.114.26.124]:35224 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729860AbgFVSIL (ORCPT ); Mon, 22 Jun 2020 14:08:11 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 3D7922B6F2D; Mon, 22 Jun 2020 14:08:10 -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 ZXtbc0lA3bhQ; Mon, 22 Jun 2020 14:08:10 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id EC6AF2B6ED8; Mon, 22 Jun 2020 14:08:09 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com EC6AF2B6ED8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1592849289; bh=yRiGtquXYPCKnEyIARANnhxrsJ53NZMKmbMlMj8D3uI=; h=From:To:Date:Message-Id; b=N3ALkBp9RQZIFWtwiMynZtPjd5HMR4YvsfQd5dMx7+3BByTANsJvoOnfcy4t4k6mn 4FX2u5Jb+xUmHHKn+SLffg+HzkmktZbIvbJWTy70pTjKnu8qPc/R8IBG8zkDYAYAaa FiRvuXeP7ZtXLHJQXUcwNLbkzfOqZkFyxlo17UCFIWOhjXhRLjvHxNXB7W5XljCk9+ ny0jcbAXT8ft0Yf/5ijh5ajg2l7wybGLFtVTIVifBw48K87tXc72DjrYRkWbaT/ZWm 6snoEqzILBd9C8gcQhwiSAT60Q0V/YyJLaFWU9dtOO+W9S7Ae6F+0+CkbdEt+MQ1zg Kn6NjoxJWL3yQ== 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 7WF-2it4VbFd; Mon, 22 Jun 2020 14:08:09 -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 9B8562B6F27; Mon, 22 Jun 2020 14:08:09 -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 2/3] Linux: Use rseq in sched_getcpu if available (v9) Date: Mon, 22 Jun 2020 14:08:02 -0400 Message-Id: <20200622180803.1449-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200622180803.1449-1-mathieu.desnoyers@efficios.com> References: <20200622180803.1449-1-mathieu.desnoyers@efficios.com> Sender: linux-api-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-api@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. Changes since v7: - Fix incorrect merge with commit d0def09ff6 ("linux: Fix vDSO macros build with time64 interfaces") Changes since v8: - Update patch title. - Add /* RSEQ_SIG */ for #else and #endif. --- sysdeps/unix/sysv/linux/sched_getcpu.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c index c019cfb3cf..c0f992e056 100644 --- a/sysdeps/unix/sysv/linux/sched_getcpu.c +++ b/sysdeps/unix/sysv/linux/sched_getcpu.c @@ -18,10 +18,12 @@ #include #include #include +#include #include +#include -int -sched_getcpu (void) +static int +vsyscall_sched_getcpu (void) { unsigned int cpu; int r = -1; @@ -32,3 +34,19 @@ sched_getcpu (void) #endif return r == -1 ? r : cpu; } + +#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 /* RSEQ_SIG */ +int +sched_getcpu (void) +{ + return vsyscall_sched_getcpu (); +} +#endif /* RSEQ_SIG */ -- 2.17.1