treewide: devm_kmalloc() -> devm_kmalloc_array()
authorKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 21:04:57 +0000 (14:04 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 23:19:22 +0000 (16:19 -0700)
The devm_kmalloc() function has a 2-factor argument form,
devm_kmalloc_array(). This patch replaces cases of:

        devm_kmalloc(handle, a * b, gfp)

with:
        devm_kmalloc_array(handle, a * b, gfp)

as well as handling cases of:

        devm_kmalloc(handle, a * b * c, gfp)

with:

        devm_kmalloc(handle, array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        devm_kmalloc_array(handle, array_size(a, b), c, gfp)

This does, however, attempt to ignore constant size factors like:

        devm_kmalloc(handle, 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.

Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kmalloc..." instead of "= devm_kmalloc...".

The Coccinelle script used for this was:

// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@

(
  devm_kmalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
  , ...)
|
  devm_kmalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
  , ...)
)

// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@

(
  devm_kmalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
  , ...)
)

// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@

(
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
  , ...)
)

// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@

- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
  , ...)

// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@

(
  devm_kmalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
)

// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@

(
  devm_kmalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  devm_kmalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
)

// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@

(
  devm_kmalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kmalloc(HANDLE,
- 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 HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  devm_kmalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kmalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
  , ...)
|
  devm_kmalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
  , ...)
|
  devm_kmalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
  , ...)
|
  devm_kmalloc(HANDLE,
- 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 HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  devm_kmalloc(HANDLE, sizeof(THING) * C2, ...)
|
  devm_kmalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
  devm_kmalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kmalloc(HANDLE, C1 * C2, ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- (E1) * E2
+ E1, E2
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- (E1) * (E2)
+ E1, E2
  , ...)
|
- devm_kmalloc
+ devm_kmalloc_array
  (HANDLE,
- E1 * E2
+ E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
drivers/iio/adc/max1027.c
drivers/nfc/fdp/i2c.c
drivers/pinctrl/freescale/pinctrl-imx.c
drivers/pinctrl/mvebu/pinctrl-armada-xp.c
drivers/regulator/s2mps11.c

index 375da6491499faca088a0e03f4fa684b6bf14f8b..311c1a89c329eb5599421508e43a7e3625cb821f 100644 (file)
@@ -422,8 +422,8 @@ static int max1027_probe(struct spi_device *spi)
        indio_dev->num_channels = st->info->num_channels;
        indio_dev->available_scan_masks = st->info->available_scan_masks;
 
-       st->buffer = devm_kmalloc(&indio_dev->dev,
-                                 indio_dev->num_channels * 2,
+       st->buffer = devm_kmalloc_array(&indio_dev->dev,
+                                 indio_dev->num_channels, 2,
                                  GFP_KERNEL);
        if (st->buffer == NULL) {
                dev_err(&indio_dev->dev, "Can't allocate buffer\n");
index c4da50e07bbcbe2318a27f55c5e49baf5f3c077b..d8d70dd830b07fe6254e1e5d52e12045a4ea64f4 100644 (file)
@@ -259,8 +259,8 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
                /* Add 1 to the length to inclue the length byte itself */
                len++;
 
-               *fw_vsc_cfg = devm_kmalloc(dev,
-                                          len * sizeof(**fw_vsc_cfg),
+               *fw_vsc_cfg = devm_kmalloc_array(dev,
+                                          len, sizeof(**fw_vsc_cfg),
                                           GFP_KERNEL);
 
                r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
index 844537681fd72d77b665bc481beb9990e741481c..28e5b7f620443dd8d55aa04b90c535bee6f7aa14 100644 (file)
@@ -698,8 +698,9 @@ int imx_pinctrl_probe(struct platform_device *pdev,
        if (!ipctl)
                return -ENOMEM;
 
-       ipctl->pin_regs = devm_kmalloc(&pdev->dev, sizeof(*ipctl->pin_regs) *
-                                     info->npins, GFP_KERNEL);
+       ipctl->pin_regs = devm_kmalloc_array(&pdev->dev,
+                                      info->npins, sizeof(*ipctl->pin_regs),
+                                      GFP_KERNEL);
        if (!ipctl->pin_regs)
                return -ENOMEM;
 
index 5e828468e43da2243582da0b8c52cc3557f9beb0..43231fd065a186b4bf0729fff828f9e368be29e4 100644 (file)
@@ -630,8 +630,8 @@ static int armada_xp_pinctrl_probe(struct platform_device *pdev)
 
        nregs = DIV_ROUND_UP(soc->nmodes, MVEBU_MPPS_PER_REG);
 
-       mpp_saved_regs = devm_kmalloc(&pdev->dev, nregs * sizeof(u32),
-                                     GFP_KERNEL);
+       mpp_saved_regs = devm_kmalloc_array(&pdev->dev, nregs, sizeof(u32),
+                                           GFP_KERNEL);
        if (!mpp_saved_regs)
                return -ENOMEM;
 
index b4e588cce03dee1ec64ee577fb5886e724d1181a..d1207ec683db22e72c4811a04f2d9529b7a65906 100644 (file)
@@ -1139,8 +1139,8 @@ static int s2mps11_pmic_probe(struct platform_device *pdev)
                return -EINVAL;
        }
 
-       s2mps11->ext_control_gpio = devm_kmalloc(&pdev->dev,
-                       sizeof(*s2mps11->ext_control_gpio) * rdev_num,
+       s2mps11->ext_control_gpio = devm_kmalloc_array(&pdev->dev,
+                       rdev_num, sizeof(*s2mps11->ext_control_gpio),
                        GFP_KERNEL);
        if (!s2mps11->ext_control_gpio)
                return -ENOMEM;