KVM: x86: Add a cap to disable NX hugepages on a VM
Subsystems (5)
v5 cover KVM: x86: Add a cap to disable NX hugepages on a VM
Given the high cost of NX hugepages in terms of TLB performance, it may be desirable to disable the mitigation on a per-VM basis. In the case of public cloud providers with many VMs on a single host, some VMs may be more trusted than others. In order to maximize performance on critical VMs, while still providing some protection to the host from iTLB Multihit, allow the mitigation to be selectively disabled.
Disabling NX hugepages on a VM is relatively straightforward, but I took this as an opportunity to add some NX hugepages test coverage and clean up selftests infrastructure a bit.
This series was tested with the new selftest and the rest of the KVM selftests on an Intel Haswell machine.
The following tests failed, but I do not believe that has anything to do with this series:
userspace_io_test
vmx_nested_tsc_scaling_test
vmx_preemption_timer_test
Changelog:v1->v2:
Dropped the complicated memslot refactor in favor of Ricardo Koller's patch with a similar effect.
Incorporated David Dunn's feedback and reviewed by tag: shortened waits to speed up test.v2->v3:
Incorporated a suggestion from David on how to build the NX huge pages test.
Fixed a build breakage identified by David.
Dropped the per-vm nx_huge_pages field in favor of simply checking the global + per-VM disable override.
Documented the new capability
Separated out the commit to test disabling NX huge pages Removed permission check when checking if the disable NX capability is supported.
Added test coverage for the permission check.v3->v4:
Collected RB's from Jing and David
Modified stat collection to reduce a memory allocation [David] Incorporated various improvments to the NX test [David] Changed the NX disable test to run by default [David] Removed some now unnecessary commits
Dropped the code to dump KVM stats from the binary stats test, and factor out parts of the existing test to library functions instead. [David, Jing, Sean]
Dropped the improvement to a debugging log message as it's no longer relevant to this series.v4->v5:
Incorporated cleanup suggestions from David and Sean
Added a patch with style fixes for the binary stats test from Sean Added a restriction that NX huge pages can only be disabled before vCPUs are created [Sean]
Ben Gardon (9):
KVM: selftests: Remove dynamic memory allocation for stats header
KVM: selftests: Read binary stats header in lib
KVM: selftests: Read binary stats desc in lib
KVM: selftests: Read binary stat data in lib
KVM: selftests: Add NX huge pages test
KVM: x86: Fix errant brace in KVM capability handling
KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis
KVM: selftests: Factor out calculation of pages needed for a VM
KVM: selftests: Test disabling NX hugepages on a VM
Sean Christopherson (1):
KVM: selftests: Clean up coding style in binary stats test
Documentation/virt/kvm/api.rst | 13 + arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/mmu.h | 9 +- arch/x86/kvm/mmu/spte.c | 7 +- arch/x86/kvm/mmu/spte.h | 3 +- arch/x86/kvm/mmu/tdp_mmu.c | 3 +- arch/x86/kvm/x86.c | 25 +- include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/Makefile | 10 + .../selftests/kvm/include/kvm_util_base.h | 13 + .../selftests/kvm/kvm_binary_stats_test.c | 142 ++++++----- tools/testing/selftests/kvm/lib/kvm_util.c | 232 ++++++++++++++++-- .../selftests/kvm/x86_64/nx_huge_pages_test.c | 206 ++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 25 ++ 14 files changed, 597 insertions(+), 94 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c create mode 100755 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 1/10 KVM: selftests: Remove dynamic memory allocation for stats header
There's no need to allocate dynamic memory for the stats header since its size is known at compile time.
--- .../selftests/kvm/kvm_binary_stats_test.c | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +27 -31
@@ -26,56 +26,53 @@ static void stats_test(int stats_fd)int i;size_t size_desc;size_t size_data =0 ; -struct kvm_stats_header *header; +struct kvm_stats_header header;char *id;struct kvm_stats_desc *stats_desc;u64 *stats_data;struct kvm_stats_desc *pdesc;/* Read kvm stats header */ - header =malloc (sizeof (*header)); -TEST_ASSERT (header,"Allocate memory for stats header" ); - - ret =read (stats_fd, header,sizeof (*header)); -TEST_ASSERT (ret ==sizeof (*header),"Read stats header" ); - size_desc =sizeof (*stats_desc) + header->name_size; + ret =read (stats_fd, &header,sizeof (header)); +TEST_ASSERT (ret ==sizeof (header),"Read stats header" ); + size_desc =sizeof (*stats_desc) + header.name_size;/* Read kvm stats id string */ - id =malloc (header->name_size); + id =malloc (header.name_size);TEST_ASSERT (id,"Allocate memory for id string" ); - ret =read (stats_fd, id, header->name_size); -TEST_ASSERT (ret == header->name_size,"Read id string" ); + ret =read (stats_fd, id, header.name_size); +TEST_ASSERT (ret == header.name_size,"Read id string" );/* Check id string, that should start with "kvm" */ -TEST_ASSERT (!strncmp (id,"kvm" ,3 ) &&strlen (id) < header->name_size, +TEST_ASSERT (!strncmp (id,"kvm" ,3 ) &&strlen (id) < header.name_size,"Invalid KVM stats type, id: %s" , id);/* Sanity check for other fields in header */ -if (header->num_desc ==0 ) { +if (header.num_desc ==0 ) {printf ("No KVM stats defined!" );return ; }/* Check overlap */ -TEST_ASSERT (header->desc_offset >0 && header->data_offset >0 - && header->desc_offset >=sizeof (*header) - && header->data_offset >=sizeof (*header), +TEST_ASSERT (header.desc_offset >0 && header.data_offset >0 + && header.desc_offset >=sizeof (header) + && header.data_offset >=sizeof (header),"Invalid offset fields in header" ); -TEST_ASSERT (header->desc_offset > header->data_offset || - (header->desc_offset + size_desc * header->num_desc <= - header->data_offset), +TEST_ASSERT (header.desc_offset > header.data_offset || + (header.desc_offset + size_desc * header.num_desc <= + header.data_offset),"Descriptor block is overlapped with data block" );/* Allocate memory for stats descriptors */ - stats_desc =calloc (header->num_desc, size_desc); + stats_desc =calloc (header.num_desc, size_desc);TEST_ASSERT (stats_desc,"Allocate memory for stats descriptors" );/* Read kvm stats descriptors */ ret =pread (stats_fd, stats_desc, - size_desc * header->num_desc, header->desc_offset); -TEST_ASSERT (ret == size_desc * header->num_desc, + size_desc * header.num_desc, header.desc_offset); +TEST_ASSERT (ret == size_desc * header.num_desc,"Read KVM stats descriptors" );/* Sanity check for fields in descriptors */ -for (i =0 ; i < header->num_desc; ++i) { +for (i =0 ; i < header.num_desc; ++i) { pdesc = (void *)stats_desc + i * size_desc;/* Check type,unit,base boundaries */ TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
@@ -104,7 +101,7 @@ static void stats_test(int stats_fd)break ; }/* Check name string */ -TEST_ASSERT (strlen (pdesc->name) < header->name_size, +TEST_ASSERT (strlen (pdesc->name) < header.name_size,"KVM stats name(%s) too long" , pdesc->name);/* Check size field, which should not be zero */ TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
@@ -124,14 +121,14 @@ static void stats_test(int stats_fd) size_data += pdesc->size *sizeof (*stats_data); }/* Check overlap */ -TEST_ASSERT (header->data_offset >= header->desc_offset - || header->data_offset + size_data <= header->desc_offset, +TEST_ASSERT (header.data_offset >= header.desc_offset + || header.data_offset + size_data <= header.desc_offset,"Data block is overlapped with Descriptor block" );/* Check validity of all stats data size */ -TEST_ASSERT (size_data >= header->num_desc *sizeof (*stats_data), +TEST_ASSERT (size_data >= header.num_desc *sizeof (*stats_data),"Data size is not correct" );/* Check stats offset */ -for (i =0 ; i < header->num_desc; ++i) { +for (i =0 ; i < header.num_desc; ++i) { pdesc = (void *)stats_desc + i * size_desc; TEST_ASSERT(pdesc->offset < size_data, "Invalid offset (%u) for stats: %s",
@@ -142,15 +139,15 @@ static void stats_test(int stats_fd) stats_data =malloc (size_data);TEST_ASSERT (stats_data,"Allocate memory for stats data" );/* Read kvm stats data as a bulk */ - ret =pread (stats_fd, stats_data, size_data, header->data_offset); + ret =pread (stats_fd, stats_data, size_data, header.data_offset);TEST_ASSERT (ret == size_data,"Read KVM stats data" );/* Read kvm stats data one by one */ size_data =0 ; -for (i =0 ; i < header->num_desc; ++i) { +for (i =0 ; i < header.num_desc; ++i) { pdesc = (void *)stats_desc + i * size_desc; ret =pread (stats_fd, stats_data, pdesc->size *sizeof (*stats_data), - header->data_offset + size_data); + header.data_offset + size_data);TEST_ASSERT (ret == pdesc->size *sizeof (*stats_data),"Read data of KVM stats: %s" , pdesc->name); size_data += pdesc->size *sizeof (*stats_data);
@@ -159,7 +156,6 @@ static void stats_test(int stats_fd)free (stats_data);free (stats_desc);free (id); -free (header); }
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 2/10 KVM: selftests: Read binary stats header in lib
In reply to Ben GardonMove the code to read the binary stats header to the KVM selftests library. It will be re-used by other tests to check KVM behavior.
No functional change intended.
--- .../selftests/kvm/include/kvm_util_base.h | 1 + .../selftests/kvm/kvm_binary_stats_test.c | 4 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
@@ -400,6 +400,7 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);int vm_get_stats_fd (struct kvm_vm *vm);int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid); +void read_stats_header (int stats_fd,struct kvm_stats_header *header);uint32_t guest_get_vcpuid (void );
tools/testing/selftests/kvm/kvm_binary_stats_test.c +2 -2
@@ -33,8 +33,8 @@ static void stats_test(int stats_fd)struct kvm_stats_desc *pdesc;/* Read kvm stats header */ - ret =read (stats_fd, &header,sizeof (header)); -TEST_ASSERT (ret ==sizeof (header),"Read stats header" ); +read_stats_header (stats_fd, &header); + size_desc =sizeof (*stats_desc) + header.name_size;/* Read kvm stats id string */
tools/testing/selftests/kvm/lib/kvm_util.c +21 -0
@@ -2556,3 +2556,24 @@ int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid)return ioctl (vcpu->fd,KVM_GET_STATS_FD ,NULL ); } + +/* +* Read binary stats header +* +* Input Args: +* stats_fd - the file descriptor for the binary stats file from which to read +* +* Output Args: +* header - a binary stats metadata header to be filled with data +* +* Return: void +* +* Read a header for the binary stats interface. +*/ +void read_stats_header (int stats_fd,struct kvm_stats_header *header) +{ +ssize_t ret; + + ret =read (stats_fd, header,sizeof (*header)); +TEST_ASSERT (ret ==sizeof (*header),"Read stats header" ); +}
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 3/10 KVM: selftests: Read binary stats desc in lib
In reply to Ben GardonMove the code to read the binary stats descriptors to the KVM selftests library. It will be re-used by other tests to check KVM behavior.
No functional change intended.
--- .../selftests/kvm/include/kvm_util_base.h | 2 + .../selftests/kvm/kvm_binary_stats_test.c | 8 +--- tools/testing/selftests/kvm/lib/kvm_util.c | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
@@ -401,6 +401,8 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);int vm_get_stats_fd (struct kvm_vm *vm);int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid);void read_stats_header (int stats_fd,struct kvm_stats_header *header); +struct kvm_stats_desc *read_stats_desc (int stats_fd, +struct kvm_stats_header *header);uint32_t guest_get_vcpuid (void );
tools/testing/selftests/kvm/kvm_binary_stats_test.c +1 -7
@@ -62,14 +62,8 @@ static void stats_test(int stats_fd) header.data_offset), "Descriptor block is overlapped with data block"); -/* Allocate memory for stats descriptors */ - stats_desc =calloc (header.num_desc, size_desc); -TEST_ASSERT (stats_desc,"Allocate memory for stats descriptors" );/* Read kvm stats descriptors */ - ret =pread (stats_fd, stats_desc, - size_desc * header.num_desc, header.desc_offset); -TEST_ASSERT (ret == size_desc * header.num_desc, -"Read KVM stats descriptors" ); + stats_desc =read_stats_desc (stats_fd, &header);/* Sanity check for fields in descriptors */ for (i =0 ; i < header.num_desc; ++i) {
tools/testing/selftests/kvm/lib/kvm_util.c +38 -0
@@ -2577,3 +2577,41 @@ void read_stats_header(int stats_fd, struct kvm_stats_header *header) ret =read (stats_fd, header,sizeof (*header));TEST_ASSERT (ret ==sizeof (*header),"Read stats header" ); } + +static ssize_t stats_descs_size (struct kvm_stats_header *header) +{ +return header->num_desc * + (sizeof (struct kvm_stats_desc ) + header->name_size); +} + +/* +* Read binary stats descriptors +* +* Input Args: +* stats_fd - the file descriptor for the binary stats file from which to read +* header - the binary stats metadata header corresponding to the given FD +* +* Output Args: None +* +* Return: +* A pointer to a newly allocated series of stat descriptors. +* Caller is responsible for freeing the returned kvm_stats_desc. +* +* Read the stats descriptors from the binary stats interface. +*/ +struct kvm_stats_desc *read_stats_desc (int stats_fd, +struct kvm_stats_header *header) +{ +struct kvm_stats_desc *stats_desc; +ssize_t ret; + + stats_desc =malloc (stats_descs_size (header)); +TEST_ASSERT (stats_desc,"Allocate memory for stats descriptors" ); + + ret =pread (stats_fd, stats_desc,stats_descs_size (header), + header->desc_offset); +TEST_ASSERT (ret ==stats_descs_size (header), +"Read KVM stats descriptors" ); + +return stats_desc; +}
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 4/10 KVM: selftests: Clean up coding style in binary stats test
In reply to Ben GardonFrom: Sean Christopherson <[email protected]>
Fix a variety of code style violations and/or inconsistencies in the binary stats test. The 80 char limit is a soft limit and can and should be ignored/violated if doing so improves the overall code readability.
Specifically, provide consistent indentation and don't split expressions at arbitrary points just to honor the 80 char limit.
Opportunistically expand/add comments to call out the more subtle aspects of the code.
--- .../selftests/kvm/kvm_binary_stats_test.c | 91 ++++++++++++------- 1 file changed, 56 insertions(+), 35 deletions(-)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +56 -35
@@ -35,47 +35,64 @@ static void stats_test(int stats_fd)/* Read kvm stats header */ read_stats_header (stats_fd, &header); +/* +* The base size of the descriptor is defined by KVM's ABI, but the +* size of the name field is variable as far as KVM's ABI is concerned. +* But, the size of name is constant for a given instance of KVM and +* is provided by KVM in the overall stats header. +*/ size_desc =sizeof (*stats_desc) + header.name_size;/* Read kvm stats id string */ id =malloc (header.name_size);TEST_ASSERT (id,"Allocate memory for id string" ); + ret =read (stats_fd, id, header.name_size);TEST_ASSERT (ret == header.name_size,"Read id string" );/* Check id string, that should start with "kvm" */ TEST_ASSERT (!strncmp (id,"kvm" ,3 ) &&strlen (id) < header.name_size, -"Invalid KVM stats type, id: %s" , id); +"Invalid KVM stats type, id: %s" , id);/* Sanity check for other fields in header */ if (header.num_desc ==0 ) {printf ("No KVM stats defined!" );return ; } -/* Check overlap */ -TEST_ASSERT (header.desc_offset >0 && header.data_offset >0 - && header.desc_offset >=sizeof (header) - && header.data_offset >=sizeof (header), -"Invalid offset fields in header" ); +/* +* The descriptor and data offsets must be valid, they must not overlap +* the header, and the descriptor and data blocks must not overlap each +* other. Note, the data block is rechecked after its size is known. +*/ +TEST_ASSERT (header.desc_offset && header.desc_offset >=sizeof (header) && + header.data_offset && header.data_offset >=sizeof (header), +"Invalid offset fields in header" ); +TEST_ASSERT (header.desc_offset > header.data_offset || - (header.desc_offset + size_desc * header.num_desc <= - header.data_offset), -"Descriptor block is overlapped with data block" ); + (header.desc_offset + size_desc * header.num_desc <= header.data_offset), +"Descriptor block is overlapped with data block" );/* Read kvm stats descriptors */ stats_desc =read_stats_desc (stats_fd, &header);/* Sanity check for fields in descriptors */ for (i =0 ; i < header.num_desc; ++i) { +/* +* Note, size_desc includes the of the name field, which is +* variable, i.e. this is NOT equivalent to &stats_desc[i]. +*/ pdesc = (void *)stats_desc + i * size_desc; -/* Check type,unit,base boundaries */ -TEST_ASSERT ((pdesc->flags &KVM_STATS_TYPE_MASK ) - <=KVM_STATS_TYPE_MAX ,"Unknown KVM stats type" ); -TEST_ASSERT ((pdesc->flags &KVM_STATS_UNIT_MASK ) - <=KVM_STATS_UNIT_MAX ,"Unknown KVM stats unit" ); -TEST_ASSERT ((pdesc->flags &KVM_STATS_BASE_MASK ) - <=KVM_STATS_BASE_MAX ,"Unknown KVM stats base" ); -/* Check exponent for stats unit + +/* Check type, unit, and base boundaries */ +TEST_ASSERT ((pdesc->flags &KVM_STATS_TYPE_MASK ) <=KVM_STATS_TYPE_MAX , +"Unknown KVM stats type" ); +TEST_ASSERT ((pdesc->flags &KVM_STATS_UNIT_MASK ) <=KVM_STATS_UNIT_MAX , +"Unknown KVM stats unit" ); +TEST_ASSERT ((pdesc->flags &KVM_STATS_BASE_MASK ) <=KVM_STATS_BASE_MAX , +"Unknown KVM stats base" ); + +/* +* Check exponent for stats unit * Exponent for counter should be greater than or equal to 0 * Exponent for unit bytes should be greater than or equal to 0 * Exponent for unit seconds should be less than or equal to 0
@@ -86,47 +103,51 @@ static void stats_test(int stats_fd)case KVM_STATS_UNIT_NONE :case KVM_STATS_UNIT_BYTES :case KVM_STATS_UNIT_CYCLES : -TEST_ASSERT (pdesc->exponent >=0 , -"Unsupported KVM stats unit" ); +TEST_ASSERT (pdesc->exponent >=0 ,"Unsupported KVM stats unit" );break ;case KVM_STATS_UNIT_SECONDS : -TEST_ASSERT (pdesc->exponent <=0 , -"Unsupported KVM stats unit" ); +TEST_ASSERT (pdesc->exponent <=0 ,"Unsupported KVM stats unit" );break ; }/* Check name string */ TEST_ASSERT (strlen (pdesc->name) < header.name_size, -"KVM stats name(%s) too long" , pdesc->name); +"KVM stats name(%s) too long" , pdesc->name);/* Check size field, which should not be zero */ -TEST_ASSERT (pdesc->size,"KVM descriptor(%s) with size of 0" , - pdesc->name); +TEST_ASSERT (pdesc->size, +"KVM descriptor(%s) with size of 0" , pdesc->name);/* Check bucket_size field */ switch (pdesc->flags &KVM_STATS_TYPE_MASK ) {case KVM_STATS_TYPE_LINEAR_HIST :TEST_ASSERT (pdesc->bucket_size, -"Bucket size of Linear Histogram stats (%s) is zero" , - pdesc->name); +"Bucket size of Linear Histogram stats (%s) is zero" , + pdesc->name);break ;default :TEST_ASSERT (!pdesc->bucket_size, -"Bucket size of stats (%s) is not zero" , - pdesc->name); +"Bucket size of stats (%s) is not zero" , + pdesc->name); } size_data += pdesc->size *sizeof (*stats_data); } -/* Check overlap */ -TEST_ASSERT (header.data_offset >= header.desc_offset - || header.data_offset + size_data <= header.desc_offset, -"Data block is overlapped with Descriptor block" ); + +/* +* Now that the size of the data block is known, verify the data block +* doesn't overlap the descriptor block. +*/ +TEST_ASSERT (header.data_offset >= header.desc_offset || + header.data_offset + size_data <= header.desc_offset, +"Data block is overlapped with Descriptor block" ); +/* Check validity of all stats data size */ TEST_ASSERT (size_data >= header.num_desc *sizeof (*stats_data), -"Data size is not correct" ); +"Data size is not correct" ); +/* Check stats offset */ for (i =0 ; i < header.num_desc; ++i) { pdesc = (void *)stats_desc + i * size_desc;TEST_ASSERT (pdesc->offset < size_data, -"Invalid offset (%u) for stats: %s" , - pdesc->offset, pdesc->name); +"Invalid offset (%u) for stats: %s" , + pdesc->offset, pdesc->name); }/* Allocate memory for stats data */
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 5/10 KVM: selftests: Read binary stat data in lib
In reply to Ben GardonMove the code to read the binary stats data to the KVM selftests library. It will be re-used by other tests to check KVM behavior.
No functional change intended.
--- .../selftests/kvm/include/kvm_util_base.h | 3 ++ .../selftests/kvm/kvm_binary_stats_test.c | 7 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
@@ -403,6 +403,9 @@ int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);void read_stats_header (int stats_fd,struct kvm_stats_header *header);struct kvm_stats_desc *read_stats_desc (int stats_fd,struct kvm_stats_header *header); +int read_stat_data (int stats_fd,struct kvm_stats_header *header, +struct kvm_stats_desc *desc,uint64_t *data, +ssize_t max_elements);uint32_t guest_get_vcpuid (void );
tools/testing/selftests/kvm/kvm_binary_stats_test.c +2 -5
@@ -160,11 +160,8 @@ static void stats_test(int stats_fd) size_data =0 ;for (i =0 ; i < header.num_desc; ++i) { pdesc = (void *)stats_desc + i * size_desc; - ret =pread (stats_fd, stats_data, - pdesc->size *sizeof (*stats_data), - header.data_offset + size_data); -TEST_ASSERT (ret == pdesc->size *sizeof (*stats_data), -"Read data of KVM stats: %s" , pdesc->name); +read_stat_data (stats_fd, &header, pdesc, stats_data, + pdesc->size); size_data += pdesc->size *sizeof (*stats_data); }
tools/testing/selftests/kvm/lib/kvm_util.c +36 -0
@@ -2615,3 +2615,39 @@ struct kvm_stats_desc *read_stats_desc(int stats_fd,return stats_desc; } + +/* +* Read stat data for a particular stat +* +* Input Args: +* stats_fd - the file descriptor for the binary stats file from which to read +* header - the binary stats metadata header corresponding to the given FD +* desc - the binary stat metadata for the particular stat to be read +* max_elements - the maximum number of 8-byte values to read into data +* +* Output Args: +* data - the buffer into which stat data should be read +* +* Return: +* The number of data elements read into data or -ERRNO on error. +* +* Read the data values of a specified stat from the binary stats interface. +*/ +int read_stat_data (int stats_fd,struct kvm_stats_header *header, +struct kvm_stats_desc *desc,uint64_t *data, +ssize_t max_elements) +{ +ssize_t size =min ((ssize_t )desc->size, max_elements); +ssize_t ret; + + ret =pread (stats_fd, data, size *sizeof (*data), + header->data_offset + desc->offset); + +/* ret from pread is in bytes. */ + ret = ret /sizeof (*data); + +TEST_ASSERT (ret == size, +"Read data of KVM stats: %s" , desc->name); + +return ret; +}
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 6/10 KVM: selftests: Add NX huge pages test
In reply to Ben GardonThere's currently no test coverage of NX hugepages in KVM selftests, so add a basic test to ensure that the feature works as intended.
--- tools/testing/selftests/kvm/Makefile | 10 ++ .../selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 62 +++++++ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 166 ++++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 25 +++ 5 files changed, 264 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c create mode 100755 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh
tools/testing/selftests/kvm/Makefile +10 -0
@@ -43,6 +43,10 @@ LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handler LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c LIBKVM_riscv = lib/riscv/processor.c lib/riscv/ucall.c +# Non-compiled test targets +TEST_PROGS_x86_64 += x86_64/nx_huge_pages_test.sh + +# Compiled test targets TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features
@@ -104,6 +108,9 @@ TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test +# Compiled outputs used by test targets +TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test + TEST_GEN_PROGS_aarch64 += aarch64/arch_timer TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
@@ -142,7 +149,9 @@ TEST_GEN_PROGS_riscv += kvm_page_table_test TEST_GEN_PROGS_riscv += set_memory_region_test TEST_GEN_PROGS_riscv += kvm_binary_stats_test +TEST_PROGS += $(TEST_PROGS_$(UNAME_M)) TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M)) +TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(UNAME_M)) LIBKVM += $(LIBKVM_$(UNAME_M)) INSTALL_HDR_PATH = $(top_srcdir)/usr
@@ -193,6 +202,7 @@ $(OUTPUT)/libkvm.a: $(LIBKVM_OBJS) x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) all: $(STATIC_LIBS) $(TEST_GEN_PROGS): $(STATIC_LIBS) +$(TEST_GEN_PROGS_EXTENDED): $(STATIC_LIBS) cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib .. cscope:
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
@@ -406,6 +406,7 @@ struct kvm_stats_desc *read_stats_desc(int stats_fd,int read_stat_data (int stats_fd,struct kvm_stats_header *header,struct kvm_stats_desc *desc,uint64_t *data,ssize_t max_elements); +uint64_t vm_get_stat (struct kvm_vm *vm,const char *stat_name);uint32_t guest_get_vcpuid (void );
tools/testing/selftests/kvm/lib/kvm_util.c +62 -0
65 lines not on this page · raw message
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file +166 -0
166 lines not on this page · raw message
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +25 -0
@@ -0,0 +1,25 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only */ + +# tools/testing/selftests/kvm/nx_huge_page_test.sh +# Copyright (C) 2022, Google LLC. + +NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages) +NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio) +NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms) +HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages) + +echo 1 > /sys/module/kvm/parameters/nx_huge_pages +echo 1 > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo 100 > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo 200 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +./nx_huge_pages_test +RET=$? + +echo $NX_HUGE_PAGES > /sys/module/kvm/parameters/nx_huge_pages +echo $NX_HUGE_PAGES_RECOVERY_RATIO > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo $NX_HUGE_PAGES_RECOVERY_PERIOD > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo $HUGE_PAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +exit $RET
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
Sean Christopherson
maintainerRe: [PATCH v5 06/10] KVM: selftests: Add NX huge pages test
Quoted 2 lines · Ben Gardon
There's currently no test coverage of NX hugepages in KVM selftests, so add a basic test to ensure that the feature works as intended.
...
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c line 6
Quoted 12 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file +6
-0@@ -0,0 +1,166 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* tools/testing/selftests/kvm/nx_huge_page_test.c +* +* Usage: to be run via nx_huge_page_test.sh, which does the necessary +* environment setup and teardown
It would be really nice if this test could either (a) do something useful without having to manually set /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages, or (b) refuse to run unless it's (likely) been invoked by the script. E.g. maybe add a magic token that must be passed in? That way just running the bare test will provide a helpful skip message, but someone that wants to fiddle with it can still run it manually.
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c line 87
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file +5
-0+int main (int argc,char **argv) +{ +struct kvm_vm *vm; +struct timespec ts; +void *hva;
This needs to check if the workaround is actually enabled via module param. Not as big a deal if there's a magic number, but it's also not too hard to query a module param. Or at least, it shouldn't be, I'm fairly certain that's one of the things I want to address in the selftests overhaul.
Aha! Actually, IIUC, the patch that validates the per-VM override adds full support for the module param being turned off.
So, how about pull in the tweaks to the expected number to this patch, and then the per-VM override test just makes disable_nx a logical OR of the module param beyond off or the test using the per-VM override.
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh line 5
Quoted 11 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +5
-0@@ -0,0 +1,25 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only */ + +# tools/testing/selftests/kvm/nx_huge_page_test.sh +# Copyright (C) 2022, Google LLC.
This should either check for root or use sudo.
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh line 17
Quoted 11 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +11
-0+NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages) +NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio) +NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms) +HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages) + +echo 1 > /sys/module/kvm/parameters/nx_huge_pages +echo 1 > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo 100 > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo 200 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +./nx_huge_pages_test
I would much prefer this find its path and use that to reference the test, e.g. this fails if invoking the script from anything but the x86_64 subdirectory. I'd provide a snippet of how to do that, but my scripting skills are garbage :-)
Quoted 11 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +8
-0+RET=$? + +echo $NX_HUGE_PAGES > /sys/module/kvm/parameters/nx_huge_pages +echo $NX_HUGE_PAGES_RECOVERY_RATIO > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo $NX_HUGE_PAGES_RECOVERY_PERIOD > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo $HUGE_PAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +exit $RETSignature · 1 line
2.35.1.1178.g4f1659d476-goog
Re: [PATCH v5 06/10] KVM: selftests: Add NX huge pages test
Quoted 45 lines · Sean Christopherson
Quoted 2 lines · Ben Gardon
There's currently no test coverage of NX hugepages in KVM selftests, so add a basic test to ensure that the feature works as intended.
...
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c line 6
Quoted 12 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file +6
-0@@ -0,0 +1,166 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* tools/testing/selftests/kvm/nx_huge_page_test.c +* +* Usage: to be run via nx_huge_page_test.sh, which does the necessary +* environment setup and teardown It would be really nice if this test could either (a) do something useful without having to manually set
/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages, or (b) refuse to run unless it's (likely) been invoked by the script. E.g. maybe add a magic token that must be passed in? That way just running the bare test will provide a helpful skip message, but someone that wants to fiddle with it can still run it manually.tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c line 87
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file +5
-0+int main (int argc,char **argv) +{ +struct kvm_vm *vm; +struct timespec ts; +void *hva;This needs to check if the workaround is actually enabled via module param. Not as big a deal if there's a magic number, but it's also not too hard to query a module param. Or at least, it shouldn't be, I'm fairly certain that's one of the things I want to address in the selftests overhaul.
Aha! Actually, IIUC, the patch that validates the per-VM override adds full support for the module param being turned off.
So, how about pull in the tweaks to the expected number to this patch, and then the per-VM override test just makes
disable_nxa logical OR of the module param beyond off or the test using the per-VM override.
Quoted 13 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh line 5
Quoted 11 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +5
-0@@ -0,0 +1,25 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only */ + +# tools/testing/selftests/kvm/nx_huge_page_test.sh +# Copyright (C) 2022, Google LLC.This should either check for root or use sudo.
Is there not any scenario where the below setup commands could work without root?
Quoted 28 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh line 17
Quoted 12 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +11
-0+NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages) +NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio) +NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms) +HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages) + +echo 1 > /sys/module/kvm/parameters/nx_huge_pages +echo 1 > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo 100 > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo 200 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +./nx_huge_pages_testI would much prefer this find its path and use that to reference the test, e.g. this fails if invoking the script from anything but the
x86_64subdirectory. I'd provide a snippet of how to do that, but my scripting skills are garbage :-)Quoted 11 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +8
-1+RET=$? + +echo $NX_HUGE_PAGES > /sys/module/kvm/parameters/nx_huge_pages +echo $NX_HUGE_PAGES_RECOVERY_RATIO > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo $NX_HUGE_PAGES_RECOVERY_PERIOD > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo $HUGE_PAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +exit $RET --2.35.1.1178.g4f1659d476-goog
Sean Christopherson
maintainerRe: [PATCH v5 06/10] KVM: selftests: Add NX huge pages test
Quoted 15 lines · Ben Gardon
Quoted 5 lines · Sean Christopherson
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +0
-0Quoted 8 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh line 5
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file +5
-0@@ -0,0 +1,25 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only */ + +# tools/testing/selftests/kvm/nx_huge_page_test.sh +# Copyright (C) 2022, Google LLC.This should either check for root or use sudo.
Quoted 2 lines
Is there not any scenario where the below setup commands could work without root?
Technically, yes, someone could be running the test as a non-root user with the necessary permissions. Practically speaking, I would be surprised if anyone configures module params in that way.
That said, there's certainly no harm in using sudo as a fall back.
PATCH v5 7/10 KVM: x86: Fix errant brace in KVM capability handling
In reply to Ben GardonThe braces around the KVM_CAP_XSAVE2 block also surround the KVM_CAP_PMU_CAPABILITY block, likely the result of a merge issue. Simply move the curly brace back to where it belongs.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -4382,10 +4382,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)if (r <sizeof (struct kvm_xsave )) r =sizeof (struct kvm_xsave );break ; + }case KVM_CAP_PMU_CAPABILITY : r = enable_pmu ?KVM_CAP_PMU_VALID_MASK :0 ;break ; - }case KVM_CAP_DISABLE_QUIRKS2 : r =KVM_X86_VALID_QUIRKS ;break ;
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 8/10 KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis
In reply to Ben GardonIn some cases, the NX hugepage mitigation for iTLB multihit is not needed for all guests on a host. Allow disabling the mitigation on a per-VM basis to avoid the performance hit of NX hugepages on trusted workloads.
In order to disable NX hugepages on a VM, ensure that the userspace actor has permission to reboot the system. Since disabling NX hugepages would allow a guest to crash the system, it is similar to reboot permissions.
Ideally, KVM would require userspace to prove it has access to KVM's nx_huge_pages module param, e.g. so that userspace can opt out without needing full reboot permissions. But getting access to the module param file info is difficult because it is buried in layers of sysfs and module glue. Requiring CAP_SYS_BOOT is sufficient for all known use cases.
--- Documentation/virt/kvm/api.rst | 13 +++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 9 +++++---- arch/x86/kvm/mmu/spte.c | 7 ++++--- arch/x86/kvm/mmu/spte.h | 3 ++- arch/x86/kvm/mmu/tdp_mmu.c | 3 ++- arch/x86/kvm/x86.c | 23 +++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 8 files changed, 52 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +13 -0
@@ -7855,6 +7855,19 @@ At this time, KVM_PMU_CAP_DISABLE is the only capability. Setting this capability will disable PMU virtualization for that VM. Usermode should adjust CPUID leaf 0xA to reflect that the PMU is disabled. +8.36 KVM_CAP_VM_DISABLE_NX_HUGE_PAGES +--------------------------- + +:Capability KVM_CAP_PMU_CAPABILITY +:Architectures: x86 +:Type: vm +:Returns 0 on success, -EPERM if the userspace process does not + have CAP_SYS_BOOT + +This capability disables the NX huge pages mitigation for iTLB MULTIHIT. + +The capability has no effect if the nx_huge_pages module parameter is not set. + 9. Known KVM API problems =========================
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1240,6 +1240,8 @@ struct kvm_arch {hpa_t hv_root_tdp;spinlock_t hv_root_tdp_lock;#endif + +bool disable_nx_huge_pages; };struct kvm_vm_stat {
arch/x86/kvm/mmu.h +5 -4
@@ -173,9 +173,10 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);extern int nx_huge_pages; -static inline bool is_nx_huge_page_enabled (void ) +static inline bool is_nx_huge_page_enabled (struct kvm *kvm) { -return READ_ONCE (nx_huge_pages); +return READ_ONCE (nx_huge_pages) && + !kvm->arch.disable_nx_huge_pages; } static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
@@ -191,8 +192,8 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .user = err & PFERR_USER_MASK, .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), - .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - + .nx_huge_page_workaround_enabled = + is_nx_huge_page_enabled(vcpu->kvm), .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K,
arch/x86/kvm/mmu/spte.c +4 -3
@@ -116,7 +116,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, spte |=spte_shadow_accessed_mask (spte);if (level >PG_LEVEL_4K && (pte_access &ACC_EXEC_MASK ) && -is_nx_huge_page_enabled ()) { +is_nx_huge_page_enabled (vcpu->kvm)) { pte_access &= ~ACC_EXEC_MASK ; }
@@ -215,7 +215,8 @@ static u64 make_spte_executable(u64 spte)* This is used during huge page splitting to build the SPTEs that make up the * new page table. */ -u64 make_huge_page_split_spte (u64 huge_spte,int huge_level,int index) +u64 make_huge_page_split_spte (struct kvm *kvm,u64 huge_spte,int huge_level, +int index) {u64 child_spte;int child_level;
@@ -243,7 +244,7 @@ u64 make_huge_page_split_spte(u64 huge_spte, int huge_level, int index)* When splitting to a 4K page, mark the page executable as the * NX hugepage mitigation no longer applies. */ -if (is_nx_huge_page_enabled ()) +if (is_nx_huge_page_enabled (kvm)) child_spte =make_spte_executable (child_spte); }
arch/x86/kvm/mmu/spte.h +2 -1
@@ -415,7 +415,8 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, unsigned int pte_access, gfn_t gfn, kvm_pfn_t pfn, u64 old_spte, bool prefetch, bool can_unsync, bool host_writable, u64 *new_spte); -u64 make_huge_page_split_spte (u64 huge_spte,int huge_level,int index); +u64 make_huge_page_split_spte (struct kvm *kvm,u64 huge_spte,int huge_level, +int index);u64 make_nonleaf_spte (u64 *child_pt,bool ad_disabled);u64 make_mmio_spte (struct kvm_vcpu *vcpu,u64 gfn,unsigned int access);u64 mark_spte_for_access_track (u64 spte);
arch/x86/kvm/mmu/tdp_mmu.c +2 -1
@@ -1469,7 +1469,8 @@ static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,* not been linked in yet and thus is not reachable from any other CPU. */ for (i =0 ; i <PT64_ENT_PER_PAGE ; i++) - sp->spt[i] =make_huge_page_split_spte (huge_spte, level, i); + sp->spt[i] =make_huge_page_split_spte (kvm, huge_spte, + level, i);/* * Replace the huge spte with a pointer to the populated lower level
arch/x86/kvm/x86.c +23 -0
@@ -4286,6 +4286,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SYS_ATTRIBUTES :case KVM_CAP_VAPIC :case KVM_CAP_ENABLE_CAP : +case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES : r =1 ;break ;case KVM_CAP_EXIT_HYPERCALL :
@@ -6079,6 +6080,28 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES : + r = -EINVAL ; +if (cap->args[0 ]) +break ; + +/* +* Since the risk of disabling NX hugepages is a guest crashing +* the system, ensure the userspace process has permission to +* reboot the system. +*/ +if (!capable (CAP_SYS_BOOT )) { + r = -EPERM ; +break ; + } + +mutex_lock (&kvm->lock); +if (!kvm->created_vcpus) { + kvm->arch.disable_nx_huge_pages = true; + r =0 ; + } +mutex_unlock (&kvm->lock); +break ;default : r = -EINVAL ;break ;
include/uapi/linux/kvm.h +1 -0
@@ -1148,6 +1148,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_PMU_CAPABILITY 212#define KVM_CAP_DISABLE_QUIRKS2 213#define KVM_CAP_VM_TSC_CONTROL 214 +#define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 215#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
Sean Christopherson
maintainerRe: [PATCH v5 08/10] KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis
Quoted 6 lines · Ben Gardon
Documentation/virt/kvm/api.rst +0
-0@@ -7855,6 +7855,19 @@ At this time, KVM_PMU_CAP_DISABLE is the only capability. Setting this capability will disable PMU virtualization for that VM. Usermode
Documentation/virt/kvm/api.rst At this time, KVM_PMU_CAP_DISABLE is the only capability. Setting · line 7865
Quoted 10 lines
Documentation/virt/kvm/api.rst +8
-0should adjust CPUID leaf 0xA to reflect that the PMU is disabled. +8.36 KVM_CAP_VM_DISABLE_NX_HUGE_PAGES +--------------------------- + +:Capability KVM_CAP_PMU_CAPABILITY +:Architectures: x86 +:Type: vm +:Returns 0 on success, -EPERM if the userspace process does not + have CAP_SYS_BOOT
Needs to document the -EINVAL cases, especially the requirement that this be called before VMs are created. The
Quoted 25 lines
Documentation/virt/kvm/api.rst +4
-0+This capability disables the NX huge pages mitigation for iTLB MULTIHIT. + +The capability has no effect if the nx_huge_pages module parameter is not set. + 9. Known KVM API problems =========================arch/x86/include/asm/kvm_host.h +2
-0@@ -1240,6 +1240,8 @@ struct kvm_arch {hpa_t hv_root_tdp;spinlock_t hv_root_tdp_lock;#endif + +bool disable_nx_huge_pages; };struct kvm_vm_stat {arch/x86/kvm/mmu.h +0
-0
arch/x86/kvm/mmu.h struct kvm_page_fault { · line 179
Quoted 9 lines
arch/x86/kvm/mmu.h +3
-2@@ -173,9 +173,10 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);extern int nx_huge_pages; -static inline bool is_nx_huge_page_enabled (void ) +static inline bool is_nx_huge_page_enabled (struct kvm *kvm) { -return READ_ONCE (nx_huge_pages); +return READ_ONCE (nx_huge_pages) && + !kvm->arch.disable_nx_huge_pages;
No need for a newline, that fits on a single line.
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter, · line 1473
Quoted 11 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-1@@ -1469,7 +1469,8 @@ static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,* not been linked in yet and thus is not reachable from any other CPU. */ for (i =0 ; i <PT64_ENT_PER_PAGE ; i++) - sp->spt[i] =make_huge_page_split_spte (huge_spte, level, i); + sp->spt[i] =make_huge_page_split_spte (kvm, huge_spte, + level, i);
Just let this poke past 80 chars.
Quoted 15 lines
arch/x86/kvm/x86.c +1
-0@@ -4286,6 +4286,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SYS_ATTRIBUTES :case KVM_CAP_VAPIC :case KVM_CAP_ENABLE_CAP : +case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES : r =1 ;break ;case KVM_CAP_EXIT_HYPERCALL :@@ -6079,6 +6080,28 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);
arch/x86/kvm/x86.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6091
Quoted 10 lines
arch/x86/kvm/x86.c +9
-0break ; +case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES : + r = -EINVAL ; +if (cap->args[0 ]) +break ; + +/* +* Since the risk of disabling NX hugepages is a guest crashing +* the system, ensure the userspace process has permission to +* reboot the system.
Since I'm nitpicking already and there's also a comment...
Can you call out that, unlike the actual reboot() syscall, the process needs the capability in the init? namespace (I don't actual know the terminology) because exposing /dev/kvm into a container doesn't magically limit the iTLB multihit bug to that container. I.e. that this _must_ use capable(), not ns_capable().
Amusingly, someone could subvert the selftest's SYS_reboot heuristic by running the test in a container :-)
PATCH v5 9/10 KVM: selftests: Factor out calculation of pages needed for a VM
In reply to Ben GardonFactor out the calculation of the number of pages needed for a VM to make it easier to separate creating the VM and adding vCPUs.
--- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 59 ++++++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +4 -0
@@ -312,6 +312,10 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, vm_paddr_t paddr_min, uint32_t memslot);vm_paddr_t vm_alloc_page_table (struct kvm_vm *vm); +uint64_t vm_pages_needed (enum vm_guest_mode mode,uint32_t nr_vcpus, +uint64_t slot0_mem_pages,uint64_t extra_mem_pages, +uint32_t num_percpu_pages); +/* * Create a VM with reasonable defaults *
tools/testing/selftests/kvm/lib/kvm_util.c +43 -16
88 lines not on this page · raw message
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
PATCH v5 10/10 KVM: selftests: Test disabling NX hugepages on a VM
In reply to Ben GardonAdd an argument to the NX huge pages test to test disabling the feature on a VM using the new capability.
--- .../selftests/kvm/include/kvm_util_base.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 16 ++++- .../selftests/kvm/x86_64/nx_huge_pages_test.c | 62 +++++++++++++++---- 3 files changed, 68 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
@@ -414,4 +414,6 @@ uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name);uint32_t guest_get_vcpuid (void ); +int vm_disable_nx_huge_pages (struct kvm_vm *vm); +#endif /* SELFTEST_KVM_UTIL_BASE_H */
tools/testing/selftests/kvm/lib/kvm_util.c +15 -1
@@ -112,6 +112,11 @@ int vm_check_cap(struct kvm_vm *vm, long cap)return ret; } +static int __vm_enable_cap (struct kvm_vm *vm,struct kvm_enable_cap *cap) +{ +return ioctl (vm->fd,KVM_ENABLE_CAP , cap); +} +/* VM Enable Capability * * Input Args:
@@ -128,7 +133,7 @@ int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap) {int ret; - ret =ioctl (vm->fd,KVM_ENABLE_CAP , cap); + ret =__vm_enable_cap (vm, cap);TEST_ASSERT (ret ==0 ,"KVM_ENABLE_CAP IOCTL failed,\n" " rc: %i errno: %i" , ret, errno);
@@ -2740,3 +2745,12 @@ uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) stat_name, ret);return data; } + +int vm_disable_nx_huge_pages (struct kvm_vm *vm) +{ +struct kvm_enable_cap cap = {0 }; + + cap.cap =KVM_CAP_VM_DISABLE_NX_HUGE_PAGES ; + cap.args[0 ] =0 ; +return __vm_enable_cap (vm, &cap); +}
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +51 -11
108 lines not on this page · raw message
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
Sean Christopherson
maintainerRe: [PATCH v5 10/10] KVM: selftests: Test disabling NX hugepages on a VM
Quoted 14 lines · Ben Gardon
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +2
-0@@ -13,6 +13,8 @@#include <fcntl.h> #include <stdint.h> #include <time.h> +#include <linux/reboot.h> +#include <sys/syscall.h> #include <test_util.h> #include "kvm_util.h"
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c static void check_split_count(struct kvm_vm *vm, int expected_splits) · line 85
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +1
-1@@ -80,13 +82,45 @@ static void check_split_count(struct kvm_vm *vm, int expected_splits) expected_splits, actual_splits); } -int main (int argc,char **argv) +void run_test (bool disable_nx)
Probably worth naming this disable_nx_workaround or disable_nx_mitigation, it's quite easy to think this means "disable EFER.NX".
Quoted 19 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +13
-2{struct kvm_vm *vm;struct timespec ts; +uint64_t pages;void *hva; - - vm =vm_create_default (0 ,0 , guest_code); +int r; + + pages =vm_pages_needed (VM_MODE_DEFAULT ,1 ,DEFAULT_GUEST_PHY_PAGES , +0 ,0 ); + vm =vm_create_without_vcpus (VM_MODE_DEFAULT , pages); + +if (disable_nx) { +kvm_check_cap (KVM_CAP_VM_DISABLE_NX_HUGE_PAGES ); + +/* +* Check if this process has the reboot permissions needed to +* disable NX huge pages on a VM.
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c static void check_split_count(struct kvm_vm *vm, int expected_splits) · line 112
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +10
-0+* +* The reboot call below will never have any effect because +* the magic values are not set correctly, however the +* permission check is done before the magic value check. +*/ + r =syscall (SYS_reboot,0 ,0 ,0 ,NULL ); +if (r && errno ==EPERM ) { + r =vm_disable_nx_huge_pages (vm); +TEST_ASSERT (r ==EPERM , +"This process should not have permission to disable NX huge pages" );
First off, huge kudos for negative testing! But, it's going to provide poor coverage if we teach everyone to use the runner script, because that'll likely require root on most hosts, e.g. to futz with the module param.
Aha! Idea. And it should eliminate the SYS_reboot shenanigans, which while hilarious, are mildy scary.
In the runner script, wrap all the modification of sysfs knobs with sudo, and then (again with sudo) do:
setcap cap_sys_boot+ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test MAGIC_NUMBER -b
where "-b" means "has CAP_SYS_BOOT". And then
setcap cap_sys_boot-ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test MAGIC_NUMBER
Hmm, and I guess if the script is run as root, just skip the second invocation.
Quoted 15 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +11
-0+return ; + } + +TEST_ASSERT (r && errno ==EINVAL , +"Reboot syscall should fail with -EINVAL" ); + + r =vm_disable_nx_huge_pages (vm); +TEST_ASSERT (!r,"Disabling NX huge pages should succeed if process has reboot permissions" ); + } + +vm_vcpu_add_default (vm,0 , guest_code); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB, HPAGE_GPA, HPAGE_SLOT,
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c int main(int argc, char **argv) · line 159
Quoted 7 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +2
-2@@ -121,21 +155,21 @@ int main(int argc, char **argv)* to be remapped at 4k. */ vcpu_run (vm,0 ); -check_2m_page_count (vm,1 ); -check_split_count (vm,1 ); +check_2m_page_count (vm, disable_nx ?2 :1 ); +check_split_count (vm, disable_nx ?0 :1 );
Can you update the comments to explain why these magic number of pages are expected for NX enabled/disabled? As Jim has pointed out, just because KVM and selftests might agree that 1==2, doesn't mean that their math is correct :-)
Re: [PATCH v5 10/10] KVM: selftests: Test disabling NX hugepages on a VM
Quoted 68 lines · Sean Christopherson
Quoted 14 lines · Ben Gardon
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +2
-0@@ -13,6 +13,8 @@#include <fcntl.h> #include <stdint.h> #include <time.h> +#include <linux/reboot.h> +#include <sys/syscall.h> #include <test_util.h> #include "kvm_util.h" tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c static void check_split_count(struct kvm_vm *vm, int expected_splits) · line 85
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +1
-1@@ -80,13 +82,45 @@ static void check_split_count(struct kvm_vm *vm, int expected_splits) expected_splits, actual_splits); } -int main (int argc,char **argv) +void run_test (bool disable_nx)Probably worth naming this
disable_nx_workaroundordisable_nx_mitigation, it's quite easy to think this means "disable EFER.NX".Quoted 19 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +13
-2{struct kvm_vm *vm;struct timespec ts; +uint64_t pages;void *hva; - - vm =vm_create_default (0 ,0 , guest_code); +int r; + + pages =vm_pages_needed (VM_MODE_DEFAULT ,1 ,DEFAULT_GUEST_PHY_PAGES , +0 ,0 ); + vm =vm_create_without_vcpus (VM_MODE_DEFAULT , pages); + +if (disable_nx) { +kvm_check_cap (KVM_CAP_VM_DISABLE_NX_HUGE_PAGES ); + +/* +* Check if this process has the reboot permissions needed to +* disable NX huge pages on a VM. tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c static void check_split_count(struct kvm_vm *vm, int expected_splits) · line 112
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +10
-0+* +* The reboot call below will never have any effect because +* the magic values are not set correctly, however the +* permission check is done before the magic value check. +*/ + r =syscall (SYS_reboot,0 ,0 ,0 ,NULL ); +if (r && errno ==EPERM ) { + r =vm_disable_nx_huge_pages (vm); +TEST_ASSERT (r ==EPERM , +"This process should not have permission to disable NX huge pages" );First off, huge kudos for negative testing! But, it's going to provide poor coverage if we teach everyone to use the runner script, because that'll likely require root on most hosts, e.g. to futz with the module param.
Aha! Idea. And it should eliminate the
SYS_rebootshenanigans, which while hilarious, are mildy scary.In the runner script, wrap all the modification of sysfs knobs with sudo, and then (again with sudo) do:
setcap cap_sys_boot+ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test
MAGIC_NUMBER-b
Quoted 6 lines
where "-b" means "has
CAP_SYS_BOOT". And thensetcap cap_sys_boot-ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test
MAGIC_NUMBERHmm, and I guess if the script is run as root, just skip the second invocation.
Wouldn't it be easier to just run the test binary twice and just have the second time run without root permissions? I don't know if there's an easy way to do that.
Quoted 27 lines
Quoted 16 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +11
-0+return ; + } + +TEST_ASSERT (r && errno ==EINVAL , +"Reboot syscall should fail with -EINVAL" ); + + r =vm_disable_nx_huge_pages (vm); +TEST_ASSERT (!r,"Disabling NX huge pages should succeed if process has reboot permissions" ); + } + +vm_vcpu_add_default (vm,0 , guest_code); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB, HPAGE_GPA, HPAGE_SLOT,tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c int main(int argc, char **argv) · line 159
Quoted 7 lines
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +2
-2@@ -121,21 +155,21 @@ int main(int argc, char **argv)* to be remapped at 4k. */ vcpu_run (vm,0 ); -check_2m_page_count (vm,1 ); -check_split_count (vm,1 ); +check_2m_page_count (vm, disable_nx ?2 :1 ); +check_split_count (vm, disable_nx ?0 :1 );Can you update the comments to explain why these magic number of pages are expected for NX enabled/disabled? As Jim has pointed out, just because KVM and selftests might agree that 1==2, doesn't mean that their math is correct :-)
Sean Christopherson
maintainerRe: [PATCH v5 10/10] KVM: selftests: Test disabling NX hugepages on a VM
Quoted 16 lines · Ben Gardon
Quoted 15 lines · Sean Christopherson
First off, huge kudos for negative testing! But, it's going to provide poor coverage if we teach everyone to use the runner script, because that'll likely require root on most hosts, e.g. to futz with the module param.
Aha! Idea. And it should eliminate the
SYS_rebootshenanigans, which while hilarious, are mildy scary.In the runner script, wrap all the modification of sysfs knobs with sudo, and then (again with sudo) do:
setcap cap_sys_boot+ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test
MAGIC_NUMBER-bwhere "-b" means "has
CAP_SYS_BOOT". And then
Quoted 8 lines
Quoted 4 lines
setcap cap_sys_boot-ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test
MAGIC_NUMBERHmm, and I guess if the script is run as root, just skip the second invocation.
Wouldn't it be easier to just run the test binary twice and just have the second time run without root permissions? I don't know if there's an easy way to do that.
I don't think so, e.g. what if there is no other user account to switch to? On the other hand, I doubt I'm the only person that typically runs selftests with a user account.
Using setcap isn't hard, e.g.
# If the test isn't running as root, verify KVM correctly rejects the # per-VM override if the process doesn't have CAP_SYS_BOOT. if [[ $(id -u) -ne 0 ]]; then
sudo setcap cap_sys_boot-ep path/to/nx_huge_pages_test path/to/nx_huge_pages_test MAGIC_NUMBER
sudo setcap cap_sys_boot+ep path/to/nx_huge_pages_test
fi
# The test now has CAP_SYS_BOOT, or is running as root. path/to/nx_huge_pages_test MAGIC_NUMBER -b
Bonus points if you want to save/restore the capability.
Quoted 7 lines · Ben Gardon
Given the high cost of NX hugepages in terms of TLB performance, it may be desirable to disable the mitigation on a per-VM basis. In the case of public cloud providers with many VMs on a single host, some VMs may be more trusted than others. In order to maximize performance on critical VMs, while still providing some protection to the host from iTLB Multihit, allow the mitigation to be selectively disabled.
For the series:
Quoted 83 lines
Disabling NX hugepages on a VM is relatively straightforward, but I took this as an opportunity to add some NX hugepages test coverage and clean up selftests infrastructure a bit.
This series was tested with the new selftest and the rest of the KVM selftests on an Intel Haswell machine.
The following tests failed, but I do not believe that has anything to do with this series:
userspace_io_test
vmx_nested_tsc_scaling_test
vmx_preemption_timer_testChangelog:
v1->v2:
Dropped the complicated memslot refactor in favor of Ricardo Koller's patch with a similar effect.
Incorporated David Dunn's feedback and reviewed by tag: shortened waits to speed up test.v2->v3:
Incorporated a suggestion from David on how to build the NX huge pages test.
Fixed a build breakage identified by David.
Dropped the per-vmnx_huge_pagesfield in favor of simply checking the global + per-VM disable override.
Documented the new capability
Separated out the commit to test disabling NX huge pages Removed permission check when checking if the disable NX capability is supported.
Added test coverage for the permission check.v3->v4:
Collected RB's from Jing and David
Modified stat collection to reduce a memory allocation [David] Incorporated various improvments to the NX test [David] Changed the NX disable test to run by default [David] Removed some now unnecessary commits
Dropped the code to dump KVM stats from the binary stats test, and factor out parts of the existing test to library functions instead. [David, Jing, Sean]
Dropped the improvement to a debugging log message as it's no longer relevant to this series.v4->v5:
Incorporated cleanup suggestions from David and Sean
Added a patch with style fixes for the binary stats test from Sean Added a restriction that NX huge pages can only be disabled before vCPUs are created [Sean]Ben Gardon (9):
KVM: selftests: Remove dynamic memory allocation for stats header
KVM: selftests: Read binary stats header in lib
KVM: selftests: Read binary stats desc in lib
KVM: selftests: Read binary stat data in lib
KVM: selftests: Add NX huge pages test
KVM: x86: Fix errant brace in KVM capability handling
KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis
KVM: selftests: Factor out calculation of pages needed for a VM
KVM: selftests: Test disabling NX hugepages on a VMSean Christopherson (1):
KVM: selftests: Clean up coding style in binary stats testDocumentation/virt/kvm/api.rst | 13 + arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/mmu.h | 9 +- arch/x86/kvm/mmu/spte.c | 7 +- arch/x86/kvm/mmu/spte.h | 3 +- arch/x86/kvm/mmu/tdp_mmu.c | 3 +- arch/x86/kvm/x86.c | 25 +- include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/Makefile | 10 + .../selftests/kvm/include/kvm_util_base.h | 13 + .../selftests/kvm/kvm_binary_stats_test.c | 142 ++++++----- tools/testing/selftests/kvm/lib/kvm_util.c | 232 ++++++++++++++++-- .../selftests/kvm/x86_64/nx_huge_pages_test.c | 206 ++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 25 ++ 14 files changed, 597 insertions(+), 94 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c create mode 100755 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh--
2.35.1.1178.g4f1659d476-goog