1169695Skan// SPDX-License-Identifier: GPL-2.0-only 2169695Skan#include "dm-core.h" 3169695Skan 4169695Skan/* 5169695Skan * The kobject release method must not be placed in the module itself, 6169695Skan * otherwise we are subject to module unload races. 7169695Skan * 8169695Skan * The release method is called when the last reference to the kobject is 9169695Skan * dropped. It may be called by any other kernel code that drops the last 10169695Skan * reference. 11169695Skan * 12169695Skan * The release method suffers from module unload race. We may prevent the 13169695Skan * module from being unloaded at the start of the release method (using 14169695Skan * increased module reference count or synchronizing against the release 15169695Skan * method), however there is no way to prevent the module from being 16169695Skan * unloaded at the end of the release method. 17169695Skan * 18169695Skan * If this code were placed in the dm module, the following race may 19169695Skan * happen: 20169695Skan * 1. Some other process takes a reference to dm kobject 21169695Skan * 2. The user issues ioctl function to unload the dm device 22169695Skan * 3. dm_sysfs_exit calls kobject_put, however the object is not released 23169695Skan * because of the other reference taken at step 1 24169695Skan * 4. dm_sysfs_exit waits on the completion 25169695Skan * 5. The other process that took the reference in step 1 drops it, 26169695Skan * dm_kobject_release is called from this process 27169695Skan * 6. dm_kobject_release calls complete() 28169695Skan * 7. a reschedule happens before dm_kobject_release returns 29169695Skan * 8. dm_sysfs_exit continues, the dm device is unloaded, module reference 30169695Skan * count is decremented 31169695Skan * 9. The user unloads the dm module 32169695Skan * 10. The other process that was rescheduled in step 7 continues to run, 33169695Skan * it is now executing code in unloaded module, so it crashes 34169695Skan * 35169695Skan * Note that if the process that takes the foreign reference to dm kobject 36169695Skan * has a low priority and the system is sufficiently loaded with 37169695Skan * higher-priority processes that prevent the low-priority process from 38169695Skan * being scheduled long enough, this bug may really happen. 39169695Skan * 40169695Skan * In order to fix this module unload race, we place the release method 41169695Skan * into a helper code that is compiled directly into the kernel. 42169695Skan */ 43169695Skan 44169695Skanvoid dm_kobject_release(struct kobject *kobj) 45169695Skan{ 46169695Skan complete(dm_get_completion_from_kobject(kobj)); 47169695Skan} 48169695SkanEXPORT_SYMBOL(dm_kobject_release); 49169695Skan