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 /* XXX move things up to the top, comment */
54 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
56 char *cfs_trace_console_buffers[NR_CPUS][CFS_TCD_TYPE_MAX];
57 char cfs_tracefile[TRACEFILE_NAME_SIZE];
58 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
59 static struct tracefiled_ctl trace_tctl;
60 static DEFINE_MUTEX(cfs_trace_thread_mutex);
61 static int thread_running = 0;
63 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
64 static DECLARE_RWSEM(cfs_tracefile_sem);
66 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
67 struct cfs_trace_cpu_data *tcd);
69 /* trace file lock routines */
70 /* The walking argument indicates the locking comes from all tcd types
71 * iterator and we must lock it and dissable local irqs to avoid deadlocks
72 * with other interrupt locks that might be happening. See LU-1311
75 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
76 __acquires(&tcd->tcd_lock)
78 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
79 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
80 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
81 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
82 spin_lock_bh(&tcd->tcd_lock);
83 else if (unlikely(walking))
84 spin_lock_irq(&tcd->tcd_lock);
86 spin_lock(&tcd->tcd_lock);
90 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
91 __releases(&tcd->tcd_lock)
93 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
94 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
95 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
96 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
97 spin_unlock_bh(&tcd->tcd_lock);
98 else if (unlikely(walking))
99 spin_unlock_irq(&tcd->tcd_lock);
101 spin_unlock(&tcd->tcd_lock);
104 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
105 for (i = 0; cfs_trace_data[i] && \
106 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
107 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
109 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
112 return CFS_TCD_TYPE_IRQ;
114 return CFS_TCD_TYPE_SOFTIRQ;
115 return CFS_TCD_TYPE_PROC;
118 static inline struct cfs_trace_cpu_data *
119 cfs_trace_get_tcd(void)
121 struct cfs_trace_cpu_data *tcd =
122 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
124 cfs_trace_lock_tcd(tcd, 0);
129 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
131 cfs_trace_unlock_tcd(tcd, 0);
136 /* percents to share the total debug memory for each type */
137 static unsigned int pages_factor[CFS_TCD_TYPE_MAX] = {
138 80, /* 80% pages for CFS_TCD_TYPE_PROC */
139 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
140 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
143 int cfs_tracefile_init_arch(void)
145 struct cfs_trace_cpu_data *tcd;
149 /* initialize trace_data */
150 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
151 for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
153 kmalloc_array(num_possible_cpus(),
154 sizeof(union cfs_trace_data_union),
156 if (!cfs_trace_data[i])
160 /* arch related info initialized */
161 cfs_tcd_for_each(tcd, i, j) {
162 spin_lock_init(&tcd->tcd_lock);
163 tcd->tcd_pages_factor = pages_factor[i];
168 for (i = 0; i < num_possible_cpus(); i++)
169 for (j = 0; j < 3; j++) {
170 cfs_trace_console_buffers[i][j] =
171 kmalloc(CFS_TRACE_CONSOLE_BUFFER_SIZE,
174 if (!cfs_trace_console_buffers[i][j])
181 for (i = 0; i < num_possible_cpus(); i++)
182 for (j = 0; j < 3; j++) {
183 kfree(cfs_trace_console_buffers[i][j]);
184 cfs_trace_console_buffers[i][j] = NULL;
187 for (i = 0; cfs_trace_data[i]; i++) {
188 kfree(cfs_trace_data[i]);
189 cfs_trace_data[i] = NULL;
191 pr_err("lnet: Not enough memory\n");
195 void cfs_tracefile_fini_arch(void)
200 for (i = 0; i < num_possible_cpus(); i++)
201 for (j = 0; j < 3; j++) {
202 kfree(cfs_trace_console_buffers[i][j]);
203 cfs_trace_console_buffers[i][j] = NULL;
206 for (i = 0; cfs_trace_data[i]; i++) {
207 kfree(cfs_trace_data[i]);
208 cfs_trace_data[i] = NULL;
212 static inline struct cfs_trace_page *
213 cfs_tage_from_list(struct list_head *list)
215 return list_entry(list, struct cfs_trace_page, linkage);
218 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
221 struct cfs_trace_page *tage;
223 /* My caller is trying to free memory */
224 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
228 * Don't spam console with allocation failures: they will be reported
229 * by upper layer anyway.
232 page = alloc_page(gfp);
236 tage = kmalloc(sizeof(*tage), gfp);
243 atomic_inc(&cfs_tage_allocated);
247 static void cfs_tage_free(struct cfs_trace_page *tage)
249 __LASSERT(tage != NULL);
250 __LASSERT(tage->page != NULL);
252 __free_page(tage->page);
254 atomic_dec(&cfs_tage_allocated);
257 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
258 struct list_head *queue)
260 __LASSERT(tage != NULL);
261 __LASSERT(queue != NULL);
263 list_move_tail(&tage->linkage, queue);
266 /* return a page that has 'len' bytes left at the end */
267 static struct cfs_trace_page *
268 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
270 struct cfs_trace_page *tage;
272 if (tcd->tcd_cur_pages > 0) {
273 __LASSERT(!list_empty(&tcd->tcd_pages));
274 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
275 if (tage->used + len <= PAGE_SIZE)
279 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
280 if (tcd->tcd_cur_stock_pages > 0) {
281 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
282 --tcd->tcd_cur_stock_pages;
283 list_del_init(&tage->linkage);
285 tage = cfs_tage_alloc(GFP_ATOMIC);
286 if (unlikely(tage == NULL)) {
287 if ((!(current->flags & PF_MEMALLOC) ||
288 in_interrupt()) && printk_ratelimit())
289 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
296 tage->cpu = smp_processor_id();
297 tage->type = tcd->tcd_type;
298 list_add_tail(&tage->linkage, &tcd->tcd_pages);
299 tcd->tcd_cur_pages++;
301 if (tcd->tcd_cur_pages > 8 && thread_running) {
302 struct tracefiled_ctl *tctl = &trace_tctl;
304 * wake up tracefiled to process some pages.
306 wake_up(&tctl->tctl_waitq);
313 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
315 int pgcount = tcd->tcd_cur_pages / 10;
316 struct page_collection pc;
317 struct cfs_trace_page *tage;
318 struct cfs_trace_page *tmp;
321 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
322 * from here: this will lead to infinite recursion.
325 if (printk_ratelimit())
326 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
327 pgcount + 1, tcd->tcd_cur_pages);
329 INIT_LIST_HEAD(&pc.pc_pages);
331 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
335 list_move_tail(&tage->linkage, &pc.pc_pages);
336 tcd->tcd_cur_pages--;
338 put_pages_on_tcd_daemon_list(&pc, tcd);
341 /* return a page that has 'len' bytes left at the end */
342 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
345 struct cfs_trace_page *tage;
348 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
349 * from here: this will lead to infinite recursion.
352 if (len > PAGE_SIZE) {
353 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
358 tage = cfs_trace_get_tage_try(tcd, len);
363 if (tcd->tcd_cur_pages > 0) {
364 tage = cfs_tage_from_list(tcd->tcd_pages.next);
366 cfs_tage_to_tail(tage, &tcd->tcd_pages);
371 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
372 struct libcfs_debug_msg_data *msgdata,
375 struct timespec64 ts;
377 ktime_get_real_ts64(&ts);
379 header->ph_subsys = msgdata->msg_subsys;
380 header->ph_mask = msgdata->msg_mask;
381 header->ph_cpu_id = smp_processor_id();
382 header->ph_type = cfs_trace_buf_idx_get();
383 /* y2038 safe since all user space treats this as unsigned, but
384 * will overflow in 2106
386 header->ph_sec = (u32)ts.tv_sec;
387 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
388 header->ph_stack = stack;
389 header->ph_pid = current->pid;
390 header->ph_line_num = msgdata->msg_line;
391 header->ph_extern_pid = 0;
395 * tty_write_msg - write a message to a certain tty, not just the console.
396 * @tty: the destination tty_struct
397 * @msg: the message to write
399 * tty_write_message is not exported, so write a same function for it
402 static void tty_write_msg(struct tty_struct *tty, const char *msg)
404 mutex_lock(&tty->atomic_write_lock);
406 if (tty->ops->write && tty->count > 0)
407 tty->ops->write(tty, msg, strlen(msg));
409 mutex_unlock(&tty->atomic_write_lock);
410 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
413 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
415 struct tty_struct *tty;
417 tty = get_current_tty();
421 tty_write_msg(tty, prefix);
422 if ((mask & D_EMERG) || (mask & D_ERROR))
423 tty_write_msg(tty, "Error");
424 tty_write_msg(tty, ": ");
425 tty_write_msg(tty, msg);
429 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
430 const char *buf, int len, const char *file,
433 char *prefix = "Lustre";
435 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
438 if (mask & D_CONSOLE) {
440 pr_emerg("%sError: %.*s", prefix, len, buf);
441 else if (mask & D_ERROR)
442 pr_err("%sError: %.*s", prefix, len, buf);
443 else if (mask & D_WARNING)
444 pr_warn("%s: %.*s", prefix, len, buf);
445 else if (mask & libcfs_printk)
446 pr_info("%s: %.*s", prefix, len, buf);
449 pr_emerg("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
450 hdr->ph_pid, hdr->ph_extern_pid, file,
451 hdr->ph_line_num, fn, len, buf);
452 else if (mask & D_ERROR)
453 pr_err("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
454 hdr->ph_pid, hdr->ph_extern_pid, file,
455 hdr->ph_line_num, fn, len, buf);
456 else if (mask & D_WARNING)
457 pr_warn("%s: %d:%d:(%s:%d:%s()) %.*s", prefix,
458 hdr->ph_pid, hdr->ph_extern_pid, file,
459 hdr->ph_line_num, fn, len, buf);
460 else if (mask & (D_CONSOLE | libcfs_printk))
461 pr_info("%s: %.*s", prefix, len, buf);
465 cfs_tty_write_message(prefix, mask, buf);
468 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
469 const char *format, ...)
471 struct cfs_trace_cpu_data *tcd = NULL;
472 struct ptldebug_header header = {0};
473 struct cfs_trace_page *tage;
474 /* string_buf is used only if tcd != NULL, and is always set then */
475 char *string_buf = NULL;
478 int needed = 85; /* seeded with average message length */
482 int mask = msgdata->msg_mask;
483 char *file = (char *)msgdata->msg_file;
484 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
486 if (strchr(file, '/'))
487 file = strrchr(file, '/') + 1;
489 tcd = cfs_trace_get_tcd();
491 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
492 * pins us to a particular CPU. This avoids an smp_processor_id()
493 * warning on Linux when debugging is enabled.
495 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
497 if (!tcd) /* arch may not log in IRQ context */
500 if (tcd->tcd_cur_pages == 0)
501 header.ph_flags |= PH_FLAG_FIRST_RECORD;
503 if (tcd->tcd_shutting_down) {
504 cfs_trace_put_tcd(tcd);
509 known_size = strlen(file) + 1;
511 known_size += strlen(msgdata->msg_fn) + 1;
513 if (libcfs_debug_binary)
514 known_size += sizeof(header);
517 * May perform an additional pass to update 'needed' and increase
518 * tage buffer size to match vsnprintf reported size required
519 * On the second pass (retry=1) use vscnprintf [which returns
520 * number of bytes written not including the terminating nul]
521 * to clarify `needed` is used as number of bytes written
522 * for the remainder of this function
524 for (retry = 0; retry < 2; retry++) {
525 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
527 if (needed + known_size > PAGE_SIZE)
530 cfs_trace_put_tcd(tcd);
535 string_buf = (char *)page_address(tage->page) +
536 tage->used + known_size;
538 max_nob = PAGE_SIZE - tage->used - known_size;
540 pr_emerg("LustreError: negative max_nob: %d\n",
543 cfs_trace_put_tcd(tcd);
548 va_start(ap, format);
550 needed = vscnprintf(string_buf, max_nob, format, ap);
552 needed = vsnprintf(string_buf, max_nob, format, ap);
555 if (needed < max_nob) /* well. printing ok.. */
559 /* `needed` is actual bytes written to string_buf */
560 if (*(string_buf + needed - 1) != '\n') {
561 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
562 file, msgdata->msg_line, msgdata->msg_fn);
563 } else if (mask & D_TTY) {
564 /* TTY needs '\r\n' to move carriage to leftmost position */
565 if (needed < 2 || *(string_buf + needed - 2) != '\r')
566 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
567 file, msgdata->msg_line, msgdata->msg_fn);
570 header.ph_len = known_size + needed;
571 debug_buf = (char *)page_address(tage->page) + tage->used;
573 if (libcfs_debug_binary) {
574 memcpy(debug_buf, &header, sizeof(header));
575 tage->used += sizeof(header);
576 debug_buf += sizeof(header);
579 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
580 tage->used += strlen(file) + 1;
581 debug_buf += strlen(file) + 1;
583 if (msgdata->msg_fn) {
584 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
585 tage->used += strlen(msgdata->msg_fn) + 1;
586 debug_buf += strlen(msgdata->msg_fn) + 1;
589 __LASSERT(debug_buf == string_buf);
591 tage->used += needed;
592 __LASSERT(tage->used <= PAGE_SIZE);
595 if ((mask & libcfs_printk) == 0) {
596 /* no console output requested */
598 cfs_trace_put_tcd(tcd);
603 if (libcfs_console_ratelimit &&
604 cdls->cdls_next != 0 && /* not first time ever */
605 time_before(jiffies, cdls->cdls_next)) {
606 /* skipping a console message */
609 cfs_trace_put_tcd(tcd);
613 if (time_after(jiffies, cdls->cdls_next +
614 libcfs_console_max_delay +
615 cfs_time_seconds(10))) {
616 /* last timeout was a long time ago */
617 cdls->cdls_delay /= libcfs_console_backoff * 4;
619 cdls->cdls_delay *= libcfs_console_backoff;
622 if (cdls->cdls_delay < libcfs_console_min_delay)
623 cdls->cdls_delay = libcfs_console_min_delay;
624 else if (cdls->cdls_delay > libcfs_console_max_delay)
625 cdls->cdls_delay = libcfs_console_max_delay;
627 /* ensure cdls_next is never zero after it's been seen */
628 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
632 cfs_print_to_console(&header, mask, string_buf, needed, file,
634 cfs_trace_put_tcd(tcd);
636 string_buf = cfs_trace_get_console_buffer();
638 va_start(ap, format);
639 needed = vscnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
643 cfs_print_to_console(&header, mask,
644 string_buf, needed, file, msgdata->msg_fn);
649 if (cdls != NULL && cdls->cdls_count != 0) {
650 string_buf = cfs_trace_get_console_buffer();
652 needed = scnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
653 "Skipped %d previous similar message%s\n",
655 (cdls->cdls_count > 1) ? "s" : "");
657 /* Do not allow print this to TTY */
658 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
659 needed, file, msgdata->msg_fn);
662 cdls->cdls_count = 0;
667 EXPORT_SYMBOL(libcfs_debug_msg);
670 cfs_trace_assertion_failed(const char *str,
671 struct libcfs_debug_msg_data *msgdata)
673 struct ptldebug_header hdr;
675 libcfs_panic_in_progress = 1;
676 libcfs_catastrophe = 1;
679 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
681 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
682 msgdata->msg_file, msgdata->msg_fn);
684 panic("Lustre debug assertion failure\n");
690 panic_collect_pages(struct page_collection *pc)
692 /* Do the collect_pages job on a single CPU: assumes that all other
693 * CPUs have been stopped during a panic. If this isn't true for some
694 * arch, this will have to be implemented separately in each arch. */
697 struct cfs_trace_cpu_data *tcd;
699 INIT_LIST_HEAD(&pc->pc_pages);
701 cfs_tcd_for_each(tcd, i, j) {
702 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
703 tcd->tcd_cur_pages = 0;
705 if (pc->pc_want_daemon_pages) {
706 list_splice_init(&tcd->tcd_daemon_pages,
708 tcd->tcd_cur_daemon_pages = 0;
713 static void collect_pages_on_all_cpus(struct page_collection *pc)
715 struct cfs_trace_cpu_data *tcd;
718 for_each_possible_cpu(cpu) {
719 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
720 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
721 tcd->tcd_cur_pages = 0;
722 if (pc->pc_want_daemon_pages) {
723 list_splice_init(&tcd->tcd_daemon_pages,
725 tcd->tcd_cur_daemon_pages = 0;
731 static void collect_pages(struct page_collection *pc)
733 INIT_LIST_HEAD(&pc->pc_pages);
735 if (libcfs_panic_in_progress)
736 panic_collect_pages(pc);
738 collect_pages_on_all_cpus(pc);
741 static void put_pages_back_on_all_cpus(struct page_collection *pc)
743 struct cfs_trace_cpu_data *tcd;
744 struct list_head *cur_head;
745 struct cfs_trace_page *tage;
746 struct cfs_trace_page *tmp;
749 for_each_possible_cpu(cpu) {
750 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
751 cur_head = tcd->tcd_pages.next;
753 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
756 __LASSERT_TAGE_INVARIANT(tage);
758 if (tage->cpu != cpu || tage->type != i)
761 cfs_tage_to_tail(tage, cur_head);
762 tcd->tcd_cur_pages++;
768 static void put_pages_back(struct page_collection *pc)
770 if (!libcfs_panic_in_progress)
771 put_pages_back_on_all_cpus(pc);
774 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
775 * we have a good amount of data at all times for dumping during an LBUG, even
776 * if we have been steadily writing (and otherwise discarding) pages via the
778 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
779 struct cfs_trace_cpu_data *tcd)
781 struct cfs_trace_page *tage;
782 struct cfs_trace_page *tmp;
784 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
785 __LASSERT_TAGE_INVARIANT(tage);
787 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
790 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
791 tcd->tcd_cur_daemon_pages++;
793 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
794 struct cfs_trace_page *victim;
796 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
797 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
799 __LASSERT_TAGE_INVARIANT(victim);
801 list_del(&victim->linkage);
802 cfs_tage_free(victim);
803 tcd->tcd_cur_daemon_pages--;
808 static void put_pages_on_daemon_list(struct page_collection *pc)
810 struct cfs_trace_cpu_data *tcd;
813 for_each_possible_cpu(cpu) {
814 cfs_tcd_for_each_type_lock(tcd, i, cpu)
815 put_pages_on_tcd_daemon_list(pc, tcd);
819 void cfs_trace_debug_print(void)
821 struct page_collection pc;
822 struct cfs_trace_page *tage;
823 struct cfs_trace_page *tmp;
825 pc.pc_want_daemon_pages = 1;
827 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
831 __LASSERT_TAGE_INVARIANT(tage);
834 p = page_address(page);
835 while (p < ((char *)page_address(page) + tage->used)) {
836 struct ptldebug_header *hdr;
841 p += strlen(file) + 1;
844 len = hdr->ph_len - (int)(p - (char *)hdr);
846 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
851 list_del(&tage->linkage);
856 int cfs_tracefile_dump_all_pages(char *filename)
858 struct page_collection pc;
860 struct cfs_trace_page *tage;
861 struct cfs_trace_page *tmp;
865 down_write(&cfs_tracefile_sem);
867 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
871 pr_err("LustreError: can't open %s for dump: rc = %d\n",
876 pc.pc_want_daemon_pages = 1;
878 if (list_empty(&pc.pc_pages)) {
883 /* ok, for now, just write the pages. in the future we'll be building
884 * iobufs with the pages and calling generic_direct_IO */
885 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
887 __LASSERT_TAGE_INVARIANT(tage);
889 buf = kmap(tage->page);
890 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
892 if (rc != (int)tage->used) {
893 pr_warn("Lustre: wanted to write %u but wrote %d\n",
896 __LASSERT(list_empty(&pc.pc_pages));
899 list_del(&tage->linkage);
903 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
905 pr_err("LustreError: sync returns: rc = %d\n", rc);
907 filp_close(filp, NULL);
909 up_write(&cfs_tracefile_sem);
913 void cfs_trace_flush_pages(void)
915 struct page_collection pc;
916 struct cfs_trace_page *tage;
917 struct cfs_trace_page *tmp;
919 pc.pc_want_daemon_pages = 1;
921 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
923 __LASSERT_TAGE_INVARIANT(tage);
925 list_del(&tage->linkage);
930 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
931 const char __user *usr_buffer, int usr_buffer_nob)
935 if (usr_buffer_nob > knl_buffer_nob)
938 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
941 nob = strnlen(knl_buffer, usr_buffer_nob);
942 while (--nob >= 0) /* strip trailing whitespace */
943 if (!isspace(knl_buffer[nob]))
946 if (nob < 0) /* empty string */
949 if (nob == knl_buffer_nob) /* no space to terminate */
952 knl_buffer[nob + 1] = 0; /* terminate */
955 EXPORT_SYMBOL(cfs_trace_copyin_string);
957 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
958 const char *knl_buffer, char *append)
960 /* NB if 'append' != NULL, it's a single character to append to the
961 * copied out string - usually "\n", for /proc entries and "" (i.e. a
962 * terminating zero byte) for sysctl entries */
963 int nob = strlen(knl_buffer);
965 if (nob > usr_buffer_nob)
966 nob = usr_buffer_nob;
968 if (copy_to_user(usr_buffer, knl_buffer, nob))
971 if (append != NULL && nob < usr_buffer_nob) {
972 if (copy_to_user(usr_buffer + nob, append, 1))
980 EXPORT_SYMBOL(cfs_trace_copyout_string);
982 int cfs_trace_allocate_string_buffer(char **str, int nob)
984 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
987 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
994 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
999 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1003 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1004 usr_str, usr_str_nob);
1008 if (str[0] != '/') {
1012 rc = cfs_tracefile_dump_all_pages(str);
1018 int cfs_trace_daemon_command(char *str)
1022 down_write(&cfs_tracefile_sem);
1024 if (strcmp(str, "stop") == 0) {
1025 up_write(&cfs_tracefile_sem);
1026 cfs_trace_stop_thread();
1027 down_write(&cfs_tracefile_sem);
1028 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
1030 } else if (strncmp(str, "size=", 5) == 0) {
1033 rc = kstrtoul(str + 5, 10, &tmp);
1035 if (tmp < 10 || tmp > 20480)
1036 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
1038 cfs_tracefile_size = tmp << 20;
1040 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
1042 } else if (str[0] != '/') {
1045 strcpy(cfs_tracefile, str);
1047 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
1048 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
1050 cfs_trace_start_thread();
1053 up_write(&cfs_tracefile_sem);
1057 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
1062 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1066 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1067 usr_str, usr_str_nob);
1069 rc = cfs_trace_daemon_command(str);
1075 int cfs_trace_set_debug_mb(int mb)
1079 unsigned long pages;
1080 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
1081 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
1082 struct cfs_trace_cpu_data *tcd;
1084 if (mb < num_possible_cpus()) {
1085 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
1086 mb, num_possible_cpus());
1087 mb = num_possible_cpus();
1091 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
1096 mb /= num_possible_cpus();
1097 pages = mb << (20 - PAGE_SHIFT);
1099 down_write(&cfs_tracefile_sem);
1101 cfs_tcd_for_each(tcd, i, j)
1102 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1104 up_write(&cfs_tracefile_sem);
1109 int cfs_trace_get_debug_mb(void)
1113 struct cfs_trace_cpu_data *tcd;
1114 int total_pages = 0;
1116 down_read(&cfs_tracefile_sem);
1118 cfs_tcd_for_each(tcd, i, j)
1119 total_pages += tcd->tcd_max_pages;
1121 up_read(&cfs_tracefile_sem);
1123 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1126 static int tracefiled(void *arg)
1128 struct page_collection pc;
1129 struct tracefiled_ctl *tctl = arg;
1130 struct cfs_trace_page *tage;
1131 struct cfs_trace_page *tmp;
1137 /* we're started late enough that we pick up init's fs context */
1138 /* this is so broken in uml? what on earth is going on? */
1140 complete(&tctl->tctl_start);
1143 wait_queue_entry_t __wait;
1145 pc.pc_want_daemon_pages = 0;
1147 if (list_empty(&pc.pc_pages))
1151 down_read(&cfs_tracefile_sem);
1152 if (cfs_tracefile[0] != 0) {
1153 filp = filp_open(cfs_tracefile,
1154 O_CREAT | O_RDWR | O_LARGEFILE,
1159 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1163 up_read(&cfs_tracefile_sem);
1165 put_pages_on_daemon_list(&pc);
1166 __LASSERT(list_empty(&pc.pc_pages));
1170 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1171 struct dentry *de = file_dentry(filp);
1172 static loff_t f_pos;
1174 __LASSERT_TAGE_INVARIANT(tage);
1176 if (f_pos >= (off_t)cfs_tracefile_size)
1178 else if (f_pos > i_size_read(de->d_inode))
1179 f_pos = i_size_read(de->d_inode);
1181 buf = kmap(tage->page);
1182 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1184 if (rc != (int)tage->used) {
1185 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1187 put_pages_back(&pc);
1188 __LASSERT(list_empty(&pc.pc_pages));
1193 filp_close(filp, NULL);
1194 put_pages_on_daemon_list(&pc);
1195 if (!list_empty(&pc.pc_pages)) {
1198 pr_alert("Lustre: trace pages aren't empty\n");
1199 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1200 for (i = 0; i < num_possible_cpus(); i++)
1202 pr_cont("%d(on) ", i);
1204 pr_cont("%d(off) ", i);
1208 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1210 pr_err("Lustre: page %d belongs to cpu %d\n",
1212 pr_err("Lustre: There are %d pages unwritten\n", i);
1214 __LASSERT(list_empty(&pc.pc_pages));
1216 if (atomic_read(&tctl->tctl_shutdown)) {
1217 if (last_loop == 0) {
1224 init_waitqueue_entry(&__wait, current);
1225 add_wait_queue(&tctl->tctl_waitq, &__wait);
1226 schedule_timeout_interruptible(cfs_time_seconds(1));
1227 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1229 complete(&tctl->tctl_stop);
1233 int cfs_trace_start_thread(void)
1235 struct tracefiled_ctl *tctl = &trace_tctl;
1238 mutex_lock(&cfs_trace_thread_mutex);
1242 init_completion(&tctl->tctl_start);
1243 init_completion(&tctl->tctl_stop);
1244 init_waitqueue_head(&tctl->tctl_waitq);
1245 atomic_set(&tctl->tctl_shutdown, 0);
1247 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1252 wait_for_completion(&tctl->tctl_start);
1255 mutex_unlock(&cfs_trace_thread_mutex);
1259 void cfs_trace_stop_thread(void)
1261 struct tracefiled_ctl *tctl = &trace_tctl;
1263 mutex_lock(&cfs_trace_thread_mutex);
1264 if (thread_running) {
1265 pr_info("Lustre: shutting down debug daemon thread...\n");
1266 atomic_set(&tctl->tctl_shutdown, 1);
1267 wait_for_completion(&tctl->tctl_stop);
1270 mutex_unlock(&cfs_trace_thread_mutex);
1273 int cfs_tracefile_init(int max_pages)
1275 struct cfs_trace_cpu_data *tcd;
1281 rc = cfs_tracefile_init_arch();
1285 cfs_tcd_for_each(tcd, i, j) {
1286 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1287 factor = tcd->tcd_pages_factor;
1288 INIT_LIST_HEAD(&tcd->tcd_pages);
1289 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1290 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1291 tcd->tcd_cur_pages = 0;
1292 tcd->tcd_cur_stock_pages = 0;
1293 tcd->tcd_cur_daemon_pages = 0;
1294 tcd->tcd_max_pages = (max_pages * factor) / 100;
1295 LASSERT(tcd->tcd_max_pages > 0);
1296 tcd->tcd_shutting_down = 0;
1301 static void trace_cleanup_on_all_cpus(void)
1303 struct cfs_trace_cpu_data *tcd;
1304 struct cfs_trace_page *tage;
1305 struct cfs_trace_page *tmp;
1308 for_each_possible_cpu(cpu) {
1309 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1310 tcd->tcd_shutting_down = 1;
1312 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1313 __LASSERT_TAGE_INVARIANT(tage);
1315 list_del(&tage->linkage);
1316 cfs_tage_free(tage);
1318 tcd->tcd_cur_pages = 0;
1323 static void cfs_trace_cleanup(void)
1325 struct page_collection pc;
1327 INIT_LIST_HEAD(&pc.pc_pages);
1329 trace_cleanup_on_all_cpus();
1331 cfs_tracefile_fini_arch();
1334 void cfs_tracefile_exit(void)
1336 cfs_trace_stop_thread();
1337 cfs_trace_cleanup();