Skip to content

Commit d2435a8

Browse files
Erick Archertursulin
authored andcommitted
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: Erick Archer <erick.archer@gmx.com> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20240208181318.4259-1-erick.archer@gmx.com
1 parent 4104e63 commit d2435a8

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

drivers/gpu/drm/i915/i915_syncmap.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,10 @@ struct i915_syncmap {
7575
unsigned int height;
7676
unsigned int bitmap;
7777
struct i915_syncmap *parent;
78-
/*
79-
* Following this header is an array of either seqno or child pointers:
80-
* union {
81-
* u32 seqno[KSYNCMAP];
82-
* struct i915_syncmap *child[KSYNCMAP];
83-
* };
84-
*/
78+
union {
79+
DECLARE_FLEX_ARRAY(u32, seqno);
80+
DECLARE_FLEX_ARRAY(struct i915_syncmap *, child);
81+
};
8582
};
8683

8784
/**
@@ -99,13 +96,13 @@ void i915_syncmap_init(struct i915_syncmap **root)
9996
static inline u32 *__sync_seqno(struct i915_syncmap *p)
10097
{
10198
GEM_BUG_ON(p->height);
102-
return (u32 *)(p + 1);
99+
return p->seqno;
103100
}
104101

105102
static inline struct i915_syncmap **__sync_child(struct i915_syncmap *p)
106103
{
107104
GEM_BUG_ON(!p->height);
108-
return (struct i915_syncmap **)(p + 1);
105+
return p->child;
109106
}
110107

111108
static inline unsigned int
@@ -200,7 +197,7 @@ __sync_alloc_leaf(struct i915_syncmap *parent, u64 id)
200197
{
201198
struct i915_syncmap *p;
202199

203-
p = kmalloc(sizeof(*p) + KSYNCMAP * sizeof(u32), GFP_KERNEL);
200+
p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
204201
if (unlikely(!p))
205202
return NULL;
206203

@@ -282,7 +279,7 @@ static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno)
282279
unsigned int above;
283280

284281
/* Insert a join above the current layer */
285-
next = kzalloc(sizeof(*next) + KSYNCMAP * sizeof(next),
282+
next = kzalloc(struct_size(next, child, KSYNCMAP),
286283
GFP_KERNEL);
287284
if (unlikely(!next))
288285
return -ENOMEM;

0 commit comments

Comments
 (0)