xen/domain: unify domain ID allocation
PATCH v3 1/1 xen/domain: unify domain ID allocation
From: Denis Mukhin <[email protected]>
Unify the logic of domain ID allocation, so that both the initial domain creation and the usage by domctl use the same helper function across architectures (Arm and x86).
Wrap the domain ID allocation as an arch-independent function domid_alloc() in common/domain.c.
Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excluding hardware_domid.
Move the is_free_domid() helper closer to domid_alloc(). Simplify is_free_domid() by removing the domain ID range check, as the ID is now guaranteed to be within the valid range. Additionally, update the predicate to return a bool value instead of an int.
---
Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1770801847
I hope by moving it to an isolated thread the review will be simpler.
Changes since v2:
- added max_init_domid change so the patch does not need to depend on other patches
- moved uses of domid_alloc() close to domain_create()
- use DOMID_INVALID as the indicator for doing an exhaustive domain ID search
--- xen/arch/arm/dom0less-build.c | 19 ++++++++------- xen/arch/arm/domain_build.c | 19 +++++++++++---- xen/arch/x86/setup.c | 8 +++++-- xen/common/domain.c | 45 +++++++++++++++++++++++++++++++++++ xen/common/domctl.c | 45 ++++------------------------------- xen/include/xen/domain.h | 2 ++ 6 files changed, 81 insertions(+), 57 deletions(-)
xen/arch/arm/dom0less-build.c +10 -9
@@ -993,15 +993,13 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags =0U ; +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_find_property (node,"xen,static-mem" ,NULL ) ) {if ( llc_coloring_enabled )
@@ -1125,12 +1123,15 @@ 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) -*/ - d =domain_create (++max_init_domid, &d_cfg, flags); + rc =domid_alloc (DOMID_INVALID ); +if ( rc <0 ) +panic ("Error allocating ID for domain %s (rc = %d)\n" , +dt_node_name (node), rc); + domid = rc; +if ( max_init_domid < domid ) + max_init_domid = domid; + + 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 +14 -5
@@ -2370,6 +2370,7 @@ void __init create_dom0(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags = CDF_privileged; +domid_t domid;int rc;/* The vGIC for DOM0 is exactly emulating the hardware GIC */
@@ -2394,19 +2395,27 @@ void __init create_dom0(void)if ( !llc_coloring_enabled ) flags |= CDF_directmap; - dom0 =domain_create (0 , &dom0_cfg, flags); + rc =domid_alloc (get_initial_domain_id ()); +if ( rc <0 ) +panic ("Error allocating domain ID %d (rc = %d)\n" , +get_initial_domain_id (), rc); + domid = rc; + + 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 %d (rc = %d)\n" , + domid, rc);if (alloc_dom0_vcpu0 (dom0) ==NULL ) -panic ("Error creating domain 0 vcpu0\n" ); +panic ("Error creating domain %d vcpu0\n" , domid); 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 %d (rc = %d)\n" , + domid, rc); }/*
xen/arch/x86/setup.c +6 -2
@@ -1009,8 +1009,12 @@ 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 <0 ) +panic ("Error allocating domain ID %d (rc = %d)\n" , +get_initial_domain_id (), bd->domid); + d =domain_create (bd->domid, &dom0_cfg, pv_shim ?0 : CDF_privileged);if (IS_ERR (d) )panic ("Error creating d%u: %ld\n" , bd->domid,PTR_ERR (d));
xen/common/domain.c +45 -0
51 lines not on this page · raw message
xen/common/domctl.c +4 -41
58 lines not on this page · raw message
xen/include/xen/domain.h +2 -0
@@ -38,6 +38,8 @@ void arch_get_domain_info(const struct domain *d,domid_t get_initial_domain_id (void ); +int domid_alloc (int hint); +/* CDF_* constant. Internal flags for domain creation. */ /* Is this a privileged domain? */ #define CDF_privileged (1U << 0)
Signature · 1 line
2.34.1
Quoted 15 lines · On 16.04.2025 08:15, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Unify the logic of domain ID allocation, so that both the initial domain creation and the usage by domctl use the same helper function across architectures (Arm and x86).
Wrap the domain ID allocation as an arch-independent function
domid_alloc()incommon/domain.c.Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excludinghardware_domid.
Quoted 6 lines
Move the
is_free_domid()helper closer todomid_alloc(). Simplifyis_free_domid()by removing the domain ID range check, as the ID is now guaranteed to be within the valid range. Additionally, update the predicate to return a bool value instead of an int.Signed-off-by Denis Mukhin <[email protected]>
Please can you clarify whether this is intended to be no functional change (as far as one would be able to observe from the outside)? (It isn't, and when it isn't, the behavioral change needs justifying. Which I fear you won't be able to, in which case it needs undoing. Not using the first unused ID is a deliberate property of the present allocation scheme.)
Quoted 8 lines
--- xen/arch/arm/dom0less-build.c | 19 ++++++++------- xen/arch/arm/domain_build.c | 19 +++++++++++---- xen/arch/x86/setup.c | 8 +++++-- xen/common/domain.c | 45 +++++++++++++++++++++++++++++++++++ xen/common/domctl.c | 45 ++++------------------------------- xen/include/xen/domain.h | 2 ++ 6 files changed, 81 insertions(+), 57 deletions(-)
This suggests it's not clearly an improvement. And I'm heavily inclined to ask (also considering the above) that this simply be dropped.
Quoted 15 lines
xen/arch/arm/domain_build.c +1
-1@@ -2370,6 +2370,7 @@ void __init create_dom0(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags = CDF_privileged; +domid_t domid;int rc;/* The vGIC for DOM0 is exactly emulating the hardware GIC */ @@ -2394,19 +2395,27 @@ void __init create_dom0(void)if ( !llc_coloring_enabled ) flags |= CDF_directmap; - dom0 =domain_create (0 , &dom0_cfg, flags);
xen/arch/arm/domain_build.c void __init create_dom0(void) · line 2406
Quoted 10 lines
xen/arch/arm/domain_build.c +8
-1+ rc =domid_alloc (get_initial_domain_id ()); +if ( rc <0 ) +panic ("Error allocating domain ID %d (rc = %d)\n" , +get_initial_domain_id (), rc); + domid = rc; + + 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));
Up to here using domid is okay. However, ...
Quoted 5 lines
xen/arch/arm/domain_build.c +2
-1if ( 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 %d (rc = %d)\n" , + domid, rc);
xen/arch/arm/domain_build.c void __init create_dom0(void) · line 2419
Quoted 10 lines
xen/arch/arm/domain_build.c +3
-2if (alloc_dom0_vcpu0 (dom0) ==NULL ) -panic ("Error creating domain 0 vcpu0\n" ); +panic ("Error creating domain %d vcpu0\n" , domid); 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 %d (rc = %d)\n" , + domid, rc); }
... these all would better use %pd, when already being touched.
While touching all of these I think you also want to aim at making output match that %pd or %pv would result in, if they were usable at those places.
xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1013
Quoted 10 lines
xen/arch/x86/setup.c +2
-2@@ -1009,8 +1009,12 @@ 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 ());
You're clipping the int return value to domid_t here, and thus ...
xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1014
Quoted 1 line
xen/arch/x86/setup.c +1
-0+if ( bd->domid <0 )
... this condition will be always false. I'm surprised the compiler didn't flag this for you.
Quoted 10 lines
xen/common/domain.c +4
-0@@ -66,6 +66,51 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);static struct domain *domain_hash[DOMAIN_HASH_SIZE ];struct domain *domain_list; +static inline bool is_free_domid (domid_t dom) +{ +struct domain *d =rcu_lock_domain_by_id (dom); +
xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 82
Quoted 10 lines
xen/common/domain.c +10
-0+if ( d ) +rcu_unlock_domain (d); + +return !d; +} + +/* +* Allocate new domain ID based on the hint. +* +* If hint is outside of valid [0..DOMID_FIRST_RESERVED] range of IDs,
That's [0, DOMID_FIRST_RESERVED), to be unambiguous. In C array initializer notation it would be [0 ... DOMID_FIRST_RESERVED - 1].
xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 86
Quoted 4 lines
xen/common/domain.c +4
-0+* perform an exhaustive search of the first free domain ID excluding +* hardware_domid. +*/ +int domid_alloc (int hint)
I would have thought that I did comment already on the parameter being plain int.
Quoted 12 lines
xen/common/domain.c +12
-0+{ +domid_t domid; + +if ( hint >=0 && hint <DOMID_FIRST_RESERVED ) + { +if ( !is_free_domid (hint) ) +return -EEXIST ; + + domid = hint; + } +else + {
xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 108
Quoted 10 lines
xen/common/domain.c +10
-0+for ( domid =0 ; domid <DOMID_FIRST_RESERVED ; domid++ ) + { +if ( domid == hardware_domid ) +continue ; +if (is_free_domid (domid) ) +break ; + } + +if ( domid ==DOMID_FIRST_RESERVED ) +return -ENOMEM ;
There's no memory allocation here, so why ENOMEM? ENOSPC may already be slightly better.
Jan
Quoted 21 lines · Jan Beulich
Quoted 20 lines · On 16.04.2025 08:15, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Unify the logic of domain ID allocation, so that both the initial domain creation and the usage by domctl use the same helper function across architectures (Arm and x86).
Wrap the domain ID allocation as an arch-independent function
domid_alloc()incommon/domain.c.Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excludinghardware_domid.Move the
is_free_domid()helper closer todomid_alloc(). Simplifyis_free_domid()by removing the domain ID range check, as the ID is now guaranteed to be within the valid range. Additionally, update the predicate to return a bool value instead of an int.
Quoted 7 lines
Quoted 1 line
Signed-off-by Denis Mukhin <[email protected]>Please can you clarify whether this is intended to be no functional change (as far as one would be able to observe from the outside)? (It isn't, and when it isn't, the behavioral change needs justifying. Which I fear you won't be able to, in which case it needs undoing. Not using the first unused ID is a deliberate property of the present allocation scheme.)
Reverted the algorithm to the original one (v4).
Thanks for review!
Quoted 139 lines
Quoted 9 lines
--- xen/arch/arm/dom0less-build.c | 19 ++++++++------- xen/arch/arm/domain_build.c | 19 +++++++++++---- xen/arch/x86/setup.c | 8 +++++-- xen/common/domain.c | 45 +++++++++++++++++++++++++++++++++++ xen/common/domctl.c | 45 ++++------------------------------- xen/include/xen/domain.h | 2 ++ 6 files changed, 81 insertions(+), 57 deletions(-)This suggests it's not clearly an improvement. And I'm heavily inclined to ask (also considering the above) that this simply be dropped.
Quoted 15 lines
xen/arch/arm/domain_build.c +1
-1@@ -2370,6 +2370,7 @@ void __init create_dom0(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags = CDF_privileged; +domid_t domid;int rc;/* The vGIC for DOM0 is exactly emulating the hardware GIC */ @@ -2394,19 +2395,27 @@ void __init create_dom0(void)if ( !llc_coloring_enabled ) flags |= CDF_directmap; - dom0 =domain_create (0 , &dom0_cfg, flags);xen/arch/arm/domain_build.c void __init create_dom0(void) · line 2406
Quoted 10 lines
xen/arch/arm/domain_build.c +8
-1+ rc =domid_alloc (get_initial_domain_id ()); +if ( rc <0 ) +panic ("Error allocating domain ID %d (rc = %d)\n" , +get_initial_domain_id (), rc); + domid = rc; + + 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));Up to here using domid is okay. However, ...
Quoted 5 lines
xen/arch/arm/domain_build.c +2
-1if ( 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 %d (rc = %d)\n" , + domid, rc);xen/arch/arm/domain_build.c void __init create_dom0(void) · line 2419
Quoted 10 lines
xen/arch/arm/domain_build.c +3
-2if (alloc_dom0_vcpu0 (dom0) ==NULL ) -panic ("Error creating domain 0 vcpu0\n" ); +panic ("Error creating domain %d vcpu0\n" , domid); 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 %d (rc = %d)\n" , + domid, rc); }... these all would better use %pd, when already being touched.
While touching all of these I think you also want to aim at making output match that %pd or %pv would result in, if they were usable at those places.
xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1013
Quoted 10 lines
xen/arch/x86/setup.c +2
-2@@ -1009,8 +1009,12 @@ 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 ());You're clipping the int return value to
domid_there, and thus ...xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1014
Quoted 1 line
xen/arch/x86/setup.c +1
-0+if ( bd->domid <0 )... this condition will be always false. I'm surprised the compiler didn't flag this for you.
Quoted 10 lines
xen/common/domain.c +4
-0@@ -66,6 +66,51 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);static struct domain *domain_hash[DOMAIN_HASH_SIZE ];struct domain *domain_list; +static inline bool is_free_domid (domid_t dom) +{ +struct domain *d =rcu_lock_domain_by_id (dom); +xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 82
Quoted 10 lines
xen/common/domain.c +10
-0+if ( d ) +rcu_unlock_domain (d); + +return !d; +} + +/* +* Allocate new domain ID based on the hint. +* +* If hint is outside of valid [0..DOMID_FIRST_RESERVED] range of IDs, That's [0,
DOMID_FIRST_RESERVED), to be unambiguous. In C array initializer notation it would be [0 ...DOMID_FIRST_RESERVED- 1].xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 86
Quoted 4 lines
xen/common/domain.c +4
-0+* perform an exhaustive search of the first free domain ID excluding +* hardware_domid. +*/ +int domid_alloc (int hint)I would have thought that I did comment already on the parameter being plain int.
Quoted 12 lines
xen/common/domain.c +12
-0+{ +domid_t domid; + +if ( hint >=0 && hint <DOMID_FIRST_RESERVED ) + { +if ( !is_free_domid (hint) ) +return -EEXIST ; + + domid = hint; + } +else + {xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 108
Quoted 10 lines
xen/common/domain.c +10
-0+for ( domid =0 ; domid <DOMID_FIRST_RESERVED ; domid++ ) + { +if ( domid == hardware_domid ) +continue ; +if (is_free_domid (domid) ) +break ; + } + +if ( domid ==DOMID_FIRST_RESERVED ) +return -ENOMEM ;There's no memory allocation here, so why ENOMEM? ENOSPC may already be slightly better.
Jan