nommu.c 48 KB

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