diff mbox series

[v4,01/10] lib/ref_tracker: add unlocked leak print helper

Message ID 20230224-track_gt-v4-1-464e8ab4c9ab@intel.com (mailing list archive)
State New, archived
Headers show
Series drm/i915: use ref_tracker library for tracking wakerefs | expand

Commit Message

Andrzej Hajda March 6, 2023, 4:31 p.m. UTC
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 <andrzej.hajda@intel.com>
---
 include/linux/ref_tracker.h |  8 ++++++
 lib/ref_tracker.c           | 66 ++++++++++++++++++++++++++-------------------
 2 files changed, 46 insertions(+), 28 deletions(-)

Comments

Andi Shyti March 19, 2023, 10:59 p.m. UTC | #1
Hi Andrzej,

[...]

> diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c
> index dc7b14aa3431e2..5e9f90bbf771b0 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)

can we call this ref_tracker_dir_print_locked() instead of using
the '__'?

> +{
> +	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);
> +		}

Just thinking whether this should go on a different patch, but I
don't have a strong opinion.

Looks good!

Andi
Andrzej Hajda March 20, 2023, 3:30 p.m. UTC | #2
On 19.03.2023 23:59, Andi Shyti wrote:
> Hi Andrzej,
> 
> [...]
> 
>> diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c
>> index dc7b14aa3431e2..5e9f90bbf771b0 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)
> 
> can we call this ref_tracker_dir_print_locked() instead of using
> the '__'?
> 

OK, 'locked' convention looks better.

Regards
Andrzej

>> +{
>> +	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);
>> +		}
> 
> Just thinking whether this should go on a different patch, but I
> don't have a strong opinion.
> 
> Looks good!
> 
> Andi
diff mbox series

Patch

diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h
index 9ca353ab712b5e..3e9e9df2a41f5f 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 dc7b14aa3431e2..5e9f90bbf771b0 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)