dm-builtin.c 2.0 KB

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