Commit d2435a8e authored by Erick Archer's avatar Erick Archer Committed by Tvrtko Ursulin

drm/i915: Add flex arrays to struct i915_syncmap

The "struct i915_syncmap" uses a dynamically sized set of trailing
elements. It can use an "u32" array or a "struct i915_syncmap *"
array.

So, use the preferred way in the kernel declaring flexible arrays [1].
Because there are two possibilities for the trailing arrays, it is
necessary to declare a union and use the DECLARE_FLEX_ARRAY macro.

The comment can be removed as the union is now clear enough.

Also, avoid the open-coded arithmetic in the memory allocator functions
[2] using the "struct_size" macro.

Moreover, refactor the "__sync_seqno" and "__sync_child" functions due
to now it is possible to use the union members added to the structure.
This way, it is also possible to avoid the open-coded arithmetic in
pointers.

Link: https://www.kernel.org/doc/html/next/process/deprecated.html#zero-length-and-one-element-arrays [1]
Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [2]
Signed-off-by: default avatarErick Archer <erick.archer@gmx.com>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarTvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20240208181318.4259-1-erick.archer@gmx.com
parent 4104e634
...@@ -75,13 +75,10 @@ struct i915_syncmap { ...@@ -75,13 +75,10 @@ struct i915_syncmap {
unsigned int height; unsigned int height;
unsigned int bitmap; unsigned int bitmap;
struct i915_syncmap *parent; struct i915_syncmap *parent;
/* union {
* Following this header is an array of either seqno or child pointers: DECLARE_FLEX_ARRAY(u32, seqno);
* union { DECLARE_FLEX_ARRAY(struct i915_syncmap *, child);
* u32 seqno[KSYNCMAP]; };
* struct i915_syncmap *child[KSYNCMAP];
* };
*/
}; };
/** /**
...@@ -99,13 +96,13 @@ void i915_syncmap_init(struct i915_syncmap **root) ...@@ -99,13 +96,13 @@ void i915_syncmap_init(struct i915_syncmap **root)
static inline u32 *__sync_seqno(struct i915_syncmap *p) static inline u32 *__sync_seqno(struct i915_syncmap *p)
{ {
GEM_BUG_ON(p->height); GEM_BUG_ON(p->height);
return (u32 *)(p + 1); return p->seqno;
} }
static inline struct i915_syncmap **__sync_child(struct i915_syncmap *p) static inline struct i915_syncmap **__sync_child(struct i915_syncmap *p)
{ {
GEM_BUG_ON(!p->height); GEM_BUG_ON(!p->height);
return (struct i915_syncmap **)(p + 1); return p->child;
} }
static inline unsigned int static inline unsigned int
...@@ -200,7 +197,7 @@ __sync_alloc_leaf(struct i915_syncmap *parent, u64 id) ...@@ -200,7 +197,7 @@ __sync_alloc_leaf(struct i915_syncmap *parent, u64 id)
{ {
struct i915_syncmap *p; struct i915_syncmap *p;
p = kmalloc(sizeof(*p) + KSYNCMAP * sizeof(u32), GFP_KERNEL); p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
if (unlikely(!p)) if (unlikely(!p))
return NULL; return NULL;
...@@ -282,7 +279,7 @@ static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno) ...@@ -282,7 +279,7 @@ static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno)
unsigned int above; unsigned int above;
/* Insert a join above the current layer */ /* Insert a join above the current layer */
next = kzalloc(sizeof(*next) + KSYNCMAP * sizeof(next), next = kzalloc(struct_size(next, child, KSYNCMAP),
GFP_KERNEL); GFP_KERNEL);
if (unlikely(!next)) if (unlikely(!next))
return -ENOMEM; return -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment