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