xref: /linux/lib/vdso/datastore.c (revision df7fcbefa71090a276fb841ffe19b8e5f12b4767)
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_HAVE_GENERIC_VDSO
15 static union vdso_data_store vdso_time_data_store __page_aligned_data;
16 struct vdso_time_data *vdso_k_time_data = vdso_time_data_store.data;
17 static_assert(sizeof(vdso_time_data_store) == PAGE_SIZE);
18 #endif /* CONFIG_HAVE_GENERIC_VDSO */
19 
20 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
21 			     struct vm_area_struct *vma, struct vm_fault *vmf)
22 {
23 	struct page *timens_page = find_timens_vvar_page(vma);
24 	unsigned long addr, pfn;
25 	vm_fault_t err;
26 
27 	switch (vmf->pgoff) {
28 	case VDSO_TIME_PAGE_OFFSET:
29 		if (!IS_ENABLED(CONFIG_HAVE_GENERIC_VDSO))
30 			return VM_FAULT_SIGBUS;
31 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
32 		if (timens_page) {
33 			/*
34 			 * Fault in VVAR page too, since it will be accessed
35 			 * to get clock data anyway.
36 			 */
37 			addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * PAGE_SIZE;
38 			err = vmf_insert_pfn(vma, addr, pfn);
39 			if (unlikely(err & VM_FAULT_ERROR))
40 				return err;
41 			pfn = page_to_pfn(timens_page);
42 		}
43 		break;
44 	case VDSO_TIMENS_PAGE_OFFSET:
45 		/*
46 		 * If a task belongs to a time namespace then a namespace
47 		 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
48 		 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
49 		 * offset.
50 		 * See also the comment near timens_setup_vdso_data().
51 		 */
52 		if (!IS_ENABLED(CONFIG_TIME_NS) || !timens_page)
53 			return VM_FAULT_SIGBUS;
54 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
55 		break;
56 	default:
57 		return VM_FAULT_SIGBUS;
58 	}
59 
60 	return vmf_insert_pfn(vma, vmf->address, pfn);
61 }
62 
63 const struct vm_special_mapping vdso_vvar_mapping = {
64 	.name	= "[vvar]",
65 	.fault	= vvar_fault,
66 };
67 
68 struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr)
69 {
70 	return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE,
71 					VM_READ | VM_MAYREAD | VM_IO | VM_DONTDUMP | VM_PFNMAP,
72 					&vdso_vvar_mapping);
73 }
74 
75 #ifdef CONFIG_TIME_NS
76 /*
77  * The vvar page layout depends on whether a task belongs to the root or
78  * non-root time namespace. Whenever a task changes its namespace, the VVAR
79  * page tables are cleared and then they will be re-faulted with a
80  * corresponding layout.
81  * See also the comment near timens_setup_vdso_data() for details.
82  */
83 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
84 {
85 	struct mm_struct *mm = task->mm;
86 	struct vm_area_struct *vma;
87 	VMA_ITERATOR(vmi, mm, 0);
88 
89 	mmap_read_lock(mm);
90 	for_each_vma(vmi, vma) {
91 		if (vma_is_special_mapping(vma, &vdso_vvar_mapping))
92 			zap_vma_pages(vma);
93 	}
94 	mmap_read_unlock(mm);
95 
96 	return 0;
97 }
98 
99 struct vdso_time_data *arch_get_vdso_data(void *vvar_page)
100 {
101 	return (struct vdso_time_data *)vvar_page;
102 }
103 #endif
104