Merge tag 'perf-urgent-2020-04-05' of git://git.kernel.org/pub/scm/linux/kernel/git...
authorLinus Torvalds <torvalds@linux-foundation.org>
Sun, 5 Apr 2020 19:26:24 +0000 (12:26 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 5 Apr 2020 19:26:24 +0000 (12:26 -0700)
Pull more perf updates from Thomas Gleixner:
 "Perf updates all over the place:

  core:

   - Support for cgroup tracking in samples to allow cgroup based
     analysis

  tools:

   - Support for cgroup analysis

   - Commandline option and hotkey for perf top to change the sort order

   - A set of fixes all over the place

   - Various build system related improvements

   - Updates of the X86 pmu event JSON data

   - Documentation updates"

* tag 'perf-urgent-2020-04-05' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (55 commits)
  perf python: Fix clang detection to strip out options passed in $CC
  perf tools: Support Python 3.8+ in Makefile
  perf script: Fix invalid read of directory entry after closedir()
  perf script report: Fix SEGFAULT when using DWARF mode
  perf script: add -S/--symbols documentation
  perf pmu-events x86: Use CPU_CLK_UNHALTED.THREAD in Kernel_Utilization metric
  perf events parser: Add missing Intel CPU events to parser
  perf script: Allow --symbol to accept hexadecimal addresses
  perf report/top TUI: Fix title line formatting
  perf top: Support hotkey to change sort order
  perf top: Support --group-sort-idx to change the sort order
  perf symbols: Fix arm64 gap between kernel start and module end
  perf build-test: Honour JOBS to override detection of number of cores
  perf script: Add --show-cgroup-events option
  perf top: Add --all-cgroups option
  perf record: Add --all-cgroups option
  perf record: Support synthesizing cgroup events
  perf report: Add 'cgroup' sort key
  perf cgroup: Maintain cgroup hierarchy
  perf tools: Basic support for CGROUP event
  ...

1  2 
init/Kconfig
kernel/events/core.c
tools/build/feature/Makefile

diff --combined init/Kconfig
index f095ec64bb91765f1881f3584870e06b82600816,93e1fafa88ceb4bbe1ad0b31694c9f567ea1ccc4..1c12059e0f7e6b643f10176b5389c83aa9330c51
@@@ -6,7 -6,8 +6,7 @@@ config DEFCONFIG_LIS
        default "/lib/modules/$(shell,uname -r)/.config"
        default "/etc/kernel-config"
        default "/boot/config-$(shell,uname -r)"
 -      default ARCH_DEFCONFIG
 -      default "arch/$(ARCH)/defconfig"
 +      default "arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG)"
  
  config CC_IS_GCC
        def_bool $(success,$(CC) --version | head -n 1 | grep -q gcc)
@@@ -1029,7 -1030,8 +1029,8 @@@ config CGROUP_PER
        help
          This option extends the perf per-cpu mode to restrict monitoring
          to threads which belong to the cgroup specified and run on the
-         designated cpu.
+         designated cpu.  Or this can be used to have cgroup ID in samples
+         so that it can monitor performance events among cgroups.
  
          Say N if unsure.
  
@@@ -1618,19 -1620,6 +1619,19 @@@ config KALLSYMS_BASE_RELATIV
  # end of the "standard kernel features (expert users)" menu
  
  # syscall, maps, verifier
 +
 +config BPF_LSM
 +      bool "LSM Instrumentation with BPF"
 +      depends on BPF_EVENTS
 +      depends on BPF_SYSCALL
 +      depends on SECURITY
 +      depends on BPF_JIT
 +      help
 +        Enables instrumentation of the security hooks with eBPF programs for
 +        implementing dynamic MAC and Audit Policies.
 +
 +        If you are unsure how to answer this question, answer N.
 +
  config BPF_SYSCALL
        bool "Enable bpf() system call"
        select BPF
@@@ -2223,19 -2212,6 +2224,19 @@@ config TRIM_UNUSED_KSYM
  
          If unsure, or if you need to build out-of-tree modules, say N.
  
 +config UNUSED_KSYMS_WHITELIST
 +      string "Whitelist of symbols to keep in ksymtab"
 +      depends on TRIM_UNUSED_KSYMS
 +      help
 +        By default, all unused exported symbols will be un-exported from the
 +        build when TRIM_UNUSED_KSYMS is selected.
 +
 +        UNUSED_KSYMS_WHITELIST allows to whitelist symbols that must be kept
 +        exported at all times, even in absence of in-tree users. The value to
 +        set here is the path to a text file containing the list of symbols,
 +        one per line. The path can be absolute, or relative to the kernel
 +        source tree.
 +
  endif # MODULES
  
  config MODULES_TREE_LOOKUP
diff --combined kernel/events/core.c
index e1459df730438f5b5525c801800d16c247219b2e,1569979c891229743c6e55ec1e426b18f2e324d0..81e6d80cb219efe7f6398100a40d5918ca398730
@@@ -387,6 -387,7 +387,7 @@@ static atomic_t nr_freq_events __read_m
  static atomic_t nr_switch_events __read_mostly;
  static atomic_t nr_ksymbol_events __read_mostly;
  static atomic_t nr_bpf_events __read_mostly;
+ static atomic_t nr_cgroup_events __read_mostly;
  
  static LIST_HEAD(pmus);
  static DEFINE_MUTEX(pmus_lock);
@@@ -1295,7 -1296,7 +1296,7 @@@ static void put_ctx(struct perf_event_c
   * function.
   *
   * Lock order:
 - *    cred_guard_mutex
 + *    exec_update_mutex
   *    task_struct::perf_event_mutex
   *      perf_event_context::mutex
   *        perf_event::child_mutex;
@@@ -1861,6 -1862,9 +1862,9 @@@ static void __perf_event_header_size(st
        if (sample_type & PERF_SAMPLE_PHYS_ADDR)
                size += sizeof(data->phys_addr);
  
+       if (sample_type & PERF_SAMPLE_CGROUP)
+               size += sizeof(data->cgroup);
        event->header_size = size;
  }
  
@@@ -4608,6 -4612,8 +4612,8 @@@ static void unaccount_event(struct perf
                atomic_dec(&nr_comm_events);
        if (event->attr.namespaces)
                atomic_dec(&nr_namespaces_events);
+       if (event->attr.cgroup)
+               atomic_dec(&nr_cgroup_events);
        if (event->attr.task)
                atomic_dec(&nr_task_events);
        if (event->attr.freq)
@@@ -6864,6 -6870,9 +6870,9 @@@ void perf_output_sample(struct perf_out
        if (sample_type & PERF_SAMPLE_PHYS_ADDR)
                perf_output_put(handle, data->phys_addr);
  
+       if (sample_type & PERF_SAMPLE_CGROUP)
+               perf_output_put(handle, data->cgroup);
        if (sample_type & PERF_SAMPLE_AUX) {
                perf_output_put(handle, data->aux_size);
  
@@@ -7063,6 -7072,16 +7072,16 @@@ void perf_prepare_sample(struct perf_ev
        if (sample_type & PERF_SAMPLE_PHYS_ADDR)
                data->phys_addr = perf_virt_to_phys(data->addr);
  
+ #ifdef CONFIG_CGROUP_PERF
+       if (sample_type & PERF_SAMPLE_CGROUP) {
+               struct cgroup *cgrp;
+               /* protected by RCU */
+               cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup;
+               data->cgroup = cgroup_id(cgrp);
+       }
+ #endif
        if (sample_type & PERF_SAMPLE_AUX) {
                u64 size;
  
@@@ -7735,6 -7754,105 +7754,105 @@@ void perf_event_namespaces(struct task_
                        NULL);
  }
  
+ /*
+  * cgroup tracking
+  */
+ #ifdef CONFIG_CGROUP_PERF
+ struct perf_cgroup_event {
+       char                            *path;
+       int                             path_size;
+       struct {
+               struct perf_event_header        header;
+               u64                             id;
+               char                            path[];
+       } event_id;
+ };
+ static int perf_event_cgroup_match(struct perf_event *event)
+ {
+       return event->attr.cgroup;
+ }
+ static void perf_event_cgroup_output(struct perf_event *event, void *data)
+ {
+       struct perf_cgroup_event *cgroup_event = data;
+       struct perf_output_handle handle;
+       struct perf_sample_data sample;
+       u16 header_size = cgroup_event->event_id.header.size;
+       int ret;
+       if (!perf_event_cgroup_match(event))
+               return;
+       perf_event_header__init_id(&cgroup_event->event_id.header,
+                                  &sample, event);
+       ret = perf_output_begin(&handle, event,
+                               cgroup_event->event_id.header.size);
+       if (ret)
+               goto out;
+       perf_output_put(&handle, cgroup_event->event_id);
+       __output_copy(&handle, cgroup_event->path, cgroup_event->path_size);
+       perf_event__output_id_sample(event, &handle, &sample);
+       perf_output_end(&handle);
+ out:
+       cgroup_event->event_id.header.size = header_size;
+ }
+ static void perf_event_cgroup(struct cgroup *cgrp)
+ {
+       struct perf_cgroup_event cgroup_event;
+       char path_enomem[16] = "//enomem";
+       char *pathname;
+       size_t size;
+       if (!atomic_read(&nr_cgroup_events))
+               return;
+       cgroup_event = (struct perf_cgroup_event){
+               .event_id  = {
+                       .header = {
+                               .type = PERF_RECORD_CGROUP,
+                               .misc = 0,
+                               .size = sizeof(cgroup_event.event_id),
+                       },
+                       .id = cgroup_id(cgrp),
+               },
+       };
+       pathname = kmalloc(PATH_MAX, GFP_KERNEL);
+       if (pathname == NULL) {
+               cgroup_event.path = path_enomem;
+       } else {
+               /* just to be sure to have enough space for alignment */
+               cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64));
+               cgroup_event.path = pathname;
+       }
+       /*
+        * Since our buffer works in 8 byte units we need to align our string
+        * size to a multiple of 8. However, we must guarantee the tail end is
+        * zero'd out to avoid leaking random bits to userspace.
+        */
+       size = strlen(cgroup_event.path) + 1;
+       while (!IS_ALIGNED(size, sizeof(u64)))
+               cgroup_event.path[size++] = '\0';
+       cgroup_event.event_id.header.size += size;
+       cgroup_event.path_size = size;
+       perf_iterate_sb(perf_event_cgroup_output,
+                       &cgroup_event,
+                       NULL);
+       kfree(pathname);
+ }
+ #endif
  /*
   * mmap tracking
   */
@@@ -8417,22 -8535,23 +8535,22 @@@ static void perf_event_bpf_emit_ksymbol
                                         enum perf_bpf_event_type type)
  {
        bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
 -      char sym[KSYM_NAME_LEN];
        int i;
  
        if (prog->aux->func_cnt == 0) {
 -              bpf_get_prog_name(prog, sym);
                perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
                                   (u64)(unsigned long)prog->bpf_func,
 -                                 prog->jited_len, unregister, sym);
 +                                 prog->jited_len, unregister,
 +                                 prog->aux->ksym.name);
        } else {
                for (i = 0; i < prog->aux->func_cnt; i++) {
                        struct bpf_prog *subprog = prog->aux->func[i];
  
 -                      bpf_get_prog_name(subprog, sym);
                        perf_event_ksymbol(
                                PERF_RECORD_KSYMBOL_TYPE_BPF,
                                (u64)(unsigned long)subprog->bpf_func,
 -                              subprog->jited_len, unregister, sym);
 +                              subprog->jited_len, unregister,
 +                              prog->aux->ksym.name);
                }
        }
  }
