1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/gfp.h> 4 #include <linux/init.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 static u8 vdso_initdata[VDSO_NR_PAGES * PAGE_SIZE] __aligned(PAGE_SIZE) __initdata = {}; 12 13 #ifdef CONFIG_GENERIC_GETTIMEOFDAY 14 struct vdso_time_data *vdso_k_time_data __ro_after_init = 15 (void *)&vdso_initdata[VDSO_TIME_PAGE_OFFSET * PAGE_SIZE]; 16 17 static_assert(sizeof(struct vdso_time_data) <= PAGE_SIZE); 18 #endif /* CONFIG_GENERIC_GETTIMEOFDAY */ 19 20 #ifdef CONFIG_VDSO_GETRANDOM 21 struct vdso_rng_data *vdso_k_rng_data __ro_after_init = 22 (void *)&vdso_initdata[VDSO_RNG_PAGE_OFFSET * PAGE_SIZE]; 23 24 static_assert(sizeof(struct vdso_rng_data) <= PAGE_SIZE); 25 #endif /* CONFIG_VDSO_GETRANDOM */ 26 27 #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA 28 struct vdso_arch_data *vdso_k_arch_data __ro_after_init = 29 (void *)&vdso_initdata[VDSO_ARCH_PAGES_START * PAGE_SIZE]; 30 #endif /* CONFIG_ARCH_HAS_VDSO_ARCH_DATA */ 31 32 static struct page *vdso_data_pages __ro_after_init; 33 34 void __init vdso_setup_data_pages(void) 35 { 36 unsigned int order = get_order(VDSO_NR_PAGES * PAGE_SIZE); 37 38 /* 39 * Allocate the data pages dynamically. SPARC does not support mapping 40 * static pages to be mapped into userspace. 41 * It is also a requirement for mlockall() support. 42 * 43 * Do not use folios. In time namespaces the pages are mapped in a different order 44 * to userspace, which is not handled by the folio optimizations in finish_fault(). 45 */ 46 vdso_data_pages = alloc_pages(GFP_KERNEL, order); 47 if (!vdso_data_pages) 48 panic("Unable to allocate VDSO storage pages"); 49 50 /* The pages are mapped one-by-one into userspace and each one needs to be refcounted. */ 51 split_page(vdso_data_pages, order); 52 53 /* Move the data already written by other subsystems to the new pages */ 54 memcpy(page_address(vdso_data_pages), vdso_initdata, VDSO_NR_PAGES * PAGE_SIZE); 55 56 if (IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY)) 57 vdso_k_time_data = page_address(vdso_data_pages + VDSO_TIME_PAGE_OFFSET); 58 59 if (IS_ENABLED(CONFIG_VDSO_GETRANDOM)) 60 vdso_k_rng_data = page_address(vdso_data_pages + VDSO_RNG_PAGE_OFFSET); 61 62 if (IS_ENABLED(CONFIG_ARCH_HAS_VDSO_ARCH_DATA)) 63 vdso_k_arch_data = page_address(vdso_data_pages + VDSO_ARCH_PAGES_START); 64 } 65 66 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, 67 struct vm_area_struct *vma, struct vm_fault *vmf) 68 { 69 struct page *page, *timens_page; 70 71 if (unlikely(vmf->flags & FAULT_FLAG_REMOTE)) 72 return VM_FAULT_SIGBUS; 73 74 page = vdso_data_pages + vmf->pgoff; 75 timens_page = find_timens_vvar_page(vma); 76 77 switch (vmf->pgoff) { 78 case VDSO_TIME_PAGE_OFFSET: 79 if (!IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY)) 80 break; 81 if (timens_page) { 82 /* 83 * Fault in VVAR page too, since it will be accessed 84 * to get clock data anyway. 85 */ 86 unsigned long addr; 87 vm_fault_t err; 88 89 addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * PAGE_SIZE; 90 err = vmf_insert_page(vma, addr, page); 91 if (unlikely(err & VM_FAULT_ERROR)) 92 return err; 93 page = timens_page; 94 } 95 break; 96 case VDSO_TIMENS_PAGE_OFFSET: 97 /* 98 * If a task belongs to a time namespace then a namespace 99 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and 100 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET 101 * offset. 102 * See also the comment near timens_setup_vdso_data(). 103 */ 104 if (!IS_ENABLED(CONFIG_TIME_NS) || !timens_page) 105 break; 106 page = vdso_data_pages + VDSO_TIME_PAGE_OFFSET; 107 break; 108 case VDSO_RNG_PAGE_OFFSET: 109 case VDSO_ARCH_PAGES_START ... VDSO_ARCH_PAGES_END: 110 break; 111 default: 112 return VM_FAULT_SIGBUS; 113 } 114 115 get_page(page); 116 vmf->page = page; 117 return 0; 118 } 119 120 const struct vm_special_mapping vdso_vvar_mapping = { 121 .name = "[vvar]", 122 .fault = vvar_fault, 123 }; 124 125 struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr) 126 { 127 return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE, 128 VM_READ | VM_MAYREAD | VM_IO | VM_DONTDUMP | 129 VM_MIXEDMAP | VM_SEALED_SYSMAP, 130 &vdso_vvar_mapping); 131 } 132