devres.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. ================================
  2. Devres - Managed Device Resource
  3. ================================
  4. Tejun Heo <[email protected]>
  5. First draft 10 January 2007
  6. .. contents
  7. 1. Intro : Huh? Devres?
  8. 2. Devres : Devres in a nutshell
  9. 3. Devres Group : Group devres'es and release them together
  10. 4. Details : Life time rules, calling context, ...
  11. 5. Overhead : How much do we have to pay for this?
  12. 6. List of managed interfaces: Currently implemented managed interfaces
  13. 1. Intro
  14. --------
  15. devres came up while trying to convert libata to use iomap. Each
  16. iomapped address should be kept and unmapped on driver detach. For
  17. example, a plain SFF ATA controller (that is, good old PCI IDE) in
  18. native mode makes use of 5 PCI BARs and all of them should be
  19. maintained.
  20. As with many other device drivers, libata low level drivers have
  21. sufficient bugs in ->remove and ->probe failure path. Well, yes,
  22. that's probably because libata low level driver developers are lazy
  23. bunch, but aren't all low level driver developers? After spending a
  24. day fiddling with braindamaged hardware with no document or
  25. braindamaged document, if it's finally working, well, it's working.
  26. For one reason or another, low level drivers don't receive as much
  27. attention or testing as core code, and bugs on driver detach or
  28. initialization failure don't happen often enough to be noticeable.
  29. Init failure path is worse because it's much less travelled while
  30. needs to handle multiple entry points.
  31. So, many low level drivers end up leaking resources on driver detach
  32. and having half broken failure path implementation in ->probe() which
  33. would leak resources or even cause oops when failure occurs. iomap
  34. adds more to this mix. So do msi and msix.
  35. 2. Devres
  36. ---------
  37. devres is basically linked list of arbitrarily sized memory areas
  38. associated with a struct device. Each devres entry is associated with
  39. a release function. A devres can be released in several ways. No
  40. matter what, all devres entries are released on driver detach. On
  41. release, the associated release function is invoked and then the
  42. devres entry is freed.
  43. Managed interface is created for resources commonly used by device
  44. drivers using devres. For example, coherent DMA memory is acquired
  45. using dma_alloc_coherent(). The managed version is called
  46. dmam_alloc_coherent(). It is identical to dma_alloc_coherent() except
  47. for the DMA memory allocated using it is managed and will be
  48. automatically released on driver detach. Implementation looks like
  49. the following::
  50. struct dma_devres {
  51. size_t size;
  52. void *vaddr;
  53. dma_addr_t dma_handle;
  54. };
  55. static void dmam_coherent_release(struct device *dev, void *res)
  56. {
  57. struct dma_devres *this = res;
  58. dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  59. }
  60. dmam_alloc_coherent(dev, size, dma_handle, gfp)
  61. {
  62. struct dma_devres *dr;
  63. void *vaddr;
  64. dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
  65. ...
  66. /* alloc DMA memory as usual */
  67. vaddr = dma_alloc_coherent(...);
  68. ...
  69. /* record size, vaddr, dma_handle in dr */
  70. dr->vaddr = vaddr;
  71. ...
  72. devres_add(dev, dr);
  73. return vaddr;
  74. }
  75. If a driver uses dmam_alloc_coherent(), the area is guaranteed to be
  76. freed whether initialization fails half-way or the device gets
  77. detached. If most resources are acquired using managed interface, a
  78. driver can have much simpler init and exit code. Init path basically
  79. looks like the following::
  80. my_init_one()
  81. {
  82. struct mydev *d;
  83. d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
  84. if (!d)
  85. return -ENOMEM;
  86. d->ring = dmam_alloc_coherent(...);
  87. if (!d->ring)
  88. return -ENOMEM;
  89. if (check something)
  90. return -EINVAL;
  91. ...
  92. return register_to_upper_layer(d);
  93. }
  94. And exit path::
  95. my_remove_one()
  96. {
  97. unregister_from_upper_layer(d);
  98. shutdown_my_hardware();
  99. }
  100. As shown above, low level drivers can be simplified a lot by using
  101. devres. Complexity is shifted from less maintained low level drivers
  102. to better maintained higher layer. Also, as init failure path is
  103. shared with exit path, both can get more testing.
  104. Note though that when converting current calls or assignments to
  105. managed devm_* versions it is up to you to check if internal operations
  106. like allocating memory, have failed. Managed resources pertains to the
  107. freeing of these resources *only* - all other checks needed are still
  108. on you. In some cases this may mean introducing checks that were not
  109. necessary before moving to the managed devm_* calls.
  110. 3. Devres group
  111. ---------------
  112. Devres entries can be grouped using devres group. When a group is
  113. released, all contained normal devres entries and properly nested
  114. groups are released. One usage is to rollback series of acquired
  115. resources on failure. For example::
  116. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  117. return -ENOMEM;
  118. acquire A;
  119. if (failed)
  120. goto err;
  121. acquire B;
  122. if (failed)
  123. goto err;
  124. ...
  125. devres_remove_group(dev, NULL);
  126. return 0;
  127. err:
  128. devres_release_group(dev, NULL);
  129. return err_code;
  130. As resource acquisition failure usually means probe failure, constructs
  131. like above are usually useful in midlayer driver (e.g. libata core
  132. layer) where interface function shouldn't have side effect on failure.
  133. For LLDs, just returning error code suffices in most cases.
  134. Each group is identified by `void *id`. It can either be explicitly
  135. specified by @id argument to devres_open_group() or automatically
  136. created by passing NULL as @id as in the above example. In both
  137. cases, devres_open_group() returns the group's id. The returned id
  138. can be passed to other devres functions to select the target group.
  139. If NULL is given to those functions, the latest open group is
  140. selected.
  141. For example, you can do something like the following::
  142. int my_midlayer_create_something()
  143. {
  144. if (!devres_open_group(dev, my_midlayer_create_something, GFP_KERNEL))
  145. return -ENOMEM;
  146. ...
  147. devres_close_group(dev, my_midlayer_create_something);
  148. return 0;
  149. }
  150. void my_midlayer_destroy_something()
  151. {
  152. devres_release_group(dev, my_midlayer_create_something);
  153. }
  154. 4. Details
  155. ----------
  156. Lifetime of a devres entry begins on devres allocation and finishes
  157. when it is released or destroyed (removed and freed) - no reference
  158. counting.
  159. devres core guarantees atomicity to all basic devres operations and
  160. has support for single-instance devres types (atomic
  161. lookup-and-add-if-not-found). Other than that, synchronizing
  162. concurrent accesses to allocated devres data is caller's
  163. responsibility. This is usually non-issue because bus ops and
  164. resource allocations already do the job.
  165. For an example of single-instance devres type, read pcim_iomap_table()
  166. in lib/devres.c.
  167. All devres interface functions can be called without context if the
  168. right gfp mask is given.
  169. 5. Overhead
  170. -----------
  171. Each devres bookkeeping info is allocated together with requested data
  172. area. With debug option turned off, bookkeeping info occupies 16
  173. bytes on 32bit machines and 24 bytes on 64bit (three pointers rounded
  174. up to ull alignment). If singly linked list is used, it can be
  175. reduced to two pointers (8 bytes on 32bit, 16 bytes on 64bit).
  176. Each devres group occupies 8 pointers. It can be reduced to 6 if
  177. singly linked list is used.
  178. Memory space overhead on ahci controller with two ports is between 300
  179. and 400 bytes on 32bit machine after naive conversion (we can
  180. certainly invest a bit more effort into libata core layer).
  181. 6. List of managed interfaces
  182. -----------------------------
  183. CLOCK
  184. devm_clk_get()
  185. devm_clk_get_optional()
  186. devm_clk_put()
  187. devm_clk_bulk_get()
  188. devm_clk_bulk_get_all()
  189. devm_clk_bulk_get_optional()
  190. devm_get_clk_from_child()
  191. devm_clk_hw_register()
  192. devm_of_clk_add_hw_provider()
  193. devm_clk_hw_register_clkdev()
  194. DMA
  195. dmaenginem_async_device_register()
  196. dmam_alloc_coherent()
  197. dmam_alloc_attrs()
  198. dmam_free_coherent()
  199. dmam_pool_create()
  200. dmam_pool_destroy()
  201. DRM
  202. devm_drm_dev_alloc()
  203. GPIO
  204. devm_gpiod_get()
  205. devm_gpiod_get_array()
  206. devm_gpiod_get_array_optional()
  207. devm_gpiod_get_index()
  208. devm_gpiod_get_index_optional()
  209. devm_gpiod_get_optional()
  210. devm_gpiod_put()
  211. devm_gpiod_unhinge()
  212. devm_gpiochip_add_data()
  213. devm_gpio_request()
  214. devm_gpio_request_one()
  215. I2C
  216. devm_i2c_add_adapter()
  217. devm_i2c_new_dummy_device()
  218. IIO
  219. devm_iio_device_alloc()
  220. devm_iio_device_register()
  221. devm_iio_dmaengine_buffer_setup()
  222. devm_iio_kfifo_buffer_setup()
  223. devm_iio_map_array_register()
  224. devm_iio_triggered_buffer_setup()
  225. devm_iio_trigger_alloc()
  226. devm_iio_trigger_register()
  227. devm_iio_channel_get()
  228. devm_iio_channel_get_all()
  229. INPUT
  230. devm_input_allocate_device()
  231. IO region
  232. devm_release_mem_region()
  233. devm_release_region()
  234. devm_release_resource()
  235. devm_request_mem_region()
  236. devm_request_free_mem_region()
  237. devm_request_region()
  238. devm_request_resource()
  239. IOMAP
  240. devm_ioport_map()
  241. devm_ioport_unmap()
  242. devm_ioremap()
  243. devm_ioremap_uc()
  244. devm_ioremap_wc()
  245. devm_ioremap_resource() : checks resource, requests memory region, ioremaps
  246. devm_ioremap_resource_wc()
  247. devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device
  248. devm_platform_ioremap_resource_byname()
  249. devm_platform_get_and_ioremap_resource()
  250. devm_iounmap()
  251. pcim_iomap()
  252. pcim_iomap_regions() : do request_region() and iomap() on multiple BARs
  253. pcim_iomap_table() : array of mapped addresses indexed by BAR
  254. pcim_iounmap()
  255. IRQ
  256. devm_free_irq()
  257. devm_request_any_context_irq()
  258. devm_request_irq()
  259. devm_request_threaded_irq()
  260. devm_irq_alloc_descs()
  261. devm_irq_alloc_desc()
  262. devm_irq_alloc_desc_at()
  263. devm_irq_alloc_desc_from()
  264. devm_irq_alloc_descs_from()
  265. devm_irq_alloc_generic_chip()
  266. devm_irq_setup_generic_chip()
  267. devm_irq_domain_create_sim()
  268. LED
  269. devm_led_classdev_register()
  270. devm_led_classdev_unregister()
  271. MDIO
  272. devm_mdiobus_alloc()
  273. devm_mdiobus_alloc_size()
  274. devm_mdiobus_register()
  275. devm_of_mdiobus_register()
  276. MEM
  277. devm_free_pages()
  278. devm_get_free_pages()
  279. devm_kasprintf()
  280. devm_kcalloc()
  281. devm_kfree()
  282. devm_kmalloc()
  283. devm_kmalloc_array()
  284. devm_kmemdup()
  285. devm_krealloc()
  286. devm_kstrdup()
  287. devm_kvasprintf()
  288. devm_kzalloc()
  289. MFD
  290. devm_mfd_add_devices()
  291. MUX
  292. devm_mux_chip_alloc()
  293. devm_mux_chip_register()
  294. devm_mux_control_get()
  295. devm_mux_state_get()
  296. NET
  297. devm_alloc_etherdev()
  298. devm_alloc_etherdev_mqs()
  299. devm_register_netdev()
  300. PER-CPU MEM
  301. devm_alloc_percpu()
  302. devm_free_percpu()
  303. PCI
  304. devm_pci_alloc_host_bridge() : managed PCI host bridge allocation
  305. devm_pci_remap_cfgspace() : ioremap PCI configuration space
  306. devm_pci_remap_cfg_resource() : ioremap PCI configuration space resource
  307. pcim_enable_device() : after success, all PCI ops become managed
  308. pcim_pin_device() : keep PCI device enabled after release
  309. PHY
  310. devm_usb_get_phy()
  311. devm_usb_put_phy()
  312. PINCTRL
  313. devm_pinctrl_get()
  314. devm_pinctrl_put()
  315. devm_pinctrl_get_select()
  316. devm_pinctrl_register()
  317. devm_pinctrl_register_and_init()
  318. devm_pinctrl_unregister()
  319. POWER
  320. devm_reboot_mode_register()
  321. devm_reboot_mode_unregister()
  322. PWM
  323. devm_pwm_get()
  324. devm_fwnode_pwm_get()
  325. REGULATOR
  326. devm_regulator_bulk_register_supply_alias()
  327. devm_regulator_bulk_get()
  328. devm_regulator_bulk_get_enable()
  329. devm_regulator_bulk_put()
  330. devm_regulator_get()
  331. devm_regulator_get_enable()
  332. devm_regulator_get_enable_optional()
  333. devm_regulator_get_exclusive()
  334. devm_regulator_get_optional()
  335. devm_regulator_irq_helper()
  336. devm_regulator_put()
  337. devm_regulator_register()
  338. devm_regulator_register_notifier()
  339. devm_regulator_register_supply_alias()
  340. devm_regulator_unregister_notifier()
  341. RESET
  342. devm_reset_control_get()
  343. devm_reset_controller_register()
  344. RTC
  345. devm_rtc_device_register()
  346. devm_rtc_allocate_device()
  347. devm_rtc_register_device()
  348. devm_rtc_nvmem_register()
  349. SERDEV
  350. devm_serdev_device_open()
  351. SLAVE DMA ENGINE
  352. devm_acpi_dma_controller_register()
  353. SPI
  354. devm_spi_alloc_master()
  355. devm_spi_alloc_slave()
  356. devm_spi_register_master()
  357. WATCHDOG
  358. devm_watchdog_register_device()