Skip to content

Commit 34327e9

Browse files
WeiXiong Liaokees
authored andcommitted
pstore/zone,blk: Add ftrace frontend support
Support backend for ftrace. To enable ftrace backend, just make ftrace_size be greater than 0 and a multiple of 4096. Signed-off-by: WeiXiong Liao <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Co-developed-by: Colin Ian King <[email protected]> Signed-off-by: Colin Ian King <[email protected]> Link: https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent cc9c4d1 commit 34327e9

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

fs/pstore/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,15 @@ config PSTORE_BLK_CONSOLE_SIZE
248248

249249
NOTE that, both Kconfig and module parameters can configure
250250
pstore/blk, but module parameters have priority over Kconfig.
251+
252+
config PSTORE_BLK_FTRACE_SIZE
253+
int "Size in Kbytes of ftrace log to store"
254+
depends on PSTORE_BLK
255+
depends on PSTORE_FTRACE
256+
default 64
257+
help
258+
This just sets size of ftrace log (ftrace_size) for pstore/blk. The
259+
size is in KB and must be a multiple of 4.
260+
261+
NOTE that, both Kconfig and module parameters can configure
262+
pstore/blk, but module parameters have priority over Kconfig.

fs/pstore/blk.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ static long console_size = -1;
4343
module_param(console_size, long, 0400);
4444
MODULE_PARM_DESC(console_size, "console size in kbytes");
4545

