From patchwork Mon Apr 10 19:58:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 55674 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD10C3858409 for ; Mon, 10 Apr 2023 19:59:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD10C3858409 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681156777; bh=Ba8jRetWvSOdAXVVFW9bWRLYOmgSEH7rDNU/diyXD/o=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oPLMXQpWSjeXnKKKM9TPgR2nP163BPcJo8wnml+/79/mVTFi+74q9uk3CkCbQBppd HHs3TCXea3K0lXasuxKkkSXxFhpbIdJWEODJBDDMRD2+vMEbsm8e3p4PFuforo1c/J sKbgwUHB3pOodUXK+G0gFpY5W3ZciFIqBH0zj81k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 4CFC43858D38 for ; Mon, 10 Apr 2023 19:59:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CFC43858D38 Received: by mail-ot1-x32f.google.com with SMTP id f10-20020a9d2c0a000000b006a13f728172so701870otb.3 for ; Mon, 10 Apr 2023 12:59:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681156751; x=1683748751; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ba8jRetWvSOdAXVVFW9bWRLYOmgSEH7rDNU/diyXD/o=; b=te4/CzXG3eRk1TTZp21rS4sUheNw6CFwKcGEYOelWiyjhfFGlygOGVzgnV3nyq+6fl XpfY3V2wle4ilS1sH+cT97oK4MMXoNGH8DmycN2di5dJ78DrjlXHzdX8FPg4u6sJw/J6 A9qPx2Nm7mIcjIuoOYWGNwF5GfJdfQSMKl25Odqm7ZH1zgM/wNYNac8/8Rl3m7446vRO XQhGJb0HYNpq81Vvz8qrpXWbWGCk1G0JFKyimmzs+nsQaqa1GFE/lFYftnkgn2OBDptr qsU+6wtEWlg9xr/8oQVOFc0Ms0dbCerpSZo1sr+XXL+yWJqT+aDYE7fn1S0IdDqFFlcB svHw== X-Gm-Message-State: AAQBX9c/isbXl0kiU3OEopt6AytBsP6/3wlusBFkoRx2opE9/NAAjUVO Yj4XFS9FekURGW9d2s0CWXnDa3860S8j6KF8RjmpoQ== X-Google-Smtp-Source: AKy350ZpQIptFuRLgo9CZQxqHvA5ZRWqhQo/xjVGVPLOnmtD0DvyheZ0ie9knm85XTfQ4EmNAN7NdQ== X-Received: by 2002:a9d:77d4:0:b0:69f:b065:7716 with SMTP id w20-20020a9d77d4000000b0069fb0657716mr4510207otl.11.1681156751516; Mon, 10 Apr 2023 12:59:11 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:55a1:d59a:609a:284:6ed]) by smtp.gmail.com with ESMTPSA id r7-20020a9d7507000000b006a30260ccfdsm4673293otk.11.2023.04.10.12.59.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Apr 2023 12:59:10 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 00/22] Fix Race conditions in pthread cancellation [BZ#12683] Date: Mon, 10 Apr 2023 16:58:45 -0300 Message-Id: <20230410195907.4123869-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This updated version from previous one [1] is simpler due the changes already in done during past releases. It only requires changes on cancellation entrypoint, the SIGCANCEL handler, and minor changes on pthread_cancel. It also take in consideration the recent regressions like 404656009b, where some syscall can not be interrupted if not async mode is not enabled. Each cancellable entrypoint is now a just tail call to internal __syscall_cancel, with the nont required arguments set to 0 (it also allows some code size optimization). The single-thread or pthread cancellation disable is handled at __syscall_cancel as well. The first patch is the generic bits, while the rest is arch-specific syscall_cancel.S entry points and/or arch-specific bits (like 7 argument support for mips or argument handling for x32). So it can not be installed separately, since the generic version does not work correctly on all ABIs (another option might to move the fix itself to be the last patch). I have checked on x86_64, i686, aarch64, arm, powerpc, powerpc64, powerpc64le, sparc64, and mips. For the rest of the implementation I manually crafted the syscall_cancel.S from the compiler generated built of the generic syscall_cancel.c. Changes from v4: - Rebased against master, added or1k and loongarch. - Make SYSCALL_CANCEL and INTERNAL_SYSCALL_CANCEL macros to work better as tail call, to do not require argument suffling. - Fixed x32 to use TYPEFY and ARGIFY macros. - Fixed m68k to use names registers. - Fixed typos from last version. [1] https://sourceware.org/pipermail/libc-alpha/2020-April/112517.html Adhemerval Zanella (22): nptl: Fix Race conditions in pthread cancellation [BZ#12683] x86_64: Fix Race conditions in pthread cancellation [BZ#12683] x32: Fix Race conditions in pthread cancellation [BZ#12683] i386: Fix Race conditions in pthread cancellation [BZ#12683] aarch64: Fix Race conditions in pthread cancellation [BZ#12683] arm: Fix Race conditions in pthread cancellation [BZ#12683] powerpc: Fix Race conditions in pthread cancellation [BZ#12683] sparc: Fix Race conditions in pthread cancellation [BZ#12683] s390: Fix Race conditions in pthread cancellation [BZ#12683] riscv: Fix Race conditions in pthread cancellation [BZ#12683] arc: Fix Race conditions in pthread cancellation [BZ #12683] ia64: Fix Race conditions in pthread cancellation [BZ#12683] sh: Fix Race conditions in pthread cancellation [BZ#12683] nios2: Fix Race conditions in pthread cancellation [BZ#12683] microblaze: Fix Race conditions in pthread cancellation [BZ#12683] hppa: Fix Race conditions in pthread cancellation [BZ#12683] m68k: Fix Race conditions in pthread cancellation [BZ#12683] alpha: Fix Race conditions in pthread cancellation [BZ#12683] csky: Fix Race conditions in pthread cancellation [BZ#12683] mips: Fix Race conditions in pthread cancellation [BZ#12683] or1k: Fix Race conditions in pthread cancellation [BZ#12683] loongarch: Fix Race conditions in pthread cancellation [BZ#12683] elf/Makefile | 5 +- nptl/Makefile | 11 +- nptl/cancellation.c | 115 ++++++------ nptl/cleanup_defer.c | 5 +- nptl/descr-const.sym | 6 + nptl/descr.h | 18 ++ nptl/libc-cleanup.c | 5 +- nptl/pthread_cancel.c | 78 +++----- nptl/pthread_exit.c | 4 +- nptl/pthread_setcancelstate.c | 2 +- nptl/pthread_setcanceltype.c | 2 +- nptl/pthread_testcancel.c | 5 +- nptl/tst-cancel31.c | 100 ++++++++++ sysdeps/generic/syscall_types.h | 25 +++ sysdeps/nptl/cancellation-pc-check.h | 54 ++++++ sysdeps/nptl/lowlevellock-futex.h | 20 +- sysdeps/nptl/pthreadP.h | 11 +- sysdeps/powerpc/powerpc32/sysdep.h | 3 + sysdeps/powerpc/powerpc64/sysdep.h | 19 ++ sysdeps/sh/sysdep.h | 1 + sysdeps/unix/sysdep.h | 173 ++++++++++++++---- .../unix/sysv/linux/aarch64/syscall_cancel.S | 59 ++++++ .../unix/sysv/linux/alpha/syscall_cancel.S | 80 ++++++++ sysdeps/unix/sysv/linux/arc/syscall_cancel.S | 56 ++++++ sysdeps/unix/sysv/linux/arm/syscall_cancel.S | 78 ++++++++ sysdeps/unix/sysv/linux/csky/syscall_cancel.S | 114 ++++++++++++ sysdeps/unix/sysv/linux/hppa/syscall_cancel.S | 81 ++++++++ sysdeps/unix/sysv/linux/i386/syscall_cancel.S | 103 +++++++++++ .../sysv/linux/ia64/cancellation-pc-check.h | 48 +++++ sysdeps/unix/sysv/linux/ia64/syscall_cancel.S | 81 ++++++++ .../sysv/linux/loongarch/syscall_cancel.S | 54 ++++++ sysdeps/unix/sysv/linux/m68k/syscall_cancel.S | 84 +++++++++ .../sysv/linux/microblaze/syscall_cancel.S | 61 ++++++ .../sysv/linux/mips/mips32/syscall_cancel.S | 128 +++++++++++++ sysdeps/unix/sysv/linux/mips/mips32/sysdep.h | 4 + .../linux/mips/mips64/n32/syscall_types.h | 28 +++ .../sysv/linux/mips/mips64/syscall_cancel.S | 108 +++++++++++ sysdeps/unix/sysv/linux/mips/mips64/sysdep.h | 52 +++--- .../unix/sysv/linux/nios2/syscall_cancel.S | 95 ++++++++++ sysdeps/unix/sysv/linux/or1k/syscall_cancel.S | 63 +++++++ .../unix/sysv/linux/powerpc/syscall_cancel.S | 65 +++++++ .../unix/sysv/linux/riscv/syscall_cancel.S | 67 +++++++ .../sysv/linux/s390/s390-32/syscall_cancel.S | 62 +++++++ .../sysv/linux/s390/s390-64/syscall_cancel.S | 62 +++++++ sysdeps/unix/sysv/linux/sh/syscall_cancel.S | 126 +++++++++++++ sysdeps/unix/sysv/linux/socketcall.h | 35 +++- .../sysv/linux/sparc/sparc32/syscall_cancel.S | 71 +++++++ .../sysv/linux/sparc/sparc64/syscall_cancel.S | 74 ++++++++ sysdeps/unix/sysv/linux/syscall_cancel.c | 73 ++++++++ sysdeps/unix/sysv/linux/sysdep-cancel.h | 12 -- .../unix/sysv/linux/x86_64/syscall_cancel.S | 57 ++++++ .../sysv/linux/x86_64/x32/syscall_types.h | 34 ++++ sysdeps/x86_64/nptl/tcb-offsets.sym | 3 - 53 files changed, 2551 insertions(+), 229 deletions(-) create mode 100644 nptl/descr-const.sym create mode 100644 nptl/tst-cancel31.c create mode 100644 sysdeps/generic/syscall_types.h create mode 100644 sysdeps/nptl/cancellation-pc-check.h create mode 100644 sysdeps/unix/sysv/linux/aarch64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/alpha/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/arc/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/arm/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/csky/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/hppa/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/i386/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/ia64/cancellation-pc-check.h create mode 100644 sysdeps/unix/sysv/linux/ia64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/loongarch/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/m68k/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/microblaze/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/mips/mips32/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/mips/mips64/n32/syscall_types.h create mode 100644 sysdeps/unix/sysv/linux/mips/mips64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/nios2/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/or1k/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/powerpc/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/riscv/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/s390/s390-32/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/sh/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/syscall_cancel.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/syscall_cancel.S create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/syscall_types.h