From patchwork Mon Oct 9 12:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13413522 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42706E95A8E for ; Mon, 9 Oct 2023 12:13:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2A2C6B0122; Mon, 9 Oct 2023 08:13:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDA936B0123; Mon, 9 Oct 2023 08:13:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA2806B0125; Mon, 9 Oct 2023 08:13:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id ABBB46B0122 for ; Mon, 9 Oct 2023 08:13:13 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2FFD416019B for ; Mon, 9 Oct 2023 12:13:13 +0000 (UTC) X-FDA: 81325812666.12.E590316 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf19.hostedemail.com (Postfix) with ESMTP id 94FF11A0016 for ; Mon, 9 Oct 2023 12:13:10 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DfxThh3J; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696853591; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MEl4cqby43N13Mjcl/O3LJnqEXj3XGhNXgxotEKCoww=; b=8HZofBC+xdF/EP31Tgme4IjdJZ+Na7iSA0jgZFIdJ5Bw79JwXMBiSIDX9UBgBwNMMQ151A rYfeP90SuRBG1y28CwYv8jSk8yE/OIJuQPztNdNUBY/fjub1Nwk1xSwpNPYjjBeknD5DxS hu3IutyUbNqPwrAdKqed0QTBlRhcvhM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DfxThh3J; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696853591; a=rsa-sha256; cv=none; b=cVsIpVKm83w+lLOtrU9/rzUm/0f+EJ7k5VeMj/D2gfbWvr8LpIX20UfV5NiOWVPcLfd28L 1K4kXdLRX6aWJeXhPGOH39LBjdGYLGnhHULwbvfNgh9f2IHm8jID+D4TQ0b6ED/47OpGL8 TzSdcIDhPk/c/whyQIJOfN8FnbNYxQ8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id AF30FCE13A3; Mon, 9 Oct 2023 12:13:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDCB5C43395; Mon, 9 Oct 2023 12:13:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696853587; bh=E2gRzIKAAZCXusVDWf1D5bQzl21y5pQx3M5rhoickm0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DfxThh3JD9h1tOr6J5VYdfQd4aCvkngudYG5TecsG7vkGYuC35Q80neOaDu6Tbny3 rYdJcf4Z/S4JZmg0CAeSvONbOti9zln+C7neG4Vf86ChE2Mta/qf372narPM63V8O4 pZi4HYe9kK9lHF6zKKaF0kBs20M2CYaBCgk4jsg9kxvgh0WEJHKqussFVu+wD0Qj8l exXX68bKmGAEYYIhseFueDHD4rEWnEt73UlBhpnZkZuSLkYwHg5c4oQOdDCs+D0oa/ J4AFMlo42agVepPlqtIEifI5v6p+Zo7ia5shuBI5uCm6cwApjChHrrbO8MZGL679q4 EJZONL4I72DXg== From: Mark Brown Date: Mon, 09 Oct 2023 13:08:55 +0100 Subject: [PATCH v6 21/38] arm64/gcs: Allocate a new GCS for threads with GCS enabled MIME-Version: 1.0 Message-Id: <20231009-arm64-gcs-v6-21-78e55deaa4dd@kernel.org> References: <20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org> In-Reply-To: <20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org> To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrew Morton , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Arnd Bergmann , Oleg Nesterov , Eric Biederman , Kees Cook , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=5172; i=broonie@kernel.org; h=from:subject:message-id; bh=E2gRzIKAAZCXusVDWf1D5bQzl21y5pQx3M5rhoickm0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlI+2hQ86d3PPUInKcJHG5iSyi18S/kyJjbjGChV3k KtZIv9uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZSPtoQAKCRAk1otyXVSH0JBVB/ 9gNePxI0Zw/qlNdC1P5YbfLS0mCs1hDryY8iNHtyG5iv6tWakcE03on8SkNYa2TyeV1OydkoqWHKuY f0o9+onuTPpxlE0M5XeF7Q61az2uZqpJ/6mGH3zIODsz+Md7CFkp4qYDMn3uPFn8uVZLVaY5FuC1Nw WueTGn9bybX9TklFREMnvNoD4RlGceACd+YtAfCM7bRBHlSVXxstlgfgW02nqRgMJu9K5Hh9T3yB8v JAlVMUr+LBseMtXMVVQGv/aeq9AYUoiv/IDlGrOe8xQ4u8uDYsJ5lIvYLv4whZcFxEa0YW0EwWCDLM 4YFrFzSM33KvcTwbHPvv/8tHTE17Pq X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 94FF11A0016 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: qmy8gu1frcnpyhjd7amp3hft89zedzuu X-HE-Tag: 1696853590-398579 X-HE-Meta: U2FsdGVkX18n8SDEWubkzCrgDmMQTjEwMbjBrtIggLChjGshRZbKE8sF8tGnLyL+GHuAkUIrm7wWWkU1gjPx40eJ9Ke1SMZfoLcXUgIlIEYHNtKf1JojzKOFWErUawTKAtTkH3pjpduLlY6c0f8ILPH+Y7vaNjriQpnXdSeJbtREx3rZRf7GEfEl7RBQw9OcXHtqmZQB2fbHHYAc41oSsEB0oRBDAa+ShjvfMGpKAV2cq/MWvTfL0XVS0wqvwdlrsMtn03uKHTxfFOr5BlRsrb9PpNlrBehTgpxt7SmZgdB6vIjLRZs1Qf1YP9jZL5mmIRSp0gBweDdGN38pZQs14wPbHGJVFHuNI6xnC0sIanmHMn+OClzRLqw8IS8rZBdwM5uXJT2PrgWz6lkivX0c4yQGW38aet+03ro+uK8P3FaRhQuVwj20gG9hQJqlw7QWKivOZAlKXypslDskHJjqHTjtcRy3PZ0Ei0HdifguQ+6BDtIMSR5MeQFuOAQHiNXQH4/ISh/StOezPrP+2tiXqQrmD0yGVn0IWDb6L6z9QaPZNr/VxI+KAsQk5OBE+L0hIbHRjdM4cOHCUxSXeC2LuM5dgNzpKG36ufnjhGdKg59ypR6YdMvC22YklULFkiZ+lzDVx9c2UIWdRTFLwXrUCZLGIIzZY6AxCDi8nqZmRUbYvypA8EHW8VsO01Ortkh1NklZ0HahIBa9L1NbxXcN1v3YMzvPB5NRL3UDh+G/nOf4Ev+MkWNP7G8XPghvqg+TIUz3PQxii2SKiSnauehlxDfk1sCQgOKzxoSXuRiGXAYVNqMmS2/cRgx/6GS6J5aikojW2b+rh9qEYPQ3hqC+vsW8FDg0FT9n7sIYGaJbbjgTGLcrMxwy9s1FVxx6GEv6z8ar5uBelzNY9OE028u3/ThnA6J7IW6Ogg96a9R+5T7tT/WyDU0lxkxyYnqYi9uAlGMwSn5uwBCiG+BumME TwPiwoWh PeQK7p2tdMq2RvT97eCdaAEtJ1GV5LrsPmdEp1izroFqKUY9o0Sw2b2JFLEaTdGolLt5xWMsF6Tl9cdl4C/Ba3O9N25ZoIqdoCiB4QoEBjM43PUsBYyOQNr1r6Q23DJyD8J0YYrb1jW85WBJSFJqZwLuFBzVxzzBU5RlmBODS0eS6YwQ4QyGLMcYoal3VBpf6ctzKx+y+PO1Wns7Xm3jgLhvUrNamg/wdmwYEgyUiMBVTt9x5+b3QJ4E0YlyGw3Dim1jTnHxzZILJBZiN5co2dt5pGoALnG5y0rPuxnBptlKnqcjzTsUjr8k5Kab46qIf9hYZKLzoxOH2IW8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We do not currently have a mechanism to specify a new GCS for a new thread so when a thread is created which has GCS enabled allocate one for it. Since there is no current API for specifying the size of the GCS we follow the extensively discussed x86 implementation and allocate min(RLIMIT_STACK, 4G). Since the GCS only stores the call stack and not any variables this should be more than sufficient for most applications. When allocating the stack we initialise GCSPR_EL0 to point to one entry below the end of the region allocated, this keeps the top entry of the stack 0 so software walking the GCS can easily detect the end of the region. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 7 +++++++ arch/arm64/kernel/process.c | 36 +++++++++++++++++++++++++++++++++ arch/arm64/mm/gcs.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index 04594ef59dad..4371a2f99b4a 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -58,6 +58,8 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task) void gcs_set_el0_mode(struct task_struct *task); void gcs_free(struct task_struct *task); void gcs_preserve_current_state(void); +unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + unsigned long clone_flags, size_t size); #else @@ -69,6 +71,11 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task) static inline void gcs_set_el0_mode(struct task_struct *task) { } static inline void gcs_free(struct task_struct *task) { } static inline void gcs_preserve_current_state(void) { } +static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + unsigned long clone_flags, size_t size) +{ + return -ENOTSUPP; +} #endif diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 84bac012f744..bc4f73fb0713 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -285,9 +285,40 @@ static void flush_gcs(void) write_sysreg_s(0, SYS_GCSPR_EL0); } +static int copy_thread_gcs(struct task_struct *p, unsigned long clone_flags, + size_t stack_size) +{ + unsigned long gcs; + + if (!system_supports_gcs()) + return 0; + + if (!task_gcs_el0_enabled(p)) + return 0; + + p->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0); + + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) + return 0; + + /* Ensure the current state of the GCS is seen by CoW */ + gcsb_dsync(); + + gcs = gcs_alloc_thread_stack(p, clone_flags, stack_size); + if (IS_ERR_VALUE(gcs)) + return PTR_ERR((void *)gcs); + + return 0; +} + #else static void flush_gcs(void) { } +static int copy_thread_gcs(struct task_struct *p, unsigned long clone_flags, + size_t stack_size) +{ + return 0; +} #endif @@ -369,6 +400,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long stack_start = args->stack; unsigned long tls = args->tls; struct pt_regs *childregs = task_pt_regs(p); + int ret; memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); @@ -410,6 +442,10 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.uw.tp_value = tls; p->thread.tpidr2_el0 = 0; } + + ret = copy_thread_gcs(p, clone_flags, args->stack_size); + if (ret != 0) + return ret; } else { /* * A kthread has no context to ERET to, so ensure any buggy diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index b0a67efc522b..cb0a64bf90af 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -8,6 +8,53 @@ #include #include +static unsigned long alloc_gcs(unsigned long addr, unsigned long size, + unsigned long token_offset, bool set_res_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long mapped_addr, unused; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + mapped_addr = do_mmap(NULL, addr, size, PROT_READ | PROT_WRITE, flags, + VM_SHADOW_STACK, 0, &unused, NULL); + mmap_write_unlock(mm); + + return mapped_addr; +} + +static unsigned long gcs_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + /* Allocate RLIMIT_STACK/2 with limits of PAGE_SIZE..2G */ + size = PAGE_ALIGN(min_t(unsigned long long, + rlimit(RLIMIT_STACK) / 2, SZ_2G)); + return max(PAGE_SIZE, size); +} + +unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + unsigned long clone_flags, size_t size) +{ + unsigned long addr; + + size = gcs_size(size); + + addr = alloc_gcs(0, size, 0, 0); + if (IS_ERR_VALUE(addr)) + return addr; + + tsk->thread.gcs_base = addr; + tsk->thread.gcs_size = size; + tsk->thread.gcspr_el0 = addr + size - sizeof(u64); + + return addr; +} + /* * Apply the GCS mode configured for the specified task to the * hardware.