devres.c 24 KB

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