Skip to content

Commit cc9c4d1

Browse files
WeiXiong Liaokees
authored andcommitted
pstore/zone,blk: Add console frontend support
Support backend for console. To enable console backend, just make console_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]/ Signed-off-by: Kees Cook <[email protected]>
1 parent 0dc0682 commit cc9c4d1

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed

fs/pstore/Kconfig

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ config PSTORE_BLK_BLKDEV
180180
help
181181
Which block device should be used for pstore/blk.
182182

183-
It accept the following variants:
183+
It accepts the following variants:
184184
1) <hex_major><hex_minor> device number in hexadecimal representation,
185185
with no leading 0x, for example b302.
186-
2) /dev/<disk_name> represents the device number of disk
187-
3) /dev/<disk_name><decimal> represents the device number
186+
2) /dev/<disk_name> represents the device name of disk
187+
3) /dev/<disk_name><decimal> represents the device name and number
188188
of partition - device number of disk plus the partition number
189189
4) /dev/<disk_name>p<decimal> - same as the above, this form is
190190
used when disk name of partitioned disk ends with a digit.
@@ -236,3 +236,15 @@ config PSTORE_BLK_PMSG_SIZE
236236

237237
NOTE that, both Kconfig and module parameters can configure
238238
pstore/blk, but module parameters have priority over Kconfig.
239+
240+
config PSTORE_BLK_CONSOLE_SIZE
241+
int "Size in Kbytes of console log to store"
242+
depends on PSTORE_BLK
243+
depends on PSTORE_CONSOLE
244+
default 64
245+
help
246+
This just sets size of console log (console_size) to store via
247+
pstore/blk. The size is in KB and must be a multiple of 4.
248+
249+
NOTE that, both Kconfig and module parameters can configure
250+
pstore/blk, but module parameters have priority over Kconfig.

fs/pstore/blk.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ static long pmsg_size = -1;
3535
module_param(pmsg_size, long, 0400);
3636
MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
3737

38+
#if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
39+
static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
40+
#else
41+
static long console_size = -1;
42+
#endif
43+
module_param(console_size, long, 0400);
44+
MODULE_PARM_DESC(console_size, "console size in kbytes");
45+
3846
/*
3947
* blkdev - the block device to use for pstore storage
4048
*
@@ -91,7 +99,8 @@ struct bdev_info {
9199
* whole disk).
92100
* On success, the number of bytes should be returned, others
93101
* means error.
94-
* @write: The same as @read.
102+
* @write: The same as @read, but the following error number:
103+
* -EBUSY means try to write again later.
95104
* @panic_write:The write operation only used for panic case. It's optional
96105
* if you do not care panic log. The parameters and return value
97106
* are the same as @read.
@@ -142,6 +151,7 @@ static int psblk_register_do(struct pstore_device_info *dev)
142151

143152
verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
144153
verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
154+
verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
145155
#undef verify_size
146156

147157
pstore_zone_info->total_size = dev->total_size;

fs/pstore/zone.c

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ struct pstore_zone {
9191
*
9292
* @kpszs: kmsg dump storage zones
9393
* @ppsz: pmsg storage zone
94+
* @cpsz: console storage zone
9495
* @kmsg_max_cnt: max count of @kpszs
9596
* @kmsg_read_cnt: counter of total read kmsg dumps
9697
* @kmsg_write_cnt: counter of total kmsg dump writes
9798
* @pmsg_read_cnt: counter of total read pmsg zone
99+
* @console_read_cnt: counter of total read console zone
98100
* @oops_counter: counter of oops dumps
99101
* @panic_counter: counter of panic dumps
100102
* @recovered: whether finished recovering data from storage
@@ -106,10 +108,12 @@ struct pstore_zone {
106108
struct psz_context {
107109
struct pstore_zone **kpszs;
108110
struct pstore_zone *ppsz;
111+
struct pstore_zone *cpsz;
109112
unsigned int kmsg_max_cnt;
110113
unsigned int kmsg_read_cnt;
111114
unsigned int kmsg_write_cnt;
112115
unsigned int pmsg_read_cnt;
116+
unsigned int console_read_cnt;
113117
/*
114118
* These counters should be calculated during recovery.
115119
* It records the oops/panic times after crashes rather than boots.
@@ -129,6 +133,9 @@ struct psz_context {
129133
};
130134
static struct psz_context pstore_zone_cxt;
131135

136+
static void psz_flush_all_dirty_zones(struct work_struct *);
137+
static DECLARE_DELAYED_WORK(psz_cleaner, psz_flush_all_dirty_zones);
138+
132139
/**
133140
* enum psz_flush_mode - flush mode for psz_zone_write()
134141
*
@@ -237,6 +244,9 @@ static int psz_zone_write(struct pstore_zone *zone,
237244
return 0;
238245
dirty:
239246
atomic_set(&zone->dirty, true);
247+
/* flush dirty zones nicely */
248+
if (wcnt == -EBUSY && !is_on_panic())
249+
schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(500));
240250
return -EBUSY;
241251
}
242252

