From nobody Thu Dec 30 15:36:39 2021 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 245E4191DC9D; Thu, 30 Dec 2021 15:36:41 +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 4JPsnh0X4Pz3rqg; Thu, 30 Dec 2021 15:36:40 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 C28D31AC60; Thu, 30 Dec 2021 15:36:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BUFad2I011072; Thu, 30 Dec 2021 15:36:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BUFadqi011071; Thu, 30 Dec 2021 15:36:39 GMT (envelope-from git) Date: Thu, 30 Dec 2021 15:36:39 GMT Message-Id: <202112301536.1BUFadqi011071@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Marcin Wojtas Subject: git: d8247df24c1d - stable/13 - libthr: Use kern.stacktop for thread stack calculation. 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: mw X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d8247df24c1d3bacadd60a6633d76fb6cb56fd0e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1640878600; 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=y/g/XkGLgGu8njDFmIwCXlUyXvZJ79yif6vstpa4mRc=; b=eFXfLliqVXW0YF927dQi3TUJ9fOEtFoXi0lavlB4HGfx6F1KSUVTnPgtDpo1lkVfGYkmSp 7mg4OpAikR0tIoIdliPDFG+o/C8tyZRbeRH2Gr0wTdiFdjF+E2u62OcmXUIOV2fiSr1MwB YTSS+Te8bUYEKTNWSN5b3XOTM2LpJdDBT6z2SsMOgB7cQJXgS+iVuowaOjc0oLdsTi2Jod fcs9bLG6ZCpX1kmTHLvBHmYnCuLA2M/DGfuJSz2LM2g762LJCb830tlypx0Gqj2JddxWdp RRtJttSdYONUIb0FOPCAm23xaPYnUSFB5DPMjq5/21HpWmbNnBFG9Ex/2/CcJQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1640878600; a=rsa-sha256; cv=none; b=anNjegpGz7XJaPKcAwguP9S7VCKkfnyoZMtMokNAD5s/x91Xi2i0ostBPKaNzPlVGeG1a/ K8OnkeZztb5A1C5PG+kfJ1+VVjaSGGHGUjMjpXkleApo8P5x08H/F3N4uFd1OPePVr3Qdk hiF4cqtsrEteWsEuidsXs3qolW9s1wJG/lbamfkehu/MPwgC14Fdb66IxjpYcoizCRBN3U MWAhys4SC/SSgsq3pjzivSCsWJr2katZ8f2rMkYkCd33NvBscs72iajeY0YT1qGtO2Mu2q saLDc6/0b1vQg8sBrPQPn5IfjzcbxrRoSB921xtpbNKUgkFjSN9XJ/IRfYVRDw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mw: URL: https://cgit.FreeBSD.org/src/commit/?id=d8247df24c1d3bacadd60a6633d76fb6cb56fd0e commit d8247df24c1d3bacadd60a6633d76fb6cb56fd0e Author: Dawid Gorecki AuthorDate: 2021-10-13 19:06:05 +0000 Commit: Marcin Wojtas CommitDate: 2021-12-30 15:25:25 +0000 libthr: Use kern.stacktop for thread stack calculation. Use the new kern.stacktop sysctl to retrieve the address of stack top instead of kern.usrstack. kern.usrstack does not have any knowledge of the stack gap, so this can cause problems with thread stacks. Using kern.stacktop sysctl should fix most of those problems. kern.usrstack is used as a fallback when kern.stacktop cannot be read. Rename usrstack variables to stacktop to reflect this change. Fixes problems with firefox and thunderbird not starting with stack gap enabled. PR: 239873 Reviewed by: kib Obtained from: Semihalf Sponsored by: Stormshield MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D31898 (cherry picked from commit 78df56ccfcb40013a3e6904bd6d39836220c3550) --- lib/libthr/thread/thr_init.c | 19 +++++++++++-------- lib/libthr/thread/thr_private.h | 2 +- lib/libthr/thread/thr_stack.c | 22 ++++++++++++---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index 82bde10a153e..f2a66c406e85 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -61,7 +61,7 @@ __FBSDID("$FreeBSD$"); #include "libc_private.h" #include "thr_private.h" -char *_usrstack; +char *_stacktop; struct pthread *_thr_initial; int _libthr_debug; int _thread_event_mask; @@ -388,7 +388,7 @@ init_main_thread(struct pthread *thread) * resource limits, so this stack needs an explicitly mapped * red zone to protect the thread stack that is just beyond. */ - if (mmap(_usrstack - _thr_stack_initial - + if (mmap(_stacktop - _thr_stack_initial - _thr_guard_default, _thr_guard_default, 0, MAP_ANON, -1, 0) == MAP_FAILED) PANIC("Cannot allocate red zone for initial thread"); @@ -402,7 +402,7 @@ init_main_thread(struct pthread *thread) * actually free() it; it just puts it in the free * stack queue for later reuse. */ - thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial; + thread->attr.stackaddr_attr = _stacktop - _thr_stack_initial; thread->attr.stacksize_attr = _thr_stack_initial; thread->attr.guardsize_attr = _thr_guard_default; thread->attr.flags |= THR_STACK_USER; @@ -427,7 +427,7 @@ init_main_thread(struct pthread *thread) thread->attr.prio = sched_param.sched_priority; #ifdef _PTHREAD_FORCED_UNWIND - thread->unwind_stackend = _usrstack; + thread->unwind_stackend = _stacktop; #endif /* Others cleared to zero by thr_alloc() */ @@ -464,10 +464,13 @@ init_private(void) __thr_malloc_init(); /* Find the stack top */ mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof (_usrstack); - if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) - PANIC("Cannot get kern.usrstack from sysctl"); + mib[1] = KERN_STACKTOP; + len = sizeof (_stacktop); + if (sysctl(mib, 2, &_stacktop, &len, NULL, 0) == -1) { + mib[1] = KERN_USRSTACK; + if (sysctl(mib, 2, &_stacktop, &len, NULL, 0) == -1) + PANIC("Cannot get kern.usrstack from sysctl"); + } env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); if (env_bigstack != NULL || env_splitstack == NULL) { diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index a5bbc5997d30..d6fb74bb4372 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -724,7 +724,7 @@ extern int __isthreaded; * Global variables for the pthread kernel. */ -extern char *_usrstack __hidden; +extern char *_stacktop __hidden; /* For debugger */ extern int _libthr_debug; diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index b08bafdd9417..fe50bc76db5f 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -149,18 +149,20 @@ singlethread_map_stacks_exec(void) { int mib[2]; struct rlimit rlim; - u_long usrstack; + u_long stacktop; size_t len; mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof(usrstack); - if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &usrstack, &len, NULL, 0) - == -1) - return; + mib[1] = KERN_STACKTOP; + len = sizeof(stacktop); + if (sysctl(mib, nitems(mib), &stacktop, &len, NULL, 0) == -1) { + mib[1] = KERN_USRSTACK; + if (sysctl(mib, nitems(mib), &stacktop, &len, NULL, 0) == -1) + return; + } if (getrlimit(RLIMIT_STACK, &rlim) == -1) return; - mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur), + mprotect((void *)(uintptr_t)(stacktop - rlim.rlim_cur), rlim.rlim_cur, _rtld_get_stack_prot()); } @@ -213,7 +215,7 @@ _thr_stack_alloc(struct pthread_attr *attr) /* * Use the garbage collector lock for synchronization of the - * spare stack lists and allocations from usrstack. + * spare stack lists and allocations from stacktop. */ THREAD_LIST_WRLOCK(curthread); /* @@ -249,11 +251,11 @@ _thr_stack_alloc(struct pthread_attr *attr) } else { /* - * Allocate a stack from or below usrstack, depending + * Allocate a stack from or below stacktop, depending * on the LIBPTHREAD_BIGSTACK_MAIN env variable. */ if (last_stack == NULL) - last_stack = _usrstack - _thr_stack_initial - + last_stack = _stacktop - _thr_stack_initial - _thr_guard_default; /* Allocate a new stack. */