From nobody Fri Sep 16 20:25:43 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 4MTlvD31PMz4d6gm; Fri, 16 Sep 2022 20:25:44 +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 4MTlvD29HZz3Ml9; Fri, 16 Sep 2022 20:25:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663359944; 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=uA5vLFbuBQzkYMLHgvhq+aoiAuP885qZT6jvDaSzrq0=; b=jXuFYy4dITSmIZqR172JMuknztxEi/ZOw/ZccdPZP/sb/1PRWbPJoT9EmHOo8nmgqWQSvu 0ytuVzPp9NIHTjENa21+HFweFTJKDglS3qdETK9Xew7DGRv7EJKGLu4DxERDxhz5JOIBBK eHaTw4A3JC/63mpPd4218CKFeIFthpirIGzzK48Eqs7Z4Tzwxd6Dbhjv4wgV1n7TIfUtdg S8NH5IEm417LPHkY97j/JmLFRelwTvg9eiOao45Yo6HIyK3NuUuW7QUOM5pCgTbQJUfDhH rN9hwN2gSR8Q8e1i5M09VLXU//q+icNfByXko+eBEYQuMW0oGz2UHO7nfVdXjQ== 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 4MTlvD1909zSyc; Fri, 16 Sep 2022 20:25:44 +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 28GKPivb026463; Fri, 16 Sep 2022 20:25:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28GKPhmk026462; Fri, 16 Sep 2022 20:25:43 GMT (envelope-from git) Date: Fri, 16 Sep 2022 20:25:43 GMT Message-Id: <202209162025.28GKPhmk026462@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: e2879ece4314 - main - 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/main X-Git-Reftype: branch X-Git-Commit: e2879ece4314eed2d22fe484bd4adfcbb1009685 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1663359944; 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=uA5vLFbuBQzkYMLHgvhq+aoiAuP885qZT6jvDaSzrq0=; b=Yk09YcP+qXBRTf4FtcW3edrmQL/zsGV15yCrHgB+bgTJ8L2dDtpsF2y9joI3+IrhaC/dlS dAshE2BSh6yMpDc1v6yuXbV3959bW56owl690JzyczjDqHdzKTf1DIdo7Zo6J7ZP0sUT2E /OuTsgEd6+2ctYwh8SSdc6w+qZSMIQ8UaJL4OBSQHDEiBFvQaYQjwydXNTIO0E2Ohc56Fo kTMJdOVF4w19WSVKKGwqo9AqxJK3N4uTCQ2anPfrFO6fzDkiXOSGR6vfe0G5xA8HMSUKav KgEFq7fGQejPp0Nezr/0GH2rloCVWLJG4t/fuAZW4B0lH0pg3I3mVUurObScNQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1663359944; a=rsa-sha256; cv=none; b=mIFkMvxNLhbJukUZc9jnlYfC/kAcq8eWgZLZu3p5x6Eg88/ZQskc1k3hwrScjmIeaG53kl X3794lsYaTj76yK3d4uwaKQXdgRO6W9lKFDudkNpN5SDXs+XbGqCDs9sTZ839Xwm1wu1/I gHS1uKz+NY/nCckWl7+lydFojjsP2KGa+1WRTEUxCO9ocROaRv5uw4Oygh9GQnXZ9fuv51 v5K4ZZupAwXipiTChuJihVTdCtKegQGj4q2TxR3314KHa1Tf7AD+viBJaIvHXUNGwATQwc dMEG9jk6d7OuxLF6KZU+MhHfeo7Bn8yDRGAOhx2+5lWbQM1nEjT4+RWkXNGCJQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=e2879ece4314eed2d22fe484bd4adfcbb1009685 commit e2879ece4314eed2d22fe484bd4adfcbb1009685 Author: Konstantin Belousov AuthorDate: 2022-09-12 19:45:21 +0000 Commit: Konstantin Belousov CommitDate: 2022-09-16 20:24:06 +0000 libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") and get_rlimit(RLIMIT_STACK) Reviewed by: brooks, imp (previous version) Discussed with: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36540 --- 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