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>
38 #define DEBUG_SUBSYSTEM S_LNET
39 #include "tracefile.h"
41 #include <linux/ctype.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>
52 enum cfs_trace_buf_type {
53 CFS_TCD_TYPE_PROC = 0,
59 union cfs_trace_data_union (*cfs_trace_data[CFS_TCD_TYPE_CNT])[NR_CPUS] __cacheline_aligned;
61 char cfs_tracefile[TRACEFILE_NAME_SIZE];
62 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
63 static struct tracefiled_ctl trace_tctl;
64 static DEFINE_MUTEX(cfs_trace_thread_mutex);
65 static int thread_running = 0;
67 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
68 static DECLARE_RWSEM(cfs_tracefile_sem);
70 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
71 struct cfs_trace_cpu_data *tcd);
73 /* trace file lock routines */
74 /* The walking argument indicates the locking comes from all tcd types
75 * iterator and we must lock it and dissable local irqs to avoid deadlocks
76 * with other interrupt locks that might be happening. See LU-1311
79 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
80 __acquires(&tcd->tcd_lock)
82 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
83 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
84 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
85 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
86 spin_lock_bh(&tcd->tcd_lock);
87 else if (unlikely(walking))
88 spin_lock_irq(&tcd->tcd_lock);
90 spin_lock(&tcd->tcd_lock);
94 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
95 __releases(&tcd->tcd_lock)
97 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
98 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
99 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
100 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
101 spin_unlock_bh(&tcd->tcd_lock);
102 else if (unlikely(walking))
103 spin_unlock_irq(&tcd->tcd_lock);
105 spin_unlock(&tcd->tcd_lock);
108 #define cfs_tcd_for_each(tcd, i, j) \
109 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) \
110 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd); \
111 j < num_possible_cpus(); \
112 j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
114 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
115 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i] && \
116 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
117 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
119 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
122 return CFS_TCD_TYPE_IRQ;
124 return CFS_TCD_TYPE_SOFTIRQ;
125 return CFS_TCD_TYPE_PROC;
128 static inline struct cfs_trace_cpu_data *
129 cfs_trace_get_tcd(void)
131 struct cfs_trace_cpu_data *tcd =
132 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
134 cfs_trace_lock_tcd(tcd, 0);
139 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
141 cfs_trace_unlock_tcd(tcd, 0);
146 static inline struct cfs_trace_page *
147 cfs_tage_from_list(struct list_head *list)
149 return list_entry(list, struct cfs_trace_page, linkage);
152 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
155 struct cfs_trace_page *tage;
157 /* My caller is trying to free memory */
158 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
162 * Don't spam console with allocation failures: they will be reported
163 * by upper layer anyway.
166 page = alloc_page(gfp);
170 tage = kmalloc(sizeof(*tage), gfp);
177 atomic_inc(&cfs_tage_allocated);
181 static void cfs_tage_free(struct cfs_trace_page *tage)
183 __LASSERT(tage != NULL);
184 __LASSERT(tage->page != NULL);
186 __free_page(tage->page);
188 atomic_dec(&cfs_tage_allocated);
191 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
192 struct list_head *queue)
194 __LASSERT(tage != NULL);
195 __LASSERT(queue != NULL);
197 list_move_tail(&tage->linkage, queue);
200 /* return a page that has 'len' bytes left at the end */
201 static struct cfs_trace_page *
202 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
204 struct cfs_trace_page *tage;
206 if (tcd->tcd_cur_pages > 0) {
207 __LASSERT(!list_empty(&tcd->tcd_pages));
208 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
209 if (tage->used + len <= PAGE_SIZE)
213 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
214 if (tcd->tcd_cur_stock_pages > 0) {
215 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
216 --tcd->tcd_cur_stock_pages;
217 list_del_init(&tage->linkage);
219 tage = cfs_tage_alloc(GFP_ATOMIC);
220 if (unlikely(tage == NULL)) {
221 if ((!(current->flags & PF_MEMALLOC) ||
222 in_interrupt()) && printk_ratelimit())
223 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
230 tage->cpu = smp_processor_id();
231 tage->type = tcd->tcd_type;
232 list_add_tail(&tage->linkage, &tcd->tcd_pages);
233 tcd->tcd_cur_pages++;
235 if (tcd->tcd_cur_pages > 8 && thread_running) {
236 struct tracefiled_ctl *tctl = &trace_tctl;
238 * wake up tracefiled to process some pages.
240 wake_up(&tctl->tctl_waitq);
247 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
249 int pgcount = tcd->tcd_cur_pages / 10;
250 struct page_collection pc;
251 struct cfs_trace_page *tage;
252 struct cfs_trace_page *tmp;
255 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
256 * from here: this will lead to infinite recursion.
259 if (printk_ratelimit())
260 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
261 pgcount + 1, tcd->tcd_cur_pages);
263 INIT_LIST_HEAD(&pc.pc_pages);
265 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
269 list_move_tail(&tage->linkage, &pc.pc_pages);
270 tcd->tcd_cur_pages--;
272 put_pages_on_tcd_daemon_list(&pc, tcd);
275 /* return a page that has 'len' bytes left at the end */
276 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
279 struct cfs_trace_page *tage;
282 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
283 * from here: this will lead to infinite recursion.
286 if (len > PAGE_SIZE) {
287 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
292 tage = cfs_trace_get_tage_try(tcd, len);
297 if (tcd->tcd_cur_pages > 0) {
298 tage = cfs_tage_from_list(tcd->tcd_pages.next);
300 cfs_tage_to_tail(tage, &tcd->tcd_pages);
305 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
306 struct libcfs_debug_msg_data *msgdata,
309 struct timespec64 ts;
311 ktime_get_real_ts64(&ts);
313 header->ph_subsys = msgdata->msg_subsys;
314 header->ph_mask = msgdata->msg_mask;
315 header->ph_cpu_id = smp_processor_id();
316 header->ph_type = cfs_trace_buf_idx_get();
317 /* y2038 safe since all user space treats this as unsigned, but
318 * will overflow in 2106
320 header->ph_sec = (u32)ts.tv_sec;
321 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
322 header->ph_stack = stack;
323 header->ph_pid = current->pid;
324 header->ph_line_num = msgdata->msg_line;
325 header->ph_extern_pid = 0;
329 * tty_write_msg - write a message to a certain tty, not just the console.
330 * @tty: the destination tty_struct
331 * @msg: the message to write
333 * tty_write_message is not exported, so write a same function for it
336 static void tty_write_msg(struct tty_struct *tty, const char *msg)
338 mutex_lock(&tty->atomic_write_lock);
340 if (tty->ops->write && tty->count > 0)
341 tty->ops->write(tty, msg, strlen(msg));
343 mutex_unlock(&tty->atomic_write_lock);
344 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
347 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
349 struct tty_struct *tty;
351 tty = get_current_tty();
355 tty_write_msg(tty, prefix);
356 if ((mask & D_EMERG) || (mask & D_ERROR))
357 tty_write_msg(tty, "Error");
358 tty_write_msg(tty, ": ");
359 tty_write_msg(tty, msg);
363 static void cfs_vprint_to_console(struct ptldebug_header *hdr, int mask,
364 struct va_format *vaf, const char *file,
367 char *prefix = "Lustre";
369 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
372 if (mask & D_CONSOLE) {
374 pr_emerg("%sError: %pV", prefix, vaf);
375 else if (mask & D_ERROR)
376 pr_err("%sError: %pV", prefix, vaf);
377 else if (mask & D_WARNING)
378 pr_warn("%s: %pV", prefix, vaf);
379 else if (mask & libcfs_printk)
380 pr_info("%s: %pV", prefix, vaf);
383 pr_emerg("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
384 hdr->ph_pid, hdr->ph_extern_pid, file,
385 hdr->ph_line_num, fn, vaf);
386 else if (mask & D_ERROR)
387 pr_err("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
388 hdr->ph_pid, hdr->ph_extern_pid, file,
389 hdr->ph_line_num, fn, vaf);
390 else if (mask & D_WARNING)
391 pr_warn("%s: %d:%d:(%s:%d:%s()) %pV", prefix,
392 hdr->ph_pid, hdr->ph_extern_pid, file,
393 hdr->ph_line_num, fn, vaf);
394 else if (mask & (D_CONSOLE | libcfs_printk))
395 pr_info("%s: %pV", prefix, vaf);
399 /* tty_write_msg doesn't handle formatting */
400 cfs_tty_write_message(prefix, mask, vaf->fmt);
403 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
404 const char *file, const char *fn,
405 const char *fmt, ...)
407 struct va_format vaf;
413 cfs_vprint_to_console(hdr, mask, &vaf, file, fn);
416 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
417 const char *format, ...)
419 struct cfs_trace_cpu_data *tcd = NULL;
420 struct ptldebug_header header = {0};
421 struct cfs_trace_page *tage;
422 /* string_buf is used only if tcd != NULL, and is always set then */
423 char *string_buf = NULL;
426 int needed = 85; /* seeded with average message length */
430 int mask = msgdata->msg_mask;
431 char *file = (char *)msgdata->msg_file;
432 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
434 if (strchr(file, '/'))
435 file = strrchr(file, '/') + 1;
437 tcd = cfs_trace_get_tcd();
439 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
440 * pins us to a particular CPU. This avoids an smp_processor_id()
441 * warning on Linux when debugging is enabled.
443 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
445 if (!tcd) /* arch may not log in IRQ context */
448 if (tcd->tcd_cur_pages == 0)
449 header.ph_flags |= PH_FLAG_FIRST_RECORD;
451 if (tcd->tcd_shutting_down) {
452 cfs_trace_put_tcd(tcd);
457 known_size = strlen(file) + 1;
459 known_size += strlen(msgdata->msg_fn) + 1;
461 if (libcfs_debug_binary)
462 known_size += sizeof(header);
465 * May perform an additional pass to update 'needed' and increase
466 * tage buffer size to match vsnprintf reported size required
467 * On the second pass (retry=1) use vscnprintf [which returns
468 * number of bytes written not including the terminating nul]
469 * to clarify `needed` is used as number of bytes written
470 * for the remainder of this function
472 for (retry = 0; retry < 2; retry++) {
473 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
475 if (needed + known_size > PAGE_SIZE)
478 cfs_trace_put_tcd(tcd);
483 string_buf = (char *)page_address(tage->page) +
484 tage->used + known_size;
486 max_nob = PAGE_SIZE - tage->used - known_size;
488 pr_emerg("LustreError: negative max_nob: %d\n",
491 cfs_trace_put_tcd(tcd);
496 va_start(ap, format);
498 needed = vscnprintf(string_buf, max_nob, format, ap);
500 needed = vsnprintf(string_buf, max_nob, format, ap);
503 if (needed < max_nob) /* well. printing ok.. */
507 /* `needed` is actual bytes written to string_buf */
508 if (*(string_buf + needed - 1) != '\n') {
509 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
510 file, msgdata->msg_line, msgdata->msg_fn);
511 } else if (mask & D_TTY) {
512 /* TTY needs '\r\n' to move carriage to leftmost position */
513 if (needed < 2 || *(string_buf + needed - 2) != '\r')
514 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
515 file, msgdata->msg_line, msgdata->msg_fn);
516 if (strnchr(string_buf, needed, '%'))
517 pr_info("Lustre: format at %s:%d:%s mustn't contain %%\n",
518 file, msgdata->msg_line, msgdata->msg_fn);
521 header.ph_len = known_size + needed;
522 debug_buf = (char *)page_address(tage->page) + tage->used;
524 if (libcfs_debug_binary) {
525 memcpy(debug_buf, &header, sizeof(header));
526 tage->used += sizeof(header);
527 debug_buf += sizeof(header);
530 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
531 tage->used += strlen(file) + 1;
532 debug_buf += strlen(file) + 1;
534 if (msgdata->msg_fn) {
535 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
536 tage->used += strlen(msgdata->msg_fn) + 1;
537 debug_buf += strlen(msgdata->msg_fn) + 1;
540 __LASSERT(debug_buf == string_buf);
542 tage->used += needed;
543 __LASSERT(tage->used <= PAGE_SIZE);
546 if ((mask & libcfs_printk) == 0) {
547 /* no console output requested */
549 cfs_trace_put_tcd(tcd);
554 if (libcfs_console_ratelimit &&
555 cdls->cdls_next != 0 && /* not first time ever */
556 time_before(jiffies, cdls->cdls_next)) {
557 /* skipping a console message */
560 cfs_trace_put_tcd(tcd);
564 if (time_after(jiffies, cdls->cdls_next +
565 libcfs_console_max_delay +
566 cfs_time_seconds(10))) {
567 /* last timeout was a long time ago */
568 cdls->cdls_delay /= libcfs_console_backoff * 4;
570 cdls->cdls_delay *= libcfs_console_backoff;
573 if (cdls->cdls_delay < libcfs_console_min_delay)
574 cdls->cdls_delay = libcfs_console_min_delay;
575 else if (cdls->cdls_delay > libcfs_console_max_delay)
576 cdls->cdls_delay = libcfs_console_max_delay;
578 /* ensure cdls_next is never zero after it's been seen */
579 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
583 cfs_print_to_console(&header, mask, file, msgdata->msg_fn,
585 cfs_trace_put_tcd(tcd);
587 struct va_format vaf;
589 va_start(ap, format);
592 cfs_vprint_to_console(&header, mask,
593 &vaf, file, msgdata->msg_fn);
597 if (cdls != NULL && cdls->cdls_count != 0) {
598 /* Do not allow print this to TTY */
599 cfs_print_to_console(&header, mask & ~D_TTY, file,
601 "Skipped %d previous similar message%s\n",
603 (cdls->cdls_count > 1) ? "s" : "");
605 cdls->cdls_count = 0;
610 EXPORT_SYMBOL(libcfs_debug_msg);
613 cfs_trace_assertion_failed(const char *str,
614 struct libcfs_debug_msg_data *msgdata)
616 struct ptldebug_header hdr;
618 libcfs_panic_in_progress = 1;
619 libcfs_catastrophe = 1;
622 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
624 cfs_print_to_console(&hdr, D_EMERG, msgdata->msg_file, msgdata->msg_fn,
627 panic("Lustre debug assertion failure\n");
633 panic_collect_pages(struct page_collection *pc)
635 /* Do the collect_pages job on a single CPU: assumes that all other
636 * CPUs have been stopped during a panic. If this isn't true for some
637 * arch, this will have to be implemented separately in each arch. */
640 struct cfs_trace_cpu_data *tcd;
642 INIT_LIST_HEAD(&pc->pc_pages);
644 cfs_tcd_for_each(tcd, i, j) {
645 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
646 tcd->tcd_cur_pages = 0;
648 if (pc->pc_want_daemon_pages) {
649 list_splice_init(&tcd->tcd_daemon_pages,
651 tcd->tcd_cur_daemon_pages = 0;
656 static void collect_pages_on_all_cpus(struct page_collection *pc)
658 struct cfs_trace_cpu_data *tcd;
661 for_each_possible_cpu(cpu) {
662 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
663 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
664 tcd->tcd_cur_pages = 0;
665 if (pc->pc_want_daemon_pages) {
666 list_splice_init(&tcd->tcd_daemon_pages,
668 tcd->tcd_cur_daemon_pages = 0;
674 static void collect_pages(struct page_collection *pc)
676 INIT_LIST_HEAD(&pc->pc_pages);
678 if (libcfs_panic_in_progress)
679 panic_collect_pages(pc);
681 collect_pages_on_all_cpus(pc);
684 static void put_pages_back_on_all_cpus(struct page_collection *pc)
686 struct cfs_trace_cpu_data *tcd;
687 struct list_head *cur_head;
688 struct cfs_trace_page *tage;
689 struct cfs_trace_page *tmp;
692 for_each_possible_cpu(cpu) {
693 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
694 cur_head = tcd->tcd_pages.next;
696 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
699 __LASSERT_TAGE_INVARIANT(tage);
701 if (tage->cpu != cpu || tage->type != i)
704 cfs_tage_to_tail(tage, cur_head);
705 tcd->tcd_cur_pages++;
711 static void put_pages_back(struct page_collection *pc)
713 if (!libcfs_panic_in_progress)
714 put_pages_back_on_all_cpus(pc);
717 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
718 * we have a good amount of data at all times for dumping during an LBUG, even
719 * if we have been steadily writing (and otherwise discarding) pages via the
721 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
722 struct cfs_trace_cpu_data *tcd)
724 struct cfs_trace_page *tage;
725 struct cfs_trace_page *tmp;
727 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
728 __LASSERT_TAGE_INVARIANT(tage);
730 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
733 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
734 tcd->tcd_cur_daemon_pages++;
736 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
737 struct cfs_trace_page *victim;
739 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
740 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
742 __LASSERT_TAGE_INVARIANT(victim);
744 list_del(&victim->linkage);
745 cfs_tage_free(victim);
746 tcd->tcd_cur_daemon_pages--;
751 static void put_pages_on_daemon_list(struct page_collection *pc)
753 struct cfs_trace_cpu_data *tcd;
756 for_each_possible_cpu(cpu) {
757 cfs_tcd_for_each_type_lock(tcd, i, cpu)
758 put_pages_on_tcd_daemon_list(pc, tcd);
762 #ifdef LNET_DUMP_ON_PANIC
763 void cfs_trace_debug_print(void)
765 struct page_collection pc;
766 struct cfs_trace_page *tage;
767 struct cfs_trace_page *tmp;
769 pc.pc_want_daemon_pages = 1;
771 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
775 __LASSERT_TAGE_INVARIANT(tage);
778 p = page_address(page);
779 while (p < ((char *)page_address(page) + tage->used)) {
780 struct ptldebug_header *hdr;
785 p += strlen(file) + 1;
788 len = hdr->ph_len - (int)(p - (char *)hdr);
790 cfs_print_to_console(hdr, D_EMERG, file, fn,
796 list_del(&tage->linkage);
800 #endif /* LNET_DUMP_ON_PANIC */
802 int cfs_tracefile_dump_all_pages(char *filename)
804 struct page_collection pc;
806 struct cfs_trace_page *tage;
807 struct cfs_trace_page *tmp;
811 down_write(&cfs_tracefile_sem);
813 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
817 pr_err("LustreError: can't open %s for dump: rc = %d\n",
822 pc.pc_want_daemon_pages = 1;
824 if (list_empty(&pc.pc_pages)) {
829 /* ok, for now, just write the pages. in the future we'll be building
830 * iobufs with the pages and calling generic_direct_IO */
831 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
833 __LASSERT_TAGE_INVARIANT(tage);
835 buf = kmap(tage->page);
836 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
838 if (rc != (int)tage->used) {
839 pr_warn("Lustre: wanted to write %u but wrote %d\n",
842 __LASSERT(list_empty(&pc.pc_pages));
845 list_del(&tage->linkage);
849 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
851 pr_err("LustreError: sync returns: rc = %d\n", rc);
853 filp_close(filp, NULL);
855 up_write(&cfs_tracefile_sem);
859 void cfs_trace_flush_pages(void)
861 struct page_collection pc;
862 struct cfs_trace_page *tage;
864 pc.pc_want_daemon_pages = 1;
866 while (!list_empty(&pc.pc_pages)) {
867 tage = list_first_entry(&pc.pc_pages,
868 struct cfs_trace_page, linkage);
869 __LASSERT_TAGE_INVARIANT(tage);
871 list_del(&tage->linkage);
876 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
877 const char __user *usr_buffer, int usr_buffer_nob)
881 if (usr_buffer_nob > knl_buffer_nob)
884 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
887 nob = strnlen(knl_buffer, usr_buffer_nob);
888 while (--nob >= 0) /* strip trailing whitespace */
889 if (!isspace(knl_buffer[nob]))
892 if (nob < 0) /* empty string */
895 if (nob == knl_buffer_nob) /* no space to terminate */
898 knl_buffer[nob + 1] = 0; /* terminate */
901 EXPORT_SYMBOL(cfs_trace_copyin_string);
903 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
904 const char *knl_buffer, char *append)
906 /* NB if 'append' != NULL, it's a single character to append to the
907 * copied out string - usually "\n", for /proc entries and "" (i.e. a
908 * terminating zero byte) for sysctl entries */
909 int nob = strlen(knl_buffer);
911 if (nob > usr_buffer_nob)
912 nob = usr_buffer_nob;
914 if (copy_to_user(usr_buffer, knl_buffer, nob))
917 if (append != NULL && nob < usr_buffer_nob) {
918 if (copy_to_user(usr_buffer + nob, append, 1))
926 EXPORT_SYMBOL(cfs_trace_copyout_string);
928 int cfs_trace_allocate_string_buffer(char **str, int nob)
930 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
933 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
940 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
945 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
949 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
950 usr_str, usr_str_nob);
958 rc = cfs_tracefile_dump_all_pages(str);
964 int cfs_trace_daemon_command(char *str)
968 down_write(&cfs_tracefile_sem);
970 if (strcmp(str, "stop") == 0) {
971 up_write(&cfs_tracefile_sem);
972 cfs_trace_stop_thread();
973 down_write(&cfs_tracefile_sem);
974 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
976 } else if (strncmp(str, "size=", 5) == 0) {
979 rc = kstrtoul(str + 5, 10, &tmp);
981 if (tmp < 10 || tmp > 20480)
982 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
984 cfs_tracefile_size = tmp << 20;
986 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
988 } else if (str[0] != '/') {
991 strcpy(cfs_tracefile, str);
993 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
994 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
996 cfs_trace_start_thread();
999 up_write(&cfs_tracefile_sem);
1003 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
1008 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
1012 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
1013 usr_str, usr_str_nob);
1015 rc = cfs_trace_daemon_command(str);
1021 int cfs_trace_set_debug_mb(int mb)
1025 unsigned long pages;
1026 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
1027 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
1028 struct cfs_trace_cpu_data *tcd;
1030 if (mb < num_possible_cpus()) {
1031 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
1032 mb, num_possible_cpus());
1033 mb = num_possible_cpus();
1037 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
1042 mb /= num_possible_cpus();
1043 pages = mb << (20 - PAGE_SHIFT);
1045 down_write(&cfs_tracefile_sem);
1047 cfs_tcd_for_each(tcd, i, j)
1048 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1050 up_write(&cfs_tracefile_sem);
1055 int cfs_trace_get_debug_mb(void)
1059 struct cfs_trace_cpu_data *tcd;
1060 int total_pages = 0;
1062 down_read(&cfs_tracefile_sem);
1064 cfs_tcd_for_each(tcd, i, j)
1065 total_pages += tcd->tcd_max_pages;
1067 up_read(&cfs_tracefile_sem);
1069 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1072 static int tracefiled(void *arg)
1074 struct page_collection pc;
1075 struct tracefiled_ctl *tctl = arg;
1076 struct cfs_trace_page *tage;
1077 struct cfs_trace_page *tmp;
1083 /* we're started late enough that we pick up init's fs context */
1084 /* this is so broken in uml? what on earth is going on? */
1086 complete(&tctl->tctl_start);
1088 pc.pc_want_daemon_pages = 0;
1090 while (!last_loop) {
1091 wait_event_timeout(tctl->tctl_waitq,
1092 ({ collect_pages(&pc);
1093 !list_empty(&pc.pc_pages); }) ||
1094 atomic_read(&tctl->tctl_shutdown),
1095 cfs_time_seconds(1));
1096 if (atomic_read(&tctl->tctl_shutdown))
1098 if (list_empty(&pc.pc_pages))
1102 down_read(&cfs_tracefile_sem);
1103 if (cfs_tracefile[0] != 0) {
1104 filp = filp_open(cfs_tracefile,
1105 O_CREAT | O_RDWR | O_LARGEFILE,
1110 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1114 up_read(&cfs_tracefile_sem);
1116 put_pages_on_daemon_list(&pc);
1117 __LASSERT(list_empty(&pc.pc_pages));
1121 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1122 struct dentry *de = file_dentry(filp);
1123 static loff_t f_pos;
1125 __LASSERT_TAGE_INVARIANT(tage);
1127 if (f_pos >= (off_t)cfs_tracefile_size)
1129 else if (f_pos > i_size_read(de->d_inode))
1130 f_pos = i_size_read(de->d_inode);
1132 buf = kmap(tage->page);
1133 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1135 if (rc != (int)tage->used) {
1136 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1138 put_pages_back(&pc);
1139 __LASSERT(list_empty(&pc.pc_pages));
1144 filp_close(filp, NULL);
1145 put_pages_on_daemon_list(&pc);
1146 if (!list_empty(&pc.pc_pages)) {
1149 pr_alert("Lustre: trace pages aren't empty\n");
1150 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1151 for (i = 0; i < num_possible_cpus(); i++)
1153 pr_cont("%d(on) ", i);
1155 pr_cont("%d(off) ", i);
1159 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1161 pr_err("Lustre: page %d belongs to cpu %d\n",
1163 pr_err("Lustre: There are %d pages unwritten\n", i);
1165 __LASSERT(list_empty(&pc.pc_pages));
1167 complete(&tctl->tctl_stop);
1171 int cfs_trace_start_thread(void)
1173 struct tracefiled_ctl *tctl = &trace_tctl;
1176 mutex_lock(&cfs_trace_thread_mutex);
1180 init_completion(&tctl->tctl_start);
1181 init_completion(&tctl->tctl_stop);
1182 init_waitqueue_head(&tctl->tctl_waitq);
1183 atomic_set(&tctl->tctl_shutdown, 0);
1185 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1190 wait_for_completion(&tctl->tctl_start);
1193 mutex_unlock(&cfs_trace_thread_mutex);
1197 void cfs_trace_stop_thread(void)
1199 struct tracefiled_ctl *tctl = &trace_tctl;
1201 mutex_lock(&cfs_trace_thread_mutex);
1202 if (thread_running) {
1203 pr_info("Lustre: shutting down debug daemon thread...\n");
1204 atomic_set(&tctl->tctl_shutdown, 1);
1205 wait_for_completion(&tctl->tctl_stop);
1208 mutex_unlock(&cfs_trace_thread_mutex);
1211 /* percents to share the total debug memory for each type */
1212 static unsigned int pages_factor[CFS_TCD_TYPE_CNT] = {
1213 80, /* 80% pages for CFS_TCD_TYPE_PROC */
1214 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
1215 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
1218 int cfs_tracefile_init(int max_pages)
1220 struct cfs_trace_cpu_data *tcd;
1224 /* initialize trace_data */
1225 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
1226 for (i = 0; i < CFS_TCD_TYPE_CNT; i++) {
1228 kmalloc_array(num_possible_cpus(),
1229 sizeof(union cfs_trace_data_union),
1231 if (!cfs_trace_data[i])
1232 goto out_trace_data;
1235 /* arch related info initialized */
1236 cfs_tcd_for_each(tcd, i, j) {
1237 int factor = pages_factor[i];
1239 spin_lock_init(&tcd->tcd_lock);
1240 tcd->tcd_pages_factor = factor;
1244 INIT_LIST_HEAD(&tcd->tcd_pages);
1245 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1246 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1247 tcd->tcd_cur_pages = 0;
1248 tcd->tcd_cur_stock_pages = 0;
1249 tcd->tcd_cur_daemon_pages = 0;
1250 tcd->tcd_max_pages = (max_pages * factor) / 100;
1251 LASSERT(tcd->tcd_max_pages > 0);
1252 tcd->tcd_shutting_down = 0;
1258 for (i = 0; cfs_trace_data[i]; i++) {
1259 kfree(cfs_trace_data[i]);
1260 cfs_trace_data[i] = NULL;
1262 pr_err("lnet: Not enough memory\n");
1266 static void trace_cleanup_on_all_cpus(void)
1268 struct cfs_trace_cpu_data *tcd;
1269 struct cfs_trace_page *tage;
1272 for_each_possible_cpu(cpu) {
1273 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1274 if (!tcd->tcd_pages_factor)
1275 /* Not initialised */
1277 tcd->tcd_shutting_down = 1;
1279 while (!list_empty(&tcd->tcd_pages)) {
1280 tage = list_first_entry(&tcd->tcd_pages,
1281 struct cfs_trace_page,
1283 __LASSERT_TAGE_INVARIANT(tage);
1285 list_del(&tage->linkage);
1286 cfs_tage_free(tage);
1288 tcd->tcd_cur_pages = 0;
1293 static void cfs_trace_cleanup(void)
1295 struct page_collection pc;
1298 INIT_LIST_HEAD(&pc.pc_pages);
1300 trace_cleanup_on_all_cpus();
1302 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) {
1303 kfree(cfs_trace_data[i]);
1304 cfs_trace_data[i] = NULL;
1308 void cfs_tracefile_exit(void)
1310 cfs_trace_stop_thread();
1311 cfs_trace_cleanup();