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 /* Pages containing records already processed by daemon.
61 * Link via ->lru, use size in ->private
63 static LIST_HEAD(daemon_pages);
64 static long daemon_pages_count;
65 static long daemon_pages_max;
67 char cfs_tracefile[TRACEFILE_NAME_SIZE];
68 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
70 struct task_struct *tctl_task;
72 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
73 static DECLARE_RWSEM(cfs_tracefile_sem);
75 /* trace file lock routines */
76 /* The walking argument indicates the locking comes from all tcd types
77 * iterator and we must lock it and dissable local irqs to avoid deadlocks
78 * with other interrupt locks that might be happening. See LU-1311
81 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
82 __acquires(&tcd->tcd_lock)
84 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
85 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
86 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
87 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
88 spin_lock_bh(&tcd->tcd_lock);
89 else if (unlikely(walking))
90 spin_lock_irq(&tcd->tcd_lock);
92 spin_lock(&tcd->tcd_lock);
96 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
97 __releases(&tcd->tcd_lock)
99 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
100 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
101 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
102 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
103 spin_unlock_bh(&tcd->tcd_lock);
104 else if (unlikely(walking))
105 spin_unlock_irq(&tcd->tcd_lock);
107 spin_unlock(&tcd->tcd_lock);
110 #define cfs_tcd_for_each(tcd, i, j) \
111 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) \
112 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd); \
113 j < num_possible_cpus(); \
114 j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
116 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
117 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i] && \
118 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
119 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
121 enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
124 return CFS_TCD_TYPE_IRQ;
126 return CFS_TCD_TYPE_SOFTIRQ;
127 return CFS_TCD_TYPE_PROC;
130 static inline struct cfs_trace_cpu_data *
131 cfs_trace_get_tcd(void)
133 struct cfs_trace_cpu_data *tcd =
134 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
136 cfs_trace_lock_tcd(tcd, 0);
141 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
143 cfs_trace_unlock_tcd(tcd, 0);
148 static inline struct cfs_trace_page *
149 cfs_tage_from_list(struct list_head *list)
151 return list_entry(list, struct cfs_trace_page, linkage);
154 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
157 struct cfs_trace_page *tage;
159 /* My caller is trying to free memory */
160 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
164 * Don't spam console with allocation failures: they will be reported
165 * by upper layer anyway.
168 page = alloc_page(gfp);
172 tage = kmalloc(sizeof(*tage), gfp);
179 atomic_inc(&cfs_tage_allocated);
183 static void cfs_tage_free(struct cfs_trace_page *tage)
185 __LASSERT(tage != NULL);
186 __LASSERT(tage->page != NULL);
188 __free_page(tage->page);
190 atomic_dec(&cfs_tage_allocated);
193 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
194 struct list_head *queue)
196 __LASSERT(tage != NULL);
197 __LASSERT(queue != NULL);
199 list_move_tail(&tage->linkage, queue);
202 /* return a page that has 'len' bytes left at the end */
203 static struct cfs_trace_page *
204 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
206 struct cfs_trace_page *tage;
207 struct task_struct *tsk;
209 if (tcd->tcd_cur_pages > 0) {
210 __LASSERT(!list_empty(&tcd->tcd_pages));
211 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
212 if (tage->used + len <= PAGE_SIZE)
216 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
217 if (tcd->tcd_cur_stock_pages > 0) {
218 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
219 --tcd->tcd_cur_stock_pages;
220 list_del_init(&tage->linkage);
222 tage = cfs_tage_alloc(GFP_ATOMIC);
223 if (unlikely(tage == NULL)) {
224 if ((!(current->flags & PF_MEMALLOC) ||
225 in_interrupt()) && printk_ratelimit())
226 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
233 tage->cpu = smp_processor_id();
234 tage->type = tcd->tcd_type;
235 list_add_tail(&tage->linkage, &tcd->tcd_pages);
236 tcd->tcd_cur_pages++;
239 if (tcd->tcd_cur_pages > 8 && tsk)
241 * wake up tracefiled to process some pages.
243 wake_up_process(tsk);
250 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
252 int pgcount = tcd->tcd_cur_pages / 10;
253 struct page_collection pc;
254 struct cfs_trace_page *tage;
255 struct cfs_trace_page *tmp;
258 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
259 * from here: this will lead to infinite recursion.
262 if (printk_ratelimit())
263 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
264 pgcount + 1, tcd->tcd_cur_pages);
266 INIT_LIST_HEAD(&pc.pc_pages);
268 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
272 list_del(&tage->linkage);
274 tcd->tcd_cur_pages--;
278 /* return a page that has 'len' bytes left at the end */
279 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
282 struct cfs_trace_page *tage;
285 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
286 * from here: this will lead to infinite recursion.
289 if (len > PAGE_SIZE) {
290 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
295 tage = cfs_trace_get_tage_try(tcd, len);
300 if (tcd->tcd_cur_pages > 0) {
301 tage = cfs_tage_from_list(tcd->tcd_pages.next);
303 cfs_tage_to_tail(tage, &tcd->tcd_pages);
308 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
309 struct libcfs_debug_msg_data *msgdata,
312 struct timespec64 ts;
314 ktime_get_real_ts64(&ts);
316 header->ph_subsys = msgdata->msg_subsys;
317 header->ph_mask = msgdata->msg_mask;
318 header->ph_cpu_id = smp_processor_id();
319 header->ph_type = cfs_trace_buf_idx_get();
320 /* y2038 safe since all user space treats this as unsigned, but
321 * will overflow in 2106
323 header->ph_sec = (u32)ts.tv_sec;
324 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
325 header->ph_stack = stack;
326 header->ph_pid = current->pid;
327 header->ph_line_num = msgdata->msg_line;
328 header->ph_extern_pid = 0;
332 * tty_write_msg - write a message to a certain tty, not just the console.
333 * @tty: the destination tty_struct
334 * @msg: the message to write
336 * tty_write_message is not exported, so write a same function for it
339 static void tty_write_msg(struct tty_struct *tty, const char *msg)
341 mutex_lock(&tty->atomic_write_lock);
343 if (tty->ops->write && tty->count > 0)
344 tty->ops->write(tty, msg, strlen(msg));
346 mutex_unlock(&tty->atomic_write_lock);
347 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
350 static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
352 struct tty_struct *tty;
354 tty = get_current_tty();
358 tty_write_msg(tty, prefix);
359 if ((mask & D_EMERG) || (mask & D_ERROR))
360 tty_write_msg(tty, "Error");
361 tty_write_msg(tty, ": ");
362 tty_write_msg(tty, msg);
366 static void cfs_vprint_to_console(struct ptldebug_header *hdr, int mask,
367 struct va_format *vaf, const char *file,
370 char *prefix = "Lustre";
372 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
375 if (mask & D_CONSOLE) {
377 pr_emerg("%sError: %pV", prefix, vaf);
378 else if (mask & D_ERROR)
379 pr_err("%sError: %pV", prefix, vaf);
380 else if (mask & D_WARNING)
381 pr_warn("%s: %pV", prefix, vaf);
382 else if (mask & libcfs_printk)
383 pr_info("%s: %pV", prefix, vaf);
386 pr_emerg("%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_ERROR)
390 pr_err("%sError: %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_WARNING)
394 pr_warn("%s: %d:%d:(%s:%d:%s()) %pV", prefix,
395 hdr->ph_pid, hdr->ph_extern_pid, file,
396 hdr->ph_line_num, fn, vaf);
397 else if (mask & (D_CONSOLE | libcfs_printk))
398 pr_info("%s: %pV", prefix, vaf);
402 /* tty_write_msg doesn't handle formatting */
403 cfs_tty_write_message(prefix, mask, vaf->fmt);
406 static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
407 const char *file, const char *fn,
408 const char *fmt, ...)
410 struct va_format vaf;
416 cfs_vprint_to_console(hdr, mask, &vaf, file, fn);
419 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
420 const char *format, ...)
422 struct cfs_trace_cpu_data *tcd = NULL;
423 struct ptldebug_header header = {0};
424 struct cfs_trace_page *tage;
425 /* string_buf is used only if tcd != NULL, and is always set then */
426 char *string_buf = NULL;
429 int needed = 85; /* seeded with average message length */
433 int mask = msgdata->msg_mask;
434 char *file = (char *)msgdata->msg_file;
435 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
437 if (strchr(file, '/'))
438 file = strrchr(file, '/') + 1;
440 tcd = cfs_trace_get_tcd();
442 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
443 * pins us to a particular CPU. This avoids an smp_processor_id()
444 * warning on Linux when debugging is enabled.
446 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
448 if (!tcd) /* arch may not log in IRQ context */
451 if (tcd->tcd_cur_pages == 0)
452 header.ph_flags |= PH_FLAG_FIRST_RECORD;
454 if (tcd->tcd_shutting_down) {
455 cfs_trace_put_tcd(tcd);
460 known_size = strlen(file) + 1;
462 known_size += strlen(msgdata->msg_fn) + 1;
464 if (libcfs_debug_binary)
465 known_size += sizeof(header);
468 * May perform an additional pass to update 'needed' and increase
469 * tage buffer size to match vsnprintf reported size required
470 * On the second pass (retry=1) use vscnprintf [which returns
471 * number of bytes written not including the terminating nul]
472 * to clarify `needed` is used as number of bytes written
473 * for the remainder of this function
475 for (retry = 0; retry < 2; retry++) {
476 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
478 if (needed + known_size > PAGE_SIZE)
481 cfs_trace_put_tcd(tcd);
486 string_buf = (char *)page_address(tage->page) +
487 tage->used + known_size;
489 max_nob = PAGE_SIZE - tage->used - known_size;
491 pr_emerg("LustreError: negative max_nob: %d\n",
494 cfs_trace_put_tcd(tcd);
499 va_start(ap, format);
501 needed = vscnprintf(string_buf, max_nob, format, ap);
503 needed = vsnprintf(string_buf, max_nob, format, ap);
506 if (needed < max_nob) /* well. printing ok.. */
510 /* `needed` is actual bytes written to string_buf */
511 if (*(string_buf + needed - 1) != '\n') {
512 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
513 file, msgdata->msg_line, msgdata->msg_fn);
514 } else if (mask & D_TTY) {
515 /* TTY needs '\r\n' to move carriage to leftmost position */
516 if (needed < 2 || *(string_buf + needed - 2) != '\r')
517 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
518 file, msgdata->msg_line, msgdata->msg_fn);
519 if (strnchr(string_buf, needed, '%'))
520 pr_info("Lustre: format at %s:%d:%s mustn't contain %%\n",
521 file, msgdata->msg_line, msgdata->msg_fn);
524 header.ph_len = known_size + needed;
525 debug_buf = (char *)page_address(tage->page) + tage->used;
527 if (libcfs_debug_binary) {
528 memcpy(debug_buf, &header, sizeof(header));
529 tage->used += sizeof(header);
530 debug_buf += sizeof(header);
533 strlcpy(debug_buf, file, PAGE_SIZE - tage->used);
534 tage->used += strlen(file) + 1;
535 debug_buf += strlen(file) + 1;
537 if (msgdata->msg_fn) {
538 strlcpy(debug_buf, msgdata->msg_fn, PAGE_SIZE - tage->used);
539 tage->used += strlen(msgdata->msg_fn) + 1;
540 debug_buf += strlen(msgdata->msg_fn) + 1;
543 __LASSERT(debug_buf == string_buf);
545 tage->used += needed;
546 __LASSERT(tage->used <= PAGE_SIZE);
549 if ((mask & libcfs_printk) == 0) {
550 /* no console output requested */
552 cfs_trace_put_tcd(tcd);
557 if (libcfs_console_ratelimit &&
558 cdls->cdls_next != 0 && /* not first time ever */
559 time_before(jiffies, cdls->cdls_next)) {
560 /* skipping a console message */
563 cfs_trace_put_tcd(tcd);
567 if (time_after(jiffies, cdls->cdls_next +
568 libcfs_console_max_delay +
569 cfs_time_seconds(10))) {
570 /* last timeout was a long time ago */
571 cdls->cdls_delay /= libcfs_console_backoff * 4;
573 cdls->cdls_delay *= libcfs_console_backoff;
576 if (cdls->cdls_delay < libcfs_console_min_delay)
577 cdls->cdls_delay = libcfs_console_min_delay;
578 else if (cdls->cdls_delay > libcfs_console_max_delay)
579 cdls->cdls_delay = libcfs_console_max_delay;
581 /* ensure cdls_next is never zero after it's been seen */
582 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
586 cfs_print_to_console(&header, mask, file, msgdata->msg_fn,
588 cfs_trace_put_tcd(tcd);
590 struct va_format vaf;
592 va_start(ap, format);
595 cfs_vprint_to_console(&header, mask,
596 &vaf, file, msgdata->msg_fn);
600 if (cdls != NULL && cdls->cdls_count != 0) {
601 /* Do not allow print this to TTY */
602 cfs_print_to_console(&header, mask & ~D_TTY, file,
604 "Skipped %d previous similar message%s\n",
606 (cdls->cdls_count > 1) ? "s" : "");
608 cdls->cdls_count = 0;
613 EXPORT_SYMBOL(libcfs_debug_msg);
616 cfs_trace_assertion_failed(const char *str,
617 struct libcfs_debug_msg_data *msgdata)
619 struct ptldebug_header hdr;
621 libcfs_panic_in_progress = 1;
622 libcfs_catastrophe = 1;
625 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
627 cfs_print_to_console(&hdr, D_EMERG, msgdata->msg_file, msgdata->msg_fn,
630 panic("Lustre debug assertion failure\n");
636 panic_collect_pages(struct page_collection *pc)
638 /* Do the collect_pages job on a single CPU: assumes that all other
639 * CPUs have been stopped during a panic. If this isn't true for some
640 * arch, this will have to be implemented separately in each arch. */
643 struct cfs_trace_cpu_data *tcd;
645 INIT_LIST_HEAD(&pc->pc_pages);
647 cfs_tcd_for_each(tcd, i, j) {
648 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
649 tcd->tcd_cur_pages = 0;
653 static void collect_pages_on_all_cpus(struct page_collection *pc)
655 struct cfs_trace_cpu_data *tcd;
658 for_each_possible_cpu(cpu) {
659 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
660 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
661 tcd->tcd_cur_pages = 0;
666 static void collect_pages(struct page_collection *pc)
668 INIT_LIST_HEAD(&pc->pc_pages);
670 if (libcfs_panic_in_progress)
671 panic_collect_pages(pc);
673 collect_pages_on_all_cpus(pc);
676 static void put_pages_back_on_all_cpus(struct page_collection *pc)
678 struct cfs_trace_cpu_data *tcd;
679 struct list_head *cur_head;
680 struct cfs_trace_page *tage;
681 struct cfs_trace_page *tmp;
684 for_each_possible_cpu(cpu) {
685 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
686 cur_head = tcd->tcd_pages.next;
688 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
691 __LASSERT_TAGE_INVARIANT(tage);
693 if (tage->cpu != cpu || tage->type != i)
696 cfs_tage_to_tail(tage, cur_head);
697 tcd->tcd_cur_pages++;
703 static void put_pages_back(struct page_collection *pc)
705 if (!libcfs_panic_in_progress)
706 put_pages_back_on_all_cpus(pc);
709 #ifdef LNET_DUMP_ON_PANIC
710 void cfs_trace_debug_print(void)
712 struct page_collection pc;
713 struct cfs_trace_page *tage;
714 struct cfs_trace_page *tmp;
718 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
721 __LASSERT_TAGE_INVARIANT(tage);
724 p = page_address(page);
725 while (p < ((char *)page_address(page) + tage->used)) {
726 struct ptldebug_header *hdr;
731 p += strlen(file) + 1;
734 len = hdr->ph_len - (int)(p - (char *)hdr);
736 cfs_print_to_console(hdr, D_EMERG, file, fn,
742 list_del(&tage->linkage);
745 down_write(&cfs_tracefile_sem);
746 while ((page = list_first_entry_or_null(&daemon_pages,
747 struct page, lru)) != NULL) {
750 p = page_address(page);
751 while (p < ((char *)page_address(page) + page->private)) {
752 struct ptldebug_header *hdr;
758 p += strlen(file) + 1;
761 len = hdr->ph_len - (int)(p - (char *)hdr);
763 cfs_print_to_console(hdr, D_EMERG, file, fn,
768 list_del_init(&page->lru);
769 daemon_pages_count -= 1;
772 up_write(&cfs_tracefile_sem);
774 #endif /* LNET_DUMP_ON_PANIC */
776 int cfs_tracefile_dump_all_pages(char *filename)
778 struct page_collection pc;
780 struct cfs_trace_page *tage;
781 struct cfs_trace_page *tmp;
786 down_write(&cfs_tracefile_sem);
788 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
792 pr_err("LustreError: can't open %s for dump: rc = %d\n",
798 if (list_empty(&pc.pc_pages)) {
803 /* ok, for now, just write the pages. in the future we'll be building
804 * iobufs with the pages and calling generic_direct_IO */
805 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
807 __LASSERT_TAGE_INVARIANT(tage);
809 buf = kmap(tage->page);
810 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
812 if (rc != (int)tage->used) {
813 pr_warn("Lustre: wanted to write %u but wrote %d\n",
816 __LASSERT(list_empty(&pc.pc_pages));
819 list_del(&tage->linkage);
822 while ((page = list_first_entry_or_null(&daemon_pages,
823 struct page, lru)) != NULL) {
824 buf = page_address(page);
825 rc = cfs_kernel_write(filp, buf, page->private, &filp->f_pos);
826 if (rc != (int)page->private) {
827 pr_warn("Lustre: wanted to write %u but wrote %d\n",
828 (int)page->private, rc);
831 list_del(&page->lru);
832 daemon_pages_count -= 1;
835 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
837 pr_err("LustreError: sync returns: rc = %d\n", rc);
839 filp_close(filp, NULL);
841 up_write(&cfs_tracefile_sem);
845 void cfs_trace_flush_pages(void)
847 struct page_collection pc;
848 struct cfs_trace_page *tage;
852 while (!list_empty(&pc.pc_pages)) {
853 tage = list_first_entry(&pc.pc_pages,
854 struct cfs_trace_page, linkage);
855 __LASSERT_TAGE_INVARIANT(tage);
857 list_del(&tage->linkage);
861 down_write(&cfs_tracefile_sem);
862 while ((page = list_first_entry_or_null(&daemon_pages,
863 struct page, lru)) != NULL) {
864 list_del(&page->lru);
865 daemon_pages_count -= 1;
868 up_write(&cfs_tracefile_sem);
871 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
872 const char *knl_buffer, char *append)
874 /* NB if 'append' != NULL, it's a single character to append to the
875 * copied out string - usually "\n", for /proc entries and "" (i.e. a
876 * terminating zero byte) for sysctl entries */
877 int nob = strlen(knl_buffer);
879 if (nob > usr_buffer_nob)
880 nob = usr_buffer_nob;
882 if (copy_to_user(usr_buffer, knl_buffer, nob))
885 if (append != NULL && nob < usr_buffer_nob) {
886 if (copy_to_user(usr_buffer + nob, append, 1))
894 EXPORT_SYMBOL(cfs_trace_copyout_string);
896 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
902 str = memdup_user_nul(usr_str, usr_str_nob);
910 rc = cfs_tracefile_dump_all_pages(path);
916 int cfs_trace_daemon_command(char *str)
920 down_write(&cfs_tracefile_sem);
922 if (strcmp(str, "stop") == 0) {
923 up_write(&cfs_tracefile_sem);
924 cfs_trace_stop_thread();
925 down_write(&cfs_tracefile_sem);
926 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
928 } else if (strncmp(str, "size=", 5) == 0) {
931 rc = kstrtoul(str + 5, 10, &tmp);
933 if (tmp < 10 || tmp > 20480)
934 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
936 cfs_tracefile_size = tmp << 20;
938 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
940 } else if (str[0] != '/') {
943 strcpy(cfs_tracefile, str);
945 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
946 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
948 cfs_trace_start_thread();
951 up_write(&cfs_tracefile_sem);
955 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
960 str = memdup_user_nul(usr_str, usr_str_nob);
964 rc = cfs_trace_daemon_command(strim(str));
970 int cfs_trace_set_debug_mb(int mb)
975 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
976 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
977 struct cfs_trace_cpu_data *tcd;
979 if (mb < num_possible_cpus()) {
980 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
981 mb, num_possible_cpus());
982 mb = num_possible_cpus();
986 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
991 mb /= num_possible_cpus();
992 pages = mb << (20 - PAGE_SHIFT);
994 down_write(&cfs_tracefile_sem);
996 cfs_tcd_for_each(tcd, i, j)
997 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
999 daemon_pages_max = pages;
1000 up_write(&cfs_tracefile_sem);
1005 int cfs_trace_get_debug_mb(void)
1009 struct cfs_trace_cpu_data *tcd;
1010 int total_pages = 0;
1012 down_read(&cfs_tracefile_sem);
1014 cfs_tcd_for_each(tcd, i, j)
1015 total_pages += tcd->tcd_max_pages;
1017 up_read(&cfs_tracefile_sem);
1019 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1022 static int tracefiled(void *arg)
1024 struct page_collection pc;
1025 struct cfs_trace_page *tage;
1026 struct cfs_trace_page *tmp;
1032 while (!last_loop) {
1033 LIST_HEAD(for_daemon_pages);
1034 int for_daemon_pages_count = 0;
1035 schedule_timeout_interruptible(cfs_time_seconds(1));
1036 if (kthread_should_stop())
1039 if (list_empty(&pc.pc_pages))
1043 down_read(&cfs_tracefile_sem);
1044 if (cfs_tracefile[0] != 0) {
1045 filp = filp_open(cfs_tracefile,
1046 O_CREAT | O_RDWR | O_LARGEFILE,
1051 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1055 up_read(&cfs_tracefile_sem);
1057 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1058 __LASSERT_TAGE_INVARIANT(tage);
1061 struct dentry *de = file_dentry(filp);
1062 static loff_t f_pos;
1064 if (f_pos >= (off_t)cfs_tracefile_size)
1066 else if (f_pos > i_size_read(de->d_inode))
1067 f_pos = i_size_read(de->d_inode);
1069 buf = kmap(tage->page);
1070 rc = cfs_kernel_write(filp, buf, tage->used,
1073 if (rc != (int)tage->used) {
1074 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1076 put_pages_back(&pc);
1077 __LASSERT(list_empty(&pc.pc_pages));
1081 list_del_init(&tage->linkage);
1082 list_add_tail(&tage->page->lru, &for_daemon_pages);
1083 for_daemon_pages_count += 1;
1085 tage->page->private = (int)tage->used;
1087 atomic_dec(&cfs_tage_allocated);
1091 filp_close(filp, NULL);
1093 down_write(&cfs_tracefile_sem);
1094 list_splice_tail(&for_daemon_pages, &daemon_pages);
1095 daemon_pages_count += for_daemon_pages_count;
1096 while (daemon_pages_count > daemon_pages_max) {
1097 struct page *p = list_first_entry(&daemon_pages,
1101 daemon_pages_count -= 1;
1103 up_write(&cfs_tracefile_sem);
1105 if (!list_empty(&pc.pc_pages)) {
1108 pr_alert("Lustre: trace pages aren't empty\n");
1109 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1110 for (i = 0; i < num_possible_cpus(); i++)
1112 pr_cont("%d(on) ", i);
1114 pr_cont("%d(off) ", i);
1118 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1120 pr_err("Lustre: page %d belongs to cpu %d\n",
1122 pr_err("Lustre: There are %d pages unwritten\n", i);
1124 __LASSERT(list_empty(&pc.pc_pages));
1130 int cfs_trace_start_thread(void)
1132 struct task_struct *tsk;
1138 tsk = kthread_create(tracefiled, NULL, "ktracefiled");
1141 else if (cmpxchg(&tctl_task, NULL, tsk) != NULL)
1142 /* already running */
1145 wake_up_process(tsk);
1150 void cfs_trace_stop_thread(void)
1152 struct task_struct *tsk;
1154 tsk = xchg(&tctl_task, NULL);
1156 pr_info("Lustre: shutting down debug daemon thread...\n");
1161 /* percents to share the total debug memory for each type */
1162 static unsigned int pages_factor[CFS_TCD_TYPE_CNT] = {
1163 80, /* 80% pages for CFS_TCD_TYPE_PROC */
1164 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
1165 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
1168 int cfs_tracefile_init(int max_pages)
1170 struct cfs_trace_cpu_data *tcd;
1174 /* initialize trace_data */
1175 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
1176 for (i = 0; i < CFS_TCD_TYPE_CNT; i++) {
1178 kmalloc_array(num_possible_cpus(),
1179 sizeof(union cfs_trace_data_union),
1181 if (!cfs_trace_data[i])
1182 goto out_trace_data;
1185 /* arch related info initialized */
1186 cfs_tcd_for_each(tcd, i, j) {
1187 int factor = pages_factor[i];
1189 spin_lock_init(&tcd->tcd_lock);
1190 tcd->tcd_pages_factor = factor;
1194 INIT_LIST_HEAD(&tcd->tcd_pages);
1195 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1196 tcd->tcd_cur_pages = 0;
1197 tcd->tcd_cur_stock_pages = 0;
1198 tcd->tcd_max_pages = (max_pages * factor) / 100;
1199 LASSERT(tcd->tcd_max_pages > 0);
1200 tcd->tcd_shutting_down = 0;
1202 daemon_pages_max = max_pages;
1207 for (i = 0; cfs_trace_data[i]; i++) {
1208 kfree(cfs_trace_data[i]);
1209 cfs_trace_data[i] = NULL;
1211 pr_err("lnet: Not enough memory\n");
1215 static void trace_cleanup_on_all_cpus(void)
1217 struct cfs_trace_cpu_data *tcd;
1218 struct cfs_trace_page *tage;
1221 for_each_possible_cpu(cpu) {
1222 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1223 if (!tcd->tcd_pages_factor)
1224 /* Not initialised */
1226 tcd->tcd_shutting_down = 1;
1228 while (!list_empty(&tcd->tcd_pages)) {
1229 tage = list_first_entry(&tcd->tcd_pages,
1230 struct cfs_trace_page,
1232 __LASSERT_TAGE_INVARIANT(tage);
1234 list_del(&tage->linkage);
1235 cfs_tage_free(tage);
1237 tcd->tcd_cur_pages = 0;
1242 static void cfs_trace_cleanup(void)
1244 struct page_collection pc;
1247 INIT_LIST_HEAD(&pc.pc_pages);
1249 trace_cleanup_on_all_cpus();
1251 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) {
1252 kfree(cfs_trace_data[i]);
1253 cfs_trace_data[i] = NULL;
1257 void cfs_tracefile_exit(void)
1259 cfs_trace_stop_thread();
1260 cfs_trace_flush_pages();
1261 cfs_trace_cleanup();