Skip to contentSkip to conversation

Add generic vdso_base tracking

PATCH Superseded Dmitry Safonov 4 participants 37 messages Nov 24, 2020 – Nov 26, 2020

Subsystems (23)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add generic vdso_base tracking

v2 Changes:
- Rename user_landing to vdso_base as it tracks vDSO VMA start address, rather than the explicit address to land (Andy)
- Reword and don't use "new-execed" and "new-born" task (Andy)
- Fix failures reported by build robot

Started from discussion [1], where was noted that currently a couple of architectures support mremap() for vdso/sigpage, but not munmap(). If an application maps something on the ex-place of vdso/sigpage, later after processing signal it will land there (good luck!)

Patches set is based on linux-next (next-20201123) and it depends on changes in x86/cleanups (those reclaim TIF_IA32/TIF_X32) and also on my changes in akpm (fixing several mremap() issues).

Logically, the patches set divides on:
- patch 1: a cleanup for patches in x86/cleanups
- patches 2-11: cleanups for arch_setup_additional_pages()
- patches 12-13: x86 signal changes for unmapped vdso
- patches 14-19: provide generic vdso_base in mm_struct

In the end, besides cleanups, it's now more predictable what happens for applications with unmapped vdso on architectures those support .mremap() for vdso/sigpage.

I'm aware of only one user that unmaps vdso - Valgrind [2]. (there possibly are more, but this one is "special", it unmaps vdso, but
not vvar, which confuses CRIU [Checkpoint Restore In Userspace], that's why I'm aware of it)

Patches as a .git branch:
https://github.com/0x7f454c46/linux/tree/setup_additional_pages-v2

v1 Link:
https://lore.kernel.org/lkml/[email protected]/

[1]: https://lore.kernel.org/linux-arch/CAJwJo6ZANqYkSHbQ+3b+Fi_VT80MtrzEV5yreQAWx-L8j8x2zA@mail.gmail.com/
[2]: https://github.com/checkpoint-restore/criu/issues/488

Cc Alexander Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Christophe Leroy <[email protected]>
Cc Guo Ren <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Russell King <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Will Deacon <[email protected]>

Dmitry Safonov (19):
x86/elf: Check in_x32_syscall() in compat_arch_setup_additional_pages()
elf: Move arch_setup_additional_pages() to generic elf.h
arm64: Use in_compat_task() in arch_setup_additional_pages()
x86: Remove compat_arch_setup_additional_pages()
elf: Remove compat_arch_setup_additional_pages()
elf/vdso: Reuse arch_setup_additional_pages() parameters
elf: Use sysinfo_ehdr in ARCH_DLINFO()
arm/vdso: Remove vdso pointer from mm->context
s390/vdso: Remove vdso_base pointer from mm->context sparc/vdso: Remove vdso pointer from mm->context
mm/mmap: Make vm_special_mapping::mremap return void x86/signal: Land on &frame->retcode when vdso isn't mapped x86/signal: Check if vdso_image_32 is mapped before trying to land on it
mm: Add vdso_base in mm_struct
x86/vdso: Migrate to generic vdso_base
arm/vdso: Migrate to generic vdso_base
arm64/vdso: Migrate compat signals to generic vdso_base arm64/vdso: Migrate native signals to generic vdso_base mips/vdso: Migrate to generic vdso_base

 arch/alpha/include/asm/elf.h              |  2 +-
 arch/arm/Kconfig                          |  2 +
 arch/arm/include/asm/elf.h                | 10 +---
 arch/arm/include/asm/mmu.h                |  3 -
 arch/arm/include/asm/vdso.h               |  6 +-
 arch/arm/kernel/process.c                 | 14 +----
 arch/arm/kernel/signal.c                  |  6 +-
 arch/arm/kernel/vdso.c                    | 20 ++-----
 arch/arm64/Kconfig                        |  2 +
 arch/arm64/include/asm/elf.h              | 27 ++-------
 arch/arm64/kernel/signal.c                | 10 +++-
 arch/arm64/kernel/signal32.c              | 17 ++++--
 arch/arm64/kernel/vdso.c                  | 50 +++++++---------
 arch/csky/Kconfig                         |  1 +
 arch/csky/include/asm/elf.h               |  4 --
 arch/csky/kernel/vdso.c                   |  3 +-
 arch/hexagon/Kconfig                      |  1 +
 arch/hexagon/include/asm/elf.h            |  6 --
 arch/hexagon/kernel/vdso.c                |  3 +-
 arch/ia64/include/asm/elf.h               |  2 +-
 arch/mips/Kconfig                         |  2 +
 arch/mips/include/asm/elf.h               | 10 +---
 arch/mips/kernel/signal.c                 | 11 ++--
 arch/mips/kernel/vdso.c                   |  5 +-
 arch/mips/vdso/genvdso.c                  |  9 ---
 arch/nds32/Kconfig                        |  1 +
 arch/nds32/include/asm/elf.h              |  8 +--
 arch/nds32/kernel/vdso.c                  |  3 +-
 arch/nios2/Kconfig                        |  1 +
 arch/nios2/include/asm/elf.h              |  4 --
 arch/nios2/mm/init.c                      |  2 +-
 arch/powerpc/Kconfig                      |  1 +
 arch/powerpc/include/asm/elf.h            |  9 +--
 arch/powerpc/kernel/vdso.c                |  3 +-
 arch/riscv/Kconfig                        |  1 +
 arch/riscv/include/asm/elf.h              |  9 +--
 arch/riscv/kernel/vdso.c                  | 11 ++--
 arch/s390/Kconfig                         |  1 +
 arch/s390/include/asm/elf.h               | 10 +---
 arch/s390/include/asm/mmu.h               |  1 -
 arch/s390/kernel/vdso.c                   | 13 +---
 arch/sh/Kconfig                           |  1 +
 arch/sh/include/asm/elf.h                 | 16 ++---
 arch/sh/kernel/vsyscall/vsyscall.c        |  3 +-
 arch/sparc/Kconfig                        |  1 +
 arch/sparc/include/asm/elf_64.h           | 11 +---
 arch/sparc/include/asm/mmu_64.h           |  1 -
 arch/sparc/vdso/vma.c                     | 18 +++---
 arch/x86/Kconfig                          |  2 +
 arch/x86/entry/common.c                   |  7 ++-
 arch/x86/entry/vdso/extable.c             |  4 +-
 arch/x86/entry/vdso/vma.c                 | 72 ++++++++++++-----------
 arch/x86/ia32/ia32_signal.c               | 18 +++---
 arch/x86/include/asm/compat.h             |  6 ++
 arch/x86/include/asm/elf.h                | 44 +++++---------
 arch/x86/include/asm/mmu.h                |  1 -
 arch/x86/include/asm/vdso.h               |  4 ++
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  3 +-
 arch/x86/kernel/signal.c                  | 25 ++++----
 arch/x86/um/asm/elf.h                     |  9 +--
 arch/x86/um/vdso/vma.c                    |  2 +-
 fs/Kconfig.binfmt                         |  3 +
 fs/aio.c                                  |  3 +-
 fs/binfmt_elf.c                           | 19 +++---
 fs/binfmt_elf_fdpic.c                     | 17 +++---
 fs/compat_binfmt_elf.c                    | 12 ----
 include/linux/elf.h                       | 24 ++++++--
 include/linux/mm.h                        |  3 +-
 include/linux/mm_types.h                  | 12 +++-
 mm/Kconfig                                |  3 +
 mm/mmap.c                                 | 21 ++++++-
 mm/mremap.c                               |  2 +-
 72 files changed, 313 insertions(+), 358 deletions(-)

base-commit: 62918e6fd7b5751c1285c7f8c6cbd27eb6600c02

Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 1/19 x86/elf: Check in_x32_syscall() in compat_arch_setup_additional_pages()

Partly revert commit 3316ec8ccd34 ("x86/elf: Use e_machine to check for x32/ia32 in setup_additional_pages()") and commit 9a29a671902c ("elf:
Expose ELF header on arch_setup_additional_pages()".
Both patches did a good thing: removed usage of TIF_X32, but with a price of additional macros ARCH_SETUP_ADDITIONAL_PAGES() and ifdeffs.

Instead, use in_x32_syscall() - the first thing load_elf_binary() does after parsing and checking new ELF binary. It's done that early after exec() that way also allows to use it in mmap() code straight away, which needs it to know which mmap_base to use (see arch_pick_mmap_layout()). Add comments that describe how it works.

---
 arch/x86/entry/vdso/vma.c     |  4 ++--
 arch/x86/include/asm/compat.h |  6 ++++++
 arch/x86/include/asm/elf.h    |  6 ++----
 fs/binfmt_elf.c               | 10 +++++++---
 fs/compat_binfmt_elf.c        | 11 +++--------
 include/linux/elf.h           |  5 -----
 6 files changed, 20 insertions(+), 22 deletions(-)
arch/x86/entry/vdso/vma.c +2 -2
index 44de75b21fab..4eea508e9b10 100644
@@ -396,10 +396,10 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 
 #ifdef CONFIG_COMPAT
 int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp, bool x32)
+				       int uses_interp)
 {
 #ifdef CONFIG_X86_X32_ABI
-	if (x32) {
+	if (in_x32_syscall()) {
 		if (!vdso64_enabled)
 			return 0;
 		return map_vdso_randomized(&vdso_image_x32);
arch/x86/include/asm/compat.h +6 -0
index f145e3326c6d..4489bd60640b 100644
@@ -197,6 +197,12 @@ static inline bool in_x32_syscall(void)
 	return false;
 }
 
+/*
+ * Valid all time on the context of process that performs a syscall.
+ * Just born process has __X32_SYSCALL_BIT or TS_COMPAT set very
+ * early in load_binary() on setting personality and flags.
+ * See also set_personality_ia32().
+ */
 static inline bool in_32bit_syscall(void)
 {
 	return in_ia32_syscall() || in_x32_syscall();
arch/x86/include/asm/elf.h +2 -4
index 44a9b9940535..109697a19eb1 100644
@@ -383,10 +383,8 @@ struct linux_binprm;
 extern int arch_setup_additional_pages(struct linux_binprm *bprm,
 				       int uses_interp);
 extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
-					      int uses_interp, bool x32);
-#define COMPAT_ARCH_SETUP_ADDITIONAL_PAGES(bprm, ex, interpreter)	\
-	compat_arch_setup_additional_pages(bprm, interpreter,		\
-					   (ex->e_machine == EM_X86_64))
+					      int uses_interp);
+#define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
 
 /* Do not change the values. See get_align_mask() */
 enum align_flags {
fs/binfmt_elf.c +7 -3
index ac0b5fc30ea6..3de72c0e0406 100644
@@ -999,8 +999,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	if (retval)
 		goto out_free_dentry;
 
-	/* Do this immediately, since STACK_TOP as used in setup_arg_pages
-	   may depend on the personality.  */
+	/*
+	 * Do this immediately, since STACK_TOP as used in setup_arg_pages
+	 * may depend on the personality. At this moment we start
+	 * pretending that we are in a context of compat syscall for
+	 * compatible applications on x86, in_compat_syscall() starts working.
+	 */
 	SET_PERSONALITY2(*elf_ex, &arch_state);
 	if (elf_read_implies_exec(*elf_ex, executable_stack))
 		current->personality |= READ_IMPLIES_EXEC;
@@ -1246,7 +1250,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	set_binfmt(&elf_format);
 
 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
-	retval = ARCH_SETUP_ADDITIONAL_PAGES(bprm, elf_ex, !!interpreter);
+	retval = arch_setup_additional_pages(bprm, !!interpreter);
 	if (retval < 0)
 		goto out;
 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
fs/compat_binfmt_elf.c +3 -8
index 2c557229696a..12b991368f0a 100644
@@ -115,16 +115,11 @@
 #define START_THREAD		COMPAT_START_THREAD
 #endif
 
-#ifdef compat_arch_setup_additional_pages
-#define COMPAT_ARCH_SETUP_ADDITIONAL_PAGES(bprm, ex, interpreter) \
-	compat_arch_setup_additional_pages(bprm, interpreter)
-#endif
-
-#ifdef	COMPAT_ARCH_SETUP_ADDITIONAL_PAGES
+#ifdef	compat_arch_setup_additional_pages
 #undef	ARCH_HAS_SETUP_ADDITIONAL_PAGES
 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-#undef	ARCH_SETUP_ADDITIONAL_PAGES
-#define	ARCH_SETUP_ADDITIONAL_PAGES COMPAT_ARCH_SETUP_ADDITIONAL_PAGES
+#undef	arch_setup_additional_pages
+#define	arch_setup_additional_pages compat_arch_setup_additional_pages
 #endif
 
 #ifdef	compat_elf_read_implies_exec
include/linux/elf.h +0 -5
index c9a46c4e183b..6dbcfe7a3fd7 100644
@@ -27,11 +27,6 @@
 	start_thread(regs, elf_entry, start_stack)
 #endif
 
-#if defined(ARCH_HAS_SETUP_ADDITIONAL_PAGES) && !defined(ARCH_SETUP_ADDITIONAL_PAGES)
-#define ARCH_SETUP_ADDITIONAL_PAGES(bprm, ex, interpreter) \
-	arch_setup_additional_pages(bprm, interpreter)
-#endif
-
 #define ELF32_GNU_PROPERTY_ALIGN	4
 #define ELF64_GNU_PROPERTY_ALIGN	8
 
Signature · 1 line
2.29.2
#2 lore raw

PATCH v2 2/19 elf: Move arch_setup_additional_pages() to generic elf.h

In reply to Dmitry Safonov

Ifdef the function in the header, not in the code. Following kernel style, move it to Kconfig.
All it makes it easier to follow when the option is enabled/disabled. Remove re-definition from compat_binfmt_elf, as it's always defined under compat_arch_setup_additional_pages (to be reworked).

---
 arch/arm/Kconfig                |  1 +
 arch/arm/include/asm/elf.h      |  5 -----
 arch/arm64/Kconfig              |  1 +
 arch/arm64/include/asm/elf.h    |  6 +-----
 arch/csky/Kconfig               |  1 +
 arch/csky/include/asm/elf.h     |  4 ----
 arch/hexagon/Kconfig            |  1 +
 arch/hexagon/include/asm/elf.h  |  6 ------
 arch/mips/Kconfig               |  1 +
 arch/mips/include/asm/elf.h     |  5 -----
 arch/nds32/Kconfig              |  1 +
 arch/nds32/include/asm/elf.h    |  3 ---
 arch/nios2/Kconfig              |  1 +
 arch/nios2/include/asm/elf.h    |  4 ----
 arch/powerpc/Kconfig            |  1 +
 arch/powerpc/include/asm/elf.h  |  5 -----
 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/elf.h    |  4 ----
 arch/s390/Kconfig               |  1 +
 arch/s390/include/asm/elf.h     |  5 -----
 arch/sh/Kconfig                 |  1 +
 arch/sh/include/asm/elf.h       |  6 ------
 arch/sparc/Kconfig              |  1 +
 arch/sparc/include/asm/elf_64.h |  6 ------
 arch/x86/Kconfig                |  1 +
 arch/x86/include/asm/elf.h      |  4 ----
 arch/x86/um/asm/elf.h           |  5 -----
 fs/Kconfig.binfmt               |  3 +++
 fs/binfmt_elf.c                 |  2 --
 fs/binfmt_elf_fdpic.c           |  3 +--
 fs/compat_binfmt_elf.c          |  2 --
 include/linux/elf.h             | 12 ++++++++++++
 32 files changed, 30 insertions(+), 73 deletions(-)
arch/arm/Kconfig +1 -0
index 3b1caaefbff0..a6330e7eb2b8 100644
@@ -15,6 +15,7 @@ config ARM
 	select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
 	select ARCH_HAS_PTE_SPECIAL if ARM_LPAE
 	select ARCH_HAS_PHYS_TO_DMA
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES if MMU
 	select ARCH_HAS_SETUP_DMA_OPS
 	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_STRICT_KERNEL_RWX if MMU && !XIP_KERNEL
arch/arm/include/asm/elf.h +0 -5
index b8102a6ddf16..47347d7412ec 100644
@@ -137,7 +137,6 @@ extern int arm_elf_read_implies_exec(int);
 extern void elf_set_personality(const struct elf32_hdr *);
 #define SET_PERSONALITY(ex)	elf_set_personality(&(ex))
 
-#ifdef CONFIG_MMU
 #ifdef CONFIG_VDSO
 #define ARCH_DLINFO						\
 do {								\
@@ -145,9 +144,5 @@ do {								\
 		    (elf_addr_t)current->mm->context.vdso);	\
 } while (0)
 #endif
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-int arch_setup_additional_pages(struct linux_binprm *, int);
-#endif
 
 #endif
arch/arm64/Kconfig +1 -0
index 629a293cc408..35d8e4acd38d 100644
@@ -26,6 +26,7 @@ config ARM64
 	select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
 	select ARCH_HAS_PTE_DEVMAP
 	select ARCH_HAS_PTE_SPECIAL
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SETUP_DMA_OPS
 	select ARCH_HAS_SET_DIRECT_MAP
 	select ARCH_HAS_SET_MEMORY
arch/arm64/include/asm/elf.h +1 -5
index 8d1c8dcb87fd..d1073ffa7f24 100644
@@ -181,11 +181,6 @@ do {									\
 		NEW_AUX_ENT(AT_IGNORE, 0);				\
 } while (0)
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
-
 /* 1GB of VA */
 #ifdef CONFIG_COMPAT
 #define STACK_RND_MASK			(test_thread_flag(TIF_32BIT) ? \
@@ -242,6 +237,7 @@ do {									\
 #else
 #define COMPAT_ARCH_DLINFO
 #endif
+struct linux_binprm;
 extern int aarch32_setup_additional_pages(struct linux_binprm *bprm,
 					  int uses_interp);
 #define compat_arch_setup_additional_pages \
arch/csky/Kconfig +1 -0
index 21b2ab099c8b..f068d67456a2 100644
@@ -4,6 +4,7 @@ config CSKY
 	select ARCH_32BIT_OFF_T
 	select ARCH_HAS_DMA_PREP_COHERENT
 	select ARCH_HAS_GCOV_PROFILE_ALL
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SYNC_DMA_FOR_CPU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_USE_BUILTIN_BSWAP
arch/csky/include/asm/elf.h +0 -4
index eb2cc5a673b5..8ec675f8d0d9 100644
@@ -84,8 +84,4 @@ extern int dump_task_regs(struct task_struct *tsk, elf_gregset_t *elf_regs);
 #define ELF_PLATFORM		(NULL)
 #define SET_PERSONALITY(ex)	set_personality(PER_LINUX)
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
 #endif /* __ASM_CSKY_ELF_H */
arch/hexagon/Kconfig +1 -0
index 6e00c16a36b5..74bc280523e4 100644
@@ -5,6 +5,7 @@ comment "Linux Kernel Configuration for Hexagon"
 config HEXAGON
 	def_bool y
 	select ARCH_32BIT_OFF_T
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_NO_PREEMPT
 	select HAVE_OPROFILE
arch/hexagon/include/asm/elf.h +0 -6
index 5bfdd9b147fd..eba4131610aa 100644
@@ -207,10 +207,4 @@ do {					\
  */
 #define ELF_PLATFORM  (NULL)
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
-
-
 #endif
arch/mips/Kconfig +1 -0
index 74e8af4931db..818a9b2c78f1 100644
@@ -7,6 +7,7 @@ config MIPS
 	select ARCH_HAS_FORTIFY_SOURCE
 	select ARCH_HAS_KCOV
 	select ARCH_HAS_PTE_SPECIAL if !(32BIT && CPU_HAS_RIXI)
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HAS_UBSAN_SANITIZE_ALL
 	select ARCH_SUPPORTS_UPROBES
arch/mips/include/asm/elf.h +0 -5
index 71c7622025d1..6665cf7870b3 100644
@@ -480,11 +480,6 @@ do {									\
 		    (unsigned long)current->mm->context.vdso);		\
 } while (0)
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
-
 #ifdef CONFIG_MIPS_FP_SUPPORT
 
 struct arch_elf_state {
arch/nds32/Kconfig +1 -0
index 62313902d75d..02afe5ebdfff 100644
@@ -8,6 +8,7 @@ config NDS32
 	def_bool y
 	select ARCH_32BIT_OFF_T
 	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SYNC_DMA_FOR_CPU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_WANT_FRAME_POINTERS if FTRACE
arch/nds32/include/asm/elf.h +0 -3
index 1853dc89b8ac..36cec4ae5a84 100644
@@ -173,8 +173,5 @@ do {								\
 	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
 		    (elf_addr_t)current->mm->context.vdso);	\
 } while (0)
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-int arch_setup_additional_pages(struct linux_binprm *, int);
 
 #endif
arch/nios2/Kconfig +1 -0
index c24955c81c92..8159123a995e 100644
@@ -3,6 +3,7 @@ config NIOS2
 	def_bool y
 	select ARCH_32BIT_OFF_T
 	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SYNC_DMA_FOR_CPU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_HAS_DMA_SET_UNCACHED
arch/nios2/include/asm/elf.h +0 -4
index 984dd6de17c2..4f8baaef843f 100644
@@ -28,10 +28,6 @@
 /* regs is struct pt_regs, pr_reg is elf_gregset_t (which is
    now struct_user_regs, they are different) */
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES	1
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-	int uses_interp);
 #define ELF_CORE_COPY_REGS(pr_reg, regs)				\
 { do {									\
 	/* Bleech. */							\
arch/powerpc/Kconfig +1 -0
index 6fd7d38a60c8..4221f171d1a9 100644
@@ -134,6 +134,7 @@ config PPC
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_MEMBARRIER_CALLBACKS
 	select ARCH_HAS_MEMBARRIER_SYNC_CORE
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_SCALED_CPUTIME		if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
 	select ARCH_HAS_STRICT_KERNEL_RWX	if (PPC32 && !HIBERNATION)
 	select ARCH_HAS_TICK_BROADCAST		if GENERIC_CLOCKEVENTS_BROADCAST
arch/powerpc/include/asm/elf.h +0 -5
index 53ed2ca40151..ba0e1e331088 100644
@@ -111,11 +111,6 @@ extern int dcache_bsize;
 extern int icache_bsize;
 extern int ucache_bsize;
 
-/* vDSO has arch_setup_additional_pages */
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
 #define VDSO_AUX_ENT(a,b) NEW_AUX_ENT(a,b)
 
 /* 1GB for 64bit, 8MB for 32bit */
arch/riscv/Kconfig +1 -0
index e7f55e43f42f..db4da8147583 100644
@@ -26,6 +26,7 @@ config RISCV
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_SET_DIRECT_MAP
 	select ARCH_HAS_SET_MEMORY
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES if MMU
 	select ARCH_HAS_STRICT_KERNEL_RWX if MMU
 	select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX
 	select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
arch/riscv/include/asm/elf.h +0 -4
index 5c725e1df58b..5e2543673e42 100644
@@ -75,10 +75,6 @@ do {								\
 	NEW_AUX_ENT(AT_L2_CACHEGEOMETRY,			\
 		get_cache_geometry(2, CACHE_TYPE_UNIFIED));	\
 } while (0)
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-	int uses_interp);
 #endif /* CONFIG_MMU */
 
 #endif /* _ASM_RISCV_ELF_H */
arch/s390/Kconfig +1 -0
index 0eebaf40c259..f8e3c5dcb572 100644
@@ -66,6 +66,7 @@ config S390
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_SET_MEMORY
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_STRICT_KERNEL_RWX
 	select ARCH_HAS_STRICT_MODULE_RWX
 	select ARCH_HAS_SYSCALL_WRAPPER
arch/s390/include/asm/elf.h +0 -5
index 5775fc22f410..f6ce9df8a996 100644
@@ -276,9 +276,4 @@ do {									    \
 			    (unsigned long)current->mm->context.vdso_base); \
 } while (0)
 
-struct linux_binprm;
-
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-int arch_setup_additional_pages(struct linux_binprm *, int);
-
 #endif
arch/sh/Kconfig +1 -0
index 5fa580219a86..c0bae180d8b8 100644
@@ -8,6 +8,7 @@ config SUPERH
 	select ARCH_HAS_GIGANTIC_PAGE
 	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_HAS_PTE_SPECIAL
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES if VSYSCALL
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HIBERNATION_POSSIBLE if MMU
 	select ARCH_MIGHT_HAVE_PC_PARPORT
arch/sh/include/asm/elf.h +0 -6
index 2862d6d1cb64..9b3e22e771a1 100644
@@ -164,12 +164,6 @@ do {									\
 	set_personality(PER_LINUX_32BIT | (current->personality & (~PER_MASK)))
 
 #ifdef CONFIG_VSYSCALL
-/* vDSO has arch_setup_additional_pages */
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
-
 extern unsigned int vdso_enabled;
 extern void __kernel_vsyscall;
 
arch/sparc/Kconfig +1 -0
index da6e6f0d71b2..c53d117f73aa 100644
@@ -13,6 +13,7 @@ config 64BIT
 config SPARC
 	bool
 	default y
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES if SPARC64
 	select ARCH_MIGHT_HAVE_PC_PARPORT if SPARC64 && PCI
 	select ARCH_MIGHT_HAVE_PC_SERIO
 	select DMA_OPS
arch/sparc/include/asm/elf_64.h +0 -6
index 8fb09eec8c3e..63a622c36df3 100644
@@ -223,10 +223,4 @@ do {									\
 	NEW_AUX_ENT(AT_ADI_NBITS, adi_state.caps.nbits);		\
 	NEW_AUX_ENT(AT_ADI_UEONADI, adi_state.caps.ue_on_adi);		\
 } while (0)
-
-struct linux_binprm;
-
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES	1
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-					int uses_interp);
 #endif /* !(__ASM_SPARC64_ELF_H) */
arch/x86/Kconfig +1 -0
index 6d7c133e2025..d676e0bad1f1 100644
@@ -79,6 +79,7 @@ config X86
 	select ARCH_HAS_COPY_MC			if X86_64
 	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_SET_DIRECT_MAP
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_STRICT_KERNEL_RWX
 	select ARCH_HAS_STRICT_MODULE_RWX
 	select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
arch/x86/include/asm/elf.h +0 -4
index 109697a19eb1..d00b723eea2d 100644
@@ -378,10 +378,6 @@ else									\
 	 vdso_image_32.sym___kernel_vsyscall)
 
 struct linux_binprm;
-
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
 extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
 					      int uses_interp);
 #define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
