93b3ac65c2d4f0014628cca66decec0df45c45cf
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / node.c
1 /*
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>
9 #include <linux/memory.h>
10 #include <linux/node.h>
11 #include <linux/hugetlb.h>
12 #include <linux/cpumask.h>
13 #include <linux/topology.h>
14 #include <linux/nodemask.h>
15 #include <linux/cpu.h>
16 #include <linux/device.h>
17 #include <linux/swap.h>
18
19 static struct sysdev_class_attribute *node_state_attrs[];
20
21 static struct sysdev_class node_class = {
22 .name = "node",
23 .attrs = node_state_attrs,
24 };
25
26
27 static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
28 {
29 struct node *node_dev = to_node(dev);
30 const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id);
31 int len;
32
33 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
34 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
35
36 len = type?
37 cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
38 cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
39 buf[len++] = '\n';
40 buf[len] = '\0';
41 return len;
42 }
43
44 static inline ssize_t node_read_cpumask(struct sys_device *dev,
45 struct sysdev_attribute *attr, char *buf)
46 {
47 return node_read_cpumap(dev, 0, buf);
48 }
49 static inline ssize_t node_read_cpulist(struct sys_device *dev,
50 struct sysdev_attribute *attr, char *buf)
51 {
52 return node_read_cpumap(dev, 1, buf);
53 }
54
55 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
56 static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
57
58 #define K(x) ((x) << (PAGE_SHIFT - 10))
59 static ssize_t node_read_meminfo(struct sys_device * dev,
60 struct sysdev_attribute *attr, char * buf)
61 {
62 int n;
63 int nid = dev->id;
64 struct sysinfo i;
65
66 si_meminfo_node(&i, nid);
67
68 n = sprintf(buf, "\n"
69 "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"
78 "Node %d Unevictable: %8lu kB\n"
79 "Node %d Mlocked: %8lu kB\n"
80 #ifdef CONFIG_HIGHMEM
81 "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"
85 #endif
86 "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"
91 "Node %d Shmem: %8lu kB\n"
92 "Node %d KernelStack: %8lu kB\n"
93 "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",
100 nid, K(i.totalram),
101 nid, K(i.freeram),
102 nid, K(i.totalram - i.freeram),
103 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)),
111 nid, K(node_page_state(nid, NR_UNEVICTABLE)),
112 nid, K(node_page_state(nid, NR_MLOCK)),
113 #ifdef CONFIG_HIGHMEM
114 nid, K(i.totalhigh),
115 nid, K(i.freehigh),
116 nid, K(i.totalram - i.totalhigh),
117 nid, K(i.freeram - i.freehigh),
118 #endif
119 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
120 nid, K(node_page_state(nid, NR_WRITEBACK)),
121 nid, K(node_page_state(nid, NR_FILE_PAGES)),
122 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
123 nid, K(node_page_state(nid, NR_ANON_PAGES)),
124 nid, K(node_page_state(nid, NR_SHMEM)),
125 nid, node_page_state(nid, NR_KERNEL_STACK) *
126 THREAD_SIZE / 1024,
127 nid, K(node_page_state(nid, NR_PAGETABLE)),
128 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
129 nid, K(node_page_state(nid, NR_BOUNCE)),
130 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
131 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)));
135 n += hugetlb_report_node_meminfo(nid, buf + n);
136 return n;
137 }
138
139 #undef K
140 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
141
142 static ssize_t node_read_numastat(struct sys_device * dev,
143 struct sysdev_attribute *attr, char * buf)
144 {
145 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",
152 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));
158 }
159 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
160
161 static ssize_t node_read_distance(struct sys_device * dev,
162 struct sysdev_attribute *attr, char * buf)
163 {
164 int nid = dev->id;
165 int len = 0;
166 int i;
167
168 /*
169 * buf is currently PAGE_SIZE in length and each node needs 4 chars
170 * at the most (distance + space or newline).
171 */
172 BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
173
174 for_each_online_node(i)
175 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
176
177 len += sprintf(buf + len, "\n");
178 return len;
179 }
180 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
181
182 #ifdef CONFIG_HUGETLBFS
183 /*
184 * hugetlbfs per node attributes registration interface:
185 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
186 * it will register its per node attributes for all online nodes with
187 * memory. It will also call register_hugetlbfs_with_node(), below, to
188 * register its attribute registration functions with this node driver.
189 * Once these hooks have been initialized, the node driver will call into
190 * the hugetlb module to [un]register attributes for hot-plugged nodes.
191 */
192 static node_registration_func_t __hugetlb_register_node;
193 static node_registration_func_t __hugetlb_unregister_node;
194
195 static inline bool hugetlb_register_node(struct node *node)
196 {
197 if (__hugetlb_register_node &&
198 node_state(node->sysdev.id, N_HIGH_MEMORY)) {
199 __hugetlb_register_node(node);
200 return true;
201 }
202 return false;
203 }
204
205 static inline void hugetlb_unregister_node(struct node *node)
206 {
207 if (__hugetlb_unregister_node)
208 __hugetlb_unregister_node(node);
209 }
210
211 void register_hugetlbfs_with_node(node_registration_func_t doregister,
212 node_registration_func_t unregister)
213 {
214 __hugetlb_register_node = doregister;
215 __hugetlb_unregister_node = unregister;
216 }
217 #else
218 static inline void hugetlb_register_node(struct node *node) {}
219
220 static inline void hugetlb_unregister_node(struct node *node) {}
221 #endif
222
223
224 /*
225 * register_node - Setup a sysfs device for a node.
226 * @num - Node number to use when creating the device.
227 *
228 * Initialize and register the node device.
229 */
230 int register_node(struct node *node, int num, struct node *parent)
231 {
232 int error;
233
234 node->sysdev.id = num;
235 node->sysdev.cls = &node_class;
236 error = sysdev_register(&node->sysdev);
237
238 if (!error){
239 sysdev_create_file(&node->sysdev, &attr_cpumap);
240 sysdev_create_file(&node->sysdev, &attr_cpulist);
241 sysdev_create_file(&node->sysdev, &attr_meminfo);
242 sysdev_create_file(&node->sysdev, &attr_numastat);
243 sysdev_create_file(&node->sysdev, &attr_distance);
244
245 scan_unevictable_register_node(node);
246
247 hugetlb_register_node(node);
248 }
249 return error;
250 }
251
252 /**
253 * unregister_node - unregister a node device
254 * @node: node going away
255 *
256 * Unregisters a node device @node. All the devices on the node must be
257 * unregistered before calling this function.
258 */
259 void unregister_node(struct node *node)
260 {
261 sysdev_remove_file(&node->sysdev, &attr_cpumap);
262 sysdev_remove_file(&node->sysdev, &attr_cpulist);
263 sysdev_remove_file(&node->sysdev, &attr_meminfo);
264 sysdev_remove_file(&node->sysdev, &attr_numastat);
265 sysdev_remove_file(&node->sysdev, &attr_distance);
266
267 scan_unevictable_unregister_node(node);
268 hugetlb_unregister_node(node); /* no-op, if memoryless node */
269
270 sysdev_unregister(&node->sysdev);
271 }
272
273 struct node node_devices[MAX_NUMNODES];
274
275 /*
276 * register cpu under node
277 */
278 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
279 {
280 int ret;
281 struct sys_device *obj;
282
283 if (!node_online(nid))
284 return 0;
285
286 obj = get_cpu_sysdev(cpu);
287 if (!obj)
288 return 0;
289
290 ret = sysfs_create_link(&node_devices[nid].sysdev.kobj,
291 &obj->kobj,
292 kobject_name(&obj->kobj));
293 if (ret)
294 return ret;
295
296 return sysfs_create_link(&obj->kobj,
297 &node_devices[nid].sysdev.kobj,
298 kobject_name(&node_devices[nid].sysdev.kobj));
299 }
300
301 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
302 {
303 struct sys_device *obj;
304
305 if (!node_online(nid))
306 return 0;
307
308 obj = get_cpu_sysdev(cpu);
309 if (!obj)
310 return 0;
311
312 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
313 kobject_name(&obj->kobj));
314 sysfs_remove_link(&obj->kobj,
315 kobject_name(&node_devices[nid].sysdev.kobj));
316
317 return 0;
318 }
319
320 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
321 #define page_initialized(page) (page->lru.next)
322
323 static int get_nid_for_pfn(unsigned long pfn)
324 {
325 struct page *page;
326
327 if (!pfn_valid_within(pfn))
328 return -1;
329 page = pfn_to_page(pfn);
330 if (!page_initialized(page))
331 return -1;
332 return pfn_to_nid(pfn);
333 }
334
335 /* register memory section under specified node if it spans that node */
336 int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
337 {
338 int ret;
339 unsigned long pfn, sect_start_pfn, sect_end_pfn;
340
341 if (!mem_blk)
342 return -EFAULT;
343 if (!node_online(nid))
344 return 0;
345 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
346 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
347 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
348 int page_nid;
349
350 page_nid = get_nid_for_pfn(pfn);
351 if (page_nid < 0)
352 continue;
353 if (page_nid != nid)
354 continue;
355 ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
356 &mem_blk->sysdev.kobj,
357 kobject_name(&mem_blk->sysdev.kobj));
358 if (ret)
359 return ret;
360
361 return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj,
362 &node_devices[nid].sysdev.kobj,
363 kobject_name(&node_devices[nid].sysdev.kobj));
364 }
365 /* mem section does not span the specified node */
366 return 0;
367 }
368
369 /* unregister memory section under all nodes that it spans */
370 int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
371 {
372 NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
373 unsigned long pfn, sect_start_pfn, sect_end_pfn;
374
375 if (!mem_blk) {
376 NODEMASK_FREE(unlinked_nodes);
377 return -EFAULT;
378 }
379 if (!unlinked_nodes)
380 return -ENOMEM;
381 nodes_clear(*unlinked_nodes);
382 sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
383 sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
384 for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
385 int nid;
386
387 nid = get_nid_for_pfn(pfn);
388 if (nid < 0)
389 continue;
390 if (!node_online(nid))
391 continue;
392 if (node_test_and_set(nid, *unlinked_nodes))
393 continue;
394 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
395 kobject_name(&mem_blk->sysdev.kobj));
396 sysfs_remove_link(&mem_blk->sysdev.kobj,
397 kobject_name(&node_devices[nid].sysdev.kobj));
398 }
399 NODEMASK_FREE(unlinked_nodes);
400 return 0;
401 }
402
403 static int link_mem_sections(int nid)
404 {
405 unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
406 unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
407 unsigned long pfn;
408 int err = 0;
409
410 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
411 unsigned long section_nr = pfn_to_section_nr(pfn);
412 struct mem_section *mem_sect;
413 struct memory_block *mem_blk;
414 int ret;
415
416 if (!present_section_nr(section_nr))
417 continue;
418 mem_sect = __nr_to_section(section_nr);
419 mem_blk = find_memory_block(mem_sect);
420 ret = register_mem_sect_under_node(mem_blk, nid);
421 if (!err)
422 err = ret;
423
424 /* discard ref obtained in find_memory_block() */
425 kobject_put(&mem_blk->sysdev.kobj);
426 }
427 return err;
428 }
429
430 #ifdef CONFIG_HUGETLBFS
431 /*
432 * Handle per node hstate attribute [un]registration on transistions
433 * to/from memoryless state.
434 */
435 static void node_hugetlb_work(struct work_struct *work)
436 {
437 struct node *node = container_of(work, struct node, node_work);
438
439 /*
440 * We only get here when a node transitions to/from memoryless state.
441 * We can detect which transition occurred by examining whether the
442 * node has memory now. hugetlb_register_node() already check this
443 * so we try to register the attributes. If that fails, then the
444 * node has transitioned to memoryless, try to unregister the
445 * attributes.
446 */
447 if (!hugetlb_register_node(node))
448 hugetlb_unregister_node(node);
449 }
450
451 static void init_node_hugetlb_work(int nid)
452 {
453 INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work);
454 }
455
456 static int node_memory_callback(struct notifier_block *self,
457 unsigned long action, void *arg)
458 {
459 struct memory_notify *mnb = arg;
460 int nid = mnb->status_change_nid;
461
462 switch (action) {
463 case MEM_ONLINE:
464 case MEM_OFFLINE:
465 /*
466 * offload per node hstate [un]registration to a work thread
467 * when transitioning to/from memoryless state.
468 */
469 if (nid != NUMA_NO_NODE)
470 schedule_work(&node_devices[nid].node_work);
471 break;
472
473 case MEM_GOING_ONLINE:
474 case MEM_GOING_OFFLINE:
475 case MEM_CANCEL_ONLINE:
476 case MEM_CANCEL_OFFLINE:
477 default:
478 break;
479 }
480
481 return NOTIFY_OK;
482 }
483 #endif /* CONFIG_HUGETLBFS */
484 #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
485
486 static int link_mem_sections(int nid) { return 0; }
487 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
488
489 #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
490 !defined(CONFIG_HUGETLBFS)
491 static inline int node_memory_callback(struct notifier_block *self,
492 unsigned long action, void *arg)
493 {
494 return NOTIFY_OK;
495 }
496
497 static void init_node_hugetlb_work(int nid) { }
498
499 #endif
500
501 int register_one_node(int nid)
502 {
503 int error = 0;
504 int cpu;
505
506 if (node_online(nid)) {
507 int p_node = parent_node(nid);
508 struct node *parent = NULL;
509
510 if (p_node != nid)
511 parent = &node_devices[p_node];
512
513 error = register_node(&node_devices[nid], nid, parent);
514
515 /* link cpu under this node */
516 for_each_present_cpu(cpu) {
517 if (cpu_to_node(cpu) == nid)
518 register_cpu_under_node(cpu, nid);
519 }
520
521 /* link memory sections under this node */
522 error = link_mem_sections(nid);
523
524 /* initialize work queue for memory hot plug */
525 init_node_hugetlb_work(nid);
526 }
527
528 return error;
529
530 }
531
532 void unregister_one_node(int nid)
533 {
534 unregister_node(&node_devices[nid]);
535 }
536
537 /*
538 * node states attributes
539 */
540
541 static ssize_t print_nodes_state(enum node_states state, char *buf)
542 {
543 int n;
544
545 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
546 if (n > 0 && PAGE_SIZE > n + 1) {
547 *(buf + n++) = '\n';
548 *(buf + n++) = '\0';
549 }
550 return n;
551 }
552
553 struct node_attr {
554 struct sysdev_class_attribute attr;
555 enum node_states state;
556 };
557
558 static ssize_t show_node_state(struct sysdev_class *class,
559 struct sysdev_class_attribute *attr, char *buf)
560 {
561 struct node_attr *na = container_of(attr, struct node_attr, attr);
562 return print_nodes_state(na->state, buf);
563 }
564
565 #define _NODE_ATTR(name, state) \
566 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
567
568 static struct node_attr node_state_attr[] = {
569 _NODE_ATTR(possible, N_POSSIBLE),
570 _NODE_ATTR(online, N_ONLINE),
571 _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
572 _NODE_ATTR(has_cpu, N_CPU),
573 #ifdef CONFIG_HIGHMEM
574 _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
575 #endif
576 };
577
578 static struct sysdev_class_attribute *node_state_attrs[] = {
579 &node_state_attr[0].attr,
580 &node_state_attr[1].attr,
581 &node_state_attr[2].attr,
582 &node_state_attr[3].attr,
583 #ifdef CONFIG_HIGHMEM
584 &node_state_attr[4].attr,
585 #endif
586 NULL
587 };
588
589 #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
590 static int __init register_node_type(void)
591 {
592 int ret;
593
594 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
595 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
596
597 ret = sysdev_class_register(&node_class);
598 if (!ret) {
599 hotplug_memory_notifier(node_memory_callback,
600 NODE_CALLBACK_PRI);
601 }
602
603 /*
604 * Note: we're not going to unregister the node class if we fail
605 * to register the node state class attribute files.
606 */
607 return ret;
608 }
609 postcore_initcall(register_node_type);