Skip to contentSkip to conversation

remove libxenctrl usage from xenstored

PATCH Superseded Juergen Gross 2 participants 26 messages Jan 7, 2025 – Jan 8, 2025

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover remove libxenctrl usage from xenstored

Xenstored is using libxenctrl for only one purpose: to get information about state of domains.

This patch series is removing that dependency by introducing a new stable interface which can be used by xenstored instead.

There was a RFC series sent out 3 years ago, which I have taken as a base and by addressing all comments from back then.

The main differences since that RFC series are:

- Instead of introducing an new main hypercall for a stable management interface I have just added a new domctl sub-op, as requested in 2021.

- I have added a new library libxenmanage for easy use of the new stable hypervisor interface. Main motivation for adding the library was the recent attempt to decouple oxenstored from the Xen git tree. By using the new library, oxenstored could benefit in the same way as xenstored from the new interface: it would be possible to rely on stable libraries only.

- Mini-OS has gained some more config options recently, so it was rather easy to make xenstore[pvh]-stubdom independent from libxenctrl, too.

Please note that the last patch can be committed only after the related Mini-OS patch "config: add support for libxenmanage" has gone in AND the Mini-OS commit-id has been updated in Config.mk accordingly! The Mini-OS patch has been Acked already, so it can go in as soon as patch 4 of this series (the one introducing libxenmanage) has been committed.

Changes in V2:
- new patch 1
- former patch 5 mover earlier, now patch 2 (can go in without the rest of the series)
- addressed comments

Changes in V3:
- addressed comments

Changes in V4:
- patches 1 and 3 of V3 dropped, as already committed
- addressed comments

Changes in V5:
- addressed comments

Changes in V6:
- patch 1 of V5 has been committed
- new patches 1-3 for fixing a race and avoiding new races with the added functionality (result of a comment by Jan Beulich)
- rework of locking in patch 4 (Jan Beulich)

Juergen Gross (7):
xen/events: fix race with set_global_virq_handler() xen/events: don't allow binding a global virq from any domain xen/events: allow setting of global virq handler only for unbound
virqs
xen: add bitmap to indicate per-domain state changes
xen: add new domctl get_changed_domain
tools/libs: add a new libxenmanage library tools/xenstored: use new stable interface instead of libxenctrl

 stubdom/Makefile                       |   8 +-
 stubdom/mini-os.mk                     |   1 +
 tools/flask/policy/modules/dom0.te     |   2 +-
 tools/flask/policy/modules/xen.if      |   4 +-
 tools/flask/policy/modules/xenstore.te |   1 +
 tools/include/xenmanage.h              |  92 ++++++++++++++
 tools/libs/Makefile                    |   1 +
 tools/libs/manage/Makefile             |  10 ++
 tools/libs/manage/Makefile.common      |   3 +
 tools/libs/manage/core.c               | 168 +++++++++++++++++++++++++
 tools/libs/manage/libxenmanage.map     |   8 ++
 tools/libs/uselibs.mk                  |   2 +
 tools/xenstored/Makefile               |   2 +-
 tools/xenstored/Makefile.common        |   2 +-
 tools/xenstored/core.h                 |   1 -
 tools/xenstored/domain.c               |  52 +++-----
 tools/xenstored/lu.c                   |   1 +
 tools/xenstored/lu_daemon.c            |   1 +
 xen/common/domain.c                    | 121 ++++++++++++++++++
 xen/common/domctl.c                    |  18 ++-
 xen/common/event_channel.c             |  94 ++++++++++++--
 xen/include/public/domctl.h            |  26 ++++
 xen/include/xen/event.h                |  11 ++
 xen/include/xen/sched.h                |   5 +
 xen/include/xsm/dummy.h                |   8 ++
 xen/include/xsm/xsm.h                  |   6 +
 xen/xsm/dummy.c                        |   1 +
 xen/xsm/flask/hooks.c                  |   7 ++
 xen/xsm/flask/policy/access_vectors    |   2 +
 29 files changed, 604 insertions(+), 54 deletions(-)
 create mode 100644 tools/include/xenmanage.h
 create mode 100644 tools/libs/manage/Makefile
 create mode 100644 tools/libs/manage/Makefile.common
 create mode 100644 tools/libs/manage/core.c
 create mode 100644 tools/libs/manage/libxenmanage.map
Signature · 1 line
2.43.0
#1 lore raw

PATCH v6 1/7 xen/events: fix race with set_global_virq_handler()

There is a possible race scenario between set_global_virq_handler() and clear_global_virq_handlers() targeting the same domain, which might result in that domain ending as a zombie domain.

In case set_global_virq_handler() is being called for a domain which is just dying, it might happen that clear_global_virq_handlers() is running first, resulting in set_global_virq_handler() taking a new reference for that domain and entering in the global_virq_handlers[] array afterwards. The reference will never be dropped, thus the domain will never be freed completely.

This can be fixed by checking the is_dying state of the domain inside the region guarded by global_virq_handlers_lock. In case the domain is dying, handle it as if the domain wouldn't exist, which will be the case in near future anyway.

Fixes 87521589aa6a ("xen: allow global VIRQ handlers to be delegated to other domains")

---
V6:
- new patch

---
 xen/common/event_channel.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
xen/common/event_channel.c +13 -3
index 8db2ca4ba2..f2b64c48fb 100644
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
 int set_global_virq_handler(struct domain *d, uint32_t virq)
 {
     struct domain *old;
+    int rc = 0;
 
     if (virq >= NR_VIRQS)
         return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
         return -EINVAL;
 
     spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }
+    else
+    {
+        old = global_virq_handlers[virq];
+        global_virq_handlers[virq] = d;
+    }
     spin_unlock(&global_virq_handlers_lock);
 
     if (old != NULL)
         put_domain(old);
 
-    return 0;
+    return rc;
 }
 
 static void clear_global_virq_handlers(struct domain *d)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v6 1/7] xen/events: fix race with set_global_virq_handler()

Quoted 12 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
 int set_global_virq_handler(struct domain *d, uint32_t virq)
 {
     struct domain *old;
+    int rc = 0;
 
     if (virq >= NR_VIRQS)
         return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
         return -EINVAL;

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1001

Quoted 10 lines
xen/common/event_channel.c +6 -2
 
     spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }

While I can see how this eliminates the zombie domain aspect, this doesn't fully eliminate the race. Doing so would require (also) using the domain's event lock. Assuming we're okay with the remaining race, imo a code comment would be needed to state this (including the fact that it's then unpredictable whether this operation might still succeed for a domain already having d->is_dying != DOMDYING_alive).

Plus the way you do it the early success path remains; ideally that case would also fail for an already dying domain.

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1007

Quoted 6 lines
xen/common/event_channel.c +5 -0
+    else
+    {
+        old = global_virq_handlers[virq];
+        global_virq_handlers[virq] = d;
+    }
     spin_unlock(&global_virq_handlers_lock);

Nit: Much like you do at the top of your addition, a new blank line at the bottom would be nice.

Jan

#3 lore raw

Re: [PATCH v6 1/7] xen/events: fix race with set_global_virq_handler()

Quoted 14 lines · Jan Beulich
Quoted 13 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
  int set_global_virq_handler(struct domain *d, uint32_t virq)
  {
      struct domain *old;
+    int rc = 0;
 
      if (virq >= NR_VIRQS)
          return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
          return -EINVAL;
 
Quoted 16 lines

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1001

Quoted 9 lines
xen/common/event_channel.c +6 -2
      spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }

While I can see how this eliminates the zombie domain aspect, this doesn't fully eliminate the race. Doing so would require (also) using the domain's event lock. Assuming we're okay with the remaining race, imo a code comment would be needed to state this (including the fact that it's then unpredictable whether this operation might still succeed for a domain already having d->is_dying != DOMDYING_alive).

AFAIU you mean that it is still possible to set a domain to handle a virq when it is in the process of going down, especially if is_dying is set just after it has been tested to be DOMDYING_alive?

I don't see this being a problem, as the same would happen if the domain would go down just a millisecond later. This is something we will never be able to handle.

And after all the call of clear_global_virq_handlers() will now reset the handling domain to the hardware domain in all cases.

Quoted 3 lines

Plus the way you do it the early success path remains; ideally that case would also fail for an already dying domain.

Same again: clear_global_virq_handlers() will reset the handling domain.

Quoted 10 lines

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1007

Quoted 7 lines
xen/common/event_channel.c +5 -0
+    else
+    {
+        old = global_virq_handlers[virq];
+        global_virq_handlers[virq] = d;
+    }
      spin_unlock(&global_virq_handlers_lock);

Nit: Much like you do at the top of your addition, a new blank line at the bottom would be nice.

Will add that if a respin is needed.

Juergen

#4 lore raw

Re: [PATCH v6 1/7] xen/events: fix race with set_global_virq_handler()

Quoted 32 lines · Juergen Gross
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
  int set_global_virq_handler(struct domain *d, uint32_t virq)
  {
      struct domain *old;
+    int rc = 0;
 
      if (virq >= NR_VIRQS)
          return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
          return -EINVAL;
 
Quoted 16 lines

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1001

Quoted 9 lines
xen/common/event_channel.c +6 -2
      spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }

While I can see how this eliminates the zombie domain aspect, this doesn't fully eliminate the race. Doing so would require (also) using the domain's event lock. Assuming we're okay with the remaining race, imo a code comment would be needed to state this (including the fact that it's then unpredictable whether this operation might still succeed for a domain already having d->is_dying != DOMDYING_alive).

Quoted 7 lines

AFAIU you mean that it is still possible to set a domain to handle a virq when it is in the process of going down, especially if is_dying is set just after it has been tested to be DOMDYING_alive?

I don't see this being a problem, as the same would happen if the domain would go down just a millisecond later. This is something we will never be able to handle.

Right, but the sequence of events in the case you mention is different: The insertion into the array would still happen while the domain isn't marked dying.

Quoted 2 lines

And after all the call of clear_global_virq_handlers() will now reset the handling domain to the hardware domain in all cases.

Of course, but in the meantime an event may be sent to such a domain already marked dying. That likely isn't going to cause problems, but is unexpected with what description here says is being addressed.

Quoted 4 lines
Quoted 2 lines