arch/x86/um/asm/elf.h +0 -5
index c907b20d4993..7bc1d9d94d66 100644
@@ -181,11 +181,6 @@ do {								\
 #define FIXADDR_USER_START      0
 #define FIXADDR_USER_END        0
 
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-	int uses_interp);
-
 extern unsigned long um_vdso_addr;
 #define AT_SYSINFO_EHDR 33
 #define ARCH_DLINFO	NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr)
fs/Kconfig.binfmt +3 -0
index 885da6d983b4..4ed2ab0a1565 100644
@@ -39,6 +39,9 @@ config ARCH_BINFMT_ELF_STATE
 config ARCH_HAVE_ELF_PROT
 	bool
 
+config ARCH_HAS_SETUP_ADDITIONAL_PAGES
+	bool
+
 config ARCH_USE_GNU_PROPERTY
 	bool
 
fs/binfmt_elf.c +0 -2
index 3de72c0e0406..b9adbeb59101 100644
@@ -1249,11 +1249,9 @@ static int load_elf_binary(struct linux_binprm *bprm)
 
 	set_binfmt(&elf_format);
 
-#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	retval = arch_setup_additional_pages(bprm, !!interpreter);
 	if (retval < 0)
 		goto out;
-#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
 
 	retval = create_elf_tables(bprm, elf_ex,
 			  load_addr, interp_load_addr, e_entry);
fs/binfmt_elf_fdpic.c +1 -2
index be4062b8ba75..b9a6d1b2b5bb 100644
@@ -374,11 +374,10 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 				 executable_stack);
 	if (retval < 0)
 		goto error;
-#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
+
 	retval = arch_setup_additional_pages(bprm, !!interpreter_name);
 	if (retval < 0)
 		goto error;
-#endif
 #endif
 
 	/* load the executable and interpreter into memory */
fs/compat_binfmt_elf.c +0 -2
index 12b991368f0a..3606dd3a32f5 100644
@@ -116,8 +116,6 @@
 #endif
 
 #ifdef	compat_arch_setup_additional_pages
-#undef	ARCH_HAS_SETUP_ADDITIONAL_PAGES
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
 #undef	arch_setup_additional_pages
 #define	arch_setup_additional_pages compat_arch_setup_additional_pages
 #endif
include/linux/elf.h +12 -0
index 6dbcfe7a3fd7..95bf7a1abaef 100644
@@ -104,4 +104,16 @@ static inline int arch_elf_adjust_prot(int prot,
 }
 #endif
 
+struct linux_binprm;
+#ifdef CONFIG_ARCH_HAS_SETUP_ADDITIONAL_PAGES
+extern int arch_setup_additional_pages(struct linux_binprm *bprm,
+				       int uses_interp);
+#else
+static inline int arch_setup_additional_pages(struct linux_binprm *bprm,
+				       int uses_interp)
+{
+	return 0;
+}
+#endif
+
 #endif /* _LINUX_ELF_H */
Signature · 1 line
2.29.2
#3 lore raw

Re: [PATCH v2 02/19] elf: Move arch_setup_additional_pages() to generic elf.h

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 47 lines

Ifdef the function in the header, not in the code. Following kernel style, move it to Kconfig.
All it makes it easier to follow when the option is enabled/disabled. Remove re-definition from compat_binfmt_elf, as it's always defined under compat_arch_setup_additional_pages (to be reworked).

---
  arch/arm/Kconfig                |  1 +
  arch/arm/include/asm/elf.h      |  5 -----
  arch/arm64/Kconfig              |  1 +
  arch/arm64/include/asm/elf.h    |  6 +-----
  arch/csky/Kconfig               |  1 +
  arch/csky/include/asm/elf.h     |  4 ----
  arch/hexagon/Kconfig            |  1 +
  arch/hexagon/include/asm/elf.h  |  6 ------
  arch/mips/Kconfig               |  1 +
  arch/mips/include/asm/elf.h     |  5 -----
  arch/nds32/Kconfig              |  1 +
  arch/nds32/include/asm/elf.h    |  3 ---
  arch/nios2/Kconfig              |  1 +
  arch/nios2/include/asm/elf.h    |  4 ----
  arch/powerpc/Kconfig            |  1 +
  arch/powerpc/include/asm/elf.h  |  5 -----
  arch/riscv/Kconfig              |  1 +
  arch/riscv/include/asm/elf.h    |  4 ----
  arch/s390/Kconfig               |  1 +
  arch/s390/include/asm/elf.h     |  5 -----
  arch/sh/Kconfig                 |  1 +
  arch/sh/include/asm/elf.h       |  6 ------
  arch/sparc/Kconfig              |  1 +
  arch/sparc/include/asm/elf_64.h |  6 ------
  arch/x86/Kconfig                |  1 +
  arch/x86/include/asm/elf.h      |  4 ----
  arch/x86/um/asm/elf.h           |  5 -----
  fs/Kconfig.binfmt               |  3 +++
  fs/binfmt_elf.c                 |  2 --
  fs/binfmt_elf_fdpic.c           |  3 +--
  fs/compat_binfmt_elf.c          |  2 --
  include/linux/elf.h             | 12 ++++++++++++
  32 files changed, 30 insertions(+), 73 deletions(-)
arch/powerpc/Kconfig +0 -0
index 6fd7d38a60c8..4221f171d1a9 100644

arch/powerpc/Kconfig config PPC · line 137

Quoted 4 lines
arch/powerpc/Kconfig +1 -0
@@ -134,6 +134,7 @@ config PPC
  	select ARCH_HAS_PTE_SPECIAL
  	select ARCH_HAS_MEMBARRIER_CALLBACKS
  	select ARCH_HAS_MEMBARRIER_SYNC_CORE
+	select ARCH_HAS_SETUP_ADDITIONAL_PAGES

We try to keep alphabetic order on powerpc, should go after ARCH_HAS_SCALED_CPUTIME

Quoted 9 lines
arch/powerpc/Kconfig +0 -0
  	select ARCH_HAS_SCALED_CPUTIME		if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
  	select ARCH_HAS_STRICT_KERNEL_RWX	if (PPC32 && !HIBERNATION)
  	select ARCH_HAS_TICK_BROADCAST		if GENERIC_CLOCKEVENTS_BROADCAST
arch/powerpc/include/asm/elf.h +0 -0
index 53ed2ca40151..ba0e1e331088 100644
@@ -111,11 +111,6 @@ extern int dcache_bsize;
  extern int icache_bsize;

arch/powerpc/include/asm/elf.h extern int dcache_bsize; · line 116

Quoted 10 lines
arch/powerpc/include/asm/elf.h +0 -5
  extern int ucache_bsize;
 
-/* vDSO has arch_setup_additional_pages */
-#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
-struct linux_binprm;
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
  #define VDSO_AUX_ENT(a,b) NEW_AUX_ENT(a,b)
 
  /* 1GB for 64bit, 8MB for 32bit */

Christophe

#4 lore raw

Re: [PATCH v2 02/19] elf: Move arch_setup_additional_pages() to generic elf.h

On 11/24/20 6:12 AM, Christophe Leroy wrote:
[..]

Quoted 4 lines
Quoted 4 lines
arch/powerpc/Kconfig +0 -0
index 6fd7d38a60c8..4221f171d1a9 100644
Quoted 8 lines
Quoted 5 lines
arch/powerpc/Kconfig +0 -0
@@ -134,6 +134,7 @@ config PPC

      select ARCH_HAS_PTE_SPECIAL
      select ARCH_HAS_MEMBARRIER_CALLBACKS
      select ARCH_HAS_MEMBARRIER_SYNC_CORE
+    select ARCH_HAS_SETUP_ADDITIONAL_PAGES

We try to keep alphabetic order on powerpc, should go after ARCH_HAS_SCALED_CPUTIME

Yep, I think I have to learn the alphabet.

Thanks for noticing,
Dmitry

#5 lore raw

PATCH v2 3/19 arm64: Use in_compat_task() in arch_setup_additional_pages()

In reply to Dmitry Safonov

Instead of providing compat_arch_setup_additional_pages(), check if the task is compatible from personality, which is set earlier in load_elf_binary(). That will align code with powerpc and sparc, also it'll allow to completely remove compat_arch_setyp_addtional_pages() macro after doing the same for x86, simiplifying the binfmt code in the end.

---
 arch/arm64/include/asm/elf.h |  5 -----
 arch/arm64/kernel/vdso.c     | 21 ++++++++++-----------
 2 files changed, 10 insertions(+), 16 deletions(-)
arch/arm64/include/asm/elf.h +0 -5
index d1073ffa7f24..a81953bcc1cf 100644
@@ -237,11 +237,6 @@ do {									\
 #else
 #define COMPAT_ARCH_DLINFO
 #endif
-struct linux_binprm;
-extern int aarch32_setup_additional_pages(struct linux_binprm *bprm,
-					  int uses_interp);
-#define compat_arch_setup_additional_pages \
-					aarch32_setup_additional_pages
 
 #endif /* CONFIG_COMPAT */
 
arch/arm64/kernel/vdso.c +10 -11
index cee5d04ea9ad..1b710deb84d6 100644
@@ -401,29 +401,24 @@ static int aarch32_sigreturn_setup(struct mm_struct *mm)
 	return PTR_ERR_OR_ZERO(ret);
 }
 
-int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+static int aarch32_setup_additional_pages(struct linux_binprm *bprm,
+					  int uses_interp)
 {
 	struct mm_struct *mm = current->mm;
 	int ret;
 
-	if (mmap_write_lock_killable(mm))
-		return -EINTR;
-
 	ret = aarch32_kuser_helpers_setup(mm);
 	if (ret)
-		goto out;
+		return ret;
 
 	if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
 		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
 					       uses_interp);
 		if (ret)
-			goto out;
+			return ret;
 	}
 
-	ret = aarch32_sigreturn_setup(mm);
-out:
-	mmap_write_unlock(mm);
-	return ret;
+	return aarch32_sigreturn_setup(mm);
 }
 #endif /* CONFIG_COMPAT */
 
@@ -460,7 +455,11 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	if (mmap_write_lock_killable(mm))
 		return -EINTR;
 
-	ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
+	if (is_compat_task())
+		ret = aarch32_setup_additional_pages(bprm, uses_interp);
+	else
+		ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
+
 	mmap_write_unlock(mm);
 
 	return ret;
Signature · 1 line
2.29.2
#6 lore raw

PATCH v2 4/19 x86: Remove compat_arch_setup_additional_pages()

In reply to Dmitry Safonov

The same as for x32 task, detect ia32 task by in_ia32_syscall(). It's valid as execed task is pretending to be in a syscall of relevant bitness/ABI, see the comment near in_32bit_syscall().

Removing compat_arch_setup_additional_pages() provides single point of entry - arch_setup_additional_pages(), makes ifdeffery easier to read, aligns the code with powerpc and sparc (mips also has single vdso setup function, but instead of taking bitness from mm.context, takes vdso image pointer there).
Together with arm64 code align to use in_compat_task(), it makes possible to remove compat_arch_setup_additional_pages() macro re-definition from compat elf code (another redefined marco less).

---
 arch/x86/entry/vdso/vma.c  | 41 +++++++++++++++++++-------------------
 arch/x86/include/asm/elf.h |  5 -----
 2 files changed, 21 insertions(+), 25 deletions(-)
arch/x86/entry/vdso/vma.c +21 -20
index 4eea508e9b10..aace862ed9a1 100644
@@ -376,48 +376,49 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
 }
 
 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
-static int load_vdso32(void)
+static int load_vdso_ia32(void)
 {
 	if (vdso32_enabled != 1)  /* Other values all mean "disabled" */
 		return 0;
 
 	return map_vdso(&vdso_image_32, 0);
 }
+#else
+static int load_vdso_ia32(void)
+{
+	WARN_ON_ONCE(1);
+	return -ENODATA;
+}
 #endif
 
 #ifdef CONFIG_X86_64
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+static int load_vdso_64(void)
 {
 	if (!vdso64_enabled)
 		return 0;
 
-	return map_vdso_randomized(&vdso_image_64);
-}
-
-#ifdef CONFIG_COMPAT
-int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp)
-{
 #ifdef CONFIG_X86_X32_ABI
-	if (in_x32_syscall()) {
-		if (!vdso64_enabled)
-			return 0;
+	if (in_x32_syscall())
 		return map_vdso_randomized(&vdso_image_x32);
-	}
 #endif
-#ifdef CONFIG_IA32_EMULATION
-	return load_vdso32();
+
+	return map_vdso_randomized(&vdso_image_64);
+}
 #else
-	return 0;
-#endif
+static int load_vdso_64(void)
+{
+	WARN_ON_ONCE(1);
+	return -ENODATA;
 }
 #endif
