summaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2018-06-12 17:03:40 -0400
committerKees Cook <keescook@chromium.org>2018-06-12 19:19:22 -0400
commit6396bb221514d2876fd6dc0aa2a1f240d99b37bb (patch)
treec5c501e859b93de096b1f01160135612ed765059 /drivers/usb
parent6da2ec56059c3c7a7e5f729e6349e74ace1e5c57 (diff)
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This patch replaces cases of: kzalloc(a * b, gfp) with: kcalloc(a * b, gfp) as well as handling cases of: kzalloc(a * b * c, gfp) with: kzalloc(array3_size(a, b, c), gfp) as it's slightly less ugly than: kzalloc_array(array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: kzalloc(4 * 1024, gfp) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ type TYPE; expression THING, E; @@ ( kzalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | kzalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( kzalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(char) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(u8) * COUNT + COUNT , ...) | kzalloc( - sizeof(__u8) * COUNT + COUNT , ...) | kzalloc( - sizeof(char) * COUNT + COUNT , ...) | kzalloc( - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - kzalloc + kcalloc ( - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ - kzalloc + kcalloc ( - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( kzalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( kzalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kzalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kzalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kzalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | kzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ identifier STRIDE, SIZE, COUNT; @@ ( kzalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products, // when they're not all constants... @@ expression E1, E2, E3; constant C1, C2, C3; @@ ( kzalloc(C1 * C2 * C3, ...) | kzalloc( - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | kzalloc( - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | kzalloc( - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | kzalloc( - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants, // keeping sizeof() as the second factor argument. @@ expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( kzalloc(sizeof(THING) * C2, ...) | kzalloc(sizeof(TYPE) * C2, ...) | kzalloc(C1 * C2 * C3, ...) | kzalloc(C1 * C2, ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - kzalloc + kcalloc ( - (E1) * E2 + E1, E2 , ...) | - kzalloc + kcalloc ( - (E1) * (E2) + E1, E2 , ...) | - kzalloc + kcalloc ( - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/core/hub.c2
-rw-r--r--drivers/usb/dwc2/hcd.c11
-rw-r--r--drivers/usb/gadget/udc/bdc/bdc_ep.c6
-rw-r--r--drivers/usb/gadget/udc/fsl_udc_core.c2
-rw-r--r--drivers/usb/host/ehci-sched.c5
-rw-r--r--drivers/usb/host/imx21-hcd.c4
-rw-r--r--drivers/usb/mon/mon_bin.c3
-rw-r--r--drivers/usb/renesas_usbhs/mod_gadget.c2
-rw-r--r--drivers/usb/renesas_usbhs/pipe.c3
-rw-r--r--drivers/usb/wusbcore/wa-rpipe.c3
10 files changed, 23 insertions, 18 deletions
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 26c2438d2889..fcae521df29b 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1376,7 +1376,7 @@ static int hub_configure(struct usb_hub *hub,
1376 dev_info(hub_dev, "%d port%s detected\n", maxchild, 1376 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1377 (maxchild == 1) ? "" : "s"); 1377 (maxchild == 1) ? "" : "s");
1378 1378
1379 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL); 1379 hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL);
1380 if (!hub->ports) { 1380 if (!hub->ports) {
1381 ret = -ENOMEM; 1381 ret = -ENOMEM;
1382 goto fail; 1382 goto fail;
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 1faefea16cec..edaf0b6af4f0 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -5079,13 +5079,14 @@ int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5079 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg); 5079 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5080 5080
5081#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 5081#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5082 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) * 5082 hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
5083 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL); 5083 sizeof(*hsotg->frame_num_array),
5084 GFP_KERNEL);
5084 if (!hsotg->frame_num_array) 5085 if (!hsotg->frame_num_array)
5085 goto error1; 5086 goto error1;
5086 hsotg->last_frame_num_array = kzalloc( 5087 hsotg->last_frame_num_array =
5087 sizeof(*hsotg->last_frame_num_array) * 5088 kcalloc(FRAME_NUM_ARRAY_SIZE,
5088 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL); 5089 sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5089 if (!hsotg->last_frame_num_array) 5090 if (!hsotg->last_frame_num_array)
5090 goto error1; 5091 goto error1;
5091#endif 5092#endif
diff --git a/drivers/usb/gadget/udc/bdc/bdc_ep.c b/drivers/usb/gadget/udc/bdc/bdc_ep.c
index 03149b9d7ea7..a4d9b5e1e50e 100644
--- a/drivers/usb/gadget/udc/bdc/bdc_ep.c
+++ b/drivers/usb/gadget/udc/bdc/bdc_ep.c
@@ -138,9 +138,9 @@ static int ep_bd_list_alloc(struct bdc_ep *ep)
138 __func__, ep, num_tabs); 138 __func__, ep, num_tabs);
139 139
140 /* Allocate memory for table array */ 140 /* Allocate memory for table array */
141 ep->bd_list.bd_table_array = kzalloc( 141 ep->bd_list.bd_table_array = kcalloc(num_tabs,
142 num_tabs * sizeof(struct bd_table *), 142 sizeof(struct bd_table *),
143 GFP_ATOMIC); 143 GFP_ATOMIC);
144 if (!ep->bd_list.bd_table_array) 144 if (!ep->bd_list.bd_table_array)
145 return -ENOMEM; 145 return -ENOMEM;
146 146
diff --git a/drivers/usb/gadget/udc/fsl_udc_core.c b/drivers/usb/gadget/udc/fsl_udc_core.c
index 9a3f7db26a5e..be59309e848c 100644
--- a/drivers/usb/gadget/udc/fsl_udc_core.c
+++ b/drivers/usb/gadget/udc/fsl_udc_core.c
@@ -2246,7 +2246,7 @@ static int struct_udc_setup(struct fsl_udc *udc,
2246 pdata = dev_get_platdata(&pdev->dev); 2246 pdata = dev_get_platdata(&pdev->dev);
2247 udc->phy_mode = pdata->phy_mode; 2247 udc->phy_mode = pdata->phy_mode;
2248 2248
2249 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL); 2249 udc->eps = kcalloc(udc->max_ep, sizeof(struct fsl_ep), GFP_KERNEL);
2250 if (!udc->eps) 2250 if (!udc->eps)
2251 return -1; 2251 return -1;
2252 2252
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index e56db44708bc..1d87295682b8 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -117,8 +117,9 @@ static struct ehci_tt *find_tt(struct usb_device *udev)
117 if (utt->multi) { 117 if (utt->multi) {
118 tt_index = utt->hcpriv; 118 tt_index = utt->hcpriv;
119 if (!tt_index) { /* Create the index array */ 119 if (!tt_index) { /* Create the index array */
120 tt_index = kzalloc(utt->hub->maxchild * 120 tt_index = kcalloc(utt->hub->maxchild,
121 sizeof(*tt_index), GFP_ATOMIC); 121 sizeof(*tt_index),
122 GFP_ATOMIC);
122 if (!tt_index) 123 if (!tt_index)
123 return ERR_PTR(-ENOMEM); 124 return ERR_PTR(-ENOMEM);
124 utt->hcpriv = tt_index; 125 utt->hcpriv = tt_index;
diff --git a/drivers/usb/host/imx21-hcd.c b/drivers/usb/host/imx21-hcd.c
index 3a8bbfe43a8e..6e3dad19d369 100644
--- a/drivers/usb/host/imx21-hcd.c
+++ b/drivers/usb/host/imx21-hcd.c
@@ -741,8 +741,8 @@ static int imx21_hc_urb_enqueue_isoc(struct usb_hcd *hcd,
741 if (urb_priv == NULL) 741 if (urb_priv == NULL)
742 return -ENOMEM; 742 return -ENOMEM;
743 743
744 urb_priv->isoc_td = kzalloc( 744 urb_priv->isoc_td = kcalloc(urb->number_of_packets, sizeof(struct td),
745 sizeof(struct td) * urb->number_of_packets, mem_flags); 745 mem_flags);
746 if (urb_priv->isoc_td == NULL) { 746 if (urb_priv->isoc_td == NULL) {
747 ret = -ENOMEM; 747 ret = -ENOMEM;
748 goto alloc_td_failed; 748 goto alloc_td_failed;
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index 34e866ad4a81..ad2c082bd0fb 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -1024,7 +1024,8 @@ static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg
1024 return -EINVAL; 1024 return -EINVAL;
1025 1025
1026 size = CHUNK_ALIGN(arg); 1026 size = CHUNK_ALIGN(arg);
1027 vec = kzalloc(sizeof(struct mon_pgmap) * (size / CHUNK_SIZE), GFP_KERNEL); 1027 vec = kcalloc(size / CHUNK_SIZE, sizeof(struct mon_pgmap),
1028 GFP_KERNEL);
1028 if (vec == NULL) { 1029 if (vec == NULL) {
1029 ret = -ENOMEM; 1030 ret = -ENOMEM;
1030 break; 1031 break;
diff --git a/drivers/usb/renesas_usbhs/mod_gadget.c b/drivers/usb/renesas_usbhs/mod_gadget.c
index 34ee9ebe12a3..33d059c40616 100644
--- a/drivers/usb/renesas_usbhs/mod_gadget.c
+++ b/drivers/usb/renesas_usbhs/mod_gadget.c
@@ -1068,7 +1068,7 @@ int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1068 if (!gpriv) 1068 if (!gpriv)
1069 return -ENOMEM; 1069 return -ENOMEM;
1070 1070
1071 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); 1071 uep = kcalloc(pipe_size, sizeof(struct usbhsg_uep), GFP_KERNEL);
1072 if (!uep) { 1072 if (!uep) {
1073 ret = -ENOMEM; 1073 ret = -ENOMEM;
1074 goto usbhs_mod_gadget_probe_err_gpriv; 1074 goto usbhs_mod_gadget_probe_err_gpriv;
diff --git a/drivers/usb/renesas_usbhs/pipe.c b/drivers/usb/renesas_usbhs/pipe.c
index 9677e0e31475..c4922b96c93b 100644
--- a/drivers/usb/renesas_usbhs/pipe.c
+++ b/drivers/usb/renesas_usbhs/pipe.c
@@ -803,7 +803,8 @@ int usbhs_pipe_probe(struct usbhs_priv *priv)
803 return -EINVAL; 803 return -EINVAL;
804 } 804 }
805 805
806 info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL); 806 info->pipe = kcalloc(pipe_size, sizeof(struct usbhs_pipe),
807 GFP_KERNEL);
807 if (!info->pipe) 808 if (!info->pipe)
808 return -ENOMEM; 809 return -ENOMEM;
809 810
diff --git a/drivers/usb/wusbcore/wa-rpipe.c b/drivers/usb/wusbcore/wa-rpipe.c
index d0f1a6698460..38884aac862b 100644
--- a/drivers/usb/wusbcore/wa-rpipe.c
+++ b/drivers/usb/wusbcore/wa-rpipe.c
@@ -470,7 +470,8 @@ error:
470int wa_rpipes_create(struct wahc *wa) 470int wa_rpipes_create(struct wahc *wa)
471{ 471{
472 wa->rpipes = le16_to_cpu(wa->wa_descr->wNumRPipes); 472 wa->rpipes = le16_to_cpu(wa->wa_descr->wNumRPipes);
473 wa->rpipe_bm = kzalloc(BITS_TO_LONGS(wa->rpipes)*sizeof(unsigned long), 473 wa->rpipe_bm = kcalloc(BITS_TO_LONGS(wa->rpipes),
474 sizeof(unsigned long),
474 GFP_KERNEL); 475 GFP_KERNEL);
475 if (wa->rpipe_bm == NULL) 476 if (wa->rpipe_bm == NULL)
476 return -ENOMEM; 477 return -ENOMEM;