From patchwork Mon Dec 20 23:41:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: D Scott Phillips X-Patchwork-Id: 12696946 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 252D0C433EF for ; Mon, 20 Dec 2021 23:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=H+hWhCQxCarVnnTX01ScQcgeASbz/UjaDbcQghvsAbs=; b=QIaKzDkYnc7L7G qt3eSje/ljOZceSWoiu2FTrQ9sNaIhDIOZfn1nnJpAuJvkIfzD66ZFsuJz5pon+0xy+sMNlAIw2tD ydTa/IG57oa2wpdNuMI6K9AwRGw6RHYZv2z2jWQo0terQX3tyv64lXBos4zgb5cddwt/YTNTtmh5M iWbNjGhvVB6dtK/HX3yIim73K4zMuN68PM9VR7mK6TMVRPSYTb/l+YxpZ9x9yqtOqqqJtZr40UTca QuvFhsW25nTQYjI6tf5Up2gy6eiPJmuka/8WNwivzuGQoJ88g3zVRlHApe+ltHRL9uAg+LLdYLkwV JmNBVl6f50aEGNp3bq3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mzSHY-004xdj-1b; Mon, 20 Dec 2021 23:41:28 +0000 Received: from mail-bn8nam11on20711.outbound.protection.outlook.com ([2a01:111:f400:7eae::711] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mzSHU-004xdP-4f for linux-arm-kernel@lists.infradead.org; Mon, 20 Dec 2021 23:41:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TEaXRvdOmZw/DyQ9EpE5YF1lFOLMbAK06LSuZRGNvFhT2hjbn+9kRY9ioyTiUiGVkPKawgwsC16u2QbphD9FE+mUEsLLsja1JPDv4qM7RcUbp6eWSfep53uooV6VJIS6bHx4G2kQRA59kSfQDqMT+3oOYYcAvNIC08jJkrpI9JgamDlzIi/2qIYLOhebhTorznJ9DQnYbCZdrBGYDKdXE1APGLbBvJFydyVBU1l5qrFU44BLIt3nlSoPgRiakHEbzKe3dX6K31Gczf/J6RCUYBAN1qKk4nOd4rHXJ14GKHpPhk6oYqphDhEZQqvnmKZMBX//2uUHYD1ySU+ktPnqrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DtZDmiwlwOyUD2ZNNJ03l52xkHnJPHApK79Rs0Gn780=; b=Joo0Hn0Nvah4MQibnlLRIEpGeTRtaWP1q9WqKtYch4PNBYSpKpIz7wEaMQsk3sVVgdcPJvqBEdLdMhST/gbRgOfEH4eyYpO9MfZt7KA4GMehwg18dvX6QSsOWQecKY2wGxaePkv7FPILbUNKig3VOFLxrMBQ0btXtbRxoTVIhmaIwz27lRRA5ySNiVIX8xTSQn7XUttfBMafWLOf14NHZcnuqD6rMbKDB7fu1rquMNcZywPiXenjxHe4fzaLyxyyUTkhHxpsmJmONS9S2c7GV6BwWVxbU9wSIHLKHvkA8eD2XaKatH1n5M0PnQkWLdtEcIZCDiC7/Y+rahI1nnThnQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DtZDmiwlwOyUD2ZNNJ03l52xkHnJPHApK79Rs0Gn780=; b=YX8yUvCRpL3UUkyRoRBvarJkvkM0goDRjYu/+gYSLQJAHV5MoY2a43pvmchyEQzeehsmBFLlrvPf9Q+PtE2Sj7EBT0eR5Gs9hZdxdHmPym9hr2e8Q9ZWSalmwQtT7JafwusVdepZWBCza++RZt/Ntu2saKcdgD/FD31IK5Ukqsw= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from MWHPR0101MB2893.prod.exchangelabs.com (2603:10b6:301:33::25) by MW2PR0102MB3578.prod.exchangelabs.com (2603:10b6:302:12::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.13; Mon, 20 Dec 2021 23:41:18 +0000 Received: from MWHPR0101MB2893.prod.exchangelabs.com ([fe80::526:8374:e93f:3648]) by MWHPR0101MB2893.prod.exchangelabs.com ([fe80::526:8374:e93f:3648%4]) with mapi id 15.20.4801.020; Mon, 20 Dec 2021 23:41:18 +0000 From: D Scott Phillips To: linux-arm-kernel@lists.infradead.org Cc: Marc Zyngier , Catalin Marinas , Will Deacon , Darren Hart , patches@amperecomputing.com Subject: [PATCH v5] arm64: errata: Fix exec handling in erratum 1418040 workaround Date: Mon, 20 Dec 2021 15:41:14 -0800 Message-Id: <20211220234114.3926-1-scott@os.amperecomputing.com> X-Mailer: git-send-email 2.31.1 X-ClientProxiedBy: CH2PR14CA0005.namprd14.prod.outlook.com (2603:10b6:610:60::15) To MWHPR0101MB2893.prod.exchangelabs.com (2603:10b6:301:33::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 05bf91b9-0ce6-45b5-5e00-08d9c4123839 X-MS-TrafficTypeDiagnostic: MW2PR0102MB3578:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: N/jNgdMFb8f6La8rXQ8eRJAqW9n61+J/MT7X50qUy7XzjghNVVPQ8QqXWCjHThyhHaZT/G+nlpWThZBLcThkPpXiool/0QMBZ8/sSRnQUT/xMvIydiIIeoAj2phHlu64m1WDVDbLP1+Y+zdty9usUmEXE8VwQKbiuEwI6glikQtDScsoOzeH10VGzQNt29FFwTquuVIZT9NaOEO8U3djLU66i41iNKPIM5ngddPUQRjwJImUoGL7HsVW5Pt2t8pvckgAdgTLKbnwYJ6NuDo8y0KMGV4q5cXJFn8Oru0ApdjtoKqOGSml6FmcuafacJYmdtipCVEGT2Ba8StEH1rLQMqghWuXOTZ0v/tz9/T7kGGSGm41iSXvFeSBvZbVyGT9LpE0WnQsVpn11kzMcRwCX7+vb3HA/ZXl08FMll5IEdnIYHBptgzGc/g3y/+Aol0hx0xlUQPswKdD+YFxkerzqGJkrZRn6VpaxwFX5nKiUQcRWW6Yh/Fo+hJ3h9mxO3M6uSuxwtnK7cj/zD63gSMTmzQ5if2Uju3UDa95ZwlJzlsSeTf2c78n9KZxDlCCNg7DnKEElxu6TK6Jvw1b4m9mnbb8P6Xr9eAmL0zLRki80jF0sEUEGndTPDI11fp1x97GOEz+iI4AvsHuD4WLGYyctmcmm1wXkpTnCdSKyshiL3OAn5k6IBW5pNEgaZLUygnlIhhUB6mf/06fE/5yFYLcug== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MWHPR0101MB2893.prod.exchangelabs.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(8936002)(6916009)(6506007)(316002)(2906002)(66556008)(66946007)(6486002)(54906003)(107886003)(66476007)(2616005)(4326008)(186003)(83380400001)(8676002)(508600001)(1076003)(6512007)(26005)(38350700002)(38100700002)(86362001)(6666004)(5660300002)(52116002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oJNvD9ZBXoNSP1vPcMhM+KIDGnbAp98Zi2HR9IUcjVOAygyJcPQhb7B/IYAf0nW5zLkXXzq9wIAXViG2oqjyLjN/BFpY/4uH07q2HWdyDp8cVh0VkfyA2ndg3oJhe2Kl/iQVanNWENPBPd0V4qixJfvMmxnHgZA9PX7MQSeUtKbcY6gn4OKiMeI9/uCiAnXk05Z6LrCEpVJJ/SDme3UhncxxcHu2MUP5RhasNyt+T4SwpVNufOiC4CKupZx4j8s+2lse6sg0cm1CuR6/5IanJmlMddtivHpII8xJc1JqamxF+YU5Lgmt1zCwW6E9u4tiT8GsbmuKJ2nCpC3w03peRqHp0S5tH14viZQoeK4ZRB+DRXuPl8wXWWfUS0oEbOZw9Q1h5YrkgwXvpfb2v/avqa5is1X4GSkgGBzbdS6vTgMgcXt6yA448EX01TYv82RoCya0w9xEIHtbXR23wKTZSz/9GJIOSpx8ZxulZwioNQN8OyqgevU5Lgm0Htjb2qqsp/dcArX9LFv+FJS/izWIJzv7DqLrffppZq/ZNY51b9smu5ZSbdhHnGl2AaideiZqaGcKTjMbYW9IDa2apz5hHFhIjElE10i/MnrguzNTWEGfjFEGbfjNE+PMa50JB7dEmPZlTqOtkKEeyOkg3yJSWyZcxHkR5G+ONS4Yf+HPFnrhX5NmjfJqOxbygmSobaiN7Te4TM3LwEFf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Network-Message-Id: 05bf91b9-0ce6-45b5-5e00-08d9c4123839 X-MS-Exchange-CrossTenant-AuthSource: MWHPR0101MB2893.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Dec 2021 23:41:18.4968 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hsf52iz55/VJnDBQ2wxsekGkA5ZzaX5JH24FaIXA8y+Ylny2NuMJQoO9wwMTwsk2S+zKNv6Z3vHpTkqxMCGRvotQv0h8NejRwidtdcPiOhX52z3l3zuY/VheS2OrWwHb X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR0102MB3578 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211220_154124_308949_1E68496A X-CRM114-Status: GOOD ( 14.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The erratum 1418040 workaround enables CNTVCT_EL1 access trapping in EL0 when executing compat threads. The workaround is applied when switching between tasks, but the need for the workaround could also change at an exec(), when a non-compat task execs a compat binary or vice versa. Apply the workaround in arch_setup_new_exec(). This leaves a small window of time between SET_PERSONALITY and arch_setup_new_exec where preemption could occur and confuse the old workaround logic that compares TIF_32BIT between prev and next. Instead, we can just read cntkctl to make sure it's in the state that the next task needs. I measured cntkctl read time to be about the same as a mov from a general-purpose register on N1. Update the workaround logic to examine the current value of cntkctl instead of the previous task's compat state. Fixes: d49f7d7376d0 ("arm64: Move handling of erratum 1418040 into C code") Signed-off-by: D Scott Phillips Cc: # 5.4.x Reviewed-by: Marc Zyngier --- v5: - Move preempt_enable/disable out of switch_to path (Marc) - commit message nits (Marc) v4: - Move exec() handling into arch_setup_new_exec(), drop prev32==next32 comparison to fix possible confusion in the small window between SET_PERSONALITY() and arch_setup_new_exec(). (Catalin) v3: - Un-nest conditionals (Marc) v2: - Use sysreg_clear_set instead of open coding (Marc) - guard this_cpu_has_cap() check under IS_ENABLED() to avoid tons of WARN_ON(preemptible()) when built with !CONFIG_ARM64_ERRATUM_1418040 arch/arm64/kernel/process.c | 39 +++++++++++++++---------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index aacf2f5559a8..271d4bbf468e 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -439,34 +439,26 @@ static void entry_task_switch(struct task_struct *next) /* * ARM erratum 1418040 handling, affecting the 32bit view of CNTVCT. - * Assuming the virtual counter is enabled at the beginning of times: - * - * - disable access when switching from a 64bit task to a 32bit task - * - enable access when switching from a 32bit task to a 64bit task + * Ensure access is disabled when switching to a 32bit task, ensure + * access is enabled when switching to a 64bit task. */ -static void erratum_1418040_thread_switch(struct task_struct *prev, - struct task_struct *next) +static void erratum_1418040_thread_switch(struct task_struct *next) { - bool prev32, next32; - u64 val; - - if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040)) - return; - - prev32 = is_compat_thread(task_thread_info(prev)); - next32 = is_compat_thread(task_thread_info(next)); - - if (prev32 == next32 || !this_cpu_has_cap(ARM64_WORKAROUND_1418040)) + if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) || + !this_cpu_has_cap(ARM64_WORKAROUND_1418040)) return; - val = read_sysreg(cntkctl_el1); - - if (!next32) - val |= ARCH_TIMER_USR_VCT_ACCESS_EN; + if (is_compat_thread(task_thread_info(next))) + sysreg_clear_set(cntkctl_el1, ARCH_TIMER_USR_VCT_ACCESS_EN, 0); else - val &= ~ARCH_TIMER_USR_VCT_ACCESS_EN; + sysreg_clear_set(cntkctl_el1, 0, ARCH_TIMER_USR_VCT_ACCESS_EN); +} - write_sysreg(val, cntkctl_el1); +static void erratum_1418040_new_exec(void) +{ + preempt_disable(); + erratum_1418040_thread_switch(current); + preempt_enable(); } /* @@ -501,7 +493,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev, contextidr_thread_switch(next); entry_task_switch(next); ssbs_thread_switch(next); - erratum_1418040_thread_switch(prev, next); + erratum_1418040_thread_switch(next); ptrauth_thread_switch_user(next); /* @@ -611,6 +603,7 @@ void arch_setup_new_exec(void) current->mm->context.flags = mmflags; ptrauth_thread_init_user(); mte_thread_init_user(); + erratum_1418040_new_exec(); if (task_spec_ssb_noexec(current)) { arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,