Skip to contentSkip to conversation

xen/domain: domain ID allocation

PATCH Superseded [email protected] 2 participants 4 messages May 4, 2025

v5 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v5 cover xen/domain: domain ID allocation

The patch series adds new library calls for allocating domain IDs. Patch 1 introduces new domid_{init,alloc,free} calls. Patch 2 adjusts hardware domain ID treatment on Arm.

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

Denis Mukhin (2):
xen/domain: unify domain ID allocation
xen/domain: adjust domain ID allocation for Arm

 xen/arch/arm/dom0less-build.c | 17 ++++----
 xen/arch/arm/domain_build.c   | 17 +++++---
 xen/arch/arm/setup.c          |  2 +
 xen/arch/x86/setup.c          | 13 +++++--
 xen/common/domain.c           | 73 +++++++++++++++++++++++++++++++++++
 xen/common/domctl.c           | 41 ++------------------
 xen/include/xen/domain.h      |  4 ++
 7 files changed, 112 insertions(+), 55 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/2 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 rangeset.

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is not used;
- Otherwise, perform an exhaustive search starting from the end of the used domain ID range. domid_alloc() guarantees that two subsequent calls will result in different IDs allocation.

Initialize the domain IDs rangeset from the new domid_init() which is called from arch setup code.

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

No functional change intended.

---
Changes since v4:
- reworked to rangeset
- dropped hardware_domid change

---
 xen/arch/arm/dom0less-build.c | 10 +++--
 xen/arch/arm/domain_build.c   | 17 ++++++---
 xen/arch/arm/setup.c          |  2 +
 xen/arch/x86/setup.c          | 13 +++++--
 xen/common/domain.c           | 70 +++++++++++++++++++++++++++++++++++
 xen/common/domctl.c           | 41 ++------------------
 xen/include/xen/domain.h      |  4 ++
 7 files changed, 107 insertions(+), 50 deletions(-)
xen/arch/arm/dom0less-build.c +6 -4
index a356fc94fc..9eb64ec60d 100644
@@ -1038,15 +1038,13 @@ void __init create_domUs(void)
         };
         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");
-
         if ( dt_property_read_u32(node, "capabilities", &val) )
         {
             if ( val & ~DOMAIN_CAPS_MASK )
@@ -1223,7 +1221,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/arch/arm/domain_build.c +12 -5
index 270a6b97e4..29e22a8809 100644
@@ -2371,6 +2371,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 */
@@ -2395,19 +2396,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/arm/setup.c +2 -0
index 10b46d0684..c3959e8d8e 100644
@@ -418,6 +418,8 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)
 
     timer_init();
 
+    domid_init();
+
     init_idle_domain();
 
     rcu_init();
xen/arch/x86/setup.c +9 -4
index 2518954124..02f665f520 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);
@@ -1915,6 +1918,8 @@ void asmlinkage __init noreturn __start_xen(void)
     mmio_ro_ranges = rangeset_new(NULL, "r/o mmio ranges",
                                   RANGESETF_prettyprint_hex);
 
+    domid_init();
+
     xsm_multiboot_init(bi);
 
     /*
xen/common/domain.c +70 -0

82 lines not on this page · raw message

xen/common/domctl.c +4 -37

58 lines not on this page · raw message

xen/include/xen/domain.h +4 -0
index e10baf2615..3bc960e6a4 100644
@@ -38,6 +38,10 @@ void arch_get_domain_info(const struct domain *d,
 
 domid_t get_initial_domain_id(void);
 
+void domid_init(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

PATCH v5 2/2 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.

---
Changes since v4:
- new patch

---
 xen/arch/arm/dom0less-build.c | 9 +++------
 xen/arch/arm/domain_build.c   | 4 ++--
 xen/common/domain.c           | 5 ++++-
 3 files changed, 9 insertions(+), 9 deletions(-)
xen/arch/arm/dom0less-build.c +3 -6
index 9eb64ec60d..61e01b7306 100644
@@ -1216,14 +1216,11 @@ void __init create_domUs(void)
         if ( !llc_coloring_enabled && llc_colors_str )
             panic("'llc-colors' found, but LLC coloring is disabled\n");
 
-        /*
-         * 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/arch/arm/domain_build.c +2 -2
index 29e22a8809..c85d72fdf1 100644
@@ -2396,9 +2396,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/domain.c +4 -1
index 0ba3cdc47d..055397b5aa 100644
@@ -85,7 +85,7 @@ void __init domid_init(void)
  *
  * If hint is outside of valid [0..DOMID_FIRST_RESERVED - 1] range of IDs,
  * perform an exhaustive search starting from the end of the used domain ID
- * range.
+ * range, excluding get_initial_domain_id() ID.
  */
 domid_t domid_alloc(domid_t domid)
 {
@@ -103,6 +103,9 @@ domid_t domid_alloc(domid_t domid)
             if ( domid == DOMID_FIRST_RESERVED )
                 domid = 0;
 
+            if ( domid == get_initial_domain_id() )
+                continue;
+
             if ( !rangeset_contains_singleton(domid_rangeset, domid) )
                 break;
         }
Signature · 1 line
2.34.1
#3 lore raw
In reply to [email protected]
Quoted 11 lines · On 5/4/25 9:56 AM, [email protected] wrote:

The patch series adds new library calls for allocating domain IDs. Patch 1 introduces new domid_{init,alloc,free} calls. Patch 2 adjusts hardware domain ID treatment on Arm.

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

Denis Mukhin (2):
xen/domain: unify domain ID allocation
xen/domain: adjust domain ID allocation for Arm

Quoted 8 lines
 xen/arch/arm/dom0less-build.c | 17 ++++----
 xen/arch/arm/domain_build.c   | 17 +++++---
 xen/arch/arm/setup.c          |  2 +
 xen/arch/x86/setup.c          | 13 +++++--
 xen/common/domain.c           | 73 +++++++++++++++++++++++++++++++++++
 xen/common/domctl.c           | 41 ++------------------
 xen/include/xen/domain.h      |  4 ++
 7 files changed, 112 insertions(+), 55 deletions(-)

This email was marked as spam due to a DKIM failure. Is this a mailing list misconfiguration?

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)