Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

maple_tree: add test to replicate low memory race conditions

Add new callback fields to the userspace implementation of struct
kmem_cache. This allows for executing callback functions in order to
further test low memory scenarios where node allocation is retried.

This callback can help test race conditions by calling a function when a
low memory event is tested.

Link: https://lkml.kernel.org/r/20240812190543.71967-2-sidhartha.kumar@oracle.com
Signed-off-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Sidhartha Kumar and committed by
Andrew Morton
617f8e4d e1b8b883

+101 -1
+13
lib/maple_tree.c
··· 7005 7005 kmem_cache_set_non_kernel(maple_node_cache, val); 7006 7006 } 7007 7007 7008 + extern void kmem_cache_set_callback(struct kmem_cache *cachep, 7009 + void (*callback)(void *)); 7010 + void mt_set_callback(void (*callback)(void *)) 7011 + { 7012 + kmem_cache_set_callback(maple_node_cache, callback); 7013 + } 7014 + 7015 + extern void kmem_cache_set_private(struct kmem_cache *cachep, void *private); 7016 + void mt_set_private(void *private) 7017 + { 7018 + kmem_cache_set_private(maple_node_cache, private); 7019 + } 7020 + 7008 7021 extern unsigned long kmem_cache_get_alloc(struct kmem_cache *); 7009 7022 unsigned long mt_get_alloc_size(void) 7010 7023 {
+63
tools/testing/radix-tree/maple.c
··· 36224 36224 36225 36225 extern void test_kmem_cache_bulk(void); 36226 36226 36227 + /* callback function used for check_nomem_writer_race() */ 36228 + static void writer2(void *maple_tree) 36229 + { 36230 + struct maple_tree *mt = (struct maple_tree *)maple_tree; 36231 + MA_STATE(mas, mt, 6, 10); 36232 + 36233 + mtree_lock(mas.tree); 36234 + mas_store(&mas, xa_mk_value(0xC)); 36235 + mas_destroy(&mas); 36236 + mtree_unlock(mas.tree); 36237 + } 36238 + 36239 + /* 36240 + * check_nomem_writer_race() - test a possible race in the mas_nomem() path 36241 + * @mt: The tree to build. 36242 + * 36243 + * There is a possible race condition in low memory conditions when mas_nomem() 36244 + * gives up its lock. A second writer can chagne the entry that the primary 36245 + * writer executing the mas_nomem() path is modifying. This test recreates this 36246 + * scenario to ensure we are handling it correctly. 36247 + */ 36248 + static void check_nomem_writer_race(struct maple_tree *mt) 36249 + { 36250 + MA_STATE(mas, mt, 0, 5); 36251 + 36252 + mt_set_non_kernel(0); 36253 + /* setup root with 2 values with NULL in between */ 36254 + mtree_store_range(mt, 0, 5, xa_mk_value(0xA), GFP_KERNEL); 36255 + mtree_store_range(mt, 6, 10, NULL, GFP_KERNEL); 36256 + mtree_store_range(mt, 11, 15, xa_mk_value(0xB), GFP_KERNEL); 36257 + 36258 + /* setup writer 2 that will trigger the race condition */ 36259 + mt_set_private(mt); 36260 + mt_set_callback(writer2); 36261 + 36262 + mtree_lock(mt); 36263 + /* erase 0-5 */ 36264 + mas_erase(&mas); 36265 + 36266 + /* index 6-10 should retain the value from writer 2 */ 36267 + check_load(mt, 6, xa_mk_value(0xC)); 36268 + mtree_unlock(mt); 36269 + 36270 + /* test for the same race but with mas_store_gfp() */ 36271 + mtree_store_range(mt, 0, 5, xa_mk_value(0xA), GFP_KERNEL); 36272 + mtree_store_range(mt, 6, 10, NULL, GFP_KERNEL); 36273 + 36274 + mas_set_range(&mas, 0, 5); 36275 + mtree_lock(mt); 36276 + mas_store_gfp(&mas, NULL, GFP_KERNEL); 36277 + 36278 + /* ensure write made by writer 2 is retained */ 36279 + check_load(mt, 6, xa_mk_value(0xC)); 36280 + 36281 + mt_set_private(NULL); 36282 + mt_set_callback(NULL); 36283 + mtree_unlock(mt); 36284 + } 36285 + 36227 36286 void farmer_tests(void) 36228 36287 { 36229 36288 struct maple_node *node; ··· 36314 36255 36315 36256 mt_init_flags(&tree, 0); 36316 36257 check_dfs_preorder(&tree); 36258 + mtree_destroy(&tree); 36259 + 36260 + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU); 36261 + check_nomem_writer_race(&tree); 36317 36262 mtree_destroy(&tree); 36318 36263 36319 36264 mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE);
+25 -1
tools/testing/shared/linux.c
··· 26 26 unsigned int non_kernel; 27 27 unsigned long nr_allocated; 28 28 unsigned long nr_tallocated; 29 + bool exec_callback; 30 + void (*callback)(void *); 31 + void *private; 29 32 }; 33 + 34 + void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *)) 35 + { 36 + cachep->callback = callback; 37 + } 38 + 39 + void kmem_cache_set_private(struct kmem_cache *cachep, void *private) 40 + { 41 + cachep->private = private; 42 + } 30 43 31 44 void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val) 32 45 { ··· 71 58 { 72 59 void *p; 73 60 61 + if (cachep->exec_callback) { 62 + if (cachep->callback) 63 + cachep->callback(cachep->private); 64 + cachep->exec_callback = false; 65 + } 66 + 74 67 if (!(gfp & __GFP_DIRECT_RECLAIM)) { 75 - if (!cachep->non_kernel) 68 + if (!cachep->non_kernel) { 69 + cachep->exec_callback = true; 76 70 return NULL; 71 + } 77 72 78 73 cachep->non_kernel--; 79 74 } ··· 244 223 ret->objs = NULL; 245 224 ret->ctor = ctor; 246 225 ret->non_kernel = 0; 226 + ret->exec_callback = false; 227 + ret->callback = NULL; 228 + ret->private = NULL; 247 229 return ret; 248 230 } 249 231