mmap.c 71 KB

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