1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/export.h>
3 #include <linux/ref_tracker.h>
4 #include <linux/slab.h>
5 #include <linux/stacktrace.h>
6 #include <linux/stackdepot.h>
8 #define REF_TRACKER_STACK_ENTRIES 16
11 struct list_head head; /* anchor into dir->list or dir->quarantine */
13 depot_stack_handle_t alloc_stack_handle;
14 depot_stack_handle_t free_stack_handle;
17 void ref_tracker_dir_exit(struct ref_tracker_dir *dir)
19 struct ref_tracker *tracker, *n;
23 spin_lock_irqsave(&dir->lock, flags);
24 list_for_each_entry_safe(tracker, n, &dir->quarantine, head) {
25 list_del(&tracker->head);
27 dir->quarantine_avail++;
29 list_for_each_entry_safe(tracker, n, &dir->list, head) {
30 pr_err("leaked reference.\n");
31 if (tracker->alloc_stack_handle)
32 stack_depot_print(tracker->alloc_stack_handle);
34 list_del(&tracker->head);
37 spin_unlock_irqrestore(&dir->lock, flags);
39 WARN_ON_ONCE(refcount_read(&dir->untracked) != 1);
41 EXPORT_SYMBOL(ref_tracker_dir_exit);
43 void ref_tracker_dir_print(struct ref_tracker_dir *dir,
44 unsigned int display_limit)
46 struct ref_tracker *tracker;
50 spin_lock_irqsave(&dir->lock, flags);
51 list_for_each_entry(tracker, &dir->list, head) {
52 if (i < display_limit) {
53 pr_err("leaked reference.\n");
54 if (tracker->alloc_stack_handle)
55 stack_depot_print(tracker->alloc_stack_handle);
61 spin_unlock_irqrestore(&dir->lock, flags);
63 EXPORT_SYMBOL(ref_tracker_dir_print);
65 int ref_tracker_alloc(struct ref_tracker_dir *dir,
66 struct ref_tracker **trackerp,
69 unsigned long entries[REF_TRACKER_STACK_ENTRIES];
70 struct ref_tracker *tracker;
71 unsigned int nr_entries;
75 if (gfp & __GFP_DIRECT_RECLAIM)
76 gfp_mask |= __GFP_NOFAIL;
77 *trackerp = tracker = kzalloc(sizeof(*tracker), gfp_mask);
78 if (unlikely(!tracker)) {
79 pr_err_once("memory allocation failure, unreliable refcount tracker.\n");
80 refcount_inc(&dir->untracked);
83 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
84 nr_entries = filter_irq_stacks(entries, nr_entries);
85 tracker->alloc_stack_handle = stack_depot_save(entries, nr_entries, gfp);
87 spin_lock_irqsave(&dir->lock, flags);
88 list_add(&tracker->head, &dir->list);
89 spin_unlock_irqrestore(&dir->lock, flags);
92 EXPORT_SYMBOL_GPL(ref_tracker_alloc);
94 int ref_tracker_free(struct ref_tracker_dir *dir,
95 struct ref_tracker **trackerp)
97 unsigned long entries[REF_TRACKER_STACK_ENTRIES];
98 struct ref_tracker *tracker = *trackerp;
99 depot_stack_handle_t stack_handle;
100 unsigned int nr_entries;
104 refcount_dec(&dir->untracked);
107 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
108 nr_entries = filter_irq_stacks(entries, nr_entries);
109 stack_handle = stack_depot_save(entries, nr_entries, GFP_ATOMIC);
111 spin_lock_irqsave(&dir->lock, flags);
113 pr_err("reference already released.\n");
114 if (tracker->alloc_stack_handle) {
115 pr_err("allocated in:\n");
116 stack_depot_print(tracker->alloc_stack_handle);
118 if (tracker->free_stack_handle) {
119 pr_err("freed in:\n");
120 stack_depot_print(tracker->free_stack_handle);
122 spin_unlock_irqrestore(&dir->lock, flags);
126 tracker->dead = true;
128 tracker->free_stack_handle = stack_handle;
130 list_move_tail(&tracker->head, &dir->quarantine);
131 if (!dir->quarantine_avail) {
132 tracker = list_first_entry(&dir->quarantine, struct ref_tracker, head);
133 list_del(&tracker->head);
135 dir->quarantine_avail--;
138 spin_unlock_irqrestore(&dir->lock, flags);
143 EXPORT_SYMBOL_GPL(ref_tracker_free);