mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Minchan Kim <minchan@kernel.org>
To: Yi Wang <wang.yi59@zte.com.cn>
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,
	jiang.xuexin@zte.com.cn, zhanglin <zhang.lin16@zte.com.cn>
Subject: Re: [PATCH v3] zram: add restriction on dynamic zram device creation
Date: Sun, 27 Sep 2020 12:09:48 -0700	[thread overview]
Message-ID: <20200927190948.GB422329@google.com> (raw)
In-Reply-To: <1600651821-46836-1-git-send-email-wang.yi59@zte.com.cn>

On Mon, Sep 21, 2020 at 09:30:21AM +0800, Yi Wang wrote:
> 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>
> Signed-off-by: Yi Wang <wang.yi59@zte.com.cn>
> ---
>  v1->v2:
>  change hard-coded initial max_num_devices into configurable way.
>  v2->v3:
>  Move inc/dec num_devices logic into zram_add/zram_remove.
> 
>  drivers/block/zram/Kconfig    |  7 +++++++
>  drivers/block/zram/zram_drv.c | 26 +++++++++++++++-----------
>  2 files changed, 22 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig
> index fe7a4b7d30cf..8455e5ec608b 100644
> --- a/drivers/block/zram/Kconfig
> +++ b/drivers/block/zram/Kconfig
> @@ -37,3 +37,10 @@ config ZRAM_MEMORY_TRACKING
>  	  /sys/kernel/debug/zram/zramX/block_state.
>  
>  	  See Documentation/admin-guide/blockdev/zram.rst for more information.
> +
> +config ZRAM_DEV_MAX_COUNT
> +	int "Max number of zram devices to be created"
> +	depends on ZRAM
> +	default 256
> +	help
> +	  This option specifies the maximum number of zram devices.
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 36d49159140f..5e8978095d68 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";
>  
> +static unsigned int num_devices;
>  /* Module params (documentation at end) */
> -static unsigned int num_devices = 1;
> +static unsigned int max_num_devices = CONFIG_ZRAM_DEV_MAX_COUNT;
>  /*
>   * Pages that compress to sizes equals or greater than this are stored
>   * uncompressed in memory.
> @@ -1878,6 +1879,9 @@ static int zram_add(void)
>  	struct request_queue *queue;
>  	int ret, device_id;
>  
> +	if (num_devices >= max_num_devices)
> +		return -ENOSPC;
> +
>  	zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
>  	if (!zram)
>  		return -ENOMEM;
> @@ -1955,6 +1959,7 @@ static int zram_add(void)
>  	strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
>  
>  	zram_debugfs_register(zram);
> +	num_devices += 1;
>  	pr_info("Added device: %s\n", zram->disk->disk_name);
>  	return device_id;
>  
> @@ -1998,6 +2003,7 @@ static int zram_remove(struct zram *zram)
>  	blk_cleanup_queue(zram->disk->queue);
>  	put_disk(zram->disk);
>  	kfree(zram);
> +	num_devices -= 1;
>  	return 0;
>  }
>  
> @@ -2111,14 +2117,12 @@ static int __init zram_init(void)
>  		return -EBUSY;
>  	}
>  
> -	while (num_devices != 0) {
> -		mutex_lock(&zram_index_mutex);
> -		ret = zram_add();
> -		mutex_unlock(&zram_index_mutex);
> -		if (ret < 0)
> -			goto out_error;
> -		num_devices--;
> -	}
> +	mutex_lock(&zram_index_mutex);
> +	num_devices = 0;
> +	ret = zram_add();
> +	mutex_unlock(&zram_index_mutex);
> +	if (ret < 0)
> +		goto out_error;
>  
>  	return 0;
>  
> @@ -2135,8 +2139,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");

Why did you remove num_devices? It's orthgonal with max_num_devices.
If we remove it, users have been used it to create pre-created zram
device will be broken.

If user asked it more than max_num_devices, we could just return the error
in zram_init with simple pr_err to notice configuration wrong.


> +module_param(max_num_devices, uint, 0);
> +MODULE_PARM_DESC(max_num_devices, "Max number of zram devices to be created");
>  
>  MODULE_LICENSE("Dual BSD/GPL");
>  MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
> -- 
> 2.17.1



           reply	other threads:[~2020-09-27 19:09 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <1600651821-46836-1-git-send-email-wang.yi59@zte.com.cn>]

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=20200927190948.GB422329@google.com \
    --to=minchan@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=jiang.xuexin@zte.com.cn \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ngupta@vflare.org \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=wang.yi59@zte.com.cn \
    --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

all inboxes | Powered by JetHome®