nommu.c 48 KB

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