From nobody Wed Feb 21 17:23:10 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 4Tg35S1cDDz59yyk for ; Wed, 21 Feb 2024 17:23:24 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Tg35R3d9Dz4mJn for ; Wed, 21 Feb 2024 17:23:23 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-3394b892691so3829327f8f.1 for ; Wed, 21 Feb 2024 09:23:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708536202; x=1709141002; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lfaz/mBcCkRHXE0Fbn5HIcxkL6EKby/AwuiGFignCOk=; b=W6RHzxPGod258mBZBGNy4JwdlMutNYBlVNWhv5j0hSeR7eZadusVTeJQiZ1FPpUiLN O4WJ12SGuN+8hp8LioiS6mBikpYjKd91BnaNAAiY0MRyUh18iHVwyAzPrzbMi4ESlXuw 95t1jy8D4DZ7VMM1A9C181bmna91F+QKgzcpwMQh44DCq0Tc1pkyJTqnOUIiFbszSaNh FSH1z2uSi7WbwbXLItQ6jzCdDzxOQ/v/H0ohHoTnI3CejNxxi4qxno1Zbd0U/cj9Lp3i JbKs5rIPrsU40b2qoOlnca744Hi39+Okg0LCe1PBoHBapV+SZ3KxlZX+VKd80NJR6owJ BBug== X-Forwarded-Encrypted: i=1; AJvYcCWpuwVMohuKSTdRx2H1jMV9dVqQmsea15Xv5R2WewV4/zFpVEBtx5A/y67TS2f3i71nxC7N2FuHdPiwG0jxYYF/ih1i+uhI0GL0+iwjsR9b X-Gm-Message-State: AOJu0YxfF7nGaie4RE6QrUfVGYo6pXl8GudOpxhkMOyJwM/sNwB5YTBw 69EkkC0M0gfXANU8qbKPfui4whLd3b9mkFyU3VwCtzaS2A8rE8rmTj/SkDycVITh4OiV+dIDHbi l X-Google-Smtp-Source: AGHT+IGTP4AHl49j7DhFU3z1V74cudhSGvHngciyNefkwSbP5r5mWpXWKRffo/TNvSxsQHD3IH89Qg== X-Received: by 2002:adf:e742:0:b0:33d:3e02:a5b0 with SMTP id c2-20020adfe742000000b0033d3e02a5b0mr135601wrn.11.1708536201650; Wed, 21 Feb 2024 09:23:21 -0800 (PST) Received: from smtpclient.apple ([131.111.5.246]) by smtp.gmail.com with ESMTPSA id o11-20020adfcf0b000000b0033b4dae972asm17489417wrj.37.2024.02.21.09.23.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2024 09:23:21 -0800 (PST) Content-Type: text/plain; charset=utf-8 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 (Mac OS X Mail 16.0 \(3774.200.91.1.1\)) Subject: Re: git: 8271d9b99a3b - main - libsys: remove usage of pthread_once and _once_stub From: Jessica Clarke In-Reply-To: Date: Wed, 21 Feb 2024 17:23:10 +0000 Cc: "src-committers@freebsd.org" , "dev-commits-src-all@freebsd.org" , "dev-commits-src-main@freebsd.org" Content-Transfer-Encoding: quoted-printable Message-Id: <4715B319-B7DE-4D06-9F27-00CFE5AF89A7@freebsd.org> References: <202402210029.41L0TOH5000231@gitrepo.freebsd.org> <964A29A2-4C51-4037-8EBE-320008D48AE0@freebsd.org> To: Konstantin Belousov X-Mailer: Apple Mail (2.3774.200.91.1.1) X-Spamd-Bar: ---- X-Rspamd-Queue-Id: 4Tg35R3d9Dz4mJn X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US] On 21 Feb 2024, at 14:17, Konstantin Belousov = wrote: >=20 > On Wed, Feb 21, 2024 at 12:51:04AM +0000, Jessica Clarke wrote: >> On 21 Feb 2024, at 00:29, Konstantin Belousov = wrote: >>>=20 >>> The branch main has been updated by kib: >>>=20 >>> URL: = https://cgit.FreeBSD.org/src/commit/?id=3D8271d9b99a3b98c662ee9a6257a14428= 4b7e1728 >>>=20 >>> commit 8271d9b99a3b98c662ee9a6257a144284b7e1728 >>> Author: Konstantin Belousov >>> AuthorDate: 2024-02-20 14:45:29 +0000 >>> Commit: Konstantin Belousov >>> CommitDate: 2024-02-21 00:26:11 +0000 >>>=20 >>> libsys: remove usage of pthread_once and _once_stub >>>=20 >>> 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. >>=20 >> You still need to load acquire and store release aux_once though, >> otherwise you can see aux_once as true yet read the pre-initialised >> data. In practice that=E2=80=99s surely very hard to hit, but the = code as >> written is now wrong. Also, idempotence should probably be made >> unnecessary by using 0/1/2 state for uninitialised/initialising/ >> initialised, as it=E2=80=99s still technically UB from a C AM = perspective due >> to not being data race free if two threads initialise at the same = time. >> Better to just do the correct thing rather than risk things going = wrong. >=20 > There is too much to handle 'in process' state for loosing thread, I = need > the whole libthr machinery. What do you need libthr for? In pseudo-C: x =3D load_acquire(&aux_once) if (__predict_true(x =3D=3D 2)) return; if (x =3D=3D 1 || !compare_exchange_strong_acquire(&aux_once, &x, 1)) { while (x !=3D 2) { yield(); x =3D load_acquire(&aux_once) } return; } /* initialise as before */ store_release(&aux_once, 2); I believe that=E2=80=99s all you need. Or compare exchange 0 to 1 as the initial operation; makes the source code shorter at the expense of a more expensive fast path: x =3D 0; if (__predict_true(!compare_exchange_strong_acquire(&aux_once, &x, 1)) { while (__predict_false(x !=3D 2)) { yield(); x =3D load_acquire(&aux_once) } return; } /* initialise as before */ store_release(&aux_once, 2); I probably have bugs in the above, but you get the gist. > I added the fences, thanks for noting. Thanks. Jess > WRT being UB from pure C, we already have much more assumptions about > atomicity.