mmap.c 88 KB

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