All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
To: Minchan Kim <minchan@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Jerome Marchand <jmarchan@redhat.com>,
	Nitin Gupta <ngupta@vflare.org>,
	linux-kernel@vger.kernel.org,
	Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Subject: [PATCHv7 6/7] zram: add set_max_streams knob
Date: Tue, 25 Feb 2014 14:34:32 +0300	[thread overview]
Message-ID: <1393328073-12313-7-git-send-email-sergey.senozhatsky@gmail.com> (raw)
In-Reply-To: <1393328073-12313-1-git-send-email-sergey.senozhatsky@gmail.com>

This patch allows to change max_comp_streams on initialised zcomp.

Introduce zcomp set_max_streams() knob, zcomp_strm_multi_set_max_streams()
and zcomp_strm_single_set_max_streams() callbacks to change streams limit
for zcomp_strm_multi and zcomp_strm_single, accordingly. set_max_streams
for single steam zcomp does nothing.

If user has lowered the limit, then zcomp_strm_multi_set_max_streams()
attempts to immediately free extra streams (as much as it can, depending
on idle streams availability).

Note, this patch does not allow to change stream 'policy' from single to
multi stream (or vice versa) on already initialised compression backend,
this will be introduced later.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 drivers/block/zram/zcomp.c    | 39 +++++++++++++++++++++++++++++++++++++++
 drivers/block/zram/zcomp.h    |  3 +++
 drivers/block/zram/zram_drv.c |  7 ++-----
 3 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index 1bcb70e..adfbfee 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -135,6 +135,32 @@ static void zcomp_strm_multi_put(struct zcomp *comp, struct zcomp_strm *zstrm)
 	wake_up(&zs->strm_wait);
 }
 
+/* change max_strm limit */
+static void zcomp_strm_multi_set_max_streams(struct zcomp *comp, int num_strm)
+{
+	struct zcomp_strm_multi *zs = comp->stream;
+
+	/* single stream handled as a special case by zcomp_strm_single */
+	if (num_strm < 2)
+		return;
+
+	spin_lock(&zs->strm_lock);
+	zs->max_strm = num_strm;
+	/*
+	 * if user has lowered the limit and there are idle streams,
+	 * immediately free as much streams (and memory) as we can.
+	 */
+	while (atomic_read(&zs->avail_strm) > num_strm &&
+			!list_empty(&zs->idle_strm)) {
+		struct zcomp_strm *zstrm = list_entry(zs->idle_strm.next,
+				struct zcomp_strm, list);
+		list_del(&zstrm->list);
+		zcomp_strm_free(comp, zstrm);
+		atomic_dec(&zs->avail_strm);
+	}
+	spin_unlock(&zs->strm_lock);
+}
+
 static void zcomp_strm_multi_destroy(struct zcomp *comp)
 {
 	struct zcomp_strm_multi *zs = comp->stream;
@@ -158,6 +184,7 @@ static int zcomp_strm_multi_create(struct zcomp *comp, int num_strm)
 	comp->destroy = zcomp_strm_multi_destroy;
 	comp->strm_get = zcomp_strm_multi_get;
 	comp->strm_put = zcomp_strm_multi_put;
+	comp->set_max_streams = zcomp_strm_multi_set_max_streams;
 	zs = kmalloc(sizeof(struct zcomp_strm_multi), GFP_KERNEL);
 	comp->stream = zs;
 	if (!zs)
@@ -193,6 +220,12 @@ static void zcomp_strm_single_put(struct zcomp *comp, struct zcomp_strm *zstrm)
 	mutex_unlock(&zs->strm_lock);
 }
 
+static void zcomp_strm_single_set_max_streams(struct zcomp *comp, int num_strm)
+{
+	/* zcomp_strm_single support only max_comp_streams == 1 */
+	return;
+}
+
 static void zcomp_strm_single_destroy(struct zcomp *comp)
 {
 	struct zcomp_strm_single *zs = comp->stream;
@@ -207,6 +240,7 @@ static int zcomp_strm_single_create(struct zcomp *comp)
 	comp->destroy = zcomp_strm_single_destroy;
 	comp->strm_get = zcomp_strm_single_get;
 	comp->strm_put = zcomp_strm_single_put;
+	comp->set_max_streams = zcomp_strm_single_set_max_streams;
 	zs = kmalloc(sizeof(struct zcomp_strm_single), GFP_KERNEL);
 	comp->stream = zs;
 	if (!zs)
@@ -221,6 +255,11 @@ static int zcomp_strm_single_create(struct zcomp *comp)
 	return 0;
 }
 
+void zcomp_set_max_streams(struct zcomp *comp, int num_strm)
+{
+	comp->set_max_streams(comp, num_strm);
+}
+
 struct zcomp_strm *zcomp_strm_get(struct zcomp *comp)
 {
 	return comp->strm_get(comp);
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index 5514509..9645e16 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -39,6 +39,7 @@ struct zcomp {
 
 	struct zcomp_strm *(*strm_get)(struct zcomp *comp);
 	void (*strm_put)(struct zcomp *comp, struct zcomp_strm *zstrm);
+	void (*set_max_streams)(struct zcomp *comp, int num_strm);
 	void (*destroy)(struct zcomp *comp);
 };
 
@@ -53,4 +54,6 @@ int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
 
 int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
 		size_t src_len, unsigned char *dst);
+
+void zcomp_set_max_streams(struct zcomp *comp, int num_strm);
 #endif /* _ZCOMP_H_ */
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 42b9c7f..34a0921 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -132,12 +132,9 @@ static ssize_t max_comp_streams_store(struct device *dev,
 	if (num < 1)
 		return -EINVAL;
 	down_write(&zram->init_lock);
-	if (init_done(zram)) {
-		up_write(&zram->init_lock);
-		pr_info("Can't set max_comp_streams for initialized device\n");
-		return -EBUSY;
-	}
 	zram->max_comp_streams = num;
+	if (init_done(zram))
+		zcomp_set_max_streams(zram->comp, num);
 	up_write(&zram->init_lock);
 	return len;
 }
-- 
1.9.0.291.g027825b


  parent reply	other threads:[~2014-02-25 11:38 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-25 11:34 [PATCHv7 0/7] add compressing abstraction and multi stream support Sergey Senozhatsky
2014-02-25 11:34 ` [PATCHv7 1/7] zram: introduce compressing backend abstraction Sergey Senozhatsky
2014-02-25 11:34 ` [PATCHv7 2/7] zram: use zcomp compressing backends Sergey Senozhatsky
2014-02-25 11:34 ` [PATCHv7 3/7] zram: factor out single stream compression Sergey Senozhatsky
2014-02-26  4:34   ` Minchan Kim
2014-02-26 11:32     ` Sergey Senozhatsky
2014-02-25 11:34 ` [PATCHv7 4/7] zram: add multi stream functionality Sergey Senozhatsky
2014-02-26  4:35   ` Minchan Kim
2014-02-25 11:34 ` [PATCHv7 5/7] zram: enable multi stream compression support in zram Sergey Senozhatsky
2014-02-26  4:35   ` Minchan Kim
2014-02-25 11:34 ` Sergey Senozhatsky [this message]
2014-02-26  4:35   ` [PATCHv7 6/7] zram: add set_max_streams knob Minchan Kim
2014-02-25 11:34 ` [PATCHv7 7/7] zram: document max_comp_streams Sergey Senozhatsky

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=1393328073-12313-7-git-send-email-sergey.senozhatsky@gmail.com \
    --to=sergey.senozhatsky@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jmarchan@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minchan@kernel.org \
    --cc=ngupta@vflare.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 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.