blob: 2b09b68b9f78cae8c7b0f0d42b60042dbae12e8c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Hansen3947be12005-10-29 18:16:54 -07002/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003 * Memory subsystem support
Dave Hansen3947be12005-10-29 18:16:54 -07004 *
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 Hansen3947be12005-10-29 18:16:54 -070014#include <linux/module.h>
15#include <linux/init.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/topology.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Dave Hansen3947be12005-10-29 18:16:54 -070018#include <linux/device.h>
19#include <linux/memory.h>
Dave Hansen3947be12005-10-29 18:16:54 -070020#include <linux/memory_hotplug.h>
21#include <linux/mm.h>
Shaohua Li9f1b16a2008-10-18 20:27:12 -070022#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Scott Cheloha4fb6eab2020-06-03 16:03:48 -070024#include <linux/xarray.h>
Shaohua Li9f1b16a2008-10-18 20:27:12 -070025
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Dave Hansen3947be12005-10-29 18:16:54 -070028
29#define MEMORY_CLASS_NAME "memory"
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060030
David Hildenbrand4dc82072020-04-06 20:07:24 -070031static 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
David Hildenbrand5f47adf2020-04-06 20:07:44 -070038int memhp_online_type_from_str(const char *str)
David Hildenbrand4dc82072020-04-06 20:07:24 -070039{
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 Zheng7315f0c2013-08-28 14:38:27 +080049#define to_memory_block(dev) container_of(dev, struct memory_block, dev)
50
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060051static int sections_per_block;
52
David Hildenbrand90ec010f2019-07-18 15:57:40 -070053static inline unsigned long base_memory_block_id(unsigned long section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060054{
55 return section_nr / sections_per_block;
56}
Dave Hansen3947be12005-10-29 18:16:54 -070057
David Hildenbrand90ec010f2019-07-18 15:57:40 -070058static inline unsigned long pfn_to_block_id(unsigned long pfn)
David Hildenbranddb051a02019-07-18 15:56:56 -070059{
60 return base_memory_block_id(pfn_to_section_nr(pfn));
61}
62
David Hildenbrandea884642019-07-18 15:57:50 -070063static inline unsigned long phys_to_block_id(unsigned long phys)
64{
65 return pfn_to_block_id(PFN_DOWN(phys));
66}
67
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020068static int memory_subsys_online(struct device *dev);
69static int memory_subsys_offline(struct device *dev);
70
Kay Sievers10fbcf42011-12-21 14:48:43 -080071static struct bus_type memory_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +010072 .name = MEMORY_CLASS_NAME,
Kay Sievers10fbcf42011-12-21 14:48:43 -080073 .dev_name = MEMORY_CLASS_NAME,
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020074 .online = memory_subsys_online,
75 .offline = memory_subsys_offline,
Dave Hansen3947be12005-10-29 18:16:54 -070076};
77
Scott Cheloha4fb6eab2020-06-03 16:03:48 -070078/*
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 */
83static DEFINE_XARRAY(memory_blocks);
84
Alan Sterne041c682006-03-27 01:16:30 -080085static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070086
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080087int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070088{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020089 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070090}
Hannes Hering3c82c302008-05-07 14:43:01 +020091EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070092
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080093void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070094{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020095 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070096}
Hannes Hering3c82c302008-05-07 14:43:01 +020097EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070098
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080099static void memory_block_release(struct device *dev)
100{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800101 struct memory_block *mem = to_memory_block(dev);
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -0800102
103 kfree(mem);
104}
105
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600106unsigned long __weak memory_block_size_bytes(void)
107{
108 return MIN_MEMORY_BLOCK_SIZE;
109}
Dave Hansenc221c0b2019-02-25 10:57:40 -0800110EXPORT_SYMBOL_GPL(memory_block_size_bytes);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600111
Dave Hansen3947be12005-10-29 18:16:54 -0700112/*
David Hildenbrandf915fb72019-09-23 15:35:43 -0700113 * Show the first physical section index (number) of this memory block.
Dave Hansen3947be12005-10-29 18:16:54 -0700114 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100115static ssize_t phys_index_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700117{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800118 struct memory_block *mem = to_memory_block(dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600119 unsigned long phys_index;
120
121 phys_index = mem->start_section_nr / sections_per_block;
122 return sprintf(buf, "%08lx\n", phys_index);
123}
124
Dave Hansen3947be12005-10-29 18:16:54 -0700125/*
David Hildenbrand53cdc1c2020-03-28 19:17:19 -0700126 * Legacy interface that we cannot remove. Always indicate "removable"
127 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700128 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100129static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700131{
David Hildenbrand53cdc1c2020-03-28 19:17:19 -0700132 return sprintf(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700133}
134
135/*
Dave Hansen3947be12005-10-29 18:16:54 -0700136 * online, offline, going offline, etc.
137 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100138static ssize_t state_show(struct device *dev, struct device_attribute *attr,
139 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700140{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800141 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700142 ssize_t len = 0;
143
144 /*
145 * We can probably put these states in a nice little array
146 * so that they're not open-coded
147 */
148 switch (mem->state) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200149 case MEM_ONLINE:
150 len = sprintf(buf, "online\n");
151 break;
152 case MEM_OFFLINE:
153 len = sprintf(buf, "offline\n");
154 break;
155 case MEM_GOING_OFFLINE:
156 len = sprintf(buf, "going-offline\n");
157 break;
158 default:
159 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
160 mem->state);
161 WARN_ON(1);
162 break;
Dave Hansen3947be12005-10-29 18:16:54 -0700163 }
164
165 return len;
166}
167
Yasunori Goto7b78d332007-10-21 16:41:36 -0700168int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700169{
Alan Sterne041c682006-03-27 01:16:30 -0800170 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700171}
172
173/*
174 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
175 * OK to have direct references to sparsemem variables in here.
176 */
177static int
Baoquan He063b8a42019-05-13 17:19:35 -0700178memory_block_action(unsigned long start_section_nr, unsigned long action,
David Hildenbrandbd5c2342020-01-30 22:14:54 -0800179 int online_type, int nid)
Dave Hansen3947be12005-10-29 18:16:54 -0700180{
Wen Congyanga16cee12012-10-08 16:33:58 -0700181 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000182 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700183 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700184
Baoquan He063b8a42019-05-13 17:19:35 -0700185 start_pfn = section_nr_to_pfn(start_section_nr);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700186
Dave Hansen3947be12005-10-29 18:16:54 -0700187 switch (action) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200188 case MEM_ONLINE:
David Hildenbrandbd5c2342020-01-30 22:14:54 -0800189 ret = online_pages(start_pfn, nr_pages, online_type, nid);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200190 break;
191 case MEM_OFFLINE:
192 ret = offline_pages(start_pfn, nr_pages);
193 break;
194 default:
195 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
Baoquan He063b8a42019-05-13 17:19:35 -0700196 "%ld\n", __func__, start_section_nr, action, action);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200197 ret = -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700198 }
Dave Hansen3947be12005-10-29 18:16:54 -0700199
200 return ret;
201}
202
Nathan Fontenotdc18d702017-02-24 15:00:02 -0800203static int memory_block_change_state(struct memory_block *mem,
Seth Jenningsfa2be402013-08-20 16:05:05 -0500204 unsigned long to_state, unsigned long from_state_req)
Dave Hansen3947be12005-10-29 18:16:54 -0700205{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700206 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600207
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200208 if (mem->state != from_state_req)
209 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700210
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600211 if (to_state == MEM_OFFLINE)
212 mem->state = MEM_GOING_OFFLINE;
213
Seth Jenningsfa2be402013-08-20 16:05:05 -0500214 ret = memory_block_action(mem->start_section_nr, to_state,
David Hildenbrandbd5c2342020-01-30 22:14:54 -0800215 mem->online_type, mem->nid);
Seth Jenningsfa2be402013-08-20 16:05:05 -0500216
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200217 mem->state = ret ? from_state_req : to_state;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500218
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200219 return ret;
220}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600221
Seth Jenningsfa2be402013-08-20 16:05:05 -0500222/* The device lock serializes operations on memory_subsys_[online|offline] */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200223static int memory_subsys_online(struct device *dev)
224{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800225 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200226 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700227
Seth Jenningsfa2be402013-08-20 16:05:05 -0500228 if (mem->state == MEM_ONLINE)
229 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200230
Seth Jenningsfa2be402013-08-20 16:05:05 -0500231 /*
David Hildenbrandefc978a2020-04-06 20:07:20 -0700232 * When called via device_online() without configuring the online_type,
233 * we want to default to MMOP_ONLINE.
Seth Jenningsfa2be402013-08-20 16:05:05 -0500234 */
David Hildenbrandefc978a2020-04-06 20:07:20 -0700235 if (mem->online_type == MMOP_OFFLINE)
David Hildenbrand956f8b42020-04-06 20:07:16 -0700236 mem->online_type = MMOP_ONLINE;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200237
Seth Jenningsfa2be402013-08-20 16:05:05 -0500238 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
David Hildenbrandefc978a2020-04-06 20:07:20 -0700239 mem->online_type = MMOP_OFFLINE;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500240
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200241 return ret;
242}
243
244static int memory_subsys_offline(struct device *dev)
245{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800246 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200247
Seth Jenningsfa2be402013-08-20 16:05:05 -0500248 if (mem->state == MEM_OFFLINE)
249 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200250
Seth Jenningsfa2be402013-08-20 16:05:05 -0500251 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200252}
253
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100254static ssize_t state_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700256{
David Hildenbrand4dc82072020-04-06 20:07:24 -0700257 const int online_type = memhp_online_type_from_str(buf);
Gu Zheng7315f0c2013-08-28 14:38:27 +0800258 struct memory_block *mem = to_memory_block(dev);
David Hildenbrand4dc82072020-04-06 20:07:24 -0700259 int ret;
260
261 if (online_type < 0)
262 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700263
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200264 ret = lock_device_hotplug_sysfs();
265 if (ret)
266 return ret;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200267
Seth Jenningsfa2be402013-08-20 16:05:05 -0500268 switch (online_type) {
Tang Chen4f7c6b42014-08-06 16:05:13 -0700269 case MMOP_ONLINE_KERNEL:
270 case MMOP_ONLINE_MOVABLE:
David Hildenbrand956f8b42020-04-06 20:07:16 -0700271 case MMOP_ONLINE:
David Hildenbrand381eab42018-10-30 15:10:29 -0700272 /* mem->online_type is protected by device_hotplug_lock */
Seth Jenningsfa2be402013-08-20 16:05:05 -0500273 mem->online_type = online_type;
274 ret = device_online(&mem->dev);
275 break;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700276 case MMOP_OFFLINE:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500277 ret = device_offline(&mem->dev);
278 break;
279 default:
280 ret = -EINVAL; /* should never happen */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200281 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200282
283 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600284
Reza Arbabd66ba152016-10-07 17:00:15 -0700285 if (ret < 0)
Dave Hansen3947be12005-10-29 18:16:54 -0700286 return ret;
Reza Arbabd66ba152016-10-07 17:00:15 -0700287 if (ret)
288 return -EINVAL;
289
Dave Hansen3947be12005-10-29 18:16:54 -0700290 return count;
291}
292
293/*
294 * phys_device is a bad name for this. What I really want
295 * is a way to differentiate between memory ranges that
296 * are part of physical devices that constitute
297 * a complete removable unit or fru.
298 * i.e. do these ranges belong to the same physical device,
299 * s.t. if I offline all of these sections I can then
300 * remove the physical device?
301 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100302static ssize_t phys_device_show(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800303 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700304{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800305 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700306 return sprintf(buf, "%d\n", mem->phys_device);
307}
308
Zhang Zhened2f2402014-10-09 15:26:31 -0700309#ifdef CONFIG_MEMORY_HOTREMOVE
Michal Hockoe5e68932017-09-06 16:19:37 -0700310static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
311 unsigned long nr_pages, int online_type,
312 struct zone *default_zone)
313{
314 struct zone *zone;
315
Michal Hockoe5e68932017-09-06 16:19:37 -0700316 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
317 if (zone != default_zone) {
318 strcat(buf, " ");
319 strcat(buf, zone->name);
320 }
321}
322
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100323static ssize_t valid_zones_show(struct device *dev,
Zhang Zhened2f2402014-10-09 15:26:31 -0700324 struct device_attribute *attr, char *buf)
325{
326 struct memory_block *mem = to_memory_block(dev);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700327 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Zhang Zhened2f2402014-10-09 15:26:31 -0700328 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Michal Hockoe5e68932017-09-06 16:19:37 -0700329 struct zone *default_zone;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700330 int nid;
Zhang Zhened2f2402014-10-09 15:26:31 -0700331
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700332 /*
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700333 * Check the existing zone. Make sure that we do that only on the
334 * online nodes otherwise the page_zone is not reliable
335 */
336 if (mem->state == MEM_ONLINE) {
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700337 /*
338 * The block contains more than one zone can not be offlined.
339 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
340 */
David Hildenbrand92917992020-02-03 17:34:26 -0800341 default_zone = test_pages_in_a_zone(start_pfn,
342 start_pfn + nr_pages);
343 if (!default_zone)
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700344 return sprintf(buf, "none\n");
David Hildenbrand92917992020-02-03 17:34:26 -0800345 strcat(buf, default_zone->name);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700346 goto out;
Zhang Zhened2f2402014-10-09 15:26:31 -0700347 }
348
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700349 nid = mem->nid;
David Hildenbrand956f8b42020-04-06 20:07:16 -0700350 default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, start_pfn,
351 nr_pages);
Michal Hockoe5e68932017-09-06 16:19:37 -0700352 strcat(buf, default_zone->name);
Zhang Zhened2f2402014-10-09 15:26:31 -0700353
Michal Hockoe5e68932017-09-06 16:19:37 -0700354 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
355 default_zone);
356 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
357 default_zone);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700358out:
Reza Arbaba371d9f2016-07-26 15:22:27 -0700359 strcat(buf, "\n");
360
361 return strlen(buf);
Zhang Zhened2f2402014-10-09 15:26:31 -0700362}
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100363static DEVICE_ATTR_RO(valid_zones);
Zhang Zhened2f2402014-10-09 15:26:31 -0700364#endif
365
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100366static DEVICE_ATTR_RO(phys_index);
367static DEVICE_ATTR_RW(state);
368static DEVICE_ATTR_RO(phys_device);
369static DEVICE_ATTR_RO(removable);
Dave Hansen3947be12005-10-29 18:16:54 -0700370
Dave Hansen3947be12005-10-29 18:16:54 -0700371/*
David Hildenbrandf915fb72019-09-23 15:35:43 -0700372 * Show the memory block size (shared by all memory blocks).
Dave Hansen3947be12005-10-29 18:16:54 -0700373 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100374static ssize_t block_size_bytes_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700376{
David Hildenbrand902ce63b2019-09-23 15:35:46 -0700377 return sprintf(buf, "%lx\n", memory_block_size_bytes());
Dave Hansen3947be12005-10-29 18:16:54 -0700378}
379
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100380static DEVICE_ATTR_RO(block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700381
Dave Hansen3947be12005-10-29 18:16:54 -0700382/*
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700383 * Memory auto online policy.
384 */
385
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100386static ssize_t auto_online_blocks_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700388{
David Hildenbrand862919e2020-04-06 20:07:40 -0700389 return sprintf(buf, "%s\n",
390 online_type_to_str[memhp_default_online_type]);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700391}
392
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100393static ssize_t auto_online_blocks_store(struct device *dev,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700396{
David Hildenbrand5f47adf2020-04-06 20:07:44 -0700397 const int online_type = memhp_online_type_from_str(buf);
398
399 if (online_type < 0)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700400 return -EINVAL;
401
David Hildenbrand5f47adf2020-04-06 20:07:44 -0700402 memhp_default_online_type = online_type;
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700403 return count;
404}
405
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100406static DEVICE_ATTR_RW(auto_online_blocks);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700407
408/*
Dave Hansen3947be12005-10-29 18:16:54 -0700409 * Some architectures will have custom drivers to do this, and
410 * will not need to do it from userspace. The fake hot-add code
411 * as well as ppc64 will do all of their discovery in userspace
412 * and will require this interface.
413 */
414#ifdef CONFIG_ARCH_MEMORY_PROBE
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100415static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700417{
418 u64 phys_addr;
John Allencb5490a2016-01-14 15:22:16 -0800419 int nid, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000420 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700421
Zhang Zhenb69deb22014-08-06 16:06:06 -0700422 ret = kstrtoull(buf, 0, &phys_addr);
423 if (ret)
424 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700425
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000426 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
427 return -EINVAL;
428
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700429 ret = lock_device_hotplug_sysfs();
430 if (ret)
zhong jiang37803842019-04-18 17:50:16 -0700431 return ret;
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700432
John Allencb5490a2016-01-14 15:22:16 -0800433 nid = memory_add_physaddr_to_nid(phys_addr);
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700434 ret = __add_memory(nid, phys_addr,
435 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600436
John Allencb5490a2016-01-14 15:22:16 -0800437 if (ret)
438 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700439
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530440 ret = count;
441out:
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700442 unlock_device_hotplug();
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530443 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700444}
Dave Hansen3947be12005-10-29 18:16:54 -0700445
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100446static DEVICE_ATTR_WO(probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700447#endif
448
Andi Kleenfacb6012009-12-16 12:20:00 +0100449#ifdef CONFIG_MEMORY_FAILURE
450/*
451 * Support for offlining pages of memory
452 */
453
454/* Soft offline a page */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100455static ssize_t soft_offline_page_store(struct device *dev,
456 struct device_attribute *attr,
457 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100458{
459 int ret;
460 u64 pfn;
461 if (!capable(CAP_SYS_ADMIN))
462 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900463 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100464 return -EINVAL;
465 pfn >>= PAGE_SHIFT;
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800466 ret = soft_offline_page(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100467 return ret == 0 ? count : ret;
468}
469
470/* Forcibly offline a page, including killing processes. */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100471static ssize_t hard_offline_page_store(struct device *dev,
472 struct device_attribute *attr,
473 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100474{
475 int ret;
476 u64 pfn;
477 if (!capable(CAP_SYS_ADMIN))
478 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900479 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100480 return -EINVAL;
481 pfn >>= PAGE_SHIFT;
Eric W. Biederman83b57532017-07-09 18:14:01 -0500482 ret = memory_failure(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100483 return ret ? ret : count;
484}
485
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100486static DEVICE_ATTR_WO(soft_offline_page);
487static DEVICE_ATTR_WO(hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100488#endif
489
Dave Hansen3947be12005-10-29 18:16:54 -0700490/*
491 * Note that phys_device is optional. It is here to allow for
492 * differentiation between which *physical* devices each
493 * section belongs to...
494 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400495int __weak arch_get_memory_phys_device(unsigned long start_pfn)
496{
497 return 0;
498}
Dave Hansen3947be12005-10-29 18:16:54 -0700499
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700500/*
501 * A reference for the returned memory block device is acquired.
502 *
503 * Called under device_hotplug_lock.
504 */
David Hildenbranddd625282019-07-18 15:57:53 -0700505static struct memory_block *find_memory_block_by_id(unsigned long block_id)
Robin Holt98383032010-09-29 14:00:55 -0500506{
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700507 struct memory_block *mem;
Robin Holt98383032010-09-29 14:00:55 -0500508
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700509 mem = xa_load(&memory_blocks, block_id);
510 if (mem)
511 get_device(&mem->dev);
512 return mem;
David Hildenbranddb051a02019-07-18 15:56:56 -0700513}
514
Dave Hansen3947be12005-10-29 18:16:54 -0700515/*
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700516 * Called under device_hotplug_lock.
Dave Hansen3947be12005-10-29 18:16:54 -0700517 */
Gary Hadec04fc582009-01-06 14:39:14 -0800518struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700519{
David Hildenbranddd625282019-07-18 15:57:53 -0700520 unsigned long block_id = base_memory_block_id(__section_nr(section));
521
522 return find_memory_block_by_id(block_id);
Dave Hansen3947be12005-10-29 18:16:54 -0700523}
524
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500525static struct attribute *memory_memblk_attrs[] = {
526 &dev_attr_phys_index.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500527 &dev_attr_state.attr,
528 &dev_attr_phys_device.attr,
529 &dev_attr_removable.attr,
Zhang Zhened2f2402014-10-09 15:26:31 -0700530#ifdef CONFIG_MEMORY_HOTREMOVE
531 &dev_attr_valid_zones.attr,
532#endif
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500533 NULL
534};
535
536static struct attribute_group memory_memblk_attr_group = {
537 .attrs = memory_memblk_attrs,
538};
539
540static const struct attribute_group *memory_memblk_attr_groups[] = {
541 &memory_memblk_attr_group,
542 NULL,
543};
544
545/*
546 * register_memory - Setup a sysfs device for a memory block
547 */
548static
549int register_memory(struct memory_block *memory)
550{
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530551 int ret;
552
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500553 memory->dev.bus = &memory_subsys;
554 memory->dev.id = memory->start_section_nr / sections_per_block;
555 memory->dev.release = memory_block_release;
556 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700557 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500558
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530559 ret = device_register(&memory->dev);
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700560 if (ret) {
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530561 put_device(&memory->dev);
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700562 return ret;
563 }
564 ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
565 GFP_KERNEL));
566 if (ret) {
567 put_device(&memory->dev);
568 device_unregister(&memory->dev);
569 }
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530570 return ret;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500571}
572
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700573static int init_memory_block(struct memory_block **memory,
574 unsigned long block_id, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500575{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600576 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500577 unsigned long start_pfn;
578 int ret = 0;
579
David Hildenbranddd625282019-07-18 15:57:53 -0700580 mem = find_memory_block_by_id(block_id);
David Hildenbranddb051a02019-07-18 15:56:56 -0700581 if (mem) {
582 put_device(&mem->dev);
583 return -EEXIST;
584 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600585 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500586 if (!mem)
587 return -ENOMEM;
588
David Hildenbrand18115822019-07-18 15:56:46 -0700589 mem->start_section_nr = block_id * sections_per_block;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500590 mem->state = state;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600591 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500592 mem->phys_device = arch_get_memory_phys_device(start_pfn);
David Hildenbrandd84f2f52019-09-23 15:35:40 -0700593 mem->nid = NUMA_NO_NODE;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500594
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600595 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600596
597 *memory = mem;
598 return ret;
599}
600
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700601static int add_memory_block(unsigned long base_section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600602{
David Hildenbrand68c3a6a2020-04-06 20:06:40 -0700603 int section_count = 0;
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500604 struct memory_block *mem;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700605 unsigned long nr;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600606
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700607 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
608 nr++)
609 if (present_section_nr(nr))
David Hildenbrand18115822019-07-18 15:56:46 -0700610 section_count++;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600611
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500612 if (section_count == 0)
613 return 0;
David Hildenbrand68c3a6a2020-04-06 20:06:40 -0700614 return init_memory_block(&mem, base_memory_block_id(base_section_nr),
615 MEM_ONLINE);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500616}
617
David Hildenbranddb051a02019-07-18 15:56:56 -0700618static void unregister_memory(struct memory_block *memory)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700619{
David Hildenbranddb051a02019-07-18 15:56:56 -0700620 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
621 return;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700622
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700623 WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
624
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700625 /* drop the ref. we got via find_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500626 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700627 device_unregister(&memory->dev);
628}
629
David Hildenbranddb051a02019-07-18 15:56:56 -0700630/*
631 * Create memory block devices for the given memory area. Start and size
632 * have to be aligned to memory block granularity. Memory block devices
633 * will be initialized as offline.
David Hildenbrand848e19a2019-11-30 17:54:14 -0800634 *
635 * Called under device_hotplug_lock.
David Hildenbranddb051a02019-07-18 15:56:56 -0700636 */
637int create_memory_block_devices(unsigned long start, unsigned long size)
638{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700639 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
640 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
David Hildenbranddb051a02019-07-18 15:56:56 -0700641 struct memory_block *mem;
642 unsigned long block_id;
643 int ret = 0;
644
645 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
646 !IS_ALIGNED(size, memory_block_size_bytes())))
647 return -EINVAL;
648
David Hildenbranddb051a02019-07-18 15:56:56 -0700649 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
650 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
651 if (ret)
652 break;
David Hildenbranddb051a02019-07-18 15:56:56 -0700653 }
654 if (ret) {
655 end_block_id = block_id;
656 for (block_id = start_block_id; block_id != end_block_id;
657 block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700658 mem = find_memory_block_by_id(block_id);
David Hildenbrand848e19a2019-11-30 17:54:14 -0800659 if (WARN_ON_ONCE(!mem))
660 continue;
David Hildenbranddb051a02019-07-18 15:56:56 -0700661 unregister_memory(mem);
662 }
663 }
David Hildenbranddb051a02019-07-18 15:56:56 -0700664 return ret;
665}
666
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700667/*
668 * Remove memory block devices for the given memory area. Start and size
669 * have to be aligned to memory block granularity. Memory block devices
670 * have to be offline.
David Hildenbrand848e19a2019-11-30 17:54:14 -0800671 *
672 * Called under device_hotplug_lock.
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700673 */
674void remove_memory_block_devices(unsigned long start, unsigned long size)
Dave Hansen3947be12005-10-29 18:16:54 -0700675{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700676 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
677 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
Dave Hansen3947be12005-10-29 18:16:54 -0700678 struct memory_block *mem;
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700679 unsigned long block_id;
Dave Hansen3947be12005-10-29 18:16:54 -0700680
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700681 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
682 !IS_ALIGNED(size, memory_block_size_bytes())))
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700683 return;
684
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700685 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700686 mem = find_memory_block_by_id(block_id);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700687 if (WARN_ON_ONCE(!mem))
688 continue;
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700689 unregister_memory_block_under_nodes(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600690 unregister_memory(mem);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700691 }
Dave Hansen3947be12005-10-29 18:16:54 -0700692}
693
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800694/* return true if the memory block is offlined, otherwise, return false */
695bool is_memblock_offlined(struct memory_block *mem)
696{
697 return mem->state == MEM_OFFLINE;
698}
699
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500700static struct attribute *memory_root_attrs[] = {
701#ifdef CONFIG_ARCH_MEMORY_PROBE
702 &dev_attr_probe.attr,
703#endif
704
705#ifdef CONFIG_MEMORY_FAILURE
706 &dev_attr_soft_offline_page.attr,
707 &dev_attr_hard_offline_page.attr,
708#endif
709
710 &dev_attr_block_size_bytes.attr,
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700711 &dev_attr_auto_online_blocks.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500712 NULL
713};
714
715static struct attribute_group memory_root_attr_group = {
716 .attrs = memory_root_attrs,
717};
718
719static const struct attribute_group *memory_root_attr_groups[] = {
720 &memory_root_attr_group,
721 NULL,
722};
723
Wen Congyange90bdb72012-10-08 16:34:01 -0700724/*
David Hildenbrand848e19a2019-11-30 17:54:14 -0800725 * Initialize the sysfs support for memory devices. At the time this function
726 * is called, we cannot have concurrent creation/deletion of memory block
727 * devices, the device_hotplug_lock is not needed.
Dave Hansen3947be12005-10-29 18:16:54 -0700728 */
David Hildenbrand902ce63b2019-09-23 15:35:46 -0700729void __init memory_dev_init(void)
Dave Hansen3947be12005-10-29 18:16:54 -0700730{
Dave Hansen3947be12005-10-29 18:16:54 -0700731 int ret;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700732 unsigned long block_sz, nr;
Dave Hansen3947be12005-10-29 18:16:54 -0700733
David Hildenbrand902ce63b2019-09-23 15:35:46 -0700734 /* Validate the configured memory block size */
735 block_sz = memory_block_size_bytes();
736 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
737 panic("Memory block size not suitable: 0x%lx\n", block_sz);
738 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
739
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500740 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800741 if (ret)
David Hildenbrand848e19a2019-11-30 17:54:14 -0800742 panic("%s() failed to register subsystem: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700743
744 /*
745 * Create entries for memory sections that were found
746 * during boot and have been initialized
747 */
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700748 for (nr = 0; nr <= __highest_present_section_nr;
749 nr += sections_per_block) {
David Hildenbrand848e19a2019-11-30 17:54:14 -0800750 ret = add_memory_block(nr);
751 if (ret)
752 panic("%s() failed to add memory block: %d\n", __func__,
753 ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700754 }
Dave Hansen3947be12005-10-29 18:16:54 -0700755}
David Hildenbrandea884642019-07-18 15:57:50 -0700756
757/**
758 * walk_memory_blocks - walk through all present memory blocks overlapped
759 * by the range [start, start + size)
760 *
761 * @start: start address of the memory range
762 * @size: size of the memory range
763 * @arg: argument passed to func
764 * @func: callback for each memory section walked
765 *
766 * This function walks through all present memory blocks overlapped by the
767 * range [start, start + size), calling func on each memory block.
768 *
769 * In case func() returns an error, walking is aborted and the error is
770 * returned.
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700771 *
772 * Called under device_hotplug_lock.
David Hildenbrandea884642019-07-18 15:57:50 -0700773 */
774int walk_memory_blocks(unsigned long start, unsigned long size,
775 void *arg, walk_memory_blocks_func_t func)
776{
777 const unsigned long start_block_id = phys_to_block_id(start);
778 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
779 struct memory_block *mem;
780 unsigned long block_id;
781 int ret = 0;
782
David Hildenbranddd625282019-07-18 15:57:53 -0700783 if (!size)
784 return 0;
785
David Hildenbrandea884642019-07-18 15:57:50 -0700786 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700787 mem = find_memory_block_by_id(block_id);
David Hildenbrandea884642019-07-18 15:57:50 -0700788 if (!mem)
789 continue;
790
791 ret = func(mem, arg);
792 put_device(&mem->dev);
793 if (ret)
794 break;
795 }
796 return ret;
797}
David Hildenbrand2c91f8f2019-11-15 17:34:57 -0800798
799struct for_each_memory_block_cb_data {
800 walk_memory_blocks_func_t func;
801 void *arg;
802};
803
804static int for_each_memory_block_cb(struct device *dev, void *data)
805{
806 struct memory_block *mem = to_memory_block(dev);
807 struct for_each_memory_block_cb_data *cb_data = data;
808
809 return cb_data->func(mem, cb_data->arg);
810}
811
812/**
813 * for_each_memory_block - walk through all present memory blocks
814 *
815 * @arg: argument passed to func
816 * @func: callback for each memory block walked
817 *
818 * This function walks through all present memory blocks, calling func on
819 * each memory block.
820 *
821 * In case func() returns an error, walking is aborted and the error is
822 * returned.
823 */
824int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
825{
826 struct for_each_memory_block_cb_data cb_data = {
827 .func = func,
828 .arg = arg,
829 };
830
831 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
832 for_each_memory_block_cb);
833}