Skip to content

Commit 0bd359e

Browse files
jmeurinmiquelraynal
authored andcommitted
mtd: mtdoops: Create a header structure for the saved mtdoops.
Create a dump header to enable the addition of fields without having to modify the rest of the code. Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Jean-Marc Eurin <jmeurin@google.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/linux-mtd/20220421234244.2172003-3-jmeurin@google.com
1 parent fbb83e5 commit 0bd359e

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

drivers/mtd/mtdoops.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
/* Maximum MTD partition size */
2323
#define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
2424

25-
#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
26-
#define MTDOOPS_HEADER_SIZE 8
27-
2825
static unsigned long record_size = 4096;
2926
module_param(record_size, ulong, 0400);
3027
MODULE_PARM_DESC(record_size,
@@ -40,6 +37,13 @@ module_param(dump_oops, int, 0600);
4037
MODULE_PARM_DESC(dump_oops,
4138
"set to 1 to dump oopses, 0 to only dump panics (default 1)");
4239

40+
#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
41+
42+
struct mtdoops_hdr {
43+
u32 seq;
44+
u32 magic;
45+
} __packed;
46+
4347
static struct mtdoops_context {
4448
struct kmsg_dumper dump;
4549

@@ -178,16 +182,16 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic)
178182
{
179183
struct mtd_info *mtd = cxt->mtd;
180184
size_t retlen;
181-
u32 *hdr;
185+
struct mtdoops_hdr *hdr;
182186
int ret;
183187

184188
if (test_and_set_bit(0, &cxt->oops_buf_busy))
185189
return;
186190

187191
/* Add mtdoops header to the buffer */
188-
hdr = cxt->oops_buf;
189-
hdr[0] = cxt->nextcount;
190-
hdr[1] = MTDOOPS_KERNMSG_MAGIC;
192+
hdr = (struct mtdoops_hdr *)cxt->oops_buf;
193+
hdr->seq = cxt->nextcount;
194+
hdr->magic = MTDOOPS_KERNMSG_MAGIC;
191195

192196
if (panic) {
193197
ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
@@ -222,41 +226,41 @@ static void mtdoops_workfunc_write(struct work_struct *work)
222226
static void find_next_position(struct mtdoops_context *cxt)
223227
{
224228
struct mtd_info *mtd = cxt->mtd;
229+
struct mtdoops_hdr hdr;
225230
int ret, page, maxpos = 0;
226-
u32 count[MTDOOPS_HEADER_SIZE/sizeof(u32)], maxcount = 0xffffffff;
231+
u32 maxcount = 0xffffffff;
227232
size_t retlen;
228233

229234
for (page = 0; page < cxt->oops_pages; page++) {
230235
if (mtd_block_isbad(mtd, page * record_size))
231236
continue;
232237
/* Assume the page is used */
233238
mark_page_used(cxt, page);
234-
ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
235-
&retlen, (u_char *)&count[0]);
236-
if (retlen != MTDOOPS_HEADER_SIZE ||
239+
ret = mtd_read(mtd, page * record_size, sizeof(hdr),
240+
&retlen, (u_char *)&hdr);
241+
if (retlen != sizeof(hdr) ||
237242
(ret < 0 && !mtd_is_bitflip(ret))) {
238-
printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
239-
page * record_size, retlen,
240-
MTDOOPS_HEADER_SIZE, ret);
243+
printk(KERN_ERR "mtdoops: read failure at %ld (%zu of %zu read), err %d\n",
244+
page * record_size, retlen, sizeof(hdr), ret);
241245
continue;
242246
}
243247

244-
if (count[0] == 0xffffffff && count[1] == 0xffffffff)
248+
if (hdr.seq == 0xffffffff && hdr.magic == 0xffffffff)
245249
mark_page_unused(cxt, page);
246-
if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
250+
if (hdr.seq == 0xffffffff || hdr.magic != MTDOOPS_KERNMSG_MAGIC)
247251
continue;
248252
if (maxcount == 0xffffffff) {
249-
maxcount = count[0];
253+
maxcount = hdr.seq;
250254
maxpos = page;
251-
} else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
252-
maxcount = count[0];
255+
} else if (hdr.seq < 0x40000000 && maxcount > 0xc0000000) {
256+
maxcount = hdr.seq;
253257
maxpos = page;
254-
} else if (count[0] > maxcount && count[0] < 0xc0000000) {
255-
maxcount = count[0];
258+
} else if (hdr.seq > maxcount && hdr.seq < 0xc0000000) {
259+
maxcount = hdr.seq;
256260
maxpos = page;
257-
} else if (count[0] > maxcount && count[0] > 0xc0000000
261+
} else if (hdr.seq > maxcount && hdr.seq > 0xc0000000
258262
&& maxcount > 0x80000000) {
259-
maxcount = count[0];
263+
maxcount = hdr.seq;
260264
maxpos = page;
261265
}
262266
}
@@ -287,8 +291,9 @@ static void mtdoops_do_dump(struct kmsg_dumper *dumper,
287291

288292
if (test_and_set_bit(0, &cxt->oops_buf_busy))
289293
return;
290-
kmsg_dump_get_buffer(&iter, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
291-
record_size - MTDOOPS_HEADER_SIZE, NULL);
294+
kmsg_dump_get_buffer(&iter, true,
295+
cxt->oops_buf + sizeof(struct mtdoops_hdr),
296+
record_size - sizeof(struct mtdoops_hdr), NULL);
292297
clear_bit(0, &cxt->oops_buf_busy);
293298

294299
if (reason != KMSG_DUMP_OOPS) {

0 commit comments

Comments
 (0)