4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/tracefile.c
34 * Author: Zach Brown <zab@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LNET
40 #define LUSTRE_TRACEFILE_PRIVATE
41 #include "tracefile.h"
43 #include <linux/ctype.h>
45 #include <linux/kthread.h>
46 #include <linux/pagemap.h>
47 #include <linux/poll.h>
48 #include <linux/tty.h>
49 #include <linux/uaccess.h>
50 #include <libcfs/linux/linux-fs.h>
51 #include <libcfs/libcfs.h>
53 #define TCD_MAX_TYPES 8
55 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
57 char *cfs_trace_console_buffers[NR_CPUS][CFS_TCD_TYPE_MAX];
58 char cfs_tracefile[TRACEFILE_NAME_SIZE];
59 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
60 static struct tracefiled_ctl trace_tctl;
61 static DEFINE_MUTEX(cfs_trace_thread_mutex);
62 static int thread_running = 0;
64 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
65 static DECLARE_RWSEM(cfs_tracefile_sem);
67 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
68 struct cfs_trace_cpu_data *tcd);
70 /* trace file lock routines */
71 /* The walking argument indicates the locking comes from all tcd types
72 * iterator and we must lock it and dissable local irqs to avoid deadlocks
73 * with other interrupt locks that might be happening. See LU-1311
76 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
77 __acquires(&tcd->tcd_lock)
79 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
80 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
81 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
82 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
83 spin_lock_bh(&tcd->tcd_lock);
84 else if (unlikely(walking))
85 spin_lock_irq(&tcd->tcd_lock);
87 spin_lock(&tcd->tcd_lock);
91 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
92 __releases(&tcd->tcd_lock)
94 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
95 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
96 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
97 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
98 spin_unlock_bh(&tcd->tcd_lock);
99 else if (unlikely(walking))
100 spin_unlock_irq(&tcd->tcd_lock);
102 spin_unlock(&tcd->tcd_lock);
105 #define cfs_tcd_for_each(tcd, i, j) \
106 for (i = 0; cfs_trace_data[i]; i++) \
107 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd); \
108 j < num_possible_cpus(); \
109 j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
111 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
112 for (i = 0; cfs_trace_data[i] && \
113 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
114 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
116 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
119 return CFS_TCD_TYPE_IRQ;
121 return CFS_TCD_TYPE_SOFTIRQ;
122 return CFS_TCD_TYPE_PROC;
125 static inline struct cfs_trace_cpu_data *
126 cfs_trace_get_tcd(void)
128 struct cfs_trace_cpu_data *tcd =
129 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
131 cfs_trace_lock_tcd(tcd, 0);
136 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
138 cfs_trace_unlock_tcd(tcd, 0);
143 /* percents to share the total debug memory for each type */
144 static unsigned int pages_factor[CFS_TCD_TYPE_MAX] = {
145 80, /* 80% pages for CFS_TCD_TYPE_PROC */
146 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
147 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
150 int cfs_tracefile_init_arch(void)
152 struct cfs_trace_cpu_data *tcd;
156 /* initialize trace_data */
157 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
158 for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
160 kmalloc_array(num_possible_cpus(),
161 sizeof(union cfs_trace_data_union),
163 if (!cfs_trace_data[i])
167 /* arch related info initialized */
168 cfs_tcd_for_each(tcd, i, j) {
169 spin_lock_init(&tcd->tcd_lock);
170 tcd->tcd_pages_factor = pages_factor[i];
175 for (i = 0; i < num_possible_cpus(); i++)
176 for (j = 0; j < 3; j++) {
177 cfs_trace_console_buffers[i][j] =
178 kmalloc(CFS_TRACE_CONSOLE_BUFFER_SIZE,
181 if (!cfs_trace_console_buffers[i][j])
188 for (i = 0; i < num_possible_cpus(); i++)
189 for (j = 0; j < 3; j++) {
190 kfree(cfs_trace_console_buffers[i][j]);
191 cfs_trace_console_buffers[i][j] = NULL;
194 for (i = 0; cfs_trace_data[i]; i++) {
195 kfree(cfs_trace_data[i]);
196 cfs_trace_data[i] = NULL;
198 pr_err("lnet: Not enough memory\n");
202 void cfs_tracefile_fini_arch(void)
207 for (i = 0; i < num_possible_cpus(); i++)
208 for (j = 0; j < 3; j++) {
209 kfree(cfs_trace_console_buffers[i][j]);
210 cfs_trace_console_buffers[i][j] = NULL;
213 for (i = 0; cfs_trace_data[i]; i++) {
214 kfree(cfs_trace_data[i]);
215 cfs_trace_data[i] = NULL;
219 static inline struct cfs_trace_page *
220 cfs_tage_from_list(struct list_head *list)
222 return list_entry(list, struct cfs_trace_page, linkage);
225 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
228 struct cfs_trace_page *tage;
230 /* My caller is trying to free memory */
231 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
235 * Don't spam console with allocation failures: they will be reported
236 * by upper layer anyway.
239 page = alloc_page(gfp);
243 tage = kmalloc(sizeof(*tage), gfp);
250 atomic_inc(&cfs_tage_allocated);
254 static void cfs_tage_free(struct cfs_trace_page *tage)
256 __LASSERT(tage != NULL);
257 __LASSERT(tage->page != NULL);
259 __free_page(tage->page);
261 atomic_dec(&cfs_tage_allocated);
264 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
265 struct list_head *queue)
267 __LASSERT(tage != NULL);
268 __LASSERT(queue != NULL);
270 list_move_tail(&tage->linkage, queue);
273 /* return a page that has 'len' bytes left at the end */
274 static struct cfs_trace_page *
275 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
277 struct cfs_trace_page *tage;
279 if (tcd->tcd_cur_pages > 0) {
280 __LASSERT(!list_empty(&tcd->tcd_pages));
281 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
282 if (tage->used + len <= PAGE_SIZE)
286 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
287 if (tcd->tcd_cur_stock_pages > 0) {
288 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
289 --tcd->tcd_cur_stock_pages;
290 list_del_init(&tage->linkage);
292 tage = cfs_tage_alloc(GFP_ATOMIC);
293 if (unlikely(tage == NULL)) {
294 if ((!(current->flags & PF_MEMALLOC) ||
295 in_interrupt()) && printk_ratelimit())
296 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
303 tage->cpu = smp_processor_id();
304 tage->type = tcd->tcd_type;
305 list_add_tail(&tage->linkage, &tcd->tcd_pages);
306 tcd->tcd_cur_pages++;
308 if (tcd->tcd_cur_pages > 8 && thread_running) {
309 struct tracefiled_ctl *tctl = &trace_tctl;
311 * wake up tracefiled to process some pages.
313 wake_up(&tctl->tctl_waitq);
320 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
322 int pgcount = tcd->tcd_cur_pages / 10;
323 struct page_collection pc;
324 struct cfs_trace_page *tage;
325 struct cfs_trace_page *tmp;
328 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
329 * from here: this will lead to infinite recursion.
332 if (printk_ratelimit())
333 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
334 pgcount + 1, tcd->tcd_cur_pages);
336 INIT_LIST_HEAD(&pc.pc_pages);
338 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
342 list_move_tail(&tage->linkage, &pc.pc_pages);
343 tcd->tcd_cur_pages--;
345 put_pages_on_tcd_daemon_list(&pc, tcd);
348 /* return a page that has 'len' bytes left at the end */
349 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
352 struct cfs_trace_page *tage;
355 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
356 * from here: this will lead to infinite recursion.
359 if (len > PAGE_SIZE) {
360 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
365 tage = cfs_trace_get_tage_try(tcd, len);
370 if (tcd->tcd_cur_pages > 0) {
371 tage = cfs_tage_from_list(tcd->tcd_pages.next);
373 cfs_tage_to_tail(tage, &tcd->tcd_pages);
378 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
379 struct libcfs_debug_msg_data *msgdata,
382 struct timespec64 ts;
384 ktime_get_real_ts64(&ts);
386 header->ph_subsys = msgdata->msg_subsys;
387 header->ph_mask = msgdata->msg_mask;
388 header->ph_cpu_id = smp_processor_id();
389 header->ph_type = cfs_trace_buf_idx_get();
390 /* y2038 safe since all user space treats this as unsigned, but
391 * will overflow in 2106
393 header->ph_sec = (u32)ts.tv_sec;
394 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
395 header->ph_stack = stack;
396 header->ph_pid = current->pid;
397 header->ph_line_num = msgdata->msg_line;
398 header->ph_extern_pid = 0;
402 * tty_write_msg - write a message to a certain tty, not just the console.
403 * @tty: the destination tty_struct
404 * @msg: the message to write
406 * tty_write_message is not exported, so write a same function for it
409 static void tty_write_msg(struct tty_struct *tty, const char *msg)
411 mutex_lock(&tty->atomic_write_lock);
413 if (tty->ops->write && tty->count > 0)
414 tty->ops->write(tty, msg, strlen(msg));
416 mutex_unlock(&tty->atomic_write_lock);
417 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
420 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
422 struct tty_struct *tty;
424 tty = get_current_tty();
428 tty_write_msg(tty, prefix);
429 if ((mask & D_EMERG) || (mask & D_ERROR))
430 tty_write_msg(tty, "Error");
431 tty_write_msg(tty, ": ");
432 tty_write_msg(tty, msg);
436 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
437 const char *buf, int len, const char *file,
440 char *prefix = "Lustre";
442 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
445 if (mask & D_CONSOLE) {
447 pr_emerg("%sError: %.*s", prefix, len, buf);
448 else if (mask & D_ERROR)
449 pr_err("%sError: %.*s", prefix, len, buf);
450 else if (mask & D_WARNING)
451 pr_warn("%s: %.*s", prefix, len, buf);
452 else if (mask & libcfs_printk)
453 pr_info("%s: %.*s", prefix, len, buf);
456 pr_emerg("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
457 hdr->ph_pid, hdr->ph_extern_pid, file,
458 hdr->ph_line_num, fn, len, buf);
459 else if (mask & D_ERROR)
460 pr_err("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
461 hdr->ph_pid, hdr->ph_extern_pid, file,
462 hdr->ph_line_num, fn, len, buf);
463 else if (mask & D_WARNING)
464 pr_warn("%s: %d:%d:(%s:%d:%s()) %.*s", prefix,
465 hdr->ph_pid, hdr->ph_extern_pid, file,
466 hdr->ph_line_num, fn, len, buf);
467 else if (mask & (D_CONSOLE | libcfs_printk))
468 pr_info("%s: %.*s", prefix, len, buf);
472 cfs_tty_write_message(prefix, mask, buf);
475 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
476 const char *format, ...)
478 struct cfs_trace_cpu_data *tcd = NULL;
479 struct ptldebug_header header = {0};
480 struct cfs_trace_page *tage;
481 /* string_buf is used only if tcd != NULL, and is always set then */
482 char *string_buf = NULL;
485 int needed = 85; /* seeded with average message length */
489 int mask = msgdata->msg_mask;
490 char *file = (char *)msgdata->msg_file;
491 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
493 if (strchr(file, '/'))
494 file = strrchr(file, '/') + 1;
496 tcd = cfs_trace_get_tcd();
498 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
499 * pins us to a particular CPU. This avoids an smp_processor_id()
500 * warning on Linux when debugging is enabled.
502 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
504 if (!tcd) /* arch may not log in IRQ context */
507 if (tcd->tcd_cur_pages == 0)
508 header.ph_flags |= PH_FLAG_FIRST_RECORD;
510 if (tcd->tcd_shutting_down) {
511 cfs_trace_put_tcd(tcd);
516 known_size = strlen(file) + 1;
518 known_size += strlen(msgdata->msg_fn) + 1;
520 if (libcfs_debug_binary)
521 known_size += sizeof(header);
524 * May perform an additional pass to update 'needed' and increase
525 * tage buffer size to match vsnprintf reported size required
526 * On the second pass (retry=1) use vscnprintf [which returns
527 * number of bytes written not including the terminating nul]
528 * to clarify `needed` is used as number of bytes written
529 * for the remainder of this function
531 for (retry = 0; retry < 2; retry++) {
532 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
534 if (needed + known_size > PAGE_SIZE)
537 cfs_trace_put_tcd(tcd);
542 string_buf = (char *)page_address(tage->page) +
543 tage->used + known_size;
545 max_nob = PAGE_SIZE - tage->used - known_size;
547 pr_emerg("LustreError: negative max_nob: %d\n",
550 cfs_trace_put_tcd(tcd);
555 va_start(ap, format);
557 needed = vscnprintf(string_buf, max_nob, format, ap);
559 needed = vsnprintf(string_buf, max_nob, format, ap);
562 if (needed < max_nob) /* well. printing ok.. */
566 /* `needed` is actual bytes written to string_buf */
567 if (*(string_buf + needed - 1) != '\n') {
568 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
569 file, msgdata->msg_line, msgdata->msg_fn);
570 } else if (mask & D_TTY) {
571 /* TTY needs '\r\n' to move carriage to leftmost position */
572 if (needed < 2 || *(string_buf + needed - 2) != '\r')
573 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
574 file, msgdata->msg_line, msgdata->msg_fn);
577 header.ph_len = known_size + needed;
578 debug_buf = (char *)page_address(tage->page) + tage->used;
580 if (libcfs_debug_binary) {
581 memcpy(debug_buf, &header, sizeof(header));
582 tage->used += sizeof(header);
583 debug_buf += sizeof(header);
586 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
587 tage->used += strlen(file) + 1;
588 debug_buf += strlen(file) + 1;
590 if (msgdata->msg_fn) {
591 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
592 tage->used += strlen(msgdata->msg_fn) + 1;
593 debug_buf += strlen(msgdata->msg_fn) + 1;
596 __LASSERT(debug_buf == string_buf);
598 tage->used += needed;
599 __LASSERT(tage->used <= PAGE_SIZE);
602 if ((mask & libcfs_printk) == 0) {
603 /* no console output requested */
605 cfs_trace_put_tcd(tcd);
610 if (libcfs_console_ratelimit &&
611 cdls->cdls_next != 0 && /* not first time ever */
612 time_before(jiffies, cdls->cdls_next)) {
613 /* skipping a console message */
616 cfs_trace_put_tcd(tcd);
620 if (time_after(jiffies, cdls->cdls_next +
621 libcfs_console_max_delay +
622 cfs_time_seconds(10))) {
623 /* last timeout was a long time ago */
624 cdls->cdls_delay /= libcfs_console_backoff * 4;
626 cdls->cdls_delay *= libcfs_console_backoff;
629 if (cdls->cdls_delay < libcfs_console_min_delay)
630 cdls->cdls_delay = libcfs_console_min_delay;
631 else if (cdls->cdls_delay > libcfs_console_max_delay)
632 cdls->cdls_delay = libcfs_console_max_delay;
634 /* ensure cdls_next is never zero after it's been seen */
635 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
639 cfs_print_to_console(&header, mask, string_buf, needed, file,
641 cfs_trace_put_tcd(tcd);
643 string_buf = cfs_trace_get_console_buffer();
645 va_start(ap, format);
646 needed = vscnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
650 cfs_print_to_console(&header, mask,
651 string_buf, needed, file, msgdata->msg_fn);
656 if (cdls != NULL && cdls->cdls_count != 0) {
657 string_buf = cfs_trace_get_console_buffer();
659 needed = scnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
660 "Skipped %d previous similar message%s\n",
662 (cdls->cdls_count > 1) ? "s" : "");
664 /* Do not allow print this to TTY */
665 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
666 needed, file, msgdata->msg_fn);
669 cdls->cdls_count = 0;
674 EXPORT_SYMBOL(libcfs_debug_msg);
677 cfs_trace_assertion_failed(const char *str,
678 struct libcfs_debug_msg_data *msgdata)
680 struct ptldebug_header hdr;
682 libcfs_panic_in_progress = 1;
683 libcfs_catastrophe = 1;
686 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
688 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
689 msgdata->msg_file, msgdata->msg_fn);
691 panic("Lustre debug assertion failure\n");
697 panic_collect_pages(struct page_collection *pc)
699 /* Do the collect_pages job on a single CPU: assumes that all other
700 * CPUs have been stopped during a panic. If this isn't true for some
701 * arch, this will have to be implemented separately in each arch. */
704 struct cfs_trace_cpu_data *tcd;
706 INIT_LIST_HEAD(&pc->pc_pages);
708 cfs_tcd_for_each(tcd, i, j) {
709 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
710 tcd->tcd_cur_pages = 0;
712 if (pc->pc_want_daemon_pages) {
713 list_splice_init(&tcd->tcd_daemon_pages,
715 tcd->tcd_cur_daemon_pages = 0;
720 static void collect_pages_on_all_cpus(struct page_collection *pc)
722 struct cfs_trace_cpu_data *tcd;
725 for_each_possible_cpu(cpu) {
726 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
727 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
728 tcd->tcd_cur_pages = 0;
729 if (pc->pc_want_daemon_pages) {
730 list_splice_init(&tcd->tcd_daemon_pages,
732 tcd->tcd_cur_daemon_pages = 0;
738 static void collect_pages(struct page_collection *pc)
740 INIT_LIST_HEAD(&pc->pc_pages);
742 if (libcfs_panic_in_progress)
743 panic_collect_pages(pc);
745 collect_pages_on_all_cpus(pc);
748 static void put_pages_back_on_all_cpus(struct page_collection *pc)
750 struct cfs_trace_cpu_data *tcd;
751 struct list_head *cur_head;
752 struct cfs_trace_page *tage;
753 struct cfs_trace_page *tmp;
756 for_each_possible_cpu(cpu) {
757 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
758 cur_head = tcd->tcd_pages.next;
760 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
763 __LASSERT_TAGE_INVARIANT(tage);
765 if (tage->cpu != cpu || tage->type != i)
768 cfs_tage_to_tail(tage, cur_head);
769 tcd->tcd_cur_pages++;
775 static void put_pages_back(struct page_collection *pc)
777 if (!libcfs_panic_in_progress)
778 put_pages_back_on_all_cpus(pc);
781 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
782 * we have a good amount of data at all times for dumping during an LBUG, even
783 * if we have been steadily writing (and otherwise discarding) pages via the
785 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
786 struct cfs_trace_cpu_data *tcd)
788 struct cfs_trace_page *tage;
789 struct cfs_trace_page *tmp;
791 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
792 __LASSERT_TAGE_INVARIANT(tage);
794 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
797 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
798 tcd->tcd_cur_daemon_pages++;
800 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
801 struct cfs_trace_page *victim;
803 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
804 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
806 __LASSERT_TAGE_INVARIANT(victim);
808 list_del(&victim->linkage);
809 cfs_tage_free(victim);
810 tcd->tcd_cur_daemon_pages--;
815 static void put_pages_on_daemon_list(struct page_collection *pc)
817 struct cfs_trace_cpu_data *tcd;
820 for_each_possible_cpu(cpu) {
821 cfs_tcd_for_each_type_lock(tcd, i, cpu)
822 put_pages_on_tcd_daemon_list(pc, tcd);
826 void cfs_trace_debug_print(void)
828 struct page_collection pc;
829 struct cfs_trace_page *tage;
830 struct cfs_trace_page *tmp;
832 pc.pc_want_daemon_pages = 1;
834 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
838 __LASSERT_TAGE_INVARIANT(tage);
841 p = page_address(page);
842 while (p < ((char *)page_address(page) + tage->used)) {
843 struct ptldebug_header *hdr;
848 p += strlen(file) + 1;
851 len = hdr->ph_len - (int)(p - (char *)hdr);
853 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
858 list_del(&tage->linkage);
863 int cfs_tracefile_dump_all_pages(char *filename)
865 struct page_collection pc;
867 struct cfs_trace_page *tage;
868 struct cfs_trace_page *tmp;
872 down_write(&cfs_tracefile_sem);
874 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
878 pr_err("LustreError: can't open %s for dump: rc = %d\n",
883 pc.pc_want_daemon_pages = 1;
885 if (list_empty(&pc.pc_pages)) {
890 /* ok, for now, just write the pages. in the future we'll be building
891 * iobufs with the pages and calling generic_direct_IO */
892 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
894 __LASSERT_TAGE_INVARIANT(tage);
896 buf = kmap(tage->page);
897 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
899 if (rc != (int)tage->used) {
900 pr_warn("Lustre: wanted to write %u but wrote %d\n",
903 __LASSERT(list_empty(&pc.pc_pages));
906 list_del(&tage->linkage);
910 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
912 pr_err("LustreError: sync returns: rc = %d\n", rc);
914 filp_close(filp, NULL);
916 up_write(&cfs_tracefile_sem);
920 void cfs_trace_flush_pages(void)
922 struct page_collection pc;
923 struct cfs_trace_page *tage;
924 struct cfs_trace_page *tmp;
926 pc.pc_want_daemon_pages = 1;
928 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
930 __LASSERT_TAGE_INVARIANT(tage);
932 list_del(&tage->linkage);
937 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
938 const char __user *usr_buffer, int usr_buffer_nob)
942 if (usr_buffer_nob > knl_buffer_nob)
945 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
948 nob = strnlen(knl_buffer, usr_buffer_nob);
949 while (--nob >= 0) /* strip trailing whitespace */
950 if (!isspace(knl_buffer[nob]))
953 if (nob < 0) /* empty string */
956 if (nob == knl_buffer_nob) /* no space to terminate */
959 knl_buffer[nob + 1] = 0; /* terminate */
962 EXPORT_SYMBOL(cfs_trace_copyin_string);
964 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
965 const char *knl_buffer, char *append)
967 /* NB if 'append' != NULL, it's a single character to append to the
968 * copied out string - usually "\n", for /proc entries and "" (i.e. a
969 * terminating zero byte) for sysctl entries */
970 int nob = strlen(knl_buffer);
972 if (nob > usr_buffer_nob)
973 nob = usr_buffer_nob;
975 if (copy_to_user(usr_buffer, knl_buffer, nob))
978 if (append != NULL && nob < usr_buffer_nob) {
979 if (copy_to_user(usr_buffer + nob, append, 1))
987 EXPORT_SYMBOL(cfs_trace_copyout_string);
989 int cfs_trace_allocate_string_buffer(char **str, int nob)
991 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
994 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
1001 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
1006 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1010 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1011 usr_str, usr_str_nob);
1015 if (str[0] != '/') {
1019 rc = cfs_tracefile_dump_all_pages(str);
1025 int cfs_trace_daemon_command(char *str)
1029 down_write(&cfs_tracefile_sem);
1031 if (strcmp(str, "stop") == 0) {
1032 up_write(&cfs_tracefile_sem);
1033 cfs_trace_stop_thread();
1034 down_write(&cfs_tracefile_sem);
1035 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
1037 } else if (strncmp(str, "size=", 5) == 0) {
1040 rc = kstrtoul(str + 5, 10, &tmp);
1042 if (tmp < 10 || tmp > 20480)
1043 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
1045 cfs_tracefile_size = tmp << 20;
1047 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
1049 } else if (str[0] != '/') {
1052 strcpy(cfs_tracefile, str);
1054 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
1055 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
1057 cfs_trace_start_thread();
1060 up_write(&cfs_tracefile_sem);
1064 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
1069 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1073 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1074 usr_str, usr_str_nob);
1076 rc = cfs_trace_daemon_command(str);
1082 int cfs_trace_set_debug_mb(int mb)
1086 unsigned long pages;
1087 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
1088 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
1089 struct cfs_trace_cpu_data *tcd;
1091 if (mb < num_possible_cpus()) {
1092 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
1093 mb, num_possible_cpus());
1094 mb = num_possible_cpus();
1098 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
1103 mb /= num_possible_cpus();
1104 pages = mb << (20 - PAGE_SHIFT);
1106 down_write(&cfs_tracefile_sem);
1108 cfs_tcd_for_each(tcd, i, j)
1109 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1111 up_write(&cfs_tracefile_sem);
1116 int cfs_trace_get_debug_mb(void)
1120 struct cfs_trace_cpu_data *tcd;
1121 int total_pages = 0;
1123 down_read(&cfs_tracefile_sem);
1125 cfs_tcd_for_each(tcd, i, j)
1126 total_pages += tcd->tcd_max_pages;
1128 up_read(&cfs_tracefile_sem);
1130 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1133 static int tracefiled(void *arg)
1135 struct page_collection pc;
1136 struct tracefiled_ctl *tctl = arg;
1137 struct cfs_trace_page *tage;
1138 struct cfs_trace_page *tmp;
1144 /* we're started late enough that we pick up init's fs context */
1145 /* this is so broken in uml? what on earth is going on? */
1147 complete(&tctl->tctl_start);
1150 wait_queue_entry_t __wait;
1152 pc.pc_want_daemon_pages = 0;
1154 if (list_empty(&pc.pc_pages))
1158 down_read(&cfs_tracefile_sem);
1159 if (cfs_tracefile[0] != 0) {
1160 filp = filp_open(cfs_tracefile,
1161 O_CREAT | O_RDWR | O_LARGEFILE,
1166 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1170 up_read(&cfs_tracefile_sem);
1172 put_pages_on_daemon_list(&pc);
1173 __LASSERT(list_empty(&pc.pc_pages));
1177 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1178 struct dentry *de = file_dentry(filp);
1179 static loff_t f_pos;
1181 __LASSERT_TAGE_INVARIANT(tage);
1183 if (f_pos >= (off_t)cfs_tracefile_size)
1185 else if (f_pos > i_size_read(de->d_inode))
1186 f_pos = i_size_read(de->d_inode);
1188 buf = kmap(tage->page);
1189 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1191 if (rc != (int)tage->used) {
1192 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1194 put_pages_back(&pc);
1195 __LASSERT(list_empty(&pc.pc_pages));
1200 filp_close(filp, NULL);
1201 put_pages_on_daemon_list(&pc);
1202 if (!list_empty(&pc.pc_pages)) {
1205 pr_alert("Lustre: trace pages aren't empty\n");
1206 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1207 for (i = 0; i < num_possible_cpus(); i++)
1209 pr_cont("%d(on) ", i);
1211 pr_cont("%d(off) ", i);
1215 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1217 pr_err("Lustre: page %d belongs to cpu %d\n",
1219 pr_err("Lustre: There are %d pages unwritten\n", i);
1221 __LASSERT(list_empty(&pc.pc_pages));
1223 if (atomic_read(&tctl->tctl_shutdown)) {
1224 if (last_loop == 0) {
1231 init_waitqueue_entry(&__wait, current);
1232 add_wait_queue(&tctl->tctl_waitq, &__wait);
1233 schedule_timeout_interruptible(cfs_time_seconds(1));
1234 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1236 complete(&tctl->tctl_stop);
1240 int cfs_trace_start_thread(void)
1242 struct tracefiled_ctl *tctl = &trace_tctl;
1245 mutex_lock(&cfs_trace_thread_mutex);
1249 init_completion(&tctl->tctl_start);
1250 init_completion(&tctl->tctl_stop);
1251 init_waitqueue_head(&tctl->tctl_waitq);
1252 atomic_set(&tctl->tctl_shutdown, 0);
1254 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1259 wait_for_completion(&tctl->tctl_start);
1262 mutex_unlock(&cfs_trace_thread_mutex);
1266 void cfs_trace_stop_thread(void)
1268 struct tracefiled_ctl *tctl = &trace_tctl;
1270 mutex_lock(&cfs_trace_thread_mutex);
1271 if (thread_running) {
1272 pr_info("Lustre: shutting down debug daemon thread...\n");
1273 atomic_set(&tctl->tctl_shutdown, 1);
1274 wait_for_completion(&tctl->tctl_stop);
1277 mutex_unlock(&cfs_trace_thread_mutex);
1280 int cfs_tracefile_init(int max_pages)
1282 struct cfs_trace_cpu_data *tcd;
1288 rc = cfs_tracefile_init_arch();
1292 cfs_tcd_for_each(tcd, i, j) {
1293 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1294 factor = tcd->tcd_pages_factor;
1295 INIT_LIST_HEAD(&tcd->tcd_pages);
1296 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1297 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1298 tcd->tcd_cur_pages = 0;
1299 tcd->tcd_cur_stock_pages = 0;
1300 tcd->tcd_cur_daemon_pages = 0;
1301 tcd->tcd_max_pages = (max_pages * factor) / 100;
1302 LASSERT(tcd->tcd_max_pages > 0);
1303 tcd->tcd_shutting_down = 0;
1308 static void trace_cleanup_on_all_cpus(void)
1310 struct cfs_trace_cpu_data *tcd;
1311 struct cfs_trace_page *tage;
1312 struct cfs_trace_page *tmp;
1315 for_each_possible_cpu(cpu) {
1316 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1317 tcd->tcd_shutting_down = 1;
1319 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1320 __LASSERT_TAGE_INVARIANT(tage);
1322 list_del(&tage->linkage);
1323 cfs_tage_free(tage);
1325 tcd->tcd_cur_pages = 0;
1330 static void cfs_trace_cleanup(void)
1332 struct page_collection pc;
1334 INIT_LIST_HEAD(&pc.pc_pages);
1336 trace_cleanup_on_all_cpus();
1338 cfs_tracefile_fini_arch();
1341 void cfs_tracefile_exit(void)
1343 cfs_trace_stop_thread();
1344 cfs_trace_cleanup();