Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 2 | /* |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 3 | * Memory subsystem support |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * Written by Matt Tolentino <matthew.e.tolentino@intel.com> |
| 6 | * Dave Hansen <haveblue@us.ibm.com> |
| 7 | * |
| 8 | * This file provides the necessary infrastructure to represent |
| 9 | * a SPARSEMEM-memory-model system's physical memory in /sysfs. |
| 10 | * All arch-independent code that assumes MEMORY_HOTPLUG requires |
| 11 | * SPARSEMEM should be contained here, or in mm/memory_hotplug.c. |
| 12 | */ |
| 13 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 16 | #include <linux/topology.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/memory.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 20 | #include <linux/memory_hotplug.h> |
| 21 | #include <linux/mm.h> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 22 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 24 | #include <linux/xarray.h> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 25 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 26 | #include <linux/atomic.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 28 | |
| 29 | #define MEMORY_CLASS_NAME "memory" |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 30 | |
David Hildenbrand | 4dc8207 | 2020-04-06 20:07:24 -0700 | [diff] [blame] | 31 | static const char *const online_type_to_str[] = { |
| 32 | [MMOP_OFFLINE] = "offline", |
| 33 | [MMOP_ONLINE] = "online", |
| 34 | [MMOP_ONLINE_KERNEL] = "online_kernel", |
| 35 | [MMOP_ONLINE_MOVABLE] = "online_movable", |
| 36 | }; |
| 37 | |
Anshuman Khandual | 1adf8b4 | 2021-02-25 17:17:13 -0800 | [diff] [blame] | 38 | int mhp_online_type_from_str(const char *str) |
David Hildenbrand | 4dc8207 | 2020-04-06 20:07:24 -0700 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) { |
| 43 | if (sysfs_streq(str, online_type_to_str[i])) |
| 44 | return i; |
| 45 | } |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 49 | #define to_memory_block(dev) container_of(dev, struct memory_block, dev) |
| 50 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 51 | static int sections_per_block; |
| 52 | |
Wei Yang | 178bdbe | 2020-06-23 10:57:01 +0800 | [diff] [blame] | 53 | static inline unsigned long memory_block_id(unsigned long section_nr) |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 54 | { |
| 55 | return section_nr / sections_per_block; |
| 56 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 57 | |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 58 | static inline unsigned long pfn_to_block_id(unsigned long pfn) |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 59 | { |
Wei Yang | 178bdbe | 2020-06-23 10:57:01 +0800 | [diff] [blame] | 60 | return memory_block_id(pfn_to_section_nr(pfn)); |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 61 | } |
| 62 | |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 63 | static inline unsigned long phys_to_block_id(unsigned long phys) |
| 64 | { |
| 65 | return pfn_to_block_id(PFN_DOWN(phys)); |
| 66 | } |
| 67 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 68 | static int memory_subsys_online(struct device *dev); |
| 69 | static int memory_subsys_offline(struct device *dev); |
| 70 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 71 | static struct bus_type memory_subsys = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 72 | .name = MEMORY_CLASS_NAME, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 73 | .dev_name = MEMORY_CLASS_NAME, |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 74 | .online = memory_subsys_online, |
| 75 | .offline = memory_subsys_offline, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Memory blocks are cached in a local radix tree to avoid |
| 80 | * a costly linear search for the corresponding device on |
| 81 | * the subsystem bus. |
| 82 | */ |
| 83 | static DEFINE_XARRAY(memory_blocks); |
| 84 | |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Memory groups, indexed by memory group id (mgid). |
| 87 | */ |
| 88 | static DEFINE_XARRAY_FLAGS(memory_groups, XA_FLAGS_ALLOC); |
David Hildenbrand | 3fcebf9 | 2021-09-07 19:55:48 -0700 | [diff] [blame] | 89 | #define MEMORY_GROUP_MARK_DYNAMIC XA_MARK_1 |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 90 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 91 | static BLOCKING_NOTIFIER_HEAD(memory_chain); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 92 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 93 | int register_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 94 | { |
Ioana Ciornei | 2aeebca | 2015-03-08 12:48:35 +0200 | [diff] [blame] | 95 | return blocking_notifier_chain_register(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 96 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 97 | EXPORT_SYMBOL(register_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 98 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 99 | void unregister_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 100 | { |
Ioana Ciornei | 2aeebca | 2015-03-08 12:48:35 +0200 | [diff] [blame] | 101 | blocking_notifier_chain_unregister(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 102 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 103 | EXPORT_SYMBOL(unregister_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 104 | |
Yasuaki Ishimatsu | fa7194e | 2012-12-11 16:00:44 -0800 | [diff] [blame] | 105 | static void memory_block_release(struct device *dev) |
| 106 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 107 | struct memory_block *mem = to_memory_block(dev); |
Yasuaki Ishimatsu | fa7194e | 2012-12-11 16:00:44 -0800 | [diff] [blame] | 108 | |
| 109 | kfree(mem); |
| 110 | } |
| 111 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 112 | unsigned long __weak memory_block_size_bytes(void) |
| 113 | { |
| 114 | return MIN_MEMORY_BLOCK_SIZE; |
| 115 | } |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 116 | EXPORT_SYMBOL_GPL(memory_block_size_bytes); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 117 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 118 | /* |
David Hildenbrand | f915fb7 | 2019-09-23 15:35:43 -0700 | [diff] [blame] | 119 | * Show the first physical section index (number) of this memory block. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 120 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 121 | static ssize_t phys_index_show(struct device *dev, |
| 122 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 123 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 124 | struct memory_block *mem = to_memory_block(dev); |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 125 | unsigned long phys_index; |
| 126 | |
| 127 | phys_index = mem->start_section_nr / sections_per_block; |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 128 | |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 129 | return sysfs_emit(buf, "%08lx\n", phys_index); |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 130 | } |
| 131 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 132 | /* |
David Hildenbrand | 53cdc1c | 2020-03-28 19:17:19 -0700 | [diff] [blame] | 133 | * Legacy interface that we cannot remove. Always indicate "removable" |
| 134 | * with CONFIG_MEMORY_HOTREMOVE - bad heuristic. |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 135 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 136 | static ssize_t removable_show(struct device *dev, struct device_attribute *attr, |
| 137 | char *buf) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 138 | { |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 139 | return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 143 | * online, offline, going offline, etc. |
| 144 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 145 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 146 | char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 147 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 148 | struct memory_block *mem = to_memory_block(dev); |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 149 | const char *output; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * We can probably put these states in a nice little array |
| 153 | * so that they're not open-coded |
| 154 | */ |
| 155 | switch (mem->state) { |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 156 | case MEM_ONLINE: |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 157 | output = "online"; |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 158 | break; |
| 159 | case MEM_OFFLINE: |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 160 | output = "offline"; |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 161 | break; |
| 162 | case MEM_GOING_OFFLINE: |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 163 | output = "going-offline"; |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 164 | break; |
| 165 | default: |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 166 | WARN_ON(1); |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 167 | return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 170 | return sysfs_emit(buf, "%s\n", output); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 173 | int memory_notify(unsigned long val, void *v) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 174 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 175 | return blocking_notifier_call_chain(&memory_chain, val, v); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 178 | static int memory_block_online(struct memory_block *mem) |
| 179 | { |
| 180 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
| 181 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 182 | unsigned long nr_vmemmap_pages = mem->nr_vmemmap_pages; |
| 183 | struct zone *zone; |
| 184 | int ret; |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 185 | |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 186 | zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group, |
| 187 | start_pfn, nr_pages); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Although vmemmap pages have a different lifecycle than the pages |
| 191 | * they describe (they remain until the memory is unplugged), doing |
| 192 | * their initialization and accounting at memory onlining/offlining |
| 193 | * stage helps to keep accounting easier to follow - e.g vmemmaps |
| 194 | * belong to the same zone as the memory they backed. |
| 195 | */ |
| 196 | if (nr_vmemmap_pages) { |
| 197 | ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | ret = online_pages(start_pfn + nr_vmemmap_pages, |
David Hildenbrand | 836809e | 2021-09-07 19:55:30 -0700 | [diff] [blame] | 203 | nr_pages - nr_vmemmap_pages, zone, mem->group); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 204 | if (ret) { |
| 205 | if (nr_vmemmap_pages) |
| 206 | mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Account once onlining succeeded. If the zone was unpopulated, it is |
| 212 | * now already properly populated. |
| 213 | */ |
| 214 | if (nr_vmemmap_pages) |
David Hildenbrand | 836809e | 2021-09-07 19:55:30 -0700 | [diff] [blame] | 215 | adjust_present_page_count(pfn_to_page(start_pfn), mem->group, |
David Hildenbrand | 4b09700 | 2021-09-07 19:55:19 -0700 | [diff] [blame] | 216 | nr_vmemmap_pages); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 217 | |
| 218 | return ret; |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int memory_block_offline(struct memory_block *mem) |
| 222 | { |
| 223 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
| 224 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 225 | unsigned long nr_vmemmap_pages = mem->nr_vmemmap_pages; |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 226 | int ret; |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 227 | |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 228 | /* |
| 229 | * Unaccount before offlining, such that unpopulated zone and kthreads |
| 230 | * can properly be torn down in offline_pages(). |
| 231 | */ |
David Hildenbrand | 4b09700 | 2021-09-07 19:55:19 -0700 | [diff] [blame] | 232 | if (nr_vmemmap_pages) |
David Hildenbrand | 836809e | 2021-09-07 19:55:30 -0700 | [diff] [blame] | 233 | adjust_present_page_count(pfn_to_page(start_pfn), mem->group, |
David Hildenbrand | 4b09700 | 2021-09-07 19:55:19 -0700 | [diff] [blame] | 234 | -nr_vmemmap_pages); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 235 | |
| 236 | ret = offline_pages(start_pfn + nr_vmemmap_pages, |
David Hildenbrand | 836809e | 2021-09-07 19:55:30 -0700 | [diff] [blame] | 237 | nr_pages - nr_vmemmap_pages, mem->group); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 238 | if (ret) { |
| 239 | /* offline_pages() failed. Account back. */ |
| 240 | if (nr_vmemmap_pages) |
David Hildenbrand | 4b09700 | 2021-09-07 19:55:19 -0700 | [diff] [blame] | 241 | adjust_present_page_count(pfn_to_page(start_pfn), |
David Hildenbrand | 836809e | 2021-09-07 19:55:30 -0700 | [diff] [blame] | 242 | mem->group, nr_vmemmap_pages); |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | if (nr_vmemmap_pages) |
| 247 | mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages); |
| 248 | |
| 249 | return ret; |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 252 | /* |
| 253 | * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is |
| 254 | * OK to have direct references to sparsemem variables in here. |
| 255 | */ |
| 256 | static int |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 257 | memory_block_action(struct memory_block *mem, unsigned long action) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 258 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 259 | int ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 260 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 261 | switch (action) { |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 262 | case MEM_ONLINE: |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 263 | ret = memory_block_online(mem); |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 264 | break; |
| 265 | case MEM_OFFLINE: |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 266 | ret = memory_block_offline(mem); |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 267 | break; |
| 268 | default: |
| 269 | WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: " |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 270 | "%ld\n", __func__, mem->start_section_nr, action, action); |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 271 | ret = -EINVAL; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 272 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | |
Nathan Fontenot | dc18d70 | 2017-02-24 15:00:02 -0800 | [diff] [blame] | 277 | static int memory_block_change_state(struct memory_block *mem, |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 278 | unsigned long to_state, unsigned long from_state_req) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 279 | { |
Greg Kroah-Hartman | de0ed36 | 2011-10-18 14:00:57 -0700 | [diff] [blame] | 280 | int ret = 0; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 281 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 282 | if (mem->state != from_state_req) |
| 283 | return -EINVAL; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 284 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 285 | if (to_state == MEM_OFFLINE) |
| 286 | mem->state = MEM_GOING_OFFLINE; |
| 287 | |
Oscar Salvador | 8736cc2 | 2021-05-04 18:39:33 -0700 | [diff] [blame] | 288 | ret = memory_block_action(mem, to_state); |
Rafael J. Wysocki | b2c064b | 2013-05-23 10:38:55 +0200 | [diff] [blame] | 289 | mem->state = ret ? from_state_req : to_state; |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 290 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 291 | return ret; |
| 292 | } |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 293 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 294 | /* The device lock serializes operations on memory_subsys_[online|offline] */ |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 295 | static int memory_subsys_online(struct device *dev) |
| 296 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 297 | struct memory_block *mem = to_memory_block(dev); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 298 | int ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 299 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 300 | if (mem->state == MEM_ONLINE) |
| 301 | return 0; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 302 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 303 | /* |
David Hildenbrand | efc978a | 2020-04-06 20:07:20 -0700 | [diff] [blame] | 304 | * When called via device_online() without configuring the online_type, |
| 305 | * we want to default to MMOP_ONLINE. |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 306 | */ |
David Hildenbrand | efc978a | 2020-04-06 20:07:20 -0700 | [diff] [blame] | 307 | if (mem->online_type == MMOP_OFFLINE) |
David Hildenbrand | 956f8b4 | 2020-04-06 20:07:16 -0700 | [diff] [blame] | 308 | mem->online_type = MMOP_ONLINE; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 309 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 310 | ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
David Hildenbrand | efc978a | 2020-04-06 20:07:20 -0700 | [diff] [blame] | 311 | mem->online_type = MMOP_OFFLINE; |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 312 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int memory_subsys_offline(struct device *dev) |
| 317 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 318 | struct memory_block *mem = to_memory_block(dev); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 319 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 320 | if (mem->state == MEM_OFFLINE) |
| 321 | return 0; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 322 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 323 | return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 324 | } |
| 325 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 326 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 327 | const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 328 | { |
Anshuman Khandual | 1adf8b4 | 2021-02-25 17:17:13 -0800 | [diff] [blame] | 329 | const int online_type = mhp_online_type_from_str(buf); |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 330 | struct memory_block *mem = to_memory_block(dev); |
David Hildenbrand | 4dc8207 | 2020-04-06 20:07:24 -0700 | [diff] [blame] | 331 | int ret; |
| 332 | |
| 333 | if (online_type < 0) |
| 334 | return -EINVAL; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 335 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 336 | ret = lock_device_hotplug_sysfs(); |
| 337 | if (ret) |
| 338 | return ret; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 339 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 340 | switch (online_type) { |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 341 | case MMOP_ONLINE_KERNEL: |
| 342 | case MMOP_ONLINE_MOVABLE: |
David Hildenbrand | 956f8b4 | 2020-04-06 20:07:16 -0700 | [diff] [blame] | 343 | case MMOP_ONLINE: |
David Hildenbrand | 381eab4 | 2018-10-30 15:10:29 -0700 | [diff] [blame] | 344 | /* mem->online_type is protected by device_hotplug_lock */ |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 345 | mem->online_type = online_type; |
| 346 | ret = device_online(&mem->dev); |
| 347 | break; |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 348 | case MMOP_OFFLINE: |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 349 | ret = device_offline(&mem->dev); |
| 350 | break; |
| 351 | default: |
| 352 | ret = -EINVAL; /* should never happen */ |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 353 | } |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 354 | |
| 355 | unlock_device_hotplug(); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 356 | |
Reza Arbab | d66ba15 | 2016-10-07 17:00:15 -0700 | [diff] [blame] | 357 | if (ret < 0) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 358 | return ret; |
Reza Arbab | d66ba15 | 2016-10-07 17:00:15 -0700 | [diff] [blame] | 359 | if (ret) |
| 360 | return -EINVAL; |
| 361 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 362 | return count; |
| 363 | } |
| 364 | |
| 365 | /* |
David Hildenbrand | e9a2e48 | 2021-02-25 17:17:24 -0800 | [diff] [blame] | 366 | * Legacy interface that we cannot remove: s390x exposes the storage increment |
| 367 | * covered by a memory block, allowing for identifying which memory blocks |
| 368 | * comprise a storage increment. Since a memory block spans complete |
| 369 | * storage increments nowadays, this interface is basically unused. Other |
| 370 | * archs never exposed != 0. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 371 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 372 | static ssize_t phys_device_show(struct device *dev, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 373 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 374 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 375 | struct memory_block *mem = to_memory_block(dev); |
David Hildenbrand | e9a2e48 | 2021-02-25 17:17:24 -0800 | [diff] [blame] | 376 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 377 | |
David Hildenbrand | e9a2e48 | 2021-02-25 17:17:24 -0800 | [diff] [blame] | 378 | return sysfs_emit(buf, "%d\n", |
| 379 | arch_get_memory_phys_device(start_pfn)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 382 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 383 | static int print_allowed_zone(char *buf, int len, int nid, |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 384 | struct memory_group *group, |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 385 | unsigned long start_pfn, unsigned long nr_pages, |
| 386 | int online_type, struct zone *default_zone) |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 387 | { |
| 388 | struct zone *zone; |
| 389 | |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 390 | zone = zone_for_pfn_range(online_type, nid, group, start_pfn, nr_pages); |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 391 | if (zone == default_zone) |
| 392 | return 0; |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 393 | |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 394 | return sysfs_emit_at(buf, len, " %s", zone->name); |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 395 | } |
| 396 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 397 | static ssize_t valid_zones_show(struct device *dev, |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 398 | struct device_attribute *attr, char *buf) |
| 399 | { |
| 400 | struct memory_block *mem = to_memory_block(dev); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 401 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 402 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 403 | struct memory_group *group = mem->group; |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 404 | struct zone *default_zone; |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 405 | int nid = mem->nid; |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 406 | int len = 0; |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 407 | |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 408 | /* |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 409 | * Check the existing zone. Make sure that we do that only on the |
| 410 | * online nodes otherwise the page_zone is not reliable |
| 411 | */ |
| 412 | if (mem->state == MEM_ONLINE) { |
Mikhail Zaslonko | 4e8346d | 2018-09-04 15:46:09 -0700 | [diff] [blame] | 413 | /* |
| 414 | * The block contains more than one zone can not be offlined. |
| 415 | * This can happen e.g. for ZONE_DMA and ZONE_DMA32 |
| 416 | */ |
David Hildenbrand | 9291799 | 2020-02-03 17:34:26 -0800 | [diff] [blame] | 417 | default_zone = test_pages_in_a_zone(start_pfn, |
| 418 | start_pfn + nr_pages); |
| 419 | if (!default_zone) |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 420 | return sysfs_emit(buf, "%s\n", "none"); |
| 421 | len += sysfs_emit_at(buf, len, "%s", default_zone->name); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 422 | goto out; |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 423 | } |
| 424 | |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 425 | default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, group, |
| 426 | start_pfn, nr_pages); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 427 | |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 428 | len += sysfs_emit_at(buf, len, "%s", default_zone->name); |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 429 | len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages, |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 430 | MMOP_ONLINE_KERNEL, default_zone); |
David Hildenbrand | 445fcf7 | 2021-09-07 19:55:45 -0700 | [diff] [blame] | 431 | len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages, |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 432 | MMOP_ONLINE_MOVABLE, default_zone); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 433 | out: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 434 | len += sysfs_emit_at(buf, len, "\n"); |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 435 | return len; |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 436 | } |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 437 | static DEVICE_ATTR_RO(valid_zones); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 438 | #endif |
| 439 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 440 | static DEVICE_ATTR_RO(phys_index); |
| 441 | static DEVICE_ATTR_RW(state); |
| 442 | static DEVICE_ATTR_RO(phys_device); |
| 443 | static DEVICE_ATTR_RO(removable); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 444 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 445 | /* |
David Hildenbrand | f915fb7 | 2019-09-23 15:35:43 -0700 | [diff] [blame] | 446 | * Show the memory block size (shared by all memory blocks). |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 447 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 448 | static ssize_t block_size_bytes_show(struct device *dev, |
| 449 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 450 | { |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 451 | return sysfs_emit(buf, "%lx\n", memory_block_size_bytes()); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 452 | } |
| 453 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 454 | static DEVICE_ATTR_RO(block_size_bytes); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 455 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 456 | /* |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 457 | * Memory auto online policy. |
| 458 | */ |
| 459 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 460 | static ssize_t auto_online_blocks_show(struct device *dev, |
| 461 | struct device_attribute *attr, char *buf) |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 462 | { |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 463 | return sysfs_emit(buf, "%s\n", |
Anshuman Khandual | 1adf8b4 | 2021-02-25 17:17:13 -0800 | [diff] [blame] | 464 | online_type_to_str[mhp_default_online_type]); |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 465 | } |
| 466 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 467 | static ssize_t auto_online_blocks_store(struct device *dev, |
| 468 | struct device_attribute *attr, |
| 469 | const char *buf, size_t count) |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 470 | { |
Anshuman Khandual | 1adf8b4 | 2021-02-25 17:17:13 -0800 | [diff] [blame] | 471 | const int online_type = mhp_online_type_from_str(buf); |
David Hildenbrand | 5f47adf | 2020-04-06 20:07:44 -0700 | [diff] [blame] | 472 | |
| 473 | if (online_type < 0) |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 474 | return -EINVAL; |
| 475 | |
Anshuman Khandual | 1adf8b4 | 2021-02-25 17:17:13 -0800 | [diff] [blame] | 476 | mhp_default_online_type = online_type; |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 477 | return count; |
| 478 | } |
| 479 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 480 | static DEVICE_ATTR_RW(auto_online_blocks); |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 481 | |
| 482 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 483 | * Some architectures will have custom drivers to do this, and |
| 484 | * will not need to do it from userspace. The fake hot-add code |
| 485 | * as well as ppc64 will do all of their discovery in userspace |
| 486 | * and will require this interface. |
| 487 | */ |
| 488 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 489 | static ssize_t probe_store(struct device *dev, struct device_attribute *attr, |
| 490 | const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 491 | { |
| 492 | u64 phys_addr; |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 493 | int nid, ret; |
Anton Blanchard | 61b94fe | 2011-09-15 06:26:15 +1000 | [diff] [blame] | 494 | unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 495 | |
Zhang Zhen | b69deb2 | 2014-08-06 16:06:06 -0700 | [diff] [blame] | 496 | ret = kstrtoull(buf, 0, &phys_addr); |
| 497 | if (ret) |
| 498 | return ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 499 | |
Anton Blanchard | 61b94fe | 2011-09-15 06:26:15 +1000 | [diff] [blame] | 500 | if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1)) |
| 501 | return -EINVAL; |
| 502 | |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 503 | ret = lock_device_hotplug_sysfs(); |
| 504 | if (ret) |
zhong jiang | 3780384 | 2019-04-18 17:50:16 -0700 | [diff] [blame] | 505 | return ret; |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 506 | |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 507 | nid = memory_add_physaddr_to_nid(phys_addr); |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 508 | ret = __add_memory(nid, phys_addr, |
David Hildenbrand | b611719 | 2020-10-15 20:08:44 -0700 | [diff] [blame] | 509 | MIN_MEMORY_BLOCK_SIZE * sections_per_block, |
| 510 | MHP_NONE); |
Nathan Fontenot | 6add7cd | 2011-01-31 10:55:23 -0600 | [diff] [blame] | 511 | |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 512 | if (ret) |
| 513 | goto out; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 514 | |
Nikanth Karthikesan | 9f0af69 | 2011-03-24 11:46:18 +0530 | [diff] [blame] | 515 | ret = count; |
| 516 | out: |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 517 | unlock_device_hotplug(); |
Nikanth Karthikesan | 9f0af69 | 2011-03-24 11:46:18 +0530 | [diff] [blame] | 518 | return ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 519 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 520 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 521 | static DEVICE_ATTR_WO(probe); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 522 | #endif |
| 523 | |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 524 | #ifdef CONFIG_MEMORY_FAILURE |
| 525 | /* |
| 526 | * Support for offlining pages of memory |
| 527 | */ |
| 528 | |
| 529 | /* Soft offline a page */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 530 | static ssize_t soft_offline_page_store(struct device *dev, |
| 531 | struct device_attribute *attr, |
| 532 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 533 | { |
| 534 | int ret; |
| 535 | u64 pfn; |
| 536 | if (!capable(CAP_SYS_ADMIN)) |
| 537 | return -EPERM; |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 538 | if (kstrtoull(buf, 0, &pfn) < 0) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | pfn >>= PAGE_SHIFT; |
Naoya Horiguchi | feec24a | 2019-11-30 17:53:38 -0800 | [diff] [blame] | 541 | ret = soft_offline_page(pfn, 0); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 542 | return ret == 0 ? count : ret; |
| 543 | } |
| 544 | |
| 545 | /* Forcibly offline a page, including killing processes. */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 546 | static ssize_t hard_offline_page_store(struct device *dev, |
| 547 | struct device_attribute *attr, |
| 548 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 549 | { |
| 550 | int ret; |
| 551 | u64 pfn; |
| 552 | if (!capable(CAP_SYS_ADMIN)) |
| 553 | return -EPERM; |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 554 | if (kstrtoull(buf, 0, &pfn) < 0) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 555 | return -EINVAL; |
| 556 | pfn >>= PAGE_SHIFT; |
Eric W. Biederman | 83b5753 | 2017-07-09 18:14:01 -0500 | [diff] [blame] | 557 | ret = memory_failure(pfn, 0); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 558 | return ret ? ret : count; |
| 559 | } |
| 560 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 561 | static DEVICE_ATTR_WO(soft_offline_page); |
| 562 | static DEVICE_ATTR_WO(hard_offline_page); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 563 | #endif |
| 564 | |
David Hildenbrand | e9a2e48 | 2021-02-25 17:17:24 -0800 | [diff] [blame] | 565 | /* See phys_device_show(). */ |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 566 | int __weak arch_get_memory_phys_device(unsigned long start_pfn) |
| 567 | { |
| 568 | return 0; |
| 569 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 570 | |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 571 | /* |
| 572 | * A reference for the returned memory block device is acquired. |
| 573 | * |
| 574 | * Called under device_hotplug_lock. |
| 575 | */ |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 576 | static struct memory_block *find_memory_block_by_id(unsigned long block_id) |
Robin Holt | 9838303 | 2010-09-29 14:00:55 -0500 | [diff] [blame] | 577 | { |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 578 | struct memory_block *mem; |
Robin Holt | 9838303 | 2010-09-29 14:00:55 -0500 | [diff] [blame] | 579 | |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 580 | mem = xa_load(&memory_blocks, block_id); |
| 581 | if (mem) |
| 582 | get_device(&mem->dev); |
| 583 | return mem; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 586 | /* |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 587 | * Called under device_hotplug_lock. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 588 | */ |
Ohhoon Kwon | fc1f5e9 | 2021-09-02 14:57:01 -0700 | [diff] [blame] | 589 | struct memory_block *find_memory_block(unsigned long section_nr) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 590 | { |
Ohhoon Kwon | fc1f5e9 | 2021-09-02 14:57:01 -0700 | [diff] [blame] | 591 | unsigned long block_id = memory_block_id(section_nr); |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 592 | |
| 593 | return find_memory_block_by_id(block_id); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 596 | static struct attribute *memory_memblk_attrs[] = { |
| 597 | &dev_attr_phys_index.attr, |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 598 | &dev_attr_state.attr, |
| 599 | &dev_attr_phys_device.attr, |
| 600 | &dev_attr_removable.attr, |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 601 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 602 | &dev_attr_valid_zones.attr, |
| 603 | #endif |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 604 | NULL |
| 605 | }; |
| 606 | |
Rikard Falkeborn | 5a57676 | 2021-05-28 23:34:08 +0200 | [diff] [blame] | 607 | static const struct attribute_group memory_memblk_attr_group = { |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 608 | .attrs = memory_memblk_attrs, |
| 609 | }; |
| 610 | |
| 611 | static const struct attribute_group *memory_memblk_attr_groups[] = { |
| 612 | &memory_memblk_attr_group, |
| 613 | NULL, |
| 614 | }; |
| 615 | |
| 616 | /* |
| 617 | * register_memory - Setup a sysfs device for a memory block |
| 618 | */ |
| 619 | static |
| 620 | int register_memory(struct memory_block *memory) |
| 621 | { |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 622 | int ret; |
| 623 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 624 | memory->dev.bus = &memory_subsys; |
| 625 | memory->dev.id = memory->start_section_nr / sections_per_block; |
| 626 | memory->dev.release = memory_block_release; |
| 627 | memory->dev.groups = memory_memblk_attr_groups; |
Linus Torvalds | f991fae | 2013-07-03 14:35:40 -0700 | [diff] [blame] | 628 | memory->dev.offline = memory->state == MEM_OFFLINE; |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 629 | |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 630 | ret = device_register(&memory->dev); |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 631 | if (ret) { |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 632 | put_device(&memory->dev); |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 633 | return ret; |
| 634 | } |
| 635 | ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory, |
| 636 | GFP_KERNEL)); |
| 637 | if (ret) { |
| 638 | put_device(&memory->dev); |
| 639 | device_unregister(&memory->dev); |
| 640 | } |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 641 | return ret; |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 644 | static int init_memory_block(unsigned long block_id, unsigned long state, |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 645 | unsigned long nr_vmemmap_pages, |
| 646 | struct memory_group *group) |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 647 | { |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 648 | struct memory_block *mem; |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 649 | int ret = 0; |
| 650 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 651 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 652 | if (mem) { |
| 653 | put_device(&mem->dev); |
| 654 | return -EEXIST; |
| 655 | } |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 656 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 657 | if (!mem) |
| 658 | return -ENOMEM; |
| 659 | |
David Hildenbrand | 1811582 | 2019-07-18 15:56:46 -0700 | [diff] [blame] | 660 | mem->start_section_nr = block_id * sections_per_block; |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 661 | mem->state = state; |
David Hildenbrand | d84f2f5 | 2019-09-23 15:35:40 -0700 | [diff] [blame] | 662 | mem->nid = NUMA_NO_NODE; |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 663 | mem->nr_vmemmap_pages = nr_vmemmap_pages; |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 664 | INIT_LIST_HEAD(&mem->group_next); |
| 665 | |
| 666 | if (group) { |
| 667 | mem->group = group; |
| 668 | list_add(&mem->group_next, &group->memory_blocks); |
| 669 | } |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 670 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 671 | ret = register_memory(mem); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 672 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 673 | return ret; |
| 674 | } |
| 675 | |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 676 | static int add_memory_block(unsigned long base_section_nr) |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 677 | { |
David Hildenbrand | 68c3a6a | 2020-04-06 20:06:40 -0700 | [diff] [blame] | 678 | int section_count = 0; |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 679 | unsigned long nr; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 680 | |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 681 | for (nr = base_section_nr; nr < base_section_nr + sections_per_block; |
| 682 | nr++) |
| 683 | if (present_section_nr(nr)) |
David Hildenbrand | 1811582 | 2019-07-18 15:56:46 -0700 | [diff] [blame] | 684 | section_count++; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 685 | |
Seth Jennings | cb5e39b | 2013-08-20 12:13:03 -0500 | [diff] [blame] | 686 | if (section_count == 0) |
| 687 | return 0; |
Wei Yang | 178bdbe | 2020-06-23 10:57:01 +0800 | [diff] [blame] | 688 | return init_memory_block(memory_block_id(base_section_nr), |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 689 | MEM_ONLINE, 0, NULL); |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 690 | } |
| 691 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 692 | static void unregister_memory(struct memory_block *memory) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 693 | { |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 694 | if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys)) |
| 695 | return; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 696 | |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 697 | WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL); |
| 698 | |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 699 | if (memory->group) { |
| 700 | list_del(&memory->group_next); |
| 701 | memory->group = NULL; |
| 702 | } |
| 703 | |
David Hildenbrand | cb7b3a3 | 2019-05-13 17:21:37 -0700 | [diff] [blame] | 704 | /* drop the ref. we got via find_memory_block() */ |
Seth Jennings | df2b717 | 2013-08-20 12:12:59 -0500 | [diff] [blame] | 705 | put_device(&memory->dev); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 706 | device_unregister(&memory->dev); |
| 707 | } |
| 708 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 709 | /* |
| 710 | * Create memory block devices for the given memory area. Start and size |
| 711 | * have to be aligned to memory block granularity. Memory block devices |
| 712 | * will be initialized as offline. |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 713 | * |
| 714 | * Called under device_hotplug_lock. |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 715 | */ |
Oscar Salvador | a08a2ae | 2021-05-04 18:39:42 -0700 | [diff] [blame] | 716 | int create_memory_block_devices(unsigned long start, unsigned long size, |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 717 | unsigned long vmemmap_pages, |
| 718 | struct memory_group *group) |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 719 | { |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 720 | const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start)); |
| 721 | unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size)); |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 722 | struct memory_block *mem; |
| 723 | unsigned long block_id; |
| 724 | int ret = 0; |
| 725 | |
| 726 | if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) || |
| 727 | !IS_ALIGNED(size, memory_block_size_bytes()))) |
| 728 | return -EINVAL; |
| 729 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 730 | for (block_id = start_block_id; block_id != end_block_id; block_id++) { |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 731 | ret = init_memory_block(block_id, MEM_OFFLINE, vmemmap_pages, |
| 732 | group); |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 733 | if (ret) |
| 734 | break; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 735 | } |
| 736 | if (ret) { |
| 737 | end_block_id = block_id; |
| 738 | for (block_id = start_block_id; block_id != end_block_id; |
| 739 | block_id++) { |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 740 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 741 | if (WARN_ON_ONCE(!mem)) |
| 742 | continue; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 743 | unregister_memory(mem); |
| 744 | } |
| 745 | } |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 746 | return ret; |
| 747 | } |
| 748 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 749 | /* |
| 750 | * Remove memory block devices for the given memory area. Start and size |
| 751 | * have to be aligned to memory block granularity. Memory block devices |
| 752 | * have to be offline. |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 753 | * |
| 754 | * Called under device_hotplug_lock. |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 755 | */ |
| 756 | void remove_memory_block_devices(unsigned long start, unsigned long size) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 757 | { |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 758 | const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start)); |
| 759 | const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 760 | struct memory_block *mem; |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 761 | unsigned long block_id; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 762 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 763 | if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) || |
| 764 | !IS_ALIGNED(size, memory_block_size_bytes()))) |
David Hildenbrand | cb7b3a3 | 2019-05-13 17:21:37 -0700 | [diff] [blame] | 765 | return; |
| 766 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 767 | for (block_id = start_block_id; block_id != end_block_id; block_id++) { |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 768 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 769 | if (WARN_ON_ONCE(!mem)) |
| 770 | continue; |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 771 | unregister_memory_block_under_nodes(mem); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 772 | unregister_memory(mem); |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 773 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 776 | /* return true if the memory block is offlined, otherwise, return false */ |
| 777 | bool is_memblock_offlined(struct memory_block *mem) |
| 778 | { |
| 779 | return mem->state == MEM_OFFLINE; |
| 780 | } |
| 781 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 782 | static struct attribute *memory_root_attrs[] = { |
| 783 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
| 784 | &dev_attr_probe.attr, |
| 785 | #endif |
| 786 | |
| 787 | #ifdef CONFIG_MEMORY_FAILURE |
| 788 | &dev_attr_soft_offline_page.attr, |
| 789 | &dev_attr_hard_offline_page.attr, |
| 790 | #endif |
| 791 | |
| 792 | &dev_attr_block_size_bytes.attr, |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 793 | &dev_attr_auto_online_blocks.attr, |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 794 | NULL |
| 795 | }; |
| 796 | |
Rikard Falkeborn | 5a57676 | 2021-05-28 23:34:08 +0200 | [diff] [blame] | 797 | static const struct attribute_group memory_root_attr_group = { |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 798 | .attrs = memory_root_attrs, |
| 799 | }; |
| 800 | |
| 801 | static const struct attribute_group *memory_root_attr_groups[] = { |
| 802 | &memory_root_attr_group, |
| 803 | NULL, |
| 804 | }; |
| 805 | |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 806 | /* |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 807 | * Initialize the sysfs support for memory devices. At the time this function |
| 808 | * is called, we cannot have concurrent creation/deletion of memory block |
| 809 | * devices, the device_hotplug_lock is not needed. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 810 | */ |
David Hildenbrand | 902ce63b | 2019-09-23 15:35:46 -0700 | [diff] [blame] | 811 | void __init memory_dev_init(void) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 812 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 813 | int ret; |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 814 | unsigned long block_sz, nr; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 815 | |
David Hildenbrand | 902ce63b | 2019-09-23 15:35:46 -0700 | [diff] [blame] | 816 | /* Validate the configured memory block size */ |
| 817 | block_sz = memory_block_size_bytes(); |
| 818 | if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE) |
| 819 | panic("Memory block size not suitable: 0x%lx\n", block_sz); |
| 820 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 821 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 822 | ret = subsys_system_register(&memory_subsys, memory_root_attr_groups); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 823 | if (ret) |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 824 | panic("%s() failed to register subsystem: %d\n", __func__, ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Create entries for memory sections that were found |
| 828 | * during boot and have been initialized |
| 829 | */ |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 830 | for (nr = 0; nr <= __highest_present_section_nr; |
| 831 | nr += sections_per_block) { |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 832 | ret = add_memory_block(nr); |
| 833 | if (ret) |
| 834 | panic("%s() failed to add memory block: %d\n", __func__, |
| 835 | ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 836 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 837 | } |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 838 | |
| 839 | /** |
| 840 | * walk_memory_blocks - walk through all present memory blocks overlapped |
| 841 | * by the range [start, start + size) |
| 842 | * |
| 843 | * @start: start address of the memory range |
| 844 | * @size: size of the memory range |
| 845 | * @arg: argument passed to func |
| 846 | * @func: callback for each memory section walked |
| 847 | * |
| 848 | * This function walks through all present memory blocks overlapped by the |
| 849 | * range [start, start + size), calling func on each memory block. |
| 850 | * |
| 851 | * In case func() returns an error, walking is aborted and the error is |
| 852 | * returned. |
Scott Cheloha | 4fb6eab | 2020-06-03 16:03:48 -0700 | [diff] [blame] | 853 | * |
| 854 | * Called under device_hotplug_lock. |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 855 | */ |
| 856 | int walk_memory_blocks(unsigned long start, unsigned long size, |
| 857 | void *arg, walk_memory_blocks_func_t func) |
| 858 | { |
| 859 | const unsigned long start_block_id = phys_to_block_id(start); |
| 860 | const unsigned long end_block_id = phys_to_block_id(start + size - 1); |
| 861 | struct memory_block *mem; |
| 862 | unsigned long block_id; |
| 863 | int ret = 0; |
| 864 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 865 | if (!size) |
| 866 | return 0; |
| 867 | |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 868 | for (block_id = start_block_id; block_id <= end_block_id; block_id++) { |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 869 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 870 | if (!mem) |
| 871 | continue; |
| 872 | |
| 873 | ret = func(mem, arg); |
| 874 | put_device(&mem->dev); |
| 875 | if (ret) |
| 876 | break; |
| 877 | } |
| 878 | return ret; |
| 879 | } |
David Hildenbrand | 2c91f8f | 2019-11-15 17:34:57 -0800 | [diff] [blame] | 880 | |
| 881 | struct for_each_memory_block_cb_data { |
| 882 | walk_memory_blocks_func_t func; |
| 883 | void *arg; |
| 884 | }; |
| 885 | |
| 886 | static int for_each_memory_block_cb(struct device *dev, void *data) |
| 887 | { |
| 888 | struct memory_block *mem = to_memory_block(dev); |
| 889 | struct for_each_memory_block_cb_data *cb_data = data; |
| 890 | |
| 891 | return cb_data->func(mem, cb_data->arg); |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * for_each_memory_block - walk through all present memory blocks |
| 896 | * |
| 897 | * @arg: argument passed to func |
| 898 | * @func: callback for each memory block walked |
| 899 | * |
| 900 | * This function walks through all present memory blocks, calling func on |
| 901 | * each memory block. |
| 902 | * |
| 903 | * In case func() returns an error, walking is aborted and the error is |
| 904 | * returned. |
| 905 | */ |
| 906 | int for_each_memory_block(void *arg, walk_memory_blocks_func_t func) |
| 907 | { |
| 908 | struct for_each_memory_block_cb_data cb_data = { |
| 909 | .func = func, |
| 910 | .arg = arg, |
| 911 | }; |
| 912 | |
| 913 | return bus_for_each_dev(&memory_subsys, NULL, &cb_data, |
| 914 | for_each_memory_block_cb); |
| 915 | } |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 916 | |
| 917 | /* |
| 918 | * This is an internal helper to unify allocation and initialization of |
| 919 | * memory groups. Note that the passed memory group will be copied to a |
| 920 | * dynamically allocated memory group. After this call, the passed |
| 921 | * memory group should no longer be used. |
| 922 | */ |
| 923 | static int memory_group_register(struct memory_group group) |
| 924 | { |
| 925 | struct memory_group *new_group; |
| 926 | uint32_t mgid; |
| 927 | int ret; |
| 928 | |
| 929 | if (!node_possible(group.nid)) |
| 930 | return -EINVAL; |
| 931 | |
| 932 | new_group = kzalloc(sizeof(group), GFP_KERNEL); |
| 933 | if (!new_group) |
| 934 | return -ENOMEM; |
| 935 | *new_group = group; |
| 936 | INIT_LIST_HEAD(&new_group->memory_blocks); |
| 937 | |
| 938 | ret = xa_alloc(&memory_groups, &mgid, new_group, xa_limit_31b, |
| 939 | GFP_KERNEL); |
| 940 | if (ret) { |
| 941 | kfree(new_group); |
| 942 | return ret; |
David Hildenbrand | 3fcebf9 | 2021-09-07 19:55:48 -0700 | [diff] [blame] | 943 | } else if (group.is_dynamic) { |
| 944 | xa_set_mark(&memory_groups, mgid, MEMORY_GROUP_MARK_DYNAMIC); |
David Hildenbrand | 028fc57 | 2021-09-07 19:55:26 -0700 | [diff] [blame] | 945 | } |
| 946 | return mgid; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * memory_group_register_static() - Register a static memory group. |
| 951 | * @nid: The node id. |
| 952 | * @max_pages: The maximum number of pages we'll have in this static memory |
| 953 | * group. |
| 954 | * |
| 955 | * Register a new static memory group and return the memory group id. |
| 956 | * All memory in the group belongs to a single unit, such as a DIMM. All |
| 957 | * memory belonging to a static memory group is added in one go to be removed |
| 958 | * in one go -- it's static. |
| 959 | * |
| 960 | * Returns an error if out of memory, if the node id is invalid, if no new |
| 961 | * memory groups can be registered, or if max_pages is invalid (0). Otherwise, |
| 962 | * returns the new memory group id. |
| 963 | */ |
| 964 | int memory_group_register_static(int nid, unsigned long max_pages) |
| 965 | { |
| 966 | struct memory_group group = { |
| 967 | .nid = nid, |
| 968 | .s = { |
| 969 | .max_pages = max_pages, |
| 970 | }, |
| 971 | }; |
| 972 | |
| 973 | if (!max_pages) |
| 974 | return -EINVAL; |
| 975 | return memory_group_register(group); |
| 976 | } |
| 977 | EXPORT_SYMBOL_GPL(memory_group_register_static); |
| 978 | |
| 979 | /** |
| 980 | * memory_group_register_dynamic() - Register a dynamic memory group. |
| 981 | * @nid: The node id. |
| 982 | * @unit_pages: Unit in pages in which is memory added/removed in this dynamic |
| 983 | * memory group. |
| 984 | * |
| 985 | * Register a new dynamic memory group and return the memory group id. |
| 986 | * Memory within a dynamic memory group is added/removed dynamically |
| 987 | * in unit_pages. |
| 988 | * |
| 989 | * Returns an error if out of memory, if the node id is invalid, if no new |
| 990 | * memory groups can be registered, or if unit_pages is invalid (0, not a |
| 991 | * power of two, smaller than a single memory block). Otherwise, returns the |
| 992 | * new memory group id. |
| 993 | */ |
| 994 | int memory_group_register_dynamic(int nid, unsigned long unit_pages) |
| 995 | { |
| 996 | struct memory_group group = { |
| 997 | .nid = nid, |
| 998 | .is_dynamic = true, |
| 999 | .d = { |
| 1000 | .unit_pages = unit_pages, |
| 1001 | }, |
| 1002 | }; |
| 1003 | |
| 1004 | if (!unit_pages || !is_power_of_2(unit_pages) || |
| 1005 | unit_pages < PHYS_PFN(memory_block_size_bytes())) |
| 1006 | return -EINVAL; |
| 1007 | return memory_group_register(group); |
| 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(memory_group_register_dynamic); |
| 1010 | |
| 1011 | /** |
| 1012 | * memory_group_unregister() - Unregister a memory group. |
| 1013 | * @mgid: the memory group id |
| 1014 | * |
| 1015 | * Unregister a memory group. If any memory block still belongs to this |
| 1016 | * memory group, unregistering will fail. |
| 1017 | * |
| 1018 | * Returns -EINVAL if the memory group id is invalid, returns -EBUSY if some |
| 1019 | * memory blocks still belong to this memory group and returns 0 if |
| 1020 | * unregistering succeeded. |
| 1021 | */ |
| 1022 | int memory_group_unregister(int mgid) |
| 1023 | { |
| 1024 | struct memory_group *group; |
| 1025 | |
| 1026 | if (mgid < 0) |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | group = xa_load(&memory_groups, mgid); |
| 1030 | if (!group) |
| 1031 | return -EINVAL; |
| 1032 | if (!list_empty(&group->memory_blocks)) |
| 1033 | return -EBUSY; |
| 1034 | xa_erase(&memory_groups, mgid); |
| 1035 | kfree(group); |
| 1036 | return 0; |
| 1037 | } |
| 1038 | EXPORT_SYMBOL_GPL(memory_group_unregister); |
| 1039 | |
| 1040 | /* |
| 1041 | * This is an internal helper only to be used in core memory hotplug code to |
| 1042 | * lookup a memory group. We don't care about locking, as we don't expect a |
| 1043 | * memory group to get unregistered while adding memory to it -- because |
| 1044 | * the group and the memory is managed by the same driver. |
| 1045 | */ |
| 1046 | struct memory_group *memory_group_find_by_id(int mgid) |
| 1047 | { |
| 1048 | return xa_load(&memory_groups, mgid); |
| 1049 | } |
David Hildenbrand | 3fcebf9 | 2021-09-07 19:55:48 -0700 | [diff] [blame] | 1050 | |
| 1051 | /* |
| 1052 | * This is an internal helper only to be used in core memory hotplug code to |
| 1053 | * walk all dynamic memory groups excluding a given memory group, either |
| 1054 | * belonging to a specific node, or belonging to any node. |
| 1055 | */ |
| 1056 | int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, |
| 1057 | struct memory_group *excluded, void *arg) |
| 1058 | { |
| 1059 | struct memory_group *group; |
| 1060 | unsigned long index; |
| 1061 | int ret = 0; |
| 1062 | |
| 1063 | xa_for_each_marked(&memory_groups, index, group, |
| 1064 | MEMORY_GROUP_MARK_DYNAMIC) { |
| 1065 | if (group == excluded) |
| 1066 | continue; |
| 1067 | #ifdef CONFIG_NUMA |
| 1068 | if (nid != NUMA_NO_NODE && group->nid != nid) |
| 1069 | continue; |
| 1070 | #endif /* CONFIG_NUMA */ |
| 1071 | ret = func(group, arg); |
| 1072 | if (ret) |
| 1073 | break; |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |