[PATCH v2 5/5] um: vdso: wire up the vvar mapping, layout and build (MMU only)

Haikal Firmansyah haikalfirmansyh.kernel at gmail.com
Wed Nov 25 01:38:11 PST 2026


Map a 4-page [vvar] below the [vdso] text and install it with
vdso_install_vvar_mapping(), grow [vdso] from a single page to as
many pages as the text needs, and lay the vvar out in the linker
script (VDSO_VVAR_SYMS, __VDSO_PAGES=4, adapted from
arch/x86/entry/vdso/common/vdso-layout.lds.S). Export clock_getres
and switch the vDSO objects from um_vdso.o to vclock_gettime.o on
MMU builds, adding -I$(srctree) -D__DISABLE_EXPORTS -DBUILD_VDSO
to the vDSO compile flags.

The vvar mapping and the generic vDSO object are MMU-only:
arch_setup_additional_pages() (which installs the [vvar] mapping)
is already inside #ifdef CONFIG_MMU, and the Makefile now builds
vclock_gettime.o only when CONFIG_MMU=y, keeping um_vdso.o (the
syscall shim) for NOMMU. NOMMU therefore needs no vvar page and
its build is untouched by this patch.

Signed-off-by: Haikal Firmansyah <haikalfirmansyh.kernel at gmail.com>
---
 arch/x86/um/vdso/Makefile          | 14 ++++-
 arch/x86/um/vdso/vdso-layout.lds.S | 28 ++++++++++
 arch/x86/um/vdso/vdso.lds.S        |  2 +
 arch/x86/um/vdso/vma.c             | 83 ++++++++++++++++++++++++------
 4 files changed, 109 insertions(+), 18 deletions(-)

diff --git a/arch/x86/um/vdso/Makefile b/arch/x86/um/vdso/Makefile
index 7664cbedb..5d4ee9142 100644
--- a/arch/x86/um/vdso/Makefile
+++ b/arch/x86/um/vdso/Makefile
@@ -4,7 +4,15 @@
 #

 # files to link into the vdso
-vobjs-y := vdso-note.o um_vdso.o
+vobjs-y := vdso-note.o
+# The generic vDSO (vclock_gettime.o) is MMU-only; NOMMU keeps the
+# syscall-shim vDSO (um_vdso.o), which needs none of the generic
+# vDSO framework.
+ifeq ($(CONFIG_MMU),y)
+vobjs-y += vclock_gettime.o
+else
+vobjs-y += um_vdso.o
+endif

 # files to link into kernel
 obj-y += vdso.o vma.o
@@ -35,7 +43,8 @@ $(obj)/%.so: $(obj)/%.so.dbg FORCE
 #
 CFL := $(PROFILING) -mcmodel=small -fPIC -O2
-fasynchronous-unwind-tables -m64 \
        $(filter -g%,$(KBUILD_CFLAGS)) -fno-stack-protector \
-       -fno-omit-frame-pointer -foptimize-sibling-calls
+       -fno-omit-frame-pointer -foptimize-sibling-calls \
+       -I$(srctree) -D__DISABLE_EXPORTS -DBUILD_VDSO

 $(vobjs): KBUILD_CFLAGS += $(CFL)

@@ -44,6 +53,7 @@ $(vobjs): KBUILD_CFLAGS += $(CFL)
 #
 CFLAGS_REMOVE_vdso-note.o = -pg -fprofile-arcs -ftest-coverage
 CFLAGS_REMOVE_um_vdso.o = -pg -fprofile-arcs -ftest-coverage
+CFLAGS_REMOVE_vclock_gettime.o = -pg -fprofile-arcs -ftest-coverage

 #
 # The DSO images are built using a special linker script.
diff --git a/arch/x86/um/vdso/vdso-layout.lds.S
b/arch/x86/um/vdso/vdso-layout.lds.S
index 439b790df..754d004b5 100644
--- a/arch/x86/um/vdso/vdso-layout.lds.S
+++ b/arch/x86/um/vdso/vdso-layout.lds.S
@@ -3,10 +3,32 @@
  * Linker script for vDSO.  This is an ELF shared object prelinked to
  * its virtual address, and with only one read-only segment.
  * This script controls its layout.
