summaryrefslogtreecommitdiff
path: root/drivers/dax/kmem.c
blob: a48d699cf344c97ef41327990fca1c5170689252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2016-2019 Intel Corporation. All rights reserved. */
#include <linux/memremap.h>
#include <linux/pagemap.h>
#include <linux/memory.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/dax.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/memory-tiers.h>
#include <linux/memory_hotplug.h>
#include <linux/string_helpers.h>
#include "dax-private.h"
#include "bus.h"

/*
 * Default abstract distance assigned to the NUMA node onlined
 * by DAX/kmem if the low level platform driver didn't initialize
 * one for this NUMA node.
 */
#define MEMTIER_DEFAULT_DAX_ADISTANCE	(MEMTIER_ADISTANCE_DRAM * 5)

/* Memory resource name used for add_memory_driver_managed(). */
static const char *kmem_name;
/* Set if any memory will remain added when the driver will be unloaded. */
static bool any_hotremove_failed;

static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
{
	struct dev_dax_range *dax_range = &dev_dax->ranges[i];
	struct range *range = &dax_range->range;

	*r = memory_block_aligned_range(range);
	if (r->start >= r->end) {
		r->start = range->start;
		r->end = range->end;
		return -ENOSPC;
	}
	return 0;
}

struct dax_kmem_data {
	const char *res_name;
	int mgid;
	int state;
	struct mutex lock; /* protects hotplug state transitions */
	struct resource *res[];
};

static DEFINE_MUTEX(kmem_memory_type_lock);
static LIST_HEAD(kmem_memory_types);

static struct memory_dev_type *kmem_find_alloc_memory_type(int adist)
{
	guard(mutex)(&kmem_memory_type_lock);
	return mt_find_alloc_memory_type(adist, &kmem_memory_types);
}

static void kmem_put_memory_types(void)
{
	guard(mutex)(&kmem_memory_type_lock);
	mt_put_memory_types(&kmem_memory_types);
}

/* True for the online states a kmem dax device can hold. */
static bool dax_kmem_state_is_online(int state)
{
	return state == MMOP_ONLINE ||
	       state == MMOP_ONLINE_KERNEL ||
	       state == MMOP_ONLINE_MOVABLE;
}

/**
 * dax_kmem_do_hotplug - hotplug memory for dax kmem device
 * @dev_dax: the dev_dax instance
 * @data: the dax_kmem_data structure with resource tracking
 * @online_type: the online policy to use for the memory blocks
 *
 * Hotplugs all ranges in the dev_dax region as system memory with the
 * provided online policy (offline, online, online_movable, online_kernel).
 *
 * Returns the number of successfully mapped ranges, or negative error.
 */
static int dax_kmem_do_hotplug(struct dev_dax *dev_dax,
			       struct dax_kmem_data *data,
			       int online_type)
{
	struct device *dev = &dev_dax->dev;
	int i, rc, added = 0;
	mhp_t mhp_flags;

	if (dax_kmem_state_is_online(data->state))
		return -EINVAL;

	if (online_type < MMOP_OFFLINE || online_type > MMOP_ONLINE_MOVABLE)
		return -EINVAL;

	for (i = 0; i < dev_dax->nr_range; i++) {
		struct range range;

		rc = dax_kmem_range(dev_dax, i, &range);
		if (rc)
			continue;

		/*
		 * init_resources() is best-effort: if a reservation conflict
		 * occurs it keeps the range but leaves res[i]=NULL. For hotplug
		 * on probe systems, this means kmem will partially online.
		 *
		 * We have to keep this behavior not to break those systems.
		 * For those systems - atomicity only applies to valid ranges.
		 */
		if (!data->res[i])
			continue;

		mhp_flags = MHP_NID_IS_MGID;
		if (dev_dax->memmap_on_memory)
			mhp_flags |= MHP_MEMMAP_ON_MEMORY;

		/*
		 * Ensure that future kexec'd kernels will not treat
		 * this as RAM automatically.
		 */
		rc = __add_memory_driver_managed(data->mgid, range.start,
				range_len(&range), kmem_name, mhp_flags,
				online_type);

		if (rc) {
			dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
				 i, range.start, range.end);
			/*
			 * Release the reservation for the range that failed to
			 * add so a later hotremove does not try to remove memory
			 * that was never added.
			 */
			if (data->res[i]) {
				remove_resource(data->res[i]);
				kfree(data->res[i]);
				data->res[i] = NULL;
			}
			if (added)
				continue;
			return rc;
		}
		added++;
	}

	return added;
}

