Whamcloud - gitweb
LU-14428 libcfs: discard cfs_trace_copyin_string()
[fs/lustre-release.git] / libcfs / libcfs / tracefile.c
index 2d0ec57..36a16fa 100644 (file)
@@ -35,9 +35,7 @@
  * Author: Phil Schwan <phil@clusterfs.com>
  */
 
-
 #define DEBUG_SUBSYSTEM S_LNET
-#define LUSTRE_TRACEFILE_PRIVATE
 #include "tracefile.h"
 
 #include <linux/ctype.h>
 #include <libcfs/linux/linux-fs.h>
 #include <libcfs/libcfs.h>
 
-#define TCD_MAX_TYPES                  8
 
-union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
+enum cfs_trace_buf_type {
+       CFS_TCD_TYPE_PROC = 0,
+       CFS_TCD_TYPE_SOFTIRQ,
+       CFS_TCD_TYPE_IRQ,
+       CFS_TCD_TYPE_CNT
+};
+
+union cfs_trace_data_union (*cfs_trace_data[CFS_TCD_TYPE_CNT])[NR_CPUS] __cacheline_aligned;
 
-char *cfs_trace_console_buffers[NR_CPUS][CFS_TCD_TYPE_MAX];
 char cfs_tracefile[TRACEFILE_NAME_SIZE];
 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
 static struct tracefiled_ctl trace_tctl;
@@ -76,7 +79,7 @@ static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
 int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
        __acquires(&tcd->tcd_lock)
 {
-       __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
+       __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
        if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
                spin_lock_irqsave(&tcd->tcd_lock, tcd->tcd_lock_flags);
        else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
@@ -91,7 +94,7 @@ int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
 void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
        __releases(&tcd->tcd_lock)
 {
-       __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_MAX);
+       __LASSERT(tcd->tcd_type < CFS_TCD_TYPE_CNT);
        if (tcd->tcd_type == CFS_TCD_TYPE_IRQ)
                spin_unlock_irqrestore(&tcd->tcd_lock, tcd->tcd_lock_flags);
        else if (tcd->tcd_type == CFS_TCD_TYPE_SOFTIRQ)
@@ -103,13 +106,13 @@ void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking)
 }
 
 #define cfs_tcd_for_each(tcd, i, j)                                    \
-       for (i = 0; cfs_trace_data[i]; i++)                             \
+       for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++)     \
                for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd);     \
                     j < num_possible_cpus();                           \
                     j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
 
 #define cfs_tcd_for_each_type_lock(tcd, i, cpu)                                \
-       for (i = 0; cfs_trace_data[i] &&                                \
+       for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i] &&        \
             (tcd = &(*cfs_trace_data[i])[cpu].tcd) &&                  \
             cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
 
@@ -357,9 +360,9 @@ static void cfs_tty_write_message(const char *prefix, int mask, const char *msg)
        tty_kref_put(tty);
 }
 
