Skip to content

Commit be35f48

Browse files
Mikulas Patockasnitm
authored andcommitted
dm: wait until embedded kobject is released before destroying a device
There may be other parts of the kernel holding a reference on the dm kobject. We must wait until all references are dropped before deallocating the mapped_device structure. The dm_kobject_release method signals that all references are dropped via completion. But dm_kobject_release doesn't free the kobject (which is embedded in the mapped_device structure). This is the sequence of operations: * when destroying a DM device, call kobject_put from dm_sysfs_exit * wait until all users stop using the kobject, when it happens the release method is called * the release method signals the completion and should return without delay * the dm device removal code that waits on the completion continues * the dm device removal code drops the dm_mod reference the device had * the dm device removal code frees the mapped_device structure that contains the kobject Using kobject this way should avoid the module unload race that was mentioned at the beginning of this thread: https://lkml.org/lkml/2014/1/4/83 Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com> Cc: stable@vger.kernel.org
1 parent 1ddd641 commit be35f48

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎drivers/md/dm-sysfs.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ static const struct sysfs_ops dm_sysfs_ops = {
7979
.show = dm_attr_show,
8080
};
8181

82+
static void dm_kobject_release(struct kobject *kobj)
83+
{
84+
complete(dm_get_completion_from_kobject(kobj));
85+
}
86+
8287
/*
8388
* dm kobject is embedded in mapped_device structure
8489
* no need to define release function here
8590
*/
8691
static struct kobj_type dm_ktype = {
8792
.sysfs_ops = &dm_sysfs_ops,
8893
.default_attrs = dm_attrs,
94+
.release = dm_kobject_release,
8995
};
9096

9197
/*
@@ -104,5 +110,7 @@ int dm_sysfs_init(struct mapped_device *md)
104110
*/
105111
void dm_sysfs_exit(struct mapped_device *md)
106112
{
107-
kobject_put(dm_kobject(md));
113+
struct kobject *kobj = dm_kobject(md);
114+
kobject_put(kobj);
115+
wait_for_completion(dm_get_completion_from_kobject(kobj));
108116
}

‎drivers/md/dm.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ struct mapped_device {
203203
/* sysfs handle */
204204
struct kobject kobj;
205205

206+
/* wait until the kobject is released */
207+
struct completion kobj_completion;
208+
206209
/* zero-length flush that will be cloned and submitted to targets */
207210
struct bio flush_bio;
208211

@@ -2041,6 +2044,7 @@ static struct mapped_device *alloc_dev(int minor)
20412044
init_waitqueue_head(&md->wait);
20422045
INIT_WORK(&md->work, dm_wq_work);
20432046
init_waitqueue_head(&md->eventq);
2047+
init_completion(&md->kobj_completion);
20442048

20452049
md->disk->major = _major;
20462050
md->disk->first_minor = minor;
@@ -2919,6 +2923,13 @@ struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
29192923
return md;
29202924
}
29212925

2926+
struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
2927+
{
2928+
struct mapped_device *md = container_of(kobj, struct mapped_device, kobj);
2929+
2930+
return &md->kobj_completion;
2931+
}
2932+
29222933
int dm_suspended_md(struct mapped_device *md)
29232934
{
29242935
return test_bit(DMF_SUSPENDED, &md->flags);

‎drivers/md/dm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/list.h>
1616
#include <linux/blkdev.h>
1717
#include <linux/hdreg.h>
18+
#include <linux/completion.h>
1819

1920
#include "dm-stats.h"
2021

@@ -152,6 +153,7 @@ int dm_sysfs_init(struct mapped_device *md);
152153
void dm_sysfs_exit(struct mapped_device *md);
153154
struct kobject *dm_kobject(struct mapped_device *md);
154155
struct mapped_device *dm_get_from_kobject(struct kobject *kobj);
156+
struct completion *dm_get_completion_from_kobject(struct kobject *kobj);
155157

156158
/*
157159
* Targets for linear and striped mappings

0 commit comments

Comments
 (0)