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