Skip to contentSkip to conversation

xen/watchdog: Identify which domain watchdog fired

PATCH Superseded Andrew Cooper 4 participants 14 messages Mar 3, 2025 – Mar 10, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover xen/watchdog: Identify which domain watchdog fired

See patch 2 for rational.

Since v1, use struct domain's alignment as the safety check.

Andrew Cooper (2):
xen/domain: Annotate struct domain as page aligned xen/watchdog: Identify which domain watchdog fired

 xen/common/sched/core.c | 23 ++++++++++++++++++++---
 xen/include/xen/sched.h |  2 +-
 2 files changed, 21 insertions(+), 4 deletions(-)

base-commit: e95dc03717b8ae00de2a0b41b88905af6170b210

Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 1/2 xen/domain: Annotate struct domain as page aligned

struct domain is always a page aligned allocation. Update it's type to reflect this, so we can safely reuse the lower bits in the pointer for auxiliary information.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

v2:
* New

Interestingly this does cause two changes in the resulting binary, both caused by GCC electing to use a more complicated addressing mode to save one ADD instruction.

---
 xen/include/xen/sched.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/sched.h +1 -1
index 037c83fda219..8412b45178ca 100644
@@ -645,7 +645,7 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
-};
+} __aligned(PAGE_SIZE);
 
 static inline struct page_list_head *page_to_list(
     struct domain *d, const struct page_info *pg)
Signature · 1 line
2.39.5
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] xen/domain: Annotate struct domain as page aligned

Quoted 7 lines · Andrew Cooper

struct domain is always a page aligned allocation. Update it's type to reflect this, so we can safely reuse the lower bits in the pointer for auxiliary information.

No functional change.

Quoted 37 lines

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

v2:
* New

Interestingly this does cause two changes in the resulting binary, both caused by GCC electing to use a more complicated addressing mode to save one ADD instruction.

---
 xen/include/xen/sched.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/sched.h +1 -1
index 037c83fda219..8412b45178ca 100644
@@ -645,7 +645,7 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
-};
+} __aligned(PAGE_SIZE);
 
 static inline struct page_list_head *page_to_list(
     struct domain *d, const struct page_info *pg)
Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH v2 1/2] xen/domain: Annotate struct domain as page aligned

In reply to Andrew Cooper
Quoted 18 lines · Andrew Cooper

struct domain is always a page aligned allocation. Update it's type to reflect this, so we can safely reuse the lower bits in the pointer for auxiliary information.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

Quoted 6 lines

v2:
* New

Interestingly this does cause two changes in the resulting binary, both caused by GCC electing to use a more complicated addressing mode to save one ADD instruction.

That's on x86, I suppose?

xen/include/xen/sched.h struct domain · line 651

Quoted 11 lines
xen/include/xen/sched.h +1 -1
@@ -645,7 +645,7 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
-};
+} __aligned(PAGE_SIZE);
 
 static inline struct page_list_head *page_to_list(
     struct domain *d, const struct page_info *pg)

I understand struct domain is where you need the annotation right away, but is there a reason not to do the same for struct vcpu right away?

Jan

#4 lore raw

Re: [PATCH v2 1/2] xen/domain: Annotate struct domain as page aligned

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Andrew Cooper

struct domain is always a page aligned allocation. Update it's type to reflect this, so we can safely reuse the lower bits in the pointer for auxiliary information.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

Quoted 7 lines
Quoted 6 lines

v2:
* New

Interestingly this does cause two changes in the resulting binary, both caused by GCC electing to use a more complicated addressing mode to save one ADD instruction.

That's on x86, I suppose?

Yes.

Quoted 10 lines
Quoted 10 lines
xen/include/xen/sched.h +1 -1
@@ -645,7 +645,7 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
-};
+} __aligned(PAGE_SIZE);
 
Quoted 4 lines

xen/include/xen/sched.h struct domain · line 651

Quoted 2 lines
xen/include/xen/sched.h +0 -0
 static inline struct page_list_head *page_to_list(
     struct domain *d, const struct page_info *pg)

I understand struct domain is where you need the annotation right away, but is there a reason not to do the same for struct vcpu right away?

struct vcpu is more complicated.  It's multi-page on ARM, and I have a strong suspicion that alignment is going to have to change away from PAGE_SIZE for architectures wanting to use larger page sizes.

e.g. with 64k pagetables, I expect these will still want 4k alignment, and therefore they'll all want changing. i.e. I'm probably creating less work for someone in the future by not annotating struct vcpu. ~Andrew

#5 lore raw

Re: [PATCH v2 1/2] xen/domain: Annotate struct domain as page aligned

Quoted 16 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
xen/include/xen/sched.h +1 -1
@@ -645,7 +645,7 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
-};
+} __aligned(PAGE_SIZE);
 
Quoted 4 lines

xen/include/xen/sched.h struct domain · line 651