-static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
-                                const char *buf, int len, const char *file,
-                                const char *fn)
+static void cfs_vprint_to_console(struct ptldebug_header *hdr, int mask,
+                                 struct va_format *vaf, const char *file,
+                                 const char *fn)
 {
        char *prefix = "Lustre";
 
@@ -368,32 +371,46 @@ static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
 
        if (mask & D_CONSOLE) {
                if (mask & D_EMERG)
-                       pr_emerg("%sError: %.*s", prefix, len, buf);
+                       pr_emerg("%sError: %pV", prefix, vaf);
                else if (mask & D_ERROR)
-                       pr_err("%sError: %.*s", prefix, len, buf);
+                       pr_err("%sError: %pV", prefix, vaf);
                else if (mask & D_WARNING)
-                       pr_warn("%s: %.*s", prefix, len, buf);
+                       pr_warn("%s: %pV", prefix, vaf);
                else if (mask & libcfs_printk)
-                       pr_info("%s: %.*s", prefix, len, buf);
+                       pr_info("%s: %pV", prefix, vaf);
        } else {
                if (mask & D_EMERG)
-                       pr_emerg("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
+                       pr_emerg("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
                                 hdr->ph_pid, hdr->ph_extern_pid, file,
-                                hdr->ph_line_num, fn, len, buf);
+                                hdr->ph_line_num, fn, vaf);
                else if (mask & D_ERROR)
-                       pr_err("%sError: %d:%d:(%s:%d:%s()) %.*s", prefix,
+                       pr_err("%sError: %d:%d:(%s:%d:%s()) %pV", prefix,
                               hdr->ph_pid, hdr->ph_extern_pid, file,
-                              hdr->ph_line_num, fn, len, buf);
+                              hdr->ph_line_num, fn, vaf);
                else if (mask & D_WARNING)
-                       pr_warn("%s: %d:%d:(%s:%d:%s()) %.*s", prefix,
+                       pr_warn("%s: %d:%d:(%s:%d:%s()) %pV", prefix,
                                hdr->ph_pid, hdr->ph_extern_pid, file,
-                               hdr->ph_line_num, fn, len, buf);
+                               hdr->ph_line_num, fn, vaf);
                else if (mask & (D_CONSOLE | libcfs_printk))
-                       pr_info("%s: %.*s", prefix, len, buf);
+                       pr_info("%s: %pV", prefix, vaf);
        }
 
        if (mask & D_TTY)
-               cfs_tty_write_message(prefix, mask, buf);
+               /* tty_write_msg doesn't handle formatting */
+               cfs_tty_write_message(prefix, mask, vaf->fmt);
+}
+
+static void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
+                                const char *file, const char *fn,
+                                const char *fmt, ...)
+{
+       struct va_format vaf;
+       va_list args;
+
+       va_start(args, fmt);
+       vaf.fmt = fmt;
+       vaf.va = &args;
+       cfs_vprint_to_console(hdr, mask, &vaf, file, fn);
 }
 
 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
@@ -496,6 +513,9 @@ int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
                if (needed < 2 || *(string_buf + needed - 2) != '\r')
                        pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
                                file, msgdata->msg_line, msgdata->msg_fn);
+               if (strnchr(string_buf, needed, '%'))
+                       pr_info("Lustre: format at %s:%d:%s mustn't contain %%\n",
+                               file, msgdata->msg_line, msgdata->msg_fn);
        }
 
        header.ph_len = known_size + needed;
@@ -560,36 +580,28 @@ console:
        }
 
        if (tcd) {
-               cfs_print_to_console(&header, mask, string_buf, needed, file,
-                                    msgdata->msg_fn);
+               cfs_print_to_console(&header, mask, file, msgdata->msg_fn,
+                                    "%s", string_buf);
                cfs_trace_put_tcd(tcd);
        } else {
-               string_buf = cfs_trace_get_console_buffer();
+               struct va_format vaf;
 
                va_start(ap, format);
-               needed = vscnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
-                                   format, ap);
+               vaf.fmt = format;
+               vaf.va = &ap;
+               cfs_vprint_to_console(&header, mask,
+                                     &vaf, file, msgdata->msg_fn);
                va_end(ap);
-
-               cfs_print_to_console(&header, mask,
-                                    string_buf, needed, file, msgdata->msg_fn);
-
-               put_cpu();
        }
 
        if (cdls != NULL && cdls->cdls_count != 0) {
-               string_buf = cfs_trace_get_console_buffer();
-
-               needed = scnprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
-                                  "Skipped %d previous similar message%s\n",
-                                  cdls->cdls_count,
-                                  (cdls->cdls_count > 1) ? "s" : "");
-
                /* Do not allow print this to TTY */
-               cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
-                                    needed, file, msgdata->msg_fn);
+               cfs_print_to_console(&header, mask & ~D_TTY, file,
+                                    msgdata->msg_fn,
+                                    "Skipped %d previous similar message%s\n",
+                                    cdls->cdls_count,
+                                    (cdls->cdls_count > 1) ? "s" : "");
 
