From patchwork Fri Jun 2 10:21:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 102459 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp927381vqr; Fri, 2 Jun 2023 03:34:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4jKmvBuLSqxdr9sd65niIRG3KK9FhPcj3HjhaNE6xl1fziWDDErAiyMq0RO5GhSewPAhGX X-Received: by 2002:a9d:4d86:0:b0:6af:704c:9759 with SMTP id u6-20020a9d4d86000000b006af704c9759mr2119842otk.24.1685702068462; Fri, 02 Jun 2023 03:34:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702068; cv=none; d=google.com; s=arc-20160816; b=lQatOG4rYckq5tyozru3xPiS7D1LSTGbYTrXdWn1u9CdxYzALbYKKuuc3Q5UzTMJ3S nfO0i1Ovz5VhmKePCRI8GaxQnFD1B9teHKcTeQMwg659+GDyaZmWhX1d/Iy4roFMmu45 ik2RN0oveu4J7aFphc8ZM1cawPFqBZ/WMS7m12KKtsO6vWftD8doqa83xpVxZ5PBoFPb EM4+hJKMQf809DbAWbn6Y9hR8g4YzZuGk6Z+VV+2zuiJuYnU/KKp2k3Z3ZO3ujGOVakM zMqrLoQ87JZ3rfY5sNTiaMN14BICIiLIWyIOMauxzWUV7EH4ttEsji0B1L3CTXWtgvgj QvoQ== 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=4XI1a23hPEkJsqk4x5iY/TgQUqezKAcgLInQR1S70hE=; b=Btrks3rnP/uSpHEFBgw/XrVNriu/syyWFveIua6PKkEBC0vW3lgFRFhPupTDk/kYH1 JAEbvhXKC8q8ytuqzpTyA58ccIBTul4RblWdx4tvhVdfw06E9gFlrEN+WWlWTafB+Kuo +Km099L/nC0viCc+oNV20IQBdawgOpIr8M/j7fuJPrtVTmq3Vhj6aebOxu8sdxgCauh5 7tfheyQZfA3ElWlMmWpM+mCR4GUfGPyKf3UEFOg0Td0grDqvC/Uh+SYT2GjP4WtWOqnJ y+AAXRiNVeYUhWZ4C7GrXlJuB0wyj9wY7yKHtwsnSVT/WMETZ3/Xg6iPvb4DJviyV4gA 6QJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hh+hZkyl; 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 t12-20020aa7946c000000b00652908aedd3si522143pfq.249.2023.06.02.03.34.13; Fri, 02 Jun 2023 03:34:28 -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=hh+hZkyl; 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 S234277AbjFBKW2 (ORCPT + 99 others); Fri, 2 Jun 2023 06:22:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234783AbjFBKWK (ORCPT ); Fri, 2 Jun 2023 06:22:10 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2778AE46; Fri, 2 Jun 2023 03:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685701319; x=1717237319; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=mqfual4qhwBGATemiquiJwy5ApxQS+xFr1kM/b4gHaA=; b=hh+hZkylqBJps5gBBqcyt41SapR0L0ClMUfhK8X3QXfC9LXiAMchv/jC izDKQLi+9G4h3AuBxD33gcPZSOB64FGanNY4U/r4rLNqewUssApOMkhBw 21ENoAU5I1zsyl9lzvCFFps+c3ui3zfNpdgl5qvO8v+wtzw8LaIIKgOou fP059TiAKxI31UBXpD+P1gKNBhFVXmQ1afjjI66nrNeeZ5LUo6zDeFgOx Ym/mNNesdcQE4JeuWlDZOzyP4hA3As918nwqqgIIlJMNUrKLu9NtenxuT Xf1dwcQyU2HjyfLg094zWSjI0xwbTAMmFL22kve8Mtrkbh/+iA05P4zPD A==; X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="358267602" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="358267602" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2023 03:21:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="707804966" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="707804966" Received: from lab-ah.igk.intel.com ([10.102.138.202]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2023 03:21:55 -0700 From: Andrzej Hajda Date: Fri, 02 Jun 2023 12:21:33 +0200 Subject: [PATCH v9 1/4] lib/ref_tracker: add unlocked leak print helper MIME-Version: 1.0 Message-Id: <20230224-track_gt-v9-1-5b47a33f55d1@intel.com> References: <20230224-track_gt-v9-0-5b47a33f55d1@intel.com> In-Reply-To: <20230224-track_gt-v9-0-5b47a33f55d1@intel.com> To: Eric Dumazet , Jakub Kicinski , "David S. Miller" Cc: Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Chris Wilson , netdev@vger.kernel.org, Dmitry Vyukov , Andi Shyti , Andrzej Hajda X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,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, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767586732021889119?= X-GMAIL-MSGID: =?utf-8?q?1767586732021889119?= 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 Reviewed-by: Eric Dumazet --- 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)