From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-5.6 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id C22B91F8C6 for ; Thu, 26 Aug 2021 17:41:47 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DBE4A3858413 for ; Thu, 26 Aug 2021 17:41:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DBE4A3858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1629999705; bh=nmrJIe8t4nahiH53Ko3vxLcjUHkCMaJG/GEtemVavgQ=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=c+WmHfqFae+8fmX4OGk74xxbDD4t0Rrzvhr+3+ldy4dIBEhBwHHeFMCMNmC1i0jC3 kM3qPcFvIxaQMVa2QSrJZDn3xylOWQen9LEaRPiTg0RoIpP44HH1dDxioAjVw37Wlc Fts9HEW8raoe0keehFoBtTbNdZZqtjh+vzKH4heU= Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by sourceware.org (Postfix) with ESMTPS id A56063858D3C for ; Thu, 26 Aug 2021 17:41:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A56063858D3C Received: by mail-qk1-x72d.google.com with SMTP id m21so4216828qkm.13 for ; Thu, 26 Aug 2021 10:41:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=nmrJIe8t4nahiH53Ko3vxLcjUHkCMaJG/GEtemVavgQ=; b=Y4uuRADdSfYqpTkf11c6OLRlWbOmrglR/YIb7ZixM1tR4f39P8tZ9/IZp+aszM5Ik3 t/kF3yyY+wVPrQXwECMR2DcuyL/oU8p1Rfz44hF6um4WbURrxppU9bU4SM2TnIcQue+i mQeEozs72tsBUOmRI0BhXZbN2hUxUAUBCf8F/nRNvztQf+agic/2whh4DmYTk4rcU7jt eUEM+1h6wLkAiv5oIgI8lIooc+RpWPdmHJDhETd+ZU3qpytSh4LzabfLaa94AtMHW5pB VqWo/ihOL6MUeQoiZq2zNzA8UDCQ4ox5PDPRdVg9KW8ej6NVa7K4w9wepIbJKpzgaa7p on+Q== X-Gm-Message-State: AOAM530MmCnu0wRsZK7gdaEV5r1W9+xOgppgolONR2SjCKzETKib/drM XlsTglGgVNWXmqRxh1rQjCqwuNGAQXFsWA== X-Google-Smtp-Source: ABdhPJzydmvLFPQMEhjUwvStkmD8JZqlH7cSudvXHioNcJXGcfoKxQnspgjSWMTvGHN9x7syfbDaUA== X-Received: by 2002:a37:a596:: with SMTP id o144mr5102309qke.135.1629999676877; Thu, 26 Aug 2021 10:41:16 -0700 (PDT) Received: from ?IPv6:2804:431:c7ca:1a68:4e82:9e7c:b7e4:7f3? ([2804:431:c7ca:1a68:4e82:9e7c:b7e4:7f3]) by smtp.gmail.com with ESMTPSA id h20sm2187181qtr.81.2021.08.26.10.41.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 26 Aug 2021 10:41:16 -0700 (PDT) Subject: Re: [PATCH v2 12/19] nptl: Use tidlock when accessing TID on pthread_getcpuclockid To: Florian Weimer References: <20210823195047.543237-1-adhemerval.zanella@linaro.org> <20210823195047.543237-13-adhemerval.zanella@linaro.org> <87v93s5lj7.fsf@oldenburg.str.redhat.com> Message-ID: <7a9eb9c1-f83b-2d3b-cf21-daf70ce00dd0@linaro.org> Date: Thu, 26 Aug 2021 14:41:15 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <87v93s5lj7.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" On 26/08/2021 11:27, Florian Weimer wrote: > * Adhemerval Zanella: > >> Checked on x86_64-linux-gnu. >> --- >> nptl/pthread_getcpuclockid.c | 27 +++++++++++++++++---------- >> 1 file changed, 17 insertions(+), 10 deletions(-) >> >> diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c >> index 0a6656ea4c..8c0db0b9ba 100644 >> --- a/nptl/pthread_getcpuclockid.c >> +++ b/nptl/pthread_getcpuclockid.c >> @@ -29,16 +29,23 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid) >> struct pthread *pd = (struct pthread *) threadid; >> >> /* Make sure the descriptor is valid. */ >> - if (INVALID_TD_P (pd)) >> - /* Not a valid thread handle. */ >> - return ESRCH; >> - >> - /* The clockid_t value is a simple computation from the TID. */ >> - >> - const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED); >> - >> - *clockid = tidclock; >> - return 0; >> + sigset_t oldmask; >> + __libc_signal_block_all (&oldmask); >> + lll_lock (pd->tidlock, LLL_PRIVATE); >> + >> + int res; >> + if (pd->tid != 0) >> + { >> + *clockid = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED); >> + res = 0; >> + } >> + else >> + res = -ESRCH; >> + >> + lll_unlock (pd->tidlock, LLL_PRIVATE); >> + __libc_signal_restore_set (&oldmask); >> + >> + return res; > > This doesn't really solve the race, does it? The caller cannot use the > clock ID safely. Not really if the callee intention is to use along clock_gettime() or any other routine. I added this patch more for consistency. In fact, it makes me wonder if the requested improvement such as a pthread pid accessor [1] does really make sense, since it is inherent racy in a way that there is no guarantee that the tid will be valid when it is actually used. [1] https://sourceware.org/bugzilla/show_bug.cgi?id=27880