1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
5 * Copyright (C) 2012 ARM Limited
6 * Copyright (C) 2015 Regents of the University of California
11 #include <linux/slab.h>
12 #include <linux/binfmts.h>
13 #include <linux/err.h>
16 #include <linux/time_namespace.h>
18 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
19 #include <vdso/datapage.h>
25 extern char vdso_start[], vdso_end[];
27 extern char compat_vdso_start[], compat_vdso_end[];
31 VVAR_DATA_PAGE_OFFSET,
32 VVAR_TIMENS_PAGE_OFFSET,
41 #define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
47 struct vdso_data data;
49 } vdso_data_store __page_aligned_data;
50 struct vdso_data *vdso_data = &vdso_data_store.data;
54 const char *vdso_code_start;
55 const char *vdso_code_end;
56 unsigned long vdso_pages;
58 struct vm_special_mapping *dm;
60 struct vm_special_mapping *cm;
63 static struct __vdso_info vdso_info;
65 static struct __vdso_info compat_vdso_info;
68 static int vdso_mremap(const struct vm_special_mapping *sm,
69 struct vm_area_struct *new_vma)
71 current->mm->context.vdso = (void *)new_vma->vm_start;
76 static void __init __vdso_init(struct __vdso_info *vdso_info)
79 struct page **vdso_pagelist;
82 if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4))
83 panic("vDSO is not a valid ELF object!\n");
85 vdso_info->vdso_pages = (
86 vdso_info->vdso_code_end -
87 vdso_info->vdso_code_start) >>
90 vdso_pagelist = kcalloc(vdso_info->vdso_pages,
91 sizeof(struct page *),
93 if (vdso_pagelist == NULL)
94 panic("vDSO kcalloc failed!\n");
96 /* Grab the vDSO code pages. */
97 pfn = sym_to_pfn(vdso_info->vdso_code_start);
99 for (i = 0; i < vdso_info->vdso_pages; i++)
100 vdso_pagelist[i] = pfn_to_page(pfn + i);
102 vdso_info->cm->pages = vdso_pagelist;
105 #ifdef CONFIG_TIME_NS
106 struct vdso_data *arch_get_vdso_data(void *vvar_page)
108 return (struct vdso_data *)(vvar_page);
112 * The vvar mapping contains data for a specific time namespace, so when a task
113 * changes namespace we must unmap its vvar data for the old namespace.
114 * Subsequent faults will map in data for the new namespace.
116 * For more details see timens_setup_vdso_data().
118 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
120 struct mm_struct *mm = task->mm;
121 struct vm_area_struct *vma;
122 VMA_ITERATOR(vmi, mm, 0);
126 for_each_vma(vmi, vma) {
127 unsigned long size = vma->vm_end - vma->vm_start;
129 if (vma_is_special_mapping(vma, vdso_info.dm))
130 zap_page_range(vma, vma->vm_start, size);
132 if (vma_is_special_mapping(vma, compat_vdso_info.dm))
133 zap_page_range(vma, vma->vm_start, size);
137 mmap_read_unlock(mm);
141 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
143 if (likely(vma->vm_mm == current->mm))
144 return current->nsproxy->time_ns->vvar_page;
147 * VM_PFNMAP | VM_IO protect .fault() handler from being called
148 * through interfaces like /proc/$pid/mem or
149 * process_vm_{readv,writev}() as long as there's no .access()
150 * in special_mapping_vmops.
151 * For more details check_vma_flags() and __access_remote_vm()
153 WARN(1, "vvar_page accessed remotely");
158 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
164 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
165 struct vm_area_struct *vma, struct vm_fault *vmf)
167 struct page *timens_page = find_timens_vvar_page(vma);
170 switch (vmf->pgoff) {
171 case VVAR_DATA_PAGE_OFFSET:
173 pfn = page_to_pfn(timens_page);
175 pfn = sym_to_pfn(vdso_data);
177 #ifdef CONFIG_TIME_NS
178 case VVAR_TIMENS_PAGE_OFFSET:
180 * If a task belongs to a time namespace then a namespace
181 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and
182 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET
184 * See also the comment near timens_setup_vdso_data().
187 return VM_FAULT_SIGBUS;
188 pfn = sym_to_pfn(vdso_data);
190 #endif /* CONFIG_TIME_NS */
192 return VM_FAULT_SIGBUS;
195 return vmf_insert_pfn(vma, vmf->address, pfn);
198 static struct vm_special_mapping rv_vdso_maps[] __ro_after_init = {
199 [RV_VDSO_MAP_VVAR] = {
203 [RV_VDSO_MAP_VDSO] = {
205 .mremap = vdso_mremap,
209 static struct __vdso_info vdso_info __ro_after_init = {
211 .vdso_code_start = vdso_start,
212 .vdso_code_end = vdso_end,
213 .dm = &rv_vdso_maps[RV_VDSO_MAP_VVAR],
214 .cm = &rv_vdso_maps[RV_VDSO_MAP_VDSO],
218 static struct vm_special_mapping rv_compat_vdso_maps[] __ro_after_init = {
219 [RV_VDSO_MAP_VVAR] = {
223 [RV_VDSO_MAP_VDSO] = {
225 .mremap = vdso_mremap,
229 static struct __vdso_info compat_vdso_info __ro_after_init = {
230 .name = "compat_vdso",
231 .vdso_code_start = compat_vdso_start,
232 .vdso_code_end = compat_vdso_end,
233 .dm = &rv_compat_vdso_maps[RV_VDSO_MAP_VVAR],
234 .cm = &rv_compat_vdso_maps[RV_VDSO_MAP_VDSO],
238 static int __init vdso_init(void)
240 __vdso_init(&vdso_info);
242 __vdso_init(&compat_vdso_info);
247 arch_initcall(vdso_init);
249 static int __setup_additional_pages(struct mm_struct *mm,
250 struct linux_binprm *bprm,
252 struct __vdso_info *vdso_info)
254 unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
257 BUILD_BUG_ON(VVAR_NR_PAGES != __VVAR_PAGES);
259 vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT;
260 /* Be sure to map the data page */
261 vdso_mapping_len = vdso_text_len + VVAR_SIZE;
263 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
264 if (IS_ERR_VALUE(vdso_base)) {
265 ret = ERR_PTR(vdso_base);
269 ret = _install_special_mapping(mm, vdso_base, VVAR_SIZE,
270 (VM_READ | VM_MAYREAD | VM_PFNMAP), vdso_info->dm);
274 vdso_base += VVAR_SIZE;
275 mm->context.vdso = (void *)vdso_base;
278 _install_special_mapping(mm, vdso_base, vdso_text_len,
279 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
288 mm->context.vdso = NULL;
293 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
296 struct mm_struct *mm = current->mm;
299 if (mmap_write_lock_killable(mm))
302 ret = __setup_additional_pages(mm, bprm, uses_interp,
304 mmap_write_unlock(mm);
310 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
312 struct mm_struct *mm = current->mm;
315 if (mmap_write_lock_killable(mm))
318 ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info);
319 mmap_write_unlock(mm);