mmap.c 58 KB

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