linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Liam Howlett <liam.howlett@oracle.com>
To: "linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"maple-tree@lists.infradead.org" <maple-tree@lists.infradead.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Subject: [PATCH v3 27/48] nommu: Convert nommu to using the vma iterator
Date: Tue, 17 Jan 2023 02:34:18 +0000	[thread overview]
Message-ID: <20230117023335.1690727-28-Liam.Howlett@oracle.com> (raw)
In-Reply-To: <20230117023335.1690727-1-Liam.Howlett@oracle.com>

From: "Liam R. Howlett" <Liam.Howlett@oracle.com>

Gain type safety in nommu by using the vma_iterator and not the maple
tree directly.

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 mm/nommu.c | 79 +++++++++++++++++++++---------------------------------
 1 file changed, 31 insertions(+), 48 deletions(-)

diff --git a/mm/nommu.c b/mm/nommu.c
index 5b83938ecb67..595f942c6101 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -544,19 +544,6 @@ static void put_nommu_region(struct vm_region *region)
 	__put_nommu_region(region);
 }
 
-void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas)
-{
-	mas_set_range(mas, vma->vm_start, vma->vm_end - 1);
-	mas_store_prealloc(mas, vma);
-}
-
-void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas)
-{
-	mas->index = vma->vm_start;
-	mas->last = vma->vm_end - 1;
-	mas_store_prealloc(mas, NULL);
-}
-
 static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)
 {
 	vma->vm_mm = mm;
@@ -574,13 +561,13 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)
 }
 
 /*
- * mas_add_vma_to_mm() - Maple state variant of add_mas_to_mm().
- * @mas: The maple state with preallocations.
+ * vmi_add_vma_to_mm() - VMA Iterator variant of add_vmi_to_mm().
+ * @vmi: The VMA iterator
  * @mm: The mm_struct
  * @vma: The vma to add
  *
  */
-static void mas_add_vma_to_mm(struct ma_state *mas, struct mm_struct *mm,
+static void vmi_add_vma_to_mm(struct vma_iterator *vmi, struct mm_struct *mm,
 			      struct vm_area_struct *vma)
 {
 	BUG_ON(!vma->vm_region);
@@ -589,7 +576,7 @@ static void mas_add_vma_to_mm(struct ma_state *mas, struct mm_struct *mm,
 	mm->map_count++;
 
 	/* add the VMA to the tree */
-	vma_mas_store(vma, mas);
+	vma_iter_store(vmi, vma);
 }
 
 /*
@@ -600,14 +587,14 @@ static void mas_add_vma_to_mm(struct ma_state *mas, struct mm_struct *mm,
  */
 static int add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
 {
-	MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end);
+	VMA_ITERATOR(vmi, mm, vma->vm_start);
 
-	if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
+	if (vma_iter_prealloc(&vmi, vma)) {
 		pr_warn("Allocation of vma tree for process %d failed\n",
 		       current->pid);
 		return -ENOMEM;
 	}
-	mas_add_vma_to_mm(&mas, mm, vma);
+	vmi_add_vma_to_mm(&vmi, mm, vma);
 	return 0;
 }
 
@@ -626,14 +613,15 @@ static void cleanup_vma_from_mm(struct vm_area_struct *vma)
 		i_mmap_unlock_write(mapping);
 	}
 }
+
 /*
  * delete a VMA from its owning mm_struct and address space
  */
 static int delete_vma_from_mm(struct vm_area_struct *vma)
 {
-	MA_STATE(mas, &vma->vm_mm->mm_mt, 0, 0);
+	VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start);
 
-	if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
+	if (vma_iter_prealloc(&vmi, vma)) {
 		pr_warn("Allocation of vma tree for process %d failed\n",
 		       current->pid);
 		return -ENOMEM;
@@ -641,10 +629,9 @@ static int delete_vma_from_mm(struct vm_area_struct *vma)
 	cleanup_vma_from_mm(vma);
 
 	/* remove from the MM's tree and list */
-	vma_mas_remove(vma, &mas);
+	vma_iter_clear(&vmi, vma->vm_start, vma->vm_end);
 	return 0;
 }
-
 /*
  * destroy a VMA record
  */
@@ -675,9 +662,9 @@ EXPORT_SYMBOL(find_vma_intersection);
  */
 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
 {
-	MA_STATE(mas, &mm->mm_mt, addr, addr);
+	VMA_ITERATOR(vmi, mm, addr);
 
-	return mas_walk(&mas);
+	return vma_iter_load(&vmi);
 }
 EXPORT_SYMBOL(find_vma);
 
