Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
3947be19 DH |
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 | ||
3947be19 DH |
19 | #include <linux/node.h> |
20 | #include <linux/compiler.h> | |
da19cbcf | 21 | #include <linux/mutex.h> |
3947be19 | 22 | |
05cf9639 | 23 | #define MIN_MEMORY_BLOCK_SIZE (1UL << SECTION_SIZE_BITS) |
a63fdc51 | 24 | |
028fc57a DH |
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 | * @blocks: List of all memory blocks belonging to this memory group. | |
836809ec DH |
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. | |
028fc57a DH |
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; | |
836809ec DH |
54 | unsigned long present_kernel_pages; |
55 | unsigned long present_movable_pages; | |
028fc57a DH |
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 | ||
3947be19 | 67 | struct memory_block { |
d3360164 | 68 | unsigned long start_section_nr; |
fa2be40f | 69 | unsigned long state; /* serialized by the dev->lock */ |
fa2be40f | 70 | int online_type; /* for passing data to online routine */ |
d0dc12e8 | 71 | int nid; /* NID for this memory block */ |
395f6081 DH |
72 | /* |
73 | * The single zone of this memory block if all PFNs of this memory block | |
74 | * that are System RAM (not a memory hole, not ZONE_DEVICE ranges) are | |
75 | * managed by a single zone. NULL if multiple zones (including nodes) | |
76 | * apply. | |
77 | */ | |
78 | struct zone *zone; | |
e9a2e48e | 79 | struct device dev; |
a08a2ae3 OS |
80 | /* |
81 | * Number of vmemmap pages. These pages | |
82 | * lay at the beginning of the memory block. | |
83 | */ | |
84 | unsigned long nr_vmemmap_pages; | |
028fc57a DH |
85 | struct memory_group *group; /* group (if any) for this block */ |
86 | struct list_head group_next; /* next block inside memory group */ | |
5033091d NH |
87 | #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG) |
88 | atomic_long_t nr_hwpoison; | |
89 | #endif | |
3947be19 DH |
90 | }; |
91 | ||
bc32df00 | 92 | int arch_get_memory_phys_device(unsigned long start_pfn); |
e0a8400c | 93 | unsigned long memory_block_size_bytes(void); |
f642fb58 | 94 | int set_memory_block_size_order(unsigned int order); |
bc32df00 | 95 | |
3947be19 DH |
96 | /* These states are exposed to userspace as text strings in sysfs */ |
97 | #define MEM_ONLINE (1<<0) /* exposed to userspace */ | |
98 | #define MEM_GOING_OFFLINE (1<<1) /* exposed to userspace */ | |
99 | #define MEM_OFFLINE (1<<2) /* exposed to userspace */ | |
7b78d335 YG |
100 | #define MEM_GOING_ONLINE (1<<3) |
101 | #define MEM_CANCEL_ONLINE (1<<4) | |
102 | #define MEM_CANCEL_OFFLINE (1<<5) | |
3947be19 | 103 | |
7b78d335 YG |
104 | struct memory_notify { |
105 | unsigned long start_pfn; | |
106 | unsigned long nr_pages; | |
d9713679 | 107 | int status_change_nid_normal; |
7b78d335 YG |
108 | int status_change_nid; |
109 | }; | |
3947be19 | 110 | |
8c65b4a6 TS |
111 | struct notifier_block; |
112 | struct mem_section; | |
113 | ||
0c40ba4f ND |
114 | /* |
115 | * Priorities for the hotplug memory callback routines (stored in decreasing | |
116 | * order in the callback chain) | |
117 | */ | |
1eeaa4fd LS |
118 | #define DEFAULT_CALLBACK_PRI 0 |
119 | #define SLAB_CALLBACK_PRI 1 | |
120 | #define HMAT_CALLBACK_PRI 2 | |
121 | #define MM_COMPUTE_BATCH_PRI 10 | |
122 | #define CPUSET_CALLBACK_PRI 10 | |
123 | #define MEMTIER_HOTPLUG_PRI 100 | |
124 | #define KSM_CALLBACK_PRI 100 | |
0c40ba4f | 125 | |
50f9481e | 126 | #ifndef CONFIG_MEMORY_HOTPLUG |
902ce63b | 127 | static inline void memory_dev_init(void) |
3947be19 | 128 | { |
902ce63b | 129 | return; |
3947be19 DH |
130 | } |
131 | static inline int register_memory_notifier(struct notifier_block *nb) | |
132 | { | |
133 | return 0; | |
134 | } | |
135 | static inline void unregister_memory_notifier(struct notifier_block *nb) | |
136 | { | |
137 | } | |
7b78d335 YG |
138 | static inline int memory_notify(unsigned long val, void *v) |
139 | { | |
140 | return 0; | |
141 | } | |
50f9481e DH |
142 | static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri) |
143 | { | |
144 | return 0; | |
145 | } | |
50f9481e | 146 | #else /* CONFIG_MEMORY_HOTPLUG */ |
7b78d335 YG |
147 | extern int register_memory_notifier(struct notifier_block *nb); |
148 | extern void unregister_memory_notifier(struct notifier_block *nb); | |
a08a2ae3 | 149 | int create_memory_block_devices(unsigned long start, unsigned long size, |
028fc57a DH |
150 | unsigned long vmemmap_pages, |
151 | struct memory_group *group); | |
4c4b7f9b | 152 | void remove_memory_block_devices(unsigned long start, unsigned long size); |
902ce63b | 153 | extern void memory_dev_init(void); |
7b78d335 | 154 | extern int memory_notify(unsigned long val, void *v); |
fc1f5e98 | 155 | extern struct memory_block *find_memory_block(unsigned long section_nr); |
ea884641 DH |
156 | typedef int (*walk_memory_blocks_func_t)(struct memory_block *, void *); |
157 | extern int walk_memory_blocks(unsigned long start, unsigned long size, | |
158 | void *arg, walk_memory_blocks_func_t func); | |
2c91f8fc | 159 | extern int for_each_memory_block(void *arg, walk_memory_blocks_func_t func); |
028fc57a DH |
160 | |
161 | extern int memory_group_register_static(int nid, unsigned long max_pages); | |
162 | extern int memory_group_register_dynamic(int nid, unsigned long unit_pages); | |
163 | extern int memory_group_unregister(int mgid); | |
164 | struct memory_group *memory_group_find_by_id(int mgid); | |
3fcebf90 DH |
165 | typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *); |
166 | int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, | |
167 | struct memory_group *excluded, void *arg); | |
f02c6968 | 168 | #define hotplug_memory_notifier(fn, pri) ({ \ |
02d0e675 | 169 | static __meminitdata struct notifier_block fn##_mem_nb =\ |
f02c6968 | 170 | { .notifier_call = fn, .priority = pri };\ |
3947be19 | 171 | register_memory_notifier(&fn##_mem_nb); \ |
f02c6968 | 172 | }) |
395f6081 DH |
173 | |
174 | #ifdef CONFIG_NUMA | |
175 | void memory_block_add_nid(struct memory_block *mem, int nid, | |
176 | enum meminit_context context); | |
177 | #endif /* CONFIG_NUMA */ | |
50f9481e | 178 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
3947be19 | 179 | |
0e39ac44 MD |
180 | /* |
181 | * Kernel text modification mutex, used for code patching. Users of this lock | |
182 | * can sleep. | |
183 | */ | |
184 | extern struct mutex text_mutex; | |
185 | ||
3947be19 | 186 | #endif /* _LINUX_MEMORY_H_ */ |