public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: LKML <linux-kernel@vger.kernel.org>,
	x86@kernel.org, "Paul E. McKenney" <paulmck@kernel.org>,
	Andy Lutomirski <luto@kernel.org>,
	Alexandre Chartre <alexandre.chartre@oracle.com>,
	Frederic Weisbecker <frederic@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <sean.j.christopherson@intel.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Joel Fernandes <joel@joelfernandes.org>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Juergen Gross <jgross@suse.com>, Brian Gerst <brgerst@gmail.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Will Deacon <will@kernel.org>
Subject: Re: [patch V4 part 2 07/18] context_tracking: Ensure that the critical path cannot be instrumented
Date: Fri, 8 May 2020 17:23:09 +0900	[thread overview]
Message-ID: <20200508172309.470787329827c8bfcd797eb1@kernel.org> (raw)
In-Reply-To: <20200505134340.811520478@linutronix.de>

On Tue, 05 May 2020 15:41:19 +0200
Thomas Gleixner <tglx@linutronix.de> wrote:

> context tracking lacks a few protection mechanisms against instrumentation:
> 
>  - While the core functions are marked NOKPROBE they lack protection
>    against function tracing which is required as the function entry/exit
>    points can be utilized by BPF.
> 
>  - static functions invoked from the protected functions need to be marked
>    as well as they can be instrumented otherwise.
> 
>  - using plain inline allows the compiler to emit traceable and probable
>    functions.
> 
> Fix this by marking the functions noinstr and converting the plain inlines
> to __always_inline.
> 
> The NOKPROBE_SYMBOL() annotations are removed as the .noinstr.text section
> is already excluded from being probed.
> 
> Cures the following objtool warnings:
> 
>  vmlinux.o: warning: objtool: enter_from_user_mode()+0x34: call to __context_tracking_exit() leaves .noinstr.text section
>  vmlinux.o: warning: objtool: prepare_exit_to_usermode()+0x29: call to __context_tracking_enter() leaves .noinstr.text section
>  vmlinux.o: warning: objtool: syscall_return_slowpath()+0x29: call to __context_tracking_enter() leaves .noinstr.text section
>  vmlinux.o: warning: objtool: do_syscall_64()+0x7f: call to __context_tracking_enter() leaves .noinstr.text section
>  vmlinux.o: warning: objtool: do_int80_syscall_32()+0x3d: call to __context_tracking_enter() leaves .noinstr.text section
>  vmlinux.o: warning: objtool: do_fast_syscall_32()+0x9c: call to __context_tracking_enter() leaves .noinstr.text section
> 
> and generates new ones...
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

Looks good to me.

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Thanks!

