Skip to content

Commit 5548dd7

Browse files
rpptakpm00
authored andcommitted
tools/testing: fix testing/vma and testing/radix-tree build
Build of VMA and radix-tree tests is unhappy after the conversion of kzalloc() to kzalloc_obj() in lib/idr.c: cc -I../shared -I. -I../../include -I../../arch/x86/include -I../../../lib -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address -fsanitize=undefined -DNUM_VMA_FLAG_BITS=128 -DNUM_MM_FLAG_BITS=128 -c -o idr.o idr.c idr.c: In function `ida_alloc_range': idr.c:420:34: error: implicit declaration of function `kzalloc_obj'; did you mean `kzalloc_node'? [-Wimplicit-function-declaration] 420 | bitmap = kzalloc_obj(*bitmap, GFP_NOWAIT); | ^~~~~~~~~~~ | kzalloc_node idr.c:420:32: error: assignment to `struct ida_bitmap *' from `int' makes pointer from integer without a cast [-Wint-conversion] 420 | bitmap = kzalloc_obj(*bitmap, GFP_NOWAIT); | ^ idr.c:447:40: error: assignment to `struct ida_bitmap *' from `int' makes pointer from integer without a cast [-Wint-conversion] 447 | bitmap = kzalloc_obj(*bitmap, GFP_NOWAIT); | ^ idr.c:468:15: error: assignment to `struct ida_bitmap *' from `int' makes pointer from integer without a cast [-Wint-conversion] 468 | alloc = kzalloc_obj(*bitmap, gfp); | ^ make: *** [<builtin>: idr.o] Error 1 Import necessary macros from include/linux to tools/include/linux to fix the compilation. Link: https://lkml.kernel.org/r/20260225233111.2760752-1-rppt@kernel.org Fixes: 69050f8 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Tested-by: SeongJae Park <sj@kernel.org> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: David Hildenbrand <david@kernel.org> Cc: Kees Cook <kees@kernel.org> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 2d28ed5 commit 5548dd7

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

tools/include/linux/gfp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <linux/types.h>
66
#include <linux/gfp_types.h>
77

8+
/* Helper macro to avoid gfp flags if they are the default one */
9+
#define __default_gfp(a,...) a
10+
#define default_gfp(...) __default_gfp(__VA_ARGS__ __VA_OPT__(,) GFP_KERNEL)
11+
812
static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags)
913
{
1014
return !!(gfp_flags & __GFP_DIRECT_RECLAIM);

tools/include/linux/overflow.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@
6868
__builtin_mul_overflow(__a, __b, __d); \
6969
})
7070

71+
/**
72+
* size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
73+
* @factor1: first factor
74+
* @factor2: second factor
75+
*
76+
* Returns: calculate @factor1 * @factor2, both promoted to size_t,
77+
* with any overflow causing the return value to be SIZE_MAX. The
78+
* lvalue must be size_t to avoid implicit type conversion.
79+
*/
80+
static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
81+
{
82+
size_t bytes;
83+
84+
if (check_mul_overflow(factor1, factor2, &bytes))
85+
return SIZE_MAX;
86+
87+
return bytes;
88+
}
89+
7190
/**
7291
* array_size() - Calculate size of 2-dimensional array.
7392
*

tools/include/linux/slab.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,13 @@ static inline unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf)
202202
return sheaf->size;
203203
}
204204

205+
#define __alloc_objs(KMALLOC, GFP, TYPE, COUNT) \
206+
({ \
207+
const size_t __obj_size = size_mul(sizeof(TYPE), COUNT); \
208+
(TYPE *)KMALLOC(__obj_size, GFP); \
209+
})
210+
211+
#define kzalloc_obj(P, ...) \
212+
__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
213+
205214
#endif /* _TOOLS_SLAB_H */

0 commit comments

Comments
 (0)