devres.c 31 KB

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