mmap.c 71 KB

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