nommu.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/mm/nommu.c
  4. *
  5. * Replacement code for mm functions to support CPU's that don't
  6. * have any form of memory management unit (thus no virtual memory).
  7. *
  8. * See Documentation/admin-guide/mm/nommu-mmap.rst
  9. *
  10. * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
  11. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  12. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  13. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  14. * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/export.h>
  18. #include <linux/mm.h>
  19. #include <linux/sched/mm.h>
  20. #include <linux/mman.h>
  21. #include <linux/swap.h>
  22. #include <linux/file.h>
  23. #include <linux/highmem.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/compiler.h>
  29. #include <linux/mount.h>
  30. #include <linux/personality.h>
  31. #include <linux/security.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/audit.h>
  34. #include <linux/printk.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/uio.h>
  37. #include <asm/tlb.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/mmu_context.h>
  40. #include "internal.h"
  41. unsigned long highest_memmap_pfn;
  42. int heap_stack_gap = 0;
  43. atomic_long_t mmap_pages_allocated;
  44. /* list of mapped, potentially shareable regions */
  45. static struct kmem_cache *vm_region_jar;
  46. struct rb_root nommu_region_tree = RB_ROOT;
  47. DECLARE_RWSEM(nommu_region_sem);
  48. const struct vm_operations_struct generic_file_vm_ops = {
  49. };
  50. /*
  51. * Return the total memory allocated for this pointer, not
  52. * just what the caller asked for.
  53. *
  54. * Doesn't have to be accurate, i.e. may have races.
  55. */
  56. unsigned int kobjsize(const void *objp)
  57. {
  58. struct folio *folio;
  59. /*
  60. * If the object we have should not have ksize performed on it,
  61. * return size of 0
  62. */
  63. if (!objp || !virt_addr_valid(objp))
  64. return 0;
  65. folio = virt_to_folio(objp);
  66. /*
  67. * If the allocator sets PageSlab, we know the pointer came from
  68. * kmalloc().
  69. */
  70. if (folio_test_slab(folio))
  71. return ksize(objp);
  72. /*
  73. * If it's not a large folio, see if we have a matching VMA
  74. * region. This test is intentionally done in reverse order,
  75. * so if there's no VMA, we still fall through and hand back
  76. * PAGE_SIZE for 0-order folios.
  77. */
  78. if (!folio_test_large(folio)) {
  79. struct vm_area_struct *vma;
  80. vma = find_vma(current->mm, (unsigned long)objp);
  81. if (vma)
  82. return vma->vm_end - vma->vm_start;
  83. }
  84. /*
  85. * The ksize() function is only guaranteed to work for pointers
  86. * returned by kmalloc(). So handle arbitrary pointers here.
  87. */
  88. return folio_size(folio);
  89. }
  90. void vfree(const void *addr)
  91. {
  92. kfree(addr);
  93. }
  94. EXPORT_SYMBOL(vfree);
  95. void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask)
  96. {
  97. /*
  98. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  99. * returns only a logical address.
  100. */
  101. return kmalloc_noprof(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  102. }
  103. EXPORT_SYMBOL(__vmalloc_noprof);
  104. void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align,
  105. gfp_t flags, int node)
  106. {
  107. return krealloc_noprof(p, size, (flags | __GFP_COMP) & ~__GFP_HIGHMEM);
  108. }
  109. void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
  110. unsigned long start, unsigned long end, gfp_t gfp_mask,
  111. pgprot_t prot, unsigned long vm_flags, int node,
  112. const void *caller)
  113. {
  114. return __vmalloc_noprof(size, gfp_mask);
  115. }
  116. void *__vmalloc_node_noprof(unsigned long size, unsigned long align, gfp_t gfp_mask,
  117. int node, const void *caller)
  118. {
  119. return __vmalloc_noprof(size, gfp_mask);
  120. }
  121. static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
  122. {
  123. void *ret;
  124. ret = __vmalloc(size, flags);
  125. if (ret) {
  126. struct vm_area_struct *vma;
  127. mmap_write_lock(current->mm);
  128. vma = find_vma(current->mm, (unsigned long)ret);
  129. if (vma)
  130. vm_flags_set(vma, VM_USERMAP);
  131. mmap_write_unlock(current->mm);
  132. }
  133. return ret;
  134. }
  135. void *vmalloc_user_noprof(unsigned long size)
  136. {
  137. return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
  138. }
  139. EXPORT_SYMBOL(vmalloc_user_noprof);
  140. struct page *vmalloc_to_page(const void *addr)
  141. {
  142. return virt_to_page(addr);
  143. }
  144. EXPORT_SYMBOL(vmalloc_to_page);
  145. unsigned long vmalloc_to_pfn(const void *addr)
  146. {
  147. return page_to_pfn(virt_to_page(addr));
  148. }
  149. EXPORT_SYMBOL(vmalloc_to_pfn);
  150. long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
  151. {
  152. /* Don't allow overflow */
  153. if ((unsigned long) addr + count < count)
  154. count = -(unsigned long) addr;
  155. return copy_to_iter(addr, count, iter);
  156. }
  157. /*
  158. * vmalloc - allocate virtually contiguous memory
  159. *
  160. * @size: allocation size
  161. *
  162. * Allocate enough pages to cover @size from the page level
  163. * allocator and map them into contiguous kernel virtual space.
  164. *
  165. * For tight control over page level allocator and protection flags
  166. * use __vmalloc() instead.
  167. */
  168. void *vmalloc_noprof(unsigned long size)
  169. {
  170. return __vmalloc_noprof(size, GFP_KERNEL);
  171. }
  172. EXPORT_SYMBOL(vmalloc_noprof);
  173. /*
  174. * vmalloc_huge_node - allocate virtually contiguous memory, on a node
  175. *
  176. * @size: allocation size
  177. * @gfp_mask: flags for the page level allocator
  178. * @node: node to use for allocation or NUMA_NO_NODE
  179. *
  180. * Allocate enough pages to cover @size from the page level
  181. * allocator and map them into contiguous kernel virtual space.
  182. *
  183. * Due to NOMMU implications the node argument and HUGE page attribute is
  184. * ignored.
  185. */
  186. void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int node)
  187. {
  188. return __vmalloc_noprof(size, gfp_mask);
  189. }
  190. /*
  191. * vzalloc - allocate virtually contiguous memory with zero fill
  192. *
  193. * @size: allocation size
  194. *
  195. * Allocate enough pages to cover @size from the page level
  196. * allocator and map them into contiguous kernel virtual space.
  197. * The memory allocated is set to zero.
  198. *
  199. * For tight control over page level allocator and protection flags
  200. * use __vmalloc() instead.
  201. */
  202. void *vzalloc_noprof(unsigned long size)
  203. {
  204. return __vmalloc_noprof(size, GFP_KERNEL | __GFP_ZERO);
  205. }
  206. EXPORT_SYMBOL(vzalloc_noprof);
  207. /**
  208. * vmalloc_node - allocate memory on a specific node
  209. * @size: allocation size
  210. * @node: numa node
  211. *
  212. * Allocate enough pages to cover @size from the page level
  213. * allocator and map them into contiguous kernel virtual space.
  214. *
  215. * For tight control over page level allocator and protection flags
  216. * use __vmalloc() instead.
  217. */
  218. void *vmalloc_node_noprof(unsigned long size, int node)
  219. {
  220. return vmalloc_noprof(size);
  221. }
  222. EXPORT_SYMBOL(vmalloc_node_noprof);
  223. /**
  224. * vzalloc_node - allocate memory on a specific node with zero fill
  225. * @size: allocation size
  226. * @node: numa node
  227. *
  228. * Allocate enough pages to cover @size from the page level
  229. * allocator and map them into contiguous kernel virtual space.
  230. * The memory allocated is set to zero.
  231. *
  232. * For tight control over page level allocator and protection flags
  233. * use __vmalloc() instead.
  234. */
  235. void *vzalloc_node_noprof(unsigned long size, int node)
  236. {
  237. return vzalloc_noprof(size);
  238. }
  239. EXPORT_SYMBOL(vzalloc_node_noprof);
  240. /**
  241. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  242. * @size: allocation size
  243. *
  244. * Allocate enough 32bit PA addressable pages to cover @size from the
  245. * page level allocator and map them into contiguous kernel virtual space.
  246. */
  247. void *vmalloc_32_noprof(unsigned long size)
  248. {
  249. return __vmalloc_noprof(size, GFP_KERNEL);
  250. }
  251. EXPORT_SYMBOL(vmalloc_32_noprof);
  252. /**
  253. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  254. * @size: allocation size
  255. *
  256. * The resulting memory area is 32bit addressable and zeroed so it can be
  257. * mapped to userspace without leaking data.
  258. *
  259. * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
  260. * remap_vmalloc_range() are permissible.
  261. */
  262. void *vmalloc_32_user_noprof(unsigned long size)
  263. {
  264. /*
  265. * We'll have to sort out the ZONE_DMA bits for 64-bit,
  266. * but for now this can simply use vmalloc_user() directly.
  267. */
  268. return vmalloc_user_noprof(size);
  269. }
  270. EXPORT_SYMBOL(vmalloc_32_user_noprof);
  271. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  272. {
  273. BUG();
  274. return NULL;
  275. }
  276. EXPORT_SYMBOL(vmap);
  277. void vunmap(const void *addr)
  278. {
  279. BUG();
  280. }
  281. EXPORT_SYMBOL(vunmap);
  282. void *vm_map_ram(struct page **pages, unsigned int count, int node)
  283. {
  284. BUG();
  285. return NULL;
  286. }
  287. EXPORT_SYMBOL(vm_map_ram);
  288. void vm_unmap_ram(const void *mem, unsigned int count)
  289. {
  290. BUG();
  291. }
  292. EXPORT_SYMBOL(vm_unmap_ram);
  293. void vm_unmap_aliases(void)
  294. {
  295. }
  296. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  297. void free_vm_area(struct vm_struct *area)
  298. {
  299. BUG();
  300. }
  301. EXPORT_SYMBOL_GPL(free_vm_area);
  302. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  303. struct page *page)
  304. {
  305. return -EINVAL;
  306. }
  307. EXPORT_SYMBOL(vm_insert_page);
  308. int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
  309. struct page **pages, unsigned long *num)
  310. {
  311. return -EINVAL;
  312. }
  313. EXPORT_SYMBOL(vm_insert_pages);
  314. int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
  315. unsigned long num)
  316. {
  317. return -EINVAL;
  318. }
  319. EXPORT_SYMBOL(vm_map_pages);
  320. int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
  321. unsigned long num)
  322. {
  323. return -EINVAL;
  324. }
  325. EXPORT_SYMBOL(vm_map_pages_zero);
  326. /*
  327. * sys_brk() for the most part doesn't need the global kernel
  328. * lock, except when an application is doing something nasty
  329. * like trying to un-brk an area that has already been mapped
  330. * to a regular file. in this case, the unmapping will need
  331. * to invoke file system routines that need the global lock.
  332. */
  333. SYSCALL_DEFINE1(brk, unsigned long, brk)
  334. {
  335. struct mm_struct *mm = current->mm;
  336. if (brk < mm->start_brk || brk > mm->context.end_brk)
  337. return mm->brk;
  338. if (mm->brk == brk)
  339. return mm->brk;
  340. /*
  341. * Always allow shrinking brk
  342. */
  343. if (brk <= mm->brk) {
  344. mm->brk = brk;
  345. return brk;
  346. }
  347. /*
  348. * Ok, looks good - let it rip.
  349. */
  350. flush_icache_user_range(mm->brk, brk);
  351. return mm->brk = brk;
  352. }
  353. static int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
  354. static const struct ctl_table nommu_table[] = {
  355. {
  356. .procname = "nr_trim_pages",
  357. .data = &sysctl_nr_trim_pages,
  358. .maxlen = sizeof(sysctl_nr_trim_pages),
  359. .mode = 0644,
  360. .proc_handler = proc_dointvec_minmax,
  361. .extra1 = SYSCTL_ZERO,
  362. },
  363. };
  364. /*
  365. * initialise the percpu counter for VM and region record slabs, initialise VMA
  366. * state.
  367. */
  368. void __init mmap_init(void)
  369. {
  370. int ret;
  371. ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
  372. VM_BUG_ON(ret);
  373. vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
  374. register_sysctl_init("vm", nommu_table);
  375. vma_state_init();
  376. }
  377. /*
  378. * validate the region tree
  379. * - the caller must hold the region lock
  380. */
  381. #ifdef CONFIG_DEBUG_NOMMU_REGIONS
  382. static noinline void validate_nommu_regions(void)
  383. {
  384. struct vm_region *region, *last;
  385. struct rb_node *p, *lastp;
  386. lastp = rb_first(&nommu_region_tree);
  387. if (!lastp)
  388. return;
  389. last = rb_entry(lastp, struct vm_region, vm_rb);
  390. BUG_ON(last->vm_end <= last->vm_start);
  391. BUG_ON(last->vm_top < last->vm_end);
  392. while ((p = rb_next(lastp))) {
  393. region = rb_entry(p, struct vm_region, vm_rb);
  394. last = rb_entry(lastp, struct vm_region, vm_rb);
  395. BUG_ON(region->vm_end <= region->vm_start);
  396. BUG_ON(region->vm_top < region->vm_end);
  397. BUG_ON(region->vm_start < last->vm_top);
  398. lastp = p;
  399. }
  400. }
  401. #else
  402. static void validate_nommu_regions(void)
  403. {
  404. }
  405. #endif
  406. /*
  407. * add a region into the global tree
  408. */
  409. static void add_nommu_region(struct vm_region *region)
  410. {
  411. struct vm_region *pregion;
  412. struct rb_node **p, *parent;
  413. validate_nommu_regions();
  414. parent = NULL;
  415. p = &nommu_region_tree.rb_node;
  416. while (*p) {
  417. parent = *p;
  418. pregion = rb_entry(parent, struct vm_region, vm_rb);
  419. if (region->vm_start < pregion->vm_start)
  420. p = &(*p)->rb_left;
  421. else if (region->vm_start > pregion->vm_start)
  422. p = &(*p)->rb_right;
  423. else if (pregion == region)
  424. return;
  425. else
  426. BUG();
  427. }
  428. rb_link_node(&region->vm_rb, parent, p);
  429. rb_insert_color(&region->vm_rb, &nommu_region_tree);
  430. validate_nommu_regions();
  431. }
  432. /*
  433. * delete a region from the global tree
  434. */
  435. static void delete_nommu_region(struct vm_region *region)
  436. {
  437. BUG_ON(!nommu_region_tree.rb_node);
  438. validate_nommu_regions();
  439. rb_erase(&region->vm_rb, &nommu_region_tree);
  440. validate_nommu_regions();
  441. }
  442. /*
  443. * free a contiguous series of pages
  444. */
  445. static void free_page_series(unsigned long from, unsigned long to)
  446. {
  447. for (; from < to; from += PAGE_SIZE) {
  448. struct page *page = virt_to_page((void *)from);
  449. atomic_long_dec(&mmap_pages_allocated);
  450. put_page(page);
  451. }
  452. }
  453. /*
  454. * release a reference to a region
  455. * - the caller must hold the region semaphore for writing, which this releases
  456. * - the region may not have been added to the tree yet, in which case vm_top
  457. * will equal vm_start
  458. */
  459. static void __put_nommu_region(struct vm_region *region)
  460. __releases(nommu_region_sem)
  461. {
  462. BUG_ON(!nommu_region_tree.rb_node);
  463. if (--region->vm_usage == 0) {
  464. if (region->vm_top > region->vm_start)
  465. delete_nommu_region(region);
  466. up_write(&nommu_region_sem);
  467. if (region->vm_file)
  468. fput(region->vm_file);
  469. /* IO memory and memory shared directly out of the pagecache
  470. * from ramfs/tmpfs mustn't be released here */
  471. if (region->vm_flags & VM_MAPPED_COPY)
  472. free_page_series(region->vm_start, region->vm_top);
  473. kmem_cache_free(vm_region_jar, region);
  474. } else {
  475. up_write(&nommu_region_sem);
  476. }
  477. }
  478. /*
  479. * release a reference to a region
  480. */
  481. static void put_nommu_region(struct vm_region *region)
  482. {
  483. down_write(&nommu_region_sem);
  484. __put_nommu_region(region);
  485. }
  486. static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)
  487. {
  488. vma->vm_mm = mm;
  489. /* add the VMA to the mapping */
  490. if (vma->vm_file) {
  491. struct address_space *mapping = vma->vm_file->f_mapping;
  492. i_mmap_lock_write(mapping);
  493. flush_dcache_mmap_lock(mapping);
  494. vma_interval_tree_insert(vma, &mapping->i_mmap);
  495. flush_dcache_mmap_unlock(mapping);
  496. i_mmap_unlock_write(mapping);
  497. }
  498. }
  499. static void cleanup_vma_from_mm(struct vm_area_struct *vma)
  500. {
  501. vma->vm_mm->map_count--;
  502. /* remove the VMA from the mapping */
  503. if (vma->vm_file) {
  504. struct address_space *mapping;
  505. mapping = vma->vm_file->f_mapping;
  506. i_mmap_lock_write(mapping);
  507. flush_dcache_mmap_lock(mapping);
  508. vma_interval_tree_remove(vma, &mapping->i_mmap);
  509. flush_dcache_mmap_unlock(mapping);
  510. i_mmap_unlock_write(mapping);
  511. }
  512. }
  513. /*
  514. * delete a VMA from its owning mm_struct and address space
  515. */
  516. static int delete_vma_from_mm(struct vm_area_struct *vma)
  517. {
  518. VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start);
  519. vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
  520. if (vma_iter_prealloc(&vmi, NULL)) {
  521. pr_warn("Allocation of vma tree for process %d failed\n",
  522. current->pid);
  523. return -ENOMEM;
  524. }
  525. cleanup_vma_from_mm(vma);
  526. /* remove from the MM's tree and list */
  527. vma_iter_clear(&vmi);
  528. return 0;
  529. }
  530. /*
  531. * destroy a VMA record
  532. */
  533. static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  534. {
  535. vma_close(vma);
  536. if (vma->vm_file)
  537. fput(vma->vm_file);
  538. put_nommu_region(vma->vm_region);
  539. vm_area_free(vma);
  540. }
  541. struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
  542. unsigned long start_addr,
  543. unsigned long end_addr)
  544. {
  545. unsigned long index = start_addr;
  546. mmap_assert_locked(mm);
  547. return mt_find(&mm->mm_mt, &index, end_addr - 1);
  548. }
  549. EXPORT_SYMBOL(find_vma_intersection);
  550. /*
  551. * look up the first VMA in which addr resides, NULL if none
  552. * - should be called with mm->mmap_lock at least held readlocked
  553. */
  554. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  555. {
  556. VMA_ITERATOR(vmi, mm, addr);
  557. return vma_iter_load(&vmi);
  558. }
  559. EXPORT_SYMBOL(find_vma);
  560. /*
  561. * expand a stack to a given address
  562. * - not supported under NOMMU conditions
  563. */
  564. int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr)
  565. {
  566. return -ENOMEM;
  567. }
  568. struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr)
  569. {
  570. mmap_read_unlock(mm);
  571. return NULL;
  572. }
  573. /*
  574. * look up the first VMA exactly that exactly matches addr
  575. * - should be called with mm->mmap_lock at least held readlocked
  576. */
  577. static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  578. unsigned long addr,
  579. unsigned long len)
  580. {
  581. struct vm_area_struct *vma;
  582. unsigned long end = addr + len;
  583. VMA_ITERATOR(vmi, mm, addr);
  584. vma = vma_iter_load(&vmi);
  585. if (!vma)
  586. return NULL;
  587. if (vma->vm_start != addr)
  588. return NULL;
  589. if (vma->vm_end != end)
  590. return NULL;
  591. return vma;
  592. }
  593. /*
  594. * determine whether a mapping should be permitted and, if so, what sort of
  595. * mapping we're capable of supporting
  596. */
  597. static int validate_mmap_request(struct file *file,
  598. unsigned long addr,
  599. unsigned long len,
  600. unsigned long prot,
  601. unsigned long flags,
  602. unsigned long pgoff,
  603. unsigned long *_capabilities)
  604. {
  605. unsigned long capabilities, rlen;
  606. int ret;
  607. /* do the simple checks first */
  608. if (flags & MAP_FIXED)
  609. return -EINVAL;
  610. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  611. (flags & MAP_TYPE) != MAP_SHARED)
  612. return -EINVAL;
  613. if (!len)
  614. return -EINVAL;
  615. /* Careful about overflows.. */
  616. rlen = PAGE_ALIGN(len);
  617. if (!rlen || rlen > TASK_SIZE)
  618. return -ENOMEM;
  619. /* offset overflow? */
  620. if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
  621. return -EOVERFLOW;
  622. if (file) {
  623. /* files must support mmap */
  624. if (!can_mmap_file(file))
  625. return -ENODEV;
  626. /* work out if what we've got could possibly be shared
  627. * - we support chardevs that provide their own "memory"
  628. * - we support files/blockdevs that are memory backed
  629. */
  630. if (file->f_op->mmap_capabilities) {
  631. capabilities = file->f_op->mmap_capabilities(file);
  632. } else {
  633. /* no explicit capabilities set, so assume some
  634. * defaults */
  635. switch (file_inode(file)->i_mode & S_IFMT) {
  636. case S_IFREG:
  637. case S_IFBLK:
  638. capabilities = NOMMU_MAP_COPY;
  639. break;
  640. case S_IFCHR:
  641. capabilities =
  642. NOMMU_MAP_DIRECT |
  643. NOMMU_MAP_READ |
  644. NOMMU_MAP_WRITE;
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. }
  650. /* eliminate any capabilities that we can't support on this
  651. * device */
  652. if (!file->f_op->get_unmapped_area)
  653. capabilities &= ~NOMMU_MAP_DIRECT;
  654. if (!(file->f_mode & FMODE_CAN_READ))
  655. capabilities &= ~NOMMU_MAP_COPY;
  656. /* The file shall have been opened with read permission. */
  657. if (!(file->f_mode & FMODE_READ))
  658. return -EACCES;
  659. if (flags & MAP_SHARED) {
  660. /* do checks for writing, appending and locking */
  661. if ((prot & PROT_WRITE) &&
  662. !(file->f_mode & FMODE_WRITE))
  663. return -EACCES;
  664. if (IS_APPEND(file_inode(file)) &&
  665. (file->f_mode & FMODE_WRITE))
  666. return -EACCES;
  667. if (!(capabilities & NOMMU_MAP_DIRECT))
  668. return -ENODEV;
  669. /* we mustn't privatise shared mappings */
  670. capabilities &= ~NOMMU_MAP_COPY;
  671. } else {
  672. /* we're going to read the file into private memory we
  673. * allocate */
  674. if (!(capabilities & NOMMU_MAP_COPY))
  675. return -ENODEV;
  676. /* we don't permit a private writable mapping to be
  677. * shared with the backing device */
  678. if (prot & PROT_WRITE)
  679. capabilities &= ~NOMMU_MAP_DIRECT;
  680. }
  681. if (capabilities & NOMMU_MAP_DIRECT) {
  682. if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
  683. ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
  684. ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
  685. ) {
  686. capabilities &= ~NOMMU_MAP_DIRECT;
  687. if (flags & MAP_SHARED) {
  688. pr_warn("MAP_SHARED not completely supported on !MMU\n");
  689. return -EINVAL;
  690. }
  691. }
  692. }
  693. /* handle executable mappings and implied executable
  694. * mappings */
  695. if (path_noexec(&file->f_path)) {
  696. if (prot & PROT_EXEC)
  697. return -EPERM;
  698. } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  699. /* handle implication of PROT_EXEC by PROT_READ */
  700. if (current->personality & READ_IMPLIES_EXEC) {
  701. if (capabilities & NOMMU_MAP_EXEC)
  702. prot |= PROT_EXEC;
  703. }
  704. } else if ((prot & PROT_READ) &&
  705. (prot & PROT_EXEC) &&
  706. !(capabilities & NOMMU_MAP_EXEC)
  707. ) {
  708. /* backing file is not executable, try to copy */
  709. capabilities &= ~NOMMU_MAP_DIRECT;
  710. }
  711. } else {
  712. /* anonymous mappings are always memory backed and can be
  713. * privately mapped
  714. */
  715. capabilities = NOMMU_MAP_COPY;
  716. /* handle PROT_EXEC implication by PROT_READ */
  717. if ((prot & PROT_READ) &&
  718. (current->personality & READ_IMPLIES_EXEC))
  719. prot |= PROT_EXEC;
  720. }
  721. /* allow the security API to have its say */
  722. ret = security_mmap_addr(addr);
  723. if (ret < 0)
  724. return ret;
  725. /* looks okay */
  726. *_capabilities = capabilities;
  727. return 0;
  728. }
  729. /*
  730. * we've determined that we can make the mapping, now translate what we
  731. * now know into VMA flags
  732. */
  733. static vm_flags_t determine_vm_flags(struct file *file,
  734. unsigned long prot,
  735. unsigned long flags,
  736. unsigned long capabilities)
  737. {
  738. vm_flags_t vm_flags;
  739. vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags);
  740. if (!file) {
  741. /*
  742. * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because
  743. * there is no fork().
  744. */
  745. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  746. } else if (flags & MAP_PRIVATE) {
  747. /* MAP_PRIVATE file mapping */
  748. if (capabilities & NOMMU_MAP_DIRECT)
  749. vm_flags |= (capabilities & NOMMU_VMFLAGS);
  750. else
  751. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  752. if (!(prot & PROT_WRITE) && !current->ptrace)
  753. /*
  754. * R/O private file mapping which cannot be used to
  755. * modify memory, especially also not via active ptrace
  756. * (e.g., set breakpoints) or later by upgrading
  757. * permissions (no mprotect()). We can try overlaying
  758. * the file mapping, which will work e.g., on chardevs,
  759. * ramfs/tmpfs/shmfs and romfs/cramf.
  760. */
  761. vm_flags |= VM_MAYOVERLAY;
  762. } else {
  763. /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */
  764. vm_flags |= VM_SHARED | VM_MAYSHARE |
  765. (capabilities & NOMMU_VMFLAGS);
  766. }
  767. return vm_flags;
  768. }
  769. /*
  770. * set up a shared mapping on a file (the driver or filesystem provides and
  771. * pins the storage)
  772. */
  773. static int do_mmap_shared_file(struct vm_area_struct *vma)
  774. {
  775. int ret;
  776. ret = mmap_file(vma->vm_file, vma);
  777. if (ret == 0) {
  778. vma->vm_region->vm_top = vma->vm_region->vm_end;
  779. return 0;
  780. }
  781. if (ret != -ENOSYS)
  782. return ret;
  783. /* getting -ENOSYS indicates that direct mmap isn't possible (as
  784. * opposed to tried but failed) so we can only give a suitable error as
  785. * it's not possible to make a private copy if MAP_SHARED was given */
  786. return -ENODEV;
  787. }
  788. /*
  789. * set up a private mapping or an anonymous shared mapping
  790. */
  791. static int do_mmap_private(struct vm_area_struct *vma,
  792. struct vm_region *region,
  793. unsigned long len,
  794. unsigned long capabilities)
  795. {
  796. unsigned long total, point;
  797. void *base;
  798. int ret, order;
  799. /*
  800. * Invoke the file's mapping function so that it can keep track of
  801. * shared mappings on devices or memory. VM_MAYOVERLAY will be set if
  802. * it may attempt to share, which will make is_nommu_shared_mapping()
  803. * happy.
  804. */
  805. if (capabilities & NOMMU_MAP_DIRECT) {
  806. ret = mmap_file(vma->vm_file, vma);
  807. /* shouldn't return success if we're not sharing */
  808. if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags)))
  809. ret = -ENOSYS;
  810. if (ret == 0) {
  811. vma->vm_region->vm_top = vma->vm_region->vm_end;
  812. return 0;
  813. }
  814. if (ret != -ENOSYS)
  815. return ret;
  816. /* getting an ENOSYS error indicates that direct mmap isn't
  817. * possible (as opposed to tried but failed) so we'll try to
  818. * make a private copy of the data and map that instead */
  819. }
  820. /* allocate some memory to hold the mapping
  821. * - note that this may not return a page-aligned address if the object
  822. * we're allocating is smaller than a page
  823. */
  824. order = get_order(len);
  825. total = 1 << order;
  826. point = len >> PAGE_SHIFT;
  827. /* we don't want to allocate a power-of-2 sized page set */
  828. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
  829. total = point;
  830. base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
  831. if (!base)
  832. goto enomem;
  833. atomic_long_add(total, &mmap_pages_allocated);
  834. vm_flags_set(vma, VM_MAPPED_COPY);
  835. region->vm_flags = vma->vm_flags;
  836. region->vm_start = (unsigned long) base;
  837. region->vm_end = region->vm_start + len;
  838. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  839. vma->vm_start = region->vm_start;
  840. vma->vm_end = region->vm_start + len;
  841. if (vma->vm_file) {
  842. /* read the contents of a file into the copy */
  843. loff_t fpos;
  844. fpos = vma->vm_pgoff;
  845. fpos <<= PAGE_SHIFT;
  846. ret = kernel_read(vma->vm_file, base, len, &fpos);
  847. if (ret < 0)
  848. goto error_free;
  849. /* clear the last little bit */
  850. if (ret < len)
  851. memset(base + ret, 0, len - ret);
  852. } else {
  853. vma_set_anonymous(vma);
  854. }
  855. return 0;
  856. error_free:
  857. free_page_series(region->vm_start, region->vm_top);
  858. region->vm_start = vma->vm_start = 0;
  859. region->vm_end = vma->vm_end = 0;
  860. region->vm_top = 0;
  861. return ret;
  862. enomem:
  863. pr_err("Allocation of length %lu from process %d (%s) failed\n",
  864. len, current->pid, current->comm);
  865. show_mem();
  866. return -ENOMEM;
  867. }
  868. /*
  869. * handle mapping creation for uClinux
  870. */
  871. unsigned long do_mmap(struct file *file,
  872. unsigned long addr,
  873. unsigned long len,
  874. unsigned long prot,
  875. unsigned long flags,
  876. vm_flags_t vm_flags,
  877. unsigned long pgoff,
  878. unsigned long *populate,
  879. struct list_head *uf)
  880. {
  881. struct vm_area_struct *vma;
  882. struct vm_region *region;
  883. struct rb_node *rb;
  884. unsigned long capabilities, result;
  885. int ret;
  886. VMA_ITERATOR(vmi, current->mm, 0);
  887. *populate = 0;
  888. /* decide whether we should attempt the mapping, and if so what sort of
  889. * mapping */
  890. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  891. &capabilities);
  892. if (ret < 0)
  893. return ret;
  894. /* we ignore the address hint */
  895. addr = 0;
  896. len = PAGE_ALIGN(len);
  897. /* we've determined that we can make the mapping, now translate what we
  898. * now know into VMA flags */
  899. vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
  900. /* we're going to need to record the mapping */
  901. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  902. if (!region)
  903. goto error_getting_region;
  904. vma = vm_area_alloc(current->mm);
  905. if (!vma)
  906. goto error_getting_vma;
  907. region->vm_usage = 1;
  908. region->vm_flags = vm_flags;
  909. region->vm_pgoff = pgoff;
  910. vm_flags_init(vma, vm_flags);
  911. vma->vm_pgoff = pgoff;
  912. if (file) {
  913. region->vm_file = get_file(file);
  914. vma->vm_file = get_file(file);
  915. }
  916. down_write(&nommu_region_sem);
  917. /* if we want to share, we need to check for regions created by other
  918. * mmap() calls that overlap with our proposed mapping
  919. * - we can only share with a superset match on most regular files
  920. * - shared mappings on character devices and memory backed files are
  921. * permitted to overlap inexactly as far as we are concerned for in
  922. * these cases, sharing is handled in the driver or filesystem rather
  923. * than here
  924. */
  925. if (is_nommu_shared_mapping(vm_flags)) {
  926. struct vm_region *pregion;
  927. unsigned long pglen, rpglen, pgend, rpgend, start;
  928. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  929. pgend = pgoff + pglen;
  930. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  931. pregion = rb_entry(rb, struct vm_region, vm_rb);
  932. if (!is_nommu_shared_mapping(pregion->vm_flags))
  933. continue;
  934. /* search for overlapping mappings on the same file */
  935. if (file_inode(pregion->vm_file) !=
  936. file_inode(file))
  937. continue;
  938. if (pregion->vm_pgoff >= pgend)
  939. continue;
  940. rpglen = pregion->vm_end - pregion->vm_start;
  941. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  942. rpgend = pregion->vm_pgoff + rpglen;
  943. if (pgoff >= rpgend)
  944. continue;
  945. /* handle inexactly overlapping matches between
  946. * mappings */
  947. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  948. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  949. /* new mapping is not a subset of the region */
  950. if (!(capabilities & NOMMU_MAP_DIRECT))
  951. goto sharing_violation;
  952. continue;
  953. }
  954. /* we've found a region we can share */
  955. pregion->vm_usage++;
  956. vma->vm_region = pregion;
  957. start = pregion->vm_start;
  958. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  959. vma->vm_start = start;
  960. vma->vm_end = start + len;
  961. if (pregion->vm_flags & VM_MAPPED_COPY)
  962. vm_flags_set(vma, VM_MAPPED_COPY);
  963. else {
  964. ret = do_mmap_shared_file(vma);
  965. if (ret < 0) {
  966. vma->vm_region = NULL;
  967. vma->vm_start = 0;
  968. vma->vm_end = 0;
  969. pregion->vm_usage--;
  970. pregion = NULL;
  971. goto error_just_free;
  972. }
  973. }
  974. fput(region->vm_file);
  975. kmem_cache_free(vm_region_jar, region);
  976. region = pregion;
  977. result = start;
  978. goto share;
  979. }
  980. /* obtain the address at which to make a shared mapping
  981. * - this is the hook for quasi-memory character devices to
  982. * tell us the location of a shared mapping
  983. */
  984. if (capabilities & NOMMU_MAP_DIRECT) {
  985. addr = file->f_op->get_unmapped_area(file, addr, len,
  986. pgoff, flags);
  987. if (IS_ERR_VALUE(addr)) {
  988. ret = addr;
  989. if (ret != -ENOSYS)
  990. goto error_just_free;
  991. /* the driver refused to tell us where to site
  992. * the mapping so we'll have to attempt to copy
  993. * it */
  994. ret = -ENODEV;
  995. if (!(capabilities & NOMMU_MAP_COPY))
  996. goto error_just_free;
  997. capabilities &= ~NOMMU_MAP_DIRECT;
  998. } else {
  999. vma->vm_start = region->vm_start = addr;
  1000. vma->vm_end = region->vm_end = addr + len;
  1001. }
  1002. }
  1003. }
  1004. vma->vm_region = region;
  1005. /* set up the mapping
  1006. * - the region is filled in if NOMMU_MAP_DIRECT is still set
  1007. */
  1008. if (file && vma->vm_flags & VM_SHARED)
  1009. ret = do_mmap_shared_file(vma);
  1010. else
  1011. ret = do_mmap_private(vma, region, len, capabilities);
  1012. if (ret < 0)
  1013. goto error_just_free;
  1014. add_nommu_region(region);
  1015. /* clear anonymous mappings that don't ask for uninitialized data */
  1016. if (!vma->vm_file &&
  1017. (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
  1018. !(flags & MAP_UNINITIALIZED)))
  1019. memset((void *)region->vm_start, 0,
  1020. region->vm_end - region->vm_start);
  1021. /* okay... we have a mapping; now we have to register it */
  1022. result = vma->vm_start;
  1023. current->mm->total_vm += len >> PAGE_SHIFT;
  1024. share:
  1025. BUG_ON(!vma->vm_region);
  1026. vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
  1027. if (vma_iter_prealloc(&vmi, vma))
  1028. goto error_just_free;
  1029. setup_vma_to_mm(vma, current->mm);
  1030. current->mm->map_count++;
  1031. /* add the VMA to the tree */
  1032. vma_iter_store_new(&vmi, vma);
  1033. /* we flush the region from the icache only when the first executable
  1034. * mapping of it is made */
  1035. if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
  1036. flush_icache_user_range(region->vm_start, region->vm_end);
  1037. region->vm_icache_flushed = true;
  1038. }
  1039. up_write(&nommu_region_sem);
  1040. return result;
  1041. error_just_free:
  1042. up_write(&nommu_region_sem);
  1043. error:
  1044. vma_iter_free(&vmi);
  1045. if (region->vm_file)
  1046. fput(region->vm_file);
  1047. kmem_cache_free(vm_region_jar, region);
  1048. if (vma->vm_file)
  1049. fput(vma->vm_file);
  1050. vm_area_free(vma);
  1051. return ret;
  1052. sharing_violation:
  1053. up_write(&nommu_region_sem);
  1054. pr_warn("Attempt to share mismatched mappings\n");
  1055. ret = -EINVAL;
  1056. goto error;
  1057. error_getting_vma:
  1058. kmem_cache_free(vm_region_jar, region);
  1059. pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
  1060. len, current->pid);
  1061. show_mem();
  1062. return -ENOMEM;
  1063. error_getting_region:
  1064. pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
  1065. len, current->pid);
  1066. show_mem();
  1067. return -ENOMEM;
  1068. }
  1069. unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
  1070. unsigned long prot, unsigned long flags,
  1071. unsigned long fd, unsigned long pgoff)
  1072. {
  1073. struct file *file = NULL;
  1074. unsigned long retval = -EBADF;
  1075. audit_mmap_fd(fd, flags);
  1076. if (!(flags & MAP_ANONYMOUS)) {
  1077. file = fget(fd);
  1078. if (!file)
  1079. goto out;
  1080. }
  1081. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1082. if (file)
  1083. fput(file);
  1084. out:
  1085. return retval;
  1086. }
  1087. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1088. unsigned long, prot, unsigned long, flags,
  1089. unsigned long, fd, unsigned long, pgoff)
  1090. {
  1091. return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
  1092. }
  1093. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1094. struct mmap_arg_struct {
  1095. unsigned long addr;
  1096. unsigned long len;
  1097. unsigned long prot;
  1098. unsigned long flags;
  1099. unsigned long fd;
  1100. unsigned long offset;
  1101. };
  1102. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1103. {
  1104. struct mmap_arg_struct a;
  1105. if (copy_from_user(&a, arg, sizeof(a)))
  1106. return -EFAULT;
  1107. if (offset_in_page(a.offset))
  1108. return -EINVAL;
  1109. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1110. a.offset >> PAGE_SHIFT);
  1111. }
  1112. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1113. /*
  1114. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1115. * for the first part or the tail.
  1116. */
  1117. static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
  1118. unsigned long addr, int new_below)
  1119. {
  1120. struct vm_area_struct *new;
  1121. struct vm_region *region;
  1122. unsigned long npages;
  1123. struct mm_struct *mm;
  1124. /* we're only permitted to split anonymous regions (these should have
  1125. * only a single usage on the region) */
  1126. if (vma->vm_file)
  1127. return -ENOMEM;
  1128. mm = vma->vm_mm;
  1129. if (mm->map_count >= sysctl_max_map_count)
  1130. return -ENOMEM;
  1131. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1132. if (!region)
  1133. return -ENOMEM;
  1134. new = vm_area_dup(vma);
  1135. if (!new)
  1136. goto err_vma_dup;
  1137. /* most fields are the same, copy all, and then fixup */
  1138. *region = *vma->vm_region;
  1139. new->vm_region = region;
  1140. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1141. if (new_below) {
  1142. region->vm_top = region->vm_end = new->vm_end = addr;
  1143. } else {
  1144. region->vm_start = new->vm_start = addr;
  1145. region->vm_pgoff = new->vm_pgoff += npages;
  1146. }
  1147. vma_iter_config(vmi, new->vm_start, new->vm_end);
  1148. if (vma_iter_prealloc(vmi, vma)) {
  1149. pr_warn("Allocation of vma tree for process %d failed\n",
  1150. current->pid);
  1151. goto err_vmi_preallocate;
  1152. }
  1153. if (new->vm_ops && new->vm_ops->open)
  1154. new->vm_ops->open(new);
  1155. down_write(&nommu_region_sem);
  1156. delete_nommu_region(vma->vm_region);
  1157. if (new_below) {
  1158. vma->vm_region->vm_start = vma->vm_start = addr;
  1159. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1160. } else {
  1161. vma->vm_region->vm_end = vma->vm_end = addr;
  1162. vma->vm_region->vm_top = addr;
  1163. }
  1164. add_nommu_region(vma->vm_region);
  1165. add_nommu_region(new->vm_region);
  1166. up_write(&nommu_region_sem);
  1167. setup_vma_to_mm(vma, mm);
  1168. setup_vma_to_mm(new, mm);
  1169. vma_iter_store_new(vmi, new);
  1170. mm->map_count++;
  1171. return 0;
  1172. err_vmi_preallocate:
  1173. vm_area_free(new);
  1174. err_vma_dup:
  1175. kmem_cache_free(vm_region_jar, region);
  1176. return -ENOMEM;
  1177. }
  1178. /*
  1179. * shrink a VMA by removing the specified chunk from either the beginning or
  1180. * the end
  1181. */
  1182. static int vmi_shrink_vma(struct vma_iterator *vmi,
  1183. struct vm_area_struct *vma,
  1184. unsigned long from, unsigned long to)
  1185. {
  1186. struct vm_region *region;
  1187. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1188. * and list */
  1189. if (from > vma->vm_start) {
  1190. if (vma_iter_clear_gfp(vmi, from, vma->vm_end, GFP_KERNEL))
  1191. return -ENOMEM;
  1192. vma->vm_end = from;
  1193. } else {
  1194. if (vma_iter_clear_gfp(vmi, vma->vm_start, to, GFP_KERNEL))
  1195. return -ENOMEM;
  1196. vma->vm_start = to;
  1197. }
  1198. /* cut the backing region down to size */
  1199. region = vma->vm_region;
  1200. BUG_ON(region->vm_usage != 1);
  1201. down_write(&nommu_region_sem);
  1202. delete_nommu_region(region);
  1203. if (from > region->vm_start) {
  1204. to = region->vm_top;
  1205. region->vm_top = region->vm_end = from;
  1206. } else {
  1207. region->vm_start = to;
  1208. }
  1209. add_nommu_region(region);
  1210. up_write(&nommu_region_sem);
  1211. free_page_series(from, to);
  1212. return 0;
  1213. }
  1214. /*
  1215. * release a mapping
  1216. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1217. * VMA, though it need not cover the whole VMA
  1218. */
  1219. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
  1220. {
  1221. VMA_ITERATOR(vmi, mm, start);
  1222. struct vm_area_struct *vma;
  1223. unsigned long end;
  1224. int ret = 0;
  1225. len = PAGE_ALIGN(len);
  1226. if (len == 0)
  1227. return -EINVAL;
  1228. end = start + len;
  1229. /* find the first potentially overlapping VMA */
  1230. vma = vma_find(&vmi, end);
  1231. if (!vma) {
  1232. static int limit;
  1233. if (limit < 5) {
  1234. pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
  1235. current->pid, current->comm,
  1236. start, start + len - 1);
  1237. limit++;
  1238. }
  1239. return -EINVAL;
  1240. }
  1241. /* we're allowed to split an anonymous VMA but not a file-backed one */
  1242. if (vma->vm_file) {
  1243. do {
  1244. if (start > vma->vm_start)
  1245. return -EINVAL;
  1246. if (end == vma->vm_end)
  1247. goto erase_whole_vma;
  1248. vma = vma_find(&vmi, end);
  1249. } while (vma);
  1250. return -EINVAL;
  1251. } else {
  1252. /* the chunk must be a subset of the VMA found */
  1253. if (start == vma->vm_start && end == vma->vm_end)
  1254. goto erase_whole_vma;
  1255. if (start < vma->vm_start || end > vma->vm_end)
  1256. return -EINVAL;
  1257. if (offset_in_page(start))
  1258. return -EINVAL;
  1259. if (end != vma->vm_end && offset_in_page(end))
  1260. return -EINVAL;
  1261. if (start != vma->vm_start && end != vma->vm_end) {
  1262. ret = split_vma(&vmi, vma, start, 1);
  1263. if (ret < 0)
  1264. return ret;
  1265. }
  1266. return vmi_shrink_vma(&vmi, vma, start, end);
  1267. }
  1268. erase_whole_vma:
  1269. if (delete_vma_from_mm(vma))
  1270. ret = -ENOMEM;
  1271. else
  1272. delete_vma(mm, vma);
  1273. return ret;
  1274. }
  1275. int vm_munmap(unsigned long addr, size_t len)
  1276. {
  1277. struct mm_struct *mm = current->mm;
  1278. int ret;
  1279. mmap_write_lock(mm);
  1280. ret = do_munmap(mm, addr, len, NULL);
  1281. mmap_write_unlock(mm);
  1282. return ret;
  1283. }
  1284. EXPORT_SYMBOL(vm_munmap);
  1285. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1286. {
  1287. return vm_munmap(addr, len);
  1288. }
  1289. /*
  1290. * release all the mappings made in a process's VM space
  1291. */
  1292. void exit_mmap(struct mm_struct *mm)
  1293. {
  1294. VMA_ITERATOR(vmi, mm, 0);
  1295. struct vm_area_struct *vma;
  1296. if (!mm)
  1297. return;
  1298. mm->total_vm = 0;
  1299. /*
  1300. * Lock the mm to avoid assert complaining even though this is the only
  1301. * user of the mm
  1302. */
  1303. mmap_write_lock(mm);
  1304. for_each_vma(vmi, vma) {
  1305. cleanup_vma_from_mm(vma);
  1306. delete_vma(mm, vma);
  1307. cond_resched();
  1308. }
  1309. __mt_destroy(&mm->mm_mt);
  1310. mmap_write_unlock(mm);
  1311. }
  1312. /*
  1313. * expand (or shrink) an existing mapping, potentially moving it at the same
  1314. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  1315. *
  1316. * under NOMMU conditions, we only permit changing a mapping's size, and only
  1317. * as long as it stays within the region allocated by do_mmap_private() and the
  1318. * block is not shareable
  1319. *
  1320. * MREMAP_FIXED is not supported under NOMMU conditions
  1321. */
  1322. static unsigned long do_mremap(unsigned long addr,
  1323. unsigned long old_len, unsigned long new_len,
  1324. unsigned long flags, unsigned long new_addr)
  1325. {
  1326. struct vm_area_struct *vma;
  1327. /* insanity checks first */
  1328. old_len = PAGE_ALIGN(old_len);
  1329. new_len = PAGE_ALIGN(new_len);
  1330. if (old_len == 0 || new_len == 0)
  1331. return (unsigned long) -EINVAL;
  1332. if (offset_in_page(addr))
  1333. return -EINVAL;
  1334. if (flags & MREMAP_FIXED && new_addr != addr)
  1335. return (unsigned long) -EINVAL;
  1336. vma = find_vma_exact(current->mm, addr, old_len);
  1337. if (!vma)
  1338. return (unsigned long) -EINVAL;
  1339. if (vma->vm_end != vma->vm_start + old_len)
  1340. return (unsigned long) -EFAULT;
  1341. if (is_nommu_shared_mapping(vma->vm_flags))
  1342. return (unsigned long) -EPERM;
  1343. if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
  1344. return (unsigned long) -ENOMEM;
  1345. /* all checks complete - do it */
  1346. vma->vm_end = vma->vm_start + new_len;
  1347. return vma->vm_start;
  1348. }
  1349. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  1350. unsigned long, new_len, unsigned long, flags,
  1351. unsigned long, new_addr)
  1352. {
  1353. unsigned long ret;
  1354. mmap_write_lock(current->mm);
  1355. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1356. mmap_write_unlock(current->mm);
  1357. return ret;
  1358. }
  1359. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1360. unsigned long pfn, unsigned long size, pgprot_t prot)
  1361. {
  1362. if (addr != (pfn << PAGE_SHIFT))
  1363. return -EINVAL;
  1364. vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
  1365. return 0;
  1366. }
  1367. EXPORT_SYMBOL(remap_pfn_range);
  1368. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
  1369. {
  1370. unsigned long pfn = start >> PAGE_SHIFT;
  1371. unsigned long vm_len = vma->vm_end - vma->vm_start;
  1372. pfn += vma->vm_pgoff;
  1373. return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
  1374. }
  1375. EXPORT_SYMBOL(vm_iomap_memory);
  1376. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1377. unsigned long pgoff)
  1378. {
  1379. unsigned int size = vma->vm_end - vma->vm_start;
  1380. if (!(vma->vm_flags & VM_USERMAP))
  1381. return -EINVAL;
  1382. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1383. vma->vm_end = vma->vm_start + size;
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL(remap_vmalloc_range);
  1387. vm_fault_t filemap_fault(struct vm_fault *vmf)
  1388. {
  1389. BUG();
  1390. return 0;
  1391. }
  1392. EXPORT_SYMBOL(filemap_fault);
  1393. vm_fault_t filemap_map_pages(struct vm_fault *vmf,
  1394. pgoff_t start_pgoff, pgoff_t end_pgoff)
  1395. {
  1396. BUG();
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(filemap_map_pages);
  1400. static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1401. void *buf, int len, unsigned int gup_flags)
  1402. {
  1403. struct vm_area_struct *vma;
  1404. int write = gup_flags & FOLL_WRITE;
  1405. if (mmap_read_lock_killable(mm))
  1406. return 0;
  1407. /* the access must start within one of the target process's mappings */
  1408. vma = find_vma(mm, addr);
  1409. if (vma) {
  1410. /* don't overrun this mapping */
  1411. if (addr + len >= vma->vm_end)
  1412. len = vma->vm_end - addr;
  1413. /* only read or write mappings where it is permitted */
  1414. if (write && vma->vm_flags & VM_MAYWRITE)
  1415. copy_to_user_page(vma, NULL, addr,
  1416. (void *) addr, buf, len);
  1417. else if (!write && vma->vm_flags & VM_MAYREAD)
  1418. copy_from_user_page(vma, NULL, addr,
  1419. buf, (void *) addr, len);
  1420. else
  1421. len = 0;
  1422. } else {
  1423. len = 0;
  1424. }
  1425. mmap_read_unlock(mm);
  1426. return len;
  1427. }
  1428. /**
  1429. * access_remote_vm - access another process' address space
  1430. * @mm: the mm_struct of the target address space
  1431. * @addr: start address to access
  1432. * @buf: source or destination buffer
  1433. * @len: number of bytes to transfer
  1434. * @gup_flags: flags modifying lookup behaviour
  1435. *
  1436. * The caller must hold a reference on @mm.
  1437. */
  1438. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1439. void *buf, int len, unsigned int gup_flags)
  1440. {
  1441. return __access_remote_vm(mm, addr, buf, len, gup_flags);
  1442. }
  1443. /*
  1444. * Access another process' address space.
  1445. * - source/target buffer must be kernel space
  1446. */
  1447. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
  1448. unsigned int gup_flags)
  1449. {
  1450. struct mm_struct *mm;
  1451. if (addr + len < addr)
  1452. return 0;
  1453. mm = get_task_mm(tsk);
  1454. if (!mm)
  1455. return 0;
  1456. len = __access_remote_vm(mm, addr, buf, len, gup_flags);
  1457. mmput(mm);
  1458. return len;
  1459. }
  1460. EXPORT_SYMBOL_GPL(access_process_vm);
  1461. #ifdef CONFIG_BPF_SYSCALL
  1462. /*
  1463. * Copy a string from another process's address space as given in mm.
  1464. * If there is any error return -EFAULT.
  1465. */
  1466. static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr,
  1467. void *buf, int len)
  1468. {
  1469. unsigned long addr_end;
  1470. struct vm_area_struct *vma;
  1471. int ret = -EFAULT;
  1472. *(char *)buf = '\0';
  1473. if (mmap_read_lock_killable(mm))
  1474. return ret;
  1475. /* the access must start within one of the target process's mappings */
  1476. vma = find_vma(mm, addr);
  1477. if (!vma)
  1478. goto out;
  1479. if (check_add_overflow(addr, len, &addr_end))
  1480. goto out;
  1481. /* don't overrun this mapping */
  1482. if (addr_end > vma->vm_end)
  1483. len = vma->vm_end - addr;
  1484. /* only read mappings where it is permitted */
  1485. if (vma->vm_flags & VM_MAYREAD) {
  1486. ret = strscpy(buf, (char *)addr, len);
  1487. if (ret < 0)
  1488. ret = len - 1;
  1489. }
  1490. out:
  1491. mmap_read_unlock(mm);
  1492. return ret;
  1493. }
  1494. /**
  1495. * copy_remote_vm_str - copy a string from another process's address space.
  1496. * @tsk: the task of the target address space
  1497. * @addr: start address to read from
  1498. * @buf: destination buffer
  1499. * @len: number of bytes to copy
  1500. * @gup_flags: flags modifying lookup behaviour (unused)
  1501. *
  1502. * The caller must hold a reference on @mm.
  1503. *
  1504. * Return: number of bytes copied from @addr (source) to @buf (destination);
  1505. * not including the trailing NUL. Always guaranteed to leave NUL-terminated
  1506. * buffer. On any error, return -EFAULT.
  1507. */
  1508. int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr,
  1509. void *buf, int len, unsigned int gup_flags)
  1510. {
  1511. struct mm_struct *mm;
  1512. int ret;
  1513. if (unlikely(len == 0))
  1514. return 0;
  1515. mm = get_task_mm(tsk);
  1516. if (!mm) {
  1517. *(char *)buf = '\0';
  1518. return -EFAULT;
  1519. }
  1520. ret = __copy_remote_vm_str(mm, addr, buf, len);
  1521. mmput(mm);
  1522. return ret;
  1523. }
  1524. EXPORT_SYMBOL_GPL(copy_remote_vm_str);
  1525. #endif /* CONFIG_BPF_SYSCALL */
  1526. /**
  1527. * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
  1528. * @inode: The inode to check
  1529. * @size: The current filesize of the inode
  1530. * @newsize: The proposed filesize of the inode
  1531. *
  1532. * Check the shared mappings on an inode on behalf of a shrinking truncate to
  1533. * make sure that any outstanding VMAs aren't broken and then shrink the
  1534. * vm_regions that extend beyond so that do_mmap() doesn't
  1535. * automatically grant mappings that are too large.
  1536. */
  1537. int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
  1538. size_t newsize)
  1539. {
  1540. struct vm_area_struct *vma;
  1541. struct vm_region *region;
  1542. pgoff_t low, high;
  1543. size_t r_size, r_top;
  1544. low = newsize >> PAGE_SHIFT;
  1545. high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1546. down_write(&nommu_region_sem);
  1547. i_mmap_lock_read(inode->i_mapping);
  1548. /* search for VMAs that fall within the dead zone */
  1549. vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
  1550. /* found one - only interested if it's shared out of the page
  1551. * cache */
  1552. if (vma->vm_flags & VM_SHARED) {
  1553. i_mmap_unlock_read(inode->i_mapping);
  1554. up_write(&nommu_region_sem);
  1555. return -ETXTBSY; /* not quite true, but near enough */
  1556. }
  1557. }
  1558. /* reduce any regions that overlap the dead zone - if in existence,
  1559. * these will be pointed to by VMAs that don't overlap the dead zone
  1560. *
  1561. * we don't check for any regions that start beyond the EOF as there
  1562. * shouldn't be any
  1563. */
  1564. vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
  1565. if (!(vma->vm_flags & VM_SHARED))
  1566. continue;
  1567. region = vma->vm_region;
  1568. r_size = region->vm_top - region->vm_start;
  1569. r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
  1570. if (r_top > newsize) {
  1571. region->vm_top -= r_top - newsize;
  1572. if (region->vm_end > region->vm_top)
  1573. region->vm_end = region->vm_top;
  1574. }
  1575. }
  1576. i_mmap_unlock_read(inode->i_mapping);
  1577. up_write(&nommu_region_sem);
  1578. return 0;
  1579. }
  1580. /*
  1581. * Initialise sysctl_user_reserve_kbytes.
  1582. *
  1583. * This is intended to prevent a user from starting a single memory hogging
  1584. * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
  1585. * mode.
  1586. *
  1587. * The default value is min(3% of free memory, 128MB)
  1588. * 128MB is enough to recover with sshd/login, bash, and top/kill.
  1589. */
  1590. static int __meminit init_user_reserve(void)
  1591. {
  1592. unsigned long free_kbytes;
  1593. free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
  1594. sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
  1595. return 0;
  1596. }
  1597. subsys_initcall(init_user_reserve);
  1598. /*
  1599. * Initialise sysctl_admin_reserve_kbytes.
  1600. *
  1601. * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
  1602. * to log in and kill a memory hogging process.
  1603. *
  1604. * Systems with more than 256MB will reserve 8MB, enough to recover
  1605. * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
  1606. * only reserve 3% of free pages by default.
  1607. */
  1608. static int __meminit init_admin_reserve(void)
  1609. {
  1610. unsigned long free_kbytes;
  1611. free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
  1612. sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
  1613. return 0;
  1614. }
  1615. subsys_initcall(init_admin_reserve);
  1616. int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  1617. {
  1618. mmap_write_lock(oldmm);
  1619. dup_mm_exe_file(mm, oldmm);
  1620. mmap_write_unlock(oldmm);
  1621. return 0;
  1622. }