Skip to contentSkip to conversation

xen/domain: introduce DOMID_ANY

PATCH Superseded [email protected] 3 participants 6 messages Apr 8 – May 7

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 xen/domain: introduce DOMID_ANY

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

---
Changes since v6:
- fixed libxl_domid_valid_guest() check in libxl_create.c
- Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2438790748

---
 tools/libs/light/libxl_create.c         |  4 +++-
 tools/tests/domid/harness.h             |  1 +
 tools/tests/domid/test-domid.c          | 12 ++++++------
 xen/common/device-tree/dom0less-build.c |  2 +-
 xen/common/domctl.c                     |  3 +--
 xen/common/domid.c                      |  5 ++++-
 xen/include/public/xen.h                |  7 +++++++
 7 files changed, 23 insertions(+), 11 deletions(-)
tools/libs/light/libxl_create.c +3 -1
index bfc9149096a3..6fd62d140389 100644
@@ -681,8 +681,10 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,
                     continue;
 
                 local_domid = v;
+            } else if (libxl_domid_valid_guest(info->domid)) {
+                local_domid = info->domid;
             } else {
-                local_domid = info->domid; /* May not be valid */
+                local_domid = DOMID_ANY;
             }
 
             ret = xc_domain_create(ctx->xch, &local_domid, &create);
