nommu.c 52 KB

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