blob: 365cd4a7f239757e56ba2371df054fca778556a7 [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
Anshuman Khandual1adf8b42021-02-25 17:17:13 -080038int mhp_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
Wei Yang178bdbe2020-06-23 10:57:01 +080053static inline unsigned long 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{
Wei Yang178bdbe2020-06-23 10:57:01 +080060 return memory_block_id(pfn_to_section_nr(pfn));
David Hildenbranddb051a02019-07-18 15:56:56 -070061}
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
David Hildenbrand028fc572021-09-07 19:55:26 -070085/*
86 * Memory groups, indexed by memory group id (mgid).
87 */
88static DEFINE_XARRAY_FLAGS(memory_groups, XA_FLAGS_ALLOC);
David Hildenbrand3fcebf92021-09-07 19:55:48 -070089#define MEMORY_GROUP_MARK_DYNAMIC XA_MARK_1
David Hildenbrand028fc572021-09-07 19:55:26 -070090
Alan Sterne041c682006-03-27 01:16:30 -080091static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070092
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080093int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070094{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020095 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070096}
Hannes Hering3c82c302008-05-07 14:43:01 +020097EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070098
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080099void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -0700100{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +0200101 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -0700102}
Hannes Hering3c82c302008-05-07 14:43:01 +0200103EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -0700104
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -0800105static void memory_block_release(struct device *dev)
106{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800107 struct memory_block *mem = to_memory_block(dev);
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -0800108
109 kfree(mem);
110}
111
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600112unsigned long __weak memory_block_size_bytes(void)
113{
114 return MIN_MEMORY_BLOCK_SIZE;
115}
Dave Hansenc221c0b2019-02-25 10:57:40 -0800116EXPORT_SYMBOL_GPL(memory_block_size_bytes);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600117
Dave Hansen3947be12005-10-29 18:16:54 -0700118/*
David Hildenbrandf915fb72019-09-23 15:35:43 -0700119 * Show the first physical section index (number) of this memory block.
Dave Hansen3947be12005-10-29 18:16:54 -0700120 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100121static ssize_t phys_index_show(struct device *dev,
122 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700123{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800124 struct memory_block *mem = to_memory_block(dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600125 unsigned long phys_index;
126
127 phys_index = mem->start_section_nr / sections_per_block;
Joe Perches948b3ed2020-09-16 13:40:42 -0700128
Joe Perchesaa838892020-09-16 13:40:39 -0700129 return sysfs_emit(buf, "%08lx\n", phys_index);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600130}
131
Dave Hansen3947be12005-10-29 18:16:54 -0700132/*
David Hildenbrand53cdc1c2020-03-28 19:17:19 -0700133 * Legacy interface that we cannot remove. Always indicate "removable"
134 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700135 */
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{
Joe Perchesaa838892020-09-16 13:40:39 -0700139 return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700140}
141
142/*
Dave Hansen3947be12005-10-29 18:16:54 -0700143 * online, offline, going offline, etc.
144 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100145static ssize_t state_show(struct device *dev, struct device_attribute *attr,
146 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700147{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800148 struct memory_block *mem = to_memory_block(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700149 const char *output;
Dave Hansen3947be12005-10-29 18:16:54 -0700150
151 /*
152 * We can probably put these states in a nice little array
153 * so that they're not open-coded
154 */
155 switch (mem->state) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200156 case MEM_ONLINE:
Joe Perches973c3912020-09-16 13:40:40 -0700157 output = "online";
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200158 break;
159 case MEM_OFFLINE:
Joe Perches973c3912020-09-16 13:40:40 -0700160 output = "offline";
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200161 break;
162 case MEM_GOING_OFFLINE:
Joe Perches973c3912020-09-16 13:40:40 -0700163 output = "going-offline";
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200164 break;
165 default:
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200166 WARN_ON(1);
Joe Perches973c3912020-09-16 13:40:40 -0700167 return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
Dave Hansen3947be12005-10-29 18:16:54 -0700168 }
169
Joe Perches973c3912020-09-16 13:40:40 -0700170 return sysfs_emit(buf, "%s\n", output);
Dave Hansen3947be12005-10-29 18:16:54 -0700171}
172
Yasunori Goto7b78d332007-10-21 16:41:36 -0700173int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700174{
Alan Sterne041c682006-03-27 01:16:30 -0800175 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700176}
177
Oscar Salvador8736cc22021-05-04 18:39:33 -0700178static int memory_block_online(struct memory_block *mem)
179{
180 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
181 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700182 unsigned long nr_vmemmap_pages = mem->nr_vmemmap_pages;
183 struct zone *zone;
184 int ret;
Oscar Salvador8736cc22021-05-04 18:39:33 -0700185
David Hildenbrand445fcf72021-09-07 19:55:45 -0700186 zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group,
187 start_pfn, nr_pages);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700188
189 /*
190 * Although vmemmap pages have a different lifecycle than the pages
191 * they describe (they remain until the memory is unplugged), doing
192 * their initialization and accounting at memory onlining/offlining
193 * stage helps to keep accounting easier to follow - e.g vmemmaps
194 * belong to the same zone as the memory they backed.
195 */
196 if (nr_vmemmap_pages) {
197 ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone);
198 if (ret)
199 return ret;
200 }
201
202 ret = online_pages(start_pfn + nr_vmemmap_pages,
David Hildenbrand836809e2021-09-07 19:55:30 -0700203 nr_pages - nr_vmemmap_pages, zone, mem->group);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700204 if (ret) {
205 if (nr_vmemmap_pages)
206 mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
207 return ret;
208 }
209
210 /*
211 * Account once onlining succeeded. If the zone was unpopulated, it is
212 * now already properly populated.
213 */
214 if (nr_vmemmap_pages)
David Hildenbrand836809e2021-09-07 19:55:30 -0700215 adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
David Hildenbrand4b097002021-09-07 19:55:19 -0700216 nr_vmemmap_pages);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700217
218 return ret;
Oscar Salvador8736cc22021-05-04 18:39:33 -0700219}
220
221static int memory_block_offline(struct memory_block *mem)
222{
223 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
224 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700225 unsigned long nr_vmemmap_pages = mem->nr_vmemmap_pages;
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700226 int ret;
Oscar Salvador8736cc22021-05-04 18:39:33 -0700227
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700228 /*
229 * Unaccount before offlining, such that unpopulated zone and kthreads
230 * can properly be torn down in offline_pages().
231 */
David Hildenbrand4b097002021-09-07 19:55:19 -0700232 if (nr_vmemmap_pages)
David Hildenbrand836809e2021-09-07 19:55:30 -0700233 adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
David Hildenbrand4b097002021-09-07 19:55:19 -0700234 -nr_vmemmap_pages);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700235
236 ret = offline_pages(start_pfn + nr_vmemmap_pages,
David Hildenbrand836809e2021-09-07 19:55:30 -0700237 nr_pages - nr_vmemmap_pages, mem->group);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700238 if (ret) {
239 /* offline_pages() failed. Account back. */
240 if (nr_vmemmap_pages)
David Hildenbrand4b097002021-09-07 19:55:19 -0700241 adjust_present_page_count(pfn_to_page(start_pfn),
David Hildenbrand836809e2021-09-07 19:55:30 -0700242 mem->group, nr_vmemmap_pages);
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700243 return ret;
244 }
245
246 if (nr_vmemmap_pages)
247 mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
248
249 return ret;
Oscar Salvador8736cc22021-05-04 18:39:33 -0700250}
251
Dave Hansen3947be12005-10-29 18:16:54 -0700252/*
253 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
254 * OK to have direct references to sparsemem variables in here.
255 */
256static int
Oscar Salvador8736cc22021-05-04 18:39:33 -0700257memory_block_action(struct memory_block *mem, unsigned long action)
Dave Hansen3947be12005-10-29 18:16:54 -0700258{
Dave Hansen3947be12005-10-29 18:16:54 -0700259 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700260
Dave Hansen3947be12005-10-29 18:16:54 -0700261 switch (action) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200262 case MEM_ONLINE:
Oscar Salvador8736cc22021-05-04 18:39:33 -0700263 ret = memory_block_online(mem);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200264 break;
265 case MEM_OFFLINE:
Oscar Salvador8736cc22021-05-04 18:39:33 -0700266 ret = memory_block_offline(mem);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200267 break;
268 default:
269 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
Oscar Salvador8736cc22021-05-04 18:39:33 -0700270 "%ld\n", __func__, mem->start_section_nr, action, action);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200271 ret = -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700272 }
Dave Hansen3947be12005-10-29 18:16:54 -0700273
274 return ret;
275}
276
Nathan Fontenotdc18d702017-02-24 15:00:02 -0800277static int memory_block_change_state(struct memory_block *mem,
Seth Jenningsfa2be402013-08-20 16:05:05 -0500278 unsigned long to_state, unsigned long from_state_req)
Dave Hansen3947be12005-10-29 18:16:54 -0700279{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700280 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600281
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200282 if (mem->state != from_state_req)
283 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700284
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600285 if (to_state == MEM_OFFLINE)
286 mem->state = MEM_GOING_OFFLINE;
287
Oscar Salvador8736cc22021-05-04 18:39:33 -0700288 ret = memory_block_action(mem, to_state);
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200289 mem->state = ret ? from_state_req : to_state;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500290
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200291 return ret;
292}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600293
Seth Jenningsfa2be402013-08-20 16:05:05 -0500294/* The device lock serializes operations on memory_subsys_[online|offline] */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200295static int memory_subsys_online(struct device *dev)
296{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800297 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200298 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700299
Seth Jenningsfa2be402013-08-20 16:05:05 -0500300 if (mem->state == MEM_ONLINE)
301 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200302
Seth Jenningsfa2be402013-08-20 16:05:05 -0500303 /*
David Hildenbrandefc978a2020-04-06 20:07:20 -0700304 * When called via device_online() without configuring the online_type,
305 * we want to default to MMOP_ONLINE.
Seth Jenningsfa2be402013-08-20 16:05:05 -0500306 */
David Hildenbrandefc978a2020-04-06 20:07:20 -0700307 if (mem->online_type == MMOP_OFFLINE)
David Hildenbrand956f8b42020-04-06 20:07:16 -0700308 mem->online_type = MMOP_ONLINE;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200309
Seth Jenningsfa2be402013-08-20 16:05:05 -0500310 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
David Hildenbrandefc978a2020-04-06 20:07:20 -0700311 mem->online_type = MMOP_OFFLINE;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500312
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 Jenningsfa2be402013-08-20 16:05:05 -0500323 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200324}
325
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100326static ssize_t state_store(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700328{
Anshuman Khandual1adf8b42021-02-25 17:17:13 -0800329 const int online_type = mhp_online_type_from_str(buf);
Gu Zheng7315f0c2013-08-28 14:38:27 +0800330 struct memory_block *mem = to_memory_block(dev);
David Hildenbrand4dc82072020-04-06 20:07:24 -0700331 int ret;
332
333 if (online_type < 0)
334 return -EINVAL;
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
Seth Jenningsfa2be402013-08-20 16:05:05 -0500340 switch (online_type) {
Tang Chen4f7c6b42014-08-06 16:05:13 -0700341 case MMOP_ONLINE_KERNEL:
342 case MMOP_ONLINE_MOVABLE:
David Hildenbrand956f8b42020-04-06 20:07:16 -0700343 case MMOP_ONLINE:
David Hildenbrand381eab42018-10-30 15:10:29 -0700344 /* mem->online_type is protected by device_hotplug_lock */
Seth Jenningsfa2be402013-08-20 16:05:05 -0500345 mem->online_type = online_type;
346 ret = device_online(&mem->dev);
347 break;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700348 case MMOP_OFFLINE:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500349 ret = device_offline(&mem->dev);
350 break;
351 default:
352 ret = -EINVAL; /* should never happen */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200353 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200354
355 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600356
Reza Arbabd66ba152016-10-07 17:00:15 -0700357 if (ret < 0)
Dave Hansen3947be12005-10-29 18:16:54 -0700358 return ret;
Reza Arbabd66ba152016-10-07 17:00:15 -0700359 if (ret)
360 return -EINVAL;
361
Dave Hansen3947be12005-10-29 18:16:54 -0700362 return count;
363}
364
365/*
David Hildenbrande9a2e482021-02-25 17:17:24 -0800366 * Legacy interface that we cannot remove: s390x exposes the storage increment
367 * covered by a memory block, allowing for identifying which memory blocks
368 * comprise a storage increment. Since a memory block spans complete
369 * storage increments nowadays, this interface is basically unused. Other
370 * archs never exposed != 0.
Dave Hansen3947be12005-10-29 18:16:54 -0700371 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100372static ssize_t phys_device_show(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800373 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700374{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800375 struct memory_block *mem = to_memory_block(dev);
David Hildenbrande9a2e482021-02-25 17:17:24 -0800376 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Joe Perches948b3ed2020-09-16 13:40:42 -0700377
David Hildenbrande9a2e482021-02-25 17:17:24 -0800378 return sysfs_emit(buf, "%d\n",
379 arch_get_memory_phys_device(start_pfn));
Dave Hansen3947be12005-10-29 18:16:54 -0700380}
381
Zhang Zhened2f2402014-10-09 15:26:31 -0700382#ifdef CONFIG_MEMORY_HOTREMOVE
Joe Perches973c3912020-09-16 13:40:40 -0700383static int print_allowed_zone(char *buf, int len, int nid,
David Hildenbrand445fcf72021-09-07 19:55:45 -0700384 struct memory_group *group,
Joe Perches973c3912020-09-16 13:40:40 -0700385 unsigned long start_pfn, unsigned long nr_pages,
386 int online_type, struct zone *default_zone)
Michal Hockoe5e68932017-09-06 16:19:37 -0700387{
388 struct zone *zone;
389
David Hildenbrand445fcf72021-09-07 19:55:45 -0700390 zone = zone_for_pfn_range(online_type, nid, group, start_pfn, nr_pages);
Joe Perches973c3912020-09-16 13:40:40 -0700391 if (zone == default_zone)
392 return 0;
Joe Perches948b3ed2020-09-16 13:40:42 -0700393
Joe Perches973c3912020-09-16 13:40:40 -0700394 return sysfs_emit_at(buf, len, " %s", zone->name);
Michal Hockoe5e68932017-09-06 16:19:37 -0700395}
396
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100397static ssize_t valid_zones_show(struct device *dev,
Zhang Zhened2f2402014-10-09 15:26:31 -0700398 struct device_attribute *attr, char *buf)
399{
400 struct memory_block *mem = to_memory_block(dev);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700401 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Zhang Zhened2f2402014-10-09 15:26:31 -0700402 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
David Hildenbrand445fcf72021-09-07 19:55:45 -0700403 struct memory_group *group = mem->group;
Michal Hockoe5e68932017-09-06 16:19:37 -0700404 struct zone *default_zone;
David Hildenbrand445fcf72021-09-07 19:55:45 -0700405 int nid = mem->nid;
Joe Perches973c3912020-09-16 13:40:40 -0700406 int len = 0;
Zhang Zhened2f2402014-10-09 15:26:31 -0700407
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700408 /*
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700409 * Check the existing zone. Make sure that we do that only on the
410 * online nodes otherwise the page_zone is not reliable
411 */
412 if (mem->state == MEM_ONLINE) {
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700413 /*
414 * The block contains more than one zone can not be offlined.
415 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
416 */
David Hildenbrand92917992020-02-03 17:34:26 -0800417 default_zone = test_pages_in_a_zone(start_pfn,
418 start_pfn + nr_pages);
419 if (!default_zone)
Joe Perches973c3912020-09-16 13:40:40 -0700420 return sysfs_emit(buf, "%s\n", "none");
421 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700422 goto out;
Zhang Zhened2f2402014-10-09 15:26:31 -0700423 }
424
David Hildenbrand445fcf72021-09-07 19:55:45 -0700425 default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, group,
426 start_pfn, nr_pages);
Zhang Zhened2f2402014-10-09 15:26:31 -0700427
Joe Perches973c3912020-09-16 13:40:40 -0700428 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
David Hildenbrand445fcf72021-09-07 19:55:45 -0700429 len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
Joe Perches973c3912020-09-16 13:40:40 -0700430 MMOP_ONLINE_KERNEL, default_zone);
David Hildenbrand445fcf72021-09-07 19:55:45 -0700431 len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
Joe Perches973c3912020-09-16 13:40:40 -0700432 MMOP_ONLINE_MOVABLE, default_zone);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700433out:
Joe Perches948b3ed2020-09-16 13:40:42 -0700434 len += sysfs_emit_at(buf, len, "\n");
Joe Perches973c3912020-09-16 13:40:40 -0700435 return len;
Zhang Zhened2f2402014-10-09 15:26:31 -0700436}
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100437static DEVICE_ATTR_RO(valid_zones);
Zhang Zhened2f2402014-10-09 15:26:31 -0700438#endif
439
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100440static DEVICE_ATTR_RO(phys_index);
441static DEVICE_ATTR_RW(state);
442static DEVICE_ATTR_RO(phys_device);
443static DEVICE_ATTR_RO(removable);
Dave Hansen3947be12005-10-29 18:16:54 -0700444
Dave Hansen3947be12005-10-29 18:16:54 -0700445/*
David Hildenbrandf915fb72019-09-23 15:35:43 -0700446 * Show the memory block size (shared by all memory blocks).
Dave Hansen3947be12005-10-29 18:16:54 -0700447 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100448static ssize_t block_size_bytes_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700450{
Joe Perchesaa838892020-09-16 13:40:39 -0700451 return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
Dave Hansen3947be12005-10-29 18:16:54 -0700452}
453
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100454static DEVICE_ATTR_RO(block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700455
Dave Hansen3947be12005-10-29 18:16:54 -0700456/*
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700457 * Memory auto online policy.
458 */
459
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100460static ssize_t auto_online_blocks_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700462{
Joe Perchesaa838892020-09-16 13:40:39 -0700463 return sysfs_emit(buf, "%s\n",
Anshuman Khandual1adf8b42021-02-25 17:17:13 -0800464 online_type_to_str[mhp_default_online_type]);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700465}
466
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100467static ssize_t auto_online_blocks_store(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700470{
Anshuman Khandual1adf8b42021-02-25 17:17:13 -0800471 const int online_type = mhp_online_type_from_str(buf);
David Hildenbrand5f47adf2020-04-06 20:07:44 -0700472
473 if (online_type < 0)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700474 return -EINVAL;
475
Anshuman Khandual1adf8b42021-02-25 17:17:13 -0800476 mhp_default_online_type = online_type;
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700477 return count;
478}
479
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100480static DEVICE_ATTR_RW(auto_online_blocks);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700481
482/*
Dave Hansen3947be12005-10-29 18:16:54 -0700483 * Some architectures will have custom drivers to do this, and
484 * will not need to do it from userspace. The fake hot-add code
485 * as well as ppc64 will do all of their discovery in userspace
486 * and will require this interface.
487 */
488#ifdef CONFIG_ARCH_MEMORY_PROBE
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100489static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
490 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700491{
492 u64 phys_addr;
John Allencb5490a2016-01-14 15:22:16 -0800493 int nid, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000494 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700495
Zhang Zhenb69deb22014-08-06 16:06:06 -0700496 ret = kstrtoull(buf, 0, &phys_addr);
497 if (ret)
498 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700499
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000500 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
501 return -EINVAL;
502
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700503 ret = lock_device_hotplug_sysfs();
504 if (ret)
zhong jiang37803842019-04-18 17:50:16 -0700505 return ret;
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700506
John Allencb5490a2016-01-14 15:22:16 -0800507 nid = memory_add_physaddr_to_nid(phys_addr);
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700508 ret = __add_memory(nid, phys_addr,
David Hildenbrandb6117192020-10-15 20:08:44 -0700509 MIN_MEMORY_BLOCK_SIZE * sections_per_block,
510 MHP_NONE);
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600511
John Allencb5490a2016-01-14 15:22:16 -0800512 if (ret)
513 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700514
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530515 ret = count;
516out:
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700517 unlock_device_hotplug();
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530518 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700519}
Dave Hansen3947be12005-10-29 18:16:54 -0700520
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100521static DEVICE_ATTR_WO(probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700522#endif
523
Andi Kleenfacb6012009-12-16 12:20:00 +0100524#ifdef CONFIG_MEMORY_FAILURE
525/*
526 * Support for offlining pages of memory
527 */
528
529/* Soft offline a page */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100530static ssize_t soft_offline_page_store(struct device *dev,
531 struct device_attribute *attr,
532 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100533{
534 int ret;
535 u64 pfn;
536 if (!capable(CAP_SYS_ADMIN))
537 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900538 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100539 return -EINVAL;
540 pfn >>= PAGE_SHIFT;
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800541 ret = soft_offline_page(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100542 return ret == 0 ? count : ret;
543}
544
545/* Forcibly offline a page, including killing processes. */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100546static ssize_t hard_offline_page_store(struct device *dev,
547 struct device_attribute *attr,
548 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100549{
550 int ret;
551 u64 pfn;
552 if (!capable(CAP_SYS_ADMIN))
553 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900554 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100555 return -EINVAL;
556 pfn >>= PAGE_SHIFT;
Eric W. Biederman83b57532017-07-09 18:14:01 -0500557 ret = memory_failure(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100558 return ret ? ret : count;
559}
560
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100561static DEVICE_ATTR_WO(soft_offline_page);
562static DEVICE_ATTR_WO(hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100563#endif
564
David Hildenbrande9a2e482021-02-25 17:17:24 -0800565/* See phys_device_show(). */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400566int __weak arch_get_memory_phys_device(unsigned long start_pfn)
567{
568 return 0;
569}
Dave Hansen3947be12005-10-29 18:16:54 -0700570
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700571/*
572 * A reference for the returned memory block device is acquired.
573 *
574 * Called under device_hotplug_lock.
575 */
David Hildenbranddd625282019-07-18 15:57:53 -0700576static struct memory_block *find_memory_block_by_id(unsigned long block_id)
Robin Holt98383032010-09-29 14:00:55 -0500577{
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700578 struct memory_block *mem;
Robin Holt98383032010-09-29 14:00:55 -0500579
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700580 mem = xa_load(&memory_blocks, block_id);
581 if (mem)
582 get_device(&mem->dev);
583 return mem;
David Hildenbranddb051a02019-07-18 15:56:56 -0700584}
585
Dave Hansen3947be12005-10-29 18:16:54 -0700586/*
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700587 * Called under device_hotplug_lock.
Dave Hansen3947be12005-10-29 18:16:54 -0700588 */
Ohhoon Kwonfc1f5e92021-09-02 14:57:01 -0700589struct memory_block *find_memory_block(unsigned long section_nr)
Dave Hansen3947be12005-10-29 18:16:54 -0700590{
Ohhoon Kwonfc1f5e92021-09-02 14:57:01 -0700591 unsigned long block_id = memory_block_id(section_nr);
David Hildenbranddd625282019-07-18 15:57:53 -0700592
593 return find_memory_block_by_id(block_id);
Dave Hansen3947be12005-10-29 18:16:54 -0700594}
595
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500596static struct attribute *memory_memblk_attrs[] = {
597 &dev_attr_phys_index.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500598 &dev_attr_state.attr,
599 &dev_attr_phys_device.attr,
600 &dev_attr_removable.attr,
Zhang Zhened2f2402014-10-09 15:26:31 -0700601#ifdef CONFIG_MEMORY_HOTREMOVE
602 &dev_attr_valid_zones.attr,
603#endif
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500604 NULL
605};
606
Rikard Falkeborn5a576762021-05-28 23:34:08 +0200607static const struct attribute_group memory_memblk_attr_group = {
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500608 .attrs = memory_memblk_attrs,
609};
610
611static const struct attribute_group *memory_memblk_attr_groups[] = {
612 &memory_memblk_attr_group,
613 NULL,
614};
615
616/*
617 * register_memory - Setup a sysfs device for a memory block
618 */
619static
620int register_memory(struct memory_block *memory)
621{
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530622 int ret;
623
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500624 memory->dev.bus = &memory_subsys;
625 memory->dev.id = memory->start_section_nr / sections_per_block;
626 memory->dev.release = memory_block_release;
627 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700628 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500629
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530630 ret = device_register(&memory->dev);
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700631 if (ret) {
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530632 put_device(&memory->dev);
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700633 return ret;
634 }
635 ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
636 GFP_KERNEL));
637 if (ret) {
638 put_device(&memory->dev);
639 device_unregister(&memory->dev);
640 }
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530641 return ret;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500642}
643
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700644static int init_memory_block(unsigned long block_id, unsigned long state,
David Hildenbrand028fc572021-09-07 19:55:26 -0700645 unsigned long nr_vmemmap_pages,
646 struct memory_group *group)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500647{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600648 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500649 int ret = 0;
650
David Hildenbranddd625282019-07-18 15:57:53 -0700651 mem = find_memory_block_by_id(block_id);
David Hildenbranddb051a02019-07-18 15:56:56 -0700652 if (mem) {
653 put_device(&mem->dev);
654 return -EEXIST;
655 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600656 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500657 if (!mem)
658 return -ENOMEM;
659
David Hildenbrand18115822019-07-18 15:56:46 -0700660 mem->start_section_nr = block_id * sections_per_block;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500661 mem->state = state;
David Hildenbrandd84f2f52019-09-23 15:35:40 -0700662 mem->nid = NUMA_NO_NODE;
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700663 mem->nr_vmemmap_pages = nr_vmemmap_pages;
David Hildenbrand028fc572021-09-07 19:55:26 -0700664 INIT_LIST_HEAD(&mem->group_next);
665
666 if (group) {
667 mem->group = group;
668 list_add(&mem->group_next, &group->memory_blocks);
669 }
Nathan Fontenote4619c82010-10-19 12:44:20 -0500670
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600671 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600672
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600673 return ret;
674}
675
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700676static int add_memory_block(unsigned long base_section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600677{
David Hildenbrand68c3a6a2020-04-06 20:06:40 -0700678 int section_count = 0;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700679 unsigned long nr;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600680
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700681 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
682 nr++)
683 if (present_section_nr(nr))
David Hildenbrand18115822019-07-18 15:56:46 -0700684 section_count++;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600685
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500686 if (section_count == 0)
687 return 0;
Wei Yang178bdbe2020-06-23 10:57:01 +0800688 return init_memory_block(memory_block_id(base_section_nr),
David Hildenbrand028fc572021-09-07 19:55:26 -0700689 MEM_ONLINE, 0, NULL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500690}
691
David Hildenbranddb051a02019-07-18 15:56:56 -0700692static void unregister_memory(struct memory_block *memory)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700693{
David Hildenbranddb051a02019-07-18 15:56:56 -0700694 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
695 return;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700696
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700697 WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
698
David Hildenbrand028fc572021-09-07 19:55:26 -0700699 if (memory->group) {
700 list_del(&memory->group_next);
701 memory->group = NULL;
702 }
703
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700704 /* drop the ref. we got via find_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500705 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700706 device_unregister(&memory->dev);
707}
708
David Hildenbranddb051a02019-07-18 15:56:56 -0700709/*
710 * Create memory block devices for the given memory area. Start and size
711 * have to be aligned to memory block granularity. Memory block devices
712 * will be initialized as offline.
David Hildenbrand848e19a2019-11-30 17:54:14 -0800713 *
714 * Called under device_hotplug_lock.
David Hildenbranddb051a02019-07-18 15:56:56 -0700715 */
Oscar Salvadora08a2ae2021-05-04 18:39:42 -0700716int create_memory_block_devices(unsigned long start, unsigned long size,
David Hildenbrand028fc572021-09-07 19:55:26 -0700717 unsigned long vmemmap_pages,
718 struct memory_group *group)
David Hildenbranddb051a02019-07-18 15:56:56 -0700719{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700720 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
721 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
David Hildenbranddb051a02019-07-18 15:56:56 -0700722 struct memory_block *mem;
723 unsigned long block_id;
724 int ret = 0;
725
726 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
727 !IS_ALIGNED(size, memory_block_size_bytes())))
728 return -EINVAL;
729
David Hildenbranddb051a02019-07-18 15:56:56 -0700730 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
David Hildenbrand028fc572021-09-07 19:55:26 -0700731 ret = init_memory_block(block_id, MEM_OFFLINE, vmemmap_pages,
732 group);
David Hildenbranddb051a02019-07-18 15:56:56 -0700733 if (ret)
734 break;
David Hildenbranddb051a02019-07-18 15:56:56 -0700735 }
736 if (ret) {
737 end_block_id = block_id;
738 for (block_id = start_block_id; block_id != end_block_id;
739 block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700740 mem = find_memory_block_by_id(block_id);
David Hildenbrand848e19a2019-11-30 17:54:14 -0800741 if (WARN_ON_ONCE(!mem))
742 continue;
David Hildenbranddb051a02019-07-18 15:56:56 -0700743 unregister_memory(mem);
744 }
745 }
David Hildenbranddb051a02019-07-18 15:56:56 -0700746 return ret;
747}
748
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700749/*
750 * Remove memory block devices for the given memory area. Start and size
751 * have to be aligned to memory block granularity. Memory block devices
752 * have to be offline.
David Hildenbrand848e19a2019-11-30 17:54:14 -0800753 *
754 * Called under device_hotplug_lock.
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700755 */
756void remove_memory_block_devices(unsigned long start, unsigned long size)
Dave Hansen3947be12005-10-29 18:16:54 -0700757{
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700758 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
759 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
Dave Hansen3947be12005-10-29 18:16:54 -0700760 struct memory_block *mem;
David Hildenbrand90ec010f2019-07-18 15:57:40 -0700761 unsigned long block_id;
Dave Hansen3947be12005-10-29 18:16:54 -0700762
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700763 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
764 !IS_ALIGNED(size, memory_block_size_bytes())))
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700765 return;
766
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700767 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700768 mem = find_memory_block_by_id(block_id);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700769 if (WARN_ON_ONCE(!mem))
770 continue;
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700771 unregister_memory_block_under_nodes(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600772 unregister_memory(mem);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700773 }
Dave Hansen3947be12005-10-29 18:16:54 -0700774}
775
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800776/* return true if the memory block is offlined, otherwise, return false */
777bool is_memblock_offlined(struct memory_block *mem)
778{
779 return mem->state == MEM_OFFLINE;
780}
781
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500782static struct attribute *memory_root_attrs[] = {
783#ifdef CONFIG_ARCH_MEMORY_PROBE
784 &dev_attr_probe.attr,
785#endif
786
787#ifdef CONFIG_MEMORY_FAILURE
788 &dev_attr_soft_offline_page.attr,
789 &dev_attr_hard_offline_page.attr,
790#endif
791
792 &dev_attr_block_size_bytes.attr,
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700793 &dev_attr_auto_online_blocks.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500794 NULL
795};
796
Rikard Falkeborn5a576762021-05-28 23:34:08 +0200797static const struct attribute_group memory_root_attr_group = {
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500798 .attrs = memory_root_attrs,
799};
800
801static const struct attribute_group *memory_root_attr_groups[] = {
802 &memory_root_attr_group,
803 NULL,
804};
805
Wen Congyange90bdb72012-10-08 16:34:01 -0700806/*
David Hildenbrand848e19a2019-11-30 17:54:14 -0800807 * Initialize the sysfs support for memory devices. At the time this function
808 * is called, we cannot have concurrent creation/deletion of memory block
809 * devices, the device_hotplug_lock is not needed.
Dave Hansen3947be12005-10-29 18:16:54 -0700810 */
David Hildenbrand902ce63b2019-09-23 15:35:46 -0700811void __init memory_dev_init(void)
Dave Hansen3947be12005-10-29 18:16:54 -0700812{
Dave Hansen3947be12005-10-29 18:16:54 -0700813 int ret;
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700814 unsigned long block_sz, nr;
Dave Hansen3947be12005-10-29 18:16:54 -0700815
David Hildenbrand902ce63b2019-09-23 15:35:46 -0700816 /* Validate the configured memory block size */
817 block_sz = memory_block_size_bytes();
818 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
819 panic("Memory block size not suitable: 0x%lx\n", block_sz);
820 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
821
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500822 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800823 if (ret)
David Hildenbrand848e19a2019-11-30 17:54:14 -0800824 panic("%s() failed to register subsystem: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700825
826 /*
827 * Create entries for memory sections that were found
828 * during boot and have been initialized
829 */
David Hildenbrand2491f0a2019-07-18 15:57:37 -0700830 for (nr = 0; nr <= __highest_present_section_nr;
831 nr += sections_per_block) {
David Hildenbrand848e19a2019-11-30 17:54:14 -0800832 ret = add_memory_block(nr);
833 if (ret)
834 panic("%s() failed to add memory block: %d\n", __func__,
835 ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700836 }
Dave Hansen3947be12005-10-29 18:16:54 -0700837}
David Hildenbrandea884642019-07-18 15:57:50 -0700838
839/**
840 * walk_memory_blocks - walk through all present memory blocks overlapped
841 * by the range [start, start + size)
842 *
843 * @start: start address of the memory range
844 * @size: size of the memory range
845 * @arg: argument passed to func
846 * @func: callback for each memory section walked
847 *
848 * This function walks through all present memory blocks overlapped by the
849 * range [start, start + size), calling func on each memory block.
850 *
851 * In case func() returns an error, walking is aborted and the error is
852 * returned.
Scott Cheloha4fb6eab2020-06-03 16:03:48 -0700853 *
854 * Called under device_hotplug_lock.
David Hildenbrandea884642019-07-18 15:57:50 -0700855 */
856int walk_memory_blocks(unsigned long start, unsigned long size,
857 void *arg, walk_memory_blocks_func_t func)
858{
859 const unsigned long start_block_id = phys_to_block_id(start);
860 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
861 struct memory_block *mem;
862 unsigned long block_id;
863 int ret = 0;
864
David Hildenbranddd625282019-07-18 15:57:53 -0700865 if (!size)
866 return 0;
867
David Hildenbrandea884642019-07-18 15:57:50 -0700868 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
David Hildenbranddd625282019-07-18 15:57:53 -0700869 mem = find_memory_block_by_id(block_id);
David Hildenbrandea884642019-07-18 15:57:50 -0700870 if (!mem)
871 continue;
872
873 ret = func(mem, arg);
874 put_device(&mem->dev);
875 if (ret)
876 break;
877 }
878 return ret;
879}
David Hildenbrand2c91f8f2019-11-15 17:34:57 -0800880
881struct for_each_memory_block_cb_data {
882 walk_memory_blocks_func_t func;
883 void *arg;
884};
885
886static int for_each_memory_block_cb(struct device *dev, void *data)
887{
888 struct memory_block *mem = to_memory_block(dev);
889 struct for_each_memory_block_cb_data *cb_data = data;
890
891 return cb_data->func(mem, cb_data->arg);
892}
893
894/**
895 * for_each_memory_block - walk through all present memory blocks
896 *
897 * @arg: argument passed to func
898 * @func: callback for each memory block walked
899 *
900 * This function walks through all present memory blocks, calling func on
901 * each memory block.
902 *
903 * In case func() returns an error, walking is aborted and the error is
904 * returned.
905 */
906int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
907{
908 struct for_each_memory_block_cb_data cb_data = {
909 .func = func,
910 .arg = arg,
911 };
912
913 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
914 for_each_memory_block_cb);
915}
David Hildenbrand028fc572021-09-07 19:55:26 -0700916
917/*
918 * This is an internal helper to unify allocation and initialization of
919 * memory groups. Note that the passed memory group will be copied to a
920 * dynamically allocated memory group. After this call, the passed
921 * memory group should no longer be used.
922 */
923static int memory_group_register(struct memory_group group)
924{
925 struct memory_group *new_group;
926 uint32_t mgid;
927 int ret;
928
929 if (!node_possible(group.nid))
930 return -EINVAL;
931
932 new_group = kzalloc(sizeof(group), GFP_KERNEL);
933 if (!new_group)
934 return -ENOMEM;
935 *new_group = group;
936 INIT_LIST_HEAD(&new_group->memory_blocks);
937
938 ret = xa_alloc(&memory_groups, &mgid, new_group, xa_limit_31b,
939 GFP_KERNEL);
940 if (ret) {
941 kfree(new_group);
942 return ret;
David Hildenbrand3fcebf92021-09-07 19:55:48 -0700943 } else if (group.is_dynamic) {
944 xa_set_mark(&memory_groups, mgid, MEMORY_GROUP_MARK_DYNAMIC);
David Hildenbrand028fc572021-09-07 19:55:26 -0700945 }
946 return mgid;
947}
948
949/**
950 * memory_group_register_static() - Register a static memory group.
951 * @nid: The node id.
952 * @max_pages: The maximum number of pages we'll have in this static memory
953 * group.
954 *
955 * Register a new static memory group and return the memory group id.
956 * All memory in the group belongs to a single unit, such as a DIMM. All
957 * memory belonging to a static memory group is added in one go to be removed
958 * in one go -- it's static.
959 *
960 * Returns an error if out of memory, if the node id is invalid, if no new
961 * memory groups can be registered, or if max_pages is invalid (0). Otherwise,
962 * returns the new memory group id.
963 */
964int memory_group_register_static(int nid, unsigned long max_pages)
965{
966 struct memory_group group = {
967 .nid = nid,
968 .s = {
969 .max_pages = max_pages,
970 },
971 };
972
973 if (!max_pages)
974 return -EINVAL;
975 return memory_group_register(group);
976}
977EXPORT_SYMBOL_GPL(memory_group_register_static);
978
979/**
980 * memory_group_register_dynamic() - Register a dynamic memory group.
981 * @nid: The node id.
982 * @unit_pages: Unit in pages in which is memory added/removed in this dynamic
983 * memory group.
984 *
985 * Register a new dynamic memory group and return the memory group id.
986 * Memory within a dynamic memory group is added/removed dynamically
987 * in unit_pages.
988 *
989 * Returns an error if out of memory, if the node id is invalid, if no new
990 * memory groups can be registered, or if unit_pages is invalid (0, not a
991 * power of two, smaller than a single memory block). Otherwise, returns the
992 * new memory group id.
993 */
994int memory_group_register_dynamic(int nid, unsigned long unit_pages)
995{
996 struct memory_group group = {
997 .nid = nid,
998 .is_dynamic = true,
999 .d = {
1000 .unit_pages = unit_pages,
1001 },
1002 };
1003
1004 if (!unit_pages || !is_power_of_2(unit_pages) ||
1005 unit_pages < PHYS_PFN(memory_block_size_bytes()))
1006 return -EINVAL;
1007 return memory_group_register(group);
1008}
1009EXPORT_SYMBOL_GPL(memory_group_register_dynamic);
1010
1011/**
1012 * memory_group_unregister() - Unregister a memory group.
1013 * @mgid: the memory group id
1014 *
1015 * Unregister a memory group. If any memory block still belongs to this
1016 * memory group, unregistering will fail.
1017 *
1018 * Returns -EINVAL if the memory group id is invalid, returns -EBUSY if some
1019 * memory blocks still belong to this memory group and returns 0 if
1020 * unregistering succeeded.
1021 */
1022int memory_group_unregister(int mgid)
1023{
1024 struct memory_group *group;
1025
1026 if (mgid < 0)
1027 return -EINVAL;
1028
1029 group = xa_load(&memory_groups, mgid);
1030 if (!group)
1031 return -EINVAL;
1032 if (!list_empty(&group->memory_blocks))
1033 return -EBUSY;
1034 xa_erase(&memory_groups, mgid);
1035 kfree(group);
1036 return 0;
1037}
1038EXPORT_SYMBOL_GPL(memory_group_unregister);
1039
1040/*
1041 * This is an internal helper only to be used in core memory hotplug code to
1042 * lookup a memory group. We don't care about locking, as we don't expect a
1043 * memory group to get unregistered while adding memory to it -- because
1044 * the group and the memory is managed by the same driver.
1045 */
1046struct memory_group *memory_group_find_by_id(int mgid)
1047{
1048 return xa_load(&memory_groups, mgid);
1049}
David Hildenbrand3fcebf92021-09-07 19:55:48 -07001050
1051/*
1052 * This is an internal helper only to be used in core memory hotplug code to
1053 * walk all dynamic memory groups excluding a given memory group, either
1054 * belonging to a specific node, or belonging to any node.
1055 */
1056int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
1057 struct memory_group *excluded, void *arg)
1058{
1059 struct memory_group *group;
1060 unsigned long index;
1061 int ret = 0;
1062
1063 xa_for_each_marked(&memory_groups, index, group,
1064 MEMORY_GROUP_MARK_DYNAMIC) {
1065 if (group == excluded)
1066 continue;
1067#ifdef CONFIG_NUMA
1068 if (nid != NUMA_NO_NODE && group->nid != nid)
1069 continue;
1070#endif /* CONFIG_NUMA */
1071 ret = func(group, arg);
1072 if (ret)
1073 break;
1074 }
1075 return ret;
1076}