xref: /openbmc/linux/drivers/md/dm-builtin.c (revision aa07f9d8)
1*3bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
24cc96131SMike Snitzer #include "dm-core.h"
32995fa78SMikulas Patocka 
42995fa78SMikulas Patocka /*
52995fa78SMikulas Patocka  * The kobject release method must not be placed in the module itself,
62995fa78SMikulas Patocka  * otherwise we are subject to module unload races.
72995fa78SMikulas Patocka  *
82995fa78SMikulas Patocka  * The release method is called when the last reference to the kobject is
92995fa78SMikulas Patocka  * dropped. It may be called by any other kernel code that drops the last
102995fa78SMikulas Patocka  * reference.
112995fa78SMikulas Patocka  *
122995fa78SMikulas Patocka  * The release method suffers from module unload race. We may prevent the
132995fa78SMikulas Patocka  * module from being unloaded at the start of the release method (using
142995fa78SMikulas Patocka  * increased module reference count or synchronizing against the release
152995fa78SMikulas Patocka  * method), however there is no way to prevent the module from being
162995fa78SMikulas Patocka  * unloaded at the end of the release method.
172995fa78SMikulas Patocka  *
182995fa78SMikulas Patocka  * If this code were placed in the dm module, the following race may
192995fa78SMikulas Patocka  * happen:
202995fa78SMikulas Patocka  *  1. Some other process takes a reference to dm kobject
212995fa78SMikulas Patocka  *  2. The user issues ioctl function to unload the dm device
222995fa78SMikulas Patocka  *  3. dm_sysfs_exit calls kobject_put, however the object is not released
232995fa78SMikulas Patocka  *     because of the other reference taken at step 1
242995fa78SMikulas Patocka  *  4. dm_sysfs_exit waits on the completion
252995fa78SMikulas Patocka  *  5. The other process that took the reference in step 1 drops it,
262995fa78SMikulas Patocka  *     dm_kobject_release is called from this process
272995fa78SMikulas Patocka  *  6. dm_kobject_release calls complete()
282995fa78SMikulas Patocka  *  7. a reschedule happens before dm_kobject_release returns
292995fa78SMikulas Patocka  *  8. dm_sysfs_exit continues, the dm device is unloaded, module reference
302995fa78SMikulas Patocka  *     count is decremented
312995fa78SMikulas Patocka  *  9. The user unloads the dm module
322995fa78SMikulas Patocka  * 10. The other process that was rescheduled in step 7 continues to run,
332995fa78SMikulas Patocka  *     it is now executing code in unloaded module, so it crashes
342995fa78SMikulas Patocka  *
352995fa78SMikulas Patocka  * Note that if the process that takes the foreign reference to dm kobject
362995fa78SMikulas Patocka  * has a low priority and the system is sufficiently loaded with
372995fa78SMikulas Patocka  * higher-priority processes that prevent the low-priority process from
382995fa78SMikulas Patocka  * being scheduled long enough, this bug may really happen.
392995fa78SMikulas Patocka  *
402995fa78SMikulas Patocka  * In order to fix this module unload race, we place the release method
412995fa78SMikulas Patocka  * into a helper code that is compiled directly into the kernel.
422995fa78SMikulas Patocka  */
432995fa78SMikulas Patocka 
dm_kobject_release(struct kobject * kobj)442995fa78SMikulas Patocka void dm_kobject_release(struct kobject *kobj)
452995fa78SMikulas Patocka {
462995fa78SMikulas Patocka 	complete(dm_get_completion_from_kobject(kobj));
472995fa78SMikulas Patocka }
482995fa78SMikulas Patocka EXPORT_SYMBOL(dm_kobject_release);
49