Plus the way you do it the early success path remains; ideally that case would also fail for an already dying domain.

Same again: clear_global_virq_handlers() will reset the handling domain.

Right.

In summary: As indicated, we may be okay with the remaining race, but then we also should be making clear that we've decided to leave it at that. Hence my earlier request: If we accept this, say (and briefly justify) this in a code comment.

Jan

#5 lore raw

Re: [PATCH v6 1/7] xen/events: fix race with set_global_virq_handler()

Quoted 55 lines · Jan Beulich
Quoted 32 lines · Juergen Gross
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
   int set_global_virq_handler(struct domain *d, uint32_t virq)
   {
       struct domain *old;
+    int rc = 0;
 
       if (virq >= NR_VIRQS)
           return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
           return -EINVAL;
 
Quoted 16 lines

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1001

Quoted 9 lines
xen/common/event_channel.c +6 -2
       spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }

While I can see how this eliminates the zombie domain aspect, this doesn't fully eliminate the race. Doing so would require (also) using the domain's event lock. Assuming we're okay with the remaining race, imo a code comment would be needed to state this (including the fact that it's then unpredictable whether this operation might still succeed for a domain already having d->is_dying != DOMDYING_alive).

Quoted 7 lines

AFAIU you mean that it is still possible to set a domain to handle a virq when it is in the process of going down, especially if is_dying is set just after it has been tested to be DOMDYING_alive?

I don't see this being a problem, as the same would happen if the domain would go down just a millisecond later. This is something we will never be able to handle.

Right, but the sequence of events in the case you mention is different: The insertion into the array would still happen while the domain isn't marked dying.

Quoted 2 lines

And after all the call of clear_global_virq_handlers() will now reset the handling domain to the hardware domain in all cases.

Of course, but in the meantime an event may be sent to such a domain already marked dying. That likely isn't going to cause problems, but is unexpected with what description here says is being addressed.

Quoted 3 lines
Quoted 2 lines

Plus the way you do it the early success path remains; ideally that case would also fail for an already dying domain.

Quoted 8 lines
Quoted 1 line

Same again: clear_global_virq_handlers() will reset the handling domain.

Right.

In summary: As indicated, we may be okay with the remaining race, but then we also should be making clear that we've decided to leave it at that. Hence my earlier request: If we accept this, say (and briefly justify) this in a code comment.

Okay, would you be fine with:

Note that this check won't guarantee that a domain just going down can't be set as the handling domain of a virq, as the is_dying indicator might change just after testing it.
This isn't going to be a major problem, as clear_global_virq_handlers() is guaranteed to run afterwards and it will reset the handling domain for the virq to the hardware domain.

Juergen

#6 lore raw

Re: [PATCH v6 1/7] xen/events: fix race with set_global_virq_handler()

Quoted 65 lines · Juergen Gross
Quoted 55 lines · Jan Beulich
Quoted 32 lines · Juergen Gross
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -979,6 +979,7 @@ void send_global_virq(uint32_t virq)
   int set_global_virq_handler(struct domain *d, uint32_t virq)
   {
       struct domain *old;
+    int rc = 0;
 
       if (virq >= NR_VIRQS)
           return -EINVAL;
@@ -992,14 +993,23 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
           return -EINVAL;
 
Quoted 16 lines

xen/common/event_channel.c int set_global_virq_handler(struct domain *d, uint32_t virq) · line 1001

Quoted 9 lines
xen/common/event_channel.c +6 -2
       spin_lock(&global_virq_handlers_lock);
-    old = global_virq_handlers[virq];
-    global_virq_handlers[virq] = d;
+
+    if ( d->is_dying != DOMDYING_alive )
+    {
+        old = d;
+        rc = -EINVAL;
+    }

While I can see how this eliminates the zombie domain aspect, this doesn't fully eliminate the race. Doing so would require (also) using the domain's event lock. Assuming we're okay with the remaining race, imo a code comment would be needed to state this (including the fact that it's then unpredictable whether this operation might still succeed for a domain already having d->is_dying != DOMDYING_alive).

Quoted 7 lines

AFAIU you mean that it is still possible to set a domain to handle a virq when it is in the process of going down, especially if is_dying is set just after it has been tested to be DOMDYING_alive?

I don't see this being a problem, as the same would happen if the domain would go down just a millisecond later. This is something we will never be able to handle.

Right, but the sequence of events in the case you mention is different: The insertion into the array would still happen while the domain isn't marked dying.

Quoted 2 lines

And after all the call of clear_global_virq_handlers() will now reset the handling domain to the hardware domain in all cases.

Of course, but in the meantime an event may be sent to such a domain already marked dying. That likely isn't going to cause problems, but is unexpected with what description here says is being addressed.

Quoted 3 lines
Quoted 2 lines

Plus the way you do it the early success path remains; ideally that case would also fail for an already dying domain.

Quoted 8 lines
Quoted 1 line

Same again: clear_global_virq_handlers() will reset the handling domain.

Right.

In summary: As indicated, we may be okay with the remaining race, but then we also should be making clear that we've decided to leave it at that. Hence my earlier request: If we accept this, say (and briefly justify) this in a code comment.

Quoted 8 lines

Okay, would you be fine with:

Note that this check won't guarantee that a domain just going down can't be set as the handling domain of a virq, as the is_dying indicator might change just after testing it.
This isn't going to be a major problem, as clear_global_virq_handlers() is guaranteed to run afterwards and it will reset the handling domain for the virq to the hardware domain.

Reads okay, thanks.

Jan

#7 lore raw

PATCH v6 2/7 xen/events: don't allow binding a global virq from any domain

In reply to Juergen Gross

Today Xen will happily allow binding a global virq by a domain which isn't configured to receive it. This won't result in any bad actions, but the bind will appear to have succeeded with no event ever being received by that event channel.

Instead of allowing the bind, error out if the domain isn't set to handle that virq.

---
V6:
- new patch

---
 xen/common/event_channel.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
xen/common/event_channel.c +15 -5
index f2b64c48fb..62060dc66b 100644
@@ -120,6 +120,13 @@ static uint8_t get_xen_consumer(xen_event_channel_notification_t fn)
 /* Get the notification function for a given Xen-bound event channel. */
 #define xen_notification_fn(e) (xen_consumers[(e)->xen_consumer-1])
 
+static struct domain *global_virq_handlers[NR_VIRQS] __read_mostly;
+
+static struct domain *get_global_virq_handler(unsigned int virq)
+{
+    return global_virq_handlers[virq] ?: hardware_domain;
+}
+
 static bool virq_is_global(unsigned int virq)
 {
     switch ( virq )
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
     */
     virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;
+        if ( vcpu != 0 )
+            return -EINVAL;
+    }
 
     if ( (v = domain_vcpu(d, vcpu)) == NULL )
         return -ENOENT;
@@ -965,15 +977,13 @@ void send_guest_pirq(struct domain *d, const struct pirq *pirq)
     }
 }
 
-static struct domain *global_virq_handlers[NR_VIRQS] __read_mostly;
-
 static DEFINE_SPINLOCK(global_virq_handlers_lock);
 
 void send_global_virq(uint32_t virq)
 {
     ASSERT(virq_is_global(virq));
 
-    send_guest_global_virq(global_virq_handlers[virq] ?: hardware_domain, virq);
+    send_guest_global_virq(get_global_virq_handler(virq), virq);
 }
 
 int set_global_virq_handler(struct domain *d, uint32_t virq)
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v6 2/7] xen/events: don't allow binding a global virq from any domain

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_event_channel_notification_t fn) · line 123

Quoted 7 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -120,6 +120,13 @@ static uint8_t get_xen_consumer(xen_event_channel_notification_t fn)
 /* Get the notification function for a given Xen-bound event channel. */
 #define xen_notification_fn(e) (xen_consumers[(e)->xen_consumer-1])
 
+static struct domain *global_virq_handlers[NR_VIRQS] __read_mostly;

Nit: While you move this line around, it would be nice if the attribute could then also move to its canonical place (between type and identifier).

Quoted 9 lines
xen/common/event_channel.c +5 -0
+static struct domain *get_global_virq_handler(unsigned int virq)
+{
+    return global_virq_handlers[virq] ?: hardware_domain;
+}
+
 static bool virq_is_global(unsigned int virq)
 {
     switch ( virq )

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port) · line 492

Quoted 9 lines
xen/common/event_channel.c +4 -2
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
     */
     virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;

Hmm. While this eliminates the problem for the common, race free case, the handler changing right after the check would still mean the bind would succeed.

Plus this way you're breaking a case that afaict has been working so
far: The bind happening before the setting of the handler. With a lot of unrelated if-s and when-s this could e.g. be of interest when considering a re-startable Xenstore domain. The one to take over could start first, obtain state from the original one while that's still active, and be nominated the handler of the global vIRQ only in the last moment.

Jan

#9 lore raw

Re: [PATCH v6 2/7] xen/events: don't allow binding a global virq from any domain

Quoted 24 lines · Jan Beulich

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_event_channel_notification_t fn) · line 123

Quoted 7 lines · Juergen Gross
xen/common/event_channel.c +1 -0
@@ -120,6 +120,13 @@ static uint8_t get_xen_consumer(xen_event_channel_notification_t fn)
  /* Get the notification function for a given Xen-bound event channel. */
  #define xen_notification_fn(e) (xen_consumers[(e)->xen_consumer-1])
 
+static struct domain *global_virq_handlers[NR_VIRQS] __read_mostly;

Nit: While you move this line around, it would be nice if the attribute could then also move to its canonical place (between type and identifier).

Quoted 12 lines
xen/common/event_channel.c +5 -0
+static struct domain *get_global_virq_handler(unsigned int virq)
+{
+    return global_virq_handlers[virq] ?: hardware_domain;
+}
+
  static bool virq_is_global(unsigned int virq)
  {
      switch ( virq )
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
      */
      virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
Quoted 10 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port) · line 492

Quoted 6 lines
xen/common/event_channel.c +4 -2
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;

Hmm. While this eliminates the problem for the common, race free case, the handler changing right after the check would still mean the bind would succeed.

Are you fine with me adding a paragraph to the commit message saying that a future patch will handle this case?

This future patch is patch 4 of the series, which will need to be modified to check the handling domain inside the event_lock.