-#else
+
 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 {
-	return load_vdso32();
+	if (in_ia32_syscall())
+		return load_vdso_ia32();
+
+	return load_vdso_64();
 }
-#endif
 
 #ifdef CONFIG_X86_64
 static __init int vdso_setup(char *s)
arch/x86/include/asm/elf.h +0 -5
index d00b723eea2d..51a08f6b18e5 100644
@@ -377,11 +377,6 @@ else									\
 	((unsigned long)current->mm->context.vdso +			\
 	 vdso_image_32.sym___kernel_vsyscall)
 
-struct linux_binprm;
-extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
-					      int uses_interp);
-#define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
-
 /* Do not change the values. See get_align_mask() */
 enum align_flags {
 	ALIGN_VA_32	= BIT(0),
Signature · 1 line
2.29.2
#7 lore raw

PATCH v2 5/19 elf: Remove compat_arch_setup_additional_pages()

In reply to Dmitry Safonov

Now that all users rely on detecting bitness of new-born task checking personality, remove compat_arch_setup_additional_pages() macro, simplifying the code.

---
 fs/compat_binfmt_elf.c | 5 -----
 1 file changed, 5 deletions(-)
fs/compat_binfmt_elf.c +0 -5
index 3606dd3a32f5..da8ee4d6e451 100644
@@ -115,11 +115,6 @@
 #define START_THREAD		COMPAT_START_THREAD
 #endif
 
-#ifdef	compat_arch_setup_additional_pages
-#undef	arch_setup_additional_pages
-#define	arch_setup_additional_pages compat_arch_setup_additional_pages
-#endif
-
 #ifdef	compat_elf_read_implies_exec
 #undef	elf_read_implies_exec
 #define	elf_read_implies_exec compat_elf_read_implies_exec
Signature · 1 line
2.29.2
#8 lore raw

Re: [PATCH v2 05/19] elf: Remove compat_arch_setup_additional_pages()

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 3 lines

Now that all users rely on detecting bitness of new-born task checking personality, remove compat_arch_setup_additional_pages() macro, simplifying the code.

I understand from cover that you wanted to reword "new-born" ?

Quoted 23 lines
---
  fs/compat_binfmt_elf.c | 5 -----
  1 file changed, 5 deletions(-)
fs/compat_binfmt_elf.c +0 -5
index 3606dd3a32f5..da8ee4d6e451 100644
@@ -115,11 +115,6 @@
  #define START_THREAD		COMPAT_START_THREAD
  #endif
 
-#ifdef	compat_arch_setup_additional_pages
-#undef	arch_setup_additional_pages
-#define	arch_setup_additional_pages compat_arch_setup_additional_pages
-#endif
-
  #ifdef	compat_elf_read_implies_exec
  #undef	elf_read_implies_exec
  #define	elf_read_implies_exec compat_elf_read_implies_exec
#9 lore raw

Re: [PATCH v2 05/19] elf: Remove compat_arch_setup_additional_pages()

Quoted 8 lines · Christophe Leroy

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 3 lines

Now that all users rely on detecting bitness of new-born task checking personality, remove compat_arch_setup_additional_pages() macro, simplifying the code.

I understand from cover that you wanted to reword "new-born" ?

Yes. I have reread the messages, not sure how I've missed it,

Thanks,
Dmitry

#10 lore raw

PATCH v2 6/19 elf/vdso: Reuse arch_setup_additional_pages() parameters

In reply to Dmitry Safonov

Both parameters of arch_setup_additional_pages() are currently unused. commit fc5243d98ac2 ("[S390] arch_setup_additional_pages arguments") tried to introduce useful arguments, but they still are not used.

Remove old parameters and introduce sysinfo_ehdr argument that will be used to return vdso address to put as AT_SYSINFO_EHDR tag in auxiliary vector. The reason to add this parameter is that many architecture have vDSO pointer saved in their mm->context with the only purpose to use it later in ARCH_DLINFO. That's the macro for elf loader to setup sysinfo_ehdr tag.

Return sysinfo_ehdr address that will be later used by ARCH_DLINFO as an argument. That will allow to drop vDSO pointer from mm->context and any code responsible to track vDSO position on platforms that don't use vDSO as a landing in userspace (arm/s390/sparc).

Cc Albert Ou <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Paul Walmsley <[email protected]>
---
 arch/arm/include/asm/vdso.h        |  6 ++++--
 arch/arm/kernel/process.c          |  4 ++--
 arch/arm/kernel/vdso.c             | 10 +++++++---
 arch/arm64/kernel/vdso.c           | 17 ++++++++--------
 arch/csky/kernel/vdso.c            |  3 ++-
 arch/hexagon/kernel/vdso.c         |  3 ++-
 arch/mips/kernel/vdso.c            |  3 ++-
 arch/nds32/kernel/vdso.c           |  3 ++-
 arch/nios2/mm/init.c               |  2 +-
 arch/powerpc/kernel/vdso.c         |  3 ++-
 arch/riscv/kernel/vdso.c           | 11 +++++-----
 arch/s390/kernel/vdso.c            |  3 ++-
 arch/sh/kernel/vsyscall/vsyscall.c |  3 ++-
 arch/sparc/vdso/vma.c              | 15 +++++++-------
 arch/x86/entry/vdso/vma.c          | 32 +++++++++++++++++-------------
 arch/x86/um/vdso/vma.c             |  2 +-
 fs/binfmt_elf.c                    |  3 ++-
 fs/binfmt_elf_fdpic.c              |  3 ++-
 include/linux/elf.h                | 17 +++++++++++-----
 19 files changed, 85 insertions(+), 58 deletions(-)
arch/arm/include/asm/vdso.h +4 -2
index 5b85889f82ee..6b2b3b1fe833 100644
@@ -10,13 +10,15 @@ struct mm_struct;
 
 #ifdef CONFIG_VDSO
 
-void arm_install_vdso(struct mm_struct *mm, unsigned long addr);
+void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+		      unsigned long *sysinfo_ehdr);
 
 extern unsigned int vdso_total_pages;
 
 #else /* CONFIG_VDSO */
 
-static inline void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
+static inline void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+				    unsigned long *sysinfo_ehdr)
 {
 }
 
arch/arm/kernel/process.c +2 -2
index d0220da1d1b1..0e90cba8ac7a 100644
@@ -389,7 +389,7 @@ static const struct vm_special_mapping sigpage_mapping = {
 	.mremap = sigpage_mremap,
 };
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
@@ -430,7 +430,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	 * to be fatal to the process, so no error check needed
 	 * here.
 	 */
-	arm_install_vdso(mm, addr + PAGE_SIZE);
+	arm_install_vdso(mm, addr + PAGE_SIZE, sysinfo_ehdr);
 
  up_fail:
 	mmap_write_unlock(mm);
arch/arm/kernel/vdso.c +7 -3
index 3408269d19c7..710e5ca99a53 100644
@@ -233,7 +233,8 @@ static int install_vvar(struct mm_struct *mm, unsigned long addr)
 }
 
 /* assumes mmap_lock is write-locked */
-void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
+void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+		      unsigned long *sysinfo_ehdr)
 {
 	struct vm_area_struct *vma;
 	unsigned long len;
@@ -254,7 +255,10 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
 		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
 		&vdso_text_mapping);
 
-	if (!IS_ERR(vma))
-		mm->context.vdso = addr;
+	if (IS_ERR(vma))
+		return;
+
+	mm->context.vdso = addr;
+	*sysinfo_ehdr = addr;
 }
 
arch/arm64/kernel/vdso.c +8 -9
index 1b710deb84d6..666338724a07 100644
@@ -213,8 +213,7 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
 
 static int __setup_additional_pages(enum vdso_abi abi,
 				    struct mm_struct *mm,
-				    struct linux_binprm *bprm,
-				    int uses_interp)
+				    unsigned long *sysinfo_ehdr)
 {
 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
 	unsigned long gp_flags = 0;
@@ -250,6 +249,8 @@ static int __setup_additional_pages(enum vdso_abi abi,
 	if (IS_ERR(ret))
 		goto up_fail;
 
+	*sysinfo_ehdr = vdso_base;
+
 	return 0;
 
 up_fail:
@@ -401,8 +402,7 @@ static int aarch32_sigreturn_setup(struct mm_struct *mm)
 	return PTR_ERR_OR_ZERO(ret);
 }
 
-static int aarch32_setup_additional_pages(struct linux_binprm *bprm,
-					  int uses_interp)
+static int aarch32_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	int ret;
@@ -412,8 +412,7 @@ static int aarch32_setup_additional_pages(struct linux_binprm *bprm,
 		return ret;
 
 	if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
-		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
-					       uses_interp);
+		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, sysinfo_ehdr);
 		if (ret)
 			return ret;
 	}
@@ -447,7 +446,7 @@ static int __init vdso_init(void)
 }
 arch_initcall(vdso_init);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	int ret;
@@ -456,9 +455,9 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 		return -EINTR;
 
 	if (is_compat_task())
-		ret = aarch32_setup_additional_pages(bprm, uses_interp);
+		ret = aarch32_setup_additional_pages(sysinfo_ehdr);
 	else
-		ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
+		ret = __setup_additional_pages(VDSO_ABI_AA64, mm, sysinfo_ehdr);
 
 	mmap_write_unlock(mm);
 
arch/csky/kernel/vdso.c +2 -1
index abc3dbc658d4..f72f76915c59 100644
@@ -44,7 +44,7 @@ static int __init init_vdso(void)
 }
 subsys_initcall(init_vdso);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	int ret;
 	unsigned long addr;
@@ -68,6 +68,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 		goto up_fail;
 
 	mm->context.vdso = (void *)addr;
+	*sysinfo_ehdr = addr;
 
 up_fail:
 	mmap_write_unlock(mm);
arch/hexagon/kernel/vdso.c +2 -1
index b70970ac809f..39e78fe82b99 100644
@@ -46,7 +46,7 @@ arch_initcall(vdso_init);
 /*
  * Called from binfmt_elf.  Create a VMA for the vDSO page.
  */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	int ret;
 	unsigned long vdso_base;
@@ -74,6 +74,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 		goto up_fail;
 
 	mm->context.vdso = (void *)vdso_base;
+	*sysinfo_ehdr = vdso_base;
 
 up_fail:
 	mmap_write_unlock(mm);
arch/mips/kernel/vdso.c +2 -1
index 7d0b91ad2581..e124c68322bb 100644
@@ -86,7 +86,7 @@ static unsigned long vdso_base(void)
 	return base;
 }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mips_vdso_image *image = current->thread.abi->vdso;
 	struct mm_struct *mm = current->mm;
@@ -184,6 +184,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	}
 
 	mm->context.vdso = (void *)vdso_addr;
+	*sysinfo_ehdr = vdso_addr;
 	ret = 0;
 
 out:
arch/nds32/kernel/vdso.c +2 -1
index e16009a07971..530164221166 100644
@@ -111,7 +111,7 @@ unsigned long inline vdso_random_addr(unsigned long vdso_mapping_len)
 	return addr;
 }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
@@ -176,6 +176,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	/*Map vdso to user space */
 	vdso_base += PAGE_SIZE;
 	mm->context.vdso = (void *)vdso_base;
+	*sysinfo_ehdr = vdso_base;
 	vma = _install_special_mapping(mm, vdso_base, vdso_text_len,
 				       VM_READ | VM_EXEC |
 				       VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
arch/nios2/mm/init.c +1 -1
index 61862dbb0e32..e09e54198ac6 100644
@@ -104,7 +104,7 @@ static int alloc_kuser_page(void)
 }
 arch_initcall(alloc_kuser_page);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	int ret;
arch/powerpc/kernel/vdso.c +2 -1
index 8dad44262e75..0ec3bbe7fb36 100644
@@ -122,7 +122,7 @@ struct lib64_elfinfo
  * This is called from binfmt_elf, we create the special vma for the
  * vDSO and insert it into the mm struct tree
  */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	struct page **vdso_pagelist;
@@ -211,6 +211,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	}
 
 	mmap_write_unlock(mm);
+	*sysinfo_ehdr = vdso_base;
 	return 0;
 
  fail_mmapsem:
arch/riscv/kernel/vdso.c +6 -5
index 678204231700..55b6d36d42ec 100644
@@ -56,11 +56,10 @@ static int __init vdso_init(void)
 }
 arch_initcall(vdso_init);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm,
-	int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
-	unsigned long vdso_base, vdso_len;
+	unsigned long vdso_base, vvar_base, vdso_len;
 	int ret;
 
 	vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
@@ -89,12 +88,14 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
 		goto end;
 	}
 
-	vdso_base += (vdso_pages << PAGE_SHIFT);
-	ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
+	vvar_base = vdso_base + (vdso_pages << PAGE_SHIFT);
+	ret = install_special_mapping(mm, vvar_base, PAGE_SIZE,
 		(VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
 
 	if (unlikely(ret))
 		mm->context.vdso = NULL;
+	else
+		*sysinfo_ehdr = vdso_base;
 end:
 	mmap_write_unlock(mm);
 	return ret;
arch/s390/kernel/vdso.c +2 -1
index 6c9ec9521203..810b72f8985c 100644
@@ -150,7 +150,7 @@ void vdso_free_per_cpu(struct lowcore *lowcore)
  * This is called from binfmt_elf, we create the special vma for the
  * vDSO and insert it into the mm struct tree
  */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
@@ -205,6 +205,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 	}
 
 	current->mm->context.vdso_base = vdso_base;
+	*sysinfo_ehdr = vdso_base;
 	rc = 0;
 
 out_up:
arch/sh/kernel/vsyscall/vsyscall.c +2 -1
index 1bd85a6949c4..de8df3261b4f 100644
@@ -55,7 +55,7 @@ int __init vsyscall_init(void)
 }
 
 /* Setup a VMA at program startup for the vsyscall page */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	unsigned long addr;
@@ -78,6 +78,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 		goto up_fail;
 
 	current->mm->context.vdso = (void *)addr;
+	*sysinfo_ehdr = addr;
 
 up_fail:
 	mmap_write_unlock(mm);
arch/sparc/vdso/vma.c +8 -7
index cc19e09b0fa1..bf9195fe9bcc 100644
@@ -346,8 +346,6 @@ static int __init init_vdso(void)
 }
 subsys_initcall(init_vdso);
 
-struct linux_binprm;
-
 /* Shuffle the vdso up a bit, randomly. */
 static unsigned long vdso_addr(unsigned long start, unsigned int len)
 {
@@ -359,7 +357,8 @@ static unsigned long vdso_addr(unsigned long start, unsigned int len)
 }
 
 static int map_vdso(const struct vdso_image *image,
-		struct vm_special_mapping *vdso_mapping)
+		    struct vm_special_mapping *vdso_mapping,
+		    unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
@@ -421,12 +420,14 @@ static int map_vdso(const struct vdso_image *image,
 up_fail:
 	if (ret)
 		current->mm->context.vdso = NULL;
+	else
+		*sysinfo_ehdr = text_start;
 
 	mmap_write_unlock(mm);
 	return ret;
 }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 
 	if (!vdso_enabled)
@@ -434,11 +435,11 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 
 #if defined CONFIG_COMPAT
 	if (!(is_32bit_task()))
-		return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
+		return map_vdso(&vdso_image_64_builtin, &vdso_mapping64, sysinfo_ehdr);
 	else
-		return map_vdso(&vdso_image_32_builtin, &vdso_mapping32);
+		return map_vdso(&vdso_image_32_builtin, &vdso_mapping32, sysinfo_ehdr);
 #else
-	return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
+	return map_vdso(&vdso_image_64_builtin, &vdso_mapping64, sysinfo_ehdr);
 #endif
 
 }
arch/x86/entry/vdso/vma.c +18 -14
index aace862ed9a1..5b9020742e66 100644
@@ -243,7 +243,8 @@ static const struct vm_special_mapping vvar_mapping = {
  * @image          - blob to map
  * @addr           - request a specific address (zero to map at free addr)
  */
-static int map_vdso(const struct vdso_image *image, unsigned long addr)
+static int map_vdso(const struct vdso_image *image, unsigned long addr,
+		    unsigned long *sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
@@ -290,6 +291,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr)
 	} else {
 		current->mm->context.vdso = (void __user *)text_start;
 		current->mm->context.vdso_image = image;
+		*sysinfo_ehdr = text_start;
 	}
 
 up_fail:
@@ -342,11 +344,12 @@ static unsigned long vdso_addr(unsigned long start, unsigned len)
 	return addr;
 }
 
-static int map_vdso_randomized(const struct vdso_image *image)
+static int map_vdso_randomized(const struct vdso_image *image,
+			       unsigned long *sysinfo_ehdr)
 {
 	unsigned long addr = vdso_addr(current->mm->start_stack, image->size-image->sym_vvar_start);
 
-	return map_vdso(image, addr);
+	return map_vdso(image, addr, sysinfo_ehdr);
 }
 #endif
 
@@ -354,6 +357,7 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
+	unsigned long unused;
 
 	mmap_write_lock(mm);
 	/*
@@ -372,19 +376,19 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
 	}
 	mmap_write_unlock(mm);
 
-	return map_vdso(image, addr);
+	return map_vdso(image, addr, &unused);
 }
 
 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
-static int load_vdso_ia32(void)
+static int load_vdso_ia32(unsigned long *sysinfo_ehdr)
 {
 	if (vdso32_enabled != 1)  /* Other values all mean "disabled" */
 		return 0;
 
-	return map_vdso(&vdso_image_32, 0);
+	return map_vdso(&vdso_image_32, 0, sysinfo_ehdr);
 }
 #else
-static int load_vdso_ia32(void)
+static int load_vdso_ia32(unsigned long *sysinfo_ehdr)
 {
 	WARN_ON_ONCE(1);
 	return -ENODATA;
@@ -392,32 +396,32 @@ static int load_vdso_ia32(void)
 #endif
 
 #ifdef CONFIG_X86_64
-static int load_vdso_64(void)
+static int load_vdso_64(unsigned long *sysinfo_ehdr)
 {
 	if (!vdso64_enabled)
 		return 0;
 
 #ifdef CONFIG_X86_X32_ABI
 	if (in_x32_syscall())
-		return map_vdso_randomized(&vdso_image_x32);
+		return map_vdso_randomized(&vdso_image_x32, sysinfo_ehdr);
 #endif
 
-	return map_vdso_randomized(&vdso_image_64);
+	return map_vdso_randomized(&vdso_image_64, sysinfo_ehdr);
 }
 #else
-static int load_vdso_64(void)
+static int load_vdso_64(unsigned long *sysinfo_ehdr)
 {
 	WARN_ON_ONCE(1);
 	return -ENODATA;
 }
 #endif
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	if (in_ia32_syscall())
-		return load_vdso_ia32();
+		return load_vdso_ia32(sysinfo_ehdr);
 
-	return load_vdso_64();
+	return load_vdso_64(sysinfo_ehdr);
 }
 
 #ifdef CONFIG_X86_64
arch/x86/um/vdso/vma.c +1 -1
index 76d9f6ce7a3d..77488065f7cc 100644
@@ -50,7 +50,7 @@ static int __init init_vdso(void)
 }
 subsys_initcall(init_vdso);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	int err;
 	struct mm_struct *mm = current->mm;
fs/binfmt_elf.c +2 -1
index b9adbeb59101..049ff514aa19 100644
@@ -833,6 +833,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	unsigned long interp_load_addr = 0;
 	unsigned long start_code, end_code, start_data, end_data;
 	unsigned long reloc_func_desc __maybe_unused = 0;
