Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6171983rwr; Mon, 24 Apr 2023 15:08:55 -0700 (PDT) X-Google-Smtp-Source: AKy350YMeBCg1m3nc+ePzom/CGyyz61H6AywkruQdybuGC1XFYFzy03KodtMVyvkZAgIOtobdv4L X-Received: by 2002:a05:6a20:938b:b0:f4:d4a8:9c82 with SMTP id x11-20020a056a20938b00b000f4d4a89c82mr6832108pzh.47.1682374134746; Mon, 24 Apr 2023 15:08:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682374134; cv=none; d=google.com; s=arc-20160816; b=iAQyj0yhe55Aa726sTsIZmFEFXhdsMdADE0qy5B3tpGWBeRUp3VyP8q7y6fly5V5Fd X1nuousn3RxEZV8hCPZoHIv5D1gP1xQwamtMfcoxfKcmW+GVPh1LOpegTpddxLlIn4Ol 5zipXbCnaX0S09FHGm50epjNTk/u8Le3wsocVOIcmyLSba/cgpttqk7/f0u0wFYlDyaS zl3F8NG9CWjcIqQ/mtmI1vCxfOwfMpKVjQcjCbkmApHjuSBChvpAEao+YH6jQbWaeBmj FRwdG/dt5Zv+E8ypzOrZOpqOdwHjCh4uoIxcneo/wkr/LQaPes3AFMm9Ht2G2TWn0vv8 jQfw== 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=o7tiIpebJiXe4njC8w4mIVPKQtBgyOMYFWDJ2GOFnFE=; b=mofddSMH3AF4/1P26RY4FuNgrpir/o2DlmsaLlzeGDZRucv2cixElml1s8JHEbGhed mUHVhQ1u1fnnfnknabICRo36LBXbwC5/fmOav2b9qZrl6u3dQ73hQG2MqfCw4vAwrmmz gBP/kGoFz2JO2LgxZVXNMidI0d28PFhPCSgVTzZwNeJ7UQlvQkYb1lBHK+Z1V5qydbu4 pXkJ0QGalw4vTUhVg6in/0kUvvnROCWcGmRS49VOdiHmt2twx7MBd3dkoNJv6zyctuDZ +bKAt7Vsd0G64cM428ltUctw9Y72oM/2ULQrt4T/DVggF57eIxJ6MGgV8lKQ2aFV3gwF eoiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MMSDwdWY; 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 w190-20020a6382c7000000b0050fb7c5a656si12142701pgd.190.2023.04.24.15.08.34; Mon, 24 Apr 2023 15:08:54 -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=MMSDwdWY; 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 S233183AbjDXWGT (ORCPT + 99 others); Mon, 24 Apr 2023 18:06:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233162AbjDXWGF (ORCPT ); Mon, 24 Apr 2023 18:06:05 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3A9E8A74; Mon, 24 Apr 2023 15:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682373963; x=1713909963; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=9543xZcCMZfDtzLHnM5dTa2W9tWhV6LjC/60obdR+TE=; b=MMSDwdWYPY0afh/6S5wrIv9uUi7xfMhfUdt+U0XR4u1f3l2vS6QGHWC0 6Kw1qhCdT0STSr8jkGgrD60v6zpNEKHAgbE1rycGtn4ibrbLf30noms1D lTerNV2IIExiTAT+wLGuX/mkAjpSX7WBJjcxuoPoifOCOvVafz0cjRC8/ Cxb6DKw+ad6us2jeBFHYECDLoCXHKXlL6Ftv34TENs4nAgUrPfLj1YHTe QiFJSUTfgnDRFE70O23HS3/pzumVIoJnx+4muigYA5nG+RAY1N6FlSOnP 8HOQTdzFWQ0YQxSjO/XezIDW04U5YmzvYSODUrd/b0UkbyPzbNc9iV41N Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10690"; a="335473696" X-IronPort-AV: E=Sophos;i="5.99,223,1677571200"; d="scan'208";a="335473696" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2023 15:06:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10690"; a="939500186" X-IronPort-AV: E=Sophos;i="5.99,223,1677571200"; d="scan'208";a="939500186" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2023 15:05:59 -0700 From: Andrzej Hajda Date: Tue, 25 Apr 2023 00:05:40 +0200 Subject: [PATCH v8 3/7] 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-v8-3-4b6517e61be6@intel.com> References: <20230224-track_gt-v8-0-4b6517e61be6@intel.com> In-Reply-To: <20230224-track_gt-v8-0-4b6517e61be6@intel.com> To: Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Daniel Vetter , Eric Dumazet Cc: linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Chris Wilson , netdev@vger.kernel.org, Jakub Kicinski , Dmitry Vyukov , "David S. Miller" , Andi Shyti , Andrzej Hajda X-Mailer: b4 0.11.1 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,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, T_SCC_BODY_TEXT_LINE 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 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 19a69e7809d6c1..8eac4f3d52547c 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -46,6 +46,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); @@ -74,6 +76,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