tools/tests/domid/harness.h +1 -0
index 17eb22a9a854..65da0d075a2b 100644
@@ -41,6 +41,7 @@ extern unsigned long find_next_zero_bit(const unsigned long *addr,
 
 #define DOMID_FIRST_RESERVED            (100)
 #define DOMID_INVALID                   (101)
+#define DOMID_ANY                       (102)
 
 #endif /* _TEST_HARNESS_ */
 
tools/tests/domid/test-domid.c +6 -6
index 5915c4699a5c..71cc4e7fd86d 100644
@@ -41,20 +41,20 @@ int main(int argc, char **argv)
         domid_free(expected);
 
     /*
-     * Test that that two consecutive calls of domid_alloc(DOMID_INVALID)
+     * Test that that two consecutive calls of domid_alloc(DOMID_ANY)
      * will never return the same ID.
      * NB: ID#0 is reserved and shall not be allocated by
-     * domid_alloc(DOMID_INVALID).
+     * domid_alloc(DOMID_ANY).
      */
     for ( expected = 1; expected < DOMID_FIRST_RESERVED; expected++ )
     {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
         verify(allocated == expected,
                "TEST 3: expected %u allocated %u\n", expected, allocated);
     }
     for ( expected = 1; expected < DOMID_FIRST_RESERVED; expected++ )
     {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
         verify(allocated == DOMID_INVALID,
                "TEST 4: expected %u allocated %u\n", DOMID_INVALID, allocated);
     }
@@ -64,7 +64,7 @@ int main(int argc, char **argv)
         domid_free(expected);
     for ( expected = 1; expected < DOMID_FIRST_RESERVED / 2; expected++ )
     {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
         verify(allocated == expected,
                "TEST 5: expected %u allocated %u\n", expected, allocated);
     }
@@ -72,7 +72,7 @@ int main(int argc, char **argv)
     /* Re-allocate last ID from [1..DOMID_FIRST_RESERVED - 1]. */
     expected = DOMID_FIRST_RESERVED - 1;
     domid_free(DOMID_FIRST_RESERVED - 1);
-    allocated = domid_alloc(DOMID_INVALID);
+    allocated = domid_alloc(DOMID_ANY);
     verify(allocated == expected,
            "TEST 6: expected %u allocated %u\n", expected, allocated);
 
xen/common/device-tree/dom0less-build.c +1 -1
index 840d14419da2..3c18dae5e625 100644
@@ -852,7 +852,7 @@ void __init create_domUs(void)
         if ( (max_init_domid + 1) >= DOMID_FIRST_RESERVED )
             panic("No more domain IDs available\n");
 
-        domid = domid_alloc(DOMID_INVALID);
+        domid = domid_alloc(DOMID_ANY);
         if ( domid == DOMID_INVALID )
             panic("Error allocating ID for domain %s\n", dt_node_name(node));
 
xen/common/domctl.c +1 -2
index 93738931c575..b969f5ada6cc 100644
@@ -409,8 +409,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
     case XEN_DOMCTL_createdomain:
     {
-        /* NB: ID#0 is reserved, find the first suitable ID instead. */
-        domid_t domid = domid_alloc(op->domain ?: DOMID_INVALID);
+        domid_t domid = domid_alloc(op->domain);
 
         if ( domid == DOMID_INVALID )
         {
xen/common/domid.c +4 -1
index 2387ddb08300..b0258e477c1a 100644
@@ -19,7 +19,7 @@ static DECLARE_BITMAP(domid_bitmap, DOMID_FIRST_RESERVED);
  * @param domid Domain ID hint:
  * - If an explicit domain ID is provided, verify its availability and use it
  *   if ID is not used;
- * - If DOMID_INVALID is provided, search [1..DOMID_FIRST_RESERVED-1] range,
+ * - If DOMID_ANY is provided, search [1..DOMID_FIRST_RESERVED-1] range,
  *   starting from the last used ID. Implementation guarantees that two
  *   consecutive calls will never return the same ID. ID#0 is reserved for
  *   the first boot domain (currently, dom0) and excluded from the allocation
@@ -31,6 +31,9 @@ domid_t domid_alloc(domid_t domid)
 {
     static domid_t domid_last;
 
+    if ( domid >= DOMID_FIRST_RESERVED && domid != DOMID_ANY )
+        return DOMID_INVALID;
+
     spin_lock(&domid_lock);
 
     /* Exact match. */
xen/include/public/xen.h +7 -0
index b12fd10e6315..f35a6f21f063 100644
@@ -608,6 +608,13 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t);
 /* DOMID_INVALID is used to identify pages with unknown owner. */
 #define DOMID_INVALID        xen_mk_uint(0x7FF4)
 
+/*
+ * DOMID_ANY is used to signal no specific domain ID requested.
+ * Handler should pick a valid ID, or handle it as a broadcast value
+ * depending on the context.
+ */
+#define DOMID_ANY            xen_mk_uint(0x7FF5)
+
 /* Idle domain. */
 #define DOMID_IDLE           xen_mk_uint(0x7FFF)
 
Signature · 1 line
2.53.0
Reviewed-by on v7 1/1
#1 lore raw
Quoted 7 lines · On Wed, Apr 08, 2026 at 04:40:46PM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Quoted 6 lines

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

And I believe stefano's review would still apply, right?

Thanks,

--
| Vates

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#2 lore raw
In reply to [email protected]
Quoted 11 lines · On Wed, Apr 08, 2026 at 04:40:46PM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Quoted 7 lines

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

---
Changes since v6:
- fixed libxl_domid_valid_guest() check in libxl_create.c
- Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2438790748

Unfortunattly, this patch breaks tests in tools/tests, see

    https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2471684142

The tests produces this:

Running /usr/lib/xen/tests/test-mem-claim
Memory claims tests
Free pages: 3059012, Oustanding claims: 0
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-mem-claim

Running /usr/lib/xen/tests/test-paging-mempool Paging mempool tests
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-paging-mempool

Running /usr/lib/xen/tests/test-resource
XENMEM_acquire_resource tests
Test x86 PV
Domain create failure: 17 - File exists
Test x86 PVH
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-resource

Running /usr/lib/xen/tests/test-tsx
TSX tests
Host: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0 Got 16 CPUs
Testing MSR_TSX_FORCE_ABORT consistency
Testing MSR_TSX_CTRL consistency
Testing MSR_MCU_OPT_CTRL consistency
Testing RTM behaviour
Got #UD
Testing PV default/max policies
Max: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Def: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Testing HVM default/max policies
Max: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Def: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Testing PV guest
Domain create failure: 17 - File exists
Testing HVM guest
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-tsx

At least, the domid test passes :-)
Running /usr/lib/xen/tests/test-domid
PASSED

--
| Vates

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#3 lore raw
Quoted 31 lines · Anthony PERARD
Quoted 11 lines · On Wed, Apr 08, 2026 at 04:40:46PM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Quoted 7 lines

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

---
Changes since v6:
- fixed libxl_domid_valid_guest() check in libxl_create.c
- Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2438790748

Unfortunattly, this patch breaks tests in tools/tests, see

     https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2471684142

The tests produces this:

Running /usr/lib/xen/tests/test-mem-claim
Memory claims tests
Free pages: 3059012, Oustanding claims: 0
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-mem-claim

Quoted 4 lines

Running /usr/lib/xen/tests/test-paging-mempool Paging mempool tests
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-paging-mempool

I've looked into this one. The input domid is 0 here, which will be rejected now.

I guess the other failing cases have the same issue.

I have one request: could the definition of DOMID_ANY be split out into a patch of its own? I have a security related patch series pending which would need that definition for another purpose (not an XSA, though).

Juergen

#4 lore raw
In reply to Anthony PERARD
Quoted 56 lines · Anthony PERARD
Quoted 11 lines · On Wed, Apr 08, 2026 at 04:40:46PM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Quoted 7 lines

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

---
Changes since v6:
- fixed libxl_domid_valid_guest() check in libxl_create.c
- Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2438790748

Unfortunattly, this patch breaks tests in tools/tests, see

    https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2471684142

The tests produces this:

Running /usr/lib/xen/tests/test-mem-claim
Memory claims tests
Free pages: 3059012, Oustanding claims: 0
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-mem-claim

Running /usr/lib/xen/tests/test-paging-mempool Paging mempool tests
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-paging-mempool

Running /usr/lib/xen/tests/test-resource
XENMEM_acquire_resource tests
Test x86 PV
Domain create failure: 17 - File exists
Test x86 PVH
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-resource

Running /usr/lib/xen/tests/test-tsx
TSX tests
Host: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0 Got 16 CPUs
Testing MSR_TSX_FORCE_ABORT consistency
Testing MSR_TSX_CTRL consistency
Testing MSR_MCU_OPT_CTRL consistency
Testing RTM behaviour
Got #UD
Testing PV default/max policies
Max: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Def: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0

Quoted 8 lines

Testing HVM default/max policies
Max: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Def: RTM 0, HLE 0, TSX_FORCE_ABORT 0, RTM_ALWAYS_ABORT 0, TSX_CTRL 0
Testing PV guest
Domain create failure: 17 - File exists
Testing HVM guest
Domain create failure: 17 - File exists
FAILED: /usr/lib/xen/tests/test-tsx

Yeah, I did not run those tests locally and the failing tests are not part of "basic" CI, certain gitlab privileges are required to execute the full CI test suite (as it turns out).

Perhaps tools tests should be part of a CI runs available to all users, at least on demand?

Sent v8 with a fixups for the tests:
https://lore.kernel.org/xen-devel/[email protected]/

(Thanks to Stefano with help on running a full CI cycle).

Quoted 4 lines

At least, the domid test passes :-)
Running /usr/lib/xen/tests/test-domid
PASSED

:-)

