xen/domain: introduce DOMID_ANY
PATCH v6 1/1 xen/domain: introduce DOMID_ANY
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 inXEN_DOMCTL_createdomain: turns out libxl__domain_make() (toolstack) uses 0xffff as domain ID.
Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")
---
Changes since v5:
- fixed domain ID validity check in libxl__domain_make()
- CI run: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2368290952
--- tools/libs/light/libxl_create.c | 6 ++++-- 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, 24 insertions(+), 12 deletions(-)
tools/libs/light/libxl_create.c +4 -2
@@ -666,7 +666,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, }for (;;) { -uint32_t local_domid; +uint32_t local_domid =DOMID_INVALID ;bool recent;if (info->domid ==RANDOM_DOMID ) {
@@ -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 (local_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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
tools/libs/light/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, · line 669
Quoted 10 lines · On Fri, Mar 06, 2026 at 06:54:52PM -0800, [email protected] wrote:
tools/libs/light/libxl_create.c +1
-1@@ -666,7 +666,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, }for (;;) { -uint32_t local_domid; +uint32_t local_domid =DOMID_INVALID ;
Surely, this change is unnecessary?
tools/libs/light/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, · line 684
Quoted 8 lines
tools/libs/light/libxl_create.c +1
-0bool recent;if (info->domid ==RANDOM_DOMID ) {@@ -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 (local_domid)) {
You meant to check info->domid, right?
tools/libs/light/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, · line 688
Quoted 5 lines
tools/libs/light/libxl_create.c +2
-1+ local_domid = info->domid; }else { - local_domid = info->domid;/* May not be valid */ + local_domid =DOMID_ANY ; }
Cheers,
--
Anthony Perard | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech