linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yi Wang <wang.yi59@zte.com.cn>
To: minchan@kernel.org
Cc: ngupta@vflare.org, sergey.senozhatsky.work@gmail.com,
	axboe@kernel.dk, linux-kernel@vger.kernel.org,
	linux-block@vger.kernel.org, xue.zhihong@zte.com.cn,
	wang.yi59@zte.com.cn, jiang.xuexin@zte.com.cn,
	zhanglin <zhang.lin16@zte.com.cn>
Subject: [PATCH] zram: add restriction on dynamic zram device creation
Date: Tue, 1 Sep 2020 14:42:47 +0800	[thread overview]
Message-ID: <1598942567-33440-1-git-send-email-wang.yi59@zte.com.cn> (raw)

From: zhanglin <zhang.lin16@zte.com.cn>

Add max_num_devices to limit dynamic zram device creation to prevent
 potential OOM

Signed-off-by: zhanglin <zhang.lin16@zte.com.cn>
---
 drivers/block/zram/zram_drv.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 36d49159140f..4f2c4eef5051 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -43,8 +43,9 @@ static DEFINE_MUTEX(zram_index_mutex);
 static int zram_major;
 static const char *default_compressor = "lzo-rle";
 
-/* Module params (documentation at end) */
 static unsigned int num_devices = 1;
+/* Module params (documentation at end) */
+static unsigned int max_num_devices = 8;
 /*
  * Pages that compress to sizes equals or greater than this are stored
  * uncompressed in memory.
@@ -2013,10 +2014,16 @@ static ssize_t hot_add_show(struct class *class,
 			struct class_attribute *attr,
 			char *buf)
 {
-	int ret;
+	int ret = -ENOSPC;
 
 	mutex_lock(&zram_index_mutex);
+	if (num_devices >= max_num_devices) {
+		mutex_unlock(&zram_index_mutex);
+		return ret;
+	}
 	ret = zram_add();
+	if (ret >= 0)
+		num_devices += 1;
 	mutex_unlock(&zram_index_mutex);
 
 	if (ret < 0)
@@ -2046,8 +2053,10 @@ static ssize_t hot_remove_store(struct class *class,
 	zram = idr_find(&zram_index_idr, dev_id);
 	if (zram) {
 		ret = zram_remove(zram);
-		if (!ret)
+		if (!ret) {
 			idr_remove(&zram_index_idr, dev_id);
+			num_devices -= 1;
+		}
 	} else {
 		ret = -ENODEV;
 	}
@@ -2089,6 +2098,7 @@ static void destroy_devices(void)
 static int __init zram_init(void)
 {
 	int ret;
+	unsigned int i;
 
 	ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
 				      zcomp_cpu_up_prepare, zcomp_cpu_dead);
@@ -2111,13 +2121,15 @@ static int __init zram_init(void)
 		return -EBUSY;
 	}
 
-	while (num_devices != 0) {
+	if (num_devices > max_num_devices)
+		num_devices = max_num_devices;
+
+	for (i = 0; i < num_devices; i++) {
 		mutex_lock(&zram_index_mutex);
 		ret = zram_add();
 		mutex_unlock(&zram_index_mutex);
 		if (ret < 0)
 			goto out_error;
-		num_devices--;
 	}
 
 	return 0;
@@ -2135,8 +2147,8 @@ static void __exit zram_exit(void)
 module_init(zram_init);
 module_exit(zram_exit);
 
-module_param(num_devices, uint, 0);
-MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
+module_param(max_num_devices, uint, 0);
+MODULE_PARM_DESC(max_num_devices, "Max number of created zram devices");
 
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
-- 
2.17.1


             reply	other threads:[~2020-09-01  6:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-01  6:42 Yi Wang [this message]
2020-09-03 19:53 ` [PATCH] zram: add restriction on dynamic zram device creation Minchan Kim

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=1598942567-33440-1-git-send-email-wang.yi59@zte.com.cn \
    --to=wang.yi59@zte.com.cn \
    --cc=axboe@kernel.dk \
    --cc=jiang.xuexin@zte.com.cn \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minchan@kernel.org \
    --cc=ngupta@vflare.org \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=xue.zhihong@zte.com.cn \
    --cc=zhang.lin16@zte.com.cn \
    /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).