Skip to contentSkip to conversation

xen/arm: Enforce alignment check in debug build for {read, write}_atomic

PATCH In review Ayan Kumar Halder 4 participants 4 messages Nov 8, 2022 – Nov 10, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xen/arm: Enforce alignment check in debug build for {read, write}_atomic

From: Ayan Kumar Halder <[email protected]>

Xen provides helper to atomically read/write memory (see {read, write}_atomic()). Those helpers can only work if the address is aligned to the size of the access (see B2.2.1 ARM DDI 08476I.a).

On Arm32, the alignment is already enforced by the processor because HSCTLR.A bit is set (it enforce alignment for every access). For Arm64, this bit is not set because memcpy()/memset() can use unaligned access for performance reason (the implementation is taken from the Cortex library).

To avoid any overhead in production build, the alignment will only be checked using an ASSERT. Note that it might be possible to do it in production build using the acquire/exclusive version of load/store. But this is left to a follow-up (if wanted).

---

Changes from :-
v1 - 1. Referred to the latest Arm Architecture Reference Manual in the commit message.

v2 - 1. Updated commit message to specify the reason for using ASSERT().
2. Added Julien's SoB.

 xen/arch/arm/include/asm/atomic.h | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/arm/include/asm/atomic.h +2 -0
index 1f60c28b1b..64314d59b3 100644
@@ -78,6 +78,7 @@ static always_inline void read_atomic_size(const volatile void *p,
                                            void *res,
                                            unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
     switch ( size )
     {
     case 1:
@@ -102,6 +103,7 @@ static always_inline void write_atomic_size(volatile void *p,
                                             void *val,
                                             unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
     switch ( size )
     {
     case 1:
Signature · 1 line
2.17.1
#1 lore raw

Hi Ayan,,

Quoted 19 lines · Ayan Kumar Halder

From: Ayan Kumar Halder <[email protected]>

Xen provides helper to atomically read/write memory (see {read, write}_atomic()). Those helpers can only work if the address is aligned to the size of the access (see B2.2.1 ARM DDI 08476I.a).

On Arm32, the alignment is already enforced by the processor because HSCTLR.A bit is set (it enforce alignment for every access). For Arm64, this bit is not set because memcpy()/memset() can use unaligned access for performance reason (the implementation is taken from the Cortex library).

To avoid any overhead in production build, the alignment will only be checked using an ASSERT. Note that it might be possible to do it in production build using the acquire/exclusive version of load/store. But this is left to a follow-up (if wanted).

Quoted 4 lines

I confirm my Reviewed-by.

Side note: You should actually have removed it :-)

Cheers
Bertrand

Quoted 35 lines

---

Changes from :-
v1 - 1. Referred to the latest Arm Architecture Reference Manual in the commit message.

v2 - 1. Updated commit message to specify the reason for using ASSERT().
2. Added Julien's SoB.

xen/arch/arm/include/asm/atomic.h | 2 ++

1 file changed, 2 insertions(+)
xen/arch/arm/include/asm/atomic.h +2 -0
index 1f60c28b1b..64314d59b3 100644
@@ -78,6 +78,7 @@ static always_inline void read_atomic_size(const volatile void *p,
                                           void *res,
                                           unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
    switch ( size )
    {
    case 1:
@@ -102,6 +103,7 @@ static always_inline void write_atomic_size(volatile void *p,
                                            void *val,
                                            unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
    switch ( size )
    {
    case 1:
Signature · 1 line
2.17.1
Acked-by on v3 1/1
#2 lore raw
In reply to Ayan Kumar Halder
Quoted 17 lines · Ayan Kumar Halder

From: Ayan Kumar Halder <[email protected]>

Xen provides helper to atomically read/write memory (see {read, write}_atomic()). Those helpers can only work if the address is aligned to the size of the access (see B2.2.1 ARM DDI 08476I.a).

On Arm32, the alignment is already enforced by the processor because HSCTLR.A bit is set (it enforce alignment for every access). For Arm64, this bit is not set because memcpy()/memset() can use unaligned access for performance reason (the implementation is taken from the Cortex library).

To avoid any overhead in production build, the alignment will only be checked using an ASSERT. Note that it might be possible to do it in production build using the acquire/exclusive version of load/store. But this is left to a follow-up (if wanted).

Quoted 4 lines
Quoted 35 lines

---

Changes from :-
v1 - 1. Referred to the latest Arm Architecture Reference Manual in the commit message.

v2 - 1. Updated commit message to specify the reason for using ASSERT().
2. Added Julien's SoB.

 xen/arch/arm/include/asm/atomic.h | 2 ++
 1 file changed, 2 insertions(+)
xen/arch/arm/include/asm/atomic.h +2 -0
index 1f60c28b1b..64314d59b3 100644
@@ -78,6 +78,7 @@ static always_inline void read_atomic_size(const volatile void *p,
                                            void *res,
                                            unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
     switch ( size )
     {
     case 1:
@@ -102,6 +103,7 @@ static always_inline void write_atomic_size(volatile void *p,
                                             void *val,
                                             unsigned int size)
 {
+    ASSERT(IS_ALIGNED((vaddr_t)p, size));
     switch ( size )
     {
     case 1:
Signature · 1 line
2.17.1
#3 lore raw

Hi,

Quoted 18 lines · Stefano Stabellini
Quoted 17 lines · Ayan Kumar Halder

From: Ayan Kumar Halder <[email protected]>

Xen provides helper to atomically read/write memory (see {read, write}_atomic()). Those helpers can only work if the address is aligned to the size of the access (see B2.2.1 ARM DDI 08476I.a).

On Arm32, the alignment is already enforced by the processor because HSCTLR.A bit is set (it enforce alignment for every access). For Arm64, this bit is not set because memcpy()/memset() can use unaligned access for performance reason (the implementation is taken from the Cortex library).

To avoid any overhead in production build, the alignment will only be checked using an ASSERT. Note that it might be possible to do it in production build using the acquire/exclusive version of load/store. But this is left to a follow-up (if wanted).

Quoted 6 lines
Quoted 4 lines

I have pushed this patch in a branch for-next/4.18 on my public repo. I will apply the patch to staging once the tree re-opened.

Cheers,

Signature · 1 line
Julien Grall