Quoted 7 lines

Plus this way you're breaking a case that afaict has been working so
far: The bind happening before the setting of the handler. With a lot of unrelated if-s and when-s this could e.g. be of interest when considering a re-startable Xenstore domain. The one to take over could start first, obtain state from the original one while that's still active, and be nominated the handler of the global vIRQ only in the last moment.

This is a racy situation, too. If the old domain receives the virq after sending the state, this would need to be handled by transferring the virq information to the new domain, which can result in a never ending story.

This is the reason why the domain state bitmap is reset to contain all existing domains to be flagged as "changed", as otherwise a change might get lost.

I'd rather be able to handle today's use cases in a sane way than to try handling any weird future use cases which we don't know yet.

I think today's behavior is more or less insane and the new behavior is much easier to understand and more intuitive.

Juergen

#10 lore raw

Re: [PATCH v6 2/7] xen/events: don't allow binding a global virq from any domain

Quoted 17 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/common/event_channel.c +0 -0
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
      */
      virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
Quoted 10 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port) · line 492

Quoted 6 lines
xen/common/event_channel.c +4 -2
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;

Hmm. While this eliminates the problem for the common, race free case, the handler changing right after the check would still mean the bind would succeed.

Quoted 5 lines

Are you fine with me adding a paragraph to the commit message saying that a future patch will handle this case?

This future patch is patch 4 of the series, which will need to be modified to check the handling domain inside the event_lock.

I think this would be okay, so long as patches 2...4 are then also all committed together.

Quoted 16 lines
Quoted 7 lines

Plus this way you're breaking a case that afaict has been working so
far: The bind happening before the setting of the handler. With a lot of unrelated if-s and when-s this could e.g. be of interest when considering a re-startable Xenstore domain. The one to take over could start first, obtain state from the original one while that's still active, and be nominated the handler of the global vIRQ only in the last moment.

This is a racy situation, too. If the old domain receives the virq after sending the state, this would need to be handled by transferring the virq information to the new domain, which can result in a never ending story.

This is the reason why the domain state bitmap is reset to contain all existing domains to be flagged as "changed", as otherwise a change might get lost.

Quoted 5 lines

I'd rather be able to handle today's use cases in a sane way than to try handling any weird future use cases which we don't know yet.

I think today's behavior is more or less insane and the new behavior is much easier to understand and more intuitive.

Hmm, I'd like to leave this then for input by other maintainers.

Jan

#11 lore raw

Re: [PATCH v6 2/7] xen/events: don't allow binding a global virq from any domain

Quoted 43 lines · Jan Beulich
Quoted 17 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/common/event_channel.c +0 -0
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
       */
       virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
Quoted 10 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port) · line 492

Quoted 6 lines
xen/common/event_channel.c +4 -2
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;

Hmm. While this eliminates the problem for the common, race free case, the handler changing right after the check would still mean the bind would succeed.

Quoted 5 lines

Are you fine with me adding a paragraph to the commit message saying that a future patch will handle this case?

This future patch is patch 4 of the series, which will need to be modified to check the handling domain inside the event_lock.

I think this would be okay, so long as patches 2...4 are then also all committed together.

Quoted 16 lines
Quoted 7 lines

Plus this way you're breaking a case that afaict has been working so
far: The bind happening before the setting of the handler. With a lot of unrelated if-s and when-s this could e.g. be of interest when considering a re-startable Xenstore domain. The one to take over could start first, obtain state from the original one while that's still active, and be nominated the handler of the global vIRQ only in the last moment.

This is a racy situation, too. If the old domain receives the virq after sending the state, this would need to be handled by transferring the virq information to the new domain, which can result in a never ending story.

This is the reason why the domain state bitmap is reset to contain all existing domains to be flagged as "changed", as otherwise a change might get lost.

Quoted 7 lines
Quoted 5 lines

I'd rather be able to handle today's use cases in a sane way than to try handling any weird future use cases which we don't know yet.

I think today's behavior is more or less insane and the new behavior is much easier to understand and more intuitive.

Hmm, I'd like to leave this then for input by other maintainers.

Just one additional remark to your re-startable xenstore domain scenario above:

It wouldn't be possible today to do the same with a xenstore daemon in e.g. dom0, as binding the virq another time from within the same domain would be rejected by the hypervisor. In the xenstore domain case you'd either need the old domain to ask dom0 to change the handler (so much about less communication needed), or you'd need to give the xenstore domain the right to do the handler change itself, requiring to use flask or to modify the dummy XSM rights of the xenstore domain.

Juergen

#12 lore raw

Re: [PATCH v6 2/7] xen/events: don't allow binding a global virq from any domain

Quoted 52 lines · Jürgen Groß
Quoted 43 lines · Jan Beulich
Quoted 17 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/common/event_channel.c +0 -0
@@ -479,8 +486,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
       */
       virq = array_index_nospec(virq, ARRAY_SIZE(v->virq_to_evtchn));
 
Quoted 10 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port) · line 492

