Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1100741ybi; Wed, 3 Jul 2019 09:30:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqxWD5ushBsyYPyMoAYmVWH5pAcuDb1AuDPazMp8YcbHGvDegz3NSxpaeyA1IfmCNHiDdqtK X-Received: by 2002:a63:f1c:: with SMTP id e28mr37490072pgl.147.1562171418046; Wed, 03 Jul 2019 09:30:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562171418; cv=none; d=google.com; s=arc-20160816; b=DZgrqOIotHYrTtmBucVBgl68NRyfwcgjDMMA4ts5m9U6b1z7U6347s1NVDLVSpctcJ 5qBpG7GtyH+xUnHpCmbv+aXje3YZSYWW12IBD9hgnBWHJKo0fXvkQyvXZoTLF20A/Ji6 TNOm71ijOGs6PPpinJGZA5PIZ/BJyR2oLFRP0bdVPVsfU4uGzEYjn3cHUuYOfSD8cOg+ Ofh7100GKUfD0k4Jj7Ec7rZJey7CH0zdcIPtB6X0oJfX7mfFldQIytjs8TgNILoQTAYd jMR7RX1yTXSi5R8ql7F9AIqaj84AIEo1v392mW32z6k/mJWfsZ9UooT3ZvIhspnKe5Kf l7GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:message-id:date:subject:cc:from :dkim-signature; bh=WTPoqn8CSbIoCMVQ2nER3Fw2FFFBtKsXZetFkbEpLjQ=; b=iWOSgl8mhT23t2MFGigv9juUuBQK2k9j926JftTynY+IlMaVSId3hyliAs7T8pPdpK 96lfMwcbxB89CuCRA6s6CVZFQTUScpjY4j21qb9xrLhNxtenGAZzVhS3IRXaLpWtlahU 6ZtfLhq4rUdYr1X3v5Ss4BjF/xMLctXyDpw7GGYTCwhsQNgBq639e7jFAYgCJ1teVkTQ dzUvn+Xyx0fPHcsoAo/Y4Ess5wIqqo/e7zfY+DeH2kXYUWHzKKTBPlgOH2qq6Q6NYba/ ul4Bhv092NWU96eaV6/BqUyX9J/udyPORjFyBTaNVtYB5L7G10CtBnYvBsSZs3JlOtrd u8nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Fwv33z2R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a19si2362882pjv.47.2019.07.03.09.30.03; Wed, 03 Jul 2019 09:30:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Fwv33z2R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727266AbfGCQ3G (ORCPT + 99 others); Wed, 3 Jul 2019 12:29:06 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:37213 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727076AbfGCQ3F (ORCPT ); Wed, 3 Jul 2019 12:29:05 -0400 Received: by mail-pl1-f193.google.com with SMTP id bh12so1522472plb.4; Wed, 03 Jul 2019 09:29:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=WTPoqn8CSbIoCMVQ2nER3Fw2FFFBtKsXZetFkbEpLjQ=; b=Fwv33z2RbQDYeP/BBRyfjMj18Q/gq19jPWitm//Ck+muhSMn3RePYHZuoMn2fmiofM WOtLoznwgdhF7EYQYOFR6QZYWiSumAHxJPn18a8W2cWbgWn9o7DgzWxBKHAicFV6T4Fe 8yTP3cBaKpba/vLZCM77LR/B9/WjNlk/iKlWP6sC6+fIpTVU2Fke04qsVaIp43IuFi5q S3yAxN3ERbDdPOqAjadQ32k7M5Wh45siiK/rktwDfUzP6TCC1SVhOpRE1RDvG6SFTAEx 25SUZQ841IPDQEKorMEEGwz9Xb9GxX2PMQERAVgAQN2E9OqySyONq2xKtokFKgmPTDuA kkPQ== 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; bh=WTPoqn8CSbIoCMVQ2nER3Fw2FFFBtKsXZetFkbEpLjQ=; b=Hsckt4aYI/oB0VuqT+QqoJgtU1YydWtXaW9J/7PaJnR6d5XGgJrdWtPVrmTTEEPpbj uqGzgS/lF5NPJisbhaxFxclUI0UThsavnmtUT5BZ6K6Q/wHOW+feediC8fVuP4LaTjPG KJuno9DcJp6ABToERZp5X6zGDGmezang4MYcqQPFyRKxS2LgOdtcsZtzstWSh2QsSXvZ r1adxujVjxft9d6TGRPgV0k+/pTWYc3jwyOEQAERNl7QxNkGLNK8cXeFCHEnhSkY+LuU x/92ZYnJ2VxDEE4irbaSu/M7QdTmVLlszjUVqRNqsf5guR0nrCxDcw6QVwqkTissOyeD Kb+Q== X-Gm-Message-State: APjAAAVlfu16GD9J2ObQX8GLzvq2IomBoUSJTFtHFVIN3PJI2+NGeIOH ynu0Kpt3y9NCmv5GsRVqd7k= X-Received: by 2002:a17:902:8c98:: with SMTP id t24mr44631754plo.320.1562171344636; Wed, 03 Jul 2019 09:29:04 -0700 (PDT) Received: from hfq-skylake.ipads-lab.se.sjtu.edu.cn ([202.120.40.82]) by smtp.googlemail.com with ESMTPSA id z186sm3060038pfz.7.2019.07.03.09.29.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Jul 2019 09:29:04 -0700 (PDT) From: Fuqian Huang Cc: Mauro Carvalho Chehab , Antoine Jacquet , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Fuqian Huang Subject: [PATCH v2 13/35] media/usb: Use kmemdup rather than duplicating its implementation Date: Thu, 4 Jul 2019 00:28:57 +0800 Message-Id: <20190703162857.32461-1-huangfq.daxian@gmail.com> X-Mailer: git-send-email 2.11.0 To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org kmemdup is introduced to duplicate a region of memory in a neat way. Rather than kmalloc/kzalloc + memcpy, which the programmer needs to write the size twice (sometimes lead to mistakes), kmemdup improves readability, leads to smaller code and also reduce the chances of mistakes. Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy. Signed-off-by: Fuqian Huang --- Changes in v2: - Fix a typo in commit message (memset -> memcpy) drivers/media/usb/em28xx/em28xx-cards.c | 3 +-- drivers/media/usb/zr364xx/zr364xx.c | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c index 1283c7ca9ad5..6e33782c3ca6 100644 --- a/drivers/media/usb/em28xx/em28xx-cards.c +++ b/drivers/media/usb/em28xx/em28xx-cards.c @@ -3566,13 +3566,12 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, static int em28xx_duplicate_dev(struct em28xx *dev) { int nr; - struct em28xx *sec_dev = kzalloc(sizeof(*sec_dev), GFP_KERNEL); + struct em28xx *sec_dev = kmemdup(dev, sizeof(*sec_dev), GFP_KERNEL); if (!sec_dev) { dev->dev_next = NULL; return -ENOMEM; } - memcpy(sec_dev, dev, sizeof(*sec_dev)); /* Check to see next free device and mark as used */ do { nr = find_first_zero_bit(em28xx_devused, EM28XX_MAXBOARDS); diff --git a/drivers/media/usb/zr364xx/zr364xx.c b/drivers/media/usb/zr364xx/zr364xx.c index 37a7992585df..39df6bfece2c 100644 --- a/drivers/media/usb/zr364xx/zr364xx.c +++ b/drivers/media/usb/zr364xx/zr364xx.c @@ -199,12 +199,10 @@ static int send_control_msg(struct usb_device *udev, u8 request, u16 value, { int status; - unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL); + unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL); if (!transfer_buffer) return -ENOMEM; - memcpy(transfer_buffer, cp, size); - status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), request, -- 2.11.0