Skip to contentSkip to conversation

Anonymous VMA naming patches

PATCH Superseded Suren Baghdasaryan 8 participants 29 messages Aug 27, 2021 – Sep 3, 2021

Subsystems (10)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

v8 cover Anonymous VMA naming patches

There were a number of previous attempts to upstream support for anonymous VMA naming. The original submission by Colin Cross [1] implemented a dictionary of refcounted names to reuse same name strings. Dave Hansen suggested [2] to use userspace pointers instead and the patch was rewritten that way. The last v7 version of this patch was posted by Sumit Semwal [3] and a very similar patch has been used in Android to name anonymous VMAs for a number of years. Concerns about this patch were raised by Kees Cook
[4] noting the lack of string sanitization and the use of userspace pointers from the kernel. In conclusion [5], it was suggested to strndup_user the strings from userspace, perform appropriate checks and store a copy as a vm_area_struct member. Performance impact from additional strdup's during fork() should be measured by allocating a large number (64k) of VMAs with longest names and timing fork()s.

This patchset implements the suggested approach in the first 2 patches and the 3rd patch implements simple refcounting to avoid strdup'ing the names during fork() and minimize the regression.

Proposed test was conducted on an ARM64 Android device with CPU frequency locked at 2.4GHz, performance governor and Android system being stopped (adb shell stop) to minimize the noise. Test includes 3 different scenarios. In each scenario a process with 64K named anonymous VMAs forks children 1000 times while timing each fork and reporting the average time. The scenarios differ in the VMA content:

1. VMAs are not populated with any data (not realistic scenario but helps in emphasizing the regression).
2. Each VMA contains 1 page populated with random data.
3. Each VMA contains 10 pages populated with random data.

With the first 2 patches implementing strdup approach, the average fork() times are:

                              unnamed VMAs      named VMAs      REGRESSION
Unpopulated VMAs              16.73ms           23.34ms         39.51%
VMAs with 1 page of data      51.98ms           59.94ms         15.31%
VMAs with 10 pages of data    66.86ms           76.31ms         14.13%

From the perf results, the regression can be attributed to strlen() and strdup() calls. The regression shrinking with the increased amount of populated data can be attributed mostly to anon_vma_fork() and copy_page_range() consuming more time during fork().

After the refcounting implemented in the last patch of this series the results are:

                              unnamed VMAs      named VMAs      REGRESSION
Unpopulated VMAs              16.36ms           18.35ms         12.16%%
VMAs with 1 page of data      48.16ms           51.30ms         6.52%
VMAs with 10 pages of data    64.23ms           67.69ms         5.39%

From the perf results, the regression can be attributed to refcount_inc_checked() (called from kref_get()).

While there is obviously a measurable regression, 64K named anonymous VMAs is truly a worst case scenario. In the real usage, the only current user of this feature, namely Android, rarely has processes with the number of VMAs reaching 4000 (that's the highest I've measured). The regression of forking a process with that number of VMAs is at the noise level.

1. https://lore.kernel.org/linux-mm/[email protected]/
2. https://lore.kernel.org/linux-mm/[email protected]/
3. https://lore.kernel.org/linux-mm/[email protected]/
4. https://lore.kernel.org/linux-mm/202009031031.D32EF57ED@keescook/
5. https://lore.kernel.org/linux-mm/[email protected]/

Colin Cross (2):
mm: rearrange madvise code to allow for reuse
mm: add a field to store names for private anonymous memory

Suren Baghdasaryan (1):
mm: add anonymous vma name refcounting

 Documentation/filesystems/proc.rst |   2 +
 fs/proc/task_mmu.c                 |  14 +-
 fs/userfaultfd.c                   |   7 +-
 include/linux/mm.h                 |  13 +-
 include/linux/mm_types.h           |  55 +++-
 include/uapi/linux/prctl.h         |   3 +
 kernel/fork.c                      |   2 +
 kernel/sys.c                       |  48 ++++
 mm/madvise.c                       | 447 +++++++++++++++++++----------
 mm/mempolicy.c                     |   3 +-
 mm/mlock.c                         |   2 +-
 mm/mmap.c                          |  38 +--
 mm/mprotect.c                      |   2 +-
 13 files changed, 462 insertions(+), 174 deletions(-)
Signature · 1 line
2.33.0.259.gc128427fd7-goog
author reviewer
#1 lore raw

PATCH v8 1/3 mm: rearrange madvise code to allow for reuse

From: Colin Cross <[email protected]>

Refactor the madvise syscall to allow for parts of it to be reused by a prctl syscall that affects vmas.

Move the code that walks vmas in a virtual address range into a function that takes a function pointer as a parameter. The only caller for now is sys_madvise, which uses it to call madvise_vma_behavior on each vma, but the next patch will add an additional caller.

Move handling all vma behaviors inside madvise_behavior, and rename it to madvise_vma_behavior.

Move the code that updates the flags on a vma, including splitting or merging the vma as necessary, into a new function called madvise_update_vma. The next patch will add support for updating a new anon_name field as well.

Cc Pekka Enberg <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Jan Glauber <[email protected]>
Cc John Stultz <[email protected]>
Cc Rob Landley <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc David Rientjes <[email protected]>
Cc Al Viro <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michel Lespinasse <[email protected]>
Cc Tang Chen <[email protected]>
Cc Robin Holt <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
[sumits: rebased over v5.9-rc3]
[surenb: rebased over v5.14-rc7]
---
 mm/madvise.c | 319 +++++++++++++++++++++++++++------------------------
 1 file changed, 172 insertions(+), 147 deletions(-)
mm/madvise.c +172 -147
index 5c065bc8b5f6..359cd3fa612c 100644
@@ -63,76 +63,20 @@ static int madvise_need_mmap_write(int behavior)
 }
 
 /*
- * We can potentially split a vm area into separate
- * areas, each area with its own behavior.
+ * Update the vm_flags on regiion of a vma, splitting it or merging it as
+ * necessary.  Must be called with mmap_sem held for writing;
  */
-static long madvise_behavior(struct vm_area_struct *vma,
-		     struct vm_area_struct **prev,
-		     unsigned long start, unsigned long end, int behavior)
+static int madvise_update_vma(struct vm_area_struct *vma,
+			      struct vm_area_struct **prev, unsigned long start,
+			      unsigned long end, unsigned long new_flags)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	int error = 0;
+	int error;
 	pgoff_t pgoff;
-	unsigned long new_flags = vma->vm_flags;
-
-	switch (behavior) {
-	case MADV_NORMAL:
-		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
-		break;
-	case MADV_SEQUENTIAL:
-		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
-		break;
-	case MADV_RANDOM:
-		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
-		break;
-	case MADV_DONTFORK:
-		new_flags |= VM_DONTCOPY;
-		break;
-	case MADV_DOFORK:
-		if (vma->vm_flags & VM_IO) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags &= ~VM_DONTCOPY;
-		break;
-	case MADV_WIPEONFORK:
-		/* MADV_WIPEONFORK is only supported on anonymous memory. */
-		if (vma->vm_file || vma->vm_flags & VM_SHARED) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags |= VM_WIPEONFORK;
-		break;
-	case MADV_KEEPONFORK:
-		new_flags &= ~VM_WIPEONFORK;
-		break;
-	case MADV_DONTDUMP:
-		new_flags |= VM_DONTDUMP;
-		break;
-	case MADV_DODUMP:
-		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags &= ~VM_DONTDUMP;
-		break;
-	case MADV_MERGEABLE:
-	case MADV_UNMERGEABLE:
-		error = ksm_madvise(vma, start, end, behavior, &new_flags);
-		if (error)
-			goto out_convert_errno;
-		break;
-	case MADV_HUGEPAGE:
-	case MADV_NOHUGEPAGE:
-		error = hugepage_madvise(vma, &new_flags, behavior);
-		if (error)
-			goto out_convert_errno;
-		break;
-	}
 
 	if (new_flags == vma->vm_flags) {
 		*prev = vma;
-		goto out;
+		return 0;
 	}
 
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
@@ -149,21 +93,21 @@ static long madvise_behavior(struct vm_area_struct *vma,
 	if (start != vma->vm_start) {
 		if (unlikely(mm->map_count >= sysctl_max_map_count)) {
 			error = -ENOMEM;
-			goto out;
+			return error;
 		}
 		error = __split_vma(mm, vma, start, 1);
 		if (error)
-			goto out_convert_errno;
+			return error;
 	}
 
 	if (end != vma->vm_end) {
 		if (unlikely(mm->map_count >= sysctl_max_map_count)) {
 			error = -ENOMEM;
-			goto out;
+			return error;
 		}
 		error = __split_vma(mm, vma, end, 0);
 		if (error)
-			goto out_convert_errno;
+			return error;
 	}
 
 success:
@@ -172,15 +116,7 @@ static long madvise_behavior(struct vm_area_struct *vma,
 	 */
 	vma->vm_flags = new_flags;
 
-out_convert_errno:
-	/*
-	 * madvise() returns EAGAIN if kernel resources, such as
-	 * slab, are temporarily unavailable.
-	 */
-	if (error == -ENOMEM)
-		error = -EAGAIN;
-out:
-	return error;
+	return 0;
 }
 
 #ifdef CONFIG_SWAP
@@ -930,6 +866,96 @@ static long madvise_remove(struct vm_area_struct *vma,
 	return error;
 }
 
+/*
+ * Apply an madvise behavior to a region of a vma.  madvise_update_vma
+ * will handle splitting a vm area into separate areas, each area with its own
+ * behavior.
+ */
+static int madvise_vma_behavior(struct vm_area_struct *vma,
+				struct vm_area_struct **prev,
+				unsigned long start, unsigned long end,
+				unsigned long behavior)
+{
+	int error = 0;
+	unsigned long new_flags = vma->vm_flags;
+
+	switch (behavior) {
+	case MADV_REMOVE:
+		return madvise_remove(vma, prev, start, end);
+	case MADV_WILLNEED:
+		return madvise_willneed(vma, prev, start, end);
+	case MADV_COLD:
+		return madvise_cold(vma, prev, start, end);
+	case MADV_PAGEOUT:
+		return madvise_pageout(vma, prev, start, end);
+	case MADV_FREE:
+	case MADV_DONTNEED:
+		return madvise_dontneed_free(vma, prev, start, end, behavior);
+	case MADV_POPULATE_READ:
+	case MADV_POPULATE_WRITE:
+		return madvise_populate(vma, prev, start, end, behavior);
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
+	case MADV_SEQUENTIAL:
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
+		break;
+	case MADV_RANDOM:
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
+		break;
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags &= ~VM_DONTCOPY;
+		break;
+	case MADV_WIPEONFORK:
+		/* MADV_WIPEONFORK is only supported on anonymous memory. */
+		if (vma->vm_file || vma->vm_flags & VM_SHARED) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags |= VM_WIPEONFORK;
+		break;
+	case MADV_KEEPONFORK:
+		new_flags &= ~VM_WIPEONFORK;
+		break;
+	case MADV_DONTDUMP:
+		new_flags |= VM_DONTDUMP;
+		break;
+	case MADV_DODUMP:
+		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags &= ~VM_DONTDUMP;
+		break;
+	case MADV_MERGEABLE:
+	case MADV_UNMERGEABLE:
+		error = ksm_madvise(vma, start, end, behavior, &new_flags);
+		if (error)
+			goto out;
+		break;
+	case MADV_HUGEPAGE:
+	case MADV_NOHUGEPAGE:
+		error = hugepage_madvise(vma, &new_flags, behavior);
+		if (error)
+			goto out;
+		break;
+	}
+
+	error = madvise_update_vma(vma, prev, start, end, new_flags);
+
+out:
+	if (error == -ENOMEM)
+		error = -EAGAIN;
+	return error;
+}
+
 #ifdef CONFIG_MEMORY_FAILURE
 /*
  * Error injection support for memory error handling.
@@ -978,30 +1004,6 @@ static int madvise_inject_error(int behavior,
 }
 #endif
 
-static long
-madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
-		unsigned long start, unsigned long end, int behavior)
-{
-	switch (behavior) {
-	case MADV_REMOVE:
-		return madvise_remove(vma, prev, start, end);
-	case MADV_WILLNEED:
-		return madvise_willneed(vma, prev, start, end);
-	case MADV_COLD:
-		return madvise_cold(vma, prev, start, end);
-	case MADV_PAGEOUT:
-		return madvise_pageout(vma, prev, start, end);
-	case MADV_FREE:
-	case MADV_DONTNEED:
-		return madvise_dontneed_free(vma, prev, start, end, behavior);
-	case MADV_POPULATE_READ:
-	case MADV_POPULATE_WRITE:
-		return madvise_populate(vma, prev, start, end, behavior);
-	default:
-		return madvise_behavior(vma, prev, start, end, behavior);
-	}
-}
-
 static bool
 madvise_behavior_valid(int behavior)
 {
@@ -1054,6 +1056,73 @@ process_madvise_behavior_valid(int behavior)
 	}
 }
 
+/*
+ * Walk the vmas in range [start,end), and call the visit function on each one.
+ * The visit function will get start and end parameters that cover the overlap
+ * between the current vma and the original range.  Any unmapped regions in the
+ * original range will result in this function returning -ENOMEM while still
+ * calling the visit function on all of the existing vmas in the range.
+ * Must be called with the mmap_lock held for reading or writing.
+ */
+static
+int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
+		      unsigned long end, unsigned long arg,
+		      int (*visit)(struct vm_area_struct *vma,
+				   struct vm_area_struct **prev, unsigned long start,
+				   unsigned long end, unsigned long arg))
+{
+	struct vm_area_struct *vma;
+	struct vm_area_struct *prev;
+	unsigned long tmp;
+	int unmapped_error = 0;
+
+	/*
+	 * If the interval [start,end) covers some unmapped address
+	 * ranges, just ignore them, but return -ENOMEM at the end.
+	 * - different from the way of handling in mlock etc.
+	 */
+	vma = find_vma_prev(mm, start, &prev);
+	if (vma && start > vma->vm_start)
+		prev = vma;
+
+	for (;;) {
+		int error;
+
+		/* Still start < end. */
+		if (!vma)
+			return -ENOMEM;
+
+		/* Here start < (end|vma->vm_end). */
+		if (start < vma->vm_start) {
+			unmapped_error = -ENOMEM;
+			start = vma->vm_start;
+			if (start >= end)
+				break;
+		}
+
+		/* Here vma->vm_start <= start < (end|vma->vm_end) */
+		tmp = vma->vm_end;
+		if (end < tmp)
+			tmp = end;
+
+		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
+		error = visit(vma, &prev, start, tmp, arg);
+		if (error)
+			return error;
+		start = tmp;
+		if (prev && start < prev->vm_end)
+			start = prev->vm_end;
+		if (start >= end)
+			break;
+		if (prev)
+			vma = prev->vm_next;
+		else	/* madvise_remove dropped mmap_lock */
+			vma = find_vma(mm, start);
+	}
+
+	return unmapped_error;
+}
+
 /*
  * The madvise(2) system call.
  *
@@ -1126,9 +1195,7 @@ process_madvise_behavior_valid(int behavior)
  */
 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
 {
-	unsigned long end, tmp;
-	struct vm_area_struct *vma, *prev;
-	int unmapped_error = 0;
+	unsigned long end;
 	int error = -EINVAL;
 	int write;
 	size_t len;
@@ -1168,51 +1235,9 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
 		mmap_read_lock(mm);
 	}
 
