Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6172207rwr; Mon, 24 Apr 2023 15:09:09 -0700 (PDT) X-Google-Smtp-Source: AKy350YT74mLuzgzFVWAGnw01w89/oA3kBBAPt/EQAxB6WsU7sIOSqrVuJtMJbOgD880LWBBSXf0 X-Received: by 2002:a05:6a00:807:b0:63a:fae3:9890 with SMTP id m7-20020a056a00080700b0063afae39890mr21393418pfk.24.1682374149144; Mon, 24 Apr 2023 15:09:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682374149; cv=none; d=google.com; s=arc-20160816; b=crwKN3eR+qhrUhn88x+Y6s+jYfmFvFT/cglY0BWBoaPju3EY8lrF9VfwDTZGfXS3YK Sh3903PVuXxK4DXcrNDv4Ca4PITGHWipLe/Q5FZ4kjOqdTrYZoSQJDR+66QQ3JQ0XrGj KrSHj9+YpLU5D/VohJen+J/d5mI801wwUS/+J76NgnviSA0s8ADt8bEKeg5DM6yzTKIC dsDpq9ZqEc/y9cXIILjI/V5RiVF5esbCQAUljY9q9FahMNZeoqnR5339G6r/BqINB+4Y udoY5Bz5VUNnbxOntT9SKfSyEtyaNjEcsTw9KwizcPoM3+yHWrAQiJ2vvaXWWRTX58Gs UXwA== 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=4XI1a23hPEkJsqk4x5iY/TgQUqezKAcgLInQR1S70hE=; b=y4cx3PBRpH1JyKcYxwjb/tHI7d1bHqcuDuSebsvgPuCBaiDZcql3h4IgQsgdstf3NT fotKbJpmtDP/bwCcTH3SFNr2KRHgzczsPkKB2DOYFWx7Tv3sukhGmrEKuafaz8mPFH6X /TaPQBRqOT/pIqn72aNXihu061lUabF1acdibzoqK1g4IrBkmRUHzXtjME8A7UloW31n sfkjVJL/3jlXpi/nMUVAiwSW0iWVLxTcoMzMKMoQa9hUNGlZyXHKzK2OOY1wzJMVwfqQ 8LMX/e7BaOONcHra1dTGpK5En3YBEDSdkUCoaOwWgEGDmw3BHsxlbIfDFh3CNxtjtfHS k5dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TMK0IoF5; 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 g29-20020aa79f1d000000b0063b5e9c3c28si11877897pfr.35.2023.04.24.15.08.54; Mon, 24 Apr 2023 15:09:09 -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=TMK0IoF5; 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 S233149AbjDXWGC (ORCPT + 99 others); Mon, 24 Apr 2023 18:06:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233139AbjDXWGA (ORCPT ); Mon, 24 Apr 2023 18:06:00 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91701B9; Mon, 24 Apr 2023 15:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682373958; x=1713909958; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=mqfual4qhwBGATemiquiJwy5ApxQS+xFr1kM/b4gHaA=; b=TMK0IoF5MaQ56YwVWrgkRLuV0Y39XQ0p8v2XyJPNDe/rR9MOkwd8fPGN /Nb/OQjbEkKRz1jj7ZdfqxgQL/K+MFqiVEHLbejAHwlngiBPY2Smqfnym MNn7Z9d/yqGaW3z1ymnMA7W12wr2BiVFWT3bkt/luucTGlXj2hcBxq6oV URx4Xof4njoq3xn40q11Lqi6K3Do8kIVCwp1HgkYsQIebiXPACRoeovHG sIIYXywPkoEy1MJA8e0Awlzxe7ljZq79iTdb6+xMj8fZ1AgcB2UfXXppH GyaXlAz8Bl1vlEOl7IqyLiF6KFPADDiE+t3qAIZJIyjcLMnS1y8QBO2gD w==; X-IronPort-AV: E=McAfee;i="6600,9927,10690"; a="335473641" X-IronPort-AV: E=Sophos;i="5.99,223,1677571200"; d="scan'208";a="335473641" 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:05:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10690"; a="939500112" X-IronPort-AV: E=Sophos;i="5.99,223,1677571200"; d="scan'208";a="939500112" 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:53 -0700 From: Andrzej Hajda Date: Tue, 25 Apr 2023 00:05:38 +0200 Subject: [PATCH v8 1/7] lib/ref_tracker: add unlocked leak print helper MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-track_gt-v8-1-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 To have reliable detection of leaks, caller must be able to check under the same lock both: tracked counter and the leaks. dir.lock is natural candidate for such lock and unlocked print helper can be called with this lock taken. As a bonus we can reuse this helper in ref_tracker_dir_exit. Signed-off-by: Andrzej Hajda Reviewed-by: Andi Shyti Reviewed-by: Eric Dumazet --- include/linux/ref_tracker.h | 8 ++++++ lib/ref_tracker.c | 66 ++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h index 9ca353ab712b5e..87a92f2bec1b88 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -36,6 +36,9 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, void ref_tracker_dir_exit(struct ref_tracker_dir *dir); +void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, + unsigned int display_limit); + void ref_tracker_dir_print(struct ref_tracker_dir *dir, unsigned int display_limit); @@ -56,6 +59,11 @@ static inline void ref_tracker_dir_exit(struct ref_tracker_dir *dir) { } +static inline void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, + unsigned int display_limit) +{ +} + static inline void ref_tracker_dir_print(struct ref_tracker_dir *dir, unsigned int display_limit) { diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c index dc7b14aa3431e2..d4eb0929af8f96 100644 --- a/lib/ref_tracker.c +++ b/lib/ref_tracker.c @@ -14,6 +14,38 @@ 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 *tracker; + unsigned int i = 0; + + lockdep_assert_held(&dir->lock); + + 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; + } + } +} +EXPORT_SYMBOL(ref_tracker_dir_print_locked); + +void ref_tracker_dir_print(struct ref_tracker_dir *dir, + unsigned int display_limit) +{ + unsigned long flags; + + spin_lock_irqsave(&dir->lock, flags); + ref_tracker_dir_print_locked(dir, display_limit); + spin_unlock_irqrestore(&dir->lock, flags); +} +EXPORT_SYMBOL(ref_tracker_dir_print); + void ref_tracker_dir_exit(struct ref_tracker_dir *dir) { struct ref_tracker *tracker, *n; @@ -27,13 +59,13 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir) kfree(tracker); dir->quarantine_avail++; } - list_for_each_entry_safe(tracker, n, &dir->list, head) { - pr_err("leaked reference.\n"); - if (tracker->alloc_stack_handle) - stack_depot_print(tracker->alloc_stack_handle); + if (!list_empty(&dir->list)) { + ref_tracker_dir_print_locked(dir, 16); leak = true; - list_del(&tracker->head); - kfree(tracker); + list_for_each_entry_safe(tracker, n, &dir->list, head) { + list_del(&tracker->head); + kfree(tracker); + } } spin_unlock_irqrestore(&dir->lock, flags); WARN_ON_ONCE(leak); @@ -42,28 +74,6 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir) } EXPORT_SYMBOL(ref_tracker_dir_exit); -void ref_tracker_dir_print(struct ref_tracker_dir *dir, - unsigned int display_limit) -{ - struct ref_tracker *tracker; - unsigned long flags; - unsigned int i = 0; - - spin_lock_irqsave(&dir->lock, flags); - 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; - } - } - spin_unlock_irqrestore(&dir->lock, flags); -} -EXPORT_SYMBOL(ref_tracker_dir_print); - int ref_tracker_alloc(struct ref_tracker_dir *dir, struct ref_tracker **trackerp, gfp_t gfp) -- 2.34.1