nommu.c 54 KB

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