Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp226840rwl; Wed, 29 Mar 2023 00:27:42 -0700 (PDT) X-Google-Smtp-Source: AKy350blPyTmMIrQjlXiwhF/qAuud91TI5WfyqNY8YcchxyOxHFqNaJxSsTV5D8LAqdQevb/Jybf X-Received: by 2002:a17:906:148d:b0:8b1:7e23:5041 with SMTP id x13-20020a170906148d00b008b17e235041mr17798382ejc.39.1680074862147; Wed, 29 Mar 2023 00:27:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680074862; cv=none; d=google.com; s=arc-20160816; b=z2HVlpMAVBsPs2PkIcq4RlTUAACBWxPHHAp6WJQvRvN/AzeV0FweaOk/DzNHqcnwdo ktT3I+AMwHnpP8+Rtrp6LVGosJZsNO0HMWxGdmKLDT7QwOga/7+lN4VcMC1sR9WzNyGi 5DfvcpWFOLc/FqmjmfGF258Z94kb2ZZKNc5kZq9m1dyI9ZQeEsp6xvXcHuRGyYMgm106 sjiLtmCU7uc+XFeDpDVdNGdJhZSbWrMMPr3ajTcfdgPFrXkMXAi/nmD5qaZCrhryxfMz eUnLthredCbNQJIUY5WvFk5X6wJcTqpJ2+lZT84K//bYtL1F33Q4l8Dp+yQXFDpcgTVU Q2cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=eK06zgGmn0TfapCBH9CX2D8DI2CSafJVUHaDOBdWDXY=; b=qcoRhD2fq1Cbpfcg8S3QV4eFJKj9BMTw9pf+bSEwLbqsPZC6yQ46wKQ0M3It9pPqKG xkowua3FHla2W2ZQ+296l3p5155T+FEg8Z8YI9hjKdsopPwBe7vqh1DZi2wk+NDAsQYp ypXDfRjPEAqG9qBR3yi93iAfDTXAjvXvya0sc6qKioJPNqO0CFLhRjy20rxHPsAOsCV6 48KHV0+XdzyrJ5z/GKnpTYRTg/1YGSa3ROjmyTwL8H6HqXQsQIwrt+/0bmzhXx+TAKZY AD/gXA/Ggq93XbASdq9wlfuQ+LRtU2/xUBuW11AUobanLrwr4eJveG1Sni/3Y5KIIVXT OVVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DWixSVHr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne29-20020a1709077b9d00b0093295ea18c2si40013592ejc.392.2023.03.29.00.27.13; Wed, 29 Mar 2023 00:27:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DWixSVHr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230130AbjC2H02 (ORCPT + 99 others); Wed, 29 Mar 2023 03:26:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230107AbjC2HZy (ORCPT ); Wed, 29 Mar 2023 03:25:54 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7C124237; Wed, 29 Mar 2023 00:25:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680074710; x=1711610710; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=SjGS/NBEg10FgFcG60Eo0L1khFE2QrsMt8Dx57P3heg=; b=DWixSVHr5R58Hm+pe7shAwq86AVHNptzdu1QUOS4ANzZIbDpelQwLFMJ tCDiGiWCXO8q+UavYXDqEhDE3Vo7OCS1vKbeCqTA/MgjPPG3dqc2ZmMaB BGoQ5T59cVCw9G+FlaUk9qod7U7M5cUM/SV//jOduyu3hOrHXJW7ZstAE vSniH2GJe2n2Bh8SV0JFYnwS5Wg6kUk3Ne5/Kb/16yUqC2MZ5MY1Hqh84 SJFgNXcmWufapSDNMRfsj6Rwgh4BmyLNOYHYsQqd32e0jfLB7IzbdktX0 LJlxpP00tMVerZ3dvQ5p8JNX5jmtdYYtglgbvEZ+WCdBzBx0L4EF5Lwye A==; X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="368569689" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="368569689" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2023 00:24:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="827772817" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="827772817" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2023 00:24:41 -0700 From: Andrzej Hajda Date: Wed, 29 Mar 2023 09:24:14 +0200 Subject: [PATCH v6 3/8] lib/ref_tracker: add printing to memory buffer MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-track_gt-v6-3-0dc8601fd02f@intel.com> References: <20230224-track_gt-v6-0-0dc8601fd02f@intel.com> In-Reply-To: <20230224-track_gt-v6-0-0dc8601fd02f@intel.com> To: Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Daniel Vetter Cc: linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Chris Wilson , netdev@vger.kernel.org, Eric Dumazet , Jakub Kicinski , Dmitry Vyukov , "David S. Miller" , Andi Shyti , Andrzej Hajda X-Mailer: b4 0.11.1 X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similar to stack_(depot|trace)_snprint the patch adds helper to printing stats to memory buffer. It will be helpful in case of debugfs. Signed-off-by: Andrzej Hajda --- include/linux/ref_tracker.h | 8 +++++++ lib/ref_tracker.c | 56 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h index fc9ef9952f01fd..4cd260efc4023f 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -50,6 +50,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, void ref_tracker_dir_print(struct ref_tracker_dir *dir, unsigned int display_limit); +int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size); + int ref_tracker_alloc(struct ref_tracker_dir *dir, struct ref_tracker **trackerp, gfp_t gfp); @@ -78,6 +80,12 @@ static inline void ref_tracker_dir_print(struct ref_tracker_dir *dir, { } +static inline int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, + char *buf, size_t size) +{ + return 0; +} + static inline int ref_tracker_alloc(struct ref_tracker_dir *dir, struct ref_tracker **trackerp, gfp_t gfp) diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c index 2ffe79c90c1771..cce4614b07940f 100644 --- a/lib/ref_tracker.c +++ b/lib/ref_tracker.c @@ -62,8 +62,27 @@ ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit) return stats; } -void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, - unsigned int display_limit) +struct ostream { + char *buf; + int size, used; +}; + +#define pr_ostream(stream, fmt, args...) \ +({ \ + struct ostream *_s = (stream); \ +\ + if (!_s->buf) { \ + pr_err(fmt, ##args); \ + } else { \ + int ret, len = _s->size - _s->used; \ + ret = snprintf(_s->buf + _s->used, len, pr_fmt(fmt), ##args); \ + _s->used += min(ret, len); \ + } \ +}) + +static void +__ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir, + unsigned int display_limit, struct ostream *s) { struct ref_tracker_dir_stats *stats; unsigned int i = 0, skipped; @@ -77,8 +96,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, stats = ref_tracker_get_stats(dir, display_limit); if (IS_ERR(stats)) { - pr_err("%s@%pK: couldn't get stats, error %pe\n", - dir->name, dir, stats); + pr_ostream(s, "%s@%pK: couldn't get stats, error %pe\n", + dir->name, dir, stats); return; } @@ -88,19 +107,27 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, stack = stats->stacks[i].stack_handle; if (sbuf && !stack_depot_snprint(stack, sbuf, STACK_BUF_SIZE, 4)) sbuf[0] = 0; - pr_err("%s@%pK has %d/%d users at\n%s\n", dir->name, dir, - stats->stacks[i].count, stats->total, sbuf); + pr_ostream(s, "%s@%pK has %d/%d users at\n%s\n", dir->name, dir, + stats->stacks[i].count, stats->total, sbuf); skipped -= stats->stacks[i].count; } if (skipped) - pr_err("%s@%pK skipped reports about %d/%d users.\n", - dir->name, dir, skipped, stats->total); + pr_ostream(s, "%s@%pK skipped reports about %d/%d users.\n", + dir->name, dir, skipped, stats->total); kfree(sbuf); kfree(stats); } + +void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, + unsigned int display_limit) +{ + struct ostream os = {}; + + __ref_tracker_dir_pr_ostream(dir, display_limit, &os); +} EXPORT_SYMBOL(ref_tracker_dir_print_locked); void ref_tracker_dir_print(struct ref_tracker_dir *dir, @@ -114,6 +141,19 @@ void ref_tracker_dir_print(struct ref_tracker_dir *dir, } EXPORT_SYMBOL(ref_tracker_dir_print); +int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size) +{ + struct ostream os = { .buf = buf, .size = size }; + unsigned long flags; + + spin_lock_irqsave(&dir->lock, flags); + __ref_tracker_dir_pr_ostream(dir, 16, &os); + spin_unlock_irqrestore(&dir->lock, flags); + + return os.used; +} +EXPORT_SYMBOL(ref_tracker_dir_snprint); + void ref_tracker_dir_exit(struct ref_tracker_dir *dir) { struct ref_tracker *tracker, *n; -- 2.34.1