nommu.c 52 KB

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