persist.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. USB device persistence during system suspend
  2. Alan Stern <stern@rowland.harvard.edu>
  3. September 2, 2006 (Updated May 29, 2007)
  4. What is the problem?
  5. According to the USB specification, when a USB bus is suspended the
  6. bus must continue to supply suspend current (around 1-5 mA). This
  7. is so that devices can maintain their internal state and hubs can
  8. detect connect-change events (devices being plugged in or unplugged).
  9. The technical term is "power session".
  10. If a USB device's power session is interrupted then the system is
  11. required to behave as though the device has been unplugged. It's a
  12. conservative approach; in the absence of suspend current the computer
  13. has no way to know what has actually happened. Perhaps the same
  14. device is still attached or perhaps it was removed and a different
  15. device plugged into the port. The system must assume the worst.
  16. By default, Linux behaves according to the spec. If a USB host
  17. controller loses power during a system suspend, then when the system
  18. wakes up all the devices attached to that controller are treated as
  19. though they had disconnected. This is always safe and it is the
  20. "officially correct" thing to do.
  21. For many sorts of devices this behavior doesn't matter in the least.
  22. If the kernel wants to believe that your USB keyboard was unplugged
  23. while the system was asleep and a new keyboard was plugged in when the
  24. system woke up, who cares? It'll still work the same when you type on
  25. it.
  26. Unfortunately problems _can_ arise, particularly with mass-storage
  27. devices. The effect is exactly the same as if the device really had
  28. been unplugged while the system was suspended. If you had a mounted
  29. filesystem on the device, you're out of luck -- everything in that
  30. filesystem is now inaccessible. This is especially annoying if your
  31. root filesystem was located on the device, since your system will
  32. instantly crash.
  33. Loss of power isn't the only mechanism to worry about. Anything that
  34. interrupts a power session will have the same effect. For example,
  35. even though suspend current may have been maintained while the system
  36. was asleep, on many systems during the initial stages of wakeup the
  37. firmware (i.e., the BIOS) resets the motherboard's USB host
  38. controllers. Result: all the power sessions are destroyed and again
  39. it's as though you had unplugged all the USB devices. Yes, it's
  40. entirely the BIOS's fault, but that doesn't do _you_ any good unless
  41. you can convince the BIOS supplier to fix the problem (lots of luck!).
  42. On many systems the USB host controllers will get reset after a
  43. suspend-to-RAM. On almost all systems, no suspend current is
  44. available during hibernation (also known as swsusp or suspend-to-disk).
  45. You can check the kernel log after resuming to see if either of these
  46. has happened; look for lines saying "root hub lost power or was reset".
  47. In practice, people are forced to unmount any filesystems on a USB
  48. device before suspending. If the root filesystem is on a USB device,
  49. the system can't be suspended at all. (All right, it _can_ be
  50. suspended -- but it will crash as soon as it wakes up, which isn't
  51. much better.)
  52. What is the solution?
  53. Setting CONFIG_USB_PERSIST will cause the kernel to work around these
  54. issues. It enables a mode in which the core USB device data
  55. structures are allowed to persist across a power-session disruption.
  56. It works like this. If the kernel sees that a USB host controller is
  57. not in the expected state during resume (i.e., if the controller was
  58. reset or otherwise had lost power) then it applies a persistence check
  59. to each of the USB devices below that controller for which the
  60. "persist" attribute is set. It doesn't try to resume the device; that
  61. can't work once the power session is gone. Instead it issues a USB
  62. port reset and then re-enumerates the device. (This is exactly the
  63. same thing that happens whenever a USB device is reset.) If the
  64. re-enumeration shows that the device now attached to that port has the
  65. same descriptors as before, including the Vendor and Product IDs, then
  66. the kernel continues to use the same device structure. In effect, the
  67. kernel treats the device as though it had merely been reset instead of
  68. unplugged.
  69. If no device is now attached to the port, or if the descriptors are
  70. different from what the kernel remembers, then the treatment is what
  71. you would expect. The kernel destroys the old device structure and
  72. behaves as though the old device had been unplugged and a new device
  73. plugged in, just as it would without the CONFIG_USB_PERSIST option.
  74. The end result is that the USB device remains available and usable.
  75. Filesystem mounts and memory mappings are unaffected, and the world is
  76. now a good and happy place.
  77. Note that even when CONFIG_USB_PERSIST is set, the "persist" feature
  78. will be applied only to those devices for which it is enabled. You
  79. can enable the feature by doing (as root):
  80. echo 1 >/sys/bus/usb/devices/.../power/persist
  81. where the "..." should be filled in the with the device's ID. Disable
  82. the feature by writing 0 instead of 1. For hubs the feature is
  83. automatically and permanently enabled, so you only have to worry about
  84. setting it for devices where it really matters.
  85. Is this the best solution?
  86. Perhaps not. Arguably, keeping track of mounted filesystems and
  87. memory mappings across device disconnects should be handled by a
  88. centralized Logical Volume Manager. Such a solution would allow you
  89. to plug in a USB flash device, create a persistent volume associated
  90. with it, unplug the flash device, plug it back in later, and still
  91. have the same persistent volume associated with the device. As such
  92. it would be more far-reaching than CONFIG_USB_PERSIST.
  93. On the other hand, writing a persistent volume manager would be a big
  94. job and using it would require significant input from the user. This
  95. solution is much quicker and easier -- and it exists now, a giant
  96. point in its favor!
  97. Furthermore, the USB_PERSIST option applies to _all_ USB devices, not
  98. just mass-storage devices. It might turn out to be equally useful for
  99. other device types, such as network interfaces.
  100. WARNING: Using CONFIG_USB_PERSIST can be dangerous!!
  101. When recovering an interrupted power session the kernel does its best
  102. to make sure the USB device hasn't been changed; that is, the same
  103. device is still plugged into the port as before. But the checks
  104. aren't guaranteed to be 100% accurate.
  105. If you replace one USB device with another of the same type (same
  106. manufacturer, same IDs, and so on) there's an excellent chance the
  107. kernel won't detect the change. Serial numbers and other strings are
  108. not compared. In many cases it wouldn't help if they were, because
  109. manufacturers frequently omit serial numbers entirely in their
  110. devices.
  111. Furthermore it's quite possible to leave a USB device exactly the same
  112. while changing its media. If you replace the flash memory card in a
  113. USB card reader while the system is asleep, the kernel will have no
  114. way to know you did it. The kernel will assume that nothing has
  115. happened and will continue to use the partition tables, inodes, and
  116. memory mappings for the old card.
  117. If the kernel gets fooled in this way, it's almost certain to cause
  118. data corruption and to crash your system. You'll have no one to blame
  119. but yourself.
  120. YOU HAVE BEEN WARNED! USE AT YOUR OWN RISK!
  121. That having been said, most of the time there shouldn't be any trouble
  122. at all. The "persist" feature can be extremely useful. Make the most
  123. of it.