Skip to contentSkip to conversation

debugobject: fix concurrency issues with is_static_object

PATCH Applied Schspa Shi 7 participants 26 messages Mar 3, 2023 – May 2, 2023

Subsystems (3)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Schspa Shi

author
#0 lore raw

PATCH 1/2 debugobject: fix concurrency issues with is_static_object

The is_static_object implementation relay on the initial state of the object. If multiple places are accessed concurrently, there is a probability that the debug object has been registered in the system, which will invalidate the judgment of is_static_object.

The following is the scenario where the problem occurs:

T0 T1
=========================================================================
mod_timer();
debug_object_assert_init

	db = get_bucket((unsigned long) addr);
	raw_spin_lock_irqsave(&db->lock, flags);
	obj = lookup_object(addr, db);
    if (!obj) {
		raw_spin_unlock_irqrestore(&db->lock, flags);
        << Context switch >>
                                             mod_timer();
                                               debug_object_assert_init
                                               ...
                                               enqueue_timer();
        /*
         * The initial state changed a static timer object, and
         * is_static_object will return false
         */

        if (descr->is_static_object &&
			descr->is_static_object(addr)) {
                debug_object_init();
            } else {
               << Hit here for a static object >>
			   debug_print_object(&o, "assert_init");
			   debug_object_fixup(descr->fixup_assert_init, addr,
					   ODEBUG_STATE_NOTAVAILABLE);
            }
    }

To fix it, we got the is_static_object called within db->lock, and save it's state to struct debug_obj. This will ensure we won't hit the code branch not belong to the static object.

For the same static object, debug_object_init may enter multiple times, but there is a lock in debug_object_init to ensure that there is no problem.

---
 include/linux/debugobjects.h |  1 +
 lib/debugobjects.c           | 71 ++++++++++++++++++++++++++++--------
 2 files changed, 56 insertions(+), 16 deletions(-)
include/linux/debugobjects.h +1 -0
index 32444686b6ff4..544a6111b97f6 100644
@@ -30,6 +30,7 @@ struct debug_obj {
 	enum debug_obj_state	state;
 	unsigned int		astate;
 	void			*object;
+	bool			is_static;
 	const struct debug_obj_descr *descr;
 };
 
lib/debugobjects.c +55 -16

145 lines not on this page · raw message

Signature · 1 line
2.39.2
author
#1 lore raw

PATCH 2/2 debugobject: add unit test for static debug object

Add test case to enusre that static debug object correctness.

Tested on little-endian arm64 qemu, result:

[    2.385735] KTAP version 1
[    2.385860] 1..1
[    2.386406]     KTAP version 1
[    2.386658]     # Subtest: static debugobject init
[    2.386726]     1..1
[    2.401777]     ok 1 static_debugobject_test
[    2.402455] ok 1 static debugobject init
---
 MAINTAINERS                    |   5 ++
 lib/Kconfig.debug              |  14 ++++
 lib/Makefile                   |   2 +
 lib/test_static_debug_object.c | 125 +++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 lib/test_static_debug_object.c
MAINTAINERS +5 -0
index b0db911207ba4..38187e2921691 100644
@@ -23202,6 +23202,11 @@ L:	[email protected]
 S:	Maintained
 F:	mm/zswap.c
 
+STATIC DEBUGOBJECT TEST
+M:	Schspa Shi <[email protected]>
+S:	Maintained
+F:	lib/test_static_debug_object.c
+
 THE REST
 M:	Linus Torvalds <[email protected]>
 L:	[email protected]
lib/Kconfig.debug +14 -0
index c8b379e2e9adc..9d5ee631d4380 100644
@@ -2801,6 +2801,20 @@ config TEST_CLOCKSOURCE_WATCHDOG
 
 	  If unsure, say N.
 
+config TEST_STATIC_DEBUGOBJECT
+	tristate "KUnit test for static debugobject"
+	depends on KUNIT
+	select KPROBES
+	select DEBUG_OBJECTS
+	select DEBUG_OBJECTS_TIMERS
+	help
+	  This builds the static debugobject unit test, which runs on boot.
+	  Tests the static debug object correctness.
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
lib/Makefile +2 -0
index baf2821f7a00f..f663686beabd9 100644
@@ -427,3 +427,5 @@ $(obj)/$(TEST_FORTIFY_LOG): $(addprefix $(obj)/, $(TEST_FORTIFY_LOGS)) FORCE
 ifeq ($(CONFIG_FORTIFY_SOURCE),y)
 $(obj)/string.o: $(obj)/$(TEST_FORTIFY_LOG)
 endif
+
+obj-$(CONFIG_TEST_STATIC_DEBUGOBJECT) += test_static_debug_object.o
lib/test_static_debug_object.c new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.39.2
author
#2 lore raw

Re: [PATCH 2/2] debugobject: add unit test for static debug object

Quoted 12 lines · Schspa Shi

Add test case to enusre that static debug object correctness.

Tested on little-endian arm64 qemu, result:

[    2.385735] KTAP version 1
[    2.385860] 1..1
[    2.386406]     KTAP version 1
[    2.386658]     # Subtest: static debugobject init
[    2.386726]     1..1
[    2.401777]     ok 1 static_debugobject_test
[    2.402455] ok 1 static debugobject init
Quoted 8 lines
---
 MAINTAINERS                    |   5 ++
 lib/Kconfig.debug              |  14 ++++
 lib/Makefile                   |   2 +
 lib/test_static_debug_object.c | 125 +++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 lib/test_static_debug_object.c

Hi tglx:

What do you think about this test case? Should we need it ? There are some platform compatibility issues here that need a little optimization.

Signature · 2 lines
BRs
Schspa Shi
#3 lore raw

Re: [PATCH 2/2] debugobject: add unit test for static debug object

Quoted 10 lines · Schspa Shi
Quoted 6 lines · Schspa Shi
 MAINTAINERS                    |   5 ++
 lib/Kconfig.debug              |  14 ++++
 lib/Makefile                   |   2 +
 lib/test_static_debug_object.c | 125 +++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 lib/test_static_debug_object.c

What do you think about this test case? Should we need it ? There are some platform compatibility issues here that need a little optimization.

What does it buy over the existing self test. Nothing AFACIT aside of extra code.

Thanks,

tglx

author
#4 lore raw

Re: [PATCH 2/2] debugobject: add unit test for static debug object

Quoted 9 lines · Thomas Gleixner
Quoted 8 lines · Schspa Shi
Quoted 6 lines · Schspa Shi
 MAINTAINERS                    |   5 ++
 lib/Kconfig.debug              |  14 ++++
 lib/Makefile                   |   2 +
 lib/test_static_debug_object.c | 125 +++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 lib/test_static_debug_object.c
Quoted 6 lines
Quoted 2 lines

What do you think about this test case? Should we need it ? There are some platform compatibility issues here that need a little optimization.

What does it buy over the existing self test. Nothing AFACIT aside of extra code.

It checks the race of the is_static_object() call in the previous BUG. This test can used to make sure the new fix patch works. The existing self test have no ability to check this.

Quoted 3 lines

Thanks,

tglx

Signature · 2 lines
BRs
Schspa Shi
#5 lore raw

Re: [PATCH 2/2] debugobject: add unit test for static debug object

Quoted 5 lines · Schspa Shi
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines · Schspa Shi

What do you think about this test case? Should we need it ? There are some platform compatibility issues here that need a little optimization.

Quoted 7 lines
Quoted 3 lines

What does it buy over the existing self test. Nothing AFACIT aside of extra code.

It checks the race of the is_static_object() call in the previous BUG. This test can used to make sure the new fix patch works. The existing self test have no ability to check this.

Sure it can somehow make sure that it works.

Don't misunderstand me. I'm all for self tests, but this one really falls into the category of testing the obvious. There are tons of more interesting places in the kernel which lack self tests than this particular oddity which is well understood and more than unlikely to come back.

That said, it would be valuable if you could add your Tested-by to the final patch, which will be queued up soonish.

Thanks,

tglx

#6 lore raw
In reply to Schspa Shi
Quoted 57 lines · Schspa Shi

The is_static_object implementation relay on the initial state of the object. If multiple places are accessed concurrently, there is a probability that the debug object has been registered in the system, which will invalidate the judgment of is_static_object.

The following is the scenario where the problem occurs:

T0 T1
=========================================================================

mod_timer();
   debug_object_assert_init
	db = get_bucket((unsigned long) addr);
	raw_spin_lock_irqsave(&db->lock, flags);
	obj = lookup_object(addr, db);
     if (!obj) {
		raw_spin_unlock_irqrestore(&db->lock, flags);
         << Context switch >>
                                              mod_timer();
                                                debug_object_assert_init
                                                ...
                                                enqueue_timer();
         /*
          * The initial state changed a static timer object, and
          * is_static_object will return false
          */

         if (descr->is_static_object &&
			descr->is_static_object(addr)) {
                 debug_object_init();
             } else {
                << Hit here for a static object >>
			   debug_print_object(&o, "assert_init");
			   debug_object_fixup(descr->fixup_assert_init, addr,
					   ODEBUG_STATE_NOTAVAILABLE);
             }
     }

