From mboxrd@z Thu Jan 1 00:00:00 1970 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 5039EEE6450 for ; Thu, 12 Sep 2024 12:48:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A04166B00A3; Thu, 12 Sep 2024 08:48:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B3FA6B00A5; Thu, 12 Sep 2024 08:48:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87B9E6B00A7; Thu, 12 Sep 2024 08:48:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 68E376B00A3 for ; Thu, 12 Sep 2024 08:48:48 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EF79A1A044D for ; Thu, 12 Sep 2024 12:48:47 +0000 (UTC) X-FDA: 82556065494.20.C8F006A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 2DEF840015 for ; Thu, 12 Sep 2024 12:48:45 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726145221; 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: in-reply-to:in-reply-to:references:references; bh=rLBLI5dVrkWWSsMh7JaRkvOf4Z8ws21UnPX+021OswE=; b=S9i4MUWXiNYaaa2tjGThCZTnQAdSwyNlASvTWnbyrVeN0ZP7ic3h4VLJIylmWj8sMmbTaW SwUEsL1Q4KfET85esD8MMgMf6ZnHjImZkg+eN29Ui1kWsBbQntBMEVM4cugCSNKAXEL7PW u0LlqwaC4s/FizHHU15avKKFI5PA69Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726145221; a=rsa-sha256; cv=none; b=QwPpWOFZHLVCnKny144lHYI9bHekZxvyImbYAjmXxuR+vliU3JNX4lqLnNttTKM1rpSkqT RBydQXmR7VWshq6KZ7mxgkDA2VG3jfT93lTZvdYpdFkwj8+/LJ0wCfTJ0Dje5cAOb5HJkm 1R4pX6dPLevsSBzhq5vpkE5QgTvjLns= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com; dmarc=pass (policy=none) header.from=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 96090DA7; Thu, 12 Sep 2024 05:49:14 -0700 (PDT) Received: from e124191.cambridge.arm.com (e124191.cambridge.arm.com [10.1.197.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 04ED03F73B; Thu, 12 Sep 2024 05:48:40 -0700 (PDT) Date: Thu, 12 Sep 2024 13:48:35 +0100 From: Joey Gouly To: Will Deacon Cc: Dave Hansen , Kevin Brodsky , linux-arm-kernel@lists.infradead.org, nd@arm.com, akpm@linux-foundation.org, aneesh.kumar@kernel.org, aneesh.kumar@linux.ibm.com, anshuman.khandual@arm.com, bp@alien8.de, broonie@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, hpa@zytor.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, maz@kernel.org, mingo@redhat.com, mpe@ellerman.id.au, naveen.n.rao@linux.ibm.com, npiggin@gmail.com, oliver.upton@linux.dev, shuah@kernel.org, skhan@linuxfoundation.org, szabolcs.nagy@arm.com, tglx@linutronix.de, x86@kernel.org, kvmarm@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v5 06/30] arm64: context switch POR_EL0 register Message-ID: <20240912124835.GA1220495@e124191.cambridge.arm.com> References: <20240822151113.1479789-1-joey.gouly@arm.com> <20240822151113.1479789-7-joey.gouly@arm.com> <425b8f8c-b6b5-422a-b5f4-41dd2d1ae3bb@arm.com> <6c8ad091-a56b-41ba-b403-2e3c2e578100@intel.com> <20240912105017.GA22788@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240912105017.GA22788@willie-the-truck> X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2DEF840015 X-Stat-Signature: unywdctbntniay69p9tquj6panjw8hio X-HE-Tag: 1726145325-32886 X-HE-Meta: U2FsdGVkX18Mp09vFOBEbDX66I+FMClXJMQhsUdyg5ynSavHSv0G/nuycHI02p6eoKoTVkyty2TxcYK/VaGDaQfGzsN+tiqy/p3xl/jKoqTX4mqV/XZmUTo8Lm8Oh6YOOFtCHOFI6pbY7EkWUgIn64ZJeQkfh5G0ldVqh5kkZvk/MHE3zC5DJvoCfou4xC/+s+J+JPzuONhf2QtsR1h+iKt5nv8zWCnJsn/g0TisoVbPD//KARp1+kaHZqA9WXAt/51nmZMueptf7MyNAgVY/uQ7bkpuB+94ACKZaNwKalDhDok5vY3lA/aG2tr7fkseJODTuxLwDmB39G9hC7scvAZmn6WmgLlMiTL/wH8w0Ak02E4WAAJeSWcNgMQlovaa+e9u7G2ysy9Q/UbQeu6tkH+UobWW2VMwq8S9TltKQYsxr7BOlqFmvPKtFJ47Nt/rP6t1eJ6UUpHbIZIy6SwJf7mY0o3h44hqQUA8ySyI1jVF/bmg7+Rq4GJTDM+Di4yZA1+I170+3KGxv3eYe+J7tBj9ltxo0eUOj5VNtHEaCjgWdG8Sgn0pDUin7dx2SnjI6vGD1EwXqxlq6d25Jn0VH1b1fBoOhZhY8XbIG5xu7EEwH2JC8N0zUENSQrhv27lKzwCC1vqNF2qbGzg/uuMc+6dEkfMwUXmk1qBGtVxp0e8A/+9WN7+ctTVliwUw5aOCYc3oiA+BbqXk7yll8RD5s8kPlkgh++HQzZufu1lVffx5+ZKC0QH44OZrie5h/Oo3q6iq3LftX2+KlbgZpZxEipn/TkUMh9OyxCFApn78VEp9HvqoCeoQx0goLsXObaQOP3x+FFvcIu1Yjs9rCGdFceK7MJEY0MDlroEfNFptB2J+Iq3WBCCgxIGZwuT4hcAHOksykRWuZWuMHcdr3Z6Ars3odeGbOEEH12Mgpvqz8ecucpdpfT4v7siW4+bghomwdzJqABD0Hi5EM/kwFG2 4hsx9GC5 FD6LqZK/SvMRHu9wz17mZy6tvHLshY4mPv5T2ZGMjQcEQNjx0v+Uxao+yUdfhwicTYcNwjCANddq6An4NwiLsbGU2aE15trywoZrn+lUyZ4GrFeUFHQbYj6rgWjDoqul9SiYK5OB9OZnL1aMo9gAKDa5CGCPoddqw2BTqgmEPOE84Ppja61nA5YlZGSuhp8gsBaon 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: List-Subscribe: List-Unsubscribe: On Thu, Sep 12, 2024 at 11:50:18AM +0100, Will Deacon wrote: > Hi Dave, > > On Wed, Sep 11, 2024 at 08:33:54AM -0700, Dave Hansen wrote: > > On 9/11/24 08:01, Kevin Brodsky wrote: > > > On 22/08/2024 17:10, Joey Gouly wrote: > > >> @@ -371,6 +382,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) > > >> if (system_supports_tpidr2()) > > >> p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0); > > >> > > >> + if (system_supports_poe()) > > >> + p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); > > > Here we are only reloading POR_EL0's value if the target is a user > > > thread. However, as this series stands, POR_EL0 is also relevant to > > > kthreads, because any uaccess or GUP done from a kthread will also be > > > checked against POR_EL0. This is especially important in cases like the > > > io_uring kthread, which accesses the memory of the user process that > > > spawned it. To prevent such a kthread from inheriting a stale value of > > > POR_EL0, it seems that we should reload POR_EL0's value in all cases > > > (user and kernel thread). > > > > The problem with this is trying to figure out which POR_EL0 to use. The > > kthread could have been spawned ages ago and might not have a POR_EL0 > > which is very different from the current value of any of the threads in > > the process right now. > > > > There's also no great way for a kthread to reach out and grab an updated > > value. It's all completely inherently racy. > > > > > Other approaches could also be considered (e.g. resetting POR_EL0 to > > > unrestricted when creating a kthread), see my reply on v4 [1]. > > > > I kinda think this is the only way to go. It's the only sensible, > > predictable way. I _think_ it's what x86 will end up doing with PKRU, > > but there's been enough churn there that I'd need to go double check > > what happens in practice. > > I agree. > > > Either way, it would be nice to get an io_uring test in here that > > actually spawns kthreads: > > > > tools/testing/selftests/mm/protection_keys.c > > It would be good to update Documentation/core-api/protection-keys.rst > as well, since the example with read() raises more questions than it > answers! > > Kevin, Joey -- I've got this series queued in arm64 as-is, so perhaps > you could send some patches on top so we can iron this out in time for > 6.12? I'll also be at LPC next week if you're about. I found the code in arch/x86 that does this, I must have missed this previously. arch/x86/kernel/process.c: int copy_thread() /* Kernel thread ? */ if (unlikely(p->flags & PF_KTHREAD)) { p->thread.pkru = pkru_get_init_value(); memset(childregs, 0, sizeof(struct pt_regs)); kthread_frame_init(frame, args->fn, args->fn_arg); return 0; } I can send a similar patch for arm64. I have no idea how to write io_uring code, so looking for examples I can work with to get a test written. Might just send the arm64 fix first, if that's fine? Thanks, Joey