xref: /linux/drivers/md/dm-builtin.c (revision a2cce7a9f1b8cc3d4edce106fb971529f1d4d9ce)
1 #include "dm.h"
2 
3 /*
4  * The kobject release method must not be placed in the module itself,
5  * otherwise we are subject to module unload races.
6  *
7  * The release method is called when the last reference to the kobject is
8  * dropped. It may be called by any other kernel code that drops the last
9  * reference.
10  *
11  * The release method suffers from module unload race. We may prevent the
12  * module from being unloaded at the start of the release method (using
13  * increased module reference count or synchronizing against the release
14  * method), however there is no way to prevent the module from being
15  * unloaded at the end of the release method.
16  *
17  * If this code were placed in the dm module, the following race may
18  * happen:
19  *  1. Some other process takes a reference to dm kobject
20  *  2. The user issues ioctl function to unload the dm device
21  *  3. dm_sysfs_exit calls kobject_put, however the object is not released
22  *     because of the other reference taken at step 1
23  *  4. dm_sysfs_exit waits on the completion
24  *  5. The other process that took the reference in step 1 drops it,
25  *     dm_kobject_release is called from this process
26  *  6. dm_kobject_release calls complete()
27  *  7. a reschedule happens before dm_kobject_release returns
28  *  8. dm_sysfs_exit continues, the dm device is unloaded, module reference
29  *     count is decremented
30  *  9. The user unloads the dm module
31  * 10. The other process that was rescheduled in step 7 continues to run,
32  *     it is now executing code in unloaded module, so it crashes
33  *
34  * Note that if the process that takes the foreign reference to dm kobject
35  * has a low priority and the system is sufficiently loaded with
36  * higher-priority processes that prevent the low-priority process from
37  * being scheduled long enough, this bug may really happen.
38  *
39  * In order to fix this module unload race, we place the release method
40  * into a helper code that is compiled directly into the kernel.
41  */
42 
43 void dm_kobject_release(struct kobject *kobj)
44 {
45 	complete(dm_get_completion_from_kobject(kobj));
46 }
47 
48 EXPORT_SYMBOL(dm_kobject_release);
49