From nobody Fri Sep 30 02:04:16 2022 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 4Mdtnr691rz4dfPM; Fri, 30 Sep 2022 02:04:16 +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 4Mdtnr5XYSz3fQN; Fri, 30 Sep 2022 02:04:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664503456; 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=r6c66eKBaBUj6HLl5eV/8x61k+PvguLaC1DkO1njCLg=; b=qYfzAyQpaZRjqyjJd55/OuT394qq2ltFcZ2rY6YXFMIziJgF4AP9sZM9PCC2OyNNSZqIym osOJq1Rrkerw4586bbCh/jorjhGYHQPKsf8mDG1X+Osq/rwlfxkwMwTzKDZgo0tF/6dM0H TTEMH6ZctBzt07zszxgJB10o3ZMBgFXLfCdmIa0pyX6O6NX/6+M57e2V+gEFJbyq23b4r5 4lfVgTEdZvjd/Y78nkgvPEEYXicmp+fKTkyQoPKxWbrrrYtbPxZgkW5qn4iMvHbETGpUg/ JuGiUY+ULOuS8JkM3wXc+KZQbMyR584orirvUKM4OGQK2kFIFR+4bBlwvwG/aw== 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 4Mdtnr4dKsz11tF; Fri, 30 Sep 2022 02:04:16 +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 28U24GFq099271; Fri, 30 Sep 2022 02:04:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28U24GhN099270; Fri, 30 Sep 2022 02:04:16 GMT (envelope-from git) Date: Fri, 30 Sep 2022 02:04:16 GMT Message-Id: <202209300204.28U24GhN099270@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: b904f197ced4 - stable/13 - libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") and get_rlimit(RLIMIT_STACK) 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: b904f197ced4f98aa6f105eab937167e59314950 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664503456; 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=r6c66eKBaBUj6HLl5eV/8x61k+PvguLaC1DkO1njCLg=; b=nPbUIWPB6k5VuS7COEvEj50kZ0O+dyvME/BwWOtRgxmsc8z3JI59/zwteFVMhf8cGpj+Ax aXGPrLc6OYcZHLiv/9xWdP4eCBU+4h7Zn9XZla/HmZBJ6gig6MnYn8wCltd9MEzJoYnnij hb+R21f2eB6lNfBn3Dcb4kk2Cuv2SOaEO8RAudL+PXKC6GxpzI53Gf7Ejs4m++uiX4pztC LmHMbExpflJuG86lWWXJU3Cj5GPBd9dxuh6eLB/LeAPCDEb/qOX0MunilT3VSqpuj4OLxR xuqNuV3tZC9TifqJM2vI67ZoFdKRiSlMileNQ+s62TF15BjYs/g3VuQY1F9XqA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664503456; a=rsa-sha256; cv=none; b=m559ahRXoAEFtrVsHFREKZbsqHZv5qukYccTwiViSkSHyfXpvRXwzfKj2zKa5Nd1QNLKMu caeeGiy9eI9UnTEP3dTOeIqDl9zGFiHo4w/2eQ3kwNTTWpYnLWJ3MrUisRTjNe03QjQz4j BfRXIdLBfo3m7zHXtuKjKdA4nymZapUo0goiLRROxDeo8n9+Ojhk5+/AmHGgJV9fXORGbP cIvHcfmVug4GuNjOnu0SDDf3Qu3wTRw36swQn8JUVHwlITUG6DZeGuEZHp2koxa9R2ZEhM i4LhrrUJSAfKUzgZCKHYSWyJKCAcDCgxYsC6quE7AplgLF2/5MwFEcl0ZM/OFA== 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=b904f197ced4f98aa6f105eab937167e59314950 commit b904f197ced4f98aa6f105eab937167e59314950 Author: Konstantin Belousov AuthorDate: 2022-09-12 19:45:21 +0000 Commit: Konstantin Belousov CommitDate: 2022-09-30 00:29:10 +0000 libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") and get_rlimit(RLIMIT_STACK) (cherry picked from commit e2879ece4314eed2d22fe484bd4adfcbb1009685) --- lib/libc/gen/elf_utils.c | 27 ++++++++++++++++----------- lib/libthr/thread/thr_init.c | 25 +++++++++++++++++-------- lib/libthr/thread/thr_stack.c | 26 ++++++++++++++++---------- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/libc/gen/elf_utils.c b/lib/libc/gen/elf_utils.c index ea5cc25f8601..ed3ac843f7c3 100644 --- a/lib/libc/gen/elf_utils.c +++ b/lib/libc/gen/elf_utils.c @@ -29,6 +29,7 @@ */ #include +#include #include #include #include @@ -77,19 +78,23 @@ __libc_map_stacks_exec(void) { int mib[2]; struct rlimit rlim; - u_long usrstack; + u_long usrstack, stacksz; size_t len; - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof(usrstack); - if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) - == -1) - return; - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - return; - mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur), - rlim.rlim_cur, _rtld_get_stack_prot()); + 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()); } #pragma weak __pthread_map_stacks_exec diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index 0ab051e57994..b9d3265a05dc 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include +#include #include #include #include @@ -462,18 +463,26 @@ init_private(void) if (init_once == 0) { __thr_pshared_init(); __thr_malloc_init(); + /* Find the stack top */ - 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 (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"); + } env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); if (env_bigstack != NULL || env_splitstack == NULL) { - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - PANIC("Cannot get stack rlimit"); - _thr_stack_initial = rlim.rlim_cur; + 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; + } } _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF); if (_thr_is_smp == -1) diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index af396fe2ba93..34536ecfad2e 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include @@ -149,18 +150,23 @@ singlethread_map_stacks_exec(void) { int mib[2]; struct rlimit rlim; - u_long usrstack; + u_long usrstack, stacksz; size_t len; - mib[0] = CTL_KERN; - mib[1] = KERN_USRSTACK; - len = sizeof(usrstack); - if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) == -1) - return; - if (getrlimit(RLIMIT_STACK, &rlim) == -1) - return; - mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur), - rlim.rlim_cur, _rtld_get_stack_prot()); + 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()); } void