From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Cc: libc-alpha@sourceware.org References: <20210823195047.543237-1-adhemerval.zanella@linaro.org> <20210823195047.543237-13-adhemerval.zanella@linaro.org> <87v93s5lj7.fsf@oldenburg.str.redhat.com> From: Adhemerval Zanella 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-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: Thu, 26 Aug 2021 17:41:28 -0000 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