Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pstore/ram: Avoid needless alloc during header write

Since the header is a fixed small maximum size, just use a stack variable
to avoid memory allocation in the write path.

Signed-off-by: Kees Cook <keescook@chromium.org>

+3 -7
+3 -7
fs/pstore/ram.c
··· 345 345 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz, 346 346 struct pstore_record *record) 347 347 { 348 - char *hdr; 348 + char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */ 349 349 size_t len; 350 350 351 - 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", 352 353 (time64_t)record->time.tv_sec, 353 354 record->time.tv_nsec / 1000, 354 355 record->compressed ? 'C' : 'D'); 355 - if (WARN_ON_ONCE(!hdr)) 356 - return 0; 357 - 358 - len = strlen(hdr); 359 356 persistent_ram_write(prz, hdr, len); 360 - kfree(hdr); 361 357 362 358 return len; 363 359 }