libs/light: use the cpuid feature names from cpufeatureset.h
PATCH 1/1 libs/light: use the cpuid feature names from cpufeatureset.h
The current implementation in libxl_cpuid_parse_config() requires keeping a list of cpuid feature bits that should be mostly in sync with the contents of cpufeatureset.h.
Avoid such duplication by using the automatically generated list of cpuid features in INIT_FEATURE_NAMES in order to map feature names to featureset bits, and then translate from featureset bits into cpuid leaf, subleaf, register tuple.
Note that the full contents of the previous cpuid translation table can't be removed. That's because some feature names allowed by libxl are not described in the featuresets, or because naming has diverged and the previous nomenclature is preserved for compatibility reasons.
Should result in no functional change observed by callers, albeit some new cpuid features will be available as a result of the change.
While there constify cpuid_flags name field.
---
There's currently no table to map from featureset word into cpuid leaf, so open-code one (feature_to_cpuid). Otherwise we would need to parse the comments in cpufeatureset.h in order to obtain the leaf/subleaf/register each word maps into.
One possible way of improving would be to use a cpu_policy and featureset in libxl_cpuid_parse_config(), but that requires adding an xc_cpu_policy_t object to libxl_domain_build_info, and the proposed patch is already an improvement over the current status.
--- docs/man/xl.cfg.5.pod.in | 22 +-- tools/libs/light/libxl_cpuid.c | 246 ++++++++++----------------------- tools/xl/xl_parse.c | 3 + 3 files changed, 78 insertions(+), 193 deletions(-)
docs/man/xl.cfg.5.pod.in +5 -17
@@ -2010,24 +2010,12 @@ proccount procpkg stepping =back -List of keys taking a character: +List of keys taking a character can be found in the public header file +L<arch-x86/cpufeatureset.h|http://xenbits.xen.org/docs/unstable/hypercall/x86_64/include,public,arch-x86,cpufeatureset.h.html> -=over 4 - -3dnow 3dnowext 3dnowprefetch abm acpi adx aes altmovcr8 apic arat avx avx2 -avx512-4fmaps avx512-4vnniw avx512bw avx512cd avx512dq avx512er avx512f -avx512ifma avx512pf avx512vbmi avx512vl bmi1 bmi2 clflushopt clfsh clwb cmov -cmplegacy cmpxchg16 cmpxchg8 cmt cntxid dca de ds dscpl dtes64 erms est extapic -f16c ffxsr fma fma4 fpu fsgsbase fxsr hle htt hypervisor ia64 ibs invpcid -invtsc lahfsahf lm lwp mca mce misalignsse mmx mmxext monitor movbe mpx msr -mtrr nodeid nx ospke osvw osxsave pae page1gb pat pbe pcid pclmulqdq pdcm -perfctr_core perfctr_nb pge pku popcnt pse pse36 psn rdrand rdseed rdtscp rtm -sha skinit smap smep smx ss sse sse2 sse3 sse4.1 sse4.2 sse4_1 sse4_2 sse4a -ssse3 svm svm_decode svm_lbrv svm_npt svm_nrips svm_pausefilt svm_tscrate -svm_vmcbclean syscall sysenter tbm tm tm2 topoext tsc tsc-deadline tsc_adjust -umip vme vmx wdt x2apic xop xsave xtpr - -=back +Note that C<clflush> is described as an option that takes a value, and that +takes precedence over the C<clflush> flag in C<cpufeatureset.h>. The feature +flag must be referenced as C<clfsh>. =back
tools/libs/light/libxl_cpuid.c +70 -176
325 lines not on this page · raw message
tools/xl/xl_parse.c +3 -0
@@ -2620,6 +2620,9 @@ skip_usbdev:case 3 : errstr ="illegal CPUID value (must be: [0|1|x|k|s])" ;break ; +case 4 : + errstr ="out of memory" ; +break ;default : errstr ="unknown error" ;break ;
Signature · 1 line
2.40.0
tools/libs/light/libxl_cpuid.c static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list, · line 89
Quoted 8 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +4
-0@@ -81,6 +83,14 @@ static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list,return *list + i; } +static int search_feature (const void *a,const void *b) +{ +const char *key = a; +const char *feat = ((struct {const char *n; } *)b)->n;
The cast should be (const struct { const char *n; } *) to not drop the const.
Thanks, Roger.
tools/libs/light/libxl_cpuid.c void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list) · line 56
Quoted 6 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +1
-1@@ -51,7 +53,7 @@ void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)* Used for the static structure describing all features. */ struct cpuid_flags { -char * name; +const char * name;
Nit: Would you mind also moving * to its designated position at this occasion?
tools/libs/light/libxl_cpuid.c static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list, · line 89
Quoted 8 lines
tools/libs/light/libxl_cpuid.c +4
-0@@ -81,6 +83,14 @@ static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list,return *list + i; } +static int search_feature (const void *a,const void *b) +{ +const char *key = a; +const char *feat = ((struct {const char *n; } *)b)->n;
Urgh - what a cast. There's no connection at all between this and struct feature_name in libxl_cpuid_parse_config(). I think you want to move that type declaration out of the function, to also use it here. But if not, comments on both sides are going to be necessary imo.
tools/libs/light/libxl_cpuid.c static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list, · line 91
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+return strncmp (key, feat,strlen (key)) ?:strlen (key) -strlen (feat);
Why not simply
return strcmp(key, feat);
? Both names are nul-terminated, first and foremost because otherwise using strlen() wouldn't be appropriate either.
Quoted 14 lines
tools/libs/light/libxl_cpuid.c +10
-0@@ -342,6 +214,28 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)if (!strncmp (str, flag->name, sep - str) && flag->name[sep - str] ==0 )break ; } +if (flag->name ==NULL ) { +const struct feature_name *feat; +/* Provide a NUL terminated feature name to the search helper. */ +char *name =strndup (str, sep - str); + +if (name ==NULL ) +return 4 ; + + feat =bsearch (name, features,ARRAY_SIZE (features),sizeof (features[0 ]), + search_feature);
tools/libs/light/libxl_cpuid.c int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str) · line 236
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+free (name); + +if (feat !=NULL ) { + f.name = feat->name; + f.leaf = feature_to_cpuid[feat->bit /32 ].leaf; + f.subleaf = feature_to_cpuid[feat->bit /32 ].subleaf; + f.reg = feature_to_cpuid[feat->bit /32 ].reg; + f.bit = feat->bit %32 ; + f.length =1 , + flag = &f;
Nit: Too deep indentation throughout here.
Jan
Quoted 9 lines · Roger Pau Monne
The current implementation in
libxl_cpuid_parse_config()requires keeping a list of cpuid feature bits that should be mostly in sync with the contents ofcpufeatureset.h.Avoid such duplication by using the automatically generated list of cpuid features in
INIT_FEATURE_NAMESin order to map feature names to featureset bits, and then translate from featureset bits into cpuid leaf, subleaf, register tuple.
Quoted 7 lines
Note that the full contents of the previous cpuid translation table can't be removed. That's because some feature names allowed by libxl are not described in the featuresets, or because naming has diverged and the previous nomenclature is preserved for compatibility reasons.
Should result in no functional change observed by callers, albeit some new cpuid features will be available as a result of the change.
I've looked at the removed lists, and some cpuid flag name might be missing with this patch.
When looking in "libxl_cpuid.c" and tools/include/xen/lib/x86/cpuid-autogen.h (INIT_FEATURE_NAMES), I found that some flags removed from libxl_cpuid.c seems to be missing with this patch:
"sse4_1"
"sse4_2"
"tsc_adjust"
"cmt"I did the same with the list removed from the man page, and those flags are missing (some were probably also missing before, so probably not a problem:
"avx512ifma"
"avx512vbmi"
"cmt"
"sse4_1"
"sse4_2"
"tsc_adjust"So, at least for the first list, is it a problem? Or did I failed to check them properly?
(It seems that "cmt" or "tsc_adjust" for example comes from libvirt, 90b2a267c19c ("libxl: add more cpuid flags handling"))
Quoted 9 lines
While there constify
cpuid_flagsname field.Signed-off-by Roger Pau Monné <[email protected]>---
docs/man/xl.cfg.5.pod.in +0
-0
docs/man/xl.cfg.5.pod.in proccount procpkg stepping · line 2014
Quoted 6 lines
docs/man/xl.cfg.5.pod.in +2
-1@@ -2010,24 +2010,12 @@ proccount procpkg stepping =back -List of keys taking a character: +List of keys taking a character can be found in the public header file +L<arch-x86/cpufeatureset.h|http://xenbits.xen.org/docs/unstable/hypercall/x86_64/include,public,arch-x86,cpufeatureset.h.html>
https:// ;-)
And this probably want to be "xenbits.xenproject.org"
Also, I think there's maybe an issue with this link. Maybe someone can assume that "TM1" takes a character, but that flags I think would get rejected, issue with upper case vs lower case. Then, if we deal with the casing, we still have feature like "AVX512_IFMA", which would only be recognize if written "avx512-ifma", so issue with "-" vs "_".
Quoted 5 lines
tools/libs/light/libxl_cpuid.c +0
-0
tools/libs/light/libxl_cpuid.c int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str) · line 223
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +7
-0@@ -342,6 +214,28 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)if (!strncmp (str, flag->name, sep - str) && flag->name[sep - str] ==0 )break ; } +if (flag->name ==NULL ) { +const struct feature_name *feat; +/* Provide a NUL terminated feature name to the search helper. */ +char *name =strndup (str, sep - str); + +if (name ==NULL ) +return 4 ;
out-of-memory are usually fatal in libxl. Any reason to use strndup instead of libxl__strndup? I guess we don't have any libxl_ctx, so we can't use the libxl function.
So, instead of returning an arbitrary integer that isn't returned yet by the function, could you return ERROR_NOMEM?
I wonder if it would be possible to use a buffer on the stack instead, but it might not be worth the effort to find the right size.
Thanks,
Signature · 1 line
Anthony PERARD
Quoted 21 lines · Anthony PERARD
Quoted 9 lines · Roger Pau Monne
The current implementation in
libxl_cpuid_parse_config()requires keeping a list of cpuid feature bits that should be mostly in sync with the contents ofcpufeatureset.h.Avoid such duplication by using the automatically generated list of cpuid features in
INIT_FEATURE_NAMESin order to map feature names to featureset bits, and then translate from featureset bits into cpuid leaf, subleaf, register tuple.Quoted 7 lines
Note that the full contents of the previous cpuid translation table can't be removed. That's because some feature names allowed by libxl are not described in the featuresets, or because naming has diverged and the previous nomenclature is preserved for compatibility reasons.
Should result in no functional change observed by callers, albeit some new cpuid features will be available as a result of the change.
I've looked at the removed lists, and some cpuid flag name might be missing with this patch.
Quoted 8 lines
When looking in "
libxl_cpuid.c" andtools/include/xen/lib/x86/cpuid-autogen.h(INIT_FEATURE_NAMES), I found that some flags removed fromlibxl_cpuid.cseems to be missing with this patch:"sse4_1" "sse4_2" "tsc_adjust" "cmt"
Hm, I got confused with the _ vs - notation, and with cmt I wrongly deleted it I guess.
Quoted 5 lines
I did the same with the list removed from the man page, and those flags are missing (some were probably also missing before, so probably not a problem:
"avx512ifma" "avx512vbmi"
Those two where already missing, so I'm not planning to add those.
Quoted 4 lines
"cmt"
"sse4_1"
"sse4_2"
"tsc_adjust"Those are an oversight and will be added back to cpuid_flags array.
Quoted 24 lines
So, at least for the first list, is it a problem? Or did I failed to check them properly?
(It seems that "cmt" or "tsc_adjust" for example comes from libvirt,90b2a267c19c("libxl: add more cpuid flags handling"))Quoted 9 lines
While there constify
cpuid_flagsname field.Signed-off-by Roger Pau Monné <[email protected]>---
docs/man/xl.cfg.5.pod.in +0
-0docs/man/xl.cfg.5.pod.in proccount procpkg stepping · line 2014
Quoted 6 lines
docs/man/xl.cfg.5.pod.in +2
-1@@ -2010,24 +2010,12 @@ proccount procpkg stepping =back -List of keys taking a character: +List of keys taking a character can be found in the public header file +L<arch-x86/cpufeatureset.h|http://xenbits.xen.org/docs/unstable/hypercall/x86_64/include,public,arch-x86,cpufeatureset.h.html>https:// ;-)
Quoted 7 lines
And this probably want to be "xenbits.xenproject.org"
Also, I think there's maybe an issue with this link. Maybe someone can assume that "TM1" takes a character, but that flags I think would get rejected, issue with upper case vs lower case. Then, if we deal with the casing, we still have feature like "
AVX512_IFMA", which would only be recognize if written "avx512-ifma", so issue with "-" vs "_".
Right, it's not perfect, but I don't see much better way if we want to keep the documentation in sync.
Quoted 11 lines
Quoted 11 lines
tools/libs/light/libxl_cpuid.c +3
-0@@ -342,6 +214,28 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)if (!strncmp (str, flag->name, sep - str) && flag->name[sep - str] ==0 )break ; } +if (flag->name ==NULL ) { +const struct feature_name *feat; +/* Provide a NUL terminated feature name to the search helper. */
Quoted 8 lines
tools/libs/light/libxl_cpuid.c int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str) · line 223
Quoted 4 lines
tools/libs/light/libxl_cpuid.c +4
-0+char *name =strndup (str, sep - str); + +if (name ==NULL ) +return 4 ;out-of-memory are usually fatal in libxl. Any reason to use
strndupinstead oflibxl__strndup? I guess we don't have anylibxl_ctx, so we can't use the libxl function.
Yes, even NO_GC requires a CTX.
Quoted 2 lines
So, instead of returning an arbitrary integer that isn't returned yet by the function, could you return
ERROR_NOMEM?
That would be my preference, but the function already returns (arbitrary) integers as error codes, so I'm not sure whether ERROR_NOMEM won't alias with one of the existing error codes.
Quoted 2 lines
I wonder if it would be possible to use a buffer on the stack instead, but it might not be worth the effort to find the right size.
Hm, I could in theory try to do that, as feature names tend to be small, however I think the implementation would be fragile, as the length of the feature name is based on user input (iow: we would do an alloca() using a user provided size).
Thanks, Roger.
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Anthony PERARD
docs/man/xl.cfg.5.pod.in proccount procpkg stepping · line 2014
Quoted 11 lines · Roger Pau Monne
docs/man/xl.cfg.5.pod.in +2
-1@@ -2010,24 +2010,12 @@ proccount procpkg stepping =back -List of keys taking a character: +List of keys taking a character can be found in the public header file +L<arch-x86/cpufeatureset.h|http://xenbits.xen.org/docs/unstable/hypercall/x86_64/include,public,arch-x86,cpufeatureset.h.html>https:// ;-)
And this probably want to be "xenbits.xenproject.org"
Quoted 8 lines
Quoted 5 lines
Also, I think there's maybe an issue with this link. Maybe someone can assume that "TM1" takes a character, but that flags I think would get rejected, issue with upper case vs lower case. Then, if we deal with the casing, we still have feature like "
AVX512_IFMA", which would only be recognize if written "avx512-ifma", so issue with "-" vs "_".Right, it's not perfect, but I don't see much better way if we want to keep the documentation in sync.
I guess we can start with the change you proposed.
Then, maybe adding that the keys needs to be lower case with dash instead of underscore might be helpful in the man page.
And if we really feel generous, we could just handle both syntax, and have the function apply the necessary transformation, lower case and s/_/-/.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
tools/libs/light/libxl_cpuid.c +1
-0@@ -342,6 +214,28 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)if (!strncmp (str, flag->name, sep - str) && flag->name[sep - str] ==0 )break ; } +if (flag->name ==NULL ) {Quoted 8 lines
tools/libs/light/libxl_cpuid.c int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str) · line 223
Quoted 6 lines
tools/libs/light/libxl_cpuid.c +6
-0+const struct feature_name *feat; +/* Provide a NUL terminated feature name to the search helper. */ +char *name =strndup (str, sep - str); + +if (name ==NULL ) +return 4 ;So, instead of returning an arbitrary integer that isn't returned yet by the function, could you return
ERROR_NOMEM?
Quoted 3 lines
That would be my preference, but the function already returns (arbitrary) integers as error codes, so I'm not sure whether
ERROR_NOMEMwon't alias with one of the existing error codes.
All libxl error code are negative, so we should be fine.
Cheers,
Signature · 1 line
Anthony PERARD