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 <spl-debug.h>
41 #include <spl-trace.h>
44 #ifdef SS_DEBUG_SUBSYS
45 #undef SS_DEBUG_SUBSYS
48 #define SS_DEBUG_SUBSYS SS_DEBUG
50 /* Debug log support enabled */
53 unsigned long spl_debug_subsys
= ~0;
54 EXPORT_SYMBOL(spl_debug_subsys
);
55 module_param(spl_debug_subsys
, ulong
, 0644);
56 MODULE_PARM_DESC(spl_debug_subsys
, "Subsystem debugging level mask.");
58 unsigned long spl_debug_mask
= SD_CANTMASK
;
59 EXPORT_SYMBOL(spl_debug_mask
);
60 module_param(spl_debug_mask
, ulong
, 0644);
61 MODULE_PARM_DESC(spl_debug_mask
, "Debugging level mask.");
63 unsigned long spl_debug_printk
= SD_CANTMASK
;
64 EXPORT_SYMBOL(spl_debug_printk
);
65 module_param(spl_debug_printk
, ulong
, 0644);
66 MODULE_PARM_DESC(spl_debug_printk
, "Console printk level mask.");
68 int spl_debug_mb
= -1;
69 EXPORT_SYMBOL(spl_debug_mb
);
70 module_param(spl_debug_mb
, int, 0644);
71 MODULE_PARM_DESC(spl_debug_mb
, "Total debug buffer size.");
73 unsigned int spl_debug_binary
= 1;
74 EXPORT_SYMBOL(spl_debug_binary
);
76 unsigned int spl_debug_catastrophe
;
77 EXPORT_SYMBOL(spl_debug_catastrophe
);
79 unsigned int spl_debug_panic_on_bug
= 0;
80 EXPORT_SYMBOL(spl_debug_panic_on_bug
);
81 module_param(spl_debug_panic_on_bug
, uint
, 0644);
82 MODULE_PARM_DESC(spl_debug_panic_on_bug
, "Panic on BUG");
84 static char spl_debug_file_name
[PATH_MAX
];
85 char spl_debug_file_path
[PATH_MAX
] = "/tmp/spl-log";
87 unsigned int spl_console_ratelimit
= 1;
88 EXPORT_SYMBOL(spl_console_ratelimit
);
90 long spl_console_max_delay
;
91 EXPORT_SYMBOL(spl_console_max_delay
);
93 long spl_console_min_delay
;
94 EXPORT_SYMBOL(spl_console_min_delay
);
96 unsigned int spl_console_backoff
= SPL_DEFAULT_BACKOFF
;
97 EXPORT_SYMBOL(spl_console_backoff
);
99 unsigned int spl_debug_stack
;
100 EXPORT_SYMBOL(spl_debug_stack
);
102 static int spl_panic_in_progress
;
104 union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
] __cacheline_aligned
;
105 char *trace_console_buffers
[NR_CPUS
][3];
106 struct rw_semaphore trace_sem
;
107 atomic_t trace_tage_allocated
= ATOMIC_INIT(0);
109 static int spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *);
110 static void trace_fini(void);
113 /* Memory percentage breakdown by type */
114 static unsigned int pages_factor
[TCD_TYPE_MAX
] = {
115 80, /* 80% pages for TCD_TYPE_PROC */
116 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
117 10 /* 10% pages for TCD_TYPE_IRQ */
121 spl_debug_subsys2str(int subsys
)
188 spl_debug_dbg2str(int debug
)
215 spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
)
217 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
222 if (mask
== 0) { /* "0" */
226 } else { /* space-separated tokens */
227 for (i
= 0; i
< 32; i
++) {
230 if ((mask
& bit
) == 0)
234 if (token
== NULL
) /* unused bit */
237 if (len
> 0) { /* separator? */
243 while (*token
!= 0) {
252 /* terminate 'str' */
262 spl_debug_token2mask(int *mask
, const char *str
, int len
, int is_subsys
)
264 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
269 /* match against known tokens */
270 for (i
= 0; i
< 32; i
++) {
274 if (token
== NULL
) /* unused? */
279 if (j
== len
) { /* end of token */
290 if (str
[j
] == token
[j
])
293 if (str
[j
] < 'A' || 'Z' < str
[j
])
296 if (str
[j
] - 'A' + 'a' != token
[j
])
301 return -EINVAL
; /* no match */
305 spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
)
308 int m
= 0, matched
, n
, t
;
310 /* Allow a number for backwards compatibility */
311 for (n
= strlen(str
); n
> 0; n
--)
312 if (!isspace(str
[n
-1]))
316 if ((t
= sscanf(str
, "%i%n", &m
, &matched
)) >= 1 && matched
== n
) {
321 /* <str> must be a list of debug tokens or numbers separated by
322 * whitespace and optionally an operator ('+' or '-'). If an operator
323 * appears first in <str>, '*mask' is used as the starting point
324 * (relative), otherwise 0 is used (absolute). An operator applies to
325 * all following tokens up to the next operator. */
328 while (isspace(*str
)) /* skip whitespace */
334 if (*str
== '+' || *str
== '-') {
337 /* op on first token == relative */
341 while (isspace(*str
)) /* skip whitespace */
344 if (*str
== 0) /* trailing op */
348 /* find token length */
349 for (n
= 0; str
[n
] != 0 && !isspace(str
[n
]); n
++);
352 if (spl_debug_token2mask(&t
, str
, n
, is_subsys
) != 0)
372 spl_debug_dumplog_internal(dumplog_priv_t
*dp
)
376 journal_info
= current
->journal_info
;
377 current
->journal_info
= NULL
;
379 snprintf(spl_debug_file_name
, sizeof(spl_debug_file_path
) - 1,
380 "%s.%ld.%ld", spl_debug_file_path
,
381 get_seconds(), (long)dp
->dp_pid
);
382 printk("SPL: Dumping log to %s\n", spl_debug_file_name
);
383 spl_debug_dump_all_pages(dp
, spl_debug_file_name
);
385 current
->journal_info
= journal_info
;
389 spl_debug_dumplog_thread(void *arg
)
391 dumplog_priv_t
*dp
= (dumplog_priv_t
*)arg
;
393 spl_debug_dumplog_internal(dp
);
394 atomic_set(&dp
->dp_done
, 1);
395 wake_up(&dp
->dp_waitq
);
396 complete_and_exit(NULL
, 0);
398 return 0; /* Unreachable */
401 /* When flag is set do not use a new thread for the debug dump */
403 spl_debug_dumplog(int flags
)
405 struct task_struct
*tsk
;
408 init_waitqueue_head(&dp
.dp_waitq
);
409 dp
.dp_pid
= current
->pid
;
411 atomic_set(&dp
.dp_done
, 0);
413 if (dp
.dp_flags
& DL_NOTHREAD
) {
414 spl_debug_dumplog_internal(&dp
);
417 tsk
= kthread_create(spl_debug_dumplog_thread
,(void *)&dp
,"spl_debug");
421 wake_up_process(tsk
);
422 wait_event(dp
.dp_waitq
, atomic_read(&dp
.dp_done
));
427 EXPORT_SYMBOL(spl_debug_dumplog
);
430 trace_get_console_buffer(void)
437 } else if (in_softirq()) {
443 return trace_console_buffers
[cpu
][idx
];
447 trace_put_console_buffer(char *buffer
)
453 trace_lock_tcd(struct trace_cpu_data
*tcd
)
455 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
457 spin_lock_irqsave(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
463 trace_unlock_tcd(struct trace_cpu_data
*tcd
)
465 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
467 spin_unlock_irqrestore(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
470 static struct trace_cpu_data
*
474 struct trace_cpu_data
*tcd
;
478 tcd
= &(*trace_data
[TCD_TYPE_IRQ
])[cpu
].tcd
;
479 else if (in_softirq())
480 tcd
= &(*trace_data
[TCD_TYPE_SOFTIRQ
])[cpu
].tcd
;
482 tcd
= &(*trace_data
[TCD_TYPE_PROC
])[cpu
].tcd
;
490 trace_put_tcd (struct trace_cpu_data
*tcd
)
492 trace_unlock_tcd(tcd
);
498 trace_set_debug_header(struct spl_debug_header
*header
, int subsys
,
499 int mask
, const int line
, unsigned long stack
)
503 do_gettimeofday(&tv
);
505 header
->ph_subsys
= subsys
;
506 header
->ph_mask
= mask
;
507 header
->ph_cpu_id
= smp_processor_id();
508 header
->ph_sec
= (__u32
)tv
.tv_sec
;
509 header
->ph_usec
= tv
.tv_usec
;
510 header
->ph_stack
= stack
;
511 header
->ph_pid
= current
->pid
;
512 header
->ph_line_num
= line
;
518 trace_print_to_console(struct spl_debug_header
*hdr
, int mask
, const char *buf
,
519 int len
, const char *file
, const char *fn
)
521 char *prefix
= "SPL", *ptype
= NULL
;
523 if ((mask
& SD_EMERG
) != 0) {
526 } else if ((mask
& SD_ERROR
) != 0) {
529 } else if ((mask
& SD_WARNING
) != 0) {
531 ptype
= KERN_WARNING
;
532 } else if ((mask
& (SD_CONSOLE
| spl_debug_printk
)) != 0) {
537 if ((mask
& SD_CONSOLE
) != 0) {
538 printk("%s%s: %.*s", ptype
, prefix
, len
, buf
);
540 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype
, prefix
,
541 hdr
->ph_pid
, hdr
->ph_stack
, file
,
542 hdr
->ph_line_num
, fn
, len
, buf
);
549 trace_max_debug_mb(void)
551 return MAX(512, ((num_physpages
>> (20 - PAGE_SHIFT
)) * 80) / 100);
554 static struct trace_page
*
558 struct trace_page
*tage
;
560 page
= alloc_pages(gfp
| __GFP_NOWARN
, 0);
564 tage
= kmalloc(sizeof(*tage
), gfp
);
566 __free_pages(page
, 0);
571 atomic_inc(&trace_tage_allocated
);
577 tage_free(struct trace_page
*tage
)
579 __ASSERT(tage
!= NULL
);
580 __ASSERT(tage
->page
!= NULL
);
582 __free_pages(tage
->page
, 0);
584 atomic_dec(&trace_tage_allocated
);
587 static struct trace_page
*
588 tage_from_list(struct list_head
*list
)
590 return list_entry(list
, struct trace_page
, linkage
);
594 tage_to_tail(struct trace_page
*tage
, struct list_head
*queue
)
596 __ASSERT(tage
!= NULL
);
597 __ASSERT(queue
!= NULL
);
599 list_move_tail(&tage
->linkage
, queue
);
602 /* try to return a page that has 'len' bytes left at the end */
603 static struct trace_page
*
604 trace_get_tage_try(struct trace_cpu_data
*tcd
, unsigned long len
)
606 struct trace_page
*tage
;
608 if (tcd
->tcd_cur_pages
> 0) {
609 __ASSERT(!list_empty(&tcd
->tcd_pages
));
610 tage
= tage_from_list(tcd
->tcd_pages
.prev
);
611 if (tage
->used
+ len
<= PAGE_SIZE
)
615 if (tcd
->tcd_cur_pages
< tcd
->tcd_max_pages
) {
616 if (tcd
->tcd_cur_stock_pages
> 0) {
617 tage
= tage_from_list(tcd
->tcd_stock_pages
.prev
);
618 tcd
->tcd_cur_stock_pages
--;
619 list_del_init(&tage
->linkage
);
621 tage
= tage_alloc(GFP_ATOMIC
);
624 "failure to allocate a tage (%ld)\n",
631 tage
->cpu
= smp_processor_id();
632 tage
->type
= tcd
->tcd_type
;
633 list_add_tail(&tage
->linkage
, &tcd
->tcd_pages
);
634 tcd
->tcd_cur_pages
++;
642 /* return a page that has 'len' bytes left at the end */
643 static struct trace_page
*
644 trace_get_tage(struct trace_cpu_data
*tcd
, unsigned long len
)
646 struct trace_page
*tage
;
648 __ASSERT(len
<= PAGE_SIZE
);
650 tage
= trace_get_tage_try(tcd
, len
);
654 if (tcd
->tcd_cur_pages
> 0) {
655 tage
= tage_from_list(tcd
->tcd_pages
.next
);
657 tage_to_tail(tage
, &tcd
->tcd_pages
);
664 spl_debug_msg(void *arg
, int subsys
, int mask
, const char *file
,
665 const char *fn
, const int line
, const char *format
, ...)
667 spl_debug_limit_state_t
*cdls
= arg
;
668 struct trace_cpu_data
*tcd
= NULL
;
669 struct spl_debug_header header
= { 0, };
670 struct trace_page
*tage
;
671 /* string_buf is used only if tcd != NULL, and is always set then */
672 char *string_buf
= NULL
;
675 int needed
= 85; /* average message length */
681 subsys
= SS_DEBUG_SUBSYS
;
686 if (strchr(file
, '/'))
687 file
= strrchr(file
, '/') + 1;
689 trace_set_debug_header(&header
, subsys
, mask
, line
, 0);
691 tcd
= trace_get_tcd();
695 if (tcd
->tcd_shutting_down
) {
701 known_size
= strlen(file
) + 1;
703 known_size
+= strlen(fn
) + 1;
705 if (spl_debug_binary
)
706 known_size
+= sizeof(header
);
708 /* '2' used because vsnprintf returns real size required for output
709 * _without_ terminating NULL. */
710 for (i
= 0; i
< 2; i
++) {
711 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
713 if (needed
+ known_size
> PAGE_SIZE
)
721 string_buf
= (char *)page_address(tage
->page
) +
722 tage
->used
+ known_size
;
724 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
726 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
735 va_start(ap
, format
);
736 needed
+= vsnprintf(string_buf
, max_nob
, format
, ap
);
740 if (needed
< max_nob
)
744 header
.ph_len
= known_size
+ needed
;
745 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
747 if (spl_debug_binary
) {
748 memcpy(debug_buf
, &header
, sizeof(header
));
749 tage
->used
+= sizeof(header
);
750 debug_buf
+= sizeof(header
);
753 strcpy(debug_buf
, file
);
754 tage
->used
+= strlen(file
) + 1;
755 debug_buf
+= strlen(file
) + 1;
758 strcpy(debug_buf
, fn
);
759 tage
->used
+= strlen(fn
) + 1;
760 debug_buf
+= strlen(fn
) + 1;
763 __ASSERT(debug_buf
== string_buf
);
765 tage
->used
+= needed
;
766 __ASSERT (tage
->used
<= PAGE_SIZE
);
769 if ((mask
& spl_debug_printk
) == 0) {
770 /* no console output requested */
777 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
778 !time_before(cdls
->cdls_next
, jiffies
)) {
779 /* skipping a console message */
786 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
787 (10 * HZ
), jiffies
)) {
788 /* last timeout was a long time ago */
789 cdls
->cdls_delay
/= spl_console_backoff
* 4;
791 cdls
->cdls_delay
*= spl_console_backoff
;
793 if (cdls
->cdls_delay
< spl_console_min_delay
)
794 cdls
->cdls_delay
= spl_console_min_delay
;
795 else if (cdls
->cdls_delay
> spl_console_max_delay
)
796 cdls
->cdls_delay
= spl_console_max_delay
;
799 /* ensure cdls_next is never zero after it's been seen */
800 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
804 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
807 string_buf
= trace_get_console_buffer();
810 if (format
!= NULL
) {
811 va_start(ap
, format
);
812 needed
+= vsnprintf(string_buf
,
813 TRACE_CONSOLE_BUFFER_SIZE
, format
, ap
);
816 trace_print_to_console(&header
, mask
,
817 string_buf
, needed
, file
, fn
);
819 trace_put_console_buffer(string_buf
);
822 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
823 string_buf
= trace_get_console_buffer();
825 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
826 "Skipped %d previous similar message%s\n",
827 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
829 trace_print_to_console(&header
, mask
,
830 string_buf
, needed
, file
, fn
);
832 trace_put_console_buffer(string_buf
);
833 cdls
->cdls_count
= 0;
838 EXPORT_SYMBOL(spl_debug_msg
);
840 /* Do the collect_pages job on a single CPU: assumes that all other
841 * CPUs have been stopped during a panic. If this isn't true for
842 * some arch, this will have to be implemented separately in each arch.
845 collect_pages_from_single_cpu(struct page_collection
*pc
)
847 struct trace_cpu_data
*tcd
;
850 tcd_for_each(tcd
, i
, j
) {
851 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
852 tcd
->tcd_cur_pages
= 0;
857 collect_pages_on_all_cpus(struct page_collection
*pc
)
859 struct trace_cpu_data
*tcd
;
862 spin_lock(&pc
->pc_lock
);
863 for_each_possible_cpu(cpu
) {
864 tcd_for_each_type_lock(tcd
, i
, cpu
) {
865 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
866 tcd
->tcd_cur_pages
= 0;
869 spin_unlock(&pc
->pc_lock
);
873 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
875 INIT_LIST_HEAD(&pc
->pc_pages
);
877 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
878 collect_pages_from_single_cpu(pc
);
880 collect_pages_on_all_cpus(pc
);
884 put_pages_back_on_all_cpus(struct page_collection
*pc
)
886 struct trace_cpu_data
*tcd
;
887 struct list_head
*cur_head
;
888 struct trace_page
*tage
;
889 struct trace_page
*tmp
;
892 spin_lock(&pc
->pc_lock
);
894 for_each_possible_cpu(cpu
) {
895 tcd_for_each_type_lock(tcd
, i
, cpu
) {
896 cur_head
= tcd
->tcd_pages
.next
;
898 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
900 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
903 tage_to_tail(tage
, cur_head
);
904 tcd
->tcd_cur_pages
++;
909 spin_unlock(&pc
->pc_lock
);
913 put_pages_back(struct page_collection
*pc
)
915 if (!spl_panic_in_progress
)
916 put_pages_back_on_all_cpus(pc
);
920 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
922 struct page_collection pc
;
924 struct trace_page
*tage
;
925 struct trace_page
*tmp
;
929 down_write(&trace_sem
);
931 filp
= spl_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
935 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
940 spin_lock_init(&pc
.pc_lock
);
941 collect_pages(dp
, &pc
);
942 if (list_empty(&pc
.pc_pages
)) {
950 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
951 rc
= spl_filp_write(filp
, page_address(tage
->page
),
952 tage
->used
, spl_filp_poff(filp
));
953 if (rc
!= (int)tage
->used
) {
954 printk(KERN_WARNING
"SPL: Wanted to write %u "
955 "but wrote %d\n", tage
->used
, rc
);
957 __ASSERT(list_empty(&pc
.pc_pages
));
960 list_del(&tage
->linkage
);
966 rc
= spl_filp_fsync(filp
, 1);
968 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
970 spl_filp_close(filp
);
972 up_write(&trace_sem
);
978 spl_debug_flush_pages(void)
981 struct page_collection pc
;
982 struct trace_page
*tage
;
983 struct trace_page
*tmp
;
985 spin_lock_init(&pc
.pc_lock
);
986 init_waitqueue_head(&dp
.dp_waitq
);
987 dp
.dp_pid
= current
->pid
;
989 atomic_set(&dp
.dp_done
, 0);
991 collect_pages(&dp
, &pc
);
992 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
993 list_del(&tage
->linkage
);
999 spl_debug_set_mask(unsigned long mask
) {
1000 spl_debug_mask
= mask
;
1003 EXPORT_SYMBOL(spl_debug_set_mask
);
1006 spl_debug_get_mask(void) {
1007 return spl_debug_mask
;
1009 EXPORT_SYMBOL(spl_debug_get_mask
);
1012 spl_debug_set_subsys(unsigned long subsys
) {
1013 spl_debug_subsys
= subsys
;
1016 EXPORT_SYMBOL(spl_debug_set_subsys
);
1019 spl_debug_get_subsys(void) {
1020 return spl_debug_subsys
;
1022 EXPORT_SYMBOL(spl_debug_get_subsys
);
1025 spl_debug_set_mb(int mb
)
1028 int limit
= trace_max_debug_mb();
1029 struct trace_cpu_data
*tcd
;
1031 if (mb
< num_possible_cpus()) {
1032 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1033 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1038 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1039 "%dMB - upper limit is %d\n", mb
, limit
);
1043 mb
/= num_possible_cpus();
1044 pages
= mb
<< (20 - PAGE_SHIFT
);
1046 down_write(&trace_sem
);
1048 tcd_for_each(tcd
, i
, j
)
1049 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1051 up_write(&trace_sem
);
1055 EXPORT_SYMBOL(spl_debug_set_mb
);
1058 spl_debug_get_mb(void)
1061 struct trace_cpu_data
*tcd
;
1062 int total_pages
= 0;
1064 down_read(&trace_sem
);
1066 tcd_for_each(tcd
, i
, j
)
1067 total_pages
+= tcd
->tcd_max_pages
;
1069 up_read(&trace_sem
);
1071 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1073 EXPORT_SYMBOL(spl_debug_get_mb
);
1075 void spl_debug_dumpstack(struct task_struct
*tsk
)
1077 extern void show_task(struct task_struct
*);
1082 printk("SPL: Showing stack for process %d\n", tsk
->pid
);
1085 EXPORT_SYMBOL(spl_debug_dumpstack
);
1087 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1089 spl_debug_catastrophe
= 1;
1090 spl_debug_msg(NULL
, 0, SD_EMERG
, file
, func
, line
, "SPL PANIC\n");
1093 panic("SPL PANIC in interrupt.\n");
1095 if (in_atomic() || irqs_disabled())
1096 flags
|= DL_NOTHREAD
;
1098 /* Ensure all debug pages and dumped by current cpu */
1099 if (spl_debug_panic_on_bug
)
1100 spl_panic_in_progress
= 1;
1102 spl_debug_dumpstack(NULL
);
1103 spl_debug_dumplog(flags
);
1105 if (spl_debug_panic_on_bug
)
1108 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1112 EXPORT_SYMBOL(spl_debug_bug
);
1115 spl_debug_clear_buffer(void)
1117 spl_debug_flush_pages();
1120 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1123 spl_debug_mark_buffer(char *text
)
1125 SDEBUG(SD_WARNING
, "*************************************\n");
1126 SDEBUG(SD_WARNING
, "DEBUG MARKER: %s\n", text
);
1127 SDEBUG(SD_WARNING
, "*************************************\n");
1131 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1134 trace_init(int max_pages
)
1136 struct trace_cpu_data
*tcd
;
1139 init_rwsem(&trace_sem
);
1141 /* initialize trace_data */
1142 memset(trace_data
, 0, sizeof(trace_data
));
1143 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1144 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1145 NR_CPUS
, GFP_KERNEL
);
1146 if (trace_data
[i
] == NULL
)
1150 tcd_for_each(tcd
, i
, j
) {
1151 spin_lock_init(&tcd
->tcd_lock
);
1152 tcd
->tcd_pages_factor
= pages_factor
[i
];
1155 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1156 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1157 tcd
->tcd_cur_pages
= 0;
1158 tcd
->tcd_cur_stock_pages
= 0;
1159 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1160 tcd
->tcd_shutting_down
= 0;
1163 for (i
= 0; i
< num_possible_cpus(); i
++) {
1164 for (j
= 0; j
< 3; j
++) {
1165 trace_console_buffers
[i
][j
] =
1166 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1169 if (trace_console_buffers
[i
][j
] == NULL
)
1177 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1182 spl_debug_init(void)
1184 int rc
, max
= spl_debug_mb
;
1186 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1187 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1189 /* If spl_debug_mb is set to an invalid value or uninitialized
1190 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1191 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1192 max
>= 512 || max
< 0) {
1193 max
= TCD_MAX_PAGES
;
1195 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1198 rc
= trace_init(max
);
1206 trace_cleanup_on_all_cpus(void)
1208 struct trace_cpu_data
*tcd
;
1209 struct trace_page
*tage
;
1210 struct trace_page
*tmp
;
1213 for_each_possible_cpu(cpu
) {
1214 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1215 tcd
->tcd_shutting_down
= 1;
1217 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1219 list_del(&tage
->linkage
);
1222 tcd
->tcd_cur_pages
= 0;
1232 trace_cleanup_on_all_cpus();
1234 for (i
= 0; i
< num_possible_cpus(); i
++) {
1235 for (j
= 0; j
< 3; j
++) {
1236 if (trace_console_buffers
[i
][j
] != NULL
) {
1237 kfree(trace_console_buffers
[i
][j
]);
1238 trace_console_buffers
[i
][j
] = NULL
;
1243 for (i
= 0; i
< TCD_TYPE_MAX
&& trace_data
[i
] != NULL
; i
++) {
1244 kfree(trace_data
[i
]);
1245 trace_data
[i
] = NULL
;
1250 spl_debug_fini(void)
1255 #endif /* DEBUG_LOG */