To fix it, we got the is_static_object called within db->lock, and save it's state to struct debug_obj. This will ensure we won't hit the code branch not belong to the static object.

For the same static object, debug_object_init may enter multiple times, but there is a lock in debug_object_init to ensure that there is no problem.

---
  include/linux/debugobjects.h |  1 +
  lib/debugobjects.c           | 71 ++++++++++++++++++++++++++++--------
  2 files changed, 56 insertions(+), 16 deletions(-)
include/linux/debugobjects.h +0 -0
index 32444686b6ff4..544a6111b97f6 100644

include/linux/debugobjects.h struct debug_obj { · line 35

Quoted 6 lines
include/linux/debugobjects.h +1 -0
@@ -30,6 +30,7 @@ struct debug_obj {
  	enum debug_obj_state	state;
  	unsigned int		astate;
  	void			*object;
+	bool			is_static;
  	const struct debug_obj_descr *descr;
  };

The patch looks reasonable. My main concern is the increase in size of the debug_obj structure. It is an additional 8 bytes on 64-bit arches. How much will we save performance-wise by caching it in the debug_obj. Alternatively, you may pack it within the current size by, maybe, reducing the size of state.

Cheers,
Longman

Quoted 160 lines
lib/debugobjects.c +55 -16
index df86e649d8be0..d1be18158a1f7 100644
@@ -275,6 +275,8 @@ alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *d
  		obj->descr  = descr;
  		obj->state  = ODEBUG_STATE_NONE;
  		obj->astate = 0;
+		obj->is_static = descr->is_static_object &&
+			descr->is_static_object(addr);
  		hlist_add_head(&obj->node, &b->list);
  	}
  	return obj;
@@ -581,7 +583,16 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
  			debug_objects_oom();
  			return;
  		}
+
  		check_stack = true;
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;
+		}
  	}
 
  	switch (obj->state) {
@@ -640,6 +651,29 @@ void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
  }
  EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
 
+/*
+ * Check static object.
+ */
+static bool debug_object_check_static(struct debug_bucket *db, void *addr,
+			const struct debug_obj_descr *descr)
+{
+	struct debug_obj *obj;
+
+	/*
+	 * The is_static_object implementation relay on the initial state of the
+	 * object. If multiple places are accessed concurrently, there is a
+	 * probability that the debug object has been registered in the system,
+	 * which will invalidate the judgment of is_static_object.
+	 */
+	lockdep_assert_held(&db->lock);
+
+	obj = lookup_object(addr, db);
+	if (likely(obj))
+		return obj->is_static;
+
+	return descr->is_static_object && descr->is_static_object(addr);
+}
+
  /**
   * debug_object_activate - debug checks when an object is activated
   * @addr:	address of the object
@@ -656,6 +690,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  	struct debug_obj o = { .object = addr,
  			       .state = ODEBUG_STATE_NOTAVAILABLE,
  			       .descr = descr };
+	bool is_static;
 
  	if (!debug_objects_enabled)
  		return 0;
@@ -696,6 +731,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  		return ret;
  	}
 
+	is_static = debug_object_check_static(db, addr, descr);
  	raw_spin_unlock_irqrestore(&db->lock, flags);
 
  	/*
@@ -705,7 +741,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  	 * static object is tracked in the object tracker. If
  	 * not, this must be a bug, so we try to fix it up.
  	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
+	if (is_static) {
  		/* track this static object */
  		debug_object_init(addr, descr);
  		debug_object_activate(addr, descr);
@@ -872,6 +908,7 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
+	bool is_static;
 
  	if (!debug_objects_enabled)
  		return;
@@ -886,13 +923,14 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
 
