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