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/
31 * libcfs/libcfs/tracefile.c
33 * Author: Zach Brown <zab@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include "tracefile.h"
40 #include <linux/ctype.h>
42 #include <linux/kthread.h>
43 #include <linux/pagemap.h>
44 #include <linux/poll.h>
45 #include <linux/tty.h>
46 #include <linux/uaccess.h>
47 #include <libcfs/linux/linux-fs.h>
48 #include <libcfs/libcfs.h>
51 enum cfs_trace_buf_type {
52 CFS_TCD_TYPE_PROC = 0,
58 union cfs_trace_data_union (*cfs_trace_data[CFS_TCD_TYPE_CNT])[NR_CPUS] __cacheline_aligned;
60 char cfs_tracefile[TRACEFILE_NAME_SIZE];
61 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
63 struct task_struct *tctl_task;
65 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
66 static DECLARE_RWSEM(cfs_tracefile_sem);
68 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
69 struct cfs_trace_cpu_data *tcd);
71 /* trace file lock routines */
72 /* The walking argument indicates the locking comes from all tcd types
73 * iterator and we must lock it and dissable local irqs to avoid deadlocks
74 * with other interrupt locks that might be happening. See LU-1311
77 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
78 __acquires(&tcd->tcd_lock)
80 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
81 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
82 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
83 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
84 spin_lock_bh(&tcd->tcd_lock);
85 else if (unlikely(walking))
86 spin_lock_irq(&tcd->tcd_lock);
88 spin_lock(&tcd->tcd_lock);
92 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
93 __releases(&tcd->tcd_lock)
95 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
96 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
97 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
98 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
99 spin_unlock_bh(&tcd->tcd_lock);
100 else if (unlikely(walking))
101 spin_unlock_irq(&tcd->tcd_lock);
103 spin_unlock(&tcd->tcd_lock);
106 #define cfs_tcd_for_each(tcd, i, j) \
107 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) \
108 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd); \
109 j < num_possible_cpus(); \
110 j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
112 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
113 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i] && \
114 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
115 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
117 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
120 return CFS_TCD_TYPE_IRQ;
122 return CFS_TCD_TYPE_SOFTIRQ;
123 return CFS_TCD_TYPE_PROC;
126 static inline struct cfs_trace_cpu_data *
127 cfs_trace_get_tcd(void)
129 struct cfs_trace_cpu_data *tcd =
130 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
132 cfs_trace_lock_tcd(tcd, 0);
137 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
139 cfs_trace_unlock_tcd(tcd, 0);
144 static inline struct cfs_trace_page *
145 cfs_tage_from_list(struct list_head *list)
147 return list_entry(list, struct cfs_trace_page, linkage);
150 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
153 struct cfs_trace_page *tage;
155 /* My caller is trying to free memory */
156 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
160 * Don't spam console with allocation failures: they will be reported
161 * by upper layer anyway.
164 page = alloc_page(gfp);
168 tage = kmalloc(sizeof(*tage), gfp);
175 atomic_inc(&cfs_tage_allocated);
179 static void cfs_tage_free(struct cfs_trace_page *tage)
181 __LASSERT(tage != NULL);
182 __LASSERT(tage->page != NULL);
184 __free_page(tage->page);
186 atomic_dec(&cfs_tage_allocated);
189 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
190 struct list_head *queue)
192 __LASSERT(tage != NULL);
193 __LASSERT(queue != NULL);
195 list_move_tail(&tage->linkage, queue);
198 /* return a page that has 'len' bytes left at the end */
199 static struct cfs_trace_page *
200 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
202 struct cfs_trace_page *tage;
203 struct task_struct *tsk;
205 if (tcd->tcd_cur_pages > 0) {
206 __LASSERT(!list_empty(&tcd->tcd_pages));
207 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
208 if (tage->used + len <= PAGE_SIZE)
212 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
213 if (tcd->tcd_cur_stock_pages > 0) {
214 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
215 --tcd->tcd_cur_stock_pages;
216 list_del_init(&tage->linkage);
218 tage = cfs_tage_alloc(GFP_ATOMIC);
219 if (unlikely(tage == NULL)) {
220 if ((!(current->flags & PF_MEMALLOC) ||
221 in_interrupt()) && printk_ratelimit())
222 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
229 tage->cpu = smp_processor_id();
230 tage->type = tcd->tcd_type;
231 list_add_tail(&tage->linkage, &tcd->tcd_pages);
232 tcd->tcd_cur_pages++;
235 if (tcd->tcd_cur_pages > 8 && tsk)
237 * wake up tracefiled to process some pages.
239 wake_up_process(tsk);
246 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
248 int pgcount = tcd->tcd_cur_pages / 10;
249 struct page_collection pc;
250 struct cfs_trace_page *tage;
251 struct cfs_trace_page *tmp;
254 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
255 * from here: this will lead to infinite recursion.
258 if (printk_ratelimit())
259 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
260 pgcount + 1, tcd->tcd_cur_pages);
262 INIT_LIST_HEAD(&pc.pc_pages);
264 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
268 list_move_tail(&tage->linkage, &pc.pc_pages);
269 tcd->tcd_cur_pages--;
271 put_pages_on_tcd_daemon_list(&pc, tcd);
274 /* return a page that has 'len' bytes left at the end */
275 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
278 struct cfs_trace_page *tage;
281 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
282 * from here: this will lead to infinite recursion.
285 if (len > PAGE_SIZE) {
286 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
291 tage = cfs_trace_get_tage_try(tcd, len);
296 if (tcd->tcd_cur_pages > 0) {
297 tage = cfs_tage_from_list(tcd->tcd_pages.next);
299 cfs_tage_to_tail(tage, &tcd->tcd_pages);
304 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
305 struct libcfs_debug_msg_data *msgdata,
308 struct timespec64 ts;
310 ktime_get_real_ts64(&ts);
312 header->ph_subsys = msgdata->msg_subsys;
313 header->ph_mask = msgdata->msg_mask;
314 header->ph_cpu_id = smp_processor_id();
315 header->ph_type = cfs_trace_buf_idx_get();
316 /* y2038 safe since all user space treats this as unsigned, but
317 * will overflow in 2106
319 header->ph_sec = (u32)ts.tv_sec;
320 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
321 header->ph_stack = stack;
322 header->ph_pid = current->pid;
323 header->ph_line_num = msgdata->msg_line;
324 header->ph_extern_pid = 0;
328 * tty_write_msg - write a message to a certain tty, not just the console.
329 * @tty: the destination tty_struct
330 * @msg: the message to write
332 * tty_write_message is not exported, so write a same function for it
335 static void tty_write_msg(struct tty_struct *tty, const char *msg)
337 mutex_lock(&tty->atomic_write_lock);
339 if (tty->ops->write && tty->count > 0)
340 tty->ops->write(tty, msg, strlen(msg));
342 mutex_unlock(&tty->atomic_write_lock);
343 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
346 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
348 struct tty_struct *tty;
350 tty = get_current_tty();
354 tty_write_msg(tty, prefix);
355 if ((mask & D_EMERG) || (mask & D_ERROR))
356 tty_write_msg(tty, "Error");
357 tty_write_msg(tty, ": ");
358 tty_write_msg(tty, msg);
362 static void cfs_vprint_to_console(struct ptldebug_header *hdr, int mask,
363 struct va_format *vaf, const char *file,
366 char *prefix = "Lustre";
368 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
371 if (mask & D_CONSOLE) {
373 pr_emerg("%sError: %pV", prefix, vaf);
374 else if (mask & D_ERROR)
375 pr_err("%sError: %pV", prefix, vaf);
376 else if (mask & D_WARNING)
377 pr_warn("%s: %pV", prefix, vaf);
378 else if (mask & libcfs_printk)
379 pr_info("%s: %pV", prefix, vaf);
382 pr_emerg("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
383 hdr->ph_pid, hdr->ph_extern_pid, file,
384 hdr->ph_line_num, fn, vaf);
385 else if (mask & D_ERROR)
386 pr_err("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
387 hdr->ph_pid, hdr->ph_extern_pid, file,
388 hdr->ph_line_num, fn, vaf);
389 else if (mask & D_WARNING)
390 pr_warn("%s: %d:%d:(%s:%d:%s()) %pV", prefix,
391 hdr->ph_pid, hdr->ph_extern_pid, file,
392 hdr->ph_line_num, fn, vaf);
393 else if (mask & (D_CONSOLE | libcfs_printk))
394 pr_info("%s: %pV", prefix, vaf);
398 /* tty_write_msg doesn't handle formatting */
399 cfs_tty_write_message(prefix, mask, vaf->fmt);
402 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
403 const char *file, const char *fn,
404 const char *fmt, ...)
406 struct va_format vaf;
412 cfs_vprint_to_console(hdr, mask, &vaf, file, fn);
415 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
416 const char *format, ...)
418 struct cfs_trace_cpu_data *tcd = NULL;
419 struct ptldebug_header header = {0};
420 struct cfs_trace_page *tage;
421 /* string_buf is used only if tcd != NULL, and is always set then */
422 char *string_buf = NULL;
425 int needed = 85; /* seeded with average message length */
429 int mask = msgdata->msg_mask;
430 char *file = (char *)msgdata->msg_file;
431 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
433 if (strchr(file, '/'))
434 file = strrchr(file, '/') + 1;
436 tcd = cfs_trace_get_tcd();
438 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
439 * pins us to a particular CPU. This avoids an smp_processor_id()
440 * warning on Linux when debugging is enabled.
442 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
444 if (!tcd) /* arch may not log in IRQ context */
447 if (tcd->tcd_cur_pages == 0)
448 header.ph_flags |= PH_FLAG_FIRST_RECORD;
450 if (tcd->tcd_shutting_down) {
451 cfs_trace_put_tcd(tcd);
456 known_size = strlen(file) + 1;
458 known_size += strlen(msgdata->msg_fn) + 1;
460 if (libcfs_debug_binary)
461 known_size += sizeof(header);
464 * May perform an additional pass to update 'needed' and increase
465 * tage buffer size to match vsnprintf reported size required
466 * On the second pass (retry=1) use vscnprintf [which returns
467 * number of bytes written not including the terminating nul]
468 * to clarify `needed` is used as number of bytes written
469 * for the remainder of this function
471 for (retry = 0; retry < 2; retry++) {
472 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
474 if (needed + known_size > PAGE_SIZE)
477 cfs_trace_put_tcd(tcd);
482 string_buf = (char *)page_address(tage->page) +
483 tage->used + known_size;
485 max_nob = PAGE_SIZE - tage->used - known_size;
487 pr_emerg("LustreError: negative max_nob: %d\n",
490 cfs_trace_put_tcd(tcd);
495 va_start(ap, format);
497 needed = vscnprintf(string_buf, max_nob, format, ap);
499 needed = vsnprintf(string_buf, max_nob, format, ap);
502 if (needed < max_nob) /* well. printing ok.. */
506 /* `needed` is actual bytes written to string_buf */
507 if (*(string_buf + needed - 1) != '\n') {
508 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
509 file, msgdata->msg_line, msgdata->msg_fn);
510 } else if (mask & D_TTY) {
511 /* TTY needs '\r\n' to move carriage to leftmost position */
512 if (needed < 2 || *(string_buf + needed - 2) != '\r')
513 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
514 file, msgdata->msg_line, msgdata->msg_fn);
515 if (strnchr(string_buf, needed, '%'))
516 pr_info("Lustre: format at %s:%d:%s mustn't contain %%\n",
517 file, msgdata->msg_line, msgdata->msg_fn);
520 header.ph_len = known_size + needed;
521 debug_buf = (char *)page_address(tage->page) + tage->used;
523 if (libcfs_debug_binary) {
524 memcpy(debug_buf, &header, sizeof(header));
525 tage->used += sizeof(header);
526 debug_buf += sizeof(header);
529 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
530 tage->used += strlen(file) + 1;
531 debug_buf += strlen(file) + 1;
533 if (msgdata->msg_fn) {
534 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
535 tage->used += strlen(msgdata->msg_fn) + 1;
536 debug_buf += strlen(msgdata->msg_fn) + 1;
539 __LASSERT(debug_buf == string_buf);
541 tage->used += needed;
542 __LASSERT(tage->used <= PAGE_SIZE);
545 if ((mask & libcfs_printk) == 0) {
546 /* no console output requested */
548 cfs_trace_put_tcd(tcd);
553 if (libcfs_console_ratelimit &&
554 cdls->cdls_next != 0 && /* not first time ever */
555 time_before(jiffies, cdls->cdls_next)) {
556 /* skipping a console message */
559 cfs_trace_put_tcd(tcd);
563 if (time_after(jiffies, cdls->cdls_next +
564 libcfs_console_max_delay +
565 cfs_time_seconds(10))) {
566 /* last timeout was a long time ago */
567 cdls->cdls_delay /= libcfs_console_backoff * 4;
569 cdls->cdls_delay *= libcfs_console_backoff;
572 if (cdls->cdls_delay < libcfs_console_min_delay)
573 cdls->cdls_delay = libcfs_console_min_delay;
574 else if (cdls->cdls_delay > libcfs_console_max_delay)
575 cdls->cdls_delay = libcfs_console_max_delay;
577 /* ensure cdls_next is never zero after it's been seen */
578 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
582 cfs_print_to_console(&header, mask, file, msgdata->msg_fn,
584 cfs_trace_put_tcd(tcd);
586 struct va_format vaf;
588 va_start(ap, format);
591 cfs_vprint_to_console(&header, mask,
592 &vaf, file, msgdata->msg_fn);
596 if (cdls != NULL && cdls->cdls_count != 0) {
597 /* Do not allow print this to TTY */
598 cfs_print_to_console(&header, mask & ~D_TTY, file,
600 "Skipped %d previous similar message%s\n",
602 (cdls->cdls_count > 1) ? "s" : "");
604 cdls->cdls_count = 0;
609 EXPORT_SYMBOL(libcfs_debug_msg);
612 cfs_trace_assertion_failed(const char *str,
613 struct libcfs_debug_msg_data *msgdata)
615 struct ptldebug_header hdr;
617 libcfs_panic_in_progress = 1;
618 libcfs_catastrophe = 1;
621 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
623 cfs_print_to_console(&hdr, D_EMERG, msgdata->msg_file, msgdata->msg_fn,
626 panic("Lustre debug assertion failure\n");
632 panic_collect_pages(struct page_collection *pc)
634 /* Do the collect_pages job on a single CPU: assumes that all other
635 * CPUs have been stopped during a panic. If this isn't true for some
636 * arch, this will have to be implemented separately in each arch. */
639 struct cfs_trace_cpu_data *tcd;
641 INIT_LIST_HEAD(&pc->pc_pages);
643 cfs_tcd_for_each(tcd, i, j) {
644 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
645 tcd->tcd_cur_pages = 0;
647 if (pc->pc_want_daemon_pages) {
648 list_splice_init(&tcd->tcd_daemon_pages,
650 tcd->tcd_cur_daemon_pages = 0;
655 static void collect_pages_on_all_cpus(struct page_collection *pc)
657 struct cfs_trace_cpu_data *tcd;
660 for_each_possible_cpu(cpu) {
661 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
662 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
663 tcd->tcd_cur_pages = 0;
664 if (pc->pc_want_daemon_pages) {
665 list_splice_init(&tcd->tcd_daemon_pages,
667 tcd->tcd_cur_daemon_pages = 0;
673 static void collect_pages(struct page_collection *pc)
675 INIT_LIST_HEAD(&pc->pc_pages);
677 if (libcfs_panic_in_progress)
678 panic_collect_pages(pc);
680 collect_pages_on_all_cpus(pc);
683 static void put_pages_back_on_all_cpus(struct page_collection *pc)
685 struct cfs_trace_cpu_data *tcd;
686 struct list_head *cur_head;
687 struct cfs_trace_page *tage;
688 struct cfs_trace_page *tmp;
691 for_each_possible_cpu(cpu) {
692 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
693 cur_head = tcd->tcd_pages.next;
695 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
698 __LASSERT_TAGE_INVARIANT(tage);
700 if (tage->cpu != cpu || tage->type != i)
703 cfs_tage_to_tail(tage, cur_head);
704 tcd->tcd_cur_pages++;
710 static void put_pages_back(struct page_collection *pc)
712 if (!libcfs_panic_in_progress)
713 put_pages_back_on_all_cpus(pc);
716 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
717 * we have a good amount of data at all times for dumping during an LBUG, even
718 * if we have been steadily writing (and otherwise discarding) pages via the
720 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
721 struct cfs_trace_cpu_data *tcd)
723 struct cfs_trace_page *tage;
724 struct cfs_trace_page *tmp;
726 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
727 __LASSERT_TAGE_INVARIANT(tage);
729 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
732 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
733 tcd->tcd_cur_daemon_pages++;
735 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
736 struct cfs_trace_page *victim;
738 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
739 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
741 __LASSERT_TAGE_INVARIANT(victim);
743 list_del(&victim->linkage);
744 cfs_tage_free(victim);
745 tcd->tcd_cur_daemon_pages--;
750 static void put_pages_on_daemon_list(struct page_collection *pc)
752 struct cfs_trace_cpu_data *tcd;
755 for_each_possible_cpu(cpu) {
756 cfs_tcd_for_each_type_lock(tcd, i, cpu)
757 put_pages_on_tcd_daemon_list(pc, tcd);
761 #ifdef LNET_DUMP_ON_PANIC
762 void cfs_trace_debug_print(void)
764 struct page_collection pc;
765 struct cfs_trace_page *tage;
766 struct cfs_trace_page *tmp;
768 pc.pc_want_daemon_pages = 1;
770 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
774 __LASSERT_TAGE_INVARIANT(tage);
777 p = page_address(page);
778 while (p < ((char *)page_address(page) + tage->used)) {
779 struct ptldebug_header *hdr;
784 p += strlen(file) + 1;
787 len = hdr->ph_len - (int)(p - (char *)hdr);
789 cfs_print_to_console(hdr, D_EMERG, file, fn,
795 list_del(&tage->linkage);
799 #endif /* LNET_DUMP_ON_PANIC */
801 int cfs_tracefile_dump_all_pages(char *filename)
803 struct page_collection pc;
805 struct cfs_trace_page *tage;
806 struct cfs_trace_page *tmp;
810 down_write(&cfs_tracefile_sem);
812 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
816 pr_err("LustreError: can't open %s for dump: rc = %d\n",
821 pc.pc_want_daemon_pages = 1;
823 if (list_empty(&pc.pc_pages)) {
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) {
832 __LASSERT_TAGE_INVARIANT(tage);
834 buf = kmap(tage->page);
835 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
837 if (rc != (int)tage->used) {
838 pr_warn("Lustre: wanted to write %u but wrote %d\n",
841 __LASSERT(list_empty(&pc.pc_pages));
844 list_del(&tage->linkage);
848 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
850 pr_err("LustreError: sync returns: rc = %d\n", rc);
852 filp_close(filp, NULL);
854 up_write(&cfs_tracefile_sem);
858 void cfs_trace_flush_pages(void)
860 struct page_collection pc;
861 struct cfs_trace_page *tage;
863 pc.pc_want_daemon_pages = 1;
865 while (!list_empty(&pc.pc_pages)) {
866 tage = list_first_entry(&pc.pc_pages,
867 struct cfs_trace_page, linkage);
868 __LASSERT_TAGE_INVARIANT(tage);
870 list_del(&tage->linkage);
875 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
876 const char *knl_buffer, char *append)
878 /* NB if 'append' != NULL, it's a single character to append to the
879 * copied out string - usually "\n", for /proc entries and "" (i.e. a
880 * terminating zero byte) for sysctl entries */
881 int nob = strlen(knl_buffer);
883 if (nob > usr_buffer_nob)
884 nob = usr_buffer_nob;
886 if (copy_to_user(usr_buffer, knl_buffer, nob))
889 if (append != NULL && nob < usr_buffer_nob) {
890 if (copy_to_user(usr_buffer + nob, append, 1))
898 EXPORT_SYMBOL(cfs_trace_copyout_string);
900 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
906 str = memdup_user_nul(usr_str, usr_str_nob);
914 rc = cfs_tracefile_dump_all_pages(path);
920 int cfs_trace_daemon_command(char *str)
924 down_write(&cfs_tracefile_sem);
926 if (strcmp(str, "stop") == 0) {
927 up_write(&cfs_tracefile_sem);
928 cfs_trace_stop_thread();
929 down_write(&cfs_tracefile_sem);
930 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
932 } else if (strncmp(str, "size=", 5) == 0) {
935 rc = kstrtoul(str + 5, 10, &tmp);
937 if (tmp < 10 || tmp > 20480)
938 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
940 cfs_tracefile_size = tmp << 20;
942 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
944 } else if (str[0] != '/') {
947 strcpy(cfs_tracefile, str);
949 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
950 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
952 cfs_trace_start_thread();
955 up_write(&cfs_tracefile_sem);
959 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
964 str = memdup_user_nul(usr_str, usr_str_nob);
968 rc = cfs_trace_daemon_command(strim(str));
974 int cfs_trace_set_debug_mb(int mb)
979 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
980 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
981 struct cfs_trace_cpu_data *tcd;
983 if (mb < num_possible_cpus()) {
984 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
985 mb, num_possible_cpus());
986 mb = num_possible_cpus();
990 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
995 mb /= num_possible_cpus();
996 pages = mb << (20 - PAGE_SHIFT);
998 down_write(&cfs_tracefile_sem);
1000 cfs_tcd_for_each(tcd, i, j)
1001 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1003 up_write(&cfs_tracefile_sem);
1008 int cfs_trace_get_debug_mb(void)
1012 struct cfs_trace_cpu_data *tcd;
1013 int total_pages = 0;
1015 down_read(&cfs_tracefile_sem);
1017 cfs_tcd_for_each(tcd, i, j)
1018 total_pages += tcd->tcd_max_pages;
1020 up_read(&cfs_tracefile_sem);
1022 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1025 static int tracefiled(void *arg)
1027 struct page_collection pc;
1028 struct cfs_trace_page *tage;
1029 struct cfs_trace_page *tmp;
1035 pc.pc_want_daemon_pages = 0;
1037 while (!last_loop) {
1038 schedule_timeout_interruptible(cfs_time_seconds(1));
1039 if (kthread_should_stop())
1042 if (list_empty(&pc.pc_pages))
1046 down_read(&cfs_tracefile_sem);
1047 if (cfs_tracefile[0] != 0) {
1048 filp = filp_open(cfs_tracefile,
1049 O_CREAT | O_RDWR | O_LARGEFILE,
1054 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1058 up_read(&cfs_tracefile_sem);
1060 put_pages_on_daemon_list(&pc);
1061 __LASSERT(list_empty(&pc.pc_pages));
1065 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1066 struct dentry *de = file_dentry(filp);
1067 static loff_t f_pos;
1069 __LASSERT_TAGE_INVARIANT(tage);
1071 if (f_pos >= (off_t)cfs_tracefile_size)
1073 else if (f_pos > i_size_read(de->d_inode))
1074 f_pos = i_size_read(de->d_inode);
1076 buf = kmap(tage->page);
1077 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1079 if (rc != (int)tage->used) {
1080 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1082 put_pages_back(&pc);
1083 __LASSERT(list_empty(&pc.pc_pages));
1088 filp_close(filp, NULL);
1089 put_pages_on_daemon_list(&pc);
1090 if (!list_empty(&pc.pc_pages)) {
1093 pr_alert("Lustre: trace pages aren't empty\n");
1094 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1095 for (i = 0; i < num_possible_cpus(); i++)
1097 pr_cont("%d(on) ", i);
1099 pr_cont("%d(off) ", i);
1103 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1105 pr_err("Lustre: page %d belongs to cpu %d\n",
1107 pr_err("Lustre: There are %d pages unwritten\n", i);
1109 __LASSERT(list_empty(&pc.pc_pages));
1115 int cfs_trace_start_thread(void)
1117 struct task_struct *tsk;
1123 tsk = kthread_create(tracefiled, NULL, "ktracefiled");
1126 else if (cmpxchg(&tctl_task, NULL, tsk) != NULL)
1127 /* already running */
1130 wake_up_process(tsk);
1135 void cfs_trace_stop_thread(void)
1137 struct task_struct *tsk;
1139 tsk = xchg(&tctl_task, NULL);
1141 pr_info("Lustre: shutting down debug daemon thread...\n");
1146 /* percents to share the total debug memory for each type */
1147 static unsigned int pages_factor[CFS_TCD_TYPE_CNT] = {
1148 80, /* 80% pages for CFS_TCD_TYPE_PROC */
1149 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
1150 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
1153 int cfs_tracefile_init(int max_pages)
1155 struct cfs_trace_cpu_data *tcd;
1159 /* initialize trace_data */
1160 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
1161 for (i = 0; i < CFS_TCD_TYPE_CNT; i++) {
1163 kmalloc_array(num_possible_cpus(),
1164 sizeof(union cfs_trace_data_union),
1166 if (!cfs_trace_data[i])
1167 goto out_trace_data;
1170 /* arch related info initialized */
1171 cfs_tcd_for_each(tcd, i, j) {
1172 int factor = pages_factor[i];
1174 spin_lock_init(&tcd->tcd_lock);
1175 tcd->tcd_pages_factor = factor;
1179 INIT_LIST_HEAD(&tcd->tcd_pages);
1180 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1181 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1182 tcd->tcd_cur_pages = 0;
1183 tcd->tcd_cur_stock_pages = 0;
1184 tcd->tcd_cur_daemon_pages = 0;
1185 tcd->tcd_max_pages = (max_pages * factor) / 100;
1186 LASSERT(tcd->tcd_max_pages > 0);
1187 tcd->tcd_shutting_down = 0;
1193 for (i = 0; cfs_trace_data[i]; i++) {
1194 kfree(cfs_trace_data[i]);
1195 cfs_trace_data[i] = NULL;
1197 pr_err("lnet: Not enough memory\n");
1201 static void trace_cleanup_on_all_cpus(void)
1203 struct cfs_trace_cpu_data *tcd;
1204 struct cfs_trace_page *tage;
1207 for_each_possible_cpu(cpu) {
1208 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1209 if (!tcd->tcd_pages_factor)
1210 /* Not initialised */
1212 tcd->tcd_shutting_down = 1;
1214 while (!list_empty(&tcd->tcd_pages)) {
1215 tage = list_first_entry(&tcd->tcd_pages,
1216 struct cfs_trace_page,
1218 __LASSERT_TAGE_INVARIANT(tage);
1220 list_del(&tage->linkage);
1221 cfs_tage_free(tage);
1223 tcd->tcd_cur_pages = 0;
1228 static void cfs_trace_cleanup(void)
1230 struct page_collection pc;
1233 INIT_LIST_HEAD(&pc.pc_pages);
1235 trace_cleanup_on_all_cpus();
1237 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) {
1238 kfree(cfs_trace_data[i]);
1239 cfs_trace_data[i] = NULL;
1243 void cfs_tracefile_exit(void)
1245 cfs_trace_stop_thread();
1246 cfs_trace_cleanup();