mshv: Extend create partition ioctl to support cpu features
Subsystems (2)
PATCH v4 1/1 mshv: Extend create partition ioctl to support cpu features
From: Muminul Islam <[email protected]>
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new mshv_create_partition_v2 structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.
Introduce a new flag MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES which enables the new structure. If unset, the original mshv_create_partition struct is used, with the old behavior of enabling all features.
---
Changes in v4:
- Change BIT() to BIT_ULL() [Michael Kelley]
- Enforce pt_num_cpu_fbanks == MSHV_NUM_CPU_FEATURES_BANKS and expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few places
Changes in v3:
- Remove the new cpu features definitions in hvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]
Changes in v2:
- Fix exposure of CONFIG_X86_64 to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]
--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)
drivers/hv/mshv_root_main.c +92 -21
@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ;if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK ) || args.pt_isolation >=MSHV_PT_ISOLATION_COUNT )return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ; + +if (args.pt_num_cpu_fbanks !=MSHV_NUM_CPU_FEATURES_BANKS || +mshv_field_nonzero (args, pt_rsvd) || +mshv_field_nonzero (args, pt_rsvd1)) +return -EINVAL ; + +/* +* Note this assumes MSHV_NUM_CPU_FEATURES_BANKS will never +* change and equals HV_PARTITION_PROCESSOR_FEATURES_BANKS +* (i.e. 2). +* +* Further banks (index >= 2) will be modifiable as 'early' +* properties via the set partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] = args.pt_cpu_fbanks[i]; + +#if IS_ENABLED (CONFIG_X86_64 ) + disabled_xsave->as_uint64 = args.pt_disabled_xsave; +#else +/* +* In practice this field is ignored on arm64, but safer to +* zero it in case it is ever used. +*/ + disabled_xsave->as_uint64 =0 ; + +if (mshv_field_nonzero (args, pt_rsvd2)) +return -EINVAL ; +#endif + }else { +/* +* v1 behavior: try to enable everything. The hypervisor will +* disable features that are not supported. The banks can be +* queried via the get partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =0 ; + + disabled_xsave->as_uint64 =0 ; + } +/* Only support EXO partitions */ - creation_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | -HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + *pt_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | +HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_LAPIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_X2APIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_GPA_SUPER_PAGES )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_LAPIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_X2APIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; -if (args.pt_flags &BIT (MSHV_PT_BIT_GPA_SUPER_PAGES )) - creation_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; + isol_props->as_uint64 =0 ;switch (args.pt_isolation) {case MSHV_PT_ISOLATION_NONE : - isolation_properties.isolation_type = -HV_PARTITION_ISOLATION_TYPE_NONE ; + isol_props->isolation_type =HV_PARTITION_ISOLATION_TYPE_NONE ;break ; } +return 0 ; +} + +static long +mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +{ +u64 creation_flags; +struct hv_partition_creation_properties creation_properties; +union hv_partition_isolation_properties isolation_properties; +struct mshv_partition *partition; +struct file *file; +int fd; +long ret; + + ret =mshv_ioctl_process_pt_flags (user_arg, &creation_flags, + &creation_properties, + &isolation_properties); +if (ret) +return ret; + partition =kzalloc (sizeof (*partition),GFP_KERNEL );if (!partition)return -ENOMEM ;
include/uapi/linux/mshv.h +34 -0
@@ -26,6 +26,7 @@ enum {MSHV_PT_BIT_LAPIC ,MSHV_PT_BIT_X2APIC ,MSHV_PT_BIT_GPA_SUPER_PAGES , +MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES , MSHV_PT_BIT_COUNT, };
@@ -41,6 +42,8 @@ enum {* @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* * @pt_isolation: MSHV_PT_ISOLATION_* * +* This is the initial/v1 version for backward compatibility. +* * Returns a file descriptor to act as a handle to a guest partition. * At this point the partition is not yet initialized in the hypervisor. * Some operations must be done with the partition in this state, e.g. setting
@@ -52,6 +55,37 @@ struct mshv_create_partition {__u64 pt_isolation; }; +#define MSHV_NUM_CPU_FEATURES_BANKS 2 + +/** +* struct mshv_create_partition_v2 +* +* This is extended version of the above initial MSHV_CREATE_PARTITION +* ioctl and allows for following additional parameters: +* +* @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. +* @pt_cpu_fbanks: Disabled processor feature banks array. +* @pt_disabled_xsave: Disabled xsave feature bits. +* +* pt_cpu_fbanks and pt_disabled_xsave are passed through as-is to the create +* partition hypercall. +* +* Returns : same as above original mshv_create_partition +*/ +struct mshv_create_partition_v2 { +__u64 pt_flags; +__u64 pt_isolation; +__u16 pt_num_cpu_fbanks; +__u8 pt_rsvd[6 ];/* MBZ */ +__u64 pt_cpu_fbanks[MSHV_NUM_CPU_FEATURES_BANKS ]; +__u64 pt_rsvd1[2 ];/* MBZ */ +#if defined(__x86_64__) +__u64 pt_disabled_xsave; +#else +__u64 pt_rsvd2;/* MBZ */ +#endif +} __packed; +/* /dev/mshv */ #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_partition)
Signature · 1 line
2.34.1
From: Nuno Das Neves <[email protected]> Sent: Tuesday, November 11, 2025 3:20 PM
Quoted 85 lines
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +13
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ; if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||
drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1930
Quoted 10 lines
drivers/hv/mshv_root_main.c +7
-0args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ;
There's subtle issue here that I didn't notice previously. This second copy_from_user() re-populates the first two fields of the "args" local variable. These two fields were validated by code a few lines above. But there's no guarantee that a second read of user space will get the same values. User space could have another thread that changes the user space values between the two copy_from_user() calls, and thereby sneak in some bogus values to be used further down in this function. Because of this risk, there's a general rule for kernel code, which is to avoid multiple accesses to the same user space values. There are places in the kernel where such double reads would be an exploitable security hole.
The fix would be to validate the pt_flags and pt_isolation fields again, or to have the second copy_from_user copy only the additional fields. But it's also the case that the way the pt_flags and pt_isolation fields are used further down in this function, nothing bad can happen if malicious user space should succeed in sneaking in some bogus values.
Net, as currently coded, there's nothing that needs to be fixed. It would be more robust to do one of the two fixes, if for no other reason than to acknowledge awareness of the risk of reading user space twice. But I'm not going to insist on a respin.
Quoted 153 lines
drivers/hv/mshv_root_main.c +72
-10+ +if (args.pt_num_cpu_fbanks !=MSHV_NUM_CPU_FEATURES_BANKS || +mshv_field_nonzero (args, pt_rsvd) || +mshv_field_nonzero (args, pt_rsvd1)) +return -EINVAL ; + +/* +* Note this assumes MSHV_NUM_CPU_FEATURES_BANKS will never +* change and equals HV_PARTITION_PROCESSOR_FEATURES_BANKS +* (i.e. 2). +* +* Further banks (index >= 2) will be modifiable as 'early' +* properties via the set partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] = args.pt_cpu_fbanks[i]; + +#if IS_ENABLED (CONFIG_X86_64 ) + disabled_xsave->as_uint64 = args.pt_disabled_xsave; +#else +/* +* In practice this field is ignored on arm64, but safer to +* zero it in case it is ever used. +*/ + disabled_xsave->as_uint64 =0 ; + +if (mshv_field_nonzero (args, pt_rsvd2)) +return -EINVAL ; +#endif + }else { +/* +* v1 behavior: try to enable everything. The hypervisor will +* disable features that are not supported. The banks can be +* queried via the get partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =0 ; + + disabled_xsave->as_uint64 =0 ; + } +/* Only support EXO partitions */ - creation_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | -HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + *pt_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | +HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_LAPIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_X2APIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_GPA_SUPER_PAGES )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_LAPIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_X2APIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; -if (args.pt_flags &BIT (MSHV_PT_BIT_GPA_SUPER_PAGES )) - creation_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; + isol_props->as_uint64 =0 ;switch (args.pt_isolation) {case MSHV_PT_ISOLATION_NONE : - isolation_properties.isolation_type = -HV_PARTITION_ISOLATION_TYPE_NONE ; + isol_props->isolation_type =HV_PARTITION_ISOLATION_TYPE_NONE ;break ; } +return 0 ; +} + +static long +mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +{ +u64 creation_flags; +struct hv_partition_creation_properties creation_properties; +union hv_partition_isolation_properties isolation_properties; +struct mshv_partition *partition; +struct file *file; +int fd; +long ret; + + ret =mshv_ioctl_process_pt_flags (user_arg, &creation_flags, + &creation_properties, + &isolation_properties); +if (ret) +return ret; + partition =kzalloc (sizeof (*partition),GFP_KERNEL );if (!partition)return -ENOMEM ;include/uapi/linux/mshv.h +34
-1@@ -26,6 +26,7 @@ enum {MSHV_PT_BIT_LAPIC ,MSHV_PT_BIT_X2APIC ,MSHV_PT_BIT_GPA_SUPER_PAGES , +MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES , MSHV_PT_BIT_COUNT, };@@ -41,6 +42,8 @@ enum {* @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* * @pt_isolation: MSHV_PT_ISOLATION_* * +* This is the initial/v1 version for backward compatibility. +* * Returns a file descriptor to act as a handle to a guest partition. * At this point the partition is not yet initialized in the hypervisor. * Some operations must be done with the partition in this state, e.g. setting @@ -52,6 +55,37 @@ struct mshv_create_partition {__u64 pt_isolation; }; +#define MSHV_NUM_CPU_FEATURES_BANKS 2 + +/** +* struct mshv_create_partition_v2 +* +* This is extended version of the above initial MSHV_CREATE_PARTITION +* ioctl and allows for following additional parameters: +* +* @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. +* @pt_cpu_fbanks: Disabled processor feature banks array. +* @pt_disabled_xsave: Disabled xsave feature bits. +* +* pt_cpu_fbanks and pt_disabled_xsave are passed through as-is to the create +* partition hypercall. +* +* Returns : same as above original mshv_create_partition +*/ +struct mshv_create_partition_v2 { +__u64 pt_flags; +__u64 pt_isolation; +__u16 pt_num_cpu_fbanks; +__u8 pt_rsvd[6 ];/* MBZ */ +__u64 pt_cpu_fbanks[MSHV_NUM_CPU_FEATURES_BANKS ]; +__u64 pt_rsvd1[2 ];/* MBZ */ +#if defined(__x86_64__) +__u64 pt_disabled_xsave; +#else +__u64 pt_rsvd2;/* MBZ */ +#endif +} __packed; +/* /dev/mshv */ #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_partition) --
Quoted 1 line
2.34.1
Other than the double read of user space, LGTM.
Wei Liu
maintainerQuoted 113 lines · Michael Kelley
From: Nuno Das Neves <[email protected]> Sent: Tuesday, November 11, 2025 3:20 PM
Quoted 85 lines
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +13
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ; if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1930
Quoted 10 lines
drivers/hv/mshv_root_main.c +7
-0args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ;There's subtle issue here that I didn't notice previously. This second
copy_from_user()re-populates the first two fields of the "args" local variable. These two fields were validated by code a few lines above. But there's no guarantee that a second read of user space will get the same values. User space could have another thread that changes the user space values between the twocopy_from_user()calls, and thereby sneak in some bogus values to be used further down in this function. Because of this risk, there's a general rule for kernel code, which is to avoid multiple accesses to the same user space values. There are places in the kernel where such double reads would be an exploitable security hole.The fix would be to validate the
pt_flagsandpt_isolationfields again, or to have the secondcopy_from_usercopy only the additional fields. But it's also the case that the way thept_flagsandpt_isolationfields are used further down in this function, nothing bad can happen if malicious user space should succeed in sneaking in some bogus values.
Quoted 4 lines
Net, as currently coded, there's nothing that needs to be fixed. It would be more robust to do one of the two fixes, if for no other reason than to acknowledge awareness of the risk of reading user space twice. But I'm not going to insist on a respin.
Nuno, I can commit this patch first. If you can post a diff later I can squash it in.
/* Re-validate fields after the second copy_from_user */
if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||
args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)
return -EINVAL;
Perhaps something like this after the second copy_from_user()?
Quoted 3 lines
Quoted 1 line
Other than the double read of user space, LGTM.
Reviewed-by Michael Kelley <[email protected]>
Thank you for the detailed review!
Wei
Quoted 100 lines · Wei Liu
Quoted 99 lines · Michael Kelley
From: Nuno Das Neves <[email protected]> Sent: Tuesday, November 11, 2025 3:20 PM
Quoted 85 lines
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +13
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ; if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1930
Quoted 10 lines
drivers/hv/mshv_root_main.c +7
-0args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ;There's subtle issue here that I didn't notice previously. This second
copy_from_user()re-populates the first two fields of the "args" local variable. These two fields were
Quoted 8 lines
Quoted 8 lines
validated by code a few lines above. But there's no guarantee that a second read of user space will get the same values. User space could have another thread that changes the user space values between the two
copy_from_user()calls, and thereby sneak in some bogus values to be used further down in this function. Because of this risk, there's a general rule for kernel code, which is to avoid multiple accesses to the same user space values. There are places in the kernel where such double reads would be an exploitable security hole.
Good catch Michael! It's something I had read about once before long ago but had forgotten. I wonder if there's some kind of automation that could warn about potential issues - i.e. copy_from_user() on the same pointer twice.
Quoted 6 lines
Quoted 6 lines
The fix would be to validate the
pt_flagsandpt_isolationfields again, or to have the secondcopy_from_usercopy only the additional fields. But it's also the case that the way thept_flagsandpt_isolationfields are used further down in this function, nothing bad can happen if malicious user space should succeed in sneaking in some bogus values.
Quoted 7 lines
Quoted 4 lines
Net, as currently coded, there's nothing that needs to be fixed. It would be more robust to do one of the two fixes, if for no other reason than to acknowledge awareness of the risk of reading user space twice. But I'm not going to insist on a respin.
Nuno, I can commit this patch first. If you can post a diff later I can squash it in.
It might be easier if I just spin a v5 today? I'll send it soon.
Quoted 8 lines
/* Re-validate fields after the second copy_from_user */if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||
args.pt_isolation >=MSHV_PT_ISOLATION_COUNT)
return-EINVAL;Perhaps something like this after the second
copy_from_user()?
Yes, that sounds fine. I thought about just copying the second part of the struct but re-checking those fields looks like a simpler and less error-prone way to me.
Nuno
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Other than the double read of user space, LGTM.
Reviewed-by Michael Kelley <[email protected]>Thank you for the detailed review!
Wei
Wei Liu
maintainerQuoted 125 lines · Nuno Das Neves
Quoted 100 lines · Wei Liu
Quoted 99 lines · Michael Kelley
From: Nuno Das Neves <[email protected]> Sent: Tuesday, November 11, 2025 3:20 PM
Quoted 85 lines
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +13
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ; if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK) ||drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1930
Quoted 10 lines
drivers/hv/mshv_root_main.c +7
-0args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ;There's subtle issue here that I didn't notice previously. This second
copy_from_user()re-populates the first two fields of the "args" local variable. These two fields wereQuoted 8 lines
Quoted 8 lines
validated by code a few lines above. But there's no guarantee that a second read of user space will get the same values. User space could have another thread that changes the user space values between the two
copy_from_user()calls, and thereby sneak in some bogus values to be used further down in this function. Because of this risk, there's a general rule for kernel code, which is to avoid multiple accesses to the same user space values. There are places in the kernel where such double reads would be an exploitable security hole.Good catch Michael! It's something I had read about once before long ago but had forgotten. I wonder if there's some kind of automation that could warn about potential issues - i.e.
copy_from_user()on the same pointer twice.Quoted 11 lines
Quoted 10 lines
The fix would be to validate the
pt_flagsandpt_isolationfields again, or to have the secondcopy_from_usercopy only the additional fields. But it's also the case that the way thept_flagsandpt_isolationfields are used further down in this function, nothing bad can happen if malicious user space should succeed in sneaking in some bogus values.Net, as currently coded, there's nothing that needs to be fixed. It would be more robust to do one of the two fixes, if for no other reason than to acknowledge awareness of the risk of reading user space twice. But I'm not going to insist on a respin.
Quoted 4 lines
Quoted 2 lines
Nuno, I can commit this patch first. If you can post a diff later I can squash it in.
It might be easier if I just spin a v5 today? I'll send it soon.
Yes, please resend. Thanks.
Wei
Quoted 111 lines · Nuno Das Neves
From: Muminul Islam <[email protected]>
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +35
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ;if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK ) || args.pt_isolation >=MSHV_PT_ISOLATION_COUNT )return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ; + +if (args.pt_num_cpu_fbanks !=MSHV_NUM_CPU_FEATURES_BANKS || +mshv_field_nonzero (args, pt_rsvd) || +mshv_field_nonzero (args, pt_rsvd1)) +return -EINVAL ; + +/* +* Note this assumes MSHV_NUM_CPU_FEATURES_BANKS will never +* change and equals HV_PARTITION_PROCESSOR_FEATURES_BANKS +* (i.e. 2). +* +* Further banks (index >= 2) will be modifiable as 'early' +* properties via the set partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++)
drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1955
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+ disabled_procs->as_uint64[i] = args.pt_cpu_fbanks[i]; + +#if IS_ENABLED (CONFIG_X86_64 ) + disabled_xsave->as_uint64 = args.pt_disabled_xsave; +#else +/* +* In practice this field is ignored on arm64, but safer to +* zero it in case it is ever used. +*/ + disabled_xsave->as_uint64 =0 ;
Why not explicitly treat non-zero value as invalid here instead? Isn't it always better than implicitly (and silently) zeroing it?
Quoted 26 lines
drivers/hv/mshv_root_main.c +23
-2+ +if (mshv_field_nonzero (args, pt_rsvd2)) +return -EINVAL ; +#endif + }else { +/* +* v1 behavior: try to enable everything. The hypervisor will +* disable features that are not supported. The banks can be +* queried via the get partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =0 ; + + disabled_xsave->as_uint64 =0 ; + } +/* Only support EXO partitions */ - creation_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | -HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + *pt_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | +HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_LAPIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_X2APIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ;
drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1983
Quoted 10 lines
drivers/hv/mshv_root_main.c +3
-6+if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_GPA_SUPER_PAGES )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_LAPIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_X2APIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; -if (args.pt_flags &BIT (MSHV_PT_BIT_GPA_SUPER_PAGES )) - creation_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; + isol_props->as_uint64 =0 ;
These properties are missing the nested one.
I'd recommend squeezing that change in this patch
Thanks,
Stanislav
Quoted 93 lines
drivers/hv/mshv_root_main.c +21
-2switch (args.pt_isolation) {case MSHV_PT_ISOLATION_NONE : - isolation_properties.isolation_type = -HV_PARTITION_ISOLATION_TYPE_NONE ; + isol_props->isolation_type =HV_PARTITION_ISOLATION_TYPE_NONE ;break ; } +return 0 ; +} + +static long +mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +{ +u64 creation_flags; +struct hv_partition_creation_properties creation_properties; +union hv_partition_isolation_properties isolation_properties; +struct mshv_partition *partition; +struct file *file; +int fd; +long ret; + + ret =mshv_ioctl_process_pt_flags (user_arg, &creation_flags, + &creation_properties, + &isolation_properties); +if (ret) +return ret; + partition =kzalloc (sizeof (*partition),GFP_KERNEL );if (!partition)return -ENOMEM ;include/uapi/linux/mshv.h +34
-0@@ -26,6 +26,7 @@ enum {MSHV_PT_BIT_LAPIC ,MSHV_PT_BIT_X2APIC ,MSHV_PT_BIT_GPA_SUPER_PAGES , +MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES , MSHV_PT_BIT_COUNT, };@@ -41,6 +42,8 @@ enum {* @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* * @pt_isolation: MSHV_PT_ISOLATION_* * +* This is the initial/v1 version for backward compatibility. +* * Returns a file descriptor to act as a handle to a guest partition. * At this point the partition is not yet initialized in the hypervisor. * Some operations must be done with the partition in this state, e.g. setting @@ -52,6 +55,37 @@ struct mshv_create_partition {__u64 pt_isolation; }; +#define MSHV_NUM_CPU_FEATURES_BANKS 2 + +/** +* struct mshv_create_partition_v2 +* +* This is extended version of the above initial MSHV_CREATE_PARTITION +* ioctl and allows for following additional parameters: +* +* @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. +* @pt_cpu_fbanks: Disabled processor feature banks array. +* @pt_disabled_xsave: Disabled xsave feature bits. +* +* pt_cpu_fbanks and pt_disabled_xsave are passed through as-is to the create +* partition hypercall. +* +* Returns : same as above original mshv_create_partition +*/ +struct mshv_create_partition_v2 { +__u64 pt_flags; +__u64 pt_isolation; +__u16 pt_num_cpu_fbanks; +__u8 pt_rsvd[6 ];/* MBZ */ +__u64 pt_cpu_fbanks[MSHV_NUM_CPU_FEATURES_BANKS ]; +__u64 pt_rsvd1[2 ];/* MBZ */ +#if defined(__x86_64__) +__u64 pt_disabled_xsave; +#else +__u64 pt_rsvd2;/* MBZ */ +#endif +} __packed; +/* /dev/mshv */ #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_partition)Signature · 1 line
2.34.1
Quoted 117 lines · Stanislav Kinsburskii
Quoted 116 lines · Nuno Das Neves
From: Muminul Islam <[email protected]>
The existing mshv create partition ioctl does not provide a way to specify which cpu features are enabled in the guest. Instead, it attempts to enable all features and those that are not supported are silently disabled by the hypervisor.
This was done to reduce unnecessary complexity and is sufficient for many cases. However, new scenarios require fine-grained control over these features.
Define a new
mshv_create_partition_v2structure which supports passing the disabled processor and xsave feature bits through to the create partition hypercall directly.Introduce a new flag
MSHV_PT_BIT_CPU_AND_XSAVE_FEATURESwhich enables the new structure. If unset, the originalmshv_create_partitionstruct is used, with the old behavior of enabling all features.Co-developed-by Jinank Jain <[email protected]>Signed-off-by Jinank Jain <[email protected]>Signed-off-by Muminul Islam <[email protected]>Signed-off-by Nuno Das Neves <[email protected]>---
Changes in v4:
- ChangeBIT()toBIT_ULL()[Michael Kelley]
- Enforcept_num_cpu_fbanks==MSHV_NUM_CPU_FEATURES_BANKSand expect that number to never change. In future, additional processor banks will be settable as 'early' partition properties. Remove redundant code that set default values for unspecified banks [Michael Kelley]
- Set xsave features to 0 on arm64 [Michael Kelley]
- Add clarifying comments in a few placesChanges in v3:
- Remove the new cpu features definitions inhvhdk.h, and retain the old behavior of enabling all features for the old struct. For the v2 struct, still disable unspecified feature banks, since that makes it robust to future extensions.
- Amend comments and commit message to reflect the above
- Fix unused variable on arm64 [kernel test robot]Changes in v2:
- Fix exposure ofCONFIG_X86_64to uapi [kernel test robot]
- Fix compilation issue on arm64 [kernel test robot]--- drivers/hv/mshv_root_main.c | 113 +++++++++++++++++++++++++++++------- include/uapi/linux/mshv.h | 34 +++++++++++ 2 files changed, 126 insertions(+), 21 deletions(-)drivers/hv/mshv_root_main.c +40
-11@@ -1900,43 +1900,114 @@ add_partition(struct mshv_partition *partition)return 0 ; } -static long -mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +static_assert (MSHV_NUM_CPU_FEATURES_BANKS == +HV_PARTITION_PROCESSOR_FEATURES_BANKS ); + +static long mshv_ioctl_process_pt_flags (void __user *user_arg,u64 *pt_flags, +struct hv_partition_creation_properties *cr_props, +union hv_partition_isolation_properties *isol_props) { -struct mshv_create_partition args; -u64 creation_flags; -struct hv_partition_creation_properties creation_properties = {}; -union hv_partition_isolation_properties isolation_properties = {}; -struct mshv_partition *partition; -struct file *file; -int fd; -long ret; +int i; +struct mshv_create_partition_v2 args; +union hv_partition_processor_features *disabled_procs; +union hv_partition_processor_xsave_features *disabled_xsave; -if (copy_from_user (&args, user_arg,sizeof (args))) +/* First, copy v1 struct in case user is on previous versions */ +if (copy_from_user (&args, user_arg, +sizeof (struct mshv_create_partition )))return -EFAULT ;if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK ) || args.pt_isolation >=MSHV_PT_ISOLATION_COUNT )return -EINVAL ; + disabled_procs = &cr_props->disabled_processor_features; + disabled_xsave = &cr_props->disabled_processor_xsave_features; + +/* Check if user provided newer struct with feature fields */ +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +if (copy_from_user (&args, user_arg,sizeof (args))) +return -EFAULT ; + +if (args.pt_num_cpu_fbanks !=MSHV_NUM_CPU_FEATURES_BANKS || +mshv_field_nonzero (args, pt_rsvd) || +mshv_field_nonzero (args, pt_rsvd1)) +return -EINVAL ; + +/* +* Note this assumes MSHV_NUM_CPU_FEATURES_BANKS will never +* change and equals HV_PARTITION_PROCESSOR_FEATURES_BANKS +* (i.e. 2). +* +* Further banks (index >= 2) will be modifiable as 'early' +* properties via the set partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] = args.pt_cpu_fbanks[i]; + +#if IS_ENABLED (CONFIG_X86_64 ) + disabled_xsave->as_uint64 = args.pt_disabled_xsave; +#else
Quoted 9 lines
drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1955
Quoted 5 lines
drivers/hv/mshv_root_main.c +5
-0+/* +* In practice this field is ignored on arm64, but safer to +* zero it in case it is ever used. +*/ + disabled_xsave->as_uint64 =0 ;Why not explicitly treat non-zero value as invalid here instead? Isn't it always better than implicitly (and silently) zeroing it?
This is setting the hypercall input field to zero, because it looks like the hypervisor ignores it on arm64. That's not ideal but it isn't under our control here so all we can do is zero it.
The ioctl argument which we *do* control lacks the xsave field on ARM64, instead we have a reserved field which is checked below (pt_rsvd2).
Quoted 29 lines
Quoted 29 lines
drivers/hv/mshv_root_main.c +25
-2+ +if (mshv_field_nonzero (args, pt_rsvd2)) +return -EINVAL ; +#endif + }else { +/* +* v1 behavior: try to enable everything. The hypervisor will +* disable features that are not supported. The banks can be +* queried via the get partition property hypercall. +*/ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =0 ; + + disabled_xsave->as_uint64 =0 ; + } +/* Only support EXO partitions */ - creation_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | -HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + *pt_flags =HV_PARTITION_CREATION_FLAG_EXO_PARTITION | +HV_PARTITION_CREATION_FLAG_INTERCEPT_MESSAGE_PAGE_ENABLED ; + +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_LAPIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_X2APIC )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; +if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_GPA_SUPER_PAGES )) + *pt_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ;
Quoted 11 lines
drivers/hv/mshv_root_main.c add_partition(struct mshv_partition *partition) · line 1983
Quoted 7 lines
drivers/hv/mshv_root_main.c +1
-6-if (args.pt_flags &BIT (MSHV_PT_BIT_LAPIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_LAPIC_ENABLED ; -if (args.pt_flags &BIT (MSHV_PT_BIT_X2APIC )) - creation_flags |=HV_PARTITION_CREATION_FLAG_X2APIC_CAPABLE ; -if (args.pt_flags &BIT (MSHV_PT_BIT_GPA_SUPER_PAGES )) - creation_flags |=HV_PARTITION_CREATION_FLAG_GPA_SUPER_PAGES_ENABLED ; + isol_props->as_uint64 =0 ;These properties are missing the nested one.
I'd recommend squeezing that change in this patch
The nested flag is not introduced yet. It should be in a separate patch.
FYI, there is a v5 of this patch I posted on November 13.
Nuno
Quoted 96 lines
Thanks,
StanislavQuoted 93 lines
drivers/hv/mshv_root_main.c +21
-2switch (args.pt_isolation) {case MSHV_PT_ISOLATION_NONE : - isolation_properties.isolation_type = -HV_PARTITION_ISOLATION_TYPE_NONE ; + isol_props->isolation_type =HV_PARTITION_ISOLATION_TYPE_NONE ;break ; } +return 0 ; +} + +static long +mshv_ioctl_create_partition (void __user *user_arg,struct device *module_dev) +{ +u64 creation_flags; +struct hv_partition_creation_properties creation_properties; +union hv_partition_isolation_properties isolation_properties; +struct mshv_partition *partition; +struct file *file; +int fd; +long ret; + + ret =mshv_ioctl_process_pt_flags (user_arg, &creation_flags, + &creation_properties, + &isolation_properties); +if (ret) +return ret; + partition =kzalloc (sizeof (*partition),GFP_KERNEL );if (!partition)return -ENOMEM ;include/uapi/linux/mshv.h +34
-0@@ -26,6 +26,7 @@ enum {MSHV_PT_BIT_LAPIC ,MSHV_PT_BIT_X2APIC ,MSHV_PT_BIT_GPA_SUPER_PAGES , +MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES , MSHV_PT_BIT_COUNT, };@@ -41,6 +42,8 @@ enum {* @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* * @pt_isolation: MSHV_PT_ISOLATION_* * +* This is the initial/v1 version for backward compatibility. +* * Returns a file descriptor to act as a handle to a guest partition. * At this point the partition is not yet initialized in the hypervisor. * Some operations must be done with the partition in this state, e.g. setting @@ -52,6 +55,37 @@ struct mshv_create_partition {__u64 pt_isolation; }; +#define MSHV_NUM_CPU_FEATURES_BANKS 2 + +/** +* struct mshv_create_partition_v2 +* +* This is extended version of the above initial MSHV_CREATE_PARTITION +* ioctl and allows for following additional parameters: +* +* @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. +* @pt_cpu_fbanks: Disabled processor feature banks array. +* @pt_disabled_xsave: Disabled xsave feature bits. +* +* pt_cpu_fbanks and pt_disabled_xsave are passed through as-is to the create +* partition hypercall. +* +* Returns : same as above original mshv_create_partition +*/ +struct mshv_create_partition_v2 { +__u64 pt_flags; +__u64 pt_isolation; +__u16 pt_num_cpu_fbanks; +__u8 pt_rsvd[6 ];/* MBZ */ +__u64 pt_cpu_fbanks[MSHV_NUM_CPU_FEATURES_BANKS ]; +__u64 pt_rsvd1[2 ];/* MBZ */ +#if defined(__x86_64__) +__u64 pt_disabled_xsave; +#else +__u64 pt_rsvd2;/* MBZ */ +#endif +} __packed; +/* /dev/mshv */ #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_partition)Signature · 1 line
2.34.1