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