Whamcloud - gitweb
8cca867a1480358dc1353865d5829eda26599463
[fs/lustre-release.git] / libcfs / libcfs / tracefile.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) 2012, 2017, 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/tracefile.c
33  *
34  * Author: Zach Brown <zab@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LNET
39 #include "tracefile.h"
40
41 #include <linux/ctype.h>
42 #include <linux/fs.h>
43 #include <linux/kthread.h>
44 #include <linux/pagemap.h>
45 #include <linux/poll.h>
46 #include <linux/tty.h>
47 #include <linux/uaccess.h>
48 #include <libcfs/linux/linux-fs.h>
49 #include <libcfs/libcfs.h>
50
51 #define CFS_TRACE_CONSOLE_BUFFER_SIZE   1024
52 #define TCD_MAX_TYPES                   8
53
54 enum cfs_trace_buf_type {
55         CFS_TCD_TYPE_PROC = 0,
56         CFS_TCD_TYPE_SOFTIRQ,
57         CFS_TCD_TYPE_IRQ,
58         CFS_TCD_TYPE_MAX
59 };
60
61 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
62
63 char *cfs_trace_console_buffers[NR_CPUS][CFS_TCD_TYPE_MAX];
64 char cfs_tracefile[TRACEFILE_NAME_SIZE];
65 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
66 static struct tracefiled_ctl trace_tctl;
67 static DEFINE_MUTEX(cfs_trace_thread_mutex);
68 static int thread_running = 0;
69
70 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
71 static DECLARE_RWSEM(cfs_tracefile_sem);
72
73 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
74                                         struct cfs_trace_cpu_data *tcd);
75
76 /* trace file lock routines */
77 /* The walking argument indicates the locking comes from all tcd types
78  * iterator and we must lock it and dissable local irqs to avoid deadlocks
79  * with other interrupt locks that might be happening. See LU-1311
80  * for details.
81  */
82 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
83         __acquires(&tcd->tcd_lock)
84 {
85         __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
86         if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
87                 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
88         else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
89                 spin_lock_bh(&tcd->tcd_lock);
90         else if (unlikely(walking))
91                 spin_lock_irq(&tcd->tcd_lock);
92         else
93                 spin_lock(&tcd->tcd_lock);
94         return 1;
95 }
96
97 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
98         __releases(&tcd->tcd_lock)
99 {
100         __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
101         if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
102                 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
103         else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
104                 spin_unlock_bh(&tcd->tcd_lock);
105         else if (unlikely(walking))
106                 spin_unlock_irq(&tcd->tcd_lock);
107         else
108                 spin_unlock(&tcd->tcd_lock);
109 }
110
111 #define cfs_tcd_for_each(tcd, i, j)                                     \
112         for (i = 0; cfs_trace_data[i]; i++)                             \
113                 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd);     \
114                      j < num_possible_cpus();                           \
115                      j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
116
117 #define cfs_tcd_for_each_type_lock(tcd, i, cpu)                         \
118         for (i = 0; cfs_trace_data[i] &&                                \
119              (tcd = &(*cfs_trace_data[i])[cpu].tcd) &&                  \
120              cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
121
122 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
123 {
124         if (in_irq())
125                 return CFS_TCD_TYPE_IRQ;
126         if (in_softirq())
127                 return CFS_TCD_TYPE_SOFTIRQ;
128         return CFS_TCD_TYPE_PROC;
129 }
130
131 static inline char *cfs_trace_get_console_buffer(void)
132 {
133         unsigned int i = get_cpu();
134         unsigned int j = cfs_trace_buf_idx_get();
135
136         return cfs_trace_console_buffers[i][j];
137 }
138
139 static inline struct cfs_trace_cpu_data *
140 cfs_trace_get_tcd(void)
141 {
142         struct cfs_trace_cpu_data *tcd =
143                 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
144
145         cfs_trace_lock_tcd(tcd, 0);
146
147         return tcd;
148 }
149
150 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
151 {
152         cfs_trace_unlock_tcd(tcd, 0);
153
154         put_cpu();
155 }
156
157 static inline struct cfs_trace_page *
158 cfs_tage_from_list(struct list_head *list)
159 {
160         return list_entry(list, struct cfs_trace_page, linkage);
161 }
162
163 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
164 {
165         struct page            *page;
166         struct cfs_trace_page *tage;
167
168         /* My caller is trying to free memory */
169         if (!in_interrupt() && (current->flags & PF_MEMALLOC))
170                 return NULL;
171
172         /*
173          * Don't spam console with allocation failures: they will be reported
174          * by upper layer anyway.
175          */
176         gfp |= __GFP_NOWARN;
177         page = alloc_page(gfp);
178         if (page == NULL)
179                 return NULL;
180
181         tage = kmalloc(sizeof(*tage), gfp);
182         if (tage == NULL) {
183                 __free_page(page);
184                 return NULL;
185         }
186
187         tage->page = page;
188         atomic_inc(&cfs_tage_allocated);
189         return tage;
190 }
191
192 static void cfs_tage_free(struct cfs_trace_page *tage)
193 {
194         __LASSERT(tage != NULL);
195         __LASSERT(tage->page != NULL);
196
197         __free_page(tage->page);
198         kfree(tage);
199         atomic_dec(&cfs_tage_allocated);
200 }
201
202 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
203                              struct list_head *queue)
204 {
205         __LASSERT(tage != NULL);
206         __LASSERT(queue != NULL);
207
208         list_move_tail(&tage->linkage, queue);
209 }
210
211 /* return a page that has 'len' bytes left at the end */
212 static struct cfs_trace_page *
213 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
214 {
215         struct cfs_trace_page *tage;
216
217         if (tcd->tcd_cur_pages > 0) {
218                 __LASSERT(!list_empty(&tcd->tcd_pages));
219                 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
220                 if (tage->used + len <= PAGE_SIZE)
221                         return tage;
222         }
223
224         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
225                 if (tcd->tcd_cur_stock_pages > 0) {
226                         tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
227                         --tcd->tcd_cur_stock_pages;
228                         list_del_init(&tage->linkage);
229                 } else {
230                         tage = cfs_tage_alloc(GFP_ATOMIC);
231                         if (unlikely(tage == NULL)) {
232                                 if ((!(current->flags & PF_MEMALLOC) ||
233                                      in_interrupt()) && printk_ratelimit())
234                                         pr_warn("Lustre: cannot allocate a tage (%ld)\n",
235                                                 tcd->tcd_cur_pages);
236                                 return NULL;
237                         }
238                 }
239
240                 tage->used = 0;
241                 tage->cpu = smp_processor_id();
242                 tage->type = tcd->tcd_type;
243                 list_add_tail(&tage->linkage, &tcd->tcd_pages);
244                 tcd->tcd_cur_pages++;
245
246                 if (tcd->tcd_cur_pages > 8 && thread_running) {
247                         struct tracefiled_ctl *tctl = &trace_tctl;
248                         /*
249                          * wake up tracefiled to process some pages.
250                          */
251                         wake_up(&tctl->tctl_waitq);
252                 }
253                 return tage;
254         }
255         return NULL;
256 }
257
258 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
259 {
260         int pgcount = tcd->tcd_cur_pages / 10;
261         struct page_collection pc;
262         struct cfs_trace_page *tage;
263         struct cfs_trace_page *tmp;
264
265         /*
266          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
267          * from here: this will lead to infinite recursion.
268          */
269
270         if (printk_ratelimit())
271                 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
272                         pgcount + 1, tcd->tcd_cur_pages);
273
274         INIT_LIST_HEAD(&pc.pc_pages);
275
276         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
277                 if (pgcount-- == 0)
278                         break;
279
280                 list_move_tail(&tage->linkage, &pc.pc_pages);
281                 tcd->tcd_cur_pages--;
282         }
283         put_pages_on_tcd_daemon_list(&pc, tcd);
284 }
285
286 /* return a page that has 'len' bytes left at the end */
287 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
288                                                  unsigned long len)
289 {
290         struct cfs_trace_page *tage;
291
292         /*
293          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
294          * from here: this will lead to infinite recursion.
295          */
296
297         if (len > PAGE_SIZE) {
298                 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
299                        len);
300                 return NULL;
301         }
302
303         tage = cfs_trace_get_tage_try(tcd, len);
304         if (tage != NULL)
305                 return tage;
306         if (thread_running)
307                 cfs_tcd_shrink(tcd);
308         if (tcd->tcd_cur_pages > 0) {
309                 tage = cfs_tage_from_list(tcd->tcd_pages.next);
310                 tage->used = 0;
311                 cfs_tage_to_tail(tage, &tcd->tcd_pages);
312         }
313         return tage;
314 }
315
316 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
317                                     struct libcfs_debug_msg_data *msgdata,
318                                     unsigned long stack)
319 {
320         struct timespec64 ts;
321
322         ktime_get_real_ts64(&ts);
323
324         header->ph_subsys = msgdata->msg_subsys;
325         header->ph_mask = msgdata->msg_mask;
326         header->ph_cpu_id = smp_processor_id();
327         header->ph_type = cfs_trace_buf_idx_get();
328         /* y2038 safe since all user space treats this as unsigned, but
329          * will overflow in 2106
330          */
331         header->ph_sec = (u32)ts.tv_sec;
332         header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
333         header->ph_stack = stack;
334         header->ph_pid = current->pid;
335         header->ph_line_num = msgdata->msg_line;
336         header->ph_extern_pid = 0;
337 }
338
339 /**
340  * tty_write_msg - write a message to a certain tty, not just the console.
341  * @tty: the destination tty_struct
342  * @msg: the message to write
343  *
344  * tty_write_message is not exported, so write a same function for it
345  *
346  */
347 static void tty_write_msg(struct tty_struct *tty, const char *msg)
348 {
349         mutex_lock(&tty->atomic_write_lock);
350         tty_lock(tty);
351         if (tty->ops->write && tty->count > 0)
352                 tty->ops->write(tty, msg, strlen(msg));
353         tty_unlock(tty);
354         mutex_unlock(&tty->atomic_write_lock);
355         wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
356 }
357
358 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
359 {
360         struct tty_struct *tty;
361
362         tty = get_current_tty();
363         if (!tty)
364                 return;
365
366         tty_write_msg(tty, prefix);
367         if ((mask & D_EMERG) || (mask & D_ERROR))
368                 tty_write_msg(tty, "Error");
369         tty_write_msg(tty, ": ");
370         tty_write_msg(tty, msg);
371         tty_kref_put(tty);
372 }
373
374 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
375                                  const char *buf, int len, const char *file,
376                                  const char *fn)
377 {
378         char *prefix = "Lustre";
379
380         if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
381                 prefix = "LNet";
382
383         if (mask & D_CONSOLE) {
384                 if (mask & D_EMERG)
385                         pr_emerg("%sError: %.*s", prefix, len, buf);
386                 else if (mask & D_ERROR)
387                         pr_err("%sError: %.*s", prefix, len, buf);
388                 else if (mask & D_WARNING)
389                         pr_warn("%s: %.*s", prefix, len, buf);
390                 else if (mask & libcfs_printk)
391                         pr_info("%s: %.*s", prefix, len, buf);
392         } else {
393                 if (mask & D_EMERG)
394                         pr_emerg("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
395                                  hdr->ph_pid, hdr->ph_extern_pid, file,
396                                  hdr->ph_line_num, fn, len, buf);
397                 else if (mask & D_ERROR)
398                         pr_err("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
399                                hdr->ph_pid, hdr->ph_extern_pid, file,
400                                hdr->ph_line_num, fn, len, buf);
401                 else if (mask & D_WARNING)
402                         pr_warn("%s: %d:%d:(%s:%d:%s()) %.*s", prefix,
403                                 hdr->ph_pid, hdr->ph_extern_pid, file,
404                                 hdr->ph_line_num, fn, len, buf);
405                 else if (mask & (D_CONSOLE | libcfs_printk))
406                         pr_info("%s: %.*s", prefix, len, buf);
407         }
408
409         if (mask & D_TTY)
410                 cfs_tty_write_message(prefix, mask, buf);
411 }
412
413 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
414                      const char *format, ...)
415 {
416         struct cfs_trace_cpu_data *tcd = NULL;
417         struct ptldebug_header header = {0};
418         struct cfs_trace_page *tage;
419         /* string_buf is used only if tcd != NULL, and is always set then */
420         char *string_buf = NULL;
421         char *debug_buf;
422         int known_size;
423         int needed = 85; /* seeded with average message length */
424         int max_nob;
425         va_list ap;
426         int retry;
427         int mask = msgdata->msg_mask;
428         char *file = (char *)msgdata->msg_file;
429         struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
430
431         if (strchr(file, '/'))
432                 file = strrchr(file, '/') + 1;
433
434         tcd = cfs_trace_get_tcd();
435
436         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
437          * pins us to a particular CPU.  This avoids an smp_processor_id()
438          * warning on Linux when debugging is enabled.
439          */
440         cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
441
442         if (!tcd)                /* arch may not log in IRQ context */
443                 goto console;
444
445         if (tcd->tcd_cur_pages == 0)
446                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
447
448         if (tcd->tcd_shutting_down) {
449                 cfs_trace_put_tcd(tcd);
450                 tcd = NULL;
451                 goto console;
452         }
453
454         known_size = strlen(file) + 1;
455         if (msgdata->msg_fn)
456                 known_size += strlen(msgdata->msg_fn) + 1;
457
458         if (libcfs_debug_binary)
459                 known_size += sizeof(header);
460
461         /*
462          * May perform an additional pass to update 'needed' and increase
463          * tage buffer size to match vsnprintf reported size required
464          * On the second pass (retry=1) use vscnprintf [which returns
465          * number of bytes written not including the terminating nul]
466          * to clarify `needed` is used as number of bytes written
467          * for the remainder of this function
468          */
469         for (retry = 0; retry < 2; retry++) {
470                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
471                 if (!tage) {
472                         if (needed + known_size > PAGE_SIZE)
473                                 mask |= D_ERROR;
474
475                         cfs_trace_put_tcd(tcd);
476                         tcd = NULL;
477                         goto console;
478                 }
479
480                 string_buf = (char *)page_address(tage->page) +
481                              tage->used + known_size;
482
483                 max_nob = PAGE_SIZE - tage->used - known_size;
484                 if (max_nob <= 0) {
485                         pr_emerg("LustreError: negative max_nob: %d\n",
486                                  max_nob);
487                         mask |= D_ERROR;
488                         cfs_trace_put_tcd(tcd);
489                         tcd = NULL;
490                         goto console;
491                 }
492
493                 va_start(ap, format);
494                 if (retry)
495                         needed = vscnprintf(string_buf, max_nob, format, ap);
496                 else
497                         needed = vsnprintf(string_buf, max_nob, format, ap);
498                 va_end(ap);
499
500                 if (needed < max_nob) /* well. printing ok.. */
501                         break;
502         }
503
504         /* `needed` is actual bytes written to string_buf */
505         if (*(string_buf + needed - 1) != '\n') {
506                 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
507                         file, msgdata->msg_line, msgdata->msg_fn);
508         } else if (mask & D_TTY) {
509                 /* TTY needs '\r\n' to move carriage to leftmost position */
510                 if (needed < 2 || *(string_buf + needed - 2) != '\r')
511                         pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
512                                 file, msgdata->msg_line, msgdata->msg_fn);
513         }
514
515         header.ph_len = known_size + needed;
516         debug_buf = (char *)page_address(tage->page) + tage->used;
517
518         if (libcfs_debug_binary) {
519                 memcpy(debug_buf, &header, sizeof(header));
520                 tage->used += sizeof(header);
521                 debug_buf += sizeof(header);
522         }
523
524         strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
525         tage->used += strlen(file) + 1;
526         debug_buf += strlen(file) + 1;
527
528         if (msgdata->msg_fn) {
529                 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
530                 tage->used += strlen(msgdata->msg_fn) + 1;
531                 debug_buf += strlen(msgdata->msg_fn) + 1;
532         }
533
534         __LASSERT(debug_buf == string_buf);
535
536         tage->used += needed;
537         __LASSERT(tage->used <= PAGE_SIZE);
538
539 console:
540         if ((mask & libcfs_printk) == 0) {
541                 /* no console output requested */
542                 if (tcd != NULL)
543                         cfs_trace_put_tcd(tcd);
544                 return 1;
545         }
546
547         if (cdls != NULL) {
548                 if (libcfs_console_ratelimit &&
549                     cdls->cdls_next != 0 &&     /* not first time ever */
550                     time_before(jiffies, cdls->cdls_next)) {
551                         /* skipping a console message */
552                         cdls->cdls_count++;
553                         if (tcd != NULL)
554                                 cfs_trace_put_tcd(tcd);
555                         return 1;
556                 }
557
558                 if (time_after(jiffies, cdls->cdls_next +
559                                         libcfs_console_max_delay +
560                                         cfs_time_seconds(10))) {
561                         /* last timeout was a long time ago */
562                         cdls->cdls_delay /= libcfs_console_backoff * 4;
563                 } else {
564                         cdls->cdls_delay *= libcfs_console_backoff;
565                 }
566
567                 if (cdls->cdls_delay < libcfs_console_min_delay)
568                         cdls->cdls_delay = libcfs_console_min_delay;
569                 else if (cdls->cdls_delay > libcfs_console_max_delay)
570                         cdls->cdls_delay = libcfs_console_max_delay;
571
572                 /* ensure cdls_next is never zero after it's been seen */
573                 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
574         }
575
576         if (tcd) {
577                 cfs_print_to_console(&header, mask, string_buf, needed, file,
578                                      msgdata->msg_fn);
579                 cfs_trace_put_tcd(tcd);
580         } else {
581                 string_buf = cfs_trace_get_console_buffer();
582
583                 va_start(ap, format);
584                 needed = vscnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
585                                     format, ap);
586                 va_end(ap);
587
588                 cfs_print_to_console(&header, mask,
589                                      string_buf, needed, file, msgdata->msg_fn);
590
591                 put_cpu();
592         }
593
594         if (cdls != NULL && cdls->cdls_count != 0) {
595                 string_buf = cfs_trace_get_console_buffer();
596
597                 needed = scnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
598                                    "Skipped %d previous similar message%s\n",
599                                    cdls->cdls_count,
600                                    (cdls->cdls_count > 1) ? "s" : "");
601
602                 /* Do not allow print this to TTY */
603                 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
604                                      needed, file, msgdata->msg_fn);
605
606                 put_cpu();
607                 cdls->cdls_count = 0;
608         }
609
610         return 0;
611 }
612 EXPORT_SYMBOL(libcfs_debug_msg);
613
614 void
615 cfs_trace_assertion_failed(const char *str,
616                            struct libcfs_debug_msg_data *msgdata)
617 {
618         struct ptldebug_header hdr;
619
620         libcfs_panic_in_progress = 1;
621         libcfs_catastrophe = 1;
622         smp_mb();
623
624         cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
625
626         cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
627                              msgdata->msg_file, msgdata->msg_fn);
628
629         panic("Lustre debug assertion failure\n");
630
631         /* not reached */
632 }
633
634 static void
635 panic_collect_pages(struct page_collection *pc)
636 {
637         /* Do the collect_pages job on a single CPU: assumes that all other
638          * CPUs have been stopped during a panic.  If this isn't true for some
639          * arch, this will have to be implemented separately in each arch.  */
640         int                        i;
641         int                        j;
642         struct cfs_trace_cpu_data *tcd;
643
644         INIT_LIST_HEAD(&pc->pc_pages);
645
646         cfs_tcd_for_each(tcd, i, j) {
647                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
648                 tcd->tcd_cur_pages = 0;
649
650                 if (pc->pc_want_daemon_pages) {
651                         list_splice_init(&tcd->tcd_daemon_pages,
652                                                 &pc->pc_pages);
653                         tcd->tcd_cur_daemon_pages = 0;
654                 }
655         }
656 }
657
658 static void collect_pages_on_all_cpus(struct page_collection *pc)
659 {
660         struct cfs_trace_cpu_data *tcd;
661         int i, cpu;
662
663         for_each_possible_cpu(cpu) {
664                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
665                         list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
666                         tcd->tcd_cur_pages = 0;
667                         if (pc->pc_want_daemon_pages) {
668                                 list_splice_init(&tcd->tcd_daemon_pages,
669                                                         &pc->pc_pages);
670                                 tcd->tcd_cur_daemon_pages = 0;
671                         }
672                 }
673         }
674 }
675
676 static void collect_pages(struct page_collection *pc)
677 {
678         INIT_LIST_HEAD(&pc->pc_pages);
679
680         if (libcfs_panic_in_progress)
681                 panic_collect_pages(pc);
682         else
683                 collect_pages_on_all_cpus(pc);
684 }
685
686 static void put_pages_back_on_all_cpus(struct page_collection *pc)
687 {
688         struct cfs_trace_cpu_data *tcd;
689         struct list_head *cur_head;
690         struct cfs_trace_page *tage;
691         struct cfs_trace_page *tmp;
692         int i, cpu;
693
694         for_each_possible_cpu(cpu) {
695                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
696                         cur_head = tcd->tcd_pages.next;
697
698                         list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
699                                                  linkage) {
700
701                                 __LASSERT_TAGE_INVARIANT(tage);
702
703                                 if (tage->cpu != cpu || tage->type != i)
704                                         continue;
705
706                                 cfs_tage_to_tail(tage, cur_head);
707                                 tcd->tcd_cur_pages++;
708                         }
709                 }
710         }
711 }
712
713 static void put_pages_back(struct page_collection *pc)
714 {
715         if (!libcfs_panic_in_progress)
716                 put_pages_back_on_all_cpus(pc);
717 }
718
719 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
720  * we have a good amount of data at all times for dumping during an LBUG, even
721  * if we have been steadily writing (and otherwise discarding) pages via the
722  * debug daemon. */
723 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
724                                          struct cfs_trace_cpu_data *tcd)
725 {
726         struct cfs_trace_page *tage;
727         struct cfs_trace_page *tmp;
728
729         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
730                 __LASSERT_TAGE_INVARIANT(tage);
731
732                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
733                         continue;
734
735                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
736                 tcd->tcd_cur_daemon_pages++;
737
738                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
739                         struct cfs_trace_page *victim;
740
741                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
742                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
743
744                         __LASSERT_TAGE_INVARIANT(victim);
745
746                         list_del(&victim->linkage);
747                         cfs_tage_free(victim);
748                         tcd->tcd_cur_daemon_pages--;
749                 }
750         }
751 }
752
753 static void put_pages_on_daemon_list(struct page_collection *pc)
754 {
755         struct cfs_trace_cpu_data *tcd;
756         int i, cpu;
757
758         for_each_possible_cpu(cpu) {
759                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
760                         put_pages_on_tcd_daemon_list(pc, tcd);
761         }
762 }
763
764 void cfs_trace_debug_print(void)
765 {
766         struct page_collection pc;
767         struct cfs_trace_page *tage;
768         struct cfs_trace_page *tmp;
769
770         pc.pc_want_daemon_pages = 1;
771         collect_pages(&pc);
772         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
773                 char *p, *file, *fn;
774                 struct page *page;
775
776                 __LASSERT_TAGE_INVARIANT(tage);
777
778                 page = tage->page;
779                 p = page_address(page);
780                 while (p < ((char *)page_address(page) + tage->used)) {
781                         struct ptldebug_header *hdr;
782                         int len;
783                         hdr = (void *)p;
784                         p += sizeof(*hdr);
785                         file = p;
786                         p += strlen(file) + 1;
787                         fn = p;
788                         p += strlen(fn) + 1;
789                         len = hdr->ph_len - (int)(p - (char *)hdr);
790
791                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
792
793                         p += len;
794                 }
795
796                 list_del(&tage->linkage);
797                 cfs_tage_free(tage);
798         }
799 }
800
801 int cfs_tracefile_dump_all_pages(char *filename)
802 {
803         struct page_collection  pc;
804         struct file             *filp;
805         struct cfs_trace_page   *tage;
806         struct cfs_trace_page   *tmp;
807         char                    *buf;
808         int rc;
809
810         down_write(&cfs_tracefile_sem);
811
812         filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
813         if (IS_ERR(filp)) {
814                 rc = PTR_ERR(filp);
815                 filp = NULL;
816                 pr_err("LustreError: can't open %s for dump: rc = %d\n",
817                       filename, rc);
818                 goto out;
819         }
820
821         pc.pc_want_daemon_pages = 1;
822         collect_pages(&pc);
823         if (list_empty(&pc.pc_pages)) {
824                 rc = 0;
825                 goto close;
826         }
827
828         /* ok, for now, just write the pages.  in the future we'll be building
829          * iobufs with the pages and calling generic_direct_IO */
830         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
831
832                 __LASSERT_TAGE_INVARIANT(tage);
833
834                 buf = kmap(tage->page);
835                 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
836                 kunmap(tage->page);
837                 if (rc != (int)tage->used) {
838                         pr_warn("Lustre: wanted to write %u but wrote %d\n",
839                                 tage->used, rc);
840                         put_pages_back(&pc);
841                         __LASSERT(list_empty(&pc.pc_pages));
842                         break;
843                 }
844                 list_del(&tage->linkage);
845                 cfs_tage_free(tage);
846         }
847
848         rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
849         if (rc)
850                 pr_err("LustreError: sync returns: rc = %d\n", rc);
851 close:
852         filp_close(filp, NULL);
853 out:
854         up_write(&cfs_tracefile_sem);
855         return rc;
856 }
857
858 void cfs_trace_flush_pages(void)
859 {
860         struct page_collection pc;
861         struct cfs_trace_page *tage;
862         struct cfs_trace_page *tmp;
863
864         pc.pc_want_daemon_pages = 1;
865         collect_pages(&pc);
866         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
867
868                 __LASSERT_TAGE_INVARIANT(tage);
869
870                 list_del(&tage->linkage);
871                 cfs_tage_free(tage);
872         }
873 }
874
875 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
876                             const char __user *usr_buffer, int usr_buffer_nob)
877 {
878         int    nob;
879
880         if (usr_buffer_nob > knl_buffer_nob)
881                 return -EOVERFLOW;
882
883         if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
884                 return -EFAULT;
885
886         nob = strnlen(knl_buffer, usr_buffer_nob);
887         while (--nob >= 0)                      /* strip trailing whitespace */
888                 if (!isspace(knl_buffer[nob]))
889                         break;
890
891         if (nob < 0)                            /* empty string */
892                 return -EINVAL;
893
894         if (nob == knl_buffer_nob)              /* no space to terminate */
895                 return -EOVERFLOW;
896
897         knl_buffer[nob + 1] = 0;                /* terminate */
898         return 0;
899 }
900 EXPORT_SYMBOL(cfs_trace_copyin_string);
901
902 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
903                              const char *knl_buffer, char *append)
904 {
905         /* NB if 'append' != NULL, it's a single character to append to the
906          * copied out string - usually "\n", for /proc entries and "" (i.e. a
907          * terminating zero byte) for sysctl entries */
908         int   nob = strlen(knl_buffer);
909
910         if (nob > usr_buffer_nob)
911                 nob = usr_buffer_nob;
912
913         if (copy_to_user(usr_buffer, knl_buffer, nob))
914                 return -EFAULT;
915
916         if (append != NULL && nob < usr_buffer_nob) {
917                 if (copy_to_user(usr_buffer + nob, append, 1))
918                         return -EFAULT;
919
920                 nob++;
921         }
922
923         return nob;
924 }
925 EXPORT_SYMBOL(cfs_trace_copyout_string);
926
927 int cfs_trace_allocate_string_buffer(char **str, int nob)
928 {
929         if (nob > 2 * PAGE_SIZE)        /* string must be "sensible" */
930                 return -EINVAL;
931
932         *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
933         if (*str == NULL)
934                 return -ENOMEM;
935
936         return 0;
937 }
938
939 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
940 {
941         char         *str;
942         int           rc;
943
944         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
945         if (rc != 0)
946                 return rc;
947
948         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
949                                      usr_str, usr_str_nob);
950         if (rc != 0)
951                 goto out;
952
953         if (str[0] != '/') {
954                 rc = -EINVAL;
955                 goto out;
956         }
957         rc = cfs_tracefile_dump_all_pages(str);
958 out:
959         kfree(str);
960         return rc;
961 }
962
963 int cfs_trace_daemon_command(char *str)
964 {
965         int       rc = 0;
966
967         down_write(&cfs_tracefile_sem);
968
969         if (strcmp(str, "stop") == 0) {
970                 up_write(&cfs_tracefile_sem);
971                 cfs_trace_stop_thread();
972                 down_write(&cfs_tracefile_sem);
973                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
974
975         } else if (strncmp(str, "size=", 5) == 0) {
976                 unsigned long tmp;
977
978                 rc = kstrtoul(str + 5, 10, &tmp);
979                 if (!rc) {
980                         if (tmp < 10 || tmp > 20480)
981                                 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
982                         else
983                                 cfs_tracefile_size = tmp << 20;
984                 }
985         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
986                 rc = -ENAMETOOLONG;
987         } else if (str[0] != '/') {
988                 rc = -EINVAL;
989         } else {
990                 strcpy(cfs_tracefile, str);
991
992                 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
993                         cfs_tracefile, (long)(cfs_tracefile_size >> 10));
994
995                 cfs_trace_start_thread();
996         }
997
998         up_write(&cfs_tracefile_sem);
999         return rc;
1000 }
1001
1002 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
1003 {
1004         char *str;
1005         int   rc;
1006
1007         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1008         if (rc != 0)
1009                 return rc;
1010
1011         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1012                                  usr_str, usr_str_nob);
1013         if (rc == 0)
1014                 rc = cfs_trace_daemon_command(str);
1015
1016         kfree(str);
1017         return rc;
1018 }
1019
1020 int cfs_trace_set_debug_mb(int mb)
1021 {
1022         int i;
1023         int j;
1024         unsigned long pages;
1025         unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
1026         unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
1027         struct cfs_trace_cpu_data *tcd;
1028
1029         if (mb < num_possible_cpus()) {
1030                 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
1031                         mb, num_possible_cpus());
1032                 mb = num_possible_cpus();
1033         }
1034
1035         if (mb > limit) {
1036                 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
1037                         mb, limit);
1038                 mb = limit;
1039         }
1040
1041         mb /= num_possible_cpus();
1042         pages = mb << (20 - PAGE_SHIFT);
1043
1044         down_write(&cfs_tracefile_sem);
1045
1046         cfs_tcd_for_each(tcd, i, j)
1047                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1048
1049         up_write(&cfs_tracefile_sem);
1050
1051         return mb;
1052 }
1053
1054 int cfs_trace_get_debug_mb(void)
1055 {
1056         int i;
1057         int j;
1058         struct cfs_trace_cpu_data *tcd;
1059         int total_pages = 0;
1060
1061         down_read(&cfs_tracefile_sem);
1062
1063         cfs_tcd_for_each(tcd, i, j)
1064                 total_pages += tcd->tcd_max_pages;
1065
1066         up_read(&cfs_tracefile_sem);
1067
1068         return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1069 }
1070
1071 static int tracefiled(void *arg)
1072 {
1073         struct page_collection pc;
1074         struct tracefiled_ctl *tctl = arg;
1075         struct cfs_trace_page *tage;
1076         struct cfs_trace_page *tmp;
1077         struct file *filp;
1078         char *buf;
1079         int last_loop = 0;
1080         int rc;
1081
1082         /* we're started late enough that we pick up init's fs context */
1083         /* this is so broken in uml?  what on earth is going on? */
1084
1085         complete(&tctl->tctl_start);
1086
1087         while (1) {
1088                 wait_queue_entry_t __wait;
1089
1090                 pc.pc_want_daemon_pages = 0;
1091                 collect_pages(&pc);
1092                 if (list_empty(&pc.pc_pages))
1093                         goto end_loop;
1094
1095                 filp = NULL;
1096                 down_read(&cfs_tracefile_sem);
1097                 if (cfs_tracefile[0] != 0) {
1098                         filp = filp_open(cfs_tracefile,
1099                                          O_CREAT | O_RDWR | O_LARGEFILE,
1100                                          0600);
1101                         if (IS_ERR(filp)) {
1102                                 rc = PTR_ERR(filp);
1103                                 filp = NULL;
1104                                 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1105                                         cfs_tracefile, rc);
1106                         }
1107                 }
1108                 up_read(&cfs_tracefile_sem);
1109                 if (filp == NULL) {
1110                         put_pages_on_daemon_list(&pc);
1111                         __LASSERT(list_empty(&pc.pc_pages));
1112                         goto end_loop;
1113                 }
1114
1115                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1116                         struct dentry *de = file_dentry(filp);
1117                         static loff_t f_pos;
1118
1119                         __LASSERT_TAGE_INVARIANT(tage);
1120
1121                         if (f_pos >= (off_t)cfs_tracefile_size)
1122                                 f_pos = 0;
1123                         else if (f_pos > i_size_read(de->d_inode))
1124                                 f_pos = i_size_read(de->d_inode);
1125
1126                         buf = kmap(tage->page);
1127                         rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1128                         kunmap(tage->page);
1129                         if (rc != (int)tage->used) {
1130                                 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1131                                         tage->used, rc);
1132                                 put_pages_back(&pc);
1133                                 __LASSERT(list_empty(&pc.pc_pages));
1134                                 break;
1135                         }
1136                 }
1137
1138                 filp_close(filp, NULL);
1139                 put_pages_on_daemon_list(&pc);
1140                 if (!list_empty(&pc.pc_pages)) {
1141                         int i;
1142
1143                         pr_alert("Lustre: trace pages aren't empty\n");
1144                         pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1145                         for (i = 0; i < num_possible_cpus(); i++)
1146                                 if (cpu_online(i))
1147                                         pr_cont("%d(on) ", i);
1148                                 else
1149                                         pr_cont("%d(off) ", i);
1150                         pr_cont("\n");
1151
1152                         i = 0;
1153                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1154                                                  linkage)
1155                                 pr_err("Lustre: page %d belongs to cpu %d\n",
1156                                        ++i, tage->cpu);
1157                         pr_err("Lustre: There are %d pages unwritten\n", i);
1158                 }
1159                 __LASSERT(list_empty(&pc.pc_pages));
1160 end_loop:
1161                 if (atomic_read(&tctl->tctl_shutdown)) {
1162                         if (last_loop == 0) {
1163                                 last_loop = 1;
1164                                 continue;
1165                         } else {
1166                                 break;
1167                         }
1168                 }
1169                 init_wait(&__wait);
1170                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1171                 schedule_timeout_interruptible(cfs_time_seconds(1));
1172                 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1173         }
1174         complete(&tctl->tctl_stop);
1175         return 0;
1176 }
1177
1178 int cfs_trace_start_thread(void)
1179 {
1180         struct tracefiled_ctl *tctl = &trace_tctl;
1181         int rc = 0;
1182
1183         mutex_lock(&cfs_trace_thread_mutex);
1184         if (thread_running)
1185                 goto out;
1186
1187         init_completion(&tctl->tctl_start);
1188         init_completion(&tctl->tctl_stop);
1189         init_waitqueue_head(&tctl->tctl_waitq);
1190         atomic_set(&tctl->tctl_shutdown, 0);
1191
1192         if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1193                 rc = -ECHILD;
1194                 goto out;
1195         }
1196
1197         wait_for_completion(&tctl->tctl_start);
1198         thread_running = 1;
1199 out:
1200         mutex_unlock(&cfs_trace_thread_mutex);
1201         return rc;
1202 }
1203
1204 void cfs_trace_stop_thread(void)
1205 {
1206         struct tracefiled_ctl *tctl = &trace_tctl;
1207
1208         mutex_lock(&cfs_trace_thread_mutex);
1209         if (thread_running) {
1210                 pr_info("Lustre: shutting down debug daemon thread...\n");
1211                 atomic_set(&tctl->tctl_shutdown, 1);
1212                 wait_for_completion(&tctl->tctl_stop);
1213                 thread_running = 0;
1214         }
1215         mutex_unlock(&cfs_trace_thread_mutex);
1216 }
1217
1218 /* percents to share the total debug memory for each type */
1219 static unsigned int pages_factor[CFS_TCD_TYPE_MAX] = {
1220         80, /* 80% pages for CFS_TCD_TYPE_PROC */
1221         10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
1222         10  /* 10% pages for CFS_TCD_TYPE_IRQ */
1223 };
1224
1225 int cfs_tracefile_init(int max_pages)
1226 {
1227         struct cfs_trace_cpu_data *tcd;
1228         int i;
1229         int j;
1230
1231         /* initialize trace_data */
1232         memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
1233         for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
1234                 cfs_trace_data[i] =
1235                         kmalloc_array(num_possible_cpus(),
1236                                       sizeof(union cfs_trace_data_union),
1237                                       GFP_KERNEL);
1238                 if (!cfs_trace_data[i])
1239                         goto out_trace_data;
1240         }
1241
1242         /* arch related info initialized */
1243         cfs_tcd_for_each(tcd, i, j) {
1244                 int factor = pages_factor[i];
1245
1246                 spin_lock_init(&tcd->tcd_lock);
1247                 tcd->tcd_pages_factor = factor;
1248                 tcd->tcd_type = i;
1249                 tcd->tcd_cpu = j;
1250
1251                 INIT_LIST_HEAD(&tcd->tcd_pages);
1252                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1253                 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1254                 tcd->tcd_cur_pages = 0;
1255                 tcd->tcd_cur_stock_pages = 0;
1256                 tcd->tcd_cur_daemon_pages = 0;
1257                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1258                 LASSERT(tcd->tcd_max_pages > 0);
1259                 tcd->tcd_shutting_down = 0;
1260         }
1261
1262         for (i = 0; i < num_possible_cpus(); i++)
1263                 for (j = 0; j < 3; j++) {
1264                         cfs_trace_console_buffers[i][j] =
1265                                 kmalloc(CFS_TRACE_CONSOLE_BUFFER_SIZE,
1266                                         GFP_KERNEL);
1267                         if (!cfs_trace_console_buffers[i][j])
1268                                 goto out_buffers;
1269                 }
1270
1271         return 0;
1272
1273 out_buffers:
1274         for (i = 0; i < num_possible_cpus(); i++)
1275                 for (j = 0; j < 3; j++) {
1276                         kfree(cfs_trace_console_buffers[i][j]);
1277                         cfs_trace_console_buffers[i][j] = NULL;
1278                 }
1279 out_trace_data:
1280         for (i = 0; cfs_trace_data[i]; i++) {
1281                 kfree(cfs_trace_data[i]);
1282                 cfs_trace_data[i] = NULL;
1283         }
1284         pr_err("lnet: Not enough memory\n");
1285         return -ENOMEM;
1286 }
1287
1288 static void trace_cleanup_on_all_cpus(void)
1289 {
1290         struct cfs_trace_cpu_data *tcd;
1291         struct cfs_trace_page *tage;
1292         struct cfs_trace_page *tmp;
1293         int i, cpu;
1294
1295         for_each_possible_cpu(cpu) {
1296                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1297                         if (!tcd->tcd_pages_factor)
1298                                 /* Not initialised */
1299                                 continue;
1300                         tcd->tcd_shutting_down = 1;
1301
1302                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1303                                 __LASSERT_TAGE_INVARIANT(tage);
1304
1305                                 list_del(&tage->linkage);
1306                                 cfs_tage_free(tage);
1307                         }
1308                         tcd->tcd_cur_pages = 0;
1309                 }
1310         }
1311 }
1312
1313 static void cfs_trace_cleanup(void)
1314 {
1315         struct page_collection pc;
1316         int i;
1317         int j;
1318
1319         INIT_LIST_HEAD(&pc.pc_pages);
1320
1321         trace_cleanup_on_all_cpus();
1322
1323         for (i = 0; i < num_possible_cpus(); i++)
1324                 for (j = 0; j < 3; j++) {
1325                         kfree(cfs_trace_console_buffers[i][j]);
1326                         cfs_trace_console_buffers[i][j] = NULL;
1327                 }
1328
1329         for (i = 0; cfs_trace_data[i]; i++) {
1330                 kfree(cfs_trace_data[i]);
1331                 cfs_trace_data[i] = NULL;
1332         }
1333 }
1334
1335 void cfs_tracefile_exit(void)
1336 {
1337         cfs_trace_stop_thread();
1338         cfs_trace_cleanup();
1339 }