All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: "Leizhen (ThunderTown)" <thunder.leizhen@huawei.com>,
	LKML <linux-kernel@vger.kernel.org>
Cc: Waiman Long <longman@redhat.com>
Subject: Re: [patch 25/25] debugobjects: Track object usage to avoid premature freeing of objects
Date: Sun, 13 Oct 2024 20:45:57 +0200	[thread overview]
Message-ID: <87bjznhme2.ffs@tglx> (raw)
In-Reply-To: <33d4dca7-3a1b-c99d-7e76-160a5fca61c2@huawei.com>

On Thu, Oct 10 2024 at 21:13, Leizhen wrote:
>> +static bool pool_can_fill(struct obj_pool *dst, struct obj_pool *src)
>> +{
>> +	unsigned int cnt = pool_count(dst);
>> +
>> +	if (cnt >= dst->min_cnt)
>> +		return true;
>
> There's already an interception in function debug_objects_fill_pool().
> It's unlikely to be true.
>
> debug_objects_fill_pool() --> fill_pool() --> pool_can_fill()
> :
> 	if (likely(!pool_should_refill(&pool_global)))
> 		return;

While they are different checks, you're right.

If fill_pool_from_freelist() reused objects and was no able to refill
the global pool above the threshold level, then fill_pool() won't find
objects enough objects in the to free pool to refill, so it's just
checking for a completely unlikely corner case.

I just validated that it does not make a difference. Updated patch below

Thanks for spotting this!

       tglx
---
Subject: debugobjects: Track object usage to avoid premature freeing of objects
From: Thomas Gleixner <tglx@linutronix.de>
Date: Sat, 14 Sep 2024 21:33:19 +0200

The freelist is freed at a constant rate independent of the actual usage
requirements. That's bad in scenarios where usage comes in bursts. The end
of a burst puts the object on the free list and freeing proceeds even when
the next burst which requires objects started again.

Keep track of the usage with a exponentially wheighted moving average and
take that into account in the worker function which frees objects from the
free list.

This further reduces the kmem_cache allocation/free rate for a full kernel
compile:

   	    kmem_cache_alloc()	kmem_cache_free()
Baseline:   225k		245k
Usage:	    170k		117k

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

--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -13,6 +13,7 @@
 #include <linux/hash.h>
 #include <linux/kmemleak.h>
 #include <linux/sched.h>
+#include <linux/sched/loadavg.h>
 #include <linux/sched/task_stack.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
@@ -86,6 +87,7 @@ static struct obj_pool pool_to_free = {
 
 static HLIST_HEAD(pool_boot);
 
+static unsigned long		avg_usage;
 static bool			obj_freeing;
 
 static int __data_racy			debug_objects_maxchain __read_mostly;
@@ -427,11 +429,31 @@ static struct debug_obj *lookup_object(v
 	return NULL;
 }
 
+static void calc_usage(void)
+{
+	static DEFINE_RAW_SPINLOCK(avg_lock);
+	static unsigned long avg_period;
+	unsigned long cur, now = jiffies;
+
+	if (!time_after_eq(now, READ_ONCE(avg_period)))
+		return;
+
+	if (!raw_spin_trylock(&avg_lock))
+		return;
+
+	WRITE_ONCE(avg_period, now + msecs_to_jiffies(10));
+	cur = READ_ONCE(pool_global.stats.cur_used) * ODEBUG_FREE_WORK_MAX;
+	WRITE_ONCE(avg_usage, calc_load(avg_usage, EXP_5, cur));
+	raw_spin_unlock(&avg_lock);
+}
+
 static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b,
 				      const struct debug_obj_descr *descr)
 {
 	struct debug_obj *obj;
 
+	calc_usage();
+
 	if (static_branch_likely(&obj_cache_enabled))
 		obj = pcpu_alloc();
 	else
@@ -450,14 +472,26 @@ static struct debug_obj *alloc_object(vo
 /* workqueue function to free objects. */
 static void free_obj_work(struct work_struct *work)
 {
-	bool free = true;
+	static unsigned long last_use_avg;
+	unsigned long cur_used, last_used, delta;
+	unsigned int max_free = 0;
 
 	WRITE_ONCE(obj_freeing, false);
 
+	/* Rate limit freeing based on current use average */
+	cur_used = READ_ONCE(avg_usage);
+	last_used = last_use_avg;
+	last_use_avg = cur_used;
+
 	if (!pool_count(&pool_to_free))
 		return;
 
-	for (unsigned int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) {
+	if (cur_used <= last_used) {
+		delta = (last_used - cur_used) / ODEBUG_FREE_WORK_MAX;
+		max_free = min(delta, ODEBUG_FREE_WORK_MAX);
+	}
+
+	for (int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) {
 		HLIST_HEAD(tofree);
 
 		/* Acquire and drop the lock for each batch */
@@ -468,9 +502,10 @@ static void free_obj_work(struct work_st
 			/* Refill the global pool if possible */
 			if (pool_move_batch(&pool_global, &pool_to_free)) {
 				/* Don't free as there seems to be demand */
-				free = false;
-			} else if (free) {
+				max_free = 0;
+			} else if (max_free) {
 				pool_pop_batch(&tofree, &pool_to_free);
+				max_free--;
 			} else {
 				return;
 			}
@@ -1110,7 +1145,7 @@ static int debug_stats_show(struct seq_f
 	for_each_possible_cpu(cpu)
 		pcp_free += per_cpu(pool_pcpu.cnt, cpu);
 
-	pool_used = data_race(pool_global.stats.cur_used);
+	pool_used = READ_ONCE(pool_global.stats.cur_used);
 	pcp_free = min(pool_used, pcp_free);
 	pool_used -= pcp_free;
 

  reply	other threads:[~2024-10-13 18:46 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-07 16:49 [patch 00/25] debugobjects: Rework object handling Thomas Gleixner
2024-10-07 16:49 ` [patch 01/25] debugobjects: Delete a piece of redundant code Thomas Gleixner
2024-10-07 16:49 ` [patch 02/25] debugobjects: Collect newly allocated objects in a list to reduce lock contention Thomas Gleixner
2024-10-07 16:49 ` [patch 03/25] debugobjects: Dont destroy kmem cache in init() Thomas Gleixner
2024-10-10  2:14   ` Leizhen (ThunderTown)
2024-10-10 11:46     ` Thomas Gleixner
2024-10-10 13:31       ` Leizhen (ThunderTown)
2024-10-11 20:37         ` Thomas Gleixner
2024-10-12  1:50           ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:49 ` [patch 04/25] debugobjects: Remove pointless hlist initialization Thomas Gleixner
2024-10-10  2:19   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:49 ` [patch 05/25] debugobjects: Dont free objects directly on CPU hotplug Thomas Gleixner
2024-10-10  2:33   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:49 ` [patch 06/25] debugobjects: Reuse put_objects() on OOM Thomas Gleixner
2024-10-10  2:38   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:49 ` [patch 07/25] debugobjects: Remove pointless debug printk Thomas Gleixner
2024-10-10  2:44   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 08/25] debugobjects: Provide and use free_object_list() Thomas Gleixner
2024-10-10  2:54   ` Leizhen (ThunderTown)
2024-10-11 20:40     ` Thomas Gleixner
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 09/25] debugobjects: Make debug_objects_enabled bool Thomas Gleixner
2024-10-10  3:00   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 10/25] debugobjects: Reduce parallel pool fill attempts Thomas Gleixner
2024-10-07 16:50 ` [patch 11/25] debugobjects: Move pools into a datastructure Thomas Gleixner
2024-10-10  3:47   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 12/25] debugobjects: Use separate list head for boot pool Thomas Gleixner
2024-10-10  4:04   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 13/25] debugobjects: Rename and tidy up per CPU pools Thomas Gleixner
2024-10-10  6:23   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 14/25] debugobjects: Move min/max count into pool struct Thomas Gleixner
2024-10-10  6:26   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 15/25] debugobjects: Rework object allocation Thomas Gleixner
2024-10-10  6:39   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 16/25] debugobjects: Rework object freeing Thomas Gleixner
2024-10-10  7:39   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 17/25] debugobjects: Rework free_object_work() Thomas Gleixner
2024-10-10  8:10   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 18/25] debugobjects: Use static key for boot pool selection Thomas Gleixner
2024-10-10  8:12   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 19/25] debugobjects: Prepare for batching Thomas Gleixner
2024-10-10  8:15   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 20/25] debugobjects: Prepare kmem_cache allocations " Thomas Gleixner
2024-10-10  8:40   ` Leizhen (ThunderTown)
2024-10-11 20:47     ` Thomas Gleixner
2024-10-12  2:02       ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 21/25] debugobjects: Implement batch processing Thomas Gleixner
2024-10-10  9:39   ` Leizhen (ThunderTown)
2024-10-11 20:48     ` Thomas Gleixner
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 22/25] debugobjects: Move pool statistics into global_pool struct Thomas Gleixner
2024-10-10  9:50   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 23/25] debugobjects: Double the per CPU slots Thomas Gleixner
2024-10-10  9:51   ` Leizhen (ThunderTown)
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 24/25] debugobjects: Refill per CPU pool more agressively Thomas Gleixner
2024-10-10 10:02   ` Leizhen (ThunderTown)
2024-10-11 20:49     ` Thomas Gleixner
2024-10-15 15:36   ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner
2024-10-07 16:50 ` [patch 25/25] debugobjects: Track object usage to avoid premature freeing of objects Thomas Gleixner
2024-10-10 13:13   ` Leizhen (ThunderTown)
2024-10-13 18:45     ` Thomas Gleixner [this message]
2024-10-14  1:46       ` Leizhen (ThunderTown)
2024-10-15 15:36       ` [tip: core/debugobjects] " tip-bot2 for Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bjznhme2.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=thunder.leizhen@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.