linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Matias Bjørling" <mb@lightnvm.io>
To: axboe@fb.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Hans Holmberg" <hans.holmberg@cnexlabs.com>,
	"Matias Bjørling" <mb@lightnvm.io>
Subject: [GIT PULL 28/45] lightnvm: pblk: stop recreating global caches
Date: Tue,  9 Oct 2018 13:11:58 +0200	[thread overview]
Message-ID: <20181009111215.7653-29-mb@lightnvm.io> (raw)
In-Reply-To: <20181009111215.7653-1-mb@lightnvm.io>

From: Hans Holmberg <hans.holmberg@cnexlabs.com>

Pblk should not create a set of global caches every time
a pblk instance is created. The global caches should be
made available only when there is one or more pblk instances.

This patch bundles the global caches together with a kref
keeping track of whether the caches should be available or not.

Also, turn the global pblk lock into a mutex that explicitly
protects the caches (as this was the only purpose of the lock).

Signed-off-by: Hans Holmberg <hans.holmberg@cnexlabs.com>
Signed-off-by: Matias Bjørling <mb@lightnvm.io>
---
 drivers/lightnvm/pblk-init.c | 132 +++++++++++++++++++++++------------
 1 file changed, 86 insertions(+), 46 deletions(-)

diff --git a/drivers/lightnvm/pblk-init.c b/drivers/lightnvm/pblk-init.c
index 9aebdee8e4c9..fb66bc84d5ca 100644
--- a/drivers/lightnvm/pblk-init.c
+++ b/drivers/lightnvm/pblk-init.c
@@ -26,9 +26,24 @@ static unsigned int write_buffer_size;
 module_param(write_buffer_size, uint, 0644);
 MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
 
-static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
-				*pblk_w_rq_cache;
-static DECLARE_RWSEM(pblk_lock);
+struct pblk_global_caches {
+	struct kmem_cache	*ws;
+	struct kmem_cache	*rec;
+	struct kmem_cache	*g_rq;
+	struct kmem_cache	*w_rq;
+
+	struct kref		kref;
+
+	struct mutex		mutex; /* Ensures consistency between
+					* caches and kref
+					*/
+};
+
+static struct pblk_global_caches pblk_caches = {
+	.mutex = __MUTEX_INITIALIZER(pblk_caches.mutex),
+	.kref = KREF_INIT(0),
+};
+
 struct bio_set pblk_bio_set;
 
 static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
@@ -307,53 +322,80 @@ static int pblk_set_addrf(struct pblk *pblk)
 	return 0;
 }
 
-static int pblk_init_global_caches(struct pblk *pblk)
+static int pblk_create_global_caches(void)
 {
-	down_write(&pblk_lock);
-	pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
+
+	pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
 				sizeof(struct pblk_line_ws), 0, 0, NULL);
-	if (!pblk_ws_cache) {
-		up_write(&pblk_lock);
+	if (!pblk_caches.ws)
 		return -ENOMEM;
-	}
 
-	pblk_rec_cache = kmem_cache_create("pblk_rec",
+	pblk_caches.rec = kmem_cache_create("pblk_rec",
 				sizeof(struct pblk_rec_ctx), 0, 0, NULL);
-	if (!pblk_rec_cache) {
-		kmem_cache_destroy(pblk_ws_cache);
-		up_write(&pblk_lock);
-		return -ENOMEM;
-	}
+	if (!pblk_caches.rec)
+		goto fail_destroy_ws;
 
-	pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
+	pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
 				0, 0, NULL);
-	if (!pblk_g_rq_cache) {
-		kmem_cache_destroy(pblk_ws_cache);
-		kmem_cache_destroy(pblk_rec_cache);
-		up_write(&pblk_lock);
-		return -ENOMEM;
-	}
+	if (!pblk_caches.g_rq)
+		goto fail_destroy_rec;
 
-	pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
+	pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
 				0, 0, NULL);
-	if (!pblk_w_rq_cache) {
-		kmem_cache_destroy(pblk_ws_cache);
-		kmem_cache_destroy(pblk_rec_cache);
-		kmem_cache_destroy(pblk_g_rq_cache);
-		up_write(&pblk_lock);
-		return -ENOMEM;
-	}
-	up_write(&pblk_lock);
+	if (!pblk_caches.w_rq)
+		goto fail_destroy_g_rq;
 
 	return 0;
