node.c 16 KB

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