@@ -709,9 +696,9 @@ static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
 {
 	struct vm_area_struct *vma;
 	unsigned long end = addr + len;
-	MA_STATE(mas, &mm->mm_mt, addr, addr);
+	VMA_ITERATOR(vmi, mm, addr);
 
-	vma = mas_walk(&mas);
+	vma = vma_iter_load(&vmi);
 	if (!vma)
 		return NULL;
 	if (vma->vm_start != addr)
@@ -1052,7 +1039,7 @@ unsigned long do_mmap(struct file *file,
 	vm_flags_t vm_flags;
 	unsigned long capabilities, result;
 	int ret;
-	MA_STATE(mas, &current->mm->mm_mt, 0, 0);
+	VMA_ITERATOR(vmi, current->mm, 0);
 
 	*populate = 0;
 
@@ -1081,8 +1068,8 @@ unsigned long do_mmap(struct file *file,
 	if (!vma)
 		goto error_getting_vma;
 
-	if (mas_preallocate(&mas, vma, GFP_KERNEL))
-		goto error_maple_preallocate;
+	if (vma_iter_prealloc(&vmi, vma))
+		goto error_vma_iter_prealloc;
 
 	region->vm_usage = 1;
 	region->vm_flags = vm_flags;
@@ -1224,7 +1211,7 @@ unsigned long do_mmap(struct file *file,
 	current->mm->total_vm += len >> PAGE_SHIFT;
 
 share:
-	mas_add_vma_to_mm(&mas, current->mm, vma);
+	vmi_add_vma_to_mm(&vmi, current->mm, vma);
 
 	/* we flush the region from the icache only when the first executable
 	 * mapping of it is made  */
@@ -1240,7 +1227,7 @@ unsigned long do_mmap(struct file *file,
 error_just_free:
 	up_write(&nommu_region_sem);
 error:
-	mas_destroy(&mas);
+	vma_iter_free(&vmi);
 	if (region->vm_file)
 		fput(region->vm_file);
 	kmem_cache_free(vm_region_jar, region);
@@ -1268,7 +1255,7 @@ unsigned long do_mmap(struct file *file,
 	show_free_areas(0, NULL);
 	return -ENOMEM;
 
-error_maple_preallocate:
+error_vma_iter_prealloc:
 	kmem_cache_free(vm_region_jar, region);
 	vm_area_free(vma);
 	pr_warn("Allocation of vma tree for process %d failed\n", current->pid);
@@ -1334,20 +1321,18 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  * split a vma into two pieces at address 'addr', a new vma is allocated either
  * for the first part or the tail.
  */
-int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
-	      unsigned long addr, int new_below)
+int vmi_split_vma(struct vma_iterator *vmi, struct mm_struct *mm,
+		  struct vm_area_struct *vma, unsigned long addr, int new_below)
 {
 	struct vm_area_struct *new;
 	struct vm_region *region;
 	unsigned long npages;
-	MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end);
 
 	/* we're only permitted to split anonymous regions (these should have
 	 * only a single usage on the region) */
 	if (vma->vm_file)
 		return -ENOMEM;
 
-	mm = vma->vm_mm;
 	if (mm->map_count >= sysctl_max_map_count)
 		return -ENOMEM;
 
@@ -1359,10 +1344,10 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
 	if (!new)
 		goto err_vma_dup;
 
-	if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
+	if (vma_iter_prealloc(vmi, vma)) {
 		pr_warn("Allocation of vma tree for process %d failed\n",
 			current->pid);
-		goto err_mas_preallocate;
+		goto err_vmi_preallocate;
 	}
 
 	/* most fields are the same, copy all, and then fixup */
@@ -1396,13 +1381,11 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
 
 	setup_vma_to_mm(vma, mm);
 	setup_vma_to_mm(new, mm);
-	mas_set_range(&mas, vma->vm_start, vma->vm_end - 1);
-	mas_store(&mas, vma);
-	vma_mas_store(new, &mas);
+	vma_iter_store(vmi, new);
 	mm->map_count++;
 	return 0;
 
-err_mas_preallocate:
+err_vmi_preallocate:
 	vm_area_free(new);
 err_vma_dup:
 	kmem_cache_free(vm_region_jar, region);
@@ -1456,7 +1439,7 @@ static int shrink_vma(struct mm_struct *mm,
  */
 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
 {
-	MA_STATE(mas, &mm->mm_mt, start, start);
+	VMA_ITERATOR(vmi, mm, start);
 	struct vm_area_struct *vma;
 	unsigned long end;
 	int ret = 0;
@@ -1468,7 +1451,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
 	end = start + len;
 
 	/* find the first potentially overlapping VMA */
-	vma = mas_find(&mas, end - 1);
+	vma = vma_find(&vmi, end);
 	if (!vma) {
 		static int limit;
 		if (limit < 5) {
@@ -1487,7 +1470,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
 				return -EINVAL;
 			if (end == vma->vm_end)
 				goto erase_whole_vma;
-			vma = mas_next(&mas, end - 1);
+			vma = vma_find(&vmi, end);
 		} while (vma);
 		return -EINVAL;
 	} else {
@@ -1501,7 +1484,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
 		if (end != vma->vm_end && offset_in_page(end))
 			return -EINVAL;
 		if (start != vma->vm_start && end != vma->vm_end) {
-			ret = split_vma(mm, vma, start, 1);
+			ret = vmi_split_vma(&vmi, mm, vma, start, 1);
 			if (ret < 0)
 				return ret;
 		}
-- 
2.35.1


  parent reply	other threads:[~2023-01-17  5:04 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17  2:34 [PATCH v3 00/48] VMA tree type safety and remove __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 01/48] maple_tree: Add mas_init() function Liam Howlett
2023-01-17  2:34 ` [PATCH v3 03/48] maple_tree: Reduce user error potential Liam Howlett
2023-01-17  2:34 ` [PATCH v3 02/48] maple_tree: Fix potential rcu issue Liam Howlett
2023-01-17  2:34 ` [PATCH v3 04/48] test_maple_tree: Test modifications while iterating Liam Howlett
2023-01-17  2:34 ` [PATCH v3 05/48] maple_tree: Fix handle of invalidated state in mas_wr_store_setup() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 07/48] mm: Expand vma iterator interface Liam Howlett
2023-01-17  2:34 ` [PATCH v3 06/48] maple_tree: Fix mas_prev() and mas_find() state handling Liam Howlett
2023-01-17  2:34 ` [PATCH v3 08/48] mm/mmap: convert brk to use vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 09/48] kernel/fork: Convert forking to using the vmi iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 12/48] mmap: Change do_mas_munmap and do_mas_aligned_munmap() to use vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 10/48] mmap: Convert vma_link() " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 11/48] mm/mmap: Remove preallocation from do_mas_align_munmap() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 14/48] mm: Add temporary vma iterator versions of vma_merge(), split_vma(), and __split_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 13/48] mmap: Convert vma_expand() to use vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 15/48] ipc/shm: Use the vma iterator for munmap calls Liam Howlett
2023-01-17  2:34 ` [PATCH v3 16/48] userfaultfd: Use vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 18/48] mlock: Convert mlock to " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 17/48] mm: Change mprotect_fixup " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 21/48] task_mmu: Convert " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 20/48] mempolicy: " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 19/48] coredump: " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 23/48] madvise: Use vmi iterator for __split_vma() and vma_merge() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 24/48] mmap: Pass through vmi iterator to __split_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 22/48] sched: Convert to vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 25/48] mmap: Use vmi version of vma_merge() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 28/48] nommu: Pass through vma iterator to shrink_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 26/48] mm/mremap: Use vmi version of vma_merge() Liam Howlett
2023-01-17  2:34 ` Liam Howlett [this message]
2023-01-17  2:34 ` [PATCH v3 30/48] mm/damon: Stop using vma_mas_store() for maple tree store Liam Howlett
2023-01-17 19:11   ` SeongJae Park
2023-01-17 19:16     ` SeongJae Park
2023-01-17 22:20     ` Daniel Latypov
2023-01-17 22:47       ` Liam Howlett
2023-01-19  2:00         ` SeongJae Park
2023-01-19 18:55           ` Liam R. Howlett
2023-01-17  2:34 ` [PATCH v3 29/48] mm: Switch vma_merge(), split_vma(), and __split_vma to vma iterator Liam Howlett
2023-01-17  2:34 ` [PATCH v3 31/48] mmap: Convert __vma_adjust() to use " Liam Howlett
2023-01-17  2:34 ` [PATCH v3 35/48] mm: Pass vma iterator through to __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 33/48] madvise: Use split_vma() instead of __split_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 34/48] mm: Remove unnecessary write to vma iterator in __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 32/48] mm: Pass through vma iterator to __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 36/48] mm: Add vma iterator to vma_adjust() arguments Liam Howlett
2023-01-17  2:34 ` [PATCH v3 38/48] mm: Change munmap splitting order and move_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 37/48] mmap: Clean up mmap_region() unrolling Liam Howlett
2023-01-17  2:34 ` [PATCH v3 41/48] mm/mmap: Use vma_prepare() and vma_complete() in vma_expand() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 39/48] mm/mmap: move anon_vma setting in __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 42/48] mm/mmap: Introduce init_vma_prep() and init_multi_vma_prep() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 40/48] mm/mmap: Refactor locking out of __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 43/48] mm: Don't use __vma_adjust() in __split_vma() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 44/48] mm/mmap: Don't use __vma_adjust() in shift_arg_pages() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 45/48] mm/mmap: Introduce dup_vma_anon() helper Liam Howlett
2023-01-17  2:34 ` [PATCH v3 47/48] mm/mmap: Remove __vma_adjust() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 46/48] mm/mmap: Convert do_brk_flags() to use vma_prepare() and vma_complete() Liam Howlett
2023-01-17  2:34 ` [PATCH v3 48/48] vma_merge: Set vma iterator to correct position Liam Howlett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230117023335.1690727-28-Liam.Howlett@oracle.com \
    --to=liam.howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maple-tree@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox