From 0048f176788f3c5bd3348244f6e2a1a3ad9b733b Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Thu, 14 May 2015 11:36:36 -0400 Subject: [PATCH 01/16] printk: guard the amount written per line by devkmsg_read() This patchset updates netconsole so that it can emit messages with the same header as used in /dev/kmsg which gives neconsole receiver full log information which enables things like structured logging and detection of lost messages. This patch: devkmsg_read() uses 8k buffer and assumes that the formatted output message won't overrun which seems safe given LOG_LINE_MAX, the current use of dict and the escaping method being used; however, we're planning to use devkmsg formatting wider and accounting for the buffer size properly isn't that complicated. This patch defines CONSOLE_EXT_LOG_MAX as 8192 and updates devkmsg_read() so that it limits output accordingly. Signed-off-by: Tejun Heo Cc: David Miller Cc: Kay Sievers Reviewed-by: Petr Mladek Cc: Tetsuo Handa Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Change-Id: Idead54c1fb93161aebd0e00be0b66f96b907233b --- include/linux/printk.h | 4 ++++ kernel/printk_kmsg.c | 34 ++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 708b8a8..db9831e 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -33,6 +33,10 @@ static inline const char *printk_skip_level(const char *buffer) return buffer; } +#ifdef CONFIG_MULTIPLE_KMSG +#define CONSOLE_EXT_LOG_MAX 8192 +#endif + extern int console_printk[]; #define console_loglevel (console_printk[0]) diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 7480558..c7023f7 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -487,6 +487,11 @@ static int check_syslog_permissions(int type, bool from_file) return security_syslog(type); } +static void append_char(char **pp, char *e, char c) +{ + if (*pp < e) + *(*pp)++ = c; +} /* /dev/kmsg - userspace message inject/listen interface */ struct devkmsg_user { @@ -494,7 +499,7 @@ struct devkmsg_user { u32 idx; enum log_flags prev; struct mutex lock; - char buf[8192]; + char buf[CONSOLE_EXT_LOG_MAX]; }; static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, @@ -558,6 +563,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, { struct devkmsg_user *user = file->private_data; struct log *msg; + char *p, *e; u64 ts_usec; size_t i; char cont = '-'; @@ -567,6 +573,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, if (!user) return -EBADF; + p = user->buf; + e = user->buf + sizeof(user->buf); + ret = mutex_lock_interruptible(&user->lock); if (ret) return ret; @@ -613,9 +622,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))) cont = '+'; - len = sprintf(user->buf, "%u,%llu,%llu,%c;", - (msg->facility << 3) | msg->level, - user->seq, ts_usec, cont); + p += scnprintf(p, e - p, "%u,%llu,%llu,%c;", + (msg->facility << 3) | msg->level, + user->seq, ts_usec, cont); user->prev = msg->flags; /* escape non-printable characters */ @@ -623,11 +632,11 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, unsigned char c = log_text(msg)[i]; if (c < ' ' || c >= 127 || c == '\\') - len += sprintf(user->buf + len, "\\x%02x", c); + p += scnprintf(p, e - p, "\\x%02x", c); else - user->buf[len++] = c; + append_char(&p, e, c); } - user->buf[len++] = '\n'; + append_char(&p, e, '\n'); if (msg->dict_len) { bool line = true; @@ -636,30 +645,31 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, unsigned char c = log_dict(msg)[i]; if (line) { - user->buf[len++] = ' '; + append_char(&p, e, ' '); line = false; } if (c == '\0') { - user->buf[len++] = '\n'; + append_char(&p, e, '\n'); line = true; continue; } if (c < ' ' || c >= 127 || c == '\\') { - len += sprintf(user->buf + len, "\\x%02x", c); + p += scnprintf(p, e - p, "\\x%02x", c); continue; } - user->buf[len++] = c; + append_char(&p, e, c); } - user->buf[len++] = '\n'; + append_char(&p, e, '\n'); } user->idx = log_next(user->idx); user->seq++; raw_spin_unlock_irq(&logbuf_lock); + len = p - user->buf; if (len > count) { ret = -EINVAL; goto out; -- 2.7.4 From 6f06fc0519598f64be7a04781c82ab470bb4de2d Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Thu, 2 Jul 2015 16:32:28 +0200 Subject: [PATCH 02/16] printk: move code regarding log message storing format Preparation commit for future changes purpose. Moves some code responsible for storing log messages in proper format. Change-Id: Idead14e73d498e1e9ecba2da0e897a99ee15c583 Signed-off-by: Marcin Niesluchowski --- kernel/printk_kmsg.c | 255 +++++++++++++++++++++++++-------------------------- 1 file changed, 123 insertions(+), 132 deletions(-) diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index c7023f7..7420cc9 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -441,6 +441,129 @@ static void log_store(int facility, int level, log_next_seq++; } +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) +{ + unsigned long rem_nsec; + + if (!printk_time) + return 0; + + rem_nsec = do_div(ts, 1000000000); + + if (!buf) + return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts); + + return sprintf(buf, "[%5lu.%06lu] ", + (unsigned long)ts, rem_nsec / 1000); +} + +/* + * Continuation lines are buffered, and not committed to the record buffer + * until the line is complete, or a race forces it. The line fragments + * though, are printed immediately to the consoles to ensure everything has + * reached the console in case of a kernel crash. + */ +static struct cont { + char buf[LOG_LINE_MAX]; + size_t len; /* length == 0 means unused buffer */ + size_t cons; /* bytes written to console */ + 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 facility of first message */ + enum log_flags flags; /* prefix, newline flags */ + bool flushed:1; /* buffer sealed and committed */ +} cont; + +static void cont_flush(enum log_flags flags) +{ + if (cont.flushed) + return; + if (cont.len == 0) + return; + + if (cont.cons) { + /* + * If a fragment of this line was directly flushed to the + * console; wait for the console to pick up the rest of the + * line. LOG_NOCONS suppresses a duplicated output. + */ + log_store(cont.facility, cont.level, flags | LOG_NOCONS, + cont.ts_nsec, NULL, 0, cont.buf, cont.len); + cont.flags = flags; + cont.flushed = true; + } else { + /* + * If no fragment of this line ever reached the console, + * just submit it to the store and free the buffer. + */ + log_store(cont.facility, cont.level, flags, 0, + NULL, 0, cont.buf, cont.len); + cont.len = 0; + } +} + +static bool cont_add(int facility, int level, const char *text, size_t len) +{ + if (cont.len && cont.flushed) + return false; + + if (cont.len + len > sizeof(cont.buf)) { + /* the line gets too long, split it up in separate records */ + cont_flush(LOG_CONT); + return false; + } + + if (!cont.len) { + cont.facility = facility; + cont.level = level; + cont.owner = current; + cont.ts_nsec = local_clock(); + cont.flags = 0; + cont.cons = 0; + cont.flushed = false; + } + + memcpy(cont.buf + cont.len, text, len); + cont.len += len; + + if (cont.len > (sizeof(cont.buf) * 80) / 100) + cont_flush(LOG_CONT); + + return true; +} + +static size_t cont_print_text(char *text, size_t size) +{ + size_t textlen = 0; + size_t len; + + if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) { + textlen += print_time(cont.ts_nsec, text); + size -= textlen; + } + + len = cont.len - cont.cons; + if (len > 0) { + if (len+1 > size) + len = size-1; + memcpy(text + textlen, cont.buf + cont.cons, len); + textlen += len; + cont.cons = cont.len; + } + + if (cont.flushed) { + if (cont.flags & LOG_NEWLINE) + text[textlen++] = '\n'; + /* got everything, release buffer */ + cont.len = 0; + } + return textlen; +} + #ifdef CONFIG_SECURITY_DMESG_RESTRICT int dmesg_restrict = 1; #else @@ -949,29 +1072,6 @@ 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 -module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); - -static size_t print_time(u64 ts, char *buf) -{ - unsigned long rem_nsec; - - if (!printk_time) - return 0; - - rem_nsec = do_div(ts, 1000000000); - - if (!buf) - return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts); - - return sprintf(buf, "[%5lu.%06lu] ", - (unsigned long)ts, rem_nsec / 1000); -} - #ifdef CONFIG_PRINTK_PROCESS static size_t print_process(const struct log *msg, char *buf) { @@ -1494,115 +1594,6 @@ static inline void printk_delay(void) } } -/* - * Continuation lines are buffered, and not committed to the record buffer - * until the line is complete, or a race forces it. The line fragments - * though, are printed immediately to the consoles to ensure everything has - * reached the console in case of a kernel crash. - */ -static struct cont { - char buf[LOG_LINE_MAX]; - size_t len; /* length == 0 means unused buffer */ - size_t cons; /* bytes written to console */ - 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 */ - enum log_flags flags; /* prefix, newline flags */ - bool flushed:1; /* buffer sealed and committed */ - int cpu; -} cont; - -static void cont_flush(enum log_flags flags) -{ - if (cont.flushed) - return; - if (cont.len == 0) - return; - - if (cont.cons) { - /* - * If a fragment of this line was directly flushed to the - * console; wait for the console to pick up the rest of the - * line. LOG_NOCONS suppresses a duplicated output. - */ - log_store(cont.facility, cont.level, flags | LOG_NOCONS, - cont.ts_nsec, NULL, 0, cont.buf, cont.len, cont.cpu); - cont.flags = flags; - cont.flushed = true; - } else { - /* - * If no fragment of this line ever reached the console, - * just submit it to the store and free the buffer. - */ - log_store(cont.facility, cont.level, flags, 0, - NULL, 0, cont.buf, cont.len, cont.cpu); - cont.len = 0; - } -} - -static bool cont_add(int facility, int level, const char *text, size_t len) -{ - if (cont.len && cont.flushed) - return false; - - if (cont.len + len > sizeof(cont.buf)) { - /* the line gets too long, split it up in separate records */ - cont_flush(LOG_CONT); - return false; - } - - if (!cont.len) { - cont.facility = facility; - cont.level = level; - cont.owner = current; - cont.ts_nsec = local_clock(); - cont.flags = 0; - cont.cons = 0; - cont.flushed = false; - } - - memcpy(cont.buf + cont.len, text, len); - cont.len += len; - - if (cont.len > (sizeof(cont.buf) * 80) / 100) - cont_flush(LOG_CONT); - - return true; -} - -static size_t cont_print_text(char *text, size_t size) -{ - size_t textlen = 0; - size_t len; - - if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) { - textlen += print_time(cont.ts_nsec, text); -#ifdef CONFIG_PRINTK_PROCESS - *(text+textlen) = ' '; - textlen += print_process(NULL, NULL); -#endif - size -= textlen; - } - - len = cont.len - cont.cons; - if (len > 0) { - if (len+1 > size) - len = size-1; - memcpy(text + textlen, cont.buf + cont.cons, len); - textlen += len; - cont.cons = cont.len; - } - - if (cont.flushed) { - if (cont.flags & LOG_NEWLINE) - text[textlen++] = '\n'; - /* got everything, release buffer */ - cont.len = 0; - } - return textlen; -} - asmlinkage int vprintk_emit(int facility, int level, const char *dict, size_t dictlen, const char *fmt, va_list args) -- 2.7.4 From e221c136cf53246dc738b469174d0657b5c2dbc8 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Thu, 2 Jul 2015 16:54:51 +0200 Subject: [PATCH 03/16] printk: add one function for storing log in proper format Preparation commit for future changes purpose. Separate code responsible for storing log message in proper format from operations on consoles by putting it in another function. Change-Id: Idead21785b8e8a57cd504471d0537a399b4d9cd9 Signed-off-by: Marcin Niesluchowski --- kernel/printk_kmsg.c | 176 ++++++++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 78 deletions(-) diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 7420cc9..fe93011 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -564,6 +564,102 @@ static size_t cont_print_text(char *text, size_t size) return textlen; } +static int log_format_and_store(int facility, int level, + const char *dict, size_t dictlen, + const char *fmt, va_list args) +{ + static char textbuf[LOG_LINE_MAX]; + char *text = textbuf; + size_t text_len = 0; + enum log_flags lflags = 0; + int printed_len = 0; + + /* + * The printf needs to come first; we need the syslog + * prefix which might be passed-in as a parameter. + */ + text_len = vscnprintf(text, sizeof(textbuf), fmt, args); + + /* mark and strip a trailing newline */ + if (text_len && text[text_len-1] == '\n') { + text_len--; + lflags |= LOG_NEWLINE; + } + + /* strip kernel syslog prefix and extract log level or control flags */ + if (facility == 0) { + int kern_level = printk_get_level(text); + + if (kern_level) { + const char *end_of_header = printk_skip_level(text); + + switch (kern_level) { + case '0' ... '7': + if (level == LOGLEVEL_DEFAULT) + level = kern_level - '0'; + /* fallthrough */ + case 'd': /* KERN_DEFAULT */ + lflags |= LOG_PREFIX; + } + /* + * No need to check length here because vscnprintf + * put '\0' at the end of the string. Only valid and + * newly printed level is detected. + */ + text_len -= end_of_header - text; + text = (char *)end_of_header; + } + } + + if (level == LOGLEVEL_DEFAULT) + level = default_message_loglevel; + + if (dict) + lflags |= LOG_PREFIX|LOG_NEWLINE; + + if (!(lflags & LOG_NEWLINE)) { + /* + * Flush the conflicting buffer. An earlier newline was missing, + * or another task also prints continuation lines. + */ + if (cont.len && (lflags & LOG_PREFIX || cont.owner != current)) + cont_flush(LOG_NEWLINE); + + /* buffer line if possible, otherwise store it right away */ + if (cont_add(facility, level, text, text_len)) + printed_len += text_len; + else + printed_len += log_store(facility, level, + lflags | LOG_CONT, 0, + dict, dictlen, text, text_len); + } else { + bool stored = false; + + /* + * If an earlier newline was missing and it was the same task, + * either merge it with the current buffer and flush, or if + * there was a race with interrupts (prefix == true) then just + * flush it out and store this line separately. + * If the preceding printk was from a different task and missed + * a newline, flush and append the newline. + */ + if (cont.len) { + if (cont.owner == current && !(lflags & LOG_PREFIX)) + stored = cont_add(facility, level, text, + text_len); + cont_flush(LOG_NEWLINE); + } + + if (stored) + printed_len += text_len; + else + printed_len += log_store(facility, level, + lflags, 0, dict, dictlen, + text, text_len); + } + return printed_len; +} + #ifdef CONFIG_SECURITY_DMESG_RESTRICT int dmesg_restrict = 1; #else @@ -1599,10 +1695,6 @@ asmlinkage int vprintk_emit(int facility, int level, const char *fmt, va_list args) { static int recursion_bug; - static char textbuf[LOG_LINE_MAX]; - char *text = textbuf; - size_t text_len; - enum log_flags lflags = 0; unsigned long flags; int this_cpu; int printed_len = 0; @@ -1647,80 +1739,8 @@ asmlinkage int vprintk_emit(int facility, int level, NULL, 0, recursion_msg, printed_len, logbuf_cpu); } - /* - * The printf needs to come first; we need the syslog - * prefix which might be passed-in as a parameter. - */ - text_len = vscnprintf(text, sizeof(textbuf), fmt, args); - -#ifdef CONFIG_DEBUG_LL - printascii(text); -#endif - - /* mark and strip a trailing newline */ - if (text_len && text[text_len-1] == '\n') { - text_len--; - lflags |= LOG_NEWLINE; - } - - /* strip kernel syslog prefix and extract log level or control flags */ - if (facility == 0) { - int kern_level = printk_get_level(text); - - if (kern_level) { - const char *end_of_header = printk_skip_level(text); - switch (kern_level) { - case '0' ... '7': - if (level == -1) - level = kern_level - '0'; - case 'd': /* KERN_DEFAULT */ - lflags |= LOG_PREFIX; - case 'c': /* KERN_CONT */ - break; - } - text_len -= end_of_header - text; - text = (char *)end_of_header; - } - } - - if (level == -1) - level = default_message_loglevel; - - if (dict) - lflags |= LOG_PREFIX|LOG_NEWLINE; - - if (!(lflags & LOG_NEWLINE)) { - /* - * Flush the conflicting buffer. An earlier newline was missing, - * or another task also prints continuation lines. - */ - if (cont.len && (lflags & LOG_PREFIX || cont.owner != current)) - cont_flush(LOG_NEWLINE); - - /* buffer line if possible, otherwise store it right away */ - if (!cont_add(facility, level, text, text_len)) - log_store(facility, level, lflags | LOG_CONT, 0, - dict, dictlen, text, text_len, logbuf_cpu); - } else { - bool stored = false; - - /* - * If an earlier newline was missing and it was the same task, - * either merge it with the current buffer and flush, or if - * there was a race with interrupts (prefix == true) then just - * flush it out and store this line separately. - */ - if (cont.len && cont.owner == current) { - if (!(lflags & LOG_PREFIX)) - stored = cont_add(facility, level, text, text_len); - cont_flush(LOG_NEWLINE); - } - - if (!stored) - log_store(facility, level, lflags, 0, - dict, dictlen, text, text_len, logbuf_cpu); - } - printed_len += text_len; + printed_len += log_format_and_store(facility, level, dict, dictlen, + fmt, args); /* * Try to acquire and then immediately release the console semaphore. -- 2.7.4 From fec77d9a5d4be649d8859d3f45d8a17a96edc825 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Wed, 29 Apr 2015 19:37:05 +0200 Subject: [PATCH 04/16] kmsg: introduce additional kmsg devices support kmsg device provides operations on cyclic logging buffer used mainly by kernel but also in userspace by privileged processes. Additional kmsg devices keep the same log format but may be added dynamically with custom size. Signed-off-by: Marcin Niesluchowski Change-Id: Ideada11d07e2a9c9b8c342a1027a350c9531d6f1 --- fs/proc/kmsg.c | 10 +- kernel/printk_kmsg.c | 728 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 498 insertions(+), 240 deletions(-) diff --git a/fs/proc/kmsg.c b/fs/proc/kmsg.c index bdfabda..6801d2c 100644 --- a/fs/proc/kmsg.c +++ b/fs/proc/kmsg.c @@ -17,7 +17,11 @@ #include #include -extern wait_queue_head_t log_wait; +#ifdef CONFIG_MULTIPLE_KMSG +extern wait_queue_head_t *log_wait; +#else +extern wait_queue_head_t log_wait; +#endif static int kmsg_open(struct inode * inode, struct file * file) { @@ -41,7 +45,11 @@ static ssize_t kmsg_read(struct file *file, char __user *buf, static unsigned int kmsg_poll(struct file *file, poll_table *wait) { +#ifdef CONFIG_MULTIPLE_KMSG + poll_wait(file, log_wait, wait); +#else poll_wait(file, &log_wait, wait); +#endif if (do_syslog(SYSLOG_ACTION_SIZE_UNREAD, NULL, 0, SYSLOG_FROM_PROC)) return POLLIN | POLLRDNORM; return 0; diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index fe93011..de15ce1 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -226,28 +226,36 @@ struct log { #endif }; +struct log_buffer { +#ifdef CONFIG_PRINTK + struct list_head list; /* kmsg as head of the list */ + char *buf; /* cyclic log buffer */ + u32 len; /* buffer length */ + wait_queue_head_t wait; /* wait queue for kmsg buffer */ +#endif /* - * The logbuf_lock protects kmsg buffer, indices, counters. It is also - * used in interesting ways to provide interlocking in console_unlock(); + * The lock protects kmsg buffer, indices, counters. This can be taken within + * the scheduler's rq lock. It must be released before calling console_unlock() + * or anything else that might wake up a process. */ -static DEFINE_RAW_SPINLOCK(logbuf_lock); + raw_spinlock_t lock; + u64 first_seq; /* sequence number of the first record stored */ + u32 first_idx; /* index of the first record stored */ +/* sequence number of the next record to store */ + u64 next_seq; +#ifdef CONFIG_PRINTK + u32 next_idx; /* index of the next record to store */ + int minor; /* minor representing buffer device */ +#endif +}; #ifdef CONFIG_PRINTK -DECLARE_WAIT_QUEUE_HEAD(log_wait); /* the next printk record to read by syslog(READ) or /proc/kmsg */ static u64 syslog_seq; static u32 syslog_idx; static enum log_flags syslog_prev; static size_t syslog_partial; -/* index and sequence number of the first record stored in the buffer */ -static u64 log_first_seq; -static u32 log_first_idx; - -/* index and sequence number of the next record to store in the buffer */ -static u64 log_next_seq; -static u32 log_next_idx; - /* the next printk record to write to the console */ static u64 console_seq; static u32 console_idx; @@ -270,12 +278,37 @@ static u32 clear_idx; #else #define LOG_ALIGN __alignof__(struct log) #endif -#define __LOG_BUF_LEN (1 << CONFIG_LOG_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; +#define __LOG_BUF_K_LEN (1 << CONFIG_LOG_BUF_SHIFT) +static char __log_buf_k[__LOG_BUF_K_LEN] __aligned(LOG_ALIGN); + +static struct log_buffer log_buf = { + .list = LIST_HEAD_INIT(log_buf.list), + .buf = __log_buf_k, + .len = __LOG_BUF_K_LEN, + .lock = __RAW_SPIN_LOCK_UNLOCKED(log_buf.lock), + .wait = __WAIT_QUEUE_HEAD_INITIALIZER(log_buf.wait), + .first_seq = 0, + .first_idx = 0, + .next_seq = 0, + .next_idx = 0, + .minor = 0, +}; + +wait_queue_head_t *log_wait = &log_buf.wait; + +/* Return log buffer address */ +char *log_buf_addr_get(void) +{ + return log_buf.buf; +} + +/* Return log buffer size */ +u32 log_buf_len_get(void) +{ + return log_buf.len; +} -/* cpu currently holding logbuf_lock */ +/* cpu currently holding log_buf.lock */ static volatile unsigned int logbuf_cpu = UINT_MAX; /* human readable text of the record */ @@ -291,23 +324,23 @@ static char *log_dict(const struct log *msg) } /* get record by index; idx must point to valid msg */ -static struct log *log_from_idx(u32 idx) +static struct log *log_from_idx(struct log_buffer *log_b, u32 idx) { - struct log *msg = (struct log *)(log_buf + idx); + struct log *msg = (struct log *)(log_b->buf + idx); /* * A length == 0 record is the end of buffer marker. Wrap around and * read the message at the start of the buffer. */ if (!msg->len) - return (struct log *)log_buf; + return (struct log *)log_b->buf; return msg; } /* get next record; idx must point to valid msg */ -static u32 log_next(u32 idx) +static u32 log_next(struct log_buffer *log_b, u32 idx) { - struct log *msg = (struct log *)(log_buf + idx); + struct log *msg = (struct log *)(log_b->buf + idx); /* length == 0 indicates the end of the buffer; wrap */ /* @@ -316,14 +349,94 @@ static u32 log_next(u32 idx) * return the one after that. */ if (!msg->len) { - msg = (struct log *)log_buf; + msg = (struct log *)log_b->buf; return msg->len; } return idx + msg->len; } +/* + * Check whether there is enough free space for the given message. + * + * The same values of first_idx and next_idx mean that the buffer + * is either empty or full. + * + * If the buffer is empty, we must respect the position of the indexes. + * They cannot be reset to the beginning of the buffer. + */ +static int logbuf_has_space(struct log_buffer *log_b, u32 msg_size, bool empty) +{ + u32 free; + + if (log_b->next_idx > log_b->first_idx || empty) + free = max(log_b->len - log_b->next_idx, log_b->first_idx); + else + free = log_b->first_idx - log_b->next_idx; + + /* + * We need space also for an empty header that signalizes wrapping + * of the buffer. + */ + return free >= msg_size + sizeof(struct log); +} + +static int log_make_free_space(struct log_buffer *log_b, u32 msg_size) +{ + while (log_b->first_seq < log_b->next_seq) { + if (logbuf_has_space(log_b, msg_size, false)) + return 0; + /* drop old messages until we have enough contiguous space */ + log_b->first_idx = log_next(log_b, log_b->first_idx); + log_b->first_seq++; + } + + /* sequence numbers are equal, so the log buffer is empty */ + if (logbuf_has_space(log_b, msg_size, true)) + return 0; + + return -ENOMEM; +} + +/* compute the message size including the padding bytes */ +static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len) +{ + u32 size; + + size = sizeof(struct log) + text_len + dict_len; + *pad_len = (-size) & (LOG_ALIGN - 1); + size += *pad_len; + + return size; +} + +/* + * Define how much of the log buffer we could take at maximum. The value + * must be greater than two. Note that only half of the buffer is available + * when the index points to the middle. + */ +#define MAX_LOG_TAKE_PART 4 +static const char trunc_msg[] = ""; + + +static u32 truncate_msg(struct log_buffer *log_b, + u16 *text_len, + u16 *dict_len, u32 *pad_len) +{ + /* + * The message should not take the whole buffer. Otherwise, it might + * get removed too soon. + */ + u32 max_text_len = log_b->len / MAX_LOG_TAKE_PART; + if (*text_len > max_text_len) + *text_len = max_text_len; + /* disable the "dict" completely */ + *dict_len = 0; + /* compute the size again, count also the warning message */ + return msg_used_size(*text_len + strlen(trunc_msg), 0, pad_len); +} + #ifdef CONFIG_SEC_LOG -static char initial_log_buf[__LOG_BUF_LEN]; +static char initial_log_buf[__LOG_BUF_K_LEN]; static unsigned int initial_log_idx = 0; static void (*log_text_hook)(char *text, size_t size); static char *seclog_buf; @@ -334,14 +447,14 @@ void register_log_text_hook(void (*f)(char *text, size_t size), char * buf, unsigned *position, size_t bufsize) { unsigned long flags; - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); if (buf && bufsize) { seclog_buf = buf; seclog_ptr = position; seclog_size = bufsize; log_text_hook = f; } - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); } EXPORT_SYMBOL(register_log_text_hook); static size_t msg_print_text(const struct log *msg, enum log_flags prev, @@ -349,7 +462,8 @@ static size_t msg_print_text(const struct log *msg, enum log_flags prev, #endif /* insert record into the buffer, discard old ones, update heads */ -static void log_store(int facility, int level, +static int log_store(struct log_buffer *log_b, + int facility, int level, enum log_flags flags, u64 ts_nsec, const char *dict, u16 dict_len, const char *text, u16 text_len, int cpu) @@ -358,38 +472,29 @@ static void log_store(int facility, int level, u32 size, pad_len; /* number of '\0' padding bytes to next message */ - size = sizeof(struct log) + text_len + dict_len; - pad_len = (-size) & (LOG_ALIGN - 1); - size += pad_len; - - while (log_first_seq < log_next_seq) { - u32 free; - - if (log_next_idx > log_first_idx) - free = max(log_buf_len - log_next_idx, log_first_idx); - else - free = log_first_idx - log_next_idx; - - if (free > size + sizeof(struct log)) - break; + size = msg_used_size(text_len, dict_len, &pad_len); - /* drop old messages until we have enough contiuous space */ - log_first_idx = log_next(log_first_idx); - log_first_seq++; + if (log_make_free_space(log_b, size)) { + /* truncate the message if it is too long for empty buffer */ + size = truncate_msg(log_b, &text_len, &dict_len, &pad_len); + /* survive when the log buffer is too small for trunc_msg */ + if (log_make_free_space(log_b, size)) + return 0; } - if (log_next_idx + size + sizeof(struct log) >= log_buf_len) { + if (log_b->next_idx + size + sizeof(struct log) > log_b->len) { /* * This message + an additional empty header does not fit * at the end of the buffer. Add an empty header with len == 0 * to signify a wrap around. */ - memset(log_buf + log_next_idx, 0, sizeof(struct log)); - log_next_idx = 0; + memset(log_b->buf + log_b->next_idx, 0, + sizeof(struct log)); + log_b->next_idx = 0; } /* fill message */ - msg = (struct log *)(log_buf + log_next_idx); + msg = (struct log *)(log_b->buf + log_b->next_idx); memcpy(log_text(msg), text, text_len); msg->text_len = text_len; memcpy(log_dict(msg), dict, dict_len); @@ -426,7 +531,7 @@ static void log_store(int facility, int level, sec_text, 1024); log_text_hook(sec_text, size); - } else if (initial_log_idx < (__LOG_BUF_LEN)) { + } else if (initial_log_idx < (__LOG_BUF_K_LEN)) { /* Storing of kernel boot logs prior to log_text_hook() * registration */ @@ -437,8 +542,10 @@ static void log_store(int facility, int level, } #endif /* insert message */ - log_next_idx += msg->len; - log_next_seq++; + log_b->next_idx += msg->len; + log_b->next_seq++; + + return msg->text_len; } static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME); @@ -476,6 +583,7 @@ static struct cont { u8 facility; /* log facility of first message */ enum log_flags flags; /* prefix, newline flags */ bool flushed:1; /* buffer sealed and committed */ + int cpu; } cont; static void cont_flush(enum log_flags flags) @@ -491,8 +599,9 @@ static void cont_flush(enum log_flags flags) * console; wait for the console to pick up the rest of the * line. LOG_NOCONS suppresses a duplicated output. */ - log_store(cont.facility, cont.level, flags | LOG_NOCONS, - cont.ts_nsec, NULL, 0, cont.buf, cont.len); + log_store(&log_buf, cont.facility, cont.level, + flags | LOG_NOCONS, cont.ts_nsec, NULL, 0, + cont.buf, cont.len, cont.cpu); cont.flags = flags; cont.flushed = true; } else { @@ -500,8 +609,8 @@ static void cont_flush(enum log_flags flags) * If no fragment of this line ever reached the console, * just submit it to the store and free the buffer. */ - log_store(cont.facility, cont.level, flags, 0, - NULL, 0, cont.buf, cont.len); + log_store(&log_buf, cont.facility, cont.level, flags, 0, + NULL, 0, cont.buf, cont.len, cont.cpu); cont.len = 0; } } @@ -564,9 +673,10 @@ static size_t cont_print_text(char *text, size_t size) return textlen; } -static int log_format_and_store(int facility, int level, +static int log_format_and_store(struct log_buffer *log_b, + int facility, int level, const char *dict, size_t dictlen, - const char *fmt, va_list args) + const char *fmt, int cpu, va_list args) { static char textbuf[LOG_LINE_MAX]; char *text = textbuf; @@ -595,7 +705,7 @@ static int log_format_and_store(int facility, int level, switch (kern_level) { case '0' ... '7': - if (level == LOGLEVEL_DEFAULT) + if (level == DEFAULT_MESSAGE_LOGLEVEL) level = kern_level - '0'; /* fallthrough */ case 'd': /* KERN_DEFAULT */ @@ -611,12 +721,16 @@ static int log_format_and_store(int facility, int level, } } - if (level == LOGLEVEL_DEFAULT) + if (level == DEFAULT_MESSAGE_LOGLEVEL) level = default_message_loglevel; if (dict) lflags |= LOG_PREFIX|LOG_NEWLINE; + if (log_b != &log_buf) + return log_store(log_b, facility, level, lflags, 0, + dict, dictlen, text, text_len, cpu); + if (!(lflags & LOG_NEWLINE)) { /* * Flush the conflicting buffer. An earlier newline was missing, @@ -629,9 +743,10 @@ static int log_format_and_store(int facility, int level, if (cont_add(facility, level, text, text_len)) printed_len += text_len; else - printed_len += log_store(facility, level, + printed_len += log_store(log_b, facility, level, lflags | LOG_CONT, 0, - dict, dictlen, text, text_len); + dict, dictlen, text, + text_len, cpu); } else { bool stored = false; @@ -653,9 +768,9 @@ static int log_format_and_store(int facility, int level, if (stored) printed_len += text_len; else - printed_len += log_store(facility, level, + printed_len += log_store(log_b, facility, level, lflags, 0, dict, dictlen, - text, text_len); + text, text_len, cpu); } return printed_len; } @@ -721,6 +836,34 @@ struct devkmsg_user { char buf[CONSOLE_EXT_LOG_MAX]; }; +static int kmsg_sys_write(int minor, int level, const char *fmt, ...) +{ + va_list args; + int ret = -ENXIO; + struct log_buffer *log_b; + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor != minor) + continue; + + raw_spin_lock(&log_b->lock); + + va_start(args, fmt); + log_format_and_store(log_b, 1 /* LOG_USER */, level, + NULL, 0, fmt, smp_processor_id(), args); + va_end(args); + wake_up_interruptible(&log_b->wait); + + raw_spin_unlock(&log_b->lock); + + ret = 0; + break; + } + rcu_read_unlock(); + return ret; +} + static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, unsigned long count, loff_t pos) { @@ -730,6 +873,7 @@ static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, int facility = 1; /* LOG_USER */ size_t len = iov_length(iv, count); ssize_t ret = len; + int minor = iminor(iocb->ki_filp->f_inode); if (len > LOG_LINE_MAX) return -EINVAL; @@ -771,14 +915,22 @@ static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, } line[len] = '\0'; - printk_emit(facility, level, NULL, 0, "%s", line); + if (minor == log_buf.minor) { + printk_emit(facility, level, NULL, 0, "%s", line); + } else { + int error = kmsg_sys_write(minor, level, "%s", line); + + if (error) + ret = error; + } + out: kfree(buf); return ret; } -static ssize_t devkmsg_read(struct file *file, char __user *buf, - size_t count, loff_t *ppos) +static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file, + char __user *buf, size_t count, loff_t *ppos) { struct devkmsg_user *user = file->private_data; struct log *msg; @@ -789,41 +941,38 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, size_t len; ssize_t ret; - if (!user) - return -EBADF; - p = user->buf; e = user->buf + sizeof(user->buf); ret = mutex_lock_interruptible(&user->lock); if (ret) return ret; - raw_spin_lock_irq(&logbuf_lock); - while (user->seq == log_next_seq) { + raw_spin_lock_irq(&log_b->lock); + while (user->seq == log_b->next_seq) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_b->lock); goto out; } - raw_spin_unlock_irq(&logbuf_lock); - ret = wait_event_interruptible(log_wait, - user->seq != log_next_seq); + raw_spin_unlock_irq(&log_b->lock); + ret = wait_event_interruptible(log_b->wait, + user->seq != log_b->next_seq); if (ret) goto out; - raw_spin_lock_irq(&logbuf_lock); + raw_spin_lock_irq(&log_b->lock); } - if (user->seq < log_first_seq) { + if (user->seq < log_b->first_seq) { /* our last seen message is gone, return error and reset */ - user->idx = log_first_idx; - user->seq = log_first_seq; + user->idx = log_b->first_idx; + user->seq = log_b->first_seq; ret = -EPIPE; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_b->lock); goto out; } - msg = log_from_idx(user->idx); + msg = log_from_idx(log_b, user->idx); ts_usec = msg->ts_nsec; do_div(ts_usec, 1000); @@ -884,9 +1033,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, append_char(&p, e, '\n'); } - user->idx = log_next(user->idx); + user->idx = log_next(log_b, user->idx); user->seq++; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_b->lock); len = p - user->buf; if (len > count) { @@ -902,26 +1051,53 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, out: mutex_unlock(&user->lock); return ret; + } -static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) +static ssize_t devkmsg_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) { struct devkmsg_user *user = file->private_data; - loff_t ret = 0; + ssize_t ret = -ENXIO; + int minor = iminor(file->f_inode); + struct log_buffer *log_b; if (!user) return -EBADF; - if (offset) - return -ESPIPE; - raw_spin_lock_irq(&logbuf_lock); + if (minor == log_buf.minor) + return kmsg_read(&log_buf, file, buf, count, ppos); + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + ret = kmsg_read(log_b, file, buf, count, ppos); + break; + } + } + rcu_read_unlock(); + return ret; +} + +static loff_t kmsg_llseek(struct log_buffer *log_b, struct file *file, + int whence) +{ + struct devkmsg_user *user = file->private_data; + loff_t ret = 0; + + raw_spin_lock_irq(&log_b->lock); switch (whence) { case SEEK_SET: /* the first record */ - user->idx = log_first_idx; - user->seq = log_first_seq; + user->idx = log_b->first_idx; + user->seq = log_b->first_seq; break; case SEEK_DATA: + /* no clear index for kmsg_sys buffers */ + if (log_b != &log_buf) { + ret = -EINVAL; + break; + } /* * The first record after the last SYSLOG_ACTION_CLEAR, * like issued by 'dmesg -c'. Reading /dev/kmsg itself @@ -932,52 +1108,90 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) break; case SEEK_END: /* after the last record */ - user->idx = log_next_idx; - user->seq = log_next_seq; + user->idx = log_b->next_idx; + user->seq = log_b->next_seq; break; default: ret = -EINVAL; } - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_b->lock); return ret; } -static unsigned int devkmsg_poll(struct file *file, poll_table *wait) +static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) { struct devkmsg_user *user = file->private_data; - int ret = 0; + loff_t ret = -ENXIO; + int minor = iminor(file->f_inode); + struct log_buffer *log_b; if (!user) - return POLLERR|POLLNVAL; + return -EBADF; + if (offset) + return -ESPIPE; + + if (minor == log_buf.minor) + return kmsg_llseek(&log_buf, file, whence); + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + ret = kmsg_llseek(log_b, file, whence); + break; + } + } + rcu_read_unlock(); + return ret; +} + +static unsigned int kmsg_poll(struct log_buffer *log_b, + struct file *file, poll_table *wait) +{ + struct devkmsg_user *user = file->private_data; + int ret = 0; - poll_wait(file, &log_wait, wait); + poll_wait(file, &log_b->wait, wait); - raw_spin_lock_irq(&logbuf_lock); - if (user->seq < log_next_seq) { + raw_spin_lock_irq(&log_b->lock); + if (user->seq < log_b->next_seq) { /* return error when data has vanished underneath us */ - if (user->seq < log_first_seq) + if (user->seq < log_b->first_seq) ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI; else ret = POLLIN|POLLRDNORM; } - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_b->lock); return ret; } -static int devkmsg_open(struct inode *inode, struct file *file) +static unsigned int devkmsg_poll(struct file *file, poll_table *wait) { - struct devkmsg_user *user; - int err; + struct devkmsg_user *user = file->private_data; + int ret = POLLERR|POLLNVAL; + int minor = iminor(file->f_inode); + struct log_buffer *log_b; - /* write-only does not need any file context */ - if ((file->f_flags & O_ACCMODE) == O_WRONLY) - return 0; + if (!user) + return POLLERR|POLLNVAL; + + if (minor == log_buf.minor) + return kmsg_poll(&log_buf, file, wait); + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + ret = kmsg_poll(log_b, file, wait); + break; + } + } + rcu_read_unlock(); + return ret; +} - err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL, - SYSLOG_FROM_READER); - if (err) - return err; +static int kmsg_open(struct log_buffer *log_b, struct file *file) +{ + struct devkmsg_user *user; user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); if (!user) @@ -985,15 +1199,45 @@ static int devkmsg_open(struct inode *inode, struct file *file) mutex_init(&user->lock); - raw_spin_lock_irq(&logbuf_lock); - user->idx = log_first_idx; - user->seq = log_first_seq; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_lock_irq(&log_b->lock); + user->idx = log_b->first_idx; + user->seq = log_b->first_seq; + raw_spin_unlock_irq(&log_b->lock); file->private_data = user; return 0; } +static int devkmsg_open(struct inode *inode, struct file *file) +{ + int ret = -ENXIO; + int minor = iminor(file->f_inode); + struct log_buffer *log_b; + + /* write-only does not need any file context */ + if ((file->f_flags & O_ACCMODE) == O_WRONLY) + return 0; + + if (minor == log_buf.minor) { + ret = check_syslog_permissions(SYSLOG_ACTION_READ_ALL, + SYSLOG_FROM_READER); + if (ret) + return ret; + + return kmsg_open(&log_buf, file); + } + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + ret = kmsg_open(log_b, file); + break; + } + } + rcu_read_unlock(); + return ret; +} + static int devkmsg_release(struct inode *inode, struct file *file) { struct devkmsg_user *user = file->private_data; @@ -1027,9 +1271,11 @@ const struct file_operations kmsg_fops = { void log_buf_kexec_setup(void) { VMCOREINFO_SYMBOL(log_buf); - VMCOREINFO_SYMBOL(log_buf_len); - VMCOREINFO_SYMBOL(log_first_idx); - VMCOREINFO_SYMBOL(log_next_idx); + VMCOREINFO_STRUCT_SIZE(log_buffer); + VMCOREINFO_OFFSET(log_buffer, buf); + VMCOREINFO_OFFSET(log_buffer, len); + VMCOREINFO_OFFSET(log_buffer, first_idx); + VMCOREINFO_OFFSET(log_buffer, next_idx); /* * Export struct log size and field offsets. User space tools can * parse it and detect any changes to structure down the line. @@ -1042,7 +1288,7 @@ void log_buf_kexec_setup(void) } #endif -/* requested log_buf_len from kernel cmdline */ +/* 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 */ @@ -1052,7 +1298,7 @@ static int __init log_buf_len_setup(char *str) if (size) size = roundup_pow_of_two(size); - if (size > log_buf_len) + if (size > log_buf.len) new_log_buf_len = size; return 0; @@ -1080,22 +1326,22 @@ void __init setup_log_buf(int early) } if (unlikely(!new_log_buf)) { - pr_err("log_buf_len: %ld bytes not available\n", + pr_err("log_buf.len: %ld bytes not available\n", new_log_buf_len); return; } - raw_spin_lock_irqsave(&logbuf_lock, flags); - log_buf_len = new_log_buf_len; - log_buf = new_log_buf; + raw_spin_lock_irqsave(&log_buf.lock, flags); + log_buf.len = new_log_buf_len; + log_buf.buf = new_log_buf; new_log_buf_len = 0; - free = __LOG_BUF_LEN - log_next_idx; - memcpy(log_buf, __log_buf, __LOG_BUF_LEN); - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + free = __LOG_BUF_K_LEN - log_buf.next_idx; + memcpy(log_buf.buf, __log_buf_k, __LOG_BUF_K_LEN); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); - pr_info("log_buf_len: %d\n", log_buf_len); + pr_info("log_buf.len: %d\n", log_buf.len); pr_info("early log buf free: %d(%d%%)\n", - free, (free * 100) / __LOG_BUF_LEN); + free, (free * 100) / __LOG_BUF_K_LEN); } static bool __read_mostly ignore_loglevel; @@ -1281,26 +1527,26 @@ static int syslog_print(char __user *buf, int size) size_t n; size_t skip; - raw_spin_lock_irq(&logbuf_lock); - if (syslog_seq < log_first_seq) { + raw_spin_lock_irq(&log_buf.lock); + if (syslog_seq < log_buf.first_seq) { /* messages are gone, move to first one */ - syslog_seq = log_first_seq; - syslog_idx = log_first_idx; + syslog_seq = log_buf.first_seq; + syslog_idx = log_buf.first_idx; syslog_prev = 0; syslog_partial = 0; } - if (syslog_seq == log_next_seq) { - raw_spin_unlock_irq(&logbuf_lock); + if (syslog_seq == log_buf.next_seq) { + raw_spin_unlock_irq(&log_buf.lock); break; } skip = syslog_partial; - msg = log_from_idx(syslog_idx); + msg = log_from_idx(&log_buf, syslog_idx); n = msg_print_text(msg, syslog_prev, true, text, LOG_LINE_MAX + PREFIX_MAX); if (n - syslog_partial <= size) { /* message fits into buffer, move forward */ - syslog_idx = log_next(syslog_idx); + syslog_idx = log_next(&log_buf, syslog_idx); syslog_seq++; syslog_prev = msg->flags; n -= syslog_partial; @@ -1311,7 +1557,7 @@ static int syslog_print(char __user *buf, int size) syslog_partial += n; } else n = 0; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_buf.lock); if (!n) break; @@ -1340,17 +1586,17 @@ static int syslog_print_all(char __user *buf, int size, bool clear) if (!text) return -ENOMEM; - raw_spin_lock_irq(&logbuf_lock); + raw_spin_lock_irq(&log_buf.lock); if (buf) { u64 next_seq; u64 seq; u32 idx; enum log_flags prev; - if (clear_seq < log_first_seq) { + if (clear_seq < log_buf.first_seq) { /* messages are gone, move to first available one */ - clear_seq = log_first_seq; - clear_idx = log_first_idx; + clear_seq = log_buf.first_seq; + clear_idx = log_buf.first_idx; } /* @@ -1360,12 +1606,12 @@ static int syslog_print_all(char __user *buf, int size, bool clear) seq = clear_seq; idx = clear_idx; prev = 0; - while (seq < log_next_seq) { - struct log *msg = log_from_idx(idx); + while (seq < log_buf.next_seq) { + struct log *msg = log_from_idx(&log_buf, idx); len += msg_print_text(msg, prev, true, NULL, 0); prev = msg->flags; - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; } @@ -1373,22 +1619,22 @@ static int syslog_print_all(char __user *buf, int size, bool clear) seq = clear_seq; idx = clear_idx; prev = 0; - while (len > size && seq < log_next_seq) { - struct log *msg = log_from_idx(idx); + while (len > size && seq < log_buf.next_seq) { + struct log *msg = log_from_idx(&log_buf, idx); len -= msg_print_text(msg, prev, true, NULL, 0); prev = msg->flags; - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; } /* last message fitting into this dump */ - next_seq = log_next_seq; + next_seq = log_buf.next_seq; len = 0; prev = 0; while (len >= 0 && seq < next_seq) { - struct log *msg = log_from_idx(idx); + struct log *msg = log_from_idx(&log_buf, idx); int textlen; textlen = msg_print_text(msg, prev, true, text, @@ -1397,31 +1643,31 @@ static int syslog_print_all(char __user *buf, int size, bool clear) len = textlen; break; } - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; prev = msg->flags; - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_buf.lock); if (copy_to_user(buf + len, text, textlen)) len = -EFAULT; else len += textlen; - raw_spin_lock_irq(&logbuf_lock); + raw_spin_lock_irq(&log_buf.lock); - if (seq < log_first_seq) { + if (seq < log_buf.first_seq) { /* messages are gone, move to next one */ - seq = log_first_seq; - idx = log_first_idx; + seq = log_buf.first_seq; + idx = log_buf.first_idx; prev = 0; } } } if (clear) { - clear_seq = log_next_seq; - clear_idx = log_next_idx; + clear_seq = log_buf.next_seq; + clear_idx = log_buf.next_idx; } - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_buf.lock); kfree(text); return len; @@ -1457,8 +1703,8 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) error = -EFAULT; goto out; } - error = wait_event_interruptible(log_wait, - syslog_seq != log_next_seq); + error = wait_event_interruptible(log_buf.wait, + syslog_seq != log_buf.next_seq); if (error) goto out; error = syslog_print(buf, len); @@ -1512,11 +1758,11 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) break; /* Number of chars in the log buffer */ case SYSLOG_ACTION_SIZE_UNREAD: - raw_spin_lock_irq(&logbuf_lock); - if (syslog_seq < log_first_seq) { + raw_spin_lock_irq(&log_buf.lock); + if (syslog_seq < log_buf.first_seq) { /* messages are gone, move to first one */ - syslog_seq = log_first_seq; - syslog_idx = log_first_idx; + syslog_seq = log_buf.first_seq; + syslog_idx = log_buf.first_idx; syslog_prev = 0; syslog_partial = 0; } @@ -1526,28 +1772,28 @@ 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_buf.next_idx - syslog_idx; } else { u64 seq = syslog_seq; u32 idx = syslog_idx; enum log_flags prev = syslog_prev; error = 0; - while (seq < log_next_seq) { - struct log *msg = log_from_idx(idx); + while (seq < log_buf.next_seq) { + struct log *msg = log_from_idx(&log_buf, idx); error += msg_print_text(msg, prev, true, NULL, 0); - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; prev = msg->flags; } error -= syslog_partial; } - raw_spin_unlock_irq(&logbuf_lock); + raw_spin_unlock_irq(&log_buf.lock); break; /* Size of the log buffer */ case SYSLOG_ACTION_SIZE_BUFFER: - error = log_buf_len; + error = log_buf.len; break; default: error = -EINVAL; @@ -1609,7 +1855,7 @@ static void zap_locks(void) debug_locks_off(); /* If a crash is occurring, make sure we can't deadlock */ - raw_spin_lock_init(&logbuf_lock); + raw_spin_lock_init(&log_buf.lock); /* And make sure that we print immediately */ sema_init(&console_sem, 1); } @@ -1645,12 +1891,12 @@ static inline int can_use_console(unsigned int cpu) * console_lock held, and 'console_locked' set) if it * is successful, false otherwise. * - * This gets called with the 'logbuf_lock' spinlock held and + * This gets called with the 'log_buf.lock' spinlock held and * interrupts disabled. It should return with 'lockbuf_lock' * released but interrupts still disabled. */ static int console_trylock_for_printk(unsigned int cpu) - __releases(&logbuf_lock) + __releases(&log_buf.lock) { int retval = 0, wake = 0; @@ -1670,7 +1916,7 @@ static int console_trylock_for_printk(unsigned int cpu) } } logbuf_cpu = UINT_MAX; - raw_spin_unlock(&logbuf_lock); + raw_spin_unlock(&log_buf.lock); if (wake) up(&console_sem); return retval; @@ -1725,7 +1971,7 @@ asmlinkage int vprintk_emit(int facility, int level, } lockdep_off(); - raw_spin_lock(&logbuf_lock); + raw_spin_lock(&log_buf.lock); logbuf_cpu = this_cpu; if (recursion_bug) { @@ -1733,21 +1979,21 @@ asmlinkage int vprintk_emit(int facility, int level, "BUG: recent printk recursion!"; recursion_bug = 0; - printed_len += strlen(recursion_msg); /* emit KERN_CRIT message */ - log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0, - NULL, 0, recursion_msg, printed_len, logbuf_cpu); + printed_len += log_store(&log_buf, 0, 2, LOG_PREFIX|LOG_NEWLINE, 0, + NULL, 0, recursion_msg, strlen(recursion_msg), this_cpu); + } - printed_len += log_format_and_store(facility, level, dict, dictlen, - fmt, args); + printed_len += log_format_and_store(&log_buf, facility, level, dict, dictlen, + fmt, this_cpu, args); /* * Try to acquire and then immediately release the console semaphore. * The release will print out buffers and wake up /dev/kmsg and syslog() * users. * - * The console_trylock_for_printk() function will release 'logbuf_lock' + * The console_trylock_for_printk() function will release 'log_buf.lock' * regardless of whether it actually gets the console semaphore or not. */ if (console_trylock_for_printk(this_cpu)) @@ -1828,15 +2074,19 @@ EXPORT_SYMBOL(printk); #define LOG_LINE_MAX 0 #define PREFIX_MAX 0 -#define LOG_LINE_MAX 0 + +static struct log_buffer log_buf = { + .lock = __RAW_SPIN_LOCK_UNLOCKED(log_buf.lock), + .first_seq = 0, + .first_idx = 0, + .next_seq = 0, +}; + static u64 syslog_seq; static u32 syslog_idx; static u64 console_seq; static u32 console_idx; static enum log_flags syslog_prev; -static u64 log_first_seq; -static u32 log_first_idx; -static u64 log_next_seq; static enum log_flags console_prev; static struct cont { size_t len; @@ -1844,8 +2094,8 @@ static struct cont { u8 level; bool flushed:1; } cont; -static struct log *log_from_idx(u32 idx) { return NULL; } -static u32 log_next(u32 idx) { return 0; } +static struct log *log_from_idx(struct log_buffer *log_b, u32 idx) { return NULL; } +static u32 log_next(struct log_buffer *log_b, u32 idx) { return 0; } static void call_console_drivers(int level, const char *text, size_t len) {} static size_t msg_print_text(const struct log *msg, enum log_flags prev, bool syslog, char *buf, size_t size) { return 0; } @@ -2114,7 +2364,7 @@ static void console_cont_flush(char *text, size_t size) unsigned long flags; size_t len; - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); if (!cont.len) goto out; @@ -2124,18 +2374,18 @@ static void console_cont_flush(char *text, size_t size) * busy. The earlier ones need to be printed before this one, we * did not flush any fragment so far, so just let it queue up. */ - if (console_seq < log_next_seq && !cont.cons) + if (console_seq < log_buf.next_seq && !cont.cons) goto out; len = cont_print_text(text, size); - raw_spin_unlock(&logbuf_lock); + raw_spin_unlock(&log_buf.lock); stop_critical_timings(); call_console_drivers(cont.level, text, len); start_critical_timings(); local_irq_restore(flags); return; out: - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); } /** @@ -2175,29 +2425,29 @@ again: size_t len; int level; - raw_spin_lock_irqsave(&logbuf_lock, flags); - if (seen_seq != log_next_seq) { + raw_spin_lock_irqsave(&log_buf.lock, flags); + if (seen_seq != log_buf.next_seq) { wake_klogd = true; - seen_seq = log_next_seq; + seen_seq = log_buf.next_seq; } - if (console_seq < log_first_seq) { + if (console_seq < log_buf.first_seq) { /* messages are gone, move to first one */ - console_seq = log_first_seq; - console_idx = log_first_idx; + console_seq = log_buf.first_seq; + console_idx = log_buf.first_idx; console_prev = 0; } skip: - if (console_seq == log_next_seq) + if (console_seq == log_buf.next_seq) break; - msg = log_from_idx(console_idx); + msg = log_from_idx(&log_buf, console_idx); if (msg->flags & LOG_NOCONS) { /* * Skip record we have buffered and already printed * directly to the console when we received it. */ - console_idx = log_next(console_idx); + console_idx = log_next(&log_buf, console_idx); console_seq++; /* * We will get here again when we register a new @@ -2212,10 +2462,10 @@ skip: level = msg->level; len = msg_print_text(msg, console_prev, false, text, sizeof(text)); - console_idx = log_next(console_idx); + console_idx = log_next(&log_buf, console_idx); console_seq++; console_prev = msg->flags; - raw_spin_unlock(&logbuf_lock); + raw_spin_unlock(&log_buf.lock); stop_critical_timings(); /* don't trace print latency */ call_console_drivers(level, text, len); @@ -2229,7 +2479,7 @@ skip: if (unlikely(exclusive_console)) exclusive_console = NULL; - raw_spin_unlock(&logbuf_lock); + raw_spin_unlock(&log_buf.lock); up(&console_sem); @@ -2239,9 +2489,9 @@ skip: * there's a new owner and the console_unlock() from them will do the * flush, no worries. */ - raw_spin_lock(&logbuf_lock); - retry = console_seq != log_next_seq; - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_lock(&log_buf.lock); + retry = console_seq != log_buf.next_seq; + raw_spin_unlock_irqrestore(&log_buf.lock, flags); if (retry && console_trylock()) goto again; @@ -2475,11 +2725,11 @@ void register_console(struct console *newcon) * console_unlock(); will print out the buffered messages * for us. */ - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); console_seq = syslog_seq; console_idx = syslog_idx; console_prev = syslog_prev; - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); /* * We're about to replay the log buffer. Only do this to the * just-registered console to avoid excessive message spam to @@ -2592,7 +2842,7 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work) } if (pending & PRINTK_PENDING_WAKEUP) - wake_up_interruptible(&log_wait); + wake_up_interruptible(&log_buf.wait); } static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { @@ -2603,7 +2853,7 @@ static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { void wake_up_klogd(void) { preempt_disable(); - if (waitqueue_active(&log_wait)) { + if (waitqueue_active(&log_buf.wait)) { this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); irq_work_queue(&__get_cpu_var(wake_up_klogd_work)); } @@ -2753,12 +3003,12 @@ void kmsg_dump(enum kmsg_dump_reason reason) /* initialize iterator with data about the stored records */ dumper->active = true; - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); dumper->cur_seq = clear_seq; dumper->cur_idx = clear_idx; - dumper->next_seq = log_next_seq; - dumper->next_idx = log_next_idx; - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + dumper->next_seq = log_buf.next_seq; + dumper->next_idx = log_buf.next_idx; + raw_spin_unlock_irqrestore(&log_buf.lock, flags); /* invoke dumper which will iterate over records */ dumper->dump(dumper, reason); @@ -2798,20 +3048,20 @@ bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog, if (!dumper->active) goto out; - if (dumper->cur_seq < log_first_seq) { + if (dumper->cur_seq < log_buf.first_seq) { /* messages are gone, move to first available one */ - dumper->cur_seq = log_first_seq; - dumper->cur_idx = log_first_idx; + dumper->cur_seq = log_buf.first_seq; + dumper->cur_idx = log_buf.first_idx; } /* last entry */ - if (dumper->cur_seq >= log_next_seq) + if (dumper->cur_seq >= log_buf.next_seq) goto out; - msg = log_from_idx(dumper->cur_idx); + msg = log_from_idx(&log_buf, dumper->cur_idx); l = msg_print_text(msg, 0, syslog, line, size); - dumper->cur_idx = log_next(dumper->cur_idx); + dumper->cur_idx = log_next(&log_buf, dumper->cur_idx); dumper->cur_seq++; ret = true; out: @@ -2843,9 +3093,9 @@ bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, unsigned long flags; bool ret; - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); return ret; } @@ -2885,16 +3135,16 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, if (!dumper->active) goto out; - raw_spin_lock_irqsave(&logbuf_lock, flags); - if (dumper->cur_seq < log_first_seq) { + raw_spin_lock_irqsave(&log_buf.lock, flags); + if (dumper->cur_seq < log_buf.first_seq) { /* messages are gone, move to first available one */ - dumper->cur_seq = log_first_seq; - dumper->cur_idx = log_first_idx; + dumper->cur_seq = log_buf.first_seq; + dumper->cur_idx = log_buf.first_idx; } /* last entry */ if (dumper->cur_seq >= dumper->next_seq) { - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); goto out; } @@ -2903,10 +3153,10 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, idx = dumper->cur_idx; prev = 0; while (seq < dumper->next_seq) { - struct log *msg = log_from_idx(idx); + struct log *msg = log_from_idx(&log_buf, idx); l += msg_print_text(msg, prev, true, NULL, 0); - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; prev = msg->flags; } @@ -2916,10 +3166,10 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, idx = dumper->cur_idx; prev = 0; while (l > size && seq < dumper->next_seq) { - struct log *msg = log_from_idx(idx); + struct log *msg = log_from_idx(&log_buf, idx); l -= msg_print_text(msg, prev, true, NULL, 0); - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; prev = msg->flags; } @@ -2931,10 +3181,10 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, l = 0; prev = 0; while (seq < dumper->next_seq) { - struct log *msg = log_from_idx(idx); + struct log *msg = log_from_idx(&log_buf, idx); l += msg_print_text(msg, prev, syslog, buf + l, size - l); - idx = log_next(idx); + idx = log_next(&log_buf, idx); seq++; prev = msg->flags; } @@ -2942,7 +3192,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, dumper->next_seq = next_seq; dumper->next_idx = next_idx; ret = true; - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); out: if (len) *len = l; @@ -2964,8 +3214,8 @@ void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper) { dumper->cur_seq = clear_seq; dumper->cur_idx = clear_idx; - dumper->next_seq = log_next_seq; - dumper->next_idx = log_next_idx; + dumper->next_seq = log_buf.next_seq; + dumper->next_idx = log_buf.next_idx; } /** @@ -2980,9 +3230,9 @@ void kmsg_dump_rewind(struct kmsg_dumper *dumper) { unsigned long flags; - raw_spin_lock_irqsave(&logbuf_lock, flags); + raw_spin_lock_irqsave(&log_buf.lock, flags); kmsg_dump_rewind_nolock(dumper); - raw_spin_unlock_irqrestore(&logbuf_lock, flags); + raw_spin_unlock_irqrestore(&log_buf.lock, flags); } EXPORT_SYMBOL_GPL(kmsg_dump_rewind); -- 2.7.4 From 0fdc8981f9d5c789add92f0837c297ff7f8ae9a3 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Mon, 20 Jul 2015 14:52:06 +0200 Subject: [PATCH 05/16] kmsg: add additional buffers support to memory class Memory class does not support additional kmsg buffers. Add additional kmsg buffers support to: * devnode() callback of "mem" class * file operations of major "mem" character device Signed-off-by: Marcin Niesluchowski Change-Id: Ideadca14d2f2e8abd653ab8677e04132b7d9757e --- drivers/char/mem.c | 37 +++++++++++++++++++++++++++++++++++-- include/linux/printk.h | 34 ++++++++++++++++++++++++++++++++++ kernel/printk_kmsg.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 38d3069..1dcc6b3 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -880,7 +880,7 @@ static const struct memdev { [7] = { "full", 0666, &full_fops, NULL }, [8] = { "random", 0666, &random_fops, NULL }, [9] = { "urandom", 0666, &urandom_fops, NULL }, -#ifdef CONFIG_PRINTK +#if defined(CONFIG_PRINTK) && !defined(CONFIG_MULTIPLE_KMSG) [11] = { "kmsg", 0644, &kmsg_fops, NULL }, #endif #ifdef CONFIG_CRASH_DUMP @@ -895,7 +895,11 @@ static int memory_open(struct inode *inode, struct file *filp) minor = iminor(inode); if (minor >= ARRAY_SIZE(devlist)) +#ifdef CONFIG_MULTIPLE_KMSG + return kmsg_memory_open(inode, filp); +#else return -ENXIO; +#endif dev = &devlist[minor]; if (!dev->fops) @@ -920,19 +924,42 @@ static const struct file_operations memory_fops = { .llseek = noop_llseek, }; +#ifdef CONFIG_MULTIPLE_KMSG +static char *mem_devnode(struct device *dev, umode_t *mode) +{ + int minor = MINOR(dev->devt); + + if (!mode) + goto out; + + if (minor >= ARRAY_SIZE(devlist)) { + kmsg_mode(minor, mode); + goto out; + } + + if (devlist[minor].mode) + *mode = devlist[minor].mode; +out: + return NULL; +} +#else static char *mem_devnode(struct device *dev, umode_t *mode) { if (mode && devlist[MINOR(dev->devt)].mode) *mode = devlist[MINOR(dev->devt)].mode; return NULL; } +#endif -static struct class *mem_class; +struct class *mem_class; static int __init chr_dev_init(void) { int minor; int err; +#ifdef CONFIG_MULTIPLE_KMSG + struct device *kmsg; +#endif err = bdi_init(&zero_bdi); if (err) @@ -960,6 +987,12 @@ static int __init chr_dev_init(void) NULL, devlist[minor].name); } +#ifdef CONFIG_MULTIPLE_KMSG + kmsg = init_kmsg(KMSG_MINOR, 0644); + if (IS_ERR(kmsg)) + return PTR_ERR(kmsg); +#endif + return tty_init(); } diff --git a/include/linux/printk.h b/include/linux/printk.h index db9831e..1d2aa37 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -357,6 +357,40 @@ extern void dump_stack(void) __cold; extern const struct file_operations kmsg_fops; +#ifdef CONFIG_MULTIPLE_KMSG +struct file; +struct inode; + +#ifdef CONFIG_PRINTK + +extern struct class *mem_class; + +#define KMSG_MINOR 11 + +extern struct device *init_kmsg(int minor, umode_t mode); +extern int kmsg_memory_open(struct inode *inode, struct file *filp); +extern int kmsg_mode(int minor, umode_t *mode); + +#else + +static inline struct device *init_kmsg(int minor, umode_t mode) +{ + return NULL; +} + +static inline int kmsg_memory_open(struct inode *inode, struct file *filp) +{ + return -ENXIO; +} + +static inline int kmsg_mode(int minor, umode_t *mode) +{ + return -ENXIO; +} + +#endif +#endif + enum { DUMP_PREFIX_NONE, DUMP_PREFIX_ADDRESS, diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index de15ce1..77efb8b 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -45,6 +45,8 @@ #include #include #include +#include +#include #include @@ -245,6 +247,7 @@ struct log_buffer { u64 next_seq; #ifdef CONFIG_PRINTK u32 next_idx; /* index of the next record to store */ + int mode; /* mode of device */ int minor; /* minor representing buffer device */ #endif }; @@ -291,6 +294,7 @@ static struct log_buffer log_buf = { .first_idx = 0, .next_seq = 0, .next_idx = 0, + .mode = 0, .minor = 0, }; @@ -1259,6 +1263,45 @@ const struct file_operations kmsg_fops = { .release = devkmsg_release, }; +/* Should be used for device registration */ +struct device *init_kmsg(int minor, umode_t mode) +{ + log_buf.minor = minor; + log_buf.mode = mode; + return device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), + NULL, "kmsg"); +} + +int kmsg_memory_open(struct inode *inode, struct file *filp) +{ + filp->f_op = &kmsg_fops; + + return kmsg_fops.open(inode, filp); +} + +int kmsg_mode(int minor, umode_t *mode) +{ + int ret = -ENXIO; + struct log_buffer *log_b; + + if (minor == log_buf.minor) { + *mode = log_buf.mode; + return 0; + } + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + *mode = log_b->mode; + ret = 0; + break; + } + } + rcu_read_unlock(); + + return ret; +} + #ifdef CONFIG_KEXEC /* * This appends the listed symbols to /proc/vmcoreinfo -- 2.7.4 From 5c16f974533ce31fcd39819a2d8ae06e283f6212 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Mon, 20 Apr 2015 13:03:10 +0200 Subject: [PATCH 06/16] kmsg: add function for adding and deleting additional buffers Additional kmsg buffers should be created and deleted dynamically. Adding two functions * kmsg_sys_buffer_add() creates additional kmsg buffer returning minor * kmsg_sys_buffer_del() deletes one based on provided minor Signed-off-by: Marcin Niesluchowski Change-Id: Idead13dfef110bc05fee3fcf91ce7d44b6e5a46c --- include/linux/printk.h | 9 ++++ kernel/printk_kmsg.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 1d2aa37..303418b 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -370,6 +370,8 @@ extern struct class *mem_class; extern struct device *init_kmsg(int minor, umode_t mode); extern int kmsg_memory_open(struct inode *inode, struct file *filp); extern int kmsg_mode(int minor, umode_t *mode); +extern int kmsg_sys_buffer_add(size_t size, umode_t mode); +extern void kmsg_sys_buffer_del(int minor); #else @@ -388,6 +390,13 @@ static inline int kmsg_mode(int minor, umode_t *mode) return -ENXIO; } +static inline int kmsg_sys_buffer_add(size_t size, umode_t mode) +{ + return -ENXIO; +} + +static inline void kmsg_sys_buffer_del(int minor) {} + #endif #endif diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 77efb8b..09a2ee5 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -46,6 +46,8 @@ #include #include #include +#include +#include #include #include @@ -234,6 +236,7 @@ struct log_buffer { char *buf; /* cyclic log buffer */ u32 len; /* buffer length */ wait_queue_head_t wait; /* wait queue for kmsg buffer */ + struct kref refcount; /* refcount for kmsg_sys buffers */ #endif /* * The lock protects kmsg buffer, indices, counters. This can be taken within @@ -274,6 +277,7 @@ static u32 clear_idx; #define PREFIX_MAX 32 #endif #define LOG_LINE_MAX 1024 - PREFIX_MAX +#define KMSG_NUM_MAX 255 /* record buffer */ #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) @@ -290,6 +294,7 @@ static struct log_buffer log_buf = { .len = __LOG_BUF_K_LEN, .lock = __RAW_SPIN_LOCK_UNLOCKED(log_buf.lock), .wait = __WAIT_QUEUE_HEAD_INITIALIZER(log_buf.wait), + .refcount = { .refcount = { .counter = 0 } }, .first_seq = 0, .first_idx = 0, .next_seq = 0, @@ -840,6 +845,15 @@ struct devkmsg_user { char buf[CONSOLE_EXT_LOG_MAX]; }; +void log_buf_release(struct kref *ref) +{ + struct log_buffer *log_b = container_of(ref, struct log_buffer, + refcount); + + kfree(log_b->buf); + kfree(log_b); +} + static int kmsg_sys_write(int minor, int level, const char *fmt, ...) { va_list args; @@ -922,7 +936,7 @@ static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, if (minor == log_buf.minor) { printk_emit(facility, level, NULL, 0, "%s", line); } else { - int error = kmsg_sys_write(minor, level, "%s", line); + int error = kmsg_sys_write(minor, level, NULL, 0, "%s", line); if (error) ret = error; @@ -960,8 +974,21 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file, } raw_spin_unlock_irq(&log_b->lock); - ret = wait_event_interruptible(log_b->wait, - user->seq != log_b->next_seq); + + if (log_b == &log_buf) { + ret = wait_event_interruptible(log_b->wait, + user->seq != log_b->next_seq); + } else { + rcu_read_unlock(); + kref_get(&log_b->refcount); + ret = wait_event_interruptible(log_b->wait, + user->seq != log_b->next_seq); + if (log_b->minor == -1) + ret = -ENXIO; + if (kref_put(&log_b->refcount, log_buf_release)) + ret = -ENXIO; + rcu_read_lock(); + } if (ret) goto out; raw_spin_lock_irq(&log_b->lock); @@ -1185,8 +1212,14 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait) rcu_read_lock(); list_for_each_entry_rcu(log_b, &log_buf.list, list) { if (log_b->minor == minor) { + kref_get(&log_b->refcount); + rcu_read_unlock(); + ret = kmsg_poll(log_b, file, wait); - break; + + if (kref_put(&log_b->refcount, log_buf_release)) + return POLLERR|POLLNVAL; + return ret; } } rcu_read_unlock(); @@ -1302,6 +1335,88 @@ int kmsg_mode(int minor, umode_t *mode) return ret; } +static DEFINE_SPINLOCK(kmsg_sys_list_lock); + +int kmsg_sys_buffer_add(size_t size, umode_t mode) +{ + unsigned long flags; + int minor = log_buf.minor; + struct log_buffer *log_b; + struct log_buffer *log_b_new; + + if (size < LOG_LINE_MAX + PREFIX_MAX) + return -EINVAL; + + log_b_new = kzalloc(sizeof(struct log_buffer), GFP_KERNEL); + if (!log_b_new) + return -ENOMEM; + + log_b_new->buf = kmalloc(size, GFP_KERNEL); + if (!log_b_new->buf) { + kfree(log_b_new); + return -ENOMEM; + } + + log_b_new->len = size; + log_b_new->lock = __RAW_SPIN_LOCK_UNLOCKED(log_b_new->lock); + init_waitqueue_head(&log_b_new->wait); + kref_init(&log_b_new->refcount); + log_b_new->mode = mode; + + kref_get(&log_b_new->refcount); + + spin_lock_irqsave(&kmsg_sys_list_lock, flags); + + list_for_each_entry(log_b, &log_buf.list, list) { + if (log_b->minor - minor > 1) + break; + + minor = log_b->minor; + } + + if (!(minor & MINORMASK) || (minor & MINORMASK) >= KMSG_NUM_MAX) { + kref_put(&log_b->refcount, log_buf_release); + spin_unlock_irqrestore(&kmsg_sys_list_lock, flags); + return -ERANGE; + } + + minor += 1; + log_b_new->minor = minor; + + list_add_tail_rcu(&log_b_new->list, &log_b->list); + + spin_unlock_irqrestore(&kmsg_sys_list_lock, flags); + + return minor; +} + +void kmsg_sys_buffer_del(int minor) +{ + unsigned long flags; + struct log_buffer *log_b; + + spin_lock_irqsave(&kmsg_sys_list_lock, flags); + + list_for_each_entry(log_b, &log_buf.list, list) { + if (log_b->minor == minor) + break; + } + + if (log_b == &log_buf) { + spin_unlock_irqrestore(&kmsg_sys_list_lock, flags); + return; + } + + list_del_rcu(&log_b->list); + + spin_unlock_irqrestore(&kmsg_sys_list_lock, flags); + + log_b->minor = -1; + wake_up_interruptible(&log_b->wait); + + kref_put(&log_b->refcount, log_buf_release); +} + #ifdef CONFIG_KEXEC /* * This appends the listed symbols to /proc/vmcoreinfo -- 2.7.4 From 091c2a8b8d5de78c5250e168395cb3f0cf49b8d6 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Mon, 27 Apr 2015 11:20:34 +0200 Subject: [PATCH 07/16] kmsg: add predefined _PID, _TID, _COMM keywords to kmsg* log dict kmsg* devices write operation wrote no dict along with message Due to usage of kmsg devices in userspace dict has been added identifying pid, tid and comm of writing process. Signed-off-by: Marcin Niesluchowski Change-Id: Idead2fa29607785031e37542c2f48481b04f9949 --- kernel/printk_kmsg.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 09a2ee5..5b2afd9 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -854,7 +854,34 @@ void log_buf_release(struct kref *ref) kfree(log_b); } -static int kmsg_sys_write(int minor, int level, const char *fmt, ...) +#define MAX_PID_LEN 20 +#define MAX_TID_LEN 20 +/* + * Fromat below describes dict appended to message written from userspace: + * "_PID=\0_TID=\0_COMM=" + * KMSG_DICT_MAX_LEN definition represents maximal length of this dict. + */ +#define KMSG_DICT_MAX_LEN (5 + MAX_PID_LEN + 1 + \ + 5 + MAX_TID_LEN + 1 + \ + 6 + TASK_COMM_LEN) + +static size_t set_kmsg_dict(char *buf) +{ + size_t len; + + len = sprintf(buf, "_PID=%d", task_tgid_nr(current)) + 1; + len += sprintf(buf + len, "_TID=%d", task_pid_nr(current)) + 1; + memcpy(buf + len, "_COMM=", 6); + len += 6; + get_task_comm(buf + len, current); + while (buf[len] != '\0') + len++; + return len; +} + +static int kmsg_sys_write(int minor, int level, + const char *dict, size_t dictlen, + const char *fmt, ...) { va_list args; int ret = -ENXIO; @@ -869,7 +896,7 @@ static int kmsg_sys_write(int minor, int level, const char *fmt, ...) va_start(args, fmt); log_format_and_store(log_b, 1 /* LOG_USER */, level, - NULL, 0, fmt, smp_processor_id(), args); + dict, dictlen, fmt, smp_processor_id(), args); va_end(args); wake_up_interruptible(&log_b->wait); @@ -890,6 +917,8 @@ static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, int level = default_message_loglevel; int facility = 1; /* LOG_USER */ size_t len = iov_length(iv, count); + char dict[KMSG_DICT_MAX_LEN]; + size_t dictlen; ssize_t ret = len; int minor = iminor(iocb->ki_filp->f_inode); @@ -933,10 +962,12 @@ static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, } line[len] = '\0'; + dictlen = set_kmsg_dict(dict); + if (minor == log_buf.minor) { - printk_emit(facility, level, NULL, 0, "%s", line); + printk_emit(facility, level, dict, dictlen, "%s", line); } else { - int error = kmsg_sys_write(minor, level, NULL, 0, "%s", line); + int error = kmsg_sys_write(minor, level, dict, dictlen, "%s", line); if (error) ret = error; -- 2.7.4 From a07f4b2c1fcdb6152ffd8e61a26eb9191e982814 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Thu, 18 Jun 2015 11:31:00 +0200 Subject: [PATCH 08/16] kmsg: add ioctl for adding and deleting kmsg* devices There is no possibility to add/delete kmsg* buffers from userspace. Adds following ioctl for main kmsg device adding and deleting additional kmsg devices: * KMSG_CMD_BUFFER_ADD * KMSG_CMD_BUFFER_DEL Signed-off-by: Marcin Niesluchowski Change-Id: Idead7a787892706249f50f1a19ca7a568753845a --- Documentation/ioctl/ioctl-number.txt | 1 + drivers/char/mem.c | 2 +- include/linux/printk.h | 7 ++ include/uapi/linux/Kbuild | 4 ++ include/uapi/linux/kmsg_ioctl.h | 30 +++++++++ kernel/printk_kmsg.c | 127 +++++++++++++++++++++++++++++++++++ 6 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/kmsg_ioctl.h diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 237acab..3c5e4a7 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -308,6 +308,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX 0xB3 00 linux/mmc/ioctl.h +0xBB 00-02 uapi/linux/kmsg_ioctl.h 0xC0 00-0F linux/usb/iowarrior.h 0xCB 00-1F CBM serial IEC bus in development: diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 1dcc6b3..ec7275d 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -896,7 +896,7 @@ static int memory_open(struct inode *inode, struct file *filp) minor = iminor(inode); if (minor >= ARRAY_SIZE(devlist)) #ifdef CONFIG_MULTIPLE_KMSG - return kmsg_memory_open(inode, filp); + return kmsg_memory_open_ext(inode, filp); #else return -ENXIO; #endif diff --git a/include/linux/printk.h b/include/linux/printk.h index 303418b..40b0d07 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -356,6 +356,7 @@ extern void dump_stack(void) __cold; #endif extern const struct file_operations kmsg_fops; +extern const struct file_operations kmsg_fops_ext; #ifdef CONFIG_MULTIPLE_KMSG struct file; @@ -369,6 +370,7 @@ extern struct class *mem_class; extern struct device *init_kmsg(int minor, umode_t mode); extern int kmsg_memory_open(struct inode *inode, struct file *filp); +extern int kmsg_memory_open_ext(struct inode *inode, struct file *filp); extern int kmsg_mode(int minor, umode_t *mode); extern int kmsg_sys_buffer_add(size_t size, umode_t mode); extern void kmsg_sys_buffer_del(int minor); @@ -385,6 +387,11 @@ static inline int kmsg_memory_open(struct inode *inode, struct file *filp) return -ENXIO; } +static inline int kmsg_memory_open_ext(struct inode *inode, struct file *filp) +{ + return -ENXIO; +} + static inline int kmsg_mode(int minor, umode_t *mode) { return -ENXIO; diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild index 405887b..8cef1e9 100644 --- a/include/uapi/linux/Kbuild +++ b/include/uapi/linux/Kbuild @@ -214,6 +214,10 @@ header-y += kexec.h header-y += keyboard.h header-y += keyctl.h +ifdef CONFIG_MULTIPLE_KMSG +header-y += kmsg_ioctl.h +endif + ifneq ($(wildcard $(srctree)/arch/$(SRCARCH)/include/uapi/asm/kvm.h \ $(srctree)/arch/$(SRCARCH)/include/asm/kvm.h),) header-y += kvm.h diff --git a/include/uapi/linux/kmsg_ioctl.h b/include/uapi/linux/kmsg_ioctl.h new file mode 100644 index 0000000..89c0c61 --- /dev/null +++ b/include/uapi/linux/kmsg_ioctl.h @@ -0,0 +1,30 @@ +/* + * This is ioctl include for kmsg* devices + */ + +#ifndef _KMSG_IOCTL_H_ +#define _KMSG_IOCTL_H_ + +#include +#include + +struct kmsg_cmd_buffer_add { + size_t size; + unsigned short mode; + int minor; +} __attribute__((packed)); + +#define KMSG_IOCTL_MAGIC 0xBB + +/* + * A ioctl interface for kmsg device. + * + * KMSG_CMD_BUFFER_ADD: Creates additional kmsg device based on its size + * and mode. Minor of created device is put. + * KMSG_CMD_BUFFER_DEL: Removes additional kmsg device based on its minor + */ +#define KMSG_CMD_BUFFER_ADD _IOWR(KMSG_IOCTL_MAGIC, 0x00, \ + struct kmsg_cmd_buffer_add) +#define KMSG_CMD_BUFFER_DEL _IOW(KMSG_IOCTL_MAGIC, 0x01, int) + +#endif diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 5b2afd9..00352ab 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -59,6 +59,10 @@ #define CREATE_TRACE_POINTS #include +#ifdef CONFIG_PRINTK +#include +#endif + #ifdef CONFIG_DEBUG_LL extern void printascii(char *); #endif @@ -1324,9 +1328,125 @@ const struct file_operations kmsg_fops = { .aio_write = devkmsg_writev, .llseek = devkmsg_llseek, .poll = devkmsg_poll, + .unlocked_ioctl = devkmsg_ioctl, + .compat_ioctl = devkmsg_ioctl, .release = devkmsg_release, }; +#define MAX_MINOR_LEN 20 + +static int kmsg_open_ext(struct inode *inode, struct file *file) +{ + return kmsg_fops.open(inode, file); +} + +static ssize_t kmsg_writev_ext(struct kiocb *iocb, const struct iovec *iov, unsigned long count, loff_t pos) +{ + return kmsg_fops.aio_write(iocb, iov, count, pos); +} + +static ssize_t kmsg_read_ext(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + return kmsg_fops.read(file, buf, count, ppos); +} + +static loff_t kmsg_llseek_ext(struct file *file, loff_t offset, int whence) +{ + return kmsg_fops.llseek(file, offset, whence); +} + +static unsigned int kmsg_poll_ext(struct file *file, + struct poll_table_struct *wait) +{ + return kmsg_fops.poll(file, wait); +} + +static long kmsg_ioctl_buffers(struct file *file, unsigned int cmd, + unsigned long arg) +{ + void __user *argp = (void __user *)arg; + size_t size; + umode_t mode; + char name[4 + MAX_MINOR_LEN + 1]; + struct device *dev; + int minor; + + if (iminor(file->f_inode) != log_buf.minor) + return -ENOTTY; + + switch (cmd) { + case KMSG_CMD_BUFFER_ADD: + if (copy_from_user(&size, argp, sizeof(size))) + return -EFAULT; + argp += sizeof(size); + if (copy_from_user(&mode, argp, sizeof(mode))) + return -EFAULT; + argp += sizeof(mode); + minor = kmsg_sys_buffer_add(size, mode); + if (minor < 0) + return minor; + sprintf(name, "kmsg%d", minor); + dev = device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor), + NULL, name); + if (IS_ERR(dev)) { + kmsg_sys_buffer_del(minor); + return PTR_ERR(dev); + } + if (copy_to_user(argp, &minor, sizeof(minor))) { + device_destroy(mem_class, MKDEV(MEM_MAJOR, minor)); + kmsg_sys_buffer_del(minor); + return -EFAULT; + } + return 0; + case KMSG_CMD_BUFFER_DEL: + if (copy_from_user(&minor, argp, sizeof(minor))) + return -EFAULT; + if (minor <= log_buf.minor) + return -EINVAL; + device_destroy(mem_class, MKDEV(MEM_MAJOR, minor)); + kmsg_sys_buffer_del(minor); + return 0; + } + return -ENOTTY; +} + +static long kmsg_unlocked_ioctl_ext(struct file *file, unsigned int cmd, + unsigned long arg) +{ + long ret = kmsg_ioctl_buffers(file, cmd, arg); + + if (ret == -ENOTTY) + return kmsg_fops.unlocked_ioctl(file, cmd, arg); + return ret; +} + +static long kmsg_compat_ioctl_ext(struct file *file, unsigned int cmd, + unsigned long arg) +{ + long ret = kmsg_ioctl_buffers(file, cmd, arg); + + if (ret == -ENOTTY) + return kmsg_fops.compat_ioctl(file, cmd, arg); + return ret; +} + +static int kmsg_release_ext(struct inode *inode, struct file *file) +{ + return kmsg_fops.release(inode, file); +} + +const struct file_operations kmsg_fops_ext = { + .open = kmsg_open_ext, + .read = kmsg_read_ext, + .aio_write = kmsg_writev_ext, + .llseek = kmsg_llseek_ext, + .poll = kmsg_poll_ext, + .unlocked_ioctl = kmsg_unlocked_ioctl_ext, + .compat_ioctl = kmsg_compat_ioctl_ext, + .release = kmsg_release_ext, +}; + /* Should be used for device registration */ struct device *init_kmsg(int minor, umode_t mode) { @@ -1343,6 +1463,13 @@ int kmsg_memory_open(struct inode *inode, struct file *filp) return kmsg_fops.open(inode, filp); } +int kmsg_memory_open_ext(struct inode *inode, struct file *filp) +{ + filp->f_op = &kmsg_fops_ext; + + return kmsg_fops_ext.open(inode, filp); +} + int kmsg_mode(int minor, umode_t *mode) { int ret = -ENXIO; -- 2.7.4 From 50441b316ad0a7db9552fcacddbbd073cf32d7a0 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Thu, 21 May 2015 16:24:30 +0200 Subject: [PATCH 09/16] kmsg: add ioctl for kmsg* devices operating on buffers There is no possibility to clear additional kmsg buffers, get size of them or know what size should be passed to read file operation (too small size causes it to retrun -EINVAL). Add following ioctls which solve those issues: * KMSG_CMD_GET_BUF_SIZE * KMSG_CMD_GET_READ_SIZE_MAX * KMSG_CMD_CLEAR Signed-off-by: Marcin Niesluchowski Change-Id: Ideade7e0b5c66bde3415f3190059742bac79333b --- Documentation/ioctl/ioctl-number.txt | 2 +- include/uapi/linux/kmsg_ioctl.h | 15 ++++++ kernel/printk_kmsg.c | 100 ++++++++++++++++++++++++++--------- 3 files changed, 90 insertions(+), 27 deletions(-) diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt index 3c5e4a7..6f2e987 100644 --- a/Documentation/ioctl/ioctl-number.txt +++ b/Documentation/ioctl/ioctl-number.txt @@ -308,7 +308,7 @@ Code Seq#(hex) Include File Comments 0xB1 00-1F PPPoX 0xB3 00 linux/mmc/ioctl.h -0xBB 00-02 uapi/linux/kmsg_ioctl.h +0xBB 00-83 uapi/linux/kmsg_ioctl.h 0xC0 00-0F linux/usb/iowarrior.h 0xCB 00-1F CBM serial IEC bus in development: diff --git a/include/uapi/linux/kmsg_ioctl.h b/include/uapi/linux/kmsg_ioctl.h index 89c0c61..2389d9f 100644 --- a/include/uapi/linux/kmsg_ioctl.h +++ b/include/uapi/linux/kmsg_ioctl.h @@ -27,4 +27,19 @@ struct kmsg_cmd_buffer_add { struct kmsg_cmd_buffer_add) #define KMSG_CMD_BUFFER_DEL _IOW(KMSG_IOCTL_MAGIC, 0x01, int) +/* + * A ioctl interface for kmsg* devices. + * + * KMSG_CMD_GET_BUF_SIZE: Retrieve cyclic log buffer size associated with + * device. + * KMSG_CMD_GET_READ_SIZE_MAX: Retrieve max size of data read by kmsg read + * operation. + * KMSG_CMD_CLEAR: Clears cyclic log buffer. After that operation + * there is no data to read from buffer unless + * logs are written. + */ +#define KMSG_CMD_GET_BUF_SIZE _IOR(KMSG_IOCTL_MAGIC, 0x80, __u32) +#define KMSG_CMD_GET_READ_SIZE_MAX _IOR(KMSG_IOCTL_MAGIC, 0x81, __u32) +#define KMSG_CMD_CLEAR _IO(KMSG_IOCTL_MAGIC, 0x82) + #endif diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index 00352ab..cfcf3d9 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -254,6 +254,10 @@ struct log_buffer { u64 next_seq; #ifdef CONFIG_PRINTK u32 next_idx; /* index of the next record to store */ +/* sequence number of the next record to read after last 'clear' command */ + u64 clear_seq; +/* index of the next record to read after last 'clear' command */ + u32 clear_idx; int mode; /* mode of device */ int minor; /* minor representing buffer device */ #endif @@ -271,10 +275,6 @@ static u64 console_seq; static u32 console_idx; static enum log_flags console_prev; -/* the next printk record to read after the last 'clear' command */ -static u64 clear_seq; -static u32 clear_idx; - #ifdef CONFIG_PRINTK_PROCESS #define PREFIX_MAX 48 #else @@ -303,6 +303,8 @@ static struct log_buffer log_buf = { .first_idx = 0, .next_seq = 0, .next_idx = 0, + .clear_seq = 0, + .clear_idx = 0, .mode = 0, .minor = 0, }; @@ -1159,18 +1161,14 @@ static loff_t kmsg_llseek(struct log_buffer *log_b, struct file *file, user->seq = log_b->first_seq; break; case SEEK_DATA: - /* no clear index for kmsg_sys buffers */ - if (log_b != &log_buf) { - ret = -EINVAL; - break; - } /* * The first record after the last SYSLOG_ACTION_CLEAR, - * like issued by 'dmesg -c'. Reading /dev/kmsg itself - * changes no global state, and does not clear anything. + * like issued by 'dmesg -c' or KMSG_CMD_CLEAR ioctl + * command. Reading /dev/kmsg itself changes no global + * state, and does not clear anything. */ - user->idx = clear_idx; - user->seq = clear_seq; + user->idx = log_b->clear_idx; + user->seq = log_b->clear_seq; break; case SEEK_END: /* after the last record */ @@ -1310,6 +1308,56 @@ static int devkmsg_open(struct inode *inode, struct file *file) return ret; } +static long kmsg_ioctl(struct log_buffer *log_b, unsigned int cmd, + unsigned long arg) +{ + void __user *argp = (void __user *)arg; + static const u32 read_size_max = CONSOLE_EXT_LOG_MAX; + + switch (cmd) { + case KMSG_CMD_GET_BUF_SIZE: + if (copy_to_user(argp, &log_b->len, sizeof(u32))) + return -EFAULT; + break; + case KMSG_CMD_GET_READ_SIZE_MAX: + if (copy_to_user(argp, &read_size_max, sizeof(u32))) + return -EFAULT; + break; + case KMSG_CMD_CLEAR: + if (!capable(CAP_SYSLOG)) + return -EPERM; + raw_spin_lock_irq(&log_b->lock); + log_b->clear_seq = log_b->next_seq; + log_b->clear_idx = log_b->next_idx; + raw_spin_unlock_irq(&log_b->lock); + break; + default: + return -ENOTTY; + } + return 0; +} + +static long devkmsg_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + long ret = -ENXIO; + int minor = iminor(file->f_inode); + struct log_buffer *log_b; + + if (minor == log_buf.minor) + return kmsg_ioctl(&log_buf, cmd, arg); + + rcu_read_lock(); + list_for_each_entry_rcu(log_b, &log_buf.list, list) { + if (log_b->minor == minor) { + ret = kmsg_ioctl(log_b, cmd, arg); + break; + } + } + rcu_read_unlock(); + return ret; +} + static int devkmsg_release(struct inode *inode, struct file *file) { struct devkmsg_user *user = file->private_data; @@ -1909,18 +1957,18 @@ static int syslog_print_all(char __user *buf, int size, bool clear) u32 idx; enum log_flags prev; - if (clear_seq < log_buf.first_seq) { + if (log_buf.clear_seq < log_buf.first_seq) { /* messages are gone, move to first available one */ - clear_seq = log_buf.first_seq; - clear_idx = log_buf.first_idx; + log_buf.clear_seq = log_buf.first_seq; + log_buf.clear_idx = log_buf.first_idx; } /* * Find first record that fits, including all following records, * into the user-provided buffer for this dump. */ - seq = clear_seq; - idx = clear_idx; + seq = log_buf.clear_seq; + idx = log_buf.clear_idx; prev = 0; while (seq < log_buf.next_seq) { struct log *msg = log_from_idx(&log_buf, idx); @@ -1932,8 +1980,8 @@ static int syslog_print_all(char __user *buf, int size, bool clear) } /* move first record forward until length fits into the buffer */ - seq = clear_seq; - idx = clear_idx; + seq = log_buf.clear_seq; + idx = log_buf.clear_idx; prev = 0; while (len > size && seq < log_buf.next_seq) { struct log *msg = log_from_idx(&log_buf, idx); @@ -1980,8 +2028,8 @@ static int syslog_print_all(char __user *buf, int size, bool clear) } if (clear) { - clear_seq = log_buf.next_seq; - clear_idx = log_buf.next_idx; + log_buf.clear_seq = log_buf.next_seq; + log_buf.clear_idx = log_buf.next_idx; } raw_spin_unlock_irq(&log_buf.lock); @@ -3320,8 +3368,8 @@ void kmsg_dump(enum kmsg_dump_reason reason) dumper->active = true; raw_spin_lock_irqsave(&log_buf.lock, flags); - dumper->cur_seq = clear_seq; - dumper->cur_idx = clear_idx; + dumper->cur_seq = log_buf.clear_seq; + dumper->cur_idx = log_buf.clear_idx; dumper->next_seq = log_buf.next_seq; dumper->next_idx = log_buf.next_idx; raw_spin_unlock_irqrestore(&log_buf.lock, flags); @@ -3528,8 +3576,8 @@ EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer); */ void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper) { - dumper->cur_seq = clear_seq; - dumper->cur_idx = clear_idx; + dumper->cur_seq = log_buf.clear_seq; + dumper->cur_idx = log_buf.clear_idx; dumper->next_seq = log_buf.next_seq; dumper->next_idx = log_buf.next_idx; } -- 2.7.4 From 0e1d62ad7718e7a5bcb1d177832e36d9993a266c Mon Sep 17 00:00:00 2001 From: Paul Osmialowski Date: Fri, 12 Feb 2016 16:01:23 +0100 Subject: [PATCH 10/16] kmsg: selftests this patch adds selftests framework and four test scenarios for kmsg. The framework shape and code was inspired by similar selftests framework for kdbus. Signed-off-by: Paul Osmialowski [Fixed multithreaded test bug: buffer size > LOG_LINE_MAX] Signed-off-by: Kazimierz Krosman Change-Id: Icedc0fee86c90430dcdb59d592392fbac05b42f5 --- samples/kmsg/kmsg-api.h | 44 +++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/kmsg/.gitignore | 1 + tools/testing/selftests/kmsg/Makefile | 30 ++ tools/testing/selftests/kmsg/kmsg-test.c | 344 +++++++++++++++++++++ tools/testing/selftests/kmsg/kmsg-test.h | 28 ++ tools/testing/selftests/kmsg/test-buffer-add-del.c | 78 +++++ .../kmsg/test-buffer-add-write-read-del.c | 163 ++++++++++ .../kmsg/test-buffer-buf-multithreaded-torture.c | 201 ++++++++++++ .../selftests/kmsg/test-buffer-buf-torture.c | 141 +++++++++ 10 files changed, 1031 insertions(+) create mode 100644 samples/kmsg/kmsg-api.h create mode 100644 tools/testing/selftests/kmsg/.gitignore create mode 100644 tools/testing/selftests/kmsg/Makefile create mode 100644 tools/testing/selftests/kmsg/kmsg-test.c create mode 100644 tools/testing/selftests/kmsg/kmsg-test.h create mode 100644 tools/testing/selftests/kmsg/test-buffer-add-del.c create mode 100644 tools/testing/selftests/kmsg/test-buffer-add-write-read-del.c create mode 100644 tools/testing/selftests/kmsg/test-buffer-buf-multithreaded-torture.c create mode 100644 tools/testing/selftests/kmsg/test-buffer-buf-torture.c diff --git a/samples/kmsg/kmsg-api.h b/samples/kmsg/kmsg-api.h new file mode 100644 index 0000000..9004acd --- /dev/null +++ b/samples/kmsg/kmsg-api.h @@ -0,0 +1,44 @@ +#ifndef KMSG_API_H +#define KMSG_API_H + +#include +#include +#include +#include + +static inline int kmsg_cmd_buffer_add(int fd, struct kmsg_cmd_buffer_add *cmd) +{ + int ret = ioctl(fd, KMSG_CMD_BUFFER_ADD, cmd); + + return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0; +} + +static inline int kmsg_cmd_buffer_del(int fd, int *minor) +{ + int ret = ioctl(fd, KMSG_CMD_BUFFER_DEL, minor); + + return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0; +} + +static inline int kmsg_cmd_get_buf_size(int fd, uint32_t *size) +{ + int ret = ioctl(fd, KMSG_CMD_GET_BUF_SIZE, size); + + return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0; +} + +static inline int kmsg_cmd_get_read_size_max(int fd, uint32_t *max_size) +{ + int ret = ioctl(fd, KMSG_CMD_GET_READ_SIZE_MAX, max_size); + + return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0; +} + +static inline int kmsg_cmd_clear(int fd) +{ + int ret = ioctl(fd, KMSG_CMD_CLEAR); + + return (ret < 0) ? (errno > 0 ? -errno : -EINVAL) : 0; +} + +#endif /* KMSG_API_H */ diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 2cee2b7..f07b189 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -2,6 +2,7 @@ TARGETS = breakpoints TARGETS += cpu-hotplug TARGETS += efivarfs TARGETS += kcmp +TARGETS += kmsg TARGETS += memory-hotplug TARGETS += mqueue TARGETS += mount diff --git a/tools/testing/selftests/kmsg/.gitignore b/tools/testing/selftests/kmsg/.gitignore new file mode 100644 index 0000000..687d517 --- /dev/null +++ b/tools/testing/selftests/kmsg/.gitignore @@ -0,0 +1 @@ +kmsg-test diff --git a/tools/testing/selftests/kmsg/Makefile b/tools/testing/selftests/kmsg/Makefile new file mode 100644 index 0000000..cee2e2b --- /dev/null +++ b/tools/testing/selftests/kmsg/Makefile @@ -0,0 +1,30 @@ +CFLAGS += -I../../../../usr/include/ +CFLAGS += -I../../../../samples/kmsg/ +CFLAGS += -I../../../../include/uapi/ +CFLAGS += -std=gnu99 -Wall +CFLAGS += -DKBUILD_MODNAME=\"kmsg\" -D_GNU_SOURCE +CFLAGS += -pthread +LDLIBS += -pthread + +OBJS= \ + kmsg-test.o \ + test-buffer-add-del.o \ + test-buffer-add-write-read-del.o \ + test-buffer-buf-torture.o \ + test-buffer-buf-multithreaded-torture.o + +all: kmsg-test + +include ../lib.mk + +%.o: %.c kmsg-test.h + $(CC) $(CFLAGS) -c $< -o $@ + +kmsg-test: $(OBJS) + $(CC) $(CFLAGS) $^ $(LDLIBS) -o $@ + +run_tests: + ./kmsg-test + +clean: + rm -f *.o kmsg-test diff --git a/tools/testing/selftests/kmsg/kmsg-test.c b/tools/testing/selftests/kmsg/kmsg-test.c new file mode 100644 index 0000000..282ec1f --- /dev/null +++ b/tools/testing/selftests/kmsg/kmsg-test.c @@ -0,0 +1,344 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#include "kmsg-test.h" + +struct kmsg_test { + const char *name; + const char *desc; + int (*func)(const struct kmsg_test_args *args); +}; + +static const struct kmsg_test tests[] = { + { + .name = "buffer-add-del", + .desc = "create and delete kmsg devices", + .func = kmsg_test_buffer_add_del, + }, { + .name = "buffer-add-write-read-del", + .desc = "create w/r and del kmsg device", + .func = kmsg_test_buffer_add_write_read_del, + }, { + .name = "buffer-buf-torture", + .desc = "fill more than whole buffer can hold", + .func = kmsg_test_buffer_buf_torture, + }, { + .name = "buffer-buf-multithreaded-torture", + .desc = "fill from many threads", + .func = kmsg_test_buffer_buf_multithreaded_torture, + }, +}; + +#define N_TESTS ARRAY_SIZE(tests) + +FILE *kmsg_get_device(int minor, const char *mode) +{ + char path[80] = ""; + dev_t dev = makedev(1, minor); + + if (minor < 0) { + printf("Invalid minor number %d\n", minor); + return NULL; + } + + snprintf(path, sizeof(path), "/tmp/kmsg-%d", minor); + + if (access(path, F_OK) < 0) { + if (mknod(path, S_IFCHR | 0600, dev)) { + printf("Cannot create device %s with minor %d\n", + path, minor); + return NULL; + } + } + + if (access(path, F_OK) < 0) { + printf("Cannot access device %s\n", path); + return NULL; + } + + return fopen(path, mode); +} + +int kmsg_drop_device(int minor) +{ + char path[80] = ""; + + if (minor < 0) { + printf("Invalid minor number %d\n", minor); + return -1; + } + + snprintf(path, sizeof(path), "/tmp/kmsg-%d", minor); + + return unlink(path); +} + +static void usage(const char *argv0) +{ + unsigned int i, j; + + printf("Usage: %s [options]\n" + "Options:\n" + "\t-x, --loop Run in a loop\n" + "\t-f, --fork Fork before running a test\n" + "\t-h, --help Print this help\n" + "\t-t, --test Run one specific test only\n" + "\t-w, --wait Wait before actually starting test\n" + "\n", argv0); + + printf("By default, all test are run once, and a summary is printed.\n" + "Available tests for --test:\n\n"); + + for (i = 0; i < N_TESTS; i++) { + const struct kmsg_test *t = tests + i; + + printf("\t%s", t->name); + + for (j = 0; j < 60 - strlen(t->name); j++) + printf(" "); + + printf("Test %s\n", t->desc); + } + + printf("\n"); + printf("Note that some tests may, if run specifically by --test, "); + printf("behave differently, and not terminate by themselves.\n"); +} + +static void print_test_result(int ret) +{ + switch (ret) { + case KSFT_PASS: + printf("OK"); + break; + case KSFT_SKIP: + printf("SKIPPED"); + break; + case KSFT_FAIL: + printf("ERROR"); + break; + } +} + +static int test_run(const struct kmsg_test *t, + const struct kmsg_test_args *kmsg_args, + int wait) +{ + int ret; + + if (wait > 0) { + printf("Sleeping %d seconds before running test ...\n", wait); + sleep(wait); + } + + ret = t->func(kmsg_args); + return ret; +} + +static int test_run_forked(const struct kmsg_test *t, + const struct kmsg_test_args *kmsg_args, + int wait) +{ + int ret; + pid_t pid; + + pid = fork(); + if (pid < 0) { + return KSFT_FAIL; + } else if (pid == 0) { + ret = test_run(t, kmsg_args, wait); + _exit(ret); + } + + pid = waitpid(pid, &ret, 0); + if (pid <= 0) + return KSFT_FAIL; + else if (!WIFEXITED(ret)) + return KSFT_FAIL; + else + return WEXITSTATUS(ret); +} + +static int start_all_tests(const struct kmsg_test_args *kmsg_args) +{ + int retval; + int ret = KSFT_PASS; + unsigned int i, n; + const struct kmsg_test *t; + + for (i = 0; i < N_TESTS; i++) { + t = tests + i; + + printf("Testing %s (%s) ", t->desc, t->name); + for (n = 0; n < 60 - strlen(t->desc) - strlen(t->name); n++) + printf("."); + printf(" "); + + retval = test_run_forked(t, kmsg_args, 0); + switch (retval) { + case KSFT_PASS: + ksft_inc_pass_cnt(); + break; + case KSFT_SKIP: + ksft_inc_xskip_cnt(); + break; + case KSFT_FAIL: + default: + ret = KSFT_FAIL; + ksft_inc_fail_cnt(); + break; + } + + print_test_result(retval); + printf("\n"); + } + + return ret; +} + +static int start_one_test(const struct kmsg_test_args *kmsg_args) +{ + int i, ret = KSFT_PASS; + bool test_found = false; + const struct kmsg_test *t; + + for (i = 0; i < N_TESTS; i++) { + t = tests + i; + + if (strcmp(t->name, kmsg_args->test)) + continue; + + do { + test_found = true; + if (kmsg_args->fork) + ret = test_run_forked(t, kmsg_args, + kmsg_args->wait); + else + ret = test_run(t, kmsg_args, + kmsg_args->wait); + + printf("Testing %s: ", t->desc); + print_test_result(ret); + printf("\n"); + + if ((ret != KSFT_PASS) && (ret != KSFT_SKIP)) + break; + } while (kmsg_args->loop); + + return ret; + } + + if (!test_found) { + printf("Unknown test-id '%s'\n", kmsg_args->test); + return KSFT_FAIL; + } + + return ret; +} + +static int start_tests(const struct kmsg_test_args *kmsg_args) +{ + int retval; + int ret = KSFT_PASS; + + if (kmsg_args->test) { + retval = start_one_test(kmsg_args); + switch (retval) { + case KSFT_PASS: + ksft_inc_pass_cnt(); + break; + case KSFT_SKIP: + ksft_inc_xskip_cnt(); + break; + case KSFT_FAIL: + default: + ret = KSFT_FAIL; + ksft_inc_fail_cnt(); + break; + } + } else { + do { + ret = start_all_tests(kmsg_args); + if ((ret != KSFT_PASS) && (ret != KSFT_SKIP)) + break; + } while (kmsg_args->loop); + } + + return ret; +} + +int main(int argc, char *argv[]) +{ + int t, ret = 0; + struct kmsg_test_args *kmsg_args; + char *exec = basename(argv[0]); + + kmsg_args = malloc(sizeof(*kmsg_args)); + if (!kmsg_args) { + printf("unable to malloc() kmsg_args\n"); + return ksft_exit_fail(); + } + + memset(kmsg_args, 0, sizeof(*kmsg_args)); + + static const struct option options[] = { + { "loop", no_argument, NULL, 'x' }, + { "help", no_argument, NULL, 'h' }, + { "test", required_argument, NULL, 't' }, + { "wait", required_argument, NULL, 'w' }, + { "fork", no_argument, NULL, 'f' }, + {} + }; + + if (strcmp(exec, "kmsg-test") != 0) + kmsg_args->test = exec; + + while ((t = getopt_long(argc, argv, "hxfm:r:t:b:w:a", + options, NULL)) >= 0) { + switch (t) { + case 'x': + kmsg_args->loop = 1; + break; + + case 't': + kmsg_args->test = optarg; + break; + + case 'w': + kmsg_args->wait = strtol(optarg, NULL, 10); + break; + + case 'f': + kmsg_args->fork = 1; + break; + + default: + case 'h': + usage(argv[0]); + return ksft_exit_fail(); + } + } + + ret = start_tests(kmsg_args); + + free(kmsg_args); + + ksft_print_cnts(); + + if ((ret != KSFT_PASS) && (ret != KSFT_SKIP)) + return ksft_exit_fail(); + + return ksft_exit_pass(); +} diff --git a/tools/testing/selftests/kmsg/kmsg-test.h b/tools/testing/selftests/kmsg/kmsg-test.h new file mode 100644 index 0000000..d9f770c --- /dev/null +++ b/tools/testing/selftests/kmsg/kmsg-test.h @@ -0,0 +1,28 @@ +#ifndef _KMSG_TEST_H_ +#define _KMSG_TEST_H_ + +#include + +#define DEV_KMSG "/dev/kmsg" + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +#define KMSG_REQUESTED_BUF_SIZE (1024 * 256) + +struct kmsg_test_args { + int loop; + int wait; + int fork; + const char *test; +}; + +FILE *kmsg_get_device(int minor, const char *mode); +int kmsg_drop_device(int minor); + +int kmsg_test_buffer_add_del(const struct kmsg_test_args *args); +int kmsg_test_buffer_add_write_read_del(const struct kmsg_test_args *args); +int kmsg_test_buffer_buf_torture(const struct kmsg_test_args *args); +int kmsg_test_buffer_buf_multithreaded_torture( + const struct kmsg_test_args *args); + +#endif /* _KMSG_TEST_H_ */ diff --git a/tools/testing/selftests/kmsg/test-buffer-add-del.c b/tools/testing/selftests/kmsg/test-buffer-add-del.c new file mode 100644 index 0000000..4acef53 --- /dev/null +++ b/tools/testing/selftests/kmsg/test-buffer-add-del.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#include "kmsg-test.h" + +int kmsg_test_buffer_add_del(const struct kmsg_test_args *args) +{ + int i; + int fd = open(DEV_KMSG, O_RDWR); + struct kmsg_cmd_buffer_add cmd = { 0 }; + int minors[] = { -1, -1, -1, -1 }; + FILE *fds[ARRAY_SIZE(minors)]; + int retval = KSFT_PASS; + uint32_t size; + + if (fd < 0) { + printf("Failed: cannot open %s\n", DEV_KMSG); + return KSFT_FAIL; + } + + for (i = 0; i < ARRAY_SIZE(minors); i++) { + fds[i] = NULL; + cmd.size = KMSG_REQUESTED_BUF_SIZE; + cmd.mode = 0662; + if (kmsg_cmd_buffer_add(fd, &cmd)) { + printf("Failed to add buffer\n"); + goto error; + } + if (cmd.minor < 0) { + printf("Minor number < 0\n"); + goto error; + } + minors[i] = cmd.minor; + fds[i] = kmsg_get_device(minors[i], "r"); + if (!fds[i]) { + printf("Cannot get device %d\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(fds[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + } + + goto cleanup; + +error: + retval = KSFT_FAIL; + +cleanup: + for (i = 0; i < ARRAY_SIZE(minors); i++) { + if (minors[i] < 0) + continue; + if (fds[i]) + fclose(fds[i]); + if (kmsg_drop_device(minors[i])) { + printf("Failed to delete device file %d\n", i); + retval = KSFT_FAIL; + } + if (kmsg_cmd_buffer_del(fd, &minors[i])) { + printf("Failed to delete buffer %d\n", i); + retval = KSFT_FAIL; + } + } + close(fd); + return retval; +} diff --git a/tools/testing/selftests/kmsg/test-buffer-add-write-read-del.c b/tools/testing/selftests/kmsg/test-buffer-add-write-read-del.c new file mode 100644 index 0000000..2f21bce --- /dev/null +++ b/tools/testing/selftests/kmsg/test-buffer-add-write-read-del.c @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#include "kmsg-test.h" + +static const char *message(char *buff, size_t size, int i, int j) +{ + snprintf(buff, size, "Test message (%d, %d)", i, j); + return buff; +} + +int kmsg_test_buffer_add_write_read_del(const struct kmsg_test_args *args) +{ + int i, j; + int fd = open(DEV_KMSG, O_RDWR); + struct kmsg_cmd_buffer_add cmd = { 0 }; + int minors[] = { -1, -1, -1, -1 }; + FILE *fds[ARRAY_SIZE(minors)]; + FILE *log[ARRAY_SIZE(minors)]; + int logfd; + int retval = KSFT_PASS; + uint32_t size; + char txt[80] = ""; + char *buff = NULL; + const char *msg; + char *msgend; + + if (fd < 0) { + printf("Failed: cannot open %s\n", DEV_KMSG); + return KSFT_FAIL; + } + + for (i = 0; i < ARRAY_SIZE(minors); i++) { + fds[i] = NULL; + log[i] = NULL; + cmd.size = KMSG_REQUESTED_BUF_SIZE; + cmd.mode = 0662; + if (kmsg_cmd_buffer_add(fd, &cmd)) { + printf("Failed to add buffer\n"); + goto error; + } + if (cmd.minor < 0) { + printf("Minor number < 0\n"); + goto error; + } + minors[i] = cmd.minor; + + fds[i] = kmsg_get_device(minors[i], "w"); + if (!fds[i]) { + printf("Cannot get device %d for write\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(fds[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + log[i] = kmsg_get_device(minors[i], "r"); + if (!log[i]) { + printf("Cannot get device %d for read\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(log[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + + for (j = 0; j <= i; j++) { + if (kmsg_cmd_clear(fileno(fds[j]))) { + printf("Cannot clear buffer on device %d\n", j); + goto error; + } + fprintf(fds[j], "%s\n", message(txt, ARRAY_SIZE(txt), + i, j)); + fflush(fds[j]); + } + + for (j = 0; j <= i; j++) { + logfd = fileno(log[j]); + size = 0; + if (kmsg_cmd_get_read_size_max(logfd, &size)) { + printf("Cannot get buf size on device %d\n", j); + goto error; + } + if (!size) { + printf("Expected non-zero buf size on %d\n", j); + goto error; + } + buff = malloc(size); + if (!buff) { + printf("Out of memory\n"); + goto error; + } + if (read(logfd, buff, size) <= 0) { + printf("Could not read from buffer %d\n", j); + goto error; + } + msg = strchr(buff, ';'); + msgend = strchr(buff, '\n'); + if ((!msg) || (!msgend)) { + printf("Could not read stored log on %d\n", j); + goto error; + } + msg++; + *msgend = 0; + if (strcmp(msg, message(txt, ARRAY_SIZE(txt), i, j))) { + printf("Messages do not match on %d\n", j); + goto error; + } + free(buff); + buff = NULL; + } + } + + goto cleanup; + +error: + retval = KSFT_FAIL; + +cleanup: + for (i = 0; i < ARRAY_SIZE(minors); i++) { + if (minors[i] < 0) + continue; + if (fds[i]) + fclose(fds[i]); + if (log[i]) { + if (kmsg_cmd_clear(fileno(log[i]))) { + printf("Failed to clear device %d\n", i); + retval = KSFT_FAIL; + } + fclose(log[i]); + } + if (kmsg_drop_device(minors[i])) { + printf("Failed to delete device file %d\n", i); + retval = KSFT_FAIL; + } + if (kmsg_cmd_buffer_del(fd, &minors[i])) { + printf("Failed to delete buffer %d\n", i); + retval = KSFT_FAIL; + } + } + close(fd); + if (buff) + free(buff); + return retval; +} diff --git a/tools/testing/selftests/kmsg/test-buffer-buf-multithreaded-torture.c b/tools/testing/selftests/kmsg/test-buffer-buf-multithreaded-torture.c new file mode 100644 index 0000000..7202dc6 --- /dev/null +++ b/tools/testing/selftests/kmsg/test-buffer-buf-multithreaded-torture.c @@ -0,0 +1,201 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#include "kmsg-test.h" + +#define SOME_BUFF_SIZE (1024-33) +#define THREADS_PER_DEVICE 10 + +static bool ok = true; +static bool nok = !true; + +static void *kmsg_test_thread_func(void *data) +{ + char buff[SOME_BUFF_SIZE]; + int minor = *((int *)data); + FILE *f = kmsg_get_device(minor, "w"); + int fd; + void *retval = &ok; + int iter; + ssize_t s; + uint32_t size, done; + uint32_t max_size; + + memset(buff, 'A', ARRAY_SIZE(buff)); + buff[ARRAY_SIZE(buff) - 1] = 0; + + if (!f) { + printf("Cannot get device for write\n"); + return &nok; + } + fd = fileno(f); + + size = 0; + if (kmsg_cmd_get_buf_size(fd, &size)) { + printf("Cannot get buf size\n"); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size\n"); + goto error; + } + + if (kmsg_cmd_clear(fd)) { + printf("Cannot clear buffer\n"); + goto error; + } + + iter = 0; + while (done < (KMSG_REQUESTED_BUF_SIZE * 2)) { + s = write(fd, buff, ARRAY_SIZE(buff)); + if (s < 0) { + printf("Cannot write iteration %d\n", iter); + goto error; + } + done += s; + + max_size = 0; + if (kmsg_cmd_get_read_size_max(fd, &max_size)) { + printf("Cannot get max_size\n"); + goto error; + } + if (!max_size) { + printf("Expected non-zero max_size\n"); + goto error; + } + + iter++; + } + + goto cleanup; + +error: + retval = &nok; + +cleanup: + fclose(f); + + return retval; +} + +int kmsg_test_buffer_buf_multithreaded_torture( + const struct kmsg_test_args *args) +{ + int i, j; + int fd = open(DEV_KMSG, O_RDWR); + struct kmsg_cmd_buffer_add cmd = { 0 }; + int minors[] = { -1, -1, -1, -1 }; + FILE *log[ARRAY_SIZE(minors)]; + int retval = KSFT_PASS; + pthread_t threads[ARRAY_SIZE(minors)][THREADS_PER_DEVICE]; + bool started[ARRAY_SIZE(minors)][THREADS_PER_DEVICE]; + uint32_t size; + uint32_t max_size; + void *retptr; + + for (i = 0; i < ARRAY_SIZE(minors); i++) + for (j = 0; j < THREADS_PER_DEVICE; j++) + started[i][j] = false; + + if (fd < 0) { + printf("Failed: cannot open %s\n", DEV_KMSG); + return KSFT_FAIL; + } + + for (i = 0; i < ARRAY_SIZE(minors); i++) { + log[i] = NULL; + cmd.size = KMSG_REQUESTED_BUF_SIZE; + cmd.mode = 0662; + if (kmsg_cmd_buffer_add(fd, &cmd)) { + printf("Failed to add buffer\n"); + goto error; + } + if (cmd.minor < 0) { + printf("Minor number < 0\n"); + goto error; + } + minors[i] = cmd.minor; + + log[i] = kmsg_get_device(minors[i], "r"); + if (!log[i]) { + printf("Cannot get device %d for read\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(log[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + + for (j = 0; j < THREADS_PER_DEVICE; j++) { + if (pthread_create(&threads[i][j], NULL, + kmsg_test_thread_func, &minors[i])) { + printf("Cannot create thread %d for dev %d\n", + j, i); + goto error; + } + started[i][j] = true; + } + } + + goto cleanup; + +error: + retval = KSFT_FAIL; + +cleanup: + for (i = 0; i < ARRAY_SIZE(minors); i++) { + for (j = 0; j < THREADS_PER_DEVICE; j++) + if (started[i][j]) { + if (pthread_join(threads[i][j], &retptr)) { + printf("pthread_join() failed %d:%d\n", + i, j); + retval = KSFT_FAIL; + } + if (!(*((bool *)retptr))) + retval = KSFT_FAIL; + } + if (minors[i] < 0) + continue; + if (log[i]) { + max_size = 0; + if (kmsg_cmd_get_read_size_max(fileno(log[i]), + &max_size)) { + printf("Cannot get max_size\n"); + retval = KSFT_FAIL; + } + if (!max_size) { + printf("Expected non-zero max_size\n"); + retval = KSFT_FAIL; + } + if (kmsg_cmd_clear(fileno(log[i]))) { + printf("Failed to clear device %d\n", i); + retval = KSFT_FAIL; + } + fclose(log[i]); + } + if (kmsg_drop_device(minors[i])) { + printf("Failed to delete device file %d\n", i); + retval = KSFT_FAIL; + } + if (kmsg_cmd_buffer_del(fd, &minors[i])) { + printf("Failed to delete buffer %d\n", i); + retval = KSFT_FAIL; + } + } + close(fd); + return retval; +} diff --git a/tools/testing/selftests/kmsg/test-buffer-buf-torture.c b/tools/testing/selftests/kmsg/test-buffer-buf-torture.c new file mode 100644 index 0000000..829b342 --- /dev/null +++ b/tools/testing/selftests/kmsg/test-buffer-buf-torture.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#include "kmsg-test.h" + +#define SOME_BUFF_SIZE 4096 + +int kmsg_test_buffer_buf_torture(const struct kmsg_test_args *args) +{ + int i, iter; + int fd = open(DEV_KMSG, O_RDWR); + struct kmsg_cmd_buffer_add cmd = { 0 }; + int minors[] = { -1, -1, -1, -1 }; + FILE *fds[ARRAY_SIZE(minors)]; + FILE *log[ARRAY_SIZE(minors)]; + int retval = KSFT_PASS; + char buff[SOME_BUFF_SIZE]; + ssize_t s; + int logfd; + uint32_t size, done; + uint32_t max_size; + + memset(buff, 'A', ARRAY_SIZE(buff)); + buff[ARRAY_SIZE(buff) - 1] = 0; + + if (fd < 0) { + printf("Failed: cannot open %s\n", DEV_KMSG); + return KSFT_FAIL; + } + + for (i = 0; i < ARRAY_SIZE(minors); i++) { + fds[i] = NULL; + log[i] = NULL; + cmd.size = KMSG_REQUESTED_BUF_SIZE; + cmd.mode = 0662; + if (kmsg_cmd_buffer_add(fd, &cmd)) { + printf("Failed to add buffer\n"); + goto error; + } + if (cmd.minor < 0) { + printf("Minor number < 0\n"); + goto error; + } + minors[i] = cmd.minor; + + fds[i] = kmsg_get_device(minors[i], "w"); + if (!fds[i]) { + printf("Cannot get device %d for write\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(fds[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + log[i] = kmsg_get_device(minors[i], "r"); + if (!log[i]) { + printf("Cannot get device %d for read\n", i); + goto error; + } + size = 0; + if (kmsg_cmd_get_buf_size(fileno(log[i]), &size)) { + printf("Cannot get buf size on defice %d\n", i); + goto error; + } + if (size != KMSG_REQUESTED_BUF_SIZE) { + printf("Invalid buf size on device %d\n", i); + goto error; + } + + logfd = fileno(fds[i]); + if (kmsg_cmd_clear(logfd)) { + printf("Cannot clear buffer on device %d\n", i); + goto error; + } + + iter = 0; + while (done < (KMSG_REQUESTED_BUF_SIZE * 2)) { + s = write(logfd, buff, ARRAY_SIZE(buff)); + if (s < 0) { + printf("Cannot write %d to device %d, %s\n", + iter, i, strerror(errno)); + goto error; + } + done += s; + + max_size = 0; + if (kmsg_cmd_get_read_size_max(logfd, &max_size)) { + printf("Cannot get max_size on device %d\n", i); + goto error; + } + if (!max_size) { + printf("Expected non-zero max_size on %d\n", i); + goto error; + } + + iter++; + } + } + + goto cleanup; + +error: + retval = KSFT_FAIL; + +cleanup: + for (i = 0; i < ARRAY_SIZE(minors); i++) { + if (minors[i] < 0) + continue; + if (fds[i]) + fclose(fds[i]); + if (log[i]) { + if (kmsg_cmd_clear(fileno(log[i]))) { + printf("Failed to clear device %d\n", i); + retval = KSFT_FAIL; + } + fclose(log[i]); + } + if (kmsg_drop_device(minors[i])) { + printf("Failed to delete device file %d\n", i); + retval = KSFT_FAIL; + } + if (kmsg_cmd_buffer_del(fd, &minors[i])) { + printf("Failed to delete buffer %d\n", i); + retval = KSFT_FAIL; + } + } + close(fd); + return retval; +} -- 2.7.4 From 7f0f50c4a517fed5afe2df9058bac816a9ac2857 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Fri, 11 Mar 2016 10:51:32 +0900 Subject: [PATCH 11/16] kmsg: set config to use multiple kmssage at TM1 Change-Id: I4eeaaf17b35ecae108d52f67e880e1e374b05955 Signed-off-by: Kichan Kwon --- arch/arm/configs/tizen_tm1_defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm/configs/tizen_tm1_defconfig b/arch/arm/configs/tizen_tm1_defconfig index ebfc0b9..8b898a8 100755 --- a/arch/arm/configs/tizen_tm1_defconfig +++ b/arch/arm/configs/tizen_tm1_defconfig @@ -146,6 +146,7 @@ CONFIG_SYSCTL_SYSCALL=y CONFIG_KALLSYMS=y CONFIG_KALLSYMS_ALL=y CONFIG_PRINTK=y +CONFIG_MULTIPLE_KMSG=y CONFIG_BUG=y CONFIG_ELF_CORE=y CONFIG_BASE_FULL=y -- 2.7.4 From cb8d39c70e6e3245c00bf3c8d9adce447fff619d Mon Sep 17 00:00:00 2001 From: Joonyoung Shim Date: Thu, 11 Feb 2016 11:01:03 +0900 Subject: [PATCH 12/16] drm/sprd: fix always gem creation of imported dma-buf The sprd_prime_import() creates gem object always even though there is existing gem object that refers memory of imported dma-buf. This patch will make to reuse existing gem object on this case. Change-Id: I4aa31bd2a41a511774b9e1aaf150ddbf45728c22 Signed-off-by: Joonyoung Shim --- drivers/gpu/drm/sprd/sprd_drm_gem.c | 15 +++++++++++++++ drivers/staging/android/ion/ion.c | 13 ++++++++++++- drivers/staging/android/ion/ion.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/sprd/sprd_drm_gem.c b/drivers/gpu/drm/sprd/sprd_drm_gem.c index f92a3f1..1fe2363 100755 --- a/drivers/gpu/drm/sprd/sprd_drm_gem.c +++ b/drivers/gpu/drm/sprd/sprd_drm_gem.c @@ -537,6 +537,21 @@ struct drm_gem_object *sprd_prime_import(struct drm_device *dev, goto err; } + obj = ion_get_gem(ion_handle); + if (obj) { + sprd_gem_obj = to_sprd_gem_obj(obj); + if (sprd_gem_obj->buffer->ion_handle != ion_handle) { + DRM_ERROR("Unable get GEM object from ion\n"); + ret = -EINVAL; + goto err; + } + + drm_gem_object_reference(obj); + ion_free(private->sprd_drm_ion_client, ion_handle); + + return obj; + } + buf = sprd_drm_init_buf(dev, size); if (!buf) { DRM_ERROR("Unable to allocate the GEM buffer\n"); diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 7258486..4bbc600 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -576,6 +576,15 @@ struct ion_handle *ion_alloc_with_gem(struct ion_client *client, size_t len, return handle; } EXPORT_SYMBOL(ion_alloc_with_gem); + +struct drm_gem_object *ion_get_gem(struct ion_handle *handle) +{ + if (handle && handle->buffer) + return handle->buffer->obj; + + return NULL; +} +EXPORT_SYMBOL(ion_get_gem); #endif void ion_free(struct ion_client *client, struct ion_handle *handle) @@ -1216,8 +1225,10 @@ static void ion_dma_buf_release(struct dma_buf *dmabuf) ion_buffer_put(buffer); #ifdef CONFIG_DRM_SPRD - if (buffer->obj) + if (buffer->obj) { drm_gem_object_unreference_unlocked(buffer->obj); + buffer->obj = NULL; + } #endif } diff --git a/drivers/staging/android/ion/ion.h b/drivers/staging/android/ion/ion.h index 6be45c2..b489564 100644 --- a/drivers/staging/android/ion/ion.h +++ b/drivers/staging/android/ion/ion.h @@ -127,6 +127,7 @@ struct ion_handle *ion_alloc_with_gem(struct ion_client *client, size_t len, size_t align, unsigned int heap_id_mask, unsigned int flags, struct drm_gem_object *obj); +struct drm_gem_object *ion_get_gem(struct ion_handle *handle); #endif /** * ion_free - free a handle -- 2.7.4 From f3d8ba362cba7aaa8c6fce7046562352033e3743 Mon Sep 17 00:00:00 2001 From: Seung-Woo Kim Date: Tue, 22 Mar 2016 16:29:50 +0900 Subject: [PATCH 13/16] packaging: remove unnecessary dzImage-recovery This patch removes unnecessary dzImage-recovery from the package. Change-Id: I5be95182a69566948930d5821bc7b5ffb8319f81 Signed-off-by: Seung-Woo Kim --- packaging/linux-3.10-sc7730.spec | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packaging/linux-3.10-sc7730.spec b/packaging/linux-3.10-sc7730.spec index cbef595..0e9dd80 100644 --- a/packaging/linux-3.10-sc7730.spec +++ b/packaging/linux-3.10-sc7730.spec @@ -42,12 +42,10 @@ print("\n") print("%files -n linux-3.10-sc7730_"..targets.." \n") print("/boot/kernel/mod_"..targets.." \n") print("/boot/kernel/kernel-"..targets.."/dzImage \n") -print("/boot/kernel/kernel-"..targets.."/dzImage-recovery \n") print("\n") print("%post -n linux-3.10-sc7730_"..targets.." \n") print("cp -r /boot/kernel/mod_"..targets.."/lib/modules/* /lib/modules/. \n") print("mv /boot/kernel/kernel-"..targets.."/dzImage /boot/kernel/. \n") -print("mv /boot/kernel/kernel-"..targets.."/dzImage-recovery /boot/kernel/. \n") print("\n") print("%description -n linux-3.10-sc7730_"..targets.." \n") print("This package provides the sc7730_eur linux kernel image & module.img. \n") @@ -113,7 +111,6 @@ for i in %{BOARDS}; do cp -f arch/arm/boot/zImage %_builddir/zImage.$target cp -f arch/arm/boot/dzImage %_builddir/dzImage.$target - cp -f arch/arm/boot/dzImage %_builddir/dzImage-recovery.$target cp -f System.map %_builddir/System.map.$target cp -f .config %_builddir/config.$target cp -f vmlinux %_builddir/vmlinux.$target @@ -167,7 +164,6 @@ for i in %{BOARDS}; do mv %_builddir/zImage.$target %{buildroot}/boot/kernel/kernel-$i/zImage mv %_builddir/dzImage.$target %{buildroot}/boot/kernel/kernel-$i/dzImage - mv %_builddir/dzImage-recovery.$target %{buildroot}/boot/kernel/kernel-$i/dzImage-recovery mv %_builddir/System.map.$target %{buildroot}/boot/kernel/kernel-$i/System.map mv %_builddir/config.$target %{buildroot}/boot/kernel/kernel-$i/config -- 2.7.4 From 49313f7398637b595b4a3df179aab1133b955042 Mon Sep 17 00:00:00 2001 From: Seung-Woo Kim Date: Wed, 23 Mar 2016 19:55:03 +0900 Subject: [PATCH 14/16] build: change model name as tm1 The model name, tm1 hsould be used for build and module build. So this patch fixes to change model name. Change-Id: I0bf1aeacc54ca1bb88d684161c2ff531d160f1e5 Signed-off-by: Seung-Woo Kim --- release.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/release.sh b/release.sh index da44017..7fa20f1 100755 --- a/release.sh +++ b/release.sh @@ -15,14 +15,11 @@ if [ "${MODEL}" = "" ]; then echo "Warnning: failed to get machine id." echo "ex)./release.sh model_name" echo "ex)--------------------------------------------------" - echo "ex)./release.sh coreprimeve3g" - echo "ex)./release.sh grandprimeve3g" - echo "ex)./release.sh z3lte" - echo "ex)./release.sh z3" + echo "ex)./release.sh tm1" exit fi -if [ ${MODEL} = "coreprimeve3g" -o ${MODEL} = "z3" ]; then +if [ ${MODEL} = "coreprimeve3g" -o ${MODEL} = "tm1" ]; then MODULE=1 else MODULE=0 -- 2.7.4 From 3f4db203e94241c5ce67b2a22319608939961c84 Mon Sep 17 00:00:00 2001 From: Kunhoon Baik Date: Wed, 30 Mar 2016 17:59:31 +0900 Subject: [PATCH 15/16] [Multiple Kmsg] avoid not-allowable mutex lock condition Change-Id: Icd2c90535687558aa3f294471edb865ef178a5b4 --- kernel/printk_kmsg.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c index cfcf3d9..ceb15f6 100644 --- a/kernel/printk_kmsg.c +++ b/kernel/printk_kmsg.c @@ -1016,7 +1016,6 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file, ret = wait_event_interruptible(log_b->wait, user->seq != log_b->next_seq); } else { - rcu_read_unlock(); kref_get(&log_b->refcount); ret = wait_event_interruptible(log_b->wait, user->seq != log_b->next_seq); @@ -1024,7 +1023,6 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file, ret = -ENXIO; if (kref_put(&log_b->refcount, log_buf_release)) ret = -ENXIO; - rcu_read_lock(); } if (ret) goto out; @@ -1129,6 +1127,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, ssize_t ret = -ENXIO; int minor = iminor(file->f_inode); struct log_buffer *log_b; + int found = 0; if (!user) return -EBADF; @@ -1139,11 +1138,17 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, rcu_read_lock(); list_for_each_entry_rcu(log_b, &log_buf.list, list) { if (log_b->minor == minor) { - ret = kmsg_read(log_b, file, buf, count, ppos); + found = 1; + kref_get(&log_b->refcount); break; } } rcu_read_unlock(); + + if(found){ + ret = kmsg_read(log_b, file, buf, count, ppos); + kref_put(&log_b->refcount, log_buf_release); + } return ret; } @@ -1283,6 +1288,7 @@ static int devkmsg_open(struct inode *inode, struct file *file) int ret = -ENXIO; int minor = iminor(file->f_inode); struct log_buffer *log_b; + int found = 0; /* write-only does not need any file context */ if ((file->f_flags & O_ACCMODE) == O_WRONLY) @@ -1300,11 +1306,17 @@ static int devkmsg_open(struct inode *inode, struct file *file) rcu_read_lock(); list_for_each_entry_rcu(log_b, &log_buf.list, list) { if (log_b->minor == minor) { - ret = kmsg_open(log_b, file); + found = 1; + kref_get(&log_b->refcount); break; } } rcu_read_unlock(); + + if(found){ + ret = kmsg_open(log_b, file); + kref_put(&log_b->refcount, log_buf_release); + } return ret; } -- 2.7.4 From c677c469ec0dfc7a84b8157f66c997f586b8d60a Mon Sep 17 00:00:00 2001 From: Joonyoung Shim Date: Tue, 22 Mar 2016 16:39:19 +0900 Subject: [PATCH 16/16] drm/sprd: save pid/tgid in private file data Let's save pid/tgid in private file data only once when gem object is created or prime_fd is imported and use them on gem_info. This can solve wrong pid/tgid information of gem_info node for imported gem object found on tizen_3.0 platform. Change-Id: Icfefe0d140ff2955144d509c862875d2d48241eb Signed-off-by: Joonyoung Shim --- drivers/gpu/drm/sprd/sprd_drm_drv.c | 8 +++++--- drivers/gpu/drm/sprd/sprd_drm_drv.h | 2 ++ drivers/gpu/drm/sprd/sprd_drm_gem.c | 41 +++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/sprd/sprd_drm_gem.h | 3 +++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/sprd/sprd_drm_drv.c b/drivers/gpu/drm/sprd/sprd_drm_drv.c index 6846b40..c9a38aa 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_drv.c +++ b/drivers/gpu/drm/sprd/sprd_drm_drv.c @@ -49,6 +49,8 @@ static int sprd_drm_gem_one_info(int id, void *ptr, void *data) { struct drm_gem_object *obj = (struct drm_gem_object *)ptr; struct sprd_drm_gem_info_data *gem_info_data = data; + struct drm_sprd_file_private *file_priv = + gem_info_data->filp->driver_priv; struct sprd_drm_gem_obj *sprd_gem; struct sprd_drm_gem_buf *buf; @@ -65,8 +67,8 @@ static int sprd_drm_gem_one_info(int id, void *ptr, void *data) seq_printf(gem_info_data->m, "%5d\t%5d\t%4d\t%4d\t\t%4d\t0x%08lx\t0x%x\t%4d\t%4d\t\t" "%4d\t\t0x%p\t%6d\n", - (unsigned long)sprd_gem->pid, - (unsigned long)sprd_gem->tgid, + file_priv->pid, + file_priv->tgid, id, atomic_read(&obj->refcount.refcount) - 1, obj->handle_count, @@ -635,7 +637,7 @@ static struct drm_driver sprd_drm_driver = { .dumb_map_offset = sprd_drm_gem_dumb_map_offset, .dumb_destroy = sprd_drm_gem_dumb_destroy, .prime_handle_to_fd = drm_gem_prime_handle_to_fd, - .prime_fd_to_handle = drm_gem_prime_fd_to_handle, + .prime_fd_to_handle = sprd_drm_gem_prime_fd_to_handle, #ifdef CONFIG_DRM_SPRD_DMABUF .gem_prime_export = sprd_dmabuf_prime_export, .gem_prime_import = sprd_dmabuf_prime_import, diff --git a/drivers/gpu/drm/sprd/sprd_drm_drv.h b/drivers/gpu/drm/sprd/sprd_drm_drv.h index e0dbd4a..f4e63fc 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_drv.h +++ b/drivers/gpu/drm/sprd/sprd_drm_drv.h @@ -45,6 +45,8 @@ struct sprd_drm_ipp_private { struct drm_sprd_file_private { struct sprd_drm_ipp_private *ipp_priv; + pid_t pid; + pid_t tgid; }; /* diff --git a/drivers/gpu/drm/sprd/sprd_drm_gem.c b/drivers/gpu/drm/sprd/sprd_drm_gem.c index 1fe2363..537f2fc 100755 --- a/drivers/gpu/drm/sprd/sprd_drm_gem.c +++ b/drivers/gpu/drm/sprd/sprd_drm_gem.c @@ -282,6 +282,25 @@ static int sprd_drm_gem_handle_create(struct drm_gem_object *obj, return 0; } +static void sprd_drm_gem_register_pid(struct drm_file *file_priv) +{ + struct drm_sprd_file_private *driver_priv = file_priv->driver_priv; + + if (!driver_priv->pid && !driver_priv->tgid) { + driver_priv->pid = task_pid_nr(current); + driver_priv->tgid = task_tgid_nr(current); + } else { + if (driver_priv->pid != task_pid_nr(current)) + DRM_DEBUG_KMS("wrong pid: %ld, %ld\n", + (unsigned long)driver_priv->pid, + (unsigned long)task_pid_nr(current)); + if (driver_priv->tgid != task_tgid_nr(current)) + DRM_DEBUG_KMS("wrong tgid: %ld, %ld\n", + (unsigned long)driver_priv->tgid, + (unsigned long)task_tgid_nr(current)); + } +} + void sprd_drm_gem_destroy(struct sprd_drm_gem_obj *sprd_gem_obj) { struct drm_gem_object *obj; @@ -447,6 +466,8 @@ int sprd_drm_gem_create_ioctl(struct drm_device *dev, void *data, return ret; } + sprd_drm_gem_register_pid(file_priv); + do_gettimeofday(&val_end); time_end = (uint64_t)(val_end.tv_sec * 1000000 + val_end.tv_usec); @@ -481,6 +502,8 @@ int sprd_drm_gem_create_index_ioctl(struct drm_device *dev, void *data, return ret; } + sprd_drm_gem_register_pid(file_priv); + DRM_INFO("%s:h[%d]cnt[%d]sz[%d %d %d]f[0x%x]o[0x%x]a[0x%x]\n", __func__,args->handle, args->bufcount, (int)args->idx_size[0], (int)args->idx_size[1], (int)args->idx_size[2], @@ -649,6 +672,22 @@ err: return ERR_PTR(ret); } +int sprd_drm_gem_prime_fd_to_handle(struct drm_device *dev, + struct drm_file *file_priv, int prime_fd, uint32_t *handle) +{ + int ret; + + ret = drm_gem_prime_fd_to_handle(dev, file_priv, prime_fd, handle); + if (ret < 0) + goto out; + + sprd_drm_gem_register_pid(file_priv); + +out: + return ret; +} + + void *sprd_drm_gem_get_dma_addr(struct drm_device *dev, unsigned int gem_handle, struct drm_file *file_priv) @@ -1093,6 +1132,8 @@ int sprd_drm_gem_dumb_create(struct drm_file *file_priv, return ret; } + sprd_drm_gem_register_pid(file_priv); + return 0; } diff --git a/drivers/gpu/drm/sprd/sprd_drm_gem.h b/drivers/gpu/drm/sprd/sprd_drm_gem.h index 7e787cc..b8acc10 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_gem.h +++ b/drivers/gpu/drm/sprd/sprd_drm_gem.h @@ -126,6 +126,9 @@ struct dma_buf *sprd_prime_export(struct drm_device *dev, struct drm_gem_object *sprd_prime_import(struct drm_device *dev, struct dma_buf *dma_buf); +int sprd_drm_gem_prime_fd_to_handle(struct drm_device *dev, + struct drm_file *file_priv, int prime_fd, uint32_t *handle); + /* * get dma address from gem handle and this function could be used for * other drivers such as 2d/3d acceleration drivers. -- 2.7.4