devres.c 32 KB

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