drm/i915: Prefer struct_size over open coded arithmetic
Subsystems (2)
PATCH 1/1 drm/i915: Prefer struct_size over open coded arithmetic
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct i915_syncmap a union with two flexible array members (these arrays share the same memory layout). This is possible using the new DECLARE_FLEX_ARRAY macro. And then, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc and kzalloc() functions.
Also, take the opportunity to refactor the __sync_seqno and __sync_child making them more readable.
This code was detected with the help of Coccinelle and audited and fixed manually.
--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/i915_syncmap.c +8 -4
@@ -82,6 +82,10 @@ struct i915_syncmap {* struct i915_syncmap *child[KSYNCMAP]; * }; */ +union { + DECLARE_FLEX_ARRAY(u32, seqno); + DECLARE_FLEX_ARRAY(struct i915_syncmap *, child); + }; };/**
@@ -99,13 +103,13 @@ void i915_syncmap_init(struct i915_syncmap **root)static inline u32 *__sync_seqno (struct i915_syncmap *p) {GEM_BUG_ON (p->height); -return (u32 *)(p +1 ); +return p->seqno; }static inline struct i915_syncmap **__sync_child (struct i915_syncmap *p) {GEM_BUG_ON (!p->height); -return (struct i915_syncmap **)(p +1 ); +return p->child; } static inline unsigned int
@@ -200,7 +204,7 @@ __sync_alloc_leaf(struct i915_syncmap *parent, u64 id) {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))return NULL ;
@@ -282,7 +286,7 @@ static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno)unsigned int above;/* Insert a join above the current layer */ - next =kzalloc (sizeof (*next) +KSYNCMAP *sizeof (next), + next =kzalloc (struct_size (next, child,KSYNCMAP ),GFP_KERNEL );if (unlikely (!next))return -ENOMEM ;
--
2.25.1
Patchwork
CI[Intel-gfx] ✗ Fi.CI.BUILD: failure for drm/i915: Prefer struct_size over open coded arithmetic
== Series Details ==
== Series Details ==
Series: drm/i915: Prefer struct_size over open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failure
== Summary ==
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
CC [M] drivers/gpu/drm/i915/i915_syncmap.odrivers/gpu/drm/i915/i915_syncmap.c:86:3: error: expected specifier-qualifier-list before ‘DECLARE_FLEX_ARRAY’
DECLARE_FLEX_ARRAY(u32, seqno);
^~~~~~~~~~~~~~~~~~drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_seqno’:drivers/gpu/drm/i915/i915_syncmap.c:106:10: error: ‘struct i915_syncmap’ has no member named ‘seqno’
return p->seqno;
^~
drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_child’:
drivers/gpu/drm/i915/i915_syncmap.c:112:10: error: ‘struct i915_syncmap’ has no member named ‘child’
return p->child;
^~
In file included from ./include/linux/slab.h:16,
from drivers/gpu/drm/i915/i915_syncmap.c:25:
drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_alloc_leaf’:
./include/linux/overflow.h:194:18: error: ‘struct i915_syncmap’ has no member named ‘seqno’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~
drivers/gpu/drm/i915/i915_syncmap.c:207:14: note: in expansion of macro ‘struct_size’
p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
^~~~~~~~~~~
In file included from ./include/linux/bits.h:22,
from ./include/linux/ratelimit_types.h:5,
from ./include/linux/printk.h:10,
from ./include/asm-generic/bug.h:22,
from ./arch/x86/include/asm/bug.h:84,
from ./include/linux/bug.h:5,
from ./include/linux/mmdebug.h:5,
from ./include/linux/gfp.h:5,
from ./include/linux/slab.h:15,
from drivers/gpu/drm/i915/i915_syncmap.c:25:
./include/linux/overflow.h:194:49: error: ‘struct i915_syncmap’ has no member named ‘seqno’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~
./include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:46: note: in expansion of macro ‘__same_type’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~
drivers/gpu/drm/i915/i915_syncmap.c:207:14: note: in expansion of macro ‘struct_size’
p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
^~~~~~~~~~~
./include/linux/overflow.h:194:49: error: ‘struct i915_syncmap’ has no member named ‘seqno’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~
./include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:46: note: in expansion of macro ‘__same_type’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~
drivers/gpu/drm/i915/i915_syncmap.c:207:14: note: in expansion of macro ‘struct_size’
p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
^~~~~~~~~~~
./include/linux/build_bug.h:16:51: error: bit-field ‘<anonymous>’ width not an integer constant
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:28: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~
drivers/gpu/drm/i915/i915_syncmap.c:207:14: note: in expansion of macro ‘struct_size’
p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
^~~~~~~~~~~
In file included from ./include/linux/slab.h:16,
from drivers/gpu/drm/i915/i915_syncmap.c:25:
drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_set’:
./include/linux/overflow.h:194:18: error: ‘struct i915_syncmap’ has no member named ‘child’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~drivers/gpu/drm/i915/i915_syncmap.c:289:19: note: in expansion of macro ‘struct_size’
next = kzalloc(struct_size(next, child, KSYNCMAP),
^~~~~~~~~~~
In file included from ./include/linux/bits.h:22,
from ./include/linux/ratelimit_types.h:5,
from ./include/linux/printk.h:10,
from ./include/asm-generic/bug.h:22,
from ./arch/x86/include/asm/bug.h:84,
from ./include/linux/bug.h:5,
from ./include/linux/mmdebug.h:5,
from ./include/linux/gfp.h:5,
from ./include/linux/slab.h:15,
from drivers/gpu/drm/i915/i915_syncmap.c:25:
./include/linux/overflow.h:194:49: error: ‘struct i915_syncmap’ has no member named ‘child’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~
./include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:46: note: in expansion of macro ‘__same_type’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~drivers/gpu/drm/i915/i915_syncmap.c:289:19: note: in expansion of macro ‘struct_size’
next = kzalloc(struct_size(next, child, KSYNCMAP),
^~~~~~~~~~~./include/linux/overflow.h:194:49: error: ‘struct i915_syncmap’ has no member named ‘child’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~
./include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:46: note: in expansion of macro ‘__same_type’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~drivers/gpu/drm/i915/i915_syncmap.c:289:19: note: in expansion of macro ‘struct_size’
next = kzalloc(struct_size(next, child, KSYNCMAP),
^~~~~~~~~~~./include/linux/build_bug.h:16:51: error: bit-field ‘<anonymous>’ width not an integer constant
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^
./include/linux/compiler.h:258:28: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
^~~~~~~~~~~~~~~~~
./include/linux/overflow.h:194:30: note: in expansion of macro ‘__must_be_array’
sizeof(*(p)->member) + __must_be_array((p)->member),\
^~~~~~~~~~~~~~~drivers/gpu/drm/i915/i915_syncmap.c:289:19: note: in expansion of macro ‘struct_size’
next = kzalloc(struct_size(next, child, KSYNCMAP),
^~~~~~~~~~~drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_child’:drivers/gpu/drm/i915/i915_syncmap.c:113:1: error: control reaches end of non-void function [-Werror=return-type]
}
^drivers/gpu/drm/i915/i915_syncmap.c: In function ‘__sync_seqno’:drivers/gpu/drm/i915/i915_syncmap.c:107:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1: all warnings being treated as errorsscripts/Makefile.build:277: recipe for target 'drivers/gpu/drm/i915/i915_syncmap.o' failed make[4]: *** [drivers/gpu/drm/i915/i915_syncmap.o] Error 1scripts/Makefile.build:540: recipe for target 'drivers/gpu/drm/i915' failed
make[3]: *** [drivers/gpu/drm/i915] Error 2scripts/Makefile.build:540: recipe for target 'drivers/gpu/drm' failed
make[2]: *** [drivers/gpu/drm] Error 2scripts/Makefile.build:540: recipe for target 'drivers/gpu' failed
make[1]: *** [drivers/gpu] Error 2
Makefile:1868: recipe for target 'drivers' failed
make: *** [drivers] Error 2
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
I received a mail telling that this patch doesn't build:
== Series Details ==
Series: drm/i915: Prefer struct_size over open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failure
But it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
Regards,
Len
Jani Nikula
maintainerQuoted 37 lines · Len Baker
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)I received a mail telling that this patch doesn't build:
Quoted 8 lines
== Series Details ==
Series: drm/i915: Prefer
struct_sizeover open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failureBut it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
drm-tip [1]. It's a sort of linux-next for graphics. I think there are still some branches that don't feed to linux-next.
BR,
Jani.
[1] https://cgit.freedesktop.org/drm/drm-tip
Quoted 3 lines
Regards,
Len
Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
Quoted 44 lines · Jani Nikula
Quoted 43 lines · Len Baker
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)I received a mail telling that this patch doesn't build:
== Series Details ==
Series: drm/i915: Prefer
struct_sizeover open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failure
Quoted 5 lines
Quoted 2 lines
But it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
drm-tip [1]. It's a sort of linux-next for graphics. I think there are still some branches that don't feed to linux-next.
Yeah we need to get gt-next in linux-next asap. Joonas promised to send out his patch to make that happen in dim.
-Daniel
Quoted 14 lines
BR,
Jani.[1] https://cgit.freedesktop.org/drm/drm-tip
Quoted 3 lines
Regards,
LenSignature · 1 line
Jani Nikula, Intel Open Source Graphics Center
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Hi Daniel and Jani,
Quoted 48 lines · Daniel Vetter
Quoted 47 lines · Jani Nikula
Quoted 45 lines · Len Baker
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)I received a mail telling that this patch doesn't build:
== Series Details ==
Series: drm/i915: Prefer
struct_sizeover open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failureBut it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
Quoted 6 lines
Quoted 2 lines
drm-tip [1]. It's a sort of linux-next for graphics. I think there are still some branches that don't feed to linux-next.
Yeah we need to get gt-next in linux-next asap. Joonas promised to send out his patch to make that happen in dim.
-Daniel
Is there a possibility that you give an "Acked-by" tag? And then this patch goes to the mainline through the Kees' tree or Gustavo's tree?
Or is it better to wait for drm-tip to update?
Regards,
Len
Quoted 20 lines
Quoted 10 lines
BR,
Jani.[1] https://cgit.freedesktop.org/drm/drm-tip
Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Regards,
Len--
Jani Nikula, Intel Open Source Graphics Center--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Jani Nikula
maintainerQuoted 54 lines · Len Baker
Hi Daniel and Jani,
Quoted 51 lines · Daniel Vetter
Quoted 49 lines · Jani Nikula
Quoted 37 lines · Len Baker
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)I received a mail telling that this patch doesn't build:
Quoted 8 lines
== Series Details ==
Series: drm/i915: Prefer
struct_sizeover open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failureBut it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
drm-tip [1]. It's a sort of linux-next for graphics. I think there are still some branches that don't feed to linux-next.
Quoted 6 lines
Quoted 3 lines
Yeah we need to get gt-next in linux-next asap. Joonas promised to send out his patch to make that happen in dim.
-DanielIs there a possibility that you give an "Acked-by" tag? And then this patch goes to the mainline through the Kees' tree or Gustavo's tree?
If this does not apply to drm-intel-gt-next (or drm-tip), applying it to some other branch will just cause unnecessary conflicts later on. It's unnecessary extra work. It's not an urgent fix or anything, there is no reason to do that. So that's a NAK.
Quoted 1 line
Or is it better to wait for drm-tip to update?
drm-tip is up to date, it's just that one of the branches that feed to it is (was?) not feeding to linux-next.
If you're contributing to drm, please consider basing your patches on top of drm-tip.
BR,
Jani.
Quoted 24 lines
Regards,
LenQuoted 20 lines
Quoted 10 lines
BR,
Jani.[1] https://cgit.freedesktop.org/drm/drm-tip
Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Regards,
Len--
Jani Nikula, Intel Open Source Graphics Center--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
Hi Jani,
Quoted 59 lines · Jani Nikula
Quoted 58 lines · Len Baker
Hi Daniel and Jani,
Quoted 54 lines · Daniel Vetter
Quoted 44 lines · Jani Nikula
Quoted 43 lines · Len Baker
Hi,
Quoted 25 lines · Len Baker
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code.
So, add at the end of the struct
i915_syncmapa union with two flexible array members (these arrays share the same memory layout). This is possible using the newDECLARE_FLEX_ARRAYmacro. And then, use thestruct_size()helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc andkzalloc()functions.Also, take the opportunity to refactor the
__sync_seqnoand__sync_childmaking them more readable.This code was detected with the help of Coccinelle and audited and fixed manually.
Quoted 6 lines
Signed-off-by Len Baker <[email protected]>--- drivers/gpu/drm/i915/i915_syncmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)I received a mail telling that this patch doesn't build:
== Series Details ==
Series: drm/i915: Prefer
struct_sizeover open coded arithmetic
URL : https://patchwork.freedesktop.org/series/95408/
State : failureQuoted 5 lines
Quoted 2 lines
But it builds without error against linux-next (tag next-20211001). Against which tree and branch do I need to build?
drm-tip [1]. It's a sort of linux-next for graphics. I think there are still some branches that don't feed to linux-next.
Yeah we need to get gt-next in linux-next asap. Joonas promised to send out his patch to make that happen in dim.
-Daniel
Quoted 7 lines
Quoted 2 lines
Is there a possibility that you give an "Acked-by" tag? And then this patch goes to the mainline through the Kees' tree or Gustavo's tree?
If this does not apply to drm-intel-gt-next (or drm-tip), applying it to some other branch will just cause unnecessary conflicts later on. It's unnecessary extra work. It's not an urgent fix or anything, there is no reason to do that. So that's a NAK.
Ok. Understood.
Quoted 4 lines
Quoted 1 line
Or is it better to wait for drm-tip to update?
drm-tip is up to date, it's just that one of the branches that feed to it is (was?) not feeding to linux-next.
Sorry, but I'm missing something here. In linux-next this is the commit history of include/linux/stddef.h file:
3080ea5553cc stddef: Introduce DECLARE_FLEX_ARRAY() helper 50d7bd38c3aa stddef: Introduce struct_group() helper macro e7f18c22e6be stddef: Fix kerndoc for sizeof_field() and offsetofend() 4229a470175b stddef.h: Introduce sizeof_field()
...
But in drm-tip this is the commit history:
4229a470175b stddef.h: Introduce sizeof_field()
...
For this patch the DECLARE_FLEX_ARRAY() helper is needed. But the build fails due to the last tree commits for stddef.h file are not present. So, if I understand correctly, drm-tip is not up to date with linux-next.
Regards,
Len
Quoted 36 lines
If you're contributing to drm, please consider basing your patches on top of drm-tip.
BR,
Jani.Quoted 24 lines
Regards,
LenQuoted 20 lines
Quoted 10 lines
BR,
Jani.[1] https://cgit.freedesktop.org/drm/drm-tip
Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Regards,
Len--
Jani Nikula, Intel Open Source Graphics Center--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch--
Jani Nikula, Intel Open Source Graphics Center
Jani Nikula
maintainerQuoted 9 lines · Len Baker
Sorry, but I'm missing something here. In linux-next this is the commit history of
include/linux/stddef.hfile:
3080ea5553ccstddef: IntroduceDECLARE_FLEX_ARRAY()helper50d7bd38c3aastddef: Introducestruct_group()helper macroe7f18c22e6bestddef: Fix kerndoc forsizeof_field()andoffsetofend()4229a470175bstddef.h: Introducesizeof_field()
...
Quoted 8 lines
But in drm-tip this is the commit history:
4229a470175bstddef.h: Introducesizeof_field()
...For this patch the
DECLARE_FLEX_ARRAY()helper is needed. But the build fails due to the last tree commits forstddef.hfile are not present. So, if I understand correctly, drm-tip is not up to date with linux-next.
linux-next is an ephemeral integration branch for most arch, subsystem and driver -next branches.
drm-tip is an ephemeral integration branch for drm subsystem and driver -next branches.
They contain different sets of branches. They are constantly rebuilt. They are not the end result or end goal.
If a problem (or a solution, for that matter) only exists in the merge of some of those branches, you can't actually fix it until such a merge exists somewhere more permanent than an ephemeral integration branch.
BR,
Jani.
Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
Hi,
Quoted 25 lines · Jani Nikula
Quoted 9 lines · Len Baker
Sorry, but I'm missing something here. In linux-next this is the commit history of
include/linux/stddef.hfile:
3080ea5553ccstddef: IntroduceDECLARE_FLEX_ARRAY()helper50d7bd38c3aastddef: Introducestruct_group()helper macroe7f18c22e6bestddef: Fix kerndoc forsizeof_field()andoffsetofend()4229a470175bstddef.h: Introducesizeof_field()
...Quoted 8 lines
But in drm-tip this is the commit history:
4229a470175bstddef.h: Introducesizeof_field()
...For this patch the
DECLARE_FLEX_ARRAY()helper is needed. But the build fails due to the last tree commits forstddef.hfile are not present. So, if I understand correctly, drm-tip is not up to date with linux-next.linux-next is an ephemeral integration branch for most arch, subsystem and driver -next branches.
drm-tip is an ephemeral integration branch for drm subsystem and driver -next branches.
Quoted 6 lines
They contain different sets of branches. They are constantly rebuilt. They are not the end result or end goal.
If a problem (or a solution, for that matter) only exists in the merge of some of those branches, you can't actually fix it until such a merge exists somewhere more permanent than an ephemeral integration branch.
Ok, understood. Thanks for the clarification.
Regards,
Len