-	/*
-	 * If the interval [start,end) covers some unmapped address
-	 * ranges, just ignore them, but return -ENOMEM at the end.
-	 * - different from the way of handling in mlock etc.
-	 */
-	vma = find_vma_prev(mm, start, &prev);
-	if (vma && start > vma->vm_start)
-		prev = vma;
-
 	blk_start_plug(&plug);
-	for (;;) {
-		/* Still start < end. */
-		error = -ENOMEM;
-		if (!vma)
-			goto out;
-
-		/* Here start < (end|vma->vm_end). */
-		if (start < vma->vm_start) {
-			unmapped_error = -ENOMEM;
-			start = vma->vm_start;
-			if (start >= end)
-				goto out;
-		}
-
-		/* Here vma->vm_start <= start < (end|vma->vm_end) */
-		tmp = vma->vm_end;
-		if (end < tmp)
-			tmp = end;
-
-		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
-		error = madvise_vma(vma, &prev, start, tmp, behavior);
-		if (error)
-			goto out;
-		start = tmp;
-		if (prev && start < prev->vm_end)
-			start = prev->vm_end;
-		error = unmapped_error;
-		if (start >= end)
-			goto out;
-		if (prev)
-			vma = prev->vm_next;
-		else	/* madvise_remove dropped mmap_lock */
-			vma = find_vma(mm, start);
-	}
-out:
+	error = madvise_walk_vmas(mm, start, end, behavior,
+			madvise_vma_behavior);
 	blk_finish_plug(&plug);
 	if (write)
 		mmap_write_unlock(mm);
Signature · 1 line
2.33.0.259.gc128427fd7-goog
#2 lore raw

Re: [PATCH v8 1/3] mm: rearrange madvise code to allow for reuse

Quoted 40 lines · Suren Baghdasaryan

From: Colin Cross <[email protected]>

Refactor the madvise syscall to allow for parts of it to be reused by a prctl syscall that affects vmas.

Move the code that walks vmas in a virtual address range into a function that takes a function pointer as a parameter. The only caller for now is sys_madvise, which uses it to call madvise_vma_behavior on each vma, but the next patch will add an additional caller.

Move handling all vma behaviors inside madvise_behavior, and rename it to madvise_vma_behavior.

Move the code that updates the flags on a vma, including splitting or merging the vma as necessary, into a new function called madvise_update_vma. The next patch will add support for updating a new anon_name field as well.

Cc Pekka Enberg <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Jan Glauber <[email protected]>
Cc John Stultz <[email protected]>
Cc Rob Landley <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc David Rientjes <[email protected]>
Cc Al Viro <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michel Lespinasse <[email protected]>
Cc Tang Chen <[email protected]>
Cc Robin Holt <[email protected]>
Cc Shaohua Li <[email protected]>
Quoted 8 lines
Cc Sasha Levin <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
[sumits: rebased over v5.9-rc3]
[surenb: rebased over v5.14-rc7]

Other folks have already reviewed this, and it does look okay to me, too, but I find it a bit hard to review. There are at least 3 things happening in this patch:
- moving to the walker
- merging two behavior routines
- extracting flag setting from behavior checking

It seems like those could be separate patches, but I'm probably overly picky. :)

-Kees

Quoted 405 lines
---
 mm/madvise.c | 319 +++++++++++++++++++++++++++------------------------
 1 file changed, 172 insertions(+), 147 deletions(-)
mm/madvise.c +172 -147
index 5c065bc8b5f6..359cd3fa612c 100644
@@ -63,76 +63,20 @@ static int madvise_need_mmap_write(int behavior)
 }
 
 /*
- * We can potentially split a vm area into separate
- * areas, each area with its own behavior.
+ * Update the vm_flags on regiion of a vma, splitting it or merging it as
+ * necessary.  Must be called with mmap_sem held for writing;
  */
-static long madvise_behavior(struct vm_area_struct *vma,
-		     struct vm_area_struct **prev,
-		     unsigned long start, unsigned long end, int behavior)
+static int madvise_update_vma(struct vm_area_struct *vma,
+			      struct vm_area_struct **prev, unsigned long start,
+			      unsigned long end, unsigned long new_flags)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	int error = 0;
+	int error;
 	pgoff_t pgoff;
-	unsigned long new_flags = vma->vm_flags;
-
-	switch (behavior) {
-	case MADV_NORMAL:
-		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
-		break;
-	case MADV_SEQUENTIAL:
-		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
-		break;
-	case MADV_RANDOM:
-		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
-		break;
-	case MADV_DONTFORK:
-		new_flags |= VM_DONTCOPY;
-		break;
-	case MADV_DOFORK:
-		if (vma->vm_flags & VM_IO) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags &= ~VM_DONTCOPY;
-		break;
-	case MADV_WIPEONFORK:
-		/* MADV_WIPEONFORK is only supported on anonymous memory. */
-		if (vma->vm_file || vma->vm_flags & VM_SHARED) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags |= VM_WIPEONFORK;
-		break;
-	case MADV_KEEPONFORK:
-		new_flags &= ~VM_WIPEONFORK;
-		break;
-	case MADV_DONTDUMP:
-		new_flags |= VM_DONTDUMP;
-		break;
-	case MADV_DODUMP:
-		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
-			error = -EINVAL;
-			goto out;
-		}
-		new_flags &= ~VM_DONTDUMP;
-		break;
-	case MADV_MERGEABLE:
-	case MADV_UNMERGEABLE:
-		error = ksm_madvise(vma, start, end, behavior, &new_flags);
-		if (error)
-			goto out_convert_errno;
-		break;
-	case MADV_HUGEPAGE:
-	case MADV_NOHUGEPAGE:
-		error = hugepage_madvise(vma, &new_flags, behavior);
-		if (error)
-			goto out_convert_errno;
-		break;
-	}
 
 	if (new_flags == vma->vm_flags) {
 		*prev = vma;
-		goto out;
+		return 0;
 	}
 
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
@@ -149,21 +93,21 @@ static long madvise_behavior(struct vm_area_struct *vma,
 	if (start != vma->vm_start) {
 		if (unlikely(mm->map_count >= sysctl_max_map_count)) {
 			error = -ENOMEM;
-			goto out;
+			return error;
 		}
 		error = __split_vma(mm, vma, start, 1);
 		if (error)
-			goto out_convert_errno;
+			return error;
 	}
 
 	if (end != vma->vm_end) {
 		if (unlikely(mm->map_count >= sysctl_max_map_count)) {
 			error = -ENOMEM;
-			goto out;
+			return error;
 		}
 		error = __split_vma(mm, vma, end, 0);
 		if (error)
-			goto out_convert_errno;
+			return error;
 	}
 
 success:
@@ -172,15 +116,7 @@ static long madvise_behavior(struct vm_area_struct *vma,
 	 */
 	vma->vm_flags = new_flags;
 
-out_convert_errno:
-	/*
-	 * madvise() returns EAGAIN if kernel resources, such as
-	 * slab, are temporarily unavailable.
-	 */
-	if (error == -ENOMEM)
-		error = -EAGAIN;
-out:
-	return error;
+	return 0;
 }
 
 #ifdef CONFIG_SWAP
@@ -930,6 +866,96 @@ static long madvise_remove(struct vm_area_struct *vma,
 	return error;
 }
 
+/*
+ * Apply an madvise behavior to a region of a vma.  madvise_update_vma
+ * will handle splitting a vm area into separate areas, each area with its own
+ * behavior.
+ */
+static int madvise_vma_behavior(struct vm_area_struct *vma,
+				struct vm_area_struct **prev,
+				unsigned long start, unsigned long end,
+				unsigned long behavior)
+{
+	int error = 0;
+	unsigned long new_flags = vma->vm_flags;
+
+	switch (behavior) {
+	case MADV_REMOVE:
+		return madvise_remove(vma, prev, start, end);
+	case MADV_WILLNEED:
+		return madvise_willneed(vma, prev, start, end);
+	case MADV_COLD:
+		return madvise_cold(vma, prev, start, end);
+	case MADV_PAGEOUT:
+		return madvise_pageout(vma, prev, start, end);
+	case MADV_FREE:
+	case MADV_DONTNEED:
+		return madvise_dontneed_free(vma, prev, start, end, behavior);
+	case MADV_POPULATE_READ:
+	case MADV_POPULATE_WRITE:
+		return madvise_populate(vma, prev, start, end, behavior);
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
+	case MADV_SEQUENTIAL:
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
+		break;
+	case MADV_RANDOM:
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
+		break;
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags &= ~VM_DONTCOPY;
+		break;
+	case MADV_WIPEONFORK:
+		/* MADV_WIPEONFORK is only supported on anonymous memory. */
+		if (vma->vm_file || vma->vm_flags & VM_SHARED) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags |= VM_WIPEONFORK;
+		break;
+	case MADV_KEEPONFORK:
+		new_flags &= ~VM_WIPEONFORK;
+		break;
+	case MADV_DONTDUMP:
+		new_flags |= VM_DONTDUMP;
+		break;
+	case MADV_DODUMP:
+		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
+			error = -EINVAL;
+			goto out;
+		}
+		new_flags &= ~VM_DONTDUMP;
+		break;
+	case MADV_MERGEABLE:
+	case MADV_UNMERGEABLE:
+		error = ksm_madvise(vma, start, end, behavior, &new_flags);
+		if (error)
+			goto out;
+		break;
+	case MADV_HUGEPAGE:
+	case MADV_NOHUGEPAGE:
+		error = hugepage_madvise(vma, &new_flags, behavior);
+		if (error)
+			goto out;
+		break;
+	}
+
+	error = madvise_update_vma(vma, prev, start, end, new_flags);
+
+out:
+	if (error == -ENOMEM)
+		error = -EAGAIN;
+	return error;
+}
+
 #ifdef CONFIG_MEMORY_FAILURE
 /*
  * Error injection support for memory error handling.
@@ -978,30 +1004,6 @@ static int madvise_inject_error(int behavior,
 }
 #endif
 
-static long
-madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
-		unsigned long start, unsigned long end, int behavior)
-{
-	switch (behavior) {
-	case MADV_REMOVE:
-		return madvise_remove(vma, prev, start, end);
-	case MADV_WILLNEED:
-		return madvise_willneed(vma, prev, start, end);
-	case MADV_COLD:
-		return madvise_cold(vma, prev, start, end);
-	case MADV_PAGEOUT:
-		return madvise_pageout(vma, prev, start, end);
-	case MADV_FREE:
-	case MADV_DONTNEED:
-		return madvise_dontneed_free(vma, prev, start, end, behavior);
-	case MADV_POPULATE_READ:
-	case MADV_POPULATE_WRITE:
-		return madvise_populate(vma, prev, start, end, behavior);
-	default:
-		return madvise_behavior(vma, prev, start, end, behavior);
-	}
-}
-
 static bool
 madvise_behavior_valid(int behavior)
 {
@@ -1054,6 +1056,73 @@ process_madvise_behavior_valid(int behavior)
 	}
 }
 
+/*
+ * Walk the vmas in range [start,end), and call the visit function on each one.
+ * The visit function will get start and end parameters that cover the overlap
+ * between the current vma and the original range.  Any unmapped regions in the
+ * original range will result in this function returning -ENOMEM while still
+ * calling the visit function on all of the existing vmas in the range.
+ * Must be called with the mmap_lock held for reading or writing.
+ */
+static
+int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
+		      unsigned long end, unsigned long arg,
+		      int (*visit)(struct vm_area_struct *vma,
+				   struct vm_area_struct **prev, unsigned long start,
+				   unsigned long end, unsigned long arg))
+{
+	struct vm_area_struct *vma;
+	struct vm_area_struct *prev;
+	unsigned long tmp;
+	int unmapped_error = 0;
+
+	/*
+	 * If the interval [start,end) covers some unmapped address
+	 * ranges, just ignore them, but return -ENOMEM at the end.
+	 * - different from the way of handling in mlock etc.
+	 */
+	vma = find_vma_prev(mm, start, &prev);
+	if (vma && start > vma->vm_start)
+		prev = vma;
+
+	for (;;) {
+		int error;
+
+		/* Still start < end. */
+		if (!vma)
+			return -ENOMEM;
+
+		/* Here start < (end|vma->vm_end). */
+		if (start < vma->vm_start) {
+			unmapped_error = -ENOMEM;
+			start = vma->vm_start;
+			if (start >= end)
+				break;
+		}
+
+		/* Here vma->vm_start <= start < (end|vma->vm_end) */
+		tmp = vma->vm_end;
+		if (end < tmp)
+			tmp = end;
+
+		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
+		error = visit(vma, &prev, start, tmp, arg);
+		if (error)
+			return error;
+		start = tmp;
+		if (prev && start < prev->vm_end)
+			start = prev->vm_end;
+		if (start >= end)
+			break;
+		if (prev)
+			vma = prev->vm_next;
+		else	/* madvise_remove dropped mmap_lock */
+			vma = find_vma(mm, start);
+	}
+
+	return unmapped_error;
+}
+
 /*
  * The madvise(2) system call.
  *
@@ -1126,9 +1195,7 @@ process_madvise_behavior_valid(int behavior)
  */
 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
 {
-	unsigned long end, tmp;
-	struct vm_area_struct *vma, *prev;
-	int unmapped_error = 0;
+	unsigned long end;
 	int error = -EINVAL;
 	int write;
 	size_t len;
@@ -1168,51 +1235,9 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
 		mmap_read_lock(mm);
 	}
 
