memory.c 60 KB

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