memory.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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/init.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/tlb.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/pgtable.h>
  51. #include <linux/swapops.h>
  52. #include <linux/elf.h>
  53. #ifndef CONFIG_NEED_MULTIPLE_NODES
  54. /* use the per-pgdat data instead for discontigmem - mbligh */
  55. unsigned long max_mapnr;
  56. struct page *mem_map;
  57. EXPORT_SYMBOL(max_mapnr);
  58. EXPORT_SYMBOL(mem_map);
  59. #endif
  60. unsigned long num_physpages;
  61. /*
  62. * A number of key systems in x86 including ioremap() rely on the assumption
  63. * that high_memory defines the upper bound on direct map memory, then end
  64. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  65. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  66. * and ZONE_HIGHMEM.
  67. */
  68. void * high_memory;
  69. unsigned long vmalloc_earlyreserve;
  70. EXPORT_SYMBOL(num_physpages);
  71. EXPORT_SYMBOL(high_memory);
  72. EXPORT_SYMBOL(vmalloc_earlyreserve);
  73. /*
  74. * If a p?d_bad entry is found while walking page tables, report
  75. * the error, before resetting entry to p?d_none. Usually (but
  76. * very seldom) called out from the p?d_none_or_clear_bad macros.
  77. */
  78. void pgd_clear_bad(pgd_t *pgd)
  79. {
  80. pgd_ERROR(*pgd);
  81. pgd_clear(pgd);
  82. }
  83. void pud_clear_bad(pud_t *pud)
  84. {
  85. pud_ERROR(*pud);
  86. pud_clear(pud);
  87. }
  88. void pmd_clear_bad(pmd_t *pmd)
  89. {
  90. pmd_ERROR(*pmd);
  91. pmd_clear(pmd);
  92. }
  93. /*
  94. * Note: this doesn't free the actual pages themselves. That
  95. * has been handled earlier when unmapping all the memory regions.
  96. */
  97. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
  98. {
  99. struct page *page = pmd_page(*pmd);
  100. pmd_clear(pmd);
  101. pte_free_tlb(tlb, page);
  102. dec_page_state(nr_page_table_pages);
  103. tlb->mm->nr_ptes--;
  104. }
  105. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  106. unsigned long addr, unsigned long end,
  107. unsigned long floor, unsigned long ceiling)
  108. {
  109. pmd_t *pmd;
  110. unsigned long next;
  111. unsigned long start;
  112. start = addr;
  113. pmd = pmd_offset(pud, addr);
  114. do {
  115. next = pmd_addr_end(addr, end);
  116. if (pmd_none_or_clear_bad(pmd))
  117. continue;
  118. free_pte_range(tlb, pmd);
  119. } while (pmd++, addr = next, addr != end);
  120. start &= PUD_MASK;
  121. if (start < floor)
  122. return;
  123. if (ceiling) {
  124. ceiling &= PUD_MASK;
  125. if (!ceiling)
  126. return;
  127. }
  128. if (end - 1 > ceiling - 1)
  129. return;
  130. pmd = pmd_offset(pud, start);
  131. pud_clear(pud);
  132. pmd_free_tlb(tlb, pmd);
  133. }
  134. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  135. unsigned long addr, unsigned long end,
  136. unsigned long floor, unsigned long ceiling)
  137. {
  138. pud_t *pud;
  139. unsigned long next;
  140. unsigned long start;
  141. start = addr;
  142. pud = pud_offset(pgd, addr);
  143. do {
  144. next = pud_addr_end(addr, end);
  145. if (pud_none_or_clear_bad(pud))
  146. continue;
  147. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  148. } while (pud++, addr = next, addr != end);
  149. start &= PGDIR_MASK;
  150. if (start < floor)
  151. return;
  152. if (ceiling) {
  153. ceiling &= PGDIR_MASK;
  154. if (!ceiling)
  155. return;
  156. }
  157. if (end - 1 > ceiling - 1)
  158. return;
  159. pud = pud_offset(pgd, start);
  160. pgd_clear(pgd);
  161. pud_free_tlb(tlb, pud);
  162. }
  163. /*
  164. * This function frees user-level page tables of a process.
  165. *
  166. * Must be called with pagetable lock held.
  167. */
  168. void free_pgd_range(struct mmu_gather **tlb,
  169. unsigned long addr, unsigned long end,
  170. unsigned long floor, unsigned long ceiling)
  171. {
  172. pgd_t *pgd;
  173. unsigned long next;
  174. unsigned long start;
  175. /*
  176. * The next few lines have given us lots of grief...
  177. *
  178. * Why are we testing PMD* at this top level? Because often
  179. * there will be no work to do at all, and we'd prefer not to
  180. * go all the way down to the bottom just to discover that.
  181. *
  182. * Why all these "- 1"s? Because 0 represents both the bottom
  183. * of the address space and the top of it (using -1 for the
  184. * top wouldn't help much: the masks would do the wrong thing).
  185. * The rule is that addr 0 and floor 0 refer to the bottom of
  186. * the address space, but end 0 and ceiling 0 refer to the top
  187. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  188. * that end 0 case should be mythical).
  189. *
  190. * Wherever addr is brought up or ceiling brought down, we must
  191. * be careful to reject "the opposite 0" before it confuses the
  192. * subsequent tests. But what about where end is brought down
  193. * by PMD_SIZE below? no, end can't go down to 0 there.
  194. *
  195. * Whereas we round start (addr) and ceiling down, by different
  196. * masks at different levels, in order to test whether a table
  197. * now has no other vmas using it, so can be freed, we don't
  198. * bother to round floor or end up - the tests don't need that.
  199. */
  200. addr &= PMD_MASK;
  201. if (addr < floor) {
  202. addr += PMD_SIZE;
  203. if (!addr)
  204. return;
  205. }
  206. if (ceiling) {
  207. ceiling &= PMD_MASK;
  208. if (!ceiling)
  209. return;
  210. }
  211. if (end - 1 > ceiling - 1)
  212. end -= PMD_SIZE;
  213. if (addr > end - 1)
  214. return;
  215. start = addr;
  216. pgd = pgd_offset((*tlb)->mm, addr);
  217. do {
  218. next = pgd_addr_end(addr, end);
  219. if (pgd_none_or_clear_bad(pgd))
  220. continue;
  221. free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  222. } while (pgd++, addr = next, addr != end);
  223. if (!(*tlb)->fullmm)
  224. flush_tlb_pgtables((*tlb)->mm, start, end);
  225. }
  226. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
  227. unsigned long floor, unsigned long ceiling)
  228. {
  229. while (vma) {
  230. struct vm_area_struct *next = vma->vm_next;
  231. unsigned long addr = vma->vm_start;
  232. /*
  233. * Hide vma from rmap and vmtruncate before freeing pgtables
  234. */
  235. anon_vma_unlink(vma);
  236. unlink_file_vma(vma);
  237. if (is_hugepage_only_range(vma->vm_mm, addr, HPAGE_SIZE)) {
  238. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  239. floor, next? next->vm_start: ceiling);
  240. } else {
  241. /*
  242. * Optimization: gather nearby vmas into one call down
  243. */
  244. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  245. && !is_hugepage_only_range(vma->vm_mm, next->vm_start,
  246. HPAGE_SIZE)) {
  247. vma = next;
  248. next = vma->vm_next;
  249. anon_vma_unlink(vma);
  250. unlink_file_vma(vma);
  251. }
  252. free_pgd_range(tlb, addr, vma->vm_end,
  253. floor, next? next->vm_start: ceiling);
  254. }
  255. vma = next;
  256. }
  257. }
  258. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  259. {
  260. struct page *new = pte_alloc_one(mm, address);
  261. if (!new)
  262. return -ENOMEM;
  263. spin_lock(&mm->page_table_lock);
  264. if (pmd_present(*pmd)) /* Another has populated it */
  265. pte_free(new);
  266. else {
  267. mm->nr_ptes++;
  268. inc_page_state(nr_page_table_pages);
  269. pmd_populate(mm, pmd, new);
  270. }
  271. spin_unlock(&mm->page_table_lock);
  272. return 0;
  273. }
  274. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  275. {
  276. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  277. if (!new)
  278. return -ENOMEM;
  279. spin_lock(&init_mm.page_table_lock);
  280. if (pmd_present(*pmd)) /* Another has populated it */
  281. pte_free_kernel(new);
  282. else
  283. pmd_populate_kernel(&init_mm, pmd, new);
  284. spin_unlock(&init_mm.page_table_lock);
  285. return 0;
  286. }
  287. static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
  288. {
  289. if (file_rss)
  290. add_mm_counter(mm, file_rss, file_rss);
  291. if (anon_rss)
  292. add_mm_counter(mm, anon_rss, anon_rss);
  293. }
  294. /*
  295. * This function is called to print an error when a pte in a
  296. * !VM_RESERVED region is found pointing to an invalid pfn (which
  297. * is an error.
  298. *
  299. * The calling function must still handle the error.
  300. */
  301. void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
  302. {
  303. printk(KERN_ERR "Bad pte = %08llx, process = %s, "
  304. "vm_flags = %lx, vaddr = %lx\n",
  305. (long long)pte_val(pte),
  306. (vma->vm_mm == current->mm ? current->comm : "???"),
  307. vma->vm_flags, vaddr);
  308. dump_stack();
  309. }
  310. /*
  311. * copy one vm_area from one task to the other. Assumes the page tables
  312. * already present in the new task to be cleared in the whole range
  313. * covered by this vma.
  314. */
  315. static inline void
  316. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  317. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  318. unsigned long addr, int *rss)
  319. {
  320. unsigned long vm_flags = vma->vm_flags;
  321. pte_t pte = *src_pte;
  322. struct page *page;
  323. unsigned long pfn;
  324. /* pte contains position in swap or file, so copy. */
  325. if (unlikely(!pte_present(pte))) {
  326. if (!pte_file(pte)) {
  327. swap_duplicate(pte_to_swp_entry(pte));
  328. /* make sure dst_mm is on swapoff's mmlist. */
  329. if (unlikely(list_empty(&dst_mm->mmlist))) {
  330. spin_lock(&mmlist_lock);
  331. list_add(&dst_mm->mmlist, &src_mm->mmlist);
  332. spin_unlock(&mmlist_lock);
  333. }
  334. }
  335. goto out_set_pte;
  336. }
  337. /* If the region is VM_RESERVED, the mapping is not
  338. * mapped via rmap - duplicate the pte as is.
  339. */
  340. if (vm_flags & VM_RESERVED)
  341. goto out_set_pte;
  342. pfn = pte_pfn(pte);
  343. /* If the pte points outside of valid memory but
  344. * the region is not VM_RESERVED, we have a problem.
  345. */
  346. if (unlikely(!pfn_valid(pfn))) {
  347. print_bad_pte(vma, pte, addr);
  348. goto out_set_pte; /* try to do something sane */
  349. }
  350. page = pfn_to_page(pfn);
  351. /*
  352. * If it's a COW mapping, write protect it both
  353. * in the parent and the child
  354. */
  355. if ((vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) {
  356. ptep_set_wrprotect(src_mm, addr, src_pte);
  357. pte = *src_pte;
  358. }
  359. /*
  360. * If it's a shared mapping, mark it clean in
  361. * the child
  362. */
  363. if (vm_flags & VM_SHARED)
  364. pte = pte_mkclean(pte);
  365. pte = pte_mkold(pte);
  366. get_page(page);
  367. page_dup_rmap(page);
  368. rss[!!PageAnon(page)]++;
  369. out_set_pte:
  370. set_pte_at(dst_mm, addr, dst_pte, pte);
  371. }
  372. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  373. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  374. unsigned long addr, unsigned long end)
  375. {
  376. pte_t *src_pte, *dst_pte;
  377. spinlock_t *src_ptl, *dst_ptl;
  378. int progress = 0;
  379. int rss[2];
  380. again:
  381. rss[1] = rss[0] = 0;
  382. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  383. if (!dst_pte)
  384. return -ENOMEM;
  385. src_pte = pte_offset_map_nested(src_pmd, addr);
  386. src_ptl = &src_mm->page_table_lock;
  387. spin_lock(src_ptl);
  388. do {
  389. /*
  390. * We are holding two locks at this point - either of them
  391. * could generate latencies in another task on another CPU.
  392. */
  393. if (progress >= 32) {
  394. progress = 0;
  395. if (need_resched() ||
  396. need_lockbreak(src_ptl) ||
  397. need_lockbreak(dst_ptl))
  398. break;
  399. }
  400. if (pte_none(*src_pte)) {
  401. progress++;
  402. continue;
  403. }
  404. copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
  405. progress += 8;
  406. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  407. spin_unlock(src_ptl);
  408. pte_unmap_nested(src_pte - 1);
  409. add_mm_rss(dst_mm, rss[0], rss[1]);
  410. pte_unmap_unlock(dst_pte - 1, dst_ptl);
  411. cond_resched();
  412. if (addr != end)
  413. goto again;
  414. return 0;
  415. }
  416. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  417. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  418. unsigned long addr, unsigned long end)
  419. {
  420. pmd_t *src_pmd, *dst_pmd;
  421. unsigned long next;
  422. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  423. if (!dst_pmd)
  424. return -ENOMEM;
  425. src_pmd = pmd_offset(src_pud, addr);
  426. do {
  427. next = pmd_addr_end(addr, end);
  428. if (pmd_none_or_clear_bad(src_pmd))
  429. continue;
  430. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  431. vma, addr, next))
  432. return -ENOMEM;
  433. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  434. return 0;
  435. }
  436. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  437. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  438. unsigned long addr, unsigned long end)
  439. {
  440. pud_t *src_pud, *dst_pud;
  441. unsigned long next;
  442. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  443. if (!dst_pud)
  444. return -ENOMEM;
  445. src_pud = pud_offset(src_pgd, addr);
  446. do {
  447. next = pud_addr_end(addr, end);
  448. if (pud_none_or_clear_bad(src_pud))
  449. continue;
  450. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  451. vma, addr, next))
  452. return -ENOMEM;
  453. } while (dst_pud++, src_pud++, addr = next, addr != end);
  454. return 0;
  455. }
  456. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  457. struct vm_area_struct *vma)
  458. {
  459. pgd_t *src_pgd, *dst_pgd;
  460. unsigned long next;
  461. unsigned long addr = vma->vm_start;
  462. unsigned long end = vma->vm_end;
  463. /*
  464. * Don't copy ptes where a page fault will fill them correctly.
  465. * Fork becomes much lighter when there are big shared or private
  466. * readonly mappings. The tradeoff is that copy_page_range is more
  467. * efficient than faulting.
  468. */
  469. if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_RESERVED))) {
  470. if (!vma->anon_vma)
  471. return 0;
  472. }
  473. if (is_vm_hugetlb_page(vma))
  474. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  475. dst_pgd = pgd_offset(dst_mm, addr);
  476. src_pgd = pgd_offset(src_mm, addr);
  477. do {
  478. next = pgd_addr_end(addr, end);
  479. if (pgd_none_or_clear_bad(src_pgd))
  480. continue;
  481. if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  482. vma, addr, next))
  483. return -ENOMEM;
  484. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  485. return 0;
  486. }
  487. static void zap_pte_range(struct mmu_gather *tlb,
  488. struct vm_area_struct *vma, pmd_t *pmd,
  489. unsigned long addr, unsigned long end,
  490. struct zap_details *details)
  491. {
  492. struct mm_struct *mm = tlb->mm;
  493. pte_t *pte;
  494. spinlock_t *ptl;
  495. int file_rss = 0;
  496. int anon_rss = 0;
  497. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  498. do {
  499. pte_t ptent = *pte;
  500. if (pte_none(ptent))
  501. continue;
  502. if (pte_present(ptent)) {
  503. struct page *page = NULL;
  504. if (!(vma->vm_flags & VM_RESERVED)) {
  505. unsigned long pfn = pte_pfn(ptent);
  506. if (unlikely(!pfn_valid(pfn)))
  507. print_bad_pte(vma, ptent, addr);
  508. else
  509. page = pfn_to_page(pfn);
  510. }
  511. if (unlikely(details) && page) {
  512. /*
  513. * unmap_shared_mapping_pages() wants to
  514. * invalidate cache without truncating:
  515. * unmap shared but keep private pages.
  516. */
  517. if (details->check_mapping &&
  518. details->check_mapping != page->mapping)
  519. continue;
  520. /*
  521. * Each page->index must be checked when
  522. * invalidating or truncating nonlinear.
  523. */
  524. if (details->nonlinear_vma &&
  525. (page->index < details->first_index ||
  526. page->index > details->last_index))
  527. continue;
  528. }
  529. ptent = ptep_get_and_clear_full(mm, addr, pte,
  530. tlb->fullmm);
  531. tlb_remove_tlb_entry(tlb, pte, addr);
  532. if (unlikely(!page))
  533. continue;
  534. if (unlikely(details) && details->nonlinear_vma
  535. && linear_page_index(details->nonlinear_vma,
  536. addr) != page->index)
  537. set_pte_at(mm, addr, pte,
  538. pgoff_to_pte(page->index));
  539. if (PageAnon(page))
  540. anon_rss--;
  541. else {
  542. if (pte_dirty(ptent))
  543. set_page_dirty(page);
  544. if (pte_young(ptent))
  545. mark_page_accessed(page);
  546. file_rss--;
  547. }
  548. page_remove_rmap(page);
  549. tlb_remove_page(tlb, page);
  550. continue;
  551. }
  552. /*
  553. * If details->check_mapping, we leave swap entries;
  554. * if details->nonlinear_vma, we leave file entries.
  555. */
  556. if (unlikely(details))
  557. continue;
  558. if (!pte_file(ptent))
  559. free_swap_and_cache(pte_to_swp_entry(ptent));
  560. pte_clear_full(mm, addr, pte, tlb->fullmm);
  561. } while (pte++, addr += PAGE_SIZE, addr != end);
  562. add_mm_rss(mm, file_rss, anon_rss);
  563. pte_unmap_unlock(pte - 1, ptl);
  564. }
  565. static inline void zap_pmd_range(struct mmu_gather *tlb,
  566. struct vm_area_struct *vma, pud_t *pud,
  567. unsigned long addr, unsigned long end,
  568. struct zap_details *details)
  569. {
  570. pmd_t *pmd;
  571. unsigned long next;
  572. pmd = pmd_offset(pud, addr);
  573. do {
  574. next = pmd_addr_end(addr, end);
  575. if (pmd_none_or_clear_bad(pmd))
  576. continue;
  577. zap_pte_range(tlb, vma, pmd, addr, next, details);
  578. } while (pmd++, addr = next, addr != end);
  579. }
  580. static inline void zap_pud_range(struct mmu_gather *tlb,
  581. struct vm_area_struct *vma, pgd_t *pgd,
  582. unsigned long addr, unsigned long end,
  583. struct zap_details *details)
  584. {
  585. pud_t *pud;
  586. unsigned long next;
  587. pud = pud_offset(pgd, addr);
  588. do {
  589. next = pud_addr_end(addr, end);
  590. if (pud_none_or_clear_bad(pud))
  591. continue;
  592. zap_pmd_range(tlb, vma, pud, addr, next, details);
  593. } while (pud++, addr = next, addr != end);
  594. }
  595. static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  596. unsigned long addr, unsigned long end,
  597. struct zap_details *details)
  598. {
  599. pgd_t *pgd;
  600. unsigned long next;
  601. if (details && !details->check_mapping && !details->nonlinear_vma)
  602. details = NULL;
  603. BUG_ON(addr >= end);
  604. tlb_start_vma(tlb, vma);
  605. pgd = pgd_offset(vma->vm_mm, addr);
  606. do {
  607. next = pgd_addr_end(addr, end);
  608. if (pgd_none_or_clear_bad(pgd))
  609. continue;
  610. zap_pud_range(tlb, vma, pgd, addr, next, details);
  611. } while (pgd++, addr = next, addr != end);
  612. tlb_end_vma(tlb, vma);
  613. }
  614. #ifdef CONFIG_PREEMPT
  615. # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
  616. #else
  617. /* No preempt: go for improved straight-line efficiency */
  618. # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
  619. #endif
  620. /**
  621. * unmap_vmas - unmap a range of memory covered by a list of vma's
  622. * @tlbp: address of the caller's struct mmu_gather
  623. * @vma: the starting vma
  624. * @start_addr: virtual address at which to start unmapping
  625. * @end_addr: virtual address at which to end unmapping
  626. * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
  627. * @details: details of nonlinear truncation or shared cache invalidation
  628. *
  629. * Returns the end address of the unmapping (restart addr if interrupted).
  630. *
  631. * Unmap all pages in the vma list.
  632. *
  633. * We aim to not hold locks for too long (for scheduling latency reasons).
  634. * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
  635. * return the ending mmu_gather to the caller.
  636. *
  637. * Only addresses between `start' and `end' will be unmapped.
  638. *
  639. * The VMA list must be sorted in ascending virtual address order.
  640. *
  641. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  642. * range after unmap_vmas() returns. So the only responsibility here is to
  643. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  644. * drops the lock and schedules.
  645. */
  646. unsigned long unmap_vmas(struct mmu_gather **tlbp,
  647. struct vm_area_struct *vma, unsigned long start_addr,
  648. unsigned long end_addr, unsigned long *nr_accounted,
  649. struct zap_details *details)
  650. {
  651. unsigned long zap_bytes = ZAP_BLOCK_SIZE;
  652. unsigned long tlb_start = 0; /* For tlb_finish_mmu */
  653. int tlb_start_valid = 0;
  654. unsigned long start = start_addr;
  655. spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
  656. int fullmm = (*tlbp)->fullmm;
  657. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
  658. unsigned long end;
  659. start = max(vma->vm_start, start_addr);
  660. if (start >= vma->vm_end)
  661. continue;
  662. end = min(vma->vm_end, end_addr);
  663. if (end <= vma->vm_start)
  664. continue;
  665. if (vma->vm_flags & VM_ACCOUNT)
  666. *nr_accounted += (end - start) >> PAGE_SHIFT;
  667. while (start != end) {
  668. unsigned long block;
  669. if (!tlb_start_valid) {
  670. tlb_start = start;
  671. tlb_start_valid = 1;
  672. }
  673. if (is_vm_hugetlb_page(vma)) {
  674. block = end - start;
  675. unmap_hugepage_range(vma, start, end);
  676. } else {
  677. block = min(zap_bytes, end - start);
  678. unmap_page_range(*tlbp, vma, start,
  679. start + block, details);
  680. }
  681. start += block;
  682. zap_bytes -= block;
  683. if ((long)zap_bytes > 0)
  684. continue;
  685. tlb_finish_mmu(*tlbp, tlb_start, start);
  686. if (need_resched() ||
  687. (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
  688. if (i_mmap_lock) {
  689. *tlbp = NULL;
  690. goto out;
  691. }
  692. cond_resched();
  693. }
  694. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  695. tlb_start_valid = 0;
  696. zap_bytes = ZAP_BLOCK_SIZE;
  697. }
  698. }
  699. out:
  700. return start; /* which is now the end (or restart) address */
  701. }
  702. /**
  703. * zap_page_range - remove user pages in a given range
  704. * @vma: vm_area_struct holding the applicable pages
  705. * @address: starting address of pages to zap
  706. * @size: number of bytes to zap
  707. * @details: details of nonlinear truncation or shared cache invalidation
  708. */
  709. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  710. unsigned long size, struct zap_details *details)
  711. {
  712. struct mm_struct *mm = vma->vm_mm;
  713. struct mmu_gather *tlb;
  714. unsigned long end = address + size;
  715. unsigned long nr_accounted = 0;
  716. lru_add_drain();
  717. tlb = tlb_gather_mmu(mm, 0);
  718. update_hiwater_rss(mm);
  719. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  720. if (tlb)
  721. tlb_finish_mmu(tlb, address, end);
  722. return end;
  723. }
  724. /*
  725. * Do a quick page-table lookup for a single page.
  726. * mm->page_table_lock must be held.
  727. */
  728. struct page *follow_page(struct mm_struct *mm, unsigned long address, int write)
  729. {
  730. pgd_t *pgd;
  731. pud_t *pud;
  732. pmd_t *pmd;
  733. pte_t *ptep, pte;
  734. unsigned long pfn;
  735. struct page *page;
  736. page = follow_huge_addr(mm, address, write);
  737. if (! IS_ERR(page))
  738. return page;
  739. pgd = pgd_offset(mm, address);
  740. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  741. goto out;
  742. pud = pud_offset(pgd, address);
  743. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  744. goto out;
  745. pmd = pmd_offset(pud, address);
  746. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  747. goto out;
  748. if (pmd_huge(*pmd))
  749. return follow_huge_pmd(mm, address, pmd, write);
  750. ptep = pte_offset_map(pmd, address);
  751. if (!ptep)
  752. goto out;
  753. pte = *ptep;
  754. pte_unmap(ptep);
  755. if (pte_present(pte)) {
  756. if (write && !pte_write(pte))
  757. goto out;
  758. pfn = pte_pfn(pte);
  759. if (pfn_valid(pfn)) {
  760. page = pfn_to_page(pfn);
  761. if (write && !pte_dirty(pte) &&!PageDirty(page))
  762. set_page_dirty(page);
  763. mark_page_accessed(page);
  764. return page;
  765. }
  766. }
  767. out:
  768. return NULL;
  769. }
  770. static inline int
  771. untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
  772. unsigned long address)
  773. {
  774. pgd_t *pgd;
  775. pud_t *pud;
  776. pmd_t *pmd;
  777. /* Check if the vma is for an anonymous mapping. */
  778. if (vma->vm_ops && vma->vm_ops->nopage)
  779. return 0;
  780. /* Check if page directory entry exists. */
  781. pgd = pgd_offset(mm, address);
  782. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  783. return 1;
  784. pud = pud_offset(pgd, address);
  785. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  786. return 1;
  787. /* Check if page middle directory entry exists. */
  788. pmd = pmd_offset(pud, address);
  789. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  790. return 1;
  791. /* There is a pte slot for 'address' in 'mm'. */
  792. return 0;
  793. }
  794. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  795. unsigned long start, int len, int write, int force,
  796. struct page **pages, struct vm_area_struct **vmas)
  797. {
  798. int i;
  799. unsigned int flags;
  800. /*
  801. * Require read or write permissions.
  802. * If 'force' is set, we only require the "MAY" flags.
  803. */
  804. flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  805. flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  806. i = 0;
  807. do {
  808. struct vm_area_struct * vma;
  809. vma = find_extend_vma(mm, start);
  810. if (!vma && in_gate_area(tsk, start)) {
  811. unsigned long pg = start & PAGE_MASK;
  812. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  813. pgd_t *pgd;
  814. pud_t *pud;
  815. pmd_t *pmd;
  816. pte_t *pte;
  817. if (write) /* user gate pages are read-only */
  818. return i ? : -EFAULT;
  819. if (pg > TASK_SIZE)
  820. pgd = pgd_offset_k(pg);
  821. else
  822. pgd = pgd_offset_gate(mm, pg);
  823. BUG_ON(pgd_none(*pgd));
  824. pud = pud_offset(pgd, pg);
  825. BUG_ON(pud_none(*pud));
  826. pmd = pmd_offset(pud, pg);
  827. if (pmd_none(*pmd))
  828. return i ? : -EFAULT;
  829. pte = pte_offset_map(pmd, pg);
  830. if (pte_none(*pte)) {
  831. pte_unmap(pte);
  832. return i ? : -EFAULT;
  833. }
  834. if (pages) {
  835. pages[i] = pte_page(*pte);
  836. get_page(pages[i]);
  837. }
  838. pte_unmap(pte);
  839. if (vmas)
  840. vmas[i] = gate_vma;
  841. i++;
  842. start += PAGE_SIZE;
  843. len--;
  844. continue;
  845. }
  846. if (!vma || (vma->vm_flags & (VM_IO | VM_RESERVED))
  847. || !(flags & vma->vm_flags))
  848. return i ? : -EFAULT;
  849. if (is_vm_hugetlb_page(vma)) {
  850. i = follow_hugetlb_page(mm, vma, pages, vmas,
  851. &start, &len, i);
  852. continue;
  853. }
  854. spin_lock(&mm->page_table_lock);
  855. do {
  856. int write_access = write;
  857. struct page *page;
  858. cond_resched_lock(&mm->page_table_lock);
  859. while (!(page = follow_page(mm, start, write_access))) {
  860. int ret;
  861. /*
  862. * Shortcut for anonymous pages. We don't want
  863. * to force the creation of pages tables for
  864. * insanely big anonymously mapped areas that
  865. * nobody touched so far. This is important
  866. * for doing a core dump for these mappings.
  867. */
  868. if (!write && untouched_anonymous_page(mm,vma,start)) {
  869. page = ZERO_PAGE(start);
  870. break;
  871. }
  872. spin_unlock(&mm->page_table_lock);
  873. ret = __handle_mm_fault(mm, vma, start, write_access);
  874. /*
  875. * The VM_FAULT_WRITE bit tells us that do_wp_page has
  876. * broken COW when necessary, even if maybe_mkwrite
  877. * decided not to set pte_write. We can thus safely do
  878. * subsequent page lookups as if they were reads.
  879. */
  880. if (ret & VM_FAULT_WRITE)
  881. write_access = 0;
  882. switch (ret & ~VM_FAULT_WRITE) {
  883. case VM_FAULT_MINOR:
  884. tsk->min_flt++;
  885. break;
  886. case VM_FAULT_MAJOR:
  887. tsk->maj_flt++;
  888. break;
  889. case VM_FAULT_SIGBUS:
  890. return i ? i : -EFAULT;
  891. case VM_FAULT_OOM:
  892. return i ? i : -ENOMEM;
  893. default:
  894. BUG();
  895. }
  896. spin_lock(&mm->page_table_lock);
  897. }
  898. if (pages) {
  899. pages[i] = page;
  900. flush_dcache_page(page);
  901. page_cache_get(page);
  902. }
  903. if (vmas)
  904. vmas[i] = vma;
  905. i++;
  906. start += PAGE_SIZE;
  907. len--;
  908. } while (len && start < vma->vm_end);
  909. spin_unlock(&mm->page_table_lock);
  910. } while (len);
  911. return i;
  912. }
  913. EXPORT_SYMBOL(get_user_pages);
  914. static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  915. unsigned long addr, unsigned long end, pgprot_t prot)
  916. {
  917. pte_t *pte;
  918. spinlock_t *ptl;
  919. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  920. if (!pte)
  921. return -ENOMEM;
  922. do {
  923. struct page *page = ZERO_PAGE(addr);
  924. pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
  925. page_cache_get(page);
  926. page_add_file_rmap(page);
  927. inc_mm_counter(mm, file_rss);
  928. BUG_ON(!pte_none(*pte));
  929. set_pte_at(mm, addr, pte, zero_pte);
  930. } while (pte++, addr += PAGE_SIZE, addr != end);
  931. pte_unmap_unlock(pte - 1, ptl);
  932. return 0;
  933. }
  934. static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
  935. unsigned long addr, unsigned long end, pgprot_t prot)
  936. {
  937. pmd_t *pmd;
  938. unsigned long next;
  939. pmd = pmd_alloc(mm, pud, addr);
  940. if (!pmd)
  941. return -ENOMEM;
  942. do {
  943. next = pmd_addr_end(addr, end);
  944. if (zeromap_pte_range(mm, pmd, addr, next, prot))
  945. return -ENOMEM;
  946. } while (pmd++, addr = next, addr != end);
  947. return 0;
  948. }
  949. static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  950. unsigned long addr, unsigned long end, pgprot_t prot)
  951. {
  952. pud_t *pud;
  953. unsigned long next;
  954. pud = pud_alloc(mm, pgd, addr);
  955. if (!pud)
  956. return -ENOMEM;
  957. do {
  958. next = pud_addr_end(addr, end);
  959. if (zeromap_pmd_range(mm, pud, addr, next, prot))
  960. return -ENOMEM;
  961. } while (pud++, addr = next, addr != end);
  962. return 0;
  963. }
  964. int zeromap_page_range(struct vm_area_struct *vma,
  965. unsigned long addr, unsigned long size, pgprot_t prot)
  966. {
  967. pgd_t *pgd;
  968. unsigned long next;
  969. unsigned long end = addr + size;
  970. struct mm_struct *mm = vma->vm_mm;
  971. int err;
  972. BUG_ON(addr >= end);
  973. pgd = pgd_offset(mm, addr);
  974. flush_cache_range(vma, addr, end);
  975. do {
  976. next = pgd_addr_end(addr, end);
  977. err = zeromap_pud_range(mm, pgd, addr, next, prot);
  978. if (err)
  979. break;
  980. } while (pgd++, addr = next, addr != end);
  981. return err;
  982. }
  983. /*
  984. * maps a range of physical memory into the requested pages. the old
  985. * mappings are removed. any references to nonexistent pages results
  986. * in null mappings (currently treated as "copy-on-access")
  987. */
  988. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  989. unsigned long addr, unsigned long end,
  990. unsigned long pfn, pgprot_t prot)
  991. {
  992. pte_t *pte;
  993. spinlock_t *ptl;
  994. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  995. if (!pte)
  996. return -ENOMEM;
  997. do {
  998. BUG_ON(!pte_none(*pte));
  999. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1000. pfn++;
  1001. } while (pte++, addr += PAGE_SIZE, addr != end);
  1002. pte_unmap_unlock(pte - 1, ptl);
  1003. return 0;
  1004. }
  1005. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1006. unsigned long addr, unsigned long end,
  1007. unsigned long pfn, pgprot_t prot)
  1008. {
  1009. pmd_t *pmd;
  1010. unsigned long next;
  1011. pfn -= addr >> PAGE_SHIFT;
  1012. pmd = pmd_alloc(mm, pud, addr);
  1013. if (!pmd)
  1014. return -ENOMEM;
  1015. do {
  1016. next = pmd_addr_end(addr, end);
  1017. if (remap_pte_range(mm, pmd, addr, next,
  1018. pfn + (addr >> PAGE_SHIFT), prot))
  1019. return -ENOMEM;
  1020. } while (pmd++, addr = next, addr != end);
  1021. return 0;
  1022. }
  1023. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1024. unsigned long addr, unsigned long end,
  1025. unsigned long pfn, pgprot_t prot)
  1026. {
  1027. pud_t *pud;
  1028. unsigned long next;
  1029. pfn -= addr >> PAGE_SHIFT;
  1030. pud = pud_alloc(mm, pgd, addr);
  1031. if (!pud)
  1032. return -ENOMEM;
  1033. do {
  1034. next = pud_addr_end(addr, end);
  1035. if (remap_pmd_range(mm, pud, addr, next,
  1036. pfn + (addr >> PAGE_SHIFT), prot))
  1037. return -ENOMEM;
  1038. } while (pud++, addr = next, addr != end);
  1039. return 0;
  1040. }
  1041. /* Note: this is only safe if the mm semaphore is held when called. */
  1042. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1043. unsigned long pfn, unsigned long size, pgprot_t prot)
  1044. {
  1045. pgd_t *pgd;
  1046. unsigned long next;
  1047. unsigned long end = addr + PAGE_ALIGN(size);
  1048. struct mm_struct *mm = vma->vm_mm;
  1049. int err;
  1050. /*
  1051. * Physically remapped pages are special. Tell the
  1052. * rest of the world about it:
  1053. * VM_IO tells people not to look at these pages
  1054. * (accesses can have side effects).
  1055. * VM_RESERVED tells the core MM not to "manage" these pages
  1056. * (e.g. refcount, mapcount, try to swap them out).
  1057. */
  1058. vma->vm_flags |= VM_IO | VM_RESERVED;
  1059. BUG_ON(addr >= end);
  1060. pfn -= addr >> PAGE_SHIFT;
  1061. pgd = pgd_offset(mm, addr);
  1062. flush_cache_range(vma, addr, end);
  1063. do {
  1064. next = pgd_addr_end(addr, end);
  1065. err = remap_pud_range(mm, pgd, addr, next,
  1066. pfn + (addr >> PAGE_SHIFT), prot);
  1067. if (err)
  1068. break;
  1069. } while (pgd++, addr = next, addr != end);
  1070. return err;
  1071. }
  1072. EXPORT_SYMBOL(remap_pfn_range);
  1073. /*
  1074. * handle_pte_fault chooses page fault handler according to an entry
  1075. * which was read non-atomically. Before making any commitment, on
  1076. * those architectures or configurations (e.g. i386 with PAE) which
  1077. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1078. * must check under lock before unmapping the pte and proceeding
  1079. * (but do_wp_page is only called after already making such a check;
  1080. * and do_anonymous_page and do_no_page can safely check later on).
  1081. */
  1082. static inline int pte_unmap_same(struct mm_struct *mm,
  1083. pte_t *page_table, pte_t orig_pte)
  1084. {
  1085. int same = 1;
  1086. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1087. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1088. spin_lock(&mm->page_table_lock);
  1089. same = pte_same(*page_table, orig_pte);
  1090. spin_unlock(&mm->page_table_lock);
  1091. }
  1092. #endif
  1093. pte_unmap(page_table);
  1094. return same;
  1095. }
  1096. /*
  1097. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1098. * servicing faults for write access. In the normal case, do always want
  1099. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1100. * that do not have writing enabled, when used by access_process_vm.
  1101. */
  1102. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1103. {
  1104. if (likely(vma->vm_flags & VM_WRITE))
  1105. pte = pte_mkwrite(pte);
  1106. return pte;
  1107. }
  1108. /*
  1109. * This routine handles present pages, when users try to write
  1110. * to a shared page. It is done by copying the page to a new address
  1111. * and decrementing the shared-page counter for the old page.
  1112. *
  1113. * Note that this routine assumes that the protection checks have been
  1114. * done by the caller (the low-level page fault routine in most cases).
  1115. * Thus we can safely just mark it writable once we've done any necessary
  1116. * COW.
  1117. *
  1118. * We also mark the page dirty at this point even though the page will
  1119. * change only once the write actually happens. This avoids a few races,
  1120. * and potentially makes it more efficient.
  1121. *
  1122. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1123. * but allow concurrent faults), with pte both mapped and locked.
  1124. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1125. */
  1126. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1127. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1128. spinlock_t *ptl, pte_t orig_pte)
  1129. {
  1130. struct page *old_page, *new_page;
  1131. unsigned long pfn = pte_pfn(orig_pte);
  1132. pte_t entry;
  1133. int ret = VM_FAULT_MINOR;
  1134. BUG_ON(vma->vm_flags & VM_RESERVED);
  1135. if (unlikely(!pfn_valid(pfn))) {
  1136. /*
  1137. * Page table corrupted: show pte and kill process.
  1138. */
  1139. print_bad_pte(vma, orig_pte, address);
  1140. ret = VM_FAULT_OOM;
  1141. goto unlock;
  1142. }
  1143. old_page = pfn_to_page(pfn);
  1144. if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
  1145. int reuse = can_share_swap_page(old_page);
  1146. unlock_page(old_page);
  1147. if (reuse) {
  1148. flush_cache_page(vma, address, pfn);
  1149. entry = pte_mkyoung(orig_pte);
  1150. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1151. ptep_set_access_flags(vma, address, page_table, entry, 1);
  1152. update_mmu_cache(vma, address, entry);
  1153. lazy_mmu_prot_update(entry);
  1154. ret |= VM_FAULT_WRITE;
  1155. goto unlock;
  1156. }
  1157. }
  1158. /*
  1159. * Ok, we need to copy. Oh, well..
  1160. */
  1161. page_cache_get(old_page);
  1162. pte_unmap_unlock(page_table, ptl);
  1163. if (unlikely(anon_vma_prepare(vma)))
  1164. goto oom;
  1165. if (old_page == ZERO_PAGE(address)) {
  1166. new_page = alloc_zeroed_user_highpage(vma, address);
  1167. if (!new_page)
  1168. goto oom;
  1169. } else {
  1170. new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1171. if (!new_page)
  1172. goto oom;
  1173. copy_user_highpage(new_page, old_page, address);
  1174. }
  1175. /*
  1176. * Re-check the pte - we dropped the lock
  1177. */
  1178. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1179. if (likely(pte_same(*page_table, orig_pte))) {
  1180. page_remove_rmap(old_page);
  1181. if (!PageAnon(old_page)) {
  1182. inc_mm_counter(mm, anon_rss);
  1183. dec_mm_counter(mm, file_rss);
  1184. }
  1185. flush_cache_page(vma, address, pfn);
  1186. entry = mk_pte(new_page, vma->vm_page_prot);
  1187. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1188. ptep_establish(vma, address, page_table, entry);
  1189. update_mmu_cache(vma, address, entry);
  1190. lazy_mmu_prot_update(entry);
  1191. lru_cache_add_active(new_page);
  1192. page_add_anon_rmap(new_page, vma, address);
  1193. /* Free the old page.. */
  1194. new_page = old_page;
  1195. ret |= VM_FAULT_WRITE;
  1196. }
  1197. page_cache_release(new_page);
  1198. page_cache_release(old_page);
  1199. unlock:
  1200. pte_unmap_unlock(page_table, ptl);
  1201. return ret;
  1202. oom:
  1203. page_cache_release(old_page);
  1204. return VM_FAULT_OOM;
  1205. }
  1206. /*
  1207. * Helper functions for unmap_mapping_range().
  1208. *
  1209. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1210. *
  1211. * We have to restart searching the prio_tree whenever we drop the lock,
  1212. * since the iterator is only valid while the lock is held, and anyway
  1213. * a later vma might be split and reinserted earlier while lock dropped.
  1214. *
  1215. * The list of nonlinear vmas could be handled more efficiently, using
  1216. * a placeholder, but handle it in the same way until a need is shown.
  1217. * It is important to search the prio_tree before nonlinear list: a vma
  1218. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1219. * while the lock is dropped; but never shifted from list to prio_tree.
  1220. *
  1221. * In order to make forward progress despite restarting the search,
  1222. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1223. * quickly skip it next time around. Since the prio_tree search only
  1224. * shows us those vmas affected by unmapping the range in question, we
  1225. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1226. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1227. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1228. * i_mmap_lock.
  1229. *
  1230. * In order to make forward progress despite repeatedly restarting some
  1231. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1232. * and restart from that address when we reach that vma again. It might
  1233. * have been split or merged, shrunk or extended, but never shifted: so
  1234. * restart_addr remains valid so long as it remains in the vma's range.
  1235. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1236. * values so we can save vma's restart_addr in its truncate_count field.
  1237. */
  1238. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1239. static void reset_vma_truncate_counts(struct address_space *mapping)
  1240. {
  1241. struct vm_area_struct *vma;
  1242. struct prio_tree_iter iter;
  1243. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1244. vma->vm_truncate_count = 0;
  1245. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1246. vma->vm_truncate_count = 0;
  1247. }
  1248. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1249. unsigned long start_addr, unsigned long end_addr,
  1250. struct zap_details *details)
  1251. {
  1252. unsigned long restart_addr;
  1253. int need_break;
  1254. again:
  1255. restart_addr = vma->vm_truncate_count;
  1256. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1257. start_addr = restart_addr;
  1258. if (start_addr >= end_addr) {
  1259. /* Top of vma has been split off since last time */
  1260. vma->vm_truncate_count = details->truncate_count;
  1261. return 0;
  1262. }
  1263. }
  1264. restart_addr = zap_page_range(vma, start_addr,
  1265. end_addr - start_addr, details);
  1266. need_break = need_resched() ||
  1267. need_lockbreak(details->i_mmap_lock);
  1268. if (restart_addr >= end_addr) {
  1269. /* We have now completed this vma: mark it so */
  1270. vma->vm_truncate_count = details->truncate_count;
  1271. if (!need_break)
  1272. return 0;
  1273. } else {
  1274. /* Note restart_addr in vma's truncate_count field */
  1275. vma->vm_truncate_count = restart_addr;
  1276. if (!need_break)
  1277. goto again;
  1278. }
  1279. spin_unlock(details->i_mmap_lock);
  1280. cond_resched();
  1281. spin_lock(details->i_mmap_lock);
  1282. return -EINTR;
  1283. }
  1284. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1285. struct zap_details *details)
  1286. {
  1287. struct vm_area_struct *vma;
  1288. struct prio_tree_iter iter;
  1289. pgoff_t vba, vea, zba, zea;
  1290. restart:
  1291. vma_prio_tree_foreach(vma, &iter, root,
  1292. details->first_index, details->last_index) {
  1293. /* Skip quickly over those we have already dealt with */
  1294. if (vma->vm_truncate_count == details->truncate_count)
  1295. continue;
  1296. vba = vma->vm_pgoff;
  1297. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1298. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1299. zba = details->first_index;
  1300. if (zba < vba)
  1301. zba = vba;
  1302. zea = details->last_index;
  1303. if (zea > vea)
  1304. zea = vea;
  1305. if (unmap_mapping_range_vma(vma,
  1306. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1307. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1308. details) < 0)
  1309. goto restart;
  1310. }
  1311. }
  1312. static inline void unmap_mapping_range_list(struct list_head *head,
  1313. struct zap_details *details)
  1314. {
  1315. struct vm_area_struct *vma;
  1316. /*
  1317. * In nonlinear VMAs there is no correspondence between virtual address
  1318. * offset and file offset. So we must perform an exhaustive search
  1319. * across *all* the pages in each nonlinear VMA, not just the pages
  1320. * whose virtual address lies outside the file truncation point.
  1321. */
  1322. restart:
  1323. list_for_each_entry(vma, head, shared.vm_set.list) {
  1324. /* Skip quickly over those we have already dealt with */
  1325. if (vma->vm_truncate_count == details->truncate_count)
  1326. continue;
  1327. details->nonlinear_vma = vma;
  1328. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1329. vma->vm_end, details) < 0)
  1330. goto restart;
  1331. }
  1332. }
  1333. /**
  1334. * unmap_mapping_range - unmap the portion of all mmaps
  1335. * in the specified address_space corresponding to the specified
  1336. * page range in the underlying file.
  1337. * @mapping: the address space containing mmaps to be unmapped.
  1338. * @holebegin: byte in first page to unmap, relative to the start of
  1339. * the underlying file. This will be rounded down to a PAGE_SIZE
  1340. * boundary. Note that this is different from vmtruncate(), which
  1341. * must keep the partial page. In contrast, we must get rid of
  1342. * partial pages.
  1343. * @holelen: size of prospective hole in bytes. This will be rounded
  1344. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1345. * end of the file.
  1346. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1347. * but 0 when invalidating pagecache, don't throw away private data.
  1348. */
  1349. void unmap_mapping_range(struct address_space *mapping,
  1350. loff_t const holebegin, loff_t const holelen, int even_cows)
  1351. {
  1352. struct zap_details details;
  1353. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1354. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1355. /* Check for overflow. */
  1356. if (sizeof(holelen) > sizeof(hlen)) {
  1357. long long holeend =
  1358. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1359. if (holeend & ~(long long)ULONG_MAX)
  1360. hlen = ULONG_MAX - hba + 1;
  1361. }
  1362. details.check_mapping = even_cows? NULL: mapping;
  1363. details.nonlinear_vma = NULL;
  1364. details.first_index = hba;
  1365. details.last_index = hba + hlen - 1;
  1366. if (details.last_index < details.first_index)
  1367. details.last_index = ULONG_MAX;
  1368. details.i_mmap_lock = &mapping->i_mmap_lock;
  1369. spin_lock(&mapping->i_mmap_lock);
  1370. /* serialize i_size write against truncate_count write */
  1371. smp_wmb();
  1372. /* Protect against page faults, and endless unmapping loops */
  1373. mapping->truncate_count++;
  1374. /*
  1375. * For archs where spin_lock has inclusive semantics like ia64
  1376. * this smp_mb() will prevent to read pagetable contents
  1377. * before the truncate_count increment is visible to
  1378. * other cpus.
  1379. */
  1380. smp_mb();
  1381. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1382. if (mapping->truncate_count == 0)
  1383. reset_vma_truncate_counts(mapping);
  1384. mapping->truncate_count++;
  1385. }
  1386. details.truncate_count = mapping->truncate_count;
  1387. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1388. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1389. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1390. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1391. spin_unlock(&mapping->i_mmap_lock);
  1392. }
  1393. EXPORT_SYMBOL(unmap_mapping_range);
  1394. /*
  1395. * Handle all mappings that got truncated by a "truncate()"
  1396. * system call.
  1397. *
  1398. * NOTE! We have to be ready to update the memory sharing
  1399. * between the file and the memory map for a potential last
  1400. * incomplete page. Ugly, but necessary.
  1401. */
  1402. int vmtruncate(struct inode * inode, loff_t offset)
  1403. {
  1404. struct address_space *mapping = inode->i_mapping;
  1405. unsigned long limit;
  1406. if (inode->i_size < offset)
  1407. goto do_expand;
  1408. /*
  1409. * truncation of in-use swapfiles is disallowed - it would cause
  1410. * subsequent swapout to scribble on the now-freed blocks.
  1411. */
  1412. if (IS_SWAPFILE(inode))
  1413. goto out_busy;
  1414. i_size_write(inode, offset);
  1415. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1416. truncate_inode_pages(mapping, offset);
  1417. goto out_truncate;
  1418. do_expand:
  1419. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1420. if (limit != RLIM_INFINITY && offset > limit)
  1421. goto out_sig;
  1422. if (offset > inode->i_sb->s_maxbytes)
  1423. goto out_big;
  1424. i_size_write(inode, offset);
  1425. out_truncate:
  1426. if (inode->i_op && inode->i_op->truncate)
  1427. inode->i_op->truncate(inode);
  1428. return 0;
  1429. out_sig:
  1430. send_sig(SIGXFSZ, current, 0);
  1431. out_big:
  1432. return -EFBIG;
  1433. out_busy:
  1434. return -ETXTBSY;
  1435. }
  1436. EXPORT_SYMBOL(vmtruncate);
  1437. /*
  1438. * Primitive swap readahead code. We simply read an aligned block of
  1439. * (1 << page_cluster) entries in the swap area. This method is chosen
  1440. * because it doesn't cost us any seek time. We also make sure to queue
  1441. * the 'original' request together with the readahead ones...
  1442. *
  1443. * This has been extended to use the NUMA policies from the mm triggering
  1444. * the readahead.
  1445. *
  1446. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1447. */
  1448. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1449. {
  1450. #ifdef CONFIG_NUMA
  1451. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1452. #endif
  1453. int i, num;
  1454. struct page *new_page;
  1455. unsigned long offset;
  1456. /*
  1457. * Get the number of handles we should do readahead io to.
  1458. */
  1459. num = valid_swaphandles(entry, &offset);
  1460. for (i = 0; i < num; offset++, i++) {
  1461. /* Ok, do the async read-ahead now */
  1462. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1463. offset), vma, addr);
  1464. if (!new_page)
  1465. break;
  1466. page_cache_release(new_page);
  1467. #ifdef CONFIG_NUMA
  1468. /*
  1469. * Find the next applicable VMA for the NUMA policy.
  1470. */
  1471. addr += PAGE_SIZE;
  1472. if (addr == 0)
  1473. vma = NULL;
  1474. if (vma) {
  1475. if (addr >= vma->vm_end) {
  1476. vma = next_vma;
  1477. next_vma = vma ? vma->vm_next : NULL;
  1478. }
  1479. if (vma && addr < vma->vm_start)
  1480. vma = NULL;
  1481. } else {
  1482. if (next_vma && addr >= next_vma->vm_start) {
  1483. vma = next_vma;
  1484. next_vma = vma->vm_next;
  1485. }
  1486. }
  1487. #endif
  1488. }
  1489. lru_add_drain(); /* Push any new pages onto the LRU now */
  1490. }
  1491. /*
  1492. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1493. * but allow concurrent faults), and pte mapped but not yet locked.
  1494. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1495. */
  1496. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1497. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1498. int write_access, pte_t orig_pte)
  1499. {
  1500. spinlock_t *ptl;
  1501. struct page *page;
  1502. swp_entry_t entry;
  1503. pte_t pte;
  1504. int ret = VM_FAULT_MINOR;
  1505. if (!pte_unmap_same(mm, page_table, orig_pte))
  1506. goto out;
  1507. entry = pte_to_swp_entry(orig_pte);
  1508. page = lookup_swap_cache(entry);
  1509. if (!page) {
  1510. swapin_readahead(entry, address, vma);
  1511. page = read_swap_cache_async(entry, vma, address);
  1512. if (!page) {
  1513. /*
  1514. * Back out if somebody else faulted in this pte
  1515. * while we released the pte lock.
  1516. */
  1517. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1518. if (likely(pte_same(*page_table, orig_pte)))
  1519. ret = VM_FAULT_OOM;
  1520. goto unlock;
  1521. }
  1522. /* Had to read the page from swap area: Major fault */
  1523. ret = VM_FAULT_MAJOR;
  1524. inc_page_state(pgmajfault);
  1525. grab_swap_token();
  1526. }
  1527. mark_page_accessed(page);
  1528. lock_page(page);
  1529. /*
  1530. * Back out if somebody else already faulted in this pte.
  1531. */
  1532. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1533. if (unlikely(!pte_same(*page_table, orig_pte)))
  1534. goto out_nomap;
  1535. if (unlikely(!PageUptodate(page))) {
  1536. ret = VM_FAULT_SIGBUS;
  1537. goto out_nomap;
  1538. }
  1539. /* The page isn't present yet, go ahead with the fault. */
  1540. inc_mm_counter(mm, anon_rss);
  1541. pte = mk_pte(page, vma->vm_page_prot);
  1542. if (write_access && can_share_swap_page(page)) {
  1543. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1544. write_access = 0;
  1545. }
  1546. flush_icache_page(vma, page);
  1547. set_pte_at(mm, address, page_table, pte);
  1548. page_add_anon_rmap(page, vma, address);
  1549. swap_free(entry);
  1550. if (vm_swap_full())
  1551. remove_exclusive_swap_page(page);
  1552. unlock_page(page);
  1553. if (write_access) {
  1554. if (do_wp_page(mm, vma, address,
  1555. page_table, pmd, ptl, pte) == VM_FAULT_OOM)
  1556. ret = VM_FAULT_OOM;
  1557. goto out;
  1558. }
  1559. /* No need to invalidate - it was non-present before */
  1560. update_mmu_cache(vma, address, pte);
  1561. lazy_mmu_prot_update(pte);
  1562. unlock:
  1563. pte_unmap_unlock(page_table, ptl);
  1564. out:
  1565. return ret;
  1566. out_nomap:
  1567. pte_unmap_unlock(page_table, ptl);
  1568. unlock_page(page);
  1569. page_cache_release(page);
  1570. return ret;
  1571. }
  1572. /*
  1573. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1574. * but allow concurrent faults), and pte mapped but not yet locked.
  1575. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1576. */
  1577. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1578. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1579. int write_access)
  1580. {
  1581. struct page *page;
  1582. spinlock_t *ptl;
  1583. pte_t entry;
  1584. if (write_access) {
  1585. /* Allocate our own private page. */
  1586. pte_unmap(page_table);
  1587. if (unlikely(anon_vma_prepare(vma)))
  1588. goto oom;
  1589. page = alloc_zeroed_user_highpage(vma, address);
  1590. if (!page)
  1591. goto oom;
  1592. entry = mk_pte(page, vma->vm_page_prot);
  1593. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1594. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1595. if (!pte_none(*page_table))
  1596. goto release;
  1597. inc_mm_counter(mm, anon_rss);
  1598. lru_cache_add_active(page);
  1599. SetPageReferenced(page);
  1600. page_add_anon_rmap(page, vma, address);
  1601. } else {
  1602. /* Map the ZERO_PAGE - vm_page_prot is readonly */
  1603. page = ZERO_PAGE(address);
  1604. page_cache_get(page);
  1605. entry = mk_pte(page, vma->vm_page_prot);
  1606. ptl = &mm->page_table_lock;
  1607. spin_lock(ptl);
  1608. if (!pte_none(*page_table))
  1609. goto release;
  1610. inc_mm_counter(mm, file_rss);
  1611. page_add_file_rmap(page);
  1612. }
  1613. set_pte_at(mm, address, page_table, entry);
  1614. /* No need to invalidate - it was non-present before */
  1615. update_mmu_cache(vma, address, entry);
  1616. lazy_mmu_prot_update(entry);
  1617. unlock:
  1618. pte_unmap_unlock(page_table, ptl);
  1619. return VM_FAULT_MINOR;
  1620. release:
  1621. page_cache_release(page);
  1622. goto unlock;
  1623. oom:
  1624. return VM_FAULT_OOM;
  1625. }
  1626. /*
  1627. * do_no_page() tries to create a new page mapping. It aggressively
  1628. * tries to share with existing pages, but makes a separate copy if
  1629. * the "write_access" parameter is true in order to avoid the next
  1630. * page fault.
  1631. *
  1632. * As this is called only for pages that do not currently exist, we
  1633. * do not need to flush old virtual caches or the TLB.
  1634. *
  1635. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1636. * but allow concurrent faults), and pte mapped but not yet locked.
  1637. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1638. */
  1639. static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1640. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1641. int write_access)
  1642. {
  1643. spinlock_t *ptl;
  1644. struct page *new_page;
  1645. struct address_space *mapping = NULL;
  1646. pte_t entry;
  1647. unsigned int sequence = 0;
  1648. int ret = VM_FAULT_MINOR;
  1649. int anon = 0;
  1650. pte_unmap(page_table);
  1651. if (vma->vm_file) {
  1652. mapping = vma->vm_file->f_mapping;
  1653. sequence = mapping->truncate_count;
  1654. smp_rmb(); /* serializes i_size against truncate_count */
  1655. }
  1656. retry:
  1657. new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
  1658. /*
  1659. * No smp_rmb is needed here as long as there's a full
  1660. * spin_lock/unlock sequence inside the ->nopage callback
  1661. * (for the pagecache lookup) that acts as an implicit
  1662. * smp_mb() and prevents the i_size read to happen
  1663. * after the next truncate_count read.
  1664. */
  1665. /* no page was available -- either SIGBUS or OOM */
  1666. if (new_page == NOPAGE_SIGBUS)
  1667. return VM_FAULT_SIGBUS;
  1668. if (new_page == NOPAGE_OOM)
  1669. return VM_FAULT_OOM;
  1670. /*
  1671. * Should we do an early C-O-W break?
  1672. */
  1673. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1674. struct page *page;
  1675. if (unlikely(anon_vma_prepare(vma)))
  1676. goto oom;
  1677. page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1678. if (!page)
  1679. goto oom;
  1680. copy_user_highpage(page, new_page, address);
  1681. page_cache_release(new_page);
  1682. new_page = page;
  1683. anon = 1;
  1684. }
  1685. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1686. /*
  1687. * For a file-backed vma, someone could have truncated or otherwise
  1688. * invalidated this page. If unmap_mapping_range got called,
  1689. * retry getting the page.
  1690. */
  1691. if (mapping && unlikely(sequence != mapping->truncate_count)) {
  1692. pte_unmap_unlock(page_table, ptl);
  1693. page_cache_release(new_page);
  1694. cond_resched();
  1695. sequence = mapping->truncate_count;
  1696. smp_rmb();
  1697. goto retry;
  1698. }
  1699. /*
  1700. * This silly early PAGE_DIRTY setting removes a race
  1701. * due to the bad i386 page protection. But it's valid
  1702. * for other architectures too.
  1703. *
  1704. * Note that if write_access is true, we either now have
  1705. * an exclusive copy of the page, or this is a shared mapping,
  1706. * so we can make it writable and dirty to avoid having to
  1707. * handle that later.
  1708. */
  1709. /* Only go through if we didn't race with anybody else... */
  1710. if (pte_none(*page_table)) {
  1711. flush_icache_page(vma, new_page);
  1712. entry = mk_pte(new_page, vma->vm_page_prot);
  1713. if (write_access)
  1714. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1715. set_pte_at(mm, address, page_table, entry);
  1716. if (anon) {
  1717. inc_mm_counter(mm, anon_rss);
  1718. lru_cache_add_active(new_page);
  1719. page_add_anon_rmap(new_page, vma, address);
  1720. } else if (!(vma->vm_flags & VM_RESERVED)) {
  1721. inc_mm_counter(mm, file_rss);
  1722. page_add_file_rmap(new_page);
  1723. }
  1724. } else {
  1725. /* One of our sibling threads was faster, back out. */
  1726. page_cache_release(new_page);
  1727. goto unlock;
  1728. }
  1729. /* no need to invalidate: a not-present page shouldn't be cached */
  1730. update_mmu_cache(vma, address, entry);
  1731. lazy_mmu_prot_update(entry);
  1732. unlock:
  1733. pte_unmap_unlock(page_table, ptl);
  1734. return ret;
  1735. oom:
  1736. page_cache_release(new_page);
  1737. return VM_FAULT_OOM;
  1738. }
  1739. /*
  1740. * Fault of a previously existing named mapping. Repopulate the pte
  1741. * from the encoded file_pte if possible. This enables swappable
  1742. * nonlinear vmas.
  1743. *
  1744. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1745. * but allow concurrent faults), and pte mapped but not yet locked.
  1746. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1747. */
  1748. static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1749. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1750. int write_access, pte_t orig_pte)
  1751. {
  1752. pgoff_t pgoff;
  1753. int err;
  1754. if (!pte_unmap_same(mm, page_table, orig_pte))
  1755. return VM_FAULT_MINOR;
  1756. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  1757. /*
  1758. * Page table corrupted: show pte and kill process.
  1759. */
  1760. print_bad_pte(vma, orig_pte, address);
  1761. return VM_FAULT_OOM;
  1762. }
  1763. /* We can then assume vm->vm_ops && vma->vm_ops->populate */
  1764. pgoff = pte_to_pgoff(orig_pte);
  1765. err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
  1766. vma->vm_page_prot, pgoff, 0);
  1767. if (err == -ENOMEM)
  1768. return VM_FAULT_OOM;
  1769. if (err)
  1770. return VM_FAULT_SIGBUS;
  1771. return VM_FAULT_MAJOR;
  1772. }
  1773. /*
  1774. * These routines also need to handle stuff like marking pages dirty
  1775. * and/or accessed for architectures that don't do it in hardware (most
  1776. * RISC architectures). The early dirtying is also good on the i386.
  1777. *
  1778. * There is also a hook called "update_mmu_cache()" that architectures
  1779. * with external mmu caches can use to update those (ie the Sparc or
  1780. * PowerPC hashed page tables that act as extended TLBs).
  1781. *
  1782. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1783. * but allow concurrent faults), and pte mapped but not yet locked.
  1784. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1785. */
  1786. static inline int handle_pte_fault(struct mm_struct *mm,
  1787. struct vm_area_struct *vma, unsigned long address,
  1788. pte_t *pte, pmd_t *pmd, int write_access)
  1789. {
  1790. pte_t entry;
  1791. spinlock_t *ptl;
  1792. entry = *pte;
  1793. if (!pte_present(entry)) {
  1794. if (pte_none(entry)) {
  1795. if (!vma->vm_ops || !vma->vm_ops->nopage)
  1796. return do_anonymous_page(mm, vma, address,
  1797. pte, pmd, write_access);
  1798. return do_no_page(mm, vma, address,
  1799. pte, pmd, write_access);
  1800. }
  1801. if (pte_file(entry))
  1802. return do_file_page(mm, vma, address,
  1803. pte, pmd, write_access, entry);
  1804. return do_swap_page(mm, vma, address,
  1805. pte, pmd, write_access, entry);
  1806. }
  1807. ptl = &mm->page_table_lock;
  1808. spin_lock(ptl);
  1809. if (unlikely(!pte_same(*pte, entry)))
  1810. goto unlock;
  1811. if (write_access) {
  1812. if (!pte_write(entry))
  1813. return do_wp_page(mm, vma, address,
  1814. pte, pmd, ptl, entry);
  1815. entry = pte_mkdirty(entry);
  1816. }
  1817. entry = pte_mkyoung(entry);
  1818. ptep_set_access_flags(vma, address, pte, entry, write_access);
  1819. update_mmu_cache(vma, address, entry);
  1820. lazy_mmu_prot_update(entry);
  1821. unlock:
  1822. pte_unmap_unlock(pte, ptl);
  1823. return VM_FAULT_MINOR;
  1824. }
  1825. /*
  1826. * By the time we get here, we already hold the mm semaphore
  1827. */
  1828. int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1829. unsigned long address, int write_access)
  1830. {
  1831. pgd_t *pgd;
  1832. pud_t *pud;
  1833. pmd_t *pmd;
  1834. pte_t *pte;
  1835. __set_current_state(TASK_RUNNING);
  1836. inc_page_state(pgfault);
  1837. if (unlikely(is_vm_hugetlb_page(vma)))
  1838. return hugetlb_fault(mm, vma, address, write_access);
  1839. pgd = pgd_offset(mm, address);
  1840. pud = pud_alloc(mm, pgd, address);
  1841. if (!pud)
  1842. return VM_FAULT_OOM;
  1843. pmd = pmd_alloc(mm, pud, address);
  1844. if (!pmd)
  1845. return VM_FAULT_OOM;
  1846. pte = pte_alloc_map(mm, pmd, address);
  1847. if (!pte)
  1848. return VM_FAULT_OOM;
  1849. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  1850. }
  1851. #ifndef __PAGETABLE_PUD_FOLDED
  1852. /*
  1853. * Allocate page upper directory.
  1854. * We've already handled the fast-path in-line.
  1855. */
  1856. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  1857. {
  1858. pud_t *new = pud_alloc_one(mm, address);
  1859. if (!new)
  1860. return -ENOMEM;
  1861. spin_lock(&mm->page_table_lock);
  1862. if (pgd_present(*pgd)) /* Another has populated it */
  1863. pud_free(new);
  1864. else
  1865. pgd_populate(mm, pgd, new);
  1866. spin_unlock(&mm->page_table_lock);
  1867. return 0;
  1868. }
  1869. #endif /* __PAGETABLE_PUD_FOLDED */
  1870. #ifndef __PAGETABLE_PMD_FOLDED
  1871. /*
  1872. * Allocate page middle directory.
  1873. * We've already handled the fast-path in-line.
  1874. */
  1875. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  1876. {
  1877. pmd_t *new = pmd_alloc_one(mm, address);
  1878. if (!new)
  1879. return -ENOMEM;
  1880. spin_lock(&mm->page_table_lock);
  1881. #ifndef __ARCH_HAS_4LEVEL_HACK
  1882. if (pud_present(*pud)) /* Another has populated it */
  1883. pmd_free(new);
  1884. else
  1885. pud_populate(mm, pud, new);
  1886. #else
  1887. if (pgd_present(*pud)) /* Another has populated it */
  1888. pmd_free(new);
  1889. else
  1890. pgd_populate(mm, pud, new);
  1891. #endif /* __ARCH_HAS_4LEVEL_HACK */
  1892. spin_unlock(&mm->page_table_lock);
  1893. return 0;
  1894. }
  1895. #endif /* __PAGETABLE_PMD_FOLDED */
  1896. int make_pages_present(unsigned long addr, unsigned long end)
  1897. {
  1898. int ret, len, write;
  1899. struct vm_area_struct * vma;
  1900. vma = find_vma(current->mm, addr);
  1901. if (!vma)
  1902. return -1;
  1903. write = (vma->vm_flags & VM_WRITE) != 0;
  1904. if (addr >= end)
  1905. BUG();
  1906. if (end > vma->vm_end)
  1907. BUG();
  1908. len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
  1909. ret = get_user_pages(current, current->mm, addr,
  1910. len, write, 0, NULL, NULL);
  1911. if (ret < 0)
  1912. return ret;
  1913. return ret == len ? 0 : -1;
  1914. }
  1915. /*
  1916. * Map a vmalloc()-space virtual address to the physical page.
  1917. */
  1918. struct page * vmalloc_to_page(void * vmalloc_addr)
  1919. {
  1920. unsigned long addr = (unsigned long) vmalloc_addr;
  1921. struct page *page = NULL;
  1922. pgd_t *pgd = pgd_offset_k(addr);
  1923. pud_t *pud;
  1924. pmd_t *pmd;
  1925. pte_t *ptep, pte;
  1926. if (!pgd_none(*pgd)) {
  1927. pud = pud_offset(pgd, addr);
  1928. if (!pud_none(*pud)) {
  1929. pmd = pmd_offset(pud, addr);
  1930. if (!pmd_none(*pmd)) {
  1931. ptep = pte_offset_map(pmd, addr);
  1932. pte = *ptep;
  1933. if (pte_present(pte))
  1934. page = pte_page(pte);
  1935. pte_unmap(ptep);
  1936. }
  1937. }
  1938. }
  1939. return page;
  1940. }
  1941. EXPORT_SYMBOL(vmalloc_to_page);
  1942. /*
  1943. * Map a vmalloc()-space virtual address to the physical page frame number.
  1944. */
  1945. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  1946. {
  1947. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  1948. }
  1949. EXPORT_SYMBOL(vmalloc_to_pfn);
  1950. #if !defined(__HAVE_ARCH_GATE_AREA)
  1951. #if defined(AT_SYSINFO_EHDR)
  1952. static struct vm_area_struct gate_vma;
  1953. static int __init gate_vma_init(void)
  1954. {
  1955. gate_vma.vm_mm = NULL;
  1956. gate_vma.vm_start = FIXADDR_USER_START;
  1957. gate_vma.vm_end = FIXADDR_USER_END;
  1958. gate_vma.vm_page_prot = PAGE_READONLY;
  1959. gate_vma.vm_flags = VM_RESERVED;
  1960. return 0;
  1961. }
  1962. __initcall(gate_vma_init);
  1963. #endif
  1964. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  1965. {
  1966. #ifdef AT_SYSINFO_EHDR
  1967. return &gate_vma;
  1968. #else
  1969. return NULL;
  1970. #endif
  1971. }
  1972. int in_gate_area_no_task(unsigned long addr)
  1973. {
  1974. #ifdef AT_SYSINFO_EHDR
  1975. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  1976. return 1;
  1977. #endif
  1978. return 0;
  1979. }
  1980. #endif /* __HAVE_ARCH_GATE_AREA */