-               put_cpu();
                cdls->cdls_count = 0;
        }
 
@@ -609,8 +621,8 @@ cfs_trace_assertion_failed(const char *str,
 
        cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
 
-       cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
-                            msgdata->msg_file, msgdata->msg_fn);
+       cfs_print_to_console(&hdr, D_EMERG, msgdata->msg_file, msgdata->msg_fn,
+                            "%s", str);
 
        panic("Lustre debug assertion failure\n");
 
@@ -747,6 +759,7 @@ static void put_pages_on_daemon_list(struct page_collection *pc)
         }
 }
 
+#ifdef LNET_DUMP_ON_PANIC
 void cfs_trace_debug_print(void)
 {
        struct page_collection pc;
@@ -764,25 +777,27 @@ void cfs_trace_debug_print(void)
                page = tage->page;
                p = page_address(page);
                while (p < ((char *)page_address(page) + tage->used)) {
-                        struct ptldebug_header *hdr;
-                        int len;
-                        hdr = (void *)p;
-                        p += sizeof(*hdr);
-                        file = p;
-                        p += strlen(file) + 1;
-                        fn = p;
-                        p += strlen(fn) + 1;
-                        len = hdr->ph_len - (int)(p - (char *)hdr);
-
-                        cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
-
-                        p += len;
-                }
+                       struct ptldebug_header *hdr;
+                       int len;
+                       hdr = (void *)p;
+                       p += sizeof(*hdr);
+                       file = p;
+                       p += strlen(file) + 1;
+                       fn = p;
+                       p += strlen(fn) + 1;
+                       len = hdr->ph_len - (int)(p - (char *)hdr);
+
+                       cfs_print_to_console(hdr, D_EMERG, file, fn,
+                                            "%.*s", len, p);
+
+                       p += len;
+               }
 
                list_del(&tage->linkage);
                cfs_tage_free(tage);
        }
 }
+#endif /* LNET_DUMP_ON_PANIC */
 
 int cfs_tracefile_dump_all_pages(char *filename)
 {
@@ -845,12 +860,12 @@ void cfs_trace_flush_pages(void)
 {
        struct page_collection pc;
        struct cfs_trace_page *tage;
-       struct cfs_trace_page *tmp;
 
        pc.pc_want_daemon_pages = 1;
        collect_pages(&pc);
-       list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
-
+       while (!list_empty(&pc.pc_pages)) {
+               tage = list_first_entry(&pc.pc_pages,
+                                       struct cfs_trace_page, linkage);
                __LASSERT_TAGE_INVARIANT(tage);
 
                list_del(&tage->linkage);
@@ -858,33 +873,6 @@ void cfs_trace_flush_pages(void)
        }
 }
 
-int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
-                           const char __user *usr_buffer, int usr_buffer_nob)
-{
-        int    nob;
-
-        if (usr_buffer_nob > knl_buffer_nob)
-                return -EOVERFLOW;
-
-       if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
-                return -EFAULT;
-
-        nob = strnlen(knl_buffer, usr_buffer_nob);
-       while (--nob >= 0)                      /* strip trailing whitespace */
-                if (!isspace(knl_buffer[nob]))
-                        break;
-
-        if (nob < 0)                            /* empty string */
-                return -EINVAL;
-
-        if (nob == knl_buffer_nob)              /* no space to terminate */
-                return -EOVERFLOW;
-
-        knl_buffer[nob + 1] = 0;                /* terminate */
-        return 0;
-}
-EXPORT_SYMBOL(cfs_trace_copyin_string);
-
 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
                              const char *knl_buffer, char *append)
 {
@@ -924,26 +912,22 @@ int cfs_trace_allocate_string_buffer(char **str, int nob)
 
 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
 {
-        char         *str;
-        int           rc;
-
-        rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
-        if (rc != 0)
-                return rc;
+       char *str;
+       char *path;
+       int rc;
 
-        rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
-                                     usr_str, usr_str_nob);
-        if (rc != 0)
-                goto out;
+       str = memdup_user_nul(usr_str, usr_str_nob);
+       if (!str)
+               return -ENOMEM;
 
-        if (str[0] != '/') {
-                rc = -EINVAL;
-                goto out;
-        }
-        rc = cfs_tracefile_dump_all_pages(str);
-out:
+       path = strim(str);
+       if (path[0] != '/')
+               rc = -EINVAL;
+       else
+               rc = cfs_tracefile_dump_all_pages(path);
        kfree(str);
-        return rc;
+
+       return rc;
 }
 
 int cfs_trace_daemon_command(char *str)
