nommu.c 52 KB

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