From owner-dev-commits-src-main@freebsd.org Tue Jan 12 11:16:38 2021 Return-Path: Delivered-To: dev-commits-src-main@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 CA1F54D36E7; Tue, 12 Jan 2021 11:16:38 +0000 (UTC) (envelope-from arichardson.kde@gmail.com) Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DFSh65BZdz4pF6; Tue, 12 Jan 2021 11:16:38 +0000 (UTC) (envelope-from arichardson.kde@gmail.com) Received: by mail-ej1-f44.google.com with SMTP id b9so2995496ejy.0; Tue, 12 Jan 2021 03:16:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=LrL61kvAVHBFwauF7cpIPhMVKd6X34++FcT2HSQUIr4=; b=XHAXR6Arn8+4UuLTzOBgFX4Oq8JjgYjCaqdBLGMrDjIKy5U2sMIwE+CbDgHWcWrlso RoyU/SkebcdGY3ua+y7QuEm9eUMn+F8McjkfsX0h1ejWbyz9bLfH7hgGb49hhmVZQEJE FCLetslVSqTkDGrqbEo535M6fnmf8LQRSdh61FTUM7zvS9oCR08QB/61+quV7Nyw5h3q pEjBG3okroFBC6yhpta17kJuQiUUdAHjklVYOJz0k3O/FA2x+cJev5mR713GRpkESuYB asv0Nm67GSE/niQeP3imcgG6+n884E+pTd5ZVUf5DfGoWL1/O8TnEMaYTHWQ2iimSNBa NiVA== X-Gm-Message-State: AOAM532UGE5DMBTcWIxeYprLiT7KomXZDjhxemKGlCZzDc/E891E3CzC yoKcf4T/57RvoeZEGInjLJNyIy3aSSMXzg== X-Google-Smtp-Source: ABdhPJx9zGhpbN34Ct4dJ7Eh/lerihV/gVk52Ji/8MvVgtRkld6O5RroqubXjy9UU5yE4GjnmWh1hA== X-Received: by 2002:a17:906:ca08:: with SMTP id jt8mr2693057ejb.368.1610450197213; Tue, 12 Jan 2021 03:16:37 -0800 (PST) Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com. [209.85.221.50]) by smtp.gmail.com with ESMTPSA id a23sm1074046ejg.7.2021.01.12.03.16.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Jan 2021 03:16:37 -0800 (PST) Received: by mail-wr1-f50.google.com with SMTP id y17so2048239wrr.10; Tue, 12 Jan 2021 03:16:36 -0800 (PST) X-Received: by 2002:a5d:6ccb:: with SMTP id c11mr3771110wrc.224.1610450196641; Tue, 12 Jan 2021 03:16:36 -0800 (PST) MIME-Version: 1.0 References: <202101121046.10CAkLxI018198@gitrepo.freebsd.org> In-Reply-To: <202101121046.10CAkLxI018198@gitrepo.freebsd.org> From: Alexander Richardson Date: Tue, 12 Jan 2021 11:16:25 +0000 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: git: 85d028223bc2 - main - libthr malloc: support recursion on thr_malloc_umtx. To: Konstantin Belousov Cc: src-committers , dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 4DFSh65BZdz4pF6 X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; TAGGED_FROM(0.00)[] X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2021 11:16:38 -0000 On Tue, 12 Jan 2021 at 10:46, Konstantin Belousov wrote: > > The branch main has been updated by kib: > > URL: https://cgit.FreeBSD.org/src/commit/?id=85d028223bc2768651f4d44881644ceb5dc2a664 > > commit 85d028223bc2768651f4d44881644ceb5dc2a664 > Author: Konstantin Belousov > AuthorDate: 2021-01-12 09:02:37 +0000 > Commit: Konstantin Belousov > CommitDate: 2021-01-12 10:45:44 +0000 > > libthr malloc: support recursion on thr_malloc_umtx. > > One possible way the recursion can happen is during fork: suppose > that fork is called from early code that did not triggered > jemalloc(3) initialization yet. Then we lock thr_malloc lock, and > call malloc_prefork() that might require initialization of jemalloc > pthread_mutexes, calling into libthr malloc. It is safe to allow > recursion for this occurence. > > PR: 252579 > Reported by: Vasily Postnicov > MFC after: 1 week > Sponsored by: The FreeBSD Foundation > --- > lib/libthr/thread/thr_malloc.c | 13 +++++++++++-- > 1 file changed, 11 insertions(+), 2 deletions(-) > > diff --git a/lib/libthr/thread/thr_malloc.c b/lib/libthr/thread/thr_malloc.c > index 2f1c81142942..80a81f9c6c27 100644 > --- a/lib/libthr/thread/thr_malloc.c > +++ b/lib/libthr/thread/thr_malloc.c > @@ -41,6 +41,7 @@ int npagesizes; > size_t *pagesizes; > static size_t pagesizes_d[2]; > static struct umutex thr_malloc_umtx; > +static u_int thr_malloc_umtx_level; > > void > __thr_malloc_init(void) > @@ -60,11 +61,16 @@ __thr_malloc_init(void) > static void > thr_malloc_lock(struct pthread *curthread) > { > + uint32_t curtid; > > if (curthread == NULL) > return; > curthread->locklevel++; > - _thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); > + curtid = TID(curthread); > + if ((uint32_t)thr_malloc_umtx.m_owner == curtid) > + thr_malloc_umtx_level++; > + else > + _thr_umutex_lock(&thr_malloc_umtx, curtid); > } > > static void > @@ -73,7 +79,10 @@ thr_malloc_unlock(struct pthread *curthread) > > if (curthread == NULL) > return; > - _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); > + if (thr_malloc_umtx_level > 0) > + thr_malloc_umtx_level--; Should this also assert that "thr_malloc_umtx.m_owner == curtid"? Thanks, Alex > + else > + _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); > curthread->locklevel--; > _thr_ast(curthread); > }