mmap.c 65 KB

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