From patchwork Mon Mar 6 21:55:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 735960 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vcYxS09G4z9sN1 for ; Tue, 7 Mar 2017 09:14:52 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="mKY3IcM0"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3vcYxR615TzDqdP for ; Tue, 7 Mar 2017 09:14:51 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="mKY3IcM0"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mail-pg0-x22a.google.com (mail-pg0-x22a.google.com [IPv6:2607:f8b0:400e:c05::22a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3vcYWM1WpFzDqXp for ; Tue, 7 Mar 2017 08:55:43 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="mKY3IcM0"; dkim-atps=neutral Received: by mail-pg0-x22a.google.com with SMTP id 187so16867893pgb.3 for ; Mon, 06 Mar 2017 13:55:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eTNQ9hJV7HR/B5eT6f489Ldq7/nFVTcE2pBGIivXIM0=; b=mKY3IcM0OK1w+xJhan2AOrC1DJdcrRsQeNyCdhGWvp1th88M7P54U2b6M28u2W4iHC uLrN8+YP6Cfwbfi6poJj5b7Gy+8ipM9Gy8mkMjnBVsz5HSZJtbgKiLEJ2JxmKnhlAYcK voDrP+/j7punERMPZWYjix8q485dJquM/E7kg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eTNQ9hJV7HR/B5eT6f489Ldq7/nFVTcE2pBGIivXIM0=; b=QKTOzG2/o/5VEk2Bb39uFsRsGe5mUa3MAMc12Ci9UibR9KNbxLggda4T8O6sEsH0pA yZjkBei4kw55juqk1PjPv+e5gB+KnllVIstSzOh/wrtdOZWBGL6ptZIf0wbPYPAvRgtY U9cd/SRGwBAwXK9f/QFBIvk4TPgkmV/n47p40TEZg/KpnfMFyS81PD+w531uFVtoGJ5l ZEKDqo9agQAmz1ho4dnc7vHmpsZwiV70eCp9vSOCZscVXUyL3QVT3KqmikhaMdd6LQAw 9FdvrxcxBAH751L2znpFk55fb1CKjhH7jzlUjqnOlAwgewALt+v/ShY0uTUD9T5ojjtX HvUA== X-Gm-Message-State: AMke39nrbToil04bLYoSaopjn+7kFc2DbLY9v5/k3qVQhhf0zUYGMDGlPsAjcCwCejHmet4w X-Received: by 10.84.194.129 with SMTP id h1mr29796871pld.56.1488837341546; Mon, 06 Mar 2017 13:55:41 -0800 (PST) Received: from www.outflux.net ([2002:ada4:7085:0:205a:2b55:4e6d:7d63]) by smtp.gmail.com with ESMTPSA id h9sm41362045pfd.103.2017.03.06.13.55.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Mar 2017 13:55:38 -0800 (PST) From: Kees Cook To: linux-kernel@vger.kernel.org Subject: [PATCH 12/18] pstore: Pass record contents instead of copying Date: Mon, 6 Mar 2017 13:55:26 -0800 Message-Id: <1488837332-71582-13-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488837332-71582-1-git-send-email-keescook@chromium.org> References: <1488837332-71582-1-git-send-email-keescook@chromium.org> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-efi@vger.kernel.org, linux-doc@vger.kernel.org, Matt Fleming , Pan Xinhui , Paul Gortmaker , Paul Mackerras , Anton Vorontsov , linux-acpi@vger.kernel.org, Nathan Fontenot , Len Brown , Kees Cook , Nobuhiro Iwamatsu , Geliang Tang , Tony Luck , Daniel Axtens , Qiuxu Zhuo , Ard Biesheuvel , "Rafael J. Wysocki" , Colin Cross , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" pstore_mkfile() shouldn't have to memcpy the record contents. It can use the existing copy instead. This adjusts the allocation lifetime management and renames the contents variable from "data" to "buf" to assist moving to struct pstore_record in the future. Signed-off-by: Kees Cook --- fs/pstore/inode.c | 22 +++++++++++++++------- fs/pstore/platform.c | 16 ++++++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/fs/pstore/inode.c b/fs/pstore/inode.c index a98787bab3e6..3d1f047e4f41 100644 --- a/fs/pstore/inode.c +++ b/fs/pstore/inode.c @@ -52,7 +52,7 @@ struct pstore_private { u64 id; int count; ssize_t size; - char data[]; + char *buf; }; struct pstore_ftrace_seq_data { @@ -63,6 +63,14 @@ struct pstore_ftrace_seq_data { #define REC_SIZE sizeof(struct pstore_ftrace_record) +static void free_pstore_private(struct pstore_private *private) +{ + if (!private) + return; + kfree(private->buf); + kfree(private); +} + static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos) { struct pstore_private *ps = s->private; @@ -105,7 +113,7 @@ static int pstore_ftrace_seq_show(struct seq_file *s, void *v) { struct pstore_private *ps = s->private; struct pstore_ftrace_seq_data *data = v; - struct pstore_ftrace_record *rec = (void *)(ps->data + data->off); + struct pstore_ftrace_record *rec = (void *)(ps->buf + data->off); seq_printf(s, "CPU:%d ts:%llu %08lx %08lx %pf <- %pF\n", pstore_ftrace_decode_cpu(rec), @@ -143,7 +151,7 @@ static ssize_t pstore_file_read(struct file *file, char __user *userbuf, if (ps->type == PSTORE_TYPE_FTRACE) return seq_read(file, userbuf, count, ppos); - return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size); + return simple_read_from_buffer(userbuf, count, ppos, ps->buf, ps->size); } static int pstore_file_open(struct inode *inode, struct file *file) @@ -221,7 +229,7 @@ static void pstore_evict_inode(struct inode *inode) spin_lock_irqsave(&allpstore_lock, flags); list_del(&p->list); spin_unlock_irqrestore(&allpstore_lock, flags); - kfree(p); + free_pstore_private(p); } } @@ -332,7 +340,7 @@ int pstore_mkfile(struct pstore_record *record) goto fail; inode->i_mode = S_IFREG | 0444; inode->i_fop = &pstore_file_operations; - private = kmalloc(sizeof *private + size, GFP_KERNEL); + private = kzalloc(sizeof(*private), GFP_KERNEL); if (!private) goto fail_alloc; private->type = record->type; @@ -394,7 +402,7 @@ int pstore_mkfile(struct pstore_record *record) if (!dentry) goto fail_lockedalloc; - memcpy(private->data, record->buf, size); + private->buf = record->buf; inode->i_size = private->size = size; inode->i_private = private; @@ -414,7 +422,7 @@ int pstore_mkfile(struct pstore_record *record) fail_lockedalloc: inode_unlock(d_inode(root)); - kfree(private); + free_pstore_private(private); fail_alloc: iput(inode); diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index c0d401e732e6..d897e2f11b6a 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -828,14 +828,22 @@ void pstore_get_records(int quiet) if (psi->open && psi->open(psi)) goto out; + /* + * Backend callback read() allocates record.buf. decompress_record() + * may reallocate record.buf. On success, pstore_mkfile() will keep + * the record.buf, so free it only on failure. + */ while ((record.size = psi->read(&record)) > 0) { decompress_record(&record); rc = pstore_mkfile(&record); + if (rc) { + /* pstore_mkfile() did not take buf, so free it. */ + kfree(record.buf); + if (rc != -EEXIST || !quiet) + failed++; + } - if (rc && (rc != -EEXIST || !quiet)) - failed++; - - kfree(record.buf); + /* Reset for next record. */ memset(&record, 0, sizeof(record)); record.psi = psi; }