mmap.c 58 KB

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