resource.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/resource.c
  4. *
  5. * Copyright (C) 1999 Linus Torvalds
  6. * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
  7. *
  8. * Arbitrary resource management.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/export.h>
  12. #include <linux/errno.h>
  13. #include <linux/ioport.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/fs.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/pseudo_fs.h>
  20. #include <linux/sched.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/device.h>
  23. #include <linux/pfn.h>
  24. #include <linux/mm.h>
  25. #include <linux/mount.h>
  26. #include <linux/resource_ext.h>
  27. #include <uapi/linux/magic.h>
  28. #include <linux/string.h>
  29. #include <linux/vmalloc.h>
  30. #include <asm/io.h>
  31. struct resource ioport_resource = {
  32. .name = "PCI IO",
  33. .start = 0,
  34. .end = IO_SPACE_LIMIT,
  35. .flags = IORESOURCE_IO,
  36. };
  37. EXPORT_SYMBOL(ioport_resource);
  38. struct resource iomem_resource = {
  39. .name = "PCI mem",
  40. .start = 0,
  41. .end = -1,
  42. .flags = IORESOURCE_MEM,
  43. };
  44. EXPORT_SYMBOL(iomem_resource);
  45. static DEFINE_RWLOCK(resource_lock);
  46. static struct resource *next_resource(struct resource *p, bool skip_children)
  47. {
  48. if (!skip_children && p->child)
  49. return p->child;
  50. while (!p->sibling && p->parent)
  51. p = p->parent;
  52. return p->sibling;
  53. }
  54. #define for_each_resource(_root, _p, _skip_children) \
  55. for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children))
  56. #ifdef CONFIG_PROC_FS
  57. enum { MAX_IORES_LEVEL = 5 };
  58. static void *r_start(struct seq_file *m, loff_t *pos)
  59. __acquires(resource_lock)
  60. {
  61. struct resource *root = pde_data(file_inode(m->file));
  62. struct resource *p;
  63. loff_t l = *pos;
  64. read_lock(&resource_lock);
  65. for_each_resource(root, p, false) {
  66. if (l-- == 0)
  67. break;
  68. }
  69. return p;
  70. }
  71. static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  72. {
  73. struct resource *p = v;
  74. (*pos)++;
  75. return (void *)next_resource(p, false);
  76. }
  77. static void r_stop(struct seq_file *m, void *v)
  78. __releases(resource_lock)
  79. {
  80. read_unlock(&resource_lock);
  81. }
  82. static int r_show(struct seq_file *m, void *v)
  83. {
  84. struct resource *root = pde_data(file_inode(m->file));
  85. struct resource *r = v, *p;
  86. unsigned long long start, end;
  87. int width = root->end < 0x10000 ? 4 : 8;
  88. int depth;
  89. for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
  90. if (p->parent == root)
  91. break;
  92. if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
  93. start = r->start;
  94. end = r->end;
  95. } else {
  96. start = end = 0;
  97. }
  98. seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
  99. depth * 2, "",
  100. width, start,
  101. width, end,
  102. r->name ? r->name : "<BAD>");
  103. return 0;
  104. }
  105. static const struct seq_operations resource_op = {
  106. .start = r_start,
  107. .next = r_next,
  108. .stop = r_stop,
  109. .show = r_show,
  110. };
  111. static int __init ioresources_init(void)
  112. {
  113. proc_create_seq_data("ioports", 0, NULL, &resource_op,
  114. &ioport_resource);
  115. proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
  116. return 0;
  117. }
  118. __initcall(ioresources_init);
  119. #endif /* CONFIG_PROC_FS */
  120. static void free_resource(struct resource *res)
  121. {
  122. /**
  123. * If the resource was allocated using memblock early during boot
  124. * we'll leak it here: we can only return full pages back to the
  125. * buddy and trying to be smart and reusing them eventually in
  126. * alloc_resource() overcomplicates resource handling.
  127. */
  128. if (res && PageSlab(virt_to_head_page(res)))
  129. kfree(res);
  130. }
  131. static struct resource *alloc_resource(gfp_t flags)
  132. {
  133. return kzalloc(sizeof(struct resource), flags);
  134. }
  135. /* Return the conflict entry if you can't request it */
  136. static struct resource * __request_resource(struct resource *root, struct resource *new)
  137. {
  138. resource_size_t start = new->start;
  139. resource_size_t end = new->end;
  140. struct resource *tmp, **p;
  141. if (end < start)
  142. return root;
  143. if (start < root->start)
  144. return root;
  145. if (end > root->end)
  146. return root;
  147. p = &root->child;
  148. for (;;) {
  149. tmp = *p;
  150. if (!tmp || tmp->start > end) {
  151. new->sibling = tmp;
  152. *p = new;
  153. new->parent = root;
  154. return NULL;
  155. }
  156. p = &tmp->sibling;
  157. if (tmp->end < start)
  158. continue;
  159. return tmp;
  160. }
  161. }
  162. static int __release_resource(struct resource *old, bool release_child)
  163. {
  164. struct resource *tmp, **p, *chd;
  165. p = &old->parent->child;
  166. for (;;) {
  167. tmp = *p;
  168. if (!tmp)
  169. break;
  170. if (tmp == old) {
  171. if (release_child || !(tmp->child)) {
  172. *p = tmp->sibling;
  173. } else {
  174. for (chd = tmp->child;; chd = chd->sibling) {
  175. chd->parent = tmp->parent;
  176. if (!(chd->sibling))
  177. break;
  178. }
  179. *p = tmp->child;
  180. chd->sibling = tmp->sibling;
  181. }
  182. old->parent = NULL;
  183. return 0;
  184. }
  185. p = &tmp->sibling;
  186. }
  187. return -EINVAL;
  188. }
  189. static void __release_child_resources(struct resource *r)
  190. {
  191. struct resource *tmp, *p;
  192. resource_size_t size;
  193. p = r->child;
  194. r->child = NULL;
  195. while (p) {
  196. tmp = p;
  197. p = p->sibling;
  198. tmp->parent = NULL;
  199. tmp->sibling = NULL;
  200. __release_child_resources(tmp);
  201. printk(KERN_DEBUG "release child resource %pR\n", tmp);
  202. /* need to restore size, and keep flags */
  203. size = resource_size(tmp);
  204. tmp->start = 0;
  205. tmp->end = size - 1;
  206. }
  207. }
  208. void release_child_resources(struct resource *r)
  209. {
  210. write_lock(&resource_lock);
  211. __release_child_resources(r);
  212. write_unlock(&resource_lock);
  213. }
  214. /**
  215. * request_resource_conflict - request and reserve an I/O or memory resource
  216. * @root: root resource descriptor
  217. * @new: resource descriptor desired by caller
  218. *
  219. * Returns 0 for success, conflict resource on error.
  220. */
  221. struct resource *request_resource_conflict(struct resource *root, struct resource *new)
  222. {
  223. struct resource *conflict;
  224. write_lock(&resource_lock);
  225. conflict = __request_resource(root, new);
  226. write_unlock(&resource_lock);
  227. return conflict;
  228. }
  229. /**
  230. * request_resource - request and reserve an I/O or memory resource
  231. * @root: root resource descriptor
  232. * @new: resource descriptor desired by caller
  233. *
  234. * Returns 0 for success, negative error code on error.
  235. */
  236. int request_resource(struct resource *root, struct resource *new)
  237. {
  238. struct resource *conflict;
  239. conflict = request_resource_conflict(root, new);
  240. return conflict ? -EBUSY : 0;
  241. }
  242. EXPORT_SYMBOL(request_resource);
  243. /**
  244. * release_resource - release a previously reserved resource
  245. * @old: resource pointer
  246. */
  247. int release_resource(struct resource *old)
  248. {
  249. int retval;
  250. write_lock(&resource_lock);
  251. retval = __release_resource(old, true);
  252. write_unlock(&resource_lock);
  253. return retval;
  254. }
  255. EXPORT_SYMBOL(release_resource);
  256. /**
  257. * find_next_iomem_res - Finds the lowest iomem resource that covers part of
  258. * [@start..@end].
  259. *
  260. * If a resource is found, returns 0 and @*res is overwritten with the part
  261. * of the resource that's within [@start..@end]; if none is found, returns
  262. * -ENODEV. Returns -EINVAL for invalid parameters.
  263. *
  264. * @start: start address of the resource searched for
  265. * @end: end address of same resource
  266. * @flags: flags which the resource must have
  267. * @desc: descriptor the resource must have
  268. * @res: return ptr, if resource found
  269. *
  270. * The caller must specify @start, @end, @flags, and @desc
  271. * (which may be IORES_DESC_NONE).
  272. */
  273. static int find_next_iomem_res(resource_size_t start, resource_size_t end,
  274. unsigned long flags, unsigned long desc,
  275. struct resource *res)
  276. {
  277. struct resource *p;
  278. if (!res)
  279. return -EINVAL;
  280. if (start >= end)
  281. return -EINVAL;
  282. read_lock(&resource_lock);
  283. for_each_resource(&iomem_resource, p, false) {
  284. /* If we passed the resource we are looking for, stop */
  285. if (p->start > end) {
  286. p = NULL;
  287. break;
  288. }
  289. /* Skip until we find a range that matches what we look for */
  290. if (p->end < start)
  291. continue;
  292. if ((p->flags & flags) != flags)
  293. continue;
  294. if ((desc != IORES_DESC_NONE) && (desc != p->desc))
  295. continue;
  296. /* Found a match, break */
  297. break;
  298. }
  299. if (p) {
  300. /* copy data */
  301. *res = (struct resource) {
  302. .start = max(start, p->start),
  303. .end = min(end, p->end),
  304. .flags = p->flags,
  305. .desc = p->desc,
  306. .parent = p->parent,
  307. };
  308. }
  309. read_unlock(&resource_lock);
  310. return p ? 0 : -ENODEV;
  311. }
  312. static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
  313. unsigned long flags, unsigned long desc,
  314. void *arg,
  315. int (*func)(struct resource *, void *))
  316. {
  317. struct resource res;
  318. int ret = -EINVAL;
  319. while (start < end &&
  320. !find_next_iomem_res(start, end, flags, desc, &res)) {
  321. ret = (*func)(&res, arg);
  322. if (ret)
  323. break;
  324. start = res.end + 1;
  325. }
  326. return ret;
  327. }
  328. /**
  329. * walk_iomem_res_desc - Walks through iomem resources and calls func()
  330. * with matching resource ranges.
  331. * *
  332. * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
  333. * @flags: I/O resource flags
  334. * @start: start addr
  335. * @end: end addr
  336. * @arg: function argument for the callback @func
  337. * @func: callback function that is called for each qualifying resource area
  338. *
  339. * All the memory ranges which overlap start,end and also match flags and
  340. * desc are valid candidates.
  341. *
  342. * NOTE: For a new descriptor search, define a new IORES_DESC in
  343. * <linux/ioport.h> and set it in 'desc' of a target resource entry.
  344. */
  345. int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
  346. u64 end, void *arg, int (*func)(struct resource *, void *))
  347. {
  348. return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
  349. }
  350. EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
  351. /*
  352. * This function calls the @func callback against all memory ranges of type
  353. * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
  354. * Now, this function is only for System RAM, it deals with full ranges and
  355. * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
  356. * ranges.
  357. */
  358. int walk_system_ram_res(u64 start, u64 end, void *arg,
  359. int (*func)(struct resource *, void *))
  360. {
  361. unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  362. return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
  363. func);
  364. }
  365. /*
  366. * This function, being a variant of walk_system_ram_res(), calls the @func
  367. * callback against all memory ranges of type System RAM which are marked as
  368. * IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from
  369. * higher to lower.
  370. */
  371. int walk_system_ram_res_rev(u64 start, u64 end, void *arg,
  372. int (*func)(struct resource *, void *))
  373. {
  374. struct resource res, *rams;
  375. int rams_size = 16, i;
  376. unsigned long flags;
  377. int ret = -1;
  378. /* create a list */
  379. rams = kvcalloc(rams_size, sizeof(struct resource), GFP_KERNEL);
  380. if (!rams)
  381. return ret;
  382. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  383. i = 0;
  384. while ((start < end) &&
  385. (!find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res))) {
  386. if (i >= rams_size) {
  387. /* re-alloc */
  388. struct resource *rams_new;
  389. rams_new = kvrealloc(rams, (rams_size + 16) * sizeof(struct resource),
  390. GFP_KERNEL);
  391. if (!rams_new)
  392. goto out;
  393. rams = rams_new;
  394. rams_size += 16;
  395. }
  396. rams[i++] = res;
  397. start = res.end + 1;
  398. }
  399. /* go reverse */
  400. for (i--; i >= 0; i--) {
  401. ret = (*func)(&rams[i], arg);
  402. if (ret)
  403. break;
  404. }
  405. out:
  406. kvfree(rams);
  407. return ret;
  408. }
  409. /*
  410. * This function calls the @func callback against all memory ranges, which
  411. * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
  412. */
  413. int walk_mem_res(u64 start, u64 end, void *arg,
  414. int (*func)(struct resource *, void *))
  415. {
  416. unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  417. return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
  418. func);
  419. }
  420. /*
  421. * This function calls the @func callback against all memory ranges of type
  422. * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
  423. * It is to be used only for System RAM.
  424. */
  425. int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
  426. void *arg, int (*func)(unsigned long, unsigned long, void *))
  427. {
  428. resource_size_t start, end;
  429. unsigned long flags;
  430. struct resource res;
  431. unsigned long pfn, end_pfn;
  432. int ret = -EINVAL;
  433. start = (u64) start_pfn << PAGE_SHIFT;
  434. end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
  435. flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  436. while (start < end &&
  437. !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
  438. pfn = PFN_UP(res.start);
  439. end_pfn = PFN_DOWN(res.end + 1);
  440. if (end_pfn > pfn)
  441. ret = (*func)(pfn, end_pfn - pfn, arg);
  442. if (ret)
  443. break;
  444. start = res.end + 1;
  445. }
  446. return ret;
  447. }
  448. static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
  449. {
  450. return 1;
  451. }
  452. /*
  453. * This generic page_is_ram() returns true if specified address is
  454. * registered as System RAM in iomem_resource list.
  455. */
  456. int __weak page_is_ram(unsigned long pfn)
  457. {
  458. return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
  459. }
  460. EXPORT_SYMBOL_GPL(page_is_ram);
  461. static int __region_intersects(struct resource *parent, resource_size_t start,
  462. size_t size, unsigned long flags,
  463. unsigned long desc)
  464. {
  465. resource_size_t ostart, oend;
  466. int type = 0; int other = 0;
  467. struct resource *p, *dp;
  468. bool is_type, covered;
  469. struct resource res;
  470. res.start = start;
  471. res.end = start + size - 1;
  472. for (p = parent->child; p ; p = p->sibling) {
  473. if (!resource_overlaps(p, &res))
  474. continue;
  475. is_type = (p->flags & flags) == flags &&
  476. (desc == IORES_DESC_NONE || desc == p->desc);
  477. if (is_type) {
  478. type++;
  479. continue;
  480. }
  481. /*
  482. * Continue to search in descendant resources as if the
  483. * matched descendant resources cover some ranges of 'p'.
  484. *
  485. * |------------- "CXL Window 0" ------------|
  486. * |-- "System RAM" --|
  487. *
  488. * will behave similar as the following fake resource
  489. * tree when searching "System RAM".
  490. *
  491. * |-- "System RAM" --||-- "CXL Window 0a" --|
  492. */
  493. covered = false;
  494. ostart = max(res.start, p->start);
  495. oend = min(res.end, p->end);
  496. for_each_resource(p, dp, false) {
  497. if (!resource_overlaps(dp, &res))
  498. continue;
  499. is_type = (dp->flags & flags) == flags &&
  500. (desc == IORES_DESC_NONE || desc == dp->desc);
  501. if (is_type) {
  502. type++;
  503. /*
  504. * Range from 'ostart' to 'dp->start'
  505. * isn't covered by matched resource.
  506. */
  507. if (dp->start > ostart)
  508. break;
  509. if (dp->end >= oend) {
  510. covered = true;
  511. break;
  512. }
  513. /* Remove covered range */
  514. ostart = max(ostart, dp->end + 1);
  515. }
  516. }
  517. if (!covered)
  518. other++;
  519. }
  520. if (type == 0)
  521. return REGION_DISJOINT;
  522. if (other == 0)
  523. return REGION_INTERSECTS;
  524. return REGION_MIXED;
  525. }
  526. /**
  527. * region_intersects() - determine intersection of region with known resources
  528. * @start: region start address
  529. * @size: size of region
  530. * @flags: flags of resource (in iomem_resource)
  531. * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
  532. *
  533. * Check if the specified region partially overlaps or fully eclipses a
  534. * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
  535. * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
  536. * return REGION_MIXED if the region overlaps @flags/@desc and another
  537. * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
  538. * and no other defined resource. Note that REGION_INTERSECTS is also
  539. * returned in the case when the specified region overlaps RAM and undefined
  540. * memory holes.
  541. *
  542. * region_intersect() is used by memory remapping functions to ensure
  543. * the user is not remapping RAM and is a vast speed up over walking
  544. * through the resource table page by page.
  545. */
  546. int region_intersects(resource_size_t start, size_t size, unsigned long flags,
  547. unsigned long desc)
  548. {
  549. int ret;
  550. read_lock(&resource_lock);
  551. ret = __region_intersects(&iomem_resource, start, size, flags, desc);
  552. read_unlock(&resource_lock);
  553. return ret;
  554. }
  555. EXPORT_SYMBOL_GPL(region_intersects);
  556. void __weak arch_remove_reservations(struct resource *avail)
  557. {
  558. }
  559. static void resource_clip(struct resource *res, resource_size_t min,
  560. resource_size_t max)
  561. {
  562. if (res->start < min)
  563. res->start = min;
  564. if (res->end > max)
  565. res->end = max;
  566. }
  567. /*
  568. * Find empty space in the resource tree with the given range and
  569. * alignment constraints
  570. */
  571. static int __find_resource_space(struct resource *root, struct resource *old,
  572. struct resource *new, resource_size_t size,
  573. struct resource_constraint *constraint)
  574. {
  575. struct resource *this = root->child;
  576. struct resource tmp = *new, avail, alloc;
  577. resource_alignf alignf = constraint->alignf;
  578. tmp.start = root->start;
  579. /*
  580. * Skip past an allocated resource that starts at 0, since the assignment
  581. * of this->start - 1 to tmp->end below would cause an underflow.
  582. */
  583. if (this && this->start == root->start) {
  584. tmp.start = (this == old) ? old->start : this->end + 1;
  585. this = this->sibling;
  586. }
  587. for(;;) {
  588. if (this)
  589. tmp.end = (this == old) ? this->end : this->start - 1;
  590. else
  591. tmp.end = root->end;
  592. if (tmp.end < tmp.start)
  593. goto next;
  594. resource_clip(&tmp, constraint->min, constraint->max);
  595. arch_remove_reservations(&tmp);
  596. /* Check for overflow after ALIGN() */
  597. avail.start = ALIGN(tmp.start, constraint->align);
  598. avail.end = tmp.end;
  599. avail.flags = new->flags & ~IORESOURCE_UNSET;
  600. if (avail.start >= tmp.start) {
  601. alloc.flags = avail.flags;
  602. if (alignf) {
  603. alloc.start = alignf(constraint->alignf_data,
  604. &avail, size, constraint->align);
  605. } else {
  606. alloc.start = avail.start;
  607. }
  608. alloc.end = alloc.start + size - 1;
  609. if (alloc.start <= alloc.end &&
  610. resource_contains(&avail, &alloc)) {
  611. new->start = alloc.start;
  612. new->end = alloc.end;
  613. return 0;
  614. }
  615. }
  616. next: if (!this || this->end == root->end)
  617. break;
  618. if (this != old)
  619. tmp.start = this->end + 1;
  620. this = this->sibling;
  621. }
  622. return -EBUSY;
  623. }
  624. /**
  625. * find_resource_space - Find empty space in the resource tree
  626. * @root: Root resource descriptor
  627. * @new: Resource descriptor awaiting an empty resource space
  628. * @size: The minimum size of the empty space
  629. * @constraint: The range and alignment constraints to be met
  630. *
  631. * Finds an empty space under @root in the resource tree satisfying range and
  632. * alignment @constraints.
  633. *
  634. * Return:
  635. * * %0 - if successful, @new members start, end, and flags are altered.
  636. * * %-EBUSY - if no empty space was found.
  637. */
  638. int find_resource_space(struct resource *root, struct resource *new,
  639. resource_size_t size,
  640. struct resource_constraint *constraint)
  641. {
  642. return __find_resource_space(root, NULL, new, size, constraint);
  643. }
  644. EXPORT_SYMBOL_GPL(find_resource_space);
  645. /**
  646. * reallocate_resource - allocate a slot in the resource tree given range & alignment.
  647. * The resource will be relocated if the new size cannot be reallocated in the
  648. * current location.
  649. *
  650. * @root: root resource descriptor
  651. * @old: resource descriptor desired by caller
  652. * @newsize: new size of the resource descriptor
  653. * @constraint: the size and alignment constraints to be met.
  654. */
  655. static int reallocate_resource(struct resource *root, struct resource *old,
  656. resource_size_t newsize,
  657. struct resource_constraint *constraint)
  658. {
  659. int err=0;
  660. struct resource new = *old;
  661. struct resource *conflict;
  662. write_lock(&resource_lock);
  663. if ((err = __find_resource_space(root, old, &new, newsize, constraint)))
  664. goto out;
  665. if (resource_contains(&new, old)) {
  666. old->start = new.start;
  667. old->end = new.end;
  668. goto out;
  669. }
  670. if (old->child) {
  671. err = -EBUSY;
  672. goto out;
  673. }
  674. if (resource_contains(old, &new)) {
  675. old->start = new.start;
  676. old->end = new.end;
  677. } else {
  678. __release_resource(old, true);
  679. *old = new;
  680. conflict = __request_resource(root, old);
  681. BUG_ON(conflict);
  682. }
  683. out:
  684. write_unlock(&resource_lock);
  685. return err;
  686. }
  687. /**
  688. * allocate_resource - allocate empty slot in the resource tree given range & alignment.
  689. * The resource will be reallocated with a new size if it was already allocated
  690. * @root: root resource descriptor
  691. * @new: resource descriptor desired by caller
  692. * @size: requested resource region size
  693. * @min: minimum boundary to allocate
  694. * @max: maximum boundary to allocate
  695. * @align: alignment requested, in bytes
  696. * @alignf: alignment function, optional, called if not NULL
  697. * @alignf_data: arbitrary data to pass to the @alignf function
  698. */
  699. int allocate_resource(struct resource *root, struct resource *new,
  700. resource_size_t size, resource_size_t min,
  701. resource_size_t max, resource_size_t align,
  702. resource_alignf alignf,
  703. void *alignf_data)
  704. {
  705. int err;
  706. struct resource_constraint constraint;
  707. constraint.min = min;
  708. constraint.max = max;
  709. constraint.align = align;
  710. constraint.alignf = alignf;
  711. constraint.alignf_data = alignf_data;
  712. if ( new->parent ) {
  713. /* resource is already allocated, try reallocating with
  714. the new constraints */
  715. return reallocate_resource(root, new, size, &constraint);
  716. }
  717. write_lock(&resource_lock);
  718. err = find_resource_space(root, new, size, &constraint);
  719. if (err >= 0 && __request_resource(root, new))
  720. err = -EBUSY;
  721. write_unlock(&resource_lock);
  722. return err;
  723. }
  724. EXPORT_SYMBOL(allocate_resource);
  725. /**
  726. * lookup_resource - find an existing resource by a resource start address
  727. * @root: root resource descriptor
  728. * @start: resource start address
  729. *
  730. * Returns a pointer to the resource if found, NULL otherwise
  731. */
  732. struct resource *lookup_resource(struct resource *root, resource_size_t start)
  733. {
  734. struct resource *res;
  735. read_lock(&resource_lock);
  736. for (res = root->child; res; res = res->sibling) {
  737. if (res->start == start)
  738. break;
  739. }
  740. read_unlock(&resource_lock);
  741. return res;
  742. }
  743. /*
  744. * Insert a resource into the resource tree. If successful, return NULL,
  745. * otherwise return the conflicting resource (compare to __request_resource())
  746. */
  747. static struct resource * __insert_resource(struct resource *parent, struct resource *new)
  748. {
  749. struct resource *first, *next;
  750. for (;; parent = first) {
  751. first = __request_resource(parent, new);
  752. if (!first)
  753. return first;
  754. if (first == parent)
  755. return first;
  756. if (WARN_ON(first == new)) /* duplicated insertion */
  757. return first;
  758. if ((first->start > new->start) || (first->end < new->end))
  759. break;
  760. if ((first->start == new->start) && (first->end == new->end))
  761. break;
  762. }
  763. for (next = first; ; next = next->sibling) {
  764. /* Partial overlap? Bad, and unfixable */
  765. if (next->start < new->start || next->end > new->end)
  766. return next;
  767. if (!next->sibling)
  768. break;
  769. if (next->sibling->start > new->end)
  770. break;
  771. }
  772. new->parent = parent;
  773. new->sibling = next->sibling;
  774. new->child = first;
  775. next->sibling = NULL;
  776. for (next = first; next; next = next->sibling)
  777. next->parent = new;
  778. if (parent->child == first) {
  779. parent->child = new;
  780. } else {
  781. next = parent->child;
  782. while (next->sibling != first)
  783. next = next->sibling;
  784. next->sibling = new;
  785. }
  786. return NULL;
  787. }
  788. /**
  789. * insert_resource_conflict - Inserts resource in the resource tree
  790. * @parent: parent of the new resource
  791. * @new: new resource to insert
  792. *
  793. * Returns 0 on success, conflict resource if the resource can't be inserted.
  794. *
  795. * This function is equivalent to request_resource_conflict when no conflict
  796. * happens. If a conflict happens, and the conflicting resources
  797. * entirely fit within the range of the new resource, then the new
  798. * resource is inserted and the conflicting resources become children of
  799. * the new resource.
  800. *
  801. * This function is intended for producers of resources, such as FW modules
  802. * and bus drivers.
  803. */
  804. struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
  805. {
  806. struct resource *conflict;
  807. write_lock(&resource_lock);
  808. conflict = __insert_resource(parent, new);
  809. write_unlock(&resource_lock);
  810. return conflict;
  811. }
  812. /**
  813. * insert_resource - Inserts a resource in the resource tree
  814. * @parent: parent of the new resource
  815. * @new: new resource to insert
  816. *
  817. * Returns 0 on success, -EBUSY if the resource can't be inserted.
  818. *
  819. * This function is intended for producers of resources, such as FW modules
  820. * and bus drivers.
  821. */
  822. int insert_resource(struct resource *parent, struct resource *new)
  823. {
  824. struct resource *conflict;
  825. conflict = insert_resource_conflict(parent, new);
  826. return conflict ? -EBUSY : 0;
  827. }
  828. EXPORT_SYMBOL_GPL(insert_resource);
  829. /**
  830. * insert_resource_expand_to_fit - Insert a resource into the resource tree
  831. * @root: root resource descriptor
  832. * @new: new resource to insert
  833. *
  834. * Insert a resource into the resource tree, possibly expanding it in order
  835. * to make it encompass any conflicting resources.
  836. */
  837. void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
  838. {
  839. if (new->parent)
  840. return;
  841. write_lock(&resource_lock);
  842. for (;;) {
  843. struct resource *conflict;
  844. conflict = __insert_resource(root, new);
  845. if (!conflict)
  846. break;
  847. if (conflict == root)
  848. break;
  849. /* Ok, expand resource to cover the conflict, then try again .. */
  850. if (conflict->start < new->start)
  851. new->start = conflict->start;
  852. if (conflict->end > new->end)
  853. new->end = conflict->end;
  854. pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
  855. }
  856. write_unlock(&resource_lock);
  857. }
  858. /*
  859. * Not for general consumption, only early boot memory map parsing, PCI
  860. * resource discovery, and late discovery of CXL resources are expected
  861. * to use this interface. The former are built-in and only the latter,
  862. * CXL, is a module.
  863. */
  864. EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL);
  865. /**
  866. * remove_resource - Remove a resource in the resource tree
  867. * @old: resource to remove
  868. *
  869. * Returns 0 on success, -EINVAL if the resource is not valid.
  870. *
  871. * This function removes a resource previously inserted by insert_resource()
  872. * or insert_resource_conflict(), and moves the children (if any) up to
  873. * where they were before. insert_resource() and insert_resource_conflict()
  874. * insert a new resource, and move any conflicting resources down to the
  875. * children of the new resource.
  876. *
  877. * insert_resource(), insert_resource_conflict() and remove_resource() are
  878. * intended for producers of resources, such as FW modules and bus drivers.
  879. */
  880. int remove_resource(struct resource *old)
  881. {
  882. int retval;
  883. write_lock(&resource_lock);
  884. retval = __release_resource(old, false);
  885. write_unlock(&resource_lock);
  886. return retval;
  887. }
  888. EXPORT_SYMBOL_GPL(remove_resource);
  889. static int __adjust_resource(struct resource *res, resource_size_t start,
  890. resource_size_t size)
  891. {
  892. struct resource *tmp, *parent = res->parent;
  893. resource_size_t end = start + size - 1;
  894. int result = -EBUSY;
  895. if (!parent)
  896. goto skip;
  897. if ((start < parent->start) || (end > parent->end))
  898. goto out;
  899. if (res->sibling && (res->sibling->start <= end))
  900. goto out;
  901. tmp = parent->child;
  902. if (tmp != res) {
  903. while (tmp->sibling != res)
  904. tmp = tmp->sibling;
  905. if (start <= tmp->end)
  906. goto out;
  907. }
  908. skip:
  909. for (tmp = res->child; tmp; tmp = tmp->sibling)
  910. if ((tmp->start < start) || (tmp->end > end))
  911. goto out;
  912. res->start = start;
  913. res->end = end;
  914. result = 0;
  915. out:
  916. return result;
  917. }
  918. /**
  919. * adjust_resource - modify a resource's start and size
  920. * @res: resource to modify
  921. * @start: new start value
  922. * @size: new size
  923. *
  924. * Given an existing resource, change its start and size to match the
  925. * arguments. Returns 0 on success, -EBUSY if it can't fit.
  926. * Existing children of the resource are assumed to be immutable.
  927. */
  928. int adjust_resource(struct resource *res, resource_size_t start,
  929. resource_size_t size)
  930. {
  931. int result;
  932. write_lock(&resource_lock);
  933. result = __adjust_resource(res, start, size);
  934. write_unlock(&resource_lock);
  935. return result;
  936. }
  937. EXPORT_SYMBOL(adjust_resource);
  938. static void __init
  939. __reserve_region_with_split(struct resource *root, resource_size_t start,
  940. resource_size_t end, const char *name)
  941. {
  942. struct resource *parent = root;
  943. struct resource *conflict;
  944. struct resource *res = alloc_resource(GFP_ATOMIC);
  945. struct resource *next_res = NULL;
  946. int type = resource_type(root);
  947. if (!res)
  948. return;
  949. res->name = name;
  950. res->start = start;
  951. res->end = end;
  952. res->flags = type | IORESOURCE_BUSY;
  953. res->desc = IORES_DESC_NONE;
  954. while (1) {
  955. conflict = __request_resource(parent, res);
  956. if (!conflict) {
  957. if (!next_res)
  958. break;
  959. res = next_res;
  960. next_res = NULL;
  961. continue;
  962. }
  963. /* conflict covered whole area */
  964. if (conflict->start <= res->start &&
  965. conflict->end >= res->end) {
  966. free_resource(res);
  967. WARN_ON(next_res);
  968. break;
  969. }
  970. /* failed, split and try again */
  971. if (conflict->start > res->start) {
  972. end = res->end;
  973. res->end = conflict->start - 1;
  974. if (conflict->end < end) {
  975. next_res = alloc_resource(GFP_ATOMIC);
  976. if (!next_res) {
  977. free_resource(res);
  978. break;
  979. }
  980. next_res->name = name;
  981. next_res->start = conflict->end + 1;
  982. next_res->end = end;
  983. next_res->flags = type | IORESOURCE_BUSY;
  984. next_res->desc = IORES_DESC_NONE;
  985. }
  986. } else {
  987. res->start = conflict->end + 1;
  988. }
  989. }
  990. }
  991. void __init
  992. reserve_region_with_split(struct resource *root, resource_size_t start,
  993. resource_size_t end, const char *name)
  994. {
  995. int abort = 0;
  996. write_lock(&resource_lock);
  997. if (root->start > start || root->end < end) {
  998. pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
  999. (unsigned long long)start, (unsigned long long)end,
  1000. root);
  1001. if (start > root->end || end < root->start)
  1002. abort = 1;
  1003. else {
  1004. if (end > root->end)
  1005. end = root->end;
  1006. if (start < root->start)
  1007. start = root->start;
  1008. pr_err("fixing request to [0x%llx-0x%llx]\n",
  1009. (unsigned long long)start,
  1010. (unsigned long long)end);
  1011. }
  1012. dump_stack();
  1013. }
  1014. if (!abort)
  1015. __reserve_region_with_split(root, start, end, name);
  1016. write_unlock(&resource_lock);
  1017. }
  1018. /**
  1019. * resource_alignment - calculate resource's alignment
  1020. * @res: resource pointer
  1021. *
  1022. * Returns alignment on success, 0 (invalid alignment) on failure.
  1023. */
  1024. resource_size_t resource_alignment(struct resource *res)
  1025. {
  1026. switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
  1027. case IORESOURCE_SIZEALIGN:
  1028. return resource_size(res);
  1029. case IORESOURCE_STARTALIGN:
  1030. return res->start;
  1031. default:
  1032. return 0;
  1033. }
  1034. }
  1035. /*
  1036. * This is compatibility stuff for IO resources.
  1037. *
  1038. * Note how this, unlike the above, knows about
  1039. * the IO flag meanings (busy etc).
  1040. *
  1041. * request_region creates a new busy region.
  1042. *
  1043. * release_region releases a matching busy region.
  1044. */
  1045. static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
  1046. static struct inode *iomem_inode;
  1047. #ifdef CONFIG_IO_STRICT_DEVMEM
  1048. static void revoke_iomem(struct resource *res)
  1049. {
  1050. /* pairs with smp_store_release() in iomem_init_inode() */
  1051. struct inode *inode = smp_load_acquire(&iomem_inode);
  1052. /*
  1053. * Check that the initialization has completed. Losing the race
  1054. * is ok because it means drivers are claiming resources before
  1055. * the fs_initcall level of init and prevent iomem_get_mapping users
  1056. * from establishing mappings.
  1057. */
  1058. if (!inode)
  1059. return;
  1060. /*
  1061. * The expectation is that the driver has successfully marked
  1062. * the resource busy by this point, so devmem_is_allowed()
  1063. * should start returning false, however for performance this
  1064. * does not iterate the entire resource range.
  1065. */
  1066. if (devmem_is_allowed(PHYS_PFN(res->start)) &&
  1067. devmem_is_allowed(PHYS_PFN(res->end))) {
  1068. /*
  1069. * *cringe* iomem=relaxed says "go ahead, what's the
  1070. * worst that can happen?"
  1071. */
  1072. return;
  1073. }
  1074. unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
  1075. }
  1076. #else
  1077. static void revoke_iomem(struct resource *res) {}
  1078. #endif
  1079. struct address_space *iomem_get_mapping(void)
  1080. {
  1081. /*
  1082. * This function is only called from file open paths, hence guaranteed
  1083. * that fs_initcalls have completed and no need to check for NULL. But
  1084. * since revoke_iomem can be called before the initcall we still need
  1085. * the barrier to appease checkers.
  1086. */
  1087. return smp_load_acquire(&iomem_inode)->i_mapping;
  1088. }
  1089. static int __request_region_locked(struct resource *res, struct resource *parent,
  1090. resource_size_t start, resource_size_t n,
  1091. const char *name, int flags)
  1092. {
  1093. DECLARE_WAITQUEUE(wait, current);
  1094. res->name = name;
  1095. res->start = start;
  1096. res->end = start + n - 1;
  1097. for (;;) {
  1098. struct resource *conflict;
  1099. res->flags = resource_type(parent) | resource_ext_type(parent);
  1100. res->flags |= IORESOURCE_BUSY | flags;
  1101. res->desc = parent->desc;
  1102. conflict = __request_resource(parent, res);
  1103. if (!conflict)
  1104. break;
  1105. /*
  1106. * mm/hmm.c reserves physical addresses which then
  1107. * become unavailable to other users. Conflicts are
  1108. * not expected. Warn to aid debugging if encountered.
  1109. */
  1110. if (parent == &iomem_resource &&
  1111. conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
  1112. pr_warn("Unaddressable device %s %pR conflicts with %pR\n",
  1113. conflict->name, conflict, res);
  1114. }
  1115. if (conflict != parent) {
  1116. if (!(conflict->flags & IORESOURCE_BUSY)) {
  1117. parent = conflict;
  1118. continue;
  1119. }
  1120. }
  1121. if (conflict->flags & flags & IORESOURCE_MUXED) {
  1122. add_wait_queue(&muxed_resource_wait, &wait);
  1123. write_unlock(&resource_lock);
  1124. set_current_state(TASK_UNINTERRUPTIBLE);
  1125. schedule();
  1126. remove_wait_queue(&muxed_resource_wait, &wait);
  1127. write_lock(&resource_lock);
  1128. continue;
  1129. }
  1130. /* Uhhuh, that didn't work out.. */
  1131. return -EBUSY;
  1132. }
  1133. return 0;
  1134. }
  1135. /**
  1136. * __request_region - create a new busy resource region
  1137. * @parent: parent resource descriptor
  1138. * @start: resource start address
  1139. * @n: resource region size
  1140. * @name: reserving caller's ID string
  1141. * @flags: IO resource flags
  1142. */
  1143. struct resource *__request_region(struct resource *parent,
  1144. resource_size_t start, resource_size_t n,
  1145. const char *name, int flags)
  1146. {
  1147. struct resource *res = alloc_resource(GFP_KERNEL);
  1148. int ret;
  1149. if (!res)
  1150. return NULL;
  1151. write_lock(&resource_lock);
  1152. ret = __request_region_locked(res, parent, start, n, name, flags);
  1153. write_unlock(&resource_lock);
  1154. if (ret) {
  1155. free_resource(res);
  1156. return NULL;
  1157. }
  1158. if (parent == &iomem_resource)
  1159. revoke_iomem(res);
  1160. return res;
  1161. }
  1162. EXPORT_SYMBOL(__request_region);
  1163. /**
  1164. * __release_region - release a previously reserved resource region
  1165. * @parent: parent resource descriptor
  1166. * @start: resource start address
  1167. * @n: resource region size
  1168. *
  1169. * The described resource region must match a currently busy region.
  1170. */
  1171. void __release_region(struct resource *parent, resource_size_t start,
  1172. resource_size_t n)
  1173. {
  1174. struct resource **p;
  1175. resource_size_t end;
  1176. p = &parent->child;
  1177. end = start + n - 1;
  1178. write_lock(&resource_lock);
  1179. for (;;) {
  1180. struct resource *res = *p;
  1181. if (!res)
  1182. break;
  1183. if (res->start <= start && res->end >= end) {
  1184. if (!(res->flags & IORESOURCE_BUSY)) {
  1185. p = &res->child;
  1186. continue;
  1187. }
  1188. if (res->start != start || res->end != end)
  1189. break;
  1190. *p = res->sibling;
  1191. write_unlock(&resource_lock);
  1192. if (res->flags & IORESOURCE_MUXED)
  1193. wake_up(&muxed_resource_wait);
  1194. free_resource(res);
  1195. return;
  1196. }
  1197. p = &res->sibling;
  1198. }
  1199. write_unlock(&resource_lock);
  1200. pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
  1201. }
  1202. EXPORT_SYMBOL(__release_region);
  1203. #ifdef CONFIG_MEMORY_HOTREMOVE
  1204. /**
  1205. * release_mem_region_adjustable - release a previously reserved memory region
  1206. * @start: resource start address
  1207. * @size: resource region size
  1208. *
  1209. * This interface is intended for memory hot-delete. The requested region
  1210. * is released from a currently busy memory resource. The requested region
  1211. * must either match exactly or fit into a single busy resource entry. In
  1212. * the latter case, the remaining resource is adjusted accordingly.
  1213. * Existing children of the busy memory resource must be immutable in the
  1214. * request.
  1215. *
  1216. * Note:
  1217. * - Additional release conditions, such as overlapping region, can be
  1218. * supported after they are confirmed as valid cases.
  1219. * - When a busy memory resource gets split into two entries, the code
  1220. * assumes that all children remain in the lower address entry for
  1221. * simplicity. Enhance this logic when necessary.
  1222. */
  1223. void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
  1224. {
  1225. struct resource *parent = &iomem_resource;
  1226. struct resource *new_res = NULL;
  1227. bool alloc_nofail = false;
  1228. struct resource **p;
  1229. struct resource *res;
  1230. resource_size_t end;
  1231. end = start + size - 1;
  1232. if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
  1233. return;
  1234. /*
  1235. * We free up quite a lot of memory on memory hotunplug (esp., memap),
  1236. * just before releasing the region. This is highly unlikely to
  1237. * fail - let's play save and make it never fail as the caller cannot
  1238. * perform any error handling (e.g., trying to re-add memory will fail
  1239. * similarly).
  1240. */
  1241. retry:
  1242. new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
  1243. p = &parent->child;
  1244. write_lock(&resource_lock);
  1245. while ((res = *p)) {
  1246. if (res->start >= end)
  1247. break;
  1248. /* look for the next resource if it does not fit into */
  1249. if (res->start > start || res->end < end) {
  1250. p = &res->sibling;
  1251. continue;
  1252. }
  1253. if (!(res->flags & IORESOURCE_MEM))
  1254. break;
  1255. if (!(res->flags & IORESOURCE_BUSY)) {
  1256. p = &res->child;
  1257. continue;
  1258. }
  1259. /* found the target resource; let's adjust accordingly */
  1260. if (res->start == start && res->end == end) {
  1261. /* free the whole entry */
  1262. *p = res->sibling;
  1263. free_resource(res);
  1264. } else if (res->start == start && res->end != end) {
  1265. /* adjust the start */
  1266. WARN_ON_ONCE(__adjust_resource(res, end + 1,
  1267. res->end - end));
  1268. } else if (res->start != start && res->end == end) {
  1269. /* adjust the end */
  1270. WARN_ON_ONCE(__adjust_resource(res, res->start,
  1271. start - res->start));
  1272. } else {
  1273. /* split into two entries - we need a new resource */
  1274. if (!new_res) {
  1275. new_res = alloc_resource(GFP_ATOMIC);
  1276. if (!new_res) {
  1277. alloc_nofail = true;
  1278. write_unlock(&resource_lock);
  1279. goto retry;
  1280. }
  1281. }
  1282. new_res->name = res->name;
  1283. new_res->start = end + 1;
  1284. new_res->end = res->end;
  1285. new_res->flags = res->flags;
  1286. new_res->desc = res->desc;
  1287. new_res->parent = res->parent;
  1288. new_res->sibling = res->sibling;
  1289. new_res->child = NULL;
  1290. if (WARN_ON_ONCE(__adjust_resource(res, res->start,
  1291. start - res->start)))
  1292. break;
  1293. res->sibling = new_res;
  1294. new_res = NULL;
  1295. }
  1296. break;
  1297. }
  1298. write_unlock(&resource_lock);
  1299. free_resource(new_res);
  1300. }
  1301. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1302. #ifdef CONFIG_MEMORY_HOTPLUG
  1303. static bool system_ram_resources_mergeable(struct resource *r1,
  1304. struct resource *r2)
  1305. {
  1306. /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
  1307. return r1->flags == r2->flags && r1->end + 1 == r2->start &&
  1308. r1->name == r2->name && r1->desc == r2->desc &&
  1309. !r1->child && !r2->child;
  1310. }
  1311. /**
  1312. * merge_system_ram_resource - mark the System RAM resource mergeable and try to
  1313. * merge it with adjacent, mergeable resources
  1314. * @res: resource descriptor
  1315. *
  1316. * This interface is intended for memory hotplug, whereby lots of contiguous
  1317. * system ram resources are added (e.g., via add_memory*()) by a driver, and
  1318. * the actual resource boundaries are not of interest (e.g., it might be
  1319. * relevant for DIMMs). Only resources that are marked mergeable, that have the
  1320. * same parent, and that don't have any children are considered. All mergeable
  1321. * resources must be immutable during the request.
  1322. *
  1323. * Note:
  1324. * - The caller has to make sure that no pointers to resources that are
  1325. * marked mergeable are used anymore after this call - the resource might
  1326. * be freed and the pointer might be stale!
  1327. * - release_mem_region_adjustable() will split on demand on memory hotunplug
  1328. */
  1329. void merge_system_ram_resource(struct resource *res)
  1330. {
  1331. const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  1332. struct resource *cur;
  1333. if (WARN_ON_ONCE((res->flags & flags) != flags))
  1334. return;
  1335. write_lock(&resource_lock);
  1336. res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
  1337. /* Try to merge with next item in the list. */
  1338. cur = res->sibling;
  1339. if (cur && system_ram_resources_mergeable(res, cur)) {
  1340. res->end = cur->end;
  1341. res->sibling = cur->sibling;
  1342. free_resource(cur);
  1343. }
  1344. /* Try to merge with previous item in the list. */
  1345. cur = res->parent->child;
  1346. while (cur && cur->sibling != res)
  1347. cur = cur->sibling;
  1348. if (cur && system_ram_resources_mergeable(cur, res)) {
  1349. cur->end = res->end;
  1350. cur->sibling = res->sibling;
  1351. free_resource(res);
  1352. }
  1353. write_unlock(&resource_lock);
  1354. }
  1355. #endif /* CONFIG_MEMORY_HOTPLUG */
  1356. /*
  1357. * Managed region resource
  1358. */
  1359. static void devm_resource_release(struct device *dev, void *ptr)
  1360. {
  1361. struct resource **r = ptr;
  1362. release_resource(*r);
  1363. }
  1364. /**
  1365. * devm_request_resource() - request and reserve an I/O or memory resource
  1366. * @dev: device for which to request the resource
  1367. * @root: root of the resource tree from which to request the resource
  1368. * @new: descriptor of the resource to request
  1369. *
  1370. * This is a device-managed version of request_resource(). There is usually
  1371. * no need to release resources requested by this function explicitly since
  1372. * that will be taken care of when the device is unbound from its driver.
  1373. * If for some reason the resource needs to be released explicitly, because
  1374. * of ordering issues for example, drivers must call devm_release_resource()
  1375. * rather than the regular release_resource().
  1376. *
  1377. * When a conflict is detected between any existing resources and the newly
  1378. * requested resource, an error message will be printed.
  1379. *
  1380. * Returns 0 on success or a negative error code on failure.
  1381. */
  1382. int devm_request_resource(struct device *dev, struct resource *root,
  1383. struct resource *new)
  1384. {
  1385. struct resource *conflict, **ptr;
  1386. ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
  1387. if (!ptr)
  1388. return -ENOMEM;
  1389. *ptr = new;
  1390. conflict = request_resource_conflict(root, new);
  1391. if (conflict) {
  1392. dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
  1393. new, conflict->name, conflict);
  1394. devres_free(ptr);
  1395. return -EBUSY;
  1396. }
  1397. devres_add(dev, ptr);
  1398. return 0;
  1399. }
  1400. EXPORT_SYMBOL(devm_request_resource);
  1401. static int devm_resource_match(struct device *dev, void *res, void *data)
  1402. {
  1403. struct resource **ptr = res;
  1404. return *ptr == data;
  1405. }
  1406. /**
  1407. * devm_release_resource() - release a previously requested resource
  1408. * @dev: device for which to release the resource
  1409. * @new: descriptor of the resource to release
  1410. *
  1411. * Releases a resource previously requested using devm_request_resource().
  1412. */
  1413. void devm_release_resource(struct device *dev, struct resource *new)
  1414. {
  1415. WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
  1416. new));
  1417. }
  1418. EXPORT_SYMBOL(devm_release_resource);
  1419. struct region_devres {
  1420. struct resource *parent;
  1421. resource_size_t start;
  1422. resource_size_t n;
  1423. };
  1424. static void devm_region_release(struct device *dev, void *res)
  1425. {
  1426. struct region_devres *this = res;
  1427. __release_region(this->parent, this->start, this->n);
  1428. }
  1429. static int devm_region_match(struct device *dev, void *res, void *match_data)
  1430. {
  1431. struct region_devres *this = res, *match = match_data;
  1432. return this->parent == match->parent &&
  1433. this->start == match->start && this->n == match->n;
  1434. }
  1435. struct resource *
  1436. __devm_request_region(struct device *dev, struct resource *parent,
  1437. resource_size_t start, resource_size_t n, const char *name)
  1438. {
  1439. struct region_devres *dr = NULL;
  1440. struct resource *res;
  1441. dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
  1442. GFP_KERNEL);
  1443. if (!dr)
  1444. return NULL;
  1445. dr->parent = parent;
  1446. dr->start = start;
  1447. dr->n = n;
  1448. res = __request_region(parent, start, n, name, 0);
  1449. if (res)
  1450. devres_add(dev, dr);
  1451. else
  1452. devres_free(dr);
  1453. return res;
  1454. }
  1455. EXPORT_SYMBOL(__devm_request_region);
  1456. void __devm_release_region(struct device *dev, struct resource *parent,
  1457. resource_size_t start, resource_size_t n)
  1458. {
  1459. struct region_devres match_data = { parent, start, n };
  1460. __release_region(parent, start, n);
  1461. WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
  1462. &match_data));
  1463. }
  1464. EXPORT_SYMBOL(__devm_release_region);
  1465. /*
  1466. * Reserve I/O ports or memory based on "reserve=" kernel parameter.
  1467. */
  1468. #define MAXRESERVE 4
  1469. static int __init reserve_setup(char *str)
  1470. {
  1471. static int reserved;
  1472. static struct resource reserve[MAXRESERVE];
  1473. for (;;) {
  1474. unsigned int io_start, io_num;
  1475. int x = reserved;
  1476. struct resource *parent;
  1477. if (get_option(&str, &io_start) != 2)
  1478. break;
  1479. if (get_option(&str, &io_num) == 0)
  1480. break;
  1481. if (x < MAXRESERVE) {
  1482. struct resource *res = reserve + x;
  1483. /*
  1484. * If the region starts below 0x10000, we assume it's
  1485. * I/O port space; otherwise assume it's memory.
  1486. */
  1487. if (io_start < 0x10000) {
  1488. res->flags = IORESOURCE_IO;
  1489. parent = &ioport_resource;
  1490. } else {
  1491. res->flags = IORESOURCE_MEM;
  1492. parent = &iomem_resource;
  1493. }
  1494. res->name = "reserved";
  1495. res->start = io_start;
  1496. res->end = io_start + io_num - 1;
  1497. res->flags |= IORESOURCE_BUSY;
  1498. res->desc = IORES_DESC_NONE;
  1499. res->child = NULL;
  1500. if (request_resource(parent, res) == 0)
  1501. reserved = x+1;
  1502. }
  1503. }
  1504. return 1;
  1505. }
  1506. __setup("reserve=", reserve_setup);
  1507. /*
  1508. * Check if the requested addr and size spans more than any slot in the
  1509. * iomem resource tree.
  1510. */
  1511. int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
  1512. {
  1513. resource_size_t end = addr + size - 1;
  1514. struct resource *p;
  1515. int err = 0;
  1516. read_lock(&resource_lock);
  1517. for_each_resource(&iomem_resource, p, false) {
  1518. /*
  1519. * We can probably skip the resources without
  1520. * IORESOURCE_IO attribute?
  1521. */
  1522. if (p->start > end)
  1523. continue;
  1524. if (p->end < addr)
  1525. continue;
  1526. if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
  1527. PFN_DOWN(p->end) >= PFN_DOWN(end))
  1528. continue;
  1529. /*
  1530. * if a resource is "BUSY", it's not a hardware resource
  1531. * but a driver mapping of such a resource; we don't want
  1532. * to warn for those; some drivers legitimately map only
  1533. * partial hardware resources. (example: vesafb)
  1534. */
  1535. if (p->flags & IORESOURCE_BUSY)
  1536. continue;
  1537. pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
  1538. &addr, &end, p->name, p);
  1539. err = -1;
  1540. break;
  1541. }
  1542. read_unlock(&resource_lock);
  1543. return err;
  1544. }
  1545. #ifdef CONFIG_STRICT_DEVMEM
  1546. static int strict_iomem_checks = 1;
  1547. #else
  1548. static int strict_iomem_checks;
  1549. #endif
  1550. /*
  1551. * Check if an address is exclusive to the kernel and must not be mapped to
  1552. * user space, for example, via /dev/mem.
  1553. *
  1554. * Returns true if exclusive to the kernel, otherwise returns false.
  1555. */
  1556. bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
  1557. {
  1558. const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
  1559. IORESOURCE_EXCLUSIVE;
  1560. bool skip_children = false, err = false;
  1561. struct resource *p;
  1562. read_lock(&resource_lock);
  1563. for_each_resource(root, p, skip_children) {
  1564. if (p->start >= addr + size)
  1565. break;
  1566. if (p->end < addr) {
  1567. skip_children = true;
  1568. continue;
  1569. }
  1570. skip_children = false;
  1571. /*
  1572. * IORESOURCE_SYSTEM_RAM resources are exclusive if
  1573. * IORESOURCE_EXCLUSIVE is set, even if they
  1574. * are not busy and even if "iomem=relaxed" is set. The
  1575. * responsible driver dynamically adds/removes system RAM within
  1576. * such an area and uncontrolled access is dangerous.
  1577. */
  1578. if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
  1579. err = true;
  1580. break;
  1581. }
  1582. /*
  1583. * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
  1584. * or CONFIG_IO_STRICT_DEVMEM is enabled and the
  1585. * resource is busy.
  1586. */
  1587. if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
  1588. continue;
  1589. if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
  1590. || p->flags & IORESOURCE_EXCLUSIVE) {
  1591. err = true;
  1592. break;
  1593. }
  1594. }
  1595. read_unlock(&resource_lock);
  1596. return err;
  1597. }
  1598. bool iomem_is_exclusive(u64 addr)
  1599. {
  1600. return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
  1601. PAGE_SIZE);
  1602. }
  1603. struct resource_entry *resource_list_create_entry(struct resource *res,
  1604. size_t extra_size)
  1605. {
  1606. struct resource_entry *entry;
  1607. entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
  1608. if (entry) {
  1609. INIT_LIST_HEAD(&entry->node);
  1610. entry->res = res ? res : &entry->__res;
  1611. }
  1612. return entry;
  1613. }
  1614. EXPORT_SYMBOL(resource_list_create_entry);
  1615. void resource_list_free(struct list_head *head)
  1616. {
  1617. struct resource_entry *entry, *tmp;
  1618. list_for_each_entry_safe(entry, tmp, head, node)
  1619. resource_list_destroy_entry(entry);
  1620. }
  1621. EXPORT_SYMBOL(resource_list_free);
  1622. #ifdef CONFIG_GET_FREE_REGION
  1623. #define GFR_DESCENDING (1UL << 0)
  1624. #define GFR_REQUEST_REGION (1UL << 1)
  1625. #ifdef PA_SECTION_SHIFT
  1626. #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT)
  1627. #else
  1628. #define GFR_DEFAULT_ALIGN PAGE_SIZE
  1629. #endif
  1630. static resource_size_t gfr_start(struct resource *base, resource_size_t size,
  1631. resource_size_t align, unsigned long flags)
  1632. {
  1633. if (flags & GFR_DESCENDING) {
  1634. resource_size_t end;
  1635. end = min_t(resource_size_t, base->end, PHYSMEM_END);
  1636. return end - size + 1;
  1637. }
  1638. return ALIGN(max(base->start, align), align);
  1639. }
  1640. static bool gfr_continue(struct resource *base, resource_size_t addr,
  1641. resource_size_t size, unsigned long flags)
  1642. {
  1643. if (flags & GFR_DESCENDING)
  1644. return addr > size && addr >= base->start;
  1645. /*
  1646. * In the ascend case be careful that the last increment by
  1647. * @size did not wrap 0.
  1648. */
  1649. return addr > addr - size &&
  1650. addr <= min_t(resource_size_t, base->end, PHYSMEM_END);
  1651. }
  1652. static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
  1653. unsigned long flags)
  1654. {
  1655. if (flags & GFR_DESCENDING)
  1656. return addr - size;
  1657. return addr + size;
  1658. }
  1659. static void remove_free_mem_region(void *_res)
  1660. {
  1661. struct resource *res = _res;
  1662. if (res->parent)
  1663. remove_resource(res);
  1664. free_resource(res);
  1665. }
  1666. static struct resource *
  1667. get_free_mem_region(struct device *dev, struct resource *base,
  1668. resource_size_t size, const unsigned long align,
  1669. const char *name, const unsigned long desc,
  1670. const unsigned long flags)
  1671. {
  1672. resource_size_t addr;
  1673. struct resource *res;
  1674. struct region_devres *dr = NULL;
  1675. size = ALIGN(size, align);
  1676. res = alloc_resource(GFP_KERNEL);
  1677. if (!res)
  1678. return ERR_PTR(-ENOMEM);
  1679. if (dev && (flags & GFR_REQUEST_REGION)) {
  1680. dr = devres_alloc(devm_region_release,
  1681. sizeof(struct region_devres), GFP_KERNEL);
  1682. if (!dr) {
  1683. free_resource(res);
  1684. return ERR_PTR(-ENOMEM);
  1685. }
  1686. } else if (dev) {
  1687. if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
  1688. return ERR_PTR(-ENOMEM);
  1689. }
  1690. write_lock(&resource_lock);
  1691. for (addr = gfr_start(base, size, align, flags);
  1692. gfr_continue(base, addr, align, flags);
  1693. addr = gfr_next(addr, align, flags)) {
  1694. if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
  1695. REGION_DISJOINT)
  1696. continue;
  1697. if (flags & GFR_REQUEST_REGION) {
  1698. if (__request_region_locked(res, &iomem_resource, addr,
  1699. size, name, 0))
  1700. break;
  1701. if (dev) {
  1702. dr->parent = &iomem_resource;
  1703. dr->start = addr;
  1704. dr->n = size;
  1705. devres_add(dev, dr);
  1706. }
  1707. res->desc = desc;
  1708. write_unlock(&resource_lock);
  1709. /*
  1710. * A driver is claiming this region so revoke any
  1711. * mappings.
  1712. */
  1713. revoke_iomem(res);
  1714. } else {
  1715. res->start = addr;
  1716. res->end = addr + size - 1;
  1717. res->name = name;
  1718. res->desc = desc;
  1719. res->flags = IORESOURCE_MEM;
  1720. /*
  1721. * Only succeed if the resource hosts an exclusive
  1722. * range after the insert
  1723. */
  1724. if (__insert_resource(base, res) || res->child)
  1725. break;
  1726. write_unlock(&resource_lock);
  1727. }
  1728. return res;
  1729. }
  1730. write_unlock(&resource_lock);
  1731. if (flags & GFR_REQUEST_REGION) {
  1732. free_resource(res);
  1733. devres_free(dr);
  1734. } else if (dev)
  1735. devm_release_action(dev, remove_free_mem_region, res);
  1736. return ERR_PTR(-ERANGE);
  1737. }
  1738. /**
  1739. * devm_request_free_mem_region - find free region for device private memory
  1740. *
  1741. * @dev: device struct to bind the resource to
  1742. * @size: size in bytes of the device memory to add
  1743. * @base: resource tree to look in
  1744. *
  1745. * This function tries to find an empty range of physical address big enough to
  1746. * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
  1747. * memory, which in turn allocates struct pages.
  1748. */
  1749. struct resource *devm_request_free_mem_region(struct device *dev,
  1750. struct resource *base, unsigned long size)
  1751. {
  1752. unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
  1753. return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
  1754. dev_name(dev),
  1755. IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
  1756. }
  1757. EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
  1758. struct resource *request_free_mem_region(struct resource *base,
  1759. unsigned long size, const char *name)
  1760. {
  1761. unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
  1762. return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
  1763. IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
  1764. }
  1765. EXPORT_SYMBOL_GPL(request_free_mem_region);
  1766. /**
  1767. * alloc_free_mem_region - find a free region relative to @base
  1768. * @base: resource that will parent the new resource
  1769. * @size: size in bytes of memory to allocate from @base
  1770. * @align: alignment requirements for the allocation
  1771. * @name: resource name
  1772. *
  1773. * Buses like CXL, that can dynamically instantiate new memory regions,
  1774. * need a method to allocate physical address space for those regions.
  1775. * Allocate and insert a new resource to cover a free, unclaimed by a
  1776. * descendant of @base, range in the span of @base.
  1777. */
  1778. struct resource *alloc_free_mem_region(struct resource *base,
  1779. unsigned long size, unsigned long align,
  1780. const char *name)
  1781. {
  1782. /* Default of ascending direction and insert resource */
  1783. unsigned long flags = 0;
  1784. return get_free_mem_region(NULL, base, size, align, name,
  1785. IORES_DESC_NONE, flags);
  1786. }
  1787. EXPORT_SYMBOL_GPL(alloc_free_mem_region);
  1788. #endif /* CONFIG_GET_FREE_REGION */
  1789. static int __init strict_iomem(char *str)
  1790. {
  1791. if (strstr(str, "relaxed"))
  1792. strict_iomem_checks = 0;
  1793. if (strstr(str, "strict"))
  1794. strict_iomem_checks = 1;
  1795. return 1;
  1796. }
  1797. static int iomem_fs_init_fs_context(struct fs_context *fc)
  1798. {
  1799. return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
  1800. }
  1801. static struct file_system_type iomem_fs_type = {
  1802. .name = "iomem",
  1803. .owner = THIS_MODULE,
  1804. .init_fs_context = iomem_fs_init_fs_context,
  1805. .kill_sb = kill_anon_super,
  1806. };
  1807. static int __init iomem_init_inode(void)
  1808. {
  1809. static struct vfsmount *iomem_vfs_mount;
  1810. static int iomem_fs_cnt;
  1811. struct inode *inode;
  1812. int rc;
  1813. rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
  1814. if (rc < 0) {
  1815. pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
  1816. return rc;
  1817. }
  1818. inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
  1819. if (IS_ERR(inode)) {
  1820. rc = PTR_ERR(inode);
  1821. pr_err("Cannot allocate inode for iomem: %d\n", rc);
  1822. simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
  1823. return rc;
  1824. }
  1825. /*
  1826. * Publish iomem revocation inode initialized.
  1827. * Pairs with smp_load_acquire() in revoke_iomem().
  1828. */
  1829. smp_store_release(&iomem_inode, inode);
  1830. return 0;
  1831. }
  1832. fs_initcall(iomem_init_inode);
  1833. __setup("iomem=", strict_iomem);