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/uaccess.h>
46 #include <libcfs/linux/linux-fs.h>
47 #include <libcfs/libcfs.h>
50 enum cfs_trace_buf_type {
51 CFS_TCD_TYPE_PROC = 0,
57 union cfs_trace_data_union (*cfs_trace_data[CFS_TCD_TYPE_CNT])[NR_CPUS] __cacheline_aligned;
59 /* Pages containing records already processed by daemon.
60 * Link via ->lru, use size in ->private
62 static LIST_HEAD(daemon_pages);
63 static long daemon_pages_count;
64 static long daemon_pages_max;
66 char cfs_tracefile[TRACEFILE_NAME_SIZE];
67 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
69 struct task_struct *tctl_task;
71 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
72 static DECLARE_RWSEM(cfs_tracefile_sem);
74 /* trace file lock routines */
75 /* The walking argument indicates the locking comes from all tcd types
76 * iterator and we must lock it and dissable local irqs to avoid deadlocks
77 * with other interrupt locks that might be happening. See LU-1311
80 static int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
81 __acquires(&tcd->tcd_lock)
83 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
84 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
85 spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
86 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
87 spin_lock_bh(&tcd->tcd_lock);
88 else if (unlikely(walking))
89 spin_lock_irq(&tcd->tcd_lock);
91 spin_lock(&tcd->tcd_lock);
95 static void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
96 __releases(&tcd->tcd_lock)
98 __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
99 if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
100 spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
101 else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
102 spin_unlock_bh(&tcd->tcd_lock);
103 else if (unlikely(walking))
104 spin_unlock_irq(&tcd->tcd_lock);
106 spin_unlock(&tcd->tcd_lock);
109 #define cfs_tcd_for_each(tcd, i, j) \
110 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) \
111 for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd); \
112 j < num_possible_cpus(); \
113 j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
115 #define cfs_tcd_for_each_type_lock(tcd, i, cpu) \
116 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i] && \
117 (tcd = &(*cfs_trace_data[i])[cpu].tcd) && \
118 cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
120 static enum cfs_trace_buf_type cfs_trace_buf_idx_get(void)
123 return CFS_TCD_TYPE_IRQ;
125 return CFS_TCD_TYPE_SOFTIRQ;
126 return CFS_TCD_TYPE_PROC;
129 static inline struct cfs_trace_cpu_data *
130 cfs_trace_get_tcd(void)
132 struct cfs_trace_cpu_data *tcd =
133 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[get_cpu()].tcd;
135 cfs_trace_lock_tcd(tcd, 0);
140 static inline void cfs_trace_put_tcd(struct cfs_trace_cpu_data *tcd)
142 cfs_trace_unlock_tcd(tcd, 0);
147 static inline struct cfs_trace_page *
148 cfs_tage_from_list(struct list_head *list)
150 return list_entry(list, struct cfs_trace_page, linkage);
153 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
156 struct cfs_trace_page *tage;
158 /* My caller is trying to free memory */
159 if (!in_interrupt() && (current->flags & PF_MEMALLOC))
163 * Don't spam console with allocation failures: they will be reported
164 * by upper layer anyway.
167 page = alloc_page(gfp);
171 tage = kmalloc(sizeof(*tage), gfp);
178 atomic_inc(&cfs_tage_allocated);
182 static void cfs_tage_free(struct cfs_trace_page *tage)
184 __LASSERT(tage != NULL);
185 __LASSERT(tage->page != NULL);
187 __free_page(tage->page);
189 atomic_dec(&cfs_tage_allocated);
192 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
193 struct list_head *queue)
195 __LASSERT(tage != NULL);
196 __LASSERT(queue != NULL);
198 list_move_tail(&tage->linkage, queue);
201 /* return a page that has 'len' bytes left at the end */
202 static struct cfs_trace_page *
203 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
205 struct cfs_trace_page *tage;
206 struct task_struct *tsk;
208 if (tcd->tcd_cur_pages > 0) {
209 __LASSERT(!list_empty(&tcd->tcd_pages));
210 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
211 if (tage->used + len <= PAGE_SIZE)
215 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
216 if (tcd->tcd_cur_stock_pages > 0) {
217 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
218 --tcd->tcd_cur_stock_pages;
219 list_del_init(&tage->linkage);
221 tage = cfs_tage_alloc(GFP_ATOMIC);
222 if (unlikely(tage == NULL)) {
223 if ((!(current->flags & PF_MEMALLOC) ||
224 in_interrupt()) && printk_ratelimit())
225 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
232 tage->cpu = smp_processor_id();
233 tage->type = tcd->tcd_type;
234 list_add_tail(&tage->linkage, &tcd->tcd_pages);
235 tcd->tcd_cur_pages++;
238 if (tcd->tcd_cur_pages > 8 && tsk)
240 * wake up tracefiled to process some pages.
242 wake_up_process(tsk);
249 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
251 int pgcount = tcd->tcd_cur_pages / 10;
252 struct page_collection pc;
253 struct cfs_trace_page *tage;
254 struct cfs_trace_page *tmp;
257 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
258 * from here: this will lead to infinite recursion.
261 if (printk_ratelimit())
262 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
263 pgcount + 1, tcd->tcd_cur_pages);
265 INIT_LIST_HEAD(&pc.pc_pages);
267 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
271 list_del(&tage->linkage);
273 tcd->tcd_cur_pages--;
277 /* return a page that has 'len' bytes left at the end */
278 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
281 struct cfs_trace_page *tage;
284 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
285 * from here: this will lead to infinite recursion.
288 if (len > PAGE_SIZE) {
289 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
294 tage = cfs_trace_get_tage_try(tcd, len);
299 if (tcd->tcd_cur_pages > 0) {
300 tage = cfs_tage_from_list(tcd->tcd_pages.next);
302 cfs_tage_to_tail(tage, &tcd->tcd_pages);
307 static void cfs_set_ptldebug_header(struct ptldebug_header *header,
308 struct libcfs_debug_msg_data *msgdata)
310 struct timespec64 ts;
312 ktime_get_real_ts64(&ts);
314 header->ph_subsys = msgdata->msg_subsys;
315 header->ph_mask = msgdata->msg_mask;
316 header->ph_cpu_id = smp_processor_id();
317 header->ph_type = cfs_trace_buf_idx_get();
318 /* y2038 safe since all user space treats this as unsigned, but
319 * will overflow in 2106
321 header->ph_sec = (u32)ts.tv_sec;
322 header->ph_usec = ts.tv_nsec / NSEC_PER_USEC;
323 header->ph_pid = current->pid;
324 header->ph_line_num = msgdata->msg_line;
325 header->ph_extern_pid = 0;
328 static void cfs_vprint_to_console(struct ptldebug_header *hdr,
329 struct va_format *vaf, const char *file,
332 char *prefix = "Lustre";
333 int mask = hdr->ph_mask;
335 if (hdr->ph_subsys == S_LND || hdr->ph_subsys == S_LNET)
338 if (mask & D_CONSOLE) {
340 pr_emerg("%sError: %pV", prefix, vaf);
341 else if (mask & D_ERROR)
342 pr_err("%sError: %pV", prefix, vaf);
343 else if (mask & D_WARNING)
344 pr_warn("%s: %pV", prefix, vaf);
345 else if (mask & libcfs_printk)
346 pr_info("%s: %pV", prefix, vaf);
349 pr_emerg("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
350 hdr->ph_pid, hdr->ph_extern_pid, file,
351 hdr->ph_line_num, fn, vaf);
352 else if (mask & D_ERROR)
353 pr_err("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
354 hdr->ph_pid, hdr->ph_extern_pid, file,
355 hdr->ph_line_num, fn, vaf);
356 else if (mask & D_WARNING)
357 pr_warn("%s: %d:%d:(%s:%d:%s()) %pV", prefix,
358 hdr->ph_pid, hdr->ph_extern_pid, file,
359 hdr->ph_line_num, fn, vaf);
360 else if (mask & (D_CONSOLE | libcfs_printk))
361 pr_info("%s: %pV", prefix, vaf);
365 static void cfs_print_to_console(struct ptldebug_header *hdr, const char *file,
366 const char *fn, const char *fmt, ...)
368 struct va_format vaf;
374 cfs_vprint_to_console(hdr, &vaf, file, fn);
377 #define DEBUG_FORMAT_BUFFER_SIZE (256 - sizeof(unsigned long))
379 struct debug_format_buffer {
380 unsigned long dfb_flags;
381 char dfb_buf[DEBUG_FORMAT_BUFFER_SIZE];
383 struct pcpu_format_pool {
384 struct debug_format_buffer pf_dfb[4096 / DEBUG_FORMAT_BUFFER_SIZE];
391 static struct pcpu_format_pool __percpu *debug_format_pool;
393 int debug_format_buffer_alloc_buffers(void)
395 struct pcpu_format_pool __percpu *obj;
396 struct pcpu_format_pool *pbuf;
399 obj = alloc_percpu(struct pcpu_format_pool);
402 for_each_possible_cpu(cpu) {
403 pbuf = per_cpu_ptr(obj, cpu);
404 memset(pbuf, 0, sizeof(*pbuf));
406 debug_format_pool = obj;
410 EXPORT_SYMBOL(debug_format_buffer_alloc_buffers);
412 void debug_format_buffer_free_buffers(void)
414 struct pcpu_format_pool __percpu *tmp = debug_format_pool;
415 struct pcpu_format_pool *pbuf;
419 if (!debug_format_pool)
422 debug_format_pool = NULL;
425 for_each_possible_cpu(cpu) {
426 pbuf = per_cpu_ptr(tmp, cpu);
427 for (i = 0; i < ARRAY_SIZE(pbuf->pf_dfb); i++)
428 set_bit(PF_INUSE, &pbuf->pf_dfb[i].dfb_flags);
432 EXPORT_SYMBOL(debug_format_buffer_free_buffers);
434 bool libcfs_debug_raw_pointers;
436 bool get_debug_raw_pointers(void)
438 return libcfs_debug_raw_pointers;
440 EXPORT_SYMBOL(get_debug_raw_pointers);
442 void set_debug_raw_pointers(bool value)
444 libcfs_debug_raw_pointers = value;
446 EXPORT_SYMBOL(set_debug_raw_pointers);
449 #define raw_cpu_ptr(p) this_cpu_ptr(p)
452 static struct debug_format_buffer *debug_format_buffer_get_locked(void)
454 struct debug_format_buffer *dfb = NULL;
455 struct pcpu_format_pool *pbuf;
458 if (!debug_format_pool)
461 pbuf = raw_cpu_ptr(debug_format_pool);
462 for (i = 0; i < ARRAY_SIZE(pbuf->pf_dfb); i++) {
463 if (!test_and_set_bit(PF_INUSE, &pbuf->pf_dfb[i].dfb_flags)) {
464 dfb = &pbuf->pf_dfb[i];
471 static void debug_format_buffer_put_locked(struct debug_format_buffer *dfb)
473 if (!debug_format_pool || !dfb)
475 clear_bit(PF_INUSE, &dfb->dfb_flags);
478 /* return number of %p to %px replacements or < 0 on error */
479 static bool rewrite_format(const char *fmt, size_t nfsz, char *new_fmt)
486 if (WARN_ON_ONCE(!fmt))
492 if (written + 2 >= nfsz)
498 /* Replace %p with %px */
501 if (written + 2 >= nfsz)
505 } else if (p[0] == 'p' && !isalnum(p[1])) {
506 if (written + 3 >= nfsz)
521 * @fmt: caller provided format string
522 * @m: if non-null points to per-cpu object
524 * return result is format string to use, it will be either:
525 * fmt: when no changes need to be made to original format
526 * *m->dfb_buf: when percpu pre-allocated is sufficient to hold updated format
528 static inline const char *debug_format(const char *fmt,
529 struct debug_format_buffer **m)
531 struct debug_format_buffer *dfb_fmt;
534 if (likely(!libcfs_debug_raw_pointers))
536 if (!strstr(fmt, "%p"))
539 /* try to rewrite format into buf */
540 dfb_fmt = debug_format_buffer_get_locked();
542 size_t len = sizeof(dfb_fmt->dfb_buf) - 1;
544 if (rewrite_format(fmt, len, dfb_fmt->dfb_buf)) {
546 return dfb_fmt->dfb_buf;
548 debug_format_buffer_put_locked(dfb_fmt);
554 void libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
555 const char *format, ...)
557 struct cfs_trace_cpu_data *tcd = NULL;
558 struct ptldebug_header header = {0};
559 struct cfs_trace_page *tage;
560 /* string_buf is used only if tcd != NULL, and is always set then */
561 char *string_buf = NULL;
564 int needed = 85; /* seeded with average message length */
568 const char *file = msgdata->msg_file;
569 const char *fn = msgdata->msg_fn;
570 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
571 struct debug_format_buffer *dfb = NULL;
573 format = debug_format(format, &dfb);
575 if (strchr(file, '/'))
576 file = strrchr(file, '/') + 1;
578 tcd = cfs_trace_get_tcd();
580 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
581 * pins us to a particular CPU. This avoids an smp_processor_id()
582 * warning on Linux when debugging is enabled.
584 cfs_set_ptldebug_header(&header, msgdata);
586 if (!tcd) /* arch may not log in IRQ context */
589 if (tcd->tcd_cur_pages == 0)
590 header.ph_flags |= PH_FLAG_FIRST_RECORD;
592 if (tcd->tcd_shutting_down) {
593 cfs_trace_put_tcd(tcd);
598 known_size = strlen(file) + 1;
600 known_size += strlen(fn) + 1;
602 if (libcfs_debug_binary)
603 known_size += sizeof(header);
606 * May perform an additional pass to update 'needed' and increase
607 * tage buffer size to match vsnprintf reported size required
608 * On the second pass (retry=1) use vscnprintf [which returns
609 * number of bytes written not including the terminating nul]
610 * to clarify `needed` is used as number of bytes written
611 * for the remainder of this function
613 for (retry = 0; retry < 2; retry++) {
614 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
616 if (needed + known_size > PAGE_SIZE)
617 header.ph_mask |= D_ERROR;
619 cfs_trace_put_tcd(tcd);
624 string_buf = (char *)page_address(tage->page) +
625 tage->used + known_size;
627 max_nob = PAGE_SIZE - tage->used - known_size;
629 pr_emerg("LustreError: negative max_nob: %d\n",
631 header.ph_mask |= D_ERROR;
632 cfs_trace_put_tcd(tcd);
637 va_start(ap, format);
639 needed = vscnprintf(string_buf, max_nob, format, ap);
641 needed = vsnprintf(string_buf, max_nob, format, ap);
644 if (needed < max_nob) /* well. printing ok.. */
648 /* `needed` is actual bytes written to string_buf */
649 if (unlikely(*(string_buf + needed - 1) != '\n')) {
650 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
651 file, header.ph_line_num, fn);
654 header.ph_len = known_size + needed;
655 debug_buf = (char *)page_address(tage->page) + tage->used;
657 if (libcfs_debug_binary) {
658 memcpy(debug_buf, &header, sizeof(header));
659 tage->used += sizeof(header);
660 debug_buf += sizeof(header);
663 snprintf(debug_buf, PAGE_SIZE - tage->used, "%s", file);
664 tage->used += strlen(file) + 1;
665 debug_buf += strlen(file) + 1;
668 snprintf(debug_buf, PAGE_SIZE - tage->used, "%s", fn);
669 tage->used += strlen(fn) + 1;
670 debug_buf += strlen(fn) + 1;
673 __LASSERT(debug_buf == string_buf);
675 tage->used += needed;
676 __LASSERT(tage->used <= PAGE_SIZE);
679 if ((header.ph_mask & libcfs_printk) == 0) {
680 /* no console output requested */
682 cfs_trace_put_tcd(tcd);
687 if (libcfs_console_ratelimit &&
688 cdls->cdls_next != 0 && /* not first time ever */
689 time_before(jiffies, cdls->cdls_next)) {
690 /* skipping a console message */
693 cfs_trace_put_tcd(tcd);
697 if (time_after(jiffies, cdls->cdls_next +
698 libcfs_console_max_delay +
699 cfs_time_seconds(10))) {
700 /* last timeout was a long time ago */
701 cdls->cdls_delay /= libcfs_console_backoff * 4;
703 cdls->cdls_delay *= libcfs_console_backoff;
706 if (cdls->cdls_delay < libcfs_console_min_delay)
707 cdls->cdls_delay = libcfs_console_min_delay;
708 else if (cdls->cdls_delay > libcfs_console_max_delay)
709 cdls->cdls_delay = libcfs_console_max_delay;
711 /* ensure cdls_next is never zero after it's been seen */
712 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
716 cfs_print_to_console(&header, file, fn, "%s", string_buf);
717 cfs_trace_put_tcd(tcd);
719 struct va_format vaf;
721 va_start(ap, format);
724 cfs_vprint_to_console(&header, &vaf, file, fn);
728 if (cdls != NULL && cdls->cdls_count != 0) {
729 cfs_print_to_console(&header, file, fn,
730 "Skipped %d previous similar message%s\n",
732 (cdls->cdls_count > 1) ? "s" : "");
734 cdls->cdls_count = 0;
737 debug_format_buffer_put_locked(dfb);
739 EXPORT_SYMBOL(libcfs_debug_msg);
742 cfs_trace_assertion_failed(const char *str,
743 struct libcfs_debug_msg_data *msgdata)
745 struct ptldebug_header hdr;
747 libcfs_panic_in_progress = 1;
748 libcfs_catastrophe = 1;
751 cfs_set_ptldebug_header(&hdr, msgdata);
752 hdr.ph_mask = D_EMERG;
754 cfs_print_to_console(&hdr, msgdata->msg_file, msgdata->msg_fn,
757 panic("Lustre debug assertion failure\n");
763 panic_collect_pages(struct page_collection *pc)
765 /* Do the collect_pages job on a single CPU: assumes that all other
766 * CPUs have been stopped during a panic. If this isn't true for some
767 * arch, this will have to be implemented separately in each arch. */
770 struct cfs_trace_cpu_data *tcd;
772 INIT_LIST_HEAD(&pc->pc_pages);
774 cfs_tcd_for_each(tcd, i, j) {
775 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
776 tcd->tcd_cur_pages = 0;
780 static void collect_pages_on_all_cpus(struct page_collection *pc)
782 struct cfs_trace_cpu_data *tcd;
785 for_each_possible_cpu(cpu) {
786 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
787 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
788 tcd->tcd_cur_pages = 0;
793 static void collect_pages(struct page_collection *pc)
795 INIT_LIST_HEAD(&pc->pc_pages);
797 if (libcfs_panic_in_progress)
798 panic_collect_pages(pc);
800 collect_pages_on_all_cpus(pc);
803 static void put_pages_back_on_all_cpus(struct page_collection *pc)
805 struct cfs_trace_cpu_data *tcd;
806 struct list_head *cur_head;
807 struct cfs_trace_page *tage;
808 struct cfs_trace_page *tmp;
811 for_each_possible_cpu(cpu) {
812 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
813 cur_head = tcd->tcd_pages.next;
815 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
818 __LASSERT_TAGE_INVARIANT(tage);
820 if (tage->cpu != cpu || tage->type != i)
823 cfs_tage_to_tail(tage, cur_head);
824 tcd->tcd_cur_pages++;
830 static void put_pages_back(struct page_collection *pc)
832 if (!libcfs_panic_in_progress)
833 put_pages_back_on_all_cpus(pc);
836 #ifdef LNET_DUMP_ON_PANIC
837 void cfs_trace_debug_print(void)
839 struct page_collection pc;
840 struct cfs_trace_page *tage;
841 struct cfs_trace_page *tmp;
845 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
848 __LASSERT_TAGE_INVARIANT(tage);
851 p = page_address(page);
852 while (p < ((char *)page_address(page) + tage->used)) {
853 struct ptldebug_header *hdr;
859 p += strlen(file) + 1;
862 len = hdr->ph_len - (int)(p - (char *)hdr);
864 cfs_print_to_console(hdr, D_EMERG, file, fn,
870 list_del(&tage->linkage);
873 down_write(&cfs_tracefile_sem);
874 while ((page = list_first_entry_or_null(&daemon_pages,
875 struct page, lru)) != NULL) {
878 p = page_address(page);
879 while (p < ((char *)page_address(page) + page->private)) {
880 struct ptldebug_header *hdr;
886 p += strlen(file) + 1;
889 len = hdr->ph_len - (int)(p - (char *)hdr);
891 cfs_print_to_console(hdr, D_EMERG, file, fn,
896 list_del_init(&page->lru);
897 daemon_pages_count -= 1;
900 up_write(&cfs_tracefile_sem);
902 #endif /* LNET_DUMP_ON_PANIC */
904 int cfs_tracefile_dump_all_pages(char *filename)
906 struct page_collection pc;
908 struct cfs_trace_page *tage;
909 struct cfs_trace_page *tmp;
914 down_write(&cfs_tracefile_sem);
916 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
920 pr_err("LustreError: can't open %s for dump: rc = %d\n",
926 if (list_empty(&pc.pc_pages)) {
931 /* ok, for now, just write the pages. in the future we'll be building
932 * iobufs with the pages and calling generic_direct_IO */
933 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
935 __LASSERT_TAGE_INVARIANT(tage);
937 buf = kmap(tage->page);
938 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
940 if (rc != (int)tage->used) {
941 pr_warn("Lustre: wanted to write %u but wrote %d\n",
944 __LASSERT(list_empty(&pc.pc_pages));
947 list_del(&tage->linkage);
950 while ((page = list_first_entry_or_null(&daemon_pages,
951 struct page, lru)) != NULL) {
952 buf = page_address(page);
953 rc = cfs_kernel_write(filp, buf, page->private, &filp->f_pos);
954 if (rc != (int)page->private) {
955 pr_warn("Lustre: wanted to write %u but wrote %d\n",
956 (int)page->private, rc);
959 list_del(&page->lru);
960 daemon_pages_count -= 1;
963 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
965 pr_err("LustreError: sync returns: rc = %d\n", rc);
967 filp_close(filp, NULL);
969 up_write(&cfs_tracefile_sem);
973 void cfs_trace_flush_pages(void)
975 struct page_collection pc;
976 struct cfs_trace_page *tage;
980 while (!list_empty(&pc.pc_pages)) {
981 tage = list_first_entry(&pc.pc_pages,
982 struct cfs_trace_page, linkage);
983 __LASSERT_TAGE_INVARIANT(tage);
985 list_del(&tage->linkage);
989 down_write(&cfs_tracefile_sem);
990 while ((page = list_first_entry_or_null(&daemon_pages,
991 struct page, lru)) != NULL) {
992 list_del(&page->lru);
993 daemon_pages_count -= 1;
996 up_write(&cfs_tracefile_sem);
999 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
1000 const char *knl_buffer, char *append)
1002 /* NB if 'append' != NULL, it's a single character to append to the
1003 * copied out string - usually "\n", for /proc entries and "" (i.e. a
1004 * terminating zero byte) for sysctl entries */
1005 int nob = strlen(knl_buffer);
1007 if (nob > usr_buffer_nob)
1008 nob = usr_buffer_nob;
1010 if (copy_to_user(usr_buffer, knl_buffer, nob))
1013 if (append != NULL && nob < usr_buffer_nob) {
1014 if (copy_to_user(usr_buffer + nob, append, 1))
1022 EXPORT_SYMBOL(cfs_trace_copyout_string);
1024 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
1030 str = memdup_user_nul(usr_str, usr_str_nob);
1032 return PTR_ERR(str);
1038 rc = cfs_tracefile_dump_all_pages(path);
1044 int cfs_trace_daemon_command(char *str)
1048 down_write(&cfs_tracefile_sem);
1050 if (strcmp(str, "stop") == 0) {
1051 up_write(&cfs_tracefile_sem);
1052 cfs_trace_stop_thread();
1053 down_write(&cfs_tracefile_sem);
1054 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
1056 } else if (strncmp(str, "size=", 5) == 0) {
1059 rc = kstrtoul(str + 5, 10, &tmp);
1061 if (tmp < 10 || tmp > 20480)
1062 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
1064 cfs_tracefile_size = tmp << 20;
1066 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
1068 } else if (str[0] != '/') {
1071 strcpy(cfs_tracefile, str);
1073 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
1074 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
1076 cfs_trace_start_thread();
1079 up_write(&cfs_tracefile_sem);
1083 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
1088 str = memdup_user_nul(usr_str, usr_str_nob);
1090 return PTR_ERR(str);
1092 rc = cfs_trace_daemon_command(strim(str));
1098 int cfs_trace_set_debug_mb(int mb)
1102 unsigned long pages;
1103 unsigned long total_mb = (cfs_totalram_pages() >> (20 - PAGE_SHIFT));
1104 unsigned long limit = max_t(unsigned long, 512, (total_mb * 4) / 5);
1105 struct cfs_trace_cpu_data *tcd;
1107 if (mb < num_possible_cpus()) {
1108 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
1109 mb, num_possible_cpus());
1110 mb = num_possible_cpus();
1114 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %lu MB.\n",
1119 mb /= num_possible_cpus();
1120 pages = mb << (20 - PAGE_SHIFT);
1122 down_write(&cfs_tracefile_sem);
1124 cfs_tcd_for_each(tcd, i, j)
1125 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
1127 daemon_pages_max = pages;
1128 up_write(&cfs_tracefile_sem);
1133 int cfs_trace_get_debug_mb(void)
1137 struct cfs_trace_cpu_data *tcd;
1138 int total_pages = 0;
1140 down_read(&cfs_tracefile_sem);
1142 cfs_tcd_for_each(tcd, i, j)
1143 total_pages += tcd->tcd_max_pages;
1145 up_read(&cfs_tracefile_sem);
1148 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
1153 static int tracefiled(void *arg)
1155 struct page_collection pc;
1156 struct cfs_trace_page *tage;
1157 struct cfs_trace_page *tmp;
1163 while (!last_loop) {
1164 LIST_HEAD(for_daemon_pages);
1165 int for_daemon_pages_count = 0;
1166 schedule_timeout_interruptible(cfs_time_seconds(1));
1167 if (kthread_should_stop())
1170 if (list_empty(&pc.pc_pages))
1174 down_read(&cfs_tracefile_sem);
1175 if (cfs_tracefile[0] != 0) {
1176 filp = filp_open(cfs_tracefile,
1177 O_CREAT | O_RDWR | O_LARGEFILE,
1182 pr_warn("Lustre: couldn't open %s: rc = %d\n",
1186 up_read(&cfs_tracefile_sem);
1188 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1189 __LASSERT_TAGE_INVARIANT(tage);
1192 struct dentry *de = file_dentry(filp);
1193 static loff_t f_pos;
1195 if (f_pos >= (off_t)cfs_tracefile_size)
1197 else if (f_pos > i_size_read(de->d_inode))
1198 f_pos = i_size_read(de->d_inode);
1200 buf = kmap(tage->page);
1201 rc = cfs_kernel_write(filp, buf, tage->used,
1204 if (rc != (int)tage->used) {
1205 pr_warn("Lustre: wanted to write %u but wrote %d\n",
1207 put_pages_back(&pc);
1208 __LASSERT(list_empty(&pc.pc_pages));
1212 list_del_init(&tage->linkage);
1213 list_add_tail(&tage->page->lru, &for_daemon_pages);
1214 for_daemon_pages_count += 1;
1216 tage->page->private = (int)tage->used;
1218 atomic_dec(&cfs_tage_allocated);
1222 filp_close(filp, NULL);
1224 down_write(&cfs_tracefile_sem);
1225 list_splice_tail(&for_daemon_pages, &daemon_pages);
1226 daemon_pages_count += for_daemon_pages_count;
1227 while (daemon_pages_count > daemon_pages_max) {
1228 struct page *p = list_first_entry(&daemon_pages,
1232 daemon_pages_count -= 1;
1234 up_write(&cfs_tracefile_sem);
1236 if (!list_empty(&pc.pc_pages)) {
1239 pr_alert("Lustre: trace pages aren't empty\n");
1240 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
1241 for (i = 0; i < num_possible_cpus(); i++)
1243 pr_cont("%d(on) ", i);
1245 pr_cont("%d(off) ", i);
1249 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1251 pr_err("Lustre: page %d belongs to cpu %d\n",
1253 pr_err("Lustre: There are %d pages unwritten\n", i);
1255 __LASSERT(list_empty(&pc.pc_pages));
1261 int cfs_trace_start_thread(void)
1263 struct task_struct *tsk;
1269 tsk = kthread_create(tracefiled, NULL, "ktracefiled");
1272 else if (cmpxchg(&tctl_task, NULL, tsk) != NULL)
1273 /* already running */
1276 wake_up_process(tsk);
1281 void cfs_trace_stop_thread(void)
1283 struct task_struct *tsk;
1285 tsk = xchg(&tctl_task, NULL);
1287 pr_info("Lustre: shutting down debug daemon thread...\n");
1292 /* percents to share the total debug memory for each type */
1293 static unsigned int pages_factor[CFS_TCD_TYPE_CNT] = {
1294 80, /* 80% pages for CFS_TCD_TYPE_PROC */
1295 10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
1296 10 /* 10% pages for CFS_TCD_TYPE_IRQ */
1299 int cfs_tracefile_init(int max_pages)
1301 struct cfs_trace_cpu_data *tcd;
1305 /* initialize trace_data */
1306 memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
1307 for (i = 0; i < CFS_TCD_TYPE_CNT; i++) {
1309 kmalloc_array(num_possible_cpus(),
1310 sizeof(union cfs_trace_data_union),
1312 if (!cfs_trace_data[i])
1313 goto out_trace_data;
1316 /* arch related info initialized */
1317 cfs_tcd_for_each(tcd, i, j) {
1318 int factor = pages_factor[i];
1320 /* Note that we have three separate spin_lock_init()
1321 * calls so that the locks get three separate classes
1322 * and lockdep never thinks they are related. As they
1323 * are used in different interrupt contexts, lockdep
1324 * would otherwise think that the usage would conflict.
1327 case CFS_TCD_TYPE_PROC:
1328 spin_lock_init(&tcd->tcd_lock);
1330 case CFS_TCD_TYPE_SOFTIRQ:
1331 spin_lock_init(&tcd->tcd_lock);
1333 case CFS_TCD_TYPE_IRQ:
1334 spin_lock_init(&tcd->tcd_lock);
1337 tcd->tcd_pages_factor = factor;
1341 INIT_LIST_HEAD(&tcd->tcd_pages);
1342 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1343 tcd->tcd_cur_pages = 0;
1344 tcd->tcd_cur_stock_pages = 0;
1345 tcd->tcd_max_pages = (max_pages * factor) / 100;
1346 LASSERT(tcd->tcd_max_pages > 0);
1347 tcd->tcd_shutting_down = 0;
1349 daemon_pages_max = max_pages;
1354 for (i = 0; cfs_trace_data[i]; i++) {
1355 kfree(cfs_trace_data[i]);
1356 cfs_trace_data[i] = NULL;
1358 pr_err("lnet: Not enough memory\n");
1362 static void trace_cleanup_on_all_cpus(void)
1364 struct cfs_trace_cpu_data *tcd;
1365 struct cfs_trace_page *tage;
1368 for_each_possible_cpu(cpu) {
1369 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1370 if (!tcd->tcd_pages_factor)
1371 /* Not initialised */
1373 tcd->tcd_shutting_down = 1;
1375 while (!list_empty(&tcd->tcd_pages)) {
1376 tage = list_first_entry(&tcd->tcd_pages,
1377 struct cfs_trace_page,
1379 __LASSERT_TAGE_INVARIANT(tage);
1381 list_del(&tage->linkage);
1382 cfs_tage_free(tage);
1384 tcd->tcd_cur_pages = 0;
1389 static void cfs_trace_cleanup(void)
1391 struct page_collection pc;
1394 INIT_LIST_HEAD(&pc.pc_pages);
1396 trace_cleanup_on_all_cpus();
1398 for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) {
1399 kfree(cfs_trace_data[i]);
1400 cfs_trace_data[i] = NULL;
1404 void cfs_tracefile_exit(void)
1406 cfs_trace_stop_thread();
1407 cfs_trace_flush_pages();
1408 cfs_trace_cleanup();