linux-sg2042/sound/soc
Kees Cook 6da2ec5605 treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:

        kmalloc(a * b, gfp)

with:
        kmalloc_array(a * b, gfp)

as well as handling cases of:

        kmalloc(a * b * c, gfp)

with:

        kmalloc(array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        kmalloc_array(array_size(a, b), c, gfp)

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

        kmalloc(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 tools/ directory was manually excluded, since it has its own
implementation of kmalloc().

The Coccinelle script used for this was:

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

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

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

(
  kmalloc(
-	sizeof(u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	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;
@@

(
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

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

- kmalloc
+ kmalloc_array
  (
-	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;
@@

(
  kmalloc(
-	sizeof(TYPE) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	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;
@@

(
  kmalloc(
-	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	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;
@@

(
  kmalloc(
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	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;
@@

(
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	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;
@@

(
  kmalloc(sizeof(THING) * C2, ...)
|
  kmalloc(sizeof(TYPE) * C2, ...)
|
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * E2
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
adi
amd ASoC: AMD: make channel 1 dma as circular 2018-05-29 14:56:21 +01:00
atmel ASoC: atmel-i2s: add driver for the new Atmel I2S controller 2018-05-29 15:37:21 +01:00
au1x ASoC: au1x: dma: replace platform to component 2018-02-12 11:45:36 +00:00
bcm ASoC: Remove depends on HAS_DMA in case of platform dependency 2018-04-18 11:17:09 +01:00
cirrus ASoC: cirrus: i2s: IRQ-based stream watchdog 2018-05-11 11:27:33 +09:00
codecs treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
davinci ASoC: davinci-mcasp: Only disable inactive serializer 2018-05-17 15:46:22 +09:00
dwc Merge remote-tracking branches 'asoc/topic/dwc', 'asoc/topic/es7134', 'asoc/topic/es8316', 'asoc/topic/es8328' and 'asoc/topic/fsl' into asoc-next 2018-03-28 10:29:36 +08:00
fsl ASoC: Updates for v4.18 2018-06-05 16:51:55 +02:00
generic ASoC: simple-card: set cpu dai clk in hw_params 2018-06-01 11:27:26 +01:00
hisilicon ASoC: hisilicon: fix spelling mistake: "uknown" -> "unknown" 2018-05-13 10:08:11 +09:00
img ASoC: pistachio: replace codec to component 2018-02-12 11:08:03 +00:00
intel Merge branch 'asoc-4.17' into asoc-4.18 merge window 2018-06-05 15:25:41 +01:00
jz4740
kirkwood ASoC: Remove depends on HAS_DMA in case of platform dependency 2018-04-18 11:17:09 +01:00
mediatek ASoC: mt6797-mt6351: add hostless phone call path 2018-05-31 11:25:12 +01:00
mxs Merge remote-tracking branches 'asoc/topic/mtk', 'asoc/topic/mxs', 'asoc/topic/mxs-sgtl5000', 'asoc/topic/nau8540' and 'asoc/topic/nau8824' into asoc-next 2018-01-26 15:27:22 +00:00
nuc900 ASoC: nuc900-pcm: replace platform to component 2018-02-12 11:45:37 +00:00
omap ASoC: omap: fix compile-test building 2018-05-25 18:34:46 +01:00
pxa ASoC: pxa-ssp: simplify pxa_ssp_set_dai_sysclk() 2018-05-22 11:57:17 +01:00
qcom - Introduce arithmetic overflow test helper functions (Rasmus) 2018-06-06 17:27:14 -07:00
rockchip ASoC: rockchip: cdn-dp sound output use spdif 2018-05-23 09:37:56 +01:00
samsung Merge remote-tracking branches 'asoc/topic/wm8991', 'asoc/topic/wm8994', 'asoc/topic/wm8995', 'asoc/topic/wm8996' and 'asoc/topic/wm9081' into asoc-next 2018-03-28 10:31:54 +08:00
sh ASoC: sh: Update menu title and platform dependency 2018-04-20 17:52:49 +01:00
sirf ASoC: sirf: remove duplicated bit-wise or of USP_RXFIFO_THD_INT 2018-02-26 10:19:20 +00:00
spear
sti
stm ASoC: stm32: sai: Add support of S/PDIF playback 2018-02-26 11:19:04 +00:00
sunxi Merge remote-tracking branches 'asoc/topic/sta529', 'asoc/topic/sti-sas', 'asoc/topic/stm32', 'asoc/topic/sun4i' and 'asoc/topic/sun8i' into asoc-next 2018-03-28 10:30:49 +08:00
tegra ASoC: wm8903: replace codec to component 2018-02-12 09:41:37 +00:00
txx9 ASoC: txx9aclc: replace platform to component 2018-02-12 11:45:38 +00:00
uniphier ASoC: uniphier: add digital output volume for UniPhier sound system 2018-05-09 18:40:39 +09:00
ux500 ASoC: ux500: add MODULE_LICENSE tag 2018-01-10 16:42:10 +00:00
xtensa ASoC: xtfpga-i2s: replace platform to component 2018-02-12 11:45:32 +00:00
zte ASoC: zte: remove duplicate includes 2018-05-17 15:46:22 +09:00
Kconfig ASoC: remove blackfin drivers 2018-03-14 09:35:29 -07:00
Makefile ASoC: remove codec reg_cache 2018-05-09 18:38:23 +09:00
soc-ac97.c ASoC: ac97: replace codec to component 2018-02-12 09:37:29 +00:00
soc-acpi.c ASoC: Intel - Convert to use acpi_dev_get_first_match_name() 2018-01-12 21:20:29 +00:00
soc-compress.c ASoC: compress: Fix up some trivial formatting issues 2018-05-11 12:24:31 +09:00
soc-core.c ASoC: soc-core: remove legacy_dai_naming from snd_soc_register_dais() 2018-05-09 18:40:09 +09:00
soc-dapm.c - Introduce arithmetic overflow test helper functions (Rasmus) 2018-06-06 17:27:14 -07:00
soc-devres.c ASoC: Remove platform code now everything is componentised 2018-04-26 12:24:43 +01:00
soc-generic-dmaengine-pcm.c ASoC: Revert "ASoC: soc-generic-dmaengine-pcm: Fix error handling" 2018-02-26 19:59:31 +00:00
soc-io.c ASoC: remove Codec related code 2018-05-09 18:39:07 +09:00
soc-jack.c ASoC: remove Codec related code 2018-05-09 18:39:07 +09:00
soc-ops.c ASoC: change mask in snd_soc_get/put_volsw_sx to unsigned int 2018-01-03 12:32:04 +00:00
soc-pcm.c ASoC: core: Fix return code shown on error for hw_params 2018-05-30 16:34:28 +01:00
soc-topology.c ASoC: topology: Move v4 manifest header data structures to uapi 2018-06-01 18:09:13 +01:00
soc-utils.c ASoC: soc-utils: replace codec to component 2018-02-12 11:46:14 +00:00