From nobody Wed Feb 21 00:29:24 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TfcbT1Vfbz59yfD; Wed, 21 Feb 2024 00:29:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TfcbS6RBdz4CfT; Wed, 21 Feb 2024 00:29:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708475364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yoBXrt7gGvugbJ0XmQmS9+Bv5FRbbn2M5J84zkPvRf0=; b=jV9fjNAA8QZsqUT/Zul3I/WCFIphI7eCAC9QXraExt6yJIm5tQV2MsIc5Se6qlkZVpP6KA ZP1PaDi9qMb7jQQjBtq3QGwIRFB/B8FXU6+aOoMui+QhjbHgnGatnmHyV4QjAhUdE3S2eD oPMB+kpcCvqRNsXZ/vFxzqtU37a0W+psWN9mi8F6vZ1gi3Ml2fcRw5hjmd4Zw5n/dVEd8o JCvmWMNGkodc1sovNpKHcqlxL6eYVrZuKpTguri2zWc+G1X1hcUxxOvn8X4+2VFVHI0mkg gkIZHouz05MhYaxRFmPXz5d+ynpXx1jAhm5XWW7LOmVy1bBwYWyokUtlrUjuVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708475364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yoBXrt7gGvugbJ0XmQmS9+Bv5FRbbn2M5J84zkPvRf0=; b=ZKkbrrIixxbnBvX5HvJN3zBtsNda8BeF/Obe0WsBSHXq6Wdt3nXsEOcqiKZD3F+gdRh4+I lkao7uxbQixLgHUE7hiiRyem7ZJBwaWwlP6LvxvDjGyy/nIrn1Qpxg+Bz2eO9bzRnvRfQ7 n4LjgQJNrj1qkdnIxUMy9bIexdocfTBxXxdeJBftEVBqbnRMDPYbL2zlJ1q0HDcbdE7OlA hGwj0YKG/ygh+HcpnA6yWHZDUZkrpUcKzX3m9+7++ceZhxwbvCQNG9s1nIsIvGfAfDlgAr xxNwkw+1Tjf4k0VRRv3cX/1zWEVB6unvhkPqTav65BACP9zcLebLrnt4oaZ78w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1708475364; a=rsa-sha256; cv=none; b=s6kH3xYZA2+xuWScH0ewW4ONj1Aj1Q/o+w0lnra3y+Ku4QFFoSBWMYX4eMzLrF9hYYVqd1 6Eyc46d6FrlXYkbUq05smhZQuy1I5MbjPcd6VWNNRMMA2h7q36ZTA39F1WY7lV0M2M+q2E G5UxFMqVvC/XuNAsclW7tks3i6WLXHjlVcJuUuDP8gQy6Oe/OaVkK9BxdMdJkp+Vr4eD4f DpBdXyaka1ZLNHJ3mzPmHyDgWI+bx6WYlciBiliH05U4CoGQcj1wWODbpdd9wYhxvvnCij hgfh31n+zwG9wlIFvg8XV8qlE/Dm8hdl2p9+yVyLHy5eEpV4ufNdUnmRgc1MIg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TfcbS5VcpzgqB; Wed, 21 Feb 2024 00:29:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41L0TOLe000234; Wed, 21 Feb 2024 00:29:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41L0TOH5000231; Wed, 21 Feb 2024 00:29:24 GMT (envelope-from git) Date: Wed, 21 Feb 2024 00:29:24 GMT Message-Id: <202402210029.41L0TOH5000231@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 8271d9b99a3b - main - libsys: remove usage of pthread_once and _once_stub List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8271d9b99a3b98c662ee9a6257a144284b7e1728 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=8271d9b99a3b98c662ee9a6257a144284b7e1728 commit 8271d9b99a3b98c662ee9a6257a144284b7e1728 Author: Konstantin Belousov AuthorDate: 2024-02-20 14:45:29 +0000 Commit: Konstantin Belousov CommitDate: 2024-02-21 00:26:11 +0000 libsys: remove usage of pthread_once and _once_stub that existed in auxv.c, use simple bool gate instead. This leaves a small window if two threads try to call _elf_aux_info(3) simultaneously. The situation is safe because auxv parsing is really idempotent. The parsed data is the same, and we store atomic types (int/long/ptr) so double-init does not matter. Reviewed by: brooks, imp Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D43985 --- lib/libc/gen/Makefile.inc | 1 + lib/{libsys => libc/gen}/_once_stub.c | 0 lib/libsys/Makefile.sys | 1 - lib/libsys/auxv.c | 20 ++++++++++++++++++-- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 8d30e06cfed9..ce7a34d0e58e 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -9,6 +9,7 @@ CONFSPACKAGE= runtime SRCS+= \ __pthread_mutex_init_calloc_cb_stub.c \ __xuname.c \ + _once_stub.c \ _pthread_stubs.c \ _rand48.c \ _spinlock_stub.c \ diff --git a/lib/libsys/_once_stub.c b/lib/libc/gen/_once_stub.c similarity index 100% rename from lib/libsys/_once_stub.c rename to lib/libc/gen/_once_stub.c diff --git a/lib/libsys/Makefile.sys b/lib/libsys/Makefile.sys index cb9ca1749ba8..e33a11bacb57 100644 --- a/lib/libsys/Makefile.sys +++ b/lib/libsys/Makefile.sys @@ -33,7 +33,6 @@ PSEUDO+= _clock_gettime.o _gettimeofday.o SRCS+= \ __error.c \ __getosreldate.c \ - _once_stub.c \ getpagesize.c \ getpagesizes.c \ interposing_table.c diff --git a/lib/libsys/auxv.c b/lib/libsys/auxv.c index b0b3a8ed708b..88f49ef53be1 100644 --- a/lib/libsys/auxv.c +++ b/lib/libsys/auxv.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include "un-namespace.h" @@ -40,6 +41,8 @@ extern int _DYNAMIC; #pragma weak _DYNAMIC void *__elf_aux_vector; + +#ifndef PIC static pthread_once_t aux_vector_once = PTHREAD_ONCE_INIT; static void @@ -61,8 +64,9 @@ __init_elf_aux_vector(void) return; _once(&aux_vector_once, init_aux_vector_once); } +#endif -static pthread_once_t aux_once = PTHREAD_ONCE_INIT; +static bool aux_once = false; static int pagesize, osreldate, canary_len, ncpus, pagesizes_len, bsdflags; static int hwcap_present, hwcap2_present; static char *canary, *pagesizes, *execpath; @@ -77,11 +81,19 @@ static void _init_aux_powerpc_fixup(void); int _powerpc_elf_aux_info(int, void *, int); #endif +/* + * This function might be called and actual body executed more than + * once in multithreading environment. Due to this, it is and must + * continue to be idempotent. All stores are atomic (no store + * tearing), because we only assign to int/long/ptr. + */ static void init_aux(void) { Elf_Auxinfo *aux; + if (aux_once) + return; for (aux = __elf_aux_vector; aux->a_type != AT_NULL; aux++) { switch (aux->a_type) { case AT_BSDFLAGS: @@ -166,6 +178,8 @@ init_aux(void) if (!powerpc_new_auxv_format) _init_aux_powerpc_fixup(); #endif + + aux_once = true; } #ifdef __powerpc__ @@ -256,10 +270,12 @@ _elf_aux_info(int aux, void *buf, int buflen) { int res; +#ifndef PIC __init_elf_aux_vector(); +#endif if (__elf_aux_vector == NULL) return (ENOSYS); - _once(&aux_once, init_aux); + init_aux(); /* idempotent */ if (buflen < 0) return (EINVAL);