• Adrian Hunter's avatar
    perf header: Fix segfault on build_mem_topology() error path · 70df0783
    Adrian Hunter authored
    Do not increase the node count unless a node has been successfully read,
    because it can lead to a segfault if an error occurs.
    
    For example, if perf exceeds the open file limit in memory_node__read(),
    which, on a test system, could be made to happen by setting the file limit
    to exactly 32:
    
     Before:
    
      $ ulimit -n 32
      $ perf mem record --all-user -- sleep 1
      [ perf record: Woken up 1 times to write data ]
      failed: can't open memory sysfs data
      perf: Segmentation fault
      Obtained 14 stack frames.
      perf(sighandler_dump_stack+0x48) [0x55f4b1f59558]
      /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f4ba1c42520]
      /lib/x86_64-linux-gnu/libc.so.6(free+0x1e) [0x7f4ba1ca53fe]
      perf(+0x178ff4) [0x55f4b1f48ff4]
      perf(+0x179a70) [0x55f4b1f49a70]
      perf(+0x17ef5d) [0x55f4b1f4ef5d]
      perf(+0x85c0b) [0x55f4b1e55c0b]
      perf(cmd_record+0xe1d) [0x55f4b1e5920d]
      perf(cmd_mem+0xc96) [0x55f4b1e80e56]
      perf(+0x130460) [0x55f4b1f00460]
      perf(main+0x689) [0x55f4b1e427d9]
      /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f4ba1c29d90]
      /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7f4ba1c29e40]
      perf(_start+0x25) [0x55f4b1e42a25]
      Segmentation fault (core dumped)
      $
    
    After:
    
      $ ulimit -n 32
      $ perf mem record --all-user -- sleep 1
      [ perf record: Woken up 1 times to write data ]
      failed: can't open memory sysfs data
      [ perf record: Captured and wrote 0.005 MB perf.data (11 samples) ]
      $
    
    Fixes: f8e502b9 ("perf header: Ensure bitmaps are freed")
    Signed-off-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
    Acked-by: default avatarIan Rogers <irogers@google.com>
    Cc: German Gomez <german.gomez@arm.com>
    Cc: Ian Rogers <irogers@google.com>
    Cc: James Clark <james.clark@arm.com>
    Cc: Jiri Olsa <jolsa@kernel.org>
    Cc: Leo Yan <leo.yan@linaro.org>
    Cc: Namhyung Kim <namhyung@kernel.org>
    Link: https://lore.kernel.org/r/20231123075848.9652-2-adrian.hunter@intel.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
    70df0783
header.c 97.9 KB