Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp714970yba; Wed, 24 Apr 2019 08:27:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqzZPOS4t3wW/chPsh/Pa0jjpKvPsefmlb6hkmKXnvAEMcxSDftf/c1K1jzHyOoDGdTfnhhl X-Received: by 2002:a63:3d4c:: with SMTP id k73mr31302618pga.154.1556119646033; Wed, 24 Apr 2019 08:27:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556119646; cv=none; d=google.com; s=arc-20160816; b=OjjVGNhQIwQaQiLxClvTCknqgw0XEyG1+ADbKd+UGiRaKKDc6AyjAV7CR3hgdBn6jm tZCen/mKacWUJYl4U+Q0Xh4mIwlb2mDQC3gIGF1XnvFiQCPrwl0oMgBHWOR4/tk2NBfE 4HNK0kpYcCroowBr96xJnSjvHtKCDE2pLZfg6o07u/WhTE9Q8oidpU8n3nCOSaoVkNO3 8WhzdtpKV4k0WfONC59dxVe3NS04mf1bQEMJs4SSpkTGAFWijtABw1tyxdt/P+r3hxpI IyfN1KHki7HTZ293lTuzLWBMKgK8U+03TRfyIEKrN/j3HrBWY5LTzjtilD1OkYSykbal VImA== 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=CUoFbCvjdu5aTIEDIFoQGhpcp1T3MBcIhWXnIMH56Qg=; b=00KxwVeYbj+ii/c+0ZG1jnF8SS0G7nkArvsmY8lonLtYj7v+Uj8azHJIxrWNqhDre1 Z+pl2OUCwZ9M/AzubKc4yFDTzBHHSWxqiyMKnSJpQUIcLMZaie48if9kdIS8fDSAM0ZI ix10yI1IT5B0UswYInaS0AhKCOxPtwvKwAn2MNMRoH33IMR/KnUVQpdRv1vzTLSgQgoM P5el68DX2Dc+mKpxBjqGbYeXE2Un1DRGYhp//cx7Yo4z/qjfNApyKWWnQJQ+QiEMoKOE vmmmGqRgX0qCnSqb3UTTRbDgOlp7KwN1UYsP99Kk1qibfKXm0YPFrNiCTNB1pfk00Fzy Vyrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=R1sqUIpJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id p2si17583130pgk.326.2019.04.24.08.27.10; Wed, 24 Apr 2019 08:27:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=R1sqUIpJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1731281AbfDXPZp (ORCPT + 99 others); Wed, 24 Apr 2019 11:25:45 -0400 Received: from mail.efficios.com ([167.114.142.138]:57938 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731147AbfDXPZj (ORCPT ); Wed, 24 Apr 2019 11:25:39 -0400 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 67AAE1D9187; Wed, 24 Apr 2019 11:25:37 -0400 (EDT) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id n8vos7CosmwW; Wed, 24 Apr 2019 11:25:36 -0400 (EDT) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id A4C2D1D9176; Wed, 24 Apr 2019 11:25:36 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com A4C2D1D9176 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1556119536; bh=CUoFbCvjdu5aTIEDIFoQGhpcp1T3MBcIhWXnIMH56Qg=; h=From:To:Date:Message-Id; b=R1sqUIpJ7+JGZ1QXp1UN/cYtKLUJXMfEpaMpgWhnVZRGahrlSAa/ibfoCc3DR7C9F OZsLlZCYza35hnc/i4+LHQhEYIFCLvYxm4ab2efTLf8RPNSk52JicGHhaQIuggyPw7 CDRm3syOsWiqgkLeZXvPcUj5mN6YeWLiBfTm98q512HGDVLwVZnBw5axgWLABm0/8S n9n3vgC8r4aflcYxpz5GZAJknUGX0kxJu7LTKwvc6yE5hdmBGNJuOw/Iq0HCtG9OyC T8swY9E1qbCdOQAt4Hsy/f7+F2Z7aryRJdufHnLKhceKW8ask+I/yuf3JsRcbErgjk OBcFWwbFZwxLg== 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 JuT1lGuHm6bW; Wed, 24 Apr 2019 11:25:36 -0400 (EDT) Received: from thinkos.internal.efficios.com (192-222-157-41.qc.cable.ebox.net [192.222.157.41]) by mail.efficios.com (Postfix) with ESMTPSA id 8CC561D9154; Wed, 24 Apr 2019 11:25:35 -0400 (EDT) From: Mathieu Desnoyers To: Peter Zijlstra , "Paul E . McKenney" , Boqun Feng Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Thomas Gleixner , Andy Lutomirski , Dave Watson , Paul Turner , Andrew Morton , Russell King , Ingo Molnar , "H . Peter Anvin" , Andi Kleen , Chris Lameter , Ben Maurer , Steven Rostedt , Josh Triplett , Linus Torvalds , Catalin Marinas , Will Deacon , Michael Kerrisk , Joel Fernandes , Shuah Khan , Mathieu Desnoyers , Carlos O'Donell , Florian Weimer , Joseph Myers , Szabolcs Nagy Subject: [RFC PATCH for 5.2 03/10] rseq/selftests: Use __rseq_handled symbol to coexist with glibc Date: Wed, 24 Apr 2019 11:24:55 -0400 Message-Id: <20190424152502.14246-4-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190424152502.14246-1-mathieu.desnoyers@efficios.com> References: <20190424152502.14246-1-mathieu.desnoyers@efficios.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to integrate rseq into user-space applications, expose a __rseq_handled symbol so many rseq users can be linked into the same application (e.g. librseq and glibc). The __rseq_refcount TLS variable is static to the librseq library. It ensures that rseq syscall registration/unregistration happens only for the most early/late caller to rseq_{,un}register_current_thread for each thread, thus ensuring that rseq is registered across the lifetime of all rseq users for a given thread. Signed-off-by: Mathieu Desnoyers CC: Shuah Khan 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: linux-api@vger.kernel.org --- tools/testing/selftests/rseq/rseq.c | 55 +++++++++++++++++++++++++++++++------ tools/testing/selftests/rseq/rseq.h | 1 + 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/rseq/rseq.c b/tools/testing/selftests/rseq/rseq.c index 4847e97ed049..d44cc17ad929 100644 --- a/tools/testing/selftests/rseq/rseq.c +++ b/tools/testing/selftests/rseq/rseq.c @@ -25,18 +25,27 @@ #include #include #include +#include #include "rseq.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -__attribute__((tls_model("initial-exec"))) __thread -volatile struct rseq __rseq_abi = { +__thread volatile struct rseq __rseq_abi = { .cpu_id = RSEQ_CPU_ID_UNINITIALIZED, }; -static __attribute__((tls_model("initial-exec"))) __thread -volatile int refcount; +/* + * Shared with other libraries. This library may take rseq ownership if it is + * still 0 when executing the library constructor. Set to 1 by library + * constructor when handling rseq. Set to 0 in destructor if handling rseq. + */ +int __rseq_handled; + +/* Whether this library have ownership of rseq registration. */ +static int rseq_ownership; + +static __thread volatile uint32_t __rseq_refcount; static void signal_off_save(sigset_t *oldset) { @@ -69,8 +78,14 @@ int rseq_register_current_thread(void) int rc, ret = 0; sigset_t oldset; + if (!rseq_ownership) + return 0; signal_off_save(&oldset); - if (refcount++) + if (__rseq_refcount == UINT_MAX) { + ret = -1; + goto end; + } + if (__rseq_refcount++) goto end; rc = sys_rseq(&__rseq_abi, sizeof(struct rseq), 0, RSEQ_SIG); if (!rc) { @@ -78,9 +93,9 @@ int rseq_register_current_thread(void) goto end; } if (errno != EBUSY) - __rseq_abi.cpu_id = -2; + __rseq_abi.cpu_id = RSEQ_CPU_ID_REGISTRATION_FAILED; ret = -1; - refcount--; + __rseq_lib_abi.refcount--; end: signal_restore(oldset); return ret; @@ -91,13 +106,20 @@ int rseq_unregister_current_thread(void) int rc, ret = 0; sigset_t oldset; + if (!rseq_ownership) + return 0; signal_off_save(&oldset); - if (--refcount) + if (!__rseq_refcount) { + ret = -1; + goto end; + } + if (--__rseq_refcount) goto end; rc = sys_rseq(&__rseq_abi, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, RSEQ_SIG); if (!rc) goto end; + __rseq_refcount = 1; ret = -1; end: signal_restore(oldset); @@ -115,3 +137,20 @@ int32_t rseq_fallback_current_cpu(void) } return cpu; } + +void __attribute__((constructor)) rseq_init(void) +{ + /* Check whether rseq is handled by another library. */ + if (__rseq_handled) + return; + __rseq_handled = 1; + rseq_ownership = 1; +} + +void __attribute__((destructor)) rseq_fini(void) +{ + if (!rseq_ownership) + return; + __rseq_handled = 0; + rseq_ownership = 0; +} diff --git a/tools/testing/selftests/rseq/rseq.h b/tools/testing/selftests/rseq/rseq.h index c72eb70f9b52..26348e2c44f3 100644 --- a/tools/testing/selftests/rseq/rseq.h +++ b/tools/testing/selftests/rseq/rseq.h @@ -45,6 +45,7 @@ #endif extern __thread volatile struct rseq __rseq_abi; +extern int __rseq_handled; #define rseq_likely(x) __builtin_expect(!!(x), 1) #define rseq_unlikely(x) __builtin_expect(!!(x), 0) -- 2.11.0