From patchwork Fri Jun 2 10:21:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 102469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp928936vqr; Fri, 2 Jun 2023 03:37:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45tYcJ6wuZz5xgOS7t0Jj1bm8eCiaFqFXfZdJdM6AoWPFv67dwOZUo5GuagVnA+UKFNoRf X-Received: by 2002:a92:c68c:0:b0:32f:80a1:2e44 with SMTP id o12-20020a92c68c000000b0032f80a12e44mr8663980ilg.9.1685702266836; Fri, 02 Jun 2023 03:37:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702266; cv=none; d=google.com; s=arc-20160816; b=VrjSvhNRKSPrDbvlw44ZRIn3s2I7lDsqf88m0nEJwSrhPoz+KODiQeyFpGbJOydOu/ 2hCdbz1emSZ+n/ICN3Hm2fvimi8eLwLZlBGfNokjTj4gtFQIDEUkULwc1hwZ2TJWSwqp HANAqQLy80P6ImN+SQwWFcnQH6S82Ta6EcdBeiPedQpo4vB2D60HvXerIqX3Xuv8ABZZ LOKils5LI2JB7pJYXt3mc4jzJwL5HZU1luKf1MCz8vzs/8pnbaFbO1AbZZLEsLy0jYbZ YT0bzyqXIHQTnuY4GgLhwkyGd5TB2hksXEmyWH/6fT1Nha1zy4eHlZlFOLgQ6KQATNON CLIA== 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=I8S9X+fFve3OGFy12TY/AcZ347NDmRxBR7waMwLcuO8=; b=owHR2hHk8jYstZQeKcpsbTrV06AwoJbtEr0/qi/gKj4LqoRfiE1OZUnvD5kIHF71yI HJCHQwtS/hV1f1ZyEr+uY2bIhyCHyJl3qx10JRf+6m/pn5xdgJYOvQBH05oUvdFjlkOY S3VU6XIQXiCDe9BG0RQaVsFba2jR/cB6ne4Yyi/eGdf7TF1+aS4nyFzr3NsKVr69VQfx eDM1GJb9IfHLFeLxps8qFSMVol8MfEMexRp3sUZ4POXbpLC4X0aWOY0b3aYd98M5mKVt tf5HLA+oQmzZY09gcshWphLgC6hX06x/efOvH3FxNdiEmcM9NUtToxdtwmQiSC9xJiIf gQ9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Y9ZskJIm; 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 bs4-20020a632804000000b0053f3d04e671si761558pgb.691.2023.06.02.03.37.32; Fri, 02 Jun 2023 03:37:46 -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=Y9ZskJIm; 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 S234955AbjFBKWn (ORCPT + 99 others); Fri, 2 Jun 2023 06:22:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235347AbjFBKWN (ORCPT ); Fri, 2 Jun 2023 06:22:13 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24AD6E5C; Fri, 2 Jun 2023 03:22:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685701326; x=1717237326; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=V829dPDXLt7WqeU+6inlDIEaA7ebMjnZBGRC6GePQ5U=; b=Y9ZskJImOqc8HAHxSSauJw2Dl+A+GiRMMT79HRpWGxaNhkU6S5Xk3h8P X1EIlSeK9sxqRBrSRGUEFSFrkeHnnXJoYQJqq2tB2duuuVvK9LGtbLVsz 8sIWeR/Q1RAdKp6cdz+PNGgiOw/6xh/fO/6i3uv83591sz1aNKZ5WSxqC XHb6coHI7xCLn/IZ+rYCllVs0rjA1PzcG3kZxp4T4Ke71HP/gVBrXR8Uh Ea/MXsKBgqwz/PTwhE8u4ESwtHfUhQeRqZaLx/rFAbGrv8kA4O0cbDdOc IxGcgrIcZ1tW8aMndaB7A8UR2rXD1XklqqhOC2AKSJdY6ycSisCHXKTNu Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="358267631" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="358267631" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2023 03:22:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="707804984" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="707804984" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2023 03:22:02 -0700 From: Andrzej Hajda Date: Fri, 02 Jun 2023 12:21:35 +0200 Subject: [PATCH v9 3/4] lib/ref_tracker: add printing to memory buffer MIME-Version: 1.0 Message-Id: <20230224-track_gt-v9-3-5b47a33f55d1@intel.com> References: <20230224-track_gt-v9-0-5b47a33f55d1@intel.com> In-Reply-To: <20230224-track_gt-v9-0-5b47a33f55d1@intel.com> To: Eric Dumazet , Jakub Kicinski , "David S. Miller" Cc: Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Chris Wilson , netdev@vger.kernel.org, Dmitry Vyukov , Andi Shyti , Andrzej Hajda X-Mailer: b4 0.12.2 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,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?1767586939867090329?= X-GMAIL-MSGID: =?utf-8?q?1767586939867090329?= 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 Reviewed-by: Eric Dumazet --- 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;