-	/*
-	 * If the interval [start,end) covers some unmapped address
-	 * ranges, just ignore them, but return -ENOMEM at the end.
-	 * - different from the way of handling in mlock etc.
-	 */
-	vma = find_vma_prev(mm, start, &prev);
-	if (vma && start > vma->vm_start)
-		prev = vma;
-
 	blk_start_plug(&plug);
-	for (;;) {
-		/* Still start < end. */
-		error = -ENOMEM;
-		if (!vma)
-			goto out;
-
-		/* Here start < (end|vma->vm_end). */
-		if (start < vma->vm_start) {
-			unmapped_error = -ENOMEM;
-			start = vma->vm_start;
-			if (start >= end)
-				goto out;
-		}
-
-		/* Here vma->vm_start <= start < (end|vma->vm_end) */
-		tmp = vma->vm_end;
-		if (end < tmp)
-			tmp = end;
-
-		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
-		error = madvise_vma(vma, &prev, start, tmp, behavior);
-		if (error)
-			goto out;
-		start = tmp;
-		if (prev && start < prev->vm_end)
-			start = prev->vm_end;
-		error = unmapped_error;
-		if (start >= end)
-			goto out;
-		if (prev)
-			vma = prev->vm_next;
-		else	/* madvise_remove dropped mmap_lock */
-			vma = find_vma(mm, start);
-	}
-out:
+	error = madvise_walk_vmas(mm, start, end, behavior,
+			madvise_vma_behavior);
 	blk_finish_plug(&plug);
 	if (write)
 		mmap_write_unlock(mm);
Signature · 1 line
2.33.0.259.gc128427fd7-goog
Signature · 1 line
Kees Cook
author reviewer
#3 lore raw

Re: [PATCH v8 1/3] mm: rearrange madvise code to allow for reuse

Quoted 58 lines · Kees Cook
Quoted 40 lines · Suren Baghdasaryan

From: Colin Cross <[email protected]>

Refactor the madvise syscall to allow for parts of it to be reused by a prctl syscall that affects vmas.

Move the code that walks vmas in a virtual address range into a function that takes a function pointer as a parameter. The only caller for now is sys_madvise, which uses it to call madvise_vma_behavior on each vma, but the next patch will add an additional caller.

Move handling all vma behaviors inside madvise_behavior, and rename it to madvise_vma_behavior.

Move the code that updates the flags on a vma, including splitting or merging the vma as necessary, into a new function called madvise_update_vma. The next patch will add support for updating a new anon_name field as well.

Cc Pekka Enberg <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Jan Glauber <[email protected]>
Cc John Stultz <[email protected]>
Cc Rob Landley <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc David Rientjes <[email protected]>
Cc Al Viro <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michel Lespinasse <[email protected]>
Cc Tang Chen <[email protected]>
Cc Robin Holt <[email protected]>
Cc Shaohua Li <[email protected]>
Quoted 8 lines
Cc Sasha Levin <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
[sumits: rebased over v5.9-rc3]
[surenb: rebased over v5.14-rc7]

Other folks have already reviewed this, and it does look okay to me, too, but I find it a bit hard to review. There are at least 3 things happening in this patch:
- moving to the walker
- merging two behavior routines
- extracting flag setting from behavior checking

Quoted 2 lines

It seems like those could be separate patches, but I'm probably overly picky. :)

Thank you for taking a look. I wanted to keep the patch as close to the original as possible, but if more people find it hard to review then I'll break it up into smaller pieces.
Thanks,
Suren.

Quoted 411 lines

-Kees

Quoted 403 lines
---
 mm/madvise.c | 319 +++++++++++++++++++++++++++------------------------
 1 file changed, 172 insertions(+), 147 deletions(-)
mm/madvise.c +172 -148
index 5c065bc8b5f6..359cd3fa612c 100644
@@ -63,76 +63,20 @@ static int madvise_need_mmap_write(int behavior)
 }
 
 /*
- * We can potentially split a vm area into separate
- * areas, each area with its own behavior.
+ * Update the vm_flags on regiion of a vma, splitting it or merging it as
+ * necessary.  Must be called with mmap_sem held for writing;
  */
-static long madvise_behavior(struct vm_area_struct *vma,
-                  struct vm_area_struct **prev,
-                  unsigned long start, unsigned long end, int behavior)
+static int madvise_update_vma(struct vm_area_struct *vma,
+                           struct vm_area_struct **prev, unsigned long start,
+                           unsigned long end, unsigned long new_flags)
 {
      struct mm_struct *mm = vma->vm_mm;
-     int error = 0;
+     int error;
      pgoff_t pgoff;
-     unsigned long new_flags = vma->vm_flags;
-
-     switch (behavior) {
-     case MADV_NORMAL:
-             new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
-             break;
-     case MADV_SEQUENTIAL:
-             new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
-             break;
-     case MADV_RANDOM:
-             new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
-             break;
-     case MADV_DONTFORK:
-             new_flags |= VM_DONTCOPY;
-             break;
-     case MADV_DOFORK:
-             if (vma->vm_flags & VM_IO) {
-                     error = -EINVAL;
-                     goto out;
-             }
-             new_flags &= ~VM_DONTCOPY;
-             break;
-     case MADV_WIPEONFORK:
-             /* MADV_WIPEONFORK is only supported on anonymous memory. */
-             if (vma->vm_file || vma->vm_flags & VM_SHARED) {
-                     error = -EINVAL;
-                     goto out;
-             }
-             new_flags |= VM_WIPEONFORK;
-             break;
-     case MADV_KEEPONFORK:
-             new_flags &= ~VM_WIPEONFORK;
-             break;
-     case MADV_DONTDUMP:
-             new_flags |= VM_DONTDUMP;
-             break;
-     case MADV_DODUMP:
-             if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
-                     error = -EINVAL;
-                     goto out;
-             }
-             new_flags &= ~VM_DONTDUMP;
-             break;
-     case MADV_MERGEABLE:
-     case MADV_UNMERGEABLE:
-             error = ksm_madvise(vma, start, end, behavior, &new_flags);
-             if (error)
-                     goto out_convert_errno;
-             break;
-     case MADV_HUGEPAGE:
-     case MADV_NOHUGEPAGE:
-             error = hugepage_madvise(vma, &new_flags, behavior);
-             if (error)
-                     goto out_convert_errno;
-             break;
-     }
 
      if (new_flags == vma->vm_flags) {
              *prev = vma;
-             goto out;
+             return 0;
      }
 
      pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
