Skip to content

Commit a39f009

Browse files
committed
Merge tag 'pstore-v5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull pstore cleanups from Kees Cook: - Remove some needless memory allocations (Yue Hu, Kees Cook) - Add zero-length checks to avoid no-op calls (Yue Hu) * tag 'pstore-v5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: pstore/ram: Avoid needless alloc during header write pstore/ram: Add kmsg hlen zero check to ramoops_pstore_write() pstore/ram: Move initialization earlier pstore: Avoid writing records with zero size pstore/ram: Replace dummy_data heap memory with stack memory
2 parents 5bbf1b6 + 93ee4b7 commit a39f009

2 files changed

Lines changed: 32 additions & 35 deletions

File tree

‎fs/pstore/platform.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c)
501501
{
502502
struct pstore_record record;
503503

504+
if (!c)
505+
return;
506+
504507
pstore_record_init(&record, psinfo);
505508
record.type = PSTORE_TYPE_CONSOLE;
506509

‎fs/pstore/ram.c‎

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ struct ramoops_context {
110110
};
111111

112112
static struct platform_device *dummy;
113-
static struct ramoops_platform_data *dummy_data;
114113

115114
static int ramoops_pstore_open(struct pstore_info *psi)
116115
{
@@ -346,17 +345,15 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record)
346345
static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
347346
struct pstore_record *record)
348347
{
349-
char *hdr;
348+
char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */
350349
size_t len;
351350

352-
hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
351+
len = scnprintf(hdr, sizeof(hdr),
352+
RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
353353
(time64_t)record->time.tv_sec,
354354
record->time.tv_nsec / 1000,
355355
record->compressed ? 'C' : 'D');
356-
WARN_ON_ONCE(!hdr);
357-
len = hdr ? strlen(hdr) : 0;
358356
persistent_ram_write(prz, hdr, len);
359-
kfree(hdr);
360357

361358
return len;
362359
}
@@ -424,6 +421,9 @@ static int notrace ramoops_pstore_write(struct pstore_record *record)
424421

425422
/* Build header and append record contents. */
426423
hlen = ramoops_write_kmsg_hdr(prz, record);
424+
if (!hlen)
425+
return -ENOMEM;
426+
427427
size = record->size;
428428
if (size + hlen > prz->buffer_size)
429429
size = prz->buffer_size - hlen;
@@ -716,15 +716,6 @@ static int ramoops_probe(struct platform_device *pdev)
716716
phys_addr_t paddr;
717717
int err = -EINVAL;
718718

719-
if (dev_of_node(dev) && !pdata) {
720-
pdata = &pdata_local;
721-
memset(pdata, 0, sizeof(*pdata));
722-
723-
err = ramoops_parse_dt(pdev, pdata);
724-
if (err < 0)
725-
goto fail_out;
726-
}
727-
728719
/*
729720
* Only a single ramoops area allowed at a time, so fail extra
730721
* probes.
@@ -734,6 +725,15 @@ static int ramoops_probe(struct platform_device *pdev)
734725
goto fail_out;
735726
}
736727

728+
if (dev_of_node(dev) && !pdata) {
729+
pdata = &pdata_local;
730+
memset(pdata, 0, sizeof(*pdata));
731+
732+
err = ramoops_parse_dt(pdev, pdata);
733+
if (err < 0)
734+
goto fail_out;
735+
}
736+
737737
/* Make sure we didn't get bogus platform data pointer. */
738738
if (!pdata) {
739739
pr_err("NULL platform data\n");
@@ -892,13 +892,12 @@ static inline void ramoops_unregister_dummy(void)
892892
{
893893
platform_device_unregister(dummy);
894894
dummy = NULL;
895-
896-
kfree(dummy_data);
897-
dummy_data = NULL;
898895
}
899896

900897
static void __init ramoops_register_dummy(void)
901898
{
899+
struct ramoops_platform_data pdata;
900+
902901
/*
903902
* Prepare a dummy platform data structure to carry the module
904903
* parameters. If mem_size isn't set, then there are no module
@@ -909,30 +908,25 @@ static void __init ramoops_register_dummy(void)
909908

910909
pr_info("using module parameters\n");
911910

912-
dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
913-
if (!dummy_data) {
914-
pr_info("could not allocate pdata\n");
915-
return;
916-
}
917-
918-
dummy_data->mem_size = mem_size;
919-
dummy_data->mem_address = mem_address;
920-
dummy_data->mem_type = mem_type;
921-
dummy_data->record_size = record_size;
922-
dummy_data->console_size = ramoops_console_size;
923-
dummy_data->ftrace_size = ramoops_ftrace_size;
924-
dummy_data->pmsg_size = ramoops_pmsg_size;
925-
dummy_data->dump_oops = dump_oops;
926-
dummy_data->flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
911+
memset(&pdata, 0, sizeof(pdata));
912+
pdata.mem_size = mem_size;
913+
pdata.mem_address = mem_address;
914+
pdata.mem_type = mem_type;
915+
pdata.record_size = record_size;
916+
pdata.console_size = ramoops_console_size;
917+
pdata.ftrace_size = ramoops_ftrace_size;
918+
pdata.pmsg_size = ramoops_pmsg_size;
919+
pdata.dump_oops = dump_oops;
920+
pdata.flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
927921

928922
/*
929923
* For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
930924
* (using 1 byte for ECC isn't much of use anyway).
931925
*/
932-
dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
926+
pdata.ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
933927

934928
dummy = platform_device_register_data(NULL, "ramoops", -1,
935-
dummy_data, sizeof(struct ramoops_platform_data));
929+
&pdata, sizeof(pdata));
936930
if (IS_ERR(dummy)) {
937931
pr_info("could not create platform device: %ld\n",
938932
PTR_ERR(dummy));

0 commit comments

Comments
 (0)