From patchwork Tue Jun 7 16:47:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 9162015 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3522260467 for ; Tue, 7 Jun 2016 16:50:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2618C27248 for ; Tue, 7 Jun 2016 16:50:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1A2F12824F; Tue, 7 Jun 2016 16:50:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 89D1F27248 for ; Tue, 7 Jun 2016 16:50:01 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1bAKB2-00022B-Ga; Tue, 07 Jun 2016 16:48:28 +0000 Received: from mail-pa0-x22e.google.com ([2607:f8b0:400e:c03::22e]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1bAKAs-0001ul-RE for linux-arm-kernel@lists.infradead.org; Tue, 07 Jun 2016 16:48:19 +0000 Received: by mail-pa0-x22e.google.com with SMTP id bz2so47560819pad.1 for ; Tue, 07 Jun 2016 09:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6QpFn2uQoGHtzeMFinR6scJkIhnehU3fIXaXUjzAMWY=; b=GTNaSCdYLZEtwZSmuDSCWcEsweuzD+fbOwZeMgBPhSEx9AABVhPHi5YqQywQ6s6jyI ZY88OFuX/chpSEANe3uBNwTqupAt0APVYrytt8vISoPbcnWUMWJuovPxqCQaUGSa6N0F j+cOTDLE15KJ2vIT8NlTwm83UmHV6r/SSUvlU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6QpFn2uQoGHtzeMFinR6scJkIhnehU3fIXaXUjzAMWY=; b=abWFiIpUxCNzRiBPs0bNgwY/wl+l92SZiha+ZlEKU6+ncxftLksXH8LMFtCUT+KLqp jSJ8gVXxzIb4HN7X0gZmfAhnDy/56Qu2fFSKekm5/xMV5MhErE3S7oYnM9LXHv0A0kSV lVpaH/XI+EUDHgEjPukk4+MaoQsCyiSbLTLVW8s5yW3x0IL+Q9Z1xHEt9JcRfWtXHhQH ziLpjDlaHFs7bkpz5Sspt2/DpjUCW7tmpuE3Jvuoyl3Q6EA8vFxFhlblHKQkTyZompnK STCQUrdlHnkLeU1+BB3SYrYhiC/zCAlM/bw3D5czT7j3P+ycvgFU7wRH5I15cIYCMe9u WbfQ== X-Gm-Message-State: ALyK8tLPgCQkW4E1N38T0Y2MGqVm0Wg9VgA/nJ3SkupphN0eHtG3nRL6E6kjNuNXbfEy8PB4 X-Received: by 10.66.132.103 with SMTP id ot7mr394919pab.60.1465318078023; Tue, 07 Jun 2016 09:47:58 -0700 (PDT) Received: from localhost.localdomain (ip68-101-172-78.sd.sd.cox.net. [68.101.172.78]) by smtp.gmail.com with ESMTPSA id fd8sm34342567pac.23.2016.06.07.09.47.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Jun 2016 09:47:57 -0700 (PDT) From: Stephen Boyd To: linux-kernel@vger.kernel.org Subject: [RESEND/PATCHv4 v4 1/3] firmware: Consolidate kmap/read/write logic Date: Tue, 7 Jun 2016 09:47:39 -0700 Message-Id: <20160607164741.31849-2-stephen.boyd@linaro.org> X-Mailer: git-send-email 2.9.0-rc1 In-Reply-To: <20160607164741.31849-1-stephen.boyd@linaro.org> References: <20160607164741.31849-1-stephen.boyd@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160607_094818_913402_86D0F0DC X-CRM114-Status: GOOD ( 13.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ming Lei , Vikram Mulukutla , Mark Brown , Andrew Morton , Mimi Zohar , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP We use similar structured code to read and write the kmapped firmware pages. The only difference is read copies from the kmap region and write copies to it. Consolidate this into one function to reduce duplication. Cc: Vikram Mulukutla Signed-off-by: Stephen Boyd --- drivers/base/firmware_class.c | 57 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c index 773fc3099769..01d55723d82c 100644 --- a/drivers/base/firmware_class.c +++ b/drivers/base/firmware_class.c @@ -691,6 +691,29 @@ out: static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); +static void firmware_rw(struct firmware_buf *buf, char *buffer, + loff_t offset, size_t count, bool read) +{ + while (count) { + void *page_data; + int page_nr = offset >> PAGE_SHIFT; + int page_ofs = offset & (PAGE_SIZE-1); + int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); + + page_data = kmap(buf->pages[page_nr]); + + if (read) + memcpy(buffer, page_data + page_ofs, page_cnt); + else + memcpy(page_data + page_ofs, buffer, page_cnt); + + kunmap(buf->pages[page_nr]); + buffer += page_cnt; + offset += page_cnt; + count -= page_cnt; + } +} + static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buffer, loff_t offset, size_t count) @@ -715,21 +738,8 @@ static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, ret_count = count; - while (count) { - void *page_data; - int page_nr = offset >> PAGE_SHIFT; - int page_ofs = offset & (PAGE_SIZE-1); - int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); - - page_data = kmap(buf->pages[page_nr]); - - memcpy(buffer, page_data + page_ofs, page_cnt); + firmware_rw(buf, buffer, offset, count, true); - kunmap(buf->pages[page_nr]); - buffer += page_cnt; - offset += page_cnt; - count -= page_cnt; - } out: mutex_unlock(&fw_lock); return ret_count; @@ -809,24 +819,9 @@ static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, goto out; retval = count; + firmware_rw(buf, buffer, offset, count, false); - while (count) { - void *page_data; - int page_nr = offset >> PAGE_SHIFT; - int page_ofs = offset & (PAGE_SIZE - 1); - int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); - - page_data = kmap(buf->pages[page_nr]); - - memcpy(page_data + page_ofs, buffer, page_cnt); - - kunmap(buf->pages[page_nr]); - buffer += page_cnt; - offset += page_cnt; - count -= page_cnt; - } - - buf->size = max_t(size_t, offset, buf->size); + buf->size = max_t(size_t, offset + count, buf->size); out: mutex_unlock(&fw_lock); return retval;