From patchwork Wed Mar 1 19:19:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 9599001 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 0218360547 for ; Wed, 1 Mar 2017 19:22:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1F4E20700 for ; Wed, 1 Mar 2017 19:22:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D66ED27D85; Wed, 1 Mar 2017 19:22:08 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DEAF2756B for ; Wed, 1 Mar 2017 19:22:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753240AbdCATWH (ORCPT ); Wed, 1 Mar 2017 14:22:07 -0500 Received: from smtprelay0242.hostedemail.com ([216.40.44.242]:50372 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752096AbdCATWH (ORCPT ); Wed, 1 Mar 2017 14:22:07 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay06.hostedemail.com (Postfix) with ESMTP id 6D47A9EA13; Wed, 1 Mar 2017 19:19:17 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: drop06_8b2daa72bd24a X-Filterd-Recvd-Size: 2597 Received: from joe-laptop.perches.com (unknown [47.151.132.55]) (Authenticated sender: joe@perches.com) by omf13.hostedemail.com (Postfix) with ESMTPA; Wed, 1 Mar 2017 19:19:16 +0000 (UTC) From: Joe Perches To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] HID: Use krealloc Date: Wed, 1 Mar 2017 11:19:09 -0800 Message-Id: <4048f6d655d55b19fdaecab4e61f6883e12f72f0.1488395879.git.joe@perches.com> X-Mailer: git-send-email 2.10.0.rc2.1.g053435c In-Reply-To: References: Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This kmalloc/memcpy is equivalent to krealloc, so use krealloc to avoid a possibly unnecessary memcpy. Miscellanea: o Add temporary variables osize and nsize to improve readability o krealloc has a defect and does not support GFP_ZERO so the memset must be kept Signed-off-by: Joe Perches --- drivers/hid/hid-core.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index e9e87d337446..b1ebf53ca879 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -131,21 +131,19 @@ static int open_collection(struct hid_parser *parser, unsigned type) } if (parser->device->maxcollection == parser->device->collection_size) { - collection = kmalloc(sizeof(struct hid_collection) * - parser->device->collection_size * 2, GFP_KERNEL); - if (collection == NULL) { - hid_err(parser->device, "failed to reallocate collection array\n"); + unsigned int osize = parser->device->collection_size; + unsigned int nsize = osize * 2; + + collection = krealloc(parser->device->collection, + nsize * sizeof(struct hid_collection), + GFP_KERNEL); + if (!collection) return -ENOMEM; - } - memcpy(collection, parser->device->collection, - sizeof(struct hid_collection) * - parser->device->collection_size); - memset(collection + parser->device->collection_size, 0, - sizeof(struct hid_collection) * - parser->device->collection_size); - kfree(parser->device->collection); + + memset(collection + osize, 0, + sizeof(struct hid_collection) * osize); parser->device->collection = collection; - parser->device->collection_size *= 2; + parser->device->collection_size = nsize; } parser->collection_stack[parser->collection_stack_ptr++] =