Skip to contentSkip to conversation

oops and panic message logging to MTD

PATCH/RFC Richard Purdie 3 participants 9 messages Jun 18, 2007 – Jul 4, 2007 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

Kernel oops and panic messages are invaluable when debugging crashes. These messages often don't make it to flash based logging methods (say a syslog on jffs2) due to the overheads involved in writing to flash.

This patch allows you to turn an MTD partition into a circular log buffer where kernel oops and panic messages are written to. The messages are obtained by registering a console driver and checking oops_in_progress. Erases are performed in advance to maximise the chances of a saving messages.

To activate it, add console=ttyMTDx to the kernel commandline (where x is the mtd device number to use).

http://folks.o-hand.com/richard/oopslog.c is an example of extracting this information in userspace. It will work with an mtdblock device or a dump of an mtd partition.

---
 drivers/mtd/Kconfig   |    8 +
 drivers/mtd/Makefile  |    1 
 drivers/mtd/mtdoops.c |  365 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 374 insertions(+)
drivers/mtd/Kconfig +9 -0
Index: linux/drivers/mtd/Kconfig
===================================================================
@@ -278,6 +278,15 @@ config SSFDC
 	  This enables read only access to SmartMedia formatted NAND
 	  flash. You can mount it with FAT file system.
 
+config MTD_OOPS
+	tristate "Log panic/oops to an MTD buffer"
+	depends on MTD
+	help
+	  This enables panic and oops messages to be logged to a circular
+	  buffer in a flash partition where it can be read back at some
+	  later point. Add console=ttyMTDx to the commandline to activate
+	  (where x is the MTD partition number to use).
+
 source "drivers/mtd/chips/Kconfig"
 
 source "drivers/mtd/maps/Kconfig"
drivers/mtd/Makefile +1 -0
Index: linux/drivers/mtd/Makefile
===================================================================
@@ -23,6 +23,7 @@ obj-$(CONFIG_NFTL)		+= nftl.o
 obj-$(CONFIG_INFTL)		+= inftl.o
 obj-$(CONFIG_RFD_FTL)		+= rfd_ftl.o
 obj-$(CONFIG_SSFDC)		+= ssfdc.o
+obj-$(CONFIG_MTD_OOPS)		+= mtdoops.o
 
 nftl-objs		:= nftlcore.o nftlmount.o
 inftl-objs		:= inftlcore.o inftlmount.o
drivers/mtd/mtdoops.c new file +365 -0

365 lines not on this page · raw message

#1 lore raw

drivers/mtd/mtdoops.c line 66

Quoted 13 lines · Richard Purdie
drivers/mtd/mtdoops.c new file +13 -0
+static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
+{
+	struct erase_info erase;
+	DECLARE_WAITQUEUE(wait, current);
+	wait_queue_head_t wait_q;
+	int ret;
+
+	init_waitqueue_head(&wait_q);
+	erase.mtd = mtd;
+	erase.callback = mtdoops_erase_callback;
+	erase.addr = offset;
+	if (mtd->erasesize < OOPS_PAGE_SIZE)
+		erase.len = OOPS_PAGE_SIZE;

It seems to me that your code won't work if mtd->erasesize < OOPS_PAGE_SIZE anyway, so this check should not be here I guess.

drivers/mtd/mtdoops.c line 111

Quoted 8 lines
drivers/mtd/mtdoops.c new file +8 -0
+	ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
+			&retlen, (u_char *) &count);
+	if ((retlen != 4) || (ret < 0)) {
+		printk(KERN_ERR "mtdoops: Read failure at %d (%d of 4 read)"
+				", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
+				retlen, ret);
+		return 1;
+	}

mtd->read() returns -EUCLEAN in case of a correctable bit-flip, ignore this error code here and elsewhere as well please.

Quoted 8 lines
drivers/mtd/mtdoops.c new file +8 -0
+static void mtdoops_prepare(struct mtdoops_context *cxt)
+{
+	struct mtd_info *mtd = cxt->mtd;
+	int i = 0, j, ret, mod;
+
+	/* We were unregistered */
+	if (!mtd)
+		return;

drivers/mtd/mtdoops.c line 140

Quoted 10 lines
drivers/mtd/mtdoops.c new file +10 -0
+
+	mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
+	if (mod != 0) {
+		cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
+		if (cxt->nextpage > cxt->oops_pages)
+			cxt->nextpage = 0;
+	}
+
+	while (mtd->block_isbad &&
+			mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE)) {

Well, mtd->block_isbad() may return error, unlikely, bu still. You also ignore the error at other places.

Quoted 5 lines
drivers/mtd/mtdoops.c new file +5 -0
+badblock:
+		printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
+				cxt->nextpage * OOPS_PAGE_SIZE);
+		i++;
+		cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);

