Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp226293rwl; Wed, 29 Mar 2023 00:27:01 -0700 (PDT) X-Google-Smtp-Source: AKy350bbzZ4kPYfdW8+yH4i3eQ+KjQnBbavmmTDA5IdQXgm9TAfrgn9QaphJe2RDq49Ir+gQnSYD X-Received: by 2002:a05:6402:26d5:b0:4af:7bdc:188e with SMTP id x21-20020a05640226d500b004af7bdc188emr1346624edd.16.1680074820944; Wed, 29 Mar 2023 00:27:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680074820; cv=none; d=google.com; s=arc-20160816; b=Q+CLla2tc+AhOSDQy1sHEQKYb1m92CYmQcFk+bLl3xQe8vOGlxhD9KT63UkhKGyKzM s2m6ycDuQhvxd36yGuTxJfcHDIxE9jfnIm1lOK5pi4JtwxB1AvDf00BCuewUBUCq5Ppu K8jFIOXbphBd6HMfyh5FpUkwuC11ILw/IKNNIn91JqbpuPLG2L/HJP/HfabCaeJrxfjn IUo8+3p1G+SnRGv6+Kx4Kkm8vO3t1YBQ/kBuui0d8EPkPq92fvlHJ628GQeFfZcbeaJl yJU4frBkaR1oP7TaaD5qTKux/u1jj8NTPCaFCAxYuy8fTNZsfYn8cl1Y6aIMx9KrOZBE +2yg== 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=Oyv6fg7oGPUoq0MdQL9r0gWMiSMU5Bnx5Pv9OV93+EE=; b=h0uRipveLBJRp8jwKb/h5r8qZ6ufx2033RSUcPJyMRaWT/70mDh3U/eoqLAFozXetN u18xd1wFkpL2/oW/u1zuPjFJp7vf4Z7KIoyl1yGWZNuqEMkcY7RoFMqvLnlE1ipAoPS9 m+uLzEqbWHhTvkVFpZo+V8ong6Jvob+GWS0OlpgwKofNDVN4BFXikfAm79A8kVXs7+I/ V0mJ3PXFzh/2SO/dYLDhn/CyaQPRtVX9mN3Bk2BTSB/4s+NGyhSuPvQbq5hOSnasU/Jo o4sGFD2+QZRDWB1XPs3ih9IiIMzWI8QGCl32McvAoDlb4lIGhIfQyq8pIJoXRba8xt3T oCVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JFuO8PPW; 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 f4-20020a056402150400b005024c86c50bsi5160337edw.392.2023.03.29.00.26.36; Wed, 29 Mar 2023 00:27:00 -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=JFuO8PPW; 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 S230108AbjC2HZh (ORCPT + 99 others); Wed, 29 Mar 2023 03:25:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230085AbjC2HZH (ORCPT ); Wed, 29 Mar 2023 03:25:07 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF51B1BF6; Wed, 29 Mar 2023 00:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680074693; x=1711610693; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=k2hx/WBFqL7VuFz0I6+8fGkxNCCWOhJe2NR5annG8QI=; b=JFuO8PPWIBLrgCMC8lH27V9G5XfPcd9AG3vC7iwKO5ytIM15B/of/fp9 T6ZeLil8gKT7lzL7F5LyLKReX4Ve4iLluQZr5e5N6tcQtNh+PNZtySBSF /I1e90NPiD9PIgPT3SUUeFNiGDVdySHnZ0SW8mJ3HDw36PGr1nCgkwKzG bBgemV96c+OJ7qqM+f3VydFdkQ9MgQalK/+ZaxuaN0JQ8AhXtEU6jGJDe Cv8yBDoZfbqUdRlRGaMwt5XF0RUvh326198wiNPWDvT8eBsY1pMEZDrki YgVl9pvwaQE1NmWFKh5oSXvNJ10rCoC4XgwDq+Hr9pPjWgw1I67s8srdG g==; X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="368569565" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="368569565" 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:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="827772778" X-IronPort-AV: E=Sophos;i="5.98,300,1673942400"; d="scan'208";a="827772778" 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:35 -0700 From: Andrzej Hajda Date: Wed, 29 Mar 2023 09:24:12 +0200 Subject: [PATCH v6 1/8] 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-v6-1-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 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 --- 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