[patch 08/25] debugobjects: Provide and use free_object_list()

Thomas Gleixner posted 25 patches 1 month, 3 weeks ago
[patch 08/25] debugobjects: Provide and use free_object_list()
Posted by Thomas Gleixner 1 month, 3 weeks ago
Move the loop to free a list of objects into a helper function so it can be
reused later.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 lib/debugobjects.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -125,6 +125,20 @@ static const char *obj_states[ODEBUG_STA
 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
 };
 
+static void free_object_list(struct hlist_head *head)
+{
+	struct hlist_node *tmp;
+	struct debug_obj *obj;
+	int cnt = 0;
+
+	hlist_for_each_entry_safe(obj, tmp, head, node) {
+		hlist_del(&obj->node);
+		kmem_cache_free(obj_cache, obj);
+		cnt++;
+	}
+	debug_objects_freed += cnt;
+}
+
 static void fill_pool(void)
 {
 	gfp_t gfp = __GFP_HIGH | __GFP_NOWARN;
@@ -286,7 +300,6 @@ alloc_object(void *addr, struct debug_bu
  */
 static void free_obj_work(struct work_struct *work)
 {
-	struct hlist_node *tmp;
 	struct debug_obj *obj;
 	unsigned long flags;
 	HLIST_HEAD(tofree);
@@ -323,15 +336,11 @@ static void free_obj_work(struct work_st
 	 */
 	if (obj_nr_tofree) {
 		hlist_move_list(&obj_to_free, &tofree);
-		debug_objects_freed += obj_nr_tofree;
 		WRITE_ONCE(obj_nr_tofree, 0);
 	}
 	raw_spin_unlock_irqrestore(&pool_lock, flags);
 
-	hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
-		hlist_del(&obj->node);
-		kmem_cache_free(obj_cache, obj);
-	}
+	free_object_list(&tofree);
 }
 
 static void __free_object(struct debug_obj *obj)
@@ -1334,6 +1343,7 @@ static bool __init debug_objects_replace
 	}
 	return true;
 free:
+	/* Can't use free_object_list() as the cache is not populated yet */
 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
 		hlist_del(&obj->node);
 		kmem_cache_free(cache, obj);
Re: [patch 08/25] debugobjects: Provide and use free_object_list()
Posted by Leizhen (ThunderTown) 1 month, 2 weeks ago

On 2024/10/8 0:50, Thomas Gleixner wrote:
> Move the loop to free a list of objects into a helper function so it can be
> reused later.
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
>  lib/debugobjects.c |   22 ++++++++++++++++------
>  1 file changed, 16 insertions(+), 6 deletions(-)
> 
> --- a/lib/debugobjects.c
> +++ b/lib/debugobjects.c
> @@ -125,6 +125,20 @@ static const char *obj_states[ODEBUG_STA
>  	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
>  };
>  
> +static void free_object_list(struct hlist_head *head)
> +{
> +	struct hlist_node *tmp;
> +	struct debug_obj *obj;
> +	int cnt = 0;
> +
> +	hlist_for_each_entry_safe(obj, tmp, head, node) {
> +		hlist_del(&obj->node);
> +		kmem_cache_free(obj_cache, obj);
> +		cnt++;
> +	}
> +	debug_objects_freed += cnt;

debug_objects_freed was previously protected by pool_lock. Use atomic?

> +}
> +
>  static void fill_pool(void)
>  {
>  	gfp_t gfp = __GFP_HIGH | __GFP_NOWARN;
> @@ -286,7 +300,6 @@ alloc_object(void *addr, struct debug_bu
>   */
>  static void free_obj_work(struct work_struct *work)
>  {
> -	struct hlist_node *tmp;
>  	struct debug_obj *obj;
>  	unsigned long flags;
>  	HLIST_HEAD(tofree);
> @@ -323,15 +336,11 @@ static void free_obj_work(struct work_st
>  	 */
>  	if (obj_nr_tofree) {
>  		hlist_move_list(&obj_to_free, &tofree);
> -		debug_objects_freed += obj_nr_tofree;
>  		WRITE_ONCE(obj_nr_tofree, 0);
>  	}
>  	raw_spin_unlock_irqrestore(&pool_lock, flags);
>  
> -	hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
> -		hlist_del(&obj->node);
> -		kmem_cache_free(obj_cache, obj);
> -	}
> +	free_object_list(&tofree);
>  }
>  
>  static void __free_object(struct debug_obj *obj)
> @@ -1334,6 +1343,7 @@ static bool __init debug_objects_replace
>  	}
>  	return true;
>  free:
> +	/* Can't use free_object_list() as the cache is not populated yet */
>  	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
>  		hlist_del(&obj->node);
>  		kmem_cache_free(cache, obj);
> 
> .
> 

-- 
Regards,
  Zhen Lei
Re: [patch 08/25] debugobjects: Provide and use free_object_list()
Posted by Thomas Gleixner 1 month, 2 weeks ago
On Thu, Oct 10 2024 at 10:54, Leizhen wrote:
> On 2024/10/8 0:50, Thomas Gleixner wrote:
>> Move the loop to free a list of objects into a helper function so it can be
>> reused later.
>> 
>> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
>> ---
>>  lib/debugobjects.c |   22 ++++++++++++++++------
>>  1 file changed, 16 insertions(+), 6 deletions(-)
>> 
>> --- a/lib/debugobjects.c
>> +++ b/lib/debugobjects.c
>> @@ -125,6 +125,20 @@ static const char *obj_states[ODEBUG_STA
>>  	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
>>  };
>>  
>> +static void free_object_list(struct hlist_head *head)
>> +{
>> +	struct hlist_node *tmp;
>> +	struct debug_obj *obj;
>> +	int cnt = 0;
>> +
>> +	hlist_for_each_entry_safe(obj, tmp, head, node) {
>> +		hlist_del(&obj->node);
>> +		kmem_cache_free(obj_cache, obj);
>> +		cnt++;
>> +	}
>> +	debug_objects_freed += cnt;
>
> debug_objects_freed was previously protected by pool_lock. Use atomic?

It's statistics, so I'm not really worried about it whether it's a bit
off or not. 

But aside of that the freeing is single threaded because objects are
only handed back to the kmem_cache from the worker thread.

Thanks,

        tglx
[tip: core/debugobjects] debugobjects: Provide and use free_object_list()
Posted by tip-bot2 for Thomas Gleixner 1 month, 1 week ago
The following commit has been merged into the core/debugobjects branch of tip:

Commit-ID:     49a5cb827d3d625944d48518acec4e4b9d61e1da
Gitweb:        https://git.kernel.org/tip/49a5cb827d3d625944d48518acec4e4b9d61e1da
Author:        Thomas Gleixner <tglx@linutronix.de>
AuthorDate:    Mon, 07 Oct 2024 18:50:01 +02:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Tue, 15 Oct 2024 17:30:31 +02:00

debugobjects: Provide and use free_object_list()

Move the loop to free a list of objects into a helper function so it can be
reused later.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/all/20241007164913.453912357@linutronix.de

---
 lib/debugobjects.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/debugobjects.c b/lib/debugobjects.c
index fc54115..6ccdfeb 100644
--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -125,6 +125,20 @@ static const char *obj_states[ODEBUG_STATE_MAX] = {
 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
 };
 
+static void free_object_list(struct hlist_head *head)
+{
+	struct hlist_node *tmp;
+	struct debug_obj *obj;
+	int cnt = 0;
+
+	hlist_for_each_entry_safe(obj, tmp, head, node) {
+		hlist_del(&obj->node);
+		kmem_cache_free(obj_cache, obj);
+		cnt++;
+	}
+	debug_objects_freed += cnt;
+}
+
 static void fill_pool(void)
 {
 	gfp_t gfp = __GFP_HIGH | __GFP_NOWARN;
@@ -286,7 +300,6 @@ init_obj:
  */
 static void free_obj_work(struct work_struct *work)
 {
-	struct hlist_node *tmp;
 	struct debug_obj *obj;
 	unsigned long flags;
 	HLIST_HEAD(tofree);
@@ -323,15 +336,11 @@ free_objs:
 	 */
 	if (obj_nr_tofree) {
 		hlist_move_list(&obj_to_free, &tofree);
-		debug_objects_freed += obj_nr_tofree;
 		WRITE_ONCE(obj_nr_tofree, 0);
 	}
 	raw_spin_unlock_irqrestore(&pool_lock, flags);
 
-	hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
-		hlist_del(&obj->node);
-		kmem_cache_free(obj_cache, obj);
-	}
+	free_object_list(&tofree);
 }
 
 static void __free_object(struct debug_obj *obj)
@@ -1334,6 +1343,7 @@ static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache
 	}
 	return true;
 free:
+	/* Can't use free_object_list() as the cache is not populated yet */
 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
 		hlist_del(&obj->node);
 		kmem_cache_free(cache, obj);