+	unsigned long sysinfo_ehdr = 0;
 	int executable_stack = EXSTACK_DEFAULT;
 	struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
 	struct elfhdr *interp_elf_ex = NULL;
@@ -1249,7 +1250,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 
 	set_binfmt(&elf_format);
 
-	retval = arch_setup_additional_pages(bprm, !!interpreter);
+	retval = arch_setup_additional_pages(&sysinfo_ehdr);
 	if (retval < 0)
 		goto out;
 
fs/binfmt_elf_fdpic.c +2 -1
index b9a6d1b2b5bb..c9ee3c240855 100644
@@ -183,6 +183,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 {
 	struct elf_fdpic_params exec_params, interp_params;
 	struct pt_regs *regs = current_pt_regs();
+	unsigned long sysinfo_ehdr = 0;
 	struct elf_phdr *phdr;
 	unsigned long stack_size, entryaddr;
 #ifdef ELF_FDPIC_PLAT_INIT
@@ -375,7 +376,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 	if (retval < 0)
 		goto error;
 
-	retval = arch_setup_additional_pages(bprm, !!interpreter_name);
+	retval = arch_setup_additional_pages(&sysinfo_ehdr);
 	if (retval < 0)
 		goto error;
 #endif
include/linux/elf.h +12 -5
index 95bf7a1abaef..a8bea5611a4b 100644
@@ -104,13 +104,20 @@ static inline int arch_elf_adjust_prot(int prot,
 }
 #endif
 
-struct linux_binprm;
 #ifdef CONFIG_ARCH_HAS_SETUP_ADDITIONAL_PAGES
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
+/**
+ * arch_setup_additional_pages - Premap VMAs in a new-execed process
+ * @sysinfo_ehdr:	Returns vDSO position to be set in the initial
+ *			auxiliary vector (tag AT_SYSINFO_EHDR) by binfmt
+ *			loader. On failure isn't initialized.
+ *			As address == 0 is never used, it allows to check
+ *			if the tag should be set.
+ *
+ * Return: Zero if successful, or a negative error code on failure.
+ */
+extern int arch_setup_additional_pages(unsigned long *sysinfo_ehdr);
 #else
-static inline int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp)
+static inline int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 {
 	return 0;
 }
Signature · 1 line
2.29.2
#11 lore raw

Re: [PATCH v2 06/19] elf/vdso: Reuse arch_setup_additional_pages() parameters

"Reuse arch_setup_additional_pages() parameters"

Did you mean "remove" ? Or "Revise" ?

Maybe could be:

"Modify arch_setup_additional_pages() parameters"

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 623 lines

Both parameters of arch_setup_additional_pages() are currently unused. commit fc5243d98ac2 ("[S390] arch_setup_additional_pages arguments") tried to introduce useful arguments, but they still are not used.

Remove old parameters and introduce sysinfo_ehdr argument that will be used to return vdso address to put as AT_SYSINFO_EHDR tag in auxiliary vector. The reason to add this parameter is that many architecture have vDSO pointer saved in their mm->context with the only purpose to use it later in ARCH_DLINFO. That's the macro for elf loader to setup sysinfo_ehdr tag.

Return sysinfo_ehdr address that will be later used by ARCH_DLINFO as an argument. That will allow to drop vDSO pointer from mm->context and any code responsible to track vDSO position on platforms that don't use vDSO as a landing in userspace (arm/s390/sparc).

Cc Albert Ou <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Paul Walmsley <[email protected]>
---
  arch/arm/include/asm/vdso.h        |  6 ++++--
  arch/arm/kernel/process.c          |  4 ++--
  arch/arm/kernel/vdso.c             | 10 +++++++---
  arch/arm64/kernel/vdso.c           | 17 ++++++++--------
  arch/csky/kernel/vdso.c            |  3 ++-
  arch/hexagon/kernel/vdso.c         |  3 ++-
  arch/mips/kernel/vdso.c            |  3 ++-
  arch/nds32/kernel/vdso.c           |  3 ++-
  arch/nios2/mm/init.c               |  2 +-
  arch/powerpc/kernel/vdso.c         |  3 ++-
  arch/riscv/kernel/vdso.c           | 11 +++++-----
  arch/s390/kernel/vdso.c            |  3 ++-
  arch/sh/kernel/vsyscall/vsyscall.c |  3 ++-
  arch/sparc/vdso/vma.c              | 15 +++++++-------
  arch/x86/entry/vdso/vma.c          | 32 +++++++++++++++++-------------
  arch/x86/um/vdso/vma.c             |  2 +-
  fs/binfmt_elf.c                    |  3 ++-
  fs/binfmt_elf_fdpic.c              |  3 ++-
  include/linux/elf.h                | 17 +++++++++++-----
  19 files changed, 85 insertions(+), 58 deletions(-)
arch/arm/include/asm/vdso.h +4 -2
index 5b85889f82ee..6b2b3b1fe833 100644
@@ -10,13 +10,15 @@ struct mm_struct;
 
  #ifdef CONFIG_VDSO
 
-void arm_install_vdso(struct mm_struct *mm, unsigned long addr);
+void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+		      unsigned long *sysinfo_ehdr);
 
  extern unsigned int vdso_total_pages;
 
  #else /* CONFIG_VDSO */
 
-static inline void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
+static inline void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+				    unsigned long *sysinfo_ehdr)
  {
  }
 
arch/arm/kernel/process.c +2 -2
index d0220da1d1b1..0e90cba8ac7a 100644
@@ -389,7 +389,7 @@ static const struct vm_special_mapping sigpage_mapping = {
  	.mremap = sigpage_mremap,
  };
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
@@ -430,7 +430,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  	 * to be fatal to the process, so no error check needed
  	 * here.
  	 */
-	arm_install_vdso(mm, addr + PAGE_SIZE);
+	arm_install_vdso(mm, addr + PAGE_SIZE, sysinfo_ehdr);
 
   up_fail:
  	mmap_write_unlock(mm);
arch/arm/kernel/vdso.c +7 -3
index 3408269d19c7..710e5ca99a53 100644
@@ -233,7 +233,8 @@ static int install_vvar(struct mm_struct *mm, unsigned long addr)
  }
 
  /* assumes mmap_lock is write-locked */
-void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
+void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
+		      unsigned long *sysinfo_ehdr)
  {
  	struct vm_area_struct *vma;
  	unsigned long len;
@@ -254,7 +255,10 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
  		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  		&vdso_text_mapping);
 
-	if (!IS_ERR(vma))
-		mm->context.vdso = addr;
+	if (IS_ERR(vma))
+		return;
+
+	mm->context.vdso = addr;
+	*sysinfo_ehdr = addr;
  }
 
arch/arm64/kernel/vdso.c +8 -9
index 1b710deb84d6..666338724a07 100644
@@ -213,8 +213,7 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
 
  static int __setup_additional_pages(enum vdso_abi abi,
  				    struct mm_struct *mm,
-				    struct linux_binprm *bprm,
-				    int uses_interp)
+				    unsigned long *sysinfo_ehdr)
  {
  	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
  	unsigned long gp_flags = 0;
@@ -250,6 +249,8 @@ static int __setup_additional_pages(enum vdso_abi abi,
  	if (IS_ERR(ret))
  		goto up_fail;
 
+	*sysinfo_ehdr = vdso_base;
+
  	return 0;
 
  up_fail:
@@ -401,8 +402,7 @@ static int aarch32_sigreturn_setup(struct mm_struct *mm)
  	return PTR_ERR_OR_ZERO(ret);
  }
 
-static int aarch32_setup_additional_pages(struct linux_binprm *bprm,
-					  int uses_interp)
+static int aarch32_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	int ret;
@@ -412,8 +412,7 @@ static int aarch32_setup_additional_pages(struct linux_binprm *bprm,
  		return ret;
 
  	if (IS_ENABLED(CONFIG_COMPAT_VDSO)) {
-		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, bprm,
-					       uses_interp);
+		ret = __setup_additional_pages(VDSO_ABI_AA32, mm, sysinfo_ehdr);
  		if (ret)
  			return ret;
  	}
@@ -447,7 +446,7 @@ static int __init vdso_init(void)
  }
  arch_initcall(vdso_init);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	int ret;
@@ -456,9 +455,9 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  		return -EINTR;
 
  	if (is_compat_task())
-		ret = aarch32_setup_additional_pages(bprm, uses_interp);
+		ret = aarch32_setup_additional_pages(sysinfo_ehdr);
  	else
-		ret = __setup_additional_pages(VDSO_ABI_AA64, mm, bprm, uses_interp);
+		ret = __setup_additional_pages(VDSO_ABI_AA64, mm, sysinfo_ehdr);
 
  	mmap_write_unlock(mm);
 
arch/csky/kernel/vdso.c +2 -1
index abc3dbc658d4..f72f76915c59 100644
@@ -44,7 +44,7 @@ static int __init init_vdso(void)
  }
  subsys_initcall(init_vdso);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	int ret;
  	unsigned long addr;
@@ -68,6 +68,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  		goto up_fail;
 
  	mm->context.vdso = (void *)addr;
+	*sysinfo_ehdr = addr;
 
  up_fail:
  	mmap_write_unlock(mm);
arch/hexagon/kernel/vdso.c +2 -1
index b70970ac809f..39e78fe82b99 100644
@@ -46,7 +46,7 @@ arch_initcall(vdso_init);
  /*
   * Called from binfmt_elf.  Create a VMA for the vDSO page.
   */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	int ret;
  	unsigned long vdso_base;
@@ -74,6 +74,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  		goto up_fail;
 
  	mm->context.vdso = (void *)vdso_base;
+	*sysinfo_ehdr = vdso_base;
 
  up_fail:
  	mmap_write_unlock(mm);
arch/mips/kernel/vdso.c +2 -1
index 7d0b91ad2581..e124c68322bb 100644
@@ -86,7 +86,7 @@ static unsigned long vdso_base(void)
  	return base;
  }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mips_vdso_image *image = current->thread.abi->vdso;
  	struct mm_struct *mm = current->mm;
@@ -184,6 +184,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  	}
 
  	mm->context.vdso = (void *)vdso_addr;
+	*sysinfo_ehdr = vdso_addr;
  	ret = 0;
 
  out:
arch/nds32/kernel/vdso.c +2 -1
index e16009a07971..530164221166 100644
@@ -111,7 +111,7 @@ unsigned long inline vdso_random_addr(unsigned long vdso_mapping_len)
  	return addr;
  }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
@@ -176,6 +176,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  	/*Map vdso to user space */
  	vdso_base += PAGE_SIZE;
  	mm->context.vdso = (void *)vdso_base;
+	*sysinfo_ehdr = vdso_base;
  	vma = _install_special_mapping(mm, vdso_base, vdso_text_len,
  				       VM_READ | VM_EXEC |
  				       VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
arch/nios2/mm/init.c +1 -1
index 61862dbb0e32..e09e54198ac6 100644
@@ -104,7 +104,7 @@ static int alloc_kuser_page(void)
  }
  arch_initcall(alloc_kuser_page);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	int ret;
arch/powerpc/kernel/vdso.c +2 -1
index 8dad44262e75..0ec3bbe7fb36 100644
@@ -122,7 +122,7 @@ struct lib64_elfinfo
   * This is called from binfmt_elf, we create the special vma for the
   * vDSO and insert it into the mm struct tree
   */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	struct page **vdso_pagelist;
@@ -211,6 +211,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  	}
 
  	mmap_write_unlock(mm);
+	*sysinfo_ehdr = vdso_base;
  	return 0;
 
   fail_mmapsem:
arch/riscv/kernel/vdso.c +6 -5
index 678204231700..55b6d36d42ec 100644
@@ -56,11 +56,10 @@ static int __init vdso_init(void)
  }
  arch_initcall(vdso_init);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm,
-	int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
-	unsigned long vdso_base, vdso_len;
+	unsigned long vdso_base, vvar_base, vdso_len;
  	int ret;
 
  	vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
@@ -89,12 +88,14 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
  		goto end;
  	}
 
-	vdso_base += (vdso_pages << PAGE_SHIFT);
-	ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
+	vvar_base = vdso_base + (vdso_pages << PAGE_SHIFT);
+	ret = install_special_mapping(mm, vvar_base, PAGE_SIZE,
  		(VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
 
  	if (unlikely(ret))
  		mm->context.vdso = NULL;
+	else
+		*sysinfo_ehdr = vdso_base;
  end:
  	mmap_write_unlock(mm);
  	return ret;
arch/s390/kernel/vdso.c +2 -1
index 6c9ec9521203..810b72f8985c 100644
@@ -150,7 +150,7 @@ void vdso_free_per_cpu(struct lowcore *lowcore)
   * This is called from binfmt_elf, we create the special vma for the
   * vDSO and insert it into the mm struct tree
   */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
@@ -205,6 +205,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  	}
 
  	current->mm->context.vdso_base = vdso_base;
+	*sysinfo_ehdr = vdso_base;
  	rc = 0;
 
  out_up:
arch/sh/kernel/vsyscall/vsyscall.c +2 -1
index 1bd85a6949c4..de8df3261b4f 100644
@@ -55,7 +55,7 @@ int __init vsyscall_init(void)
  }
 
  /* Setup a VMA at program startup for the vsyscall page */
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	unsigned long addr;
@@ -78,6 +78,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  		goto up_fail;
 
  	current->mm->context.vdso = (void *)addr;
+	*sysinfo_ehdr = addr;
 
  up_fail:
  	mmap_write_unlock(mm);
arch/sparc/vdso/vma.c +8 -7
index cc19e09b0fa1..bf9195fe9bcc 100644
@@ -346,8 +346,6 @@ static int __init init_vdso(void)
  }
  subsys_initcall(init_vdso);
 
-struct linux_binprm;
-
  /* Shuffle the vdso up a bit, randomly. */
  static unsigned long vdso_addr(unsigned long start, unsigned int len)
  {
@@ -359,7 +357,8 @@ static unsigned long vdso_addr(unsigned long start, unsigned int len)
  }
 
  static int map_vdso(const struct vdso_image *image,
-		struct vm_special_mapping *vdso_mapping)
+		    struct vm_special_mapping *vdso_mapping,
+		    unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
@@ -421,12 +420,14 @@ static int map_vdso(const struct vdso_image *image,
  up_fail:
  	if (ret)
  		current->mm->context.vdso = NULL;
+	else
+		*sysinfo_ehdr = text_start;
 
  	mmap_write_unlock(mm);
  	return ret;
  }
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
 
  	if (!vdso_enabled)
@@ -434,11 +435,11 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
 
  #if defined CONFIG_COMPAT
  	if (!(is_32bit_task()))
-		return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
+		return map_vdso(&vdso_image_64_builtin, &vdso_mapping64, sysinfo_ehdr);
  	else
-		return map_vdso(&vdso_image_32_builtin, &vdso_mapping32);
+		return map_vdso(&vdso_image_32_builtin, &vdso_mapping32, sysinfo_ehdr);
  #else
-	return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
+	return map_vdso(&vdso_image_64_builtin, &vdso_mapping64, sysinfo_ehdr);
  #endif
 
  }
arch/x86/entry/vdso/vma.c +18 -14
index aace862ed9a1..5b9020742e66 100644
@@ -243,7 +243,8 @@ static const struct vm_special_mapping vvar_mapping = {
   * @image          - blob to map
   * @addr           - request a specific address (zero to map at free addr)
   */
-static int map_vdso(const struct vdso_image *image, unsigned long addr)
+static int map_vdso(const struct vdso_image *image, unsigned long addr,
+		    unsigned long *sysinfo_ehdr)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
@@ -290,6 +291,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr)
  	} else {
  		current->mm->context.vdso = (void __user *)text_start;
  		current->mm->context.vdso_image = image;
+		*sysinfo_ehdr = text_start;
  	}
 
  up_fail:
@@ -342,11 +344,12 @@ static unsigned long vdso_addr(unsigned long start, unsigned len)
  	return addr;
  }
 
-static int map_vdso_randomized(const struct vdso_image *image)
+static int map_vdso_randomized(const struct vdso_image *image,
+			       unsigned long *sysinfo_ehdr)
  {
  	unsigned long addr = vdso_addr(current->mm->start_stack, image->size-image->sym_vvar_start);
 
-	return map_vdso(image, addr);
+	return map_vdso(image, addr, sysinfo_ehdr);
  }
  #endif
 
@@ -354,6 +357,7 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
+	unsigned long unused;
 
  	mmap_write_lock(mm);
  	/*
@@ -372,19 +376,19 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
  	}
  	mmap_write_unlock(mm);
 
-	return map_vdso(image, addr);
+	return map_vdso(image, addr, &unused);
  }
 
  #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
-static int load_vdso_ia32(void)
+static int load_vdso_ia32(unsigned long *sysinfo_ehdr)
  {
  	if (vdso32_enabled != 1)  /* Other values all mean "disabled" */
  		return 0;
 
-	return map_vdso(&vdso_image_32, 0);
+	return map_vdso(&vdso_image_32, 0, sysinfo_ehdr);
  }
  #else
-static int load_vdso_ia32(void)
+static int load_vdso_ia32(unsigned long *sysinfo_ehdr)
  {
  	WARN_ON_ONCE(1);
  	return -ENODATA;
@@ -392,32 +396,32 @@ static int load_vdso_ia32(void)
  #endif
 
  #ifdef CONFIG_X86_64
-static int load_vdso_64(void)
+static int load_vdso_64(unsigned long *sysinfo_ehdr)
  {
  	if (!vdso64_enabled)
  		return 0;
 
  #ifdef CONFIG_X86_X32_ABI
  	if (in_x32_syscall())
-		return map_vdso_randomized(&vdso_image_x32);
+		return map_vdso_randomized(&vdso_image_x32, sysinfo_ehdr);
  #endif
 
-	return map_vdso_randomized(&vdso_image_64);
+	return map_vdso_randomized(&vdso_image_64, sysinfo_ehdr);
  }
  #else
-static int load_vdso_64(void)
+static int load_vdso_64(unsigned long *sysinfo_ehdr)
  {
  	WARN_ON_ONCE(1);
  	return -ENODATA;
  }
  #endif
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	if (in_ia32_syscall())
-		return load_vdso_ia32();
+		return load_vdso_ia32(sysinfo_ehdr);
 
-	return load_vdso_64();
+	return load_vdso_64(sysinfo_ehdr);
  }
 
  #ifdef CONFIG_X86_64
arch/x86/um/vdso/vma.c +1 -1
index 76d9f6ce7a3d..77488065f7cc 100644
@@ -50,7 +50,7 @@ static int __init init_vdso(void)
  }
  subsys_initcall(init_vdso);
 
-int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
+int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	int err;
  	struct mm_struct *mm = current->mm;
fs/binfmt_elf.c +2 -1
index b9adbeb59101..049ff514aa19 100644
@@ -833,6 +833,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
  	unsigned long interp_load_addr = 0;
  	unsigned long start_code, end_code, start_data, end_data;
  	unsigned long reloc_func_desc __maybe_unused = 0;
+	unsigned long sysinfo_ehdr = 0;
  	int executable_stack = EXSTACK_DEFAULT;
  	struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
  	struct elfhdr *interp_elf_ex = NULL;
@@ -1249,7 +1250,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 
  	set_binfmt(&elf_format);
 
-	retval = arch_setup_additional_pages(bprm, !!interpreter);
+	retval = arch_setup_additional_pages(&sysinfo_ehdr);
  	if (retval < 0)
  		goto out;
 
