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> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 24 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 25 | #include <linux/atomic.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 27 | |
| 28 | #define MEMORY_CLASS_NAME "memory" |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 29 | |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 30 | #define to_memory_block(dev) container_of(dev, struct memory_block, dev) |
| 31 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 32 | static int sections_per_block; |
| 33 | |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 34 | static inline unsigned long base_memory_block_id(unsigned long section_nr) |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 35 | { |
| 36 | return section_nr / sections_per_block; |
| 37 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 38 | |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 39 | static inline unsigned long pfn_to_block_id(unsigned long pfn) |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 40 | { |
| 41 | return base_memory_block_id(pfn_to_section_nr(pfn)); |
| 42 | } |
| 43 | |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 44 | static inline unsigned long phys_to_block_id(unsigned long phys) |
| 45 | { |
| 46 | return pfn_to_block_id(PFN_DOWN(phys)); |
| 47 | } |
| 48 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 49 | static int memory_subsys_online(struct device *dev); |
| 50 | static int memory_subsys_offline(struct device *dev); |
| 51 | |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 52 | static struct bus_type memory_subsys = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 53 | .name = MEMORY_CLASS_NAME, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 54 | .dev_name = MEMORY_CLASS_NAME, |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 55 | .online = memory_subsys_online, |
| 56 | .offline = memory_subsys_offline, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 59 | static BLOCKING_NOTIFIER_HEAD(memory_chain); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 60 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 61 | int register_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 62 | { |
Ioana Ciornei | 2aeebca | 2015-03-08 12:48:35 +0200 | [diff] [blame] | 63 | return blocking_notifier_chain_register(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 64 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 65 | EXPORT_SYMBOL(register_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 66 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 67 | void unregister_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 68 | { |
Ioana Ciornei | 2aeebca | 2015-03-08 12:48:35 +0200 | [diff] [blame] | 69 | blocking_notifier_chain_unregister(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 70 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 71 | EXPORT_SYMBOL(unregister_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 72 | |
Yasuaki Ishimatsu | fa7194e | 2012-12-11 16:00:44 -0800 | [diff] [blame] | 73 | static void memory_block_release(struct device *dev) |
| 74 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 75 | struct memory_block *mem = to_memory_block(dev); |
Yasuaki Ishimatsu | fa7194e | 2012-12-11 16:00:44 -0800 | [diff] [blame] | 76 | |
| 77 | kfree(mem); |
| 78 | } |
| 79 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 80 | unsigned long __weak memory_block_size_bytes(void) |
| 81 | { |
| 82 | return MIN_MEMORY_BLOCK_SIZE; |
| 83 | } |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(memory_block_size_bytes); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 85 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 86 | /* |
David Hildenbrand | f915fb7 | 2019-09-23 15:35:43 -0700 | [diff] [blame] | 87 | * Show the first physical section index (number) of this memory block. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 88 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 89 | static ssize_t phys_index_show(struct device *dev, |
| 90 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 91 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 92 | struct memory_block *mem = to_memory_block(dev); |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 93 | unsigned long phys_index; |
| 94 | |
| 95 | phys_index = mem->start_section_nr / sections_per_block; |
| 96 | return sprintf(buf, "%08lx\n", phys_index); |
| 97 | } |
| 98 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 99 | /* |
David Hildenbrand | 53cdc1c | 2020-03-28 19:17:19 -0700 | [diff] [blame] | 100 | * Legacy interface that we cannot remove. Always indicate "removable" |
| 101 | * with CONFIG_MEMORY_HOTREMOVE - bad heuristic. |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 102 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 103 | static ssize_t removable_show(struct device *dev, struct device_attribute *attr, |
| 104 | char *buf) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 105 | { |
David Hildenbrand | 53cdc1c | 2020-03-28 19:17:19 -0700 | [diff] [blame] | 106 | return sprintf(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 110 | * online, offline, going offline, etc. |
| 111 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 112 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 113 | char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 114 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 115 | struct memory_block *mem = to_memory_block(dev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 116 | ssize_t len = 0; |
| 117 | |
| 118 | /* |
| 119 | * We can probably put these states in a nice little array |
| 120 | * so that they're not open-coded |
| 121 | */ |
| 122 | switch (mem->state) { |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 123 | case MEM_ONLINE: |
| 124 | len = sprintf(buf, "online\n"); |
| 125 | break; |
| 126 | case MEM_OFFLINE: |
| 127 | len = sprintf(buf, "offline\n"); |
| 128 | break; |
| 129 | case MEM_GOING_OFFLINE: |
| 130 | len = sprintf(buf, "going-offline\n"); |
| 131 | break; |
| 132 | default: |
| 133 | len = sprintf(buf, "ERROR-UNKNOWN-%ld\n", |
| 134 | mem->state); |
| 135 | WARN_ON(1); |
| 136 | break; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return len; |
| 140 | } |
| 141 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 142 | int memory_notify(unsigned long val, void *v) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 143 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 144 | return blocking_notifier_call_chain(&memory_chain, val, v); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 148 | * The probe routines leave the pages uninitialized, just as the bootmem code |
| 149 | * does. Make sure we do not access them, but instead use only information from |
| 150 | * within sections. |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 151 | */ |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 152 | static bool pages_correctly_probed(unsigned long start_pfn) |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 153 | { |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 154 | unsigned long section_nr = pfn_to_section_nr(start_pfn); |
| 155 | unsigned long section_nr_end = section_nr + sections_per_block; |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 156 | unsigned long pfn = start_pfn; |
| 157 | |
| 158 | /* |
| 159 | * memmap between sections is not contiguous except with |
| 160 | * SPARSEMEM_VMEMMAP. We lookup the page once per section |
| 161 | * and assume memmap is contiguous within each section |
| 162 | */ |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 163 | for (; section_nr < section_nr_end; section_nr++) { |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 164 | if (WARN_ON_ONCE(!pfn_valid(pfn))) |
| 165 | return false; |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 166 | |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 167 | if (!present_section_nr(section_nr)) { |
Michal Hocko | 1ecc07f | 2018-12-28 00:39:34 -0800 | [diff] [blame] | 168 | pr_warn("section %ld pfn[%lx, %lx) not present\n", |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 169 | section_nr, pfn, pfn + PAGES_PER_SECTION); |
| 170 | return false; |
| 171 | } else if (!valid_section_nr(section_nr)) { |
Michal Hocko | 1ecc07f | 2018-12-28 00:39:34 -0800 | [diff] [blame] | 172 | pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n", |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 173 | section_nr, pfn, pfn + PAGES_PER_SECTION); |
| 174 | return false; |
| 175 | } else if (online_section_nr(section_nr)) { |
Michal Hocko | 1ecc07f | 2018-12-28 00:39:34 -0800 | [diff] [blame] | 176 | pr_warn("section %ld pfn[%lx, %lx) is already online\n", |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 177 | section_nr, pfn, pfn + PAGES_PER_SECTION); |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 178 | return false; |
| 179 | } |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 180 | pfn += PAGES_PER_SECTION; |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 187 | * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is |
| 188 | * OK to have direct references to sparsemem variables in here. |
| 189 | */ |
| 190 | static int |
Baoquan He | 063b8a4 | 2019-05-13 17:19:35 -0700 | [diff] [blame] | 191 | memory_block_action(unsigned long start_section_nr, unsigned long action, |
David Hildenbrand | bd5c234 | 2020-01-30 22:14:54 -0800 | [diff] [blame] | 192 | int online_type, int nid) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 193 | { |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 194 | unsigned long start_pfn; |
Anton Blanchard | 5409d2c | 2011-05-11 17:25:14 +1000 | [diff] [blame] | 195 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 196 | int ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 197 | |
Baoquan He | 063b8a4 | 2019-05-13 17:19:35 -0700 | [diff] [blame] | 198 | start_pfn = section_nr_to_pfn(start_section_nr); |
Greg Kroah-Hartman | de0ed36 | 2011-10-18 14:00:57 -0700 | [diff] [blame] | 199 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 200 | switch (action) { |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 201 | case MEM_ONLINE: |
Pavel Tatashin | b77eab7 | 2018-04-05 16:22:52 -0700 | [diff] [blame] | 202 | if (!pages_correctly_probed(start_pfn)) |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 203 | return -EBUSY; |
Mel Gorman | 2bbcb878 | 2011-10-17 16:38:20 +0200 | [diff] [blame] | 204 | |
David Hildenbrand | bd5c234 | 2020-01-30 22:14:54 -0800 | [diff] [blame] | 205 | ret = online_pages(start_pfn, nr_pages, online_type, nid); |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 206 | break; |
| 207 | case MEM_OFFLINE: |
| 208 | ret = offline_pages(start_pfn, nr_pages); |
| 209 | break; |
| 210 | default: |
| 211 | WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: " |
Baoquan He | 063b8a4 | 2019-05-13 17:19:35 -0700 | [diff] [blame] | 212 | "%ld\n", __func__, start_section_nr, action, action); |
Ioana Ciornei | 3d3af6a | 2015-03-08 12:29:04 +0200 | [diff] [blame] | 213 | ret = -EINVAL; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 214 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
Nathan Fontenot | dc18d70 | 2017-02-24 15:00:02 -0800 | [diff] [blame] | 219 | static int memory_block_change_state(struct memory_block *mem, |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 220 | unsigned long to_state, unsigned long from_state_req) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 221 | { |
Greg Kroah-Hartman | de0ed36 | 2011-10-18 14:00:57 -0700 | [diff] [blame] | 222 | int ret = 0; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 223 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 224 | if (mem->state != from_state_req) |
| 225 | return -EINVAL; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 226 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 227 | if (to_state == MEM_OFFLINE) |
| 228 | mem->state = MEM_GOING_OFFLINE; |
| 229 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 230 | ret = memory_block_action(mem->start_section_nr, to_state, |
David Hildenbrand | bd5c234 | 2020-01-30 22:14:54 -0800 | [diff] [blame] | 231 | mem->online_type, mem->nid); |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 232 | |
Rafael J. Wysocki | b2c064b | 2013-05-23 10:38:55 +0200 | [diff] [blame] | 233 | mem->state = ret ? from_state_req : to_state; |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 234 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 235 | return ret; |
| 236 | } |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 237 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 238 | /* The device lock serializes operations on memory_subsys_[online|offline] */ |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 239 | static int memory_subsys_online(struct device *dev) |
| 240 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 241 | struct memory_block *mem = to_memory_block(dev); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 242 | int ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 243 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 244 | if (mem->state == MEM_ONLINE) |
| 245 | return 0; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 246 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 247 | /* |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 248 | * If we are called from state_store(), online_type will be |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 249 | * set >= 0 Otherwise we were called from the device online |
| 250 | * attribute and need to set the online_type. |
| 251 | */ |
| 252 | if (mem->online_type < 0) |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 253 | mem->online_type = MMOP_ONLINE_KEEP; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 254 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 255 | ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
| 256 | |
| 257 | /* clear online_type */ |
| 258 | mem->online_type = -1; |
| 259 | |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static int memory_subsys_offline(struct device *dev) |
| 264 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 265 | struct memory_block *mem = to_memory_block(dev); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 266 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 267 | if (mem->state == MEM_OFFLINE) |
| 268 | return 0; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 269 | |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 270 | return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 271 | } |
| 272 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 273 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 274 | const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 275 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 276 | struct memory_block *mem = to_memory_block(dev); |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 277 | int ret, online_type; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 278 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 279 | ret = lock_device_hotplug_sysfs(); |
| 280 | if (ret) |
| 281 | return ret; |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 282 | |
Tang Chen | 1f6a6cc | 2014-08-06 16:05:11 -0700 | [diff] [blame] | 283 | if (sysfs_streq(buf, "online_kernel")) |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 284 | online_type = MMOP_ONLINE_KERNEL; |
Tang Chen | 1f6a6cc | 2014-08-06 16:05:11 -0700 | [diff] [blame] | 285 | else if (sysfs_streq(buf, "online_movable")) |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 286 | online_type = MMOP_ONLINE_MOVABLE; |
Tang Chen | 1f6a6cc | 2014-08-06 16:05:11 -0700 | [diff] [blame] | 287 | else if (sysfs_streq(buf, "online")) |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 288 | online_type = MMOP_ONLINE_KEEP; |
Tang Chen | 1f6a6cc | 2014-08-06 16:05:11 -0700 | [diff] [blame] | 289 | else if (sysfs_streq(buf, "offline")) |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 290 | online_type = MMOP_OFFLINE; |
Yasuaki Ishimatsu | a37f863 | 2013-10-11 15:36:25 +0900 | [diff] [blame] | 291 | else { |
| 292 | ret = -EINVAL; |
| 293 | goto err; |
| 294 | } |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 295 | |
| 296 | switch (online_type) { |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 297 | case MMOP_ONLINE_KERNEL: |
| 298 | case MMOP_ONLINE_MOVABLE: |
| 299 | case MMOP_ONLINE_KEEP: |
David Hildenbrand | 381eab4 | 2018-10-30 15:10:29 -0700 | [diff] [blame] | 300 | /* mem->online_type is protected by device_hotplug_lock */ |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 301 | mem->online_type = online_type; |
| 302 | ret = device_online(&mem->dev); |
| 303 | break; |
Tang Chen | 4f7c6b4 | 2014-08-06 16:05:13 -0700 | [diff] [blame] | 304 | case MMOP_OFFLINE: |
Seth Jennings | fa2be40 | 2013-08-20 16:05:05 -0500 | [diff] [blame] | 305 | ret = device_offline(&mem->dev); |
| 306 | break; |
| 307 | default: |
| 308 | ret = -EINVAL; /* should never happen */ |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 309 | } |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 310 | |
Yasuaki Ishimatsu | a37f863 | 2013-10-11 15:36:25 +0900 | [diff] [blame] | 311 | err: |
Rafael J. Wysocki | 4960e05 | 2013-05-08 14:18:37 +0200 | [diff] [blame] | 312 | unlock_device_hotplug(); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 313 | |
Reza Arbab | d66ba15 | 2016-10-07 17:00:15 -0700 | [diff] [blame] | 314 | if (ret < 0) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 315 | return ret; |
Reza Arbab | d66ba15 | 2016-10-07 17:00:15 -0700 | [diff] [blame] | 316 | if (ret) |
| 317 | return -EINVAL; |
| 318 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 319 | return count; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * phys_device is a bad name for this. What I really want |
| 324 | * is a way to differentiate between memory ranges that |
| 325 | * are part of physical devices that constitute |
| 326 | * a complete removable unit or fru. |
| 327 | * i.e. do these ranges belong to the same physical device, |
| 328 | * s.t. if I offline all of these sections I can then |
| 329 | * remove the physical device? |
| 330 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 331 | static ssize_t phys_device_show(struct device *dev, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 332 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 333 | { |
Gu Zheng | 7315f0c | 2013-08-28 14:38:27 +0800 | [diff] [blame] | 334 | struct memory_block *mem = to_memory_block(dev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 335 | return sprintf(buf, "%d\n", mem->phys_device); |
| 336 | } |
| 337 | |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 338 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 339 | static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn, |
| 340 | unsigned long nr_pages, int online_type, |
| 341 | struct zone *default_zone) |
| 342 | { |
| 343 | struct zone *zone; |
| 344 | |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 345 | zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages); |
| 346 | if (zone != default_zone) { |
| 347 | strcat(buf, " "); |
| 348 | strcat(buf, zone->name); |
| 349 | } |
| 350 | } |
| 351 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 352 | static ssize_t valid_zones_show(struct device *dev, |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 353 | struct device_attribute *attr, char *buf) |
| 354 | { |
| 355 | struct memory_block *mem = to_memory_block(dev); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 356 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 357 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 358 | struct zone *default_zone; |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 359 | int nid; |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 360 | |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 361 | /* |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 362 | * Check the existing zone. Make sure that we do that only on the |
| 363 | * online nodes otherwise the page_zone is not reliable |
| 364 | */ |
| 365 | if (mem->state == MEM_ONLINE) { |
Mikhail Zaslonko | 4e8346d | 2018-09-04 15:46:09 -0700 | [diff] [blame] | 366 | /* |
| 367 | * The block contains more than one zone can not be offlined. |
| 368 | * This can happen e.g. for ZONE_DMA and ZONE_DMA32 |
| 369 | */ |
David Hildenbrand | 9291799 | 2020-02-03 17:34:26 -0800 | [diff] [blame] | 370 | default_zone = test_pages_in_a_zone(start_pfn, |
| 371 | start_pfn + nr_pages); |
| 372 | if (!default_zone) |
Mikhail Zaslonko | 4e8346d | 2018-09-04 15:46:09 -0700 | [diff] [blame] | 373 | return sprintf(buf, "none\n"); |
David Hildenbrand | 9291799 | 2020-02-03 17:34:26 -0800 | [diff] [blame] | 374 | strcat(buf, default_zone->name); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 375 | goto out; |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Mikhail Zaslonko | 4e8346d | 2018-09-04 15:46:09 -0700 | [diff] [blame] | 378 | nid = mem->nid; |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 379 | default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages); |
| 380 | strcat(buf, default_zone->name); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 381 | |
Michal Hocko | e5e6893 | 2017-09-06 16:19:37 -0700 | [diff] [blame] | 382 | print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL, |
| 383 | default_zone); |
| 384 | print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE, |
| 385 | default_zone); |
Michal Hocko | f1dd2cd | 2017-07-06 15:38:11 -0700 | [diff] [blame] | 386 | out: |
Reza Arbab | a371d9f | 2016-07-26 15:22:27 -0700 | [diff] [blame] | 387 | strcat(buf, "\n"); |
| 388 | |
| 389 | return strlen(buf); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 390 | } |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 391 | static DEVICE_ATTR_RO(valid_zones); |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 392 | #endif |
| 393 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 394 | static DEVICE_ATTR_RO(phys_index); |
| 395 | static DEVICE_ATTR_RW(state); |
| 396 | static DEVICE_ATTR_RO(phys_device); |
| 397 | static DEVICE_ATTR_RO(removable); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 398 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 399 | /* |
David Hildenbrand | f915fb7 | 2019-09-23 15:35:43 -0700 | [diff] [blame] | 400 | * Show the memory block size (shared by all memory blocks). |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 401 | */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 402 | static ssize_t block_size_bytes_show(struct device *dev, |
| 403 | struct device_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 404 | { |
David Hildenbrand | 902ce63b | 2019-09-23 15:35:46 -0700 | [diff] [blame] | 405 | return sprintf(buf, "%lx\n", memory_block_size_bytes()); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 406 | } |
| 407 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 408 | static DEVICE_ATTR_RO(block_size_bytes); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 409 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 410 | /* |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 411 | * Memory auto online policy. |
| 412 | */ |
| 413 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 414 | static ssize_t auto_online_blocks_show(struct device *dev, |
| 415 | struct device_attribute *attr, char *buf) |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 416 | { |
| 417 | if (memhp_auto_online) |
| 418 | return sprintf(buf, "online\n"); |
| 419 | else |
| 420 | return sprintf(buf, "offline\n"); |
| 421 | } |
| 422 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 423 | static ssize_t auto_online_blocks_store(struct device *dev, |
| 424 | struct device_attribute *attr, |
| 425 | const char *buf, size_t count) |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 426 | { |
| 427 | if (sysfs_streq(buf, "online")) |
| 428 | memhp_auto_online = true; |
| 429 | else if (sysfs_streq(buf, "offline")) |
| 430 | memhp_auto_online = false; |
| 431 | else |
| 432 | return -EINVAL; |
| 433 | |
| 434 | return count; |
| 435 | } |
| 436 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 437 | static DEVICE_ATTR_RW(auto_online_blocks); |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 438 | |
| 439 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 440 | * Some architectures will have custom drivers to do this, and |
| 441 | * will not need to do it from userspace. The fake hot-add code |
| 442 | * as well as ppc64 will do all of their discovery in userspace |
| 443 | * and will require this interface. |
| 444 | */ |
| 445 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 446 | static ssize_t probe_store(struct device *dev, struct device_attribute *attr, |
| 447 | const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 448 | { |
| 449 | u64 phys_addr; |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 450 | int nid, ret; |
Anton Blanchard | 61b94fe | 2011-09-15 06:26:15 +1000 | [diff] [blame] | 451 | unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 452 | |
Zhang Zhen | b69deb2 | 2014-08-06 16:06:06 -0700 | [diff] [blame] | 453 | ret = kstrtoull(buf, 0, &phys_addr); |
| 454 | if (ret) |
| 455 | return ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 456 | |
Anton Blanchard | 61b94fe | 2011-09-15 06:26:15 +1000 | [diff] [blame] | 457 | if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1)) |
| 458 | return -EINVAL; |
| 459 | |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 460 | ret = lock_device_hotplug_sysfs(); |
| 461 | if (ret) |
zhong jiang | 3780384 | 2019-04-18 17:50:16 -0700 | [diff] [blame] | 462 | return ret; |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 463 | |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 464 | nid = memory_add_physaddr_to_nid(phys_addr); |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 465 | ret = __add_memory(nid, phys_addr, |
| 466 | MIN_MEMORY_BLOCK_SIZE * sections_per_block); |
Nathan Fontenot | 6add7cd | 2011-01-31 10:55:23 -0600 | [diff] [blame] | 467 | |
John Allen | cb5490a | 2016-01-14 15:22:16 -0800 | [diff] [blame] | 468 | if (ret) |
| 469 | goto out; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 470 | |
Nikanth Karthikesan | 9f0af69 | 2011-03-24 11:46:18 +0530 | [diff] [blame] | 471 | ret = count; |
| 472 | out: |
David Hildenbrand | 8df1d0e | 2018-10-30 15:10:24 -0700 | [diff] [blame] | 473 | unlock_device_hotplug(); |
Nikanth Karthikesan | 9f0af69 | 2011-03-24 11:46:18 +0530 | [diff] [blame] | 474 | return ret; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 475 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 476 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 477 | static DEVICE_ATTR_WO(probe); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 478 | #endif |
| 479 | |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 480 | #ifdef CONFIG_MEMORY_FAILURE |
| 481 | /* |
| 482 | * Support for offlining pages of memory |
| 483 | */ |
| 484 | |
| 485 | /* Soft offline a page */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 486 | static ssize_t soft_offline_page_store(struct device *dev, |
| 487 | struct device_attribute *attr, |
| 488 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 489 | { |
| 490 | int ret; |
| 491 | u64 pfn; |
| 492 | if (!capable(CAP_SYS_ADMIN)) |
| 493 | return -EPERM; |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 494 | if (kstrtoull(buf, 0, &pfn) < 0) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 495 | return -EINVAL; |
| 496 | pfn >>= PAGE_SHIFT; |
Naoya Horiguchi | feec24a | 2019-11-30 17:53:38 -0800 | [diff] [blame] | 497 | ret = soft_offline_page(pfn, 0); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 498 | return ret == 0 ? count : ret; |
| 499 | } |
| 500 | |
| 501 | /* Forcibly offline a page, including killing processes. */ |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 502 | static ssize_t hard_offline_page_store(struct device *dev, |
| 503 | struct device_attribute *attr, |
| 504 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 505 | { |
| 506 | int ret; |
| 507 | u64 pfn; |
| 508 | if (!capable(CAP_SYS_ADMIN)) |
| 509 | return -EPERM; |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 510 | if (kstrtoull(buf, 0, &pfn) < 0) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 511 | return -EINVAL; |
| 512 | pfn >>= PAGE_SHIFT; |
Eric W. Biederman | 83b5753 | 2017-07-09 18:14:01 -0500 | [diff] [blame] | 513 | ret = memory_failure(pfn, 0); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 514 | return ret ? ret : count; |
| 515 | } |
| 516 | |
David Hildenbrand | 3f8e917 | 2018-12-03 12:16:11 +0100 | [diff] [blame] | 517 | static DEVICE_ATTR_WO(soft_offline_page); |
| 518 | static DEVICE_ATTR_WO(hard_offline_page); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 519 | #endif |
| 520 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 521 | /* |
| 522 | * Note that phys_device is optional. It is here to allow for |
| 523 | * differentiation between which *physical* devices each |
| 524 | * section belongs to... |
| 525 | */ |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 526 | int __weak arch_get_memory_phys_device(unsigned long start_pfn) |
| 527 | { |
| 528 | return 0; |
| 529 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 530 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 531 | /* A reference for the returned memory block device is acquired. */ |
| 532 | 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] | 533 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 534 | struct device *dev; |
Robin Holt | 9838303 | 2010-09-29 14:00:55 -0500 | [diff] [blame] | 535 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 536 | dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL); |
| 537 | return dev ? to_memory_block(dev) : NULL; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 540 | /* |
| 541 | * For now, we have a linear search to go find the appropriate |
| 542 | * memory_block corresponding to a particular phys_index. If |
| 543 | * this gets to be a real problem, we can always use a radix |
| 544 | * tree or something here. |
| 545 | * |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 546 | * This could be made generic for all device subsystems. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 547 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 548 | struct memory_block *find_memory_block(struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 549 | { |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 550 | unsigned long block_id = base_memory_block_id(__section_nr(section)); |
| 551 | |
| 552 | return find_memory_block_by_id(block_id); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 555 | static struct attribute *memory_memblk_attrs[] = { |
| 556 | &dev_attr_phys_index.attr, |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 557 | &dev_attr_state.attr, |
| 558 | &dev_attr_phys_device.attr, |
| 559 | &dev_attr_removable.attr, |
Zhang Zhen | ed2f240 | 2014-10-09 15:26:31 -0700 | [diff] [blame] | 560 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 561 | &dev_attr_valid_zones.attr, |
| 562 | #endif |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 563 | NULL |
| 564 | }; |
| 565 | |
| 566 | static struct attribute_group memory_memblk_attr_group = { |
| 567 | .attrs = memory_memblk_attrs, |
| 568 | }; |
| 569 | |
| 570 | static const struct attribute_group *memory_memblk_attr_groups[] = { |
| 571 | &memory_memblk_attr_group, |
| 572 | NULL, |
| 573 | }; |
| 574 | |
| 575 | /* |
| 576 | * register_memory - Setup a sysfs device for a memory block |
| 577 | */ |
| 578 | static |
| 579 | int register_memory(struct memory_block *memory) |
| 580 | { |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 581 | int ret; |
| 582 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 583 | memory->dev.bus = &memory_subsys; |
| 584 | memory->dev.id = memory->start_section_nr / sections_per_block; |
| 585 | memory->dev.release = memory_block_release; |
| 586 | memory->dev.groups = memory_memblk_attr_groups; |
Linus Torvalds | f991fae | 2013-07-03 14:35:40 -0700 | [diff] [blame] | 587 | memory->dev.offline = memory->state == MEM_OFFLINE; |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 588 | |
Arvind Yadav | 085aa2d | 2018-04-26 21:12:09 +0530 | [diff] [blame] | 589 | ret = device_register(&memory->dev); |
| 590 | if (ret) |
| 591 | put_device(&memory->dev); |
| 592 | |
| 593 | return ret; |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 594 | } |
| 595 | |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 596 | static int init_memory_block(struct memory_block **memory, |
| 597 | unsigned long block_id, unsigned long state) |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 598 | { |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 599 | struct memory_block *mem; |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 600 | unsigned long start_pfn; |
| 601 | int ret = 0; |
| 602 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 603 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 604 | if (mem) { |
| 605 | put_device(&mem->dev); |
| 606 | return -EEXIST; |
| 607 | } |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 608 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 609 | if (!mem) |
| 610 | return -ENOMEM; |
| 611 | |
David Hildenbrand | 1811582 | 2019-07-18 15:56:46 -0700 | [diff] [blame] | 612 | mem->start_section_nr = block_id * sections_per_block; |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 613 | mem->state = state; |
Nathan Fontenot | d336016 | 2011-01-20 10:44:29 -0600 | [diff] [blame] | 614 | start_pfn = section_nr_to_pfn(mem->start_section_nr); |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 615 | mem->phys_device = arch_get_memory_phys_device(start_pfn); |
David Hildenbrand | d84f2f5 | 2019-09-23 15:35:40 -0700 | [diff] [blame] | 616 | mem->nid = NUMA_NO_NODE; |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 617 | |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 618 | ret = register_memory(mem); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 619 | |
| 620 | *memory = mem; |
| 621 | return ret; |
| 622 | } |
| 623 | |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 624 | static int add_memory_block(unsigned long base_section_nr) |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 625 | { |
David Hildenbrand | 68c3a6a | 2020-04-06 20:06:40 -0700 | [diff] [blame^] | 626 | int section_count = 0; |
Seth Jennings | cb5e39b | 2013-08-20 12:13:03 -0500 | [diff] [blame] | 627 | struct memory_block *mem; |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 628 | unsigned long nr; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 629 | |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 630 | for (nr = base_section_nr; nr < base_section_nr + sections_per_block; |
| 631 | nr++) |
| 632 | if (present_section_nr(nr)) |
David Hildenbrand | 1811582 | 2019-07-18 15:56:46 -0700 | [diff] [blame] | 633 | section_count++; |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 634 | |
Seth Jennings | cb5e39b | 2013-08-20 12:13:03 -0500 | [diff] [blame] | 635 | if (section_count == 0) |
| 636 | return 0; |
David Hildenbrand | 68c3a6a | 2020-04-06 20:06:40 -0700 | [diff] [blame^] | 637 | return init_memory_block(&mem, base_memory_block_id(base_section_nr), |
| 638 | MEM_ONLINE); |
Nathan Fontenot | e4619c8 | 2010-10-19 12:44:20 -0500 | [diff] [blame] | 639 | } |
| 640 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 641 | static void unregister_memory(struct memory_block *memory) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 642 | { |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 643 | if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys)) |
| 644 | return; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 645 | |
David Hildenbrand | cb7b3a3 | 2019-05-13 17:21:37 -0700 | [diff] [blame] | 646 | /* drop the ref. we got via find_memory_block() */ |
Seth Jennings | df2b717 | 2013-08-20 12:12:59 -0500 | [diff] [blame] | 647 | put_device(&memory->dev); |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 648 | device_unregister(&memory->dev); |
| 649 | } |
| 650 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 651 | /* |
| 652 | * Create memory block devices for the given memory area. Start and size |
| 653 | * have to be aligned to memory block granularity. Memory block devices |
| 654 | * will be initialized as offline. |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 655 | * |
| 656 | * Called under device_hotplug_lock. |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 657 | */ |
| 658 | int create_memory_block_devices(unsigned long start, unsigned long size) |
| 659 | { |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 660 | const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start)); |
| 661 | 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] | 662 | struct memory_block *mem; |
| 663 | unsigned long block_id; |
| 664 | int ret = 0; |
| 665 | |
| 666 | if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) || |
| 667 | !IS_ALIGNED(size, memory_block_size_bytes()))) |
| 668 | return -EINVAL; |
| 669 | |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 670 | for (block_id = start_block_id; block_id != end_block_id; block_id++) { |
| 671 | ret = init_memory_block(&mem, block_id, MEM_OFFLINE); |
| 672 | if (ret) |
| 673 | break; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 674 | } |
| 675 | if (ret) { |
| 676 | end_block_id = block_id; |
| 677 | for (block_id = start_block_id; block_id != end_block_id; |
| 678 | block_id++) { |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 679 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 680 | if (WARN_ON_ONCE(!mem)) |
| 681 | continue; |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 682 | unregister_memory(mem); |
| 683 | } |
| 684 | } |
David Hildenbrand | db051a0 | 2019-07-18 15:56:56 -0700 | [diff] [blame] | 685 | return ret; |
| 686 | } |
| 687 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Remove memory block devices for the given memory area. Start and size |
| 690 | * have to be aligned to memory block granularity. Memory block devices |
| 691 | * have to be offline. |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 692 | * |
| 693 | * Called under device_hotplug_lock. |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 694 | */ |
| 695 | void remove_memory_block_devices(unsigned long start, unsigned long size) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 696 | { |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 697 | const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start)); |
| 698 | 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] | 699 | struct memory_block *mem; |
David Hildenbrand | 90ec010f | 2019-07-18 15:57:40 -0700 | [diff] [blame] | 700 | unsigned long block_id; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 701 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 702 | if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) || |
| 703 | !IS_ALIGNED(size, memory_block_size_bytes()))) |
David Hildenbrand | cb7b3a3 | 2019-05-13 17:21:37 -0700 | [diff] [blame] | 704 | return; |
| 705 | |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 706 | 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] | 707 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 708 | if (WARN_ON_ONCE(!mem)) |
| 709 | continue; |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 710 | unregister_memory_block_under_nodes(mem); |
Nathan Fontenot | 0c2c99b | 2011-01-20 10:43:34 -0600 | [diff] [blame] | 711 | unregister_memory(mem); |
David Hildenbrand | 4c4b7f9 | 2019-07-18 15:57:06 -0700 | [diff] [blame] | 712 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Yasuaki Ishimatsu | 6677e3e | 2013-02-22 16:32:52 -0800 | [diff] [blame] | 715 | /* return true if the memory block is offlined, otherwise, return false */ |
| 716 | bool is_memblock_offlined(struct memory_block *mem) |
| 717 | { |
| 718 | return mem->state == MEM_OFFLINE; |
| 719 | } |
| 720 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 721 | static struct attribute *memory_root_attrs[] = { |
| 722 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
| 723 | &dev_attr_probe.attr, |
| 724 | #endif |
| 725 | |
| 726 | #ifdef CONFIG_MEMORY_FAILURE |
| 727 | &dev_attr_soft_offline_page.attr, |
| 728 | &dev_attr_hard_offline_page.attr, |
| 729 | #endif |
| 730 | |
| 731 | &dev_attr_block_size_bytes.attr, |
Vitaly Kuznetsov | 31bc385 | 2016-03-15 14:56:48 -0700 | [diff] [blame] | 732 | &dev_attr_auto_online_blocks.attr, |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 733 | NULL |
| 734 | }; |
| 735 | |
| 736 | static struct attribute_group memory_root_attr_group = { |
| 737 | .attrs = memory_root_attrs, |
| 738 | }; |
| 739 | |
| 740 | static const struct attribute_group *memory_root_attr_groups[] = { |
| 741 | &memory_root_attr_group, |
| 742 | NULL, |
| 743 | }; |
| 744 | |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 745 | /* |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 746 | * Initialize the sysfs support for memory devices. At the time this function |
| 747 | * is called, we cannot have concurrent creation/deletion of memory block |
| 748 | * devices, the device_hotplug_lock is not needed. |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 749 | */ |
David Hildenbrand | 902ce63b | 2019-09-23 15:35:46 -0700 | [diff] [blame] | 750 | void __init memory_dev_init(void) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 751 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 752 | int ret; |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 753 | unsigned long block_sz, nr; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 754 | |
David Hildenbrand | 902ce63b | 2019-09-23 15:35:46 -0700 | [diff] [blame] | 755 | /* Validate the configured memory block size */ |
| 756 | block_sz = memory_block_size_bytes(); |
| 757 | if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE) |
| 758 | panic("Memory block size not suitable: 0x%lx\n", block_sz); |
| 759 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 760 | |
Nathan Fontenot | 96b2c0f | 2013-06-04 14:42:28 -0500 | [diff] [blame] | 761 | ret = subsys_system_register(&memory_subsys, memory_root_attr_groups); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 762 | if (ret) |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 763 | panic("%s() failed to register subsystem: %d\n", __func__, ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * Create entries for memory sections that were found |
| 767 | * during boot and have been initialized |
| 768 | */ |
David Hildenbrand | 2491f0a | 2019-07-18 15:57:37 -0700 | [diff] [blame] | 769 | for (nr = 0; nr <= __highest_present_section_nr; |
| 770 | nr += sections_per_block) { |
David Hildenbrand | 848e19a | 2019-11-30 17:54:14 -0800 | [diff] [blame] | 771 | ret = add_memory_block(nr); |
| 772 | if (ret) |
| 773 | panic("%s() failed to add memory block: %d\n", __func__, |
| 774 | ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 775 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 776 | } |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 777 | |
| 778 | /** |
| 779 | * walk_memory_blocks - walk through all present memory blocks overlapped |
| 780 | * by the range [start, start + size) |
| 781 | * |
| 782 | * @start: start address of the memory range |
| 783 | * @size: size of the memory range |
| 784 | * @arg: argument passed to func |
| 785 | * @func: callback for each memory section walked |
| 786 | * |
| 787 | * This function walks through all present memory blocks overlapped by the |
| 788 | * range [start, start + size), calling func on each memory block. |
| 789 | * |
| 790 | * In case func() returns an error, walking is aborted and the error is |
| 791 | * returned. |
| 792 | */ |
| 793 | int walk_memory_blocks(unsigned long start, unsigned long size, |
| 794 | void *arg, walk_memory_blocks_func_t func) |
| 795 | { |
| 796 | const unsigned long start_block_id = phys_to_block_id(start); |
| 797 | const unsigned long end_block_id = phys_to_block_id(start + size - 1); |
| 798 | struct memory_block *mem; |
| 799 | unsigned long block_id; |
| 800 | int ret = 0; |
| 801 | |
David Hildenbrand | dd62528 | 2019-07-18 15:57:53 -0700 | [diff] [blame] | 802 | if (!size) |
| 803 | return 0; |
| 804 | |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 805 | 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] | 806 | mem = find_memory_block_by_id(block_id); |
David Hildenbrand | ea88464 | 2019-07-18 15:57:50 -0700 | [diff] [blame] | 807 | if (!mem) |
| 808 | continue; |
| 809 | |
| 810 | ret = func(mem, arg); |
| 811 | put_device(&mem->dev); |
| 812 | if (ret) |
| 813 | break; |
| 814 | } |
| 815 | return ret; |
| 816 | } |
David Hildenbrand | 2c91f8f | 2019-11-15 17:34:57 -0800 | [diff] [blame] | 817 | |
| 818 | struct for_each_memory_block_cb_data { |
| 819 | walk_memory_blocks_func_t func; |
| 820 | void *arg; |
| 821 | }; |
| 822 | |
| 823 | static int for_each_memory_block_cb(struct device *dev, void *data) |
| 824 | { |
| 825 | struct memory_block *mem = to_memory_block(dev); |
| 826 | struct for_each_memory_block_cb_data *cb_data = data; |
| 827 | |
| 828 | return cb_data->func(mem, cb_data->arg); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * for_each_memory_block - walk through all present memory blocks |
| 833 | * |
| 834 | * @arg: argument passed to func |
| 835 | * @func: callback for each memory block walked |
| 836 | * |
| 837 | * This function walks through all present memory blocks, calling func on |
| 838 | * each memory block. |
| 839 | * |
| 840 | * In case func() returns an error, walking is aborted and the error is |
| 841 | * returned. |
| 842 | */ |
| 843 | int for_each_memory_block(void *arg, walk_memory_blocks_func_t func) |
| 844 | { |
| 845 | struct for_each_memory_block_cb_data cb_data = { |
| 846 | .func = func, |
| 847 | .arg = arg, |
| 848 | }; |
| 849 | |
| 850 | return bus_for_each_dev(&memory_subsys, NULL, &cb_data, |
| 851 | for_each_memory_block_cb); |
| 852 | } |