Skip to contentSkip to conversation

perf ksymbol: fix memory leak: decrease refcount of map and dso

PATCH Superseded Riccardo Mancini 3 participants 9 messages Jun 2, 2021 – Jun 18, 2021 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf ksymbol: fix memory leak: decrease refcount of map and dso

ASan reported a memory leak of BPF-related ksymbols map and dso. The leak is caused by refcount never reaching 0, due to missing __put calls in the function machine__process_ksymbol_register. Once the dso is inserted in map, dso__put should be called, since map__new2 has increased its refcount to 2.
The same thing applies for the map when it's inserted into the rb-tree in maps (maps__insert increases the refcount to 2).

$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6992 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20
    #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10
    [...]

Indirect leak of 8702 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20
    #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9
    #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21
    [...]

Indirect leak of 1520 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8
    [...]

Indirect leak of 1406 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8
    [...]
---
 tools/perf/util/machine.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/machine.c +2 -0
index 3ff4936a15a42..d5937778875e1 100644
@@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		if (dso) {
 			dso->kernel = DSO_SPACE__KERNEL;
 			map = map__new2(0, dso);
+			dso__put(dso);
 		}
 
 		if (!dso || !map) {
@@ -792,6 +793,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		map->start = event->ksymbol.addr;
 		map->end = map->start + event->ksymbol.len;
 		maps__insert(&machine->kmaps, map);
+		map__put(map);
 		dso__set_loaded(dso);
 
 		if (is_bpf_image(event->ksymbol.name)) {
Signature · 1 line
2.31.1
reviewer
#1 lore raw
Quoted 51 lines · Riccardo Mancini

ASan reported a memory leak of BPF-related ksymbols map and dso. The leak is caused by refcount never reaching 0, due to missing __put calls in the function machine__process_ksymbol_register. Once the dso is inserted in map, dso__put should be called, since map__new2 has increased its refcount to 2.
The same thing applies for the map when it's inserted into the rb-tree in maps (maps__insert increases the refcount to 2).

$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6992 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20
    #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10
    [...]

Indirect leak of 8702 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20
    #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9
    #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21
    [...]

Indirect leak of 1520 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8
    [...]

Indirect leak of 1406 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8
    [...]
---
 tools/perf/util/machine.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/machine.c +0 -0
index 3ff4936a15a42..d5937778875e1 100644

tools/perf/util/machine.c static int machine__process_ksymbol_register(struct machine *machine, · line 779

Quoted 4 lines
tools/perf/util/machine.c +1 -0
@@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                if (dso) {
                        dso->kernel = DSO_SPACE__KERNEL;
                        map = map__new2(0, dso);
+                       dso__put(dso);

Will this cause 2 puts if the map allocation fails? Perhaps this should be "if (map) dso__put(dso);".

Thanks,
Ian

Quoted 14 lines
tools/perf/util/machine.c +1 -1
                }
 
                if (!dso || !map) {
@@ -792,6 +793,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                map->start = event->ksymbol.addr;
                map->end = map->start + event->ksymbol.len;
                maps__insert(&machine->kmaps, map);
+               map__put(map);
                dso__set_loaded(dso);
 
                if (is_bpf_image(event->ksymbol.name)) {
--

2.31.1

#2 lore raw

Em Thu, Jun 03, 2021 at 09:26:40PM -0700, Ian Rogers escreveu:

Quoted 10 lines · Riccardo Mancini

Review static int machine__process_ksymbol_register(struct machine *machine, · line 779

Quoted 6 lines

+++ b/tools/perf/util/machine.c

(quoted diff) +1 -0
@@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                if (dso) {
                        dso->kernel = DSO_SPACE__KERNEL;
                        map = map__new2(0, dso);
+                       dso__put(dso);

Will this cause 2 puts if the map allocation fails? Perhaps this should be "if (map) dso__put(dso);".

I think its just a matter of removing the put in the error path, i.e. the patch becomes what is at the end of this message.

I.e. if map__new2() fails, we want to drop the dso reference, and if it works, we already have a reference to it, obtained in map__new2().

But looking at this code now I realize that maps__find() should grab a refcount for the map it returns, because in this machine__process_ksymbol_register() function we use reference that 'map' after the if block, i.e. we use it if it came from maps__find() or if we created it machine__process_ksymbol_register, so there is a possible race where other thread removes it from the list and map__put()s it ending up in map__delete() while we still use it in machine__process_ksymbol_register(), right?

- Arnaldo

Quoted 11 lines

Review static int machine__process_ksymbol_register(struct machine *machine, · line 799

Quoted 11 lines
(quoted diff) +1 -0
                }
 
                if (!dso || !map) {
@@ -792,6 +793,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                map->start = event->ksymbol.addr;
                map->end = map->start + event->ksymbol.len;
                maps__insert(&machine->kmaps, map);
+               map__put(map);
                dso__set_loaded(dso);
 
                if (is_bpf_image(event->ksymbol.name)) {
tools/perf/util/machine.c +2 -1
index 3ff4936a15a42f74..da19be7da284c250 100644
@@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		if (dso) {
 			dso->kernel = DSO_SPACE__KERNEL;
 			map = map__new2(0, dso);
+			dso__put(dso);
 		}
 
 		if (!dso || !map) {
-			dso__put(dso);
 			return -ENOMEM;
 		}
 
@@ -792,6 +792,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		map->start = event->ksymbol.addr;
 		map->end = map->start + event->ksymbol.len;
 		maps__insert(&machine->kmaps, map);
+		map__put(map);
 		dso__set_loaded(dso);
 
 		if (is_bpf_image(event->ksymbol.name)) {
#3 lore raw

Hi,

Quoted 13 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 03, 2021 at 09:26:40PM -0700, Ian Rogers escreveu:

Quoted 11 lines · Riccardo Mancini

tools/perf/util/machine.c static int machine__process_ksymbol_register(struct · line 780

Quoted 7 lines

+++ b/tools/perf/util/machine.c

tools/perf/util/machine.c +1 -0
@@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct
 machine *machine,
                 if (dso) {
                         dso->kernel = DSO_SPACE__KERNEL;
                         map = map__new2(0, dso);
+                       dso__put(dso);

Will this cause 2 puts if the map allocation fails? Perhaps this should be "if (map) dso__put(dso);".

Quoted 5 lines

I think its just a matter of removing the put in the error path, i.e. the patch becomes what is at the end of this message.

I.e. if map__new2() fails, we want to drop the dso reference, and if it works, we already have a reference to it, obtained in map__new2().

Agree.
I'm sorry for this stupid oversight.
Should we make it a series including the fix to the issue you pointed out below, or should I send you a v2 and fix the other issue in a subsequent patch?

Quoted 8 lines

But looking at this code now I realize that maps__find() should grab a refcount for the map it returns, because in this machine__process_ksymbol_register() function we use reference that 'map' after the if block, i.e. we use it if it came from maps__find() or if we created it machine__process_ksymbol_register, so there is a possible race where other thread removes it from the list and map__put()s it ending up in map__delete() while we still use it in machine__process_ksymbol_register(), right?

Agree. It should be placed before up_read to avoid races, right? Then we would need to see where it's called and add the appropriate map__put.

In addition, having a look at other possible concurrency issues in map.c:
- maps__for_each_entry should always be called with either read or write lock,
am I right? It looks like this is not done in certain parts of the code. If such lock is taken, then grabbing the refcount on the looping variable is not needed unless we need to return it, right?
- maps__first and map__next do not grab a refcount and neither a lock. If
they're used through a lock-protected loop, it's not a problem, but maybe it's worth making explicit that they are not to be used directly (through either a comment or adding some underscores in their names).
- maps__empty: should probably take a reader lock.
- maps__find_symbol: the returned symbol is not protected (the caller does not
receive a refcount to neither map or dso, so if dso is deleted, his reference to the symbol gets invalidated). Depending on how it's being used it might not be a problem, but in the general scenario I think it's not thread-safe.

Riccardo

Quoted 42 lines

- Arnaldo

Quoted 12 lines
Quoted 12 lines
(quoted diff) +0 -0
                 }
 

                if (!dso || !map) {

tools/perf/util/machine.c +1 -0
@@ -792,6 +793,7 @@ static int machine__process_ksymbol_register(struct
 machine *machine,
                 map->start = event->ksymbol.addr;
                 map->end = map->start + event->ksymbol.len;
                 maps__insert(&machine->kmaps, map);
+               map__put(map);
                 dso__set_loaded(dso);
 

                if (is_bpf_image(event->ksymbol.name)) {

tools/perf/util/machine.c +1 -0
index 3ff4936a15a42f74..da19be7da284c250 100644
@@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct
 machine *machine,
                 if (dso) {
                         dso->kernel = DSO_SPACE__KERNEL;
                         map = map__new2(0, dso);
+                       dso__put(dso);
                 }


                if (!dso || !map) {
-                       dso__put(dso);
                        return -ENOMEM;
                }

tools/perf/util/machine.c +1 -0
@@ -792,6 +792,7 @@ static int machine__process_ksymbol_register(struct
 machine *machine,
                 map->start = event->ksymbol.addr;
                 map->end = map->start + event->ksymbol.len;
                 maps__insert(&machine->kmaps, map);
+               map__put(map);
                 dso__set_loaded(dso);


                if (is_bpf_image(event->ksymbol.name)) {

#4 lore raw

Em Fri, Jun 04, 2021 at 05:16:39PM +0200, Riccardo Mancini escreveu:

Quoted 19 lines

Hi,

Quoted 16 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 03, 2021 at 09:26:40PM -0700, Ian Rogers escreveu:

Quoted 11 lines · Riccardo Mancini

tools/perf/util/machine.c static int machine__process_ksymbol_register(struct · line 780

Quoted 7 lines

+++ b/tools/perf/util/machine.c

tools/perf/util/machine.c +1 -0
@@ -776,6 +776,7 @@ static int machine__process_ksymbol_register(struct
 machine *machine,
                 if (dso) {
                         dso->kernel = DSO_SPACE__KERNEL;
                         map = map__new2(0, dso);
+                       dso__put(dso);

Will this cause 2 puts if the map allocation fails? Perhaps this should be "if (map) dso__put(dso);".

I think its just a matter of removing the put in the error path, i.e. the patch becomes what is at the end of this message.

Quoted 7 lines
Quoted 2 lines

I.e. if map__new2() fails, we want to drop the dso reference, and if it works, we already have a reference to it, obtained in map__new2().

Agree.
I'm sorry for this stupid oversight.
Should we make it a series including the fix to the issue you pointed out below, or should I send you a v2 and fix the other issue in a subsequent patch?

Please send a v2 patch, and then consider starting a new series with the issues below.

Quoted 10 lines
Quoted 8 lines

But looking at this code now I realize that maps__find() should grab a refcount for the map it returns, because in this machine__process_ksymbol_register() function we use reference that 'map' after the if block, i.e. we use it if it came from maps__find() or if we created it machine__process_ksymbol_register, so there is a possible race where other thread removes it from the list and map__put()s it ending up in map__delete() while we still use it in machine__process_ksymbol_register(), right?

Agree. It should be placed before up_read to avoid races, right?

Yes, we have to grab a refcount while we are sure its not going away, then return that as the lookup result, whoever receives that refcounted entry should use it and then drop the refcount.

Quoted 1 line

Then we would need to see where it's called and add the appropriate map__put.

yes

Quoted 1 line

In addition, having a look at other possible concurrency issues in map.c:

Its good to have new eyes looking at this, exactly at a time we're discussing further parallelizing perf :-)

Quoted 2 lines

- maps__for_each_entry should always be called with either read or write lock,
am I right? It looks like this is not done in certain parts of the code. If such

Right.

Quoted 2 lines

lock is taken, then grabbing the refcount on the looping variable is not needed unless we need to return it, right?

Right, returning an entry needs to take a refcount.

Quoted 2 lines

- maps__first and map__next do not grab a refcount and neither a lock. If
they're used through a lock-protected loop, it's not a problem, but maybe it's

yes

Quoted 2 lines

worth making explicit that they are not to be used directly (through either a comment or adding some underscores in their names).

yes, __ in front means, in kernel style, that it does less than the non __ prefixed, same name, function.

Quoted 1 line

- maps__empty: should probably take a reader lock.

Indeed.

Quoted 4 lines

- maps__find_symbol: the returned symbol is not protected (the caller does not
receive a refcount to neither map or dso, so if dso is deleted, his reference to the symbol gets invalidated). Depending on how it's being used it might not be a problem, but in the general scenario I think it's not thread-safe.

Yes, that function is also problematic.

Thanks for looking into this, please consider sending patches for these issues,

- Arnaldo

#5 lore raw

PATCH v2 1/1 perf ksymbol: fix memory leak: decrease refcount of map and dso

ASan reported a memory leak of BPF-related ksymbols map and dso. The leak is caused by refount never reaching 0, due to missing __put calls in the function machine__process_ksymbol_register.
Once the dso is inserted in the map, dso__put should be called (map__new2 increases the refcount to 2). The same thing applies for the map when it's inserted into maps (maps__insert increases the refcount to 2).

$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6992 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20
    #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10
    [...]

Indirect leak of 8702 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20
    #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9
    #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21
    [...]

Indirect leak of 1520 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8
    [...]

Indirect leak of 1406 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8
    [...]
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
index 3ff4936a15a4..da19be7da284 100644
@@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		if (dso) {
 			dso->kernel = DSO_SPACE__KERNEL;
 			map = map__new2(0, dso);
+			dso__put(dso);
 		}
 
 		if (!dso || !map) {
-			dso__put(dso);
 			return -ENOMEM;
 		}
 
@@ -792,6 +792,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		map->start = event->ksymbol.addr;
 		map->end = map->start + event->ksymbol.len;
 		maps__insert(&machine->kmaps, map);
+		map__put(map);
 		dso__set_loaded(dso);
 
 		if (is_bpf_image(event->ksymbol.name)) {
Signature · 1 line
2.23.0
maintainer Applied
#6 lore raw

Em Sat, Jun 12, 2021 at 07:37:48PM +0200, Riccardo Mancini escreveu:

Quoted 8 lines

ASan reported a memory leak of BPF-related ksymbols map and dso. The leak is caused by refount never reaching 0, due to missing __put calls in the function machine__process_ksymbol_register.
Once the dso is inserted in the map, dso__put should be called (map__new2 increases the refcount to 2). The same thing applies for the map when it's inserted into maps (maps__insert increases the refcount to 2).

Thanks, applied.

- Arnaldo

Quoted 64 lines
$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6992 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20
    #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10
    [...]

Indirect leak of 8702 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20
    #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9
    #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21
    [...]

Indirect leak of 1520 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8
    [...]

Indirect leak of 1406 byte(s) in 19 object(s) allocated from:

    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8
    [...]
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
index 3ff4936a15a4..da19be7da284 100644
@@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		if (dso) {
 			dso->kernel = DSO_SPACE__KERNEL;
 			map = map__new2(0, dso);
+			dso__put(dso);
 		}
 
 		if (!dso || !map) {
-			dso__put(dso);
 			return -ENOMEM;
 		}
 
@@ -792,6 +792,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		map->start = event->ksymbol.addr;
 		map->end = map->start + event->ksymbol.len;
 		maps__insert(&machine->kmaps, map);
+		map__put(map);
 		dso__set_loaded(dso);
 
 		if (is_bpf_image(event->ksymbol.name)) {
Signature · 1 line
2.23.0
Signature · 1 line
- Arnaldo
#7 lore raw
In reply to Arnaldo Carvalho de Melo

Hi,

On Fri, 2021-06-04 at 15:29 -0300, Arnaldo Carvalho de Melo wrote:
<SNIP>

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines

But looking at this code now I realize that maps__find() should grab a refcount for the map it returns, because in this machine__process_ksymbol_register() function we use reference that 'map' after the if block, i.e. we use it if it came from maps__find() or if we created it machine__process_ksymbol_register, so there is a possible race where other thread removes it from the list and map__put()s it ending up in map__delete() while we still use it in machine__process_ksymbol_register(), right?

Agree. It should be placed before up_read to avoid races, right?

Quoted 8 lines

Yes, we have to grab a refcount while we are sure its not going away, then return that as the lookup result, whoever receives that refcounted entry should use it and then drop the refcount.

Quoted 2 lines

Then we would need to see where it's called and add the appropriate map__put.

yes

This function has quite a number of callers (direct and indirect) so the the patch is becoming huge.

One of these callers is thread__find_map, which returns an addr_location (actually it's an output argument). This addr_location holds references to map, maps and thread without getting any refcnt (actually in one function it gets it on the thread and a comment tells to put it once done). If I'm not wrong, this addr_location is never malloced (always a local variable) and, is should be present in parts of the code where there should be a refcnt on the thread. Therefore, maybe it does not get the refcnts since it assumes that thread (upon which depends maps and as a consequence map) is always refcnted in its context. However, I think that it should get all refcnts anyways for clarity and to prevent possible misuses (if I understood correctly, Ian is of the same opinion).

My solution would be to add the refcnt grabbing for map, maps and thread in thread__find_map, releasing them in addr_location__put, and then making sure all callers call it when no longer in use.

Following the same reasoning, I added refcnt grabbing also to mem_info, branch_info (map was already refcnted, I added it also to maps for coherency), map_symbol (as in branch_info, I added it to maps), and in other places in which I saw a pointer was passed without refcounting.

Most changes are quite trivial, however, the changelog is huge:

48 files changed, 472 insertions(+), 157 deletions(-)

Most of them are just returns converted to goto for calling the __put functions.

Doing so, I managed to remove memory leaks caused by refcounting also in perf- report (I wanted to try also perf top but I encountered another memory-related issue). However, the changelog is huge and testing all of it is challenging (especially since I can test missing puts only with ASan's LeakSanitizer and its reports are usually full of leaks, which I am trying to fix along the way, I will send some patches in the following days). How would you go about it? Do you have any suggestions?

Quoted 44 lines

Quoted 1 line

In addition, having a look at other possible concurrency issues in map.c:

Its good to have new eyes looking at this, exactly at a time we're discussing further parallelizing perf :-)

Quoted 4 lines

 - maps__for_each_entry should always be called with either read or write lock,
am I right? It looks like this is not done in certain parts of the code. If such

Right.

Quoted 3 lines

lock is taken, then grabbing the refcount on the looping variable is not needed
unless we need to return it, right?

Right, returning an entry needs to take a refcount.

Quoted 3 lines

 - maps__first and map__next do not grab a refcount and neither a lock. If they're used through a lock-protected loop, it's not a problem, but maybe it's

yes

Quoted 3 lines

worth making explicit that they are not to be used directly (through either a
comment or adding some underscores in their names).

yes, __ in front means, in kernel style, that it does less than the non __ prefixed, same name, function.

Quoted 1 line

 - maps__empty: should probably take a reader lock.

Indeed.

Quoted 7 lines

 - maps__find_symbol: the returned symbol is not protected (the caller does not
receive a refcount to neither map or dso, so if dso is deleted, his reference to
the symbol gets invalidated). Depending on how it's being used it might not be a
problem, but in the general scenario I think it's not thread-safe.

Quoted 1 line

Yes, that function is also problematic.

This issue is easier to solve than expected since the map is returned as **mapp, so it's just a matter of making sure that the caller always passes it and then puts the refcnt.

Thanks,
Riccardo

Quoted 5 lines

Thanks for looking into this, please consider sending patches for these issues,

- Arnaldo

#8 lore raw

Em Fri, Jun 18, 2021 at 12:01:28PM +0200, Riccardo Mancini escreveu:

Quoted 27 lines

Hi,

On Fri, 2021-06-04 at 15:29 -0300, Arnaldo Carvalho de Melo wrote:
<SNIP>

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines

But looking at this code now I realize that maps__find() should grab a refcount for the map it returns, because in this machine__process_ksymbol_register() function we use reference that 'map' after the if block, i.e. we use it if it came from maps__find() or if we created it machine__process_ksymbol_register, so there is a possible race where other thread removes it from the list and map__put()s it ending up in map__delete() while we still use it in machine__process_ksymbol_register(), right?

Agree. It should be placed before up_read to avoid races, right?

Quoted 8 lines

Yes, we have to grab a refcount while we are sure its not going away, then return that as the lookup result, whoever receives that refcounted entry should use it and then drop the refcount.

Quoted 2 lines

Then we would need to see where it's called and add the appropriate map__put.

yes

This function has quite a number of callers (direct and indirect) so the the patch is becoming huge.

Quoted 11 lines

One of these callers is thread__find_map, which returns an addr_location (actually it's an output argument). This addr_location holds references to map, maps and thread without getting any refcnt (actually in one function it gets it on the thread and a comment tells to put it once done). If I'm not wrong, this addr_location is never malloced (always a local variable) and, is should be present in parts of the code where there should be a refcnt on the thread. Therefore, maybe it does not get the refcnts since it assumes that thread (upon which depends maps and as a consequence map) is always refcnted in its context. However, I think that it should get all refcnts anyways for clarity and to prevent possible misuses (if I understood correctly, Ian is of the same opinion).

agreed, but this will incur extra costs, we should perhaos use perf to measure how much it costs. :-)

Quoted 3 lines

My solution would be to add the refcnt grabbing for map, maps and thread in thread__find_map, releasing them in addr_location__put, and then making sure all callers call it when no longer in use.

Ok

Quoted 8 lines

Following the same reasoning, I added refcnt grabbing also to mem_info, branch_info (map was already refcnted, I added it also to maps for coherency), map_symbol (as in branch_info, I added it to maps), and in other places in which I saw a pointer was passed without refcounting.

Most changes are quite trivial, however, the changelog is huge:

48 files changed, 472 insertions(+), 157 deletions(-)

Most of them are just returns converted to goto for calling the __put functions.

So you could first do a prep patch converting functions to have gotos, which would be a no-logic change, and then do the rest?

Quoted 3 lines

Doing so, I managed to remove memory leaks caused by refcounting also in perf- report (I wanted to try also perf top but I encountered another memory-related issue). However, the changelog is huge and testing all of it is challenging

So we should break it in as many small steps as possible, knowing that each step is fixing just one of the problems, i.e. aSAN will continue reporting problems, but less problems as you go on adding more fixes.

Quoted 4 lines

(especially since I can test missing puts only with ASan's LeakSanitizer and its reports are usually full of leaks, which I am trying to fix along the way, I will send some patches in the following days). How would you go about it? Do you have any suggestions?

See above, thanks for working on this!

- Arnaldo