devres.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/devres.c - device resource management
  4. *
  5. * Copyright (c) 2006 SUSE Linux Products GmbH
  6. * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/percpu.h>
  12. #include <asm/sections.h>
  13. #include "base.h"
  14. struct devres_node {
  15. struct list_head entry;
  16. dr_release_t release;
  17. #ifdef CONFIG_DEBUG_DEVRES
  18. const char *name;
  19. size_t size;
  20. #endif
  21. };
  22. struct devres {
  23. struct devres_node node;
  24. /*
  25. * Some archs want to perform DMA into kmalloc caches
  26. * and need a guaranteed alignment larger than
  27. * the alignment of a 64-bit integer.
  28. * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
  29. * buffer alignment as if it was allocated by plain kmalloc().
  30. */
  31. u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
  32. };
  33. struct devres_group {
  34. struct devres_node node[2];
  35. void *id;
  36. int color;
  37. /* -- 8 pointers */
  38. };
  39. #ifdef CONFIG_DEBUG_DEVRES
  40. static int log_devres = 0;
  41. module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
  42. static void set_node_dbginfo(struct devres_node *node, const char *name,
  43. size_t size)
  44. {
  45. node->name = name;
  46. node->size = size;
  47. }
  48. static void devres_log(struct device *dev, struct devres_node *node,
  49. const char *op)
  50. {
  51. if (unlikely(log_devres))
  52. dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
  53. op, node, node->name, (unsigned long)node->size);
  54. }
  55. #else /* CONFIG_DEBUG_DEVRES */
  56. #define set_node_dbginfo(node, n, s) do {} while (0)
  57. #define devres_log(dev, node, op) do {} while (0)
  58. #endif /* CONFIG_DEBUG_DEVRES */
  59. /*
  60. * Release functions for devres group. These callbacks are used only
  61. * for identification.
  62. */
  63. static void group_open_release(struct device *dev, void *res)
  64. {
  65. /* noop */
  66. }
  67. static void group_close_release(struct device *dev, void *res)
  68. {
  69. /* noop */
  70. }
  71. static struct devres_group * node_to_group(struct devres_node *node)
  72. {
  73. if (node->release == &group_open_release)
  74. return container_of(node, struct devres_group, node[0]);
  75. if (node->release == &group_close_release)
  76. return container_of(node, struct devres_group, node[1]);
  77. return NULL;
  78. }
  79. static __always_inline struct devres * alloc_dr(dr_release_t release,
  80. size_t size, gfp_t gfp, int nid)
  81. {
  82. size_t tot_size;
  83. struct devres *dr;
  84. /* We must catch any near-SIZE_MAX cases that could overflow. */
  85. if (unlikely(check_add_overflow(sizeof(struct devres), size,
  86. &tot_size)))
  87. return NULL;
  88. dr = kmalloc_node_track_caller(tot_size, gfp, nid);
  89. if (unlikely(!dr))
  90. return NULL;
  91. memset(dr, 0, offsetof(struct devres, data));
  92. INIT_LIST_HEAD(&dr->node.entry);
  93. dr->node.release = release;
  94. return dr;
  95. }
  96. static void add_dr(struct device *dev, struct devres_node *node)
  97. {
  98. devres_log(dev, node, "ADD");
  99. BUG_ON(!list_empty(&node->entry));
  100. list_add_tail(&node->entry, &dev->devres_head);
  101. }
  102. #ifdef CONFIG_DEBUG_DEVRES
  103. void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
  104. const char *name)
  105. {
  106. struct devres *dr;
  107. dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
  108. if (unlikely(!dr))
  109. return NULL;
  110. set_node_dbginfo(&dr->node, name, size);
  111. return dr->data;
  112. }
  113. EXPORT_SYMBOL_GPL(__devres_alloc_node);
  114. #else
  115. /**
  116. * devres_alloc - Allocate device resource data
  117. * @release: Release function devres will be associated with
  118. * @size: Allocation size
  119. * @gfp: Allocation flags
  120. * @nid: NUMA node
  121. *
  122. * Allocate devres of @size bytes. The allocated area is zeroed, then
  123. * associated with @release. The returned pointer can be passed to
  124. * other devres_*() functions.
  125. *
  126. * RETURNS:
  127. * Pointer to allocated devres on success, NULL on failure.
  128. */
  129. void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
  130. {
  131. struct devres *dr;
  132. dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
  133. if (unlikely(!dr))
  134. return NULL;
  135. return dr->data;
  136. }
  137. EXPORT_SYMBOL_GPL(devres_alloc_node);
  138. #endif
  139. /**
  140. * devres_for_each_res - Resource iterator
  141. * @dev: Device to iterate resource from
  142. * @release: Look for resources associated with this release function
  143. * @match: Match function (optional)
  144. * @match_data: Data for the match function
  145. * @fn: Function to be called for each matched resource.
  146. * @data: Data for @fn, the 3rd parameter of @fn
  147. *
  148. * Call @fn for each devres of @dev which is associated with @release
  149. * and for which @match returns 1.
  150. *
  151. * RETURNS:
  152. * void
  153. */
  154. void devres_for_each_res(struct device *dev, dr_release_t release,
  155. dr_match_t match, void *match_data,
  156. void (*fn)(struct device *, void *, void *),
  157. void *data)
  158. {
  159. struct devres_node *node;
  160. struct devres_node *tmp;
  161. unsigned long flags;
  162. if (!fn)
  163. return;
  164. spin_lock_irqsave(&dev->devres_lock, flags);
  165. list_for_each_entry_safe_reverse(node, tmp,
  166. &dev->devres_head, entry) {
  167. struct devres *dr = container_of(node, struct devres, node);
  168. if (node->release != release)
  169. continue;
  170. if (match && !match(dev, dr->data, match_data))
  171. continue;
  172. fn(dev, dr->data, data);
  173. }
  174. spin_unlock_irqrestore(&dev->devres_lock, flags);
  175. }
  176. EXPORT_SYMBOL_GPL(devres_for_each_res);
  177. /**
  178. * devres_free - Free device resource data
  179. * @res: Pointer to devres data to free
  180. *
  181. * Free devres created with devres_alloc().
  182. */
  183. void devres_free(void *res)
  184. {
  185. if (res) {
  186. struct devres *dr = container_of(res, struct devres, data);
  187. BUG_ON(!list_empty(&dr->node.entry));
  188. kfree(dr);
  189. }
  190. }
  191. EXPORT_SYMBOL_GPL(devres_free);
  192. /**
  193. * devres_add - Register device resource
  194. * @dev: Device to add resource to
  195. * @res: Resource to register
  196. *
  197. * Register devres @res to @dev. @res should have been allocated
  198. * using devres_alloc(). On driver detach, the associated release
  199. * function will be invoked and devres will be freed automatically.
  200. */
  201. void devres_add(struct device *dev, void *res)
  202. {
  203. struct devres *dr = container_of(res, struct devres, data);
  204. unsigned long flags;
  205. spin_lock_irqsave(&dev->devres_lock, flags);
  206. add_dr(dev, &dr->node);
  207. spin_unlock_irqrestore(&dev->devres_lock, flags);
  208. }
  209. EXPORT_SYMBOL_GPL(devres_add);
  210. static struct devres *find_dr(struct device *dev, dr_release_t release,
  211. dr_match_t match, void *match_data)
  212. {
  213. struct devres_node *node;
  214. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  215. struct devres *dr = container_of(node, struct devres, node);
  216. if (node->release != release)
  217. continue;
  218. if (match && !match(dev, dr->data, match_data))
  219. continue;
  220. return dr;
  221. }
  222. return NULL;
  223. }
  224. /**
  225. * devres_find - Find device resource
  226. * @dev: Device to lookup resource from
  227. * @release: Look for resources associated with this release function
  228. * @match: Match function (optional)
  229. * @match_data: Data for the match function
  230. *
  231. * Find the latest devres of @dev which is associated with @release
  232. * and for which @match returns 1. If @match is NULL, it's considered
  233. * to match all.
  234. *
  235. * RETURNS:
  236. * Pointer to found devres, NULL if not found.
  237. */
  238. void * devres_find(struct device *dev, dr_release_t release,
  239. dr_match_t match, void *match_data)
  240. {
  241. struct devres *dr;
  242. unsigned long flags;
  243. spin_lock_irqsave(&dev->devres_lock, flags);
  244. dr = find_dr(dev, release, match, match_data);
  245. spin_unlock_irqrestore(&dev->devres_lock, flags);
  246. if (dr)
  247. return dr->data;
  248. return NULL;
  249. }
  250. EXPORT_SYMBOL_GPL(devres_find);
  251. /**
  252. * devres_get - Find devres, if non-existent, add one atomically
  253. * @dev: Device to lookup or add devres for
  254. * @new_res: Pointer to new initialized devres to add if not found
  255. * @match: Match function (optional)
  256. * @match_data: Data for the match function
  257. *
  258. * Find the latest devres of @dev which has the same release function
  259. * as @new_res and for which @match return 1. If found, @new_res is
  260. * freed; otherwise, @new_res is added atomically.
  261. *
  262. * RETURNS:
  263. * Pointer to found or added devres.
  264. */
  265. void * devres_get(struct device *dev, void *new_res,
  266. dr_match_t match, void *match_data)
  267. {
  268. struct devres *new_dr = container_of(new_res, struct devres, data);
  269. struct devres *dr;
  270. unsigned long flags;
  271. spin_lock_irqsave(&dev->devres_lock, flags);
  272. dr = find_dr(dev, new_dr->node.release, match, match_data);
  273. if (!dr) {
  274. add_dr(dev, &new_dr->node);
  275. dr = new_dr;
  276. new_res = NULL;
  277. }
  278. spin_unlock_irqrestore(&dev->devres_lock, flags);
  279. devres_free(new_res);
  280. return dr->data;
  281. }
  282. EXPORT_SYMBOL_GPL(devres_get);
  283. /**
  284. * devres_remove - Find a device resource and remove it
  285. * @dev: Device to find resource from
  286. * @release: Look for resources associated with this release function
  287. * @match: Match function (optional)
  288. * @match_data: Data for the match function
  289. *
  290. * Find the latest devres of @dev associated with @release and for
  291. * which @match returns 1. If @match is NULL, it's considered to
  292. * match all. If found, the resource is removed atomically and
  293. * returned.
  294. *
  295. * RETURNS:
  296. * Pointer to removed devres on success, NULL if not found.
  297. */
  298. void * devres_remove(struct device *dev, dr_release_t release,
  299. dr_match_t match, void *match_data)
  300. {
  301. struct devres *dr;
  302. unsigned long flags;
  303. spin_lock_irqsave(&dev->devres_lock, flags);
  304. dr = find_dr(dev, release, match, match_data);
  305. if (dr) {
  306. list_del_init(&dr->node.entry);
  307. devres_log(dev, &dr->node, "REM");
  308. }
  309. spin_unlock_irqrestore(&dev->devres_lock, flags);
  310. if (dr)
  311. return dr->data;
  312. return NULL;
  313. }
  314. EXPORT_SYMBOL_GPL(devres_remove);
  315. /**
  316. * devres_destroy - Find a device resource and destroy it
  317. * @dev: Device to find resource from
  318. * @release: Look for resources associated with this release function
  319. * @match: Match function (optional)
  320. * @match_data: Data for the match function
  321. *
  322. * Find the latest devres of @dev associated with @release and for
  323. * which @match returns 1. If @match is NULL, it's considered to
  324. * match all. If found, the resource is removed atomically and freed.
  325. *
  326. * Note that the release function for the resource will not be called,
  327. * only the devres-allocated data will be freed. The caller becomes
  328. * responsible for freeing any other data.
  329. *
  330. * RETURNS:
  331. * 0 if devres is found and freed, -ENOENT if not found.
  332. */
  333. int devres_destroy(struct device *dev, dr_release_t release,
  334. dr_match_t match, void *match_data)
  335. {
  336. void *res;
  337. res = devres_remove(dev, release, match, match_data);
  338. if (unlikely(!res))
  339. return -ENOENT;
  340. devres_free(res);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL_GPL(devres_destroy);
  344. /**
  345. * devres_release - Find a device resource and destroy it, calling release
  346. * @dev: Device to find resource from
  347. * @release: Look for resources associated with this release function
  348. * @match: Match function (optional)
  349. * @match_data: Data for the match function
  350. *
  351. * Find the latest devres of @dev associated with @release and for
  352. * which @match returns 1. If @match is NULL, it's considered to
  353. * match all. If found, the resource is removed atomically, the
  354. * release function called and the resource freed.
  355. *
  356. * RETURNS:
  357. * 0 if devres is found and freed, -ENOENT if not found.
  358. */
  359. int devres_release(struct device *dev, dr_release_t release,
  360. dr_match_t match, void *match_data)
  361. {
  362. void *res;
  363. res = devres_remove(dev, release, match, match_data);
  364. if (unlikely(!res))
  365. return -ENOENT;
  366. (*release)(dev, res);
  367. devres_free(res);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(devres_release);
  371. static int remove_nodes(struct device *dev,
  372. struct list_head *first, struct list_head *end,
  373. struct list_head *todo)
  374. {
  375. int cnt = 0, nr_groups = 0;
  376. struct list_head *cur;
  377. /* First pass - move normal devres entries to @todo and clear
  378. * devres_group colors.
  379. */
  380. cur = first;
  381. while (cur != end) {
  382. struct devres_node *node;
  383. struct devres_group *grp;
  384. node = list_entry(cur, struct devres_node, entry);
  385. cur = cur->next;
  386. grp = node_to_group(node);
  387. if (grp) {
  388. /* clear color of group markers in the first pass */
  389. grp->color = 0;
  390. nr_groups++;
  391. } else {
  392. /* regular devres entry */
  393. if (&node->entry == first)
  394. first = first->next;
  395. list_move_tail(&node->entry, todo);
  396. cnt++;
  397. }
  398. }
  399. if (!nr_groups)
  400. return cnt;
  401. /* Second pass - Scan groups and color them. A group gets
  402. * color value of two iff the group is wholly contained in
  403. * [cur, end). That is, for a closed group, both opening and
  404. * closing markers should be in the range, while just the
  405. * opening marker is enough for an open group.
  406. */
  407. cur = first;
  408. while (cur != end) {
  409. struct devres_node *node;
  410. struct devres_group *grp;
  411. node = list_entry(cur, struct devres_node, entry);
  412. cur = cur->next;
  413. grp = node_to_group(node);
  414. BUG_ON(!grp || list_empty(&grp->node[0].entry));
  415. grp->color++;
  416. if (list_empty(&grp->node[1].entry))
  417. grp->color++;
  418. BUG_ON(grp->color <= 0 || grp->color > 2);
  419. if (grp->color == 2) {
  420. /* No need to update cur or end. The removed
  421. * nodes are always before both.
  422. */
  423. list_move_tail(&grp->node[0].entry, todo);
  424. list_del_init(&grp->node[1].entry);
  425. }
  426. }
  427. return cnt;
  428. }
  429. static int release_nodes(struct device *dev, struct list_head *first,
  430. struct list_head *end, unsigned long flags)
  431. __releases(&dev->devres_lock)
  432. {
  433. LIST_HEAD(todo);
  434. int cnt;
  435. struct devres *dr, *tmp;
  436. cnt = remove_nodes(dev, first, end, &todo);
  437. spin_unlock_irqrestore(&dev->devres_lock, flags);
  438. /* Release. Note that both devres and devres_group are
  439. * handled as devres in the following loop. This is safe.
  440. */
  441. list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
  442. devres_log(dev, &dr->node, "REL");
  443. dr->node.release(dev, dr->data);
  444. kfree(dr);
  445. }
  446. return cnt;
  447. }
  448. /**
  449. * devres_release_all - Release all managed resources
  450. * @dev: Device to release resources for
  451. *
  452. * Release all resources associated with @dev. This function is
  453. * called on driver detach.
  454. */
  455. int devres_release_all(struct device *dev)
  456. {
  457. unsigned long flags;
  458. /* Looks like an uninitialized device structure */
  459. if (WARN_ON(dev->devres_head.next == NULL))
  460. return -ENODEV;
  461. spin_lock_irqsave(&dev->devres_lock, flags);
  462. return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
  463. flags);
  464. }
  465. /**
  466. * devres_open_group - Open a new devres group
  467. * @dev: Device to open devres group for
  468. * @id: Separator ID
  469. * @gfp: Allocation flags
  470. *
  471. * Open a new devres group for @dev with @id. For @id, using a
  472. * pointer to an object which won't be used for another group is
  473. * recommended. If @id is NULL, address-wise unique ID is created.
  474. *
  475. * RETURNS:
  476. * ID of the new group, NULL on failure.
  477. */
  478. void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
  479. {
  480. struct devres_group *grp;
  481. unsigned long flags;
  482. grp = kmalloc(sizeof(*grp), gfp);
  483. if (unlikely(!grp))
  484. return NULL;
  485. grp->node[0].release = &group_open_release;
  486. grp->node[1].release = &group_close_release;
  487. INIT_LIST_HEAD(&grp->node[0].entry);
  488. INIT_LIST_HEAD(&grp->node[1].entry);
  489. set_node_dbginfo(&grp->node[0], "grp<", 0);
  490. set_node_dbginfo(&grp->node[1], "grp>", 0);
  491. grp->id = grp;
  492. if (id)
  493. grp->id = id;
  494. spin_lock_irqsave(&dev->devres_lock, flags);
  495. add_dr(dev, &grp->node[0]);
  496. spin_unlock_irqrestore(&dev->devres_lock, flags);
  497. return grp->id;
  498. }
  499. EXPORT_SYMBOL_GPL(devres_open_group);
  500. /* Find devres group with ID @id. If @id is NULL, look for the latest. */
  501. static struct devres_group * find_group(struct device *dev, void *id)
  502. {
  503. struct devres_node *node;
  504. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  505. struct devres_group *grp;
  506. if (node->release != &group_open_release)
  507. continue;
  508. grp = container_of(node, struct devres_group, node[0]);
  509. if (id) {
  510. if (grp->id == id)
  511. return grp;
  512. } else if (list_empty(&grp->node[1].entry))
  513. return grp;
  514. }
  515. return NULL;
  516. }
  517. /**
  518. * devres_close_group - Close a devres group
  519. * @dev: Device to close devres group for
  520. * @id: ID of target group, can be NULL
  521. *
  522. * Close the group identified by @id. If @id is NULL, the latest open
  523. * group is selected.
  524. */
  525. void devres_close_group(struct device *dev, void *id)
  526. {
  527. struct devres_group *grp;
  528. unsigned long flags;
  529. spin_lock_irqsave(&dev->devres_lock, flags);
  530. grp = find_group(dev, id);
  531. if (grp)
  532. add_dr(dev, &grp->node[1]);
  533. else
  534. WARN_ON(1);
  535. spin_unlock_irqrestore(&dev->devres_lock, flags);
  536. }
  537. EXPORT_SYMBOL_GPL(devres_close_group);
  538. /**
  539. * devres_remove_group - Remove a devres group
  540. * @dev: Device to remove group for
  541. * @id: ID of target group, can be NULL
  542. *
  543. * Remove the group identified by @id. If @id is NULL, the latest
  544. * open group is selected. Note that removing a group doesn't affect
  545. * any other resources.
  546. */
  547. void devres_remove_group(struct device *dev, void *id)
  548. {
  549. struct devres_group *grp;
  550. unsigned long flags;
  551. spin_lock_irqsave(&dev->devres_lock, flags);
  552. grp = find_group(dev, id);
  553. if (grp) {
  554. list_del_init(&grp->node[0].entry);
  555. list_del_init(&grp->node[1].entry);
  556. devres_log(dev, &grp->node[0], "REM");
  557. } else
  558. WARN_ON(1);
  559. spin_unlock_irqrestore(&dev->devres_lock, flags);
  560. kfree(grp);
  561. }
  562. EXPORT_SYMBOL_GPL(devres_remove_group);
  563. /**
  564. * devres_release_group - Release resources in a devres group
  565. * @dev: Device to release group for
  566. * @id: ID of target group, can be NULL
  567. *
  568. * Release all resources in the group identified by @id. If @id is
  569. * NULL, the latest open group is selected. The selected group and
  570. * groups properly nested inside the selected group are removed.
  571. *
  572. * RETURNS:
  573. * The number of released non-group resources.
  574. */
  575. int devres_release_group(struct device *dev, void *id)
  576. {
  577. struct devres_group *grp;
  578. unsigned long flags;
  579. int cnt = 0;
  580. spin_lock_irqsave(&dev->devres_lock, flags);
  581. grp = find_group(dev, id);
  582. if (grp) {
  583. struct list_head *first = &grp->node[0].entry;
  584. struct list_head *end = &dev->devres_head;
  585. if (!list_empty(&grp->node[1].entry))
  586. end = grp->node[1].entry.next;
  587. cnt = release_nodes(dev, first, end, flags);
  588. } else {
  589. WARN_ON(1);
  590. spin_unlock_irqrestore(&dev->devres_lock, flags);
  591. }
  592. return cnt;
  593. }
  594. EXPORT_SYMBOL_GPL(devres_release_group);
  595. /*
  596. * Custom devres actions allow inserting a simple function call
  597. * into the teadown sequence.
  598. */
  599. struct action_devres {
  600. void *data;
  601. void (*action)(void *);
  602. };
  603. static int devm_action_match(struct device *dev, void *res, void *p)
  604. {
  605. struct action_devres *devres = res;
  606. struct action_devres *target = p;
  607. return devres->action == target->action &&
  608. devres->data == target->data;
  609. }
  610. static void devm_action_release(struct device *dev, void *res)
  611. {
  612. struct action_devres *devres = res;
  613. devres->action(devres->data);
  614. }
  615. /**
  616. * devm_add_action() - add a custom action to list of managed resources
  617. * @dev: Device that owns the action
  618. * @action: Function that should be called
  619. * @data: Pointer to data passed to @action implementation
  620. *
  621. * This adds a custom action to the list of managed resources so that
  622. * it gets executed as part of standard resource unwinding.
  623. */
  624. int devm_add_action(struct device *dev, void (*action)(void *), void *data)
  625. {
  626. struct action_devres *devres;
  627. devres = devres_alloc(devm_action_release,
  628. sizeof(struct action_devres), GFP_KERNEL);
  629. if (!devres)
  630. return -ENOMEM;
  631. devres->data = data;
  632. devres->action = action;
  633. devres_add(dev, devres);
  634. return 0;
  635. }
  636. EXPORT_SYMBOL_GPL(devm_add_action);
  637. /**
  638. * devm_remove_action() - removes previously added custom action
  639. * @dev: Device that owns the action
  640. * @action: Function implementing the action
  641. * @data: Pointer to data passed to @action implementation
  642. *
  643. * Removes instance of @action previously added by devm_add_action().
  644. * Both action and data should match one of the existing entries.
  645. */
  646. void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
  647. {
  648. struct action_devres devres = {
  649. .data = data,
  650. .action = action,
  651. };
  652. WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
  653. &devres));
  654. }
  655. EXPORT_SYMBOL_GPL(devm_remove_action);
  656. /*
  657. * Managed kmalloc/kfree
  658. */
  659. static void devm_kmalloc_release(struct device *dev, void *res)
  660. {
  661. /* noop */
  662. }
  663. static int devm_kmalloc_match(struct device *dev, void *res, void *data)
  664. {
  665. return res == data;
  666. }
  667. /**
  668. * devm_kmalloc - Resource-managed kmalloc
  669. * @dev: Device to allocate memory for
  670. * @size: Allocation size
  671. * @gfp: Allocation gfp flags
  672. *
  673. * Managed kmalloc. Memory allocated with this function is
  674. * automatically freed on driver detach. Like all other devres
  675. * resources, guaranteed alignment is unsigned long long.
  676. *
  677. * RETURNS:
  678. * Pointer to allocated memory on success, NULL on failure.
  679. */
  680. void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
  681. {
  682. struct devres *dr;
  683. /* use raw alloc_dr for kmalloc caller tracing */
  684. dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
  685. if (unlikely(!dr))
  686. return NULL;
  687. /*
  688. * This is named devm_kzalloc_release for historical reasons
  689. * The initial implementation did not support kmalloc, only kzalloc
  690. */
  691. set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
  692. devres_add(dev, dr->data);
  693. return dr->data;
  694. }
  695. EXPORT_SYMBOL_GPL(devm_kmalloc);
  696. /**
  697. * devm_kstrdup - Allocate resource managed space and
  698. * copy an existing string into that.
  699. * @dev: Device to allocate memory for
  700. * @s: the string to duplicate
  701. * @gfp: the GFP mask used in the devm_kmalloc() call when
  702. * allocating memory
  703. * RETURNS:
  704. * Pointer to allocated string on success, NULL on failure.
  705. */
  706. char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
  707. {
  708. size_t size;
  709. char *buf;
  710. if (!s)
  711. return NULL;
  712. size = strlen(s) + 1;
  713. buf = devm_kmalloc(dev, size, gfp);
  714. if (buf)
  715. memcpy(buf, s, size);
  716. return buf;
  717. }
  718. EXPORT_SYMBOL_GPL(devm_kstrdup);
  719. /**
  720. * devm_kstrdup_const - resource managed conditional string duplication
  721. * @dev: device for which to duplicate the string
  722. * @s: the string to duplicate
  723. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  724. *
  725. * Strings allocated by devm_kstrdup_const will be automatically freed when
  726. * the associated device is detached.
  727. *
  728. * RETURNS:
  729. * Source string if it is in .rodata section otherwise it falls back to
  730. * devm_kstrdup.
  731. */
  732. const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
  733. {
  734. if (is_kernel_rodata((unsigned long)s))
  735. return s;
  736. return devm_kstrdup(dev, s, gfp);
  737. }
  738. EXPORT_SYMBOL_GPL(devm_kstrdup_const);
  739. /**
  740. * devm_kvasprintf - Allocate resource managed space and format a string
  741. * into that.
  742. * @dev: Device to allocate memory for
  743. * @gfp: the GFP mask used in the devm_kmalloc() call when
  744. * allocating memory
  745. * @fmt: The printf()-style format string
  746. * @ap: Arguments for the format string
  747. * RETURNS:
  748. * Pointer to allocated string on success, NULL on failure.
  749. */
  750. char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
  751. va_list ap)
  752. {
  753. unsigned int len;
  754. char *p;
  755. va_list aq;
  756. va_copy(aq, ap);
  757. len = vsnprintf(NULL, 0, fmt, aq);
  758. va_end(aq);
  759. p = devm_kmalloc(dev, len+1, gfp);
  760. if (!p)
  761. return NULL;
  762. vsnprintf(p, len+1, fmt, ap);
  763. return p;
  764. }
  765. EXPORT_SYMBOL(devm_kvasprintf);
  766. /**
  767. * devm_kasprintf - Allocate resource managed space and format a string
  768. * into that.
  769. * @dev: Device to allocate memory for
  770. * @gfp: the GFP mask used in the devm_kmalloc() call when
  771. * allocating memory
  772. * @fmt: The printf()-style format string
  773. * @...: Arguments for the format string
  774. * RETURNS:
  775. * Pointer to allocated string on success, NULL on failure.
  776. */
  777. char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
  778. {
  779. va_list ap;
  780. char *p;
  781. va_start(ap, fmt);
  782. p = devm_kvasprintf(dev, gfp, fmt, ap);
  783. va_end(ap);
  784. return p;
  785. }
  786. EXPORT_SYMBOL_GPL(devm_kasprintf);
  787. /**
  788. * devm_kfree - Resource-managed kfree
  789. * @dev: Device this memory belongs to
  790. * @p: Memory to free
  791. *
  792. * Free memory allocated with devm_kmalloc().
  793. */
  794. void devm_kfree(struct device *dev, const void *p)
  795. {
  796. int rc;
  797. /*
  798. * Special case: pointer to a string in .rodata returned by
  799. * devm_kstrdup_const().
  800. */
  801. if (unlikely(is_kernel_rodata((unsigned long)p)))
  802. return;
  803. rc = devres_destroy(dev, devm_kmalloc_release,
  804. devm_kmalloc_match, (void *)p);
  805. WARN_ON(rc);
  806. }
  807. EXPORT_SYMBOL_GPL(devm_kfree);
  808. /**
  809. * devm_kmemdup - Resource-managed kmemdup
  810. * @dev: Device this memory belongs to
  811. * @src: Memory region to duplicate
  812. * @len: Memory region length
  813. * @gfp: GFP mask to use
  814. *
  815. * Duplicate region of a memory using resource managed kmalloc
  816. */
  817. void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
  818. {
  819. void *p;
  820. p = devm_kmalloc(dev, len, gfp);
  821. if (p)
  822. memcpy(p, src, len);
  823. return p;
  824. }
  825. EXPORT_SYMBOL_GPL(devm_kmemdup);
  826. struct pages_devres {
  827. unsigned long addr;
  828. unsigned int order;
  829. };
  830. static int devm_pages_match(struct device *dev, void *res, void *p)
  831. {
  832. struct pages_devres *devres = res;
  833. struct pages_devres *target = p;
  834. return devres->addr == target->addr;
  835. }
  836. static void devm_pages_release(struct device *dev, void *res)
  837. {
  838. struct pages_devres *devres = res;
  839. free_pages(devres->addr, devres->order);
  840. }
  841. /**
  842. * devm_get_free_pages - Resource-managed __get_free_pages
  843. * @dev: Device to allocate memory for
  844. * @gfp_mask: Allocation gfp flags
  845. * @order: Allocation size is (1 << order) pages
  846. *
  847. * Managed get_free_pages. Memory allocated with this function is
  848. * automatically freed on driver detach.
  849. *
  850. * RETURNS:
  851. * Address of allocated memory on success, 0 on failure.
  852. */
  853. unsigned long devm_get_free_pages(struct device *dev,
  854. gfp_t gfp_mask, unsigned int order)
  855. {
  856. struct pages_devres *devres;
  857. unsigned long addr;
  858. addr = __get_free_pages(gfp_mask, order);
  859. if (unlikely(!addr))
  860. return 0;
  861. devres = devres_alloc(devm_pages_release,
  862. sizeof(struct pages_devres), GFP_KERNEL);
  863. if (unlikely(!devres)) {
  864. free_pages(addr, order);
  865. return 0;
  866. }
  867. devres->addr = addr;
  868. devres->order = order;
  869. devres_add(dev, devres);
  870. return addr;
  871. }
  872. EXPORT_SYMBOL_GPL(devm_get_free_pages);
  873. /**
  874. * devm_free_pages - Resource-managed free_pages
  875. * @dev: Device this memory belongs to
  876. * @addr: Memory to free
  877. *
  878. * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
  879. * there is no need to supply the @order.
  880. */
  881. void devm_free_pages(struct device *dev, unsigned long addr)
  882. {
  883. struct pages_devres devres = { .addr = addr };
  884. WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
  885. &devres));
  886. }
  887. EXPORT_SYMBOL_GPL(devm_free_pages);
  888. static void devm_percpu_release(struct device *dev, void *pdata)
  889. {
  890. void __percpu *p;
  891. p = *(void __percpu **)pdata;
  892. free_percpu(p);
  893. }
  894. static int devm_percpu_match(struct device *dev, void *data, void *p)
  895. {
  896. struct devres *devr = container_of(data, struct devres, data);
  897. return *(void **)devr->data == p;
  898. }
  899. /**
  900. * __devm_alloc_percpu - Resource-managed alloc_percpu
  901. * @dev: Device to allocate per-cpu memory for
  902. * @size: Size of per-cpu memory to allocate
  903. * @align: Alignment of per-cpu memory to allocate
  904. *
  905. * Managed alloc_percpu. Per-cpu memory allocated with this function is
  906. * automatically freed on driver detach.
  907. *
  908. * RETURNS:
  909. * Pointer to allocated memory on success, NULL on failure.
  910. */
  911. void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  912. size_t align)
  913. {
  914. void *p;
  915. void __percpu *pcpu;
  916. pcpu = __alloc_percpu(size, align);
  917. if (!pcpu)
  918. return NULL;
  919. p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
  920. if (!p) {
  921. free_percpu(pcpu);
  922. return NULL;
  923. }
  924. *(void __percpu **)p = pcpu;
  925. devres_add(dev, p);
  926. return pcpu;
  927. }
  928. EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
  929. /**
  930. * devm_free_percpu - Resource-managed free_percpu
  931. * @dev: Device this memory belongs to
  932. * @pdata: Per-cpu memory to free
  933. *
  934. * Free memory allocated with devm_alloc_percpu().
  935. */
  936. void devm_free_percpu(struct device *dev, void __percpu *pdata)
  937. {
  938. WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
  939. (void *)pdata));
  940. }
  941. EXPORT_SYMBOL_GPL(devm_free_percpu);