+		is_static = debug_object_check_static(db, addr, descr);
  		raw_spin_unlock_irqrestore(&db->lock, flags);
  		/*
  		 * Maybe the object is static, and we let the type specific
  		 * code confirm. Track this static object if true, else invoke
  		 * fixup.
  		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
+		if (is_static) {
  			/* Track this static object */
  			debug_object_init(addr, descr);
  		} else {
@@ -1215,7 +1253,8 @@ static __initconst const struct debug_obj_descr descr_type_test = {
  	.fixup_free		= fixup_free,
  };
 
-static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };
 
  static void __init debug_objects_selftest(void)
  {
@@ -1256,26 +1295,26 @@ static void __init debug_objects_selftest(void)
  	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
 
-	obj.static_init = 1;
-	debug_object_activate(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	debug_object_activate(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
-	debug_object_init(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  		goto out;
-	debug_object_free(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
+	debug_object_free(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
 
  #ifdef CONFIG_DEBUG_OBJECTS_FREE
-	debug_object_init(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_INIT, fixups, warnings))
  		goto out;
-	debug_object_activate(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+	debug_object_activate(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
-	__debug_check_no_obj_freed(&obj, sizeof(obj));
-	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
+	__debug_check_no_obj_freed(&sobj, sizeof(sobj));
+	if (check_results(&sobj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  		goto out;
  #endif
  	pr_info("selftest passed\n");
author
#7 lore raw
Quoted 53 lines · Waiman Long
Quoted 52 lines · Schspa Shi

The is_static_object implementation relay on the initial state of the object. If multiple places are accessed concurrently, there is a probability that the debug object has been registered in the system, which will invalidate the judgment of is_static_object.

The following is the scenario where the problem occurs:

T0 T1
=========================================================================

mod_timer();
   debug_object_assert_init
	db = get_bucket((unsigned long) addr);
	raw_spin_lock_irqsave(&db->lock, flags);
	obj = lookup_object(addr, db);
     if (!obj) {
		raw_spin_unlock_irqrestore(&db->lock, flags);
         << Context switch >>
                                              mod_timer();
                                                debug_object_assert_init
                                                ...
                                                enqueue_timer();
         /*
          * The initial state changed a static timer object, and
          * is_static_object will return false
          */

         if (descr->is_static_object &&
			descr->is_static_object(addr)) {
                 debug_object_init();
             } else {
                << Hit here for a static object >>
			   debug_print_object(&o, "assert_init");
			   debug_object_fixup(descr->fixup_assert_init, addr,
					   ODEBUG_STATE_NOTAVAILABLE);
             }
     }

To fix it, we got the is_static_object called within db->lock, and save it's state to struct debug_obj. This will ensure we won't hit the code branch not belong to the static object.

For the same static object, debug_object_init may enter multiple times, but there is a lock in debug_object_init to ensure that there is no problem.

---
  include/linux/debugobjects.h |  1 +
  lib/debugobjects.c           | 71 ++++++++++++++++++++++++++++--------
  2 files changed, 56 insertions(+), 16 deletions(-)
Quoted 17 lines

include/linux/debugobjects.h struct debug_obj { · line 35

Quoted 11 lines
include/linux/debugobjects.h +1 -0
index 32444686b6ff4..544a6111b97f6 100644
@@ -30,6 +30,7 @@ struct debug_obj {
  	enum debug_obj_state	state;
  	unsigned int		astate;
  	void			*object;
+	bool			is_static;
  	const struct debug_obj_descr *descr;
  };

The patch looks reasonable. My main concern is the increase in size of the debug_obj structure. It is an additional 8 bytes on 64-bit arches. How much will we save performance-wise by caching it in the debug_obj. Alternatively, you may pack it within the current size by, maybe, reducing the size of state.

Yes, we can change this to:

struct debug_obj {
	struct hlist_node	node;
	struct {
		enum debug_obj_state	state : 31;
		bool					is_static : 1;
	};
	unsigned int		astate;
	void			*object;
	const struct debug_obj_descr *descr;
};

Which won't increase the object size.

(gdb) ptype /o struct debug_obj
/* offset | size */ type = struct debug_obj {

/*      0      |       0 */    struct hlist_node {
                                   <incomplete type>

                                   /* total size (bytes):    0 */
                               } node;
/*     16      |       4 */    struct {
/*     16: 0   |       4 */        enum debug_obj_state state : 31;
/*     19: 7   |       1 */        bool is_static : 1;
                                   /* total size (bytes):    4 */
                               };
/*     20      |       4 */    unsigned int astate;
/*     24      |       8 */    void *object;
/*     32      |       8 */    const struct debug_obj_descr *descr;
                               /* total size (bytes):   40 */
                             }
Quoted 151 lines

Cheers,
Longman

Quoted 148 lines
include/linux/debugobjects.h +0 -0
  diff --git a/lib/debugobjects.c b/lib/debugobjects.c

index df86e649d8be0..d1be18158a1f7 100644

lib/debugobjects.c +52 -14
@@ -275,6 +275,8 @@ alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *d
  		obj->descr  = descr;
  		obj->state  = ODEBUG_STATE_NONE;
  		obj->astate = 0;
+		obj->is_static = descr->is_static_object &&
+			descr->is_static_object(addr);
  		hlist_add_head(&obj->node, &b->list);
  	}
  	return obj;
@@ -581,7 +583,16 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
  			debug_objects_oom();
  			return;
  		}
+
  		check_stack = true;
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;
+		}
  	}
    	switch (obj->state) {
@@ -640,6 +651,29 @@ void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
  }
  EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
  +/*
+ * Check static object.
+ */
+static bool debug_object_check_static(struct debug_bucket *db, void *addr,
+			const struct debug_obj_descr *descr)
+{
+	struct debug_obj *obj;
+
+	/*
+	 * The is_static_object implementation relay on the initial state of the
+	 * object. If multiple places are accessed concurrently, there is a
+	 * probability that the debug object has been registered in the system,
+	 * which will invalidate the judgment of is_static_object.
+	 */
+	lockdep_assert_held(&db->lock);
+
+	obj = lookup_object(addr, db);
+	if (likely(obj))
+		return obj->is_static;
+
+	return descr->is_static_object && descr->is_static_object(addr);
+}
+
  /**
   * debug_object_activate - debug checks when an object is activated
   * @addr:	address of the object
@@ -656,6 +690,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  	struct debug_obj o = { .object = addr,
  			       .state = ODEBUG_STATE_NOTAVAILABLE,
  			       .descr = descr };
+	bool is_static;
    	if (!debug_objects_enabled)
  		return 0;
@@ -696,6 +731,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  		return ret;
  	}
  +	is_static = debug_object_check_static(db, addr, descr);
  	raw_spin_unlock_irqrestore(&db->lock, flags);
    	/*
@@ -705,7 +741,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
  	 * static object is tracked in the object tracker. If
  	 * not, this must be a bug, so we try to fix it up.
  	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
+	if (is_static) {
  		/* track this static object */
  		debug_object_init(addr, descr);
  		debug_object_activate(addr, descr);
@@ -872,6 +908,7 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
+	bool is_static;
    	if (!debug_objects_enabled)
  		return;
@@ -886,13 +923,14 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  +		is_static = debug_object_check_static(db, addr, descr);
  		raw_spin_unlock_irqrestore(&db->lock, flags);
  		/*
  		 * Maybe the object is static, and we let the type specific
  		 * code confirm. Track this static object if true, else invoke
  		 * fixup.
  		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
+		if (is_static) {
  			/* Track this static object */
  			debug_object_init(addr, descr);
  		} else {
@@ -1215,7 +1253,8 @@ static __initconst const struct debug_obj_descr descr_type_test = {
  	.fixup_free		= fixup_free,
  };
  -static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };
    static void __init debug_objects_selftest(void)
  {
@@ -1256,26 +1295,26 @@ static void __init debug_objects_selftest(void)
  	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
  -	obj.static_init = 1;
-	debug_object_activate(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	debug_object_activate(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
-	debug_object_init(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  		goto out;
-	debug_object_free(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
+	debug_object_free(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
    #ifdef CONFIG_DEBUG_OBJECTS_FREE
-	debug_object_init(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_INIT, fixups, warnings))
  		goto out;
-	debug_object_activate(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+	debug_object_activate(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
-	__debug_check_no_obj_freed(&obj, sizeof(obj));
-	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
+	__debug_check_no_obj_freed(&sobj, sizeof(sobj));
+	if (check_results(&sobj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  		goto out;
  #endif
  	pr_info("selftest passed\n");
Signature · 2 lines
BRs
Schspa Shi
#8 lore raw
Quoted 19 lines · Schspa Shi
Quoted 8 lines · Waiman Long
Quoted 8 lines
include/linux/debugobjects.h +0 -0
index 32444686b6ff4..544a6111b97f6 100644
@@ -30,6 +30,7 @@ struct debug_obj {
  	enum debug_obj_state	state;
  	unsigned int		astate;
  	void			*object;
Quoted 9 lines

include/linux/debugobjects.h struct debug_obj { · line 35

Quoted 3 lines
include/linux/debugobjects.h +1 -0
+	bool			is_static;
  	const struct debug_obj_descr *descr;
  };

The patch looks reasonable. My main concern is the increase in size of the debug_obj structure. It is an additional 8 bytes on 64-bit arches. How much will we save performance-wise by caching it in the debug_obj. Alternatively, you may pack it within the current size by, maybe, reducing the size of state.

Quoted 8 lines

Yes, we can change this to:

struct debug_obj {
	struct hlist_node	node;
	struct {
		enum debug_obj_state	state : 31;
		bool					is_static : 1;
	};

and thereby making debugobjects even more slower than it is right now. Please check the resulting assembly code before proposing quick "solutions".

Thanks,

tglx

#9 lore raw

On Sat, 04 Mar 2023 01:14:21 +0100 Thomas Gleixner <[email protected]>

Quoted 29 lines · Schspa Shi
Quoted 19 lines
Quoted 8 lines · Waiman Long
Quoted 8 lines
include/linux/debugobjects.h +0 -0
index 32444686b6ff4..544a6111b97f6 100644
@@ -30,6 +30,7 @@ struct debug_obj {
  	enum debug_obj_state	state;
  	unsigned int		astate;
  	void			*object;
Quoted 9 lines

include/linux/debugobjects.h struct debug_obj { · line 35

Quoted 3 lines
include/linux/debugobjects.h +1 -0
+	bool			is_static;
  	const struct debug_obj_descr *descr;
  };

The patch looks reasonable. My main concern is the increase in size of the debug_obj structure. It is an additional 8 bytes on 64-bit arches. How much will we save performance-wise by caching it in the debug_obj. Alternatively, you may pack it within the current size by, maybe, reducing the size of state.

Quoted 8 lines

Yes, we can change this to:

struct debug_obj {
	struct hlist_node	node;
	struct {
		enum debug_obj_state	state : 31;
		bool					is_static : 1;
	};
Quoted 7 lines

and thereby making debugobjects even more slower than it is right now. Please check the resulting assembly code before proposing quick "solutions".

Thanks,

tglx

Add a debug flag to timer instead in order to make timer_is_static_object() depend no longer on TIMER_ENTRY_STATIC. Then it always gives the correct answer in every context.

Hillf

+++ y/include/linux/timer.h
@@ -18,6 +18,10 @@ struct timer_list {
void (*function)(struct timer_list *);
u32 flags;

+#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
+ u32 is_static;
+#define TIMER_IS_STATIC_MAGIC 0xFBFBBEEF
+#endif
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
@@ -73,6 +77,17 @@ struct timer_list {

#define TIMER_TRACE_FLAGMASK (TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)

+
+#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
+#define __TIMER_INITIALIZER(_function, _flags) { \
+ .entry = { .next = TIMER_ENTRY_STATIC }, \
+ .function = (_function), \
+ .flags = (_flags), \
+ .is_static = TIMER_IS_STATIC_MAGIC, \
+ __TIMER_LOCKDEP_MAP_INITIALIZER( \
+ __FILE__ ":" __stringify(__LINE__)) \
+ }
+#else
#define __TIMER_INITIALIZER(_function, _flags) { \
.entry = { .next = TIMER_ENTRY_STATIC }, \
.function = (_function), \
@@ -80,6 +95,7 @@ struct timer_list {
__TIMER_LOCKDEP_MAP_INITIALIZER( \
__FILE__ ":" __stringify(__LINE__)) \
}
+#endif /* CONFIG_DEBUG_OBJECTS_TIMERS */

#define DEFINE_TIMER(_name, _function) \
struct timer_list _name = \
+++ y/kernel/time/timer.c
@@ -677,8 +677,7 @@ static bool timer_is_static_object(void
{
struct timer_list *timer = addr;

- return (timer->entry.pprev == NULL &&
- timer->entry.next == TIMER_ENTRY_STATIC);
+ return timer->is_static == TIMER_IS_STATIC_MAGIC;
}

/*
@@ -775,6 +774,7 @@ static const struct debug_obj_descr time
static inline void debug_timer_init(struct timer_list *timer) {
debug_object_init(timer, &timer_debug_descr);
+ timer->is_static = 0;
}

static inline void debug_timer_activate(struct timer_list *timer)
@@ -804,6 +804,7 @@ void init_timer_on_stack_key(struct time
{
debug_object_init_on_stack(timer, &timer_debug_descr); do_init_timer(timer, func, flags, name, key);
+ timer->is_static = 0;
}
EXPORT_SYMBOL_GPL(init_timer_on_stack_key);

#10 lore raw
In reply to Schspa Shi

Hi!

Quoted 28 lines · Schspa Shi

The is_static_object implementation relay on the initial state of the object. If multiple places are accessed concurrently, there is a probability that the debug object has been registered in the system, which will invalidate the judgment of is_static_object.

The following is the scenario where the problem occurs:

T0 T1
=========================================================================
mod_timer();
debug_object_assert_init

	db = get_bucket((unsigned long) addr);
	raw_spin_lock_irqsave(&db->lock, flags);
	obj = lookup_object(addr, db);
    if (!obj) {
		raw_spin_unlock_irqrestore(&db->lock, flags);
        << Context switch >>
                                             mod_timer();
                                               debug_object_assert_init
                                               ...
                                               enqueue_timer();
        /*
         * The initial state changed a static timer object, and
         * is_static_object will return false
         */

        if (descr->is_static_object &&
			descr->is_static_object(addr)) {
Quoted 8 lines
                debug_object_init();
            } else {
               << Hit here for a static object >>
			   debug_print_object(&o, "assert_init");
			   debug_object_fixup(descr->fixup_assert_init, addr,
					   ODEBUG_STATE_NOTAVAILABLE);
            }
    }

That analysis is correct.

Quoted 16 lines
lib/debugobjects.c +2 -0
index df86e649d8be0..d1be18158a1f7 100644
@@ -275,6 +275,8 @@ alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *d
 		obj->descr  = descr;
 		obj->state  = ODEBUG_STATE_NONE;
 		obj->astate = 0;
+		obj->is_static = descr->is_static_object &&
+			descr->is_static_object(addr);
 		hlist_add_head(&obj->node, &b->list);
 	}
 	return obj;
@@ -581,7 +583,16 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 			debug_objects_oom();
 			return;

lib/debugobjects.c __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack · line 594

Quoted 10 lines
lib/debugobjects.c +8 -0
 		}
+
 		check_stack = true;
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;

This is broken. If the object is static and already hashed and in active state then this returns and fails to detect the re-initialization of an active object.

Quoted 7 lines
lib/debugobjects.c +7 -0
+/*
+ * Check static object.
+ */
+static bool debug_object_check_static(struct debug_bucket *db, void *addr,
+			const struct debug_obj_descr *descr)
+{
+	struct debug_obj *obj;

lib/debugobjects.c __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack

Quoted 10 lines
lib/debugobjects.c +10 -0
+
+	/*
+	 * The is_static_object implementation relay on the initial state of the
+	 * object. If multiple places are accessed concurrently, there is a
+	 * probability that the debug object has been registered in the system,
+	 * which will invalidate the judgment of is_static_object.
+	 */
+	lockdep_assert_held(&db->lock);
+
+	obj = lookup_object(addr, db);

What does this lookup solve? See below...

lib/debugobjects.c int debug_object_activate(void *addr, const struct debug_obj_descr *descr) · line 734

Quoted 11 lines
lib/debugobjects.c +6 -0
+	if (likely(obj))
+		return obj->is_static;
+
+	return descr->is_static_object && descr->is_static_object(addr);
+}
@@ -696,6 +731,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 		return ret;
 	}
 
+	is_static = debug_object_check_static(db, addr, descr);

It's already clear that the object is not hashed because activate does:

        lock()
        lookup()
        if (ob) {
           ....
           unlock();
        }

At this point nothing has changed after the lookup because the hash bucket is still locked.

lib/debugobjects.c int debug_object_activate(void *addr, const struct debug_obj_descr *descr) · line 746

Quoted 11 lines
lib/debugobjects.c +1 -1
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
 	/*
@@ -705,7 +741,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 	 * static object is tracked in the object tracker. If
 	 * not, this must be a bug, so we try to fix it up.
 	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
+	if (is_static) {
 		/* track this static object */
 		debug_object_init(addr, descr);

This cannot work as the change in debug_object_init() is not correct and there is no way to make it correct.

Quoted 3 lines
lib/debugobjects.c +2 -1
-static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };

...

lib/debugobjects.c

Quoted 1 line
lib/debugobjects.c +0 -1
-	obj.static_init = 1;

Plus the s/obj/sobj/ which should be equivalent, unless I'm missing something here.

But that also changes the actual test:

- obj.static_init = 1;
- debug_object_activate(&obj, &descr_type_test);
- if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+ debug_object_init(&sobj, &descr_type_test);
+ debug_object_activate(&sobj, &descr_type_test);
+ if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))

		goto out;

That's _not_ equivalent.

The original code checks exactly for the case where the object initialization does not happen before debug_object_activate() is invoked which is perfectly correct for statically initialized objects. Initializing the statically allocated object breaks that test. The changelog is utterly silent about that.

The proper fix for this is obvious from the analysis. The problem happens because the failed lookup drops the hash bucket lock which opens the window for the concurrent caller. So why dropping the hash bucket lock at all?

Uncompiled and untested fix below.

Thanks,

tglx

---
 lib/debugobjects.c |  127 +++++++++++++++++++++++++++--------------------------
 1 file changed, 67 insertions(+), 60 deletions(-)
lib/debugobjects.c +67 -60
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -273,7 +269,7 @@ alloc_object(void *addr, struct debug_bu
 	if (obj) {
 		obj->object = addr;
 		obj->descr  = descr;
-		obj->state  = ODEBUG_STATE_NONE;
+		obj->state  = ODEBUG_STATE_INIT;
 		obj->astate = 0;
 		hlist_add_head(&obj->node, &b->list);
 	}
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(voi
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+	}
+
+	/*
+	 * On success the returned object is in INIT state as that's the
+	 * correct state for all callers.
+	 */
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ static void
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ static void
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_s
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocaction has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocaction has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);
 
author
#11 lore raw
Quoted 65 lines · Thomas Gleixner

Hi!

Quoted 28 lines · Schspa Shi

The is_static_object implementation relay on the initial state of the object. If multiple places are accessed concurrently, there is a probability that the debug object has been registered in the system, which will invalidate the judgment of is_static_object.

The following is the scenario where the problem occurs:

T0 T1
=========================================================================
mod_timer();
debug_object_assert_init

	db = get_bucket((unsigned long) addr);
	raw_spin_lock_irqsave(&db->lock, flags);
	obj = lookup_object(addr, db);
    if (!obj) {
		raw_spin_unlock_irqrestore(&db->lock, flags);
        << Context switch >>
                                             mod_timer();
                                               debug_object_assert_init
                                               ...
                                               enqueue_timer();
        /*
         * The initial state changed a static timer object, and
         * is_static_object will return false
         */

        if (descr->is_static_object &&
			descr->is_static_object(addr)) {
Quoted 8 lines
                debug_object_init();
            } else {
               << Hit here for a static object >>
			   debug_print_object(&o, "assert_init");
			   debug_object_fixup(descr->fixup_assert_init, addr,
					   ODEBUG_STATE_NOTAVAILABLE);
            }
    }

That analysis is correct.

Quoted 22 lines
lib/debugobjects.c +6 -0
index df86e649d8be0..d1be18158a1f7 100644
@@ -275,6 +275,8 @@ alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *d
 		obj->descr  = descr;
 		obj->state  = ODEBUG_STATE_NONE;
 		obj->astate = 0;
+		obj->is_static = descr->is_static_object &&
+			descr->is_static_object(addr);
 		hlist_add_head(&obj->node, &b->list);
 	}
 	return obj;
@@ -581,7 +583,16 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 			debug_objects_oom();
 			return;
 		}
+
 		check_stack = true;
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
Quoted 9 lines

lib/debugobjects.c static void · line 613

Quoted 4 lines
lib/debugobjects.c +4 -0
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;

This is broken. If the object is static and already hashed and in active state then this returns and fails to detect the re-initialization of an active object.

Yes, it's right, this can be fixed by pass a skip_ifstatic parameters from debug_object_activate. then re-initialization of an active object can be detected.

Quoted 11 lines
Quoted 11 lines
lib/debugobjects.c +11 -0
+/*
+ * Check static object.
+ */
+static bool debug_object_check_static(struct debug_bucket *db, void *addr,
+			const struct debug_obj_descr *descr)
+{
+	struct debug_obj *obj;
+
+	/*
+	 * The is_static_object implementation relay on the initial state of the
+	 * object. If multiple places are accessed concurrently, there is a
Quoted 9 lines

lib/debugobjects.c

Quoted 6 lines
lib/debugobjects.c +6 -0
+	 * probability that the debug object has been registered in the system,
+	 * which will invalidate the judgment of is_static_object.
+	 */
+	lockdep_assert_held(&db->lock);
+
+	obj = lookup_object(addr, db);

What does this lookup solve? See below...

This lookup can be droped by add a extra obj parameter.

Quoted 21 lines

lib/debugobjects.c int debug_object_activate(void *addr, const struct debug_obj_descr *descr) · line 734

Quoted 11 lines
lib/debugobjects.c +6 -0
+	if (likely(obj))
+		return obj->is_static;
+
+	return descr->is_static_object && descr->is_static_object(addr);
+}
@@ -696,6 +731,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 		return ret;
 	}
 
+	is_static = debug_object_check_static(db, addr, descr);

It's already clear that the object is not hashed because activate does:

        lock()
        lookup()
        if (ob) {
           ....
           unlock();
        }
Quoted 3 lines

At this point nothing has changed after the lookup because the hash bucket is still locked.

Yes, the obj variable should be passed to debug_object_check_static.

Quoted 15 lines

lib/debugobjects.c int debug_object_activate(void *addr, const struct debug_obj_descr *descr) · line 746

Quoted 11 lines
lib/debugobjects.c +1 -1
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
 	/*
@@ -705,7 +741,7 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 	 * static object is tracked in the object tracker. If
 	 * not, this must be a bug, so we try to fix it up.
 	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
+	if (is_static) {
 		/* track this static object */
 		debug_object_init(addr, descr);

This cannot work as the change in debug_object_init() is not correct and there is no way to make it correct.

Function debug_object_init() can be fixed as explained above.

Quoted 11 lines
Quoted 3 lines
lib/debugobjects.c +2 -1
-static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };

...

lib/debugobjects.c

Quoted 1 line
lib/debugobjects.c +0 -1
-	obj.static_init = 1;

Plus the s/obj/sobj/ which should be equivalent, unless I'm missing something here.

We have saved the is_static state when it is used at the first time, so the is_static_object function won't be called in this environment.

Quoted 10 lines

But that also changes the actual test:

(quoted diff) +3 -3
-	obj.static_init = 1;
-	debug_object_activate(&obj, &descr_type_test);
-	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
+	debug_object_init(&sobj, &descr_type_test);
+	debug_object_activate(&sobj, &descr_type_test);
+	if (check_results(&sobj, ODEBUG_STATE_ACTIVE, fixups, warnings))
		goto out;
Quoted 8 lines

That's _not_ equivalent.

The original code checks exactly for the case where the object initialization does not happen before debug_object_activate() is invoked which is perfectly correct for statically initialized objects. Initializing the statically allocated object breaks that test. The changelog is utterly silent about that.

This debug_object_init(&sobj, &descr_type_test) can be droped. I'm sorry to add this extra change.

Quoted 28 lines

The proper fix for this is obvious from the analysis. The problem happens because the failed lookup drops the hash bucket lock which opens the window for the concurrent caller. So why dropping the hash bucket lock at all?

Uncompiled and untested fix below.

Thanks,

tglx

---
 lib/debugobjects.c |  127 +++++++++++++++++++++++++++--------------------------
 1 file changed, 67 insertions(+), 60 deletions(-)
lib/debugobjects.c +0 -4
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {

lib/debugobjects.c alloc_object(void *addr, struct debug_bu · line 272

Quoted 5 lines
lib/debugobjects.c +1 -1
@@ -273,7 +269,7 @@ alloc_object(void *addr, struct debug_bu
 	if (obj) {
 		obj->object = addr;
 		obj->descr  = descr;
-		obj->state  = ODEBUG_STATE_NONE;
+		obj->state  = ODEBUG_STATE_INIT;

This actually droped the ODEBUG_STATE_NONE state. If we active a uninitialized object, there will be no error report.

This should be

if (descr->is_static_object && descr->is_static_object(addr))

	obj->state  = ODEBUG_STATE_INIT;
else
	obj->state  = ODEBUG_STATE_NONE;

But this can't resolve the initial state requirement from the is_static_object() call.

I think we can report an error when calling debug_object_free() from a static object. If don't do so, there is no way to determine it's a static object. When its initialization state changes, the is_static_object() call will return the wrong value.

Please see the fellowing test case:

	obj.static_init = 1;
	debug_object_activate(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
		goto out;
	debug_object_init(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
		goto out;
	debug_object_free(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
		goto out;

    /*
     * for the static debugobject, it's initial value will be changed
     * once used.
     */
	obj.static_init = 2;
	debug_object_activate(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
		goto out;

    /* This test will fail */
Quoted 185 lines
lib/debugobjects.c +63 -54
 		obj->astate = 0;
 		hlist_add_head(&obj->node, &b->list);
 	}
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(voi
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+	}
+
+	/*
+	 * On success the returned object is in INIT state as that's the
+	 * correct state for all callers.
+	 */
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ static void
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ static void
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_s
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocaction has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocaction has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();

lib/debugobjects.c void debug_object_assert_init(void *addr · line 916

Quoted 10 lines
lib/debugobjects.c +3 -1
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);
 

I test this patch, with my above change, and it seems to work well, but we still need to add extra flags to store its static state. And debug_object_free() should report an error for the static object.

I think we should introduce lookup_object_or_alloc and is_static at the same time.

Signature · 2 lines
BRs
Schspa Shi
#12 lore raw
Quoted 24 lines · Schspa Shi
Quoted 12 lines · Thomas Gleixner

Review

Quoted 7 lines
(quoted diff) +7 -0
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;

This is broken. If the object is static and already hashed and in active state then this returns and fails to detect the re-initialization of an active object.

Yes, it's right, this can be fixed by pass a skip_ifstatic parameters from debug_object_activate. then re-initialization of an active object can be detected.

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -1
-static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };

...

Quoted 8 lines
Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +0 -1
-	obj.static_init = 1;

Plus the s/obj/sobj/ which should be equivalent, unless I'm missing something here.

We have saved the is_static state when it is used at the first time, so the is_static_object function won't be called in this environment.

There is zero requirement for saving that state.

Quoted 9 lines
Quoted 9 lines
 lib/debugobjects.c |  127 +++++++++++++++++++++++++++--------------------------
 1 file changed, 67 insertions(+), 60 deletions(-)
lib/debugobjects.c +0 -0
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
Quoted 16 lines

lib/debugobjects.c alloc_object(void *addr, struct debug_bu · line 272

Quoted 13 lines
lib/debugobjects.c +1 -5
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -273,7 +269,7 @@ alloc_object(void *addr, struct debug_bu
 	if (obj) {
 		obj->object = addr;
 		obj->descr  = descr;
-		obj->state  = ODEBUG_STATE_NONE;
+		obj->state  = ODEBUG_STATE_INIT;

This actually droped the ODEBUG_STATE_NONE state. If we active a uninitialized object, there will be no error report.

Indeed.

Quoted 6 lines

This should be

if (descr->is_static_object && descr->is_static_object(addr))

	obj->state  = ODEBUG_STATE_INIT;
else
	obj->state  = ODEBUG_STATE_NONE;

Kinda.

Quoted 2 lines

But this can't resolve the initial state requirement from the is_static_object() call.

Which requirement? The is_static_object() call takes the address of the actual object and has nothing to do with the tracking object at all.

Quoted 3 lines

I think we can report an error when calling debug_object_free() from a static object. If don't do so, there is no way to determine it's a static object.

The memory allocator will tell you loudly when you try to free a static object. So no point in having another check.

Quoted 2 lines

When its initialization state changes, the is_static_object() call will return the wrong value.

That call is only relevant on the first invocation when there is no tracking object yet. So what's the problem you are trying to solve?

Quoted 3 lines

Please see the fellowing test case:

	obj.static_init = 1;

This is pointless, really. Once the object is tracked it does not matter at all whether it was statically or dynamically allocated.

Quoted 4 lines

I test this patch, with my above change, and it seems to work well, but we still need to add extra flags to store its static state. And debug_object_free() should report an error for the static object.

No, we don't.

Quoted 2 lines

I think we should introduce lookup_object_or_alloc and is_static at the same time.

What for?

Thanks,

tglx

#13 lore raw
Quoted 5 lines · Thomas Gleixner
Quoted 2 lines · Schspa Shi

I think we should introduce lookup_object_or_alloc and is_static at the same time.

What for?

The below has the NONE/INIT issue addressed and plugs that race completely, so no further action required.

Thanks,

tglx
---

lib/debugobjects.c +66 -59
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(voi
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+	enum debug_obj_state state = ODEBUG_STATE_NONE;
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+		/* Statically allocated objects are considered initialized */
+		state = ODEBUG_STATE_INIT;
+	}
+
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		obj->state = state;
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ static void
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ static void
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_s
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocaction has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocaction has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);
 
author
#14 lore raw
Quoted 209 lines · Thomas Gleixner
Quoted 5 lines · Thomas Gleixner
Quoted 2 lines · Schspa Shi

I think we should introduce lookup_object_or_alloc and is_static at the same time.

What for?

The below has the NONE/INIT issue addressed and plugs that race completely, so no further action required.

Thanks,

tglx
---

lib/debugobjects.c +63 -58
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(voi
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+	enum debug_obj_state state = ODEBUG_STATE_NONE;
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+		/* Statically allocated objects are considered initialized */
+		state = ODEBUG_STATE_INIT;
+	}
+
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		obj->state = state;
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ static void
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ static void
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_s
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocaction has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocaction has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();

lib/debugobjects.c void debug_object_assert_init(void *addr · line 916

Quoted 10 lines
lib/debugobjects.c +3 -1
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);
 

It's OK if you don't think debug_object_free() should report a error when the object is static. But I still think we should report an error and modify the autotest case in debug_objects_selftest() to remove debug_object_free() call on the static object.

Signature · 2 lines
BRs
Schspa Shi
#15 lore raw

PATCH 1/1 debugobject: Prevent init race with static objects

In reply to Thomas Gleixner

Statically initialized objects are usually not initialized via the init() function of the subsystem. They are special cased and the subsystem provides a function to validate whether an object which is not yet tracked by debugobjects is statically initialized. This means the object is started to be tracked on first use, e.g. activation.

This works perfectly fine, unless there are two concurrent operations on that object. Schspa decoded the problem:

T0 T1

debug_object_assert_init(addr)
lock_hash_bucket()
obj = lookup_object(addr);
if (!obj) {

  	unlock_hash_bucket();
	- > preemption
			            lock_subsytem_object(addr);
				      activate_object(addr)
				      lock_hash_bucket();
				      obj = lookup_object(addr);
				      if (!obj) {
				    	unlock_hash_bucket();
					if (is_static_object(addr))
					   init_and_track(addr);
				      lock_hash_bucket();
				      obj = lookup_object(addr);
				      obj->state = ACTIVATED;
				      unlock_hash_bucket();

				    subsys function modifies content of addr,
				    so static object detection does
				    not longer work.

				    unlock_subsytem_object(addr);
				    
        if (is_static_object(addr)) <- Fails

	  debugobject emits a warning and invokes the fixup function which
	  reinitializes the already active object in the worst case.

This race exists forever, but was never observed until mod_timer() got a debug_object_assert_init() added which is outside of the timer base lock held section right at the beginning of the function to cover the lockless early exit points too.

Rework the code so that the lookup, the static object check and the tracking object association happens atomically under the hash bucket lock. This prevents the issue completely as all callers are serialized on the hash bucket lock and therefore cannot observe inconsistent state.

---
 lib/debugobjects.c |  125 +++++++++++++++++++++++++++--------------------------
 1 file changed, 66 insertions(+), 59 deletions(-)
lib/debugobjects.c +66 -59
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(voi
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+	enum debug_obj_state state = ODEBUG_STATE_NONE;
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+		/* Statically allocated objects are considered initialized */
+		state = ODEBUG_STATE_INIT;
+	}
+
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		obj->state = state;
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ static void
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ static void
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_s
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocaction has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocaction has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);
 
Reviewed-by on v1 1/1
#16 lore raw

Re: [PATCH] debugobject: Prevent init race with static objects

Quoting Thomas Gleixner (2023-04-12 00:54:39)

Quoted 2 lines

Statically initialized objects are usually not initialized via the init() function of the subsystem. They are special cased and the subsystem

[...]

Quoted 6 lines

Rework the code so that the lookup, the static object check and the tracking object association happens atomically under the hash bucket lock. This prevents the issue completely as all callers are serialized on the hash bucket lock and therefore cannot observe inconsistent state.

Quoted 7 lines

---

Tiny nitpick below

Quoted 14 lines
lib/debugobjects.c +0 -7
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, co
 
        raw_spin_unlock_irqrestore(&db->lock, flags);
 
-       /*
-        * We are here when a static object is activated. We
-        * let the type specific code confirm whether this is
-        * true or not. if true, we just make sure that the
-        * static object is tracked in the object tracker. If
-        * not, this must be a bug, so we try to fix it up.
-        */

lib/debugobjects.c int debug_object_activate(void *addr, co · line 726

Quoted 10 lines
lib/debugobjects.c +1 -9
-       if (descr->is_static_object && descr->is_static_object(addr)) {
-               /* track this static object */
-               debug_object_init(addr, descr);
-               debug_object_activate(addr, descr);
-       } else {
-               debug_print_object(&o, "activate");
-               ret = debug_object_fixup(descr->fixup_activate, addr,
-                                       ODEBUG_STATE_NOTAVAILABLE);
-               return ret ? 0 : -EINVAL;
+       /* If NULL the allocaction has hit OOM */

s/allocaction/allocation/

Or is this "alloc action"? Or should it be "lookup_object_or_alloc() has hit OOM"?

Quoted 14 lines
lib/debugobjects.c +8 -1
+       if (!obj) {
+               debug_objects_oom();
+               return 0;
        }
-       return 0;
+
+       /* Object is neither static nor tracked. It's not initialized */
+       debug_print_object(&o, "activate");
+       ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+       return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 

lib/debugobjects.c void debug_object_assert_init(void *addr · line 905

Quoted 10 lines
lib/debugobjects.c +5 -1
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr
        db = get_bucket((unsigned long) addr);
 
        raw_spin_lock_irqsave(&db->lock, flags);
+       obj = lookup_object_or_alloc(addr, db, descr, false, true);
+       raw_spin_unlock_irqrestore(&db->lock, flags);
+       if (likely(!IS_ERR_OR_NULL(obj)))
+               return;
 
-       obj = lookup_object(addr, db);
+       /* If NULL the allocaction has hit OOM */

Same comment.

#17 lore raw

Re: [PATCH] debugobject: Prevent init race with static objects

Quoted 4 lines · Stephen Boyd

Quoting Thomas Gleixner (2023-04-12 00:54:39)

Quoted 1 line

+ /* If NULL the allocaction has hit OOM */

s/allocaction/allocation/

That's what I probably wanted to type.

Quoted 4 lines

lib/debugobjects.c void debug_object_assert_init(void *addr · line 905

Quoted 2 lines
lib/debugobjects.c +1 -1
-       obj = lookup_object(addr, db);
+       /* If NULL the allocaction has hit OOM */

Same comment.

The wonders of copy & pasta!

Thanks for the review!

tglx

#18 lore raw

Re: [PATCH] debugobject: Prevent init race with static objects

In reply to Thomas Gleixner
Quoted 8 lines · Thomas Gleixner

Statically initialized objects are usually not initialized via the init() function of the subsystem. They are special cased and the subsystem provides a function to validate whether an object which is not yet tracked by debugobjects is statically initialized. This means the object is started to be tracked on first use, e.g. activation.

This works perfectly fine, unless there are two concurrent operations on that object. Schspa decoded the problem:

[...]

Quoted 10 lines

This race exists forever, but was never observed until mod_timer() got a debug_object_assert_init() added which is outside of the timer base lock held section right at the beginning of the function to cover the lockless early exit points too.

Rework the code so that the lookup, the static object check and the tracking object association happens atomically under the hash bucket lock. This prevents the issue completely as all callers are serialized on the hash bucket lock and therefore cannot observe inconsistent state.

Quoted 6 lines

Thomas,

With this patch we see the following warning in the kernel log during boot:

"ODEBUG: Out of memory. ODEBUG disabled"

In which case, the stats are:

# cat /sys/kernel/debug/debug_objects/stats
max_chain     :24
max_checked   :37
warnings      :0
fixups        :0
pool_free     :4297
pool_pcp_free :84
pool_min_free :0
pool_used     :0
pool_max_used :6615
on_free_list  :0
objs_allocated:15616
objs_freed    :11319

If I revert this patch, the warning disappears and I see the following stats:

# cat /sys/kernel/debug/debug_objects/stats
max_chain     :25
max_checked   :40
warnings      :0
fixups        :0
pool_free     :1219
pool_pcp_free :209
pool_min_free :289
pool_used     :1578
pool_max_used :8026
on_free_list  :0
objs_allocated:32304
objs_freed    :29507

The following diff seems to solve the problem for me:

lib/debugobjects.c +1 -1
index b796799fadb2..af4bd66c571c 100644
@@ -21,7 +21,7 @@
 #define ODEBUG_HASH_BITS       14
 #define ODEBUG_HASH_SIZE       (1 << ODEBUG_HASH_BITS)
 
-#define ODEBUG_POOL_SIZE       1024
+#define ODEBUG_POOL_SIZE       (16 * 1024)
 #define ODEBUG_POOL_MIN_LEVEL  256
 #define ODEBUG_POOL_PERCPU_SIZE        64
 #define ODEBUG_BATCH_SIZE      16

In which case, the stats are:

# cat /sys/kernel/debug/debug_objects/stats 
max_chain     :28
max_checked   :64
warnings      :0
fixups        :0
pool_free     :14789
pool_pcp_free :192
pool_min_free :8120
pool_used     :1595
pool_max_used :8264
on_free_list  :0
objs_allocated:16384
objs_freed    :0

I'm not familiar with the debugobjects code, but maybe it makes sense to make "ODEBUG_POOL_SIZE" configurable via Kconfig in a similar fashion to "CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE"?

Please let me know if more information is required or if you want me to test a patch.

Thanks

#19 lore raw

Re: [PATCH] debugobject: Prevent init race with static objects

Ido!

Quoted 5 lines · Ido Schimmel

On Wed, Apr 12, 2023 at 09:54:39AM +0200, Thomas Gleixner wrote:
With this patch we see the following warning in the kernel log during boot:

"ODEBUG: Out of memory. ODEBUG disabled"

...

lib/debugobjects.c line 24

Quoted 12 lines

The following diff seems to solve the problem for me:

lib/debugobjects.c +1 -1
index b796799fadb2..af4bd66c571c 100644
@@ -21,7 +21,7 @@
 #define ODEBUG_HASH_BITS       14
 #define ODEBUG_HASH_SIZE       (1 << ODEBUG_HASH_BITS)
 
-#define ODEBUG_POOL_SIZE       1024
+#define ODEBUG_POOL_SIZE       (16 * 1024)

That's a big hammer :)

Quoted 3 lines

I'm not familiar with the debugobjects code, but maybe it makes sense to make "ODEBUG_POOL_SIZE" configurable via Kconfig in a similar fashion to "CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE"?

I don't think so.

The change in that patch is neither debug_objects_activate() nor debug_objecs_assert_init() no longer invoke debug_object_init() which is now the only place doing pool refills. So depending on the number of statically allocated objects this might deplete the pool quick enough.

Does the patch below restore the old behaviour?

Thanks,

tglx
---

lib/debugobjects.c +15 -6
index b796799fadb2..003edc5ebd67 100644
@@ -587,6 +587,16 @@ static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket
 	return NULL;
 }
 
+static void debug_objects_fill_pool(void)
+{
+	/*
+	 * On RT enabled kernels the pool refill must happen in preemptible
+	 * context:
+	 */
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
+		fill_pool();
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
@@ -595,12 +605,7 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 	struct debug_obj *obj;
 	unsigned long flags;
 
-	/*
-	 * On RT enabled kernels the pool refill must happen in preemptible
-	 * context:
-	 */
-	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
-		fill_pool();
+	debug_objects_fill_pool();
 
 	db = get_bucket((unsigned long) addr);
 
@@ -685,6 +690,8 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 	if (!debug_objects_enabled)
 		return 0;
 
+	debug_objects_fill_pool();
+
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
@@ -894,6 +901,8 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 	if (!debug_objects_enabled)
 		return;
 
+	debug_objects_fill_pool();
+
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
Tested-by
#20 lore raw

Re: [PATCH] debugobject: Prevent init race with static objects

Quoted 6 lines · Thomas Gleixner

The change in that patch is neither debug_objects_activate() nor debug_objecs_assert_init() no longer invoke debug_object_init() which is now the only place doing pool refills. So depending on the number of statically allocated objects this might deplete the pool quick enough.

Does the patch below restore the old behaviour?

Yes. Couldn't reproduce the issue with the proposed fix. Feel free to add:

Thanks for the quick fix!

#21 lore raw

[tip: core/debugobjects] debugobject: Ensure pool refill (again)

In reply to Thomas Gleixner
The following commit has been merged into the core/debugobjects branch of tip:

The following commit has been merged into the core/debugobjects branch of tip:

Commit-ID:     0af462f19e635ad522f28981238334620881badc
Gitweb:        https://git.kernel.org/tip/0af462f19e635ad522f28981238334620881badc
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Mon, 01 May 2023 17:42:06 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 02 May 2023 10:07:04 +02:00

debugobject: Ensure pool refill (again)

The recent fix to ensure atomicity of lookup and allocation inadvertently broke the pool refill mechanism.

Prior to that change debug_objects_activate() and debug_objecs_assert_init() invoked debug_objecs_init() to set up the tracking object for statically initialized objects. That's not longer the case and debug_objecs_init() is now the only place which does pool refills.

Depending on the number of statically initialized objects this can be enough to actually deplete the pool, which was observed by Ido via a debugobjects OOM warning.

Restore the old behaviour by adding explicit refill opportunities to debug_objects_activate() and debug_objecs_assert_init().

Fixes 63a759694eed ("debugobject: Prevent init race with static objects")
---
 lib/debugobjects.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
lib/debugobjects.c +15 -6
index b796799..003edc5 100644
@@ -587,6 +587,16 @@ static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket 
 	return NULL;
 }
 
+static void debug_objects_fill_pool(void)
+{
+	/*
+	 * On RT enabled kernels the pool refill must happen in preemptible
+	 * context:
+	 */
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
+		fill_pool();
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
@@ -595,12 +605,7 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 	struct debug_obj *obj;
 	unsigned long flags;
 
-	/*
-	 * On RT enabled kernels the pool refill must happen in preemptible
-	 * context:
-	 */
-	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
-		fill_pool();
+	debug_objects_fill_pool();
 
 	db = get_bucket((unsigned long) addr);
 
@@ -685,6 +690,8 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 	if (!debug_objects_enabled)
 		return 0;
 
+	debug_objects_fill_pool();
+
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
@@ -894,6 +901,8 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 	if (!debug_objects_enabled)
 		return;
 
+	debug_objects_fill_pool();
+
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
author
#22 lore raw
In reply to Thomas Gleixner
Quoted 71 lines · Thomas Gleixner
Quoted 24 lines · Schspa Shi
Quoted 12 lines · Thomas Gleixner

Review

Quoted 7 lines
(quoted diff) +7 -0
+	} else {
+		/*
+		 * The debug object is inited, and we should check this again
+		 */
+		if (obj->is_static) {
+			raw_spin_unlock_irqrestore(&db->lock, flags);
+			return;

This is broken. If the object is static and already hashed and in active state then this returns and fails to detect the re-initialization of an active object.

Yes, it's right, this can be fixed by pass a skip_ifstatic parameters from debug_object_activate. then re-initialization of an active object can be detected.

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -1
-static __initdata struct self_test obj = { .static_init = 0 };
+static struct self_test obj __initdata = { .static_init = 0 };
+static struct self_test sobj __initdata = { .static_init = 1 };

...

Quoted 8 lines
Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +0 -1
-	obj.static_init = 1;

Plus the s/obj/sobj/ which should be equivalent, unless I'm missing something here.

We have saved the is_static state when it is used at the first time, so the is_static_object function won't be called in this environment.

There is zero requirement for saving that state.

Quoted 9 lines
Quoted 9 lines
 lib/debugobjects.c |  127 +++++++++++++++++++++++++++--------------------------
 1 file changed, 67 insertions(+), 60 deletions(-)
lib/debugobjects.c +0 -0
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(
 	return obj;
 }
 
Quoted 16 lines

lib/debugobjects.c alloc_object(void *addr, struct debug_bu · line 272

Quoted 13 lines
lib/debugobjects.c +1 -5
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -273,7 +269,7 @@ alloc_object(void *addr, struct debug_bu
 	if (obj) {
 		obj->object = addr;
 		obj->descr  = descr;
-		obj->state  = ODEBUG_STATE_NONE;
+		obj->state  = ODEBUG_STATE_INIT;

This actually droped the ODEBUG_STATE_NONE state. If we active a uninitialized object, there will be no error report.

Indeed.

Quoted 6 lines

This should be

if (descr->is_static_object && descr->is_static_object(addr))

	obj->state  = ODEBUG_STATE_INIT;
else
	obj->state  = ODEBUG_STATE_NONE;
Quoted 8 lines

Kinda.

Quoted 2 lines

But this can't resolve the initial state requirement from the is_static_object() call.

Which requirement? The is_static_object() call takes the address of the actual object and has nothing to do with the tracking object at all.

This is for the fellowing test case, actually we calls debug_object_free() from a static object in our selftest, if we don't report any thing when call debug_object_free from a static object, we there is no such issues.

	obj.static_init = 1;
	debug_object_activate(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
		goto out;
	debug_object_init(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
		goto out;


    /*
     * We need to remove the debug_object_free here, because it's not
     * a legal operation.
     */

- debug_object_free(&obj, &descr_type_test);
- if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
- goto out;

#if 0
    /*
     * for the static debugobject, it's initial value will be changed
     * once used.
     */
	obj.static_init = 2;
	debug_object_activate(&obj, &descr_type_test);
	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
		goto out;

    /* This test will fail */

#endif

Quoted 21 lines
Quoted 3 lines

I think we can report an error when calling debug_object_free() from a static object. If don't do so, there is no way to determine it's a static object.

The memory allocator will tell you loudly when you try to free a static object. So no point in having another check.

Quoted 2 lines

When its initialization state changes, the is_static_object() call will return the wrong value.

That call is only relevant on the first invocation when there is no tracking object yet. So what's the problem you are trying to solve?

Quoted 3 lines

Please see the fellowing test case:

	obj.static_init = 1;

This is pointless, really. Once the object is tracked it does not matter at all whether it was statically or dynamically allocated.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

I test this patch, with my above change, and it seems to work well, but we still need to add extra flags to store its static state. And debug_object_free() should report an error for the static object.

No, we don't.

OK, we don't need to store the state if don't take care the debug_object_free() call on static object at all. If so, we should delete the debug_object_free() call on static object at debug_objects_selftest().

Quoted 5 lines
Quoted 2 lines

I think we should introduce lookup_object_or_alloc and is_static at the same time.

What for?

To report an error when someone calls debug_object_free on a static object.

Quoted 3 lines

Thanks,

tglx

Signature · 2 lines
BRs
Schspa Shi
#23 lore raw
Quoted 9 lines · Schspa Shi
Quoted 3 lines · Thomas Gleixner

Which requirement? The is_static_object() call takes the address of the actual object and has nothing to do with the tracking object at all.

This is for the fellowing test case, actually we calls debug_object_free() from a static object in our selftest, if we don't report any thing when call debug_object_free from a static object, we there is no such issues.

That's an artifical and completely pointless test case. As I told you before the memory subsystem will warn when it's tried to free a static object. debug_objects_free() is invoked from the memory subsystem *free* functions.

What is the value of another warning?

Nothing at all.

So why would we add extra code just to keep track of something completely redundant?

Thanks,

tglx

author
#24 lore raw
Quoted 16 lines · Thomas Gleixner
Quoted 9 lines · Schspa Shi
Quoted 3 lines · Thomas Gleixner

Which requirement? The is_static_object() call takes the address of the actual object and has nothing to do with the tracking object at all.

This is for the fellowing test case, actually we calls debug_object_free() from a static object in our selftest, if we don't report any thing when call debug_object_free from a static object, we there is no such issues.

That's an artifical and completely pointless test case. As I told you before the memory subsystem will warn when it's tried to free a static object. debug_objects_free() is invoked from the memory subsystem *free* functions.

Quoted 7 lines

What is the value of another warning?

Nothing at all.

So why would we add extra code just to keep track of something completely redundant?

OK, there is really no need to do this extra check. And should I submit a new patch version with your change 😃?

Quoted 3 lines

Thanks,

tglx

Signature · 2 lines
BRs
Schspa Shi
#25 lore raw

[tip: core/debugobjects] debugobject: Prevent init race with static objects

In reply to Schspa Shi
The following commit has been merged into the core/debugobjects branch of tip:

The following commit has been merged into the core/debugobjects branch of tip:

Commit-ID:     63a759694eed61025713b3e14dd827c8548daadc
Gitweb:        https://git.kernel.org/tip/63a759694eed61025713b3e14dd827c8548daadc
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Wed, 12 Apr 2023 09:54:39 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 15 Apr 2023 23:13:36 +02:00

debugobject: Prevent init race with static objects

Statically initialized objects are usually not initialized via the init() function of the subsystem. They are special cased and the subsystem provides a function to validate whether an object which is not yet tracked by debugobjects is statically initialized. This means the object is started to be tracked on first use, e.g. activation.

This works perfectly fine, unless there are two concurrent operations on that object. Schspa decoded the problem:

T0 T1

debug_object_assert_init(addr)
lock_hash_bucket()
obj = lookup_object(addr);
if (!obj) {

  	unlock_hash_bucket();
	- > preemption
			            lock_subsytem_object(addr);
				      activate_object(addr)
				      lock_hash_bucket();
				      obj = lookup_object(addr);
				      if (!obj) {
				    	unlock_hash_bucket();
					if (is_static_object(addr))
					   init_and_track(addr);
				      lock_hash_bucket();
				      obj = lookup_object(addr);
				      obj->state = ACTIVATED;
				      unlock_hash_bucket();

				    subsys function modifies content of addr,
				    so static object detection does
				    not longer work.

				    unlock_subsytem_object(addr);
				    
        if (is_static_object(addr)) <- Fails

	  debugobject emits a warning and invokes the fixup function which
	  reinitializes the already active object in the worst case.

This race exists forever, but was never observed until mod_timer() got a debug_object_assert_init() added which is outside of the timer base lock held section right at the beginning of the function to cover the lockless early exit points too.

Rework the code so that the lookup, the static object check and the tracking object association happens atomically under the hash bucket lock. This prevents the issue completely as all callers are serialized on the hash bucket lock and therefore cannot observe inconsistent state.

---
 lib/debugobjects.c | 125 +++++++++++++++++++++++---------------------
 1 file changed, 66 insertions(+), 59 deletions(-)
lib/debugobjects.c +66 -59
index df86e64..b796799 100644
@@ -216,10 +216,6 @@ static struct debug_obj *__alloc_object(struct hlist_head *list)
 	return obj;
 }
 
-/*
- * Allocate a new object. If the pool is empty, switch off the debugger.
- * Must be called with interrupts disabled.
- */
 static struct debug_obj *
 alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
 {
@@ -552,11 +548,49 @@ static void debug_object_is_on_stack(void *addr, int onstack)
 	WARN_ON(1);
 }
 
+static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
+						const struct debug_obj_descr *descr,
+						bool onstack, bool alloc_ifstatic)
+{
+	struct debug_obj *obj = lookup_object(addr, b);
+	enum debug_obj_state state = ODEBUG_STATE_NONE;
+
+	if (likely(obj))
+		return obj;
+
+	/*
+	 * debug_object_init() unconditionally allocates untracked
+	 * objects. It does not matter whether it is a static object or
+	 * not.
+	 *
+	 * debug_object_assert_init() and debug_object_activate() allow
+	 * allocation only if the descriptor callback confirms that the
+	 * object is static and considered initialized. For non-static
+	 * objects the allocation needs to be done from the fixup callback.
+	 */
+	if (unlikely(alloc_ifstatic)) {
+		if (!descr->is_static_object || !descr->is_static_object(addr))
+			return ERR_PTR(-ENOENT);
+		/* Statically allocated objects are considered initialized */
+		state = ODEBUG_STATE_INIT;
+	}
+
+	obj = alloc_object(addr, b, descr);
+	if (likely(obj)) {
+		obj->state = state;
+		debug_object_is_on_stack(addr, onstack);
+		return obj;
+	}
+
+	/* Out of memory. Do the cleanup outside of the locked region */
+	debug_objects_enabled = 0;
+	return NULL;
+}
+
 static void
 __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
 {
 	enum debug_obj_state state;
-	bool check_stack = false;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -572,16 +606,11 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (!obj) {
-		obj = alloc_object(addr, db, descr);
-		if (!obj) {
-			debug_objects_enabled = 0;
-			raw_spin_unlock_irqrestore(&db->lock, flags);
-			debug_objects_oom();
-			return;
-		}
-		check_stack = true;
+	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
+	if (unlikely(!obj)) {
+		raw_spin_unlock_irqrestore(&db->lock, flags);
+		debug_objects_oom();
+		return;
 	}
 
 	switch (obj->state) {
@@ -607,8 +636,6 @@ __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack
 	}
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
-	if (check_stack)
-		debug_object_is_on_stack(addr, onstack);
 }
 
 /**
@@ -648,14 +675,12 @@ EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
  */
 int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	enum debug_obj_state state;
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
 	int ret;
-	struct debug_obj o = { .object = addr,
-			       .state = ODEBUG_STATE_NOTAVAILABLE,
-			       .descr = descr };
 
 	if (!debug_objects_enabled)
 		return 0;
@@ -664,8 +689,8 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 
 	raw_spin_lock_irqsave(&db->lock, flags);
 
-	obj = lookup_object(addr, db);
-	if (obj) {
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	if (likely(!IS_ERR_OR_NULL(obj))) {
 		bool print_object = false;
 
 		switch (obj->state) {
@@ -698,24 +723,16 @@ int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
 
 	raw_spin_unlock_irqrestore(&db->lock, flags);
 
-	/*
-	 * We are here when a static object is activated. We
-	 * let the type specific code confirm whether this is
-	 * true or not. if true, we just make sure that the
-	 * static object is tracked in the object tracker. If
-	 * not, this must be a bug, so we try to fix it up.
-	 */
-	if (descr->is_static_object && descr->is_static_object(addr)) {
-		/* track this static object */
-		debug_object_init(addr, descr);
-		debug_object_activate(addr, descr);
-	} else {
-		debug_print_object(&o, "activate");
-		ret = debug_object_fixup(descr->fixup_activate, addr,
-					ODEBUG_STATE_NOTAVAILABLE);
-		return ret ? 0 : -EINVAL;
+	/* If NULL the allocation has hit OOM */
+	if (!obj) {
+		debug_objects_oom();
+		return 0;
 	}
-	return 0;
+
+	/* Object is neither static nor tracked. It's not initialized */
+	debug_print_object(&o, "activate");
+	ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
+	return ret ? 0 : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(debug_object_activate);
 
@@ -869,6 +886,7 @@ EXPORT_SYMBOL_GPL(debug_object_free);
  */
 void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 {
+	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
 	struct debug_bucket *db;
 	struct debug_obj *obj;
 	unsigned long flags;
@@ -879,31 +897,20 @@ void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
 	db = get_bucket((unsigned long) addr);
 
 	raw_spin_lock_irqsave(&db->lock, flags);
+	obj = lookup_object_or_alloc(addr, db, descr, false, true);
+	raw_spin_unlock_irqrestore(&db->lock, flags);
+	if (likely(!IS_ERR_OR_NULL(obj)))
+		return;
 
-	obj = lookup_object(addr, db);
+	/* If NULL the allocation has hit OOM */
 	if (!obj) {
-		struct debug_obj o = { .object = addr,
-				       .state = ODEBUG_STATE_NOTAVAILABLE,
-				       .descr = descr };
-
-		raw_spin_unlock_irqrestore(&db->lock, flags);
-		/*
-		 * Maybe the object is static, and we let the type specific
-		 * code confirm. Track this static object if true, else invoke
-		 * fixup.
-		 */
-		if (descr->is_static_object && descr->is_static_object(addr)) {
-			/* Track this static object */
-			debug_object_init(addr, descr);
-		} else {
-			debug_print_object(&o, "assert_init");
-			debug_object_fixup(descr->fixup_assert_init, addr,
-					   ODEBUG_STATE_NOTAVAILABLE);
-		}
+		debug_objects_oom();
 		return;
 	}
 
-	raw_spin_unlock_irqrestore(&db->lock, flags);
+	/* Object is neither tracked nor static. It's not initialized. */
+	debug_print_object(&o, "assert_init");
+	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
 }
 EXPORT_SYMBOL_GPL(debug_object_assert_init);