From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752583AbdCATsL (ORCPT ); Wed, 1 Mar 2017 14:48:11 -0500 Received: from smtprelay0123.hostedemail.com ([216.40.44.123]:59411 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750813AbdCATr3 (ORCPT ); Wed, 1 Mar 2017 14:47:29 -0500 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::,RULES_HIT:41:69:355:379:541:800:960:966:973:988:989:1260:1345:1359:1437:1534:1541:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2914:3138:3139:3140:3141:3142:3352:3865:3867:3868:3871:4321:4385:4605:5007:6261:10004:10848:11026:11233:11658:11914:12043:12296:12438:12555:12679:12895:12986:13069:13311:13357:14181:14384:14394:14721:21080:30054,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: drop06_8b2daa72bd24a X-Filterd-Recvd-Size: 2597 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-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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++] = -- 2.10.0.rc2.1.g053435c