> ---
>  include/linux/context_tracking.h       |    6 +++---
>  include/linux/context_tracking_state.h |    6 +++---
>  kernel/context_tracking.c              |   14 ++++++++------
>  3 files changed, 14 insertions(+), 12 deletions(-)
> 
> --- a/include/linux/context_tracking.h
> +++ b/include/linux/context_tracking.h
> @@ -33,13 +33,13 @@ static inline void user_exit(void)
>  }
>  
>  /* Called with interrupts disabled.  */
> -static inline void user_enter_irqoff(void)
> +static __always_inline void user_enter_irqoff(void)
>  {
>  	if (context_tracking_enabled())
>  		__context_tracking_enter(CONTEXT_USER);
>  
>  }
> -static inline void user_exit_irqoff(void)
> +static __always_inline void user_exit_irqoff(void)
>  {
>  	if (context_tracking_enabled())
>  		__context_tracking_exit(CONTEXT_USER);
> @@ -75,7 +75,7 @@ static inline void exception_exit(enum c
>   * is enabled.  If context tracking is disabled, returns
>   * CONTEXT_DISABLED.  This should be used primarily for debugging.
>   */
> -static inline enum ctx_state ct_state(void)
> +static __always_inline enum ctx_state ct_state(void)
>  {
>  	return context_tracking_enabled() ?
>  		this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
> --- a/include/linux/context_tracking_state.h
> +++ b/include/linux/context_tracking_state.h
> @@ -26,12 +26,12 @@ struct context_tracking {
>  extern struct static_key_false context_tracking_key;
>  DECLARE_PER_CPU(struct context_tracking, context_tracking);
>  
> -static inline bool context_tracking_enabled(void)
> +static __always_inline bool context_tracking_enabled(void)
>  {
>  	return static_branch_unlikely(&context_tracking_key);
>  }
>  
> -static inline bool context_tracking_enabled_cpu(int cpu)
> +static __always_inline bool context_tracking_enabled_cpu(int cpu)
>  {
>  	return context_tracking_enabled() && per_cpu(context_tracking.active, cpu);
>  }
> @@ -41,7 +41,7 @@ static inline bool context_tracking_enab
>  	return context_tracking_enabled() && __this_cpu_read(context_tracking.active);
>  }
>  
> -static inline bool context_tracking_in_user(void)
> +static __always_inline bool context_tracking_in_user(void)
>  {
>  	return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
>  }
> --- a/kernel/context_tracking.c
> +++ b/kernel/context_tracking.c
> @@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(context_tracking_key);
>  DEFINE_PER_CPU(struct context_tracking, context_tracking);
>  EXPORT_SYMBOL_GPL(context_tracking);
>  
> -static bool context_tracking_recursion_enter(void)
> +static noinstr bool context_tracking_recursion_enter(void)
>  {
>  	int recursion;
>  
> @@ -45,7 +45,7 @@ static bool context_tracking_recursion_e
>  	return false;
>  }
>  
> -static void context_tracking_recursion_exit(void)
> +static __always_inline void context_tracking_recursion_exit(void)
>  {
>  	__this_cpu_dec(context_tracking.recursion);
>  }
> @@ -59,7 +59,7 @@ static void context_tracking_recursion_e
>   * instructions to execute won't use any RCU read side critical section
>   * because this function sets RCU in extended quiescent state.
>   */
> -void __context_tracking_enter(enum ctx_state state)
> +void noinstr __context_tracking_enter(enum ctx_state state)
>  {
>  	/* Kernel threads aren't supposed to go to userspace */
>  	WARN_ON_ONCE(!current->mm);
> @@ -77,8 +77,10 @@ void __context_tracking_enter(enum ctx_s
>  			 * on the tick.
>  			 */
>  			if (state == CONTEXT_USER) {
> +				instr_begin();
>  				trace_user_enter(0);
>  				vtime_user_enter(current);
> +				instr_end();
>  			}
>  			rcu_user_enter();
>  		}
> @@ -99,7 +101,6 @@ void __context_tracking_enter(enum ctx_s
>  	}
>  	context_tracking_recursion_exit();
>  }
> -NOKPROBE_SYMBOL(__context_tracking_enter);
>  EXPORT_SYMBOL_GPL(__context_tracking_enter);
>  
>  void context_tracking_enter(enum ctx_state state)
> @@ -142,7 +143,7 @@ NOKPROBE_SYMBOL(context_tracking_user_en
>   * This call supports re-entrancy. This way it can be called from any exception
>   * handler without needing to know if we came from userspace or not.
>   */
> -void __context_tracking_exit(enum ctx_state state)
> +void noinstr __context_tracking_exit(enum ctx_state state)
>  {
>  	if (!context_tracking_recursion_enter())
>  		return;
> @@ -155,15 +156,16 @@ void __context_tracking_exit(enum ctx_st
>  			 */
>  			rcu_user_exit();
>  			if (state == CONTEXT_USER) {
> +				instr_begin();
>  				vtime_user_exit(current);
>  				trace_user_exit(0);
> +				instr_end();
>  			}
>  		}
>  		__this_cpu_write(context_tracking.state, CONTEXT_KERNEL);
>  	}
>  	context_tracking_recursion_exit();
>  }
> -NOKPROBE_SYMBOL(__context_tracking_exit);
>  EXPORT_SYMBOL_GPL(__context_tracking_exit);
>  
>  void context_tracking_exit(enum ctx_state state)
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

  reply	other threads:[~2020-05-08  8:23 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-05 13:41 [patch V4 part 2 00/18] x86/entry: Entry/exception code rework, syscall and KVM changes Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 01/18] x86/entry/64: Move non entry code into .text section Thomas Gleixner
2020-05-06 15:51   ` Peter Zijlstra
2020-05-08  1:31   ` Steven Rostedt
2020-05-08 23:53   ` Andy Lutomirski
2020-05-10 13:39     ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 02/18] x86/entry/32: " Thomas Gleixner
2020-05-07 13:15   ` Alexandre Chartre
2020-05-07 14:14     ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 03/18] x86/entry: Mark enter_from_user_mode() noinstr Thomas Gleixner
2020-05-08  8:21   ` Masami Hiramatsu
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 04/18] x86/entry/common: Protect against instrumentation Thomas Gleixner
2020-05-07 13:39   ` Alexandre Chartre
2020-05-07 14:13     ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 05/18] x86/entry: Move irq tracing on syscall entry to C-code Thomas Gleixner
2020-05-07 13:55   ` Alexandre Chartre
2020-05-07 14:10     ` Thomas Gleixner
2020-05-07 15:03       ` Thomas Gleixner
2020-05-07 17:06         ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 06/18] x86/entry: Move irq flags tracing to prepare_exit_to_usermode() Thomas Gleixner
2020-05-08 23:57   ` Andy Lutomirski
2020-05-09 10:16     ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 07/18] context_tracking: Ensure that the critical path cannot be instrumented Thomas Gleixner
2020-05-08  8:23   ` Masami Hiramatsu [this message]
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 08/18] lib/smp_processor_id: Move it into noinstr section Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] x86/speculation/mds: Mark mds_user_clear_cpu_buffers() __always_inline tip-bot2 for Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] lib/smp_processor_id: Move it into noinstr section tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 09/18] x86/speculation/mds: Mark mds_user_clear_cpu_buffers() __always_inline Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 10/18] x86/entry/64: Check IF in __preempt_enable_notrace() thunk Thomas Gleixner
2020-05-07 14:15   ` Alexandre Chartre
2020-05-09  0:10   ` Andy Lutomirski
2020-05-09 10:25     ` Thomas Gleixner
2020-05-10 18:47       ` Thomas Gleixner
2020-05-11 18:27         ` Thomas Gleixner
2020-05-12  1:48     ` Steven Rostedt
2020-05-12  1:51   ` Steven Rostedt
2020-05-12  8:14     ` Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 11/18] x86/entry/64: Mark ___preempt_schedule_notrace() thunk noinstr Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 12/18] x86,objtool: Make entry_64_compat.S objtool clean Thomas Gleixner
2020-05-09  0:11   ` Andy Lutomirski
2020-05-09 10:06     ` Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] x86/entry: " tip-bot2 for Peter Zijlstra
2020-05-05 13:41 ` [patch V4 part 2 13/18] x86/kvm: Move context tracking where it belongs Thomas Gleixner
2020-05-06  7:42   ` Paolo Bonzini
2020-05-09  0:14   ` Andy Lutomirski
2020-05-09 10:12     ` Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 14/18] x86/kvm/vmx: Add hardirq tracing to guest enter/exit Thomas Gleixner
2020-05-06  7:55   ` Paolo Bonzini
2020-05-05 13:41 ` [patch V4 part 2 15/18] x86/kvm/svm: Handle hardirqs proper on " Thomas Gleixner
2020-05-06  8:15   ` Paolo Bonzini
2020-05-06  8:48     ` Thomas Gleixner
2020-05-06  9:21       ` Paolo Bonzini
2020-05-07 14:44         ` [patch V5 " Thomas Gleixner
2020-05-08 13:45           ` Paolo Bonzini
2020-05-08 14:01             ` Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 16/18] context_tracking: Make guest_enter/exit() .noinstr ready Thomas Gleixner
2020-05-19 19:58   ` [tip: x86/entry] " tip-bot2 for Thomas Gleixner
2020-05-05 13:41 ` [patch V4 part 2 17/18] x86/kvm/vmx: Move guest enter/exit into .noinstr.text Thomas Gleixner
2020-05-06  8:17   ` Paolo Bonzini
2020-05-05 13:41 ` [patch V4 part 2 18/18] x86/kvm/svm: " Thomas Gleixner
2020-05-06  8:17   ` Paolo Bonzini
2020-05-07 14:47   ` Alexandre Chartre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200508172309.470787329827c8bfcd797eb1@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=alexandre.chartre@oracle.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=brgerst@gmail.com \
    --cc=frederic@kernel.org \
    --cc=jgross@suse.com \
    --cc=joel@joelfernandes.org \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=sean.j.christopherson@intel.com \
    --cc=tglx@linutronix.de \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox