[Devel] [PATCH RHEL8 COMMIT] ploop: Use kmem_cache for struct ploop_cow allocation

Konstantin Khorenko khorenko at virtuozzo.com
Tue Dec 10 18:33:16 MSK 2019


The commit is pushed to "branch-rh8-4.18.0-80.1.2.vz8.2.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-80.1.2.vz8.2.6
------>
commit 46c69401490978aad50ef3608fed75de0ba2be63
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Tue Dec 10 18:33:13 2019 +0300

    ploop: Use kmem_cache for struct ploop_cow allocation
    
    This works better under memory pressure. Also note,
    that target registering in dm_ploop_init() changed
    for convenience.
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-ploop-map.c    |  4 ++--
 drivers/md/dm-ploop-target.c | 26 ++++++++++++++++----------
 drivers/md/dm-ploop.h        |  1 +
 3 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 27ad48c5e16a..a67656b2b01e 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -514,7 +514,7 @@ static void complete_cow(struct ploop_cow *cow, blk_status_t bi_status)
 
 	queue_work(ploop->wq, &ploop->worker);
 	free_bio_with_pages(ploop, cow->cluster_bio);
-	kfree(cow);
+	kmem_cache_free(cow_cache, cow);
 }
 
 static void piwb_discard_completed(struct ploop *ploop, bool success,
@@ -1012,7 +1012,7 @@ int submit_cluster_cow(struct ploop *ploop, unsigned int level,
 	if (!bio)
 		goto err;
 
-	cow = kmalloc(sizeof(*cow), GFP_NOIO);
+	cow = kmem_cache_alloc(cow_cache, GFP_NOIO);
 	if (!cow)
 		goto err;
 
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 904cacade2db..bdeee2334499 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -13,6 +13,7 @@
 #define DM_MSG_PREFIX "ploop"
 
 struct kmem_cache *piocb_cache;
+struct kmem_cache *cow_cache;
 
 static void inflight_bios_ref_exit0(struct percpu_ref *ref)
 {
@@ -297,28 +298,33 @@ static struct target_type ploop_target = {
 
 static int __init dm_ploop_init(void)
 {
-	int r;
+	int r = -ENOMEM;
+
+	piocb_cache = kmem_cache_create("ploop-iocb", sizeof(struct ploop_iocb),
+					0, 0, NULL);
+	cow_cache = kmem_cache_create("ploop-cow", sizeof(struct ploop_cow),
+				      0, 0, NULL);
+	if (!piocb_cache || !cow_cache)
+		goto err;
 
 	r = dm_register_target(&ploop_target);
 	if (r) {
 		DMERR("ploop target registration failed: %d", r);
-		return r;
-	}
-
-	piocb_cache = kmem_cache_create("ploop-iocb", sizeof(struct ploop_iocb),
-					0, 0, NULL);
-	if (!piocb_cache) {
-		dm_unregister_target(&ploop_target);
-		return -ENOMEM;
+		goto err;
 	}
 
 	return 0;
+err:
+	kmem_cache_destroy(piocb_cache);
+	kmem_cache_destroy(cow_cache);
+	return r;
 }
 
 static void __exit dm_ploop_exit(void)
 {
-	kmem_cache_destroy(piocb_cache);
 	dm_unregister_target(&ploop_target);
+	kmem_cache_destroy(cow_cache);
+	kmem_cache_destroy(piocb_cache);
 }
 
 module_init(dm_ploop_init);
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 0571e8fe0e79..1dc29e4f5345 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -273,6 +273,7 @@ struct ploop_cow {
 };
 
 extern struct kmem_cache *piocb_cache;
+extern struct kmem_cache *cow_cache;
 
 static inline bool ploop_is_ro(struct ploop *ploop)
 {



More information about the Devel mailing list