Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp835428pxb; Thu, 17 Feb 2022 16:09:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzepm/grT9X4SlzUeMw5hRbTdl8F9hOERjFKykO/+Ay4DYHIJWEhkA2yuQKUHMnMs1hc23W X-Received: by 2002:a17:903:2406:b0:14d:6447:990c with SMTP id e6-20020a170903240600b0014d6447990cmr5050724plo.22.1645142964698; Thu, 17 Feb 2022 16:09:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645142964; cv=none; d=google.com; s=arc-20160816; b=VFuz1VOjCvsDz81MXeqdxjkh7Tg1OcoPNBc3bhlG1qSKG5hoGgN65ZWUbK+YD/QW/L CIL+Lppg+D2ZdWyW2n3s9zOL2dPLmrxENAZsomhz1ApQzwi63R8ALyALHWsfSZJz32B7 OKZ+isG14YFvSBghMANDqmbEFpWYBE7/dP+XXgmCdb+5BphuitCBk65fI+4idjhyKxNZ 5ffpTiFP2YZ5KNAbmKDFq1b+eNr52GJ61evCgTdcFDMJ93raTMJLvev7WJSTAkZzxHa2 +VYbvDxjq+TMXZYl8Mq/tOsMO8eLtK4+nvUEIKzdrHQ3anYVn5TA2/L8RBPeit2uzeFH b4sA== 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=VWT+9RBZlW+49FJLCHmp9IABN8bbW8iTBEv7IheFnho=; b=lzcGCRSnyOWFCVKFxDWMpFKAOfqowzoI8OHWpPZ58qUjh7kf+tukotdmgRt1I9E5Zv CUPHLrC/4C2ErXyNe0Ih7dZBVgCinLF11TVb/4ZUc5uhSyefKOOqlG03GJMxnOb8T/9t XhKt6F17bFzMuy7vRSOm4lpplExp2M0LAwjvoR4vDTP5hJw75KL33IClwxdB4+IHXJXe m5uADwOdhmNT/26OTn8O6Nb87+Q7FpGddLkBSpayT3qoWC8hHTzdoet/+E1HKVtV9hQN Vo1IJh5LPknlBrsBUAiftmaPIvDDrwd1z+J80HEPQHNUMkllaQIHZqCIu7b630ELiwII zlWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YyMtKIKD; 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 i5si2207544pjz.146.2022.02.17.16.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 16:09:24 -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=YyMtKIKD; 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 776DA33AE3E; Thu, 17 Feb 2022 15:35:28 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241592AbiBQOFV (ORCPT + 99 others); Thu, 17 Feb 2022 09:05:21 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:54382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241568AbiBQOFP (ORCPT ); Thu, 17 Feb 2022 09:05:15 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E22EC9D049; Thu, 17 Feb 2022 06:04: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=1645106699; x=1676642699; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Pk7RlYaQyw781A8X6p8V9ya5Qc0/GolTrSOpzx8g9s8=; b=YyMtKIKDHCz72I1vfAEHvY3adee4EZ929e9dwbHiCm08Ft7USy0GFr99 XJJ4833AQpBW39XS+A26aOM9GEycMagBchSNnbifZMy3b0jwMQMznXttR 5pwgQOCYRn9zcJbJHsbPj3BoSUxVq6d23mCmCjAgIjBrWFP5d3mqbHdH4 MSnsp/2mOYUnxKk6utvyAEjz5MwYO+4zb7ewr9geose9/CiZGhADS00tA yD6eGJIFnqrOhoovCr/N31N7RoIOMdedOmwBbJY17VTqns1mJC3ztDWPG yR0VS4aacDVz4DLRIpHVtPCp3GyiRmyRZvnH3OHx5uquGIYIX8d7jl3YB A==; X-IronPort-AV: E=McAfee;i="6200,9189,10260"; a="231501836" X-IronPort-AV: E=Sophos;i="5.88,376,1635231600"; d="scan'208";a="231501836" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Feb 2022 06:04:59 -0800 X-IronPort-AV: E=Sophos;i="5.88,376,1635231600"; d="scan'208";a="530241097" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Feb 2022 06:04: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 1/9] lib/ref_tracker: add unlocked leak print helper Date: Thu, 17 Feb 2022 15:04:33 +0100 Message-Id: <20220217140441.1218045-2-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220217140441.1218045-1-andrzej.hajda@intel.com> References: <20220217140441.1218045-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 60f3453be23e6..b9c968a716483 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -32,6 +32,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); @@ -52,6 +55,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 a6789c0c626b0..1b0c6d645d64a 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; @@ -26,13 +58,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); @@ -40,28 +72,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