mmu_context_iommu.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  IOMMU helpers in MMU context.
 *
 *  Copyright (C) 2015 IBM Corp. <aik@ozlabs.ru>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 *
 */

13
#include <linux/sched/signal.h>
14 15 16 17
#include <linux/slab.h>
#include <linux/rculist.h>
#include <linux/vmalloc.h>
#include <linux/mutex.h>
18 19 20
#include <linux/migrate.h>
#include <linux/hugetlb.h>
#include <linux/swap.h>
21
#include <linux/sizes.h>
22
#include <asm/mmu_context.h>
23
#include <asm/pte-walk.h>
24 25 26

static DEFINE_MUTEX(mem_list_mutex);

27 28 29
#define MM_IOMMU_TABLE_GROUP_PAGE_DIRTY	0x1
#define MM_IOMMU_TABLE_GROUP_PAGE_MASK	~(SZ_4K - 1)

30 31 32 33 34
struct mm_iommu_table_group_mem_t {
	struct list_head next;
	struct rcu_head rcu;
	unsigned long used;
	atomic64_t mapped;
35
	unsigned int pageshift;
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
	u64 ua;			/* userspace address */
	u64 entries;		/* number of entries in hpas[] */
	u64 *hpas;		/* vmalloc'ed */
};

static long mm_iommu_adjust_locked_vm(struct mm_struct *mm,
		unsigned long npages, bool incr)
{
	long ret = 0, locked, lock_limit;

	if (!npages)
		return 0;

	down_write(&mm->mmap_sem);

	if (incr) {
		locked = mm->locked_vm + npages;
		lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
			ret = -ENOMEM;
		else
			mm->locked_vm += npages;
	} else {
		if (WARN_ON_ONCE(npages > mm->locked_vm))
			npages = mm->locked_vm;
		mm->locked_vm -= npages;
	}

	pr_debug("[%d] RLIMIT_MEMLOCK HASH64 %c%ld %ld/%ld\n",
65
			current ? current->pid : 0,
66 67 68 69 70 71 72 73 74
			incr ? '+' : '-',
			npages << PAGE_SHIFT,
			mm->locked_vm << PAGE_SHIFT,
			rlimit(RLIMIT_MEMLOCK));
	up_write(&mm->mmap_sem);

	return ret;
}

75
bool mm_iommu_preregistered(struct mm_struct *mm)
76
{
77
	return !list_empty(&mm->context.iommu_group_mem_list);
78 79 80
}
EXPORT_SYMBOL_GPL(mm_iommu_preregistered);

81 82 83
/*
 * Taken from alloc_migrate_target with changes to remove CMA allocations
 */
84
struct page *new_iommu_non_cma_page(struct page *page, unsigned long private)
85 86 87 88
{
	gfp_t gfp_mask = GFP_USER;
	struct page *new_page;

89
	if (PageCompound(page))
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		return NULL;

	if (PageHighMem(page))
		gfp_mask |= __GFP_HIGHMEM;

	/*
	 * We don't want the allocation to force an OOM if possibe
	 */
	new_page = alloc_page(gfp_mask | __GFP_NORETRY | __GFP_NOWARN);
	return new_page;
}

static int mm_iommu_move_page_from_cma(struct page *page)
{
	int ret = 0;
	LIST_HEAD(cma_migrate_pages);

	/* Ignore huge pages for now */
108
	if (PageCompound(page))
109 110 111 112 113 114 115 116 117 118 119
		return -EBUSY;

	lru_add_drain();
	ret = isolate_lru_page(page);
	if (ret)
		return ret;

	list_add(&page->lru, &cma_migrate_pages);
	put_page(page); /* Drop the gup reference */

	ret = migrate_pages(&cma_migrate_pages, new_iommu_non_cma_page,
120
				NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE);
121 122 123 124 125 126 127 128
	if (ret) {
		if (!list_empty(&cma_migrate_pages))
			putback_movable_pages(&cma_migrate_pages);
	}

	return 0;
}

129
long mm_iommu_get(struct mm_struct *mm, unsigned long ua, unsigned long entries,
130 131 132 133
		struct mm_iommu_table_group_mem_t **pmem)
{
	struct mm_iommu_table_group_mem_t *mem;
	long i, j, ret = 0, locked_entries = 0;
134 135
	unsigned int pageshift;
	unsigned long flags;
136
	unsigned long cur_ua;
137 138 139 140
	struct page *page = NULL;

