blob: fe4b24f05f6ad0459c060489524289f352adfaec [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
37static inline int base_memory_block_id(int section_nr)
38{
39 return section_nr / sections_per_block;
40}
Dave Hansen3947be12005-10-29 18:16:54 -070041
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020042static int memory_subsys_online(struct device *dev);
43static int memory_subsys_offline(struct device *dev);
44
Kay Sievers10fbcf42011-12-21 14:48:43 -080045static struct bus_type memory_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010046 .name = MEMORY_CLASS_NAME,
Kay Sievers10fbcf42011-12-21 14:48:43 -080047 .dev_name = MEMORY_CLASS_NAME,
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020048 .online = memory_subsys_online,
49 .offline = memory_subsys_offline,
Dave Hansen3947be12005-10-29 18:16:54 -070050};
51
Alan Sterne041c682006-03-27 01:16:30 -080052static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070053
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080054int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070055{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020056 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070057}
Hannes Hering3c82c302008-05-07 14:43:01 +020058EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070059
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080060void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070061{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020062 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070063}
Hannes Hering3c82c302008-05-07 14:43:01 +020064EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070065
Robert Jennings925cc712009-12-17 14:44:38 +000066static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
67
68int register_memory_isolate_notifier(struct notifier_block *nb)
69{
70 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
71}
72EXPORT_SYMBOL(register_memory_isolate_notifier);
73
74void unregister_memory_isolate_notifier(struct notifier_block *nb)
75{
76 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
77}
78EXPORT_SYMBOL(unregister_memory_isolate_notifier);
79
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080080static void memory_block_release(struct device *dev)
81{
Gu Zheng7315f0c2013-08-28 14:38:27 +080082 struct memory_block *mem = to_memory_block(dev);
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080083
84 kfree(mem);
85}
86
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060087unsigned long __weak memory_block_size_bytes(void)
88{
89 return MIN_MEMORY_BLOCK_SIZE;
90}
91
92static unsigned long get_memory_block_size(void)
93{
94 unsigned long block_sz;
95
96 block_sz = memory_block_size_bytes();
97
98 /* Validate blk_sz is a power of 2 and not less than section size */
99 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
100 WARN_ON(1);
101 block_sz = MIN_MEMORY_BLOCK_SIZE;
102 }
103
104 return block_sz;
105}
106
Dave Hansen3947be12005-10-29 18:16:54 -0700107/*
108 * use this as the physical section index that this memsection
109 * uses.
110 */
111
Kay Sievers10fbcf42011-12-21 14:48:43 -0800112static ssize_t show_mem_start_phys_index(struct device *dev,
113 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700114{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800115 struct memory_block *mem = to_memory_block(dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600116 unsigned long phys_index;
117
118 phys_index = mem->start_section_nr / sections_per_block;
119 return sprintf(buf, "%08lx\n", phys_index);
120}
121
Dave Hansen3947be12005-10-29 18:16:54 -0700122/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700123 * Show whether the section of memory is likely to be hot-removable
124 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800125static ssize_t show_mem_removable(struct device *dev,
126 struct device_attribute *attr, char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700127{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600128 unsigned long i, pfn;
129 int ret = 1;
Gu Zheng7315f0c2013-08-28 14:38:27 +0800130 struct memory_block *mem = to_memory_block(dev);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700131
Michal Hocko8b0662f2017-07-06 15:37:53 -0700132 if (mem->state != MEM_ONLINE)
133 goto out;
134
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600135 for (i = 0; i < sections_per_block; i++) {
Russ Anderson21ea9f52013-08-28 16:35:18 -0700136 if (!present_section_nr(mem->start_section_nr + i))
137 continue;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600138 pfn = section_nr_to_pfn(mem->start_section_nr + i);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600139 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
140 }
141
Michal Hocko8b0662f2017-07-06 15:37:53 -0700142out:
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700143 return sprintf(buf, "%d\n", ret);
144}
145
146/*
Dave Hansen3947be12005-10-29 18:16:54 -0700147 * online, offline, going offline, etc.
148 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800149static ssize_t show_mem_state(struct device *dev,
150 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700151{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800152 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700153 ssize_t len = 0;
154
155 /*
156 * We can probably put these states in a nice little array
157 * so that they're not open-coded
158 */
159 switch (mem->state) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200160 case MEM_ONLINE:
161 len = sprintf(buf, "online\n");
162 break;
163 case MEM_OFFLINE:
164 len = sprintf(buf, "offline\n");
165 break;
166 case MEM_GOING_OFFLINE:
167 len = sprintf(buf, "going-offline\n");
168 break;
169 default:
170 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
171 mem->state);
172 WARN_ON(1);
173 break;
Dave Hansen3947be12005-10-29 18:16:54 -0700174 }
175
176 return len;
177}
178
Yasunori Goto7b78d332007-10-21 16:41:36 -0700179int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700180{
Alan Sterne041c682006-03-27 01:16:30 -0800181 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700182}
183
Robert Jennings925cc712009-12-17 14:44:38 +0000184int memory_isolate_notify(unsigned long val, void *v)
185{
186 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
187}
188
Dave Hansen3947be12005-10-29 18:16:54 -0700189/*
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200190 * The probe routines leave the pages reserved, just as the bootmem code does.
191 * Make sure they're still that way.
192 */
Tang Chen6056d612013-04-29 15:08:40 -0700193static bool pages_correctly_reserved(unsigned long start_pfn)
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200194{
195 int i, j;
196 struct page *page;
197 unsigned long pfn = start_pfn;
198
199 /*
200 * memmap between sections is not contiguous except with
201 * SPARSEMEM_VMEMMAP. We lookup the page once per section
202 * and assume memmap is contiguous within each section
203 */
204 for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
205 if (WARN_ON_ONCE(!pfn_valid(pfn)))
206 return false;
207 page = pfn_to_page(pfn);
208
209 for (j = 0; j < PAGES_PER_SECTION; j++) {
210 if (PageReserved(page + j))
211 continue;
212
213 printk(KERN_WARNING "section number %ld page number %d "
214 "not reserved, was it already online?\n",
215 pfn_to_section_nr(pfn), j);
216
217 return false;
218 }
219 }
220
221 return true;
222}
223
224/*
Dave Hansen3947be12005-10-29 18:16:54 -0700225 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
226 * OK to have direct references to sparsemem variables in here.
David Rientjes30467e02015-04-14 15:45:11 -0700227 * Must already be protected by mem_hotplug_begin().
Dave Hansen3947be12005-10-29 18:16:54 -0700228 */
229static int
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800230memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700231{
Wen Congyanga16cee12012-10-08 16:33:58 -0700232 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000233 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700234 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700235
Sheng Yong19c07d52015-04-14 15:44:54 -0700236 start_pfn = section_nr_to_pfn(phys_index);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700237
Dave Hansen3947be12005-10-29 18:16:54 -0700238 switch (action) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200239 case MEM_ONLINE:
240 if (!pages_correctly_reserved(start_pfn))
241 return -EBUSY;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200242
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200243 ret = online_pages(start_pfn, nr_pages, online_type);
244 break;
245 case MEM_OFFLINE:
246 ret = offline_pages(start_pfn, nr_pages);
247 break;
248 default:
249 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
250 "%ld\n", __func__, phys_index, action, action);
251 ret = -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700252 }
Dave Hansen3947be12005-10-29 18:16:54 -0700253
254 return ret;
255}
256
Nathan Fontenotdc18d702017-02-24 15:00:02 -0800257static int memory_block_change_state(struct memory_block *mem,
Seth Jenningsfa2be402013-08-20 16:05:05 -0500258 unsigned long to_state, unsigned long from_state_req)
Dave Hansen3947be12005-10-29 18:16:54 -0700259{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700260 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600261
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200262 if (mem->state != from_state_req)
263 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700264
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600265 if (to_state == MEM_OFFLINE)
266 mem->state = MEM_GOING_OFFLINE;
267
Seth Jenningsfa2be402013-08-20 16:05:05 -0500268 ret = memory_block_action(mem->start_section_nr, to_state,
269 mem->online_type);
270
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200271 mem->state = ret ? from_state_req : to_state;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500272
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200273 return ret;
274}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600275
Seth Jenningsfa2be402013-08-20 16:05:05 -0500276/* The device lock serializes operations on memory_subsys_[online|offline] */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200277static int memory_subsys_online(struct device *dev)
278{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800279 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200280 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700281
Seth Jenningsfa2be402013-08-20 16:05:05 -0500282 if (mem->state == MEM_ONLINE)
283 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200284
Seth Jenningsfa2be402013-08-20 16:05:05 -0500285 /*
286 * If we are called from store_mem_state(), online_type will be
287 * set >= 0 Otherwise we were called from the device online
288 * attribute and need to set the online_type.
289 */
290 if (mem->online_type < 0)
Tang Chen4f7c6b42014-08-06 16:05:13 -0700291 mem->online_type = MMOP_ONLINE_KEEP;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200292
David Rientjes30467e02015-04-14 15:45:11 -0700293 /* Already under protection of mem_hotplug_begin() */
Seth Jenningsfa2be402013-08-20 16:05:05 -0500294 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
295
296 /* clear online_type */
297 mem->online_type = -1;
298
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200299 return ret;
300}
301
302static int memory_subsys_offline(struct device *dev)
303{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800304 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200305
Seth Jenningsfa2be402013-08-20 16:05:05 -0500306 if (mem->state == MEM_OFFLINE)
307 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200308
Seth Jennings26bbe7e2015-12-11 13:40:57 -0800309 /* Can't offline block with non-present sections */
310 if (mem->section_count != sections_per_block)
311 return -EINVAL;
312
Seth Jenningsfa2be402013-08-20 16:05:05 -0500313 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200314}
315
Dave Hansen3947be12005-10-29 18:16:54 -0700316static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800317store_mem_state(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700319{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800320 struct memory_block *mem = to_memory_block(dev);
Seth Jenningsfa2be402013-08-20 16:05:05 -0500321 int ret, online_type;
Dave Hansen3947be12005-10-29 18:16:54 -0700322
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200323 ret = lock_device_hotplug_sysfs();
324 if (ret)
325 return ret;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200326
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700327 if (sysfs_streq(buf, "online_kernel"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700328 online_type = MMOP_ONLINE_KERNEL;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700329 else if (sysfs_streq(buf, "online_movable"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700330 online_type = MMOP_ONLINE_MOVABLE;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700331 else if (sysfs_streq(buf, "online"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700332 online_type = MMOP_ONLINE_KEEP;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700333 else if (sysfs_streq(buf, "offline"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700334 online_type = MMOP_OFFLINE;
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900335 else {
336 ret = -EINVAL;
337 goto err;
338 }
Seth Jenningsfa2be402013-08-20 16:05:05 -0500339
David Rientjes30467e02015-04-14 15:45:11 -0700340 /*
341 * Memory hotplug needs to hold mem_hotplug_begin() for probe to find
342 * the correct memory block to online before doing device_online(dev),
343 * which will take dev->mutex. Take the lock early to prevent an
344 * inversion, memory_subsys_online() callbacks will be implemented by
345 * assuming it's already protected.
346 */
347 mem_hotplug_begin();
348
Seth Jenningsfa2be402013-08-20 16:05:05 -0500349 switch (online_type) {
Tang Chen4f7c6b42014-08-06 16:05:13 -0700350 case MMOP_ONLINE_KERNEL:
351 case MMOP_ONLINE_MOVABLE:
352 case MMOP_ONLINE_KEEP:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500353 mem->online_type = online_type;
354 ret = device_online(&mem->dev);
355 break;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700356 case MMOP_OFFLINE:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500357 ret = device_offline(&mem->dev);
358 break;
359 default:
360 ret = -EINVAL; /* should never happen */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200361 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200362
David Rientjes30467e02015-04-14 15:45:11 -0700363 mem_hotplug_done();
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900364err:
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200365 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600366
Reza Arbabd66ba152016-10-07 17:00:15 -0700367 if (ret < 0)
Dave Hansen3947be12005-10-29 18:16:54 -0700368 return ret;
Reza Arbabd66ba152016-10-07 17:00:15 -0700369 if (ret)
370 return -EINVAL;
371
Dave Hansen3947be12005-10-29 18:16:54 -0700372 return count;
373}
374
375/*
376 * phys_device is a bad name for this. What I really want
377 * is a way to differentiate between memory ranges that
378 * are part of physical devices that constitute
379 * a complete removable unit or fru.
380 * i.e. do these ranges belong to the same physical device,
381 * s.t. if I offline all of these sections I can then
382 * remove the physical device?
383 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800384static ssize_t show_phys_device(struct device *dev,
385 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700386{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800387 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700388 return sprintf(buf, "%d\n", mem->phys_device);
389}
390
Zhang Zhened2f2402014-10-09 15:26:31 -0700391#ifdef CONFIG_MEMORY_HOTREMOVE
Michal Hockoe5e68932017-09-06 16:19:37 -0700392static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
393 unsigned long nr_pages, int online_type,
394 struct zone *default_zone)
395{
396 struct zone *zone;
397
Michal Hockoe5e68932017-09-06 16:19:37 -0700398 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
399 if (zone != default_zone) {
400 strcat(buf, " ");
401 strcat(buf, zone->name);
402 }
403}
404
Zhang Zhened2f2402014-10-09 15:26:31 -0700405static ssize_t show_valid_zones(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct memory_block *mem = to_memory_block(dev);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700409 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Zhang Zhened2f2402014-10-09 15:26:31 -0700410 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700411 unsigned long valid_start_pfn, valid_end_pfn;
Michal Hockoe5e68932017-09-06 16:19:37 -0700412 struct zone *default_zone;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700413 int nid;
Zhang Zhened2f2402014-10-09 15:26:31 -0700414
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700415 /*
416 * The block contains more than one zone can not be offlined.
417 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
418 */
419 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages, &valid_start_pfn, &valid_end_pfn))
Zhang Zhened2f2402014-10-09 15:26:31 -0700420 return sprintf(buf, "none\n");
421
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700422 start_pfn = valid_start_pfn;
423 nr_pages = valid_end_pfn - start_pfn;
Zhang Zhened2f2402014-10-09 15:26:31 -0700424
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700425 /*
426 * Check the existing zone. Make sure that we do that only on the
427 * online nodes otherwise the page_zone is not reliable
428 */
429 if (mem->state == MEM_ONLINE) {
430 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
431 goto out;
Zhang Zhened2f2402014-10-09 15:26:31 -0700432 }
433
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700434 nid = pfn_to_nid(start_pfn);
Michal Hockoe5e68932017-09-06 16:19:37 -0700435 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
436 strcat(buf, default_zone->name);
Zhang Zhened2f2402014-10-09 15:26:31 -0700437
Michal Hockoe5e68932017-09-06 16:19:37 -0700438 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
439 default_zone);
440 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
441 default_zone);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700442out:
Reza Arbaba371d9f2016-07-26 15:22:27 -0700443 strcat(buf, "\n");
444
445 return strlen(buf);
Zhang Zhened2f2402014-10-09 15:26:31 -0700446}
447static DEVICE_ATTR(valid_zones, 0444, show_valid_zones, NULL);
448#endif
449
Kay Sievers10fbcf42011-12-21 14:48:43 -0800450static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
Kay Sievers10fbcf42011-12-21 14:48:43 -0800451static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
452static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
453static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700454
Dave Hansen3947be12005-10-29 18:16:54 -0700455/*
456 * Block size attribute stuff
457 */
458static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800459print_block_size(struct device *dev, struct device_attribute *attr,
Andi Kleen8564a6c2010-01-05 12:48:06 +0100460 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700461{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600462 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700463}
464
Kay Sievers10fbcf42011-12-21 14:48:43 -0800465static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700466
Dave Hansen3947be12005-10-29 18:16:54 -0700467/*
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700468 * Memory auto online policy.
469 */
470
471static ssize_t
472show_auto_online_blocks(struct device *dev, struct device_attribute *attr,
473 char *buf)
474{
475 if (memhp_auto_online)
476 return sprintf(buf, "online\n");
477 else
478 return sprintf(buf, "offline\n");
479}
480
481static ssize_t
482store_auto_online_blocks(struct device *dev, struct device_attribute *attr,
483 const char *buf, size_t count)
484{
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
495static DEVICE_ATTR(auto_online_blocks, 0644, show_auto_online_blocks,
496 store_auto_online_blocks);
497
498/*
Dave Hansen3947be12005-10-29 18:16:54 -0700499 * Some architectures will have custom drivers to do this, and
500 * will not need to do it from userspace. The fake hot-add code
501 * as well as ppc64 will do all of their discovery in userspace
502 * and will require this interface.
503 */
504#ifdef CONFIG_ARCH_MEMORY_PROBE
505static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800506memory_probe_store(struct device *dev, struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100507 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700508{
509 u64 phys_addr;
John Allencb5490a2016-01-14 15:22:16 -0800510 int nid, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000511 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700512
Zhang Zhenb69deb22014-08-06 16:06:06 -0700513 ret = kstrtoull(buf, 0, &phys_addr);
514 if (ret)
515 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700516
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000517 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
518 return -EINVAL;
519
John Allencb5490a2016-01-14 15:22:16 -0800520 nid = memory_add_physaddr_to_nid(phys_addr);
521 ret = add_memory(nid, phys_addr,
522 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600523
John Allencb5490a2016-01-14 15:22:16 -0800524 if (ret)
525 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700526
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530527 ret = count;
528out:
529 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700530}
Dave Hansen3947be12005-10-29 18:16:54 -0700531
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500532static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
Dave Hansen3947be12005-10-29 18:16:54 -0700533#endif
534
Andi Kleenfacb6012009-12-16 12:20:00 +0100535#ifdef CONFIG_MEMORY_FAILURE
536/*
537 * Support for offlining pages of memory
538 */
539
540/* Soft offline a page */
541static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800542store_soft_offline_page(struct device *dev,
543 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100544 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100545{
546 int ret;
547 u64 pfn;
548 if (!capable(CAP_SYS_ADMIN))
549 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900550 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100551 return -EINVAL;
552 pfn >>= PAGE_SHIFT;
553 if (!pfn_valid(pfn))
554 return -ENXIO;
555 ret = soft_offline_page(pfn_to_page(pfn), 0);
556 return ret == 0 ? count : ret;
557}
558
559/* Forcibly offline a page, including killing processes. */
560static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800561store_hard_offline_page(struct device *dev,
562 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100563 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100564{
565 int ret;
566 u64 pfn;
567 if (!capable(CAP_SYS_ADMIN))
568 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900569 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100570 return -EINVAL;
571 pfn >>= PAGE_SHIFT;
Eric W. Biederman83b57532017-07-09 18:14:01 -0500572 ret = memory_failure(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100573 return ret ? ret : count;
574}
575
Felipe Balbi74fef7a2013-02-18 21:09:03 +0200576static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
577static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100578#endif
579
Dave Hansen3947be12005-10-29 18:16:54 -0700580/*
581 * Note that phys_device is optional. It is here to allow for
582 * differentiation between which *physical* devices each
583 * section belongs to...
584 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400585int __weak arch_get_memory_phys_device(unsigned long start_pfn)
586{
587 return 0;
588}
Dave Hansen3947be12005-10-29 18:16:54 -0700589
Kay Sievers10fbcf42011-12-21 14:48:43 -0800590/*
591 * A reference for the returned object is held and the reference for the
592 * hinted object is released.
593 */
Robin Holt98383032010-09-29 14:00:55 -0500594struct memory_block *find_memory_block_hinted(struct mem_section *section,
595 struct memory_block *hint)
596{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600597 int block_id = base_memory_block_id(__section_nr(section));
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
Dave Hansen3947be12005-10-29 18:16:54 -0700609/*
610 * For now, we have a linear search to go find the appropriate
611 * memory_block corresponding to a particular phys_index. If
612 * this gets to be a real problem, we can always use a radix
613 * tree or something here.
614 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800615 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700616 */
Gary Hadec04fc582009-01-06 14:39:14 -0800617struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700618{
Robin Holt98383032010-09-29 14:00:55 -0500619 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700620}
621
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500622static struct attribute *memory_memblk_attrs[] = {
623 &dev_attr_phys_index.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500624 &dev_attr_state.attr,
625 &dev_attr_phys_device.attr,
626 &dev_attr_removable.attr,
Zhang Zhened2f2402014-10-09 15:26:31 -0700627#ifdef CONFIG_MEMORY_HOTREMOVE
628 &dev_attr_valid_zones.attr,
629#endif
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500630 NULL
631};
632
633static struct attribute_group memory_memblk_attr_group = {
634 .attrs = memory_memblk_attrs,
635};
636
637static const struct attribute_group *memory_memblk_attr_groups[] = {
638 &memory_memblk_attr_group,
639 NULL,
640};
641
642/*
643 * register_memory - Setup a sysfs device for a memory block
644 */
645static
646int register_memory(struct memory_block *memory)
647{
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500648 memory->dev.bus = &memory_subsys;
649 memory->dev.id = memory->start_section_nr / sections_per_block;
650 memory->dev.release = memory_block_release;
651 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700652 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500653
Seth Jennings879f1be2013-08-20 12:12:58 -0500654 return device_register(&memory->dev);
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500655}
656
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600657static int init_memory_block(struct memory_block **memory,
658 struct mem_section *section, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500659{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600660 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500661 unsigned long start_pfn;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600662 int scn_nr;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500663 int ret = 0;
664
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600665 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500666 if (!mem)
667 return -ENOMEM;
668
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600669 scn_nr = __section_nr(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600670 mem->start_section_nr =
671 base_memory_block_id(scn_nr) * sections_per_block;
672 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500673 mem->state = state;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600674 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500675 mem->phys_device = arch_get_memory_phys_device(start_pfn);
676
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600677 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600678
679 *memory = mem;
680 return ret;
681}
682
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500683static int add_memory_block(int base_section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600684{
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500685 struct memory_block *mem;
686 int i, ret, section_count = 0, section_nr;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600687
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500688 for (i = base_section_nr;
689 (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS;
690 i++) {
691 if (!present_section_nr(i))
692 continue;
693 if (section_count == 0)
694 section_nr = i;
695 section_count++;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800696 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600697
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500698 if (section_count == 0)
699 return 0;
700 ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
701 if (ret)
702 return ret;
703 mem->section_count = section_count;
704 return 0;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500705}
706
David Rientjes4edd7ce2013-04-29 15:08:22 -0700707/*
708 * need an interface for the VM to add new memory regions,
709 * but without onlining it.
710 */
711int register_new_memory(int nid, struct mem_section *section)
712{
Seth Jenningsd7f80532013-08-20 12:13:00 -0500713 int ret = 0;
714 struct memory_block *mem;
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500715
716 mutex_lock(&mem_sysfs_mutex);
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500717
Seth Jenningsd7f80532013-08-20 12:13:00 -0500718 mem = find_memory_block(section);
719 if (mem) {
720 mem->section_count++;
721 put_device(&mem->dev);
722 } else {
723 ret = init_memory_block(&mem, section, MEM_OFFLINE);
724 if (ret)
725 goto out;
Seth Jennings56c6b5d2016-01-14 15:20:21 -0800726 mem->section_count++;
Seth Jenningsd7f80532013-08-20 12:13:00 -0500727 }
728
729 if (mem->section_count == sections_per_block)
730 ret = register_mem_sect_under_node(mem, nid);
731out:
732 mutex_unlock(&mem_sysfs_mutex);
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500733 return ret;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700734}
735
736#ifdef CONFIG_MEMORY_HOTREMOVE
737static void
738unregister_memory(struct memory_block *memory)
739{
740 BUG_ON(memory->dev.bus != &memory_subsys);
741
742 /* drop the ref. we got in remove_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500743 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700744 device_unregister(&memory->dev);
745}
746
Seth Jenningscc292b02016-01-14 15:20:24 -0800747static int remove_memory_section(unsigned long node_id,
David Rientjes4edd7ce2013-04-29 15:08:22 -0700748 struct mem_section *section, int phys_device)
Dave Hansen3947be12005-10-29 18:16:54 -0700749{
750 struct memory_block *mem;
751
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500752 mutex_lock(&mem_sysfs_mutex);
Michal Hocko1b862ae2017-07-06 15:37:45 -0700753
754 /*
755 * Some users of the memory hotplug do not want/need memblock to
756 * track all sections. Skip over those.
757 */
Dave Hansen3947be12005-10-29 18:16:54 -0700758 mem = find_memory_block(section);
Michal Hocko1b862ae2017-07-06 15:37:45 -0700759 if (!mem)
760 goto out_unlock;
761
Nathan Fontenotd3360162011-01-20 10:44:29 -0600762 unregister_mem_sect_under_nodes(mem, __section_nr(section));
Nathan Fontenot07681212010-10-19 12:46:19 -0500763
764 mem->section_count--;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500765 if (mem->section_count == 0)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600766 unregister_memory(mem);
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500767 else
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500768 put_device(&mem->dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700769
Michal Hocko1b862ae2017-07-06 15:37:45 -0700770out_unlock:
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500771 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700772 return 0;
773}
774
Dave Hansen3947be12005-10-29 18:16:54 -0700775int unregister_memory_section(struct mem_section *section)
776{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700777 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700778 return -EINVAL;
779
Seth Jenningscc292b02016-01-14 15:20:24 -0800780 return remove_memory_section(0, section, 0);
Dave Hansen3947be12005-10-29 18:16:54 -0700781}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700782#endif /* CONFIG_MEMORY_HOTREMOVE */
Dave Hansen3947be12005-10-29 18:16:54 -0700783
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800784/* return true if the memory block is offlined, otherwise, return false */
785bool is_memblock_offlined(struct memory_block *mem)
786{
787 return mem->state == MEM_OFFLINE;
788}
789
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500790static struct attribute *memory_root_attrs[] = {
791#ifdef CONFIG_ARCH_MEMORY_PROBE
792 &dev_attr_probe.attr,
793#endif
794
795#ifdef CONFIG_MEMORY_FAILURE
796 &dev_attr_soft_offline_page.attr,
797 &dev_attr_hard_offline_page.attr,
798#endif
799
800 &dev_attr_block_size_bytes.attr,
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700801 &dev_attr_auto_online_blocks.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500802 NULL
803};
804
805static struct attribute_group memory_root_attr_group = {
806 .attrs = memory_root_attrs,
807};
808
809static const struct attribute_group *memory_root_attr_groups[] = {
810 &memory_root_attr_group,
811 NULL,
812};
813
Wen Congyange90bdb72012-10-08 16:34:01 -0700814/*
Dave Hansen3947be12005-10-29 18:16:54 -0700815 * Initialize the sysfs support for memory devices...
816 */
817int __init memory_dev_init(void)
818{
819 unsigned int i;
820 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800821 int err;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600822 unsigned long block_sz;
Dave Hansen3947be12005-10-29 18:16:54 -0700823
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500824 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800825 if (ret)
826 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700827
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600828 block_sz = get_memory_block_size();
829 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
830
Dave Hansen3947be12005-10-29 18:16:54 -0700831 /*
832 * Create entries for memory sections that were found
833 * during boot and have been initialized
834 */
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500835 mutex_lock(&mem_sysfs_mutex);
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500836 for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
Dave Hansenc4e1be92017-07-06 15:36:44 -0700837 /* Don't iterate over sections we know are !present: */
838 if (i > __highest_present_section_nr)
839 break;
840
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500841 err = add_memory_block(i);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800842 if (!ret)
843 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700844 }
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500845 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700846
Andrew Morton28ec24e2006-12-06 20:37:29 -0800847out:
848 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800849 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700850 return ret;
851}