Quoted 2 lines
xen/include/xen/sched.h +0 -0
 static inline struct page_list_head *page_to_list(
     struct domain *d, const struct page_info *pg)

I understand struct domain is where you need the annotation right away, but is there a reason not to do the same for struct vcpu right away?

Quoted 7 lines

struct vcpu is more complicated.  It's multi-page on ARM, and I have a strong suspicion that alignment is going to have to change away from PAGE_SIZE for architectures wanting to use larger page sizes.

e.g. with 64k pagetables, I expect these will still want 4k alignment, and therefore they'll all want changing. i.e. I'm probably creating less work for someone in the future by not annotating struct vcpu. ~Andrew

Wouldn't the same hold for struct domain?

Jan

#6 lore raw

PATCH v2 2/2 xen/watchdog: Identify which domain watchdog fired

In reply to Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

v2:
* BUILD_BUG_ON() against the alignment of d.

---
 xen/common/sched/core.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
xen/common/sched/core.c +20 -3
index d6296d99fdb9..3db0fe32ccd8 100644
@@ -1534,12 +1534,19 @@ long vcpu_yield(void)
 
 static void cf_check domain_watchdog_timeout(void *data)
 {
-    struct domain *d = data;
+    /*
+     * The data parameter encodes the watchdog id in the low bits of
+     * the domain pointer.
+     */
+    struct domain *d = _p((unsigned long)data & PAGE_MASK);
+    unsigned int id = (unsigned long)data & ~PAGE_MASK;
+
+    BUILD_BUG_ON(!(alignof(d) < PAGE_SIZE));
 
     if ( d->is_shutting_down || d->is_dying )
         return;
 
-    printk("Watchdog timer fired for domain %u\n", d->domain_id);
+    printk("Watchdog timer %u fired for %pd\n", id, d);
     domain_shutdown(d, SHUTDOWN_watchdog);
 }
 
@@ -1593,7 +1600,17 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+    {
+        void *data = d;
+
+        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(d));
+
+        /*
+         * For the timer callback parameter, encode the watchdog id in
+         * the low bits of the domain pointer.
+         */
+        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, data + i, 0);
+    }
 }
 
 void watchdog_domain_destroy(struct domain *d)
Signature · 1 line
2.39.5
#7 lore raw
Quoted 7 lines · Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

Quoted 8 lines

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

The code looks like it would work as intended.

I checked with the MISRA rules and it looks like it would fall under the allowed exception. Please have a run through ECLAIR to make sure it doesn't cause regressions (especially R11.2).

Quoted 65 lines

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

v2:
* BUILD_BUG_ON() against the alignment of d.

---
 xen/common/sched/core.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
xen/common/sched/core.c +20 -3
index d6296d99fdb9..3db0fe32ccd8 100644
@@ -1534,12 +1534,19 @@ long vcpu_yield(void)
 
 static void cf_check domain_watchdog_timeout(void *data)
 {
-    struct domain *d = data;
+    /*
+     * The data parameter encodes the watchdog id in the low bits of
+     * the domain pointer.
+     */
+    struct domain *d = _p((unsigned long)data & PAGE_MASK);
+    unsigned int id = (unsigned long)data & ~PAGE_MASK;
+
+    BUILD_BUG_ON(!(alignof(d) < PAGE_SIZE));
 
     if ( d->is_shutting_down || d->is_dying )
         return;
 
-    printk("Watchdog timer fired for domain %u\n", d->domain_id);
+    printk("Watchdog timer %u fired for %pd\n", id, d);
     domain_shutdown(d, SHUTDOWN_watchdog);
 }
 
@@ -1593,7 +1600,17 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+    {
+        void *data = d;
+
+        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(d));
+
+        /*
+         * For the timer callback parameter, encode the watchdog id in
+         * the low bits of the domain pointer.
+         */
+        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, data + i, 0);
+    }
 }
 
 void watchdog_domain_destroy(struct domain *d)
Signature · 1 line
2.39.5
#8 lore raw
Quoted 12 lines · Stefano Stabellini
Quoted 11 lines · Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Quoted 5 lines
Quoted 4 lines

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

The code looks like it would work as intended.

It's already deployed into production[1].  (Guess /why/ we need to distinguish the two watchdogs.)

Quoted 3 lines

I checked with the MISRA rules and it looks like it would fall under the allowed exception. Please have a run through ECLAIR to make sure it doesn't cause regressions (especially R11.2).

Eclair is happy:

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1700050949

~Andrew

[1] Actually, the version that took me 2 minute to write one evening over beer after work when the initial request for help came in. Followed shortly by an explanation of how alignment works.

#9 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper
xen/common/sched/core.c +0 -0
@@ -1534,12 +1534,19 @@ long vcpu_yield(void)
 
 static void cf_check domain_watchdog_timeout(void *data)

