Skip to contentSkip to conversation

xvmalloc: adjust XVFREE() ordering

PATCH In review Jan Beulich 3 participants 5 messages Apr 16 – Apr 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xvmalloc: adjust XVFREE() ordering

What c4f427ec879e ("xen: Swap order of actions in the FREE*() macros") did should have been done right away when XVFREE() was introduced.

Amends: 9102fcd9579f ("mm: introduce xvmalloc() et al and use for grant table allocations")

xen/include/xen/xvmalloc.h +2 -1
@@ -71,8 +71,9 @@ void *_xvrealloc(void *va, size_t size,
 
 /* Free an allocation, and zero the pointer to it. */
 #define XVFREE(p) do { \
-    xvfree(p);         \
+    void *_ptr_ = (p); \
     (p) = NULL;        \
+    xvfree(_ptr_);     \
 } while ( false )
 
 static inline void *_xvmalloc_array(
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Jan Beulich

What c4f427ec879e ("xen: Swap order of actions in the FREE*() macros") did should have been done right away when XVFREE() was introduced.

Amends: 9102fcd9579f ("mm: introduce xvmalloc() et al and use for grant table allocations")

No intention to stir controversy, but I feel like this should better use the Fixes tag, as it's not an omission or similar, but code fix.

Thanks, Roger.

#2 lore raw
Quoted 8 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

What c4f427ec879e ("xen: Swap order of actions in the FREE*() macros") did should have been done right away when XVFREE() was introduced.

Amends: 9102fcd9579f ("mm: introduce xvmalloc() et al and use for grant table allocations")

Thanks.

Quoted 2 lines

No intention to stir controversy, but I feel like this should better use the Fixes tag, as it's not an omission or similar, but code fix.

I would have used Fixes: if I was able to spot a use where the difference would actually matter in existing code. I can switch, but in the absence thereof I deemed Amends: (marginally) more precise.

Jan

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

What c4f427ec879e ("xen: Swap order of actions in the FREE*() macros") did should have been done right away when XVFREE() was introduced.

Amends: 9102fcd9579f ("mm: introduce xvmalloc() et al and use for grant table allocations")

Quoted 8 lines
Quoted 1 line

Thanks.

Quoted 2 lines

No intention to stir controversy, but I feel like this should better use the Fixes tag, as it's not an omission or similar, but code fix.

I would have used Fixes: if I was able to spot a use where the difference would actually matter in existing code. I can switch, but in the absence thereof I deemed Amends: (marginally) more precise.

It's speculative defence in depth.

Zeroing the pointer in memory before freeing the block is strictly better than the other way around.

For the local CPU it prevents speculative use-after-free (the store queue is always in program order), and whether it does this for remote CPUs depends on how strong the platform memory ordering is.

This all came from the Ghostrace paper.

~Andrew

#4 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

What c4f427ec879e ("xen: Swap order of actions in the FREE*() macros") did should have been done right away when XVFREE() was introduced.

Amends: 9102fcd9579f ("mm: introduce xvmalloc() et al and use for grant table allocations")

On Arm64, and only for alpine-3.18-gcc-debug-arm64-boot-cpupools, this reproducibly causes:

.xen-syms.2.o.sym renamed from .xen-syms.1.o.sym +6 -6
diff -u  ./.xen-syms.1.o.sym  ./.xen-syms.2.o.sym
@@ -7,11 +7,11 @@
 0000000000000000 l    d  .bss	0000000000000000 .bss
 0000000000000000 l    d  .rodata	0000000000000000 .rodata
 0000000000000000 l    d  .note.GNU-stack	0000000000000000 .note.GNU-stack
-0000000000000000 g     O .rodata	0000000000004dd8 .hidden symbols_addresses
-0000000000004dd8 g     O .rodata	0000000000000004 .hidden symbols_num_addrs
-0000000000004ddc g     O .rodata	0000000000007721 .hidden symbols_names
-000000000000c500 g     O .rodata	0000000000000028 .hidden symbols_markers
-000000000000c528 g     O .rodata	00000000000003b5 .hidden symbols_token_table
-000000000000c8de g     O .rodata	0000000000000200 .hidden symbols_token_index
+0000000000000000 g     O .rodata	0000000000004de8 .hidden symbols_addresses
+0000000000004de8 g     O .rodata	0000000000000004 .hidden symbols_num_addrs
+0000000000004dec g     O .rodata	0000000000007749 .hidden symbols_names
+000000000000c538 g     O .rodata	0000000000000028 .hidden symbols_markers
+000000000000c560 g     O .rodata	00000000000003b5 .hidden symbols_token_table
+000000000000c916 g     O .rodata	0000000000000200 .hidden symbols_token_index
 

There are only two uses of XVFREE() in code that is built for Arm. I'll see if I can repro locally with the exact same .config, but I fear my chances are slim.

It feels wrong to push the patch nevertheless, but it also feels wrong to have to keep it out.

Jan

Quoted 13 lines
xen/include/xen/xvmalloc.h +2 -1
@@ -71,8 +71,9 @@ void *_xvrealloc(void *va, size_t size,
 
 /* Free an allocation, and zero the pointer to it. */
 #define XVFREE(p) do { \
-    xvfree(p);         \
+    void *_ptr_ = (p); \
     (p) = NULL;        \
+    xvfree(_ptr_);     \
 } while ( false )
 
 static inline void *_xvmalloc_array(