devres.c 16 KB

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