blob: 8e02e865cc65af52b48b7417b60f1a881d5be692 [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>
Feng Tang56f35472020-08-06 23:23:15 -070016#include <linux/mman.h>
Mel Gorman708614e2008-07-23 21:26:51 -070017#include "internal.h"
Mel Gorman6b74ab92008-07-23 21:26:49 -070018
Nishanth Aravamudan5e9426ab2008-07-23 21:27:39 -070019#ifdef CONFIG_DEBUG_MEMORY_INIT
Rasmus Villemoes194e8152015-02-12 15:00:12 -080020int __meminitdata mminit_loglevel;
Mel Gorman6b74ab92008-07-23 21:26:49 -070021
Andrew Morton5c9ffc92008-08-05 13:01:01 -070022#ifndef SECTIONS_SHIFT
23#define SECTIONS_SHIFT 0
24#endif
25
Mel Gorman68ad8df2008-07-23 21:26:52 -070026/* The zonelists are simply reported, validation is manual. */
Rasmus Villemoes0e2342c2015-02-12 15:00:09 -080027void __init mminit_verify_zonelist(void)
Mel Gorman68ad8df2008-07-23 21:26:52 -070028{
29 int nid;
30
31 if (mminit_loglevel < MMINIT_VERIFY)
32 return;
33
34 for_each_online_node(nid) {
35 pg_data_t *pgdat = NODE_DATA(nid);
36 struct zone *zone;
37 struct zoneref *z;
38 struct zonelist *zonelist;
39 int i, listid, zoneid;
40
Mateusz Noseke46b8932020-04-06 20:08:36 -070041 BUILD_BUG_ON(MAX_ZONELISTS > 2);
Mel Gorman68ad8df2008-07-23 21:26:52 -070042 for (i = 0; i < MAX_ZONELISTS * MAX_NR_ZONES; i++) {
43
44 /* Identify the zone and nodelist */
45 zoneid = i % MAX_NR_ZONES;
46 listid = i / MAX_NR_ZONES;
47 zonelist = &pgdat->node_zonelists[listid];
48 zone = &pgdat->node_zones[zoneid];
49 if (!populated_zone(zone))
50 continue;
51
52 /* Print information about the zonelist */
53 printk(KERN_DEBUG "mminit::zonelist %s %d:%s = ",
54 listid > 0 ? "thisnode" : "general", nid,
55 zone->name);
56
57 /* Iterate the zonelist */
Pavel Tatashinc1093b72018-08-21 21:53:32 -070058 for_each_zone_zonelist(zone, z, zonelist, zoneid)
59 pr_cont("%d:%s ", zone_to_nid(zone), zone->name);
Joe Perches11705322016-03-17 14:19:50 -070060 pr_cont("\n");
Mel Gorman68ad8df2008-07-23 21:26:52 -070061 }
62 }
63}
64
Mel Gorman708614e2008-07-23 21:26:51 -070065void __init mminit_verify_pageflags_layout(void)
66{
67 int shift, width;
68 unsigned long or_mask, add_mask;
69
70 shift = 8 * sizeof(unsigned long);
Jing Xia86fea8b2020-06-01 21:52:49 -070071 width = shift - SECTIONS_WIDTH - NODES_WIDTH - ZONES_WIDTH
72 - LAST_CPUPID_SHIFT - KASAN_TAG_WIDTH;
Mel Gorman708614e2008-07-23 21:26:51 -070073 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths",
Jing Xia86fea8b2020-06-01 21:52:49 -070074 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d Flags %d\n",
Mel Gorman708614e2008-07-23 21:26:51 -070075 SECTIONS_WIDTH,
76 NODES_WIDTH,
77 ZONES_WIDTH,
Peter Zijlstra90572892013-10-07 11:29:20 +010078 LAST_CPUPID_WIDTH,
Jing Xia86fea8b2020-06-01 21:52:49 -070079 KASAN_TAG_WIDTH,
Mel Gorman708614e2008-07-23 21:26:51 -070080 NR_PAGEFLAGS);
81 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_shifts",
Jing Xia86fea8b2020-06-01 21:52:49 -070082 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d\n",
Mel Gorman708614e2008-07-23 21:26:51 -070083 SECTIONS_SHIFT,
Mel Gorman708614e2008-07-23 21:26:51 -070084 NODES_SHIFT,
Mel Gormana4e1b4c2013-02-22 16:34:47 -080085 ZONES_SHIFT,
Jing Xia86fea8b2020-06-01 21:52:49 -070086 LAST_CPUPID_SHIFT,
87 KASAN_TAG_WIDTH);
Mel Gormana4e1b4c2013-02-22 16:34:47 -080088 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_pgshifts",
Jing Xia86fea8b2020-06-01 21:52:49 -070089 "Section %lu Node %lu Zone %lu Lastcpupid %lu Kasantag %lu\n",
Mel Gorman708614e2008-07-23 21:26:51 -070090 (unsigned long)SECTIONS_PGSHIFT,
91 (unsigned long)NODES_PGSHIFT,
Mel Gormana4e1b4c2013-02-22 16:34:47 -080092 (unsigned long)ZONES_PGSHIFT,
Jing Xia86fea8b2020-06-01 21:52:49 -070093 (unsigned long)LAST_CPUPID_PGSHIFT,
94 (unsigned long)KASAN_TAG_PGSHIFT);
Mel Gormana4e1b4c2013-02-22 16:34:47 -080095 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);
Mel Gorman708614e2008-07-23 21:26:51 -070099 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_usage",
Mel Gormana4e1b4c2013-02-22 16:34:47 -0800100 "location: %d -> %d layout %d -> %d unused %d -> %d page-flags\n",
Mel Gorman708614e2008-07-23 21:26:51 -0700101 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
Peter Zijlstra90572892013-10-07 11:29:20 +0100106#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
Mel Gormana4e1b4c2013-02-22 16:34:47 -0800107 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags",
Peter Zijlstra90572892013-10-07 11:29:20 +0100108 "Last cpupid not in page flags");
Mel Gormana4e1b4c2013-02-22 16:34:47 -0800109#endif
Mel Gorman708614e2008-07-23 21:26:51 -0700110
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
Mel Gorman6b74ab92008-07-23 21:26:49 -0700134static __init int set_mminit_loglevel(char *str)
135{
136 get_option(&str, &mminit_loglevel);
137 return 0;
138}
139early_param("mminit_loglevel", set_mminit_loglevel);
Nishanth Aravamudan5e9426ab2008-07-23 21:27:39 -0700140#endif /* CONFIG_DEBUG_MEMORY_INIT */
Nishanth Aravamudanff7ea792008-07-23 21:27:39 -0700141
142struct kobject *mm_kobj;
143EXPORT_SYMBOL_GPL(mm_kobj);
144
Tim Chen917d9292013-07-03 15:02:44 -0700145#ifdef CONFIG_SMP
146s32 vm_committed_as_batch = 32;
147
Feng Tang56f35472020-08-06 23:23:15 -0700148void mm_compute_batch(int overcommit_policy)
Tim Chen917d9292013-07-03 15:02:44 -0700149{
150 u64 memsized_batch;
151 s32 nr = num_present_cpus();
152 s32 batch = max_t(s32, nr*2, 32);
Feng Tang56f35472020-08-06 23:23:15 -0700153 unsigned long ram_pages = totalram_pages();
Tim Chen917d9292013-07-03 15:02:44 -0700154
Feng Tang56f35472020-08-06 23:23:15 -0700155 /*
156 * For policy OVERCOMMIT_NEVER, set batch size to 0.4% of
157 * (total memory/#cpus), and lift it to 25% for other policies
158 * to easy the possible lock contention for percpu_counter
159 * vm_committed_as, while the max limit is INT_MAX
160 */
161 if (overcommit_policy == OVERCOMMIT_NEVER)
162 memsized_batch = min_t(u64, ram_pages/nr/256, INT_MAX);
163 else
164 memsized_batch = min_t(u64, ram_pages/nr/4, INT_MAX);
Tim Chen917d9292013-07-03 15:02:44 -0700165
166 vm_committed_as_batch = max_t(s32, memsized_batch, batch);
167}
168
169static int __meminit mm_compute_batch_notifier(struct notifier_block *self,
170 unsigned long action, void *arg)
171{
172 switch (action) {
173 case MEM_ONLINE:
174 case MEM_OFFLINE:
Feng Tang56f35472020-08-06 23:23:15 -0700175 mm_compute_batch(sysctl_overcommit_memory);
Gustavo A. R. Silva01359eb2020-12-14 19:15:00 -0800176 break;
Tim Chen917d9292013-07-03 15:02:44 -0700177 default:
178 break;
179 }
180 return NOTIFY_OK;
181}
182
183static struct notifier_block compute_batch_nb __meminitdata = {
184 .notifier_call = mm_compute_batch_notifier,
185 .priority = IPC_CALLBACK_PRI, /* use lowest priority */
186};
187
188static int __init mm_compute_batch_init(void)
189{
Feng Tang56f35472020-08-06 23:23:15 -0700190 mm_compute_batch(sysctl_overcommit_memory);
Tim Chen917d9292013-07-03 15:02:44 -0700191 register_hotmemory_notifier(&compute_batch_nb);
192
193 return 0;
194}
195
196__initcall(mm_compute_batch_init);
197
198#endif
199
Nishanth Aravamudanff7ea792008-07-23 21:27:39 -0700200static int __init mm_sysfs_init(void)
201{
202 mm_kobj = kobject_create_and_add("mm", kernel_kobj);
203 if (!mm_kobj)
204 return -ENOMEM;
205
206 return 0;
207}
Hugh Dickinse82cb952014-01-27 17:06:55 -0800208postcore_initcall(mm_sysfs_init);