Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdclass / lu_ref.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2012, 2017, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  *
32  * lustre/obdclass/lu_ref.c
33  *
34  * Lustre reference.
35  *
36  *   Author: Nikita Danilov <nikita.danilov@sun.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lu_ref.h>
45
46 #ifdef CONFIG_LUSTRE_DEBUG_LU_REF
47 /**
48  * Asserts a condition for a given lu_ref. Must be called with
49  * lu_ref::lf_guard held.
50  */
51 #define REFASSERT(ref, expr) do {                                       \
52         struct lu_ref *__tmp = (ref);                                   \
53                                                                         \
54         if (unlikely(!(expr))) {                                        \
55                 lu_ref_print(__tmp);                                    \
56                 spin_unlock(&__tmp->lf_guard);                          \
57                 lu_ref_print_all();                                     \
58                 LASSERT(0);                                             \
59                 spin_lock(&__tmp->lf_guard);                            \
60         }                                                               \
61 } while (0)
62
63 static struct kmem_cache *lu_ref_link_kmem;
64
65 static struct lu_kmem_descr lu_ref_caches[] = {
66         {
67                 .ckd_cache = &lu_ref_link_kmem,
68                 .ckd_name  = "lu_ref_link_kmem",
69                 .ckd_size  = sizeof(struct lu_ref_link)
70         },
71         {
72                 .ckd_cache = NULL
73         }
74 };
75
76 /**
77  * Global list of active (initialized, but not finalized) lu_ref's.
78  *
79  * Protected by lu_ref_refs_guard.
80  */
81 static LIST_HEAD(lu_ref_refs);
82 static DEFINE_SPINLOCK(lu_ref_refs_guard);
83 static struct lu_ref lu_ref_marker = {
84         .lf_guard       = __SPIN_LOCK_UNLOCKED(lu_ref_marker.lf_guard),
85         .lf_list        = LIST_HEAD_INIT(lu_ref_marker.lf_list),
86         .lf_linkage     = LIST_HEAD_INIT(lu_ref_marker.lf_linkage)
87 };
88
89 void lu_ref_print(const struct lu_ref *ref)
90 {
91         struct lu_ref_link *link;
92
93         CERROR("lu_ref: %p %d %d %s:%d\n",
94                ref, ref->lf_refs, ref->lf_failed, ref->lf_func, ref->lf_line);
95         list_for_each_entry(link, &ref->lf_list, ll_linkage) {
96                 CERROR("     link: %s %p\n", link->ll_scope, link->ll_source);
97         }
98 }
99
100 static int lu_ref_is_marker(const struct lu_ref *ref)
101 {
102         return ref == &lu_ref_marker;
103 }
104
105 void lu_ref_print_all(void)
106 {
107         struct lu_ref *ref;
108
109         spin_lock(&lu_ref_refs_guard);
110         list_for_each_entry(ref, &lu_ref_refs, lf_linkage) {
111                 if (lu_ref_is_marker(ref))
112                         continue;
113
114                 spin_lock(&ref->lf_guard);
115                 lu_ref_print(ref);
116                 spin_unlock(&ref->lf_guard);
117         }
118         spin_unlock(&lu_ref_refs_guard);
119 }
120
121 void lu_ref_init_loc(struct lu_ref *ref, const char *func, const int line)
122 {
123         ref->lf_refs = 0;
124         ref->lf_func = func;
125         ref->lf_line = line;
126         spin_lock_init(&ref->lf_guard);
127         INIT_LIST_HEAD(&ref->lf_list);
128         spin_lock(&lu_ref_refs_guard);
129         list_add(&ref->lf_linkage, &lu_ref_refs);
130         spin_unlock(&lu_ref_refs_guard);
131 }
132 EXPORT_SYMBOL(lu_ref_init_loc);
133
134 void lu_ref_fini(struct lu_ref *ref)
135 {
136         spin_lock(&ref->lf_guard);
137         REFASSERT(ref, list_empty(&ref->lf_list));
138         REFASSERT(ref, ref->lf_refs == 0);
139         spin_unlock(&ref->lf_guard);
140         spin_lock(&lu_ref_refs_guard);
141         list_del_init(&ref->lf_linkage);
142         spin_unlock(&lu_ref_refs_guard);
143 }
144 EXPORT_SYMBOL(lu_ref_fini);
145
146 static struct lu_ref_link *lu_ref_add_context(struct lu_ref *ref,
147                                               int flags,
148                                               const char *scope,
149                                               const void *source)
150 {
151         struct lu_ref_link *link;
152
153         link = NULL;
154         if (lu_ref_link_kmem) {
155                 OBD_SLAB_ALLOC_PTR_GFP(link, lu_ref_link_kmem, flags);
156                 if (link) {
157                         link->ll_ref = ref;
158                         link->ll_scope = scope;
159                         link->ll_source = source;
160                         spin_lock(&ref->lf_guard);
161                         list_add_tail(&link->ll_linkage, &ref->lf_list);
162                         ref->lf_refs++;
163                         spin_unlock(&ref->lf_guard);
164                 }
165         }
166
167         if (!link) {
168                 spin_lock(&ref->lf_guard);
169                 ref->lf_failed++;
170                 spin_unlock(&ref->lf_guard);
171                 link = ERR_PTR(-ENOMEM);
172         }
173
174         return link;
175 }
176
177 void lu_ref_add(struct lu_ref *ref, const char *scope, const void *source)
178 {
179         might_sleep();
180         lu_ref_add_context(ref, GFP_NOFS, scope, source);
181 }
182 EXPORT_SYMBOL(lu_ref_add);
183
184 void lu_ref_add_at(struct lu_ref *ref, struct lu_ref_link *link,
185                    const char *scope, const void *source)
186 {
187         link->ll_ref = ref;
188         link->ll_scope = scope;
189         link->ll_source = source;
190         spin_lock(&ref->lf_guard);
191         list_add_tail(&link->ll_linkage, &ref->lf_list);
192         ref->lf_refs++;
193         spin_unlock(&ref->lf_guard);
194 }
195 EXPORT_SYMBOL(lu_ref_add_at);
196
197 /**
198  * Version of lu_ref_add() to be used in non-blockable contexts.
199  */
200 void lu_ref_add_atomic(struct lu_ref *ref, const char *scope,
201                        const void *source)
202 {
203         lu_ref_add_context(ref, GFP_ATOMIC, scope, source);
204 }
205 EXPORT_SYMBOL(lu_ref_add_atomic);
206
207 static inline int lu_ref_link_eq(const struct lu_ref_link *link,
208                                  const char *scope,
209                                  const void *source)
210 {
211         return link->ll_source == source && !strcmp(link->ll_scope, scope);
212 }
213
214 /**
215  * Maximal chain length seen so far.
216  */
217 static unsigned int lu_ref_chain_max_length = 127;
218
219 /**
220  * Searches for a lu_ref_link with given [scope, source] within given lu_ref.
221  */
222 static struct lu_ref_link *lu_ref_find(struct lu_ref *ref, const char *scope,
223                                        const void *source)
224 {
225         struct lu_ref_link *link;
226         unsigned int iterations;
227
228         iterations = 0;
229         list_for_each_entry(link, &ref->lf_list, ll_linkage) {
230                 ++iterations;
231                 if (lu_ref_link_eq(link, scope, source)) {
232                         if (iterations > lu_ref_chain_max_length) {
233                                 CWARN("Long lu_ref chain %d \"%s\":%p\n",
234                                       iterations, scope, source);
235                                 lu_ref_chain_max_length = iterations * 3 / 2;
236                         }
237                         return link;
238                 }
239         }
240         return NULL;
241 }
242
243 void lu_ref_del(struct lu_ref *ref, const char *scope, const void *source)
244 {
245         struct lu_ref_link *link;
246
247         spin_lock(&ref->lf_guard);
248         link = lu_ref_find(ref, scope, source);
249         if (link) {
250                 list_del(&link->ll_linkage);
251                 ref->lf_refs--;
252                 spin_unlock(&ref->lf_guard);
253                 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
254         } else {
255                 REFASSERT(ref, ref->lf_failed > 0);
256                 ref->lf_failed--;
257                 spin_unlock(&ref->lf_guard);
258         }
259 }
260 EXPORT_SYMBOL(lu_ref_del);
261
262 void lu_ref_set_at(struct lu_ref *ref, struct lu_ref_link *link,
263                    const char *scope,
264                    const void *source0, const void *source1)
265 {
266         spin_lock(&ref->lf_guard);
267         REFASSERT(ref, !IS_ERR_OR_NULL(link));
268         REFASSERT(ref, link->ll_ref == ref);
269         REFASSERT(ref, lu_ref_link_eq(link, scope, source0));
270         link->ll_source = source1;
271         spin_unlock(&ref->lf_guard);
272 }
273 EXPORT_SYMBOL(lu_ref_set_at);
274
275 void lu_ref_del_at(struct lu_ref *ref, struct lu_ref_link *link,
276                    const char *scope, const void *source)
277 {
278         spin_lock(&ref->lf_guard);
279         REFASSERT(ref, !IS_ERR_OR_NULL(link));
280         REFASSERT(ref, link->ll_ref == ref);
281         REFASSERT(ref, lu_ref_link_eq(link, scope, source));
282         list_del(&link->ll_linkage);
283         ref->lf_refs--;
284         spin_unlock(&ref->lf_guard);
285 }
286 EXPORT_SYMBOL(lu_ref_del_at);
287
288 static void *lu_ref_seq_start(struct seq_file *seq, loff_t *pos)
289 {
290         struct lu_ref *ref = seq->private;
291
292         spin_lock(&lu_ref_refs_guard);
293         if (list_empty(&ref->lf_linkage))
294                 ref = NULL;
295         spin_unlock(&lu_ref_refs_guard);
296
297         return ref;
298 }
299
300 static void *lu_ref_seq_next(struct seq_file *seq, void *p, loff_t *pos)
301 {
302         struct lu_ref *ref = p;
303         struct lu_ref *next;
304
305         LASSERT(seq->private == p);
306         LASSERT(!list_empty(&ref->lf_linkage));
307
308         (*pos)++;
309         spin_lock(&lu_ref_refs_guard);
310         next = list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
311         if (&next->lf_linkage == &lu_ref_refs)
312                 p = NULL;
313         else
314                 list_move(&ref->lf_linkage, &next->lf_linkage);
315         spin_unlock(&lu_ref_refs_guard);
316
317         return p;
318 }
319
320 static void lu_ref_seq_stop(struct seq_file *seq, void *p)
321 {
322         /* Nothing to do */
323 }
324
325
326 static int lu_ref_seq_show(struct seq_file *seq, void *p)
327 {
328         struct lu_ref *ref  = p;
329         struct lu_ref *next;
330
331         spin_lock(&lu_ref_refs_guard);
332         next = list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
333         if ((&next->lf_linkage == &lu_ref_refs) || lu_ref_is_marker(next)) {
334                 spin_unlock(&lu_ref_refs_guard);
335                 return 0;
336         }
337
338         /* print the entry */
339         spin_lock(&next->lf_guard);
340         seq_printf(seq, "lu_ref: %p %d %d %s:%d\n",
341                    next, next->lf_refs, next->lf_failed,
342                    next->lf_func, next->lf_line);
343         if (next->lf_refs > 64) {
344                 seq_puts(seq, "  too many references, skip\n");
345         } else {
346                 struct lu_ref_link *link;
347                 int i = 0;
348
349                 list_for_each_entry(link, &next->lf_list, ll_linkage)
350                         seq_printf(seq, "  #%d link: %s %p\n",
351                                    i++, link->ll_scope, link->ll_source);
352         }
353         spin_unlock(&next->lf_guard);
354         spin_unlock(&lu_ref_refs_guard);
355
356         return 0;
357 }
358
359 static const struct seq_operations lu_ref_seq_ops = {
360         .start  = lu_ref_seq_start,
361         .stop   = lu_ref_seq_stop,
362         .next   = lu_ref_seq_next,
363         .show   = lu_ref_seq_show
364 };
365
366 static int lu_ref_seq_open(struct inode *inode, struct file *file)
367 {
368         struct lu_ref *marker = &lu_ref_marker;
369         int result = 0;
370
371         result = seq_open(file, &lu_ref_seq_ops);
372         if (result == 0) {
373                 spin_lock(&lu_ref_refs_guard);
374                 if (!list_empty(&marker->lf_linkage))
375                         result = -EAGAIN;
376                 else
377                         list_add(&marker->lf_linkage, &lu_ref_refs);
378                 spin_unlock(&lu_ref_refs_guard);
379
380                 if (result == 0) {
381                         struct seq_file *f = file->private_data;
382
383                         f->private = marker;
384                 } else {
385                         seq_release(inode, file);
386                 }
387         }
388
389         return result;
390 }
391
392 static int lu_ref_seq_release(struct inode *inode, struct file *file)
393 {
394         struct seq_file *m = file->private_data;
395         struct lu_ref *ref = m->private;
396
397         spin_lock(&lu_ref_refs_guard);
398         list_del_init(&ref->lf_linkage);
399         spin_unlock(&lu_ref_refs_guard);
400
401         return seq_release(inode, file);
402 }
403
404 static const struct file_operations lu_ref_dump_fops = {
405         .owner          = THIS_MODULE,
406         .open           = lu_ref_seq_open,
407         .read           = seq_read,
408         .llseek         = seq_lseek,
409         .release        = lu_ref_seq_release
410 };
411
412 int lu_ref_global_init(void)
413 {
414         int result;
415
416         CDEBUG(D_CONSOLE,
417                "lu_ref tracking is enabled. Performance isn't.\n");
418
419         result = lu_kmem_init(lu_ref_caches);
420         if (result)
421                 return result;
422
423         debugfs_create_file("lu_refs", 0444, debugfs_lustre_root,
424                             NULL, &lu_ref_dump_fops);
425
426         return result;
427 }
428
429 void lu_ref_global_fini(void)
430 {
431         /* debugfs file gets cleaned up by debugfs_remove_recursive on
432          * debugfs_lustre_root
433          */
434         lu_kmem_fini(lu_ref_caches);
435 }
436
437 #endif /* CONFIG_LUSTRE_DEBUG_LU_REF */