Quoted 8 lines

--
| Vates

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#5 lore raw
In reply to [email protected]
Quoted 144 lines · On 09.04.26 01:40, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add a new symbol DOMID_ANY to improve the readability of the code.

Update all relevant domid_alloc() call sites and harden the domid_alloc() input value check.

Also, fix problem with passing invalid domain IDs in
XEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.

Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")

---
Changes since v6:
- fixed libxl_domid_valid_guest() check in libxl_create.c
- Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2438790748

---
  tools/libs/light/libxl_create.c         |  4 +++-
  tools/tests/domid/harness.h             |  1 +
  tools/tests/domid/test-domid.c          | 12 ++++++------
  xen/common/device-tree/dom0less-build.c |  2 +-
  xen/common/domctl.c                     |  3 +--
  xen/common/domid.c                      |  5 ++++-
  xen/include/public/xen.h                |  7 +++++++
  7 files changed, 23 insertions(+), 11 deletions(-)
tools/libs/light/libxl_create.c +3 -1
index bfc9149096a3..6fd62d140389 100644
@@ -681,8 +681,10 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,
                      continue;
 
                  local_domid = v;
+            } else if (libxl_domid_valid_guest(info->domid)) {
+                local_domid = info->domid;
              } else {
-                local_domid = info->domid; /* May not be valid */
+                local_domid = DOMID_ANY;
              }
 
              ret = xc_domain_create(ctx->xch, &local_domid, &create);
