xref: /linux-6.15/drivers/base/node.c (revision 84b25926)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
310fbcf4cSKay Sievers  * Basic Node interface support
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/mm.h>
9c04fc586SGary Hade #include <linux/memory.h>
10fa25c503SKOSAKI Motohiro #include <linux/vmstat.h>
116e259e7dSAndrew Morton #include <linux/notifier.h>
121da177e4SLinus Torvalds #include <linux/node.h>
131da177e4SLinus Torvalds #include <linux/hugetlb.h>
14ed4a6d7fSMel Gorman #include <linux/compaction.h>
151da177e4SLinus Torvalds #include <linux/cpumask.h>
161da177e4SLinus Torvalds #include <linux/topology.h>
171da177e4SLinus Torvalds #include <linux/nodemask.h>
1876b67ed9SKAMEZAWA Hiroyuki #include <linux/cpu.h>
19bde631a5SLee Schermerhorn #include <linux/device.h>
2008d9dbe7SKeith Busch #include <linux/pm_runtime.h>
21af936a16SLee Schermerhorn #include <linux/swap.h>
2218e5b539STejun Heo #include <linux/slab.h>
231da177e4SLinus Torvalds 
24580fc9c7SGreg Kroah-Hartman static const struct bus_type node_subsys = {
25af5ca3f4SKay Sievers 	.name = "node",
2610fbcf4cSKay Sievers 	.dev_name = "node",
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
cpumap_read(struct file * file,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)2975bd50faSTian Tao static inline ssize_t cpumap_read(struct file *file, struct kobject *kobj,
30562e932aSThomas Weißschuh 				  const struct bin_attribute *attr, char *buf,
3175bd50faSTian Tao 				  loff_t off, size_t count)
321da177e4SLinus Torvalds {
3375bd50faSTian Tao 	struct device *dev = kobj_to_dev(kobj);
341da177e4SLinus Torvalds 	struct node *node_dev = to_node(dev);
3575bd50faSTian Tao 	cpumask_var_t mask;
3675bd50faSTian Tao 	ssize_t n;
371da177e4SLinus Torvalds 
38064f0e93SZhen Lei 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
39064f0e93SZhen Lei 		return 0;
40064f0e93SZhen Lei 
41064f0e93SZhen Lei 	cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask);
4275bd50faSTian Tao 	n = cpumap_print_bitmask_to_buf(buf, mask, off, count);
43064f0e93SZhen Lei 	free_cpumask_var(mask);
44064f0e93SZhen Lei 
45064f0e93SZhen Lei 	return n;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
485943c0dcSThomas Weißschuh static const BIN_ATTR_RO(cpumap, CPUMAP_FILE_MAX_BYTES);
4975bd50faSTian Tao 
cpulist_read(struct file * file,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)5075bd50faSTian Tao static inline ssize_t cpulist_read(struct file *file, struct kobject *kobj,
51562e932aSThomas Weißschuh 				   const struct bin_attribute *attr, char *buf,
5275bd50faSTian Tao 				   loff_t off, size_t count)
5339106dcfSMike Travis {
5475bd50faSTian Tao 	struct device *dev = kobj_to_dev(kobj);
5575bd50faSTian Tao 	struct node *node_dev = to_node(dev);
5675bd50faSTian Tao 	cpumask_var_t mask;
5775bd50faSTian Tao 	ssize_t n;
5875bd50faSTian Tao 
5975bd50faSTian Tao 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
6075bd50faSTian Tao 		return 0;
6175bd50faSTian Tao 
6275bd50faSTian Tao 	cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask);
6375bd50faSTian Tao 	n = cpumap_print_list_to_buf(buf, mask, off, count);
6475bd50faSTian Tao 	free_cpumask_var(mask);
6575bd50faSTian Tao 
6675bd50faSTian Tao 	return n;
6739106dcfSMike Travis }
68948b3edbSJoe Perches 
695943c0dcSThomas Weißschuh static const BIN_ATTR_RO(cpulist, CPULIST_FILE_MAX_BYTES);
701da177e4SLinus Torvalds 
7108d9dbe7SKeith Busch /**
7208d9dbe7SKeith Busch  * struct node_access_nodes - Access class device to hold user visible
7308d9dbe7SKeith Busch  * 			      relationships to other nodes.
7408d9dbe7SKeith Busch  * @dev:	Device for this memory access class
7508d9dbe7SKeith Busch  * @list_node:	List element in the node's access list
7608d9dbe7SKeith Busch  * @access:	The access class rank
776a954e94SDave Jiang  * @coord:	Heterogeneous memory performance coordinates
7808d9dbe7SKeith Busch  */
7908d9dbe7SKeith Busch struct node_access_nodes {
8008d9dbe7SKeith Busch 	struct device		dev;
8108d9dbe7SKeith Busch 	struct list_head	list_node;
82e7deeb9dSJinchao Wang 	unsigned int		access;
83e1cf33aaSKeith Busch #ifdef CONFIG_HMEM_REPORTING
846a954e94SDave Jiang 	struct access_coordinate	coord;
85e1cf33aaSKeith Busch #endif
8608d9dbe7SKeith Busch };
8708d9dbe7SKeith Busch #define to_access_nodes(dev) container_of(dev, struct node_access_nodes, dev)
8808d9dbe7SKeith Busch 
8908d9dbe7SKeith Busch static struct attribute *node_init_access_node_attrs[] = {
9008d9dbe7SKeith Busch 	NULL,
9108d9dbe7SKeith Busch };
9208d9dbe7SKeith Busch 
9308d9dbe7SKeith Busch static struct attribute *node_targ_access_node_attrs[] = {
9408d9dbe7SKeith Busch 	NULL,
9508d9dbe7SKeith Busch };
9608d9dbe7SKeith Busch 
9708d9dbe7SKeith Busch static const struct attribute_group initiators = {
9808d9dbe7SKeith Busch 	.name	= "initiators",
9908d9dbe7SKeith Busch 	.attrs	= node_init_access_node_attrs,
10008d9dbe7SKeith Busch };
10108d9dbe7SKeith Busch 
10208d9dbe7SKeith Busch static const struct attribute_group targets = {
10308d9dbe7SKeith Busch 	.name	= "targets",
10408d9dbe7SKeith Busch 	.attrs	= node_targ_access_node_attrs,
10508d9dbe7SKeith Busch };
10608d9dbe7SKeith Busch 
10708d9dbe7SKeith Busch static const struct attribute_group *node_access_node_groups[] = {
10808d9dbe7SKeith Busch 	&initiators,
10908d9dbe7SKeith Busch 	&targets,
11008d9dbe7SKeith Busch 	NULL,
11108d9dbe7SKeith Busch };
11208d9dbe7SKeith Busch 
node_remove_accesses(struct node * node)11308d9dbe7SKeith Busch static void node_remove_accesses(struct node *node)
11408d9dbe7SKeith Busch {
11508d9dbe7SKeith Busch 	struct node_access_nodes *c, *cnext;
11608d9dbe7SKeith Busch 
11708d9dbe7SKeith Busch 	list_for_each_entry_safe(c, cnext, &node->access_list, list_node) {
11808d9dbe7SKeith Busch 		list_del(&c->list_node);
11908d9dbe7SKeith Busch 		device_unregister(&c->dev);
12008d9dbe7SKeith Busch 	}
12108d9dbe7SKeith Busch }
12208d9dbe7SKeith Busch 
node_access_release(struct device * dev)12308d9dbe7SKeith Busch static void node_access_release(struct device *dev)
12408d9dbe7SKeith Busch {
12508d9dbe7SKeith Busch 	kfree(to_access_nodes(dev));
12608d9dbe7SKeith Busch }
12708d9dbe7SKeith Busch 
node_init_node_access(struct node * node,enum access_coordinate_class access)12808d9dbe7SKeith Busch static struct node_access_nodes *node_init_node_access(struct node *node,
12911270e52SDave Jiang 						       enum access_coordinate_class access)
13008d9dbe7SKeith Busch {
13108d9dbe7SKeith Busch 	struct node_access_nodes *access_node;
13208d9dbe7SKeith Busch 	struct device *dev;
13308d9dbe7SKeith Busch 
13408d9dbe7SKeith Busch 	list_for_each_entry(access_node, &node->access_list, list_node)
13508d9dbe7SKeith Busch 		if (access_node->access == access)
13608d9dbe7SKeith Busch 			return access_node;
13708d9dbe7SKeith Busch 
13808d9dbe7SKeith Busch 	access_node = kzalloc(sizeof(*access_node), GFP_KERNEL);
13908d9dbe7SKeith Busch 	if (!access_node)
14008d9dbe7SKeith Busch 		return NULL;
14108d9dbe7SKeith Busch 
14208d9dbe7SKeith Busch 	access_node->access = access;
14308d9dbe7SKeith Busch 	dev = &access_node->dev;
14408d9dbe7SKeith Busch 	dev->parent = &node->dev;
14508d9dbe7SKeith Busch 	dev->release = node_access_release;
14608d9dbe7SKeith Busch 	dev->groups = node_access_node_groups;
14708d9dbe7SKeith Busch 	if (dev_set_name(dev, "access%u", access))
14808d9dbe7SKeith Busch 		goto free;
14908d9dbe7SKeith Busch 
15008d9dbe7SKeith Busch 	if (device_register(dev))
15108d9dbe7SKeith Busch 		goto free_name;
15208d9dbe7SKeith Busch 
15308d9dbe7SKeith Busch 	pm_runtime_no_callbacks(dev);
15408d9dbe7SKeith Busch 	list_add_tail(&access_node->list_node, &node->access_list);
15508d9dbe7SKeith Busch 	return access_node;
15608d9dbe7SKeith Busch free_name:
15708d9dbe7SKeith Busch 	kfree_const(dev->kobj.name);
15808d9dbe7SKeith Busch free:
15908d9dbe7SKeith Busch 	kfree(access_node);
16008d9dbe7SKeith Busch 	return NULL;
16108d9dbe7SKeith Busch }
16208d9dbe7SKeith Busch 
163e1cf33aaSKeith Busch #ifdef CONFIG_HMEM_REPORTING
1647810f4dcSDave Jiang #define ACCESS_ATTR(property)						\
1657810f4dcSDave Jiang static ssize_t property##_show(struct device *dev,			\
166e1cf33aaSKeith Busch 			   struct device_attribute *attr,		\
167e1cf33aaSKeith Busch 			   char *buf)					\
168e1cf33aaSKeith Busch {									\
169948b3edbSJoe Perches 	return sysfs_emit(buf, "%u\n",					\
1706a954e94SDave Jiang 			  to_access_nodes(dev)->coord.property);	\
171e1cf33aaSKeith Busch }									\
1727810f4dcSDave Jiang static DEVICE_ATTR_RO(property)
173e1cf33aaSKeith Busch 
1746284a6e8SJoe Perches ACCESS_ATTR(read_bandwidth);
1756284a6e8SJoe Perches ACCESS_ATTR(read_latency);
1766284a6e8SJoe Perches ACCESS_ATTR(write_bandwidth);
1776284a6e8SJoe Perches ACCESS_ATTR(write_latency);
178e1cf33aaSKeith Busch 
179e1cf33aaSKeith Busch static struct attribute *access_attrs[] = {
180e1cf33aaSKeith Busch 	&dev_attr_read_bandwidth.attr,
181e1cf33aaSKeith Busch 	&dev_attr_read_latency.attr,
182e1cf33aaSKeith Busch 	&dev_attr_write_bandwidth.attr,
183e1cf33aaSKeith Busch 	&dev_attr_write_latency.attr,
184e1cf33aaSKeith Busch 	NULL,
185e1cf33aaSKeith Busch };
186e1cf33aaSKeith Busch 
187e1cf33aaSKeith Busch /**
188e1cf33aaSKeith Busch  * node_set_perf_attrs - Set the performance values for given access class
189e1cf33aaSKeith Busch  * @nid: Node identifier to be set
1906a954e94SDave Jiang  * @coord: Heterogeneous memory performance coordinates
191e1cf33aaSKeith Busch  * @access: The access class the for the given attributes
192e1cf33aaSKeith Busch  */
node_set_perf_attrs(unsigned int nid,struct access_coordinate * coord,enum access_coordinate_class access)1936a954e94SDave Jiang void node_set_perf_attrs(unsigned int nid, struct access_coordinate *coord,
19411270e52SDave Jiang 			 enum access_coordinate_class access)
195e1cf33aaSKeith Busch {
196e1cf33aaSKeith Busch 	struct node_access_nodes *c;
197e1cf33aaSKeith Busch 	struct node *node;
198e1cf33aaSKeith Busch 	int i;
199e1cf33aaSKeith Busch 
200e1cf33aaSKeith Busch 	if (WARN_ON_ONCE(!node_online(nid)))
201e1cf33aaSKeith Busch 		return;
202e1cf33aaSKeith Busch 
203e1cf33aaSKeith Busch 	node = node_devices[nid];
204e1cf33aaSKeith Busch 	c = node_init_node_access(node, access);
205e1cf33aaSKeith Busch 	if (!c)
206e1cf33aaSKeith Busch 		return;
207e1cf33aaSKeith Busch 
2086a954e94SDave Jiang 	c->coord = *coord;
209e1cf33aaSKeith Busch 	for (i = 0; access_attrs[i] != NULL; i++) {
210e1cf33aaSKeith Busch 		if (sysfs_add_file_to_group(&c->dev.kobj, access_attrs[i],
211e1cf33aaSKeith Busch 					    "initiators")) {
212e1cf33aaSKeith Busch 			pr_info("failed to add performance attribute to node %d\n",
213e1cf33aaSKeith Busch 				nid);
214e1cf33aaSKeith Busch 			break;
215e1cf33aaSKeith Busch 		}
216e1cf33aaSKeith Busch 	}
217e1cf33aaSKeith Busch }
218debdce20SDave Jiang EXPORT_SYMBOL_GPL(node_set_perf_attrs);
219acc02a10SKeith Busch 
220acc02a10SKeith Busch /**
221acc02a10SKeith Busch  * struct node_cache_info - Internal tracking for memory node caches
222acc02a10SKeith Busch  * @dev:	Device represeting the cache level
223acc02a10SKeith Busch  * @node:	List element for tracking in the node
224acc02a10SKeith Busch  * @cache_attrs:Attributes for this cache level
225acc02a10SKeith Busch  */
226acc02a10SKeith Busch struct node_cache_info {
227acc02a10SKeith Busch 	struct device dev;
228acc02a10SKeith Busch 	struct list_head node;
229acc02a10SKeith Busch 	struct node_cache_attrs cache_attrs;
230acc02a10SKeith Busch };
231acc02a10SKeith Busch #define to_cache_info(device) container_of(device, struct node_cache_info, dev)
232acc02a10SKeith Busch 
233acc02a10SKeith Busch #define CACHE_ATTR(name, fmt) 						\
234acc02a10SKeith Busch static ssize_t name##_show(struct device *dev,				\
235acc02a10SKeith Busch 			   struct device_attribute *attr,		\
236acc02a10SKeith Busch 			   char *buf)					\
237acc02a10SKeith Busch {									\
238948b3edbSJoe Perches 	return sysfs_emit(buf, fmt "\n",				\
239948b3edbSJoe Perches 			  to_cache_info(dev)->cache_attrs.name);	\
240acc02a10SKeith Busch }									\
241fd03c075SRuiqi Gong static DEVICE_ATTR_RO(name);
242acc02a10SKeith Busch 
243acc02a10SKeith Busch CACHE_ATTR(size, "%llu")
244acc02a10SKeith Busch CACHE_ATTR(line_size, "%u")
245acc02a10SKeith Busch CACHE_ATTR(indexing, "%u")
246acc02a10SKeith Busch CACHE_ATTR(write_policy, "%u")
247*84b25926SDave Jiang CACHE_ATTR(address_mode, "%#x")
248acc02a10SKeith Busch 
249acc02a10SKeith Busch static struct attribute *cache_attrs[] = {
250acc02a10SKeith Busch 	&dev_attr_indexing.attr,
251acc02a10SKeith Busch 	&dev_attr_size.attr,
252acc02a10SKeith Busch 	&dev_attr_line_size.attr,
253acc02a10SKeith Busch 	&dev_attr_write_policy.attr,
254*84b25926SDave Jiang 	&dev_attr_address_mode.attr,
255acc02a10SKeith Busch 	NULL,
256acc02a10SKeith Busch };
257acc02a10SKeith Busch ATTRIBUTE_GROUPS(cache);
258acc02a10SKeith Busch 
node_cache_release(struct device * dev)259acc02a10SKeith Busch static void node_cache_release(struct device *dev)
260acc02a10SKeith Busch {
261acc02a10SKeith Busch 	kfree(dev);
262acc02a10SKeith Busch }
263acc02a10SKeith Busch 
node_cacheinfo_release(struct device * dev)264acc02a10SKeith Busch static void node_cacheinfo_release(struct device *dev)
265acc02a10SKeith Busch {
266acc02a10SKeith Busch 	struct node_cache_info *info = to_cache_info(dev);
267acc02a10SKeith Busch 	kfree(info);
268acc02a10SKeith Busch }
269acc02a10SKeith Busch 
node_init_cache_dev(struct node * node)270acc02a10SKeith Busch static void node_init_cache_dev(struct node *node)
271acc02a10SKeith Busch {
272acc02a10SKeith Busch 	struct device *dev;
273acc02a10SKeith Busch 
274acc02a10SKeith Busch 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
275acc02a10SKeith Busch 	if (!dev)
276acc02a10SKeith Busch 		return;
277acc02a10SKeith Busch 
2784ce535ecSDan Carpenter 	device_initialize(dev);
279acc02a10SKeith Busch 	dev->parent = &node->dev;
280acc02a10SKeith Busch 	dev->release = node_cache_release;
281acc02a10SKeith Busch 	if (dev_set_name(dev, "memory_side_cache"))
2824ce535ecSDan Carpenter 		goto put_device;
283acc02a10SKeith Busch 
2844ce535ecSDan Carpenter 	if (device_add(dev))
2854ce535ecSDan Carpenter 		goto put_device;
286acc02a10SKeith Busch 
287acc02a10SKeith Busch 	pm_runtime_no_callbacks(dev);
288acc02a10SKeith Busch 	node->cache_dev = dev;
289acc02a10SKeith Busch 	return;
2904ce535ecSDan Carpenter put_device:
2914ce535ecSDan Carpenter 	put_device(dev);
292acc02a10SKeith Busch }
293acc02a10SKeith Busch 
294acc02a10SKeith Busch /**
295acc02a10SKeith Busch  * node_add_cache() - add cache attribute to a memory node
296acc02a10SKeith Busch  * @nid: Node identifier that has new cache attributes
297acc02a10SKeith Busch  * @cache_attrs: Attributes for the cache being added
298acc02a10SKeith Busch  */
node_add_cache(unsigned int nid,struct node_cache_attrs * cache_attrs)299acc02a10SKeith Busch void node_add_cache(unsigned int nid, struct node_cache_attrs *cache_attrs)
300acc02a10SKeith Busch {
301acc02a10SKeith Busch 	struct node_cache_info *info;
302acc02a10SKeith Busch 	struct device *dev;
303acc02a10SKeith Busch 	struct node *node;
304acc02a10SKeith Busch 
305acc02a10SKeith Busch 	if (!node_online(nid) || !node_devices[nid])
306acc02a10SKeith Busch 		return;
307acc02a10SKeith Busch 
308acc02a10SKeith Busch 	node = node_devices[nid];
309acc02a10SKeith Busch 	list_for_each_entry(info, &node->cache_attrs, node) {
310acc02a10SKeith Busch 		if (info->cache_attrs.level == cache_attrs->level) {
311acc02a10SKeith Busch 			dev_warn(&node->dev,
312acc02a10SKeith Busch 				"attempt to add duplicate cache level:%d\n",
313acc02a10SKeith Busch 				cache_attrs->level);
314acc02a10SKeith Busch 			return;
315acc02a10SKeith Busch 		}
316acc02a10SKeith Busch 	}
317acc02a10SKeith Busch 
318acc02a10SKeith Busch 	if (!node->cache_dev)
319acc02a10SKeith Busch 		node_init_cache_dev(node);
320acc02a10SKeith Busch 	if (!node->cache_dev)
321acc02a10SKeith Busch 		return;
322acc02a10SKeith Busch 
323acc02a10SKeith Busch 	info = kzalloc(sizeof(*info), GFP_KERNEL);
324acc02a10SKeith Busch 	if (!info)
325acc02a10SKeith Busch 		return;
326acc02a10SKeith Busch 
327acc02a10SKeith Busch 	dev = &info->dev;
3284ce535ecSDan Carpenter 	device_initialize(dev);
329acc02a10SKeith Busch 	dev->parent = node->cache_dev;
330acc02a10SKeith Busch 	dev->release = node_cacheinfo_release;
331acc02a10SKeith Busch 	dev->groups = cache_groups;
332acc02a10SKeith Busch 	if (dev_set_name(dev, "index%d", cache_attrs->level))
3334ce535ecSDan Carpenter 		goto put_device;
334acc02a10SKeith Busch 
335acc02a10SKeith Busch 	info->cache_attrs = *cache_attrs;
3364ce535ecSDan Carpenter 	if (device_add(dev)) {
337acc02a10SKeith Busch 		dev_warn(&node->dev, "failed to add cache level:%d\n",
338acc02a10SKeith Busch 			 cache_attrs->level);
3394ce535ecSDan Carpenter 		goto put_device;
340acc02a10SKeith Busch 	}
341acc02a10SKeith Busch 	pm_runtime_no_callbacks(dev);
342acc02a10SKeith Busch 	list_add_tail(&info->node, &node->cache_attrs);
343acc02a10SKeith Busch 	return;
3444ce535ecSDan Carpenter put_device:
3454ce535ecSDan Carpenter 	put_device(dev);
346acc02a10SKeith Busch }
347acc02a10SKeith Busch 
node_remove_caches(struct node * node)348acc02a10SKeith Busch static void node_remove_caches(struct node *node)
349acc02a10SKeith Busch {
350acc02a10SKeith Busch 	struct node_cache_info *info, *next;
351acc02a10SKeith Busch 
352acc02a10SKeith Busch 	if (!node->cache_dev)
353acc02a10SKeith Busch 		return;
354acc02a10SKeith Busch 
355acc02a10SKeith Busch 	list_for_each_entry_safe(info, next, &node->cache_attrs, node) {
356acc02a10SKeith Busch 		list_del(&info->node);
357acc02a10SKeith Busch 		device_unregister(&info->dev);
358acc02a10SKeith Busch 	}
359acc02a10SKeith Busch 	device_unregister(node->cache_dev);
360acc02a10SKeith Busch }
361acc02a10SKeith Busch 
node_init_caches(unsigned int nid)362acc02a10SKeith Busch static void node_init_caches(unsigned int nid)
363acc02a10SKeith Busch {
364acc02a10SKeith Busch 	INIT_LIST_HEAD(&node_devices[nid]->cache_attrs);
365acc02a10SKeith Busch }
366acc02a10SKeith Busch #else
node_init_caches(unsigned int nid)367acc02a10SKeith Busch static void node_init_caches(unsigned int nid) { }
node_remove_caches(struct node * node)368acc02a10SKeith Busch static void node_remove_caches(struct node *node) { }
369e1cf33aaSKeith Busch #endif
370e1cf33aaSKeith Busch 
3711da177e4SLinus Torvalds #define K(x) ((x) << (PAGE_SHIFT - 10))
node_read_meminfo(struct device * dev,struct device_attribute * attr,char * buf)37210fbcf4cSKay Sievers static ssize_t node_read_meminfo(struct device *dev,
37310fbcf4cSKay Sievers 			struct device_attribute *attr, char *buf)
3741da177e4SLinus Torvalds {
375948b3edbSJoe Perches 	int len = 0;
3761da177e4SLinus Torvalds 	int nid = dev->id;
377599d0c95SMel Gorman 	struct pglist_data *pgdat = NODE_DATA(nid);
3781da177e4SLinus Torvalds 	struct sysinfo i;
37961f94e18SVlastimil Babka 	unsigned long sreclaimable, sunreclaimable;
380b6038942SShakeel Butt 	unsigned long swapcached = 0;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	si_meminfo_node(&i, nid);
383d42f3245SRoman Gushchin 	sreclaimable = node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B);
384d42f3245SRoman Gushchin 	sunreclaimable = node_page_state_pages(pgdat, NR_SLAB_UNRECLAIMABLE_B);
385b6038942SShakeel Butt #ifdef CONFIG_SWAP
386b6038942SShakeel Butt 	swapcached = node_page_state_pages(pgdat, NR_SWAPCACHE);
387b6038942SShakeel Butt #endif
388948b3edbSJoe Perches 	len = sysfs_emit_at(buf, len,
3891da177e4SLinus Torvalds 			    "Node %d MemTotal:       %8lu kB\n"
3901da177e4SLinus Torvalds 			    "Node %d MemFree:        %8lu kB\n"
3911da177e4SLinus Torvalds 			    "Node %d MemUsed:        %8lu kB\n"
392b6038942SShakeel Butt 			    "Node %d SwapCached:     %8lu kB\n"
3931da177e4SLinus Torvalds 			    "Node %d Active:         %8lu kB\n"
3941da177e4SLinus Torvalds 			    "Node %d Inactive:       %8lu kB\n"
3954f98a2feSRik van Riel 			    "Node %d Active(anon):   %8lu kB\n"
3964f98a2feSRik van Riel 			    "Node %d Inactive(anon): %8lu kB\n"
3974f98a2feSRik van Riel 			    "Node %d Active(file):   %8lu kB\n"
3984f98a2feSRik van Riel 			    "Node %d Inactive(file): %8lu kB\n"
3995344b7e6SNick Piggin 			    "Node %d Unevictable:    %8lu kB\n"
4007ee92255SKOSAKI Motohiro 			    "Node %d Mlocked:        %8lu kB\n",
4017ee92255SKOSAKI Motohiro 			    nid, K(i.totalram),
4027ee92255SKOSAKI Motohiro 			    nid, K(i.freeram),
4037ee92255SKOSAKI Motohiro 			    nid, K(i.totalram - i.freeram),
404b6038942SShakeel Butt 			    nid, K(swapcached),
405599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
406599d0c95SMel Gorman 				   node_page_state(pgdat, NR_ACTIVE_FILE)),
407599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
408599d0c95SMel Gorman 				   node_page_state(pgdat, NR_INACTIVE_FILE)),
409599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
410599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
411599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
412599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
413599d0c95SMel Gorman 			    nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
41475ef7184SMel Gorman 			    nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
4157ee92255SKOSAKI Motohiro 
416182e8e23SChristoph Lameter #ifdef CONFIG_HIGHMEM
417948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len,
4181da177e4SLinus Torvalds 			     "Node %d HighTotal:      %8lu kB\n"
4191da177e4SLinus Torvalds 			     "Node %d HighFree:       %8lu kB\n"
4201da177e4SLinus Torvalds 			     "Node %d LowTotal:       %8lu kB\n"
4217ee92255SKOSAKI Motohiro 			     "Node %d LowFree:        %8lu kB\n",
4227ee92255SKOSAKI Motohiro 			     nid, K(i.totalhigh),
4237ee92255SKOSAKI Motohiro 			     nid, K(i.freehigh),
4247ee92255SKOSAKI Motohiro 			     nid, K(i.totalram - i.totalhigh),
4257ee92255SKOSAKI Motohiro 			     nid, K(i.freeram - i.freehigh));
426182e8e23SChristoph Lameter #endif
427948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len,
428c07e02dbSMartin Hicks 			     "Node %d Dirty:          %8lu kB\n"
429c07e02dbSMartin Hicks 			     "Node %d Writeback:      %8lu kB\n"
430347ce434SChristoph Lameter 			     "Node %d FilePages:      %8lu kB\n"
431c07e02dbSMartin Hicks 			     "Node %d Mapped:         %8lu kB\n"
432f3dbd344SChristoph Lameter 			     "Node %d AnonPages:      %8lu kB\n"
4334b02108aSKOSAKI Motohiro 			     "Node %d Shmem:          %8lu kB\n"
434c6a7f572SKOSAKI Motohiro 			     "Node %d KernelStack:    %8lu kB\n"
435628d06a4SSami Tolvanen #ifdef CONFIG_SHADOW_CALL_STACK
436628d06a4SSami Tolvanen 			     "Node %d ShadowCallStack:%8lu kB\n"
437628d06a4SSami Tolvanen #endif
438df849a15SChristoph Lameter 			     "Node %d PageTables:     %8lu kB\n"
439ebc97a52SYosry Ahmed 			     "Node %d SecPageTables:  %8lu kB\n"
440f5ef68daSAndrew Morton 			     "Node %d NFS_Unstable:   %8lu kB\n"
441d2c5e30cSChristoph Lameter 			     "Node %d Bounce:         %8lu kB\n"
442fc3ba692SMiklos Szeredi 			     "Node %d WritebackTmp:   %8lu kB\n"
44361f94e18SVlastimil Babka 			     "Node %d KReclaimable:   %8lu kB\n"
444972d1a7bSChristoph Lameter 			     "Node %d Slab:           %8lu kB\n"
445972d1a7bSChristoph Lameter 			     "Node %d SReclaimable:   %8lu kB\n"
44605b258e9SDavid Rientjes 			     "Node %d SUnreclaim:     %8lu kB\n"
44705b258e9SDavid Rientjes #ifdef CONFIG_TRANSPARENT_HUGEPAGE
44805b258e9SDavid Rientjes 			     "Node %d AnonHugePages:  %8lu kB\n"
44965c45377SKirill A. Shutemov 			     "Node %d ShmemHugePages: %8lu kB\n"
45065c45377SKirill A. Shutemov 			     "Node %d ShmemPmdMapped: %8lu kB\n"
45160fbf0abSSong Liu 			     "Node %d FileHugePages:  %8lu kB\n"
45260fbf0abSSong Liu 			     "Node %d FilePmdMapped:  %8lu kB\n"
45305b258e9SDavid Rientjes #endif
454dcdfdd40SKirill A. Shutemov #ifdef CONFIG_UNACCEPTED_MEMORY
455dcdfdd40SKirill A. Shutemov 			     "Node %d Unaccepted:     %8lu kB\n"
456dcdfdd40SKirill A. Shutemov #endif
45705b258e9SDavid Rientjes 			     ,
45811fb9989SMel Gorman 			     nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
45911fb9989SMel Gorman 			     nid, K(node_page_state(pgdat, NR_WRITEBACK)),
46011fb9989SMel Gorman 			     nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
46150658e2eSMel Gorman 			     nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
4624b9d0fabSMel Gorman 			     nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
463cc7452b6SRafael Aquini 			     nid, K(i.sharedram),
464991e7673SShakeel Butt 			     nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
465628d06a4SSami Tolvanen #ifdef CONFIG_SHADOW_CALL_STACK
466991e7673SShakeel Butt 			     nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
467628d06a4SSami Tolvanen #endif
468f0c0c115SShakeel Butt 			     nid, K(node_page_state(pgdat, NR_PAGETABLE)),
469ebc97a52SYosry Ahmed 			     nid, K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
4708d92890bSNeilBrown 			     nid, 0UL,
47175ef7184SMel Gorman 			     nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),
47211fb9989SMel Gorman 			     nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
47361f94e18SVlastimil Babka 			     nid, K(sreclaimable +
47461f94e18SVlastimil Babka 				    node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
47561f94e18SVlastimil Babka 			     nid, K(sreclaimable + sunreclaimable),
47661f94e18SVlastimil Babka 			     nid, K(sreclaimable),
47761f94e18SVlastimil Babka 			     nid, K(sunreclaimable)
47805b258e9SDavid Rientjes #ifdef CONFIG_TRANSPARENT_HUGEPAGE
47961f94e18SVlastimil Babka 			     ,
48069473e5dSMuchun Song 			     nid, K(node_page_state(pgdat, NR_ANON_THPS)),
48157b2847dSMuchun Song 			     nid, K(node_page_state(pgdat, NR_SHMEM_THPS)),
482a1528e21SMuchun Song 			     nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
483bf9eceadSMuchun Song 			     nid, K(node_page_state(pgdat, NR_FILE_THPS)),
484380780e7SMuchun Song 			     nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED))
48505b258e9SDavid Rientjes #endif
486dcdfdd40SKirill A. Shutemov #ifdef CONFIG_UNACCEPTED_MEMORY
487dcdfdd40SKirill A. Shutemov 			     ,
488dcdfdd40SKirill A. Shutemov 			     nid, K(sum_zone_node_page_state(nid, NR_UNACCEPTED))
489dcdfdd40SKirill A. Shutemov #endif
49061f94e18SVlastimil Babka 			    );
4917981593bSJoe Perches 	len += hugetlb_report_node_meminfo(buf, len, nid);
492948b3edbSJoe Perches 	return len;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds #undef K
496948b3edbSJoe Perches static DEVICE_ATTR(meminfo, 0444, node_read_meminfo, NULL);
4971da177e4SLinus Torvalds 
node_read_numastat(struct device * dev,struct device_attribute * attr,char * buf)49810fbcf4cSKay Sievers static ssize_t node_read_numastat(struct device *dev,
49910fbcf4cSKay Sievers 				  struct device_attribute *attr, char *buf)
5001da177e4SLinus Torvalds {
501f19298b9SMel Gorman 	fold_vm_numa_events();
502aa838896SJoe Perches 	return sysfs_emit(buf,
5031da177e4SLinus Torvalds 			  "numa_hit %lu\n"
5041da177e4SLinus Torvalds 			  "numa_miss %lu\n"
5051da177e4SLinus Torvalds 			  "numa_foreign %lu\n"
5061da177e4SLinus Torvalds 			  "interleave_hit %lu\n"
5071da177e4SLinus Torvalds 			  "local_node %lu\n"
5081da177e4SLinus Torvalds 			  "other_node %lu\n",
509f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_HIT),
510f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_MISS),
511f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_FOREIGN),
512f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_INTERLEAVE_HIT),
513f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_LOCAL),
514f19298b9SMel Gorman 			  sum_zone_numa_event_state(dev->id, NUMA_OTHER));
5151da177e4SLinus Torvalds }
516948b3edbSJoe Perches static DEVICE_ATTR(numastat, 0444, node_read_numastat, NULL);
5171da177e4SLinus Torvalds 
node_read_vmstat(struct device * dev,struct device_attribute * attr,char * buf)51810fbcf4cSKay Sievers static ssize_t node_read_vmstat(struct device *dev,
51910fbcf4cSKay Sievers 				struct device_attribute *attr, char *buf)
5202ac39037SMichael Rubin {
5212ac39037SMichael Rubin 	int nid = dev->id;
52275ef7184SMel Gorman 	struct pglist_data *pgdat = NODE_DATA(nid);
523fa25c503SKOSAKI Motohiro 	int i;
524948b3edbSJoe Perches 	int len = 0;
525fa25c503SKOSAKI Motohiro 
526fa25c503SKOSAKI Motohiro 	for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
527948b3edbSJoe Perches 		len += sysfs_emit_at(buf, len, "%s %lu\n",
528948b3edbSJoe Perches 				     zone_stat_name(i),
52975ef7184SMel Gorman 				     sum_zone_node_page_state(nid, i));
53075ef7184SMel Gorman 
5313a321d2aSKemi Wang #ifdef CONFIG_NUMA
532f19298b9SMel Gorman 	fold_vm_numa_events();
533f19298b9SMel Gorman 	for (i = 0; i < NR_VM_NUMA_EVENT_ITEMS; i++)
534948b3edbSJoe Perches 		len += sysfs_emit_at(buf, len, "%s %lu\n",
535948b3edbSJoe Perches 				     numa_stat_name(i),
536f19298b9SMel Gorman 				     sum_zone_numa_event_state(nid, i));
5373a321d2aSKemi Wang 
538948b3edbSJoe Perches #endif
53969473e5dSMuchun Song 	for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
54069473e5dSMuchun Song 		unsigned long pages = node_page_state_pages(pgdat, i);
54169473e5dSMuchun Song 
54269473e5dSMuchun Song 		if (vmstat_item_print_in_thp(i))
54369473e5dSMuchun Song 			pages /= HPAGE_PMD_NR;
54469473e5dSMuchun Song 		len += sysfs_emit_at(buf, len, "%s %lu\n", node_stat_name(i),
54569473e5dSMuchun Song 				     pages);
54669473e5dSMuchun Song 	}
547fa25c503SKOSAKI Motohiro 
548948b3edbSJoe Perches 	return len;
5492ac39037SMichael Rubin }
550948b3edbSJoe Perches static DEVICE_ATTR(vmstat, 0444, node_read_vmstat, NULL);
5512ac39037SMichael Rubin 
node_read_distance(struct device * dev,struct device_attribute * attr,char * buf)55210fbcf4cSKay Sievers static ssize_t node_read_distance(struct device *dev,
55310fbcf4cSKay Sievers 				  struct device_attribute *attr, char *buf)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	int nid = dev->id;
5561da177e4SLinus Torvalds 	int len = 0;
5571da177e4SLinus Torvalds 	int i;
5581da177e4SLinus Torvalds 
55912ee3c0aSDavid Rientjes 	/*
56012ee3c0aSDavid Rientjes 	 * buf is currently PAGE_SIZE in length and each node needs 4 chars
56112ee3c0aSDavid Rientjes 	 * at the most (distance + space or newline).
56212ee3c0aSDavid Rientjes 	 */
56312ee3c0aSDavid Rientjes 	BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
5641da177e4SLinus Torvalds 
565948b3edbSJoe Perches 	for_each_online_node(i) {
566948b3edbSJoe Perches 		len += sysfs_emit_at(buf, len, "%s%d",
567948b3edbSJoe Perches 				     i ? " " : "", node_distance(nid, i));
568948b3edbSJoe Perches 	}
5691da177e4SLinus Torvalds 
570948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len, "\n");
5711da177e4SLinus Torvalds 	return len;
5721da177e4SLinus Torvalds }
573948b3edbSJoe Perches static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
5741da177e4SLinus Torvalds 
5753c9b8aafSTakashi Iwai static struct attribute *node_dev_attrs[] = {
5763c9b8aafSTakashi Iwai 	&dev_attr_meminfo.attr,
5773c9b8aafSTakashi Iwai 	&dev_attr_numastat.attr,
5783c9b8aafSTakashi Iwai 	&dev_attr_distance.attr,
5793c9b8aafSTakashi Iwai 	&dev_attr_vmstat.attr,
5803c9b8aafSTakashi Iwai 	NULL
5813c9b8aafSTakashi Iwai };
58275bd50faSTian Tao 
5835943c0dcSThomas Weißschuh static const struct bin_attribute *node_dev_bin_attrs[] = {
58475bd50faSTian Tao 	&bin_attr_cpumap,
58575bd50faSTian Tao 	&bin_attr_cpulist,
58675bd50faSTian Tao 	NULL
58775bd50faSTian Tao };
58875bd50faSTian Tao 
58975bd50faSTian Tao static const struct attribute_group node_dev_group = {
59075bd50faSTian Tao 	.attrs = node_dev_attrs,
5915943c0dcSThomas Weißschuh 	.bin_attrs_new = node_dev_bin_attrs,
59275bd50faSTian Tao };
59375bd50faSTian Tao 
59475bd50faSTian Tao static const struct attribute_group *node_dev_groups[] = {
59575bd50faSTian Tao 	&node_dev_group,
59650468e43SJarkko Sakkinen #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
59750468e43SJarkko Sakkinen 	&arch_node_dev_group,
59850468e43SJarkko Sakkinen #endif
59944b8f8bfSJiaqi Yan #ifdef CONFIG_MEMORY_FAILURE
60044b8f8bfSJiaqi Yan 	&memory_failure_attr_group,
60144b8f8bfSJiaqi Yan #endif
60275bd50faSTian Tao 	NULL
60375bd50faSTian Tao };
6043c9b8aafSTakashi Iwai 
node_device_release(struct device * dev)6058c7b5b4eSYasuaki Ishimatsu static void node_device_release(struct device *dev)
6068c7b5b4eSYasuaki Ishimatsu {
607b958d4d0SMuchun Song 	kfree(to_node(dev));
6088c7b5b4eSYasuaki Ishimatsu }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
611405ae7d3SRobert P. J. Day  * register_node - Setup a sysfs device for a node.
6121da177e4SLinus Torvalds  * @num - Node number to use when creating the device.
6131da177e4SLinus Torvalds  *
6141da177e4SLinus Torvalds  * Initialize and register the node device.
6151da177e4SLinus Torvalds  */
register_node(struct node * node,int num)616a7be6e5aSDou Liyang static int register_node(struct node *node, int num)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	int error;
6191da177e4SLinus Torvalds 
62010fbcf4cSKay Sievers 	node->dev.id = num;
62110fbcf4cSKay Sievers 	node->dev.bus = &node_subsys;
6228c7b5b4eSYasuaki Ishimatsu 	node->dev.release = node_device_release;
6237ca7ec40SGreg Kroah-Hartman 	node->dev.groups = node_dev_groups;
62410fbcf4cSKay Sievers 	error = device_register(&node->dev);
6251da177e4SLinus Torvalds 
626a4a00b45SMuchun Song 	if (error) {
627c1cc0d51SArvind Yadav 		put_device(&node->dev);
628a4a00b45SMuchun Song 	} else {
6299a305230SLee Schermerhorn 		hugetlb_register_node(node);
630ed4a6d7fSMel Gorman 		compaction_register_node(node);
6311da177e4SLinus Torvalds 	}
632b958d4d0SMuchun Song 
6331da177e4SLinus Torvalds 	return error;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6364b45099bSKeiichiro Tokunaga /**
6374b45099bSKeiichiro Tokunaga  * unregister_node - unregister a node device
6384b45099bSKeiichiro Tokunaga  * @node: node going away
6394b45099bSKeiichiro Tokunaga  *
6404b45099bSKeiichiro Tokunaga  * Unregisters a node device @node.  All the devices on the node must be
6414b45099bSKeiichiro Tokunaga  * unregistered before calling this function.
6424b45099bSKeiichiro Tokunaga  */
unregister_node(struct node * node)6434b45099bSKeiichiro Tokunaga void unregister_node(struct node *node)
6444b45099bSKeiichiro Tokunaga {
645a4a00b45SMuchun Song 	hugetlb_unregister_node(node);
646da63dc84SMiaohe Lin 	compaction_unregister_node(node);
64708d9dbe7SKeith Busch 	node_remove_accesses(node);
648acc02a10SKeith Busch 	node_remove_caches(node);
64910fbcf4cSKay Sievers 	device_unregister(&node->dev);
6504b45099bSKeiichiro Tokunaga }
6514b45099bSKeiichiro Tokunaga 
6528732794bSWen Congyang struct node *node_devices[MAX_NUMNODES];
6530fc44159SYasunori Goto 
65476b67ed9SKAMEZAWA Hiroyuki /*
65576b67ed9SKAMEZAWA Hiroyuki  * register cpu under node
65676b67ed9SKAMEZAWA Hiroyuki  */
register_cpu_under_node(unsigned int cpu,unsigned int nid)65776b67ed9SKAMEZAWA Hiroyuki int register_cpu_under_node(unsigned int cpu, unsigned int nid)
65876b67ed9SKAMEZAWA Hiroyuki {
6591830794aSAlex Chiang 	int ret;
6608a25a2fdSKay Sievers 	struct device *obj;
661f8246f31SAlex Chiang 
662f8246f31SAlex Chiang 	if (!node_online(nid))
663f8246f31SAlex Chiang 		return 0;
664f8246f31SAlex Chiang 
6658a25a2fdSKay Sievers 	obj = get_cpu_device(cpu);
66676b67ed9SKAMEZAWA Hiroyuki 	if (!obj)
66776b67ed9SKAMEZAWA Hiroyuki 		return 0;
668f8246f31SAlex Chiang 
6698732794bSWen Congyang 	ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
67076b67ed9SKAMEZAWA Hiroyuki 				&obj->kobj,
67176b67ed9SKAMEZAWA Hiroyuki 				kobject_name(&obj->kobj));
6721830794aSAlex Chiang 	if (ret)
6731830794aSAlex Chiang 		return ret;
6741830794aSAlex Chiang 
6751830794aSAlex Chiang 	return sysfs_create_link(&obj->kobj,
6768732794bSWen Congyang 				 &node_devices[nid]->dev.kobj,
6778732794bSWen Congyang 				 kobject_name(&node_devices[nid]->dev.kobj));
67876b67ed9SKAMEZAWA Hiroyuki }
67976b67ed9SKAMEZAWA Hiroyuki 
68008d9dbe7SKeith Busch /**
68108d9dbe7SKeith Busch  * register_memory_node_under_compute_node - link memory node to its compute
68208d9dbe7SKeith Busch  *					     node for a given access class.
68358cb346cSMauro Carvalho Chehab  * @mem_nid:	Memory node number
68458cb346cSMauro Carvalho Chehab  * @cpu_nid:	Cpu  node number
68508d9dbe7SKeith Busch  * @access:	Access class to register
68608d9dbe7SKeith Busch  *
68708d9dbe7SKeith Busch  * Description:
68808d9dbe7SKeith Busch  * 	For use with platforms that may have separate memory and compute nodes.
68908d9dbe7SKeith Busch  * 	This function will export node relationships linking which memory
69008d9dbe7SKeith Busch  * 	initiator nodes can access memory targets at a given ranked access
69108d9dbe7SKeith Busch  * 	class.
69208d9dbe7SKeith Busch  */
register_memory_node_under_compute_node(unsigned int mem_nid,unsigned int cpu_nid,enum access_coordinate_class access)69308d9dbe7SKeith Busch int register_memory_node_under_compute_node(unsigned int mem_nid,
69408d9dbe7SKeith Busch 					    unsigned int cpu_nid,
69511270e52SDave Jiang 					    enum access_coordinate_class access)
69608d9dbe7SKeith Busch {
69708d9dbe7SKeith Busch 	struct node *init_node, *targ_node;
69808d9dbe7SKeith Busch 	struct node_access_nodes *initiator, *target;
69908d9dbe7SKeith Busch 	int ret;
70008d9dbe7SKeith Busch 
70108d9dbe7SKeith Busch 	if (!node_online(cpu_nid) || !node_online(mem_nid))
70208d9dbe7SKeith Busch 		return -ENODEV;
70308d9dbe7SKeith Busch 
70408d9dbe7SKeith Busch 	init_node = node_devices[cpu_nid];
70508d9dbe7SKeith Busch 	targ_node = node_devices[mem_nid];
70608d9dbe7SKeith Busch 	initiator = node_init_node_access(init_node, access);
70708d9dbe7SKeith Busch 	target = node_init_node_access(targ_node, access);
70808d9dbe7SKeith Busch 	if (!initiator || !target)
70908d9dbe7SKeith Busch 		return -ENOMEM;
71008d9dbe7SKeith Busch 
71108d9dbe7SKeith Busch 	ret = sysfs_add_link_to_group(&initiator->dev.kobj, "targets",
71208d9dbe7SKeith Busch 				      &targ_node->dev.kobj,
71308d9dbe7SKeith Busch 				      dev_name(&targ_node->dev));
71408d9dbe7SKeith Busch 	if (ret)
71508d9dbe7SKeith Busch 		return ret;
71608d9dbe7SKeith Busch 
71708d9dbe7SKeith Busch 	ret = sysfs_add_link_to_group(&target->dev.kobj, "initiators",
71808d9dbe7SKeith Busch 				      &init_node->dev.kobj,
71908d9dbe7SKeith Busch 				      dev_name(&init_node->dev));
72008d9dbe7SKeith Busch 	if (ret)
72108d9dbe7SKeith Busch 		goto err;
72208d9dbe7SKeith Busch 
72308d9dbe7SKeith Busch 	return 0;
72408d9dbe7SKeith Busch  err:
72508d9dbe7SKeith Busch 	sysfs_remove_link_from_group(&initiator->dev.kobj, "targets",
72608d9dbe7SKeith Busch 				     dev_name(&targ_node->dev));
72708d9dbe7SKeith Busch 	return ret;
72808d9dbe7SKeith Busch }
72908d9dbe7SKeith Busch 
unregister_cpu_under_node(unsigned int cpu,unsigned int nid)73076b67ed9SKAMEZAWA Hiroyuki int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
73176b67ed9SKAMEZAWA Hiroyuki {
7328a25a2fdSKay Sievers 	struct device *obj;
733b9d52dadSAlex Chiang 
734b9d52dadSAlex Chiang 	if (!node_online(nid))
735b9d52dadSAlex Chiang 		return 0;
736b9d52dadSAlex Chiang 
7378a25a2fdSKay Sievers 	obj = get_cpu_device(cpu);
738b9d52dadSAlex Chiang 	if (!obj)
739b9d52dadSAlex Chiang 		return 0;
740b9d52dadSAlex Chiang 
7418732794bSWen Congyang 	sysfs_remove_link(&node_devices[nid]->dev.kobj,
74276b67ed9SKAMEZAWA Hiroyuki 			  kobject_name(&obj->kobj));
7431830794aSAlex Chiang 	sysfs_remove_link(&obj->kobj,
7448732794bSWen Congyang 			  kobject_name(&node_devices[nid]->dev.kobj));
745b9d52dadSAlex Chiang 
74676b67ed9SKAMEZAWA Hiroyuki 	return 0;
74776b67ed9SKAMEZAWA Hiroyuki }
74876b67ed9SKAMEZAWA Hiroyuki 
74950f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
get_nid_for_pfn(unsigned long pfn)750bd721ea7SFabian Frederick static int __ref get_nid_for_pfn(unsigned long pfn)
751c04fc586SGary Hade {
7523a80a7faSMel Gorman #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
7538cdde385SThomas Gleixner 	if (system_state < SYSTEM_RUNNING)
7543a80a7faSMel Gorman 		return early_pfn_to_nid(pfn);
7553a80a7faSMel Gorman #endif
756c04fc586SGary Hade 	return pfn_to_nid(pfn);
757c04fc586SGary Hade }
758c04fc586SGary Hade 
do_register_memory_block_under_node(int nid,struct memory_block * mem_blk,enum meminit_context context)75990c7eaebSLaurent Dufour static void do_register_memory_block_under_node(int nid,
760395f6081SDavid Hildenbrand 						struct memory_block *mem_blk,
761395f6081SDavid Hildenbrand 						enum meminit_context context)
762c04fc586SGary Hade {
763f85086f9SLaurent Dufour 	int ret;
764d84f2f5aSDavid Hildenbrand 
765395f6081SDavid Hildenbrand 	memory_block_add_nid(mem_blk, nid, context);
766d84f2f5aSDavid Hildenbrand 
7678732794bSWen Congyang 	ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
76810fbcf4cSKay Sievers 				       &mem_blk->dev.kobj,
76910fbcf4cSKay Sievers 				       kobject_name(&mem_blk->dev.kobj));
77090c7eaebSLaurent Dufour 	if (ret && ret != -EEXIST)
77190c7eaebSLaurent Dufour 		dev_err_ratelimited(&node_devices[nid]->dev,
77290c7eaebSLaurent Dufour 				    "can't create link to %s in sysfs (%d)\n",
77390c7eaebSLaurent Dufour 				    kobject_name(&mem_blk->dev.kobj), ret);
774dee5d0d5SAlex Chiang 
77590c7eaebSLaurent Dufour 	ret = sysfs_create_link_nowarn(&mem_blk->dev.kobj,
7768732794bSWen Congyang 				&node_devices[nid]->dev.kobj,
7778732794bSWen Congyang 				kobject_name(&node_devices[nid]->dev.kobj));
77890c7eaebSLaurent Dufour 	if (ret && ret != -EEXIST)
77990c7eaebSLaurent Dufour 		dev_err_ratelimited(&mem_blk->dev,
78090c7eaebSLaurent Dufour 				    "can't create link to %s in sysfs (%d)\n",
78190c7eaebSLaurent Dufour 				    kobject_name(&node_devices[nid]->dev.kobj),
78290c7eaebSLaurent Dufour 				    ret);
783c04fc586SGary Hade }
784f85086f9SLaurent Dufour 
785f85086f9SLaurent Dufour /* register memory section under specified node if it spans that node */
register_mem_block_under_node_early(struct memory_block * mem_blk,void * arg)786f85086f9SLaurent Dufour static int register_mem_block_under_node_early(struct memory_block *mem_blk,
787f85086f9SLaurent Dufour 					       void *arg)
788f85086f9SLaurent Dufour {
789f85086f9SLaurent Dufour 	unsigned long memory_block_pfns = memory_block_size_bytes() / PAGE_SIZE;
790f85086f9SLaurent Dufour 	unsigned long start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
791f85086f9SLaurent Dufour 	unsigned long end_pfn = start_pfn + memory_block_pfns - 1;
792f85086f9SLaurent Dufour 	int nid = *(int *)arg;
793f85086f9SLaurent Dufour 	unsigned long pfn;
794f85086f9SLaurent Dufour 
795f85086f9SLaurent Dufour 	for (pfn = start_pfn; pfn <= end_pfn; pfn++) {
796f85086f9SLaurent Dufour 		int page_nid;
797f85086f9SLaurent Dufour 
798f85086f9SLaurent Dufour 		/*
799f85086f9SLaurent Dufour 		 * memory block could have several absent sections from start.
800f85086f9SLaurent Dufour 		 * skip pfn range from absent section
801f85086f9SLaurent Dufour 		 */
802f85086f9SLaurent Dufour 		if (!pfn_in_present_section(pfn)) {
803f85086f9SLaurent Dufour 			pfn = round_down(pfn + PAGES_PER_SECTION,
804f85086f9SLaurent Dufour 					 PAGES_PER_SECTION) - 1;
805f85086f9SLaurent Dufour 			continue;
806f85086f9SLaurent Dufour 		}
807f85086f9SLaurent Dufour 
808f85086f9SLaurent Dufour 		/*
809f85086f9SLaurent Dufour 		 * We need to check if page belongs to nid only at the boot
810f85086f9SLaurent Dufour 		 * case because node's ranges can be interleaved.
811f85086f9SLaurent Dufour 		 */
812f85086f9SLaurent Dufour 		page_nid = get_nid_for_pfn(pfn);
813f85086f9SLaurent Dufour 		if (page_nid < 0)
814f85086f9SLaurent Dufour 			continue;
815f85086f9SLaurent Dufour 		if (page_nid != nid)
816f85086f9SLaurent Dufour 			continue;
817f85086f9SLaurent Dufour 
818395f6081SDavid Hildenbrand 		do_register_memory_block_under_node(nid, mem_blk, MEMINIT_EARLY);
81990c7eaebSLaurent Dufour 		return 0;
820f85086f9SLaurent Dufour 	}
821c04fc586SGary Hade 	/* mem section does not span the specified node */
822c04fc586SGary Hade 	return 0;
823c04fc586SGary Hade }
824c04fc586SGary Hade 
8254c4b7f9bSDavid Hildenbrand /*
826f85086f9SLaurent Dufour  * During hotplug we know that all pages in the memory block belong to the same
827f85086f9SLaurent Dufour  * node.
828f85086f9SLaurent Dufour  */
register_mem_block_under_node_hotplug(struct memory_block * mem_blk,void * arg)829f85086f9SLaurent Dufour static int register_mem_block_under_node_hotplug(struct memory_block *mem_blk,
830f85086f9SLaurent Dufour 						 void *arg)
831f85086f9SLaurent Dufour {
832f85086f9SLaurent Dufour 	int nid = *(int *)arg;
833f85086f9SLaurent Dufour 
834395f6081SDavid Hildenbrand 	do_register_memory_block_under_node(nid, mem_blk, MEMINIT_HOTPLUG);
83590c7eaebSLaurent Dufour 	return 0;
836f85086f9SLaurent Dufour }
837f85086f9SLaurent Dufour 
838f85086f9SLaurent Dufour /*
839d84f2f5aSDavid Hildenbrand  * Unregister a memory block device under the node it spans. Memory blocks
840d84f2f5aSDavid Hildenbrand  * with multiple nodes cannot be offlined and therefore also never be removed.
8414c4b7f9bSDavid Hildenbrand  */
unregister_memory_block_under_nodes(struct memory_block * mem_blk)842a31b264cSDavid Hildenbrand void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
843c04fc586SGary Hade {
844d84f2f5aSDavid Hildenbrand 	if (mem_blk->nid == NUMA_NO_NODE)
845d84f2f5aSDavid Hildenbrand 		return;
846c04fc586SGary Hade 
847d84f2f5aSDavid Hildenbrand 	sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
84810fbcf4cSKay Sievers 			  kobject_name(&mem_blk->dev.kobj));
84910fbcf4cSKay Sievers 	sysfs_remove_link(&mem_blk->dev.kobj,
850d84f2f5aSDavid Hildenbrand 			  kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
851c04fc586SGary Hade }
852c04fc586SGary Hade 
register_memory_blocks_under_node(int nid,unsigned long start_pfn,unsigned long end_pfn,enum meminit_context context)853cc651559SDavid Hildenbrand void register_memory_blocks_under_node(int nid, unsigned long start_pfn,
854cc651559SDavid Hildenbrand 				       unsigned long end_pfn,
855f85086f9SLaurent Dufour 				       enum meminit_context context)
856c04fc586SGary Hade {
857f85086f9SLaurent Dufour 	walk_memory_blocks_func_t func;
858f85086f9SLaurent Dufour 
859f85086f9SLaurent Dufour 	if (context == MEMINIT_HOTPLUG)
860f85086f9SLaurent Dufour 		func = register_mem_block_under_node_hotplug;
861f85086f9SLaurent Dufour 	else
862f85086f9SLaurent Dufour 		func = register_mem_block_under_node_early;
863f85086f9SLaurent Dufour 
86490c7eaebSLaurent Dufour 	walk_memory_blocks(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn - start_pfn),
86590c7eaebSLaurent Dufour 			   (void *)&nid, func);
86690c7eaebSLaurent Dufour 	return;
867c04fc586SGary Hade }
86850f9481eSDavid Hildenbrand #endif /* CONFIG_MEMORY_HOTPLUG */
86939da08cbSLee Schermerhorn 
__register_one_node(int nid)8709037a993SMichal Hocko int __register_one_node(int nid)
8710fc44159SYasunori Goto {
8729037a993SMichal Hocko 	int error;
8739037a993SMichal Hocko 	int cpu;
87448b5928eSGregory Price 	struct node *node;
8750fc44159SYasunori Goto 
87648b5928eSGregory Price 	node = kzalloc(sizeof(struct node), GFP_KERNEL);
87748b5928eSGregory Price 	if (!node)
8788732794bSWen Congyang 		return -ENOMEM;
8798732794bSWen Congyang 
88048b5928eSGregory Price 	INIT_LIST_HEAD(&node->access_list);
88148b5928eSGregory Price 	node_devices[nid] = node;
88248b5928eSGregory Price 
883a7be6e5aSDou Liyang 	error = register_node(node_devices[nid], nid);
88476b67ed9SKAMEZAWA Hiroyuki 
88576b67ed9SKAMEZAWA Hiroyuki 	/* link cpu under this node */
88676b67ed9SKAMEZAWA Hiroyuki 	for_each_present_cpu(cpu) {
88776b67ed9SKAMEZAWA Hiroyuki 		if (cpu_to_node(cpu) == nid)
88876b67ed9SKAMEZAWA Hiroyuki 			register_cpu_under_node(cpu, nid);
88976b67ed9SKAMEZAWA Hiroyuki 	}
890c04fc586SGary Hade 
891acc02a10SKeith Busch 	node_init_caches(nid);
8920fc44159SYasunori Goto 
8930fc44159SYasunori Goto 	return error;
8940fc44159SYasunori Goto }
8950fc44159SYasunori Goto 
unregister_one_node(int nid)8960fc44159SYasunori Goto void unregister_one_node(int nid)
8970fc44159SYasunori Goto {
89892d585efSXishi Qiu 	if (!node_devices[nid])
89992d585efSXishi Qiu 		return;
90092d585efSXishi Qiu 
9018732794bSWen Congyang 	unregister_node(node_devices[nid]);
9028732794bSWen Congyang 	node_devices[nid] = NULL;
9030fc44159SYasunori Goto }
9040fc44159SYasunori Goto 
905bde631a5SLee Schermerhorn /*
906bde631a5SLee Schermerhorn  * node states attributes
907bde631a5SLee Schermerhorn  */
908bde631a5SLee Schermerhorn 
909b15f562fSAndi Kleen struct node_attr {
91010fbcf4cSKay Sievers 	struct device_attribute attr;
911b15f562fSAndi Kleen 	enum node_states state;
912b15f562fSAndi Kleen };
913b15f562fSAndi Kleen 
show_node_state(struct device * dev,struct device_attribute * attr,char * buf)91410fbcf4cSKay Sievers static ssize_t show_node_state(struct device *dev,
91510fbcf4cSKay Sievers 			       struct device_attribute *attr, char *buf)
916bde631a5SLee Schermerhorn {
917b15f562fSAndi Kleen 	struct node_attr *na = container_of(attr, struct node_attr, attr);
918948b3edbSJoe Perches 
919948b3edbSJoe Perches 	return sysfs_emit(buf, "%*pbl\n",
920948b3edbSJoe Perches 			  nodemask_pr_args(&node_states[na->state]));
921bde631a5SLee Schermerhorn }
922bde631a5SLee Schermerhorn 
923b15f562fSAndi Kleen #define _NODE_ATTR(name, state) \
92410fbcf4cSKay Sievers 	{ __ATTR(name, 0444, show_node_state, NULL), state }
925bde631a5SLee Schermerhorn 
926b15f562fSAndi Kleen static struct node_attr node_state_attr[] = {
927fcf07d22SLai Jiangshan 	[N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
928fcf07d22SLai Jiangshan 	[N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
929fcf07d22SLai Jiangshan 	[N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
930bde631a5SLee Schermerhorn #ifdef CONFIG_HIGHMEM
931fcf07d22SLai Jiangshan 	[N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
932bde631a5SLee Schermerhorn #endif
93320b2f52bSLai Jiangshan 	[N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
934fcf07d22SLai Jiangshan 	[N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
935894c26a1SJonathan Cameron 	[N_GENERIC_INITIATOR] = _NODE_ATTR(has_generic_initiator,
936894c26a1SJonathan Cameron 					   N_GENERIC_INITIATOR),
937bde631a5SLee Schermerhorn };
938bde631a5SLee Schermerhorn 
93910fbcf4cSKay Sievers static struct attribute *node_state_attrs[] = {
940fcf07d22SLai Jiangshan 	&node_state_attr[N_POSSIBLE].attr.attr,
941fcf07d22SLai Jiangshan 	&node_state_attr[N_ONLINE].attr.attr,
942fcf07d22SLai Jiangshan 	&node_state_attr[N_NORMAL_MEMORY].attr.attr,
9433701cde6SAndi Kleen #ifdef CONFIG_HIGHMEM
944fcf07d22SLai Jiangshan 	&node_state_attr[N_HIGH_MEMORY].attr.attr,
9453701cde6SAndi Kleen #endif
94620b2f52bSLai Jiangshan 	&node_state_attr[N_MEMORY].attr.attr,
947fcf07d22SLai Jiangshan 	&node_state_attr[N_CPU].attr.attr,
948894c26a1SJonathan Cameron 	&node_state_attr[N_GENERIC_INITIATOR].attr.attr,
9493701cde6SAndi Kleen 	NULL
9503701cde6SAndi Kleen };
951bde631a5SLee Schermerhorn 
9525a576764SRikard Falkeborn static const struct attribute_group memory_root_attr_group = {
95310fbcf4cSKay Sievers 	.attrs = node_state_attrs,
95410fbcf4cSKay Sievers };
95510fbcf4cSKay Sievers 
95610fbcf4cSKay Sievers static const struct attribute_group *cpu_root_attr_groups[] = {
95710fbcf4cSKay Sievers 	&memory_root_attr_group,
95810fbcf4cSKay Sievers 	NULL,
95910fbcf4cSKay Sievers };
96010fbcf4cSKay Sievers 
node_dev_init(void)9612848a28bSDavid Hildenbrand void __init node_dev_init(void)
9621da177e4SLinus Torvalds {
9632848a28bSDavid Hildenbrand 	int ret, i;
964bde631a5SLee Schermerhorn 
9653701cde6SAndi Kleen  	BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
9663701cde6SAndi Kleen  	BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
9673701cde6SAndi Kleen 
96810fbcf4cSKay Sievers 	ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
9692848a28bSDavid Hildenbrand 	if (ret)
9702848a28bSDavid Hildenbrand 		panic("%s() failed to register subsystem: %d\n", __func__, ret);
9712848a28bSDavid Hildenbrand 
972bde631a5SLee Schermerhorn 	/*
9732848a28bSDavid Hildenbrand 	 * Create all node devices, which will properly link the node
9742848a28bSDavid Hildenbrand 	 * to applicable memory block devices and already created cpu devices.
975bde631a5SLee Schermerhorn 	 */
9762848a28bSDavid Hildenbrand 	for_each_online_node(i) {
9772848a28bSDavid Hildenbrand 		ret = register_one_node(i);
9782848a28bSDavid Hildenbrand 		if (ret)
9792848a28bSDavid Hildenbrand 			panic("%s() failed to add node: %d\n", __func__, ret);
9801da177e4SLinus Torvalds 	}
9812848a28bSDavid Hildenbrand }
982