devres.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. /* Looks like an uninitialized device structure */
  376. if (WARN_ON(dev->devres_head.next == NULL))
  377. return -ENODEV;
  378. spin_lock_irqsave(&dev->devres_lock, flags);
  379. return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
  380. flags);
  381. }
  382. /**
  383. * devres_open_group - Open a new devres group
  384. * @dev: Device to open devres group for
  385. * @id: Separator ID
  386. * @gfp: Allocation flags
  387. *
  388. * Open a new devres group for @dev with @id. For @id, using a
  389. * pointer to an object which won't be used for another group is
  390. * recommended. If @id is NULL, address-wise unique ID is created.
  391. *
  392. * RETURNS:
  393. * ID of the new group, NULL on failure.
  394. */
  395. void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
  396. {
  397. struct devres_group *grp;
  398. unsigned long flags;
  399. grp = kmalloc(sizeof(*grp), gfp);
  400. if (unlikely(!grp))
  401. return NULL;
  402. grp->node[0].release = &group_open_release;
  403. grp->node[1].release = &group_close_release;
  404. INIT_LIST_HEAD(&grp->node[0].entry);
  405. INIT_LIST_HEAD(&grp->node[1].entry);
  406. set_node_dbginfo(&grp->node[0], "grp<", 0);
  407. set_node_dbginfo(&grp->node[1], "grp>", 0);
  408. grp->id = grp;
  409. if (id)
  410. grp->id = id;
  411. spin_lock_irqsave(&dev->devres_lock, flags);
  412. add_dr(dev, &grp->node[0]);
  413. spin_unlock_irqrestore(&dev->devres_lock, flags);
  414. return grp->id;
  415. }
  416. EXPORT_SYMBOL_GPL(devres_open_group);
  417. /* Find devres group with ID @id. If @id is NULL, look for the latest. */
  418. static struct devres_group * find_group(struct device *dev, void *id)
  419. {
  420. struct devres_node *node;
  421. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  422. struct devres_group *grp;
  423. if (node->release != &group_open_release)
  424. continue;
  425. grp = container_of(node, struct devres_group, node[0]);
  426. if (id) {
  427. if (grp->id == id)
  428. return grp;
  429. } else if (list_empty(&grp->node[1].entry))
  430. return grp;
  431. }
  432. return NULL;
  433. }
  434. /**
  435. * devres_close_group - Close a devres group
  436. * @dev: Device to close devres group for
  437. * @id: ID of target group, can be NULL
  438. *
  439. * Close the group identified by @id. If @id is NULL, the latest open
  440. * group is selected.
  441. */
  442. void devres_close_group(struct device *dev, void *id)
  443. {
  444. struct devres_group *grp;
  445. unsigned long flags;
  446. spin_lock_irqsave(&dev->devres_lock, flags);
  447. grp = find_group(dev, id);
  448. if (grp)
  449. add_dr(dev, &grp->node[1]);
  450. else
  451. WARN_ON(1);
  452. spin_unlock_irqrestore(&dev->devres_lock, flags);
  453. }
  454. EXPORT_SYMBOL_GPL(devres_close_group);
  455. /**
  456. * devres_remove_group - Remove a devres group
  457. * @dev: Device to remove group for
  458. * @id: ID of target group, can be NULL
  459. *
  460. * Remove the group identified by @id. If @id is NULL, the latest
  461. * open group is selected. Note that removing a group doesn't affect
  462. * any other resources.
  463. */
  464. void devres_remove_group(struct device *dev, void *id)
  465. {
  466. struct devres_group *grp;
  467. unsigned long flags;
  468. spin_lock_irqsave(&dev->devres_lock, flags);
  469. grp = find_group(dev, id);
  470. if (grp) {
  471. list_del_init(&grp->node[0].entry);
  472. list_del_init(&grp->node[1].entry);
  473. devres_log(dev, &grp->node[0], "REM");
  474. } else
  475. WARN_ON(1);
  476. spin_unlock_irqrestore(&dev->devres_lock, flags);
  477. kfree(grp);
  478. }
  479. EXPORT_SYMBOL_GPL(devres_remove_group);
  480. /**
  481. * devres_release_group - Release resources in a devres group
  482. * @dev: Device to release group for
  483. * @id: ID of target group, can be NULL
  484. *
  485. * Release all resources in the group identified by @id. If @id is
  486. * NULL, the latest open group is selected. The selected group and
  487. * groups properly nested inside the selected group are removed.
  488. *
  489. * RETURNS:
  490. * The number of released non-group resources.
  491. */
  492. int devres_release_group(struct device *dev, void *id)
  493. {
  494. struct devres_group *grp;
  495. unsigned long flags;
  496. int cnt = 0;
  497. spin_lock_irqsave(&dev->devres_lock, flags);
  498. grp = find_group(dev, id);
  499. if (grp) {
  500. struct list_head *first = &grp->node[0].entry;
  501. struct list_head *end = &dev->devres_head;
  502. if (!list_empty(&grp->node[1].entry))
  503. end = grp->node[1].entry.next;
  504. cnt = release_nodes(dev, first, end, flags);
  505. } else {
  506. WARN_ON(1);
  507. spin_unlock_irqrestore(&dev->devres_lock, flags);
  508. }
  509. return cnt;
  510. }
  511. EXPORT_SYMBOL_GPL(devres_release_group);
  512. /*
  513. * Managed kzalloc/kfree
  514. */
  515. static void devm_kzalloc_release(struct device *dev, void *res)
  516. {
  517. /* noop */
  518. }
  519. static int devm_kzalloc_match(struct device *dev, void *res, void *data)
  520. {
  521. return res == data;
  522. }
  523. /**
  524. * devm_kzalloc - Resource-managed kzalloc
  525. * @dev: Device to allocate memory for
  526. * @size: Allocation size
  527. * @gfp: Allocation gfp flags
  528. *
  529. * Managed kzalloc. Memory allocated with this function is
  530. * automatically freed on driver detach. Like all other devres
  531. * resources, guaranteed alignment is unsigned long long.
  532. *
  533. * RETURNS:
  534. * Pointer to allocated memory on success, NULL on failure.
  535. */
  536. void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
  537. {
  538. struct devres *dr;
  539. /* use raw alloc_dr for kmalloc caller tracing */
  540. dr = alloc_dr(devm_kzalloc_release, size, gfp);
  541. if (unlikely(!dr))
  542. return NULL;
  543. set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
  544. devres_add(dev, dr->data);
  545. return dr->data;
  546. }
  547. EXPORT_SYMBOL_GPL(devm_kzalloc);
  548. /**
  549. * devm_kfree - Resource-managed kfree
  550. * @dev: Device this memory belongs to
  551. * @p: Memory to free
  552. *
  553. * Free memory allocated with dev_kzalloc().
  554. */
  555. void devm_kfree(struct device *dev, void *p)
  556. {
  557. int rc;
  558. rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
  559. WARN_ON(rc);
  560. }
  561. EXPORT_SYMBOL_GPL(devm_kfree);