blob: 435e5f794b3ba83721e51edb62b5b53d2a98d408 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Mel Gorman6b74ab92008-07-23 21:26:49 -07002/*
3 * mm_init.c - Memory initialisation verification and debugging
4 *
5 * Copyright 2008 IBM Corporation, 2008
6 * Author Mel Gorman <mel@csn.ul.ie>
7 *
8 */
9#include <linux/kernel.h>
10#include <linux/init.h>
Nishanth Aravamudanff7ea792008-07-23 21:27:39 -070011#include <linux/kobject.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040012#include <linux/export.h>
Tim Chen917d9292013-07-03 15:02:44 -070013#include <linux/memory.h>
14#include <linux/notifier.h>
Mel Gorman7e18adb2015-06-30 14:57:05 -070015#include <linux/sched.h>
Mel Gorman708614e2008-07-23 21:26:51 -070016#include "internal.h"
Mel Gorman6b74ab92008-07-23 21:26:49 -070017
Nishanth Aravamudan5e9426ab2008-07-23 21:27:39 -070018#ifdef CONFIG_DEBUG_MEMORY_INIT
Rasmus Villemoes194e8152015-02-12 15:00:12 -080019int __meminitdata mminit_loglevel;
Mel Gorman6b74ab92008-07-23 21:26:49 -070020
Andrew Morton5c9ffc92008-08-05 13:01:01 -070021#ifndef SECTIONS_SHIFT
22#define SECTIONS_SHIFT 0
23#endif
24
Mel Gorman68ad8df2008-07-23 21:26:52 -070025/* The zonelists are simply reported, validation is manual. */
Rasmus Villemoes0e2342c2015-02-12 15:00:09 -080026void __init mminit_verify_zonelist(void)
Mel Gorman68ad8df2008-07-23 21:26:52 -070027{
28 int nid;
29
30 if (mminit_loglevel < MMINIT_VERIFY)
31 return;
32
33 for_each_online_node(nid) {
34 pg_data_t *pgdat = NODE_DATA(nid);
35 struct zone *zone;
36 struct zoneref *z;
37 struct zonelist *zonelist;
38 int i, listid, zoneid;
39
Mateusz Noseke46b8932020-04-06 20:08:36 -070040 BUILD_BUG_ON(MAX_ZONELISTS > 2);
Mel Gorman68ad8df2008-07-23 21:26:52 -070041 for (i = 0; i < MAX_ZONELISTS * MAX_NR_ZONES; i++) {
42
43 /* Identify the zone and nodelist */
44 zoneid = i % MAX_NR_ZONES;
45 listid = i / MAX_NR_ZONES;
46 zonelist = &pgdat->node_zonelists[listid];
47 zone = &pgdat->node_zones[zoneid];
48 if (!populated_zone(zone))
49 continue;
50
51 /* Print information about the zonelist */
52 printk(KERN_DEBUG "mminit::zonelist %s %d:%s = ",
53 listid > 0 ? "thisnode" : "general", nid,
54 zone->name);
55
56 /* Iterate the zonelist */
Pavel Tatashinc1093b72018-08-21 21:53:32 -070057 for_each_zone_zonelist(zone, z, zonelist, zoneid)
58 pr_cont("%d:%s ", zone_to_nid(zone), zone->name);
Joe Perches11705322016-03-17 14:19:50 -070059 pr_cont("\n");
Mel Gorman68ad8df2008-07-23 21:26:52 -070060 }
61 }
62}
63
Mel Gorman708614e2008-07-23 21:26:51 -070064void __init mminit_verify_pageflags_layout(void)
65{
66 int shift, width;
67 unsigned long or_mask, add_mask;
68
69 shift = 8 * sizeof(unsigned long);
Jing Xia86fea8b2020-06-01 21:52:49 -070070 width = shift - SECTIONS_WIDTH - NODES_WIDTH - ZONES_WIDTH
71 - LAST_CPUPID_SHIFT - KASAN_TAG_WIDTH;
Mel Gorman708614e2008-07-23 21:26:51 -070072 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths",
Jing Xia86fea8b2020-06-01 21:52:49 -070073 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d Flags %d\n",
Mel Gorman708614e2008-07-23 21:26:51 -070074 SECTIONS_WIDTH,
75 NODES_WIDTH,
76 ZONES_WIDTH,
Peter Zijlstra90572892013-10-07 11:29:20 +010077 LAST_CPUPID_WIDTH,
Jing Xia86fea8b2020-06-01 21:52:49 -070078 KASAN_TAG_WIDTH,
Mel Gorman708614e2008-07-23 21:26:51 -070079 NR_PAGEFLAGS);
80 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_shifts",
Jing Xia86fea8b2020-06-01 21:52:49 -070081 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d\n",
Mel Gorman708614e2008-07-23 21:26:51 -070082 SECTIONS_SHIFT,
Mel Gorman708614e2008-07-23 21:26:51 -070083 NODES_SHIFT,
Mel Gormana4e1b4c2013-02-22 16:34:47 -080084 ZONES_SHIFT,
Jing Xia86fea8b2020-06-01 21:52:49 -070085 LAST_CPUPID_SHIFT,
86 KASAN_TAG_WIDTH);
Mel Gormana4e1b4c2013-02-22 16:34:47 -080087 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_pgshifts",
Jing Xia86fea8b2020-06-01 21:52:49 -070088 "Section %lu Node %lu Zone %lu Lastcpupid %lu Kasantag %lu\n",
Mel Gorman708614e2008-07-23 21:26:51 -070089 (unsigned long)SECTIONS_PGSHIFT,
90 (unsigned long)NODES_PGSHIFT,
Mel Gormana4e1b4c2013-02-22 16:34:47 -080091 (unsigned long)ZONES_PGSHIFT,
Jing Xia86fea8b2020-06-01 21:52:49 -070092 (unsigned long)LAST_CPUPID_PGSHIFT,
93 (unsigned long)KASAN_TAG_PGSHIFT);
Mel Gormana4e1b4c2013-02-22 16:34:47 -080094 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodezoneid",
95 "Node/Zone ID: %lu -> %lu\n",
96 (unsigned long)(ZONEID_PGOFF + ZONEID_SHIFT),
97 (unsigned long)ZONEID_PGOFF);
Mel Gorman708614e2008-07-23 21:26:51 -070098 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_usage",
Mel Gormana4e1b4c2013-02-22 16:34:47 -080099 "location: %d -> %d layout %d -> %d unused %d -> %d page-flags\n",
Mel Gorman708614e2008-07-23 21:26:51 -0700100 shift, width, width, NR_PAGEFLAGS, NR_PAGEFLAGS, 0);
101#ifdef NODE_NOT_IN_PAGE_FLAGS
102 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags",
103 "Node not in page flags");
104#endif
Peter Zijlstra90572892013-10-07 11:29:20 +0100105#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
Mel Gormana4e1b4c2013-02-22 16:34:47 -0800106 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags",
Peter Zijlstra90572892013-10-07 11:29:20 +0100107 "Last cpupid not in page flags");
Mel Gormana4e1b4c2013-02-22 16:34:47 -0800108#endif
Mel Gorman708614e2008-07-23 21:26:51 -0700109
110 if (SECTIONS_WIDTH) {
111 shift -= SECTIONS_WIDTH;
112 BUG_ON(shift != SECTIONS_PGSHIFT);
113 }
114 if (NODES_WIDTH) {
115 shift -= NODES_WIDTH;
116 BUG_ON(shift != NODES_PGSHIFT);
117 }
118 if (ZONES_WIDTH) {
119 shift -= ZONES_WIDTH;
120 BUG_ON(shift != ZONES_PGSHIFT);
121 }
122
123 /* Check for bitmask overlaps */
124 or_mask = (ZONES_MASK << ZONES_PGSHIFT) |
125 (NODES_MASK << NODES_PGSHIFT) |
126 (SECTIONS_MASK << SECTIONS_PGSHIFT);
127 add_mask = (ZONES_MASK << ZONES_PGSHIFT) +
128 (NODES_MASK << NODES_PGSHIFT) +
129 (SECTIONS_MASK << SECTIONS_PGSHIFT);
130 BUG_ON(or_mask != add_mask);
131}
132
Mel Gorman6b74ab92008-07-23 21:26:49 -0700133static __init int set_mminit_loglevel(char *str)
134{
135 get_option(&str, &mminit_loglevel);
136 return 0;
137}
138early_param("mminit_loglevel", set_mminit_loglevel);
Nishanth Aravamudan5e9426ab2008-07-23 21:27:39 -0700139#endif /* CONFIG_DEBUG_MEMORY_INIT */
Nishanth Aravamudanff7ea792008-07-23 21:27:39 -0700140
141struct kobject *mm_kobj;
142EXPORT_SYMBOL_GPL(mm_kobj);
143
Tim Chen917d9292013-07-03 15:02:44 -0700144#ifdef CONFIG_SMP
145s32 vm_committed_as_batch = 32;
146
147static void __meminit mm_compute_batch(void)
148{
149 u64 memsized_batch;
150 s32 nr = num_present_cpus();
151 s32 batch = max_t(s32, nr*2, 32);
152
153 /* batch size set to 0.4% of (total memory/#cpus), or max int32 */
Arun KSca79b0c2018-12-28 00:34:29 -0800154 memsized_batch = min_t(u64, (totalram_pages()/nr)/256, 0x7fffffff);
Tim Chen917d9292013-07-03 15:02:44 -0700155
156 vm_committed_as_batch = max_t(s32, memsized_batch, batch);
157}
158
159static int __meminit mm_compute_batch_notifier(struct notifier_block *self,
160 unsigned long action, void *arg)
161{
162 switch (action) {
163 case MEM_ONLINE:
164 case MEM_OFFLINE:
165 mm_compute_batch();
166 default:
167 break;
168 }
169 return NOTIFY_OK;
170}
171
172static struct notifier_block compute_batch_nb __meminitdata = {
173 .notifier_call = mm_compute_batch_notifier,
174 .priority = IPC_CALLBACK_PRI, /* use lowest priority */
175};
176
177static int __init mm_compute_batch_init(void)
178{
179 mm_compute_batch();
180 register_hotmemory_notifier(&compute_batch_nb);
181
182 return 0;
183}
184
185__initcall(mm_compute_batch_init);
186
187#endif
188
Nishanth Aravamudanff7ea792008-07-23 21:27:39 -0700189static int __init mm_sysfs_init(void)
190{
191 mm_kobj = kobject_create_and_add("mm", kernel_kobj);
192 if (!mm_kobj)
193 return -ENOMEM;
194
195 return 0;
196}
Hugh Dickinse82cb952014-01-27 17:06:55 -0800197postcore_initcall(mm_sysfs_init);