nommu.c 52 KB

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