mmap.c 72 KB

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