1 #ifndef _LINUX_MMZONE_H
2 #define _LINUX_MMZONE_H
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/wait.h>
10 #include <linux/cache.h>
11 #include <linux/threads.h>
12 #include <linux/numa.h>
13 #include <linux/init.h>
14 #include <linux/seqlock.h>
15 #include <linux/nodemask.h>
16 #include <asm/atomic.h>
19 /* Free memory management - zoned buddy allocator. */
20 #ifndef CONFIG_FORCE_MAX_ZONEORDER
23 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
25 #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
28 struct list_head free_list;
29 unsigned long nr_free;
35 * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
36 * So add a wild amount of padding here to ensure that they fall into separate
37 * cachelines. There are very few zone structures in the machine, so space
38 * consumption is not a concern here.
40 #if defined(CONFIG_SMP)
43 } ____cacheline_internodealigned_in_smp;
44 #define ZONE_PADDING(name) struct zone_padding name;
46 #define ZONE_PADDING(name)
50 NR_ANON_PAGES, /* Mapped anonymous pages */
51 NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
52 only modified from process context */
54 NR_VM_ZONE_STAT_ITEMS };
56 struct per_cpu_pages {
57 int count; /* number of pages in the list */
58 int high; /* high watermark, emptying needed */
59 int batch; /* chunk size for buddy add/remove */
60 struct list_head list; /* the list of pages */
63 struct per_cpu_pageset {
64 struct per_cpu_pages pcp[2]; /* 0: hot. 1: cold */
66 s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
70 unsigned long numa_hit; /* allocated in intended node */
71 unsigned long numa_miss; /* allocated in non intended node */
72 unsigned long numa_foreign; /* was intended here, hit elsewhere */
73 unsigned long interleave_hit; /* interleaver prefered this zone */
74 unsigned long local_node; /* allocation from local node */
75 unsigned long other_node; /* allocation from other node */
77 } ____cacheline_aligned_in_smp;
80 #define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)])
82 #define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)])
88 #define ZONE_HIGHMEM 3
90 #define MAX_NR_ZONES 4 /* Sync this with ZONES_SHIFT */
91 #define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */
95 * When a memory allocation must conform to specific limitations (such
96 * as being suitable for DMA) the caller will pass in hints to the
97 * allocator in the gfp_mask, in the zone modifier bits. These bits
98 * are used to select a priority ordered list of memory zones which
99 * match the requested limits. GFP_ZONEMASK defines which bits within
100 * the gfp_mask should be considered as zone modifiers. Each valid
101 * combination of the zone modifier bits has a corresponding list
102 * of zones (in node_zonelists). Thus for two zone modifiers there
103 * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
104 * be 8 (2 ** 3) zonelists. GFP_ZONETYPES defines the number of possible
105 * combinations of zone modifiers in "zone modifier space".
107 * As an optimisation any zone modifier bits which are only valid when
108 * no other zone modifier bits are set (loners) should be placed in
109 * the highest order bits of this field. This allows us to reduce the
110 * extent of the zonelists thus saving space. For example in the case
111 * of three zone modifier bits, we could require up to eight zonelists.
112 * If the left most zone modifier is a "loner" then the highest valid
113 * zonelist would be four allowing us to allocate only five zonelists.
114 * Use the first form for GFP_ZONETYPES when the left most bit is not
115 * a "loner", otherwise use the second.
117 * NOTE! Make sure this matches the zones in <linux/gfp.h>
119 #define GFP_ZONEMASK 0x07
120 /* #define GFP_ZONETYPES (GFP_ZONEMASK + 1) */ /* Non-loner */
121 #define GFP_ZONETYPES ((GFP_ZONEMASK + 1) / 2 + 1) /* Loner */
124 * On machines where it is needed (eg PCs) we divide physical memory
125 * into multiple physical zones. On a 32bit PC we have 4 zones:
127 * ZONE_DMA < 16 MB ISA DMA capable memory
128 * ZONE_DMA32 0 MB Empty
129 * ZONE_NORMAL 16-896 MB direct mapped by the kernel
130 * ZONE_HIGHMEM > 896 MB only page cache and user processes
134 /* Fields commonly accessed by the page allocator */
135 unsigned long free_pages;
136 unsigned long pages_min, pages_low, pages_high;
138 * We don't know if the memory that we're going to allocate will be freeable
139 * or/and it will be released eventually, so to avoid totally wasting several
140 * GB of ram we must reserve some of the lower zone memory (otherwise we risk
141 * to run OOM on the lower zones despite there's tons of freeable ram
142 * on the higher zones). This array is recalculated at runtime if the
143 * sysctl_lowmem_reserve_ratio sysctl changes.
145 unsigned long lowmem_reserve[MAX_NR_ZONES];
148 struct per_cpu_pageset *pageset[NR_CPUS];
150 struct per_cpu_pageset pageset[NR_CPUS];
153 * free areas of different sizes
156 #ifdef CONFIG_MEMORY_HOTPLUG
157 /* see spanned/present_pages for more description */
158 seqlock_t span_seqlock;
160 struct free_area free_area[MAX_ORDER];
165 /* Fields commonly accessed by the page reclaim scanner */
167 struct list_head active_list;
168 struct list_head inactive_list;
169 unsigned long nr_scan_active;
170 unsigned long nr_scan_inactive;
171 unsigned long nr_active;
172 unsigned long nr_inactive;
173 unsigned long pages_scanned; /* since last reclaim */
174 int all_unreclaimable; /* All pages pinned */
176 /* A count of how many reclaimers are scanning this zone */
177 atomic_t reclaim_in_progress;
179 /* Zone statistics */
180 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
183 * prev_priority holds the scanning priority for this zone. It is
184 * defined as the scanning priority at which we achieved our reclaim
185 * target at the previous try_to_free_pages() or balance_pgdat()
188 * We use prev_priority as a measure of how much stress page reclaim is
189 * under - it drives the swappiness decision: whether to unmap mapped
192 * temp_priority is used to remember the scanning priority at which
193 * this zone was successfully refilled to free_pages == pages_high.
195 * Access to both these fields is quite racy even on uniprocessor. But
196 * it is expected to average out OK.
203 /* Rarely used or read-mostly fields */
206 * wait_table -- the array holding the hash table
207 * wait_table_hash_nr_entries -- the size of the hash table array
208 * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
210 * The purpose of all these is to keep track of the people
211 * waiting for a page to become available and make them
212 * runnable again when possible. The trouble is that this
213 * consumes a lot of space, especially when so few things
214 * wait on pages at a given time. So instead of using
215 * per-page waitqueues, we use a waitqueue hash table.
217 * The bucket discipline is to sleep on the same queue when
218 * colliding and wake all in that wait queue when removing.
219 * When something wakes, it must check to be sure its page is
220 * truly available, a la thundering herd. The cost of a
221 * collision is great, but given the expected load of the
222 * table, they should be so rare as to be outweighed by the
223 * benefits from the saved space.
225 * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
226 * primary users of these fields, and in mm/page_alloc.c
227 * free_area_init_core() performs the initialization of them.
229 wait_queue_head_t * wait_table;
230 unsigned long wait_table_hash_nr_entries;
231 unsigned long wait_table_bits;
234 * Discontig memory support fields.
236 struct pglist_data *zone_pgdat;
237 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
238 unsigned long zone_start_pfn;
241 * zone_start_pfn, spanned_pages and present_pages are all
242 * protected by span_seqlock. It is a seqlock because it has
243 * to be read outside of zone->lock, and it is done in the main
244 * allocator path. But, it is written quite infrequently.
246 * The lock is declared along with zone->lock because it is
247 * frequently read in proximity to zone->lock. It's good to
248 * give them a chance of being in the same cacheline.
250 unsigned long spanned_pages; /* total size, including holes */
251 unsigned long present_pages; /* amount of memory (excluding holes) */
254 * rarely used fields:
257 } ____cacheline_internodealigned_in_smp;
261 * The "priority" of VM scanning is how much of the queues we will scan in one
262 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
263 * queues ("queue_length >> 12") during an aging round.
265 #define DEF_PRIORITY 12
268 * One allocation request operates on a zonelist. A zonelist
269 * is a list of zones, the first one is the 'goal' of the
270 * allocation, the other zones are fallback zones, in decreasing
273 * Right now a zonelist takes up less than a cacheline. We never
274 * modify it apart from boot-up, and only a few indices are used,
275 * so despite the zonelist table being relatively big, the cache
276 * footprint of this construct is very small.
279 struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
284 * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
285 * (mostly NUMA machines?) to denote a higher-level memory zone than the
288 * On NUMA machines, each NUMA node would have a pg_data_t to describe
289 * it's memory layout.
291 * Memory statistics and page replacement data structures are maintained on a
295 typedef struct pglist_data {
296 struct zone node_zones[MAX_NR_ZONES];
297 struct zonelist node_zonelists[GFP_ZONETYPES];
299 #ifdef CONFIG_FLAT_NODE_MEM_MAP
300 struct page *node_mem_map;
302 struct bootmem_data *bdata;
303 #ifdef CONFIG_MEMORY_HOTPLUG
305 * Must be held any time you expect node_start_pfn, node_present_pages
306 * or node_spanned_pages stay constant. Holding this will also
307 * guarantee that any pfn_valid() stays that way.
309 * Nests above zone->lock and zone->size_seqlock.
311 spinlock_t node_size_lock;
313 unsigned long node_start_pfn;
314 unsigned long node_present_pages; /* total number of physical pages */
315 unsigned long node_spanned_pages; /* total size of physical page
316 range, including holes */
318 wait_queue_head_t kswapd_wait;
319 struct task_struct *kswapd;
320 int kswapd_max_order;
323 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
324 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
325 #ifdef CONFIG_FLAT_NODE_MEM_MAP
326 #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
328 #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
330 #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
332 #include <linux/memory_hotplug.h>
334 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
335 unsigned long *free, struct pglist_data *pgdat);
336 void get_zone_counts(unsigned long *active, unsigned long *inactive,
337 unsigned long *free);
338 void build_all_zonelists(void);
339 void wakeup_kswapd(struct zone *zone, int order);
340 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
341 int classzone_idx, int alloc_flags);
343 extern int init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
346 #ifdef CONFIG_HAVE_MEMORY_PRESENT
347 void memory_present(int nid, unsigned long start, unsigned long end);
349 static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
352 #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
353 unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
357 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
359 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
361 static inline int populated_zone(struct zone *zone)
363 return (!!zone->present_pages);
366 static inline int is_highmem_idx(int idx)
368 return (idx == ZONE_HIGHMEM);
371 static inline int is_normal_idx(int idx)
373 return (idx == ZONE_NORMAL);
377 * is_highmem - helper function to quickly check if a struct zone is a
378 * highmem zone or not. This is an attempt to keep references
379 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
380 * @zone - pointer to struct zone variable
382 static inline int is_highmem(struct zone *zone)
384 return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM;
387 static inline int is_normal(struct zone *zone)
389 return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
392 static inline int is_dma32(struct zone *zone)
394 return zone == zone->zone_pgdat->node_zones + ZONE_DMA32;
397 static inline int is_dma(struct zone *zone)
399 return zone == zone->zone_pgdat->node_zones + ZONE_DMA;
402 /* These two functions are used to setup the per zone pages min values */
405 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
406 void __user *, size_t *, loff_t *);
407 extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
408 int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
409 void __user *, size_t *, loff_t *);
410 int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file *,
411 void __user *, size_t *, loff_t *);
413 #include <linux/topology.h>
414 /* Returns the number of the current Node. */
416 #define numa_node_id() (cpu_to_node(raw_smp_processor_id()))
419 #ifndef CONFIG_NEED_MULTIPLE_NODES
421 extern struct pglist_data contig_page_data;
422 #define NODE_DATA(nid) (&contig_page_data)
423 #define NODE_MEM_MAP(nid) mem_map
424 #define MAX_NODES_SHIFT 1
426 #else /* CONFIG_NEED_MULTIPLE_NODES */
428 #include <asm/mmzone.h>
430 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
432 extern struct pglist_data *first_online_pgdat(void);
433 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
434 extern struct zone *next_zone(struct zone *zone);
437 * for_each_pgdat - helper macro to iterate over all nodes
438 * @pgdat - pointer to a pg_data_t variable
440 #define for_each_online_pgdat(pgdat) \
441 for (pgdat = first_online_pgdat(); \
443 pgdat = next_online_pgdat(pgdat))
445 * for_each_zone - helper macro to iterate over all memory zones
446 * @zone - pointer to struct zone variable
448 * The user only needs to declare the zone variable, for_each_zone
451 #define for_each_zone(zone) \
452 for (zone = (first_online_pgdat())->node_zones; \
454 zone = next_zone(zone))
456 #ifdef CONFIG_SPARSEMEM
457 #include <asm/sparsemem.h>
460 #if BITS_PER_LONG == 32
462 * with 32 bit page->flags field, we reserve 9 bits for node/zone info.
463 * there are 4 zones (3 bits) and this leaves 9-3=6 bits for nodes.
465 #define FLAGS_RESERVED 9
467 #elif BITS_PER_LONG == 64
469 * with 64 bit flags field, there's plenty of room.
471 #define FLAGS_RESERVED 32
475 #error BITS_PER_LONG not defined
479 #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
480 #define early_pfn_to_nid(nid) (0UL)
483 #ifdef CONFIG_FLATMEM
484 #define pfn_to_nid(pfn) (0)
487 #define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
488 #define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
490 #ifdef CONFIG_SPARSEMEM
493 * SECTION_SHIFT #bits space required to store a section #
495 * PA_SECTION_SHIFT physical address to/from section number
496 * PFN_SECTION_SHIFT pfn to/from section number
498 #define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
500 #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
501 #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
503 #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
505 #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
506 #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
508 #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
509 #error Allocator MAX_ORDER exceeds SECTION_SIZE
515 * This is, logically, a pointer to an array of struct
516 * pages. However, it is stored with some other magic.
517 * (see sparse.c::sparse_init_one_section())
519 * Additionally during early boot we encode node id of
520 * the location of the section here to guide allocation.
521 * (see sparse.c::memory_present())
523 * Making it a UL at least makes someone do a cast
524 * before using it wrong.
526 unsigned long section_mem_map;
529 #ifdef CONFIG_SPARSEMEM_EXTREME
530 #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
532 #define SECTIONS_PER_ROOT 1
535 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
536 #define NR_SECTION_ROOTS (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
537 #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
539 #ifdef CONFIG_SPARSEMEM_EXTREME
540 extern struct mem_section *mem_section[NR_SECTION_ROOTS];
542 extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
545 static inline struct mem_section *__nr_to_section(unsigned long nr)
547 if (!mem_section[SECTION_NR_TO_ROOT(nr)])
549 return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
551 extern int __section_nr(struct mem_section* ms);
554 * We use the lower bits of the mem_map pointer to store
555 * a little bit of information. There should be at least
556 * 3 bits here due to 32-bit alignment.
558 #define SECTION_MARKED_PRESENT (1UL<<0)
559 #define SECTION_HAS_MEM_MAP (1UL<<1)
560 #define SECTION_MAP_LAST_BIT (1UL<<2)
561 #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
562 #define SECTION_NID_SHIFT 2
564 static inline struct page *__section_mem_map_addr(struct mem_section *section)
566 unsigned long map = section->section_mem_map;
567 map &= SECTION_MAP_MASK;
568 return (struct page *)map;
571 static inline int valid_section(struct mem_section *section)
573 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
576 static inline int section_has_mem_map(struct mem_section *section)
578 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
581 static inline int valid_section_nr(unsigned long nr)
583 return valid_section(__nr_to_section(nr));
586 static inline struct mem_section *__pfn_to_section(unsigned long pfn)
588 return __nr_to_section(pfn_to_section_nr(pfn));
591 static inline int pfn_valid(unsigned long pfn)
593 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
595 return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
599 * These are _only_ used during initialisation, therefore they
600 * can use __initdata ... They could have names to indicate
604 #define pfn_to_nid(pfn) \
606 unsigned long __pfn_to_nid_pfn = (pfn); \
607 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
610 #define pfn_to_nid(pfn) (0)
613 #define early_pfn_valid(pfn) pfn_valid(pfn)
614 void sparse_init(void);
616 #define sparse_init() do {} while (0)
617 #define sparse_index_init(_sec, _nid) do {} while (0)
618 #endif /* CONFIG_SPARSEMEM */
620 #ifndef early_pfn_valid
621 #define early_pfn_valid(pfn) (1)
624 void memory_present(int nid, unsigned long start, unsigned long end);
625 unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
627 #endif /* !__ASSEMBLY__ */
628 #endif /* __KERNEL__ */
629 #endif /* _LINUX_MMZONE_H */