@@@ -9367,6 -9486,7 +9485,6 @@@ static void bpf_overflow_handler(struc
        int ret = 0;
  
        ctx.regs = perf_arch_bpf_user_pt_regs(regs);
 -      preempt_disable();
        if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
                goto out;
        rcu_read_lock();
        rcu_read_unlock();
  out:
        __this_cpu_dec(bpf_prog_active);
 -      preempt_enable();
        if (!ret)
                return;
  
@@@ -10778,6 -10899,8 +10896,8 @@@ static void account_event(struct perf_e
                atomic_inc(&nr_comm_events);
        if (event->attr.namespaces)
                atomic_inc(&nr_namespaces_events);
+       if (event->attr.cgroup)
+               atomic_inc(&nr_cgroup_events);
        if (event->attr.task)
                atomic_inc(&nr_task_events);
        if (event->attr.freq)
@@@ -11157,6 -11280,12 +11277,12 @@@ static int perf_copy_attr(struct perf_e
  
        if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
                ret = perf_reg_validate(attr->sample_regs_intr);
+ #ifndef CONFIG_CGROUP_PERF
+       if (attr->sample_type & PERF_SAMPLE_CGROUP)
+               return -EINVAL;
+ #endif
  out:
        return ret;
  
@@@ -11425,14 -11554,14 +11551,14 @@@ SYSCALL_DEFINE5(perf_event_open
        }
  
        if (task) {
 -              err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
 +              err = mutex_lock_interruptible(&task->signal->exec_update_mutex);
                if (err)
                        goto err_task;
  
                /*
                 * Reuse ptrace permission checks for now.
                 *
 -               * We must hold cred_guard_mutex across this and any potential
 +               * We must hold exec_update_mutex across this and any potential
                 * perf_install_in_context() call for this new event to
                 * serialize against exec() altering our credentials (and the
                 * perf_event_exit_task() that could imply).
        mutex_unlock(&ctx->mutex);
  
        if (task) {
 -              mutex_unlock(&task->signal->cred_guard_mutex);
 +              mutex_unlock(&task->signal->exec_update_mutex);
                put_task_struct(task);
        }
  
@@@ -11757,7 -11886,7 +11883,7 @@@ err_alloc
                free_event(event);
  err_cred:
        if (task)
 -              mutex_unlock(&task->signal->cred_guard_mutex);
 +              mutex_unlock(&task->signal->exec_update_mutex);
  err_task:
        if (task)
                put_task_struct(task);
@@@ -12062,7 -12191,7 +12188,7 @@@ static void perf_event_exit_task_contex
  /*
   * When a child task exits, feed back event values to parent events.
   *
 - * Can be called with cred_guard_mutex held when called from
 + * Can be called with exec_update_mutex held when called from
   * install_exec_creds().
   */
  void perf_event_exit_task(struct task_struct *child)
@@@ -12754,6 -12883,12 +12880,12 @@@ static void perf_cgroup_css_free(struc
        kfree(jc);
  }
  
+ static int perf_cgroup_css_online(struct cgroup_subsys_state *css)
+ {
+       perf_event_cgroup(css->cgroup);
+       return 0;
+ }
  static int __perf_cgroup_move(void *info)
  {
        struct task_struct *task = info;
@@@ -12775,6 -12910,7 +12907,7 @@@ static void perf_cgroup_attach(struct c
  struct cgroup_subsys perf_event_cgrp_subsys = {
        .css_alloc      = perf_cgroup_css_alloc,
        .css_free       = perf_cgroup_css_free,
+       .css_online     = perf_cgroup_css_online,
        .attach         = perf_cgroup_attach,
        /*
         * Implicitly enable on dfl hierarchy so that perf events can
index ab8e89a7009c78c10a54f974de77b2136e670f0c,621f528f782232a1068a409c53d119c6c254ef92..92012381393ad97e72c4eec27f56c691edec7bac
@@@ -68,7 -68,7 +68,8 @@@ FILES
           test-llvm-version.bin                        \
           test-libaio.bin                      \
           test-libzstd.bin                     \
-          test-clang-bpf-global-var.bin
++         test-clang-bpf-global-var.bin                \
+          test-file-handle.bin
  
  FILES := $(addprefix $(OUTPUT),$(FILES))
  
@@@ -76,7 -76,6 +77,7 @@@ CC ?= $(CROSS_COMPILE)gc
  CXX ?= $(CROSS_COMPILE)g++
  PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
  LLVM_CONFIG ?= llvm-config
 +CLANG ?= clang
  
  all: $(FILES)
  
@@@ -323,10 -322,8 +324,12 @@@ $(OUTPUT)test-libaio.bin
  $(OUTPUT)test-libzstd.bin:
        $(BUILD) -lzstd
  
 +$(OUTPUT)test-clang-bpf-global-var.bin:
 +      $(CLANG) -S -g -target bpf -o - $(patsubst %.bin,%.c,$(@F)) |   \
 +              grep BTF_KIND_VAR
 +
+ $(OUTPUT)test-file-handle.bin:
+       $(BUILD)
  
  ###############################