blob: 0204384b4d1dc7215542f94a2ba4f7a8c51970b6 [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>
Daniel Walkerda19cbc2008-02-04 23:35:47 -080022#include <linux/mutex.h>
Shaohua Li9f1b16a2008-10-18 20:27:12 -070023#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.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
Nathan Fontenot2938ffb2010-10-19 12:45:24 -050029static DEFINE_MUTEX(mem_sysfs_mutex);
30
Dave Hansen3947be12005-10-29 18:16:54 -070031#define MEMORY_CLASS_NAME "memory"
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060032
Gu Zheng7315f0c2013-08-28 14:38:27 +080033#define to_memory_block(dev) container_of(dev, struct memory_block, dev)
34
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060035static int sections_per_block;
36
David Hildenbrand90ec010f2019-07-18 15:57:40 -070037static inline unsigned long base_memory_block_id(unsigned long section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060038{
39 return section_nr / sections_per_block;
40}
Dave Hansen3947be12005-10-29 18:16:54 -070041
David Hildenbrand90ec010f2019-07-18 15:57:40 -070042static inline unsigned long pfn_to_block_id(unsigned long pfn)
David Hildenbranddb051a02019-07-18 15:56:56 -070043{
44 return base_memory_block_id(pfn_to_section_nr(pfn));
45}
46
David Hildenbrandea884642019-07-18 15:57:50 -070047static inline unsigned long phys_to_block_id(unsigned long phys)
48{
49 return pfn_to_block_id(PFN_DOWN(phys));
50}
51
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020052static int memory_subsys_online(struct device *dev);
53static int memory_subsys_offline(struct device *dev);
54
Kay Sievers10fbcf42011-12-21 14:48:43 -080055static struct bus_type memory_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +010056 .name = MEMORY_CLASS_NAME,
Kay Sievers10fbcf42011-12-21 14:48:43 -080057 .dev_name = MEMORY_CLASS_NAME,
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020058 .online = memory_subsys_online,
59 .offline = memory_subsys_offline,
Dave Hansen3947be12005-10-29 18:16:54 -070060};
61
Alan Sterne041c682006-03-27 01:16:30 -080062static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070063
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080064int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070065{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020066 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070067}
Hannes Hering3c82c302008-05-07 14:43:01 +020068EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070069
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080070void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070071{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020072 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070073}
Hannes Hering3c82c302008-05-07 14:43:01 +020074EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070075
Robert Jennings925cc712009-12-17 14:44:38 +000076static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
77
78int register_memory_isolate_notifier(struct notifier_block *nb)
79{
80 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
81}
82EXPORT_SYMBOL(register_memory_isolate_notifier);
83
84void unregister_memory_isolate_notifier(struct notifier_block *nb)
85{
86 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
87}
88EXPORT_SYMBOL(unregister_memory_isolate_notifier);
89
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080090static void memory_block_release(struct device *dev)
91{
Gu Zheng7315f0c2013-08-28 14:38:27 +080092 struct memory_block *mem = to_memory_block(dev);
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080093
94 kfree(mem);
95}
96
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060097unsigned long __weak memory_block_size_bytes(void)
98{
99 return MIN_MEMORY_BLOCK_SIZE;
100}
Dave Hansenc221c0b2019-02-25 10:57:40 -0800101EXPORT_SYMBOL_GPL(memory_block_size_bytes);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600102
103static unsigned long get_memory_block_size(void)
104{
105 unsigned long block_sz;
106
107 block_sz = memory_block_size_bytes();
108
109 /* Validate blk_sz is a power of 2 and not less than section size */
110 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
111 WARN_ON(1);
112 block_sz = MIN_MEMORY_BLOCK_SIZE;
113 }
114
115 return block_sz;
116}
117
Dave Hansen3947be12005-10-29 18:16:54 -0700118/*
119 * use this as the physical section index that this memsection
120 * uses.
121 */
122
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100123static ssize_t phys_index_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700125{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800126 struct memory_block *mem = to_memory_block(dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600127 unsigned long phys_index;
128
129 phys_index = mem->start_section_nr / sections_per_block;
130 return sprintf(buf, "%08lx\n", phys_index);
131}
132
Dave Hansen3947be12005-10-29 18:16:54 -0700133/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700134 * Show whether the section of memory is likely to be hot-removable
135 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100136static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
137 char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700138{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800139 struct memory_block *mem = to_memory_block(dev);
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700140 unsigned long pfn;
141 int ret = 1, i;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700142
Michal Hocko8b0662f2017-07-06 15:37:53 -0700143 if (mem->state != MEM_ONLINE)
144 goto out;
145
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600146 for (i = 0; i < sections_per_block; i++) {
Russ Anderson21ea9f52013-08-28 16:35:18 -0700147 if (!present_section_nr(mem->start_section_nr + i))
148 continue;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600149 pfn = section_nr_to_pfn(mem->start_section_nr + i);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600150 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
151 }
152
Michal Hocko8b0662f2017-07-06 15:37:53 -0700153out:
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700154 return sprintf(buf, "%d\n", ret);
155}
156
157/*
Dave Hansen3947be12005-10-29 18:16:54 -0700158 * online, offline, going offline, etc.
159 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100160static ssize_t state_show(struct device *dev, struct device_attribute *attr,
161 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700162{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800163 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700164 ssize_t len = 0;
165
166 /*
167 * We can probably put these states in a nice little array
168 * so that they're not open-coded
169 */
170 switch (mem->state) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200171 case MEM_ONLINE:
172 len = sprintf(buf, "online\n");
173 break;
174 case MEM_OFFLINE:
175 len = sprintf(buf, "offline\n");
176 break;
177 case MEM_GOING_OFFLINE:
178 len = sprintf(buf, "going-offline\n");
179 break;
180 default:
181 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
182 mem->state);
183 WARN_ON(1);
184 break;
Dave Hansen3947be12005-10-29 18:16:54 -0700185 }
186
187 return len;
188}
189
Yasunori Goto7b78d332007-10-21 16:41:36 -0700190int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700191{
Alan Sterne041c682006-03-27 01:16:30 -0800192 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700193}
194
Robert Jennings925cc712009-12-17 14:44:38 +0000195int memory_isolate_notify(unsigned long val, void *v)
196{
197 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
198}
199
Dave Hansen3947be12005-10-29 18:16:54 -0700200/*
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700201 * The probe routines leave the pages uninitialized, just as the bootmem code
202 * does. Make sure we do not access them, but instead use only information from
203 * within sections.
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200204 */
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700205static bool pages_correctly_probed(unsigned long start_pfn)
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200206{
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700207 unsigned long section_nr = pfn_to_section_nr(start_pfn);
208 unsigned long section_nr_end = section_nr + sections_per_block;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200209 unsigned long pfn = start_pfn;
210
211 /*
212 * memmap between sections is not contiguous except with
213 * SPARSEMEM_VMEMMAP. We lookup the page once per section
214 * and assume memmap is contiguous within each section
215 */
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700216 for (; section_nr < section_nr_end; section_nr++) {
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200217 if (WARN_ON_ONCE(!pfn_valid(pfn)))
218 return false;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200219
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700220 if (!present_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800221 pr_warn("section %ld pfn[%lx, %lx) not present\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700222 section_nr, pfn, pfn + PAGES_PER_SECTION);
223 return false;
224 } else if (!valid_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800225 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700226 section_nr, pfn, pfn + PAGES_PER_SECTION);
227 return false;
228 } else if (online_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800229 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700230 section_nr, pfn, pfn + PAGES_PER_SECTION);
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200231 return false;
232 }
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700233 pfn += PAGES_PER_SECTION;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200234 }
235
236 return true;
237}
238
239/*
Dave Hansen3947be12005-10-29 18:16:54 -0700240 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
241 * OK to have direct references to sparsemem variables in here.
242 */
243static int
Baoquan He063b8a42019-05-13 17:19:35 -0700244memory_block_action(unsigned long start_section_nr, unsigned long action,
245 int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700246{
Wen Congyanga16cee12012-10-08 16:33:58 -0700247 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000248 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700249 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700250
Baoquan He063b8a42019-05-13 17:19:35 -0700251 start_pfn = section_nr_to_pfn(start_section_nr);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700252
Dave Hansen3947be12005-10-29 18:16:54 -0700253 switch (action) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200254 case MEM_ONLINE:
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700255 if (!pages_correctly_probed(start_pfn))
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200256 return -EBUSY;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200257
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200258 ret = online_pages(start_pfn, nr_pages, online_type);
259 break;
260 case MEM_OFFLINE:
261 ret = offline_pages(start_pfn, nr_pages);
262 break;
263 default:
264 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
Baoquan He063b8a42019-05-13 17:19:35 -0700265 "%ld\n", __func__, start_section_nr, action, action);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200266 ret = -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700267 }
Dave Hansen3947be12005-10-29 18:16:54 -0700268
269 return ret;
270}
271
Nathan Fontenotdc18d702017-02-24 15:00:02 -0800272static int memory_block_change_state(struct memory_block *mem,
Seth Jenningsfa2be402013-08-20 16:05:05 -0500273 unsigned long to_state, unsigned long from_state_req)
Dave Hansen3947be12005-10-29 18:16:54 -0700274{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700275 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600276
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200277 if (mem->state != from_state_req)
278 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700279
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600280 if (to_state == MEM_OFFLINE)
281 mem->state = MEM_GOING_OFFLINE;
282
Seth Jenningsfa2be402013-08-20 16:05:05 -0500283 ret = memory_block_action(mem->start_section_nr, to_state,
284 mem->online_type);
285
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200286 mem->state = ret ? from_state_req : to_state;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500287
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200288 return ret;
289}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600290
Seth Jenningsfa2be402013-08-20 16:05:05 -0500291/* The device lock serializes operations on memory_subsys_[online|offline] */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200292static int memory_subsys_online(struct device *dev)
293{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800294 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200295 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700296
Seth Jenningsfa2be402013-08-20 16:05:05 -0500297 if (mem->state == MEM_ONLINE)
298 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200299
Seth Jenningsfa2be402013-08-20 16:05:05 -0500300 /*
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100301 * If we are called from state_store(), online_type will be
Seth Jenningsfa2be402013-08-20 16:05:05 -0500302 * set >= 0 Otherwise we were called from the device online
303 * attribute and need to set the online_type.
304 */
305 if (mem->online_type < 0)
Tang Chen4f7c6b42014-08-06 16:05:13 -0700306 mem->online_type = MMOP_ONLINE_KEEP;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200307
Seth Jenningsfa2be402013-08-20 16:05:05 -0500308 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
309
310 /* clear online_type */
311 mem->online_type = -1;
312
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200313 return ret;
314}
315
316static int memory_subsys_offline(struct device *dev)
317{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800318 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200319
Seth Jenningsfa2be402013-08-20 16:05:05 -0500320 if (mem->state == MEM_OFFLINE)
321 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200322
Seth Jennings26bbe7e2015-12-11 13:40:57 -0800323 /* Can't offline block with non-present sections */
324 if (mem->section_count != sections_per_block)
325 return -EINVAL;
326
Seth Jenningsfa2be402013-08-20 16:05:05 -0500327 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200328}
329
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100330static ssize_t state_store(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700332{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800333 struct memory_block *mem = to_memory_block(dev);
Seth Jenningsfa2be402013-08-20 16:05:05 -0500334 int ret, online_type;
Dave Hansen3947be12005-10-29 18:16:54 -0700335
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200336 ret = lock_device_hotplug_sysfs();
337 if (ret)
338 return ret;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200339
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700340 if (sysfs_streq(buf, "online_kernel"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700341 online_type = MMOP_ONLINE_KERNEL;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700342 else if (sysfs_streq(buf, "online_movable"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700343 online_type = MMOP_ONLINE_MOVABLE;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700344 else if (sysfs_streq(buf, "online"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700345 online_type = MMOP_ONLINE_KEEP;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700346 else if (sysfs_streq(buf, "offline"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700347 online_type = MMOP_OFFLINE;
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900348 else {
349 ret = -EINVAL;
350 goto err;
351 }
Seth Jenningsfa2be402013-08-20 16:05:05 -0500352
353 switch (online_type) {
Tang Chen4f7c6b42014-08-06 16:05:13 -0700354 case MMOP_ONLINE_KERNEL:
355 case MMOP_ONLINE_MOVABLE:
356 case MMOP_ONLINE_KEEP:
David Hildenbrand381eab42018-10-30 15:10:29 -0700357 /* mem->online_type is protected by device_hotplug_lock */
Seth Jenningsfa2be402013-08-20 16:05:05 -0500358 mem->online_type = online_type;
359 ret = device_online(&mem->dev);
360 break;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700361 case MMOP_OFFLINE:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500362 ret = device_offline(&mem->dev);
363 break;
364 default:
365 ret = -EINVAL; /* should never happen */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200366 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200367
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900368err:
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200369 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600370
Reza Arbabd66ba152016-10-07 17:00:15 -0700371 if (ret < 0)
Dave Hansen3947be12005-10-29 18:16:54 -0700372 return ret;
Reza Arbabd66ba152016-10-07 17:00:15 -0700373 if (ret)
374 return -EINVAL;
375
Dave Hansen3947be12005-10-29 18:16:54 -0700376 return count;
377}
378
379/*
380 * phys_device is a bad name for this. What I really want
381 * is a way to differentiate between memory ranges that
382 * are part of physical devices that constitute
383 * a complete removable unit or fru.
384 * i.e. do these ranges belong to the same physical device,
385 * s.t. if I offline all of these sections I can then
386 * remove the physical device?
387 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100388static ssize_t phys_device_show(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800389 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700390{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800391 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700392 return sprintf(buf, "%d\n", mem->phys_device);
393}
394
Zhang Zhened2f2402014-10-09 15:26:31 -0700395#ifdef CONFIG_MEMORY_HOTREMOVE
Michal Hockoe5e68932017-09-06 16:19:37 -0700396static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
397 unsigned long nr_pages, int online_type,
398 struct zone *default_zone)
399{
400 struct zone *zone;
401
Michal Hockoe5e68932017-09-06 16:19:37 -0700402 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
403 if (zone != default_zone) {
404 strcat(buf, " ");
405 strcat(buf, zone->name);
406 }
407}
408
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100409static ssize_t valid_zones_show(struct device *dev,
Zhang Zhened2f2402014-10-09 15:26:31 -0700410 struct device_attribute *attr, char *buf)
411{
412 struct memory_block *mem = to_memory_block(dev);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700413 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Zhang Zhened2f2402014-10-09 15:26:31 -0700414 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700415 unsigned long valid_start_pfn, valid_end_pfn;
Michal Hockoe5e68932017-09-06 16:19:37 -0700416 struct zone *default_zone;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700417 int nid;
Zhang Zhened2f2402014-10-09 15:26:31 -0700418
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700419 /*
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700420 * Check the existing zone. Make sure that we do that only on the
421 * online nodes otherwise the page_zone is not reliable
422 */
423 if (mem->state == MEM_ONLINE) {
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700424 /*
425 * The block contains more than one zone can not be offlined.
426 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
427 */
428 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
429 &valid_start_pfn, &valid_end_pfn))
430 return sprintf(buf, "none\n");
431 start_pfn = valid_start_pfn;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700432 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
433 goto out;
Zhang Zhened2f2402014-10-09 15:26:31 -0700434 }
435
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700436 nid = mem->nid;
Michal Hockoe5e68932017-09-06 16:19:37 -0700437 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
438 strcat(buf, default_zone->name);
Zhang Zhened2f2402014-10-09 15:26:31 -0700439
Michal Hockoe5e68932017-09-06 16:19:37 -0700440 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
441 default_zone);
442 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
443 default_zone);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700444out:
Reza Arbaba371d9f2016-07-26 15:22:27 -0700445 strcat(buf, "\n");
446
447 return strlen(buf);
Zhang Zhened2f2402014-10-09 15:26:31 -0700448}
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100449static DEVICE_ATTR_RO(valid_zones);
Zhang Zhened2f2402014-10-09 15:26:31 -0700450#endif
451
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100452static DEVICE_ATTR_RO(phys_index);
453static DEVICE_ATTR_RW(state);
454static DEVICE_ATTR_RO(phys_device);
455static DEVICE_ATTR_RO(removable);
Dave Hansen3947be12005-10-29 18:16:54 -0700456
Dave Hansen3947be12005-10-29 18:16:54 -0700457/*
458 * Block size attribute stuff
459 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100460static ssize_t block_size_bytes_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700462{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600463 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700464}
465
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100466static DEVICE_ATTR_RO(block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700467
Dave Hansen3947be12005-10-29 18:16:54 -0700468/*
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700469 * Memory auto online policy.
470 */
471
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100472static ssize_t auto_online_blocks_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700474{
475 if (memhp_auto_online)
476 return sprintf(buf, "online\n");
477 else
478 return sprintf(buf, "offline\n");
479}
480
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100481static ssize_t auto_online_blocks_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700484{
485 if (sysfs_streq(buf, "online"))
486 memhp_auto_online = true;
487 else if (sysfs_streq(buf, "offline"))
488 memhp_auto_online = false;
489 else
490 return -EINVAL;
491
492 return count;
493}
494
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100495static DEVICE_ATTR_RW(auto_online_blocks);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700496
497/*
Dave Hansen3947be12005-10-29 18:16:54 -0700498 * Some architectures will have custom drivers to do this, and
499 * will not need to do it from userspace. The fake hot-add code
500 * as well as ppc64 will do all of their discovery in userspace
501 * and will require this interface.
502 */
503#ifdef CONFIG_ARCH_MEMORY_PROBE
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100504static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700506{
507 u64 phys_addr;
John Allencb5490a2016-01-14 15:22:16 -0800508 int nid, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000509 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700510
Zhang Zhenb69deb22014-08-06 16:06:06 -0700511 ret = kstrtoull(buf, 0, &phys_addr);
512 if (ret)
513 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700514
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000515 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
516 return -EINVAL;
517
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700518 ret = lock_device_hotplug_sysfs();
519 if (ret)
zhong jiang37803842019-04-18 17:50:16 -0700520 return ret;
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700521
John Allencb5490a2016-01-14 15:22:16 -0800522 nid = memory_add_physaddr_to_nid(phys_addr);
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700523 ret = __add_memory(nid, phys_addr,
524 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600525
John Allencb5490a2016-01-14 15:22:16 -0800526 if (ret)
527 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700528
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530529 ret = count;
530out:
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700531 unlock_device_hotplug();
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530532 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700533}
Dave Hansen3947be12005-10-29 18:16:54 -0700534
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100535static DEVICE_ATTR_WO(probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700536#endif
537
Andi Kleenfacb6012009-12-16 12:20:00 +0100538#ifdef CONFIG_MEMORY_FAILURE
539/*
540 * Support for offlining pages of memory
541 */
542
543/* Soft offline a page */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100544static ssize_t soft_offline_page_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100547{
548 int ret;
549 u64 pfn;
550 if (!capable(CAP_SYS_ADMIN))
551 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900552 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100553 return -EINVAL;
554 pfn >>= PAGE_SHIFT;
555 if (!pfn_valid(pfn))
556 return -ENXIO;
557 ret = soft_offline_page(pfn_to_page(pfn), 0);
558 return ret == 0 ? count : ret;
559}
560
561/* Forcibly offline a page, including killing processes. */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100562static ssize_t hard_offline_page_store(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100565{
566 int ret;
567 u64 pfn;
568 if (!capable(CAP_SYS_ADMIN))
569 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900570 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100571 return -EINVAL;
572 pfn >>= PAGE_SHIFT;
Eric W. Biederman83b57532017-07-09 18:14:01 -0500573 ret = memory_failure(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100574 return ret ? ret : count;
575}
576
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100577static DEVICE_ATTR_WO(soft_offline_page);
578static DEVICE_ATTR_WO(hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100579#endif
580
Dave Hansen3947be12005-10-29 18:16:54 -0700581/*
582 * Note that phys_device is optional. It is here to allow for
583 * differentiation between which *physical* devices each
584 * section belongs to...
585 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400586int __weak arch_get_memory_phys_device(unsigned long start_pfn)
587{
588 return 0;
589}
Dave Hansen3947be12005-10-29 18:16:54 -0700590
Kay Sievers10fbcf42011-12-21 14:48:43 -0800591/*
592 * A reference for the returned object is held and the reference for the
593 * hinted object is released.
594 */
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700595static struct memory_block *find_memory_block_by_id(unsigned long block_id,
David Hildenbranddb051a02019-07-18 15:56:56 -0700596 struct memory_block *hint)
Robin Holt98383032010-09-29 14:00:55 -0500597{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800598 struct device *hintdev = hint ? &hint->dev : NULL;
599 struct device *dev;
Robin Holt98383032010-09-29 14:00:55 -0500600
Kay Sievers10fbcf42011-12-21 14:48:43 -0800601 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
602 if (hint)
603 put_device(&hint->dev);
604 if (!dev)
Robin Holt98383032010-09-29 14:00:55 -0500605 return NULL;
Gu Zheng7315f0c2013-08-28 14:38:27 +0800606 return to_memory_block(dev);
Robin Holt98383032010-09-29 14:00:55 -0500607}
608
David Hildenbranddb051a02019-07-18 15:56:56 -0700609struct memory_block *find_memory_block_hinted(struct mem_section *section,
610 struct memory_block *hint)
611{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700612 unsigned long block_id = base_memory_block_id(__section_nr(section));
David Hildenbranddb051a02019-07-18 15:56:56 -0700613
614 return find_memory_block_by_id(block_id, hint);
615}
616
Dave Hansen3947be12005-10-29 18:16:54 -0700617/*
618 * For now, we have a linear search to go find the appropriate
619 * memory_block corresponding to a particular phys_index. If
620 * this gets to be a real problem, we can always use a radix
621 * tree or something here.
622 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800623 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700624 */
Gary Hadec04fc582009-01-06 14:39:14 -0800625struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700626{
Robin Holt98383032010-09-29 14:00:55 -0500627 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700628}
629
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500630static struct attribute *memory_memblk_attrs[] = {
631 &dev_attr_phys_index.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500632 &dev_attr_state.attr,
633 &dev_attr_phys_device.attr,
634 &dev_attr_removable.attr,
Zhang Zhened2f2402014-10-09 15:26:31 -0700635#ifdef CONFIG_MEMORY_HOTREMOVE
636 &dev_attr_valid_zones.attr,
637#endif
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500638 NULL
639};
640
641static struct attribute_group memory_memblk_attr_group = {
642 .attrs = memory_memblk_attrs,
643};
644
645static const struct attribute_group *memory_memblk_attr_groups[] = {
646 &memory_memblk_attr_group,
647 NULL,
648};
649
650/*
651 * register_memory - Setup a sysfs device for a memory block
652 */
653static
654int register_memory(struct memory_block *memory)
655{
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530656 int ret;
657
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500658 memory->dev.bus = &memory_subsys;
659 memory->dev.id = memory->start_section_nr / sections_per_block;
660 memory->dev.release = memory_block_release;
661 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700662 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500663
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530664 ret = device_register(&memory->dev);
665 if (ret)
666 put_device(&memory->dev);
667
668 return ret;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500669}
670
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700671static int init_memory_block(struct memory_block **memory,
672 unsigned long block_id, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500673{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600674 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500675 unsigned long start_pfn;
676 int ret = 0;
677
David Hildenbranddb051a02019-07-18 15:56:56 -0700678 mem = find_memory_block_by_id(block_id, NULL);
679 if (mem) {
680 put_device(&mem->dev);
681 return -EEXIST;
682 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600683 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500684 if (!mem)
685 return -ENOMEM;
686
David Hildenbrand18115822019-07-18 15:56:46 -0700687 mem->start_section_nr = block_id * sections_per_block;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600688 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500689 mem->state = state;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600690 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500691 mem->phys_device = arch_get_memory_phys_device(start_pfn);
692
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600693 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600694
695 *memory = mem;
696 return ret;
697}
698
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700699static int add_memory_block(unsigned long base_section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600700{
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700701 int ret, section_count = 0;
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500702 struct memory_block *mem;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700703 unsigned long nr;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600704
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700705 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
706 nr++)
707 if (present_section_nr(nr))
David Hildenbrand18115822019-07-18 15:56:46 -0700708 section_count++;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600709
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500710 if (section_count == 0)
711 return 0;
David Hildenbrand18115822019-07-18 15:56:46 -0700712 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
713 MEM_ONLINE);
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500714 if (ret)
715 return ret;
716 mem->section_count = section_count;
717 return 0;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500718}
719
David Hildenbranddb051a02019-07-18 15:56:56 -0700720static void unregister_memory(struct memory_block *memory)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700721{
David Hildenbranddb051a02019-07-18 15:56:56 -0700722 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
723 return;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700724
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700725 /* drop the ref. we got via find_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500726 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700727 device_unregister(&memory->dev);
728}
729
David Hildenbranddb051a02019-07-18 15:56:56 -0700730/*
731 * Create memory block devices for the given memory area. Start and size
732 * have to be aligned to memory block granularity. Memory block devices
733 * will be initialized as offline.
734 */
735int create_memory_block_devices(unsigned long start, unsigned long size)
736{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700737 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
738 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
David Hildenbranddb051a02019-07-18 15:56:56 -0700739 struct memory_block *mem;
740 unsigned long block_id;
741 int ret = 0;
742
743 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
744 !IS_ALIGNED(size, memory_block_size_bytes())))
745 return -EINVAL;
746
747 mutex_lock(&mem_sysfs_mutex);
748 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
749 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
750 if (ret)
751 break;
752 mem->section_count = sections_per_block;
753 }
754 if (ret) {
755 end_block_id = block_id;
756 for (block_id = start_block_id; block_id != end_block_id;
757 block_id++) {
758 mem = find_memory_block_by_id(block_id, NULL);
759 mem->section_count = 0;
760 unregister_memory(mem);
761 }
762 }
763 mutex_unlock(&mem_sysfs_mutex);
764 return ret;
765}
766
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700767/*
768 * Remove memory block devices for the given memory area. Start and size
769 * have to be aligned to memory block granularity. Memory block devices
770 * have to be offline.
771 */
772void remove_memory_block_devices(unsigned long start, unsigned long size)
Dave Hansen3947be12005-10-29 18:16:54 -0700773{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700774 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
775 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
Dave Hansen3947be12005-10-29 18:16:54 -0700776 struct memory_block *mem;
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700777 unsigned long block_id;
Dave Hansen3947be12005-10-29 18:16:54 -0700778
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700779 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
780 !IS_ALIGNED(size, memory_block_size_bytes())))
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700781 return;
782
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500783 mutex_lock(&mem_sysfs_mutex);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700784 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
785 mem = find_memory_block_by_id(block_id, NULL);
786 if (WARN_ON_ONCE(!mem))
787 continue;
788 mem->section_count = 0;
789 unregister_memory_block_under_nodes(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600790 unregister_memory(mem);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700791 }
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500792 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700793}
794
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800795/* return true if the memory block is offlined, otherwise, return false */
796bool is_memblock_offlined(struct memory_block *mem)
797{
798 return mem->state == MEM_OFFLINE;
799}
800
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500801static struct attribute *memory_root_attrs[] = {
802#ifdef CONFIG_ARCH_MEMORY_PROBE
803 &dev_attr_probe.attr,
804#endif
805
806#ifdef CONFIG_MEMORY_FAILURE
807 &dev_attr_soft_offline_page.attr,
808 &dev_attr_hard_offline_page.attr,
809#endif
810
811 &dev_attr_block_size_bytes.attr,
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700812 &dev_attr_auto_online_blocks.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500813 NULL
814};
815
816static struct attribute_group memory_root_attr_group = {
817 .attrs = memory_root_attrs,
818};
819
820static const struct attribute_group *memory_root_attr_groups[] = {
821 &memory_root_attr_group,
822 NULL,
823};
824
Wen Congyange90bdb72012-10-08 16:34:01 -0700825/*
Dave Hansen3947be12005-10-29 18:16:54 -0700826 * Initialize the sysfs support for memory devices...
827 */
828int __init memory_dev_init(void)
829{
Dave Hansen3947be12005-10-29 18:16:54 -0700830 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800831 int err;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700832 unsigned long block_sz, nr;
Dave Hansen3947be12005-10-29 18:16:54 -0700833
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500834 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800835 if (ret)
836 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700837
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600838 block_sz = get_memory_block_size();
839 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
840
Dave Hansen3947be12005-10-29 18:16:54 -0700841 /*
842 * Create entries for memory sections that were found
843 * during boot and have been initialized
844 */
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500845 mutex_lock(&mem_sysfs_mutex);
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700846 for (nr = 0; nr <= __highest_present_section_nr;
847 nr += sections_per_block) {
848 err = add_memory_block(nr);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800849 if (!ret)
850 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700851 }
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500852 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700853
Andrew Morton28ec24e2006-12-06 20:37:29 -0800854out:
855 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800856 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700857 return ret;
858}
David Hildenbrandea884642019-07-18 15:57:50 -0700859
860/**
861 * walk_memory_blocks - walk through all present memory blocks overlapped
862 * by the range [start, start + size)
863 *
864 * @start: start address of the memory range
865 * @size: size of the memory range
866 * @arg: argument passed to func
867 * @func: callback for each memory section walked
868 *
869 * This function walks through all present memory blocks overlapped by the
870 * range [start, start + size), calling func on each memory block.
871 *
872 * In case func() returns an error, walking is aborted and the error is
873 * returned.
874 */
875int walk_memory_blocks(unsigned long start, unsigned long size,
876 void *arg, walk_memory_blocks_func_t func)
877{
878 const unsigned long start_block_id = phys_to_block_id(start);
879 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
880 struct memory_block *mem;
881 unsigned long block_id;
882 int ret = 0;
883
884 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
885 mem = find_memory_block_by_id(block_id, NULL);
886 if (!mem)
887 continue;
888
889 ret = func(mem, arg);
890 put_device(&mem->dev);
891 if (ret)
892 break;
893 }
894 return ret;
895}