xen/common/sched/core.c long vcpu_yield(void) · line 1544

Quoted 10 lines
xen/common/sched/core.c +8 -1
 {
-    struct domain *d = data;
+    /*
+     * The data parameter encodes the watchdog id in the low bits of
+     * the domain pointer.
+     */
+    struct domain *d = _p((unsigned long)data & PAGE_MASK);
+    unsigned int id = (unsigned long)data & ~PAGE_MASK;
+
+    BUILD_BUG_ON(!(alignof(d) < PAGE_SIZE));
    BUILD_BUG_ON(alignof(*d) < PAGE_SIZE);

xen/common/sched/core.c void watchdog_domain_init(struct domain *d) · line 1606

Quoted 9 lines
xen/common/sched/core.c +4 -1
@@ -1593,7 +1600,17 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+    {
+        void *data = d;
+
+        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(d));
        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(*d));

Jan

#10 lore raw
Quoted 21 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
xen/common/sched/core.c +0 -0
@@ -1534,12 +1534,19 @@ long vcpu_yield(void)
 
 static void cf_check domain_watchdog_timeout(void *data)

xen/common/sched/core.c long vcpu_yield(void) · line 1544

Quoted 10 lines
xen/common/sched/core.c +8 -1
 {
-    struct domain *d = data;
+    /*
+     * The data parameter encodes the watchdog id in the low bits of
+     * the domain pointer.
+     */
+    struct domain *d = _p((unsigned long)data & PAGE_MASK);
+    unsigned int id = (unsigned long)data & ~PAGE_MASK;
+
+    BUILD_BUG_ON(!(alignof(d) < PAGE_SIZE));
    BUILD_BUG_ON(alignof(*d) < PAGE_SIZE);
Quoted 3 lines
xen/common/sched/core.c +0 -0
@@ -1593,7 +1600,17 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
Quoted 7 lines

xen/common/sched/core.c void watchdog_domain_init(struct domain *d) · line 1606

Quoted 6 lines
xen/common/sched/core.c +4 -1
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+    {
+        void *data = d;
+
+        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(d));
        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(*d));

Well, that's embarrassing.  That explains why I was having issues, and I was clearly asleep when writing...

~Andrew

#11 lore raw

PATCH v3 1/1 xen/watchdog: Identify which domain watchdog fired

In reply to Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Dario Faggioli <[email protected]>
CC: Juergen Gross <[email protected]>
CC: George Dunlap <[email protected]>

v3:
* Fix alignof() expressions.
v2:
* BUILD_BUG_ON() against the alignment of d.

---
 xen/common/sched/core.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
xen/common/sched/core.c +20 -3
index d6296d99fdb9..9043414290a8 100644
@@ -1534,12 +1534,19 @@ long vcpu_yield(void)
 
 static void cf_check domain_watchdog_timeout(void *data)
 {
-    struct domain *d = data;
+    /*
+     * The data parameter encodes the watchdog id in the low bits of
+     * the domain pointer.
+     */
+    struct domain *d = _p((unsigned long)data & PAGE_MASK);
+    unsigned int id = (unsigned long)data & ~PAGE_MASK;
+
+    BUILD_BUG_ON(alignof(*d) < PAGE_SIZE);
 
     if ( d->is_shutting_down || d->is_dying )
         return;
 
-    printk("Watchdog timer fired for domain %u\n", d->domain_id);
+    printk("Watchdog timer %u fired for %pd\n", id, d);
     domain_shutdown(d, SHUTDOWN_watchdog);
 }
 
@@ -1593,7 +1600,17 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+    {
+        void *data = d;
+
+        BUILD_BUG_ON(NR_DOMAIN_WATCHDOG_TIMERS > alignof(*d));
+
+        /*
+         * For the timer callback parameter, encode the watchdog id in
+         * the low bits of the domain pointer.
+         */
+        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, data + i, 0);
+    }
 }
 
 void watchdog_domain_destroy(struct domain *d)

base-commit: 4d4725901e474fb3a61814e3c7bcb563cbdd857a
prerequisite-patch-id: abf66c7a76178c5cf225f0c67efd6c430e95974a

Signature · 1 line
2.39.5
Reviewed-by on v3 1/1
#12 lore raw
Quoted 7 lines · Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

Quoted 8 lines

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

Reviewed-by on v3 1/1
#13 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

When a watchdog fires, the domain is crashed and can't dump any state.

Xen allows a domain to have two separate watchdogs. Therefore, for a domain running multiple watchdogs (e.g. one based around network, one for disk), it is important for diagnostics to know which watchdog fired.

Quoted 8 lines

As the printk() is in a timer callback, this is a bit awkward to arrange, but there are 12 spare bits in the bottom of the domain pointer owing to its alignment.

Reuse these bits to encode the watchdog id too, so the one which fired is identified when the domain is crashed.

Juergen