@@ -293,6 +303,21 @@ static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new)
293303
return 0;
294304
}
295305

306+
static void psz_flush_all_dirty_zones(struct work_struct *work)
307+
{
308+
struct psz_context *cxt = &pstore_zone_cxt;
309+
int ret = 0;
310+
311+
if (cxt->ppsz)
312+
ret |= psz_flush_dirty_zone(cxt->ppsz);
313+
if (cxt->cpsz)
314+
ret |= psz_flush_dirty_zone(cxt->cpsz);
315+
if (cxt->kpszs)
316+
ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
317+
if (ret && cxt->pstore_zone_info)
318+
schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
319+
}
320+
296321
static int psz_kmsg_recover_data(struct psz_context *cxt)
297322
{
298323
struct pstore_zone_info *info = cxt->pstore_zone_info;
@@ -545,6 +570,10 @@ static inline int psz_recovery(struct psz_context *cxt)
545570
goto out;
546571

547572
ret = psz_recover_zone(cxt, cxt->ppsz);
573+
if (ret)
574+
goto out;
575+
576+
ret = psz_recover_zone(cxt, cxt->cpsz);
548577

549578
out:
550579
if (unlikely(ret))
@@ -562,6 +591,7 @@ static int psz_pstore_open(struct pstore_info *psi)
562591

563592
cxt->kmsg_read_cnt = 0;
564593
cxt->pmsg_read_cnt = 0;
594+
cxt->console_read_cnt = 0;
565595
return 0;
566596
}
567597

@@ -626,8 +656,9 @@ static int psz_pstore_erase(struct pstore_record *record)
626656
return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record);
627657
case PSTORE_TYPE_PMSG:
628658
return psz_record_erase(cxt, cxt->ppsz);
629-
default:
630-
return -EINVAL;
659+
case PSTORE_TYPE_CONSOLE:
660+
return psz_record_erase(cxt, cxt->cpsz);
661+
default: return -EINVAL;
631662
}
632663
}
633664

@@ -690,9 +721,10 @@ static int notrace psz_kmsg_write(struct psz_context *cxt,
690721
return -ENOSPC;
691722

692723
ret = psz_kmsg_write_record(cxt, record);
693-
if (!ret) {
724+
if (!ret && is_on_panic()) {
725+
/* ensure all data are flushed to storage when panic */
694726
pr_debug("try to flush other dirty zones\n");
695-
psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
727+
psz_flush_all_dirty_zones(NULL);
696728
}
697729

698730
/* always return 0 as we had handled it on buffer */
@@ -756,9 +788,18 @@ static int notrace psz_pstore_write(struct pstore_record *record)
756788
record->reason == KMSG_DUMP_PANIC)
757789
atomic_set(&cxt->on_panic, 1);
758790

791+
/*
792+
* if on panic, do not write except panic records
793+
* Fix case that panic_write prints log which wakes up console backend.
794+
*/
795+
if (is_on_panic() && record->type != PSTORE_TYPE_DMESG)
796+
return -EBUSY;
797+
759798
switch (record->type) {
760799
case PSTORE_TYPE_DMESG:
761800
return psz_kmsg_write(cxt, record);
801+
case PSTORE_TYPE_CONSOLE:
802+
return psz_record_write(cxt->cpsz, record);
762803
case PSTORE_TYPE_PMSG:
763804
return psz_record_write(cxt->ppsz, record);
764805
default:
@@ -783,6 +824,13 @@ static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
783824
return zone;
784825
}
785826

827+
if (cxt->console_read_cnt == 0) {
828+
cxt->console_read_cnt++;
829+
zone = cxt->cpsz;
830+
if (psz_old_ok(zone))
831+
return zone;
832+
}
833+
786834
return NULL;
787835
}
788836

