Skip to contentSkip to conversation

powerpc/mm_iommu: Allow pinning large regions

PATCH In review Alexey Kardashevskiy 2 participants 2 messages Apr 2, 2019 – Apr 8, 2019 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/mm_iommu: Allow pinning large regions

When called with vmas_arg==NULL, get_user_pages_longterm() allocates an array of nr_pages*8 which can easily get greater that the max order, for example, registering memory for a 256GB guest does this and fails in __alloc_pages_nodemask().

This adds a loop over chunks of entries to fit the max order limit.

Fixes 678e174c4c16 ("powerpc/mm/iommu: allow migration of cma allocated pages during mm_iommu_do_alloc", 2019-03-05)
---
 arch/powerpc/mm/mmu_context_iommu.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
arch/powerpc/mm/mmu_context_iommu.c +21 -4
index 36a826e23d45..e058064b013c 100644
@@ -131,6 +131,7 @@ long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
 	unsigned int pageshift, mem_pageshift;
 	struct page **hpages;
 	phys_addr_t *hpas;
+	unsigned long entry, chunk, pinned;
 
 	mutex_lock(&mem_list_mutex);
 	if (mm_iommu_find(mm, ua, entries)) {
@@ -152,13 +153,29 @@ long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
 	}
 
 	down_read(&mm->mmap_sem);
-	ret = get_user_pages_longterm(ua, entries, FOLL_WRITE, hpages, NULL);
+	chunk = (1UL << (PAGE_SHIFT + MAX_ORDER - 1)) /
+		sizeof(struct vm_area_struct *);
+	chunk = min(chunk, entries);
+	for (entry = 0, pinned = 0; entry < entries; entry += chunk) {
+		unsigned long n = min(entries - entry, chunk);
+
+		ret = get_user_pages_longterm(ua + (entry << PAGE_SHIFT), n,
+				FOLL_WRITE, hpages + entry, NULL);
+		if (ret == n) {
+			pinned += n;
+			continue;
+		}
+		if (ret >= 0)
+			pinned += ret;
+		break;
+	}
 	up_read(&mm->mmap_sem);
-	if (ret != entries) {
+	if (pinned != entries) {
 		/* free the reference taken */
-		for (i = 0; i < ret; i++)
+		for (i = 0; i < pinned; i++)
 			put_page(hpages[i]);
-		ret = -EFAULT;
+		if (!ret)
+			ret = -EFAULT;
 		goto cleanup_exit;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 26 lines · Alexey Kardashevskiy

When called with vmas_arg==NULL, get_user_pages_longterm() allocates an array of nr_pages*8 which can easily get greater that the max order, for example, registering memory for a 256GB guest does this and fails in __alloc_pages_nodemask().

This adds a loop over chunks of entries to fit the max order limit.

Fixes 678e174c4c16 ("powerpc/mm/iommu: allow migration of cma allocated pages during mm_iommu_do_alloc", 2019-03-05)
---
 arch/powerpc/mm/mmu_context_iommu.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
arch/powerpc/mm/mmu_context_iommu.c +1 -0
index 36a826e23d45..e058064b013c 100644
@@ -131,6 +131,7 @@ long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
 	unsigned int pageshift, mem_pageshift;
 	struct page **hpages;
 	phys_addr_t *hpas;
+	unsigned long entry, chunk, pinned;
 
 	mutex_lock(&mem_list_mutex);
 	if (mm_iommu_find(mm, ua, entries)) {

arch/powerpc/mm/mmu_context_iommu.c long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries, · line 158

Quoted 7 lines
arch/powerpc/mm/mmu_context_iommu.c +3 -1
@@ -152,13 +153,29 @@ long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
 	}
 
 	down_read(&mm->mmap_sem);
-	ret = get_user_pages_longterm(ua, entries, FOLL_WRITE, hpages, NULL);
+	chunk = (1UL << (PAGE_SHIFT + MAX_ORDER - 1)) /
+		sizeof(struct vm_area_struct *);
+	chunk = min(chunk, entries);

I think this is redundant with..

arch/powerpc/mm/mmu_context_iommu.c long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries, · line 160

Quoted 2 lines
arch/powerpc/mm/mmu_context_iommu.c +2 -0
+	for (entry = 0, pinned = 0; entry < entries; entry += chunk) {
+		unsigned long n = min(entries - entry, chunk);

.. this.

But otherwise LGTM.

Quoted 24 lines
arch/powerpc/mm/mmu_context_iommu.c +15 -3
+
+		ret = get_user_pages_longterm(ua + (entry << PAGE_SHIFT), n,
+				FOLL_WRITE, hpages + entry, NULL);
+		if (ret == n) {
+			pinned += n;
+			continue;
+		}
+		if (ret >= 0)
+			pinned += ret;
+		break;
+	}
 	up_read(&mm->mmap_sem);
-	if (ret != entries) {
+	if (pinned != entries) {
 		/* free the reference taken */
-		for (i = 0; i < ret; i++)
+		for (i = 0; i < pinned; i++)
 			put_page(hpages[i]);
-		ret = -EFAULT;
+		if (!ret)
+			ret = -EFAULT;
 		goto cleanup_exit;
 	}
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson