blob: 86d6cd92ce3d8cc9ffa5b964d84e8e06d92bc163 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003 * Basic Node interface support
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/mm.h>
Gary Hadec04fc582009-01-06 14:39:14 -08009#include <linux/memory.h>
KOSAKI Motohirofa25c502011-05-24 17:11:28 -070010#include <linux/vmstat.h>
Andrew Morton6e259e72013-04-29 15:08:07 -070011#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/node.h>
13#include <linux/hugetlb.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070014#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/cpumask.h>
16#include <linux/topology.h>
17#include <linux/nodemask.h>
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070018#include <linux/cpu.h>
Lee Schermerhornbde631a2007-10-16 01:26:27 -070019#include <linux/device.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070020#include <linux/swap.h>
Tejun Heo18e5b532010-04-06 19:23:33 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Kay Sievers10fbcf42011-12-21 14:48:43 -080023static struct bus_type node_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +010024 .name = "node",
Kay Sievers10fbcf42011-12-21 14:48:43 -080025 .dev_name = "node",
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28
Sudeep Holla5aaba362014-09-30 14:48:22 +010029static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Zhen Lei064f0e92017-10-13 15:57:50 -070031 ssize_t n;
32 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct node *node_dev = to_node(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Mike Travis39106dc2008-04-08 11:43:03 -070035 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
36 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Zhen Lei064f0e92017-10-13 15:57:50 -070038 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
39 return 0;
40
41 cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask);
42 n = cpumap_print_to_pagebuf(list, buf, mask);
43 free_cpumask_var(mask);
44
45 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Kay Sievers10fbcf42011-12-21 14:48:43 -080048static inline ssize_t node_read_cpumask(struct device *dev,
49 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070050{
Sudeep Holla5aaba362014-09-30 14:48:22 +010051 return node_read_cpumap(dev, false, buf);
Mike Travis39106dc2008-04-08 11:43:03 -070052}
Kay Sievers10fbcf42011-12-21 14:48:43 -080053static inline ssize_t node_read_cpulist(struct device *dev,
54 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070055{
Sudeep Holla5aaba362014-09-30 14:48:22 +010056 return node_read_cpumap(dev, true, buf);
Mike Travis39106dc2008-04-08 11:43:03 -070057}
58
Kay Sievers10fbcf42011-12-21 14:48:43 -080059static DEVICE_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
60static DEVICE_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define K(x) ((x) << (PAGE_SHIFT - 10))
Kay Sievers10fbcf42011-12-21 14:48:43 -080063static ssize_t node_read_meminfo(struct device *dev,
64 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 int n;
67 int nid = dev->id;
Mel Gorman599d0c92016-07-28 15:45:31 -070068 struct pglist_data *pgdat = NODE_DATA(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct sysinfo i;
Vlastimil Babka61f94e12018-10-26 15:05:50 -070070 unsigned long sreclaimable, sunreclaimable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 si_meminfo_node(&i, nid);
Vlastimil Babka61f94e12018-10-26 15:05:50 -070073 sreclaimable = node_page_state(pgdat, NR_SLAB_RECLAIMABLE);
74 sunreclaimable = node_page_state(pgdat, NR_SLAB_UNRECLAIMABLE);
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -070075 n = sprintf(buf,
Rik van Riel4f98a2f2008-10-18 20:26:32 -070076 "Node %d MemTotal: %8lu kB\n"
77 "Node %d MemFree: %8lu kB\n"
78 "Node %d MemUsed: %8lu kB\n"
79 "Node %d Active: %8lu kB\n"
80 "Node %d Inactive: %8lu kB\n"
81 "Node %d Active(anon): %8lu kB\n"
82 "Node %d Inactive(anon): %8lu kB\n"
83 "Node %d Active(file): %8lu kB\n"
84 "Node %d Inactive(file): %8lu kB\n"
Nick Piggin5344b7e2008-10-18 20:26:51 -070085 "Node %d Unevictable: %8lu kB\n"
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -070086 "Node %d Mlocked: %8lu kB\n",
87 nid, K(i.totalram),
88 nid, K(i.freeram),
89 nid, K(i.totalram - i.freeram),
Mel Gorman599d0c92016-07-28 15:45:31 -070090 nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
91 node_page_state(pgdat, NR_ACTIVE_FILE)),
92 nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
93 node_page_state(pgdat, NR_INACTIVE_FILE)),
94 nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
95 nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
96 nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
97 nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
98 nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
Mel Gorman75ef7182016-07-28 15:45:24 -070099 nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700100
Christoph Lameter182e8e22006-09-25 23:31:10 -0700101#ifdef CONFIG_HIGHMEM
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700102 n += sprintf(buf + n,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700103 "Node %d HighTotal: %8lu kB\n"
104 "Node %d HighFree: %8lu kB\n"
105 "Node %d LowTotal: %8lu kB\n"
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700106 "Node %d LowFree: %8lu kB\n",
107 nid, K(i.totalhigh),
108 nid, K(i.freehigh),
109 nid, K(i.totalram - i.totalhigh),
110 nid, K(i.freeram - i.freehigh));
Christoph Lameter182e8e22006-09-25 23:31:10 -0700111#endif
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700112 n += sprintf(buf + n,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700113 "Node %d Dirty: %8lu kB\n"
114 "Node %d Writeback: %8lu kB\n"
115 "Node %d FilePages: %8lu kB\n"
116 "Node %d Mapped: %8lu kB\n"
117 "Node %d AnonPages: %8lu kB\n"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700118 "Node %d Shmem: %8lu kB\n"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700119 "Node %d KernelStack: %8lu kB\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700120 "Node %d PageTables: %8lu kB\n"
121 "Node %d NFS_Unstable: %8lu kB\n"
122 "Node %d Bounce: %8lu kB\n"
123 "Node %d WritebackTmp: %8lu kB\n"
Vlastimil Babka61f94e12018-10-26 15:05:50 -0700124 "Node %d KReclaimable: %8lu kB\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700125 "Node %d Slab: %8lu kB\n"
126 "Node %d SReclaimable: %8lu kB\n"
David Rientjes05b258e2011-01-13 15:47:14 -0800127 "Node %d SUnreclaim: %8lu kB\n"
128#ifdef CONFIG_TRANSPARENT_HUGEPAGE
129 "Node %d AnonHugePages: %8lu kB\n"
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700130 "Node %d ShmemHugePages: %8lu kB\n"
131 "Node %d ShmemPmdMapped: %8lu kB\n"
David Rientjes05b258e2011-01-13 15:47:14 -0800132#endif
133 ,
Mel Gorman11fb9982016-07-28 15:46:20 -0700134 nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
135 nid, K(node_page_state(pgdat, NR_WRITEBACK)),
136 nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
Mel Gorman50658e22016-07-28 15:46:14 -0700137 nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700138 nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
Rafael Aquinicc7452b2014-08-06 16:06:38 -0700139 nid, K(i.sharedram),
Andy Lutomirskid30dd8b2016-07-28 15:48:14 -0700140 nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB),
Mel Gorman75ef7182016-07-28 15:45:24 -0700141 nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)),
Mel Gorman11fb9982016-07-28 15:46:20 -0700142 nid, K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
Mel Gorman75ef7182016-07-28 15:45:24 -0700143 nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),
Mel Gorman11fb9982016-07-28 15:46:20 -0700144 nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
Vlastimil Babka61f94e12018-10-26 15:05:50 -0700145 nid, K(sreclaimable +
146 node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
147 nid, K(sreclaimable + sunreclaimable),
148 nid, K(sreclaimable),
149 nid, K(sunreclaimable)
David Rientjes05b258e2011-01-13 15:47:14 -0800150#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Vlastimil Babka61f94e12018-10-26 15:05:50 -0700151 ,
Mel Gorman11fb9982016-07-28 15:46:20 -0700152 nid, K(node_page_state(pgdat, NR_ANON_THPS) *
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700153 HPAGE_PMD_NR),
Mel Gorman11fb9982016-07-28 15:46:20 -0700154 nid, K(node_page_state(pgdat, NR_SHMEM_THPS) *
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700155 HPAGE_PMD_NR),
Mel Gorman11fb9982016-07-28 15:46:20 -0700156 nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) *
Vlastimil Babka61f94e12018-10-26 15:05:50 -0700157 HPAGE_PMD_NR)
David Rientjes05b258e2011-01-13 15:47:14 -0800158#endif
Vlastimil Babka61f94e12018-10-26 15:05:50 -0700159 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 n += hugetlb_report_node_meminfo(nid, buf + n);
161 return n;
162}
163
164#undef K
Kay Sievers10fbcf42011-12-21 14:48:43 -0800165static DEVICE_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Kay Sievers10fbcf42011-12-21 14:48:43 -0800167static ssize_t node_read_numastat(struct device *dev,
168 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return sprintf(buf,
171 "numa_hit %lu\n"
172 "numa_miss %lu\n"
173 "numa_foreign %lu\n"
174 "interleave_hit %lu\n"
175 "local_node %lu\n"
176 "other_node %lu\n",
Kemi Wang3a321d22017-09-08 16:12:48 -0700177 sum_zone_numa_state(dev->id, NUMA_HIT),
178 sum_zone_numa_state(dev->id, NUMA_MISS),
179 sum_zone_numa_state(dev->id, NUMA_FOREIGN),
180 sum_zone_numa_state(dev->id, NUMA_INTERLEAVE_HIT),
181 sum_zone_numa_state(dev->id, NUMA_LOCAL),
182 sum_zone_numa_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800184static DEVICE_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Kay Sievers10fbcf42011-12-21 14:48:43 -0800186static ssize_t node_read_vmstat(struct device *dev,
187 struct device_attribute *attr, char *buf)
Michael Rubin2ac39032010-10-26 14:21:35 -0700188{
189 int nid = dev->id;
Mel Gorman75ef7182016-07-28 15:45:24 -0700190 struct pglist_data *pgdat = NODE_DATA(nid);
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700191 int i;
192 int n = 0;
193
194 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
195 n += sprintf(buf+n, "%s %lu\n", vmstat_text[i],
Mel Gorman75ef7182016-07-28 15:45:24 -0700196 sum_zone_node_page_state(nid, i));
197
Kemi Wang3a321d22017-09-08 16:12:48 -0700198#ifdef CONFIG_NUMA
199 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
Mel Gorman75ef7182016-07-28 15:45:24 -0700200 n += sprintf(buf+n, "%s %lu\n",
201 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
Kemi Wang3a321d22017-09-08 16:12:48 -0700202 sum_zone_numa_state(nid, i));
203#endif
204
205 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
206 n += sprintf(buf+n, "%s %lu\n",
207 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
208 NR_VM_NUMA_STAT_ITEMS],
Mel Gorman75ef7182016-07-28 15:45:24 -0700209 node_page_state(pgdat, i));
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700210
211 return n;
Michael Rubin2ac39032010-10-26 14:21:35 -0700212}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800213static DEVICE_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL);
Michael Rubin2ac39032010-10-26 14:21:35 -0700214
Kay Sievers10fbcf42011-12-21 14:48:43 -0800215static ssize_t node_read_distance(struct device *dev,
Ana Nedelcu518d3f32015-03-08 12:48:48 +0200216 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int nid = dev->id;
219 int len = 0;
220 int i;
221
David Rientjes12ee3c02010-03-10 14:50:21 -0800222 /*
223 * buf is currently PAGE_SIZE in length and each node needs 4 chars
224 * at the most (distance + space or newline).
225 */
226 BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 for_each_online_node(i)
229 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
230
231 len += sprintf(buf + len, "\n");
232 return len;
233}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800234static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Takashi Iwai3c9b8aa2015-01-29 12:29:22 +0100236static struct attribute *node_dev_attrs[] = {
237 &dev_attr_cpumap.attr,
238 &dev_attr_cpulist.attr,
239 &dev_attr_meminfo.attr,
240 &dev_attr_numastat.attr,
241 &dev_attr_distance.attr,
242 &dev_attr_vmstat.attr,
243 NULL
244};
Greg Kroah-Hartman7ca7ec42015-03-25 13:47:17 +0100245ATTRIBUTE_GROUPS(node_dev);
Takashi Iwai3c9b8aa2015-01-29 12:29:22 +0100246
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800247#ifdef CONFIG_HUGETLBFS
248/*
249 * hugetlbfs per node attributes registration interface:
250 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800251 * it will register its per node attributes for all online nodes with
252 * memory. It will also call register_hugetlbfs_with_node(), below, to
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800253 * register its attribute registration functions with this node driver.
254 * Once these hooks have been initialized, the node driver will call into
255 * the hugetlb module to [un]register attributes for hot-plugged nodes.
256 */
257static node_registration_func_t __hugetlb_register_node;
258static node_registration_func_t __hugetlb_unregister_node;
259
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800260static inline bool hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800261{
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800262 if (__hugetlb_register_node &&
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -0800263 node_state(node->dev.id, N_MEMORY)) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800264 __hugetlb_register_node(node);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800265 return true;
266 }
267 return false;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800268}
269
270static inline void hugetlb_unregister_node(struct node *node)
271{
272 if (__hugetlb_unregister_node)
273 __hugetlb_unregister_node(node);
274}
275
276void register_hugetlbfs_with_node(node_registration_func_t doregister,
277 node_registration_func_t unregister)
278{
279 __hugetlb_register_node = doregister;
280 __hugetlb_unregister_node = unregister;
281}
282#else
283static inline void hugetlb_register_node(struct node *node) {}
284
285static inline void hugetlb_unregister_node(struct node *node) {}
286#endif
287
Yasuaki Ishimatsu8c7b5b42012-12-11 16:00:57 -0800288static void node_device_release(struct device *dev)
289{
290 struct node *node = to_node(dev);
291
292#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS)
293 /*
294 * We schedule the work only when a memory section is
295 * onlined/offlined on this node. When we come here,
296 * all the memory on this node has been offlined,
297 * so we won't enqueue new work to this work.
298 *
299 * The work is using node->node_work, so we should
300 * flush work before freeing the memory.
301 */
302 flush_work(&node->node_work);
303#endif
304 kfree(node);
305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100308 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * @num - Node number to use when creating the device.
310 *
311 * Initialize and register the node device.
312 */
Dou Liyanga7be6e52017-07-10 15:49:20 -0700313static int register_node(struct node *node, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 int error;
316
Kay Sievers10fbcf42011-12-21 14:48:43 -0800317 node->dev.id = num;
318 node->dev.bus = &node_subsys;
Yasuaki Ishimatsu8c7b5b42012-12-11 16:00:57 -0800319 node->dev.release = node_device_release;
Greg Kroah-Hartman7ca7ec42015-03-25 13:47:17 +0100320 node->dev.groups = node_dev_groups;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800321 error = device_register(&node->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Arvind Yadavc1cc0d52018-03-11 11:25:50 +0530323 if (error)
324 put_device(&node->dev);
325 else {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800326 hugetlb_register_node(node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700327
328 compaction_register_node(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 return error;
331}
332
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900333/**
334 * unregister_node - unregister a node device
335 * @node: node going away
336 *
337 * Unregisters a node device @node. All the devices on the node must be
338 * unregistered before calling this function.
339 */
340void unregister_node(struct node *node)
341{
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800342 hugetlb_unregister_node(node); /* no-op, if memoryless node */
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700343
Kay Sievers10fbcf42011-12-21 14:48:43 -0800344 device_unregister(&node->dev);
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900345}
346
Wen Congyang87327942012-12-11 16:00:56 -0800347struct node *node_devices[MAX_NUMNODES];
Yasunori Goto0fc44152006-06-27 02:53:38 -0700348
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700349/*
350 * register cpu under node
351 */
352int register_cpu_under_node(unsigned int cpu, unsigned int nid)
353{
Alex Chiang18307942009-12-14 17:59:08 -0800354 int ret;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800355 struct device *obj;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700356
Alex Chiangf8246f32009-12-14 17:59:06 -0800357 if (!node_online(nid))
358 return 0;
359
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800360 obj = get_cpu_device(cpu);
Alex Chiangf8246f32009-12-14 17:59:06 -0800361 if (!obj)
362 return 0;
363
Wen Congyang87327942012-12-11 16:00:56 -0800364 ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
Alex Chiangf8246f32009-12-14 17:59:06 -0800365 &obj->kobj,
366 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800367 if (ret)
368 return ret;
369
370 return sysfs_create_link(&obj->kobj,
Wen Congyang87327942012-12-11 16:00:56 -0800371 &node_devices[nid]->dev.kobj,
372 kobject_name(&node_devices[nid]->dev.kobj));
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700373}
374
375int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
376{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800377 struct device *obj;
Alex Chiangb9d52da2009-12-14 17:59:07 -0800378
379 if (!node_online(nid))
380 return 0;
381
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800382 obj = get_cpu_device(cpu);
Alex Chiangb9d52da2009-12-14 17:59:07 -0800383 if (!obj)
384 return 0;
385
Wen Congyang87327942012-12-11 16:00:56 -0800386 sysfs_remove_link(&node_devices[nid]->dev.kobj,
Alex Chiangb9d52da2009-12-14 17:59:07 -0800387 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800388 sysfs_remove_link(&obj->kobj,
Wen Congyang87327942012-12-11 16:00:56 -0800389 kobject_name(&node_devices[nid]->dev.kobj));
Alex Chiangb9d52da2009-12-14 17:59:07 -0800390
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700391 return 0;
392}
393
Gary Hadec04fc582009-01-06 14:39:14 -0800394#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700395static int __ref get_nid_for_pfn(unsigned long pfn)
Gary Hadec04fc582009-01-06 14:39:14 -0800396{
Gary Hadec04fc582009-01-06 14:39:14 -0800397 if (!pfn_valid_within(pfn))
398 return -1;
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700399#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
Thomas Gleixner8cdde382017-05-16 20:42:39 +0200400 if (system_state < SYSTEM_RUNNING)
Mel Gorman3a80a7f2015-06-30 14:57:02 -0700401 return early_pfn_to_nid(pfn);
402#endif
Gary Hadec04fc582009-01-06 14:39:14 -0800403 return pfn_to_nid(pfn);
404}
405
406/* register memory section under specified node if it spans that node */
Oscar Salvador4fbce632018-08-17 15:46:22 -0700407int register_mem_sect_under_node(struct memory_block *mem_blk, void *arg)
Gary Hadec04fc582009-01-06 14:39:14 -0800408{
Oscar Salvador4fbce632018-08-17 15:46:22 -0700409 int ret, nid = *(int *)arg;
Gary Hadec04fc582009-01-06 14:39:14 -0800410 unsigned long pfn, sect_start_pfn, sect_end_pfn;
411
Pavel Tatashind0dc12e2018-04-05 16:23:00 -0700412 mem_blk->nid = nid;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600413
414 sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
415 sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
416 sect_end_pfn += PAGES_PER_SECTION - 1;
Gary Hadec04fc582009-01-06 14:39:14 -0800417 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
418 int page_nid;
419
Yinghai Lu04697852015-09-04 15:42:39 -0700420 /*
421 * memory block could have several absent sections from start.
422 * skip pfn range from absent section
423 */
424 if (!pfn_present(pfn)) {
425 pfn = round_down(pfn + PAGES_PER_SECTION,
426 PAGES_PER_SECTION) - 1;
427 continue;
428 }
429
Pavel Tatashinfc44f7f2018-04-05 16:22:56 -0700430 /*
431 * We need to check if page belongs to nid only for the boot
432 * case, during hotplug we know that all pages in the memory
433 * block belong to the same node.
434 */
Oscar Salvador4fbce632018-08-17 15:46:22 -0700435 if (system_state == SYSTEM_BOOTING) {
Pavel Tatashinfc44f7f2018-04-05 16:22:56 -0700436 page_nid = get_nid_for_pfn(pfn);
437 if (page_nid < 0)
438 continue;
439 if (page_nid != nid)
440 continue;
441 }
Wen Congyang87327942012-12-11 16:00:56 -0800442 ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800443 &mem_blk->dev.kobj,
444 kobject_name(&mem_blk->dev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800445 if (ret)
446 return ret;
447
Kay Sievers10fbcf42011-12-21 14:48:43 -0800448 return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
Wen Congyang87327942012-12-11 16:00:56 -0800449 &node_devices[nid]->dev.kobj,
450 kobject_name(&node_devices[nid]->dev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800451 }
452 /* mem section does not span the specified node */
453 return 0;
454}
455
456/* unregister memory section under all nodes that it spans */
Nathan Fontenotd3360162011-01-20 10:44:29 -0600457int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
458 unsigned long phys_index)
Gary Hadec04fc582009-01-06 14:39:14 -0800459{
David Rientjes9ae49fa2009-12-14 17:59:46 -0800460 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
Gary Hadec04fc582009-01-06 14:39:14 -0800461 unsigned long pfn, sect_start_pfn, sect_end_pfn;
462
David Rientjes9ae49fa2009-12-14 17:59:46 -0800463 if (!mem_blk) {
464 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800465 return -EFAULT;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800466 }
467 if (!unlinked_nodes)
468 return -ENOMEM;
469 nodes_clear(*unlinked_nodes);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600470
471 sect_start_pfn = section_nr_to_pfn(phys_index);
Gary Hadec04fc582009-01-06 14:39:14 -0800472 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
473 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
Roel Kluin47504982009-03-10 12:55:45 -0700474 int nid;
Gary Hadec04fc582009-01-06 14:39:14 -0800475
476 nid = get_nid_for_pfn(pfn);
477 if (nid < 0)
478 continue;
479 if (!node_online(nid))
480 continue;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800481 if (node_test_and_set(nid, *unlinked_nodes))
Gary Hadec04fc582009-01-06 14:39:14 -0800482 continue;
Wen Congyang87327942012-12-11 16:00:56 -0800483 sysfs_remove_link(&node_devices[nid]->dev.kobj,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800484 kobject_name(&mem_blk->dev.kobj));
485 sysfs_remove_link(&mem_blk->dev.kobj,
Wen Congyang87327942012-12-11 16:00:56 -0800486 kobject_name(&node_devices[nid]->dev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800487 }
David Rientjes9ae49fa2009-12-14 17:59:46 -0800488 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800489 return 0;
490}
491
Oscar Salvador4fbce632018-08-17 15:46:22 -0700492int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
Gary Hadec04fc582009-01-06 14:39:14 -0800493{
Oscar Salvador4fbce632018-08-17 15:46:22 -0700494 return walk_memory_range(start_pfn, end_pfn, (void *)&nid,
495 register_mem_sect_under_node);
Gary Hadec04fc582009-01-06 14:39:14 -0800496}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800497
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800498#ifdef CONFIG_HUGETLBFS
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800499/*
500 * Handle per node hstate attribute [un]registration on transistions
501 * to/from memoryless state.
502 */
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800503static void node_hugetlb_work(struct work_struct *work)
504{
505 struct node *node = container_of(work, struct node, node_work);
506
507 /*
508 * We only get here when a node transitions to/from memoryless state.
509 * We can detect which transition occurred by examining whether the
510 * node has memory now. hugetlb_register_node() already check this
511 * so we try to register the attributes. If that fails, then the
512 * node has transitioned to memoryless, try to unregister the
513 * attributes.
514 */
515 if (!hugetlb_register_node(node))
516 hugetlb_unregister_node(node);
517}
518
519static void init_node_hugetlb_work(int nid)
520{
Wen Congyang87327942012-12-11 16:00:56 -0800521 INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800522}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800523
524static int node_memory_callback(struct notifier_block *self,
525 unsigned long action, void *arg)
526{
527 struct memory_notify *mnb = arg;
528 int nid = mnb->status_change_nid;
529
530 switch (action) {
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800531 case MEM_ONLINE:
532 case MEM_OFFLINE:
533 /*
534 * offload per node hstate [un]registration to a work thread
535 * when transitioning to/from memoryless state.
536 */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800537 if (nid != NUMA_NO_NODE)
Wen Congyang87327942012-12-11 16:00:56 -0800538 schedule_work(&node_devices[nid]->node_work);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800539 break;
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800540
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800541 case MEM_GOING_ONLINE:
542 case MEM_GOING_OFFLINE:
543 case MEM_CANCEL_ONLINE:
544 case MEM_CANCEL_OFFLINE:
545 default:
546 break;
547 }
548
549 return NOTIFY_OK;
550}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800551#endif /* CONFIG_HUGETLBFS */
Michal Hocko9037a992017-07-06 15:37:49 -0700552#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800553
554#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
555 !defined(CONFIG_HUGETLBFS)
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800556static inline int node_memory_callback(struct notifier_block *self,
557 unsigned long action, void *arg)
558{
559 return NOTIFY_OK;
560}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800561
562static void init_node_hugetlb_work(int nid) { }
563
564#endif
Gary Hadec04fc582009-01-06 14:39:14 -0800565
Michal Hocko9037a992017-07-06 15:37:49 -0700566int __register_one_node(int nid)
Yasunori Goto0fc44152006-06-27 02:53:38 -0700567{
Michal Hocko9037a992017-07-06 15:37:49 -0700568 int error;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700569 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700570
Michal Hocko9037a992017-07-06 15:37:49 -0700571 node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
572 if (!node_devices[nid])
573 return -ENOMEM;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700574
Dou Liyanga7be6e52017-07-10 15:49:20 -0700575 error = register_node(node_devices[nid], nid);
Wen Congyang87327942012-12-11 16:00:56 -0800576
Michal Hocko9037a992017-07-06 15:37:49 -0700577 /* link cpu under this node */
578 for_each_present_cpu(cpu) {
579 if (cpu_to_node(cpu) == nid)
580 register_cpu_under_node(cpu, nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -0700581 }
582
Michal Hocko9037a992017-07-06 15:37:49 -0700583 /* initialize work queue for memory hot plug */
584 init_node_hugetlb_work(nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -0700585
Michal Hocko9037a992017-07-06 15:37:49 -0700586 return error;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700587}
588
589void unregister_one_node(int nid)
590{
Xishi Qiu92d585e2014-03-06 17:18:21 +0800591 if (!node_devices[nid])
592 return;
593
Wen Congyang87327942012-12-11 16:00:56 -0800594 unregister_node(node_devices[nid]);
Wen Congyang87327942012-12-11 16:00:56 -0800595 node_devices[nid] = NULL;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700596}
597
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700598/*
599 * node states attributes
600 */
601
602static ssize_t print_nodes_state(enum node_states state, char *buf)
603{
604 int n;
605
Tejun Heof799b1a2015-02-13 14:37:56 -0800606 n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
607 nodemask_pr_args(&node_states[state]));
Ryota Ozakif6238812012-05-29 15:06:20 -0700608 buf[n++] = '\n';
609 buf[n] = '\0';
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700610 return n;
611}
612
Andi Kleenb15f5622010-01-05 12:47:59 +0100613struct node_attr {
Kay Sievers10fbcf42011-12-21 14:48:43 -0800614 struct device_attribute attr;
Andi Kleenb15f5622010-01-05 12:47:59 +0100615 enum node_states state;
616};
617
Kay Sievers10fbcf42011-12-21 14:48:43 -0800618static ssize_t show_node_state(struct device *dev,
619 struct device_attribute *attr, char *buf)
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700620{
Andi Kleenb15f5622010-01-05 12:47:59 +0100621 struct node_attr *na = container_of(attr, struct node_attr, attr);
622 return print_nodes_state(na->state, buf);
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700623}
624
Andi Kleenb15f5622010-01-05 12:47:59 +0100625#define _NODE_ATTR(name, state) \
Kay Sievers10fbcf42011-12-21 14:48:43 -0800626 { __ATTR(name, 0444, show_node_state, NULL), state }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700627
Andi Kleenb15f5622010-01-05 12:47:59 +0100628static struct node_attr node_state_attr[] = {
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800629 [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
630 [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
631 [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700632#ifdef CONFIG_HIGHMEM
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800633 [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700634#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -0800635 [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800636 [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700637};
638
Kay Sievers10fbcf42011-12-21 14:48:43 -0800639static struct attribute *node_state_attrs[] = {
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800640 &node_state_attr[N_POSSIBLE].attr.attr,
641 &node_state_attr[N_ONLINE].attr.attr,
642 &node_state_attr[N_NORMAL_MEMORY].attr.attr,
Andi Kleen3701cde2010-01-05 12:48:04 +0100643#ifdef CONFIG_HIGHMEM
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800644 &node_state_attr[N_HIGH_MEMORY].attr.attr,
Andi Kleen3701cde2010-01-05 12:48:04 +0100645#endif
Lai Jiangshan20b2f522012-12-12 13:52:00 -0800646 &node_state_attr[N_MEMORY].attr.attr,
Lai Jiangshanfcf07d22012-12-11 16:03:13 -0800647 &node_state_attr[N_CPU].attr.attr,
Andi Kleen3701cde2010-01-05 12:48:04 +0100648 NULL
649};
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700650
Kay Sievers10fbcf42011-12-21 14:48:43 -0800651static struct attribute_group memory_root_attr_group = {
652 .attrs = node_state_attrs,
653};
654
655static const struct attribute_group *cpu_root_attr_groups[] = {
656 &memory_root_attr_group,
657 NULL,
658};
659
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800660#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900661static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700663 int ret;
664
Andi Kleen3701cde2010-01-05 12:48:04 +0100665 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
666 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
667
Kay Sievers10fbcf42011-12-21 14:48:43 -0800668 ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800669 if (!ret) {
Andrew Morton6e259e72013-04-29 15:08:07 -0700670 static struct notifier_block node_memory_callback_nb = {
671 .notifier_call = node_memory_callback,
672 .priority = NODE_CALLBACK_PRI,
673 };
674 register_hotmemory_notifier(&node_memory_callback_nb);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800675 }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700676
677 /*
678 * Note: we're not going to unregister the node class if we fail
679 * to register the node state class attribute files.
680 */
681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683postcore_initcall(register_node_type);