Skip to contentSkip to conversation

xen/domain: domain ID allocation

PATCH Superseded [email protected] 2 participants 11 messages May 21, 2025 – Jun 2, 2025

v8 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v8 cover xen/domain: domain ID allocation

The patch series adds new library calls for allocating domain IDs.

Patch 1 introduces new domid_{alloc,free} calls.
Patch 2 adjusts hardware domain ID treatment on Arm.
Patch 3 is an RFC: introduces new CONFIG_MAX_DOMID parameter to limit the number of user domains during run-time.

Link to v7: https://lore.kernel.org/xen-devel/[email protected]/
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1828093998

Denis Mukhin (3):
xen/domain: unify domain ID allocation
xen/domain: adjust domain ID allocation for Arm
xen/domain: introduce CONFIG_MAX_DOMID

 xen/arch/arm/domain_build.c             | 17 +++++--
 xen/arch/arm/tee/ffa.c                  |  3 +-
 xen/arch/x86/cpu/mcheck/mce.c           |  2 +-
 xen/arch/x86/cpu/vpmu.c                 |  2 +-
 xen/arch/x86/setup.c                    | 11 +++--
 xen/common/Kconfig                      |  7 +++
 xen/common/device-tree/dom0less-build.c | 17 ++++---
 xen/common/domain.c                     | 62 +++++++++++++++++++++++--
 xen/common/domctl.c                     | 42 ++---------------
 xen/common/sched/core.c                 |  4 +-
 xen/drivers/passthrough/vtd/iommu.c     |  2 +-
 xen/include/public/domctl.h             |  2 +-
 xen/include/xen/domain.h                |  3 ++
 13 files changed, 108 insertions(+), 66 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v8 1/3 xen/domain: unify domain ID allocation

From: Denis Mukhin <[email protected]>

Currently, hypervisor code has two different non-system domain ID allocation implementations:

(a) Sequential IDs allocation in dom0less Arm code based on max_init_domid;

(b) Sequential IDs allocation in XEN_DOMCTL_createdomain; does not use max_init_domid (both Arm and x86).

It makes sense to have a common helper code for such task across architectures (Arm and x86) and between dom0less / toolstack domU allocation.

Wrap the domain ID allocation as an arch-independent function domid_alloc() in common/domain.c based on the bitmap.

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is not used;
- If DOMID_INVALID is provided, search the range [0..DOMID_FIRST_RESERVED-1], starting from the last used ID and wrapping around as needed. It guarantees that two consecutive calls will never return the same ID.

Also, remove is_free_domid() helper as it is not needed now.

No functional change intended.

---
Changes since v7:
- Use DOMID_FIRST_RESERVED

---
 xen/arch/arm/domain_build.c             | 17 ++++++---
 xen/arch/x86/setup.c                    | 11 +++---
 xen/common/device-tree/dom0less-build.c | 10 +++---
 xen/common/domain.c                     | 47 +++++++++++++++++++++++++
 xen/common/domctl.c                     | 42 +++-------------------
 xen/include/xen/domain.h                |  3 ++
 6 files changed, 80 insertions(+), 50 deletions(-)
xen/arch/arm/domain_build.c +12 -5
index b189a7cfae..e9d563c269 100644
@@ -2010,6 +2010,7 @@ void __init create_dom0(void)
         .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version),
     };
     unsigned int flags = CDF_privileged | CDF_hardware;
+    domid_t domid;
     int rc;
 
     /* The vGIC for DOM0 is exactly emulating the hardware GIC */
@@ -2034,19 +2035,25 @@ void __init create_dom0(void)
     if ( !llc_coloring_enabled )
         flags |= CDF_directmap;
 
-    dom0 = domain_create(0, &dom0_cfg, flags);
+    domid = domid_alloc(0);
+    if ( domid == DOMID_INVALID )
+        panic("Error allocating domain ID 0\n");
+
+    dom0 = domain_create(domid, &dom0_cfg, flags);
     if ( IS_ERR(dom0) )
-        panic("Error creating domain 0 (rc = %ld)\n", PTR_ERR(dom0));
+        panic("Error creating domain %d (rc = %ld)\n", domid, PTR_ERR(dom0));
 
     if ( llc_coloring_enabled && (rc = dom0_set_llc_colors(dom0)) )
-        panic("Error initializing LLC coloring for domain 0 (rc = %d)\n", rc);
+        panic("Error initializing LLC coloring for domain %pd (rc = %d)\n",
+              dom0, rc);
 
     if ( alloc_dom0_vcpu0(dom0) == NULL )
