linux-brain/drivers/gpu/drm/nouveau/nvkm/subdev/bios
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
..
base.c drm/nouveau/bios: pointers beyond end of first image need special handling 2016-07-14 11:53:25 +10:00
bit.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
boost.c drm/nouveau/bios/bitP: check that table is long enough for optional pointers 2017-04-29 22:39:23 +10:00
conn.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
cstep.c drm/nouveau/bios/bitP: check that table is long enough for optional pointers 2017-04-29 22:39:23 +10:00
dcb.c drm/nouveau/bios: parse 8.1 Gbps DP link rate 2016-01-11 11:28:22 +10:00
disp.c drm/nouveau/bios/disp: fix handling of "match any protocol" entries 2016-06-02 13:53:30 +10:00
dp.c drm/nouveau/kms/nv50-: determine MST support from DP Info Table 2018-05-18 15:01:29 +10:00
extdev.c drm/nouveau/bios/extdev: also parse v4.1 table 2016-03-14 10:13:26 +10:00
fan.c drm/nouveau/bios/bitP: check that table is long enough for optional pointers 2017-04-29 22:39:23 +10:00
gpio.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
i2c.c drm/nouveau/i2c: transition pad/ports away from being based on nvkm_object 2015-08-28 12:40:29 +10:00
iccsense.c treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
image.c drm/nouveau/bios: pointers beyond end of first image need special handling 2016-07-14 11:53:25 +10:00
init.c drm/nouveau/bios/init: use ARRAY_SIZE 2017-11-02 13:32:15 +10:00
Kbuild drm/nouveau/bios/power_budget: Add basic power budget parsing 2017-02-17 17:38:16 +10:00
M0203.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
M0205.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
M0209.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
mxm.c drm/nouveau/bios/mxm: handle digital connector table 1.1 2016-12-06 09:08:23 +10:00
npde.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
P0260.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
pcir.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00
perf.c drm/nouveau/bios/perf: pointers are 32-bit 2016-11-28 15:39:35 +10:00
pll.c drm/nouveau/bios/pll: limits table 5.0 2018-05-18 15:01:31 +10:00
pmu.c drm/nouveau/bios: pointers beyond end of first image need special handling 2016-07-14 11:53:25 +10:00
power_budget.c drm/nouveau/bios/bitP: check that table is long enough for optional pointers 2017-04-29 22:39:23 +10:00
priv.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
ramcfg.c drm/nouveau/bios: convert to new-style nvkm_subdev 2015-08-28 12:40:42 +10:00
rammap.c drm/nouveau/bios/rammap: 32-bit bios pointers 2016-07-14 11:53:25 +10:00
shadow.c drm/nouveau/bios: require checksum to match for fast acpi shadow method 2016-11-07 14:03:36 +10:00
shadowacpi.c drm/nouveau/bios: require checksum to match for fast acpi shadow method 2016-11-07 14:03:36 +10:00
shadowof.c drm/nouveau/bios: fix OF loading 2015-10-12 13:54:56 +10:00
shadowpci.c drm/nouveau/device: remove pci/platform_device from common struct 2015-08-28 12:40:49 +10:00
shadowramin.c drm/nouveau/bios/gv100: initial support 2018-05-18 15:01:31 +10:00
shadowrom.c drm/nouveau/pci: new subdev 2015-08-28 12:40:48 +10:00
therm.c drm/nouveau/bios/therm: pointers are 32-bit 2016-11-28 15:39:35 +10:00
timing.c drm/nouveau/bios/timing: mark expected switch fall-throughs 2017-11-03 09:12:10 +10:00
vmap.c drm/nouveau/bios/vmap: pointers are 32-bit 2016-11-28 15:39:35 +10:00
volt.c drm/nouveau/bios/volt: Parse min and max for Version 0x40 2017-06-16 14:04:19 +10:00
vpstate.c drm/nouveau/bios/bitP: check that table is long enough for optional pointers 2017-04-29 22:39:23 +10:00
xpio.c drm/nouveau/bios: remove object accessor functions 2015-08-28 12:40:26 +10:00