memory.c 64 KB

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