-        panic("Error creating domain 0 vcpu0\n");
+        panic("Error creating domain %pdv0\n", dom0);
 
     rc = construct_dom0(dom0);
     if ( rc )
-        panic("Could not set up DOM0 guest OS (rc = %d)\n", rc);
+        panic("Could not set up guest OS for domain %pd (rc = %d)\n",
+              dom0, rc);
 
     set_xs_domain(dom0);
 }
xen/arch/x86/setup.c +7 -4
index 2518954124..ac1c3e669b 100644
@@ -1030,8 +1030,11 @@ static struct domain *__init create_dom0(struct boot_info *bi)
     if ( iommu_enabled )
         dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
-    /* Create initial domain.  Not d0 for pvshim. */
-    bd->domid = get_initial_domain_id();
+    /* Allocate initial domain ID. Not d0 for pvshim. */
+    bd->domid = domid_alloc(get_initial_domain_id());
+    if ( bd->domid == DOMID_INVALID )
+        panic("Error allocating domain ID %d\n", get_initial_domain_id());
+
     d = domain_create(bd->domid, &dom0_cfg,
                       pv_shim ? 0 : CDF_privileged | CDF_hardware);
     if ( IS_ERR(d) )
@@ -1063,7 +1066,7 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 
         if ( (strlen(acpi_param) == 0) && acpi_disabled )
         {
-            printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
+            printk("ACPI is disabled, notifying domain %pd (acpi=off)\n", d);
             safe_strcpy(acpi_param, "off");
         }
 
@@ -1078,7 +1081,7 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 
     bd->d = d;
     if ( construct_dom0(bd) != 0 )
-        panic("Could not construct domain 0\n");
+        panic("Could not construct domain %pd\n", d);
 
     bd->cmdline = NULL;
     xfree(cmdline);
xen/common/device-tree/dom0less-build.c +6 -4
index 2c56f13771..9236dbae11 100644
@@ -850,15 +850,13 @@ void __init create_domUs(void)
         struct xen_domctl_createdomain d_cfg = {0};
         unsigned int flags = 0U;
         bool has_dtb = false;
+        domid_t domid;
         uint32_t val;
         int rc;
 
         if ( !dt_device_is_compatible(node, "xen,domain") )
             continue;
 
-        if ( (max_init_domid + 1) >= DOMID_FIRST_RESERVED )
-            panic("No more domain IDs available\n");
-
         d_cfg.max_evtchn_port = 1023;
         d_cfg.max_grant_frames = -1;
         d_cfg.max_maptrack_frames = -1;
@@ -981,7 +979,11 @@ void __init create_domUs(void)
          * very important to use the pre-increment operator to call
          * domain_create() with a domid > 0. (domid == 0 is reserved for Dom0)
          */
-        d = domain_create(++max_init_domid, &d_cfg, flags);
+        domid = domid_alloc(++max_init_domid);
+        if ( domid == DOMID_INVALID )
+            panic("Error allocating ID for domain %s\n", dt_node_name(node));
+
+        d = domain_create(domid, &d_cfg, flags);
         if ( IS_ERR(d) )
             panic("Error creating domain %s (rc = %ld)\n",
                   dt_node_name(node), PTR_ERR(d));
xen/common/domain.c +47 -0

65 lines not on this page · raw message

xen/common/domctl.c +5 -37

61 lines not on this page · raw message

xen/include/xen/domain.h +3 -0
index e10baf2615..8aab05ae93 100644
@@ -38,6 +38,9 @@ void arch_get_domain_info(const struct domain *d,
 
 domid_t get_initial_domain_id(void);
 
+domid_t domid_alloc(domid_t domid);
+void domid_free(domid_t domid);
+
 /* CDF_* constant. Internal flags for domain creation. */
 /* Is this a privileged domain? */
 #define CDF_privileged           (1U << 0)
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v8 1/3] xen/domain: unify domain ID allocation

xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 72

Quoted 10 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/common/domain.c +4 -0
@@ -66,6 +66,11 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);
 static struct domain *domain_hash[DOMAIN_HASH_SIZE];
 struct domain *domain_list;
 
+/* Non-system domain ID allocator. */
+static DEFINE_SPINLOCK(domid_lock);
+static DECLARE_BITMAP(domid_bitmap, DOMID_FIRST_RESERVED);
+static domid_t domid_last;

Please move this into the narrowest possible scope. No reason to expose it to the entire CU.

Quoted 10 lines
xen/common/domain.c +6 -0
@@ -2405,6 +2412,46 @@ domid_t get_initial_domain_id(void)
     return hardware_domid;
 }
 
