1
0
Fork 0
alistair23-linux/drivers/gpu/drm/nouveau
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
..
dispnv04 drm/nouveau: Replace the iturbt_709 prop with the standard COLOR_ENCODING prop 2018-03-08 18:25:04 +02:00
dispnv50 drm/nouveau/kms/gv100: initial support 2018-05-18 15:01:46 +10:00
include drm/nouveau/gr/gv100: initial support 2018-05-18 15:01:47 +10:00
nvif treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
nvkm treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
Kbuild drm/nouveau/kms/nv50-: move code underneath dispnv50/ 2018-05-18 15:01:26 +10:00
Kconfig drm/nouveau/mmu: implement new vmm backend 2017-11-02 13:32:27 +10:00
nouveau_abi16.c drm/nouveau/core: recognise gv100 2018-05-18 15:01:31 +10:00
nouveau_abi16.h main drm pull request for v4.15 2017-11-15 20:42:10 -08:00
nouveau_acpi.c drm/nouveau: fix nouveau_dsm_get_client_id()'s return type 2018-05-18 17:09:46 +10:00
nouveau_acpi.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_backlight.c drm/nouveau/bl: fix backlight regression 2018-03-16 11:55:05 +10:00
nouveau_bios.c drm/nouveau/bios: make const arrays hwsq_signature and edid_sig static 2017-11-03 09:12:10 +10:00
nouveau_bios.h
nouveau_bo.c drm/nouveau/ce/gv100: initial support 2018-05-18 15:01:46 +10:00
nouveau_bo.h drm/nouveau/ttm: don't dereference nvbo::cli, it can outlive client 2018-05-10 13:11:52 +10:00
nouveau_chan.c drm/nouveau/fifo/gv100: initial support 2018-05-18 15:01:46 +10:00
nouveau_chan.h drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nouveau_connector.c drm/nouveau: fix mode_valid's return type 2018-05-18 17:09:35 +10:00
nouveau_connector.h
nouveau_crtc.h drm/nouveau/kms/nv50: prepare for double-buffered LUTs 2018-02-02 15:24:06 +10:00
nouveau_debugfs.c
nouveau_debugfs.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_display.c drm/nouveau/kms: move display class instantiation to library 2018-05-18 15:01:26 +10:00
nouveau_display.h drm/nouveau/kms: move display class instantiation to library 2018-05-18 15:01:26 +10:00
nouveau_dma.c drm/nouveau/fifo/gv100: initial support 2018-05-18 15:01:46 +10:00
nouveau_dma.h drm/nouveau/drm/nv50-: remove allocation of sw class 2018-05-18 15:01:26 +10:00
nouveau_dp.c
nouveau_drm.c drm/nouveau/fifo/gv100: initial support 2018-05-18 15:01:46 +10:00
nouveau_drv.h drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nouveau_encoder.h drm/nouveau/kms/nv50: handle SetControlOutputResource from head 2018-05-18 15:01:28 +10:00
nouveau_fbcon.c drm/nouveau/fbcon: add module parameter to select bits-per-pixel 2018-02-02 15:24:05 +10:00
nouveau_fbcon.h drm/nouveau: Use drm_fb_helper_output_poll_changed() 2017-12-08 13:07:01 +01:00
nouveau_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nouveau_fence.h drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nouveau_gem.c drm/nouveau/gem: tie deferred unmapping of buffers to VMA fence completion 2018-05-18 15:01:26 +10:00
nouveau_gem.h main drm pull request for v4.15 2017-11-15 20:42:10 -08:00
nouveau_hwmon.c drm/nouveau: fix temp/pwm visibility, skip hwmon when no sensors exist 2018-05-18 17:09:48 +10:00
nouveau_hwmon.h
nouveau_ioc32.c
nouveau_ioctl.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_led.c
nouveau_led.h
nouveau_mem.c drm/nouveau: use alternate memory type for system-memory buffers with kind != 0 2017-12-19 10:16:37 +10:00
nouveau_mem.h drm/nouveau: switch over to new memory and vmm interfaces 2017-11-02 13:32:33 +10:00
nouveau_nvif.c
nouveau_platform.c
nouveau_platform.h
nouveau_prime.c
nouveau_reg.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_sgdma.c drm/ttm: add bo as parameter to the ttm_tt_create callback 2018-03-14 14:38:27 -05:00
nouveau_ttm.c drm/nouveau/ttm: don't dereference nvbo::cli, it can outlive client 2018-05-10 13:11:52 +10:00
nouveau_ttm.h drm/ttm: add bo as parameter to the ttm_tt_create callback 2018-03-14 14:38:27 -05:00
nouveau_usif.c
nouveau_usif.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_vga.c drm/nouveau: Use drm_fb_helper_output_poll_changed() 2017-12-08 13:07:01 +01:00
nouveau_vga.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nouveau_vmm.c drm/nouveau/gem: attach fences to VMAs to track GPU usage 2018-05-18 15:01:26 +10:00
nouveau_vmm.h drm/nouveau/gem: attach fences to VMAs to track GPU usage 2018-05-18 15:01:26 +10:00
nv04_fbcon.c
nv04_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nv10_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nv10_fence.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nv17_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nv50_display.h drm/nouveau/kms/nv50-: split each resource type into their own source files 2018-05-18 15:01:28 +10:00
nv50_fbcon.c drm/nouveau: separate constant-va tracking from nvkm vma structure 2017-11-02 13:32:21 +10:00
nv50_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nv84_fence.c drm/nouveau/fifo: support channel count query 2018-05-18 15:01:21 +10:00
nvc0_fbcon.c drm/nouveau: separate constant-va tracking from nvkm vma structure 2017-11-02 13:32:21 +10:00
nvc0_fence.c