devres.c 31 KB

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