+domid_t domid_alloc(domid_t domid)
+{
+    spin_lock(&domid_lock);
+
+    if ( domid < DOMID_FIRST_RESERVED )
+    {

xen/common/domain.c domid_t get_initial_domain_id(void) · line 2430

Quoted 10 lines
xen/common/domain.c +10 -0
+        if ( __test_and_set_bit(domid, domid_bitmap) )
+            domid = DOMID_INVALID;
+    }
+    else
+    {
+        domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED,
+                                   domid_last);
+
+        if ( domid == DOMID_FIRST_RESERVED )
+            domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED, 0);

Despite what the last sentence of the description says, there is a subtle behavioral change here: The original code would never return what was called "rover" there. If the most recently created domain went away, you would return its ID again here if no other ID is free (which is easier to run into with patch 3 in place, and MAX_DOMID set to a pretty low value). (Noticing only later: This could even occur without wrapping, as the last argument passed is just domid_last, without adding in 1.)

I agree it's debatable whether using the sole available ID instead of failing wouldn't be better(?) behavior in this specific case, but such definitely can't go silently.

Furthermore you once again are potentially returning ID 0 here (after wrapping), when the original code specifically avoided that (irrespective of there actually being a Dom0, that is; i.e. covering both the dom0less case and the late-hwdom one).

To be frank, being at v8 I find it problematic that there still are (unmentioned and potentially unwanted) behavioral changes here. This kind of supports my earlier raised question of whether we actually want this sort of a change.

Jan

#3 lore raw

Re: [PATCH v8 1/3] xen/domain: unify domain ID allocation

Quoted 7 lines · Jan Beulich
Quoted 6 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/common/domain.c +0 -0
@@ -66,6 +66,11 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);
 static struct domain *domain_hash[DOMAIN_HASH_SIZE];
 struct domain *domain_list;
 
Quoted 7 lines

xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 72

Quoted 4 lines
xen/common/domain.c +4 -0
+/* Non-system domain ID allocator. */
+static DEFINE_SPINLOCK(domid_lock);
+static DECLARE_BITMAP(domid_bitmap, DOMID_FIRST_RESERVED);
+static domid_t domid_last;

Please move this into the narrowest possible scope. No reason to expose it to the entire CU.

Will do.

Quoted 22 lines
Quoted 11 lines
xen/common/domain.c +6 -0
@@ -2405,6 +2412,46 @@ domid_t get_initial_domain_id(void)
     return hardware_domid;
 }
 
+domid_t domid_alloc(domid_t domid)
+{
+    spin_lock(&domid_lock);
+
+    if ( domid < DOMID_FIRST_RESERVED )
+    {

xen/common/domain.c domid_t get_initial_domain_id(void) · line 2430

Quoted 10 lines
xen/common/domain.c +10 -0
+        if ( __test_and_set_bit(domid, domid_bitmap) )
+            domid = DOMID_INVALID;
+    }
+    else
+    {
+        domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED,
+                                   domid_last);
+
+        if ( domid == DOMID_FIRST_RESERVED )
+            domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED, 0);
Quoted 7 lines

Despite what the last sentence of the description says, there is a subtle behavioral change here: The original code would never return what was called "rover" there. If the most recently created domain went away, you would return its ID again here if no other ID is free (which is easier to run into with patch 3 in place, and MAX_DOMID set to a pretty low value). (Noticing only later: This could even occur without wrapping, as the last argument passed is just domid_last, without adding in 1.)

Thanks for spotting the problem!

Quoted 10 lines

I agree it's debatable whether using the sole available ID instead of failing wouldn't be better(?) behavior in this specific case, but such definitely can't go silently.

Furthermore you once again are potentially returning ID 0 here (after wrapping), when the original code specifically avoided that (irrespective of there actually being a Dom0, that is; i.e. covering both the dom0less case and the late-hwdom one).

Quoted 4 lines

To be frank, being at v8 I find it problematic that there still are (unmentioned and potentially unwanted) behavioral changes here. This kind of supports my earlier raised question of whether we actually want this sort of a change.

I appreciate patience and the feedback for the series!

Quoted 2 lines

Jan

#4 lore raw

PATCH v8 2/3 xen/domain: adjust domain ID allocation for Arm

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Remove the hardcoded domain ID 0 allocation for hardware domain and replace it with a call to get_initial_domain_id() (returns the value of hardware_domid on Arm).

