Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1897769ybh; Sun, 8 Mar 2020 16:24:40 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsCk8N3BVFE3/FLcodYgWLVo6zgMqOCTXUwkiLwptt1i5sF66Bsr6I4HShIsI65LolUr5uT X-Received: by 2002:a05:6830:57b:: with SMTP id f27mr4990796otc.363.1583709880281; Sun, 08 Mar 2020 16:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583709880; cv=none; d=google.com; s=arc-20160816; b=HQqKCxACktZNXD/WfQ/vr0lL2mjjPL1UulUh8U0I0ViE0hea+sWuPI3xopI6RfNbX9 ZclxCJ2x+6n18lS3cc/eq8xjJ14z3flGvgBryfzB3DYZAKHNbFWOTeuC1hYQVZ5WoBD/ 3rODRZvNQxpoqUIL5WCxjl7Kh9qdBjdZ0Hf8hzjUfoSZNkFZgRZcQ/L+nMozVs8CqQBl 3khXYHInReXpP53oP0cmJkCYCCBifTGOeF2MujKjTg9zdGGg28PAL3VqYdFtY3zUweBZ DnhF7zTu6SwWAlMiqLLQetRw0vVPOIOyQKjxO9Ovm9kmSPjV+AKBoalpbouygYtjULcS zTkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:subject:cc:to :from:date:user-agent:message-id; bh=aSnz4PFQIrat0GRt4hKyybtUhuXM/cODmuq/f201id0=; b=Ty6LGZwkeaxD1Sl2oeJmT5dtrlZy/MT0vnM11/wiQDN0CL95Gk7kHEnwa2eSR0Twju +FYh5ETOvJS2vbF0WmdwazoH3Xx00IJzNYYj+0kZ8CB4gG/6d25sSaPvSOjYfO7/zmL7 Z1oKinJBWfcUJkrhpjoNZ8ca6HkIRkquDxe862vPtTBb6Ek3TdAKnLoqgCfVbiq05ZmA QF1NI/AUmHzJp3XO+zzlxzcCV581J/0/XJtyMDQlYddeGfMfUubA9jCIAwSQjysyzUUC 54mKt8XRi3ON7IG5Y8YU9UoL7feFafj/a5j2unmFOpDfEX27h660v+6Zw3IThOYZTfjG EynQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f74si658093oib.15.2020.03.08.16.24.28; Sun, 08 Mar 2020 16:24:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726632AbgCHXXv (ORCPT + 99 others); Sun, 8 Mar 2020 19:23:51 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:57211 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726383AbgCHXXu (ORCPT ); Sun, 8 Mar 2020 19:23:50 -0400 Received: from p5de0bf0b.dip0.t-ipconnect.de ([93.224.191.11] helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jB5Ga-00033N-NS; Mon, 09 Mar 2020 00:23:29 +0100 Received: from nanos.tec.linutronix.de (localhost [IPv6:::1]) by nanos.tec.linutronix.de (Postfix) with ESMTP id 2AC2610408A; Mon, 9 Mar 2020 00:23:28 +0100 (CET) Message-Id: <20200308222609.017810037@linutronix.de> User-Agent: quilt/0.65 Date: Sun, 08 Mar 2020 23:24:00 +0100 From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Steven Rostedt , Brian Gerst , Juergen Gross , Frederic Weisbecker , Alexandre Chartre Subject: [patch part-II V2 01/13] context_tracking: Ensure that the critical path cannot be instrumented References: <20200308222359.370649591@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 adding the missing notrace/NOKPROBE annotations and converting the plain inlines to __always_inline. Signed-off-by: Thomas Gleixner --- include/linux/context_tracking.h | 14 +++++++------- include/linux/context_tracking_state.h | 6 +++--- kernel/context_tracking.c | 9 +++++---- 3 files changed, 15 insertions(+), 14 deletions(-) --- a/include/linux/context_tracking.h +++ b/include/linux/context_tracking.h @@ -20,32 +20,32 @@ extern void context_tracking_exit(enum c extern void context_tracking_user_enter(void); extern void context_tracking_user_exit(void); -static inline void user_enter(void) +static __always_inline void user_enter(void) { if (context_tracking_enabled()) context_tracking_enter(CONTEXT_USER); } -static inline void user_exit(void) +static __always_inline void user_exit(void) { if (context_tracking_enabled()) context_tracking_exit(CONTEXT_USER); } /* 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); } -static inline enum ctx_state exception_enter(void) +static __always_inline enum ctx_state exception_enter(void) { enum ctx_state prev_ctx; @@ -59,7 +59,7 @@ static inline enum ctx_state exception_e return prev_ctx; } -static inline void exception_exit(enum ctx_state prev_ctx) +static __always_inline void exception_exit(enum ctx_state prev_ctx) { if (context_tracking_enabled()) { if (prev_ctx != CONTEXT_KERNEL) @@ -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 notrace bool context_tracking_recursion_enter(void) { int recursion; @@ -44,8 +44,9 @@ static bool context_tracking_recursion_e return false; } +NOKPROBE_SYMBOL(context_tracking_recursion_enter); -static void context_tracking_recursion_exit(void) +static __always_inline void context_tracking_recursion_exit(void) { __this_cpu_dec(context_tracking.recursion); } @@ -59,7 +60,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 notrace __context_tracking_enter(enum ctx_state state) { /* Kernel threads aren't supposed to go to userspace */ WARN_ON_ONCE(!current->mm); @@ -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 notrace __context_tracking_exit(enum ctx_state state) { if (!context_tracking_recursion_enter()) return;