From patchwork Tue Mar 28 15:15:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 76132 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2304935vqo; Tue, 28 Mar 2023 08:27:08 -0700 (PDT) X-Google-Smtp-Source: AKy350ZloGQxL3AmntDd7Q4t90SDSK9IYlsgggtwKfwgn4ZrnCrlYHVK+SfgO8uRgSAAy4qjzKuG X-Received: by 2002:a17:906:3288:b0:8b1:fc58:a4ad with SMTP id 8-20020a170906328800b008b1fc58a4admr17759308ejw.11.1680017228046; Tue, 28 Mar 2023 08:27:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680017228; cv=none; d=google.com; s=arc-20160816; b=mYJlTvpGFo+FIV9EIztgfshkpX95z59kTCflFnuvpljThM40w2cgxt28vG45on9eBS TwXRr5kPBSVCrtGMsM9Tf5VtsIduiERbIvx2hKBqmWFVu/UBZOjBJAFuDVxkzoiBu08O u4OFBf2BiYngKBiB5h3t4KWdAxdta2jwnQZkUbOfAqCOhUYq9HvX+H2fAE//ece+Duor RktHnmMjnp6oYlD97t5FBQ6+cz+VrR53SrnZ8lD6bOU5lZ76G8/oYSO/pEYEhco4tS3+ Rf0PGvFaiZ/yuGjqbryjwQQ5TziYT3/3IyPTNBlNK9suyFs9czsh0feeyzMsGBya4bEd OtAA== 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=EjxpR5qKOcM8NAqyEUqEi+8EBJ43g85EbQGaTX3C+gMx9jl4MdUw6XQzParNnTZ5hJ bxn2CI2CCLvyvWtFNyzbPnynvUlCgJsb9X6q0A4FNqdrrRcGJdUtEuHWJBOjwf5UzXJx 14J7c4GxgQDNvrACTzXNuV3CsLTetAHnBt8Snf6qVwtHddalEh3saylDRzzxo3Pc3GXh mVtVqdhgTmFVnZc6JbknR0fTmxCgG2v1701mxcfQyBGuv8wEhnJHT8KtG3kmVUh3nu3+ 7lZDljIESQYOZ774UUxcttplUgcseuw2S/7KSTTB0Fk74loNXdLGjyBzfNI18YdRBCoS N2dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ChPqkUu4; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p9-20020a1709061b4900b0092fe3fdc1fdsi28944922ejg.605.2023.03.28.08.26.43; Tue, 28 Mar 2023 08:27:08 -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=ChPqkUu4; 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 S233911AbjC1PR1 (ORCPT + 99 others); Tue, 28 Mar 2023 11:17:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232624AbjC1PRO (ORCPT ); Tue, 28 Mar 2023 11:17:14 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D34C61117F; Tue, 28 Mar 2023 08:16:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680016588; x=1711552588; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=k2hx/WBFqL7VuFz0I6+8fGkxNCCWOhJe2NR5annG8QI=; b=ChPqkUu4nXQMSKWoAx6g1zrYvJ8YQgnBgllAnsuMLFqKSDD6eLhjmz53 sRSvi2z+TIlep5fLxlaZ7lp3k0/UdQnxXUr45ROS5HQFfMAW/k47Z/IvI TR4HrHbjMO2ZpEQFCKkaU05YddJ5aKd7NAPKyU4MkZrBGGPMREq9PJgYZ tA9ZI6necyza8uCd+C2KG8+AysrfYm5yPKI9o27sgM+3LfuuD4eesG+6j 1EM1x5ljApxHhqSz/5uFOimDIrZxz+toO6ey4lNLiznmRBdFoJg/KL7fj JYnC2bDD4nZHqMrjFtdrgfQacPZ21TwlsSe0xOdx/yFiwJI3LX4hzEm4P Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="403208612" X-IronPort-AV: E=Sophos;i="5.98,297,1673942400"; d="scan'208";a="403208612" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2023 08:15:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10663"; a="773181739" X-IronPort-AV: E=Sophos;i="5.98,297,1673942400"; d="scan'208";a="773181739" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2023 08:15:46 -0700 From: Andrzej Hajda Date: Tue, 28 Mar 2023 17:15:24 +0200 Subject: [PATCH v5 1/8] lib/ref_tracker: add unlocked leak print helper MIME-Version: 1.0 Message-Id: <20230224-track_gt-v5-1-77be86f2c872@intel.com> References: <20230224-track_gt-v5-0-77be86f2c872@intel.com> In-Reply-To: <20230224-track_gt-v5-0-77be86f2c872@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" , Andrzej Hajda , Andi Shyti 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,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761625744905432767?= X-GMAIL-MSGID: =?utf-8?q?1761625744905432767?= 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)