[Devel] [PATCH RHEL8 COMMIT] mm/mempool: Use kvmalloc to allocate array of element pointers

Konstantin Khorenko khorenko at virtuozzo.com
Tue May 25 12:16:39 MSK 2021


The commit is pushed to "branch-rh8-4.18.0-240.1.1.vz8.5.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-240.1.1.vz8.5.33
------>
commit 51880dfa07ff88a92890770ede9bfdd09804566d
Author: Andrey Ryabinin <aryabinin at virtuozzo.com>
Date:   Tue May 25 12:16:39 2021 +0300

    mm/mempool: Use kvmalloc to allocate array of element pointers
    
    The minimal number of mempool elements passed to
    mempool_create()/mempool_resize() could be large enough to
    trigger high order allocation in kmalloc_node(). High order
    allocations are costly and/or may fail if memory is fragmented.
    Since we don't need the array of element pointers to be physically
    contiguous, use kvmalloc_node() to allocate it.
    
    https://jira.sw.ru/browse/HCI-132
    https://pmc.acronis.com/browse/VSTOR-14758
    
    Signed-off-by: Andrey Ryabinin <aryabinin at virtuozzo.com>
    Reviewed-by: Kirill Tkhai <ktkhai at virtuozzo.com>
    
    (cherry-picked from vz7 commit 083d46fa7553 ("mm/mempool: Use kvmalloc to
    allocate array of element pointers"))
    
    Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 mm/mempool.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/mm/mempool.c b/mm/mempool.c
index b54f2c20e5e0..30eb316c511c 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -154,7 +154,7 @@ void mempool_exit(mempool_t *pool)
 		void *element = remove_element(pool, GFP_KERNEL);
 		pool->free(element, pool->pool_data);
 	}
-	kfree(pool->elements);
+	kvfree(pool->elements);
 	pool->elements = NULL;
 }
 EXPORT_SYMBOL(mempool_exit);
@@ -188,7 +188,7 @@ int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
 	pool->free	= free_fn;
 	init_waitqueue_head(&pool->wait);
 
-	pool->elements = kmalloc_array_node(min_nr, sizeof(void *),
+	pool->elements = kvmalloc_node(min_nr * sizeof(void *),
 					    gfp_mask, node_id);
 	if (!pool->elements)
 		return -ENOMEM;
@@ -294,6 +294,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 	void *element;
 	void **new_elements;
 	unsigned long flags;
+	void *old_elements = NULL;
 
 	BUG_ON(new_min_nr <= 0);
 	might_sleep();
@@ -312,7 +313,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	/* Grow the pool */
-	new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
+	new_elements = kvmalloc_array(new_min_nr, sizeof(*new_elements),
 				     GFP_KERNEL);
 	if (!new_elements)
 		return -ENOMEM;
@@ -321,12 +322,12 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 	if (unlikely(new_min_nr <= pool->min_nr)) {
 		/* Raced, other resize will do our work */
 		spin_unlock_irqrestore(&pool->lock, flags);
-		kfree(new_elements);
+		kvfree(new_elements);
 		goto out;
 	}
 	memcpy(new_elements, pool->elements,
 			pool->curr_nr * sizeof(*new_elements));
-	kfree(pool->elements);
+	old_elements = pool->elements;
 	pool->elements = new_elements;
 	pool->min_nr = new_min_nr;
 
@@ -347,6 +348,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 out_unlock:
 	spin_unlock_irqrestore(&pool->lock, flags);
 out:
+	kvfree(old_elements);
 	return 0;
 }
 EXPORT_SYMBOL(mempool_resize);


More information about the Devel mailing list