printk: enable interrupts before calling console_trylock_for_printk()
[pandora-kernel.git] / kernel / printk / printk.c
index 6f598f9..df202fe 100644 (file)
@@ -45,6 +45,7 @@
 #include <linux/poll.h>
 #include <linux/irq_work.h>
 #include <linux/utsname.h>
+#include <linux/ctype.h>
 
 #include <asm/uaccess.h>
 
@@ -56,7 +57,7 @@
 
 int console_printk[4] = {
        CONSOLE_LOGLEVEL_DEFAULT,       /* console_loglevel */
-       DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
+       MESSAGE_LOGLEVEL_DEFAULT,       /* default_message_loglevel */
        CONSOLE_LOGLEVEL_MIN,           /* minimum_console_loglevel */
        CONSOLE_LOGLEVEL_DEFAULT,       /* default_console_loglevel */
 };
@@ -113,9 +114,9 @@ static int __down_trylock_console_sem(unsigned long ip)
  * This is used for debugging the mess that is the VT code by
  * keeping track if we have the console semaphore held. It's
  * definitely not the perfect debug tool (we don't know if _WE_
- * hold it are racing, but it helps tracking those weird code
- * path in the console code where we end up in places I want
- * locked without the console sempahore held
+ * hold it and are racing, but it helps tracking those weird code
+ * paths in the console code where we end up in places I want
+ * locked without the console sempahore held).
  */
 static int console_locked, console_suspended;
 
@@ -146,8 +147,8 @@ static int console_may_schedule;
  * the overall length of the record.
  *
  * The heads to the first and last entry in the buffer, as well as the
- * sequence numbers of these both entries are maintained when messages
- * are stored..
+ * sequence numbers of these entries are maintained when messages are
+ * stored.
  *
  * If the heads indicate available messages, the length in the header
  * tells the start next message. A length == 0 for the next message
@@ -257,7 +258,7 @@ static u64 clear_seq;
 static u32 clear_idx;
 
 #define PREFIX_MAX             32
-#define LOG_LINE_MAX           1024 - PREFIX_MAX
+#define LOG_LINE_MAX           (1024 - PREFIX_MAX)
 
 /* record buffer */
 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
@@ -266,6 +267,7 @@ static u32 clear_idx;
 #define LOG_ALIGN __alignof__(struct printk_log)
 #endif
 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
+#define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
 static char *log_buf = __log_buf;
 static u32 log_buf_len = __LOG_BUF_LEN;
@@ -344,7 +346,7 @@ static int log_make_free_space(u32 msg_size)
        while (log_first_seq < log_next_seq) {
                if (logbuf_has_space(msg_size, false))
                        return 0;
-               /* drop old messages until we have enough continuous space */
+               /* drop old messages until we have enough contiguous space */
                log_first_idx = log_next(log_first_idx);
                log_first_seq++;
        }
@@ -453,11 +455,7 @@ static int log_store(int facility, int level,
        return msg->text_len;
 }
 
