Skip to contentSkip to conversation

xen/xmalloc: XMEM_POOL_POISON improvements

PATCH In review Andrew Cooper 4 participants 6 messages Oct 20, 2023 – Oct 23, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/xmalloc: XMEM_POOL_POISON improvements

When in use, the spew:

(XEN) Assertion '!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE, (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE)' failed at common/xmalloc_tlsf.c:246

is unweidly and meaningless to non-Xen developers. Therefore:

* Switch to IS_ENABLED(). There's no need for full #ifdef-ary.
* Pull memchr_inv() out into the if(), and provide a better error message.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>

Observations from the debugging of:
https://github.com/Dasharo/dasharo-issues/issues/488

There's a further bug. XMEM_POOL_POISON can be enabled in release builds, where the ASSERT() gets dropped.

However, upping to a BUG() can't provide any helpful message out to the user.

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

---
 xen/common/xmalloc_tlsf.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
xen/common/xmalloc_tlsf.c +7 -8
index 349b31cb4cc1..13305cd87c2f 100644
@@ -249,11 +249,11 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl,
     }
     b->ptr.free_ptr = (struct free_ptr) {NULL, NULL};
 
-#ifdef CONFIG_XMEM_POOL_POISON
-    if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
-        ASSERT(!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
-                           (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE));
-#endif /* CONFIG_XMEM_POOL_POISON */
+    if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
+         (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE &&
+         memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
+                    (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE) )
+        ASSERT(!"XMEM Pool corruption found");
 }
 
 /**
@@ -261,11 +261,10 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl,
  */
 static inline void INSERT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl, int sl)
 {
-#ifdef CONFIG_XMEM_POOL_POISON
-    if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
+    if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
+         (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
         memset(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
                (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE);
-#endif /* CONFIG_XMEM_POOL_POISON */
 
     b->ptr.free_ptr = (struct free_ptr) {NULL, p->matrix[fl][sl]};
     if ( p->matrix[fl][sl] )
Signature · 1 line
2.30.2
#1 lore raw

Hi Andrew,

Quoted 21 lines · Andrew Cooper

When in use, the spew:

   (XEN) Assertion '!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE, (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE)' failed at common/xmalloc_tlsf.c:246

is unweidly and meaningless to non-Xen developers. Therefore:

* Switch to IS_ENABLED(). There's no need for full #ifdef-ary.
* Pull memchr_inv() out into the if(), and provide a better error message.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>

Observations from the debugging of:
https://github.com/Dasharo/dasharo-issues/issues/488

Quoted 7 lines

There's a further bug. XMEM_POOL_POISON can be enabled in release builds, where the ASSERT() gets dropped.

However, upping to a BUG() can't provide any helpful message out to the user.

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

Do we actually care about the registers values? If not, we can either use:

printk(...);
BUG();

Or

panic(...);

This would allow us to use XMEM_POOL_POISON in prod build before BUG() can accept string.

Quoted 29 lines
---
  xen/common/xmalloc_tlsf.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
xen/common/xmalloc_tlsf.c +5 -5
index 349b31cb4cc1..13305cd87c2f 100644
@@ -249,11 +249,11 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl,
      }
      b->ptr.free_ptr = (struct free_ptr) {NULL, NULL};
 
-#ifdef CONFIG_XMEM_POOL_POISON
-    if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
-        ASSERT(!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
-                           (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE));
-#endif /* CONFIG_XMEM_POOL_POISON */
+    if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
+         (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE &&
+         memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
+                    (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE) )
+        ASSERT(!"XMEM Pool corruption found");
  }
 
  /**
@@ -261,11 +261,10 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl,
   */
  static inline void INSERT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl, int sl)
  {

xen/common/xmalloc_tlsf.c static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int fl, · line 270

Quoted 10 lines
xen/common/xmalloc_tlsf.c +2 -3
-#ifdef CONFIG_XMEM_POOL_POISON
-    if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
+    if ( IS_ENABLED(CONFIG_XMEM_POOL_POISON) &&
+         (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE )
          memset(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE,
                 (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE);
-#endif /* CONFIG_XMEM_POOL_POISON */
 
      b->ptr.free_ptr = (struct free_ptr) {NULL, p->matrix[fl][sl]};
      if ( p->matrix[fl][sl] )

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 44 lines · Julien Grall

Hi Andrew,

Quoted 28 lines · Andrew Cooper

When in use, the spew:

   (XEN) Assertion '!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE, (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE)' failed at common/xmalloc_tlsf.c:246

is unweidly and meaningless to non-Xen developers.  Therefore:

  * Switch to IS_ENABLED().  There's no need for full #ifdef-ary.
  * Pull memchr_inv() out into the if(), and provide a better error message.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>

Observations from the debugging of:
   https://github.com/Dasharo/dasharo-issues/issues/488

There's a further bug.  XMEM_POOL_POISON can be enabled in release builds,
where the ASSERT() gets dropped.

Quoted 6 lines

However, upping to a BUG() can't provide any helpful message out to the user.

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

Do we actually care about the registers values? If not, we can either use:

printk(...);
BUG();

Quoted 6 lines

Or

panic(...);

This would allow us to use XMEM_POOL_POISON in prod build before BUG() can accept string.

We care about the backtrace, so panic() on it's own isn't good enough.

But printk();BUG(); should be good enough so I'll swap to that.

~Andrew

#3 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

When in use, the spew:

(XEN) Assertion '!memchr_inv(b->ptr.buffer + MIN_BLOCK_SIZE, POISON_BYTE, (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE)' failed at common/xmalloc_tlsf.c:246

is unweidly and meaningless to non-Xen developers.

Just to mention it (no objection to the changes done): While I agree with unwieldy (and I'd add "of limited use"), an assertion message not necessarily being meaningful to non-Xen developers is imo not really a criteria - that's to be expected in various cases.

Quoted 4 lines

Therefore:

* Switch to IS_ENABLED(). There's no need for full #ifdef-ary.
* Pull memchr_inv() out into the if(), and provide a better error message.

Thing is - this "better" error message now ends up being less specific, and hence could mean also other kinds of corruption.

Quoted 9 lines

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>

Quoted 5 lines

Observations from the debugging of:
https://github.com/Dasharo/dasharo-issues/issues/488

There's a further bug. XMEM_POOL_POISON can be enabled in release builds, where the ASSERT() gets dropped.

Just to mention it - this limits usefulness, but doesn't make the option entirely useless. The poisoning itself also allows certain cases of use- after-free to be caught. The assertion really is "only" about write-after- free. (Maybe the improved error message could indicate so?)

Quoted 4 lines

However, upping to a BUG() can't provide any helpful message out to the user.

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

I agree with Julien's suggestion of using panic() in the meantime, as a possible alternative. Question though is whether it's better to halt the system right away, as opposed to e.g. permitting orderly shutdown to cover the case where the corruption ends up not being "deadly".

Jan

#4 lore raw
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper

However, upping to a BUG() can't provide any helpful message out to the user.

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

I agree with Julien's suggestion of using panic() in the meantime, as a possible alternative.

We might care about the stack trace, so would be helpful to print it, maybe WARN + panic?

Quoted 3 lines

Question though is whether it's better to halt the
system right away, as opposed to e.g. permitting orderly shutdown to cover the case where the corruption ends up not being "deadly".

Hm, won't this be risky, as we could then possibly corrupt data on disk for example?

Thanks, Roger.

#5 lore raw
Quoted 4 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

However, upping to a BUG() can't provide any helpful message out to the user.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I tried modifying BUG() to take an optional message, but xen/bug.h needs untangling substantially before that will work, and I don't have time right now.

I agree with Julien's suggestion of using panic() in the meantime, as a possible alternative.

We might care about the stack trace, so would be helpful to print it, maybe WARN + panic?

Perhaps, yes.

Quoted 6 lines
Quoted 3 lines

Question though is whether it's better to halt the
system right away, as opposed to e.g. permitting orderly shutdown to cover the case where the corruption ends up not being "deadly".

Hm, won't this be risky, as we could then possibly corrupt data on disk for example?

It is risky - you never really know which of the two options is better.

Jan