nommu.c 49 KB

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