devres.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. initialization failure don'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 acquisition 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_create_something);
  140. return 0;
  141. }
  142. void my_midlayer_destroy_something()
  143. {
  144. devres_release_group(dev, my_midlayer_create_something);
  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/devres.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. CLOCK
  176. devm_clk_get()
  177. devm_clk_put()
  178. devm_clk_hw_register()
  179. DMA
  180. dmam_alloc_coherent()
  181. dmam_alloc_noncoherent()
  182. dmam_declare_coherent_memory()
  183. dmam_free_coherent()
  184. dmam_free_noncoherent()
  185. dmam_pool_create()
  186. dmam_pool_destroy()
  187. GPIO
  188. devm_gpiod_get()
  189. devm_gpiod_get_index()
  190. devm_gpiod_get_index_optional()
  191. devm_gpiod_get_optional()
  192. devm_gpiod_put()
  193. devm_gpiochip_add_data()
  194. devm_gpiochip_remove()
  195. devm_gpio_request()
  196. devm_gpio_request_one()
  197. devm_gpio_free()
  198. IIO
  199. devm_iio_device_alloc()
  200. devm_iio_device_free()
  201. devm_iio_device_register()
  202. devm_iio_device_unregister()
  203. devm_iio_kfifo_allocate()
  204. devm_iio_kfifo_free()
  205. devm_iio_triggered_buffer_setup()
  206. devm_iio_triggered_buffer_cleanup()
  207. devm_iio_trigger_alloc()
  208. devm_iio_trigger_free()
  209. devm_iio_trigger_register()
  210. devm_iio_trigger_unregister()
  211. devm_iio_channel_get()
  212. devm_iio_channel_release()
  213. devm_iio_channel_get_all()
  214. devm_iio_channel_release_all()
  215. INPUT
  216. devm_input_allocate_device()
  217. IO region
  218. devm_release_mem_region()
  219. devm_release_region()
  220. devm_release_resource()
  221. devm_request_mem_region()
  222. devm_request_region()
  223. devm_request_resource()
  224. IOMAP
  225. devm_ioport_map()
  226. devm_ioport_unmap()
  227. devm_ioremap()
  228. devm_ioremap_nocache()
  229. devm_ioremap_wc()
  230. devm_ioremap_resource() : checks resource, requests memory region, ioremaps
  231. devm_iounmap()
  232. pcim_iomap()
  233. pcim_iomap_regions() : do request_region() and iomap() on multiple BARs
  234. pcim_iomap_table() : array of mapped addresses indexed by BAR
  235. pcim_iounmap()
  236. IRQ
  237. devm_free_irq()
  238. devm_request_any_context_irq()
  239. devm_request_irq()
  240. devm_request_threaded_irq()
  241. LED
  242. devm_led_classdev_register()
  243. devm_led_classdev_unregister()
  244. MDIO
  245. devm_mdiobus_alloc()
  246. devm_mdiobus_alloc_size()
  247. devm_mdiobus_free()
  248. MEM
  249. devm_free_pages()
  250. devm_get_free_pages()
  251. devm_kasprintf()
  252. devm_kcalloc()
  253. devm_kfree()
  254. devm_kmalloc()
  255. devm_kmalloc_array()
  256. devm_kmemdup()
  257. devm_kstrdup()
  258. devm_kvasprintf()
  259. devm_kzalloc()
  260. MFD
  261. devm_mfd_add_devices()
  262. PCI
  263. pcim_enable_device() : after success, all PCI ops become managed
  264. pcim_pin_device() : keep PCI device enabled after release
  265. PHY
  266. devm_usb_get_phy()
  267. devm_usb_put_phy()
  268. PINCTRL
  269. devm_pinctrl_get()
  270. devm_pinctrl_put()
  271. devm_pinctrl_register()
  272. devm_pinctrl_unregister()
  273. POWER
  274. devm_reboot_mode_register()
  275. devm_reboot_mode_unregister()
  276. PWM
  277. devm_pwm_get()
  278. devm_pwm_put()
  279. REGULATOR
  280. devm_regulator_bulk_get()
  281. devm_regulator_get()
  282. devm_regulator_put()
  283. devm_regulator_register()
  284. RESET
  285. devm_reset_control_get()
  286. devm_reset_controller_register()
  287. SLAVE DMA ENGINE
  288. devm_acpi_dma_controller_register()
  289. SPI
  290. devm_spi_register_master()
  291. WATCHDOG
  292. devm_watchdog_register_device()