From nobody Fri Sep 30 02:04:17 2022 X-Original-To: dev-commits-src-branches@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 4Mdtnt01Fdz4dfC1; Fri, 30 Sep 2022 02:04:18 +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 4Mdtns6d2Sz3fYb; Fri, 30 Sep 2022 02:04:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664503457; 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=zFX/3Yy0MY+vY7dm2E6Jp6Ns1nl4h2msucGI9Sqs3sg=; b=qP2r3/8/TZjPMCZyB7nAdDZHtH7Znw4/HJ17B4Z95bzTb8QyH3FXoS23lPb08ReOAlTG5D TZs6piu6nzrx9tDPVV+Wl1JqZiQAgBpQD4oybRHQH0C0rdiKbZu6zea/BQRCBehkJEH2YV /DtcfoMJzXUHVuISlwiDDpJMThI1Zvt2Qkkhxf1cbnWWqYJzH4q3LzTS9WCt3dxYgCVTS/ 3LAXzMzamYuQf2PEhd5trrGaCLe5Z+odfG1oKesZbNckh+GV3IvtU8eeMFyy7a5Vr4Xhs4 hBJbCfSe1m/HwLzaXcCxKncvy213QMpeVtZRR8JsHfMS4QsCEtQcEUVEM7Ksmw== 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 4Mdtns5k5bz11fM; Fri, 30 Sep 2022 02:04:17 +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 28U24HF1099297; Fri, 30 Sep 2022 02:04:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28U24HA3099296; Fri, 30 Sep 2022 02:04:17 GMT (envelope-from git) Date: Fri, 30 Sep 2022 02:04:17 GMT Message-Id: <202209300204.28U24HA3099296@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: f52dbee828b3 - stable/13 - libthr: extract code to get main stack base and size into helpers List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f52dbee828b398864980c6085485e96c78ba97c8 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664503457; 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=zFX/3Yy0MY+vY7dm2E6Jp6Ns1nl4h2msucGI9Sqs3sg=; b=GNao3gjG5l/Z8e6WMViFjXDXkBbIQyPL9Ly/5yNfD8Vd8W1NL2EZzo/gOrAD67kr95XCTp Siv0CgCaF/UfwMxdmM3a6ldzOKBzd7COzKhR+Xqg5DKIRePMe7PRFmd6/liChjEafb0KM/ JPUroSaNIS9mQW3XZ95a7fH+7DlYeP9F4FGECEjG75nL5UhpebquNGnrtyV5jBY7Y4zwzP znJ+C/nP3LV4dv53e21TvoadapkLi73iTLi6byZoe69CRv/c/eN3BJ6lxcEzlqF4hRB+gP jJQlLvsYmtYaYzr87IeAOqNaBMcKMlT0Iugbp2+Pe3W04ujdMsvMsQKyC8dkLA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664503457; a=rsa-sha256; cv=none; b=VzGekA/qbo0P96MXsqpMb5Te5fXqeKhU563C8ZcrW6JWV3x7Uoue9Fk3c6Wp+VIja3jgRM SnPdcxP1kKAb8x2Dd0e+xSj1sx0mi+lQ/jR/7X4rz4oBMkyIQ7FeC08XIVIUztp4BeFfKv Dxa9MSTCCIyXUK2KuMCC1gVhtzCZVr+VNhzlTUwQJ5aQiBO85v8LsiLCmoOAvbjZaBXn94 aLiiMSk2CwCgZyJv71cKo+xVXW2N65fUluiL+YmNfdLCR7KXQsK1fMkgyITtxNGThIMmXd 4m4WgreHmezzgbxosLn1id53bDLV2v9uBT+9BLiWl6mw9pBDvA+IcfE98yI5ZQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=f52dbee828b398864980c6085485e96c78ba97c8 commit f52dbee828b398864980c6085485e96c78ba97c8 Author: Konstantin Belousov AuthorDate: 2022-09-13 22:14:24 +0000 Commit: Konstantin Belousov CommitDate: 2022-09-30 00:29:10 +0000 libthr: extract code to get main stack base and size into helpers (cherry picked from commit e03c7f50054182549e097f9fbd9387d8c55ef521) --- lib/libthr/thread/thr_init.c | 56 ++++++++++++++++++++++++++++------------- lib/libthr/thread/thr_private.h | 2 ++ lib/libthr/thread/thr_stack.c | 24 +++++------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index b9d3265a05dc..1c4867cb8185 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -434,12 +434,43 @@ init_main_thread(struct pthread *thread) /* Others cleared to zero by thr_alloc() */ } -static void -init_private(void) +bool +__thr_get_main_stack_base(char **base) { - struct rlimit rlim; size_t len; int mib[2]; + + if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0) + return (true); + + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof(*base); + if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0) + return (true); + + return (false); +} + +bool +__thr_get_main_stack_lim(size_t *lim) +{ + struct rlimit rlim; + + if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0) + return (true); + + if (getrlimit(RLIMIT_STACK, &rlim) == 0) { + *lim = rlim.rlim_cur; + return (true); + } + + return (false); +} + +static void +init_private(void) +{ char *env, *env_bigstack, *env_splitstack; _thr_umutex_init(&_mutex_static_lock); @@ -465,24 +496,13 @@ init_private(void) __thr_malloc_init(); /* Find the stack top */ - if (elf_aux_info(AT_USRSTACKBASE, &_usrstack, - sizeof(_usrstack)) != 0) { - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof (_usrstack); - if (sysctl(mib, nitems(mib), &_usrstack, &len, - NULL, 0) == -1) - PANIC("Cannot get kern.usrstack from sysctl"); - } + if (!__thr_get_main_stack_base(&_usrstack)) + PANIC("Cannot get kern.usrstack"); env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); if (env_bigstack != NULL || env_splitstack == NULL) { - if (elf_aux_info(AT_USRSTACKLIM, &_thr_stack_initial, - sizeof(_thr_stack_initial)) != 0) { - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - PANIC("Cannot get stack rlimit"); - _thr_stack_initial = rlim.rlim_cur; - } + if (!__thr_get_main_stack_lim(&_thr_stack_initial)) + PANIC("Cannot get stack rlimit"); } _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF); if (_thr_is_smp == -1) diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index a5bbc5997d30..0e88c6711817 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -1102,6 +1102,8 @@ int _thr_mutex_destroy(pthread_mutex_t *); int _thr_mutex_unlock(pthread_mutex_t *); int __Tthr_mutex_lock(pthread_mutex_t *); int __Tthr_mutex_trylock(pthread_mutex_t *); +bool __thr_get_main_stack_base(char **base); +bool __thr_get_main_stack_lim(size_t *lim); __END_DECLS __NULLABILITY_PRAGMA_POP diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index 34536ecfad2e..3befb740639e 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -148,25 +148,13 @@ _thr_stack_fix_protection(struct pthread *thrd) static void singlethread_map_stacks_exec(void) { - int mib[2]; - struct rlimit rlim; - u_long usrstack, stacksz; - size_t len; + char *usrstack; + size_t stacksz; - if (elf_aux_info(AT_USRSTACKBASE, &usrstack, sizeof(usrstack)) != 0) { - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof(usrstack); - if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) == -1) - return; - } - if (elf_aux_info(AT_USRSTACKLIM, &stacksz, sizeof(stacksz)) != 0) { - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - return; - stacksz = rlim.rlim_cur; - } - mprotect((void *)(uintptr_t)(usrstack - stacksz), stacksz, - _rtld_get_stack_prot()); + if (!__thr_get_main_stack_base(&usrstack) || + !__thr_get_main_stack_lim(&stacksz)) + return; + mprotect(usrstack - stacksz, stacksz, _rtld_get_stack_prot()); } void