mmap.c 53 KB

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