@@ -149,21 +93,21 @@ static long madvise_behavior(struct vm_area_struct *vma,
      if (start != vma->vm_start) {
              if (unlikely(mm->map_count >= sysctl_max_map_count)) {
                      error = -ENOMEM;
-                     goto out;
+                     return error;
              }
              error = __split_vma(mm, vma, start, 1);
              if (error)
-                     goto out_convert_errno;
+                     return error;
      }
 
      if (end != vma->vm_end) {
              if (unlikely(mm->map_count >= sysctl_max_map_count)) {
                      error = -ENOMEM;
-                     goto out;
+                     return error;
              }
              error = __split_vma(mm, vma, end, 0);
              if (error)
-                     goto out_convert_errno;
+                     return error;
      }
 
 success:
@@ -172,15 +116,7 @@ static long madvise_behavior(struct vm_area_struct *vma,
       */
      vma->vm_flags = new_flags;
 
-out_convert_errno:
-     /*
-      * madvise() returns EAGAIN if kernel resources, such as
-      * slab, are temporarily unavailable.
-      */
-     if (error == -ENOMEM)
-             error = -EAGAIN;
-out:
-     return error;
+     return 0;
 }
 
 #ifdef CONFIG_SWAP
@@ -930,6 +866,96 @@ static long madvise_remove(struct vm_area_struct *vma,
      return error;
 }
 
+/*
+ * Apply an madvise behavior to a region of a vma.  madvise_update_vma
+ * will handle splitting a vm area into separate areas, each area with its own
+ * behavior.
+ */
+static int madvise_vma_behavior(struct vm_area_struct *vma,
+                             struct vm_area_struct **prev,
+                             unsigned long start, unsigned long end,
+                             unsigned long behavior)
+{
+     int error = 0;
+     unsigned long new_flags = vma->vm_flags;
+
+     switch (behavior) {
+     case MADV_REMOVE:
+             return madvise_remove(vma, prev, start, end);
+     case MADV_WILLNEED:
+             return madvise_willneed(vma, prev, start, end);
+     case MADV_COLD:
+             return madvise_cold(vma, prev, start, end);
+     case MADV_PAGEOUT:
+             return madvise_pageout(vma, prev, start, end);
+     case MADV_FREE:
+     case MADV_DONTNEED:
+             return madvise_dontneed_free(vma, prev, start, end, behavior);
+     case MADV_POPULATE_READ:
+     case MADV_POPULATE_WRITE:
+             return madvise_populate(vma, prev, start, end, behavior);
+     case MADV_NORMAL:
+             new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+             break;
+     case MADV_SEQUENTIAL:
+             new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
+             break;
+     case MADV_RANDOM:
+             new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
+             break;
+     case MADV_DONTFORK:
+             new_flags |= VM_DONTCOPY;
+             break;
+     case MADV_DOFORK:
+             if (vma->vm_flags & VM_IO) {
+                     error = -EINVAL;
+                     goto out;
+             }
+             new_flags &= ~VM_DONTCOPY;
+             break;
+     case MADV_WIPEONFORK:
+             /* MADV_WIPEONFORK is only supported on anonymous memory. */
+             if (vma->vm_file || vma->vm_flags & VM_SHARED) {
+                     error = -EINVAL;
+                     goto out;
+             }
+             new_flags |= VM_WIPEONFORK;
+             break;
+     case MADV_KEEPONFORK:
+             new_flags &= ~VM_WIPEONFORK;
+             break;
+     case MADV_DONTDUMP:
+             new_flags |= VM_DONTDUMP;
+             break;
+     case MADV_DODUMP:
+             if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
+                     error = -EINVAL;
+                     goto out;
+             }
+             new_flags &= ~VM_DONTDUMP;
+             break;
+     case MADV_MERGEABLE:
+     case MADV_UNMERGEABLE:
+             error = ksm_madvise(vma, start, end, behavior, &new_flags);
+             if (error)
+                     goto out;
+             break;
+     case MADV_HUGEPAGE:
+     case MADV_NOHUGEPAGE:
+             error = hugepage_madvise(vma, &new_flags, behavior);
+             if (error)
+                     goto out;
+             break;
+     }
+
+     error = madvise_update_vma(vma, prev, start, end, new_flags);
+
+out:
+     if (error == -ENOMEM)
+             error = -EAGAIN;
+     return error;
+}
+
 #ifdef CONFIG_MEMORY_FAILURE
 /*
  * Error injection support for memory error handling.
@@ -978,30 +1004,6 @@ static int madvise_inject_error(int behavior,
 }
 #endif
 
-static long
-madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
-             unsigned long start, unsigned long end, int behavior)
-{
-     switch (behavior) {
-     case MADV_REMOVE:
-             return madvise_remove(vma, prev, start, end);
-     case MADV_WILLNEED:
-             return madvise_willneed(vma, prev, start, end);
-     case MADV_COLD:
-             return madvise_cold(vma, prev, start, end);
-     case MADV_PAGEOUT:
-             return madvise_pageout(vma, prev, start, end);
-     case MADV_FREE:
-     case MADV_DONTNEED:
-             return madvise_dontneed_free(vma, prev, start, end, behavior);
-     case MADV_POPULATE_READ:
-     case MADV_POPULATE_WRITE:
-             return madvise_populate(vma, prev, start, end, behavior);
-     default:
-             return madvise_behavior(vma, prev, start, end, behavior);
-     }
-}
-
 static bool
 madvise_behavior_valid(int behavior)
 {
@@ -1054,6 +1056,73 @@ process_madvise_behavior_valid(int behavior)
      }
 }
 
+/*
+ * Walk the vmas in range [start,end), and call the visit function on each one.
+ * The visit function will get start and end parameters that cover the overlap
+ * between the current vma and the original range.  Any unmapped regions in the
+ * original range will result in this function returning -ENOMEM while still
+ * calling the visit function on all of the existing vmas in the range.
+ * Must be called with the mmap_lock held for reading or writing.
+ */
+static
+int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
+                   unsigned long end, unsigned long arg,
+                   int (*visit)(struct vm_area_struct *vma,
+                                struct vm_area_struct **prev, unsigned long start,
+                                unsigned long end, unsigned long arg))
+{
+     struct vm_area_struct *vma;
+     struct vm_area_struct *prev;
+     unsigned long tmp;
+     int unmapped_error = 0;
+
+     /*
+      * If the interval [start,end) covers some unmapped address
+      * ranges, just ignore them, but return -ENOMEM at the end.
+      * - different from the way of handling in mlock etc.
+      */
+     vma = find_vma_prev(mm, start, &prev);
+     if (vma && start > vma->vm_start)
+             prev = vma;
+
+     for (;;) {
+             int error;
+
+             /* Still start < end. */
+             if (!vma)
+                     return -ENOMEM;
+
+             /* Here start < (end|vma->vm_end). */
+             if (start < vma->vm_start) {
+                     unmapped_error = -ENOMEM;
+                     start = vma->vm_start;
+                     if (start >= end)
+                             break;
+             }
+
+             /* Here vma->vm_start <= start < (end|vma->vm_end) */
+             tmp = vma->vm_end;
+             if (end < tmp)
+                     tmp = end;
+
+             /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
+             error = visit(vma, &prev, start, tmp, arg);
+             if (error)
+                     return error;
+             start = tmp;
+             if (prev && start < prev->vm_end)
+                     start = prev->vm_end;
+             if (start >= end)
+                     break;
+             if (prev)
+                     vma = prev->vm_next;
+             else    /* madvise_remove dropped mmap_lock */
+                     vma = find_vma(mm, start);
+     }
+
+     return unmapped_error;
+}
+
 /*
  * The madvise(2) system call.
  *
@@ -1126,9 +1195,7 @@ process_madvise_behavior_valid(int behavior)
  */
 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
 {
-     unsigned long end, tmp;
-     struct vm_area_struct *vma, *prev;
-     int unmapped_error = 0;
+     unsigned long end;
      int error = -EINVAL;
      int write;
      size_t len;
@@ -1168,51 +1235,9 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
              mmap_read_lock(mm);
      }
 
-     /*
-      * If the interval [start,end) covers some unmapped address
-      * ranges, just ignore them, but return -ENOMEM at the end.
-      * - different from the way of handling in mlock etc.
-      */
-     vma = find_vma_prev(mm, start, &prev);
-     if (vma && start > vma->vm_start)
-             prev = vma;
-
      blk_start_plug(&plug);
-     for (;;) {
-             /* Still start < end. */
-             error = -ENOMEM;
-             if (!vma)
-                     goto out;
-
-             /* Here start < (end|vma->vm_end). */
-             if (start < vma->vm_start) {
-                     unmapped_error = -ENOMEM;
-                     start = vma->vm_start;
-                     if (start >= end)
-                             goto out;
-             }
-
-             /* Here vma->vm_start <= start < (end|vma->vm_end) */
-             tmp = vma->vm_end;
-             if (end < tmp)
-                     tmp = end;
-
-             /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
-             error = madvise_vma(vma, &prev, start, tmp, behavior);
-             if (error)
-                     goto out;
-             start = tmp;
-             if (prev && start < prev->vm_end)
-                     start = prev->vm_end;
-             error = unmapped_error;
-             if (start >= end)
-                     goto out;
-             if (prev)
-                     vma = prev->vm_next;
-             else    /* madvise_remove dropped mmap_lock */
-                     vma = find_vma(mm, start);
-     }
-out:
+     error = madvise_walk_vmas(mm, start, end, behavior,
+                     madvise_vma_behavior);
      blk_finish_plug(&plug);
      if (write)
              mmap_write_unlock(mm);
--
Quoted 2 lines

2.33.0.259.gc128427fd7-goog

--
Kees Cook

#4 lore raw

Re: [PATCH v8 1/3] mm: rearrange madvise code to allow for reuse

In reply to Suren Baghdasaryan

On Fri, Aug 27, 2021 at 12:18:56PM -0700, Suren Baghdasaryan wrote:
...

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 879

Quoted 12 lines
mm/madvise.c +11 -0
+/*
+ * Apply an madvise behavior to a region of a vma.  madvise_update_vma
+ * will handle splitting a vm area into separate areas, each area with its own
+ * behavior.
+ */
+static int madvise_vma_behavior(struct vm_area_struct *vma,
+				struct vm_area_struct **prev,
+				unsigned long start, unsigned long end,
+				unsigned long behavior)
+{
+	int error = 0;

Hi Suren! A nitpick -- this variable is never used with default value so I think we could drop assignment here.
...

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 911

Quoted 6 lines
mm/madvise.c +6 -0
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;

We can exit early here, without jumping to the end of the function, right?

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 919

Quoted 8 lines
mm/madvise.c +8 -0
+			goto out;
+		}
+		new_flags &= ~VM_DONTCOPY;
+		break;
+	case MADV_WIPEONFORK:
+		/* MADV_WIPEONFORK is only supported on anonymous memory. */
+		if (vma->vm_file || vma->vm_flags & VM_SHARED) {
+			error = -EINVAL;

And here too.

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 932

Quoted 13 lines
mm/madvise.c +13 -0
+			goto out;
+		}
+		new_flags |= VM_WIPEONFORK;
+		break;
+	case MADV_KEEPONFORK:
+		new_flags &= ~VM_WIPEONFORK;
+		break;
+	case MADV_DONTDUMP:
+		new_flags |= VM_DONTDUMP;
+		break;
+	case MADV_DODUMP:
+		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
+			error = -EINVAL;

Same.

Quoted 11 lines
mm/madvise.c +11 -0
+			goto out;
+		}
+		new_flags &= ~VM_DONTDUMP;
+		break;
+	case MADV_MERGEABLE:
+	case MADV_UNMERGEABLE:
+		error = ksm_madvise(vma, start, end, behavior, &new_flags);
+		if (error)
+			goto out;
+		break;
+	case MADV_HUGEPAGE:

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 953

Quoted 10 lines
mm/madvise.c +10 -0
+	case MADV_NOHUGEPAGE:
+		error = hugepage_madvise(vma, &new_flags, behavior);
+		if (error)
+			goto out;
+		break;
+	}
+
+	error = madvise_update_vma(vma, prev, start, end, new_flags);
+
+out:

I suppose we better keep the former comment on why we maps ENOMEM to EAGAIN?

Cyrill

author reviewer
#5 lore raw

Re: [PATCH v8 1/3] mm: rearrange madvise code to allow for reuse

Quoted 71 lines · Cyrill Gorcunov

On Fri, Aug 27, 2021 at 12:18:56PM -0700, Suren Baghdasaryan wrote:
...

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 879

Quoted 12 lines
mm/madvise.c +11 -0
+/*
+ * Apply an madvise behavior to a region of a vma.  madvise_update_vma
+ * will handle splitting a vm area into separate areas, each area with its own
+ * behavior.
+ */
+static int madvise_vma_behavior(struct vm_area_struct *vma,
+                             struct vm_area_struct **prev,
+                             unsigned long start, unsigned long end,
+                             unsigned long behavior)
+{
+     int error = 0;

Hi Suren! A nitpick -- this variable is never used with default value so I think we could drop assignment here.
...

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 911

Quoted 6 lines
mm/madvise.c +6 -0
+     case MADV_DONTFORK:
+             new_flags |= VM_DONTCOPY;
+             break;
+     case MADV_DOFORK:
+             if (vma->vm_flags & VM_IO) {
+                     error = -EINVAL;

We can exit early here, without jumping to the end of the function, right?

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 919

Quoted 8 lines
mm/madvise.c +8 -0
+                     goto out;
+             }
+             new_flags &= ~VM_DONTCOPY;
+             break;
+     case MADV_WIPEONFORK:
+             /* MADV_WIPEONFORK is only supported on anonymous memory. */
+             if (vma->vm_file || vma->vm_flags & VM_SHARED) {
+                     error = -EINVAL;

And here too.

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 932

Quoted 13 lines
mm/madvise.c +13 -0
+                     goto out;
+             }
+             new_flags |= VM_WIPEONFORK;
+             break;
+     case MADV_KEEPONFORK:
+             new_flags &= ~VM_WIPEONFORK;
+             break;
+     case MADV_DONTDUMP:
+             new_flags |= VM_DONTDUMP;
+             break;
+     case MADV_DODUMP:
+             if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
+                     error = -EINVAL;

Same.

Quoted 15 lines
mm/madvise.c +15 -0
+                     goto out;
+             }
+             new_flags &= ~VM_DONTDUMP;
+             break;
+     case MADV_MERGEABLE:
+     case MADV_UNMERGEABLE:
+             error = ksm_madvise(vma, start, end, behavior, &new_flags);
+             if (error)
+                     goto out;
+             break;
+     case MADV_HUGEPAGE:
+     case MADV_NOHUGEPAGE:
+             error = hugepage_madvise(vma, &new_flags, behavior);
+             if (error)
+                     goto out;
Quoted 8 lines

mm/madvise.c static long madvise_remove(struct vm_area_struct *vma, · line 953

Quoted 6 lines
mm/madvise.c +6 -0
+             break;
+     }
+
+     error = madvise_update_vma(vma, prev, start, end, new_flags);
+
+out:

I suppose we better keep the former comment on why we maps ENOMEM to EAGAIN?

Thanks for the review Cyrill! Proposed changes sound good to me. Will change in the next revision.
Suren.

Quoted 2 lines

Cyrill

author reviewer
#6 lore raw

PATCH v8 2/3 mm: add a field to store names for private anonymous memory

In reply to Suren Baghdasaryan

