Skip to contentSkip to conversation

RFC: Changing dm core data structure relationships (0/5)

PATCH New Jun'ichi Nomura 1 participant 6 messages Mar 17, 2006 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RFC: Changing dm core data structure relationships (0/5)

Hello,

This is a proposal of changing dm core structure for simplification, SMP race avoidance and guaranteed release ordering. Patches don't affect dm targets.

I would like to hear your opinion on this.

The core motivation for these changes are followings:
- Device open and remove can race.
As a result, the device we opened might be freed by someone.
- We lost control on device when it's removed.
e.g. if the device is opened and suspended, we no longer really remove the device.
- With dm-table-store-md.patch in -mm, table has reference to md. So we would like to guarantee that table is freed before md.
It's achived in the third patch.

Patches are created and tested based on 2.6.16-rc6-mm1 plus:
- dm-flush-queue-eintr.patch
- dm-tidy-mdptr.patch
- dm-table-store-md.patch

  - dm-md-dependency-tree-in-sysfs-add_subdirs.patch                           14-Mar-2006 18:33  3.2K
  - dm-md-dependency-tree-in-sysfs-bd_claim_by_kobj.patch                      14-Mar-2006 18:33  9.5K
  - dm-md-dependency-tree-in-sysfs-dm_deptree.patch                            14-Mar-2006 18:33  2.8K
  - dm-md-dependency-tree-in-sysfs-kobject_add_dir.patch                       14-Mar-2006 18:33  1.9K

- dm-md-dependency-tree-in-sysfs-md_deptree.patch

Thanks,

Signature · 105 lines
Jun'ichi Nomura, NEC Solutions (America), Inc.

--------------------------------------------------------------------------
Description of patches

01-dm-sem.patch
  Literally replace _hash_lock with dm_sem. Make it extern.

02-put-table-after-unlock.patch
  Moving dm_table_put() which may destroy table to outside of dm_sem.
  This is for next patches which dm_sem is held is device close
  thus table destructor may otherwise deadlock.

03-hc-open-count.patch
  Moving open counter to hc. Disables removal of hc with openers.
  md->holders become purely a reference counter of md.
    * disk->private_data points to hc rather than md.
      When hc is removed, disk->private_data becomes NULL and no new open
      is possible.
    * Open count is protected by dm_sem to avoid open vs. remove race.
    * Upon hc removal, tables are released from md.
    * Table gets reference to md on creation, puts on free.
      Thus we can guarantee md is not freed before its tables are feed.
    * dm_get_md(), which maps minor to md, checks interface_ptr to avoid
      getting md being removed.

04-remove-dm-md-get.patch
    * dm_get_md() is removed. There is no user.

05-move-new-map-to-md.patch
  Moving hc->new_map to md->new_map.
    * Map loading no longer needs global dm_sem.
      Just grab md and lock map_lock.
    * Most of __find_device_hash_cell() can be replaced by find_device().

Should be applied in this order.

Concerns:
  - Is moving dm_blk_open/close to dm-ioctl.c right thing?
    Is it nice to rename dm-ioctl.c to dm-interface.c or something?
  - Does other .c files need to get hc->count?
  - dm_table_get_md() really needs dm_get()?
    We can assume md exists whenever the table exists.

--------------------------------------------------------------------
Structures:
  - mapped_device (md)
  - hash_cell (hc)
  - dm_table (table)
  - gendisk (disk)
  - request_queue (queue)
  - minor number (minor)

Relationships:
  - hc owns 1 md
  - md owns 0-2 tables, 1 disk
  - disk owns 1 queue and 1 minor number

Roles:
  - hc manages access from outside of dm (block device, ioctl).
    It has open counter. hc can be removed from list only when
    there is no opener.
  - md is a core of dm device. Access to table needs to go through md.

Locks:
  - dm_sem: hc list, gendisk->dm device binding
  - io_lock: DMF_BLOCK_IO, md->deferred, md->wait
  - map_lock: md->map, md->new_map
  - suspend_lock: DMF_SUSPENDED

