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 tcd
= trace_get_tcd();
690 trace_set_debug_header(&header
, subsys
, mask
, line
, 0);
694 if (tcd
->tcd_shutting_down
) {
700 known_size
= strlen(file
) + 1;
702 known_size
+= strlen(fn
) + 1;
704 if (spl_debug_binary
)
705 known_size
+= sizeof(header
);
707 /* '2' used because vsnprintf returns real size required for output
708 * _without_ terminating NULL. */
709 for (i
= 0; i
< 2; i
++) {
710 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
712 if (needed
+ known_size
> PAGE_SIZE
)
720 string_buf
= (char *)page_address(tage
->page
) +
721 tage
->used
+ known_size
;
723 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
725 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
734 va_start(ap
, format
);
735 needed
+= vsnprintf(string_buf
, max_nob
, format
, ap
);
739 if (needed
< max_nob
)
743 header
.ph_len
= known_size
+ needed
;
744 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
746 if (spl_debug_binary
) {
747 memcpy(debug_buf
, &header
, sizeof(header
));
748 tage
->used
+= sizeof(header
);
749 debug_buf
+= sizeof(header
);
752 strcpy(debug_buf
, file
);
753 tage
->used
+= strlen(file
) + 1;
754 debug_buf
+= strlen(file
) + 1;
757 strcpy(debug_buf
, fn
);
758 tage
->used
+= strlen(fn
) + 1;
759 debug_buf
+= strlen(fn
) + 1;
762 __ASSERT(debug_buf
== string_buf
);
764 tage
->used
+= needed
;
765 __ASSERT (tage
->used
<= PAGE_SIZE
);
768 if ((mask
& spl_debug_printk
) == 0) {
769 /* no console output requested */
776 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
777 !time_before(cdls
->cdls_next
, jiffies
)) {
778 /* skipping a console message */
785 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
786 (10 * HZ
), jiffies
)) {
787 /* last timeout was a long time ago */
788 cdls
->cdls_delay
/= spl_console_backoff
* 4;
790 cdls
->cdls_delay
*= spl_console_backoff
;
792 if (cdls
->cdls_delay
< spl_console_min_delay
)
793 cdls
->cdls_delay
= spl_console_min_delay
;
794 else if (cdls
->cdls_delay
> spl_console_max_delay
)
795 cdls
->cdls_delay
= spl_console_max_delay
;
798 /* ensure cdls_next is never zero after it's been seen */
799 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
803 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
806 string_buf
= trace_get_console_buffer();
809 if (format
!= NULL
) {
810 va_start(ap
, format
);
811 needed
+= vsnprintf(string_buf
,
812 TRACE_CONSOLE_BUFFER_SIZE
, format
, ap
);
815 trace_print_to_console(&header
, mask
,
816 string_buf
, needed
, file
, fn
);
818 trace_put_console_buffer(string_buf
);
821 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
822 string_buf
= trace_get_console_buffer();
824 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
825 "Skipped %d previous similar message%s\n",
826 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
828 trace_print_to_console(&header
, mask
,
829 string_buf
, needed
, file
, fn
);
831 trace_put_console_buffer(string_buf
);
832 cdls
->cdls_count
= 0;
837 EXPORT_SYMBOL(spl_debug_msg
);
839 /* Do the collect_pages job on a single CPU: assumes that all other
840 * CPUs have been stopped during a panic. If this isn't true for
841 * some arch, this will have to be implemented separately in each arch.
844 collect_pages_from_single_cpu(struct page_collection
*pc
)
846 struct trace_cpu_data
*tcd
;
849 tcd_for_each(tcd
, i
, j
) {
850 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
851 tcd
->tcd_cur_pages
= 0;
856 collect_pages_on_all_cpus(struct page_collection
*pc
)
858 struct trace_cpu_data
*tcd
;
861 spin_lock(&pc
->pc_lock
);
862 for_each_possible_cpu(cpu
) {
863 tcd_for_each_type_lock(tcd
, i
, cpu
) {
864 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
865 tcd
->tcd_cur_pages
= 0;
868 spin_unlock(&pc
->pc_lock
);
872 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
874 INIT_LIST_HEAD(&pc
->pc_pages
);
876 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
877 collect_pages_from_single_cpu(pc
);
879 collect_pages_on_all_cpus(pc
);
883 put_pages_back_on_all_cpus(struct page_collection
*pc
)
885 struct trace_cpu_data
*tcd
;
886 struct list_head
*cur_head
;
887 struct trace_page
*tage
;
888 struct trace_page
*tmp
;
891 spin_lock(&pc
->pc_lock
);
893 for_each_possible_cpu(cpu
) {
894 tcd_for_each_type_lock(tcd
, i
, cpu
) {
895 cur_head
= tcd
->tcd_pages
.next
;
897 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
899 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
902 tage_to_tail(tage
, cur_head
);
903 tcd
->tcd_cur_pages
++;
908 spin_unlock(&pc
->pc_lock
);
912 put_pages_back(struct page_collection
*pc
)
914 if (!spl_panic_in_progress
)
915 put_pages_back_on_all_cpus(pc
);
919 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
921 struct page_collection pc
;
923 struct trace_page
*tage
;
924 struct trace_page
*tmp
;
928 down_write(&trace_sem
);
930 filp
= spl_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
934 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
939 spin_lock_init(&pc
.pc_lock
);
940 collect_pages(dp
, &pc
);
941 if (list_empty(&pc
.pc_pages
)) {
949 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
950 rc
= spl_filp_write(filp
, page_address(tage
->page
),
951 tage
->used
, spl_filp_poff(filp
));
952 if (rc
!= (int)tage
->used
) {
953 printk(KERN_WARNING
"SPL: Wanted to write %u "
954 "but wrote %d\n", tage
->used
, rc
);
956 __ASSERT(list_empty(&pc
.pc_pages
));
959 list_del(&tage
->linkage
);
965 rc
= spl_filp_fsync(filp
, 1);
967 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
969 spl_filp_close(filp
);
971 up_write(&trace_sem
);
977 spl_debug_flush_pages(void)
980 struct page_collection pc
;
981 struct trace_page
*tage
;
982 struct trace_page
*tmp
;
984 spin_lock_init(&pc
.pc_lock
);
985 init_waitqueue_head(&dp
.dp_waitq
);
986 dp
.dp_pid
= current
->pid
;
988 atomic_set(&dp
.dp_done
, 0);
990 collect_pages(&dp
, &pc
);
991 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
992 list_del(&tage
->linkage
);
998 spl_debug_set_mask(unsigned long mask
) {
999 spl_debug_mask
= mask
;
1002 EXPORT_SYMBOL(spl_debug_set_mask
);
1005 spl_debug_get_mask(void) {
1006 return spl_debug_mask
;
1008 EXPORT_SYMBOL(spl_debug_get_mask
);
1011 spl_debug_set_subsys(unsigned long subsys
) {
1012 spl_debug_subsys
= subsys
;
1015 EXPORT_SYMBOL(spl_debug_set_subsys
);
1018 spl_debug_get_subsys(void) {
1019 return spl_debug_subsys
;
1021 EXPORT_SYMBOL(spl_debug_get_subsys
);
1024 spl_debug_set_mb(int mb
)
1027 int limit
= trace_max_debug_mb();
1028 struct trace_cpu_data
*tcd
;
1030 if (mb
< num_possible_cpus()) {
1031 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1032 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1037 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1038 "%dMB - upper limit is %d\n", mb
, limit
);
1042 mb
/= num_possible_cpus();
1043 pages
= mb
<< (20 - PAGE_SHIFT
);
1045 down_write(&trace_sem
);
1047 tcd_for_each(tcd
, i
, j
)
1048 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1050 up_write(&trace_sem
);
1054 EXPORT_SYMBOL(spl_debug_set_mb
);
1057 spl_debug_get_mb(void)
1060 struct trace_cpu_data
*tcd
;
1061 int total_pages
= 0;
1063 down_read(&trace_sem
);
1065 tcd_for_each(tcd
, i
, j
)
1066 total_pages
+= tcd
->tcd_max_pages
;
1068 up_read(&trace_sem
);
1070 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1072 EXPORT_SYMBOL(spl_debug_get_mb
);
1074 void spl_debug_dumpstack(struct task_struct
*tsk
)
1076 extern void show_task(struct task_struct
*);
1081 printk("SPL: Showing stack for process %d\n", tsk
->pid
);
1084 EXPORT_SYMBOL(spl_debug_dumpstack
);
1086 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1088 spl_debug_catastrophe
= 1;
1089 spl_debug_msg(NULL
, 0, SD_EMERG
, file
, func
, line
, "SPL PANIC\n");
1092 panic("SPL PANIC in interrupt.\n");
1094 if (in_atomic() || irqs_disabled())
1095 flags
|= DL_NOTHREAD
;
1097 /* Ensure all debug pages and dumped by current cpu */
1098 if (spl_debug_panic_on_bug
)
1099 spl_panic_in_progress
= 1;
1101 spl_debug_dumpstack(NULL
);
1102 spl_debug_dumplog(flags
);
1104 if (spl_debug_panic_on_bug
)
1107 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1111 EXPORT_SYMBOL(spl_debug_bug
);
1114 spl_debug_clear_buffer(void)
1116 spl_debug_flush_pages();
1119 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1122 spl_debug_mark_buffer(char *text
)
1124 SDEBUG(SD_WARNING
, "*************************************\n");
1125 SDEBUG(SD_WARNING
, "DEBUG MARKER: %s\n", text
);
1126 SDEBUG(SD_WARNING
, "*************************************\n");
1130 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1133 trace_init(int max_pages
)
1135 struct trace_cpu_data
*tcd
;
1138 init_rwsem(&trace_sem
);
1140 /* initialize trace_data */
1141 memset(trace_data
, 0, sizeof(trace_data
));
1142 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1143 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1144 NR_CPUS
, GFP_KERNEL
);
1145 if (trace_data
[i
] == NULL
)
1149 tcd_for_each(tcd
, i
, j
) {
1150 spin_lock_init(&tcd
->tcd_lock
);
1151 tcd
->tcd_pages_factor
= pages_factor
[i
];
1154 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1155 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1156 tcd
->tcd_cur_pages
= 0;
1157 tcd
->tcd_cur_stock_pages
= 0;
1158 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1159 tcd
->tcd_shutting_down
= 0;
1162 for (i
= 0; i
< num_possible_cpus(); i
++) {
1163 for (j
= 0; j
< 3; j
++) {
1164 trace_console_buffers
[i
][j
] =
1165 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1168 if (trace_console_buffers
[i
][j
] == NULL
)
1176 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1181 spl_debug_init(void)
1183 int rc
, max
= spl_debug_mb
;
1185 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1186 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1188 /* If spl_debug_mb is set to an invalid value or uninitialized
1189 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1190 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1191 max
>= 512 || max
< 0) {
1192 max
= TCD_MAX_PAGES
;
1194 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1197 rc
= trace_init(max
);
1205 trace_cleanup_on_all_cpus(void)
1207 struct trace_cpu_data
*tcd
;
1208 struct trace_page
*tage
;
1209 struct trace_page
*tmp
;
1212 for_each_possible_cpu(cpu
) {
1213 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1214 tcd
->tcd_shutting_down
= 1;
1216 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1218 list_del(&tage
->linkage
);
1221 tcd
->tcd_cur_pages
= 0;
1231 trace_cleanup_on_all_cpus();
1233 for (i
= 0; i
< num_possible_cpus(); i
++) {
1234 for (j
= 0; j
< 3; j
++) {
1235 if (trace_console_buffers
[i
][j
] != NULL
) {
1236 kfree(trace_console_buffers
[i
][j
]);
1237 trace_console_buffers
[i
][j
] = NULL
;
1242 for (i
= 0; i
< TCD_TYPE_MAX
&& trace_data
[i
] != NULL
; i
++) {
1243 kfree(trace_data
[i
]);
1244 trace_data
[i
] = NULL
;
1249 spl_debug_fini(void)
1254 #endif /* DEBUG_LOG */