mm.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. #ifndef _LINUX_MM_H
  2. #define _LINUX_MM_H
  3. #include <linux/sched.h>
  4. #include <linux/errno.h>
  5. #include <linux/capability.h>
  6. #ifdef __KERNEL__
  7. #include <linux/gfp.h>
  8. #include <linux/list.h>
  9. #include <linux/mmzone.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/prio_tree.h>
  12. #include <linux/fs.h>
  13. #include <linux/mutex.h>
  14. #include <linux/debug_locks.h>
  15. struct mempolicy;
  16. struct anon_vma;
  17. #ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */
  18. extern unsigned long max_mapnr;
  19. #endif
  20. extern unsigned long num_physpages;
  21. extern void * high_memory;
  22. extern unsigned long vmalloc_earlyreserve;
  23. extern int page_cluster;
  24. #ifdef CONFIG_SYSCTL
  25. extern int sysctl_legacy_va_layout;
  26. #else
  27. #define sysctl_legacy_va_layout 0
  28. #endif
  29. #include <asm/page.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/processor.h>
  32. #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
  33. /*
  34. * Linux kernel virtual memory manager primitives.
  35. * The idea being to have a "virtual" mm in the same way
  36. * we have a virtual fs - giving a cleaner interface to the
  37. * mm details, and allowing different kinds of memory mappings
  38. * (from shared memory to executable loading to arbitrary
  39. * mmap() functions).
  40. */
  41. /*
  42. * This struct defines a memory VMM memory area. There is one of these
  43. * per VM-area/task. A VM area is any part of the process virtual memory
  44. * space that has a special rule for the page-fault handlers (ie a shared
  45. * library, the executable area etc).
  46. */
  47. struct vm_area_struct {
  48. struct mm_struct * vm_mm; /* The address space we belong to. */
  49. unsigned long vm_start; /* Our start address within vm_mm. */
  50. unsigned long vm_end; /* The first byte after our end address
  51. within vm_mm. */
  52. /* linked list of VM areas per task, sorted by address */
  53. struct vm_area_struct *vm_next;
  54. pgprot_t vm_page_prot; /* Access permissions of this VMA. */
  55. unsigned long vm_flags; /* Flags, listed below. */
  56. struct rb_node vm_rb;
  57. /*
  58. * For areas with an address space and backing store,
  59. * linkage into the address_space->i_mmap prio tree, or
  60. * linkage to the list of like vmas hanging off its node, or
  61. * linkage of vma in the address_space->i_mmap_nonlinear list.
  62. */
  63. union {
  64. struct {
  65. struct list_head list;
  66. void *parent; /* aligns with prio_tree_node parent */
  67. struct vm_area_struct *head;
  68. } vm_set;
  69. struct raw_prio_tree_node prio_tree_node;
  70. } shared;
  71. /*
  72. * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
  73. * list, after a COW of one of the file pages. A MAP_SHARED vma
  74. * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
  75. * or brk vma (with NULL file) can only be in an anon_vma list.
  76. */
  77. struct list_head anon_vma_node; /* Serialized by anon_vma->lock */
  78. struct anon_vma *anon_vma; /* Serialized by page_table_lock */
  79. /* Function pointers to deal with this struct. */
  80. struct vm_operations_struct * vm_ops;
  81. /* Information about our backing store: */
  82. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  83. units, *not* PAGE_CACHE_SIZE */
  84. struct file * vm_file; /* File we map to (can be NULL). */
  85. void * vm_private_data; /* was vm_pte (shared mem) */
  86. unsigned long vm_truncate_count;/* truncate_count or restart_addr */
  87. #ifndef CONFIG_MMU
  88. atomic_t vm_usage; /* refcount (VMAs shared if !MMU) */
  89. #endif
  90. #ifdef CONFIG_NUMA
  91. struct mempolicy *vm_policy; /* NUMA policy for the VMA */
  92. #endif
  93. };
  94. /*
  95. * This struct defines the per-mm list of VMAs for uClinux. If CONFIG_MMU is
  96. * disabled, then there's a single shared list of VMAs maintained by the
  97. * system, and mm's subscribe to these individually
  98. */
  99. struct vm_list_struct {
  100. struct vm_list_struct *next;
  101. struct vm_area_struct *vma;
  102. };
  103. #ifndef CONFIG_MMU
  104. extern struct rb_root nommu_vma_tree;
  105. extern struct rw_semaphore nommu_vma_sem;
  106. extern unsigned int kobjsize(const void *objp);
  107. #endif
  108. /*
  109. * vm_flags..
  110. */
  111. #define VM_READ 0x00000001 /* currently active flags */
  112. #define VM_WRITE 0x00000002
  113. #define VM_EXEC 0x00000004
  114. #define VM_SHARED 0x00000008
  115. /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
  116. #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
  117. #define VM_MAYWRITE 0x00000020
  118. #define VM_MAYEXEC 0x00000040
  119. #define VM_MAYSHARE 0x00000080
  120. #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
  121. #define VM_GROWSUP 0x00000200
  122. #define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
  123. #define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
  124. #define VM_EXECUTABLE 0x00001000
  125. #define VM_LOCKED 0x00002000
  126. #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
  127. /* Used by sys_madvise() */
  128. #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
  129. #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
  130. #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
  131. #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
  132. #define VM_RESERVED 0x00080000 /* Count as reserved_vm like IO */
  133. #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
  134. #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
  135. #define VM_NONLINEAR 0x00800000 /* Is non-linear (remap_file_pages) */
  136. #define VM_MAPPED_COPY 0x01000000 /* T if mapped copy of data (nommu mmap) */
  137. #define VM_INSERTPAGE 0x02000000 /* The vma has had "vm_insert_page()" done on it */
  138. #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */
  139. #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
  140. #endif
  141. #ifdef CONFIG_STACK_GROWSUP
  142. #define VM_STACK_FLAGS (VM_GROWSUP | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  143. #else
  144. #define VM_STACK_FLAGS (VM_GROWSDOWN | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  145. #endif
  146. #define VM_READHINTMASK (VM_SEQ_READ | VM_RAND_READ)
  147. #define VM_ClearReadHint(v) (v)->vm_flags &= ~VM_READHINTMASK
  148. #define VM_NormalReadHint(v) (!((v)->vm_flags & VM_READHINTMASK))
  149. #define VM_SequentialReadHint(v) ((v)->vm_flags & VM_SEQ_READ)
  150. #define VM_RandomReadHint(v) ((v)->vm_flags & VM_RAND_READ)
  151. /*
  152. * mapping from the currently active vm_flags protection bits (the
  153. * low four bits) to a page protection mask..
  154. */
  155. extern pgprot_t protection_map[16];
  156. /*
  157. * These are the virtual MM functions - opening of an area, closing and
  158. * unmapping it (needed to keep files on disk up-to-date etc), pointer
  159. * to the functions called when a no-page or a wp-page exception occurs.
  160. */
  161. struct vm_operations_struct {
  162. void (*open)(struct vm_area_struct * area);
  163. void (*close)(struct vm_area_struct * area);
  164. struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
  165. int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
  166. /* notification that a previously read-only page is about to become
  167. * writable, if an error is returned it will cause a SIGBUS */
  168. int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
  169. #ifdef CONFIG_NUMA
  170. int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
  171. struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
  172. unsigned long addr);
  173. int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from,
  174. const nodemask_t *to, unsigned long flags);
  175. #endif
  176. };
  177. struct mmu_gather;
  178. struct inode;
  179. /*
  180. * Each physical page in the system has a struct page associated with
  181. * it to keep track of whatever it is we are using the page for at the
  182. * moment. Note that we have no way to track which tasks are using
  183. * a page.
  184. */
  185. struct page {
  186. unsigned long flags; /* Atomic flags, some possibly
  187. * updated asynchronously */
  188. atomic_t _count; /* Usage count, see below. */
  189. atomic_t _mapcount; /* Count of ptes mapped in mms,
  190. * to show when page is mapped
  191. * & limit reverse map searches.
  192. */
  193. union {
  194. struct {
  195. unsigned long private; /* Mapping-private opaque data:
  196. * usually used for buffer_heads
  197. * if PagePrivate set; used for
  198. * swp_entry_t if PageSwapCache;
  199. * indicates order in the buddy
  200. * system if PG_buddy is set.
  201. */
  202. struct address_space *mapping; /* If low bit clear, points to
  203. * inode address_space, or NULL.
  204. * If page mapped as anonymous
  205. * memory, low bit is set, and
  206. * it points to anon_vma object:
  207. * see PAGE_MAPPING_ANON below.
  208. */
  209. };
  210. #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
  211. spinlock_t ptl;
  212. #endif
  213. };
  214. pgoff_t index; /* Our offset within mapping. */
  215. struct list_head lru; /* Pageout list, eg. active_list
  216. * protected by zone->lru_lock !
  217. */
  218. /*
  219. * On machines where all RAM is mapped into kernel address space,
  220. * we can simply calculate the virtual address. On machines with
  221. * highmem some memory is mapped into kernel virtual memory
  222. * dynamically, so we need a place to store that address.
  223. * Note that this field could be 16 bits on x86 ... ;)
  224. *
  225. * Architectures with slow multiplication can define
  226. * WANT_PAGE_VIRTUAL in asm/page.h
  227. */
  228. #if defined(WANT_PAGE_VIRTUAL)
  229. void *virtual; /* Kernel virtual address (NULL if
  230. not kmapped, ie. highmem) */
  231. #endif /* WANT_PAGE_VIRTUAL */
  232. };
  233. #define page_private(page) ((page)->private)
  234. #define set_page_private(page, v) ((page)->private = (v))
  235. /*
  236. * FIXME: take this include out, include page-flags.h in
  237. * files which need it (119 of them)
  238. */
  239. #include <linux/page-flags.h>
  240. /*
  241. * Methods to modify the page usage count.
  242. *
  243. * What counts for a page usage:
  244. * - cache mapping (page->mapping)
  245. * - private data (page->private)
  246. * - page mapped in a task's page tables, each mapping
  247. * is counted separately
  248. *
  249. * Also, many kernel routines increase the page count before a critical
  250. * routine so they can be sure the page doesn't go away from under them.
  251. */
  252. /*
  253. * Drop a ref, return true if the logical refcount fell to zero (the page has
  254. * no users)
  255. */
  256. static inline int put_page_testzero(struct page *page)
  257. {
  258. BUG_ON(atomic_read(&page->_count) == 0);
  259. return atomic_dec_and_test(&page->_count);
  260. }
  261. /*
  262. * Try to grab a ref unless the page has a refcount of zero, return false if
  263. * that is the case.
  264. */
  265. static inline int get_page_unless_zero(struct page *page)
  266. {
  267. return atomic_inc_not_zero(&page->_count);
  268. }
  269. extern void FASTCALL(__page_cache_release(struct page *));
  270. static inline int page_count(struct page *page)
  271. {
  272. if (unlikely(PageCompound(page)))
  273. page = (struct page *)page_private(page);
  274. return atomic_read(&page->_count);
  275. }
  276. static inline void get_page(struct page *page)
  277. {
  278. if (unlikely(PageCompound(page)))
  279. page = (struct page *)page_private(page);
  280. atomic_inc(&page->_count);
  281. }
  282. /*
  283. * Setup the page count before being freed into the page allocator for
  284. * the first time (boot or memory hotplug)
  285. */
  286. static inline void init_page_count(struct page *page)
  287. {
  288. atomic_set(&page->_count, 1);
  289. }
  290. void put_page(struct page *page);
  291. void split_page(struct page *page, unsigned int order);
  292. /*
  293. * Multiple processes may "see" the same page. E.g. for untouched
  294. * mappings of /dev/null, all processes see the same page full of
  295. * zeroes, and text pages of executables and shared libraries have
  296. * only one copy in memory, at most, normally.
  297. *
  298. * For the non-reserved pages, page_count(page) denotes a reference count.
  299. * page_count() == 0 means the page is free. page->lru is then used for
  300. * freelist management in the buddy allocator.
  301. * page_count() == 1 means the page is used for exactly one purpose
  302. * (e.g. a private data page of one process).
  303. *
  304. * A page may be used for kmalloc() or anyone else who does a
  305. * __get_free_page(). In this case the page_count() is at least 1, and
  306. * all other fields are unused but should be 0 or NULL. The
  307. * management of this page is the responsibility of the one who uses
  308. * it.
  309. *
  310. * The other pages (we may call them "process pages") are completely
  311. * managed by the Linux memory manager: I/O, buffers, swapping etc.
  312. * The following discussion applies only to them.
  313. *
  314. * A page may belong to an inode's memory mapping. In this case,
  315. * page->mapping is the pointer to the inode, and page->index is the
  316. * file offset of the page, in units of PAGE_CACHE_SIZE.
  317. *
  318. * A page contains an opaque `private' member, which belongs to the
  319. * page's address_space. Usually, this is the address of a circular
  320. * list of the page's disk buffers.
  321. *
  322. * For pages belonging to inodes, the page_count() is the number of
  323. * attaches, plus 1 if `private' contains something, plus one for
  324. * the page cache itself.
  325. *
  326. * Instead of keeping dirty/clean pages in per address-space lists, we instead
  327. * now tag pages as dirty/under writeback in the radix tree.
  328. *
  329. * There is also a per-mapping radix tree mapping index to the page
  330. * in memory if present. The tree is rooted at mapping->root.
  331. *
  332. * All process pages can do I/O:
  333. * - inode pages may need to be read from disk,
  334. * - inode pages which have been modified and are MAP_SHARED may need
  335. * to be written to disk,
  336. * - private pages which have been modified may need to be swapped out
  337. * to swap space and (later) to be read back into memory.
  338. */
  339. /*
  340. * The zone field is never updated after free_area_init_core()
  341. * sets it, so none of the operations on it need to be atomic.
  342. */
  343. /*
  344. * page->flags layout:
  345. *
  346. * There are three possibilities for how page->flags get
  347. * laid out. The first is for the normal case, without
  348. * sparsemem. The second is for sparsemem when there is
  349. * plenty of space for node and section. The last is when
  350. * we have run out of space and have to fall back to an
  351. * alternate (slower) way of determining the node.
  352. *
  353. * No sparsemem: | NODE | ZONE | ... | FLAGS |
  354. * with space for node: | SECTION | NODE | ZONE | ... | FLAGS |
  355. * no space for node: | SECTION | ZONE | ... | FLAGS |
  356. */
  357. #ifdef CONFIG_SPARSEMEM
  358. #define SECTIONS_WIDTH SECTIONS_SHIFT
  359. #else
  360. #define SECTIONS_WIDTH 0
  361. #endif
  362. #define ZONES_WIDTH ZONES_SHIFT
  363. #if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT <= FLAGS_RESERVED
  364. #define NODES_WIDTH NODES_SHIFT
  365. #else
  366. #define NODES_WIDTH 0
  367. #endif
  368. /* Page flags: | [SECTION] | [NODE] | ZONE | ... | FLAGS | */
  369. #define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
  370. #define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
  371. #define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
  372. /*
  373. * We are going to use the flags for the page to node mapping if its in
  374. * there. This includes the case where there is no node, so it is implicit.
  375. */
  376. #define FLAGS_HAS_NODE (NODES_WIDTH > 0 || NODES_SHIFT == 0)
  377. #ifndef PFN_SECTION_SHIFT
  378. #define PFN_SECTION_SHIFT 0
  379. #endif
  380. /*
  381. * Define the bit shifts to access each section. For non-existant
  382. * sections we define the shift as 0; that plus a 0 mask ensures
  383. * the compiler will optimise away reference to them.
  384. */
  385. #define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
  386. #define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
  387. #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
  388. /* NODE:ZONE or SECTION:ZONE is used to lookup the zone from a page. */
  389. #if FLAGS_HAS_NODE
  390. #define ZONETABLE_SHIFT (NODES_SHIFT + ZONES_SHIFT)
  391. #else
  392. #define ZONETABLE_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT)
  393. #endif
  394. #define ZONETABLE_PGSHIFT ZONES_PGSHIFT
  395. #if SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > FLAGS_RESERVED
  396. #error SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > FLAGS_RESERVED
  397. #endif
  398. #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
  399. #define NODES_MASK ((1UL << NODES_WIDTH) - 1)
  400. #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
  401. #define ZONETABLE_MASK ((1UL << ZONETABLE_SHIFT) - 1)
  402. static inline unsigned long page_zonenum(struct page *page)
  403. {
  404. return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
  405. }
  406. struct zone;
  407. extern struct zone *zone_table[];
  408. static inline int page_zone_id(struct page *page)
  409. {
  410. return (page->flags >> ZONETABLE_PGSHIFT) & ZONETABLE_MASK;
  411. }
  412. static inline struct zone *page_zone(struct page *page)
  413. {
  414. return zone_table[page_zone_id(page)];
  415. }
  416. static inline unsigned long page_to_nid(struct page *page)
  417. {
  418. if (FLAGS_HAS_NODE)
  419. return (page->flags >> NODES_PGSHIFT) & NODES_MASK;
  420. else
  421. return page_zone(page)->zone_pgdat->node_id;
  422. }
  423. static inline unsigned long page_to_section(struct page *page)
  424. {
  425. return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
  426. }
  427. static inline void set_page_zone(struct page *page, unsigned long zone)
  428. {
  429. page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
  430. page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
  431. }
  432. static inline void set_page_node(struct page *page, unsigned long node)
  433. {
  434. page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
  435. page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
  436. }
  437. static inline void set_page_section(struct page *page, unsigned long section)
  438. {
  439. page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
  440. page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
  441. }
  442. static inline void set_page_links(struct page *page, unsigned long zone,
  443. unsigned long node, unsigned long pfn)
  444. {
  445. set_page_zone(page, zone);
  446. set_page_node(page, node);
  447. set_page_section(page, pfn_to_section_nr(pfn));
  448. }
  449. /*
  450. * Some inline functions in vmstat.h depend on page_zone()
  451. */
  452. #include <linux/vmstat.h>
  453. #ifndef CONFIG_DISCONTIGMEM
  454. /* The array of struct pages - for discontigmem use pgdat->lmem_map */
  455. extern struct page *mem_map;
  456. #endif
  457. static __always_inline void *lowmem_page_address(struct page *page)
  458. {
  459. return __va(page_to_pfn(page) << PAGE_SHIFT);
  460. }
  461. #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
  462. #define HASHED_PAGE_VIRTUAL
  463. #endif
  464. #if defined(WANT_PAGE_VIRTUAL)
  465. #define page_address(page) ((page)->virtual)
  466. #define set_page_address(page, address) \
  467. do { \
  468. (page)->virtual = (address); \
  469. } while(0)
  470. #define page_address_init() do { } while(0)
  471. #endif
  472. #if defined(HASHED_PAGE_VIRTUAL)
  473. void *page_address(struct page *page);
  474. void set_page_address(struct page *page, void *virtual);
  475. void page_address_init(void);
  476. #endif
  477. #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
  478. #define page_address(page) lowmem_page_address(page)
  479. #define set_page_address(page, address) do { } while(0)
  480. #define page_address_init() do { } while(0)
  481. #endif
  482. /*
  483. * On an anonymous page mapped into a user virtual memory area,
  484. * page->mapping points to its anon_vma, not to a struct address_space;
  485. * with the PAGE_MAPPING_ANON bit set to distinguish it.
  486. *
  487. * Please note that, confusingly, "page_mapping" refers to the inode
  488. * address_space which maps the page from disk; whereas "page_mapped"
  489. * refers to user virtual address space into which the page is mapped.
  490. */
  491. #define PAGE_MAPPING_ANON 1
  492. extern struct address_space swapper_space;
  493. static inline struct address_space *page_mapping(struct page *page)
  494. {
  495. struct address_space *mapping = page->mapping;
  496. if (unlikely(PageSwapCache(page)))
  497. mapping = &swapper_space;
  498. else if (unlikely((unsigned long)mapping & PAGE_MAPPING_ANON))
  499. mapping = NULL;
  500. return mapping;
  501. }
  502. static inline int PageAnon(struct page *page)
  503. {
  504. return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
  505. }
  506. /*
  507. * Return the pagecache index of the passed page. Regular pagecache pages
  508. * use ->index whereas swapcache pages use ->private
  509. */
  510. static inline pgoff_t page_index(struct page *page)
  511. {
  512. if (unlikely(PageSwapCache(page)))
  513. return page_private(page);
  514. return page->index;
  515. }
  516. /*
  517. * The atomic page->_mapcount, like _count, starts from -1:
  518. * so that transitions both from it and to it can be tracked,
  519. * using atomic_inc_and_test and atomic_add_negative(-1).
  520. */
  521. static inline void reset_page_mapcount(struct page *page)
  522. {
  523. atomic_set(&(page)->_mapcount, -1);
  524. }
  525. static inline int page_mapcount(struct page *page)
  526. {
  527. return atomic_read(&(page)->_mapcount) + 1;
  528. }
  529. /*
  530. * Return true if this page is mapped into pagetables.
  531. */
  532. static inline int page_mapped(struct page *page)
  533. {
  534. return atomic_read(&(page)->_mapcount) >= 0;
  535. }
  536. /*
  537. * Error return values for the *_nopage functions
  538. */
  539. #define NOPAGE_SIGBUS (NULL)
  540. #define NOPAGE_OOM ((struct page *) (-1))
  541. /*
  542. * Different kinds of faults, as returned by handle_mm_fault().
  543. * Used to decide whether a process gets delivered SIGBUS or
  544. * just gets major/minor fault counters bumped up.
  545. */
  546. #define VM_FAULT_OOM 0x00
  547. #define VM_FAULT_SIGBUS 0x01
  548. #define VM_FAULT_MINOR 0x02
  549. #define VM_FAULT_MAJOR 0x03
  550. /*
  551. * Special case for get_user_pages.
  552. * Must be in a distinct bit from the above VM_FAULT_ flags.
  553. */
  554. #define VM_FAULT_WRITE 0x10
  555. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  556. extern void show_free_areas(void);
  557. #ifdef CONFIG_SHMEM
  558. struct page *shmem_nopage(struct vm_area_struct *vma,
  559. unsigned long address, int *type);
  560. int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new);
  561. struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  562. unsigned long addr);
  563. int shmem_lock(struct file *file, int lock, struct user_struct *user);
  564. #else
  565. #define shmem_nopage filemap_nopage
  566. static inline int shmem_lock(struct file *file, int lock,
  567. struct user_struct *user)
  568. {
  569. return 0;
  570. }
  571. static inline int shmem_set_policy(struct vm_area_struct *vma,
  572. struct mempolicy *new)
  573. {
  574. return 0;
  575. }
  576. static inline struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  577. unsigned long addr)
  578. {
  579. return NULL;
  580. }
  581. #endif
  582. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags);
  583. extern int shmem_mmap(struct file *file, struct vm_area_struct *vma);
  584. int shmem_zero_setup(struct vm_area_struct *);
  585. #ifndef CONFIG_MMU
  586. extern unsigned long shmem_get_unmapped_area(struct file *file,
  587. unsigned long addr,
  588. unsigned long len,
  589. unsigned long pgoff,
  590. unsigned long flags);
  591. #endif
  592. static inline int can_do_mlock(void)
  593. {
  594. if (capable(CAP_IPC_LOCK))
  595. return 1;
  596. if (current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur != 0)
  597. return 1;
  598. return 0;
  599. }
  600. extern int user_shm_lock(size_t, struct user_struct *);
  601. extern void user_shm_unlock(size_t, struct user_struct *);
  602. /*
  603. * Parameter block passed down to zap_pte_range in exceptional cases.
  604. */
  605. struct zap_details {
  606. struct vm_area_struct *nonlinear_vma; /* Check page->index if set */
  607. struct address_space *check_mapping; /* Check page->mapping if set */
  608. pgoff_t first_index; /* Lowest page->index to unmap */
  609. pgoff_t last_index; /* Highest page->index to unmap */
  610. spinlock_t *i_mmap_lock; /* For unmap_mapping_range: */
  611. unsigned long truncate_count; /* Compare vm_truncate_count */
  612. };
  613. struct page *vm_normal_page(struct vm_area_struct *, unsigned long, pte_t);
  614. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  615. unsigned long size, struct zap_details *);
  616. unsigned long unmap_vmas(struct mmu_gather **tlb,
  617. struct vm_area_struct *start_vma, unsigned long start_addr,
  618. unsigned long end_addr, unsigned long *nr_accounted,
  619. struct zap_details *);
  620. void free_pgd_range(struct mmu_gather **tlb, unsigned long addr,
  621. unsigned long end, unsigned long floor, unsigned long ceiling);
  622. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *start_vma,
  623. unsigned long floor, unsigned long ceiling);
  624. int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
  625. struct vm_area_struct *vma);
  626. int zeromap_page_range(struct vm_area_struct *vma, unsigned long from,
  627. unsigned long size, pgprot_t prot);
  628. void unmap_mapping_range(struct address_space *mapping,
  629. loff_t const holebegin, loff_t const holelen, int even_cows);
  630. static inline void unmap_shared_mapping_range(struct address_space *mapping,
  631. loff_t const holebegin, loff_t const holelen)
  632. {
  633. unmap_mapping_range(mapping, holebegin, holelen, 0);
  634. }
  635. extern int vmtruncate(struct inode * inode, loff_t offset);
  636. extern int vmtruncate_range(struct inode * inode, loff_t offset, loff_t end);
  637. extern int install_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t prot);
  638. extern int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long pgoff, pgprot_t prot);
  639. #ifdef CONFIG_MMU
  640. extern int __handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma,
  641. unsigned long address, int write_access);
  642. static inline int handle_mm_fault(struct mm_struct *mm,
  643. struct vm_area_struct *vma, unsigned long address,
  644. int write_access)
  645. {
  646. return __handle_mm_fault(mm, vma, address, write_access) &
  647. (~VM_FAULT_WRITE);
  648. }
  649. #else
  650. static inline int handle_mm_fault(struct mm_struct *mm,
  651. struct vm_area_struct *vma, unsigned long address,
  652. int write_access)
  653. {
  654. /* should never happen if there's no MMU */
  655. BUG();
  656. return VM_FAULT_SIGBUS;
  657. }
  658. #endif
  659. extern int make_pages_present(unsigned long addr, unsigned long end);
  660. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
  661. void install_arg_page(struct vm_area_struct *, struct page *, unsigned long);
  662. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
  663. int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
  664. void print_bad_pte(struct vm_area_struct *, pte_t, unsigned long);
  665. int __set_page_dirty_buffers(struct page *page);
  666. int __set_page_dirty_nobuffers(struct page *page);
  667. int redirty_page_for_writepage(struct writeback_control *wbc,
  668. struct page *page);
  669. int FASTCALL(set_page_dirty(struct page *page));
  670. int set_page_dirty_lock(struct page *page);
  671. int clear_page_dirty_for_io(struct page *page);
  672. extern unsigned long do_mremap(unsigned long addr,
  673. unsigned long old_len, unsigned long new_len,
  674. unsigned long flags, unsigned long new_addr);
  675. /*
  676. * Prototype to add a shrinker callback for ageable caches.
  677. *
  678. * These functions are passed a count `nr_to_scan' and a gfpmask. They should
  679. * scan `nr_to_scan' objects, attempting to free them.
  680. *
  681. * The callback must return the number of objects which remain in the cache.
  682. *
  683. * The callback will be passed nr_to_scan == 0 when the VM is querying the
  684. * cache size, so a fastpath for that case is appropriate.
  685. */
  686. typedef int (*shrinker_t)(int nr_to_scan, gfp_t gfp_mask);
  687. /*
  688. * Add an aging callback. The int is the number of 'seeks' it takes
  689. * to recreate one of the objects that these functions age.
  690. */
  691. #define DEFAULT_SEEKS 2
  692. struct shrinker;
  693. extern struct shrinker *set_shrinker(int, shrinker_t);
  694. extern void remove_shrinker(struct shrinker *shrinker);
  695. extern pte_t *FASTCALL(get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl));
  696. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
  697. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
  698. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address);
  699. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address);
  700. /*
  701. * The following ifdef needed to get the 4level-fixup.h header to work.
  702. * Remove it when 4level-fixup.h has been removed.
  703. */
  704. #if defined(CONFIG_MMU) && !defined(__ARCH_HAS_4LEVEL_HACK)
  705. static inline pud_t *pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  706. {
  707. return (unlikely(pgd_none(*pgd)) && __pud_alloc(mm, pgd, address))?
  708. NULL: pud_offset(pgd, address);
  709. }
  710. static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  711. {
  712. return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
  713. NULL: pmd_offset(pud, address);
  714. }
  715. #endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */
  716. #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
  717. /*
  718. * We tuck a spinlock to guard each pagetable page into its struct page,
  719. * at page->private, with BUILD_BUG_ON to make sure that this will not
  720. * overflow into the next struct page (as it might with DEBUG_SPINLOCK).
  721. * When freeing, reset page->mapping so free_pages_check won't complain.
  722. */
  723. #define __pte_lockptr(page) &((page)->ptl)
  724. #define pte_lock_init(_page) do { \
  725. spin_lock_init(__pte_lockptr(_page)); \
  726. } while (0)
  727. #define pte_lock_deinit(page) ((page)->mapping = NULL)
  728. #define pte_lockptr(mm, pmd) ({(void)(mm); __pte_lockptr(pmd_page(*(pmd)));})
  729. #else
  730. /*
  731. * We use mm->page_table_lock to guard all pagetable pages of the mm.
  732. */
  733. #define pte_lock_init(page) do {} while (0)
  734. #define pte_lock_deinit(page) do {} while (0)
  735. #define pte_lockptr(mm, pmd) ({(void)(pmd); &(mm)->page_table_lock;})
  736. #endif /* NR_CPUS < CONFIG_SPLIT_PTLOCK_CPUS */
  737. #define pte_offset_map_lock(mm, pmd, address, ptlp) \
  738. ({ \
  739. spinlock_t *__ptl = pte_lockptr(mm, pmd); \
  740. pte_t *__pte = pte_offset_map(pmd, address); \
  741. *(ptlp) = __ptl; \
  742. spin_lock(__ptl); \
  743. __pte; \
  744. })
  745. #define pte_unmap_unlock(pte, ptl) do { \
  746. spin_unlock(ptl); \
  747. pte_unmap(pte); \
  748. } while (0)
  749. #define pte_alloc_map(mm, pmd, address) \
  750. ((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
  751. NULL: pte_offset_map(pmd, address))
  752. #define pte_alloc_map_lock(mm, pmd, address, ptlp) \
  753. ((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
  754. NULL: pte_offset_map_lock(mm, pmd, address, ptlp))
  755. #define pte_alloc_kernel(pmd, address) \
  756. ((unlikely(!pmd_present(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
  757. NULL: pte_offset_kernel(pmd, address))
  758. extern void free_area_init(unsigned long * zones_size);
  759. extern void free_area_init_node(int nid, pg_data_t *pgdat,
  760. unsigned long * zones_size, unsigned long zone_start_pfn,
  761. unsigned long *zholes_size);
  762. extern void memmap_init_zone(unsigned long, int, unsigned long, unsigned long);
  763. extern void setup_per_zone_pages_min(void);
  764. extern void mem_init(void);
  765. extern void show_mem(void);
  766. extern void si_meminfo(struct sysinfo * val);
  767. extern void si_meminfo_node(struct sysinfo *val, int nid);
  768. #ifdef CONFIG_NUMA
  769. extern void setup_per_cpu_pageset(void);
  770. #else
  771. static inline void setup_per_cpu_pageset(void) {}
  772. #endif
  773. /* prio_tree.c */
  774. void vma_prio_tree_add(struct vm_area_struct *, struct vm_area_struct *old);
  775. void vma_prio_tree_insert(struct vm_area_struct *, struct prio_tree_root *);
  776. void vma_prio_tree_remove(struct vm_area_struct *, struct prio_tree_root *);
  777. struct vm_area_struct *vma_prio_tree_next(struct vm_area_struct *vma,
  778. struct prio_tree_iter *iter);
  779. #define vma_prio_tree_foreach(vma, iter, root, begin, end) \
  780. for (prio_tree_iter_init(iter, root, begin, end), vma = NULL; \
  781. (vma = vma_prio_tree_next(vma, iter)); )
  782. static inline void vma_nonlinear_insert(struct vm_area_struct *vma,
  783. struct list_head *list)
  784. {
  785. vma->shared.vm_set.parent = NULL;
  786. list_add_tail(&vma->shared.vm_set.list, list);
  787. }
  788. /* mmap.c */
  789. extern int __vm_enough_memory(long pages, int cap_sys_admin);
  790. extern void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  791. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert);
  792. extern struct vm_area_struct *vma_merge(struct mm_struct *,
  793. struct vm_area_struct *prev, unsigned long addr, unsigned long end,
  794. unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
  795. struct mempolicy *);
  796. extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
  797. extern int split_vma(struct mm_struct *,
  798. struct vm_area_struct *, unsigned long addr, int new_below);
  799. extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
  800. extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *,
  801. struct rb_node **, struct rb_node *);
  802. extern void unlink_file_vma(struct vm_area_struct *);
  803. extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
  804. unsigned long addr, unsigned long len, pgoff_t pgoff);
  805. extern void exit_mmap(struct mm_struct *);
  806. extern int may_expand_vm(struct mm_struct *mm, unsigned long npages);
  807. extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  808. extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  809. unsigned long len, unsigned long prot,
  810. unsigned long flag, unsigned long pgoff);
  811. static inline unsigned long do_mmap(struct file *file, unsigned long addr,
  812. unsigned long len, unsigned long prot,
  813. unsigned long flag, unsigned long offset)
  814. {
  815. unsigned long ret = -EINVAL;
  816. if ((offset + PAGE_ALIGN(len)) < offset)
  817. goto out;
  818. if (!(offset & ~PAGE_MASK))
  819. ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
  820. out:
  821. return ret;
  822. }
  823. extern int do_munmap(struct mm_struct *, unsigned long, size_t);
  824. extern unsigned long do_brk(unsigned long, unsigned long);
  825. /* filemap.c */
  826. extern unsigned long page_unuse(struct page *);
  827. extern void truncate_inode_pages(struct address_space *, loff_t);
  828. extern void truncate_inode_pages_range(struct address_space *,
  829. loff_t lstart, loff_t lend);
  830. /* generic vm_area_ops exported for stackable file systems */
  831. extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int *);
  832. extern int filemap_populate(struct vm_area_struct *, unsigned long,
  833. unsigned long, pgprot_t, unsigned long, int);
  834. /* mm/page-writeback.c */
  835. int write_one_page(struct page *page, int wait);
  836. /* readahead.c */
  837. #define VM_MAX_READAHEAD 128 /* kbytes */
  838. #define VM_MIN_READAHEAD 16 /* kbytes (includes current page) */
  839. #define VM_MAX_CACHE_HIT 256 /* max pages in a row in cache before
  840. * turning readahead off */
  841. int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
  842. pgoff_t offset, unsigned long nr_to_read);
  843. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  844. pgoff_t offset, unsigned long nr_to_read);
  845. unsigned long page_cache_readahead(struct address_space *mapping,
  846. struct file_ra_state *ra,
  847. struct file *filp,
  848. pgoff_t offset,
  849. unsigned long size);
  850. void handle_ra_miss(struct address_space *mapping,
  851. struct file_ra_state *ra, pgoff_t offset);
  852. unsigned long max_sane_readahead(unsigned long nr);
  853. /* Do stack extension */
  854. extern int expand_stack(struct vm_area_struct *vma, unsigned long address);
  855. #ifdef CONFIG_IA64
  856. extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
  857. #endif
  858. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  859. extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
  860. extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
  861. struct vm_area_struct **pprev);
  862. /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
  863. NULL if none. Assume start_addr < end_addr. */
  864. static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
  865. {
  866. struct vm_area_struct * vma = find_vma(mm,start_addr);
  867. if (vma && end_addr <= vma->vm_start)
  868. vma = NULL;
  869. return vma;
  870. }
  871. static inline unsigned long vma_pages(struct vm_area_struct *vma)
  872. {
  873. return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  874. }
  875. struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
  876. struct page *vmalloc_to_page(void *addr);
  877. unsigned long vmalloc_to_pfn(void *addr);
  878. int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
  879. unsigned long pfn, unsigned long size, pgprot_t);
  880. int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
  881. struct page *follow_page(struct vm_area_struct *, unsigned long address,
  882. unsigned int foll_flags);
  883. #define FOLL_WRITE 0x01 /* check pte is writable */
  884. #define FOLL_TOUCH 0x02 /* mark page accessed */
  885. #define FOLL_GET 0x04 /* do get_page on page */
  886. #define FOLL_ANON 0x08 /* give ZERO_PAGE if no pgtable */
  887. #ifdef CONFIG_PROC_FS
  888. void vm_stat_account(struct mm_struct *, unsigned long, struct file *, long);
  889. #else
  890. static inline void vm_stat_account(struct mm_struct *mm,
  891. unsigned long flags, struct file *file, long pages)
  892. {
  893. }
  894. #endif /* CONFIG_PROC_FS */
  895. #ifndef CONFIG_DEBUG_PAGEALLOC
  896. static inline void
  897. kernel_map_pages(struct page *page, int numpages, int enable)
  898. {
  899. if (!PageHighMem(page) && !enable)
  900. debug_check_no_locks_freed(page_address(page),
  901. numpages * PAGE_SIZE);
  902. }
  903. #endif
  904. extern struct vm_area_struct *get_gate_vma(struct task_struct *tsk);
  905. #ifdef __HAVE_ARCH_GATE_AREA
  906. int in_gate_area_no_task(unsigned long addr);
  907. int in_gate_area(struct task_struct *task, unsigned long addr);
  908. #else
  909. int in_gate_area_no_task(unsigned long addr);
  910. #define in_gate_area(task, addr) ({(void)task; in_gate_area_no_task(addr);})
  911. #endif /* __HAVE_ARCH_GATE_AREA */
  912. /* /proc/<pid>/oom_adj set to -17 protects from the oom-killer */
  913. #define OOM_DISABLE -17
  914. int drop_caches_sysctl_handler(struct ctl_table *, int, struct file *,
  915. void __user *, size_t *, loff_t *);
  916. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  917. unsigned long lru_pages);
  918. void drop_pagecache(void);
  919. void drop_slab(void);
  920. #ifndef CONFIG_MMU
  921. #define randomize_va_space 0
  922. #else
  923. extern int randomize_va_space;
  924. #endif
  925. const char *arch_vma_name(struct vm_area_struct *vma);
  926. #endif /* __KERNEL__ */
  927. #endif /* _LINUX_MM_H */