Ordering:
  - Allocation/Register
       1) allocation of md, hc, disk, queue, minor can occur in any order
       2) register md to disk
       3) register disk (it eventually register minor and queue)
          <device visible from block layer>
       4) register md to hc
          <block device access enabled>
       5) register hc to hash list
          <ioctl access enabled>
       6) table allocation (load)
       7) table register (resume -> bind)
          <I/O enabled>
  - Freeing/Unregister (reordered proposal)
       [lock dm_sem]
       3) unregister hc from hash list
          <ioctl access disabled>
       4) unregister md from hc
       6) unregister md from disk
          <block device access rejected>
       [unlock dm_sem]
       1) table unregister (resume -> unbind)
          <I/O disabled>
       2) table free
       5) unregister disk
          <block device access disabled>
       7) free in any order

       (*) Moving 1) and 2) later has a benefit that we don't
           need to care for racing with new table registration.
       (*) Swapping the order of 5) and 6), because 5) eventually
           cause kobject_uevent(hotplug) which may sleep.
           It's not good thing to do in global dm_sem.
           Doing 6) makes future open to fail so it has same shutdown
           effect and faster.
#1 lore raw

PATCH 1/1 RFC: Changing dm core (1/5): Make _hash_lock extern

This patch makes _hash_lock extern so that dm.c can touch it. It's renamed to dm_sem.

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Rename _hash_lock to dm_sem and make it extern for dm.c.