From: Colin Cross <[email protected]>

In many userspace applications, and especially in VM based applications like Android uses heavily, there are multiple different allocators in use.
At a minimum there is libc malloc and the stack, and in many cases there
are libc malloc, the stack, direct syscalls to mmap anonymous memory, and multiple VM heaps (one for small objects, one for big objects, etc.). Each of these layers usually has its own tools to inspect its usage; malloc by compiling a debug version, the VM through heap inspection tools, and for direct syscalls there is usually no way to track them.

On Android we heavily use a set of tools that use an extended version of the logic covered in Documentation/vm/pagemap.txt to walk all pages mapped in userspace and slice their usage by process, shared (COW) vs. unique mappings, backing, etc. This can account for real physical memory usage even in cases like fork without exec (which Android uses heavily to share as many private COW pages as possible between processes), Kernel SamePage Merging, and clean zero pages. It produces a measurement of the pages that only exist in that process (USS, for unique), and a measurement of the physical memory usage of that process with the cost of shared pages being evenly split between processes that share them (PSS).

If all anonymous memory is indistinguishable then figuring out the real physical memory usage (PSS) of each heap requires either a pagemap walking tool that can understand the heap debugging of every layer, or for every layer's heap debugging tools to implement the pagemap walking logic, in which case it is hard to get a consistent view of memory across the whole system.

Tracking the information in userspace leads to all sorts of problems. It either needs to be stored inside the process, which means every process has to have an API to export its current heap information upon request, or it has to be stored externally in a filesystem that somebody needs to clean up on crashes. It needs to be readable while the process is still running, so it has to have some sort of synchronization with every layer of userspace. Efficiently tracking the ranges requires reimplementing something like the kernel vma trees, and linking to it from every layer of userspace. It requires more memory, more syscalls, more runtime cost, and more complexity to separately track regions that the kernel is already tracking.

This patch adds a field to /proc/pid/maps and /proc/pid/smaps to show a userspace-provided name for anonymous vmas. The names of named anonymous vmas are shown in /proc/pid/maps and /proc/pid/smaps as [anon:<name>].

Userspace can set the name for a region of memory by calling prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, (unsigned long)name); Setting the name to NULL clears it. The name length limit is 64 bytes including NUL-terminator (to have some reasonable limit and because the longest name used in Android has 50 chars) and is checked to contain only printable characters.

The name is stored in a pointer in the shared union in vm_area_struct that points to a null terminated string. Anonymous vmas with the same name (equivalent strings) and are otherwise mergeable will be merged. The name pointers are not shared between vmas even if they contain the same name. The name pointer is stored in a union with fields that are only used on file-backed mappings, so it does not increase memory usage.

The patch is based on the original patch developed by Colin Cross, more specifically on its latest version [1] posted upstream by Sumit Semwal. It used a userspace pointer to store vma names. In that design, name pointers could be shared between vmas. However during the last upstreaming attempt, Kees Cook raised concerns [2] about this approach and suggested to copy the name into kernel memory space, perform validity checks [3] and store as a string referenced from vm_area_struct.
One big concern is about fork() performance which would need to strdup anonymous vma names. Dave Hansen suggested experimenting with worst-case scenario of forking a process with 64k vmas having longest possible names [4]. I ran this experiment on an ARM64 Android device and recorded a worst-case regression of almost 40% when forking such a process. This regression is addressed in the followup patch which replaces the pointer to a name with a refcounted structure that allows sharing the name pointer between vmas of the same name. Instead of duplicating the string during fork() or when splitting a vma it increments the refcount.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/202009031031.D32EF57ED@keescook/
[3] https://lore.kernel.org/linux-mm/202009031022.3834F692@keescook/
[4] https://lore.kernel.org/linux-mm/[email protected]/

[surenb: rebased over v5.14-rc7, replaced userpointer with a kernel copy and added input sanitization. The bulk of the work here was done by Colin Cross, therefore, with his permission, keeping him as the author]

---
 Documentation/filesystems/proc.rst |   2 +
 fs/proc/task_mmu.c                 |  14 +++-
 fs/userfaultfd.c                   |   7 +-
 include/linux/mm.h                 |  13 +++-
 include/linux/mm_types.h           |  48 +++++++++++--
 include/uapi/linux/prctl.h         |   3 +
 kernel/fork.c                      |   2 +
 kernel/sys.c                       |  48 +++++++++++++
 mm/madvise.c                       | 112 +++++++++++++++++++++++++++--
 mm/mempolicy.c                     |   3 +-
 mm/mlock.c                         |   2 +-
 mm/mmap.c                          |  38 +++++-----
 mm/mprotect.c                      |   2 +-
 13 files changed, 261 insertions(+), 33 deletions(-)
Documentation/filesystems/proc.rst +2 -0
index 042c418f4090..a067eec54ef1 100644
@@ -431,6 +431,8 @@ is not associated with a file:
  [stack]                    the stack of the main process
  [vdso]                     the "virtual dynamic shared object",
                             the kernel system call handler
+[anon:<name>]               an anonymous mapping that has been
+                            named by userspace
  =======                    ====================================
 
  or if empty, the mapping is anonymous.
fs/proc/task_mmu.c +13 -1
index eb97468dfe4c..2ce5b3c4e7fc 100644
@@ -308,6 +308,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 
 	name = arch_vma_name(vma);
 	if (!name) {
+		const char *anon_name;
+
 		if (!mm) {
 			name = "[vdso]";
 			goto done;
@@ -319,8 +321,18 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 			goto done;
 		}
 
-		if (is_stack(vma))
+		if (is_stack(vma)) {
 			name = "[stack]";
+			goto done;
+		}
+
+		anon_name = vma_anon_name(vma);
+		if (anon_name) {
+			seq_pad(m, ' ');
+			seq_puts(m, "[anon:");
+			seq_write(m, anon_name, strlen(anon_name));
+			seq_putc(m, ']');
+		}
 	}
 
 done:
fs/userfaultfd.c +4 -3
index 5c2d806e6ae5..5057843fb71a 100644
@@ -876,7 +876,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
 				 new_flags, vma->anon_vma,
 				 vma->vm_file, vma->vm_pgoff,
 				 vma_policy(vma),
-				 NULL_VM_UFFD_CTX);
+				 NULL_VM_UFFD_CTX, vma_anon_name(vma));
 		if (prev)
 			vma = prev;
 		else
@@ -1440,7 +1440,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
 				 vma_policy(vma),
-				 ((struct vm_userfaultfd_ctx){ ctx }));
+				 ((struct vm_userfaultfd_ctx){ ctx }),
+				 vma_anon_name(vma));
 		if (prev) {
 			vma = prev;
 			goto next;
@@ -1617,7 +1618,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 		prev = vma_merge(mm, prev, start, vma_end, new_flags,
 				 vma->anon_vma, vma->vm_file, vma->vm_pgoff,
 				 vma_policy(vma),
-				 NULL_VM_UFFD_CTX);
+				 NULL_VM_UFFD_CTX, vma_anon_name(vma));
 		if (prev) {
 			vma = prev;
 			goto next;
include/linux/mm.h +12 -1
index 7ca22e6e694a..45c003fae7fe 100644
@@ -2548,7 +2548,7 @@ static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
 extern struct vm_area_struct *vma_merge(struct mm_struct *,
 	struct vm_area_struct *prev, unsigned long addr, unsigned long end,
 	unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
-	struct mempolicy *, struct vm_userfaultfd_ctx);
+	struct mempolicy *, struct vm_userfaultfd_ctx, const char *);
 extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
 extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
 	unsigned long addr, int new_below);
@@ -3283,5 +3283,16 @@ static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
 	return 0;
 }
 
+#ifdef CONFIG_ADVISE_SYSCALLS
+int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+			  unsigned long len_in, const char *name);
+#else
+static inline int
+madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+		      unsigned long len_in, const char *name) {
+	return 0;
+}
+#endif
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_MM_H */
include/linux/mm_types.h +44 -4

59 lines not on this page · raw message

include/uapi/linux/prctl.h +3 -0
index 967d9c55323d..968582cd91b5 100644
@@ -267,4 +267,7 @@ struct prctl_mm_map {
 # define PR_SCHED_CORE_SHARE_FROM	3 /* pull core_sched cookie to pid */
 # define PR_SCHED_CORE_MAX		4
 
+#define PR_SET_VMA		0x53564d41
+# define PR_SET_VMA_ANON_NAME		0
+
 #endif /* _LINUX_PRCTL_H */
kernel/fork.c +2 -0
index 44f4c2d83763..e086f56a4628 100644
@@ -366,12 +366,14 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
 		*new = data_race(*orig);
 		INIT_LIST_HEAD(&new->anon_vma_chain);
 		new->vm_next = new->vm_prev = NULL;
+		dup_vma_anon_name(orig, new);
 	}
 	return new;
 }
 
 void vm_area_free(struct vm_area_struct *vma)
 {
+	free_vma_anon_name(vma);
 	kmem_cache_free(vm_area_cachep, vma);
 }
 
kernel/sys.c +48 -0

60 lines not on this page · raw message

mm/madvise.c +107 -5

162 lines not on this page · raw message

mm/mempolicy.c +2 -1
index e32360e90274..cc21ca7e9d40 100644
@@ -811,7 +811,8 @@ static int mbind_range(struct mm_struct *mm, unsigned long start,
 			((vmstart - vma->vm_start) >> PAGE_SHIFT);
 		prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
 				 vma->anon_vma, vma->vm_file, pgoff,
-				 new_pol, vma->vm_userfaultfd_ctx);
+				 new_pol, vma->vm_userfaultfd_ctx,
+				 vma_anon_name(vma));
 		if (prev) {
 			vma = prev;
 			next = vma->vm_next;
mm/mlock.c +1 -1
index 16d2ee160d43..c878515680af 100644
@@ -511,7 +511,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
 	*prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
 			  vma->vm_file, pgoff, vma_policy(vma),
-			  vma->vm_userfaultfd_ctx);
+			  vma->vm_userfaultfd_ctx, vma_anon_name(vma));
 	if (*prev) {
 		vma = *prev;
 		goto success;
mm/mmap.c +22 -16
index ca54d36d203a..baf00fbb1f4c 100644
@@ -1032,7 +1032,8 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  */
 static inline int is_mergeable_vma(struct vm_area_struct *vma,
 				struct file *file, unsigned long vm_flags,
-				struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
+				struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
+				const char *anon_name)
 {
 	/*
 	 * VM_SOFTDIRTY should not prevent from VMA merging, if we
@@ -1050,6 +1051,8 @@ static inline int is_mergeable_vma(struct vm_area_struct *vma,
 		return 0;
 	if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
 		return 0;
+	if (!is_same_vma_anon_name(vma, anon_name))
+		return 0;
 	return 1;
 }
 
@@ -1082,9 +1085,10 @@ static int
 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
 		     struct anon_vma *anon_vma, struct file *file,
 		     pgoff_t vm_pgoff,
-		     struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
+		     struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
+		     const char *anon_name)
 {
-	if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
+	if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) &&
 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
 		if (vma->vm_pgoff == vm_pgoff)
 			return 1;
@@ -1103,9 +1107,10 @@ static int
 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
 		    struct anon_vma *anon_vma, struct file *file,
 		    pgoff_t vm_pgoff,
-		    struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
+		    struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
+		     const char *anon_name)
 {
-	if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
+	if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) &&
 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
 		pgoff_t vm_pglen;
 		vm_pglen = vma_pages(vma);
@@ -1116,9 +1121,9 @@ can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
 }
 
 /*
- * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
- * whether that can be merged with its predecessor or its successor.
- * Or both (it neatly fills a hole).
+ * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
+ * figure out whether that can be merged with its predecessor or its
+ * successor.  Or both (it neatly fills a hole).
  *
  * In most cases - when called for mmap, brk or mremap - [addr,end) is
  * certain not to be mapped by the time vma_merge is called; but when
@@ -1163,7 +1168,8 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 			unsigned long end, unsigned long vm_flags,
 			struct anon_vma *anon_vma, struct file *file,
 			pgoff_t pgoff, struct mempolicy *policy,
-			struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
+			struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
+			const char *anon_name)
 {
 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
 	struct vm_area_struct *area, *next;
@@ -1193,7 +1199,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 			mpol_equal(vma_policy(prev), policy) &&
 			can_vma_merge_after(prev, vm_flags,
 					    anon_vma, file, pgoff,
-					    vm_userfaultfd_ctx)) {
+					    vm_userfaultfd_ctx, anon_name)) {
 		/*
 		 * OK, it can.  Can we now merge in the successor as well?
 		 */
@@ -1202,7 +1208,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 				can_vma_merge_before(next, vm_flags,
 						     anon_vma, file,
 						     pgoff+pglen,
-						     vm_userfaultfd_ctx) &&
+						     vm_userfaultfd_ctx, anon_name) &&
 				is_mergeable_anon_vma(prev->anon_vma,
 						      next->anon_vma, NULL)) {
 							/* cases 1, 6 */
@@ -1225,7 +1231,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 			mpol_equal(policy, vma_policy(next)) &&
 			can_vma_merge_before(next, vm_flags,
 					     anon_vma, file, pgoff+pglen,
-					     vm_userfaultfd_ctx)) {
+					     vm_userfaultfd_ctx, anon_name)) {
 		if (prev && addr < prev->vm_end)	/* case 4 */
 			err = __vma_adjust(prev, prev->vm_start,
 					 addr, prev->vm_pgoff, NULL, next);
@@ -1766,7 +1772,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 	 * Can we just expand an old mapping?
 	 */
 	vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
-			NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX);
+			NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
 	if (vma)
 		goto out;
 
