perf tools: Fix pyrf_evlist__read_on_cpu interface
Jiri Olsa
reviewercover perf tools: Fix pyrf_evlist__read_on_cpu interface
hi,
Jaroslav reported errors from valgrind over perf python script, which led us to discover serious issue with python interface.
It affects tuned daemon, which depends on this interface, and makes it hang.
thanks,
jirka
---
Jiri Olsa (2):
perf tools: Store real cpu number in the struct perf_mmap perf python: Fix pyrf_evlist__read_on_cpu interface
tools/perf/util/evlist.c | 2 +- tools/perf/util/mmap.c | 4 +++- tools/perf/util/mmap.h | 4 +++- tools/perf/util/python.c | 20 +++++++++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-)
Jiri Olsa
reviewerPATCH 1/2 perf tools: Store real cpu number in the struct perf_mmap
Storing the real cpu number in the struct perf_mmap, which be used by python interface that allows user to read particular memory map for given cpu.
--- tools/perf/util/evlist.c | 2 +- tools/perf/util/mmap.c | 4 +++- tools/perf/util/mmap.h | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/evlist.c +1 -1
@@ -803,7 +803,7 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,if (*output ==-1 ) { *output = fd; -if (perf_mmap__mmap (&maps[idx], mp, *output) <0 ) +if (perf_mmap__mmap (&maps[idx], mp, *output, evlist_cpu) <0 )return -1 ; }else {if (ioctl (fd,PERF_EVENT_IOC_SET_OUTPUT , *output) !=0 )
tools/perf/util/mmap.c +3 -1
@@ -164,7 +164,8 @@ void perf_mmap__munmap(struct perf_mmap *map)auxtrace_mmap__munmap (&map->auxtrace_mmap); } -int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd) +int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp, +int fd,int cpu) {/* * The last one will be done at perf_mmap__consume(), so that we
@@ -191,6 +192,7 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd)return -1 ; } map->fd = fd; + map->cpu = cpu;if (auxtrace_mmap__mmap (&map->auxtrace_mmap, &mp->auxtrace_mp, map->base, fd))
tools/perf/util/mmap.h +3 -1
@@ -18,6 +18,7 @@ struct perf_mmap {void *base;int mask;int fd; +int cpu;refcount_t refcnt;u64 prev;u64 start;
@@ -60,7 +61,8 @@ struct mmap_params {struct auxtrace_mmap_params auxtrace_mp; }; -int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd); +int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp, +int fd,int cpu);void perf_mmap__munmap (struct perf_mmap *map);void perf_mmap__get (struct perf_mmap *map);
Signature · 1 line
2.17.1
tip-bot for Jiri Olsa
merge bot[tip:perf/urgent] perf mmap: Store real cpu number in 'struct perf_mmap'
perf mmap: Store real cpu number in 'struct perf_mmap'
Commit-ID: 31fb4c0d7b88f036edb96a6a3bd791289ea2f931 Gitweb: https://git.kernel.org/tip/31fb4c0d7b88f036edb96a6a3bd791289ea2f931 Author: Jiri Olsa <[email protected]> AuthorDate: Fri, 17 Aug 2018 13:45:55 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 20 Aug 2018 08:54:59 -0300
perf mmap: Store real cpu number in 'struct perf_mmap'
Store the real cpu number in 'struct perf_mmap', which will be used by python interface that allows user to read a particular memory map for given cpu.
--- tools/perf/util/evlist.c | 2 +- tools/perf/util/mmap.c | 3 ++- tools/perf/util/mmap.h | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/evlist.c +1 -1
@@ -803,7 +803,7 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,if (*output ==-1 ) { *output = fd; -if (perf_mmap__mmap (&maps[idx], mp, *output) <0 ) +if (perf_mmap__mmap (&maps[idx], mp, *output, evlist_cpu) <0 )return -1 ; }else {if (ioctl (fd,PERF_EVENT_IOC_SET_OUTPUT , *output) !=0 )
tools/perf/util/mmap.c +2 -1
@@ -164,7 +164,7 @@ void perf_mmap__munmap(struct perf_mmap *map)auxtrace_mmap__munmap (&map->auxtrace_mmap); } -int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd) +int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd,int cpu) {/* * The last one will be done at perf_mmap__consume(), so that we
@@ -191,6 +191,7 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd)return -1 ; } map->fd = fd; + map->cpu = cpu;if (auxtrace_mmap__mmap (&map->auxtrace_mmap, &mp->auxtrace_mp, map->base, fd))
tools/perf/util/mmap.h +2 -1
@@ -18,6 +18,7 @@ struct perf_mmap {void *base;int mask;int fd; +int cpu;refcount_t refcnt;u64 prev;u64 start;
@@ -60,7 +61,7 @@ struct mmap_params {struct auxtrace_mmap_params auxtrace_mp; }; -int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd); +int perf_mmap__mmap (struct perf_mmap *map,struct mmap_params *mp,int fd,int cpu);void perf_mmap__munmap (struct perf_mmap *map);void perf_mmap__get (struct perf_mmap *map);
Jiri Olsa
reviewerPATCH 2/2 perf python: Fix pyrf_evlist__read_on_cpu interface
In reply to Jiri OlsaJaroslav reported errors from valgrind over perf python script:
# echo 0 > /sys/devices/system/cpu/cpu4/online # valgrind ./test.py ==7524== Memcheck, a memory error detector ... ==7524== Command: ./test.py ==7524== pid 7526 exited ==7524== Invalid read of size 8 ==7524== at 0xCC2C2B3: perf_mmap__read_forward (evlist.c:780) ==7524== by 0xCC2A681: pyrf_evlist__read_on_cpu (python.c:959) ... ==7524== Address 0x65c4868 is 16 bytes after a block of size 459,36.. ==7524== at 0x4C2B955: calloc (vg_replace_malloc.c:711) ==7524== by 0xCC2F484: zalloc (util.h:35) ==7524== by 0xCC2F484: perf_evlist__alloc_mmap (evlist.c:978) ...
The reason for this is in the python interface, that allows script to pass arbitrary cpu number, which is then used to access struct perf_evlist::mmap array. That's obviously wrong and works only when if all cpus are available and fails if some cpu is missing, like in the example above.
This patch makes the pyrf_evlist__read_on_cpu search the evlist's maps array for the proper map to access.
It's linear search at the moment. Based on the way how is the read_on_cpu used, I don't think we need to be fast in here. But we could add some hash in the middle to make it fast/er.
We don't allow python interface to set write_backward event attribute, so it's safe to check only evlist's mmaps.
--- tools/perf/util/python.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
tools/perf/util/python.c +19 -1
@@ -11,6 +11,7 @@#include "cpumap.h" #include "print_binary.h" #include "thread_map.h" +#include "mmap.h" #if PY_MAJOR_VERSION <3 #define _PyUnicode_FromString (arg) \
@@ -976,6 +977,20 @@ static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,return Py_BuildValue ("i" , evlist->nr_entries); } +static struct perf_mmap *get_md (struct perf_evlist *evlist,int cpu) +{ +int i; + +for (i =0 ; i < evlist->nr_mmaps; i++) { +struct perf_mmap *md = &evlist->mmap[i]; + +if (md->cpu == cpu) +return md; + } + +return NULL ; +} +static PyObject *pyrf_evlist__read_on_cpu (struct pyrf_evlist *pevlist,PyObject *args,PyObject *kwargs) {
@@ -990,7 +1005,10 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, &cpu, &sample_id_all))return NULL ; - md = &evlist->mmap[cpu]; + md =get_md (evlist, cpu); +if (!md) +return NULL ; +if (perf_mmap__read_init (md) <0 ) goto end;
Signature · 1 line
2.17.1
tip-bot for Jiri Olsa
merge bot[tip:perf/urgent] perf python: Fix pyrf_evlist__read_on_cpu() interface
perf python: Fix pyrf_evlist__read_on_cpu() interface
Commit-ID: 721f0dfc3ce821c6a32820ab63edfb48ed4af075 Gitweb: https://git.kernel.org/tip/721f0dfc3ce821c6a32820ab63edfb48ed4af075 Author: Jiri Olsa <[email protected]> AuthorDate: Fri, 17 Aug 2018 13:45:56 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 20 Aug 2018 08:54:59 -0300
perf python: Fix pyrf_evlist__read_on_cpu() interface
Jaroslav reported errors from valgrind over perf python script:
# echo 0 > /sys/devices/system/cpu/cpu4/online # valgrind ./test.py ==7524== Memcheck, a memory error detector ... ==7524== Command: ./test.py ==7524== pid 7526 exited ==7524== Invalid read of size 8 ==7524== at 0xCC2C2B3: perf_mmap__read_forward (evlist.c:780) ==7524== by 0xCC2A681: pyrf_evlist__read_on_cpu (python.c:959) ... ==7524== Address 0x65c4868 is 16 bytes after a block of size 459,36.. ==7524== at 0x4C2B955: calloc (vg_replace_malloc.c:711) ==7524== by 0xCC2F484: zalloc (util.h:35) ==7524== by 0xCC2F484: perf_evlist__alloc_mmap (evlist.c:978) ...
The reason for this is in the python interface, that allows a script to pass arbitrary cpu number, which is then used to access struct perf_evlist::mmap array. That's obviously wrong and works only when if all cpus are available and fails if some cpu is missing, like in the example above.
This patch makes pyrf_evlist__read_on_cpu() search the evlist's maps array for the proper map to access.
It's linear search at the moment. Based on the way how is the read_on_cpu used, I don't think we need to be fast in here. But we could add some hash in the middle to make it fast/er.
We don't allow python interface to set write_backward event attribute, so it's safe to check only evlist's mmaps.
--- tools/perf/util/python.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
tools/perf/util/python.c +19 -1
@@ -11,6 +11,7 @@#include "cpumap.h" #include "print_binary.h" #include "thread_map.h" +#include "mmap.h" #if PY_MAJOR_VERSION <3 #define _PyUnicode_FromString (arg) \
@@ -976,6 +977,20 @@ static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,return Py_BuildValue ("i" , evlist->nr_entries); } +static struct perf_mmap *get_md (struct perf_evlist *evlist,int cpu) +{ +int i; + +for (i =0 ; i < evlist->nr_mmaps; i++) { +struct perf_mmap *md = &evlist->mmap[i]; + +if (md->cpu == cpu) +return md; + } + +return NULL ; +} +static PyObject *pyrf_evlist__read_on_cpu (struct pyrf_evlist *pevlist,PyObject *args,PyObject *kwargs) {
@@ -990,7 +1005,10 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, &cpu, &sample_id_all))return NULL ; - md = &evlist->mmap[cpu]; + md =get_md (evlist, cpu); +if (!md) +return NULL ; +if (perf_mmap__read_init (md) <0 ) goto end;
Em Fri, Aug 17, 2018 at 01:45:54PM +0200, Jiri Olsa escreveu:
Quoted 6 lines
hi,
Jaroslav reported errors from valgrind over perf python script, which led us to discover serious issue with python interface.It affects tuned daemon, which depends on this interface, and makes it hang.
Thanks, applied.
- Arnaldo