fs/binfmt_elf_fdpic.c +2 -1
index b9a6d1b2b5bb..c9ee3c240855 100644
@@ -183,6 +183,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
  {
  	struct elf_fdpic_params exec_params, interp_params;
  	struct pt_regs *regs = current_pt_regs();
+	unsigned long sysinfo_ehdr = 0;
  	struct elf_phdr *phdr;
  	unsigned long stack_size, entryaddr;
  #ifdef ELF_FDPIC_PLAT_INIT
@@ -375,7 +376,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
  	if (retval < 0)
  		goto error;
 
-	retval = arch_setup_additional_pages(bprm, !!interpreter_name);
+	retval = arch_setup_additional_pages(&sysinfo_ehdr);
  	if (retval < 0)
  		goto error;
  #endif
include/linux/elf.h +12 -5
index 95bf7a1abaef..a8bea5611a4b 100644
@@ -104,13 +104,20 @@ static inline int arch_elf_adjust_prot(int prot,
  }
  #endif
 
-struct linux_binprm;
  #ifdef CONFIG_ARCH_HAS_SETUP_ADDITIONAL_PAGES
-extern int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp);
+/**
+ * arch_setup_additional_pages - Premap VMAs in a new-execed process
+ * @sysinfo_ehdr:	Returns vDSO position to be set in the initial
+ *			auxiliary vector (tag AT_SYSINFO_EHDR) by binfmt
+ *			loader. On failure isn't initialized.
+ *			As address == 0 is never used, it allows to check
+ *			if the tag should be set.
+ *
+ * Return: Zero if successful, or a negative error code on failure.
+ */
+extern int arch_setup_additional_pages(unsigned long *sysinfo_ehdr);
  #else
-static inline int arch_setup_additional_pages(struct linux_binprm *bprm,
-				       int uses_interp)
+static inline int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  {
  	return 0;
  }
#12 lore raw

Re: [PATCH v2 06/19] elf/vdso: Reuse arch_setup_additional_pages() parameters

Quoted 7 lines · Christophe Leroy

"Reuse arch_setup_additional_pages() parameters"

Did you mean "remove" ? Or "Revise" ?

Maybe could be:

"Modify arch_setup_additional_pages() parameters"

Sure.

Thanks,
Dmitry

#13 lore raw

PATCH v2 7/19 elf: Use sysinfo_ehdr in ARCH_DLINFO()

In reply to Dmitry Safonov

Instead mm->context.vdso use the pointer provided by elf loader. That allows to drop the pointer on arm/s390/sparc.

Cc Christian Borntraeger <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 arch/alpha/include/asm/elf.h    |  2 +-
 arch/arm/include/asm/elf.h      |  5 ++---
 arch/arm64/include/asm/elf.h    | 18 +++++-------------
 arch/ia64/include/asm/elf.h     |  2 +-
 arch/mips/include/asm/elf.h     |  5 ++---
 arch/nds32/include/asm/elf.h    |  5 ++---
 arch/powerpc/include/asm/elf.h  |  4 ++--
 arch/riscv/include/asm/elf.h    |  5 ++---
 arch/s390/include/asm/elf.h     |  5 ++---
 arch/sh/include/asm/elf.h       | 10 +++++-----
 arch/sparc/include/asm/elf_64.h |  5 ++---
 arch/x86/include/asm/elf.h      | 33 ++++++++++++++-------------------
 arch/x86/um/asm/elf.h           |  4 ++--
 fs/binfmt_elf.c                 |  6 +++---
 fs/binfmt_elf_fdpic.c           | 11 ++++++-----
 15 files changed, 51 insertions(+), 69 deletions(-)
arch/alpha/include/asm/elf.h +1 -1
index 8049997fa372..701e820f28f0 100644
@@ -155,7 +155,7 @@ extern int alpha_l2_cacheshape;
 extern int alpha_l3_cacheshape;
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
   do {								\
     NEW_AUX_ENT(AT_L1I_CACHESHAPE, alpha_l1i_cacheshape);	\
     NEW_AUX_ENT(AT_L1D_CACHESHAPE, alpha_l1d_cacheshape);	\
arch/arm/include/asm/elf.h +2 -3
index 47347d7412ec..76a0f04190f0 100644
@@ -138,10 +138,9 @@ extern void elf_set_personality(const struct elf32_hdr *);
 #define SET_PERSONALITY(ex)	elf_set_personality(&(ex))
 
 #ifdef CONFIG_VDSO
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
 do {								\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-		    (elf_addr_t)current->mm->context.vdso);	\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 } while (0)
 #endif
 
arch/arm64/include/asm/elf.h +5 -13
index a81953bcc1cf..e62818967a69 100644
@@ -165,10 +165,9 @@ typedef struct user_fpsimd_state elf_fpregset_t;
 })
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO							\
+#define ARCH_DLINFO(sysinfo_ehdr)					\
 do {									\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,					\
-		    (elf_addr_t)current->mm->context.vdso);		\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);			\
 									\
 	/*								\
 	 * Should always be nonzero unless there's a kernel bug.	\
@@ -223,19 +222,12 @@ typedef compat_elf_greg_t		compat_elf_gregset_t[COMPAT_ELF_NGREG];
 	set_thread_flag(TIF_32BIT);					\
  })
 #ifdef CONFIG_COMPAT_VDSO
-#define COMPAT_ARCH_DLINFO						\
+#define COMPAT_ARCH_DLINFO(sysinfo_ehdr)				\
 do {									\
-	/*								\
-	 * Note that we use Elf64_Off instead of elf_addr_t because	\
-	 * elf_addr_t in compat is defined as Elf32_Addr and casting	\
-	 * current->mm->context.vdso to it triggers a cast warning of	\
-	 * cast from pointer to integer of different size.		\
-	 */								\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,					\
-			(Elf64_Off)current->mm->context.vdso);		\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);			\
 } while (0)
 #else
-#define COMPAT_ARCH_DLINFO
+#define COMPAT_ARCH_DLINFO(sysinfo_ehdr)
 #endif
 
 #endif /* CONFIG_COMPAT */
arch/ia64/include/asm/elf.h +1 -1
index 6629301a2620..a257e5abddce 100644
@@ -208,7 +208,7 @@ struct task_struct;
 #define GATE_EHDR	((const struct elfhdr *) GATE_ADDR)
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO								\
+#define ARCH_DLINFO(sysinfo_ehdr)						\
 do {										\
 	extern char __kernel_syscall_via_epc[];					\
 	NEW_AUX_ENT(AT_SYSINFO, (unsigned long) __kernel_syscall_via_epc);	\
arch/mips/include/asm/elf.h +2 -3
index 6665cf7870b3..5bdc8d00ac37 100644
@@ -474,10 +474,9 @@ extern const char *__elf_base_platform;
 #endif
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO							\
+#define ARCH_DLINFO(sysinfo_ehdr)					\
 do {									\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,					\
-		    (unsigned long)current->mm->context.vdso);		\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);			\
 } while (0)
 
 #ifdef CONFIG_MIPS_FP_SUPPORT
arch/nds32/include/asm/elf.h +2 -3
index 36cec4ae5a84..4f5894208efe 100644
@@ -165,13 +165,12 @@ struct elf32_hdr;
 #define FPU_AUX_ENT	NEW_AUX_ENT(AT_IGNORE, 0)
 #endif
 
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
 do {								\
 	/* Optional FPU initialization */			\
 	FPU_AUX_ENT;						\
 								\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-		    (elf_addr_t)current->mm->context.vdso);	\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 } while (0)
 
 #endif
arch/powerpc/include/asm/elf.h +2 -2
index ba0e1e331088..b1ae4411f4bd 100644
@@ -155,7 +155,7 @@ extern int ucache_bsize;
  *   even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
  * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes
  */
-#define ARCH_DLINFO							\
+#define ARCH_DLINFO(sysinfo_ehdr)					\
 do {									\
 	/* Handle glibc compatibility. */				\
 	NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC);			\
@@ -164,7 +164,7 @@ do {									\
 	NEW_AUX_ENT(AT_DCACHEBSIZE, dcache_bsize);			\
 	NEW_AUX_ENT(AT_ICACHEBSIZE, icache_bsize);			\
 	NEW_AUX_ENT(AT_UCACHEBSIZE, ucache_bsize);			\
-	VDSO_AUX_ENT(AT_SYSINFO_EHDR, current->mm->context.vdso_base);	\
+	VDSO_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);			\
 	ARCH_DLINFO_CACHE_GEOMETRY;					\
 } while (0)
 
arch/riscv/include/asm/elf.h +2 -3
index 5e2543673e42..cd659e1dc259 100644
@@ -58,10 +58,9 @@ extern unsigned long elf_hwcap;
 #define ELF_PLATFORM	(NULL)
 
 #ifdef CONFIG_MMU
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
 do {								\
-	NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-		(elf_addr_t)current->mm->context.vdso);		\
+	NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 	NEW_AUX_ENT(AT_L1I_CACHESIZE,				\
 		get_cache_size(1, CACHE_TYPE_INST));		\
 	NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY,			\
arch/s390/include/asm/elf.h +2 -3
index f6ce9df8a996..1087a52f3257 100644
@@ -269,11 +269,10 @@ do {								\
 #define STACK_RND_MASK	MMAP_RND_MASK
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO							    \
+#define ARCH_DLINFO(sysinfo_ehdr)					    \
 do {									    \
 	if (vdso_enabled)						    \
-		NEW_AUX_ENT(AT_SYSINFO_EHDR,				    \
-			    (unsigned long)current->mm->context.vdso_base); \
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		    \
 } while (0)
 
 #endif
arch/sh/include/asm/elf.h +5 -5
index 9b3e22e771a1..03b813c0bc39 100644
@@ -170,13 +170,13 @@ extern void __kernel_vsyscall;
 #define VDSO_BASE		((unsigned long)current->mm->context.vdso)
 #define VDSO_SYM(x)		(VDSO_BASE + (unsigned long)(x))
 
-#define VSYSCALL_AUX_ENT					\
+#define VSYSCALL_AUX_ENT(sysinfo_ehdr)				\
 	if (vdso_enabled)					\
-		NEW_AUX_ENT(AT_SYSINFO_EHDR, VDSO_BASE);	\
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);	\
 	else							\
 		NEW_AUX_ENT(AT_IGNORE, 0)
 #else
-#define VSYSCALL_AUX_ENT	NEW_AUX_ENT(AT_IGNORE, 0)
+#define VSYSCALL_AUX_ENT(sysinfo_ehdr)	NEW_AUX_ENT(AT_IGNORE, 0)
 #endif /* CONFIG_VSYSCALL */
 
 #ifdef CONFIG_SH_FPU
@@ -188,13 +188,13 @@ extern void __kernel_vsyscall;
 extern int l1i_cache_shape, l1d_cache_shape, l2_cache_shape;
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
 do {								\
 	/* Optional FPU initialization */			\
 	FPU_AUX_ENT;						\
 								\
 	/* Optional vsyscall entry */				\
-	VSYSCALL_AUX_ENT;					\
+	VSYSCALL_AUX_ENT(sysinfo_ehdr);				\
 								\
 	/* Cache desc */					\
 	NEW_AUX_ENT(AT_L1I_CACHESHAPE, l1i_cache_shape);	\
arch/sparc/include/asm/elf_64.h +2 -3
index 63a622c36df3..1e7295b5ae2f 100644
@@ -213,12 +213,11 @@ do {	if ((ex).e_ident[EI_CLASS] == ELFCLASS32)	\
 
 extern unsigned int vdso_enabled;
 
-#define	ARCH_DLINFO							\
+#define	ARCH_DLINFO(sysinfo_ehdr)					\
 do {									\
 	extern struct adi_config adi_state;				\
 	if (vdso_enabled)						\
-		NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-			    (unsigned long)current->mm->context.vdso);	\
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 	NEW_AUX_ENT(AT_ADI_BLKSZ, adi_state.caps.blksz);		\
 	NEW_AUX_ENT(AT_ADI_NBITS, adi_state.caps.nbits);		\
 	NEW_AUX_ENT(AT_ADI_UEONADI, adi_state.caps.ue_on_adi);		\
arch/x86/include/asm/elf.h +14 -19
index 51a08f6b18e5..ace9e9da3521 100644
@@ -306,11 +306,14 @@ extern u32 elf_hwcap2;
 
 struct task_struct;
 
-#define	ARCH_DLINFO_IA32						\
+#define VDSO_ENTRY(sysinfo_ehdr)					\
+	(sysinfo_ehdr + vdso_image_32.sym___kernel_vsyscall)
+
+#define	ARCH_DLINFO_IA32(sysinfo_ehdr)					\
 do {									\
-	if (VDSO_CURRENT_BASE) {					\
-		NEW_AUX_ENT(AT_SYSINFO,	VDSO_ENTRY);			\
-		NEW_AUX_ENT(AT_SYSINFO_EHDR, VDSO_CURRENT_BASE);	\
+	if (sysinfo_ehdr) {						\
+		NEW_AUX_ENT(AT_SYSINFO,	VDSO_ENTRY(sysinfo_ehdr));	\
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 	}								\
 } while (0)
 
@@ -344,39 +347,31 @@ extern bool mmap_address_hint_valid(unsigned long addr, unsigned long len);
 #define __STACK_RND_MASK(is32bit) ((is32bit) ? 0x7ff : 0x3fffff)
 #define STACK_RND_MASK __STACK_RND_MASK(mmap_is_ia32())
 
-#define ARCH_DLINFO							\
+#define ARCH_DLINFO(sysinfo_ehdr)					\
 do {									\
 	if (vdso64_enabled)						\
-		NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-			    (unsigned long __force)current->mm->context.vdso); \
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 } while (0)
 
 /* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */
-#define ARCH_DLINFO_X32							\
+#define ARCH_DLINFO_X32(sysinfo_ehdr)					\
 do {									\
 	if (vdso64_enabled)						\
-		NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
-			    (unsigned long __force)current->mm->context.vdso); \
+		NEW_AUX_ENT(AT_SYSINFO_EHDR, sysinfo_ehdr);		\
 } while (0)
 
 #define AT_SYSINFO		32
 
-#define COMPAT_ARCH_DLINFO						\
+#define COMPAT_ARCH_DLINFO(sysinfo_ehdr)				\
 if (exec->e_machine == EM_X86_64)					\
-	ARCH_DLINFO_X32;						\
+	ARCH_DLINFO_X32(sysinfo_ehdr);					\
 else									\
-	ARCH_DLINFO_IA32
+	ARCH_DLINFO_IA32(sysinfo_ehdr)
 
 #define COMPAT_ELF_ET_DYN_BASE	(TASK_UNMAPPED_BASE + 0x1000000)
 
 #endif /* !CONFIG_X86_32 */
 
-#define VDSO_CURRENT_BASE	((unsigned long)current->mm->context.vdso)
-
-#define VDSO_ENTRY							\
-	((unsigned long)current->mm->context.vdso +			\
-	 vdso_image_32.sym___kernel_vsyscall)
-
 /* Do not change the values. See get_align_mask() */
 enum align_flags {
 	ALIGN_VA_32	= BIT(0),
arch/x86/um/asm/elf.h +2 -2
index 7bc1d9d94d66..953c6b7f992e 100644
@@ -88,7 +88,7 @@ extern unsigned long __kernel_vsyscall;
 #define AT_SYSINFO		32
 #define AT_SYSINFO_EHDR		33
 
-#define ARCH_DLINFO						\
+#define ARCH_DLINFO(sysinfo_ehdr)				\
 do {								\
 	if ( vsyscall_ehdr ) {					\
 		NEW_AUX_ENT(AT_SYSINFO,	__kernel_vsyscall);	\
@@ -183,7 +183,7 @@ do {								\
 
 extern unsigned long um_vdso_addr;
 #define AT_SYSINFO_EHDR 33
-#define ARCH_DLINFO	NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr)
+#define ARCH_DLINFO(sysinfo_ehdr)	NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr)
 
 #endif
 
fs/binfmt_elf.c +3 -3
index 049ff514aa19..bc81f29b3cfb 100644
@@ -171,7 +171,7 @@ static int padzero(unsigned long elf_bss)
 static int
 create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 		unsigned long load_addr, unsigned long interp_load_addr,
-		unsigned long e_entry)
+		unsigned long e_entry, unsigned long sysinfo_ehdr)
 {
 	struct mm_struct *mm = current->mm;
 	unsigned long p = bprm->p;
@@ -251,7 +251,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
 	 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
 	 * ARCH_DLINFO changes
 	 */
-	ARCH_DLINFO;
+	ARCH_DLINFO(sysinfo_ehdr);
 #endif
 	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
 	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
@@ -1255,7 +1255,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		goto out;
 
 	retval = create_elf_tables(bprm, elf_ex,
-			  load_addr, interp_load_addr, e_entry);
+			  load_addr, interp_load_addr, e_entry, sysinfo_ehdr);
 	if (retval < 0)
 		goto out;
 
fs/binfmt_elf_fdpic.c +6 -5
index c9ee3c240855..f0e4803ab6ca 100644
@@ -63,7 +63,7 @@ static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
 
 static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
 				   struct elf_fdpic_params *,
-				   struct elf_fdpic_params *);
+				   struct elf_fdpic_params *, unsigned long);
 
 #ifndef CONFIG_MMU
 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
@@ -434,8 +434,8 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 	current->mm->start_stack = current->mm->start_brk + stack_size;
 #endif
 
-	if (create_elf_fdpic_tables(bprm, current->mm,
-				    &exec_params, &interp_params) < 0)
+	if (create_elf_fdpic_tables(bprm, current->mm, &exec_params,
+				    &interp_params, sysinfo_ehdr) < 0)
 		goto error;
 
 	kdebug("- start_code  %lx", current->mm->start_code);
@@ -496,7 +496,8 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 				   struct mm_struct *mm,
 				   struct elf_fdpic_params *exec_params,
-				   struct elf_fdpic_params *interp_params)
+				   struct elf_fdpic_params *interp_params,
+				   unsigned long sysinfo_ehdr)
 {
 	const struct cred *cred = current_cred();
 	unsigned long sp, csp, nitems;
@@ -664,7 +665,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	/* ARCH_DLINFO must come last so platform specific code can enforce
 	 * special alignment requirements on the AUXV if necessary (eg. PPC).
 	 */
-	ARCH_DLINFO;
+	ARCH_DLINFO(sysinfo_ehdr);
 #endif
 #undef NEW_AUX_ENT
 
Signature · 1 line
2.29.2
#14 lore raw

PATCH v2 8/19 arm/vdso: Remove vdso pointer from mm->context

In reply to Dmitry Safonov

Not used any more.

---
 arch/arm/include/asm/mmu.h |  3 ---
 arch/arm/kernel/vdso.c     | 12 ------------
 2 files changed, 15 deletions(-)
arch/arm/include/asm/mmu.h +0 -3
index 1592a4264488..2397b0a19f59 100644
@@ -12,9 +12,6 @@ typedef struct {
 #endif
 	unsigned int	vmalloc_seq;
 	unsigned long	sigpage;
-#ifdef CONFIG_VDSO
-	unsigned long	vdso;
-#endif
 #ifdef CONFIG_BINFMT_ELF_FDPIC
 	unsigned long	exec_fdpic_loadmap;
 	unsigned long	interp_fdpic_loadmap;
arch/arm/kernel/vdso.c +0 -12
index 710e5ca99a53..4b39c7d8f525 100644
@@ -47,17 +47,8 @@ static const struct vm_special_mapping vdso_data_mapping = {
 	.pages = &vdso_data_page,
 };
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
-		struct vm_area_struct *new_vma)
-{
-	current->mm->context.vdso = new_vma->vm_start;
-
-	return 0;
-}
-
 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
 	.name = "[vdso]",
-	.mremap = vdso_mremap,
 };
 
 struct elfinfo {
@@ -239,8 +230,6 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
 	struct vm_area_struct *vma;
 	unsigned long len;
 
-	mm->context.vdso = 0;
-
 	if (vdso_text_pagelist == NULL)
 		return;
 
@@ -258,7 +247,6 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
 	if (IS_ERR(vma))
 		return;
 
-	mm->context.vdso = addr;
 	*sysinfo_ehdr = addr;
 }
 