+
+fail_destroy_g_rq:
+	kmem_cache_destroy(pblk_caches.g_rq);
+fail_destroy_rec:
+	kmem_cache_destroy(pblk_caches.rec);
+fail_destroy_ws:
+	kmem_cache_destroy(pblk_caches.ws);
+
+	return -ENOMEM;
+}
+
+static int pblk_get_global_caches(void)
+{
+	int ret;
+
+	mutex_lock(&pblk_caches.mutex);
+
+	if (kref_read(&pblk_caches.kref) > 0) {
+		kref_get(&pblk_caches.kref);
+		mutex_unlock(&pblk_caches.mutex);
+		return 0;
+	}
+
+	ret = pblk_create_global_caches();
+
+	if (!ret)
+		kref_get(&pblk_caches.kref);
+
+	mutex_unlock(&pblk_caches.mutex);
+
+	return ret;
+}
+
+static void pblk_destroy_global_caches(struct kref *ref)
+{
+	struct pblk_global_caches *c;
+
+	c = container_of(ref, struct pblk_global_caches, kref);
+
+	kmem_cache_destroy(c->ws);
+	kmem_cache_destroy(c->rec);
+	kmem_cache_destroy(c->g_rq);
+	kmem_cache_destroy(c->w_rq);
 }
 
-static void pblk_free_global_caches(struct pblk *pblk)
+static void pblk_put_global_caches(void)
 {
-	kmem_cache_destroy(pblk_ws_cache);
-	kmem_cache_destroy(pblk_rec_cache);
-	kmem_cache_destroy(pblk_g_rq_cache);
-	kmem_cache_destroy(pblk_w_rq_cache);
+	mutex_lock(&pblk_caches.mutex);
+	kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
+	mutex_unlock(&pblk_caches.mutex);
 }
 
 static int pblk_core_init(struct pblk *pblk)
@@ -382,7 +424,7 @@ static int pblk_core_init(struct pblk *pblk)
 	if (!pblk->pad_dist)
 		return -ENOMEM;
 
-	if (pblk_init_global_caches(pblk))
+	if (pblk_get_global_caches())
 		goto fail_free_pad_dist;
 
 	/* Internal bios can be at most the sectors signaled by the device. */
@@ -391,27 +433,27 @@ static int pblk_core_init(struct pblk *pblk)
 		goto free_global_caches;
 
 	ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
-				     pblk_ws_cache);
+				     pblk_caches.ws);
 	if (ret)
 		goto free_page_bio_pool;
 
 	ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
-				     pblk_rec_cache);
+				     pblk_caches.rec);
 	if (ret)
 		goto free_gen_ws_pool;
 
 	ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
-				     pblk_g_rq_cache);
+				     pblk_caches.g_rq);
 	if (ret)
 		goto free_rec_pool;
 
 	ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
-				     pblk_g_rq_cache);
+				     pblk_caches.g_rq);
 	if (ret)
 		goto free_r_rq_pool;
 
 	ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
-				     pblk_w_rq_cache);
+				     pblk_caches.w_rq);
 	if (ret)
 		goto free_e_rq_pool;
 
@@ -457,7 +499,7 @@ static int pblk_core_init(struct pblk *pblk)
 free_page_bio_pool:
 	mempool_exit(&pblk->page_bio_pool);
 free_global_caches:
-	pblk_free_global_caches(pblk);
+	pblk_put_global_caches();
 fail_free_pad_dist:
 	kfree(pblk->pad_dist);
 	return -ENOMEM;
@@ -481,7 +523,7 @@ static void pblk_core_free(struct pblk *pblk)
 	mempool_exit(&pblk->e_rq_pool);
 	mempool_exit(&pblk->w_rq_pool);
 
-	pblk_free_global_caches(pblk);
+	pblk_put_global_caches();
 	kfree(pblk->pad_dist);
 }
 
