devres.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. Devres - Managed Device Resource
  2. ================================
  3. Tejun Heo <teheo@suse.de>
  4. First draft 10 January 2007
  5. 1. Intro : Huh? Devres?
  6. 2. Devres : Devres in a nutshell
  7. 3. Devres Group : Group devres'es and release them together
  8. 4. Details : Life time rules, calling context, ...
  9. 5. Overhead : How much do we have to pay for this?
  10. 6. List of managed interfaces : Currently implemented managed interfaces
  11. 1. Intro
  12. --------
  13. devres came up while trying to convert libata to use iomap. Each
  14. iomapped address should be kept and unmapped on driver detach. For
  15. example, a plain SFF ATA controller (that is, good old PCI IDE) in
  16. native mode makes use of 5 PCI BARs and all of them should be
  17. maintained.
  18. As with many other device drivers, libata low level drivers have
  19. sufficient bugs in ->remove and ->probe failure path. Well, yes,
  20. that's probably because libata low level driver developers are lazy
  21. bunch, but aren't all low level driver developers? After spending a
  22. day fiddling with braindamaged hardware with no document or
  23. braindamaged document, if it's finally working, well, it's working.
  24. For one reason or another, low level drivers don't receive as much
  25. attention or testing as core code, and bugs on driver detach or
  26. initilaization failure doesn't happen often enough to be noticeable.
  27. Init failure path is worse because it's much less travelled while
  28. needs to handle multiple entry points.
  29. So, many low level drivers end up leaking resources on driver detach
  30. and having half broken failure path implementation in ->probe() which
  31. would leak resources or even cause oops when failure occurs. iomap
  32. adds more to this mix. So do msi and msix.
  33. 2. Devres
  34. ---------
  35. devres is basically linked list of arbitrarily sized memory areas
  36. associated with a struct device. Each devres entry is associated with
  37. a release function. A devres can be released in several ways. No
  38. matter what, all devres entries are released on driver detach. On
  39. release, the associated release function is invoked and then the
  40. devres entry is freed.
  41. Managed interface is created for resources commonly used by device
  42. drivers using devres. For example, coherent DMA memory is acquired
  43. using dma_alloc_coherent(). The managed version is called
  44. dmam_alloc_coherent(). It is identical to dma_alloc_coherent() except
  45. for the DMA memory allocated using it is managed and will be
  46. automatically released on driver detach. Implementation looks like
  47. the following.
  48. struct dma_devres {
  49. size_t size;
  50. void *vaddr;
  51. dma_addr_t dma_handle;
  52. };
  53. static void dmam_coherent_release(struct device *dev, void *res)
  54. {
  55. struct dma_devres *this = res;
  56. dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  57. }
  58. dmam_alloc_coherent(dev, size, dma_handle, gfp)
  59. {
  60. struct dma_devres *dr;
  61. void *vaddr;
  62. dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
  63. ...
  64. /* alloc DMA memory as usual */
  65. vaddr = dma_alloc_coherent(...);
  66. ...
  67. /* record size, vaddr, dma_handle in dr */
  68. dr->vaddr = vaddr;
  69. ...
  70. devres_add(dev, dr);
  71. return vaddr;
  72. }
  73. If a driver uses dmam_alloc_coherent(), the area is guaranteed to be
  74. freed whether initialization fails half-way or the device gets
  75. detached. If most resources are acquired using managed interface, a
  76. driver can have much simpler init and exit code. Init path basically
  77. looks like the following.
  78. my_init_one()
  79. {
  80. struct mydev *d;
  81. d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
  82. if (!d)
  83. return -ENOMEM;
  84. d->ring = dmam_alloc_coherent(...);
  85. if (!d->ring)
  86. return -ENOMEM;
  87. if (check something)
  88. return -EINVAL;
  89. ...
  90. return register_to_upper_layer(d);
  91. }
  92. And exit path,
  93. my_remove_one()
  94. {
  95. unregister_from_upper_layer(d);
  96. shutdown_my_hardware();
  97. }
  98. As shown above, low level drivers can be simplified a lot by using
  99. devres. Complexity is shifted from less maintained low level drivers
  100. to better maintained higher layer. Also, as init failure path is
  101. shared with exit path, both can get more testing.
  102. 3. Devres group
  103. ---------------
  104. Devres entries can be grouped using devres group. When a group is
  105. released, all contained normal devres entries and properly nested
  106. groups are released. One usage is to rollback series of acquired
  107. resources on failure. For example,
  108. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  109. return -ENOMEM;
  110. acquire A;
  111. if (failed)
  112. goto err;
  113. acquire B;
  114. if (failed)
  115. goto err;
  116. ...
  117. devres_remove_group(dev, NULL);
  118. return 0;
  119. err:
  120. devres_release_group(dev, NULL);
  121. return err_code;
  122. As resource acquision failure usually means probe failure, constructs
  123. like above are usually useful in midlayer driver (e.g. libata core
  124. layer) where interface function shouldn't have side effect on failure.
  125. For LLDs, just returning error code suffices in most cases.
  126. Each group is identified by void *id. It can either be explicitly
  127. specified by @id argument to devres_open_group() or automatically
  128. created by passing NULL as @id as in the above example. In both
  129. cases, devres_open_group() returns the group's id. The returned id
  130. can be passed to other devres functions to select the target group.
  131. If NULL is given to those functions, the latest open group is
  132. selected.
  133. For example, you can do something like the following.
  134. int my_midlayer_create_something()
  135. {
  136. if (!devres_open_group(dev, my_midlayer_create_something, GFP_KERNEL))
  137. return -ENOMEM;
  138. ...
  139. devres_close_group(dev, my_midlayer_something);
  140. return 0;
  141. }
  142. void my_midlayer_destroy_something()
  143. {
  144. devres_release_group(dev, my_midlayer_create_soemthing);
  145. }
  146. 4. Details
  147. ----------
  148. Lifetime of a devres entry begins on devres allocation and finishes
  149. when it is released or destroyed (removed and freed) - no reference
  150. counting.
  151. devres core guarantees atomicity to all basic devres operations and
  152. has support for single-instance devres types (atomic
  153. lookup-and-add-if-not-found). Other than that, synchronizing
  154. concurrent accesses to allocated devres data is caller's
  155. responsibility. This is usually non-issue because bus ops and
  156. resource allocations already do the job.
  157. For an example of single-instance devres type, read pcim_iomap_table()
  158. in lib/iomap.c.
  159. All devres interface functions can be called without context if the
  160. right gfp mask is given.
  161. 5. Overhead
  162. -----------
  163. Each devres bookkeeping info is allocated together with requested data
  164. area. With debug option turned off, bookkeeping info occupies 16
  165. bytes on 32bit machines and 24 bytes on 64bit (three pointers rounded
  166. up to ull alignment). If singly linked list is used, it can be
  167. reduced to two pointers (8 bytes on 32bit, 16 bytes on 64bit).
  168. Each devres group occupies 8 pointers. It can be reduced to 6 if
  169. singly linked list is used.
  170. Memory space overhead on ahci controller with two ports is between 300
  171. and 400 bytes on 32bit machine after naive conversion (we can
  172. certainly invest a bit more effort into libata core layer).
  173. 6. List of managed interfaces
  174. -----------------------------
  175. IO region
  176. devm_request_region()
  177. devm_request_mem_region()
  178. devm_release_region()
  179. devm_release_mem_region()
  180. IRQ
  181. devm_request_irq()
  182. devm_free_irq()
  183. DMA
  184. dmam_alloc_coherent()
  185. dmam_free_coherent()
  186. dmam_alloc_noncoherent()
  187. dmam_free_noncoherent()
  188. dmam_declare_coherent_memory()
  189. dmam_pool_create()
  190. dmam_pool_destroy()
  191. PCI
  192. pcim_enable_device() : after success, all PCI ops become managed
  193. pcim_pin_device() : keep PCI device enabled after release
  194. IOMAP
  195. devm_ioport_map()
  196. devm_ioport_unmap()
  197. devm_ioremap()
  198. devm_ioremap_nocache()
  199. devm_iounmap()
  200. pcim_iomap()
  201. pcim_iounmap()
  202. pcim_iomap_table() : array of mapped addresses indexed by BAR
  203. pcim_iomap_regions() : do request_region() and iomap() on multiple BARs