datastore.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/linkage.h>
  3. #include <linux/mmap_lock.h>
  4. #include <linux/mm.h>
  5. #include <linux/time_namespace.h>
  6. #include <linux/types.h>
  7. #include <linux/vdso_datastore.h>
  8. #include <vdso/datapage.h>
  9. /*
  10. * The vDSO data page.
  11. */
  12. #ifdef CONFIG_GENERIC_GETTIMEOFDAY
  13. static union {
  14. struct vdso_time_data data;
  15. u8 page[PAGE_SIZE];
  16. } vdso_time_data_store __page_aligned_data;
  17. struct vdso_time_data *vdso_k_time_data = &vdso_time_data_store.data;
  18. static_assert(sizeof(vdso_time_data_store) == PAGE_SIZE);
  19. #endif /* CONFIG_GENERIC_GETTIMEOFDAY */
  20. #ifdef CONFIG_VDSO_GETRANDOM
  21. static union {
  22. struct vdso_rng_data data;
  23. u8 page[PAGE_SIZE];
  24. } vdso_rng_data_store __page_aligned_data;
  25. struct vdso_rng_data *vdso_k_rng_data = &vdso_rng_data_store.data;
  26. static_assert(sizeof(vdso_rng_data_store) == PAGE_SIZE);
  27. #endif /* CONFIG_VDSO_GETRANDOM */
  28. #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA
  29. static union {
  30. struct vdso_arch_data data;
  31. u8 page[VDSO_ARCH_DATA_SIZE];
  32. } vdso_arch_data_store __page_aligned_data;
  33. struct vdso_arch_data *vdso_k_arch_data = &vdso_arch_data_store.data;
  34. #endif /* CONFIG_ARCH_HAS_VDSO_ARCH_DATA */
  35. static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
  36. struct vm_area_struct *vma, struct vm_fault *vmf)
  37. {
  38. struct page *timens_page = find_timens_vvar_page(vma);
  39. unsigned long addr, pfn;
  40. vm_fault_t err;
  41. switch (vmf->pgoff) {
  42. case VDSO_TIME_PAGE_OFFSET:
  43. if (!IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY))
  44. return VM_FAULT_SIGBUS;
  45. pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
  46. if (timens_page) {
  47. /*
  48. * Fault in VVAR page too, since it will be accessed
  49. * to get clock data anyway.
  50. */
  51. addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * PAGE_SIZE;
  52. err = vmf_insert_pfn(vma, addr, pfn);
  53. if (unlikely(err & VM_FAULT_ERROR))
  54. return err;
  55. pfn = page_to_pfn(timens_page);
  56. }
  57. break;
  58. case VDSO_TIMENS_PAGE_OFFSET:
  59. /*
  60. * If a task belongs to a time namespace then a namespace
  61. * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
  62. * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
  63. * offset.
  64. * See also the comment near timens_setup_vdso_data().
  65. */
  66. if (!IS_ENABLED(CONFIG_TIME_NS) || !timens_page)
  67. return VM_FAULT_SIGBUS;
  68. pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
  69. break;
  70. case VDSO_RNG_PAGE_OFFSET:
  71. if (!IS_ENABLED(CONFIG_VDSO_GETRANDOM))
  72. return VM_FAULT_SIGBUS;
  73. pfn = __phys_to_pfn(__pa_symbol(vdso_k_rng_data));
  74. break;
  75. case VDSO_ARCH_PAGES_START ... VDSO_ARCH_PAGES_END:
  76. if (!IS_ENABLED(CONFIG_ARCH_HAS_VDSO_ARCH_DATA))
  77. return VM_FAULT_SIGBUS;
  78. pfn = __phys_to_pfn(__pa_symbol(vdso_k_arch_data)) +
  79. vmf->pgoff - VDSO_ARCH_PAGES_START;
  80. break;
  81. default:
  82. return VM_FAULT_SIGBUS;
  83. }
  84. return vmf_insert_pfn(vma, vmf->address, pfn);
  85. }
  86. const struct vm_special_mapping vdso_vvar_mapping = {
  87. .name = "[vvar]",
  88. .fault = vvar_fault,
  89. };
  90. struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr)
  91. {
  92. return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE,
  93. VM_READ | VM_MAYREAD | VM_IO | VM_DONTDUMP |
  94. VM_PFNMAP | VM_SEALED_SYSMAP,
  95. &vdso_vvar_mapping);
  96. }
  97. #ifdef CONFIG_TIME_NS
  98. /*
  99. * The vvar page layout depends on whether a task belongs to the root or
  100. * non-root time namespace. Whenever a task changes its namespace, the VVAR
  101. * page tables are cleared and then they will be re-faulted with a
  102. * corresponding layout.
  103. * See also the comment near timens_setup_vdso_clock_data() for details.
  104. */
  105. int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
  106. {
  107. struct mm_struct *mm = task->mm;
  108. struct vm_area_struct *vma;
  109. VMA_ITERATOR(vmi, mm, 0);
  110. mmap_read_lock(mm);
  111. for_each_vma(vmi, vma) {
  112. if (vma_is_special_mapping(vma, &vdso_vvar_mapping))
  113. zap_vma_pages(vma);
  114. }
  115. mmap_read_unlock(mm);
  116. return 0;
  117. }
  118. #endif