From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D2F0C4727E for ; Fri, 25 Sep 2020 21:37:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E54B020659 for ; Fri, 25 Sep 2020 21:37:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eLDZbRto" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728736AbgIYVhO (ORCPT ); Fri, 25 Sep 2020 17:37:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727495AbgIYVhO (ORCPT ); Fri, 25 Sep 2020 17:37:14 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EA6CC0613CE for ; Fri, 25 Sep 2020 14:37:14 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id mm21so168046pjb.4 for ; Fri, 25 Sep 2020 14:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TW6wlmmfMd4aKumjZRuqMrr4jbBaACouFEXMzF3b4zg=; b=eLDZbRtoVy4VydFLnbYK5iZ6uaMxjoYEvpd//jvLPkn0cL9HuuHKw1petsfWZnRHDK SdlJzJJvmDrf7CXh/AzJdh5sr0eXRBip0oCiez+KVPahBWODvk11O8ys3IbB9t1ulqjw vYXN6lffRPPN9sXgBjkkxqlUAhD1GSpkQqmH+16NtsCLhLEmoJQAlbopBsRvwHVM5md/ EniFLu+gXxSgGOvUjOOh5V3Z6PQFOh55sdkQDuysdYf04LJljhrt/mu4jpbISUgywfXK Iz1D5PAcrwn8XYmSNEANYCMt7Ps7gpcoPsNdLNr7re/yNpGlLYvH/QwAIrlXJ+C297pt wPYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TW6wlmmfMd4aKumjZRuqMrr4jbBaACouFEXMzF3b4zg=; b=RDrnwyGligCKs4xydc8JuO8G9mXGSg788ix9Jn6ye6SwfIh5qAqQNEiF/b1mzzKFle bLxs+VjlhFL0gGocU6Ckgmq0SIWc4G827pzxLyokYCI5JOJHw/Lz+8Q2dP41AD7/ERDi /uk7zQEz23YoX7VTVi4rcQDkJoSC9AQ3TgxDuj8InbqEKGURPwsnyRXAnq4Si9YPrgoV vB2zFJ79wr51VWZ1tXy1RMwNS5kNhSxvybhSZL7pWWcR7kHw84cFc4bwskzKN1xGTCKG LxQZtcMLJGpMJy8l5ud6s5Dc8wrcq/h38xPKLm61Zi10d/uaMytjO5rN/ExE3jF+TEZE IFOA== X-Gm-Message-State: AOAM533nkIWgu1M1SwcRQLXSK6x+vL+zypEjMaFRLbhiOWfcN1TbV/B3 BeCXZYpwQtVg3p1+9QWEKhST9qe45u99VGAID3iXvQ== X-Google-Smtp-Source: ABdhPJweWYHxudjQMVOBW7Gj3F73F5BA3V0qipJ2JCG/uVqPAPgDRT5YAo01pi8LD2GOO7+8Qz1tE17pXvY6Y28yD+k= X-Received: by 2002:a17:90a:9403:: with SMTP id r3mr506486pjo.52.1601069833708; Fri, 25 Sep 2020 14:37:13 -0700 (PDT) MIME-Version: 1.0 References: <20200925211206.423598568@goodmis.org> <20200925211819.767526657@goodmis.org> In-Reply-To: <20200925211819.767526657@goodmis.org> From: Axel Rasmussen Date: Fri, 25 Sep 2020 14:36:37 -0700 Message-ID: Subject: Re: [PATCH 1/3 v2] tracepoints: Add helper to test if tracepoint is enabled in a header To: Steven Rostedt Cc: LKML , Yafang Shao , Andrew Morton , Vlastimil Babka , Michel Lespinasse , Daniel Jordan , Davidlohr Bueso , Linux MM , Ingo Molnar , Joonsoo Kim Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 25, 2020 at 2:18 PM Steven Rostedt wrote: > > 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, as well as bloat the > kernel as the trace_() function is not a small inline, 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 this overhead, a static_branch can be used to only > have the tracepoint wrapper get called when the tracepoint is enabled. > > 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 | 27 +++++++++++++++++++++++ > include/linux/tracepoint-defs.h | 34 +++++++++++++++++++++++++++++ > 2 files changed, 61 insertions(+) > > diff --git a/Documentation/trace/tracepoints.rst b/Documentation/trace/tracepoints.rst > index 6e3ce3bf3593..68579ebd1e4c 100644 > --- a/Documentation/trace/tracepoints.rst > +++ b/Documentation/trace/tracepoints.rst > @@ -146,3 +146,30 @@ 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, as > +well as the trace_() is not that small of an inline > +and can bloat the kernel if used by other inlined functions. Instead, > +include tracepoint-defs.h and use tracepoint_enabled(). > + > +In a C file:: > + > + void do_trace_foo_bar_wrapper(args) > + { > + trace_foo_bar(args); > + } > + > +In the header file:: > + > + DECLEARE_TRACEPOINT(foo_bar); Should be "DECLARE_..." > + > + static inline void some_inline_function() > + { > + [..] > + if (tracepoint_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..60625973faaf 100644 > --- a/include/linux/tracepoint-defs.h > +++ b/include/linux/tracepoint-defs.h > @@ -48,4 +48,38 @@ 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 and bloat the kernel. Instead, use > + * tracepoint_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_bar()", you would need to create a wrapper function > + * in a C file to call trace_foo_bar(): > + * void do_trace_foo_bar(args) { trace_foo_bar(args); } > + * Then in the header file, declare the tracepoint: > + * DECLARE_TRACEPOINT(foo_bar); > + * And call your wrapper: > + * static inline void some_inlined_function() { > + * [..] > + * if (tracepoint_enabled(foo_bar)) > + * do_trace_foo_bar(args); > + * [..] > + * } > + * > + * Note: tracepoint_enabled(foo_bar) is equivalent to trace_foo_bar_enabled() > + * but is safe to have in headers, where trace_foo_bar_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 > -- > 2.28.0 > > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69314C4741F for ; Fri, 25 Sep 2020 21:37:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DABD220637 for ; Fri, 25 Sep 2020 21:37:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eLDZbRto" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DABD220637 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 142BC6B005C; Fri, 25 Sep 2020 17:37:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CBEB6B005D; Fri, 25 Sep 2020 17:37:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 009398E0001; Fri, 25 Sep 2020 17:37:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0170.hostedemail.com [216.40.44.170]) by kanga.kvack.org (Postfix) with ESMTP id DDAEB6B005C for ; Fri, 25 Sep 2020 17:37:15 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 823B252A8 for ; Fri, 25 Sep 2020 21:37:15 +0000 (UTC) X-FDA: 77302894830.17.grape80_1e181bd2716a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id 5C81C180D0180 for ; Fri, 25 Sep 2020 21:37:15 +0000 (UTC) X-HE-Tag: grape80_1e181bd2716a X-Filterd-Recvd-Size: 7404 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Fri, 25 Sep 2020 21:37:14 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id t7so170674pjd.3 for ; Fri, 25 Sep 2020 14:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TW6wlmmfMd4aKumjZRuqMrr4jbBaACouFEXMzF3b4zg=; b=eLDZbRtoVy4VydFLnbYK5iZ6uaMxjoYEvpd//jvLPkn0cL9HuuHKw1petsfWZnRHDK SdlJzJJvmDrf7CXh/AzJdh5sr0eXRBip0oCiez+KVPahBWODvk11O8ys3IbB9t1ulqjw vYXN6lffRPPN9sXgBjkkxqlUAhD1GSpkQqmH+16NtsCLhLEmoJQAlbopBsRvwHVM5md/ EniFLu+gXxSgGOvUjOOh5V3Z6PQFOh55sdkQDuysdYf04LJljhrt/mu4jpbISUgywfXK Iz1D5PAcrwn8XYmSNEANYCMt7Ps7gpcoPsNdLNr7re/yNpGlLYvH/QwAIrlXJ+C297pt wPYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TW6wlmmfMd4aKumjZRuqMrr4jbBaACouFEXMzF3b4zg=; b=d4HO7eXzAhWVC4ZMhvnByoH0yYw03w61sfBXtm3M24W8NU9OebHOMPZyx+HT33EApD xNDNwwCcRveUPlknC3qqoyVGl+qX0Kf72SxMzfEKnu/puv60W0XuM/5HHU1jQbp5aW58 is5cIpQ1QFEArGMf9WP42hFXQ+8vfmzUmfwsWZ99Z5Y+yI0vfggVIKnCx16kwZB2tdTO wNlWC6qbhAgYAyCb/j861HlhTwDRrvO5TEjx/gjwXzWiYs5U8D+7lvwcUVnDEg1eVuwn oZaEZkSfmWAmO5eKVo2EeOeN7T4rrO6l61fVUUiuk3QHCMUpvIKOe1iKZundyigSOHcX XiEg== X-Gm-Message-State: AOAM531cLzuZLvRZipy3QFrPW7menvvlyUdvSPS3cIwO2ad/Hk8l0uBA rnl/pqkrKLzrhg+50PA00iS3UpEZ9zdsB61XhYx5iQ== X-Google-Smtp-Source: ABdhPJweWYHxudjQMVOBW7Gj3F73F5BA3V0qipJ2JCG/uVqPAPgDRT5YAo01pi8LD2GOO7+8Qz1tE17pXvY6Y28yD+k= X-Received: by 2002:a17:90a:9403:: with SMTP id r3mr506486pjo.52.1601069833708; Fri, 25 Sep 2020 14:37:13 -0700 (PDT) MIME-Version: 1.0 References: <20200925211206.423598568@goodmis.org> <20200925211819.767526657@goodmis.org> In-Reply-To: <20200925211819.767526657@goodmis.org> From: Axel Rasmussen Date: Fri, 25 Sep 2020 14:36:37 -0700 Message-ID: Subject: Re: [PATCH 1/3 v2] tracepoints: Add helper to test if tracepoint is enabled in a header To: Steven Rostedt Cc: LKML , Yafang Shao , Andrew Morton , Vlastimil Babka , Michel Lespinasse , Daniel Jordan , Davidlohr Bueso , Linux MM , Ingo Molnar , Joonsoo Kim Content-Type: text/plain; charset="UTF-8" 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: On Fri, Sep 25, 2020 at 2:18 PM Steven Rostedt wrote: > > 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, as well as bloat the > kernel as the trace_() function is not a small inline, 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 this overhead, a static_branch can be used to only > have the tracepoint wrapper get called when the tracepoint is enabled. > > 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 | 27 +++++++++++++++++++++++ > include/linux/tracepoint-defs.h | 34 +++++++++++++++++++++++++++++ > 2 files changed, 61 insertions(+) > > diff --git a/Documentation/trace/tracepoints.rst b/Documentation/trace/tracepoints.rst > index 6e3ce3bf3593..68579ebd1e4c 100644 > --- a/Documentation/trace/tracepoints.rst > +++ b/Documentation/trace/tracepoints.rst > @@ -146,3 +146,30 @@ 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, as > +well as the trace_() is not that small of an inline > +and can bloat the kernel if used by other inlined functions. Instead, > +include tracepoint-defs.h and use tracepoint_enabled(). > + > +In a C file:: > + > + void do_trace_foo_bar_wrapper(args) > + { > + trace_foo_bar(args); > + } > + > +In the header file:: > + > + DECLEARE_TRACEPOINT(foo_bar); Should be "DECLARE_..." > + > + static inline void some_inline_function() > + { > + [..] > + if (tracepoint_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..60625973faaf 100644 > --- a/include/linux/tracepoint-defs.h > +++ b/include/linux/tracepoint-defs.h > @@ -48,4 +48,38 @@ 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 and bloat the kernel. Instead, use > + * tracepoint_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_bar()", you would need to create a wrapper function > + * in a C file to call trace_foo_bar(): > + * void do_trace_foo_bar(args) { trace_foo_bar(args); } > + * Then in the header file, declare the tracepoint: > + * DECLARE_TRACEPOINT(foo_bar); > + * And call your wrapper: > + * static inline void some_inlined_function() { > + * [..] > + * if (tracepoint_enabled(foo_bar)) > + * do_trace_foo_bar(args); > + * [..] > + * } > + * > + * Note: tracepoint_enabled(foo_bar) is equivalent to trace_foo_bar_enabled() > + * but is safe to have in headers, where trace_foo_bar_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 > -- > 2.28.0 > >