On 22.04.2025 23:54, dm...@proton.me wrote:
> --- a/xen/common/domain.c
> +++ b/xen/common/domain.c
> @@ -66,6 +66,57 @@ DEFINE_RCU_READ_LOCK(domlist_read_lock);
>  static struct domain *domain_hash[DOMAIN_HASH_SIZE];
>  struct domain *domain_list;
>  
> +/* Domain ID allocator */
> +static unsigned int domid_last;
> +
> +static inline bool is_free_domid(domid_t dom)
> +{
> +    struct domain *d = rcu_lock_domain_by_id(dom);
> +
> +    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 - 1] range of IDs,
> + * perform an exhaustive search starting from the end of the used domain ID
> + * range, excluding hardware_domid.
> + */
> +domid_t domid_alloc(domid_t hint)
> +{
> +    domid_t domid = DOMID_INVALID;
> +
> +    if ( hint < DOMID_FIRST_RESERVED )
> +    {
> +        /* Exact match. */
> +        if ( is_free_domid(hint) )
> +            domid = hint;
> +    }
> +    else
> +    {
> +        for ( domid = domid_last + 1; domid != domid_last; domid++ )
> +        {
> +            if ( domid == DOMID_FIRST_RESERVED )
> +                domid = 0;
> +
> +            if ( domid == hardware_domid )
> +                continue;
> +
> +            if ( is_free_domid(domid) )
> +                break;
> +        }
> +
> +        if ( domid != domid_last )
> +            domid_last = domid;
> +    }
> +
> +    return domid;
> +}

The function name suggests the ID returned is firmly allocated by the time
the caller gets to see / use it. Yet that's not the case. Two back-to-back
calls here with the same argument will yield the same result, afaict. This
supports my prior statement that I don't think it is a good idea to
"centralize" things like this.

Jan

Reply via email to