Skip to contentSkip to conversation

perf/core: fix mlock accounting in perf_mmap()

PATCH Applied Song Liu 4 participants 5 messages Jan 23, 2020 – Jan 29, 2020

Subsystems (2)
lore
Share & tools
lore

Song Liu

author
#0 lore raw

PATCH v3 1/1 perf/core: fix mlock accounting in perf_mmap()

Eecreasing sysctl_perf_event_mlock between two consecutive perf_mmap()s of a perf ring buffer may lead to an integer underflow in locked memory accounting. This may lead to the undesired behaviors, such as failures in BPF map creation.

Address this by adjusting the accounting logic to take into account the possibility that the amount of already locked memory may exceed the current limit.

Fixes c4b75479741c ("perf/core: Make the mlock accounting simple again")
Cc Alexander Shishkin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/events/core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/events/core.c +9 -1
index e543bab787e5..fdb7f7ef380c 100644
@@ -5917,7 +5917,15 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 	 */
 	user_lock_limit *= num_online_cpus();
 
-	user_locked = atomic_long_read(&user->locked_vm) + user_extra;
+	user_locked = atomic_long_read(&user->locked_vm);
+
+	/*
+	 * sysctl_perf_event_mlock may have changed, so that
+	 *     user->locked_vm > user_lock_limit
+	 */
+	if (user_locked > user_lock_limit)
+		user_locked = user_lock_limit;
+	user_locked += user_extra;
 
 	if (user_locked > user_lock_limit) {
 		/*
Signature · 1 line
2.17.1
reviewer Acked-by on v3 1/1
#1 lore raw
Quoted 1 line · Song Liu

Eecreasing sysctl_perf_event_mlock between two consecutive perf_mmap()s of

Typo: "Decreasing".

Quoted 8 lines

a perf ring buffer may lead to an integer underflow in locked memory accounting. This may lead to the undesired behaviors, such as failures in BPF map creation.

Address this by adjusting the accounting logic to take into account the possibility that the amount of already locked memory may exceed the current limit.

Quoted 7 lines
Fixes c4b75479741c ("perf/core: Make the mlock accounting simple again")
Cc Alexander Shishkin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Peter Zijlstra <[email protected]>

Other than that,

Thanks,
--
Alex

author
#2 lore raw

Thanks Alex!

Quoted 7 lines · Alexander Shishkin
Quoted 1 line · Song Liu

Eecreasing sysctl_perf_event_mlock between two consecutive perf_mmap()s of

Typo: "Decreasing".

Peter, could you please fix this typo when you apply the patch? I guess it is not necessary to spam the list with a v4 just for this typo.

Thanks,
Song

Quoted 24 lines
Quoted 9 lines

a perf ring buffer may lead to an integer underflow in locked memory accounting. This may lead to the undesired behaviors, such as failures in BPF map creation.

Address this by adjusting the accounting logic to take into account the possibility that the amount of already locked memory may exceed the current limit.

Quoted 7 lines
Fixes c4b75479741c ("perf/core: Make the mlock accounting simple again")
Cc Alexander Shishkin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Peter Zijlstra <[email protected]>

Other than that,

Thanks,
--
Alex

maintainer
#3 lore raw
Quoted 4 lines · Song Liu

Thanks Alex!

Quoted 2 lines · Alexander Shishkin
Quoted 8 lines
Quoted 5 lines
Quoted 1 line · Song Liu

Eecreasing sysctl_perf_event_mlock between two consecutive perf_mmap()s of

Typo: "Decreasing".

Peter, could you please fix this typo when you apply the patch? I guess it is not necessary to spam the list with a v4 just for this typo.

Done!

merge bot Applied
#4 lore raw

[tip: perf/urgent] perf/core: Fix mlock 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:     003461559ef7a9bd0239bae35a22ad8924d6e9ad
Gitweb:        https://git.kernel.org/tip/003461559ef7a9bd0239bae35a22ad8924d6e9ad
Author:        Song Liu <[email protected]>
AuthorDate:    Thu, 23 Jan 2020 10:11:46 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 28 Jan 2020 21:20:18 +01:00

perf/core: Fix mlock accounting in perf_mmap()

Decreasing sysctl_perf_event_mlock between two consecutive perf_mmap()s of a perf ring buffer may lead to an integer underflow in locked memory accounting. This may lead to the undesired behaviors, such as failures in BPF map creation.

Address this by adjusting the accounting logic to take into account the possibility that the amount of already locked memory may exceed the current limit.

---
 kernel/events/core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/events/core.c +9 -1
index 2173c23..2d9aeba 100644
@@ -5916,7 +5916,15 @@ accounting:
 	 */
 	user_lock_limit *= num_online_cpus();
 
-	user_locked = atomic_long_read(&user->locked_vm) + user_extra;
+	user_locked = atomic_long_read(&user->locked_vm);
+
+	/*
+	 * sysctl_perf_event_mlock may have changed, so that
+	 *     user->locked_vm > user_lock_limit
+	 */
+	if (user_locked > user_lock_limit)
+		user_locked = user_lock_limit;
+	user_locked += user_extra;
 
 	if (user_locked > user_lock_limit) {
 		/*