lkml.org 
[lkml]   [2000]   [Aug]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH] resizeable printk ring buffer

The printk ring buffer has doubled in size several times to accomodate
the amount of boot messages spewed out by kernels with lots of drivers.
This always seemed like a bit of a waste to me since after the boot
messages had been logged to disk, the original 4k buffer is more than
adequate during normal operation. To others, the idea of having a big
128k ring buffer might be appealing. Hence this patch.

The buffer used at bootstrap is marked initdata and an initcall is
used to bounce the messages into a kmalloc'd region before the
initdata is freed. With a minor extension to the dmesg program (and
kernel syscall) the admin can resise the ring buffer from 4k to 128k.
Messages in the buffer are preserved if the buffer hasn't wrapped
yet and if they will fit in the new size.

To resize without yet having the modified dmesg, you essentially do:

i = syscall(103 /*syslog*/ , 9, NULL, kbytes);

where kbytes is 4, 8, 16 ... 128.

I'm not advocating this for 2.4 as it isn't a bugfix, but thought
others might want to play with it between now and 2.5.x

Paul.

--- linux~/kernel/printk.c Fri Jul 7 03:47:57 2000
+++ linux/kernel/printk.c Tue Aug 15 05:17:24 2000
@@ -12,18 +12,20 @@
* Modified for sysctl support, 1/8/97, Chris Horn.
* Fixed SMP synchronization, 08/08/99, Manfred Spraul
* manfreds@colorfullife.com
+ * Dynamically resizeable printk ring buffer, 07/2000, Paul Gortmaker.
*/

#include <linux/mm.h>
#include <linux/tty_driver.h>
#include <linux/smp_lock.h>
#include <linux/console.h>
+#include <linux/slab.h>
#include <linux/init.h>

#include <asm/uaccess.h>

-#define LOG_BUF_LEN (16384)
-#define LOG_BUF_MASK (LOG_BUF_LEN-1)
+#define BOOTLOG_BUF_LEN (16384)
+#define LOG_BUF_MASK (log_buf_len-1)

static char buf[1024];

@@ -46,7 +48,9 @@
spinlock_t console_lock = SPIN_LOCK_UNLOCKED;

struct console *console_drivers = NULL;
-static char log_buf[LOG_BUF_LEN];
+static char bootlog_buf[BOOTLOG_BUF_LEN] __initdata = {0, };
+static unsigned int log_buf_len = BOOTLOG_BUF_LEN;
+static char *log_buf = bootlog_buf;
static unsigned long log_start;
static unsigned long logged_chars;
struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
@@ -119,13 +123,13 @@
* 6 -- Disable printk's to console
* 7 -- Enable printk's to console
* 8 -- Set level of messages printed to console
+ * 9 -- Set ring buffer size in kB (4k -> 128k, 2^n)
*/
int do_syslog(int type, char * buf, int len)
{
unsigned long i, j, limit, count;
- int do_clear = 0;
- char c;
- int error = -EPERM;
+ int error, do_clear = 0;
+ char c, *new_buf;

error = 0;
switch (type) {
@@ -175,8 +179,8 @@
if (error)
goto out;
count = len;
- if (count > LOG_BUF_LEN)
- count = LOG_BUF_LEN;
+ if (count > log_buf_len)
+ count = log_buf_len;
spin_lock_irq(&console_lock);
if (count > logged_chars)
count = logged_chars;
@@ -191,7 +195,7 @@
*/
for(i=0;i < count;i++) {
j = limit-1-i;
- if (j+LOG_BUF_LEN < log_start+log_size)
+ if (j+log_buf_len < log_start+log_size)
break;
c = log_buf[ j & LOG_BUF_MASK ];
spin_unlock_irq(&console_lock);
@@ -236,6 +240,30 @@
spin_unlock_irq(&console_lock);
error = 0;
break;
+ case 9:
+ error = -EINVAL;
+ i = 128;
+ while (i != len && i != 4) i>>=1;
+ if (i != len)
+ goto out;
+ error = -ENOMEM;
+ new_buf = kmalloc(len*1024, GFP_KERNEL);
+ if (new_buf == NULL)
+ goto out;
+ memset(new_buf, 0, len*1024);
+ spin_lock_irq(&console_lock);
+ limit = log_start + log_size;
+ /* Copy iff it hasn't wrapped & will fit. */
+ if (limit < log_buf_len && logged_chars <= len*1024)
+ memcpy(new_buf, log_buf, logged_chars);
+ else
+ logged_chars = log_size = log_start = 0;
+ kfree(log_buf);
+ log_buf = new_buf;
+ log_buf_len = len*1024;
+ spin_unlock_irq(&console_lock);
+ error = 0;
+ break;
default:
error = -EINVAL;
break;
@@ -285,7 +313,7 @@
line_feed = 0;
for (; p < buf_end; p++) {
log_buf[(log_start+log_size) & LOG_BUF_MASK] = *p;
- if (log_size < LOG_BUF_LEN)
+ if (log_size < log_buf_len)
log_size++;
else
log_start++;
@@ -495,3 +523,26 @@
tty->driver.write(tty, 0, msg, strlen(msg));
return;
}
+
+/*
+ * Kernel boot messages are 1st stored in __initdata, then copied to
+ * kmalloc so you can use do_syslog() above to resize buffer later.
+ */
+static int __init printk_ring_copy(void)
+{
+ char *new_buf;
+ unsigned long flags;
+
+ new_buf = kmalloc(BOOTLOG_BUF_LEN, GFP_KERNEL);
+ if (new_buf == NULL) {
+ printk(KERN_EMERG "No memory for new printk buffer!?!\n");
+ return -ENOMEM;
+ }
+ spin_lock_irqsave(&console_lock, flags);
+ memcpy(new_buf, log_buf, BOOTLOG_BUF_LEN);
+ log_buf = new_buf;
+ spin_unlock_irqrestore(&console_lock, flags);
+ return 0;
+}
+
+__initcall(printk_ring_copy);




_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 12:37    [W:0.024 / U:7.228 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site