Skip to content

Commit 55fb412

Browse files
committed
debugobjects: Dont destroy kmem cache in init()
debug_objects_mem_init() is invoked from mm_core_init() before work queues are available. If debug_objects_mem_init() destroys the kmem cache in the error path it causes an Oops in __queue_work(): Oops: Oops: 0000 [#1] PREEMPT SMP PTI RIP: 0010:__queue_work+0x35/0x6a0 queue_work_on+0x66/0x70 flush_all_cpus_locked+0xdf/0x1a0 __kmem_cache_shutdown+0x2f/0x340 kmem_cache_destroy+0x4e/0x150 mm_core_init+0x9e/0x120 start_kernel+0x298/0x800 x86_64_start_reservations+0x18/0x30 x86_64_start_kernel+0xc5/0xe0 common_startup_64+0x12c/0x138 Further the object cache pointer is used in various places to check for early boot operation. It is exposed before the replacments for the static boot time objects are allocated and the self test operates on it. This can be avoided by: 1) Running the self test with the static boot objects 2) Exposing it only after the replacement objects have been added to the pool. Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/[email protected]
1 parent 813fd07 commit 55fb412

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

lib/debugobjects.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ static __initconst const struct debug_obj_descr descr_type_test = {
12111211

12121212
static __initdata struct self_test obj = { .static_init = 0 };
12131213

1214-
static void __init debug_objects_selftest(void)
1214+
static bool __init debug_objects_selftest(void)
12151215
{
12161216
int fixups, oldfixups, warnings, oldwarnings;
12171217
unsigned long flags;
@@ -1280,9 +1280,10 @@ static void __init debug_objects_selftest(void)
12801280
descr_test = NULL;
12811281

12821282
local_irq_restore(flags);
1283+
return !!debug_objects_enabled;
12831284
}
12841285
#else
1285-
static inline void debug_objects_selftest(void) { }
1286+
static inline bool debug_objects_selftest(void) { return true; }
12861287
#endif
12871288

12881289
/*
@@ -1302,31 +1303,28 @@ void __init debug_objects_early_init(void)
13021303
}
13031304

13041305
/*
1305-
* Convert the statically allocated objects to dynamic ones:
1306+
* Convert the statically allocated objects to dynamic ones.
1307+
* debug_objects_mem_init() is called early so only one CPU is up and
1308+
* interrupts are disabled, which means it is safe to replace the active
1309+
* object references.
13061310
*/
1307-
static int __init debug_objects_replace_static_objects(void)
1311+
static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13081312
{
13091313
struct debug_bucket *db = obj_hash;
1310-
struct hlist_node *tmp;
13111314
struct debug_obj *obj, *new;
1315+
struct hlist_node *tmp;
13121316
HLIST_HEAD(objects);
13131317
int i, cnt = 0;
13141318

13151319
for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1316-
obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1320+
obj = kmem_cache_zalloc(cache, GFP_KERNEL);
13171321
if (!obj)
13181322
goto free;
13191323
hlist_add_head(&obj->node, &objects);
13201324
}
13211325

13221326
debug_objects_allocated += i;
13231327

1324-
/*
1325-
* debug_objects_mem_init() is now called early that only one CPU is up
1326-
* and interrupts have been disabled, so it is safe to replace the
1327-
* active object references.
1328-
*/
1329-
13301328
/*
13311329
* Replace the statically allocated objects list with the allocated
13321330
* objects list.
@@ -1347,15 +1345,14 @@ static int __init debug_objects_replace_static_objects(void)
13471345
}
13481346
}
13491347

1350-
pr_debug("%d of %d active objects replaced\n",
1351-
cnt, obj_pool_used);
1352-
return 0;
1348+
pr_debug("%d of %d active objects replaced\n", cnt, obj_pool_used);
1349+
return true;
13531350
free:
13541351
hlist_for_each_entry_safe(obj, tmp, &objects, node) {
13551352
hlist_del(&obj->node);
1356-
kmem_cache_free(obj_cache, obj);
1353+
kmem_cache_free(cache, obj);
13571354
}
1358-
return -ENOMEM;
1355+
return false;
13591356
}
13601357

13611358
/*
@@ -1366,6 +1363,7 @@ static int __init debug_objects_replace_static_objects(void)
13661363
*/
13671364
void __init debug_objects_mem_init(void)
13681365
{
1366+
struct kmem_cache *cache;
13691367
int cpu, extras;
13701368

13711369
if (!debug_objects_enabled)
@@ -1380,29 +1378,33 @@ void __init debug_objects_mem_init(void)
13801378
for_each_possible_cpu(cpu)
13811379
INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
13821380

1383-
obj_cache = kmem_cache_create("debug_objects_cache",
1384-
sizeof (struct debug_obj), 0,
1385-
SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1386-
NULL);
1381+
if (!debug_objects_selftest())
1382+
return;
13871383

1388-
if (!obj_cache || debug_objects_replace_static_objects()) {
1384+
cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
1385+
SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
1386+
1387+
if (!cache || !debug_objects_replace_static_objects(cache)) {
13891388
debug_objects_enabled = 0;
1390-
kmem_cache_destroy(obj_cache);
1391-
pr_warn("out of memory.\n");
1389+
pr_warn("Out of memory.\n");
13921390
return;
1393-
} else
1394-
debug_objects_selftest();
1395-
1396-
#ifdef CONFIG_HOTPLUG_CPU
1397-
cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
1398-
object_cpu_offline);
1399-
#endif
1391+
}
14001392

14011393
/*
1402-
* Increase the thresholds for allocating and freeing objects
1403-
* according to the number of possible CPUs available in the system.
1394+
* Adjust the thresholds for allocating and freeing objects
1395+
* according to the number of possible CPUs available in the
1396+
* system.
14041397
*/
14051398
extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
14061399
debug_objects_pool_size += extras;
14071400
debug_objects_pool_min_level += extras;
1401+
1402+
/* Everything worked. Expose the cache */
1403+
obj_cache = cache;
1404+
1405+
#ifdef CONFIG_HOTPLUG_CPU
1406+
cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
1407+
object_cpu_offline);
1408+
#endif
1409+
return;
14081410
}

0 commit comments

Comments
 (0)