drivers/mtd/mtdoops.c line 155

Quoted 10 lines
drivers/mtd/mtdoops.c new file +10 -0
+		if (cxt->nextpage > cxt->oops_pages)
+			cxt->nextpage = 0;
+		if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
+			printk(KERN_ERR "mtdoops: All blocks bad!\n");
+			return;
+		}
+	}
+
+	for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
+		ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);

Ugh, why do you make it this difficult way instead of

for (all EBs) {
    ret = erase()
    if (ret == -EIO) {
        markbad();
    } else
        return err;
}

drivers/mtd/mtdoops.c line 160

Quoted 5 lines
drivers/mtd/mtdoops.c new file +5 -0
+
+	if (ret < 0) {
+		if (mtd->block_markbad)
+			mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
+		goto badblock;

Please, mark EB as bad only in case of -EIO. Also, do not ignore error code of mtd->block_markbad()

Is it possible to re-structure the code and erase/check if EB is bad in _one_ cycle (thus avoiding this goto which is difficult to understand)?

Surely all you want is to format the partition. So make a loop, skip bad EBs and erase good ones. In case of erase failure (-EIO) mark the EB as bad.

drivers/mtd/mtdoops.c line 184

Quoted 9 lines
drivers/mtd/mtdoops.c new file +9 -0
+static int find_next_position(struct mtdoops_context *cxt)
+{
+	struct mtd_info *mtd = cxt->mtd;
+	int page, maxpos = 0;
+	u32 count, maxcount = 0xffffffff;
+	size_t retlen;
+
+	for (page = 0; page < cxt->oops_pages; page++) {
+		mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);

Please, check return code here.

Signature · 2 lines
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#8 lore raw
In reply to Richard Purdie
Quoted 25 lines · Richard Purdie
drivers/mtd/mtdoops.c new file +25 -0
+static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
+{
+	struct erase_info erase;
+	DECLARE_WAITQUEUE(wait, current);
+	wait_queue_head_t wait_q;
+	int ret;
+
+	init_waitqueue_head(&wait_q);
+	erase.mtd = mtd;
+	erase.callback = mtdoops_erase_callback;
+	erase.addr = offset;
+	if (mtd->erasesize < OOPS_PAGE_SIZE)
+		erase.len = OOPS_PAGE_SIZE;
+	else
+		erase.len = mtd->erasesize;
+	erase.priv = (u_long)&wait_q;
+
+	set_current_state(TASK_INTERRUPTIBLE);
+	add_wait_queue(&wait_q, &wait);
+
+	ret = mtd->erase(mtd, &erase);
+	if (ret) {
+		set_current_state(TASK_RUNNING);
+		remove_wait_queue(&wait_q, &wait);
+		printk(KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "

drivers/mtd/mtdoops.c line 88

Quoted 10 lines
drivers/mtd/mtdoops.c new file +10 -0
+				     "on \"%s\" failed\n",
+			erase.addr, erase.len, mtd->name);
+		return ret;
+	}
+
+	schedule();  /* Wait for erase to finish. */
+	remove_wait_queue(&wait_q, &wait);
+
+	return 0;
+}

Also, could you please use wait_event_interruptible() instead of set_current_state() which looks better (indeed, you have wait queue, so use wq calls).

Signature · 2 lines
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#2 lore raw
In reply to Artem Bityutskiy
Quoted 6 lines · Artem Bityutskiy

drivers/mtd/mtdoops.c line 66

Quoted 2 lines · Richard Purdie
drivers/mtd/mtdoops.c new file +2 -0
+	if (mtd->erasesize < OOPS_PAGE_SIZE)
+		erase.len = OOPS_PAGE_SIZE;

It seems to me that your code won't work if mtd->erasesize < OOPS_PAGE_SIZE anyway, so this check should not be here I guess.

Its just a sanity check...

Quoted 11 lines

drivers/mtd/mtdoops.c line 111

Quoted 8 lines
drivers/mtd/mtdoops.c new file +8 -0
+	ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
+			&retlen, (u_char *) &count);
+	if ((retlen != 4) || (ret < 0)) {
+		printk(KERN_ERR "mtdoops: Read failure at %d (%d of 4 read)"
+				", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE,
+				retlen, ret);
+		return 1;
+	}

mtd->read() returns -EUCLEAN in case of a correctable bit-flip, ignore this error code here and elsewhere as well please.

ok.

Quoted 13 lines
Quoted 13 lines
drivers/mtd/mtdoops.c new file +13 -0
+static void mtdoops_prepare(struct mtdoops_context *cxt)
+{
+	struct mtd_info *mtd = cxt->mtd;
+	int i = 0, j, ret, mod;
+
+	/* We were unregistered */
+	if (!mtd)
+		return;
+
+	mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
+	if (mod != 0) {
+		cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
+		if (cxt->nextpage > cxt->oops_pages)
Quoted 8 lines

drivers/mtd/mtdoops.c line 140

Quoted 5 lines
drivers/mtd/mtdoops.c new file +5 -0
+			cxt->nextpage = 0;
+	}
+
+	while (mtd->block_isbad &&
+			mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE)) {

Well, mtd->block_isbad() may return error, unlikely, bu still. You also ignore the error at other places.

Ignoring that is deliberate since it doesn't really matter if the block is bad or we can't read from it, the action is the same...

Quoted 18 lines
Quoted 5 lines
drivers/mtd/mtdoops.c new file +5 -0
+badblock:
+		printk(KERN_WARNING "mtdoops: Bad block at %08x\n",
+				cxt->nextpage * OOPS_PAGE_SIZE);
+		i++;
+		cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);

drivers/mtd/mtdoops.c line 155

Quoted 10 lines
drivers/mtd/mtdoops.c new file +10 -0
+		if (cxt->nextpage > cxt->oops_pages)
+			cxt->nextpage = 0;
+		if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
+			printk(KERN_ERR "mtdoops: All blocks bad!\n");
+			return;
+		}
+	}
+
+	for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
+		ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);