@@ -1074,7 +1116,6 @@ static void pblk_exit(void *private, bool graceful)
 {
 	struct pblk *pblk = private;
 
-	down_write(&pblk_lock);
 	pblk_gc_exit(pblk, graceful);
 	pblk_tear_down(pblk, graceful);
 
@@ -1083,7 +1124,6 @@ static void pblk_exit(void *private, bool graceful)
 #endif
 
 	pblk_free(pblk);
-	up_write(&pblk_lock);
 }
 
 static sector_t pblk_capacity(void *private)
-- 
2.17.1


  parent reply	other threads:[~2018-10-09 11:14 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-09 11:11 [GIT PULL 00/45] lightnvm updates for 4.20 Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 01/45] lightnvm: remove dependencies on BLK_DEV_NVME and PCI Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 02/45] lightnvm: combine 1.2 and 2.0 command flags Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 03/45] lightnvm: pblk: fix rqd.error return value in pblk_blk_erase_sync Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 04/45] lightnvm: move device L2P detection to core Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 05/45] lightnvm: pblk: fix race condition on metadata I/O Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 06/45] lightnvm: move bad block and chunk state logic to core Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 07/45] lightnvm: pblk: unify vector max req constants Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 08/45] lightnvm: pblk: fix incorrect min_write_pgs Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 09/45] lightnvm: pblk: remove size and out of bounds read check Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 10/45] lightnvm: pblk: refactor put line fn on read completion Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 11/45] lightnvm: pblk: add helpers for chunk addresses Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 12/45] lightnvm: pblk: improve line helpers Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 13/45] lightnvm: pblk: fix comment typo Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 14/45] lightnvm: pblk: remove unused variable Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 15/45] lightnvm: pblk: guarantee emeta on line close Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 16/45] lightnvm: introduce nvm_rq_to_ppa_list Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 17/45] lightnvm: pblk: allocate line map bitmaps using a mempool Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 18/45] lightnvm: pblk: remove unused parameters in pblk_up_rq Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 19/45] lightnvm: pblk: fix up prints in pblk_read_check_rand Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 20/45] lightnvm: pblk: fix write amplificiation calculation Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 21/45] lightnvm: pblk: remove debug from pblk_[down/up]_page Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 22/45] lightnvm: pblk: add trace events for chunk states Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 23/45] lightnvm: pblk: add trace events for line state changes Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 24/45] lightnvm: pblk: add trace events for pblk " Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 25/45] lightnvm: pblk: add tracing for chunk resets Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 26/45] lightnvm: move ppa transformations to core Matias Bjørling
2018-10-09 11:11 ` [GIT PULL 27/45] lightnvm: pblk: calculate line pad distance in helper Matias Bjørling
2018-10-09 11:11 ` Matias Bjørling [this message]
2018-10-09 11:11 ` [GIT PULL 29/45] lightnvm: pblk: fix mapping issue on failed writes Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 30/45] lightnvm: pblk: fix two sleep-in-atomic-context bugs Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 31/45] lightnvm: use internal allocation for chunk log page Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 32/45] lightnvm: pblk: encapsulate rqd dma allocations Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 33/45] lightnvm: pblk: refactor metadata paths Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 34/45] lightnvm: pblk: take write semaphore on metadata Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 35/45] lightnvm: pblk: recover open lines on 2.0 devices Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 36/45] lightnvm: pblk: add SPDX license tag Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 37/45] lightnvm: pblk: fix race on sysfs line state Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 38/45] lightnvm: pblk: remove unused function Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 39/45] lightnvm: pblk: encapsulate rb pointer operations Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 40/45] lightnvm: pblk: move ring buffer alloc/free rb init Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 41/45] lightnvm: pblk: guarantee mw_cunits on read buffer Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 42/45] lightnvm: do no update csecs and sos on 1.2 Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 43/45] lightnvm: pblk: fix error handling of pblk_lines_init() Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 44/45] lightnvm: pblk: consider max hw sectors supported for max_write_pgs Matias Bjørling
2018-10-09 11:12 ` [GIT PULL 45/45] lightnvm: pblk: guarantee that backpointer is respected on writer stall Matias Bjørling
2018-10-09 14:25 ` [GIT PULL 00/45] lightnvm updates for 4.20 Jens Axboe

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=20181009111215.7653-29-mb@lightnvm.io \
    --to=mb@lightnvm.io \
    --cc=axboe@fb.com \
    --cc=hans.holmberg@cnexlabs.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).