1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_MEMORY_HOTPLUG_H
3 #define __LINUX_MEMORY_HOTPLUG_H
5 #include <linux/mmzone.h>
6 #include <linux/spinlock.h>
7 #include <linux/notifier.h>
19 #ifdef CONFIG_HAVE_ARCH_NODEDATA_EXTENSION
21 * For supporting node-hotadd, we have to allocate a new pgdat.
23 * If an arch has generic style NODE_DATA(),
24 * node_data[nid] = kzalloc() works well. But it depends on the architecture.
26 * In general, generic_alloc_nodedata() is used.
29 extern pg_data_t *arch_alloc_nodedata(int nid);
30 extern void arch_refresh_nodedata(int nid, pg_data_t *pgdat);
32 #else /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */
34 #define arch_alloc_nodedata(nid) generic_alloc_nodedata(nid)
38 * XXX: node aware allocation can't work well to get new node's memory at this time.
39 * Because, pgdat for the new node is not allocated/initialized yet itself.
40 * To use new node's memory, more consideration will be necessary.
42 #define generic_alloc_nodedata(nid) \
44 memblock_alloc(sizeof(*pgdat), SMP_CACHE_BYTES); \
47 extern pg_data_t *node_data[];
48 static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
50 node_data[nid] = pgdat;
53 #else /* !CONFIG_NUMA */
56 static inline pg_data_t *generic_alloc_nodedata(int nid)
61 static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
64 #endif /* CONFIG_NUMA */
65 #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */
67 #ifdef CONFIG_MEMORY_HOTPLUG
68 struct page *pfn_to_online_page(unsigned long pfn);
70 /* Types for control the zone type of onlined and offlined memory */
72 /* Offline the memory. */
74 /* Online the memory. Zone depends, see default_zone_for_pfn(). */
76 /* Online the memory to ZONE_NORMAL. */
78 /* Online the memory to ZONE_MOVABLE. */
82 /* Flags for add_memory() and friends to specify memory hotplug details. */
83 typedef int __bitwise mhp_t;
85 /* No special request */
86 #define MHP_NONE ((__force mhp_t)0)
88 * Allow merging of the added System RAM resource with adjacent,
89 * mergeable resources. After a successful call to add_memory_resource()
90 * with this flag set, the resource pointer must no longer be used as it
91 * might be stale, or the resource might have changed.
93 #define MHP_MERGE_RESOURCE ((__force mhp_t)BIT(0))
96 * We want memmap (struct page array) to be self contained.
97 * To do so, we will use the beginning of the hot-added range to build
98 * the page tables for the memmap array that describes the entire range.
99 * Only selected architectures support it with SPARSE_VMEMMAP.
100 * This is only a hint, the core kernel can decide to not do this based on
101 * different alignment checks.
103 #define MHP_MEMMAP_ON_MEMORY ((__force mhp_t)BIT(1))
105 * The nid field specifies a memory group id (mgid) instead. The memory group
106 * implies the node id (nid).
108 #define MHP_NID_IS_MGID ((__force mhp_t)BIT(2))
111 * Extended parameters for memory hotplug:
112 * altmap: alternative allocator for memmap array (optional)
113 * pgprot: page protection flags to apply to newly created page tables
117 struct vmem_altmap *altmap;
119 struct dev_pagemap *pgmap;
122 bool mhp_range_allowed(u64 start, u64 size, bool need_mapping);
123 struct range mhp_get_pluggable_range(bool need_mapping);
126 * Zone resizing functions
128 * Note: any attempt to resize a zone should has pgdat_resize_lock()
129 * zone_span_writelock() both held. This ensure the size of a zone
130 * can't be changed while pgdat_resize_lock() held.
132 static inline unsigned zone_span_seqbegin(struct zone *zone)
134 return read_seqbegin(&zone->span_seqlock);
136 static inline int zone_span_seqretry(struct zone *zone, unsigned iv)
138 return read_seqretry(&zone->span_seqlock, iv);
140 static inline void zone_span_writelock(struct zone *zone)
142 write_seqlock(&zone->span_seqlock);
144 static inline void zone_span_writeunlock(struct zone *zone)
146 write_sequnlock(&zone->span_seqlock);
148 static inline void zone_seqlock_init(struct zone *zone)
150 seqlock_init(&zone->span_seqlock);
152 extern void adjust_present_page_count(struct page *page,
153 struct memory_group *group,
155 /* VM interface that may be used by firmware interface */
156 extern int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
158 extern void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages);
159 extern int online_pages(unsigned long pfn, unsigned long nr_pages,
160 struct zone *zone, struct memory_group *group);
161 extern void __offline_isolated_pages(unsigned long start_pfn,
162 unsigned long end_pfn);
164 typedef void (*online_page_callback_t)(struct page *page, unsigned int order);
166 extern void generic_online_page(struct page *page, unsigned int order);
167 extern int set_online_page_callback(online_page_callback_t callback);
168 extern int restore_online_page_callback(online_page_callback_t callback);
170 extern int try_online_node(int nid);
172 extern int arch_add_memory(int nid, u64 start, u64 size,
173 struct mhp_params *params);
174 extern u64 max_mem_size;
176 extern int mhp_online_type_from_str(const char *str);
178 /* Default online_type (MMOP_*) when new memory blocks are added. */
179 extern int mhp_default_online_type;
180 /* If movable_node boot option specified */
181 extern bool movable_node_enabled;
182 static inline bool movable_node_is_enabled(void)
184 return movable_node_enabled;
187 extern void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap);
188 extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,
189 struct vmem_altmap *altmap);
191 /* reasonably generic interface to expand the physical pages */
192 extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
193 struct mhp_params *params);
195 #ifndef CONFIG_ARCH_HAS_ADD_PAGES
196 static inline int add_pages(int nid, unsigned long start_pfn,
197 unsigned long nr_pages, struct mhp_params *params)
199 return __add_pages(nid, start_pfn, nr_pages, params);
201 #else /* ARCH_HAS_ADD_PAGES */
202 int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
203 struct mhp_params *params);
204 #endif /* ARCH_HAS_ADD_PAGES */
206 void get_online_mems(void);
207 void put_online_mems(void);
209 void mem_hotplug_begin(void);
210 void mem_hotplug_done(void);
212 /* See kswapd_is_running() */
213 static inline void pgdat_kswapd_lock(pg_data_t *pgdat)
215 mutex_lock(&pgdat->kswapd_lock);
218 static inline void pgdat_kswapd_unlock(pg_data_t *pgdat)
220 mutex_unlock(&pgdat->kswapd_lock);
223 static inline void pgdat_kswapd_lock_init(pg_data_t *pgdat)
225 mutex_init(&pgdat->kswapd_lock);
228 #else /* ! CONFIG_MEMORY_HOTPLUG */
229 #define pfn_to_online_page(pfn) \
231 struct page *___page = NULL; \
232 if (pfn_valid(pfn)) \
233 ___page = pfn_to_page(pfn); \
237 static inline unsigned zone_span_seqbegin(struct zone *zone)
241 static inline int zone_span_seqretry(struct zone *zone, unsigned iv)
245 static inline void zone_span_writelock(struct zone *zone) {}
246 static inline void zone_span_writeunlock(struct zone *zone) {}
247 static inline void zone_seqlock_init(struct zone *zone) {}
249 static inline int try_online_node(int nid)
254 static inline void get_online_mems(void) {}
255 static inline void put_online_mems(void) {}
257 static inline void mem_hotplug_begin(void) {}
258 static inline void mem_hotplug_done(void) {}
260 static inline bool movable_node_is_enabled(void)
265 static inline void pgdat_kswapd_lock(pg_data_t *pgdat) {}
266 static inline void pgdat_kswapd_unlock(pg_data_t *pgdat) {}
267 static inline void pgdat_kswapd_lock_init(pg_data_t *pgdat) {}
268 #endif /* ! CONFIG_MEMORY_HOTPLUG */
271 * Keep this declaration outside CONFIG_MEMORY_HOTPLUG as some
272 * platforms might override and use arch_get_mappable_range()
273 * for internal non memory hotplug purposes.
275 struct range arch_get_mappable_range(void);
277 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
279 * pgdat resizing functions
282 void pgdat_resize_lock(struct pglist_data *pgdat, unsigned long *flags)
284 spin_lock_irqsave(&pgdat->node_size_lock, *flags);
287 void pgdat_resize_unlock(struct pglist_data *pgdat, unsigned long *flags)
289 spin_unlock_irqrestore(&pgdat->node_size_lock, *flags);
292 void pgdat_resize_init(struct pglist_data *pgdat)
294 spin_lock_init(&pgdat->node_size_lock);
296 #else /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */
298 * Stub functions for when hotplug is off
300 static inline void pgdat_resize_lock(struct pglist_data *p, unsigned long *f) {}
301 static inline void pgdat_resize_unlock(struct pglist_data *p, unsigned long *f) {}
302 static inline void pgdat_resize_init(struct pglist_data *pgdat) {}
303 #endif /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */
305 #ifdef CONFIG_MEMORY_HOTREMOVE
307 extern void try_offline_node(int nid);
308 extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
309 struct zone *zone, struct memory_group *group);
310 extern int remove_memory(u64 start, u64 size);
311 extern void __remove_memory(u64 start, u64 size);
312 extern int offline_and_remove_memory(u64 start, u64 size);
315 static inline void try_offline_node(int nid) {}
317 static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
318 struct zone *zone, struct memory_group *group)
323 static inline int remove_memory(u64 start, u64 size)
328 static inline void __remove_memory(u64 start, u64 size) {}
329 #endif /* CONFIG_MEMORY_HOTREMOVE */
331 #ifdef CONFIG_MEMORY_HOTPLUG
332 extern void __ref free_area_init_core_hotplug(struct pglist_data *pgdat);
333 extern int __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags);
334 extern int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags);
335 extern int add_memory_resource(int nid, struct resource *resource,
337 extern int add_memory_driver_managed(int nid, u64 start, u64 size,
338 const char *resource_name,
340 extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
341 unsigned long nr_pages,
342 struct vmem_altmap *altmap, int migratetype);
343 extern void remove_pfn_range_from_zone(struct zone *zone,
344 unsigned long start_pfn,
345 unsigned long nr_pages);
346 extern int sparse_add_section(int nid, unsigned long pfn,
347 unsigned long nr_pages, struct vmem_altmap *altmap,
348 struct dev_pagemap *pgmap);
349 extern void sparse_remove_section(unsigned long pfn, unsigned long nr_pages,
350 struct vmem_altmap *altmap);
351 extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map,
353 extern struct zone *zone_for_pfn_range(int online_type, int nid,
354 struct memory_group *group, unsigned long start_pfn,
355 unsigned long nr_pages);
356 extern int arch_create_linear_mapping(int nid, u64 start, u64 size,
357 struct mhp_params *params);
358 void arch_remove_linear_mapping(u64 start, u64 size);
359 #endif /* CONFIG_MEMORY_HOTPLUG */
361 #endif /* __LINUX_MEMORY_HOTPLUG_H */