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