lib min_heap: Improve min_heap safety, testing, and documentation
Subsystems (3)
Kuan-Wei Chiu
maintainercover lib min_heap: Improve min_heap safety, testing, and documentation
Improve the min heap implementation by enhancing type safety with container_of, reducing the attack vector by replacing test function calls with inline variants, and adding a brief API introduction in min_heap.h. It also includes author information in Documentation/core-api/min_heap.rst.
Kuan-Wei Chiu (4):
lib min_heap: Improve type safety in min_heap macros by using
container_of
lib/test_min_heap: Use inline min heap variants to reduce attack
vector
lib min_heap: Add brief introduction to Min Heap API Documentation/core-api: min_heap: Add author information
Documentation/core-api/min_heap.rst | 2 + include/linux/min_heap.h | 72 ++++++++++++++++++----------- lib/Kconfig.debug | 1 - lib/test_min_heap.c | 30 ++++++------ 4 files changed, 63 insertions(+), 42 deletions(-)
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH 1/4 lib min_heap: Improve type safety in min_heap macros by using container_of
The current implementation of min_heap macros uses explicit casting to min_heap_char *, which prevents the compiler from detecting incorrect pointer types. This can lead to errors if non-min_heap pointers are passed inadvertently.
To enhance safety, replace all explicit casts to min_heap_char * with the use of container_of(&(_heap)->nr, min_heap_char, nr). This approach ensures that the _heap parameter is indeed a min_heap_char-compatible structure, allowing the compiler to catch improper usages.
--- include/linux/min_heap.h | 61 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 26 deletions(-)
include/linux/min_heap.h +35 -26
@@ -218,7 +218,7 @@ void __min_heap_init_inline(min_heap_char *heap, void *data, int size) }#define min_heap_init_inline (_heap, _data, _size) \ - __min_heap_init_inline((min_heap_char *)_heap, _data, _size) + __min_heap_init_inline(container_of(&(_heap)->nr, min_heap_char, nr), _data, _size)/* Get the minimum element from the heap. */ static __always_inline
@@ -228,7 +228,8 @@ void *__min_heap_peek_inline(struct min_heap_char *heap) }#define min_heap_peek_inline (_heap) \ - (__minheap_cast(_heap) __min_heap_peek_inline((min_heap_char *)_heap)) + (__minheap_cast(_heap) \ + __min_heap_peek_inline(container_of(&(_heap)->nr, min_heap_char, nr)))/* Check if the heap is full. */ static __always_inline
@@ -238,7 +239,7 @@ bool __min_heap_full_inline(min_heap_char *heap) }#define min_heap_full_inline (_heap) \ - __min_heap_full_inline((min_heap_char *)_heap) + __min_heap_full_inline(container_of(&(_heap)->nr, min_heap_char, nr))/* Sift the element at pos down the heap. */ static __always_inline
@@ -277,8 +278,8 @@ void __min_heap_sift_down_inline(min_heap_char *heap, int pos, size_t elem_size, }#define min_heap_sift_down_inline (_heap, _pos, _func, _args) \ - __min_heap_sift_down_inline((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), \ - _func, _args) + __min_heap_sift_down_inline(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \ + __minheap_obj_size(_heap), _func, _args)/* Sift up ith element from the heap, O(log2(nr)). */ static __always_inline
@@ -304,8 +305,8 @@ void __min_heap_sift_up_inline(min_heap_char *heap, size_t elem_size, size_t idx }#define min_heap_sift_up_inline (_heap, _idx, _func, _args) \ - __min_heap_sift_up_inline((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, \ - _func, _args) + __min_heap_sift_up_inline(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _idx, _func, _args)/* Floyd's approach to heapification that is O(nr). */ static __always_inline
@@ -319,7 +320,8 @@ void __min_heapify_all_inline(min_heap_char *heap, size_t elem_size, }#define min_heapify_all_inline (_heap, _func, _args) \ - __min_heapify_all_inline((min_heap_char *)_heap, __minheap_obj_size(_heap), _func, _args) + __min_heapify_all_inline(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _func, _args)/* Remove minimum element from the heap, O(log2(nr)). */ static __always_inline
@@ -340,7 +342,8 @@ bool __min_heap_pop_inline(min_heap_char *heap, size_t elem_size, }#define min_heap_pop_inline (_heap, _func, _args) \ - __min_heap_pop_inline((min_heap_char *)_heap, __minheap_obj_size(_heap), _func, _args) + __min_heap_pop_inline(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _func, _args)/* * Remove the minimum element and then push the given element. The
@@ -356,8 +359,8 @@ void __min_heap_pop_push_inline(min_heap_char *heap, const void *element, size_t }#define min_heap_pop_push_inline (_heap, _element, _func, _args) \ - __min_heap_pop_push_inline((min_heap_char *)_heap, _element, __minheap_obj_size(_heap), \ - _func, _args) + __min_heap_pop_push_inline(container_of(&(_heap)->nr, min_heap_char, nr), _element, \ + __minheap_obj_size(_heap), _func, _args)/* Push an element on to the heap, O(log2(nr)). */ static __always_inline
@@ -382,8 +385,8 @@ bool __min_heap_push_inline(min_heap_char *heap, const void *element, size_t ele }#define min_heap_push_inline (_heap, _element, _func, _args) \ - __min_heap_push_inline((min_heap_char *)_heap, _element, __minheap_obj_size(_heap), \ - _func, _args) + __min_heap_push_inline(container_of(&(_heap)->nr, min_heap_char, nr), _element, \ + __minheap_obj_size(_heap), _func, _args)/* Remove ith element from the heap, O(log2(nr)). */ static __always_inline
@@ -411,8 +414,8 @@ bool __min_heap_del_inline(min_heap_char *heap, size_t elem_size, size_t idx, }#define min_heap_del_inline (_heap, _idx, _func, _args) \ - __min_heap_del_inline((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, \ - _func, _args) + __min_heap_del_inline(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _idx, _func, _args)void __min_heap_init (min_heap_char *heap,void *data,int size);void *__min_heap_peek (struct min_heap_char *heap);
@@ -433,25 +436,31 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx, const struct min_heap_callbacks *func, void *args);#define min_heap_init (_heap, _data, _size) \ - __min_heap_init((min_heap_char *)_heap, _data, _size) + __min_heap_init(container_of(&(_heap)->nr, min_heap_char, nr), _data, _size)#define min_heap_peek (_heap) \ - (__minheap_cast(_heap) __min_heap_peek((min_heap_char *)_heap)) + (__minheap_cast(_heap) __min_heap_peek(container_of(&(_heap)->nr, min_heap_char, nr)))#define min_heap_full (_heap) \ - __min_heap_full((min_heap_char *)_heap) + __min_heap_full(container_of(&(_heap)->nr, min_heap_char, nr))#define min_heap_sift_down (_heap, _pos, _func, _args) \ - __min_heap_sift_down((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args) + __min_heap_sift_down(container_of(&(_heap)->nr, min_heap_char, nr), _pos, \ + __minheap_obj_size(_heap), _func, _args)#define min_heap_sift_up (_heap, _idx, _func, _args) \ - __min_heap_sift_up((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, _func, _args) + __min_heap_sift_up(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _idx, _func, _args)#define min_heapify_all (_heap, _func, _args) \ - __min_heapify_all((min_heap_char *)_heap, __minheap_obj_size(_heap), _func, _args) + __min_heapify_all(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _func, _args)#define min_heap_pop (_heap, _func, _args) \ - __min_heap_pop((min_heap_char *)_heap, __minheap_obj_size(_heap), _func, _args) + __min_heap_pop(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _func, _args)#define min_heap_pop_push (_heap, _element, _func, _args) \ - __min_heap_pop_push((min_heap_char *)_heap, _element, __minheap_obj_size(_heap), \ - _func, _args) + __min_heap_pop_push(container_of(&(_heap)->nr, min_heap_char, nr), _element, \ + __minheap_obj_size(_heap), _func, _args)#define min_heap_push (_heap, _element, _func, _args) \ - __min_heap_push((min_heap_char *)_heap, _element, __minheap_obj_size(_heap), _func, _args) + __min_heap_push(container_of(&(_heap)->nr, min_heap_char, nr), _element, \ + __minheap_obj_size(_heap), _func, _args)#define min_heap_del (_heap, _idx, _func, _args) \ - __min_heap_del((min_heap_char *)_heap, __minheap_obj_size(_heap), _idx, _func, _args) + __min_heap_del(container_of(&(_heap)->nr, min_heap_char, nr), \ + __minheap_obj_size(_heap), _idx, _func, _args)#endif /* _LINUX_MIN_HEAP_H */
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH 2/4 lib/test_min_heap: Use inline min heap variants to reduce attack vector
In reply to Kuan-Wei ChiuTo address concerns about increasing the attack vector, remove the select MIN_HEAP dependency from TEST_MIN_HEAP in Kconfig.debug.
Additionally, all min heap test function calls in lib/test_min_heap.c are replaced with their inline variants. By exclusively using inline variants, we eliminate the need to enable CONFIG_MIN_HEAP for testing purposes.
--- lib/Kconfig.debug | 1 - lib/test_min_heap.c | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-)
lib/Kconfig.debug +0 -1
@@ -2269,7 +2269,6 @@ config TEST_LIST_SORT config TEST_MIN_HEAP tristate "Min heap test" depends on DEBUG_KERNEL || m - select MIN_HEAP help Enable this to turn on min heap function tests. This test is executed only once during system boot (so affects only boot time),
lib/test_min_heap.c +15 -15
@@ -32,7 +32,7 @@ static __init int pop_verify_heap(bool min_heap,int last; last = values[0 ]; -min_heap_pop (heap, funcs,NULL ); +min_heap_pop_inline (heap, funcs,NULL );while (heap->nr >0 ) {if (min_heap) {if (last > values[0 ]) {
@@ -48,7 +48,7 @@ static __init int pop_verify_heap(bool min_heap, } } last = values[0 ]; -min_heap_pop (heap, funcs,NULL ); +min_heap_pop_inline (heap, funcs,NULL ); }return err; }
@@ -69,7 +69,7 @@ static __init int test_heapify_all(bool min_heap)int i, err;/* Test with known set of values. */ -min_heapify_all (&heap, &funcs,NULL ); +min_heapify_all_inline (&heap, &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs);
@@ -78,7 +78,7 @@ static __init int test_heapify_all(bool min_heap)for (i =0 ; i < heap.nr; i++) values[i] =get_random_u32 (); -min_heapify_all (&heap, &funcs,NULL ); +min_heapify_all_inline (&heap, &funcs,NULL ); err +=pop_verify_heap (min_heap, &heap, &funcs);return err;
@@ -102,14 +102,14 @@ static __init int test_heap_push(bool min_heap)/* Test with known set of values copied from data. */ for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &data[i], &funcs,NULL ); +min_heap_push_inline (&heap, &data[i], &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs);/* Test with randomly generated values. */ while (heap.nr < heap.size) { temp =get_random_u32 (); -min_heap_push (&heap, &temp, &funcs,NULL ); +min_heap_push_inline (&heap, &temp, &funcs,NULL ); } err +=pop_verify_heap (min_heap, &heap, &funcs);
@@ -135,22 +135,22 @@ static __init int test_heap_pop_push(bool min_heap)/* Fill values with data to pop and replace. */ temp = min_heap ?0x80000000 :0x7FFFFFFF ;for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &temp, &funcs,NULL ); +min_heap_push_inline (&heap, &temp, &funcs,NULL );/* Test with known set of values copied from data. */ for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_pop_push (&heap, &data[i], &funcs,NULL ); +min_heap_pop_push_inline (&heap, &data[i], &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs); heap.nr =0 ;for (i =0 ; i <ARRAY_SIZE (data); i++) -min_heap_push (&heap, &temp, &funcs,NULL ); +min_heap_push_inline (&heap, &temp, &funcs,NULL );/* Test with randomly generated values. */ for (i =0 ; i <ARRAY_SIZE (data); i++) { temp =get_random_u32 (); -min_heap_pop_push (&heap, &temp, &funcs,NULL ); +min_heap_pop_push_inline (&heap, &temp, &funcs,NULL ); } err +=pop_verify_heap (min_heap, &heap, &funcs);
@@ -163,7 +163,7 @@ static __init int test_heap_del(bool min_heap) -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };struct min_heap_test heap; -min_heap_init (&heap, values,ARRAY_SIZE (values)); +min_heap_init_inline (&heap, values,ARRAY_SIZE (values)); heap.nr =ARRAY_SIZE (values);struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than,
@@ -172,9 +172,9 @@ static __init int test_heap_del(bool min_heap)int i, err;/* Test with known set of values. */ -min_heapify_all (&heap, &funcs,NULL ); +min_heapify_all_inline (&heap, &funcs,NULL );for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) -min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); +min_heap_del_inline (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); err =pop_verify_heap (min_heap, &heap, &funcs);
@@ -182,10 +182,10 @@ static __init int test_heap_del(bool min_heap) heap.nr =ARRAY_SIZE (values);for (i =0 ; i < heap.nr; i++) values[i] =get_random_u32 (); -min_heapify_all (&heap, &funcs,NULL ); +min_heapify_all_inline (&heap, &funcs,NULL );for (i =0 ; i <ARRAY_SIZE (values) /2 ; i++) -min_heap_del (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); +min_heap_del_inline (&heap,get_random_u32 () % heap.nr, &funcs,NULL ); err +=pop_verify_heap (min_heap, &heap, &funcs);return err;
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH 3/4 lib min_heap: Add brief introduction to Min Heap API
In reply to Kuan-Wei ChiuA short description of the Min Heap API is added to the min_heap.h, explaining its purpose for managing min-heaps and emphasizing the use of macro wrappers instead of direct function calls. For more details, users are directed to the documentation at Documentation/core-api/min_heap.rst.
--- include/linux/min_heap.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/min_heap.h +11 -0
@@ -6,6 +6,17 @@#include <linux/string.h> #include <linux/types.h> +/* +* The Min Heap API provides utilities for managing min-heaps, a binary tree +* structure where each node's value is less than or equal to its children's +* values, ensuring the smallest element is at the root. +* +* Users should avoid directly calling functions prefixed with __min_heap_*(). +* Instead, use the provided macro wrappers. +* +* For further details and examples, refer to Documentation/core-api/min_heap.rst. +*/ +/** * Data structure to hold a min-heap. * @nr: Number of elements currently in the heap.
Signature · 1 line
2.34.1
Kuan-Wei Chiu
maintainerPATCH 4/4 Documentation/core-api: min_heap: Add author information
In reply to Kuan-Wei ChiuAs with other documentation files, author information is added to min_heap.rst, providing contact details for any questions regarding the Min Heap API or the document itself.
--- Documentation/core-api/min_heap.rst | 2 ++ 1 file changed, 2 insertions(+)
Documentation/core-api/min_heap.rst +2 -0
@@ -4,6 +4,8 @@ Min Heap API ============ +:Author: Kuan-Wei Chiu <[email protected]> + Introduction ============
Signature · 1 line
2.34.1