mmap.c 57 KB

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