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 #include <linux/notifier.h>
23
24 #define MIN_MEMORY_BLOCK_SIZE (1UL << SECTION_SIZE_BITS)
25
26 /**
27 * struct memory_group - a logical group of memory blocks
28 * @nid: The node id for all memory blocks inside the memory group.
29 * @blocks: List of all memory blocks belonging to this memory group.
30 * @present_kernel_pages: Present (online) memory outside ZONE_MOVABLE of this
31 * memory group.
32 * @present_movable_pages: Present (online) memory in ZONE_MOVABLE of this
33 * memory group.
34 * @is_dynamic: The memory group type: static vs. dynamic
35 * @s.max_pages: Valid with &memory_group.is_dynamic == false. The maximum
36 * number of pages we'll have in this static memory group.
37 * @d.unit_pages: Valid with &memory_group.is_dynamic == true. Unit in pages
38 * in which memory is added/removed in this dynamic memory group.
39 * This granularity defines the alignment of a unit in physical
40 * address space; it has to be at least as big as a single
41 * memory block.
42 *
43 * A memory group logically groups memory blocks; each memory block
44 * belongs to at most one memory group. A memory group corresponds to
45 * a memory device, such as a DIMM or a NUMA node, which spans multiple
46 * memory blocks and might even span multiple non-contiguous physical memory
47 * ranges.
48 *
49 * Modification of members after registration is serialized by memory
50 * hot(un)plug code.
51 */
52 struct memory_group {
53 int nid;
54 struct list_head memory_blocks;
55 unsigned long present_kernel_pages;
56 unsigned long present_movable_pages;
57 bool is_dynamic;
58 union {
59 struct {
60 unsigned long max_pages;
61 } s;
62 struct {
63 unsigned long unit_pages;
64 } d;
65 };
66 };
67
68 struct memory_block {
69 unsigned long start_section_nr;
70 unsigned long state; /* serialized by the dev->lock */
71 int online_type; /* for passing data to online routine */
72 int nid; /* NID for this memory block */
73 struct device dev;
74 /*
75 * Number of vmemmap pages. These pages
76 * lay at the beginning of the memory block.
77 */
78 unsigned long nr_vmemmap_pages;
79 struct memory_group *group; /* group (if any) for this block */
80 struct list_head group_next; /* next block inside memory group */
81 };
82
83 int arch_get_memory_phys_device(unsigned long start_pfn);
84 unsigned long memory_block_size_bytes(void);
85 int set_memory_block_size_order(unsigned int order);
86
87 /* These states are exposed to userspace as text strings in sysfs */
88 #define MEM_ONLINE (1<<0) /* exposed to userspace */
89 #define MEM_GOING_OFFLINE (1<<1) /* exposed to userspace */
90 #define MEM_OFFLINE (1<<2) /* exposed to userspace */
91 #define MEM_GOING_ONLINE (1<<3)
92 #define MEM_CANCEL_ONLINE (1<<4)
93 #define MEM_CANCEL_OFFLINE (1<<5)
94
95 struct memory_notify {
96 unsigned long start_pfn;
97 unsigned long nr_pages;
98 int status_change_nid_normal;
99 int status_change_nid;
100 };
101
102 struct notifier_block;
103 struct mem_section;
104
105 /*
106 * Priorities for the hotplug memory callback routines (stored in decreasing
107 * order in the callback chain)
108 */
109 #define SLAB_CALLBACK_PRI 1
110 #define IPC_CALLBACK_PRI 10
111
112 #ifndef CONFIG_MEMORY_HOTPLUG
memory_dev_init(void)113 static inline void memory_dev_init(void)
114 {
115 return;
116 }
register_memory_notifier(struct notifier_block * nb)117 static inline int register_memory_notifier(struct notifier_block *nb)
118 {
119 return 0;
120 }
unregister_memory_notifier(struct notifier_block * nb)121 static inline void unregister_memory_notifier(struct notifier_block *nb)
122 {
123 }
memory_notify(unsigned long val,void * v)124 static inline int memory_notify(unsigned long val, void *v)
125 {
126 return 0;
127 }
hotplug_memory_notifier(notifier_fn_t fn,int pri)128 static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri)
129 {
130 return 0;
131 }
132 /* These aren't inline functions due to a GCC bug. */
133 #define register_hotmemory_notifier(nb) ({ (void)(nb); 0; })
134 #define unregister_hotmemory_notifier(nb) ({ (void)(nb); })
135 #else /* CONFIG_MEMORY_HOTPLUG */
136 extern int register_memory_notifier(struct notifier_block *nb);
137 extern void unregister_memory_notifier(struct notifier_block *nb);
138 int create_memory_block_devices(unsigned long start, unsigned long size,
139 unsigned long vmemmap_pages,
140 struct memory_group *group);
141 void remove_memory_block_devices(unsigned long start, unsigned long size);
142 extern void memory_dev_init(void);
143 extern int memory_notify(unsigned long val, void *v);
144 extern struct memory_block *find_memory_block(unsigned long section_nr);
145 typedef int (*walk_memory_blocks_func_t)(struct memory_block *, void *);
146 extern int walk_memory_blocks(unsigned long start, unsigned long size,
147 void *arg, walk_memory_blocks_func_t func);
148 extern int for_each_memory_block(void *arg, walk_memory_blocks_func_t func);
149
150 extern int memory_group_register_static(int nid, unsigned long max_pages);
151 extern int memory_group_register_dynamic(int nid, unsigned long unit_pages);
152 extern int memory_group_unregister(int mgid);
153 struct memory_group *memory_group_find_by_id(int mgid);
154 typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *);
155 int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
156 struct memory_group *excluded, void *arg);
157 #define hotplug_memory_notifier(fn, pri) ({ \
158 static __meminitdata struct notifier_block fn##_mem_nb =\
159 { .notifier_call = fn, .priority = pri };\
160 register_memory_notifier(&fn##_mem_nb); \
161 })
162 #define register_hotmemory_notifier(nb) register_memory_notifier(nb)
163 #define unregister_hotmemory_notifier(nb) unregister_memory_notifier(nb)
164 #endif /* CONFIG_MEMORY_HOTPLUG */
165
166 /*
167 * Kernel text modification mutex, used for code patching. Users of this lock
168 * can sleep.
169 */
170 extern struct mutex text_mutex;
171
172 #endif /* _LINUX_MEMORY_H_ */
173