49 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2#include "dm-core.h"3 4/*5 * The kobject release method must not be placed in the module itself,6 * otherwise we are subject to module unload races.7 *8 * The release method is called when the last reference to the kobject is9 * dropped. It may be called by any other kernel code that drops the last10 * reference.11 *12 * The release method suffers from module unload race. We may prevent the13 * module from being unloaded at the start of the release method (using14 * increased module reference count or synchronizing against the release15 * method), however there is no way to prevent the module from being16 * unloaded at the end of the release method.17 *18 * If this code were placed in the dm module, the following race may19 * happen:20 * 1. Some other process takes a reference to dm kobject21 * 2. The user issues ioctl function to unload the dm device22 * 3. dm_sysfs_exit calls kobject_put, however the object is not released23 * because of the other reference taken at step 124 * 4. dm_sysfs_exit waits on the completion25 * 5. The other process that took the reference in step 1 drops it,26 * dm_kobject_release is called from this process27 * 6. dm_kobject_release calls complete()28 * 7. a reschedule happens before dm_kobject_release returns29 * 8. dm_sysfs_exit continues, the dm device is unloaded, module reference30 * count is decremented31 * 9. The user unloads the dm module32 * 10. The other process that was rescheduled in step 7 continues to run,33 * it is now executing code in unloaded module, so it crashes34 *35 * Note that if the process that takes the foreign reference to dm kobject36 * has a low priority and the system is sufficiently loaded with37 * higher-priority processes that prevent the low-priority process from38 * being scheduled long enough, this bug may really happen.39 *40 * In order to fix this module unload race, we place the release method41 * into a helper code that is compiled directly into the kernel.42 */43 44void dm_kobject_release(struct kobject *kobj)45{46 complete(dm_get_completion_from_kobject(kobj));47}48EXPORT_SYMBOL(dm_kobject_release);49