drivers/md/dm.h +1 -0
@@ -172,6 +172,7 @@ int dm_split_args(int *argc, char ***arg
  */
 int dm_interface_init(void);
 void dm_interface_exit(void);
+extern struct rw_semaphore dm_sem;
 
 /*
  * Targets for linear and striped mappings
drivers/md/dm-ioctl.c +26 -26

196 lines not on this page · raw message

#2 lore raw

PATCH 1/1 RFC: Changing dm core (2/5) : Put_table in safer place

In reply to Jun'ichi Nomura

Move dm_table_put() outside of dm_sem.

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Move dm_table_put() outside of dm_sem.

 dm-ioctl.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
drivers/md/dm-ioctl.c +11 -4
@@ -984,7 +984,7 @@ static int table_load(struct dm_ioctl *p
 {
 	int r;
 	struct hash_cell *hc;
-	struct dm_table *t;
+	struct dm_table *t, *oldmap = NULL;
 	struct mapped_device *md;
 
 	md = find_device(param);
@@ -1005,14 +1005,14 @@ static int table_load(struct dm_ioctl *p
 	hc = dm_get_mdptr(md);
 	if (!hc || hc->md != md) {
 		DMWARN("device has been removed from the dev hash table.");
-		dm_table_put(t);
+		oldmap = t;
 		up_write(&dm_sem);
 		r = -ENXIO;
 		goto out;
 	}
 
 	if (hc->new_map)
-		dm_table_put(hc->new_map);
+		oldmap = hc->new_map;
 	hc->new_map = t;
 	up_write(&dm_sem);
 
@@ -1020,6 +1020,8 @@ static int table_load(struct dm_ioctl *p
 	r = __dev_status(md, param);
 
 out:
+	if (oldmap)
+		dm_table_put(oldmap);
 	dm_put(md);
 
 	return r;
@@ -1029,6 +1031,7 @@ static int table_clear(struct dm_ioctl *
 {
 	int r;
 	struct hash_cell *hc;
+	struct dm_table *oldmap = NULL;
 
 	down_write(&dm_sem);
 
@@ -1040,7 +1043,7 @@ static int table_clear(struct dm_ioctl *
 	}
 
 	if (hc->new_map) {
-		dm_table_put(hc->new_map);
+		oldmap = hc->new_map;
 		hc->new_map = NULL;
 	}
 
@@ -1048,6 +1051,10 @@ static int table_clear(struct dm_ioctl *
 
 	r = __dev_status(hc->md, param);
 	up_write(&dm_sem);
+
+	if (oldmap)
+		dm_table_put(oldmap);
+
 	return r;
 }
 
#3 lore raw

PATCH 1/1 RFC: Changing dm core (3/5): hash_cell open counter

In reply to Jun'ichi Nomura

Move open counter from md to hash_cell.
The counter is checked at hash_cell removal.
Non-zero counter disallows removal.

As a result, md->holders becomes pure reference counter of md. It guarantees md is not freed until all tables are freed.

By protecting the counter, the race between open and remove can be avoided.

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Move open counter from md to hash_cell.
The counter is checked at hash_cell removal.
Non-zero counter disallows removal.

As a result, md->holders becomes pure reference counter of md. It guarantees md is not freed until all tables are freed.

 dm-ioctl.c |  133 ++++++++++++++++++++++++++++++++++++++++++++++---------------
 dm-table.c |    2 
 dm.c       |   63 +++++++++++++---------------
 dm.h       |    1 
 4 files changed, 134 insertions(+), 65 deletions(-)
drivers/md/dm.c +30 -33
@@ -206,27 +206,6 @@ static void __exit dm_exit(void)
 		_exits[i]();
 }
 
-/*
- * Block device functions
- */
-static int dm_blk_open(struct inode *inode, struct file *file)
-{
-	struct mapped_device *md;
-
-	md = inode->i_bdev->bd_disk->private_data;
-	dm_get(md);
-	return 0;
-}
-
-static int dm_blk_close(struct inode *inode, struct file *file)
-{
-	struct mapped_device *md;
-
-	md = inode->i_bdev->bd_disk->private_data;
-	dm_put(md);
-	return 0;
-}
-
 static inline struct dm_io *alloc_io(struct mapped_device *md)
 {
 	return mempool_alloc(md->io_pool, GFP_NOIO);
@@ -835,7 +814,6 @@ static struct mapped_device *alloc_dev(u
 	md->disk->first_minor = minor;
 	md->disk->fops = &dm_blk_dops;
 	md->disk->queue = md->queue;
-	md->disk->private_data = md;
 	sprintf(md->disk->disk_name, "dm-%d", minor);
 	add_disk(md->disk);
 
@@ -861,6 +839,11 @@ static void free_dev(struct mapped_devic
 {
 	unsigned int minor = md->disk->first_minor;
 
+	if (atomic_read(&md->holders) || md->interface_ptr) {
+		DMERR("Trying to free a device which still has user");
+		BUG();
+	}
+
 	if (md->suspended_bdev) {
 		thaw_bdev(md->suspended_bdev, NULL);
 		bdput(md->suspended_bdev);
@@ -978,8 +961,10 @@ struct mapped_device *dm_get_md(dev_t de
 {
 	struct mapped_device *md = dm_find_md(dev);
 
-	if (md)
-		dm_get(md);
+	if (!md || !md->interface_ptr)
+		return NULL;
+
+	dm_get(md);
 
 	return md;
 }
@@ -1001,18 +986,25 @@ void dm_get(struct mapped_device *md)
 
 void dm_put(struct mapped_device *md)
 {
+	if (atomic_dec_and_test(&md->holders))
+		free_dev(md);
+}
+
+/*
+ * Called when md is released from interface container
+ */
+void dm_free(struct mapped_device *md)
+{
 	struct dm_table *map;
 
-	if (atomic_dec_and_test(&md->holders)) {
-		map = dm_get_table(md);
-		if (!dm_suspended(md)) {
-			dm_table_presuspend_targets(map);
-			dm_table_postsuspend_targets(map);
-		}
-		__unbind(md);
-		dm_table_put(map);
-		free_dev(md);
+	map = dm_get_table(md);
+	if (!dm_suspended(md)) {
+		dm_table_presuspend_targets(map);
+		dm_table_postsuspend_targets(map);
 	}
+	dm_table_put(map);
+	__unbind(md);
+	dm_put(md); /* this likely to be the last put */
 }
 
 /*
@@ -1252,6 +1244,11 @@ int dm_suspended(struct mapped_device *m
 	return test_bit(DMF_SUSPENDED, &md->flags);
 }
 
+/*
+ * Block device interface
+ */
+int dm_blk_open(struct inode *inode, struct file *file);
+int dm_blk_close(struct inode *inode, struct file *file);
 static struct block_device_operations dm_blk_dops = {
 	.open = dm_blk_open,
 	.release = dm_blk_close,
drivers/md/dm.h +1 -0
@@ -55,6 +55,7 @@ struct mapped_device *dm_get_md(dev_t de
  */
 void dm_get(struct mapped_device *md);
 void dm_put(struct mapped_device *md);
+void dm_free(struct mapped_device *md);
 
 /*
  * A device can still be used while suspended, but I/O is deferred.
drivers/md/dm-ioctl.c +101 -32

291 lines not on this page · raw message

drivers/md/dm-table.c +2 -0
@@ -233,6 +233,7 @@ int dm_table_create(struct dm_table **re
 
 	t->mode = mode;
 	t->md = md;
+	dm_get(md);
 	*result = t;
 	return 0;
 }
@@ -276,6 +277,7 @@ static void table_destroy(struct dm_tabl
 		free_devices(&t->devices);
 	}
 
+	dm_put(t->md);
 	kfree(t);
 }
 
#4 lore raw

PATCH 1/1 RFC: Changing dm core (4/5): remove dm_get_md()

In reply to Jun'ichi Nomura

Remove dm_get_md(), which is no longer used.

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Remove dm_get_md(), which is no longer used.

 dm.c |   31 -------------------------------
 dm.h |    1 -
 2 files changed, 32 deletions(-)
drivers/md/dm.c +0 -31
@@ -938,37 +938,6 @@ int dm_create_with_minor(unsigned int mi
 	return create_aux(minor, 1, result);
 }
 
-static struct mapped_device *dm_find_md(dev_t dev)
-{
-	struct mapped_device *md;
-	unsigned minor = MINOR(dev);
-
-	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
-		return NULL;
-
-	mutex_lock(&_minor_lock);
-
-	md = idr_find(&_minor_idr, minor);
-	if (!md || (dm_disk(md)->first_minor != minor))
-		md = NULL;
-
-	mutex_unlock(&_minor_lock);
-
-	return md;
-}
-
-struct mapped_device *dm_get_md(dev_t dev)
-{
-	struct mapped_device *md = dm_find_md(dev);
-
-	if (!md || !md->interface_ptr)
-		return NULL;
-
-	dm_get(md);
-
-	return md;
-}
-
 void *dm_get_mdptr(struct mapped_device *md)
 {
 	return md->interface_ptr;
drivers/md/dm.h +0 -1
@@ -48,7 +48,6 @@ int dm_create(struct mapped_device **md)
 int dm_create_with_minor(unsigned int minor, struct mapped_device **md);
 void dm_set_mdptr(struct mapped_device *md, void *ptr);
 void *dm_get_mdptr(struct mapped_device *md);
-struct mapped_device *dm_get_md(dev_t dev);
 
 /*
  * Reference counting for md.
#5 lore raw

PATCH 1/1 RFC: Changing dm core: (5/5): Move new_map from hash_cell to mapped_device

In reply to Jun'ichi Nomura

Move new_map from hash_cell to mapped_device.

This simplifies dm-ioctl.c a bit.

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Move new_map from hash_cell to mapped_device.

 dm-ioctl.c |   72 +++++++++++--------------------------------------------------
 dm.c       |   58 +++++++++++++++++++++++++++++++++++++++++++++++--
 dm.h       |   10 +++++++-
 3 files changed, 78 insertions(+), 62 deletions(-)
drivers/md/dm.c +56 -2
@@ -84,6 +84,10 @@ struct mapped_device {
 	 * The current mapping.
 	 */
 	struct dm_table *map;
+	/*
+	 * Loaded, inactive mapping.
+	 */
+	struct dm_table *new_map;
 
 	/*
 	 * io objects are allocated from here.
@@ -877,6 +881,18 @@ static void __set_size(struct mapped_dev
 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
 }
 
+static struct dm_table *__extract_new_map(struct mapped_device *md)
+{
+	struct dm_table *t;
+
+	write_lock(&md->map_lock);
+	t = md->new_map;
+	md->new_map = NULL;
+	write_unlock(&md->map_lock);
+
+	return t;
+}
+
 static int __bind(struct mapped_device *md, struct dm_table *t)
 {
 	request_queue_t *q = md->queue;
@@ -887,7 +903,6 @@ static int __bind(struct mapped_device *
 	if (size == 0)
 		return 0;
 
-	dm_table_get(t);
 	dm_table_event_callback(t, event_callback, md);
 
 	write_lock(&md->map_lock);
@@ -966,6 +981,7 @@ void dm_free(struct mapped_device *md)
 {
 	struct dm_table *map;
 
+	dm_table_put(md->new_map);
 	map = dm_get_table(md);
 	if (!dm_suspended(md)) {
 		dm_table_presuspend_targets(map);
@@ -994,9 +1010,10 @@ static void __flush_deferred_io(struct m
 /*
  * Swap in a new table (destroying old one).
  */
-int dm_swap_table(struct mapped_device *md, struct dm_table *table)
+int dm_swap_table(struct mapped_device *md)
 {
 	int r = -EINVAL;
+	sturct dm_table *table;
 
 	down(&md->suspend_lock);
 
@@ -1004,6 +1021,13 @@ int dm_swap_table(struct mapped_device *
 	if (!dm_suspended(md))
 		goto out;
 
+	/* no-op if no table is loaded */
+	table = __extract_new_map(md);
+	if (!table) {
+		r = 0;
+		goto out;
+	}
+
 	__unbind(md);
 	r = __bind(md, table);
 
@@ -1012,6 +1036,26 @@ out:
 	return r;
 }
 
+ /*
+  * Load a new table (or clear a loaded table if t == NULL).
+  * Should be called outside of dm_sem.
+  */
+int dm_load_table(struct mapped_device *md, struct dm_table *t)
+{
+	struct dm_table *old_map = NULL;
+
+	write_lock(&md->map_lock);
+	if (md->new_map)
+		old_map = md->new_map;
+	md->new_map = t;
+	write_unlock(&md->map_lock);
+
+	if (old_map)
+		dm_table_put(old_map);
+
+	return 1;
+}
+
 /*
  * Functions to lock and unlock any filesystem running on the
  * device.
@@ -1213,6 +1257,16 @@ int dm_suspended(struct mapped_device *m
 	return test_bit(DMF_SUSPENDED, &md->flags);
 }
 
+int dm_inactive_present(struct mapped_device *md)
+{
+	return md->new_map != NULL;
+}
+
+int dm_get_mode(struct mapped_device *md)
+{
+	return md->map ? dm_table_get_mode(md->map) : 0;
+}
+
 /*
  * Block device interface
  */
drivers/md/dm.h +9 -1
@@ -65,7 +65,13 @@ int dm_resume(struct mapped_device *md);
 /*
  * The device must be suspended before calling this method.
  */
-int dm_swap_table(struct mapped_device *md, struct dm_table *t);
+int dm_swap_table(struct mapped_device *md);
+
+/*
+ * No lock is needed
+ */
+int dm_load_table(struct mapped_device *md, struct dm_table *t);
+
 
 /*
  * Drop a reference on the table when you've finished with the
@@ -84,6 +90,8 @@ int dm_wait_event(struct mapped_device *
  */
 struct gendisk *dm_disk(struct mapped_device *md);
 int dm_suspended(struct mapped_device *md);
+int dm_inactive_present(struct mapped_device *md);
+int dm_get_mode(struct mapped_device *md);
 
 /*-----------------------------------------------------------------
  * Functions for manipulating a table.  Tables are also reference
drivers/md/dm-ioctl.c +13 -59

157 lines not on this page · raw message