From patchwork Fri Dec 17 21:19:20 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: 12696732 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 DF9E6C433F5 for ; Fri, 17 Dec 2021 21:21:03 +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=mJABcfNx1tWtBCi8BpBkvY7Vbk3E8T2bUFQZiZYIZ4U=; b=BubJBUFaA4+YXE 58BJHVLzbE7EMIRBZ59sjXikiYxO1YQFlYCeNe+RBibWGpuqRv3zKKTdHtLGgl77XutQv9oA0rQ9K na+OeBK2sSbOAJb32Y2JVW9OaLnnIkSHUsU5ptUTXCmtnSKVMFpSGbOqldExRK/Ls8H/QYIWFwmfg 5x/eQH+Sctq54pwXCCh/eqhA3mPnYdd6adugRMbVmOKrk2kAxhCUR3CWSaYm7lq4m1Ew8n1lkbskP 6lO3EPXDRHxuJN58xCGM3nUvlMKsaDeqWrEggE71XpjJDjL/rpRaMAEukJmzDdyPpcgGb0ae4tXF8 R31nqlUe381rpPa/iWBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1myKda-00CPvx-3w; Fri, 17 Dec 2021 21:19:34 +0000 Received: from mail-mw2nam08on2118.outbound.protection.outlook.com ([40.107.101.118] helo=NAM04-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1myKdW-00CPvS-TS for linux-arm-kernel@lists.infradead.org; Fri, 17 Dec 2021 21:19:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZGCwV6DbTmn0U4lb/kn5nQWcOmEbKkWocxPCnCQjnQ5OCdfrf8nXxRsFdhELMaW6pvlJgdsH76If+CdO0BrQnAUNym+fkebuAX2pYFcHSsYqzpAcRvsJBZhJp1zeKPSVciqIcORG8Ih81/pO3b7cHSd7H0ks81G8x6e7n9wo5ELN1VP1wh8atDPv8ZEkxML/BBa5yChuPC9J6lbnbSER3o/BMIQs/3spAQSiSqywYaXxgCst9rRukqZq/qOLooQ5gDudEfFXEiZ6XkKyB5d5gzE0anDmxmxdJkVJx7OoAJ25co51m4/L9z0j2VNvDVXdB1xEVDMPIcIOM00C4kmpqA== 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=ph9uUSsge3o+Bm+vUZaMHkOCKSNo7/C5lNqBPiHBMrk=; b=c6KfPJ0teTElHyOvS17zhp7i0+9IuN5uK3vl7KAD4B9uFUmboBrgziFhWlQsSVOc8QGhtDWyq1q7NGpNe/iBOEXRkYgu47QU5+KLq1TA3S+cIEh1w+2TkG8mjE4so7+c98XwsTxZtdxM1lrnfa9MydBOWULg1QdGE3O6mbgQ2OBbvSOLUdOxCU0EblNVrOGjTpCBPwmN6kTK9FfB8izfkSbrEyYJFPoHt6dm4j7OkYm8wNhkTdIQSuUnZPsxtYhus5WHqIfhErkQgYupepqoshaUNw7RbHPzpYMzLq0l7GAK2aCuki6ld4T1o2zh0vlmhgJJP1+ogCgxekEoyVir5A== 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=ph9uUSsge3o+Bm+vUZaMHkOCKSNo7/C5lNqBPiHBMrk=; b=M/woK8x4nKE6ZJEcSFGI3/s7p8yprTQMSKsjoj7fVA6Lpc4uCgVCZ48ykSwSGTywALZR3ZBe1aFO7xmdicXSXGH7kSP090m0fUpi7mOSww8rsMo2KeB0vkdOl04xcAJfVbI6J3PrA+rfl331uATDBA2vd9dgj0wTanX2vMRD9yg= 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 MWHPR0101MB3136.prod.exchangelabs.com (2603:10b6:301:2f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.18; Fri, 17 Dec 2021 21:19:23 +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.4778.015; Fri, 17 Dec 2021 21:19:23 +0000 From: D Scott Phillips To: linux-arm-kernel@lists.infradead.org Cc: Catalin Marinas , Marc Zyngier , Will Deacon , Darren Hart , patches@amperecomputing.com Subject: [PATCH v4] arm64: errata: Fix exec handling in erratum 1418040 workaround Date: Fri, 17 Dec 2021 13:19:20 -0800 Message-Id: <20211217211920.2004032-1-scott@os.amperecomputing.com> X-Mailer: git-send-email 2.31.1 X-ClientProxiedBy: CH0PR04CA0097.namprd04.prod.outlook.com (2603:10b6:610:75::12) To MWHPR0101MB2893.prod.exchangelabs.com (2603:10b6:301:33::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b1dc1e53-4ffc-4128-e5e3-08d9c1a2e5b9 X-MS-TrafficTypeDiagnostic: MWHPR0101MB3136:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0Y36jR2F4qQKQ96TWthuyyDPQtYC6ZVCAirvonyZl4jMqCLYDYJTJOg41gXQX/GM62Gp4HpEwUsubYGq4O8nQsi29uDq3oZNK1SQWQ4PE023poLORvskPbv2JDAvH28FTgqQPnpgEX2B6tP9l3vSwLoJ8KxN2miWmFPKFfJkRaiE/U0roqorovNz9Ksg7/14K4gxHBHzypzUqEKlvZ2dGhmAOdckJqlnoW2JCbErkEpY5qcq4LP55VwLHObAhGqxCQSoC4jxN8szMlVip4Pd6aLHfowSQlegnVMbAxwH9ejk8tzyVE0xxYQQK+JvTZgxOPckAhM69Zk7N5d2mCel1oCaE8yCh9hkRLCEO/7Syd2QuuHeld4FBsB4naUAu7XsY2iKVBiIWOTMKtKUZ+AgkKi176JvbH+tQRJlrhaa9pkg7pfAdxCIkreo0BNOrLhilcBRbDBEa1fPX4qkJr/7YEc0Lq97ak5lsTNCsYuOyEKQ9g+ur754Pr9Rsv1Lpi2fRj3TEuFszc9lVRqsKqMCYeeOIu18DZb0QuzEpiUane/BY/iCZXNKkyk4kiCbYluOhyfqrlXuRU7yfGFReQyiCQUJmfCrv3bNcuzfSOtoh0OLI7zlfhnT24rmplHF+ynuPQmhF7xF2eIxUDJ3i7aSbe2bSd5c/QTVgo8AtgiK7K2oyGUOp5KTmSFg06P3vOH3hEZumMT7Kpco73u0T5oPTQ== 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)(54906003)(83380400001)(6486002)(5660300002)(6916009)(8936002)(2906002)(66556008)(2616005)(508600001)(6506007)(316002)(6512007)(86362001)(107886003)(38100700002)(4326008)(8676002)(66946007)(52116002)(26005)(1076003)(38350700002)(66476007)(186003); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: n7auJMFS/U01EDX5dKhXaVcZM3g1ys1i9vdq6+KPG2OW0ETqWNgBzvBuLE/AHc1uf01fuDIrvb1wIA83oQ2ARSZAqs8Sk2DpZCqgwxpTQen5ipdvN+kd5nEtLXiVw7DlKFZmvxzaj51XBr+3B05HRzsEdS3tNEOwTThWppz4+3EafUYAmb2Ew5kgv6Exal1rdTRJhxkOkSdzmNNAhVkuG6+9SixZN+UJ3fBtdZ9boP45mZtIZPMfFpG6HYb02T7SxXCqdfyxiunmIWGDEeCE7UByQNrvLsBwCjXJSGaNR4NxXceN4EX2xfEPbti/MVldHjKTQ5i0dKiicH8J2tpN+iECge5XEsd0O7tohR7MGjkMF4krpxPeCgEFQwZMkBUiavJIJ6aNcrTzOQYc6wniFaLTcmASjBokAHqd5tP5lkk2NIzC1dWMPH1+teO3dV3CFSS8KSANYywcajNJUo17f2N3VWNT/0MXI8E2JzSk7FN7zZ/WfCokv2qd1KhDBoYVSnauMl+/uIhNX0SU1fx0J4rGR8SPXszTiAWkJfu6L+5sUqHoJ2ghSp+qPI8HQ3rykqftz5haQ9eyWUFIdzfywYnUTzxOsnQA9IfH4JBod/o2a5YYPjUjV4gkVpkZOkT4aPxEB3z/ffZqplU1WXm11UAy7d8+fNSgxdWSLLXjHevNQCIVnmsbuEFIy4dCt0yVA/0JOFduvVNs+Uyad0v/tFpzAH+gW3uLyUZik9aea8d8NerrutvG92vMmd6j8w2wfSe/npGRU1rY4IB3xrmGkMA07X/kx9xhk8Mb/U97cvHrwwqW9euJ8W9VrCAlDNBUSmwZ/5sbhg49RLwvHOuwcrXBTnIhIqpdEiHOzeMGTrf3xlynTuVNcC7Q+dD+Agmy/s2RUqbmoeYeF7bUThp9sm6opHlPWZSj+d3PwD3XpgrIqUZZ0rg5cEKjNf4LpxTtwcVpk6LhjVDnbCqFExPve8zDf/10qUoiUAGW2tHnq+4MJism1TVaVCRSOj2ABDcBQHuLxnsHI6KyIPijF9NUDKTXNozPZV9pMUK1lXwqLLrk8HFaR1L7/8Je8aduDJDwEfUBsfmucqbJx60lJ0IB/G4gYVrDxXInpVRy2iV6YGVtFp9kKURbsn18yPYGNYr4Ct2BZ337TY6+feRy1DIA3QY3BAhcofog33JrkO1hYL44c40C10tX6K2B3Un3+MtdITR8SKj3PeeanZQstm5swupVrOQYBfIUA9w/uHIwlnX1xDtkrkOsZNHI9Rn9eLQRclMCvBBi6D6/7siRUNzBlTlesVjgNHTLHQ82Phd6jZtj0BhvPu6YwdXtgNpjIkQ+HV0NxyRyixjXYWyW9x349H1q7gn8V8Fe9qNU0Vq3uJpGrTfolzUBT5iyaNYYqvGByjGv8Wb0bfvz4fDOLJXkk0zYzOAXTjZ8imTHprqdNsFoNjWQajNazHzV9LV1aZ1ThH9yBXW5SSrVOByorFbnDM1np2i23wynedk8+HMhYeSop0S3XtvolemwLAs70vnmbH80JZtIdakTa707KZDpgrHywLjF6sAsXqSjivI+JoydbdPUQj5ITTbKNhoG4nAq84bGOXAcToQDMCVy6FypeOGuI1da6TMk8WM1qSZiM8XPWHpAN3hJkms/+24TEEZD4/j+Gci2t54dWb8OgeHUibm6bJcq3NcB3osCkXBvg4q/KA4q04vDR4bJsRRbjtuvsp3F2S4w8A1FjQq7W6X2rg== X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: b1dc1e53-4ffc-4128-e5e3-08d9c1a2e5b9 X-MS-Exchange-CrossTenant-AuthSource: MWHPR0101MB2893.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2021 21:19:23.5945 (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: laMteNn165zwPZrAvLI6FWerN/FZRUWzAukZ1d2yK/YRPxFnF4LpLGaFi7+bVDy2tXoQPtIZ4gti0XdaPSuRuPdACXJaplMKt7MEOu/kKg82FMRgJn4zB5TqzaH1fgCP X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR0101MB3136 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211217_131931_041353_3FB03F2B X-CRM114-Status: GOOD ( 14.92 ) 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 vct access trapping 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(). The 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 --- 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 | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index aacf2f5559a8..b37ff23e625e 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -439,34 +439,23 @@ 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; + preempt_disable(); - 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); + preempt_enable(); } /* @@ -501,7 +490,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 +600,7 @@ void arch_setup_new_exec(void) current->mm->context.flags = mmflags; ptrauth_thread_init_user(); mte_thread_init_user(); + erratum_1418040_thread_switch(current); if (task_spec_ssb_noexec(current)) { arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,