[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v5 21/22] libxl/arm: Introduce domctl command for IOMMU vSID mapping



Hi Milan,

> diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c
> index 94cfab0fa173..0bd3e45fbd5a 100644
> --- a/tools/libs/ctrl/xc_domain.c
> +++ b/tools/libs/ctrl/xc_domain.c
> @@ -2222,6 +2222,31 @@ int xc_domain_set_llc_colors(xc_interface *xch, 
> uint32_t domid,
>  
>      return ret;
>  }
> +
> +int xc_domain_viommu_allocate_vsid_range(xc_interface *xch,
> +                                         uint32_t domid,
> +                                         uint16_t nr_sids,
> +                                         uint32_t first_psid,
> +                                         uint32_t phandle,
> +                                         uint32_t *first_vsid)
> +{
> +    int err;
> +    struct xen_domctl domctl = {};
> +
> +    domctl.cmd = XEN_DOMCTL_viommu_alloc_vsid_range;
> +    domctl.domain = domid;
> +    domctl.u.viommu_alloc_vsid_range.first_psid = first_psid;
> +    domctl.u.viommu_alloc_vsid_range.phandle = phandle;
> +    domctl.u.viommu_alloc_vsid_range.nr_sids = nr_sids;

Nit: you could initialise the struct with less repetition, I know it's
not common in this file, but it's common enough in libxenctrl, and I
hope you agree the result looks better:

    struct xen_domctl domctl = {
        .cmd = XEN_DOMCTL_viommu_alloc_vsid_range,
        .domain = domid,
        .u.viommu_alloc_vsid_range = {
            .first_psid = first_psid,
            .phandle = phandle,
            .nr_sids = nr_sids,
        },
    };

> diff --git a/tools/libs/light/libxl_arm.c b/tools/libs/light/libxl_arm.c
> index 0dc3789d08ea..c1e41ed7fedf 100644
> --- a/tools/libs/light/libxl_arm.c
> +++ b/tools/libs/light/libxl_arm.c
> @@ -1342,11 +1342,101 @@ static int copy_partial_fdt(libxl__gc *gc, void 
> *fdt, void *pfdt)
>      return 0;
>  }
>  
> -static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
> +/*
> + * Store virtualized 'iommus' properties for every node attached to IOMMU
> + * and passthroughed to guest.
> + * Used as a lookup table for mapping <phandle pSID> -> <vhandle vSID>
> + */
> +struct viommu_stream {
> +    XEN_LIST_ENTRY(struct viommu_stream) entry;
> +    char path[128];          /* DT path, stable across resizes */
> +    uint32_t domid;
> +    fdt32_t *iommus;         /* fully virtualized iommus property */
> +};
> +
> +static XEN_LIST_HEAD(, struct viommu_stream) viommu_stream_list;
> +
> +/*
> + * Helper function which creates mapping of dt node to
> + * to virtualized 'iommus' property
> + * Mappings stored in a global 'viommu_stream_list' to
> + * make it reusable for every fdt resize

The `viommu_stream_list` isn't needed. You can call
`viommu_modify_partial_fdt() on the user-provided device tree before
even starting to generate an FDT for the guest. That way,
`viommu_stream_list()` will only be called once.

> + */
> +static int viommu_get_stream(libxl__gc *gc,
> +                             uint32_t domid,
> +                             const fdt32_t *prop,
> +                             int proplen,
> +                             const char* path, fdt32_t **iommus)
>  {
> -    int nodeoff, proplen, i, r;
> +    int i, r;
> +    uint32_t vsid, psid, phandle;
> +    struct viommu_stream *viommu_stream;
> +
> +    /* Lookup if stream for target device is already allocated */
> +    XEN_LIST_FOREACH(viommu_stream, &viommu_stream_list, entry)
> +    {
> +        if (!strcmp(viommu_stream->path, path) &&
> +            viommu_stream->domid == domid) {
> +            *iommus = viommu_stream->iommus;
> +            return 0;
> +        }
> +    }
> +
> +    /* Allocate new viommu stream */
> +    viommu_stream = malloc(sizeof(struct viommu_stream));
> +    if (!viommu_stream)
> +        return ERROR_NOMEM;

For better or for worse, we don't really do this dance of checking for
allocation failure in libxl. Instead, we crash the application.

So, could you use the libxl helpers for allocation? That is,
libxl__malloc() or here libxl__zalloc() which can also replace the
memset(). You can use `NOGC` instead of `gc` if you really need the
allocation to stick around after libxl is done with the domain creation.

> +    memset(viommu_stream, 0, sizeof(struct viommu_stream));
> +    viommu_stream->iommus = malloc(proplen);
> +    if (!viommu_stream->iommus) {
> +        free(viommu_stream);
> +        return ERROR_NOMEM;
> +    }
> +    memset(viommu_stream->iommus, 0, proplen);
> +
> +    LOG(DEBUG, "Creating vIOMMU stream for device %s",

Use LOGD() to add the domain id, since it's known to the function.

> +        path);
> +
> +    /*
> +     * Virtualize device "iommus" property
> +     * (replace pIOMMU with vIOMMU phandle and pSIDs with mapped vSIDs)
> +     */
> +    for (i = 0; i < proplen / 8; ++i) {
> +        /* Allocate new vSID mapped to pSID and physical IOMMU */
> +        phandle = fdt32_to_cpu(prop[i * 2]);
> +        psid = fdt32_to_cpu(prop[i * 2 + 1]);
> +        r = xc_domain_viommu_allocate_vsid_range(CTX->xch, domid, 1, psid,
> +                                                 phandle, &vsid);
> +        if (r) {
> +            LOG(ERROR, "Can't allocate new vSID/vRID for guest IOMMU 
> device");

Use `LOGE` instead, which print `errno` in addition. And since you know
the domain id, you can even use `LOGED` to also print the dom id
associated with the error.

> +            free(viommu_stream->iommus);
> +            free(viommu_stream);
> +            return r;
> +        }
> +        viommu_stream->iommus[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
> +        viommu_stream->iommus[i * 2 + 1] = cpu_to_fdt32(vsid);
> +        LOG(DEBUG, "Mapped vSID: %u to pSID: %u", vsid, psid);
> +    }
> +
> +    strcpy(viommu_stream->path, path);
> +    viommu_stream->domid = domid;
> +    *iommus = viommu_stream->iommus;
> +
> +    XEN_LIST_INSERT_HEAD(&viommu_stream_list, viommu_stream, entry);
> +
> +    return 0;
> +}
> +
> +/*
> + * Used to update partial fdt when vIOMMU is enabled
> + * Maps dt properties of IOMMU devices to virtual IOMMU
> + */
> +static int viommu_modify_partial_fdt(libxl__gc *gc, void *pfdt, uint32_t 
> domid)
> +{
> +    int nodeoff, proplen, r;
>      const fdt32_t *prop;
>      fdt32_t *prop_c;
> +    char path[128];
>  
>      nodeoff = fdt_path_offset(pfdt, "/passthrough");
>      if (nodeoff < 0)
> @@ -1360,11 +1450,16 @@ static int modify_partial_fdt(libxl__gc *gc, void 
> *pfdt)
>          if (!prop)
>              continue;
>  
> -        prop_c = libxl__zalloc(gc, proplen);
> +        r = fdt_get_path(pfdt, nodeoff, path, sizeof(path));
> +        if ( r < 0 ) {
> +            LOG(ERROR, "Can't get passthrough node path");

You may want to print `r` to have at least some kind of information
about why fdt_get_path() failed. It would be even better if there's a
function in fdt that can transform the value in `r` into an error
message.

> +            return r;
> +        }
>  
> -        for (i = 0; i < proplen / 8; ++i) {
> -            prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
> -            prop_c[i * 2 + 1] = prop[i * 2 + 1];
> +        r = viommu_get_stream(gc, domid, prop, proplen, path, &prop_c);
> +        if (r) {
> +            LOG(ERROR, "Can't get viommu stream");
> +            return r;
>          }
>  
>          r = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
> @@ -1376,7 +1471,6 @@ static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
>  
>      return 0;
>  }
> -

Nit: could you keep that empty line?

>  #else
>  
>  static int check_partial_fdt(libxl__gc *gc, void *fdt, size_t size)
> @@ -1395,7 +1489,7 @@ static int copy_partial_fdt(libxl__gc *gc, void *fdt, 
> void *pfdt)
>      return -FDT_ERR_INTERNAL;
>  }
>  
> -static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
> +static int viommu_modify_partial_fdt(libxl__gc *gc, void *pfdt, uint32_t 
> domid)

Could you change the function name in the previous patch? That would be
better than renaming the same function multiple time throughout the
series.

Thanks,


--
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.