	mutex_lock(&mem_list_mutex);

141
	list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			next) {
		if ((mem->ua == ua) && (mem->entries == entries)) {
			++mem->used;
			*pmem = mem;
			goto unlock_exit;
		}

		/* Overlap? */
		if ((mem->ua < (ua + (entries << PAGE_SHIFT))) &&
				(ua < (mem->ua +
				       (mem->entries << PAGE_SHIFT)))) {
			ret = -EINVAL;
			goto unlock_exit;
		}

	}

159
	ret = mm_iommu_adjust_locked_vm(mm, entries, true);
160 161 162 163 164 165 166 167 168 169 170
	if (ret)
		goto unlock_exit;

	locked_entries = entries;

	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
	if (!mem) {
		ret = -ENOMEM;
		goto unlock_exit;
	}

171 172 173 174 175 176
	/*
	 * For a starting point for a maximum page size calculation
	 * we use @ua and @entries natural alignment to allow IOMMU pages
	 * smaller than huge pages but still bigger than PAGE_SIZE.
	 */
	mem->pageshift = __ffs(ua | (entries << PAGE_SHIFT));
177
	mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
178 179 180 181 182 183 184
	if (!mem->hpas) {
		kfree(mem);
		ret = -ENOMEM;
		goto unlock_exit;
	}

	for (i = 0; i < entries; ++i) {
185 186
		cur_ua = ua + (i << PAGE_SHIFT);
		if (1 != get_user_pages_fast(cur_ua,
187
					1/* pages */, 1/* iswrite */, &page)) {
188
			ret = -EFAULT;
189
			for (j = 0; j < i; ++j)
190 191
				put_page(pfn_to_page(mem->hpas[j] >>
						PAGE_SHIFT));
192 193 194 195
			vfree(mem->hpas);
			kfree(mem);
			goto unlock_exit;
		}
196 197 198 199 200 201
		/*
		 * If we get a page from the CMA zone, since we are going to
		 * be pinning these entries, we might as well move them out
		 * of the CMA zone if possible. NOTE: faulting in + migration
		 * can be expensive. Batching can be considered later
		 */
202
		if (is_migrate_cma_page(page)) {
203 204
			if (mm_iommu_move_page_from_cma(page))
				goto populate;
205
			if (1 != get_user_pages_fast(cur_ua,
206 207 208 209 210 211 212 213 214 215 216 217
						1/* pages */, 1/* iswrite */,
						&page)) {
				ret = -EFAULT;
				for (j = 0; j < i; ++j)
					put_page(pfn_to_page(mem->hpas[j] >>
								PAGE_SHIFT));
				vfree(mem->hpas);
				kfree(mem);
				goto unlock_exit;
			}
		}
populate:
218
		pageshift = PAGE_SHIFT;
219
		if (mem->pageshift > PAGE_SHIFT && PageCompound(page)) {
220 221 222
			pte_t *pte;
			struct page *head = compound_head(page);
			unsigned int compshift = compound_order(head);
223
			unsigned int pteshift;
224 225

			local_irq_save(flags); /* disables as well */
226
			pte = find_linux_pte(mm->pgd, cur_ua, NULL, &pteshift);
227 228 229

			/* Double check it is still the same pinned page */
			if (pte && pte_page(*pte) == head &&
230 231
			    pteshift == compshift + PAGE_SHIFT)
				pageshift = max_t(unsigned int, pteshift,
232
						PAGE_SHIFT);
233
			local_irq_restore(flags);
234 235
		}
		mem->pageshift = min(mem->pageshift, pageshift);
236 237 238 239 240 241 242 243 244
		mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
	}

	atomic64_set(&mem->mapped, 1);
	mem->used = 1;
	mem->ua = ua;
	mem->entries = entries;
	*pmem = mem;

245
	list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
246 247 248

unlock_exit:
	if (locked_entries && ret)
249
		mm_iommu_adjust_locked_vm(mm, locked_entries, false);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	mutex_unlock(&mem_list_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mm_iommu_get);

static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
{
	long i;
	struct page *page = NULL;

	for (i = 0; i < mem->entries; ++i) {
		if (!mem->hpas[i])
			continue;

		page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
		if (!page)
			continue;

270 271 272
		if (mem->hpas[i] & MM_IOMMU_TABLE_GROUP_PAGE_DIRTY)
			SetPageDirty(page);

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
		put_page(page);
		mem->hpas[i] = 0;
	}
}

static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
{

	mm_iommu_unpin(mem);
	vfree(mem->hpas);
	kfree(mem);
}

static void mm_iommu_free(struct rcu_head *head)
{
	struct mm_iommu_table_group_mem_t *mem = container_of(head,
			struct mm_iommu_table_group_mem_t, rcu);

	mm_iommu_do_free(mem);
}

