Skip to contentSkip to conversation

drop test_thread_flag checks

PATCH In review Dmitry Safonov 3 participants 11 messages Apr 14, 2016 – Apr 20, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover drop test_thread_flag checks

As it was suggested by Andy, I'm working on TIF_IA32 flag removal. In this first four places, it's quite trivial to use user_64bit_mode instead of test_thread_flag(). This should fix possible problems for native applications that change their code selector to __USER32_CS, but do not have TIF_IA32 flag.

I still quite don't know what to do with uprobes using ia32_compat check and will make the next patches about ptrace & signals usage of TIF_IA32.

Dmitry Safonov (4):
x86/events: down with test_thread_flag(TIF_IA32) x86/intel: down with test_thread_flag(TIF_IA32) x86/intel lbr: down with test_thread_flag(TIF_IA32) x86/oprofile: down with test_thread_flag(TIF_IA32)

 arch/x86/events/core.c        |  2 +-
 arch/x86/events/intel/core.c  |  2 +-
 arch/x86/events/intel/ds.c    |  2 +-
 arch/x86/events/intel/lbr.c   | 17 ++++++++++-------
 arch/x86/events/perf_event.h  |  2 +-
 arch/x86/oprofile/backtrace.c |  2 +-
 6 files changed, 15 insertions(+), 12 deletions(-)
Signature · 1 line
2.8.0
#1 lore raw

PATCH 1/4 x86/events: down with test_thread_flag(TIF_IA32)

We can use user_64bit_mode(regs) here instead of thread flag because we have full register frame.

---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
index 041e442a3e28..91d101a9a6e9 100644
@@ -2269,7 +2269,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 	struct stack_frame_ia32 frame;
 	const void __user *fp;
 
-	if (!test_thread_flag(TIF_IA32))
+	if (user_64bit_mode(regs))
 		return 0;
 
 	cs_base = get_segment_base(regs->cs);
Signature · 1 line
2.8.0
#4 lore raw

PATCH 2/4 x86/intel: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov

For IP + one insturction fixup there is need to know in which state (compat/native) is application. Now it's done with TIF_IA32 test, which is buggy, as the process may change it's CS register to __USER32_CS descriptor (and vice-versa) so instruction interpreter will fail to correctly fixup IP.
Changing to user_64bit_mode to check for interrupt register set is better, however it may race with task, that changes it's code selector frequiently.

---
 arch/x86/events/intel/ds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/ds.c +1 -1
index 8584b90d8e0b..e903a8d3b4b0 100644
@@ -962,7 +962,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) || user_64bit_mode(regs);
 #endif
 		insn_init(&insn, kaddr, size, is_64bit);
 		insn_get_length(&insn);
Signature · 1 line
2.8.0
#9 lore raw

PATCH 4/4 x86/oprofile: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov

As we have here full register set - just use user_64bit_mode on it.

---
 arch/x86/oprofile/backtrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/oprofile/backtrace.c +1 -1
index cb31a4440e58..405dadaee74a 100644
@@ -69,7 +69,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;
Signature · 1 line
2.8.0
#5 lore raw

PATCH 3/4 x86/intel lbr: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov

Use user_mode64_bit to check process state. For that pass interrupt register set from irq handler.
This should fix opcode decoder misinterpreting ABI for tasks that change their code selector.

---
 arch/x86/events/intel/core.c |  2 +-
 arch/x86/events/intel/lbr.c  | 17 ++++++++++-------
 arch/x86/events/perf_event.h |  2 +-
 3 files changed, 12 insertions(+), 9 deletions(-)
arch/x86/events/intel/core.c +1 -1
index 68fa55b4d42e..df13d1d6dbf6 100644
@@ -1860,7 +1860,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 
 	loops = 0;
 again:
