memory.c 61 KB

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