devres.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/devres.c - device resource management
  4. *
  5. * Copyright (c) 2006 SUSE Linux Products GmbH
  6. * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/percpu.h>
  14. #include "base.h"
  15. struct devres_node {
  16. struct list_head entry;
  17. dr_release_t release;
  18. #ifdef CONFIG_DEBUG_DEVRES
  19. const char *name;
  20. size_t size;
  21. #endif
  22. };
  23. struct devres {
  24. struct devres_node node;
  25. /* -- 3 pointers */
  26. unsigned long long data[]; /* guarantee ull alignment */
  27. };
  28. struct devres_group {
  29. struct devres_node node[2];
  30. void *id;
  31. int color;
  32. /* -- 8 pointers */
  33. };
  34. #ifdef CONFIG_DEBUG_DEVRES
  35. static int log_devres = 0;
  36. module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
  37. static void set_node_dbginfo(struct devres_node *node, const char *name,
  38. size_t size)
  39. {
  40. node->name = name;
  41. node->size = size;
  42. }
  43. static void devres_log(struct device *dev, struct devres_node *node,
  44. const char *op)
  45. {
  46. if (unlikely(log_devres))
  47. dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
  48. op, node, node->name, (unsigned long)node->size);
  49. }
  50. #else /* CONFIG_DEBUG_DEVRES */
  51. #define set_node_dbginfo(node, n, s) do {} while (0)
  52. #define devres_log(dev, node, op) do {} while (0)
  53. #endif /* CONFIG_DEBUG_DEVRES */
  54. /*
  55. * Release functions for devres group. These callbacks are used only
  56. * for identification.
  57. */
  58. static void group_open_release(struct device *dev, void *res)
  59. {
  60. /* noop */
  61. }
  62. static void group_close_release(struct device *dev, void *res)
  63. {
  64. /* noop */
  65. }
  66. static struct devres_group * node_to_group(struct devres_node *node)
  67. {
  68. if (node->release == &group_open_release)
  69. return container_of(node, struct devres_group, node[0]);
  70. if (node->release == &group_close_release)
  71. return container_of(node, struct devres_group, node[1]);
  72. return NULL;
  73. }
  74. static __always_inline struct devres * alloc_dr(dr_release_t release,
  75. size_t size, gfp_t gfp, int nid)
  76. {
  77. size_t tot_size = sizeof(struct devres) + size;
  78. struct devres *dr;
  79. dr = kmalloc_node_track_caller(tot_size, gfp, nid);
  80. if (unlikely(!dr))
  81. return NULL;
  82. memset(dr, 0, offsetof(struct devres, data));
  83. INIT_LIST_HEAD(&dr->node.entry);
  84. dr->node.release = release;
  85. return dr;
  86. }
  87. static void add_dr(struct device *dev, struct devres_node *node)
  88. {
  89. devres_log(dev, node, "ADD");
  90. BUG_ON(!list_empty(&node->entry));
  91. list_add_tail(&node->entry, &dev->devres_head);
  92. }
  93. #ifdef CONFIG_DEBUG_DEVRES
  94. void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
  95. const char *name)
  96. {
  97. struct devres *dr;
  98. dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
  99. if (unlikely(!dr))
  100. return NULL;
  101. set_node_dbginfo(&dr->node, name, size);
  102. return dr->data;
  103. }
  104. EXPORT_SYMBOL_GPL(__devres_alloc_node);
  105. #else
  106. /**
  107. * devres_alloc - Allocate device resource data
  108. * @release: Release function devres will be associated with
  109. * @size: Allocation size
  110. * @gfp: Allocation flags
  111. * @nid: NUMA node
  112. *
  113. * Allocate devres of @size bytes. The allocated area is zeroed, then
  114. * associated with @release. The returned pointer can be passed to
  115. * other devres_*() functions.
  116. *
  117. * RETURNS:
  118. * Pointer to allocated devres on success, NULL on failure.
  119. */
  120. void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
  121. {
  122. struct devres *dr;
  123. dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
  124. if (unlikely(!dr))
  125. return NULL;
  126. return dr->data;
  127. }
  128. EXPORT_SYMBOL_GPL(devres_alloc_node);
  129. #endif
  130. /**
  131. * devres_for_each_res - Resource iterator
  132. * @dev: Device to iterate resource from
  133. * @release: Look for resources associated with this release function
  134. * @match: Match function (optional)
  135. * @match_data: Data for the match function
  136. * @fn: Function to be called for each matched resource.
  137. * @data: Data for @fn, the 3rd parameter of @fn
  138. *
  139. * Call @fn for each devres of @dev which is associated with @release
  140. * and for which @match returns 1.
  141. *
  142. * RETURNS:
  143. * void
  144. */
  145. void devres_for_each_res(struct device *dev, dr_release_t release,
  146. dr_match_t match, void *match_data,
  147. void (*fn)(struct device *, void *, void *),
  148. void *data)
  149. {
  150. struct devres_node *node;
  151. struct devres_node *tmp;
  152. unsigned long flags;
  153. if (!fn)
  154. return;
  155. spin_lock_irqsave(&dev->devres_lock, flags);
  156. list_for_each_entry_safe_reverse(node, tmp,
  157. &dev->devres_head, entry) {
  158. struct devres *dr = container_of(node, struct devres, node);
  159. if (node->release != release)
  160. continue;
  161. if (match && !match(dev, dr->data, match_data))
  162. continue;
  163. fn(dev, dr->data, data);
  164. }
  165. spin_unlock_irqrestore(&dev->devres_lock, flags);
  166. }
  167. EXPORT_SYMBOL_GPL(devres_for_each_res);
  168. /**
  169. * devres_free - Free device resource data
  170. * @res: Pointer to devres data to free
  171. *
  172. * Free devres created with devres_alloc().
  173. */
  174. void devres_free(void *res)
  175. {
  176. if (res) {
  177. struct devres *dr = container_of(res, struct devres, data);
  178. BUG_ON(!list_empty(&dr->node.entry));
  179. kfree(dr);
  180. }
  181. }
  182. EXPORT_SYMBOL_GPL(devres_free);
  183. /**
  184. * devres_add - Register device resource
  185. * @dev: Device to add resource to
  186. * @res: Resource to register
  187. *
  188. * Register devres @res to @dev. @res should have been allocated
  189. * using devres_alloc(). On driver detach, the associated release
  190. * function will be invoked and devres will be freed automatically.
  191. */
  192. void devres_add(struct device *dev, void *res)
  193. {
  194. struct devres *dr = container_of(res, struct devres, data);
  195. unsigned long flags;
  196. spin_lock_irqsave(&dev->devres_lock, flags);
  197. add_dr(dev, &dr->node);
  198. spin_unlock_irqrestore(&dev->devres_lock, flags);
  199. }
  200. EXPORT_SYMBOL_GPL(devres_add);
  201. static struct devres *find_dr(struct device *dev, dr_release_t release,
  202. dr_match_t match, void *match_data)
  203. {
  204. struct devres_node *node;
  205. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  206. struct devres *dr = container_of(node, struct devres, node);
  207. if (node->release != release)
  208. continue;
  209. if (match && !match(dev, dr->data, match_data))
  210. continue;
  211. return dr;
  212. }
  213. return NULL;
  214. }
  215. /**
  216. * devres_find - Find device resource
  217. * @dev: Device to lookup resource from
  218. * @release: Look for resources associated with this release function
  219. * @match: Match function (optional)
  220. * @match_data: Data for the match function
  221. *
  222. * Find the latest devres of @dev which is associated with @release
  223. * and for which @match returns 1. If @match is NULL, it's considered
  224. * to match all.
  225. *
  226. * RETURNS:
  227. * Pointer to found devres, NULL if not found.
  228. */
  229. void * devres_find(struct device *dev, dr_release_t release,
  230. dr_match_t match, void *match_data)
  231. {
  232. struct devres *dr;
  233. unsigned long flags;
  234. spin_lock_irqsave(&dev->devres_lock, flags);
  235. dr = find_dr(dev, release, match, match_data);
  236. spin_unlock_irqrestore(&dev->devres_lock, flags);
  237. if (dr)
  238. return dr->data;
  239. return NULL;
  240. }
  241. EXPORT_SYMBOL_GPL(devres_find);
  242. /**
  243. * devres_get - Find devres, if non-existent, add one atomically
  244. * @dev: Device to lookup or add devres for
  245. * @new_res: Pointer to new initialized devres to add if not found
  246. * @match: Match function (optional)
  247. * @match_data: Data for the match function
  248. *
  249. * Find the latest devres of @dev which has the same release function
  250. * as @new_res and for which @match return 1. If found, @new_res is
  251. * freed; otherwise, @new_res is added atomically.
  252. *
  253. * RETURNS:
  254. * Pointer to found or added devres.
  255. */
  256. void * devres_get(struct device *dev, void *new_res,
  257. dr_match_t match, void *match_data)
  258. {
  259. struct devres *new_dr = container_of(new_res, struct devres, data);
  260. struct devres *dr;
  261. unsigned long flags;
  262. spin_lock_irqsave(&dev->devres_lock, flags);
  263. dr = find_dr(dev, new_dr->node.release, match, match_data);
  264. if (!dr) {
  265. add_dr(dev, &new_dr->node);
  266. dr = new_dr;
  267. new_res = NULL;
  268. }
  269. spin_unlock_irqrestore(&dev->devres_lock, flags);
  270. devres_free(new_res);
  271. return dr->data;
  272. }
  273. EXPORT_SYMBOL_GPL(devres_get);
  274. /**
  275. * devres_remove - Find a device resource and remove it
  276. * @dev: Device to find resource from
  277. * @release: Look for resources associated with this release function
  278. * @match: Match function (optional)
  279. * @match_data: Data for the match function
  280. *
  281. * Find the latest devres of @dev associated with @release and for
  282. * which @match returns 1. If @match is NULL, it's considered to
  283. * match all. If found, the resource is removed atomically and
  284. * returned.
  285. *
  286. * RETURNS:
  287. * Pointer to removed devres on success, NULL if not found.
  288. */
  289. void * devres_remove(struct device *dev, dr_release_t release,
  290. dr_match_t match, void *match_data)
  291. {
  292. struct devres *dr;
  293. unsigned long flags;
  294. spin_lock_irqsave(&dev->devres_lock, flags);
  295. dr = find_dr(dev, release, match, match_data);
  296. if (dr) {
  297. list_del_init(&dr->node.entry);
  298. devres_log(dev, &dr->node, "REM");
  299. }
  300. spin_unlock_irqrestore(&dev->devres_lock, flags);
  301. if (dr)
  302. return dr->data;
  303. return NULL;
  304. }
  305. EXPORT_SYMBOL_GPL(devres_remove);
  306. /**
  307. * devres_destroy - Find a device resource and destroy it
  308. * @dev: Device to find resource from
  309. * @release: Look for resources associated with this release function
  310. * @match: Match function (optional)
  311. * @match_data: Data for the match function
  312. *
  313. * Find the latest devres of @dev associated with @release and for
  314. * which @match returns 1. If @match is NULL, it's considered to
  315. * match all. If found, the resource is removed atomically and freed.
  316. *
  317. * Note that the release function for the resource will not be called,
  318. * only the devres-allocated data will be freed. The caller becomes
  319. * responsible for freeing any other data.
  320. *
  321. * RETURNS:
  322. * 0 if devres is found and freed, -ENOENT if not found.
  323. */
  324. int devres_destroy(struct device *dev, dr_release_t release,
  325. dr_match_t match, void *match_data)
  326. {
  327. void *res;
  328. res = devres_remove(dev, release, match, match_data);
  329. if (unlikely(!res))
  330. return -ENOENT;
  331. devres_free(res);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(devres_destroy);
  335. /**
  336. * devres_release - Find a device resource and destroy it, calling release
  337. * @dev: Device to find resource from
  338. * @release: Look for resources associated with this release function
  339. * @match: Match function (optional)
  340. * @match_data: Data for the match function
  341. *
  342. * Find the latest devres of @dev associated with @release and for
  343. * which @match returns 1. If @match is NULL, it's considered to
  344. * match all. If found, the resource is removed atomically, the
  345. * release function called and the resource freed.
  346. *
  347. * RETURNS:
  348. * 0 if devres is found and freed, -ENOENT if not found.
  349. */
  350. int devres_release(struct device *dev, dr_release_t release,
  351. dr_match_t match, void *match_data)
  352. {
  353. void *res;
  354. res = devres_remove(dev, release, match, match_data);
  355. if (unlikely(!res))
  356. return -ENOENT;
  357. (*release)(dev, res);
  358. devres_free(res);
  359. return 0;
  360. }
  361. EXPORT_SYMBOL_GPL(devres_release);
  362. static int remove_nodes(struct device *dev,
  363. struct list_head *first, struct list_head *end,
  364. struct list_head *todo)
  365. {
  366. int cnt = 0, nr_groups = 0;
  367. struct list_head *cur;
  368. /* First pass - move normal devres entries to @todo and clear
  369. * devres_group colors.
  370. */
  371. cur = first;
  372. while (cur != end) {
  373. struct devres_node *node;
  374. struct devres_group *grp;
  375. node = list_entry(cur, struct devres_node, entry);
  376. cur = cur->next;
  377. grp = node_to_group(node);
  378. if (grp) {
  379. /* clear color of group markers in the first pass */
  380. grp->color = 0;
  381. nr_groups++;
  382. } else {
  383. /* regular devres entry */
  384. if (&node->entry == first)
  385. first = first->next;
  386. list_move_tail(&node->entry, todo);
  387. cnt++;
  388. }
  389. }
  390. if (!nr_groups)
  391. return cnt;
  392. /* Second pass - Scan groups and color them. A group gets
  393. * color value of two iff the group is wholly contained in
  394. * [cur, end). That is, for a closed group, both opening and
  395. * closing markers should be in the range, while just the
  396. * opening marker is enough for an open group.
  397. */
  398. cur = first;
  399. while (cur != end) {
  400. struct devres_node *node;
  401. struct devres_group *grp;
  402. node = list_entry(cur, struct devres_node, entry);
  403. cur = cur->next;
  404. grp = node_to_group(node);
  405. BUG_ON(!grp || list_empty(&grp->node[0].entry));
  406. grp->color++;
  407. if (list_empty(&grp->node[1].entry))
  408. grp->color++;
  409. BUG_ON(grp->color <= 0 || grp->color > 2);
  410. if (grp->color == 2) {
  411. /* No need to update cur or end. The removed
  412. * nodes are always before both.
  413. */
  414. list_move_tail(&grp->node[0].entry, todo);
  415. list_del_init(&grp->node[1].entry);
  416. }
  417. }
  418. return cnt;
  419. }
  420. static int release_nodes(struct device *dev, struct list_head *first,
  421. struct list_head *end, unsigned long flags)
  422. __releases(&dev->devres_lock)
  423. {
  424. LIST_HEAD(todo);
  425. int cnt;
  426. struct devres *dr, *tmp;
  427. cnt = remove_nodes(dev, first, end, &todo);
  428. spin_unlock_irqrestore(&dev->devres_lock, flags);
  429. /* Release. Note that both devres and devres_group are
  430. * handled as devres in the following loop. This is safe.
  431. */
  432. list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
  433. devres_log(dev, &dr->node, "REL");
  434. dr->node.release(dev, dr->data);
  435. kfree(dr);
  436. }
  437. return cnt;
  438. }
  439. /**
  440. * devres_release_all - Release all managed resources
  441. * @dev: Device to release resources for
  442. *
  443. * Release all resources associated with @dev. This function is
  444. * called on driver detach.
  445. */
  446. int devres_release_all(struct device *dev)
  447. {
  448. unsigned long flags;
  449. /* Looks like an uninitialized device structure */
  450. if (WARN_ON(dev->devres_head.next == NULL))
  451. return -ENODEV;
  452. spin_lock_irqsave(&dev->devres_lock, flags);
  453. return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
  454. flags);
  455. }
  456. /**
  457. * devres_open_group - Open a new devres group
  458. * @dev: Device to open devres group for
  459. * @id: Separator ID
  460. * @gfp: Allocation flags
  461. *
  462. * Open a new devres group for @dev with @id. For @id, using a
  463. * pointer to an object which won't be used for another group is
  464. * recommended. If @id is NULL, address-wise unique ID is created.
  465. *
  466. * RETURNS:
  467. * ID of the new group, NULL on failure.
  468. */
  469. void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
  470. {
  471. struct devres_group *grp;
  472. unsigned long flags;
  473. grp = kmalloc(sizeof(*grp), gfp);
  474. if (unlikely(!grp))
  475. return NULL;
  476. grp->node[0].release = &group_open_release;
  477. grp->node[1].release = &group_close_release;
  478. INIT_LIST_HEAD(&grp->node[0].entry);
  479. INIT_LIST_HEAD(&grp->node[1].entry);
  480. set_node_dbginfo(&grp->node[0], "grp<", 0);
  481. set_node_dbginfo(&grp->node[1], "grp>", 0);
  482. grp->id = grp;
  483. if (id)
  484. grp->id = id;
  485. spin_lock_irqsave(&dev->devres_lock, flags);
  486. add_dr(dev, &grp->node[0]);
  487. spin_unlock_irqrestore(&dev->devres_lock, flags);
  488. return grp->id;
  489. }
  490. EXPORT_SYMBOL_GPL(devres_open_group);
  491. /* Find devres group with ID @id. If @id is NULL, look for the latest. */
  492. static struct devres_group * find_group(struct device *dev, void *id)
  493. {
  494. struct devres_node *node;
  495. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  496. struct devres_group *grp;
  497. if (node->release != &group_open_release)
  498. continue;
  499. grp = container_of(node, struct devres_group, node[0]);
  500. if (id) {
  501. if (grp->id == id)
  502. return grp;
  503. } else if (list_empty(&grp->node[1].entry))
  504. return grp;
  505. }
  506. return NULL;
  507. }
  508. /**
  509. * devres_close_group - Close a devres group
  510. * @dev: Device to close devres group for
  511. * @id: ID of target group, can be NULL
  512. *
  513. * Close the group identified by @id. If @id is NULL, the latest open
  514. * group is selected.
  515. */
  516. void devres_close_group(struct device *dev, void *id)
  517. {
  518. struct devres_group *grp;
  519. unsigned long flags;
  520. spin_lock_irqsave(&dev->devres_lock, flags);
  521. grp = find_group(dev, id);
  522. if (grp)
  523. add_dr(dev, &grp->node[1]);
  524. else
  525. WARN_ON(1);
  526. spin_unlock_irqrestore(&dev->devres_lock, flags);
  527. }
  528. EXPORT_SYMBOL_GPL(devres_close_group);
  529. /**
  530. * devres_remove_group - Remove a devres group
  531. * @dev: Device to remove group for
  532. * @id: ID of target group, can be NULL
  533. *
  534. * Remove the group identified by @id. If @id is NULL, the latest
  535. * open group is selected. Note that removing a group doesn't affect
  536. * any other resources.
  537. */
  538. void devres_remove_group(struct device *dev, void *id)
  539. {
  540. struct devres_group *grp;
  541. unsigned long flags;
  542. spin_lock_irqsave(&dev->devres_lock, flags);
  543. grp = find_group(dev, id);
  544. if (grp) {
  545. list_del_init(&grp->node[0].entry);
  546. list_del_init(&grp->node[1].entry);
  547. devres_log(dev, &grp->node[0], "REM");
  548. } else
  549. WARN_ON(1);
  550. spin_unlock_irqrestore(&dev->devres_lock, flags);
  551. kfree(grp);
  552. }
  553. EXPORT_SYMBOL_GPL(devres_remove_group);
  554. /**
  555. * devres_release_group - Release resources in a devres group
  556. * @dev: Device to release group for
  557. * @id: ID of target group, can be NULL
  558. *
  559. * Release all resources in the group identified by @id. If @id is
  560. * NULL, the latest open group is selected. The selected group and
  561. * groups properly nested inside the selected group are removed.
  562. *
  563. * RETURNS:
  564. * The number of released non-group resources.
  565. */
  566. int devres_release_group(struct device *dev, void *id)
  567. {
  568. struct devres_group *grp;
  569. unsigned long flags;
  570. int cnt = 0;
  571. spin_lock_irqsave(&dev->devres_lock, flags);
  572. grp = find_group(dev, id);
  573. if (grp) {
  574. struct list_head *first = &grp->node[0].entry;
  575. struct list_head *end = &dev->devres_head;
  576. if (!list_empty(&grp->node[1].entry))
  577. end = grp->node[1].entry.next;
  578. cnt = release_nodes(dev, first, end, flags);
  579. } else {
  580. WARN_ON(1);
  581. spin_unlock_irqrestore(&dev->devres_lock, flags);
  582. }
  583. return cnt;
  584. }
  585. EXPORT_SYMBOL_GPL(devres_release_group);
  586. /*
  587. * Custom devres actions allow inserting a simple function call
  588. * into the teadown sequence.
  589. */
  590. struct action_devres {
  591. void *data;
  592. void (*action)(void *);
  593. };
  594. static int devm_action_match(struct device *dev, void *res, void *p)
  595. {
  596. struct action_devres *devres = res;
  597. struct action_devres *target = p;
  598. return devres->action == target->action &&
  599. devres->data == target->data;
  600. }
  601. static void devm_action_release(struct device *dev, void *res)
  602. {
  603. struct action_devres *devres = res;
  604. devres->action(devres->data);
  605. }
  606. /**
  607. * devm_add_action() - add a custom action to list of managed resources
  608. * @dev: Device that owns the action
  609. * @action: Function that should be called
  610. * @data: Pointer to data passed to @action implementation
  611. *
  612. * This adds a custom action to the list of managed resources so that
  613. * it gets executed as part of standard resource unwinding.
  614. */
  615. int devm_add_action(struct device *dev, void (*action)(void *), void *data)
  616. {
  617. struct action_devres *devres;
  618. devres = devres_alloc(devm_action_release,
  619. sizeof(struct action_devres), GFP_KERNEL);
  620. if (!devres)
  621. return -ENOMEM;
  622. devres->data = data;
  623. devres->action = action;
  624. devres_add(dev, devres);
  625. return 0;
  626. }
  627. EXPORT_SYMBOL_GPL(devm_add_action);
  628. /**
  629. * devm_remove_action() - removes previously added custom action
  630. * @dev: Device that owns the action
  631. * @action: Function implementing the action
  632. * @data: Pointer to data passed to @action implementation
  633. *
  634. * Removes instance of @action previously added by devm_add_action().
  635. * Both action and data should match one of the existing entries.
  636. */
  637. void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
  638. {
  639. struct action_devres devres = {
  640. .data = data,
  641. .action = action,
  642. };
  643. WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
  644. &devres));
  645. }
  646. EXPORT_SYMBOL_GPL(devm_remove_action);
  647. /*
  648. * Managed kmalloc/kfree
  649. */
  650. static void devm_kmalloc_release(struct device *dev, void *res)
  651. {
  652. /* noop */
  653. }
  654. static int devm_kmalloc_match(struct device *dev, void *res, void *data)
  655. {
  656. return res == data;
  657. }
  658. /**
  659. * devm_kmalloc - Resource-managed kmalloc
  660. * @dev: Device to allocate memory for
  661. * @size: Allocation size
  662. * @gfp: Allocation gfp flags
  663. *
  664. * Managed kmalloc. Memory allocated with this function is
  665. * automatically freed on driver detach. Like all other devres
  666. * resources, guaranteed alignment is unsigned long long.
  667. *
  668. * RETURNS:
  669. * Pointer to allocated memory on success, NULL on failure.
  670. */
  671. void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
  672. {
  673. struct devres *dr;
  674. /* use raw alloc_dr for kmalloc caller tracing */
  675. dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
  676. if (unlikely(!dr))
  677. return NULL;
  678. /*
  679. * This is named devm_kzalloc_release for historical reasons
  680. * The initial implementation did not support kmalloc, only kzalloc
  681. */
  682. set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
  683. devres_add(dev, dr->data);
  684. return dr->data;
  685. }
  686. EXPORT_SYMBOL_GPL(devm_kmalloc);
  687. /**
  688. * devm_kstrdup - Allocate resource managed space and
  689. * copy an existing string into that.
  690. * @dev: Device to allocate memory for
  691. * @s: the string to duplicate
  692. * @gfp: the GFP mask used in the devm_kmalloc() call when
  693. * allocating memory
  694. * RETURNS:
  695. * Pointer to allocated string on success, NULL on failure.
  696. */
  697. char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
  698. {
  699. size_t size;
  700. char *buf;
  701. if (!s)
  702. return NULL;
  703. size = strlen(s) + 1;
  704. buf = devm_kmalloc(dev, size, gfp);
  705. if (buf)
  706. memcpy(buf, s, size);
  707. return buf;
  708. }
  709. EXPORT_SYMBOL_GPL(devm_kstrdup);
  710. /**
  711. * devm_kvasprintf - Allocate resource managed space and format a string
  712. * into that.
  713. * @dev: Device to allocate memory for
  714. * @gfp: the GFP mask used in the devm_kmalloc() call when
  715. * allocating memory
  716. * @fmt: The printf()-style format string
  717. * @ap: Arguments for the format string
  718. * RETURNS:
  719. * Pointer to allocated string on success, NULL on failure.
  720. */
  721. char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
  722. va_list ap)
  723. {
  724. unsigned int len;
  725. char *p;
  726. va_list aq;
  727. va_copy(aq, ap);
  728. len = vsnprintf(NULL, 0, fmt, aq);
  729. va_end(aq);
  730. p = devm_kmalloc(dev, len+1, gfp);
  731. if (!p)
  732. return NULL;
  733. vsnprintf(p, len+1, fmt, ap);
  734. return p;
  735. }
  736. EXPORT_SYMBOL(devm_kvasprintf);
  737. /**
  738. * devm_kasprintf - Allocate resource managed space and format a string
  739. * into that.
  740. * @dev: Device to allocate memory for
  741. * @gfp: the GFP mask used in the devm_kmalloc() call when
  742. * allocating memory
  743. * @fmt: The printf()-style format string
  744. * @...: Arguments for the format string
  745. * RETURNS:
  746. * Pointer to allocated string on success, NULL on failure.
  747. */
  748. char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
  749. {
  750. va_list ap;
  751. char *p;
  752. va_start(ap, fmt);
  753. p = devm_kvasprintf(dev, gfp, fmt, ap);
  754. va_end(ap);
  755. return p;
  756. }
  757. EXPORT_SYMBOL_GPL(devm_kasprintf);
  758. /**
  759. * devm_kfree - Resource-managed kfree
  760. * @dev: Device this memory belongs to
  761. * @p: Memory to free
  762. *
  763. * Free memory allocated with devm_kmalloc().
  764. */
  765. void devm_kfree(struct device *dev, void *p)
  766. {
  767. int rc;
  768. rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
  769. WARN_ON(rc);
  770. }
  771. EXPORT_SYMBOL_GPL(devm_kfree);
  772. /**
  773. * devm_kmemdup - Resource-managed kmemdup
  774. * @dev: Device this memory belongs to
  775. * @src: Memory region to duplicate
  776. * @len: Memory region length
  777. * @gfp: GFP mask to use
  778. *
  779. * Duplicate region of a memory using resource managed kmalloc
  780. */
  781. void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
  782. {
  783. void *p;
  784. p = devm_kmalloc(dev, len, gfp);
  785. if (p)
  786. memcpy(p, src, len);
  787. return p;
  788. }
  789. EXPORT_SYMBOL_GPL(devm_kmemdup);
  790. struct pages_devres {
  791. unsigned long addr;
  792. unsigned int order;
  793. };
  794. static int devm_pages_match(struct device *dev, void *res, void *p)
  795. {
  796. struct pages_devres *devres = res;
  797. struct pages_devres *target = p;
  798. return devres->addr == target->addr;
  799. }
  800. static void devm_pages_release(struct device *dev, void *res)
  801. {
  802. struct pages_devres *devres = res;
  803. free_pages(devres->addr, devres->order);
  804. }
  805. /**
  806. * devm_get_free_pages - Resource-managed __get_free_pages
  807. * @dev: Device to allocate memory for
  808. * @gfp_mask: Allocation gfp flags
  809. * @order: Allocation size is (1 << order) pages
  810. *
  811. * Managed get_free_pages. Memory allocated with this function is
  812. * automatically freed on driver detach.
  813. *
  814. * RETURNS:
  815. * Address of allocated memory on success, 0 on failure.
  816. */
  817. unsigned long devm_get_free_pages(struct device *dev,
  818. gfp_t gfp_mask, unsigned int order)
  819. {
  820. struct pages_devres *devres;
  821. unsigned long addr;
  822. addr = __get_free_pages(gfp_mask, order);
  823. if (unlikely(!addr))
  824. return 0;
  825. devres = devres_alloc(devm_pages_release,
  826. sizeof(struct pages_devres), GFP_KERNEL);
  827. if (unlikely(!devres)) {
  828. free_pages(addr, order);
  829. return 0;
  830. }
  831. devres->addr = addr;
  832. devres->order = order;
  833. devres_add(dev, devres);
  834. return addr;
  835. }
  836. EXPORT_SYMBOL_GPL(devm_get_free_pages);
  837. /**
  838. * devm_free_pages - Resource-managed free_pages
  839. * @dev: Device this memory belongs to
  840. * @addr: Memory to free
  841. *
  842. * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
  843. * there is no need to supply the @order.
  844. */
  845. void devm_free_pages(struct device *dev, unsigned long addr)
  846. {
  847. struct pages_devres devres = { .addr = addr };
  848. WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
  849. &devres));
  850. }
  851. EXPORT_SYMBOL_GPL(devm_free_pages);
  852. static void devm_percpu_release(struct device *dev, void *pdata)
  853. {
  854. void __percpu *p;
  855. p = *(void __percpu **)pdata;
  856. free_percpu(p);
  857. }
  858. static int devm_percpu_match(struct device *dev, void *data, void *p)
  859. {
  860. struct devres *devr = container_of(data, struct devres, data);
  861. return *(void **)devr->data == p;
  862. }
  863. /**
  864. * __devm_alloc_percpu - Resource-managed alloc_percpu
  865. * @dev: Device to allocate per-cpu memory for
  866. * @size: Size of per-cpu memory to allocate
  867. * @align: Alignment of per-cpu memory to allocate
  868. *
  869. * Managed alloc_percpu. Per-cpu memory allocated with this function is
  870. * automatically freed on driver detach.
  871. *
  872. * RETURNS:
  873. * Pointer to allocated memory on success, NULL on failure.
  874. */
  875. void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  876. size_t align)
  877. {
  878. void *p;
  879. void __percpu *pcpu;
  880. pcpu = __alloc_percpu(size, align);
  881. if (!pcpu)
  882. return NULL;
  883. p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
  884. if (!p) {
  885. free_percpu(pcpu);
  886. return NULL;
  887. }
  888. *(void __percpu **)p = pcpu;
  889. devres_add(dev, p);
  890. return pcpu;
  891. }
  892. EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
  893. /**
  894. * devm_free_percpu - Resource-managed free_percpu
  895. * @dev: Device this memory belongs to
  896. * @pdata: Per-cpu memory to free
  897. *
  898. * Free memory allocated with devm_alloc_percpu().
  899. */
  900. void devm_free_percpu(struct device *dev, void __percpu *pdata)
  901. {
  902. WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
  903. (void *)pdata));
  904. }
  905. EXPORT_SYMBOL_GPL(devm_free_percpu);