Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * include/linux/memory.h - generic memory definition
0004  *
0005  * This is mainly for topological representation. We define the
0006  * basic "struct memory_block" here, which can be embedded in per-arch
0007  * definitions or NUMA information.
0008  *
0009  * Basic handling of the devices is done in drivers/base/memory.c
0010  * and system devices are handled in drivers/base/sys.c.
0011  *
0012  * Memory block are exported via sysfs in the class/memory/devices/
0013  * directory.
0014  *
0015  */
0016 #ifndef _LINUX_MEMORY_H_
0017 #define _LINUX_MEMORY_H_
0018 
0019 #include <linux/node.h>
0020 #include <linux/compiler.h>
0021 #include <linux/mutex.h>
0022 #include <linux/notifier.h>
0023 
0024 #define MIN_MEMORY_BLOCK_SIZE     (1UL << SECTION_SIZE_BITS)
0025 
0026 /**
0027  * struct memory_group - a logical group of memory blocks
0028  * @nid: The node id for all memory blocks inside the memory group.
0029  * @blocks: List of all memory blocks belonging to this memory group.
0030  * @present_kernel_pages: Present (online) memory outside ZONE_MOVABLE of this
0031  *            memory group.
0032  * @present_movable_pages: Present (online) memory in ZONE_MOVABLE of this
0033  *             memory group.
0034  * @is_dynamic: The memory group type: static vs. dynamic
0035  * @s.max_pages: Valid with &memory_group.is_dynamic == false. The maximum
0036  *       number of pages we'll have in this static memory group.
0037  * @d.unit_pages: Valid with &memory_group.is_dynamic == true. Unit in pages
0038  *        in which memory is added/removed in this dynamic memory group.
0039  *        This granularity defines the alignment of a unit in physical
0040  *        address space; it has to be at least as big as a single
0041  *        memory block.
0042  *
0043  * A memory group logically groups memory blocks; each memory block
0044  * belongs to at most one memory group. A memory group corresponds to
0045  * a memory device, such as a DIMM or a NUMA node, which spans multiple
0046  * memory blocks and might even span multiple non-contiguous physical memory
0047  * ranges.
0048  *
0049  * Modification of members after registration is serialized by memory
0050  * hot(un)plug code.
0051  */
0052 struct memory_group {
0053     int nid;
0054     struct list_head memory_blocks;
0055     unsigned long present_kernel_pages;
0056     unsigned long present_movable_pages;
0057     bool is_dynamic;
0058     union {
0059         struct {
0060             unsigned long max_pages;
0061         } s;
0062         struct {
0063             unsigned long unit_pages;
0064         } d;
0065     };
0066 };
0067 
0068 struct memory_block {
0069     unsigned long start_section_nr;
0070     unsigned long state;        /* serialized by the dev->lock */
0071     int online_type;        /* for passing data to online routine */
0072     int nid;            /* NID for this memory block */
0073     /*
0074      * The single zone of this memory block if all PFNs of this memory block
0075      * that are System RAM (not a memory hole, not ZONE_DEVICE ranges) are
0076      * managed by a single zone. NULL if multiple zones (including nodes)
0077      * apply.
0078      */
0079     struct zone *zone;
0080     struct device dev;
0081     /*
0082      * Number of vmemmap pages. These pages
0083      * lay at the beginning of the memory block.
0084      */
0085     unsigned long nr_vmemmap_pages;
0086     struct memory_group *group; /* group (if any) for this block */
0087     struct list_head group_next;    /* next block inside memory group */
0088 };
0089 
0090 int arch_get_memory_phys_device(unsigned long start_pfn);
0091 unsigned long memory_block_size_bytes(void);
0092 int set_memory_block_size_order(unsigned int order);
0093 
0094 /* These states are exposed to userspace as text strings in sysfs */
0095 #define MEM_ONLINE      (1<<0) /* exposed to userspace */
0096 #define MEM_GOING_OFFLINE   (1<<1) /* exposed to userspace */
0097 #define MEM_OFFLINE     (1<<2) /* exposed to userspace */
0098 #define MEM_GOING_ONLINE    (1<<3)
0099 #define MEM_CANCEL_ONLINE   (1<<4)
0100 #define MEM_CANCEL_OFFLINE  (1<<5)
0101 
0102 struct memory_notify {
0103     unsigned long start_pfn;
0104     unsigned long nr_pages;
0105     int status_change_nid_normal;
0106     int status_change_nid;
0107 };
0108 
0109 struct notifier_block;
0110 struct mem_section;
0111 
0112 /*
0113  * Priorities for the hotplug memory callback routines (stored in decreasing
0114  * order in the callback chain)
0115  */
0116 #define SLAB_CALLBACK_PRI       1
0117 #define IPC_CALLBACK_PRI        10
0118 
0119 #ifndef CONFIG_MEMORY_HOTPLUG
0120 static inline void memory_dev_init(void)
0121 {
0122     return;
0123 }
0124 static inline int register_memory_notifier(struct notifier_block *nb)
0125 {
0126     return 0;
0127 }
0128 static inline void unregister_memory_notifier(struct notifier_block *nb)
0129 {
0130 }
0131 static inline int memory_notify(unsigned long val, void *v)
0132 {
0133     return 0;
0134 }
0135 static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri)
0136 {
0137     return 0;
0138 }
0139 /* These aren't inline functions due to a GCC bug. */
0140 #define register_hotmemory_notifier(nb)    ({ (void)(nb); 0; })
0141 #define unregister_hotmemory_notifier(nb)  ({ (void)(nb); })
0142 #else /* CONFIG_MEMORY_HOTPLUG */
0143 extern int register_memory_notifier(struct notifier_block *nb);
0144 extern void unregister_memory_notifier(struct notifier_block *nb);
0145 int create_memory_block_devices(unsigned long start, unsigned long size,
0146                 unsigned long vmemmap_pages,
0147                 struct memory_group *group);
0148 void remove_memory_block_devices(unsigned long start, unsigned long size);
0149 extern void memory_dev_init(void);
0150 extern int memory_notify(unsigned long val, void *v);
0151 extern struct memory_block *find_memory_block(unsigned long section_nr);
0152 typedef int (*walk_memory_blocks_func_t)(struct memory_block *, void *);
0153 extern int walk_memory_blocks(unsigned long start, unsigned long size,
0154                   void *arg, walk_memory_blocks_func_t func);
0155 extern int for_each_memory_block(void *arg, walk_memory_blocks_func_t func);
0156 
0157 extern int memory_group_register_static(int nid, unsigned long max_pages);
0158 extern int memory_group_register_dynamic(int nid, unsigned long unit_pages);
0159 extern int memory_group_unregister(int mgid);
0160 struct memory_group *memory_group_find_by_id(int mgid);
0161 typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *);
0162 int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
0163                    struct memory_group *excluded, void *arg);
0164 #define hotplug_memory_notifier(fn, pri) ({     \
0165     static __meminitdata struct notifier_block fn##_mem_nb =\
0166         { .notifier_call = fn, .priority = pri };\
0167     register_memory_notifier(&fn##_mem_nb);         \
0168 })
0169 #define register_hotmemory_notifier(nb)     register_memory_notifier(nb)
0170 #define unregister_hotmemory_notifier(nb)   unregister_memory_notifier(nb)
0171 
0172 #ifdef CONFIG_NUMA
0173 void memory_block_add_nid(struct memory_block *mem, int nid,
0174               enum meminit_context context);
0175 #endif /* CONFIG_NUMA */
0176 #endif  /* CONFIG_MEMORY_HOTPLUG */
0177 
0178 /*
0179  * Kernel text modification mutex, used for code patching. Users of this lock
0180  * can sleep.
0181  */
0182 extern struct mutex text_mutex;
0183 
0184 #endif /* _LINUX_MEMORY_H_ */