xref: /openbmc/linux/mm/mm_init.c (revision a2cce7a9)
1 /*
2  * mm_init.c - Memory initialisation verification and debugging
3  *
4  * Copyright 2008 IBM Corporation, 2008
5  * Author Mel Gorman <mel@csn.ul.ie>
6  *
7  */
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/kobject.h>
11 #include <linux/export.h>
12 #include <linux/memory.h>
13 #include <linux/notifier.h>
14 #include <linux/sched.h>
15 #include "internal.h"
16 
17 #ifdef CONFIG_DEBUG_MEMORY_INIT
18 int __meminitdata mminit_loglevel;
19 
20 #ifndef SECTIONS_SHIFT
21 #define SECTIONS_SHIFT	0
22 #endif
23 
24 /* The zonelists are simply reported, validation is manual. */
25 void __init mminit_verify_zonelist(void)
26 {
27 	int nid;
28 
29 	if (mminit_loglevel < MMINIT_VERIFY)
30 		return;
31 
32 	for_each_online_node(nid) {
33 		pg_data_t *pgdat = NODE_DATA(nid);
34 		struct zone *zone;
35 		struct zoneref *z;
36 		struct zonelist *zonelist;
37 		int i, listid, zoneid;
38 
39 		BUG_ON(MAX_ZONELISTS > 2);
40 		for (i = 0; i < MAX_ZONELISTS * MAX_NR_ZONES; i++) {
41 
42 			/* Identify the zone and nodelist */
43 			zoneid = i % MAX_NR_ZONES;
44 			listid = i / MAX_NR_ZONES;
45 			zonelist = &pgdat->node_zonelists[listid];
46 			zone = &pgdat->node_zones[zoneid];
47 			if (!populated_zone(zone))
48 				continue;
49 
50 			/* Print information about the zonelist */
51 			printk(KERN_DEBUG "mminit::zonelist %s %d:%s = ",
52 				listid > 0 ? "thisnode" : "general", nid,
53 				zone->name);
54 
55 			/* Iterate the zonelist */
56 			for_each_zone_zonelist(zone, z, zonelist, zoneid) {
57 #ifdef CONFIG_NUMA
58 				printk(KERN_CONT "%d:%s ",
59 					zone->node, zone->name);
60 #else
61 				printk(KERN_CONT "0:%s ", zone->name);
62 #endif /* CONFIG_NUMA */
63 			}
64 			printk(KERN_CONT "\n");
65 		}
66 	}
67 }
68 
69 void __init mminit_verify_pageflags_layout(void)
70 {
71 	int shift, width;
72 	unsigned long or_mask, add_mask;
73 
74 	shift = 8 * sizeof(unsigned long);
75 	width = shift - SECTIONS_WIDTH - NODES_WIDTH - ZONES_WIDTH - LAST_CPUPID_SHIFT;
76 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths",
77 		"Section %d Node %d Zone %d Lastcpupid %d Flags %d\n",
78 		SECTIONS_WIDTH,
79 		NODES_WIDTH,
80 		ZONES_WIDTH,
81 		LAST_CPUPID_WIDTH,
82 		NR_PAGEFLAGS);
83 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_shifts",
84 		"Section %d Node %d Zone %d Lastcpupid %d\n",
85 		SECTIONS_SHIFT,
86 		NODES_SHIFT,
87 		ZONES_SHIFT,
88 		LAST_CPUPID_SHIFT);
89 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_pgshifts",
90 		"Section %lu Node %lu Zone %lu Lastcpupid %lu\n",
91 		(unsigned long)SECTIONS_PGSHIFT,
92 		(unsigned long)NODES_PGSHIFT,
93 		(unsigned long)ZONES_PGSHIFT,
94 		(unsigned long)LAST_CPUPID_PGSHIFT);
95 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodezoneid",
96 		"Node/Zone ID: %lu -> %lu\n",
97 		(unsigned long)(ZONEID_PGOFF + ZONEID_SHIFT),
98 		(unsigned long)ZONEID_PGOFF);
99 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_usage",
100 		"location: %d -> %d layout %d -> %d unused %d -> %d page-flags\n",
101 		shift, width, width, NR_PAGEFLAGS, NR_PAGEFLAGS, 0);
102 #ifdef NODE_NOT_IN_PAGE_FLAGS
103 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags",
104 		"Node not in page flags");
105 #endif
106 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
107 	mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags",
108 		"Last cpupid not in page flags");
109 #endif
110 
111 	if (SECTIONS_WIDTH) {
112 		shift -= SECTIONS_WIDTH;
113 		BUG_ON(shift != SECTIONS_PGSHIFT);
114 	}
115 	if (NODES_WIDTH) {
116 		shift -= NODES_WIDTH;
117 		BUG_ON(shift != NODES_PGSHIFT);
118 	}
119 	if (ZONES_WIDTH) {
120 		shift -= ZONES_WIDTH;
121 		BUG_ON(shift != ZONES_PGSHIFT);
122 	}
123 
124 	/* Check for bitmask overlaps */
125 	or_mask = (ZONES_MASK << ZONES_PGSHIFT) |
126 			(NODES_MASK << NODES_PGSHIFT) |
127 			(SECTIONS_MASK << SECTIONS_PGSHIFT);
128 	add_mask = (ZONES_MASK << ZONES_PGSHIFT) +
129 			(NODES_MASK << NODES_PGSHIFT) +
130 			(SECTIONS_MASK << SECTIONS_PGSHIFT);
131 	BUG_ON(or_mask != add_mask);
132 }
133 
134 static __init int set_mminit_loglevel(char *str)
135 {
136 	get_option(&str, &mminit_loglevel);
137 	return 0;
138 }
139 early_param("mminit_loglevel", set_mminit_loglevel);
140 #endif /* CONFIG_DEBUG_MEMORY_INIT */
141 
142 struct kobject *mm_kobj;
143 EXPORT_SYMBOL_GPL(mm_kobj);
144 
145 #ifdef CONFIG_SMP
146 s32 vm_committed_as_batch = 32;
147 
148 static void __meminit mm_compute_batch(void)
149 {
150 	u64 memsized_batch;
151 	s32 nr = num_present_cpus();
152 	s32 batch = max_t(s32, nr*2, 32);
153 
154 	/* batch size set to 0.4% of (total memory/#cpus), or max int32 */
155 	memsized_batch = min_t(u64, (totalram_pages/nr)/256, 0x7fffffff);
156 
157 	vm_committed_as_batch = max_t(s32, memsized_batch, batch);
158 }
159 
160 static int __meminit mm_compute_batch_notifier(struct notifier_block *self,
161 					unsigned long action, void *arg)
162 {
163 	switch (action) {
164 	case MEM_ONLINE:
165 	case MEM_OFFLINE:
166 		mm_compute_batch();
167 	default:
168 		break;
169 	}
170 	return NOTIFY_OK;
171 }
172 
173 static struct notifier_block compute_batch_nb __meminitdata = {
174 	.notifier_call = mm_compute_batch_notifier,
175 	.priority = IPC_CALLBACK_PRI, /* use lowest priority */
176 };
177 
178 static int __init mm_compute_batch_init(void)
179 {
180 	mm_compute_batch();
181 	register_hotmemory_notifier(&compute_batch_nb);
182 
183 	return 0;
184 }
185 
186 __initcall(mm_compute_batch_init);
187 
188 #endif
189 
190 static int __init mm_sysfs_init(void)
191 {
192 	mm_kobj = kobject_create_and_add("mm", kernel_kobj);
193 	if (!mm_kobj)
194 		return -ENOMEM;
195 
196 	return 0;
197 }
198 postcore_initcall(mm_sysfs_init);
199