Ugh, why do you make it this difficult way instead of

Quoted 7 lines
for (all EBs) {
    ret = erase()
    if (ret == -EIO) {
        markbad();
    } else
        return err;
}

See below.

Quoted 7 lines

drivers/mtd/mtdoops.c line 160

Quoted 5 lines
drivers/mtd/mtdoops.c new file +5 -0
+
+	if (ret < 0) {
+		if (mtd->block_markbad)
+			mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
+		goto badblock;

Please, mark EB as bad only in case of -EIO.

ok.

Quoted 1 line

Also, do not ignore error code of mtd->block_markbad()

All we can do is print a warning, the action will be the same...

Quoted 6 lines

Is it possible to re-structure the code and erase/check if EB is bad in _one_ cycle (thus avoiding this goto which is difficult to understand)?

Surely all you want is to format the partition. So make a loop, skip bad EBs and erase good ones. In case of erase failure (-EIO) mark the EB as bad.

Its not a case of formatting the whole partition. The whole point of this code is the following use case:

1. Device crashes
2. Device reboots
3. mtdoops partition has a log of why it crashed

If you erase the whole partition each time mtdoops loads, this won't work so the code only finds the next block to use and erases that. It keeps moving forwards until it finds that block.

I usually hate goto statements but in this case it simplifies the code a lot (and it is on an error path). The alternative is a while loop with several new variables, I tried it and it was more ugly/confusing...

Quoted 11 lines

drivers/mtd/mtdoops.c line 184

Quoted 9 lines
drivers/mtd/mtdoops.c new file +9 -0
+static int find_next_position(struct mtdoops_context *cxt)
+{
+	struct mtd_info *mtd = cxt->mtd;
+	int page, maxpos = 0;
+	u32 count, maxcount = 0xffffffff;
+	size_t retlen;
+
+	for (page = 0; page < cxt->oops_pages; page++) {
+		mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);

Please, check return code here.

ok.

Quoted 3 lines

Also, could you please use wait_event_interruptible() instead of set_current_state() which looks better (indeed, you have wait queue, so use wq calls).

That piece of code is in keeping with the rest of the mtd erase handling code. Looking through the various wq macros, I don't see any which help particularly in this case...

Cheers,

Richard

#3 lore raw
Quoted 9 lines · Richard Purdie
Quoted 6 lines · Artem Bityutskiy

drivers/mtd/mtdoops.c line 66

Quoted 2 lines · Richard Purdie
drivers/mtd/mtdoops.c new file +2 -0
+	if (mtd->erasesize < OOPS_PAGE_SIZE)
+		erase.len = OOPS_PAGE_SIZE;

It seems to me that your code won't work if mtd->erasesize < OOPS_PAGE_SIZE anyway, so this check should not be here I guess.

Its just a sanity check...

Then do this once in the "add" notifier.

Quoted 5 lines
Quoted 2 lines

Well, mtd->block_isbad() may return error, unlikely, bu still. You also ignore the error at other places.

Ignoring that is deliberate since it doesn't really matter if the block is bad or we can't read from it, the action is the same...

No, bad EB is a perfectly legal thing and you should deal with it. Error code means that something very bad and sever is going on and you have to just refuse working with this device.

Quoted 3 lines
Quoted 1 line

Also, do not ignore error code of mtd->block_markbad()

All we can do is print a warning, the action will be the same...

No, the action should be returning an error and refuse doing more work.

Quoted 7 lines
Quoted 3 lines

Also, could you please use wait_event_interruptible() instead of set_current_state() which looks better (indeed, you have wait queue, so use wq calls).

That piece of code is in keeping with the rest of the mtd erase handling code. Looking through the various wq macros, I don't see any which help particularly in this case...

Well, it is matter of taste so I do not insist. But I think wait_event_interruptible() is much nicer. Glance at drivers/ubi/io.c how it looks.

Signature · 2 lines
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#4 lore raw
Quoted 8 lines · Artem Bityutskiy
Quoted 5 lines · Richard Purdie
Quoted 2 lines

Well, mtd->block_isbad() may return error, unlikely, bu still. You also ignore the error at other places.

Ignoring that is deliberate since it doesn't really matter if the block is bad or we can't read from it, the action is the same...

No, bad EB is a perfectly legal thing and you should deal with it.

The code does.

Quoted 3 lines

Error
code means that something very bad and sever is going on and you have to just refuse working with this device.

In this case, it will just move on to the next EB. There is code to handle no available EBs at which point it will refuse to work with the device.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Also, do not ignore error code of mtd->block_markbad()

All we can do is print a warning, the action will be the same...

No, the action should be returning an error and refuse doing more work.

It will refuse to do more work if no usable EB is available but it will try others first. It can be assumed mtdoops will only be used with small partitions so trying to find a usable EB before giving a fatal error shouldn't have much of an impact on the system and might just let it keep working in some strange error cases (and since its an error logger, that is good).

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

Also, could you please use wait_event_interruptible() instead of set_current_state() which looks better (indeed, you have wait queue, so use wq calls).

That piece of code is in keeping with the rest of the mtd erase handling code. Looking through the various wq macros, I don't see any which help particularly in this case...

Well, it is matter of taste so I do not insist. But I think wait_event_interruptible() is much nicer. Glance at drivers/ubi/io.c how it looks.

I'll have a look.

Cheers,

Richard

#5 lore raw
Quoted 7 lines · Richard Purdie
Quoted 3 lines

Error
code means that something very bad and sever is going on and you have to just refuse working with this device.

In this case, it will just move on to the next EB. There is code to handle no available EBs at which point it will refuse to work with the device.

My point is that instead of moving you should return error. You cannot keep working with this device just because something really bad happened, you do not know what, and you cannot react accordingly because you do not know how.

Signature · 2 lines
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#6 lore raw
In reply to Richard Purdie
Quoted 6 lines · Richard Purdie

Its not a case of formatting the whole partition. The whole point of this code is the following use case:

1. Device crashes
2. Device reboots
3. mtdoops partition has a log of why it crashed

The oops logger uses oops_in_progress variable to detect the begin and the end of an oops. The end is detected when the first non-oops line comes and oops_in_progress is false.

This works if the kernel is still running after the oops and gemerates some non-oops messages. But if there is no non-oops line following an oops, no flushing will occur and there won't be a log on flash.

Jarkko Lavinen

#7 lore raw
Quoted 8 lines · Jarkko Lavinen
Quoted 6 lines · Richard Purdie

Its not a case of formatting the whole partition. The whole point of this code is the following use case:

1. Device crashes
2. Device reboots
3. mtdoops partition has a log of why it crashed

Quoted 7 lines

The oops logger uses oops_in_progress variable to detect the begin and the end of an oops. The end is detected when the first non-oops line comes and oops_in_progress is false.

This works if the kernel is still running after the oops and gemerates some non-oops messages. But if there is no non-oops line following an oops, no flushing will occur and there won't be a log on flash.

There was a printk within bust_spinlocks which flushed the klogd queues and hence flushed the mtd_oops queue too.

I've noticed this has recently been removed and replaced with a wait queue [1] and hence the problem mentioned above now exists (but didn't when the driver was developed).

[1] http://git.o-hand.com/?p=linux-rpurdie;a=commitdiff;h=e3e8a75d2acfc61ebf25524666a0a2c6abb0620c

This raises the question of how to known when the oops has completed. The neatest solution I can see would be to add some kind of optional sync function pointer to struct console. Would that be acceptable?

Richard