All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32
@ 2020-10-04  3:25 Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
                   ` (9 more replies)
  0 siblings, 10 replies; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

This is the third version of the patch to reclaim those TI flags.  The
main difference from v2 is that it exports the elf32 headers in the
macros in a proper way, instead of doing some magic to use them.

Andy, I didn't follow (my understanding of) your suggestion to expose
the elf32 headers because by doing that in compat_start_thread would
mean also doing it in start_thread, but the later is not ELF specific.
The mechanism I used, which solves the issue and I hope is not
over-complex is the same that SET_PERSONALITY does, so there is
precedent.  It also has the benefit that we don't need to touch other
architecture functions.  Do you think in this patch series is fine?

This also drops the vmx patch, since that is being reworked by Sean and
Andy, and my patch doesn't change its behavior.

* original cover letter:

We are running out of TI flags for x86.  This patchset removes several
usages of TIF_IA32 and TIF_x32 in preparation to reclaim these flags.
After these cleanups, there is still one more user for both of them,
which I need to take a better look before removing.

Many of the ideas for this patchset came from Andy Lutomirski (Thank
you!)

These were tested by exercising these paths with x32 and ia32 binaries.

Gabriel Krisman Bertazi (10):
  x86: events: Avoid TIF_IA32 when checking 64bit mode
  x86: Simplify compat syscall userspace allocation
  x86: oprofile: Avoid TIF_IA32 when checking 64bit mode
  x86: elf: Use e_machine to choose DLINFO in compat
  elf: Expose ELF header in compat_start_thread
  x86: elf: Use e_machine to select start_thread for x32
  elf: Expose ELF header on arch_setup_additional_pages
  x86: elf: Use e_machine to select additional_pages between x32
  x86: Convert mmu context ia32_compat into a proper flags field
  x86: Reclaim TIF_IA32 and TIF_X32

 arch/x86/entry/vdso/vma.c             |  4 ++--
 arch/x86/entry/vsyscall/vsyscall_64.c |  2 +-
 arch/x86/events/core.c                |  2 +-
 arch/x86/events/intel/ds.c            |  2 +-
 arch/x86/events/intel/lbr.c           |  2 +-
 arch/x86/include/asm/compat.h         | 15 +++++++-------
 arch/x86/include/asm/elf.h            | 13 ++++++++-----
 arch/x86/include/asm/mmu.h            |  6 ++++--
 arch/x86/include/asm/mmu_context.h    |  2 +-
 arch/x86/include/asm/thread_info.h    |  4 ----
 arch/x86/kernel/perf_regs.c           |  2 +-
 arch/x86/kernel/process_64.c          | 28 +++++++++++++--------------
 arch/x86/oprofile/backtrace.c         |  2 +-
 fs/binfmt_elf.c                       |  4 ++--
 fs/compat_binfmt_elf.c                | 20 ++++++++++++++-----
 include/linux/elf.h                   | 10 ++++++++++
 16 files changed, 68 insertions(+), 50 deletions(-)

-- 
2.28.0


^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] perf/x86: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 02/10] x86: Simplify compat syscall userspace allocation Gabriel Krisman Bertazi
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

In preparation to remove TIF_IA32, stop using it in perf events code.

Tested by running perf on 32-bit, 64-bit and x32 applications.

Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 arch/x86/events/core.c      | 2 +-
 arch/x86/events/intel/ds.c  | 2 +-
 arch/x86/events/intel/lbr.c | 2 +-
 arch/x86/kernel/perf_regs.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 1cbf57dc2ac8..4fe82d9d959b 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2499,7 +2499,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent
 	struct stack_frame_ia32 frame;
 	const struct stack_frame_ia32 __user *fp;
 
-	if (!test_thread_flag(TIF_IA32))
+	if (user_64bit_mode(regs))
 		return 0;
 
 	cs_base = get_segment_base(regs->cs);
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 86848c57b55e..94bd0d3acd15 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -1261,7 +1261,7 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
 		old_to = to;
 
 #ifdef CONFIG_X86_64
-		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
+		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
 #endif
 		insn_init(&insn, kaddr, size, is_64bit);
 		insn_get_length(&insn);
diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index 8961653c5dd2..1aadb253d296 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -1221,7 +1221,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 	 * on 64-bit systems running 32-bit apps
 	 */
 #ifdef CONFIG_X86_64
-	is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
+	is64 = kernel_ip((unsigned long)addr) || any_64bit_mode(current_pt_regs());
 #endif
 	insn_init(&insn, addr, bytes_read, is64);
 	insn_get_opcode(&insn);
diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c
index bb7e1132290b..9332c49a64a8 100644
--- a/arch/x86/kernel/perf_regs.c
+++ b/arch/x86/kernel/perf_regs.c
@@ -123,7 +123,7 @@ int perf_reg_validate(u64 mask)
 
 u64 perf_reg_abi(struct task_struct *task)
 {
-	if (test_tsk_thread_flag(task, TIF_IA32))
+	if (!user_64bit_mode(task_pt_regs(task)))
 		return PERF_SAMPLE_REGS_ABI_32;
 	else
 		return PERF_SAMPLE_REGS_ABI_64;
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 02/10] x86: Simplify compat syscall userspace allocation
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/compat: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 03/10] x86: oprofile: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

When allocating user memory space for a compat system call, don't
consider whether the originating code is IA32 or X32, just allocate from
a safe region for both, beyond the redzone.  This should be safe for
IA32, and has the benefit of avoiding TIF_IA32, which we want to drop.

Suggested-by: Andy Lutomirski <luto@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
 arch/x86/include/asm/compat.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/compat.h b/arch/x86/include/asm/compat.h
index d4edf281fff4..a4b5126dff4e 100644
--- a/arch/x86/include/asm/compat.h
+++ b/arch/x86/include/asm/compat.h
@@ -179,14 +179,13 @@ typedef struct user_regs_struct compat_elf_gregset_t;
 
 static inline void __user *arch_compat_alloc_user_space(long len)
 {
-	compat_uptr_t sp;
-
-	if (test_thread_flag(TIF_IA32)) {
-		sp = task_pt_regs(current)->sp;
-	} else {
-		/* -128 for the x32 ABI redzone */
-		sp = task_pt_regs(current)->sp - 128;
-	}
+	compat_uptr_t sp = task_pt_regs(current)->sp;
+
+	/*
+	 * -128 for the x32 ABI redzone.  For IA32, it is not strictly
+	 * necessary, but not harmful.
+	 */
+	sp -= 128;
 
 	return (void __user *)round_down(sp - len, 16);
 }
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 03/10] x86: oprofile: Avoid TIF_IA32 when checking 64bit mode
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 02/10] x86: Simplify compat syscall userspace allocation Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/oprofile: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 04/10] x86: elf: Use e_machine to choose DLINFO in compat Gabriel Krisman Bertazi
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

In preparation to remove TIF_IA32, stop using it in oprofile code.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
 arch/x86/oprofile/backtrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/oprofile/backtrace.c b/arch/x86/oprofile/backtrace.c
index a2488b6e27d6..1d8391fcca68 100644
--- a/arch/x86/oprofile/backtrace.c
+++ b/arch/x86/oprofile/backtrace.c
@@ -49,7 +49,7 @@ x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
 	struct stack_frame_ia32 *head;
 
 	/* User process is IA32 */
-	if (!current || !test_thread_flag(TIF_IA32))
+	if (!current || user_64bit_mode(regs))
 		return 0;
 
 	head = (struct stack_frame_ia32 *) regs->bp;
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 04/10] x86: elf: Use e_machine to choose DLINFO in compat
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (2 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 03/10] x86: oprofile: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 05/10] elf: Expose ELF header in compat_start_thread Gabriel Krisman Bertazi
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Since TIF_X32 is going away, avoid using it to find the ELF type on
ARCH_DLINFO.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when loading ARCH_DLINFO
in compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Reviewed-by: Andy Lutomirski <luto@kernel.org>
---
 arch/x86/include/asm/elf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index b9a5d488f1a5..9220efc65d78 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -361,7 +361,7 @@ do {									\
 #define AT_SYSINFO		32
 
 #define COMPAT_ARCH_DLINFO						\
-if (test_thread_flag(TIF_X32))						\
+if (exec->e_machine == EM_X86_64)					\
 	ARCH_DLINFO_X32;						\
 else									\
 	ARCH_DLINFO_IA32
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 05/10] elf: Expose ELF header in compat_start_thread
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (3 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 04/10] x86: elf: Use e_machine to choose DLINFO in compat Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] elf: Expose ELF header in compat_start_thread() tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32 Gabriel Krisman Bertazi
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Like it is done for SET_PERSONALITY with x86, which requires the ELF
header to select correct personality parameters, x86 requires the
headers on compat_start_thread to choose starting CS for ELF32 binaries,
instead of relying on the going-away TIF_IA32/X32 flags.

This patch adds an indirection macro to ELF invocations of START_THREAD,
that x86 can reimplement to receive the extra parameter just for ELF
files.  This requires no changes to other architectures who don't need
the header information, they can continue to use the original
start_thread for ELF and non-ELF binaries, and it prevents affecting
non-ELF code paths for x86.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
 fs/binfmt_elf.c        | 2 +-
 fs/compat_binfmt_elf.c | 9 +++++++--
 include/linux/elf.h    | 5 +++++
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 13d053982dd7..7fec77a38b8d 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1279,7 +1279,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 #endif
 
 	finalize_exec(bprm);
-	start_thread(regs, elf_entry, bprm->p);
+	START_THREAD(elf_ex, regs, elf_entry, bprm->p);
 	retval = 0;
 out:
 	return retval;
diff --git a/fs/compat_binfmt_elf.c b/fs/compat_binfmt_elf.c
index 2d24c765cbd7..12b991368f0a 100644
--- a/fs/compat_binfmt_elf.c
+++ b/fs/compat_binfmt_elf.c
@@ -106,8 +106,13 @@
 #endif
 
 #ifdef	compat_start_thread
-#undef	start_thread
-#define	start_thread		compat_start_thread
+#define COMPAT_START_THREAD(ex, regs, new_ip, new_sp)	\
+	compat_start_thread(regs, new_ip, new_sp)
+#endif
+
+#ifdef	COMPAT_START_THREAD
+#undef	START_THREAD
+#define START_THREAD		COMPAT_START_THREAD
 #endif
 
 #ifdef	compat_arch_setup_additional_pages
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 5d5b0321da0b..6dbcfe7a3fd7 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -22,6 +22,11 @@
 	SET_PERSONALITY(ex)
 #endif
 
+#ifndef START_THREAD
+#define START_THREAD(elf_ex, regs, elf_entry, start_stack)	\
+	start_thread(regs, elf_entry, start_stack)
+#endif
+
 #define ELF32_GNU_PROPERTY_ALIGN	4
 #define ELF64_GNU_PROPERTY_ALIGN	8
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (4 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 05/10] elf: Expose ELF header in compat_start_thread Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-08  0:05   ` Andy Lutomirski
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 07/10] elf: Expose ELF header on arch_setup_additional_pages Gabriel Krisman Bertazi
                   ` (3 subsequent siblings)
  9 siblings, 2 replies; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Since TIF_X32 is going away, avoid using it to find the ELF type in
compat_start_thread.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when executing
start_thread in compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>

---
Changes since v2:
  - Avoid a function-like macro in compat_start_thread (Andy)
---
 arch/x86/include/asm/elf.h   | 5 +++--
 arch/x86/kernel/process_64.c | 5 ++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 9220efc65d78..109697a19eb1 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -186,8 +186,9 @@ static inline void elf_common_init(struct thread_struct *t,
 #define	COMPAT_ELF_PLAT_INIT(regs, load_addr)		\
 	elf_common_init(&current->thread, regs, __USER_DS)
 
-void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp);
-#define compat_start_thread compat_start_thread
+void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32);
+#define COMPAT_START_THREAD(ex, regs, new_ip, new_sp)	\
+	compat_start_thread(regs, new_ip, new_sp, ex->e_machine == EM_X86_64)
 
 void set_personality_ia32(bool);
 #define COMPAT_SET_PERSONALITY(ex)			\
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 9afefe325acb..a4935d134e9d 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -511,11 +511,10 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
 EXPORT_SYMBOL_GPL(start_thread);
 
 #ifdef CONFIG_COMPAT
-void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp)
+void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32)
 {
 	start_thread_common(regs, new_ip, new_sp,
-			    test_thread_flag(TIF_X32)
-			    ? __USER_CS : __USER32_CS,
+			    x32 ? __USER_CS : __USER32_CS,
 			    __USER_DS, __USER_DS);
 }
 #endif
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 07/10] elf: Expose ELF header on arch_setup_additional_pages
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (5 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32 Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] elf: Expose ELF header on arch_setup_additional_pages() tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 08/10] x86: elf: Use e_machine to select additional_pages between x32 Gabriel Krisman Bertazi
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Like it is done for SET_PERSONALITY with ARM, which requires the ELF
header to select correct personality parameters, x86 requires the
headers when selecting which vdso to load, instead of relying on the
going-away TIF_IA32/X32 flags.  This patch adds an indirection macro to
arch_setup_additional_pages, that x86 can reimplement to receive the
extra parameter just for ELF files.  This requires no changes to other
architectures, who can continue to use the original
arch_setup_additional_pages for ELF and non-ELF binaries.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
 fs/binfmt_elf.c        |  2 +-
 fs/compat_binfmt_elf.c | 11 ++++++++---
 include/linux/elf.h    |  5 +++++
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 7fec77a38b8d..b41ed57271da 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1218,7 +1218,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, !!interpreter);
+	retval = ARCH_SETUP_ADDITIONAL_PAGES(bprm, elf_ex, !!interpreter);
 	if (retval < 0)
 		goto out;
 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
diff --git a/fs/compat_binfmt_elf.c b/fs/compat_binfmt_elf.c
index 12b991368f0a..2c557229696a 100644
--- a/fs/compat_binfmt_elf.c
+++ b/fs/compat_binfmt_elf.c
@@ -115,11 +115,16 @@
 #define START_THREAD		COMPAT_START_THREAD
 #endif
 
-#ifdef	compat_arch_setup_additional_pages
+#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
 #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
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 6dbcfe7a3fd7..c9a46c4e183b 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -27,6 +27,11 @@
 	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
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 08/10] x86: elf: Use e_machine to select additional_pages between x32
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (6 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 07/10] elf: Expose ELF header on arch_setup_additional_pages Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: Use e_machine to check for x32/ia32 in setup_additional_pages() tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
  9 siblings, 1 reply; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Since TIF_X32 is going away, avoid using it to find the ELF type when
choosing which additional pages to set up.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when executing
setup_additional_pages in compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>

---
Changes since v2:
  - Avoid a function-like macro in compat_setup_additional_pages (Andy)
---
 arch/x86/entry/vdso/vma.c  | 4 ++--
 arch/x86/include/asm/elf.h | 6 ++++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c
index 9185cb1d13b9..50e5d3a2e70a 100644
--- a/arch/x86/entry/vdso/vma.c
+++ b/arch/x86/entry/vdso/vma.c
@@ -413,10 +413,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)
+				       int uses_interp, bool x32)
 {
 #ifdef CONFIG_X86_X32_ABI
-	if (test_thread_flag(TIF_X32)) {
+	if (x32) {
 		if (!vdso64_enabled)
 			return 0;
 		return map_vdso_randomized(&vdso_image_x32);
diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 109697a19eb1..44a9b9940535 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -383,8 +383,10 @@ 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);
-#define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
+					      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))
 
 /* Do not change the values. See get_align_mask() */
 enum align_flags {
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (7 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 08/10] x86: elf: Use e_machine to select additional_pages between x32 Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-08  0:07   ` Andy Lutomirski
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/mm: " tip-bot2 for Gabriel Krisman Bertazi
  2020-10-04  3:25 ` [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
  9 siblings, 2 replies; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

The ia32_compat attribute is a weird thing.  It mirrors TIF_IA32 and
TIF_X32 and is used only in two very unrelated places: (1) to decide if
the vsyscall page is accessible (2) for uprobes to find whether the
patched instruction is 32 or 64 bit.  In preparation to remove the TI
flags, we want new values for ia32_compat, but given its odd semantics,
I'd rather make it a real flags field that configures these specific
behaviours.  So, set_personality_x64 can ask for the vsyscall page,
which is not available in x32/ia32 and set_personality_ia32 can
configure the uprobe code as needed.

uprobe cannot rely on other methods like user_64bit_mode() to decide how
to patch, so it needs some specific flag like this.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>

---
Changes since v2:
  - Rename MM_CONTEXT_GATE_PAGE -> MM_CONTEXT_HAS_VSYSCALL (andy)
---
 arch/x86/entry/vsyscall/vsyscall_64.c |  2 +-
 arch/x86/include/asm/mmu.h            |  6 ++++--
 arch/x86/include/asm/mmu_context.h    |  2 +-
 arch/x86/kernel/process_64.c          | 17 +++++++++++------
 4 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
index 44c33103a955..1b40b9297083 100644
--- a/arch/x86/entry/vsyscall/vsyscall_64.c
+++ b/arch/x86/entry/vsyscall/vsyscall_64.c
@@ -316,7 +316,7 @@ static struct vm_area_struct gate_vma __ro_after_init = {
 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
 {
 #ifdef CONFIG_COMPAT
-	if (!mm || mm->context.ia32_compat)
+	if (!mm || !(mm->context.flags & MM_CONTEXT_HAS_VSYSCALL))
 		return NULL;
 #endif
 	if (vsyscall_mode == NONE)
diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h
index 9257667d13c5..6a00665574ea 100644
--- a/arch/x86/include/asm/mmu.h
+++ b/arch/x86/include/asm/mmu.h
@@ -7,6 +7,9 @@
 #include <linux/mutex.h>
 #include <linux/atomic.h>
 
+#define MM_CONTEXT_UPROBE_IA32	1 /* Uprobes on this MM assume 32-bit code */
+#define MM_CONTEXT_HAS_VSYSCALL	2 /* Whether vsyscall page is accessible on this MM */
+
 /*
  * x86 has arch-specific MMU state beyond what lives in mm_struct.
  */
@@ -33,8 +36,7 @@ typedef struct {
 #endif
 
 #ifdef CONFIG_X86_64
-	/* True if mm supports a task running in 32 bit compatibility mode. */
-	unsigned short ia32_compat;
+	unsigned short flags;
 #endif
 
 	struct mutex lock;
diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
index d98016b83755..054a79157323 100644
--- a/arch/x86/include/asm/mmu_context.h
+++ b/arch/x86/include/asm/mmu_context.h
@@ -177,7 +177,7 @@ static inline void arch_exit_mmap(struct mm_struct *mm)
 static inline bool is_64bit_mm(struct mm_struct *mm)
 {
 	return	!IS_ENABLED(CONFIG_IA32_EMULATION) ||
-		!(mm->context.ia32_compat == TIF_IA32);
+		!(mm->context.flags & MM_CONTEXT_UPROBE_IA32);
 }
 #else
 static inline bool is_64bit_mm(struct mm_struct *mm)
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index a4935d134e9d..40fa7973e4f0 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -646,10 +646,8 @@ void set_personality_64bit(void)
 	/* Pretend that this comes from a 64bit execve */
 	task_pt_regs(current)->orig_ax = __NR_execve;
 	current_thread_info()->status &= ~TS_COMPAT;
-
-	/* Ensure the corresponding mm is not marked. */
 	if (current->mm)
-		current->mm->context.ia32_compat = 0;
+		current->mm->context.flags = MM_CONTEXT_HAS_VSYSCALL;
 
 	/* TBD: overwrites user setup. Should have two bits.
 	   But 64bit processes have always behaved this way,
@@ -664,7 +662,8 @@ static void __set_personality_x32(void)
 	clear_thread_flag(TIF_IA32);
 	set_thread_flag(TIF_X32);
 	if (current->mm)
-		current->mm->context.ia32_compat = TIF_X32;
+		current->mm->context.flags = 0;
+
 	current->personality &= ~READ_IMPLIES_EXEC;
 	/*
 	 * in_32bit_syscall() uses the presence of the x32 syscall bit
@@ -684,8 +683,14 @@ static void __set_personality_ia32(void)
 #ifdef CONFIG_IA32_EMULATION
 	set_thread_flag(TIF_IA32);
 	clear_thread_flag(TIF_X32);
-	if (current->mm)
-		current->mm->context.ia32_compat = TIF_IA32;
+	if (current->mm) {
+		/*
+		 * uprobes applied to this MM need to know this and
+		 * cannot use user_64bit_mode() at that time.
+		 */
+		current->mm->context.flags = MM_CONTEXT_UPROBE_IA32;
+	}
+
 	current->personality |= force_personality32;
 	/* Prepare the first "return" to user space */
 	task_pt_regs(current)->orig_ax = __NR_ia32_execve;
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32
  2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
                   ` (8 preceding siblings ...)
  2020-10-04  3:25 ` [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field Gabriel Krisman Bertazi
@ 2020-10-04  3:25 ` Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] " tip-bot2 for Gabriel Krisman Bertazi
  2020-11-05 20:01   ` [PATCH v3 10/10] " Dmitry Safonov
  9 siblings, 2 replies; 24+ messages in thread
From: Gabriel Krisman Bertazi @ 2020-10-04  3:25 UTC (permalink / raw)
  To: luto, tglx
  Cc: hch, hpa, bp, rric, peterz, mingo, x86, linux-kernel,
	dave.hansen, sean.j.christopherson, Gabriel Krisman Bertazi,
	kernel

Now that these flags are no longer used, reclaim those TI bits.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
 arch/x86/include/asm/thread_info.h | 4 ----
 arch/x86/kernel/process_64.c       | 6 ------
 2 files changed, 10 deletions(-)

diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
index 267701ae3d86..6888aa39c4d6 100644
--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -91,7 +91,6 @@ struct thread_info {
 #define TIF_NEED_FPU_LOAD	14	/* load FPU on return to userspace */
 #define TIF_NOCPUID		15	/* CPUID is not accessible in userland */
 #define TIF_NOTSC		16	/* TSC is not accessible in userland */
-#define TIF_IA32		17	/* IA32 compatibility process */
 #define TIF_SLD			18	/* Restore split lock detection on context switch */
 #define TIF_MEMDIE		20	/* is terminating due to OOM killer */
 #define TIF_POLLING_NRFLAG	21	/* idle is polling for TIF_NEED_RESCHED */
@@ -101,7 +100,6 @@ struct thread_info {
 #define TIF_LAZY_MMU_UPDATES	27	/* task is updating the mmu lazily */
 #define TIF_SYSCALL_TRACEPOINT	28	/* syscall tracepoint instrumentation */
 #define TIF_ADDR32		29	/* 32-bit address space on 64 bits */
-#define TIF_X32			30	/* 32-bit native x86-64 binary */
 #define TIF_FSCHECK		31	/* Check FS is USER_DS on return */
 
 #define _TIF_SYSCALL_TRACE	(1 << TIF_SYSCALL_TRACE)
@@ -121,7 +119,6 @@ struct thread_info {
 #define _TIF_NEED_FPU_LOAD	(1 << TIF_NEED_FPU_LOAD)
 #define _TIF_NOCPUID		(1 << TIF_NOCPUID)
 #define _TIF_NOTSC		(1 << TIF_NOTSC)
-#define _TIF_IA32		(1 << TIF_IA32)
 #define _TIF_SLD		(1 << TIF_SLD)
 #define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
 #define _TIF_IO_BITMAP		(1 << TIF_IO_BITMAP)
@@ -130,7 +127,6 @@ struct thread_info {
 #define _TIF_LAZY_MMU_UPDATES	(1 << TIF_LAZY_MMU_UPDATES)
 #define _TIF_SYSCALL_TRACEPOINT	(1 << TIF_SYSCALL_TRACEPOINT)
 #define _TIF_ADDR32		(1 << TIF_ADDR32)
-#define _TIF_X32		(1 << TIF_X32)
 #define _TIF_FSCHECK		(1 << TIF_FSCHECK)
 
 /* flags to check in __switch_to() */
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 40fa7973e4f0..9e71101e9d61 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -640,9 +640,7 @@ void set_personality_64bit(void)
 	/* inherit personality from parent */
 
 	/* Make sure to be in 64bit mode */
-	clear_thread_flag(TIF_IA32);
 	clear_thread_flag(TIF_ADDR32);
-	clear_thread_flag(TIF_X32);
 	/* Pretend that this comes from a 64bit execve */
 	task_pt_regs(current)->orig_ax = __NR_execve;
 	current_thread_info()->status &= ~TS_COMPAT;
@@ -659,8 +657,6 @@ void set_personality_64bit(void)
 static void __set_personality_x32(void)
 {
 #ifdef CONFIG_X86_X32
-	clear_thread_flag(TIF_IA32);
-	set_thread_flag(TIF_X32);
 	if (current->mm)
 		current->mm->context.flags = 0;
 
@@ -681,8 +677,6 @@ static void __set_personality_x32(void)
 static void __set_personality_ia32(void)
 {
 #ifdef CONFIG_IA32_EMULATION
-	set_thread_flag(TIF_IA32);
-	clear_thread_flag(TIF_X32);
 	if (current->mm) {
 		/*
 		 * uprobes applied to this MM need to know this and
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32
  2020-10-04  3:25 ` [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32 Gabriel Krisman Bertazi
@ 2020-10-08  0:05   ` Andy Lutomirski
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: " tip-bot2 for Gabriel Krisman Bertazi
  1 sibling, 0 replies; 24+ messages in thread
From: Andy Lutomirski @ 2020-10-08  0:05 UTC (permalink / raw)
  To: Gabriel Krisman Bertazi
  Cc: Andrew Lutomirski, Thomas Gleixner, Christoph Hellwig,
	H. Peter Anvin, Borislav Petkov, Robert Richter, Peter Zijlstra,
	Ingo Molnar, X86 ML, LKML, Dave Hansen, Christopherson, Sean J,
	kernel

On Sat, Oct 3, 2020 at 8:26 PM Gabriel Krisman Bertazi
<krisman@collabora.com> wrote:
>
> Since TIF_X32 is going away, avoid using it to find the ELF type in
> compat_start_thread.
>
> According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
> have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
> differentiate a x32 object from a IA32 object when executing
> start_thread in compat mode.

Hmm, I suppose I can live with this.  It's not fundamentally worse
than any of the other ELF compat hacks.

Reviewed-by: Andy Lutomirski <luto@kernel.org>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field
  2020-10-04  3:25 ` [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field Gabriel Krisman Bertazi
@ 2020-10-08  0:07   ` Andy Lutomirski
  2020-10-26 12:52   ` [tip: x86/cleanups] x86/mm: " tip-bot2 for Gabriel Krisman Bertazi
  1 sibling, 0 replies; 24+ messages in thread
From: Andy Lutomirski @ 2020-10-08  0:07 UTC (permalink / raw)
  To: Gabriel Krisman Bertazi
  Cc: Andrew Lutomirski, Thomas Gleixner, Christoph Hellwig,
	H. Peter Anvin, Borislav Petkov, Robert Richter, Peter Zijlstra,
	Ingo Molnar, X86 ML, LKML, Dave Hansen, Christopherson, Sean J,
	kernel

On Sat, Oct 3, 2020 at 8:26 PM Gabriel Krisman Bertazi
<krisman@collabora.com> wrote:
>
> The ia32_compat attribute is a weird thing.  It mirrors TIF_IA32 and
> TIF_X32 and is used only in two very unrelated places: (1) to decide if
> the vsyscall page is accessible (2) for uprobes to find whether the
> patched instruction is 32 or 64 bit.  In preparation to remove the TI
> flags, we want new values for ia32_compat, but given its odd semantics,
> I'd rather make it a real flags field that configures these specific
> behaviours.  So, set_personality_x64 can ask for the vsyscall page,
> which is not available in x32/ia32 and set_personality_ia32 can
> configure the uprobe code as needed.
>
> uprobe cannot rely on other methods like user_64bit_mode() to decide how
> to patch, so it needs some specific flag like this.

Acked-by: Andy Lutomirski<luto@kernel.org>

There are still issues with vsyscall control, but they were
pre-existing and I'll try to get them fixed up.

--Andy

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/mm: Convert mmu context ia32_compat into a proper flags field
  2020-10-04  3:25 ` [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field Gabriel Krisman Bertazi
  2020-10-08  0:07   ` Andy Lutomirski
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  1 sibling, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Gabriel Krisman Bertazi, Thomas Gleixner, Andy Lutomirski, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     ff170cd0595398a7b66cb40f249eb2f10c29b66d
Gitweb:        https://git.kernel.org/tip/ff170cd0595398a7b66cb40f249eb2f10c29b66d
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:35 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:47 +01:00

x86/mm: Convert mmu context ia32_compat into a proper flags field

The ia32_compat attribute is a weird thing.  It mirrors TIF_IA32 and
TIF_X32 and is used only in two very unrelated places: (1) to decide if
the vsyscall page is accessible (2) for uprobes to find whether the
patched instruction is 32 or 64 bit.

In preparation to remove the TIF flags, a new mechanism is required for
ia32_compat, but given its odd semantics, adding a real flags field which
configures these specific behaviours is the best option.

So, set_personality_x64() can ask for the vsyscall page, which is not
available in x32/ia32 and set_personality_ia32() can configure the uprobe
code as needed.

uprobe cannot rely on other methods like user_64bit_mode() to decide how
to patch, so it needs some specific flag like this.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Andy Lutomirski<luto@kernel.org>
Link: https://lore.kernel.org/r/20201004032536.1229030-10-krisman@collabora.com
---
 arch/x86/entry/vsyscall/vsyscall_64.c |  2 +-
 arch/x86/include/asm/mmu.h            |  9 +++++++--
 arch/x86/include/asm/mmu_context.h    |  2 +-
 arch/x86/kernel/process_64.c          | 17 +++++++++++------
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
index 44c3310..1b40b92 100644
--- a/arch/x86/entry/vsyscall/vsyscall_64.c
+++ b/arch/x86/entry/vsyscall/vsyscall_64.c
@@ -316,7 +316,7 @@ static struct vm_area_struct gate_vma __ro_after_init = {
 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
 {
 #ifdef CONFIG_COMPAT
-	if (!mm || mm->context.ia32_compat)
+	if (!mm || !(mm->context.flags & MM_CONTEXT_HAS_VSYSCALL))
 		return NULL;
 #endif
 	if (vsyscall_mode == NONE)
diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h
index 9257667..5d74946 100644
--- a/arch/x86/include/asm/mmu.h
+++ b/arch/x86/include/asm/mmu.h
@@ -6,6 +6,12 @@
 #include <linux/rwsem.h>
 #include <linux/mutex.h>
 #include <linux/atomic.h>
+#include <linux/bits.h>
+
+/* Uprobes on this MM assume 32-bit code */
+#define MM_CONTEXT_UPROBE_IA32	BIT(0)
+/* vsyscall page is accessible on this MM */
+#define MM_CONTEXT_HAS_VSYSCALL	BIT(1)
 
 /*
  * x86 has arch-specific MMU state beyond what lives in mm_struct.
@@ -33,8 +39,7 @@ typedef struct {
 #endif
 
 #ifdef CONFIG_X86_64
-	/* True if mm supports a task running in 32 bit compatibility mode. */
-	unsigned short ia32_compat;
+	unsigned short flags;
 #endif
 
 	struct mutex lock;
diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
index d98016b..054a791 100644
--- a/arch/x86/include/asm/mmu_context.h
+++ b/arch/x86/include/asm/mmu_context.h
@@ -177,7 +177,7 @@ static inline void arch_exit_mmap(struct mm_struct *mm)
 static inline bool is_64bit_mm(struct mm_struct *mm)
 {
 	return	!IS_ENABLED(CONFIG_IA32_EMULATION) ||
-		!(mm->context.ia32_compat == TIF_IA32);
+		!(mm->context.flags & MM_CONTEXT_UPROBE_IA32);
 }
 #else
 static inline bool is_64bit_mm(struct mm_struct *mm)
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 5fb4103..d6efaf6 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -646,10 +646,8 @@ void set_personality_64bit(void)
 	/* Pretend that this comes from a 64bit execve */
 	task_pt_regs(current)->orig_ax = __NR_execve;
 	current_thread_info()->status &= ~TS_COMPAT;
-
-	/* Ensure the corresponding mm is not marked. */
 	if (current->mm)
-		current->mm->context.ia32_compat = 0;
+		current->mm->context.flags = MM_CONTEXT_HAS_VSYSCALL;
 
 	/* TBD: overwrites user setup. Should have two bits.
 	   But 64bit processes have always behaved this way,
@@ -664,7 +662,8 @@ static void __set_personality_x32(void)
 	clear_thread_flag(TIF_IA32);
 	set_thread_flag(TIF_X32);
 	if (current->mm)
-		current->mm->context.ia32_compat = TIF_X32;
+		current->mm->context.flags = 0;
+
 	current->personality &= ~READ_IMPLIES_EXEC;
 	/*
 	 * in_32bit_syscall() uses the presence of the x32 syscall bit
@@ -684,8 +683,14 @@ static void __set_personality_ia32(void)
 #ifdef CONFIG_IA32_EMULATION
 	set_thread_flag(TIF_IA32);
 	clear_thread_flag(TIF_X32);
-	if (current->mm)
-		current->mm->context.ia32_compat = TIF_IA32;
+	if (current->mm) {
+		/*
+		 * uprobes applied to this MM need to know this and
+		 * cannot use user_64bit_mode() at that time.
+		 */
+		current->mm->context.flags = MM_CONTEXT_UPROBE_IA32;
+	}
+
 	current->personality |= force_personality32;
 	/* Prepare the first "return" to user space */
 	task_pt_regs(current)->orig_ax = __NR_ia32_execve;

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86: Reclaim TIF_IA32 and TIF_X32
  2020-10-04  3:25 ` [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  2020-11-05 20:01   ` [PATCH v3 10/10] " Dmitry Safonov
  1 sibling, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     8d71d2bf6efec3032208958c483a247f529ffb16
Gitweb:        https://git.kernel.org/tip/8d71d2bf6efec3032208958c483a247f529ffb16
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:36 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:47 +01:00

x86: Reclaim TIF_IA32 and TIF_X32

Now that these flags are no longer used, reclaim those TIF bits.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-11-krisman@collabora.com

---
 arch/x86/include/asm/thread_info.h | 4 ----
 arch/x86/kernel/process_64.c       | 6 ------
 2 files changed, 10 deletions(-)

diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
index 44733a4..a12b964 100644
--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -91,7 +91,6 @@ struct thread_info {
 #define TIF_NEED_FPU_LOAD	14	/* load FPU on return to userspace */
 #define TIF_NOCPUID		15	/* CPUID is not accessible in userland */
 #define TIF_NOTSC		16	/* TSC is not accessible in userland */
-#define TIF_IA32		17	/* IA32 compatibility process */
 #define TIF_SLD			18	/* Restore split lock detection on context switch */
 #define TIF_MEMDIE		20	/* is terminating due to OOM killer */
 #define TIF_POLLING_NRFLAG	21	/* idle is polling for TIF_NEED_RESCHED */
@@ -101,7 +100,6 @@ struct thread_info {
 #define TIF_LAZY_MMU_UPDATES	27	/* task is updating the mmu lazily */
 #define TIF_SYSCALL_TRACEPOINT	28	/* syscall tracepoint instrumentation */
 #define TIF_ADDR32		29	/* 32-bit address space on 64 bits */
-#define TIF_X32			30	/* 32-bit native x86-64 binary */
 
 #define _TIF_SYSCALL_TRACE	(1 << TIF_SYSCALL_TRACE)
 #define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
@@ -120,7 +118,6 @@ struct thread_info {
 #define _TIF_NEED_FPU_LOAD	(1 << TIF_NEED_FPU_LOAD)
 #define _TIF_NOCPUID		(1 << TIF_NOCPUID)
 #define _TIF_NOTSC		(1 << TIF_NOTSC)
-#define _TIF_IA32		(1 << TIF_IA32)
 #define _TIF_SLD		(1 << TIF_SLD)
 #define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
 #define _TIF_IO_BITMAP		(1 << TIF_IO_BITMAP)
@@ -129,7 +126,6 @@ struct thread_info {
 #define _TIF_LAZY_MMU_UPDATES	(1 << TIF_LAZY_MMU_UPDATES)
 #define _TIF_SYSCALL_TRACEPOINT	(1 << TIF_SYSCALL_TRACEPOINT)
 #define _TIF_ADDR32		(1 << TIF_ADDR32)
-#define _TIF_X32		(1 << TIF_X32)
 
 /* flags to check in __switch_to() */
 #define _TIF_WORK_CTXSW_BASE					\
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index d6efaf6..ad582f9 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -640,9 +640,7 @@ void set_personality_64bit(void)
 	/* inherit personality from parent */
 
 	/* Make sure to be in 64bit mode */
-	clear_thread_flag(TIF_IA32);
 	clear_thread_flag(TIF_ADDR32);
-	clear_thread_flag(TIF_X32);
 	/* Pretend that this comes from a 64bit execve */
 	task_pt_regs(current)->orig_ax = __NR_execve;
 	current_thread_info()->status &= ~TS_COMPAT;
@@ -659,8 +657,6 @@ void set_personality_64bit(void)
 static void __set_personality_x32(void)
 {
 #ifdef CONFIG_X86_X32
-	clear_thread_flag(TIF_IA32);
-	set_thread_flag(TIF_X32);
 	if (current->mm)
 		current->mm->context.flags = 0;
 
@@ -681,8 +677,6 @@ static void __set_personality_x32(void)
 static void __set_personality_ia32(void)
 {
 #ifdef CONFIG_IA32_EMULATION
-	set_thread_flag(TIF_IA32);
-	clear_thread_flag(TIF_X32);
 	if (current->mm) {
 		/*
 		 * uprobes applied to this MM need to know this and

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/elf: Use e_machine to check for x32/ia32 in setup_additional_pages()
  2020-10-04  3:25 ` [PATCH v3 08/10] x86: elf: Use e_machine to select additional_pages between x32 Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     3316ec8ccd34e19690a12e65801d605d25155031
Gitweb:        https://git.kernel.org/tip/3316ec8ccd34e19690a12e65801d605d25155031
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:34 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:47 +01:00

x86/elf: Use e_machine to check for x32/ia32 in setup_additional_pages()

Since TIF_X32 is going away, avoid using it to find the ELF type when
choosing which additional pages to set up.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when executing
setup_additional_pages() in compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-9-krisman@collabora.com


---
 arch/x86/entry/vdso/vma.c  | 4 ++--
 arch/x86/include/asm/elf.h | 6 ++++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c
index 9185cb1..50e5d3a 100644
--- a/arch/x86/entry/vdso/vma.c
+++ b/arch/x86/entry/vdso/vma.c
@@ -413,10 +413,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)
+				       int uses_interp, bool x32)
 {
 #ifdef CONFIG_X86_X32_ABI
-	if (test_thread_flag(TIF_X32)) {
+	if (x32) {
 		if (!vdso64_enabled)
 			return 0;
 		return map_vdso_randomized(&vdso_image_x32);
diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 109697a..44a9b99 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -383,8 +383,10 @@ 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);
-#define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
+					      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))
 
 /* Do not change the values. See get_align_mask() */
 enum align_flags {

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] elf: Expose ELF header on arch_setup_additional_pages()
  2020-10-04  3:25 ` [PATCH v3 07/10] elf: Expose ELF header on arch_setup_additional_pages Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     9a29a671902c2be05d636045a4dd365219ca716c
Gitweb:        https://git.kernel.org/tip/9a29a671902c2be05d636045a4dd365219ca716c
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:33 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:47 +01:00

elf: Expose ELF header on arch_setup_additional_pages()

Like it is done for SET_PERSONALITY with ARM, which requires the ELF
header to select correct personality parameters, x86 requires the
headers when selecting which VDSO to load, instead of relying on the
going-away TIF_IA32/X32 flags.

Add an indirection macro to arch_setup_additional_pages(), that x86 can
reimplement to receive the extra parameter just for ELF files.  This
requires no changes to other architectures, who can continue to use the
original arch_setup_additional_pages for ELF and non-ELF binaries.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-8-krisman@collabora.com

---
 fs/binfmt_elf.c        |  2 +-
 fs/compat_binfmt_elf.c | 11 ++++++++---
 include/linux/elf.h    |  5 +++++
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b23f755..aabc11f 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1246,7 +1246,7 @@ out_free_interp:
 	set_binfmt(&elf_format);
 
 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
-	retval = arch_setup_additional_pages(bprm, !!interpreter);
+	retval = ARCH_SETUP_ADDITIONAL_PAGES(bprm, elf_ex, !!interpreter);
 	if (retval < 0)
 		goto out;
 #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
diff --git a/fs/compat_binfmt_elf.c b/fs/compat_binfmt_elf.c
index 12b9913..2c55722 100644
--- a/fs/compat_binfmt_elf.c
+++ b/fs/compat_binfmt_elf.c
@@ -115,11 +115,16 @@
 #define START_THREAD		COMPAT_START_THREAD
 #endif
 
-#ifdef	compat_arch_setup_additional_pages
+#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
 #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
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 6dbcfe7..c9a46c4 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -27,6 +27,11 @@
 	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
 

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/elf: Use e_machine to select start_thread for x32
  2020-10-04  3:25 ` [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32 Gabriel Krisman Bertazi
  2020-10-08  0:05   ` Andy Lutomirski
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  1 sibling, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Gabriel Krisman Bertazi, Thomas Gleixner, Andy Lutomirski, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     2424b14605c71a7187c14edd525044eb36bdea47
Gitweb:        https://git.kernel.org/tip/2424b14605c71a7187c14edd525044eb36bdea47
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:32 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

x86/elf: Use e_machine to select start_thread for x32

Since TIF_X32 is going away, avoid using it to find the ELF type in
compat_start_thread.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when executing start_thread()
in compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Andy Lutomirski <luto@kernel.org>
Link: https://lore.kernel.org/r/20201004032536.1229030-7-krisman@collabora.com


---
 arch/x86/include/asm/elf.h   | 5 +++--
 arch/x86/kernel/process_64.c | 5 ++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 9220efc..109697a 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -186,8 +186,9 @@ static inline void elf_common_init(struct thread_struct *t,
 #define	COMPAT_ELF_PLAT_INIT(regs, load_addr)		\
 	elf_common_init(&current->thread, regs, __USER_DS)
 
-void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp);
-#define compat_start_thread compat_start_thread
+void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32);
+#define COMPAT_START_THREAD(ex, regs, new_ip, new_sp)	\
+	compat_start_thread(regs, new_ip, new_sp, ex->e_machine == EM_X86_64)
 
 void set_personality_ia32(bool);
 #define COMPAT_SET_PERSONALITY(ex)			\
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index df342be..5fb4103 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -511,11 +511,10 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
 EXPORT_SYMBOL_GPL(start_thread);
 
 #ifdef CONFIG_COMPAT
-void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp)
+void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32)
 {
 	start_thread_common(regs, new_ip, new_sp,
-			    test_thread_flag(TIF_X32)
-			    ? __USER_CS : __USER32_CS,
+			    x32 ? __USER_CS : __USER32_CS,
 			    __USER_DS, __USER_DS);
 }
 #endif

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] elf: Expose ELF header in compat_start_thread()
  2020-10-04  3:25 ` [PATCH v3 05/10] elf: Expose ELF header in compat_start_thread Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     bc3d7bf61a9eaecccc84dc2ecc2a9a3fa4f5ec47
Gitweb:        https://git.kernel.org/tip/bc3d7bf61a9eaecccc84dc2ecc2a9a3fa4f5ec47
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:31 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

elf: Expose ELF header in compat_start_thread()

Like it is done for SET_PERSONALITY with x86, which requires the ELF header
to select correct personality parameters, x86 requires the headers on
compat_start_thread() to choose starting CS for ELF32 binaries, instead of
relying on the going-away TIF_IA32/X32 flags.

Add an indirection macro to ELF invocations of START_THREAD, that x86 can
reimplement to receive the extra parameter just for ELF files.  This
requires no changes to other architectures who don't need the header
information, they can continue to use the original start_thread for ELF and
non-ELF binaries, and it prevents affecting non-ELF code paths for x86.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-6-krisman@collabora.com

---
 fs/binfmt_elf.c        |  2 +-
 fs/compat_binfmt_elf.c |  9 +++++++--
 include/linux/elf.h    |  5 +++++
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b6b3d05..b23f755 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1307,7 +1307,7 @@ out_free_interp:
 #endif
 
 	finalize_exec(bprm);
-	start_thread(regs, elf_entry, bprm->p);
+	START_THREAD(elf_ex, regs, elf_entry, bprm->p);
 	retval = 0;
 out:
 	return retval;
diff --git a/fs/compat_binfmt_elf.c b/fs/compat_binfmt_elf.c
index 2d24c76..12b9913 100644
--- a/fs/compat_binfmt_elf.c
+++ b/fs/compat_binfmt_elf.c
@@ -106,8 +106,13 @@
 #endif
 
 #ifdef	compat_start_thread
-#undef	start_thread
-#define	start_thread		compat_start_thread
+#define COMPAT_START_THREAD(ex, regs, new_ip, new_sp)	\
+	compat_start_thread(regs, new_ip, new_sp)
+#endif
+
+#ifdef	COMPAT_START_THREAD
+#undef	START_THREAD
+#define START_THREAD		COMPAT_START_THREAD
 #endif
 
 #ifdef	compat_arch_setup_additional_pages
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 5d5b032..6dbcfe7 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -22,6 +22,11 @@
 	SET_PERSONALITY(ex)
 #endif
 
+#ifndef START_THREAD
+#define START_THREAD(elf_ex, regs, elf_entry, start_stack)	\
+	start_thread(regs, elf_entry, start_stack)
+#endif
+
 #define ELF32_GNU_PROPERTY_ALIGN	4
 #define ELF64_GNU_PROPERTY_ALIGN	8
 

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/oprofile: Avoid TIF_IA32 when checking 64bit mode
  2020-10-04  3:25 ` [PATCH v3 03/10] x86: oprofile: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     47cd4dac1fb21759ffcfe0600827c87fa6acdfa7
Gitweb:        https://git.kernel.org/tip/47cd4dac1fb21759ffcfe0600827c87fa6acdfa7
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:29 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

x86/oprofile: Avoid TIF_IA32 when checking 64bit mode

In preparation to remove TIF_IA32, stop using it in oprofile code. Use
user_64bit_mode() instead.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-4-krisman@collabora.com

---
 arch/x86/oprofile/backtrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/oprofile/backtrace.c b/arch/x86/oprofile/backtrace.c
index a2488b6..1d8391f 100644
--- a/arch/x86/oprofile/backtrace.c
+++ b/arch/x86/oprofile/backtrace.c
@@ -49,7 +49,7 @@ x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
 	struct stack_frame_ia32 *head;
 
 	/* User process is IA32 */
-	if (!current || !test_thread_flag(TIF_IA32))
+	if (!current || user_64bit_mode(regs))
 		return 0;
 
 	head = (struct stack_frame_ia32 *) regs->bp;

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/elf: Use e_machine to choose DLINFO in compat
  2020-10-04  3:25 ` [PATCH v3 04/10] x86: elf: Use e_machine to choose DLINFO in compat Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Gabriel Krisman Bertazi, Thomas Gleixner, Andy Lutomirski, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     2656af0d5abfa26d7f1e40f92e9953fe155b950a
Gitweb:        https://git.kernel.org/tip/2656af0d5abfa26d7f1e40f92e9953fe155b950a
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:30 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

x86/elf: Use e_machine to choose DLINFO in compat

Since TIF_X32 is going away, avoid using it to find the ELF type on
ARCH_DLINFO.

According to SysV AMD64 ABI Draft, an AMD64 ELF object using ILP32 must
have ELFCLASS32 with (E_MACHINE == EM_X86_64), so use that ELF field to
differentiate a x32 object from a IA32 object when loading ARCH_DLINFO in
compat mode.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Andy Lutomirski <luto@kernel.org>
Link: https://lore.kernel.org/r/20201004032536.1229030-5-krisman@collabora.com

---
 arch/x86/include/asm/elf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index b9a5d48..9220efc 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -361,7 +361,7 @@ do {									\
 #define AT_SYSINFO		32
 
 #define COMPAT_ARCH_DLINFO						\
-if (test_thread_flag(TIF_X32))						\
+if (exec->e_machine == EM_X86_64)					\
 	ARCH_DLINFO_X32;						\
 else									\
 	ARCH_DLINFO_IA32

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] x86/compat: Simplify compat syscall userspace allocation
  2020-10-04  3:25 ` [PATCH v3 02/10] x86: Simplify compat syscall userspace allocation Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Andy Lutomirski, Gabriel Krisman Bertazi, Thomas Gleixner, x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     214f0e804358cdd13b5cbe4445189f23e30618b4
Gitweb:        https://git.kernel.org/tip/214f0e804358cdd13b5cbe4445189f23e30618b4
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:28 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

x86/compat: Simplify compat syscall userspace allocation

When allocating user memory space for a compat system call, don't consider
whether the originating code is IA32 or X32, just allocate from a safe
region for both, beyond the redzone.  This should be safe for IA32, and has
the benefit of avoiding TIF_IA32, which is about to be removed.

Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20201004032536.1229030-3-krisman@collabora.com

---
 arch/x86/include/asm/compat.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/compat.h b/arch/x86/include/asm/compat.h
index 0e327a0..f145e33 100644
--- a/arch/x86/include/asm/compat.h
+++ b/arch/x86/include/asm/compat.h
@@ -177,14 +177,13 @@ typedef struct user_regs_struct compat_elf_gregset_t;
 
 static inline void __user *arch_compat_alloc_user_space(long len)
 {
-	compat_uptr_t sp;
-
-	if (test_thread_flag(TIF_IA32)) {
-		sp = task_pt_regs(current)->sp;
-	} else {
-		/* -128 for the x32 ABI redzone */
-		sp = task_pt_regs(current)->sp - 128;
-	}
+	compat_uptr_t sp = task_pt_regs(current)->sp;
+
+	/*
+	 * -128 for the x32 ABI redzone.  For IA32, it is not strictly
+	 * necessary, but not harmful.
+	 */
+	sp -= 128;
 
 	return (void __user *)round_down(sp - len, 16);
 }

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [tip: x86/cleanups] perf/x86: Avoid TIF_IA32 when checking 64bit mode
  2020-10-04  3:25 ` [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
@ 2020-10-26 12:52   ` tip-bot2 for Gabriel Krisman Bertazi
  0 siblings, 0 replies; 24+ messages in thread
From: tip-bot2 for Gabriel Krisman Bertazi @ 2020-10-26 12:52 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Andy Lutomirski, Gabriel Krisman Bertazi, Thomas Gleixner,
	Peter Zijlstra (Intel),
	x86, LKML

The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     375d4bfda57392f0865dae051e1c4bd2700e8d71
Gitweb:        https://git.kernel.org/tip/375d4bfda57392f0865dae051e1c4bd2700e8d71
Author:        Gabriel Krisman Bertazi <krisman@collabora.com>
AuthorDate:    Sat, 03 Oct 2020 23:25:27 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 26 Oct 2020 13:46:46 +01:00

perf/x86: Avoid TIF_IA32 when checking 64bit mode

In preparation to remove TIF_IA32, stop using it in perf events code.

Tested by running perf on 32-bit, 64-bit and x32 applications.

Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20201004032536.1229030-2-krisman@collabora.com

---
 arch/x86/events/core.c      | 2 +-
 arch/x86/events/intel/ds.c  | 2 +-
 arch/x86/events/intel/lbr.c | 2 +-
 arch/x86/kernel/perf_regs.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index a88c94d..77b963e 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2602,7 +2602,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent
 	struct stack_frame_ia32 frame;
 	const struct stack_frame_ia32 __user *fp;
 
-	if (!test_thread_flag(TIF_IA32))
+	if (user_64bit_mode(regs))
 		return 0;
 
 	cs_base = get_segment_base(regs->cs);
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 404315d..99a59f3 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -1259,7 +1259,7 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
 		old_to = to;
 
 #ifdef CONFIG_X86_64
-		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
+		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
 #endif
 		insn_init(&insn, kaddr, size, is_64bit);
 		insn_get_length(&insn);
diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index 8961653..1aadb25 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -1221,7 +1221,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 	 * on 64-bit systems running 32-bit apps
 	 */
 #ifdef CONFIG_X86_64
-	is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
+	is64 = kernel_ip((unsigned long)addr) || any_64bit_mode(current_pt_regs());
 #endif
 	insn_init(&insn, addr, bytes_read, is64);
 	insn_get_opcode(&insn);
diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c
index bb7e113..9332c49 100644
--- a/arch/x86/kernel/perf_regs.c
+++ b/arch/x86/kernel/perf_regs.c
@@ -123,7 +123,7 @@ int perf_reg_validate(u64 mask)
 
 u64 perf_reg_abi(struct task_struct *task)
 {
-	if (test_tsk_thread_flag(task, TIF_IA32))
+	if (!user_64bit_mode(task_pt_regs(task)))
 		return PERF_SAMPLE_REGS_ABI_32;
 	else
 		return PERF_SAMPLE_REGS_ABI_64;

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32
  2020-10-04  3:25 ` [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
  2020-10-26 12:52   ` [tip: x86/cleanups] " tip-bot2 for Gabriel Krisman Bertazi
@ 2020-11-05 20:01   ` Dmitry Safonov
  1 sibling, 0 replies; 24+ messages in thread
From: Dmitry Safonov @ 2020-11-05 20:01 UTC (permalink / raw)
  To: Gabriel Krisman Bertazi
  Cc: Andy Lutomirski, Thomas Gleixner, hch, H. Peter Anvin,
	Borislav Petkov, rric, Peter Zijlstra, Ingo Molnar, X86 ML,
	open list, Dave Hansen, sean.j.christopherson, kernel

On Sun, 4 Oct 2020 at 04:31, Gabriel Krisman Bertazi
<krisman@collabora.com> wrote:
>
> Now that these flags are no longer used, reclaim those TI bits.
>
> Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>

Oh wow!
I've just started rebasing patches that do essentially the same on linux-next
and found that it's already done and merged.

Thanks for doing this! :-)

-- 
             Dmitry

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2020-11-05 20:02 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-04  3:25 [PATCH v3 00/10] Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 01/10] x86: events: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] perf/x86: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 02/10] x86: Simplify compat syscall userspace allocation Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] x86/compat: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 03/10] x86: oprofile: Avoid TIF_IA32 when checking 64bit mode Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] x86/oprofile: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 04/10] x86: elf: Use e_machine to choose DLINFO in compat Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 05/10] elf: Expose ELF header in compat_start_thread Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] elf: Expose ELF header in compat_start_thread() tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 06/10] x86: elf: Use e_machine to select start_thread for x32 Gabriel Krisman Bertazi
2020-10-08  0:05   ` Andy Lutomirski
2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 07/10] elf: Expose ELF header on arch_setup_additional_pages Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] elf: Expose ELF header on arch_setup_additional_pages() tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 08/10] x86: elf: Use e_machine to select additional_pages between x32 Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] x86/elf: Use e_machine to check for x32/ia32 in setup_additional_pages() tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 09/10] x86: Convert mmu context ia32_compat into a proper flags field Gabriel Krisman Bertazi
2020-10-08  0:07   ` Andy Lutomirski
2020-10-26 12:52   ` [tip: x86/cleanups] x86/mm: " tip-bot2 for Gabriel Krisman Bertazi
2020-10-04  3:25 ` [PATCH v3 10/10] x86: Reclaim TIF_IA32 and TIF_X32 Gabriel Krisman Bertazi
2020-10-26 12:52   ` [tip: x86/cleanups] " tip-bot2 for Gabriel Krisman Bertazi
2020-11-05 20:01   ` [PATCH v3 10/10] " Dmitry Safonov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.