From patchwork Thu Sep 24 17:09:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 11798015 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 88EF459D for ; Thu, 24 Sep 2020 17:18:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1970C2396D for ; Thu, 24 Sep 2020 17:18:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1970C2396D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=goodmis.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8EDFB6B0062; Thu, 24 Sep 2020 13:18:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 84EC46B005D; Thu, 24 Sep 2020 13:18:51 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71AD88E0001; Thu, 24 Sep 2020 13:18:51 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0078.hostedemail.com [216.40.44.78]) by kanga.kvack.org (Postfix) with ESMTP id 498446B0068 for ; Thu, 24 Sep 2020 13:18:51 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0A12C4428 for ; Thu, 24 Sep 2020 17:18:51 +0000 (UTC) X-FDA: 77298614862.08.pigs14_5107fa127160 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id CFE741819E769 for ; Thu, 24 Sep 2020 17:18:50 +0000 (UTC) X-Spam-Summary: 1,0,0,b3407a0a1f7b2533,d41d8cd98f00b204,srs0=be04=db=goodmis.org=rostedt@kernel.org,,RULES_HIT:41:152:355:379:800:901:960:967:973:988:989:1183:1260:1263:1277:1313:1314:1345:1434:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:1801:2194:2199:2393:2525:2553:2559:2565:2682:2685:2693:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3355:3657:3770:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:4605:5007:6119:6261:6742:6755:7266:7576:7904:8660:8985:9025:9388:10004:10400:11026:11232:11473:11658:11914:12043:12219:12291:12296:12297:12438:12517:12519:12555:12895:13148:13230:13255:14181:14394:14659:14721:21080:21451:21627:21740:21781:21939:21966:30029:30041:30054:30070:30074:30090,0,RBL:198.145.29.99:@kernel.org:.lbl8.mailshell.net-62.2.0.100 64.100.201.201;04yffni3tooa64ya1yyfbaiak5cogycspcmkckhw7r6drq4cihtk9jtg7ikqdw6.hz7i4g84m5r484yfxbrhq3fmmrgdts5dy8djk3bb3d6ohgcutrx5ahrn1b4dd6 p.1-lbl8 X-HE-Tag: pigs14_5107fa127160 X-Filterd-Recvd-Size: 5256 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Thu, 24 Sep 2020 17:18:50 +0000 (UTC) Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 100F7238D6; Thu, 24 Sep 2020 17:18:49 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.94) (envelope-from ) id 1kLUtL-0029cG-3k; Thu, 24 Sep 2020 13:18:47 -0400 Message-ID: <20200924171846.993048030@goodmis.org> User-Agent: quilt/0.66 Date: Thu, 24 Sep 2020 13:09:29 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Yafang Shao , Axel Rasmussen , Andrew Morton , Vlastimil Babka , Michel Lespinasse , Daniel Jordan , Davidlohr Bueso , Linux MM , Ingo Molnar , Joonsoo Kim , Mathieu Desnoyers Subject: [PATCH 1/2] tracepoints: Add helper to test if tracepoint is enabled in a header References: <20200924170928.466191266@goodmis.org> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Steven Rostedt (VMware)" As tracepoints are discouraged from being added in a header because it can cause side effects if other tracepoints are in headers, the common workaround is to add a function call that calls a wrapper function in a C file that then calls the tracepoint. But as function calls add overhead, this function should only be called when the tracepoint in question is enabled. To get around the overhead, a static_branch can be used that only gets set when the tracepoint is enabled, and then inside the block of the static branch can contain the call to the tracepoint wrapper. Add a tracepoint_enabled(tp) macro that gets passed the name of the tracepoint, and this becomes a static_branch that is enabled when the tracepoint is enabled and is a nop when the tracepoint is disabled. Signed-off-by: Steven Rostedt (VMware) --- Documentation/trace/tracepoints.rst | 25 ++++++++++++++++++++++ include/linux/tracepoint-defs.h | 33 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/Documentation/trace/tracepoints.rst b/Documentation/trace/tracepoints.rst index 6e3ce3bf3593..833d39ee1c44 100644 --- a/Documentation/trace/tracepoints.rst +++ b/Documentation/trace/tracepoints.rst @@ -146,3 +146,28 @@ with jump labels and avoid conditional branches. define tracepoints. Check http://lwn.net/Articles/379903, http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362 for a series of articles with more details. + +If you require calling a tracepoint from a header file, it is not +recommended to call one directly or to use the trace__enabled() +function call, as tracepoints in header files can have side effects if a +header is included from a file that has CREATE_TRACE_POINTS set. Instead, +include tracepoint-defs.h and use trace_enabled(). + +In a C file:: + + void do_trace_foo_bar_wrapper(args) + { + trace_foo_bar(args); + } + +In the header file:: + + DECLEARE_TRACEPOINT(foo_bar); + + static inline void some_inline_function() + { + [..] + if (trace_enabled(foo_bar)) + do_trace_foo_bar_wrapper(args); + [..] + } diff --git a/include/linux/tracepoint-defs.h b/include/linux/tracepoint-defs.h index b29950a19205..ca2f1f77f6f8 100644 --- a/include/linux/tracepoint-defs.h +++ b/include/linux/tracepoint-defs.h @@ -48,4 +48,37 @@ struct bpf_raw_event_map { u32 writable_size; } __aligned(32); +/* + * If a tracepoint needs to be called from a header file, it is not + * recommended to call it directly, as tracepoints in header files + * may cause side-effects. Instead, use trace_enabled() to test + * if the tracepoint is enabled, then if it is, call a wrapper + * function defined in a C file that will then call the tracepoint. + * + * For "trace_foo()", you would need to create a wrapper function + * in a C file to call trace_foo(): + * void trace_bar(args) { trace_foo(args); } + * Then in the header file, declare the tracepoint: + * DECLARE_TRACEPOINT(foo); + * And call your wrapper: + * static inline void some_inlined_function() { + * [..] + * if (tracepoint_enabled(foo)) + * trace_bar(args); + * [..] + * } + * + * Note: tracepoint_enabled(foo) is equivalent to trace_foo_enabled() + * but is safe to have in headers, where trace_foo_enabled() is not. + */ +#define DECLARE_TRACEPOINT(tp) \ + extern struct tracepoint __tracepoint_##tp + +#ifdef CONFIG_TRACEPOINTS +# define tracepoint_enabled(tp) \ + static_key_false(&(__tracepoint_##tp).key) +#else +# define tracepoint_enabled(tracepoint) false +#endif + #endif