shared_zeropage_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Test shared zeropage handling (with/without storage keys)
  4. *
  5. * Copyright (C) 2024, Red Hat, Inc.
  6. */
  7. #include <sys/mman.h>
  8. #include <linux/fs.h>
  9. #include "test_util.h"
  10. #include "kvm_util.h"
  11. #include "kselftest.h"
  12. #include "ucall_common.h"
  13. static void set_storage_key(void *addr, uint8_t skey)
  14. {
  15. asm volatile("sske %0,%1" : : "d" (skey), "a" (addr));
  16. }
  17. static void guest_code(void)
  18. {
  19. /* Issue some storage key instruction. */
  20. set_storage_key((void *)0, 0x98);
  21. GUEST_DONE();
  22. }
  23. /*
  24. * Returns 1 if the shared zeropage is mapped, 0 if something else is mapped.
  25. * Returns < 0 on error or if nothing is mapped.
  26. */
  27. static int maps_shared_zeropage(int pagemap_fd, void *addr)
  28. {
  29. struct page_region region;
  30. struct pm_scan_arg arg = {
  31. .start = (uintptr_t)addr,
  32. .end = (uintptr_t)addr + 4096,
  33. .vec = (uintptr_t)&region,
  34. .vec_len = 1,
  35. .size = sizeof(struct pm_scan_arg),
  36. .category_mask = PAGE_IS_PFNZERO,
  37. .category_anyof_mask = PAGE_IS_PRESENT,
  38. .return_mask = PAGE_IS_PFNZERO,
  39. };
  40. return ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);
  41. }
  42. int main(int argc, char *argv[])
  43. {
  44. char *mem, *page0, *page1, *page2, tmp;
  45. const size_t pagesize = getpagesize();
  46. struct kvm_vcpu *vcpu;
  47. struct kvm_vm *vm;
  48. struct ucall uc;
  49. int pagemap_fd;
  50. ksft_print_header();
  51. ksft_set_plan(3);
  52. /*
  53. * We'll use memory that is not mapped into the VM for simplicity.
  54. * Shared zeropages are enabled/disabled per-process.
  55. */
  56. mem = mmap(0, 3 * pagesize, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
  57. TEST_ASSERT(mem != MAP_FAILED, "mmap() failed");
  58. /* Disable THP. Ignore errors on older kernels. */
  59. madvise(mem, 3 * pagesize, MADV_NOHUGEPAGE);
  60. page0 = mem;
  61. page1 = page0 + pagesize;
  62. page2 = page1 + pagesize;
  63. /* Can we even detect shared zeropages? */
  64. pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
  65. TEST_REQUIRE(pagemap_fd >= 0);
  66. tmp = *page0;
  67. asm volatile("" : "+r" (tmp));
  68. TEST_REQUIRE(maps_shared_zeropage(pagemap_fd, page0) == 1);
  69. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  70. /* Verify that we get the shared zeropage after VM creation. */
  71. tmp = *page1;
  72. asm volatile("" : "+r" (tmp));
  73. ksft_test_result(maps_shared_zeropage(pagemap_fd, page1) == 1,
  74. "Shared zeropages should be enabled\n");
  75. /*
  76. * Let our VM execute a storage key instruction that should
  77. * unshare all shared zeropages.
  78. */
  79. vcpu_run(vcpu);
  80. get_ucall(vcpu, &uc);
  81. TEST_ASSERT_EQ(uc.cmd, UCALL_DONE);
  82. /* Verify that we don't have a shared zeropage anymore. */
  83. ksft_test_result(!maps_shared_zeropage(pagemap_fd, page1),
  84. "Shared zeropage should be gone\n");
  85. /* Verify that we don't get any new shared zeropages. */
  86. tmp = *page2;
  87. asm volatile("" : "+r" (tmp));
  88. ksft_test_result(!maps_shared_zeropage(pagemap_fd, page2),
  89. "Shared zeropages should be disabled\n");
  90. kvm_vm_free(vm);
  91. ksft_finished();
  92. }