memory.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. /*
  2. * linux/mm/memory.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * demand-loading started 01.12.91 - seems it is high on the list of
  8. * things wanted, and it should be easy to implement. - Linus
  9. */
  10. /*
  11. * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  12. * pages started 02.12.91, seems to work. - Linus.
  13. *
  14. * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  15. * would have taken more than the 6M I have free, but it worked well as
  16. * far as I could see.
  17. *
  18. * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  19. */
  20. /*
  21. * Real VM (paging to/from disk) started 18.12.91. Much more work and
  22. * thought has to go into this. Oh, well..
  23. * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
  24. * Found it. Everything seems to work now.
  25. * 20.12.91 - Ok, making the swap-device changeable like the root.
  26. */
  27. /*
  28. * 05.04.94 - Multi-page memory management added for v1.1.
  29. * Idea by Alex Bligh (alex@cconcepts.co.uk)
  30. *
  31. * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
  32. * (Gerhard.Wichert@pdb.siemens.de)
  33. *
  34. * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
  35. */
  36. #include <linux/kernel_stat.h>
  37. #include <linux/mm.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/mman.h>
  40. #include <linux/swap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/rmap.h>
  44. #include <linux/module.h>
  45. #include <linux/delayacct.h>
  46. #include <linux/init.h>
  47. #include <linux/writeback.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/tlb.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/pgtable.h>
  53. #include <linux/swapops.h>
  54. #include <linux/elf.h>
  55. #ifndef CONFIG_NEED_MULTIPLE_NODES
  56. /* use the per-pgdat data instead for discontigmem - mbligh */
  57. unsigned long max_mapnr;
  58. struct page *mem_map;
  59. EXPORT_SYMBOL(max_mapnr);
  60. EXPORT_SYMBOL(mem_map);
  61. #endif
  62. unsigned long num_physpages;
  63. /*
  64. * A number of key systems in x86 including ioremap() rely on the assumption
  65. * that high_memory defines the upper bound on direct map memory, then end
  66. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  67. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  68. * and ZONE_HIGHMEM.
  69. */
  70. void * high_memory;
  71. EXPORT_SYMBOL(num_physpages);
  72. EXPORT_SYMBOL(high_memory);
  73. int randomize_va_space __read_mostly = 1;
  74. static int __init disable_randmaps(char *s)
  75. {
  76. randomize_va_space = 0;
  77. return 1;
  78. }
  79. __setup("norandmaps", disable_randmaps);
  80. /*
  81. * If a p?d_bad entry is found while walking page tables, report
  82. * the error, before resetting entry to p?d_none. Usually (but
  83. * very seldom) called out from the p?d_none_or_clear_bad macros.
  84. */
  85. void pgd_clear_bad(pgd_t *pgd)
  86. {
  87. pgd_ERROR(*pgd);
  88. pgd_clear(pgd);
  89. }
  90. void pud_clear_bad(pud_t *pud)
  91. {
  92. pud_ERROR(*pud);
  93. pud_clear(pud);
  94. }
  95. void pmd_clear_bad(pmd_t *pmd)
  96. {
  97. pmd_ERROR(*pmd);
  98. pmd_clear(pmd);
  99. }
  100. /*
  101. * Note: this doesn't free the actual pages themselves. That
  102. * has been handled earlier when unmapping all the memory regions.
  103. */
  104. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
  105. {
  106. struct page *page = pmd_page(*pmd);
  107. pmd_clear(pmd);
  108. pte_lock_deinit(page);
  109. pte_free_tlb(tlb, page);
  110. dec_zone_page_state(page, NR_PAGETABLE);
  111. tlb->mm->nr_ptes--;
  112. }
  113. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  114. unsigned long addr, unsigned long end,
  115. unsigned long floor, unsigned long ceiling)
  116. {
  117. pmd_t *pmd;
  118. unsigned long next;
  119. unsigned long start;
  120. start = addr;
  121. pmd = pmd_offset(pud, addr);
  122. do {
  123. next = pmd_addr_end(addr, end);
  124. if (pmd_none_or_clear_bad(pmd))
  125. continue;
  126. free_pte_range(tlb, pmd);
  127. } while (pmd++, addr = next, addr != end);
  128. start &= PUD_MASK;
  129. if (start < floor)
  130. return;
  131. if (ceiling) {
  132. ceiling &= PUD_MASK;
  133. if (!ceiling)
  134. return;
  135. }
  136. if (end - 1 > ceiling - 1)
  137. return;
  138. pmd = pmd_offset(pud, start);
  139. pud_clear(pud);
  140. pmd_free_tlb(tlb, pmd);
  141. }
  142. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  143. unsigned long addr, unsigned long end,
  144. unsigned long floor, unsigned long ceiling)
  145. {
  146. pud_t *pud;
  147. unsigned long next;
  148. unsigned long start;
  149. start = addr;
  150. pud = pud_offset(pgd, addr);
  151. do {
  152. next = pud_addr_end(addr, end);
  153. if (pud_none_or_clear_bad(pud))
  154. continue;
  155. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  156. } while (pud++, addr = next, addr != end);
  157. start &= PGDIR_MASK;
  158. if (start < floor)
  159. return;
  160. if (ceiling) {
  161. ceiling &= PGDIR_MASK;
  162. if (!ceiling)
  163. return;
  164. }
  165. if (end - 1 > ceiling - 1)
  166. return;
  167. pud = pud_offset(pgd, start);
  168. pgd_clear(pgd);
  169. pud_free_tlb(tlb, pud);
  170. }
  171. /*
  172. * This function frees user-level page tables of a process.
  173. *
  174. * Must be called with pagetable lock held.
  175. */
  176. void free_pgd_range(struct mmu_gather **tlb,
  177. unsigned long addr, unsigned long end,
  178. unsigned long floor, unsigned long ceiling)
  179. {
  180. pgd_t *pgd;
  181. unsigned long next;
  182. unsigned long start;
  183. /*
  184. * The next few lines have given us lots of grief...
  185. *
  186. * Why are we testing PMD* at this top level? Because often
  187. * there will be no work to do at all, and we'd prefer not to
  188. * go all the way down to the bottom just to discover that.
  189. *
  190. * Why all these "- 1"s? Because 0 represents both the bottom
  191. * of the address space and the top of it (using -1 for the
  192. * top wouldn't help much: the masks would do the wrong thing).
  193. * The rule is that addr 0 and floor 0 refer to the bottom of
  194. * the address space, but end 0 and ceiling 0 refer to the top
  195. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  196. * that end 0 case should be mythical).
  197. *
  198. * Wherever addr is brought up or ceiling brought down, we must
  199. * be careful to reject "the opposite 0" before it confuses the
  200. * subsequent tests. But what about where end is brought down
  201. * by PMD_SIZE below? no, end can't go down to 0 there.
  202. *
  203. * Whereas we round start (addr) and ceiling down, by different
  204. * masks at different levels, in order to test whether a table
  205. * now has no other vmas using it, so can be freed, we don't
  206. * bother to round floor or end up - the tests don't need that.
  207. */
  208. addr &= PMD_MASK;
  209. if (addr < floor) {
  210. addr += PMD_SIZE;
  211. if (!addr)
  212. return;
  213. }
  214. if (ceiling) {
  215. ceiling &= PMD_MASK;
  216. if (!ceiling)
  217. return;
  218. }
  219. if (end - 1 > ceiling - 1)
  220. end -= PMD_SIZE;
  221. if (addr > end - 1)
  222. return;
  223. start = addr;
  224. pgd = pgd_offset((*tlb)->mm, addr);
  225. do {
  226. next = pgd_addr_end(addr, end);
  227. if (pgd_none_or_clear_bad(pgd))
  228. continue;
  229. free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  230. } while (pgd++, addr = next, addr != end);
  231. }
  232. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
  233. unsigned long floor, unsigned long ceiling)
  234. {
  235. while (vma) {
  236. struct vm_area_struct *next = vma->vm_next;
  237. unsigned long addr = vma->vm_start;
  238. /*
  239. * Hide vma from rmap and vmtruncate before freeing pgtables
  240. */
  241. anon_vma_unlink(vma);
  242. unlink_file_vma(vma);
  243. if (is_vm_hugetlb_page(vma)) {
  244. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  245. floor, next? next->vm_start: ceiling);
  246. } else {
  247. /*
  248. * Optimization: gather nearby vmas into one call down
  249. */
  250. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  251. && !is_vm_hugetlb_page(next)) {
  252. vma = next;
  253. next = vma->vm_next;
  254. anon_vma_unlink(vma);
  255. unlink_file_vma(vma);
  256. }
  257. free_pgd_range(tlb, addr, vma->vm_end,
  258. floor, next? next->vm_start: ceiling);
  259. }
  260. vma = next;
  261. }
  262. }
  263. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  264. {
  265. struct page *new = pte_alloc_one(mm, address);
  266. if (!new)
  267. return -ENOMEM;
  268. pte_lock_init(new);
  269. spin_lock(&mm->page_table_lock);
  270. if (pmd_present(*pmd)) { /* Another has populated it */
  271. pte_lock_deinit(new);
  272. pte_free(new);
  273. } else {
  274. mm->nr_ptes++;
  275. inc_zone_page_state(new, NR_PAGETABLE);
  276. pmd_populate(mm, pmd, new);
  277. }
  278. spin_unlock(&mm->page_table_lock);
  279. return 0;
  280. }
  281. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  282. {
  283. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  284. if (!new)
  285. return -ENOMEM;
  286. spin_lock(&init_mm.page_table_lock);
  287. if (pmd_present(*pmd)) /* Another has populated it */
  288. pte_free_kernel(new);
  289. else
  290. pmd_populate_kernel(&init_mm, pmd, new);
  291. spin_unlock(&init_mm.page_table_lock);
  292. return 0;
  293. }
  294. static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
  295. {
  296. if (file_rss)
  297. add_mm_counter(mm, file_rss, file_rss);
  298. if (anon_rss)
  299. add_mm_counter(mm, anon_rss, anon_rss);
  300. }
  301. /*
  302. * This function is called to print an error when a bad pte
  303. * is found. For example, we might have a PFN-mapped pte in
  304. * a region that doesn't allow it.
  305. *
  306. * The calling function must still handle the error.
  307. */
  308. void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
  309. {
  310. printk(KERN_ERR "Bad pte = %08llx, process = %s, "
  311. "vm_flags = %lx, vaddr = %lx\n",
  312. (long long)pte_val(pte),
  313. (vma->vm_mm == current->mm ? current->comm : "???"),
  314. vma->vm_flags, vaddr);
  315. dump_stack();
  316. }
  317. static inline int is_cow_mapping(unsigned int flags)
  318. {
  319. return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  320. }
  321. /*
  322. * This function gets the "struct page" associated with a pte.
  323. *
  324. * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
  325. * will have each page table entry just pointing to a raw page frame
  326. * number, and as far as the VM layer is concerned, those do not have
  327. * pages associated with them - even if the PFN might point to memory
  328. * that otherwise is perfectly fine and has a "struct page".
  329. *
  330. * The way we recognize those mappings is through the rules set up
  331. * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
  332. * and the vm_pgoff will point to the first PFN mapped: thus every
  333. * page that is a raw mapping will always honor the rule
  334. *
  335. * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
  336. *
  337. * and if that isn't true, the page has been COW'ed (in which case it
  338. * _does_ have a "struct page" associated with it even if it is in a
  339. * VM_PFNMAP range).
  340. */
  341. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  342. {
  343. unsigned long pfn = pte_pfn(pte);
  344. if (unlikely(vma->vm_flags & VM_PFNMAP)) {
  345. unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
  346. if (pfn == vma->vm_pgoff + off)
  347. return NULL;
  348. if (!is_cow_mapping(vma->vm_flags))
  349. return NULL;
  350. }
  351. /*
  352. * Add some anal sanity checks for now. Eventually,
  353. * we should just do "return pfn_to_page(pfn)", but
  354. * in the meantime we check that we get a valid pfn,
  355. * and that the resulting page looks ok.
  356. */
  357. if (unlikely(!pfn_valid(pfn))) {
  358. print_bad_pte(vma, pte, addr);
  359. return NULL;
  360. }
  361. /*
  362. * NOTE! We still have PageReserved() pages in the page
  363. * tables.
  364. *
  365. * The PAGE_ZERO() pages and various VDSO mappings can
  366. * cause them to exist.
  367. */
  368. return pfn_to_page(pfn);
  369. }
  370. /*
  371. * copy one vm_area from one task to the other. Assumes the page tables
  372. * already present in the new task to be cleared in the whole range
  373. * covered by this vma.
  374. */
  375. static inline void
  376. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  377. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  378. unsigned long addr, int *rss)
  379. {
  380. unsigned long vm_flags = vma->vm_flags;
  381. pte_t pte = *src_pte;
  382. struct page *page;
  383. /* pte contains position in swap or file, so copy. */
  384. if (unlikely(!pte_present(pte))) {
  385. if (!pte_file(pte)) {
  386. swp_entry_t entry = pte_to_swp_entry(pte);
  387. swap_duplicate(entry);
  388. /* make sure dst_mm is on swapoff's mmlist. */
  389. if (unlikely(list_empty(&dst_mm->mmlist))) {
  390. spin_lock(&mmlist_lock);
  391. if (list_empty(&dst_mm->mmlist))
  392. list_add(&dst_mm->mmlist,
  393. &src_mm->mmlist);
  394. spin_unlock(&mmlist_lock);
  395. }
  396. if (is_write_migration_entry(entry) &&
  397. is_cow_mapping(vm_flags)) {
  398. /*
  399. * COW mappings require pages in both parent
  400. * and child to be set to read.
  401. */
  402. make_migration_entry_read(&entry);
  403. pte = swp_entry_to_pte(entry);
  404. set_pte_at(src_mm, addr, src_pte, pte);
  405. }
  406. }
  407. goto out_set_pte;
  408. }
  409. /*
  410. * If it's a COW mapping, write protect it both
  411. * in the parent and the child
  412. */
  413. if (is_cow_mapping(vm_flags)) {
  414. ptep_set_wrprotect(src_mm, addr, src_pte);
  415. pte = pte_wrprotect(pte);
  416. }
  417. /*
  418. * If it's a shared mapping, mark it clean in
  419. * the child
  420. */
  421. if (vm_flags & VM_SHARED)
  422. pte = pte_mkclean(pte);
  423. pte = pte_mkold(pte);
  424. page = vm_normal_page(vma, addr, pte);
  425. if (page) {
  426. get_page(page);
  427. page_dup_rmap(page, vma, addr);
  428. rss[!!PageAnon(page)]++;
  429. }
  430. out_set_pte:
  431. set_pte_at(dst_mm, addr, dst_pte, pte);
  432. }
  433. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  434. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  435. unsigned long addr, unsigned long end)
  436. {
  437. pte_t *src_pte, *dst_pte;
  438. spinlock_t *src_ptl, *dst_ptl;
  439. int progress = 0;
  440. int rss[2];
  441. again:
  442. rss[1] = rss[0] = 0;
  443. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  444. if (!dst_pte)
  445. return -ENOMEM;
  446. src_pte = pte_offset_map_nested(src_pmd, addr);
  447. src_ptl = pte_lockptr(src_mm, src_pmd);
  448. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  449. arch_enter_lazy_mmu_mode();
  450. do {
  451. /*
  452. * We are holding two locks at this point - either of them
  453. * could generate latencies in another task on another CPU.
  454. */
  455. if (progress >= 32) {
  456. progress = 0;
  457. if (need_resched() ||
  458. need_lockbreak(src_ptl) ||
  459. need_lockbreak(dst_ptl))
  460. break;
  461. }
  462. if (pte_none(*src_pte)) {
  463. progress++;
  464. continue;
  465. }
  466. copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
  467. progress += 8;
  468. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  469. arch_leave_lazy_mmu_mode();
  470. spin_unlock(src_ptl);
  471. pte_unmap_nested(src_pte - 1);
  472. add_mm_rss(dst_mm, rss[0], rss[1]);
  473. pte_unmap_unlock(dst_pte - 1, dst_ptl);
  474. cond_resched();
  475. if (addr != end)
  476. goto again;
  477. return 0;
  478. }
  479. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  480. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  481. unsigned long addr, unsigned long end)
  482. {
  483. pmd_t *src_pmd, *dst_pmd;
  484. unsigned long next;
  485. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  486. if (!dst_pmd)
  487. return -ENOMEM;
  488. src_pmd = pmd_offset(src_pud, addr);
  489. do {
  490. next = pmd_addr_end(addr, end);
  491. if (pmd_none_or_clear_bad(src_pmd))
  492. continue;
  493. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  494. vma, addr, next))
  495. return -ENOMEM;
  496. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  497. return 0;
  498. }
  499. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  500. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  501. unsigned long addr, unsigned long end)
  502. {
  503. pud_t *src_pud, *dst_pud;
  504. unsigned long next;
  505. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  506. if (!dst_pud)
  507. return -ENOMEM;
  508. src_pud = pud_offset(src_pgd, addr);
  509. do {
  510. next = pud_addr_end(addr, end);
  511. if (pud_none_or_clear_bad(src_pud))
  512. continue;
  513. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  514. vma, addr, next))
  515. return -ENOMEM;
  516. } while (dst_pud++, src_pud++, addr = next, addr != end);
  517. return 0;
  518. }
  519. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  520. struct vm_area_struct *vma)
  521. {
  522. pgd_t *src_pgd, *dst_pgd;
  523. unsigned long next;
  524. unsigned long addr = vma->vm_start;
  525. unsigned long end = vma->vm_end;
  526. /*
  527. * Don't copy ptes where a page fault will fill them correctly.
  528. * Fork becomes much lighter when there are big shared or private
  529. * readonly mappings. The tradeoff is that copy_page_range is more
  530. * efficient than faulting.
  531. */
  532. if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
  533. if (!vma->anon_vma)
  534. return 0;
  535. }
  536. if (is_vm_hugetlb_page(vma))
  537. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  538. dst_pgd = pgd_offset(dst_mm, addr);
  539. src_pgd = pgd_offset(src_mm, addr);
  540. do {
  541. next = pgd_addr_end(addr, end);
  542. if (pgd_none_or_clear_bad(src_pgd))
  543. continue;
  544. if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  545. vma, addr, next))
  546. return -ENOMEM;
  547. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  548. return 0;
  549. }
  550. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  551. struct vm_area_struct *vma, pmd_t *pmd,
  552. unsigned long addr, unsigned long end,
  553. long *zap_work, struct zap_details *details)
  554. {
  555. struct mm_struct *mm = tlb->mm;
  556. pte_t *pte;
  557. spinlock_t *ptl;
  558. int file_rss = 0;
  559. int anon_rss = 0;
  560. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  561. arch_enter_lazy_mmu_mode();
  562. do {
  563. pte_t ptent = *pte;
  564. if (pte_none(ptent)) {
  565. (*zap_work)--;
  566. continue;
  567. }
  568. (*zap_work) -= PAGE_SIZE;
  569. if (pte_present(ptent)) {
  570. struct page *page;
  571. page = vm_normal_page(vma, addr, ptent);
  572. if (unlikely(details) && page) {
  573. /*
  574. * unmap_shared_mapping_pages() wants to
  575. * invalidate cache without truncating:
  576. * unmap shared but keep private pages.
  577. */
  578. if (details->check_mapping &&
  579. details->check_mapping != page->mapping)
  580. continue;
  581. /*
  582. * Each page->index must be checked when
  583. * invalidating or truncating nonlinear.
  584. */
  585. if (details->nonlinear_vma &&
  586. (page->index < details->first_index ||
  587. page->index > details->last_index))
  588. continue;
  589. }
  590. ptent = ptep_get_and_clear_full(mm, addr, pte,
  591. tlb->fullmm);
  592. tlb_remove_tlb_entry(tlb, pte, addr);
  593. if (unlikely(!page))
  594. continue;
  595. if (unlikely(details) && details->nonlinear_vma
  596. && linear_page_index(details->nonlinear_vma,
  597. addr) != page->index)
  598. set_pte_at(mm, addr, pte,
  599. pgoff_to_pte(page->index));
  600. if (PageAnon(page))
  601. anon_rss--;
  602. else {
  603. if (pte_dirty(ptent))
  604. set_page_dirty(page);
  605. if (pte_young(ptent))
  606. SetPageReferenced(page);
  607. file_rss--;
  608. }
  609. page_remove_rmap(page, vma);
  610. tlb_remove_page(tlb, page);
  611. continue;
  612. }
  613. /*
  614. * If details->check_mapping, we leave swap entries;
  615. * if details->nonlinear_vma, we leave file entries.
  616. */
  617. if (unlikely(details))
  618. continue;
  619. if (!pte_file(ptent))
  620. free_swap_and_cache(pte_to_swp_entry(ptent));
  621. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  622. } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
  623. add_mm_rss(mm, file_rss, anon_rss);
  624. arch_leave_lazy_mmu_mode();
  625. pte_unmap_unlock(pte - 1, ptl);
  626. return addr;
  627. }
  628. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  629. struct vm_area_struct *vma, pud_t *pud,
  630. unsigned long addr, unsigned long end,
  631. long *zap_work, struct zap_details *details)
  632. {
  633. pmd_t *pmd;
  634. unsigned long next;
  635. pmd = pmd_offset(pud, addr);
  636. do {
  637. next = pmd_addr_end(addr, end);
  638. if (pmd_none_or_clear_bad(pmd)) {
  639. (*zap_work)--;
  640. continue;
  641. }
  642. next = zap_pte_range(tlb, vma, pmd, addr, next,
  643. zap_work, details);
  644. } while (pmd++, addr = next, (addr != end && *zap_work > 0));
  645. return addr;
  646. }
  647. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  648. struct vm_area_struct *vma, pgd_t *pgd,
  649. unsigned long addr, unsigned long end,
  650. long *zap_work, struct zap_details *details)
  651. {
  652. pud_t *pud;
  653. unsigned long next;
  654. pud = pud_offset(pgd, addr);
  655. do {
  656. next = pud_addr_end(addr, end);
  657. if (pud_none_or_clear_bad(pud)) {
  658. (*zap_work)--;
  659. continue;
  660. }
  661. next = zap_pmd_range(tlb, vma, pud, addr, next,
  662. zap_work, details);
  663. } while (pud++, addr = next, (addr != end && *zap_work > 0));
  664. return addr;
  665. }
  666. static unsigned long unmap_page_range(struct mmu_gather *tlb,
  667. struct vm_area_struct *vma,
  668. unsigned long addr, unsigned long end,
  669. long *zap_work, struct zap_details *details)
  670. {
  671. pgd_t *pgd;
  672. unsigned long next;
  673. if (details && !details->check_mapping && !details->nonlinear_vma)
  674. details = NULL;
  675. BUG_ON(addr >= end);
  676. tlb_start_vma(tlb, vma);
  677. pgd = pgd_offset(vma->vm_mm, addr);
  678. do {
  679. next = pgd_addr_end(addr, end);
  680. if (pgd_none_or_clear_bad(pgd)) {
  681. (*zap_work)--;
  682. continue;
  683. }
  684. next = zap_pud_range(tlb, vma, pgd, addr, next,
  685. zap_work, details);
  686. } while (pgd++, addr = next, (addr != end && *zap_work > 0));
  687. tlb_end_vma(tlb, vma);
  688. return addr;
  689. }
  690. #ifdef CONFIG_PREEMPT
  691. # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
  692. #else
  693. /* No preempt: go for improved straight-line efficiency */
  694. # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
  695. #endif
  696. /**
  697. * unmap_vmas - unmap a range of memory covered by a list of vma's
  698. * @tlbp: address of the caller's struct mmu_gather
  699. * @vma: the starting vma
  700. * @start_addr: virtual address at which to start unmapping
  701. * @end_addr: virtual address at which to end unmapping
  702. * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
  703. * @details: details of nonlinear truncation or shared cache invalidation
  704. *
  705. * Returns the end address of the unmapping (restart addr if interrupted).
  706. *
  707. * Unmap all pages in the vma list.
  708. *
  709. * We aim to not hold locks for too long (for scheduling latency reasons).
  710. * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
  711. * return the ending mmu_gather to the caller.
  712. *
  713. * Only addresses between `start' and `end' will be unmapped.
  714. *
  715. * The VMA list must be sorted in ascending virtual address order.
  716. *
  717. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  718. * range after unmap_vmas() returns. So the only responsibility here is to
  719. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  720. * drops the lock and schedules.
  721. */
  722. unsigned long unmap_vmas(struct mmu_gather **tlbp,
  723. struct vm_area_struct *vma, unsigned long start_addr,
  724. unsigned long end_addr, unsigned long *nr_accounted,
  725. struct zap_details *details)
  726. {
  727. long zap_work = ZAP_BLOCK_SIZE;
  728. unsigned long tlb_start = 0; /* For tlb_finish_mmu */
  729. int tlb_start_valid = 0;
  730. unsigned long start = start_addr;
  731. spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
  732. int fullmm = (*tlbp)->fullmm;
  733. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
  734. unsigned long end;
  735. start = max(vma->vm_start, start_addr);
  736. if (start >= vma->vm_end)
  737. continue;
  738. end = min(vma->vm_end, end_addr);
  739. if (end <= vma->vm_start)
  740. continue;
  741. if (vma->vm_flags & VM_ACCOUNT)
  742. *nr_accounted += (end - start) >> PAGE_SHIFT;
  743. while (start != end) {
  744. if (!tlb_start_valid) {
  745. tlb_start = start;
  746. tlb_start_valid = 1;
  747. }
  748. if (unlikely(is_vm_hugetlb_page(vma))) {
  749. unmap_hugepage_range(vma, start, end);
  750. zap_work -= (end - start) /
  751. (HPAGE_SIZE / PAGE_SIZE);
  752. start = end;
  753. } else
  754. start = unmap_page_range(*tlbp, vma,
  755. start, end, &zap_work, details);
  756. if (zap_work > 0) {
  757. BUG_ON(start != end);
  758. break;
  759. }
  760. tlb_finish_mmu(*tlbp, tlb_start, start);
  761. if (need_resched() ||
  762. (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
  763. if (i_mmap_lock) {
  764. *tlbp = NULL;
  765. goto out;
  766. }
  767. cond_resched();
  768. }
  769. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  770. tlb_start_valid = 0;
  771. zap_work = ZAP_BLOCK_SIZE;
  772. }
  773. }
  774. out:
  775. return start; /* which is now the end (or restart) address */
  776. }
  777. /**
  778. * zap_page_range - remove user pages in a given range
  779. * @vma: vm_area_struct holding the applicable pages
  780. * @address: starting address of pages to zap
  781. * @size: number of bytes to zap
  782. * @details: details of nonlinear truncation or shared cache invalidation
  783. */
  784. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  785. unsigned long size, struct zap_details *details)
  786. {
  787. struct mm_struct *mm = vma->vm_mm;
  788. struct mmu_gather *tlb;
  789. unsigned long end = address + size;
  790. unsigned long nr_accounted = 0;
  791. lru_add_drain();
  792. tlb = tlb_gather_mmu(mm, 0);
  793. update_hiwater_rss(mm);
  794. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  795. if (tlb)
  796. tlb_finish_mmu(tlb, address, end);
  797. return end;
  798. }
  799. /*
  800. * Do a quick page-table lookup for a single page.
  801. */
  802. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  803. unsigned int flags)
  804. {
  805. pgd_t *pgd;
  806. pud_t *pud;
  807. pmd_t *pmd;
  808. pte_t *ptep, pte;
  809. spinlock_t *ptl;
  810. struct page *page;
  811. struct mm_struct *mm = vma->vm_mm;
  812. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  813. if (!IS_ERR(page)) {
  814. BUG_ON(flags & FOLL_GET);
  815. goto out;
  816. }
  817. page = NULL;
  818. pgd = pgd_offset(mm, address);
  819. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  820. goto no_page_table;
  821. pud = pud_offset(pgd, address);
  822. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  823. goto no_page_table;
  824. pmd = pmd_offset(pud, address);
  825. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  826. goto no_page_table;
  827. if (pmd_huge(*pmd)) {
  828. BUG_ON(flags & FOLL_GET);
  829. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  830. goto out;
  831. }
  832. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  833. if (!ptep)
  834. goto out;
  835. pte = *ptep;
  836. if (!pte_present(pte))
  837. goto unlock;
  838. if ((flags & FOLL_WRITE) && !pte_write(pte))
  839. goto unlock;
  840. page = vm_normal_page(vma, address, pte);
  841. if (unlikely(!page))
  842. goto unlock;
  843. if (flags & FOLL_GET)
  844. get_page(page);
  845. if (flags & FOLL_TOUCH) {
  846. if ((flags & FOLL_WRITE) &&
  847. !pte_dirty(pte) && !PageDirty(page))
  848. set_page_dirty(page);
  849. mark_page_accessed(page);
  850. }
  851. unlock:
  852. pte_unmap_unlock(ptep, ptl);
  853. out:
  854. return page;
  855. no_page_table:
  856. /*
  857. * When core dumping an enormous anonymous area that nobody
  858. * has touched so far, we don't want to allocate page tables.
  859. */
  860. if (flags & FOLL_ANON) {
  861. page = ZERO_PAGE(0);
  862. if (flags & FOLL_GET)
  863. get_page(page);
  864. BUG_ON(flags & FOLL_WRITE);
  865. }
  866. return page;
  867. }
  868. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  869. unsigned long start, int len, int write, int force,
  870. struct page **pages, struct vm_area_struct **vmas)
  871. {
  872. int i;
  873. unsigned int vm_flags;
  874. /*
  875. * Require read or write permissions.
  876. * If 'force' is set, we only require the "MAY" flags.
  877. */
  878. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  879. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  880. i = 0;
  881. do {
  882. struct vm_area_struct *vma;
  883. unsigned int foll_flags;
  884. vma = find_extend_vma(mm, start);
  885. if (!vma && in_gate_area(tsk, start)) {
  886. unsigned long pg = start & PAGE_MASK;
  887. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  888. pgd_t *pgd;
  889. pud_t *pud;
  890. pmd_t *pmd;
  891. pte_t *pte;
  892. if (write) /* user gate pages are read-only */
  893. return i ? : -EFAULT;
  894. if (pg > TASK_SIZE)
  895. pgd = pgd_offset_k(pg);
  896. else
  897. pgd = pgd_offset_gate(mm, pg);
  898. BUG_ON(pgd_none(*pgd));
  899. pud = pud_offset(pgd, pg);
  900. BUG_ON(pud_none(*pud));
  901. pmd = pmd_offset(pud, pg);
  902. if (pmd_none(*pmd))
  903. return i ? : -EFAULT;
  904. pte = pte_offset_map(pmd, pg);
  905. if (pte_none(*pte)) {
  906. pte_unmap(pte);
  907. return i ? : -EFAULT;
  908. }
  909. if (pages) {
  910. struct page *page = vm_normal_page(gate_vma, start, *pte);
  911. pages[i] = page;
  912. if (page)
  913. get_page(page);
  914. }
  915. pte_unmap(pte);
  916. if (vmas)
  917. vmas[i] = gate_vma;
  918. i++;
  919. start += PAGE_SIZE;
  920. len--;
  921. continue;
  922. }
  923. if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
  924. || !(vm_flags & vma->vm_flags))
  925. return i ? : -EFAULT;
  926. if (is_vm_hugetlb_page(vma)) {
  927. i = follow_hugetlb_page(mm, vma, pages, vmas,
  928. &start, &len, i, write);
  929. continue;
  930. }
  931. foll_flags = FOLL_TOUCH;
  932. if (pages)
  933. foll_flags |= FOLL_GET;
  934. if (!write && !(vma->vm_flags & VM_LOCKED) &&
  935. (!vma->vm_ops || (!vma->vm_ops->nopage &&
  936. !vma->vm_ops->fault)))
  937. foll_flags |= FOLL_ANON;
  938. do {
  939. struct page *page;
  940. /*
  941. * If tsk is ooming, cut off its access to large memory
  942. * allocations. It has a pending SIGKILL, but it can't
  943. * be processed until returning to user space.
  944. */
  945. if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE)))
  946. return -ENOMEM;
  947. if (write)
  948. foll_flags |= FOLL_WRITE;
  949. cond_resched();
  950. while (!(page = follow_page(vma, start, foll_flags))) {
  951. int ret;
  952. ret = handle_mm_fault(mm, vma, start,
  953. foll_flags & FOLL_WRITE);
  954. if (ret & VM_FAULT_ERROR) {
  955. if (ret & VM_FAULT_OOM)
  956. return i ? i : -ENOMEM;
  957. else if (ret & VM_FAULT_SIGBUS)
  958. return i ? i : -EFAULT;
  959. BUG();
  960. }
  961. if (ret & VM_FAULT_MAJOR)
  962. tsk->maj_flt++;
  963. else
  964. tsk->min_flt++;
  965. /*
  966. * The VM_FAULT_WRITE bit tells us that
  967. * do_wp_page has broken COW when necessary,
  968. * even if maybe_mkwrite decided not to set
  969. * pte_write. We can thus safely do subsequent
  970. * page lookups as if they were reads.
  971. */
  972. if (ret & VM_FAULT_WRITE)
  973. foll_flags &= ~FOLL_WRITE;
  974. cond_resched();
  975. }
  976. if (pages) {
  977. pages[i] = page;
  978. flush_anon_page(vma, page, start);
  979. flush_dcache_page(page);
  980. }
  981. if (vmas)
  982. vmas[i] = vma;
  983. i++;
  984. start += PAGE_SIZE;
  985. len--;
  986. } while (len && start < vma->vm_end);
  987. } while (len);
  988. return i;
  989. }
  990. EXPORT_SYMBOL(get_user_pages);
  991. pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
  992. {
  993. pgd_t * pgd = pgd_offset(mm, addr);
  994. pud_t * pud = pud_alloc(mm, pgd, addr);
  995. if (pud) {
  996. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  997. if (pmd)
  998. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  999. }
  1000. return NULL;
  1001. }
  1002. /*
  1003. * This is the old fallback for page remapping.
  1004. *
  1005. * For historical reasons, it only allows reserved pages. Only
  1006. * old drivers should use this, and they needed to mark their
  1007. * pages reserved for the old functions anyway.
  1008. */
  1009. static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
  1010. {
  1011. int retval;
  1012. pte_t *pte;
  1013. spinlock_t *ptl;
  1014. retval = -EINVAL;
  1015. if (PageAnon(page))
  1016. goto out;
  1017. retval = -ENOMEM;
  1018. flush_dcache_page(page);
  1019. pte = get_locked_pte(mm, addr, &ptl);
  1020. if (!pte)
  1021. goto out;
  1022. retval = -EBUSY;
  1023. if (!pte_none(*pte))
  1024. goto out_unlock;
  1025. /* Ok, finally just insert the thing.. */
  1026. get_page(page);
  1027. inc_mm_counter(mm, file_rss);
  1028. page_add_file_rmap(page);
  1029. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1030. retval = 0;
  1031. out_unlock:
  1032. pte_unmap_unlock(pte, ptl);
  1033. out:
  1034. return retval;
  1035. }
  1036. /**
  1037. * vm_insert_page - insert single page into user vma
  1038. * @vma: user vma to map to
  1039. * @addr: target user address of this page
  1040. * @page: source kernel page
  1041. *
  1042. * This allows drivers to insert individual pages they've allocated
  1043. * into a user vma.
  1044. *
  1045. * The page has to be a nice clean _individual_ kernel allocation.
  1046. * If you allocate a compound page, you need to have marked it as
  1047. * such (__GFP_COMP), or manually just split the page up yourself
  1048. * (see split_page()).
  1049. *
  1050. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1051. * took an arbitrary page protection parameter. This doesn't allow
  1052. * that. Your vma protection will have to be set up correctly, which
  1053. * means that if you want a shared writable mapping, you'd better
  1054. * ask for a shared writable mapping!
  1055. *
  1056. * The page does not need to be reserved.
  1057. */
  1058. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
  1059. {
  1060. if (addr < vma->vm_start || addr >= vma->vm_end)
  1061. return -EFAULT;
  1062. if (!page_count(page))
  1063. return -EINVAL;
  1064. vma->vm_flags |= VM_INSERTPAGE;
  1065. return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
  1066. }
  1067. EXPORT_SYMBOL(vm_insert_page);
  1068. /**
  1069. * vm_insert_pfn - insert single pfn into user vma
  1070. * @vma: user vma to map to
  1071. * @addr: target user address of this page
  1072. * @pfn: source kernel pfn
  1073. *
  1074. * Similar to vm_inert_page, this allows drivers to insert individual pages
  1075. * they've allocated into a user vma. Same comments apply.
  1076. *
  1077. * This function should only be called from a vm_ops->fault handler, and
  1078. * in that case the handler should return NULL.
  1079. */
  1080. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1081. unsigned long pfn)
  1082. {
  1083. struct mm_struct *mm = vma->vm_mm;
  1084. int retval;
  1085. pte_t *pte, entry;
  1086. spinlock_t *ptl;
  1087. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  1088. BUG_ON(is_cow_mapping(vma->vm_flags));
  1089. retval = -ENOMEM;
  1090. pte = get_locked_pte(mm, addr, &ptl);
  1091. if (!pte)
  1092. goto out;
  1093. retval = -EBUSY;
  1094. if (!pte_none(*pte))
  1095. goto out_unlock;
  1096. /* Ok, finally just insert the thing.. */
  1097. entry = pfn_pte(pfn, vma->vm_page_prot);
  1098. set_pte_at(mm, addr, pte, entry);
  1099. update_mmu_cache(vma, addr, entry);
  1100. retval = 0;
  1101. out_unlock:
  1102. pte_unmap_unlock(pte, ptl);
  1103. out:
  1104. return retval;
  1105. }
  1106. EXPORT_SYMBOL(vm_insert_pfn);
  1107. /*
  1108. * maps a range of physical memory into the requested pages. the old
  1109. * mappings are removed. any references to nonexistent pages results
  1110. * in null mappings (currently treated as "copy-on-access")
  1111. */
  1112. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1113. unsigned long addr, unsigned long end,
  1114. unsigned long pfn, pgprot_t prot)
  1115. {
  1116. pte_t *pte;
  1117. spinlock_t *ptl;
  1118. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1119. if (!pte)
  1120. return -ENOMEM;
  1121. arch_enter_lazy_mmu_mode();
  1122. do {
  1123. BUG_ON(!pte_none(*pte));
  1124. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1125. pfn++;
  1126. } while (pte++, addr += PAGE_SIZE, addr != end);
  1127. arch_leave_lazy_mmu_mode();
  1128. pte_unmap_unlock(pte - 1, ptl);
  1129. return 0;
  1130. }
  1131. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1132. unsigned long addr, unsigned long end,
  1133. unsigned long pfn, pgprot_t prot)
  1134. {
  1135. pmd_t *pmd;
  1136. unsigned long next;
  1137. pfn -= addr >> PAGE_SHIFT;
  1138. pmd = pmd_alloc(mm, pud, addr);
  1139. if (!pmd)
  1140. return -ENOMEM;
  1141. do {
  1142. next = pmd_addr_end(addr, end);
  1143. if (remap_pte_range(mm, pmd, addr, next,
  1144. pfn + (addr >> PAGE_SHIFT), prot))
  1145. return -ENOMEM;
  1146. } while (pmd++, addr = next, addr != end);
  1147. return 0;
  1148. }
  1149. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1150. unsigned long addr, unsigned long end,
  1151. unsigned long pfn, pgprot_t prot)
  1152. {
  1153. pud_t *pud;
  1154. unsigned long next;
  1155. pfn -= addr >> PAGE_SHIFT;
  1156. pud = pud_alloc(mm, pgd, addr);
  1157. if (!pud)
  1158. return -ENOMEM;
  1159. do {
  1160. next = pud_addr_end(addr, end);
  1161. if (remap_pmd_range(mm, pud, addr, next,
  1162. pfn + (addr >> PAGE_SHIFT), prot))
  1163. return -ENOMEM;
  1164. } while (pud++, addr = next, addr != end);
  1165. return 0;
  1166. }
  1167. /**
  1168. * remap_pfn_range - remap kernel memory to userspace
  1169. * @vma: user vma to map to
  1170. * @addr: target user address to start at
  1171. * @pfn: physical address of kernel memory
  1172. * @size: size of map area
  1173. * @prot: page protection flags for this mapping
  1174. *
  1175. * Note: this is only safe if the mm semaphore is held when called.
  1176. */
  1177. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1178. unsigned long pfn, unsigned long size, pgprot_t prot)
  1179. {
  1180. pgd_t *pgd;
  1181. unsigned long next;
  1182. unsigned long end = addr + PAGE_ALIGN(size);
  1183. struct mm_struct *mm = vma->vm_mm;
  1184. int err;
  1185. /*
  1186. * Physically remapped pages are special. Tell the
  1187. * rest of the world about it:
  1188. * VM_IO tells people not to look at these pages
  1189. * (accesses can have side effects).
  1190. * VM_RESERVED is specified all over the place, because
  1191. * in 2.4 it kept swapout's vma scan off this vma; but
  1192. * in 2.6 the LRU scan won't even find its pages, so this
  1193. * flag means no more than count its pages in reserved_vm,
  1194. * and omit it from core dump, even when VM_IO turned off.
  1195. * VM_PFNMAP tells the core MM that the base pages are just
  1196. * raw PFN mappings, and do not have a "struct page" associated
  1197. * with them.
  1198. *
  1199. * There's a horrible special case to handle copy-on-write
  1200. * behaviour that some programs depend on. We mark the "original"
  1201. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1202. */
  1203. if (is_cow_mapping(vma->vm_flags)) {
  1204. if (addr != vma->vm_start || end != vma->vm_end)
  1205. return -EINVAL;
  1206. vma->vm_pgoff = pfn;
  1207. }
  1208. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  1209. BUG_ON(addr >= end);
  1210. pfn -= addr >> PAGE_SHIFT;
  1211. pgd = pgd_offset(mm, addr);
  1212. flush_cache_range(vma, addr, end);
  1213. do {
  1214. next = pgd_addr_end(addr, end);
  1215. err = remap_pud_range(mm, pgd, addr, next,
  1216. pfn + (addr >> PAGE_SHIFT), prot);
  1217. if (err)
  1218. break;
  1219. } while (pgd++, addr = next, addr != end);
  1220. return err;
  1221. }
  1222. EXPORT_SYMBOL(remap_pfn_range);
  1223. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1224. unsigned long addr, unsigned long end,
  1225. pte_fn_t fn, void *data)
  1226. {
  1227. pte_t *pte;
  1228. int err;
  1229. struct page *pmd_page;
  1230. spinlock_t *uninitialized_var(ptl);
  1231. pte = (mm == &init_mm) ?
  1232. pte_alloc_kernel(pmd, addr) :
  1233. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1234. if (!pte)
  1235. return -ENOMEM;
  1236. BUG_ON(pmd_huge(*pmd));
  1237. pmd_page = pmd_page(*pmd);
  1238. do {
  1239. err = fn(pte, pmd_page, addr, data);
  1240. if (err)
  1241. break;
  1242. } while (pte++, addr += PAGE_SIZE, addr != end);
  1243. if (mm != &init_mm)
  1244. pte_unmap_unlock(pte-1, ptl);
  1245. return err;
  1246. }
  1247. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1248. unsigned long addr, unsigned long end,
  1249. pte_fn_t fn, void *data)
  1250. {
  1251. pmd_t *pmd;
  1252. unsigned long next;
  1253. int err;
  1254. pmd = pmd_alloc(mm, pud, addr);
  1255. if (!pmd)
  1256. return -ENOMEM;
  1257. do {
  1258. next = pmd_addr_end(addr, end);
  1259. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1260. if (err)
  1261. break;
  1262. } while (pmd++, addr = next, addr != end);
  1263. return err;
  1264. }
  1265. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1266. unsigned long addr, unsigned long end,
  1267. pte_fn_t fn, void *data)
  1268. {
  1269. pud_t *pud;
  1270. unsigned long next;
  1271. int err;
  1272. pud = pud_alloc(mm, pgd, addr);
  1273. if (!pud)
  1274. return -ENOMEM;
  1275. do {
  1276. next = pud_addr_end(addr, end);
  1277. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1278. if (err)
  1279. break;
  1280. } while (pud++, addr = next, addr != end);
  1281. return err;
  1282. }
  1283. /*
  1284. * Scan a region of virtual memory, filling in page tables as necessary
  1285. * and calling a provided function on each leaf page table.
  1286. */
  1287. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1288. unsigned long size, pte_fn_t fn, void *data)
  1289. {
  1290. pgd_t *pgd;
  1291. unsigned long next;
  1292. unsigned long end = addr + size;
  1293. int err;
  1294. BUG_ON(addr >= end);
  1295. pgd = pgd_offset(mm, addr);
  1296. do {
  1297. next = pgd_addr_end(addr, end);
  1298. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1299. if (err)
  1300. break;
  1301. } while (pgd++, addr = next, addr != end);
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1305. /*
  1306. * handle_pte_fault chooses page fault handler according to an entry
  1307. * which was read non-atomically. Before making any commitment, on
  1308. * those architectures or configurations (e.g. i386 with PAE) which
  1309. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1310. * must check under lock before unmapping the pte and proceeding
  1311. * (but do_wp_page is only called after already making such a check;
  1312. * and do_anonymous_page and do_no_page can safely check later on).
  1313. */
  1314. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1315. pte_t *page_table, pte_t orig_pte)
  1316. {
  1317. int same = 1;
  1318. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1319. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1320. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1321. spin_lock(ptl);
  1322. same = pte_same(*page_table, orig_pte);
  1323. spin_unlock(ptl);
  1324. }
  1325. #endif
  1326. pte_unmap(page_table);
  1327. return same;
  1328. }
  1329. /*
  1330. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1331. * servicing faults for write access. In the normal case, do always want
  1332. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1333. * that do not have writing enabled, when used by access_process_vm.
  1334. */
  1335. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1336. {
  1337. if (likely(vma->vm_flags & VM_WRITE))
  1338. pte = pte_mkwrite(pte);
  1339. return pte;
  1340. }
  1341. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1342. {
  1343. /*
  1344. * If the source page was a PFN mapping, we don't have
  1345. * a "struct page" for it. We do a best-effort copy by
  1346. * just copying from the original user address. If that
  1347. * fails, we just zero-fill it. Live with it.
  1348. */
  1349. if (unlikely(!src)) {
  1350. void *kaddr = kmap_atomic(dst, KM_USER0);
  1351. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1352. /*
  1353. * This really shouldn't fail, because the page is there
  1354. * in the page tables. But it might just be unreadable,
  1355. * in which case we just give up and fill the result with
  1356. * zeroes.
  1357. */
  1358. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1359. memset(kaddr, 0, PAGE_SIZE);
  1360. kunmap_atomic(kaddr, KM_USER0);
  1361. flush_dcache_page(dst);
  1362. return;
  1363. }
  1364. copy_user_highpage(dst, src, va, vma);
  1365. }
  1366. /*
  1367. * This routine handles present pages, when users try to write
  1368. * to a shared page. It is done by copying the page to a new address
  1369. * and decrementing the shared-page counter for the old page.
  1370. *
  1371. * Note that this routine assumes that the protection checks have been
  1372. * done by the caller (the low-level page fault routine in most cases).
  1373. * Thus we can safely just mark it writable once we've done any necessary
  1374. * COW.
  1375. *
  1376. * We also mark the page dirty at this point even though the page will
  1377. * change only once the write actually happens. This avoids a few races,
  1378. * and potentially makes it more efficient.
  1379. *
  1380. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1381. * but allow concurrent faults), with pte both mapped and locked.
  1382. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1383. */
  1384. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1385. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1386. spinlock_t *ptl, pte_t orig_pte)
  1387. {
  1388. struct page *old_page, *new_page;
  1389. pte_t entry;
  1390. int reuse = 0, ret = 0;
  1391. int page_mkwrite = 0;
  1392. struct page *dirty_page = NULL;
  1393. old_page = vm_normal_page(vma, address, orig_pte);
  1394. if (!old_page)
  1395. goto gotten;
  1396. /*
  1397. * Take out anonymous pages first, anonymous shared vmas are
  1398. * not dirty accountable.
  1399. */
  1400. if (PageAnon(old_page)) {
  1401. if (!TestSetPageLocked(old_page)) {
  1402. reuse = can_share_swap_page(old_page);
  1403. unlock_page(old_page);
  1404. }
  1405. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  1406. (VM_WRITE|VM_SHARED))) {
  1407. /*
  1408. * Only catch write-faults on shared writable pages,
  1409. * read-only shared pages can get COWed by
  1410. * get_user_pages(.write=1, .force=1).
  1411. */
  1412. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  1413. /*
  1414. * Notify the address space that the page is about to
  1415. * become writable so that it can prohibit this or wait
  1416. * for the page to get into an appropriate state.
  1417. *
  1418. * We do this without the lock held, so that it can
  1419. * sleep if it needs to.
  1420. */
  1421. page_cache_get(old_page);
  1422. pte_unmap_unlock(page_table, ptl);
  1423. if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
  1424. goto unwritable_page;
  1425. /*
  1426. * Since we dropped the lock we need to revalidate
  1427. * the PTE as someone else may have changed it. If
  1428. * they did, we just return, as we can count on the
  1429. * MMU to tell us if they didn't also make it writable.
  1430. */
  1431. page_table = pte_offset_map_lock(mm, pmd, address,
  1432. &ptl);
  1433. page_cache_release(old_page);
  1434. if (!pte_same(*page_table, orig_pte))
  1435. goto unlock;
  1436. page_mkwrite = 1;
  1437. }
  1438. dirty_page = old_page;
  1439. get_page(dirty_page);
  1440. reuse = 1;
  1441. }
  1442. if (reuse) {
  1443. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1444. entry = pte_mkyoung(orig_pte);
  1445. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1446. if (ptep_set_access_flags(vma, address, page_table, entry,1))
  1447. update_mmu_cache(vma, address, entry);
  1448. ret |= VM_FAULT_WRITE;
  1449. goto unlock;
  1450. }
  1451. /*
  1452. * Ok, we need to copy. Oh, well..
  1453. */
  1454. page_cache_get(old_page);
  1455. gotten:
  1456. pte_unmap_unlock(page_table, ptl);
  1457. if (unlikely(anon_vma_prepare(vma)))
  1458. goto oom;
  1459. VM_BUG_ON(old_page == ZERO_PAGE(0));
  1460. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1461. if (!new_page)
  1462. goto oom;
  1463. cow_user_page(new_page, old_page, address, vma);
  1464. /*
  1465. * Re-check the pte - we dropped the lock
  1466. */
  1467. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1468. if (likely(pte_same(*page_table, orig_pte))) {
  1469. if (old_page) {
  1470. page_remove_rmap(old_page, vma);
  1471. if (!PageAnon(old_page)) {
  1472. dec_mm_counter(mm, file_rss);
  1473. inc_mm_counter(mm, anon_rss);
  1474. }
  1475. } else
  1476. inc_mm_counter(mm, anon_rss);
  1477. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1478. entry = mk_pte(new_page, vma->vm_page_prot);
  1479. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1480. /*
  1481. * Clear the pte entry and flush it first, before updating the
  1482. * pte with the new entry. This will avoid a race condition
  1483. * seen in the presence of one thread doing SMC and another
  1484. * thread doing COW.
  1485. */
  1486. ptep_clear_flush(vma, address, page_table);
  1487. set_pte_at(mm, address, page_table, entry);
  1488. update_mmu_cache(vma, address, entry);
  1489. lru_cache_add_active(new_page);
  1490. page_add_new_anon_rmap(new_page, vma, address);
  1491. /* Free the old page.. */
  1492. new_page = old_page;
  1493. ret |= VM_FAULT_WRITE;
  1494. }
  1495. if (new_page)
  1496. page_cache_release(new_page);
  1497. if (old_page)
  1498. page_cache_release(old_page);
  1499. unlock:
  1500. pte_unmap_unlock(page_table, ptl);
  1501. if (dirty_page) {
  1502. /*
  1503. * Yes, Virginia, this is actually required to prevent a race
  1504. * with clear_page_dirty_for_io() from clearing the page dirty
  1505. * bit after it clear all dirty ptes, but before a racing
  1506. * do_wp_page installs a dirty pte.
  1507. *
  1508. * do_no_page is protected similarly.
  1509. */
  1510. wait_on_page_locked(dirty_page);
  1511. set_page_dirty_balance(dirty_page, page_mkwrite);
  1512. put_page(dirty_page);
  1513. }
  1514. return ret;
  1515. oom:
  1516. if (old_page)
  1517. page_cache_release(old_page);
  1518. return VM_FAULT_OOM;
  1519. unwritable_page:
  1520. page_cache_release(old_page);
  1521. return VM_FAULT_SIGBUS;
  1522. }
  1523. /*
  1524. * Helper functions for unmap_mapping_range().
  1525. *
  1526. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1527. *
  1528. * We have to restart searching the prio_tree whenever we drop the lock,
  1529. * since the iterator is only valid while the lock is held, and anyway
  1530. * a later vma might be split and reinserted earlier while lock dropped.
  1531. *
  1532. * The list of nonlinear vmas could be handled more efficiently, using
  1533. * a placeholder, but handle it in the same way until a need is shown.
  1534. * It is important to search the prio_tree before nonlinear list: a vma
  1535. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1536. * while the lock is dropped; but never shifted from list to prio_tree.
  1537. *
  1538. * In order to make forward progress despite restarting the search,
  1539. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1540. * quickly skip it next time around. Since the prio_tree search only
  1541. * shows us those vmas affected by unmapping the range in question, we
  1542. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1543. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1544. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1545. * i_mmap_lock.
  1546. *
  1547. * In order to make forward progress despite repeatedly restarting some
  1548. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1549. * and restart from that address when we reach that vma again. It might
  1550. * have been split or merged, shrunk or extended, but never shifted: so
  1551. * restart_addr remains valid so long as it remains in the vma's range.
  1552. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1553. * values so we can save vma's restart_addr in its truncate_count field.
  1554. */
  1555. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1556. static void reset_vma_truncate_counts(struct address_space *mapping)
  1557. {
  1558. struct vm_area_struct *vma;
  1559. struct prio_tree_iter iter;
  1560. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1561. vma->vm_truncate_count = 0;
  1562. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1563. vma->vm_truncate_count = 0;
  1564. }
  1565. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1566. unsigned long start_addr, unsigned long end_addr,
  1567. struct zap_details *details)
  1568. {
  1569. unsigned long restart_addr;
  1570. int need_break;
  1571. /*
  1572. * files that support invalidating or truncating portions of the
  1573. * file from under mmaped areas must have their ->fault function
  1574. * return a locked page (and set VM_FAULT_LOCKED in the return).
  1575. * This provides synchronisation against concurrent unmapping here.
  1576. */
  1577. again:
  1578. restart_addr = vma->vm_truncate_count;
  1579. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1580. start_addr = restart_addr;
  1581. if (start_addr >= end_addr) {
  1582. /* Top of vma has been split off since last time */
  1583. vma->vm_truncate_count = details->truncate_count;
  1584. return 0;
  1585. }
  1586. }
  1587. restart_addr = zap_page_range(vma, start_addr,
  1588. end_addr - start_addr, details);
  1589. need_break = need_resched() ||
  1590. need_lockbreak(details->i_mmap_lock);
  1591. if (restart_addr >= end_addr) {
  1592. /* We have now completed this vma: mark it so */
  1593. vma->vm_truncate_count = details->truncate_count;
  1594. if (!need_break)
  1595. return 0;
  1596. } else {
  1597. /* Note restart_addr in vma's truncate_count field */
  1598. vma->vm_truncate_count = restart_addr;
  1599. if (!need_break)
  1600. goto again;
  1601. }
  1602. spin_unlock(details->i_mmap_lock);
  1603. cond_resched();
  1604. spin_lock(details->i_mmap_lock);
  1605. return -EINTR;
  1606. }
  1607. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1608. struct zap_details *details)
  1609. {
  1610. struct vm_area_struct *vma;
  1611. struct prio_tree_iter iter;
  1612. pgoff_t vba, vea, zba, zea;
  1613. restart:
  1614. vma_prio_tree_foreach(vma, &iter, root,
  1615. details->first_index, details->last_index) {
  1616. /* Skip quickly over those we have already dealt with */
  1617. if (vma->vm_truncate_count == details->truncate_count)
  1618. continue;
  1619. vba = vma->vm_pgoff;
  1620. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1621. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1622. zba = details->first_index;
  1623. if (zba < vba)
  1624. zba = vba;
  1625. zea = details->last_index;
  1626. if (zea > vea)
  1627. zea = vea;
  1628. if (unmap_mapping_range_vma(vma,
  1629. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1630. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1631. details) < 0)
  1632. goto restart;
  1633. }
  1634. }
  1635. static inline void unmap_mapping_range_list(struct list_head *head,
  1636. struct zap_details *details)
  1637. {
  1638. struct vm_area_struct *vma;
  1639. /*
  1640. * In nonlinear VMAs there is no correspondence between virtual address
  1641. * offset and file offset. So we must perform an exhaustive search
  1642. * across *all* the pages in each nonlinear VMA, not just the pages
  1643. * whose virtual address lies outside the file truncation point.
  1644. */
  1645. restart:
  1646. list_for_each_entry(vma, head, shared.vm_set.list) {
  1647. /* Skip quickly over those we have already dealt with */
  1648. if (vma->vm_truncate_count == details->truncate_count)
  1649. continue;
  1650. details->nonlinear_vma = vma;
  1651. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1652. vma->vm_end, details) < 0)
  1653. goto restart;
  1654. }
  1655. }
  1656. /**
  1657. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  1658. * @mapping: the address space containing mmaps to be unmapped.
  1659. * @holebegin: byte in first page to unmap, relative to the start of
  1660. * the underlying file. This will be rounded down to a PAGE_SIZE
  1661. * boundary. Note that this is different from vmtruncate(), which
  1662. * must keep the partial page. In contrast, we must get rid of
  1663. * partial pages.
  1664. * @holelen: size of prospective hole in bytes. This will be rounded
  1665. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1666. * end of the file.
  1667. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1668. * but 0 when invalidating pagecache, don't throw away private data.
  1669. */
  1670. void unmap_mapping_range(struct address_space *mapping,
  1671. loff_t const holebegin, loff_t const holelen, int even_cows)
  1672. {
  1673. struct zap_details details;
  1674. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1675. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1676. /* Check for overflow. */
  1677. if (sizeof(holelen) > sizeof(hlen)) {
  1678. long long holeend =
  1679. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1680. if (holeend & ~(long long)ULONG_MAX)
  1681. hlen = ULONG_MAX - hba + 1;
  1682. }
  1683. details.check_mapping = even_cows? NULL: mapping;
  1684. details.nonlinear_vma = NULL;
  1685. details.first_index = hba;
  1686. details.last_index = hba + hlen - 1;
  1687. if (details.last_index < details.first_index)
  1688. details.last_index = ULONG_MAX;
  1689. details.i_mmap_lock = &mapping->i_mmap_lock;
  1690. spin_lock(&mapping->i_mmap_lock);
  1691. /* Protect against endless unmapping loops */
  1692. mapping->truncate_count++;
  1693. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1694. if (mapping->truncate_count == 0)
  1695. reset_vma_truncate_counts(mapping);
  1696. mapping->truncate_count++;
  1697. }
  1698. details.truncate_count = mapping->truncate_count;
  1699. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1700. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1701. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1702. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1703. spin_unlock(&mapping->i_mmap_lock);
  1704. }
  1705. EXPORT_SYMBOL(unmap_mapping_range);
  1706. /**
  1707. * vmtruncate - unmap mappings "freed" by truncate() syscall
  1708. * @inode: inode of the file used
  1709. * @offset: file offset to start truncating
  1710. *
  1711. * NOTE! We have to be ready to update the memory sharing
  1712. * between the file and the memory map for a potential last
  1713. * incomplete page. Ugly, but necessary.
  1714. */
  1715. int vmtruncate(struct inode * inode, loff_t offset)
  1716. {
  1717. struct address_space *mapping = inode->i_mapping;
  1718. unsigned long limit;
  1719. if (inode->i_size < offset)
  1720. goto do_expand;
  1721. /*
  1722. * truncation of in-use swapfiles is disallowed - it would cause
  1723. * subsequent swapout to scribble on the now-freed blocks.
  1724. */
  1725. if (IS_SWAPFILE(inode))
  1726. goto out_busy;
  1727. i_size_write(inode, offset);
  1728. /*
  1729. * unmap_mapping_range is called twice, first simply for efficiency
  1730. * so that truncate_inode_pages does fewer single-page unmaps. However
  1731. * after this first call, and before truncate_inode_pages finishes,
  1732. * it is possible for private pages to be COWed, which remain after
  1733. * truncate_inode_pages finishes, hence the second unmap_mapping_range
  1734. * call must be made for correctness.
  1735. */
  1736. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1737. truncate_inode_pages(mapping, offset);
  1738. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1739. goto out_truncate;
  1740. do_expand:
  1741. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1742. if (limit != RLIM_INFINITY && offset > limit)
  1743. goto out_sig;
  1744. if (offset > inode->i_sb->s_maxbytes)
  1745. goto out_big;
  1746. i_size_write(inode, offset);
  1747. out_truncate:
  1748. if (inode->i_op && inode->i_op->truncate)
  1749. inode->i_op->truncate(inode);
  1750. return 0;
  1751. out_sig:
  1752. send_sig(SIGXFSZ, current, 0);
  1753. out_big:
  1754. return -EFBIG;
  1755. out_busy:
  1756. return -ETXTBSY;
  1757. }
  1758. EXPORT_SYMBOL(vmtruncate);
  1759. int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
  1760. {
  1761. struct address_space *mapping = inode->i_mapping;
  1762. /*
  1763. * If the underlying filesystem is not going to provide
  1764. * a way to truncate a range of blocks (punch a hole) -
  1765. * we should return failure right now.
  1766. */
  1767. if (!inode->i_op || !inode->i_op->truncate_range)
  1768. return -ENOSYS;
  1769. mutex_lock(&inode->i_mutex);
  1770. down_write(&inode->i_alloc_sem);
  1771. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1772. truncate_inode_pages_range(mapping, offset, end);
  1773. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1774. inode->i_op->truncate_range(inode, offset, end);
  1775. up_write(&inode->i_alloc_sem);
  1776. mutex_unlock(&inode->i_mutex);
  1777. return 0;
  1778. }
  1779. /**
  1780. * swapin_readahead - swap in pages in hope we need them soon
  1781. * @entry: swap entry of this memory
  1782. * @addr: address to start
  1783. * @vma: user vma this addresses belong to
  1784. *
  1785. * Primitive swap readahead code. We simply read an aligned block of
  1786. * (1 << page_cluster) entries in the swap area. This method is chosen
  1787. * because it doesn't cost us any seek time. We also make sure to queue
  1788. * the 'original' request together with the readahead ones...
  1789. *
  1790. * This has been extended to use the NUMA policies from the mm triggering
  1791. * the readahead.
  1792. *
  1793. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1794. */
  1795. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1796. {
  1797. #ifdef CONFIG_NUMA
  1798. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1799. #endif
  1800. int i, num;
  1801. struct page *new_page;
  1802. unsigned long offset;
  1803. /*
  1804. * Get the number of handles we should do readahead io to.
  1805. */
  1806. num = valid_swaphandles(entry, &offset);
  1807. for (i = 0; i < num; offset++, i++) {
  1808. /* Ok, do the async read-ahead now */
  1809. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1810. offset), vma, addr);
  1811. if (!new_page)
  1812. break;
  1813. page_cache_release(new_page);
  1814. #ifdef CONFIG_NUMA
  1815. /*
  1816. * Find the next applicable VMA for the NUMA policy.
  1817. */
  1818. addr += PAGE_SIZE;
  1819. if (addr == 0)
  1820. vma = NULL;
  1821. if (vma) {
  1822. if (addr >= vma->vm_end) {
  1823. vma = next_vma;
  1824. next_vma = vma ? vma->vm_next : NULL;
  1825. }
  1826. if (vma && addr < vma->vm_start)
  1827. vma = NULL;
  1828. } else {
  1829. if (next_vma && addr >= next_vma->vm_start) {
  1830. vma = next_vma;
  1831. next_vma = vma->vm_next;
  1832. }
  1833. }
  1834. #endif
  1835. }
  1836. lru_add_drain(); /* Push any new pages onto the LRU now */
  1837. }
  1838. /*
  1839. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1840. * but allow concurrent faults), and pte mapped but not yet locked.
  1841. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1842. */
  1843. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1844. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1845. int write_access, pte_t orig_pte)
  1846. {
  1847. spinlock_t *ptl;
  1848. struct page *page;
  1849. swp_entry_t entry;
  1850. pte_t pte;
  1851. int ret = 0;
  1852. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1853. goto out;
  1854. entry = pte_to_swp_entry(orig_pte);
  1855. if (is_migration_entry(entry)) {
  1856. migration_entry_wait(mm, pmd, address);
  1857. goto out;
  1858. }
  1859. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  1860. page = lookup_swap_cache(entry);
  1861. if (!page) {
  1862. grab_swap_token(); /* Contend for token _before_ read-in */
  1863. swapin_readahead(entry, address, vma);
  1864. page = read_swap_cache_async(entry, vma, address);
  1865. if (!page) {
  1866. /*
  1867. * Back out if somebody else faulted in this pte
  1868. * while we released the pte lock.
  1869. */
  1870. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1871. if (likely(pte_same(*page_table, orig_pte)))
  1872. ret = VM_FAULT_OOM;
  1873. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1874. goto unlock;
  1875. }
  1876. /* Had to read the page from swap area: Major fault */
  1877. ret = VM_FAULT_MAJOR;
  1878. count_vm_event(PGMAJFAULT);
  1879. }
  1880. mark_page_accessed(page);
  1881. lock_page(page);
  1882. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1883. /*
  1884. * Back out if somebody else already faulted in this pte.
  1885. */
  1886. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1887. if (unlikely(!pte_same(*page_table, orig_pte)))
  1888. goto out_nomap;
  1889. if (unlikely(!PageUptodate(page))) {
  1890. ret = VM_FAULT_SIGBUS;
  1891. goto out_nomap;
  1892. }
  1893. /* The page isn't present yet, go ahead with the fault. */
  1894. inc_mm_counter(mm, anon_rss);
  1895. pte = mk_pte(page, vma->vm_page_prot);
  1896. if (write_access && can_share_swap_page(page)) {
  1897. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1898. write_access = 0;
  1899. }
  1900. flush_icache_page(vma, page);
  1901. set_pte_at(mm, address, page_table, pte);
  1902. page_add_anon_rmap(page, vma, address);
  1903. swap_free(entry);
  1904. if (vm_swap_full())
  1905. remove_exclusive_swap_page(page);
  1906. unlock_page(page);
  1907. if (write_access) {
  1908. /* XXX: We could OR the do_wp_page code with this one? */
  1909. if (do_wp_page(mm, vma, address,
  1910. page_table, pmd, ptl, pte) & VM_FAULT_OOM)
  1911. ret = VM_FAULT_OOM;
  1912. goto out;
  1913. }
  1914. /* No need to invalidate - it was non-present before */
  1915. update_mmu_cache(vma, address, pte);
  1916. unlock:
  1917. pte_unmap_unlock(page_table, ptl);
  1918. out:
  1919. return ret;
  1920. out_nomap:
  1921. pte_unmap_unlock(page_table, ptl);
  1922. unlock_page(page);
  1923. page_cache_release(page);
  1924. return ret;
  1925. }
  1926. /*
  1927. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1928. * but allow concurrent faults), and pte mapped but not yet locked.
  1929. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1930. */
  1931. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1932. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1933. int write_access)
  1934. {
  1935. struct page *page;
  1936. spinlock_t *ptl;
  1937. pte_t entry;
  1938. /* Allocate our own private page. */
  1939. pte_unmap(page_table);
  1940. if (unlikely(anon_vma_prepare(vma)))
  1941. goto oom;
  1942. page = alloc_zeroed_user_highpage_movable(vma, address);
  1943. if (!page)
  1944. goto oom;
  1945. entry = mk_pte(page, vma->vm_page_prot);
  1946. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1947. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1948. if (!pte_none(*page_table))
  1949. goto release;
  1950. inc_mm_counter(mm, anon_rss);
  1951. lru_cache_add_active(page);
  1952. page_add_new_anon_rmap(page, vma, address);
  1953. set_pte_at(mm, address, page_table, entry);
  1954. /* No need to invalidate - it was non-present before */
  1955. update_mmu_cache(vma, address, entry);
  1956. unlock:
  1957. pte_unmap_unlock(page_table, ptl);
  1958. return 0;
  1959. release:
  1960. page_cache_release(page);
  1961. goto unlock;
  1962. oom:
  1963. return VM_FAULT_OOM;
  1964. }
  1965. /*
  1966. * __do_fault() tries to create a new page mapping. It aggressively
  1967. * tries to share with existing pages, but makes a separate copy if
  1968. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  1969. * the next page fault.
  1970. *
  1971. * As this is called only for pages that do not currently exist, we
  1972. * do not need to flush old virtual caches or the TLB.
  1973. *
  1974. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1975. * but allow concurrent faults), and pte neither mapped nor locked.
  1976. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1977. */
  1978. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1979. unsigned long address, pmd_t *pmd,
  1980. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  1981. {
  1982. pte_t *page_table;
  1983. spinlock_t *ptl;
  1984. struct page *page;
  1985. pte_t entry;
  1986. int anon = 0;
  1987. struct page *dirty_page = NULL;
  1988. struct vm_fault vmf;
  1989. int ret;
  1990. int page_mkwrite = 0;
  1991. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  1992. vmf.pgoff = pgoff;
  1993. vmf.flags = flags;
  1994. vmf.page = NULL;
  1995. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1996. if (likely(vma->vm_ops->fault)) {
  1997. ret = vma->vm_ops->fault(vma, &vmf);
  1998. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
  1999. return ret;
  2000. } else {
  2001. /* Legacy ->nopage path */
  2002. ret = 0;
  2003. vmf.page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
  2004. /* no page was available -- either SIGBUS or OOM */
  2005. if (unlikely(vmf.page == NOPAGE_SIGBUS))
  2006. return VM_FAULT_SIGBUS;
  2007. else if (unlikely(vmf.page == NOPAGE_OOM))
  2008. return VM_FAULT_OOM;
  2009. }
  2010. /*
  2011. * For consistency in subsequent calls, make the faulted page always
  2012. * locked.
  2013. */
  2014. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2015. lock_page(vmf.page);
  2016. else
  2017. VM_BUG_ON(!PageLocked(vmf.page));
  2018. /*
  2019. * Should we do an early C-O-W break?
  2020. */
  2021. page = vmf.page;
  2022. if (flags & FAULT_FLAG_WRITE) {
  2023. if (!(vma->vm_flags & VM_SHARED)) {
  2024. anon = 1;
  2025. if (unlikely(anon_vma_prepare(vma))) {
  2026. ret = VM_FAULT_OOM;
  2027. goto out;
  2028. }
  2029. page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
  2030. vma, address);
  2031. if (!page) {
  2032. ret = VM_FAULT_OOM;
  2033. goto out;
  2034. }
  2035. copy_user_highpage(page, vmf.page, address, vma);
  2036. } else {
  2037. /*
  2038. * If the page will be shareable, see if the backing
  2039. * address space wants to know that the page is about
  2040. * to become writable
  2041. */
  2042. if (vma->vm_ops->page_mkwrite) {
  2043. unlock_page(page);
  2044. if (vma->vm_ops->page_mkwrite(vma, page) < 0) {
  2045. ret = VM_FAULT_SIGBUS;
  2046. anon = 1; /* no anon but release vmf.page */
  2047. goto out_unlocked;
  2048. }
  2049. lock_page(page);
  2050. /*
  2051. * XXX: this is not quite right (racy vs
  2052. * invalidate) to unlock and relock the page
  2053. * like this, however a better fix requires
  2054. * reworking page_mkwrite locking API, which
  2055. * is better done later.
  2056. */
  2057. if (!page->mapping) {
  2058. ret = 0;
  2059. anon = 1; /* no anon but release vmf.page */
  2060. goto out;
  2061. }
  2062. page_mkwrite = 1;
  2063. }
  2064. }
  2065. }
  2066. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2067. /*
  2068. * This silly early PAGE_DIRTY setting removes a race
  2069. * due to the bad i386 page protection. But it's valid
  2070. * for other architectures too.
  2071. *
  2072. * Note that if write_access is true, we either now have
  2073. * an exclusive copy of the page, or this is a shared mapping,
  2074. * so we can make it writable and dirty to avoid having to
  2075. * handle that later.
  2076. */
  2077. /* Only go through if we didn't race with anybody else... */
  2078. if (likely(pte_same(*page_table, orig_pte))) {
  2079. flush_icache_page(vma, page);
  2080. entry = mk_pte(page, vma->vm_page_prot);
  2081. if (flags & FAULT_FLAG_WRITE)
  2082. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2083. set_pte_at(mm, address, page_table, entry);
  2084. if (anon) {
  2085. inc_mm_counter(mm, anon_rss);
  2086. lru_cache_add_active(page);
  2087. page_add_new_anon_rmap(page, vma, address);
  2088. } else {
  2089. inc_mm_counter(mm, file_rss);
  2090. page_add_file_rmap(page);
  2091. if (flags & FAULT_FLAG_WRITE) {
  2092. dirty_page = page;
  2093. get_page(dirty_page);
  2094. }
  2095. }
  2096. /* no need to invalidate: a not-present page won't be cached */
  2097. update_mmu_cache(vma, address, entry);
  2098. } else {
  2099. if (anon)
  2100. page_cache_release(page);
  2101. else
  2102. anon = 1; /* no anon but release faulted_page */
  2103. }
  2104. pte_unmap_unlock(page_table, ptl);
  2105. out:
  2106. unlock_page(vmf.page);
  2107. out_unlocked:
  2108. if (anon)
  2109. page_cache_release(vmf.page);
  2110. else if (dirty_page) {
  2111. set_page_dirty_balance(dirty_page, page_mkwrite);
  2112. put_page(dirty_page);
  2113. }
  2114. return ret;
  2115. }
  2116. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2117. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2118. int write_access, pte_t orig_pte)
  2119. {
  2120. pgoff_t pgoff = (((address & PAGE_MASK)
  2121. - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2122. unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0);
  2123. pte_unmap(page_table);
  2124. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2125. }
  2126. /*
  2127. * do_no_pfn() tries to create a new page mapping for a page without
  2128. * a struct_page backing it
  2129. *
  2130. * As this is called only for pages that do not currently exist, we
  2131. * do not need to flush old virtual caches or the TLB.
  2132. *
  2133. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2134. * but allow concurrent faults), and pte mapped but not yet locked.
  2135. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2136. *
  2137. * It is expected that the ->nopfn handler always returns the same pfn
  2138. * for a given virtual mapping.
  2139. *
  2140. * Mark this `noinline' to prevent it from bloating the main pagefault code.
  2141. */
  2142. static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma,
  2143. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2144. int write_access)
  2145. {
  2146. spinlock_t *ptl;
  2147. pte_t entry;
  2148. unsigned long pfn;
  2149. pte_unmap(page_table);
  2150. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  2151. BUG_ON(is_cow_mapping(vma->vm_flags));
  2152. pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK);
  2153. if (unlikely(pfn == NOPFN_OOM))
  2154. return VM_FAULT_OOM;
  2155. else if (unlikely(pfn == NOPFN_SIGBUS))
  2156. return VM_FAULT_SIGBUS;
  2157. else if (unlikely(pfn == NOPFN_REFAULT))
  2158. return 0;
  2159. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2160. /* Only go through if we didn't race with anybody else... */
  2161. if (pte_none(*page_table)) {
  2162. entry = pfn_pte(pfn, vma->vm_page_prot);
  2163. if (write_access)
  2164. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2165. set_pte_at(mm, address, page_table, entry);
  2166. }
  2167. pte_unmap_unlock(page_table, ptl);
  2168. return 0;
  2169. }
  2170. /*
  2171. * Fault of a previously existing named mapping. Repopulate the pte
  2172. * from the encoded file_pte if possible. This enables swappable
  2173. * nonlinear vmas.
  2174. *
  2175. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2176. * but allow concurrent faults), and pte mapped but not yet locked.
  2177. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2178. */
  2179. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2180. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2181. int write_access, pte_t orig_pte)
  2182. {
  2183. unsigned int flags = FAULT_FLAG_NONLINEAR |
  2184. (write_access ? FAULT_FLAG_WRITE : 0);
  2185. pgoff_t pgoff;
  2186. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2187. return 0;
  2188. if (unlikely(!(vma->vm_flags & VM_NONLINEAR) ||
  2189. !(vma->vm_flags & VM_CAN_NONLINEAR))) {
  2190. /*
  2191. * Page table corrupted: show pte and kill process.
  2192. */
  2193. print_bad_pte(vma, orig_pte, address);
  2194. return VM_FAULT_OOM;
  2195. }
  2196. pgoff = pte_to_pgoff(orig_pte);
  2197. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2198. }
  2199. /*
  2200. * These routines also need to handle stuff like marking pages dirty
  2201. * and/or accessed for architectures that don't do it in hardware (most
  2202. * RISC architectures). The early dirtying is also good on the i386.
  2203. *
  2204. * There is also a hook called "update_mmu_cache()" that architectures
  2205. * with external mmu caches can use to update those (ie the Sparc or
  2206. * PowerPC hashed page tables that act as extended TLBs).
  2207. *
  2208. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2209. * but allow concurrent faults), and pte mapped but not yet locked.
  2210. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2211. */
  2212. static inline int handle_pte_fault(struct mm_struct *mm,
  2213. struct vm_area_struct *vma, unsigned long address,
  2214. pte_t *pte, pmd_t *pmd, int write_access)
  2215. {
  2216. pte_t entry;
  2217. spinlock_t *ptl;
  2218. entry = *pte;
  2219. if (!pte_present(entry)) {
  2220. if (pte_none(entry)) {
  2221. if (vma->vm_ops) {
  2222. if (vma->vm_ops->fault || vma->vm_ops->nopage)
  2223. return do_linear_fault(mm, vma, address,
  2224. pte, pmd, write_access, entry);
  2225. if (unlikely(vma->vm_ops->nopfn))
  2226. return do_no_pfn(mm, vma, address, pte,
  2227. pmd, write_access);
  2228. }
  2229. return do_anonymous_page(mm, vma, address,
  2230. pte, pmd, write_access);
  2231. }
  2232. if (pte_file(entry))
  2233. return do_nonlinear_fault(mm, vma, address,
  2234. pte, pmd, write_access, entry);
  2235. return do_swap_page(mm, vma, address,
  2236. pte, pmd, write_access, entry);
  2237. }
  2238. ptl = pte_lockptr(mm, pmd);
  2239. spin_lock(ptl);
  2240. if (unlikely(!pte_same(*pte, entry)))
  2241. goto unlock;
  2242. if (write_access) {
  2243. if (!pte_write(entry))
  2244. return do_wp_page(mm, vma, address,
  2245. pte, pmd, ptl, entry);
  2246. entry = pte_mkdirty(entry);
  2247. }
  2248. entry = pte_mkyoung(entry);
  2249. if (ptep_set_access_flags(vma, address, pte, entry, write_access)) {
  2250. update_mmu_cache(vma, address, entry);
  2251. } else {
  2252. /*
  2253. * This is needed only for protection faults but the arch code
  2254. * is not yet telling us if this is a protection fault or not.
  2255. * This still avoids useless tlb flushes for .text page faults
  2256. * with threads.
  2257. */
  2258. if (write_access)
  2259. flush_tlb_page(vma, address);
  2260. }
  2261. unlock:
  2262. pte_unmap_unlock(pte, ptl);
  2263. return 0;
  2264. }
  2265. /*
  2266. * By the time we get here, we already hold the mm semaphore
  2267. */
  2268. int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2269. unsigned long address, int write_access)
  2270. {
  2271. pgd_t *pgd;
  2272. pud_t *pud;
  2273. pmd_t *pmd;
  2274. pte_t *pte;
  2275. __set_current_state(TASK_RUNNING);
  2276. count_vm_event(PGFAULT);
  2277. if (unlikely(is_vm_hugetlb_page(vma)))
  2278. return hugetlb_fault(mm, vma, address, write_access);
  2279. pgd = pgd_offset(mm, address);
  2280. pud = pud_alloc(mm, pgd, address);
  2281. if (!pud)
  2282. return VM_FAULT_OOM;
  2283. pmd = pmd_alloc(mm, pud, address);
  2284. if (!pmd)
  2285. return VM_FAULT_OOM;
  2286. pte = pte_alloc_map(mm, pmd, address);
  2287. if (!pte)
  2288. return VM_FAULT_OOM;
  2289. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  2290. }
  2291. #ifndef __PAGETABLE_PUD_FOLDED
  2292. /*
  2293. * Allocate page upper directory.
  2294. * We've already handled the fast-path in-line.
  2295. */
  2296. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2297. {
  2298. pud_t *new = pud_alloc_one(mm, address);
  2299. if (!new)
  2300. return -ENOMEM;
  2301. spin_lock(&mm->page_table_lock);
  2302. if (pgd_present(*pgd)) /* Another has populated it */
  2303. pud_free(new);
  2304. else
  2305. pgd_populate(mm, pgd, new);
  2306. spin_unlock(&mm->page_table_lock);
  2307. return 0;
  2308. }
  2309. #endif /* __PAGETABLE_PUD_FOLDED */
  2310. #ifndef __PAGETABLE_PMD_FOLDED
  2311. /*
  2312. * Allocate page middle directory.
  2313. * We've already handled the fast-path in-line.
  2314. */
  2315. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2316. {
  2317. pmd_t *new = pmd_alloc_one(mm, address);
  2318. if (!new)
  2319. return -ENOMEM;
  2320. spin_lock(&mm->page_table_lock);
  2321. #ifndef __ARCH_HAS_4LEVEL_HACK
  2322. if (pud_present(*pud)) /* Another has populated it */
  2323. pmd_free(new);
  2324. else
  2325. pud_populate(mm, pud, new);
  2326. #else
  2327. if (pgd_present(*pud)) /* Another has populated it */
  2328. pmd_free(new);
  2329. else
  2330. pgd_populate(mm, pud, new);
  2331. #endif /* __ARCH_HAS_4LEVEL_HACK */
  2332. spin_unlock(&mm->page_table_lock);
  2333. return 0;
  2334. }
  2335. #endif /* __PAGETABLE_PMD_FOLDED */
  2336. int make_pages_present(unsigned long addr, unsigned long end)
  2337. {
  2338. int ret, len, write;
  2339. struct vm_area_struct * vma;
  2340. vma = find_vma(current->mm, addr);
  2341. if (!vma)
  2342. return -1;
  2343. write = (vma->vm_flags & VM_WRITE) != 0;
  2344. BUG_ON(addr >= end);
  2345. BUG_ON(end > vma->vm_end);
  2346. len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
  2347. ret = get_user_pages(current, current->mm, addr,
  2348. len, write, 0, NULL, NULL);
  2349. if (ret < 0)
  2350. return ret;
  2351. return ret == len ? 0 : -1;
  2352. }
  2353. /*
  2354. * Map a vmalloc()-space virtual address to the physical page.
  2355. */
  2356. struct page * vmalloc_to_page(void * vmalloc_addr)
  2357. {
  2358. unsigned long addr = (unsigned long) vmalloc_addr;
  2359. struct page *page = NULL;
  2360. pgd_t *pgd = pgd_offset_k(addr);
  2361. pud_t *pud;
  2362. pmd_t *pmd;
  2363. pte_t *ptep, pte;
  2364. if (!pgd_none(*pgd)) {
  2365. pud = pud_offset(pgd, addr);
  2366. if (!pud_none(*pud)) {
  2367. pmd = pmd_offset(pud, addr);
  2368. if (!pmd_none(*pmd)) {
  2369. ptep = pte_offset_map(pmd, addr);
  2370. pte = *ptep;
  2371. if (pte_present(pte))
  2372. page = pte_page(pte);
  2373. pte_unmap(ptep);
  2374. }
  2375. }
  2376. }
  2377. return page;
  2378. }
  2379. EXPORT_SYMBOL(vmalloc_to_page);
  2380. /*
  2381. * Map a vmalloc()-space virtual address to the physical page frame number.
  2382. */
  2383. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  2384. {
  2385. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  2386. }
  2387. EXPORT_SYMBOL(vmalloc_to_pfn);
  2388. #if !defined(__HAVE_ARCH_GATE_AREA)
  2389. #if defined(AT_SYSINFO_EHDR)
  2390. static struct vm_area_struct gate_vma;
  2391. static int __init gate_vma_init(void)
  2392. {
  2393. gate_vma.vm_mm = NULL;
  2394. gate_vma.vm_start = FIXADDR_USER_START;
  2395. gate_vma.vm_end = FIXADDR_USER_END;
  2396. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  2397. gate_vma.vm_page_prot = __P101;
  2398. /*
  2399. * Make sure the vDSO gets into every core dump.
  2400. * Dumping its contents makes post-mortem fully interpretable later
  2401. * without matching up the same kernel and hardware config to see
  2402. * what PC values meant.
  2403. */
  2404. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  2405. return 0;
  2406. }
  2407. __initcall(gate_vma_init);
  2408. #endif
  2409. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2410. {
  2411. #ifdef AT_SYSINFO_EHDR
  2412. return &gate_vma;
  2413. #else
  2414. return NULL;
  2415. #endif
  2416. }
  2417. int in_gate_area_no_task(unsigned long addr)
  2418. {
  2419. #ifdef AT_SYSINFO_EHDR
  2420. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2421. return 1;
  2422. #endif
  2423. return 0;
  2424. }
  2425. #endif /* __HAVE_ARCH_GATE_AREA */
  2426. /*
  2427. * Access another process' address space.
  2428. * Source/target buffer must be kernel space,
  2429. * Do not walk the page table directly, use get_user_pages
  2430. */
  2431. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  2432. {
  2433. struct mm_struct *mm;
  2434. struct vm_area_struct *vma;
  2435. struct page *page;
  2436. void *old_buf = buf;
  2437. mm = get_task_mm(tsk);
  2438. if (!mm)
  2439. return 0;
  2440. down_read(&mm->mmap_sem);
  2441. /* ignore errors, just check how much was successfully transferred */
  2442. while (len) {
  2443. int bytes, ret, offset;
  2444. void *maddr;
  2445. ret = get_user_pages(tsk, mm, addr, 1,
  2446. write, 1, &page, &vma);
  2447. if (ret <= 0)
  2448. break;
  2449. bytes = len;
  2450. offset = addr & (PAGE_SIZE-1);
  2451. if (bytes > PAGE_SIZE-offset)
  2452. bytes = PAGE_SIZE-offset;
  2453. maddr = kmap(page);
  2454. if (write) {
  2455. copy_to_user_page(vma, page, addr,
  2456. maddr + offset, buf, bytes);
  2457. set_page_dirty_lock(page);
  2458. } else {
  2459. copy_from_user_page(vma, page, addr,
  2460. buf, maddr + offset, bytes);
  2461. }
  2462. kunmap(page);
  2463. page_cache_release(page);
  2464. len -= bytes;
  2465. buf += bytes;
  2466. addr += bytes;
  2467. }
  2468. up_read(&mm->mmap_sem);
  2469. mmput(mm);
  2470. return buf - old_buf;
  2471. }