memory.c 59 KB

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