Whamcloud - gitweb
LU-8066 libcfs: test for both __kernel_param_[un]lock and kernel_param_[un]lock
[fs/lustre-release.git] / libcfs / libcfs / debug.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  *
36  */
37
38 # define DEBUG_SUBSYSTEM S_LNET
39
40 #include <linux/kthread.h>
41 #include <libcfs/libcfs.h>
42 #include "tracefile.h"
43
44 static char debug_file_name[1024];
45
46 unsigned int libcfs_subsystem_debug = ~0;
47 module_param(libcfs_subsystem_debug, int, 0644);
48 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
49 EXPORT_SYMBOL(libcfs_subsystem_debug);
50
51 unsigned int libcfs_debug = (D_CANTMASK |
52                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL | D_LFSCK);
53 module_param(libcfs_debug, int, 0644);
54 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
55 EXPORT_SYMBOL(libcfs_debug);
56
57 static int libcfs_param_debug_mb_set(const char *val,
58                                      cfs_kernel_param_arg_t *kp)
59 {
60         int rc;
61         unsigned int num;
62
63         rc = kstrtouint(val, 0, &num);
64         if (rc < 0)
65                 return rc;
66
67 /*
68  * RHEL6 does not support any kind of locking so we have to provide
69  * our own
70  */
71 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
72         kernel_param_lock(THIS_MODULE);
73 #endif
74         if (!*((unsigned int *)kp->arg)) {
75                 *((unsigned int *)kp->arg) = num;
76                 return 0;
77         }
78
79         rc = cfs_trace_set_debug_mb(num);
80
81         if (!rc)
82                 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
83
84 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
85         kernel_param_unlock(THIS_MODULE);
86 #endif
87         return rc;
88 }
89
90 /* While debug_mb setting look like unsigned int, in fact
91  * it needs quite a bunch of extra processing, so we define special
92  * debug_mb parameter type with corresponding methods to handle this case
93  */
94 static struct kernel_param_ops param_ops_debug_mb = {
95         .set = libcfs_param_debug_mb_set,
96         .get = param_get_uint,
97 };
98
99 #define param_check_debug_mb(name, p) \
100                 __param_check(name, p, unsigned int)
101
102 static unsigned int libcfs_debug_mb;
103 #ifdef HAVE_KERNEL_PARAM_OPS
104 module_param(libcfs_debug_mb, debug_mb, 0644);
105 #else
106 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
107                   &param_ops_debug_mb, 0644);
108 #endif
109 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
110
111 unsigned int libcfs_printk = D_CANTMASK;
112 module_param(libcfs_printk, uint, 0644);
113 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
114
115 unsigned int libcfs_console_ratelimit = 1;
116 module_param(libcfs_console_ratelimit, uint, 0644);
117 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
118
119 static int param_set_delay_minmax(const char *val,
120                                   cfs_kernel_param_arg_t *kp,
121                                   long min, long max)
122 {
123         long d;
124         int sec;
125         int rc;
126
127         rc = kstrtoint(val, 0, &sec);
128         if (rc)
129                 return -EINVAL;
130
131         /* The sysfs setting is in centiseconds */
132         d = cfs_time_seconds(sec) / 100;
133         if (d < min || d > max)
134                 return -EINVAL;
135
136         *((unsigned int *)kp->arg) = d;
137
138         return 0;
139 }
140
141 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
142 {
143         unsigned int d = *(unsigned int *)kp->arg;
144
145         return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
146 }
147
148 unsigned int libcfs_console_max_delay;
149 unsigned int libcfs_console_min_delay;
150
151 static int param_set_console_max_delay(const char *val,
152                                        cfs_kernel_param_arg_t *kp)
153 {
154         return param_set_delay_minmax(val, kp,
155                                       libcfs_console_min_delay, INT_MAX);
156 }
157
158 static struct kernel_param_ops param_ops_console_max_delay = {
159         .set = param_set_console_max_delay,
160         .get = param_get_delay,
161 };
162
163 #define param_check_console_max_delay(name, p) \
164                 __param_check(name, p, unsigned int)
165
166 #ifdef HAVE_KERNEL_PARAM_OPS
167 module_param(libcfs_console_max_delay, console_max_delay, 0644);
168 #else
169 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
170                   param_get_delay, &param_ops_console_max_delay, 0644);
171 #endif
172 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
173
174 static int param_set_console_min_delay(const char *val,
175                                        cfs_kernel_param_arg_t *kp)
176 {
177         return param_set_delay_minmax(val, kp,
178                                       1, libcfs_console_max_delay);
179 }
180
181 static struct kernel_param_ops param_ops_console_min_delay = {
182         .set = param_set_console_min_delay,
183         .get = param_get_delay,
184 };
185
186 #define param_check_console_min_delay(name, p) \
187                 __param_check(name, p, unsigned int)
188
189 #ifdef HAVE_KERNEL_PARAM_OPS
190 module_param(libcfs_console_min_delay, console_min_delay, 0644);
191 #else
192 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
193                   param_get_delay, &param_ops_console_min_delay, 0644);
194 #endif
195 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
196
197 static int param_set_uint_minmax(const char *val,
198                                  cfs_kernel_param_arg_t *kp,
199                                  unsigned int min, unsigned int max)
200 {
201         unsigned int num;
202         int ret;
203
204         if (!val)
205                 return -EINVAL;
206
207         ret = kstrtouint(val, 0, &num);
208         if (ret < 0 || num < min || num > max)
209                 return -EINVAL;
210
211         *((unsigned int *)kp->arg) = num;
212         return 0;
213 }
214
215 static int param_set_uintpos(const char *val,
216                              cfs_kernel_param_arg_t *kp)
217 {
218         return param_set_uint_minmax(val, kp, 1, -1);
219 }
220
221 static struct kernel_param_ops param_ops_uintpos = {
222         .set = param_set_uintpos,
223         .get = param_get_uint,
224 };
225
226 #define param_check_uintpos(name, p) \
227                 __param_check(name, p, unsigned int)
228
229 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
230 #ifdef HAVE_KERNEL_PARAM_OPS
231 module_param(libcfs_console_backoff, uintpos, 0644);
232 #else
233 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
234                   &param_ops_uintpos, 0644);
235 #endif
236 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
237
238 unsigned int libcfs_debug_binary = 1;
239
240 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
241 EXPORT_SYMBOL(libcfs_stack);
242
243 unsigned int libcfs_catastrophe;
244 EXPORT_SYMBOL(libcfs_catastrophe);
245
246 unsigned int libcfs_watchdog_ratelimit = 300;
247
248 unsigned int libcfs_panic_on_lbug = 1;
249 module_param(libcfs_panic_on_lbug, uint, 0644);
250 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
251
252 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
253 EXPORT_SYMBOL(libcfs_kmemory);
254
255 static wait_queue_head_t debug_ctlwq;
256
257 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
258 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
259
260 /* We need to pass a pointer here, but elsewhere this must be a const */
261 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
262 module_param(libcfs_debug_file_path, charp, 0644);
263 MODULE_PARM_DESC(libcfs_debug_file_path,
264                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
265
266 int libcfs_panic_in_progress;
267
268 /* libcfs_debug_token2mask() expects the returned
269  * string in lower-case */
270 static const char *libcfs_debug_subsys2str(int subsys)
271 {
272         static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
273
274         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
275                 return NULL;
276
277         return libcfs_debug_subsystems[subsys];
278 }
279
280 /* libcfs_debug_token2mask() expects the returned
281  * string in lower-case */
282 static const char *libcfs_debug_dbg2str(int debug)
283 {
284         static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
285
286         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
287                 return NULL;
288
289         return libcfs_debug_masks[debug];
290 }
291
292 int
293 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
294 {
295         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
296                                                  libcfs_debug_dbg2str;
297         int           len = 0;
298         const char   *token;
299         int           i;
300
301         if (mask == 0) {                        /* "0" */
302                 if (size > 0)
303                         str[0] = '0';
304                 len = 1;
305         } else {                                /* space-separated tokens */
306                 for (i = 0; i < 32; i++) {
307                         if ((mask & (1 << i)) == 0)
308                                 continue;
309
310                         token = fn(i);
311                         if (token == NULL)              /* unused bit */
312                                 continue;
313
314                         if (len > 0) {                  /* separator? */
315                                 if (len < size)
316                                         str[len] = ' ';
317                                 len++;
318                         }
319
320                         while (*token != 0) {
321                                 if (len < size)
322                                         str[len] = *token;
323                                 token++;
324                                 len++;
325                         }
326                 }
327         }
328
329         /* terminate 'str' */
330         if (len < size)
331                 str[len] = 0;
332         else
333                 str[size - 1] = 0;
334
335         return len;
336 }
337
338 int
339 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
340 {
341         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
342                                                  libcfs_debug_dbg2str;
343         int         m = 0;
344         int         matched;
345         int         n;
346         int         t;
347
348         /* Allow a number for backwards compatibility */
349
350         for (n = strlen(str); n > 0; n--)
351                 if (!isspace(str[n-1]))
352                         break;
353         matched = n;
354
355         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
356             matched == n) {
357                 /* don't print warning for lctl set_param debug=0 or -1 */
358                 if (m != 0 && m != -1)
359                         CWARN("You are trying to use a numerical value for the "
360                               "mask - this will be deprecated in a future "
361                               "release.\n");
362                 *mask = m;
363                 return 0;
364         }
365
366         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
367                             0xffffffff);
368 }
369
370 /**
371  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
372  */
373 void libcfs_debug_dumplog_internal(void *arg)
374 {
375         static time64_t last_dump_time;
376         time64_t current_time;
377         void *journal_info;
378
379         journal_info = current->journal_info;
380         current->journal_info = NULL;
381         current_time = ktime_get_real_seconds();
382
383         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
384             current_time > last_dump_time) {
385                 last_dump_time = current_time;
386                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
387                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
388                          (s64)current_time, (uintptr_t)arg);
389                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
390                        debug_file_name);
391                 cfs_tracefile_dump_all_pages(debug_file_name);
392                 libcfs_run_debug_log_upcall(debug_file_name);
393         }
394         current->journal_info = journal_info;
395 }
396
397 static int libcfs_debug_dumplog_thread(void *arg)
398 {
399         libcfs_debug_dumplog_internal(arg);
400         wake_up(&debug_ctlwq);
401         return 0;
402 }
403
404 void libcfs_debug_dumplog(void)
405 {
406         wait_queue_t wait;
407         struct task_struct    *dumper;
408         ENTRY;
409
410         /* we're being careful to ensure that the kernel thread is
411          * able to set our state to running as it exits before we
412          * get to schedule() */
413         init_waitqueue_entry(&wait, current);
414         set_current_state(TASK_INTERRUPTIBLE);
415         add_wait_queue(&debug_ctlwq, &wait);
416
417         dumper = kthread_run(libcfs_debug_dumplog_thread,
418                              (void *)(long)current_pid(),
419                              "libcfs_debug_dumper");
420         if (IS_ERR(dumper))
421                 printk(KERN_ERR "LustreError: cannot start log dump thread:"
422                        " %ld\n", PTR_ERR(dumper));
423         else
424                 schedule();
425
426         /* be sure to teardown if cfs_create_thread() failed */
427         remove_wait_queue(&debug_ctlwq, &wait);
428         set_current_state(TASK_RUNNING);
429 }
430 EXPORT_SYMBOL(libcfs_debug_dumplog);
431
432 int libcfs_debug_init(unsigned long bufsize)
433 {
434         int    rc = 0;
435         unsigned int max = libcfs_debug_mb;
436
437         init_waitqueue_head(&debug_ctlwq);
438
439         if (libcfs_console_max_delay <= 0 || /* not set by user or */
440             libcfs_console_min_delay <= 0 || /* set to invalid values */
441             libcfs_console_min_delay >= libcfs_console_max_delay) {
442                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
443                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
444         }
445
446         if (libcfs_debug_file_path != NULL) {
447                 strlcpy(libcfs_debug_file_path_arr,
448                         libcfs_debug_file_path,
449                         sizeof(libcfs_debug_file_path_arr));
450         }
451
452         /* If libcfs_debug_mb is set to an invalid value or uninitialized
453          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
454         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
455                 max = TCD_MAX_PAGES;
456         } else {
457                 max = (max / num_possible_cpus());
458                 max = (max << (20 - PAGE_SHIFT));
459         }
460
461         rc = cfs_tracefile_init(max);
462         if (rc)
463                 return rc;
464
465         libcfs_register_panic_notifier();
466         kernel_param_lock(THIS_MODULE);
467         libcfs_debug_mb = cfs_trace_get_debug_mb();
468         kernel_param_unlock(THIS_MODULE);
469         return rc;
470 }
471
472 int libcfs_debug_cleanup(void)
473 {
474         libcfs_unregister_panic_notifier();
475         kernel_param_lock(THIS_MODULE);
476         cfs_tracefile_exit();
477         kernel_param_unlock(THIS_MODULE);
478         return 0;
479 }
480
481 int libcfs_debug_clear_buffer(void)
482 {
483         cfs_trace_flush_pages();
484         return 0;
485 }
486
487 /* Debug markers, although printed by S_LNET
488  * should not be be marked as such. */
489 #undef DEBUG_SUBSYSTEM
490 #define DEBUG_SUBSYSTEM S_UNDEFINED
491 int libcfs_debug_mark_buffer(const char *text)
492 {
493         CDEBUG(D_TRACE,"***************************************************\n");
494         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
495         CDEBUG(D_TRACE,"***************************************************\n");
496
497         return 0;
498 }
499 #undef DEBUG_SUBSYSTEM
500 #define DEBUG_SUBSYSTEM S_LNET
501
502 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
503 {
504         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
505                          rc, rc, rc);
506         return rc;
507 }
508 EXPORT_SYMBOL(libcfs_log_return);
509
510 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
511                      long rc)
512 {
513         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
514                          " : %#lx)\n", label, rc, rc, rc);
515 }
516 EXPORT_SYMBOL(libcfs_log_goto);