From patchwork Wed Apr 3 22:03:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13616744 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2B5A155A46; Wed, 3 Apr 2024 22:03:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712181813; cv=none; b=NYK0DIeHcjep1fc/KCymgQh68DzSuor6CH/GsNhgYXiisVs0v4LHF1V7jpI/gD9B7vuihC7wfULcW45aFpEqLeXFoQKEC9NHSF1YTwXhlqilsq9gkPLM+NGCzJ9w/aoTw0CnGgW/jF5H8IRHkyozRq+xYvEYKn7RdSQk2uZDOOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712181813; c=relaxed/simple; bh=5sP3DAGV0y7InFAl2knqhH/EQZOjd1FykDpAx3ilIVc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=aX8jZi/GlOb8K8INQXeJiVejavBBydmUdslhLuWRbjI6/p7UBiExmKcVOTFvjBf2axxkKj5zVR8D3ezqZ8UPwnXxO+tJ3NiJP5wpLF+Yk2BjVzYZfbDh2O1zU1fTlQpAcajmTz0TPQ/Buj2/6RlL5oaTn5T6ycB5XJlADEKf9OQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OoJ/hj0S; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OoJ/hj0S" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13EA4C433C7; Wed, 3 Apr 2024 22:03:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712181812; bh=5sP3DAGV0y7InFAl2knqhH/EQZOjd1FykDpAx3ilIVc=; h=From:To:Cc:Subject:Date:From; b=OoJ/hj0Sh1/Uqgm0SedOslB6ho9oQ5WsukOps17SwBqERToVF7DUJzp65clDN8X1a 7SVhsGcjmoWZEsWWl7NsG+6SwnCR7VkGjKwL56rOxsx7j+UgXPxmoXAUcvaMQd/hDW /K+MtUdCfPoQiR3k8oPx1EnxKeJUSItJ7Sr71/EY/jXqIW9A+h5aa+zufQgBEg4T2O H7+Z2UCjzdcu8I68mWDXh7zg5zdV2C/yErMNx7BkO9MofiZA0hTz2Ebz2rmhQA5XEo 5YapNsKaBVYKVlfpQW5KrLG8cfsw7Q7ssW6ayvUZEiETWqeIB8f6ysIOVXF7vhzVzd ijizdZQHx9GpA== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, jolsa@kernel.org, Andrii Nakryiko , "Paul E . McKenney" Subject: [PATCH v3 1/2] ftrace: make extra rcu_is_watching() validation check optional Date: Wed, 3 Apr 2024 15:03:27 -0700 Message-ID: <20240403220328.455786-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING config option to control whether ftrace low-level code performs additional rcu_is_watching()-based validation logic in an attempt to catch noinstr violations. This check is expected to never be true and is mostly useful for low-level validation of ftrace subsystem invariants. For most users it should probably be kept disabled to eliminate unnecessary runtime overhead. This improves BPF multi-kretprobe (relying on ftrace and rethook infrastructure) runtime throughput by 2%, according to BPF benchmarks ([0]). [0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/ Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Paul E. McKenney Signed-off-by: Andrii Nakryiko Acked-by: Masami Hiramatsu (Google) --- include/linux/trace_recursion.h | 2 +- kernel/trace/Kconfig | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/linux/trace_recursion.h b/include/linux/trace_recursion.h index d48cd92d2364..24ea8ac049b4 100644 --- a/include/linux/trace_recursion.h +++ b/include/linux/trace_recursion.h @@ -135,7 +135,7 @@ extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip); # define do_ftrace_record_recursion(ip, pip) do { } while (0) #endif -#ifdef CONFIG_ARCH_WANTS_NO_INSTR +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING # define trace_warn_on_no_rcu(ip) \ ({ \ bool __ret = !rcu_is_watching(); \ diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 61c541c36596..7aebd1b8f93e 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -974,6 +974,19 @@ config FTRACE_RECORD_RECURSION_SIZE This file can be reset, but the limit can not change in size at runtime. +config FTRACE_VALIDATE_RCU_IS_WATCHING + bool "Validate RCU is on during ftrace execution" + depends on FUNCTION_TRACER + depends on ARCH_WANTS_NO_INSTR + help + All callbacks that attach to the function tracing have some sort of + protection against recursion. This option is only to verify that + ftrace (and other users of ftrace_test_recursion_trylock()) are not + called outside of RCU, as if they are, it can cause a race. But it + also has a noticeable overhead when enabled. + + If unsure, say N + config RING_BUFFER_RECORD_RECURSION bool "Record functions that recurse in the ring buffer" depends on FTRACE_RECORD_RECURSION From patchwork Wed Apr 3 22:03:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13616745 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93BE2155A46; Wed, 3 Apr 2024 22:03:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712181815; cv=none; b=a6sw7oE931HfPAmFNEjz003+pzydFW+zFq152LGz2fwVYeT8H375DK8gLd9dq9imqHChWQwRRVm5FwK47g2yydSUvEHZ/+PsU9NXuOiCLJZPHq6JiPShnSNPrnr5iMbZfCj7SZyaJx+Mi2ZN3hAQmGnsiOnviHScICMqYWIpy+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712181815; c=relaxed/simple; bh=n3Q8pcct6u1puk940GRaHLodcqgH7KSOUMdJGkXoHcs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QFUHB+QDedFz02JG8804juvIBAJaAC0qX60jM/21aEA+8YR3lfyolDGVsXzJcOx51YpqBsYkrgFFsgjoJga5csXPknzRJga/6qfWV/+ptrnn8Rs7PmMS/xBLFqry3F7zZG1GX0P5IDusPAC5EkCqcaL1WmpWfRtIyaLGBw0yV84= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kMiZaEW9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kMiZaEW9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4529BC433F1; Wed, 3 Apr 2024 22:03:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712181815; bh=n3Q8pcct6u1puk940GRaHLodcqgH7KSOUMdJGkXoHcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kMiZaEW9if+NKbiFuyVUgKfHehLr0akVWn5G0Wc0cDWVBeHQVAv2ZU05VYO8gZzC7 vYj8GNMipIJgdyjEwwQqdaHEQD3G71HoVNxurc5DoXuiBP70m1bmzj/fIfxbzALX0z OvJkS9qblmGRYB9PNMMQNG93P73oNRE75ywCTq15fDVKJI5mrs4ie2EY6AjqJL8jMt u0zow5tL9IcITwRzLgfsEWYC8tZWXo4yY6XcXrd4UQP4MLnlqCo4sqUlzzj6pTPKRC 6jZlc7SHN+EOyc6dVmCHrVbBo2EhuCn39mAtP3TPKYGom/RuzW3cBYtWY6eeP0YOgC RBNczoDnlMKjA== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, rostedt@goodmis.org, mhiramat@kernel.org Cc: bpf@vger.kernel.org, jolsa@kernel.org, Andrii Nakryiko , "Paul E . McKenney" Subject: [PATCH v3 2/2] rethook: honor CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING in rethook_try_get() Date: Wed, 3 Apr 2024 15:03:28 -0700 Message-ID: <20240403220328.455786-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240403220328.455786-1-andrii@kernel.org> References: <20240403220328.455786-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Take into account CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING when validating that RCU is watching when trying to setup rethooko on a function entry. This further (in addition to improvements in the previous patch) improves BPF multi-kretprobe (which rely on rethook) runtime throughput by 2.3%, according to BPF benchmarks ([0]). [0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/ Signed-off-by: Andrii Nakryiko --- kernel/trace/rethook.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index fa03094e9e69..15b8aa4048d9 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -166,6 +166,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh) if (unlikely(!handler)) return NULL; +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING /* * This expects the caller will set up a rethook on a function entry. * When the function returns, the rethook will eventually be reclaimed @@ -174,6 +175,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh) */ if (unlikely(!rcu_is_watching())) return NULL; +#endif return (struct rethook_node *)objpool_pop(&rh->pool); }