[PATCH v3 3/3] mtd: mtdoops: panic caused mtdoops to call mtdoops_erase function immediately

Ray Zhang
2022-10-07T21:50:54Z

The panic function disables the local interrupts, preemption, and all
other processors. When the invoked mtdoops needs to erase a used page,
calling schedule_work() to do it will not work. Instead, just call
mtdoops_erase function immediately.

Tested:
~# echo c > /proc/sysrq-trigger
[ 171.654759] sysrq: Trigger a crash
[ 171.658325] Kernel panic - not syncing: sysrq triggered crash
......
[ 172.406423] mtdoops: not ready 34, 35 (erase immediately)
[ 172.432285] mtdoops: ready 34, 35
[ 172.435633] Rebooting in 10 seconds..

Signed-off-by: Ray Zhang <sgzhang@google.com>
---
drivers/mtd/mtdoops.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/drivers/mtd/mtdoops.c b/drivers/mtd/mtdoops.c
index eca48dff9b53..eccb7dea892f 100644
--- a/drivers/mtd/mtdoops.c
+++ b/drivers/mtd/mtdoops.c
@@ -168,7 +168,7 @@ static void mtdoops_workfunc_erase(struct work_struct *work)
mtdoops_erase(cxt);
}

-static void mtdoops_inc_counter(struct mtdoops_context *cxt)
+static void mtdoops_inc_counter(struct mtdoops_context *cxt, int panic)
{
cxt->nextpage++;
if (cxt->nextpage >= cxt->oops_pages)
@@ -178,12 +178,20 @@ static void mtdoops_inc_counter(struct mtdoops_context *cxt)
cxt->nextcount = 0;

if (page_is_used(cxt, cxt->nextpage)) {
- schedule_work(&cxt->work_erase);
- return;
+ pr_debug("mtdoops: not ready %d, %d (erase %s)\n",
+ cxt->nextpage, cxt->nextcount,
+ panic ? "immediately" : "scheduled");
+ if (panic) {
+ /* In case of panic, erase immediately */
+ mtdoops_erase(cxt);
+ } else {
+ /* Otherwise, schedule work to erase it "nicely" */
+ schedule_work(&cxt->work_erase);
+ }
+ } else {
+ pr_debug("mtdoops: ready %d, %d (no erase)\n",
+ cxt->nextpage, cxt->nextcount);
}
-
- pr_debug("mtdoops: ready %d, %d (no erase)\n",
- cxt->nextpage, cxt->nextcount);
}

static void mtdoops_write(struct mtdoops_context *cxt, int panic)
@@ -219,7 +227,7 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic)
mark_page_used(cxt, cxt->nextpage);
memset(cxt->oops_buf, 0xff, record_size);

- mtdoops_inc_counter(cxt);
+ mtdoops_inc_counter(cxt, panic);
out:
clear_bit(0, &cxt->oops_buf_busy);
}
@@ -284,7 +292,7 @@ static void find_next_position(struct mtdoops_context *cxt)
cxt->nextcount = maxcount;
}

- mtdoops_inc_counter(cxt);
+ mtdoops_inc_counter(cxt, 0);
}

static void mtdoops_do_dump(struct kmsg_dumper *dumper,