Update domid_alloc(DOMID_INVALID) case to ensure that get_initial_domain_id() ID is skipped during domain ID allocation to cover domU case in dom0less configuration. That also fixes a potential issue with re-using ID#0 for domUs when get_initial_domain_id() returns non-zero.

---
Changes since v7:
- use bool reserved; in domid_alloc()

---
 xen/arch/arm/domain_build.c             | 4 ++--
 xen/common/device-tree/dom0less-build.c | 9 +++------
 xen/common/domain.c                     | 6 ++++++
 3 files changed, 11 insertions(+), 8 deletions(-)
xen/arch/arm/domain_build.c +2 -2
index e9d563c269..0ad80b020a 100644
@@ -2035,9 +2035,9 @@ void __init create_dom0(void)
     if ( !llc_coloring_enabled )
         flags |= CDF_directmap;
 
-    domid = domid_alloc(0);
+    domid = domid_alloc(get_initial_domain_id());
     if ( domid == DOMID_INVALID )
-        panic("Error allocating domain ID 0\n");
+        panic("Error allocating domain ID %d\n", get_initial_domain_id());
 
     dom0 = domain_create(domid, &dom0_cfg, flags);
     if ( IS_ERR(dom0) )
xen/common/device-tree/dom0less-build.c +3 -6
index 9236dbae11..8e38affd0c 100644
@@ -974,14 +974,11 @@ void __init create_domUs(void)
 
         arch_create_domUs(node, &d_cfg, flags);
 
-        /*
-         * The variable max_init_domid is initialized with zero, so here it's
-         * very important to use the pre-increment operator to call
-         * domain_create() with a domid > 0. (domid == 0 is reserved for Dom0)
-         */
-        domid = domid_alloc(++max_init_domid);
+        domid = domid_alloc(DOMID_INVALID);
         if ( domid == DOMID_INVALID )
             panic("Error allocating ID for domain %s\n", dt_node_name(node));
+        if ( max_init_domid < domid )
+            max_init_domid = domid;
 
         d = domain_create(domid, &d_cfg, flags);
         if ( IS_ERR(d) )
xen/common/domain.c +6 -0
index 9c6932c457..01a65cb35d 100644
@@ -2423,6 +2423,9 @@ domid_t domid_alloc(domid_t domid)
     }
     else
     {
+        bool reserved = __test_and_set_bit(get_initial_domain_id(),
+                                           domid_bitmap);
+
         domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED,
                                    domid_last);
 
@@ -2438,6 +2441,9 @@ domid_t domid_alloc(domid_t domid)
             __set_bit(domid, domid_bitmap);
             domid_last = domid;
         }
+
+        if ( !reserved )
+            __clear_bit(reserved, domid_bitmap);
     }
 
     spin_unlock(&domid_lock);
Signature · 1 line
2.34.1
#5 lore raw

PATCH v8 3/3 xen/domain: introduce CONFIG_MAX_DOMID

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Embedded deployments of Xen do not need to have support for more than dozen of domains.

Introduce build-time configuration option to limit the number of domains during run-time.

Also, move DOMID_FIRST_RESERVED compile-time check from Arm to common code.

---
This is an RFC.

Changes since v7:
- moved DOMID_FIRST_RESERVED compile-time checks to common code
- changed description for MAX_DOMID Kconfig option

---
 xen/arch/arm/tee/ffa.c              |  3 +--
 xen/arch/x86/cpu/mcheck/mce.c       |  2 +-
 xen/arch/x86/cpu/vpmu.c             |  2 +-
 xen/common/Kconfig                  |  7 +++++++
 xen/common/domain.c                 | 21 ++++++++++++---------
 xen/common/sched/core.c             |  4 ++--
 xen/drivers/passthrough/vtd/iommu.c |  2 +-
 xen/include/public/domctl.h         |  2 +-
 8 files changed, 26 insertions(+), 17 deletions(-)
xen/arch/arm/tee/ffa.c +1 -2
index 3bbdd7168a..7417ce6bed 100644
@@ -331,10 +331,9 @@ static int ffa_domain_init(struct domain *d)
      * reserved for the hypervisor and we only support secure endpoints using
      * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
      */
-    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);
     BUILD_BUG_ON((DOMID_MASK & BIT(15, U)) != 0);
 
-    if ( d->domain_id >= DOMID_FIRST_RESERVED )
+    if ( d->domain_id >= CONFIG_MAX_DOMID )
         return -ERANGE;
 
     ctx = xzalloc(struct ffa_ctx);