46+
#if IS_ENABLED(CONFIG_PSTORE_FTRACE)
47+
static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
48+
#else
49+
static long ftrace_size = -1;
50+
#endif
51+
module_param(ftrace_size, long, 0400);
52+
MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
53+
4654
/*
4755
* blkdev - the block device to use for pstore storage
4856
*
@@ -152,6 +160,7 @@ static int psblk_register_do(struct pstore_device_info *dev)
152160
verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
153161
verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
154162
verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
163+
verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
155164
#undef verify_size
156165

157166
pstore_zone_info->total_size = dev->total_size;

fs/pstore/zone.c

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ struct pstore_zone {
9292
* @kpszs: kmsg dump storage zones
9393
* @ppsz: pmsg storage zone
9494
* @cpsz: console storage zone
95+
* @fpszs: ftrace storage zones
9596
* @kmsg_max_cnt: max count of @kpszs
9697
* @kmsg_read_cnt: counter of total read kmsg dumps
9798
* @kmsg_write_cnt: counter of total kmsg dump writes
9899
* @pmsg_read_cnt: counter of total read pmsg zone
99100
* @console_read_cnt: counter of total read console zone
101+
* @ftrace_max_cnt: max count of @fpszs
102+
* @ftrace_read_cnt: counter of max read ftrace zone
100103
* @oops_counter: counter of oops dumps
101104
* @panic_counter: counter of panic dumps
102105
* @recovered: whether finished recovering data from storage
@@ -109,11 +112,14 @@ struct psz_context {
109112
struct pstore_zone **kpszs;
110113
struct pstore_zone *ppsz;
111114
struct pstore_zone *cpsz;
115+
struct pstore_zone **fpszs;
112116
unsigned int kmsg_max_cnt;
113117
unsigned int kmsg_read_cnt;
114118
unsigned int kmsg_write_cnt;
115119
unsigned int pmsg_read_cnt;
116120
unsigned int console_read_cnt;
121+
unsigned int ftrace_max_cnt;
122+
unsigned int ftrace_read_cnt;
117123
/*
118124
* These counters should be calculated during recovery.
119125
* It records the oops/panic times after crashes rather than boots.
@@ -314,6 +320,8 @@ static void psz_flush_all_dirty_zones(struct work_struct *work)
314320
ret |= psz_flush_dirty_zone(cxt->cpsz);
315321
if (cxt->kpszs)
316322
ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
323+
if (cxt->fpszs)
324+
ret |= psz_flush_dirty_zones(cxt->fpszs, cxt->ftrace_max_cnt);
317325
if (ret && cxt->pstore_zone_info)
318326
schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
319327
}
@@ -550,6 +558,31 @@ static int psz_recover_zone(struct psz_context *cxt, struct pstore_zone *zone)
550558
return ret;
551559
}
552560

561+
static int psz_recover_zones(struct psz_context *cxt,
562+
struct pstore_zone **zones, unsigned int cnt)
563+
{
564+
int ret;
565+
unsigned int i;
566+
struct pstore_zone *zone;
567+
568+
if (!zones)
569+
return 0;
570+
571+
for (i = 0; i < cnt; i++) {
572+
zone = zones[i];
573+
if (unlikely(!zone))
574+
continue;
575+
ret = psz_recover_zone(cxt, zone);
576+
if (ret)
577+
goto recover_fail;
578+
}
579+
580+
return 0;
581+
recover_fail:
582+
pr_debug("recover %s[%u] failed\n", zone->name, i);
583+
return ret;
584+
}
585+
553586
/**
554587
* psz_recovery() - recover data from storage
555588
* @cxt: the context of pstore/zone
@@ -574,6 +607,10 @@ static inline int psz_recovery(struct psz_context *cxt)
574607
goto out;
575608

576609
ret = psz_recover_zone(cxt, cxt->cpsz);
610+
if (ret)
611+
goto out;
612+
613+
ret = psz_recover_zones(cxt, cxt->fpszs, cxt->ftrace_max_cnt);
577614

578615
out:
579616
if (unlikely(ret))
@@ -592,6 +629,7 @@ static int psz_pstore_open(struct pstore_info *psi)
592629
cxt->kmsg_read_cnt = 0;
593630
cxt->pmsg_read_cnt = 0;
594631
cxt->console_read_cnt = 0;
632+
cxt->ftrace_read_cnt = 0;
595633
return 0;
596634
}
597635

@@ -658,6 +696,10 @@ static int psz_pstore_erase(struct pstore_record *record)
658696
return psz_record_erase(cxt, cxt->ppsz);
659697
case PSTORE_TYPE_CONSOLE:
660698
return psz_record_erase(cxt, cxt->cpsz);
699+
case PSTORE_TYPE_FTRACE:
700+
if (record->id >= cxt->ftrace_max_cnt)
701+
return -EINVAL;
702+
return psz_record_erase(cxt, cxt->fpszs[record->id]);
661703
default: return -EINVAL;
662704
}
663705
}
@@ -802,6 +844,13 @@ static int notrace psz_pstore_write(struct pstore_record *record)
802844
return psz_record_write(cxt->cpsz, record);
803845
case PSTORE_TYPE_PMSG:
804846
return psz_record_write(cxt->ppsz, record);
847+
case PSTORE_TYPE_FTRACE: {
848+
int zonenum = smp_processor_id();
849+
850+
if (!cxt->fpszs)
851+
return -ENOSPC;
852+
return psz_record_write(cxt->fpszs[zonenum], record);
853+
}
805854
default:
806855
return -EINVAL;
807856
}
@@ -817,6 +866,14 @@ static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
817866
return zone;
818867
}
819868

869+
if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
870+
/*
871+
* No need psz_old_ok(). Let psz_ftrace_read() do so for
872+
* combination. psz_ftrace_read() should traverse over
873+
* all zones in case of some zone without data.
874+
*/
875+
return cxt->fpszs[cxt->ftrace_read_cnt++];
876+
820877
if (cxt->pmsg_read_cnt == 0) {
821878
cxt->pmsg_read_cnt++;
822879
zone = cxt->ppsz;
@@ -891,6 +948,38 @@ static ssize_t psz_kmsg_read(struct pstore_zone *zone,
891948
return size + hlen;
892949
}
893950

951+
/* try to combine all ftrace zones */
952+
static ssize_t psz_ftrace_read(struct pstore_zone *zone,
953+
struct pstore_record *record)
954+
{
955+
struct psz_context *cxt;
956+
struct psz_buffer *buf;
957+
int ret;
958+
959+
if (!zone || !record)
960+
return -ENOSPC;
961+
962+
if (!psz_old_ok(zone))
963+
goto out;
964+
965+
buf = (struct psz_buffer *)zone->oldbuf;
966+
if (!buf)
967+
return -ENOMSG;
968+
969+
ret = pstore_ftrace_combine_log(&record->buf, &record->size,
970+
(char *)buf->data, atomic_read(&buf->datalen));
971+
if (unlikely(ret))
972+
return ret;
973+
974+
out:
975+
cxt = record->psi->data;
976+
if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
977+
/* then, read next ftrace zone */
978+
return -ENOMSG;
979+
record->id = 0;
980+
return record->size ? record->size : -ENOMSG;
981+
}
982+
894983
static ssize_t psz_record_read(struct pstore_zone *zone,
895984
struct pstore_record *record)
896985
{
@@ -941,6 +1030,9 @@ static ssize_t psz_pstore_read(struct pstore_record *record)
9411030
readop = psz_kmsg_read;
9421031
record->id = cxt->kmsg_read_cnt - 1;
9431032
break;
1033+
case PSTORE_TYPE_FTRACE:
1034+
readop = psz_ftrace_read;
1035+
break;
9441036
case PSTORE_TYPE_CONSOLE:
9451037
fallthrough;
9461038
case PSTORE_TYPE_PMSG:
@@ -1005,6 +1097,8 @@ static void psz_free_all_zones(struct psz_context *cxt)
10051097
psz_free_zone(&cxt->ppsz);
10061098
if (cxt->cpsz)
10071099
psz_free_zone(&cxt->cpsz);
1100+
if (cxt->fpszs)
1101+
psz_free_zones(&cxt->fpszs, &cxt->ftrace_max_cnt);
10081102
}
10091103

10101104
static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
@@ -1115,6 +1209,17 @@ static int psz_alloc_zones(struct psz_context *cxt)
11151209
goto free_out;
11161210
}
11171211

