From owner-dev-commits-src-all@freebsd.org Tue Jul 20 19:41:00 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id BF10A651CF1; Tue, 20 Jul 2021 19:41:00 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (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 mx1.freebsd.org (Postfix) with ESMTPS id 4GTpwr2yCNz3vc3; Tue, 20 Jul 2021 19:41:00 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 16KJekis041037 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 20 Jul 2021 22:40:49 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 16KJekis041037 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 16KJekP9041036; Tue, 20 Jul 2021 22:40:46 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 20 Jul 2021 22:40:46 +0300 From: Konstantin Belousov To: Alexander Motin Cc: src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 28d70deaafa6 - main - Fix race between first rand(3) calls. Message-ID: References: <202107201730.16KHUXPM057550@gitrepo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202107201730.16KHUXPM057550@gitrepo.freebsd.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.5 X-Spam-Checker-Version: SpamAssassin 3.4.5 (2021-03-20) on tom.home X-Rspamd-Queue-Id: 4GTpwr2yCNz3vc3 X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Jul 2021 19:41:00 -0000 On Tue, Jul 20, 2021 at 05:30:33PM +0000, Alexander Motin wrote: > The branch main has been updated by mav: > > URL: https://cgit.FreeBSD.org/src/commit/?id=28d70deaafa62c5d1602de5272c0aad0fcca8aff > > commit 28d70deaafa62c5d1602de5272c0aad0fcca8aff > Author: Alexander Motin > AuthorDate: 2021-07-20 17:15:08 +0000 > Commit: Alexander Motin > CommitDate: 2021-07-20 17:30:28 +0000 > > Fix race between first rand(3) calls. > > Before this patch there was a chance for thread that called rand(3) > slightly later to see rand3_state already allocated, but not yet > initialized. While this API is not expected to be thread-safe, it > is not expected to crash. ztest on 64-thread system reproduced it > reliably for me. > > MFC after: 1 month > --- > lib/libc/stdlib/rand.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c > index bddb0f040302..353f59349e1d 100644 > --- a/lib/libc/stdlib/rand.c > +++ b/lib/libc/stdlib/rand.c > @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); > #include > #include > #include > +#include > #include "un-namespace.h" > > #include "random.h" > @@ -68,11 +69,15 @@ static struct __random_state *rand3_state; > static void > initialize_rand3(void) > { > + struct __random_state *state; > int error; > > - rand3_state = allocatestate(TYPE_3); > - error = initstate_r(rand3_state, 1, rand3_state->rst_randtbl, BREAK_3); > + state = allocatestate(TYPE_3); > + error = initstate_r(state, 1, state->rst_randtbl, BREAK_3); > assert(error == 0); > + if (!atomic_cmpset_rel_ptr((volatile uintptr_t *)&rand3_state, > + (uintptr_t)NULL, (uintptr_t)state)) > + free(state); For this to have effect on less ordered architectures (AKA non-x86), at least reads of rand3_state should be atomic_load_acq(). But consider using _once() in libc, which takes care about ordering/threads. > } > > int