-	intel_pmu_lbr_read();
+	intel_pmu_lbr_read(regs);
 	intel_pmu_ack_status(status);
 	if (++loops > 100) {
 		static bool warned = false;
arch/x86/events/intel/lbr.c +10 -7
index 6c3b7c1780c9..f1a1dbc77dea 100644
@@ -136,7 +136,9 @@ enum {
 	 X86_BR_IRQ		|\
 	 X86_BR_INT)
 
-static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
+
+static void
+intel_pmu_lbr_filter(struct pt_regs *regs, struct cpu_hw_events *cpuc);
 
 /*
  * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
@@ -500,7 +502,7 @@ static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
 	cpuc->lbr_stack.nr = out;
 }
 
-void intel_pmu_lbr_read(void)
+void intel_pmu_lbr_read(struct pt_regs *regs)
 {
 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
@@ -512,7 +514,7 @@ void intel_pmu_lbr_read(void)
 	else
 		intel_pmu_lbr_read_64(cpuc);
 
-	intel_pmu_lbr_filter(cpuc);
+	intel_pmu_lbr_filter(regs, cpuc);
 }
 
 /*
@@ -658,7 +660,8 @@ int intel_pmu_setup_lbr_filter(struct perf_event *event)
  * decoded (e.g., text page not present), then X86_BR_NONE is
  * returned.
  */
-static int branch_type(unsigned long from, unsigned long to, int abort)
+static int branch_type(unsigned long from, unsigned long to, int abort,
+		struct pt_regs *regs)
 {
 	struct insn insn;
 	void *addr;
@@ -724,7 +727,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) || user_64bit_mode(regs);
 #endif
 	insn_init(&insn, addr, bytes_read, is64);
 	insn_get_opcode(&insn);
@@ -830,7 +833,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
  * in PERF_SAMPLE_BRANCH_STACK sample may vary.
  */
 static void
-intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
+intel_pmu_lbr_filter(struct pt_regs *regs, struct cpu_hw_events *cpuc)
 {
 	u64 from, to;
 	int br_sel = cpuc->br_sel;
@@ -846,7 +849,7 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
 		from = cpuc->lbr_entries[i].from;
 		to = cpuc->lbr_entries[i].to;
 
-		type = branch_type(from, to, cpuc->lbr_entries[i].abort);
+		type = branch_type(from, to, cpuc->lbr_entries[i].abort, regs);
 		if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
 			if (cpuc->lbr_entries[i].in_tx)
 				type |= X86_BR_IN_TX;
arch/x86/events/perf_event.h +1 -1
index ad4dc7ffffb5..da1ec8240097 100644
@@ -899,7 +899,7 @@ void intel_pmu_lbr_enable_all(bool pmi);
 
 void intel_pmu_lbr_disable_all(void);
 
-void intel_pmu_lbr_read(void);
+void intel_pmu_lbr_read(struct pt_regs *regs);
 
 void intel_pmu_lbr_init_core(void);
 
Signature · 1 line
2.8.0
#2 lore raw

Re: [PATCH 1/4] x86/events: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov
Quoted 13 lines · Dmitry Safonov

We can use user_64bit_mode(regs) here instead of thread flag because we have full register frame.

---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +0 -0
index 041e442a3e28..91d101a9a6e9 100644

arch/x86/events/core.c perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) · line 2273

Quoted 6 lines
arch/x86/events/core.c +1 -1
@@ -2269,7 +2269,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
        struct stack_frame_ia32 frame;
        const void __user *fp;
 
-       if (!test_thread_flag(TIF_IA32))
+       if (user_64bit_mode(regs))
                return 0;

Peter, I got lost in the code that calls this. Are regs coming from the overflow interrupt's regs, current_pt_regs(), or perf_get_regs_user?

If it's the perf_get_regs_user, then this should be okay, but passing in the ABI field directly would be even nicer. If they're coming from the overflow interrupt's regs or current_pt_regs(), could we change that?

It might also be nice to make sure that we call perf_get_regs_user exactly once per overflow interrupt -- i.e. we could push it into the main code rather than the regs sampling code.

Quoted 5 lines
arch/x86/events/core.c +0 -1
        cs_base = get_segment_base(regs->cs);
--

2.8.0

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#6 lore raw

Re: [PATCH 3/4] x86/intel lbr: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov
Quoted 69 lines · Dmitry Safonov

Use user_mode64_bit to check process state. For that pass interrupt register set from irq handler.
This should fix opcode decoder misinterpreting ABI for tasks that change their code selector.

---
 arch/x86/events/intel/core.c |  2 +-
 arch/x86/events/intel/lbr.c  | 17 ++++++++++-------
 arch/x86/events/perf_event.h |  2 +-
 3 files changed, 12 insertions(+), 9 deletions(-)
arch/x86/events/intel/core.c +1 -1
index 68fa55b4d42e..df13d1d6dbf6 100644
@@ -1860,7 +1860,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 
        loops = 0;
 again:
-       intel_pmu_lbr_read();
+       intel_pmu_lbr_read(regs);
        intel_pmu_ack_status(status);
        if (++loops > 100) {
                static bool warned = false;
arch/x86/events/intel/lbr.c +7 -4
index 6c3b7c1780c9..f1a1dbc77dea 100644
@@ -136,7 +136,9 @@ enum {
         X86_BR_IRQ             |\
         X86_BR_INT)
 
-static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
+
+static void
+intel_pmu_lbr_filter(struct pt_regs *regs, struct cpu_hw_events *cpuc);
 
 /*
  * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
@@ -500,7 +502,7 @@ static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
        cpuc->lbr_stack.nr = out;
 }
 
-void intel_pmu_lbr_read(void)
+void intel_pmu_lbr_read(struct pt_regs *regs)
 {
        struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
@@ -512,7 +514,7 @@ void intel_pmu_lbr_read(void)
        else
                intel_pmu_lbr_read_64(cpuc);
 
-       intel_pmu_lbr_filter(cpuc);
+       intel_pmu_lbr_filter(regs, cpuc);
 }
 
 /*
@@ -658,7 +660,8 @@ int intel_pmu_setup_lbr_filter(struct perf_event *event)
  * decoded (e.g., text page not present), then X86_BR_NONE is
  * returned.
  */
-static int branch_type(unsigned long from, unsigned long to, int abort)
+static int branch_type(unsigned long from, unsigned long to, int abort,
+               struct pt_regs *regs)
 {
        struct insn insn;
        void *addr;

arch/x86/events/intel/lbr.c static int branch_type(unsigned long from, unsigned long to, int abort) · line 730

Quoted 5 lines
arch/x86/events/intel/lbr.c +1 -1
@@ -724,7 +727,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) || user_64bit_mode(regs);

Peterz, looking at this some more, would it make sense to pass user_regs and interrupt_regs (or whatever we'd call it) all the way through to here?

--Andy

#10 lore raw

Re: [PATCH 4/4] x86/oprofile: down with test_thread_flag(TIF_IA32)

In reply to Dmitry Safonov
Quoted 13 lines · Dmitry Safonov

As we have here full register set - just use user_64bit_mode on it.

---
 arch/x86/oprofile/backtrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/oprofile/backtrace.c +0 -0
index cb31a4440e58..405dadaee74a 100644

arch/x86/oprofile/backtrace.c x86_backtrace_32(struct pt_regs * const regs, unsigned int depth) · line 72

Quoted 5 lines
arch/x86/oprofile/backtrace.c +1 -1
@@ -69,7 +69,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))

This is presumably okay, but I know nothing about oprofile.

Quoted 6 lines
arch/x86/oprofile/backtrace.c +0 -1
                return 0;
 
        head = (struct stack_frame_ia32 *) regs->bp;
--

2.8.0

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
maintainer
#3 lore raw

Re: [PATCH 1/4] x86/events: down with test_thread_flag(TIF_IA32)

In reply to Andy Lutomirski
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · Dmitry Safonov

We can use user_64bit_mode(regs) here instead of thread flag because we have full register frame.

---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +0 -0
index 041e442a3e28..91d101a9a6e9 100644
@@ -2269,7 +2269,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
        struct stack_frame_ia32 frame;
        const void __user *fp;
 
Quoted 3 lines

arch/x86/events/core.c perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) · line 2273

