From fee07f5570340e67e0571a97d0bb2e469559f971 Mon Sep 17 00:00:00 2001 From: Kees Cook Date: Tue, 12 Feb 2019 13:09:36 -0800 Subject: [PATCH] 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 --- fs/pstore/ram.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index 98f37312c0ce..57e32cdc9c05 100755 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -357,19 +357,15 @@ out: static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz, struct pstore_record *record) { - char *hdr; + char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */ size_t len; - hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n", + len = scnprintf(hdr, sizeof(hdr), + RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n", record->time.tv_sec, record->time.tv_nsec / 1000, record->compressed ? 'C' : 'D'); - if (WARN_ON_ONCE(!hdr)) - return 0; - - len = strlen(hdr); persistent_ram_write(prz, hdr, len); - kfree(hdr); return len; }