mmap.c 54 KB

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