nommu.c 53 KB

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