-#ifdef CONFIG_SECURITY_DMESG_RESTRICT
-int dmesg_restrict = 1;
-#else
-int dmesg_restrict;
-#endif
+int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
 
 static int syslog_action_restricted(int type)
 {
@@ -828,26 +826,65 @@ void log_buf_kexec_setup(void)
 /* requested log_buf_len from kernel cmdline */
 static unsigned long __initdata new_log_buf_len;
 
-/* save requested log_buf_len since it's too early to process it */
-static int __init log_buf_len_setup(char *str)
+/* we practice scaling the ring buffer by powers of 2 */
+static void __init log_buf_len_update(unsigned size)
 {
-       unsigned size = memparse(str, &str);
-
        if (size)
                size = roundup_pow_of_two(size);
        if (size > log_buf_len)
                new_log_buf_len = size;
+}
+
+/* save requested log_buf_len since it's too early to process it */
+static int __init log_buf_len_setup(char *str)
+{
+       unsigned size = memparse(str, &str);
+
+       log_buf_len_update(size);
 
        return 0;
 }
 early_param("log_buf_len", log_buf_len_setup);
 
+static void __init log_buf_add_cpu(void)
+{
+       unsigned int cpu_extra;
+
+       /*
+        * archs should set up cpu_possible_bits properly with
+        * set_cpu_possible() after setup_arch() but just in
+        * case lets ensure this is valid.
+        */
+       if (num_possible_cpus() == 1)
+               return;
+
+       cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
+
+       /* by default this will only continue through for large > 64 CPUs */
+       if (cpu_extra <= __LOG_BUF_LEN / 2)
+               return;
+
+       pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
+               __LOG_CPU_MAX_BUF_LEN);
+       pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
+               cpu_extra);
+       pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
+
+       log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
+}
+
 void __init setup_log_buf(int early)
 {
        unsigned long flags;
        char *new_log_buf;
        int free;
 
+       if (log_buf != __log_buf)
+               return;
+
+       if (!early && !new_log_buf_len)
+               log_buf_add_cpu();
+
        if (!new_log_buf_len)
                return;
 
@@ -873,7 +910,7 @@ void __init setup_log_buf(int early)
        memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
        raw_spin_unlock_irqrestore(&logbuf_lock, flags);
 
-       pr_info("log_buf_len: %d\n", log_buf_len);
+       pr_info("log_buf_len: %d bytes\n", log_buf_len);
        pr_info("early log buf free: %d(%d%%)\n",
                free, (free * 100) / __LOG_BUF_LEN);
 }
@@ -948,11 +985,7 @@ static inline void boot_delay_msec(int level)
 }
 #endif
 
-#if defined(CONFIG_PRINTK_TIME)
-static bool printk_time = 1;
-#else
-static bool printk_time;
-#endif
+static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
 
 static size_t print_time(u64 ts, char *buf)
@@ -1311,7 +1344,7 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
                         * for pending data, not the size; return the count of
                         * records, not the length.
                         */
