1 #ifndef _LINUX_MMZONE_H
2 #define _LINUX_MMZONE_H
5 #ifndef __GENERATING_BOUNDS_H
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/wait.h>
10 #include <linux/bitops.h>
11 #include <linux/cache.h>
12 #include <linux/threads.h>
13 #include <linux/numa.h>
14 #include <linux/init.h>
15 #include <linux/seqlock.h>
16 #include <linux/nodemask.h>
17 #include <linux/pageblock-flags.h>
18 #include <linux/bounds.h>
19 #include <asm/atomic.h>
22 /* Free memory management - zoned buddy allocator. */
23 #ifndef CONFIG_FORCE_MAX_ZONEORDER
26 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
28 #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
31 * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
32 * costly to service. That is between allocation orders which should
33 * coelesce naturally under reasonable reclaim pressure and those which
36 #define PAGE_ALLOC_COSTLY_ORDER 3
38 #define MIGRATE_UNMOVABLE 0
39 #define MIGRATE_RECLAIMABLE 1
40 #define MIGRATE_MOVABLE 2
41 #define MIGRATE_RESERVE 3
42 #define MIGRATE_ISOLATE 4 /* can't allocate from here */
43 #define MIGRATE_TYPES 5
45 #define for_each_migratetype_order(order, type) \
46 for (order = 0; order < MAX_ORDER; order++) \
47 for (type = 0; type < MIGRATE_TYPES; type++)
49 extern int page_group_by_mobility_disabled
;
51 static inline int get_pageblock_migratetype(struct page
*page
)
53 if (unlikely(page_group_by_mobility_disabled
))
54 return MIGRATE_UNMOVABLE
;
56 return get_pageblock_flags_group(page
, PB_migrate
, PB_migrate_end
);
60 struct list_head free_list
[MIGRATE_TYPES
];
61 unsigned long nr_free
;
67 * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
68 * So add a wild amount of padding here to ensure that they fall into separate
69 * cachelines. There are very few zone structures in the machine, so space
70 * consumption is not a concern here.
72 #if defined(CONFIG_SMP)
75 } ____cacheline_internodealigned_in_smp
;
76 #define ZONE_PADDING(name) struct zone_padding name;
78 #define ZONE_PADDING(name)
82 /* First 128 byte cacheline (assuming 64 bit words) */
85 NR_INACTIVE
= NR_LRU_BASE
, /* must match order of LRU_[IN]ACTIVE */
86 NR_ACTIVE
, /* " " " " " */
87 NR_ANON_PAGES
, /* Mapped anonymous pages */
88 NR_FILE_MAPPED
, /* pagecache pages mapped into pagetables.
89 only modified from process context */
93 /* Second 128 byte cacheline */
95 NR_SLAB_UNRECLAIMABLE
,
96 NR_PAGETABLE
, /* used for pagetables */
97 NR_UNSTABLE_NFS
, /* NFS unstable pages */
100 NR_WRITEBACK_TEMP
, /* Writeback using temporary buffers */
102 NUMA_HIT
, /* allocated in intended node */
103 NUMA_MISS
, /* allocated in non intended node */
104 NUMA_FOREIGN
, /* was intended here, hit elsewhere */
105 NUMA_INTERLEAVE_HIT
, /* interleaver preferred this zone */
106 NUMA_LOCAL
, /* allocation from local node */
107 NUMA_OTHER
, /* allocation from other node */
109 NR_VM_ZONE_STAT_ITEMS
};
113 LRU_INACTIVE
=LRU_BASE
, /* must match order of NR_[IN]ACTIVE */
114 LRU_ACTIVE
, /* " " " " " */
117 #define for_each_lru(l) for (l = 0; l < NR_LRU_LISTS; l++)
119 static inline int is_active_lru(enum lru_list l
)
121 return (l
== LRU_ACTIVE
);
124 struct per_cpu_pages
{
125 int count
; /* number of pages in the list */
126 int high
; /* high watermark, emptying needed */
127 int batch
; /* chunk size for buddy add/remove */
128 struct list_head list
; /* the list of pages */
131 struct per_cpu_pageset
{
132 struct per_cpu_pages pcp
;
138 s8 vm_stat_diff
[NR_VM_ZONE_STAT_ITEMS
];
140 } ____cacheline_aligned_in_smp
;
143 #define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)])
145 #define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)])
148 #endif /* !__GENERATING_BOUNDS.H */
151 #ifdef CONFIG_ZONE_DMA
153 * ZONE_DMA is used when there are devices that are not able
154 * to do DMA to all of addressable memory (ZONE_NORMAL). Then we
155 * carve out the portion of memory that is needed for these devices.
156 * The range is arch specific.
161 * ---------------------------
162 * parisc, ia64, sparc <4G
165 * alpha Unlimited or 0-16MB.
167 * i386, x86_64 and multiple other arches
172 #ifdef CONFIG_ZONE_DMA32
174 * x86_64 needs two ZONE_DMAs because it supports devices that are
175 * only able to do DMA to the lower 16M but also 32 bit devices that
176 * can only do DMA areas below 4G.
181 * Normal addressable memory is in ZONE_NORMAL. DMA operations can be
182 * performed on pages in ZONE_NORMAL if the DMA devices support
183 * transfers to all addressable memory.
186 #ifdef CONFIG_HIGHMEM
188 * A memory area that is only addressable by the kernel through
189 * mapping portions into its own address space. This is for example
190 * used by i386 to allow the kernel to address the memory beyond
191 * 900MB. The kernel will set up special mappings (page
192 * table entries on i386) for each page that the kernel needs to
201 #ifndef __GENERATING_BOUNDS_H
204 * When a memory allocation must conform to specific limitations (such
205 * as being suitable for DMA) the caller will pass in hints to the
206 * allocator in the gfp_mask, in the zone modifier bits. These bits
207 * are used to select a priority ordered list of memory zones which
208 * match the requested limits. See gfp_zone() in include/linux/gfp.h
212 #define ZONES_SHIFT 0
213 #elif MAX_NR_ZONES <= 2
214 #define ZONES_SHIFT 1
215 #elif MAX_NR_ZONES <= 4
216 #define ZONES_SHIFT 2
218 #error ZONES_SHIFT -- too many zones configured adjust calculation
222 /* Fields commonly accessed by the page allocator */
223 unsigned long pages_min
, pages_low
, pages_high
;
225 * We don't know if the memory that we're going to allocate will be freeable
226 * or/and it will be released eventually, so to avoid totally wasting several
227 * GB of ram we must reserve some of the lower zone memory (otherwise we risk
228 * to run OOM on the lower zones despite there's tons of freeable ram
229 * on the higher zones). This array is recalculated at runtime if the
230 * sysctl_lowmem_reserve_ratio sysctl changes.
232 unsigned long lowmem_reserve
[MAX_NR_ZONES
];
237 * zone reclaim becomes active if more unmapped pages exist.
239 unsigned long min_unmapped_pages
;
240 unsigned long min_slab_pages
;
241 struct per_cpu_pageset
*pageset
[NR_CPUS
];
243 struct per_cpu_pageset pageset
[NR_CPUS
];
246 * free areas of different sizes
249 #ifdef CONFIG_MEMORY_HOTPLUG
250 /* see spanned/present_pages for more description */
251 seqlock_t span_seqlock
;
253 struct free_area free_area
[MAX_ORDER
];
255 #ifndef CONFIG_SPARSEMEM
257 * Flags for a pageblock_nr_pages block. See pageblock-flags.h.
258 * In SPARSEMEM, this map is stored in struct mem_section
260 unsigned long *pageblock_flags
;
261 #endif /* CONFIG_SPARSEMEM */
266 /* Fields commonly accessed by the page reclaim scanner */
269 struct list_head list
;
270 unsigned long nr_scan
;
272 unsigned long pages_scanned
; /* since last reclaim */
273 unsigned long flags
; /* zone flags, see below */
275 /* Zone statistics */
276 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
279 * prev_priority holds the scanning priority for this zone. It is
280 * defined as the scanning priority at which we achieved our reclaim
281 * target at the previous try_to_free_pages() or balance_pgdat()
284 * We use prev_priority as a measure of how much stress page reclaim is
285 * under - it drives the swappiness decision: whether to unmap mapped
288 * Access to both this field is quite racy even on uniprocessor. But
289 * it is expected to average out OK.
295 /* Rarely used or read-mostly fields */
298 * wait_table -- the array holding the hash table
299 * wait_table_hash_nr_entries -- the size of the hash table array
300 * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
302 * The purpose of all these is to keep track of the people
303 * waiting for a page to become available and make them
304 * runnable again when possible. The trouble is that this
305 * consumes a lot of space, especially when so few things
306 * wait on pages at a given time. So instead of using
307 * per-page waitqueues, we use a waitqueue hash table.
309 * The bucket discipline is to sleep on the same queue when
310 * colliding and wake all in that wait queue when removing.
311 * When something wakes, it must check to be sure its page is
312 * truly available, a la thundering herd. The cost of a
313 * collision is great, but given the expected load of the
314 * table, they should be so rare as to be outweighed by the
315 * benefits from the saved space.
317 * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
318 * primary users of these fields, and in mm/page_alloc.c
319 * free_area_init_core() performs the initialization of them.
321 wait_queue_head_t
* wait_table
;
322 unsigned long wait_table_hash_nr_entries
;
323 unsigned long wait_table_bits
;
326 * Discontig memory support fields.
328 struct pglist_data
*zone_pgdat
;
329 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
330 unsigned long zone_start_pfn
;
333 * zone_start_pfn, spanned_pages and present_pages are all
334 * protected by span_seqlock. It is a seqlock because it has
335 * to be read outside of zone->lock, and it is done in the main
336 * allocator path. But, it is written quite infrequently.
338 * The lock is declared along with zone->lock because it is
339 * frequently read in proximity to zone->lock. It's good to
340 * give them a chance of being in the same cacheline.
342 unsigned long spanned_pages
; /* total size, including holes */
343 unsigned long present_pages
; /* amount of memory (excluding holes) */
346 * rarely used fields:
349 } ____cacheline_internodealigned_in_smp
;
352 ZONE_ALL_UNRECLAIMABLE
, /* all pages pinned */
353 ZONE_RECLAIM_LOCKED
, /* prevents concurrent reclaim */
354 ZONE_OOM_LOCKED
, /* zone is in OOM killer zonelist */
357 static inline void zone_set_flag(struct zone
*zone
, zone_flags_t flag
)
359 set_bit(flag
, &zone
->flags
);
362 static inline int zone_test_and_set_flag(struct zone
*zone
, zone_flags_t flag
)
364 return test_and_set_bit(flag
, &zone
->flags
);
367 static inline void zone_clear_flag(struct zone
*zone
, zone_flags_t flag
)
369 clear_bit(flag
, &zone
->flags
);
372 static inline int zone_is_all_unreclaimable(const struct zone
*zone
)
374 return test_bit(ZONE_ALL_UNRECLAIMABLE
, &zone
->flags
);
377 static inline int zone_is_reclaim_locked(const struct zone
*zone
)
379 return test_bit(ZONE_RECLAIM_LOCKED
, &zone
->flags
);
382 static inline int zone_is_oom_locked(const struct zone
*zone
)
384 return test_bit(ZONE_OOM_LOCKED
, &zone
->flags
);
388 * The "priority" of VM scanning is how much of the queues we will scan in one
389 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
390 * queues ("queue_length >> 12") during an aging round.
392 #define DEF_PRIORITY 12
394 /* Maximum number of zones on a zonelist */
395 #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
400 * The NUMA zonelists are doubled becausse we need zonelists that restrict the
401 * allocations to a single node for GFP_THISNODE.
403 * [0] : Zonelist with fallback
404 * [1] : No fallback (GFP_THISNODE)
406 #define MAX_ZONELISTS 2
410 * We cache key information from each zonelist for smaller cache
411 * footprint when scanning for free pages in get_page_from_freelist().
413 * 1) The BITMAP fullzones tracks which zones in a zonelist have come
414 * up short of free memory since the last time (last_fullzone_zap)
415 * we zero'd fullzones.
416 * 2) The array z_to_n[] maps each zone in the zonelist to its node
417 * id, so that we can efficiently evaluate whether that node is
418 * set in the current tasks mems_allowed.
420 * Both fullzones and z_to_n[] are one-to-one with the zonelist,
421 * indexed by a zones offset in the zonelist zones[] array.
423 * The get_page_from_freelist() routine does two scans. During the
424 * first scan, we skip zones whose corresponding bit in 'fullzones'
425 * is set or whose corresponding node in current->mems_allowed (which
426 * comes from cpusets) is not set. During the second scan, we bypass
427 * this zonelist_cache, to ensure we look methodically at each zone.
429 * Once per second, we zero out (zap) fullzones, forcing us to
430 * reconsider nodes that might have regained more free memory.
431 * The field last_full_zap is the time we last zapped fullzones.
433 * This mechanism reduces the amount of time we waste repeatedly
434 * reexaming zones for free memory when they just came up low on
435 * memory momentarilly ago.
437 * The zonelist_cache struct members logically belong in struct
438 * zonelist. However, the mempolicy zonelists constructed for
439 * MPOL_BIND are intentionally variable length (and usually much
440 * shorter). A general purpose mechanism for handling structs with
441 * multiple variable length members is more mechanism than we want
442 * here. We resort to some special case hackery instead.
444 * The MPOL_BIND zonelists don't need this zonelist_cache (in good
445 * part because they are shorter), so we put the fixed length stuff
446 * at the front of the zonelist struct, ending in a variable length
447 * zones[], as is needed by MPOL_BIND.
449 * Then we put the optional zonelist cache on the end of the zonelist
450 * struct. This optional stuff is found by a 'zlcache_ptr' pointer in
451 * the fixed length portion at the front of the struct. This pointer
452 * both enables us to find the zonelist cache, and in the case of
453 * MPOL_BIND zonelists, (which will just set the zlcache_ptr to NULL)
454 * to know that the zonelist cache is not there.
456 * The end result is that struct zonelists come in two flavors:
457 * 1) The full, fixed length version, shown below, and
458 * 2) The custom zonelists for MPOL_BIND.
459 * The custom MPOL_BIND zonelists have a NULL zlcache_ptr and no zlcache.
461 * Even though there may be multiple CPU cores on a node modifying
462 * fullzones or last_full_zap in the same zonelist_cache at the same
463 * time, we don't lock it. This is just hint data - if it is wrong now
464 * and then, the allocator will still function, perhaps a bit slower.
468 struct zonelist_cache
{
469 unsigned short z_to_n
[MAX_ZONES_PER_ZONELIST
]; /* zone->nid */
470 DECLARE_BITMAP(fullzones
, MAX_ZONES_PER_ZONELIST
); /* zone full? */
471 unsigned long last_full_zap
; /* when last zap'd (jiffies) */
474 #define MAX_ZONELISTS 1
475 struct zonelist_cache
;
479 * This struct contains information about a zone in a zonelist. It is stored
480 * here to avoid dereferences into large structures and lookups of tables
483 struct zone
*zone
; /* Pointer to actual zone */
484 int zone_idx
; /* zone_idx(zoneref->zone) */
488 * One allocation request operates on a zonelist. A zonelist
489 * is a list of zones, the first one is the 'goal' of the
490 * allocation, the other zones are fallback zones, in decreasing
493 * If zlcache_ptr is not NULL, then it is just the address of zlcache,
494 * as explained above. If zlcache_ptr is NULL, there is no zlcache.
496 * To speed the reading of the zonelist, the zonerefs contain the zone index
497 * of the entry being read. Helper functions to access information given
498 * a struct zoneref are
500 * zonelist_zone() - Return the struct zone * for an entry in _zonerefs
501 * zonelist_zone_idx() - Return the index of the zone for an entry
502 * zonelist_node_idx() - Return the index of the node for an entry
505 struct zonelist_cache
*zlcache_ptr
; // NULL or &zlcache
506 struct zoneref _zonerefs
[MAX_ZONES_PER_ZONELIST
+ 1];
508 struct zonelist_cache zlcache
; // optional ...
512 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
513 struct node_active_region
{
514 unsigned long start_pfn
;
515 unsigned long end_pfn
;
518 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
520 #ifndef CONFIG_DISCONTIGMEM
521 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
522 extern struct page
*mem_map
;
526 * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
527 * (mostly NUMA machines?) to denote a higher-level memory zone than the
530 * On NUMA machines, each NUMA node would have a pg_data_t to describe
531 * it's memory layout.
533 * Memory statistics and page replacement data structures are maintained on a
537 typedef struct pglist_data
{
538 struct zone node_zones
[MAX_NR_ZONES
];
539 struct zonelist node_zonelists
[MAX_ZONELISTS
];
541 #ifdef CONFIG_FLAT_NODE_MEM_MAP
542 struct page
*node_mem_map
;
544 struct bootmem_data
*bdata
;
545 #ifdef CONFIG_MEMORY_HOTPLUG
547 * Must be held any time you expect node_start_pfn, node_present_pages
548 * or node_spanned_pages stay constant. Holding this will also
549 * guarantee that any pfn_valid() stays that way.
551 * Nests above zone->lock and zone->size_seqlock.
553 spinlock_t node_size_lock
;
555 unsigned long node_start_pfn
;
556 unsigned long node_present_pages
; /* total number of physical pages */
557 unsigned long node_spanned_pages
; /* total size of physical page
558 range, including holes */
560 wait_queue_head_t kswapd_wait
;
561 struct task_struct
*kswapd
;
562 int kswapd_max_order
;
565 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
566 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
567 #ifdef CONFIG_FLAT_NODE_MEM_MAP
568 #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
570 #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
572 #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
574 #include <linux/memory_hotplug.h>
576 void get_zone_counts(unsigned long *active
, unsigned long *inactive
,
577 unsigned long *free
);
578 void build_all_zonelists(void);
579 void wakeup_kswapd(struct zone
*zone
, int order
);
580 int zone_watermark_ok(struct zone
*z
, int order
, unsigned long mark
,
581 int classzone_idx
, int alloc_flags
);
582 enum memmap_context
{
586 extern int init_currently_empty_zone(struct zone
*zone
, unsigned long start_pfn
,
588 enum memmap_context context
);
590 #ifdef CONFIG_HAVE_MEMORY_PRESENT
591 void memory_present(int nid
, unsigned long start
, unsigned long end
);
593 static inline void memory_present(int nid
, unsigned long start
, unsigned long end
) {}
596 #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
597 unsigned long __init
node_memmap_size_bytes(int, unsigned long, unsigned long);
601 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
603 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
605 static inline int populated_zone(struct zone
*zone
)
607 return (!!zone
->present_pages
);
610 extern int movable_zone
;
612 static inline int zone_movable_is_highmem(void)
614 #if defined(CONFIG_HIGHMEM) && defined(CONFIG_ARCH_POPULATES_NODE_MAP)
615 return movable_zone
== ZONE_HIGHMEM
;
621 static inline int is_highmem_idx(enum zone_type idx
)
623 #ifdef CONFIG_HIGHMEM
624 return (idx
== ZONE_HIGHMEM
||
625 (idx
== ZONE_MOVABLE
&& zone_movable_is_highmem()));
631 static inline int is_normal_idx(enum zone_type idx
)
633 return (idx
== ZONE_NORMAL
);
637 * is_highmem - helper function to quickly check if a struct zone is a
638 * highmem zone or not. This is an attempt to keep references
639 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
640 * @zone - pointer to struct zone variable
642 static inline int is_highmem(struct zone
*zone
)
644 #ifdef CONFIG_HIGHMEM
645 int zone_off
= (char *)zone
- (char *)zone
->zone_pgdat
->node_zones
;
646 return zone_off
== ZONE_HIGHMEM
* sizeof(*zone
) ||
647 (zone_off
== ZONE_MOVABLE
* sizeof(*zone
) &&
648 zone_movable_is_highmem());
654 static inline int is_normal(struct zone
*zone
)
656 return zone
== zone
->zone_pgdat
->node_zones
+ ZONE_NORMAL
;
659 static inline int is_dma32(struct zone
*zone
)
661 #ifdef CONFIG_ZONE_DMA32
662 return zone
== zone
->zone_pgdat
->node_zones
+ ZONE_DMA32
;
668 static inline int is_dma(struct zone
*zone
)
670 #ifdef CONFIG_ZONE_DMA
671 return zone
== zone
->zone_pgdat
->node_zones
+ ZONE_DMA
;
677 /* These two functions are used to setup the per zone pages min values */
680 int min_free_kbytes_sysctl_handler(struct ctl_table
*, int, struct file
*,
681 void __user
*, size_t *, loff_t
*);
682 extern int sysctl_lowmem_reserve_ratio
[MAX_NR_ZONES
-1];
683 int lowmem_reserve_ratio_sysctl_handler(struct ctl_table
*, int, struct file
*,
684 void __user
*, size_t *, loff_t
*);
685 int percpu_pagelist_fraction_sysctl_handler(struct ctl_table
*, int, struct file
*,
686 void __user
*, size_t *, loff_t
*);
687 int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table
*, int,
688 struct file
*, void __user
*, size_t *, loff_t
*);
689 int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table
*, int,
690 struct file
*, void __user
*, size_t *, loff_t
*);
692 extern int numa_zonelist_order_handler(struct ctl_table
*, int,
693 struct file
*, void __user
*, size_t *, loff_t
*);
694 extern char numa_zonelist_order
[];
695 #define NUMA_ZONELIST_ORDER_LEN 16 /* string buffer size */
697 #include <linux/topology.h>
698 /* Returns the number of the current Node. */
700 #define numa_node_id() (cpu_to_node(raw_smp_processor_id()))
703 #ifndef CONFIG_NEED_MULTIPLE_NODES
705 extern struct pglist_data contig_page_data
;
706 #define NODE_DATA(nid) (&contig_page_data)
707 #define NODE_MEM_MAP(nid) mem_map
709 #else /* CONFIG_NEED_MULTIPLE_NODES */
711 #include <asm/mmzone.h>
713 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
715 extern struct pglist_data
*first_online_pgdat(void);
716 extern struct pglist_data
*next_online_pgdat(struct pglist_data
*pgdat
);
717 extern struct zone
*next_zone(struct zone
*zone
);
720 * for_each_online_pgdat - helper macro to iterate over all online nodes
721 * @pgdat - pointer to a pg_data_t variable
723 #define for_each_online_pgdat(pgdat) \
724 for (pgdat = first_online_pgdat(); \
726 pgdat = next_online_pgdat(pgdat))
728 * for_each_zone - helper macro to iterate over all memory zones
729 * @zone - pointer to struct zone variable
731 * The user only needs to declare the zone variable, for_each_zone
734 #define for_each_zone(zone) \
735 for (zone = (first_online_pgdat())->node_zones; \
737 zone = next_zone(zone))
739 static inline struct zone
*zonelist_zone(struct zoneref
*zoneref
)
741 return zoneref
->zone
;
744 static inline int zonelist_zone_idx(struct zoneref
*zoneref
)
746 return zoneref
->zone_idx
;
749 static inline int zonelist_node_idx(struct zoneref
*zoneref
)
752 /* zone_to_nid not available in this context */
753 return zoneref
->zone
->node
;
756 #endif /* CONFIG_NUMA */
760 * next_zones_zonelist - Returns the next zone at or below highest_zoneidx within the allowed nodemask using a cursor within a zonelist as a starting point
761 * @z - The cursor used as a starting point for the search
762 * @highest_zoneidx - The zone index of the highest zone to return
763 * @nodes - An optional nodemask to filter the zonelist with
764 * @zone - The first suitable zone found is returned via this parameter
766 * This function returns the next zone at or below a given zone index that is
767 * within the allowed nodemask using a cursor as the starting point for the
768 * search. The zoneref returned is a cursor that represents the current zone
769 * being examined. It should be advanced by one before calling
770 * next_zones_zonelist again.
772 struct zoneref
*next_zones_zonelist(struct zoneref
*z
,
773 enum zone_type highest_zoneidx
,
778 * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
779 * @zonelist - The zonelist to search for a suitable zone
780 * @highest_zoneidx - The zone index of the highest zone to return
781 * @nodes - An optional nodemask to filter the zonelist with
782 * @zone - The first suitable zone found is returned via this parameter
784 * This function returns the first zone at or below a given zone index that is
785 * within the allowed nodemask. The zoneref returned is a cursor that can be
786 * used to iterate the zonelist with next_zones_zonelist by advancing it by
787 * one before calling.
789 static inline struct zoneref
*first_zones_zonelist(struct zonelist
*zonelist
,
790 enum zone_type highest_zoneidx
,
794 return next_zones_zonelist(zonelist
->_zonerefs
, highest_zoneidx
, nodes
,
799 * for_each_zone_zonelist_nodemask - helper macro to iterate over valid zones in a zonelist at or below a given zone index and within a nodemask
800 * @zone - The current zone in the iterator
801 * @z - The current pointer within zonelist->zones being iterated
802 * @zlist - The zonelist being iterated
803 * @highidx - The zone index of the highest zone to return
804 * @nodemask - Nodemask allowed by the allocator
806 * This iterator iterates though all zones at or below a given zone index and
807 * within a given nodemask
809 #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
810 for (z = first_zones_zonelist(zlist, highidx, nodemask, &zone); \
812 z = next_zones_zonelist(++z, highidx, nodemask, &zone)) \
815 * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
816 * @zone - The current zone in the iterator
817 * @z - The current pointer within zonelist->zones being iterated
818 * @zlist - The zonelist being iterated
819 * @highidx - The zone index of the highest zone to return
821 * This iterator iterates though all zones at or below a given zone index.
823 #define for_each_zone_zonelist(zone, z, zlist, highidx) \
824 for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
826 #ifdef CONFIG_SPARSEMEM
827 #include <asm/sparsemem.h>
830 #if !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) && \
831 !defined(CONFIG_ARCH_POPULATES_NODE_MAP)
832 static inline unsigned long early_pfn_to_nid(unsigned long pfn
)
838 #ifdef CONFIG_FLATMEM
839 #define pfn_to_nid(pfn) (0)
842 #define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
843 #define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
845 #ifdef CONFIG_SPARSEMEM
848 * SECTION_SHIFT #bits space required to store a section #
850 * PA_SECTION_SHIFT physical address to/from section number
851 * PFN_SECTION_SHIFT pfn to/from section number
853 #define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
855 #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
856 #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
858 #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
860 #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
861 #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
863 #define SECTION_BLOCKFLAGS_BITS \
864 ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
866 #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
867 #error Allocator MAX_ORDER exceeds SECTION_SIZE
873 * This is, logically, a pointer to an array of struct
874 * pages. However, it is stored with some other magic.
875 * (see sparse.c::sparse_init_one_section())
877 * Additionally during early boot we encode node id of
878 * the location of the section here to guide allocation.
879 * (see sparse.c::memory_present())
881 * Making it a UL at least makes someone do a cast
882 * before using it wrong.
884 unsigned long section_mem_map
;
886 /* See declaration of similar field in struct zone */
887 unsigned long *pageblock_flags
;
890 #ifdef CONFIG_SPARSEMEM_EXTREME
891 #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
893 #define SECTIONS_PER_ROOT 1
896 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
897 #define NR_SECTION_ROOTS (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
898 #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
900 #ifdef CONFIG_SPARSEMEM_EXTREME
901 extern struct mem_section
*mem_section
[NR_SECTION_ROOTS
];
903 extern struct mem_section mem_section
[NR_SECTION_ROOTS
][SECTIONS_PER_ROOT
];
906 static inline struct mem_section
*__nr_to_section(unsigned long nr
)
908 if (!mem_section
[SECTION_NR_TO_ROOT(nr
)])
910 return &mem_section
[SECTION_NR_TO_ROOT(nr
)][nr
& SECTION_ROOT_MASK
];
912 extern int __section_nr(struct mem_section
* ms
);
913 extern unsigned long usemap_size(void);
916 * We use the lower bits of the mem_map pointer to store
917 * a little bit of information. There should be at least
918 * 3 bits here due to 32-bit alignment.
920 #define SECTION_MARKED_PRESENT (1UL<<0)
921 #define SECTION_HAS_MEM_MAP (1UL<<1)
922 #define SECTION_MAP_LAST_BIT (1UL<<2)
923 #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
924 #define SECTION_NID_SHIFT 2
926 static inline struct page
*__section_mem_map_addr(struct mem_section
*section
)
928 unsigned long map
= section
->section_mem_map
;
929 map
&= SECTION_MAP_MASK
;
930 return (struct page
*)map
;
933 static inline int present_section(struct mem_section
*section
)
935 return (section
&& (section
->section_mem_map
& SECTION_MARKED_PRESENT
));
938 static inline int present_section_nr(unsigned long nr
)
940 return present_section(__nr_to_section(nr
));
943 static inline int valid_section(struct mem_section
*section
)
945 return (section
&& (section
->section_mem_map
& SECTION_HAS_MEM_MAP
));
948 static inline int valid_section_nr(unsigned long nr
)
950 return valid_section(__nr_to_section(nr
));
953 static inline struct mem_section
*__pfn_to_section(unsigned long pfn
)
955 return __nr_to_section(pfn_to_section_nr(pfn
));
958 static inline int pfn_valid(unsigned long pfn
)
960 if (pfn_to_section_nr(pfn
) >= NR_MEM_SECTIONS
)
962 return valid_section(__nr_to_section(pfn_to_section_nr(pfn
)));
965 static inline int pfn_present(unsigned long pfn
)
967 if (pfn_to_section_nr(pfn
) >= NR_MEM_SECTIONS
)
969 return present_section(__nr_to_section(pfn_to_section_nr(pfn
)));
973 * These are _only_ used during initialisation, therefore they
974 * can use __initdata ... They could have names to indicate
978 #define pfn_to_nid(pfn) \
980 unsigned long __pfn_to_nid_pfn = (pfn); \
981 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
984 #define pfn_to_nid(pfn) (0)
987 #define early_pfn_valid(pfn) pfn_valid(pfn)
988 void sparse_init(void);
990 #define sparse_init() do {} while (0)
991 #define sparse_index_init(_sec, _nid) do {} while (0)
992 #endif /* CONFIG_SPARSEMEM */
994 #ifdef CONFIG_NODES_SPAN_OTHER_NODES
995 #define early_pfn_in_nid(pfn, nid) (early_pfn_to_nid(pfn) == (nid))
997 #define early_pfn_in_nid(pfn, nid) (1)
1000 #ifndef early_pfn_valid
1001 #define early_pfn_valid(pfn) (1)
1004 void memory_present(int nid
, unsigned long start
, unsigned long end
);
1005 unsigned long __init
node_memmap_size_bytes(int, unsigned long, unsigned long);
1008 * If it is possible to have holes within a MAX_ORDER_NR_PAGES, then we
1009 * need to check pfn validility within that MAX_ORDER_NR_PAGES block.
1010 * pfn_valid_within() should be used in this case; we optimise this away
1011 * when we have no holes within a MAX_ORDER_NR_PAGES block.
1013 #ifdef CONFIG_HOLES_IN_ZONE
1014 #define pfn_valid_within(pfn) pfn_valid(pfn)
1016 #define pfn_valid_within(pfn) (1)
1019 #endif /* !__GENERATING_BOUNDS.H */
1020 #endif /* !__ASSEMBLY__ */
1021 #endif /* _LINUX_MMZONE_H */