memory.h 6.3 KB

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