Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2980945ybt; Mon, 29 Jun 2020 12:03:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTwJan/duBx3eqVgVR5Eh4AeQKu4e7ZtXxU70CNW9fK7Vmx1b5nHMGypTsIhWxJ6FFAK/5 X-Received: by 2002:a17:907:7283:: with SMTP id dt3mr15640829ejc.195.1593457393321; Mon, 29 Jun 2020 12:03:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593457393; cv=none; d=google.com; s=arc-20160816; b=U2BAq6VE1frwr5sSL8PFlsJj4VrYtTgeP9+M18VrDp/4DgEte5JNEy9mzYy/ubHOOK h7QJ/yDMIekM2hEUDhbtyl5rkbdsZZqfctlSgYl9rXPNitE+USFbUU68EZ8+yzhXUxDh 87RsGLKGEYONk5X3VeEdqVqvVfNsOYIM9bAe0dBpH0jc0lZkiyCQz1V760is+cwmn1Ib hP7Akup1hU3BJhm7Qa3pz+NIwAbXOLIndAuQruGMRIZ3Z2OWoFw1SHw3Tdn2g3SX6DAV WteCLAUzpX5N4I1IT3XFnbO6Hf/3f+DeWm8KqENcPi/joRlhkKu/ADByE2fXeQxUf3yi bf0Q== 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=yRiGtquXYPCKnEyIARANnhxrsJ53NZMKmbMlMj8D3uI=; b=mSmp4gYQWmmws2DkhnPoqJxB6gq3z9uvEwyhlh2fHW6/GCWlUvbALGRbp69nw0hinL 6+8VeiZV7fBbQTndbQZMjooWxMhqQbsI2YfKl/vG3zyL2AGsCgYD6Oui14xgPlpuhm3f A+dA0yUSFQEGTzAhQffE2p8vdYm9Qa6OqGuUhBmpvOlNNDmKdmYCCeUYi+80Cnsnlyhv SXnCL8sJ7J50nKekUN2C1swKwKNmydTPhcQ3pKWHwWPlPTC+bXqlk6K8tawhewOEg6kT IlbfquJFXoR3/uD7mL63nQU4kj2CeRhcY3Mcz+tXhYRcgyt9SRk5/wW48NLtlzOef85k 0IDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=LfGlIR78; 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 c2si236931edq.228.2020.06.29.12.02.49; Mon, 29 Jun 2020 12:03:13 -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=LfGlIR78; 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 S1730227AbgF2TAq (ORCPT + 99 others); Mon, 29 Jun 2020 15:00:46 -0400 Received: from mail.efficios.com ([167.114.26.124]:48098 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725983AbgF2TAo (ORCPT ); Mon, 29 Jun 2020 15:00:44 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 2B8F926B41C; Mon, 29 Jun 2020 15:00:43 -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 nUImhvXRtgxZ; Mon, 29 Jun 2020 15:00:42 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id DD78326B41B; Mon, 29 Jun 2020 15:00:42 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com DD78326B41B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1593457242; bh=yRiGtquXYPCKnEyIARANnhxrsJ53NZMKmbMlMj8D3uI=; h=From:To:Date:Message-Id; b=LfGlIR78By8/8roQib/qqkAK6+yO2iNQnY5d2StV4LtIkEIZpT+Nhs31qOt5kI+GS lnkr4B7x/iU+2WBed+9UIb3q4RsR5SoedDOGm0bvdTwTMLMFWXfY0LUNh1DmTUNtea k5d2/GnBiQlBeWOqsR4y5JhhVKANFh7tuC3Cm5QQfqB2fTgBLjB2jcVI+g0siEoyBp D3XJQnp1XsO46qm6rml0sqtVf3EyRvZU2AJiWeztafJdk4p63iVy5Xz/gbMBSNrBDJ VzGv5fywOyAO4mFSXbruv9K98GYzFCL8g71QGy9yqYfIjLuODVVSF1pF2gs6nsTs9J tTrmL3K7NIjNw== 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 o4in7l92-71t; Mon, 29 Jun 2020 15:00:42 -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 553DC26B4AC; Mon, 29 Jun 2020 15:00:42 -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, 29 Jun 2020 15:00:35 -0400 Message-Id: <20200629190036.26982-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200629190036.26982-1-mathieu.desnoyers@efficios.com> References: <20200629190036.26982-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. 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