From: Julia Lawall Date: Sat, 15 May 2010 21:21:43 +0000 (+0200) Subject: Staging: Use kmemdup X-Git-Tag: firefly_0821_release~9833^2~2055^2^2~23 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=94002c07ff0e207a883519ccc35c0b5390b29331;p=firefly-linux-kernel-4.4.55.git Staging: Use kmemdup Use kmemdup when some other buffer is immediately copied into the allocated region. A simplified version of the semantic patch that makes this change is as follows: (http://coccinelle.lip6.fr/) // @@ expression from,to,size,flag; statement S; @@ - to = \(kmalloc\|kzalloc\)(size,flag); + to = kmemdup(from,size,flag); if (to==NULL || ...) S - memcpy(to, from, size); // Signed-off-by: Julia Lawall Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/staging/comedi/drivers/usbdux.c b/drivers/staging/comedi/drivers/usbdux.c index df71515c7a30..86f035d00675 100644 --- a/drivers/staging/comedi/drivers/usbdux.c +++ b/drivers/staging/comedi/drivers/usbdux.c @@ -840,13 +840,12 @@ static int firmwareUpload(struct usbduxsub *usbduxsub, } /* we generate a local buffer for the firmware */ - fwBuf = kzalloc(sizeFirmware, GFP_KERNEL); + fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL); if (!fwBuf) { dev_err(&usbduxsub->interface->dev, "comedi_: mem alloc for firmware failed\n"); return -ENOMEM; } - memcpy(fwBuf, firmwareBinary, sizeFirmware); ret = usbduxsub_stop(usbduxsub); if (ret < 0) { diff --git a/drivers/staging/comedi/drivers/usbduxfast.c b/drivers/staging/comedi/drivers/usbduxfast.c index 0d72c416e73b..29c3c016b93a 100644 --- a/drivers/staging/comedi/drivers/usbduxfast.c +++ b/drivers/staging/comedi/drivers/usbduxfast.c @@ -1368,13 +1368,12 @@ static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub, } /* we generate a local buffer for the firmware */ - fwBuf = kzalloc(sizeFirmware, GFP_KERNEL); + fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL); if (!fwBuf) { dev_err(&usbduxfastsub->interface->dev, "comedi_: mem alloc for firmware failed\n"); return -ENOMEM; } - memcpy(fwBuf, firmwareBinary, sizeFirmware); ret = usbduxfastsub_stop(usbduxfastsub); if (ret < 0) { diff --git a/drivers/staging/hv/vmbus.c b/drivers/staging/hv/vmbus.c index 0a9d8459db0c..007543bdb410 100644 --- a/drivers/staging/hv/vmbus.c +++ b/drivers/staging/hv/vmbus.c @@ -185,11 +185,10 @@ static void VmbusOnMsgDPC(struct hv_driver *drv) /* no msg */ break; } else { - copied = kmalloc(sizeof(*copied), GFP_ATOMIC); + copied = kmemdup(msg, sizeof(*copied), GFP_ATOMIC); if (copied == NULL) continue; - memcpy(copied, msg, sizeof(*copied)); osd_schedule_callback(gVmbusConnection.WorkQueue, VmbusOnChannelMessage, (void *)copied); diff --git a/drivers/staging/line6/dumprequest.c b/drivers/staging/line6/dumprequest.c index bb8c9da5803f..cd468c39da5c 100644 --- a/drivers/staging/line6/dumprequest.c +++ b/drivers/staging/line6/dumprequest.c @@ -105,10 +105,9 @@ int line6_wait_dump(struct line6_dump_request *l6dr, int nonblock) int line6_dumpreq_initbuf(struct line6_dump_request *l6dr, const void *buf, size_t len, int num) { - l6dr->reqbufs[num].buffer = kmalloc(len, GFP_KERNEL); + l6dr->reqbufs[num].buffer = kmemdup(buf, len, GFP_KERNEL); if (l6dr->reqbufs[num].buffer == NULL) return -ENOMEM; - memcpy(l6dr->reqbufs[num].buffer, buf, len); l6dr->reqbufs[num].length = len; return 0; } diff --git a/drivers/staging/line6/pod.c b/drivers/staging/line6/pod.c index 4983f2b51cf2..28f514611abc 100644 --- a/drivers/staging/line6/pod.c +++ b/drivers/staging/line6/pod.c @@ -1074,7 +1074,8 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod) return -ENOMEM; } - pod->buffer_versionreq = kmalloc(sizeof(pod_request_version), + pod->buffer_versionreq = kmemdup(pod_request_version, + sizeof(pod_request_version), GFP_KERNEL); if (pod->buffer_versionreq == NULL) { @@ -1083,9 +1084,6 @@ int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod) return -ENOMEM; } - memcpy(pod->buffer_versionreq, pod_request_version, - sizeof(pod_request_version)); - /* create sysfs entries: */ err = pod_create_files2(&interface->dev); if (err < 0) { diff --git a/drivers/staging/line6/variax.c b/drivers/staging/line6/variax.c index 28eb89983f36..58ddbe6393ff 100644 --- a/drivers/staging/line6/variax.c +++ b/drivers/staging/line6/variax.c @@ -486,7 +486,8 @@ int variax_init(struct usb_interface *interface, return err; } - variax->buffer_activate = kmalloc(sizeof(variax_activate), GFP_KERNEL); + variax->buffer_activate = kmemdup(variax_activate, + sizeof(variax_activate), GFP_KERNEL); if (variax->buffer_activate == NULL) { dev_err(&interface->dev, "Out of memory\n"); @@ -494,8 +495,6 @@ int variax_init(struct usb_interface *interface, return -ENOMEM; } - memcpy(variax->buffer_activate, variax_activate, - sizeof(variax_activate)); init_timer(&variax->activate_timer); /* create sysfs entries: */ diff --git a/drivers/staging/pohmelfs/config.c b/drivers/staging/pohmelfs/config.c index 9fdf2de347ea..8c8d1c282e7e 100644 --- a/drivers/staging/pohmelfs/config.c +++ b/drivers/staging/pohmelfs/config.c @@ -204,18 +204,18 @@ int pohmelfs_copy_crypto(struct pohmelfs_sb *psb) } if (g->hash_keysize) { - psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL); + psb->hash_key = kmemdup(g->hash_key, g->hash_keysize, + GFP_KERNEL); if (!psb->hash_key) goto err_out_free_cipher_string; - memcpy(psb->hash_key, g->hash_key, g->hash_keysize); psb->hash_keysize = g->hash_keysize; } if (g->cipher_keysize) { - psb->cipher_key = kmalloc(g->cipher_keysize, GFP_KERNEL); + psb->cipher_key = kmemdup(g->cipher_key, g->cipher_keysize, + GFP_KERNEL); if (!psb->cipher_key) goto err_out_free_hash; - memcpy(psb->cipher_key, g->cipher_key, g->cipher_keysize); psb->cipher_keysize = g->cipher_keysize; } @@ -454,14 +454,12 @@ static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct poh g->hash_strlen = c->strlen; g->hash_keysize = c->keysize; - g->hash_key = kmalloc(c->keysize, GFP_KERNEL); + g->hash_key = kmemdup(key, c->keysize, GFP_KERNEL); if (!g->hash_key) { kfree(g->hash_string); return -ENOMEM; } - memcpy(g->hash_key, key, c->keysize); - return 0; } @@ -479,14 +477,12 @@ static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct p g->cipher_strlen = c->strlen; g->cipher_keysize = c->keysize; - g->cipher_key = kmalloc(c->keysize, GFP_KERNEL); + g->cipher_key = kmemdup(key, c->keysize, GFP_KERNEL); if (!g->cipher_key) { kfree(g->cipher_string); return -ENOMEM; } - memcpy(g->cipher_key, key, c->keysize); - return 0; } diff --git a/drivers/staging/rtl8187se/ieee80211/ieee80211_softmac.c b/drivers/staging/rtl8187se/ieee80211/ieee80211_softmac.c index b7426fea5498..1b838a266e0d 100644 --- a/drivers/staging/rtl8187se/ieee80211/ieee80211_softmac.c +++ b/drivers/staging/rtl8187se/ieee80211/ieee80211_softmac.c @@ -2667,11 +2667,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, return -EINVAL; if (param->u.wpa_ie.len) { - buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); + buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len, + GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = param->u.wpa_ie.len; diff --git a/drivers/staging/rtl8187se/ieee80211/ieee80211_wx.c b/drivers/staging/rtl8187se/ieee80211/ieee80211_wx.c index aa3ba450ecef..07d8dbcdca28 100644 --- a/drivers/staging/rtl8187se/ieee80211/ieee80211_wx.c +++ b/drivers/staging/rtl8187se/ieee80211/ieee80211_wx.c @@ -727,10 +727,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) printk("len:%zu, ie:%d\n", len, ie[1]); return -EINVAL; } - buf = kmalloc(len, GFP_KERNEL); + buf = kmemdup(ie, len, GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, ie, len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = len; diff --git a/drivers/staging/rtl8192e/ieee80211/ieee80211_softmac.c b/drivers/staging/rtl8192e/ieee80211/ieee80211_softmac.c index 215542a00ef1..46b6e8c900e9 100644 --- a/drivers/staging/rtl8192e/ieee80211/ieee80211_softmac.c +++ b/drivers/staging/rtl8192e/ieee80211/ieee80211_softmac.c @@ -3255,11 +3255,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, return -EINVAL; if (param->u.wpa_ie.len) { - buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); + buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len, + GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = param->u.wpa_ie.len; diff --git a/drivers/staging/rtl8192e/ieee80211/ieee80211_wx.c b/drivers/staging/rtl8192e/ieee80211/ieee80211_wx.c index fcabaf3c88d4..4971b1c8e7d7 100644 --- a/drivers/staging/rtl8192e/ieee80211/ieee80211_wx.c +++ b/drivers/staging/rtl8192e/ieee80211/ieee80211_wx.c @@ -979,10 +979,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) printk("len:%zu, ie:%d\n", len, ie[1]); return -EINVAL; } - buf = kmalloc(len, GFP_KERNEL); + buf = kmemdup(ie, len, GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, ie, len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = len; diff --git a/drivers/staging/rtl8192su/ieee80211/ieee80211_softmac.c b/drivers/staging/rtl8192su/ieee80211/ieee80211_softmac.c index b9fa15fccb78..4f1f2f08b2d7 100644 --- a/drivers/staging/rtl8192su/ieee80211/ieee80211_softmac.c +++ b/drivers/staging/rtl8192su/ieee80211/ieee80211_softmac.c @@ -2844,11 +2844,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, return -EINVAL; if (param->u.wpa_ie.len) { - buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); + buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len, + GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = param->u.wpa_ie.len; diff --git a/drivers/staging/rtl8192su/ieee80211/ieee80211_wx.c b/drivers/staging/rtl8192su/ieee80211/ieee80211_wx.c index db54ad096da0..2ce5bd543eae 100644 --- a/drivers/staging/rtl8192su/ieee80211/ieee80211_wx.c +++ b/drivers/staging/rtl8192su/ieee80211/ieee80211_wx.c @@ -767,10 +767,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) printk("len: %Zd, ie:%d\n", len, ie[1]); return -EINVAL; } - buf = kmalloc(len, GFP_KERNEL); + buf = kmemdup(ie, len, GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, ie, len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = len; diff --git a/drivers/staging/rtl8192u/ieee80211/ieee80211_softmac.c b/drivers/staging/rtl8192u/ieee80211/ieee80211_softmac.c index a6955e2b45c4..a2e84c578579 100644 --- a/drivers/staging/rtl8192u/ieee80211/ieee80211_softmac.c +++ b/drivers/staging/rtl8192u/ieee80211/ieee80211_softmac.c @@ -1579,10 +1579,9 @@ static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen) if(*(t++) == MFIE_TYPE_CHALLENGE){ *chlen = *(t++); - *challenge = kmalloc(*chlen, GFP_ATOMIC); + *challenge = kmemdup(t, *chlen, GFP_ATOMIC); if (!*challenge) return -ENOMEM; - memcpy(*challenge, t, *chlen); } } @@ -2870,11 +2869,11 @@ static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee, return -EINVAL; if (param->u.wpa_ie.len) { - buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL); + buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len, + GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = param->u.wpa_ie.len; diff --git a/drivers/staging/rtl8192u/ieee80211/ieee80211_wx.c b/drivers/staging/rtl8192u/ieee80211/ieee80211_wx.c index 79b180f79e80..fb78ed2876e5 100644 --- a/drivers/staging/rtl8192u/ieee80211/ieee80211_wx.c +++ b/drivers/staging/rtl8192u/ieee80211/ieee80211_wx.c @@ -847,10 +847,9 @@ int ieee80211_wx_set_gen_ie(struct ieee80211_device *ieee, u8 *ie, size_t len) printk("len:%zu, ie:%d\n", len, ie[1]); return -EINVAL; } - buf = kmalloc(len, GFP_KERNEL); + buf = kmemdup(ie, len, GFP_KERNEL); if (buf == NULL) return -ENOMEM; - memcpy(buf, ie, len); kfree(ieee->wpa_ie); ieee->wpa_ie = buf; ieee->wpa_ie_len = len; diff --git a/drivers/staging/usbip/stub_rx.c b/drivers/staging/usbip/stub_rx.c index bc2674086673..feb9fd7a7bb9 100644 --- a/drivers/staging/usbip/stub_rx.c +++ b/drivers/staging/usbip/stub_rx.c @@ -502,13 +502,13 @@ static void stub_recv_cmd_submit(struct stub_device *sdev, } /* set priv->urb->setup_packet */ - priv->urb->setup_packet = kzalloc(8, GFP_KERNEL); + priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8, + GFP_KERNEL); if (!priv->urb->setup_packet) { dev_err(&sdev->interface->dev, "allocate setup_packet\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); return; } - memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8); /* set other members from the base header of pdu */ priv->urb->context = (void *) priv;