/**
 * dax_kmem_init_resources - create memory regions for dax kmem
 * @dev_dax: the dev_dax instance
 * @data: the dax_kmem_data structure with resource tracking
 *
 * Initializes all the resources for the DAX
 *
 * Returns the number of successfully mapped ranges, or negative error.
 */
static int dax_kmem_init_resources(struct dev_dax *dev_dax,
				   struct dax_kmem_data *data)
{
	struct device *dev = &dev_dax->dev;
	int i, rc, mapped = 0;

	for (i = 0; i < dev_dax->nr_range; i++) {
		struct resource *res;
		struct range range;

		rc = dax_kmem_range(dev_dax, i, &range);
		if (rc)
			continue;

		/* Skip ranges already added */
		if (data->res[i])
			continue;

		/* Region is permanently reserved if hotremove fails. */
		res = request_mem_region(range.start, range_len(&range),
					 data->res_name);
		if (!res) {
			dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
				 i, range.start, range.end);
			/*
			 * Once some memory has been onlined we can't
			 * assume that it can be un-onlined safely.
			 */
			if (mapped)
				continue;
			return -EBUSY;
		}
		data->res[i] = res;
		/*
		 * Set flags appropriate for System RAM.  Leave ..._BUSY clear
		 * so that add_memory() can add a child resource.  Do not
		 * inherit flags from the parent since it may set new flags
		 * unknown to us that will break add_memory() later.
		 */
		res->flags = IORESOURCE_SYSTEM_RAM;
		mapped++;
	}
	return mapped;
}

#ifdef CONFIG_MEMORY_HOTREMOVE
/**
 * dax_kmem_do_hotremove - hot-remove memory for dax kmem device
 * @dev_dax: the dev_dax instance
 * @data: the dax_kmem_data structure with resource tracking
 *
 * Offlines and removes every currently-added range in the dev_dax region
 * atomically: either all ranges are offlined and removed, or none are and
 * the device is returned to its prior state.
 *
 * Returns 0 on success, or a negative errno on failure.
 */
static int dax_kmem_do_hotremove(struct dev_dax *dev_dax,
				 struct dax_kmem_data *data)
{
	struct device *dev = &dev_dax->dev;
	struct range *ranges;
	int i, nr_ranges = 0, rc;

	ranges = kmalloc_objs(*ranges, dev_dax->nr_range);
	if (!ranges)
		return -ENOMEM;

	/* Collect the ranges that were actually added during probe. */
	for (i = 0; i < dev_dax->nr_range; i++) {
		struct range range;

		if (!data->res[i])
			continue;
		if (dax_kmem_range(dev_dax, i, &range))
			continue;
		ranges[nr_ranges++] = range;
	}

	/* Nothing added means nothing to remove. */
	if (!nr_ranges) {
		kfree(ranges);
		return 0;
	}

	rc = offline_and_remove_memory_ranges(ranges, nr_ranges);
	kfree(ranges);
	if (rc) {
		/* Recoverable: the ranges rolled back, nothing is leaked yet. */
		dev_err(dev, "hotremove failed, device left online: %d\n", rc);
		return rc;
	}

	/* All ranges removed; release the reserved resources. */
	for (i = 0; i < dev_dax->nr_range; i++) {
		if (!data->res[i])
			continue;
		remove_resource(data->res[i]);
		kfree(data->res[i]);
		data->res[i] = NULL;
	}

	return 0;
}
#else
static int dax_kmem_do_hotremove(struct dev_dax *dev_dax,
				 struct dax_kmem_data *data)
{
	return -EBUSY;
}
#endif /* CONFIG_MEMORY_HOTREMOVE */

/**
 * dax_kmem_cleanup_resources - remove the dax memory resources
 * @dev_dax: the dev_dax instance
 * @data: the dax_kmem_data structure with resource tracking
 *
 * Removes all resources in the dev_dax region.
 */
static void dax_kmem_cleanup_resources(struct dev_dax *dev_dax,
				       struct dax_kmem_data *data)
{
	int i;

	/*
	 * If the device unbind occurs before memory is hotremoved, we can never
	 * remove the memory (requires reboot).  Attempting an offline operation
	 * here may cause deadlock and a failure to finish the unbind.
	 *
	 * Note: This leaks the resources.
	 */
	if (WARN(((data->state != DAX_KMEM_UNPLUGGED) &&
		  (data->state != MMOP_OFFLINE)),
		 "Hotplug memory regions stuck online until reboot"))
		return;

	for (i = 0; i < dev_dax->nr_range; i++) {
		if (!data->res[i])
			continue;
		remove_resource(data->res[i]);
		kfree(data->res[i]);
		data->res[i] = NULL;
	}
}

