Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 3 | * Basic Node interface support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/mm.h> |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 9 | #include <linux/memory.h> |
KOSAKI Motohiro | fa25c50 | 2011-05-24 17:11:28 -0700 | [diff] [blame] | 10 | #include <linux/vmstat.h> |
Andrew Morton | 6e259e7 | 2013-04-29 15:08:07 -0700 | [diff] [blame] | 11 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/node.h> |
| 13 | #include <linux/hugetlb.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 14 | #include <linux/compaction.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/cpumask.h> |
| 16 | #include <linux/topology.h> |
| 17 | #include <linux/nodemask.h> |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 18 | #include <linux/cpu.h> |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 19 | #include <linux/device.h> |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 20 | #include <linux/pm_runtime.h> |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 21 | #include <linux/swap.h> |
Tejun Heo | 18e5b53 | 2010-04-06 19:23:33 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 24 | static struct bus_type node_subsys = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 25 | .name = "node", |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 26 | .dev_name = "node", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 30 | static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Zhen Lei | 064f0e9 | 2017-10-13 15:57:50 -0700 | [diff] [blame] | 32 | ssize_t n; |
| 33 | cpumask_var_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct node *node_dev = to_node(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 36 | /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */ |
| 37 | BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Zhen Lei | 064f0e9 | 2017-10-13 15:57:50 -0700 | [diff] [blame] | 39 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 40 | return 0; |
| 41 | |
| 42 | cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask); |
| 43 | n = cpumap_print_to_pagebuf(list, buf, mask); |
| 44 | free_cpumask_var(mask); |
| 45 | |
| 46 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 49 | static inline ssize_t node_read_cpumask(struct device *dev, |
| 50 | struct device_attribute *attr, char *buf) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 51 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 52 | return node_read_cpumap(dev, false, buf); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 53 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 54 | static inline ssize_t node_read_cpulist(struct device *dev, |
| 55 | struct device_attribute *attr, char *buf) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 56 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 57 | return node_read_cpumap(dev, true, buf); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 60 | static DEVICE_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL); |
| 61 | static DEVICE_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 63 | /** |
| 64 | * struct node_access_nodes - Access class device to hold user visible |
| 65 | * relationships to other nodes. |
| 66 | * @dev: Device for this memory access class |
| 67 | * @list_node: List element in the node's access list |
| 68 | * @access: The access class rank |
| 69 | */ |
| 70 | struct node_access_nodes { |
| 71 | struct device dev; |
| 72 | struct list_head list_node; |
| 73 | unsigned access; |
Keith Busch | e1cf33a | 2019-03-11 14:56:01 -0600 | [diff] [blame^] | 74 | #ifdef CONFIG_HMEM_REPORTING |
| 75 | struct node_hmem_attrs hmem_attrs; |
| 76 | #endif |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 77 | }; |
| 78 | #define to_access_nodes(dev) container_of(dev, struct node_access_nodes, dev) |
| 79 | |
| 80 | static struct attribute *node_init_access_node_attrs[] = { |
| 81 | NULL, |
| 82 | }; |
| 83 | |
| 84 | static struct attribute *node_targ_access_node_attrs[] = { |
| 85 | NULL, |
| 86 | }; |
| 87 | |
| 88 | static const struct attribute_group initiators = { |
| 89 | .name = "initiators", |
| 90 | .attrs = node_init_access_node_attrs, |
| 91 | }; |
| 92 | |
| 93 | static const struct attribute_group targets = { |
| 94 | .name = "targets", |
| 95 | .attrs = node_targ_access_node_attrs, |
| 96 | }; |
| 97 | |
| 98 | static const struct attribute_group *node_access_node_groups[] = { |
| 99 | &initiators, |
| 100 | &targets, |
| 101 | NULL, |
| 102 | }; |
| 103 | |
| 104 | static void node_remove_accesses(struct node *node) |
| 105 | { |
| 106 | struct node_access_nodes *c, *cnext; |
| 107 | |
| 108 | list_for_each_entry_safe(c, cnext, &node->access_list, list_node) { |
| 109 | list_del(&c->list_node); |
| 110 | device_unregister(&c->dev); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static void node_access_release(struct device *dev) |
| 115 | { |
| 116 | kfree(to_access_nodes(dev)); |
| 117 | } |
| 118 | |
| 119 | static struct node_access_nodes *node_init_node_access(struct node *node, |
| 120 | unsigned access) |
| 121 | { |
| 122 | struct node_access_nodes *access_node; |
| 123 | struct device *dev; |
| 124 | |
| 125 | list_for_each_entry(access_node, &node->access_list, list_node) |
| 126 | if (access_node->access == access) |
| 127 | return access_node; |
| 128 | |
| 129 | access_node = kzalloc(sizeof(*access_node), GFP_KERNEL); |
| 130 | if (!access_node) |
| 131 | return NULL; |
| 132 | |
| 133 | access_node->access = access; |
| 134 | dev = &access_node->dev; |
| 135 | dev->parent = &node->dev; |
| 136 | dev->release = node_access_release; |
| 137 | dev->groups = node_access_node_groups; |
| 138 | if (dev_set_name(dev, "access%u", access)) |
| 139 | goto free; |
| 140 | |
| 141 | if (device_register(dev)) |
| 142 | goto free_name; |
| 143 | |
| 144 | pm_runtime_no_callbacks(dev); |
| 145 | list_add_tail(&access_node->list_node, &node->access_list); |
| 146 | return access_node; |
| 147 | free_name: |
| 148 | kfree_const(dev->kobj.name); |
| 149 | free: |
| 150 | kfree(access_node); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Keith Busch | e1cf33a | 2019-03-11 14:56:01 -0600 | [diff] [blame^] | 154 | #ifdef CONFIG_HMEM_REPORTING |
| 155 | #define ACCESS_ATTR(name) \ |
| 156 | static ssize_t name##_show(struct device *dev, \ |
| 157 | struct device_attribute *attr, \ |
| 158 | char *buf) \ |
| 159 | { \ |
| 160 | return sprintf(buf, "%u\n", to_access_nodes(dev)->hmem_attrs.name); \ |
| 161 | } \ |
| 162 | static DEVICE_ATTR_RO(name); |
| 163 | |
| 164 | ACCESS_ATTR(read_bandwidth) |
| 165 | ACCESS_ATTR(read_latency) |
| 166 | ACCESS_ATTR(write_bandwidth) |
| 167 | ACCESS_ATTR(write_latency) |
| 168 | |
| 169 | static struct attribute *access_attrs[] = { |
| 170 | &dev_attr_read_bandwidth.attr, |
| 171 | &dev_attr_read_latency.attr, |
| 172 | &dev_attr_write_bandwidth.attr, |
| 173 | &dev_attr_write_latency.attr, |
| 174 | NULL, |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * node_set_perf_attrs - Set the performance values for given access class |
| 179 | * @nid: Node identifier to be set |
| 180 | * @hmem_attrs: Heterogeneous memory performance attributes |
| 181 | * @access: The access class the for the given attributes |
| 182 | */ |
| 183 | void node_set_perf_attrs(unsigned int nid, struct node_hmem_attrs *hmem_attrs, |
| 184 | unsigned access) |
| 185 | { |
| 186 | struct node_access_nodes *c; |
| 187 | struct node *node; |
| 188 | int i; |
| 189 | |
| 190 | if (WARN_ON_ONCE(!node_online(nid))) |
| 191 | return; |
| 192 | |
| 193 | node = node_devices[nid]; |
| 194 | c = node_init_node_access(node, access); |
| 195 | if (!c) |
| 196 | return; |
| 197 | |
| 198 | c->hmem_attrs = *hmem_attrs; |
| 199 | for (i = 0; access_attrs[i] != NULL; i++) { |
| 200 | if (sysfs_add_file_to_group(&c->dev.kobj, access_attrs[i], |
| 201 | "initiators")) { |
| 202 | pr_info("failed to add performance attribute to node %d\n", |
| 203 | nid); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | #endif |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 211 | static ssize_t node_read_meminfo(struct device *dev, |
| 212 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | int n; |
| 215 | int nid = dev->id; |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 216 | struct pglist_data *pgdat = NODE_DATA(nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | struct sysinfo i; |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 218 | unsigned long sreclaimable, sunreclaimable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | si_meminfo_node(&i, nid); |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 221 | sreclaimable = node_page_state(pgdat, NR_SLAB_RECLAIMABLE); |
| 222 | sunreclaimable = node_page_state(pgdat, NR_SLAB_UNRECLAIMABLE); |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 223 | n = sprintf(buf, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 224 | "Node %d MemTotal: %8lu kB\n" |
| 225 | "Node %d MemFree: %8lu kB\n" |
| 226 | "Node %d MemUsed: %8lu kB\n" |
| 227 | "Node %d Active: %8lu kB\n" |
| 228 | "Node %d Inactive: %8lu kB\n" |
| 229 | "Node %d Active(anon): %8lu kB\n" |
| 230 | "Node %d Inactive(anon): %8lu kB\n" |
| 231 | "Node %d Active(file): %8lu kB\n" |
| 232 | "Node %d Inactive(file): %8lu kB\n" |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 233 | "Node %d Unevictable: %8lu kB\n" |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 234 | "Node %d Mlocked: %8lu kB\n", |
| 235 | nid, K(i.totalram), |
| 236 | nid, K(i.freeram), |
| 237 | nid, K(i.totalram - i.freeram), |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 238 | nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) + |
| 239 | node_page_state(pgdat, NR_ACTIVE_FILE)), |
| 240 | nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) + |
| 241 | node_page_state(pgdat, NR_INACTIVE_FILE)), |
| 242 | nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)), |
| 243 | nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)), |
| 244 | nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)), |
| 245 | nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)), |
| 246 | nid, K(node_page_state(pgdat, NR_UNEVICTABLE)), |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 247 | nid, K(sum_zone_node_page_state(nid, NR_MLOCK))); |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 248 | |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 249 | #ifdef CONFIG_HIGHMEM |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 250 | n += sprintf(buf + n, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 251 | "Node %d HighTotal: %8lu kB\n" |
| 252 | "Node %d HighFree: %8lu kB\n" |
| 253 | "Node %d LowTotal: %8lu kB\n" |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 254 | "Node %d LowFree: %8lu kB\n", |
| 255 | nid, K(i.totalhigh), |
| 256 | nid, K(i.freehigh), |
| 257 | nid, K(i.totalram - i.totalhigh), |
| 258 | nid, K(i.freeram - i.freehigh)); |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 259 | #endif |
KOSAKI Motohiro | 7ee9225 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 260 | n += sprintf(buf + n, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 261 | "Node %d Dirty: %8lu kB\n" |
| 262 | "Node %d Writeback: %8lu kB\n" |
| 263 | "Node %d FilePages: %8lu kB\n" |
| 264 | "Node %d Mapped: %8lu kB\n" |
| 265 | "Node %d AnonPages: %8lu kB\n" |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 266 | "Node %d Shmem: %8lu kB\n" |
KOSAKI Motohiro | c6a7f57 | 2009-09-21 17:01:32 -0700 | [diff] [blame] | 267 | "Node %d KernelStack: %8lu kB\n" |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 268 | "Node %d PageTables: %8lu kB\n" |
| 269 | "Node %d NFS_Unstable: %8lu kB\n" |
| 270 | "Node %d Bounce: %8lu kB\n" |
| 271 | "Node %d WritebackTmp: %8lu kB\n" |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 272 | "Node %d KReclaimable: %8lu kB\n" |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 273 | "Node %d Slab: %8lu kB\n" |
| 274 | "Node %d SReclaimable: %8lu kB\n" |
David Rientjes | 05b258e | 2011-01-13 15:47:14 -0800 | [diff] [blame] | 275 | "Node %d SUnreclaim: %8lu kB\n" |
| 276 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 277 | "Node %d AnonHugePages: %8lu kB\n" |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 278 | "Node %d ShmemHugePages: %8lu kB\n" |
| 279 | "Node %d ShmemPmdMapped: %8lu kB\n" |
David Rientjes | 05b258e | 2011-01-13 15:47:14 -0800 | [diff] [blame] | 280 | #endif |
| 281 | , |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 282 | nid, K(node_page_state(pgdat, NR_FILE_DIRTY)), |
| 283 | nid, K(node_page_state(pgdat, NR_WRITEBACK)), |
| 284 | nid, K(node_page_state(pgdat, NR_FILE_PAGES)), |
Mel Gorman | 50658e2 | 2016-07-28 15:46:14 -0700 | [diff] [blame] | 285 | nid, K(node_page_state(pgdat, NR_FILE_MAPPED)), |
Mel Gorman | 4b9d0fa | 2016-07-28 15:46:17 -0700 | [diff] [blame] | 286 | nid, K(node_page_state(pgdat, NR_ANON_MAPPED)), |
Rafael Aquini | cc7452b | 2014-08-06 16:06:38 -0700 | [diff] [blame] | 287 | nid, K(i.sharedram), |
Andy Lutomirski | d30dd8b | 2016-07-28 15:48:14 -0700 | [diff] [blame] | 288 | nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB), |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 289 | nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 290 | nid, K(node_page_state(pgdat, NR_UNSTABLE_NFS)), |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 291 | nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 292 | nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)), |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 293 | nid, K(sreclaimable + |
| 294 | node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)), |
| 295 | nid, K(sreclaimable + sunreclaimable), |
| 296 | nid, K(sreclaimable), |
| 297 | nid, K(sunreclaimable) |
David Rientjes | 05b258e | 2011-01-13 15:47:14 -0800 | [diff] [blame] | 298 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 299 | , |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 300 | nid, K(node_page_state(pgdat, NR_ANON_THPS) * |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 301 | HPAGE_PMD_NR), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 302 | nid, K(node_page_state(pgdat, NR_SHMEM_THPS) * |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 303 | HPAGE_PMD_NR), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 304 | nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) * |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 305 | HPAGE_PMD_NR) |
David Rientjes | 05b258e | 2011-01-13 15:47:14 -0800 | [diff] [blame] | 306 | #endif |
Vlastimil Babka | 61f94e1 | 2018-10-26 15:05:50 -0700 | [diff] [blame] | 307 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | n += hugetlb_report_node_meminfo(nid, buf + n); |
| 309 | return n; |
| 310 | } |
| 311 | |
| 312 | #undef K |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 313 | static DEVICE_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 315 | static ssize_t node_read_numastat(struct device *dev, |
| 316 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return sprintf(buf, |
| 319 | "numa_hit %lu\n" |
| 320 | "numa_miss %lu\n" |
| 321 | "numa_foreign %lu\n" |
| 322 | "interleave_hit %lu\n" |
| 323 | "local_node %lu\n" |
| 324 | "other_node %lu\n", |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 325 | sum_zone_numa_state(dev->id, NUMA_HIT), |
| 326 | sum_zone_numa_state(dev->id, NUMA_MISS), |
| 327 | sum_zone_numa_state(dev->id, NUMA_FOREIGN), |
| 328 | sum_zone_numa_state(dev->id, NUMA_INTERLEAVE_HIT), |
| 329 | sum_zone_numa_state(dev->id, NUMA_LOCAL), |
| 330 | sum_zone_numa_state(dev->id, NUMA_OTHER)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 332 | static DEVICE_ATTR(numastat, S_IRUGO, node_read_numastat, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 334 | static ssize_t node_read_vmstat(struct device *dev, |
| 335 | struct device_attribute *attr, char *buf) |
Michael Rubin | 2ac3903 | 2010-10-26 14:21:35 -0700 | [diff] [blame] | 336 | { |
| 337 | int nid = dev->id; |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 338 | struct pglist_data *pgdat = NODE_DATA(nid); |
KOSAKI Motohiro | fa25c50 | 2011-05-24 17:11:28 -0700 | [diff] [blame] | 339 | int i; |
| 340 | int n = 0; |
| 341 | |
| 342 | for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) |
| 343 | n += sprintf(buf+n, "%s %lu\n", vmstat_text[i], |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 344 | sum_zone_node_page_state(nid, i)); |
| 345 | |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 346 | #ifdef CONFIG_NUMA |
| 347 | for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 348 | n += sprintf(buf+n, "%s %lu\n", |
| 349 | vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 350 | sum_zone_numa_state(nid, i)); |
| 351 | #endif |
| 352 | |
| 353 | for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) |
| 354 | n += sprintf(buf+n, "%s %lu\n", |
| 355 | vmstat_text[i + NR_VM_ZONE_STAT_ITEMS + |
| 356 | NR_VM_NUMA_STAT_ITEMS], |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 357 | node_page_state(pgdat, i)); |
KOSAKI Motohiro | fa25c50 | 2011-05-24 17:11:28 -0700 | [diff] [blame] | 358 | |
| 359 | return n; |
Michael Rubin | 2ac3903 | 2010-10-26 14:21:35 -0700 | [diff] [blame] | 360 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 361 | static DEVICE_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL); |
Michael Rubin | 2ac3903 | 2010-10-26 14:21:35 -0700 | [diff] [blame] | 362 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 363 | static ssize_t node_read_distance(struct device *dev, |
Ana Nedelcu | 518d3f3 | 2015-03-08 12:48:48 +0200 | [diff] [blame] | 364 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
| 366 | int nid = dev->id; |
| 367 | int len = 0; |
| 368 | int i; |
| 369 | |
David Rientjes | 12ee3c0 | 2010-03-10 14:50:21 -0800 | [diff] [blame] | 370 | /* |
| 371 | * buf is currently PAGE_SIZE in length and each node needs 4 chars |
| 372 | * at the most (distance + space or newline). |
| 373 | */ |
| 374 | BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | for_each_online_node(i) |
| 377 | len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i)); |
| 378 | |
| 379 | len += sprintf(buf + len, "\n"); |
| 380 | return len; |
| 381 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 382 | static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Takashi Iwai | 3c9b8aa | 2015-01-29 12:29:22 +0100 | [diff] [blame] | 384 | static struct attribute *node_dev_attrs[] = { |
| 385 | &dev_attr_cpumap.attr, |
| 386 | &dev_attr_cpulist.attr, |
| 387 | &dev_attr_meminfo.attr, |
| 388 | &dev_attr_numastat.attr, |
| 389 | &dev_attr_distance.attr, |
| 390 | &dev_attr_vmstat.attr, |
| 391 | NULL |
| 392 | }; |
Greg Kroah-Hartman | 7ca7ec4 | 2015-03-25 13:47:17 +0100 | [diff] [blame] | 393 | ATTRIBUTE_GROUPS(node_dev); |
Takashi Iwai | 3c9b8aa | 2015-01-29 12:29:22 +0100 | [diff] [blame] | 394 | |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 395 | #ifdef CONFIG_HUGETLBFS |
| 396 | /* |
| 397 | * hugetlbfs per node attributes registration interface: |
| 398 | * When/if hugetlb[fs] subsystem initializes [sometime after this module], |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 399 | * it will register its per node attributes for all online nodes with |
| 400 | * memory. It will also call register_hugetlbfs_with_node(), below, to |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 401 | * register its attribute registration functions with this node driver. |
| 402 | * Once these hooks have been initialized, the node driver will call into |
| 403 | * the hugetlb module to [un]register attributes for hot-plugged nodes. |
| 404 | */ |
| 405 | static node_registration_func_t __hugetlb_register_node; |
| 406 | static node_registration_func_t __hugetlb_unregister_node; |
| 407 | |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 408 | static inline bool hugetlb_register_node(struct node *node) |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 409 | { |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 410 | if (__hugetlb_register_node && |
Lai Jiangshan | 8cebfcd | 2012-12-12 13:51:36 -0800 | [diff] [blame] | 411 | node_state(node->dev.id, N_MEMORY)) { |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 412 | __hugetlb_register_node(node); |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 413 | return true; |
| 414 | } |
| 415 | return false; |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static inline void hugetlb_unregister_node(struct node *node) |
| 419 | { |
| 420 | if (__hugetlb_unregister_node) |
| 421 | __hugetlb_unregister_node(node); |
| 422 | } |
| 423 | |
| 424 | void register_hugetlbfs_with_node(node_registration_func_t doregister, |
| 425 | node_registration_func_t unregister) |
| 426 | { |
| 427 | __hugetlb_register_node = doregister; |
| 428 | __hugetlb_unregister_node = unregister; |
| 429 | } |
| 430 | #else |
| 431 | static inline void hugetlb_register_node(struct node *node) {} |
| 432 | |
| 433 | static inline void hugetlb_unregister_node(struct node *node) {} |
| 434 | #endif |
| 435 | |
Yasuaki Ishimatsu | 8c7b5b4 | 2012-12-11 16:00:57 -0800 | [diff] [blame] | 436 | static void node_device_release(struct device *dev) |
| 437 | { |
| 438 | struct node *node = to_node(dev); |
| 439 | |
| 440 | #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS) |
| 441 | /* |
| 442 | * We schedule the work only when a memory section is |
| 443 | * onlined/offlined on this node. When we come here, |
| 444 | * all the memory on this node has been offlined, |
| 445 | * so we won't enqueue new work to this work. |
| 446 | * |
| 447 | * The work is using node->node_work, so we should |
| 448 | * flush work before freeing the memory. |
| 449 | */ |
| 450 | flush_work(&node->node_work); |
| 451 | #endif |
| 452 | kfree(node); |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 456 | * register_node - Setup a sysfs device for a node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * @num - Node number to use when creating the device. |
| 458 | * |
| 459 | * Initialize and register the node device. |
| 460 | */ |
Dou Liyang | a7be6e5 | 2017-07-10 15:49:20 -0700 | [diff] [blame] | 461 | static int register_node(struct node *node, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | int error; |
| 464 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 465 | node->dev.id = num; |
| 466 | node->dev.bus = &node_subsys; |
Yasuaki Ishimatsu | 8c7b5b4 | 2012-12-11 16:00:57 -0800 | [diff] [blame] | 467 | node->dev.release = node_device_release; |
Greg Kroah-Hartman | 7ca7ec4 | 2015-03-25 13:47:17 +0100 | [diff] [blame] | 468 | node->dev.groups = node_dev_groups; |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 469 | error = device_register(&node->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Arvind Yadav | c1cc0d5 | 2018-03-11 11:25:50 +0530 | [diff] [blame] | 471 | if (error) |
| 472 | put_device(&node->dev); |
| 473 | else { |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 474 | hugetlb_register_node(node); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 475 | |
| 476 | compaction_register_node(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | return error; |
| 479 | } |
| 480 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 481 | /** |
| 482 | * unregister_node - unregister a node device |
| 483 | * @node: node going away |
| 484 | * |
| 485 | * Unregisters a node device @node. All the devices on the node must be |
| 486 | * unregistered before calling this function. |
| 487 | */ |
| 488 | void unregister_node(struct node *node) |
| 489 | { |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 490 | hugetlb_unregister_node(node); /* no-op, if memoryless node */ |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 491 | node_remove_accesses(node); |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 492 | device_unregister(&node->dev); |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 493 | } |
| 494 | |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 495 | struct node *node_devices[MAX_NUMNODES]; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 496 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 497 | /* |
| 498 | * register cpu under node |
| 499 | */ |
| 500 | int register_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 501 | { |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 502 | int ret; |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 503 | struct device *obj; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 504 | |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 505 | if (!node_online(nid)) |
| 506 | return 0; |
| 507 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 508 | obj = get_cpu_device(cpu); |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 509 | if (!obj) |
| 510 | return 0; |
| 511 | |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 512 | ret = sysfs_create_link(&node_devices[nid]->dev.kobj, |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 513 | &obj->kobj, |
| 514 | kobject_name(&obj->kobj)); |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 515 | if (ret) |
| 516 | return ret; |
| 517 | |
| 518 | return sysfs_create_link(&obj->kobj, |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 519 | &node_devices[nid]->dev.kobj, |
| 520 | kobject_name(&node_devices[nid]->dev.kobj)); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 523 | /** |
| 524 | * register_memory_node_under_compute_node - link memory node to its compute |
| 525 | * node for a given access class. |
| 526 | * @mem_node: Memory node number |
| 527 | * @cpu_node: Cpu node number |
| 528 | * @access: Access class to register |
| 529 | * |
| 530 | * Description: |
| 531 | * For use with platforms that may have separate memory and compute nodes. |
| 532 | * This function will export node relationships linking which memory |
| 533 | * initiator nodes can access memory targets at a given ranked access |
| 534 | * class. |
| 535 | */ |
| 536 | int register_memory_node_under_compute_node(unsigned int mem_nid, |
| 537 | unsigned int cpu_nid, |
| 538 | unsigned access) |
| 539 | { |
| 540 | struct node *init_node, *targ_node; |
| 541 | struct node_access_nodes *initiator, *target; |
| 542 | int ret; |
| 543 | |
| 544 | if (!node_online(cpu_nid) || !node_online(mem_nid)) |
| 545 | return -ENODEV; |
| 546 | |
| 547 | init_node = node_devices[cpu_nid]; |
| 548 | targ_node = node_devices[mem_nid]; |
| 549 | initiator = node_init_node_access(init_node, access); |
| 550 | target = node_init_node_access(targ_node, access); |
| 551 | if (!initiator || !target) |
| 552 | return -ENOMEM; |
| 553 | |
| 554 | ret = sysfs_add_link_to_group(&initiator->dev.kobj, "targets", |
| 555 | &targ_node->dev.kobj, |
| 556 | dev_name(&targ_node->dev)); |
| 557 | if (ret) |
| 558 | return ret; |
| 559 | |
| 560 | ret = sysfs_add_link_to_group(&target->dev.kobj, "initiators", |
| 561 | &init_node->dev.kobj, |
| 562 | dev_name(&init_node->dev)); |
| 563 | if (ret) |
| 564 | goto err; |
| 565 | |
| 566 | return 0; |
| 567 | err: |
| 568 | sysfs_remove_link_from_group(&initiator->dev.kobj, "targets", |
| 569 | dev_name(&targ_node->dev)); |
| 570 | return ret; |
| 571 | } |
| 572 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 573 | int unregister_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 574 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 575 | struct device *obj; |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 576 | |
| 577 | if (!node_online(nid)) |
| 578 | return 0; |
| 579 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 580 | obj = get_cpu_device(cpu); |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 581 | if (!obj) |
| 582 | return 0; |
| 583 | |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 584 | sysfs_remove_link(&node_devices[nid]->dev.kobj, |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 585 | kobject_name(&obj->kobj)); |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 586 | sysfs_remove_link(&obj->kobj, |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 587 | kobject_name(&node_devices[nid]->dev.kobj)); |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 588 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 592 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 593 | static int __ref get_nid_for_pfn(unsigned long pfn) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 594 | { |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 595 | if (!pfn_valid_within(pfn)) |
| 596 | return -1; |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 597 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
Thomas Gleixner | 8cdde38 | 2017-05-16 20:42:39 +0200 | [diff] [blame] | 598 | if (system_state < SYSTEM_RUNNING) |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 599 | return early_pfn_to_nid(pfn); |
| 600 | #endif |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 601 | return pfn_to_nid(pfn); |
| 602 | } |
| 603 | |
| 604 | /* register memory section under specified node if it spans that node */ |
Oscar Salvador | 4fbce63 | 2018-08-17 15:46:22 -0700 | [diff] [blame] | 605 | int register_mem_sect_under_node(struct memory_block *mem_blk, void *arg) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 606 | { |
Oscar Salvador | 4fbce63 | 2018-08-17 15:46:22 -0700 | [diff] [blame] | 607 | int ret, nid = *(int *)arg; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 608 | unsigned long pfn, sect_start_pfn, sect_end_pfn; |
| 609 | |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 610 | mem_blk->nid = nid; |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 611 | |
| 612 | sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr); |
| 613 | sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr); |
| 614 | sect_end_pfn += PAGES_PER_SECTION - 1; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 615 | for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { |
| 616 | int page_nid; |
| 617 | |
Yinghai Lu | 0469785 | 2015-09-04 15:42:39 -0700 | [diff] [blame] | 618 | /* |
| 619 | * memory block could have several absent sections from start. |
| 620 | * skip pfn range from absent section |
| 621 | */ |
| 622 | if (!pfn_present(pfn)) { |
| 623 | pfn = round_down(pfn + PAGES_PER_SECTION, |
| 624 | PAGES_PER_SECTION) - 1; |
| 625 | continue; |
| 626 | } |
| 627 | |
Pavel Tatashin | fc44f7f | 2018-04-05 16:22:56 -0700 | [diff] [blame] | 628 | /* |
| 629 | * We need to check if page belongs to nid only for the boot |
| 630 | * case, during hotplug we know that all pages in the memory |
| 631 | * block belong to the same node. |
| 632 | */ |
Oscar Salvador | 4fbce63 | 2018-08-17 15:46:22 -0700 | [diff] [blame] | 633 | if (system_state == SYSTEM_BOOTING) { |
Pavel Tatashin | fc44f7f | 2018-04-05 16:22:56 -0700 | [diff] [blame] | 634 | page_nid = get_nid_for_pfn(pfn); |
| 635 | if (page_nid < 0) |
| 636 | continue; |
| 637 | if (page_nid != nid) |
| 638 | continue; |
| 639 | } |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 640 | ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 641 | &mem_blk->dev.kobj, |
| 642 | kobject_name(&mem_blk->dev.kobj)); |
Alex Chiang | dee5d0d | 2009-12-14 17:59:05 -0800 | [diff] [blame] | 643 | if (ret) |
| 644 | return ret; |
| 645 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 646 | return sysfs_create_link_nowarn(&mem_blk->dev.kobj, |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 647 | &node_devices[nid]->dev.kobj, |
| 648 | kobject_name(&node_devices[nid]->dev.kobj)); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 649 | } |
| 650 | /* mem section does not span the specified node */ |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /* unregister memory section under all nodes that it spans */ |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 655 | int unregister_mem_sect_under_nodes(struct memory_block *mem_blk, |
| 656 | unsigned long phys_index) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 657 | { |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 658 | NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 659 | unsigned long pfn, sect_start_pfn, sect_end_pfn; |
| 660 | |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 661 | if (!mem_blk) { |
| 662 | NODEMASK_FREE(unlinked_nodes); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 663 | return -EFAULT; |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 664 | } |
| 665 | if (!unlinked_nodes) |
| 666 | return -ENOMEM; |
| 667 | nodes_clear(*unlinked_nodes); |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 668 | |
| 669 | sect_start_pfn = section_nr_to_pfn(phys_index); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 670 | sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; |
| 671 | for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { |
Roel Kluin | 4750498 | 2009-03-10 12:55:45 -0700 | [diff] [blame] | 672 | int nid; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 673 | |
| 674 | nid = get_nid_for_pfn(pfn); |
| 675 | if (nid < 0) |
| 676 | continue; |
| 677 | if (!node_online(nid)) |
| 678 | continue; |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 679 | if (node_test_and_set(nid, *unlinked_nodes)) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 680 | continue; |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 681 | sysfs_remove_link(&node_devices[nid]->dev.kobj, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 682 | kobject_name(&mem_blk->dev.kobj)); |
| 683 | sysfs_remove_link(&mem_blk->dev.kobj, |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 684 | kobject_name(&node_devices[nid]->dev.kobj)); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 685 | } |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 686 | NODEMASK_FREE(unlinked_nodes); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
Oscar Salvador | 4fbce63 | 2018-08-17 15:46:22 -0700 | [diff] [blame] | 690 | int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 691 | { |
Oscar Salvador | 4fbce63 | 2018-08-17 15:46:22 -0700 | [diff] [blame] | 692 | return walk_memory_range(start_pfn, end_pfn, (void *)&nid, |
| 693 | register_mem_sect_under_node); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 694 | } |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 695 | |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 696 | #ifdef CONFIG_HUGETLBFS |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 697 | /* |
| 698 | * Handle per node hstate attribute [un]registration on transistions |
| 699 | * to/from memoryless state. |
| 700 | */ |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 701 | static void node_hugetlb_work(struct work_struct *work) |
| 702 | { |
| 703 | struct node *node = container_of(work, struct node, node_work); |
| 704 | |
| 705 | /* |
| 706 | * We only get here when a node transitions to/from memoryless state. |
| 707 | * We can detect which transition occurred by examining whether the |
| 708 | * node has memory now. hugetlb_register_node() already check this |
| 709 | * so we try to register the attributes. If that fails, then the |
| 710 | * node has transitioned to memoryless, try to unregister the |
| 711 | * attributes. |
| 712 | */ |
| 713 | if (!hugetlb_register_node(node)) |
| 714 | hugetlb_unregister_node(node); |
| 715 | } |
| 716 | |
| 717 | static void init_node_hugetlb_work(int nid) |
| 718 | { |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 719 | INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work); |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 720 | } |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 721 | |
| 722 | static int node_memory_callback(struct notifier_block *self, |
| 723 | unsigned long action, void *arg) |
| 724 | { |
| 725 | struct memory_notify *mnb = arg; |
| 726 | int nid = mnb->status_change_nid; |
| 727 | |
| 728 | switch (action) { |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 729 | case MEM_ONLINE: |
| 730 | case MEM_OFFLINE: |
| 731 | /* |
| 732 | * offload per node hstate [un]registration to a work thread |
| 733 | * when transitioning to/from memoryless state. |
| 734 | */ |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 735 | if (nid != NUMA_NO_NODE) |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 736 | schedule_work(&node_devices[nid]->node_work); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 737 | break; |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 738 | |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 739 | case MEM_GOING_ONLINE: |
| 740 | case MEM_GOING_OFFLINE: |
| 741 | case MEM_CANCEL_ONLINE: |
| 742 | case MEM_CANCEL_OFFLINE: |
| 743 | default: |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | return NOTIFY_OK; |
| 748 | } |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 749 | #endif /* CONFIG_HUGETLBFS */ |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 750 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 751 | |
| 752 | #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \ |
| 753 | !defined(CONFIG_HUGETLBFS) |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 754 | static inline int node_memory_callback(struct notifier_block *self, |
| 755 | unsigned long action, void *arg) |
| 756 | { |
| 757 | return NOTIFY_OK; |
| 758 | } |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 759 | |
| 760 | static void init_node_hugetlb_work(int nid) { } |
| 761 | |
| 762 | #endif |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 763 | |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 764 | int __register_one_node(int nid) |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 765 | { |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 766 | int error; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 767 | int cpu; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 768 | |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 769 | node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL); |
| 770 | if (!node_devices[nid]) |
| 771 | return -ENOMEM; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 772 | |
Dou Liyang | a7be6e5 | 2017-07-10 15:49:20 -0700 | [diff] [blame] | 773 | error = register_node(node_devices[nid], nid); |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 774 | |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 775 | /* link cpu under this node */ |
| 776 | for_each_present_cpu(cpu) { |
| 777 | if (cpu_to_node(cpu) == nid) |
| 778 | register_cpu_under_node(cpu, nid); |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Keith Busch | 08d9dbe | 2019-03-11 14:56:00 -0600 | [diff] [blame] | 781 | INIT_LIST_HEAD(&node_devices[nid]->access_list); |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 782 | /* initialize work queue for memory hot plug */ |
| 783 | init_node_hugetlb_work(nid); |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 784 | |
Michal Hocko | 9037a99 | 2017-07-06 15:37:49 -0700 | [diff] [blame] | 785 | return error; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void unregister_one_node(int nid) |
| 789 | { |
Xishi Qiu | 92d585e | 2014-03-06 17:18:21 +0800 | [diff] [blame] | 790 | if (!node_devices[nid]) |
| 791 | return; |
| 792 | |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 793 | unregister_node(node_devices[nid]); |
Wen Congyang | 8732794 | 2012-12-11 16:00:56 -0800 | [diff] [blame] | 794 | node_devices[nid] = NULL; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 797 | /* |
| 798 | * node states attributes |
| 799 | */ |
| 800 | |
| 801 | static ssize_t print_nodes_state(enum node_states state, char *buf) |
| 802 | { |
| 803 | int n; |
| 804 | |
Tejun Heo | f799b1a | 2015-02-13 14:37:56 -0800 | [diff] [blame] | 805 | n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
| 806 | nodemask_pr_args(&node_states[state])); |
Ryota Ozaki | f623881 | 2012-05-29 15:06:20 -0700 | [diff] [blame] | 807 | buf[n++] = '\n'; |
| 808 | buf[n] = '\0'; |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 809 | return n; |
| 810 | } |
| 811 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 812 | struct node_attr { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 813 | struct device_attribute attr; |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 814 | enum node_states state; |
| 815 | }; |
| 816 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 817 | static ssize_t show_node_state(struct device *dev, |
| 818 | struct device_attribute *attr, char *buf) |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 819 | { |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 820 | struct node_attr *na = container_of(attr, struct node_attr, attr); |
| 821 | return print_nodes_state(na->state, buf); |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 824 | #define _NODE_ATTR(name, state) \ |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 825 | { __ATTR(name, 0444, show_node_state, NULL), state } |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 826 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 827 | static struct node_attr node_state_attr[] = { |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 828 | [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE), |
| 829 | [N_ONLINE] = _NODE_ATTR(online, N_ONLINE), |
| 830 | [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY), |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 831 | #ifdef CONFIG_HIGHMEM |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 832 | [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY), |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 833 | #endif |
Lai Jiangshan | 20b2f52 | 2012-12-12 13:52:00 -0800 | [diff] [blame] | 834 | [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY), |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 835 | [N_CPU] = _NODE_ATTR(has_cpu, N_CPU), |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 836 | }; |
| 837 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 838 | static struct attribute *node_state_attrs[] = { |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 839 | &node_state_attr[N_POSSIBLE].attr.attr, |
| 840 | &node_state_attr[N_ONLINE].attr.attr, |
| 841 | &node_state_attr[N_NORMAL_MEMORY].attr.attr, |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame] | 842 | #ifdef CONFIG_HIGHMEM |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 843 | &node_state_attr[N_HIGH_MEMORY].attr.attr, |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame] | 844 | #endif |
Lai Jiangshan | 20b2f52 | 2012-12-12 13:52:00 -0800 | [diff] [blame] | 845 | &node_state_attr[N_MEMORY].attr.attr, |
Lai Jiangshan | fcf07d2 | 2012-12-11 16:03:13 -0800 | [diff] [blame] | 846 | &node_state_attr[N_CPU].attr.attr, |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame] | 847 | NULL |
| 848 | }; |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 849 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 850 | static struct attribute_group memory_root_attr_group = { |
| 851 | .attrs = node_state_attrs, |
| 852 | }; |
| 853 | |
| 854 | static const struct attribute_group *cpu_root_attr_groups[] = { |
| 855 | &memory_root_attr_group, |
| 856 | NULL, |
| 857 | }; |
| 858 | |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 859 | #define NODE_CALLBACK_PRI 2 /* lower than SLAB */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 860 | static int __init register_node_type(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | { |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 862 | int ret; |
| 863 | |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame] | 864 | BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES); |
| 865 | BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES); |
| 866 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 867 | ret = subsys_system_register(&node_subsys, cpu_root_attr_groups); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 868 | if (!ret) { |
Andrew Morton | 6e259e7 | 2013-04-29 15:08:07 -0700 | [diff] [blame] | 869 | static struct notifier_block node_memory_callback_nb = { |
| 870 | .notifier_call = node_memory_callback, |
| 871 | .priority = NODE_CALLBACK_PRI, |
| 872 | }; |
| 873 | register_hotmemory_notifier(&node_memory_callback_nb); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 874 | } |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * Note: we're not going to unregister the node class if we fail |
| 878 | * to register the node state class attribute files. |
| 879 | */ |
| 880 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | postcore_initcall(register_node_type); |