1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/slab.h>
3 #include <linux/lockdep.h>
4 #include <linux/sysfs.h>
5 #include <linux/kobject.h>
6 #include <linux/memory.h>
7 #include <linux/memory-tiers.h>
12 /* hierarchy of memory tiers */
13 struct list_head list;
14 /* list of all memory types part of this tier */
15 struct list_head memory_types;
17 * start value of abstract distance. memory tier maps
18 * an abstract distance range,
19 * adistance_start .. adistance_start + MEMTIER_CHUNK_SIZE
23 /* All the nodes that are part of all the lower memory tiers. */
24 nodemask_t lower_tier_mask;
27 struct demotion_nodes {
31 struct node_memory_type_map {
32 struct memory_dev_type *memtype;
36 static DEFINE_MUTEX(memory_tier_lock);
37 static LIST_HEAD(memory_tiers);
38 static struct node_memory_type_map node_memory_types[MAX_NUMNODES];
39 static struct memory_dev_type *default_dram_type;
41 static struct bus_type memory_tier_subsys = {
42 .name = "memory_tiering",
43 .dev_name = "memory_tier",
46 #ifdef CONFIG_MIGRATION
47 static int top_tier_adistance;
49 * node_demotion[] examples:
53 * Node 0 & 1 are CPU + DRAM nodes, node 2 & 3 are PMEM nodes.
65 * node_demotion[0].preferred = 2
66 * node_demotion[1].preferred = 3
67 * node_demotion[2].preferred = <empty>
68 * node_demotion[3].preferred = <empty>
72 * Node 0 & 1 are CPU + DRAM nodes, node 2 is memory-only DRAM node.
82 * node_demotion[0].preferred = <empty>
83 * node_demotion[1].preferred = <empty>
84 * node_demotion[2].preferred = <empty>
88 * Node 0 is CPU + DRAM nodes, Node 1 is HBM node, node 2 is PMEM node.
100 * node_demotion[0].preferred = 2
101 * node_demotion[1].preferred = 0
102 * node_demotion[2].preferred = <empty>
105 static struct demotion_nodes *node_demotion __read_mostly;
106 #endif /* CONFIG_MIGRATION */
108 static inline struct memory_tier *to_memory_tier(struct device *device)
110 return container_of(device, struct memory_tier, dev);
113 static __always_inline nodemask_t get_memtier_nodemask(struct memory_tier *memtier)
115 nodemask_t nodes = NODE_MASK_NONE;
116 struct memory_dev_type *memtype;
118 list_for_each_entry(memtype, &memtier->memory_types, tier_sibiling)
119 nodes_or(nodes, nodes, memtype->nodes);
124 static void memory_tier_device_release(struct device *dev)
126 struct memory_tier *tier = to_memory_tier(dev);
128 * synchronize_rcu in clear_node_memory_tier makes sure
129 * we don't have rcu access to this memory tier.
134 static ssize_t nodes_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
140 mutex_lock(&memory_tier_lock);
141 nmask = get_memtier_nodemask(to_memory_tier(dev));
142 ret = sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&nmask));
143 mutex_unlock(&memory_tier_lock);
146 static DEVICE_ATTR_RO(nodes);
148 static struct attribute *memtier_dev_attrs[] = {
149 &dev_attr_nodes.attr,
153 static const struct attribute_group memtier_dev_group = {
154 .attrs = memtier_dev_attrs,
157 static const struct attribute_group *memtier_dev_groups[] = {
162 static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memtype)
165 bool found_slot = false;
166 struct memory_tier *memtier, *new_memtier;
167 int adistance = memtype->adistance;
168 unsigned int memtier_adistance_chunk_size = MEMTIER_CHUNK_SIZE;
170 lockdep_assert_held_once(&memory_tier_lock);
172 adistance = round_down(adistance, memtier_adistance_chunk_size);
174 * If the memtype is already part of a memory tier,
177 if (!list_empty(&memtype->tier_sibiling)) {
178 list_for_each_entry(memtier, &memory_tiers, list) {
179 if (adistance == memtier->adistance_start)
183 return ERR_PTR(-EINVAL);
186 list_for_each_entry(memtier, &memory_tiers, list) {
187 if (adistance == memtier->adistance_start) {
189 } else if (adistance < memtier->adistance_start) {
195 new_memtier = kzalloc(sizeof(struct memory_tier), GFP_KERNEL);
197 return ERR_PTR(-ENOMEM);
199 new_memtier->adistance_start = adistance;
200 INIT_LIST_HEAD(&new_memtier->list);
201 INIT_LIST_HEAD(&new_memtier->memory_types);
203 list_add_tail(&new_memtier->list, &memtier->list);
205 list_add_tail(&new_memtier->list, &memory_tiers);
207 new_memtier->dev.id = adistance >> MEMTIER_CHUNK_BITS;
208 new_memtier->dev.bus = &memory_tier_subsys;
209 new_memtier->dev.release = memory_tier_device_release;
210 new_memtier->dev.groups = memtier_dev_groups;
212 ret = device_register(&new_memtier->dev);
214 list_del(&memtier->list);
215 put_device(&memtier->dev);
218 memtier = new_memtier;
221 list_add(&memtype->tier_sibiling, &memtier->memory_types);
225 static struct memory_tier *__node_get_memory_tier(int node)
229 pgdat = NODE_DATA(node);
233 * Since we hold memory_tier_lock, we can avoid
234 * RCU read locks when accessing the details. No
235 * parallel updates are possible here.
237 return rcu_dereference_check(pgdat->memtier,
238 lockdep_is_held(&memory_tier_lock));
241 #ifdef CONFIG_MIGRATION
242 bool node_is_toptier(int node)
246 struct memory_tier *memtier;
248 pgdat = NODE_DATA(node);
253 memtier = rcu_dereference(pgdat->memtier);
258 if (memtier->adistance_start <= top_tier_adistance)
267 void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets)
269 struct memory_tier *memtier;
272 * pg_data_t.memtier updates includes a synchronize_rcu()
273 * which ensures that we either find NULL or a valid memtier
274 * in NODE_DATA. protect the access via rcu_read_lock();
277 memtier = rcu_dereference(pgdat->memtier);
279 *targets = memtier->lower_tier_mask;
281 *targets = NODE_MASK_NONE;
286 * next_demotion_node() - Get the next node in the demotion path
287 * @node: The starting node to lookup the next node
289 * Return: node id for next memory node in the demotion path hierarchy
290 * from @node; NUMA_NO_NODE if @node is terminal. This does not keep
291 * @node online or guarantee that it *continues* to be the next demotion
294 int next_demotion_node(int node)
296 struct demotion_nodes *nd;
302 nd = &node_demotion[node];
305 * node_demotion[] is updated without excluding this
306 * function from running.
308 * Make sure to use RCU over entire code blocks if
309 * node_demotion[] reads need to be consistent.
313 * If there are multiple target nodes, just select one
314 * target node randomly.
316 * In addition, we can also use round-robin to select
317 * target node, but we should introduce another variable
318 * for node_demotion[] to record last selected target node,
319 * that may cause cache ping-pong due to the changing of
320 * last target node. Or introducing per-cpu data to avoid
321 * caching issue, which seems more complicated. So selecting
322 * target node randomly seems better until now.
324 target = node_random(&nd->preferred);
330 static void disable_all_demotion_targets(void)
332 struct memory_tier *memtier;
335 for_each_node_state(node, N_MEMORY) {
336 node_demotion[node].preferred = NODE_MASK_NONE;
338 * We are holding memory_tier_lock, it is safe
339 * to access pgda->memtier.
341 memtier = __node_get_memory_tier(node);
343 memtier->lower_tier_mask = NODE_MASK_NONE;
346 * Ensure that the "disable" is visible across the system.
347 * Readers will see either a combination of before+disable
348 * state or disable+after. They will never see before and
349 * after state together.
355 * Find an automatic demotion target for all memory
356 * nodes. Failing here is OK. It might just indicate
357 * being at the end of a chain.
359 static void establish_demotion_targets(void)
361 struct memory_tier *memtier;
362 struct demotion_nodes *nd;
363 int target = NUMA_NO_NODE, node;
364 int distance, best_distance;
365 nodemask_t tier_nodes, lower_tier;
367 lockdep_assert_held_once(&memory_tier_lock);
369 if (!node_demotion || !IS_ENABLED(CONFIG_MIGRATION))
372 disable_all_demotion_targets();
374 for_each_node_state(node, N_MEMORY) {
376 nd = &node_demotion[node];
378 memtier = __node_get_memory_tier(node);
379 if (!memtier || list_is_last(&memtier->list, &memory_tiers))
382 * Get the lower memtier to find the demotion node list.
384 memtier = list_next_entry(memtier, list);
385 tier_nodes = get_memtier_nodemask(memtier);
387 * find_next_best_node, use 'used' nodemask as a skip list.
388 * Add all memory nodes except the selected memory tier
389 * nodelist to skip list so that we find the best node from the
392 nodes_andnot(tier_nodes, node_states[N_MEMORY], tier_nodes);
395 * Find all the nodes in the memory tier node list of same best distance.
396 * add them to the preferred mask. We randomly select between nodes
397 * in the preferred mask when allocating pages during demotion.
400 target = find_next_best_node(node, &tier_nodes);
401 if (target == NUMA_NO_NODE)
404 distance = node_distance(node, target);
405 if (distance == best_distance || best_distance == -1) {
406 best_distance = distance;
407 node_set(target, nd->preferred);
414 * Promotion is allowed from a memory tier to higher
415 * memory tier only if the memory tier doesn't include
416 * compute. We want to skip promotion from a memory tier,
417 * if any node that is part of the memory tier have CPUs.
418 * Once we detect such a memory tier, we consider that tier
419 * as top tiper from which promotion is not allowed.
421 list_for_each_entry_reverse(memtier, &memory_tiers, list) {
422 tier_nodes = get_memtier_nodemask(memtier);
423 nodes_and(tier_nodes, node_states[N_CPU], tier_nodes);
424 if (!nodes_empty(tier_nodes)) {
426 * abstract distance below the max value of this memtier
427 * is considered toptier.
429 top_tier_adistance = memtier->adistance_start +
430 MEMTIER_CHUNK_SIZE - 1;
435 * Now build the lower_tier mask for each node collecting node mask from
436 * all memory tier below it. This allows us to fallback demotion page
437 * allocation to a set of nodes that is closer the above selected
440 lower_tier = node_states[N_MEMORY];
441 list_for_each_entry(memtier, &memory_tiers, list) {
443 * Keep removing current tier from lower_tier nodes,
444 * This will remove all nodes in current and above
445 * memory tier from the lower_tier mask.
447 tier_nodes = get_memtier_nodemask(memtier);
448 nodes_andnot(lower_tier, lower_tier, tier_nodes);
449 memtier->lower_tier_mask = lower_tier;
454 static inline void disable_all_demotion_targets(void) {}
455 static inline void establish_demotion_targets(void) {}
456 #endif /* CONFIG_MIGRATION */
458 static inline void __init_node_memory_type(int node, struct memory_dev_type *memtype)
460 if (!node_memory_types[node].memtype)
461 node_memory_types[node].memtype = memtype;
463 * for each device getting added in the same NUMA node
464 * with this specific memtype, bump the map count. We
465 * Only take memtype device reference once, so that
466 * changing a node memtype can be done by droping the
467 * only reference count taken here.
470 if (node_memory_types[node].memtype == memtype) {
471 if (!node_memory_types[node].map_count++)
472 kref_get(&memtype->kref);
476 static struct memory_tier *set_node_memory_tier(int node)
478 struct memory_tier *memtier;
479 struct memory_dev_type *memtype;
480 pg_data_t *pgdat = NODE_DATA(node);
483 lockdep_assert_held_once(&memory_tier_lock);
485 if (!node_state(node, N_MEMORY))
486 return ERR_PTR(-EINVAL);
488 __init_node_memory_type(node, default_dram_type);
490 memtype = node_memory_types[node].memtype;
491 node_set(node, memtype->nodes);
492 memtier = find_create_memory_tier(memtype);
493 if (!IS_ERR(memtier))
494 rcu_assign_pointer(pgdat->memtier, memtier);
498 static void destroy_memory_tier(struct memory_tier *memtier)
500 list_del(&memtier->list);
501 device_unregister(&memtier->dev);
504 static bool clear_node_memory_tier(int node)
506 bool cleared = false;
508 struct memory_tier *memtier;
510 pgdat = NODE_DATA(node);
515 * Make sure that anybody looking at NODE_DATA who finds
516 * a valid memtier finds memory_dev_types with nodes still
517 * linked to the memtier. We achieve this by waiting for
518 * rcu read section to finish using synchronize_rcu.
519 * This also enables us to free the destroyed memory tier
520 * with kfree instead of kfree_rcu
522 memtier = __node_get_memory_tier(node);
524 struct memory_dev_type *memtype;
526 rcu_assign_pointer(pgdat->memtier, NULL);
528 memtype = node_memory_types[node].memtype;
529 node_clear(node, memtype->nodes);
530 if (nodes_empty(memtype->nodes)) {
531 list_del_init(&memtype->tier_sibiling);
532 if (list_empty(&memtier->memory_types))
533 destroy_memory_tier(memtier);
540 static void release_memtype(struct kref *kref)
542 struct memory_dev_type *memtype;
544 memtype = container_of(kref, struct memory_dev_type, kref);
548 struct memory_dev_type *alloc_memory_type(int adistance)
550 struct memory_dev_type *memtype;
552 memtype = kmalloc(sizeof(*memtype), GFP_KERNEL);
554 return ERR_PTR(-ENOMEM);
556 memtype->adistance = adistance;
557 INIT_LIST_HEAD(&memtype->tier_sibiling);
558 memtype->nodes = NODE_MASK_NONE;
559 kref_init(&memtype->kref);
562 EXPORT_SYMBOL_GPL(alloc_memory_type);
564 void destroy_memory_type(struct memory_dev_type *memtype)
566 kref_put(&memtype->kref, release_memtype);
568 EXPORT_SYMBOL_GPL(destroy_memory_type);
570 void init_node_memory_type(int node, struct memory_dev_type *memtype)
573 mutex_lock(&memory_tier_lock);
574 __init_node_memory_type(node, memtype);
575 mutex_unlock(&memory_tier_lock);
577 EXPORT_SYMBOL_GPL(init_node_memory_type);
579 void clear_node_memory_type(int node, struct memory_dev_type *memtype)
581 mutex_lock(&memory_tier_lock);
582 if (node_memory_types[node].memtype == memtype)
583 node_memory_types[node].map_count--;
585 * If we umapped all the attached devices to this node,
586 * clear the node memory type.
588 if (!node_memory_types[node].map_count) {
589 node_memory_types[node].memtype = NULL;
590 kref_put(&memtype->kref, release_memtype);
592 mutex_unlock(&memory_tier_lock);
594 EXPORT_SYMBOL_GPL(clear_node_memory_type);
596 static int __meminit memtier_hotplug_callback(struct notifier_block *self,
597 unsigned long action, void *_arg)
599 struct memory_tier *memtier;
600 struct memory_notify *arg = _arg;
603 * Only update the node migration order when a node is
604 * changing status, like online->offline.
606 if (arg->status_change_nid < 0)
607 return notifier_from_errno(0);
611 mutex_lock(&memory_tier_lock);
612 if (clear_node_memory_tier(arg->status_change_nid))
613 establish_demotion_targets();
614 mutex_unlock(&memory_tier_lock);
617 mutex_lock(&memory_tier_lock);
618 memtier = set_node_memory_tier(arg->status_change_nid);
619 if (!IS_ERR(memtier))
620 establish_demotion_targets();
621 mutex_unlock(&memory_tier_lock);
625 return notifier_from_errno(0);
628 static int __init memory_tier_init(void)
631 struct memory_tier *memtier;
633 ret = subsys_virtual_register(&memory_tier_subsys, NULL);
635 panic("%s() failed to register memory tier subsystem\n", __func__);
637 #ifdef CONFIG_MIGRATION
638 node_demotion = kcalloc(nr_node_ids, sizeof(struct demotion_nodes),
640 WARN_ON(!node_demotion);
642 mutex_lock(&memory_tier_lock);
644 * For now we can have 4 faster memory tiers with smaller adistance
645 * than default DRAM tier.
647 default_dram_type = alloc_memory_type(MEMTIER_ADISTANCE_DRAM);
648 if (!default_dram_type)
649 panic("%s() failed to allocate default DRAM tier\n", __func__);
652 * Look at all the existing N_MEMORY nodes and add them to
653 * default memory tier or to a tier if we already have memory
656 for_each_node_state(node, N_MEMORY) {
657 memtier = set_node_memory_tier(node);
660 * Continue with memtiers we are able to setup
664 establish_demotion_targets();
665 mutex_unlock(&memory_tier_lock);
667 hotplug_memory_notifier(memtier_hotplug_callback, MEMTIER_HOTPLUG_PRIO);
670 subsys_initcall(memory_tier_init);
672 bool numa_demotion_enabled = false;
674 #ifdef CONFIG_MIGRATION
676 static ssize_t numa_demotion_enabled_show(struct kobject *kobj,
677 struct kobj_attribute *attr, char *buf)
679 return sysfs_emit(buf, "%s\n",
680 numa_demotion_enabled ? "true" : "false");
683 static ssize_t numa_demotion_enabled_store(struct kobject *kobj,
684 struct kobj_attribute *attr,
685 const char *buf, size_t count)
689 ret = kstrtobool(buf, &numa_demotion_enabled);
696 static struct kobj_attribute numa_demotion_enabled_attr =
697 __ATTR(demotion_enabled, 0644, numa_demotion_enabled_show,
698 numa_demotion_enabled_store);
700 static struct attribute *numa_attrs[] = {
701 &numa_demotion_enabled_attr.attr,
705 static const struct attribute_group numa_attr_group = {
709 static int __init numa_init_sysfs(void)
712 struct kobject *numa_kobj;
714 numa_kobj = kobject_create_and_add("numa", mm_kobj);
716 pr_err("failed to create numa kobject\n");
719 err = sysfs_create_group(numa_kobj, &numa_attr_group);
721 pr_err("failed to register numa group\n");
727 kobject_put(numa_kobj);
730 subsys_initcall(numa_init_sysfs);
731 #endif /* CONFIG_SYSFS */