static int dax_kmem_parse_state(const char *buf)
{
	int online_type;

	/* "unplugged" is kmem-specific - the rest map to MMOP_ */
	if (sysfs_streq(buf, "unplugged"))
		return DAX_KMEM_UNPLUGGED;

	online_type = mhp_online_type_from_str(buf);
	/* Disallow "offline": it's not useful and creates race conditions */
	if (online_type == MMOP_OFFLINE)
		return -EINVAL;
	return online_type;
}

static ssize_t state_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	struct dax_kmem_data *data = dev_get_drvdata(dev);
	const char *state_str;

	if (data->state == DAX_KMEM_UNPLUGGED)
		state_str = "unplugged";
	else
		state_str = mhp_online_type_to_str(data->state);

	return sysfs_emit(buf, "%s\n", state_str ?: "unknown");
}

static ssize_t state_store(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t len)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
	struct dax_kmem_data *data = dev_get_drvdata(dev);
	int online_type;
	int rc;

	online_type = dax_kmem_parse_state(buf);
	if (online_type < DAX_KMEM_UNPLUGGED)
		return online_type;

	guard(mutex)(&data->lock);

	/* Already in requested state */
	if (data->state == online_type)
		return len;

	if (online_type == DAX_KMEM_UNPLUGGED) {
		rc = dax_kmem_do_hotremove(dev_dax, data);
		if (rc)
			return rc;
		data->state = DAX_KMEM_UNPLUGGED;
		return len;
	}

	/* Onlining is only allowed from the unplugged state. */
	if (data->state != DAX_KMEM_UNPLUGGED)
		return -EBUSY;

	/* Re-acquire resources if previously unplugged, otherwise no-op */
	rc = dax_kmem_init_resources(dev_dax, data);
	if (rc < 0)
		return rc;

	rc = dax_kmem_do_hotplug(dev_dax, data, online_type);
	if (rc < 0) {
		/* Total failure, drop the reservations we took. */
		dax_kmem_cleanup_resources(dev_dax, data);
		return rc;
	}

	data->state = online_type;
	return len;
}

static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
{
	struct device *dev = &dev_dax->dev;
	unsigned long total_len = 0, orig_len = 0;
	struct dax_kmem_data *data;
	struct memory_dev_type *mtype;
	int i, rc;
	int numa_node;
	int adist = MEMTIER_DEFAULT_DAX_ADISTANCE;
	int online_type = mhp_get_default_online_type();

	/*
	 * Ensure good NUMA information for the persistent memory.
	 * Without this check, there is a risk that slow memory
	 * could be mixed in a node with faster memory, causing
	 * unavoidable performance issues.
	 */
	numa_node = dev_dax->target_node;
	if (numa_node < 0) {
		dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
				numa_node);
		return -EINVAL;
	}

	mt_calc_adistance(numa_node, &adist);
	mtype = kmem_find_alloc_memory_type(adist);
	if (IS_ERR(mtype))
		return PTR_ERR(mtype);

	for (i = 0; i < dev_dax->nr_range; i++) {
		struct range range;

		orig_len += range_len(&dev_dax->ranges[i].range);
		rc = dax_kmem_range(dev_dax, i, &range);
		if (rc) {
			dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
					i, range.start, range.end);
			continue;
		}
		total_len += range_len(&range);
	}

	if (!total_len) {
		dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
		return -EINVAL;
	} else if (total_len != orig_len) {
		char buf[16];

		string_get_size(orig_len - total_len, 1, STRING_UNITS_2,
				buf, sizeof(buf));
		dev_warn(dev, "DAX region truncated by %s due to alignment\n", buf);
	}

	init_node_memory_type(numa_node, mtype);

	rc = -ENOMEM;
	data = kzalloc_flex(*data, res, dev_dax->nr_range);
	if (!data)
		goto err_dax_kmem_data;

	data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
	if (!data->res_name)
		goto err_res_name;

	rc = memory_group_register_static(numa_node, PFN_UP(total_len));
	if (rc < 0)
		goto err_reg_mgid;
	data->mgid = rc;
	data->state = DAX_KMEM_UNPLUGGED;
	mutex_init(&data->lock);

	dev_set_drvdata(dev, data);

	rc = dax_kmem_init_resources(dev_dax, data);
	if (rc < 0)
		goto err_resources;

	rc = dax_kmem_do_hotplug(dev_dax, data, online_type);
	if (rc < 0)
		goto err_hotplug;
	data->state = online_type;

	return 0;

