dm: make "dmsetup remove_all" interruptible
Mikulas Patocka
maintainerPATCH 1/1 dm: make "dmsetup remove_all" interruptible
The command "dmsetup remove_all" may take a long time (a minute for removing 1000 devices), so make it interruptible with fatal signals.
--- drivers/md/dm-ioctl.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/md/dm-ioctl.c +13 -6
@@ -64,7 +64,7 @@ struct vers_iter {static struct rb_root name_rb_tree =RB_ROOT ;static struct rb_root uuid_rb_tree =RB_ROOT ; -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred); +static int dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred,bool interruptible);/* * Guards access to both hash tables.
@@ -78,7 +78,7 @@ static DEFINE_MUTEX(dm_hash_cells_mutex)static void dm_hash_exit (void ) { -dm_hash_remove_all (false, false, false); +dm_hash_remove_all (false, false, false, false); }/*
@@ -333,7 +333,7 @@ static struct dm_table *__hash_remove(streturn table; } -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred) +static int dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred,bool interruptible) {int dev_skipped;struct rb_node *n;
@@ -347,6 +347,11 @@ retry:down_write (&_hash_lock);for (n =rb_first (&name_rb_tree); n; n =rb_next (n)) { +if (interruptible &&fatal_signal_pending (current)) { +up_write (&_hash_lock); +return -EINTR ; + } + hc = container_of(n, struct hash_cell, name_node); md = hc->md;dm_get (md);
@@ -386,6 +391,8 @@ retry:if (dev_skipped && !only_deferred)DMWARN ("remove_all left %d open device(s)" , dev_skipped); + +return 0 ; }/*
@@ -513,7 +520,7 @@ static struct mapped_device *dm_hash_renvoid dm_deferred_remove (void ) { -dm_hash_remove_all (true, false, true); +dm_hash_remove_all (true, false, true, false); }/*
@@ -529,9 +536,9 @@ typedef int (*ioctl_fn)(struct file *filstatic int remove_all (struct file *filp,struct dm_ioctl *param,size_t param_size) { -dm_hash_remove_all (true, !!(param->flags &DM_DEFERRED_REMOVE ), false); +int r =dm_hash_remove_all (true, !!(param->flags &DM_DEFERRED_REMOVE ), false, true); param->data_size =0 ; -return 0 ; +return r; }/*
drivers/md/dm-ioctl.c static DEFINE_MUTEX(dm_hash_cells_mutex) · line 82
Quoted 4 lines · Mikulas Patocka
{drivers/md/dm-ioctl.c +1
-1-dm_hash_remove_all (false, false, false); +dm_hash_remove_all (false, false, false, false); }
Three boolean arguments in a row make dm_hash_remove_all() calls much harder to read than necessary. Has it been considered to combine the three boolean arguments into a single flags argument?
Thanks,
Bart.
Mikulas Patocka
maintainerPATCH v2 1/1 dm: make "dmsetup remove_all" interruptible
Quoted 11 lines · Bart Van Assche
Review
Quoted 4 lines · Mikulas Patocka
{(quoted diff) +1
-1- dm_hash_remove_all(false, false, false); + dm_hash_remove_all(false, false, false, false); }Three boolean arguments in a row make
dm_hash_remove_all()calls much harder to read than necessary. Has it been considered to combine the three boolean arguments into a single flags argument?
Quoted 3 lines
Thanks,
Bart.
OK - I can change it to flags.
From: Mikulas Patocka <[email protected]>
The command "dmsetup remove_all" may take a long time (a minute for removing 1000 devices), so make it interruptible with fatal signals.
For better readability, the bool arguments were changed to flags.
--- drivers/md/dm-ioctl.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/md/dm-ioctl.c +21 -10
@@ -64,7 +64,11 @@ struct vers_iter {static struct rb_root name_rb_tree =RB_ROOT ;static struct rb_root uuid_rb_tree =RB_ROOT ; -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred); +#define KEEP_OPEN_DEVICES 1 +#define MARK_DEFERRED 2 +#define ONLY_DEFERRED 4 +#define INTERRUPTIBLE 8 +static int dm_hash_remove_all (unsigned flags);/* * Guards access to both hash tables.
@@ -78,7 +82,7 @@ static DEFINE_MUTEX(dm_hash_cells_mutex)static void dm_hash_exit (void ) { -dm_hash_remove_all (false, false, false); +dm_hash_remove_all (0 ); }/*
@@ -333,7 +337,7 @@ static struct dm_table *__hash_remove(streturn table; } -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred) +static int dm_hash_remove_all (unsigned flags) {int dev_skipped;struct rb_node *n;
@@ -347,12 +351,17 @@ retry:down_write (&_hash_lock);for (n =rb_first (&name_rb_tree); n; n =rb_next (n)) { +if (flags &INTERRUPTIBLE &&fatal_signal_pending (current)) { +up_write (&_hash_lock); +return -EINTR ; + } + hc = container_of(n, struct hash_cell, name_node); md = hc->md;dm_get (md); -if (keep_open_devices && -dm_lock_for_deletion (md, mark_deferred, only_deferred)) { +if (flags &KEEP_OPEN_DEVICES && +dm_lock_for_deletion (md, !!(flags &MARK_DEFERRED ), !!(flags &ONLY_DEFERRED ))) {dm_put (md); dev_skipped++;continue ;
@@ -368,7 +377,7 @@ retry: }dm_ima_measure_on_device_remove (md, true);dm_put (md); -if (likely (keep_open_devices)) +if (likely (flags &KEEP_OPEN_DEVICES ))dm_destroy (md);else dm_destroy_immediate (md);
@@ -384,8 +393,10 @@ retry:up_write (&_hash_lock); -if (dev_skipped && !only_deferred) +if (dev_skipped && !(flags &ONLY_DEFERRED ))DMWARN ("remove_all left %d open device(s)" , dev_skipped); + +return 0 ; }/*
@@ -513,7 +524,7 @@ static struct mapped_device *dm_hash_renvoid dm_deferred_remove (void ) { -dm_hash_remove_all (true, false, true); +dm_hash_remove_all (KEEP_OPEN_DEVICES |ONLY_DEFERRED ); }/*
@@ -529,9 +540,9 @@ typedef int (*ioctl_fn)(struct file *filstatic int remove_all (struct file *filp,struct dm_ioctl *param,size_t param_size) { -dm_hash_remove_all (true, !!(param->flags &DM_DEFERRED_REMOVE ), false); +int r =dm_hash_remove_all (KEEP_OPEN_DEVICES | (param->flags &DM_DEFERRED_REMOVE ?MARK_DEFERRED :0 ) |INTERRUPTIBLE ); param->data_size =0 ; -return 0 ; +return r; }/*
Quoted 4 lines · Mikulas Patocka
The command "dmsetup
remove_all" may take a long time (a minute for removing 1000 devices), so make it interruptible with fatal signals.For better readability, the bool arguments were changed to flags.
Does this patch follow the "one change per patch" rule?
drivers/md/dm-ioctl.c struct vers_iter { · line 70
Quoted 4 lines
drivers/md/dm-ioctl.c +4
-0+#define KEEP_OPEN_DEVICES 1 +#define MARK_DEFERRED 2 +#define ONLY_DEFERRED 4 +#define INTERRUPTIBLE 8
Has it been considered to add a (short) prefix to these flag names?
drivers/md/dm-ioctl.c typedef int (*ioctl_fn)(struct file *fil · line 543
Quoted 1 line
drivers/md/dm-ioctl.c +1
-0+int r =dm_hash_remove_all (KEEP_OPEN_DEVICES | (param->flags &DM_DEFERRED_REMOVE ?MARK_DEFERRED :0 ) |INTERRUPTIBLE );
Code readability could be improved by writing the above code as follows:
int r, flags = KEEP_OPEN_DEVICES | INTERRUPTIBLE; if (param->flags & DM_DEFERRED_REMOVE) flags |= MARK_DEFERRED; r = dm_hash_remove_all(flags);
Otherwise this patch looks good to me.
Thanks,
Bart.
Quoted 6 lines · Bart Van Assche
drivers/md/dm-ioctl.c struct vers_iter { · line 70
Quoted 4 lines · Mikulas Patocka
drivers/md/dm-ioctl.c +4
-0+#define KEEP_OPEN_DEVICES 1 +#define MARK_DEFERRED 2 +#define ONLY_DEFERRED 4 +#define INTERRUPTIBLE 8Has it been considered to add a (short) prefix to these flag names?
And shouldn't this be an enum? Yes, the dm code currently flaps between both styles.
| Enums are preferred when defining several related constants.
https://www.kernel.org/doc/html/v6.19/process/coding-style.html#macros-enums-and-rtl
And maybe even "(1 << 0)" to make them easier visible as flags.
Bastian
Signature · 2 lines
I'm a soldier, not a diplomat. I can only tell the truth. -- Kirk, "Errand of Mercy", stardate 3198.9
Mikulas Patocka
maintainerPATCH v3 1/1 dm: make "dmsetup remove_all" interruptible
In reply to Bart Van AsscheThe command "dmsetup remove_all" may take a long time (a minute for removing 1000 devices), so make it interruptible with fatal signals.
For better readability, the bool arguments were changed to flags.
--- drivers/md/dm-ioctl.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-)
drivers/md/dm-ioctl.c +25 -10
@@ -64,7 +64,11 @@ struct vers_iter {static struct rb_root name_rb_tree =RB_ROOT ;static struct rb_root uuid_rb_tree =RB_ROOT ; -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred); +#define DM_REMOVE_KEEP_OPEN_DEVICES 1 +#define DM_REMOVE_MARK_DEFERRED 2 +#define DM_REMOVE_ONLY_DEFERRED 4 +#define DM_REMOVE_INTERRUPTIBLE 8 +static int dm_hash_remove_all (unsigned flags);/* * Guards access to both hash tables.
@@ -78,7 +82,7 @@ static DEFINE_MUTEX(dm_hash_cells_mutex)static void dm_hash_exit (void ) { -dm_hash_remove_all (false, false, false); +dm_hash_remove_all (0 ); }/*
@@ -333,7 +337,7 @@ static struct dm_table *__hash_remove(streturn table; } -static void dm_hash_remove_all (bool keep_open_devices,bool mark_deferred,bool only_deferred) +static int dm_hash_remove_all (unsigned flags) {int dev_skipped;struct rb_node *n;
@@ -347,12 +351,17 @@ retry:down_write (&_hash_lock);for (n =rb_first (&name_rb_tree); n; n =rb_next (n)) { +if (flags &DM_REMOVE_INTERRUPTIBLE &&fatal_signal_pending (current)) { +up_write (&_hash_lock); +return -EINTR ; + } + hc = container_of(n, struct hash_cell, name_node); md = hc->md;dm_get (md); -if (keep_open_devices && -dm_lock_for_deletion (md, mark_deferred, only_deferred)) { +if (flags &DM_REMOVE_KEEP_OPEN_DEVICES && +dm_lock_for_deletion (md, !!(flags &DM_REMOVE_MARK_DEFERRED ), !!(flags &DM_REMOVE_ONLY_DEFERRED ))) {dm_put (md); dev_skipped++;continue ;
@@ -368,7 +377,7 @@ retry: }dm_ima_measure_on_device_remove (md, true);dm_put (md); -if (likely (keep_open_devices)) +if (likely (flags &DM_REMOVE_KEEP_OPEN_DEVICES ))dm_destroy (md);else dm_destroy_immediate (md);
@@ -384,8 +393,10 @@ retry:up_write (&_hash_lock); -if (dev_skipped && !only_deferred) +if (dev_skipped && !(flags &DM_REMOVE_ONLY_DEFERRED ))DMWARN ("remove_all left %d open device(s)" , dev_skipped); + +return 0 ; }/*
@@ -513,7 +524,7 @@ static struct mapped_device *dm_hash_renvoid dm_deferred_remove (void ) { -dm_hash_remove_all (true, false, true); +dm_hash_remove_all (DM_REMOVE_KEEP_OPEN_DEVICES |DM_REMOVE_ONLY_DEFERRED ); }/*
@@ -529,9 +540,13 @@ typedef int (*ioctl_fn)(struct file *filstatic int remove_all (struct file *filp,struct dm_ioctl *param,size_t param_size) { -dm_hash_remove_all (true, !!(param->flags &DM_DEFERRED_REMOVE ), false); +int r; +int flags =DM_REMOVE_KEEP_OPEN_DEVICES |DM_REMOVE_INTERRUPTIBLE ; +if (param->flags &DM_DEFERRED_REMOVE ) + flags |=DM_REMOVE_MARK_DEFERRED ; + r =dm_hash_remove_all (flags); param->data_size =0 ; -return 0 ; +return r; }/*
Quoted 4 lines · Mikulas Patocka
The command "dmsetup
remove_all" may take a long time (a minute for removing 1000 devices), so make it interruptible with fatal signals.For better readability, the bool arguments were changed to flags.