From patchwork Fri Apr 21 11:35:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 86255 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1002791vqo; Fri, 21 Apr 2023 04:46:45 -0700 (PDT) X-Google-Smtp-Source: AKy350Y3IwuMnqJrdQmCg/gJBY1iyl2oduS09p0WJj3aZoI4rOK4z5uBSMpx0xXTkpwWFYsQ6OTY X-Received: by 2002:a17:90a:e7cf:b0:246:f710:4f01 with SMTP id kb15-20020a17090ae7cf00b00246f7104f01mr4583672pjb.35.1682077604867; Fri, 21 Apr 2023 04:46:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682077604; cv=none; d=google.com; s=arc-20160816; b=FA8kbf6VjHYLe9j+Vq2pA5/evCjUuHx5BePWNx7zelo0p+xlhCoGB74ESpgBLH5c3F HSolhP3yh3tTqwwWFAWpdqu1rRUvM2R4Y+iKnfVgDrsFelDGk2SYCrcb+phbLJXtEXLB MR9Cgk9kwj8NFXQsm4eG1C0NEPNfuw6svCjzjJR2u2rrkU5DGuzIZkO3hFmiB1oJkyBL XkAhHUGdm4XxOjkjk6wsgoTBgo9Q8HjMKwnUjqcHJ+czfrnvqULSK8O15tZBMxoqFsV7 I6y66T6qXBLj/UmFt8geUWFXAMAcCxVoZFY3Ifvqiy/QzfWZMve2VcaZ+/6RD6kIaAVw mHFA== 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=fmUnYqd/UEtsPHliQqaJtvPiGp4sFoU7X1EhlAA2G0c=; b=0Y5QDhMSdYFb/O/1uh9VK8thbXQt//VMYOVp+zivmCV8josOVP5d2pjR0ycuuS7bKJ WgjkfubUKnj8yrpNbYdkVqPVFIvJXJsVoSiuyg3ADqgs04401NFkLCup2XiHYM4XfYfa MEMrytw9PvUf1CUMMV51biqu2sz6eOCerMr+bSBl5tAmeIgaR/Ud2MYlRmrRspML3KsN IEMfYtWdZINyKOIEK2wCB566vFqc6jMb6dgBpuiKjzsrxeNd22WFjlf3wkb2+tkxnQW7 56KQVbYqHkN51GiSby5lXQMDfAKMPsOzHPKJ1F/tSeN2qQ/c6nDzpzYfYuj+GHldIas4 yD8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UN5ISHjP; 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 lr10-20020a17090b4b8a00b00246ba136cc1si4789164pjb.137.2023.04.21.04.46.29; Fri, 21 Apr 2023 04:46:44 -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=UN5ISHjP; 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 S231794AbjDULfp (ORCPT + 99 others); Fri, 21 Apr 2023 07:35:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231689AbjDULfa (ORCPT ); Fri, 21 Apr 2023 07:35:30 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30291BBB8; Fri, 21 Apr 2023 04:35:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682076929; x=1713612929; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=M1WvzpZ5kIn6LF/S+fHg9iMRDVGNcxSRHktOcS2+YUM=; b=UN5ISHjP6aR8cDxTY5/VPLheDLbK+SHSSltrWMYOPfjBnq+536yVI4Vd EUsD5Dt6fuTSi1DaKZzNYZNxUplStpQbWMSDwmkz920bQutYno2ZbX5Bl HasR/u+iD8+Y3OZ1WaAAvIHkgmTi9NUSPdkX+W83wl5t09eJz4rcU6jJG 4ZkUaue4vHIXfEKvHIN2PG9+b+efZN8Ui0FNV1GZtxDRpqVVeJn6TkBY6 PmOBnq7oN3snidSwwg9uUBbHkwI2ehZmMLel/tuqr9T6PizbkdKK0qnZn 7CvHMCQLdvDISfPkV9uVOUe6I6nyFDHAMPN/LPHRHNHE2v4In5pPQcnOe g==; X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="432249885" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="432249885" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 04:35:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="642489659" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="642489659" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 04:35:25 -0700 From: Andrzej Hajda Date: Fri, 21 Apr 2023 13:35:06 +0200 Subject: [PATCH v7 3/7] lib/ref_tracker: add printing to memory buffer MIME-Version: 1.0 Message-Id: <20230224-track_gt-v7-3-11f08358c1ec@intel.com> References: <20230224-track_gt-v7-0-11f08358c1ec@intel.com> In-Reply-To: <20230224-track_gt-v7-0-11f08358c1ec@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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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?1763786206207443531?= X-GMAIL-MSGID: =?utf-8?q?1763786206207443531?= 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 Reviewed-by: Andi Shyti --- 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;