@@ -987,20 +971,17 @@ int cfs_trace_daemon_command(char *str)
 
 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
 {
-        char *str;
-        int   rc;
+       char *str;
+       int   rc;
 
-        rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
-        if (rc != 0)
-                return rc;
-
-        rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
-                                 usr_str, usr_str_nob);
-        if (rc == 0)
-                rc = cfs_trace_daemon_command(str);
+       str = memdup_user_nul(usr_str, usr_str_nob);
+       if (!str)
+               return -ENOMEM;
 
+       rc = cfs_trace_daemon_command(strim(str));
        kfree(str);
-        return rc;
+
+       return rc;
 }
 
 int cfs_trace_set_debug_mb(int mb)
@@ -1070,17 +1051,22 @@ static int tracefiled(void *arg)
 
        complete(&tctl->tctl_start);
 
-       while (1) {
-               wait_queue_entry_t __wait;
+       pc.pc_want_daemon_pages = 0;
 
-                pc.pc_want_daemon_pages = 0;
-                collect_pages(&pc);
+       while (!last_loop) {
+               wait_event_timeout(tctl->tctl_waitq,
+                                  ({ collect_pages(&pc);
+                                    !list_empty(&pc.pc_pages); }) ||
+                                  atomic_read(&tctl->tctl_shutdown),
+                                  cfs_time_seconds(1));
+               if (atomic_read(&tctl->tctl_shutdown))
+                       last_loop = 1;
                if (list_empty(&pc.pc_pages))
-                        goto end_loop;
+                       continue;
 
-                filp = NULL;
+               filp = NULL;
                down_read(&cfs_tracefile_sem);
-                if (cfs_tracefile[0] != 0) {
+               if (cfs_tracefile[0] != 0) {
                        filp = filp_open(cfs_tracefile,
                                         O_CREAT | O_RDWR | O_LARGEFILE,
                                         0600);
@@ -1092,11 +1078,11 @@ static int tracefiled(void *arg)
                        }
                }
                up_read(&cfs_tracefile_sem);
-                if (filp == NULL) {
-                        put_pages_on_daemon_list(&pc);
+               if (filp == NULL) {
+                       put_pages_on_daemon_list(&pc);
                        __LASSERT(list_empty(&pc.pc_pages));
-                        goto end_loop;
-                }
+                       continue;
+               }
 
                list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
                        struct dentry *de = file_dentry(filp);
@@ -1119,12 +1105,12 @@ static int tracefiled(void *arg)
                                __LASSERT(list_empty(&pc.pc_pages));
                                break;
                        }
-                }
+               }
 
                filp_close(filp, NULL);
-                put_pages_on_daemon_list(&pc);
+               put_pages_on_daemon_list(&pc);
                if (!list_empty(&pc.pc_pages)) {
-                        int i;
+                       int i;
 
                        pr_alert("Lustre: trace pages aren't empty\n");
                        pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
@@ -1143,22 +1129,9 @@ static int tracefiled(void *arg)
                        pr_err("Lustre: There are %d pages unwritten\n", i);
                }
                __LASSERT(list_empty(&pc.pc_pages));
