blob: 985abd7f49a7b5c03282fe6d53f7f8ad647fd095 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Andi Kleen3701cde2010-01-05 12:48:04 +010020static struct sysdev_class_attribute *node_state_attrs[];
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static struct sysdev_class node_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010023 .name = "node",
Andi Kleen3701cde2010-01-05 12:48:04 +010024 .attrs = node_state_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27
Mike Travis39106dc2008-04-08 11:43:03 -070028static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 struct node *node_dev = to_node(dev);
Rusty Russella70f7302009-03-13 14:49:46 +103031 const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int len;
33
Mike Travis39106dc2008-04-08 11:43:03 -070034 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
35 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mike Travis39106dc2008-04-08 11:43:03 -070037 len = type?
Rusty Russell29c01772008-12-13 21:20:25 +103038 cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
39 cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travisc5f59f02008-04-04 18:11:10 -070040 buf[len++] = '\n';
41 buf[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return len;
43}
44
Andi Kleen4a0b2b42008-07-01 18:48:41 +020045static inline ssize_t node_read_cpumask(struct sys_device *dev,
46 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070047{
48 return node_read_cpumap(dev, 0, buf);
49}
Andi Kleen4a0b2b42008-07-01 18:48:41 +020050static inline ssize_t node_read_cpulist(struct sys_device *dev,
51 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070052{
53 return node_read_cpumap(dev, 1, buf);
54}
55
56static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
57static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define K(x) ((x) << (PAGE_SHIFT - 10))
Andi Kleen4a0b2b42008-07-01 18:48:41 +020060static ssize_t node_read_meminfo(struct sys_device * dev,
61 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int n;
64 int nid = dev->id;
65 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 si_meminfo_node(&i, nid);
Martin Hicksc07e02d2005-09-03 15:55:11 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 n = sprintf(buf, "\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -070070 "Node %d MemTotal: %8lu kB\n"
71 "Node %d MemFree: %8lu kB\n"
72 "Node %d MemUsed: %8lu kB\n"
73 "Node %d Active: %8lu kB\n"
74 "Node %d Inactive: %8lu kB\n"
75 "Node %d Active(anon): %8lu kB\n"
76 "Node %d Inactive(anon): %8lu kB\n"
77 "Node %d Active(file): %8lu kB\n"
78 "Node %d Inactive(file): %8lu kB\n"
Nick Piggin5344b7e2008-10-18 20:26:51 -070079 "Node %d Unevictable: %8lu kB\n"
80 "Node %d Mlocked: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070081#ifdef CONFIG_HIGHMEM
Rik van Riel4f98a2f2008-10-18 20:26:32 -070082 "Node %d HighTotal: %8lu kB\n"
83 "Node %d HighFree: %8lu kB\n"
84 "Node %d LowTotal: %8lu kB\n"
85 "Node %d LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070086#endif
Rik van Riel4f98a2f2008-10-18 20:26:32 -070087 "Node %d Dirty: %8lu kB\n"
88 "Node %d Writeback: %8lu kB\n"
89 "Node %d FilePages: %8lu kB\n"
90 "Node %d Mapped: %8lu kB\n"
91 "Node %d AnonPages: %8lu kB\n"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -070092 "Node %d Shmem: %8lu kB\n"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -070093 "Node %d KernelStack: %8lu kB\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -070094 "Node %d PageTables: %8lu kB\n"
95 "Node %d NFS_Unstable: %8lu kB\n"
96 "Node %d Bounce: %8lu kB\n"
97 "Node %d WritebackTmp: %8lu kB\n"
98 "Node %d Slab: %8lu kB\n"
99 "Node %d SReclaimable: %8lu kB\n"
100 "Node %d SUnreclaim: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 nid, K(i.totalram),
102 nid, K(i.freeram),
103 nid, K(i.totalram - i.freeram),
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700104 nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
105 node_page_state(nid, NR_ACTIVE_FILE)),
106 nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
107 node_page_state(nid, NR_INACTIVE_FILE)),
108 nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
109 nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
110 nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
111 nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700112 nid, K(node_page_state(nid, NR_UNEVICTABLE)),
Nick Piggin5344b7e2008-10-18 20:26:51 -0700113 nid, K(node_page_state(nid, NR_MLOCK)),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700114#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 nid, K(i.totalhigh),
116 nid, K(i.freehigh),
117 nid, K(i.totalram - i.totalhigh),
Martin Hicksc07e02d2005-09-03 15:55:11 -0700118 nid, K(i.freeram - i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700119#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700120 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700121 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -0700122 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700123 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700124 nid, K(node_page_state(nid, NR_ANON_PAGES)),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700125 nid, K(node_page_state(nid, NR_SHMEM)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700126 nid, node_page_state(nid, NR_KERNEL_STACK) *
127 THREAD_SIZE / 1024,
Christoph Lameterdf849a12006-06-30 01:55:38 -0700128 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700129 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700130 nid, K(node_page_state(nid, NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700131 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700132 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
133 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
134 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
135 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 n += hugetlb_report_node_meminfo(nid, buf + n);
137 return n;
138}
139
140#undef K
141static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
142
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200143static ssize_t node_read_numastat(struct sys_device * dev,
144 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return sprintf(buf,
147 "numa_hit %lu\n"
148 "numa_miss %lu\n"
149 "numa_foreign %lu\n"
150 "interleave_hit %lu\n"
151 "local_node %lu\n"
152 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700153 node_page_state(dev->id, NUMA_HIT),
154 node_page_state(dev->id, NUMA_MISS),
155 node_page_state(dev->id, NUMA_FOREIGN),
156 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
157 node_page_state(dev->id, NUMA_LOCAL),
158 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
161
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200162static ssize_t node_read_distance(struct sys_device * dev,
163 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int nid = dev->id;
166 int len = 0;
167 int i;
168
David Rientjes12ee3c02010-03-10 14:50:21 -0800169 /*
170 * buf is currently PAGE_SIZE in length and each node needs 4 chars
171 * at the most (distance + space or newline).
172 */
173 BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 for_each_online_node(i)
176 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
177
178 len += sprintf(buf + len, "\n");
179 return len;
180}
181static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
182
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800183#ifdef CONFIG_HUGETLBFS
184/*
185 * hugetlbfs per node attributes registration interface:
186 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800187 * it will register its per node attributes for all online nodes with
188 * memory. It will also call register_hugetlbfs_with_node(), below, to
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800189 * register its attribute registration functions with this node driver.
190 * Once these hooks have been initialized, the node driver will call into
191 * the hugetlb module to [un]register attributes for hot-plugged nodes.
192 */
193static node_registration_func_t __hugetlb_register_node;
194static node_registration_func_t __hugetlb_unregister_node;
195
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800196static inline bool hugetlb_register_node(struct node *node)
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800197{
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800198 if (__hugetlb_register_node &&
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800199 node_state(node->sysdev.id, N_HIGH_MEMORY)) {
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800200 __hugetlb_register_node(node);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800201 return true;
202 }
203 return false;
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800204}
205
206static inline void hugetlb_unregister_node(struct node *node)
207{
208 if (__hugetlb_unregister_node)
209 __hugetlb_unregister_node(node);
210}
211
212void register_hugetlbfs_with_node(node_registration_func_t doregister,
213 node_registration_func_t unregister)
214{
215 __hugetlb_register_node = doregister;
216 __hugetlb_unregister_node = unregister;
217}
218#else
219static inline void hugetlb_register_node(struct node *node) {}
220
221static inline void hugetlb_unregister_node(struct node *node) {}
222#endif
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100226 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * @num - Node number to use when creating the device.
228 *
229 * Initialize and register the node device.
230 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900231int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int error;
234
235 node->sysdev.id = num;
236 node->sysdev.cls = &node_class;
237 error = sysdev_register(&node->sysdev);
238
239 if (!error){
240 sysdev_create_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700241 sysdev_create_file(&node->sysdev, &attr_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 sysdev_create_file(&node->sysdev, &attr_meminfo);
243 sysdev_create_file(&node->sysdev, &attr_numastat);
244 sysdev_create_file(&node->sysdev, &attr_distance);
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700245
246 scan_unevictable_register_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800247
Lee Schermerhorn9a305232009-12-14 17:58:25 -0800248 hugetlb_register_node(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 return error;
251}
252
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900253/**
254 * unregister_node - unregister a node device
255 * @node: node going away
256 *
257 * Unregisters a node device @node. All the devices on the node must be
258 * unregistered before calling this function.
259 */
260void unregister_node(struct node *node)
261{
262 sysdev_remove_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700263 sysdev_remove_file(&node->sysdev, &attr_cpulist);
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900264 sysdev_remove_file(&node->sysdev, &attr_meminfo);
265 sysdev_remove_file(&node->sysdev, &attr_numastat);
266 sysdev_remove_file(&node->sysdev, &attr_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700268 scan_unevictable_unregister_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800269 hugetlb_unregister_node(node); /* no-op, if memoryless node */
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700270
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900271 sysdev_unregister(&node->sysdev);
272}
273
Yasunori Goto0fc44152006-06-27 02:53:38 -0700274struct node node_devices[MAX_NUMNODES];
275
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700276/*
277 * register cpu under node
278 */
279int register_cpu_under_node(unsigned int cpu, unsigned int nid)
280{
Alex Chiang18307942009-12-14 17:59:08 -0800281 int ret;
Alex Chiangf8246f32009-12-14 17:59:06 -0800282 struct sys_device *obj;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700283
Alex Chiangf8246f32009-12-14 17:59:06 -0800284 if (!node_online(nid))
285 return 0;
286
287 obj = get_cpu_sysdev(cpu);
288 if (!obj)
289 return 0;
290
Alex Chiang18307942009-12-14 17:59:08 -0800291 ret = sysfs_create_link(&node_devices[nid].sysdev.kobj,
Alex Chiangf8246f32009-12-14 17:59:06 -0800292 &obj->kobj,
293 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800294 if (ret)
295 return ret;
296
297 return sysfs_create_link(&obj->kobj,
298 &node_devices[nid].sysdev.kobj,
299 kobject_name(&node_devices[nid].sysdev.kobj));
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700300}
301
302int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
303{
Alex Chiangb9d52da2009-12-14 17:59:07 -0800304 struct sys_device *obj;
305
306 if (!node_online(nid))
307 return 0;
308
309 obj = get_cpu_sysdev(cpu);
310 if (!obj)
311 return 0;
312
313 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
314 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800315 sysfs_remove_link(&obj->kobj,
316 kobject_name(&node_devices[nid].sysdev.kobj));
Alex Chiangb9d52da2009-12-14 17:59:07 -0800317
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700318 return 0;
319}
320
Gary Hadec04fc582009-01-06 14:39:14 -0800321#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
322#define page_initialized(page) (page->lru.next)
323
324static int get_nid_for_pfn(unsigned long pfn)
325{
326 struct page *page;
327
328 if (!pfn_valid_within(pfn))
329 return -1;
330 page = pfn_to_page(pfn);
331 if (!page_initialized(page))
332 return -1;
333 return pfn_to_nid(pfn);
334}
335
336/* register memory section under specified node if it spans that node */
337int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
338{
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800339 int ret;
Gary Hadec04fc582009-01-06 14:39:14 -0800340 unsigned long pfn, sect_start_pfn, sect_end_pfn;
341
342 if (!mem_blk)
343 return -EFAULT;
344 if (!node_online(nid))
345 return 0;
346 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
347 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
348 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
349 int page_nid;
350
351 page_nid = get_nid_for_pfn(pfn);
352 if (page_nid < 0)
353 continue;
354 if (page_nid != nid)
355 continue;
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800356 ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
Gary Hadec04fc582009-01-06 14:39:14 -0800357 &mem_blk->sysdev.kobj,
358 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800359 if (ret)
360 return ret;
361
362 return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj,
363 &node_devices[nid].sysdev.kobj,
364 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800365 }
366 /* mem section does not span the specified node */
367 return 0;
368}
369
370/* unregister memory section under all nodes that it spans */
371int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
372{
David Rientjes9ae49fa2009-12-14 17:59:46 -0800373 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
Gary Hadec04fc582009-01-06 14:39:14 -0800374 unsigned long pfn, sect_start_pfn, sect_end_pfn;
375
David Rientjes9ae49fa2009-12-14 17:59:46 -0800376 if (!mem_blk) {
377 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800378 return -EFAULT;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800379 }
380 if (!unlinked_nodes)
381 return -ENOMEM;
382 nodes_clear(*unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800383 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
384 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
385 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
Roel Kluin47504982009-03-10 12:55:45 -0700386 int nid;
Gary Hadec04fc582009-01-06 14:39:14 -0800387
388 nid = get_nid_for_pfn(pfn);
389 if (nid < 0)
390 continue;
391 if (!node_online(nid))
392 continue;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800393 if (node_test_and_set(nid, *unlinked_nodes))
Gary Hadec04fc582009-01-06 14:39:14 -0800394 continue;
395 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
396 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800397 sysfs_remove_link(&mem_blk->sysdev.kobj,
398 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800399 }
David Rientjes9ae49fa2009-12-14 17:59:46 -0800400 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800401 return 0;
402}
403
404static int link_mem_sections(int nid)
405{
406 unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
407 unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
408 unsigned long pfn;
409 int err = 0;
410
411 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
412 unsigned long section_nr = pfn_to_section_nr(pfn);
413 struct mem_section *mem_sect;
414 struct memory_block *mem_blk;
415 int ret;
416
417 if (!present_section_nr(section_nr))
418 continue;
419 mem_sect = __nr_to_section(section_nr);
420 mem_blk = find_memory_block(mem_sect);
421 ret = register_mem_sect_under_node(mem_blk, nid);
422 if (!err)
423 err = ret;
424
425 /* discard ref obtained in find_memory_block() */
426 kobject_put(&mem_blk->sysdev.kobj);
427 }
428 return err;
429}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800430
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800431#ifdef CONFIG_HUGETLBFS
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800432/*
433 * Handle per node hstate attribute [un]registration on transistions
434 * to/from memoryless state.
435 */
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800436static void node_hugetlb_work(struct work_struct *work)
437{
438 struct node *node = container_of(work, struct node, node_work);
439
440 /*
441 * We only get here when a node transitions to/from memoryless state.
442 * We can detect which transition occurred by examining whether the
443 * node has memory now. hugetlb_register_node() already check this
444 * so we try to register the attributes. If that fails, then the
445 * node has transitioned to memoryless, try to unregister the
446 * attributes.
447 */
448 if (!hugetlb_register_node(node))
449 hugetlb_unregister_node(node);
450}
451
452static void init_node_hugetlb_work(int nid)
453{
454 INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work);
455}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800456
457static int node_memory_callback(struct notifier_block *self,
458 unsigned long action, void *arg)
459{
460 struct memory_notify *mnb = arg;
461 int nid = mnb->status_change_nid;
462
463 switch (action) {
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800464 case MEM_ONLINE:
465 case MEM_OFFLINE:
466 /*
467 * offload per node hstate [un]registration to a work thread
468 * when transitioning to/from memoryless state.
469 */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800470 if (nid != NUMA_NO_NODE)
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800471 schedule_work(&node_devices[nid].node_work);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800472 break;
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800473
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800474 case MEM_GOING_ONLINE:
475 case MEM_GOING_OFFLINE:
476 case MEM_CANCEL_ONLINE:
477 case MEM_CANCEL_OFFLINE:
478 default:
479 break;
480 }
481
482 return NOTIFY_OK;
483}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800484#endif /* CONFIG_HUGETLBFS */
485#else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800486
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800487static int link_mem_sections(int nid) { return 0; }
488#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
489
490#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
491 !defined(CONFIG_HUGETLBFS)
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800492static inline int node_memory_callback(struct notifier_block *self,
493 unsigned long action, void *arg)
494{
495 return NOTIFY_OK;
496}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800497
498static void init_node_hugetlb_work(int nid) { }
499
500#endif
Gary Hadec04fc582009-01-06 14:39:14 -0800501
Yasunori Goto0fc44152006-06-27 02:53:38 -0700502int register_one_node(int nid)
503{
504 int error = 0;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700505 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700506
507 if (node_online(nid)) {
508 int p_node = parent_node(nid);
509 struct node *parent = NULL;
510
511 if (p_node != nid)
512 parent = &node_devices[p_node];
513
514 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700515
516 /* link cpu under this node */
517 for_each_present_cpu(cpu) {
518 if (cpu_to_node(cpu) == nid)
519 register_cpu_under_node(cpu, nid);
520 }
Gary Hadec04fc582009-01-06 14:39:14 -0800521
522 /* link memory sections under this node */
523 error = link_mem_sections(nid);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800524
525 /* initialize work queue for memory hot plug */
526 init_node_hugetlb_work(nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -0700527 }
528
529 return error;
530
531}
532
533void unregister_one_node(int nid)
534{
535 unregister_node(&node_devices[nid]);
536}
537
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700538/*
539 * node states attributes
540 */
541
542static ssize_t print_nodes_state(enum node_states state, char *buf)
543{
544 int n;
545
546 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
547 if (n > 0 && PAGE_SIZE > n + 1) {
548 *(buf + n++) = '\n';
549 *(buf + n++) = '\0';
550 }
551 return n;
552}
553
Andi Kleenb15f5622010-01-05 12:47:59 +0100554struct node_attr {
555 struct sysdev_class_attribute attr;
556 enum node_states state;
557};
558
559static ssize_t show_node_state(struct sysdev_class *class,
560 struct sysdev_class_attribute *attr, char *buf)
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700561{
Andi Kleenb15f5622010-01-05 12:47:59 +0100562 struct node_attr *na = container_of(attr, struct node_attr, attr);
563 return print_nodes_state(na->state, buf);
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700564}
565
Andi Kleenb15f5622010-01-05 12:47:59 +0100566#define _NODE_ATTR(name, state) \
567 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700568
Andi Kleenb15f5622010-01-05 12:47:59 +0100569static struct node_attr node_state_attr[] = {
570 _NODE_ATTR(possible, N_POSSIBLE),
571 _NODE_ATTR(online, N_ONLINE),
572 _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
573 _NODE_ATTR(has_cpu, N_CPU),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700574#ifdef CONFIG_HIGHMEM
Andi Kleenb15f5622010-01-05 12:47:59 +0100575 _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700576#endif
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700577};
578
Andi Kleen3701cde2010-01-05 12:48:04 +0100579static struct sysdev_class_attribute *node_state_attrs[] = {
580 &node_state_attr[0].attr,
581 &node_state_attr[1].attr,
582 &node_state_attr[2].attr,
583 &node_state_attr[3].attr,
584#ifdef CONFIG_HIGHMEM
585 &node_state_attr[4].attr,
586#endif
587 NULL
588};
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700589
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800590#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900591static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700593 int ret;
594
Andi Kleen3701cde2010-01-05 12:48:04 +0100595 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
596 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
597
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700598 ret = sysdev_class_register(&node_class);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800599 if (!ret) {
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800600 hotplug_memory_notifier(node_memory_callback,
601 NODE_CALLBACK_PRI);
602 }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700603
604 /*
605 * Note: we're not going to unregister the node class if we fail
606 * to register the node state class attribute files.
607 */
608 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610postcore_initcall(register_node_type);