xen/arch/x86/cpu/mcheck/mce.c +1 -1
index 1c348e557d..ee8ddd33b0 100644
@@ -1493,7 +1493,7 @@ long do_mca(XEN_GUEST_HANDLE_PARAM(xen_mc_t) u_xen_mc)
             d = rcu_lock_domain_by_any_id(mc_msrinject->mcinj_domid);
             if ( d == NULL )
             {
-                if ( mc_msrinject->mcinj_domid >= DOMID_FIRST_RESERVED )
+                if ( mc_msrinject->mcinj_domid >= CONFIG_MAX_DOMID )
                     return x86_mcerr("do_mca inject: incompatible flag "
                                      "MC_MSRINJ_F_GPADDR with domain %d",
                                      -EINVAL, domid);
xen/arch/x86/cpu/vpmu.c +1 -1
index c28192ea26..67d423e088 100644
@@ -174,7 +174,7 @@ void vpmu_do_interrupt(void)
      * in XENPMU_MODE_ALL, for everyone.
      */
     if ( (vpmu_mode & XENPMU_MODE_ALL) ||
-         (sampled->domain->domain_id >= DOMID_FIRST_RESERVED) )
+         (sampled->domain->domain_id >= CONFIG_MAX_DOMID) )
     {
         sampling = choose_hwdom_vcpu();
         if ( !sampling )
xen/common/Kconfig +7 -0
index 6d43be2e6e..66b91840f2 100644
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.
+
 endmenu
xen/common/domain.c +12 -9
index 01a65cb35d..204b71d096 100644
@@ -68,7 +68,7 @@ struct domain *domain_list;
 
 /* Non-system domain ID allocator. */
 static DEFINE_SPINLOCK(domid_lock);
-static DECLARE_BITMAP(domid_bitmap, DOMID_FIRST_RESERVED);
+static DECLARE_BITMAP(domid_bitmap, CONFIG_MAX_DOMID);
 static domid_t domid_last;
 
 /*
@@ -155,7 +155,7 @@ int domain_init_states(void)
     ASSERT(rw_is_write_locked_by_me(&current->domain->event_lock));
 
     dom_state_changed = xvzalloc_array(unsigned long,
-                                       BITS_TO_LONGS(DOMID_FIRST_RESERVED));
+                                       BITS_TO_LONGS(CONFIG_MAX_DOMID));
     if ( !dom_state_changed )
         return -ENOMEM;
 
@@ -235,7 +235,7 @@ int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
     while ( dom_state_changed )
     {
         dom = find_first_bit(dom_state_changed, DOMID_MASK + 1);
-        if ( dom >= DOMID_FIRST_RESERVED )
+        if ( dom >= CONFIG_MAX_DOMID )
             break;
         if ( test_and_clear_bit(dom, dom_state_changed) )
         {
@@ -824,7 +824,7 @@ struct domain *domain_create(domid_t domid,
     /* Sort out our idea of is_hardware_domain(). */
     if ( (flags & CDF_hardware) || domid == hardware_domid )
     {
-        if ( hardware_domid < 0 || hardware_domid >= DOMID_FIRST_RESERVED )
+        if ( hardware_domid < 0 || hardware_domid >= CONFIG_MAX_DOMID )
             panic("The value of hardware_dom must be a valid domain ID\n");
 
         /* late_hwdom is only allowed for dom0. */
@@ -2414,9 +2414,12 @@ domid_t get_initial_domain_id(void)
 
 domid_t domid_alloc(domid_t domid)
 {
+    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);
+    BUILD_BUG_ON(DOMID_FIRST_RESERVED < CONFIG_MAX_DOMID);
+
     spin_lock(&domid_lock);
 
-    if ( domid < DOMID_FIRST_RESERVED )
+    if ( domid < CONFIG_MAX_DOMID )
     {
         if ( __test_and_set_bit(domid, domid_bitmap) )
             domid = DOMID_INVALID;
@@ -2426,13 +2429,13 @@ domid_t domid_alloc(domid_t domid)
         bool reserved = __test_and_set_bit(get_initial_domain_id(),
                                            domid_bitmap);
 
-        domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED,
+        domid = find_next_zero_bit(domid_bitmap, CONFIG_MAX_DOMID,
                                    domid_last);
 
-        if ( domid == DOMID_FIRST_RESERVED )
-            domid = find_next_zero_bit(domid_bitmap, DOMID_FIRST_RESERVED, 0);
+        if ( domid == CONFIG_MAX_DOMID )
+            domid = find_next_zero_bit(domid_bitmap, CONFIG_MAX_DOMID, 0);
 
-        if ( domid == DOMID_FIRST_RESERVED )
+        if ( domid == CONFIG_MAX_DOMID )
         {
             domid = DOMID_INVALID;
         }
xen/common/sched/core.c +2 -2
index 9043414290..f1bfb6f6a2 100644
@@ -867,7 +867,7 @@ int sched_init_domain(struct domain *d, unsigned int poolid)
     int ret;
 
     ASSERT(d->cpupool == NULL);
-    ASSERT(d->domain_id < DOMID_FIRST_RESERVED);
+    ASSERT(d->domain_id < CONFIG_MAX_DOMID);
 
     if ( (ret = cpupool_add_domain(d, poolid)) )
         return ret;
@@ -891,7 +891,7 @@ int sched_init_domain(struct domain *d, unsigned int poolid)
 
 void sched_destroy_domain(struct domain *d)
 {
-    ASSERT(d->domain_id < DOMID_FIRST_RESERVED);
+    ASSERT(d->domain_id < CONFIG_MAX_DOMID);
 
     if ( d->cpupool )
     {
xen/drivers/passthrough/vtd/iommu.c +1 -1
index c55f02c97e..5df85ca629 100644
@@ -1509,7 +1509,7 @@ int domain_context_mapping_one(
 
         prev_did = context_domain_id(lctxt);
         domid = did_to_domain_id(iommu, prev_did);
-        if ( domid < DOMID_FIRST_RESERVED )
+        if ( domid < CONFIG_MAX_DOMID )
             prev_dom = rcu_lock_domain_by_id(domid);
         else if ( pdev ? domid == pdev->arch.pseudo_domid : domid > DOMID_MASK )
             prev_dom = rcu_lock_domain(dom_io);
xen/include/public/domctl.h +1 -1
index 5b2063eed9..0c14c30c1b 100644
@@ -36,7 +36,7 @@
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
- * If it is specified as an invalid value (0 or >= DOMID_FIRST_RESERVED),
+ * If it is specified as an invalid value (0 or >= CONFIG_MAX_DOMID),
  * an id is auto-allocated and returned.
  */
 /* XEN_DOMCTL_createdomain */
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v8 3/3] xen/domain: introduce CONFIG_MAX_DOMID

xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 334

Quoted 7 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/arch/arm/tee/ffa.c +0 -1
@@ -331,10 +331,9 @@ static int ffa_domain_init(struct domain *d)
      * reserved for the hypervisor and we only support secure endpoints using
      * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
      */
-    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);

Why's this being moved to common code? It certainly may have a purpose here (which I'm simply unaware of); I don't see what purpose it has in common code.

xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 584

Quoted 12 lines
xen/common/Kconfig +6 -0
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.

My prior comment remains: The description and help needs to be accurate, in order to not cause any confusion. In a true dom0less environment I'm not sure the "user" can create any domains (post boot, that is). And when there is Dom0 (or late hwdom), the number specified already isn't the number of domains one can create (again, post boot, which is how I understand "user domains"). If someone picked 1 as the value here, it's unclear to me how late hwdom or dom0less would work in the first place.

xen/include/public/domctl.h line 39

Quoted 8 lines
xen/include/public/domctl.h +1 -1
@@ -36,7 +36,7 @@
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
- * If it is specified as an invalid value (0 or >= DOMID_FIRST_RESERVED),
+ * If it is specified as an invalid value (0 or >= CONFIG_MAX_DOMID),

In the public interface I question the relevance of any implementation details of the hypervisor.

Jan

#7 lore raw

Re: [PATCH v8 3/3] xen/domain: introduce CONFIG_MAX_DOMID

Quoted 12 lines · Jan Beulich

xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 334

Quoted 7 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/arch/arm/tee/ffa.c +0 -1
@@ -331,10 +331,9 @@ static int ffa_domain_init(struct domain *d)
      * reserved for the hypervisor and we only support secure endpoints using
      * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
      */
-    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);

Why's this being moved to common code? It certainly may have a purpose here (which I'm simply unaware of); I don't see what purpose it has in common code.

My understanding having DOMID_FIRST_RESERVED compile-time checks in one place is good for testability: the check in question also applies to x86.

I will drop that hunk.

Quoted 7 lines
Quoted 7 lines
xen/common/Kconfig +0 -0
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
Quoted 14 lines

xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 584

Quoted 6 lines
xen/common/Kconfig +6 -0
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.

My prior comment remains: The description and help needs to be accurate, in order to not cause any confusion. In a true dom0less environment I'm not sure the "user" can create any domains (post boot, that is). And when there is Dom0 (or late hwdom), the number specified already isn't the number of domains one can create (again, post boot, which is how I understand "user domains"). If someone picked 1 as the value here, it's unclear to me how late hwdom or dom0less would work in the first place.

Do you think something like the following will be more accurate?

config MAX_DOMID
int "Maximum number of domains"
range 1 32752
default 32752
help
Specifies the maximum number of domains: dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains (domid >= DOMID_FIRST_RESERVED).

Quoted 5 lines
Quoted 5 lines
xen/include/public/domctl.h +0 -0
@@ -36,7 +36,7 @@
 
Quoted 7 lines

xen/include/public/domctl.h line 39

Quoted 4 lines
xen/include/public/domctl.h +1 -1
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
- * If it is specified as an invalid value (0 or >= DOMID_FIRST_RESERVED),
+ * If it is specified as an invalid value (0 or >= CONFIG_MAX_DOMID),

In the public interface I question the relevance of any implementation details of the hypervisor.

Ack, will revert.

Quoted 3 lines

Jan

#8 lore raw

Re: [PATCH v8 3/3] xen/domain: introduce CONFIG_MAX_DOMID

Quoted 14 lines · On 22.05.2025 02:09, [email protected] wrote:
Quoted 12 lines · Jan Beulich

xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 334

Quoted 7 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/arch/arm/tee/ffa.c +0 -1
@@ -331,10 +331,9 @@ static int ffa_domain_init(struct domain *d)
      * reserved for the hypervisor and we only support secure endpoints using
      * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
      */
-    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);

