nommu.c 48 KB

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