[tip: core/debugobjects] debugobjects: Reduce parallel pool fill attempts

From: tip-bot2 for Zhen Lei
Date: Tue Oct 15 2024 - 11:39:06 EST


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

Commit-ID: d8c6cd3a5c8008f5d42c7763a93b43d7f3a40e94
Gitweb: https://git.kernel.org/tip/d8c6cd3a5c8008f5d42c7763a93b43d7f3a40e94
Author: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
AuthorDate: Mon, 07 Oct 2024 18:50:03 +02:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
CommitterDate: Tue, 15 Oct 2024 17:30:31 +02:00

debugobjects: Reduce parallel pool fill attempts

The contention on the global pool_lock can be massive when the global pool
needs to be refilled and many CPUs try to handle this.

Address this by:

- splitting the refill from free list and allocation.

Refill from free list has no constraints vs. the context on RT, so
it can be tried outside of the RT specific preemptible() guard

- Let only one CPU handle the free list

- Let only one CPU do allocations unless the pool level is below
half of the minimum fill level.

Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Signed-off-by: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Link: https://lore.kernel.org/all/20240911083521.2257-4-thunder.leizhen@xxxxxxxxxx-
Link: https://lore.kernel.org/all/20241007164913.582118421@xxxxxxxxxxxxx

--
lib/debugobjects.c | 84 +++++++++++++++++++++++++++++++++++++----------------
1 file changed, 59 insertions(+), 25 deletions(-)

---
lib/debugobjects.c | 84 +++++++++++++++++++++++++++++++--------------
1 file changed, 59 insertions(+), 25 deletions(-)

diff --git a/lib/debugobjects.c b/lib/debugobjects.c
index 0d69095..64a72d4 100644
--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -138,14 +138,10 @@ static void free_object_list(struct hlist_head *head)
debug_objects_freed += cnt;
}

-static void fill_pool(void)
+static void fill_pool_from_freelist(void)
{
- gfp_t gfp = __GFP_HIGH | __GFP_NOWARN;
+ static unsigned long state;
struct debug_obj *obj;
- unsigned long flags;
-
- if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level))
- return;

/*
* Reuse objs from the global obj_to_free list; they will be
@@ -154,32 +150,58 @@ static void fill_pool(void)
* obj_nr_tofree is checked locklessly; the READ_ONCE() pairs with
* the WRITE_ONCE() in pool_lock critical sections.
*/
- if (READ_ONCE(obj_nr_tofree)) {
- raw_spin_lock_irqsave(&pool_lock, flags);
- /*
- * Recheck with the lock held as the worker thread might have
- * won the race and freed the global free list already.
- */
- while (obj_nr_tofree && (obj_pool_free < debug_objects_pool_min_level)) {
- obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
- hlist_del(&obj->node);
- WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
- hlist_add_head(&obj->node, &obj_pool);
- WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
- }
- raw_spin_unlock_irqrestore(&pool_lock, flags);
+ if (!READ_ONCE(obj_nr_tofree))
+ return;
+
+ /*
+ * Prevent the context from being scheduled or interrupted after
+ * setting the state flag;
+ */
+ guard(irqsave)();
+
+ /*
+ * Avoid lock contention on &pool_lock and avoid making the cache
+ * line exclusive by testing the bit before attempting to set it.
+ */
+ if (test_bit(0, &state) || test_and_set_bit(0, &state))
+ return;
+
+ guard(raw_spinlock)(&pool_lock);
+ /*
+ * Recheck with the lock held as the worker thread might have
+ * won the race and freed the global free list already.
+ */
+ while (obj_nr_tofree && (obj_pool_free < debug_objects_pool_min_level)) {
+ obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
+ hlist_del(&obj->node);
+ WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
+ hlist_add_head(&obj->node, &obj_pool);
+ WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
}
+ clear_bit(0, &state);
+}

- if (unlikely(!obj_cache))
+static void fill_pool(void)
+{
+ static atomic_t cpus_allocating;
+
+ /*
+ * Avoid allocation and lock contention when:
+ * - One other CPU is already allocating
+ * - the global pool has not reached the critical level yet
+ */
+ if (READ_ONCE(obj_pool_free) > (debug_objects_pool_min_level / 2) &&
+ atomic_read(&cpus_allocating))
return;

+ atomic_inc(&cpus_allocating);
while (READ_ONCE(obj_pool_free) < debug_objects_pool_min_level) {
struct debug_obj *new, *last = NULL;
HLIST_HEAD(head);
int cnt;

for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
- new = kmem_cache_zalloc(obj_cache, gfp);
+ new = kmem_cache_zalloc(obj_cache, __GFP_HIGH | __GFP_NOWARN);
if (!new)
break;
hlist_add_head(&new->node, &head);
@@ -187,14 +209,14 @@ static void fill_pool(void)
last = new;
}
if (!cnt)
- return;
+ break;

- raw_spin_lock_irqsave(&pool_lock, flags);
+ guard(raw_spinlock_irqsave)(&pool_lock);
hlist_splice_init(&head, &last->node, &obj_pool);
debug_objects_allocated += cnt;
WRITE_ONCE(obj_pool_free, obj_pool_free + cnt);
- raw_spin_unlock_irqrestore(&pool_lock, flags);
}
+ atomic_dec(&cpus_allocating);
}

/*
@@ -597,6 +619,18 @@ static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket

static void debug_objects_fill_pool(void)
{
+ if (unlikely(!obj_cache))
+ return;
+
+ if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level))
+ return;
+
+ /* Try reusing objects from obj_to_free_list */
+ fill_pool_from_freelist();
+
+ if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level))
+ return;
+
/*
* On RT enabled kernels the pool refill must happen in preemptible
* context -- for !RT kernels we rely on the fact that spinlock_t and