mmap.c 53 KB

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