-end_loop:
-               if (atomic_read(&tctl->tctl_shutdown)) {
-                       if (last_loop == 0) {
-                               last_loop = 1;
-                               continue;
-                       } else {
-                               break;
-                       }
-               }
-               init_waitqueue_entry(&__wait, current);
-               add_wait_queue(&tctl->tctl_waitq, &__wait);
-               schedule_timeout_interruptible(cfs_time_seconds(1));
-               remove_wait_queue(&tctl->tctl_waitq, &__wait);
-        }
+       }
        complete(&tctl->tctl_stop);
-        return 0;
+       return 0;
 }
 
 int cfs_trace_start_thread(void)
@@ -1202,7 +1175,7 @@ void cfs_trace_stop_thread(void)
 }
 
 /* percents to share the total debug memory for each type */
-static unsigned int pages_factor[CFS_TCD_TYPE_MAX] = {
+static unsigned int pages_factor[CFS_TCD_TYPE_CNT] = {
        80, /* 80% pages for CFS_TCD_TYPE_PROC */
        10, /* 10% pages for CFS_TCD_TYPE_SOFTIRQ */
        10  /* 10% pages for CFS_TCD_TYPE_IRQ */
@@ -1216,7 +1189,7 @@ int cfs_tracefile_init(int max_pages)
 
        /* initialize trace_data */
        memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
-       for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
+       for (i = 0; i < CFS_TCD_TYPE_CNT; i++) {
                cfs_trace_data[i] =
                        kmalloc_array(num_possible_cpus(),
                                      sizeof(union cfs_trace_data_union),
@@ -1245,23 +1218,8 @@ int cfs_tracefile_init(int max_pages)
                tcd->tcd_shutting_down = 0;
        }
 
-       for (i = 0; i < num_possible_cpus(); i++)
-               for (j = 0; j < 3; j++) {
-                       cfs_trace_console_buffers[i][j] =
-                               kmalloc(CFS_TRACE_CONSOLE_BUFFER_SIZE,
-                                       GFP_KERNEL);
-                       if (!cfs_trace_console_buffers[i][j])
-                               goto out_buffers;
-               }
-
        return 0;
 
-out_buffers:
-       for (i = 0; i < num_possible_cpus(); i++)
-               for (j = 0; j < 3; j++) {
-                       kfree(cfs_trace_console_buffers[i][j]);
-                       cfs_trace_console_buffers[i][j] = NULL;
-               }
 out_trace_data:
        for (i = 0; cfs_trace_data[i]; i++) {
                kfree(cfs_trace_data[i]);
@@ -1275,7 +1233,6 @@ static void trace_cleanup_on_all_cpus(void)
 {
        struct cfs_trace_cpu_data *tcd;
        struct cfs_trace_page *tage;
-       struct cfs_trace_page *tmp;
        int i, cpu;
 
        for_each_possible_cpu(cpu) {
@@ -1285,7 +1242,10 @@ static void trace_cleanup_on_all_cpus(void)
                                continue;
                        tcd->tcd_shutting_down = 1;
 
-                       list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
+                       while (!list_empty(&tcd->tcd_pages)) {
+                               tage = list_first_entry(&tcd->tcd_pages,
+                                                       struct cfs_trace_page,
+                                                       linkage);
                                __LASSERT_TAGE_INVARIANT(tage);
 
                                list_del(&tage->linkage);
@@ -1300,19 +1260,12 @@ static void cfs_trace_cleanup(void)
 {
        struct page_collection pc;
        int i;
-       int j;
 
        INIT_LIST_HEAD(&pc.pc_pages);
 
        trace_cleanup_on_all_cpus();
 
-       for (i = 0; i < num_possible_cpus(); i++)
-               for (j = 0; j < 3; j++) {
-                       kfree(cfs_trace_console_buffers[i][j]);
-                       cfs_trace_console_buffers[i][j] = NULL;
-               }
-
-       for (i = 0; cfs_trace_data[i]; i++) {
+       for (i = 0; i < CFS_TCD_TYPE_CNT && cfs_trace_data[i]; i++) {
                kfree(cfs_trace_data[i]);
                cfs_trace_data[i] = NULL;
        }