@@ -893,6 +941,8 @@ static ssize_t psz_pstore_read(struct pstore_record *record)
893941
readop = psz_kmsg_read;
894942
record->id = cxt->kmsg_read_cnt - 1;
895943
break;
944+
case PSTORE_TYPE_CONSOLE:
945+
fallthrough;
896946
case PSTORE_TYPE_PMSG:
897947
readop = psz_record_read;
898948
break;
@@ -953,6 +1003,8 @@ static void psz_free_all_zones(struct psz_context *cxt)
9531003
psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
9541004
if (cxt->ppsz)
9551005
psz_free_zone(&cxt->ppsz);
1006+
if (cxt->cpsz)
1007+
psz_free_zone(&cxt->cpsz);
9561008
}
9571009

9581010
static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
@@ -1054,6 +1106,15 @@ static int psz_alloc_zones(struct psz_context *cxt)
10541106
goto free_out;
10551107
}
10561108

1109+
off_size += info->console_size;
1110+
cxt->cpsz = psz_init_zone(PSTORE_TYPE_CONSOLE, &off,
1111+
info->console_size);
1112+
if (IS_ERR(cxt->cpsz)) {
1113+
err = PTR_ERR(cxt->cpsz);
1114+
cxt->cpsz = NULL;
1115+
goto free_out;
1116+
}
1117+
10571118
cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
10581119
info->total_size - off_size,
10591120
info->kmsg_size, &cxt->kmsg_max_cnt);
@@ -1088,7 +1149,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
10881149
return -EINVAL;
10891150
}
10901151

1091-
if (!info->kmsg_size && !info->pmsg_size) {
1152+
if (!info->kmsg_size && !info->pmsg_size && !info->console_size) {
10921153
pr_warn("at least one record size must be non-zero\n");
10931154
return -EINVAL;
10941155
}
@@ -1111,6 +1172,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
11111172
check_size(total_size, 4096);
11121173
check_size(kmsg_size, SECTOR_SIZE);
11131174
check_size(pmsg_size, SECTOR_SIZE);
1175+
check_size(console_size, SECTOR_SIZE);
11141176

11151177
#undef check_size
11161178

@@ -1137,6 +1199,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
11371199
pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
11381200
pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
11391201
pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
1202+
pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
11401203

11411204
err = psz_alloc_zones(cxt);
11421205
if (err) {
@@ -1170,6 +1233,10 @@ int register_pstore_zone(struct pstore_zone_info *info)
11701233
cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
11711234
pr_cont(" pmsg");
11721235
}
1236+
if (info->console_size) {
1237+
cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
1238+
pr_cont(" console");
1239+
}
11731240
pr_cont("\n");
11741241

11751242
err = pstore_register(&cxt->pstore);
@@ -1211,6 +1278,10 @@ void unregister_pstore_zone(struct pstore_zone_info *info)
12111278
/* Stop incoming writes from pstore. */
12121279
pstore_unregister(&cxt->pstore);
12131280

1281+
/* Flush any pending writes. */
1282+
psz_flush_all_dirty_zones(NULL);
1283+
flush_delayed_work(&psz_cleaner);
1284+
12141285
/* Clean up allocations. */
12151286
kfree(cxt->pstore.buf);
12161287
cxt->pstore.buf = NULL;

include/linux/pstore_zone.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
1818
* it must be multiple of SECTOR_SIZE(512 Bytes).
1919
* @max_reason: Maximum kmsg dump reason to store.
2020
* @pmsg_size: The size of pmsg zone which is the same as @kmsg_size.
21+
* @console_size:The size of console zone which is the same as @kmsg_size.
2122
* @read: The general read operation. Both of the function parameters
2223
* @size and @offset are relative value to storage.
2324
* On success, the number of bytes should be returned, others
2425
* means error.
25-
* @write: The same as @read.
26+
* @write: The same as @read, but -EBUSY means try to write again later.
2627
* @panic_write:The write operation only used for panic case. It's optional
2728
* if you do not care panic log. The parameters and return value
2829
* are the same as @read.
@@ -35,6 +36,7 @@ struct pstore_zone_info {
3536
unsigned long kmsg_size;
3637
int max_reason;
3738
unsigned long pmsg_size;
39+
unsigned long console_size;
3840
pstore_zone_read_op read;
3941
pstore_zone_write_op write;
4042
pstore_zone_write_op panic_write;

0 commit comments

Comments
 (0)