1212+
off_size += info->ftrace_size;
1213+
cxt->fpszs = psz_init_zones(PSTORE_TYPE_FTRACE, &off,
1214+
info->ftrace_size,
1215+
info->ftrace_size / nr_cpu_ids,
1216+
&cxt->ftrace_max_cnt);
1217+
if (IS_ERR(cxt->fpszs)) {
1218+
err = PTR_ERR(cxt->fpszs);
1219+
cxt->fpszs = NULL;
1220+
goto free_out;
1221+
}
1222+
11181223
cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
11191224
info->total_size - off_size,
11201225
info->kmsg_size, &cxt->kmsg_max_cnt);
@@ -1149,7 +1254,8 @@ int register_pstore_zone(struct pstore_zone_info *info)
11491254
return -EINVAL;
11501255
}
11511256

1152-
if (!info->kmsg_size && !info->pmsg_size && !info->console_size) {
1257+
if (!info->kmsg_size && !info->pmsg_size && !info->console_size &&
1258+
!info->ftrace_size) {
11531259
pr_warn("at least one record size must be non-zero\n");
11541260
return -EINVAL;
11551261
}
@@ -1173,6 +1279,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
11731279
check_size(kmsg_size, SECTOR_SIZE);
11741280
check_size(pmsg_size, SECTOR_SIZE);
11751281
check_size(console_size, SECTOR_SIZE);
1282+
check_size(ftrace_size, SECTOR_SIZE);
11761283

11771284
#undef check_size
11781285

@@ -1200,6 +1307,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
12001307
pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
12011308
pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
12021309
pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
1310+
pr_debug("\tftrace size : %ld Bytes\n", info->ftrace_size);
12031311

12041312
err = psz_alloc_zones(cxt);
12051313
if (err) {
@@ -1237,6 +1345,10 @@ int register_pstore_zone(struct pstore_zone_info *info)
12371345
cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
12381346
pr_cont(" console");
12391347
}
1348+
if (info->ftrace_size) {
1349+
cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
1350+
pr_cont(" ftrace");
1351+
}
12401352
pr_cont("\n");
12411353

12421354
err = pstore_register(&cxt->pstore);

include/linux/pstore_zone.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
1919
* @max_reason: Maximum kmsg dump reason to store.
2020
* @pmsg_size: The size of pmsg zone which is the same as @kmsg_size.
2121
* @console_size:The size of console zone which is the same as @kmsg_size.
22+
* @ftrace_size:The size of ftrace zone which is the same as @kmsg_size.
2223
* @read: The general read operation. Both of the function parameters
2324
* @size and @offset are relative value to storage.
2425
* On success, the number of bytes should be returned, others
@@ -37,6 +38,7 @@ struct pstore_zone_info {
3738
int max_reason;
3839
unsigned long pmsg_size;
3940
unsigned long console_size;
41+
unsigned long ftrace_size;
4042
pstore_zone_read_op read;
4143
pstore_zone_write_op write;
4244
pstore_zone_write_op panic_write;

0 commit comments

Comments
 (0)