From patchwork Mon Dec 11 19:06:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 121453 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3170251qgn; Mon, 11 Dec 2017 11:10:49 -0800 (PST) X-Google-Smtp-Source: ACJfBovnXsVle+gjzuFqYa/JVedytood5X7/yS44RQry0Ri2ViXw6uwqthkeTDNRUIMV2mOUUfVx X-Received: by 10.99.113.30 with SMTP id m30mr1167278pgc.338.1513019449810; Mon, 11 Dec 2017 11:10:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513019449; cv=none; d=google.com; s=arc-20160816; b=MLJLbjObwNH9gE8aUW+lfJumVVQaxLurC4ozxonIax3VN6RfTt9nWTSH6F23U8y8+Y d6LB/pCUfZA1t0zVlqj7dRgh5+l/Lwaho3QcTub/takX6wSqFwBhue3eVamj/Rzgaurx u3tMCUUVstyZHJ8O/g3N3RcQy5I3QaNjI0kZlDUwqDAQSXf8B75rOUGNCv3pjYyYIDlZ eeK3CSviu/0KD+VlbZys7UOQK4tmmkFZ6Iw8O12+WTGb7HRuWbi0O+vUJ4F3zr6CW9Hl NeFMv4OiJ4Qv6pZFVcDHVupaWxgVyXCa/HSalbd3VXFt44Q2MCz5ePB7UuZChCp2neNy MKIQ== 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=heQivUo4VLmG5JIoUHZMi8lzTVUfI4dCjAxjx9yyf7E=; b=B5Y7VTi/tF4n6gWJi+9hBkgdIexx6oiid8iplm+M8d2l6FBUAXzfLXL4jJn0QgUH41 B3s6W4NLIMxIb7HKNKXQGcRLTtd5npyF1Q2hW8dNOnehDCQfsxtUEkbLJF//5jK4oNYj sQXNvCClZJP5F3rMmf1A3WukFRn3lVaRuahChsmzmvMZFSAzA6Rx6C54cols/WyljihB psilfYjM83B2/zZYq9FxqfNFTMUrfkrquGKSqfzOsOSh17lgu5bx6PREwUCBk4MLBLzJ MQUiIn2nWAdhKtQNu8OZ469HwhMNYx3sYd9oihI4tX7hBvQC1Vaojwj3ZAe6N+NzGh0A Flww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ShBGN2xi; spf=pass (google.com: domain of libc-alpha-return-88030-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-88030-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 n1si10348022pld.591.2017.12.11.11.10.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Dec 2017 11:10:49 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-88030-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=ShBGN2xi; spf=pass (google.com: domain of libc-alpha-return-88030-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-88030-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=yC7NzUU6+ZhLzdme9jnIzuhwrXzufZM cL2b9DxM9SfOFeq/97CdWV+keqv5U7bEYEkxpeQ7KAZ7WwUKMcmgqJIHZYjfnZ6c NHPXm29niFIHkv5sFPDZFjN4XNDIO6+Fa7MaPhWsVbt8xCxMkTadCfEXijOEM9yd IqaR5h2kX+dU= 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=GfdPKYLipFoVueBRzLHv8Uce/gU=; b=ShBGN 2xiT91PmJl+Us1m2Wt7SE1VxyVENYIs9KY59vm2TIQYt/HDaygyeo2jfSX84QnjT SDNVJzNO/cyBIeE8RRxF4w5DYg9Xqjl+0EFYKDxaCROYNQKnjtkVfE80Khv38ZPO ZavPXlV0S2mJeBbpsrpq210I11GQS7dsfx09p4= Received: (qmail 42843 invoked by alias); 11 Dec 2017 19:07:38 -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 42764 invoked by uid 89); 11 Dec 2017 19:07:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 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, xcc X-HELO: mail-qt0-f195.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=heQivUo4VLmG5JIoUHZMi8lzTVUfI4dCjAxjx9yyf7E=; b=GXItU9wboXSSR7XvvbcgVkd2+RTCKIkRuLNTrEari3Wel7+4NF0TmQrDCorVlzvKko s9Jz+kWOUELN+Axh2DSx31gM7SOWCxrjrzYnAQll0lgviuDH1Ll2Y4tWHafthskEUfbR PWJG/su3pVZEdnbpi353Ku1Z0qN5J+LarkTC8SU9ZwSgVJFNzhFdP4VJFO5eTY/9pkVr VnmKc1lZJTJuPfL6l3GruWt4i1GhXXCSAPHxmjgR/k4D1MOS+3WeP5kzQSEKgoJ10QdE iCKjt/0EvVSUKCuzmQZEvaJOr38Djias0kUvvCeXLZ3Wb3PcInph+jV7s62Bk4P/bmhD jFSw== X-Gm-Message-State: AKGB3mIItszMaRH8loEwulADMV5yPGBBMvSEtN7VfRg9hjP/B56cX/mq 7U6fWlGVxCL8PlfpxphnD0dg427gbhk= X-Received: by 10.55.145.135 with SMTP id t129mr1869795qkd.205.1513019252607; Mon, 11 Dec 2017 11:07:32 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 15/19] nptl: sparc: Fix Race conditions in pthread cancellation (BZ#12683) Date: Mon, 11 Dec 2017 17:06:59 -0200 Message-Id: <1513019223-7603-16-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1513019223-7603-1-git-send-email-adhemerval.zanella@linaro.org> References: <1513019223-7603-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. * 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 Signed-off-by: Adhemerval Zanella --- ChangeLog | 8 +++ 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, 274 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 e36fde6..98ccaa1 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 99cf6ab..25db049 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 ba53b4a..836ba01 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)