blob: ad43185ec15adebc4d48d80ffb58275b18827da3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/node.c - basic Node class support
3 */
4
5#include <linux/sysdev.h>
6#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/node.h>
11#include <linux/hugetlb.h>
12#include <linux/cpumask.h>
13#include <linux/topology.h>
14#include <linux/nodemask.h>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070015#include <linux/cpu.h>
Lee Schermerhornbde631a2007-10-16 01:26:27 -070016#include <linux/device.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070017#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Andi Kleen3701cde2010-01-05 12:48:04 +010019static struct sysdev_class_attribute *node_state_attrs[];
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static struct sysdev_class node_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010022 .name = "node",
Andi Kleen3701cde2010-01-05 12:48:04 +010023 .attrs = node_state_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024};
25
26
Mike Travis39106dc2008-04-08 11:43:03 -070027static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct node *node_dev = to_node(dev);
Rusty Russella70f7302009-03-13 14:49:46 +103030 const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int len;
32
Mike Travis39106dc2008-04-08 11:43:03 -070033 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
34 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Mike Travis39106dc2008-04-08 11:43:03 -070036 len = type?
Rusty Russell29c01772008-12-13 21:20:25 +103037 cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
38 cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travisc5f59f02008-04-04 18:11:10 -070039 buf[len++] = '\n';
40 buf[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return len;
42}
43
Andi Kleen4a0b2b42008-07-01 18:48:41 +020044static inline ssize_t node_read_cpumask(struct sys_device *dev,
45 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070046{
47 return node_read_cpumap(dev, 0, buf);
48}
Andi Kleen4a0b2b42008-07-01 18:48:41 +020049static inline ssize_t node_read_cpulist(struct sys_device *dev,
50 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070051{
52 return node_read_cpumap(dev, 1, buf);
53}
54
55static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
56static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define K(x) ((x) << (PAGE_SHIFT - 10))
Andi Kleen4a0b2b42008-07-01 18:48:41 +020059static ssize_t node_read_meminfo(struct sys_device * dev,
60 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int n;
63 int nid = dev->id;
64 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 si_meminfo_node(&i, nid);
Martin Hicksc07e02d2005-09-03 15:55:11 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 n = sprintf(buf, "\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -070069 "Node %d MemTotal: %8lu kB\n"
70 "Node %d MemFree: %8lu kB\n"
71 "Node %d MemUsed: %8lu kB\n"
72 "Node %d Active: %8lu kB\n"
73 "Node %d Inactive: %8lu kB\n"
74 "Node %d Active(anon): %8lu kB\n"
75 "Node %d Inactive(anon): %8lu kB\n"
76 "Node %d Active(file): %8lu kB\n"
77 "Node %d Inactive(file): %8lu kB\n"
Nick Piggin5344b7e2008-10-18 20:26:51 -070078 "Node %d Unevictable: %8lu kB\n"
79 "Node %d Mlocked: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070080#ifdef CONFIG_HIGHMEM
Rik van Riel4f98a2f2008-10-18 20:26:32 -070081 "Node %d HighTotal: %8lu kB\n"
82 "Node %d HighFree: %8lu kB\n"
83 "Node %d LowTotal: %8lu kB\n"
84 "Node %d LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070085#endif
Rik van Riel4f98a2f2008-10-18 20:26:32 -070086 "Node %d Dirty: %8lu kB\n"
87 "Node %d Writeback: %8lu kB\n"
88 "Node %d FilePages: %8lu kB\n"
89 "Node %d Mapped: %8lu kB\n"
90 "Node %d AnonPages: %8lu kB\n"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -070091 "Node %d Shmem: %8lu kB\n"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -070092 "Node %d KernelStack: %8lu kB\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -070093 "Node %d PageTables: %8lu kB\n"
94 "Node %d NFS_Unstable: %8lu kB\n"
95 "Node %d Bounce: %8lu kB\n"
96 "Node %d WritebackTmp: %8lu kB\n"
97 "Node %d Slab: %8lu kB\n"
98 "Node %d SReclaimable: %8lu kB\n"
99 "Node %d SUnreclaim: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 nid, K(i.totalram),
101 nid, K(i.freeram),
102 nid, K(i.totalram - i.freeram),
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700103 nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
104 node_page_state(nid, NR_ACTIVE_FILE)),
105 nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
106 node_page_state(nid, NR_INACTIVE_FILE)),
107 nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
108 nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
109 nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
110 nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700111 nid, K(node_page_state(nid, NR_UNEVICTABLE)),
Nick Piggin5344b7e2008-10-18 20:26:51 -0700112 nid, K(node_page_state(nid, NR_MLOCK)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700113#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 nid, K(i.totalhigh),
115 nid, K(i.freehigh),
116 nid, K(i.totalram - i.totalhigh),
Martin Hicksc07e02d2005-09-03 15:55:11 -0700117 nid, K(i.freeram - i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700118#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700119 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700120 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -0700121 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700122 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700123 nid, K(node_page_state(nid, NR_ANON_PAGES)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700124 nid, K(node_page_state(nid, NR_SHMEM)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700125 nid, node_page_state(nid, NR_KERNEL_STACK) *
126 THREAD_SIZE / 1024,
Christoph Lameterdf849a12006-06-30 01:55:38 -0700127 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700128 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700129 nid, K(node_page_state(nid, NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700130 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700131 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
132 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
133 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
134 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 n += hugetlb_report_node_meminfo(nid, buf + n);
136 return n;
137}
138
139#undef K
140static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
141
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200142static ssize_t node_read_numastat(struct sys_device * dev,
143 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return sprintf(buf,
146 "numa_hit %lu\n"
147 "numa_miss %lu\n"
148 "numa_foreign %lu\n"
149 "interleave_hit %lu\n"
150 "local_node %lu\n"
151 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700152 node_page_state(dev->id, NUMA_HIT),
153 node_page_state(dev->id, NUMA_MISS),
154 node_page_state(dev->id, NUMA_FOREIGN),
155 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
156 node_page_state(dev->id, NUMA_LOCAL),
157 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
160
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200161static ssize_t node_read_distance(struct sys_device * dev,
162 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int nid = dev->id;
165 int len = 0;
166 int i;
167
168 /* buf currently PAGE_SIZE, need ~4 chars per node */
169 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
170
171 for_each_online_node(i)
172 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
173
174 len += sprintf(buf + len, "\n");
175 return len;
176}
177static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
178
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800179#ifdef CONFIG_HUGETLBFS
180/*
181 * hugetlbfs per node attributes registration interface:
182 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800183 * it will register its per node attributes for all online nodes with
184 * memory. It will also call register_hugetlbfs_with_node(), below, to
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800185 * register its attribute registration functions with this node driver.
186 * Once these hooks have been initialized, the node driver will call into
187 * the hugetlb module to [un]register attributes for hot-plugged nodes.
188 */
189static node_registration_func_t __hugetlb_register_node;
190static node_registration_func_t __hugetlb_unregister_node;
191
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800192static inline bool hugetlb_register_node(struct node *node)
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800193{
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800194 if (__hugetlb_register_node &&
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800195 node_state(node->sysdev.id, N_HIGH_MEMORY)) {
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800196 __hugetlb_register_node(node);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800197 return true;
198 }
199 return false;
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800200}
201
202static inline void hugetlb_unregister_node(struct node *node)
203{
204 if (__hugetlb_unregister_node)
205 __hugetlb_unregister_node(node);
206}
207
208void register_hugetlbfs_with_node(node_registration_func_t doregister,
209 node_registration_func_t unregister)
210{
211 __hugetlb_register_node = doregister;
212 __hugetlb_unregister_node = unregister;
213}
214#else
215static inline void hugetlb_register_node(struct node *node) {}
216
217static inline void hugetlb_unregister_node(struct node *node) {}
218#endif
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100222 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * @num - Node number to use when creating the device.
224 *
225 * Initialize and register the node device.
226 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900227int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int error;
230
231 node->sysdev.id = num;
232 node->sysdev.cls = &node_class;
233 error = sysdev_register(&node->sysdev);
234
235 if (!error){
236 sysdev_create_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700237 sysdev_create_file(&node->sysdev, &attr_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 sysdev_create_file(&node->sysdev, &attr_meminfo);
239 sysdev_create_file(&node->sysdev, &attr_numastat);
240 sysdev_create_file(&node->sysdev, &attr_distance);
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700241
242 scan_unevictable_register_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800243
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800244 hugetlb_register_node(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 return error;
247}
248
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900249/**
250 * unregister_node - unregister a node device
251 * @node: node going away
252 *
253 * Unregisters a node device @node. All the devices on the node must be
254 * unregistered before calling this function.
255 */
256void unregister_node(struct node *node)
257{
258 sysdev_remove_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700259 sysdev_remove_file(&node->sysdev, &attr_cpulist);
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900260 sysdev_remove_file(&node->sysdev, &attr_meminfo);
261 sysdev_remove_file(&node->sysdev, &attr_numastat);
262 sysdev_remove_file(&node->sysdev, &attr_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700264 scan_unevictable_unregister_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800265 hugetlb_unregister_node(node); /* no-op, if memoryless node */
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700266
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900267 sysdev_unregister(&node->sysdev);
268}
269
Yasunori Goto0fc44152006-06-27 02:53:38 -0700270struct node node_devices[MAX_NUMNODES];
271
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700272/*
273 * register cpu under node
274 */
275int register_cpu_under_node(unsigned int cpu, unsigned int nid)
276{
Alex Chiang18307942009-12-14 17:59:08 -0800277 int ret;
Alex Chiangf8246f32009-12-14 17:59:06 -0800278 struct sys_device *obj;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700279
Alex Chiangf8246f32009-12-14 17:59:06 -0800280 if (!node_online(nid))
281 return 0;
282
283 obj = get_cpu_sysdev(cpu);
284 if (!obj)
285 return 0;
286
Alex Chiang18307942009-12-14 17:59:08 -0800287 ret = sysfs_create_link(&node_devices[nid].sysdev.kobj,
Alex Chiangf8246f32009-12-14 17:59:06 -0800288 &obj->kobj,
289 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800290 if (ret)
291 return ret;
292
293 return sysfs_create_link(&obj->kobj,
294 &node_devices[nid].sysdev.kobj,
295 kobject_name(&node_devices[nid].sysdev.kobj));
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700296}
297
298int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
299{
Alex Chiangb9d52da2009-12-14 17:59:07 -0800300 struct sys_device *obj;
301
302 if (!node_online(nid))
303 return 0;
304
305 obj = get_cpu_sysdev(cpu);
306 if (!obj)
307 return 0;
308
309 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
310 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800311 sysfs_remove_link(&obj->kobj,
312 kobject_name(&node_devices[nid].sysdev.kobj));
Alex Chiangb9d52da2009-12-14 17:59:07 -0800313
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700314 return 0;
315}
316
Gary Hadec04fc582009-01-06 14:39:14 -0800317#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
318#define page_initialized(page) (page->lru.next)
319
320static int get_nid_for_pfn(unsigned long pfn)
321{
322 struct page *page;
323
324 if (!pfn_valid_within(pfn))
325 return -1;
326 page = pfn_to_page(pfn);
327 if (!page_initialized(page))
328 return -1;
329 return pfn_to_nid(pfn);
330}
331
332/* register memory section under specified node if it spans that node */
333int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
334{
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800335 int ret;
Gary Hadec04fc582009-01-06 14:39:14 -0800336 unsigned long pfn, sect_start_pfn, sect_end_pfn;
337
338 if (!mem_blk)
339 return -EFAULT;
340 if (!node_online(nid))
341 return 0;
342 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
343 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
344 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
345 int page_nid;
346
347 page_nid = get_nid_for_pfn(pfn);
348 if (page_nid < 0)
349 continue;
350 if (page_nid != nid)
351 continue;
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800352 ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
Gary Hadec04fc582009-01-06 14:39:14 -0800353 &mem_blk->sysdev.kobj,
354 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800355 if (ret)
356 return ret;
357
358 return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj,
359 &node_devices[nid].sysdev.kobj,
360 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800361 }
362 /* mem section does not span the specified node */
363 return 0;
364}
365
366/* unregister memory section under all nodes that it spans */
367int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
368{
David Rientjes9ae49fa2009-12-14 17:59:46 -0800369 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
Gary Hadec04fc582009-01-06 14:39:14 -0800370 unsigned long pfn, sect_start_pfn, sect_end_pfn;
371
David Rientjes9ae49fa2009-12-14 17:59:46 -0800372 if (!mem_blk) {
373 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800374 return -EFAULT;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800375 }
376 if (!unlinked_nodes)
377 return -ENOMEM;
378 nodes_clear(*unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800379 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
380 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
381 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
Roel Kluin47504982009-03-10 12:55:45 -0700382 int nid;
Gary Hadec04fc582009-01-06 14:39:14 -0800383
384 nid = get_nid_for_pfn(pfn);
385 if (nid < 0)
386 continue;
387 if (!node_online(nid))
388 continue;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800389 if (node_test_and_set(nid, *unlinked_nodes))
Gary Hadec04fc582009-01-06 14:39:14 -0800390 continue;
391 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
392 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800393 sysfs_remove_link(&mem_blk->sysdev.kobj,
394 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800395 }
David Rientjes9ae49fa2009-12-14 17:59:46 -0800396 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800397 return 0;
398}
399
400static int link_mem_sections(int nid)
401{
402 unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
403 unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
404 unsigned long pfn;
405 int err = 0;
406
407 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
408 unsigned long section_nr = pfn_to_section_nr(pfn);
409 struct mem_section *mem_sect;
410 struct memory_block *mem_blk;
411 int ret;
412
413 if (!present_section_nr(section_nr))
414 continue;
415 mem_sect = __nr_to_section(section_nr);
416 mem_blk = find_memory_block(mem_sect);
417 ret = register_mem_sect_under_node(mem_blk, nid);
418 if (!err)
419 err = ret;
420
421 /* discard ref obtained in find_memory_block() */
422 kobject_put(&mem_blk->sysdev.kobj);
423 }
424 return err;
425}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800426
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800427#ifdef CONFIG_HUGETLBFS
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800428/*
429 * Handle per node hstate attribute [un]registration on transistions
430 * to/from memoryless state.
431 */
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800432static void node_hugetlb_work(struct work_struct *work)
433{
434 struct node *node = container_of(work, struct node, node_work);
435
436 /*
437 * We only get here when a node transitions to/from memoryless state.
438 * We can detect which transition occurred by examining whether the
439 * node has memory now. hugetlb_register_node() already check this
440 * so we try to register the attributes. If that fails, then the
441 * node has transitioned to memoryless, try to unregister the
442 * attributes.
443 */
444 if (!hugetlb_register_node(node))
445 hugetlb_unregister_node(node);
446}
447
448static void init_node_hugetlb_work(int nid)
449{
450 INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work);
451}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800452
453static int node_memory_callback(struct notifier_block *self,
454 unsigned long action, void *arg)
455{
456 struct memory_notify *mnb = arg;
457 int nid = mnb->status_change_nid;
458
459 switch (action) {
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800460 case MEM_ONLINE:
461 case MEM_OFFLINE:
462 /*
463 * offload per node hstate [un]registration to a work thread
464 * when transitioning to/from memoryless state.
465 */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800466 if (nid != NUMA_NO_NODE)
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800467 schedule_work(&node_devices[nid].node_work);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800468 break;
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800469
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800470 case MEM_GOING_ONLINE:
471 case MEM_GOING_OFFLINE:
472 case MEM_CANCEL_ONLINE:
473 case MEM_CANCEL_OFFLINE:
474 default:
475 break;
476 }
477
478 return NOTIFY_OK;
479}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800480#endif /* CONFIG_HUGETLBFS */
481#else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800482
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800483static int link_mem_sections(int nid) { return 0; }
484#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
485
486#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
487 !defined(CONFIG_HUGETLBFS)
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800488static inline int node_memory_callback(struct notifier_block *self,
489 unsigned long action, void *arg)
490{
491 return NOTIFY_OK;
492}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800493
494static void init_node_hugetlb_work(int nid) { }
495
496#endif
Gary Hadec04fc582009-01-06 14:39:14 -0800497
Yasunori Goto0fc44152006-06-27 02:53:38 -0700498int register_one_node(int nid)
499{
500 int error = 0;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700501 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700502
503 if (node_online(nid)) {
504 int p_node = parent_node(nid);
505 struct node *parent = NULL;
506
507 if (p_node != nid)
508 parent = &node_devices[p_node];
509
510 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700511
512 /* link cpu under this node */
513 for_each_present_cpu(cpu) {
514 if (cpu_to_node(cpu) == nid)
515 register_cpu_under_node(cpu, nid);
516 }
Gary Hadec04fc582009-01-06 14:39:14 -0800517
518 /* link memory sections under this node */
519 error = link_mem_sections(nid);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800520
521 /* initialize work queue for memory hot plug */
522 init_node_hugetlb_work(nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -0700523 }
524
525 return error;
526
527}
528
529void unregister_one_node(int nid)
530{
531 unregister_node(&node_devices[nid]);
532}
533
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700534/*
535 * node states attributes
536 */
537
538static ssize_t print_nodes_state(enum node_states state, char *buf)
539{
540 int n;
541
542 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
543 if (n > 0 && PAGE_SIZE > n + 1) {
544 *(buf + n++) = '\n';
545 *(buf + n++) = '\0';
546 }
547 return n;
548}
549
Andi Kleenb15f5622010-01-05 12:47:59 +0100550struct node_attr {
551 struct sysdev_class_attribute attr;
552 enum node_states state;
553};
554
555static ssize_t show_node_state(struct sysdev_class *class,
556 struct sysdev_class_attribute *attr, char *buf)
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700557{
Andi Kleenb15f5622010-01-05 12:47:59 +0100558 struct node_attr *na = container_of(attr, struct node_attr, attr);
559 return print_nodes_state(na->state, buf);
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700560}
561
Andi Kleenb15f5622010-01-05 12:47:59 +0100562#define _NODE_ATTR(name, state) \
563 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700564
Andi Kleenb15f5622010-01-05 12:47:59 +0100565static struct node_attr node_state_attr[] = {
566 _NODE_ATTR(possible, N_POSSIBLE),
567 _NODE_ATTR(online, N_ONLINE),
568 _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
569 _NODE_ATTR(has_cpu, N_CPU),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700570#ifdef CONFIG_HIGHMEM
Andi Kleenb15f5622010-01-05 12:47:59 +0100571 _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700572#endif
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700573};
574
Andi Kleen3701cde2010-01-05 12:48:04 +0100575static struct sysdev_class_attribute *node_state_attrs[] = {
576 &node_state_attr[0].attr,
577 &node_state_attr[1].attr,
578 &node_state_attr[2].attr,
579 &node_state_attr[3].attr,
580#ifdef CONFIG_HIGHMEM
581 &node_state_attr[4].attr,
582#endif
583 NULL
584};
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700585
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800586#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900587static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700589 int ret;
590
Andi Kleen3701cde2010-01-05 12:48:04 +0100591 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
592 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
593
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700594 ret = sysdev_class_register(&node_class);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800595 if (!ret) {
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800596 hotplug_memory_notifier(node_memory_callback,
597 NODE_CALLBACK_PRI);
598 }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700599
600 /*
601 * Note: we're not going to unregister the node class if we fail
602 * to register the node state class attribute files.
603 */
604 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606postcore_initcall(register_node_type);