From owner-svn-src-head@freebsd.org Mon Feb 4 21:16:17 2019 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E22B514B35F1; Mon, 4 Feb 2019 21:16:16 +0000 (UTC) (envelope-from kib@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 83C4272D99; Mon, 4 Feb 2019 21:16:16 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7320EB964; Mon, 4 Feb 2019 21:16:16 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x14LGGQK084376; Mon, 4 Feb 2019 21:16:16 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x14LGGCq084375; Mon, 4 Feb 2019 21:16:16 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201902042116.x14LGGCq084375@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Mon, 4 Feb 2019 21:16:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r343754 - head/lib/libthr/thread X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/lib/libthr/thread X-SVN-Commit-Revision: 343754 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 83C4272D99 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.94 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_SHORT(-0.94)[-0.938,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_MEDIUM(-1.00)[-0.998,0]; NEURAL_HAM_LONG(-1.00)[-0.999,0] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Feb 2019 21:16:17 -0000 Author: kib Date: Mon Feb 4 21:16:15 2019 New Revision: 343754 URL: https://svnweb.freebsd.org/changeset/base/343754 Log: Fixes for very early use of the pthread_mutex_* and libthr malloc. When libthr is statically linked into the binary, order of the constructors execution is not deterministic. It is possible for the application constructor to use pthread_mutex_* functions before the libthr initialization was done. Handle it by: - making thr_malloc.c locking functions operational when curthread is not yet set; - making __thr_malloc_init() idempotent, allowing more than one call to it; - unconditionally calling __thr_malloc_init() before initializing a process-private mutex. Reported and tested by: mmel Sponsored by: The FreeBSD Foundation MFC after: 1 week Modified: head/lib/libthr/thread/thr_malloc.c head/lib/libthr/thread/thr_mutex.c Modified: head/lib/libthr/thread/thr_malloc.c ============================================================================== --- head/lib/libthr/thread/thr_malloc.c Mon Feb 4 20:46:57 2019 (r343753) +++ head/lib/libthr/thread/thr_malloc.c Mon Feb 4 21:16:15 2019 (r343754) @@ -46,6 +46,8 @@ void __thr_malloc_init(void) { + if (npagesizes != 0) + return; npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); if (npagesizes == -1) { npagesizes = 1; @@ -59,6 +61,8 @@ static void thr_malloc_lock(struct pthread *curthread) { + if (curthread == NULL) + return; curthread->locklevel++; _thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); } @@ -67,6 +71,8 @@ static void thr_malloc_unlock(struct pthread *curthread) { + if (curthread == NULL) + return; _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); curthread->locklevel--; _thr_ast(curthread); Modified: head/lib/libthr/thread/thr_mutex.c ============================================================================== --- head/lib/libthr/thread/thr_mutex.c Mon Feb 4 20:46:57 2019 (r343753) +++ head/lib/libthr/thread/thr_mutex.c Mon Feb 4 21:16:15 2019 (r343754) @@ -390,6 +390,7 @@ __pthread_mutex_init(pthread_mutex_t * __restrict mute } if (mutex_attr == NULL || (*mutex_attr)->m_pshared == PTHREAD_PROCESS_PRIVATE) { + __thr_malloc_init(); return (mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, __thr_calloc)); }