Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2108607rwr; Fri, 21 Apr 2023 04:36:49 -0700 (PDT) X-Google-Smtp-Source: AKy350ZefxmRO8bvoXhFuz+wlUoEf9lE0nPN5Dbi6Hr9u0XBAtNvtnLKzau1k+se9pvShcsJhLoE X-Received: by 2002:a17:902:f685:b0:1a9:4167:5db7 with SMTP id l5-20020a170902f68500b001a941675db7mr3352705plg.4.1682077009484; Fri, 21 Apr 2023 04:36:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682077009; cv=none; d=google.com; s=arc-20160816; b=FFHwo1LOYqPLfpZcjbmcDuCsxCmhBox/XhbbKldge5qxsqp3QqtvdHaZhZ+4+yf/fF qvFjC2VwVbMEL2y/6Lv2yzT4iAQTjQT52ZDk3NFH2HMPqFbODxo5e1EratQPJSiXtW0z xg4S14q1s/BDXtYK0GEiULm3SqCdvhJvrkrgXhsmYEvHMEiF5/9+mvddrGsKGfIVYi1I oxIbttryDkg77T9McyuR6IHpHsE4b3YkGXw25wb6G9DSL6lA1ZnpVpOMdoBC3rCfGJoI 8M2RSpYxzNm5gpuieC/R6J3Xu1pjDw+vT8Htwi2DEpCzbaLYAKmIB0iiUrV/c9kvm1W4 8Wvw== 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=qRQE79NI1r/Plu6yAUu8WUzraWbpiqVTW4nzzyfegnc=; b=eFV+vYknT8mPRJ0QSVw9ff440IXZPKeae/ZZsMHOrruIs10OHBoey+2vZZoG0b22VV klrzQSZbFVTJ0Y08tfCa4dkam8+VigjOXfEZdZTFHM7aWOunNWjsZ1gXTTS+/091Iv8H Dy+6b8mKHn0+x0UGecAiCsqFxd5n4grWChbGPTJLlwEAOlGOsfph7ovGgGNJNGjfhYvY OBvpVQygvy2jhFtWXeOY9RbXYoA6CHYTOd6dsI7032hK2A2xldUYtCZWVt0xYvdq+K6j 22JrR61PAmH0KKJ3CQoB8b+PUzEeboQLi/J1XvVaK3+qjDuNw6soiTtH9Cj+innzWPfE ZOrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Kvbt8uE8; 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 ll12-20020a170903090c00b001a643d9f1b8si4217726plb.643.2023.04.21.04.36.38; Fri, 21 Apr 2023 04:36: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=Kvbt8uE8; 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 S231617AbjDULf0 (ORCPT + 99 others); Fri, 21 Apr 2023 07:35:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjDULfY (ORCPT ); Fri, 21 Apr 2023 07:35:24 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17D4AAF1A; Fri, 21 Apr 2023 04:35:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682076922; x=1713612922; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=Hgf4GUcvraNUcwP7ECqhlabBrNBOaHjkbk61oEsey4w=; b=Kvbt8uE8F45GPIjvLXBh33mPF2MLlSsW77srm+4kKG1gtA3rgTxyhD8m 3Rbz8/SCGlridEDNoO/1lAtg2qcjQP5Qm0tg2V9SQDVinxmj2HAO/aaqs DXZ33HbCT7TLmxYbxSzjUZNxLvZhmxl63RtEJJUKAVJfmNJ52ISseQZIk tmE6ln/OC69KSlIz9WcfBJxb8JCIPKts/tzyRFeg+SahHmR6ldo2q5KDP 70y9eyEQU3XRinl4xMSwBxC+gzlIaOouq/INiJudNligRPZMNXW8A6xnB 4ccI2PUqKmjR2i9dZFyZt/9DAliv5XtI7KMV2gYmsm6eXoer7h0RgmYge Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="432249850" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="432249850" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 04:35:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="642489641" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="642489641" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 04:35:17 -0700 From: Andrzej Hajda Date: Fri, 21 Apr 2023 13:35:04 +0200 Subject: [PATCH v7 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-v7-1-11f08358c1ec@intel.com> References: <20230224-track_gt-v7-0-11f08358c1ec@intel.com> In-Reply-To: <20230224-track_gt-v7-0-11f08358c1ec@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=-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,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 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 --- 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