devres.c 16 KB

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