mmap.c 86 KB

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