2 * IOMMU helpers in MMU context.
4 * Copyright (C) 2015 IBM Corp. <aik@ozlabs.ru>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/sched/signal.h>
14 #include <linux/slab.h>
15 #include <linux/rculist.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mutex.h>
18 #include <linux/migrate.h>
19 #include <linux/hugetlb.h>
20 #include <linux/swap.h>
21 #include <linux/sizes.h>
22 #include <asm/mmu_context.h>
23 #include <asm/pte-walk.h>
25 static DEFINE_MUTEX(mem_list_mutex);
27 #define MM_IOMMU_TABLE_GROUP_PAGE_DIRTY 0x1
28 #define MM_IOMMU_TABLE_GROUP_PAGE_MASK ~(SZ_4K - 1)
30 struct mm_iommu_table_group_mem_t {
31 struct list_head next;
35 unsigned int pageshift;
36 u64 ua; /* userspace address */
37 u64 entries; /* number of entries in hpas[] */
38 u64 *hpas; /* vmalloc'ed */
39 #define MM_IOMMU_TABLE_INVALID_HPA ((uint64_t)-1)
40 u64 dev_hpa; /* Device memory base address */
43 static long mm_iommu_adjust_locked_vm(struct mm_struct *mm,
44 unsigned long npages, bool incr)
46 long ret = 0, locked, lock_limit;
51 down_write(&mm->mmap_sem);
54 locked = mm->locked_vm + npages;
55 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
56 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
59 mm->locked_vm += npages;
61 if (WARN_ON_ONCE(npages > mm->locked_vm))
62 npages = mm->locked_vm;
63 mm->locked_vm -= npages;
66 pr_debug("[%d] RLIMIT_MEMLOCK HASH64 %c%ld %ld/%ld\n",
67 current ? current->pid : 0,
70 mm->locked_vm << PAGE_SHIFT,
71 rlimit(RLIMIT_MEMLOCK));
72 up_write(&mm->mmap_sem);
77 bool mm_iommu_preregistered(struct mm_struct *mm)
79 return !list_empty(&mm->context.iommu_group_mem_list);
81 EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
84 * Taken from alloc_migrate_target with changes to remove CMA allocations
86 struct page *new_iommu_non_cma_page(struct page *page, unsigned long private)
88 gfp_t gfp_mask = GFP_USER;
89 struct page *new_page;
91 if (PageCompound(page))
94 if (PageHighMem(page))
95 gfp_mask |= __GFP_HIGHMEM;
98 * We don't want the allocation to force an OOM if possibe
100 new_page = alloc_page(gfp_mask | __GFP_NORETRY | __GFP_NOWARN);
104 static int mm_iommu_move_page_from_cma(struct page *page)
107 LIST_HEAD(cma_migrate_pages);
109 /* Ignore huge pages for now */
110 if (PageCompound(page))
114 ret = isolate_lru_page(page);
118 list_add(&page->lru, &cma_migrate_pages);
119 put_page(page); /* Drop the gup reference */
121 ret = migrate_pages(&cma_migrate_pages, new_iommu_non_cma_page,
122 NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE);
124 if (!list_empty(&cma_migrate_pages))
125 putback_movable_pages(&cma_migrate_pages);
131 static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
132 unsigned long entries, unsigned long dev_hpa,
133 struct mm_iommu_table_group_mem_t **pmem)
135 struct mm_iommu_table_group_mem_t *mem;
136 long i, j, ret = 0, locked_entries = 0;
137 unsigned int pageshift;
139 unsigned long cur_ua;
140 struct page *page = NULL;
142 mutex_lock(&mem_list_mutex);
144 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list,
147 if ((mem->ua < (ua + (entries << PAGE_SHIFT))) &&
149 (mem->entries << PAGE_SHIFT)))) {
156 if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
157 ret = mm_iommu_adjust_locked_vm(mm, entries, true);
161 locked_entries = entries;
164 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
170 if (dev_hpa != MM_IOMMU_TABLE_INVALID_HPA) {
171 mem->pageshift = __ffs(dev_hpa | (entries << PAGE_SHIFT));
172 mem->dev_hpa = dev_hpa;
175 mem->dev_hpa = MM_IOMMU_TABLE_INVALID_HPA;
178 * For a starting point for a maximum page size calculation
179 * we use @ua and @entries natural alignment to allow IOMMU pages
180 * smaller than huge pages but still bigger than PAGE_SIZE.
182 mem->pageshift = __ffs(ua | (entries << PAGE_SHIFT));
183 mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
190 for (i = 0; i < entries; ++i) {
191 cur_ua = ua + (i << PAGE_SHIFT);
192 if (1 != get_user_pages_fast(cur_ua,
193 1/* pages */, 1/* iswrite */, &page)) {
195 for (j = 0; j < i; ++j)
196 put_page(pfn_to_page(mem->hpas[j] >>
203 * If we get a page from the CMA zone, since we are going to
204 * be pinning these entries, we might as well move them out
205 * of the CMA zone if possible. NOTE: faulting in + migration
206 * can be expensive. Batching can be considered later
208 if (is_migrate_cma_page(page)) {
209 if (mm_iommu_move_page_from_cma(page))
211 if (1 != get_user_pages_fast(cur_ua,
212 1/* pages */, 1/* iswrite */,
215 for (j = 0; j < i; ++j)
216 put_page(pfn_to_page(mem->hpas[j] >>
224 pageshift = PAGE_SHIFT;
225 if (mem->pageshift > PAGE_SHIFT && PageCompound(page)) {
227 struct page *head = compound_head(page);
228 unsigned int compshift = compound_order(head);
229 unsigned int pteshift;
231 local_irq_save(flags); /* disables as well */
232 pte = find_linux_pte(mm->pgd, cur_ua, NULL, &pteshift);
234 /* Double check it is still the same pinned page */
235 if (pte && pte_page(*pte) == head &&
236 pteshift == compshift + PAGE_SHIFT)
237 pageshift = max_t(unsigned int, pteshift,
239 local_irq_restore(flags);
241 mem->pageshift = min(mem->pageshift, pageshift);
242 mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
246 atomic64_set(&mem->mapped, 1);
249 mem->entries = entries;
252 list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
255 if (locked_entries && ret)
256 mm_iommu_adjust_locked_vm(mm, locked_entries, false);
258 mutex_unlock(&mem_list_mutex);
263 long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
264 struct mm_iommu_table_group_mem_t **pmem)
266 return mm_iommu_do_alloc(mm, ua, entries, MM_IOMMU_TABLE_INVALID_HPA,
269 EXPORT_SYMBOL_GPL(mm_iommu_new);
271 long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua,
272 unsigned long entries, unsigned long dev_hpa,
273 struct mm_iommu_table_group_mem_t **pmem)
275 return mm_iommu_do_alloc(mm, ua, entries, dev_hpa, pmem);
277 EXPORT_SYMBOL_GPL(mm_iommu_newdev);
279 static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
282 struct page *page = NULL;
287 for (i = 0; i < mem->entries; ++i) {
291 page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
295 if (mem->hpas[i] & MM_IOMMU_TABLE_GROUP_PAGE_DIRTY)
303 static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
311 static void mm_iommu_free(struct rcu_head *head)
313 struct mm_iommu_table_group_mem_t *mem = container_of(head,
314 struct mm_iommu_table_group_mem_t, rcu);
316 mm_iommu_do_free(mem);
319 static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
321 list_del_rcu(&mem->next);
322 call_rcu(&mem->rcu, mm_iommu_free);
325 long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
328 unsigned long entries, dev_hpa;
330 mutex_lock(&mem_list_mutex);
332 if (mem->used == 0) {
338 /* There are still users, exit */
342 /* Are there still mappings? */
343 if (atomic_cmpxchg(&mem->mapped, 1, 0) != 1) {
349 /* @mapped became 0 so now mappings are disabled, release the region */
350 entries = mem->entries;
351 dev_hpa = mem->dev_hpa;
352 mm_iommu_release(mem);
354 if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
355 mm_iommu_adjust_locked_vm(mm, entries, false);
358 mutex_unlock(&mem_list_mutex);
362 EXPORT_SYMBOL_GPL(mm_iommu_put);
364 struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
365 unsigned long ua, unsigned long size)
367 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
369 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
370 if ((mem->ua <= ua) &&
371 (ua + size <= mem->ua +
372 (mem->entries << PAGE_SHIFT))) {
380 EXPORT_SYMBOL_GPL(mm_iommu_lookup);
382 struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(struct mm_struct *mm,
383 unsigned long ua, unsigned long size)
385 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
387 list_for_each_entry_lockless(mem, &mm->context.iommu_group_mem_list,
389 if ((mem->ua <= ua) &&
390 (ua + size <= mem->ua +
391 (mem->entries << PAGE_SHIFT))) {
400 struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
401 unsigned long ua, unsigned long entries)
403 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
405 mutex_lock(&mem_list_mutex);
407 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
408 if ((mem->ua == ua) && (mem->entries == entries)) {
415 mutex_unlock(&mem_list_mutex);
419 EXPORT_SYMBOL_GPL(mm_iommu_get);
421 long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
422 unsigned long ua, unsigned int pageshift, unsigned long *hpa)
424 const long entry = (ua - mem->ua) >> PAGE_SHIFT;
427 if (entry >= mem->entries)
430 if (pageshift > mem->pageshift)
434 *hpa = mem->dev_hpa + (ua - mem->ua);
438 va = &mem->hpas[entry];
439 *hpa = (*va & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
443 EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
445 long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem,
446 unsigned long ua, unsigned int pageshift, unsigned long *hpa)
448 const long entry = (ua - mem->ua) >> PAGE_SHIFT;
451 if (entry >= mem->entries)
454 if (pageshift > mem->pageshift)
458 *hpa = mem->dev_hpa + (ua - mem->ua);
462 pa = (void *) vmalloc_to_phys(&mem->hpas[entry]);
466 *hpa = (*pa & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
471 extern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua)
473 struct mm_iommu_table_group_mem_t *mem;
478 mem = mm_iommu_lookup_rm(mm, ua, PAGE_SIZE);
482 if (mem->dev_hpa != MM_IOMMU_TABLE_INVALID_HPA)
485 entry = (ua - mem->ua) >> PAGE_SHIFT;
486 va = &mem->hpas[entry];
488 pa = (void *) vmalloc_to_phys(va);
492 *pa |= MM_IOMMU_TABLE_GROUP_PAGE_DIRTY;
495 bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
496 unsigned int pageshift, unsigned long *size)
498 struct mm_iommu_table_group_mem_t *mem;
501 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
502 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
505 end = mem->dev_hpa + (mem->entries << PAGE_SHIFT);
506 if ((mem->dev_hpa <= hpa) && (hpa < end)) {
508 * Since the IOMMU page size might be bigger than
509 * PAGE_SIZE, the amount of preregistered memory
510 * starting from @hpa might be smaller than 1<<pageshift
511 * and the caller needs to distinguish this situation.
513 *size = min(1UL << pageshift, end - hpa);
520 EXPORT_SYMBOL_GPL(mm_iommu_is_devmem);
522 long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
524 if (atomic64_inc_not_zero(&mem->mapped))
527 /* Last mm_iommu_put() has been called, no more mappings allowed() */
530 EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
532 void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
534 atomic64_add_unless(&mem->mapped, -1, 1);
536 EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
538 void mm_iommu_init(struct mm_struct *mm)
540 INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);