public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Minchan Kim <minchan@kernel.org>
To: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Nitin Gupta <ngupta@vflare.org>,
	linux-kernel@vger.kernel.org,
	Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Subject: Re: [PATCHv2 03/10] zram: use idr instead of `zram_devices' array
Date: Thu, 23 Apr 2015 11:23:58 +0900	[thread overview]
Message-ID: <20150423022358.GB24928@blaptop> (raw)
In-Reply-To: <1429185356-11096-4-git-send-email-sergey.senozhatsky@gmail.com>

On Thu, Apr 16, 2015 at 08:55:49PM +0900, Sergey Senozhatsky wrote:
> This patch makes some preparations for on-demand device add/remove
> functionality.
> 
> Remove `zram_devices' array and switch to id-to-pointer translation (idr).
> idr doesn't bloat zram struct with additional members, f.e. list_head,
> yet still provides ability to match the device_id with the device pointer.
> 
> No user-space visible changes.
> 
> [do not lose -ENOMEM return code when `queue' allocation has failed]
> Reported-by: Julia Lawall <Julia.Lawall@lip6.fr>
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> ---
>  drivers/block/zram/zram_drv.c | 86 ++++++++++++++++++++++++-------------------
>  1 file changed, 49 insertions(+), 37 deletions(-)
> 
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 14043b2..4511830 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -32,12 +32,12 @@
>  #include <linux/string.h>
>  #include <linux/vmalloc.h>
>  #include <linux/err.h>
> +#include <linux/idr.h>
>  
>  #include "zram_drv.h"
>  
> -/* Globals */
> +static DEFINE_IDR(zram_index_idr);
>  static int zram_major;
> -static struct zram *zram_devices;
>  static const char *default_compressor = "lzo";
>  
>  /* Module params (documentation at end) */
> @@ -1156,10 +1156,20 @@ static struct attribute_group zram_disk_attr_group = {
>  	.attrs = zram_disk_attrs,
>  };
>  
> -static int create_device(struct zram *zram, int device_id)
> +static int zram_add(int device_id)
>  {
> +	struct zram *zram;
>  	struct request_queue *queue;
> -	int ret = -ENOMEM;
> +	int ret;
> +
> +	zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
> +	if (!zram)
> +		return -ENOMEM;
> +
> +	ret = idr_alloc(&zram_index_idr, zram, device_id,
> +			device_id + 1, GFP_KERNEL);
> +	if (ret < 0)
> +		goto out_free_dev;
>  
>  	init_rwsem(&zram->init_lock);
>  
> @@ -1167,12 +1177,13 @@ static int create_device(struct zram *zram, int device_id)
>  	if (!queue) {
>  		pr_err("Error allocating disk queue for device %d\n",
>  			device_id);
> -		goto out;
> +		ret = -ENOMEM;
> +		goto out_free_idr;
>  	}
>  
>  	blk_queue_make_request(queue, zram_make_request);
>  
> -	 /* gendisk structure */
> +	/* gendisk structure */
>  	zram->disk = alloc_disk(1);
>  	if (!zram->disk) {
>  		pr_warn("Error allocating disk structure for device %d\n",
> @@ -1237,34 +1248,42 @@ out_free_disk:
>  	put_disk(zram->disk);
>  out_free_queue:
>  	blk_cleanup_queue(queue);
> -out:
> +out_free_idr:
> +	idr_remove(&zram_index_idr, device_id);
> +out_free_dev:
> +	kfree(zram);
>  	return ret;
>  }
>  
> -static void destroy_devices(unsigned int nr)
> +static void zram_remove(struct zram *zram)
>  {
> -	struct zram *zram;
> -	unsigned int i;
> -
> -	for (i = 0; i < nr; i++) {
> -		zram = &zram_devices[i];
> -		/*
> -		 * Remove sysfs first, so no one will perform a disksize
> -		 * store while we destroy the devices
> -		 */
> -		sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
> -				&zram_disk_attr_group);
> +	/*
> +	 * Remove sysfs first, so no one will perform a disksize
> +	 * store while we destroy the devices
> +	 */
> +	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
> +			&zram_disk_attr_group);
>  
> -		zram_reset_device(zram);
> +	zram_reset_device(zram);
> +	idr_remove(&zram_index_idr, zram->disk->first_minor);
> +	blk_cleanup_queue(zram->disk->queue);
> +	del_gendisk(zram->disk);
> +	put_disk(zram->disk);
> +	kfree(zram);
> +}
>  
> -		blk_cleanup_queue(zram->disk->queue);
> -		del_gendisk(zram->disk);
> -		put_disk(zram->disk);
> -	}
> +static int zram_exit_cb(int id, void *ptr, void *data)

trivial: I prefer remove to exit.

> +{
> +	zram_remove(ptr);
> +	return 0;
> +}
>  
> -	kfree(zram_devices);
> +static void destroy_devices(void)
> +{
> +	idr_for_each(&zram_index_idr, &zram_exit_cb, NULL);
> +	idr_destroy(&zram_index_idr);
>  	unregister_blkdev(zram_major, "zram");
> -	pr_info("Destroyed %u device(s)\n", nr);
> +	pr_info("Destroyed device(s)\n");
>  }
>  
>  static int __init zram_init(void)
> @@ -1283,16 +1302,9 @@ static int __init zram_init(void)
>  		return -EBUSY;
>  	}
>  
> -	/* Allocate the device array and initialize each one */
> -	zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
> -	if (!zram_devices) {
> -		unregister_blkdev(zram_major, "zram");
> -		return -ENOMEM;
> -	}
> -
>  	for (dev_id = 0; dev_id < num_devices; dev_id++) {
> -		ret = create_device(&zram_devices[dev_id], dev_id);
> -		if (ret)
> +		ret = zram_add(dev_id);
> +		if (ret != 0)

It's better to check ret < 0 rather than ret != 0.

Otherwise,
Acked-by: Minchan Kim <minchan@kernel.org>

-- 
Kind regards,
Minchan Kim

  reply	other threads:[~2015-04-23  2:24 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-16 11:55 [PATCHv2 00/10] cleaned up on-demand device creation Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 01/10] zram: enable compaction support in zram Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 02/10] zram: cosmetic ZRAM_ATTR_RO code formatting tweak Sergey Senozhatsky
2015-04-23  2:16   ` Minchan Kim
2015-04-16 11:55 ` [PATCHv2 03/10] zram: use idr instead of `zram_devices' array Sergey Senozhatsky
2015-04-23  2:23   ` Minchan Kim [this message]
2015-04-23  4:30     ` Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 04/10] zram: factor out device reset from reset_store() Sergey Senozhatsky
2015-04-23  2:29   ` Minchan Kim
2015-04-23  4:26     ` Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 05/10] zram: reorganize code layout Sergey Senozhatsky
2015-04-23  2:32   ` Minchan Kim
2015-04-16 11:55 ` [PATCHv2 06/10] zram: remove max_num_devices limitation Sergey Senozhatsky
2015-04-23  2:36   ` Minchan Kim
2015-04-23  4:24     ` Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 07/10] zram: report every added and removed device Sergey Senozhatsky
2015-04-23  2:38   ` Minchan Kim
2015-04-23  4:23     ` Sergey Senozhatsky
2015-04-16 11:55 ` [PATCHv2 08/10] zram: trivial: correct flag operations comment Sergey Senozhatsky
2015-04-23  2:40   ` Minchan Kim
2015-04-16 11:55 ` [PATCHv2 09/10] zram: return zram device_id value from zram_add() Sergey Senozhatsky
2015-04-23  2:41   ` Minchan Kim
2015-04-16 11:55 ` [PATCHv2 10/10] zram: add dynamic device add/remove functionality Sergey Senozhatsky
2015-04-23  3:06   ` Minchan Kim
2015-04-23  3:12     ` Minchan Kim
2015-04-23  4:23     ` Sergey Senozhatsky
2015-04-23  6:20       ` Minchan Kim
2015-04-16 23:23 ` [PATCHv2 00/10] cleaned up on-demand device creation Minchan Kim
2015-04-17  0:27   ` Sergey Senozhatsky
2015-04-17  0:39   ` Sergey Senozhatsky
2015-04-17  1:00 ` Sergey Senozhatsky
2015-04-17  1:32   ` 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=20150423022358.GB24928@blaptop \
    --to=minchan@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ngupta@vflare.org \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=sergey.senozhatsky@gmail.com \
    /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