memory.c 72 KB

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