memory.c 60 KB

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