devres.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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_obj(*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. /*
  506. * Find devres group with ID @id. If @id is NULL, look for the latest open
  507. * group.
  508. */
  509. static struct devres_group *find_group(struct device *dev, void *id)
  510. {
  511. struct devres_node *node;
  512. list_for_each_entry_reverse(node, &dev->devres_head, entry) {
  513. struct devres_group *grp;
  514. if (node->release != &group_open_release)
  515. continue;
  516. grp = container_of(node, struct devres_group, node[0]);
  517. if (id) {
  518. if (grp->id == id)
  519. return grp;
  520. } else if (list_empty(&grp->node[1].entry))
  521. return grp;
  522. }
  523. return NULL;
  524. }
  525. /**
  526. * devres_close_group - Close a devres group
  527. * @dev: Device to close devres group for
  528. * @id: ID of target group, can be NULL
  529. *
  530. * Close the group identified by @id. If @id is NULL, the latest open
  531. * group is selected.
  532. */
  533. void devres_close_group(struct device *dev, void *id)
  534. {
  535. struct devres_group *grp;
  536. unsigned long flags;
  537. spin_lock_irqsave(&dev->devres_lock, flags);
  538. grp = find_group(dev, id);
  539. if (grp)
  540. add_dr(dev, &grp->node[1]);
  541. else
  542. WARN_ON(1);
  543. spin_unlock_irqrestore(&dev->devres_lock, flags);
  544. }
  545. EXPORT_SYMBOL_GPL(devres_close_group);
  546. /**
  547. * devres_remove_group - Remove a devres group
  548. * @dev: Device to remove group for
  549. * @id: ID of target group, can be NULL
  550. *
  551. * Remove the group identified by @id. If @id is NULL, the latest
  552. * open group is selected. Note that removing a group doesn't affect
  553. * any other resources.
  554. */
  555. void devres_remove_group(struct device *dev, void *id)
  556. {
  557. struct devres_group *grp;
  558. unsigned long flags;
  559. spin_lock_irqsave(&dev->devres_lock, flags);
  560. grp = find_group(dev, id);
  561. if (grp) {
  562. list_del_init(&grp->node[0].entry);
  563. list_del_init(&grp->node[1].entry);
  564. devres_log(dev, &grp->node[0], "REM");
  565. } else
  566. WARN_ON(1);
  567. spin_unlock_irqrestore(&dev->devres_lock, flags);
  568. kfree(grp);
  569. }
  570. EXPORT_SYMBOL_GPL(devres_remove_group);
  571. /**
  572. * devres_release_group - Release resources in a devres group
  573. * @dev: Device to release group for
  574. * @id: ID of target group, can be NULL
  575. *
  576. * Release all resources in the group identified by @id. If @id is
  577. * NULL, the latest open group is selected. The selected group and
  578. * groups properly nested inside the selected group are removed.
  579. *
  580. * RETURNS:
  581. * The number of released non-group resources.
  582. */
  583. int devres_release_group(struct device *dev, void *id)
  584. {
  585. struct devres_group *grp;
  586. unsigned long flags;
  587. LIST_HEAD(todo);
  588. int cnt = 0;
  589. spin_lock_irqsave(&dev->devres_lock, flags);
  590. grp = find_group(dev, id);
  591. if (grp) {
  592. struct list_head *first = &grp->node[0].entry;
  593. struct list_head *end = &dev->devres_head;
  594. if (!list_empty(&grp->node[1].entry))
  595. end = grp->node[1].entry.next;
  596. cnt = remove_nodes(dev, first, end, &todo);
  597. spin_unlock_irqrestore(&dev->devres_lock, flags);
  598. release_nodes(dev, &todo);
  599. } else if (list_empty(&dev->devres_head)) {
  600. /*
  601. * dev is probably dying via devres_release_all(): groups
  602. * have already been removed and are on the process of
  603. * being released - don't touch and don't warn.
  604. */
  605. spin_unlock_irqrestore(&dev->devres_lock, flags);
  606. } else {
  607. WARN_ON(1);
  608. spin_unlock_irqrestore(&dev->devres_lock, flags);
  609. }
  610. return cnt;
  611. }
  612. EXPORT_SYMBOL_GPL(devres_release_group);
  613. /*
  614. * Custom devres actions allow inserting a simple function call
  615. * into the teardown sequence.
  616. */
  617. struct action_devres {
  618. void *data;
  619. void (*action)(void *);
  620. };
  621. static int devm_action_match(struct device *dev, void *res, void *p)
  622. {
  623. struct action_devres *devres = res;
  624. struct action_devres *target = p;
  625. return devres->action == target->action &&
  626. devres->data == target->data;
  627. }
  628. static void devm_action_release(struct device *dev, void *res)
  629. {
  630. struct action_devres *devres = res;
  631. devres->action(devres->data);
  632. }
  633. /**
  634. * __devm_add_action() - add a custom action to list of managed resources
  635. * @dev: Device that owns the action
  636. * @action: Function that should be called
  637. * @data: Pointer to data passed to @action implementation
  638. * @name: Name of the resource (for debugging purposes)
  639. *
  640. * This adds a custom action to the list of managed resources so that
  641. * it gets executed as part of standard resource unwinding.
  642. */
  643. int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name)
  644. {
  645. struct action_devres *devres;
  646. devres = __devres_alloc_node(devm_action_release, sizeof(struct action_devres),
  647. GFP_KERNEL, NUMA_NO_NODE, name);
  648. if (!devres)
  649. return -ENOMEM;
  650. devres->data = data;
  651. devres->action = action;
  652. devres_add(dev, devres);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(__devm_add_action);
  656. bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data)
  657. {
  658. struct action_devres devres = {
  659. .data = data,
  660. .action = action,
  661. };
  662. return devres_find(dev, devm_action_release, devm_action_match, &devres);
  663. }
  664. EXPORT_SYMBOL_GPL(devm_is_action_added);
  665. /**
  666. * devm_remove_action_nowarn() - removes previously added custom action
  667. * @dev: Device that owns the action
  668. * @action: Function implementing the action
  669. * @data: Pointer to data passed to @action implementation
  670. *
  671. * Removes instance of @action previously added by devm_add_action().
  672. * Both action and data should match one of the existing entries.
  673. *
  674. * In contrast to devm_remove_action(), this function does not WARN() if no
  675. * entry could have been found.
  676. *
  677. * This should only be used if the action is contained in an object with
  678. * independent lifetime management, e.g. the Devres rust abstraction.
  679. *
  680. * Causing the warning from regular driver code most likely indicates an abuse
  681. * of the devres API.
  682. *
  683. * Returns: 0 on success, -ENOENT if no entry could have been found.
  684. */
  685. int devm_remove_action_nowarn(struct device *dev,
  686. void (*action)(void *),
  687. void *data)
  688. {
  689. struct action_devres devres = {
  690. .data = data,
  691. .action = action,
  692. };
  693. return devres_destroy(dev, devm_action_release, devm_action_match,
  694. &devres);
  695. }
  696. EXPORT_SYMBOL_GPL(devm_remove_action_nowarn);
  697. /**
  698. * devm_release_action() - release previously added custom action
  699. * @dev: Device that owns the action
  700. * @action: Function implementing the action
  701. * @data: Pointer to data passed to @action implementation
  702. *
  703. * Releases and removes instance of @action previously added by
  704. * devm_add_action(). Both action and data should match one of the
  705. * existing entries.
  706. */
  707. void devm_release_action(struct device *dev, void (*action)(void *), void *data)
  708. {
  709. struct action_devres devres = {
  710. .data = data,
  711. .action = action,
  712. };
  713. WARN_ON(devres_release(dev, devm_action_release, devm_action_match,
  714. &devres));
  715. }
  716. EXPORT_SYMBOL_GPL(devm_release_action);
  717. /*
  718. * Managed kmalloc/kfree
  719. */
  720. static void devm_kmalloc_release(struct device *dev, void *res)
  721. {
  722. /* noop */
  723. }
  724. static int devm_kmalloc_match(struct device *dev, void *res, void *data)
  725. {
  726. return res == data;
  727. }
  728. /**
  729. * devm_kmalloc - Resource-managed kmalloc
  730. * @dev: Device to allocate memory for
  731. * @size: Allocation size
  732. * @gfp: Allocation gfp flags
  733. *
  734. * Managed kmalloc. Memory allocated with this function is
  735. * automatically freed on driver detach. Like all other devres
  736. * resources, guaranteed alignment is unsigned long long.
  737. *
  738. * RETURNS:
  739. * Pointer to allocated memory on success, NULL on failure.
  740. */
  741. void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
  742. {
  743. struct devres *dr;
  744. if (unlikely(!size))
  745. return ZERO_SIZE_PTR;
  746. /* use raw alloc_dr for kmalloc caller tracing */
  747. dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
  748. if (unlikely(!dr))
  749. return NULL;
  750. /*
  751. * This is named devm_kzalloc_release for historical reasons
  752. * The initial implementation did not support kmalloc, only kzalloc
  753. */
  754. set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
  755. devres_add(dev, dr->data);
  756. return dr->data;
  757. }
  758. EXPORT_SYMBOL_GPL(devm_kmalloc);
  759. /**
  760. * devm_krealloc - Resource-managed krealloc()
  761. * @dev: Device to re-allocate memory for
  762. * @ptr: Pointer to the memory chunk to re-allocate
  763. * @new_size: New allocation size
  764. * @gfp: Allocation gfp flags
  765. *
  766. * Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().
  767. * Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,
  768. * it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the
  769. * previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't
  770. * change the order in which the release callback for the re-alloc'ed devres
  771. * will be called (except when falling back to devm_kmalloc() or when freeing
  772. * resources when new_size is zero). The contents of the memory are preserved
  773. * up to the lesser of new and old sizes.
  774. */
  775. void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
  776. {
  777. size_t total_new_size, total_old_size;
  778. struct devres *old_dr, *new_dr;
  779. unsigned long flags;
  780. if (unlikely(!new_size)) {
  781. devm_kfree(dev, ptr);
  782. return ZERO_SIZE_PTR;
  783. }
  784. if (unlikely(ZERO_OR_NULL_PTR(ptr)))
  785. return devm_kmalloc(dev, new_size, gfp);
  786. if (WARN_ON(is_kernel_rodata((unsigned long)ptr)))
  787. /*
  788. * We cannot reliably realloc a const string returned by
  789. * devm_kstrdup_const().
  790. */
  791. return NULL;
  792. if (!check_dr_size(new_size, &total_new_size))
  793. return NULL;
  794. total_old_size = ksize(container_of(ptr, struct devres, data));
  795. if (total_old_size == 0) {
  796. WARN(1, "Pointer doesn't point to dynamically allocated memory.");
  797. return NULL;
  798. }
  799. /*
  800. * If new size is smaller or equal to the actual number of bytes
  801. * allocated previously - just return the same pointer.
  802. */
  803. if (total_new_size <= total_old_size)
  804. return ptr;
  805. /*
  806. * Otherwise: allocate new, larger chunk. We need to allocate before
  807. * taking the lock as most probably the caller uses GFP_KERNEL.
  808. * alloc_dr() will call check_dr_size() to reserve extra memory
  809. * for struct devres automatically, so size @new_size user request
  810. * is delivered to it directly as devm_kmalloc() does.
  811. */
  812. new_dr = alloc_dr(devm_kmalloc_release,
  813. new_size, gfp, dev_to_node(dev));
  814. if (!new_dr)
  815. return NULL;
  816. /*
  817. * The spinlock protects the linked list against concurrent
  818. * modifications but not the resource itself.
  819. */
  820. spin_lock_irqsave(&dev->devres_lock, flags);
  821. old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr);
  822. if (!old_dr) {
  823. spin_unlock_irqrestore(&dev->devres_lock, flags);
  824. kfree(new_dr);
  825. WARN(1, "Memory chunk not managed or managed by a different device.");
  826. return NULL;
  827. }
  828. replace_dr(dev, &old_dr->node, &new_dr->node);
  829. spin_unlock_irqrestore(&dev->devres_lock, flags);
  830. /*
  831. * We can copy the memory contents after releasing the lock as we're
  832. * no longer modifying the list links.
  833. */
  834. memcpy(new_dr->data, old_dr->data,
  835. total_old_size - offsetof(struct devres, data));
  836. /*
  837. * Same for releasing the old devres - it's now been removed from the
  838. * list. This is also the reason why we must not use devm_kfree() - the
  839. * links are no longer valid.
  840. */
  841. kfree(old_dr);
  842. return new_dr->data;
  843. }
  844. EXPORT_SYMBOL_GPL(devm_krealloc);
  845. /**
  846. * devm_kstrdup - Allocate resource managed space and
  847. * copy an existing string into that.
  848. * @dev: Device to allocate memory for
  849. * @s: the string to duplicate
  850. * @gfp: the GFP mask used in the devm_kmalloc() call when
  851. * allocating memory
  852. * RETURNS:
  853. * Pointer to allocated string on success, NULL on failure.
  854. */
  855. char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
  856. {
  857. if (!s)
  858. return NULL;
  859. return devm_kmemdup(dev, s, strlen(s) + 1, gfp);
  860. }
  861. EXPORT_SYMBOL_GPL(devm_kstrdup);
  862. /**
  863. * devm_kstrdup_const - resource managed conditional string duplication
  864. * @dev: device for which to duplicate the string
  865. * @s: the string to duplicate
  866. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  867. *
  868. * Strings allocated by devm_kstrdup_const will be automatically freed when
  869. * the associated device is detached.
  870. *
  871. * RETURNS:
  872. * Source string if it is in .rodata section otherwise it falls back to
  873. * devm_kstrdup.
  874. */
  875. const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
  876. {
  877. if (is_kernel_rodata((unsigned long)s))
  878. return s;
  879. return devm_kstrdup(dev, s, gfp);
  880. }
  881. EXPORT_SYMBOL_GPL(devm_kstrdup_const);
  882. /**
  883. * devm_kvasprintf - Allocate resource managed space and format a string
  884. * into that.
  885. * @dev: Device to allocate memory for
  886. * @gfp: the GFP mask used in the devm_kmalloc() call when
  887. * allocating memory
  888. * @fmt: The printf()-style format string
  889. * @ap: Arguments for the format string
  890. * RETURNS:
  891. * Pointer to allocated string on success, NULL on failure.
  892. */
  893. char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
  894. va_list ap)
  895. {
  896. unsigned int len;
  897. char *p;
  898. va_list aq;
  899. va_copy(aq, ap);
  900. len = vsnprintf(NULL, 0, fmt, aq);
  901. va_end(aq);
  902. p = devm_kmalloc(dev, len+1, gfp);
  903. if (!p)
  904. return NULL;
  905. vsnprintf(p, len+1, fmt, ap);
  906. return p;
  907. }
  908. EXPORT_SYMBOL(devm_kvasprintf);
  909. /**
  910. * devm_kasprintf - Allocate resource managed space and format a string
  911. * into that.
  912. * @dev: Device to allocate memory for
  913. * @gfp: the GFP mask used in the devm_kmalloc() call when
  914. * allocating memory
  915. * @fmt: The printf()-style format string
  916. * @...: Arguments for the format string
  917. * RETURNS:
  918. * Pointer to allocated string on success, NULL on failure.
  919. */
  920. char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
  921. {
  922. va_list ap;
  923. char *p;
  924. va_start(ap, fmt);
  925. p = devm_kvasprintf(dev, gfp, fmt, ap);
  926. va_end(ap);
  927. return p;
  928. }
  929. EXPORT_SYMBOL_GPL(devm_kasprintf);
  930. /**
  931. * devm_kfree - Resource-managed kfree
  932. * @dev: Device this memory belongs to
  933. * @p: Memory to free
  934. *
  935. * Free memory allocated with devm_kmalloc().
  936. */
  937. void devm_kfree(struct device *dev, const void *p)
  938. {
  939. int rc;
  940. /*
  941. * Special cases: pointer to a string in .rodata returned by
  942. * devm_kstrdup_const() or NULL/ZERO ptr.
  943. */
  944. if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p)))
  945. return;
  946. rc = devres_destroy(dev, devm_kmalloc_release,
  947. devm_kmalloc_match, (void *)p);
  948. WARN_ON(rc);
  949. }
  950. EXPORT_SYMBOL_GPL(devm_kfree);
  951. /**
  952. * devm_kmemdup - Resource-managed kmemdup
  953. * @dev: Device this memory belongs to
  954. * @src: Memory region to duplicate
  955. * @len: Memory region length
  956. * @gfp: GFP mask to use
  957. *
  958. * Duplicate region of a memory using resource managed kmalloc
  959. */
  960. void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
  961. {
  962. void *p;
  963. p = devm_kmalloc(dev, len, gfp);
  964. if (p)
  965. memcpy(p, src, len);
  966. return p;
  967. }
  968. EXPORT_SYMBOL_GPL(devm_kmemdup);
  969. /**
  970. * devm_kmemdup_const - conditionally duplicate and manage a region of memory
  971. *
  972. * @dev: Device this memory belongs to
  973. * @src: memory region to duplicate
  974. * @len: memory region length,
  975. * @gfp: GFP mask to use
  976. *
  977. * Return: source address if it is in .rodata or the return value of kmemdup()
  978. * to which the function falls back otherwise.
  979. */
  980. const void *
  981. devm_kmemdup_const(struct device *dev, const void *src, size_t len, gfp_t gfp)
  982. {
  983. if (is_kernel_rodata((unsigned long)src))
  984. return src;
  985. return devm_kmemdup(dev, src, len, gfp);
  986. }
  987. EXPORT_SYMBOL_GPL(devm_kmemdup_const);
  988. struct pages_devres {
  989. unsigned long addr;
  990. unsigned int order;
  991. };
  992. static int devm_pages_match(struct device *dev, void *res, void *p)
  993. {
  994. struct pages_devres *devres = res;
  995. struct pages_devres *target = p;
  996. return devres->addr == target->addr;
  997. }
  998. static void devm_pages_release(struct device *dev, void *res)
  999. {
  1000. struct pages_devres *devres = res;
  1001. free_pages(devres->addr, devres->order);
  1002. }
  1003. /**
  1004. * devm_get_free_pages - Resource-managed __get_free_pages
  1005. * @dev: Device to allocate memory for
  1006. * @gfp_mask: Allocation gfp flags
  1007. * @order: Allocation size is (1 << order) pages
  1008. *
  1009. * Managed get_free_pages. Memory allocated with this function is
  1010. * automatically freed on driver detach.
  1011. *
  1012. * RETURNS:
  1013. * Address of allocated memory on success, 0 on failure.
  1014. */
  1015. unsigned long devm_get_free_pages(struct device *dev,
  1016. gfp_t gfp_mask, unsigned int order)
  1017. {
  1018. struct pages_devres *devres;
  1019. unsigned long addr;
  1020. addr = __get_free_pages(gfp_mask, order);
  1021. if (unlikely(!addr))
  1022. return 0;
  1023. devres = devres_alloc(devm_pages_release,
  1024. sizeof(struct pages_devres), GFP_KERNEL);
  1025. if (unlikely(!devres)) {
  1026. free_pages(addr, order);
  1027. return 0;
  1028. }
  1029. devres->addr = addr;
  1030. devres->order = order;
  1031. devres_add(dev, devres);
  1032. return addr;
  1033. }
  1034. EXPORT_SYMBOL_GPL(devm_get_free_pages);
  1035. /**
  1036. * devm_free_pages - Resource-managed free_pages
  1037. * @dev: Device this memory belongs to
  1038. * @addr: Memory to free
  1039. *
  1040. * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
  1041. * there is no need to supply the @order.
  1042. */
  1043. void devm_free_pages(struct device *dev, unsigned long addr)
  1044. {
  1045. struct pages_devres devres = { .addr = addr };
  1046. WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
  1047. &devres));
  1048. }
  1049. EXPORT_SYMBOL_GPL(devm_free_pages);
  1050. static void devm_percpu_release(struct device *dev, void *pdata)
  1051. {
  1052. void __percpu *p;
  1053. p = *(void __percpu **)pdata;
  1054. free_percpu(p);
  1055. }
  1056. /**
  1057. * __devm_alloc_percpu - Resource-managed alloc_percpu
  1058. * @dev: Device to allocate per-cpu memory for
  1059. * @size: Size of per-cpu memory to allocate
  1060. * @align: Alignment of per-cpu memory to allocate
  1061. *
  1062. * Managed alloc_percpu. Per-cpu memory allocated with this function is
  1063. * automatically freed on driver detach.
  1064. *
  1065. * RETURNS:
  1066. * Pointer to allocated memory on success, NULL on failure.
  1067. */
  1068. void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  1069. size_t align)
  1070. {
  1071. void *p;
  1072. void __percpu *pcpu;
  1073. pcpu = __alloc_percpu(size, align);
  1074. if (!pcpu)
  1075. return NULL;
  1076. p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
  1077. if (!p) {
  1078. free_percpu(pcpu);
  1079. return NULL;
  1080. }
  1081. *(void __percpu **)p = pcpu;
  1082. devres_add(dev, p);
  1083. return pcpu;
  1084. }
  1085. EXPORT_SYMBOL_GPL(__devm_alloc_percpu);