From patchwork Wed Mar 29 07:24:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 76403 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp229863vqo; Wed, 29 Mar 2023 00:38:49 -0700 (PDT) X-Google-Smtp-Source: AKy350abVi+brXEmZzgOetXv8jEffnuyHUoExWLNewlyolh3w9QqkYX6LEEraWsRMZwO3oq7O5+X X-Received: by 2002:a17:906:5acf:b0:8b1:7684:dfab with SMTP id x15-20020a1709065acf00b008b17684dfabmr22335880ejs.38.1680075529636; Wed, 29 Mar 2023 00:38:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680075529; cv=none; d=google.com; s=arc-20160816; b=KOqVGXB9kDdDukNpPgccVboeIcjsDTFsvqvBsK60Wq05UEFixv8Ptpo/4vSAbir2BC bgHTNxgHAV84lM9Qb3r8W/GAsjvyLmdOAyi6Lb0V/J7bHMouTHVHw4kEBNc1Fj/vD0CB HnRBPmKeufBEiHue+JwSyDc7oc9zon72LUWncxhLTDSNxlX1SwHQ1/ZNlvq9HgUbAzeg viGQY8TNwnCoLd4gV71z8isi/sfaNDigeeBWH2p24/hSVNOIUyAJK+LEP+Fx490uq3On TrgxLCZPYc5GrJb7+jfFuxsv4sCoAUxYFdC5y1ept32Nq3bf5jI/DCuYBssuewRVzz9r jGSQ== 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=D4VN+C0U1SR/M5OZWurjUwAETocz3Ge/YYACuN2pNzU=; b=TrqXVo2iZh/NJwatVEM81aISTLyxCYTl+BtcCocQYMnkepaI1cLJBqudOdmgrTxOu0 CzjS5wYlUcGJZcMg5zVt/VQ/5hNZ4g5uDg8Q31Zpoh6YIG/uH38dROXEuNOBZAf0dnTD e+ljkVtKpzHFx3Nkqy0HpoZo3tNsGKYpJgY3bNsQYrc28a2Tr48F8+hrC1++xN0+m67w FIgl4K3rjitRwHdp2Sf23BmD5ZEsrKqgdy3EqM0aHooUc3fhhSSVpFZSHEzi++lJC59a cBnH6Zxd9wudNIPX8vzv/iMsR4biVSnUjRvinrH+i1ugpSvYCopvc6K7ZqVISbYLpieA rqsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X7A5uMCU; 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 he19-20020a1709073d9300b00938de018211si31030773ejc.831.2023.03.29.00.38.26; Wed, 29 Mar 2023 00:38:49 -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=X7A5uMCU; 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 S229972AbjC2H0E (ORCPT + 99 others); Wed, 29 Mar 2023 03:26:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbjC2HZR (ORCPT ); Wed, 29 Mar 2023 03:25:17 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20102422C; Wed, 29 Mar 2023 00:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680074701; x=1711610701; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=kEFDoIdEQUaoXtYqPzYKibOwd36nmghIfH3/F6UGSm4=; b=X7A5uMCUJslzDjrZrWUHW16+W+gR7JuI1gl62AYEDsxcnM1R0yavbSyb ZJ8v+qY1sSQLLFSX26gCPOgvPqJ3TwbFWu07Ndj4JkNLa9XF6XfVGNkfX y4axITmTYOpEBC8jMmtgX26Io47Kq+VRmWUPexCqgfNzZd1OK/R17hHBL 84uxAlPS5+gSkmoAlLs6yTbtxNkF1wOAMc2PjPSlJIj1KQYObPjoU/dQY IfMazKn1nJ8FFs8Dr2521lDMkN48DiRug7Dh4UBrOCcLHSIO8l4jHMtv0 HHUBcc6p8xUHdlt0837b5c5taQVvj0mJ8qWUY5GixMr8Fv0qZtU8EEPhJ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="368569620" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="368569620" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2023 00:24:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="827772785" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="827772785" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2023 00:24:38 -0700 From: Andrzej Hajda Date: Wed, 29 Mar 2023 09:24:13 +0200 Subject: [PATCH v6 2/8] lib/ref_tracker: improve printing stats MIME-Version: 1.0 Message-Id: <20230224-track_gt-v6-2-0dc8601fd02f@intel.com> References: <20230224-track_gt-v6-0-0dc8601fd02f@intel.com> In-Reply-To: <20230224-track_gt-v6-0-0dc8601fd02f@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=-2.5 required=5.0 tests=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 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?1761686878502054183?= X-GMAIL-MSGID: =?utf-8?q?1761686878502054183?= In case the library is tracking busy subsystem, simply printing stack for every active reference will spam log with long, hard to read, redundant stack traces. To improve readabilty following changes have been made: - reports are printed per stack_handle - log is more compact, - added display name for ref_tracker_dir - it will differentiate multiple subsystems, - stack trace is printed indented, in the same printk call, - info about dropped references is printed as well. Signed-off-by: Andrzej Hajda Reviewed-by: Andi Shyti --- include/linux/ref_tracker.h | 15 ++++++-- lib/ref_tracker.c | 90 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h index 87a92f2bec1b88..fc9ef9952f01fd 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -17,12 +17,19 @@ struct ref_tracker_dir { bool dead; struct list_head list; /* List of active trackers */ struct list_head quarantine; /* List of dead trackers */ + char name[32]; #endif }; #ifdef CONFIG_REF_TRACKER -static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, - unsigned int quarantine_count) + +/* Temporary allow two and three arguments, until consumers are converted */ +#define ref_tracker_dir_init(_d, _q, args...) _ref_tracker_dir_init(_d, _q, ##args, #_d) +#define _ref_tracker_dir_init(_d, _q, _n, ...) __ref_tracker_dir_init(_d, _q, _n) + +static inline void __ref_tracker_dir_init(struct ref_tracker_dir *dir, + unsigned int quarantine_count, + const char *name) { INIT_LIST_HEAD(&dir->list); INIT_LIST_HEAD(&dir->quarantine); @@ -31,6 +38,7 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, dir->dead = false; refcount_set(&dir->untracked, 1); refcount_set(&dir->no_tracker, 1); + strlcpy(dir->name, name, sizeof(dir->name)); stack_depot_init(); } @@ -51,7 +59,8 @@ int ref_tracker_free(struct ref_tracker_dir *dir, #else /* CONFIG_REF_TRACKER */ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, - unsigned int quarantine_count) + unsigned int quarantine_count, + ...) { } diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c index d4eb0929af8f96..2ffe79c90c1771 100644 --- a/lib/ref_tracker.c +++ b/lib/ref_tracker.c @@ -1,11 +1,16 @@ // SPDX-License-Identifier: GPL-2.0-or-later + +#define pr_fmt(fmt) "ref_tracker: " fmt + #include +#include #include #include #include #include #define REF_TRACKER_STACK_ENTRIES 16 +#define STACK_BUF_SIZE 1024 struct ref_tracker { struct list_head head; /* anchor into dir->list or dir->quarantine */ @@ -14,24 +19,87 @@ struct ref_tracker { depot_stack_handle_t free_stack_handle; }; -void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, - unsigned int display_limit) +struct ref_tracker_dir_stats { + int total; + int count; + struct { + depot_stack_handle_t stack_handle; + unsigned int count; + } stacks[]; +}; + +static struct ref_tracker_dir_stats * +ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit) { + struct ref_tracker_dir_stats *stats; struct ref_tracker *tracker; - unsigned int i = 0; - lockdep_assert_held(&dir->lock); + stats = kmalloc(struct_size(stats, stacks, limit), + GFP_NOWAIT | __GFP_NOWARN); + if (!stats) + return ERR_PTR(-ENOMEM); + stats->total = 0; + stats->count = 0; list_for_each_entry(tracker, &dir->list, head) { - if (i < display_limit) { - pr_err("leaked reference.\n"); - if (tracker->alloc_stack_handle) - stack_depot_print(tracker->alloc_stack_handle); - i++; - } else { - break; + depot_stack_handle_t stack = tracker->alloc_stack_handle; + int i; + + ++stats->total; + for (i = 0; i < stats->count; ++i) + if (stats->stacks[i].stack_handle == stack) + break; + if (i >= limit) + continue; + if (i >= stats->count) { + stats->stacks[i].stack_handle = stack; + stats->stacks[i].count = 0; + ++stats->count; } + ++stats->stacks[i].count; + } + + return stats; +} + +void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, + unsigned int display_limit) +{ + struct ref_tracker_dir_stats *stats; + unsigned int i = 0, skipped; + depot_stack_handle_t stack; + char *sbuf; + + lockdep_assert_held(&dir->lock); + + if (list_empty(&dir->list)) + return; + + 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); + return; } + + sbuf = kmalloc(STACK_BUF_SIZE, GFP_NOWAIT | __GFP_NOWARN); + + for (i = 0, skipped = stats->total; i < stats->count; ++i) { + 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); + skipped -= stats->stacks[i].count; + } + + if (skipped) + pr_err("%s@%pK skipped reports about %d/%d users.\n", + dir->name, dir, skipped, stats->total); + + kfree(sbuf); + + kfree(stats); } EXPORT_SYMBOL(ref_tracker_dir_print_locked);