dm-table-rework-reference-counting.patch
Hi Alasdair, Mikulas,
Although I'm not sure this is correct, don't we need to convert the following 2 dm_table_put()s to dm_table_destroy()? Otherwise, the created table won't be destroyed and memory leak will happen in such error cases?
drivers/md/dm-ioctl.c:table_load()
1060 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1061 if (r)
1062 goto out;
1063
1064 r = populate_table(t, param, param_size);
1065 if (r) {
1066 dm_table_put(t);
1067 goto out;
1068 }
1069
1070 down_write(&_hash_lock);
1071 hc = dm_get_mdptr(md);
1072 if (!hc || hc->md != md) {
1073 DMWARN("device has been removed from the dev hash table.");
1074 dm_table_put(t);
1075 up_write(&_hash_lock);
1076 r = -ENXIO;
1077 goto out;1078 }
Thanks,
Kiyoshi Ueda
Mikulas Patocka
maintainerHi
You are right, thanks for the review. I grepped for other uses of dm_table_put and they seem correct - just these two were bad.
Alasdair: push this patch to Linus soon.
Another point: it looks somehow scary to free the table before unlocking _hash_lock --- most of the time, _hash_lock is took only on behalf of userspace ioctls (no problem with that), but it is also took in dm_copy_name_and_uuid that can be called from uevent code:
dm_table_event -> event_callback -> dm_send_uevents -> takes _hash_lock And dm_table_event is called directly from targets, targets wait for dm_table_event to finish in their destructor --- so calling the destructor with locked _hash_lock seems to be a deadlock possibility --- what do you think?
Mikulas
Quoted 26 lines · Kiyoshi Ueda
Hi Alasdair, Mikulas,
Although I'm not sure this is correct, don't we need to convert the following 2 dm_table_put()s to
dm_table_destroy()? Otherwise, the created table won't be destroyed and memory leak will happen in such error cases?drivers/md/dm-ioctl.c:table_load()
1060 r =dm_table_create(&t, get_mode(param), param->target_count, md);
1061 if (r)
1062 goto out;
1063
1064 r =populate_table(t, param, param_size);
1065 if (r) {
1066dm_table_put(t);
1067 goto out;
1068 }
10691070 down_write(&_hash_lock); 1071 hc = dm_get_mdptr(md); 1072 if (!hc || hc->md != md) { 1073 DMWARN("device has been removed from the dev hash table."); 1074 dm_table_put(t); 1075 up_write(&_hash_lock); 1076 r = -ENXIO; 1077 goto out;
Quoted 5 lines
1078 }
Thanks,
Kiyoshi Ueda
Fix an error introduced in dm-table-rework-reference-counting.patch.
When there is failure after table initialization, we need to use dm_table_destroy, not dm_table_put, to free the table.
dm_table_put may be used only after dm_table_get.
--- drivers/md/dm-ioctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/dm-ioctl.c +2 -2
@@ -1063,7 +1063,7 @@ static int table_load(struct dm_ioctl *p r =populate_table (t, param, param_size);if (r) { -dm_table_put (t); +dm_table_destroy (t); goto out; }
@@ -1071,7 +1071,7 @@ 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); +dm_table_destroy (t);up_write (&_hash_lock); r = -ENXIO ; goto out;
self deadlock possibility during device deletion (Was: dm-table-rework-reference-counting.patch)
Hi Mikulas, Alasdair,
Quoted 6 lines · Mikulas Patocka
Quoted 6 lines
Hi
You are right, thanks for the review. I grepped for other uses of
dm_table_putand they seem correct - just these two were bad.Alasdair: push this patch to Linus soon.
Thank you for the rapid work.
Quoted 10 lines
Quoted 10 lines
Another point: it looks somehow scary to free the table before unlocking
_hash_lock--- most of the time,_hash_lockis took only on behalf of userspace ioctls (no problem with that), but it is also took indm_copy_name_and_uuidthat can be called from uevent code:
dm_table_event->event_callback->dm_send_uevents-> takes_hash_lockAnddm_table_eventis called directly from targets, targets wait fordm_table_eventto finish in their destructor --- so calling the destructor with locked_hash_lockseems to be a deadlock possibility --- what do you think?
You are right, although there is no such target now. But there is already a code path that could cause such a deadlock.
If a mapped_device is being deleted while there are pending uevents in md->uevent_list, the ioctl for the deletion should cause self-deadlock, because __hash_remove() calls dm_table_event() with _hash_lock locked:
drivers/md/dm-ioctl.c:__hash_remove()
220 static void __hash_remove(struct hash_cell *hc) 221 {
222 struct dm_table *table;
223
224 /* remove from the dev hash */ 225 list_del(&hc->uuid_list); 226 list_del(&hc->name_list); 227 dm_set_mdptr(hc->md, NULL);
228
229 table = dm_get_table(hc->md);
230 if (table) {
231 dm_table_event(table);
232 dm_table_put(table);
233 }
We should unlock _hash_lock before handling event flush.
Quoted 1 line
Quoted 1 line
Mikulas
Thanks,
Kiyoshi Ueda
Quoted 75 lines
Quoted 75 lines · Kiyoshi Ueda
Quoted 29 lines
Quoted 29 lines
Hi Alasdair, Mikulas,
Although I'm not sure this is correct, don't we need to convert the following 2 dm_table_put()s to
dm_table_destroy()? Otherwise, the created table won't be destroyed and memory leak will happen in such error cases?drivers/md/dm-ioctl.c:table_load()
1060 r =dm_table_create(&t, get_mode(param), param->target_count, md);
1061 if (r)
1062 goto out;
1063
1064 r =populate_table(t, param, param_size);
1065 if (r) {
1066dm_table_put(t);
1067 goto out;
1068 }
10691070 down_write(&_hash_lock); 1071 hc = dm_get_mdptr(md); 1072 if (!hc || hc->md != md) { 1073 DMWARN("device has been removed from the dev hash table."); 1074 dm_table_put(t); 1075 up_write(&_hash_lock); 1076 r = -ENXIO; 1077 goto out;1078 }
Quoted 3 lines
Quoted 3 lines
Thanks,
Kiyoshi UedaFix an error introduced in dm-table-rework-reference-counting.patch.
When there is failure after table initialization, we need to use
dm_table_destroy, notdm_table_put, to free the table.
dm_table_putmay be used only afterdm_table_get.Signed-off-by Mikulas Patocka <[email protected]>CC Kiyoshi Ueda <[email protected]>--- drivers/md/dm-ioctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Index:
linux-2.6.28-devel/drivers/md/dm-ioctl.c
===================================================================drivers/md/dm-ioctl.c +2
-3@@ -1063,7 +1063,7 @@ static int table_load(struct dm_ioctl *p r =populate_table (t, param, param_size);if (r) { -dm_table_put (t); +dm_table_destroy (t); goto out; }@@ -1071,7 +1071,7 @@ 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); +dm_table_destroy (t);up_write (&_hash_lock); r = -ENXIO ; goto out; --dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel