mmap.c 56 KB

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