mmap.c 54 KB

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