Skip to contentSkip to conversation

arm64: mte: Fix MTE granule mask

PATCH In review Vladimir Murzin 3 participants 4 messages Sep 5, 2025 – Sep 8, 2025 KVM arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mte: Fix MTE granule mask

So it generates correct mask when used together with MTE_TAG_SHIFT

---
 arm/mte.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/mte.c +1 -1
index 95d58aaa..a1bed8a7 100644
@@ -26,7 +26,7 @@
 #define MTE_TCF_ASYMM	0b11
 
 #define MTE_GRANULE_SIZE        UL(16)
-#define MTE_GRANULE_MASK        (~(MTE_GRANULE_SIZE - 1))
+#define MTE_GRANULE_MASK        (MTE_GRANULE_SIZE - 1)
 #define MTE_TAG_SHIFT           56
 
 #define untagged(p)									\
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Vladimir,

Quoted 12 lines · Vladimir Murzin

So it generates correct mask when used together with MTE_TAG_SHIFT

---
 arm/mte.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/mte.c +0 -0
index 95d58aaa..a1bed8a7 100644

arm/mte.c line 29

Quoted 5 lines
arm/mte.c +1 -1
@@ -26,7 +26,7 @@
 #define MTE_TCF_ASYMM	0b11
 
 #define MTE_GRANULE_SIZE        UL(16)
-#define MTE_GRANULE_MASK        (~(MTE_GRANULE_SIZE - 1))
+#define MTE_GRANULE_MASK        (MTE_GRANULE_SIZE - 1)

And this is how it's used:

#define untagged(p) \

({											\
	unsigned long __in = (unsigned long)(p);					\
	typeof(p) __out = (typeof(p))(__in & ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT));	\

I'm going to walk through how MTE_GRANULE_MASK is used, just to make sure I got things right this time.

Without this change:

MTE_GRANULE_MASK = ~(0..01..1) = 1..10..0 -> LSB 4 bits are zero ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT) = ~(1..10..0) = 0..01..1 -> bits 63:60 are zero, and what is masked is actually the PAC field, not the logical tag.

With this patch:

MTE_GRANULE_MASK = 0..01..1 -> LSB 4 bits are 1 ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT) = ~(0..01..10..0) = 1..10..01..1 -> bits 59:56 are 0, and the logical tag is masked, which is what we want, so:

Also curious, I remember running the tests and I didn't see an error, how did you catch this?

Thanks,
Alex

Quoted 6 lines
arm/mte.c +0 -0
 #define MTE_TAG_SHIFT           56
 
 #define untagged(p)									\
Signature · 1 line
2.34.1
#2 lore raw

Hi Alexandru,

Quoted 36 lines · Alexandru Elisei

Hi Vladimir,

Quoted 12 lines · Vladimir Murzin

So it generates correct mask when used together with MTE_TAG_SHIFT

---
 arm/mte.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/mte.c +0 -0
index 95d58aaa..a1bed8a7 100644

arm/mte.c line 29

Quoted 5 lines
arm/mte.c +1 -1
@@ -26,7 +26,7 @@
 #define MTE_TCF_ASYMM	0b11
 
 #define MTE_GRANULE_SIZE        UL(16)
-#define MTE_GRANULE_MASK        (~(MTE_GRANULE_SIZE - 1))
+#define MTE_GRANULE_MASK        (MTE_GRANULE_SIZE - 1)

And this is how it's used:

#define untagged(p) \

({											\
	unsigned long __in = (unsigned long)(p);					\
	typeof(p) __out = (typeof(p))(__in & ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT));	\

I'm going to walk through how MTE_GRANULE_MASK is used, just to make sure I got things right this time.

Without this change:

MTE_GRANULE_MASK = ~(0..01..1) = 1..10..0 -> LSB 4 bits are zero ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT) = ~(1..10..0) = 0..01..1 -> bits 63:60 are zero, and what is masked is actually the PAC field, not the logical tag.

Quoted 8 lines

With this patch:

MTE_GRANULE_MASK = 0..01..1 -> LSB 4 bits are 1 ~(MTE_GRANULE_MASK << MTE_TAG_SHIFT) = ~(0..01..10..0) = 1..10..01..1 -> bits 59:56 are 0, and the logical tag is masked, which is what we want, so:

All correct, before the change we generate mask 0x0fff_ffff_ffff_ffff yet after the change it becomes 0xf0ff_ffff_ffff_ffff

Quoted 2 lines

Also curious, I remember running the tests and I didn't see an error, how did you catch this?

By eyes :D

Thanks
Vladimir

Quoted 10 lines

Thanks,
Alex

Quoted 6 lines
arm/mte.c +0 -0
 #define MTE_TAG_SHIFT           56
 
 #define untagged(p)									\
Signature · 1 line
2.34.1
#3 lore raw
In reply to Vladimir Murzin
Quoted 23 lines · Vladimir Murzin

So it generates correct mask when used together with MTE_TAG_SHIFT

---
 arm/mte.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/mte.c +1 -1
index 95d58aaa..a1bed8a7 100644
@@ -26,7 +26,7 @@
 #define MTE_TCF_ASYMM	0b11
 
 #define MTE_GRANULE_SIZE        UL(16)
-#define MTE_GRANULE_MASK        (~(MTE_GRANULE_SIZE - 1))
+#define MTE_GRANULE_MASK        (MTE_GRANULE_SIZE - 1)
 #define MTE_TAG_SHIFT           56
 
 #define untagged(p)									\
Signature · 1 line
2.34.1

Merged.

Thanks,
drew