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