Skip to contentSkip to conversation

Adjustment after introducing ASSERT_ALLOC_CONTEXT

PATCH Superseded Henry Wang 2 participants 7 messages May 6, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

v2 cover Adjustment after introducing ASSERT_ALLOC_CONTEXT

With the enhanced ASSERT_ALLOC_CONTEXT, calling request_irq before local_irq_enable on secondary cores on Arm will lead to

(XEN) Xen call trace:
(XEN) [<000000000021d86c>] alloc_xenheap_pages+0x74/0x194 (PC) (XEN) [<000000000021d864>] alloc_xenheap_pages+0x6c/0x194 (LR) (XEN) [<0000000000229e90>] xmalloc_tlsf.c#xmalloc_pool_get+0x1c/0x28 (XEN) [<000000000022a270>] xmem_pool_alloc+0x21c/0x448 (XEN) [<000000000022a8dc>] _xmalloc+0x8c/0x290
(XEN) [<000000000026b57c>] request_irq+0x40/0xb8
(XEN) [<0000000000272780>] init_timer_interrupt+0x74/0xcc (XEN) [<000000000027212c>] start_secondary+0x1b4/0x238 (XEN) [<0000000084000200>] 0000000084000200
(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 4:
(XEN) Assertion '!in_irq() && (local_irq_is_enabled() || num_online_cpus() <= 1)' failed at common/page_alloc.c:2212 (XEN) ****************************************

on systems without a big enough pool for xmalloc() to cater the requested size. To solve this issue, this series introduces two patches. The first one defers the calling of request_irq on secondary CPUs after local_irq_enable on Arm. The second one moves the definition of ASSERT_ALLOC_CONTEXT to header and uses the ASSERT_ALLOC_CONTEXT to replace the original assertion in xmalloc().

Henry Wang (2):
xen/arm: Defer request_irq on secondary CPUs after local_irq_enable xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

 xen/arch/arm/smpboot.c    | 12 +++++++++---
 xen/common/page_alloc.c   |  7 -------
 xen/common/xmalloc_tlsf.c |  2 +-
 xen/include/xen/irq.h     |  7 +++++++
 4 files changed, 17 insertions(+), 11 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/2 xen/arm: Defer request_irq on secondary CPUs after local_irq_enable

With the enhanced ASSERT_ALLOC_CONTEXT, calling request_irq before local_irq_enable on secondary cores will lead to

(XEN) Xen call trace:
(XEN) [<000000000021d86c>] alloc_xenheap_pages+0x74/0x194 (PC) (XEN) [<000000000021d864>] alloc_xenheap_pages+0x6c/0x194 (LR) (XEN) [<0000000000229e90>] xmalloc_tlsf.c#xmalloc_pool_get+0x1c/0x28 (XEN) [<000000000022a270>] xmem_pool_alloc+0x21c/0x448 (XEN) [<000000000022a8dc>] _xmalloc+0x8c/0x290
(XEN) [<000000000026b57c>] request_irq+0x40/0xb8
(XEN) [<0000000000272780>] init_timer_interrupt+0x74/0xcc (XEN) [<000000000027212c>] start_secondary+0x1b4/0x238 (XEN) [<0000000084000200>] 0000000084000200
(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 4:
(XEN) Assertion '!in_irq() && (local_irq_is_enabled() || num_online_cpus() <= 1)' failed at common/page_alloc.c:2212 (XEN) ****************************************

on systems without a big enough pool for xmalloc() to cater the requested size.

Moving the call of request_irq() past local_irq_enable() on secondary cores will make sure the assertion condition in alloc_xenheap_pages(), i.e. !in_irq && local_irq_enabled() is satisfied. It is also safe because the timer and GIC maintenance interrupt will not be used until the CPU is fully online.

---
v1 -> v2:
- Explain why the moving of code is safe in the commit message and add comments.

---
 xen/arch/arm/smpboot.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
xen/arch/arm/smpboot.c +9 -3
index 7bfd0a73a7..9bb32a301a 100644
@@ -361,9 +361,6 @@ void start_secondary(void)
 
     init_secondary_IRQ();
 
-    init_maintenance_interrupt();
-    init_timer_interrupt();
-
     set_current(idle_vcpu[cpuid]);
 
     setup_cpu_sibling_map(cpuid);
@@ -380,6 +377,15 @@ void start_secondary(void)
     cpumask_set_cpu(cpuid, &cpu_online_map);
 
     local_irq_enable();
+
+    /*
+     * Calling request_irq() after local_irq_enable() on secondary cores
+     * will make sure the assertion condition in alloc_xenheap_pages(),
+     * i.e. !in_irq && local_irq_enabled() is satisfied.
+     */
+    init_maintenance_interrupt();
+    init_timer_interrupt();
+
     local_abort_enable();
 
     check_local_cpu_errata();
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 2/2 xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

In reply to Henry Wang

xmalloc() will use a pool for allocation smaller than a page. The pool is extended only when there are no more space. At which point, alloc_xenheap_pages() is called to add more memory.

xmalloc() must be protected by ASSERT_ALLOC_CONTEXT. It should not rely on pool expanding to trigger the ASSERT_ALLOC_CONTEXT in alloc_xenheap_pages(). Hence, this commit moves the definition of ASSERT_ALLOC_CONTEXT to header and uses the ASSERT_ALLOC_CONTEXT to replace the original assertion in xmalloc().

---
v1 -> v2:
- No changes

---
 xen/common/page_alloc.c   | 7 -------
 xen/common/xmalloc_tlsf.c | 2 +-
 xen/include/xen/irq.h     | 7 +++++++
 3 files changed, 8 insertions(+), 8 deletions(-)
xen/common/page_alloc.c +0 -7
index e866e0d864..ea59cd1a4a 100644
@@ -162,13 +162,6 @@
 static char __initdata opt_badpage[100] = "";
 string_param("badpage", opt_badpage);
 
-/*
- * Heap allocations may need TLB flushes which may require IRQs to be
- * enabled (except when only 1 PCPU is online).
- */
-#define ASSERT_ALLOC_CONTEXT() \
-    ASSERT(!in_irq() && (local_irq_is_enabled() || num_online_cpus() <= 1))
-
 /*
  * no-bootscrub -> Free pages are not zeroed during boot.
  */
xen/common/xmalloc_tlsf.c +1 -1
index d2ad909502..b8f838ae74 100644
@@ -594,7 +594,7 @@ void *_xmalloc(unsigned long size, unsigned long align)
 {
     void *p = NULL;
 
-    ASSERT(!in_irq());
+    ASSERT_ALLOC_CONTEXT();
 
     if ( !size )
         return ZERO_BLOCK_PTR;
xen/include/xen/irq.h +7 -0
index d8beadd16b..300625e56d 100644
@@ -10,6 +10,13 @@
 #include <asm/hardirq.h>
 #include <public/event_channel.h>
 
+/*
+ * Heap allocations may need TLB flushes which may require IRQs to be
+ * enabled (except when only 1 PCPU is online).
+ */
+#define ASSERT_ALLOC_CONTEXT() \
+    ASSERT(!in_irq() && (local_irq_is_enabled() || num_online_cpus() <= 1))
+
 struct irqaction {
     void (*handler)(int, void *, struct cpu_user_regs *);
     const char *name;
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2 2/2] xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long align) · line 597

Quoted 8 lines · Henry Wang
xen/common/xmalloc_tlsf.c +1 -1
@@ -594,7 +594,7 @@ void *_xmalloc(unsigned long size, unsigned long align)
 {
     void *p = NULL;
 
-    ASSERT(!in_irq());
+    ASSERT_ALLOC_CONTEXT();

For one - what about xfree()?

And then did you consider taking the opportunity and moving both to the respective pool alloc functions, thus giving even better coverage? Granted there's one downside to moving it to xmem_pool_alloc(): Then the early zero-size and error returns won't be covered, so maybe we actually want checks in both places. In xfree() I think the check would then also want to move ahead of the early return.

Jan

author
#4 lore raw

RE: [PATCH v2 2/2] xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

Hi Jan,

Quoted 18 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Friday, May 6, 2022 6:33 PM
To: Henry Wang <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Wei Liu <[email protected]>; Wei Chen <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH v2 2/2] xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long

Quoted 3 lines · Henry Wang
xen/common/xmalloc_tlsf.c +0 -0
@@ -594,7 +594,7 @@ void *_xmalloc(unsigned long size, unsigned long

align)

Quoted 7 lines

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long align) · line 597

Quoted 5 lines
xen/common/xmalloc_tlsf.c +1 -1
 {
     void *p = NULL;
 
-    ASSERT(!in_irq());
+    ASSERT_ALLOC_CONTEXT();

For one - what about xfree()?

Oh you are definitely correct, thanks for pointing this out. I will definitely change the assertion in xfree() as well in v3.

Quoted 3 lines

And then did you consider taking the opportunity and moving both to the respective pool alloc functions, thus giving even better coverage?

Yeah I would love to. But sorry about the question (just for learning):
I assume you are talking about code coverage, could you please kindly add a little bit more detail to help me understand why adding the same ASSERT_ALLOC_CONTEXT would help to a better coverage? Since...

Quoted 3 lines

Granted there's one downside to moving it to xmem_pool_alloc(): Then the early zero-size and error returns won't be covered, so maybe we actually want checks in both places.

...after reading these I have a feeling that we need to add the same ASSERT_ALLOC_CONTEXT in the beginning of the xmem_pool_alloc, xmalloc_whole_pages, and xmem_pool_free, while keeping ASSERT_ALLOC_CONTEXT in _xmalloc. I think xmem_pool_alloc and xmalloc_whole_pages are only called in _xmalloc and xmem_pool_free is only called in xfree. Adding the same assertion in these three functions is duplication of code?

Thank you for your explanation and patience.

Quoted 2 lines

In xfree() I think the check
would then also want to move ahead of the early return.

Sure, will do as you suggested.

Kind regards,
Henry

Quoted 2 lines

Jan

#5 lore raw

Re: [PATCH v2 2/2] xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

Quoted 29 lines · Henry Wang
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Friday, May 6, 2022 6:33 PM

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long

Quoted 3 lines · Henry Wang
xen/common/xmalloc_tlsf.c +0 -0
@@ -594,7 +594,7 @@ void *_xmalloc(unsigned long size, unsigned long

align)

Quoted 7 lines

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long align) · line 597

Quoted 5 lines
xen/common/xmalloc_tlsf.c +1 -1
 {
     void *p = NULL;
 
-    ASSERT(!in_irq());
+    ASSERT_ALLOC_CONTEXT();

For one - what about xfree()?

Oh you are definitely correct, thanks for pointing this out. I will definitely change the assertion in xfree() as well in v3.

Quoted 3 lines

And then did you consider taking the opportunity and moving both to the respective pool alloc functions, thus giving even better coverage?

Yeah I would love to. But sorry about the question (just for learning):
I assume you are talking about code coverage, could you please kindly add a little bit more detail to help me understand why adding the same ASSERT_ALLOC_CONTEXT would help to a better coverage? Since...

Quoted 7 lines
Quoted 3 lines

Granted there's one downside to moving it to xmem_pool_alloc(): Then the early zero-size and error returns won't be covered, so maybe we actually want checks in both places.

...after reading these I have a feeling that we need to add the same ASSERT_ALLOC_CONTEXT in the beginning of the xmem_pool_alloc, xmalloc_whole_pages, and xmem_pool_free,

xmem_pool_{alloc,free}() are what my comment was about. And "coverage" was meant as "if the assertions were there, more [potential] call sites would be covered". xmalloc_whole_pages(), as you ...

Quoted 5 lines

while keeping
ASSERT_ALLOC_CONTEXT in _xmalloc. I think xmem_pool_alloc and xmalloc_whole_pages are only called in _xmalloc and xmem_pool_free is only called in xfree. Adding the same assertion in these three functions is duplication of code?

... validly note, is of no interest in this regard, as it's (1) a static helper and (2) would hit the checks in page_alloc.c. xmem_pool_{alloc,free}() otoh are non-static functions, so we will want to care about not only existing callers, but also potential future ones.

Jan

author
#6 lore raw

RE: [PATCH v2 2/2] xen/common: Use enhanced ASSERT_ALLOC_CONTEXT in xmalloc()

Hi Jan,

Quoted 51 lines

-----Original Message-----
From: Jan Beulich <[email protected]>

Quoted 29 lines · Henry Wang
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Friday, May 6, 2022 6:33 PM

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long

Quoted 3 lines · Henry Wang
xen/common/xmalloc_tlsf.c +0 -0
@@ -594,7 +594,7 @@ void *_xmalloc(unsigned long size, unsigned long

align)

Quoted 7 lines

xen/common/xmalloc_tlsf.c void *_xmalloc(unsigned long size, unsigned long align) · line 597

Quoted 5 lines
xen/common/xmalloc_tlsf.c +1 -1
 {
     void *p = NULL;
 
-    ASSERT(!in_irq());
+    ASSERT_ALLOC_CONTEXT();

For one - what about xfree()?

Oh you are definitely correct, thanks for pointing this out. I will definitely change the assertion in xfree() as well in v3.

Quoted 3 lines

And then did you consider taking the opportunity and moving both to the respective pool alloc functions, thus giving even better coverage?

Yeah I would love to. But sorry about the question (just for learning):
I assume you are talking about code coverage, could you please kindly add a little bit more detail to help me understand why adding the same ASSERT_ALLOC_CONTEXT would help to a better coverage? Since...

Quoted 7 lines
Quoted 3 lines

Granted there's one downside to moving it to xmem_pool_alloc(): Then the early zero-size and error returns won't be covered, so maybe we actually want checks in both places.

...after reading these I have a feeling that we need to add the same ASSERT_ALLOC_CONTEXT in the beginning of the xmem_pool_alloc, xmalloc_whole_pages, and xmem_pool_free,

xmem_pool_{alloc,free}() are what my comment was about. And "coverage" was meant as "if the assertions were there, more [potential] call sites would be covered". xmalloc_whole_pages(), as you ...

Quoted 5 lines

while keeping
ASSERT_ALLOC_CONTEXT in _xmalloc. I think xmem_pool_alloc and xmalloc_whole_pages are only called in _xmalloc and xmem_pool_free is only called in xfree. Adding the same assertion in these three functions is duplication of code?

Quoted 4 lines

... validly note, is of no interest in this regard, as it's (1) a static helper and (2) would hit the checks in page_alloc.c. xmem_pool_{alloc,free}() otoh are non-static functions, so we will want to care about not only existing callers, but also potential future ones.

Great thanks for your explanation. This makes great sense, so I will add ASSERT_ALLOC_CONTEXT in the beginning of xmem_pool_{alloc,free}().

Kind regards,
Henry

Quoted 2 lines

Jan