Why's this being moved to common code? It certainly may have a purpose here (which I'm simply unaware of); I don't see what purpose it has in common code.

Quoted 4 lines

My understanding having DOMID_FIRST_RESERVED compile-time checks in one place is good for testability: the check in question also applies to x86.

I will drop that hunk.

And also the other one, unless you can explain what exactly you're checking. The connection between DOMID_FIRST_RESERVED and UINT16_MAX is at best indirect, through domid_t. Yet if domid_t was widened (possible in principle, but breaking the ABI) that check would end up wrong without the compiler noticing (unless DOMID_FIRST_RESERVED was also bumped, which however is an independent thing).

Quoted 23 lines
Quoted 6 lines
Quoted 6 lines
xen/common/Kconfig +0 -0
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
Quoted 14 lines

xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 584

Quoted 6 lines
xen/common/Kconfig +6 -0
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.

My prior comment remains: The description and help needs to be accurate, in order to not cause any confusion. In a true dom0less environment I'm not sure the "user" can create any domains (post boot, that is). And when there is Dom0 (or late hwdom), the number specified already isn't the number of domains one can create (again, post boot, which is how I understand "user domains"). If someone picked 1 as the value here, it's unclear to me how late hwdom or dom0less would work in the first place.

Do you think something like the following will be more accurate?

