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