Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp680306rdb; Thu, 30 Nov 2023 15:42:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IE+xn4pbFmvrf8hZhpj1TaR6DpOohzDOiOcP1zXbMaGwE5AE8HsX/vChg53PWtLSow4uRqU X-Received: by 2002:a05:6a21:6d81:b0:18d:4836:f5b8 with SMTP id wl1-20020a056a216d8100b0018d4836f5b8mr3127739pzb.53.1701387726976; Thu, 30 Nov 2023 15:42:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701387726; cv=none; d=google.com; s=arc-20160816; b=Tp8wGNF4bKxnB9Ly91cqM1mXYst+syiEsCItF5kCtlJD3f5zgngiWOqjyu6lAsnS/E GOv3Mq3P+oHD2wjjaydwTKOQjaRd0WGgAkTtcR7pEVBIF8lsARoFF9B2fjh56tMhJoH2 vmgeVPrZLnQjZPfIZS2cSxWk69J5acBY6t86YxFjwtrL+TWafs2Iq72uYRJJAcCwalZW gbXaaBOXBQYzCpgrfsb4pCzyw84BLgybJx2Sn4ZMjNV1rf9MyRCS3WmwgiKQ3kvlh3Fu jfzZuf/B56FiB00nwjhnxF2aiFI2d8XImwKCDj9xr2hbA8US3ruEvYiUdiuHKIIjRuMF Jg1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NrPwIwR3VEBYybM6RGTBtB0qlrcLpOcaaHY2TiBjIw0=; fh=Xr9EZaLbYspLudo3lCz3I6iQD6BKril0MIzT5DobTXc=; b=t4Zalf3p0ntIRGARWXzIU1ecjTxAFn23PqaiPYl476Ord2xPHHskNzDoviYpaScfpZ YR3O+Hyx10BLv4DU+4mpdnX33cNJyWAWbLybuOq1mavPQpxBtg8uwO5caN/dQrPnZXXS cFy2Gda3i8MW82xMsfQFqSfycclo1qwykxmJzzC6wlP0oH3VRsnBSPVHTrN6XpOCoKQb GTjXx+F6VHb+lclHsO1qHsn+lAqPvi6GbYOHmPLz3qOoDOin7yqrQVM1jjmgEoqFX3sP 1fxE0HnGyESpNLqOj/nfVKzprwM/Gh/g3Ui3zzsNMuDYyIAPKG310f5z3Z96zGe5K6mo l4TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=s1l68HCg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=semihalf.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id j3-20020a63cf03000000b0059f0cebd04csi2194391pgg.722.2023.11.30.15.42.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 15:42:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=s1l68HCg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=semihalf.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 31F6680F6D0D; Thu, 30 Nov 2023 15:41:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377370AbjK3XlU (ORCPT + 99 others); Thu, 30 Nov 2023 18:41:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377368AbjK3XlR (ORCPT ); Thu, 30 Nov 2023 18:41:17 -0500 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A10CD10F3 for ; Thu, 30 Nov 2023 15:41:22 -0800 (PST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-54acdd65c88so1814165a12.2 for ; Thu, 30 Nov 2023 15:41:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf.com; s=google; t=1701387681; x=1701992481; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NrPwIwR3VEBYybM6RGTBtB0qlrcLpOcaaHY2TiBjIw0=; b=s1l68HCg7c52Zzrm/xjMY4hPvcMKxLo1Bir4T4KIEe9XuVAQvVQvZw36EF9L5VPhQQ HK9a+4BDzorCCgHnUIqsiy1FJwpP5HWx0n9abSGL1xGBH/hgeXV0XFAIJwSmN3tIDnq6 +j1wh6A5kPumIvY4UZ0NHbQFMdNuUU/P4SWE9I8KyWWNpaRkGrckvNn/dldgT0hBODj0 binyxgZwagQBHlowm6DnbLOlkjU/cGdhXP25NuqBLzXYMVeCcj723zWmyuc/cFdF2p3s 0A80DQzqH7oNTvJ5hrVN+6091V2YM0+6TNK5dWEkO37QJaJBNvy1t/lwX6Y2JlGgCAkR 45nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701387681; x=1701992481; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NrPwIwR3VEBYybM6RGTBtB0qlrcLpOcaaHY2TiBjIw0=; b=fm72K5diMbaZU5zHkzRRzYHiDn7wcR7PKwge6KhcSoEtd4xfNXtIFxbtPKcWXkOmnI kdtyuyckNtmg6fpYEgq1aXzC8TWldgcw1Ob2LgnT7hugtQBG9zsnzmEGPdrk9bUWOkOW PuBn4Z8t+VSrVef5UUCO8VbdGDyufyyIa3V7+3tpHqqAq0q29+02ZL7dvX87BXrAU3Ow 5R24cXto+eed21ZjJjadhJZ+L5KlWbNAVWRUl+aZQkLl4OHIXoyny1CRRXw/3/oMvEPo FUxipSR9dOTYWfVtdvQoSUJ8EyHI17pomSNDd/3UtXmmvIc9LMF5gnzPCSrsYCH2EDtK 1ngw== X-Gm-Message-State: AOJu0Yyeg9PQTM29C7DdfzM7oog1fipmb6Y9xL0MepLfq6RpjWAbUHZV wbkYYig5Tq79tbcRw5tf4OAP X-Received: by 2002:a17:906:fcba:b0:a16:88e8:2de7 with SMTP id qw26-20020a170906fcba00b00a1688e82de7mr232849ejb.23.1701387680935; Thu, 30 Nov 2023 15:41:20 -0800 (PST) Received: from ukaszb-l.semihalf.net (public-gprs368940.centertel.pl. [37.47.72.109]) by smtp.gmail.com with ESMTPSA id 9-20020a170906100900b009ddaf5ebb6fsm1210175ejm.177.2023.11.30.15.41.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 15:41:20 -0800 (PST) From: =?UTF-8?q?=C5=81ukasz=20Bartosik?= To: Jason Baron , Jim Cromie , Andrew Morton , Kees Cook , Douglas Anderson Cc: Guenter Roeck , Yaniv Tzoreff , Benson Leung , Steven Rostedt , Vincent Whitchurch , Pekka Paalanen , Sean Paul , Daniel Vetter , linux-kernel@vger.kernel.org, upstream@semihalf.com Subject: [PATCH v2 03/15] dyndbg: add write events to tracefs code Date: Fri, 1 Dec 2023 00:40:36 +0100 Message-ID: <20231130234048.157509-4-lb@semihalf.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog In-Reply-To: <20231130234048.157509-1-lb@semihalf.com> References: <20231130234048.157509-1-lb@semihalf.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 30 Nov 2023 15:41:57 -0800 (PST) From: Jim Cromie adds: ddebug_trace() uses trace_console() temporarily to issue printk:console event uses internal-ish __ftrace_trace_stack code: 4-context buffer stack, barriers per Steve Rostedt call it from new funcs: ddebug_printk() - print to both syslog/tracefs ddebug_dev_printk() - dev-print to both syslog/tracefs These handle both _DPRINTK_FLAGS_PRINTK and _DPRINTK_FLAGS_TRACE cases, allowing to vsnprintf the message once and use it for both, skipping past the KERN_DEBUG character for tracing. Finally, adjust the callers: __ddebug_{pr_debug,{,net,ib}dev_dbg}, replacing printk and dev_printk with the new funcs above. The _DPRINTK_FLAGS_TRACE flag character is 'T', so the following finds all callsites enabled for tracing: grep -P =p?T /proc/dynamic_debug/control This patch,~1,~2 are basically copies of: https://lore.kernel.org/lkml/ 20200825153338.17061-1-vincent.whitchurch@axis.com with a few differences: - s/dynamic_/ddebug_/ on Vincent's additions - __printf attrs on the _printk funcs - reuses trace_console() event, not adding a new "printk:dyndbg" event. next patch replaces this with 2 new events CC: vincent.whitchurch@axis.com Signed-off-by: Jim Cromie --- .../admin-guide/dynamic-debug-howto.rst | 5 +- lib/dynamic_debug.c | 156 +++++++++++++++--- 2 files changed, 133 insertions(+), 28 deletions(-) diff --git a/Documentation/admin-guide/dynamic-debug-howto.rst b/Documentation/admin-guide/dynamic-debug-howto.rst index 0b3d39c610d9..8a126e10a6c5 100644 --- a/Documentation/admin-guide/dynamic-debug-howto.rst +++ b/Documentation/admin-guide/dynamic-debug-howto.rst @@ -209,8 +209,9 @@ of the characters:: The flags are:: - p enables the pr_debug() callsite. - _ enables no flags. + p callsite prints to syslog + T callsite issues a dyndbg:* trace-event + _ enables no flags Decorator flags add to the message-prefix, in order: t Include thread ID, or diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c index ee0cb37153ef..016f33c20251 100644 --- a/lib/dynamic_debug.c +++ b/lib/dynamic_debug.c @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -90,6 +91,7 @@ static inline const char *trim_prefix(const char *path) static const struct { unsigned flag:8; char opt_char; } opt_array[] = { { _DPRINTK_FLAGS_PRINTK, 'p' }, + { _DPRINTK_FLAGS_TRACE, 'T' }, { _DPRINTK_FLAGS_INCL_MODNAME, 'm' }, { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' }, { _DPRINTK_FLAGS_INCL_SOURCENAME, 's' }, @@ -858,6 +860,98 @@ static inline char *dynamic_emit_prefix(struct _ddebug *desc, char *buf) return buf; } +/* + * This code is heavily based on __ftrace_trace_stack(). + * + * Allow 4 levels of nesting: normal, softirq, irq, NMI. + */ +#define DYNAMIC_TRACE_NESTING 4 + +struct ddebug_trace_buf { + char buf[256]; +}; + +struct ddebug_trace_bufs { + struct ddebug_trace_buf bufs[DYNAMIC_TRACE_NESTING]; +}; + +static DEFINE_PER_CPU(struct ddebug_trace_bufs, ddebug_trace_bufs); +static DEFINE_PER_CPU(int, ddebug_trace_reserve); + +static void ddebug_trace(const char *fmt, va_list args) +{ + struct ddebug_trace_buf *buf; + int bufidx; + int len; + + preempt_disable_notrace(); + + bufidx = __this_cpu_inc_return(ddebug_trace_reserve) - 1; + + if (WARN_ON_ONCE(bufidx > DYNAMIC_TRACE_NESTING)) + goto out; + + /* For the same reasons as in __ftrace_trace_stack(). */ + barrier(); + + buf = this_cpu_ptr(ddebug_trace_bufs.bufs) + bufidx; + + len = vscnprintf(buf->buf, sizeof(buf->buf), fmt, args); + trace_console(buf->buf, len); + +out: + /* As above. */ + barrier(); + __this_cpu_dec(ddebug_trace_reserve); + preempt_enable_notrace(); +} + +__printf(2, 3) +static void ddebug_printk(unsigned int flags, const char *fmt, ...) +{ + if (flags & _DPRINTK_FLAGS_TRACE) { + va_list args; + + va_start(args, fmt); + /* + * All callers include the KERN_DEBUG prefix to keep the + * vprintk case simple; strip it out for tracing. + */ + ddebug_trace(fmt + strlen(KERN_DEBUG), args); + va_end(args); + } + + if (flags & _DPRINTK_FLAGS_PRINTK) { + va_list args; + + va_start(args, fmt); + vprintk(fmt, args); + va_end(args); + } +} + +__printf(3, 4) +static void ddebug_dev_printk(unsigned int flags, const struct device *dev, + const char *fmt, ...) +{ + + if (flags & _DPRINTK_FLAGS_TRACE) { + va_list args; + + va_start(args, fmt); + ddebug_trace(fmt, args); + va_end(args); + } + + if (flags & _DPRINTK_FLAGS_PRINTK) { + va_list args; + + va_start(args, fmt); + dev_vprintk_emit(LOGLEVEL_DEBUG, dev, fmt, args); + va_end(args); + } +} + void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) { va_list args; @@ -872,16 +966,18 @@ void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) vaf.fmt = fmt; vaf.va = &args; - printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); + ddebug_printk(descriptor->flags, KERN_DEBUG "%s%pV", + dynamic_emit_prefix(descriptor, buf), &vaf); va_end(args); } EXPORT_SYMBOL(__dynamic_pr_debug); void __dynamic_dev_dbg(struct _ddebug *descriptor, - const struct device *dev, const char *fmt, ...) + const struct device *dev, const char *fmt, ...) { struct va_format vaf; + unsigned int flags; va_list args; BUG_ON(!descriptor); @@ -891,16 +987,18 @@ void __dynamic_dev_dbg(struct _ddebug *descriptor, vaf.fmt = fmt; vaf.va = &args; + flags = descriptor->flags; if (!dev) { - printk(KERN_DEBUG "(NULL device *): %pV", &vaf); + ddebug_printk(flags, KERN_DEBUG "(NULL device *): %pV", + &vaf); } else { char buf[PREFIX_SIZE] = ""; - dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV", - dynamic_emit_prefix(descriptor, buf), - dev_driver_string(dev), dev_name(dev), - &vaf); + ddebug_dev_printk(flags, dev, "%s%s %s: %pV", + dynamic_emit_prefix(descriptor, buf), + dev_driver_string(dev), dev_name(dev), + &vaf); } va_end(args); @@ -913,6 +1011,7 @@ void __dynamic_netdev_dbg(struct _ddebug *descriptor, const struct net_device *dev, const char *fmt, ...) { struct va_format vaf; + unsigned int flags; va_list args; BUG_ON(!descriptor); @@ -922,22 +1021,24 @@ void __dynamic_netdev_dbg(struct _ddebug *descriptor, vaf.fmt = fmt; vaf.va = &args; + flags = descriptor->flags; if (dev && dev->dev.parent) { char buf[PREFIX_SIZE] = ""; - dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent, - "%s%s %s %s%s: %pV", - dynamic_emit_prefix(descriptor, buf), - dev_driver_string(dev->dev.parent), - dev_name(dev->dev.parent), - netdev_name(dev), netdev_reg_state(dev), - &vaf); + ddebug_dev_printk(flags, dev->dev.parent, + "%s%s %s %s%s: %pV", + dynamic_emit_prefix(descriptor, buf), + dev_driver_string(dev->dev.parent), + dev_name(dev->dev.parent), + netdev_name(dev), netdev_reg_state(dev), + &vaf); } else if (dev) { - printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev), - netdev_reg_state(dev), &vaf); + ddebug_printk(flags, KERN_DEBUG "%s%s: %pV", + netdev_name(dev), netdev_reg_state(dev), &vaf); } else { - printk(KERN_DEBUG "(NULL net_device): %pV", &vaf); + ddebug_printk(flags, KERN_DEBUG "(NULL net_device): %pV", + &vaf); } va_end(args); @@ -953,26 +1054,29 @@ void __dynamic_ibdev_dbg(struct _ddebug *descriptor, { struct va_format vaf; va_list args; + unsigned int flags; va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; + flags = descriptor->flags; if (ibdev && ibdev->dev.parent) { char buf[PREFIX_SIZE] = ""; - dev_printk_emit(LOGLEVEL_DEBUG, ibdev->dev.parent, - "%s%s %s %s: %pV", - dynamic_emit_prefix(descriptor, buf), - dev_driver_string(ibdev->dev.parent), - dev_name(ibdev->dev.parent), - dev_name(&ibdev->dev), - &vaf); + ddebug_dev_printk(flags, ibdev->dev.parent, + "%s%s %s %s: %pV", + dynamic_emit_prefix(descriptor, buf), + dev_driver_string(ibdev->dev.parent), + dev_name(ibdev->dev.parent), + dev_name(&ibdev->dev), + &vaf); } else if (ibdev) { - printk(KERN_DEBUG "%s: %pV", dev_name(&ibdev->dev), &vaf); + ddebug_printk(flags, KERN_DEBUG "%s: %pV", + dev_name(&ibdev->dev), &vaf); } else { - printk(KERN_DEBUG "(NULL ib_device): %pV", &vaf); + ddebug_printk(flags, KERN_DEBUG "(NULL ip_device): %pV", &vaf); } va_end(args); -- 2.43.0.rc2.451.g8631bc7472-goog