memory.c 71 KB

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