Skip to contentSkip to conversation

arm64: mte: allow async MTE to be upgraded to sync on a per-CPU basis

PATCH Superseded Peter Collingbourne 2 participants 16 messages Jul 2, 2021 – Jul 13, 2021

Subsystems (2)

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v9 cover arm64: mte: allow async MTE to be upgraded to sync on a per-CPU basis

On some CPUs the performance of MTE in synchronous mode is similar to that of asynchronous mode. This makes it worthwhile to enable synchronous mode on those CPUs when asynchronous mode is requested, in order to gain the error detection benefits of synchronous mode without the performance downsides. Therefore, make it possible for user programs to opt into upgrading to synchronous mode on those CPUs.

This is done by introducing a notion of a preferred TCF mode, which is controlled on a per-CPU basis by a sysfs node. The existing SYNC and ASYNC TCF settings are repurposed as bitfields that specify a set of possible modes. If the preferred TCF mode for a particular CPU is in the user-provided mode set (this will always be the case for mode sets containing more than one mode because the kernel only supports two tag checking modes, but future kernels may support more modes) then that mode is used when running on that CPU, otherwise one of the modes in the task's mode set will be selected in a currently unspecified manner.

v8:
- split into multiple patches
- remove MTE_CTRL_TCF_NONE
- improve documentation
- disable preemption and add comment to mte_update_sctlr_user
- bring back PR_MTE_TCF_SHIFT for source compatibility
- address formatting nit

v7:
- switch to new API proposed on list

v6:
- switch to strings in sysfs nodes instead of TCF values

v5:
- updated documentation
- address some nits in mte.c

v4:
- switch to new mte_ctrl field
- make register_mte_upgrade_async_sysctl return an int
- change the sysctl to take 0 or 1 instead of raw TCF values
- "same as" -> "similar to"

v3:
- drop the device tree support
- add documentation
- add static_assert to ensure no overlap with real HW bits
- move per-CPU variable initialization to mte.c
- use smp_call_function_single instead of stop_machine

v2:
- make it an opt-in behavior
- change the format of the device tree node
- also allow controlling the feature via sysfs

Peter Collingbourne (4):
arm64: mte: rename gcr_user_excl to mte_ctrl
arm64: mte: change ASYNC and SYNC TCF settings into bitfields
arm64: mte: introduce a per-CPU tag checking mode preference
Documentation: document the preferred tag checking mode feature

 .../ABI/testing/sysfs-devices-system-cpu      |  16 ++
 .../arm64/memory-tagging-extension.rst        |  48 +++++-
 arch/arm64/include/asm/processor.h            |   8 +-
 arch/arm64/kernel/asm-offsets.c               |   2 +-
 arch/arm64/kernel/entry.S                     |   4 +-
 arch/arm64/kernel/mte.c                       | 149 ++++++++++++------
 include/uapi/linux/prctl.h                    |  11 +-
 7 files changed, 178 insertions(+), 60 deletions(-)
Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

PATCH v9 1/4 arm64: mte: rename gcr_user_excl to mte_ctrl

We are going to use this field to store more data. To prepare for that, rename it and change the users to rely on the bit position of gcr_user_excl in mte_ctrl.

---
 arch/arm64/include/asm/processor.h |  5 ++++-
 arch/arm64/kernel/asm-offsets.c    |  2 +-
 arch/arm64/kernel/entry.S          |  4 ++--
 arch/arm64/kernel/mte.c            | 14 ++++++++------
 4 files changed, 15 insertions(+), 10 deletions(-)
arch/arm64/include/asm/processor.h +4 -1
index 9df3feeee890..6322fb1714d5 100644
@@ -16,6 +16,9 @@
  */
 #define NET_IP_ALIGN	0
 
+#define MTE_CTRL_GCR_USER_EXCL_SHIFT	0
+#define MTE_CTRL_GCR_USER_EXCL_MASK	0xffff
+
 #ifndef __ASSEMBLY__
 
 #include <linux/build_bug.h>
@@ -151,7 +154,7 @@ struct thread_struct {
 	struct ptrauth_keys_kernel	keys_kernel;
 #endif
 #ifdef CONFIG_ARM64_MTE
-	u64			gcr_user_excl;
+	u64			mte_ctrl;
 #endif
 	u64			sctlr_user;
 };
arch/arm64/kernel/asm-offsets.c +1 -1
index 0cb34ccb6e73..63d02cd67b44 100644
@@ -49,7 +49,7 @@ int main(void)
   DEFINE(THREAD_KEYS_KERNEL,	offsetof(struct task_struct, thread.keys_kernel));
 #endif
 #ifdef CONFIG_ARM64_MTE
-  DEFINE(THREAD_GCR_EL1_USER,	offsetof(struct task_struct, thread.gcr_user_excl));
+  DEFINE(THREAD_MTE_CTRL,	offsetof(struct task_struct, thread.mte_ctrl));
 #endif
   BLANK();
   DEFINE(S_X0,			offsetof(struct pt_regs, regs[0]));
arch/arm64/kernel/entry.S +2 -2
index 3513984a88bd..ce59280355c5 100644
@@ -182,7 +182,7 @@ alternative_else_nop_endif
 	 * the RRND (bit[16]) setting.
 	 */
 	mrs_s	\tmp2, SYS_GCR_EL1
-	bfi	\tmp2, \tmp, #0, #16
+	bfxil	\tmp2, \tmp, #MTE_CTRL_GCR_USER_EXCL_SHIFT, #16
 	msr_s	SYS_GCR_EL1, \tmp2
 #endif
 	.endm
@@ -205,7 +205,7 @@ alternative_else_nop_endif
 alternative_if_not ARM64_MTE
 	b	1f
 alternative_else_nop_endif
