devres.c 26 KB

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