+ *
+ * Adapted from arch/x86/entry/vdso/common/vdso-layout.lds.S: same
+ * layout, without the pvclock/hvclock vclock pages (UML has none).
+ * The vvar (4 pages, __VDSO_PAGES) is placed before the vDSO text:
+ * VDSO_VVAR_SYMS is evaluated while '.' is the base of the PT_LOAD
+ * segment (VDSO_PRELINK), so vdso_u_data == text base - 4 pages, and
+ * the kernel maps [vvar] exactly there. Only the relative offset
+ * matters (the code is PIC), but it must match the kernel mapping.
  */

+#include <asm/vdso/vsyscall.h>
+#include <vdso/datapage.h>
+#include <vdso/page.h>	/* PAGE_SIZE for VDSO_VVAR_SYMS (asm mode) */
+
 SECTIONS
 {
+	/*
+	 * User/kernel shared data is before the vDSO.  This may be a little
+	 * uglier than putting it after the vDSO, but it avoids issues with
+	 * non-allocatable things that dangle past the end of the PT_LOAD
+	 * segment.
+	 */
+	. = VDSO_PRELINK;
+
+	VDSO_VVAR_SYMS
+
 	. = VDSO_PRELINK + SIZEOF_HEADERS;

 	.hash		: { *(.hash) }			:text
@@ -45,6 +67,12 @@ SECTIONS
 	. = ALIGN(0x100);

 	.text		: { *(.text*) }			:text	=0x90909090
+
+	/DISCARD/ : {
+		*(.discard)
+		*(.discard.*)
+		*(__bug_table)
+	}
 }

 /*
diff --git a/arch/x86/um/vdso/vdso.lds.S b/arch/x86/um/vdso/vdso.lds.S
index 401600eff..fcc99934e 100644
--- a/arch/x86/um/vdso/vdso.lds.S
+++ b/arch/x86/um/vdso/vdso.lds.S
@@ -24,6 +24,8 @@ VERSION {
 		__vdso_gettimeofday;
 		time;
 		__vdso_time;
+		clock_getres;
+		__vdso_clock_getres;
 	local: *;
 	};
 }
diff --git a/arch/x86/um/vdso/vma.c b/arch/x86/um/vdso/vma.c
index 8e1f5ee44..d36336266 100644
--- a/arch/x86/um/vdso/vma.c
+++ b/arch/x86/um/vdso/vma.c
@@ -1,38 +1,77 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (C) 2011 Richard Weinberger <richrd at nod.at>
+ *
+ * vDSO + vvar setup for UML.
+ *
+ * Layout at the top of the address space (um_vdso_addr stays the
+ * base of the vDSO *text*, so AT_SYSINFO_EHDR is unchanged):
+ *
+ *   um_vdso_addr - VDSO_NR_PAGES * PAGE_SIZE ... [vvar] (4 pages,
+ *       TIME/TIMENS/RNG/ARCH, installed via vdso_install_vvar_mapping)
+ *   um_vdso_addr ...                            [vdso] text, N pages
  */

 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/mm.h>
+#include <linux/vdso_datastore.h>
+#include <vdso/datapage.h>
 #include <asm/page.h>
 #include <asm/elf.h>
 #include <linux/init.h>
 #include <os.h>

 unsigned long um_vdso_addr;
