From patchwork Mon Feb 26 21:03:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 129719 Delivered-To: patch@linaro.org Received: by 10.80.172.228 with SMTP id x91csp4292608edc; Mon, 26 Feb 2018 13:07:02 -0800 (PST) X-Google-Smtp-Source: AG47ELtWWqbn2WP39ea0IwMFtvQzLavwXoUpgcdlQ9usxlAH8+JOk/2eeh2aOyS34l5ZBzk+UJ3E X-Received: by 10.101.74.74 with SMTP id a10mr3610079pgu.32.1519679222800; Mon, 26 Feb 2018 13:07:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519679222; cv=none; d=google.com; s=arc-20160816; b=tAlIJ+YDRX/QGt80Yv3XUGQBp5IV9yH0/pmRYe6QHZJT67h6p/HtOjy/ZZ1Q9mMuOd fcteMMdml/daUZC/io0Po54UP9oAFixKSBrg91dpuVIIxrimnrbo1QANWDNaQkYH/exS 6shbjvqllKWF62F08nyzahlj58Gco+TauQ12nrq1hByyH1L3sy4HAfnCMQVO6432J6gd vVH6CLZ5GJN4MTH0p3vJOPZgc7Tcy7VuwtnXghBss9b6cOIdcp5XA+KdJSgDFvDqRdpH HTSgv0OP7WzU85hWZS2czvWeJEfanAfR1EoeGVnFmsDrCHJjWwQ3QJz7zPq6F2cSn9VX t9wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=fDjQ5OUa2soFgyRdhn2SoufZT3BrNbhlmpMoS+lUuNk=; b=wXO+dWanusaPOKlN0eUgaUzZgVu6pV3nQcBXt5EQqboHw1hSCO2iT2c3mnMmtPZet9 xmwM1UkOQYLqAg72G7t0s+0gKE4SYeWZYx3IFxJ0bT+MJpkf0mFg5b+oKbm57jsaXVFo dgFJvc0YbSwvkNszpzj1XWlXpLWixbvENrTaK5yTo8XO4y0UgxIODn2Ile1SrsqM2oXc YWWczQkUlPBJHSJonSgIH3BSSTR1NN17Ph8k3lDQ7dAjmwG90pDFuIioecsIltuExC6f SlHj0yFnHtznh2NOJ/kkUtejrxH+3B6C9NGdVyvJnNfUeqsuVrfIVJaqUFiwGmJP3tu8 /PAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=LZ2ypxjv; spf=pass (google.com: domain of libc-alpha-return-90634-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-90634-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id v12si5925012pgs.356.2018.02.26.13.07.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Feb 2018 13:07:02 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-90634-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=LZ2ypxjv; spf=pass (google.com: domain of libc-alpha-return-90634-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-90634-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=nPYRv3s3dzLNbesPslWGqwf30VvjNJo B6Q9P5FF9dHJXaP8ZlMpxUJdKYmLoX/0/SvAnCogV9dz7e32to26t2LVfw9wDBMD LMu0kQ9+vw2caL9caCi7H0xF0TLGNSnh/X55dpee+hRQSwDHXyqZgfjRvj57I0/m x7NaAd3v5az8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=sG+BtDdFQulwUvgiDxwDw492IQM=; b=LZ2yp xjvEtvFQTZwOw+rzOh80l/7QZoXjEE1EXz18rNImm2Yw7fy1m6Neco+5DhFddRf6 z8OCUlXNGjR7gyavLXrmd9WQIdsLxIm1vMz45E3yGtblYfNjbtCnKwsdhApPjxJ4 m9fB0hRXw1ct/0LStKBqhItKHrTQhOWYzZ29V8= Received: (qmail 79570 invoked by alias); 26 Feb 2018 21:04:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 77816 invoked by uid 89); 26 Feb 2018 21:04:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:advance, sk:ADVANCE X-HELO: mail-qt0-f193.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=fDjQ5OUa2soFgyRdhn2SoufZT3BrNbhlmpMoS+lUuNk=; b=SVkxrPDkRCkHpE/FkXARXvgEZ2oko7YZ8wQCYqx9QCHJ3rDaa6s8IqQxY1e34fcEiN DpKb0XyuQaQDGbCY+98gDQcJRI7ruQjxYR2OD4v9ZiheIav3EVk4rc1mS+QYs217wGsC Tm4k85hL7z2nWnRdWIMUcAWyUKmxNlmgFToeaMODJqCo+jTiTrqoY1ljTASlmaZfSbOm RQr4y81LWpJtjw8CeJ7pVQWH57U/lOF/xTdk0/U3qUfiPhbLVfdYjrr7bUUx49wLKuGb fy8Cja7LGRis3YO/Kg3dz2xT7OqRPx45rG36Ge2UbBfLbq6LkaqmVAaKSU+68MIExXjc f8ew== X-Gm-Message-State: APf1xPCRGB0T5PHIVluATj2+kbjxF0+T1I06xklzrcNbQ5fPsT1e8WVd ZqxvqC7qzmNy4TCtcQ6m0yBfaXeIwwU= X-Received: by 10.200.25.134 with SMTP id u6mr18688823qtj.221.1519679048288; Mon, 26 Feb 2018 13:04:08 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 16/21] nptl: sparc: Fix Race conditions in pthread cancellation (BZ#12683) Date: Mon, 26 Feb 2018 18:03:31 -0300 Message-Id: <1519679016-12241-17-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1519679016-12241-1-git-send-email-adhemerval.zanella@linaro.org> References: <1519679016-12241-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds the sparc modifications required for the BZ#12683. It basically adds the required ucontext_get_pc function, cancellable syscall wrappers, and a fix for pause. Sparc requires arch-specific syscall_cancel implementation because INLINE_SYSCALL_NCS uses the __SYSCALL_STRING (defined different for sparc32 and sparc64) and it issues additional instructions after the syscall one to check the resulting error code. When used in the default syscall_cancel.c implementation the label __syscall_cancel_arch_end is not placed just after the syscall as expected. Both 32 and 64 bits version were based on default C version built with GCC 6.1. Also, different than other architectures, SPARC passes the sigcontext_t struct pointer as third argument in the signal handler set with SA_SIGINFO (some info at [1]) for 64 bits and the pt_regs in 32 bits. >From Linux code: * arch/sparc/kernel/signal_64.c 428 /* 3. signal handler back-trampoline and parameters */ 429 regs->u_regs[UREG_FP] = ((unsigned long) sf) - STACK_BIAS; 430 regs->u_regs[UREG_I0] = ksig->sig; 431 regs->u_regs[UREG_I1] = (unsigned long) &sf->info; 432 433 /* The sigcontext is passed in this way because of how it 434 * is defined in GLIBC's /usr/include/bits/sigcontext.h 435 * for sparc64. It includes the 128 bytes of siginfo_t. 436 */ 437 regs->u_regs[UREG_I2] = (unsigned long) &sf->info; * arch/sparc/kernel/signal_32.c: 392 regs->u_regs[UREG_FP] = (unsigned long) sf; 393 regs->u_regs[UREG_I0] = ksig->sig; 394 regs->u_regs[UREG_I1] = (unsigned long) &sf->info; 395 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs; 396 397 regs->pc = (unsigned long) ksig->ka.sa.sa_handler; 398 regs->npc = (regs->pc + 4); So to access the signal mask in the signal frame, a arch-specific ucontext_get_mask is defined which obtain the sa_mask from the context. Checked on a SPARC T5 for sparc64-linux-gnu and sparcv9-linux-gnu. [BZ #12683] * sysdeps/unix/sysv/linux/sparc/sparc32/sigcontextinfo.h (ucontext_get_pc): New function. * sysdeps/unix/sysv/linux/sparc/sparc64/sigcontextinfo.h (ucontext_get_pc): Likewise. * sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S: New file. * sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S: Likwise. * sysdeps/unix/sysv/linux/sparc/sparc64/pause.c: New file. [1] https://www.spinics.net/lists/sparclinux/msg05037.html --- ChangeLog | 9 +++ sysdeps/unix/sysv/linux/sparc/lowlevellock.h | 2 +- .../unix/sysv/linux/sparc/sparc32/sigcontextinfo.h | 52 +++++++++++++++ .../unix/sysv/linux/sparc/sparc32/syscall_cancel.S | 74 ++++++++++++++++++++++ sysdeps/unix/sysv/linux/sparc/sparc64/pause.c | 25 ++++++++ .../unix/sysv/linux/sparc/sparc64/sigcontextinfo.h | 40 ++++++++++++ .../unix/sysv/linux/sparc/sparc64/syscall_cancel.S | 74 ++++++++++++++++++++++ 7 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/pause.c create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S -- 2.7.4 diff --git a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h index e2c0b2a..ed2e746 100644 --- a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h +++ b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h @@ -117,7 +117,7 @@ __lll_timedlock (int *futex, const struct timespec *abstime, int private) { \ __typeof (tid) __tid; \ while ((__tid = (tid)) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED); \ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED); \ } \ while (0) diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/sigcontextinfo.h b/sysdeps/unix/sysv/linux/sparc/sparc32/sigcontextinfo.h index 91233b8..4114035 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/sigcontextinfo.h +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/sigcontextinfo.h @@ -16,6 +16,11 @@ License along with the GNU C Library; if not, see . */ +#ifndef _SIGCONTEXTINFO_H +#define _SIGCONTEXTINFO_H + +#include + #define SIGCONTEXT struct sigcontext * #define SIGCONTEXT_EXTRA_ARGS #define GET_PC(__ctx) ((void *) ((__ctx)->si_regs.pc)) @@ -29,3 +34,50 @@ #define GET_FRAME(__ctx) ADVANCE_STACK_FRAME (GET_STACK(__ctx)) #define CALL_SIGHANDLER(handler, signo, ctx) \ (handler)((signo), SIGCONTEXT_EXTRA_ARGS (ctx)) + +/* Different that other architectures, SPARC32 pass a pt_regs (or pt_regs32 + in 32 bits compat mode) struct pointer as third argument for sa_sigaction + handler with SA_SIGINFO. + + Also current sparc32 rt signal frame layout is: + + field | size + ---------------------------------------| ---- + struct rt_signal_frame { | + struct sparc_stackf ss; | 96 + siginfo_t info; | 128 + struct pt_regs regs; | 80 + sigset_t mask; | 128 + __siginfo_fpu_t __user *fpu_save; | 4 + unsigned int insns[2]; | 8 + stack_t stack; | 12 + unsigned int extra_size; | 4 + __siginfo_rwin_t __user *rwin_save; | 4 + }; + + So to obtain a pointer to signal mask based on address of pt_regs + we need to add 208. */ + +struct pt_regs32 +{ + unsigned int psr; + unsigned int pc; + unsigned int npc; + unsigned int y; + unsigned int u_regs[16]; +}; + +static inline uintptr_t +ucontext_get_pc (struct pt_regs32 *regs) +{ + return regs->pc; +} + +static inline sigset_t * +ucontext_get_mask (const void *ctx) +{ + return (sigset_t *)((uintptr_t)ctx + 208); +} +#define UCONTEXT_SIGMASK(ctx) ucontext_get_mask (ctx) + +#endif /* _SIGCONTEXTINFO_H */ diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S b/sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S new file mode 100644 index 0000000..c06f9d1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S @@ -0,0 +1,74 @@ +/* Cancellable syscall wrapper. Linux/sparc32 version. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* long int __syscall_cancel_arch (int *cancelhandling, + long int nr, + long int arg1, + long int arg2, + long int arg3, + long int arg4, + long int arg5, + long int arg6) */ + +ENTRY (__syscall_cancel_arch) + save %sp, -104, %sp + + cfi_window_save + cfi_register (15, 31) + cfi_def_cfa_register (30) + + .globl __syscall_cancel_arch_start +__syscall_cancel_arch_start: + + /* if (*cancelhandling & CANCELED_BITMASK) + __syscall_do_cancel() */ + ld [%i0], %g2 + andcc %g2, 4, %g0 + bne,pn %icc, 2f + + /* Issue a 6 argument syscall. */ + mov %i1, %g1 + mov %i2, %o0 + mov %i3, %o1 + mov %i4, %o2 + mov %i5, %o3 + ld [%fp+92], %o4 + ld [%fp+96], %o5 + ta 0x10 + + .globl __syscall_cancel_arch_end +__syscall_cancel_arch_end: + bcc 1f + mov 0,%g1 + sub %g0, %o0, %o0 + mov 1, %g1 + +1: + mov %o0, %i0 + return %i7+8 + nop + +2: + call __syscall_do_cancel, 0 + nop + nop + +END (__syscall_cancel_arch) +libc_hidden_def (__syscall_cancel_arch) diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/pause.c b/sysdeps/unix/sysv/linux/sparc/sparc64/pause.c new file mode 100644 index 0000000..4a0cf4d --- /dev/null +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/pause.c @@ -0,0 +1,25 @@ +/* Linux pause syscall implementation. Linux/sparc64. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* On sparc interrupted pause syscall returns with a PC indicating a + side-effect and this deviates from other architectures. Fall back to + ppool implementation. */ +#undef __NR_pause +#include diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/sigcontextinfo.h b/sysdeps/unix/sysv/linux/sparc/sparc64/sigcontextinfo.h index c9d2685..dc04c42 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/sigcontextinfo.h +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/sigcontextinfo.h @@ -16,6 +16,11 @@ License along with the GNU C Library; if not, see . */ +#ifndef _SIGCONTEXTINFO_H +#define _SIGCONTEXTINFO_H + +#include + #ifndef STACK_BIAS #define STACK_BIAS 2047 #endif @@ -29,3 +34,38 @@ #define GET_FRAME(__ctx) ADVANCE_STACK_FRAME (GET_STACK (__ctx)) #define CALL_SIGHANDLER(handler, signo, ctx) \ (handler)((signo), SIGCONTEXT_EXTRA_ARGS (ctx)) + +/* Different that other architectures, SPARC64 pass a sigcontext_t struct + pointer in third argument for sa_sigaction handler with SA_SIGINFO. + + Also current sparc64 rt signal frame layout is: + + field | size + ---------------------------------------| ---- + struct rt_signal_frame { | + struct sparc_stackf ss; | 192 + siginfo_t info; | 128 + struct pt_regs regs; | 160 + __siginfo_fpu_t __user *fpu_save; | 8 + stack_t stack; | 24 + sigset_t mask; | 128 + __siginfo_rwin_t *rwin_save; | 8 + }; + + So to obtain a pointer to signal mask based on address of info + we need to add 320. */ + +static inline uintptr_t +ucontext_get_pc (const struct sigcontext *sigctx) +{ + return sigctx->sigc_regs.tpc; +} + +static inline sigset_t * +ucontext_get_mask (const void *ctx) +{ + return (sigset_t *)((uintptr_t)ctx + 320); +} +#define UCONTEXT_SIGMASK(ctx) ucontext_get_mask (ctx) + +#endif /* _SIGCONTEXTINFO_H */ diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S b/sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S new file mode 100644 index 0000000..f3eef78 --- /dev/null +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S @@ -0,0 +1,74 @@ +/* Cancellable syscall wrapper. Linux/sparc64 version. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* long int __syscall_cancel_arch (int *cancelhandling, + long int nr, + long int arg1, + long int arg2, + long int arg3, + long int arg4, + long int arg5, + long int arg6) */ + +ENTRY (__syscall_cancel_arch) + save %sp, -176, %sp + + cfi_window_save + cfi_register (15, 31) + cfi_def_cfa_register (30) + + .globl __syscall_cancel_arch_start +__syscall_cancel_arch_start: + + /* if (*cancelhandling & CANCELED_BITMASK) + __syscall_do_cancel() */ + lduw [%i0], %g1 + andcc %g1, 4, %g0 + bne,pn %xcc, 2f + + /* Issue a 6 argument syscall. */ + mov %i1, %g1 + mov %i2, %o0 + mov %i3, %o1 + mov %i4, %o2 + mov %i5, %o3 + ldx [%fp + STACK_BIAS + 176], %o4 + ldx [%fp + STACK_BIAS + 184], %o5 + ta 0x6d + + .global __syscall_cancel_arch_end +__syscall_cancel_arch_end: + + bcc,pt %xcc, 1f + mov 0, %g1 + sub %g0, %o0, %o0 + mov 1, %g1 +1: + mov %o0, %i0 + return %i7+8 + nop + +2: + call __syscall_do_cancel, 0 + nop + nop + +END (__syscall_cancel_arch) +libc_hidden_def (__syscall_cancel_arch)