mmap.c 54 KB

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