node.c 16 KB

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