@@ -1825,7 +1831,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		 */
 		if (unlikely(vm_flags != vma->vm_flags && prev)) {
 			merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags,
-				NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX);
+				NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
 			if (merge) {
 				/* ->mmap() can change vma->vm_file and fput the original file. So
 				 * fput the vma->vm_file here or we would add an extra fput for file
@@ -3087,7 +3093,7 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long fla
 
 	/* Can we just expand an old private anonymous mapping? */
 	vma = vma_merge(mm, prev, addr, addr + len, flags,
-			NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX);
+			NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
 	if (vma)
 		goto out;
 
@@ -3280,7 +3286,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 		return NULL;	/* should never get here */
 	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
 			    vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
-			    vma->vm_userfaultfd_ctx);
+			    vma->vm_userfaultfd_ctx, vma_anon_name(vma));
 	if (new_vma) {
 		/*
 		 * Source vma may have been merged into new_vma
mm/mprotect.c +1 -1
index 883e2cc85cad..a48ff8e79f48 100644
@@ -464,7 +464,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
 			   vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
-			   vma->vm_userfaultfd_ctx);
+			   vma->vm_userfaultfd_ctx, vma_anon_name(vma));
 	if (*pprev) {
 		vma = *pprev;
 		VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
Signature · 1 line
2.33.0.259.gc128427fd7-goog
#7 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 7 lines · Suren Baghdasaryan
fs/proc/task_mmu.c +7 -0
+		anon_name = vma_anon_name(vma);
+		if (anon_name) {
+			seq_pad(m, ' ');
+			seq_puts(m, "[anon:");
+			seq_write(m, anon_name, strlen(anon_name));
+			seq_putc(m, ']');
+		}

...

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2323

Quoted 11 lines
kernel/sys.c +11 -0
+	case PR_SET_VMA_ANON_NAME:
+		name = strndup_user((const char __user *)arg,
+				    ANON_VMA_NAME_MAX_LEN);
+
+		if (IS_ERR(name))
+			return PTR_ERR(name);
+
+		for (pch = name; *pch != '\0'; pch++) {
+			if (!isprint(*pch)) {
+				kfree(name);
+				return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

#8 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 8 lines · Matthew Wilcox

fs/proc/task_mmu.c show_map_vma(struct seq_file *m, struct vm_area_struct *vma) · line 335

Quoted 7 lines · Suren Baghdasaryan
fs/proc/task_mmu.c +7 -0
+		anon_name = vma_anon_name(vma);
+		if (anon_name) {
+			seq_pad(m, ' ');
+			seq_puts(m, "[anon:");
+			seq_write(m, anon_name, strlen(anon_name));
+			seq_putc(m, ']');
+		}

Maybe after seq_pad, use: seq_printf(m, "[anon:%s]", anon_name);

Quoted 13 lines

...

Quoted 10 lines
kernel/sys.c +10 -0
+	case PR_SET_VMA_ANON_NAME:
+		name = strndup_user((const char __user *)arg,
+				    ANON_VMA_NAME_MAX_LEN);
+
+		if (IS_ERR(name))
+			return PTR_ERR(name);
+
+		for (pch = name; *pch != '\0'; pch++) {
+			if (!isprint(*pch)) {
+				kfree(name);
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+				return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

For example, just escape it here instead of refusing to take it. Something like:

	name = strndup_user((const char __user *)arg,
			    ANON_VMA_NAME_MAX_LEN);
	escaped = kasprintf(GFP_KERNEL, "%pE", name);
	if (escaped) {
		kfree(name);
		return -ENOMEM;
	}
	kfree(name);
	name = escaped;
Signature · 1 line
Kees Cook
author reviewer
#9 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 11 lines · Kees Cook
Quoted 8 lines · Matthew Wilcox

fs/proc/task_mmu.c show_map_vma(struct seq_file *m, struct vm_area_struct *vma) · line 335

Quoted 7 lines · Suren Baghdasaryan
fs/proc/task_mmu.c +7 -0
+           anon_name = vma_anon_name(vma);
+           if (anon_name) {
+                   seq_pad(m, ' ');
+                   seq_puts(m, "[anon:");
+                   seq_write(m, anon_name, strlen(anon_name));
+                   seq_putc(m, ']');
+           }
Quoted 1 line

Maybe after seq_pad, use: seq_printf(m, "[anon:%s]", anon_name);

Good idea. Will change.

Quoted 23 lines
Quoted 14 lines

...

Quoted 10 lines
kernel/sys.c +10 -0
+   case PR_SET_VMA_ANON_NAME:
+           name = strndup_user((const char __user *)arg,
+                               ANON_VMA_NAME_MAX_LEN);
+
+           if (IS_ERR(name))
+                   return PTR_ERR(name);
+
+           for (pch = name; *pch != '\0'; pch++) {
+                   if (!isprint(*pch)) {
+                           kfree(name);
Quoted 8 lines
Quoted 1 line

+ return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

Quoted 3 lines

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

I see no issue in forbidding '[' and ']' but whitespace and ':' are currently used by Android. Would forbidding or escaping '[' and ']' be enough?

Quoted 7 lines

For example, just escape it here instead of refusing to take it. Something like:

        name = strndup_user((const char __user *)arg,
                            ANON_VMA_NAME_MAX_LEN);
        escaped = kasprintf(GFP_KERNEL, "%pE", name);

Did you mean "%*pE" as in
https://www.kernel.org/doc/html/latest/core-api/printk-formats.html#raw-buffer-as-an-escaped-string
?

Quoted 9 lines
        if (escaped) {
                kfree(name);
                return -ENOMEM;
        }
        kfree(name);
        name = escaped;

--
Kees Cook

#10 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 16 lines · Suren Baghdasaryan
Quoted 15 lines · Kees Cook
Quoted 15 lines

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2323

Quoted 12 lines
kernel/sys.c +11 -0
+   case PR_SET_VMA_ANON_NAME:
+           name = strndup_user((const char __user *)arg,
+                               ANON_VMA_NAME_MAX_LEN);
+
+           if (IS_ERR(name))
+                   return PTR_ERR(name);
+
+           for (pch = name; *pch != '\0'; pch++) {
+                   if (!isprint(*pch)) {
+                           kfree(name);
+                           return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps

Indeed. There's also the issue that the kernel's ctype actually implements some almost-but-not-quite latin1, so (some) chars above 0x7f would also pass isprint() - while everybody today expects utf-8, so the ability to put almost arbitrary sequences of chars with the high bit set could certainly confuse some parsers. IOW, don't use isprint() at all, just explicitly check for the byte values that we and up agreeing to allow/forbid.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

Quoted 7 lines
Quoted 3 lines

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

I see no issue in forbidding '[' and ']' but whitespace and ':' are currently used by Android. Would forbidding or escaping '[' and ']' be enough?

how about allowing [0x20, 0x7e] except [0x5b, 0x5d], i.e. all printable (including space) ascii characters, except [ \ ] - the brackets as already discussed, and backslash because then there's nobody who can get confused about whether there's some (and then which?) escaping mechanism in play - "\n" is simply never going to appear. Simple rules, easy to implement, easy to explain in a man page.

Quoted 7 lines
Quoted 7 lines

For example, just escape it here instead of refusing to take it. Something like:

        name = strndup_user((const char __user *)arg,
                            ANON_VMA_NAME_MAX_LEN);
        escaped = kasprintf(GFP_KERNEL, "%pE", name);

I would not go down that road. First, it makes it much harder to explain the rules for what are allowed and not allowed. Second, parsers become much more complicated. Third, does the length limit then apply to the escaped or unescaped string?

Rasmus

author reviewer
#11 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 40 lines · Rasmus Villemoes
Quoted 16 lines · Suren Baghdasaryan
Quoted 15 lines · Kees Cook
Quoted 15 lines

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2323

Quoted 12 lines
kernel/sys.c +11 -0
+   case PR_SET_VMA_ANON_NAME:
+           name = strndup_user((const char __user *)arg,
+                               ANON_VMA_NAME_MAX_LEN);
+
+           if (IS_ERR(name))
+                   return PTR_ERR(name);
+
+           for (pch = name; *pch != '\0'; pch++) {
+                   if (!isprint(*pch)) {
+                           kfree(name);
+                           return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps

Indeed. There's also the issue that the kernel's ctype actually implements some almost-but-not-quite latin1, so (some) chars above 0x7f would also pass isprint() - while everybody today expects utf-8, so the ability to put almost arbitrary sequences of chars with the high bit set could certainly confuse some parsers. IOW, don't use isprint() at all, just explicitly check for the byte values that we and up agreeing to allow/forbid.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

Quoted 7 lines
Quoted 3 lines

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

I see no issue in forbidding '[' and ']' but whitespace and ':' are currently used by Android. Would forbidding or escaping '[' and ']' be enough?

Quoted 6 lines

how about allowing [0x20, 0x7e] except [0x5b, 0x5d], i.e. all printable (including space) ascii characters, except [ \ ] - the brackets as already discussed, and backslash because then there's nobody who can get confused about whether there's some (and then which?) escaping mechanism in play - "\n" is simply never going to appear. Simple rules, easy to implement, easy to explain in a man page.

Thanks for the suggestion, Rasmus. I'm all for keeping it simple. Kees, Matthew, would that be acceptable?

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines

For example, just escape it here instead of refusing to take it. Something like:

        name = strndup_user((const char __user *)arg,
                            ANON_VMA_NAME_MAX_LEN);
        escaped = kasprintf(GFP_KERNEL, "%pE", name);

I would not go down that road. First, it makes it much harder to explain the rules for what are allowed and not allowed. Second, parsers become much more complicated. Third, does the length limit then apply to the escaped or unescaped string?

Rasmus

#12 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 49 lines · Suren Baghdasaryan
Quoted 40 lines · Rasmus Villemoes
Quoted 16 lines · Suren Baghdasaryan
Quoted 15 lines · Kees Cook
Quoted 15 lines

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2323

Quoted 12 lines
kernel/sys.c +11 -0
+   case PR_SET_VMA_ANON_NAME:
+           name = strndup_user((const char __user *)arg,
+                               ANON_VMA_NAME_MAX_LEN);
+
+           if (IS_ERR(name))
+                   return PTR_ERR(name);
+
+           for (pch = name; *pch != '\0'; pch++) {
+                   if (!isprint(*pch)) {
+                           kfree(name);
+                           return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps

Indeed. There's also the issue that the kernel's ctype actually implements some almost-but-not-quite latin1, so (some) chars above 0x7f would also pass isprint() - while everybody today expects utf-8, so the ability to put almost arbitrary sequences of chars with the high bit set could certainly confuse some parsers. IOW, don't use isprint() at all, just explicitly check for the byte values that we and up agreeing to allow/forbid.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

Quoted 7 lines
Quoted 3 lines

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

I see no issue in forbidding '[' and ']' but whitespace and ':' are currently used by Android. Would forbidding or escaping '[' and ']' be enough?

Quoted 6 lines

how about allowing [0x20, 0x7e] except [0x5b, 0x5d], i.e. all printable (including space) ascii characters, except [ \ ] - the brackets as already discussed, and backslash because then there's nobody who can get confused about whether there's some (and then which?) escaping mechanism in play - "\n" is simply never going to appear. Simple rules, easy to implement, easy to explain in a man page.

Quoted 2 lines

Thanks for the suggestion, Rasmus. I'm all for keeping it simple. Kees, Matthew, would that be acceptable?

Yes, I think so. It permits all kinds of characters that might be confusing if passed on to something else, but we can't prohibit everything, and forbidding just these three should remove any confusion for any parser of /proc. Little Bobby Tables thanks you.

author reviewer
#13 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 51 lines · Matthew Wilcox
Quoted 49 lines · Suren Baghdasaryan
Quoted 46 lines · Rasmus Villemoes
Quoted 16 lines · Suren Baghdasaryan
Quoted 15 lines · Kees Cook
Quoted 15 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+   case PR_SET_VMA_ANON_NAME:
+           name = strndup_user((const char __user *)arg,
+                               ANON_VMA_NAME_MAX_LEN);
+
+           if (IS_ERR(name))
+                   return PTR_ERR(name);
+
+           for (pch = name; *pch != '\0'; pch++) {
+                   if (!isprint(*pch)) {
+                           kfree(name);
+                           return -EINVAL;

I think isprint() is too weak a check. For example, I would suggest forbidding the following characters: ':', ']', '[', ' '. Perhaps

Indeed. There's also the issue that the kernel's ctype actually implements some almost-but-not-quite latin1, so (some) chars above 0x7f would also pass isprint() - while everybody today expects utf-8, so the ability to put almost arbitrary sequences of chars with the high bit set could certainly confuse some parsers. IOW, don't use isprint() at all, just explicitly check for the byte values that we and up agreeing to allow/forbid.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

isalnum() would be better? (permit a-zA-Z0-9) I wouldn't necessarily be opposed to some punctuation characters, but let's avoid creating confusion. Do you happen to know which characters are actually in use today?

Quoted 7 lines
Quoted 3 lines

There's some sense in refusing [, ], and :, but removing " " seems unhelpful for reasonable descriptors. As long as weird stuff is escaped, I think it's fine. Any parser can just extract with m|\[anon:(.*)\]$|

I see no issue in forbidding '[' and ']' but whitespace and ':' are currently used by Android. Would forbidding or escaping '[' and ']' be enough?

how about allowing [0x20, 0x7e] except [0x5b, 0x5d], i.e. all printable (including space) ascii characters, except [ \ ] - the brackets as already discussed, and backslash because then there's nobody who can get confused about whether there's some (and then which?) escaping mechanism in play - "\n" is simply never going to appear. Simple rules, easy to implement, easy to explain in a man page.

Quoted 7 lines
Quoted 2 lines

Thanks for the suggestion, Rasmus. I'm all for keeping it simple. Kees, Matthew, would that be acceptable?

Yes, I think so. It permits all kinds of characters that might be confusing if passed on to something else, but we can't prohibit everything, and forbidding just these three should remove any confusion for any parser of /proc. Little Bobby Tables thanks you.

Thanks for all the feedback! I think I have enough change suggestions to resping the next revision. Will send an update later today.

#14 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

In reply to Suren Baghdasaryan
Quoted 29 lines · Suren Baghdasaryan

The name is stored in a pointer in the shared union in vm_area_struct that points to a null terminated string. Anonymous vmas with the same name (equivalent strings) and are otherwise mergeable will be merged. The name pointers are not shared between vmas even if they contain the same name. The name pointer is stored in a union with fields that are only used on file-backed mappings, so it does not increase memory usage.

The patch is based on the original patch developed by Colin Cross, more specifically on its latest version [1] posted upstream by Sumit Semwal. It used a userspace pointer to store vma names. In that design, name pointers could be shared between vmas. However during the last upstreaming attempt, Kees Cook raised concerns [2] about this approach and suggested to copy the name into kernel memory space, perform validity checks [3] and store as a string referenced from vm_area_struct.
One big concern is about fork() performance which would need to strdup anonymous vma names. Dave Hansen suggested experimenting with worst-case scenario of forking a process with 64k vmas having longest possible names [4]. I ran this experiment on an ARM64 Android device and recorded a worst-case regression of almost 40% when forking such a process. This regression is addressed in the followup patch which replaces the pointer to a name with a refcounted structure that allows sharing the name pointer between vmas of the same name. Instead of duplicating the string during fork() or when splitting a vma it increments the refcount.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/202009031031.D32EF57ED@keescook/
[3] https://lore.kernel.org/linux-mm/202009031022.3834F692@keescook/
[4] https://lore.kernel.org/linux-mm/[email protected]/

...

include/linux/mm_types.h typedef struct { · line 842

Quoted 12 lines
include/linux/mm_types.h +12 -0
+
+/* mmap_lock should be read-locked */
+static inline bool is_same_vma_anon_name(struct vm_area_struct *vma,
+					 const char *name)
+{
+	const char *vma_name = vma_anon_name(vma);
+
+	if (likely(!vma_name))
+		return name == NULL;
+
+	return name && !strcmp(name, vma_name);
+}

Hi Suren! There is very important moment with this new feature: if we assign a name to some VMA it won't longer be mergeable even if near VMA matches by all other attributes such as flags, permissions and etc. I mean our vma_merge() start considering the vma namings and names mismatch potentially blocks merging which happens now without this new feature. Is it known behaviour or I miss something pretty obvious here?

author reviewer
#15 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 45 lines · Cyrill Gorcunov
Quoted 29 lines · Suren Baghdasaryan

The name is stored in a pointer in the shared union in vm_area_struct that points to a null terminated string. Anonymous vmas with the same name (equivalent strings) and are otherwise mergeable will be merged. The name pointers are not shared between vmas even if they contain the same name. The name pointer is stored in a union with fields that are only used on file-backed mappings, so it does not increase memory usage.

The patch is based on the original patch developed by Colin Cross, more specifically on its latest version [1] posted upstream by Sumit Semwal. It used a userspace pointer to store vma names. In that design, name pointers could be shared between vmas. However during the last upstreaming attempt, Kees Cook raised concerns [2] about this approach and suggested to copy the name into kernel memory space, perform validity checks [3] and store as a string referenced from vm_area_struct.
One big concern is about fork() performance which would need to strdup anonymous vma names. Dave Hansen suggested experimenting with worst-case scenario of forking a process with 64k vmas having longest possible names [4]. I ran this experiment on an ARM64 Android device and recorded a worst-case regression of almost 40% when forking such a process. This regression is addressed in the followup patch which replaces the pointer to a name with a refcounted structure that allows sharing the name pointer between vmas of the same name. Instead of duplicating the string during fork() or when splitting a vma it increments the refcount.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/202009031031.D32EF57ED@keescook/
[3] https://lore.kernel.org/linux-mm/202009031022.3834F692@keescook/
[4] https://lore.kernel.org/linux-mm/[email protected]/

...

include/linux/mm_types.h typedef struct { · line 842

Quoted 12 lines
include/linux/mm_types.h +12 -0
+
+/* mmap_lock should be read-locked */
+static inline bool is_same_vma_anon_name(struct vm_area_struct *vma,
+                                      const char *name)
+{
+     const char *vma_name = vma_anon_name(vma);
+
+     if (likely(!vma_name))
+             return name == NULL;
+
+     return name && !strcmp(name, vma_name);
+}
Quoted 7 lines

Hi Suren! There is very important moment with this new feature: if we assign a name to some VMA it won't longer be mergeable even if near VMA matches by all other attributes such as flags, permissions and etc. I mean our vma_merge() start considering the vma namings and names mismatch potentially blocks merging which happens now without this new feature. Is it known behaviour or I miss something pretty obvious here?

Hi Cyrill,
Correct, this is a known drawback of naming an anonymous VMA. I think I'll need to document this in prctl(2) manpage, which I should update to include this new PR_SET_VMA_ANON_NAME option.
Thanks for pointing it out!
Suren.

reviewer
#16 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

In reply to Suren Baghdasaryan

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

Quoted 3 lines

Userspace can set the name for a region of memory by calling prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, (unsigned long)name); Setting the name to NULL clears it.

Maybe I am missing this part but I do not see this being handled anywhere.

[...]

Quoted 4 lines
include/linux/mm.h +0 -0
@@ -3283,5 +3283,16 @@ static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
 	return 0;
 }
 

