Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp1610112lkv; Wed, 19 May 2021 14:01:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx67LmJGVae9+ufvhHlGn+CnC8iFnM/GyxK+pcSnuo+HOGb7xvXaMQNrl84ecnAc9xPh0DD X-Received: by 2002:a6b:7714:: with SMTP id n20mr1566068iom.126.1621458076851; Wed, 19 May 2021 14:01:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621458076; cv=none; d=google.com; s=arc-20160816; b=dpPgEQ93D6CnbLiFw+47NpJwMg9Bg8lcJpRE80nNvgyceCdVKaLUlnhT8RNTGIgUYH pfdttbPl+oRoJVQyw4vObdbRNuLaeBhqCOECoJyjoMWLp9nIqkBU4IaEL/08lKUWx2Cs Z8BZpuZ7+H8dxYS1mXmaMW8qYpnzw19nLgTm+RboH0S5sM88PtLTZbChDJ4CSwojou7T 2btOz50J9/oPq5SrGq7CbrX03dTrzrr6Rx+zve0V7a6ZIAuivU4VHUdDEv6ecW61KTy5 o2zO6sX5ydGSaZiM/WI+bJL4N8lyJRQdkxy2sawNRgjYanRHWfJM3RAx6Bx9ZQ4i/Ev3 g+6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H+gluzK/c87K8IKp3ObwtqZ2D5iBrtAAx4x/MZ/PQPw=; b=OUjBnrxb5xzDiZtqxg5wvpVyv4wVOjyLr2oagDIe/8O5arToMBmgXuqPAplttzIXRn 6qmMFe+GHl3HtWg34zCpcXxoZoaq+/JDS2K9+0/iHLAkM8eLHuGcEpXGjTrbecGa0pk8 50RJEfPWktLwiQGn+rFE9e/Jwv/XqhYNcZSz0EMUlWQknaL10ZIsNCL4ZX2MvICj7P5x Oihao7c/Axeky0Gt/gnBvb7dau5UqUNjocWggWNdOz+gxyHgQh61lIhszYQYKq77MHLH EPcEmk0/gZBJBzQfve0GlwzlVKpZ75EbCWHLtS9mvvnM/6vL2MJKzSbju/COrDWbHdOi vQIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GWOXp+zS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t15si996535ilu.79.2021.05.19.14.01.04; Wed, 19 May 2021 14:01:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GWOXp+zS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348079AbhERJtL (ORCPT + 99 others); Tue, 18 May 2021 05:49:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:49726 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348039AbhERJtJ (ORCPT ); Tue, 18 May 2021 05:49:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E073B61396; Tue, 18 May 2021 09:47:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1621331272; bh=RTAwS1Pb2T4YH9RTWzE3GXmCSLarrFRCCZxfeC0IjGo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GWOXp+zS1a58Dta3KChjDG5CJSAxD2/cHljq4pP1pAYtJd7zAhjfF9NQE9x23csXy N8MBfE/duY99QsBbvPB2PlmyTVMH0XJI57ErJ0MU+EzCWNTRbdbBbqGp8HTT64W1LL gYdiyfNbLozlC37iqdcQtttiQpriKoprIdRxSFlKaVoLc35LortpU/SKFa2vno1R4V +bI1Jv4qPJlwXn8SRZXTKI6Z8NkGgcT3UDV1BatVuWJOE0FrVwH0DaHmISPQVg3pHy xoApFmXHckl+UbvFmk5RMZhI9EIzE+KyTrPOY6Va37bK3ZKU42ebAtliYmWsgwtIRl pOzJM3y2TiX0w== From: Will Deacon To: linux-arm-kernel@lists.infradead.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas , Marc Zyngier , Greg Kroah-Hartman , Peter Zijlstra , Morten Rasmussen , Qais Yousef , Suren Baghdasaryan , Quentin Perret , Tejun Heo , Li Zefan , Johannes Weiner , Ingo Molnar , Juri Lelli , Vincent Guittot , "Rafael J. Wysocki" , kernel-team@android.com Subject: [PATCH v6 04/21] arm64: Kill 32-bit applications scheduled on 64-bit-only CPUs Date: Tue, 18 May 2021 10:47:08 +0100 Message-Id: <20210518094725.7701-5-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210518094725.7701-1-will@kernel.org> References: <20210518094725.7701-1-will@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Scheduling a 32-bit application on a 64-bit-only CPU is a bad idea. Ensure that 32-bit applications always take the slow-path when returning to userspace on a system with mismatched support at EL0, so that we can avoid trying to run on a 64-bit-only CPU and force a SIGKILL instead. Signed-off-by: Will Deacon --- arch/arm64/kernel/process.c | 19 ++++++++++++++++++- arch/arm64/kernel/signal.c | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index b4bb67f17a2c..f4a91bf1ce0c 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -527,6 +527,15 @@ static void erratum_1418040_thread_switch(struct task_struct *prev, write_sysreg(val, cntkctl_el1); } +static void compat_thread_switch(struct task_struct *next) +{ + if (!is_compat_thread(task_thread_info(next))) + return; + + if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) + set_tsk_thread_flag(next, TIF_NOTIFY_RESUME); +} + static void update_sctlr_el1(u64 sctlr) { /* @@ -568,6 +577,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev, ssbs_thread_switch(next); erratum_1418040_thread_switch(prev, next); ptrauth_thread_switch_user(next); + compat_thread_switch(next); /* * Complete any pending TLB or cache maintenance on this CPU in case @@ -633,8 +643,15 @@ unsigned long arch_align_stack(unsigned long sp) */ void arch_setup_new_exec(void) { - current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; + unsigned long mmflags = 0; + + if (is_compat_task()) { + mmflags = MMCF_AARCH32; + if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) + set_tsk_thread_flag(current, TIF_NOTIFY_RESUME); + } + current->mm->context.flags = mmflags; ptrauth_thread_init_user(); mte_thread_init_user(); diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c index 6237486ff6bb..f8192f4ae0b8 100644 --- a/arch/arm64/kernel/signal.c +++ b/arch/arm64/kernel/signal.c @@ -911,6 +911,19 @@ static void do_signal(struct pt_regs *regs) restore_saved_sigmask(); } +static bool cpu_affinity_invalid(struct pt_regs *regs) +{ + if (!compat_user_mode(regs)) + return false; + + /* + * We're preemptible, but a reschedule will cause us to check the + * affinity again. + */ + return !cpumask_test_cpu(raw_smp_processor_id(), + system_32bit_el0_cpumask()); +} + asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags) { @@ -938,6 +951,19 @@ asmlinkage void do_notify_resume(struct pt_regs *regs, if (thread_flags & _TIF_NOTIFY_RESUME) { tracehook_notify_resume(regs); rseq_handle_notify_resume(NULL, regs); + + /* + * If we reschedule after checking the affinity + * then we must ensure that TIF_NOTIFY_RESUME + * is set so that we check the affinity again. + * Since tracehook_notify_resume() clears the + * flag, ensure that the compiler doesn't move + * it after the affinity check. + */ + barrier(); + + if (cpu_affinity_invalid(regs)) + force_sig(SIGKILL); } if (thread_flags & _TIF_FOREIGN_FPSTATE) -- 2.31.1.751.gd2f1c929bd-goog