2020-01-20 14:20:21

by Cengiz Can

[permalink] [raw]
Subject: [PATCH] tools: perf: add missing unlock to maps__insert error case

`tools/perf/util/map.c` has a function named `maps__insert` that
acquires a write lock if its in multithread context.

Even though this lock is released when function successfully completes,
there's a branch that is executed when `maps_by_name == NULL` that
returns from this function without releasing the write lock.

Added an `up_write` to release the lock when this happens.

Signed-off-by: Cengiz Can <[email protected]>
---

Hello Arnaldo,

I'm not exactly sure about the order that we must call up_write here.

Please tell me if the `__maps__free_maps_by_name` frees the
`rw_semaphore`. If that's the case, should we change the order to unlock and free?

Thanks!

tools/perf/util/map.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index fdd5bddb3075..f67960bedebb 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -549,6 +549,7 @@ void maps__insert(struct maps *maps, struct map *map)

if (maps_by_name == NULL) {
__maps__free_maps_by_name(maps);
+ up_write(&maps->lock);
return;
}

--
2.25.0


2020-01-31 08:40:06

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH] tools: perf: add missing unlock to maps__insert error case

Em Mon, Jan 20, 2020 at 05:15:54PM +0300, Cengiz Can escreveu:
> `tools/perf/util/map.c` has a function named `maps__insert` that
> acquires a write lock if its in multithread context.
>
> Even though this lock is released when function successfully completes,
> there's a branch that is executed when `maps_by_name == NULL` that
> returns from this function without releasing the write lock.
>
> Added an `up_write` to release the lock when this happens.
>
> Signed-off-by: Cengiz Can <[email protected]>
> ---
>
> Hello Arnaldo,
>
> I'm not exactly sure about the order that we must call up_write here.
>
> Please tell me if the `__maps__free_maps_by_name` frees the
> `rw_semaphore`. If that's the case, should we change the order to unlock and free?

No it doesn't free the rw_semaphore, that is in 'struct maps', what is
being freed is just something protected by rw_semaphore,
maps->maps_by_name, so your patch is right and I'm applying it, thanks.

- Arnaldo

> Thanks!
>
> tools/perf/util/map.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
> index fdd5bddb3075..f67960bedebb 100644
> --- a/tools/perf/util/map.c
> +++ b/tools/perf/util/map.c
> @@ -549,6 +549,7 @@ void maps__insert(struct maps *maps, struct map *map)
>
> if (maps_by_name == NULL) {
> __maps__free_maps_by_name(maps);
> + up_write(&maps->lock);
> return;
> }
>
> --
> 2.25.0
>

--

- Arnaldo

2020-01-31 08:45:13

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH] tools: perf: add missing unlock to maps__insert error case

Em Fri, Jan 31, 2020 at 09:38:58AM +0100, Arnaldo Carvalho de Melo escreveu:
> Em Mon, Jan 20, 2020 at 05:15:54PM +0300, Cengiz Can escreveu:
> > Please tell me if the `__maps__free_maps_by_name` frees the
> > `rw_semaphore`. If that's the case, should we change the order to unlock and free?
>
> No it doesn't free the rw_semaphore, that is in 'struct maps', what is
> being freed is just something protected by rw_semaphore,
> maps->maps_by_name, so your patch is right and I'm applying it, thanks.

BTW, you forgot to add:

Fixes: a7c2b572e217 ("perf map_groups: Auto sort maps by name, if needed")

Which I did, and next time please CC the perf tools reviewers, as noted
in MAINTAINERS, the lines starting with R:.

- Arnaldo