tools/tests/domid/harness.h +1 -0
index 17eb22a9a854..65da0d075a2b 100644
@@ -41,6 +41,7 @@ extern unsigned long find_next_zero_bit(const unsigned long *addr,
 
  #define DOMID_FIRST_RESERVED            (100)
  #define DOMID_INVALID                   (101)
+#define DOMID_ANY                       (102)
 
  #endif /* _TEST_HARNESS_ */
 
tools/tests/domid/test-domid.c +6 -6
index 5915c4699a5c..71cc4e7fd86d 100644
@@ -41,20 +41,20 @@ int main(int argc, char **argv)
          domid_free(expected);
 
      /*
-     * Test that that two consecutive calls of domid_alloc(DOMID_INVALID)
+     * Test that that two consecutive calls of domid_alloc(DOMID_ANY)
       * will never return the same ID.
       * NB: ID#0 is reserved and shall not be allocated by
-     * domid_alloc(DOMID_INVALID).
+     * domid_alloc(DOMID_ANY).
       */
      for ( expected = 1; expected < DOMID_FIRST_RESERVED; expected++ )
      {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
          verify(allocated == expected,
                 "TEST 3: expected %u allocated %u\n", expected, allocated);
      }
      for ( expected = 1; expected < DOMID_FIRST_RESERVED; expected++ )
      {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
          verify(allocated == DOMID_INVALID,
                 "TEST 4: expected %u allocated %u\n", DOMID_INVALID, allocated);
      }
@@ -64,7 +64,7 @@ int main(int argc, char **argv)
          domid_free(expected);
      for ( expected = 1; expected < DOMID_FIRST_RESERVED / 2; expected++ )
      {
-        allocated = domid_alloc(DOMID_INVALID);
+        allocated = domid_alloc(DOMID_ANY);
          verify(allocated == expected,
                 "TEST 5: expected %u allocated %u\n", expected, allocated);
      }
@@ -72,7 +72,7 @@ int main(int argc, char **argv)
      /* Re-allocate last ID from [1..DOMID_FIRST_RESERVED - 1]. */
      expected = DOMID_FIRST_RESERVED - 1;
      domid_free(DOMID_FIRST_RESERVED - 1);
-    allocated = domid_alloc(DOMID_INVALID);
+    allocated = domid_alloc(DOMID_ANY);
      verify(allocated == expected,
             "TEST 6: expected %u allocated %u\n", expected, allocated);
 
xen/common/device-tree/dom0less-build.c +1 -1
index 840d14419da2..3c18dae5e625 100644
@@ -852,7 +852,7 @@ void __init create_domUs(void)
          if ( (max_init_domid + 1) >= DOMID_FIRST_RESERVED )
              panic("No more domain IDs available\n");
 
-        domid = domid_alloc(DOMID_INVALID);
+        domid = domid_alloc(DOMID_ANY);
          if ( domid == DOMID_INVALID )
              panic("Error allocating ID for domain %s\n", dt_node_name(node));
 
xen/common/domctl.c +1 -2
index 93738931c575..b969f5ada6cc 100644
@@ -409,8 +409,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
      case XEN_DOMCTL_createdomain:
      {
-        /* NB: ID#0 is reserved, find the first suitable ID instead. */
-        domid_t domid = domid_alloc(op->domain ?: DOMID_INVALID);
+        domid_t domid = domid_alloc(op->domain);
 
          if ( domid == DOMID_INVALID )
          {
xen/common/domid.c +1 -1
index 2387ddb08300..b0258e477c1a 100644
@@ -19,7 +19,7 @@ static DECLARE_BITMAP(domid_bitmap, DOMID_FIRST_RESERVED);
   * @param domid Domain ID hint:
   * - If an explicit domain ID is provided, verify its availability and use it
   *   if ID is not used;
- * - If DOMID_INVALID is provided, search [1..DOMID_FIRST_RESERVED-1] range,
+ * - If DOMID_ANY is provided, search [1..DOMID_FIRST_RESERVED-1] range,
   *   starting from the last used ID. Implementation guarantees that two
   *   consecutive calls will never return the same ID. ID#0 is reserved for
   *   the first boot domain (currently, dom0) and excluded from the allocation

xen/common/domid.c domid_t domid_alloc(domid_t domid) · line 36

Quoted 6 lines
xen/common/domid.c +3 -0
@@ -31,6 +31,9 @@ domid_t domid_alloc(domid_t domid)
  {
      static domid_t domid_last;
 
+    if ( domid >= DOMID_FIRST_RESERVED && domid != DOMID_ANY )
+        return DOMID_INVALID;
+

Without changing tools/helpers/init-xenstore-domain.c to use DOMID_ANY instead of ~0 as input domid this will break xenstore-stubdom creation.

Another option would be to modify xc_domain_create() to use DOMID_ANY in case domid was specified as 0 or >= DOMID_FIRST_RESERVED. This would at once fix the test cases Anthony saw failing. I'm not sure this is a modification being accepted, though.

Juergen