Quoted 3 lines
arch/x86/events/core.c +1 -1
-       if (!test_thread_flag(TIF_IA32))
+       if (user_64bit_mode(regs))
                return 0;

Urgh, so why didn't I get Cc'ed to these patches to begin with?

Quoted 3 lines

Peter, I got lost in the code that calls this. Are regs coming from the overflow interrupt's regs, current_pt_regs(), or perf_get_regs_user?

So get_perf_callchain() will get regs from:

- interrupt/NMI regs
- perf_arch_fetch_caller_regs()

And when user && !user_mode(), we'll use:

- task_pt_regs() (which arguably should maybe be perf_get_regs_user())

to call perf_callchain_user(), which then, ands up calling perf_callchain_user32() which is expected to NO-OP for 64bit userspace.

Quoted 8 lines

If it's the perf_get_regs_user, then this should be okay, but passing in the ABI field directly would be even nicer. If they're coming from the overflow interrupt's regs or current_pt_regs(), could we change that?

It might also be nice to make sure that we call perf_get_regs_user exactly once per overflow interrupt -- i.e. we could push it into the main code rather than the regs sampling code.

The risk there is that we might not need the user regs at all to handle the overflow thingy, so doing it unconditionally would be unwanted.

maintainer
#7 lore raw

Re: [PATCH 3/4] x86/intel lbr: down with test_thread_flag(TIF_IA32)

In reply to Andy Lutomirski
Quoted 12 lines · Andy Lutomirski

arch/x86/events/intel/lbr.c static int branch_type(unsigned long from, unsigned long to, int abort) · line 730

Quoted 6 lines · Dmitry Safonov
arch/x86/events/intel/lbr.c +1 -1
@@ -724,7 +727,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) || user_64bit_mode(regs);

Peterz, looking at this some more, would it make sense to pass user_regs and interrupt_regs (or whatever we'd call it) all the way through to here?

Urgh; again, wtf wasn't I Cc'ed to these patches?

And not sure; if we never need the user regs, calling perf_get_user_regs() to set all that up seems like a massive waste of cycles.

#8 lore raw

Re: [PATCH 3/4] x86/intel lbr: down with test_thread_flag(TIF_IA32)

Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Andy Lutomirski

arch/x86/events/intel/lbr.c static int branch_type(unsigned long from, unsigned long to, int abort) · line 730

Quoted 6 lines · Dmitry Safonov
arch/x86/events/intel/lbr.c +1 -1
@@ -724,7 +727,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) || user_64bit_mode(regs);

Peterz, looking at this some more, would it make sense to pass user_regs and interrupt_regs (or whatever we'd call it) all the way through to here?

Quoted 1 line

Urgh; again, wtf wasn't I Cc'ed to these patches?

Sorry for that - that was my unintentional miss on git-send-email.

Quoted 3 lines

And not sure; if we never need the user regs, calling perf_get_user_regs() to set all that up seems like a massive waste of cycles.