include/linux/mm.h static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) · line 3295

Quoted 10 lines
include/linux/mm.h +10 -0
+#ifdef CONFIG_ADVISE_SYSCALLS
+int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+			  unsigned long len_in, const char *name);
+#else
+static inline int
+madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+		      unsigned long len_in, const char *name) {
+	return 0;
+}
+#endif

You want to make this depend on CONFIG_PROC_FS.

[...]

Quoted 8 lines
kernel/sys.c +8 -0
+#ifdef CONFIG_MMU
+
+#define ANON_VMA_NAME_MAX_LEN	64
+
+static int prctl_set_vma(unsigned long opt, unsigned long addr,
+			 unsigned long size, unsigned long arg)
+{
+	struct mm_struct *mm = current->mm;

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2318

Quoted 10 lines
kernel/sys.c +10 -0
+	char *name, *pch;
+	int error;
+
+	switch (opt) {
+	case PR_SET_VMA_ANON_NAME:
+		name = strndup_user((const char __user *)arg,
+				    ANON_VMA_NAME_MAX_LEN);
+
+		if (IS_ERR(name))
+			return PTR_ERR(name);

unless I am missing something NULL name would lead to an error rather than a name clearing as advertised above.

Quoted 18 lines
kernel/sys.c +18 -0
+
+		for (pch = name; *pch != '\0'; pch++) {
+			if (!isprint(*pch)) {
+				kfree(name);
+				return -EINVAL;
+			}
+		}
+
+		mmap_write_lock(mm);
+		error = madvise_set_anon_name(mm, addr, size, name);
+		mmap_write_unlock(mm);
+		kfree(name);
+		break;
+	default:
+		error = -EINVAL;
+	}
+
+	return error;
Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#17 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 9 lines · Michal Hocko' via kernel-team

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

Quoted 3 lines

Userspace can set the name for a region of memory by calling prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, (unsigned long)name); Setting the name to NULL clears it.

Maybe I am missing this part but I do not see this being handled anywhere.

It's handled in replace_vma_anon_name(). When name==NULL we call free_vma_anon_name() which frees and resets anon_name pointer. Except that, as you noticed, the check after strndup_user() will prevent NULL to be passed here. I forgot to test this case after conversion to strndup_user() and missed this important point. Thanks for pointing it out. Will fix and retest.

Quoted 6 lines

[...]

Quoted 4 lines
include/linux/mm.h +0 -0
@@ -3283,5 +3283,16 @@ static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
      return 0;
 }
 
Quoted 12 lines

include/linux/mm.h static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) · line 3295

Quoted 10 lines
include/linux/mm.h +10 -0
+#ifdef CONFIG_ADVISE_SYSCALLS
+int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+                       unsigned long len_in, const char *name);
+#else
+static inline int
+madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
+                   unsigned long len_in, const char *name) {
+     return 0;
+}
+#endif

You want to make this depend on CONFIG_PROC_FS.

Ack.

Quoted 15 lines

[...]

