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 F01F3C3DA64 for ; Tue, 6 Aug 2024 14:31:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D0706B0095; Tue, 6 Aug 2024 10:31:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8808E6B0096; Tue, 6 Aug 2024 10:31:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 748596B0098; Tue, 6 Aug 2024 10:31:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5616B6B0095 for ; Tue, 6 Aug 2024 10:31:13 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0F9D0A4E79 for ; Tue, 6 Aug 2024 14:31:13 +0000 (UTC) X-FDA: 82422058026.23.63DC7FD Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf13.hostedemail.com (Postfix) with ESMTP id A1FC420037 for ; Tue, 6 Aug 2024 14:31:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1722954587; 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=cFkox0LHfnnekAIPWggAQCmlTgIP5kHTiylzdS4zknE=; b=1vGiAs4iPHSbGH5zaxLxkmb9EXBvPbEeI/QDmANIxuWZEyMMtnyM/eoGh+Nn1GDKyiMWYj xn01nWchbCy8RMthPBvfpHFtRX/3jm6So4SMMzQi/GcT/NotYGg8XPFAwUr+h2hFWcsmF6 fotDNSsRCgWdCgyxacBqXTYMMjTIXd8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722954587; a=rsa-sha256; cv=none; b=IG1jdo4VpSvBUzbAWABDA8DiAGGVB0rzXlMNRa3qs7j0+D05dqmBCqBc7rxCJ+yOB19VO3 bqDSY+EOEtiqGTiaHG1rvu7UQtoEqsHsuO7qGPRVB6VCgSLv8ihO4bwl99U/H24+k5kcNA IvMyYCaVt/iousfGMBKpJmAf5cmC0Lw= 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 3C5DEFEC; Tue, 6 Aug 2024 07:31:35 -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 E82AC3F6A8; Tue, 6 Aug 2024 07:31:05 -0700 (PDT) Date: Tue, 6 Aug 2024 15:31:03 +0100 From: Joey Gouly To: Dave Martin Cc: linux-arm-kernel@lists.infradead.org, akpm@linux-foundation.org, aneesh.kumar@kernel.org, aneesh.kumar@linux.ibm.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, szabolcs.nagy@arm.com, tglx@linutronix.de, will@kernel.org, x86@kernel.org, kvmarm@lists.linux.dev Subject: Re: [PATCH v4 18/29] arm64: add POE signal support Message-ID: <20240806143103.GB2017741@e124191.cambridge.arm.com> References: <20240503130147.1154804-1-joey.gouly@arm.com> <20240503130147.1154804-19-joey.gouly@arm.com> <20240801155441.GB841837@e124191.cambridge.arm.com> <20240806103532.GA1986436@e124191.cambridge.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240806103532.GA1986436@e124191.cambridge.arm.com> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A1FC420037 X-Stat-Signature: rek1shbd6igipjd9agxipu4xmwcswfsa X-Rspam-User: X-HE-Tag: 1722954670-521232 X-HE-Meta: U2FsdGVkX1949rQ9c0Yh+vzlusIFZz/d4PTiIPfaKIEp/8qxeExvkhfaBr9RSNnPzYKsTVK3xPzhUq9Yuh3A2H+EOaqw1gcOeAeeEePlt3O4rHFL3by77pEupz/rXj2hZcQ75WsPu5NfscCEt/KepynlSDeIUhauHXP3we7LoExgolu6npHR3b1B0Xuw6FKHQ8MNrPEkpuD1eznDY/IrZcul/LarBnBVdsaraaakbM6mG5BzYJW2E6qNLoQXMNvBozBgfJLjw7q0Hcrh3z+oNGO2VugwRttQ09kP8LhnW9giegzN528OflriMiiD1fWNEujzBr35TDTXNw3m+oTsUHtK3CrwQ1/gtMYx/fy8fPt/Up6N8fXZXHlWGa/Pmp0qAH6nUyt6krReRlzWFHzIg+pC9MkEjntSxamJKLP/BpIDSUsf+qJugylSxgyldB7BL/trhi/BfxeebXLicf1HnXdyAUQsuSAahTNVdx9A/IlZUqwhQ9F2qdaRf3nhVtYtmrCSAHeCVTVt5vldGbwoxZcA3SwE7xrQHX1pgkHIYQIGB+zR9CdwJ6Zk7r/YV5zFGSniBLOVZCmeFphcPmCPJfT8ErhOcO4npgtbrS1ODc7UZtiVCNDoBXth0EV29CRMWecpX6lurtCxrx57Aclwh+axsferU+EJWc4rEEQghJKo6I82aXf9xHvQ6fZ/zcBjNaq3lzFtgMWq7M9CMA8eEEBoaEe3IxvYNvjWBb83roWSCjq6HAVEDGPeffn62VEr/4GVJssPVUlxcpSjKkc4T7sLF/ZnDAK5jbwB4E3FfPusIZ8GOjdP+1beeN2W04wBSCgNgwSsV5AlW9Dq7w27qXvzqGxtiN69sEuhh2gcbtS6KSS0neweXWVXCbrALxU/k5eyJtMTsYwL4Y4oWP0QT1KCxHCSJcsLci2mybnTNQMu0Z9vIpIYA5o3LuxhDaCcZygQWq8YWB+TVRiyppl 91Ua7XUs NDfmCNPogMzhfV0fVyp/0oWmx85yA0oboOo6N4SEa6qfZkEbTzfp262i0ByBPPDKpNX9uH/mQ8VKSjOdtJOhdzanOYM/UnBVQt1jKpT4mV9tsbGT4JUUwaoFl3gmTlyZJ9zfesSjg+9JVccUnRuVLAeZyXOMQPliBQETjtHhuilv+WJRmBqW+DFj3aPZgbb8QYZWjkOJJRUVaMcvsOyRXF2H5LI92mbTQTvsRD4UUAhAVELAjZ4tIbhkf+UnwuTP58khCyh8lWxMs3NQ= 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 Tue, Aug 06, 2024 at 11:35:32AM +0100, Joey Gouly wrote: > On Thu, Aug 01, 2024 at 05:22:45PM +0100, Dave Martin wrote: > > On Thu, Aug 01, 2024 at 04:54:41PM +0100, Joey Gouly wrote: > > > On Thu, Jul 25, 2024 at 05:00:18PM +0100, Dave Martin wrote: > > > > Hi, > > > > > > > > On Fri, May 03, 2024 at 02:01:36PM +0100, Joey Gouly wrote: > > > > > Add PKEY support to signals, by saving and restoring POR_EL0 from the stackframe. > > > > > > > > > > Signed-off-by: Joey Gouly > > > > > Cc: Catalin Marinas > > > > > Cc: Will Deacon > > > > > Reviewed-by: Mark Brown > > > > > Acked-by: Szabolcs Nagy > > > > > --- > > > > > arch/arm64/include/uapi/asm/sigcontext.h | 7 ++++ > > > > > arch/arm64/kernel/signal.c | 52 ++++++++++++++++++++++++ > > > > > 2 files changed, 59 insertions(+) > > > > > > > > > > diff --git a/arch/arm64/include/uapi/asm/sigcontext.h b/arch/arm64/include/uapi/asm/sigcontext.h > > > > > index 8a45b7a411e0..e4cba8a6c9a2 100644 > > > > > --- a/arch/arm64/include/uapi/asm/sigcontext.h > > > > > +++ b/arch/arm64/include/uapi/asm/sigcontext.h > > > > > > > > [...] > > > > > > > > > @@ -980,6 +1013,13 @@ static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, > > > > > return err; > > > > > } > > > > > > > > > > + if (system_supports_poe()) { > > > > > + err = sigframe_alloc(user, &user->poe_offset, > > > > > + sizeof(struct poe_context)); > > > > > + if (err) > > > > > + return err; > > > > > + } > > > > > + > > > > > return sigframe_alloc_end(user); > > > > > } > > > > > > > > > > @@ -1020,6 +1060,15 @@ static int setup_sigframe(struct rt_sigframe_user_layout *user, > > > > > __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); > > > > > } > > > > > > > > > > + if (system_supports_poe() && err == 0 && user->poe_offset) { > > > > > + struct poe_context __user *poe_ctx = > > > > > + apply_user_offset(user, user->poe_offset); > > > > > + > > > > > + __put_user_error(POE_MAGIC, &poe_ctx->head.magic, err); > > > > > + __put_user_error(sizeof(*poe_ctx), &poe_ctx->head.size, err); > > > > > + __put_user_error(read_sysreg_s(SYS_POR_EL0), &poe_ctx->por_el0, err); > > > > > + } > > > > > + > > > > > > > > Does the AArch64 procedure call standard say anything about whether > > > > POR_EL0 is caller-saved? > > > > > > I asked about this, and it doesn't say anything and they don't plan on it, > > > since it's very application specific. > > > > Right. I think that confirms that we don't absolutely need to preserve > > POR_EL0, because if compiler-generated code was allowed to fiddle with > > this and not clean up after itself, the PCS would have to document this. > > > > > > > > > > > > > > > > > > In theory we could skip saving this register if it is already > > > > POR_EL0_INIT (which it often will be), and if the signal handler is not > > > > supposed to modify and leave the modified value in the register when > > > > returning. > > > > > > > > The complexity of the additional check my be a bit pointless though, > > > > and the the handler might theoretically want to change the interrupted > > > > code's POR_EL0 explicitly, which would be complicated if POE_MAGIC is > > > > sometimes there and sometimes not. > > > > > > > > > > > > > > > I think trying to skip/optimise something here would be more effort than any > > > possible benefits! > > > > Actually, having thought about this some more I think that only dumping > > this register if != POR_EL0_INIT may be right right thing to do. > > > > This would mean that old binary would stacks never see poe_context in > > the signal frame, and so will never experience unexpected stack > > overruns (at least, not due solely to the presence of this feature). > > They can already see things they don't expect, like FPMR that was added > recently. > > > > > POE-aware signal handlers have to do something fiddly and nonportable > > to obtain the original value of POR_EL0 regardless, so requiring them > > do handle both cases (present in sigframe and absent) doesn't seem too > > onerous to me. > > If the signal handler wanted to modify the value, from the default, wouldn't it > need to mess around with the sig context stuff, to allocate some space for > POR_EL0, such that the kernel would restore it properly? (If that's even > possible). > > > > > > > Do you think this approach would break any known use cases? > > Not sure. > We talked about this offline, helped me understand it more, and I think something like this makes sense: diff --git arch/arm64/kernel/signal.c arch/arm64/kernel/signal.c index 561986947530..ca7d4e0be275 100644 --- arch/arm64/kernel/signal.c +++ arch/arm64/kernel/signal.c @@ -1024,7 +1025,10 @@ static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, return err; } - if (system_supports_poe()) { + if (system_supports_poe() && + (add_all || + mm_pkey_allocation_map(current->mm) != 0x1 || + read_sysreg_s(SYS_POR_EL0) != POR_EL0_INIT)) { err = sigframe_alloc(user, &user->poe_offset, sizeof(struct poe_context)); if (err) That is, we only save the POR_EL0 value if any pkeys have been allocated (other than pkey 0) *or* if POR_EL0 is a non-default value. The latter case is a corner case, where a userspace would have changed POR_EL0 before allocating any extra pkeys. That could be: - pkey 0, if it restricts pkey 0 without allocating other pkeys, it's unlikely the program can do anything useful anyway - Another pkey, which userspace probably shouldn't do anyway. The man pages say: The kernel guarantees that the contents of the hardware rights register (PKRU) will be preserved only for allocated protection keys. Any time a key is unallocated (either before the first call returning that key from pkey_alloc() or after it is freed via pkey_free()), the kernel may make arbitrary changes to the parts of the rights register affecting access to that key. So userspace shouldn't be changing POR_EL0 before allocating pkeys anyway.. Thanks, Joey