devres.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 "base.h"
  12. struct devres_node {
  13. struct list_head entry;
  14. dr_release_t release;
  15. #ifdef CONFIG_DEBUG_DEVRES
  16. const char *name;
  17. size_t size;
  18. #endif
  19. };
  20. struct devres {
  21. struct devres_node node;
  22. /* -- 3 pointers */
  23. unsigned long long data[]; /* guarantee ull alignment */
  24. };
  25. struct devres_group {
  26. struct devres_node node[2];
  27. void *id;
  28. int color;
  29. /* -- 8 pointers */
  30. };
  31. #ifdef CONFIG_DEBUG_DEVRES
  32. static int log_devres = 0;
  33. module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
  34. static void set_node_dbginfo(struct devres_node *node, const char *name,
  35. size_t size)
  36. {
  37. node->name = name;
  38. node->size = size;
  39. }
  40. static void devres_log(struct device *dev, struct devres_node *node,
  41. const char *op)
  42. {
  43. if (unlikely(log_devres))
  44. dev_printk(KERN_ERR, dev, "DEVRES %3s %p %s (%lu bytes)\n",
  45. op, node, node->name, (unsigned long)node->size);
  46. }
  47. #else /* CONFIG_DEBUG_DEVRES */
  48. #define set_node_dbginfo(node, n, s) do {} while (0)
  49. #define devres_log(dev, node, op) do {} while (0)
  50. #endif /* CONFIG_DEBUG_DEVRES */
  51. /*
  52. * Release functions for devres group. These callbacks are used only
  53. * for identification.
  54. */
  55. static void group_open_release(struct device *dev, void *res)
  56. {
  57. /* noop */
  58. }
  59. static void group_close_release(struct device *dev, void *res)
  60. {
  61. /* noop */
  62. }
  63. static struct devres_group * node_to_group(struct devres_node *node)
  64. {
  65. if (node->release == &group_open_release)
  66. return container_of(node, struct devres_group, node[0]);
  67. if (node->release == &group_close_release)
  68. return container_of(node, struct devres_group, node[1]);
  69. return NULL;
  70. }
  71. static __always_inline struct devres * alloc_dr(dr_release_t release,
  72. size_t size, gfp_t gfp)
  73. {
  74. size_t tot_size = sizeof(struct devres) + size;
  75. struct devres *dr;
  76. dr = kmalloc_track_caller(tot_size, gfp);
  77. if (unlikely(!dr))
  78. return NULL;
  79. memset(dr, 0, tot_size);
  80. INIT_LIST_HEAD(&dr->node.entry);
  81. dr->node.release = release;
  82. return dr;
  83. }
  84. static void add_dr(struct device *dev, struct devres_node *node)
  85. {
  86. devres_log(dev, node, "ADD");
  87. BUG_ON(!list_empty(&node->entry));
  88. list_add_tail(&node->entry, &dev->devres_head);
  89. }
  90. #ifdef CONFIG_DEBUG_DEVRES
  91. void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  92. const char *name)
  93. {
  94. struct devres *dr;
  95. dr = alloc_dr(release, size, gfp);
  96. if (unlikely(!dr))
  97. return NULL;
  98. set_node_dbginfo(&dr->node, name, size);
  99. return dr->data;
  100. }
  101. EXPORT_SYMBOL_GPL(__devres_alloc);
  102. #else
  103. /**
  104. * devres_alloc - Allocate device resource data
  105. * @release: Release function devres will be associated with
  106. * @size: Allocation size
  107. * @gfp: Allocation flags
  108. *
  109. * Allocate devres of @size bytes. The allocated area is zeroed, then
  110. * associated with @release. The returned pointer can be passed to
  111. * other devres_*() functions.
  112. *
  113. * RETURNS:
  114. * Pointer to allocated devres on success, NULL on failure.
  115. */
  116. void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  117. {
  118. struct devres *dr;
  119. dr = alloc_dr(release, size, gfp);
  120. if (unlikely(!dr))
  121. return NULL;
  122. return dr->data;
  123. }
  124. EXPORT_SYMBOL_GPL(devres_alloc);
  125. #endif
  126. /**
  127. * devres_free - Free device resource data
  128. * @res: Pointer to devres data to free
  129. *
  130. * Free devres created with devres_alloc().
  131. */
  132. void devres_free(void *res)
  133. {
  134. if (res) {
  135. struct devres *dr = container_of(res, struct devres, data);
  136. BUG_ON(!list_empty(&dr->node.entry));
  137. kfree(dr);
  138. }
  139. }
  140. EXPORT_SYMBOL_GPL(devres_free);
  141. /**
  142. * devres_add - Register device resource
  143. * @dev: Device to add resource to
  144. * @res: Resource to register
  145. *
  146. * Register devres @res to @dev. @res should have been allocated
  147. * using devres_alloc(). On driver detach, the associated release
  148. * function will be invoked and devres will be freed automatically.
  149. */
  150. void devres_add(struct device *dev, void *res)
  151. {
  152. struct devres *dr = container_of(res, struct devres, data);
  153. unsigned long flags;
  154. spin_lock_irqsave(&dev->devres_lock, flags);
  155. add_dr(dev, &dr->node);
  156. spin_unlock_irqrestore(&dev->devres_lock, flags);
  157. }
  158. EXPORT_SYMBOL_GPL(devres_add);
  159. static struct devres *find_dr(struct device *dev, dr_release_t release,
  160. dr_match_t match, void *match_data)
  161. {
  162. struct devres_node *node;
  163. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  164. struct devres *dr = container_of(node, struct devres, node);
  165. if (node->release != release)
  166. continue;
  167. if (match && !match(dev, dr->data, match_data))
  168. continue;
  169. return dr;
  170. }
  171. return NULL;
  172. }
  173. /**
  174. * devres_find - Find device resource
  175. * @dev: Device to lookup resource from
  176. * @release: Look for resources associated with this release function
  177. * @match: Match function (optional)
  178. * @match_data: Data for the match function
  179. *
  180. * Find the latest devres of @dev which is associated with @release
  181. * and for which @match returns 1. If @match is NULL, it's considered
  182. * to match all.
  183. *
  184. * RETURNS:
  185. * Pointer to found devres, NULL if not found.
  186. */
  187. void * devres_find(struct device *dev, dr_release_t release,
  188. dr_match_t match, void *match_data)
  189. {
  190. struct devres *dr;
  191. unsigned long flags;
  192. spin_lock_irqsave(&dev->devres_lock, flags);
  193. dr = find_dr(dev, release, match, match_data);
  194. spin_unlock_irqrestore(&dev->devres_lock, flags);
  195. if (dr)
  196. return dr->data;
  197. return NULL;
  198. }
  199. EXPORT_SYMBOL_GPL(devres_find);
  200. /**
  201. * devres_get - Find devres, if non-existent, add one atomically
  202. * @dev: Device to lookup or add devres for
  203. * @new_res: Pointer to new initialized devres to add if not found
  204. * @match: Match function (optional)
  205. * @match_data: Data for the match function
  206. *
  207. * Find the latest devres of @dev which has the same release function
  208. * as @new_res and for which @match return 1. If found, @new_res is
  209. * freed; otherwise, @new_res is added atomically.
  210. *
  211. * RETURNS:
  212. * Pointer to found or added devres.
  213. */
  214. void * devres_get(struct device *dev, void *new_res,
  215. dr_match_t match, void *match_data)
  216. {
  217. struct devres *new_dr = container_of(new_res, struct devres, data);
  218. struct devres *dr;
  219. unsigned long flags;
  220. spin_lock_irqsave(&dev->devres_lock, flags);
  221. dr = find_dr(dev, new_dr->node.release, match, match_data);
  222. if (!dr) {
  223. add_dr(dev, &new_dr->node);
  224. dr = new_dr;
  225. new_dr = NULL;
  226. }
  227. spin_unlock_irqrestore(&dev->devres_lock, flags);
  228. devres_free(new_dr);
  229. return dr->data;
  230. }
  231. EXPORT_SYMBOL_GPL(devres_get);
  232. /**
  233. * devres_remove - Find a device resource and remove it
  234. * @dev: Device to find resource from
  235. * @release: Look for resources associated with this release function
  236. * @match: Match function (optional)
  237. * @match_data: Data for the match function
  238. *
  239. * Find the latest devres of @dev associated with @release and for
  240. * which @match returns 1. If @match is NULL, it's considered to
  241. * match all. If found, the resource is removed atomically and
  242. * returned.
  243. *
  244. * RETURNS:
  245. * Pointer to removed devres on success, NULL if not found.
  246. */
  247. void * devres_remove(struct device *dev, dr_release_t release,
  248. dr_match_t match, void *match_data)
  249. {
  250. struct devres *dr;
  251. unsigned long flags;
  252. spin_lock_irqsave(&dev->devres_lock, flags);
  253. dr = find_dr(dev, release, match, match_data);
  254. if (dr) {
  255. list_del_init(&dr->node.entry);
  256. devres_log(dev, &dr->node, "REM");
  257. }
  258. spin_unlock_irqrestore(&dev->devres_lock, flags);
  259. if (dr)
  260. return dr->data;
  261. return NULL;
  262. }
  263. EXPORT_SYMBOL_GPL(devres_remove);
  264. /**
  265. * devres_destroy - Find a device resource and destroy it
  266. * @dev: Device to find resource from
  267. * @release: Look for resources associated with this release function
  268. * @match: Match function (optional)
  269. * @match_data: Data for the match function
  270. *
  271. * Find the latest devres of @dev associated with @release and for
  272. * which @match returns 1. If @match is NULL, it's considered to
  273. * match all. If found, the resource is removed atomically and freed.
  274. *
  275. * RETURNS:
  276. * 0 if devres is found and freed, -ENOENT if not found.
  277. */
  278. int devres_destroy(struct device *dev, dr_release_t release,
  279. dr_match_t match, void *match_data)
  280. {
  281. void *res;
  282. res = devres_remove(dev, release, match, match_data);
  283. if (unlikely(!res))
  284. return -ENOENT;
  285. devres_free(res);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(devres_destroy);
  289. static int remove_nodes(struct device *dev,
  290. struct list_head *first, struct list_head *end,
  291. struct list_head *todo)
  292. {
  293. int cnt = 0, nr_groups = 0;
  294. struct list_head *cur;
  295. /* First pass - move normal devres entries to @todo and clear
  296. * devres_group colors.
  297. */
  298. cur = first;
  299. while (cur != end) {
  300. struct devres_node *node;
  301. struct devres_group *grp;
  302. node = list_entry(cur, struct devres_node, entry);
  303. cur = cur->next;
  304. grp = node_to_group(node);
  305. if (grp) {
  306. /* clear color of group markers in the first pass */
  307. grp->color = 0;
  308. nr_groups++;
  309. } else {
  310. /* regular devres entry */
  311. if (&node->entry == first)
  312. first = first->next;
  313. list_move_tail(&node->entry, todo);
  314. cnt++;
  315. }
  316. }
  317. if (!nr_groups)
  318. return cnt;
  319. /* Second pass - Scan groups and color them. A group gets
  320. * color value of two iff the group is wholly contained in
  321. * [cur, end). That is, for a closed group, both opening and
  322. * closing markers should be in the range, while just the
  323. * opening marker is enough for an open group.
  324. */
  325. cur = first;
  326. while (cur != end) {
  327. struct devres_node *node;
  328. struct devres_group *grp;
  329. node = list_entry(cur, struct devres_node, entry);
  330. cur = cur->next;
  331. grp = node_to_group(node);
  332. BUG_ON(!grp || list_empty(&grp->node[0].entry));
  333. grp->color++;
  334. if (list_empty(&grp->node[1].entry))
  335. grp->color++;
  336. BUG_ON(grp->color <= 0 || grp->color > 2);
  337. if (grp->color == 2) {
  338. /* No need to update cur or end. The removed
  339. * nodes are always before both.
  340. */
  341. list_move_tail(&grp->node[0].entry, todo);
  342. list_del_init(&grp->node[1].entry);
  343. }
  344. }
  345. return cnt;
  346. }
  347. static int release_nodes(struct device *dev, struct list_head *first,
  348. struct list_head *end, unsigned long flags)
  349. {
  350. LIST_HEAD(todo);
  351. int cnt;
  352. struct devres *dr, *tmp;
  353. cnt = remove_nodes(dev, first, end, &todo);
  354. spin_unlock_irqrestore(&dev->devres_lock, flags);
  355. /* Release. Note that both devres and devres_group are
  356. * handled as devres in the following loop. This is safe.
  357. */
  358. list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
  359. devres_log(dev, &dr->node, "REL");
  360. dr->node.release(dev, dr->data);
  361. kfree(dr);
  362. }
  363. return cnt;
  364. }
  365. /**
  366. * devres_release_all - Release all managed resources
  367. * @dev: Device to release resources for
  368. *
  369. * Release all resources associated with @dev. This function is
  370. * called on driver detach.
  371. */
  372. int devres_release_all(struct device *dev)
  373. {
  374. unsigned long flags;
  375. spin_lock_irqsave(&dev->devres_lock, flags);
  376. return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
  377. flags);
  378. }
  379. /**
  380. * devres_open_group - Open a new devres group
  381. * @dev: Device to open devres group for
  382. * @id: Separator ID
  383. * @gfp: Allocation flags
  384. *
  385. * Open a new devres group for @dev with @id. For @id, using a
  386. * pointer to an object which won't be used for another group is
  387. * recommended. If @id is NULL, address-wise unique ID is created.
  388. *
  389. * RETURNS:
  390. * ID of the new group, NULL on failure.
  391. */
  392. void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
  393. {
  394. struct devres_group *grp;
  395. unsigned long flags;
  396. grp = kmalloc(sizeof(*grp), gfp);
  397. if (unlikely(!grp))
  398. return NULL;
  399. grp->node[0].release = &group_open_release;
  400. grp->node[1].release = &group_close_release;
  401. INIT_LIST_HEAD(&grp->node[0].entry);
  402. INIT_LIST_HEAD(&grp->node[1].entry);
  403. set_node_dbginfo(&grp->node[0], "grp<", 0);
  404. set_node_dbginfo(&grp->node[1], "grp>", 0);
  405. grp->id = grp;
  406. if (id)
  407. grp->id = id;
  408. spin_lock_irqsave(&dev->devres_lock, flags);
  409. add_dr(dev, &grp->node[0]);
  410. spin_unlock_irqrestore(&dev->devres_lock, flags);
  411. return grp->id;
  412. }
  413. EXPORT_SYMBOL_GPL(devres_open_group);
  414. /* Find devres group with ID @id. If @id is NULL, look for the latest. */
  415. static struct devres_group * find_group(struct device *dev, void *id)
  416. {
  417. struct devres_node *node;
  418. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  419. struct devres_group *grp;
  420. if (node->release != &group_open_release)
  421. continue;
  422. grp = container_of(node, struct devres_group, node[0]);
  423. if (id) {
  424. if (grp->id == id)
  425. return grp;
  426. } else if (list_empty(&grp->node[1].entry))
  427. return grp;
  428. }
  429. return NULL;
  430. }
  431. /**
  432. * devres_close_group - Close a devres group
  433. * @dev: Device to close devres group for
  434. * @id: ID of target group, can be NULL
  435. *
  436. * Close the group identified by @id. If @id is NULL, the latest open
  437. * group is selected.
  438. */
  439. void devres_close_group(struct device *dev, void *id)
  440. {
  441. struct devres_group *grp;
  442. unsigned long flags;
  443. spin_lock_irqsave(&dev->devres_lock, flags);
  444. grp = find_group(dev, id);
  445. if (grp)
  446. add_dr(dev, &grp->node[1]);
  447. else
  448. WARN_ON(1);
  449. spin_unlock_irqrestore(&dev->devres_lock, flags);
  450. }
  451. EXPORT_SYMBOL_GPL(devres_close_group);
  452. /**
  453. * devres_remove_group - Remove a devres group
  454. * @dev: Device to remove group for
  455. * @id: ID of target group, can be NULL
  456. *
  457. * Remove the group identified by @id. If @id is NULL, the latest
  458. * open group is selected. Note that removing a group doesn't affect
  459. * any other resources.
  460. */
  461. void devres_remove_group(struct device *dev, void *id)
  462. {
  463. struct devres_group *grp;
  464. unsigned long flags;
  465. spin_lock_irqsave(&dev->devres_lock, flags);
  466. grp = find_group(dev, id);
  467. if (grp) {
  468. list_del_init(&grp->node[0].entry);
  469. list_del_init(&grp->node[1].entry);
  470. devres_log(dev, &grp->node[0], "REM");
  471. } else
  472. WARN_ON(1);
  473. spin_unlock_irqrestore(&dev->devres_lock, flags);
  474. kfree(grp);
  475. }
  476. EXPORT_SYMBOL_GPL(devres_remove_group);
  477. /**
  478. * devres_release_group - Release resources in a devres group
  479. * @dev: Device to release group for
  480. * @id: ID of target group, can be NULL
  481. *
  482. * Release all resources in the group identified by @id. If @id is
  483. * NULL, the latest open group is selected. The selected group and
  484. * groups properly nested inside the selected group are removed.
  485. *
  486. * RETURNS:
  487. * The number of released non-group resources.
  488. */
  489. int devres_release_group(struct device *dev, void *id)
  490. {
  491. struct devres_group *grp;
  492. unsigned long flags;
  493. int cnt = 0;
  494. spin_lock_irqsave(&dev->devres_lock, flags);
  495. grp = find_group(dev, id);
  496. if (grp) {
  497. struct list_head *first = &grp->node[0].entry;
  498. struct list_head *end = &dev->devres_head;
  499. if (!list_empty(&grp->node[1].entry))
  500. end = grp->node[1].entry.next;
  501. cnt = release_nodes(dev, first, end, flags);
  502. } else {
  503. WARN_ON(1);
  504. spin_unlock_irqrestore(&dev->devres_lock, flags);
  505. }
  506. return cnt;
  507. }
  508. EXPORT_SYMBOL_GPL(devres_release_group);
  509. /*
  510. * Managed kzalloc/kfree
  511. */
  512. static void devm_kzalloc_release(struct device *dev, void *res)
  513. {
  514. /* noop */
  515. }
  516. static int devm_kzalloc_match(struct device *dev, void *res, void *data)
  517. {
  518. return res == data;
  519. }
  520. /**
  521. * devm_kzalloc - Resource-managed kzalloc
  522. * @dev: Device to allocate memory for
  523. * @size: Allocation size
  524. * @gfp: Allocation gfp flags
  525. *
  526. * Managed kzalloc. Memory allocated with this function is
  527. * automatically freed on driver detach. Like all other devres
  528. * resources, guaranteed alignment is unsigned long long.
  529. *
  530. * RETURNS:
  531. * Pointer to allocated memory on success, NULL on failure.
  532. */
  533. void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
  534. {
  535. struct devres *dr;
  536. /* use raw alloc_dr for kmalloc caller tracing */
  537. dr = alloc_dr(devm_kzalloc_release, size, gfp);
  538. if (unlikely(!dr))
  539. return NULL;
  540. set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
  541. devres_add(dev, dr->data);
  542. return dr->data;
  543. }
  544. EXPORT_SYMBOL_GPL(devm_kzalloc);
  545. /**
  546. * devm_kfree - Resource-managed kfree
  547. * @dev: Device this memory belongs to
  548. * @p: Memory to free
  549. *
  550. * Free memory allocated with dev_kzalloc().
  551. */
  552. void devm_kfree(struct device *dev, void *p)
  553. {
  554. int rc;
  555. rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
  556. WARN_ON(rc);
  557. }
  558. EXPORT_SYMBOL_GPL(devm_kfree);