1. 29 Sep, 2022 12 commits
  2. 27 Sep, 2022 4 commits
    • Jiri Olsa's avatar
      bpf: Check flags for branch stack in bpf_read_branch_records helper · cce6a2d7
      Jiri Olsa authored
      Recent commit [1] changed branch stack data indication from
      br_stack pointer to sample_flags in perf_sample_data struct.
      
      We need to check sample_flags for PERF_SAMPLE_BRANCH_STACK
      bit for valid branch stack data.
      
      [1] a9a931e2 ("perf: Use sample_flags for branch stack")
      
      Fixes: a9a931e2 ("perf: Use sample_flags for branch stack")
      Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Reviewed-by: default avatarKan Liang <kan.liang@linux.intel.com>
      Link: https://lore.kernel.org/r/20220927203259.590950-1-jolsa@kernel.org
      cce6a2d7
    • Marco Elver's avatar
      perf, hw_breakpoint: Fix use-after-free if perf_event_open() fails · 4674ffe2
      Marco Elver authored
      Local testing revealed that we can trigger a use-after-free during
      rhashtable lookup as follows:
      
       | BUG: KASAN: use-after-free in memcmp lib/string.c:757
       | Read of size 8 at addr ffff888107544dc0 by task perf-rhltable-n/1293
       |
       | CPU: 0 PID: 1293 Comm: perf-rhltable-n Not tainted 6.0.0-rc3-00014-g85260862789c #46
       | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.0-debian-1.16.0-4 04/01/2014
       | Call Trace:
       |  <TASK>
       |  memcmp			lib/string.c:757
       |  rhashtable_compare		include/linux/rhashtable.h:577 [inline]
       |  __rhashtable_lookup		include/linux/rhashtable.h:602 [inline]
       |  rhltable_lookup		include/linux/rhashtable.h:688 [inline]
       |  task_bp_pinned		kernel/events/hw_breakpoint.c:324
       |  toggle_bp_slot		kernel/events/hw_breakpoint.c:462
       |  __release_bp_slot		kernel/events/hw_breakpoint.c:631 [inline]
       |  release_bp_slot		kernel/events/hw_breakpoint.c:639
       |  register_perf_hw_breakpoint	kernel/events/hw_breakpoint.c:742
       |  hw_breakpoint_event_init	kernel/events/hw_breakpoint.c:976
       |  perf_try_init_event		kernel/events/core.c:11261
       |  perf_init_event		kernel/events/core.c:11325 [inline]
       |  perf_event_alloc		kernel/events/core.c:11619
       |  __do_sys_perf_event_open	kernel/events/core.c:12157
       |  do_syscall_x64 		arch/x86/entry/common.c:50 [inline]
       |  do_syscall_64		arch/x86/entry/common.c:80
       |  entry_SYSCALL_64_after_hwframe
       |  </TASK>
       |
       | Allocated by task 1292:
       |  perf_event_alloc		kernel/events/core.c:11505
       |  __do_sys_perf_event_open	kernel/events/core.c:12157
       |  do_syscall_x64		arch/x86/entry/common.c:50 [inline]
       |  do_syscall_64		arch/x86/entry/common.c:80
       |  entry_SYSCALL_64_after_hwframe
       |
       | Freed by task 1292:
       |  perf_event_alloc		kernel/events/core.c:11716
       |  __do_sys_perf_event_open	kernel/events/core.c:12157
       |  do_syscall_x64		arch/x86/entry/common.c:50 [inline]
       |  do_syscall_64		arch/x86/entry/common.c:80
       |  entry_SYSCALL_64_after_hwframe
       |
       | The buggy address belongs to the object at ffff888107544c00
       |  which belongs to the cache perf_event of size 1352
       | The buggy address is located 448 bytes inside of
       |  1352-byte region [ffff888107544c00, ffff888107545148)
      
      This happens because the first perf_event_open() managed to reserve a HW
      breakpoint slot, however, later fails for other reasons and returns. The
      second perf_event_open() runs concurrently, and during rhltable_lookup()
      looks up an entry which is being freed: since rhltable_lookup() may run
      concurrently (under the RCU read lock) with rhltable_remove(), we may
      end up with a stale entry, for which memory may also have already been
      freed when being accessed.
      
      To fix, only free the failed perf_event after an RCU grace period. This
      allows subsystems that store references to an event to always access it
      concurrently under the RCU read lock, even if initialization will fail.
      
      Given failure is unlikely and a slow-path, turning the immediate free
      into a call_rcu()-wrapped free does not affect performance elsewhere.
      
      Fixes: 0370dc31 ("perf/hw_breakpoint: Optimize list of per-task breakpoints")
      Reported-by: default avatarsyzkaller <syzkaller@googlegroups.com>
      Signed-off-by: default avatarMarco Elver <elver@google.com>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Link: https://lkml.kernel.org/r/20220927172025.1636995-1-elver@google.com
      4674ffe2
    • Namhyung Kim's avatar
      perf: Use sample_flags for raw_data · 838d9bb6
      Namhyung Kim authored
      Use the new sample_flags to indicate whether the raw data field is
      filled by the PMU driver.  Although it could check with the NULL,
      follow the same rule with other fields.
      
      Remove the raw field from the perf_sample_data_init() to minimize
      the number of cache lines touched.
      Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Link: https://lkml.kernel.org/r/20220921220032.2858517-2-namhyung@kernel.org
      838d9bb6
    • Namhyung Kim's avatar
      perf: Use sample_flags for addr · 7b084630
      Namhyung Kim authored
      Use the new sample_flags to indicate whether the addr field is filled by
      the PMU driver.  As most PMU drivers pass 0, it can set the flag only if
      it has a non-zero value.  And use 0 in perf_sample_output() if it's not
      filled already.
      Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
      Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
      Link: https://lkml.kernel.org/r/20220921220032.2858517-1-namhyung@kernel.org
      7b084630
  3. 25 Sep, 2022 8 commits
  4. 24 Sep, 2022 10 commits
  5. 23 Sep, 2022 6 commits