Skip to contentSkip to conversation

perf: rework memory accounting in perf_mmap()

PATCH Applied Song Liu 4 participants 8 messages Sep 4, 2019 – Oct 9, 2019 perf events

lore
Share & tools
lore

Song Liu

author
#0 lore raw

PATCH 1/1 perf: rework memory accounting in perf_mmap()

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|

Fix this by maintaining proper user_extra and extra.

---
 kernel/events/core.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
kernel/events/core.c +15 -2
index 0463c1151bae..a0120bdbce17 100644
@@ -5585,7 +5585,8 @@ static void perf_mmap_close(struct vm_area_struct *vma)
 	 * undo the VM accounting.
 	 */
 
-	atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
+	atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
+			&mmap_user->locked_vm);
 	atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
 	free_uid(mmap_user);
 
@@ -5729,8 +5730,20 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 
 	user_locked = atomic_long_read(&user->locked_vm) + user_extra;
 
-	if (user_locked > user_lock_limit)
+	if (user_locked <= user_lock_limit) {
+		/* charge all to locked_vm */
+	} else if (atomic_long_read(&user->locked_vm) >= user_lock_limit) {
+		/* charge all to pinned_vm */
+		extra = user_extra;
+		user_extra = 0;
+	} else {
+		/*
+		 * charge locked_vm until it hits user_lock_limit;
+		 * charge the rest from pinned_vm
+		 */
 		extra = user_locked - user_lock_limit;
+		user_extra -= extra;
+	}
 
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
Signature · 1 line
2.17.1
author
#1 lore raw

Hi Peter,

Quoted 14 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|
Quoted 6 lines

Fix this by maintaining proper user_extra and extra.

Could you please share your feedbacks/comments on this one?

Thanks,
Song

Reviewed-by
#2 lore raw

Song Liu <[email protected]> wrote on Mon [2019-Sep-16 12:43:16 -0700]:

Quoted 23 lines

Hi Peter,

Quoted 14 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|
Quoted 6 lines

Fix this by maintaining proper user_extra and extra.

Quoted 4 lines

Could you please share your feedbacks/comments on this one?

Thanks,
Song

The change looks good to me. Thanks, Song.

Hechao

author
#3 lore raw
Quoted 28 lines · Hechao Li

Song Liu <[email protected]> wrote on Mon [2019-Sep-16 12:43:16 -0700]:

Quoted 23 lines

Hi Peter,

Quoted 20 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|

Fix this by maintaining proper user_extra and extra.

Quoted 8 lines
Quoted 4 lines

Could you please share your feedbacks/comments on this one?

Thanks,
Song

The change looks good to me. Thanks, Song.

Thanks Hechao!

Hi Peter,

Does this fix look good to you?

Thanks,
Song

maintainer
#4 lore raw
In reply to Song Liu
Quoted 5 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

Quoted 8 lines
| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|

Fix this by maintaining proper user_extra and extra.

Aah, indeed.

Also, this code is unreadable (which is mostly my own fault I suppose) :/

author
#5 lore raw

Hi Peter,

Quoted 15 lines · Peter Zijlstra
Quoted 12 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|
Quoted 3 lines
Quoted 1 line

Fix this by maintaining proper user_extra and extra.

Aah, indeed.

Thanks for the feedback!

Quoted 3 lines

Also, this code is unreadable (which is mostly my own fault I suppose) :/

How does this patch look to you? Is it ready to merge?

Thanks,
Song

maintainer
#6 lore raw
Quoted 21 lines · Song Liu

Hi Peter,

Quoted 15 lines · Peter Zijlstra
Quoted 12 lines · Song Liu

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

Note: Assume "user_lock_limit" is very small.

| # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
| 0                    | 0                   | 0             |
| 1                    | user_extra          | user_extra    |
| 2                    | 3 * user_extra      | 2 * user_extra|
| 3                    | 6 * user_extra      | 3 * user_extra|
| 4                    | 10 * user_extra     | 4 * user_extra|
Quoted 3 lines
Quoted 1 line

Fix this by maintaining proper user_extra and extra.

Aah, indeed.

Quoted 7 lines

Thanks for the feedback!

Quoted 3 lines

Also, this code is unreadable (which is mostly my own fault I suppose) :/

How does this patch look to you? Is it ready to merge?

Yes, I queued it, I'll get it into tip soon. It got held up due to me working on some other patches, sorry about that.

merge bot Applied
#7 lore raw

[tip: perf/urgent] perf/core: Rework memory accounting in perf_mmap()

In reply to Song Liu
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     d44248a41337731a111374822d7d4451b64e73e4
Gitweb:        https://git.kernel.org/tip/d44248a41337731a111374822d7d4451b64e73e4
Author:        Song Liu <[email protected]>
AuthorDate:    Wed, 04 Sep 2019 14:46:18 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 09 Oct 2019 12:44:12 +02:00

perf/core: Rework memory accounting in perf_mmap()

perf_mmap() always increases user->locked_vm. As a result, "extra" could grow bigger than "user_extra", which doesn't make sense. Here is an example case:

(Note: Assume "user_lock_limit" is very small.)

  | # of perf_mmap calls |vma->vm_mm->pinned_vm|user->locked_vm|
  | 0                    | 0                   | 0             |
  | 1                    | user_extra          | user_extra    |
  | 2                    | 3 * user_extra      | 2 * user_extra|
  | 3                    | 6 * user_extra      | 3 * user_extra|
  | 4                    | 10 * user_extra     | 4 * user_extra|

Fix this by maintaining proper user_extra and extra.

---
 kernel/events/core.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
kernel/events/core.c +15 -2
index f953dd1..2b8265a 100644
@@ -5668,7 +5668,8 @@ again:
 	 * undo the VM accounting.
 	 */
 
-	atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
+	atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
+			&mmap_user->locked_vm);
 	atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
 	free_uid(mmap_user);
 
@@ -5812,8 +5813,20 @@ accounting:
 
 	user_locked = atomic_long_read(&user->locked_vm) + user_extra;
 
-	if (user_locked > user_lock_limit)
+	if (user_locked <= user_lock_limit) {
+		/* charge all to locked_vm */
+	} else if (atomic_long_read(&user->locked_vm) >= user_lock_limit) {
+		/* charge all to pinned_vm */
+		extra = user_extra;
+		user_extra = 0;
+	} else {
+		/*
+		 * charge locked_vm until it hits user_lock_limit;
+		 * charge the rest from pinned_vm
+		 */
 		extra = user_locked - user_lock_limit;
+		user_extra -= extra;
+	}
 
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;