Signature · 1 line
2.29.2
#15 lore raw

Re: [PATCH v2 08/19] arm/vdso: Remove vdso pointer from mm->context

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 1 line

Not used any more.

But what about mremap() ? Maybe you should explain why you can remove it ?

Quoted 61 lines
---
  arch/arm/include/asm/mmu.h |  3 ---
  arch/arm/kernel/vdso.c     | 12 ------------
  2 files changed, 15 deletions(-)
arch/arm/include/asm/mmu.h +0 -3
index 1592a4264488..2397b0a19f59 100644
@@ -12,9 +12,6 @@ typedef struct {
  #endif
  	unsigned int	vmalloc_seq;
  	unsigned long	sigpage;
-#ifdef CONFIG_VDSO
-	unsigned long	vdso;
-#endif
  #ifdef CONFIG_BINFMT_ELF_FDPIC
  	unsigned long	exec_fdpic_loadmap;
  	unsigned long	interp_fdpic_loadmap;
arch/arm/kernel/vdso.c +0 -12
index 710e5ca99a53..4b39c7d8f525 100644
@@ -47,17 +47,8 @@ static const struct vm_special_mapping vdso_data_mapping = {
  	.pages = &vdso_data_page,
  };
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
-		struct vm_area_struct *new_vma)
-{
-	current->mm->context.vdso = new_vma->vm_start;
-
-	return 0;
-}
-
  static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
  	.name = "[vdso]",