-	ldr	\tmp, [\tsk, #THREAD_GCR_EL1_USER]
+	ldr	\tmp, [\tsk, #THREAD_MTE_CTRL]
 
 	mte_set_gcr \tmp, \tmp2
 1:
arch/arm64/kernel/mte.c +8 -6
index 125a10e413e9..d3884d09513d 100644
@@ -199,7 +199,7 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void set_gcr_el1_excl(u64 excl)
 {
-	current->thread.gcr_user_excl = excl;
+	current->thread.mte_ctrl = excl;
 
 	/*
 	 * SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value
@@ -263,8 +263,8 @@ void mte_suspend_exit(void)
 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
 {
 	u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
-	u64 gcr_excl = ~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
-		       SYS_GCR_EL1_EXCL_MASK;
+	u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
+			SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
 
 	if (!system_supports_mte())
 		return 0;
@@ -285,10 +285,10 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg)
 
 	if (task != current) {
 		task->thread.sctlr_user = sctlr;
-		task->thread.gcr_user_excl = gcr_excl;
+		task->thread.mte_ctrl = mte_ctrl;
 	} else {
 		set_task_sctlr_el1(sctlr);
-		set_gcr_el1_excl(gcr_excl);
+		set_gcr_el1_excl(mte_ctrl);
 	}
 
 	return 0;
@@ -297,7 +297,9 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg)
 long get_mte_ctrl(struct task_struct *task)
 {
 	unsigned long ret;
-	u64 incl = ~task->thread.gcr_user_excl & SYS_GCR_EL1_EXCL_MASK;
+	u64 mte_ctrl = task->thread.mte_ctrl;
+	u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
+		   SYS_GCR_EL1_EXCL_MASK;
 
 	if (!system_supports_mte())
 		return 0;
Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Acked-by on v9 1/4
#2 lore raw

Re: [PATCH v9 1/4] arm64: mte: rename gcr_user_excl to mte_ctrl

Quoted 7 lines · Peter Collingbourne

We are going to use this field to store more data. To prepare for that, rename it and change the users to rely on the bit position of gcr_user_excl in mte_ctrl.

Quoted 6 lines
---
 arch/arm64/include/asm/processor.h |  5 ++++-
 arch/arm64/kernel/asm-offsets.c    |  2 +-
 arch/arm64/kernel/entry.S          |  4 ++--
 arch/arm64/kernel/mte.c            | 14 ++++++++------
 4 files changed, 15 insertions(+), 10 deletions(-)

Makes sense to me:

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw

PATCH v9 2/4 arm64: mte: change ASYNC and SYNC TCF settings into bitfields

In reply to Peter Collingbourne

Allow the user program to specify both ASYNC and SYNC TCF modes by repurposing the existing constants as bitfields. This will allow the kernel to select one of the modes on behalf of the user program. With this patch the kernel will always select async mode, but a subsequent patch will make this configurable.

---
v9:
- make mte_update_sctlr_user static

 arch/arm64/include/asm/processor.h |  3 ++
 arch/arm64/kernel/mte.c            | 70 ++++++++++++------------------
 include/uapi/linux/prctl.h         | 11 ++---
 3 files changed, 37 insertions(+), 47 deletions(-)
arch/arm64/include/asm/processor.h +3 -0
index 6322fb1714d5..80ceb9cbdd60 100644
@@ -19,6 +19,9 @@
 #define MTE_CTRL_GCR_USER_EXCL_SHIFT	0
 #define MTE_CTRL_GCR_USER_EXCL_MASK	0xffff
 
+#define MTE_CTRL_TCF_SYNC		(1UL << 16)
+#define MTE_CTRL_TCF_ASYNC		(1UL << 17)
+
 #ifndef __ASSEMBLY__
 
 #include <linux/build_bug.h>
arch/arm64/kernel/mte.c +28 -42

108 lines not on this page · raw message

include/uapi/linux/prctl.h +6 -5
index 18a9f59dc067..d3a5afb4c1ae 100644
@@ -234,14 +234,15 @@ struct prctl_mm_map {
 #define PR_GET_TAGGED_ADDR_CTRL		56
 # define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
 /* MTE tag check fault modes */
-# define PR_MTE_TCF_SHIFT		1
-# define PR_MTE_TCF_NONE		(0UL << PR_MTE_TCF_SHIFT)
-# define PR_MTE_TCF_SYNC		(1UL << PR_MTE_TCF_SHIFT)
-# define PR_MTE_TCF_ASYNC		(2UL << PR_MTE_TCF_SHIFT)
-# define PR_MTE_TCF_MASK		(3UL << PR_MTE_TCF_SHIFT)
+# define PR_MTE_TCF_NONE		0
+# define PR_MTE_TCF_SYNC		(1UL << 1)
+# define PR_MTE_TCF_ASYNC		(1UL << 2)
+# define PR_MTE_TCF_MASK		(PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC)
 /* MTE tag inclusion mask */
 # define PR_MTE_TAG_SHIFT		3
 # define PR_MTE_TAG_MASK		(0xffffUL << PR_MTE_TAG_SHIFT)
+/* Unused; kept only for source compatibility */
+# define PR_MTE_TCF_SHIFT		1
 
 /* Control reclaim behavior when allocating memory */
 #define PR_SET_IO_FLUSHER		57
Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#4 lore raw

Re: [PATCH v9 2/4] arm64: mte: change ASYNC and SYNC TCF settings into bitfields

Quoted 17 lines · Peter Collingbourne

Allow the user program to specify both ASYNC and SYNC TCF modes by repurposing the existing constants as bitfields. This will allow the kernel to select one of the modes on behalf of the user program. With this patch the kernel will always select async mode, but a subsequent patch will make this configurable.

---
v9:
- make mte_update_sctlr_user static

 arch/arm64/include/asm/processor.h |  3 ++
 arch/arm64/kernel/mte.c            | 70 ++++++++++++------------------
 include/uapi/linux/prctl.h         | 11 ++---
 3 files changed, 37 insertions(+), 47 deletions(-)

...

Quoted 26 lines

long set_mte_ctrl(struct task_struct *task, unsigned long arg) {

arch/arm64/kernel/mte.c +4 -14
-	u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
 	u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
 			SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
 
 	if (!system_supports_mte())
 		return 0;
 
-	switch (arg & PR_MTE_TCF_MASK) {
-	case PR_MTE_TCF_NONE:
-		sctlr |= SCTLR_EL1_TCF0_NONE;
-		break;
-	case PR_MTE_TCF_SYNC:
-		sctlr |= SCTLR_EL1_TCF0_SYNC;
-		break;
-	case PR_MTE_TCF_ASYNC:
-		sctlr |= SCTLR_EL1_TCF0_ASYNC;
-		break;
-	default:
-		return -EINVAL;
-	}
+	if (arg & PR_MTE_TCF_ASYNC)
+		mte_ctrl |= MTE_CTRL_TCF_ASYNC;
+	if (arg & PR_MTE_TCF_SYNC)
+		mte_ctrl |= MTE_CTRL_TCF_SYNC;

arch/arm64/kernel/mte.c void mte_suspend_exit(void) · line 284

Quoted 10 lines
arch/arm64/kernel/mte.c +3 -6
 
-	if (task != current) {
-		task->thread.sctlr_user = sctlr;
-		task->thread.mte_ctrl = mte_ctrl;
-	} else {
-		set_task_sctlr_el1(sctlr);
-		set_gcr_el1_excl(mte_ctrl);
+	task->thread.mte_ctrl = mte_ctrl;
+	if (task == current) {
+		mte_update_sctlr_user(task);

In conjunction with the next patch, what happens if we migrate at this point? I worry that we can install a stale sctlr_user value.

arch/arm64/kernel/mte.c void mte_suspend_exit(void) · line 285

Quoted 1 line
arch/arm64/kernel/mte.c +1 -0
+		set_task_sctlr_el1(task->thread.sctlr_user);

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#5 lore raw

Re: [PATCH v9 2/4] arm64: mte: change ASYNC and SYNC TCF settings into bitfields

Quoted 59 lines · Will Deacon
Quoted 17 lines · Peter Collingbourne

Allow the user program to specify both ASYNC and SYNC TCF modes by repurposing the existing constants as bitfields. This will allow the kernel to select one of the modes on behalf of the user program. With this patch the kernel will always select async mode, but a subsequent patch will make this configurable.

---
v9:
- make mte_update_sctlr_user static

 arch/arm64/include/asm/processor.h |  3 ++
 arch/arm64/kernel/mte.c            | 70 ++++++++++++------------------
 include/uapi/linux/prctl.h         | 11 ++---
 3 files changed, 37 insertions(+), 47 deletions(-)

...

Quoted 26 lines

long set_mte_ctrl(struct task_struct *task, unsigned long arg) {

arch/arm64/kernel/mte.c +4 -14
-     u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
      u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
                      SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
 
      if (!system_supports_mte())
              return 0;
 
-     switch (arg & PR_MTE_TCF_MASK) {
-     case PR_MTE_TCF_NONE:
-             sctlr |= SCTLR_EL1_TCF0_NONE;
-             break;
-     case PR_MTE_TCF_SYNC:
-             sctlr |= SCTLR_EL1_TCF0_SYNC;
-             break;
-     case PR_MTE_TCF_ASYNC:
-             sctlr |= SCTLR_EL1_TCF0_ASYNC;
-             break;
-     default:
-             return -EINVAL;
-     }
+     if (arg & PR_MTE_TCF_ASYNC)
+             mte_ctrl |= MTE_CTRL_TCF_ASYNC;
+     if (arg & PR_MTE_TCF_SYNC)
+             mte_ctrl |= MTE_CTRL_TCF_SYNC;

arch/arm64/kernel/mte.c void mte_suspend_exit(void) · line 284

Quoted 10 lines
arch/arm64/kernel/mte.c +3 -6
 
-     if (task != current) {
-             task->thread.sctlr_user = sctlr;
-             task->thread.mte_ctrl = mte_ctrl;
-     } else {
-             set_task_sctlr_el1(sctlr);
-             set_gcr_el1_excl(mte_ctrl);
+     task->thread.mte_ctrl = mte_ctrl;
+     if (task == current) {
+             mte_update_sctlr_user(task);
Quoted 4 lines

In conjunction with the next patch, what happens if we migrate at this point? I worry that we can install a stale sctlr_user value.

Quoted 1 line

+ set_task_sctlr_el1(task->thread.sctlr_user);

In this case, we will call mte_update_sctlr_user when scheduled onto the new CPU as a result of the change to mte_thread_switch, and both the scheduler and prctl will set SCTLR_EL1 to the new (correct) value for the current CPU.

Peter

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#6 lore raw

Re: [PATCH v9 2/4] arm64: mte: change ASYNC and SYNC TCF settings into bitfields

Quoted 42 lines · Peter Collingbourne
Quoted 41 lines · Will Deacon
Quoted 26 lines · Peter Collingbourne

long set_mte_ctrl(struct task_struct *task, unsigned long arg) {

arch/arm64/kernel/mte.c +4 -14
-     u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
      u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
                      SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
 
      if (!system_supports_mte())
              return 0;
 
-     switch (arg & PR_MTE_TCF_MASK) {
-     case PR_MTE_TCF_NONE:
-             sctlr |= SCTLR_EL1_TCF0_NONE;
-             break;
-     case PR_MTE_TCF_SYNC:
-             sctlr |= SCTLR_EL1_TCF0_SYNC;
-             break;
-     case PR_MTE_TCF_ASYNC:
-             sctlr |= SCTLR_EL1_TCF0_ASYNC;
-             break;
-     default:
-             return -EINVAL;
-     }
+     if (arg & PR_MTE_TCF_ASYNC)
+             mte_ctrl |= MTE_CTRL_TCF_ASYNC;
+     if (arg & PR_MTE_TCF_SYNC)
+             mte_ctrl |= MTE_CTRL_TCF_SYNC;

arch/arm64/kernel/mte.c void mte_suspend_exit(void) · line 284

Quoted 10 lines
arch/arm64/kernel/mte.c +3 -6
 
-     if (task != current) {
-             task->thread.sctlr_user = sctlr;
-             task->thread.mte_ctrl = mte_ctrl;
-     } else {
-             set_task_sctlr_el1(sctlr);
-             set_gcr_el1_excl(mte_ctrl);
+     task->thread.mte_ctrl = mte_ctrl;
+     if (task == current) {
+             mte_update_sctlr_user(task);

In conjunction with the next patch, what happens if we migrate at this point? I worry that we can install a stale sctlr_user value.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

+ set_task_sctlr_el1(task->thread.sctlr_user);

In this case, we will call mte_update_sctlr_user when scheduled onto the new CPU as a result of the change to mte_thread_switch, and both the scheduler and prctl will set SCTLR_EL1 to the new (correct) value for the current CPU.

Doesn't that rely on task->thread.sctlr_user being explicitly read on the new CPU? For example, the following rough sequence is what I'm worried about:

CPU x (prefer ASYNC)
set_mte_ctrl(ASYNC | SYNC)

	current->thread.mte_ctrl = ASYNC | SYNC;
	mte_update_sctlr_user
		current->thread.sctlr_user = ASYNC;
	Register Xn = current->thread.sctlr_user; // ASYNC
	<migration to CPU y>

CPU y (prefer SYNC)
mte_thread_switch

	mte_update_sctlr_user
		next->thread.sctlr_user = SYNC;
	update_sctlr_el1
		SCTLR_EL1 = SYNC;

	<resume next back in set_mte_ctrl>
	set_task_sctlr_el1(Xn); // ASYNC
	current->thread.sctlr_user = Xn; // ASYNC  XXX: also superfluous?
	SCTLR_EL1 = ASYNC;

Does that make sense?

I'm thinking set_mte_ctrl() should be using update_sctlr_el1() and disabling preemption around the whole thing, which would make it a lot closer to the context-switch path.

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#7 lore raw

Re: [PATCH v9 2/4] arm64: mte: change ASYNC and SYNC TCF settings into bitfields

Quoted 77 lines · Will Deacon
Quoted 42 lines · Peter Collingbourne
Quoted 41 lines · Will Deacon
Quoted 26 lines · Peter Collingbourne

long set_mte_ctrl(struct task_struct *task, unsigned long arg) {

arch/arm64/kernel/mte.c +4 -14
-     u64 sctlr = task->thread.sctlr_user & ~SCTLR_EL1_TCF0_MASK;
      u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
                      SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
 
      if (!system_supports_mte())
              return 0;
 
-     switch (arg & PR_MTE_TCF_MASK) {
-     case PR_MTE_TCF_NONE:
-             sctlr |= SCTLR_EL1_TCF0_NONE;
-             break;
-     case PR_MTE_TCF_SYNC:
-             sctlr |= SCTLR_EL1_TCF0_SYNC;
-             break;
-     case PR_MTE_TCF_ASYNC:
-             sctlr |= SCTLR_EL1_TCF0_ASYNC;
-             break;
-     default:
-             return -EINVAL;
-     }
+     if (arg & PR_MTE_TCF_ASYNC)
+             mte_ctrl |= MTE_CTRL_TCF_ASYNC;
+     if (arg & PR_MTE_TCF_SYNC)
+             mte_ctrl |= MTE_CTRL_TCF_SYNC;

arch/arm64/kernel/mte.c void mte_suspend_exit(void) · line 284

Quoted 10 lines
arch/arm64/kernel/mte.c +3 -6
 
-     if (task != current) {
-             task->thread.sctlr_user = sctlr;
-             task->thread.mte_ctrl = mte_ctrl;
-     } else {
-             set_task_sctlr_el1(sctlr);
-             set_gcr_el1_excl(mte_ctrl);
+     task->thread.mte_ctrl = mte_ctrl;
+     if (task == current) {
+             mte_update_sctlr_user(task);

In conjunction with the next patch, what happens if we migrate at this point? I worry that we can install a stale sctlr_user value.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

+ set_task_sctlr_el1(task->thread.sctlr_user);

In this case, we will call mte_update_sctlr_user when scheduled onto the new CPU as a result of the change to mte_thread_switch, and both the scheduler and prctl will set SCTLR_EL1 to the new (correct) value for the current CPU.

Doesn't that rely on task->thread.sctlr_user being explicitly read on the new CPU? For example, the following rough sequence is what I'm worried about:

CPU x (prefer ASYNC)
set_mte_ctrl(ASYNC | SYNC)

        current->thread.mte_ctrl = ASYNC | SYNC;
        mte_update_sctlr_user
                current->thread.sctlr_user = ASYNC;
        Register Xn = current->thread.sctlr_user; // ASYNC
        <migration to CPU y>

CPU y (prefer SYNC)
mte_thread_switch

        mte_update_sctlr_user
                next->thread.sctlr_user = SYNC;
        update_sctlr_el1
                SCTLR_EL1 = SYNC;

        <resume next back in set_mte_ctrl>
        set_task_sctlr_el1(Xn); // ASYNC
        current->thread.sctlr_user = Xn; // ASYNC  XXX: also superfluous?
        SCTLR_EL1 = ASYNC;
Quoted 5 lines

Does that make sense?

I'm thinking set_mte_ctrl() should be using update_sctlr_el1() and disabling preemption around the whole thing, which would make it a lot closer to the context-switch path.

Okay, I see what you mean. I also noticed that prctl(PR_PAC_SET_ENABLED_KEYS) would now have the same problem. In v10 I've addressed this issue by inserting a patch after this one that disables preemption in both prctl implementations.

Peter

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#8 lore raw

PATCH v9 3/4 arm64: mte: introduce a per-CPU tag checking mode preference

In reply to Peter Collingbourne

Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync.

---
 arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +75 -2
index 53d89915029d..48f218e070cc 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/prctl.h>
@@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
 
+static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
+
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
@@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void mte_update_sctlr_user(struct task_struct *task)
 {
+	/*
+	 * This can only be called on the current or next task since the CPU
+	 * must match where the thread is going to run.
+	 */
 	unsigned long sctlr = task->thread.sctlr_user;
-	unsigned long pref = MTE_CTRL_TCF_ASYNC;
 	unsigned long mte_ctrl = task->thread.mte_ctrl;
-	unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
+	unsigned long pref, resolved_mte_tcf;
 
+	preempt_disable();
+	pref = __this_cpu_read(mte_tcf_preferred);
+	resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
 	sctlr &= ~SCTLR_EL1_TCF0_MASK;
 	if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
 		sctlr |= SCTLR_EL1_TCF0_ASYNC;
 	else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
 		sctlr |= SCTLR_EL1_TCF0_SYNC;
 	task->thread.sctlr_user = sctlr;
+	preempt_enable();
 }
 
 void mte_thread_init_user(void)
@@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,
 
 	return ret;
 }
+
+static ssize_t mte_tcf_preferred_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	switch (per_cpu(mte_tcf_preferred, dev->id)) {
+	case MTE_CTRL_TCF_ASYNC:
+		return sysfs_emit(buf, "async\n");
+	case MTE_CTRL_TCF_SYNC:
+		return sysfs_emit(buf, "sync\n");
+	default:
+		return sysfs_emit(buf, "???\n");
+	}
+}
+
+static void sync_sctlr(void *arg)
+{
+	mte_update_sctlr_user(current);
+	set_task_sctlr_el1(current->thread.sctlr_user);
+}
+
+static ssize_t mte_tcf_preferred_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t count)
+{
+	ssize_t ret = 0;
+	u64 tcf;
+
+	if (sysfs_streq(buf, "async"))
+		tcf = MTE_CTRL_TCF_ASYNC;
+	else if (sysfs_streq(buf, "sync"))
+		tcf = MTE_CTRL_TCF_SYNC;
+	else
+		return -EINVAL;
+
+	device_lock(dev);
+	per_cpu(mte_tcf_preferred, dev->id) = tcf;
+
+	if (cpu_online(dev->id))
+		ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0);
+	if (ret == 0)
+		ret = count;
+	device_unlock(dev);
+
+	return ret;
+}
+static DEVICE_ATTR_RW(mte_tcf_preferred);
+
+static int register_mte_tcf_preferred_sysctl(void)
+{
+	unsigned int cpu;
+
+	if (!system_supports_mte())
+		return 0;
+
+	for_each_possible_cpu(cpu) {
+		per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC;
+		device_create_file(get_cpu_device(cpu),
+				   &dev_attr_mte_tcf_preferred);
+	}
+
+	return 0;
+}
+subsys_initcall(register_mte_tcf_preferred_sysctl);
Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#9 lore raw

Re: [PATCH v9 3/4] arm64: mte: introduce a per-CPU tag checking mode preference

Quoted 92 lines · Peter Collingbourne

Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync.

---
 arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +41 -2
index 53d89915029d..48f218e070cc 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/prctl.h>
@@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
 
+static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
+
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
@@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void mte_update_sctlr_user(struct task_struct *task)
 {
+	/*
+	 * This can only be called on the current or next task since the CPU
+	 * must match where the thread is going to run.
+	 */
 	unsigned long sctlr = task->thread.sctlr_user;
-	unsigned long pref = MTE_CTRL_TCF_ASYNC;
 	unsigned long mte_ctrl = task->thread.mte_ctrl;
-	unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
+	unsigned long pref, resolved_mte_tcf;
 
+	preempt_disable();
+	pref = __this_cpu_read(mte_tcf_preferred);
+	resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
 	sctlr &= ~SCTLR_EL1_TCF0_MASK;
 	if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
 		sctlr |= SCTLR_EL1_TCF0_ASYNC;
 	else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
 		sctlr |= SCTLR_EL1_TCF0_SYNC;
 	task->thread.sctlr_user = sctlr;
+	preempt_enable();
 }
 
 void mte_thread_init_user(void)
@@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,
 
 	return ret;
 }
+
+static ssize_t mte_tcf_preferred_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	switch (per_cpu(mte_tcf_preferred, dev->id)) {
+	case MTE_CTRL_TCF_ASYNC:
+		return sysfs_emit(buf, "async\n");
+	case MTE_CTRL_TCF_SYNC:
+		return sysfs_emit(buf, "sync\n");
+	default:
+		return sysfs_emit(buf, "???\n");
+	}
+}
+
+static void sync_sctlr(void *arg)
+{
+	mte_update_sctlr_user(current);
+	set_task_sctlr_el1(current->thread.sctlr_user);
+}
+
+static ssize_t mte_tcf_preferred_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t count)
+{
+	ssize_t ret = 0;
+	u64 tcf;
+
+	if (sysfs_streq(buf, "async"))
+		tcf = MTE_CTRL_TCF_ASYNC;

arch/arm64/kernel/mte.c int mte_ptrace_copy_tags(struct task_struct *child, long request, · line 492

Quoted 10 lines
arch/arm64/kernel/mte.c +10 -0
+	else if (sysfs_streq(buf, "sync"))
+		tcf = MTE_CTRL_TCF_SYNC;
+	else
+		return -EINVAL;
+
+	device_lock(dev);
+	per_cpu(mte_tcf_preferred, dev->id) = tcf;
+
+	if (cpu_online(dev->id))
+		ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0);

Hmm, so this call could land right in the middle of a concurrent prctl(). What happens in that case?

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#10 lore raw

Re: [PATCH v9 3/4] arm64: mte: introduce a per-CPU tag checking mode preference

Quoted 99 lines · Will Deacon
Quoted 97 lines · Peter Collingbourne

Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync.

---
 arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +46 -2
index 53d89915029d..48f218e070cc 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/prctl.h>
@@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
 
+static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
+
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
@@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void mte_update_sctlr_user(struct task_struct *task)
 {
+     /*
+      * This can only be called on the current or next task since the CPU
+      * must match where the thread is going to run.
+      */
      unsigned long sctlr = task->thread.sctlr_user;
-     unsigned long pref = MTE_CTRL_TCF_ASYNC;
      unsigned long mte_ctrl = task->thread.mte_ctrl;
-     unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
+     unsigned long pref, resolved_mte_tcf;
 
+     preempt_disable();
+     pref = __this_cpu_read(mte_tcf_preferred);
+     resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
      sctlr &= ~SCTLR_EL1_TCF0_MASK;
      if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
              sctlr |= SCTLR_EL1_TCF0_ASYNC;
      else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
              sctlr |= SCTLR_EL1_TCF0_SYNC;
      task->thread.sctlr_user = sctlr;
+     preempt_enable();
 }
 
 void mte_thread_init_user(void)
@@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,
 
      return ret;
 }
+
+static ssize_t mte_tcf_preferred_show(struct device *dev,
+                                   struct device_attribute *attr, char *buf)
+{
+     switch (per_cpu(mte_tcf_preferred, dev->id)) {
+     case MTE_CTRL_TCF_ASYNC:
+             return sysfs_emit(buf, "async\n");
+     case MTE_CTRL_TCF_SYNC:
+             return sysfs_emit(buf, "sync\n");
+     default:
+             return sysfs_emit(buf, "???\n");
+     }
+}
+
+static void sync_sctlr(void *arg)
+{
+     mte_update_sctlr_user(current);
+     set_task_sctlr_el1(current->thread.sctlr_user);
+}
+
+static ssize_t mte_tcf_preferred_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+     ssize_t ret = 0;
+     u64 tcf;
+
+     if (sysfs_streq(buf, "async"))
+             tcf = MTE_CTRL_TCF_ASYNC;
+     else if (sysfs_streq(buf, "sync"))
+             tcf = MTE_CTRL_TCF_SYNC;
+     else
+             return -EINVAL;
+
Quoted 8 lines

arch/arm64/kernel/mte.c int mte_ptrace_copy_tags(struct task_struct *child, long request, · line 492

Quoted 5 lines
arch/arm64/kernel/mte.c +5 -0
+     device_lock(dev);
+     per_cpu(mte_tcf_preferred, dev->id) = tcf;
+
+     if (cpu_online(dev->id))
+             ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0);

Hmm, so this call could land right in the middle of a concurrent prctl(). What happens in that case?

I don't think anything can go wrong. When the prctl sets mte_ctrl we then need to call mte_update_sctlr_user followed by set_task_sctlr_el1 and it doesn't matter if it happens multiple times (either mte_update_sctlr_user/set_task_sctlr_el1/mte_update_sctlr_user/set_task_sctlr_el1 or even the less likely mte_update_sctlr_user/mte_update_sctlr_user/set_task_sctlr_el1/set_task_sctlr_el1).

Actually, I'm not sure we need any code in the sync_sctlr function at all. Simply scheduling an empty function onto the CPU will cause mte_update_sctlr_user and then update_sctlr_el1 to be called when the task that was originally running on the CPU gets rescheduled onto it, as a result of the change to mte_thread_switch.

Peter

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#11 lore raw

Re: [PATCH v9 3/4] arm64: mte: introduce a per-CPU tag checking mode preference

Quoted 109 lines · Peter Collingbourne
Quoted 99 lines · Will Deacon
Quoted 97 lines · Peter Collingbourne

Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync.

---
 arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +46 -2
index 53d89915029d..48f218e070cc 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/prctl.h>
@@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
 
+static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
+
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
@@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void mte_update_sctlr_user(struct task_struct *task)
 {
+     /*
+      * This can only be called on the current or next task since the CPU
+      * must match where the thread is going to run.
+      */
      unsigned long sctlr = task->thread.sctlr_user;
-     unsigned long pref = MTE_CTRL_TCF_ASYNC;
      unsigned long mte_ctrl = task->thread.mte_ctrl;
-     unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
+     unsigned long pref, resolved_mte_tcf;
 
+     preempt_disable();
+     pref = __this_cpu_read(mte_tcf_preferred);
+     resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
      sctlr &= ~SCTLR_EL1_TCF0_MASK;
      if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
              sctlr |= SCTLR_EL1_TCF0_ASYNC;
      else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
              sctlr |= SCTLR_EL1_TCF0_SYNC;
      task->thread.sctlr_user = sctlr;
+     preempt_enable();
 }
 
 void mte_thread_init_user(void)
@@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,
 
      return ret;
 }
+
+static ssize_t mte_tcf_preferred_show(struct device *dev,
+                                   struct device_attribute *attr, char *buf)
+{
+     switch (per_cpu(mte_tcf_preferred, dev->id)) {
+     case MTE_CTRL_TCF_ASYNC:
+             return sysfs_emit(buf, "async\n");
+     case MTE_CTRL_TCF_SYNC:
+             return sysfs_emit(buf, "sync\n");
+     default:
+             return sysfs_emit(buf, "???\n");
+     }
+}
+
+static void sync_sctlr(void *arg)
+{
+     mte_update_sctlr_user(current);
+     set_task_sctlr_el1(current->thread.sctlr_user);
+}
+
+static ssize_t mte_tcf_preferred_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+     ssize_t ret = 0;
+     u64 tcf;
+
+     if (sysfs_streq(buf, "async"))
+             tcf = MTE_CTRL_TCF_ASYNC;
+     else if (sysfs_streq(buf, "sync"))
+             tcf = MTE_CTRL_TCF_SYNC;
+     else
+             return -EINVAL;
+
Quoted 8 lines

arch/arm64/kernel/mte.c int mte_ptrace_copy_tags(struct task_struct *child, long request, · line 492

Quoted 5 lines
arch/arm64/kernel/mte.c +5 -0
+     device_lock(dev);
+     per_cpu(mte_tcf_preferred, dev->id) = tcf;
+
+     if (cpu_online(dev->id))
+             ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0);

Hmm, so this call could land right in the middle of a concurrent prctl(). What happens in that case?

Quoted 6 lines

I don't think anything can go wrong. When the prctl sets mte_ctrl we then need to call mte_update_sctlr_user followed by set_task_sctlr_el1 and it doesn't matter if it happens multiple times (either mte_update_sctlr_user/set_task_sctlr_el1/mte_update_sctlr_user/set_task_sctlr_el1 or even the less likely mte_update_sctlr_user/mte_update_sctlr_user/set_task_sctlr_el1/set_task_sctlr_el1).

I think you're still (at least) relying on the compiler not to tear or cache accesses to ->thread.sctlr_user, no? It seems like it would be a lot simpler just to leave the change until the next context switch and not send the IPI at all. I think that's a reasonable behaviour because the write to sysfs is racy anyway, so we can just document this.

Quoted 5 lines

Actually, I'm not sure we need any code in the sync_sctlr function at all. Simply scheduling an empty function onto the CPU will cause mte_update_sctlr_user and then update_sctlr_el1 to be called when the task that was originally running on the CPU gets rescheduled onto it, as a result of the change to mte_thread_switch.

I'm not sure I agree here -- I thought smp_call_function_single() ran the target function directly from the IPI handler in interrupt context, without the need for a context-switch.

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#12 lore raw

Re: [PATCH v9 3/4] arm64: mte: introduce a per-CPU tag checking mode preference

Quoted 118 lines · Will Deacon
Quoted 109 lines · Peter Collingbourne
Quoted 99 lines · Will Deacon
Quoted 97 lines · Peter Collingbourne

Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync.

---
 arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +46 -2
index 53d89915029d..48f218e070cc 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/prctl.h>
@@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
 
+static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
+
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
@@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl)
 
 static void mte_update_sctlr_user(struct task_struct *task)
 {
+     /*
+      * This can only be called on the current or next task since the CPU
+      * must match where the thread is going to run.
+      */
      unsigned long sctlr = task->thread.sctlr_user;
-     unsigned long pref = MTE_CTRL_TCF_ASYNC;
      unsigned long mte_ctrl = task->thread.mte_ctrl;
-     unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
+     unsigned long pref, resolved_mte_tcf;
 
+     preempt_disable();
+     pref = __this_cpu_read(mte_tcf_preferred);
+     resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
      sctlr &= ~SCTLR_EL1_TCF0_MASK;
      if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
              sctlr |= SCTLR_EL1_TCF0_ASYNC;
      else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
              sctlr |= SCTLR_EL1_TCF0_SYNC;
      task->thread.sctlr_user = sctlr;
+     preempt_enable();
 }
 
 void mte_thread_init_user(void)
@@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,
 
      return ret;
 }
+
+static ssize_t mte_tcf_preferred_show(struct device *dev,
+                                   struct device_attribute *attr, char *buf)
+{
+     switch (per_cpu(mte_tcf_preferred, dev->id)) {
+     case MTE_CTRL_TCF_ASYNC:
+             return sysfs_emit(buf, "async\n");
+     case MTE_CTRL_TCF_SYNC:
+             return sysfs_emit(buf, "sync\n");
+     default:
+             return sysfs_emit(buf, "???\n");
+     }
+}
+
+static void sync_sctlr(void *arg)
+{
+     mte_update_sctlr_user(current);
+     set_task_sctlr_el1(current->thread.sctlr_user);
+}
+
+static ssize_t mte_tcf_preferred_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+     ssize_t ret = 0;
+     u64 tcf;
+
+     if (sysfs_streq(buf, "async"))
+             tcf = MTE_CTRL_TCF_ASYNC;
+     else if (sysfs_streq(buf, "sync"))
+             tcf = MTE_CTRL_TCF_SYNC;
+     else
+             return -EINVAL;
+
Quoted 8 lines

arch/arm64/kernel/mte.c int mte_ptrace_copy_tags(struct task_struct *child, long request, · line 492

Quoted 5 lines
arch/arm64/kernel/mte.c +5 -0
+     device_lock(dev);
+     per_cpu(mte_tcf_preferred, dev->id) = tcf;
+
+     if (cpu_online(dev->id))
+             ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0);

Hmm, so this call could land right in the middle of a concurrent prctl(). What happens in that case?

Quoted 6 lines

I don't think anything can go wrong. When the prctl sets mte_ctrl we then need to call mte_update_sctlr_user followed by set_task_sctlr_el1 and it doesn't matter if it happens multiple times (either mte_update_sctlr_user/set_task_sctlr_el1/mte_update_sctlr_user/set_task_sctlr_el1 or even the less likely mte_update_sctlr_user/mte_update_sctlr_user/set_task_sctlr_el1/set_task_sctlr_el1).

Quoted 5 lines

I think you're still (at least) relying on the compiler not to tear or cache accesses to ->thread.sctlr_user, no? It seems like it would be a lot simpler just to leave the change until the next context switch and not send the IPI at all. I think that's a reasonable behaviour because the write to sysfs is racy anyway, so we can just document this.

Works for me. I removed the call to smp_call_function_single here and updated the documentation patch to say that changes won't take effect immediately.

Quoted 9 lines
Quoted 5 lines

Actually, I'm not sure we need any code in the sync_sctlr function at all. Simply scheduling an empty function onto the CPU will cause mte_update_sctlr_user and then update_sctlr_el1 to be called when the task that was originally running on the CPU gets rescheduled onto it, as a result of the change to mte_thread_switch.

I'm not sure I agree here -- I thought smp_call_function_single() ran the target function directly from the IPI handler in interrupt context, without the need for a context-switch.

I see -- I thought that they ran as something like a kthread, but you may be right.

Peter

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#13 lore raw

PATCH v9 4/4 Documentation: document the preferred tag checking mode feature

In reply to Peter Collingbourne

Document the functionality added in the previous patches.

---
v9:
- add documentation for sysfs node under Documentation/ABI

 .../ABI/testing/sysfs-devices-system-cpu      | 16 +++++++
 .../arm64/memory-tagging-extension.rst        | 48 ++++++++++++++++---
 2 files changed, 57 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +16 -0
index fe13baa53c59..195587b7c7ec 100644
@@ -640,3 +640,19 @@ Description:	SPURR ticks for cpuX when it was idle.
 
 		This sysfs interface exposes the number of SPURR ticks
 		for cpuX when it was idle.
+
+What: 		/sys/devices/system/cpu/cpuX/mte_tcf_preferred
+Date:		Jul 2021
+Contact:	Linux ARM Kernel Mailing list <[email protected]>
+Description:	Preferred MTE tag checking mode
+
+		When a user program specifies more than one MTE tag checking
+		mode, this sysfs node is used to specify which mode should
+		be preferred when running on that CPU. Possible values:
+
+		================  ==============================================
+		"sync"	  	  Prefer synchronous mode
+		"async"	  	  Prefer asynchronous mode
+		================  ==============================================
+
+		See also: Documentation/arm64/memory-tagging-extension.rst
Documentation/arm64/memory-tagging-extension.rst +41 -7

79 lines not on this page · raw message

Signature · 7 lines
2.32.0.93.g670b81a890-goog


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Acked-by on v9 4/4
#14 lore raw

Re: [PATCH v9 4/4] Documentation: document the preferred tag checking mode feature

Quoted 18 lines · Peter Collingbourne

Document the functionality added in the previous patches.

---
v9:
- add documentation for sysfs node under Documentation/ABI

 .../ABI/testing/sysfs-devices-system-cpu      | 16 +++++++
 .../arm64/memory-tagging-extension.rst        | 48 ++++++++++++++++---
 2 files changed, 57 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +0 -0
index fe13baa53c59..195587b7c7ec 100644

Documentation/ABI/testing/sysfs-devices-system-cpu Description: SPURR ticks for cpuX when it was idle. · line 645

Quoted 6 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +3 -0
@@ -640,3 +640,19 @@ Description:	SPURR ticks for cpuX when it was idle.
 
 		This sysfs interface exposes the number of SPURR ticks
 		for cpuX when it was idle.
+
+What: 		/sys/devices/system/cpu/cpuX/mte_tcf_preferred
+Date:		Jul 2021

nit: Seems like the other entries here spell out the full name of the month.

With that fixed:

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#15 lore raw

Re: [PATCH v9 4/4] Documentation: document the preferred tag checking mode feature

Quoted 21 lines · Will Deacon
Quoted 19 lines · Peter Collingbourne

Document the functionality added in the previous patches.

---
v9:
- add documentation for sysfs node under Documentation/ABI

 .../ABI/testing/sysfs-devices-system-cpu      | 16 +++++++
 .../arm64/memory-tagging-extension.rst        | 48 ++++++++++++++++---
 2 files changed, 57 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +0 -0
index fe13baa53c59..195587b7c7ec 100644
@@ -640,3 +640,19 @@ Description:     SPURR ticks for cpuX when it was idle.
 
Quoted 7 lines

Documentation/ABI/testing/sysfs-devices-system-cpu Description: SPURR ticks for cpuX when it was idle. · line 645

Quoted 5 lines
Documentation/ABI/testing/sysfs-devices-system-cpu +3 -0
              This sysfs interface exposes the number of SPURR ticks
              for cpuX when it was idle.
+
+What:                /sys/devices/system/cpu/cpuX/mte_tcf_preferred
+Date:                Jul 2021

nit: Seems like the other entries here spell out the full name of the month.

FWIW, it seems to vary based on which part of the file you're looking at (e.g. some entries near the end abbreviate it, so I followed that pattern since my addition was at the end). But it looks like it isn't abbreviated in the majority of entries, so I changed it.

Quoted 3 lines

With that fixed:

Thanks.

Peter

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel