nommu.c 53 KB

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