memory.c 59 KB

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