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/uaccess.h>
48 #include <libcfs/linux/linux-fs.h>
49 #include <libcfs/libcfs.h>
51 /* XXX move things up to the top, comment */
52 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
54 char cfs_tracefile[TRACEFILE_NAME_SIZE];
55 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
56 static struct tracefiled_ctl trace_tctl;
57 static DEFINE_MUTEX(cfs_trace_thread_mutex);
58 static int thread_running = 0;
60 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
61 static DECLARE_RWSEM(cfs_tracefile_sem);
63 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
64 struct cfs_trace_cpu_data *tcd);
66 /* trace file lock routines */
67 /* The walking argument indicates the locking comes from all tcd types
68 * iterator and we must lock it and dissable local irqs to avoid deadlocks
69 * with other interrupt locks that might be happening. See LU-1311
72 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
73 __acquires(&tcd->tcd_lock)
75 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
76 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
77 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
78 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
79 spin_lock_bh(&tcd->tcd_lock);
80 else if (unlikely(walking))
81 spin_lock_irq(&tcd->tcd_lock);
83 spin_lock(&tcd->tcd_lock);
87 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
88 __releases(&tcd->tcd_lock)
90 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
91 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
92 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
93 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
94 spin_unlock_bh(&tcd->tcd_lock);
95 else if (unlikely(walking))
96 spin_unlock_irq(&tcd->tcd_lock);
98 spin_unlock(&tcd->tcd_lock);
101 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
102 for (i = 0; cfs_trace_data[i] && \
103 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
104 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
106 static inline struct cfs_trace_cpu_data *
107 cfs_trace_get_tcd(void)
109 struct cfs_trace_cpu_data *tcd =
110 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
112 cfs_trace_lock_tcd(tcd, 0);
117 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
119 cfs_trace_unlock_tcd(tcd, 0);
124 static inline struct cfs_trace_page *
125 cfs_tage_from_list(struct list_head *list)
127 return list_entry(list, struct cfs_trace_page, linkage);
130 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
133 struct cfs_trace_page *tage;
135 /* My caller is trying to free memory */
136 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
140 * Don't spam console with allocation failures: they will be reported
141 * by upper layer anyway.
144 page = alloc_page(gfp);
148 tage = kmalloc(sizeof(*tage), gfp);
155 atomic_inc(&cfs_tage_allocated);
159 static void cfs_tage_free(struct cfs_trace_page *tage)
161 __LASSERT(tage != NULL);
162 __LASSERT(tage->page != NULL);
164 __free_page(tage->page);
166 atomic_dec(&cfs_tage_allocated);
169 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
170 struct list_head *queue)
172 __LASSERT(tage != NULL);
173 __LASSERT(queue != NULL);
175 list_move_tail(&tage->linkage, queue);
178 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
179 struct list_head *stock)
184 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
185 * from here: this will lead to infinite recursion.
188 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
189 struct cfs_trace_page *tage;
191 tage = cfs_tage_alloc(gfp);
194 list_add_tail(&tage->linkage, stock);
199 /* return a page that has 'len' bytes left at the end */
200 static struct cfs_trace_page *
201 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
203 struct cfs_trace_page *tage;
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++;
234 if (tcd->tcd_cur_pages > 8 && thread_running) {
235 struct tracefiled_ctl *tctl = &trace_tctl;
237 * wake up tracefiled to process some pages.
239 wake_up(&tctl->tctl_waitq);
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 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
305 const char *format, ...)
307 struct cfs_trace_cpu_data *tcd = NULL;
308 struct ptldebug_header header = {0};
309 struct cfs_trace_page *tage;
310 /* string_buf is used only if tcd != NULL, and is always set then */
311 char *string_buf = NULL;
314 int needed = 85; /* seeded with average message length */
318 int mask = msgdata->msg_mask;
319 char *file = (char *)msgdata->msg_file;
320 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
322 if (strchr(file, '/'))
323 file = strrchr(file, '/') + 1;
325 tcd = cfs_trace_get_tcd();
327 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
328 * pins us to a particular CPU. This avoids an smp_processor_id()
329 * warning on Linux when debugging is enabled.
331 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
333 if (!tcd) /* arch may not log in IRQ context */
336 if (tcd->tcd_cur_pages == 0)
337 header.ph_flags |= PH_FLAG_FIRST_RECORD;
339 if (tcd->tcd_shutting_down) {
340 cfs_trace_put_tcd(tcd);
345 known_size = strlen(file) + 1;
347 known_size += strlen(msgdata->msg_fn) + 1;
349 if (libcfs_debug_binary)
350 known_size += sizeof(header);
353 * May perform an additional pass to update 'needed' and increase
354 * tage buffer size to match vsnprintf reported size required
355 * On the second pass (retry=1) use vscnprintf [which returns
356 * number of bytes written not including the terminating nul]
357 * to clarify `needed` is used as number of bytes written
358 * for the remainder of this function
360 for (retry = 0; retry < 2; retry++) {
361 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
363 if (needed + known_size > PAGE_SIZE)
366 cfs_trace_put_tcd(tcd);
371 string_buf = (char *)page_address(tage->page) +
372 tage->used + known_size;
374 max_nob = PAGE_SIZE - tage->used - known_size;
376 pr_emerg("LustreError: negative max_nob: %d\n",
379 cfs_trace_put_tcd(tcd);
384 va_start(ap, format);
386 needed = vscnprintf(string_buf, max_nob, format, ap);
388 needed = vsnprintf(string_buf, max_nob, format, ap);
391 if (needed < max_nob) /* well. printing ok.. */
395 /* `needed` is actual bytes written to string_buf */
396 if (*(string_buf + needed - 1) != '\n') {
397 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
398 file, msgdata->msg_line, msgdata->msg_fn);
399 } else if (mask & D_TTY) {
400 /* TTY needs '\r\n' to move carriage to leftmost position */
401 if (needed < 2 || *(string_buf + needed - 2) != '\r')
402 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
403 file, msgdata->msg_line, msgdata->msg_fn);
406 header.ph_len = known_size + needed;
407 debug_buf = (char *)page_address(tage->page) + tage->used;
409 if (libcfs_debug_binary) {
410 memcpy(debug_buf, &header, sizeof(header));
411 tage->used += sizeof(header);
412 debug_buf += sizeof(header);
415 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
416 tage->used += strlen(file) + 1;
417 debug_buf += strlen(file) + 1;
419 if (msgdata->msg_fn) {
420 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
421 tage->used += strlen(msgdata->msg_fn) + 1;
422 debug_buf += strlen(msgdata->msg_fn) + 1;
425 __LASSERT(debug_buf == string_buf);
427 tage->used += needed;
428 __LASSERT(tage->used <= PAGE_SIZE);
431 if ((mask & libcfs_printk) == 0) {
432 /* no console output requested */
434 cfs_trace_put_tcd(tcd);
439 if (libcfs_console_ratelimit &&
440 cdls->cdls_next != 0 && /* not first time ever */
441 time_before(jiffies, cdls->cdls_next)) {
442 /* skipping a console message */
445 cfs_trace_put_tcd(tcd);
449 if (time_after(jiffies, cdls->cdls_next +
450 libcfs_console_max_delay +
451 cfs_time_seconds(10))) {
452 /* last timeout was a long time ago */
453 cdls->cdls_delay /= libcfs_console_backoff * 4;
455 cdls->cdls_delay *= libcfs_console_backoff;
458 if (cdls->cdls_delay < libcfs_console_min_delay)
459 cdls->cdls_delay = libcfs_console_min_delay;
460 else if (cdls->cdls_delay > libcfs_console_max_delay)
461 cdls->cdls_delay = libcfs_console_max_delay;
463 /* ensure cdls_next is never zero after it's been seen */
464 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
468 cfs_print_to_console(&header, mask, string_buf, needed, file,
470 cfs_trace_put_tcd(tcd);
472 string_buf = cfs_trace_get_console_buffer();
474 va_start(ap, format);
475 needed = vscnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
479 cfs_print_to_console(&header, mask,
480 string_buf, needed, file, msgdata->msg_fn);
485 if (cdls != NULL && cdls->cdls_count != 0) {
486 string_buf = cfs_trace_get_console_buffer();
488 needed = scnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
489 "Skipped %d previous similar message%s\n",
491 (cdls->cdls_count > 1) ? "s" : "");
493 /* Do not allow print this to TTY */
494 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
495 needed, file, msgdata->msg_fn);
498 cdls->cdls_count = 0;
503 EXPORT_SYMBOL(libcfs_debug_msg);
506 cfs_trace_assertion_failed(const char *str,
507 struct libcfs_debug_msg_data *msgdata)
509 struct ptldebug_header hdr;
511 libcfs_panic_in_progress = 1;
512 libcfs_catastrophe = 1;
515 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
517 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
518 msgdata->msg_file, msgdata->msg_fn);
520 panic("Lustre debug assertion failure\n");
526 panic_collect_pages(struct page_collection *pc)
528 /* Do the collect_pages job on a single CPU: assumes that all other
529 * CPUs have been stopped during a panic. If this isn't true for some
530 * arch, this will have to be implemented separately in each arch. */
533 struct cfs_trace_cpu_data *tcd;
535 INIT_LIST_HEAD(&pc->pc_pages);
537 cfs_tcd_for_each(tcd, i, j) {
538 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
539 tcd->tcd_cur_pages = 0;
541 if (pc->pc_want_daemon_pages) {
542 list_splice_init(&tcd->tcd_daemon_pages,
544 tcd->tcd_cur_daemon_pages = 0;
549 static void collect_pages_on_all_cpus(struct page_collection *pc)
551 struct cfs_trace_cpu_data *tcd;
554 for_each_possible_cpu(cpu) {
555 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
556 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
557 tcd->tcd_cur_pages = 0;
558 if (pc->pc_want_daemon_pages) {
559 list_splice_init(&tcd->tcd_daemon_pages,
561 tcd->tcd_cur_daemon_pages = 0;
567 static void collect_pages(struct page_collection *pc)
569 INIT_LIST_HEAD(&pc->pc_pages);
571 if (libcfs_panic_in_progress)
572 panic_collect_pages(pc);
574 collect_pages_on_all_cpus(pc);
577 static void put_pages_back_on_all_cpus(struct page_collection *pc)
579 struct cfs_trace_cpu_data *tcd;
580 struct list_head *cur_head;
581 struct cfs_trace_page *tage;
582 struct cfs_trace_page *tmp;
585 for_each_possible_cpu(cpu) {
586 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
587 cur_head = tcd->tcd_pages.next;
589 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
592 __LASSERT_TAGE_INVARIANT(tage);
594 if (tage->cpu != cpu || tage->type != i)
597 cfs_tage_to_tail(tage, cur_head);
598 tcd->tcd_cur_pages++;
604 static void put_pages_back(struct page_collection *pc)
606 if (!libcfs_panic_in_progress)
607 put_pages_back_on_all_cpus(pc);
610 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
611 * we have a good amount of data at all times for dumping during an LBUG, even
612 * if we have been steadily writing (and otherwise discarding) pages via the
614 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
615 struct cfs_trace_cpu_data *tcd)
617 struct cfs_trace_page *tage;
618 struct cfs_trace_page *tmp;
620 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
621 __LASSERT_TAGE_INVARIANT(tage);
623 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
626 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
627 tcd->tcd_cur_daemon_pages++;
629 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
630 struct cfs_trace_page *victim;
632 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
633 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
635 __LASSERT_TAGE_INVARIANT(victim);
637 list_del(&victim->linkage);
638 cfs_tage_free(victim);
639 tcd->tcd_cur_daemon_pages--;
644 static void put_pages_on_daemon_list(struct page_collection *pc)
646 struct cfs_trace_cpu_data *tcd;
649 for_each_possible_cpu(cpu) {
650 cfs_tcd_for_each_type_lock(tcd, i, cpu)
651 put_pages_on_tcd_daemon_list(pc, tcd);
655 void cfs_trace_debug_print(void)
657 struct page_collection pc;
658 struct cfs_trace_page *tage;
659 struct cfs_trace_page *tmp;
661 pc.pc_want_daemon_pages = 1;
663 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
667 __LASSERT_TAGE_INVARIANT(tage);
670 p = page_address(page);
671 while (p < ((char *)page_address(page) + tage->used)) {
672 struct ptldebug_header *hdr;
677 p += strlen(file) + 1;
680 len = hdr->ph_len - (int)(p - (char *)hdr);
682 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
687 list_del(&tage->linkage);
692 int cfs_tracefile_dump_all_pages(char *filename)
694 struct page_collection pc;
696 struct cfs_trace_page *tage;
697 struct cfs_trace_page *tmp;
701 down_write(&cfs_tracefile_sem);
703 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
707 pr_err("LustreError: can't open %s for dump: rc = %d\n",
712 pc.pc_want_daemon_pages = 1;
714 if (list_empty(&pc.pc_pages)) {
719 /* ok, for now, just write the pages. in the future we'll be building
720 * iobufs with the pages and calling generic_direct_IO */
721 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
723 __LASSERT_TAGE_INVARIANT(tage);
725 buf = kmap(tage->page);
726 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
728 if (rc != (int)tage->used) {
729 pr_warn("Lustre: wanted to write %u but wrote %d\n",
732 __LASSERT(list_empty(&pc.pc_pages));
735 list_del(&tage->linkage);
739 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
741 pr_err("LustreError: sync returns: rc = %d\n", rc);
743 filp_close(filp, NULL);
745 up_write(&cfs_tracefile_sem);
749 void cfs_trace_flush_pages(void)
751 struct page_collection pc;
752 struct cfs_trace_page *tage;
753 struct cfs_trace_page *tmp;
755 pc.pc_want_daemon_pages = 1;
757 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
759 __LASSERT_TAGE_INVARIANT(tage);
761 list_del(&tage->linkage);
766 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
767 const char __user *usr_buffer, int usr_buffer_nob)
771 if (usr_buffer_nob > knl_buffer_nob)
774 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
777 nob = strnlen(knl_buffer, usr_buffer_nob);
778 while (--nob >= 0) /* strip trailing whitespace */
779 if (!isspace(knl_buffer[nob]))
782 if (nob < 0) /* empty string */
785 if (nob == knl_buffer_nob) /* no space to terminate */
788 knl_buffer[nob + 1] = 0; /* terminate */
791 EXPORT_SYMBOL(cfs_trace_copyin_string);
793 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
794 const char *knl_buffer, char *append)
796 /* NB if 'append' != NULL, it's a single character to append to the
797 * copied out string - usually "\n", for /proc entries and "" (i.e. a
798 * terminating zero byte) for sysctl entries */
799 int nob = strlen(knl_buffer);
801 if (nob > usr_buffer_nob)
802 nob = usr_buffer_nob;
804 if (copy_to_user(usr_buffer, knl_buffer, nob))
807 if (append != NULL && nob < usr_buffer_nob) {
808 if (copy_to_user(usr_buffer + nob, append, 1))
816 EXPORT_SYMBOL(cfs_trace_copyout_string);
818 int cfs_trace_allocate_string_buffer(char **str, int nob)
820 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
823 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
830 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
835 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
839 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
840 usr_str, usr_str_nob);
848 rc = cfs_tracefile_dump_all_pages(str);
854 int cfs_trace_daemon_command(char *str)
858 down_write(&cfs_tracefile_sem);
860 if (strcmp(str, "stop") == 0) {
861 up_write(&cfs_tracefile_sem);
862 cfs_trace_stop_thread();
863 down_write(&cfs_tracefile_sem);
864 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
866 } else if (strncmp(str, "size=", 5) == 0) {
869 rc = kstrtoul(str + 5, 10, &tmp);
871 if (tmp < 10 || tmp > 20480)
872 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
874 cfs_tracefile_size = tmp << 20;
876 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
878 } else if (str[0] != '/') {
881 strcpy(cfs_tracefile, str);
883 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
884 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
886 cfs_trace_start_thread();
889 up_write(&cfs_tracefile_sem);
893 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
898 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
902 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
903 usr_str, usr_str_nob);
905 rc = cfs_trace_daemon_command(str);
911 int cfs_trace_set_debug_mb(int mb)
916 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
917 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
918 struct cfs_trace_cpu_data *tcd;
920 if (mb < num_possible_cpus()) {
921 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
922 mb, num_possible_cpus());
923 mb = num_possible_cpus();
927 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
932 mb /= num_possible_cpus();
933 pages = mb << (20 - PAGE_SHIFT);
935 down_write(&cfs_tracefile_sem);
937 cfs_tcd_for_each(tcd, i, j)
938 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
940 up_write(&cfs_tracefile_sem);
945 int cfs_trace_get_debug_mb(void)
949 struct cfs_trace_cpu_data *tcd;
952 down_read(&cfs_tracefile_sem);
954 cfs_tcd_for_each(tcd, i, j)
955 total_pages += tcd->tcd_max_pages;
957 up_read(&cfs_tracefile_sem);
959 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
962 static int tracefiled(void *arg)
964 struct page_collection pc;
965 struct tracefiled_ctl *tctl = arg;
966 struct cfs_trace_page *tage;
967 struct cfs_trace_page *tmp;
973 /* we're started late enough that we pick up init's fs context */
974 /* this is so broken in uml? what on earth is going on? */
976 complete(&tctl->tctl_start);
979 wait_queue_entry_t __wait;
981 pc.pc_want_daemon_pages = 0;
983 if (list_empty(&pc.pc_pages))
987 down_read(&cfs_tracefile_sem);
988 if (cfs_tracefile[0] != 0) {
989 filp = filp_open(cfs_tracefile,
990 O_CREAT | O_RDWR | O_LARGEFILE,
995 pr_warn("Lustre: couldn't open %s: rc = %d\n",
999 up_read(&cfs_tracefile_sem);
1001 put_pages_on_daemon_list(&pc);
1002 __LASSERT(list_empty(&pc.pc_pages));
1006 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1007 struct dentry *de = file_dentry(filp);
1008 static loff_t f_pos;
1010 __LASSERT_TAGE_INVARIANT(tage);
1012 if (f_pos >= (off_t)cfs_tracefile_size)
1014 else if (f_pos > i_size_read(de->d_inode))
1015 f_pos = i_size_read(de->d_inode);
1017 buf = kmap(tage->page);
1018 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1020 if (rc != (int)tage->used) {
1021 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1023 put_pages_back(&pc);
1024 __LASSERT(list_empty(&pc.pc_pages));
1029 filp_close(filp, NULL);
1030 put_pages_on_daemon_list(&pc);
1031 if (!list_empty(&pc.pc_pages)) {
1034 pr_alert("Lustre: trace pages aren't empty\n");
1035 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1036 for (i = 0; i < num_possible_cpus(); i++)
1038 pr_cont("%d(on) ", i);
1040 pr_cont("%d(off) ", i);
1044 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1046 pr_err("Lustre: page %d belongs to cpu %d\n",
1048 pr_err("Lustre: There are %d pages unwritten\n", i);
1050 __LASSERT(list_empty(&pc.pc_pages));
1052 if (atomic_read(&tctl->tctl_shutdown)) {
1053 if (last_loop == 0) {
1060 init_waitqueue_entry(&__wait, current);
1061 add_wait_queue(&tctl->tctl_waitq, &__wait);
1062 schedule_timeout_interruptible(cfs_time_seconds(1));
1063 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1065 complete(&tctl->tctl_stop);
1069 int cfs_trace_start_thread(void)
1071 struct tracefiled_ctl *tctl = &trace_tctl;
1074 mutex_lock(&cfs_trace_thread_mutex);
1078 init_completion(&tctl->tctl_start);
1079 init_completion(&tctl->tctl_stop);
1080 init_waitqueue_head(&tctl->tctl_waitq);
1081 atomic_set(&tctl->tctl_shutdown, 0);
1083 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1088 wait_for_completion(&tctl->tctl_start);
1091 mutex_unlock(&cfs_trace_thread_mutex);
1095 void cfs_trace_stop_thread(void)
1097 struct tracefiled_ctl *tctl = &trace_tctl;
1099 mutex_lock(&cfs_trace_thread_mutex);
1100 if (thread_running) {
1101 pr_info("Lustre: shutting down debug daemon thread...\n");
1102 atomic_set(&tctl->tctl_shutdown, 1);
1103 wait_for_completion(&tctl->tctl_stop);
1106 mutex_unlock(&cfs_trace_thread_mutex);
1109 int cfs_tracefile_init(int max_pages)
1111 struct cfs_trace_cpu_data *tcd;
1117 rc = cfs_tracefile_init_arch();
1121 cfs_tcd_for_each(tcd, i, j) {
1122 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1123 factor = tcd->tcd_pages_factor;
1124 INIT_LIST_HEAD(&tcd->tcd_pages);
1125 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1126 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1127 tcd->tcd_cur_pages = 0;
1128 tcd->tcd_cur_stock_pages = 0;
1129 tcd->tcd_cur_daemon_pages = 0;
1130 tcd->tcd_max_pages = (max_pages * factor) / 100;
1131 LASSERT(tcd->tcd_max_pages > 0);
1132 tcd->tcd_shutting_down = 0;
1137 static void trace_cleanup_on_all_cpus(void)
1139 struct cfs_trace_cpu_data *tcd;
1140 struct cfs_trace_page *tage;
1141 struct cfs_trace_page *tmp;
1144 for_each_possible_cpu(cpu) {
1145 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1146 tcd->tcd_shutting_down = 1;
1148 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1149 __LASSERT_TAGE_INVARIANT(tage);
1151 list_del(&tage->linkage);
1152 cfs_tage_free(tage);
1154 tcd->tcd_cur_pages = 0;
1159 static void cfs_trace_cleanup(void)
1161 struct page_collection pc;
1163 INIT_LIST_HEAD(&pc.pc_pages);
1165 trace_cleanup_on_all_cpus();
1167 cfs_tracefile_fini_arch();
1170 void cfs_tracefile_exit(void)
1172 cfs_trace_stop_thread();
1173 cfs_trace_cleanup();