static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
{
	list_del_rcu(&mem->next);
	call_rcu(&mem->rcu, mm_iommu_free);
}

300
long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
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
{
	long ret = 0;

	mutex_lock(&mem_list_mutex);

	if (mem->used == 0) {
		ret = -ENOENT;
		goto unlock_exit;
	}

	--mem->used;
	/* There are still users, exit */
	if (mem->used)
		goto unlock_exit;

	/* Are there still mappings? */
	if (atomic_cmpxchg(&mem->mapped, 1, 0) != 1) {
		++mem->used;
		ret = -EBUSY;
		goto unlock_exit;
	}

	/* @mapped became 0 so now mappings are disabled, release the region */
	mm_iommu_release(mem);

326 327
	mm_iommu_adjust_locked_vm(mm, mem->entries, false);

328 329 330 331 332 333 334
unlock_exit:
	mutex_unlock(&mem_list_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mm_iommu_put);

335 336
struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
		unsigned long ua, unsigned long size)
337 338 339
{
	struct mm_iommu_table_group_mem_t *mem, *ret = NULL;

340
	list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
341 342 343 344 345 346 347 348 349 350 351 352
		if ((mem->ua <= ua) &&
				(ua + size <= mem->ua +
				 (mem->entries << PAGE_SHIFT))) {
			ret = mem;
			break;
		}
	}

	return ret;
}
EXPORT_SYMBOL_GPL(mm_iommu_lookup);

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(struct mm_struct *mm,
		unsigned long ua, unsigned long size)
{
	struct mm_iommu_table_group_mem_t *mem, *ret = NULL;

	list_for_each_entry_lockless(mem, &mm->context.iommu_group_mem_list,
			next) {
		if ((mem->ua <= ua) &&
				(ua + size <= mem->ua +
				 (mem->entries << PAGE_SHIFT))) {
			ret = mem;
			break;
		}
	}

	return ret;
}

371 372
struct mm_iommu_table_group_mem_t *mm_iommu_find(struct mm_struct *mm,
		unsigned long ua, unsigned long entries)
373 374 375
{
	struct mm_iommu_table_group_mem_t *mem, *ret = NULL;

376
	list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
377 378 379 380 381 382 383 384 385 386 387
		if ((mem->ua == ua) && (mem->entries == entries)) {
			ret = mem;
			break;
		}
	}

	return ret;
}
EXPORT_SYMBOL_GPL(mm_iommu_find);

long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
388
		unsigned long ua, unsigned int pageshift, unsigned long *hpa)
389 390 391 392 393 394 395
{
	const long entry = (ua - mem->ua) >> PAGE_SHIFT;
	u64 *va = &mem->hpas[entry];

	if (entry >= mem->entries)
		return -EFAULT;

396 397 398
	if (pageshift > mem->pageshift)
		return -EFAULT;

399
	*hpa = (*va & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
400 401 402 403 404

	return 0;
}
EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);

405
long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem,
406
		unsigned long ua, unsigned int pageshift, unsigned long *hpa)
407 408 409 410 411 412 413 414
{
	const long entry = (ua - mem->ua) >> PAGE_SHIFT;
	void *va = &mem->hpas[entry];
	unsigned long *pa;

	if (entry >= mem->entries)
		return -EFAULT;

415 416 417
	if (pageshift > mem->pageshift)
		return -EFAULT;

418 419 420 421
	pa = (void *) vmalloc_to_phys(va);
	if (!pa)
		return -EFAULT;

422
	*hpa = (*pa & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
423 424 425

	return 0;
}
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

extern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua)
{
	struct mm_iommu_table_group_mem_t *mem;
	long entry;
	void *va;
	unsigned long *pa;

	mem = mm_iommu_lookup_rm(mm, ua, PAGE_SIZE);
	if (!mem)
		return;

	entry = (ua - mem->ua) >> PAGE_SHIFT;
	va = &mem->hpas[entry];

	pa = (void *) vmalloc_to_phys(va);
	if (!pa)
		return;

	*pa |= MM_IOMMU_TABLE_GROUP_PAGE_DIRTY;
}
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
{
	if (atomic64_inc_not_zero(&mem->mapped))
		return 0;

	/* Last mm_iommu_put() has been called, no more mappings allowed() */
	return -ENXIO;
}
EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);

void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
{
	atomic64_add_unless(&mem->mapped, -1, 1);
}
EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);

464
void mm_iommu_init(struct mm_struct *mm)
465
{
466
	INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);
467
}