nommu.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*
  2. * linux/mm/nommu.c
  3. *
  4. * Replacement code for mm functions to support CPU's that don't
  5. * have any form of memory management unit (thus no virtual memory).
  6. *
  7. * See Documentation/nommu-mmap.txt
  8. *
  9. * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
  10. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  11. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  12. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  13. * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/swap.h>
  19. #include <linux/file.h>
  20. #include <linux/highmem.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/mount.h>
  28. #include <linux/personality.h>
  29. #include <linux/security.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/audit.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/tlb.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/mmu_context.h>
  36. #include "internal.h"
  37. #if 0
  38. #define kenter(FMT, ...) \
  39. printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  40. #define kleave(FMT, ...) \
  41. printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  42. #define kdebug(FMT, ...) \
  43. printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
  44. #else
  45. #define kenter(FMT, ...) \
  46. no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  47. #define kleave(FMT, ...) \
  48. no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  49. #define kdebug(FMT, ...) \
  50. no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
  51. #endif
  52. void *high_memory;
  53. struct page *mem_map;
  54. unsigned long max_mapnr;
  55. unsigned long num_physpages;
  56. unsigned long highest_memmap_pfn;
  57. struct percpu_counter vm_committed_as;
  58. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  59. int sysctl_overcommit_ratio = 50; /* default is 50% */
  60. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  61. int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
  62. int heap_stack_gap = 0;
  63. atomic_long_t mmap_pages_allocated;
  64. EXPORT_SYMBOL(mem_map);
  65. EXPORT_SYMBOL(num_physpages);
  66. /* list of mapped, potentially shareable regions */
  67. static struct kmem_cache *vm_region_jar;
  68. struct rb_root nommu_region_tree = RB_ROOT;
  69. DECLARE_RWSEM(nommu_region_sem);
  70. const struct vm_operations_struct generic_file_vm_ops = {
  71. };
  72. /*
  73. * Return the total memory allocated for this pointer, not
  74. * just what the caller asked for.
  75. *
  76. * Doesn't have to be accurate, i.e. may have races.
  77. */
  78. unsigned int kobjsize(const void *objp)
  79. {
  80. struct page *page;
  81. /*
  82. * If the object we have should not have ksize performed on it,
  83. * return size of 0
  84. */
  85. if (!objp || !virt_addr_valid(objp))
  86. return 0;
  87. page = virt_to_head_page(objp);
  88. /*
  89. * If the allocator sets PageSlab, we know the pointer came from
  90. * kmalloc().
  91. */
  92. if (PageSlab(page))
  93. return ksize(objp);
  94. /*
  95. * If it's not a compound page, see if we have a matching VMA
  96. * region. This test is intentionally done in reverse order,
  97. * so if there's no VMA, we still fall through and hand back
  98. * PAGE_SIZE for 0-order pages.
  99. */
  100. if (!PageCompound(page)) {
  101. struct vm_area_struct *vma;
  102. vma = find_vma(current->mm, (unsigned long)objp);
  103. if (vma)
  104. return vma->vm_end - vma->vm_start;
  105. }
  106. /*
  107. * The ksize() function is only guaranteed to work for pointers
  108. * returned by kmalloc(). So handle arbitrary pointers here.
  109. */
  110. return PAGE_SIZE << compound_order(page);
  111. }
  112. int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  113. unsigned long start, int nr_pages, unsigned int foll_flags,
  114. struct page **pages, struct vm_area_struct **vmas,
  115. int *retry)
  116. {
  117. struct vm_area_struct *vma;
  118. unsigned long vm_flags;
  119. int i;
  120. /* calculate required read or write permissions.
  121. * If FOLL_FORCE is set, we only require the "MAY" flags.
  122. */
  123. vm_flags = (foll_flags & FOLL_WRITE) ?
  124. (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  125. vm_flags &= (foll_flags & FOLL_FORCE) ?
  126. (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  127. for (i = 0; i < nr_pages; i++) {
  128. vma = find_vma(mm, start);
  129. if (!vma)
  130. goto finish_or_fault;
  131. /* protect what we can, including chardevs */
  132. if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
  133. !(vm_flags & vma->vm_flags))
  134. goto finish_or_fault;
  135. if (pages) {
  136. pages[i] = virt_to_page(start);
  137. if (pages[i])
  138. page_cache_get(pages[i]);
  139. }
  140. if (vmas)
  141. vmas[i] = vma;
  142. start = (start + PAGE_SIZE) & PAGE_MASK;
  143. }
  144. return i;
  145. finish_or_fault:
  146. return i ? : -EFAULT;
  147. }
  148. /*
  149. * get a list of pages in an address range belonging to the specified process
  150. * and indicate the VMA that covers each page
  151. * - this is potentially dodgy as we may end incrementing the page count of a
  152. * slab page or a secondary page from a compound page
  153. * - don't permit access to VMAs that don't support it, such as I/O mappings
  154. */
  155. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  156. unsigned long start, int nr_pages, int write, int force,
  157. struct page **pages, struct vm_area_struct **vmas)
  158. {
  159. int flags = 0;
  160. if (write)
  161. flags |= FOLL_WRITE;
  162. if (force)
  163. flags |= FOLL_FORCE;
  164. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  165. NULL);
  166. }
  167. EXPORT_SYMBOL(get_user_pages);
  168. /**
  169. * follow_pfn - look up PFN at a user virtual address
  170. * @vma: memory mapping
  171. * @address: user virtual address
  172. * @pfn: location to store found PFN
  173. *
  174. * Only IO mappings and raw PFN mappings are allowed.
  175. *
  176. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  177. */
  178. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  179. unsigned long *pfn)
  180. {
  181. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  182. return -EINVAL;
  183. *pfn = address >> PAGE_SHIFT;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(follow_pfn);
  187. DEFINE_RWLOCK(vmlist_lock);
  188. struct vm_struct *vmlist;
  189. void vfree(const void *addr)
  190. {
  191. kfree(addr);
  192. }
  193. EXPORT_SYMBOL(vfree);
  194. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  195. {
  196. /*
  197. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  198. * returns only a logical address.
  199. */
  200. return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  201. }
  202. EXPORT_SYMBOL(__vmalloc);
  203. void *vmalloc_user(unsigned long size)
  204. {
  205. void *ret;
  206. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  207. PAGE_KERNEL);
  208. if (ret) {
  209. struct vm_area_struct *vma;
  210. down_write(&current->mm->mmap_sem);
  211. vma = find_vma(current->mm, (unsigned long)ret);
  212. if (vma)
  213. vma->vm_flags |= VM_USERMAP;
  214. up_write(&current->mm->mmap_sem);
  215. }
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(vmalloc_user);
  219. struct page *vmalloc_to_page(const void *addr)
  220. {
  221. return virt_to_page(addr);
  222. }
  223. EXPORT_SYMBOL(vmalloc_to_page);
  224. unsigned long vmalloc_to_pfn(const void *addr)
  225. {
  226. return page_to_pfn(virt_to_page(addr));
  227. }
  228. EXPORT_SYMBOL(vmalloc_to_pfn);
  229. long vread(char *buf, char *addr, unsigned long count)
  230. {
  231. memcpy(buf, addr, count);
  232. return count;
  233. }
  234. long vwrite(char *buf, char *addr, unsigned long count)
  235. {
  236. /* Don't allow overflow */
  237. if ((unsigned long) addr + count < count)
  238. count = -(unsigned long) addr;
  239. memcpy(addr, buf, count);
  240. return(count);
  241. }
  242. /*
  243. * vmalloc - allocate virtually continguos memory
  244. *
  245. * @size: allocation size
  246. *
  247. * Allocate enough pages to cover @size from the page level
  248. * allocator and map them into continguos kernel virtual space.
  249. *
  250. * For tight control over page level allocator and protection flags
  251. * use __vmalloc() instead.
  252. */
  253. void *vmalloc(unsigned long size)
  254. {
  255. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  256. }
  257. EXPORT_SYMBOL(vmalloc);
  258. /*
  259. * vzalloc - allocate virtually continguos memory with zero fill
  260. *
  261. * @size: allocation size
  262. *
  263. * Allocate enough pages to cover @size from the page level
  264. * allocator and map them into continguos kernel virtual space.
  265. * The memory allocated is set to zero.
  266. *
  267. * For tight control over page level allocator and protection flags
  268. * use __vmalloc() instead.
  269. */
  270. void *vzalloc(unsigned long size)
  271. {
  272. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  273. PAGE_KERNEL);
  274. }
  275. EXPORT_SYMBOL(vzalloc);
  276. /**
  277. * vmalloc_node - allocate memory on a specific node
  278. * @size: allocation size
  279. * @node: numa node
  280. *
  281. * Allocate enough pages to cover @size from the page level
  282. * allocator and map them into contiguous kernel virtual space.
  283. *
  284. * For tight control over page level allocator and protection flags
  285. * use __vmalloc() instead.
  286. */
  287. void *vmalloc_node(unsigned long size, int node)
  288. {
  289. return vmalloc(size);
  290. }
  291. EXPORT_SYMBOL(vmalloc_node);
  292. /**
  293. * vzalloc_node - allocate memory on a specific node with zero fill
  294. * @size: allocation size
  295. * @node: numa node
  296. *
  297. * Allocate enough pages to cover @size from the page level
  298. * allocator and map them into contiguous kernel virtual space.
  299. * The memory allocated is set to zero.
  300. *
  301. * For tight control over page level allocator and protection flags
  302. * use __vmalloc() instead.
  303. */
  304. void *vzalloc_node(unsigned long size, int node)
  305. {
  306. return vzalloc(size);
  307. }
  308. EXPORT_SYMBOL(vzalloc_node);
  309. #ifndef PAGE_KERNEL_EXEC
  310. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  311. #endif
  312. /**
  313. * vmalloc_exec - allocate virtually contiguous, executable memory
  314. * @size: allocation size
  315. *
  316. * Kernel-internal function to allocate enough pages to cover @size
  317. * the page level allocator and map them into contiguous and
  318. * executable kernel virtual space.
  319. *
  320. * For tight control over page level allocator and protection flags
  321. * use __vmalloc() instead.
  322. */
  323. void *vmalloc_exec(unsigned long size)
  324. {
  325. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  326. }
  327. /**
  328. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  329. * @size: allocation size
  330. *
  331. * Allocate enough 32bit PA addressable pages to cover @size from the
  332. * page level allocator and map them into continguos kernel virtual space.
  333. */
  334. void *vmalloc_32(unsigned long size)
  335. {
  336. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  337. }
  338. EXPORT_SYMBOL(vmalloc_32);
  339. /**
  340. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  341. * @size: allocation size
  342. *
  343. * The resulting memory area is 32bit addressable and zeroed so it can be
  344. * mapped to userspace without leaking data.
  345. *
  346. * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
  347. * remap_vmalloc_range() are permissible.
  348. */
  349. void *vmalloc_32_user(unsigned long size)
  350. {
  351. /*
  352. * We'll have to sort out the ZONE_DMA bits for 64-bit,
  353. * but for now this can simply use vmalloc_user() directly.
  354. */
  355. return vmalloc_user(size);
  356. }
  357. EXPORT_SYMBOL(vmalloc_32_user);
  358. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  359. {
  360. BUG();
  361. return NULL;
  362. }
  363. EXPORT_SYMBOL(vmap);
  364. void vunmap(const void *addr)
  365. {
  366. BUG();
  367. }
  368. EXPORT_SYMBOL(vunmap);
  369. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  370. {
  371. BUG();
  372. return NULL;
  373. }
  374. EXPORT_SYMBOL(vm_map_ram);
  375. void vm_unmap_ram(const void *mem, unsigned int count)
  376. {
  377. BUG();
  378. }
  379. EXPORT_SYMBOL(vm_unmap_ram);
  380. void vm_unmap_aliases(void)
  381. {
  382. }
  383. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  384. /*
  385. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  386. * have one.
  387. */
  388. void __attribute__((weak)) vmalloc_sync_all(void)
  389. {
  390. }
  391. /**
  392. * alloc_vm_area - allocate a range of kernel address space
  393. * @size: size of the area
  394. *
  395. * Returns: NULL on failure, vm_struct on success
  396. *
  397. * This function reserves a range of kernel address space, and
  398. * allocates pagetables to map that range. No actual mappings
  399. * are created. If the kernel address space is not shared
  400. * between processes, it syncs the pagetable across all
  401. * processes.
  402. */
  403. struct vm_struct *alloc_vm_area(size_t size)
  404. {
  405. BUG();
  406. return NULL;
  407. }
  408. EXPORT_SYMBOL_GPL(alloc_vm_area);
  409. void free_vm_area(struct vm_struct *area)
  410. {
  411. BUG();
  412. }
  413. EXPORT_SYMBOL_GPL(free_vm_area);
  414. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  415. struct page *page)
  416. {
  417. return -EINVAL;
  418. }
  419. EXPORT_SYMBOL(vm_insert_page);
  420. /*
  421. * sys_brk() for the most part doesn't need the global kernel
  422. * lock, except when an application is doing something nasty
  423. * like trying to un-brk an area that has already been mapped
  424. * to a regular file. in this case, the unmapping will need
  425. * to invoke file system routines that need the global lock.
  426. */
  427. SYSCALL_DEFINE1(brk, unsigned long, brk)
  428. {
  429. struct mm_struct *mm = current->mm;
  430. if (brk < mm->start_brk || brk > mm->context.end_brk)
  431. return mm->brk;
  432. if (mm->brk == brk)
  433. return mm->brk;
  434. /*
  435. * Always allow shrinking brk
  436. */
  437. if (brk <= mm->brk) {
  438. mm->brk = brk;
  439. return brk;
  440. }
  441. /*
  442. * Ok, looks good - let it rip.
  443. */
  444. flush_icache_range(mm->brk, brk);
  445. return mm->brk = brk;
  446. }
  447. /*
  448. * initialise the VMA and region record slabs
  449. */
  450. void __init mmap_init(void)
  451. {
  452. int ret;
  453. ret = percpu_counter_init(&vm_committed_as, 0);
  454. VM_BUG_ON(ret);
  455. vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
  456. }
  457. /*
  458. * validate the region tree
  459. * - the caller must hold the region lock
  460. */
  461. #ifdef CONFIG_DEBUG_NOMMU_REGIONS
  462. static noinline void validate_nommu_regions(void)
  463. {
  464. struct vm_region *region, *last;
  465. struct rb_node *p, *lastp;
  466. lastp = rb_first(&nommu_region_tree);
  467. if (!lastp)
  468. return;
  469. last = rb_entry(lastp, struct vm_region, vm_rb);
  470. BUG_ON(unlikely(last->vm_end <= last->vm_start));
  471. BUG_ON(unlikely(last->vm_top < last->vm_end));
  472. while ((p = rb_next(lastp))) {
  473. region = rb_entry(p, struct vm_region, vm_rb);
  474. last = rb_entry(lastp, struct vm_region, vm_rb);
  475. BUG_ON(unlikely(region->vm_end <= region->vm_start));
  476. BUG_ON(unlikely(region->vm_top < region->vm_end));
  477. BUG_ON(unlikely(region->vm_start < last->vm_top));
  478. lastp = p;
  479. }
  480. }
  481. #else
  482. static void validate_nommu_regions(void)
  483. {
  484. }
  485. #endif
  486. /*
  487. * add a region into the global tree
  488. */
  489. static void add_nommu_region(struct vm_region *region)
  490. {
  491. struct vm_region *pregion;
  492. struct rb_node **p, *parent;
  493. validate_nommu_regions();
  494. parent = NULL;
  495. p = &nommu_region_tree.rb_node;
  496. while (*p) {
  497. parent = *p;
  498. pregion = rb_entry(parent, struct vm_region, vm_rb);
  499. if (region->vm_start < pregion->vm_start)
  500. p = &(*p)->rb_left;
  501. else if (region->vm_start > pregion->vm_start)
  502. p = &(*p)->rb_right;
  503. else if (pregion == region)
  504. return;
  505. else
  506. BUG();
  507. }
  508. rb_link_node(&region->vm_rb, parent, p);
  509. rb_insert_color(&region->vm_rb, &nommu_region_tree);
  510. validate_nommu_regions();
  511. }
  512. /*
  513. * delete a region from the global tree
  514. */
  515. static void delete_nommu_region(struct vm_region *region)
  516. {
  517. BUG_ON(!nommu_region_tree.rb_node);
  518. validate_nommu_regions();
  519. rb_erase(&region->vm_rb, &nommu_region_tree);
  520. validate_nommu_regions();
  521. }
  522. /*
  523. * free a contiguous series of pages
  524. */
  525. static void free_page_series(unsigned long from, unsigned long to)
  526. {
  527. for (; from < to; from += PAGE_SIZE) {
  528. struct page *page = virt_to_page(from);
  529. kdebug("- free %lx", from);
  530. atomic_long_dec(&mmap_pages_allocated);
  531. if (page_count(page) != 1)
  532. kdebug("free page %p: refcount not one: %d",
  533. page, page_count(page));
  534. put_page(page);
  535. }
  536. }
  537. /*
  538. * release a reference to a region
  539. * - the caller must hold the region semaphore for writing, which this releases
  540. * - the region may not have been added to the tree yet, in which case vm_top
  541. * will equal vm_start
  542. */
  543. static void __put_nommu_region(struct vm_region *region)
  544. __releases(nommu_region_sem)
  545. {
  546. kenter("%p{%d}", region, region->vm_usage);
  547. BUG_ON(!nommu_region_tree.rb_node);
  548. if (--region->vm_usage == 0) {
  549. if (region->vm_top > region->vm_start)
  550. delete_nommu_region(region);
  551. up_write(&nommu_region_sem);
  552. if (region->vm_file)
  553. fput(region->vm_file);
  554. /* IO memory and memory shared directly out of the pagecache
  555. * from ramfs/tmpfs mustn't be released here */
  556. if (region->vm_flags & VM_MAPPED_COPY) {
  557. kdebug("free series");
  558. free_page_series(region->vm_start, region->vm_top);
  559. }
  560. kmem_cache_free(vm_region_jar, region);
  561. } else {
  562. up_write(&nommu_region_sem);
  563. }
  564. }
  565. /*
  566. * release a reference to a region
  567. */
  568. static void put_nommu_region(struct vm_region *region)
  569. {
  570. down_write(&nommu_region_sem);
  571. __put_nommu_region(region);
  572. }
  573. /*
  574. * update protection on a vma
  575. */
  576. static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
  577. {
  578. #ifdef CONFIG_MPU
  579. struct mm_struct *mm = vma->vm_mm;
  580. long start = vma->vm_start & PAGE_MASK;
  581. while (start < vma->vm_end) {
  582. protect_page(mm, start, flags);
  583. start += PAGE_SIZE;
  584. }
  585. update_protections(mm);
  586. #endif
  587. }
  588. /*
  589. * add a VMA into a process's mm_struct in the appropriate place in the list
  590. * and tree and add to the address space's page tree also if not an anonymous
  591. * page
  592. * - should be called with mm->mmap_sem held writelocked
  593. */
  594. static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
  595. {
  596. struct vm_area_struct *pvma, *prev;
  597. struct address_space *mapping;
  598. struct rb_node **p, *parent, *rb_prev;
  599. kenter(",%p", vma);
  600. BUG_ON(!vma->vm_region);
  601. mm->map_count++;
  602. vma->vm_mm = mm;
  603. protect_vma(vma, vma->vm_flags);
  604. /* add the VMA to the mapping */
  605. if (vma->vm_file) {
  606. mapping = vma->vm_file->f_mapping;
  607. flush_dcache_mmap_lock(mapping);
  608. vma_prio_tree_insert(vma, &mapping->i_mmap);
  609. flush_dcache_mmap_unlock(mapping);
  610. }
  611. /* add the VMA to the tree */
  612. parent = rb_prev = NULL;
  613. p = &mm->mm_rb.rb_node;
  614. while (*p) {
  615. parent = *p;
  616. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  617. /* sort by: start addr, end addr, VMA struct addr in that order
  618. * (the latter is necessary as we may get identical VMAs) */
  619. if (vma->vm_start < pvma->vm_start)
  620. p = &(*p)->rb_left;
  621. else if (vma->vm_start > pvma->vm_start) {
  622. rb_prev = parent;
  623. p = &(*p)->rb_right;
  624. } else if (vma->vm_end < pvma->vm_end)
  625. p = &(*p)->rb_left;
  626. else if (vma->vm_end > pvma->vm_end) {
  627. rb_prev = parent;
  628. p = &(*p)->rb_right;
  629. } else if (vma < pvma)
  630. p = &(*p)->rb_left;
  631. else if (vma > pvma) {
  632. rb_prev = parent;
  633. p = &(*p)->rb_right;
  634. } else
  635. BUG();
  636. }
  637. rb_link_node(&vma->vm_rb, parent, p);
  638. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  639. /* add VMA to the VMA list also */
  640. prev = NULL;
  641. if (rb_prev)
  642. prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  643. __vma_link_list(mm, vma, prev, parent);
  644. }
  645. /*
  646. * delete a VMA from its owning mm_struct and address space
  647. */
  648. static void delete_vma_from_mm(struct vm_area_struct *vma)
  649. {
  650. struct address_space *mapping;
  651. struct mm_struct *mm = vma->vm_mm;
  652. kenter("%p", vma);
  653. protect_vma(vma, 0);
  654. mm->map_count--;
  655. if (mm->mmap_cache == vma)
  656. mm->mmap_cache = NULL;
  657. /* remove the VMA from the mapping */
  658. if (vma->vm_file) {
  659. mapping = vma->vm_file->f_mapping;
  660. flush_dcache_mmap_lock(mapping);
  661. vma_prio_tree_remove(vma, &mapping->i_mmap);
  662. flush_dcache_mmap_unlock(mapping);
  663. }
  664. /* remove from the MM's tree and list */
  665. rb_erase(&vma->vm_rb, &mm->mm_rb);
  666. if (vma->vm_prev)
  667. vma->vm_prev->vm_next = vma->vm_next;
  668. else
  669. mm->mmap = vma->vm_next;
  670. if (vma->vm_next)
  671. vma->vm_next->vm_prev = vma->vm_prev;
  672. vma->vm_mm = NULL;
  673. }
  674. /*
  675. * destroy a VMA record
  676. */
  677. static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  678. {
  679. kenter("%p", vma);
  680. if (vma->vm_ops && vma->vm_ops->close)
  681. vma->vm_ops->close(vma);
  682. if (vma->vm_file) {
  683. fput(vma->vm_file);
  684. if (vma->vm_flags & VM_EXECUTABLE)
  685. removed_exe_file_vma(mm);
  686. }
  687. put_nommu_region(vma->vm_region);
  688. kmem_cache_free(vm_area_cachep, vma);
  689. }
  690. /*
  691. * look up the first VMA in which addr resides, NULL if none
  692. * - should be called with mm->mmap_sem at least held readlocked
  693. */
  694. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  695. {
  696. struct vm_area_struct *vma;
  697. /* check the cache first */
  698. vma = mm->mmap_cache;
  699. if (vma && vma->vm_start <= addr && vma->vm_end > addr)
  700. return vma;
  701. /* trawl the list (there may be multiple mappings in which addr
  702. * resides) */
  703. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  704. if (vma->vm_start > addr)
  705. return NULL;
  706. if (vma->vm_end > addr) {
  707. mm->mmap_cache = vma;
  708. return vma;
  709. }
  710. }
  711. return NULL;
  712. }
  713. EXPORT_SYMBOL(find_vma);
  714. /*
  715. * find a VMA
  716. * - we don't extend stack VMAs under NOMMU conditions
  717. */
  718. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  719. {
  720. return find_vma(mm, addr);
  721. }
  722. /*
  723. * expand a stack to a given address
  724. * - not supported under NOMMU conditions
  725. */
  726. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  727. {
  728. return -ENOMEM;
  729. }
  730. /*
  731. * look up the first VMA exactly that exactly matches addr
  732. * - should be called with mm->mmap_sem at least held readlocked
  733. */
  734. static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  735. unsigned long addr,
  736. unsigned long len)
  737. {
  738. struct vm_area_struct *vma;
  739. unsigned long end = addr + len;
  740. /* check the cache first */
  741. vma = mm->mmap_cache;
  742. if (vma && vma->vm_start == addr && vma->vm_end == end)
  743. return vma;
  744. /* trawl the list (there may be multiple mappings in which addr
  745. * resides) */
  746. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  747. if (vma->vm_start < addr)
  748. continue;
  749. if (vma->vm_start > addr)
  750. return NULL;
  751. if (vma->vm_end == end) {
  752. mm->mmap_cache = vma;
  753. return vma;
  754. }
  755. }
  756. return NULL;
  757. }
  758. /*
  759. * determine whether a mapping should be permitted and, if so, what sort of
  760. * mapping we're capable of supporting
  761. */
  762. static int validate_mmap_request(struct file *file,
  763. unsigned long addr,
  764. unsigned long len,
  765. unsigned long prot,
  766. unsigned long flags,
  767. unsigned long pgoff,
  768. unsigned long *_capabilities)
  769. {
  770. unsigned long capabilities, rlen;
  771. unsigned long reqprot = prot;
  772. int ret;
  773. /* do the simple checks first */
  774. if (flags & MAP_FIXED) {
  775. printk(KERN_DEBUG
  776. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  777. current->pid);
  778. return -EINVAL;
  779. }
  780. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  781. (flags & MAP_TYPE) != MAP_SHARED)
  782. return -EINVAL;
  783. if (!len)
  784. return -EINVAL;
  785. /* Careful about overflows.. */
  786. rlen = PAGE_ALIGN(len);
  787. if (!rlen || rlen > TASK_SIZE)
  788. return -ENOMEM;
  789. /* offset overflow? */
  790. if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
  791. return -EOVERFLOW;
  792. if (file) {
  793. /* validate file mapping requests */
  794. struct address_space *mapping;
  795. /* files must support mmap */
  796. if (!file->f_op || !file->f_op->mmap)
  797. return -ENODEV;
  798. /* work out if what we've got could possibly be shared
  799. * - we support chardevs that provide their own "memory"
  800. * - we support files/blockdevs that are memory backed
  801. */
  802. mapping = file->f_mapping;
  803. if (!mapping)
  804. mapping = file->f_path.dentry->d_inode->i_mapping;
  805. capabilities = 0;
  806. if (mapping && mapping->backing_dev_info)
  807. capabilities = mapping->backing_dev_info->capabilities;
  808. if (!capabilities) {
  809. /* no explicit capabilities set, so assume some
  810. * defaults */
  811. switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
  812. case S_IFREG:
  813. case S_IFBLK:
  814. capabilities = BDI_CAP_MAP_COPY;
  815. break;
  816. case S_IFCHR:
  817. capabilities =
  818. BDI_CAP_MAP_DIRECT |
  819. BDI_CAP_READ_MAP |
  820. BDI_CAP_WRITE_MAP;
  821. break;
  822. default:
  823. return -EINVAL;
  824. }
  825. }
  826. /* eliminate any capabilities that we can't support on this
  827. * device */
  828. if (!file->f_op->get_unmapped_area)
  829. capabilities &= ~BDI_CAP_MAP_DIRECT;
  830. if (!file->f_op->read)
  831. capabilities &= ~BDI_CAP_MAP_COPY;
  832. /* The file shall have been opened with read permission. */
  833. if (!(file->f_mode & FMODE_READ))
  834. return -EACCES;
  835. if (flags & MAP_SHARED) {
  836. /* do checks for writing, appending and locking */
  837. if ((prot & PROT_WRITE) &&
  838. !(file->f_mode & FMODE_WRITE))
  839. return -EACCES;
  840. if (IS_APPEND(file->f_path.dentry->d_inode) &&
  841. (file->f_mode & FMODE_WRITE))
  842. return -EACCES;
  843. if (locks_verify_locked(file->f_path.dentry->d_inode))
  844. return -EAGAIN;
  845. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  846. return -ENODEV;
  847. /* we mustn't privatise shared mappings */
  848. capabilities &= ~BDI_CAP_MAP_COPY;
  849. }
  850. else {
  851. /* we're going to read the file into private memory we
  852. * allocate */
  853. if (!(capabilities & BDI_CAP_MAP_COPY))
  854. return -ENODEV;
  855. /* we don't permit a private writable mapping to be
  856. * shared with the backing device */
  857. if (prot & PROT_WRITE)
  858. capabilities &= ~BDI_CAP_MAP_DIRECT;
  859. }
  860. if (capabilities & BDI_CAP_MAP_DIRECT) {
  861. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  862. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  863. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  864. ) {
  865. capabilities &= ~BDI_CAP_MAP_DIRECT;
  866. if (flags & MAP_SHARED) {
  867. printk(KERN_WARNING
  868. "MAP_SHARED not completely supported on !MMU\n");
  869. return -EINVAL;
  870. }
  871. }
  872. }
  873. /* handle executable mappings and implied executable
  874. * mappings */
  875. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  876. if (prot & PROT_EXEC)
  877. return -EPERM;
  878. }
  879. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  880. /* handle implication of PROT_EXEC by PROT_READ */
  881. if (current->personality & READ_IMPLIES_EXEC) {
  882. if (capabilities & BDI_CAP_EXEC_MAP)
  883. prot |= PROT_EXEC;
  884. }
  885. }
  886. else if ((prot & PROT_READ) &&
  887. (prot & PROT_EXEC) &&
  888. !(capabilities & BDI_CAP_EXEC_MAP)
  889. ) {
  890. /* backing file is not executable, try to copy */
  891. capabilities &= ~BDI_CAP_MAP_DIRECT;
  892. }
  893. }
  894. else {
  895. /* anonymous mappings are always memory backed and can be
  896. * privately mapped
  897. */
  898. capabilities = BDI_CAP_MAP_COPY;
  899. /* handle PROT_EXEC implication by PROT_READ */
  900. if ((prot & PROT_READ) &&
  901. (current->personality & READ_IMPLIES_EXEC))
  902. prot |= PROT_EXEC;
  903. }
  904. /* allow the security API to have its say */
  905. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  906. if (ret < 0)
  907. return ret;
  908. /* looks okay */
  909. *_capabilities = capabilities;
  910. return 0;
  911. }
  912. /*
  913. * we've determined that we can make the mapping, now translate what we
  914. * now know into VMA flags
  915. */
  916. static unsigned long determine_vm_flags(struct file *file,
  917. unsigned long prot,
  918. unsigned long flags,
  919. unsigned long capabilities)
  920. {
  921. unsigned long vm_flags;
  922. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  923. /* vm_flags |= mm->def_flags; */
  924. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  925. /* attempt to share read-only copies of mapped file chunks */
  926. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  927. if (file && !(prot & PROT_WRITE))
  928. vm_flags |= VM_MAYSHARE;
  929. } else {
  930. /* overlay a shareable mapping on the backing device or inode
  931. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  932. * romfs/cramfs */
  933. vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
  934. if (flags & MAP_SHARED)
  935. vm_flags |= VM_SHARED;
  936. }
  937. /* refuse to let anyone share private mappings with this process if
  938. * it's being traced - otherwise breakpoints set in it may interfere
  939. * with another untraced process
  940. */
  941. if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
  942. vm_flags &= ~VM_MAYSHARE;
  943. return vm_flags;
  944. }
  945. /*
  946. * set up a shared mapping on a file (the driver or filesystem provides and
  947. * pins the storage)
  948. */
  949. static int do_mmap_shared_file(struct vm_area_struct *vma)
  950. {
  951. int ret;
  952. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  953. if (ret == 0) {
  954. vma->vm_region->vm_top = vma->vm_region->vm_end;
  955. return 0;
  956. }
  957. if (ret != -ENOSYS)
  958. return ret;
  959. /* getting -ENOSYS indicates that direct mmap isn't possible (as
  960. * opposed to tried but failed) so we can only give a suitable error as
  961. * it's not possible to make a private copy if MAP_SHARED was given */
  962. return -ENODEV;
  963. }
  964. /*
  965. * set up a private mapping or an anonymous shared mapping
  966. */
  967. static int do_mmap_private(struct vm_area_struct *vma,
  968. struct vm_region *region,
  969. unsigned long len,
  970. unsigned long capabilities)
  971. {
  972. struct page *pages;
  973. unsigned long total, point, n, rlen;
  974. void *base;
  975. int ret, order;
  976. /* invoke the file's mapping function so that it can keep track of
  977. * shared mappings on devices or memory
  978. * - VM_MAYSHARE will be set if it may attempt to share
  979. */
  980. if (capabilities & BDI_CAP_MAP_DIRECT) {
  981. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  982. if (ret == 0) {
  983. /* shouldn't return success if we're not sharing */
  984. BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
  985. vma->vm_region->vm_top = vma->vm_region->vm_end;
  986. return 0;
  987. }
  988. if (ret != -ENOSYS)
  989. return ret;
  990. /* getting an ENOSYS error indicates that direct mmap isn't
  991. * possible (as opposed to tried but failed) so we'll try to
  992. * make a private copy of the data and map that instead */
  993. }
  994. rlen = PAGE_ALIGN(len);
  995. /* allocate some memory to hold the mapping
  996. * - note that this may not return a page-aligned address if the object
  997. * we're allocating is smaller than a page
  998. */
  999. order = get_order(rlen);
  1000. kdebug("alloc order %d for %lx", order, len);
  1001. pages = alloc_pages(GFP_KERNEL, order);
  1002. if (!pages)
  1003. goto enomem;
  1004. total = 1 << order;
  1005. atomic_long_add(total, &mmap_pages_allocated);
  1006. point = rlen >> PAGE_SHIFT;
  1007. /* we allocated a power-of-2 sized page set, so we may want to trim off
  1008. * the excess */
  1009. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
  1010. while (total > point) {
  1011. order = ilog2(total - point);
  1012. n = 1 << order;
  1013. kdebug("shave %lu/%lu @%lu", n, total - point, total);
  1014. atomic_long_sub(n, &mmap_pages_allocated);
  1015. total -= n;
  1016. set_page_refcounted(pages + total);
  1017. __free_pages(pages + total, order);
  1018. }
  1019. }
  1020. for (point = 1; point < total; point++)
  1021. set_page_refcounted(&pages[point]);
  1022. base = page_address(pages);
  1023. region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
  1024. region->vm_start = (unsigned long) base;
  1025. region->vm_end = region->vm_start + rlen;
  1026. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  1027. vma->vm_start = region->vm_start;
  1028. vma->vm_end = region->vm_start + len;
  1029. if (vma->vm_file) {
  1030. /* read the contents of a file into the copy */
  1031. mm_segment_t old_fs;
  1032. loff_t fpos;
  1033. fpos = vma->vm_pgoff;
  1034. fpos <<= PAGE_SHIFT;
  1035. old_fs = get_fs();
  1036. set_fs(KERNEL_DS);
  1037. ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
  1038. set_fs(old_fs);
  1039. if (ret < 0)
  1040. goto error_free;
  1041. /* clear the last little bit */
  1042. if (ret < rlen)
  1043. memset(base + ret, 0, rlen - ret);
  1044. }
  1045. return 0;
  1046. error_free:
  1047. free_page_series(region->vm_start, region->vm_top);
  1048. region->vm_start = vma->vm_start = 0;
  1049. region->vm_end = vma->vm_end = 0;
  1050. region->vm_top = 0;
  1051. return ret;
  1052. enomem:
  1053. printk("Allocation of length %lu from process %d (%s) failed\n",
  1054. len, current->pid, current->comm);
  1055. show_free_areas(0);
  1056. return -ENOMEM;
  1057. }
  1058. /*
  1059. * handle mapping creation for uClinux
  1060. */
  1061. unsigned long do_mmap_pgoff(struct file *file,
  1062. unsigned long addr,
  1063. unsigned long len,
  1064. unsigned long prot,
  1065. unsigned long flags,
  1066. unsigned long pgoff)
  1067. {
  1068. struct vm_area_struct *vma;
  1069. struct vm_region *region;
  1070. struct rb_node *rb;
  1071. unsigned long capabilities, vm_flags, result;
  1072. int ret;
  1073. kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
  1074. /* decide whether we should attempt the mapping, and if so what sort of
  1075. * mapping */
  1076. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  1077. &capabilities);
  1078. if (ret < 0) {
  1079. kleave(" = %d [val]", ret);
  1080. return ret;
  1081. }
  1082. /* we ignore the address hint */
  1083. addr = 0;
  1084. /* we've determined that we can make the mapping, now translate what we
  1085. * now know into VMA flags */
  1086. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  1087. /* we're going to need to record the mapping */
  1088. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  1089. if (!region)
  1090. goto error_getting_region;
  1091. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1092. if (!vma)
  1093. goto error_getting_vma;
  1094. region->vm_usage = 1;
  1095. region->vm_flags = vm_flags;
  1096. region->vm_pgoff = pgoff;
  1097. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1098. vma->vm_flags = vm_flags;
  1099. vma->vm_pgoff = pgoff;
  1100. if (file) {
  1101. region->vm_file = file;
  1102. get_file(file);
  1103. vma->vm_file = file;
  1104. get_file(file);
  1105. if (vm_flags & VM_EXECUTABLE) {
  1106. added_exe_file_vma(current->mm);
  1107. vma->vm_mm = current->mm;
  1108. }
  1109. }
  1110. down_write(&nommu_region_sem);
  1111. /* if we want to share, we need to check for regions created by other
  1112. * mmap() calls that overlap with our proposed mapping
  1113. * - we can only share with a superset match on most regular files
  1114. * - shared mappings on character devices and memory backed files are
  1115. * permitted to overlap inexactly as far as we are concerned for in
  1116. * these cases, sharing is handled in the driver or filesystem rather
  1117. * than here
  1118. */
  1119. if (vm_flags & VM_MAYSHARE) {
  1120. struct vm_region *pregion;
  1121. unsigned long pglen, rpglen, pgend, rpgend, start;
  1122. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1123. pgend = pgoff + pglen;
  1124. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  1125. pregion = rb_entry(rb, struct vm_region, vm_rb);
  1126. if (!(pregion->vm_flags & VM_MAYSHARE))
  1127. continue;
  1128. /* search for overlapping mappings on the same file */
  1129. if (pregion->vm_file->f_path.dentry->d_inode !=
  1130. file->f_path.dentry->d_inode)
  1131. continue;
  1132. if (pregion->vm_pgoff >= pgend)
  1133. continue;
  1134. rpglen = pregion->vm_end - pregion->vm_start;
  1135. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1136. rpgend = pregion->vm_pgoff + rpglen;
  1137. if (pgoff >= rpgend)
  1138. continue;
  1139. /* handle inexactly overlapping matches between
  1140. * mappings */
  1141. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  1142. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  1143. /* new mapping is not a subset of the region */
  1144. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  1145. goto sharing_violation;
  1146. continue;
  1147. }
  1148. /* we've found a region we can share */
  1149. pregion->vm_usage++;
  1150. vma->vm_region = pregion;
  1151. start = pregion->vm_start;
  1152. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  1153. vma->vm_start = start;
  1154. vma->vm_end = start + len;
  1155. if (pregion->vm_flags & VM_MAPPED_COPY) {
  1156. kdebug("share copy");
  1157. vma->vm_flags |= VM_MAPPED_COPY;
  1158. } else {
  1159. kdebug("share mmap");
  1160. ret = do_mmap_shared_file(vma);
  1161. if (ret < 0) {
  1162. vma->vm_region = NULL;
  1163. vma->vm_start = 0;
  1164. vma->vm_end = 0;
  1165. pregion->vm_usage--;
  1166. pregion = NULL;
  1167. goto error_just_free;
  1168. }
  1169. }
  1170. fput(region->vm_file);
  1171. kmem_cache_free(vm_region_jar, region);
  1172. region = pregion;
  1173. result = start;
  1174. goto share;
  1175. }
  1176. /* obtain the address at which to make a shared mapping
  1177. * - this is the hook for quasi-memory character devices to
  1178. * tell us the location of a shared mapping
  1179. */
  1180. if (capabilities & BDI_CAP_MAP_DIRECT) {
  1181. addr = file->f_op->get_unmapped_area(file, addr, len,
  1182. pgoff, flags);
  1183. if (IS_ERR_VALUE(addr)) {
  1184. ret = addr;
  1185. if (ret != -ENOSYS)
  1186. goto error_just_free;
  1187. /* the driver refused to tell us where to site
  1188. * the mapping so we'll have to attempt to copy
  1189. * it */
  1190. ret = -ENODEV;
  1191. if (!(capabilities & BDI_CAP_MAP_COPY))
  1192. goto error_just_free;
  1193. capabilities &= ~BDI_CAP_MAP_DIRECT;
  1194. } else {
  1195. vma->vm_start = region->vm_start = addr;
  1196. vma->vm_end = region->vm_end = addr + len;
  1197. }
  1198. }
  1199. }
  1200. vma->vm_region = region;
  1201. /* set up the mapping
  1202. * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
  1203. */
  1204. if (file && vma->vm_flags & VM_SHARED)
  1205. ret = do_mmap_shared_file(vma);
  1206. else
  1207. ret = do_mmap_private(vma, region, len, capabilities);
  1208. if (ret < 0)
  1209. goto error_just_free;
  1210. add_nommu_region(region);
  1211. /* clear anonymous mappings that don't ask for uninitialized data */
  1212. if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
  1213. memset((void *)region->vm_start, 0,
  1214. region->vm_end - region->vm_start);
  1215. /* okay... we have a mapping; now we have to register it */
  1216. result = vma->vm_start;
  1217. current->mm->total_vm += len >> PAGE_SHIFT;
  1218. share:
  1219. add_vma_to_mm(current->mm, vma);
  1220. /* we flush the region from the icache only when the first executable
  1221. * mapping of it is made */
  1222. if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
  1223. flush_icache_range(region->vm_start, region->vm_end);
  1224. region->vm_icache_flushed = true;
  1225. }
  1226. up_write(&nommu_region_sem);
  1227. kleave(" = %lx", result);
  1228. return result;
  1229. error_just_free:
  1230. up_write(&nommu_region_sem);
  1231. error:
  1232. if (region->vm_file)
  1233. fput(region->vm_file);
  1234. kmem_cache_free(vm_region_jar, region);
  1235. if (vma->vm_file)
  1236. fput(vma->vm_file);
  1237. if (vma->vm_flags & VM_EXECUTABLE)
  1238. removed_exe_file_vma(vma->vm_mm);
  1239. kmem_cache_free(vm_area_cachep, vma);
  1240. kleave(" = %d", ret);
  1241. return ret;
  1242. sharing_violation:
  1243. up_write(&nommu_region_sem);
  1244. printk(KERN_WARNING "Attempt to share mismatched mappings\n");
  1245. ret = -EINVAL;
  1246. goto error;
  1247. error_getting_vma:
  1248. kmem_cache_free(vm_region_jar, region);
  1249. printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
  1250. " from process %d failed\n",
  1251. len, current->pid);
  1252. show_free_areas(0);
  1253. return -ENOMEM;
  1254. error_getting_region:
  1255. printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
  1256. " from process %d failed\n",
  1257. len, current->pid);
  1258. show_free_areas(0);
  1259. return -ENOMEM;
  1260. }
  1261. EXPORT_SYMBOL(do_mmap_pgoff);
  1262. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1263. unsigned long, prot, unsigned long, flags,
  1264. unsigned long, fd, unsigned long, pgoff)
  1265. {
  1266. struct file *file = NULL;
  1267. unsigned long retval = -EBADF;
  1268. audit_mmap_fd(fd, flags);
  1269. if (!(flags & MAP_ANONYMOUS)) {
  1270. file = fget(fd);
  1271. if (!file)
  1272. goto out;
  1273. }
  1274. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1275. down_write(&current->mm->mmap_sem);
  1276. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1277. up_write(&current->mm->mmap_sem);
  1278. if (file)
  1279. fput(file);
  1280. out:
  1281. return retval;
  1282. }
  1283. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1284. struct mmap_arg_struct {
  1285. unsigned long addr;
  1286. unsigned long len;
  1287. unsigned long prot;
  1288. unsigned long flags;
  1289. unsigned long fd;
  1290. unsigned long offset;
  1291. };
  1292. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1293. {
  1294. struct mmap_arg_struct a;
  1295. if (copy_from_user(&a, arg, sizeof(a)))
  1296. return -EFAULT;
  1297. if (a.offset & ~PAGE_MASK)
  1298. return -EINVAL;
  1299. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1300. a.offset >> PAGE_SHIFT);
  1301. }
  1302. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1303. /*
  1304. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1305. * for the first part or the tail.
  1306. */
  1307. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1308. unsigned long addr, int new_below)
  1309. {
  1310. struct vm_area_struct *new;
  1311. struct vm_region *region;
  1312. unsigned long npages;
  1313. kenter("");
  1314. /* we're only permitted to split anonymous regions (these should have
  1315. * only a single usage on the region) */
  1316. if (vma->vm_file)
  1317. return -ENOMEM;
  1318. if (mm->map_count >= sysctl_max_map_count)
  1319. return -ENOMEM;
  1320. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1321. if (!region)
  1322. return -ENOMEM;
  1323. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1324. if (!new) {
  1325. kmem_cache_free(vm_region_jar, region);
  1326. return -ENOMEM;
  1327. }
  1328. /* most fields are the same, copy all, and then fixup */
  1329. *new = *vma;
  1330. *region = *vma->vm_region;
  1331. new->vm_region = region;
  1332. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1333. if (new_below) {
  1334. region->vm_top = region->vm_end = new->vm_end = addr;
  1335. } else {
  1336. region->vm_start = new->vm_start = addr;
  1337. region->vm_pgoff = new->vm_pgoff += npages;
  1338. }
  1339. if (new->vm_ops && new->vm_ops->open)
  1340. new->vm_ops->open(new);
  1341. delete_vma_from_mm(vma);
  1342. down_write(&nommu_region_sem);
  1343. delete_nommu_region(vma->vm_region);
  1344. if (new_below) {
  1345. vma->vm_region->vm_start = vma->vm_start = addr;
  1346. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1347. } else {
  1348. vma->vm_region->vm_end = vma->vm_end = addr;
  1349. vma->vm_region->vm_top = addr;
  1350. }
  1351. add_nommu_region(vma->vm_region);
  1352. add_nommu_region(new->vm_region);
  1353. up_write(&nommu_region_sem);
  1354. add_vma_to_mm(mm, vma);
  1355. add_vma_to_mm(mm, new);
  1356. return 0;
  1357. }
  1358. /*
  1359. * shrink a VMA by removing the specified chunk from either the beginning or
  1360. * the end
  1361. */
  1362. static int shrink_vma(struct mm_struct *mm,
  1363. struct vm_area_struct *vma,
  1364. unsigned long from, unsigned long to)
  1365. {
  1366. struct vm_region *region;
  1367. kenter("");
  1368. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1369. * and list */
  1370. delete_vma_from_mm(vma);
  1371. if (from > vma->vm_start)
  1372. vma->vm_end = from;
  1373. else
  1374. vma->vm_start = to;
  1375. add_vma_to_mm(mm, vma);
  1376. /* cut the backing region down to size */
  1377. region = vma->vm_region;
  1378. BUG_ON(region->vm_usage != 1);
  1379. down_write(&nommu_region_sem);
  1380. delete_nommu_region(region);
  1381. if (from > region->vm_start) {
  1382. to = region->vm_top;
  1383. region->vm_top = region->vm_end = from;
  1384. } else {
  1385. region->vm_start = to;
  1386. }
  1387. add_nommu_region(region);
  1388. up_write(&nommu_region_sem);
  1389. free_page_series(from, to);
  1390. return 0;
  1391. }
  1392. /*
  1393. * release a mapping
  1394. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1395. * VMA, though it need not cover the whole VMA
  1396. */
  1397. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1398. {
  1399. struct vm_area_struct *vma;
  1400. unsigned long end = start + len;
  1401. int ret;
  1402. kenter(",%lx,%zx", start, len);
  1403. if (len == 0)
  1404. return -EINVAL;
  1405. /* find the first potentially overlapping VMA */
  1406. vma = find_vma(mm, start);
  1407. if (!vma) {
  1408. static int limit = 0;
  1409. if (limit < 5) {
  1410. printk(KERN_WARNING
  1411. "munmap of memory not mmapped by process %d"
  1412. " (%s): 0x%lx-0x%lx\n",
  1413. current->pid, current->comm,
  1414. start, start + len - 1);
  1415. limit++;
  1416. }
  1417. return -EINVAL;
  1418. }
  1419. /* we're allowed to split an anonymous VMA but not a file-backed one */
  1420. if (vma->vm_file) {
  1421. do {
  1422. if (start > vma->vm_start) {
  1423. kleave(" = -EINVAL [miss]");
  1424. return -EINVAL;
  1425. }
  1426. if (end == vma->vm_end)
  1427. goto erase_whole_vma;
  1428. vma = vma->vm_next;
  1429. } while (vma);
  1430. kleave(" = -EINVAL [split file]");
  1431. return -EINVAL;
  1432. } else {
  1433. /* the chunk must be a subset of the VMA found */
  1434. if (start == vma->vm_start && end == vma->vm_end)
  1435. goto erase_whole_vma;
  1436. if (start < vma->vm_start || end > vma->vm_end) {
  1437. kleave(" = -EINVAL [superset]");
  1438. return -EINVAL;
  1439. }
  1440. if (start & ~PAGE_MASK) {
  1441. kleave(" = -EINVAL [unaligned start]");
  1442. return -EINVAL;
  1443. }
  1444. if (end != vma->vm_end && end & ~PAGE_MASK) {
  1445. kleave(" = -EINVAL [unaligned split]");
  1446. return -EINVAL;
  1447. }
  1448. if (start != vma->vm_start && end != vma->vm_end) {
  1449. ret = split_vma(mm, vma, start, 1);
  1450. if (ret < 0) {
  1451. kleave(" = %d [split]", ret);
  1452. return ret;
  1453. }
  1454. }
  1455. return shrink_vma(mm, vma, start, end);
  1456. }
  1457. erase_whole_vma:
  1458. delete_vma_from_mm(vma);
  1459. delete_vma(mm, vma);
  1460. kleave(" = 0");
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL(do_munmap);
  1464. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1465. {
  1466. int ret;
  1467. struct mm_struct *mm = current->mm;
  1468. down_write(&mm->mmap_sem);
  1469. ret = do_munmap(mm, addr, len);
  1470. up_write(&mm->mmap_sem);
  1471. return ret;
  1472. }
  1473. /*
  1474. * release all the mappings made in a process's VM space
  1475. */
  1476. void exit_mmap(struct mm_struct *mm)
  1477. {
  1478. struct vm_area_struct *vma;
  1479. if (!mm)
  1480. return;
  1481. kenter("");
  1482. mm->total_vm = 0;
  1483. while ((vma = mm->mmap)) {
  1484. mm->mmap = vma->vm_next;
  1485. delete_vma_from_mm(vma);
  1486. delete_vma(mm, vma);
  1487. cond_resched();
  1488. }
  1489. kleave("");
  1490. }
  1491. unsigned long do_brk(unsigned long addr, unsigned long len)
  1492. {
  1493. return -ENOMEM;
  1494. }
  1495. /*
  1496. * expand (or shrink) an existing mapping, potentially moving it at the same
  1497. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  1498. *
  1499. * under NOMMU conditions, we only permit changing a mapping's size, and only
  1500. * as long as it stays within the region allocated by do_mmap_private() and the
  1501. * block is not shareable
  1502. *
  1503. * MREMAP_FIXED is not supported under NOMMU conditions
  1504. */
  1505. unsigned long do_mremap(unsigned long addr,
  1506. unsigned long old_len, unsigned long new_len,
  1507. unsigned long flags, unsigned long new_addr)
  1508. {
  1509. struct vm_area_struct *vma;
  1510. /* insanity checks first */
  1511. if (old_len == 0 || new_len == 0)
  1512. return (unsigned long) -EINVAL;
  1513. if (addr & ~PAGE_MASK)
  1514. return -EINVAL;
  1515. if (flags & MREMAP_FIXED && new_addr != addr)
  1516. return (unsigned long) -EINVAL;
  1517. vma = find_vma_exact(current->mm, addr, old_len);
  1518. if (!vma)
  1519. return (unsigned long) -EINVAL;
  1520. if (vma->vm_end != vma->vm_start + old_len)
  1521. return (unsigned long) -EFAULT;
  1522. if (vma->vm_flags & VM_MAYSHARE)
  1523. return (unsigned long) -EPERM;
  1524. if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
  1525. return (unsigned long) -ENOMEM;
  1526. /* all checks complete - do it */
  1527. vma->vm_end = vma->vm_start + new_len;
  1528. return vma->vm_start;
  1529. }
  1530. EXPORT_SYMBOL(do_mremap);
  1531. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  1532. unsigned long, new_len, unsigned long, flags,
  1533. unsigned long, new_addr)
  1534. {
  1535. unsigned long ret;
  1536. down_write(&current->mm->mmap_sem);
  1537. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1538. up_write(&current->mm->mmap_sem);
  1539. return ret;
  1540. }
  1541. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1542. unsigned int foll_flags)
  1543. {
  1544. return NULL;
  1545. }
  1546. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  1547. unsigned long to, unsigned long size, pgprot_t prot)
  1548. {
  1549. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  1550. return 0;
  1551. }
  1552. EXPORT_SYMBOL(remap_pfn_range);
  1553. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1554. unsigned long pgoff)
  1555. {
  1556. unsigned int size = vma->vm_end - vma->vm_start;
  1557. if (!(vma->vm_flags & VM_USERMAP))
  1558. return -EINVAL;
  1559. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1560. vma->vm_end = vma->vm_start + size;
  1561. return 0;
  1562. }
  1563. EXPORT_SYMBOL(remap_vmalloc_range);
  1564. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1565. unsigned long len, unsigned long pgoff, unsigned long flags)
  1566. {
  1567. return -ENOMEM;
  1568. }
  1569. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1570. {
  1571. }
  1572. void unmap_mapping_range(struct address_space *mapping,
  1573. loff_t const holebegin, loff_t const holelen,
  1574. int even_cows)
  1575. {
  1576. }
  1577. EXPORT_SYMBOL(unmap_mapping_range);
  1578. /*
  1579. * Check that a process has enough memory to allocate a new virtual
  1580. * mapping. 0 means there is enough memory for the allocation to
  1581. * succeed and -ENOMEM implies there is not.
  1582. *
  1583. * We currently support three overcommit policies, which are set via the
  1584. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1585. *
  1586. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1587. * Additional code 2002 Jul 20 by Robert Love.
  1588. *
  1589. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1590. *
  1591. * Note this is a helper function intended to be used by LSMs which
  1592. * wish to use this logic.
  1593. */
  1594. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1595. {
  1596. unsigned long free, allowed;
  1597. vm_acct_memory(pages);
  1598. /*
  1599. * Sometimes we want to use more memory than we have
  1600. */
  1601. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1602. return 0;
  1603. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1604. unsigned long n;
  1605. free = global_page_state(NR_FILE_PAGES);
  1606. free += nr_swap_pages;
  1607. /*
  1608. * Any slabs which are created with the
  1609. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1610. * which are reclaimable, under pressure. The dentry
  1611. * cache and most inode caches should fall into this
  1612. */
  1613. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1614. /*
  1615. * Leave the last 3% for root
  1616. */
  1617. if (!cap_sys_admin)
  1618. free -= free / 32;
  1619. if (free > pages)
  1620. return 0;
  1621. /*
  1622. * nr_free_pages() is very expensive on large systems,
  1623. * only call if we're about to fail.
  1624. */
  1625. n = nr_free_pages();
  1626. /*
  1627. * Leave reserved pages. The pages are not for anonymous pages.
  1628. */
  1629. if (n <= totalreserve_pages)
  1630. goto error;
  1631. else
  1632. n -= totalreserve_pages;
  1633. /*
  1634. * Leave the last 3% for root
  1635. */
  1636. if (!cap_sys_admin)
  1637. n -= n / 32;
  1638. free += n;
  1639. if (free > pages)
  1640. return 0;
  1641. goto error;
  1642. }
  1643. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1644. /*
  1645. * Leave the last 3% for root
  1646. */
  1647. if (!cap_sys_admin)
  1648. allowed -= allowed / 32;
  1649. allowed += total_swap_pages;
  1650. /* Don't let a single process grow too big:
  1651. leave 3% of the size of this process for other processes */
  1652. if (mm)
  1653. allowed -= mm->total_vm / 32;
  1654. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  1655. return 0;
  1656. error:
  1657. vm_unacct_memory(pages);
  1658. return -ENOMEM;
  1659. }
  1660. int in_gate_area_no_mm(unsigned long addr)
  1661. {
  1662. return 0;
  1663. }
  1664. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1665. {
  1666. BUG();
  1667. return 0;
  1668. }
  1669. EXPORT_SYMBOL(filemap_fault);
  1670. static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  1671. unsigned long addr, void *buf, int len, int write)
  1672. {
  1673. struct vm_area_struct *vma;
  1674. down_read(&mm->mmap_sem);
  1675. /* the access must start within one of the target process's mappings */
  1676. vma = find_vma(mm, addr);
  1677. if (vma) {
  1678. /* don't overrun this mapping */
  1679. if (addr + len >= vma->vm_end)
  1680. len = vma->vm_end - addr;
  1681. /* only read or write mappings where it is permitted */
  1682. if (write && vma->vm_flags & VM_MAYWRITE)
  1683. copy_to_user_page(vma, NULL, addr,
  1684. (void *) addr, buf, len);
  1685. else if (!write && vma->vm_flags & VM_MAYREAD)
  1686. copy_from_user_page(vma, NULL, addr,
  1687. buf, (void *) addr, len);
  1688. else
  1689. len = 0;
  1690. } else {
  1691. len = 0;
  1692. }
  1693. up_read(&mm->mmap_sem);
  1694. return len;
  1695. }
  1696. /**
  1697. * @access_remote_vm - access another process' address space
  1698. * @mm: the mm_struct of the target address space
  1699. * @addr: start address to access
  1700. * @buf: source or destination buffer
  1701. * @len: number of bytes to transfer
  1702. * @write: whether the access is a write
  1703. *
  1704. * The caller must hold a reference on @mm.
  1705. */
  1706. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1707. void *buf, int len, int write)
  1708. {
  1709. return __access_remote_vm(NULL, mm, addr, buf, len, write);
  1710. }
  1711. /*
  1712. * Access another process' address space.
  1713. * - source/target buffer must be kernel space
  1714. */
  1715. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1716. {
  1717. struct mm_struct *mm;
  1718. if (addr + len < addr)
  1719. return 0;
  1720. mm = get_task_mm(tsk);
  1721. if (!mm)
  1722. return 0;
  1723. len = __access_remote_vm(tsk, mm, addr, buf, len, write);
  1724. mmput(mm);
  1725. return len;
  1726. }
  1727. /**
  1728. * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
  1729. * @inode: The inode to check
  1730. * @size: The current filesize of the inode
  1731. * @newsize: The proposed filesize of the inode
  1732. *
  1733. * Check the shared mappings on an inode on behalf of a shrinking truncate to
  1734. * make sure that that any outstanding VMAs aren't broken and then shrink the
  1735. * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
  1736. * automatically grant mappings that are too large.
  1737. */
  1738. int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
  1739. size_t newsize)
  1740. {
  1741. struct vm_area_struct *vma;
  1742. struct prio_tree_iter iter;
  1743. struct vm_region *region;
  1744. pgoff_t low, high;
  1745. size_t r_size, r_top;
  1746. low = newsize >> PAGE_SHIFT;
  1747. high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1748. down_write(&nommu_region_sem);
  1749. /* search for VMAs that fall within the dead zone */
  1750. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1751. low, high) {
  1752. /* found one - only interested if it's shared out of the page
  1753. * cache */
  1754. if (vma->vm_flags & VM_SHARED) {
  1755. up_write(&nommu_region_sem);
  1756. return -ETXTBSY; /* not quite true, but near enough */
  1757. }
  1758. }
  1759. /* reduce any regions that overlap the dead zone - if in existence,
  1760. * these will be pointed to by VMAs that don't overlap the dead zone
  1761. *
  1762. * we don't check for any regions that start beyond the EOF as there
  1763. * shouldn't be any
  1764. */
  1765. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1766. 0, ULONG_MAX) {
  1767. if (!(vma->vm_flags & VM_SHARED))
  1768. continue;
  1769. region = vma->vm_region;
  1770. r_size = region->vm_top - region->vm_start;
  1771. r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
  1772. if (r_top > newsize) {
  1773. region->vm_top -= r_top - newsize;
  1774. if (region->vm_end > region->vm_top)
  1775. region->vm_end = region->vm_top;
  1776. }
  1777. }
  1778. up_write(&nommu_region_sem);
  1779. return 0;
  1780. }