From patchwork Tue Mar 28 15:15:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 76130 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2304206vqo; Tue, 28 Mar 2023 08:25:57 -0700 (PDT) X-Google-Smtp-Source: AKy350bCM702JR3lpFuXwYEGoHwIG3vJkhve5R3RJjny7a4jYj73Mp61IRc5Q8xEsCrSJWNLDcqf X-Received: by 2002:a05:6402:8d6:b0:502:2953:8ecb with SMTP id d22-20020a05640208d600b0050229538ecbmr12957578edz.13.1680017157693; Tue, 28 Mar 2023 08:25:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680017157; cv=none; d=google.com; s=arc-20160816; b=b+vxblQ+Hf3pITrOCRNlEmgCw3+KNxIZA0VTjZu36FT/KciFdGnco+hUoGar6cPDUx 1qdFeDBs3scxPf1/dqfmIt3RIl3g35glO7p2kXUZtKYKQXvhe/HnlDhPw4cQAO3PE6Up prkR/AXqgQiA1mMjmjo9iUkbvD8zA/273gBCHyDeJx/j3X0z2HWxHOTHPCWUB4nVIW8f ddDmZlvBebAujbKd1xW7D1wThz9edOAoS5z1sEuwD8Gs3izWjTKXUq2mP0J2ZmlXarbn ypU8EUZ1t+KRtK6/aLKpBiIBGy9nTaeaI1gayesUI7IUbPw2GPjOLxyPDQafpNnN7nWA xR1A== 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=n/Qht3OToabEtn5Gyiepg+6OWz4VR0nVuH5d7t4Nm0LCUsl5HFKT5PoDel1LOno9wb 6E/DbYuQ5HZlLGig5hY0kuh+9zL2cnRqZ0U5c15tI+z6QQNN7BuXmiGUlUPsoltbe2fO K2pHsnVaZLDWJ/R+Xr/tAy0ln4P8G8BwiUzMMmWto/WEXumJV+bvysX6FGiOdUU3wIDw LudsjaEwnep2iEbx3L0ZuL6Ti6ouDstXGhtOJBF2NKZnPwSzzLZOzLRrucMJIQ17TZaO /kDtdpza+4gVse3yoA1h7TlrEqc27XnJ1OvZ/0xazT3uXr0MrlIHrkwFJqJqfWtvZvUZ Ms/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="bl/G00C9"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r8-20020aa7d148000000b004c12b7f5a48si28452892edo.177.2023.03.28.08.25.34; Tue, 28 Mar 2023 08:25:57 -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="bl/G00C9"; 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 S233985AbjC1PSO (ORCPT + 99 others); Tue, 28 Mar 2023 11:18:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233960AbjC1PRn (ORCPT ); Tue, 28 Mar 2023 11:17:43 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76445E3AC; Tue, 28 Mar 2023 08:16:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680016609; x=1711552609; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=SjGS/NBEg10FgFcG60Eo0L1khFE2QrsMt8Dx57P3heg=; b=bl/G00C9LR4yezFHAidwNd4VSSQAW4o55g1DZb1DJWiTazQWYvjvzC/O bcRf5GvhfLzG7BAyT7DqeVyqNH+NbAaUFYl5eAEl1LeDeTxdU9BMZf4ee WguLCdzYJOFUw4pugPjby3erZO2Tmkmxv3HTerTYPSh6KKLCXoxj0b2ji gdQR5OAdFHiIwxm4x+Xedznk4MzWiNYWF5UzI7eTL2wEY+YrG23KD9An8 zgW+VAfip8OF9lEeZUobQv/nBSRiBOqUQn+Tcg2QvP1xT++Y5cRmGju/y pFFGhjYvPM6Xo4hUu+V5lM890sNY+YnMKUE12fbmsW+1ZVE7lf/UdDl4w w==; X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="403208667" X-IronPort-AV: E=Sophos;i="5.98,297,1673942400"; d="scan'208";a="403208667" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2023 08:15:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="773181761" X-IronPort-AV: E=Sophos;i="5.98,297,1673942400"; d="scan'208";a="773181761" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2023 08:15:53 -0700 From: Andrzej Hajda Date: Tue, 28 Mar 2023 17:15:26 +0200 Subject: [PATCH v5 3/8] lib/ref_tracker: add printing to memory buffer MIME-Version: 1.0 Message-Id: <20230224-track_gt-v5-3-77be86f2c872@intel.com> References: <20230224-track_gt-v5-0-77be86f2c872@intel.com> In-Reply-To: <20230224-track_gt-v5-0-77be86f2c872@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" , Andrzej Hajda , Andi Shyti 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,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761625671125322503?= X-GMAIL-MSGID: =?utf-8?q?1761625671125322503?= 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;