Quoted 13 lines
kernel/sys.c +13 -0
+#ifdef CONFIG_MMU
+
+#define ANON_VMA_NAME_MAX_LEN        64
+
+static int prctl_set_vma(unsigned long opt, unsigned long addr,
+                      unsigned long size, unsigned long arg)
+{
+     struct mm_struct *mm = current->mm;
+     char *name, *pch;
+     int error;
+
+     switch (opt) {
+     case PR_SET_VMA_ANON_NAME:
Quoted 8 lines

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2318

Quoted 5 lines
kernel/sys.c +5 -0
+             name = strndup_user((const char __user *)arg,
+                                 ANON_VMA_NAME_MAX_LEN);
+
+             if (IS_ERR(name))
+                     return PTR_ERR(name);

unless I am missing something NULL name would lead to an error rather than a name clearing as advertised above.

Correct, I missed that. Will fix.

Quoted 26 lines
Quoted 9 lines
kernel/sys.c +8 -0
+
+             for (pch = name; *pch != '\0'; pch++) {
+                     if (!isprint(*pch)) {
+                             kfree(name);
+                             return -EINVAL;
+                     }
+             }
+

kernel/sys.c int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, · line 2336

Quoted 10 lines
kernel/sys.c +10 -0
+             mmap_write_lock(mm);
+             error = madvise_set_anon_name(mm, addr, size, name);
+             mmap_write_unlock(mm);
+             kfree(name);
+             break;
+     default:
+             error = -EINVAL;
+     }
+
+     return error;

--
Michal Hocko
SUSE Labs

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

reviewer
#18 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

In reply to Suren Baghdasaryan

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 109

Quoted 10 lines
mm/madvise.c +10 -0
+static void replace_vma_anon_name(struct vm_area_struct *vma, const char *name)
+{
+	if (!name) {
+		free_vma_anon_name(vma);
+		return;
+	}
+
+	if (vma->anon_name) {
+		/* Should never happen, to dup use dup_vma_anon_name() */
+		WARN_ON(vma->anon_name == name);

What is the point of this warning?

Quoted 9 lines
mm/madvise.c +9 -0
+
+		/* Same name, nothing to do here */
+		if (!strcmp(name, vma->anon_name))
+			return;
+
+		free_vma_anon_name(vma);
+	}
+	vma->anon_name = kstrdup(name, GFP_KERNEL);
+}
Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#19 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 15 lines · Michal Hocko' via kernel-team

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 109

Quoted 10 lines
mm/madvise.c +10 -0
+static void replace_vma_anon_name(struct vm_area_struct *vma, const char *name)
+{
+     if (!name) {
+             free_vma_anon_name(vma);
+             return;
+     }
+
+     if (vma->anon_name) {
+             /* Should never happen, to dup use dup_vma_anon_name() */
+             WARN_ON(vma->anon_name == name);

What is the point of this warning?

I wanted to make sure replace_vma_anon_name() is not used from inside vm_area_dup() or some similar place (does not exist today but maybe in the future) where "new" vma is a copy of "orig" vma and new->anon_name==orig->anon_name. If someone by mistake calls replace_vma_anon_name(new, orig->anon_name) and new->anon_name==orig->anon_name then they will keep pointing to the same name pointer, which breaks an assumption that ->anon_name pointers are not shared among vmas even if the string is the same. That would eventually lead to use-after-free error. After the next patch implementing refcounting, the similar situation would lead to both new and orig vma pointing to the same anon_vma_name structure without raising the refcount, which would also lead to use-after-free error. That's why the above comment asks to use dup_vma_anon_name() if this warning ever happens.
I can remove the warning but I thought the problem is subtle enough to put some safeguards.

Quoted 17 lines

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 118

Quoted 10 lines
mm/madvise.c +9 -0
+
+             /* Same name, nothing to do here */
+             if (!strcmp(name, vma->anon_name))
+                     return;
+
+             free_vma_anon_name(vma);
+     }
+     vma->anon_name = kstrdup(name, GFP_KERNEL);
+}

--
Michal Hocko
SUSE Labs

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

reviewer
#20 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 18 lines · Suren Baghdasaryan
Quoted 15 lines · Michal Hocko' via kernel-team

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 109

Quoted 10 lines
mm/madvise.c +10 -0
+static void replace_vma_anon_name(struct vm_area_struct *vma, const char *name)
+{
+     if (!name) {
+             free_vma_anon_name(vma);
+             return;
+     }
+
+     if (vma->anon_name) {
+             /* Should never happen, to dup use dup_vma_anon_name() */
+             WARN_ON(vma->anon_name == name);

What is the point of this warning?

Quoted 16 lines

I wanted to make sure replace_vma_anon_name() is not used from inside vm_area_dup() or some similar place (does not exist today but maybe in the future) where "new" vma is a copy of "orig" vma and new->anon_name==orig->anon_name. If someone by mistake calls replace_vma_anon_name(new, orig->anon_name) and new->anon_name==orig->anon_name then they will keep pointing to the same name pointer, which breaks an assumption that ->anon_name pointers are not shared among vmas even if the string is the same. That would eventually lead to use-after-free error. After the next patch implementing refcounting, the similar situation would lead to both new and orig vma pointing to the same anon_vma_name structure without raising the refcount, which would also lead to use-after-free error. That's why the above comment asks to use dup_vma_anon_name() if this warning ever happens.
I can remove the warning but I thought the problem is subtle enough to put some safeguards.

This to me sounds very much like a debugging code that shouldn't make it to the final patch to be merged. I do see your point of an early diagnostic but we are talking about an internal MM code and that is not really designed to be robust against its own failures so I do not see why this should be any special.

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#21 lore raw

Re: [PATCH v8 2/3] mm: add a field to store names for private anonymous memory

Quoted 37 lines · Michal Hocko' via kernel-team
Quoted 18 lines · Suren Baghdasaryan
Quoted 15 lines · Michal Hocko' via kernel-team

On Fri 27-08-21 12:18:57, Suren Baghdasaryan wrote:
[...]

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 109

Quoted 10 lines
mm/madvise.c +10 -0
+static void replace_vma_anon_name(struct vm_area_struct *vma, const char *name)
+{
+     if (!name) {
+             free_vma_anon_name(vma);
+             return;
+     }
+
+     if (vma->anon_name) {
+             /* Should never happen, to dup use dup_vma_anon_name() */
+             WARN_ON(vma->anon_name == name);

What is the point of this warning?

Quoted 16 lines

I wanted to make sure replace_vma_anon_name() is not used from inside vm_area_dup() or some similar place (does not exist today but maybe in the future) where "new" vma is a copy of "orig" vma and new->anon_name==orig->anon_name. If someone by mistake calls replace_vma_anon_name(new, orig->anon_name) and new->anon_name==orig->anon_name then they will keep pointing to the same name pointer, which breaks an assumption that ->anon_name pointers are not shared among vmas even if the string is the same. That would eventually lead to use-after-free error. After the next patch implementing refcounting, the similar situation would lead to both new and orig vma pointing to the same anon_vma_name structure without raising the refcount, which would also lead to use-after-free error. That's why the above comment asks to use dup_vma_anon_name() if this warning ever happens.
I can remove the warning but I thought the problem is subtle enough to put some safeguards.

Quoted 5 lines

This to me sounds very much like a debugging code that shouldn't make it to the final patch to be merged. I do see your point of an early diagnostic but we are talking about an internal MM code and that is not really designed to be robust against its own failures so I do not see why this should be any special.

Fair enough. I posted v9 yesterday but will respin another version in a couple days. Will remove the warning then.
Thanks,
Suren.

Quoted 7 lines

--
Michal Hocko
SUSE Labs

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

author reviewer
#22 lore raw

PATCH v8 3/3 mm: add anonymous vma name refcounting

In reply to Suren Baghdasaryan

While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure.
With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable.

---
 include/linux/mm_types.h |  9 ++++++++-
 mm/madvise.c             | 42 +++++++++++++++++++++++++++++++++-------
 2 files changed, 43 insertions(+), 8 deletions(-)
include/linux/mm_types.h +8 -1
index 26a30f7a5228..a7361acf2921 100644
@@ -5,6 +5,7 @@
 #include <linux/mm_types_task.h>
 
 #include <linux/auxvec.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
 #include <linux/rbtree.h>
@@ -302,6 +303,12 @@ struct vm_userfaultfd_ctx {
 struct vm_userfaultfd_ctx {};
 #endif /* CONFIG_USERFAULTFD */
 
+struct anon_vma_name {
+	struct kref kref;
+	/* The name needs to be at the end because it is dynamically sized. */
+	char name[];
+};
+
 /*
  * This struct describes a virtual memory area. There is one of these
  * per VM-area/task. A VM area is any part of the process virtual memory
@@ -353,7 +360,7 @@ struct vm_area_struct {
 			unsigned long rb_subtree_last;
 		} shared;
 		/* Serialized by mmap_sem. */
-		char *anon_name;
+		struct anon_vma_name *anon_name;
 	};
 
 	/*
mm/madvise.c +35 -7

85 lines not on this page · raw message

Signature · 1 line
2.33.0.259.gc128427fd7-goog
#23 lore raw

Re: [PATCH v8 3/3] mm: add anonymous vma name refcounting

Quoted 10 lines · Suren Baghdasaryan

While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of

Quoted 8 lines

anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure.
With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable.

I like the refcounting; thank you!

Since patch2 adds a lot of things that are changed by patch3; maybe combine them?

Signature · 1 line
Kees Cook
author reviewer
#24 lore raw

Re: [PATCH v8 3/3] mm: add anonymous vma name refcounting

Quoted 21 lines · Kees Cook
Quoted 10 lines · Suren Baghdasaryan

While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of

Quoted 8 lines

anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure.
With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable.

Quoted 4 lines

I like the refcounting; thank you!

Since patch2 adds a lot of things that are changed by patch3; maybe combine them?

I thought it would be easier to review with the main logic being written using a basic type (string) first and then replace the basic type with a more complex refcounted structure. Also, if someone would like to rerun the tests and measure the regression of strdup vs refcounting approach, keeping this patch separate makes it easier to set up these tests.
If that's not convenient I can absolutely squash them together.

Quoted 3 lines

--
Kees Cook

#25 lore raw

Re: [PATCH v8 3/3] mm: add anonymous vma name refcounting

In reply to Suren Baghdasaryan

Am Freitag, 27. August 2021, 21:18:58 CEST schrieb Suren Baghdasaryan:

Quoted 36 lines

While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure.
With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable.

---
 include/linux/mm_types.h |  9 ++++++++-
 mm/madvise.c             | 42 +++++++++++++++++++++++++++++++++-------
 2 files changed, 43 insertions(+), 8 deletions(-)
mm/madvise.c +3 -0
index bc029f3fca6a..32ac5dc5ebf3 100644
@@ -63,6 +63,27 @@ static int madvise_need_mmap_write(int behavior)
 	}
 }
 
+static struct anon_vma_name *anon_vma_name_alloc(const char *name)
+{
+	struct anon_vma_name *anon_name;

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 78

Quoted 10 lines
mm/madvise.c +10 -0
+	size_t len = strlen(name);
+
+	/* Add 1 for NUL terminator at the end of the anon_name->name */
+	anon_name = kzalloc(sizeof(*anon_name) + len + 1,
+			    GFP_KERNEL);
+	kref_init(&anon_name->kref);
+	strcpy(anon_name->name, name);
+
+	return anon_name;
+}

Given that you overwrite anything in that struct anyway this could be reduced to kmalloc(), no? And it definitely needs a NULL check.

Eike

Signature · 7 lines
Rolf Eike Beer, emlix GmbH, http://www.emlix.com
Fon +49 551 30664-0, Fax +49 551 30664-11
Gothaer Platz 3, 37083 Göttingen, Germany
Sitz der Gesellschaft: Göttingen, Amtsgericht Göttingen HR B 3160
Geschäftsführung: Heike Jordan, Dr. Uwe Kracke – Ust-IdNr.: DE 205 198 055

emlix - smart embedded open source
author reviewer
#26 lore raw

Re: [PATCH v8 3/3] mm: add anonymous vma name refcounting

Quoted 35 lines · Rolf Eike Beer

Am Freitag, 27. August 2021, 21:18:58 CEST schrieb Suren Baghdasaryan:

Quoted 33 lines

While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure.
With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable.

---
 include/linux/mm_types.h |  9 ++++++++-
 mm/madvise.c             | 42 +++++++++++++++++++++++++++++++++-------
 2 files changed, 43 insertions(+), 8 deletions(-)
mm/madvise.c +0 -0
index bc029f3fca6a..32ac5dc5ebf3 100644
@@ -63,6 +63,27 @@ static int madvise_need_mmap_write(int behavior)
      }
 }
 
Quoted 16 lines

mm/madvise.c static int madvise_need_mmap_write(int behavior) · line 78

Quoted 13 lines
mm/madvise.c +13 -0
+static struct anon_vma_name *anon_vma_name_alloc(const char *name)
+{
+     struct anon_vma_name *anon_name;
+     size_t len = strlen(name);
+
+     /* Add 1 for NUL terminator at the end of the anon_name->name */
+     anon_name = kzalloc(sizeof(*anon_name) + len + 1,
+                         GFP_KERNEL);
+     kref_init(&anon_name->kref);
+     strcpy(anon_name->name, name);
+
+     return anon_name;
+}

Given that you overwrite anything in that struct anyway this could be reduced to kmalloc(), no? And it definitely needs a NULL check.

Ack. I'll address both points in the next revision. Thanks!
Suren.

Quoted 10 lines

Eike
--
Rolf Eike Beer, emlix GmbH, http://www.emlix.com
Fon +49 551 30664-0, Fax +49 551 30664-11
Gothaer Platz 3, 37083 Göttingen, Germany
Sitz der Gesellschaft: Göttingen, Amtsgericht Göttingen HR B 3160 Geschäftsführung: Heike Jordan, Dr. Uwe Kracke – Ust-IdNr.: DE 205 198 055

emlix - smart embedded open source

#27 lore raw
In reply to Suren Baghdasaryan

Hi!

Quoted 1 line
 Documentation/filesystems/proc.rst |   2 +

Documentation for the setting part would be welcome, too.

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
author reviewer
#28 lore raw
Quoted 6 lines · Pavel Machek

Hi!

Quoted 1 line
 Documentation/filesystems/proc.rst |   2 +

Documentation for the setting part would be welcome, too.

Absolutely! Thanks for reminding me. I'll add a description of the new PR_SET_VMA and PR_SET_VMA_ANON_NAME options for prctl(2) manpage into the second patch of this series which introduces them. After the patch is finalized and accepted I'll also post a patch to update the prctl(2) manpage.
Thanks,
Suren.

Quoted 8 lines

Best regards,
Pavel
--
http://www.livejournal.com/~pavelmachek

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].