Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4262652pxb; Mon, 21 Feb 2022 16:18:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7hAUDHiv2vSYvDjdsdzlmUnJVlgF4fUqCBLRR+CygOWINoWhsT7+uSQh6qQ25eUHyTUfR X-Received: by 2002:a63:5c51:0:b0:373:c36b:2d0c with SMTP id n17-20020a635c51000000b00373c36b2d0cmr16081462pgm.209.1645489133065; Mon, 21 Feb 2022 16:18:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645489133; cv=none; d=google.com; s=arc-20160816; b=EKuiU67v+HGj/ufzpMFWRQ7K7JQjdPz5SzfAq335CNXF98j6KUXUm3KuSTWYOF346t D7nW4BGw+4eGkrZOOelaVnBcGkUBKZ79jKWO7Ajlb33UX+Eux+5zzUnnXb7245dXQmvw 4VdE0y0XQuyswK9CnkhryTdWmHz+D858u2t5eibjzhZ7Auq0niI4DRQNLT1TRi89w6rY xMnhskJB2W3lbhn1g089wAsSSeIWO1T4mkjVyyOfxpgo0UciOCFt+4pmt3mifi7wxsfM Ii5DRbWbqTwYvZ36ZhVUW4pFq+B/ox5WZup1qmoPkCXRwR8sY+ug8AMT8J41VaDs5CGn 96UQ== 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=v1CR3gVs5D+zIhKZ3kexBPySleK1Z4h8Iz9XwTjORWs=; b=VdWDPbGlNcGsbj6/N5u38NKXUtr68Pt3JUBJjpB05sntXaJmaUWBHV8nLKHYI/IVKK 4YIUB6JxTeo0F72uQmDCBDVgHqToGLFIyCGvvFiiCn/ItnkyvFALHvIwgUDHtM4pZ9RS VsqECXIzua9pMUOkqppWlYK4sLQJBrp+o3RujXEHn6dNGDqRVvUjXLE81ya1ECBNhxYm AnviQKn6sESnk19OxHdIeOHmkfQG7eiURu5mxl5PuLwXulJW8zaIYSQ9YyLAa4NKjg17 18/+S0/pcBjr6ozHnNWh5XWGYFRa2F9dxmojyKsBiFFdtIA9/g+y88YIK3FkL5pqXzy4 AKBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ii+TO4Ne; 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 b21si28881696pls.288.2022.02.21.16.18.35; Mon, 21 Feb 2022 16:18:53 -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=ii+TO4Ne; 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 S236496AbiBUXSk (ORCPT + 99 others); Mon, 21 Feb 2022 18:18:40 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:33558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236418AbiBUXS0 (ORCPT ); Mon, 21 Feb 2022 18:18:26 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39C2024F26; Mon, 21 Feb 2022 15:17:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645485479; x=1677021479; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=g08j7LXB1cG4K+7SmHFS/Pc2Q959AY3yWgcIbyn6GFQ=; b=ii+TO4NeXTpbVV4c7UuYcDz2LFZONXN2oo/reYKQcG3G2gYgGpb93NXH jJqtXzuYIlfuNrUFHtsvlOitpXnlM4bDJEh7ajDHAtxeLq617+WI0kzOP kZtlhLEJ1X8fvJnMlRaNFeAWoip5GR3yBLqYvNHt9eGfJdLow0qnz3W1H h30KB45IrNjS9IxFs91CsSqEH6rVwLQVgCKd9uBpQTh+r5zpmGBHFeddB Yc8H1ZAVUFZoGTb6ayofBG5G6fh+/uXLcdeS2SW/7GQ8fdExt9P5VR6sh A5HYViowChbYx3j0KNBOYf+7BYzVmwk7bU2Fwbx5DIQT1mXMDr4XYefH9 A==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="251530415" X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="251530415" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:17:59 -0800 X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="638694516" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:17:56 -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 v2 05/11] lib/ref_tracker: __ref_tracker_dir_print improve printing Date: Tue, 22 Feb 2022 00:16:55 +0100 Message-Id: <20220221231705.1481059-14-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220221231705.1481059-1-andrzej.hajda@intel.com> References: <20220221231705.1481059-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 To improve readibility of ref_tracker printing following changes have been performed: - reports are printed per stack_handle - log is more compact, - added display name for ref_tracker_dir, - stack trace is printed indented, in the same printk call, - total number of references is printed every time, - print info about dropped references. Signed-off-by: Andrzej Hajda --- 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 3e9e9df2a41f5..a2cf1f6309adb 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 5e9f90bbf771b..ab1253fde244e 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(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(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); -- 2.25.1