[acme@quaco perf]$ grep -A21 "PERFORMANCE EVENTS SUBSYSTEM$" MAINTAINERS
PERFORMANCE EVENTS SUBSYSTEM
M: Peter Zijlstra <[email protected]>
M: Ingo Molnar <[email protected]>
M: Arnaldo Carvalho de Melo <[email protected]>
R: Mark Rutland <[email protected]>
R: Alexander Shishkin <[email protected]>
R: Jiri Olsa <[email protected]>
R: Namhyung Kim <[email protected]>
L: [email protected]
T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git perf/core
S: Supported
F: kernel/events/*
F: include/linux/perf_event.h
F: include/uapi/linux/perf_event.h
F: arch/*/kernel/perf_event*.c
F: arch/*/kernel/*/perf_event*.c
F: arch/*/kernel/*/*/perf_event*.c
F: arch/*/include/asm/perf_event.h
F: arch/*/kernel/perf_callchain.c
F: arch/*/events/*
F: arch/*/events/*/*
F: tools/perf/
[acme@quaco perf]$

2020-01-31 15:44:01

by Cengiz Can

[permalink] [raw]
Subject: Re: [PATCH] tools: perf: add missing unlock to maps__insert error case


On January 31, 2020 11:43:46 Arnaldo Carvalho de Melo
<[email protected]> wrote:

> Em Fri, Jan 31, 2020 at 09:38:58AM +0100, Arnaldo Carvalho de Melo escreveu:
>> Em Mon, Jan 20, 2020 at 05:15:54PM +0300, Cengiz Can escreveu:
>>> Please tell me if the `__maps__free_maps_by_name` frees the
>>> `rw_semaphore`. If that's the case, should we change the order to unlock
>>> and free?
>>
>> No it doesn't free the rw_semaphore, that is in 'struct maps', what is
>> being freed is just something protected by rw_semaphore,
>> maps->maps_by_name, so your patch is right and I'm applying it, thanks.
>
> BTW, you forgot to add:
>
> Fixes: a7c2b572e217 ("perf map_groups: Auto sort maps by name, if needed")
>
> Which I did, and next time please CC the perf tools reviewers, as noted
> in MAINTAINERS, the lines starting with R:.

Missed that. Thank you for reminding and correction.

Cheers
>
> - Arnaldo
>
> [acme@quaco perf]$ grep -A21 "PERFORMANCE EVENTS SUBSYSTEM$" MAINTAINERS
> PERFORMANCE EVENTS SUBSYSTEM
> M: Peter Zijlstra <[email protected]>
> M: Ingo Molnar <[email protected]>
> M: Arnaldo Carvalho de Melo <[email protected]>
> R: Mark Rutland <[email protected]>
> R: Alexander Shishkin <[email protected]>
> R: Jiri Olsa <[email protected]>
> R: Namhyung Kim <[email protected]>
> L: [email protected]
> T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git perf/core
> S: Supported
> F: kernel/events/*
> F: include/linux/perf_event.h
> F: include/uapi/linux/perf_event.h
> F: arch/*/kernel/perf_event*.c
> F: arch/*/kernel/*/perf_event*.c
> F: arch/*/kernel/*/*/perf_event*.c
> F: arch/*/include/asm/perf_event.h
> F: arch/*/kernel/perf_callchain.c
> F: arch/*/events/*
> F: arch/*/events/*/*
> F: tools/perf/
> [acme@quaco perf]$



Subject: [tip: perf/urgent] perf maps: Add missing unlock to maps__insert() error case

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

Commit-ID: 85fc95d75970ee7dd8e01904e7fb1197c275ba6b
Gitweb: https://git.kernel.org/tip/85fc95d75970ee7dd8e01904e7fb1197c275ba6b
Author: Cengiz Can <[email protected]>
AuthorDate: Mon, 20 Jan 2020 17:15:54 +03:00
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Fri, 31 Jan 2020 09:40:50 +01:00

perf maps: Add missing unlock to maps__insert() error case

`tools/perf/util/map.c` has a function named `maps__insert` that
acquires a write lock if its in multithread context.

Even though this lock is released when function successfully completes,
there's a branch that is executed when `maps_by_name == NULL` that
returns from this function without releasing the write lock.

Added an `up_write` to release the lock when this happens.

Fixes: a7c2b572e217 ("perf map_groups: Auto sort maps by name, if needed")
Signed-off-by: Cengiz Can <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/map.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index fdd5bdd..f67960b 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -549,6 +549,7 @@ void maps__insert(struct maps *maps, struct map *map)

if (maps_by_name == NULL) {
__maps__free_maps_by_name(maps);
+ up_write(&maps->lock);
return;
}