From 6389a96be03703b039d8720e71b0e58094fa9c76 Mon Sep 17 00:00:00 2001 From: Jin-young Jeon Date: Fri, 22 Jan 2016 12:01:22 +0900 Subject: [PATCH 01/16] Tizen: drm/sprd: change pid information on gem_info. Change-Id: If04b48e0fb9f24f0d2939d5ab7b1f6a62418e188 Signed-off-by: Jin-young Jeon --- drivers/gpu/drm/sprd/sprd_drm_drv.c | 8 ++------ drivers/gpu/drm/sprd/sprd_drm_drv.h | 1 - drivers/gpu/drm/sprd/sprd_drm_gem.c | 3 +++ drivers/gpu/drm/sprd/sprd_drm_gem.h | 5 +++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/sprd/sprd_drm_drv.c b/drivers/gpu/drm/sprd/sprd_drm_drv.c index 4f75743..6846b40 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_drv.c +++ b/drivers/gpu/drm/sprd/sprd_drm_drv.c @@ -49,9 +49,6 @@ 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 pid *pid = gem_info_data->filp->pid; - 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; @@ -68,8 +65,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", - pid_nr(pid), - file_priv->tgid, + (unsigned long)sprd_gem->pid, + (unsigned long)sprd_gem->tgid, id, atomic_read(&obj->refcount.refcount) - 1, obj->handle_count, @@ -309,7 +306,6 @@ static int sprd_drm_open(struct drm_device *dev, struct drm_file *file) if (!file_priv) return -ENOMEM; - file_priv->tgid = task_tgid_nr(current); file->driver_priv = file_priv; return sprd_drm_subdrv_open(dev, file); diff --git a/drivers/gpu/drm/sprd/sprd_drm_drv.h b/drivers/gpu/drm/sprd/sprd_drm_drv.h index dcd0fa0..e0dbd4a 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_drv.h +++ b/drivers/gpu/drm/sprd/sprd_drm_drv.h @@ -45,7 +45,6 @@ struct sprd_drm_ipp_private { struct drm_sprd_file_private { struct sprd_drm_ipp_private *ipp_priv; - pid_t tgid; }; /* diff --git a/drivers/gpu/drm/sprd/sprd_drm_gem.c b/drivers/gpu/drm/sprd/sprd_drm_gem.c index fc8b833..f92a3f1 100755 --- a/drivers/gpu/drm/sprd/sprd_drm_gem.c +++ b/drivers/gpu/drm/sprd/sprd_drm_gem.c @@ -334,6 +334,9 @@ struct sprd_drm_gem_obj *sprd_drm_gem_init(struct drm_device *dev, return NULL; } + sprd_gem_obj->pid = task_pid_nr(current); + sprd_gem_obj->tgid = task_tgid_nr(current); + DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); return sprd_gem_obj; diff --git a/drivers/gpu/drm/sprd/sprd_drm_gem.h b/drivers/gpu/drm/sprd/sprd_drm_gem.h index 9e0de42..7e787cc 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_gem.h +++ b/drivers/gpu/drm/sprd/sprd_drm_gem.h @@ -88,8 +88,9 @@ struct sprd_drm_gem_obj { struct sprd_drm_gem_buf *buffer; unsigned long size; unsigned int flags; - - int lockpid; + int lockpid; + pid_t pid; + pid_t tgid; struct drm_sprd_gem_object_wait_list_entry wait_entries[DRM_SPRD_HANDLE_WAIT_ENTRIES]; -- 2.7.4 From 6140f7a6343f41ee0b47f14ebeda707900068b6a Mon Sep 17 00:00:00 2001 From: Yevgeny Pats Date: Tue, 19 Jan 2016 22:09:04 +0000 Subject: [PATCH 02/16] KEYS: Fix keyring ref leak in join_session_keyring() This fixes CVE-2016-0728. If a thread is asked to join as a session keyring the keyring that's already set as its session, we leak a keyring reference. This can be tested with the following program: #include #include #include #include int main(int argc, const char *argv[]) { int i = 0; key_serial_t serial; serial = keyctl(KEYCTL_JOIN_SESSION_KEYRING, "leaked-keyring"); if (serial < 0) { perror("keyctl"); return -1; } if (keyctl(KEYCTL_SETPERM, serial, KEY_POS_ALL | KEY_USR_ALL) < 0) { perror("keyctl"); return -1; } for (i = 0; i < 100; i++) { serial = keyctl(KEYCTL_JOIN_SESSION_KEYRING, "leaked-keyring"); if (serial < 0) { perror("keyctl"); return -1; } } return 0; } If, after the program has run, there something like the following line in /proc/keys: 3f3d898f I--Q--- 100 perm 3f3f0000 0 0 keyring leaked-keyring: empty with a usage count of 100 * the number of times the program has been run, then the kernel is malfunctioning. If leaked-keyring has zero usages or has been garbage collected, then the problem is fixed. Change-Id: I08e8b8e929575583a94b6c84826c8f05e4dca075 Reported-by: Yevgeny Pats Signed-off-by: David Howells Acked-by: Don Zickus Acked-by: Prarit Bhargava Acked-by: Jarod Wilson Signed-off-by: James Morris [Backport from mainline commit 23567fd052a9abb6d67fe8e7a9ccdd9800a540f2 to resolve CVE-2016-0728] Signed-off-by: Seung-Woo Kim --- security/keys/process_keys.c | 1 + 1 file changed, 1 insertion(+) diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index 42defae..cd871dc 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -792,6 +792,7 @@ long join_session_keyring(const char *name) ret = PTR_ERR(keyring); goto error2; } else if (keyring == new->session_keyring) { + key_put(keyring); ret = 0; goto error2; } -- 2.7.4 From 079d94c2a61b43928cf25c79961841ff080e56a2 Mon Sep 17 00:00:00 2001 From: Joonyoung Shim Date: Wed, 17 Feb 2016 09:53:58 +0900 Subject: [PATCH 03/16] video: sprdfd: disable ESD feature The problem that the screen had been changed to block was reported at some targets. I'm not sure but it might be related with ESD feature of sprdfd driver, so as workaround, disable ESD feature. And add some #ifdef to avoid below build errors. drivers/built-in.o: In function `sprd_enable_vblank': /home/pub/git/public/tm1_tizen_3.0/linux-3.10-sc7730/drivers/gpu/drm/sprd/sprd_drm_irq.c:265: undefined reference to `panel_esd_enable' drivers/built-in.o: In function `sprd_disable_vblank': /home/pub/git/public/tm1_tizen_3.0/linux-3.10-sc7730/drivers/gpu/drm/sprd/sprd_drm_irq.c:284: undefined reference to `panel_esd_enable' Change-Id: I9228d883b0aa874e45b14b8f07cec9810634ab47 Signed-off-by: Joonyoung Shim --- arch/arm/configs/tizen_tm1_defconfig | 2 +- drivers/gpu/drm/sprd/sprd_drm_irq.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/arm/configs/tizen_tm1_defconfig b/arch/arm/configs/tizen_tm1_defconfig index dd5d277..ac4ddff 100755 --- a/arch/arm/configs/tizen_tm1_defconfig +++ b/arch/arm/configs/tizen_tm1_defconfig @@ -2191,7 +2191,7 @@ CONFIG_FB_LCD_DUMMY=y CONFIG_FB_LCD_RESERVE_MEM=y CONFIG_FB_LCD_OVERLAY_SUPPORT=y CONFIG_FB_VSYNC_SUPPORT=y -CONFIG_FB_ESD_SUPPORT=y +# CONFIG_FB_ESD_SUPPORT is not set # CONFIG_FB_DYNAMIC_CLK_SUPPORT is not set # CONFIG_FB_DYNAMIC_FREQ_SCALING is not set # CONFIG_FB_USE_MEGACORES_MIPI_DPHY is not set diff --git a/drivers/gpu/drm/sprd/sprd_drm_irq.c b/drivers/gpu/drm/sprd/sprd_drm_irq.c index 08e90f3..cc46f53 100644 --- a/drivers/gpu/drm/sprd/sprd_drm_irq.c +++ b/drivers/gpu/drm/sprd/sprd_drm_irq.c @@ -19,7 +19,9 @@ #include extern struct drm_device *sprd_drm_dev; +#ifdef CONFIG_FB_ESD_SUPPORT extern void panel_esd_enable (bool enable); +#endif #ifdef CONFIG_OF extern unsigned long g_dispc_base_addr; @@ -262,7 +264,9 @@ int sprd_enable_vblank(struct drm_device *dev, int crtc) atomic_read(&dev_priv->vbl_trg_cnt[crtc])); dev_priv->dbg_cnt = 2; +#ifdef CONFIG_FB_ESD_SUPPORT panel_esd_enable(false); +#endif return 0; } @@ -281,7 +285,9 @@ void sprd_disable_vblank(struct drm_device *dev, int crtc) atomic_read(&dev_priv->vbl_trg_cnt[crtc])); atomic_set(&dev_priv->vbl_trg_cnt[crtc], 0); +#ifdef CONFIG_FB_ESD_SUPPORT panel_esd_enable(true); +#endif } u32 sprd_drm_get_vblank_counter(struct drm_device *dev, int crtc) -- 2.7.4 From d6cde6133ed094ae4dfea8361b269f0f0e80bf81 Mon Sep 17 00:00:00 2001 From: Hyuk Lee Date: Thu, 3 Mar 2016 11:35:41 +0900 Subject: [PATCH 04/16] Enable the Joystick(JOYDEV) Change-Id: Ieedec7196065b8b620b004180b30a9a4cfd4a6ab Signed-off-by: Hyuk Lee --- arch/arm/configs/tizen_tm1_defconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm/configs/tizen_tm1_defconfig b/arch/arm/configs/tizen_tm1_defconfig index ac4ddff..ebfc0b9 100755 --- a/arch/arm/configs/tizen_tm1_defconfig +++ b/arch/arm/configs/tizen_tm1_defconfig @@ -1465,7 +1465,7 @@ CONFIG_INPUT_MATRIXKMAP=y # Userland interfaces # # CONFIG_INPUT_MOUSEDEV is not set -# CONFIG_INPUT_JOYDEV is not set +CONFIG_INPUT_JOYDEV=y CONFIG_INPUT_EVDEV=y # CONFIG_INPUT_EVBUG is not set # CONFIG_INPUT_KEYRESET is not set @@ -1502,7 +1502,7 @@ CONFIG_KEYBOARD_SC=y CONFIG_KEYBOARD_SPRD_EIC=y CONFIG_KEYBOARD_CORERIVER_TOUCHKEY=y # CONFIG_INPUT_MOUSE is not set -# CONFIG_INPUT_JOYSTICK is not set +CONFIG_INPUT_JOYSTICK=y # CONFIG_INPUT_TABLET is not set CONFIG_INPUT_TOUCHSCREEN=y # CONFIG_TOUCHSCREEN_ADS7846 is not set -- 2.7.4 From 3f1f64f58db2b35339f4f880707f3fd2f7be8015 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Thu, 11 Feb 2016 17:52:58 +0100 Subject: [PATCH 05/16] Preparation patch for KMSG. This is the first of a series of patches which implement KMSG, which is a replacement for old android log devices, serving as a fast dlog backend. The patches are not applied onto the original printk file, but to a copy. This is to keep the codebase maintainable. Any future backports will be able to merge conflictlessly and preprocessor macroes are prevented from excessive piling in the original file. This patch adds a MULTIPLE_KMSG config flag and creates the copy of the printk file which will serve as the target of subsequent KMSG patches. Change-Id: Idead56234820ff9d024da88310dc2e505cf051d0 Signed-off-by: Michal Bloch --- init/Kconfig | 6 + kernel/Makefile | 8 +- kernel/printk_kmsg.c | 3028 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 3041 insertions(+), 1 deletion(-) create mode 100644 kernel/printk_kmsg.c diff --git a/init/Kconfig b/init/Kconfig index 79b356f..552088b 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1327,6 +1327,12 @@ config PRINTK very difficult to diagnose system problems, saying N here is strongly discouraged. +config MULTIPLE_KMSG + bool "Enable support for KMSG" + depends on PRINTK + help + Enables mutiple kmsg support. Disabling uses default logger. + config BUG bool "BUG() support" if EXPERT default y diff --git a/kernel/Makefile b/kernel/Makefile index 271fd31..a65f5e2 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,7 +2,7 @@ # Makefile for the linux kernel. # -obj-y = fork.o exec_domain.o panic.o printk.o \ +obj-y = fork.o exec_domain.o panic.o \ cpu.o exit.o itimer.o time.o softirq.o resource.o \ sysctl.o sysctl_binary.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o workqueue.o pid.o task_work.o \ @@ -12,6 +12,12 @@ obj-y = fork.o exec_domain.o panic.o printk.o \ notifier.o ksysfs.o cred.o \ async.o range.o groups.o lglock.o smpboot.o +ifdef CONFIG_MULTIPLE_KMSG +obj-y += printk_kmsg.o +else +obj-y += printk.o +endif + ifdef CONFIG_FUNCTION_TRACER # Do not trace debug files and internal ftrace files CFLAGS_REMOVE_lockdep.o = -pg diff --git a/kernel/printk_kmsg.c b/kernel/printk_kmsg.c new file mode 100644 index 0000000..7480558 --- /dev/null +++ b/kernel/printk_kmsg.c @@ -0,0 +1,3028 @@ +/* + * linux/kernel/printk.c + * + * Copyright (C) 1991, 1992 Linus Torvalds + * + * Modified to make sys_syslog() more flexible: added commands to + * return the last 4k of kernel messages, regardless of whether + * they've been read or not. Added option to suppress kernel printk's + * to the console. Added hook for sending the console messages + * elsewhere, in preparation for a serial line console (someday). + * Ted Ts'o, 2/11/93. + * Modified for sysctl support, 1/8/97, Chris Horn. + * Fixed SMP synchronization, 08/08/99, Manfred Spraul + * manfred@colorfullife.com + * Rewrote bits to get rid of console_lock + * 01Mar01 Andrew Morton + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* For in_interrupt() */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#if defined(CONFIG_SEC_DEBUG) +#include +#include +#endif +#define CREATE_TRACE_POINTS +#include + +#ifdef CONFIG_DEBUG_LL +extern void printascii(char *); +#endif + +/* printk's without a loglevel use this.. */ +#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL + +/* We show everything that is MORE important than this.. */ +#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */ +#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */ + +int console_printk[4] = { + DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */ + DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */ + MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */ + DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */ +}; + +/* + * Low level drivers may need that to know if they can schedule in + * their unblank() callback or not. So let's export it. + */ +int oops_in_progress; +EXPORT_SYMBOL(oops_in_progress); + +/* + * console_sem protects the console_drivers list, and also + * provides serialisation for access to the entire console + * driver system. + */ +static DEFINE_SEMAPHORE(console_sem); +struct console *console_drivers; +EXPORT_SYMBOL_GPL(console_drivers); + +#ifdef CONFIG_LOCKDEP +static struct lockdep_map console_lock_dep_map = { + .name = "console_lock" +}; +#endif + +/* + * This is used for debugging the mess that is the VT code by + * keeping track if we have the console semaphore held. It's + * definitely not the perfect debug tool (we don't know if _WE_ + * hold it are racing, but it helps tracking those weird code + * path in the console code where we end up in places I want + * locked without the console sempahore held + */ +static int console_locked, console_suspended; + +/* + * If exclusive_console is non-NULL then only this console is to be printed to. + */ +static struct console *exclusive_console; + +/* + * Array of consoles built from command line options (console=) + */ +struct console_cmdline +{ + char name[8]; /* Name of the driver */ + int index; /* Minor dev. to use */ + char *options; /* Options for the driver */ +#ifdef CONFIG_A11Y_BRAILLE_CONSOLE + char *brl_options; /* Options for braille driver */ +#endif +}; + +#define MAX_CMDLINECONSOLES 8 + +static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; +static int selected_console = -1; +static int preferred_console = -1; +int console_set_on_cmdline; +EXPORT_SYMBOL(console_set_on_cmdline); + +/* Flag: console code may call schedule() */ +static int console_may_schedule; + +/* + * The printk log buffer consists of a chain of concatenated variable + * length records. Every record starts with a record header, containing + * the overall length of the record. + * + * The heads to the first and last entry in the buffer, as well as the + * sequence numbers of these both entries are maintained when messages + * are stored.. + * + * If the heads indicate available messages, the length in the header + * tells the start next message. A length == 0 for the next message + * indicates a wrap-around to the beginning of the buffer. + * + * Every record carries the monotonic timestamp in microseconds, as well as + * the standard userspace syslog level and syslog facility. The usual + * kernel messages use LOG_KERN; userspace-injected messages always carry + * a matching syslog facility, by default LOG_USER. The origin of every + * message can be reliably determined that way. + * + * The human readable log message directly follows the message header. The + * length of the message text is stored in the header, the stored message + * is not terminated. + * + * Optionally, a message can carry a dictionary of properties (key/value pairs), + * to provide userspace with a machine-readable message context. + * + * Examples for well-defined, commonly used property names are: + * DEVICE=b12:8 device identifier + * b12:8 block dev_t + * c127:3 char dev_t + * n8 netdev ifindex + * +sound:card0 subsystem:devname + * SUBSYSTEM=pci driver-core subsystem name + * + * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value + * follows directly after a '=' character. Every property is terminated by + * a '\0' character. The last property is not terminated. + * + * Example of a message structure: + * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec + * 0008 34 00 record is 52 bytes long + * 000a 0b 00 text is 11 bytes long + * 000c 1f 00 dictionary is 23 bytes long + * 000e 03 00 LOG_KERN (facility) LOG_ERR (level) + * 0010 69 74 27 73 20 61 20 6c "it's a l" + * 69 6e 65 "ine" + * 001b 44 45 56 49 43 "DEVIC" + * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D" + * 52 49 56 45 52 3d 62 75 "RIVER=bu" + * 67 "g" + * 0032 00 00 00 padding to next message header + * + * The 'struct log' buffer header must never be directly exported to + * userspace, it is a kernel-private implementation detail that might + * need to be changed in the future, when the requirements change. + * + * /dev/kmsg exports the structured data in the following line format: + * "level,sequnum,timestamp;\n" + * + * The optional key/value pairs are attached as continuation lines starting + * with a space character and terminated by a newline. All possible + * non-prinatable characters are escaped in the "\xff" notation. + * + * Users of the export format should ignore possible additional values + * separated by ',', and find the message after the ';' character. + */ + +enum log_flags { + LOG_NOCONS = 1, /* already flushed, do not print to console */ + LOG_NEWLINE = 2, /* text ended with a newline */ + LOG_PREFIX = 4, /* text started with a prefix */ + LOG_CONT = 8, /* text is a fragment of a continuation line */ +}; + +struct log { + u64 ts_nsec; /* timestamp in nanoseconds */ + u16 len; /* length of entire record */ + u16 text_len; /* length of text buffer */ + u16 dict_len; /* length of dictionary buffer */ + u8 facility; /* syslog facility */ + u8 flags:5; /* internal record flags */ + u8 level:3; /* syslog level */ +#ifdef CONFIG_PRINTK_PROCESS + char process[16]; /* process Name CONFIG_PRINTK_PROCESS */ + u16 pid; /* process id CONFIG_PRINTK_PROCESS */ + u16 cpu; /* cpu core number CONFIG_PRINTK_PROCESS */ + u8 in_interrupt; /* in interrupt CONFIG_PRINTK_PROCESS */ +#else + int cpu; /* the print cpu */ +#endif +}; + +/* + * The logbuf_lock protects kmsg buffer, indices, counters. It is also + * used in interesting ways to provide interlocking in console_unlock(); + */ +static DEFINE_RAW_SPINLOCK(logbuf_lock); + +#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; +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 +#define PREFIX_MAX 32 +#endif +#define LOG_LINE_MAX 1024 - PREFIX_MAX + +/* record buffer */ +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) +#define LOG_ALIGN 4 +#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; + +/* cpu currently holding logbuf_lock */ +static volatile unsigned int logbuf_cpu = UINT_MAX; + +/* human readable text of the record */ +static char *log_text(const struct log *msg) +{ + return (char *)msg + sizeof(struct log); +} + +/* optional key/value pair dictionary attached to the record */ +static char *log_dict(const struct log *msg) +{ + return (char *)msg + sizeof(struct log) + msg->text_len; +} + +/* get record by index; idx must point to valid msg */ +static struct log *log_from_idx(u32 idx) +{ + struct log *msg = (struct log *)(log_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 msg; +} + +/* get next record; idx must point to valid msg */ +static u32 log_next(u32 idx) +{ + struct log *msg = (struct log *)(log_buf + idx); + + /* length == 0 indicates the end of the buffer; wrap */ + /* + * A length == 0 record is the end of buffer marker. Wrap around and + * read the message at the start of the buffer as *this* one, and + * return the one after that. + */ + if (!msg->len) { + msg = (struct log *)log_buf; + return msg->len; + } + return idx + msg->len; +} + +#ifdef CONFIG_SEC_LOG +static char initial_log_buf[__LOG_BUF_LEN]; +static unsigned int initial_log_idx = 0; +static void (*log_text_hook)(char *text, size_t size); +static char *seclog_buf; +static unsigned *seclog_ptr; +static size_t seclog_size; +static char sec_text[1024]; /* buffer size: LOG_LINE_MAX + PREFIX_MAX */ +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); + if (buf && bufsize) { + seclog_buf = buf; + seclog_ptr = position; + seclog_size = bufsize; + log_text_hook = f; + } + raw_spin_unlock_irqrestore(&logbuf_lock, flags); +} +EXPORT_SYMBOL(register_log_text_hook); +static size_t msg_print_text(const struct log *msg, enum log_flags prev, + bool syslog, char *buf, size_t size); + +#endif +/* insert record into the buffer, discard old ones, update heads */ +static void log_store(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) +{ + struct log *msg; + 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; + + /* drop old messages until we have enough contiuous space */ + log_first_idx = log_next(log_first_idx); + log_first_seq++; + } + + if (log_next_idx + size + sizeof(struct log) >= log_buf_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; + } + + /* fill message */ + msg = (struct log *)(log_buf + log_next_idx); + memcpy(log_text(msg), text, text_len); + msg->text_len = text_len; + memcpy(log_dict(msg), dict, dict_len); + msg->dict_len = dict_len; + msg->facility = facility; + msg->level = level & 7; + msg->flags = flags & 0x1f; + msg->cpu = cpu; + if (ts_nsec > 0) + msg->ts_nsec = ts_nsec; + else + msg->ts_nsec = local_clock(); + memset(log_dict(msg) + dict_len, 0, pad_len); + msg->len = sizeof(struct log) + text_len + dict_len + pad_len; + +#ifdef CONFIG_PRINTK_PROCESS + strncpy(msg->process, current->comm, sizeof(msg->process)); + msg->pid = task_pid_nr(current); + msg->cpu = smp_processor_id(); + msg->in_interrupt = in_interrupt()? 1 : 0; +#endif + +#ifdef CONFIG_SEC_LOG + if (log_text_hook) { + if(initial_log_idx) { + /* Copying of stored initial kernel boot log to + * sec log buffer + */ + log_text_hook(initial_log_buf, initial_log_idx); + initial_log_idx = 0; + } + + size = msg_print_text(msg, msg->flags, true, + sec_text, 1024); + + log_text_hook(sec_text, size); + } else if (initial_log_idx < (__LOG_BUF_LEN)) { + /* Storing of kernel boot logs prior to log_text_hook() + * registration + */ + size = msg_print_text(msg, msg->flags, true, + sec_text, 1024); + memcpy(initial_log_buf + initial_log_idx, sec_text, size); + initial_log_idx += size; + } +#endif + /* insert message */ + log_next_idx += msg->len; + log_next_seq++; +} + +#ifdef CONFIG_SECURITY_DMESG_RESTRICT +int dmesg_restrict = 1; +#else +int dmesg_restrict; +#endif + +static int syslog_action_restricted(int type) +{ + if (dmesg_restrict) + return 1; + /* + * Unless restricted, we allow "read all" and "get buffer size" + * for everybody. + */ + return type != SYSLOG_ACTION_READ_ALL && + type != SYSLOG_ACTION_SIZE_BUFFER; +} + +static int check_syslog_permissions(int type, bool from_file) +{ + /* + * If this is from /proc/kmsg and we've already opened it, then we've + * already done the capabilities checks at open time. + */ + if (from_file && type != SYSLOG_ACTION_OPEN) + return 0; + + if (syslog_action_restricted(type)) { + if (capable(CAP_SYSLOG)) + return 0; + /* + * For historical reasons, accept CAP_SYS_ADMIN too, with + * a warning. + */ + if (capable(CAP_SYS_ADMIN)) { + pr_warn_once("%s (%d): Attempt to access syslog with " + "CAP_SYS_ADMIN but no CAP_SYSLOG " + "(deprecated).\n", + current->comm, task_pid_nr(current)); + return 0; + } + return -EPERM; + } + return security_syslog(type); +} + + +/* /dev/kmsg - userspace message inject/listen interface */ +struct devkmsg_user { + u64 seq; + u32 idx; + enum log_flags prev; + struct mutex lock; + char buf[8192]; +}; + +static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, + unsigned long count, loff_t pos) +{ + char *buf, *line; + int i; + int level = default_message_loglevel; + int facility = 1; /* LOG_USER */ + size_t len = iov_length(iv, count); + ssize_t ret = len; + + if (len > LOG_LINE_MAX) + return -EINVAL; + buf = kmalloc(len+1, GFP_KERNEL); + if (buf == NULL) + return -ENOMEM; + + line = buf; + for (i = 0; i < count; i++) { + if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) { + ret = -EFAULT; + goto out; + } + line += iv[i].iov_len; + } + + /* + * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace + * the decimal value represents 32bit, the lower 3 bit are the log + * level, the rest are the log facility. + * + * If no prefix or no userspace facility is specified, we + * enforce LOG_USER, to be able to reliably distinguish + * kernel-generated messages from userspace-injected ones. + */ + line = buf; + if (line[0] == '<') { + char *endp = NULL; + + i = simple_strtoul(line+1, &endp, 10); + if (endp && endp[0] == '>') { + level = i & 7; + if (i >> 3) + facility = i >> 3; + endp++; + len -= endp - line; + line = endp; + } + } + line[len] = '\0'; + + printk_emit(facility, level, NULL, 0, "%s", line); +out: + kfree(buf); + return ret; +} + +static ssize_t devkmsg_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct devkmsg_user *user = file->private_data; + struct log *msg; + u64 ts_usec; + size_t i; + char cont = '-'; + size_t len; + ssize_t ret; + + if (!user) + return -EBADF; + + ret = mutex_lock_interruptible(&user->lock); + if (ret) + return ret; + raw_spin_lock_irq(&logbuf_lock); + while (user->seq == log_next_seq) { + if (file->f_flags & O_NONBLOCK) { + ret = -EAGAIN; + raw_spin_unlock_irq(&logbuf_lock); + goto out; + } + + raw_spin_unlock_irq(&logbuf_lock); + ret = wait_event_interruptible(log_wait, + user->seq != log_next_seq); + if (ret) + goto out; + raw_spin_lock_irq(&logbuf_lock); + } + + if (user->seq < log_first_seq) { + /* our last seen message is gone, return error and reset */ + user->idx = log_first_idx; + user->seq = log_first_seq; + ret = -EPIPE; + raw_spin_unlock_irq(&logbuf_lock); + goto out; + } + + msg = log_from_idx(user->idx); + ts_usec = msg->ts_nsec; + do_div(ts_usec, 1000); + + /* + * If we couldn't merge continuation line fragments during the print, + * export the stored flags to allow an optional external merge of the + * records. Merging the records isn't always neccessarily correct, like + * when we hit a race during printing. In most cases though, it produces + * better readable output. 'c' in the record flags mark the first + * fragment of a line, '+' the following. + */ + if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT)) + cont = 'c'; + else if ((msg->flags & LOG_CONT) || + ((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); + user->prev = msg->flags; + + /* escape non-printable characters */ + for (i = 0; i < msg->text_len; i++) { + unsigned char c = log_text(msg)[i]; + + if (c < ' ' || c >= 127 || c == '\\') + len += sprintf(user->buf + len, "\\x%02x", c); + else + user->buf[len++] = c; + } + user->buf[len++] = '\n'; + + if (msg->dict_len) { + bool line = true; + + for (i = 0; i < msg->dict_len; i++) { + unsigned char c = log_dict(msg)[i]; + + if (line) { + user->buf[len++] = ' '; + line = false; + } + + if (c == '\0') { + user->buf[len++] = '\n'; + line = true; + continue; + } + + if (c < ' ' || c >= 127 || c == '\\') { + len += sprintf(user->buf + len, "\\x%02x", c); + continue; + } + + user->buf[len++] = c; + } + user->buf[len++] = '\n'; + } + + user->idx = log_next(user->idx); + user->seq++; + raw_spin_unlock_irq(&logbuf_lock); + + if (len > count) { + ret = -EINVAL; + goto out; + } + + if (copy_to_user(buf, user->buf, len)) { + ret = -EFAULT; + goto out; + } + ret = len; +out: + mutex_unlock(&user->lock); + return ret; +} + +static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) +{ + struct devkmsg_user *user = file->private_data; + loff_t ret = 0; + + if (!user) + return -EBADF; + if (offset) + return -ESPIPE; + + raw_spin_lock_irq(&logbuf_lock); + switch (whence) { + case SEEK_SET: + /* the first record */ + user->idx = log_first_idx; + user->seq = log_first_seq; + break; + case SEEK_DATA: + /* + * 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. + */ + user->idx = clear_idx; + user->seq = clear_seq; + break; + case SEEK_END: + /* after the last record */ + user->idx = log_next_idx; + user->seq = log_next_seq; + break; + default: + ret = -EINVAL; + } + raw_spin_unlock_irq(&logbuf_lock); + return ret; +} + +static unsigned int devkmsg_poll(struct file *file, poll_table *wait) +{ + struct devkmsg_user *user = file->private_data; + int ret = 0; + + if (!user) + return POLLERR|POLLNVAL; + + poll_wait(file, &log_wait, wait); + + raw_spin_lock_irq(&logbuf_lock); + if (user->seq < log_next_seq) { + /* return error when data has vanished underneath us */ + if (user->seq < log_first_seq) + ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI; + else + ret = POLLIN|POLLRDNORM; + } + raw_spin_unlock_irq(&logbuf_lock); + + return ret; +} + +static int devkmsg_open(struct inode *inode, struct file *file) +{ + struct devkmsg_user *user; + int err; + + /* write-only does not need any file context */ + if ((file->f_flags & O_ACCMODE) == O_WRONLY) + return 0; + + err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL, + SYSLOG_FROM_READER); + if (err) + return err; + + user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); + if (!user) + return -ENOMEM; + + 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); + + file->private_data = user; + return 0; +} + +static int devkmsg_release(struct inode *inode, struct file *file) +{ + struct devkmsg_user *user = file->private_data; + + if (!user) + return 0; + + mutex_destroy(&user->lock); + kfree(user); + return 0; +} + +const struct file_operations kmsg_fops = { + .open = devkmsg_open, + .read = devkmsg_read, + .aio_write = devkmsg_writev, + .llseek = devkmsg_llseek, + .poll = devkmsg_poll, + .release = devkmsg_release, +}; + +#ifdef CONFIG_KEXEC +/* + * This appends the listed symbols to /proc/vmcoreinfo + * + * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to + * obtain access to symbols that are otherwise very difficult to locate. These + * symbols are specifically used so that utilities can access and extract the + * dmesg log from a vmcore file after a crash. + */ +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); + /* + * Export struct log size and field offsets. User space tools can + * parse it and detect any changes to structure down the line. + */ + VMCOREINFO_STRUCT_SIZE(log); + VMCOREINFO_OFFSET(log, ts_nsec); + VMCOREINFO_OFFSET(log, len); + VMCOREINFO_OFFSET(log, text_len); + VMCOREINFO_OFFSET(log, dict_len); +} +#endif + +/* requested log_buf_len from kernel cmdline */ +static unsigned long __initdata new_log_buf_len; + +/* save requested log_buf_len since it's too early to process it */ +static int __init log_buf_len_setup(char *str) +{ + unsigned size = memparse(str, &str); + + if (size) + size = roundup_pow_of_two(size); + if (size > log_buf_len) + new_log_buf_len = size; + + return 0; +} +early_param("log_buf_len", log_buf_len_setup); + +void __init setup_log_buf(int early) +{ + unsigned long flags; + char *new_log_buf; + int free; + + if (!new_log_buf_len) + return; + + if (early) { + unsigned long mem; + + mem = memblock_alloc(new_log_buf_len, PAGE_SIZE); + if (!mem) + return; + new_log_buf = __va(mem); + } else { + new_log_buf = alloc_bootmem_nopanic(new_log_buf_len); + } + + if (unlikely(!new_log_buf)) { + 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; + 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); + + 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); +} + +static bool __read_mostly ignore_loglevel; + +static int __init ignore_loglevel_setup(char *str) +{ + ignore_loglevel = 1; + printk(KERN_INFO "debug: ignoring loglevel setting.\n"); + + return 0; +} + +early_param("ignore_loglevel", ignore_loglevel_setup); +module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to" + "print all kernel messages to the console."); + +#ifdef CONFIG_BOOT_PRINTK_DELAY + +static int boot_delay; /* msecs delay after each printk during bootup */ +static unsigned long long loops_per_msec; /* based on boot_delay */ + +static int __init boot_delay_setup(char *str) +{ + unsigned long lpj; + + lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ + loops_per_msec = (unsigned long long)lpj / 1000 * HZ; + + get_option(&str, &boot_delay); + if (boot_delay > 10 * 1000) + boot_delay = 0; + + pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " + "HZ: %d, loops_per_msec: %llu\n", + boot_delay, preset_lpj, lpj, HZ, loops_per_msec); + return 1; +} +__setup("boot_delay=", boot_delay_setup); + +static void boot_delay_msec(int level) +{ + unsigned long long k; + unsigned long timeout; + + if ((boot_delay == 0 || system_state != SYSTEM_BOOTING) + || (level >= console_loglevel && !ignore_loglevel)) { + return; + } + + k = (unsigned long long)loops_per_msec * boot_delay; + + timeout = jiffies + msecs_to_jiffies(boot_delay); + while (k) { + k--; + cpu_relax(); + /* + * use (volatile) jiffies to prevent + * compiler reduction; loop termination via jiffies + * is secondary and may or may not happen. + */ + if (time_after(jiffies, timeout)) + break; + touch_nmi_watchdog(); + } +} +#else +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) +{ + if (!buf) + return snprintf(NULL, 0, "%c[%1d:%15s:%5d] ", ' ', 0, " ", 0); + + return sprintf(buf, "%c[%1d:%15s:%5d] ", + msg->in_interrupt ? 'I' : ' ', + msg->cpu, + msg->process, + msg->pid); +} +#endif + +static size_t print_prefix(const struct log *msg, bool syslog, char *buf) +{ + size_t len = 0; + unsigned int prefix = (msg->facility << 3) | msg->level; + + if (syslog) { + if (buf) { + len += sprintf(buf, "<%u>", prefix); + } else { + len += 3; + if (prefix > 999) + len += 3; + else if (prefix > 99) + len += 2; + else if (prefix > 9) + len++; + } + } + + len += print_time(msg->ts_nsec, buf ? buf + len : NULL); +#ifdef CONFIG_PRINTK_PROCESS + len += print_process(msg, buf ? buf + len : NULL); +#endif + return len; +} + +static size_t msg_print_text(const struct log *msg, enum log_flags prev, + bool syslog, char *buf, size_t size) +{ + const char *text = log_text(msg); + size_t text_size = msg->text_len; + bool prefix = true; + bool newline = true; + size_t len = 0; + + if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)) + prefix = false; + + if (msg->flags & LOG_CONT) { + if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE)) + prefix = false; + + if (!(msg->flags & LOG_NEWLINE)) + newline = false; + } + + do { + const char *next = memchr(text, '\n', text_size); + size_t text_len; + + if (next) { + text_len = next - text; + next++; + text_size -= next - text; + } else { + text_len = text_size; + } + + if (buf) { + if (print_prefix(msg, syslog, NULL) + + text_len + 1 >= size - len) + break; + + if (prefix) + len += print_prefix(msg, syslog, buf + len); + memcpy(buf + len, text, text_len); + len += text_len; + if (next || newline) + buf[len++] = '\n'; + } else { + /* SYSLOG_ACTION_* buffer size only calculation */ + if (prefix) + len += print_prefix(msg, syslog, NULL); + len += text_len; + if (next || newline) + len++; + } + + prefix = true; + text = next; + } while (text); + + return len; +} + +static int syslog_print(char __user *buf, int size) +{ + char *text; + struct log *msg; + int len = 0; + + text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); + if (!text) + return -ENOMEM; + + while (size > 0) { + size_t n; + size_t skip; + + raw_spin_lock_irq(&logbuf_lock); + if (syslog_seq < log_first_seq) { + /* messages are gone, move to first one */ + syslog_seq = log_first_seq; + syslog_idx = log_first_idx; + syslog_prev = 0; + syslog_partial = 0; + } + if (syslog_seq == log_next_seq) { + raw_spin_unlock_irq(&logbuf_lock); + break; + } + + skip = syslog_partial; + msg = log_from_idx(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_seq++; + syslog_prev = msg->flags; + n -= syslog_partial; + syslog_partial = 0; + } else if (!len){ + /* partial read(), remember position */ + n = size; + syslog_partial += n; + } else + n = 0; + raw_spin_unlock_irq(&logbuf_lock); + + if (!n) + break; + + if (copy_to_user(buf, text + skip, n)) { + if (!len) + len = -EFAULT; + break; + } + + len += n; + size -= n; + buf += n; + } + + kfree(text); + return len; +} + +static int syslog_print_all(char __user *buf, int size, bool clear) +{ + char *text; + int len = 0; + + text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); + if (!text) + return -ENOMEM; + + raw_spin_lock_irq(&logbuf_lock); + if (buf) { + u64 next_seq; + u64 seq; + u32 idx; + enum log_flags prev; + + if (clear_seq < log_first_seq) { + /* messages are gone, move to first available one */ + clear_seq = log_first_seq; + clear_idx = log_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; + prev = 0; + while (seq < log_next_seq) { + struct log *msg = log_from_idx(idx); + + len += msg_print_text(msg, prev, true, NULL, 0); + prev = msg->flags; + idx = log_next(idx); + seq++; + } + + /* move first record forward until length fits into the buffer */ + seq = clear_seq; + idx = clear_idx; + prev = 0; + while (len > size && seq < log_next_seq) { + struct log *msg = log_from_idx(idx); + + len -= msg_print_text(msg, prev, true, NULL, 0); + prev = msg->flags; + idx = log_next(idx); + seq++; + } + + /* last message fitting into this dump */ + next_seq = log_next_seq; + + len = 0; + prev = 0; + while (len >= 0 && seq < next_seq) { + struct log *msg = log_from_idx(idx); + int textlen; + + textlen = msg_print_text(msg, prev, true, text, + LOG_LINE_MAX + PREFIX_MAX); + if (textlen < 0) { + len = textlen; + break; + } + idx = log_next(idx); + seq++; + prev = msg->flags; + + raw_spin_unlock_irq(&logbuf_lock); + if (copy_to_user(buf + len, text, textlen)) + len = -EFAULT; + else + len += textlen; + raw_spin_lock_irq(&logbuf_lock); + + if (seq < log_first_seq) { + /* messages are gone, move to next one */ + seq = log_first_seq; + idx = log_first_idx; + prev = 0; + } + } + } + + if (clear) { + clear_seq = log_next_seq; + clear_idx = log_next_idx; + } + raw_spin_unlock_irq(&logbuf_lock); + + kfree(text); + return len; +} + +int do_syslog(int type, char __user *buf, int len, bool from_file) +{ + bool clear = false; + static int saved_console_loglevel = -1; + int error; + + error = check_syslog_permissions(type, from_file); + if (error) + goto out; + + error = security_syslog(type); + if (error) + return error; + + switch (type) { + case SYSLOG_ACTION_CLOSE: /* Close log */ + break; + case SYSLOG_ACTION_OPEN: /* Open log */ + break; + case SYSLOG_ACTION_READ: /* Read from log */ + error = -EINVAL; + if (!buf || len < 0) + goto out; + error = 0; + if (!len) + goto out; + if (!access_ok(VERIFY_WRITE, buf, len)) { + error = -EFAULT; + goto out; + } + error = wait_event_interruptible(log_wait, + syslog_seq != log_next_seq); + if (error) + goto out; + error = syslog_print(buf, len); + break; + /* Read/clear last kernel messages */ + case SYSLOG_ACTION_READ_CLEAR: + clear = true; + /* FALL THRU */ + /* Read last kernel messages */ + case SYSLOG_ACTION_READ_ALL: + error = -EINVAL; + if (!buf || len < 0) + goto out; + error = 0; + if (!len) + goto out; + if (!access_ok(VERIFY_WRITE, buf, len)) { + error = -EFAULT; + goto out; + } + error = syslog_print_all(buf, len, clear); + break; + /* Clear ring buffer */ + case SYSLOG_ACTION_CLEAR: + syslog_print_all(NULL, 0, true); + break; + /* Disable logging to console */ + case SYSLOG_ACTION_CONSOLE_OFF: + if (saved_console_loglevel == -1) + saved_console_loglevel = console_loglevel; + console_loglevel = minimum_console_loglevel; + break; + /* Enable logging to console */ + case SYSLOG_ACTION_CONSOLE_ON: + if (saved_console_loglevel != -1) { + console_loglevel = saved_console_loglevel; + saved_console_loglevel = -1; + } + break; + /* Set level of messages printed to console */ + case SYSLOG_ACTION_CONSOLE_LEVEL: + error = -EINVAL; + if (len < 1 || len > 8) + goto out; + if (len < minimum_console_loglevel) + len = minimum_console_loglevel; + console_loglevel = len; + /* Implicitly re-enable logging to console */ + saved_console_loglevel = -1; + error = 0; + 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) { + /* messages are gone, move to first one */ + syslog_seq = log_first_seq; + syslog_idx = log_first_idx; + syslog_prev = 0; + syslog_partial = 0; + } + if (from_file) { + /* + * Short-cut for poll(/"proc/kmsg") which simply checks + * for pending data, not the size; return the count of + * records, not the length. + */ + error = log_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); + + error += msg_print_text(msg, prev, true, NULL, 0); + idx = log_next(idx); + seq++; + prev = msg->flags; + } + error -= syslog_partial; + } + raw_spin_unlock_irq(&logbuf_lock); + break; + /* Size of the log buffer */ + case SYSLOG_ACTION_SIZE_BUFFER: + error = log_buf_len; + break; + default: + error = -EINVAL; + break; + } +out: + return error; +} + +SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) +{ + return do_syslog(type, buf, len, SYSLOG_FROM_READER); +} + +/* + * Call the console drivers, asking them to write out + * log_buf[start] to log_buf[end - 1]. + * The console_lock must be held. + */ +static void call_console_drivers(int level, const char *text, size_t len) +{ + struct console *con; + + trace_console(text, len); + + if (level >= console_loglevel && !ignore_loglevel) + return; + if (!console_drivers) + return; + + for_each_console(con) { + if (exclusive_console && con != exclusive_console) + continue; + if (!(con->flags & CON_ENABLED)) + continue; + if (!con->write) + continue; + if (!cpu_online(smp_processor_id()) && + !(con->flags & CON_ANYTIME)) + continue; + con->write(con, text, len); + } +} + +/* + * Zap console related locks when oopsing. Only zap at most once + * every 10 seconds, to leave time for slow consoles to print a + * full oops. + */ +static void zap_locks(void) +{ + static unsigned long oops_timestamp; + + if (time_after_eq(jiffies, oops_timestamp) && + !time_after(jiffies, oops_timestamp + 30 * HZ)) + return; + + oops_timestamp = jiffies; + + debug_locks_off(); + /* If a crash is occurring, make sure we can't deadlock */ + raw_spin_lock_init(&logbuf_lock); + /* And make sure that we print immediately */ + sema_init(&console_sem, 1); +} + +/* Check if we have any console registered that can be called early in boot. */ +static int have_callable_console(void) +{ + struct console *con; + + for_each_console(con) + if (con->flags & CON_ANYTIME) + return 1; + + return 0; +} + +/* + * Can we actually use the console at this time on this cpu? + * + * Console drivers may assume that per-cpu resources have + * been allocated. So unless they're explicitly marked as + * being able to cope (CON_ANYTIME) don't call them until + * this CPU is officially up. + */ +static inline int can_use_console(unsigned int cpu) +{ + return cpu_online(cpu) || have_callable_console(); +} + +/* + * Try to get console ownership to actually show the kernel + * messages from a 'printk'. Return true (and with the + * console_lock held, and 'console_locked' set) if it + * is successful, false otherwise. + * + * This gets called with the 'logbuf_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) +{ + int retval = 0, wake = 0; + + if (console_trylock()) { + retval = 1; + + /* + * If we can't use the console, we need to release + * the console semaphore by hand to avoid flushing + * the buffer. We need to hold the console semaphore + * in order to do this test safely. + */ + if (!can_use_console(cpu)) { + console_locked = 0; + wake = 1; + retval = 0; + } + } + logbuf_cpu = UINT_MAX; + raw_spin_unlock(&logbuf_lock); + if (wake) + up(&console_sem); + return retval; +} + +int printk_delay_msec __read_mostly; + +static inline void printk_delay(void) +{ + if (unlikely(printk_delay_msec)) { + int m = printk_delay_msec; + + while (m--) { + mdelay(1); + touch_nmi_watchdog(); + } + } +} + +/* + * 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) +{ + 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; + + boot_delay_msec(level); + printk_delay(); + + /* This stops the holder of console_sem just where we want him */ + local_irq_save(flags); + this_cpu = smp_processor_id(); + + /* + * Ouch, printk recursed into itself! + */ + if (unlikely(logbuf_cpu == this_cpu)) { + /* + * If a crash is occurring during printk() on this CPU, + * then try to get the crash message out but make sure + * we can't deadlock. Otherwise just return to avoid the + * recursion and return - but flag the recursion so that + * it can be printed at the next appropriate moment: + */ + if (!oops_in_progress && !lockdep_recursing(current)) { + recursion_bug = 1; + goto out_restore_irqs; + } + zap_locks(); + } + + lockdep_off(); + raw_spin_lock(&logbuf_lock); + logbuf_cpu = this_cpu; + + if (recursion_bug) { + static const char recursion_msg[] = + "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); + } + + /* + * 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; + + /* + * 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' + * regardless of whether it actually gets the console semaphore or not. + */ + if (console_trylock_for_printk(this_cpu)) + console_unlock(); + + lockdep_on(); +out_restore_irqs: + local_irq_restore(flags); + + return printed_len; +} +EXPORT_SYMBOL(vprintk_emit); + +asmlinkage int vprintk(const char *fmt, va_list args) +{ + return vprintk_emit(0, -1, NULL, 0, fmt, args); +} +EXPORT_SYMBOL(vprintk); + +asmlinkage int printk_emit(int facility, int level, + const char *dict, size_t dictlen, + const char *fmt, ...) +{ + va_list args; + int r; + + va_start(args, fmt); + r = vprintk_emit(facility, level, dict, dictlen, fmt, args); + va_end(args); + + return r; +} +EXPORT_SYMBOL(printk_emit); + +/** + * printk - print a kernel message + * @fmt: format string + * + * This is printk(). It can be called from any context. We want it to work. + * + * We try to grab the console_lock. If we succeed, it's easy - we log the + * output and call the console drivers. If we fail to get the semaphore, we + * place the output into the log buffer and return. The current holder of + * the console_sem will notice the new output in console_unlock(); and will + * send it to the consoles before releasing the lock. + * + * One effect of this deferred printing is that code which calls printk() and + * then changes console_loglevel may break. This is because console_loglevel + * is inspected when the actual printing occurs. + * + * See also: + * printf(3) + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +asmlinkage int printk(const char *fmt, ...) +{ + va_list args; + int r; + +#ifdef CONFIG_KGDB_KDB + if (unlikely(kdb_trap_printk)) { + va_start(args, fmt); + r = vkdb_printf(fmt, args); + va_end(args); + return r; + } +#endif + va_start(args, fmt); + r = vprintk_emit(0, -1, NULL, 0, fmt, args); + va_end(args); + + return r; +} +EXPORT_SYMBOL(printk); + +#else /* CONFIG_PRINTK */ + +#define LOG_LINE_MAX 0 +#define PREFIX_MAX 0 +#define LOG_LINE_MAX 0 +static u64 syslog_seq; +static u32 syslog_idx; +static u64 console_seq; +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; + size_t cons; + 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 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; } +static size_t cont_print_text(char *text, size_t size) { return 0; } + +#endif /* CONFIG_PRINTK */ + +#ifdef CONFIG_EARLY_PRINTK +struct console *early_console; + +void early_vprintk(const char *fmt, va_list ap) +{ + if (early_console) { + char buf[512]; + int n = vscnprintf(buf, sizeof(buf), fmt, ap); + + early_console->write(early_console, buf, n); + } +} + +asmlinkage void early_printk(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + early_vprintk(fmt, ap); + va_end(ap); +} +#endif + +static int __add_preferred_console(char *name, int idx, char *options, + char *brl_options) +{ + struct console_cmdline *c; + int i; + + /* + * See if this tty is not yet registered, and + * if we have a slot free. + */ + for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) + if (strcmp(console_cmdline[i].name, name) == 0 && + console_cmdline[i].index == idx) { + if (!brl_options) + selected_console = i; + return 0; + } + if (i == MAX_CMDLINECONSOLES) + return -E2BIG; + if (!brl_options) + selected_console = i; + c = &console_cmdline[i]; + strlcpy(c->name, name, sizeof(c->name)); + c->options = options; +#ifdef CONFIG_A11Y_BRAILLE_CONSOLE + c->brl_options = brl_options; +#endif + c->index = idx; + return 0; +} +/* + * Set up a list of consoles. Called from init/main.c + */ +static int __init console_setup(char *str) +{ + char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */ + char *s, *options, *brl_options = NULL; + int idx; + +#ifdef CONFIG_A11Y_BRAILLE_CONSOLE + if (!memcmp(str, "brl,", 4)) { + brl_options = ""; + str += 4; + } else if (!memcmp(str, "brl=", 4)) { + brl_options = str + 4; + str = strchr(brl_options, ','); + if (!str) { + printk(KERN_ERR "need port name after brl=\n"); + return 1; + } + *(str++) = 0; + } +#endif + + /* + * Decode str into name, index, options. + */ + if (str[0] >= '0' && str[0] <= '9') { + strcpy(buf, "ttyS"); + strncpy(buf + 4, str, sizeof(buf) - 5); + } else { + strncpy(buf, str, sizeof(buf) - 1); + } + buf[sizeof(buf) - 1] = 0; + if ((options = strchr(str, ',')) != NULL) + *(options++) = 0; +#ifdef __sparc__ + if (!strcmp(str, "ttya")) + strcpy(buf, "ttyS0"); + if (!strcmp(str, "ttyb")) + strcpy(buf, "ttyS1"); +#endif + for (s = buf; *s; s++) + if ((*s >= '0' && *s <= '9') || *s == ',') + break; + idx = simple_strtoul(s, NULL, 10); + *s = 0; + + __add_preferred_console(buf, idx, options, brl_options); + console_set_on_cmdline = 1; + return 1; +} +__setup("console=", console_setup); + +/** + * add_preferred_console - add a device to the list of preferred consoles. + * @name: device name + * @idx: device index + * @options: options for this console + * + * The last preferred console added will be used for kernel messages + * and stdin/out/err for init. Normally this is used by console_setup + * above to handle user-supplied console arguments; however it can also + * be used by arch-specific code either to override the user or more + * commonly to provide a default console (ie from PROM variables) when + * the user has not supplied one. + */ +int add_preferred_console(char *name, int idx, char *options) +{ + return __add_preferred_console(name, idx, options, NULL); +} + +int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options) +{ + struct console_cmdline *c; + int i; + + for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) + if (strcmp(console_cmdline[i].name, name) == 0 && + console_cmdline[i].index == idx) { + c = &console_cmdline[i]; + strlcpy(c->name, name_new, sizeof(c->name)); + c->name[sizeof(c->name) - 1] = 0; + c->options = options; + c->index = idx_new; + return i; + } + /* not found */ + return -1; +} + +bool console_suspend_enabled = 1; +EXPORT_SYMBOL(console_suspend_enabled); + +static int __init console_suspend_disable(char *str) +{ + console_suspend_enabled = 0; + return 1; +} +__setup("no_console_suspend", console_suspend_disable); +module_param_named(console_suspend, console_suspend_enabled, + bool, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(console_suspend, "suspend console during suspend" + " and hibernate operations"); + +/** + * suspend_console - suspend the console subsystem + * + * This disables printk() while we go into suspend states + */ +void suspend_console(void) +{ + if (!console_suspend_enabled) + return; + printk("Suspending console(s) (use no_console_suspend to debug)\n"); + console_lock(); + console_suspended = 1; + up(&console_sem); +} + +void resume_console(void) +{ + if (!console_suspend_enabled) + return; + down(&console_sem); + console_suspended = 0; + console_unlock(); +} + +/** + * console_cpu_notify - print deferred console messages after CPU hotplug + * @self: notifier struct + * @action: CPU hotplug event + * @hcpu: unused + * + * If printk() is called from a CPU that is not online yet, the messages + * will be spooled but will not show up on the console. This function is + * called when a new CPU comes online (or fails to come up), and ensures + * that any such output gets printed. + */ +static int __cpuinit console_cpu_notify(struct notifier_block *self, + unsigned long action, void *hcpu) +{ + switch (action) { + case CPU_ONLINE: + case CPU_DEAD: + case CPU_DOWN_FAILED: + case CPU_UP_CANCELED: + console_lock(); + console_unlock(); + } + return NOTIFY_OK; +} + +/** + * console_lock - lock the console system for exclusive use. + * + * Acquires a lock which guarantees that the caller has + * exclusive access to the console system and the console_drivers list. + * + * Can sleep, returns nothing. + */ +void console_lock(void) +{ + might_sleep(); + + down(&console_sem); + if (console_suspended) + return; + console_locked = 1; + console_may_schedule = 1; + mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_); +} +EXPORT_SYMBOL(console_lock); + +/** + * console_trylock - try to lock the console system for exclusive use. + * + * Tried to acquire a lock which guarantees that the caller has + * exclusive access to the console system and the console_drivers list. + * + * returns 1 on success, and 0 on failure to acquire the lock. + */ +int console_trylock(void) +{ + if (down_trylock(&console_sem)) + return 0; + if (console_suspended) { + up(&console_sem); + return 0; + } + console_locked = 1; + console_may_schedule = 0; + mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_); + return 1; +} +EXPORT_SYMBOL(console_trylock); + +int is_console_locked(void) +{ + return console_locked; +} + +static void console_cont_flush(char *text, size_t size) +{ + unsigned long flags; + size_t len; + + raw_spin_lock_irqsave(&logbuf_lock, flags); + + if (!cont.len) + goto out; + + /* + * We still queue earlier records, likely because the console was + * 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) + goto out; + + len = cont_print_text(text, size); + raw_spin_unlock(&logbuf_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); +} + +/** + * console_unlock - unlock the console system + * + * Releases the console_lock which the caller holds on the console system + * and the console driver list. + * + * While the console_lock was held, console output may have been buffered + * by printk(). If this is the case, console_unlock(); emits + * the output prior to releasing the lock. + * + * If there is output waiting, we wake /dev/kmsg and syslog() users. + * + * console_unlock(); may be called from any context. + */ +void console_unlock(void) +{ + static char text[LOG_LINE_MAX + PREFIX_MAX]; + static u64 seen_seq; + unsigned long flags; + bool wake_klogd = false; + bool retry; + + if (console_suspended) { + up(&console_sem); + return; + } + + console_may_schedule = 0; + + /* flush buffered message fragment immediately to console */ + console_cont_flush(text, sizeof(text)); +again: + for (;;) { + struct log *msg; + size_t len; + int level; + + raw_spin_lock_irqsave(&logbuf_lock, flags); + if (seen_seq != log_next_seq) { + wake_klogd = true; + seen_seq = log_next_seq; + } + + if (console_seq < log_first_seq) { + /* messages are gone, move to first one */ + console_seq = log_first_seq; + console_idx = log_first_idx; + console_prev = 0; + } +skip: + if (console_seq == log_next_seq) + break; + + msg = log_from_idx(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_seq++; + /* + * We will get here again when we register a new + * CON_PRINTBUFFER console. Clear the flag so we + * will properly dump everything later. + */ + msg->flags &= ~LOG_NOCONS; + console_prev = msg->flags; + goto skip; + } + + level = msg->level; + len = msg_print_text(msg, console_prev, false, + text, sizeof(text)); + console_idx = log_next(console_idx); + console_seq++; + console_prev = msg->flags; + raw_spin_unlock(&logbuf_lock); + + stop_critical_timings(); /* don't trace print latency */ + call_console_drivers(level, text, len); + start_critical_timings(); + local_irq_restore(flags); + } + console_locked = 0; + mutex_release(&console_lock_dep_map, 1, _RET_IP_); + + /* Release the exclusive_console once it is used */ + if (unlikely(exclusive_console)) + exclusive_console = NULL; + + raw_spin_unlock(&logbuf_lock); + + up(&console_sem); + + /* + * Someone could have filled up the buffer again, so re-check if there's + * something to flush. In case we cannot trylock the console_sem again, + * 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); + + if (retry && console_trylock()) + goto again; + + if (wake_klogd) + wake_up_klogd(); +} +EXPORT_SYMBOL(console_unlock); + +/** + * console_conditional_schedule - yield the CPU if required + * + * If the console code is currently allowed to sleep, and + * if this CPU should yield the CPU to another task, do + * so here. + * + * Must be called within console_lock();. + */ +void __sched console_conditional_schedule(void) +{ + if (console_may_schedule) + cond_resched(); +} +EXPORT_SYMBOL(console_conditional_schedule); + +void console_unblank(void) +{ + struct console *c; + + /* + * console_unblank can no longer be called in interrupt context unless + * oops_in_progress is set to 1.. + */ + if (oops_in_progress) { + if (down_trylock(&console_sem) != 0) + return; + } else + console_lock(); + + console_locked = 1; + console_may_schedule = 0; + for_each_console(c) + if ((c->flags & CON_ENABLED) && c->unblank) + c->unblank(); + console_unlock(); +} + +/* + * Return the console tty driver structure and its associated index + */ +struct tty_driver *console_device(int *index) +{ + struct console *c; + struct tty_driver *driver = NULL; + + console_lock(); + for_each_console(c) { + if (!c->device) + continue; + driver = c->device(c, index); + if (driver) + break; + } + console_unlock(); + return driver; +} + +/* + * Prevent further output on the passed console device so that (for example) + * serial drivers can disable console output before suspending a port, and can + * re-enable output afterwards. + */ +void console_stop(struct console *console) +{ + console_lock(); + console->flags &= ~CON_ENABLED; + console_unlock(); +} +EXPORT_SYMBOL(console_stop); + +void console_start(struct console *console) +{ + console_lock(); + console->flags |= CON_ENABLED; + console_unlock(); +} +EXPORT_SYMBOL(console_start); + +static int __read_mostly keep_bootcon; + +static int __init keep_bootcon_setup(char *str) +{ + keep_bootcon = 1; + printk(KERN_INFO "debug: skip boot console de-registration.\n"); + + return 0; +} + +early_param("keep_bootcon", keep_bootcon_setup); + +/* + * The console driver calls this routine during kernel initialization + * to register the console printing procedure with printk() and to + * print any messages that were printed by the kernel before the + * console driver was initialized. + * + * This can happen pretty early during the boot process (because of + * early_printk) - sometimes before setup_arch() completes - be careful + * of what kernel features are used - they may not be initialised yet. + * + * There are two types of consoles - bootconsoles (early_printk) and + * "real" consoles (everything which is not a bootconsole) which are + * handled differently. + * - Any number of bootconsoles can be registered at any time. + * - As soon as a "real" console is registered, all bootconsoles + * will be unregistered automatically. + * - Once a "real" console is registered, any attempt to register a + * bootconsoles will be rejected + */ +void register_console(struct console *newcon) +{ + int i; + unsigned long flags; + struct console *bcon = NULL; + + /* + * before we register a new CON_BOOT console, make sure we don't + * already have a valid console + */ + if (console_drivers && newcon->flags & CON_BOOT) { + /* find the last or real console */ + for_each_console(bcon) { + if (!(bcon->flags & CON_BOOT)) { + printk(KERN_INFO "Too late to register bootconsole %s%d\n", + newcon->name, newcon->index); + return; + } + } + } + + if (console_drivers && console_drivers->flags & CON_BOOT) + bcon = console_drivers; + + if (preferred_console < 0 || bcon || !console_drivers) + preferred_console = selected_console; + + if (newcon->early_setup) + newcon->early_setup(); + + /* + * See if we want to use this console driver. If we + * didn't select a console we take the first one + * that registers here. + */ + if (preferred_console < 0) { + if (newcon->index < 0) + newcon->index = 0; + if (newcon->setup == NULL || + newcon->setup(newcon, NULL) == 0) { + newcon->flags |= CON_ENABLED; + if (newcon->device) { + newcon->flags |= CON_CONSDEV; + preferred_console = 0; + } + } + } + + /* + * See if this console matches one we selected on + * the command line. + */ + for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; + i++) { + if (strcmp(console_cmdline[i].name, newcon->name) != 0) + continue; + if (newcon->index >= 0 && + newcon->index != console_cmdline[i].index) + continue; + if (newcon->index < 0) + newcon->index = console_cmdline[i].index; +#ifdef CONFIG_A11Y_BRAILLE_CONSOLE + if (console_cmdline[i].brl_options) { + newcon->flags |= CON_BRL; + braille_register_console(newcon, + console_cmdline[i].index, + console_cmdline[i].options, + console_cmdline[i].brl_options); + return; + } +#endif + if (newcon->setup && + newcon->setup(newcon, console_cmdline[i].options) != 0) + break; + newcon->flags |= CON_ENABLED; + newcon->index = console_cmdline[i].index; + if (i == selected_console) { + newcon->flags |= CON_CONSDEV; + preferred_console = selected_console; + } + break; + } + + if (!(newcon->flags & CON_ENABLED)) + return; + + /* + * If we have a bootconsole, and are switching to a real console, + * don't print everything out again, since when the boot console, and + * the real console are the same physical device, it's annoying to + * see the beginning boot messages twice + */ + if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) + newcon->flags &= ~CON_PRINTBUFFER; + + /* + * Put this console in the list - keep the + * preferred driver at the head of the list. + */ + console_lock(); + if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { + newcon->next = console_drivers; + console_drivers = newcon; + if (newcon->next) + newcon->next->flags &= ~CON_CONSDEV; + } else { + newcon->next = console_drivers->next; + console_drivers->next = newcon; + } + if (newcon->flags & CON_PRINTBUFFER) { + /* + * console_unlock(); will print out the buffered messages + * for us. + */ + raw_spin_lock_irqsave(&logbuf_lock, flags); + console_seq = syslog_seq; + console_idx = syslog_idx; + console_prev = syslog_prev; + raw_spin_unlock_irqrestore(&logbuf_lock, flags); + /* + * We're about to replay the log buffer. Only do this to the + * just-registered console to avoid excessive message spam to + * the already-registered consoles. + */ + exclusive_console = newcon; + } + console_unlock(); + console_sysfs_notify(); + + /* + * By unregistering the bootconsoles after we enable the real console + * we get the "console xxx enabled" message on all the consoles - + * boot consoles, real consoles, etc - this is to ensure that end + * users know there might be something in the kernel's log buffer that + * went to the bootconsole (that they do not see on the real console) + */ + if (bcon && + ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && + !keep_bootcon) { + /* we need to iterate through twice, to make sure we print + * everything out, before we unregister the console(s) + */ + printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n", + newcon->name, newcon->index); + for_each_console(bcon) + if (bcon->flags & CON_BOOT) + unregister_console(bcon); + } else { + printk(KERN_INFO "%sconsole [%s%d] enabled\n", + (newcon->flags & CON_BOOT) ? "boot" : "" , + newcon->name, newcon->index); + } +} +EXPORT_SYMBOL(register_console); + +int unregister_console(struct console *console) +{ + struct console *a, *b; + int res = 1; + +#ifdef CONFIG_A11Y_BRAILLE_CONSOLE + if (console->flags & CON_BRL) + return braille_unregister_console(console); +#endif + + console_lock(); + if (console_drivers == console) { + console_drivers=console->next; + res = 0; + } else if (console_drivers) { + for (a=console_drivers->next, b=console_drivers ; + a; b=a, a=b->next) { + if (a == console) { + b->next = a->next; + res = 0; + break; + } + } + } + + /* + * If this isn't the last console and it has CON_CONSDEV set, we + * need to set it on the next preferred console. + */ + if (console_drivers != NULL && console->flags & CON_CONSDEV) + console_drivers->flags |= CON_CONSDEV; + + console_unlock(); + console_sysfs_notify(); + return res; +} +EXPORT_SYMBOL(unregister_console); + +static int __init printk_late_init(void) +{ + struct console *con; + + for_each_console(con) { + if (!keep_bootcon && con->flags & CON_BOOT) { + printk(KERN_INFO "turn off boot console %s%d\n", + con->name, con->index); + unregister_console(con); + } + } + hotcpu_notifier(console_cpu_notify, 0); + return 0; +} +late_initcall(printk_late_init); + +#if defined CONFIG_PRINTK +/* + * Delayed printk version, for scheduler-internal messages: + */ +#define PRINTK_BUF_SIZE 512 + +#define PRINTK_PENDING_WAKEUP 0x01 +#define PRINTK_PENDING_SCHED 0x02 + +static DEFINE_PER_CPU(int, printk_pending); +static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf); + +static void wake_up_klogd_work_func(struct irq_work *irq_work) +{ + int pending = __this_cpu_xchg(printk_pending, 0); + + if (pending & PRINTK_PENDING_SCHED) { + char *buf = __get_cpu_var(printk_sched_buf); + printk(KERN_WARNING "[sched_delayed] %s", buf); + } + + if (pending & PRINTK_PENDING_WAKEUP) + wake_up_interruptible(&log_wait); +} + +static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { + .func = wake_up_klogd_work_func, + .flags = IRQ_WORK_LAZY, +}; + +void wake_up_klogd(void) +{ + preempt_disable(); + if (waitqueue_active(&log_wait)) { + this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); + irq_work_queue(&__get_cpu_var(wake_up_klogd_work)); + } + preempt_enable(); +} + +int printk_deferred(const char *fmt, ...) +{ + unsigned long flags; + va_list args; + char *buf; + int r; + + local_irq_save(flags); + buf = __get_cpu_var(printk_sched_buf); + + va_start(args, fmt); + r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args); + va_end(args); + + __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED); + irq_work_queue(&__get_cpu_var(wake_up_klogd_work)); + local_irq_restore(flags); + + return r; +} + +/* + * printk rate limiting, lifted from the networking subsystem. + * + * This enforces a rate limit: not more than 10 kernel messages + * every 5s to make a denial-of-service attack impossible. + */ +DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); + +int __printk_ratelimit(const char *func) +{ + return ___ratelimit(&printk_ratelimit_state, func); +} +EXPORT_SYMBOL(__printk_ratelimit); + +/** + * printk_timed_ratelimit - caller-controlled printk ratelimiting + * @caller_jiffies: pointer to caller's state + * @interval_msecs: minimum interval between prints + * + * printk_timed_ratelimit() returns true if more than @interval_msecs + * milliseconds have elapsed since the last time printk_timed_ratelimit() + * returned true. + */ +bool printk_timed_ratelimit(unsigned long *caller_jiffies, + unsigned int interval_msecs) +{ + if (*caller_jiffies == 0 + || !time_in_range(jiffies, *caller_jiffies, + *caller_jiffies + + msecs_to_jiffies(interval_msecs))) { + *caller_jiffies = jiffies; + return true; + } + return false; +} +EXPORT_SYMBOL(printk_timed_ratelimit); + +static DEFINE_SPINLOCK(dump_list_lock); +static LIST_HEAD(dump_list); + +/** + * kmsg_dump_register - register a kernel log dumper. + * @dumper: pointer to the kmsg_dumper structure + * + * Adds a kernel log dumper to the system. The dump callback in the + * structure will be called when the kernel oopses or panics and must be + * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. + */ +int kmsg_dump_register(struct kmsg_dumper *dumper) +{ + unsigned long flags; + int err = -EBUSY; + + /* The dump callback needs to be set */ + if (!dumper->dump) + return -EINVAL; + + spin_lock_irqsave(&dump_list_lock, flags); + /* Don't allow registering multiple times */ + if (!dumper->registered) { + dumper->registered = 1; + list_add_tail_rcu(&dumper->list, &dump_list); + err = 0; + } + spin_unlock_irqrestore(&dump_list_lock, flags); + + return err; +} +EXPORT_SYMBOL_GPL(kmsg_dump_register); + +/** + * kmsg_dump_unregister - unregister a kmsg dumper. + * @dumper: pointer to the kmsg_dumper structure + * + * Removes a dump device from the system. Returns zero on success and + * %-EINVAL otherwise. + */ +int kmsg_dump_unregister(struct kmsg_dumper *dumper) +{ + unsigned long flags; + int err = -EINVAL; + + spin_lock_irqsave(&dump_list_lock, flags); + if (dumper->registered) { + dumper->registered = 0; + list_del_rcu(&dumper->list); + err = 0; + } + spin_unlock_irqrestore(&dump_list_lock, flags); + synchronize_rcu(); + + return err; +} +EXPORT_SYMBOL_GPL(kmsg_dump_unregister); + +static bool always_kmsg_dump; +module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); + +/** + * kmsg_dump - dump kernel log to kernel message dumpers. + * @reason: the reason (oops, panic etc) for dumping + * + * Call each of the registered dumper's dump() callback, which can + * retrieve the kmsg records with kmsg_dump_get_line() or + * kmsg_dump_get_buffer(). + */ +void kmsg_dump(enum kmsg_dump_reason reason) +{ + struct kmsg_dumper *dumper; + unsigned long flags; + + if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) + return; + + rcu_read_lock(); + list_for_each_entry_rcu(dumper, &dump_list, list) { + if (dumper->max_reason && reason > dumper->max_reason) + continue; + + /* initialize iterator with data about the stored records */ + dumper->active = true; + + raw_spin_lock_irqsave(&logbuf_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); + + /* invoke dumper which will iterate over records */ + dumper->dump(dumper, reason); + + /* reset iterator */ + dumper->active = false; + } + rcu_read_unlock(); +} + +/** + * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version) + * @dumper: registered kmsg dumper + * @syslog: include the "<4>" prefixes + * @line: buffer to copy the line to + * @size: maximum size of the buffer + * @len: length of line placed into buffer + * + * Start at the beginning of the kmsg buffer, with the oldest kmsg + * record, and copy one record into the provided buffer. + * + * Consecutive calls will return the next available record moving + * towards the end of the buffer with the youngest messages. + * + * A return value of FALSE indicates that there are no more records to + * read. + * + * The function is similar to kmsg_dump_get_line(), but grabs no locks. + */ +bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog, + char *line, size_t size, size_t *len) +{ + struct log *msg; + size_t l = 0; + bool ret = false; + + if (!dumper->active) + goto out; + + if (dumper->cur_seq < log_first_seq) { + /* messages are gone, move to first available one */ + dumper->cur_seq = log_first_seq; + dumper->cur_idx = log_first_idx; + } + + /* last entry */ + if (dumper->cur_seq >= log_next_seq) + goto out; + + msg = log_from_idx(dumper->cur_idx); + l = msg_print_text(msg, 0, syslog, line, size); + + dumper->cur_idx = log_next(dumper->cur_idx); + dumper->cur_seq++; + ret = true; +out: + if (len) + *len = l; + return ret; +} + +/** + * kmsg_dump_get_line - retrieve one kmsg log line + * @dumper: registered kmsg dumper + * @syslog: include the "<4>" prefixes + * @line: buffer to copy the line to + * @size: maximum size of the buffer + * @len: length of line placed into buffer + * + * Start at the beginning of the kmsg buffer, with the oldest kmsg + * record, and copy one record into the provided buffer. + * + * Consecutive calls will return the next available record moving + * towards the end of the buffer with the youngest messages. + * + * A return value of FALSE indicates that there are no more records to + * read. + */ +bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, + char *line, size_t size, size_t *len) +{ + unsigned long flags; + bool ret; + + raw_spin_lock_irqsave(&logbuf_lock, flags); + ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); + raw_spin_unlock_irqrestore(&logbuf_lock, flags); + + return ret; +} +EXPORT_SYMBOL_GPL(kmsg_dump_get_line); + +/** + * kmsg_dump_get_buffer - copy kmsg log lines + * @dumper: registered kmsg dumper + * @syslog: include the "<4>" prefixes + * @buf: buffer to copy the line to + * @size: maximum size of the buffer + * @len: length of line placed into buffer + * + * Start at the end of the kmsg buffer and fill the provided buffer + * with as many of the the *youngest* kmsg records that fit into it. + * If the buffer is large enough, all available kmsg records will be + * copied with a single call. + * + * Consecutive calls will fill the buffer with the next block of + * available older records, not including the earlier retrieved ones. + * + * A return value of FALSE indicates that there are no more records to + * read. + */ +bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, + char *buf, size_t size, size_t *len) +{ + unsigned long flags; + u64 seq; + u32 idx; + u64 next_seq; + u32 next_idx; + enum log_flags prev; + size_t l = 0; + bool ret = false; + + if (!dumper->active) + goto out; + + raw_spin_lock_irqsave(&logbuf_lock, flags); + if (dumper->cur_seq < log_first_seq) { + /* messages are gone, move to first available one */ + dumper->cur_seq = log_first_seq; + dumper->cur_idx = log_first_idx; + } + + /* last entry */ + if (dumper->cur_seq >= dumper->next_seq) { + raw_spin_unlock_irqrestore(&logbuf_lock, flags); + goto out; + } + + /* calculate length of entire buffer */ + seq = dumper->cur_seq; + idx = dumper->cur_idx; + prev = 0; + while (seq < dumper->next_seq) { + struct log *msg = log_from_idx(idx); + + l += msg_print_text(msg, prev, true, NULL, 0); + idx = log_next(idx); + seq++; + prev = msg->flags; + } + + /* move first record forward until length fits into the buffer */ + seq = dumper->cur_seq; + idx = dumper->cur_idx; + prev = 0; + while (l > size && seq < dumper->next_seq) { + struct log *msg = log_from_idx(idx); + + l -= msg_print_text(msg, prev, true, NULL, 0); + idx = log_next(idx); + seq++; + prev = msg->flags; + } + + /* last message in next interation */ + next_seq = seq; + next_idx = idx; + + l = 0; + prev = 0; + while (seq < dumper->next_seq) { + struct log *msg = log_from_idx(idx); + + l += msg_print_text(msg, prev, syslog, buf + l, size - l); + idx = log_next(idx); + seq++; + prev = msg->flags; + } + + dumper->next_seq = next_seq; + dumper->next_idx = next_idx; + ret = true; + raw_spin_unlock_irqrestore(&logbuf_lock, flags); +out: + if (len) + *len = l; + return ret; +} +EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer); + +/** + * kmsg_dump_rewind_nolock - reset the interator (unlocked version) + * @dumper: registered kmsg dumper + * + * Reset the dumper's iterator so that kmsg_dump_get_line() and + * kmsg_dump_get_buffer() can be called again and used multiple + * times within the same dumper.dump() callback. + * + * The function is similar to kmsg_dump_rewind(), but grabs no locks. + */ +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; +} + +/** + * kmsg_dump_rewind - reset the interator + * @dumper: registered kmsg dumper + * + * Reset the dumper's iterator so that kmsg_dump_get_line() and + * kmsg_dump_get_buffer() can be called again and used multiple + * times within the same dumper.dump() callback. + */ +void kmsg_dump_rewind(struct kmsg_dumper *dumper) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&logbuf_lock, flags); + kmsg_dump_rewind_nolock(dumper); + raw_spin_unlock_irqrestore(&logbuf_lock, flags); +} +EXPORT_SYMBOL_GPL(kmsg_dump_rewind); + +static char dump_stack_arch_desc_str[128]; + +/** + * dump_stack_set_arch_desc - set arch-specific str to show with task dumps + * @fmt: printf-style format string + * @...: arguments for the format string + * + * The configured string will be printed right after utsname during task + * dumps. Usually used to add arch-specific system identifiers. If an + * arch wants to make use of such an ID string, it should initialize this + * as soon as possible during boot. + */ +void __init dump_stack_set_arch_desc(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str), + fmt, args); + va_end(args); +} + +/** + * dump_stack_print_info - print generic debug info for dump_stack() + * @log_lvl: log level + * + * Arch-specific dump_stack() implementations can use this function to + * print out the same debug information as the generic dump_stack(). + */ +void dump_stack_print_info(const char *log_lvl) +{ + printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n", + log_lvl, raw_smp_processor_id(), current->pid, current->comm, + print_tainted(), init_utsname()->release, + (int)strcspn(init_utsname()->version, " "), + init_utsname()->version); + + if (dump_stack_arch_desc_str[0] != '\0') + printk("%sHardware name: %s\n", + log_lvl, dump_stack_arch_desc_str); + + print_worker_info(log_lvl, current); +} + +/** + * show_regs_print_info - print generic debug info for show_regs() + * @log_lvl: log level + * + * show_regs() implementations can use this function to print out generic + * debug information. + */ +void show_regs_print_info(const char *log_lvl) +{ + dump_stack_print_info(log_lvl); + + printk("%stask: %p ti: %p task.ti: %p\n", + log_lvl, current, current_thread_info(), + task_thread_info(current)); +} + +#endif -- 2.7.4 From 0048f176788f3c5bd3348244f6e2a1a3ad9b733b Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Thu, 14 May 2015 11:36:36 -0400 Subject: [PATCH 06/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 07/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 08/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 09/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 10/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 11/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 12/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 13/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 14/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 15/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 16/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