nommu.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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. 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);