xen/domain: introduce DOMID_ANY
PATCH v3 1/1 xen/domain: introduce DOMID_ANY
From: Denis Mukhin <[email protected]>
Add a new symbol DOMID_ANY aliasing DOMID_INVALID to improve the readability of the code.
Update all relevant domid_alloc() call sites.
Amends: 2d5065060710 ("xen/domain: unify domain ID allocation")
---
Changes since v2:
- move DOMID_ANY back to the public header; add proper guards
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2056319227
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
--- 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 | 2 +- xen/common/domid.c | 2 +- xen/include/public/xen.h | 5 +++++ 6 files changed, 15 insertions(+), 9 deletions(-)
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 DOMID_INVALID#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
@@ -848,7 +848,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 -1
@@ -410,7 +410,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 ?:DOMID_ANY );if ( domid ==DOMID_INVALID ) {
xen/common/domid.c +1 -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
xen/include/public/xen.h +5 -0
@@ -608,6 +608,11 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t);/* DOMID_INVALID is used to identify pages with unknown owner. */ #define DOMID_INVALID xen_mk_uint(0x7FF4) +#if defined(__XEN__) || defined(__XEN_TOOLS__) +/* Domain ID allocator: search [1..DOMID_FIRST_RESERVED-1] range. */ +#define DOMID_ANY DOMID_INVALID +#endif +/* Idle domain. */ #define DOMID_IDLE xen_mk_uint(0x7FFF)
Signature · 1 line
2.51.0
Quoted 9 lines · On Tue, 23 Sep 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Add a new symbol
DOMID_ANYaliasingDOMID_INVALIDto improve the readability of the code.Update all relevant
domid_alloc()call sites.Amends:
2d5065060710("xen/domain: unify domain ID allocation")Signed-off-by Denis Mukhin <[email protected]>
Quoted 131 lines
---
Changes since v2:
- moveDOMID_ANYback to the public header; add proper guards
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2056319227
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/--- 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 | 2 +- xen/common/domid.c | 2 +- xen/include/public/xen.h | 5 +++++ 6 files changed, 15 insertions(+), 9 deletions(-)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 DOMID_INVALID#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@@ -848,7 +848,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
-1@@ -410,7 +410,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 ?:DOMID_ANY );if ( domid ==DOMID_INVALID ) {xen/common/domid.c +1
-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 xen/include/public/xen.h +5
-0@@ -608,6 +608,11 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t);/* DOMID_INVALID is used to identify pages with unknown owner. */ #define DOMID_INVALID xen_mk_uint(0x7FF4) +#if defined(__XEN__) || defined(__XEN_TOOLS__) +/* Domain ID allocator: search [1..DOMID_FIRST_RESERVED-1] range. */ +#define DOMID_ANY DOMID_INVALID +#endif +/* Idle domain. */ #define DOMID_IDLE xen_mk_uint(0x7FFF)Signature · 1 line
2.51.0
Quoted 6 lines · Stefano Stabellini
Quoted 5 lines · On Tue, 23 Sep 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Add a new symbol
DOMID_ANYaliasingDOMID_INVALIDto improve the readability of the code.
Quoted 6 lines
Quoted 4 lines
Update all relevant
domid_alloc()call sites.Amends:
2d5065060710("xen/domain: unify domain ID allocation")Signed-off-by Denis Mukhin <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>
The other day concern was voiced over aliasing DOMID_ANY with DOMID_INVALID. I don't recall though who it was or where.
Jan
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Stefano Stabellini
Quoted 7 lines · On Tue, 23 Sep 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Add a new symbol
DOMID_ANYaliasingDOMID_INVALIDto improve the readability of the code.Update all relevant
domid_alloc()call sites.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Amends:
2d5065060710("xen/domain: unify domain ID allocation")Signed-off-by Denis Mukhin <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>The other day concern was voiced over aliasing
DOMID_ANYwithDOMID_INVALID. I don't recall though who it was or where.
I'm afraid it was me (at least) that voiced such concern. But then I completely forgot to reply to the patch. I don't think this is a good idea, aliasing DOMID_ANY with DOMID_INVALID is likely to be dangerous in the long run. In the example here it's fine, because the function itself doesn't use DOMID_INVALID (iow: all usages of DOMID_INVALID are replaced with DOMID_ANY).
However I could see a function wanting to use both DOMID_INVALID and DOMID_ANY for different purposes. Having both aliased to the same value is not going to work as expected. If we have to introduce DOMID_ANY it must use a different value than DOMID_INVALID. And given the context here I would be fine leaving domid_alloc() to handle getting passed DOMID_INVALID as a signal to search for an empty domid to use, I don't see a compelling reason to introduce DOMID_ANY.
Thanks, Roger.
Quoted 25 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Stefano Stabellini
Quoted 7 lines · On Tue, 23 Sep 2025, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Add a new symbol
DOMID_ANYaliasingDOMID_INVALIDto improve the readability of the code.Update all relevant
domid_alloc()call sites.Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Amends:
2d5065060710("xen/domain: unify domain ID allocation")Signed-off-by Denis Mukhin <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>The other day concern was voiced over aliasing
DOMID_ANYwithDOMID_INVALID. I don't recall though who it was or where.I'm afraid it was me (at least) that voiced such concern. But then I completely forgot to reply to the patch. I don't think this is a good idea, aliasing
DOMID_ANYwithDOMID_INVALIDis likely to be dangerous in the long run. In the example here it's fine, because the function itself doesn't useDOMID_INVALID(iow: all usages ofDOMID_INVALIDare replaced withDOMID_ANY).
Quoted 7 lines
However I could see a function wanting to use both
DOMID_INVALIDandDOMID_ANYfor different purposes. Having both aliased to the same value is not going to work as expected. If we have to introduceDOMID_ANYit must use a different value thanDOMID_INVALID. And given the context here I would be fine leavingdomid_alloc()to handle getting passedDOMID_INVALIDas a signal to search for an empty domid to use, I don't see a compelling reason to introduceDOMID_ANY.
Thanks for the feedback!
I agree with having a dedicated reservation for DOMID_ANY. I think introducing a new DOMID_ANY symbol improves code readability a bit.
Quoted 2 lines
Thanks, Roger.