2 * This file is part of the SPL: Solaris Porting Layer.
4 * This file was originally part of Lustre, http://www.lustre.org.
5 * but has subsequently been adapted for use in the SPL in
6 * accordance with the GPL.
8 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
10 * Produced at Lawrence Livermore National Laboratory
12 * Zach Brown <zab@clusterfs.com>
13 * Phil Schwan <phil@clusterfs.com>
14 * Brian Behlendorf <behlendorf1@llnl.gov>,
15 * Herb Wartens <wartens2@llnl.gov>,
16 * Jim Garlick <garlick@llnl.gov>
19 * This is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This is distributed in the hope that it will be useful, but WITHOUT
25 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include <linux/kmod.h>
36 #include <linux/vmalloc.h>
37 #include <linux/pagemap.h>
38 #include <linux/slab.h>
39 #include <linux/ctype.h>
40 #include <linux/kthread.h>
41 #include <linux/hardirq.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <sys/sysmacros.h>
46 #include <sys/debug.h>
49 #ifdef DEBUG_SUBSYSTEM
50 #undef DEBUG_SUBSYSTEM
53 #define DEBUG_SUBSYSTEM S_DEBUG
55 unsigned long spl_debug_subsys
= ~0;
56 EXPORT_SYMBOL(spl_debug_subsys
);
57 module_param(spl_debug_subsys
, long, 0644);
58 MODULE_PARM_DESC(spl_debug_subsys
, "Subsystem debugging level mask.");
60 unsigned long spl_debug_mask
= (D_EMERG
| D_ERROR
| D_WARNING
| D_CONSOLE
);
61 EXPORT_SYMBOL(spl_debug_mask
);
62 module_param(spl_debug_mask
, long, 0644);
63 MODULE_PARM_DESC(spl_debug_mask
, "Debugging level mask.");
65 unsigned long spl_debug_printk
= D_CANTMASK
;
66 EXPORT_SYMBOL(spl_debug_printk
);
67 module_param(spl_debug_printk
, long, 0644);
68 MODULE_PARM_DESC(spl_debug_printk
, "Console printk level mask.");
70 int spl_debug_mb
= -1;
71 EXPORT_SYMBOL(spl_debug_mb
);
72 module_param(spl_debug_mb
, int, 0644);
73 MODULE_PARM_DESC(spl_debug_mb
, "Total debug buffer size.");
75 unsigned int spl_debug_binary
= 1;
76 EXPORT_SYMBOL(spl_debug_binary
);
78 unsigned int spl_debug_catastrophe
;
79 EXPORT_SYMBOL(spl_debug_catastrophe
);
81 unsigned int spl_debug_panic_on_bug
= 1;
82 EXPORT_SYMBOL(spl_debug_panic_on_bug
);
83 module_param(spl_debug_panic_on_bug
, int, 0644);
84 MODULE_PARM_DESC(spl_debug_panic_on_bug
, "Panic on BUG");
86 static char spl_debug_file_name
[PATH_MAX
];
87 char spl_debug_file_path
[PATH_MAX
] = "/var/dumps/spl-log";
89 unsigned int spl_console_ratelimit
= 1;
90 EXPORT_SYMBOL(spl_console_ratelimit
);
92 long spl_console_max_delay
;
93 EXPORT_SYMBOL(spl_console_max_delay
);
95 long spl_console_min_delay
;
96 EXPORT_SYMBOL(spl_console_min_delay
);
98 unsigned int spl_console_backoff
= SPL_DEFAULT_BACKOFF
;
99 EXPORT_SYMBOL(spl_console_backoff
);
101 unsigned int spl_debug_stack
;
102 EXPORT_SYMBOL(spl_debug_stack
);
104 static int spl_panic_in_progress
;
106 union trace_data_union (*trace_data
[TCD_TYPE_MAX
])[NR_CPUS
] __cacheline_aligned
;
107 char *trace_console_buffers
[NR_CPUS
][3];
108 struct rw_semaphore trace_sem
;
109 atomic_t trace_tage_allocated
= ATOMIC_INIT(0);
111 static int spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *);
112 static void trace_fini(void);
115 /* Memory percentage breakdown by type */
116 static unsigned int pages_factor
[TCD_TYPE_MAX
] = {
117 80, /* 80% pages for TCD_TYPE_PROC */
118 10, /* 10% pages for TCD_TYPE_SOFTIRQ */
119 10 /* 10% pages for TCD_TYPE_IRQ */
123 spl_debug_subsys2str(int subsys
)
164 spl_debug_dbg2str(int debug
)
191 spl_debug_mask2str(char *str
, int size
, unsigned long mask
, int is_subsys
)
193 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
198 if (mask
== 0) { /* "0" */
202 } else { /* space-separated tokens */
203 for (i
= 0; i
< 32; i
++) {
206 if ((mask
& bit
) == 0)
210 if (token
== NULL
) /* unused bit */
213 if (len
> 0) { /* separator? */
219 while (*token
!= 0) {
228 /* terminate 'str' */
238 spl_debug_token2mask(int *mask
, const char *str
, int len
, int is_subsys
)
240 const char *(*fn
)(int bit
) = is_subsys
? spl_debug_subsys2str
:
245 /* match against known tokens */
246 for (i
= 0; i
< 32; i
++) {
250 if (token
== NULL
) /* unused? */
255 if (j
== len
) { /* end of token */
266 if (str
[j
] == token
[j
])
269 if (str
[j
] < 'A' || 'Z' < str
[j
])
272 if (str
[j
] - 'A' + 'a' != token
[j
])
277 return -EINVAL
; /* no match */
281 spl_debug_str2mask(unsigned long *mask
, const char *str
, int is_subsys
)
284 int m
= 0, matched
, n
, t
;
286 /* Allow a number for backwards compatibility */
287 for (n
= strlen(str
); n
> 0; n
--)
288 if (!isspace(str
[n
-1]))
292 if ((t
= sscanf(str
, "%i%n", &m
, &matched
)) >= 1 && matched
== n
) {
297 /* <str> must be a list of debug tokens or numbers separated by
298 * whitespace and optionally an operator ('+' or '-'). If an operator
299 * appears first in <str>, '*mask' is used as the starting point
300 * (relative), otherwise 0 is used (absolute). An operator applies to
301 * all following tokens up to the next operator. */
304 while (isspace(*str
)) /* skip whitespace */
310 if (*str
== '+' || *str
== '-') {
313 /* op on first token == relative */
317 while (isspace(*str
)) /* skip whitespace */
320 if (*str
== 0) /* trailing op */
324 /* find token length */
325 for (n
= 0; str
[n
] != 0 && !isspace(str
[n
]); n
++);
328 if (spl_debug_token2mask(&t
, str
, n
, is_subsys
) != 0)
348 spl_debug_dumplog_internal(dumplog_priv_t
*dp
)
352 journal_info
= current
->journal_info
;
353 current
->journal_info
= NULL
;
355 snprintf(spl_debug_file_name
, sizeof(spl_debug_file_path
) - 1,
356 "%s.%ld.%ld", spl_debug_file_path
,
357 get_seconds(), (long)dp
->dp_pid
);
358 printk(KERN_ALERT
"SPL: dumping log to %s\n", spl_debug_file_name
);
359 spl_debug_dump_all_pages(dp
, spl_debug_file_name
);
361 current
->journal_info
= journal_info
;
365 spl_debug_dumplog_thread(void *arg
)
367 dumplog_priv_t
*dp
= (dumplog_priv_t
*)arg
;
369 spl_debug_dumplog_internal(dp
);
370 atomic_set(&dp
->dp_done
, 1);
371 wake_up(&dp
->dp_waitq
);
372 complete_and_exit(NULL
, 0);
374 return 0; /* Unreachable */
377 /* When flag is set do not use a new thread for the debug dump */
379 spl_debug_dumplog(int flags
)
381 struct task_struct
*tsk
;
384 init_waitqueue_head(&dp
.dp_waitq
);
385 dp
.dp_pid
= current
->pid
;
387 atomic_set(&dp
.dp_done
, 0);
389 if (dp
.dp_flags
& DL_NOTHREAD
) {
390 spl_debug_dumplog_internal(&dp
);
393 tsk
= kthread_create(spl_debug_dumplog_thread
,(void *)&dp
,"spl_debug");
397 wake_up_process(tsk
);
398 wait_event(dp
.dp_waitq
, atomic_read(&dp
.dp_done
));
403 EXPORT_SYMBOL(spl_debug_dumplog
);
406 trace_get_console_buffer(void)
413 } else if (in_softirq()) {
419 return trace_console_buffers
[cpu
][idx
];
423 trace_put_console_buffer(char *buffer
)
429 trace_lock_tcd(struct trace_cpu_data
*tcd
)
431 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
433 spin_lock_irqsave(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
439 trace_unlock_tcd(struct trace_cpu_data
*tcd
)
441 __ASSERT(tcd
->tcd_type
< TCD_TYPE_MAX
);
443 spin_unlock_irqrestore(&tcd
->tcd_lock
, tcd
->tcd_lock_flags
);
446 static struct trace_cpu_data
*
450 struct trace_cpu_data
*tcd
;
454 tcd
= &(*trace_data
[TCD_TYPE_IRQ
])[cpu
].tcd
;
455 else if (in_softirq())
456 tcd
= &(*trace_data
[TCD_TYPE_SOFTIRQ
])[cpu
].tcd
;
458 tcd
= &(*trace_data
[TCD_TYPE_PROC
])[cpu
].tcd
;
466 trace_put_tcd (struct trace_cpu_data
*tcd
)
468 trace_unlock_tcd(tcd
);
474 trace_set_debug_header(struct spl_debug_header
*header
, int subsys
,
475 int mask
, const int line
, unsigned long stack
)
479 do_gettimeofday(&tv
);
481 header
->ph_subsys
= subsys
;
482 header
->ph_mask
= mask
;
483 header
->ph_cpu_id
= smp_processor_id();
484 header
->ph_sec
= (__u32
)tv
.tv_sec
;
485 header
->ph_usec
= tv
.tv_usec
;
486 header
->ph_stack
= stack
;
487 header
->ph_pid
= current
->pid
;
488 header
->ph_line_num
= line
;
494 trace_print_to_console(struct spl_debug_header
*hdr
, int mask
, const char *buf
,
495 int len
, const char *file
, const char *fn
)
497 char *prefix
= "SPL", *ptype
= NULL
;
499 if ((mask
& D_EMERG
) != 0) {
502 } else if ((mask
& D_ERROR
) != 0) {
505 } else if ((mask
& D_WARNING
) != 0) {
507 ptype
= KERN_WARNING
;
508 } else if ((mask
& (D_CONSOLE
| spl_debug_printk
)) != 0) {
513 if ((mask
& D_CONSOLE
) != 0) {
514 printk("%s%s: %.*s", ptype
, prefix
, len
, buf
);
516 printk("%s%s: %d:%d:(%s:%d:%s()) %.*s", ptype
, prefix
,
517 hdr
->ph_pid
, hdr
->ph_stack
, file
,
518 hdr
->ph_line_num
, fn
, len
, buf
);
525 trace_max_debug_mb(void)
527 return MAX(512, ((num_physpages
>> (20 - PAGE_SHIFT
)) * 80) / 100);
530 static struct trace_page
*
534 struct trace_page
*tage
;
536 page
= alloc_pages(gfp
| __GFP_NOWARN
, 0);
540 tage
= kmalloc(sizeof(*tage
), gfp
);
542 __free_pages(page
, 0);
547 atomic_inc(&trace_tage_allocated
);
553 tage_free(struct trace_page
*tage
)
555 __ASSERT(tage
!= NULL
);
556 __ASSERT(tage
->page
!= NULL
);
558 __free_pages(tage
->page
, 0);
560 atomic_dec(&trace_tage_allocated
);
563 static struct trace_page
*
564 tage_from_list(struct list_head
*list
)
566 return list_entry(list
, struct trace_page
, linkage
);
570 tage_to_tail(struct trace_page
*tage
, struct list_head
*queue
)
572 __ASSERT(tage
!= NULL
);
573 __ASSERT(queue
!= NULL
);
575 list_move_tail(&tage
->linkage
, queue
);
578 /* try to return a page that has 'len' bytes left at the end */
579 static struct trace_page
*
580 trace_get_tage_try(struct trace_cpu_data
*tcd
, unsigned long len
)
582 struct trace_page
*tage
;
584 if (tcd
->tcd_cur_pages
> 0) {
585 __ASSERT(!list_empty(&tcd
->tcd_pages
));
586 tage
= tage_from_list(tcd
->tcd_pages
.prev
);
587 if (tage
->used
+ len
<= PAGE_SIZE
)
591 if (tcd
->tcd_cur_pages
< tcd
->tcd_max_pages
) {
592 if (tcd
->tcd_cur_stock_pages
> 0) {
593 tage
= tage_from_list(tcd
->tcd_stock_pages
.prev
);
594 tcd
->tcd_cur_stock_pages
--;
595 list_del_init(&tage
->linkage
);
597 tage
= tage_alloc(GFP_ATOMIC
);
600 "failure to allocate a tage (%ld)\n",
607 tage
->cpu
= smp_processor_id();
608 tage
->type
= tcd
->tcd_type
;
609 list_add_tail(&tage
->linkage
, &tcd
->tcd_pages
);
610 tcd
->tcd_cur_pages
++;
618 /* return a page that has 'len' bytes left at the end */
619 static struct trace_page
*
620 trace_get_tage(struct trace_cpu_data
*tcd
, unsigned long len
)
622 struct trace_page
*tage
;
624 __ASSERT(len
<= PAGE_SIZE
);
626 tage
= trace_get_tage_try(tcd
, len
);
630 if (tcd
->tcd_cur_pages
> 0) {
631 tage
= tage_from_list(tcd
->tcd_pages
.next
);
633 tage_to_tail(tage
, &tcd
->tcd_pages
);
640 spl_debug_vmsg(spl_debug_limit_state_t
*cdls
, int subsys
, int mask
,
641 const char *file
, const char *fn
, const int line
,
642 const char *format1
, va_list args
, const char *format2
, ...)
644 struct trace_cpu_data
*tcd
= NULL
;
645 struct spl_debug_header header
= { 0, };
646 struct trace_page
*tage
;
647 /* string_buf is used only if tcd != NULL, and is always set then */
648 char *string_buf
= NULL
;
651 int needed
= 85; /* average message length */
657 if (strchr(file
, '/'))
658 file
= strrchr(file
, '/') + 1;
660 trace_set_debug_header(&header
, subsys
, mask
, line
, CDEBUG_STACK());
662 tcd
= trace_get_tcd();
666 if (tcd
->tcd_shutting_down
) {
672 known_size
= strlen(file
) + 1;
674 known_size
+= strlen(fn
) + 1;
676 if (spl_debug_binary
)
677 known_size
+= sizeof(header
);
679 /* '2' used because vsnprintf returns real size required for output
680 * _without_ terminating NULL. */
681 for (i
= 0; i
< 2; i
++) {
682 tage
= trace_get_tage(tcd
, needed
+ known_size
+ 1);
684 if (needed
+ known_size
> PAGE_SIZE
)
692 string_buf
= (char *)page_address(tage
->page
) +
693 tage
->used
+ known_size
;
695 max_nob
= PAGE_SIZE
- tage
->used
- known_size
;
697 printk(KERN_EMERG
"negative max_nob: %i\n", max_nob
);
707 needed
= vsnprintf(string_buf
, max_nob
, format1
, ap
);
712 remain
= max_nob
- needed
;
716 va_start(ap
, format2
);
717 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
721 if (needed
< max_nob
)
725 if (unlikely(*(string_buf
+ needed
- 1) != '\n'))
726 printk(KERN_INFO
"format at %s:%d:%s doesn't end in newline\n",
729 header
.ph_len
= known_size
+ needed
;
730 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
732 if (spl_debug_binary
) {
733 memcpy(debug_buf
, &header
, sizeof(header
));
734 tage
->used
+= sizeof(header
);
735 debug_buf
+= sizeof(header
);
738 strcpy(debug_buf
, file
);
739 tage
->used
+= strlen(file
) + 1;
740 debug_buf
+= strlen(file
) + 1;
743 strcpy(debug_buf
, fn
);
744 tage
->used
+= strlen(fn
) + 1;
745 debug_buf
+= strlen(fn
) + 1;
748 __ASSERT(debug_buf
== string_buf
);
750 tage
->used
+= needed
;
751 __ASSERT (tage
->used
<= PAGE_SIZE
);
754 if ((mask
& spl_debug_printk
) == 0) {
755 /* no console output requested */
762 if (spl_console_ratelimit
&& cdls
->cdls_next
!= 0 &&
763 !time_before(cdls
->cdls_next
, jiffies
)) {
764 /* skipping a console message */
771 if (time_before(cdls
->cdls_next
+ spl_console_max_delay
+
772 (10 * HZ
), jiffies
)) {
773 /* last timeout was a long time ago */
774 cdls
->cdls_delay
/= spl_console_backoff
* 4;
776 cdls
->cdls_delay
*= spl_console_backoff
;
778 if (cdls
->cdls_delay
< spl_console_min_delay
)
779 cdls
->cdls_delay
= spl_console_min_delay
;
780 else if (cdls
->cdls_delay
> spl_console_max_delay
)
781 cdls
->cdls_delay
= spl_console_max_delay
;
784 /* ensure cdls_next is never zero after it's been seen */
785 cdls
->cdls_next
= (jiffies
+ cdls
->cdls_delay
) | 1;
789 trace_print_to_console(&header
, mask
, string_buf
, needed
, file
, fn
);
792 string_buf
= trace_get_console_buffer();
795 if (format1
!= NULL
) {
797 needed
= vsnprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
, format1
, ap
);
800 if (format2
!= NULL
) {
801 remain
= TRACE_CONSOLE_BUFFER_SIZE
- needed
;
803 va_start(ap
, format2
);
804 needed
+= vsnprintf(string_buf
+needed
, remain
, format2
, ap
);
808 trace_print_to_console(&header
, mask
,
809 string_buf
, needed
, file
, fn
);
811 trace_put_console_buffer(string_buf
);
814 if (cdls
!= NULL
&& cdls
->cdls_count
!= 0) {
815 string_buf
= trace_get_console_buffer();
817 needed
= snprintf(string_buf
, TRACE_CONSOLE_BUFFER_SIZE
,
818 "Skipped %d previous similar message%s\n",
819 cdls
->cdls_count
, (cdls
->cdls_count
> 1) ? "s" : "");
821 trace_print_to_console(&header
, mask
,
822 string_buf
, needed
, file
, fn
);
824 trace_put_console_buffer(string_buf
);
825 cdls
->cdls_count
= 0;
830 EXPORT_SYMBOL(spl_debug_vmsg
);
832 /* Do the collect_pages job on a single CPU: assumes that all other
833 * CPUs have been stopped during a panic. If this isn't true for
834 * some arch, this will have to be implemented separately in each arch.
837 collect_pages_from_single_cpu(struct page_collection
*pc
)
839 struct trace_cpu_data
*tcd
;
842 tcd_for_each(tcd
, i
, j
) {
843 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
844 tcd
->tcd_cur_pages
= 0;
849 collect_pages_on_all_cpus(struct page_collection
*pc
)
851 struct trace_cpu_data
*tcd
;
854 spin_lock(&pc
->pc_lock
);
855 for_each_possible_cpu(cpu
) {
856 tcd_for_each_type_lock(tcd
, i
, cpu
) {
857 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
858 tcd
->tcd_cur_pages
= 0;
861 spin_unlock(&pc
->pc_lock
);
865 collect_pages(dumplog_priv_t
*dp
, struct page_collection
*pc
)
867 INIT_LIST_HEAD(&pc
->pc_pages
);
869 if (spl_panic_in_progress
|| dp
->dp_flags
& DL_SINGLE_CPU
)
870 collect_pages_from_single_cpu(pc
);
872 collect_pages_on_all_cpus(pc
);
876 put_pages_back_on_all_cpus(struct page_collection
*pc
)
878 struct trace_cpu_data
*tcd
;
879 struct list_head
*cur_head
;
880 struct trace_page
*tage
;
881 struct trace_page
*tmp
;
884 spin_lock(&pc
->pc_lock
);
886 for_each_possible_cpu(cpu
) {
887 tcd_for_each_type_lock(tcd
, i
, cpu
) {
888 cur_head
= tcd
->tcd_pages
.next
;
890 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
893 __ASSERT_TAGE_INVARIANT(tage
);
895 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
898 tage_to_tail(tage
, cur_head
);
899 tcd
->tcd_cur_pages
++;
904 spin_unlock(&pc
->pc_lock
);
908 put_pages_back(struct page_collection
*pc
)
910 if (!spl_panic_in_progress
)
911 put_pages_back_on_all_cpus(pc
);
915 trace_filp_open (const char *name
, int flags
, int mode
, int *err
)
917 struct file
*filp
= NULL
;
920 filp
= filp_open(name
, flags
, mode
);
923 printk(KERN_ERR
"SPL: Can't open %s file: %d\n", name
, rc
);
931 #define trace_filp_write(fp, b, s, p) (fp)->f_op->write((fp), (b), (s), p)
932 #define trace_filp_fsync(fp) (fp)->f_op->fsync((fp),(fp)->f_dentry,1)
933 #define trace_filp_close(f) filp_close(f, NULL)
934 #define trace_filp_poff(f) (&(f)->f_pos)
937 spl_debug_dump_all_pages(dumplog_priv_t
*dp
, char *filename
)
939 struct page_collection pc
;
941 struct trace_page
*tage
;
942 struct trace_page
*tmp
;
946 down_write(&trace_sem
);
948 filp
= trace_filp_open(filename
, O_CREAT
|O_EXCL
|O_WRONLY
|O_LARGEFILE
,
952 printk(KERN_ERR
"SPL: Can't open %s for dump: %d\n",
957 spin_lock_init(&pc
.pc_lock
);
958 collect_pages(dp
, &pc
);
959 if (list_empty(&pc
.pc_pages
)) {
967 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
968 __ASSERT_TAGE_INVARIANT(tage
);
970 rc
= trace_filp_write(filp
, page_address(tage
->page
),
971 tage
->used
, trace_filp_poff(filp
));
972 if (rc
!= (int)tage
->used
) {
973 printk(KERN_WARNING
"SPL: Wanted to write %u "
974 "but wrote %d\n", tage
->used
, rc
);
976 __ASSERT(list_empty(&pc
.pc_pages
));
979 list_del(&tage
->linkage
);
985 rc
= trace_filp_fsync(filp
);
987 printk(KERN_ERR
"SPL: Unable to sync: %d\n", rc
);
989 trace_filp_close(filp
);
991 up_write(&trace_sem
);
997 spl_debug_flush_pages(void)
1000 struct page_collection pc
;
1001 struct trace_page
*tage
;
1002 struct trace_page
*tmp
;
1004 spin_lock_init(&pc
.pc_lock
);
1005 init_waitqueue_head(&dp
.dp_waitq
);
1006 dp
.dp_pid
= current
->pid
;
1008 atomic_set(&dp
.dp_done
, 0);
1010 collect_pages(&dp
, &pc
);
1011 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
1012 __ASSERT_TAGE_INVARIANT(tage
);
1013 list_del(&tage
->linkage
);
1019 spl_debug_set_mask(unsigned long mask
) {
1020 spl_debug_mask
= mask
;
1023 EXPORT_SYMBOL(spl_debug_set_mask
);
1026 spl_debug_get_mask(void) {
1027 return spl_debug_mask
;
1029 EXPORT_SYMBOL(spl_debug_get_mask
);
1032 spl_debug_set_subsys(unsigned long subsys
) {
1033 spl_debug_subsys
= subsys
;
1036 EXPORT_SYMBOL(spl_debug_set_subsys
);
1039 spl_debug_get_subsys(void) {
1040 return spl_debug_subsys
;
1042 EXPORT_SYMBOL(spl_debug_get_subsys
);
1045 spl_debug_set_mb(int mb
)
1048 int limit
= trace_max_debug_mb();
1049 struct trace_cpu_data
*tcd
;
1051 if (mb
< num_possible_cpus()) {
1052 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1053 "%dMB - lower limit is %d\n", mb
, num_possible_cpus());
1058 printk(KERN_ERR
"SPL: Refusing to set debug buffer size to "
1059 "%dMB - upper limit is %d\n", mb
, limit
);
1063 mb
/= num_possible_cpus();
1064 pages
= mb
<< (20 - PAGE_SHIFT
);
1066 down_write(&trace_sem
);
1068 tcd_for_each(tcd
, i
, j
)
1069 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
1071 up_write(&trace_sem
);
1075 EXPORT_SYMBOL(spl_debug_set_mb
);
1078 spl_debug_get_mb(void)
1081 struct trace_cpu_data
*tcd
;
1082 int total_pages
= 0;
1084 down_read(&trace_sem
);
1086 tcd_for_each(tcd
, i
, j
)
1087 total_pages
+= tcd
->tcd_max_pages
;
1089 up_read(&trace_sem
);
1091 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
1093 EXPORT_SYMBOL(spl_debug_get_mb
);
1095 void spl_debug_dumpstack(struct task_struct
*tsk
)
1097 extern void show_task(struct task_struct
*);
1102 printk(KERN_ERR
"SPL: Showing stack for process %d\n", tsk
->pid
);
1105 EXPORT_SYMBOL(spl_debug_dumpstack
);
1107 void spl_debug_bug(char *file
, const char *func
, const int line
, int flags
)
1109 spl_debug_catastrophe
= 1;
1110 spl_debug_msg(NULL
, 0, D_EMERG
, file
, func
, line
, "SBUG\n");
1112 if (in_interrupt()) {
1113 panic("SBUG in interrupt.\n");
1117 /* Ensure all debug pages and dumped by current cpu */
1118 if (spl_debug_panic_on_bug
)
1119 spl_panic_in_progress
= 1;
1121 spl_debug_dumpstack(NULL
);
1122 spl_debug_dumplog(flags
);
1124 if (spl_debug_panic_on_bug
)
1127 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
1131 EXPORT_SYMBOL(spl_debug_bug
);
1134 spl_debug_clear_buffer(void)
1136 spl_debug_flush_pages();
1139 EXPORT_SYMBOL(spl_debug_clear_buffer
);
1142 spl_debug_mark_buffer(char *text
)
1144 CDEBUG(D_WARNING
, "*************************************\n");
1145 CDEBUG(D_WARNING
, "DEBUG MARKER: %s\n", text
);
1146 CDEBUG(D_WARNING
, "*************************************\n");
1150 EXPORT_SYMBOL(spl_debug_mark_buffer
);
1153 trace_init(int max_pages
)
1155 struct trace_cpu_data
*tcd
;
1158 init_rwsem(&trace_sem
);
1160 /* initialize trace_data */
1161 memset(trace_data
, 0, sizeof(trace_data
));
1162 for (i
= 0; i
< TCD_TYPE_MAX
; i
++) {
1163 trace_data
[i
] = kmalloc(sizeof(union trace_data_union
) *
1164 NR_CPUS
, GFP_KERNEL
);
1165 if (trace_data
[i
] == NULL
)
1169 tcd_for_each(tcd
, i
, j
) {
1170 spin_lock_init(&tcd
->tcd_lock
);
1171 tcd
->tcd_pages_factor
= pages_factor
[i
];
1174 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1175 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1176 tcd
->tcd_cur_pages
= 0;
1177 tcd
->tcd_cur_stock_pages
= 0;
1178 tcd
->tcd_max_pages
= (max_pages
* pages_factor
[i
]) / 100;
1179 tcd
->tcd_shutting_down
= 0;
1182 for (i
= 0; i
< num_possible_cpus(); i
++) {
1183 for (j
= 0; j
< 3; j
++) {
1184 trace_console_buffers
[i
][j
] =
1185 kmalloc(TRACE_CONSOLE_BUFFER_SIZE
,
1188 if (trace_console_buffers
[i
][j
] == NULL
)
1196 printk(KERN_ERR
"SPL: Insufficient memory for debug logs\n");
1203 int rc
, max
= spl_debug_mb
;
1205 spl_console_max_delay
= SPL_DEFAULT_MAX_DELAY
;
1206 spl_console_min_delay
= SPL_DEFAULT_MIN_DELAY
;
1208 /* If spl_debug_mb is set to an invalid value or uninitialized
1209 * then just make the total buffers smp_num_cpus TCD_MAX_PAGES */
1210 if (max
> (num_physpages
>> (20 - 2 - PAGE_SHIFT
)) / 5 ||
1211 max
>= 512 || max
< 0) {
1212 max
= TCD_MAX_PAGES
;
1214 max
= (max
/ num_online_cpus()) << (20 - PAGE_SHIFT
);
1217 rc
= trace_init(max
);
1225 trace_cleanup_on_all_cpus(void)
1227 struct trace_cpu_data
*tcd
;
1228 struct trace_page
*tage
;
1229 struct trace_page
*tmp
;
1232 for_each_possible_cpu(cpu
) {
1233 tcd_for_each_type_lock(tcd
, i
, cpu
) {
1234 tcd
->tcd_shutting_down
= 1;
1236 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1238 __ASSERT_TAGE_INVARIANT(tage
);
1240 list_del(&tage
->linkage
);
1243 tcd
->tcd_cur_pages
= 0;
1253 trace_cleanup_on_all_cpus();
1255 for (i
= 0; i
< num_possible_cpus(); i
++) {
1256 for (j
= 0; j
< 3; j
++) {
1257 if (trace_console_buffers
[i
][j
] != NULL
) {
1258 kfree(trace_console_buffers
[i
][j
]);
1259 trace_console_buffers
[i
][j
] = NULL
;
1264 for (i
= 0; trace_data
[i
] != NULL
; i
++) {
1265 kfree(trace_data
[i
]);
1266 trace_data
[i
] = NULL
;