Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4426684pxb; Mon, 21 Feb 2022 21:15:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHyTuX5zd+nfPRbhggbi/osVQUA9He4jT1m/F8esFS1Dsg7HELuMidsWMPpwuTaXXh96pV X-Received: by 2002:a63:d116:0:b0:372:9961:4203 with SMTP id k22-20020a63d116000000b0037299614203mr18411356pgg.361.1645506948120; Mon, 21 Feb 2022 21:15:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645506948; cv=none; d=google.com; s=arc-20160816; b=ahQpVzU8KW5pZIzxQbFoxaa6PEgf+gjYkeziIL4xOd03gbFTkeTwuCm72oUCVTfK/H ZgWnnjsxlwVVjuPmO1dracq3KcWZKp+fz+4F+OfMGYsT+umBL/9yfTXgymOxCYARplf/ 1mWcs/5c5Bedff78fXCn3B8FSSTVyw/CPLoi77FWrljKRokHdwqhpJq5npJCqd6Ia4My 193op4dBs6jlxImRkDeuIn2uufi5Mz7TzpIs1cfCe4v/S6u7o2CjR2FH62EMmxIY7gFx xpVvHhZM08snJe/tPdtT8Sda/W2hwJGmuQ8y217euPkSFvdtiV0YWxAM7ugGOL+Ktg/0 FxLQ== 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=z6VSOgRiszTNjfwLGHDYjOcsNwC/Q/uVgS40zOzFgfQ=; b=ORUmw3tnYKRAd5t1+d653EU1qTQori7K+YyW7ihiykp1qgCmzxbIbImtC7UCP1LFvw arO9vEkXXO+4o+3guzfUHiPbVE1gs/SwB5NMRx7swkVOrpykbqDNJ4u/Ei5nYKLOR/46 TD9S4fiOPsfO7S1ct3yPX3AKgGJ53p9U2lPRe/AqseI2btqeWvvNhKXQwn6Y5MNsmOOA rykckUWOBiIAtAQk4bIdYW4rc6H6lPcg14F0oITMmb0Qtrc9ZjVefnjI5kKPyU0W1bdX q46DLsP5ADp4PYlpSsdTvYnoHs3o1JiEkiDKVKz27s6WyT+jDbsC7OKU1nFi9cWE9xcC xRJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QfhJPG3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bc24si3940356pgb.462.2022.02.21.21.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 21:15:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QfhJPG3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2D22CE7288; Mon, 21 Feb 2022 20:47:54 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236330AbiBUXSs (ORCPT + 99 others); Mon, 21 Feb 2022 18:18:48 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:33512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236348AbiBUXSZ (ORCPT ); Mon, 21 Feb 2022 18:18:25 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 610CC24F06; Mon, 21 Feb 2022 15:17:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645485473; x=1677021473; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tQY4mR2KDwyrHcJb7GxZeyIMhFFAwu7Cvbv8/dkN8c0=; b=QfhJPG3XQrsENJ6Ab4usoaF9vDm5kIFUoesn62gbybpdATkTvsIlSI18 YR56q7vnmNSbzPW55sWqH8ivLCsZBHxveFpqKZbKmZzGB7yq85uqqpPGf 0xj6iX2pzb4reQ8XhnXf3beVdq+Lj7iasaJXrCNMGB8DkAYc6/rouv0il 5VH/vAqip9UxTa4F61VWFLcx4wDK6Pu6cSIUteyIq5uo/4zhc/Zzmox8n +VfmfYBE9Os+F1fiZCobzsBkYgh7Ku0gbXKeqxKGonP/zavVDkCMHPQqU nIq1IdjqXd6bD7WaVGV59O4IWhGQwP82PaFhWkSuOWdMvL8U2JKb5bO1I A==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="251530408" X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="251530408" 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:53 -0800 X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="638694491" 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:50 -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 04/11] lib/ref_tracker: add unlocked leak print helper Date: Tue, 22 Feb 2022 00:16:53 +0100 Message-Id: <20220221231705.1481059-12-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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: Chris Wilson --- 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 9ca353ab712b5..3e9e9df2a41f5 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(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(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 dc7b14aa3431e..5e9f90bbf771b 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(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); + +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(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(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.25.1