Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "dm-core.h"
0003 
0004 /*
0005  * The kobject release method must not be placed in the module itself,
0006  * otherwise we are subject to module unload races.
0007  *
0008  * The release method is called when the last reference to the kobject is
0009  * dropped. It may be called by any other kernel code that drops the last
0010  * reference.
0011  *
0012  * The release method suffers from module unload race. We may prevent the
0013  * module from being unloaded at the start of the release method (using
0014  * increased module reference count or synchronizing against the release
0015  * method), however there is no way to prevent the module from being
0016  * unloaded at the end of the release method.
0017  *
0018  * If this code were placed in the dm module, the following race may
0019  * happen:
0020  *  1. Some other process takes a reference to dm kobject
0021  *  2. The user issues ioctl function to unload the dm device
0022  *  3. dm_sysfs_exit calls kobject_put, however the object is not released
0023  *     because of the other reference taken at step 1
0024  *  4. dm_sysfs_exit waits on the completion
0025  *  5. The other process that took the reference in step 1 drops it,
0026  *     dm_kobject_release is called from this process
0027  *  6. dm_kobject_release calls complete()
0028  *  7. a reschedule happens before dm_kobject_release returns
0029  *  8. dm_sysfs_exit continues, the dm device is unloaded, module reference
0030  *     count is decremented
0031  *  9. The user unloads the dm module
0032  * 10. The other process that was rescheduled in step 7 continues to run,
0033  *     it is now executing code in unloaded module, so it crashes
0034  *
0035  * Note that if the process that takes the foreign reference to dm kobject
0036  * has a low priority and the system is sufficiently loaded with
0037  * higher-priority processes that prevent the low-priority process from
0038  * being scheduled long enough, this bug may really happen.
0039  *
0040  * In order to fix this module unload race, we place the release method
0041  * into a helper code that is compiled directly into the kernel.
0042  */
0043 
0044 void dm_kobject_release(struct kobject *kobj)
0045 {
0046     complete(dm_get_completion_from_kobject(kobj));
0047 }
0048 
0049 EXPORT_SYMBOL(dm_kobject_release);