mmap.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338
  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_rwsem
  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. i_mmap_lock_write(mapping);
  229. __remove_shared_vm_struct(vma, file, mapping);
  230. i_mmap_unlock_write(mapping);
  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. i_mmap_lock_write(mapping);
  590. }
  591. __vma_link(mm, vma, prev, rb_link, rb_parent);
  592. __vma_link_file(vma);
  593. if (mapping)
  594. i_mmap_unlock_write(mapping);
  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. int error;
  680. error = anon_vma_clone(importer, exporter);
  681. if (error)
  682. return error;
  683. importer->anon_vma = exporter->anon_vma;
  684. }
  685. }
  686. if (file) {
  687. mapping = file->f_mapping;
  688. if (!(vma->vm_flags & VM_NONLINEAR)) {
  689. root = &mapping->i_mmap;
  690. uprobe_munmap(vma, vma->vm_start, vma->vm_end);
  691. if (adjust_next)
  692. uprobe_munmap(next, next->vm_start,
  693. next->vm_end);
  694. }
  695. i_mmap_lock_write(mapping);
  696. if (insert) {
  697. /*
  698. * Put into interval tree now, so instantiated pages
  699. * are visible to arm/parisc __flush_dcache_page
  700. * throughout; but we cannot insert into address
  701. * space until vma start or end is updated.
  702. */
  703. __vma_link_file(insert);
  704. }
  705. }
  706. vma_adjust_trans_huge(vma, start, end, adjust_next);
  707. anon_vma = vma->anon_vma;
  708. if (!anon_vma && adjust_next)
  709. anon_vma = next->anon_vma;
  710. if (anon_vma) {
  711. VM_BUG_ON_VMA(adjust_next && next->anon_vma &&
  712. anon_vma != next->anon_vma, next);
  713. anon_vma_lock_write(anon_vma);
  714. anon_vma_interval_tree_pre_update_vma(vma);
  715. if (adjust_next)
  716. anon_vma_interval_tree_pre_update_vma(next);
  717. }
  718. if (root) {
  719. flush_dcache_mmap_lock(mapping);
  720. vma_interval_tree_remove(vma, root);
  721. if (adjust_next)
  722. vma_interval_tree_remove(next, root);
  723. }
  724. if (start != vma->vm_start) {
  725. vma->vm_start = start;
  726. start_changed = true;
  727. }
  728. if (end != vma->vm_end) {
  729. vma->vm_end = end;
  730. end_changed = true;
  731. }
  732. vma->vm_pgoff = pgoff;
  733. if (adjust_next) {
  734. next->vm_start += adjust_next << PAGE_SHIFT;
  735. next->vm_pgoff += adjust_next;
  736. }
  737. if (root) {
  738. if (adjust_next)
  739. vma_interval_tree_insert(next, root);
  740. vma_interval_tree_insert(vma, root);
  741. flush_dcache_mmap_unlock(mapping);
  742. }
  743. if (remove_next) {
  744. /*
  745. * vma_merge has merged next into vma, and needs
  746. * us to remove next before dropping the locks.
  747. */
  748. __vma_unlink(mm, next, vma);
  749. if (file)
  750. __remove_shared_vm_struct(next, file, mapping);
  751. } else if (insert) {
  752. /*
  753. * split_vma has split insert from vma, and needs
  754. * us to insert it before dropping the locks
  755. * (it may either follow vma or precede it).
  756. */
  757. __insert_vm_struct(mm, insert);
  758. } else {
  759. if (start_changed)
  760. vma_gap_update(vma);
  761. if (end_changed) {
  762. if (!next)
  763. mm->highest_vm_end = end;
  764. else if (!adjust_next)
  765. vma_gap_update(next);
  766. }
  767. }
  768. if (anon_vma) {
  769. anon_vma_interval_tree_post_update_vma(vma);
  770. if (adjust_next)
  771. anon_vma_interval_tree_post_update_vma(next);
  772. anon_vma_unlock_write(anon_vma);
  773. }
  774. if (mapping)
  775. i_mmap_unlock_write(mapping);
  776. if (root) {
  777. uprobe_mmap(vma);
  778. if (adjust_next)
  779. uprobe_mmap(next);
  780. }
  781. if (remove_next) {
  782. if (file) {
  783. uprobe_munmap(next, next->vm_start, next->vm_end);
  784. fput(file);
  785. }
  786. if (next->anon_vma)
  787. anon_vma_merge(vma, next);
  788. mm->map_count--;
  789. mpol_put(vma_policy(next));
  790. kmem_cache_free(vm_area_cachep, next);
  791. /*
  792. * In mprotect's case 6 (see comments on vma_merge),
  793. * we must remove another next too. It would clutter
  794. * up the code too much to do both in one go.
  795. */
  796. next = vma->vm_next;
  797. if (remove_next == 2)
  798. goto again;
  799. else if (next)
  800. vma_gap_update(next);
  801. else
  802. mm->highest_vm_end = end;
  803. }
  804. if (insert && file)
  805. uprobe_mmap(insert);
  806. validate_mm(mm);
  807. return 0;
  808. }
  809. /*
  810. * If the vma has a ->close operation then the driver probably needs to release
  811. * per-vma resources, so we don't attempt to merge those.
  812. */
  813. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  814. struct file *file, unsigned long vm_flags)
  815. {
  816. /*
  817. * VM_SOFTDIRTY should not prevent from VMA merging, if we
  818. * match the flags but dirty bit -- the caller should mark
  819. * merged VMA as dirty. If dirty bit won't be excluded from
  820. * comparison, we increase pressue on the memory system forcing
  821. * the kernel to generate new VMAs when old one could be
  822. * extended instead.
  823. */
  824. if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
  825. return 0;
  826. if (vma->vm_file != file)
  827. return 0;
  828. if (vma->vm_ops && vma->vm_ops->close)
  829. return 0;
  830. return 1;
  831. }
  832. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  833. struct anon_vma *anon_vma2,
  834. struct vm_area_struct *vma)
  835. {
  836. /*
  837. * The list_is_singular() test is to avoid merging VMA cloned from
  838. * parents. This can improve scalability caused by anon_vma lock.
  839. */
  840. if ((!anon_vma1 || !anon_vma2) && (!vma ||
  841. list_is_singular(&vma->anon_vma_chain)))
  842. return 1;
  843. return anon_vma1 == anon_vma2;
  844. }
  845. /*
  846. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  847. * in front of (at a lower virtual address and file offset than) the vma.
  848. *
  849. * We cannot merge two vmas if they have differently assigned (non-NULL)
  850. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  851. *
  852. * We don't check here for the merged mmap wrapping around the end of pagecache
  853. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  854. * wrap, nor mmaps which cover the final page at index -1UL.
  855. */
  856. static int
  857. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  858. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  859. {
  860. if (is_mergeable_vma(vma, file, vm_flags) &&
  861. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  862. if (vma->vm_pgoff == vm_pgoff)
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  869. * beyond (at a higher virtual address and file offset than) the vma.
  870. *
  871. * We cannot merge two vmas if they have differently assigned (non-NULL)
  872. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  873. */
  874. static int
  875. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  876. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  877. {
  878. if (is_mergeable_vma(vma, file, vm_flags) &&
  879. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  880. pgoff_t vm_pglen;
  881. vm_pglen = vma_pages(vma);
  882. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  883. return 1;
  884. }
  885. return 0;
  886. }
  887. /*
  888. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  889. * whether that can be merged with its predecessor or its successor.
  890. * Or both (it neatly fills a hole).
  891. *
  892. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  893. * certain not to be mapped by the time vma_merge is called; but when
  894. * called for mprotect, it is certain to be already mapped (either at
  895. * an offset within prev, or at the start of next), and the flags of
  896. * this area are about to be changed to vm_flags - and the no-change
  897. * case has already been eliminated.
  898. *
  899. * The following mprotect cases have to be considered, where AAAA is
  900. * the area passed down from mprotect_fixup, never extending beyond one
  901. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  902. *
  903. * AAAA AAAA AAAA AAAA
  904. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  905. * cannot merge might become might become might become
  906. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  907. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  908. * mremap move: PPPPNNNNNNNN 8
  909. * AAAA
  910. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  911. * might become case 1 below case 2 below case 3 below
  912. *
  913. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  914. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  915. */
  916. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  917. struct vm_area_struct *prev, unsigned long addr,
  918. unsigned long end, unsigned long vm_flags,
  919. struct anon_vma *anon_vma, struct file *file,
  920. pgoff_t pgoff, struct mempolicy *policy)
  921. {
  922. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  923. struct vm_area_struct *area, *next;
  924. int err;
  925. /*
  926. * We later require that vma->vm_flags == vm_flags,
  927. * so this tests vma->vm_flags & VM_SPECIAL, too.
  928. */
  929. if (vm_flags & VM_SPECIAL)
  930. return NULL;
  931. if (prev)
  932. next = prev->vm_next;
  933. else
  934. next = mm->mmap;
  935. area = next;
  936. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  937. next = next->vm_next;
  938. /*
  939. * Can it merge with the predecessor?
  940. */
  941. if (prev && prev->vm_end == addr &&
  942. mpol_equal(vma_policy(prev), policy) &&
  943. can_vma_merge_after(prev, vm_flags,
  944. anon_vma, file, pgoff)) {
  945. /*
  946. * OK, it can. Can we now merge in the successor as well?
  947. */
  948. if (next && end == next->vm_start &&
  949. mpol_equal(policy, vma_policy(next)) &&
  950. can_vma_merge_before(next, vm_flags,
  951. anon_vma, file, pgoff+pglen) &&
  952. is_mergeable_anon_vma(prev->anon_vma,
  953. next->anon_vma, NULL)) {
  954. /* cases 1, 6 */
  955. err = vma_adjust(prev, prev->vm_start,
  956. next->vm_end, prev->vm_pgoff, NULL);
  957. } else /* cases 2, 5, 7 */
  958. err = vma_adjust(prev, prev->vm_start,
  959. end, prev->vm_pgoff, NULL);
  960. if (err)
  961. return NULL;
  962. khugepaged_enter_vma_merge(prev, vm_flags);
  963. return prev;
  964. }
  965. /*
  966. * Can this new request be merged in front of next?
  967. */
  968. if (next && end == next->vm_start &&
  969. mpol_equal(policy, vma_policy(next)) &&
  970. can_vma_merge_before(next, vm_flags,
  971. anon_vma, file, pgoff+pglen)) {
  972. if (prev && addr < prev->vm_end) /* case 4 */
  973. err = vma_adjust(prev, prev->vm_start,
  974. addr, prev->vm_pgoff, NULL);
  975. else /* cases 3, 8 */
  976. err = vma_adjust(area, addr, next->vm_end,
  977. next->vm_pgoff - pglen, NULL);
  978. if (err)
  979. return NULL;
  980. khugepaged_enter_vma_merge(area, vm_flags);
  981. return area;
  982. }
  983. return NULL;
  984. }
  985. /*
  986. * Rough compatbility check to quickly see if it's even worth looking
  987. * at sharing an anon_vma.
  988. *
  989. * They need to have the same vm_file, and the flags can only differ
  990. * in things that mprotect may change.
  991. *
  992. * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
  993. * we can merge the two vma's. For example, we refuse to merge a vma if
  994. * there is a vm_ops->close() function, because that indicates that the
  995. * driver is doing some kind of reference counting. But that doesn't
  996. * really matter for the anon_vma sharing case.
  997. */
  998. static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
  999. {
  1000. return a->vm_end == b->vm_start &&
  1001. mpol_equal(vma_policy(a), vma_policy(b)) &&
  1002. a->vm_file == b->vm_file &&
  1003. !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
  1004. b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
  1005. }
  1006. /*
  1007. * Do some basic sanity checking to see if we can re-use the anon_vma
  1008. * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
  1009. * the same as 'old', the other will be the new one that is trying
  1010. * to share the anon_vma.
  1011. *
  1012. * NOTE! This runs with mm_sem held for reading, so it is possible that
  1013. * the anon_vma of 'old' is concurrently in the process of being set up
  1014. * by another page fault trying to merge _that_. But that's ok: if it
  1015. * is being set up, that automatically means that it will be a singleton
  1016. * acceptable for merging, so we can do all of this optimistically. But
  1017. * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
  1018. *
  1019. * IOW: that the "list_is_singular()" test on the anon_vma_chain only
  1020. * matters for the 'stable anon_vma' case (ie the thing we want to avoid
  1021. * is to return an anon_vma that is "complex" due to having gone through
  1022. * a fork).
  1023. *
  1024. * We also make sure that the two vma's are compatible (adjacent,
  1025. * and with the same memory policies). That's all stable, even with just
  1026. * a read lock on the mm_sem.
  1027. */
  1028. static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
  1029. {
  1030. if (anon_vma_compatible(a, b)) {
  1031. struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
  1032. if (anon_vma && list_is_singular(&old->anon_vma_chain))
  1033. return anon_vma;
  1034. }
  1035. return NULL;
  1036. }
  1037. /*
  1038. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  1039. * neighbouring vmas for a suitable anon_vma, before it goes off
  1040. * to allocate a new anon_vma. It checks because a repetitive
  1041. * sequence of mprotects and faults may otherwise lead to distinct
  1042. * anon_vmas being allocated, preventing vma merge in subsequent
  1043. * mprotect.
  1044. */
  1045. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  1046. {
  1047. struct anon_vma *anon_vma;
  1048. struct vm_area_struct *near;
  1049. near = vma->vm_next;
  1050. if (!near)
  1051. goto try_prev;
  1052. anon_vma = reusable_anon_vma(near, vma, near);
  1053. if (anon_vma)
  1054. return anon_vma;
  1055. try_prev:
  1056. near = vma->vm_prev;
  1057. if (!near)
  1058. goto none;
  1059. anon_vma = reusable_anon_vma(near, near, vma);
  1060. if (anon_vma)
  1061. return anon_vma;
  1062. none:
  1063. /*
  1064. * There's no absolute need to look only at touching neighbours:
  1065. * we could search further afield for "compatible" anon_vmas.
  1066. * But it would probably just be a waste of time searching,
  1067. * or lead to too many vmas hanging off the same anon_vma.
  1068. * We're trying to allow mprotect remerging later on,
  1069. * not trying to minimize memory used for anon_vmas.
  1070. */
  1071. return NULL;
  1072. }
  1073. #ifdef CONFIG_PROC_FS
  1074. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  1075. struct file *file, long pages)
  1076. {
  1077. const unsigned long stack_flags
  1078. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  1079. mm->total_vm += pages;
  1080. if (file) {
  1081. mm->shared_vm += pages;
  1082. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  1083. mm->exec_vm += pages;
  1084. } else if (flags & stack_flags)
  1085. mm->stack_vm += pages;
  1086. }
  1087. #endif /* CONFIG_PROC_FS */
  1088. /*
  1089. * If a hint addr is less than mmap_min_addr change hint to be as
  1090. * low as possible but still greater than mmap_min_addr
  1091. */
  1092. static inline unsigned long round_hint_to_min(unsigned long hint)
  1093. {
  1094. hint &= PAGE_MASK;
  1095. if (((void *)hint != NULL) &&
  1096. (hint < mmap_min_addr))
  1097. return PAGE_ALIGN(mmap_min_addr);
  1098. return hint;
  1099. }
  1100. static inline int mlock_future_check(struct mm_struct *mm,
  1101. unsigned long flags,
  1102. unsigned long len)
  1103. {
  1104. unsigned long locked, lock_limit;
  1105. /* mlock MCL_FUTURE? */
  1106. if (flags & VM_LOCKED) {
  1107. locked = len >> PAGE_SHIFT;
  1108. locked += mm->locked_vm;
  1109. lock_limit = rlimit(RLIMIT_MEMLOCK);
  1110. lock_limit >>= PAGE_SHIFT;
  1111. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1112. return -EAGAIN;
  1113. }
  1114. return 0;
  1115. }
  1116. /*
  1117. * The caller must hold down_write(&current->mm->mmap_sem).
  1118. */
  1119. unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  1120. unsigned long len, unsigned long prot,
  1121. unsigned long flags, unsigned long pgoff,
  1122. unsigned long *populate)
  1123. {
  1124. struct mm_struct *mm = current->mm;
  1125. vm_flags_t vm_flags;
  1126. *populate = 0;
  1127. /*
  1128. * Does the application expect PROT_READ to imply PROT_EXEC?
  1129. *
  1130. * (the exception is when the underlying filesystem is noexec
  1131. * mounted, in which case we dont add PROT_EXEC.)
  1132. */
  1133. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  1134. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  1135. prot |= PROT_EXEC;
  1136. if (!len)
  1137. return -EINVAL;
  1138. if (!(flags & MAP_FIXED))
  1139. addr = round_hint_to_min(addr);
  1140. /* Careful about overflows.. */
  1141. len = PAGE_ALIGN(len);
  1142. if (!len)
  1143. return -ENOMEM;
  1144. /* offset overflow? */
  1145. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  1146. return -EOVERFLOW;
  1147. /* Too many mappings? */
  1148. if (mm->map_count > sysctl_max_map_count)
  1149. return -ENOMEM;
  1150. /* Obtain the address to map to. we verify (or select) it and ensure
  1151. * that it represents a valid section of the address space.
  1152. */
  1153. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  1154. if (addr & ~PAGE_MASK)
  1155. return addr;
  1156. /* Do simple checking here so the lower-level routines won't have
  1157. * to. we assume access permissions have been handled by the open
  1158. * of the memory object, so we don't do any here.
  1159. */
  1160. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  1161. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  1162. if (flags & MAP_LOCKED)
  1163. if (!can_do_mlock())
  1164. return -EPERM;
  1165. if (mlock_future_check(mm, vm_flags, len))
  1166. return -EAGAIN;
  1167. if (file) {
  1168. struct inode *inode = file_inode(file);
  1169. switch (flags & MAP_TYPE) {
  1170. case MAP_SHARED:
  1171. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  1172. return -EACCES;
  1173. /*
  1174. * Make sure we don't allow writing to an append-only
  1175. * file..
  1176. */
  1177. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  1178. return -EACCES;
  1179. /*
  1180. * Make sure there are no mandatory locks on the file.
  1181. */
  1182. if (locks_verify_locked(file))
  1183. return -EAGAIN;
  1184. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1185. if (!(file->f_mode & FMODE_WRITE))
  1186. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  1187. /* fall through */
  1188. case MAP_PRIVATE:
  1189. if (!(file->f_mode & FMODE_READ))
  1190. return -EACCES;
  1191. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  1192. if (vm_flags & VM_EXEC)
  1193. return -EPERM;
  1194. vm_flags &= ~VM_MAYEXEC;
  1195. }
  1196. if (!file->f_op->mmap)
  1197. return -ENODEV;
  1198. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1199. return -EINVAL;
  1200. break;
  1201. default:
  1202. return -EINVAL;
  1203. }
  1204. } else {
  1205. switch (flags & MAP_TYPE) {
  1206. case MAP_SHARED:
  1207. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1208. return -EINVAL;
  1209. /*
  1210. * Ignore pgoff.
  1211. */
  1212. pgoff = 0;
  1213. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1214. break;
  1215. case MAP_PRIVATE:
  1216. /*
  1217. * Set pgoff according to addr for anon_vma.
  1218. */
  1219. pgoff = addr >> PAGE_SHIFT;
  1220. break;
  1221. default:
  1222. return -EINVAL;
  1223. }
  1224. }
  1225. /*
  1226. * Set 'VM_NORESERVE' if we should not account for the
  1227. * memory use of this mapping.
  1228. */
  1229. if (flags & MAP_NORESERVE) {
  1230. /* We honor MAP_NORESERVE if allowed to overcommit */
  1231. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1232. vm_flags |= VM_NORESERVE;
  1233. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1234. if (file && is_file_hugepages(file))
  1235. vm_flags |= VM_NORESERVE;
  1236. }
  1237. addr = mmap_region(file, addr, len, vm_flags, pgoff);
  1238. if (!IS_ERR_VALUE(addr) &&
  1239. ((vm_flags & VM_LOCKED) ||
  1240. (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
  1241. *populate = len;
  1242. return addr;
  1243. }
  1244. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1245. unsigned long, prot, unsigned long, flags,
  1246. unsigned long, fd, unsigned long, pgoff)
  1247. {
  1248. struct file *file = NULL;
  1249. unsigned long retval = -EBADF;
  1250. if (!(flags & MAP_ANONYMOUS)) {
  1251. audit_mmap_fd(fd, flags);
  1252. file = fget(fd);
  1253. if (!file)
  1254. goto out;
  1255. if (is_file_hugepages(file))
  1256. len = ALIGN(len, huge_page_size(hstate_file(file)));
  1257. retval = -EINVAL;
  1258. if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
  1259. goto out_fput;
  1260. } else if (flags & MAP_HUGETLB) {
  1261. struct user_struct *user = NULL;
  1262. struct hstate *hs;
  1263. hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
  1264. if (!hs)
  1265. return -EINVAL;
  1266. len = ALIGN(len, huge_page_size(hs));
  1267. /*
  1268. * VM_NORESERVE is used because the reservations will be
  1269. * taken when vm_ops->mmap() is called
  1270. * A dummy user value is used because we are not locking
  1271. * memory so no accounting is necessary
  1272. */
  1273. file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
  1274. VM_NORESERVE,
  1275. &user, HUGETLB_ANONHUGE_INODE,
  1276. (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  1277. if (IS_ERR(file))
  1278. return PTR_ERR(file);
  1279. }
  1280. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1281. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1282. out_fput:
  1283. if (file)
  1284. fput(file);
  1285. out:
  1286. return retval;
  1287. }
  1288. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1289. struct mmap_arg_struct {
  1290. unsigned long addr;
  1291. unsigned long len;
  1292. unsigned long prot;
  1293. unsigned long flags;
  1294. unsigned long fd;
  1295. unsigned long offset;
  1296. };
  1297. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1298. {
  1299. struct mmap_arg_struct a;
  1300. if (copy_from_user(&a, arg, sizeof(a)))
  1301. return -EFAULT;
  1302. if (a.offset & ~PAGE_MASK)
  1303. return -EINVAL;
  1304. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1305. a.offset >> PAGE_SHIFT);
  1306. }
  1307. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1308. /*
  1309. * Some shared mappigns will want the pages marked read-only
  1310. * to track write events. If so, we'll downgrade vm_page_prot
  1311. * to the private version (using protection_map[] without the
  1312. * VM_SHARED bit).
  1313. */
  1314. int vma_wants_writenotify(struct vm_area_struct *vma)
  1315. {
  1316. vm_flags_t vm_flags = vma->vm_flags;
  1317. /* If it was private or non-writable, the write bit is already clear */
  1318. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  1319. return 0;
  1320. /* The backer wishes to know when pages are first written to? */
  1321. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  1322. return 1;
  1323. /* The open routine did something to the protections that pgprot_modify
  1324. * won't preserve? */
  1325. if (pgprot_val(vma->vm_page_prot) !=
  1326. pgprot_val(vm_pgprot_modify(vma->vm_page_prot, vm_flags)))
  1327. return 0;
  1328. /* Do we need to track softdirty? */
  1329. if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
  1330. return 1;
  1331. /* Specialty mapping? */
  1332. if (vm_flags & VM_PFNMAP)
  1333. return 0;
  1334. /* Can the mapping track the dirty pages? */
  1335. return vma->vm_file && vma->vm_file->f_mapping &&
  1336. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  1337. }
  1338. /*
  1339. * We account for memory if it's a private writeable mapping,
  1340. * not hugepages and VM_NORESERVE wasn't set.
  1341. */
  1342. static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
  1343. {
  1344. /*
  1345. * hugetlb has its own accounting separate from the core VM
  1346. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  1347. */
  1348. if (file && is_file_hugepages(file))
  1349. return 0;
  1350. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  1351. }
  1352. unsigned long mmap_region(struct file *file, unsigned long addr,
  1353. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
  1354. {
  1355. struct mm_struct *mm = current->mm;
  1356. struct vm_area_struct *vma, *prev;
  1357. int error;
  1358. struct rb_node **rb_link, *rb_parent;
  1359. unsigned long charged = 0;
  1360. /* Check against address space limit. */
  1361. if (!may_expand_vm(mm, len >> PAGE_SHIFT)) {
  1362. unsigned long nr_pages;
  1363. /*
  1364. * MAP_FIXED may remove pages of mappings that intersects with
  1365. * requested mapping. Account for the pages it would unmap.
  1366. */
  1367. if (!(vm_flags & MAP_FIXED))
  1368. return -ENOMEM;
  1369. nr_pages = count_vma_pages_range(mm, addr, addr + len);
  1370. if (!may_expand_vm(mm, (len >> PAGE_SHIFT) - nr_pages))
  1371. return -ENOMEM;
  1372. }
  1373. /* Clear old maps */
  1374. error = -ENOMEM;
  1375. munmap_back:
  1376. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  1377. if (do_munmap(mm, addr, len))
  1378. return -ENOMEM;
  1379. goto munmap_back;
  1380. }
  1381. /*
  1382. * Private writable mapping: check memory availability
  1383. */
  1384. if (accountable_mapping(file, vm_flags)) {
  1385. charged = len >> PAGE_SHIFT;
  1386. if (security_vm_enough_memory_mm(mm, charged))
  1387. return -ENOMEM;
  1388. vm_flags |= VM_ACCOUNT;
  1389. }
  1390. /*
  1391. * Can we just expand an old mapping?
  1392. */
  1393. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1394. if (vma)
  1395. goto out;
  1396. /*
  1397. * Determine the object being mapped and call the appropriate
  1398. * specific mapper. the address has already been validated, but
  1399. * not unmapped, but the maps are removed from the list.
  1400. */
  1401. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1402. if (!vma) {
  1403. error = -ENOMEM;
  1404. goto unacct_error;
  1405. }
  1406. vma->vm_mm = mm;
  1407. vma->vm_start = addr;
  1408. vma->vm_end = addr + len;
  1409. vma->vm_flags = vm_flags;
  1410. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1411. vma->vm_pgoff = pgoff;
  1412. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1413. if (file) {
  1414. if (vm_flags & VM_DENYWRITE) {
  1415. error = deny_write_access(file);
  1416. if (error)
  1417. goto free_vma;
  1418. }
  1419. if (vm_flags & VM_SHARED) {
  1420. error = mapping_map_writable(file->f_mapping);
  1421. if (error)
  1422. goto allow_write_and_free_vma;
  1423. }
  1424. /* ->mmap() can change vma->vm_file, but must guarantee that
  1425. * vma_link() below can deny write-access if VM_DENYWRITE is set
  1426. * and map writably if VM_SHARED is set. This usually means the
  1427. * new file must not have been exposed to user-space, yet.
  1428. */
  1429. vma->vm_file = get_file(file);
  1430. error = file->f_op->mmap(file, vma);
  1431. if (error)
  1432. goto unmap_and_free_vma;
  1433. /* Can addr have changed??
  1434. *
  1435. * Answer: Yes, several device drivers can do it in their
  1436. * f_op->mmap method. -DaveM
  1437. * Bug: If addr is changed, prev, rb_link, rb_parent should
  1438. * be updated for vma_link()
  1439. */
  1440. WARN_ON_ONCE(addr != vma->vm_start);
  1441. addr = vma->vm_start;
  1442. vm_flags = vma->vm_flags;
  1443. } else if (vm_flags & VM_SHARED) {
  1444. error = shmem_zero_setup(vma);
  1445. if (error)
  1446. goto free_vma;
  1447. }
  1448. vma_link(mm, vma, prev, rb_link, rb_parent);
  1449. /* Once vma denies write, undo our temporary denial count */
  1450. if (file) {
  1451. if (vm_flags & VM_SHARED)
  1452. mapping_unmap_writable(file->f_mapping);
  1453. if (vm_flags & VM_DENYWRITE)
  1454. allow_write_access(file);
  1455. }
  1456. file = vma->vm_file;
  1457. out:
  1458. perf_event_mmap(vma);
  1459. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1460. if (vm_flags & VM_LOCKED) {
  1461. if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
  1462. vma == get_gate_vma(current->mm)))
  1463. mm->locked_vm += (len >> PAGE_SHIFT);
  1464. else
  1465. vma->vm_flags &= ~VM_LOCKED;
  1466. }
  1467. if (file)
  1468. uprobe_mmap(vma);
  1469. /*
  1470. * New (or expanded) vma always get soft dirty status.
  1471. * Otherwise user-space soft-dirty page tracker won't
  1472. * be able to distinguish situation when vma area unmapped,
  1473. * then new mapped in-place (which must be aimed as
  1474. * a completely new data area).
  1475. */
  1476. vma->vm_flags |= VM_SOFTDIRTY;
  1477. vma_set_page_prot(vma);
  1478. return addr;
  1479. unmap_and_free_vma:
  1480. vma->vm_file = NULL;
  1481. fput(file);
  1482. /* Undo any partial mapping done by a device driver. */
  1483. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1484. charged = 0;
  1485. if (vm_flags & VM_SHARED)
  1486. mapping_unmap_writable(file->f_mapping);
  1487. allow_write_and_free_vma:
  1488. if (vm_flags & VM_DENYWRITE)
  1489. allow_write_access(file);
  1490. free_vma:
  1491. kmem_cache_free(vm_area_cachep, vma);
  1492. unacct_error:
  1493. if (charged)
  1494. vm_unacct_memory(charged);
  1495. return error;
  1496. }
  1497. unsigned long unmapped_area(struct vm_unmapped_area_info *info)
  1498. {
  1499. /*
  1500. * We implement the search by looking for an rbtree node that
  1501. * immediately follows a suitable gap. That is,
  1502. * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
  1503. * - gap_end = vma->vm_start >= info->low_limit + length;
  1504. * - gap_end - gap_start >= length
  1505. */
  1506. struct mm_struct *mm = current->mm;
  1507. struct vm_area_struct *vma;
  1508. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1509. /* Adjust search length to account for worst case alignment overhead */
  1510. length = info->length + info->align_mask;
  1511. if (length < info->length)
  1512. return -ENOMEM;
  1513. /* Adjust search limits by the desired length */
  1514. if (info->high_limit < length)
  1515. return -ENOMEM;
  1516. high_limit = info->high_limit - length;
  1517. if (info->low_limit > high_limit)
  1518. return -ENOMEM;
  1519. low_limit = info->low_limit + length;
  1520. /* Check if rbtree root looks promising */
  1521. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1522. goto check_highest;
  1523. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1524. if (vma->rb_subtree_gap < length)
  1525. goto check_highest;
  1526. while (true) {
  1527. /* Visit left subtree if it looks promising */
  1528. gap_end = vma->vm_start;
  1529. if (gap_end >= low_limit && vma->vm_rb.rb_left) {
  1530. struct vm_area_struct *left =
  1531. rb_entry(vma->vm_rb.rb_left,
  1532. struct vm_area_struct, vm_rb);
  1533. if (left->rb_subtree_gap >= length) {
  1534. vma = left;
  1535. continue;
  1536. }
  1537. }
  1538. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1539. check_current:
  1540. /* Check if current node has a suitable gap */
  1541. if (gap_start > high_limit)
  1542. return -ENOMEM;
  1543. if (gap_end >= low_limit && gap_end - gap_start >= length)
  1544. goto found;
  1545. /* Visit right subtree if it looks promising */
  1546. if (vma->vm_rb.rb_right) {
  1547. struct vm_area_struct *right =
  1548. rb_entry(vma->vm_rb.rb_right,
  1549. struct vm_area_struct, vm_rb);
  1550. if (right->rb_subtree_gap >= length) {
  1551. vma = right;
  1552. continue;
  1553. }
  1554. }
  1555. /* Go back up the rbtree to find next candidate node */
  1556. while (true) {
  1557. struct rb_node *prev = &vma->vm_rb;
  1558. if (!rb_parent(prev))
  1559. goto check_highest;
  1560. vma = rb_entry(rb_parent(prev),
  1561. struct vm_area_struct, vm_rb);
  1562. if (prev == vma->vm_rb.rb_left) {
  1563. gap_start = vma->vm_prev->vm_end;
  1564. gap_end = vma->vm_start;
  1565. goto check_current;
  1566. }
  1567. }
  1568. }
  1569. check_highest:
  1570. /* Check highest gap, which does not precede any rbtree node */
  1571. gap_start = mm->highest_vm_end;
  1572. gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
  1573. if (gap_start > high_limit)
  1574. return -ENOMEM;
  1575. found:
  1576. /* We found a suitable gap. Clip it with the original low_limit. */
  1577. if (gap_start < info->low_limit)
  1578. gap_start = info->low_limit;
  1579. /* Adjust gap address to the desired alignment */
  1580. gap_start += (info->align_offset - gap_start) & info->align_mask;
  1581. VM_BUG_ON(gap_start + info->length > info->high_limit);
  1582. VM_BUG_ON(gap_start + info->length > gap_end);
  1583. return gap_start;
  1584. }
  1585. unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
  1586. {
  1587. struct mm_struct *mm = current->mm;
  1588. struct vm_area_struct *vma;
  1589. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1590. /* Adjust search length to account for worst case alignment overhead */
  1591. length = info->length + info->align_mask;
  1592. if (length < info->length)
  1593. return -ENOMEM;
  1594. /*
  1595. * Adjust search limits by the desired length.
  1596. * See implementation comment at top of unmapped_area().
  1597. */
  1598. gap_end = info->high_limit;
  1599. if (gap_end < length)
  1600. return -ENOMEM;
  1601. high_limit = gap_end - length;
  1602. if (info->low_limit > high_limit)
  1603. return -ENOMEM;
  1604. low_limit = info->low_limit + length;
  1605. /* Check highest gap, which does not precede any rbtree node */
  1606. gap_start = mm->highest_vm_end;
  1607. if (gap_start <= high_limit)
  1608. goto found_highest;
  1609. /* Check if rbtree root looks promising */
  1610. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1611. return -ENOMEM;
  1612. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1613. if (vma->rb_subtree_gap < length)
  1614. return -ENOMEM;
  1615. while (true) {
  1616. /* Visit right subtree if it looks promising */
  1617. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1618. if (gap_start <= high_limit && vma->vm_rb.rb_right) {
  1619. struct vm_area_struct *right =
  1620. rb_entry(vma->vm_rb.rb_right,
  1621. struct vm_area_struct, vm_rb);
  1622. if (right->rb_subtree_gap >= length) {
  1623. vma = right;
  1624. continue;
  1625. }
  1626. }
  1627. check_current:
  1628. /* Check if current node has a suitable gap */
  1629. gap_end = vma->vm_start;
  1630. if (gap_end < low_limit)
  1631. return -ENOMEM;
  1632. if (gap_start <= high_limit && gap_end - gap_start >= length)
  1633. goto found;
  1634. /* Visit left subtree if it looks promising */
  1635. if (vma->vm_rb.rb_left) {
  1636. struct vm_area_struct *left =
  1637. rb_entry(vma->vm_rb.rb_left,
  1638. struct vm_area_struct, vm_rb);
  1639. if (left->rb_subtree_gap >= length) {
  1640. vma = left;
  1641. continue;
  1642. }
  1643. }
  1644. /* Go back up the rbtree to find next candidate node */
  1645. while (true) {
  1646. struct rb_node *prev = &vma->vm_rb;
  1647. if (!rb_parent(prev))
  1648. return -ENOMEM;
  1649. vma = rb_entry(rb_parent(prev),
  1650. struct vm_area_struct, vm_rb);
  1651. if (prev == vma->vm_rb.rb_right) {
  1652. gap_start = vma->vm_prev ?
  1653. vma->vm_prev->vm_end : 0;
  1654. goto check_current;
  1655. }
  1656. }
  1657. }
  1658. found:
  1659. /* We found a suitable gap. Clip it with the original high_limit. */
  1660. if (gap_end > info->high_limit)
  1661. gap_end = info->high_limit;
  1662. found_highest:
  1663. /* Compute highest gap address at the desired alignment */
  1664. gap_end -= info->length;
  1665. gap_end -= (gap_end - info->align_offset) & info->align_mask;
  1666. VM_BUG_ON(gap_end < info->low_limit);
  1667. VM_BUG_ON(gap_end < gap_start);
  1668. return gap_end;
  1669. }
  1670. /* Get an address range which is currently unmapped.
  1671. * For shmat() with addr=0.
  1672. *
  1673. * Ugly calling convention alert:
  1674. * Return value with the low bits set means error value,
  1675. * ie
  1676. * if (ret & ~PAGE_MASK)
  1677. * error = ret;
  1678. *
  1679. * This function "knows" that -ENOMEM has the bits set.
  1680. */
  1681. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1682. unsigned long
  1683. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1684. unsigned long len, unsigned long pgoff, unsigned long flags)
  1685. {
  1686. struct mm_struct *mm = current->mm;
  1687. struct vm_area_struct *vma;
  1688. struct vm_unmapped_area_info info;
  1689. if (len > TASK_SIZE - mmap_min_addr)
  1690. return -ENOMEM;
  1691. if (flags & MAP_FIXED)
  1692. return addr;
  1693. if (addr) {
  1694. addr = PAGE_ALIGN(addr);
  1695. vma = find_vma(mm, addr);
  1696. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  1697. (!vma || addr + len <= vma->vm_start))
  1698. return addr;
  1699. }
  1700. info.flags = 0;
  1701. info.length = len;
  1702. info.low_limit = mm->mmap_base;
  1703. info.high_limit = TASK_SIZE;
  1704. info.align_mask = 0;
  1705. return vm_unmapped_area(&info);
  1706. }
  1707. #endif
  1708. /*
  1709. * This mmap-allocator allocates new areas top-down from below the
  1710. * stack's low limit (the base):
  1711. */
  1712. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1713. unsigned long
  1714. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1715. const unsigned long len, const unsigned long pgoff,
  1716. const unsigned long flags)
  1717. {
  1718. struct vm_area_struct *vma;
  1719. struct mm_struct *mm = current->mm;
  1720. unsigned long addr = addr0;
  1721. struct vm_unmapped_area_info info;
  1722. /* requested length too big for entire address space */
  1723. if (len > TASK_SIZE - mmap_min_addr)
  1724. return -ENOMEM;
  1725. if (flags & MAP_FIXED)
  1726. return addr;
  1727. /* requesting a specific address */
  1728. if (addr) {
  1729. addr = PAGE_ALIGN(addr);
  1730. vma = find_vma(mm, addr);
  1731. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  1732. (!vma || addr + len <= vma->vm_start))
  1733. return addr;
  1734. }
  1735. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  1736. info.length = len;
  1737. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  1738. info.high_limit = mm->mmap_base;
  1739. info.align_mask = 0;
  1740. addr = vm_unmapped_area(&info);
  1741. /*
  1742. * A failed mmap() very likely causes application failure,
  1743. * so fall back to the bottom-up function here. This scenario
  1744. * can happen with large stack limits and large mmap()
  1745. * allocations.
  1746. */
  1747. if (addr & ~PAGE_MASK) {
  1748. VM_BUG_ON(addr != -ENOMEM);
  1749. info.flags = 0;
  1750. info.low_limit = TASK_UNMAPPED_BASE;
  1751. info.high_limit = TASK_SIZE;
  1752. addr = vm_unmapped_area(&info);
  1753. }
  1754. return addr;
  1755. }
  1756. #endif
  1757. unsigned long
  1758. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1759. unsigned long pgoff, unsigned long flags)
  1760. {
  1761. unsigned long (*get_area)(struct file *, unsigned long,
  1762. unsigned long, unsigned long, unsigned long);
  1763. unsigned long error = arch_mmap_check(addr, len, flags);
  1764. if (error)
  1765. return error;
  1766. /* Careful about overflows.. */
  1767. if (len > TASK_SIZE)
  1768. return -ENOMEM;
  1769. get_area = current->mm->get_unmapped_area;
  1770. if (file && file->f_op->get_unmapped_area)
  1771. get_area = file->f_op->get_unmapped_area;
  1772. addr = get_area(file, addr, len, pgoff, flags);
  1773. if (IS_ERR_VALUE(addr))
  1774. return addr;
  1775. if (addr > TASK_SIZE - len)
  1776. return -ENOMEM;
  1777. if (addr & ~PAGE_MASK)
  1778. return -EINVAL;
  1779. addr = arch_rebalance_pgtables(addr, len);
  1780. error = security_mmap_addr(addr);
  1781. return error ? error : addr;
  1782. }
  1783. EXPORT_SYMBOL(get_unmapped_area);
  1784. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1785. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1786. {
  1787. struct rb_node *rb_node;
  1788. struct vm_area_struct *vma;
  1789. /* Check the cache first. */
  1790. vma = vmacache_find(mm, addr);
  1791. if (likely(vma))
  1792. return vma;
  1793. rb_node = mm->mm_rb.rb_node;
  1794. vma = NULL;
  1795. while (rb_node) {
  1796. struct vm_area_struct *tmp;
  1797. tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1798. if (tmp->vm_end > addr) {
  1799. vma = tmp;
  1800. if (tmp->vm_start <= addr)
  1801. break;
  1802. rb_node = rb_node->rb_left;
  1803. } else
  1804. rb_node = rb_node->rb_right;
  1805. }
  1806. if (vma)
  1807. vmacache_update(addr, vma);
  1808. return vma;
  1809. }
  1810. EXPORT_SYMBOL(find_vma);
  1811. /*
  1812. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1813. */
  1814. struct vm_area_struct *
  1815. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1816. struct vm_area_struct **pprev)
  1817. {
  1818. struct vm_area_struct *vma;
  1819. vma = find_vma(mm, addr);
  1820. if (vma) {
  1821. *pprev = vma->vm_prev;
  1822. } else {
  1823. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1824. *pprev = NULL;
  1825. while (rb_node) {
  1826. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1827. rb_node = rb_node->rb_right;
  1828. }
  1829. }
  1830. return vma;
  1831. }
  1832. /*
  1833. * Verify that the stack growth is acceptable and
  1834. * update accounting. This is shared with both the
  1835. * grow-up and grow-down cases.
  1836. */
  1837. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1838. {
  1839. struct mm_struct *mm = vma->vm_mm;
  1840. struct rlimit *rlim = current->signal->rlim;
  1841. unsigned long new_start;
  1842. /* address space limit tests */
  1843. if (!may_expand_vm(mm, grow))
  1844. return -ENOMEM;
  1845. /* Stack limit test */
  1846. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
  1847. return -ENOMEM;
  1848. /* mlock limit tests */
  1849. if (vma->vm_flags & VM_LOCKED) {
  1850. unsigned long locked;
  1851. unsigned long limit;
  1852. locked = mm->locked_vm + grow;
  1853. limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
  1854. limit >>= PAGE_SHIFT;
  1855. if (locked > limit && !capable(CAP_IPC_LOCK))
  1856. return -ENOMEM;
  1857. }
  1858. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1859. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1860. vma->vm_end - size;
  1861. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1862. return -EFAULT;
  1863. /*
  1864. * Overcommit.. This must be the final test, as it will
  1865. * update security statistics.
  1866. */
  1867. if (security_vm_enough_memory_mm(mm, grow))
  1868. return -ENOMEM;
  1869. /* Ok, everything looks good - let it rip */
  1870. if (vma->vm_flags & VM_LOCKED)
  1871. mm->locked_vm += grow;
  1872. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1873. return 0;
  1874. }
  1875. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1876. /*
  1877. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1878. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1879. */
  1880. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1881. {
  1882. int error;
  1883. if (!(vma->vm_flags & VM_GROWSUP))
  1884. return -EFAULT;
  1885. /*
  1886. * We must make sure the anon_vma is allocated
  1887. * so that the anon_vma locking is not a noop.
  1888. */
  1889. if (unlikely(anon_vma_prepare(vma)))
  1890. return -ENOMEM;
  1891. vma_lock_anon_vma(vma);
  1892. /*
  1893. * vma->vm_start/vm_end cannot change under us because the caller
  1894. * is required to hold the mmap_sem in read mode. We need the
  1895. * anon_vma lock to serialize against concurrent expand_stacks.
  1896. * Also guard against wrapping around to address 0.
  1897. */
  1898. if (address < PAGE_ALIGN(address+4))
  1899. address = PAGE_ALIGN(address+4);
  1900. else {
  1901. vma_unlock_anon_vma(vma);
  1902. return -ENOMEM;
  1903. }
  1904. error = 0;
  1905. /* Somebody else might have raced and expanded it already */
  1906. if (address > vma->vm_end) {
  1907. unsigned long size, grow;
  1908. size = address - vma->vm_start;
  1909. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1910. error = -ENOMEM;
  1911. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  1912. error = acct_stack_growth(vma, size, grow);
  1913. if (!error) {
  1914. /*
  1915. * vma_gap_update() doesn't support concurrent
  1916. * updates, but we only hold a shared mmap_sem
  1917. * lock here, so we need to protect against
  1918. * concurrent vma expansions.
  1919. * vma_lock_anon_vma() doesn't help here, as
  1920. * we don't guarantee that all growable vmas
  1921. * in a mm share the same root anon vma.
  1922. * So, we reuse mm->page_table_lock to guard
  1923. * against concurrent vma expansions.
  1924. */
  1925. spin_lock(&vma->vm_mm->page_table_lock);
  1926. anon_vma_interval_tree_pre_update_vma(vma);
  1927. vma->vm_end = address;
  1928. anon_vma_interval_tree_post_update_vma(vma);
  1929. if (vma->vm_next)
  1930. vma_gap_update(vma->vm_next);
  1931. else
  1932. vma->vm_mm->highest_vm_end = address;
  1933. spin_unlock(&vma->vm_mm->page_table_lock);
  1934. perf_event_mmap(vma);
  1935. }
  1936. }
  1937. }
  1938. vma_unlock_anon_vma(vma);
  1939. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  1940. validate_mm(vma->vm_mm);
  1941. return error;
  1942. }
  1943. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1944. /*
  1945. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1946. */
  1947. int expand_downwards(struct vm_area_struct *vma,
  1948. unsigned long address)
  1949. {
  1950. int error;
  1951. /*
  1952. * We must make sure the anon_vma is allocated
  1953. * so that the anon_vma locking is not a noop.
  1954. */
  1955. if (unlikely(anon_vma_prepare(vma)))
  1956. return -ENOMEM;
  1957. address &= PAGE_MASK;
  1958. error = security_mmap_addr(address);
  1959. if (error)
  1960. return error;
  1961. vma_lock_anon_vma(vma);
  1962. /*
  1963. * vma->vm_start/vm_end cannot change under us because the caller
  1964. * is required to hold the mmap_sem in read mode. We need the
  1965. * anon_vma lock to serialize against concurrent expand_stacks.
  1966. */
  1967. /* Somebody else might have raced and expanded it already */
  1968. if (address < vma->vm_start) {
  1969. unsigned long size, grow;
  1970. size = vma->vm_end - address;
  1971. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1972. error = -ENOMEM;
  1973. if (grow <= vma->vm_pgoff) {
  1974. error = acct_stack_growth(vma, size, grow);
  1975. if (!error) {
  1976. /*
  1977. * vma_gap_update() doesn't support concurrent
  1978. * updates, but we only hold a shared mmap_sem
  1979. * lock here, so we need to protect against
  1980. * concurrent vma expansions.
  1981. * vma_lock_anon_vma() doesn't help here, as
  1982. * we don't guarantee that all growable vmas
  1983. * in a mm share the same root anon vma.
  1984. * So, we reuse mm->page_table_lock to guard
  1985. * against concurrent vma expansions.
  1986. */
  1987. spin_lock(&vma->vm_mm->page_table_lock);
  1988. anon_vma_interval_tree_pre_update_vma(vma);
  1989. vma->vm_start = address;
  1990. vma->vm_pgoff -= grow;
  1991. anon_vma_interval_tree_post_update_vma(vma);
  1992. vma_gap_update(vma);
  1993. spin_unlock(&vma->vm_mm->page_table_lock);
  1994. perf_event_mmap(vma);
  1995. }
  1996. }
  1997. }
  1998. vma_unlock_anon_vma(vma);
  1999. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  2000. validate_mm(vma->vm_mm);
  2001. return error;
  2002. }
  2003. /*
  2004. * Note how expand_stack() refuses to expand the stack all the way to
  2005. * abut the next virtual mapping, *unless* that mapping itself is also
  2006. * a stack mapping. We want to leave room for a guard page, after all
  2007. * (the guard page itself is not added here, that is done by the
  2008. * actual page faulting logic)
  2009. *
  2010. * This matches the behavior of the guard page logic (see mm/memory.c:
  2011. * check_stack_guard_page()), which only allows the guard page to be
  2012. * removed under these circumstances.
  2013. */
  2014. #ifdef CONFIG_STACK_GROWSUP
  2015. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  2016. {
  2017. struct vm_area_struct *next;
  2018. address &= PAGE_MASK;
  2019. next = vma->vm_next;
  2020. if (next && next->vm_start == address + PAGE_SIZE) {
  2021. if (!(next->vm_flags & VM_GROWSUP))
  2022. return -ENOMEM;
  2023. }
  2024. return expand_upwards(vma, address);
  2025. }
  2026. struct vm_area_struct *
  2027. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  2028. {
  2029. struct vm_area_struct *vma, *prev;
  2030. addr &= PAGE_MASK;
  2031. vma = find_vma_prev(mm, addr, &prev);
  2032. if (vma && (vma->vm_start <= addr))
  2033. return vma;
  2034. if (!prev || expand_stack(prev, addr))
  2035. return NULL;
  2036. if (prev->vm_flags & VM_LOCKED)
  2037. __mlock_vma_pages_range(prev, addr, prev->vm_end, NULL);
  2038. return prev;
  2039. }
  2040. #else
  2041. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  2042. {
  2043. struct vm_area_struct *prev;
  2044. address &= PAGE_MASK;
  2045. prev = vma->vm_prev;
  2046. if (prev && prev->vm_end == address) {
  2047. if (!(prev->vm_flags & VM_GROWSDOWN))
  2048. return -ENOMEM;
  2049. }
  2050. return expand_downwards(vma, address);
  2051. }
  2052. struct vm_area_struct *
  2053. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  2054. {
  2055. struct vm_area_struct *vma;
  2056. unsigned long start;
  2057. addr &= PAGE_MASK;
  2058. vma = find_vma(mm, addr);
  2059. if (!vma)
  2060. return NULL;
  2061. if (vma->vm_start <= addr)
  2062. return vma;
  2063. if (!(vma->vm_flags & VM_GROWSDOWN))
  2064. return NULL;
  2065. start = vma->vm_start;
  2066. if (expand_stack(vma, addr))
  2067. return NULL;
  2068. if (vma->vm_flags & VM_LOCKED)
  2069. __mlock_vma_pages_range(vma, addr, start, NULL);
  2070. return vma;
  2071. }
  2072. #endif
  2073. EXPORT_SYMBOL_GPL(find_extend_vma);
  2074. /*
  2075. * Ok - we have the memory areas we should free on the vma list,
  2076. * so release them, and do the vma updates.
  2077. *
  2078. * Called with the mm semaphore held.
  2079. */
  2080. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  2081. {
  2082. unsigned long nr_accounted = 0;
  2083. /* Update high watermark before we lower total_vm */
  2084. update_hiwater_vm(mm);
  2085. do {
  2086. long nrpages = vma_pages(vma);
  2087. if (vma->vm_flags & VM_ACCOUNT)
  2088. nr_accounted += nrpages;
  2089. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  2090. vma = remove_vma(vma);
  2091. } while (vma);
  2092. vm_unacct_memory(nr_accounted);
  2093. validate_mm(mm);
  2094. }
  2095. /*
  2096. * Get rid of page table information in the indicated region.
  2097. *
  2098. * Called with the mm semaphore held.
  2099. */
  2100. static void unmap_region(struct mm_struct *mm,
  2101. struct vm_area_struct *vma, struct vm_area_struct *prev,
  2102. unsigned long start, unsigned long end)
  2103. {
  2104. struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
  2105. struct mmu_gather tlb;
  2106. lru_add_drain();
  2107. tlb_gather_mmu(&tlb, mm, start, end);
  2108. update_hiwater_rss(mm);
  2109. unmap_vmas(&tlb, vma, start, end);
  2110. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  2111. next ? next->vm_start : USER_PGTABLES_CEILING);
  2112. tlb_finish_mmu(&tlb, start, end);
  2113. }
  2114. /*
  2115. * Create a list of vma's touched by the unmap, removing them from the mm's
  2116. * vma list as we go..
  2117. */
  2118. static void
  2119. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  2120. struct vm_area_struct *prev, unsigned long end)
  2121. {
  2122. struct vm_area_struct **insertion_point;
  2123. struct vm_area_struct *tail_vma = NULL;
  2124. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  2125. vma->vm_prev = NULL;
  2126. do {
  2127. vma_rb_erase(vma, &mm->mm_rb);
  2128. mm->map_count--;
  2129. tail_vma = vma;
  2130. vma = vma->vm_next;
  2131. } while (vma && vma->vm_start < end);
  2132. *insertion_point = vma;
  2133. if (vma) {
  2134. vma->vm_prev = prev;
  2135. vma_gap_update(vma);
  2136. } else
  2137. mm->highest_vm_end = prev ? prev->vm_end : 0;
  2138. tail_vma->vm_next = NULL;
  2139. /* Kill the cache */
  2140. vmacache_invalidate(mm);
  2141. }
  2142. /*
  2143. * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
  2144. * munmap path where it doesn't make sense to fail.
  2145. */
  2146. static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2147. unsigned long addr, int new_below)
  2148. {
  2149. struct vm_area_struct *new;
  2150. int err = -ENOMEM;
  2151. if (is_vm_hugetlb_page(vma) && (addr &
  2152. ~(huge_page_mask(hstate_vma(vma)))))
  2153. return -EINVAL;
  2154. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2155. if (!new)
  2156. goto out_err;
  2157. /* most fields are the same, copy all, and then fixup */
  2158. *new = *vma;
  2159. INIT_LIST_HEAD(&new->anon_vma_chain);
  2160. if (new_below)
  2161. new->vm_end = addr;
  2162. else {
  2163. new->vm_start = addr;
  2164. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  2165. }
  2166. err = vma_dup_policy(vma, new);
  2167. if (err)
  2168. goto out_free_vma;
  2169. err = anon_vma_clone(new, vma);
  2170. if (err)
  2171. goto out_free_mpol;
  2172. if (new->vm_file)
  2173. get_file(new->vm_file);
  2174. if (new->vm_ops && new->vm_ops->open)
  2175. new->vm_ops->open(new);
  2176. if (new_below)
  2177. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  2178. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  2179. else
  2180. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  2181. /* Success. */
  2182. if (!err)
  2183. return 0;
  2184. /* Clean everything up if vma_adjust failed. */
  2185. if (new->vm_ops && new->vm_ops->close)
  2186. new->vm_ops->close(new);
  2187. if (new->vm_file)
  2188. fput(new->vm_file);
  2189. unlink_anon_vmas(new);
  2190. out_free_mpol:
  2191. mpol_put(vma_policy(new));
  2192. out_free_vma:
  2193. kmem_cache_free(vm_area_cachep, new);
  2194. out_err:
  2195. return err;
  2196. }
  2197. /*
  2198. * Split a vma into two pieces at address 'addr', a new vma is allocated
  2199. * either for the first part or the tail.
  2200. */
  2201. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2202. unsigned long addr, int new_below)
  2203. {
  2204. if (mm->map_count >= sysctl_max_map_count)
  2205. return -ENOMEM;
  2206. return __split_vma(mm, vma, addr, new_below);
  2207. }
  2208. /* Munmap is split into 2 main parts -- this part which finds
  2209. * what needs doing, and the areas themselves, which do the
  2210. * work. This now handles partial unmappings.
  2211. * Jeremy Fitzhardinge <jeremy@goop.org>
  2212. */
  2213. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  2214. {
  2215. unsigned long end;
  2216. struct vm_area_struct *vma, *prev, *last;
  2217. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  2218. return -EINVAL;
  2219. len = PAGE_ALIGN(len);
  2220. if (len == 0)
  2221. return -EINVAL;
  2222. /* Find the first overlapping VMA */
  2223. vma = find_vma(mm, start);
  2224. if (!vma)
  2225. return 0;
  2226. prev = vma->vm_prev;
  2227. /* we have start < vma->vm_end */
  2228. /* if it doesn't overlap, we have nothing.. */
  2229. end = start + len;
  2230. if (vma->vm_start >= end)
  2231. return 0;
  2232. /*
  2233. * If we need to split any vma, do it now to save pain later.
  2234. *
  2235. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  2236. * unmapped vm_area_struct will remain in use: so lower split_vma
  2237. * places tmp vma above, and higher split_vma places tmp vma below.
  2238. */
  2239. if (start > vma->vm_start) {
  2240. int error;
  2241. /*
  2242. * Make sure that map_count on return from munmap() will
  2243. * not exceed its limit; but let map_count go just above
  2244. * its limit temporarily, to help free resources as expected.
  2245. */
  2246. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  2247. return -ENOMEM;
  2248. error = __split_vma(mm, vma, start, 0);
  2249. if (error)
  2250. return error;
  2251. prev = vma;
  2252. }
  2253. /* Does it split the last one? */
  2254. last = find_vma(mm, end);
  2255. if (last && end > last->vm_start) {
  2256. int error = __split_vma(mm, last, end, 1);
  2257. if (error)
  2258. return error;
  2259. }
  2260. vma = prev ? prev->vm_next : mm->mmap;
  2261. /*
  2262. * unlock any mlock()ed ranges before detaching vmas
  2263. */
  2264. if (mm->locked_vm) {
  2265. struct vm_area_struct *tmp = vma;
  2266. while (tmp && tmp->vm_start < end) {
  2267. if (tmp->vm_flags & VM_LOCKED) {
  2268. mm->locked_vm -= vma_pages(tmp);
  2269. munlock_vma_pages_all(tmp);
  2270. }
  2271. tmp = tmp->vm_next;
  2272. }
  2273. }
  2274. /*
  2275. * Remove the vma's, and unmap the actual pages
  2276. */
  2277. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2278. unmap_region(mm, vma, prev, start, end);
  2279. arch_unmap(mm, vma, start, end);
  2280. /* Fix up all other VM information */
  2281. remove_vma_list(mm, vma);
  2282. return 0;
  2283. }
  2284. int vm_munmap(unsigned long start, size_t len)
  2285. {
  2286. int ret;
  2287. struct mm_struct *mm = current->mm;
  2288. down_write(&mm->mmap_sem);
  2289. ret = do_munmap(mm, start, len);
  2290. up_write(&mm->mmap_sem);
  2291. return ret;
  2292. }
  2293. EXPORT_SYMBOL(vm_munmap);
  2294. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2295. {
  2296. profile_munmap(addr);
  2297. return vm_munmap(addr, len);
  2298. }
  2299. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2300. {
  2301. #ifdef CONFIG_DEBUG_VM
  2302. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2303. WARN_ON(1);
  2304. up_read(&mm->mmap_sem);
  2305. }
  2306. #endif
  2307. }
  2308. /*
  2309. * this is really a simplified "do_mmap". it only handles
  2310. * anonymous maps. eventually we may be able to do some
  2311. * brk-specific accounting here.
  2312. */
  2313. static unsigned long do_brk(unsigned long addr, unsigned long len)
  2314. {
  2315. struct mm_struct *mm = current->mm;
  2316. struct vm_area_struct *vma, *prev;
  2317. unsigned long flags;
  2318. struct rb_node **rb_link, *rb_parent;
  2319. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2320. int error;
  2321. len = PAGE_ALIGN(len);
  2322. if (!len)
  2323. return addr;
  2324. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2325. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2326. if (error & ~PAGE_MASK)
  2327. return error;
  2328. error = mlock_future_check(mm, mm->def_flags, len);
  2329. if (error)
  2330. return error;
  2331. /*
  2332. * mm->mmap_sem is required to protect against another thread
  2333. * changing the mappings in case we sleep.
  2334. */
  2335. verify_mm_writelocked(mm);
  2336. /*
  2337. * Clear old maps. this also does some error checking for us
  2338. */
  2339. munmap_back:
  2340. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  2341. if (do_munmap(mm, addr, len))
  2342. return -ENOMEM;
  2343. goto munmap_back;
  2344. }
  2345. /* Check against address space limits *after* clearing old maps... */
  2346. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  2347. return -ENOMEM;
  2348. if (mm->map_count > sysctl_max_map_count)
  2349. return -ENOMEM;
  2350. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2351. return -ENOMEM;
  2352. /* Can we just expand an old private anonymous mapping? */
  2353. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2354. NULL, NULL, pgoff, NULL);
  2355. if (vma)
  2356. goto out;
  2357. /*
  2358. * create a vma struct for an anonymous mapping
  2359. */
  2360. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2361. if (!vma) {
  2362. vm_unacct_memory(len >> PAGE_SHIFT);
  2363. return -ENOMEM;
  2364. }
  2365. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2366. vma->vm_mm = mm;
  2367. vma->vm_start = addr;
  2368. vma->vm_end = addr + len;
  2369. vma->vm_pgoff = pgoff;
  2370. vma->vm_flags = flags;
  2371. vma->vm_page_prot = vm_get_page_prot(flags);
  2372. vma_link(mm, vma, prev, rb_link, rb_parent);
  2373. out:
  2374. perf_event_mmap(vma);
  2375. mm->total_vm += len >> PAGE_SHIFT;
  2376. if (flags & VM_LOCKED)
  2377. mm->locked_vm += (len >> PAGE_SHIFT);
  2378. vma->vm_flags |= VM_SOFTDIRTY;
  2379. return addr;
  2380. }
  2381. unsigned long vm_brk(unsigned long addr, unsigned long len)
  2382. {
  2383. struct mm_struct *mm = current->mm;
  2384. unsigned long ret;
  2385. bool populate;
  2386. down_write(&mm->mmap_sem);
  2387. ret = do_brk(addr, len);
  2388. populate = ((mm->def_flags & VM_LOCKED) != 0);
  2389. up_write(&mm->mmap_sem);
  2390. if (populate)
  2391. mm_populate(addr, len);
  2392. return ret;
  2393. }
  2394. EXPORT_SYMBOL(vm_brk);
  2395. /* Release all mmaps. */
  2396. void exit_mmap(struct mm_struct *mm)
  2397. {
  2398. struct mmu_gather tlb;
  2399. struct vm_area_struct *vma;
  2400. unsigned long nr_accounted = 0;
  2401. /* mm's last user has gone, and its about to be pulled down */
  2402. mmu_notifier_release(mm);
  2403. if (mm->locked_vm) {
  2404. vma = mm->mmap;
  2405. while (vma) {
  2406. if (vma->vm_flags & VM_LOCKED)
  2407. munlock_vma_pages_all(vma);
  2408. vma = vma->vm_next;
  2409. }
  2410. }
  2411. arch_exit_mmap(mm);
  2412. vma = mm->mmap;
  2413. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2414. return;
  2415. lru_add_drain();
  2416. flush_cache_mm(mm);
  2417. tlb_gather_mmu(&tlb, mm, 0, -1);
  2418. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2419. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2420. unmap_vmas(&tlb, vma, 0, -1);
  2421. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
  2422. tlb_finish_mmu(&tlb, 0, -1);
  2423. /*
  2424. * Walk the list again, actually closing and freeing it,
  2425. * with preemption enabled, without holding any MM locks.
  2426. */
  2427. while (vma) {
  2428. if (vma->vm_flags & VM_ACCOUNT)
  2429. nr_accounted += vma_pages(vma);
  2430. vma = remove_vma(vma);
  2431. }
  2432. vm_unacct_memory(nr_accounted);
  2433. WARN_ON(atomic_long_read(&mm->nr_ptes) >
  2434. (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  2435. }
  2436. /* Insert vm structure into process list sorted by address
  2437. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2438. * then i_mmap_rwsem is taken here.
  2439. */
  2440. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  2441. {
  2442. struct vm_area_struct *prev;
  2443. struct rb_node **rb_link, *rb_parent;
  2444. /*
  2445. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2446. * until its first write fault, when page's anon_vma and index
  2447. * are set. But now set the vm_pgoff it will almost certainly
  2448. * end up with (unless mremap moves it elsewhere before that
  2449. * first wfault), so /proc/pid/maps tells a consistent story.
  2450. *
  2451. * By setting it to reflect the virtual start address of the
  2452. * vma, merges and splits can happen in a seamless way, just
  2453. * using the existing file pgoff checks and manipulations.
  2454. * Similarly in do_mmap_pgoff and in do_brk.
  2455. */
  2456. if (!vma->vm_file) {
  2457. BUG_ON(vma->anon_vma);
  2458. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2459. }
  2460. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  2461. &prev, &rb_link, &rb_parent))
  2462. return -ENOMEM;
  2463. if ((vma->vm_flags & VM_ACCOUNT) &&
  2464. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2465. return -ENOMEM;
  2466. vma_link(mm, vma, prev, rb_link, rb_parent);
  2467. return 0;
  2468. }
  2469. /*
  2470. * Copy the vma structure to a new location in the same mm,
  2471. * prior to moving page table entries, to effect an mremap move.
  2472. */
  2473. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2474. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2475. bool *need_rmap_locks)
  2476. {
  2477. struct vm_area_struct *vma = *vmap;
  2478. unsigned long vma_start = vma->vm_start;
  2479. struct mm_struct *mm = vma->vm_mm;
  2480. struct vm_area_struct *new_vma, *prev;
  2481. struct rb_node **rb_link, *rb_parent;
  2482. bool faulted_in_anon_vma = true;
  2483. /*
  2484. * If anonymous vma has not yet been faulted, update new pgoff
  2485. * to match new location, to increase its chance of merging.
  2486. */
  2487. if (unlikely(!vma->vm_file && !vma->anon_vma)) {
  2488. pgoff = addr >> PAGE_SHIFT;
  2489. faulted_in_anon_vma = false;
  2490. }
  2491. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
  2492. return NULL; /* should never get here */
  2493. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2494. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  2495. if (new_vma) {
  2496. /*
  2497. * Source vma may have been merged into new_vma
  2498. */
  2499. if (unlikely(vma_start >= new_vma->vm_start &&
  2500. vma_start < new_vma->vm_end)) {
  2501. /*
  2502. * The only way we can get a vma_merge with
  2503. * self during an mremap is if the vma hasn't
  2504. * been faulted in yet and we were allowed to
  2505. * reset the dst vma->vm_pgoff to the
  2506. * destination address of the mremap to allow
  2507. * the merge to happen. mremap must change the
  2508. * vm_pgoff linearity between src and dst vmas
  2509. * (in turn preventing a vma_merge) to be
  2510. * safe. It is only safe to keep the vm_pgoff
  2511. * linear if there are no pages mapped yet.
  2512. */
  2513. VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
  2514. *vmap = vma = new_vma;
  2515. }
  2516. *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
  2517. } else {
  2518. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2519. if (new_vma) {
  2520. *new_vma = *vma;
  2521. new_vma->vm_start = addr;
  2522. new_vma->vm_end = addr + len;
  2523. new_vma->vm_pgoff = pgoff;
  2524. if (vma_dup_policy(vma, new_vma))
  2525. goto out_free_vma;
  2526. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2527. if (anon_vma_clone(new_vma, vma))
  2528. goto out_free_mempol;
  2529. if (new_vma->vm_file)
  2530. get_file(new_vma->vm_file);
  2531. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2532. new_vma->vm_ops->open(new_vma);
  2533. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2534. *need_rmap_locks = false;
  2535. }
  2536. }
  2537. return new_vma;
  2538. out_free_mempol:
  2539. mpol_put(vma_policy(new_vma));
  2540. out_free_vma:
  2541. kmem_cache_free(vm_area_cachep, new_vma);
  2542. return NULL;
  2543. }
  2544. /*
  2545. * Return true if the calling process may expand its vm space by the passed
  2546. * number of pages
  2547. */
  2548. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  2549. {
  2550. unsigned long cur = mm->total_vm; /* pages */
  2551. unsigned long lim;
  2552. lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
  2553. if (cur + npages > lim)
  2554. return 0;
  2555. return 1;
  2556. }
  2557. static int special_mapping_fault(struct vm_area_struct *vma,
  2558. struct vm_fault *vmf);
  2559. /*
  2560. * Having a close hook prevents vma merging regardless of flags.
  2561. */
  2562. static void special_mapping_close(struct vm_area_struct *vma)
  2563. {
  2564. }
  2565. static const char *special_mapping_name(struct vm_area_struct *vma)
  2566. {
  2567. return ((struct vm_special_mapping *)vma->vm_private_data)->name;
  2568. }
  2569. static const struct vm_operations_struct special_mapping_vmops = {
  2570. .close = special_mapping_close,
  2571. .fault = special_mapping_fault,
  2572. .name = special_mapping_name,
  2573. };
  2574. static const struct vm_operations_struct legacy_special_mapping_vmops = {
  2575. .close = special_mapping_close,
  2576. .fault = special_mapping_fault,
  2577. };
  2578. static int special_mapping_fault(struct vm_area_struct *vma,
  2579. struct vm_fault *vmf)
  2580. {
  2581. pgoff_t pgoff;
  2582. struct page **pages;
  2583. /*
  2584. * special mappings have no vm_file, and in that case, the mm
  2585. * uses vm_pgoff internally. So we have to subtract it from here.
  2586. * We are allowed to do this because we are the mm; do not copy
  2587. * this code into drivers!
  2588. */
  2589. pgoff = vmf->pgoff - vma->vm_pgoff;
  2590. if (vma->vm_ops == &legacy_special_mapping_vmops)
  2591. pages = vma->vm_private_data;
  2592. else
  2593. pages = ((struct vm_special_mapping *)vma->vm_private_data)->
  2594. pages;
  2595. for (; pgoff && *pages; ++pages)
  2596. pgoff--;
  2597. if (*pages) {
  2598. struct page *page = *pages;
  2599. get_page(page);
  2600. vmf->page = page;
  2601. return 0;
  2602. }
  2603. return VM_FAULT_SIGBUS;
  2604. }
  2605. static struct vm_area_struct *__install_special_mapping(
  2606. struct mm_struct *mm,
  2607. unsigned long addr, unsigned long len,
  2608. unsigned long vm_flags, const struct vm_operations_struct *ops,
  2609. void *priv)
  2610. {
  2611. int ret;
  2612. struct vm_area_struct *vma;
  2613. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2614. if (unlikely(vma == NULL))
  2615. return ERR_PTR(-ENOMEM);
  2616. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2617. vma->vm_mm = mm;
  2618. vma->vm_start = addr;
  2619. vma->vm_end = addr + len;
  2620. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
  2621. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2622. vma->vm_ops = ops;
  2623. vma->vm_private_data = priv;
  2624. ret = insert_vm_struct(mm, vma);
  2625. if (ret)
  2626. goto out;
  2627. mm->total_vm += len >> PAGE_SHIFT;
  2628. perf_event_mmap(vma);
  2629. return vma;
  2630. out:
  2631. kmem_cache_free(vm_area_cachep, vma);
  2632. return ERR_PTR(ret);
  2633. }
  2634. /*
  2635. * Called with mm->mmap_sem held for writing.
  2636. * Insert a new vma covering the given region, with the given flags.
  2637. * Its pages are supplied by the given array of struct page *.
  2638. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2639. * The region past the last page supplied will always produce SIGBUS.
  2640. * The array pointer and the pages it points to are assumed to stay alive
  2641. * for as long as this mapping might exist.
  2642. */
  2643. struct vm_area_struct *_install_special_mapping(
  2644. struct mm_struct *mm,
  2645. unsigned long addr, unsigned long len,
  2646. unsigned long vm_flags, const struct vm_special_mapping *spec)
  2647. {
  2648. return __install_special_mapping(mm, addr, len, vm_flags,
  2649. &special_mapping_vmops, (void *)spec);
  2650. }
  2651. int install_special_mapping(struct mm_struct *mm,
  2652. unsigned long addr, unsigned long len,
  2653. unsigned long vm_flags, struct page **pages)
  2654. {
  2655. struct vm_area_struct *vma = __install_special_mapping(
  2656. mm, addr, len, vm_flags, &legacy_special_mapping_vmops,
  2657. (void *)pages);
  2658. return PTR_ERR_OR_ZERO(vma);
  2659. }
  2660. static DEFINE_MUTEX(mm_all_locks_mutex);
  2661. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2662. {
  2663. if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2664. /*
  2665. * The LSB of head.next can't change from under us
  2666. * because we hold the mm_all_locks_mutex.
  2667. */
  2668. down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
  2669. /*
  2670. * We can safely modify head.next after taking the
  2671. * anon_vma->root->rwsem. If some other vma in this mm shares
  2672. * the same anon_vma we won't take it again.
  2673. *
  2674. * No need of atomic instructions here, head.next
  2675. * can't change from under us thanks to the
  2676. * anon_vma->root->rwsem.
  2677. */
  2678. if (__test_and_set_bit(0, (unsigned long *)
  2679. &anon_vma->root->rb_root.rb_node))
  2680. BUG();
  2681. }
  2682. }
  2683. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2684. {
  2685. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2686. /*
  2687. * AS_MM_ALL_LOCKS can't change from under us because
  2688. * we hold the mm_all_locks_mutex.
  2689. *
  2690. * Operations on ->flags have to be atomic because
  2691. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2692. * mm_all_locks_mutex, there may be other cpus
  2693. * changing other bitflags in parallel to us.
  2694. */
  2695. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2696. BUG();
  2697. down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
  2698. }
  2699. }
  2700. /*
  2701. * This operation locks against the VM for all pte/vma/mm related
  2702. * operations that could ever happen on a certain mm. This includes
  2703. * vmtruncate, try_to_unmap, and all page faults.
  2704. *
  2705. * The caller must take the mmap_sem in write mode before calling
  2706. * mm_take_all_locks(). The caller isn't allowed to release the
  2707. * mmap_sem until mm_drop_all_locks() returns.
  2708. *
  2709. * mmap_sem in write mode is required in order to block all operations
  2710. * that could modify pagetables and free pages without need of
  2711. * altering the vma layout (for example populate_range() with
  2712. * nonlinear vmas). It's also needed in write mode to avoid new
  2713. * anon_vmas to be associated with existing vmas.
  2714. *
  2715. * A single task can't take more than one mm_take_all_locks() in a row
  2716. * or it would deadlock.
  2717. *
  2718. * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
  2719. * mapping->flags avoid to take the same lock twice, if more than one
  2720. * vma in this mm is backed by the same anon_vma or address_space.
  2721. *
  2722. * We can take all the locks in random order because the VM code
  2723. * taking i_mmap_rwsem or anon_vma->rwsem outside the mmap_sem never
  2724. * takes more than one of them in a row. Secondly we're protected
  2725. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2726. *
  2727. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2728. * that may have to take thousand of locks.
  2729. *
  2730. * mm_take_all_locks() can fail if it's interrupted by signals.
  2731. */
  2732. int mm_take_all_locks(struct mm_struct *mm)
  2733. {
  2734. struct vm_area_struct *vma;
  2735. struct anon_vma_chain *avc;
  2736. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2737. mutex_lock(&mm_all_locks_mutex);
  2738. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2739. if (signal_pending(current))
  2740. goto out_unlock;
  2741. if (vma->vm_file && vma->vm_file->f_mapping)
  2742. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2743. }
  2744. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2745. if (signal_pending(current))
  2746. goto out_unlock;
  2747. if (vma->anon_vma)
  2748. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2749. vm_lock_anon_vma(mm, avc->anon_vma);
  2750. }
  2751. return 0;
  2752. out_unlock:
  2753. mm_drop_all_locks(mm);
  2754. return -EINTR;
  2755. }
  2756. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2757. {
  2758. if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2759. /*
  2760. * The LSB of head.next can't change to 0 from under
  2761. * us because we hold the mm_all_locks_mutex.
  2762. *
  2763. * We must however clear the bitflag before unlocking
  2764. * the vma so the users using the anon_vma->rb_root will
  2765. * never see our bitflag.
  2766. *
  2767. * No need of atomic instructions here, head.next
  2768. * can't change from under us until we release the
  2769. * anon_vma->root->rwsem.
  2770. */
  2771. if (!__test_and_clear_bit(0, (unsigned long *)
  2772. &anon_vma->root->rb_root.rb_node))
  2773. BUG();
  2774. anon_vma_unlock_write(anon_vma);
  2775. }
  2776. }
  2777. static void vm_unlock_mapping(struct address_space *mapping)
  2778. {
  2779. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2780. /*
  2781. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2782. * because we hold the mm_all_locks_mutex.
  2783. */
  2784. i_mmap_unlock_write(mapping);
  2785. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2786. &mapping->flags))
  2787. BUG();
  2788. }
  2789. }
  2790. /*
  2791. * The mmap_sem cannot be released by the caller until
  2792. * mm_drop_all_locks() returns.
  2793. */
  2794. void mm_drop_all_locks(struct mm_struct *mm)
  2795. {
  2796. struct vm_area_struct *vma;
  2797. struct anon_vma_chain *avc;
  2798. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2799. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2800. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2801. if (vma->anon_vma)
  2802. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2803. vm_unlock_anon_vma(avc->anon_vma);
  2804. if (vma->vm_file && vma->vm_file->f_mapping)
  2805. vm_unlock_mapping(vma->vm_file->f_mapping);
  2806. }
  2807. mutex_unlock(&mm_all_locks_mutex);
  2808. }
  2809. /*
  2810. * initialise the VMA slab
  2811. */
  2812. void __init mmap_init(void)
  2813. {
  2814. int ret;
  2815. ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
  2816. VM_BUG_ON(ret);
  2817. }
  2818. /*
  2819. * Initialise sysctl_user_reserve_kbytes.
  2820. *
  2821. * This is intended to prevent a user from starting a single memory hogging
  2822. * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
  2823. * mode.
  2824. *
  2825. * The default value is min(3% of free memory, 128MB)
  2826. * 128MB is enough to recover with sshd/login, bash, and top/kill.
  2827. */
  2828. static int init_user_reserve(void)
  2829. {
  2830. unsigned long free_kbytes;
  2831. free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  2832. sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
  2833. return 0;
  2834. }
  2835. subsys_initcall(init_user_reserve);
  2836. /*
  2837. * Initialise sysctl_admin_reserve_kbytes.
  2838. *
  2839. * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
  2840. * to log in and kill a memory hogging process.
  2841. *
  2842. * Systems with more than 256MB will reserve 8MB, enough to recover
  2843. * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
  2844. * only reserve 3% of free pages by default.
  2845. */
  2846. static int init_admin_reserve(void)
  2847. {
  2848. unsigned long free_kbytes;
  2849. free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  2850. sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
  2851. return 0;
  2852. }
  2853. subsys_initcall(init_admin_reserve);
  2854. /*
  2855. * Reinititalise user and admin reserves if memory is added or removed.
  2856. *
  2857. * The default user reserve max is 128MB, and the default max for the
  2858. * admin reserve is 8MB. These are usually, but not always, enough to
  2859. * enable recovery from a memory hogging process using login/sshd, a shell,
  2860. * and tools like top. It may make sense to increase or even disable the
  2861. * reserve depending on the existence of swap or variations in the recovery
  2862. * tools. So, the admin may have changed them.
  2863. *
  2864. * If memory is added and the reserves have been eliminated or increased above
  2865. * the default max, then we'll trust the admin.
  2866. *
  2867. * If memory is removed and there isn't enough free memory, then we
  2868. * need to reset the reserves.
  2869. *
  2870. * Otherwise keep the reserve set by the admin.
  2871. */
  2872. static int reserve_mem_notifier(struct notifier_block *nb,
  2873. unsigned long action, void *data)
  2874. {
  2875. unsigned long tmp, free_kbytes;
  2876. switch (action) {
  2877. case MEM_ONLINE:
  2878. /* Default max is 128MB. Leave alone if modified by operator. */
  2879. tmp = sysctl_user_reserve_kbytes;
  2880. if (0 < tmp && tmp < (1UL << 17))
  2881. init_user_reserve();
  2882. /* Default max is 8MB. Leave alone if modified by operator. */
  2883. tmp = sysctl_admin_reserve_kbytes;
  2884. if (0 < tmp && tmp < (1UL << 13))
  2885. init_admin_reserve();
  2886. break;
  2887. case MEM_OFFLINE:
  2888. free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  2889. if (sysctl_user_reserve_kbytes > free_kbytes) {
  2890. init_user_reserve();
  2891. pr_info("vm.user_reserve_kbytes reset to %lu\n",
  2892. sysctl_user_reserve_kbytes);
  2893. }
  2894. if (sysctl_admin_reserve_kbytes > free_kbytes) {
  2895. init_admin_reserve();
  2896. pr_info("vm.admin_reserve_kbytes reset to %lu\n",
  2897. sysctl_admin_reserve_kbytes);
  2898. }
  2899. break;
  2900. default:
  2901. break;
  2902. }
  2903. return NOTIFY_OK;
  2904. }
  2905. static struct notifier_block reserve_mem_nb = {
  2906. .notifier_call = reserve_mem_notifier,
  2907. };
  2908. static int __meminit init_reserve_notifier(void)
  2909. {
  2910. if (register_hotmemory_notifier(&reserve_mem_nb))
  2911. pr_err("Failed registering memory add/remove notifier for admin reserve\n");
  2912. return 0;
  2913. }
  2914. subsys_initcall(init_reserve_notifier);