Quoted 6 lines
xen/common/event_channel.c +4 -2
-    if ( virq_is_global(virq) && (vcpu != 0) )
-        return -EINVAL;
+    if ( virq_is_global(virq) )
+    {
+        if ( get_global_virq_handler(virq) != d )
+            return -EBUSY;

Hmm. While this eliminates the problem for the common, race free case, the handler changing right after the check would still mean the bind would succeed.

Quoted 5 lines

Are you fine with me adding a paragraph to the commit message saying that a future patch will handle this case?

This future patch is patch 4 of the series, which will need to be modified to check the handling domain inside the event_lock.

I think this would be okay, so long as patches 2...4 are then also all committed together.

Quoted 16 lines
Quoted 7 lines

Plus this way you're breaking a case that afaict has been working so
far: The bind happening before the setting of the handler. With a lot of unrelated if-s and when-s this could e.g. be of interest when considering a re-startable Xenstore domain. The one to take over could start first, obtain state from the original one while that's still active, and be nominated the handler of the global vIRQ only in the last moment.

This is a racy situation, too. If the old domain receives the virq after sending the state, this would need to be handled by transferring the virq information to the new domain, which can result in a never ending story.

This is the reason why the domain state bitmap is reset to contain all existing domains to be flagged as "changed", as otherwise a change might get lost.

Quoted 7 lines
Quoted 5 lines

I'd rather be able to handle today's use cases in a sane way than to try handling any weird future use cases which we don't know yet.

I think today's behavior is more or less insane and the new behavior is much easier to understand and more intuitive.

Hmm, I'd like to leave this then for input by other maintainers.

Quoted 8 lines

Just one additional remark to your re-startable xenstore domain scenario above:

It wouldn't be possible today to do the same with a xenstore daemon in e.g. dom0, as binding the virq another time from within the same domain would be rejected by the hypervisor. In the xenstore domain case you'd either need the old domain to ask dom0 to change the handler (so much about less communication needed),

Not quite. There needs to be an indication anyway of info transfer being complete. That'll be where Dom0 would then (also) put in place the new handler. The vIRQ first arriving in the new XS domain could then serve as an indication that it is now in charge of the system; I didn't check whether a courtesy one would be sent right away, or whether such sending might need adding. (Plus anyway - XS is only an example here.)

Jan

Quoted 6 lines

or you'd need to give the xenstore domain
the right to do the handler change itself, requiring to use flask or to modify the dummy XSM rights of the xenstore domain.

Juergen

#13 lore raw

PATCH v6 3/7 xen/events: allow setting of global virq handler only for unbound virqs

In reply to Juergen Gross

XEN_DOMCTL_set_virq_handler will happily steal a global virq from the current domain having bound it and assign it to another domain. The former domain will just never receive any further events for that virq without knowing what happened.

Change the behavior to allow XEN_DOMCTL_set_virq_handler only if the virq in question is not bound by the current domain allowed to use it.

Currently the only user of XEN_DOMCTL_set_virq_handler in the Xen code base is init-xenstore-domain, so changing the behavior like above will not cause any problems.

---
V6:
- new patch

---
 xen/common/event_channel.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
xen/common/event_channel.c +18 -2
index 62060dc66b..341221d420 100644
@@ -988,7 +988,8 @@ void send_global_virq(uint32_t virq)
 
 int set_global_virq_handler(struct domain *d, uint32_t virq)
 {
-    struct domain *old;
+    struct domain *old, *hdl;
+    const struct vcpu *v;
     int rc = 0;
 
     if (virq >= NR_VIRQS)
@@ -1012,7 +1013,22 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
     else
     {
         old = global_virq_handlers[virq];
-        global_virq_handlers[virq] = d;
+        hdl = get_global_virq_handler(virq);
+        if ( hdl != d )
+        {
+            read_lock(&hdl->event_lock);
+
+            v = hdl->vcpu[0];
+            if ( v && read_atomic(&v->virq_to_evtchn[virq]) )
+            {
+                rc = -EBUSY;
+                old = d;
+            }
+            else
+                global_virq_handlers[virq] = d;
+
+            read_unlock(&hdl->event_lock);
+        }
     }
     spin_unlock(&global_virq_handlers_lock);
 
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH v6 3/7] xen/events: allow setting of global virq handler only for unbound virqs

Quoted 4 lines · Juergen Gross

XEN_DOMCTL_set_virq_handler will happily steal a global virq from the current domain having bound it and assign it to another domain. The former domain will just never receive any further events for that virq without knowing what happened.

Yet - see my reply on patch 2 - it may actually have been intentional to be this way, in case the new domain is indeed the designated new handler. Otherwise such a transfer would require more coordination - the original owner would first need to unbind, then signal the completion thereof to the new owner.

Jan

#15 lore raw

Re: [PATCH v6 3/7] xen/events: allow setting of global virq handler only for unbound virqs

Quoted 11 lines · Jan Beulich
Quoted 4 lines · Juergen Gross

XEN_DOMCTL_set_virq_handler will happily steal a global virq from the current domain having bound it and assign it to another domain. The former domain will just never receive any further events for that virq without knowing what happened.

Yet - see my reply on patch 2 - it may actually have been intentional to be this way, in case the new domain is indeed the designated new handler. Otherwise such a transfer would require more coordination - the original owner would first need to unbind, then signal the completion thereof to the new owner.

Yes, like today's handling of xenstored live update.

It is the natural way to do that, as that coordination needs to be happening anyway, as I've outlined in my response to your remark on patch 2.

Juergen

#16 lore raw

PATCH v6 4/7 xen: add bitmap to indicate per-domain state changes

In reply to Juergen Gross

Add a bitmap with one bit per possible domid indicating the respective domain has changed its state (created, deleted, dying, crashed, shutdown).

Registering the VIRQ_DOM_EXC event will result in setting the bits for all existing domains and resetting all other bits.

As the usage of this bitmap is tightly coupled with the VIRQ_DOM_EXC event, it is meant to be used only by a single consumer in the system, just like the VIRQ_DOM_EXC event.

Resetting a bit will be done in a future patch.

This information is needed for Xenstore to keep track of all domains.

---
V2:
- use DOMID_FIRST_RESERVED instead of DOMID_MASK + 1 (Jan Beulich)
- use const (Jan Beulich)
- move call of domain_reset_states() into evtchn_bind_virq() (Jan Beulich)
- dynamically allocate dom_state_changed bitmap (Jan Beulich) V3:
- use xvzalloc_array() (Jan Beulich)
- don't rename existing label (Jan Beulich)
V4:
- add __read_mostly (Jan Beulich)
- use __set_bit() (Jan Beulich)
- fix error handling in evtchn_bind_virq() (Jan Beulich)
V5:
- domain_init_states() may be called only if evtchn_bind_virq() has been called validly (Jan Beulich)
V6:
- guard dom_state_changed bitmap with d->event_lock (Jan Beulich)

---
 xen/common/domain.c        | 51 ++++++++++++++++++++++++++++++++++++++
 xen/common/event_channel.c | 31 +++++++++++++++++++++++
 xen/include/xen/event.h    |  4 +++
 xen/include/xen/sched.h    |  3 +++
 4 files changed, 89 insertions(+)
xen/common/domain.c +51 -0
index 0c4cc77111..78e2732e94 100644
@@ -35,6 +35,7 @@
 #include <xen/irq.h>
 #include <xen/argo.h>
 #include <xen/llc-coloring.h>
+#include <xen/xvmalloc.h>
 #include <asm/p2m.h>
 #include <asm/processor.h>
 #include <public/sched.h>
@@ -139,6 +140,51 @@ bool __read_mostly vmtrace_available;
 
 bool __read_mostly vpmu_is_available;
 
+static unsigned long *__read_mostly dom_state_changed;
+
+int domain_init_states(void)
+{
+    const struct domain *d;
+
+    ASSERT(!dom_state_changed);
+    ASSERT(rw_is_write_locked(&current->domain->event_lock));
+
+    dom_state_changed = xvzalloc_array(unsigned long,
+                                       BITS_TO_LONGS(DOMID_FIRST_RESERVED));
+    if ( !dom_state_changed )
+        return -ENOMEM;
+
+    rcu_read_lock(&domlist_read_lock);
+
+    for_each_domain ( d )
+        set_bit(d->domain_id, dom_state_changed);
+
+    rcu_read_unlock(&domlist_read_lock);
+
+    return 0;
+}
+
+void domain_deinit_states(const struct domain *d)
+{
+    ASSERT(rw_is_write_locked(&d->event_lock));
+
+    XVFREE(dom_state_changed);
+}
+
+static void domain_changed_state(const struct domain *d)
+{
+    struct domain *hdl;
+
+    hdl = lock_dom_exc_handler();
+    if ( unlikely(!hdl) )
+        return;
+
+    if ( dom_state_changed )
+        set_bit(d->domain_id, dom_state_changed);
+
+    unlock_dom_exc_handler(hdl);
+}
+
 static void __domain_finalise_shutdown(struct domain *d)
 {
     struct vcpu *v;
@@ -153,6 +199,7 @@ static void __domain_finalise_shutdown(struct domain *d)
             return;
 
     d->is_shut_down = 1;
+    domain_changed_state(d);
     if ( (d->shutdown_code == SHUTDOWN_suspend) && d->suspend_evtchn )
         evtchn_send(d, d->suspend_evtchn);
     else
@@ -840,6 +887,7 @@ struct domain *domain_create(domid_t domid,
      */
     domlist_insert(d);
 
+    domain_changed_state(d);
     memcpy(d->handle, config->handle, sizeof(d->handle));
 
     return d;
@@ -1105,6 +1153,7 @@ int domain_kill(struct domain *d)
         /* Mem event cleanup has to go here because the rings 
          * have to be put before we call put_domain. */
         vm_event_cleanup(d);
+        domain_changed_state(d);
         put_domain(d);
         send_global_virq(VIRQ_DOM_EXC);
         /* fallthrough */
@@ -1294,6 +1343,8 @@ static void cf_check complete_domain_destroy(struct rcu_head *head)
 
     xfree(d->vcpu);
 
+    domain_changed_state(d);
+
     _domain_destroy(d);
 
     send_global_virq(VIRQ_DOM_EXC);
xen/common/event_channel.c +31 -0
index 341221d420..c247efc4b1 100644
@@ -506,10 +506,18 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
         goto out;
     }
 
+    if ( virq == VIRQ_DOM_EXC )
+    {
+        rc = domain_init_states();
+        if ( rc )
+            goto out;
+    }
+
     port = rc = evtchn_get_port(d, port);
     if ( rc < 0 )
     {
         gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        domain_deinit_states(d);
         goto out;
     }
 
@@ -742,6 +750,9 @@ int evtchn_close(struct domain *d1, int port1, bool guest)
         struct vcpu *v;
         unsigned long flags;
 
+        if ( chn1->u.virq == VIRQ_DOM_EXC )
+            domain_deinit_states(d1);
+
         v = d1->vcpu[virq_is_global(chn1->u.virq) ? 0 : chn1->notify_vcpu_id];
 
         write_lock_irqsave(&v->virq_lock, flags);
@@ -1063,6 +1074,26 @@ static void clear_global_virq_handlers(struct domain *d)
     }
 }
 
