blob: b4ac332e80723fd96a58549e95f5a86df9094503 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* internal.h: mm/ internal definitions
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
Nick Piggin0f8053a2006-03-22 00:08:33 -080011#ifndef __MM_INTERNAL_H
12#define __MM_INTERNAL_H
13
14#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Jan Beulich42b77722008-07-23 21:27:10 -070016void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
17 unsigned long floor, unsigned long ceiling);
18
Nick Piggin7835e982006-03-22 00:08:40 -080019static inline void set_page_count(struct page *page, int v)
Nick Piggin77a8a782006-01-06 00:10:57 -080020{
Nick Piggin7835e982006-03-22 00:08:40 -080021 atomic_set(&page->_count, v);
22}
23
24/*
25 * Turn a non-refcounted page (->_count == 0) into refcounted with
26 * a count of one.
27 */
28static inline void set_page_refcounted(struct page *page)
29{
Qi Yongae1276b2008-02-04 22:29:27 -080030 VM_BUG_ON(PageTail(page));
Nick Piggin725d7042006-09-25 23:30:55 -070031 VM_BUG_ON(atomic_read(&page->_count));
Nick Piggin77a8a782006-01-06 00:10:57 -080032 set_page_count(page, 1);
Nick Piggin77a8a782006-01-06 00:10:57 -080033}
34
Nick Piggin0f8053a2006-03-22 00:08:33 -080035static inline void __put_page(struct page *page)
36{
37 atomic_dec(&page->_count);
38}
39
Lee Schermerhorn894bc312008-10-18 20:26:39 -070040/*
41 * in mm/vmscan.c:
42 */
Nick Piggin62695a82008-10-18 20:26:09 -070043extern int isolate_lru_page(struct page *page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -070044extern void putback_lru_page(struct page *page);
Nick Piggin62695a82008-10-18 20:26:09 -070045
Lee Schermerhorn894bc312008-10-18 20:26:39 -070046/*
47 * in mm/page_alloc.c
48 */
Hugh Dickins22b31ee2009-01-06 14:40:09 -080049extern unsigned long highest_memmap_pfn;
Yasunori Goto0c0a4a52008-04-28 02:13:34 -070050extern void __free_pages_bootmem(struct page *page, unsigned int order);
Wu Fengguang20a03072009-06-16 15:32:22 -070051extern void prep_compound_page(struct page *page, unsigned long order);
52
Nick Piggin0f8053a2006-03-22 00:08:33 -080053
Mel Gorman48f13bf2007-10-16 01:26:10 -070054/*
55 * function for dealing with page's order in buddy system.
56 * zone->lock is already acquired when we use these.
57 * So, we don't need atomic page->flags operations here.
58 */
59static inline unsigned long page_order(struct page *page)
60{
61 VM_BUG_ON(!PageBuddy(page));
62 return page_private(page);
63}
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -080064
David Howells33925b22009-03-31 15:23:26 -070065#ifdef CONFIG_HAVE_MLOCK
Rik van Rielba470de2008-10-18 20:26:50 -070066extern long mlock_vma_pages_range(struct vm_area_struct *vma,
Nick Pigginb291f002008-10-18 20:26:44 -070067 unsigned long start, unsigned long end);
Rik van Rielba470de2008-10-18 20:26:50 -070068extern void munlock_vma_pages_range(struct vm_area_struct *vma,
69 unsigned long start, unsigned long end);
70static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
71{
72 munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
73}
David Howells33925b22009-03-31 15:23:26 -070074#endif
Nick Pigginb291f002008-10-18 20:26:44 -070075
Lee Schermerhorn894bc312008-10-18 20:26:39 -070076#ifdef CONFIG_UNEVICTABLE_LRU
77/*
78 * unevictable_migrate_page() called only from migrate_page_copy() to
79 * migrate unevictable flag to new page.
80 * Note that the old page has been isolated from the LRU lists at this
81 * point so we don't need to worry about LRU statistics.
82 */
83static inline void unevictable_migrate_page(struct page *new, struct page *old)
84{
85 if (TestClearPageUnevictable(old))
86 SetPageUnevictable(new);
87}
88#else
89static inline void unevictable_migrate_page(struct page *new, struct page *old)
90{
91}
92#endif
93
David Howells33925b22009-03-31 15:23:26 -070094#ifdef CONFIG_HAVE_MLOCKED_PAGE_BIT
Nick Pigginb291f002008-10-18 20:26:44 -070095/*
96 * Called only in fault path via page_evictable() for a new page
97 * to determine if it's being mapped into a LOCKED vma.
98 * If so, mark page as mlocked.
99 */
100static inline int is_mlocked_vma(struct vm_area_struct *vma, struct page *page)
101{
102 VM_BUG_ON(PageLRU(page));
103
104 if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
105 return 0;
106
Nick Piggin5344b7e2008-10-18 20:26:51 -0700107 if (!TestSetPageMlocked(page)) {
108 inc_zone_page_state(page, NR_MLOCK);
109 count_vm_event(UNEVICTABLE_PGMLOCKED);
110 }
Nick Pigginb291f002008-10-18 20:26:44 -0700111 return 1;
112}
113
114/*
115 * must be called with vma's mmap_sem held for read, and page locked.
116 */
117extern void mlock_vma_page(struct page *page);
118
119/*
120 * Clear the page's PageMlocked(). This can be useful in a situation where
121 * we want to unconditionally remove a page from the pagecache -- e.g.,
122 * on truncation or freeing.
123 *
124 * It is legal to call this function for any page, mlocked or not.
125 * If called for a page that is still mapped by mlocked vmas, all we do
126 * is revert to lazy LRU behaviour -- semantics are not broken.
127 */
128extern void __clear_page_mlock(struct page *page);
129static inline void clear_page_mlock(struct page *page)
130{
131 if (unlikely(TestClearPageMlocked(page)))
132 __clear_page_mlock(page);
133}
134
135/*
136 * mlock_migrate_page - called only from migrate_page_copy() to
Nick Piggin5344b7e2008-10-18 20:26:51 -0700137 * migrate the Mlocked page flag; update statistics.
Nick Pigginb291f002008-10-18 20:26:44 -0700138 */
139static inline void mlock_migrate_page(struct page *newpage, struct page *page)
140{
Nick Piggin5344b7e2008-10-18 20:26:51 -0700141 if (TestClearPageMlocked(page)) {
142 unsigned long flags;
143
144 local_irq_save(flags);
145 __dec_zone_page_state(page, NR_MLOCK);
Nick Pigginb291f002008-10-18 20:26:44 -0700146 SetPageMlocked(newpage);
Nick Piggin5344b7e2008-10-18 20:26:51 -0700147 __inc_zone_page_state(newpage, NR_MLOCK);
148 local_irq_restore(flags);
149 }
Nick Pigginb291f002008-10-18 20:26:44 -0700150}
151
David Howells33925b22009-03-31 15:23:26 -0700152#else /* CONFIG_HAVE_MLOCKED_PAGE_BIT */
Nick Pigginb291f002008-10-18 20:26:44 -0700153static inline int is_mlocked_vma(struct vm_area_struct *v, struct page *p)
154{
155 return 0;
156}
157static inline void clear_page_mlock(struct page *page) { }
158static inline void mlock_vma_page(struct page *page) { }
159static inline void mlock_migrate_page(struct page *new, struct page *old) { }
160
David Howells33925b22009-03-31 15:23:26 -0700161#endif /* CONFIG_HAVE_MLOCKED_PAGE_BIT */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700162
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -0800163/*
Andy Whitcroft69d177c2008-11-06 12:53:26 -0800164 * Return the mem_map entry representing the 'offset' subpage within
165 * the maximally aligned gigantic page 'base'. Handle any discontiguity
166 * in the mem_map at MAX_ORDER_NR_PAGES boundaries.
167 */
168static inline struct page *mem_map_offset(struct page *base, int offset)
169{
170 if (unlikely(offset >= MAX_ORDER_NR_PAGES))
171 return pfn_to_page(page_to_pfn(base) + offset);
172 return base + offset;
173}
174
175/*
176 * Iterator over all subpages withing the maximally aligned gigantic
177 * page 'base'. Handle any discontiguity in the mem_map.
178 */
179static inline struct page *mem_map_next(struct page *iter,
180 struct page *base, int offset)
181{
182 if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
183 unsigned long pfn = page_to_pfn(base) + offset;
184 if (!pfn_valid(pfn))
185 return NULL;
186 return pfn_to_page(pfn);
187 }
188 return iter + 1;
189}
190
191/*
Alexander van Heukelumb5a0e012008-02-23 15:24:06 -0800192 * FLATMEM and DISCONTIGMEM configurations use alloc_bootmem_node,
193 * so all functions starting at paging_init should be marked __init
194 * in those cases. SPARSEMEM, however, allows for memory hotplug,
195 * and alloc_bootmem_node is not used.
196 */
197#ifdef CONFIG_SPARSEMEM
198#define __paginginit __meminit
199#else
200#define __paginginit __init
201#endif
202
Mel Gorman6b74ab92008-07-23 21:26:49 -0700203/* Memory initialisation debug and verification */
204enum mminit_level {
205 MMINIT_WARNING,
206 MMINIT_VERIFY,
207 MMINIT_TRACE
208};
209
210#ifdef CONFIG_DEBUG_MEMORY_INIT
211
212extern int mminit_loglevel;
213
214#define mminit_dprintk(level, prefix, fmt, arg...) \
215do { \
216 if (level < mminit_loglevel) { \
217 printk(level <= MMINIT_WARNING ? KERN_WARNING : KERN_DEBUG); \
218 printk(KERN_CONT "mminit::" prefix " " fmt, ##arg); \
219 } \
220} while (0)
221
Mel Gorman708614e2008-07-23 21:26:51 -0700222extern void mminit_verify_pageflags_layout(void);
223extern void mminit_verify_page_links(struct page *page,
224 enum zone_type zone, unsigned long nid, unsigned long pfn);
Mel Gorman68ad8df2008-07-23 21:26:52 -0700225extern void mminit_verify_zonelist(void);
Mel Gorman708614e2008-07-23 21:26:51 -0700226
Mel Gorman6b74ab92008-07-23 21:26:49 -0700227#else
228
229static inline void mminit_dprintk(enum mminit_level level,
230 const char *prefix, const char *fmt, ...)
231{
232}
233
Mel Gorman708614e2008-07-23 21:26:51 -0700234static inline void mminit_verify_pageflags_layout(void)
235{
236}
237
238static inline void mminit_verify_page_links(struct page *page,
239 enum zone_type zone, unsigned long nid, unsigned long pfn)
240{
241}
Mel Gorman68ad8df2008-07-23 21:26:52 -0700242
243static inline void mminit_verify_zonelist(void)
244{
245}
Mel Gorman6b74ab92008-07-23 21:26:49 -0700246#endif /* CONFIG_DEBUG_MEMORY_INIT */
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700247
248/* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
249#if defined(CONFIG_SPARSEMEM)
250extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
251 unsigned long *end_pfn);
252#else
253static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
254 unsigned long *end_pfn)
255{
256}
257#endif /* CONFIG_SPARSEMEM */
258
Nick Pigginb291f002008-10-18 20:26:44 -0700259#define GUP_FLAGS_WRITE 0x1
260#define GUP_FLAGS_FORCE 0x2
261#define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
Ying Han47792802009-01-06 14:40:18 -0800262#define GUP_FLAGS_IGNORE_SIGKILL 0x8
Nick Pigginb291f002008-10-18 20:26:44 -0700263
264int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
265 unsigned long start, int len, int flags,
266 struct page **pages, struct vm_area_struct **vmas);
267
Nick Piggin0f8053a2006-03-22 00:08:33 -0800268#endif