-                       error = log_next_idx - syslog_idx;
+                       error = log_next_seq - syslog_seq;
                } else {
                        u64 seq = syslog_seq;
                        u32 idx = syslog_idx;
@@ -1417,10 +1450,9 @@ static int have_callable_console(void)
 /*
  * Can we actually use the console at this time on this cpu?
  *
- * Console drivers may assume that per-cpu resources have
- * been allocated. So unless they're explicitly marked as
- * being able to cope (CON_ANYTIME) don't call them until
- * this CPU is officially up.
+ * Console drivers may assume that per-cpu resources have been allocated. So
+ * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
+ * call them until this CPU is officially up.
  */
 static inline int can_use_console(unsigned int cpu)
 {
@@ -1433,8 +1465,10 @@ static inline int can_use_console(unsigned int cpu)
  * console_lock held, and 'console_locked' set) if it
  * is successful, false otherwise.
  */
-static int console_trylock_for_printk(unsigned int cpu)
+static int console_trylock_for_printk(void)
 {
+       unsigned int cpu = smp_processor_id();
+
        if (!console_trylock())
                return 0;
        /*
@@ -1477,7 +1511,7 @@ static struct cont {
        struct task_struct *owner;      /* task of first print*/
        u64 ts_nsec;                    /* time of first print */
        u8 level;                       /* log level of first message */
-       u8 facility;                    /* log level of first message */
+       u8 facility;                    /* log facility of first message */
        enum log_flags flags;           /* prefix, newline flags */
        bool flushed:1;                 /* buffer sealed and committed */
 } cont;
@@ -1609,7 +1643,8 @@ asmlinkage int vprintk_emit(int facility, int level,
                 */
                if (!oops_in_progress && !lockdep_recursing(current)) {
                        recursion_bug = 1;
-                       goto out_restore_irqs;
+                       local_irq_restore(flags);
+                       return 0;
                }
                zap_locks();
        }
@@ -1717,21 +1752,30 @@ asmlinkage int vprintk_emit(int facility, int level,
 
        logbuf_cpu = UINT_MAX;
        raw_spin_unlock(&logbuf_lock);
+       lockdep_on();
+       local_irq_restore(flags);
 
        /* If called from the scheduler, we can not call up(). */
        if (!in_sched) {
+               lockdep_off();
+               /*
+                * Disable preemption to avoid being preempted while holding
+                * console_sem which would prevent anyone from printing to
+                * console
+                */
+               preempt_disable();
+
                /*
                 * Try to acquire and then immediately release the console
                 * semaphore.  The release will print out buffers and wake up
                 * /dev/kmsg and syslog() users.
                 */
-               if (console_trylock_for_printk(this_cpu))
+               if (console_trylock_for_printk())
                        console_unlock();
+               preempt_enable();
+               lockdep_on();
        }
 
-       lockdep_on();
-out_restore_irqs:
-       local_irq_restore(flags);
        return printed_len;
 }
 EXPORT_SYMBOL(vprintk_emit);
@@ -1803,7 +1847,7 @@ EXPORT_SYMBOL(printk);
 
 #define LOG_LINE_MAX           0
 #define PREFIX_MAX             0
-#define LOG_LINE_MAX 0
+
 static u64 syslog_seq;
 static u32 syslog_idx;
 static u64 console_seq;
@@ -1882,11 +1926,12 @@ static int __add_preferred_console(char *name, int idx, char *options,
        return 0;
 }
 /*
- * Set up a list of consoles.  Called from init/main.c
+ * Set up a console.  Called via do_early_param() in init/main.c
+ * for each "console=" parameter in the boot command line.
  */
 static int __init console_setup(char *str)
 {
-       char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
+       char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
        char *s, *options, *brl_options = NULL;
        int idx;
 
@@ -1903,7 +1948,8 @@ static int __init console_setup(char *str)
                strncpy(buf, str, sizeof(buf) - 1);
        }
        buf[sizeof(buf) - 1] = 0;
-       if ((options = strchr(str, ',')) != NULL)
+       options = strchr(str, ',');
+       if (options)
                *(options++) = 0;
 #ifdef __sparc__
        if (!strcmp(str, "ttya"))
@@ -1912,7 +1958,7 @@ static int __init console_setup(char *str)
                strcpy(buf, "ttyS1");
 #endif
        for (s = buf; *s; s++)
-               if ((*s >= '0' && *s <= '9') || *s == ',')
+               if (isdigit(*s) || *s == ',')
                        break;
        idx = simple_strtoul(s, NULL, 10);
        *s = 0;
@@ -1951,7 +1997,6 @@ int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, cha
             i++, c++)
                if (strcmp(c->name, name) == 0 && c->index == idx) {
                        strlcpy(c->name, name_new, sizeof(c->name));
-                       c->name[sizeof(c->name) - 1] = 0;
                        c->options = options;
                        c->index = idx_new;
                        return i;
@@ -2046,8 +2091,8 @@ EXPORT_SYMBOL(console_lock);
 /**
  * console_trylock - try to lock the console system for exclusive use.
  *
- * Tried to acquire a lock which guarantees that the caller has
- * exclusive access to the console system and the console_drivers list.
+ * Try to acquire a lock which guarantees that the caller has exclusive
+ * access to the console system and the console_drivers list.
  *
  * returns 1 on success, and 0 on failure to acquire the lock.
  */
@@ -2619,14 +2664,13 @@ EXPORT_SYMBOL(__printk_ratelimit);
 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
                        unsigned int interval_msecs)
 {
-       if (*caller_jiffies == 0
-                       || !time_in_range(jiffies, *caller_jiffies,
-                                       *caller_jiffies
-                                       + msecs_to_jiffies(interval_msecs))) {
-               *caller_jiffies = jiffies;
-               return true;
-       }
-       return false;
+       unsigned long elapsed = jiffies - *caller_jiffies;
+
+       if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
+               return false;
+
+       *caller_jiffies = jiffies;
+       return true;
 }
 EXPORT_SYMBOL(printk_timed_ratelimit);