mmap.c 54 KB

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