2011-11-14 17:52:05 +00:00
|
|
|
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "brick_say.h"
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// messaging
|
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
#include <linux/fs.h>
|
2012-01-09 16:12:06 +00:00
|
|
|
#include <linux/sched.h>
|
2011-11-14 17:52:05 +00:00
|
|
|
#include <linux/preempt.h>
|
|
|
|
#include <linux/hardirq.h>
|
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
#ifndef GFP_BRICK
|
|
|
|
#define GFP_BRICK GFP_NOIO
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 16:12:06 +00:00
|
|
|
#define SAY_ORDER 0
|
|
|
|
#define SAY_BUFMAX (PAGE_SIZE << SAY_ORDER)
|
|
|
|
#define MAX_FILELEN 16
|
|
|
|
|
|
|
|
static char *say_buf[NR_CPUS] = {};
|
|
|
|
static int say_index[NR_CPUS] = {};
|
2011-11-14 17:52:05 +00:00
|
|
|
static int dump_max = 5;
|
2012-01-12 13:37:06 +00:00
|
|
|
static atomic_t overflow = ATOMIC_INIT(0);
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
static struct file *log_file = NULL;
|
|
|
|
|
2012-01-11 15:16:39 +00:00
|
|
|
static inline
|
|
|
|
void say_alloc(unsigned long cpu, bool use_atomic)
|
2011-11-14 17:52:05 +00:00
|
|
|
{
|
2012-01-30 16:29:59 +00:00
|
|
|
char *ptr;
|
|
|
|
if (likely(say_buf[cpu]) || unlikely(cpu >= NR_CPUS))
|
2012-01-09 16:12:06 +00:00
|
|
|
goto done;
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
ptr = (void*)__get_free_pages(use_atomic ? GFP_ATOMIC : GFP_BRICK, SAY_ORDER);
|
|
|
|
if (likely(ptr)) {
|
|
|
|
ptr[0] = '\0';
|
|
|
|
say_buf[cpu] = ptr;
|
2012-01-09 16:12:06 +00:00
|
|
|
say_index[cpu] = 0;
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
|
|
|
done: ;
|
|
|
|
}
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
static
|
2012-01-09 16:12:06 +00:00
|
|
|
void _say_mark(unsigned long cpu)
|
|
|
|
{
|
2012-01-30 16:29:59 +00:00
|
|
|
char *ptr;
|
|
|
|
bool use_atomic = (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK | NMI_MASK)) != 0;
|
2012-01-11 15:16:39 +00:00
|
|
|
|
|
|
|
say_alloc(cpu, use_atomic);
|
2012-01-30 16:29:59 +00:00
|
|
|
if (unlikely(use_atomic || cpu >= NR_CPUS))
|
2012-01-09 16:12:06 +00:00
|
|
|
goto done;
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
ptr = say_buf[cpu];
|
|
|
|
if (unlikely(!ptr) || !ptr[0])
|
2012-01-09 16:12:06 +00:00
|
|
|
goto done;
|
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
if (log_file) {
|
|
|
|
loff_t log_pos = 0;
|
|
|
|
int rest = say_index[cpu];
|
|
|
|
int len = 0;
|
|
|
|
while (rest > 0) {
|
2012-01-11 15:16:39 +00:00
|
|
|
int status;
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
2012-01-30 16:29:59 +00:00
|
|
|
status = vfs_write(log_file, ptr + len, rest, &log_pos);
|
2012-01-11 15:16:39 +00:00
|
|
|
set_fs(oldfs);
|
2012-01-30 16:29:59 +00:00
|
|
|
if (unlikely(status <= 0))
|
2012-01-10 12:55:50 +00:00
|
|
|
break;
|
|
|
|
len += status;
|
|
|
|
rest -= status;
|
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
#ifdef CONFIG_MARS_USE_SYSLOG
|
2012-01-10 12:55:50 +00:00
|
|
|
} else {
|
2012-01-30 16:29:59 +00:00
|
|
|
printk("%s", ptr);
|
2012-01-10 12:55:50 +00:00
|
|
|
#endif
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
ptr[0] = '\0';
|
2012-01-09 16:12:06 +00:00
|
|
|
say_index[cpu] = 0;
|
|
|
|
|
2012-01-11 15:16:39 +00:00
|
|
|
{
|
|
|
|
static long long old_jiffies = 0;
|
|
|
|
if (((long long)jiffies) - old_jiffies >= HZ * 5) {
|
|
|
|
static spinlock_t lock = SPIN_LOCK_UNLOCKED;
|
|
|
|
bool won_the_race = false;
|
|
|
|
|
|
|
|
spin_lock(&lock);
|
|
|
|
if (((long long)jiffies) - old_jiffies >= HZ * 5) {
|
|
|
|
old_jiffies = jiffies;
|
|
|
|
won_the_race = true;
|
|
|
|
}
|
|
|
|
spin_unlock(&lock);
|
|
|
|
if (won_the_race)
|
|
|
|
check_close(CONFIG_MARS_LOGFILE, false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 16:12:06 +00:00
|
|
|
done: ;
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
|
|
|
void say_mark(void)
|
|
|
|
{
|
|
|
|
unsigned long cpu = get_cpu();
|
|
|
|
_say_mark(cpu);
|
|
|
|
put_cpu();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(say_mark);
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-01-12 13:37:06 +00:00
|
|
|
static
|
|
|
|
void _say(unsigned long cpu, va_list args, bool use_args, const char *fmt, ...)
|
2011-11-14 17:52:05 +00:00
|
|
|
{
|
2012-01-12 10:07:37 +00:00
|
|
|
char *start;
|
|
|
|
int rest;
|
|
|
|
int written;
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-01-09 16:12:06 +00:00
|
|
|
if (!say_buf[cpu])
|
|
|
|
goto done;
|
|
|
|
|
2012-01-12 10:07:37 +00:00
|
|
|
rest = SAY_BUFMAX - say_index[cpu];
|
|
|
|
if (rest <= 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
start = say_buf[cpu] + say_index[cpu];
|
2012-01-12 13:37:06 +00:00
|
|
|
if (use_args) {
|
|
|
|
/* bug in gcc: use register variable
|
|
|
|
* shading the parameter
|
|
|
|
*/
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
written = vsnprintf(start, rest, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
} else {
|
|
|
|
written = vsnprintf(start, rest, fmt, args);
|
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-01-12 10:07:37 +00:00
|
|
|
if (likely(rest > written)) {
|
|
|
|
start[written] = '\0';
|
|
|
|
say_index[cpu] += written;
|
2012-01-12 13:37:06 +00:00
|
|
|
} else {
|
|
|
|
// indicate overflow
|
2012-01-30 16:29:59 +00:00
|
|
|
start[0] = '\0';
|
2012-01-12 13:37:06 +00:00
|
|
|
atomic_inc(&overflow);
|
|
|
|
}
|
|
|
|
done: ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void _check_overflow(unsigned long cpu)
|
|
|
|
{
|
2012-01-30 16:29:59 +00:00
|
|
|
int count = 0;
|
|
|
|
atomic_xchg(&overflow, count);
|
|
|
|
if (unlikely(count > 0)) {
|
|
|
|
if (likely(say_index[cpu] < SAY_BUFMAX - 8)) {
|
2012-01-12 13:37:06 +00:00
|
|
|
_say(cpu, NULL, true, "#%d#\n", count);
|
|
|
|
}
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-01-12 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void say(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
unsigned long cpu = get_cpu();
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
_say_mark(cpu);
|
2012-01-30 16:29:59 +00:00
|
|
|
if (unlikely(!say_buf[cpu]))
|
2012-01-12 13:37:06 +00:00
|
|
|
goto done;
|
|
|
|
_check_overflow(cpu);
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
_say(cpu, args, false, fmt);
|
|
|
|
va_end(args);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
|
|
|
_say_mark(cpu);
|
|
|
|
done:
|
|
|
|
put_cpu();
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(say);
|
|
|
|
|
2012-01-11 15:16:39 +00:00
|
|
|
void brick_say(bool dump, const char *prefix, const char *file, int line, const char *func, const char *fmt, ...)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
2012-01-10 12:55:50 +00:00
|
|
|
struct timespec now = CURRENT_TIME;
|
2012-01-09 16:12:06 +00:00
|
|
|
unsigned long cpu = get_cpu();
|
|
|
|
int filelen;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
_say_mark(cpu);
|
2012-01-30 16:29:59 +00:00
|
|
|
if (unlikely(!say_buf[cpu]))
|
2012-01-09 16:12:06 +00:00
|
|
|
goto done;
|
2012-01-12 13:37:06 +00:00
|
|
|
_check_overflow(cpu);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-01-12 10:07:37 +00:00
|
|
|
// limit the filename
|
2012-01-09 16:12:06 +00:00
|
|
|
filelen = strlen(file);
|
|
|
|
if (filelen > MAX_FILELEN)
|
|
|
|
file += filelen - MAX_FILELEN;
|
|
|
|
|
2012-01-12 13:37:06 +00:00
|
|
|
_say(cpu, NULL, true, "%ld.%09ld %s %s[%d] %s %d %s(): ", now.tv_sec, now.tv_nsec, prefix, current->comm, (int)cpu, file, line, func);
|
|
|
|
va_start(args, fmt);
|
|
|
|
_say(cpu, args, false, fmt);
|
|
|
|
va_end(args);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
|
|
|
_say_mark(cpu);
|
2012-01-11 15:16:39 +00:00
|
|
|
#ifdef CONFIG_DEBUG_KERNEL
|
|
|
|
if (dump)
|
|
|
|
brick_dump_stack();
|
|
|
|
#endif
|
2012-01-09 16:12:06 +00:00
|
|
|
done:
|
|
|
|
put_cpu();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say);
|
|
|
|
|
2012-01-11 15:16:39 +00:00
|
|
|
void check_open(const char *filename, bool must_exist)
|
2012-01-10 12:55:50 +00:00
|
|
|
{
|
2012-01-11 15:16:39 +00:00
|
|
|
int flags = O_EXCL | O_APPEND | O_WRONLY | O_LARGEFILE;
|
2012-01-10 12:55:50 +00:00
|
|
|
int prot = 0600;
|
|
|
|
mm_segment_t oldfs;
|
2012-01-11 15:16:39 +00:00
|
|
|
|
|
|
|
if (log_file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!must_exist)
|
|
|
|
flags |= O_CREAT;
|
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
2012-01-11 15:16:39 +00:00
|
|
|
log_file = filp_open(filename, flags, prot);
|
2012-01-10 12:55:50 +00:00
|
|
|
set_fs(oldfs);
|
2012-01-30 16:29:59 +00:00
|
|
|
if (unlikely(IS_ERR(log_file))) {
|
2012-01-11 15:16:39 +00:00
|
|
|
int status = PTR_ERR(log_file);
|
2012-01-10 12:55:50 +00:00
|
|
|
log_file = NULL;
|
2012-01-11 15:16:39 +00:00
|
|
|
say("cannot open logfile '%s', status = %d\n", filename, status);
|
|
|
|
} else {
|
|
|
|
say("opened logfile '%s' %p\n", filename, log_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_close(const char *filename, bool force, bool re_open)
|
|
|
|
{
|
|
|
|
struct kstat st = {};
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = vfs_stat((char*)filename, &st);
|
|
|
|
set_fs(oldfs);
|
|
|
|
force = (status < 0 || !st.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force) {
|
|
|
|
if (log_file) {
|
|
|
|
struct file *old;
|
|
|
|
say("closing logfile....\n");
|
|
|
|
old = log_file;
|
|
|
|
log_file = NULL;
|
|
|
|
// FIXME: this may race against vfs_write(). Use rcu here.
|
|
|
|
filp_close(old, NULL);
|
|
|
|
say("closed logfile.\n");
|
|
|
|
}
|
|
|
|
if (re_open)
|
|
|
|
check_open(filename, true);
|
2012-01-10 12:55:50 +00:00
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_say(void)
|
|
|
|
{
|
|
|
|
check_open(CONFIG_MARS_LOGFILE, true);
|
2012-01-10 12:55:50 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(init_say);
|
|
|
|
|
|
|
|
void exit_say(void)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NR_CPUS; i++) {
|
|
|
|
if (!say_buf[i])
|
|
|
|
continue;
|
|
|
|
__free_pages(virt_to_page((unsigned long)say_buf[i]), SAY_ORDER);
|
|
|
|
say_buf[i] = NULL;
|
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
check_close(CONFIG_MARS_LOGFILE, true, false);
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(exit_say);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_KERNEL
|
2011-11-14 17:52:05 +00:00
|
|
|
|
|
|
|
void brick_dump_stack(void)
|
|
|
|
{
|
|
|
|
if (dump_max > 0) {
|
|
|
|
dump_max--; // racy, but does no harm
|
|
|
|
dump_stack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(brick_dump_stack);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|