mmap.c 69 KB

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