xen/cache-col: Multiple fixes
cover xen/cache-col: Multiple fixes
Found because XEN_DOMCTL_set_llc_colors failed in XenServer's HostUEFI Secure Boot environment (which has additional checks on hypercalls). Everything else came from trying to fix that.
https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/1946483850
Andrew Cooper (4):
xen/cache-col: Remove bogus cast in domain_llc_coloring_free() xen/cache-col: Fix freeing of colouring information xen/cache-col: Fix initialisation of domain colouring information tools/libxl: Remove unconditional XEN_DOMCTL_set_llc_colors hypercall
tools/libs/light/libxl_create.c | 24 +++++++++++--------- xen/common/domain.c | 11 +++++++-- xen/common/llc-coloring.c | 40 ++++++++++++++------------------- xen/include/xen/llc-coloring.h | 2 ++ xen/include/xen/sched.h | 2 +- 5 files changed, 42 insertions(+), 37 deletions(-)
base-commit: 5c798ac8854af3528a78ca5a622c9ea68399809b
Signature · 1 line
2.39.5
PATCH 1/4 xen/cache-col: Remove bogus cast in domain_llc_coloring_free()
Using:
xfree(__va(__pa(d->llc_colors)));
is an extraordinarily expensive way of writing:
xfree((void *)d->llc_colours);
Combined with the comment indicating that this was intention, the patch should have been rejected outright.
Correct the type of d->llc_colours by removing the bogus const on it, and remove the cast when freeing it.
No functional change.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Carlo Nonato <[email protected]>
CC: Marco Solieri <[email protected]>
--- xen/common/llc-coloring.c | 7 ++----- xen/include/xen/sched.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-)
xen/common/llc-coloring.c +2 -5
@@ -309,11 +309,8 @@ int domain_set_llc_colors(struct domain *d,void domain_llc_coloring_free (struct domain *d) { -if ( !llc_coloring_enabled || d->llc_colors == default_colors ) -return ; - -/* free pointer-to-const using __va(__pa()) */ -xfree (__va (__pa (d->llc_colors))); +if ( d->llc_colors != default_colors ) +xfree (d->llc_colors); }int __init domain_set_llc_colors_from_str (struct domain *d,const char *str)
xen/include/xen/sched.h +1 -1
@@ -649,7 +649,7 @@ struct domain#ifdef CONFIG_LLC_COLORING unsigned int num_llc_colors; -const unsigned int *llc_colors; +unsigned int *llc_colors;#endif /* Console settings. */
Signature · 1 line
2.39.5
Re: [PATCH 1/4] xen/cache-col: Remove bogus cast in domain_llc_coloring_free()
Quoted 21 lines · Andrew Cooper
xen/common/llc-coloring.c +2
-5@@ -309,11 +309,8 @@ int domain_set_llc_colors(struct domain *d,void domain_llc_coloring_free (struct domain *d) { -if ( !llc_coloring_enabled || d->llc_colors == default_colors ) -return ; - -/* free pointer-to-const using __va(__pa()) */ -xfree (__va (__pa (d->llc_colors))); +if ( d->llc_colors != default_colors ) +xfree (d->llc_colors); }int __init domain_set_llc_colors_from_str (struct domain *d,const char *str)xen/include/xen/sched.h +0
-0
xen/include/xen/sched.h struct domain · line 655
Quoted 8 lines
xen/include/xen/sched.h +1
-1@@ -649,7 +649,7 @@ struct domain#ifdef CONFIG_LLC_COLORING unsigned int num_llc_colors; -const unsigned int *llc_colors; +unsigned int *llc_colors;#endif /* Console settings. */
Ah yes, I see. Yet no, I don't agree. The only sane course of action to avoid odd transformations like the above (without using casts to cast away const-ness) is to finally make xfree() et al take pointers to const void. Arguments towards why this makes sense were given before; I don't think they need repeating. Dropping the const here is rather undesirable: Once set, the colors shouldn't be altered anymore. Pointers like this hence want to be pointer-to-const, to make accidental modification less likely. Which in turn calls for the mentioned adjustment to xfree(). Which you keep objecting to for reasons I sadly cannot follow.
Jan
Stefano Stabellini
Reviewed-byRe: [PATCH 1/4] xen/cache-col: Remove bogus cast in domain_llc_coloring_free()
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Andrew Cooper
xen/common/llc-coloring.c +2
-5@@ -309,11 +309,8 @@ int domain_set_llc_colors(struct domain *d,void domain_llc_coloring_free (struct domain *d) { -if ( !llc_coloring_enabled || d->llc_colors == default_colors ) -return ; - -/* free pointer-to-const using __va(__pa()) */ -xfree (__va (__pa (d->llc_colors))); +if ( d->llc_colors != default_colors ) +xfree (d->llc_colors); }int __init domain_set_llc_colors_from_str (struct domain *d,const char *str)xen/include/xen/sched.h +1
-1@@ -649,7 +649,7 @@ struct domain#ifdef CONFIG_LLC_COLORING unsigned int num_llc_colors; -const unsigned int *llc_colors; +unsigned int *llc_colors;#endif
Quoted 11 lines
xen/include/xen/sched.h struct domain · line 655
Quoted 1 line
xen/include/xen/sched.h +0
-0/* Console settings. */ Ah yes, I see. Yet no, I don't agree. The only sane course of action to avoid odd transformations like the above (without using casts to cast away const-ness) is to finally make
xfree()et al take pointers to const void. Arguments towards why this makes sense were given before; I don't think they need repeating. Dropping the const here is rather undesirable: Once set, the colors shouldn't be altered anymore. Pointers like this hence want to be pointer-to-const, to make accidental modification less likely. Which in turn calls for the mentioned adjustment toxfree().
I agree that "Once set, the colors shouldn't be altered anymore. Pointers like this hence want to be pointer-to-const, to make accidental modification less likely".
However, I also think that the following is worse than risking accidental unwanted modifications of llc_colors:
/* free pointer-to-const using __va(__pa()) */
xfree(__va(__pa(d->llc_colors)));So in my opinion this patch is good. If/when xfree becomes able to deal with const pointers, then we can change llc_colors to be const again.
PATCH 2/4 xen/cache-col: Fix freeing of colouring information
In reply to Andrew Cooperdomain_destroy() is the wrong position to be freeing colouring information.
The comment in context identifies how domain_destroy() can be called multiple times on the same domain, leading to a double free of d->llc_colors as it's the wrong side of the atomic_cmpxchg() to be made safe.
Furthermore, by freeing d->llc_colors but leaving d->num_llc_colors nonzero, alloc_color_heap_page() can in principle use after free.
Make domain_llc_coloring_free() idempotent, zeroing d->num_llc_colors and NULL-ing d->llc_colors after freeing, and call it from domain_teardown().
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Carlo Nonato <[email protected]>
CC: Marco Solieri <[email protected]>
Cache colouring is experimental, which is why no XSA is being allocated for these bugs.
--- xen/common/domain.c | 5 +++-- xen/common/llc-coloring.c | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-)
xen/common/domain.c +3 -2
@@ -626,6 +626,9 @@ static int domain_teardown(struct domain *d)case PROG_none:BUILD_BUG_ON (PROG_none !=0 ); +/* Trivial teardown, not long-running enough to need a preemption check. */ +domain_llc_coloring_free (d); + PROGRESS(gnttab_mappings): rc =gnttab_release_mappings (d);if ( rc )
@@ -1447,8 +1450,6 @@ void domain_destroy(struct domain *d) {BUG_ON (!d->is_dying); -domain_llc_coloring_free (d); -/* May be already destroyed, or get_domain() can race us. */ if (atomic_cmpxchg (&d->refcnt,0 ,DOMAIN_DESTROYED ) !=0 )return ;
xen/common/llc-coloring.c +3 -1
@@ -309,8 +309,10 @@ int domain_set_llc_colors(struct domain *d,void domain_llc_coloring_free (struct domain *d) { + d->num_llc_colors =0 ; +if ( d->llc_colors != default_colors ) -xfree (d->llc_colors); +XFREE (d->llc_colors); }int __init domain_set_llc_colors_from_str (struct domain *d,const char *str)
Signature · 1 line
2.39.5
Re: [PATCH 2/4] xen/cache-col: Fix freeing of colouring information
Quoted 11 lines · Andrew Cooper
domain_destroy()is the wrong position to be freeing colouring information.The comment in context identifies how
domain_destroy()can be called multiple times on the same domain, leading to a double free ofd->llc_colorsas it's the wrong side of theatomic_cmpxchg()to be made safe.Furthermore, by freeing
d->llc_colorsbut leavingd->num_llc_colorsnonzero,alloc_color_heap_page()can in principle use after free.Make
domain_llc_coloring_free()idempotent, zeroingd->num_llc_colorsand NULL-ingd->llc_colorsafter freeing, and call it fromdomain_teardown().
And this pulling earlier is safe because e.g. free_color_heap_page() -> page_to_llc_color() -> mfn_to_color() don't use the domain's colors array. That's not a given (and could be different if e.g. per-domain stats were maintained), so may want mentioning.
Quoted 2 lines
Fixes 6985aa5e0c3c ("xen: extend domctl interface for cache coloring")Signed-off-by Andrew Cooper <[email protected]>
PATCH 3/4 xen/cache-col: Fix initialisation of domain colouring information
In reply to Andrew CooperRight now, domains start with no colouring information and depend on a XEN_DOMCTL_set_llc_colors(nr=0) hypercall in order to get default colours.
This in turn forces the toolstack to make a hypercall for all VMs even those without colouring configured, and to ignore errors if Xen doesn't have colouring active.
Introduce domain_llc_coloring_init(), replacing domain_set_default_colors(), and call it during the trivial initialisation in domain_create(). Leave two comments explaining the positioning.
The -EEXISTs condition in domain_set_llc_colors() needs adjusting as a consequence, but that's easy enough to keep working as before.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Carlo Nonato <[email protected]>
CC: Marco Solieri <[email protected]>
--- xen/common/domain.c | 6 ++++++ xen/common/llc-coloring.c | 33 ++++++++++++++------------------- xen/include/xen/llc-coloring.h | 2 ++ 3 files changed, 22 insertions(+), 19 deletions(-)
xen/common/domain.c +6 -0
@@ -878,6 +878,12 @@ struct domain *domain_create(domid_t domid,rwlock_init (&d->pci_lock);#endif +/* +* Doesn't allocate memory itself, but does set up data relevant for +* memory allocations. +*/ +domain_llc_coloring_init (d); +/* All error paths can depend on the above setup. */ /*
xen/common/llc-coloring.c +14 -19
@@ -230,24 +230,12 @@ void domain_dump_llc_colors(const struct domain *d)print_colors (d->llc_colors, d->num_llc_colors); } -static void domain_set_default_colors (struct domain *d) -{ -printk (XENLOG_WARNING -"LLC color config not found for %pd, using all colors\n" , d); - - d->llc_colors = default_colors; - d->num_llc_colors = max_nr_colors; -} -int __init dom0_set_llc_colors (struct domain *d) {typeof (*dom0_colors) *colors;if ( !dom0_num_colors ) - { -domain_set_default_colors (d);return 0 ; - }if ( (dom0_num_colors > max_nr_colors) || !check_colors (dom0_colors, dom0_num_colors) )
@@ -272,14 +260,11 @@ int domain_set_llc_colors(struct domain *d, {unsigned int *colors; -if ( d->num_llc_colors ) +if ( d->llc_colors != default_colors )return -EEXIST ;if ( !config->num_llc_colors ) - { -domain_set_default_colors (d);return 0 ; - }if ( config->num_llc_colors > max_nr_colors )return -EINVAL ;
@@ -307,6 +292,19 @@ int domain_set_llc_colors(struct domain *d,return 0 ; } +void domain_llc_coloring_init (struct domain *d) +{ +if ( !llc_coloring_enabled ) +return ; + +/* +* Any change to this logic needs to consider the position of our call in +* domain_create(). +*/ + d->llc_colors = default_colors; + d->num_llc_colors = max_nr_colors; +} +void domain_llc_coloring_free (struct domain *d) { d->num_llc_colors =0 ;
@@ -321,10 +319,7 @@ int __init domain_set_llc_colors_from_str(struct domain *d, const char *str)unsigned int *colors, num_colors;if ( !str ) - { -domain_set_default_colors (d);return 0 ; - } colors = xmalloc_array(unsigned int, max_nr_colors);if ( !colors )
xen/include/xen/llc-coloring.h +2 -0
@@ -21,6 +21,7 @@ extern bool llc_coloring_enabled;void llc_coloring_init (void );void dump_llc_coloring_info (void );void domain_dump_llc_colors (const struct domain *d); +void domain_llc_coloring_init (struct domain *d);void domain_llc_coloring_free (struct domain *d);#else #define llc_coloring_enabled false
@@ -28,6 +29,7 @@ void domain_llc_coloring_free(struct domain *d);static inline void llc_coloring_init (void ) {}static inline void dump_llc_coloring_info (void ) {}static inline void domain_dump_llc_colors (const struct domain *d) {} +static inline void domain_llc_coloring_init (struct domain *d) {}static inline void domain_llc_coloring_free (struct domain *d) {}#endif
Signature · 1 line
2.39.5
Re: [PATCH 3/4] xen/cache-col: Fix initialisation of domain colouring information
Quoted 11 lines · Andrew Cooper
Right now, domains start with no colouring information and depend on a
XEN_DOMCTL_set_llc_colors(nr=0)hypercall in order to get default colours.This in turn forces the toolstack to make a hypercall for all VMs even those without colouring configured, and to ignore errors if Xen doesn't have colouring active.
Introduce
domain_llc_coloring_init(), replacingdomain_set_default_colors(), and call it during the trivial initialisation indomain_create(). Leave two comments explaining the positioning.
Quoted 5 lines
The -EEXISTs condition in
domain_set_llc_colors()needs adjusting as a consequence, but that's easy enough to keep working as before.Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")Signed-off-by Andrew Cooper <[email protected]>
preferably with ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 885
Quoted 11 lines
xen/common/domain.c +5
-0@@ -878,6 +878,12 @@ struct domain *domain_create(domid_t domid,rwlock_init (&d->pci_lock);#endif +/* +* Doesn't allocate memory itself, but does set up data relevant for +* memory allocations. +*/ +domain_llc_coloring_init (d);
... the word "default" added to the comment here.
Jan
PATCH 4/4 tools/libxl: Remove unconditional XEN_DOMCTL_set_llc_colors hypercall
In reply to Andrew CooperHypercalls are not free; cache colouring is an experimental feature, and ignoring an -EOPNOTSUPP is bad form.
Now that Xen has been fixed to initialise colouring information correctly for domains, xc_domain_set_llc_colors() only needs calling for domains with explicit configuration.
Rearrange the logic to avoid the hypercall in the general case, leaving a comment explaining why it is performed so early.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Carlo Nonato <[email protected]>
CC: Marco Solieri <[email protected]>
--- tools/libs/light/libxl_create.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
tools/libs/light/libxl_create.c +13 -11
@@ -699,19 +699,21 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,/* A new domain now exists */ *domid = local_domid; - ret =xc_domain_set_llc_colors (ctx->xch, local_domid, - b_info->llc_colors, - b_info->num_llc_colors); -if (ret <0 ) { -if (errno ==EOPNOTSUPP ) { -if (b_info->num_llc_colors >0 ) { +/* +* If Cache Coloring is wanted for the guest, it must be +* communicated to Xen prior to allocating guest memory. +*/ +if (b_info->num_llc_colors) { + ret =xc_domain_set_llc_colors (ctx->xch, local_domid, + b_info->llc_colors, + b_info->num_llc_colors); +if (ret <0 ) { +if (errno ==EOPNOTSUPP ) {LOGED (ERROR , local_domid, -"LLC coloring not enabled in the hypervisor" ); - rc =ERROR_FAIL ; - goto out; +"LLC coloring not enabled in the hypervisor" ); + }else { +LOGED (ERROR , local_domid,"LLC colors allocation failed" ); } - }else { -LOGED (ERROR , local_domid,"LLC colors allocation failed" ); rc =ERROR_FAIL ; goto out; }
Signature · 1 line
2.39.5
Re: [PATCH 4/4] tools/libxl: Remove unconditional XEN_DOMCTL_set_llc_colors hypercall
Quoted 7 lines · Andrew Cooper
Hypercalls are not free; cache colouring is an experimental feature, and ignoring an
-EOPNOTSUPPis bad form.Now that Xen has been fixed to initialise colouring information correctly for domains,
xc_domain_set_llc_colors()only needs calling for domains with explicit configuration.
Quoted 5 lines
Rearrange the logic to avoid the hypercall in the general case, leaving a comment explaining why it is performed so early.
Fixes 748bd725fbec ("tools: add support for cache coloring configuration")Signed-off-by Andrew Cooper <[email protected]>
Quoted 53 lines
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Carlo Nonato <[email protected]>
CC: Marco Solieri <[email protected]>--- tools/libs/light/libxl_create.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)tools/libs/light/libxl_create.c +13
-11@@ -699,19 +699,21 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,/* A new domain now exists */ *domid = local_domid; - ret =xc_domain_set_llc_colors (ctx->xch, local_domid, - b_info->llc_colors, - b_info->num_llc_colors); -if (ret <0 ) { -if (errno ==EOPNOTSUPP ) { -if (b_info->num_llc_colors >0 ) { +/* +* If Cache Coloring is wanted for the guest, it must be +* communicated to Xen prior to allocating guest memory. +*/ +if (b_info->num_llc_colors) { + ret =xc_domain_set_llc_colors (ctx->xch, local_domid, + b_info->llc_colors, + b_info->num_llc_colors); +if (ret <0 ) { +if (errno ==EOPNOTSUPP ) {LOGED (ERROR , local_domid, -"LLC coloring not enabled in the hypervisor" ); - rc =ERROR_FAIL ; - goto out; +"LLC coloring not enabled in the hypervisor" ); + }else { +LOGED (ERROR , local_domid,"LLC colors allocation failed" ); } - }else { -LOGED (ERROR , local_domid,"LLC colors allocation failed" ); rc =ERROR_FAIL ; goto out; }Signature · 1 line
2.39.5