-	.mremap = vdso_mremap,
  };
 
  struct elfinfo {
@@ -239,8 +230,6 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
  	struct vm_area_struct *vma;
  	unsigned long len;
 
-	mm->context.vdso = 0;
-
  	if (vdso_text_pagelist == NULL)
  		return;
 
@@ -258,7 +247,6 @@ void arm_install_vdso(struct mm_struct *mm, unsigned long addr,
  	if (IS_ERR(vma))
  		return;
 
-	mm->context.vdso = addr;
  	*sysinfo_ehdr = addr;
  }
 
#16 lore raw

Re: [PATCH v2 08/19] arm/vdso: Remove vdso pointer from mm->context

Quoted 6 lines · Christophe Leroy

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 1 line

Not used any more.

But what about mremap() ? Maybe you should explain why you can remove it ?

Yep, it was only to keep track of context->vdso position. I'll add it to the description.

Thanks,
Dmitry

#17 lore raw

PATCH v2 9/19 s390/vdso: Remove vdso_base pointer from mm->context

In reply to Dmitry Safonov

Not used any more.

Cc Christian Borntraeger <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
---
 arch/s390/include/asm/mmu.h |  1 -
 arch/s390/kernel/vdso.c     | 10 ----------
 2 files changed, 11 deletions(-)
arch/s390/include/asm/mmu.h +0 -1
index e12ff0f29d1a..095d0596f700 100644
@@ -15,7 +15,6 @@ typedef struct {
 	unsigned long gmap_asce;
 	unsigned long asce;
 	unsigned long asce_limit;
-	unsigned long vdso_base;
 	/* The mmu context belongs to a secure guest. */
 	atomic_t is_protected;
 	/*
arch/s390/kernel/vdso.c +0 -10
index 810b72f8985c..3f07711a07c1 100644
@@ -58,18 +58,9 @@ static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
 	return 0;
 }
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
-		       struct vm_area_struct *vma)
-{
-	current->mm->context.vdso_base = vma->vm_start;
-
-	return 0;
-}
-
 static const struct vm_special_mapping vdso_mapping = {
 	.name = "[vdso]",
 	.fault = vdso_fault,
-	.mremap = vdso_mremap,
 };
 
 static int __init vdso_setup(char *str)
@@ -204,7 +195,6 @@ int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 		goto out_up;
 	}
 
-	current->mm->context.vdso_base = vdso_base;
 	*sysinfo_ehdr = vdso_base;
 	rc = 0;
 
Signature · 1 line
2.29.2
#18 lore raw

Re: [PATCH v2 09/19] s390/vdso: Remove vdso_base pointer from mm->context

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 1 line

Not used any more.

Same, what about mremap(), why can it be removed ?

Quoted 55 lines
Cc Christian Borntraeger <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
---
  arch/s390/include/asm/mmu.h |  1 -
  arch/s390/kernel/vdso.c     | 10 ----------
  2 files changed, 11 deletions(-)
arch/s390/include/asm/mmu.h +0 -1
index e12ff0f29d1a..095d0596f700 100644
@@ -15,7 +15,6 @@ typedef struct {
  	unsigned long gmap_asce;
  	unsigned long asce;
  	unsigned long asce_limit;
-	unsigned long vdso_base;
  	/* The mmu context belongs to a secure guest. */
  	atomic_t is_protected;
  	/*
arch/s390/kernel/vdso.c +0 -10
index 810b72f8985c..3f07711a07c1 100644
@@ -58,18 +58,9 @@ static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
  	return 0;
  }
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
-		       struct vm_area_struct *vma)
-{
-	current->mm->context.vdso_base = vma->vm_start;
-
-	return 0;
-}
-
  static const struct vm_special_mapping vdso_mapping = {
  	.name = "[vdso]",
  	.fault = vdso_fault,
-	.mremap = vdso_mremap,
  };
 
  static int __init vdso_setup(char *str)
@@ -204,7 +195,6 @@ int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
  		goto out_up;
  	}
 
-	current->mm->context.vdso_base = vdso_base;
  	*sysinfo_ehdr = vdso_base;
  	rc = 0;
 
#19 lore raw

PATCH v2 10/19 sparc/vdso: Remove vdso pointer from mm->context

In reply to Dmitry Safonov

Not used any more.

---
 arch/sparc/include/asm/mmu_64.h | 1 -
 arch/sparc/vdso/vma.c           | 5 +----
 2 files changed, 1 insertion(+), 5 deletions(-)
arch/sparc/include/asm/mmu_64.h +0 -1
index 7e2704c770e9..8e7892890d14 100644
@@ -111,7 +111,6 @@ typedef struct {
 	unsigned long		thp_pte_count;
 	struct tsb_config	tsb_block[MM_NUM_TSBS];
 	struct hv_tsb_descr	tsb_descr[MM_NUM_TSBS];
-	void			*vdso;
 	bool			adi;
 	tag_storage_desc_t	*tag_store;
 	spinlock_t		tag_lock;
arch/sparc/vdso/vma.c +1 -4
index bf9195fe9bcc..255e052223ca 100644
@@ -389,7 +389,6 @@ static int map_vdso(const struct vdso_image *image,
 	}
 
 	text_start = addr - image->sym_vvar_start;
-	current->mm->context.vdso = (void __user *)text_start;
 
 	/*
 	 * MAYWRITE to allow gdb to COW and set breakpoints
@@ -418,9 +417,7 @@ static int map_vdso(const struct vdso_image *image,
 	}
 
 up_fail:
-	if (ret)
-		current->mm->context.vdso = NULL;
-	else
+	if (!ret)
 		*sysinfo_ehdr = text_start;
 
 	mmap_write_unlock(mm);
Signature · 1 line
2.29.2
#20 lore raw

PATCH v2 11/19 mm/mmap: Make vm_special_mapping::mremap return void

In reply to Dmitry Safonov

Previously .mremap() callback needed (int) return to provide way to restrict resizing of a special mapping. Now it's restricted by providing .may_split = special_mapping_split.

Removing (int) return simplifies further changes to special_mapping_mremap() as it won't need save ret code from the callback. Also, it removes needless return 0 from callbacks.

---
 arch/arm/kernel/process.c | 3 +--
 arch/arm64/kernel/vdso.c  | 4 +---
 arch/mips/vdso/genvdso.c  | 3 +--
 arch/x86/entry/vdso/vma.c | 4 +---
 include/linux/mm_types.h  | 2 +-
 mm/mmap.c                 | 2 +-
 6 files changed, 6 insertions(+), 12 deletions(-)
arch/arm/kernel/process.c +1 -2
index 0e90cba8ac7a..5f4eced738f5 100644
@@ -376,11 +376,10 @@ static unsigned long sigpage_addr(const struct mm_struct *mm,
 static struct page *signal_page;
 extern struct page *get_signal_page(void);
 
-static int sigpage_mremap(const struct vm_special_mapping *sm,
+static void sigpage_mremap(const struct vm_special_mapping *sm,
 		struct vm_area_struct *new_vma)
 {
 	current->mm->context.sigpage = new_vma->vm_start;
-	return 0;
 }
 
 static const struct vm_special_mapping sigpage_mapping = {
arch/arm64/kernel/vdso.c +1 -3
index 666338724a07..91c1b7c716b7 100644
@@ -78,12 +78,10 @@ static union {
 } vdso_data_store __page_aligned_data;
 struct vdso_data *vdso_data = vdso_data_store.data;
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
+static void vdso_mremap(const struct vm_special_mapping *sm,
 		struct vm_area_struct *new_vma)
 {
 	current->mm->context.vdso = (void *)new_vma->vm_start;
-
-	return 0;
 }
 
 static int __vdso_init(enum vdso_abi abi)
arch/mips/vdso/genvdso.c +1 -2
index 09e30eb4be86..0303d30cde03 100644
@@ -259,13 +259,12 @@ int main(int argc, char **argv)
 	fprintf(out_file, "#include <linux/linkage.h>\n");
 	fprintf(out_file, "#include <linux/mm.h>\n");
 	fprintf(out_file, "#include <asm/vdso.h>\n");
-	fprintf(out_file, "static int vdso_mremap(\n");
+	fprintf(out_file, "static void vdso_mremap(\n");
 	fprintf(out_file, "	const struct vm_special_mapping *sm,\n");
 	fprintf(out_file, "	struct vm_area_struct *new_vma)\n");
 	fprintf(out_file, "{\n");
 	fprintf(out_file, "	current->mm->context.vdso =\n");
 	fprintf(out_file, "	(void *)(new_vma->vm_start);\n");
-	fprintf(out_file, "	return 0;\n");
 	fprintf(out_file, "}\n");
 
 	/* Write out the stripped VDSO data. */
arch/x86/entry/vdso/vma.c +1 -3
index 5b9020742e66..65780a0164e3 100644
@@ -86,15 +86,13 @@ static void vdso_fix_landing(const struct vdso_image *image,
 #endif
 }
 
-static int vdso_mremap(const struct vm_special_mapping *sm,
+static void vdso_mremap(const struct vm_special_mapping *sm,
 		struct vm_area_struct *new_vma)
 {
 	const struct vdso_image *image = current->mm->context.vdso_image;
 
 	vdso_fix_landing(image, new_vma);
 	current->mm->context.vdso = (void __user *)new_vma->vm_start;
-
-	return 0;
 }
 
 #ifdef CONFIG_TIME_NS
include/linux/mm_types.h +1 -1
index 65df8abd90bd..95a257927dae 100644
@@ -780,7 +780,7 @@ struct vm_special_mapping {
 				struct vm_area_struct *vma,
 				struct vm_fault *vmf);
 
-	int (*mremap)(const struct vm_special_mapping *sm,
+	void (*mremap)(const struct vm_special_mapping *sm,
 		     struct vm_area_struct *new_vma);
 };
 
mm/mmap.c +1 -1
index d0d458632c1b..17fe59a9780b 100644
@@ -3433,7 +3433,7 @@ static int special_mapping_mremap(struct vm_area_struct *new_vma,
 		return -EFAULT;
 
 	if (sm->mremap)
-		return sm->mremap(sm, new_vma);
+		sm->mremap(sm, new_vma);
 
 	return 0;
 }
Signature · 1 line
2.29.2
#21 lore raw

Re: [PATCH v2 11/19] mm/mmap: Make vm_special_mapping::mremap return void

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 23 lines

Previously .mremap() callback needed (int) return to provide way to restrict resizing of a special mapping. Now it's restricted by providing .may_split = special_mapping_split.

Removing (int) return simplifies further changes to special_mapping_mremap() as it won't need save ret code from the callback. Also, it removes needless return 0 from callbacks.

---
  arch/arm/kernel/process.c | 3 +--
  arch/arm64/kernel/vdso.c  | 4 +---
  arch/mips/vdso/genvdso.c  | 3 +--
  arch/x86/entry/vdso/vma.c | 4 +---
  include/linux/mm_types.h  | 2 +-
  mm/mmap.c                 | 2 +-
  6 files changed, 6 insertions(+), 12 deletions(-)
include/linux/mm_types.h +0 -0
index 65df8abd90bd..95a257927dae 100644

include/linux/mm_types.h struct vm_special_mapping { · line 784

Quoted 6 lines
include/linux/mm_types.h +1 -1
@@ -780,7 +780,7 @@ struct vm_special_mapping {
  				struct vm_area_struct *vma,
  				struct vm_fault *vmf);
 
-	int (*mremap)(const struct vm_special_mapping *sm,
+	void (*mremap)(const struct vm_special_mapping *sm,
  		     struct vm_area_struct *new_vma);

Second line should be aligned to first line's parenthesis I think.

Quoted 16 lines
include/linux/mm_types.h +0 -0
  };
 
mm/mmap.c +1 -1
index d0d458632c1b..17fe59a9780b 100644
@@ -3433,7 +3433,7 @@ static int special_mapping_mremap(struct vm_area_struct *new_vma,
  		return -EFAULT;
 
  	if (sm->mremap)
-		return sm->mremap(sm, new_vma);
+		sm->mremap(sm, new_vma);
 
  	return 0;
  }
#22 lore raw

PATCH v2 12/19 x86/signal: Land on &frame->retcode when vdso isn't mapped

In reply to Dmitry Safonov

Since commit 9fbbd4dd17d0 ("x86: Don't require the vDSO for handling a.out signals") after processing 32-bit signal if there is no vdso mapped frame->retcode is used as a landing.
Do the same for rt ia32 signals.
It also makes the ia32 compat signals match the native ia32 case.

This shouldn't be mistaken for encouragement for running binaries with executable stack, rather something to do in hopefully very rare situation with disabled or unmapped vdso and absent SA_RESTORER. For non-executable stack it'll segfault on attempt to land, rather than land on a random address where vdso was previously mapped. For programs with executable stack it'll just do the same for rt signals as for non-rt.

Discouraging users to run with executable stack is done separately in commit 47a2ebb7f505 ("execve: warn if process starts with executable stack").

---
 arch/x86/ia32/ia32_signal.c | 12 +++++++-----
 arch/x86/kernel/signal.c    | 23 ++++++++++-------------
 2 files changed, 17 insertions(+), 18 deletions(-)
arch/x86/ia32/ia32_signal.c +7 -5
index 81cf22398cd1..ea3db15b57bf 100644
@@ -270,8 +270,8 @@ int ia32_setup_frame(int sig, struct ksignal *ksig,
 	unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
 	unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
 	/*
-	 * These are actually not used anymore, but left because some
-	 * gdb versions depend on them as a marker.
+	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
+	 * gdb uses it as a signature to notice signal handler stack frames.
 	 */
 	unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
 	user_access_end();
@@ -336,14 +336,16 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
 
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
-	else
+	else if (current->mm->context.vdso)
 		restorer = current->mm->context.vdso +
 			vdso_image_32.sym___kernel_rt_sigreturn;
+	else
+		restorer = &frame->retcode;
 	unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
 
 	/*
-	 * Not actually used anymore, but left because some gdb
-	 * versions need it.
+	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
+	 * gdb uses it as a signature to notice signal handler stack frames.
 	 */
 	unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
 	unsafe_put_sigcontext32(&frame->uc.uc_mcontext, fp, regs, set, Efault);
arch/x86/kernel/signal.c +10 -13
index ea794a083c44..372ec09dc4ac 100644
@@ -317,23 +317,20 @@ __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
 	unsafe_put_user(sig, &frame->sig, Efault);
 	unsafe_put_sigcontext(&frame->sc, fp, regs, set, Efault);
 	unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
-	if (current->mm->context.vdso)
+	if (ksig->ka.sa.sa_flags & SA_RESTORER)
+		restorer = ksig->ka.sa.sa_restorer;
+	else if (current->mm->context.vdso)
 		restorer = current->mm->context.vdso +
 			vdso_image_32.sym___kernel_sigreturn;
 	else
 		restorer = &frame->retcode;
-	if (ksig->ka.sa.sa_flags & SA_RESTORER)
-		restorer = ksig->ka.sa.sa_restorer;
 
 	/* Set up to return from userspace.  */
 	unsafe_put_user(restorer, &frame->pretcode, Efault);
 
 	/*
 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
-	 *
-	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
-	 * reasons and because gdb uses it as a signature to notice
-	 * signal handler stack frames.
+	 * gdb uses it as a signature to notice signal handler stack frames.
 	 */
 	unsafe_put_user(*((u64 *)&retcode), (u64 *)frame->retcode, Efault);
 	user_access_end();
@@ -382,18 +379,18 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig,
 	unsafe_save_altstack(&frame->uc.uc_stack, regs->sp, Efault);
 
 	/* Set up to return from userspace.  */
-	restorer = current->mm->context.vdso +
-		vdso_image_32.sym___kernel_rt_sigreturn;
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
+	else if (current->mm->context.vdso)
+		restorer = current->mm->context.vdso +
+			vdso_image_32.sym___kernel_rt_sigreturn;
+	else
+		restorer = &frame->retcode;
 	unsafe_put_user(restorer, &frame->pretcode, Efault);
 
 	/*
 	 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
-	 *
-	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
-	 * reasons and because gdb uses it as a signature to notice
-	 * signal handler stack frames.
+	 * gdb uses it as a signature to notice signal handler stack frames.
 	 */
 	unsafe_put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode, Efault);
 	unsafe_put_sigcontext(&frame->uc.uc_mcontext, fp, regs, set, Efault);
Signature · 1 line
2.29.2
maintainer Acked-by on v2 12/19
#23 lore raw

Re: [PATCH v2 12/19] x86/signal: Land on &frame->retcode when vdso isn't mapped

Quoted 15 lines · Dmitry Safonov

Since commit 9fbbd4dd17d0 ("x86: Don't require the vDSO for handling a.out signals") after processing 32-bit signal if there is no vdso mapped frame->retcode is used as a landing.
Do the same for rt ia32 signals.
It also makes the ia32 compat signals match the native ia32 case.

This shouldn't be mistaken for encouragement for running binaries with executable stack, rather something to do in hopefully very rare situation with disabled or unmapped vdso and absent SA_RESTORER. For non-executable stack it'll segfault on attempt to land, rather than land on a random address where vdso was previously mapped. For programs with executable stack it'll just do the same for rt signals as for non-rt.

Quoted 5 lines

Discouraging users to run with executable stack is done separately in commit 47a2ebb7f505 ("execve: warn if process starts with executable stack").

The new code is more readable than the old code, too.

#24 lore raw

PATCH v2 13/19 x86/signal: Check if vdso_image_32 is mapped before trying to land on it

In reply to Dmitry Safonov

Provide current_has_vdso_image_32() helper and check it apriory landing attempt on vdso vma.
The helper is a macro, not a static inline funciton to avoid linux/sched/task_stack.h inclusion in asm/vdso.h.

---
 arch/x86/entry/common.c     | 7 ++++++-
 arch/x86/ia32/ia32_signal.c | 4 ++--
 arch/x86/include/asm/vdso.h | 4 ++++
 arch/x86/kernel/signal.c    | 4 ++--
 4 files changed, 14 insertions(+), 5 deletions(-)
arch/x86/entry/common.c +6 -1
index 18d8f17f755c..d9ab58cc765b 100644
@@ -142,11 +142,16 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
 __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
 {
+	unsigned long landing_pad;
+
+	if (!current_has_vdso_image_32())
+		force_sigsegv(SIGSEGV);
+
 	/*
 	 * Called using the internal vDSO SYSENTER/SYSCALL32 calling
 	 * convention.  Adjust regs so it looks like we entered using int80.
 	 */
-	unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
+	landing_pad = (unsigned long)current->mm->context.vdso +
 					vdso_image_32.sym_int80_landing_pad;
 
 	/*
arch/x86/ia32/ia32_signal.c +2 -2
index ea3db15b57bf..f87ed1d53938 100644
@@ -255,7 +255,7 @@ int ia32_setup_frame(int sig, struct ksignal *ksig,
 		restorer = ksig->ka.sa.sa_restorer;
 	} else {
 		/* Return stub is in 32bit vsyscall page */
-		if (current->mm->context.vdso)
+		if (current_has_vdso_image_32())
 			restorer = current->mm->context.vdso +
 				vdso_image_32.sym___kernel_sigreturn;
 		else
@@ -336,7 +336,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
 
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
-	else if (current->mm->context.vdso)
+	else if (current_has_vdso_image_32())
 		restorer = current->mm->context.vdso +
 			vdso_image_32.sym___kernel_rt_sigreturn;
 	else
arch/x86/include/asm/vdso.h +4 -0
index b5d23470f56b..e3829c3a6149 100644
@@ -41,6 +41,10 @@ extern const struct vdso_image vdso_image_x32;
 
 #if defined CONFIG_X86_32 || defined CONFIG_COMPAT
 extern const struct vdso_image vdso_image_32;
+
+#define current_has_vdso_image_32()					\
+	likely(current->mm->context.vdso_image == &vdso_image_32 &&	\
+		!!current->mm->context.vdso)
 #endif
 
 extern void __init init_vdso_image(const struct vdso_image *image);
arch/x86/kernel/signal.c +2 -2
index 372ec09dc4ac..6fed2e523e0a 100644
@@ -319,7 +319,7 @@ __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
 	unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
-	else if (current->mm->context.vdso)
+	else if (current_has_vdso_image_32())
 		restorer = current->mm->context.vdso +
 			vdso_image_32.sym___kernel_sigreturn;
 	else
@@ -381,7 +381,7 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig,
 	/* Set up to return from userspace.  */
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
-	else if (current->mm->context.vdso)
+	else if (current_has_vdso_image_32())
 		restorer = current->mm->context.vdso +
 			vdso_image_32.sym___kernel_rt_sigreturn;
 	else
Signature · 1 line
2.29.2
maintainer
#25 lore raw

Re: [PATCH v2 13/19] x86/signal: Check if vdso_image_32 is mapped before trying to land on it

Quoted 5 lines · Dmitry Safonov

Provide current_has_vdso_image_32() helper and check it apriory landing attempt on vdso vma.
The helper is a macro, not a static inline funciton to avoid linux/sched/task_stack.h inclusion in asm/vdso.h.

Can you make this more general? For example:

current_has_vdso(&vdso_image_whatever)

also:

arch/x86/entry/common.c static noinstr bool __do_fast_syscall_32(struct pt_regs *regs) · line 148

Quoted 12 lines
arch/x86/entry/common.c +4 -0
index 18d8f17f755c..d9ab58cc765b 100644
@@ -142,11 +142,16 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
 __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
 {
+       unsigned long landing_pad;
+
+       if (!current_has_vdso_image_32())
+               force_sigsegv(SIGSEGV);

Falling through seems incorrect here. I would instead write 0 to rip, do the force_sigsegv(), and exit, making sure that exiting goes through the correct path.

#26 lore raw

Re: [PATCH v2 13/19] x86/signal: Check if vdso_image_32 is mapped before trying to land on it

Quoted 10 lines · Andy Lutomirski
Quoted 5 lines · Dmitry Safonov

Provide current_has_vdso_image_32() helper and check it apriory landing attempt on vdso vma.
The helper is a macro, not a static inline funciton to avoid linux/sched/task_stack.h inclusion in asm/vdso.h.

Can you make this more general? For example:

current_has_vdso(&vdso_image_whatever)

Sounds good, will do.

Quoted 20 lines

also:

arch/x86/entry/common.c static noinstr bool __do_fast_syscall_32(struct pt_regs *regs) · line 148

Quoted 12 lines
arch/x86/entry/common.c +4 -0
index 18d8f17f755c..d9ab58cc765b 100644
@@ -142,11 +142,16 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
 __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
 {
+       unsigned long landing_pad;
+
+       if (!current_has_vdso_image_32())
+               force_sigsegv(SIGSEGV);

Falling through seems incorrect here. I would instead write 0 to rip, do the force_sigsegv(), and exit, making sure that exiting goes through the correct path.

Something like this?

: if (!current_has_vdso(&vdso_image_32)) {

:               regs->ax = -EFAULT;
:               regs->ip = 0;
:               force_sigsegv(SIGSEGV);
:               syscall_exit_to_user_mode(regs);

: }

Thanks,
Dmitry

#27 lore raw

PATCH v2 14/19 mm: Add vdso_base in mm_struct

In reply to Dmitry Safonov

Instead of having every architecture to define vdso_base/vdso_addr etc, provide a generic mechanism to track vdso_base for landing in userspace. It'll minimize per-architecture difference, the number of callbacks to provide.

Originally, it started from thread [1] where the need for .close() callback on vm_special_mapping was pointed, this generic code besides removing duplicated .mremap() callbacks provides a cheaper way to support munmap() on vdso mappings without introducing .close() callbacks for every architecture (with would bring even more code duplication).

[1]: https://lore.kernel.org/linux-arch/CAJwJo6ZANqYkSHbQ+3b+Fi_VT80MtrzEV5yreQAWx-L8j8x2zA@mail.gmail.com/

---
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  3 ++-
 fs/aio.c                                  |  3 ++-
 include/linux/mm.h                        |  3 ++-
 include/linux/mm_types.h                  | 10 ++++++++++
 mm/Kconfig                                |  3 +++
 mm/mmap.c                                 | 19 ++++++++++++++++++-
 mm/mremap.c                               |  2 +-
 7 files changed, 38 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +2 -1
index e916646adc69..786c97203bf6 100644
@@ -1458,7 +1458,8 @@ static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
-static int pseudo_lock_dev_mremap(struct vm_area_struct *area, unsigned long flags)
+static int pseudo_lock_dev_mremap(struct vm_area_struct *old_vma,
+		struct vm_area_struct *new_vma, unsigned long flags)
 {
 	/* Not supported */
 	return -EINVAL;
fs/aio.c +2 -1
index d213be7b8a7e..9b205ebf17e8 100644
@@ -323,7 +323,8 @@ static void aio_free_ring(struct kioctx *ctx)
 	}
 }
 
-static int aio_ring_mremap(struct vm_area_struct *vma, unsigned long flags)
+static int aio_ring_mremap(struct vm_area_struct *old_vma,
+			   struct vm_area_struct *vma, unsigned long flags)
 {
 	struct file *file = vma->vm_file;
 	struct mm_struct *mm = vma->vm_mm;
include/linux/mm.h +2 -1
index a1c25da94663..dbda1b91f971 100644
@@ -559,7 +559,8 @@ struct vm_operations_struct {
 	void (*close)(struct vm_area_struct * area);
 	/* Called any time before splitting to check if it's allowed */
 	int (*may_split)(struct vm_area_struct *area, unsigned long addr);
-	int (*mremap)(struct vm_area_struct *area, unsigned long flags);
+	int (*mremap)(struct vm_area_struct *old_vma,
+			struct vm_area_struct *new_vma, unsigned long flags);
 	/*
 	 * Called by mprotect() to make driver-specific permission
 	 * checks before mprotect() is finalised.   The VMA must not
include/linux/mm_types.h +10 -0
index 95a257927dae..e1ca750874af 100644
@@ -496,6 +496,16 @@ struct mm_struct {
 
 		/* Architecture-specific MM context */
 		mm_context_t context;
+#ifdef CONFIG_ARCH_HAS_VDSO_BASE
+		/*
+		 * Address of special mapping VMA to land after processing
+		 * a signal. Reads are unprotected: if a thread unmaps or
+		 * mremaps the mapping while another thread is processing
+		 * a signal, it can segfault while landing.
+		 */
+		void __user *vdso_base;
+#endif
+#define UNMAPPED_VDSO_BASE TASK_SIZE_MAX
 
 		unsigned long flags; /* Must use atomic bitops to access */
 
mm/Kconfig +3 -0
index 29904dc16bfc..941ae0597734 100644
@@ -893,4 +893,7 @@ config SECRETMEM
 	select GENERIC_ALLOCATOR
 	select CMA
 
+config ARCH_HAS_VDSO_BASE
+	bool
+
 endmenu
mm/mmap.c +18 -1
index 17fe59a9780b..561afc3f1744 100644
@@ -3409,11 +3409,25 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
 
 static vm_fault_t special_mapping_fault(struct vm_fault *vmf);
 
+static void update_vdso_base(struct vm_area_struct *old_vma,
+				unsigned long new_addr)
+{
+#ifdef CONFIG_ARCH_HAS_VDSO_BASE
+	struct mm_struct *mm = old_vma->vm_mm;
+
+	if (WARN_ON_ONCE(!mm))
+		return;
+	if (old_vma->vm_start == (unsigned long)mm->vdso_base)
+		mm->vdso_base = (void __user *)new_addr;
+#endif
+}
+
 /*
  * Having a close hook prevents vma merging regardless of flags.
  */
 static void special_mapping_close(struct vm_area_struct *vma)
 {
+	update_vdso_base(vma, UNMAPPED_VDSO_BASE);
 }
 
 static const char *special_mapping_name(struct vm_area_struct *vma)
@@ -3421,7 +3435,8 @@ static const char *special_mapping_name(struct vm_area_struct *vma)
 	return ((struct vm_special_mapping *)vma->vm_private_data)->name;
 }
 
-static int special_mapping_mremap(struct vm_area_struct *new_vma,
+static int special_mapping_mremap(struct vm_area_struct *old_vma,
+				  struct vm_area_struct *new_vma,
 				  unsigned long flags)
 {
 	struct vm_special_mapping *sm = new_vma->vm_private_data;
@@ -3435,6 +3450,8 @@ static int special_mapping_mremap(struct vm_area_struct *new_vma,
 	if (sm->mremap)
 		sm->mremap(sm, new_vma);
 
+	update_vdso_base(old_vma, new_vma->vm_start);
+
 	return 0;
 }
 
mm/mremap.c +1 -1
index c5590afe7165..9595f6b72101 100644
@@ -543,7 +543,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,
 	if (moved_len < old_len) {
 		err = -ENOMEM;
 	} else if (vma->vm_ops && vma->vm_ops->mremap) {
-		err = vma->vm_ops->mremap(new_vma, flags);
+		err = vma->vm_ops->mremap(vma, new_vma, flags);
 	}
 
 	if (unlikely(err)) {
Signature · 1 line
2.29.2
maintainer
#28 lore raw

Re: [PATCH v2 14/19] mm: Add vdso_base in mm_struct

Quoted 11 lines · Dmitry Safonov

Instead of having every architecture to define vdso_base/vdso_addr etc, provide a generic mechanism to track vdso_base for landing in userspace. It'll minimize per-architecture difference, the number of callbacks to provide.

Originally, it started from thread [1] where the need for .close() callback on vm_special_mapping was pointed, this generic code besides removing duplicated .mremap() callbacks provides a cheaper way to support munmap() on vdso mappings without introducing .close() callbacks for every architecture (with would bring even more code duplication).

I admit I'm slightly dubious about passing in the old vma and comparing its start address as opposed to using some more explicit "is this the vdso" check, but I guess I'm okay with that.

#29 lore raw

PATCH v2 15/19 x86/vdso: Migrate to generic vdso_base

In reply to Dmitry Safonov

Generic way to track the landing vma area.
As a bonus, after unmapping vdso, kernel won't try to land on its previous position (due to UNMAPPED_VDSO_BASE check instead of context.vdso ?= 0 check).

---
 arch/x86/Kconfig              | 1 +
 arch/x86/entry/common.c       | 2 +-
 arch/x86/entry/vdso/extable.c | 4 ++--
 arch/x86/entry/vdso/vma.c     | 9 ++++-----
 arch/x86/ia32/ia32_signal.c   | 4 ++--
 arch/x86/include/asm/mmu.h    | 1 -
 arch/x86/include/asm/vdso.h   | 2 +-
 arch/x86/kernel/signal.c      | 4 ++--
 8 files changed, 13 insertions(+), 14 deletions(-)
arch/x86/Kconfig +1 -0
index d676e0bad1f1..4f06760f849e 100644
@@ -85,6 +85,7 @@ config X86
 	select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
 	select ARCH_HAS_SYSCALL_WRAPPER
 	select ARCH_HAS_UBSAN_SANITIZE_ALL
+	select ARCH_HAS_VDSO_BASE
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select ARCH_MIGHT_HAVE_ACPI_PDC		if ACPI
arch/x86/entry/common.c +1 -1
index d9ab58cc765b..655b7d8fe734 100644
@@ -151,7 +151,7 @@ __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
 	 * Called using the internal vDSO SYSENTER/SYSCALL32 calling
 	 * convention.  Adjust regs so it looks like we entered using int80.
 	 */
-	landing_pad = (unsigned long)current->mm->context.vdso +
+	landing_pad = (unsigned long)current->mm->vdso_base +
 					vdso_image_32.sym_int80_landing_pad;
 
 	/*
arch/x86/entry/vdso/extable.c +2 -2
index afcf5b65beef..16688c2d032c 100644
@@ -25,10 +25,10 @@ bool fixup_vdso_exception(struct pt_regs *regs, int trapnr,
 	if (trapnr == X86_TRAP_DB || trapnr == X86_TRAP_BP)
 		return false;
 
-	if (!current->mm->context.vdso)
+	if (current->mm->vdso_base == (void *)UNMAPPED_VDSO_BASE)
 		return false;
 
-	base =  (unsigned long)current->mm->context.vdso + image->extable_base;
+	base = (unsigned long)current->mm->vdso_base + image->extable_base;
 	nr_entries = image->extable_len / (sizeof(*extable));
 	extable = image->extable;
 
arch/x86/entry/vdso/vma.c +4 -5
index 65780a0164e3..29a795face9d 100644
@@ -77,7 +77,7 @@ static void vdso_fix_landing(const struct vdso_image *image,
 		struct pt_regs *regs = current_pt_regs();
 		unsigned long vdso_land = image->sym_int80_landing_pad;
 		unsigned long old_land_addr = vdso_land +
-			(unsigned long)current->mm->context.vdso;
+			(unsigned long)current->mm->vdso_base;
 
 		/* Fixing userspace landing - look at do_fast_syscall_32 */
 		if (regs->ip == old_land_addr)
@@ -92,7 +92,6 @@ static void vdso_mremap(const struct vm_special_mapping *sm,
 	const struct vdso_image *image = current->mm->context.vdso_image;
 
 	vdso_fix_landing(image, new_vma);
-	current->mm->context.vdso = (void __user *)new_vma->vm_start;
 }
 
 #ifdef CONFIG_TIME_NS
@@ -287,7 +286,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr,
 		ret = PTR_ERR(vma);
 		do_munmap(mm, text_start, image->size, NULL);
 	} else {
-		current->mm->context.vdso = (void __user *)text_start;
+		current->mm->vdso_base = (void __user *)text_start;
 		current->mm->context.vdso_image = image;
 		*sysinfo_ehdr = text_start;
 	}
@@ -362,8 +361,8 @@ int map_vdso_once(const struct vdso_image *image, unsigned long addr)
 	 * Check if we have already mapped vdso blob - fail to prevent
 	 * abusing from userspace install_speciall_mapping, which may
 	 * not do accounting and rlimit right.
-	 * We could search vma near context.vdso, but it's a slowpath,
-	 * so let's explicitly check all VMAs to be completely sure.
+	 * It's a slowpath, let's explicitly check all VMAs to be
+	 * completely sure.
 	 */
 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
 		if (vma_is_special_mapping(vma, &vdso_mapping) ||
arch/x86/ia32/ia32_signal.c +2 -2
index f87ed1d53938..67204b1eeea0 100644
@@ -256,7 +256,7 @@ int ia32_setup_frame(int sig, struct ksignal *ksig,
 	} else {
 		/* Return stub is in 32bit vsyscall page */
 		if (current_has_vdso_image_32())
-			restorer = current->mm->context.vdso +
+			restorer = current->mm->vdso_base +
 				vdso_image_32.sym___kernel_sigreturn;
 		else
 			restorer = &frame->retcode;
@@ -337,7 +337,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
 	else if (current_has_vdso_image_32())
-		restorer = current->mm->context.vdso +
+		restorer = current->mm->vdso_base +
 			vdso_image_32.sym___kernel_rt_sigreturn;
 	else
 		restorer = &frame->retcode;
arch/x86/include/asm/mmu.h +0 -1
index 5d7494631ea9..7bd10e6b8386 100644
@@ -43,7 +43,6 @@ typedef struct {
 #endif
 
 	struct mutex lock;
-	void __user *vdso;			/* vdso base address */
 	const struct vdso_image *vdso_image;	/* vdso image in use */
 
 	atomic_t perf_rdpmc_allowed;	/* nonzero if rdpmc is allowed */
arch/x86/include/asm/vdso.h +1 -1
index e3829c3a6149..31b5695eded4 100644
@@ -44,7 +44,7 @@ extern const struct vdso_image vdso_image_32;
 
 #define current_has_vdso_image_32()					\
 	likely(current->mm->context.vdso_image == &vdso_image_32 &&	\
-		!!current->mm->context.vdso)
+	(unsigned long)current->mm->vdso_base != UNMAPPED_VDSO_BASE)
 #endif
 
 extern void __init init_vdso_image(const struct vdso_image *image);
arch/x86/kernel/signal.c +2 -2
index 6fed2e523e0a..85161df044f2 100644
@@ -320,7 +320,7 @@ __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
 	else if (current_has_vdso_image_32())
-		restorer = current->mm->context.vdso +
+		restorer = current->mm->vdso_base +
 			vdso_image_32.sym___kernel_sigreturn;
 	else
 		restorer = &frame->retcode;
@@ -382,7 +382,7 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig,
 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
 		restorer = ksig->ka.sa.sa_restorer;
 	else if (current_has_vdso_image_32())
-		restorer = current->mm->context.vdso +
+		restorer = current->mm->vdso_base +
 			vdso_image_32.sym___kernel_rt_sigreturn;
 	else
 		restorer = &frame->retcode;
Signature · 1 line
2.29.2
#30 lore raw

Re: [PATCH v2 15/19] x86/vdso: Migrate to generic vdso_base

Thank you for the patch! Perhaps something to improve:

Hi Dmitry,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on 62918e6fd7b5751c1285c7f8c6cbd27eb6600c02]

url:    https://github.com/0day-ci/linux/commits/Dmitry-Safonov/Add-generic-vdso_base-tracking/20201124-083732
base:    62918e6fd7b5751c1285c7f8c6cbd27eb6600c02
config: i386-randconfig-s001-20201123 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-134-gb59dbdaf-dirty
        # https://github.com/0day-ci/linux/commit/bef228ec5535f5941e842ca57e7bd54d23427cde
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Dmitry-Safonov/Add-generic-vdso_base-tracking/20201124-083732
        git checkout bef228ec5535f5941e842ca57e7bd54d23427cde
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"

>> arch/x86/entry/vdso/extable.c:28:36: sparse: sparse: incompatible types in comparison expression (different address spaces):
>> arch/x86/entry/vdso/extable.c:28:36: sparse:    void [noderef] __user *
>> arch/x86/entry/vdso/extable.c:28:36: sparse:    void *

vim +28 arch/x86/entry/vdso/extable.c

    11	
    12	bool fixup_vdso_exception(struct pt_regs *regs, int trapnr,
    13				  unsigned long error_code, unsigned long fault_addr)
    14	{
    15		const struct vdso_image *image = current->mm->context.vdso_image;
    16		const struct vdso_exception_table_entry *extable;
    17		unsigned int nr_entries, i;
    18		unsigned long base;
    19	
    20		/*
    21		 * Do not attempt to fixup #DB or #BP.  It's impossible to identify
    22		 * whether or not a #DB/#BP originated from within an SGX enclave and
    23		 * SGX enclaves are currently the only use case for vDSO fixup.
    24		 */
    25		if (trapnr == X86_TRAP_DB || trapnr == X86_TRAP_BP)
    26			return false;
    27	
  > 28		if (current->mm->vdso_base == (void *)UNMAPPED_VDSO_BASE)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#31 lore raw

PATCH v2 16/19 arm/vdso: Migrate to generic vdso_base

In reply to Dmitry Safonov

Generic way to track the landing vma area.
As a bonus, after unmapping sigpage, kernel won't try to land on its previous position (due to UNMAPPED_VDSO_BASE check instead of context.vdso ?= 0 check).

---
 arch/arm/Kconfig          | 1 +
 arch/arm/kernel/process.c | 9 +--------
 arch/arm/kernel/signal.c  | 6 +++++-
 3 files changed, 7 insertions(+), 9 deletions(-)
arch/arm/Kconfig +1 -0
index a6330e7eb2b8..1df40666b024 100644
@@ -24,6 +24,7 @@ config ARM
 	select ARCH_HAS_SYNC_DMA_FOR_CPU if SWIOTLB
 	select ARCH_HAS_TEARDOWN_DMA_OPS if MMU
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
+	select ARCH_HAS_VDSO_BASE
 	select ARCH_HAVE_CUSTOM_GPIO_H
 	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_KEEP_MEMBLOCK
arch/arm/kernel/process.c +1 -8
index 5f4eced738f5..c6d44b34b05e 100644
@@ -376,16 +376,9 @@ static unsigned long sigpage_addr(const struct mm_struct *mm,
 static struct page *signal_page;
 extern struct page *get_signal_page(void);
 
-static void sigpage_mremap(const struct vm_special_mapping *sm,
-		struct vm_area_struct *new_vma)
-{
-	current->mm->context.sigpage = new_vma->vm_start;
-}
-
 static const struct vm_special_mapping sigpage_mapping = {
 	.name = "[sigpage]",
 	.pages = &signal_page,
-	.mremap = sigpage_mremap,
 };
 
 int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
@@ -423,7 +416,7 @@ int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 		goto up_fail;
 	}
 
-	mm->context.sigpage = addr;
+	mm->vdso_base = (void __user *)addr;
 
 	/* Unlike the sigpage, failure to install the vdso is unlikely
 	 * to be fatal to the process, so no error check needed
arch/arm/kernel/signal.c +5 -1
index 9d2e916121be..ac9c0b4869c9 100644
@@ -451,13 +451,17 @@ setup_return(struct pt_regs *regs, struct ksignal *ksig,
 #ifdef CONFIG_MMU
 		if (cpsr & MODE32_BIT) {
 			struct mm_struct *mm = current->mm;
+			unsigned long land = (unsigned long)mm->vdso_base;
+
+			if (land == UNMAPPED_VDSO_BASE)
+				return 1;
 
 			/*
 			 * 32-bit code can use the signal return page
 			 * except when the MPU has protected the vectors
 			 * page from PL0
 			 */
-			retcode = mm->context.sigpage + signal_return_offset +
+			retcode = land + signal_return_offset +
 				  (idx << 2) + thumb;
 		} else
 #endif
Signature · 1 line
2.29.2
#32 lore raw

PATCH v2 17/19 arm64/vdso: Migrate compat signals to generic vdso_base

In reply to Dmitry Safonov

Generic way to track the landing vma area.
As a bonus, after unmapping sigpage, kernel won't try to land on its previous position (due to UNMAPPED_VDSO_BASE check instead of context.vdso ?= 0 check).

---
 arch/arm64/Kconfig           |  1 +
 arch/arm64/kernel/signal32.c | 17 ++++++++++++-----
 arch/arm64/kernel/vdso.c     |  2 +-
 3 files changed, 14 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +1 -0
index 35d8e4acd38d..a93ec99644b8 100644
@@ -38,6 +38,7 @@ config ARM64
 	select ARCH_HAS_SYSCALL_WRAPPER
 	select ARCH_HAS_TEARDOWN_DMA_OPS if IOMMU_SUPPORT
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
+	select ARCH_HAS_VDSO_BASE
 	select ARCH_HAVE_ELF_PROT
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select ARCH_INLINE_READ_LOCK if !PREEMPTION
arch/arm64/kernel/signal32.c +12 -5
index 2f507f565c48..3d2ac8062e99 100644
@@ -315,7 +315,7 @@ static void __user *compat_get_sigframe(struct ksignal *ksig,
 	return frame;
 }
 
-static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
+static int compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
 				compat_ulong_t __user *rc, void __user *frame,
 				int usig)
 {
@@ -342,13 +342,16 @@ static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
 		retcode = ptr_to_compat(ka->sa.sa_restorer);
 	} else {
 		/* Set up sigreturn pointer */
+		unsigned long land = (unsigned long)current->mm->vdso_base;
 		unsigned int idx = thumb << 1;
 
 		if (ka->sa.sa_flags & SA_SIGINFO)
 			idx += 3;
 
-		retcode = (unsigned long)current->mm->context.sigpage +
-			  (idx << 2) + thumb;
+		if (land == UNMAPPED_VDSO_BASE)
+			return 1;
+
+		retcode = land + (idx << 2) + thumb;
 	}
 
 	regs->regs[0]	= usig;
@@ -356,6 +359,8 @@ static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
 	regs->compat_lr	= retcode;
 	regs->pc	= handler;
 	regs->pstate	= spsr;
+
+	return 0;
 }
 
 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
@@ -425,7 +430,8 @@ int compat_setup_rt_frame(int usig, struct ksignal *ksig,
 	err |= compat_setup_sigframe(&frame->sig, regs, set);
 
 	if (err == 0) {
-		compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
+		err = compat_setup_return(regs, &ksig->ka,
+					  frame->sig.retcode, frame, usig);
 		regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
 		regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
 	}
@@ -448,7 +454,8 @@ int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
 
 	err |= compat_setup_sigframe(frame, regs, set);
 	if (err == 0)
-		compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
+		err = compat_setup_return(regs, &ksig->ka,
+					  frame->retcode, frame, usig);
 
 	return err;
 }
arch/arm64/kernel/vdso.c +1 -1
index 91c1b7c716b7..03a6519d50cd 100644
@@ -394,7 +394,7 @@ static int aarch32_sigreturn_setup(struct mm_struct *mm)
 	if (IS_ERR(ret))
 		goto out;
 
-	mm->context.sigpage = (void *)addr;
+	mm->vdso_base = (void __user *)addr;
 
 out:
 	return PTR_ERR_OR_ZERO(ret);
Signature · 1 line
2.29.2
#33 lore raw

PATCH v2 18/19 arm64/vdso: Migrate native signals to generic vdso_base

In reply to Dmitry Safonov

Generic way to track the land vma area.
As a bonus, after unmapping vdso, kernel won't try to land on its previous position (due to UNMAPPED_VDSO_BASE check instead of context.vdso ?= 0 check).

---
 arch/arm64/kernel/signal.c | 10 +++++++---
 arch/arm64/kernel/vdso.c   | 16 ++++++----------
 2 files changed, 13 insertions(+), 13 deletions(-)
arch/arm64/kernel/signal.c +7 -3
index bec6ef69704f..96e8988f033d 100644
@@ -723,9 +723,10 @@ static int get_sigframe(struct rt_sigframe_user_layout *user,
 	return 0;
 }
 
-static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
+static int setup_return(struct pt_regs *regs, struct k_sigaction *ka,
 			 struct rt_sigframe_user_layout *user, int usig)
 {
+	unsigned long land = (unsigned long)current->mm->vdso_base;
 	__sigrestore_t sigtramp;
 
 	regs->regs[0] = usig;
@@ -754,10 +755,13 @@ static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
 
 	if (ka->sa.sa_flags & SA_RESTORER)
 		sigtramp = ka->sa.sa_restorer;
+	else if (land != UNMAPPED_VDSO_BASE)
+		sigtramp = VDSO_SYMBOL(land, sigtramp);
 	else
-		sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
+		return 1;
 
 	regs->regs[30] = (unsigned long)sigtramp;
+	return 0;
 }
 
 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
@@ -780,7 +784,7 @@ static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
 	err |= setup_sigframe(&user, regs, set);
 	if (err == 0) {
-		setup_return(regs, &ksig->ka, &user, usig);
+		err = setup_return(regs, &ksig->ka, &user, usig);
 		if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
 			err |= copy_siginfo_to_user(&frame->info, &ksig->info);
 			regs->regs[1] = (unsigned long)&frame->info;
arch/arm64/kernel/vdso.c +6 -10
index 03a6519d50cd..5b44463b739a 100644
@@ -78,12 +78,6 @@ static union {
 } vdso_data_store __page_aligned_data;
 struct vdso_data *vdso_data = vdso_data_store.data;
 
-static void vdso_mremap(const struct vm_special_mapping *sm,
-		struct vm_area_struct *new_vma)
-{
-	current->mm->context.vdso = (void *)new_vma->vm_start;
-}
-
 static int __vdso_init(enum vdso_abi abi)
 {
 	int i;
@@ -239,7 +233,6 @@ static int __setup_additional_pages(enum vdso_abi abi,
 		gp_flags = VM_ARM64_BTI;
 
 	vdso_base += VVAR_NR_PAGES * PAGE_SIZE;
-	mm->context.vdso = (void *)vdso_base;
 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
 				       VM_READ|VM_EXEC|gp_flags|
 				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
@@ -247,12 +240,17 @@ static int __setup_additional_pages(enum vdso_abi abi,
 	if (IS_ERR(ret))
 		goto up_fail;
 
+	/*
+	 * 32-bit ABI is to land on sigpage (see aarch32_sigreturn_setup()),
+	 * 64-bit on vDSO.
+	 */
+	if (abi == VDSO_ABI_AA64)
+		mm->vdso_base = (void __user *)vdso_base;
 	*sysinfo_ehdr = vdso_base;
 
 	return 0;
 
 up_fail:
-	mm->context.vdso = NULL;
 	return PTR_ERR(ret);
 }
 
@@ -285,7 +283,6 @@ static struct vm_special_mapping aarch32_vdso_maps[] = {
 	},
 	[AA32_MAP_VDSO] = {
 		.name = "[vdso]",
-		.mremap = vdso_mremap,
 	},
 };
 
@@ -431,7 +428,6 @@ static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = {
 	},
 	[AA64_MAP_VDSO] = {
 		.name	= "[vdso]",
-		.mremap = vdso_mremap,
 	},
 };
 
Signature · 1 line
2.29.2
#34 lore raw

PATCH v2 19/19 mips/vdso: Migrate to generic vdso_base

In reply to Dmitry Safonov

Generic way to track the landing vma area.
As a bonus, after unmapping sigpage, kernel won't try to land on its previous position (due to UNMAPPED_VDSO_BASE check instead of context.vdso ?= 0 check).

---
 arch/mips/Kconfig         |  1 +
 arch/mips/kernel/signal.c | 11 +++++++----
 arch/mips/kernel/vdso.c   |  2 +-
 arch/mips/vdso/genvdso.c  |  8 --------
 4 files changed, 9 insertions(+), 13 deletions(-)
arch/mips/Kconfig +1 -0
index 818a9b2c78f1..70424605710f 100644
@@ -10,6 +10,7 @@ config MIPS
 	select ARCH_HAS_SETUP_ADDITIONAL_PAGES
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HAS_UBSAN_SANITIZE_ALL
+	select ARCH_HAS_VDSO_BASE
 	select ARCH_SUPPORTS_UPROBES
 	select ARCH_USE_BUILTIN_BSWAP
 	select ARCH_USE_CMPXCHG_LOCKREF if 64BIT
arch/mips/kernel/signal.c +7 -4
index f1e985109da0..e0beaf2cdc0f 100644
@@ -806,11 +806,13 @@ struct mips_abi mips_abi = {
 
 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 {
+	void *land = (void *)current->mm->vdso_base;
 	sigset_t *oldset = sigmask_to_save();
-	int ret;
+	int ret = 1;
 	struct mips_abi *abi = current->thread.abi;
-	void *vdso = current->mm->context.vdso;
 
+	if (land == (void *)UNMAPPED_VDSO_BASE)
+		goto err;
 	/*
 	 * If we were emulating a delay slot instruction, exit that frame such
 	 * that addresses in the sigframe are as expected for userland and we
@@ -843,12 +845,13 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 	rseq_signal_deliver(ksig, regs);
 
 	if (sig_uses_siginfo(&ksig->ka, abi))
-		ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
+		ret = abi->setup_rt_frame(land + abi->vdso->off_rt_sigreturn,
 					  ksig, regs, oldset);
 	else
-		ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
+		ret = abi->setup_frame(land + abi->vdso->off_sigreturn,
 				       ksig, regs, oldset);
 
+err:
 	signal_setup_done(ret, ksig, 0);
 }
 
arch/mips/kernel/vdso.c +1 -1
index e124c68322bb..b2b2e596f03b 100644
@@ -183,7 +183,7 @@ int arch_setup_additional_pages(unsigned long *sysinfo_ehdr)
 		goto out;
 	}
 
-	mm->context.vdso = (void *)vdso_addr;
+	mm->vdso_base = (void __user *)vdso_addr;
 	*sysinfo_ehdr = vdso_addr;
 	ret = 0;
 
arch/mips/vdso/genvdso.c +0 -8
index 0303d30cde03..8f581a2c8578 100644
@@ -259,13 +259,6 @@ int main(int argc, char **argv)
 	fprintf(out_file, "#include <linux/linkage.h>\n");
 	fprintf(out_file, "#include <linux/mm.h>\n");
 	fprintf(out_file, "#include <asm/vdso.h>\n");
-	fprintf(out_file, "static void vdso_mremap(\n");
-	fprintf(out_file, "	const struct vm_special_mapping *sm,\n");
-	fprintf(out_file, "	struct vm_area_struct *new_vma)\n");
-	fprintf(out_file, "{\n");
-	fprintf(out_file, "	current->mm->context.vdso =\n");
-	fprintf(out_file, "	(void *)(new_vma->vm_start);\n");
-	fprintf(out_file, "}\n");
 
 	/* Write out the stripped VDSO data. */
 	fprintf(out_file,
@@ -290,7 +283,6 @@ int main(int argc, char **argv)
 	fprintf(out_file, "\t.mapping = {\n");
 	fprintf(out_file, "\t\t.name = \"[vdso]\",\n");
 	fprintf(out_file, "\t\t.pages = vdso_pages,\n");
-	fprintf(out_file, "\t\t.mremap = vdso_mremap,\n");
 	fprintf(out_file, "\t},\n");
 
 	/* Calculate and write symbol offsets to <output file> */
Signature · 1 line
2.29.2
#35 lore raw
In reply to Dmitry Safonov

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 6 lines

v2 Changes:
- Rename user_landing to vdso_base as it tracks vDSO VMA start address, rather than the explicit address to land (Andy)
- Reword and don't use "new-execed" and "new-born" task (Andy)
- Fix failures reported by build robot

Quoted 8 lines

Started from discussion [1], where was noted that currently a couple of architectures support mremap() for vdso/sigpage, but not munmap(). If an application maps something on the ex-place of vdso/sigpage, later after processing signal it will land there (good luck!)

Patches set is based on linux-next (next-20201123) and it depends on changes in x86/cleanups (those reclaim TIF_IA32/TIF_X32) and also on my changes in akpm (fixing several mremap() issues).

I have a series that cleans up VDSO init on powerpc and migrates powerpc to _install_special_mapping() (patch 10 of the series).

https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=204396&state=%2A&archive=both

I'm wondering how we should coordinate with your series for merging.

I guess your series will also imply removal of arch_unmap() ? see https://elixir.bootlin.com/linux/v5.10-rc4/source/arch/powerpc/include/asm/mmu_context.h#L262

Quoted 16 lines

Logically, the patches set divides on:
- patch 1: a cleanup for patches in x86/cleanups
- patches 2-11: cleanups for arch_setup_additional_pages()
- patches 12-13: x86 signal changes for unmapped vdso
- patches 14-19: provide generic vdso_base in mm_struct

In the end, besides cleanups, it's now more predictable what happens for applications with unmapped vdso on architectures those support .mremap() for vdso/sigpage.

I'm aware of only one user that unmaps vdso - Valgrind [2]. (there possibly are more, but this one is "special", it unmaps vdso, but
not vvar, which confuses CRIU [Checkpoint Restore In Userspace], that's why I'm aware of it)

Quoted 9 lines

Patches as a .git branch:
https://github.com/0x7f454c46/linux/tree/setup_additional_pages-v2

v1 Link:
https://lore.kernel.org/lkml/[email protected]/

[1]: https://lore.kernel.org/linux-arch/CAJwJo6ZANqYkSHbQ+3b+Fi_VT80MtrzEV5yreQAWx-L8j8x2zA@mail.gmail.com/
[2]: https://github.com/checkpoint-restore/criu/issues/488

Christophe

#36 lore raw

Hi Christophe,

Quoted 21 lines · Christophe Leroy

Le 24/11/2020 à 01:29, Dmitry Safonov a écrit :

Quoted 6 lines

v2 Changes:
- Rename user_landing to vdso_base as it tracks vDSO VMA start address, rather than the explicit address to land (Andy)
- Reword and don't use "new-execed" and "new-born" task (Andy)
- Fix failures reported by build robot

Quoted 8 lines

Started from discussion [1], where was noted that currently a couple of architectures support mremap() for vdso/sigpage, but not munmap(). If an application maps something on the ex-place of vdso/sigpage, later after processing signal it will land there (good luck!)

Patches set is based on linux-next (next-20201123) and it depends on changes in x86/cleanups (those reclaim TIF_IA32/TIF_X32) and also on my changes in akpm (fixing several mremap() issues).

I have a series that cleans up VDSO init on powerpc and migrates powerpc to _install_special_mapping() (patch 10 of the series).

Quoted 7 lines

https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=204396&state=%2A&archive=both

I'm wondering how we should coordinate with your series for merging.

I guess your series will also imply removal of arch_unmap() ? see
https://elixir.bootlin.com/linux/v5.10-rc4/source/arch/powerpc/include/asm/mmu_context.h#L262

I think our series intersect only in that moment where I re-introduce arch_setup_additional_pages() parameters. So, in theory we could minimize the conflicts by merging both series in parallel and cleanup the result by moving to generic vdso_base on the top, what do you think?

Thanks,
Dmitry