1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Debug Implementation.
25 \*****************************************************************************/
27 #include <linux/kmod.h>
29 #include <linux/vmalloc.h>
30 #include <linux/pagemap.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/kthread.h>
34 #include <linux/hardirq.h>
35 #include <linux/interrupt.h>
36 #include <linux/spinlock.h>
37 #include <linux/proc_compat.h>
38 #include <linux/file_compat.h>
39 #include <sys/sysmacros.h>
40 #include <sys/debug.h>
43 #ifdef DEBUG_SUBSYSTEM
44 #undef DEBUG_SUBSYSTEM
47 #define DEBUG_SUBSYSTEM S_DEBUG
49 unsigned long spl_debug_subsys
= ~0;
50 EXPORT_SYMBOL(spl_debug_subsys
);
51 module_param(spl_debug_subsys
, long, 0644);
52 MODULE_PARM_DESC(spl_debug_subsys
, "Subsystem debugging level mask.");
54 unsigned long spl_debug_mask
= (D_EMERG
| D_ERROR
| D_WARNING
| D_CONSOLE
);
55 EXPORT_SYMBOL(spl_debug_mask
);
56 module_param(spl_debug_mask
, long, 0644);
57 MODULE_PARM_DESC(spl_debug_mask
, "Debugging level mask.");
59 unsigned long spl_debug_printk
= D_CANTMASK
;
60 EXPORT_SYMBOL(spl_debug_printk
);
61 module_param(spl_debug_printk
, long, 0644);
62 MODULE_PARM_DESC(spl_debug_printk
, "Console printk level mask.");
64 int spl_debug_mb
= -1;
65 EXPORT_SYMBOL(spl_debug_mb
);
66 module_param(spl_debug_mb
, int, 0644);
67 MODULE_PARM_DESC(spl_debug_mb
, "Total debug buffer size.");
69 unsigned int spl_debug_binary
= 1;
70 EXPORT_SYMBOL(spl_debug_binary
);
72 unsigned int spl_debug_catastrophe
;
73 EXPORT_SYMBOL(spl_debug_catastrophe
);
75 unsigned int spl_debug_panic_on_bug
= 0;
76 EXPORT_SYMBOL(spl_debug_panic_on_bug
);
77 module_param(spl_debug_panic_on_bug
, int, 0644);
78 MODULE_PARM_DESC(spl_debug_panic_on_bug
, "Panic on BUG");
80 static char spl_debug_file_name
[PATH_MAX
];
81 char spl_debug_file_path
[PATH_MAX
] = "/tmp/spl-log";
83 unsigned int spl_console_ratelimit
= 1;
84 EXPORT_SYMBOL(spl_console_ratelimit
);
86 long spl_console_max_delay
;
87 EXPORT_SYMBOL(spl_console_max_delay
);
89 long spl_console_min_delay
;
90 EXPORT_SYMBOL(spl_console_min_delay
);
92 unsigned int spl_console_backoff
= SPL_DEFAULT_BACKOFF
;
93 EXPORT_SYMBOL(spl_console_backoff
);
95 unsigned int spl_debug_stack
;
96 EXPORT_SYMBOL(spl_debug_stack
);
98 static int spl_panic_in_progress
;
100 union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
] __cacheline_aligned
;
101 char *trace_console_buffers
[NR_CPUS
][3];
102 struct rw_semaphore trace_sem
;
103 atomic_t trace_tage_allocated
= ATOMIC_INIT(0);
105 static int spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *);
106 static void trace_fini(void);
109 /* Memory percentage breakdown by type */
110 static unsigned int pages_factor
[TCD_TYPE_MAX
] = {
111 80, /* 80% pages for TCD_TYPE_PROC */
112 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
113 10 /* 10% pages for TCD_TYPE_IRQ */
117 spl_debug_subsys2str(int subsys
)
160 spl_debug_dbg2str(int debug
)
187 spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
)
189 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
194 if (mask
== 0) { /* "0" */
198 } else { /* space-separated tokens */
199 for (i
= 0; i
< 32; i
++) {
202 if ((mask
& bit
) == 0)
206 if (token
== NULL
) /* unused bit */
209 if (len
> 0) { /* separator? */
215 while (*token
!= 0) {
224 /* terminate 'str' */
234 spl_debug_token2mask(int *mask
, const char *str
, int len
, int is_subsys
)
236 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
241 /* match against known tokens */
242 for (i
= 0; i
< 32; i
++) {
246 if (token
== NULL
) /* unused? */
251 if (j
== len
) { /* end of token */
262 if (str
[j
] == token
[j
])
265 if (str
[j
] < 'A' || 'Z' < str
[j
])
268 if (str
[j
] - 'A' + 'a' != token
[j
])
273 return -EINVAL
; /* no match */
277 spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
)
280 int m
= 0, matched
, n
, t
;
282 /* Allow a number for backwards compatibility */
283 for (n
= strlen(str
); n
> 0; n
--)
284 if (!isspace(str
[n
-1]))
288 if ((t
= sscanf(str
, "%i%n", &m
, &matched
)) >= 1 && matched
== n
) {
293 /* <str> must be a list of debug tokens or numbers separated by
294 * whitespace and optionally an operator ('+' or '-'). If an operator
295 * appears first in <str>, '*mask' is used as the starting point
296 * (relative), otherwise 0 is used (absolute). An operator applies to
297 * all following tokens up to the next operator. */
300 while (isspace(*str
)) /* skip whitespace */
306 if (*str
== '+' || *str
== '-') {
309 /* op on first token == relative */
313 while (isspace(*str
)) /* skip whitespace */
316 if (*str
== 0) /* trailing op */
320 /* find token length */
321 for (n
= 0; str
[n
] != 0 && !isspace(str
[n
]); n
++);
324 if (spl_debug_token2mask(&t
, str
, n
, is_subsys
) != 0)
344 spl_debug_dumplog_internal(dumplog_priv_t
*dp
)
348 journal_info
= current
->journal_info
;
349 current
->journal_info
= NULL
;
351 snprintf(spl_debug_file_name
, sizeof(spl_debug_file_path
) - 1,
352 "%s.%ld.%ld", spl_debug_file_path
,
353 get_seconds(), (long)dp
->dp_pid
);
354 printk("SPL: Dumping log to %s\n", spl_debug_file_name
);
355 spl_debug_dump_all_pages(dp
, spl_debug_file_name
);
357 current
->journal_info
= journal_info
;
361 spl_debug_dumplog_thread(void *arg
)
363 dumplog_priv_t
*dp
= (dumplog_priv_t
*)arg
;
365 spl_debug_dumplog_internal(dp
);
366 atomic_set(&dp
->dp_done
, 1);
367 wake_up(&dp
->dp_waitq
);
368 complete_and_exit(NULL
, 0);
370 return 0; /* Unreachable */
373 /* When flag is set do not use a new thread for the debug dump */
375 spl_debug_dumplog(int flags
)
377 struct task_struct
*tsk
;
380 init_waitqueue_head(&dp
.dp_waitq
);
381 dp
.dp_pid
= current
->pid
;
383 atomic_set(&dp
.dp_done
, 0);
385 if (dp
.dp_flags
& DL_NOTHREAD
) {
386 spl_debug_dumplog_internal(&dp
);
389 tsk
= kthread_create(spl_debug_dumplog_thread
,(void *)&dp
,"spl_debug");
393 wake_up_process(tsk
);
394 wait_event(dp
.dp_waitq
, atomic_read(&dp
.dp_done
));
399 EXPORT_SYMBOL(spl_debug_dumplog
);
402 trace_get_console_buffer(void)
409 } else if (in_softirq()) {
415 return trace_console_buffers
[cpu
][idx
];
419 trace_put_console_buffer(char *buffer
)
425 trace_lock_tcd(struct trace_cpu_data
*tcd
)
427 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
429 spin_lock_irqsave(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
435 trace_unlock_tcd(struct trace_cpu_data
*tcd
)
437 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
439 spin_unlock_irqrestore(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
442 static struct trace_cpu_data
*
446 struct trace_cpu_data
*tcd
;
450 tcd
= &(*trace_data
[TCD_TYPE_IRQ
])[cpu
].tcd
;
451 else if (in_softirq())
452 tcd
= &(*trace_data
[TCD_TYPE_SOFTIRQ
])[cpu
].tcd
;
454 tcd
= &(*trace_data
[TCD_TYPE_PROC
])[cpu
].tcd
;
462 trace_put_tcd (struct trace_cpu_data
*tcd
)
464 trace_unlock_tcd(tcd
);
470 trace_set_debug_header(struct spl_debug_header
*header
, int subsys
,
471 int mask
, const int line
, unsigned long stack
)
475 do_gettimeofday(&tv
);
477 header
->ph_subsys
= subsys
;
478 header
->ph_mask
= mask
;
479 header
->ph_cpu_id
= smp_processor_id();
480 header
->ph_sec
= (__u32
)tv
.tv_sec
;
481 header
->ph_usec
= tv
.tv_usec
;
482 header
->ph_stack
= stack
;
483 header
->ph_pid
= current
->pid
;
484 header
->ph_line_num
= line
;
490 trace_print_to_console(struct spl_debug_header
*hdr
, int mask
, const char *buf
,
491 int len
, const char *file
, const char *fn
)
493 char *prefix
= "SPL", *ptype
= NULL
;
495 if ((mask
& D_EMERG
) != 0) {
498 } else if ((mask
& D_ERROR
) != 0) {
501 } else if ((mask
& D_WARNING
) != 0) {
503 ptype
= KERN_WARNING
;
504 } else if ((mask
& (D_CONSOLE
| spl_debug_printk
)) != 0) {
509 if ((mask
& D_CONSOLE
) != 0) {
510 printk("%s%s: %.*s", ptype
, prefix
, len
, buf
);
512 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype
, prefix
,
513 hdr
->ph_pid
, hdr
->ph_stack
, file
,
514 hdr
->ph_line_num
, fn
, len
, buf
);
521 trace_max_debug_mb(void)
523 return MAX(512, ((num_physpages
>> (20 - PAGE_SHIFT
)) * 80) / 100);
526 static struct trace_page
*
530 struct trace_page
*tage
;
532 page
= alloc_pages(gfp
| __GFP_NOWARN
, 0);
536 tage
= kmalloc(sizeof(*tage
), gfp
);
538 __free_pages(page
, 0);
543 atomic_inc(&trace_tage_allocated
);
549 tage_free(struct trace_page
*tage
)
551 __ASSERT(tage
!= NULL
);
552 __ASSERT(tage
->page
!= NULL
);
554 __free_pages(tage
->page
, 0);
556 atomic_dec(&trace_tage_allocated
);
559 static struct trace_page
*
560 tage_from_list(struct list_head
*list
)
562 return list_entry(list
, struct trace_page
, linkage
);
566 tage_to_tail(struct trace_page
*tage
, struct list_head
*queue
)
568 __ASSERT(tage
!= NULL
);
569 __ASSERT(queue
!= NULL
);
571 list_move_tail(&tage
->linkage
, queue
);
574 /* try to return a page that has 'len' bytes left at the end */
575 static struct trace_page
*
576 trace_get_tage_try(struct trace_cpu_data
*tcd
, unsigned long len
)
578 struct trace_page
*tage
;
580 if (tcd
->tcd_cur_pages
> 0) {
581 __ASSERT(!list_empty(&tcd
->tcd_pages
));
582 tage
= tage_from_list(tcd
->tcd_pages
.prev
);
583 if (tage
->used
+ len
<= PAGE_SIZE
)
587 if (tcd
->tcd_cur_pages
< tcd
->tcd_max_pages
) {
588 if (tcd
->tcd_cur_stock_pages
> 0) {
589 tage
= tage_from_list(tcd
->tcd_stock_pages
.prev
);
590 tcd
->tcd_cur_stock_pages
--;
591 list_del_init(&tage
->linkage
);
593 tage
= tage_alloc(GFP_ATOMIC
);
596 "failure to allocate a tage (%ld)\n",
603 tage
->cpu
= smp_processor_id();
604 tage
->type
= tcd
->tcd_type
;
605 list_add_tail(&tage
->linkage
, &tcd
->tcd_pages
);
606 tcd
->tcd_cur_pages
++;
614 /* return a page that has 'len' bytes left at the end */
615 static struct trace_page
*
616 trace_get_tage(struct trace_cpu_data
*tcd
, unsigned long len
)
618 struct trace_page
*tage
;
620 __ASSERT(len
<= PAGE_SIZE
);
622 tage
= trace_get_tage_try(tcd
, len
);
626 if (tcd
->tcd_cur_pages
> 0) {
627 tage
= tage_from_list(tcd
->tcd_pages
.next
);
629 tage_to_tail(tage
, &tcd
->tcd_pages
);
636 spl_debug_vmsg(spl_debug_limit_state_t
*cdls
, int subsys
, int mask
,
637 const char *file
, const char *fn
, const int line
,
638 const char *format1
, va_list args
, const char *format2
, ...)
640 struct trace_cpu_data
*tcd
= NULL
;
641 struct spl_debug_header header
= { 0, };
642 struct trace_page
*tage
;
643 /* string_buf is used only if tcd != NULL, and is always set then */
644 char *string_buf
= NULL
;
647 int needed
= 85; /* average message length */
653 if (strchr(file
, '/'))
654 file
= strrchr(file
, '/') + 1;
656 trace_set_debug_header(&header
, subsys
, mask
, line
, CDEBUG_STACK());
658 tcd
= trace_get_tcd();
662 if (tcd
->tcd_shutting_down
) {
668 known_size
= strlen(file
) + 1;
670 known_size
+= strlen(fn
) + 1;
672 if (spl_debug_binary
)
673 known_size
+= sizeof(header
);
675 /* '2' used because vsnprintf returns real size required for output
676 * _without_ terminating NULL. */
677 for (i
= 0; i
< 2; i
++) {
678 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
680 if (needed
+ known_size
> PAGE_SIZE
)
688 string_buf
= (char *)page_address(tage
->page
) +
689 tage
->used
+ known_size
;
691 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
693 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
703 needed
= vsnprintf(string_buf
, max_nob
, format1
, ap
);
708 remain
= max_nob
- needed
;
712 va_start(ap
, format2
);
713 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
717 if (needed
< max_nob
)
721 header
.ph_len
= known_size
+ needed
;
722 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
724 if (spl_debug_binary
) {
725 memcpy(debug_buf
, &header
, sizeof(header
));
726 tage
->used
+= sizeof(header
);
727 debug_buf
+= sizeof(header
);
730 strcpy(debug_buf
, file
);
731 tage
->used
+= strlen(file
) + 1;
732 debug_buf
+= strlen(file
) + 1;
735 strcpy(debug_buf
, fn
);
736 tage
->used
+= strlen(fn
) + 1;
737 debug_buf
+= strlen(fn
) + 1;
740 __ASSERT(debug_buf
== string_buf
);
742 tage
->used
+= needed
;
743 __ASSERT (tage
->used
<= PAGE_SIZE
);
746 if ((mask
& spl_debug_printk
) == 0) {
747 /* no console output requested */
754 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
755 !time_before(cdls
->cdls_next
, jiffies
)) {
756 /* skipping a console message */
763 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
764 (10 * HZ
), jiffies
)) {
765 /* last timeout was a long time ago */
766 cdls
->cdls_delay
/= spl_console_backoff
* 4;
768 cdls
->cdls_delay
*= spl_console_backoff
;
770 if (cdls
->cdls_delay
< spl_console_min_delay
)
771 cdls
->cdls_delay
= spl_console_min_delay
;
772 else if (cdls
->cdls_delay
> spl_console_max_delay
)
773 cdls
->cdls_delay
= spl_console_max_delay
;
776 /* ensure cdls_next is never zero after it's been seen */
777 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
781 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
784 string_buf
= trace_get_console_buffer();
787 if (format1
!= NULL
) {
789 needed
= vsnprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
, format1
, ap
);
792 if (format2
!= NULL
) {
793 remain
= TRACE_CONSOLE_BUFFER_SIZE
- needed
;
795 va_start(ap
, format2
);
796 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
800 trace_print_to_console(&header
, mask
,
801 string_buf
, needed
, file
, fn
);
803 trace_put_console_buffer(string_buf
);
806 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
807 string_buf
= trace_get_console_buffer();
809 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
810 "Skipped %d previous similar message%s\n",
811 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
813 trace_print_to_console(&header
, mask
,
814 string_buf
, needed
, file
, fn
);
816 trace_put_console_buffer(string_buf
);
817 cdls
->cdls_count
= 0;
822 EXPORT_SYMBOL(spl_debug_vmsg
);
824 /* Do the collect_pages job on a single CPU: assumes that all other
825 * CPUs have been stopped during a panic. If this isn't true for
826 * some arch, this will have to be implemented separately in each arch.
829 collect_pages_from_single_cpu(struct page_collection
*pc
)
831 struct trace_cpu_data
*tcd
;
834 tcd_for_each(tcd
, i
, j
) {
835 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
836 tcd
->tcd_cur_pages
= 0;
841 collect_pages_on_all_cpus(struct page_collection
*pc
)
843 struct trace_cpu_data
*tcd
;
846 spin_lock(&pc
->pc_lock
);
847 for_each_possible_cpu(cpu
) {
848 tcd_for_each_type_lock(tcd
, i
, cpu
) {
849 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
850 tcd
->tcd_cur_pages
= 0;
853 spin_unlock(&pc
->pc_lock
);
857 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
859 INIT_LIST_HEAD(&pc
->pc_pages
);
861 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
862 collect_pages_from_single_cpu(pc
);
864 collect_pages_on_all_cpus(pc
);
868 put_pages_back_on_all_cpus(struct page_collection
*pc
)
870 struct trace_cpu_data
*tcd
;
871 struct list_head
*cur_head
;
872 struct trace_page
*tage
;
873 struct trace_page
*tmp
;
876 spin_lock(&pc
->pc_lock
);
878 for_each_possible_cpu(cpu
) {
879 tcd_for_each_type_lock(tcd
, i
, cpu
) {
880 cur_head
= tcd
->tcd_pages
.next
;
882 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
885 __ASSERT_TAGE_INVARIANT(tage
);
887 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
890 tage_to_tail(tage
, cur_head
);
891 tcd
->tcd_cur_pages
++;
896 spin_unlock(&pc
->pc_lock
);
900 put_pages_back(struct page_collection
*pc
)
902 if (!spl_panic_in_progress
)
903 put_pages_back_on_all_cpus(pc
);
907 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
909 struct page_collection pc
;
911 struct trace_page
*tage
;
912 struct trace_page
*tmp
;
916 down_write(&trace_sem
);
918 filp
= spl_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
922 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
927 spin_lock_init(&pc
.pc_lock
);
928 collect_pages(dp
, &pc
);
929 if (list_empty(&pc
.pc_pages
)) {
937 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
938 __ASSERT_TAGE_INVARIANT(tage
);
940 rc
= spl_filp_write(filp
, page_address(tage
->page
),
941 tage
->used
, spl_filp_poff(filp
));
942 if (rc
!= (int)tage
->used
) {
943 printk(KERN_WARNING
"SPL: Wanted to write %u "
944 "but wrote %d\n", tage
->used
, rc
);
946 __ASSERT(list_empty(&pc
.pc_pages
));
949 list_del(&tage
->linkage
);
955 rc
= spl_filp_fsync(filp
, 1);
957 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
959 spl_filp_close(filp
);
961 up_write(&trace_sem
);
967 spl_debug_flush_pages(void)
970 struct page_collection pc
;
971 struct trace_page
*tage
;
972 struct trace_page
*tmp
;
974 spin_lock_init(&pc
.pc_lock
);
975 init_waitqueue_head(&dp
.dp_waitq
);
976 dp
.dp_pid
= current
->pid
;
978 atomic_set(&dp
.dp_done
, 0);
980 collect_pages(&dp
, &pc
);
981 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
982 __ASSERT_TAGE_INVARIANT(tage
);
983 list_del(&tage
->linkage
);
989 spl_debug_set_mask(unsigned long mask
) {
990 spl_debug_mask
= mask
;
993 EXPORT_SYMBOL(spl_debug_set_mask
);
996 spl_debug_get_mask(void) {
997 return spl_debug_mask
;
999 EXPORT_SYMBOL(spl_debug_get_mask
);
1002 spl_debug_set_subsys(unsigned long subsys
) {
1003 spl_debug_subsys
= subsys
;
1006 EXPORT_SYMBOL(spl_debug_set_subsys
);
1009 spl_debug_get_subsys(void) {
1010 return spl_debug_subsys
;
1012 EXPORT_SYMBOL(spl_debug_get_subsys
);
1015 spl_debug_set_mb(int mb
)
1018 int limit
= trace_max_debug_mb();
1019 struct trace_cpu_data
*tcd
;
1021 if (mb
< num_possible_cpus()) {
1022 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1023 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1028 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1029 "%dMB - upper limit is %d\n", mb
, limit
);
1033 mb
/= num_possible_cpus();
1034 pages
= mb
<< (20 - PAGE_SHIFT
);
1036 down_write(&trace_sem
);
1038 tcd_for_each(tcd
, i
, j
)
1039 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1041 up_write(&trace_sem
);
1045 EXPORT_SYMBOL(spl_debug_set_mb
);
1048 spl_debug_get_mb(void)
1051 struct trace_cpu_data
*tcd
;
1052 int total_pages
= 0;
1054 down_read(&trace_sem
);
1056 tcd_for_each(tcd
, i
, j
)
1057 total_pages
+= tcd
->tcd_max_pages
;
1059 up_read(&trace_sem
);
1061 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1063 EXPORT_SYMBOL(spl_debug_get_mb
);
1065 void spl_debug_dumpstack(struct task_struct
*tsk
)
1067 extern void show_task(struct task_struct
*);
1072 printk("SPL: Showing stack for process %d\n", tsk
->pid
);
1075 EXPORT_SYMBOL(spl_debug_dumpstack
);
1077 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1079 spl_debug_catastrophe
= 1;
1080 spl_debug_msg(NULL
, 0, D_EMERG
, file
, func
, line
, "SBUG\n");
1082 if (in_interrupt()) {
1083 panic("SBUG in interrupt.\n");
1087 if (in_atomic() || irqs_disabled())
1088 flags
|= DL_NOTHREAD
;
1090 /* Ensure all debug pages and dumped by current cpu */
1091 if (spl_debug_panic_on_bug
)
1092 spl_panic_in_progress
= 1;
1094 spl_debug_dumpstack(NULL
);
1095 spl_debug_dumplog(flags
);
1097 if (spl_debug_panic_on_bug
)
1100 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1104 EXPORT_SYMBOL(spl_debug_bug
);
1107 spl_debug_clear_buffer(void)
1109 spl_debug_flush_pages();
1112 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1115 spl_debug_mark_buffer(char *text
)
1117 CDEBUG(D_WARNING
, "*************************************\n");
1118 CDEBUG(D_WARNING
, "DEBUG MARKER: %s\n", text
);
1119 CDEBUG(D_WARNING
, "*************************************\n");
1123 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1126 trace_init(int max_pages
)
1128 struct trace_cpu_data
*tcd
;
1131 init_rwsem(&trace_sem
);
1133 /* initialize trace_data */
1134 memset(trace_data
, 0, sizeof(trace_data
));
1135 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1136 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1137 NR_CPUS
, GFP_KERNEL
);
1138 if (trace_data
[i
] == NULL
)
1142 tcd_for_each(tcd
, i
, j
) {
1143 spin_lock_init(&tcd
->tcd_lock
);
1144 tcd
->tcd_pages_factor
= pages_factor
[i
];
1147 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1148 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1149 tcd
->tcd_cur_pages
= 0;
1150 tcd
->tcd_cur_stock_pages
= 0;
1151 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1152 tcd
->tcd_shutting_down
= 0;
1155 for (i
= 0; i
< num_possible_cpus(); i
++) {
1156 for (j
= 0; j
< 3; j
++) {
1157 trace_console_buffers
[i
][j
] =
1158 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1161 if (trace_console_buffers
[i
][j
] == NULL
)
1169 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1176 int rc
, max
= spl_debug_mb
;
1178 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1179 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1181 /* If spl_debug_mb is set to an invalid value or uninitialized
1182 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1183 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1184 max
>= 512 || max
< 0) {
1185 max
= TCD_MAX_PAGES
;
1187 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1190 rc
= trace_init(max
);
1198 trace_cleanup_on_all_cpus(void)
1200 struct trace_cpu_data
*tcd
;
1201 struct trace_page
*tage
;
1202 struct trace_page
*tmp
;
1205 for_each_possible_cpu(cpu
) {
1206 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1207 tcd
->tcd_shutting_down
= 1;
1209 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1211 __ASSERT_TAGE_INVARIANT(tage
);
1213 list_del(&tage
->linkage
);
1216 tcd
->tcd_cur_pages
= 0;
1226 trace_cleanup_on_all_cpus();
1228 for (i
= 0; i
< num_possible_cpus(); i
++) {
1229 for (j
= 0; j
< 3; j
++) {
1230 if (trace_console_buffers
[i
][j
] != NULL
) {
1231 kfree(trace_console_buffers
[i
][j
]);
1232 trace_console_buffers
[i
][j
] = NULL
;
1237 for (i
= 0; i
< TCD_TYPE_MAX
&& trace_data
[i
] != NULL
; i
++) {
1238 kfree(trace_data
[i
]);
1239 trace_data
[i
] = NULL
;