From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-eopbgr60073.outbound.protection.outlook.com [40.107.6.73]) by sourceware.org (Postfix) with ESMTPS id 256363858D3C for ; Tue, 19 Apr 2022 12:11:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 256363858D3C Received: from DB3PR08CA0010.eurprd08.prod.outlook.com (2603:10a6:8::23) by AM8PR08MB5603.eurprd08.prod.outlook.com (2603:10a6:20b:1d4::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5164.18; Tue, 19 Apr 2022 12:11:09 +0000 Received: from DB5EUR03FT050.eop-EUR03.prod.protection.outlook.com (2603:10a6:8:0:cafe::33) by DB3PR08CA0010.outlook.office365.com (2603:10a6:8::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5164.20 via Frontend Transport; Tue, 19 Apr 2022 12:11:09 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5EUR03FT050.mail.protection.outlook.com (10.152.21.128) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5164.19 via Frontend Transport; Tue, 19 Apr 2022 12:11:09 +0000 Received: ("Tessian outbound 62985e3c34b6:v118"); Tue, 19 Apr 2022 12:11:09 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 636c5c95b5ba55d2 X-CR-MTA-TID: 64aa7808 Received: from 956bc99c8847.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id AB9EDA3B-3C85-406C-80E8-1E628D97D7B4.1; Tue, 19 Apr 2022 12:11:02 +0000 Received: from EUR01-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 956bc99c8847.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 19 Apr 2022 12:11:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SW8Il5Tt//AW/hKkVAfQxv1dxOt9/5q24GOvtwM8d4zbUyYKKbcQ2PSB9rjdmUyD9fVZKuFjw743Qg5hp5ab1wUcOHg5/hM+Sn7VETLVh1r2YyxjLbtcNFqn7TzLvptuGtQDLvewpSJg5Rnj4e8BKyONkuBD6BJdJfIRVh0+C6pPzt5Leh5LRj2qlirYM/FD0kxOdoLVuCjwCHNstC5MD0FmK7nh6cdtfBT8smM6ZSR/0kfqNwsJLAZrzpkUjQJG4H1C+8Kxhj/xysAL2P/tRBIM5SbXULFByXtthlx2WFXtS+r4d64djN5dANeFUyAj1pgJxiJWzjP8jRyopsqYCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UyyCqvskJTTi1WOvA98d/pwDREdvtMT+cfdQWjQ7tC8=; b=ey8vxJvZwxnfz7IDFFaMEyHBjywiqpVXyigFzNWrI0Wxj2JlrIJH6145quKHXTw45CVLEp24ozxDtTYZ9rJikLHx6NT5T+FXtMNbLhzzPxzM4LRh6ho6HsxCF+2IMPWwQFHvV0A7AzyRt4A+jFSEU9rCUhsTKcb+AT+EOgJdSqe9rCn2X5vapd0SO5hpOOThC07Uhs2kp69HQL4Wd2BfRfrg9ey+f7TFGZANC+REBc0AkpfWJ+V0ThjhlvWarf/rg2jX1OHuOWjEX92wFCY1vYoMRTjdgE98ypN57VJyUAveNF+B3T7KRRYNXvAlr3go0ezoQsR/RTwGQ7//3LCTNw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; Received: from DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) by AM6PR08MB3733.eurprd08.prod.outlook.com (2603:10a6:20b:89::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5164.20; Tue, 19 Apr 2022 12:11:00 +0000 Received: from DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::6092:f7f0:3af2:fbca]) by DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::6092:f7f0:3af2:fbca%6]) with mapi id 15.20.5164.025; Tue, 19 Apr 2022 12:11:00 +0000 Date: Tue, 19 Apr 2022 12:10:45 +0000 From: Szabolcs Nagy To: Adhemerval Zanella Cc: libc-alpha@sourceware.org, Florian Weimer , Aurelien Jarno Subject: Re: [PATCH v2] nptl: Handle spurious EINTR when thread cancellation is disabled (BZ#29029) Message-ID: References: <20220414154947.2187880-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220414154947.2187880-1-adhemerval.zanella@linaro.org> X-ClientProxiedBy: AM0PR02CA0115.eurprd02.prod.outlook.com (2603:10a6:20b:28c::12) To DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 4e05584b-d4e0-4687-0547-08da21fdb060 X-MS-TrafficTypeDiagnostic: AM6PR08MB3733:EE_|DB5EUR03FT050:EE_|AM8PR08MB5603:EE_ X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: MviY4OZuLP9+1JU9AcYIONZMQqFohRxochEIxbN90hjRPZEFLlxwPj6uVzRBBDkJ8rmDBA58hIAB5vhZA5I58g0wgp6CrQ2URchaYodmNq6vgOY0ammvV/1gDNW9bXo/HhnsutzmVp1ELmeB8uvYcHPAdOUSf2MppoQS6XfQXlfjw5qHdagz8Wb9Oex4vxF03rg3dZcxYnpBdx7k39KagGN2qmKGwTNdJna80PI57guFC9w326HQcAG6++7cv44nhlPK0x4fVAnXiLAE0oY+0WSjaNXS1n6WGc8Nm3kuvb78/clNGf1LAcHh6IRLamS1bk73mkV2y1AdBRbxcVpKIffyLEcNk/mE5BTVqVbhXouRY0xetyfIoW0MYjMPanCz45aI+CRvSmVgvQyOVf1g76eQ9PVtf4zoSuDvr2PwM/a4y2xrS6QUp7jGFSb7Q8yDvrA1GUA2MMpp35c1YPxjq4+wrtsYU9UjKcu6sy8o8ZhhbddE7k77TntREOknfk2DCUHhz7IU4VLIgsL67zsgFLxJsLAD2RZWKgPl/evByVAkGFVZ11bOeQQ9XFmNgPt5JOhyC6XS4TXz10pRoyjHM4gO4wgLfO3gRMy0Bk+vsOcaRySxTqe8b9amVCH59caxB9suxVWcgb0AN7KOOSwVjw== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR08MB7179.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(86362001)(66476007)(4326008)(66946007)(66556008)(2616005)(36756003)(54906003)(5660300002)(508600001)(186003)(6486002)(2906002)(44832011)(316002)(8676002)(6512007)(26005)(6916009)(6666004)(6506007)(38100700002)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3733 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT050.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 990e5e28-02bd-4b07-4678-08da21fdaaed X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9/RloqNiX1y1KfroOu29W673m+vB2WkeNogzYAESFOGQhAkvWb+SzvD92L6GbqSEghQhTR0AzRhIPP/021YLPQc+LiA4vYtQTXae8j+GfDF9H5Jw+D6NLNhANXtnHrNN6KQIydt31d/AdqPXTsjzcMf+SDk8BNdQnoy+YmZ8/2hF+L+8980NvZ8pD1WgnVizBFFal/UuevfwWCUSuQuKZ97r8/0E0fPzPUJuoeuzlo8qPU27bxEp5QX3mgdoF1lP5YCeqZl4DyC7S5FCox9s3prRpL/BzH5dmMh8RnjUw6323i+s1Dmqr/ilimdxyc9vKs3s4u3om275ncusl9FtWKaZj7loI6tLqcWmIkibudU/OC9UtIneWAIAZgESHxxT/ThpOF0xG1wim7/InoQfWMXue2yG5vQe7nRPHemGttq1r/VeM0UOF9M1QgO88TOyiQjIFWnvCXFRTY+qo0Ri3q7w83sSRBsEr4Y8KuE4sJhu1BddnFpfsJFp4QYgnrn5WTttiLKbKNPql4yQio6iQNARZ3oaGtRGP7xb9l60x8l0+v2Tpr4zF2mB6r74VxxeIH78Cr4T0WSm8Ln5OZZhVd9nyky+EJjHpBUQihzmKfJmA4l/sbbiaZzQqxkgnzrYKGveaKLOopmPtNaINKwtSbfIUGKrdqIIuS4oN9TuY41XnMUrJETUkHuKBmHndD+s X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230001)(4636009)(36840700001)(40470700004)(46966006)(6666004)(44832011)(8936002)(6512007)(86362001)(70586007)(2906002)(70206006)(316002)(6506007)(47076005)(36860700001)(82310400005)(336012)(81166007)(107886003)(6862004)(26005)(4326008)(8676002)(2616005)(186003)(356005)(36756003)(6486002)(508600001)(40460700003)(5660300002)(83380400001)(54906003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2022 12:11:09.5871 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4e05584b-d4e0-4687-0547-08da21fdb060 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DB5EUR03FT050.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR08MB5603 X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 19 Apr 2022 12:11:14 -0000 The 04/14/2022 12:49, Adhemerval Zanella via Libc-alpha wrote: > --- a/nptl/pthread_cancel.c > +++ b/nptl/pthread_cancel.c ... > + /* Some syscalls are never restarted after being interrupted by a signal > + handler, regardless of the use of SA_RESTART (they always fail with > + EINTR). So pthread_cancel cannot send SIGCANCEL unless the cancellation > + is enabled and set as asynchronous (in this case the cancellation will > + be acted in the cancellation handler instead by the syscall wrapper). > + Otherwise the target thread is set as 'cancelling' (CANCELING_BITMASK) > + by atomically setting 'cancelhandling' and the cancelation will be acted > + upon on next cancellation entrypoing in the target thread. > + > + It also requires to atomically check if cancellation is enabled and > + asynchronous, so both cancellation state and type are tracked on > + 'cancelhandling'. */ > + > + int result = 0; > + int oldval = atomic_load_relaxed (&pd->cancelhandling); > + int newval; > + do > { > - /* A single-threaded process should be able to kill itself, since there > - is nothing in the POSIX specification that says that it cannot. So > - we set multiple_threads to true so that cancellation points get > - executed. */ > - THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); > + newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK; > + if (oldval == newval) > + break; > + > + /* If the cancellation is handled asynchronously just send a > + signal. We avoid this if possible since it's more > + expensive. */ > + if (cancel_enabled_and_canceled_and_async (newval)) > + { > + /* Mark the cancellation as "in progress". */ > + int newval2 = oldval | CANCELING_BITMASK; > + if (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling, > + &oldval, newval2)) > + continue; this continue looks wrong, the cas can fail spuriously (cancelhandling == oldval) and then continue jumps to... > + > + if (pd == THREAD_SELF) > + /* This is not merely an optimization: An application may > + call pthread_cancel (pthread_self ()) without calling > + pthread_create, so the signal handler may not have been > + set up for a self-cancel. */ > + { > + pd->result = PTHREAD_CANCELED; > + if ((newval & CANCELTYPE_BITMASK) != 0) > + __do_cancel (); > + } > + else > + /* The cancellation handler will take care of marking the > + thread as canceled. */ > + result = __pthread_kill_internal (th, SIGCANCEL); > + > + break; > + } > + > + /* A single-threaded process should be able to kill itself, since > + there is nothing in the POSIX specification that says that it > + cannot. So we set multiple_threads to true so that cancellation > + points get executed. */ > + THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); > #ifndef TLS_MULTIPLE_THREADS_IN_TCB > __libc_multiple_threads = 1; > #endif > - > - THREAD_SETMEM (pd, result, PTHREAD_CANCELED); > - if (pd->cancelstate == PTHREAD_CANCEL_ENABLE > - && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) > - __do_cancel (); > - return 0; > } > + while (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling, &oldval, > + newval)); ...here and this cas updates cancelhandling to newval without actually doing any cancellation. > > - return __pthread_kill_internal (th, SIGCANCEL); > + return result; > }