nommu.c 45 KB

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