Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef _LINUX_MMZONE_H |
2 | #define _LINUX_MMZONE_H | |
3 | ||
4 | #ifdef __KERNEL__ | |
5 | #ifndef __ASSEMBLY__ | |
6 | ||
7 | #include <linux/config.h> | |
8 | #include <linux/spinlock.h> | |
9 | #include <linux/list.h> | |
10 | #include <linux/wait.h> | |
11 | #include <linux/cache.h> | |
12 | #include <linux/threads.h> | |
13 | #include <linux/numa.h> | |
14 | #include <linux/init.h> | |
bdc8cb98 | 15 | #include <linux/seqlock.h> |
1da177e4 LT |
16 | #include <asm/atomic.h> |
17 | ||
18 | /* Free memory management - zoned buddy allocator. */ | |
19 | #ifndef CONFIG_FORCE_MAX_ZONEORDER | |
20 | #define MAX_ORDER 11 | |
21 | #else | |
22 | #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER | |
23 | #endif | |
24 | ||
25 | struct free_area { | |
26 | struct list_head free_list; | |
27 | unsigned long nr_free; | |
28 | }; | |
29 | ||
30 | struct pglist_data; | |
31 | ||
32 | /* | |
33 | * zone->lock and zone->lru_lock are two of the hottest locks in the kernel. | |
34 | * So add a wild amount of padding here to ensure that they fall into separate | |
35 | * cachelines. There are very few zone structures in the machine, so space | |
36 | * consumption is not a concern here. | |
37 | */ | |
38 | #if defined(CONFIG_SMP) | |
39 | struct zone_padding { | |
40 | char x[0]; | |
22fc6ecc | 41 | } ____cacheline_internodealigned_in_smp; |
1da177e4 LT |
42 | #define ZONE_PADDING(name) struct zone_padding name; |
43 | #else | |
44 | #define ZONE_PADDING(name) | |
45 | #endif | |
46 | ||
47 | struct per_cpu_pages { | |
48 | int count; /* number of pages in the list */ | |
1da177e4 LT |
49 | int high; /* high watermark, emptying needed */ |
50 | int batch; /* chunk size for buddy add/remove */ | |
51 | struct list_head list; /* the list of pages */ | |
52 | }; | |
53 | ||
54 | struct per_cpu_pageset { | |
55 | struct per_cpu_pages pcp[2]; /* 0: hot. 1: cold */ | |
56 | #ifdef CONFIG_NUMA | |
57 | unsigned long numa_hit; /* allocated in intended node */ | |
58 | unsigned long numa_miss; /* allocated in non intended node */ | |
59 | unsigned long numa_foreign; /* was intended here, hit elsewhere */ | |
60 | unsigned long interleave_hit; /* interleaver prefered this zone */ | |
61 | unsigned long local_node; /* allocation from local node */ | |
62 | unsigned long other_node; /* allocation from other node */ | |
63 | #endif | |
64 | } ____cacheline_aligned_in_smp; | |
65 | ||
e7c8d5c9 CL |
66 | #ifdef CONFIG_NUMA |
67 | #define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)]) | |
68 | #else | |
69 | #define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)]) | |
70 | #endif | |
71 | ||
1da177e4 | 72 | #define ZONE_DMA 0 |
a2f1b424 AK |
73 | #define ZONE_DMA32 1 |
74 | #define ZONE_NORMAL 2 | |
75 | #define ZONE_HIGHMEM 3 | |
1da177e4 | 76 | |
a2f1b424 | 77 | #define MAX_NR_ZONES 4 /* Sync this with ZONES_SHIFT */ |
1da177e4 LT |
78 | #define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */ |
79 | ||
80 | ||
81 | /* | |
82 | * When a memory allocation must conform to specific limitations (such | |
83 | * as being suitable for DMA) the caller will pass in hints to the | |
84 | * allocator in the gfp_mask, in the zone modifier bits. These bits | |
85 | * are used to select a priority ordered list of memory zones which | |
86 | * match the requested limits. GFP_ZONEMASK defines which bits within | |
87 | * the gfp_mask should be considered as zone modifiers. Each valid | |
88 | * combination of the zone modifier bits has a corresponding list | |
89 | * of zones (in node_zonelists). Thus for two zone modifiers there | |
90 | * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will | |
91 | * be 8 (2 ** 3) zonelists. GFP_ZONETYPES defines the number of possible | |
92 | * combinations of zone modifiers in "zone modifier space". | |
ac3461ad | 93 | * |
79046ae0 AW |
94 | * As an optimisation any zone modifier bits which are only valid when |
95 | * no other zone modifier bits are set (loners) should be placed in | |
96 | * the highest order bits of this field. This allows us to reduce the | |
97 | * extent of the zonelists thus saving space. For example in the case | |
98 | * of three zone modifier bits, we could require up to eight zonelists. | |
99 | * If the left most zone modifier is a "loner" then the highest valid | |
100 | * zonelist would be four allowing us to allocate only five zonelists. | |
101 | * | |
ac3461ad | 102 | * NOTE! Make sure this matches the zones in <linux/gfp.h> |
1da177e4 | 103 | */ |
ac3461ad LT |
104 | #define GFP_ZONEMASK 0x07 |
105 | #define GFP_ZONETYPES 5 | |
1da177e4 LT |
106 | |
107 | /* | |
108 | * On machines where it is needed (eg PCs) we divide physical memory | |
1f6818b9 | 109 | * into multiple physical zones. On a 32bit PC we have 4 zones: |
1da177e4 LT |
110 | * |
111 | * ZONE_DMA < 16 MB ISA DMA capable memory | |
a2f1b424 | 112 | * ZONE_DMA32 0 MB Empty |
1da177e4 LT |
113 | * ZONE_NORMAL 16-896 MB direct mapped by the kernel |
114 | * ZONE_HIGHMEM > 896 MB only page cache and user processes | |
115 | */ | |
116 | ||
117 | struct zone { | |
118 | /* Fields commonly accessed by the page allocator */ | |
119 | unsigned long free_pages; | |
120 | unsigned long pages_min, pages_low, pages_high; | |
121 | /* | |
122 | * We don't know if the memory that we're going to allocate will be freeable | |
123 | * or/and it will be released eventually, so to avoid totally wasting several | |
124 | * GB of ram we must reserve some of the lower zone memory (otherwise we risk | |
125 | * to run OOM on the lower zones despite there's tons of freeable ram | |
126 | * on the higher zones). This array is recalculated at runtime if the | |
127 | * sysctl_lowmem_reserve_ratio sysctl changes. | |
128 | */ | |
129 | unsigned long lowmem_reserve[MAX_NR_ZONES]; | |
130 | ||
e7c8d5c9 CL |
131 | #ifdef CONFIG_NUMA |
132 | struct per_cpu_pageset *pageset[NR_CPUS]; | |
133 | #else | |
1da177e4 | 134 | struct per_cpu_pageset pageset[NR_CPUS]; |
e7c8d5c9 | 135 | #endif |
1da177e4 LT |
136 | /* |
137 | * free areas of different sizes | |
138 | */ | |
139 | spinlock_t lock; | |
bdc8cb98 DH |
140 | #ifdef CONFIG_MEMORY_HOTPLUG |
141 | /* see spanned/present_pages for more description */ | |
142 | seqlock_t span_seqlock; | |
143 | #endif | |
1da177e4 LT |
144 | struct free_area free_area[MAX_ORDER]; |
145 | ||
146 | ||
147 | ZONE_PADDING(_pad1_) | |
148 | ||
149 | /* Fields commonly accessed by the page reclaim scanner */ | |
150 | spinlock_t lru_lock; | |
151 | struct list_head active_list; | |
152 | struct list_head inactive_list; | |
153 | unsigned long nr_scan_active; | |
154 | unsigned long nr_scan_inactive; | |
155 | unsigned long nr_active; | |
156 | unsigned long nr_inactive; | |
157 | unsigned long pages_scanned; /* since last reclaim */ | |
158 | int all_unreclaimable; /* All pages pinned */ | |
159 | ||
1e7e5a90 MH |
160 | /* A count of how many reclaimers are scanning this zone */ |
161 | atomic_t reclaim_in_progress; | |
753ee728 | 162 | |
9eeff239 CL |
163 | /* |
164 | * timestamp (in jiffies) of the last zone reclaim that did not | |
165 | * result in freeing of pages. This is used to avoid repeated scans | |
166 | * if all memory in the zone is in use. | |
167 | */ | |
168 | unsigned long last_unsuccessful_zone_reclaim; | |
169 | ||
1da177e4 LT |
170 | /* |
171 | * prev_priority holds the scanning priority for this zone. It is | |
172 | * defined as the scanning priority at which we achieved our reclaim | |
173 | * target at the previous try_to_free_pages() or balance_pgdat() | |
174 | * invokation. | |
175 | * | |
176 | * We use prev_priority as a measure of how much stress page reclaim is | |
177 | * under - it drives the swappiness decision: whether to unmap mapped | |
178 | * pages. | |
179 | * | |
180 | * temp_priority is used to remember the scanning priority at which | |
181 | * this zone was successfully refilled to free_pages == pages_high. | |
182 | * | |
183 | * Access to both these fields is quite racy even on uniprocessor. But | |
184 | * it is expected to average out OK. | |
185 | */ | |
186 | int temp_priority; | |
187 | int prev_priority; | |
188 | ||
189 | ||
190 | ZONE_PADDING(_pad2_) | |
191 | /* Rarely used or read-mostly fields */ | |
192 | ||
193 | /* | |
194 | * wait_table -- the array holding the hash table | |
195 | * wait_table_size -- the size of the hash table array | |
196 | * wait_table_bits -- wait_table_size == (1 << wait_table_bits) | |
197 | * | |
198 | * The purpose of all these is to keep track of the people | |
199 | * waiting for a page to become available and make them | |
200 | * runnable again when possible. The trouble is that this | |
201 | * consumes a lot of space, especially when so few things | |
202 | * wait on pages at a given time. So instead of using | |
203 | * per-page waitqueues, we use a waitqueue hash table. | |
204 | * | |
205 | * The bucket discipline is to sleep on the same queue when | |
206 | * colliding and wake all in that wait queue when removing. | |
207 | * When something wakes, it must check to be sure its page is | |
208 | * truly available, a la thundering herd. The cost of a | |
209 | * collision is great, but given the expected load of the | |
210 | * table, they should be so rare as to be outweighed by the | |
211 | * benefits from the saved space. | |
212 | * | |
213 | * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the | |
214 | * primary users of these fields, and in mm/page_alloc.c | |
215 | * free_area_init_core() performs the initialization of them. | |
216 | */ | |
217 | wait_queue_head_t * wait_table; | |
218 | unsigned long wait_table_size; | |
219 | unsigned long wait_table_bits; | |
220 | ||
221 | /* | |
222 | * Discontig memory support fields. | |
223 | */ | |
224 | struct pglist_data *zone_pgdat; | |
225 | struct page *zone_mem_map; | |
226 | /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */ | |
227 | unsigned long zone_start_pfn; | |
228 | ||
bdc8cb98 DH |
229 | /* |
230 | * zone_start_pfn, spanned_pages and present_pages are all | |
231 | * protected by span_seqlock. It is a seqlock because it has | |
232 | * to be read outside of zone->lock, and it is done in the main | |
233 | * allocator path. But, it is written quite infrequently. | |
234 | * | |
235 | * The lock is declared along with zone->lock because it is | |
236 | * frequently read in proximity to zone->lock. It's good to | |
237 | * give them a chance of being in the same cacheline. | |
238 | */ | |
1da177e4 LT |
239 | unsigned long spanned_pages; /* total size, including holes */ |
240 | unsigned long present_pages; /* amount of memory (excluding holes) */ | |
241 | ||
242 | /* | |
243 | * rarely used fields: | |
244 | */ | |
245 | char *name; | |
22fc6ecc | 246 | } ____cacheline_internodealigned_in_smp; |
1da177e4 LT |
247 | |
248 | ||
249 | /* | |
250 | * The "priority" of VM scanning is how much of the queues we will scan in one | |
251 | * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the | |
252 | * queues ("queue_length >> 12") during an aging round. | |
253 | */ | |
254 | #define DEF_PRIORITY 12 | |
255 | ||
256 | /* | |
257 | * One allocation request operates on a zonelist. A zonelist | |
258 | * is a list of zones, the first one is the 'goal' of the | |
259 | * allocation, the other zones are fallback zones, in decreasing | |
260 | * priority. | |
261 | * | |
262 | * Right now a zonelist takes up less than a cacheline. We never | |
263 | * modify it apart from boot-up, and only a few indices are used, | |
264 | * so despite the zonelist table being relatively big, the cache | |
265 | * footprint of this construct is very small. | |
266 | */ | |
267 | struct zonelist { | |
268 | struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited | |
269 | }; | |
270 | ||
271 | ||
272 | /* | |
273 | * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM | |
274 | * (mostly NUMA machines?) to denote a higher-level memory zone than the | |
275 | * zone denotes. | |
276 | * | |
277 | * On NUMA machines, each NUMA node would have a pg_data_t to describe | |
278 | * it's memory layout. | |
279 | * | |
280 | * Memory statistics and page replacement data structures are maintained on a | |
281 | * per-zone basis. | |
282 | */ | |
283 | struct bootmem_data; | |
284 | typedef struct pglist_data { | |
285 | struct zone node_zones[MAX_NR_ZONES]; | |
286 | struct zonelist node_zonelists[GFP_ZONETYPES]; | |
287 | int nr_zones; | |
d41dee36 | 288 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
1da177e4 | 289 | struct page *node_mem_map; |
d41dee36 | 290 | #endif |
1da177e4 | 291 | struct bootmem_data *bdata; |
208d54e5 DH |
292 | #ifdef CONFIG_MEMORY_HOTPLUG |
293 | /* | |
294 | * Must be held any time you expect node_start_pfn, node_present_pages | |
295 | * or node_spanned_pages stay constant. Holding this will also | |
296 | * guarantee that any pfn_valid() stays that way. | |
297 | * | |
298 | * Nests above zone->lock and zone->size_seqlock. | |
299 | */ | |
300 | spinlock_t node_size_lock; | |
301 | #endif | |
1da177e4 LT |
302 | unsigned long node_start_pfn; |
303 | unsigned long node_present_pages; /* total number of physical pages */ | |
304 | unsigned long node_spanned_pages; /* total size of physical page | |
305 | range, including holes */ | |
306 | int node_id; | |
307 | struct pglist_data *pgdat_next; | |
308 | wait_queue_head_t kswapd_wait; | |
309 | struct task_struct *kswapd; | |
310 | int kswapd_max_order; | |
311 | } pg_data_t; | |
312 | ||
313 | #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages) | |
314 | #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages) | |
d41dee36 | 315 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
408fde81 | 316 | #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr)) |
d41dee36 AW |
317 | #else |
318 | #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr)) | |
319 | #endif | |
408fde81 | 320 | #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr)) |
1da177e4 | 321 | |
208d54e5 DH |
322 | #include <linux/memory_hotplug.h> |
323 | ||
1da177e4 LT |
324 | extern struct pglist_data *pgdat_list; |
325 | ||
326 | void __get_zone_counts(unsigned long *active, unsigned long *inactive, | |
327 | unsigned long *free, struct pglist_data *pgdat); | |
328 | void get_zone_counts(unsigned long *active, unsigned long *inactive, | |
329 | unsigned long *free); | |
330 | void build_all_zonelists(void); | |
331 | void wakeup_kswapd(struct zone *zone, int order); | |
332 | int zone_watermark_ok(struct zone *z, int order, unsigned long mark, | |
7fb1d9fc | 333 | int classzone_idx, int alloc_flags); |
1da177e4 LT |
334 | |
335 | #ifdef CONFIG_HAVE_MEMORY_PRESENT | |
336 | void memory_present(int nid, unsigned long start, unsigned long end); | |
337 | #else | |
338 | static inline void memory_present(int nid, unsigned long start, unsigned long end) {} | |
339 | #endif | |
340 | ||
341 | #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE | |
342 | unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long); | |
343 | #endif | |
344 | ||
345 | /* | |
346 | * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc. | |
347 | */ | |
348 | #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones) | |
349 | ||
350 | /** | |
351 | * for_each_pgdat - helper macro to iterate over all nodes | |
352 | * @pgdat - pointer to a pg_data_t variable | |
353 | * | |
354 | * Meant to help with common loops of the form | |
355 | * pgdat = pgdat_list; | |
356 | * while(pgdat) { | |
357 | * ... | |
358 | * pgdat = pgdat->pgdat_next; | |
359 | * } | |
360 | */ | |
361 | #define for_each_pgdat(pgdat) \ | |
362 | for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next) | |
363 | ||
364 | /* | |
365 | * next_zone - helper magic for for_each_zone() | |
366 | * Thanks to William Lee Irwin III for this piece of ingenuity. | |
367 | */ | |
368 | static inline struct zone *next_zone(struct zone *zone) | |
369 | { | |
370 | pg_data_t *pgdat = zone->zone_pgdat; | |
371 | ||
372 | if (zone < pgdat->node_zones + MAX_NR_ZONES - 1) | |
373 | zone++; | |
374 | else if (pgdat->pgdat_next) { | |
375 | pgdat = pgdat->pgdat_next; | |
376 | zone = pgdat->node_zones; | |
377 | } else | |
378 | zone = NULL; | |
379 | ||
380 | return zone; | |
381 | } | |
382 | ||
383 | /** | |
384 | * for_each_zone - helper macro to iterate over all memory zones | |
385 | * @zone - pointer to struct zone variable | |
386 | * | |
387 | * The user only needs to declare the zone variable, for_each_zone | |
388 | * fills it in. This basically means for_each_zone() is an | |
389 | * easier to read version of this piece of code: | |
390 | * | |
391 | * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next) | |
392 | * for (i = 0; i < MAX_NR_ZONES; ++i) { | |
393 | * struct zone * z = pgdat->node_zones + i; | |
394 | * ... | |
395 | * } | |
396 | * } | |
397 | */ | |
398 | #define for_each_zone(zone) \ | |
399 | for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone)) | |
400 | ||
f3fe6512 CK |
401 | static inline int populated_zone(struct zone *zone) |
402 | { | |
403 | return (!!zone->present_pages); | |
404 | } | |
405 | ||
1da177e4 LT |
406 | static inline int is_highmem_idx(int idx) |
407 | { | |
408 | return (idx == ZONE_HIGHMEM); | |
409 | } | |
410 | ||
411 | static inline int is_normal_idx(int idx) | |
412 | { | |
413 | return (idx == ZONE_NORMAL); | |
414 | } | |
9328b8fa | 415 | |
1da177e4 LT |
416 | /** |
417 | * is_highmem - helper function to quickly check if a struct zone is a | |
418 | * highmem zone or not. This is an attempt to keep references | |
419 | * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum. | |
420 | * @zone - pointer to struct zone variable | |
421 | */ | |
422 | static inline int is_highmem(struct zone *zone) | |
423 | { | |
424 | return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM; | |
425 | } | |
426 | ||
427 | static inline int is_normal(struct zone *zone) | |
428 | { | |
429 | return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL; | |
430 | } | |
431 | ||
9328b8fa NP |
432 | static inline int is_dma32(struct zone *zone) |
433 | { | |
434 | return zone == zone->zone_pgdat->node_zones + ZONE_DMA32; | |
435 | } | |
436 | ||
437 | static inline int is_dma(struct zone *zone) | |
438 | { | |
439 | return zone == zone->zone_pgdat->node_zones + ZONE_DMA; | |
440 | } | |
441 | ||
1da177e4 LT |
442 | /* These two functions are used to setup the per zone pages min values */ |
443 | struct ctl_table; | |
444 | struct file; | |
445 | int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *, | |
446 | void __user *, size_t *, loff_t *); | |
447 | extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1]; | |
448 | int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *, | |
449 | void __user *, size_t *, loff_t *); | |
8ad4b1fb RS |
450 | int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file *, |
451 | void __user *, size_t *, loff_t *); | |
1da177e4 LT |
452 | |
453 | #include <linux/topology.h> | |
454 | /* Returns the number of the current Node. */ | |
69d81fcd | 455 | #ifndef numa_node_id |
39c715b7 | 456 | #define numa_node_id() (cpu_to_node(raw_smp_processor_id())) |
69d81fcd | 457 | #endif |
1da177e4 | 458 | |
93b7504e | 459 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
460 | |
461 | extern struct pglist_data contig_page_data; | |
462 | #define NODE_DATA(nid) (&contig_page_data) | |
463 | #define NODE_MEM_MAP(nid) mem_map | |
464 | #define MAX_NODES_SHIFT 1 | |
1da177e4 | 465 | |
93b7504e | 466 | #else /* CONFIG_NEED_MULTIPLE_NODES */ |
1da177e4 LT |
467 | |
468 | #include <asm/mmzone.h> | |
469 | ||
93b7504e | 470 | #endif /* !CONFIG_NEED_MULTIPLE_NODES */ |
348f8b6c | 471 | |
d41dee36 AW |
472 | #ifdef CONFIG_SPARSEMEM |
473 | #include <asm/sparsemem.h> | |
474 | #endif | |
475 | ||
07808b74 | 476 | #if BITS_PER_LONG == 32 |
1da177e4 | 477 | /* |
a2f1b424 AK |
478 | * with 32 bit page->flags field, we reserve 9 bits for node/zone info. |
479 | * there are 4 zones (3 bits) and this leaves 9-3=6 bits for nodes. | |
1da177e4 | 480 | */ |
a2f1b424 | 481 | #define FLAGS_RESERVED 9 |
348f8b6c | 482 | |
1da177e4 LT |
483 | #elif BITS_PER_LONG == 64 |
484 | /* | |
485 | * with 64 bit flags field, there's plenty of room. | |
486 | */ | |
348f8b6c | 487 | #define FLAGS_RESERVED 32 |
1da177e4 | 488 | |
348f8b6c | 489 | #else |
1da177e4 | 490 | |
348f8b6c | 491 | #error BITS_PER_LONG not defined |
1da177e4 | 492 | |
1da177e4 LT |
493 | #endif |
494 | ||
b159d43f AW |
495 | #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID |
496 | #define early_pfn_to_nid(nid) (0UL) | |
497 | #endif | |
498 | ||
2bdaf115 AW |
499 | #ifdef CONFIG_FLATMEM |
500 | #define pfn_to_nid(pfn) (0) | |
501 | #endif | |
502 | ||
d41dee36 AW |
503 | #define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT) |
504 | #define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT) | |
505 | ||
506 | #ifdef CONFIG_SPARSEMEM | |
507 | ||
508 | /* | |
509 | * SECTION_SHIFT #bits space required to store a section # | |
510 | * | |
511 | * PA_SECTION_SHIFT physical address to/from section number | |
512 | * PFN_SECTION_SHIFT pfn to/from section number | |
513 | */ | |
514 | #define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS) | |
515 | ||
516 | #define PA_SECTION_SHIFT (SECTION_SIZE_BITS) | |
517 | #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT) | |
518 | ||
519 | #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT) | |
520 | ||
521 | #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT) | |
522 | #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1)) | |
523 | ||
524 | #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS | |
525 | #error Allocator MAX_ORDER exceeds SECTION_SIZE | |
526 | #endif | |
527 | ||
528 | struct page; | |
529 | struct mem_section { | |
29751f69 AW |
530 | /* |
531 | * This is, logically, a pointer to an array of struct | |
532 | * pages. However, it is stored with some other magic. | |
533 | * (see sparse.c::sparse_init_one_section()) | |
534 | * | |
535 | * Making it a UL at least makes someone do a cast | |
536 | * before using it wrong. | |
537 | */ | |
538 | unsigned long section_mem_map; | |
d41dee36 AW |
539 | }; |
540 | ||
3e347261 BP |
541 | #ifdef CONFIG_SPARSEMEM_EXTREME |
542 | #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section)) | |
543 | #else | |
544 | #define SECTIONS_PER_ROOT 1 | |
545 | #endif | |
802f192e | 546 | |
3e347261 BP |
547 | #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT) |
548 | #define NR_SECTION_ROOTS (NR_MEM_SECTIONS / SECTIONS_PER_ROOT) | |
549 | #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1) | |
802f192e | 550 | |
3e347261 BP |
551 | #ifdef CONFIG_SPARSEMEM_EXTREME |
552 | extern struct mem_section *mem_section[NR_SECTION_ROOTS]; | |
802f192e | 553 | #else |
3e347261 BP |
554 | extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]; |
555 | #endif | |
d41dee36 | 556 | |
29751f69 AW |
557 | static inline struct mem_section *__nr_to_section(unsigned long nr) |
558 | { | |
3e347261 BP |
559 | if (!mem_section[SECTION_NR_TO_ROOT(nr)]) |
560 | return NULL; | |
561 | return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK]; | |
29751f69 | 562 | } |
4ca644d9 | 563 | extern int __section_nr(struct mem_section* ms); |
29751f69 AW |
564 | |
565 | /* | |
566 | * We use the lower bits of the mem_map pointer to store | |
567 | * a little bit of information. There should be at least | |
568 | * 3 bits here due to 32-bit alignment. | |
569 | */ | |
570 | #define SECTION_MARKED_PRESENT (1UL<<0) | |
571 | #define SECTION_HAS_MEM_MAP (1UL<<1) | |
572 | #define SECTION_MAP_LAST_BIT (1UL<<2) | |
573 | #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1)) | |
574 | ||
575 | static inline struct page *__section_mem_map_addr(struct mem_section *section) | |
576 | { | |
577 | unsigned long map = section->section_mem_map; | |
578 | map &= SECTION_MAP_MASK; | |
579 | return (struct page *)map; | |
580 | } | |
581 | ||
582 | static inline int valid_section(struct mem_section *section) | |
583 | { | |
802f192e | 584 | return (section && (section->section_mem_map & SECTION_MARKED_PRESENT)); |
29751f69 AW |
585 | } |
586 | ||
587 | static inline int section_has_mem_map(struct mem_section *section) | |
588 | { | |
802f192e | 589 | return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP)); |
29751f69 AW |
590 | } |
591 | ||
592 | static inline int valid_section_nr(unsigned long nr) | |
593 | { | |
594 | return valid_section(__nr_to_section(nr)); | |
595 | } | |
596 | ||
d41dee36 AW |
597 | static inline struct mem_section *__pfn_to_section(unsigned long pfn) |
598 | { | |
29751f69 | 599 | return __nr_to_section(pfn_to_section_nr(pfn)); |
d41dee36 AW |
600 | } |
601 | ||
602 | #define pfn_to_page(pfn) \ | |
603 | ({ \ | |
604 | unsigned long __pfn = (pfn); \ | |
29751f69 | 605 | __section_mem_map_addr(__pfn_to_section(__pfn)) + __pfn; \ |
d41dee36 AW |
606 | }) |
607 | #define page_to_pfn(page) \ | |
608 | ({ \ | |
29751f69 AW |
609 | page - __section_mem_map_addr(__nr_to_section( \ |
610 | page_to_section(page))); \ | |
d41dee36 AW |
611 | }) |
612 | ||
613 | static inline int pfn_valid(unsigned long pfn) | |
614 | { | |
615 | if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) | |
616 | return 0; | |
29751f69 | 617 | return valid_section(__nr_to_section(pfn_to_section_nr(pfn))); |
d41dee36 AW |
618 | } |
619 | ||
620 | /* | |
621 | * These are _only_ used during initialisation, therefore they | |
622 | * can use __initdata ... They could have names to indicate | |
623 | * this restriction. | |
624 | */ | |
625 | #ifdef CONFIG_NUMA | |
161599ff AW |
626 | #define pfn_to_nid(pfn) \ |
627 | ({ \ | |
628 | unsigned long __pfn_to_nid_pfn = (pfn); \ | |
629 | page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \ | |
630 | }) | |
2bdaf115 AW |
631 | #else |
632 | #define pfn_to_nid(pfn) (0) | |
d41dee36 AW |
633 | #endif |
634 | ||
d41dee36 AW |
635 | #define early_pfn_valid(pfn) pfn_valid(pfn) |
636 | void sparse_init(void); | |
637 | #else | |
638 | #define sparse_init() do {} while (0) | |
28ae55c9 | 639 | #define sparse_index_init(_sec, _nid) do {} while (0) |
d41dee36 AW |
640 | #endif /* CONFIG_SPARSEMEM */ |
641 | ||
642 | #ifndef early_pfn_valid | |
643 | #define early_pfn_valid(pfn) (1) | |
644 | #endif | |
645 | ||
646 | void memory_present(int nid, unsigned long start, unsigned long end); | |
647 | unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long); | |
648 | ||
1da177e4 LT |
649 | #endif /* !__ASSEMBLY__ */ |
650 | #endif /* __KERNEL__ */ | |
651 | #endif /* _LINUX_MMZONE_H */ |