From patchwork Mon Jul 31 13:43:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13334741 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 ED9BEC04FE0 for ; Mon, 31 Jul 2023 13:52:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8807B280053; Mon, 31 Jul 2023 09:52:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 830FB280023; Mon, 31 Jul 2023 09:52:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D1C7280053; Mon, 31 Jul 2023 09:52:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5CB9B280023 for ; Mon, 31 Jul 2023 09:52:51 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 18E6C1A0B42 for ; Mon, 31 Jul 2023 13:52:51 +0000 (UTC) X-FDA: 81072047742.02.E2D676B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id E7D184001F for ; Mon, 31 Jul 2023 13:52:48 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VMvi8sHX; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1690811569; 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=z/Hfqm3gSkub6VfcgPGrQbJE+2xTyC922gBSrpwYMhs=; b=2G30omFKJJhBdbIYIurS8zVFnYf8s9zM7Fi/6HGDNdGiyT/gOtBu13cwPYEB4IBO4cw2uI mu7mY6e4jz+8+Ir/ysqb11ieKSdvILKq0yHumCbVaDUGbRf79HNQjgSuVMqkVTQBhFv9GR BsJ8/s7OO+Z215BvGqzNZkXUndGGtw0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VMvi8sHX; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690811569; a=rsa-sha256; cv=none; b=cRVMvwwpyroVy0VPrGiY1juWe98Jxq/5tx4BMKA4ODffm9GqvTuzBOalSDsHCbQ0tegGjk rvmfTGixhNVXtUHieZsZl7a98yfE7HW7TPjVTybLjvHb2F5duW1rdxfWnUh+Ho/1iT+px9 Z70wduyCCrH/2O2kl8fw+DhuPz7l1mU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1931D6116E; Mon, 31 Jul 2023 13:52:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 944B6C433C7; Mon, 31 Jul 2023 13:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690811567; bh=2KK/mGoHJbatstMicr5F2Ynx8xFfeN5q4Dmfeo7bTNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VMvi8sHX58ZXyBOhPTD8+q7Yv23XoKr5OFpfWKpLb9/CJDAtoBYlVDdu6BdWzEFqi w2tgFqvYg4zzd8AUWczMfb/JGQVTFaxvjvg5w4/uGe1qgCemzYCFJZm36BoSHwgZW7 BsJCphaezuns/OVDgcowDYbyRCK6MMZtiPDe8O/hewfusWOy8BU9DqAuz5wO7K/Z52 Ey0thCSUtvNS+itveyx8N5iWof00Pv21nh6QRWpb9rnvIViFcPfrPWiSg3t2CEloLb 9xlEEGPCkB87/PtSQ58zy2dRslm9VYS/XJjvpiJXfgFbLXC/Vu7Bz4l088pE0PYiGm jhOEpN4IFbJwQ== From: Mark Brown Date: Mon, 31 Jul 2023 14:43:28 +0100 Subject: [PATCH v3 19/36] arm64/gcs: Allocate a new GCS for threads with GCS enabled MIME-Version: 1.0 Message-Id: <20230731-arm64-gcs-v3-19-cddf9f980d98@kernel.org> References: <20230731-arm64-gcs-v3-0-cddf9f980d98@kernel.org> In-Reply-To: <20230731-arm64-gcs-v3-0-cddf9f980d98@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 , 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-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=5142; i=broonie@kernel.org; h=from:subject:message-id; bh=2KK/mGoHJbatstMicr5F2Ynx8xFfeN5q4Dmfeo7bTNE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkx7wjgR17AhihNN0JKCnsJQRA5lWhOmG2X+8nWz97 RDaryBqJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZMe8IwAKCRAk1otyXVSH0LAHB/ 9u3vp9EyTwc6A9tBQo66B6wZrS85yLwA6aHJ5+gYtDhv578YGAfS2eOsDSt7jvA5UlJe7FJpt2ghGB YYo3sMNaI0IqT3mDwSPC1yq+i9xzJvYb8shJR+9Rtq+wainEXskBH6PPLr+ohN2G43AOoQnlYOyCcZ lWWkfyNicv8aIBMbrwN/20iq1uocZTxS2HjQHCh9kG/QbAfC53jaJblqEghI7kLqNZsh3FaRb9dkvj i7HdHEUFAkLXsA0BolAR+u682Kv6HVne00fzZ3CDKawCyfRpOxvYUF0aEUDXdmWeczi4EsNtfOvyAO TP3h2h2wojkMRu7jwgRY41OHoJTRwj X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E7D184001F X-Stat-Signature: d93ygjw3uakqgsk4h754uzpfu83347dq X-Rspam-User: X-HE-Tag: 1690811568-920180 X-HE-Meta: U2FsdGVkX1/JGqVQxXtJ4rmLD65da8R/yU6yelJoIofNQ0C2NHWVvUqWNy2OF1DiNnNmaX3CYTjrMcBNzbCwQxyaW4Oucypw9AcDPBd3aCcyvNnPfWi70HVrc416J0YNBU3aCnJqRRPenJK2la0D3uhnnz8wRalSVGnQiVUkLRyg2716uz1awbXs1WArYYB0of+gUhfRE4OuR/PMm7HoJtQz1zW7e4YBBQ/PKYBJX7ZMuNetM2KaagAlduqq/7l84hW6bWb2Zk1btptMhSCt2jJICqM7TgCIRmFLnY4NjjuQGr14PRYWpPvIqEi1kMb/SbsffDGp7r6MoQ2hj6HY3Ez6RATrWtCFs6d5uSC8pYhf1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ter, 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 | 30 ++++++++++++++++++++++++ arch/arm64/mm/gcs.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 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 b8a42471aea3..1de6371ca2d8 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -284,9 +284,34 @@ static void flush_gcs(void) } } +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); + + 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 @@ -368,6 +393,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)); @@ -409,6 +435,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..1e059c37088d 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -8,6 +8,62 @@ #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, flags, + VM_SHADOW_STACK | VM_WRITE, 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 with limits of PAGE_SIZE..4G */ + size = PAGE_ALIGN(min_t(unsigned long long, + rlimit(RLIMIT_STACK), SZ_4G)); + 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; + + if (!system_supports_gcs()) + return 0; + + if (!task_gcs_el0_enabled(tsk)) + return 0; + + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) + return 0; + + 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.