mmap.c 70 KB

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