err_hotplug:
	dax_kmem_cleanup_resources(dev_dax, data);
err_resources:
	dev_set_drvdata(dev, NULL);
	memory_group_unregister(data->mgid);
err_reg_mgid:
	kfree(data->res_name);
err_res_name:
	kfree(data);
err_dax_kmem_data:
	clear_node_memory_type(numa_node, mtype);
	return rc;
}

#ifdef CONFIG_MEMORY_HOTREMOVE
/*
 * Remove the device's added ranges with remove_memory().
 * Unlike the sysfs unplug path it never offlines and fails if the blocks are
 * online (-EBUSY), so it is safe from unbind. Failures leak until reboot.
 *
 * Returns 0 only if every added range was removed.
 */
static int dax_kmem_remove_ranges(struct dev_dax *dev_dax,
				  struct dax_kmem_data *data)
{
	struct device *dev = &dev_dax->dev;
	int i, rc = 0;

	for (i = 0; i < dev_dax->nr_range; i++) {
		struct range range;

		if (!data->res[i] || dax_kmem_range(dev_dax, i, &range))
			continue;
		if (remove_memory(range.start, range_len(&range))) {
			dev_warn(dev, "mapping%d: %#llx-%#llx stuck online until reboot\n",
				 i, range.start, range.end);
			rc = -EBUSY;
			continue;
		}
		remove_resource(data->res[i]);
		kfree(data->res[i]);
		data->res[i] = NULL;
	}
	return rc;
}

static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
{
	int node = dev_dax->target_node;
	struct device *dev = &dev_dax->dev;
	struct dax_kmem_data *data = dev_get_drvdata(dev);

	/*
	 * Remove every range that is still added.  dax_kmem_remove_ranges()
	 * uses remove_memory(), which never offlines: an online block fails
	 * with -EBUSY rather than deadlocking an uninterruptible unbind.
	 *
	 * data->state only tracks daxX.Y/state writes, so it can be stale if
	 * blocks were toggled via memoryX/state. Do not trust it here and
	 * attempt simply remove_memory() - which reports the true state of
	 * each range anyway. Anything left online is leaked until reboot.
	 */
	if (dax_kmem_remove_ranges(dev_dax, data)) {
		dev_err(dev, "Hotplug regions stuck online until reboot\n");
		any_hotremove_failed = true;
		return;
	}

	memory_group_unregister(data->mgid);
	kfree(data->res_name);
	kfree(data);
	dev_set_drvdata(dev, NULL);
	/*
	 * Clear the memtype association on successful unplug.
	 * If not, we have memory blocks left which can be
	 * offlined/onlined later. We need to keep memory_dev_type
	 * for that. This implies this reference will be around
	 * till next reboot.
	 */
	clear_node_memory_type(node, NULL);
}
#else
static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
{
	/*
	 * Without hotremove purposely leak the request_mem_region() for the
	 * device-dax range and return '0' to ->remove() attempts. The removal
	 * of the device from the driver always succeeds, but the region is
	 * permanently pinned as reserved by the unreleased
	 * request_mem_region().
	 */
	any_hotremove_failed = true;
}
#endif /* CONFIG_MEMORY_HOTREMOVE */

static DEVICE_ATTR_RW(state);

static struct attribute *dev_dax_kmem_attrs[] = {
	&dev_attr_state.attr,
	NULL,
};
ATTRIBUTE_GROUPS(dev_dax_kmem);

static struct dax_device_driver device_dax_kmem_driver = {
	.probe = dev_dax_kmem_probe,
	.remove = dev_dax_kmem_remove,
	.type = DAXDRV_KMEM_TYPE,
	.drv = {
		.dev_groups = dev_dax_kmem_groups,
	},
};

static int __init dax_kmem_init(void)
{
	int rc;

	/* Resource name is permanently allocated if any hotremove fails. */
	kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
	if (!kmem_name)
		return -ENOMEM;

	rc = dax_driver_register(&device_dax_kmem_driver);
	if (rc)
		goto error_dax_driver;

	return rc;

error_dax_driver:
	kmem_put_memory_types();
	kfree_const(kmem_name);
	return rc;
}

static void __exit dax_kmem_exit(void)
{
	dax_driver_unregister(&device_dax_kmem_driver);
	if (!any_hotremove_failed)
		kfree_const(kmem_name);
	kmem_put_memory_types();
}

MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("KMEM DAX: map dax-devices as System-RAM");
MODULE_LICENSE("GPL v2");
module_init(dax_kmem_init);
module_exit(dax_kmem_exit);
MODULE_ALIAS_DAX_DEVICE(0);