| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * include/linux/memory.h - generic memory definition |
| 4 | * |
| 5 | * This is mainly for topological representation. We define the |
| 6 | * basic "struct memory_block" here, which can be embedded in per-arch |
| 7 | * definitions or NUMA information. |
| 8 | * |
| 9 | * Basic handling of the devices is done in drivers/base/memory.c |
| 10 | * and system devices are handled in drivers/base/sys.c. |
| 11 | * |
| 12 | * Memory block are exported via sysfs in the class/memory/devices/ |
| 13 | * directory. |
| 14 | * |
| 15 | */ |
| 16 | #ifndef _LINUX_MEMORY_H_ |
| 17 | #define _LINUX_MEMORY_H_ |
| 18 | |
| 19 | #include <linux/node.h> |
| 20 | #include <linux/compiler.h> |
| 21 | #include <linux/mutex.h> |
| 22 | |
| 23 | #define MIN_MEMORY_BLOCK_SIZE (1UL << SECTION_SIZE_BITS) |
| 24 | |
| 25 | /** |
| 26 | * struct memory_group - a logical group of memory blocks |
| 27 | * @nid: The node id for all memory blocks inside the memory group. |
| 28 | * @memory_blocks: List of all memory blocks belonging to this memory group. |
| 29 | * @present_kernel_pages: Present (online) memory outside ZONE_MOVABLE of this |
| 30 | * memory group. |
| 31 | * @present_movable_pages: Present (online) memory in ZONE_MOVABLE of this |
| 32 | * memory group. |
| 33 | * @is_dynamic: The memory group type: static vs. dynamic |
| 34 | * @s.max_pages: Valid with &memory_group.is_dynamic == false. The maximum |
| 35 | * number of pages we'll have in this static memory group. |
| 36 | * @d.unit_pages: Valid with &memory_group.is_dynamic == true. Unit in pages |
| 37 | * in which memory is added/removed in this dynamic memory group. |
| 38 | * This granularity defines the alignment of a unit in physical |
| 39 | * address space; it has to be at least as big as a single |
| 40 | * memory block. |
| 41 | * |
| 42 | * A memory group logically groups memory blocks; each memory block |
| 43 | * belongs to at most one memory group. A memory group corresponds to |
| 44 | * a memory device, such as a DIMM or a NUMA node, which spans multiple |
| 45 | * memory blocks and might even span multiple non-contiguous physical memory |
| 46 | * ranges. |
| 47 | * |
| 48 | * Modification of members after registration is serialized by memory |
| 49 | * hot(un)plug code. |
| 50 | */ |
| 51 | struct memory_group { |
| 52 | int nid; |
| 53 | struct list_head memory_blocks; |
| 54 | unsigned long present_kernel_pages; |
| 55 | unsigned long present_movable_pages; |
| 56 | bool is_dynamic; |
| 57 | union { |
| 58 | struct { |
| 59 | unsigned long max_pages; |
| 60 | } s; |
| 61 | struct { |
| 62 | unsigned long unit_pages; |
| 63 | } d; |
| 64 | }; |
| 65 | }; |
| 66 | |
| 67 | enum memory_block_state { |
| 68 | /* These states are exposed to userspace as text strings in sysfs */ |
| 69 | MEM_ONLINE, /* exposed to userspace */ |
| 70 | MEM_GOING_OFFLINE, /* exposed to userspace */ |
| 71 | MEM_OFFLINE, /* exposed to userspace */ |
| 72 | MEM_GOING_ONLINE, |
| 73 | MEM_CANCEL_ONLINE, |
| 74 | MEM_CANCEL_OFFLINE, |
| 75 | }; |
| 76 | |
| 77 | struct memory_block { |
| 78 | unsigned long start_section_nr; |
| 79 | enum memory_block_state state; /* serialized by the dev->lock */ |
| 80 | int online_type; /* for passing data to online routine */ |
| 81 | int nid; /* NID for this memory block */ |
| 82 | /* |
| 83 | * The single zone of this memory block if all PFNs of this memory block |
| 84 | * that are System RAM (not a memory hole, not ZONE_DEVICE ranges) are |
| 85 | * managed by a single zone. NULL if multiple zones (including nodes) |
| 86 | * apply. |
| 87 | */ |
| 88 | struct zone *zone; |
| 89 | struct device dev; |
| 90 | struct vmem_altmap *altmap; |
| 91 | struct memory_group *group; /* group (if any) for this block */ |
| 92 | struct list_head group_next; /* next block inside memory group */ |
| 93 | #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG) |
| 94 | atomic_long_t nr_hwpoison; |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | int arch_get_memory_phys_device(unsigned long start_pfn); |
| 99 | unsigned long memory_block_size_bytes(void); |
| 100 | int set_memory_block_size_order(unsigned int order); |
| 101 | |
| 102 | struct memory_notify { |
| 103 | unsigned long start_pfn; |
| 104 | unsigned long nr_pages; |
| 105 | }; |
| 106 | |
| 107 | struct notifier_block; |
| 108 | struct mem_section; |
| 109 | |
| 110 | /* |
| 111 | * Priorities for the hotplug memory callback routines. Invoked from |
| 112 | * high to low. Higher priorities correspond to higher numbers. |
| 113 | */ |
| 114 | #define DEFAULT_CALLBACK_PRI 0 |
| 115 | #define SLAB_CALLBACK_PRI 1 |
| 116 | #define CXL_CALLBACK_PRI 5 |
| 117 | #define HMAT_CALLBACK_PRI 6 |
| 118 | #define MM_COMPUTE_BATCH_PRI 10 |
| 119 | #define CPUSET_CALLBACK_PRI 10 |
| 120 | #define MEMTIER_HOTPLUG_PRI 100 |
| 121 | #define KSM_CALLBACK_PRI 100 |
| 122 | |
| 123 | #ifndef CONFIG_MEMORY_HOTPLUG |
| 124 | static inline void memory_dev_init(void) |
| 125 | { |
| 126 | return; |
| 127 | } |
| 128 | static inline int register_memory_notifier(struct notifier_block *nb) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | static inline void unregister_memory_notifier(struct notifier_block *nb) |
| 133 | { |
| 134 | } |
| 135 | static inline int memory_notify(enum memory_block_state state, void *v) |
| 136 | { |
| 137 | return 0; |
| 138 | } |
| 139 | static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri) |
| 140 | { |
| 141 | return 0; |
| 142 | } |
| 143 | static inline int memory_block_advise_max_size(unsigned long size) |
| 144 | { |
| 145 | return -ENODEV; |
| 146 | } |
| 147 | static inline unsigned long memory_block_advised_max_size(void) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | #else /* CONFIG_MEMORY_HOTPLUG */ |
| 152 | extern int register_memory_notifier(struct notifier_block *nb); |
| 153 | extern void unregister_memory_notifier(struct notifier_block *nb); |
| 154 | int create_memory_block_devices(unsigned long start, unsigned long size, |
| 155 | int nid, struct vmem_altmap *altmap, |
| 156 | struct memory_group *group); |
| 157 | void remove_memory_block_devices(unsigned long start, unsigned long size); |
| 158 | extern void memory_dev_init(void); |
| 159 | extern int memory_notify(enum memory_block_state state, void *v); |
| 160 | extern struct memory_block *find_memory_block(unsigned long section_nr); |
| 161 | typedef int (*walk_memory_blocks_func_t)(struct memory_block *, void *); |
| 162 | extern int walk_memory_blocks(unsigned long start, unsigned long size, |
| 163 | void *arg, walk_memory_blocks_func_t func); |
| 164 | extern int for_each_memory_block(void *arg, walk_memory_blocks_func_t func); |
| 165 | |
| 166 | extern int memory_group_register_static(int nid, unsigned long max_pages); |
| 167 | extern int memory_group_register_dynamic(int nid, unsigned long unit_pages); |
| 168 | extern int memory_group_unregister(int mgid); |
| 169 | struct memory_group *memory_group_find_by_id(int mgid); |
| 170 | typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *); |
| 171 | int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, |
| 172 | struct memory_group *excluded, void *arg); |
| 173 | struct memory_block *find_memory_block_by_id(unsigned long block_id); |
| 174 | #define hotplug_memory_notifier(fn, pri) ({ \ |
| 175 | static __meminitdata struct notifier_block fn##_mem_nb =\ |
| 176 | { .notifier_call = fn, .priority = pri };\ |
| 177 | register_memory_notifier(&fn##_mem_nb); \ |
| 178 | }) |
| 179 | |
| 180 | extern int sections_per_block; |
| 181 | |
| 182 | static inline unsigned long memory_block_id(unsigned long section_nr) |
| 183 | { |
| 184 | return section_nr / sections_per_block; |
| 185 | } |
| 186 | |
| 187 | static inline unsigned long pfn_to_block_id(unsigned long pfn) |
| 188 | { |
| 189 | return memory_block_id(section_nr: pfn_to_section_nr(pfn)); |
| 190 | } |
| 191 | |
| 192 | static inline unsigned long phys_to_block_id(unsigned long phys) |
| 193 | { |
| 194 | return pfn_to_block_id(PFN_DOWN(phys)); |
| 195 | } |
| 196 | |
| 197 | #ifdef CONFIG_NUMA |
| 198 | void memory_block_add_nid_early(struct memory_block *mem, int nid); |
| 199 | #endif /* CONFIG_NUMA */ |
| 200 | int memory_block_advise_max_size(unsigned long size); |
| 201 | unsigned long memory_block_advised_max_size(void); |
| 202 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 203 | |
| 204 | /* |
| 205 | * Kernel text modification mutex, used for code patching. Users of this lock |
| 206 | * can sleep. |
| 207 | */ |
| 208 | extern struct mutex text_mutex; |
| 209 | |
| 210 | #endif /* _LINUX_MEMORY_H_ */ |
| 211 | |