Quoted 8 lines

config MAX_DOMID
int "Maximum number of domains"
range 1 32752
default 32752
help
Specifies the maximum number of domains: dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains (domid >= DOMID_FIRST_RESERVED).

Especially the mention of DOMID_FIRST_RESERVED is too much of an implementation detail here, imo. Beyond that - maybe, but I'm not overly happy this way either.

As an aside - MAX_DOMID and "Maximum number of domains" are conflicting with one another, too: Do you mean "maximum ID" or "maximum number of"? The two are different by 1.

Jan

#9 lore raw

Re: [PATCH v8 3/3] xen/domain: introduce CONFIG_MAX_DOMID

Quoted 18 lines · Jan Beulich
Quoted 17 lines · On 22.05.2025 02:09, [email protected] wrote:
Quoted 12 lines · Jan Beulich

xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 334

Quoted 7 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/arch/arm/tee/ffa.c +0 -1
@@ -331,10 +331,9 @@ static int ffa_domain_init(struct domain *d)
      * reserved for the hypervisor and we only support secure endpoints using
      * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
      */
-    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);

Why's this being moved to common code? It certainly may have a purpose here (which I'm simply unaware of); I don't see what purpose it has in common code.

My understanding having DOMID_FIRST_RESERVED compile-time checks in one place is good for testability: the check in question also applies to x86.

Quoted 8 lines
Quoted 1 line

I will drop that hunk.

And also the other one, unless you can explain what exactly you're checking. The connection between DOMID_FIRST_RESERVED and UINT16_MAX is at best indirect, through domid_t. Yet if domid_t was widened (possible in principle, but breaking the ABI) that check would end up wrong without the compiler noticing (unless DOMID_FIRST_RESERVED was also bumped, which however is an independent thing).

