xref: /linux/lib/vdso/datastore.c (revision 8c7c1b5506e593ce00c42214b4fcafd640ceeb42)
1df7fcbefSThomas Weißschuh // SPDX-License-Identifier: GPL-2.0-only
2df7fcbefSThomas Weißschuh 
3df7fcbefSThomas Weißschuh #include <linux/linkage.h>
4df7fcbefSThomas Weißschuh #include <linux/mmap_lock.h>
5df7fcbefSThomas Weißschuh #include <linux/mm.h>
6df7fcbefSThomas Weißschuh #include <linux/time_namespace.h>
7df7fcbefSThomas Weißschuh #include <linux/types.h>
8df7fcbefSThomas Weißschuh #include <linux/vdso_datastore.h>
9df7fcbefSThomas Weißschuh #include <vdso/datapage.h>
10df7fcbefSThomas Weißschuh 
11df7fcbefSThomas Weißschuh /*
12df7fcbefSThomas Weißschuh  * The vDSO data page.
13df7fcbefSThomas Weißschuh  */
14df7fcbefSThomas Weißschuh #ifdef CONFIG_HAVE_GENERIC_VDSO
15ac1a42f4SThomas Weißschuh static union {
16886653e3SAnna-Maria Behnsen 	struct vdso_time_data	data;
17ac1a42f4SThomas Weißschuh 	u8			page[PAGE_SIZE];
18ac1a42f4SThomas Weißschuh } vdso_time_data_store __page_aligned_data;
19886653e3SAnna-Maria Behnsen struct vdso_time_data *vdso_k_time_data = &vdso_time_data_store.data;
20df7fcbefSThomas Weißschuh static_assert(sizeof(vdso_time_data_store) == PAGE_SIZE);
21df7fcbefSThomas Weißschuh #endif /* CONFIG_HAVE_GENERIC_VDSO */
22df7fcbefSThomas Weißschuh 
2351d6ca37SThomas Weißschuh #ifdef CONFIG_VDSO_GETRANDOM
2451d6ca37SThomas Weißschuh static union {
2551d6ca37SThomas Weißschuh 	struct vdso_rng_data	data;
2651d6ca37SThomas Weißschuh 	u8			page[PAGE_SIZE];
2751d6ca37SThomas Weißschuh } vdso_rng_data_store __page_aligned_data;
2851d6ca37SThomas Weißschuh struct vdso_rng_data *vdso_k_rng_data = &vdso_rng_data_store.data;
2951d6ca37SThomas Weißschuh static_assert(sizeof(vdso_rng_data_store) == PAGE_SIZE);
3051d6ca37SThomas Weißschuh #endif /* CONFIG_VDSO_GETRANDOM */
3151d6ca37SThomas Weißschuh 
32365841e1SThomas Weißschuh #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA
33365841e1SThomas Weißschuh static union {
34365841e1SThomas Weißschuh 	struct vdso_arch_data	data;
35365841e1SThomas Weißschuh 	u8			page[VDSO_ARCH_DATA_SIZE];
36365841e1SThomas Weißschuh } vdso_arch_data_store __page_aligned_data;
37365841e1SThomas Weißschuh struct vdso_arch_data *vdso_k_arch_data = &vdso_arch_data_store.data;
38365841e1SThomas Weißschuh #endif /* CONFIG_ARCH_HAS_VDSO_ARCH_DATA */
39365841e1SThomas Weißschuh 
vvar_fault(const struct vm_special_mapping * sm,struct vm_area_struct * vma,struct vm_fault * vmf)40df7fcbefSThomas Weißschuh static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
41df7fcbefSThomas Weißschuh 			     struct vm_area_struct *vma, struct vm_fault *vmf)
42df7fcbefSThomas Weißschuh {
43df7fcbefSThomas Weißschuh 	struct page *timens_page = find_timens_vvar_page(vma);
44df7fcbefSThomas Weißschuh 	unsigned long addr, pfn;
45df7fcbefSThomas Weißschuh 	vm_fault_t err;
46df7fcbefSThomas Weißschuh 
47df7fcbefSThomas Weißschuh 	switch (vmf->pgoff) {
48df7fcbefSThomas Weißschuh 	case VDSO_TIME_PAGE_OFFSET:
49df7fcbefSThomas Weißschuh 		if (!IS_ENABLED(CONFIG_HAVE_GENERIC_VDSO))
50df7fcbefSThomas Weißschuh 			return VM_FAULT_SIGBUS;
51df7fcbefSThomas Weißschuh 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
52df7fcbefSThomas Weißschuh 		if (timens_page) {
53df7fcbefSThomas Weißschuh 			/*
54df7fcbefSThomas Weißschuh 			 * Fault in VVAR page too, since it will be accessed
55df7fcbefSThomas Weißschuh 			 * to get clock data anyway.
56df7fcbefSThomas Weißschuh 			 */
57df7fcbefSThomas Weißschuh 			addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * PAGE_SIZE;
58df7fcbefSThomas Weißschuh 			err = vmf_insert_pfn(vma, addr, pfn);
59df7fcbefSThomas Weißschuh 			if (unlikely(err & VM_FAULT_ERROR))
60df7fcbefSThomas Weißschuh 				return err;
61df7fcbefSThomas Weißschuh 			pfn = page_to_pfn(timens_page);
62df7fcbefSThomas Weißschuh 		}
63df7fcbefSThomas Weißschuh 		break;
64df7fcbefSThomas Weißschuh 	case VDSO_TIMENS_PAGE_OFFSET:
65df7fcbefSThomas Weißschuh 		/*
66df7fcbefSThomas Weißschuh 		 * If a task belongs to a time namespace then a namespace
67df7fcbefSThomas Weißschuh 		 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
68df7fcbefSThomas Weißschuh 		 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
69df7fcbefSThomas Weißschuh 		 * offset.
70df7fcbefSThomas Weißschuh 		 * See also the comment near timens_setup_vdso_data().
71df7fcbefSThomas Weißschuh 		 */
72df7fcbefSThomas Weißschuh 		if (!IS_ENABLED(CONFIG_TIME_NS) || !timens_page)
73df7fcbefSThomas Weißschuh 			return VM_FAULT_SIGBUS;
74df7fcbefSThomas Weißschuh 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_time_data));
75df7fcbefSThomas Weißschuh 		break;
7651d6ca37SThomas Weißschuh 	case VDSO_RNG_PAGE_OFFSET:
7751d6ca37SThomas Weißschuh 		if (!IS_ENABLED(CONFIG_VDSO_GETRANDOM))
7851d6ca37SThomas Weißschuh 			return VM_FAULT_SIGBUS;
7951d6ca37SThomas Weißschuh 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_rng_data));
8051d6ca37SThomas Weißschuh 		break;
81365841e1SThomas Weißschuh 	case VDSO_ARCH_PAGES_START ... VDSO_ARCH_PAGES_END:
82365841e1SThomas Weißschuh 		if (!IS_ENABLED(CONFIG_ARCH_HAS_VDSO_ARCH_DATA))
83365841e1SThomas Weißschuh 			return VM_FAULT_SIGBUS;
84365841e1SThomas Weißschuh 		pfn = __phys_to_pfn(__pa_symbol(vdso_k_arch_data)) +
85365841e1SThomas Weißschuh 			vmf->pgoff - VDSO_ARCH_PAGES_START;
86365841e1SThomas Weißschuh 		break;
87df7fcbefSThomas Weißschuh 	default:
88df7fcbefSThomas Weißschuh 		return VM_FAULT_SIGBUS;
89df7fcbefSThomas Weißschuh 	}
90df7fcbefSThomas Weißschuh 
91df7fcbefSThomas Weißschuh 	return vmf_insert_pfn(vma, vmf->address, pfn);
92df7fcbefSThomas Weißschuh }
93df7fcbefSThomas Weißschuh 
94df7fcbefSThomas Weißschuh const struct vm_special_mapping vdso_vvar_mapping = {
95df7fcbefSThomas Weißschuh 	.name	= "[vvar]",
96df7fcbefSThomas Weißschuh 	.fault	= vvar_fault,
97df7fcbefSThomas Weißschuh };
98df7fcbefSThomas Weißschuh 
vdso_install_vvar_mapping(struct mm_struct * mm,unsigned long addr)99df7fcbefSThomas Weißschuh struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr)
100df7fcbefSThomas Weißschuh {
101df7fcbefSThomas Weißschuh 	return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE,
102*1d6fad7bSHeiko Carstens 					VM_READ | VM_MAYREAD | VM_IO | VM_DONTDUMP |
103*1d6fad7bSHeiko Carstens 					VM_PFNMAP | VM_SEALED_SYSMAP,
104df7fcbefSThomas Weißschuh 					&vdso_vvar_mapping);
105df7fcbefSThomas Weißschuh }
106df7fcbefSThomas Weißschuh 
107df7fcbefSThomas Weißschuh #ifdef CONFIG_TIME_NS
108df7fcbefSThomas Weißschuh /*
109df7fcbefSThomas Weißschuh  * The vvar page layout depends on whether a task belongs to the root or
110df7fcbefSThomas Weißschuh  * non-root time namespace. Whenever a task changes its namespace, the VVAR
111df7fcbefSThomas Weißschuh  * page tables are cleared and then they will be re-faulted with a
112df7fcbefSThomas Weißschuh  * corresponding layout.
11302352208SAnna-Maria Behnsen  * See also the comment near timens_setup_vdso_clock_data() for details.
114df7fcbefSThomas Weißschuh  */
vdso_join_timens(struct task_struct * task,struct time_namespace * ns)115df7fcbefSThomas Weißschuh int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
116df7fcbefSThomas Weißschuh {
117df7fcbefSThomas Weißschuh 	struct mm_struct *mm = task->mm;
118df7fcbefSThomas Weißschuh 	struct vm_area_struct *vma;
119df7fcbefSThomas Weißschuh 	VMA_ITERATOR(vmi, mm, 0);
120df7fcbefSThomas Weißschuh 
121df7fcbefSThomas Weißschuh 	mmap_read_lock(mm);
122df7fcbefSThomas Weißschuh 	for_each_vma(vmi, vma) {
123df7fcbefSThomas Weißschuh 		if (vma_is_special_mapping(vma, &vdso_vvar_mapping))
124df7fcbefSThomas Weißschuh 			zap_vma_pages(vma);
125df7fcbefSThomas Weißschuh 	}
126df7fcbefSThomas Weißschuh 	mmap_read_unlock(mm);
127df7fcbefSThomas Weißschuh 
128df7fcbefSThomas Weißschuh 	return 0;
129df7fcbefSThomas Weißschuh }
130df7fcbefSThomas Weißschuh #endif
131