linux_dsm_epyc7002/drivers/gpu/drm/nouveau/nvkm/engine/pm
Kees Cook acafe7e302 treewide: Use struct_size() for kmalloc()-family
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    void *entry[];
};

instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);

This patch makes the changes for kmalloc()-family (and kvmalloc()-family)
uses. It was done via automatic conversion with manual review for the
"CHECKME" non-standard cases noted below, using the following Coccinelle
script:

// pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
//                      sizeof *pkey_cache->table, GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

- alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-06 11:15:43 -07:00
..
base.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
g84.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
gf100.c drm/nouveau: silence sparse warnings about symbols not being marked static 2016-11-07 14:04:40 +10:00
gf100.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
gf108.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
gf117.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
gk104.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
gt200.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
gt215.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
Kbuild drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
nv40.c nouveau: fix nv40_perfctr_next() cleanup regression 2016-03-16 15:08:43 +10:00
nv40.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nv50.c drm/nouveau/pm: convert to new-style nvkm_engine 2015-08-28 12:40:47 +10:00
priv.h main drm pull request for v4.15 2017-11-15 20:42:10 -08:00