-static struct page *um_vdso;
+/* NULL-terminated array of the vDSO text pages (special_mapping_fault
+ * walks it by pgoff, so the terminator is required).
+ */
+static struct page **um_vdso_pages;
+static unsigned long um_vdso_text_pages;

 extern unsigned long task_size;
 extern char vdso_start[], vdso_end[];

 static int __init init_vdso(void)
 {
-	BUG_ON(vdso_end - vdso_start > PAGE_SIZE);
+	unsigned long size = vdso_end - vdso_start;
+	unsigned long offset;
+	unsigned long i;
+
+	um_vdso_text_pages = DIV_ROUND_UP(size, PAGE_SIZE);
+
+	/* +1 for the NULL terminator used by special_mapping_fault(). */
+	um_vdso_pages = kmalloc_array(um_vdso_text_pages + 1,
+				      sizeof(*um_vdso_pages), GFP_KERNEL);
+	if (!um_vdso_pages)
+		panic("Cannot allocate vdso page array\n");

-	um_vdso = alloc_page(GFP_KERNEL);
-	if (!um_vdso)
-		panic("Cannot allocate vdso\n");
+	for (i = 0; i < um_vdso_text_pages; i++) {
+		um_vdso_pages[i] = alloc_page(GFP_KERNEL);
+		if (!um_vdso_pages[i])
+			panic("Cannot allocate vdso\n");
+	}
+	um_vdso_pages[um_vdso_text_pages] = NULL;

-	copy_page(page_address(um_vdso), vdso_start);
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		unsigned long chunk = min(PAGE_SIZE, size - offset);
+		char *dst = page_address(um_vdso_pages[offset / PAGE_SIZE]);
+
+		memcpy(dst, vdso_start + offset, chunk);
+		/* Zero the tail of a partial last page (alloc_page does
+		 * not zero).
+		 */
+		if (chunk < PAGE_SIZE)
+			memset(dst + chunk, 0, PAGE_SIZE - chunk);
+	}

 #ifdef CONFIG_MMU
-	um_vdso_addr = task_size - PAGE_SIZE;
+	um_vdso_addr = task_size - um_vdso_text_pages * PAGE_SIZE;
 #else
 	/* this is fine with NOMMU as everything is accessible */
-	um_vdso_addr = (unsigned long)page_address(um_vdso);
-	os_protect_memory((void *)um_vdso_addr, vdso_end - vdso_start, 1, 0, 1);
+	um_vdso_addr = (unsigned long)page_address(um_vdso_pages[0]);
+	for (i = 0; i < um_vdso_text_pages; i++)
+		os_protect_memory(page_address(um_vdso_pages[i]), PAGE_SIZE, 1, 0, 1);
 #endif

 	return 0;
@@ -44,21 +83,33 @@ int arch_setup_additional_pages(struct
linux_binprm *bprm, int uses_interp)
 {
 	struct vm_area_struct *vma;
 	struct mm_struct *mm = current->mm;
+	unsigned long vvar_addr = um_vdso_addr - VDSO_NR_PAGES * PAGE_SIZE;
 	static struct vm_special_mapping vdso_mapping = {
 		.name = "[vdso]",
-		.pages = &um_vdso,
 	};
+	int ret = 0;

 	if (mmap_write_lock_killable(mm))
 		return -EINTR;

-	vma = _install_special_mapping(mm, um_vdso_addr, PAGE_SIZE,
-		VM_READ|VM_EXEC|
-		VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
-		&vdso_mapping);
+	/* [vvar] first (below the text), then [vdso]. */
+	vma = vdso_install_vvar_mapping(mm, vvar_addr);
+	if (IS_ERR(vma)) {
+		ret = PTR_ERR(vma);
+		goto out;
+	}

-	mmap_write_unlock(mm);
+	vdso_mapping.pages = um_vdso_pages;
+	vma = _install_special_mapping(mm, um_vdso_addr,
+				       um_vdso_text_pages * PAGE_SIZE,
+				       VM_READ | VM_EXEC |
+				       VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
+				       &vdso_mapping);
+	if (IS_ERR(vma))
+		ret = PTR_ERR(vma);

-	return IS_ERR(vma) ? PTR_ERR(vma) : 0;
+out:
+	mmap_write_unlock(mm);
+	return ret;
 }
 #endif /* CONFIG_MMU */
-- 
2.43.0



More information about the linux-um mailing list