xen/include/public/xen.h +1 -1
index 82b9c05a76..452d9f63dc 100644
@@ -572,7 +572,7 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t);
 #endif
 
 /* Domain ids >= DOMID_FIRST_RESERVED cannot be used for ordinary domains. */
-#define DOMID_FIRST_RESERVED xen_mk_uint(0x7FF0)
+#define DOMID_FIRST_RESERVED xen_mk_uint(0xFFFF7FF0)
 
 /* DOMID_SELF is used in certain contexts to refer to oneself. */
 #define DOMID_SELF           xen_mk_uint(0x7FF0)

The above patch ^^ pretty much passes the compilation on x86. The compile time check in question currently exists for Arm only but could catch such mistake when the limit is bumped (if any), it is easy to spot.

Quoted 33 lines
Quoted 24 lines
Quoted 7 lines
Quoted 7 lines
xen/common/Kconfig +0 -0
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
Quoted 14 lines

xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 584

Quoted 6 lines
xen/common/Kconfig +6 -0
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.

My prior comment remains: The description and help needs to be accurate, in order to not cause any confusion. In a true dom0less environment I'm not sure the "user" can create any domains (post boot, that is). And when there is Dom0 (or late hwdom), the number specified already isn't the number of domains one can create (again, post boot, which is how I understand "user domains"). If someone picked 1 as the value here, it's unclear to me how late hwdom or dom0less would work in the first place.

Do you think something like the following will be more accurate?

Quoted 8 lines

config MAX_DOMID
int "Maximum number of domains"
range 1 32752
default 32752
help
Specifies the maximum number of domains: dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains (domid >= DOMID_FIRST_RESERVED).

Quoted 2 lines

Especially the mention of DOMID_FIRST_RESERVED is too much of an implementation detail here, imo. Beyond that - maybe, but I'm not overly happy this way either.

Will the following description will be satisfactory?

config MAX_DOMID
int "Maximum domain ID"
range 1 32752
default 32752
help
Specifies the maximum domain ID (dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains).

Quoted 4 lines

As an aside - MAX_DOMID and "Maximum number of domains" are conflicting with one another, too: Do you mean "maximum ID" or "maximum number of"? The two are different by 1.

That would be "maximum ID", thank you.

Quoted 2 lines

Jan

#10 lore raw

Re: [PATCH v8 3/3] xen/domain: introduce CONFIG_MAX_DOMID

Quoted 41 lines · On 22.05.2025 20:08, [email protected] wrote:
Quoted 35 lines · Jan Beulich
Quoted 25 lines · On 22.05.2025 02:09, [email protected] wrote:
Quoted 7 lines · Jan Beulich
Quoted 6 lines · On 21.05.2025 02:00, [email protected] wrote:
xen/common/Kconfig +0 -0
@@ -576,4 +576,11 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
Quoted 14 lines

xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 584

Quoted 6 lines
xen/common/Kconfig +6 -0
+config MAX_DOMID
+	int "Maximum number of user domains"
+	range 1 32752
+	default 32752
+	help
+	  Specifies the maximum number of domains a user can create.

My prior comment remains: The description and help needs to be accurate, in order to not cause any confusion. In a true dom0less environment I'm not sure the "user" can create any domains (post boot, that is). And when there is Dom0 (or late hwdom), the number specified already isn't the number of domains one can create (again, post boot, which is how I understand "user domains"). If someone picked 1 as the value here, it's unclear to me how late hwdom or dom0less would work in the first place.

Do you think something like the following will be more accurate?

Quoted 8 lines

config MAX_DOMID
int "Maximum number of domains"
range 1 32752
default 32752
help
Specifies the maximum number of domains: dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains (domid >= DOMID_FIRST_RESERVED).

Quoted 2 lines

Especially the mention of DOMID_FIRST_RESERVED is too much of an implementation detail here, imo. Beyond that - maybe, but I'm not overly happy this way either.

Will the following description will be satisfactory?

Quoted 7 lines

config MAX_DOMID
int "Maximum domain ID"
range 1 32752
default 32752
help
Specifies the maximum domain ID (dom0 or late hwdom, predefined domains, post-boot domains, excluding Xen system domains).

This is better, yes, yet ...

Quoted 5 lines
Quoted 3 lines

As an aside - MAX_DOMID and "Maximum number of domains" are conflicting with one another, too: Do you mean "maximum ID" or "maximum number of"? The two are different by 1.

That would be "maximum ID", thank you.

... imo asking for "maximum" of something is perhaps a little odd. Asking for "number of" is the more usual thing imo.

Yet as before - I remain unconvinced we need such a Kconfig setting in the first place.

Jan