* [PATCH 0/4] POE sigreturn fix and extra tests
@ 2026-04-21 14:42 Kevin Brodsky
2026-04-21 14:42 ` [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing Kevin Brodsky
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-21 14:42 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Kevin Brodsky, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, Will Deacon, linux-kselftest
Commit 2e8a1acea859 ("arm64: signal: Improve POR_EL0 handling to
avoid uaccess failures") introduced special handling for EL0 registers
that impact uaccess. This did not however handle the case where a signal
handler removes the relevant record (poe_context for POE) from the
signal frame; this is clearly not typical behaviour but it is legal.
That commit resulted in arbitrary data from the kernel stack being
written to POR_EL0 in that case.
Patch 1 fixes this by tracking which fields in struct user_access_state
are actually valid. This restores the original behaviour, where POR_EL0
is left untouched if poe_context is removed.
The remaining patches add new tests to the arm64 signal kselftests to
check that POR_EL0 is reset and restored (or preserved) as expected.
---
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Joey Gouly <joey.gouly@arm.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Will Deacon <will@kernel.org>
Cc: linux-kselftest@vger.kernel.org
---
Kevin Brodsky (4):
arm64: signal: Preserve POR_EL0 if poe_context is missing
kselftest/arm64: Add POE as a feature in the signal tests
kselftest/arm64: Add POE helpers to test_signals_utils.h
kselftest/arm64: Add tests for POR_EL0 save/reset/restore
arch/arm64/kernel/signal.c | 19 +++--
.../selftests/arm64/signal/test_signals.h | 2 +
.../arm64/signal/test_signals_utils.c | 3 +
.../arm64/signal/test_signals_utils.h | 16 ++++
.../testcases/poe_missing_poe_context.c | 73 +++++++++++++++++++
.../arm64/signal/testcases/poe_restore.c | 64 ++++++++++++++++
.../arm64/signal/testcases/poe_siginfo.c | 15 ----
7 files changed, 172 insertions(+), 20 deletions(-)
create mode 100644 tools/testing/selftests/arm64/signal/testcases/poe_missing_poe_context.c
create mode 100644 tools/testing/selftests/arm64/signal/testcases/poe_restore.c
base-commit: 028ef9c96e96197026887c0f092424679298aae8
--
2.51.2
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing
2026-04-21 14:42 [PATCH 0/4] POE sigreturn fix and extra tests Kevin Brodsky
@ 2026-04-21 14:42 ` Kevin Brodsky
2026-04-22 12:19 ` Will Deacon
2026-04-21 14:42 ` [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests Kevin Brodsky
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-21 14:42 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Kevin Brodsky, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, Will Deacon, linux-kselftest
Commit 2e8a1acea859 ("arm64: signal: Improve POR_EL0 handling to
avoid uaccess failures") delayed the write to POR_EL0 in
rt_sigreturn to avoid spurious uaccess failures. This change however
relies on the poe_context frame record being present: on a system
supporting POE, calling sigreturn without a poe_context record now
results in writing arbitrary data from the kernel stack into POR_EL0.
Fix this by adding a valid_fields member to struct
user_access_state, and zeroing the struct on allocation.
restore_poe_context() then indicates that the por_el0 field is valid
by setting the corresponding bit in valid_fields, and
restore_user_access_state() only touches POR_EL0 if there is a valid
value to set it to. This is in line with how POR_EL0 was originally
handled; all frame records are currently optional, except
fpsimd_context.
restore_user_access_state() is also called if setting up the signal
frame fails, so we also initialise valid_fields in that case. For
consistency, setup_sigframe() now also checks valid_fields to decide
whether to write a poe_context record, avoiding another call to
system_supports_poe().
Fixes: 2e8a1acea859 ("arm64: signal: Improve POR_EL0 handling to avoid uaccess failures")
Reported-by: Will Deacon <will@kernel.org>
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
---
arch/arm64/kernel/signal.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 08ffc5a5aea4..3f17aed5b4f0 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -67,6 +67,8 @@ struct rt_sigframe_user_layout {
unsigned long end_offset;
};
+#define UA_STATE_HAS_POR_EL0 BIT(0)
+
/*
* Holds any EL0-controlled state that influences unprivileged memory accesses.
* This includes both accesses done in userspace and uaccess done in the kernel.
@@ -74,8 +76,12 @@ struct rt_sigframe_user_layout {
* This state needs to be carefully managed to ensure that it doesn't cause
* uaccess to fail when setting up the signal frame, and the signal handler
* itself also expects a well-defined state when entered.
+ *
+ * The valid_fields member is a bitfield (see UA_STATE_HAS_*), specifying which
+ * of the remaining fields is valid (has been set to a value).
*/
struct user_access_state {
+ unsigned int valid_fields;
u64 por_el0;
};
@@ -95,6 +101,7 @@ static void save_reset_user_access_state(struct user_access_state *ua_state)
por_enable_all |= POR_ELx_PERM_PREP(pkey, POE_RWX);
ua_state->por_el0 = read_sysreg_s(SYS_POR_EL0);
+ ua_state->valid_fields |= UA_STATE_HAS_POR_EL0;
write_sysreg_s(por_enable_all, SYS_POR_EL0);
/*
* No ISB required as we can tolerate spurious Overlay faults -
@@ -122,7 +129,7 @@ static void set_handler_user_access_state(void)
*/
static void restore_user_access_state(const struct user_access_state *ua_state)
{
- if (system_supports_poe())
+ if (ua_state->valid_fields & UA_STATE_HAS_POR_EL0)
write_sysreg_s(ua_state->por_el0, SYS_POR_EL0);
}
@@ -352,8 +359,10 @@ static int restore_poe_context(struct user_ctxs *user,
return -EINVAL;
__get_user_error(por_el0, &(user->poe->por_el0), err);
- if (!err)
+ if (!err) {
ua_state->por_el0 = por_el0;
+ ua_state->valid_fields |= UA_STATE_HAS_POR_EL0;
+ }
return err;
}
@@ -1095,7 +1104,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
{
struct pt_regs *regs = current_pt_regs();
struct rt_sigframe __user *frame;
- struct user_access_state ua_state;
+ struct user_access_state ua_state = {0};
/* Always make any pending restarted system calls return -EINTR */
current->restart_block.fn = do_no_restart_syscall;
@@ -1302,7 +1311,7 @@ static int setup_sigframe(struct rt_sigframe_user_layout *user,
err |= preserve_fpmr_context(fpmr_ctx);
}
- if (system_supports_poe() && err == 0) {
+ if ((ua_state->valid_fields & UA_STATE_HAS_POR_EL0) && err == 0) {
struct poe_context __user *poe_ctx =
apply_user_offset(user, user->poe_offset);
@@ -1507,7 +1516,7 @@ static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
{
struct rt_sigframe_user_layout user;
struct rt_sigframe __user *frame;
- struct user_access_state ua_state;
+ struct user_access_state ua_state = {0};
int err = 0;
fpsimd_save_and_flush_current_state();
--
2.51.2
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests
2026-04-21 14:42 [PATCH 0/4] POE sigreturn fix and extra tests Kevin Brodsky
2026-04-21 14:42 ` [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing Kevin Brodsky
@ 2026-04-21 14:42 ` Kevin Brodsky
2026-04-21 14:58 ` Mark Brown
2026-04-21 14:42 ` [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h Kevin Brodsky
2026-04-21 14:42 ` [PATCH 4/4] kselftest/arm64: Add tests for POR_EL0 save/reset/restore Kevin Brodsky
3 siblings, 1 reply; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-21 14:42 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Kevin Brodsky, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, Will Deacon, linux-kselftest
Add the POE feature to the signal tests framework, to allow tests to
require it.
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
---
tools/testing/selftests/arm64/signal/test_signals.h | 2 ++
tools/testing/selftests/arm64/signal/test_signals_utils.c | 3 +++
2 files changed, 5 insertions(+)
diff --git a/tools/testing/selftests/arm64/signal/test_signals.h b/tools/testing/selftests/arm64/signal/test_signals.h
index ee75a2c25ce7..c7c343494cb8 100644
--- a/tools/testing/selftests/arm64/signal/test_signals.h
+++ b/tools/testing/selftests/arm64/signal/test_signals.h
@@ -36,6 +36,7 @@ enum {
FSME_FA64_BIT,
FSME2_BIT,
FGCS_BIT,
+ FPOE_BIT,
FMAX_END
};
@@ -45,6 +46,7 @@ enum {
#define FEAT_SME_FA64 (1UL << FSME_FA64_BIT)
#define FEAT_SME2 (1UL << FSME2_BIT)
#define FEAT_GCS (1UL << FGCS_BIT)
+#define FEAT_POE (1UL << FPOE_BIT)
/*
* A descriptor used to describe and configure a test case.
diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.c b/tools/testing/selftests/arm64/signal/test_signals_utils.c
index 5d3621921cfe..4b12dbd7669d 100644
--- a/tools/testing/selftests/arm64/signal/test_signals_utils.c
+++ b/tools/testing/selftests/arm64/signal/test_signals_utils.c
@@ -31,6 +31,7 @@ static char const *const feats_names[FMAX_END] = {
" FA64 ",
" SME2 ",
" GCS ",
+ " POE ",
};
#define MAX_FEATS_SZ 128
@@ -341,6 +342,8 @@ int test_init(struct tdescr *td)
td->feats_supported |= FEAT_SME2;
if (getauxval(AT_HWCAP) & HWCAP_GCS)
td->feats_supported |= FEAT_GCS;
+ if (getauxval(AT_HWCAP2) & HWCAP2_POE)
+ td->feats_supported |= FEAT_POE;
if (feats_ok(td)) {
if (td->feats_required & td->feats_supported)
fprintf(stderr,
--
2.51.2
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h
2026-04-21 14:42 [PATCH 0/4] POE sigreturn fix and extra tests Kevin Brodsky
2026-04-21 14:42 ` [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing Kevin Brodsky
2026-04-21 14:42 ` [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests Kevin Brodsky
@ 2026-04-21 14:42 ` Kevin Brodsky
2026-04-21 15:00 ` Mark Brown
2026-04-21 14:42 ` [PATCH 4/4] kselftest/arm64: Add tests for POR_EL0 save/reset/restore Kevin Brodsky
3 siblings, 1 reply; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-21 14:42 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Kevin Brodsky, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, Will Deacon, linux-kselftest
In preparation to adding further POE signal tests, move
get_por_el0() to test_signals_utils.h and add set_por_el0().
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
---
.../selftests/arm64/signal/test_signals_utils.h | 16 ++++++++++++++++
.../arm64/signal/testcases/poe_siginfo.c | 15 ---------------
2 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h
index 36fc12b3cd60..2c7b8c64a35a 100644
--- a/tools/testing/selftests/arm64/signal/test_signals_utils.h
+++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h
@@ -57,6 +57,22 @@ static inline __attribute__((always_inline)) uint64_t get_gcspr_el0(void)
return val;
}
+#define SYS_POR_EL0 "S3_3_C10_C2_4"
+
+static inline uint64_t get_por_el0(void)
+{
+ uint64_t val;
+
+ asm volatile("mrs %0, " SYS_POR_EL0 "\n" : "=r"(val));
+
+ return val;
+}
+
+static inline void set_por_el0(uint64_t val)
+{
+ asm volatile("msr " SYS_POR_EL0 ", %0\n" :: "r"(val));
+}
+
static inline bool feats_ok(struct tdescr *td)
{
if (td->feats_incompatible & td->feats_supported)
diff --git a/tools/testing/selftests/arm64/signal/testcases/poe_siginfo.c b/tools/testing/selftests/arm64/signal/testcases/poe_siginfo.c
index 36bd9940ee05..e15fedf4da6e 100644
--- a/tools/testing/selftests/arm64/signal/testcases/poe_siginfo.c
+++ b/tools/testing/selftests/arm64/signal/testcases/poe_siginfo.c
@@ -21,21 +21,6 @@ static union {
char buf[1024 * 128];
} context;
-#define SYS_POR_EL0 "S3_3_C10_C2_4"
-
-static uint64_t get_por_el0(void)
-{
- uint64_t val;
-
- asm volatile(
- "mrs %0, " SYS_POR_EL0 "\n"
- : "=r"(val)
- :
- : );
-
- return val;
-}
-
int poe_present(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
{
struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
--
2.51.2
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 4/4] kselftest/arm64: Add tests for POR_EL0 save/reset/restore
2026-04-21 14:42 [PATCH 0/4] POE sigreturn fix and extra tests Kevin Brodsky
` (2 preceding siblings ...)
2026-04-21 14:42 ` [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h Kevin Brodsky
@ 2026-04-21 14:42 ` Kevin Brodsky
3 siblings, 0 replies; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-21 14:42 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Kevin Brodsky, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, Will Deacon, linux-kselftest
POR_EL0 is expected to be:
- Saved in the poe_context record
- Reset to POR_EL0_INIT when invoking the signal handler
- Restored from poe_context when returning from the signal handler
Add a new test, poe_restore, to check that the save/reset/restore
mechanism is working as intended. See commit 2e8a1acea859 ("arm64:
signal: Improve POR_EL0 handling to avoid uaccess failures") for
more details.
This commit did not handle the case where poe_context is missing
correctly. This was recently fixed; add a new test,
poe_missing_poe_context, to check this case.
Note: td->pass is only set to true at the very end, as an unexpected
signal may occur in case of failure (especially in
poe_missing_poe_context if POR_EL0 is restored to an invalid value).
Failures are tracked with a global, failed_check.
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
---
.../testcases/poe_missing_poe_context.c | 73 +++++++++++++++++++
.../arm64/signal/testcases/poe_restore.c | 64 ++++++++++++++++
2 files changed, 137 insertions(+)
create mode 100644 tools/testing/selftests/arm64/signal/testcases/poe_missing_poe_context.c
create mode 100644 tools/testing/selftests/arm64/signal/testcases/poe_restore.c
diff --git a/tools/testing/selftests/arm64/signal/testcases/poe_missing_poe_context.c b/tools/testing/selftests/arm64/signal/testcases/poe_missing_poe_context.c
new file mode 100644
index 000000000000..abab7400d9df
--- /dev/null
+++ b/tools/testing/selftests/arm64/signal/testcases/poe_missing_poe_context.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Arm Ltd
+ *
+ * Verify that the POR_EL0 register is left untouched on sigreturn if the
+ * POE frame record is missing.
+ */
+
+#include <asm/sigcontext.h>
+
+#include "test_signals_utils.h"
+#include "testcases.h"
+
+#define POR_EL0_INIT 0x07ul
+#define POR_EL0_CUSTOM 0x77ul
+
+static bool failed_check;
+
+static bool modify_por_el0(struct tdescr *td)
+{
+ set_por_el0(POR_EL0_CUSTOM);
+
+ return true;
+}
+
+static int signal_remove_poe_context(struct tdescr *td, siginfo_t *si,
+ ucontext_t *uc)
+{
+ struct _aarch64_ctx *ctx = GET_UC_RESV_HEAD(uc);
+ struct _aarch64_ctx *poe_ctx_head;
+
+ poe_ctx_head = get_header(ctx, POE_MAGIC, sizeof(uc->uc_mcontext),
+ NULL);
+ if (!poe_ctx_head) {
+ fprintf(stderr, "Missing poe_context record\n");
+ failed_check = true;
+ return 0;
+ }
+
+ /*
+ * Actually removing the record would require moving down the next
+ * records. An easier option is to turn it into an ESR record, which is
+ * ignored by sigreturn().
+ */
+ poe_ctx_head->magic = ESR_MAGIC;
+
+ return 0;
+}
+
+static void check_por_el0_preserved(struct tdescr *td)
+{
+ uint64_t por_el0 = get_por_el0();
+
+ if (por_el0 == POR_EL0_INIT) {
+ fprintf(stderr, "POR_EL0 preserved\n");
+ } else {
+ fprintf(stderr, "POR_EL0 unexpectedly set to %lx\n", por_el0);
+ failed_check = true;
+ }
+
+ td->pass = !failed_check;
+}
+
+struct tdescr tde = {
+ .name = "POR_EL0 missing poe_context",
+ .descr = "Remove poe_context record and check POR_EL0 is preserved",
+ .feats_required = FEAT_POE,
+ .timeout = 3,
+ .sig_trig = SIGUSR1,
+ .init = modify_por_el0,
+ .run = signal_remove_poe_context,
+ .check_result = check_por_el0_preserved,
+};
diff --git a/tools/testing/selftests/arm64/signal/testcases/poe_restore.c b/tools/testing/selftests/arm64/signal/testcases/poe_restore.c
new file mode 100644
index 000000000000..9f9a61a4214d
--- /dev/null
+++ b/tools/testing/selftests/arm64/signal/testcases/poe_restore.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Arm Ltd
+ *
+ * Verify that the POR_EL0 register is saved and restored as expected on signal
+ * entry/return.
+ */
+
+#include <asm/sigcontext.h>
+
+#include "test_signals_utils.h"
+#include "testcases.h"
+
+#define POR_EL0_INIT 0x07ul
+#define POR_EL0_CUSTOM 0x77ul
+
+static bool failed_check;
+
+static bool modify_por_el0(struct tdescr *td)
+{
+ set_por_el0(POR_EL0_CUSTOM);
+
+ return true;
+}
+
+static int signal_check_por_el0_reset(struct tdescr *td, siginfo_t *si,
+ ucontext_t *uc)
+{
+ uint64_t signal_por_el0 = get_por_el0();
+
+ if (signal_por_el0 != POR_EL0_INIT) {
+ fprintf(stderr, "POR_EL0 is %lx in signal handler (expected %lx)\n",
+ signal_por_el0, POR_EL0_INIT);
+ failed_check = true;
+ }
+
+ return 0;
+}
+
+static void check_por_el0_restored(struct tdescr *td)
+{
+ uint64_t por_el0 = get_por_el0();
+
+ if (por_el0 == POR_EL0_CUSTOM) {
+ fprintf(stderr, "POR_EL0 restored\n");
+ } else {
+ fprintf(stderr, "POR_EL0 was %lx but is now %lx\n",
+ POR_EL0_CUSTOM, por_el0);
+ failed_check = true;
+ }
+
+ td->pass = !failed_check;
+}
+
+struct tdescr tde = {
+ .name = "POR_EL0 restore",
+ .descr = "Validate that POR_EL0 is saved/restored on signal entry/return",
+ .feats_required = FEAT_POE,
+ .timeout = 3,
+ .sig_trig = SIGUSR1,
+ .init = modify_por_el0,
+ .run = signal_check_por_el0_reset,
+ .check_result = check_por_el0_restored,
+};
--
2.51.2
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests
2026-04-21 14:42 ` [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests Kevin Brodsky
@ 2026-04-21 14:58 ` Mark Brown
0 siblings, 0 replies; 11+ messages in thread
From: Mark Brown @ 2026-04-21 14:58 UTC (permalink / raw)
To: Kevin Brodsky
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Shuah Khan, Will Deacon, linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 194 bytes --]
On Tue, Apr 21, 2026 at 03:42:50PM +0100, Kevin Brodsky wrote:
> Add the POE feature to the signal tests framework, to allow tests to
> require it.
Reviewed-by: Mark Brown <broonie@kernel.org>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h
2026-04-21 14:42 ` [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h Kevin Brodsky
@ 2026-04-21 15:00 ` Mark Brown
0 siblings, 0 replies; 11+ messages in thread
From: Mark Brown @ 2026-04-21 15:00 UTC (permalink / raw)
To: Kevin Brodsky
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Shuah Khan, Will Deacon, linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 305 bytes --]
On Tue, Apr 21, 2026 at 03:42:51PM +0100, Kevin Brodsky wrote:
> In preparation to adding further POE signal tests, move
> get_por_el0() to test_signals_utils.h and add set_por_el0().
Subject line should probably also say move if there's a v2 but otherwise
Reviewed-by: Mark Brown <broonie@kernel.org>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing
2026-04-21 14:42 ` [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing Kevin Brodsky
@ 2026-04-22 12:19 ` Will Deacon
2026-04-22 14:55 ` Kevin Brodsky
0 siblings, 1 reply; 11+ messages in thread
From: Will Deacon @ 2026-04-22 12:19 UTC (permalink / raw)
To: Kevin Brodsky
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, linux-kselftest
Hey Kevin,
On Tue, Apr 21, 2026 at 03:42:49PM +0100, Kevin Brodsky wrote:
> Commit 2e8a1acea859 ("arm64: signal: Improve POR_EL0 handling to
> avoid uaccess failures") delayed the write to POR_EL0 in
> rt_sigreturn to avoid spurious uaccess failures. This change however
> relies on the poe_context frame record being present: on a system
> supporting POE, calling sigreturn without a poe_context record now
> results in writing arbitrary data from the kernel stack into POR_EL0.
>
> Fix this by adding a valid_fields member to struct
> user_access_state, and zeroing the struct on allocation.
> restore_poe_context() then indicates that the por_el0 field is valid
> by setting the corresponding bit in valid_fields, and
> restore_user_access_state() only touches POR_EL0 if there is a valid
> value to set it to. This is in line with how POR_EL0 was originally
> handled; all frame records are currently optional, except
> fpsimd_context.
>
> restore_user_access_state() is also called if setting up the signal
> frame fails, so we also initialise valid_fields in that case. For
> consistency, setup_sigframe() now also checks valid_fields to decide
> whether to write a poe_context record, avoiding another call to
> system_supports_poe().
>
> Fixes: 2e8a1acea859 ("arm64: signal: Improve POR_EL0 handling to avoid uaccess failures")
> Reported-by: Will Deacon <will@kernel.org>
> Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Thanks for fixing this. I think your patch is correct, but I have a
couple of comments inline. Please let me know what you think.
> ---
> arch/arm64/kernel/signal.c | 19 ++++++++++++++-----
> 1 file changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
> index 08ffc5a5aea4..3f17aed5b4f0 100644
> --- a/arch/arm64/kernel/signal.c
> +++ b/arch/arm64/kernel/signal.c
> @@ -67,6 +67,8 @@ struct rt_sigframe_user_layout {
> unsigned long end_offset;
> };
>
> +#define UA_STATE_HAS_POR_EL0 BIT(0)
> +
> /*
> * Holds any EL0-controlled state that influences unprivileged memory accesses.
> * This includes both accesses done in userspace and uaccess done in the kernel.
> @@ -74,8 +76,12 @@ struct rt_sigframe_user_layout {
> * This state needs to be carefully managed to ensure that it doesn't cause
> * uaccess to fail when setting up the signal frame, and the signal handler
> * itself also expects a well-defined state when entered.
> + *
> + * The valid_fields member is a bitfield (see UA_STATE_HAS_*), specifying which
> + * of the remaining fields is valid (has been set to a value).
> */
> struct user_access_state {
> + unsigned int valid_fields;
> u64 por_el0;
> };
Do you think it would be worth adding some accessors to make it easier
to keep the flags in sync? For example:
/* Stores por_el0 into uas->por_el0 and sets UA_STATE_HAS_POR_EL0 */
void set_ua_state_por_el0(struct user_access_state *uas, u64 por_el0);
/*
* If UA_STATE_HAS_POR_EL0, *por_el0 = uas->por_el0 and return 0.
* Otherwise, return -ENOENT.
*/
int get_ua_state_por_el0(struct user_access_state *uas, u64 *por_el0);
WDYT?
> @@ -1095,7 +1104,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
> {
> struct pt_regs *regs = current_pt_regs();
> struct rt_sigframe __user *frame;
> - struct user_access_state ua_state;
> + struct user_access_state ua_state = {0};
nit: {} should do (no need for the '0'). Same in setup_rt_frame().
Will
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing
2026-04-22 12:19 ` Will Deacon
@ 2026-04-22 14:55 ` Kevin Brodsky
2026-04-23 12:41 ` Will Deacon
0 siblings, 1 reply; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-22 14:55 UTC (permalink / raw)
To: Will Deacon
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, linux-kselftest
On 22/04/2026 14:19, Will Deacon wrote:
>> @@ -74,8 +76,12 @@ struct rt_sigframe_user_layout {
>> * This state needs to be carefully managed to ensure that it doesn't cause
>> * uaccess to fail when setting up the signal frame, and the signal handler
>> * itself also expects a well-defined state when entered.
>> + *
>> + * The valid_fields member is a bitfield (see UA_STATE_HAS_*), specifying which
>> + * of the remaining fields is valid (has been set to a value).
>> */
>> struct user_access_state {
>> + unsigned int valid_fields;
>> u64 por_el0;
>> };
> Do you think it would be worth adding some accessors to make it easier
> to keep the flags in sync? For example:
>
> /* Stores por_el0 into uas->por_el0 and sets UA_STATE_HAS_POR_EL0 */
> void set_ua_state_por_el0(struct user_access_state *uas, u64 por_el0);
>
> /*
> * If UA_STATE_HAS_POR_EL0, *por_el0 = uas->por_el0 and return 0.
> * Otherwise, return -ENOENT.
> */
> int get_ua_state_por_el0(struct user_access_state *uas, u64 *por_el0);
>
> WDYT?
I did get a feeling having helpers would be a good idea. I wonder if
getters/setters aren't a bit overkill though, as they make accesses to
the struct more cumbersome and we'd need a pair for every member (unless
we use some macro magic). Maybe it would be sufficient to have say
ua_state_has_field(POR_EL0) to check if the bit is set, and
ua_state_set_field_valid(POR_EL0) to set the bit?
>> @@ -1095,7 +1104,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
>> {
>> struct pt_regs *regs = current_pt_regs();
>> struct rt_sigframe __user *frame;
>> - struct user_access_state ua_state;
>> + struct user_access_state ua_state = {0};
> nit: {} should do (no need for the '0'). Same in setup_rt_frame().
Will change it, I have some vague recollection that GCC and Clang
disagreed about the meaning of {}... but that's probably fixed nowadays.
Thanks for the review!
- Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing
2026-04-22 14:55 ` Kevin Brodsky
@ 2026-04-23 12:41 ` Will Deacon
2026-04-24 9:24 ` Kevin Brodsky
0 siblings, 1 reply; 11+ messages in thread
From: Will Deacon @ 2026-04-23 12:41 UTC (permalink / raw)
To: Kevin Brodsky
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, linux-kselftest
On Wed, Apr 22, 2026 at 04:55:05PM +0200, Kevin Brodsky wrote:
> On 22/04/2026 14:19, Will Deacon wrote:
> >> @@ -74,8 +76,12 @@ struct rt_sigframe_user_layout {
> >> * This state needs to be carefully managed to ensure that it doesn't cause
> >> * uaccess to fail when setting up the signal frame, and the signal handler
> >> * itself also expects a well-defined state when entered.
> >> + *
> >> + * The valid_fields member is a bitfield (see UA_STATE_HAS_*), specifying which
> >> + * of the remaining fields is valid (has been set to a value).
> >> */
> >> struct user_access_state {
> >> + unsigned int valid_fields;
> >> u64 por_el0;
> >> };
> > Do you think it would be worth adding some accessors to make it easier
> > to keep the flags in sync? For example:
> >
> > /* Stores por_el0 into uas->por_el0 and sets UA_STATE_HAS_POR_EL0 */
> > void set_ua_state_por_el0(struct user_access_state *uas, u64 por_el0);
> >
> > /*
> > * If UA_STATE_HAS_POR_EL0, *por_el0 = uas->por_el0 and return 0.
> > * Otherwise, return -ENOENT.
> > */
> > int get_ua_state_por_el0(struct user_access_state *uas, u64 *por_el0);
> >
> > WDYT?
>
> I did get a feeling having helpers would be a good idea. I wonder if
> getters/setters aren't a bit overkill though, as they make accesses to
> the struct more cumbersome and we'd need a pair for every member (unless
> we use some macro magic).
We only have one struct member, so it's probably fine for now, and we
could group related members together in sub-structures to help in future.
But it's up to you -- I don't feel strongly about it, but requiring the
caller to update the flag manually is going to be a bug magnet.
> Maybe it would be sufficient to have say
> ua_state_has_field(POR_EL0) to check if the bit is set, and
> ua_state_set_field_valid(POR_EL0) to set the bit?
I don't think that really helps with my concern. I'd like to avoid callers
having to remember to deal with the flags when they update the data.
Will
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing
2026-04-23 12:41 ` Will Deacon
@ 2026-04-24 9:24 ` Kevin Brodsky
0 siblings, 0 replies; 11+ messages in thread
From: Kevin Brodsky @ 2026-04-24 9:24 UTC (permalink / raw)
To: Will Deacon
Cc: linux-arm-kernel, linux-kernel, Catalin Marinas, Joey Gouly,
Mark Brown, Shuah Khan, linux-kselftest
On 23/04/2026 14:41, Will Deacon wrote:
> On Wed, Apr 22, 2026 at 04:55:05PM +0200, Kevin Brodsky wrote:
>> On 22/04/2026 14:19, Will Deacon wrote:
>>>> @@ -74,8 +76,12 @@ struct rt_sigframe_user_layout {
>>>> * This state needs to be carefully managed to ensure that it doesn't cause
>>>> * uaccess to fail when setting up the signal frame, and the signal handler
>>>> * itself also expects a well-defined state when entered.
>>>> + *
>>>> + * The valid_fields member is a bitfield (see UA_STATE_HAS_*), specifying which
>>>> + * of the remaining fields is valid (has been set to a value).
>>>> */
>>>> struct user_access_state {
>>>> + unsigned int valid_fields;
>>>> u64 por_el0;
>>>> };
>>> Do you think it would be worth adding some accessors to make it easier
>>> to keep the flags in sync? For example:
>>>
>>> /* Stores por_el0 into uas->por_el0 and sets UA_STATE_HAS_POR_EL0 */
>>> void set_ua_state_por_el0(struct user_access_state *uas, u64 por_el0);
>>>
>>> /*
>>> * If UA_STATE_HAS_POR_EL0, *por_el0 = uas->por_el0 and return 0.
>>> * Otherwise, return -ENOENT.
>>> */
>>> int get_ua_state_por_el0(struct user_access_state *uas, u64 *por_el0);
>>>
>>> WDYT?
>> I did get a feeling having helpers would be a good idea. I wonder if
>> getters/setters aren't a bit overkill though, as they make accesses to
>> the struct more cumbersome and we'd need a pair for every member (unless
>> we use some macro magic).
> We only have one struct member, so it's probably fine for now, and we
> could group related members together in sub-structures to help in future.
> But it's up to you -- I don't feel strongly about it, but requiring the
> caller to update the flag manually is going to be a bug magnet.
>
>> Maybe it would be sufficient to have say
>> ua_state_has_field(POR_EL0) to check if the bit is set, and
>> ua_state_set_field_valid(POR_EL0) to set the bit?
> I don't think that really helps with my concern. I'd like to avoid callers
> having to remember to deal with the flags when they update the data.
Got it. I can't think of a better way to do this so I'll just go ahead
with your suggestion, and prefix all members of the struct with __ to
make it clear they shouldn't be accessed directly. The macro magic can
wait until we have more than one struct member ;)
I've also reconsidered setup_sigframe() and realised using valid_fields
there is not a great idea, because the record is allocated in
setup_sigframe_layout() based on system_supports_poe(), and leaving a
record uninitialised would be bad™. I'll remove that change and I'll
have preserve_poe_context() fail with a WARN_ON() if
get_ua_state_por_el0() somehow returns an error.
- Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-04-24 9:24 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-21 14:42 [PATCH 0/4] POE sigreturn fix and extra tests Kevin Brodsky
2026-04-21 14:42 ` [PATCH 1/4] arm64: signal: Preserve POR_EL0 if poe_context is missing Kevin Brodsky
2026-04-22 12:19 ` Will Deacon
2026-04-22 14:55 ` Kevin Brodsky
2026-04-23 12:41 ` Will Deacon
2026-04-24 9:24 ` Kevin Brodsky
2026-04-21 14:42 ` [PATCH 2/4] kselftest/arm64: Add POE as a feature in the signal tests Kevin Brodsky
2026-04-21 14:58 ` Mark Brown
2026-04-21 14:42 ` [PATCH 3/4] kselftest/arm64: Add POE helpers to test_signals_utils.h Kevin Brodsky
2026-04-21 15:00 ` Mark Brown
2026-04-21 14:42 ` [PATCH 4/4] kselftest/arm64: Add tests for POR_EL0 save/reset/restore Kevin Brodsky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox