blob: 36b43052001d96da6ec747d349c7e64a0645f9ec [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>
Mel Gormaned4a6d72010-05-24 14:32:29 -070012#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/cpumask.h>
14#include <linux/topology.h>
15#include <linux/nodemask.h>
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070016#include <linux/cpu.h>
Lee Schermerhornbde631a2007-10-16 01:26:27 -070017#include <linux/device.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070018#include <linux/swap.h>
Tejun Heo18e5b532010-04-06 19:23:33 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Andi Kleen3701cde2010-01-05 12:48:04 +010021static struct sysdev_class_attribute *node_state_attrs[];
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static struct sysdev_class node_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010024 .name = "node",
Andi Kleen3701cde2010-01-05 12:48:04 +010025 .attrs = node_state_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28
Mike Travis39106dc2008-04-08 11:43:03 -070029static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct node *node_dev = to_node(dev);
Rusty Russella70f7302009-03-13 14:49:46 +103032 const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int len;
34
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
Mike Travis39106dc2008-04-08 11:43:03 -070038 len = type?
Rusty Russell29c01772008-12-13 21:20:25 +103039 cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
40 cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travisc5f59f02008-04-04 18:11:10 -070041 buf[len++] = '\n';
42 buf[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return len;
44}
45
Andi Kleen4a0b2b42008-07-01 18:48:41 +020046static inline ssize_t node_read_cpumask(struct sys_device *dev,
47 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070048{
49 return node_read_cpumap(dev, 0, buf);
50}
Andi Kleen4a0b2b42008-07-01 18:48:41 +020051static inline ssize_t node_read_cpulist(struct sys_device *dev,
52 struct sysdev_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070053{
54 return node_read_cpumap(dev, 1, buf);
55}
56
57static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
58static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define K(x) ((x) << (PAGE_SHIFT - 10))
Andi Kleen4a0b2b42008-07-01 18:48:41 +020061static ssize_t node_read_meminfo(struct sys_device * dev,
62 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 int n;
65 int nid = dev->id;
66 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 si_meminfo_node(&i, nid);
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -070069 n = sprintf(buf,
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"
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -070080 "Node %d Mlocked: %8lu kB\n",
81 nid, K(i.totalram),
82 nid, K(i.freeram),
83 nid, K(i.totalram - i.freeram),
84 nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
85 node_page_state(nid, NR_ACTIVE_FILE)),
86 nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
87 node_page_state(nid, NR_INACTIVE_FILE)),
88 nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
89 nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
90 nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
91 nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
92 nid, K(node_page_state(nid, NR_UNEVICTABLE)),
93 nid, K(node_page_state(nid, NR_MLOCK)));
94
Christoph Lameter182e8e22006-09-25 23:31:10 -070095#ifdef CONFIG_HIGHMEM
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -070096 n += sprintf(buf + n,
Rik van Riel4f98a2f2008-10-18 20:26:32 -070097 "Node %d HighTotal: %8lu kB\n"
98 "Node %d HighFree: %8lu kB\n"
99 "Node %d LowTotal: %8lu kB\n"
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700100 "Node %d LowFree: %8lu kB\n",
101 nid, K(i.totalhigh),
102 nid, K(i.freehigh),
103 nid, K(i.totalram - i.totalhigh),
104 nid, K(i.freeram - i.freehigh));
Christoph Lameter182e8e22006-09-25 23:31:10 -0700105#endif
KOSAKI Motohiro7ee92252010-08-09 17:19:50 -0700106 n += sprintf(buf + n,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700107 "Node %d Dirty: %8lu kB\n"
108 "Node %d Writeback: %8lu kB\n"
109 "Node %d FilePages: %8lu kB\n"
110 "Node %d Mapped: %8lu kB\n"
111 "Node %d AnonPages: %8lu kB\n"
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700112 "Node %d Shmem: %8lu kB\n"
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700113 "Node %d KernelStack: %8lu kB\n"
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700114 "Node %d PageTables: %8lu kB\n"
115 "Node %d NFS_Unstable: %8lu kB\n"
116 "Node %d Bounce: %8lu kB\n"
117 "Node %d WritebackTmp: %8lu kB\n"
118 "Node %d Slab: %8lu kB\n"
119 "Node %d SReclaimable: %8lu kB\n"
David Rientjes05b258e2011-01-13 15:47:14 -0800120 "Node %d SUnreclaim: %8lu kB\n"
121#ifdef CONFIG_TRANSPARENT_HUGEPAGE
122 "Node %d AnonHugePages: %8lu kB\n"
123#endif
124 ,
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700125 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700126 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -0700127 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700128 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
David Rientjes05b258e2011-01-13 15:47:14 -0800129 nid, K(node_page_state(nid, NR_ANON_PAGES)
130#ifdef CONFIG_TRANSPARENT_HUGEPAGE
131 + node_page_state(nid, NR_ANON_TRANSPARENT_HUGEPAGES) *
132 HPAGE_PMD_NR
133#endif
134 ),
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700135 nid, K(node_page_state(nid, NR_SHMEM)),
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700136 nid, node_page_state(nid, NR_KERNEL_STACK) *
137 THREAD_SIZE / 1024,
Christoph Lameterdf849a12006-06-30 01:55:38 -0700138 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700139 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700140 nid, K(node_page_state(nid, NR_BOUNCE)),
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700141 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700142 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
143 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
144 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
David Rientjes05b258e2011-01-13 15:47:14 -0800145 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE))
146#ifdef CONFIG_TRANSPARENT_HUGEPAGE
147 , nid,
148 K(node_page_state(nid, NR_ANON_TRANSPARENT_HUGEPAGES) *
149 HPAGE_PMD_NR)
150#endif
151 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 n += hugetlb_report_node_meminfo(nid, buf + n);
153 return n;
154}
155
156#undef K
157static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
158
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200159static ssize_t node_read_numastat(struct sys_device * dev,
160 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return sprintf(buf,
163 "numa_hit %lu\n"
164 "numa_miss %lu\n"
165 "numa_foreign %lu\n"
166 "interleave_hit %lu\n"
167 "local_node %lu\n"
168 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700169 node_page_state(dev->id, NUMA_HIT),
170 node_page_state(dev->id, NUMA_MISS),
171 node_page_state(dev->id, NUMA_FOREIGN),
172 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
173 node_page_state(dev->id, NUMA_LOCAL),
174 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
177
Michael Rubin2ac39032010-10-26 14:21:35 -0700178static ssize_t node_read_vmstat(struct sys_device *dev,
179 struct sysdev_attribute *attr, char *buf)
180{
181 int nid = dev->id;
182 return sprintf(buf,
183 "nr_written %lu\n"
184 "nr_dirtied %lu\n",
185 node_page_state(nid, NR_WRITTEN),
186 node_page_state(nid, NR_DIRTIED));
187}
188static SYSDEV_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL);
189
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200190static ssize_t node_read_distance(struct sys_device * dev,
191 struct sysdev_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int nid = dev->id;
194 int len = 0;
195 int i;
196
David Rientjes12ee3c02010-03-10 14:50:21 -0800197 /*
198 * buf is currently PAGE_SIZE in length and each node needs 4 chars
199 * at the most (distance + space or newline).
200 */
201 BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 for_each_online_node(i)
204 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
205
206 len += sprintf(buf + len, "\n");
207 return len;
208}
209static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
210
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800211#ifdef CONFIG_HUGETLBFS
212/*
213 * hugetlbfs per node attributes registration interface:
214 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800215 * it will register its per node attributes for all online nodes with
216 * memory. It will also call register_hugetlbfs_with_node(), below, to
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800217 * register its attribute registration functions with this node driver.
218 * Once these hooks have been initialized, the node driver will call into
219 * the hugetlb module to [un]register attributes for hot-plugged nodes.
220 */
221static node_registration_func_t __hugetlb_register_node;
222static node_registration_func_t __hugetlb_unregister_node;
223
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800224static inline bool hugetlb_register_node(struct node *node)
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800225{
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800226 if (__hugetlb_register_node &&
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800227 node_state(node->sysdev.id, N_HIGH_MEMORY)) {
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800228 __hugetlb_register_node(node);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800229 return true;
230 }
231 return false;
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800232}
233
234static inline void hugetlb_unregister_node(struct node *node)
235{
236 if (__hugetlb_unregister_node)
237 __hugetlb_unregister_node(node);
238}
239
240void register_hugetlbfs_with_node(node_registration_func_t doregister,
241 node_registration_func_t unregister)
242{
243 __hugetlb_register_node = doregister;
244 __hugetlb_unregister_node = unregister;
245}
246#else
247static inline void hugetlb_register_node(struct node *node) {}
248
249static inline void hugetlb_unregister_node(struct node *node) {}
250#endif
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100254 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * @num - Node number to use when creating the device.
256 *
257 * Initialize and register the node device.
258 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900259int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int error;
262
263 node->sysdev.id = num;
264 node->sysdev.cls = &node_class;
265 error = sysdev_register(&node->sysdev);
266
267 if (!error){
268 sysdev_create_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700269 sysdev_create_file(&node->sysdev, &attr_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 sysdev_create_file(&node->sysdev, &attr_meminfo);
271 sysdev_create_file(&node->sysdev, &attr_numastat);
272 sysdev_create_file(&node->sysdev, &attr_distance);
Michael Rubin2ac39032010-10-26 14:21:35 -0700273 sysdev_create_file(&node->sysdev, &attr_vmstat);
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700274
275 scan_unevictable_register_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800276
Lee Schermerhorn9a3052302009-12-14 17:58:25 -0800277 hugetlb_register_node(node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700278
279 compaction_register_node(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 return error;
282}
283
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900284/**
285 * unregister_node - unregister a node device
286 * @node: node going away
287 *
288 * Unregisters a node device @node. All the devices on the node must be
289 * unregistered before calling this function.
290 */
291void unregister_node(struct node *node)
292{
293 sysdev_remove_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700294 sysdev_remove_file(&node->sysdev, &attr_cpulist);
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900295 sysdev_remove_file(&node->sysdev, &attr_meminfo);
296 sysdev_remove_file(&node->sysdev, &attr_numastat);
297 sysdev_remove_file(&node->sysdev, &attr_distance);
Michael Rubin2ac39032010-10-26 14:21:35 -0700298 sysdev_remove_file(&node->sysdev, &attr_vmstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700300 scan_unevictable_unregister_node(node);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800301 hugetlb_unregister_node(node); /* no-op, if memoryless node */
Lee Schermerhornaf936a12008-10-18 20:26:53 -0700302
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900303 sysdev_unregister(&node->sysdev);
304}
305
Yasunori Goto0fc44152006-06-27 02:53:38 -0700306struct node node_devices[MAX_NUMNODES];
307
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700308/*
309 * register cpu under node
310 */
311int register_cpu_under_node(unsigned int cpu, unsigned int nid)
312{
Alex Chiang18307942009-12-14 17:59:08 -0800313 int ret;
Alex Chiangf8246f32009-12-14 17:59:06 -0800314 struct sys_device *obj;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700315
Alex Chiangf8246f32009-12-14 17:59:06 -0800316 if (!node_online(nid))
317 return 0;
318
319 obj = get_cpu_sysdev(cpu);
320 if (!obj)
321 return 0;
322
Alex Chiang18307942009-12-14 17:59:08 -0800323 ret = sysfs_create_link(&node_devices[nid].sysdev.kobj,
Alex Chiangf8246f32009-12-14 17:59:06 -0800324 &obj->kobj,
325 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800326 if (ret)
327 return ret;
328
329 return sysfs_create_link(&obj->kobj,
330 &node_devices[nid].sysdev.kobj,
331 kobject_name(&node_devices[nid].sysdev.kobj));
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700332}
333
334int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
335{
Alex Chiangb9d52da2009-12-14 17:59:07 -0800336 struct sys_device *obj;
337
338 if (!node_online(nid))
339 return 0;
340
341 obj = get_cpu_sysdev(cpu);
342 if (!obj)
343 return 0;
344
345 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
346 kobject_name(&obj->kobj));
Alex Chiang18307942009-12-14 17:59:08 -0800347 sysfs_remove_link(&obj->kobj,
348 kobject_name(&node_devices[nid].sysdev.kobj));
Alex Chiangb9d52da2009-12-14 17:59:07 -0800349
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700350 return 0;
351}
352
Gary Hadec04fc582009-01-06 14:39:14 -0800353#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
354#define page_initialized(page) (page->lru.next)
355
356static int get_nid_for_pfn(unsigned long pfn)
357{
358 struct page *page;
359
360 if (!pfn_valid_within(pfn))
361 return -1;
362 page = pfn_to_page(pfn);
363 if (!page_initialized(page))
364 return -1;
365 return pfn_to_nid(pfn);
366}
367
368/* register memory section under specified node if it spans that node */
369int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
370{
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800371 int ret;
Gary Hadec04fc582009-01-06 14:39:14 -0800372 unsigned long pfn, sect_start_pfn, sect_end_pfn;
373
374 if (!mem_blk)
375 return -EFAULT;
376 if (!node_online(nid))
377 return 0;
378 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
379 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
380 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
381 int page_nid;
382
383 page_nid = get_nid_for_pfn(pfn);
384 if (page_nid < 0)
385 continue;
386 if (page_nid != nid)
387 continue;
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800388 ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
Gary Hadec04fc582009-01-06 14:39:14 -0800389 &mem_blk->sysdev.kobj,
390 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800391 if (ret)
392 return ret;
393
394 return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj,
395 &node_devices[nid].sysdev.kobj,
396 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800397 }
398 /* mem section does not span the specified node */
399 return 0;
400}
401
402/* unregister memory section under all nodes that it spans */
403int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
404{
David Rientjes9ae49fa2009-12-14 17:59:46 -0800405 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
Gary Hadec04fc582009-01-06 14:39:14 -0800406 unsigned long pfn, sect_start_pfn, sect_end_pfn;
407
David Rientjes9ae49fa2009-12-14 17:59:46 -0800408 if (!mem_blk) {
409 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800410 return -EFAULT;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800411 }
412 if (!unlinked_nodes)
413 return -ENOMEM;
414 nodes_clear(*unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800415 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
416 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
417 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
Roel Kluin47504982009-03-10 12:55:45 -0700418 int nid;
Gary Hadec04fc582009-01-06 14:39:14 -0800419
420 nid = get_nid_for_pfn(pfn);
421 if (nid < 0)
422 continue;
423 if (!node_online(nid))
424 continue;
David Rientjes9ae49fa2009-12-14 17:59:46 -0800425 if (node_test_and_set(nid, *unlinked_nodes))
Gary Hadec04fc582009-01-06 14:39:14 -0800426 continue;
427 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
428 kobject_name(&mem_blk->sysdev.kobj));
Alex Chiangdee5d0d2009-12-14 17:59:05 -0800429 sysfs_remove_link(&mem_blk->sysdev.kobj,
430 kobject_name(&node_devices[nid].sysdev.kobj));
Gary Hadec04fc582009-01-06 14:39:14 -0800431 }
David Rientjes9ae49fa2009-12-14 17:59:46 -0800432 NODEMASK_FREE(unlinked_nodes);
Gary Hadec04fc582009-01-06 14:39:14 -0800433 return 0;
434}
435
436static int link_mem_sections(int nid)
437{
438 unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
439 unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
440 unsigned long pfn;
Robin Holt63d027a2010-09-29 14:00:56 -0500441 struct memory_block *mem_blk = NULL;
Gary Hadec04fc582009-01-06 14:39:14 -0800442 int err = 0;
443
444 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
445 unsigned long section_nr = pfn_to_section_nr(pfn);
446 struct mem_section *mem_sect;
Gary Hadec04fc582009-01-06 14:39:14 -0800447 int ret;
448
449 if (!present_section_nr(section_nr))
450 continue;
451 mem_sect = __nr_to_section(section_nr);
Robin Holt63d027a2010-09-29 14:00:56 -0500452 mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
Gary Hadec04fc582009-01-06 14:39:14 -0800453 ret = register_mem_sect_under_node(mem_blk, nid);
454 if (!err)
455 err = ret;
456
457 /* discard ref obtained in find_memory_block() */
Gary Hadec04fc582009-01-06 14:39:14 -0800458 }
Robin Holt63d027a2010-09-29 14:00:56 -0500459
460 if (mem_blk)
461 kobject_put(&mem_blk->sysdev.kobj);
Gary Hadec04fc582009-01-06 14:39:14 -0800462 return err;
463}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800464
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800465#ifdef CONFIG_HUGETLBFS
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800466/*
467 * Handle per node hstate attribute [un]registration on transistions
468 * to/from memoryless state.
469 */
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800470static void node_hugetlb_work(struct work_struct *work)
471{
472 struct node *node = container_of(work, struct node, node_work);
473
474 /*
475 * We only get here when a node transitions to/from memoryless state.
476 * We can detect which transition occurred by examining whether the
477 * node has memory now. hugetlb_register_node() already check this
478 * so we try to register the attributes. If that fails, then the
479 * node has transitioned to memoryless, try to unregister the
480 * attributes.
481 */
482 if (!hugetlb_register_node(node))
483 hugetlb_unregister_node(node);
484}
485
486static void init_node_hugetlb_work(int nid)
487{
488 INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work);
489}
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800490
491static int node_memory_callback(struct notifier_block *self,
492 unsigned long action, void *arg)
493{
494 struct memory_notify *mnb = arg;
495 int nid = mnb->status_change_nid;
496
497 switch (action) {
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800498 case MEM_ONLINE:
499 case MEM_OFFLINE:
500 /*
501 * offload per node hstate [un]registration to a work thread
502 * when transitioning to/from memoryless state.
503 */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800504 if (nid != NUMA_NO_NODE)
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800505 schedule_work(&node_devices[nid].node_work);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800506 break;
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800507
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800508 case MEM_GOING_ONLINE:
509 case MEM_GOING_OFFLINE:
510 case MEM_CANCEL_ONLINE:
511 case MEM_CANCEL_OFFLINE:
512 default:
513 break;
514 }
515
516 return NOTIFY_OK;
517}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800518#endif /* CONFIG_HUGETLBFS */
519#else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800520
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800521static int link_mem_sections(int nid) { return 0; }
522#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
523
524#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
525 !defined(CONFIG_HUGETLBFS)
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800526static inline int node_memory_callback(struct notifier_block *self,
527 unsigned long action, void *arg)
528{
529 return NOTIFY_OK;
530}
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800531
532static void init_node_hugetlb_work(int nid) { }
533
534#endif
Gary Hadec04fc582009-01-06 14:39:14 -0800535
Yasunori Goto0fc44152006-06-27 02:53:38 -0700536int register_one_node(int nid)
537{
538 int error = 0;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700539 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700540
541 if (node_online(nid)) {
542 int p_node = parent_node(nid);
543 struct node *parent = NULL;
544
545 if (p_node != nid)
546 parent = &node_devices[p_node];
547
548 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700549
550 /* link cpu under this node */
551 for_each_present_cpu(cpu) {
552 if (cpu_to_node(cpu) == nid)
553 register_cpu_under_node(cpu, nid);
554 }
Gary Hadec04fc582009-01-06 14:39:14 -0800555
556 /* link memory sections under this node */
557 error = link_mem_sections(nid);
Lee Schermerhorn39da08c2009-12-14 17:58:36 -0800558
559 /* initialize work queue for memory hot plug */
560 init_node_hugetlb_work(nid);
Yasunori Goto0fc44152006-06-27 02:53:38 -0700561 }
562
563 return error;
564
565}
566
567void unregister_one_node(int nid)
568{
569 unregister_node(&node_devices[nid]);
570}
571
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700572/*
573 * node states attributes
574 */
575
576static ssize_t print_nodes_state(enum node_states state, char *buf)
577{
578 int n;
579
580 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
581 if (n > 0 && PAGE_SIZE > n + 1) {
582 *(buf + n++) = '\n';
583 *(buf + n++) = '\0';
584 }
585 return n;
586}
587
Andi Kleenb15f5622010-01-05 12:47:59 +0100588struct node_attr {
589 struct sysdev_class_attribute attr;
590 enum node_states state;
591};
592
593static ssize_t show_node_state(struct sysdev_class *class,
594 struct sysdev_class_attribute *attr, char *buf)
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700595{
Andi Kleenb15f5622010-01-05 12:47:59 +0100596 struct node_attr *na = container_of(attr, struct node_attr, attr);
597 return print_nodes_state(na->state, buf);
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700598}
599
Andi Kleenb15f5622010-01-05 12:47:59 +0100600#define _NODE_ATTR(name, state) \
601 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700602
Andi Kleenb15f5622010-01-05 12:47:59 +0100603static struct node_attr node_state_attr[] = {
604 _NODE_ATTR(possible, N_POSSIBLE),
605 _NODE_ATTR(online, N_ONLINE),
606 _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
607 _NODE_ATTR(has_cpu, N_CPU),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700608#ifdef CONFIG_HIGHMEM
Andi Kleenb15f5622010-01-05 12:47:59 +0100609 _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700610#endif
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700611};
612
Andi Kleen3701cde2010-01-05 12:48:04 +0100613static struct sysdev_class_attribute *node_state_attrs[] = {
614 &node_state_attr[0].attr,
615 &node_state_attr[1].attr,
616 &node_state_attr[2].attr,
617 &node_state_attr[3].attr,
618#ifdef CONFIG_HIGHMEM
619 &node_state_attr[4].attr,
620#endif
621 NULL
622};
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700623
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800624#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900625static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700627 int ret;
628
Andi Kleen3701cde2010-01-05 12:48:04 +0100629 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
630 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
631
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700632 ret = sysdev_class_register(&node_class);
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800633 if (!ret) {
Lee Schermerhorn4faf8d92009-12-14 17:58:35 -0800634 hotplug_memory_notifier(node_memory_callback,
635 NODE_CALLBACK_PRI);
636 }
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700637
638 /*
639 * Note: we're not going to unregister the node class if we fail
640 * to register the node state class attribute files.
641 */
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644postcore_initcall(register_node_type);