nommu.c 48 KB

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