+struct domain *lock_dom_exc_handler(void)
+{
+    struct domain *d;
+
+    d = get_global_virq_handler(VIRQ_DOM_EXC);
+    if ( unlikely(!get_domain(d)) )
+        return NULL;
+
+    read_lock(&d->event_lock);
+
+    return d;
+}
+
+void unlock_dom_exc_handler(struct domain *d)
+{
+    read_unlock(&d->event_lock);
+
+    put_domain(d);
+}
+
 int evtchn_status(evtchn_status_t *status)
 {
     struct domain   *d;
xen/include/xen/event.h +4 -0
index 48b79f3d62..5c0ba90c9f 100644
@@ -100,6 +100,10 @@ bool evtchn_virq_enabled(const struct vcpu *v, unsigned int virq);
 /* Notify remote end of a Xen-attached event channel.*/
 void notify_via_xen_event_channel(struct domain *ld, int lport);
 
+/* Lock/unlock of VIRQ_DOM_EXC associated data (read_lock(d->event_lock)). */
+struct domain *lock_dom_exc_handler(void);
+void unlock_dom_exc_handler(struct domain *d);
+
 /*
  * Internal event channel object storage.
  *
xen/include/xen/sched.h +3 -0
index 037c83fda2..9d9b89ec27 100644
@@ -805,6 +805,9 @@ void domain_resume(struct domain *d);
 
 int domain_soft_reset(struct domain *d, bool resuming);
 
+int domain_init_states(void);
+void domain_deinit_states(const struct domain *d);
+
 int vcpu_start_shutdown_deferral(struct vcpu *v);
 void vcpu_end_shutdown_deferral(struct vcpu *v);
 
Signature · 1 line
2.43.0
#17 lore raw

Re: [PATCH v6 4/7] xen: add bitmap to indicate per-domain state changes

Quoted 16 lines · Juergen Gross

Add a bitmap with one bit per possible domid indicating the respective domain has changed its state (created, deleted, dying, crashed, shutdown).

Registering the VIRQ_DOM_EXC event will result in setting the bits for all existing domains and resetting all other bits.

As the usage of this bitmap is tightly coupled with the VIRQ_DOM_EXC event, it is meant to be used only by a single consumer in the system, just like the VIRQ_DOM_EXC event.

Resetting a bit will be done in a future patch.

This information is needed for Xenstore to keep track of all domains.

Quoted 12 lines

---
V2:
- use DOMID_FIRST_RESERVED instead of DOMID_MASK + 1 (Jan Beulich)
- use const (Jan Beulich)
- move call of domain_reset_states() into evtchn_bind_virq() (Jan Beulich)
- dynamically allocate dom_state_changed bitmap (Jan Beulich) V3:
- use xvzalloc_array() (Jan Beulich)
- don't rename existing label (Jan Beulich)
V4:
- add __read_mostly (Jan Beulich)
- use __set_bit() (Jan Beulich)

This change looks to have been lost, ...

Quoted 6 lines

- fix error handling in evtchn_bind_virq() (Jan Beulich)
V5:
- domain_init_states() may be called only if evtchn_bind_virq() has been called validly (Jan Beulich)
V6:
- guard dom_state_changed bitmap with d->event_lock (Jan Beulich)

... without it being mentioned anywhere, and without it becoming clear why it would have needed undoing.

Quoted 12 lines
xen/common/domain.c +1 -0
@@ -35,6 +35,7 @@
 #include <xen/irq.h>
 #include <xen/argo.h>
 #include <xen/llc-coloring.h>
+#include <xen/xvmalloc.h>
 #include <asm/p2m.h>
 #include <asm/processor.h>
 #include <public/sched.h>
@@ -139,6 +140,51 @@ bool __read_mostly vmtrace_available;
 

xen/common/domain.c bool __read_mostly vmtrace_available; · line 150

Quoted 10 lines
xen/common/domain.c +8 -0
 bool __read_mostly vpmu_is_available;
 
+static unsigned long *__read_mostly dom_state_changed;
+
+int domain_init_states(void)
+{
+    const struct domain *d;
+
+    ASSERT(!dom_state_changed);
+    ASSERT(rw_is_write_locked(&current->domain->event_lock));

rw_is_write_locked_by_me()?

Quoted 8 lines
xen/common/domain.c +8 -0
+    dom_state_changed = xvzalloc_array(unsigned long,
+                                       BITS_TO_LONGS(DOMID_FIRST_RESERVED));
+    if ( !dom_state_changed )
+        return -ENOMEM;
+
+    rcu_read_lock(&domlist_read_lock);
+
+    for_each_domain ( d )

xen/common/domain.c bool __read_mostly vmtrace_available; · line 169

Quoted 10 lines
xen/common/domain.c +10 -0
+        set_bit(d->domain_id, dom_state_changed);
+
+    rcu_read_unlock(&domlist_read_lock);
+
+    return 0;
+}
+
+void domain_deinit_states(const struct domain *d)
+{
+    ASSERT(rw_is_write_locked(&d->event_lock));

Again.

Jan

#18 lore raw

Re: [PATCH v6 4/7] xen: add bitmap to indicate per-domain state changes

Quoted 39 lines · Jan Beulich
Quoted 16 lines · Juergen Gross

Add a bitmap with one bit per possible domid indicating the respective domain has changed its state (created, deleted, dying, crashed, shutdown).

Registering the VIRQ_DOM_EXC event will result in setting the bits for all existing domains and resetting all other bits.

As the usage of this bitmap is tightly coupled with the VIRQ_DOM_EXC event, it is meant to be used only by a single consumer in the system, just like the VIRQ_DOM_EXC event.

Resetting a bit will be done in a future patch.

This information is needed for Xenstore to keep track of all domains.

Quoted 12 lines

---
V2:
- use DOMID_FIRST_RESERVED instead of DOMID_MASK + 1 (Jan Beulich)
- use const (Jan Beulich)
- move call of domain_reset_states() into evtchn_bind_virq() (Jan Beulich)
- dynamically allocate dom_state_changed bitmap (Jan Beulich) V3:
- use xvzalloc_array() (Jan Beulich)
- don't rename existing label (Jan Beulich)
V4:
- add __read_mostly (Jan Beulich)
- use __set_bit() (Jan Beulich)

This change looks to have been lost, ...

Quoted 6 lines

- fix error handling in evtchn_bind_virq() (Jan Beulich)
V5:
- domain_init_states() may be called only if evtchn_bind_virq() has been called validly (Jan Beulich)
V6:
- guard dom_state_changed bitmap with d->event_lock (Jan Beulich)

Quoted 2 lines

... without it being mentioned anywhere, and without it becoming clear why it would have needed undoing.

Oh, thanks for spotting. I changed that by accident and missed to undo this change.

Quoted 15 lines
Quoted 15 lines
xen/common/domain.c +1 -0
@@ -35,6 +35,7 @@
  #include <xen/irq.h>
  #include <xen/argo.h>
  #include <xen/llc-coloring.h>
+#include <xen/xvmalloc.h>
  #include <asm/p2m.h>
  #include <asm/processor.h>
  #include <public/sched.h>
@@ -139,6 +140,51 @@ bool __read_mostly vmtrace_available;
 
  bool __read_mostly vpmu_is_available;
 
Quoted 10 lines

xen/common/domain.c bool __read_mostly vmtrace_available; · line 150

Quoted 8 lines
xen/common/domain.c +8 -0
+static unsigned long *__read_mostly dom_state_changed;
+
+int domain_init_states(void)
+{
+    const struct domain *d;
+
+    ASSERT(!dom_state_changed);
+    ASSERT(rw_is_write_locked(&current->domain->event_lock));

rw_is_write_locked_by_me()?

Yes, probably better.

Quoted 13 lines
Quoted 13 lines
xen/common/domain.c +12 -0
+    dom_state_changed = xvzalloc_array(unsigned long,
+                                       BITS_TO_LONGS(DOMID_FIRST_RESERVED));
+    if ( !dom_state_changed )
+        return -ENOMEM;
+
+    rcu_read_lock(&domlist_read_lock);
+
+    for_each_domain ( d )
+        set_bit(d->domain_id, dom_state_changed);
+
+    rcu_read_unlock(&domlist_read_lock);
+
Quoted 8 lines

xen/common/domain.c bool __read_mostly vmtrace_available; · line 169

Quoted 6 lines
xen/common/domain.c +6 -0
+    return 0;
+}
+
+void domain_deinit_states(const struct domain *d)
+{
+    ASSERT(rw_is_write_locked(&d->event_lock));

Again.

Yes.

Juergen

#19 lore raw

PATCH v6 5/7 xen: add new domctl get_changed_domain

In reply to Juergen Gross

Add a new domctl sub-function to get data of a domain having changed state (this is needed by Xenstore).

The returned state just contains the domid, the domain unique id, and some flags (existing, shutdown, dying).

In order to enable Xenstore stubdom being built for multiple Xen versions, make this domctl stable. For stable domctls the interface_version is always 0.

---
V1:
- use a domctl subop for the new interface (Jan Beulich)
V2:
- fix XSM hooks (Daniel P. Smith)
- remove versioning of stable sub-ops (Jan Beulich)
- use domctl.domain for retuning domid of a changed domain (Jan Beulich)
- simplify locking in get_domain_state() (Jan Beulich)
- undo stray change in event_channel.c (Jan Beulich)
V3:
- have disjunct states "dying" and "dead" (Jan Beulich)
- check padding fields to be 0 (Jan Beulich)
- drop memset() (Jan Beulich)
V4:
- add locking in get_domain_state() (Jan Beulich)
- only allow querying domain having changed state by domain receiving VIRQ_DOM_EXC events (Jan Beulich)
V5:
- use memset() (Jan Beulich)

---
 tools/flask/policy/modules/dom0.te     |  2 +-
 tools/flask/policy/modules/xen.if      |  4 +-
 tools/flask/policy/modules/xenstore.te |  1 +
 xen/common/domain.c                    | 70 ++++++++++++++++++++++++++
 xen/common/domctl.c                    | 18 ++++++-
 xen/common/event_channel.c             |  9 +++-
 xen/include/public/domctl.h            | 26 ++++++++++
 xen/include/xen/event.h                |  7 +++
 xen/include/xen/sched.h                |  2 +
 xen/include/xsm/dummy.h                |  8 +++
 xen/include/xsm/xsm.h                  |  6 +++
 xen/xsm/dummy.c                        |  1 +
 xen/xsm/flask/hooks.c                  |  7 +++
 xen/xsm/flask/policy/access_vectors    |  2 +
 14 files changed, 158 insertions(+), 5 deletions(-)
tools/flask/policy/modules/dom0.te +1 -1
index 16b8c9646d..6043c01b12 100644
@@ -40,7 +40,7 @@ allow dom0_t dom0_t:domain {
 };
 allow dom0_t dom0_t:domain2 {
 	set_cpu_policy gettsc settsc setscheduler set_vnumainfo
-	get_vnumainfo psr_cmt_op psr_alloc get_cpu_policy
+	get_vnumainfo psr_cmt_op psr_alloc get_cpu_policy get_domain_state
 };
 allow dom0_t dom0_t:resource { add remove };
 
tools/flask/policy/modules/xen.if +2 -2
index 11c1562aa5..2e06f3ed94 100644
@@ -54,7 +54,7 @@ define(`create_domain_common', `
 	allow $1 $2:domain2 { set_cpu_policy settsc setscheduler setclaim
 			set_vnumainfo get_vnumainfo cacheflush
 			psr_cmt_op psr_alloc soft_reset
-			resource_map get_cpu_policy };
+			resource_map get_cpu_policy get_domain_state };
 	allow $1 $2:security check_context;
 	allow $1 $2:shadow enable;
 	allow $1 $2:mmu { map_read map_write adjust memorymap physmap pinpage mmuext_op updatemp };
@@ -94,7 +94,7 @@ define(`manage_domain', `
 			getaddrsize pause unpause trigger shutdown destroy
 			setaffinity setdomainmaxmem getscheduler resume
 			setpodtarget getpodtarget getpagingmempool setpagingmempool };
-    allow $1 $2:domain2 set_vnumainfo;
+    allow $1 $2:domain2 { set_vnumainfo get_domain_state };
 ')
 
 # migrate_domain_out(priv, target)
tools/flask/policy/modules/xenstore.te +1 -0
index 519566ab81..49de53ebe2 100644
@@ -13,6 +13,7 @@ allow dom0_t xenstore_t:domain set_virq_handler;
 allow xenstore_t xen_t:xen writeconsole;
 # Xenstore queries domaininfo on all domains
 allow xenstore_t domain_type:domain getdomaininfo;
+allow xenstore_t domain_type:domain2 get_domain_state;
 
 # As a shortcut, the following 3 rules are used instead of adding a domain_comms
 # rule between xenstore_t and every domain type that talks to xenstore
xen/common/domain.c +70 -0

76 lines not on this page · raw message

xen/common/domctl.c +17 -1
index 05abb581a0..b897ca8723 100644
@@ -279,6 +279,11 @@ static struct vnuma_info *vnuma_init(const struct xen_domctl_vnuma *uinfo,
     return ERR_PTR(ret);
 }
 
+static bool is_stable_domctl(uint32_t cmd)
+{
+    return cmd == XEN_DOMCTL_get_domain_state;
+}
+
 long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 {
     long ret = 0;
@@ -289,7 +294,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     if ( copy_from_guest(op, u_domctl, 1) )
         return -EFAULT;
 
-    if ( op->interface_version != XEN_DOMCTL_INTERFACE_VERSION )
+    if ( op->interface_version !=
+         (is_stable_domctl(op->cmd) ? 0 : XEN_DOMCTL_INTERFACE_VERSION) )
         return -EACCES;
 
     switch ( op->cmd )
@@ -310,6 +316,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
         fallthrough;
     case XEN_DOMCTL_test_assign_device:
     case XEN_DOMCTL_vm_event_op:
+    case XEN_DOMCTL_get_domain_state:
         if ( op->domain == DOMID_INVALID )
         {
             d = NULL;
@@ -876,6 +883,15 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
             ret = -EOPNOTSUPP;
         break;
 
+    case XEN_DOMCTL_get_domain_state:
+        ret = xsm_get_domain_state(XSM_XS_PRIV, d);
+        if ( ret )
+            break;
+
+        copyback = 1;
+        ret = get_domain_state(&op->u.get_domain_state, d, &op->domain);
+        break;
+
     default:
         ret = arch_do_domctl(op, d, u_domctl);
         break;
xen/common/event_channel.c +8 -1
index c247efc4b1..ed55f73648 100644
@@ -997,6 +997,13 @@ void send_global_virq(uint32_t virq)
     send_guest_global_virq(get_global_virq_handler(virq), virq);
 }
 
+bool domain_handles_global_virq(const struct domain *d, uint32_t virq)
+{
+    ASSERT(virq_is_global(virq));
+
+    return get_global_virq_handler(virq) == d;
+}
+
 int set_global_virq_handler(struct domain *d, uint32_t virq)
 {
     struct domain *old, *hdl;
@@ -1008,7 +1015,7 @@ int set_global_virq_handler(struct domain *d, uint32_t virq)
     if (!virq_is_global(virq))
         return -EINVAL;
 
-    if (global_virq_handlers[virq] == d)
+    if ( domain_handles_global_virq(d, virq) )
         return 0;
 
     if (unlikely(!get_domain(d)))
xen/include/public/domctl.h +26 -0
index e2d392d1e5..5b2063eed9 100644
@@ -28,6 +28,7 @@
  * Pure additions (e.g. new sub-commands) or compatible interface changes
  * (e.g. adding semantics to 0-checked input fields or data to zeroed output
  * fields) don't require a change of the version.
+ * Stable ops are NOT covered by XEN_DOMCTL_INTERFACE_VERSION!
  *
  * Last version bump: Xen 4.19
  */
@@ -1243,7 +1244,30 @@ struct xen_domctl_set_llc_colors {
     XEN_GUEST_HANDLE_64(uint32) llc_colors;
 };
 
+/*
+ * XEN_DOMCTL_get_domain_state (stable interface)
+ *
+ * Get state information of a domain.
+ *
+ * In case domain is DOMID_INVALID, return information about a domain having
+ * changed state and reset the state change indicator for that domain. This
+ * function is usable only by a domain having registered the VIRQ_DOM_EXC
+ * event (normally Xenstore).
+ * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
+ */
+struct xen_domctl_get_domain_state {
+    uint16_t state;
+#define XEN_DOMCTL_GETDOMSTATE_STATE_EXIST     0x0001  /* Domain is existing. */
+#define XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN  0x0002  /* Shutdown finished. */
+#define XEN_DOMCTL_GETDOMSTATE_STATE_DYING     0x0004  /* Domain dying. */
+#define XEN_DOMCTL_GETDOMSTATE_STATE_DEAD      0x0008  /* Domain dead. */
+    uint16_t pad0;           /* Must be 0 on input, returned as 0. */
+    uint32_t pad1;           /* Must be 0 on input, returned as 0. */
+    uint64_t unique_id;      /* Unique domain identifier. */
+};
+
 struct xen_domctl {
+/* Stable domctl ops: interface_version is required to be 0.  */
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
 #define XEN_DOMCTL_destroydomain                  2
@@ -1333,6 +1357,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_dt_overlay                    87
 #define XEN_DOMCTL_gsi_permission                88
 #define XEN_DOMCTL_set_llc_colors                89
+#define XEN_DOMCTL_get_domain_state              90 /* stable interface */
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1400,6 +1425,7 @@ struct xen_domctl {
         struct xen_domctl_dt_overlay        dt_overlay;
 #endif
         struct xen_domctl_set_llc_colors    set_llc_colors;
+        struct xen_domctl_get_domain_state  get_domain_state;
         uint8_t                             pad[128];
     } u;
 };
xen/include/xen/event.h +7 -0
index 5c0ba90c9f..ae02012cc5 100644
@@ -36,6 +36,13 @@ void send_global_virq(uint32_t virq);
  */
 void send_guest_global_virq(struct domain *d, uint32_t virq);
 
+/*
+ * domain_handles_global_virq:
+ *  @d:        Suspected target domain for this VIRQ
+ *  @virq:     Virtual IRQ number (VIRQ_*), must be global
+ */
+bool domain_handles_global_virq(const struct domain *d, uint32_t virq);
+
 /*
  * sent_global_virq_handler: Set a global VIRQ handler.
  *  @d:        New target domain for this VIRQ
xen/include/xen/sched.h +2 -0
index 9d9b89ec27..ea63ca1c79 100644
@@ -807,6 +807,8 @@ int domain_soft_reset(struct domain *d, bool resuming);
 
 int domain_init_states(void);
 void domain_deinit_states(const struct domain *d);
+int get_domain_state(struct xen_domctl_get_domain_state *info,
+                     struct domain *d, domid_t *domid);
 
 int vcpu_start_shutdown_deferral(struct vcpu *v);
 void vcpu_end_shutdown_deferral(struct vcpu *v);
xen/include/xsm/dummy.h +8 -0
index 6a2fc33c3b..a8d06de6b0 100644
@@ -173,6 +173,7 @@ static XSM_INLINE int cf_check xsm_domctl(
     case XEN_DOMCTL_unbind_pt_irq:
         return xsm_default_action(XSM_DM_PRIV, current->domain, d);
     case XEN_DOMCTL_getdomaininfo:
+    case XEN_DOMCTL_get_domain_state:
         return xsm_default_action(XSM_XS_PRIV, current->domain, d);
     default:
         return xsm_default_action(XSM_PRIV, current->domain, d);
@@ -815,6 +816,13 @@ static XSM_INLINE int cf_check xsm_argo_send(
 
 #endif /* CONFIG_ARGO */
 
+static XSM_INLINE int cf_check xsm_get_domain_state(
+    XSM_DEFAULT_ARG struct domain *d)
+{
+    XSM_ASSERT_ACTION(XSM_XS_PRIV);
+    return xsm_default_action(action, current->domain, d);
+}
+
 #include <public/version.h>
 static XSM_INLINE int cf_check xsm_xen_version(XSM_DEFAULT_ARG uint32_t op)
 {
xen/include/xsm/xsm.h +6 -0
index 4dbff9d866..0689bf5c9f 100644
@@ -200,6 +200,7 @@ struct xsm_ops {
     int (*argo_register_any_source)(const struct domain *d);
     int (*argo_send)(const struct domain *d, const struct domain *t);
 #endif
+    int (*get_domain_state)(struct domain *d);
 };
 
 #ifdef CONFIG_XSM
@@ -774,6 +775,11 @@ static inline int xsm_argo_send(const struct domain *d, const struct domain *t)
 
 #endif /* CONFIG_ARGO */
 
+static inline int xsm_get_domain_state(struct domain *d)
+{
+    return alternative_call(xsm_ops.get_domain_state, d);
+}
+
 #endif /* XSM_NO_WRAPPERS */
 
 #ifdef CONFIG_MULTIBOOT
xen/xsm/dummy.c +1 -0
index e6ffa948f7..ce6fbdc6c5 100644
@@ -148,6 +148,7 @@ static const struct xsm_ops __initconst_cf_clobber dummy_ops = {
     .argo_register_any_source      = xsm_argo_register_any_source,
     .argo_send                     = xsm_argo_send,
 #endif
+    .get_domain_state              = xsm_get_domain_state,
 };
 
 void __init xsm_fixup_ops(struct xsm_ops *ops)
xen/xsm/flask/hooks.c +7 -0
index 2b4efde689..f126e50b92 100644
@@ -688,6 +688,7 @@ static int cf_check flask_domctl(struct domain *d, unsigned int cmd,
     case XEN_DOMCTL_memory_mapping:
     case XEN_DOMCTL_set_target:
     case XEN_DOMCTL_vm_event_op:
+    case XEN_DOMCTL_get_domain_state:
 
     /* These have individual XSM hooks (arch/../domctl.c) */
     case XEN_DOMCTL_bind_pt_irq:
@@ -1860,6 +1861,11 @@ static int cf_check flask_argo_send(
 
 #endif
 
+static int cf_check flask_get_domain_state(struct domain *d)
+{
+    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__GET_DOMAIN_STATE);
+}
+
 static const struct xsm_ops __initconst_cf_clobber flask_ops = {
     .set_system_active = flask_set_system_active,
     .security_domaininfo = flask_security_domaininfo,
@@ -1996,6 +2002,7 @@ static const struct xsm_ops __initconst_cf_clobber flask_ops = {
     .argo_register_any_source = flask_argo_register_any_source,
     .argo_send = flask_argo_send,
 #endif
+    .get_domain_state = flask_get_domain_state,
 };
 
 const struct xsm_ops *__init flask_init(
xen/xsm/flask/policy/access_vectors +2 -0
index a35e3d4c51..c9a8eeda4e 100644
@@ -251,6 +251,8 @@ class domain2
     resource_map
 # XEN_DOMCTL_get_cpu_policy
     get_cpu_policy
+# XEN_DOMCTL_get_domain_state
+    get_domain_state
 }
 
 # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 1 line
2.43.0
#20 lore raw

Re: [PATCH v6 5/7] xen: add new domctl get_changed_domain

Quoted 34 lines · Juergen Gross
xen/common/domain.c +28 -0
@@ -185,6 +185,76 @@ static void domain_changed_state(const struct domain *d)
     unlock_dom_exc_handler(hdl);
 }
 
+static void set_domain_state_info(struct xen_domctl_get_domain_state *info,
+                                  const struct domain *d)
+{
+    info->state = XEN_DOMCTL_GETDOMSTATE_STATE_EXIST;
+    if ( d->is_shut_down )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN;
+    if ( d->is_dying == DOMDYING_dying )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DYING;
+    if ( d->is_dying == DOMDYING_dead )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DEAD;
+    info->unique_id = d->unique_id;
+}
+
+int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
+                     domid_t *domid)
+{
+    unsigned int dom;
+    int rc = -ENOENT;
+    struct domain *hdl;
+
+    if ( info->pad0 || info->pad1 )
+        return -EINVAL;
+
+    if ( d )
+    {
+        set_domain_state_info(info, d);
+
+        return 0;

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 225

Quoted 10 lines
xen/common/domain.c +10 -0
+    }
+
+    /*
+     * Only domain registered for VIRQ_DOM_EXC event is allowed to query
+     * domains having changed state.
+     */
+    if ( !domain_handles_global_virq(current->domain, VIRQ_DOM_EXC) )
+        return -EACCES;
+
+    hdl = lock_dom_exc_handler();

Instead of leaving a small window for races between the if() and this function call, can't you simply compare hdl against current->domain?

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 232

Quoted 6 lines
xen/common/domain.c +6 -0
+    while ( dom_state_changed )
+    {
+        dom = find_first_bit(dom_state_changed, DOMID_MASK + 1);
+        if ( dom >= DOMID_FIRST_RESERVED )
+            break;
+        if ( test_and_clear_bit(dom, dom_state_changed) )

As this is now running under lock, does it really need to be test-and-clear? What mechanism would allow the flag to be cleared between the find-1st and here? Plus, like for patch 4, I think it could be __clear_bit() here.

Jan

#21 lore raw

Re: [PATCH v6 5/7] xen: add new domctl get_changed_domain

Quoted 40 lines · Jan Beulich
Quoted 39 lines · Juergen Gross
xen/common/domain.c +33 -0
@@ -185,6 +185,76 @@ static void domain_changed_state(const struct domain *d)
      unlock_dom_exc_handler(hdl);
  }
 
+static void set_domain_state_info(struct xen_domctl_get_domain_state *info,
+                                  const struct domain *d)
+{
+    info->state = XEN_DOMCTL_GETDOMSTATE_STATE_EXIST;
+    if ( d->is_shut_down )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN;
+    if ( d->is_dying == DOMDYING_dying )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DYING;
+    if ( d->is_dying == DOMDYING_dead )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DEAD;
+    info->unique_id = d->unique_id;
+}
+
+int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
+                     domid_t *domid)
+{
+    unsigned int dom;
+    int rc = -ENOENT;
+    struct domain *hdl;
+
+    if ( info->pad0 || info->pad1 )
+        return -EINVAL;
+
+    if ( d )
+    {
+        set_domain_state_info(info, d);
+
+        return 0;
+    }
+
+    /*
+     * Only domain registered for VIRQ_DOM_EXC event is allowed to query
+     * domains having changed state.
Quoted 8 lines

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 225

Quoted 5 lines
xen/common/domain.c +5 -0
+     */
+    if ( !domain_handles_global_virq(current->domain, VIRQ_DOM_EXC) )
+        return -EACCES;
+
+    hdl = lock_dom_exc_handler();

Instead of leaving a small window for races between the if() and this function call, can't you simply compare hdl against current->domain?

Good idea.

Quoted 11 lines

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 232

Quoted 7 lines
xen/common/domain.c +6 -0
+    while ( dom_state_changed )
+    {
+        dom = find_first_bit(dom_state_changed, DOMID_MASK + 1);
+        if ( dom >= DOMID_FIRST_RESERVED )
+            break;
+        if ( test_and_clear_bit(dom, dom_state_changed) )

As this is now running under lock, does it really need to be test-and-clear? What mechanism would allow the flag to be cleared between the find-1st and here? Plus, like for patch 4, I think it could be __clear_bit() here.

It is only under read_lock(), so there are concurrent calls possible. I don't think we want to use write_lock() here, do we?

Juergen

#22 lore raw

Re: [PATCH v6 5/7] xen: add new domctl get_changed_domain

Quoted 60 lines · Jürgen Groß
Quoted 40 lines · Jan Beulich
Quoted 39 lines · Juergen Gross
xen/common/domain.c +33 -0
@@ -185,6 +185,76 @@ static void domain_changed_state(const struct domain *d)
      unlock_dom_exc_handler(hdl);
  }
 
+static void set_domain_state_info(struct xen_domctl_get_domain_state *info,
+                                  const struct domain *d)
+{
+    info->state = XEN_DOMCTL_GETDOMSTATE_STATE_EXIST;
+    if ( d->is_shut_down )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN;
+    if ( d->is_dying == DOMDYING_dying )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DYING;
+    if ( d->is_dying == DOMDYING_dead )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DEAD;
+    info->unique_id = d->unique_id;
+}
+
+int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
+                     domid_t *domid)
+{
+    unsigned int dom;
+    int rc = -ENOENT;
+    struct domain *hdl;
+
+    if ( info->pad0 || info->pad1 )
+        return -EINVAL;
+
+    if ( d )
+    {
+        set_domain_state_info(info, d);
+
+        return 0;
+    }
+
+    /*
+     * Only domain registered for VIRQ_DOM_EXC event is allowed to query
+     * domains having changed state.
Quoted 8 lines

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 225

Quoted 5 lines
xen/common/domain.c +5 -0
+     */
+    if ( !domain_handles_global_virq(current->domain, VIRQ_DOM_EXC) )
+        return -EACCES;
+
+    hdl = lock_dom_exc_handler();

Instead of leaving a small window for races between the if() and this function call, can't you simply compare hdl against current->domain?

Good idea.

Quoted 8 lines
Quoted 7 lines
xen/common/domain.c +6 -0
+    while ( dom_state_changed )
+    {
+        dom = find_first_bit(dom_state_changed, DOMID_MASK + 1);
+        if ( dom >= DOMID_FIRST_RESERVED )
+            break;
+        if ( test_and_clear_bit(dom, dom_state_changed) )
Quoted 6 lines
Quoted 3 lines

As this is now running under lock, does it really need to be test-and-clear? What mechanism would allow the flag to be cleared between the find-1st and here? Plus, like for patch 4, I think it could be __clear_bit() here.

It is only under read_lock(), so there are concurrent calls possible. I don't think we want to use write_lock() here, do we?

Probably not; I have to admit I didn't even pay attention to this aspect. Then the set_bit() in domain_changed_state() also need to remain as is (in patch 4 I think it was).

Jan

#23 lore raw

Re: [PATCH v6 5/7] xen: add new domctl get_changed_domain

Quoted 65 lines · Jan Beulich
Quoted 64 lines · Jürgen Groß
Quoted 40 lines · Jan Beulich
Quoted 39 lines · Juergen Gross
xen/common/domain.c +33 -0
@@ -185,6 +185,76 @@ static void domain_changed_state(const struct domain *d)
       unlock_dom_exc_handler(hdl);
   }
 
+static void set_domain_state_info(struct xen_domctl_get_domain_state *info,
+                                  const struct domain *d)
+{
+    info->state = XEN_DOMCTL_GETDOMSTATE_STATE_EXIST;
+    if ( d->is_shut_down )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN;
+    if ( d->is_dying == DOMDYING_dying )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DYING;
+    if ( d->is_dying == DOMDYING_dead )
+        info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DEAD;
+    info->unique_id = d->unique_id;
+}
+
+int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
+                     domid_t *domid)
+{
+    unsigned int dom;
+    int rc = -ENOENT;
+    struct domain *hdl;
+
+    if ( info->pad0 || info->pad1 )
+        return -EINVAL;
+
+    if ( d )
+    {
+        set_domain_state_info(info, d);
+
+        return 0;
+    }
+
+    /*
+     * Only domain registered for VIRQ_DOM_EXC event is allowed to query
+     * domains having changed state.
Quoted 8 lines

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 225

Quoted 5 lines
xen/common/domain.c +5 -0
+     */
+    if ( !domain_handles_global_virq(current->domain, VIRQ_DOM_EXC) )
+        return -EACCES;
+
+    hdl = lock_dom_exc_handler();

Instead of leaving a small window for races between the if() and this function call, can't you simply compare hdl against current->domain?

Good idea.

Quoted 11 lines

xen/common/domain.c static void domain_changed_state(const struct domain *d) · line 232

Quoted 7 lines
xen/common/domain.c +6 -0
+    while ( dom_state_changed )
+    {
+        dom = find_first_bit(dom_state_changed, DOMID_MASK + 1);
+        if ( dom >= DOMID_FIRST_RESERVED )
+            break;
+        if ( test_and_clear_bit(dom, dom_state_changed) )

As this is now running under lock, does it really need to be test-and-clear? What mechanism would allow the flag to be cleared between the find-1st and here? Plus, like for patch 4, I think it could be __clear_bit() here.

Quoted 6 lines
Quoted 2 lines

It is only under read_lock(), so there are concurrent calls possible. I don't think we want to use write_lock() here, do we?

Probably not; I have to admit I didn't even pay attention to this aspect. Then the set_bit() in domain_changed_state() also need to remain as is (in patch 4 I think it was).

This one needs to stay, but the one in domain_init_states() can be changed to the non-atomic variant again.

Juergen

#24 lore raw

PATCH v6 6/7 tools/libs: add a new libxenmanage library

In reply to Juergen Gross

In order to have a stable interface in user land for using stable domctl and possibly later sysctl interfaces, add a new library libxenmanage.

---
V1:
- new patch
V2:
- define __XEN_TOOLS__ via Makefile (Anthony PERARD)
- use SPDX in header file (Anthony PERARD)
- change function name to xenmanage_poll_changed_domain() (Anthony PERARD)
- add short library description (Anthony PERARD)
- narrow scope of xen_domctl_get_domain_state pointer (Anthony PERARD) V4:
- use LGPL-2.1-only SPDX identifier (Anthony PERARD)

---
 tools/include/xenmanage.h          |  92 ++++++++++++++++
 tools/libs/Makefile                |   1 +
 tools/libs/manage/Makefile         |  10 ++
 tools/libs/manage/Makefile.common  |   3 +
 tools/libs/manage/core.c           | 168 +++++++++++++++++++++++++++++
 tools/libs/manage/libxenmanage.map |   8 ++
 tools/libs/uselibs.mk              |   2 +
 7 files changed, 284 insertions(+)
 create mode 100644 tools/include/xenmanage.h
 create mode 100644 tools/libs/manage/Makefile
 create mode 100644 tools/libs/manage/Makefile.common
 create mode 100644 tools/libs/manage/core.c
 create mode 100644 tools/libs/manage/libxenmanage.map
tools/include/xenmanage.h new file +92 -0
new file mode 100644
index 0000000000..956b7a0a44
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * Copyright (c) 2024 SUSE Software Solutions Germany GmbH
+ *
+ * Interfaces of libxenmanage.
+ *
+ * libxenmanage provides management functions for the host using stable
+ * hypercall interfaces.
+ */
+#ifndef XENMANAGE_H
+#define XENMANAGE_H
+
+#include <stdint.h>
+
+/* Avoid the need to #include <xentoollog.h> */
+struct xentoollog_logger;
+
+typedef struct xenmanage_handle xenmanage_handle;
+
+/*
+ * Open libxenmanage.
+ *
+ * Get a handle of the xenmanage library. The handle is required for all
+ * further operations of the library.
+ * Parameters:
+ *   logger:     Logging function to use. If NULL logging is done to stderr.
+ *   open_flags: Only 0 supported.
+ * Return value: Handle or NULL if error.
+ */
+xenmanage_handle *xenmanage_open(struct xentoollog_logger *logger,
+                                 unsigned int open_flags);
+
+/*
+ * Close libxenmanage.
+ *
+ * Return a handle of the xenmanage library.
+ * Parameters:
+ *    hdl: Handle obtained by xenmanage_open().
+ * Return value: always 0.
+ */
+int xenmanage_close(xenmanage_handle *hdl);
+
+#define XENMANAGE_GETDOMSTATE_STATE_EXIST     0x0001  /* Domain is existing. */
+#define XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN  0x0002  /* Shutdown finished. */
+#define XENMANAGE_GETDOMSTATE_STATE_DYING     0x0004  /* Domain dying. */
+#define XENMANAGE_GETDOMSTATE_STATE_DEAD      0x0008  /* Domain dead. */
+
+/*
+ * Return state information of an existing domain.
+ *
+ * Returns the domain state and unique id of the given domain.
+ * Parameters:
+ *   hdl:       handle returned by xenmanage_open()
+ *   domid:     domain id of the domain to get the information for
+ *   state:     where to store the state (XENMANAGE_GETDOMSTATE_STATE_ flags,
+ *              nothing stored if NULL)
+ *   unique_id: where to store the unique id of the domain (nothing stored if
+ *              NULL)
+ * Return value: 0 if information was stored, -1 else (errno is set)
+ */
+int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
+                              unsigned int *state, uint64_t *unique_id);
+
+/*
+ * Return information of a domain having changed state recently.
+ *
+ * Returns the domain id, state and unique id of a domain having changed
+ * state (any of the state bits was modified) since the last time information
+ * for that domain was returned by this function. Only usable by callers who
+ * have registered the VIRQ_DOM_EXC event (normally Xenstore).
+ * Parameters:
+ *   hdl:       handle returned by xenmanage_open()
+ *   domid:     where to store the domid of the domain (not NULL)
+ *   state:     where to store the state (XENMANAGE_GETDOMSTATE_STATE_ flags,
+ *              nothing stored if NULL)
+ *   unique_id: where to store the unique id of the domain (nothing stored if
+ *              NULL)
+ * Return value: 0 if information was stored, -1 else (errno is set)
+ */
+int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
+                                  unsigned int *state, uint64_t *unique_id);
+#endif /* XENMANAGE_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libs/Makefile +1 -0
index 1afcd12e2b..d39516c1b3 100644
@@ -12,6 +12,7 @@ SUBDIRS-y += devicemodel
 SUBDIRS-y += ctrl
 SUBDIRS-y += guest
 SUBDIRS-y += hypfs
+SUBDIRS-y += manage
 SUBDIRS-y += store
 SUBDIRS-y += stat
 SUBDIRS-$(CONFIG_Linux) += vchan
tools/libs/manage/Makefile new file +10 -0
new file mode 100644
index 0000000000..dbfe70d259
@@ -0,0 +1,10 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+version-script := libxenmanage.map
+
+include Makefile.common
+
+include $(XEN_ROOT)/tools/libs/libs.mk
tools/libs/manage/Makefile.common new file +3 -0
new file mode 100644
index 0000000000..533ba30fba
@@ -0,0 +1,3 @@
+CFLAGS += -D__XEN_TOOLS__
+
+OBJS-y  += core.o
tools/libs/manage/core.c new file +168 -0

168 lines not on this page · raw message

tools/libs/manage/libxenmanage.map new file +8 -0
new file mode 100644
index 0000000000..64c793e603
@@ -0,0 +1,8 @@
+VERS_1.0 {
+	global:
+		xenmanage_open;
+		xenmanage_close;
+		xenmanage_get_domain_info;
+		xenmanage_poll_changed_domain;
+	local: *; /* Do not expose anything by default */
+};
tools/libs/uselibs.mk +2 -0
index 7aa8d83e06..c0a234cfec 100644
@@ -16,6 +16,8 @@ LIBS_LIBS += devicemodel
 USELIBS_devicemodel := toollog toolcore call
 LIBS_LIBS += hypfs
 USELIBS_hypfs := toollog toolcore call
+LIBS_LIBS += manage
+USELIBS_manage := toollog toolcore call
 LIBS_LIBS += ctrl
 USELIBS_ctrl := toollog call evtchn gnttab foreignmemory devicemodel
 LIBS_LIBS += guest
Signature · 1 line
2.43.0
#25 lore raw

PATCH v6 7/7 tools/xenstored: use new stable interface instead of libxenctrl

In reply to Juergen Gross

Replace the current use of the unstable xc_domain_getinfo_single() interface with the stable domctl XEN_DOMCTL_get_domain_state call via the new libxenmanage library.

This will remove the last usage of libxenctrl by Xenstore, so update the library dependencies accordingly.

For now only do a direct replacement without using the functionality of obtaining information about domains having changed the state.

---
V1:
- use library instead of direct hypercall, only replace current libxenctrl use case

Please note that this patch can be committed only after the related Mini-OS patch "config: add support for libxenmanage" has gone in AND the Mini-OS commit-id has been updated in Config.mk accordingly!

---
 stubdom/Makefile                |  8 ++---
 stubdom/mini-os.mk              |  1 +
 tools/xenstored/Makefile        |  2 +-
 tools/xenstored/Makefile.common |  2 +-
 tools/xenstored/core.h          |  1 -
 tools/xenstored/domain.c        | 52 ++++++++++++---------------------
 tools/xenstored/lu.c            |  1 +
 tools/xenstored/lu_daemon.c     |  1 +
 8 files changed, 28 insertions(+), 40 deletions(-)
stubdom/Makefile +4 -4
index 2a81af28a1..ca800b243c 100644
@@ -307,7 +307,7 @@ endif
 # libraries under tools/libs
 #######
 
-STUB_LIBS := toolcore toollog evtchn gnttab call foreignmemory devicemodel ctrl guest
+STUB_LIBS := toolcore toollog evtchn gnttab call foreignmemory devicemodel ctrl guest manage
 
 LIBDEP_guest := cross-zlib
 
@@ -465,7 +465,7 @@ grub: cross-polarssl grub-upstream $(CROSS_ROOT) grub-$(XEN_TARGET_ARCH)-minios-
 # xenstore
 ##########
 
-xenstore-minios.gen.cfg: APP_LIBS = gnttab evtchn toollog ctrl
+xenstore-minios.gen.cfg: APP_LIBS = gnttab evtchn toollog manage
 xenstore-minios.gen.cfg: xenstore-minios.cfg Makefile
 	$(GEN_config) >$@
 
@@ -480,7 +480,7 @@ xenstore: $(CROSS_ROOT) xenstore-minios-config.mk
 # xenstorepvh
 #############
 
-xenstorepvh-minios.gen.cfg: APP_LIBS = gnttab evtchn toollog ctrl
+xenstorepvh-minios.gen.cfg: APP_LIBS = gnttab evtchn toollog manage
 xenstorepvh-minios.gen.cfg: xenstorepvh-minios.cfg Makefile
 	$(GEN_config) >$@
 
@@ -523,7 +523,7 @@ else
 pv-grub-if-enabled:
 endif
 
-XENSTORE_DEPS := libxenevtchn libxengnttab libxenctrl
+XENSTORE_DEPS := libxenevtchn libxengnttab libxenmanage
 
 .PHONY: xenstore-stubdom
 xenstore-stubdom: mini-os-$(XEN_TARGET_ARCH)-xenstore $(XENSTORE_DEPS) xenstore
stubdom/mini-os.mk +1 -0
index 7e4968e026..be32302f9e 100644
@@ -13,5 +13,6 @@ GNTTAB_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/gnttab
 CALL_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/call
 FOREIGNMEMORY_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/foreignmemory
 DEVICEMODEL_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/devicemodel
+MANAGE_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/manage
 CTRL_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/ctrl
 GUEST_PATH = $(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/guest
tools/xenstored/Makefile +1 -1
index 09adfe1d50..81c42838e0 100644
@@ -5,7 +5,7 @@ include Makefile.common
 
 xenstored: LDLIBS += $(LDLIBS_libxenevtchn)
 xenstored: LDLIBS += $(LDLIBS_libxengnttab)
-xenstored: LDLIBS += $(LDLIBS_libxenctrl)
+xenstored: LDLIBS += $(LDLIBS_libxenmanage)
 xenstored: LDLIBS += -lrt
 xenstored: LDLIBS += $(SOCKET_LIBS)
 
tools/xenstored/Makefile.common +1 -1
index 27fdb3b49e..271134fcc1 100644
@@ -12,7 +12,7 @@ XENSTORED_OBJS-$(CONFIG_MiniOS) += minios.o lu_minios.o
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
 CFLAGS += $(CFLAGS_libxenevtchn)
-CFLAGS += $(CFLAGS_libxenctrl)
+CFLAGS += $(CFLAGS_libxenmanage)
 CFLAGS += $(CFLAGS_libxentoolcore)
 
 $(XENSTORED_OBJS-y): CFLAGS += $(CFLAGS_libxengnttab)
tools/xenstored/core.h +0 -1
index e58779e88c..632886cecf 100644
@@ -19,7 +19,6 @@
 #ifndef _XENSTORED_CORE_H
 #define _XENSTORED_CORE_H
 
-#include <xenctrl.h>
 #include <xengnttab.h>
 
 #include <sys/types.h>
tools/xenstored/domain.c +19 -33

121 lines not on this page · raw message

tools/xenstored/lu.c +1 -0
index bec2a84e10..4fccbbc195 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <syslog.h>
 #include <time.h>
+#include <unistd.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 
tools/xenstored/lu_daemon.c +1 -0
index 6df6c80a2a..88d8d9e1b3 100644
@@ -6,6 +6,7 @@
  */
 
 #include <syslog.h>
+#include <unistd.h>
 #include <sys/stat.h>
 
 #include "talloc.h"
Signature · 1 line
2.43.0