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