Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4238707pxb; Mon, 21 Feb 2022 15:41:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwdjNw1NaB5fsseeXkM3o9M4VLmyjA1QrbWaBwkZDtuYDLjfqiR22JfaSbXfvvoiChqjwUI X-Received: by 2002:a50:9dcf:0:b0:410:80df:d864 with SMTP id l15-20020a509dcf000000b0041080dfd864mr23772284edk.270.1645486883300; Mon, 21 Feb 2022 15:41:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645486883; cv=none; d=google.com; s=arc-20160816; b=NOn89gnde70RmDMRuHF1hPCbvEZCkDjp91XqobI+A7vjtgGFkkxJzOndntjqla/0GC CiksfJjELMDZSPVizSRE3G563syq/aQSla7HfVDsZKQ+QKa3kfvPfxYgixPmTkbCFHDr 1jeEbfHwyMybTwgUCNfDNXI32kCoG1bL6SAsry7ZinjZeTlZrWTtCVgCVt2avzewWc7n jqhH9/4naaTAmFXG92zErMMIvzwcazTYhTCf03Etzl+56N8UDF4u2FWz0zSUjDayRrLj I4qTcvg+71sJE0qWXyHri9Hu0hy71cZ/+pBdGJWuw1l96RMVk1mwW9RyS0Ztrorxfanz nlxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bHAhmkxnVM6qoxznh4Tt+7zTwwKIvmrZ6+9nbPYxkMA=; b=OxI5UCSSYC5hyGcvkgIkEwY92BFHEeOU17W3w3owqGmlhq2xGnSlA+x3CC2BmWrRCb z1qqYm+O4mCtI0f3ou1fcs/0Y7F5qgJFlm6FMqzKypemROVdczE4WWYNr30oGk+w7g4h gmHRQGh2Nj+7FGcbZH4sM4iD1JRzxeCuX3NSbW4lkVISPC036xVF3o4n26C8YEGFxn6o 7qL/csuJ/fl5vE8LmWOZgXDu3QEDZXORQALayKc9wWbPKLJveh9530aOP1Q/B8aSbWB9 Z8QfsMkjHhPvSHjUwG8x4eenMoY08eSdt83WqpfHTdaGS92Ro8okenFgKXUPT+56t/6B VvBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mEgn5RAb; 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 f22si8759757eja.616.2022.02.21.15.41.01; Mon, 21 Feb 2022 15:41:23 -0800 (PST) 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=mEgn5RAb; 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 S236852AbiBUX0x (ORCPT + 99 others); Mon, 21 Feb 2022 18:26:53 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:40808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236767AbiBUX0i (ORCPT ); Mon, 21 Feb 2022 18:26:38 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32B7424F1F; Mon, 21 Feb 2022 15:26:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645485974; x=1677021974; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yBcBvjJ/VepSDsCpwUwjixNuXeR4oixpM4h2duuH8BA=; b=mEgn5RAbU1BhEtElJQZndjMDXJiCZMfitSUyHvqWolN3w7OLSUwuQ2Lf GBLOE/+o/tKAGzL5nVZlvwYNF+tz6/QPu0EktnsIQCiDBONykpP1tLzCB 39Xcviwvy2tRBofY/LQfb6oDeUcupHHrqzUTXz+I6pCSB5BZH5iyZ6Rd3 OcV9GauKnVs//NUpsU9Ey7iwoq5aNedXKf4jCi3IL7RWLNdC7CAqXR+TA nQqo8fXxVIA63OJl0sK9Nm0lgbHNjYURD89WLp984zyPtVQvF4sRCGEi2 BhtgmnNTaDM2urCgk+fyarx+XUNm9Y/eFoGrIgWix9iagxliglQU63PEY w==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="338011900" X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="338011900" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:26:13 -0800 X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="706397026" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:26:11 -0800 From: Andrzej Hajda To: linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, netdev Cc: Andrzej Hajda , Jani Nikula , Daniel Vetter , Lucas De Marchi , Chris Wilson , Eric Dumazet , Dmitry Vyukov , Jakub Kicinski Subject: [PATCH v3 06/11] lib/ref_tracker: add printing to memory buffer Date: Tue, 22 Feb 2022 00:25:37 +0100 Message-Id: <20220221232542.1481315-7-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220221232542.1481315-1-andrzej.hajda@intel.com> References: <20220221232542.1481315-1-andrzej.hajda@intel.com> MIME-Version: 1.0 Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 Content-Transfer-Encoding: 8bit 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 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 In case one wants to show stats via debugfs. Signed-off-by: Andrzej Hajda Reviewed-by: Chris Wilson --- 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 a2cf1f6309adb..2fdbfd2e14797 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -50,6 +50,8 @@ void __ref_tracker_dir_print(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 ab1253fde244e..2ef4596b6b36f 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(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(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(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(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); 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.25.1