memory.c 57 KB

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