xen/domain: domain ID allocation
v7 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 introduces new CONFIG_MAX_DOMID parameter to limit the number of domains during run-time.
Link to v6: https://lore.kernel.org/xen-devel/[email protected]/
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1825273124
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 | 59 +++++++++++++++++++++++-- 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, 106 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1
PATCH v7 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, perform an exhaustive search within [0..CONFIG_MAX_DOMID-1] range, starting from the last used domain ID. domid_alloc() guarantees that two subsequent calls will result in different IDs allocation.
Also, remove is_free_domid() helper as it is not needed now.
No functional change intended.
---
Changes since v6:
- reworked to bitmap
- dropped incorrect uses of ASSERT()
- fixed XEN_DOMCTL_createdomain to call domid_free() in case of unsuccessful ID allocation
--- 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 | 48 +++++++++++++++++++++++++ xen/common/domctl.c | 42 +++------------------- xen/include/xen/domain.h | 3 ++ 6 files changed, 81 insertions(+), 50 deletions(-)
xen/arch/arm/domain_build.c +12 -5
@@ -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
@@ -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
@@ -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 +48 -0
66 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
@@ -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
Re: [PATCH v7 1/3] xen/domain: unify domain ID allocation
Quoted 13 lines · On 19.05.2025 21:23, [email protected] wrote:
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 usemax_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.
Quoted 8 lines
Wrap the domain ID allocation as an arch-independent function
domid_alloc()incommon/domain.cbased on the bitmap.Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is not used;
- IfDOMID_INVALIDis provided, perform an exhaustive search within [0..CONFIG_MAX_DOMID-1] range, starting from the last used domain ID.
CONFIG_MAX_DOMID first appears in patch 3 afaics, and hence doesn't want mentioning here, yet. That patch,for eample, may never make it into the tree (and thus the description here may be confusing to future readers).
xen/common/domain.c DEFINE_RCU_READ_LOCK(domlist_read_lock); · line 70
Quoted 8 lines
xen/common/domain.c +2
-0@@ -66,6 +66,12 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);static struct domain *domain_hash[DOMAIN_HASH_SIZE ];struct domain *domain_list; +/* Non-system domain ID allocator. */ +#define CONFIG_MAX_DOMID DOMID_FIRST_RESERVED
Oh, wait - you're actually introducing such an identifier. Please don't. It's bad enough that we still have a few CONFIG_* left which aren't Kconfig-controlled. We don't want any new ones.
Jan
PATCH v7 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 v6:
- reworked to use bitmap
--- 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
@@ -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
@@ -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
@@ -2424,6 +2424,9 @@ domid_t domid_alloc(domid_t domid) }else { +domid_t reserved; + + reserved =__test_and_set_bit (get_initial_domain_id (), domid_bitmap); domid =find_next_zero_bit (domid_bitmap,CONFIG_MAX_DOMID , domid_last);
@@ -2439,6 +2442,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
Re: [PATCH v7 2/3] xen/domain: adjust domain ID allocation for Arm
xen/common/domain.c domid_t domid_alloc(domid_t domid) · line 2429
Quoted 9 lines · On 19.05.2025 21:23, [email protected] wrote:
xen/common/domain.c +3
-0@@ -2424,6 +2424,9 @@ domid_t domid_alloc(domid_t domid) }else { +domid_t reserved; + + reserved =__test_and_set_bit (get_initial_domain_id (), domid_bitmap);
This returns a (pseudo-)boolean, and hence the variable's type is wrong, as is ...
xen/common/domain.c domid_t domid_alloc(domid_t domid) · line 2447
Quoted 7 lines
xen/common/domain.c +3
-0@@ -2439,6 +2442,9 @@ domid_t domid_alloc(domid_t domid)__set_bit (domid, domid_bitmap); domid_last = domid; } + +if ( !reserved ) +__clear_bit (reserved, domid_bitmap);
... this.
Jan
PATCH v7 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.
---
Changes since v6:
- new patch
--- 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 | 7 +++---- xen/common/sched/core.c | 4 ++-- xen/drivers/passthrough/vtd/iommu.c | 2 +- xen/include/public/domctl.h | 2 +- 8 files changed, 18 insertions(+), 11 deletions(-)
xen/arch/arm/tee/ffa.c +2 -1
@@ -333,8 +333,9 @@ static int ffa_domain_init(struct domain *d)*/ BUILD_BUG_ON (DOMID_FIRST_RESERVED >=UINT16_MAX );BUILD_BUG_ON ((DOMID_MASK &BIT (15 ,U )) !=0 ); +BUILD_BUG_ON (DOMID_FIRST_RESERVED <CONFIG_MAX_DOMID ); -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
@@ -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
@@ -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
@@ -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 non-system domains" + range 1 32752 + default 32752 + help + Controls the maximum number of non-system domains in the system. + endmenu
xen/common/domain.c +3 -4
@@ -67,7 +67,6 @@ static struct domain *domain_hash[DOMAIN_HASH_SIZE];struct domain *domain_list;/* Non-system domain ID allocator. */ -#define CONFIG_MAX_DOMID DOMID_FIRST_RESERVEDstatic DEFINE_SPINLOCK (domid_lock); static DECLARE_BITMAP(domid_bitmap, CONFIG_MAX_DOMID);static domid_t domid_last;
@@ -156,7 +155,7 @@ int domain_init_states(void)ASSERT (rw_is_write_locked_by_me (¤t->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 ;
@@ -236,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) ) {
@@ -825,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. */
xen/common/sched/core.c +2 -2
@@ -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
@@ -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
@@ -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
Re: [PATCH v7 3/3] xen/domain: introduce CONFIG_MAX_DOMID
Quoted 7 lines · On 19.05.2025 21:23, [email protected] wrote:
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.
I fear I don't see the (sufficiently meaningful) gain of this. And I must have ...
Quoted 1 line
Suggested-by Julien Grall <[email protected]>
... missed tis earlier suggestion, or else I would have asked the question already there.
xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 336
Quoted 7 lines
xen/arch/arm/tee/ffa.c +1
-0@@ -333,8 +333,9 @@ static int ffa_domain_init(struct domain *d)*/ BUILD_BUG_ON (DOMID_FIRST_RESERVED >=UINT16_MAX );BUILD_BUG_ON ((DOMID_MASK &BIT (15 ,U )) !=0 ); +BUILD_BUG_ON (DOMID_FIRST_RESERVED <CONFIG_MAX_DOMID );
We want this check, yes, but in common code. It's entirely unrelated to Arm's TEE.
xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 580
Quoted 8 lines
xen/common/Kconfig +2
-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 non-system domains"
Hmm, without clarifying what a system domain is (is hwdom one? is a control domain one), I fear this may be ambiguous to users.
Jan
Re: [PATCH v7 3/3] xen/domain: introduce CONFIG_MAX_DOMID
Quoted 9 lines · Jan Beulich
Quoted 7 lines · On 19.05.2025 21:23, [email protected] wrote:
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.
Quoted 6 lines
I fear I don't see the (sufficiently meaningful) gain of this. And I must have ...
Quoted 1 line
Suggested-by Julien Grall <[email protected]>... missed tis earlier suggestion, or else I would have asked the question already there.
The code change is based on the feedback here:
https://lore.kernel.org/xen-devel/[email protected]/
It probably should have been sent as an RFC change.
Quoted 25 lines
xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 336
Quoted 8 lines
xen/arch/arm/tee/ffa.c +1
-0@@ -333,8 +333,9 @@ static int ffa_domain_init(struct domain *d)*/ BUILD_BUG_ON (DOMID_FIRST_RESERVED >=UINT16_MAX );BUILD_BUG_ON ((DOMID_MASK &BIT (15 ,U )) !=0 ); +BUILD_BUG_ON (DOMID_FIRST_RESERVED <CONFIG_MAX_DOMID );We want this check, yes, but in common code. It's entirely unrelated to Arm's TEE.
xen/common/Kconfig config BUDDY_ALLOCATOR_SIZE · line 580
Quoted 8 lines
xen/common/Kconfig +2
-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 non-system domains"Hmm, without clarifying what a system domain is (is hwdom one? is a control domain one), I fear this may be ambiguous to users.
Jan