mmap.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * mm/mmap.c
  3. *
  4. * Written by obz.
  5. *
  6. * Address space accounting code <alan@redhat.com>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. #include <linux/shm.h>
  11. #include <linux/mman.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/swap.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/capability.h>
  16. #include <linux/init.h>
  17. #include <linux/file.h>
  18. #include <linux/fs.h>
  19. #include <linux/personality.h>
  20. #include <linux/security.h>
  21. #include <linux/hugetlb.h>
  22. #include <linux/profile.h>
  23. #include <linux/module.h>
  24. #include <linux/mount.h>
  25. #include <linux/mempolicy.h>
  26. #include <linux/rmap.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/tlb.h>
  30. #ifndef arch_mmap_check
  31. #define arch_mmap_check(addr, len, flags) (0)
  32. #endif
  33. static void unmap_region(struct mm_struct *mm,
  34. struct vm_area_struct *vma, struct vm_area_struct *prev,
  35. unsigned long start, unsigned long end);
  36. /*
  37. * WARNING: the debugging will use recursive algorithms so never enable this
  38. * unless you know what you are doing.
  39. */
  40. #undef DEBUG_MM_RB
  41. /* description of effects of mapping type and prot in current implementation.
  42. * this is due to the limited x86 page protection hardware. The expected
  43. * behavior is in parens:
  44. *
  45. * map_type prot
  46. * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
  47. * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  48. * w: (no) no w: (no) no w: (yes) yes w: (no) no
  49. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  50. *
  51. * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  52. * w: (no) no w: (no) no w: (copy) copy w: (no) no
  53. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  54. *
  55. */
  56. pgprot_t protection_map[16] = {
  57. __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  58. __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  59. };
  60. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  61. {
  62. return protection_map[vm_flags &
  63. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
  64. }
  65. EXPORT_SYMBOL(vm_get_page_prot);
  66. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  67. int sysctl_overcommit_ratio = 50; /* default is 50% */
  68. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  69. atomic_t vm_committed_space = ATOMIC_INIT(0);
  70. /*
  71. * Check that a process has enough memory to allocate a new virtual
  72. * mapping. 0 means there is enough memory for the allocation to
  73. * succeed and -ENOMEM implies there is not.
  74. *
  75. * We currently support three overcommit policies, which are set via the
  76. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  77. *
  78. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  79. * Additional code 2002 Jul 20 by Robert Love.
  80. *
  81. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  82. *
  83. * Note this is a helper function intended to be used by LSMs which
  84. * wish to use this logic.
  85. */
  86. int __vm_enough_memory(long pages, int cap_sys_admin)
  87. {
  88. unsigned long free, allowed;
  89. vm_acct_memory(pages);
  90. /*
  91. * Sometimes we want to use more memory than we have
  92. */
  93. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  94. return 0;
  95. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  96. unsigned long n;
  97. free = global_page_state(NR_FILE_PAGES);
  98. free += nr_swap_pages;
  99. /*
  100. * Any slabs which are created with the
  101. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  102. * which are reclaimable, under pressure. The dentry
  103. * cache and most inode caches should fall into this
  104. */
  105. free += global_page_state(NR_SLAB_RECLAIMABLE);
  106. /*
  107. * Leave the last 3% for root
  108. */
  109. if (!cap_sys_admin)
  110. free -= free / 32;
  111. if (free > pages)
  112. return 0;
  113. /*
  114. * nr_free_pages() is very expensive on large systems,
  115. * only call if we're about to fail.
  116. */
  117. n = nr_free_pages();
  118. /*
  119. * Leave reserved pages. The pages are not for anonymous pages.
  120. */
  121. if (n <= totalreserve_pages)
  122. goto error;
  123. else
  124. n -= totalreserve_pages;
  125. /*
  126. * Leave the last 3% for root
  127. */
  128. if (!cap_sys_admin)
  129. n -= n / 32;
  130. free += n;
  131. if (free > pages)
  132. return 0;
  133. goto error;
  134. }
  135. allowed = (totalram_pages - hugetlb_total_pages())
  136. * sysctl_overcommit_ratio / 100;
  137. /*
  138. * Leave the last 3% for root
  139. */
  140. if (!cap_sys_admin)
  141. allowed -= allowed / 32;
  142. allowed += total_swap_pages;
  143. /* Don't let a single process grow too big:
  144. leave 3% of the size of this process for other processes */
  145. allowed -= current->mm->total_vm / 32;
  146. /*
  147. * cast `allowed' as a signed long because vm_committed_space
  148. * sometimes has a negative value
  149. */
  150. if (atomic_read(&vm_committed_space) < (long)allowed)
  151. return 0;
  152. error:
  153. vm_unacct_memory(pages);
  154. return -ENOMEM;
  155. }
  156. EXPORT_SYMBOL(__vm_enough_memory);
  157. /*
  158. * Requires inode->i_mapping->i_mmap_lock
  159. */
  160. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  161. struct file *file, struct address_space *mapping)
  162. {
  163. if (vma->vm_flags & VM_DENYWRITE)
  164. atomic_inc(&file->f_dentry->d_inode->i_writecount);
  165. if (vma->vm_flags & VM_SHARED)
  166. mapping->i_mmap_writable--;
  167. flush_dcache_mmap_lock(mapping);
  168. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  169. list_del_init(&vma->shared.vm_set.list);
  170. else
  171. vma_prio_tree_remove(vma, &mapping->i_mmap);
  172. flush_dcache_mmap_unlock(mapping);
  173. }
  174. /*
  175. * Unlink a file-based vm structure from its prio_tree, to hide
  176. * vma from rmap and vmtruncate before freeing its page tables.
  177. */
  178. void unlink_file_vma(struct vm_area_struct *vma)
  179. {
  180. struct file *file = vma->vm_file;
  181. if (file) {
  182. struct address_space *mapping = file->f_mapping;
  183. spin_lock(&mapping->i_mmap_lock);
  184. __remove_shared_vm_struct(vma, file, mapping);
  185. spin_unlock(&mapping->i_mmap_lock);
  186. }
  187. }
  188. /*
  189. * Close a vm structure and free it, returning the next.
  190. */
  191. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  192. {
  193. struct vm_area_struct *next = vma->vm_next;
  194. might_sleep();
  195. if (vma->vm_ops && vma->vm_ops->close)
  196. vma->vm_ops->close(vma);
  197. if (vma->vm_file)
  198. fput(vma->vm_file);
  199. mpol_free(vma_policy(vma));
  200. kmem_cache_free(vm_area_cachep, vma);
  201. return next;
  202. }
  203. asmlinkage unsigned long sys_brk(unsigned long brk)
  204. {
  205. unsigned long rlim, retval;
  206. unsigned long newbrk, oldbrk;
  207. struct mm_struct *mm = current->mm;
  208. down_write(&mm->mmap_sem);
  209. if (brk < mm->end_code)
  210. goto out;
  211. /*
  212. * Check against rlimit here. If this check is done later after the test
  213. * of oldbrk with newbrk then it can escape the test and let the data
  214. * segment grow beyond its set limit the in case where the limit is
  215. * not page aligned -Ram Gupta
  216. */
  217. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  218. if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
  219. goto out;
  220. newbrk = PAGE_ALIGN(brk);
  221. oldbrk = PAGE_ALIGN(mm->brk);
  222. if (oldbrk == newbrk)
  223. goto set_brk;
  224. /* Always allow shrinking brk. */
  225. if (brk <= mm->brk) {
  226. if (!do_munmap(mm, newbrk, oldbrk-newbrk))
  227. goto set_brk;
  228. goto out;
  229. }
  230. /* Check against existing mmap mappings. */
  231. if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
  232. goto out;
  233. /* Ok, looks good - let it rip. */
  234. if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
  235. goto out;
  236. set_brk:
  237. mm->brk = brk;
  238. out:
  239. retval = mm->brk;
  240. up_write(&mm->mmap_sem);
  241. return retval;
  242. }
  243. #ifdef DEBUG_MM_RB
  244. static int browse_rb(struct rb_root *root)
  245. {
  246. int i = 0, j;
  247. struct rb_node *nd, *pn = NULL;
  248. unsigned long prev = 0, pend = 0;
  249. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  250. struct vm_area_struct *vma;
  251. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  252. if (vma->vm_start < prev)
  253. printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
  254. if (vma->vm_start < pend)
  255. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  256. if (vma->vm_start > vma->vm_end)
  257. printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
  258. i++;
  259. pn = nd;
  260. }
  261. j = 0;
  262. for (nd = pn; nd; nd = rb_prev(nd)) {
  263. j++;
  264. }
  265. if (i != j)
  266. printk("backwards %d, forwards %d\n", j, i), i = 0;
  267. return i;
  268. }
  269. void validate_mm(struct mm_struct *mm)
  270. {
  271. int bug = 0;
  272. int i = 0;
  273. struct vm_area_struct *tmp = mm->mmap;
  274. while (tmp) {
  275. tmp = tmp->vm_next;
  276. i++;
  277. }
  278. if (i != mm->map_count)
  279. printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
  280. i = browse_rb(&mm->mm_rb);
  281. if (i != mm->map_count)
  282. printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
  283. BUG_ON(bug);
  284. }
  285. #else
  286. #define validate_mm(mm) do { } while (0)
  287. #endif
  288. static struct vm_area_struct *
  289. find_vma_prepare(struct mm_struct *mm, unsigned long addr,
  290. struct vm_area_struct **pprev, struct rb_node ***rb_link,
  291. struct rb_node ** rb_parent)
  292. {
  293. struct vm_area_struct * vma;
  294. struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
  295. __rb_link = &mm->mm_rb.rb_node;
  296. rb_prev = __rb_parent = NULL;
  297. vma = NULL;
  298. while (*__rb_link) {
  299. struct vm_area_struct *vma_tmp;
  300. __rb_parent = *__rb_link;
  301. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  302. if (vma_tmp->vm_end > addr) {
  303. vma = vma_tmp;
  304. if (vma_tmp->vm_start <= addr)
  305. return vma;
  306. __rb_link = &__rb_parent->rb_left;
  307. } else {
  308. rb_prev = __rb_parent;
  309. __rb_link = &__rb_parent->rb_right;
  310. }
  311. }
  312. *pprev = NULL;
  313. if (rb_prev)
  314. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  315. *rb_link = __rb_link;
  316. *rb_parent = __rb_parent;
  317. return vma;
  318. }
  319. static inline void
  320. __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  321. struct vm_area_struct *prev, struct rb_node *rb_parent)
  322. {
  323. if (prev) {
  324. vma->vm_next = prev->vm_next;
  325. prev->vm_next = vma;
  326. } else {
  327. mm->mmap = vma;
  328. if (rb_parent)
  329. vma->vm_next = rb_entry(rb_parent,
  330. struct vm_area_struct, vm_rb);
  331. else
  332. vma->vm_next = NULL;
  333. }
  334. }
  335. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  336. struct rb_node **rb_link, struct rb_node *rb_parent)
  337. {
  338. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  339. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  340. }
  341. static inline void __vma_link_file(struct vm_area_struct *vma)
  342. {
  343. struct file * file;
  344. file = vma->vm_file;
  345. if (file) {
  346. struct address_space *mapping = file->f_mapping;
  347. if (vma->vm_flags & VM_DENYWRITE)
  348. atomic_dec(&file->f_dentry->d_inode->i_writecount);
  349. if (vma->vm_flags & VM_SHARED)
  350. mapping->i_mmap_writable++;
  351. flush_dcache_mmap_lock(mapping);
  352. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  353. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  354. else
  355. vma_prio_tree_insert(vma, &mapping->i_mmap);
  356. flush_dcache_mmap_unlock(mapping);
  357. }
  358. }
  359. static void
  360. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  361. struct vm_area_struct *prev, struct rb_node **rb_link,
  362. struct rb_node *rb_parent)
  363. {
  364. __vma_link_list(mm, vma, prev, rb_parent);
  365. __vma_link_rb(mm, vma, rb_link, rb_parent);
  366. __anon_vma_link(vma);
  367. }
  368. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  369. struct vm_area_struct *prev, struct rb_node **rb_link,
  370. struct rb_node *rb_parent)
  371. {
  372. struct address_space *mapping = NULL;
  373. if (vma->vm_file)
  374. mapping = vma->vm_file->f_mapping;
  375. if (mapping) {
  376. spin_lock(&mapping->i_mmap_lock);
  377. vma->vm_truncate_count = mapping->truncate_count;
  378. }
  379. anon_vma_lock(vma);
  380. __vma_link(mm, vma, prev, rb_link, rb_parent);
  381. __vma_link_file(vma);
  382. anon_vma_unlock(vma);
  383. if (mapping)
  384. spin_unlock(&mapping->i_mmap_lock);
  385. mm->map_count++;
  386. validate_mm(mm);
  387. }
  388. /*
  389. * Helper for vma_adjust in the split_vma insert case:
  390. * insert vm structure into list and rbtree and anon_vma,
  391. * but it has already been inserted into prio_tree earlier.
  392. */
  393. static void
  394. __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  395. {
  396. struct vm_area_struct * __vma, * prev;
  397. struct rb_node ** rb_link, * rb_parent;
  398. __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
  399. BUG_ON(__vma && __vma->vm_start < vma->vm_end);
  400. __vma_link(mm, vma, prev, rb_link, rb_parent);
  401. mm->map_count++;
  402. }
  403. static inline void
  404. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  405. struct vm_area_struct *prev)
  406. {
  407. prev->vm_next = vma->vm_next;
  408. rb_erase(&vma->vm_rb, &mm->mm_rb);
  409. if (mm->mmap_cache == vma)
  410. mm->mmap_cache = prev;
  411. }
  412. /*
  413. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  414. * is already present in an i_mmap tree without adjusting the tree.
  415. * The following helper function should be used when such adjustments
  416. * are necessary. The "insert" vma (if any) is to be inserted
  417. * before we drop the necessary locks.
  418. */
  419. void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  420. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  421. {
  422. struct mm_struct *mm = vma->vm_mm;
  423. struct vm_area_struct *next = vma->vm_next;
  424. struct vm_area_struct *importer = NULL;
  425. struct address_space *mapping = NULL;
  426. struct prio_tree_root *root = NULL;
  427. struct file *file = vma->vm_file;
  428. struct anon_vma *anon_vma = NULL;
  429. long adjust_next = 0;
  430. int remove_next = 0;
  431. if (next && !insert) {
  432. if (end >= next->vm_end) {
  433. /*
  434. * vma expands, overlapping all the next, and
  435. * perhaps the one after too (mprotect case 6).
  436. */
  437. again: remove_next = 1 + (end > next->vm_end);
  438. end = next->vm_end;
  439. anon_vma = next->anon_vma;
  440. importer = vma;
  441. } else if (end > next->vm_start) {
  442. /*
  443. * vma expands, overlapping part of the next:
  444. * mprotect case 5 shifting the boundary up.
  445. */
  446. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  447. anon_vma = next->anon_vma;
  448. importer = vma;
  449. } else if (end < vma->vm_end) {
  450. /*
  451. * vma shrinks, and !insert tells it's not
  452. * split_vma inserting another: so it must be
  453. * mprotect case 4 shifting the boundary down.
  454. */
  455. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  456. anon_vma = next->anon_vma;
  457. importer = next;
  458. }
  459. }
  460. if (file) {
  461. mapping = file->f_mapping;
  462. if (!(vma->vm_flags & VM_NONLINEAR))
  463. root = &mapping->i_mmap;
  464. spin_lock(&mapping->i_mmap_lock);
  465. if (importer &&
  466. vma->vm_truncate_count != next->vm_truncate_count) {
  467. /*
  468. * unmap_mapping_range might be in progress:
  469. * ensure that the expanding vma is rescanned.
  470. */
  471. importer->vm_truncate_count = 0;
  472. }
  473. if (insert) {
  474. insert->vm_truncate_count = vma->vm_truncate_count;
  475. /*
  476. * Put into prio_tree now, so instantiated pages
  477. * are visible to arm/parisc __flush_dcache_page
  478. * throughout; but we cannot insert into address
  479. * space until vma start or end is updated.
  480. */
  481. __vma_link_file(insert);
  482. }
  483. }
  484. /*
  485. * When changing only vma->vm_end, we don't really need
  486. * anon_vma lock: but is that case worth optimizing out?
  487. */
  488. if (vma->anon_vma)
  489. anon_vma = vma->anon_vma;
  490. if (anon_vma) {
  491. spin_lock(&anon_vma->lock);
  492. /*
  493. * Easily overlooked: when mprotect shifts the boundary,
  494. * make sure the expanding vma has anon_vma set if the
  495. * shrinking vma had, to cover any anon pages imported.
  496. */
  497. if (importer && !importer->anon_vma) {
  498. importer->anon_vma = anon_vma;
  499. __anon_vma_link(importer);
  500. }
  501. }
  502. if (root) {
  503. flush_dcache_mmap_lock(mapping);
  504. vma_prio_tree_remove(vma, root);
  505. if (adjust_next)
  506. vma_prio_tree_remove(next, root);
  507. }
  508. vma->vm_start = start;
  509. vma->vm_end = end;
  510. vma->vm_pgoff = pgoff;
  511. if (adjust_next) {
  512. next->vm_start += adjust_next << PAGE_SHIFT;
  513. next->vm_pgoff += adjust_next;
  514. }
  515. if (root) {
  516. if (adjust_next)
  517. vma_prio_tree_insert(next, root);
  518. vma_prio_tree_insert(vma, root);
  519. flush_dcache_mmap_unlock(mapping);
  520. }
  521. if (remove_next) {
  522. /*
  523. * vma_merge has merged next into vma, and needs
  524. * us to remove next before dropping the locks.
  525. */
  526. __vma_unlink(mm, next, vma);
  527. if (file)
  528. __remove_shared_vm_struct(next, file, mapping);
  529. if (next->anon_vma)
  530. __anon_vma_merge(vma, next);
  531. } else if (insert) {
  532. /*
  533. * split_vma has split insert from vma, and needs
  534. * us to insert it before dropping the locks
  535. * (it may either follow vma or precede it).
  536. */
  537. __insert_vm_struct(mm, insert);
  538. }
  539. if (anon_vma)
  540. spin_unlock(&anon_vma->lock);
  541. if (mapping)
  542. spin_unlock(&mapping->i_mmap_lock);
  543. if (remove_next) {
  544. if (file)
  545. fput(file);
  546. mm->map_count--;
  547. mpol_free(vma_policy(next));
  548. kmem_cache_free(vm_area_cachep, next);
  549. /*
  550. * In mprotect's case 6 (see comments on vma_merge),
  551. * we must remove another next too. It would clutter
  552. * up the code too much to do both in one go.
  553. */
  554. if (remove_next == 2) {
  555. next = vma->vm_next;
  556. goto again;
  557. }
  558. }
  559. validate_mm(mm);
  560. }
  561. /*
  562. * If the vma has a ->close operation then the driver probably needs to release
  563. * per-vma resources, so we don't attempt to merge those.
  564. */
  565. #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
  566. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  567. struct file *file, unsigned long vm_flags)
  568. {
  569. if (vma->vm_flags != vm_flags)
  570. return 0;
  571. if (vma->vm_file != file)
  572. return 0;
  573. if (vma->vm_ops && vma->vm_ops->close)
  574. return 0;
  575. return 1;
  576. }
  577. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  578. struct anon_vma *anon_vma2)
  579. {
  580. return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
  581. }
  582. /*
  583. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  584. * in front of (at a lower virtual address and file offset than) the vma.
  585. *
  586. * We cannot merge two vmas if they have differently assigned (non-NULL)
  587. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  588. *
  589. * We don't check here for the merged mmap wrapping around the end of pagecache
  590. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  591. * wrap, nor mmaps which cover the final page at index -1UL.
  592. */
  593. static int
  594. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  595. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  596. {
  597. if (is_mergeable_vma(vma, file, vm_flags) &&
  598. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  599. if (vma->vm_pgoff == vm_pgoff)
  600. return 1;
  601. }
  602. return 0;
  603. }
  604. /*
  605. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  606. * beyond (at a higher virtual address and file offset than) the vma.
  607. *
  608. * We cannot merge two vmas if they have differently assigned (non-NULL)
  609. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  610. */
  611. static int
  612. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  613. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  614. {
  615. if (is_mergeable_vma(vma, file, vm_flags) &&
  616. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  617. pgoff_t vm_pglen;
  618. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  619. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  620. return 1;
  621. }
  622. return 0;
  623. }
  624. /*
  625. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  626. * whether that can be merged with its predecessor or its successor.
  627. * Or both (it neatly fills a hole).
  628. *
  629. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  630. * certain not to be mapped by the time vma_merge is called; but when
  631. * called for mprotect, it is certain to be already mapped (either at
  632. * an offset within prev, or at the start of next), and the flags of
  633. * this area are about to be changed to vm_flags - and the no-change
  634. * case has already been eliminated.
  635. *
  636. * The following mprotect cases have to be considered, where AAAA is
  637. * the area passed down from mprotect_fixup, never extending beyond one
  638. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  639. *
  640. * AAAA AAAA AAAA AAAA
  641. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  642. * cannot merge might become might become might become
  643. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  644. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  645. * mremap move: PPPPNNNNNNNN 8
  646. * AAAA
  647. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  648. * might become case 1 below case 2 below case 3 below
  649. *
  650. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  651. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  652. */
  653. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  654. struct vm_area_struct *prev, unsigned long addr,
  655. unsigned long end, unsigned long vm_flags,
  656. struct anon_vma *anon_vma, struct file *file,
  657. pgoff_t pgoff, struct mempolicy *policy)
  658. {
  659. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  660. struct vm_area_struct *area, *next;
  661. /*
  662. * We later require that vma->vm_flags == vm_flags,
  663. * so this tests vma->vm_flags & VM_SPECIAL, too.
  664. */
  665. if (vm_flags & VM_SPECIAL)
  666. return NULL;
  667. if (prev)
  668. next = prev->vm_next;
  669. else
  670. next = mm->mmap;
  671. area = next;
  672. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  673. next = next->vm_next;
  674. /*
  675. * Can it merge with the predecessor?
  676. */
  677. if (prev && prev->vm_end == addr &&
  678. mpol_equal(vma_policy(prev), policy) &&
  679. can_vma_merge_after(prev, vm_flags,
  680. anon_vma, file, pgoff)) {
  681. /*
  682. * OK, it can. Can we now merge in the successor as well?
  683. */
  684. if (next && end == next->vm_start &&
  685. mpol_equal(policy, vma_policy(next)) &&
  686. can_vma_merge_before(next, vm_flags,
  687. anon_vma, file, pgoff+pglen) &&
  688. is_mergeable_anon_vma(prev->anon_vma,
  689. next->anon_vma)) {
  690. /* cases 1, 6 */
  691. vma_adjust(prev, prev->vm_start,
  692. next->vm_end, prev->vm_pgoff, NULL);
  693. } else /* cases 2, 5, 7 */
  694. vma_adjust(prev, prev->vm_start,
  695. end, prev->vm_pgoff, NULL);
  696. return prev;
  697. }
  698. /*
  699. * Can this new request be merged in front of next?
  700. */
  701. if (next && end == next->vm_start &&
  702. mpol_equal(policy, vma_policy(next)) &&
  703. can_vma_merge_before(next, vm_flags,
  704. anon_vma, file, pgoff+pglen)) {
  705. if (prev && addr < prev->vm_end) /* case 4 */
  706. vma_adjust(prev, prev->vm_start,
  707. addr, prev->vm_pgoff, NULL);
  708. else /* cases 3, 8 */
  709. vma_adjust(area, addr, next->vm_end,
  710. next->vm_pgoff - pglen, NULL);
  711. return area;
  712. }
  713. return NULL;
  714. }
  715. /*
  716. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  717. * neighbouring vmas for a suitable anon_vma, before it goes off
  718. * to allocate a new anon_vma. It checks because a repetitive
  719. * sequence of mprotects and faults may otherwise lead to distinct
  720. * anon_vmas being allocated, preventing vma merge in subsequent
  721. * mprotect.
  722. */
  723. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  724. {
  725. struct vm_area_struct *near;
  726. unsigned long vm_flags;
  727. near = vma->vm_next;
  728. if (!near)
  729. goto try_prev;
  730. /*
  731. * Since only mprotect tries to remerge vmas, match flags
  732. * which might be mprotected into each other later on.
  733. * Neither mlock nor madvise tries to remerge at present,
  734. * so leave their flags as obstructing a merge.
  735. */
  736. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  737. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  738. if (near->anon_vma && vma->vm_end == near->vm_start &&
  739. mpol_equal(vma_policy(vma), vma_policy(near)) &&
  740. can_vma_merge_before(near, vm_flags,
  741. NULL, vma->vm_file, vma->vm_pgoff +
  742. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
  743. return near->anon_vma;
  744. try_prev:
  745. /*
  746. * It is potentially slow to have to call find_vma_prev here.
  747. * But it's only on the first write fault on the vma, not
  748. * every time, and we could devise a way to avoid it later
  749. * (e.g. stash info in next's anon_vma_node when assigning
  750. * an anon_vma, or when trying vma_merge). Another time.
  751. */
  752. BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
  753. if (!near)
  754. goto none;
  755. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  756. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  757. if (near->anon_vma && near->vm_end == vma->vm_start &&
  758. mpol_equal(vma_policy(near), vma_policy(vma)) &&
  759. can_vma_merge_after(near, vm_flags,
  760. NULL, vma->vm_file, vma->vm_pgoff))
  761. return near->anon_vma;
  762. none:
  763. /*
  764. * There's no absolute need to look only at touching neighbours:
  765. * we could search further afield for "compatible" anon_vmas.
  766. * But it would probably just be a waste of time searching,
  767. * or lead to too many vmas hanging off the same anon_vma.
  768. * We're trying to allow mprotect remerging later on,
  769. * not trying to minimize memory used for anon_vmas.
  770. */
  771. return NULL;
  772. }
  773. #ifdef CONFIG_PROC_FS
  774. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  775. struct file *file, long pages)
  776. {
  777. const unsigned long stack_flags
  778. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  779. if (file) {
  780. mm->shared_vm += pages;
  781. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  782. mm->exec_vm += pages;
  783. } else if (flags & stack_flags)
  784. mm->stack_vm += pages;
  785. if (flags & (VM_RESERVED|VM_IO))
  786. mm->reserved_vm += pages;
  787. }
  788. #endif /* CONFIG_PROC_FS */
  789. /*
  790. * The caller must hold down_write(current->mm->mmap_sem).
  791. */
  792. unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
  793. unsigned long len, unsigned long prot,
  794. unsigned long flags, unsigned long pgoff)
  795. {
  796. struct mm_struct * mm = current->mm;
  797. struct vm_area_struct * vma, * prev;
  798. struct inode *inode;
  799. unsigned int vm_flags;
  800. int correct_wcount = 0;
  801. int error;
  802. struct rb_node ** rb_link, * rb_parent;
  803. int accountable = 1;
  804. unsigned long charged = 0, reqprot = prot;
  805. /*
  806. * Does the application expect PROT_READ to imply PROT_EXEC?
  807. *
  808. * (the exception is when the underlying filesystem is noexec
  809. * mounted, in which case we dont add PROT_EXEC.)
  810. */
  811. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  812. if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
  813. prot |= PROT_EXEC;
  814. if (!len)
  815. return -EINVAL;
  816. error = arch_mmap_check(addr, len, flags);
  817. if (error)
  818. return error;
  819. /* Careful about overflows.. */
  820. len = PAGE_ALIGN(len);
  821. if (!len || len > TASK_SIZE)
  822. return -ENOMEM;
  823. /* offset overflow? */
  824. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  825. return -EOVERFLOW;
  826. /* Too many mappings? */
  827. if (mm->map_count > sysctl_max_map_count)
  828. return -ENOMEM;
  829. /* Obtain the address to map to. we verify (or select) it and ensure
  830. * that it represents a valid section of the address space.
  831. */
  832. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  833. if (addr & ~PAGE_MASK)
  834. return addr;
  835. /* Do simple checking here so the lower-level routines won't have
  836. * to. we assume access permissions have been handled by the open
  837. * of the memory object, so we don't do any here.
  838. */
  839. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  840. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  841. if (flags & MAP_LOCKED) {
  842. if (!can_do_mlock())
  843. return -EPERM;
  844. vm_flags |= VM_LOCKED;
  845. }
  846. /* mlock MCL_FUTURE? */
  847. if (vm_flags & VM_LOCKED) {
  848. unsigned long locked, lock_limit;
  849. locked = len >> PAGE_SHIFT;
  850. locked += mm->locked_vm;
  851. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  852. lock_limit >>= PAGE_SHIFT;
  853. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  854. return -EAGAIN;
  855. }
  856. inode = file ? file->f_dentry->d_inode : NULL;
  857. if (file) {
  858. switch (flags & MAP_TYPE) {
  859. case MAP_SHARED:
  860. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  861. return -EACCES;
  862. /*
  863. * Make sure we don't allow writing to an append-only
  864. * file..
  865. */
  866. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  867. return -EACCES;
  868. /*
  869. * Make sure there are no mandatory locks on the file.
  870. */
  871. if (locks_verify_locked(inode))
  872. return -EAGAIN;
  873. vm_flags |= VM_SHARED | VM_MAYSHARE;
  874. if (!(file->f_mode & FMODE_WRITE))
  875. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  876. /* fall through */
  877. case MAP_PRIVATE:
  878. if (!(file->f_mode & FMODE_READ))
  879. return -EACCES;
  880. if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
  881. if (vm_flags & VM_EXEC)
  882. return -EPERM;
  883. vm_flags &= ~VM_MAYEXEC;
  884. }
  885. if (is_file_hugepages(file))
  886. accountable = 0;
  887. if (!file->f_op || !file->f_op->mmap)
  888. return -ENODEV;
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. } else {
  894. switch (flags & MAP_TYPE) {
  895. case MAP_SHARED:
  896. vm_flags |= VM_SHARED | VM_MAYSHARE;
  897. break;
  898. case MAP_PRIVATE:
  899. /*
  900. * Set pgoff according to addr for anon_vma.
  901. */
  902. pgoff = addr >> PAGE_SHIFT;
  903. break;
  904. default:
  905. return -EINVAL;
  906. }
  907. }
  908. error = security_file_mmap(file, reqprot, prot, flags);
  909. if (error)
  910. return error;
  911. /* Clear old maps */
  912. error = -ENOMEM;
  913. munmap_back:
  914. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  915. if (vma && vma->vm_start < addr + len) {
  916. if (do_munmap(mm, addr, len))
  917. return -ENOMEM;
  918. goto munmap_back;
  919. }
  920. /* Check against address space limit. */
  921. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  922. return -ENOMEM;
  923. if (accountable && (!(flags & MAP_NORESERVE) ||
  924. sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
  925. if (vm_flags & VM_SHARED) {
  926. /* Check memory availability in shmem_file_setup? */
  927. vm_flags |= VM_ACCOUNT;
  928. } else if (vm_flags & VM_WRITE) {
  929. /*
  930. * Private writable mapping: check memory availability
  931. */
  932. charged = len >> PAGE_SHIFT;
  933. if (security_vm_enough_memory(charged))
  934. return -ENOMEM;
  935. vm_flags |= VM_ACCOUNT;
  936. }
  937. }
  938. /*
  939. * Can we just expand an old private anonymous mapping?
  940. * The VM_SHARED test is necessary because shmem_zero_setup
  941. * will create the file object for a shared anonymous map below.
  942. */
  943. if (!file && !(vm_flags & VM_SHARED) &&
  944. vma_merge(mm, prev, addr, addr + len, vm_flags,
  945. NULL, NULL, pgoff, NULL))
  946. goto out;
  947. /*
  948. * Determine the object being mapped and call the appropriate
  949. * specific mapper. the address has already been validated, but
  950. * not unmapped, but the maps are removed from the list.
  951. */
  952. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  953. if (!vma) {
  954. error = -ENOMEM;
  955. goto unacct_error;
  956. }
  957. vma->vm_mm = mm;
  958. vma->vm_start = addr;
  959. vma->vm_end = addr + len;
  960. vma->vm_flags = vm_flags;
  961. vma->vm_page_prot = protection_map[vm_flags &
  962. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
  963. vma->vm_pgoff = pgoff;
  964. if (file) {
  965. error = -EINVAL;
  966. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  967. goto free_vma;
  968. if (vm_flags & VM_DENYWRITE) {
  969. error = deny_write_access(file);
  970. if (error)
  971. goto free_vma;
  972. correct_wcount = 1;
  973. }
  974. vma->vm_file = file;
  975. get_file(file);
  976. error = file->f_op->mmap(file, vma);
  977. if (error)
  978. goto unmap_and_free_vma;
  979. } else if (vm_flags & VM_SHARED) {
  980. error = shmem_zero_setup(vma);
  981. if (error)
  982. goto free_vma;
  983. }
  984. /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
  985. * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
  986. * that memory reservation must be checked; but that reservation
  987. * belongs to shared memory object, not to vma: so now clear it.
  988. */
  989. if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
  990. vma->vm_flags &= ~VM_ACCOUNT;
  991. /* Can addr have changed??
  992. *
  993. * Answer: Yes, several device drivers can do it in their
  994. * f_op->mmap method. -DaveM
  995. */
  996. addr = vma->vm_start;
  997. pgoff = vma->vm_pgoff;
  998. vm_flags = vma->vm_flags;
  999. if (vma_wants_writenotify(vma))
  1000. vma->vm_page_prot =
  1001. protection_map[vm_flags & (VM_READ|VM_WRITE|VM_EXEC)];
  1002. if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
  1003. vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
  1004. file = vma->vm_file;
  1005. vma_link(mm, vma, prev, rb_link, rb_parent);
  1006. if (correct_wcount)
  1007. atomic_inc(&inode->i_writecount);
  1008. } else {
  1009. if (file) {
  1010. if (correct_wcount)
  1011. atomic_inc(&inode->i_writecount);
  1012. fput(file);
  1013. }
  1014. mpol_free(vma_policy(vma));
  1015. kmem_cache_free(vm_area_cachep, vma);
  1016. }
  1017. out:
  1018. mm->total_vm += len >> PAGE_SHIFT;
  1019. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1020. if (vm_flags & VM_LOCKED) {
  1021. mm->locked_vm += len >> PAGE_SHIFT;
  1022. make_pages_present(addr, addr + len);
  1023. }
  1024. if (flags & MAP_POPULATE) {
  1025. up_write(&mm->mmap_sem);
  1026. sys_remap_file_pages(addr, len, 0,
  1027. pgoff, flags & MAP_NONBLOCK);
  1028. down_write(&mm->mmap_sem);
  1029. }
  1030. return addr;
  1031. unmap_and_free_vma:
  1032. if (correct_wcount)
  1033. atomic_inc(&inode->i_writecount);
  1034. vma->vm_file = NULL;
  1035. fput(file);
  1036. /* Undo any partial mapping done by a device driver. */
  1037. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1038. charged = 0;
  1039. free_vma:
  1040. kmem_cache_free(vm_area_cachep, vma);
  1041. unacct_error:
  1042. if (charged)
  1043. vm_unacct_memory(charged);
  1044. return error;
  1045. }
  1046. EXPORT_SYMBOL(do_mmap_pgoff);
  1047. /* Get an address range which is currently unmapped.
  1048. * For shmat() with addr=0.
  1049. *
  1050. * Ugly calling convention alert:
  1051. * Return value with the low bits set means error value,
  1052. * ie
  1053. * if (ret & ~PAGE_MASK)
  1054. * error = ret;
  1055. *
  1056. * This function "knows" that -ENOMEM has the bits set.
  1057. */
  1058. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1059. unsigned long
  1060. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1061. unsigned long len, unsigned long pgoff, unsigned long flags)
  1062. {
  1063. struct mm_struct *mm = current->mm;
  1064. struct vm_area_struct *vma;
  1065. unsigned long start_addr;
  1066. if (len > TASK_SIZE)
  1067. return -ENOMEM;
  1068. if (addr) {
  1069. addr = PAGE_ALIGN(addr);
  1070. vma = find_vma(mm, addr);
  1071. if (TASK_SIZE - len >= addr &&
  1072. (!vma || addr + len <= vma->vm_start))
  1073. return addr;
  1074. }
  1075. if (len > mm->cached_hole_size) {
  1076. start_addr = addr = mm->free_area_cache;
  1077. } else {
  1078. start_addr = addr = TASK_UNMAPPED_BASE;
  1079. mm->cached_hole_size = 0;
  1080. }
  1081. full_search:
  1082. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  1083. /* At this point: (!vma || addr < vma->vm_end). */
  1084. if (TASK_SIZE - len < addr) {
  1085. /*
  1086. * Start a new search - just in case we missed
  1087. * some holes.
  1088. */
  1089. if (start_addr != TASK_UNMAPPED_BASE) {
  1090. addr = TASK_UNMAPPED_BASE;
  1091. start_addr = addr;
  1092. mm->cached_hole_size = 0;
  1093. goto full_search;
  1094. }
  1095. return -ENOMEM;
  1096. }
  1097. if (!vma || addr + len <= vma->vm_start) {
  1098. /*
  1099. * Remember the place where we stopped the search:
  1100. */
  1101. mm->free_area_cache = addr + len;
  1102. return addr;
  1103. }
  1104. if (addr + mm->cached_hole_size < vma->vm_start)
  1105. mm->cached_hole_size = vma->vm_start - addr;
  1106. addr = vma->vm_end;
  1107. }
  1108. }
  1109. #endif
  1110. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1111. {
  1112. /*
  1113. * Is this a new hole at the lowest possible address?
  1114. */
  1115. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
  1116. mm->free_area_cache = addr;
  1117. mm->cached_hole_size = ~0UL;
  1118. }
  1119. }
  1120. /*
  1121. * This mmap-allocator allocates new areas top-down from below the
  1122. * stack's low limit (the base):
  1123. */
  1124. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1125. unsigned long
  1126. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1127. const unsigned long len, const unsigned long pgoff,
  1128. const unsigned long flags)
  1129. {
  1130. struct vm_area_struct *vma;
  1131. struct mm_struct *mm = current->mm;
  1132. unsigned long addr = addr0;
  1133. /* requested length too big for entire address space */
  1134. if (len > TASK_SIZE)
  1135. return -ENOMEM;
  1136. /* requesting a specific address */
  1137. if (addr) {
  1138. addr = PAGE_ALIGN(addr);
  1139. vma = find_vma(mm, addr);
  1140. if (TASK_SIZE - len >= addr &&
  1141. (!vma || addr + len <= vma->vm_start))
  1142. return addr;
  1143. }
  1144. /* check if free_area_cache is useful for us */
  1145. if (len <= mm->cached_hole_size) {
  1146. mm->cached_hole_size = 0;
  1147. mm->free_area_cache = mm->mmap_base;
  1148. }
  1149. /* either no address requested or can't fit in requested address hole */
  1150. addr = mm->free_area_cache;
  1151. /* make sure it can fit in the remaining address space */
  1152. if (addr > len) {
  1153. vma = find_vma(mm, addr-len);
  1154. if (!vma || addr <= vma->vm_start)
  1155. /* remember the address as a hint for next time */
  1156. return (mm->free_area_cache = addr-len);
  1157. }
  1158. if (mm->mmap_base < len)
  1159. goto bottomup;
  1160. addr = mm->mmap_base-len;
  1161. do {
  1162. /*
  1163. * Lookup failure means no vma is above this address,
  1164. * else if new region fits below vma->vm_start,
  1165. * return with success:
  1166. */
  1167. vma = find_vma(mm, addr);
  1168. if (!vma || addr+len <= vma->vm_start)
  1169. /* remember the address as a hint for next time */
  1170. return (mm->free_area_cache = addr);
  1171. /* remember the largest hole we saw so far */
  1172. if (addr + mm->cached_hole_size < vma->vm_start)
  1173. mm->cached_hole_size = vma->vm_start - addr;
  1174. /* try just below the current vma->vm_start */
  1175. addr = vma->vm_start-len;
  1176. } while (len < vma->vm_start);
  1177. bottomup:
  1178. /*
  1179. * A failed mmap() very likely causes application failure,
  1180. * so fall back to the bottom-up function here. This scenario
  1181. * can happen with large stack limits and large mmap()
  1182. * allocations.
  1183. */
  1184. mm->cached_hole_size = ~0UL;
  1185. mm->free_area_cache = TASK_UNMAPPED_BASE;
  1186. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  1187. /*
  1188. * Restore the topdown base:
  1189. */
  1190. mm->free_area_cache = mm->mmap_base;
  1191. mm->cached_hole_size = ~0UL;
  1192. return addr;
  1193. }
  1194. #endif
  1195. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1196. {
  1197. /*
  1198. * Is this a new hole at the highest possible address?
  1199. */
  1200. if (addr > mm->free_area_cache)
  1201. mm->free_area_cache = addr;
  1202. /* dont allow allocations above current base */
  1203. if (mm->free_area_cache > mm->mmap_base)
  1204. mm->free_area_cache = mm->mmap_base;
  1205. }
  1206. unsigned long
  1207. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1208. unsigned long pgoff, unsigned long flags)
  1209. {
  1210. unsigned long ret;
  1211. if (!(flags & MAP_FIXED)) {
  1212. unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  1213. get_area = current->mm->get_unmapped_area;
  1214. if (file && file->f_op && file->f_op->get_unmapped_area)
  1215. get_area = file->f_op->get_unmapped_area;
  1216. addr = get_area(file, addr, len, pgoff, flags);
  1217. if (IS_ERR_VALUE(addr))
  1218. return addr;
  1219. }
  1220. if (addr > TASK_SIZE - len)
  1221. return -ENOMEM;
  1222. if (addr & ~PAGE_MASK)
  1223. return -EINVAL;
  1224. if (file && is_file_hugepages(file)) {
  1225. /*
  1226. * Check if the given range is hugepage aligned, and
  1227. * can be made suitable for hugepages.
  1228. */
  1229. ret = prepare_hugepage_range(addr, len, pgoff);
  1230. } else {
  1231. /*
  1232. * Ensure that a normal request is not falling in a
  1233. * reserved hugepage range. For some archs like IA-64,
  1234. * there is a separate region for hugepages.
  1235. */
  1236. ret = is_hugepage_only_range(current->mm, addr, len);
  1237. }
  1238. if (ret)
  1239. return -EINVAL;
  1240. return addr;
  1241. }
  1242. EXPORT_SYMBOL(get_unmapped_area);
  1243. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1244. struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
  1245. {
  1246. struct vm_area_struct *vma = NULL;
  1247. if (mm) {
  1248. /* Check the cache first. */
  1249. /* (Cache hit rate is typically around 35%.) */
  1250. vma = mm->mmap_cache;
  1251. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1252. struct rb_node * rb_node;
  1253. rb_node = mm->mm_rb.rb_node;
  1254. vma = NULL;
  1255. while (rb_node) {
  1256. struct vm_area_struct * vma_tmp;
  1257. vma_tmp = rb_entry(rb_node,
  1258. struct vm_area_struct, vm_rb);
  1259. if (vma_tmp->vm_end > addr) {
  1260. vma = vma_tmp;
  1261. if (vma_tmp->vm_start <= addr)
  1262. break;
  1263. rb_node = rb_node->rb_left;
  1264. } else
  1265. rb_node = rb_node->rb_right;
  1266. }
  1267. if (vma)
  1268. mm->mmap_cache = vma;
  1269. }
  1270. }
  1271. return vma;
  1272. }
  1273. EXPORT_SYMBOL(find_vma);
  1274. /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
  1275. struct vm_area_struct *
  1276. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1277. struct vm_area_struct **pprev)
  1278. {
  1279. struct vm_area_struct *vma = NULL, *prev = NULL;
  1280. struct rb_node * rb_node;
  1281. if (!mm)
  1282. goto out;
  1283. /* Guard against addr being lower than the first VMA */
  1284. vma = mm->mmap;
  1285. /* Go through the RB tree quickly. */
  1286. rb_node = mm->mm_rb.rb_node;
  1287. while (rb_node) {
  1288. struct vm_area_struct *vma_tmp;
  1289. vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1290. if (addr < vma_tmp->vm_end) {
  1291. rb_node = rb_node->rb_left;
  1292. } else {
  1293. prev = vma_tmp;
  1294. if (!prev->vm_next || (addr < prev->vm_next->vm_end))
  1295. break;
  1296. rb_node = rb_node->rb_right;
  1297. }
  1298. }
  1299. out:
  1300. *pprev = prev;
  1301. return prev ? prev->vm_next : vma;
  1302. }
  1303. /*
  1304. * Verify that the stack growth is acceptable and
  1305. * update accounting. This is shared with both the
  1306. * grow-up and grow-down cases.
  1307. */
  1308. static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
  1309. {
  1310. struct mm_struct *mm = vma->vm_mm;
  1311. struct rlimit *rlim = current->signal->rlim;
  1312. /* address space limit tests */
  1313. if (!may_expand_vm(mm, grow))
  1314. return -ENOMEM;
  1315. /* Stack limit test */
  1316. if (size > rlim[RLIMIT_STACK].rlim_cur)
  1317. return -ENOMEM;
  1318. /* mlock limit tests */
  1319. if (vma->vm_flags & VM_LOCKED) {
  1320. unsigned long locked;
  1321. unsigned long limit;
  1322. locked = mm->locked_vm + grow;
  1323. limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  1324. if (locked > limit && !capable(CAP_IPC_LOCK))
  1325. return -ENOMEM;
  1326. }
  1327. /*
  1328. * Overcommit.. This must be the final test, as it will
  1329. * update security statistics.
  1330. */
  1331. if (security_vm_enough_memory(grow))
  1332. return -ENOMEM;
  1333. /* Ok, everything looks good - let it rip */
  1334. mm->total_vm += grow;
  1335. if (vma->vm_flags & VM_LOCKED)
  1336. mm->locked_vm += grow;
  1337. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1338. return 0;
  1339. }
  1340. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1341. /*
  1342. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1343. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1344. */
  1345. #ifndef CONFIG_IA64
  1346. static inline
  1347. #endif
  1348. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1349. {
  1350. int error;
  1351. if (!(vma->vm_flags & VM_GROWSUP))
  1352. return -EFAULT;
  1353. /*
  1354. * We must make sure the anon_vma is allocated
  1355. * so that the anon_vma locking is not a noop.
  1356. */
  1357. if (unlikely(anon_vma_prepare(vma)))
  1358. return -ENOMEM;
  1359. anon_vma_lock(vma);
  1360. /*
  1361. * vma->vm_start/vm_end cannot change under us because the caller
  1362. * is required to hold the mmap_sem in read mode. We need the
  1363. * anon_vma lock to serialize against concurrent expand_stacks.
  1364. */
  1365. address += 4 + PAGE_SIZE - 1;
  1366. address &= PAGE_MASK;
  1367. error = 0;
  1368. /* Somebody else might have raced and expanded it already */
  1369. if (address > vma->vm_end) {
  1370. unsigned long size, grow;
  1371. size = address - vma->vm_start;
  1372. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1373. error = acct_stack_growth(vma, size, grow);
  1374. if (!error)
  1375. vma->vm_end = address;
  1376. }
  1377. anon_vma_unlock(vma);
  1378. return error;
  1379. }
  1380. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1381. #ifdef CONFIG_STACK_GROWSUP
  1382. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1383. {
  1384. return expand_upwards(vma, address);
  1385. }
  1386. struct vm_area_struct *
  1387. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1388. {
  1389. struct vm_area_struct *vma, *prev;
  1390. addr &= PAGE_MASK;
  1391. vma = find_vma_prev(mm, addr, &prev);
  1392. if (vma && (vma->vm_start <= addr))
  1393. return vma;
  1394. if (!prev || expand_stack(prev, addr))
  1395. return NULL;
  1396. if (prev->vm_flags & VM_LOCKED) {
  1397. make_pages_present(addr, prev->vm_end);
  1398. }
  1399. return prev;
  1400. }
  1401. #else
  1402. /*
  1403. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1404. */
  1405. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1406. {
  1407. int error;
  1408. /*
  1409. * We must make sure the anon_vma is allocated
  1410. * so that the anon_vma locking is not a noop.
  1411. */
  1412. if (unlikely(anon_vma_prepare(vma)))
  1413. return -ENOMEM;
  1414. anon_vma_lock(vma);
  1415. /*
  1416. * vma->vm_start/vm_end cannot change under us because the caller
  1417. * is required to hold the mmap_sem in read mode. We need the
  1418. * anon_vma lock to serialize against concurrent expand_stacks.
  1419. */
  1420. address &= PAGE_MASK;
  1421. error = 0;
  1422. /* Somebody else might have raced and expanded it already */
  1423. if (address < vma->vm_start) {
  1424. unsigned long size, grow;
  1425. size = vma->vm_end - address;
  1426. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1427. error = acct_stack_growth(vma, size, grow);
  1428. if (!error) {
  1429. vma->vm_start = address;
  1430. vma->vm_pgoff -= grow;
  1431. }
  1432. }
  1433. anon_vma_unlock(vma);
  1434. return error;
  1435. }
  1436. struct vm_area_struct *
  1437. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1438. {
  1439. struct vm_area_struct * vma;
  1440. unsigned long start;
  1441. addr &= PAGE_MASK;
  1442. vma = find_vma(mm,addr);
  1443. if (!vma)
  1444. return NULL;
  1445. if (vma->vm_start <= addr)
  1446. return vma;
  1447. if (!(vma->vm_flags & VM_GROWSDOWN))
  1448. return NULL;
  1449. start = vma->vm_start;
  1450. if (expand_stack(vma, addr))
  1451. return NULL;
  1452. if (vma->vm_flags & VM_LOCKED) {
  1453. make_pages_present(addr, start);
  1454. }
  1455. return vma;
  1456. }
  1457. #endif
  1458. /*
  1459. * Ok - we have the memory areas we should free on the vma list,
  1460. * so release them, and do the vma updates.
  1461. *
  1462. * Called with the mm semaphore held.
  1463. */
  1464. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1465. {
  1466. /* Update high watermark before we lower total_vm */
  1467. update_hiwater_vm(mm);
  1468. do {
  1469. long nrpages = vma_pages(vma);
  1470. mm->total_vm -= nrpages;
  1471. if (vma->vm_flags & VM_LOCKED)
  1472. mm->locked_vm -= nrpages;
  1473. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1474. vma = remove_vma(vma);
  1475. } while (vma);
  1476. validate_mm(mm);
  1477. }
  1478. /*
  1479. * Get rid of page table information in the indicated region.
  1480. *
  1481. * Called with the mm semaphore held.
  1482. */
  1483. static void unmap_region(struct mm_struct *mm,
  1484. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1485. unsigned long start, unsigned long end)
  1486. {
  1487. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1488. struct mmu_gather *tlb;
  1489. unsigned long nr_accounted = 0;
  1490. lru_add_drain();
  1491. tlb = tlb_gather_mmu(mm, 0);
  1492. update_hiwater_rss(mm);
  1493. unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
  1494. vm_unacct_memory(nr_accounted);
  1495. free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
  1496. next? next->vm_start: 0);
  1497. tlb_finish_mmu(tlb, start, end);
  1498. }
  1499. /*
  1500. * Create a list of vma's touched by the unmap, removing them from the mm's
  1501. * vma list as we go..
  1502. */
  1503. static void
  1504. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1505. struct vm_area_struct *prev, unsigned long end)
  1506. {
  1507. struct vm_area_struct **insertion_point;
  1508. struct vm_area_struct *tail_vma = NULL;
  1509. unsigned long addr;
  1510. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1511. do {
  1512. rb_erase(&vma->vm_rb, &mm->mm_rb);
  1513. mm->map_count--;
  1514. tail_vma = vma;
  1515. vma = vma->vm_next;
  1516. } while (vma && vma->vm_start < end);
  1517. *insertion_point = vma;
  1518. tail_vma->vm_next = NULL;
  1519. if (mm->unmap_area == arch_unmap_area)
  1520. addr = prev ? prev->vm_end : mm->mmap_base;
  1521. else
  1522. addr = vma ? vma->vm_start : mm->mmap_base;
  1523. mm->unmap_area(mm, addr);
  1524. mm->mmap_cache = NULL; /* Kill the cache. */
  1525. }
  1526. /*
  1527. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1528. * either for the first part or the the tail.
  1529. */
  1530. int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1531. unsigned long addr, int new_below)
  1532. {
  1533. struct mempolicy *pol;
  1534. struct vm_area_struct *new;
  1535. if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK))
  1536. return -EINVAL;
  1537. if (mm->map_count >= sysctl_max_map_count)
  1538. return -ENOMEM;
  1539. new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  1540. if (!new)
  1541. return -ENOMEM;
  1542. /* most fields are the same, copy all, and then fixup */
  1543. *new = *vma;
  1544. if (new_below)
  1545. new->vm_end = addr;
  1546. else {
  1547. new->vm_start = addr;
  1548. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1549. }
  1550. pol = mpol_copy(vma_policy(vma));
  1551. if (IS_ERR(pol)) {
  1552. kmem_cache_free(vm_area_cachep, new);
  1553. return PTR_ERR(pol);
  1554. }
  1555. vma_set_policy(new, pol);
  1556. if (new->vm_file)
  1557. get_file(new->vm_file);
  1558. if (new->vm_ops && new->vm_ops->open)
  1559. new->vm_ops->open(new);
  1560. if (new_below)
  1561. vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1562. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1563. else
  1564. vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1565. return 0;
  1566. }
  1567. /* Munmap is split into 2 main parts -- this part which finds
  1568. * what needs doing, and the areas themselves, which do the
  1569. * work. This now handles partial unmappings.
  1570. * Jeremy Fitzhardinge <jeremy@goop.org>
  1571. */
  1572. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1573. {
  1574. unsigned long end;
  1575. struct vm_area_struct *vma, *prev, *last;
  1576. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1577. return -EINVAL;
  1578. if ((len = PAGE_ALIGN(len)) == 0)
  1579. return -EINVAL;
  1580. /* Find the first overlapping VMA */
  1581. vma = find_vma_prev(mm, start, &prev);
  1582. if (!vma)
  1583. return 0;
  1584. /* we have start < vma->vm_end */
  1585. /* if it doesn't overlap, we have nothing.. */
  1586. end = start + len;
  1587. if (vma->vm_start >= end)
  1588. return 0;
  1589. /*
  1590. * If we need to split any vma, do it now to save pain later.
  1591. *
  1592. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  1593. * unmapped vm_area_struct will remain in use: so lower split_vma
  1594. * places tmp vma above, and higher split_vma places tmp vma below.
  1595. */
  1596. if (start > vma->vm_start) {
  1597. int error = split_vma(mm, vma, start, 0);
  1598. if (error)
  1599. return error;
  1600. prev = vma;
  1601. }
  1602. /* Does it split the last one? */
  1603. last = find_vma(mm, end);
  1604. if (last && end > last->vm_start) {
  1605. int error = split_vma(mm, last, end, 1);
  1606. if (error)
  1607. return error;
  1608. }
  1609. vma = prev? prev->vm_next: mm->mmap;
  1610. /*
  1611. * Remove the vma's, and unmap the actual pages
  1612. */
  1613. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  1614. unmap_region(mm, vma, prev, start, end);
  1615. /* Fix up all other VM information */
  1616. remove_vma_list(mm, vma);
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL(do_munmap);
  1620. asmlinkage long sys_munmap(unsigned long addr, size_t len)
  1621. {
  1622. int ret;
  1623. struct mm_struct *mm = current->mm;
  1624. profile_munmap(addr);
  1625. down_write(&mm->mmap_sem);
  1626. ret = do_munmap(mm, addr, len);
  1627. up_write(&mm->mmap_sem);
  1628. return ret;
  1629. }
  1630. static inline void verify_mm_writelocked(struct mm_struct *mm)
  1631. {
  1632. #ifdef CONFIG_DEBUG_VM
  1633. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  1634. WARN_ON(1);
  1635. up_read(&mm->mmap_sem);
  1636. }
  1637. #endif
  1638. }
  1639. /*
  1640. * this is really a simplified "do_mmap". it only handles
  1641. * anonymous maps. eventually we may be able to do some
  1642. * brk-specific accounting here.
  1643. */
  1644. unsigned long do_brk(unsigned long addr, unsigned long len)
  1645. {
  1646. struct mm_struct * mm = current->mm;
  1647. struct vm_area_struct * vma, * prev;
  1648. unsigned long flags;
  1649. struct rb_node ** rb_link, * rb_parent;
  1650. pgoff_t pgoff = addr >> PAGE_SHIFT;
  1651. int error;
  1652. len = PAGE_ALIGN(len);
  1653. if (!len)
  1654. return addr;
  1655. if ((addr + len) > TASK_SIZE || (addr + len) < addr)
  1656. return -EINVAL;
  1657. if (is_hugepage_only_range(mm, addr, len))
  1658. return -EINVAL;
  1659. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  1660. error = arch_mmap_check(addr, len, flags);
  1661. if (error)
  1662. return error;
  1663. /*
  1664. * mlock MCL_FUTURE?
  1665. */
  1666. if (mm->def_flags & VM_LOCKED) {
  1667. unsigned long locked, lock_limit;
  1668. locked = len >> PAGE_SHIFT;
  1669. locked += mm->locked_vm;
  1670. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1671. lock_limit >>= PAGE_SHIFT;
  1672. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1673. return -EAGAIN;
  1674. }
  1675. /*
  1676. * mm->mmap_sem is required to protect against another thread
  1677. * changing the mappings in case we sleep.
  1678. */
  1679. verify_mm_writelocked(mm);
  1680. /*
  1681. * Clear old maps. this also does some error checking for us
  1682. */
  1683. munmap_back:
  1684. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1685. if (vma && vma->vm_start < addr + len) {
  1686. if (do_munmap(mm, addr, len))
  1687. return -ENOMEM;
  1688. goto munmap_back;
  1689. }
  1690. /* Check against address space limits *after* clearing old maps... */
  1691. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1692. return -ENOMEM;
  1693. if (mm->map_count > sysctl_max_map_count)
  1694. return -ENOMEM;
  1695. if (security_vm_enough_memory(len >> PAGE_SHIFT))
  1696. return -ENOMEM;
  1697. /* Can we just expand an old private anonymous mapping? */
  1698. if (vma_merge(mm, prev, addr, addr + len, flags,
  1699. NULL, NULL, pgoff, NULL))
  1700. goto out;
  1701. /*
  1702. * create a vma struct for an anonymous mapping
  1703. */
  1704. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1705. if (!vma) {
  1706. vm_unacct_memory(len >> PAGE_SHIFT);
  1707. return -ENOMEM;
  1708. }
  1709. vma->vm_mm = mm;
  1710. vma->vm_start = addr;
  1711. vma->vm_end = addr + len;
  1712. vma->vm_pgoff = pgoff;
  1713. vma->vm_flags = flags;
  1714. vma->vm_page_prot = protection_map[flags &
  1715. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
  1716. vma_link(mm, vma, prev, rb_link, rb_parent);
  1717. out:
  1718. mm->total_vm += len >> PAGE_SHIFT;
  1719. if (flags & VM_LOCKED) {
  1720. mm->locked_vm += len >> PAGE_SHIFT;
  1721. make_pages_present(addr, addr + len);
  1722. }
  1723. return addr;
  1724. }
  1725. EXPORT_SYMBOL(do_brk);
  1726. /* Release all mmaps. */
  1727. void exit_mmap(struct mm_struct *mm)
  1728. {
  1729. struct mmu_gather *tlb;
  1730. struct vm_area_struct *vma = mm->mmap;
  1731. unsigned long nr_accounted = 0;
  1732. unsigned long end;
  1733. lru_add_drain();
  1734. flush_cache_mm(mm);
  1735. tlb = tlb_gather_mmu(mm, 1);
  1736. /* Don't update_hiwater_rss(mm) here, do_exit already did */
  1737. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  1738. end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  1739. vm_unacct_memory(nr_accounted);
  1740. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
  1741. tlb_finish_mmu(tlb, 0, end);
  1742. /*
  1743. * Walk the list again, actually closing and freeing it,
  1744. * with preemption enabled, without holding any MM locks.
  1745. */
  1746. while (vma)
  1747. vma = remove_vma(vma);
  1748. BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  1749. }
  1750. /* Insert vm structure into process list sorted by address
  1751. * and into the inode's i_mmap tree. If vm_file is non-NULL
  1752. * then i_mmap_lock is taken here.
  1753. */
  1754. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  1755. {
  1756. struct vm_area_struct * __vma, * prev;
  1757. struct rb_node ** rb_link, * rb_parent;
  1758. /*
  1759. * The vm_pgoff of a purely anonymous vma should be irrelevant
  1760. * until its first write fault, when page's anon_vma and index
  1761. * are set. But now set the vm_pgoff it will almost certainly
  1762. * end up with (unless mremap moves it elsewhere before that
  1763. * first wfault), so /proc/pid/maps tells a consistent story.
  1764. *
  1765. * By setting it to reflect the virtual start address of the
  1766. * vma, merges and splits can happen in a seamless way, just
  1767. * using the existing file pgoff checks and manipulations.
  1768. * Similarly in do_mmap_pgoff and in do_brk.
  1769. */
  1770. if (!vma->vm_file) {
  1771. BUG_ON(vma->anon_vma);
  1772. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1773. }
  1774. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  1775. if (__vma && __vma->vm_start < vma->vm_end)
  1776. return -ENOMEM;
  1777. if ((vma->vm_flags & VM_ACCOUNT) &&
  1778. security_vm_enough_memory(vma_pages(vma)))
  1779. return -ENOMEM;
  1780. vma_link(mm, vma, prev, rb_link, rb_parent);
  1781. return 0;
  1782. }
  1783. /*
  1784. * Copy the vma structure to a new location in the same mm,
  1785. * prior to moving page table entries, to effect an mremap move.
  1786. */
  1787. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  1788. unsigned long addr, unsigned long len, pgoff_t pgoff)
  1789. {
  1790. struct vm_area_struct *vma = *vmap;
  1791. unsigned long vma_start = vma->vm_start;
  1792. struct mm_struct *mm = vma->vm_mm;
  1793. struct vm_area_struct *new_vma, *prev;
  1794. struct rb_node **rb_link, *rb_parent;
  1795. struct mempolicy *pol;
  1796. /*
  1797. * If anonymous vma has not yet been faulted, update new pgoff
  1798. * to match new location, to increase its chance of merging.
  1799. */
  1800. if (!vma->vm_file && !vma->anon_vma)
  1801. pgoff = addr >> PAGE_SHIFT;
  1802. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1803. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  1804. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  1805. if (new_vma) {
  1806. /*
  1807. * Source vma may have been merged into new_vma
  1808. */
  1809. if (vma_start >= new_vma->vm_start &&
  1810. vma_start < new_vma->vm_end)
  1811. *vmap = new_vma;
  1812. } else {
  1813. new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  1814. if (new_vma) {
  1815. *new_vma = *vma;
  1816. pol = mpol_copy(vma_policy(vma));
  1817. if (IS_ERR(pol)) {
  1818. kmem_cache_free(vm_area_cachep, new_vma);
  1819. return NULL;
  1820. }
  1821. vma_set_policy(new_vma, pol);
  1822. new_vma->vm_start = addr;
  1823. new_vma->vm_end = addr + len;
  1824. new_vma->vm_pgoff = pgoff;
  1825. if (new_vma->vm_file)
  1826. get_file(new_vma->vm_file);
  1827. if (new_vma->vm_ops && new_vma->vm_ops->open)
  1828. new_vma->vm_ops->open(new_vma);
  1829. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  1830. }
  1831. }
  1832. return new_vma;
  1833. }
  1834. /*
  1835. * Return true if the calling process may expand its vm space by the passed
  1836. * number of pages
  1837. */
  1838. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  1839. {
  1840. unsigned long cur = mm->total_vm; /* pages */
  1841. unsigned long lim;
  1842. lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  1843. if (cur + npages > lim)
  1844. return 0;
  1845. return 1;
  1846. }