mmap.c 71 KB

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