LCOV - code coverage report
Current view: top level - kernel/events - core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 81 2581 3.1 %
Date: 2014-04-16 Functions: 19 234 8.1 %
Branches: 39 1902 2.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Performance events core code:
       3                 :            :  *
       4                 :            :  *  Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
       5                 :            :  *  Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
       6                 :            :  *  Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
       7                 :            :  *  Copyright  ©  2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
       8                 :            :  *
       9                 :            :  * For licensing details see kernel-base/COPYING
      10                 :            :  */
      11                 :            : 
      12                 :            : #include <linux/fs.h>
      13                 :            : #include <linux/mm.h>
      14                 :            : #include <linux/cpu.h>
      15                 :            : #include <linux/smp.h>
      16                 :            : #include <linux/idr.h>
      17                 :            : #include <linux/file.h>
      18                 :            : #include <linux/poll.h>
      19                 :            : #include <linux/slab.h>
      20                 :            : #include <linux/hash.h>
      21                 :            : #include <linux/tick.h>
      22                 :            : #include <linux/sysfs.h>
      23                 :            : #include <linux/dcache.h>
      24                 :            : #include <linux/percpu.h>
      25                 :            : #include <linux/ptrace.h>
      26                 :            : #include <linux/reboot.h>
      27                 :            : #include <linux/vmstat.h>
      28                 :            : #include <linux/device.h>
      29                 :            : #include <linux/export.h>
      30                 :            : #include <linux/vmalloc.h>
      31                 :            : #include <linux/hardirq.h>
      32                 :            : #include <linux/rculist.h>
      33                 :            : #include <linux/uaccess.h>
      34                 :            : #include <linux/syscalls.h>
      35                 :            : #include <linux/anon_inodes.h>
      36                 :            : #include <linux/kernel_stat.h>
      37                 :            : #include <linux/perf_event.h>
      38                 :            : #include <linux/ftrace_event.h>
      39                 :            : #include <linux/hw_breakpoint.h>
      40                 :            : #include <linux/mm_types.h>
      41                 :            : #include <linux/cgroup.h>
      42                 :            : 
      43                 :            : #include "internal.h"
      44                 :            : 
      45                 :            : #include <asm/irq_regs.h>
      46                 :            : 
      47                 :            : struct remote_function_call {
      48                 :            :         struct task_struct      *p;
      49                 :            :         int                     (*func)(void *info);
      50                 :            :         void                    *info;
      51                 :            :         int                     ret;
      52                 :            : };
      53                 :            : 
      54                 :          0 : static void remote_function(void *data)
      55                 :            : {
      56                 :            :         struct remote_function_call *tfc = data;
      57                 :          0 :         struct task_struct *p = tfc->p;
      58                 :            : 
      59         [ #  # ]:          0 :         if (p) {
      60                 :          0 :                 tfc->ret = -EAGAIN;
      61 [ #  # ][ #  # ]:          0 :                 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
      62                 :          0 :                         return;
      63                 :            :         }
      64                 :            : 
      65                 :          0 :         tfc->ret = tfc->func(tfc->info);
      66                 :            : }
      67                 :            : 
      68                 :            : /**
      69                 :            :  * task_function_call - call a function on the cpu on which a task runs
      70                 :            :  * @p:          the task to evaluate
      71                 :            :  * @func:       the function to be called
      72                 :            :  * @info:       the function call argument
      73                 :            :  *
      74                 :            :  * Calls the function @func when the task is currently running. This might
      75                 :            :  * be on the current CPU, which just calls the function directly
      76                 :            :  *
      77                 :            :  * returns: @func return value, or
      78                 :            :  *          -ESRCH  - when the process isn't running
      79                 :            :  *          -EAGAIN - when the process moved away
      80                 :            :  */
      81                 :            : static int
      82                 :          0 : task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
      83                 :            : {
      84                 :          0 :         struct remote_function_call data = {
      85                 :            :                 .p      = p,
      86                 :            :                 .func   = func,
      87                 :            :                 .info   = info,
      88                 :            :                 .ret    = -ESRCH, /* No such (running) process */
      89                 :            :         };
      90                 :            : 
      91         [ #  # ]:          0 :         if (task_curr(p))
      92                 :          0 :                 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
      93                 :            : 
      94                 :          0 :         return data.ret;
      95                 :            : }
      96                 :            : 
      97                 :            : /**
      98                 :            :  * cpu_function_call - call a function on the cpu
      99                 :            :  * @func:       the function to be called
     100                 :            :  * @info:       the function call argument
     101                 :            :  *
     102                 :            :  * Calls the function @func on the remote cpu.
     103                 :            :  *
     104                 :            :  * returns: @func return value or -ENXIO when the cpu is offline
     105                 :            :  */
     106                 :          0 : static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
     107                 :            : {
     108                 :          0 :         struct remote_function_call data = {
     109                 :            :                 .p      = NULL,
     110                 :            :                 .func   = func,
     111                 :            :                 .info   = info,
     112                 :            :                 .ret    = -ENXIO, /* No such CPU */
     113                 :            :         };
     114                 :            : 
     115                 :          0 :         smp_call_function_single(cpu, remote_function, &data, 1);
     116                 :            : 
     117                 :          0 :         return data.ret;
     118                 :            : }
     119                 :            : 
     120                 :            : #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
     121                 :            :                        PERF_FLAG_FD_OUTPUT  |\
     122                 :            :                        PERF_FLAG_PID_CGROUP |\
     123                 :            :                        PERF_FLAG_FD_CLOEXEC)
     124                 :            : 
     125                 :            : /*
     126                 :            :  * branch priv levels that need permission checks
     127                 :            :  */
     128                 :            : #define PERF_SAMPLE_BRANCH_PERM_PLM \
     129                 :            :         (PERF_SAMPLE_BRANCH_KERNEL |\
     130                 :            :          PERF_SAMPLE_BRANCH_HV)
     131                 :            : 
     132                 :            : enum event_type_t {
     133                 :            :         EVENT_FLEXIBLE = 0x1,
     134                 :            :         EVENT_PINNED = 0x2,
     135                 :            :         EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
     136                 :            : };
     137                 :            : 
     138                 :            : /*
     139                 :            :  * perf_sched_events : >0 events exist
     140                 :            :  * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
     141                 :            :  */
     142                 :            : struct static_key_deferred perf_sched_events __read_mostly;
     143                 :            : static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
     144                 :            : static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
     145                 :            : 
     146                 :            : static atomic_t nr_mmap_events __read_mostly;
     147                 :            : static atomic_t nr_comm_events __read_mostly;
     148                 :            : static atomic_t nr_task_events __read_mostly;
     149                 :            : static atomic_t nr_freq_events __read_mostly;
     150                 :            : 
     151                 :            : static LIST_HEAD(pmus);
     152                 :            : static DEFINE_MUTEX(pmus_lock);
     153                 :            : static struct srcu_struct pmus_srcu;
     154                 :            : 
     155                 :            : /*
     156                 :            :  * perf event paranoia level:
     157                 :            :  *  -1 - not paranoid at all
     158                 :            :  *   0 - disallow raw tracepoint access for unpriv
     159                 :            :  *   1 - disallow cpu events for unpriv
     160                 :            :  *   2 - disallow kernel profiling for unpriv
     161                 :            :  */
     162                 :            : int sysctl_perf_event_paranoid __read_mostly = 1;
     163                 :            : 
     164                 :            : /* Minimum for 512 kiB + 1 user control page */
     165                 :            : int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
     166                 :            : 
     167                 :            : /*
     168                 :            :  * max perf event sample rate
     169                 :            :  */
     170                 :            : #define DEFAULT_MAX_SAMPLE_RATE         100000
     171                 :            : #define DEFAULT_SAMPLE_PERIOD_NS        (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
     172                 :            : #define DEFAULT_CPU_TIME_MAX_PERCENT    25
     173                 :            : 
     174                 :            : int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
     175                 :            : 
     176                 :            : static int max_samples_per_tick __read_mostly   = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
     177                 :            : static int perf_sample_period_ns __read_mostly  = DEFAULT_SAMPLE_PERIOD_NS;
     178                 :            : 
     179                 :            : static int perf_sample_allowed_ns __read_mostly =
     180                 :            :         DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
     181                 :            : 
     182                 :          0 : void update_perf_cpu_limits(void)
     183                 :            : {
     184                 :          0 :         u64 tmp = perf_sample_period_ns;
     185                 :            : 
     186                 :          0 :         tmp *= sysctl_perf_cpu_time_max_percent;
     187                 :          0 :         do_div(tmp, 100);
     188                 :          0 :         ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
     189                 :          0 : }
     190                 :            : 
     191                 :            : static int perf_rotate_context(struct perf_cpu_context *cpuctx);
     192                 :            : 
     193                 :          0 : int perf_proc_update_handler(struct ctl_table *table, int write,
     194                 :            :                 void __user *buffer, size_t *lenp,
     195                 :            :                 loff_t *ppos)
     196                 :            : {
     197                 :          2 :         int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
     198                 :            : 
     199         [ -  + ]:          2 :         if (ret || !write)
     200                 :            :                 return ret;
     201                 :            : 
     202                 :          0 :         max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
     203                 :          0 :         perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
     204                 :          0 :         update_perf_cpu_limits();
     205                 :            : 
     206                 :          0 :         return 0;
     207                 :            : }
     208                 :            : 
     209                 :            : int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
     210                 :            : 
     211                 :          0 : int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
     212                 :            :                                 void __user *buffer, size_t *lenp,
     213                 :            :                                 loff_t *ppos)
     214                 :            : {
     215                 :          2 :         int ret = proc_dointvec(table, write, buffer, lenp, ppos);
     216                 :            : 
     217         [ -  + ]:          2 :         if (ret || !write)
     218                 :            :                 return ret;
     219                 :            : 
     220                 :          0 :         update_perf_cpu_limits();
     221                 :            : 
     222                 :          0 :         return 0;
     223                 :            : }
     224                 :            : 
     225                 :            : /*
     226                 :            :  * perf samples are done in some very critical code paths (NMIs).
     227                 :            :  * If they take too much CPU time, the system can lock up and not
     228                 :            :  * get any real work done.  This will drop the sample rate when
     229                 :            :  * we detect that events are taking too long.
     230                 :            :  */
     231                 :            : #define NR_ACCUMULATED_SAMPLES 128
     232                 :            : static DEFINE_PER_CPU(u64, running_sample_length);
     233                 :            : 
     234                 :          0 : void perf_sample_event_took(u64 sample_len_ns)
     235                 :            : {
     236                 :            :         u64 avg_local_sample_len;
     237                 :            :         u64 local_samples_len;
     238                 :          0 :         u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
     239                 :            : 
     240         [ #  # ]:          0 :         if (allowed_ns == 0)
     241                 :            :                 return;
     242                 :            : 
     243                 :            :         /* decay the counter by 1 average sample */
     244                 :          0 :         local_samples_len = __get_cpu_var(running_sample_length);
     245                 :          0 :         local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
     246                 :          0 :         local_samples_len += sample_len_ns;
     247                 :          0 :         __get_cpu_var(running_sample_length) = local_samples_len;
     248                 :            : 
     249                 :            :         /*
     250                 :            :          * note: this will be biased artifically low until we have
     251                 :            :          * seen NR_ACCUMULATED_SAMPLES.  Doing it this way keeps us
     252                 :            :          * from having to maintain a count.
     253                 :            :          */
     254                 :          0 :         avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
     255                 :            : 
     256         [ #  # ]:          0 :         if (avg_local_sample_len <= allowed_ns)
     257                 :            :                 return;
     258                 :            : 
     259         [ #  # ]:          0 :         if (max_samples_per_tick <= 1)
     260                 :            :                 return;
     261                 :            : 
     262                 :          0 :         max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
     263                 :          0 :         sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
     264                 :          0 :         perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
     265                 :            : 
     266         [ #  # ]:          0 :         printk_ratelimited(KERN_WARNING
     267                 :            :                         "perf samples too long (%lld > %lld), lowering "
     268                 :            :                         "kernel.perf_event_max_sample_rate to %d\n",
     269                 :            :                         avg_local_sample_len, allowed_ns,
     270                 :            :                         sysctl_perf_event_sample_rate);
     271                 :            : 
     272                 :          0 :         update_perf_cpu_limits();
     273                 :            : }
     274                 :            : 
     275                 :            : static atomic64_t perf_event_id;
     276                 :            : 
     277                 :            : static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
     278                 :            :                               enum event_type_t event_type);
     279                 :            : 
     280                 :            : static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
     281                 :            :                              enum event_type_t event_type,
     282                 :            :                              struct task_struct *task);
     283                 :            : 
     284                 :            : static void update_context_time(struct perf_event_context *ctx);
     285                 :            : static u64 perf_event_time(struct perf_event *event);
     286                 :            : 
     287                 :          0 : void __weak perf_event_print_debug(void)        { }
     288                 :            : 
     289                 :          0 : extern __weak const char *perf_pmu_name(void)
     290                 :            : {
     291                 :          0 :         return "pmu";
     292                 :            : }
     293                 :            : 
     294                 :            : static inline u64 perf_clock(void)
     295                 :            : {
     296                 :          0 :         return local_clock();
     297                 :            : }
     298                 :            : 
     299                 :            : static inline struct perf_cpu_context *
     300                 :            : __get_cpu_context(struct perf_event_context *ctx)
     301                 :            : {
     302                 :          0 :         return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
     303                 :            : }
     304                 :            : 
     305                 :            : static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
     306                 :            :                           struct perf_event_context *ctx)
     307                 :            : {
     308                 :          0 :         raw_spin_lock(&cpuctx->ctx.lock);
     309   [ #  #  #  #  :          0 :         if (ctx)
             #  #  #  # ]
     310                 :          0 :                 raw_spin_lock(&ctx->lock);
     311                 :            : }
     312                 :            : 
     313                 :          0 : static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
     314                 :            :                             struct perf_event_context *ctx)
     315                 :            : {
     316         [ #  # ]:          0 :         if (ctx)
     317                 :            :                 raw_spin_unlock(&ctx->lock);
     318                 :            :         raw_spin_unlock(&cpuctx->ctx.lock);
     319                 :          0 : }
     320                 :            : 
     321                 :            : #ifdef CONFIG_CGROUP_PERF
     322                 :            : 
     323                 :            : /*
     324                 :            :  * perf_cgroup_info keeps track of time_enabled for a cgroup.
     325                 :            :  * This is a per-cpu dynamically allocated data structure.
     326                 :            :  */
     327                 :            : struct perf_cgroup_info {
     328                 :            :         u64                             time;
     329                 :            :         u64                             timestamp;
     330                 :            : };
     331                 :            : 
     332                 :            : struct perf_cgroup {
     333                 :            :         struct cgroup_subsys_state      css;
     334                 :            :         struct perf_cgroup_info __percpu *info;
     335                 :            : };
     336                 :            : 
     337                 :            : /*
     338                 :            :  * Must ensure cgroup is pinned (css_get) before calling
     339                 :            :  * this function. In other words, we cannot call this function
     340                 :            :  * if there is no cgroup event for the current CPU context.
     341                 :            :  */
     342                 :            : static inline struct perf_cgroup *
     343                 :            : perf_cgroup_from_task(struct task_struct *task)
     344                 :            : {
     345                 :            :         return container_of(task_css(task, perf_subsys_id),
     346                 :            :                             struct perf_cgroup, css);
     347                 :            : }
     348                 :            : 
     349                 :            : static inline bool
     350                 :            : perf_cgroup_match(struct perf_event *event)
     351                 :            : {
     352                 :            :         struct perf_event_context *ctx = event->ctx;
     353                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
     354                 :            : 
     355                 :            :         /* @event doesn't care about cgroup */
     356                 :            :         if (!event->cgrp)
     357                 :            :                 return true;
     358                 :            : 
     359                 :            :         /* wants specific cgroup scope but @cpuctx isn't associated with any */
     360                 :            :         if (!cpuctx->cgrp)
     361                 :            :                 return false;
     362                 :            : 
     363                 :            :         /*
     364                 :            :          * Cgroup scoping is recursive.  An event enabled for a cgroup is
     365                 :            :          * also enabled for all its descendant cgroups.  If @cpuctx's
     366                 :            :          * cgroup is a descendant of @event's (the test covers identity
     367                 :            :          * case), it's a match.
     368                 :            :          */
     369                 :            :         return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
     370                 :            :                                     event->cgrp->css.cgroup);
     371                 :            : }
     372                 :            : 
     373                 :            : static inline bool perf_tryget_cgroup(struct perf_event *event)
     374                 :            : {
     375                 :            :         return css_tryget(&event->cgrp->css);
     376                 :            : }
     377                 :            : 
     378                 :            : static inline void perf_put_cgroup(struct perf_event *event)
     379                 :            : {
     380                 :            :         css_put(&event->cgrp->css);
     381                 :            : }
     382                 :            : 
     383                 :            : static inline void perf_detach_cgroup(struct perf_event *event)
     384                 :            : {
     385                 :            :         perf_put_cgroup(event);
     386                 :            :         event->cgrp = NULL;
     387                 :            : }
     388                 :            : 
     389                 :            : static inline int is_cgroup_event(struct perf_event *event)
     390                 :            : {
     391                 :            :         return event->cgrp != NULL;
     392                 :            : }
     393                 :            : 
     394                 :            : static inline u64 perf_cgroup_event_time(struct perf_event *event)
     395                 :            : {
     396                 :            :         struct perf_cgroup_info *t;
     397                 :            : 
     398                 :            :         t = per_cpu_ptr(event->cgrp->info, event->cpu);
     399                 :            :         return t->time;
     400                 :            : }
     401                 :            : 
     402                 :            : static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
     403                 :            : {
     404                 :            :         struct perf_cgroup_info *info;
     405                 :            :         u64 now;
     406                 :            : 
     407                 :            :         now = perf_clock();
     408                 :            : 
     409                 :            :         info = this_cpu_ptr(cgrp->info);
     410                 :            : 
     411                 :            :         info->time += now - info->timestamp;
     412                 :            :         info->timestamp = now;
     413                 :            : }
     414                 :            : 
     415                 :            : static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
     416                 :            : {
     417                 :            :         struct perf_cgroup *cgrp_out = cpuctx->cgrp;
     418                 :            :         if (cgrp_out)
     419                 :            :                 __update_cgrp_time(cgrp_out);
     420                 :            : }
     421                 :            : 
     422                 :            : static inline void update_cgrp_time_from_event(struct perf_event *event)
     423                 :            : {
     424                 :            :         struct perf_cgroup *cgrp;
     425                 :            : 
     426                 :            :         /*
     427                 :            :          * ensure we access cgroup data only when needed and
     428                 :            :          * when we know the cgroup is pinned (css_get)
     429                 :            :          */
     430                 :            :         if (!is_cgroup_event(event))
     431                 :            :                 return;
     432                 :            : 
     433                 :            :         cgrp = perf_cgroup_from_task(current);
     434                 :            :         /*
     435                 :            :          * Do not update time when cgroup is not active
     436                 :            :          */
     437                 :            :         if (cgrp == event->cgrp)
     438                 :            :                 __update_cgrp_time(event->cgrp);
     439                 :            : }
     440                 :            : 
     441                 :            : static inline void
     442                 :            : perf_cgroup_set_timestamp(struct task_struct *task,
     443                 :            :                           struct perf_event_context *ctx)
     444                 :            : {
     445                 :            :         struct perf_cgroup *cgrp;
     446                 :            :         struct perf_cgroup_info *info;
     447                 :            : 
     448                 :            :         /*
     449                 :            :          * ctx->lock held by caller
     450                 :            :          * ensure we do not access cgroup data
     451                 :            :          * unless we have the cgroup pinned (css_get)
     452                 :            :          */
     453                 :            :         if (!task || !ctx->nr_cgroups)
     454                 :            :                 return;
     455                 :            : 
     456                 :            :         cgrp = perf_cgroup_from_task(task);
     457                 :            :         info = this_cpu_ptr(cgrp->info);
     458                 :            :         info->timestamp = ctx->timestamp;
     459                 :            : }
     460                 :            : 
     461                 :            : #define PERF_CGROUP_SWOUT       0x1 /* cgroup switch out every event */
     462                 :            : #define PERF_CGROUP_SWIN        0x2 /* cgroup switch in events based on task */
     463                 :            : 
     464                 :            : /*
     465                 :            :  * reschedule events based on the cgroup constraint of task.
     466                 :            :  *
     467                 :            :  * mode SWOUT : schedule out everything
     468                 :            :  * mode SWIN : schedule in based on cgroup for next
     469                 :            :  */
     470                 :            : void perf_cgroup_switch(struct task_struct *task, int mode)
     471                 :            : {
     472                 :            :         struct perf_cpu_context *cpuctx;
     473                 :            :         struct pmu *pmu;
     474                 :            :         unsigned long flags;
     475                 :            : 
     476                 :            :         /*
     477                 :            :          * disable interrupts to avoid geting nr_cgroup
     478                 :            :          * changes via __perf_event_disable(). Also
     479                 :            :          * avoids preemption.
     480                 :            :          */
     481                 :            :         local_irq_save(flags);
     482                 :            : 
     483                 :            :         /*
     484                 :            :          * we reschedule only in the presence of cgroup
     485                 :            :          * constrained events.
     486                 :            :          */
     487                 :            :         rcu_read_lock();
     488                 :            : 
     489                 :            :         list_for_each_entry_rcu(pmu, &pmus, entry) {
     490                 :            :                 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
     491                 :            :                 if (cpuctx->unique_pmu != pmu)
     492                 :            :                         continue; /* ensure we process each cpuctx once */
     493                 :            : 
     494                 :            :                 /*
     495                 :            :                  * perf_cgroup_events says at least one
     496                 :            :                  * context on this CPU has cgroup events.
     497                 :            :                  *
     498                 :            :                  * ctx->nr_cgroups reports the number of cgroup
     499                 :            :                  * events for a context.
     500                 :            :                  */
     501                 :            :                 if (cpuctx->ctx.nr_cgroups > 0) {
     502                 :            :                         perf_ctx_lock(cpuctx, cpuctx->task_ctx);
     503                 :            :                         perf_pmu_disable(cpuctx->ctx.pmu);
     504                 :            : 
     505                 :            :                         if (mode & PERF_CGROUP_SWOUT) {
     506                 :            :                                 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
     507                 :            :                                 /*
     508                 :            :                                  * must not be done before ctxswout due
     509                 :            :                                  * to event_filter_match() in event_sched_out()
     510                 :            :                                  */
     511                 :            :                                 cpuctx->cgrp = NULL;
     512                 :            :                         }
     513                 :            : 
     514                 :            :                         if (mode & PERF_CGROUP_SWIN) {
     515                 :            :                                 WARN_ON_ONCE(cpuctx->cgrp);
     516                 :            :                                 /*
     517                 :            :                                  * set cgrp before ctxsw in to allow
     518                 :            :                                  * event_filter_match() to not have to pass
     519                 :            :                                  * task around
     520                 :            :                                  */
     521                 :            :                                 cpuctx->cgrp = perf_cgroup_from_task(task);
     522                 :            :                                 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
     523                 :            :                         }
     524                 :            :                         perf_pmu_enable(cpuctx->ctx.pmu);
     525                 :            :                         perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
     526                 :            :                 }
     527                 :            :         }
     528                 :            : 
     529                 :            :         rcu_read_unlock();
     530                 :            : 
     531                 :            :         local_irq_restore(flags);
     532                 :            : }
     533                 :            : 
     534                 :            : static inline void perf_cgroup_sched_out(struct task_struct *task,
     535                 :            :                                          struct task_struct *next)
     536                 :            : {
     537                 :            :         struct perf_cgroup *cgrp1;
     538                 :            :         struct perf_cgroup *cgrp2 = NULL;
     539                 :            : 
     540                 :            :         /*
     541                 :            :          * we come here when we know perf_cgroup_events > 0
     542                 :            :          */
     543                 :            :         cgrp1 = perf_cgroup_from_task(task);
     544                 :            : 
     545                 :            :         /*
     546                 :            :          * next is NULL when called from perf_event_enable_on_exec()
     547                 :            :          * that will systematically cause a cgroup_switch()
     548                 :            :          */
     549                 :            :         if (next)
     550                 :            :                 cgrp2 = perf_cgroup_from_task(next);
     551                 :            : 
     552                 :            :         /*
     553                 :            :          * only schedule out current cgroup events if we know
     554                 :            :          * that we are switching to a different cgroup. Otherwise,
     555                 :            :          * do no touch the cgroup events.
     556                 :            :          */
     557                 :            :         if (cgrp1 != cgrp2)
     558                 :            :                 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
     559                 :            : }
     560                 :            : 
     561                 :            : static inline void perf_cgroup_sched_in(struct task_struct *prev,
     562                 :            :                                         struct task_struct *task)
     563                 :            : {
     564                 :            :         struct perf_cgroup *cgrp1;
     565                 :            :         struct perf_cgroup *cgrp2 = NULL;
     566                 :            : 
     567                 :            :         /*
     568                 :            :          * we come here when we know perf_cgroup_events > 0
     569                 :            :          */
     570                 :            :         cgrp1 = perf_cgroup_from_task(task);
     571                 :            : 
     572                 :            :         /* prev can never be NULL */
     573                 :            :         cgrp2 = perf_cgroup_from_task(prev);
     574                 :            : 
     575                 :            :         /*
     576                 :            :          * only need to schedule in cgroup events if we are changing
     577                 :            :          * cgroup during ctxsw. Cgroup events were not scheduled
     578                 :            :          * out of ctxsw out if that was not the case.
     579                 :            :          */
     580                 :            :         if (cgrp1 != cgrp2)
     581                 :            :                 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
     582                 :            : }
     583                 :            : 
     584                 :            : static inline int perf_cgroup_connect(int fd, struct perf_event *event,
     585                 :            :                                       struct perf_event_attr *attr,
     586                 :            :                                       struct perf_event *group_leader)
     587                 :            : {
     588                 :            :         struct perf_cgroup *cgrp;
     589                 :            :         struct cgroup_subsys_state *css;
     590                 :            :         struct fd f = fdget(fd);
     591                 :            :         int ret = 0;
     592                 :            : 
     593                 :            :         if (!f.file)
     594                 :            :                 return -EBADF;
     595                 :            : 
     596                 :            :         rcu_read_lock();
     597                 :            : 
     598                 :            :         css = css_from_dir(f.file->f_dentry, &perf_subsys);
     599                 :            :         if (IS_ERR(css)) {
     600                 :            :                 ret = PTR_ERR(css);
     601                 :            :                 goto out;
     602                 :            :         }
     603                 :            : 
     604                 :            :         cgrp = container_of(css, struct perf_cgroup, css);
     605                 :            :         event->cgrp = cgrp;
     606                 :            : 
     607                 :            :         /* must be done before we fput() the file */
     608                 :            :         if (!perf_tryget_cgroup(event)) {
     609                 :            :                 event->cgrp = NULL;
     610                 :            :                 ret = -ENOENT;
     611                 :            :                 goto out;
     612                 :            :         }
     613                 :            : 
     614                 :            :         /*
     615                 :            :          * all events in a group must monitor
     616                 :            :          * the same cgroup because a task belongs
     617                 :            :          * to only one perf cgroup at a time
     618                 :            :          */
     619                 :            :         if (group_leader && group_leader->cgrp != cgrp) {
     620                 :            :                 perf_detach_cgroup(event);
     621                 :            :                 ret = -EINVAL;
     622                 :            :         }
     623                 :            : out:
     624                 :            :         rcu_read_unlock();
     625                 :            :         fdput(f);
     626                 :            :         return ret;
     627                 :            : }
     628                 :            : 
     629                 :            : static inline void
     630                 :            : perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
     631                 :            : {
     632                 :            :         struct perf_cgroup_info *t;
     633                 :            :         t = per_cpu_ptr(event->cgrp->info, event->cpu);
     634                 :            :         event->shadow_ctx_time = now - t->timestamp;
     635                 :            : }
     636                 :            : 
     637                 :            : static inline void
     638                 :            : perf_cgroup_defer_enabled(struct perf_event *event)
     639                 :            : {
     640                 :            :         /*
     641                 :            :          * when the current task's perf cgroup does not match
     642                 :            :          * the event's, we need to remember to call the
     643                 :            :          * perf_mark_enable() function the first time a task with
     644                 :            :          * a matching perf cgroup is scheduled in.
     645                 :            :          */
     646                 :            :         if (is_cgroup_event(event) && !perf_cgroup_match(event))
     647                 :            :                 event->cgrp_defer_enabled = 1;
     648                 :            : }
     649                 :            : 
     650                 :            : static inline void
     651                 :            : perf_cgroup_mark_enabled(struct perf_event *event,
     652                 :            :                          struct perf_event_context *ctx)
     653                 :            : {
     654                 :            :         struct perf_event *sub;
     655                 :            :         u64 tstamp = perf_event_time(event);
     656                 :            : 
     657                 :            :         if (!event->cgrp_defer_enabled)
     658                 :            :                 return;
     659                 :            : 
     660                 :            :         event->cgrp_defer_enabled = 0;
     661                 :            : 
     662                 :            :         event->tstamp_enabled = tstamp - event->total_time_enabled;
     663                 :            :         list_for_each_entry(sub, &event->sibling_list, group_entry) {
     664                 :            :                 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
     665                 :            :                         sub->tstamp_enabled = tstamp - sub->total_time_enabled;
     666                 :            :                         sub->cgrp_defer_enabled = 0;
     667                 :            :                 }
     668                 :            :         }
     669                 :            : }
     670                 :            : #else /* !CONFIG_CGROUP_PERF */
     671                 :            : 
     672                 :            : static inline bool
     673                 :            : perf_cgroup_match(struct perf_event *event)
     674                 :            : {
     675                 :            :         return true;
     676                 :            : }
     677                 :            : 
     678                 :            : static inline void perf_detach_cgroup(struct perf_event *event)
     679                 :            : {}
     680                 :            : 
     681                 :            : static inline int is_cgroup_event(struct perf_event *event)
     682                 :            : {
     683                 :            :         return 0;
     684                 :            : }
     685                 :            : 
     686                 :            : static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
     687                 :            : {
     688                 :            :         return 0;
     689                 :            : }
     690                 :            : 
     691                 :            : static inline void update_cgrp_time_from_event(struct perf_event *event)
     692                 :            : {
     693                 :            : }
     694                 :            : 
     695                 :            : static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
     696                 :            : {
     697                 :            : }
     698                 :            : 
     699                 :            : static inline void perf_cgroup_sched_out(struct task_struct *task,
     700                 :            :                                          struct task_struct *next)
     701                 :            : {
     702                 :            : }
     703                 :            : 
     704                 :            : static inline void perf_cgroup_sched_in(struct task_struct *prev,
     705                 :            :                                         struct task_struct *task)
     706                 :            : {
     707                 :            : }
     708                 :            : 
     709                 :            : static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
     710                 :            :                                       struct perf_event_attr *attr,
     711                 :            :                                       struct perf_event *group_leader)
     712                 :            : {
     713                 :            :         return -EINVAL;
     714                 :            : }
     715                 :            : 
     716                 :            : static inline void
     717                 :            : perf_cgroup_set_timestamp(struct task_struct *task,
     718                 :            :                           struct perf_event_context *ctx)
     719                 :            : {
     720                 :            : }
     721                 :            : 
     722                 :            : void
     723                 :          0 : perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
     724                 :            : {
     725                 :          0 : }
     726                 :            : 
     727                 :            : static inline void
     728                 :            : perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
     729                 :            : {
     730                 :            : }
     731                 :            : 
     732                 :            : static inline u64 perf_cgroup_event_time(struct perf_event *event)
     733                 :            : {
     734                 :            :         return 0;
     735                 :            : }
     736                 :            : 
     737                 :            : static inline void
     738                 :            : perf_cgroup_defer_enabled(struct perf_event *event)
     739                 :            : {
     740                 :            : }
     741                 :            : 
     742                 :            : static inline void
     743                 :            : perf_cgroup_mark_enabled(struct perf_event *event,
     744                 :            :                          struct perf_event_context *ctx)
     745                 :            : {
     746                 :            : }
     747                 :            : #endif
     748                 :            : 
     749                 :            : /*
     750                 :            :  * set default to be dependent on timer tick just
     751                 :            :  * like original code
     752                 :            :  */
     753                 :            : #define PERF_CPU_HRTIMER (1000 / HZ)
     754                 :            : /*
     755                 :            :  * function must be called with interrupts disbled
     756                 :            :  */
     757                 :          0 : static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
     758                 :            : {
     759                 :            :         struct perf_cpu_context *cpuctx;
     760                 :            :         enum hrtimer_restart ret = HRTIMER_NORESTART;
     761                 :            :         int rotations = 0;
     762                 :            : 
     763         [ #  # ]:          0 :         WARN_ON(!irqs_disabled());
     764                 :            : 
     765                 :          0 :         cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
     766                 :            : 
     767                 :          0 :         rotations = perf_rotate_context(cpuctx);
     768                 :            : 
     769                 :            :         /*
     770                 :            :          * arm timer if needed
     771                 :            :          */
     772         [ #  # ]:          0 :         if (rotations) {
     773                 :            :                 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
     774                 :            :                 ret = HRTIMER_RESTART;
     775                 :            :         }
     776                 :            : 
     777                 :          0 :         return ret;
     778                 :            : }
     779                 :            : 
     780                 :            : /* CPU is going down */
     781                 :          0 : void perf_cpu_hrtimer_cancel(int cpu)
     782                 :            : {
     783                 :            :         struct perf_cpu_context *cpuctx;
     784                 :            :         struct pmu *pmu;
     785                 :            :         unsigned long flags;
     786                 :            : 
     787 [ #  # ][ #  # ]:          0 :         if (WARN_ON(cpu != smp_processor_id()))
     788                 :          0 :                 return;
     789                 :            : 
     790                 :            :         local_irq_save(flags);
     791                 :            : 
     792                 :            :         rcu_read_lock();
     793                 :            : 
     794         [ #  # ]:          0 :         list_for_each_entry_rcu(pmu, &pmus, entry) {
     795                 :          0 :                 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
     796                 :            : 
     797         [ #  # ]:          0 :                 if (pmu->task_ctx_nr == perf_sw_context)
     798                 :          0 :                         continue;
     799                 :            : 
     800                 :          0 :                 hrtimer_cancel(&cpuctx->hrtimer);
     801                 :            :         }
     802                 :            : 
     803                 :            :         rcu_read_unlock();
     804                 :            : 
     805         [ #  # ]:          0 :         local_irq_restore(flags);
     806                 :            : }
     807                 :            : 
     808                 :          0 : static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
     809                 :            : {
     810                 :          0 :         struct hrtimer *hr = &cpuctx->hrtimer;
     811                 :          0 :         struct pmu *pmu = cpuctx->ctx.pmu;
     812                 :            :         int timer;
     813                 :            : 
     814                 :            :         /* no multiplexing needed for SW PMU */
     815         [ #  # ]:          0 :         if (pmu->task_ctx_nr == perf_sw_context)
     816                 :          0 :                 return;
     817                 :            : 
     818                 :            :         /*
     819                 :            :          * check default is sane, if not set then force to
     820                 :            :          * default interval (1/tick)
     821                 :            :          */
     822                 :          0 :         timer = pmu->hrtimer_interval_ms;
     823         [ #  # ]:          0 :         if (timer < 1)
     824                 :          0 :                 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
     825                 :            : 
     826                 :          0 :         cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
     827                 :            : 
     828                 :          0 :         hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
     829                 :          0 :         hr->function = perf_cpu_hrtimer_handler;
     830                 :            : }
     831                 :            : 
     832                 :          0 : static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
     833                 :            : {
     834                 :          0 :         struct hrtimer *hr = &cpuctx->hrtimer;
     835                 :          0 :         struct pmu *pmu = cpuctx->ctx.pmu;
     836                 :            : 
     837                 :            :         /* not for SW PMU */
     838         [ #  # ]:          0 :         if (pmu->task_ctx_nr == perf_sw_context)
     839                 :            :                 return;
     840                 :            : 
     841         [ #  # ]:          0 :         if (hrtimer_active(hr))
     842                 :            :                 return;
     843                 :            : 
     844         [ #  # ]:          0 :         if (!hrtimer_callback_running(hr))
     845                 :          0 :                 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
     846                 :            :                                          0, HRTIMER_MODE_REL_PINNED, 0);
     847                 :            : }
     848                 :            : 
     849                 :          0 : void perf_pmu_disable(struct pmu *pmu)
     850                 :            : {
     851                 :          0 :         int *count = this_cpu_ptr(pmu->pmu_disable_count);
     852   [ #  #  #  # ]:          0 :         if (!(*count)++)
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     853                 :          0 :                 pmu->pmu_disable(pmu);
     854                 :          0 : }
     855                 :            : 
     856                 :          0 : void perf_pmu_enable(struct pmu *pmu)
     857                 :            : {
     858                 :          0 :         int *count = this_cpu_ptr(pmu->pmu_disable_count);
     859 [ #  # ][ #  # ]:          0 :         if (!--(*count))
         [ #  # ][ #  # ]
           [ #  #  #  #  
           #  # ][ #  # ]
           [ #  #  #  # ]
           [ #  #  #  # ]
                 [ #  # ]
     860                 :          0 :                 pmu->pmu_enable(pmu);
     861                 :          0 : }
     862                 :            : 
     863                 :            : static DEFINE_PER_CPU(struct list_head, rotation_list);
     864                 :            : 
     865                 :            : /*
     866                 :            :  * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
     867                 :            :  * because they're strictly cpu affine and rotate_start is called with IRQs
     868                 :            :  * disabled, while rotate_context is called from IRQ context.
     869                 :            :  */
     870                 :          0 : static void perf_pmu_rotate_start(struct pmu *pmu)
     871                 :            : {
     872                 :          0 :         struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
     873                 :          0 :         struct list_head *head = &__get_cpu_var(rotation_list);
     874                 :            : 
     875         [ #  # ]:          0 :         WARN_ON(!irqs_disabled());
     876                 :            : 
     877         [ #  # ]:          0 :         if (list_empty(&cpuctx->rotation_list))
     878                 :            :                 list_add(&cpuctx->rotation_list, head);
     879                 :          0 : }
     880                 :            : 
     881                 :          0 : static void get_ctx(struct perf_event_context *ctx)
     882                 :            : {
     883         [ #  # ]:          0 :         WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
     884                 :          0 : }
     885                 :            : 
     886                 :          0 : static void put_ctx(struct perf_event_context *ctx)
     887                 :            : {
     888         [ #  # ]:          0 :         if (atomic_dec_and_test(&ctx->refcount)) {
     889         [ #  # ]:          0 :                 if (ctx->parent_ctx)
     890                 :          0 :                         put_ctx(ctx->parent_ctx);
     891         [ #  # ]:          0 :                 if (ctx->task)
     892                 :            :                         put_task_struct(ctx->task);
     893                 :          0 :                 kfree_rcu(ctx, rcu_head);
     894                 :            :         }
     895                 :          0 : }
     896                 :            : 
     897                 :            : static void unclone_ctx(struct perf_event_context *ctx)
     898                 :            : {
     899 [ #  # ][ #  # ]:          0 :         if (ctx->parent_ctx) {
                 [ #  # ]
     900                 :          0 :                 put_ctx(ctx->parent_ctx);
     901                 :          0 :                 ctx->parent_ctx = NULL;
     902                 :            :         }
     903                 :          0 :         ctx->generation++;
     904                 :            : }
     905                 :            : 
     906                 :            : static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
     907                 :            : {
     908                 :            :         /*
     909                 :            :          * only top level events have the pid namespace they were created in
     910                 :            :          */
     911 [ #  # ][ #  # ]:          0 :         if (event->parent)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     912                 :            :                 event = event->parent;
     913                 :            : 
     914                 :          0 :         return task_tgid_nr_ns(p, event->ns);
     915                 :            : }
     916                 :            : 
     917                 :            : static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
     918                 :            : {
     919                 :            :         /*
     920                 :            :          * only top level events have the pid namespace they were created in
     921                 :            :          */
     922 [ #  # ][ #  # ]:          0 :         if (event->parent)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     923                 :            :                 event = event->parent;
     924                 :            : 
     925                 :          0 :         return task_pid_nr_ns(p, event->ns);
     926                 :            : }
     927                 :            : 
     928                 :            : /*
     929                 :            :  * If we inherit events we want to return the parent event id
     930                 :            :  * to userspace.
     931                 :            :  */
     932                 :            : static u64 primary_event_id(struct perf_event *event)
     933                 :            : {
     934                 :          0 :         u64 id = event->id;
     935                 :            : 
     936 [ #  # ][ #  # ]:          0 :         if (event->parent)
           [ #  #  #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     937                 :          0 :                 id = event->parent->id;
     938                 :            : 
     939                 :            :         return id;
     940                 :            : }
     941                 :            : 
     942                 :            : /*
     943                 :            :  * Get the perf_event_context for a task and lock it.
     944                 :            :  * This has to cope with with the fact that until it is locked,
     945                 :            :  * the context could get moved to another task.
     946                 :            :  */
     947                 :            : static struct perf_event_context *
     948                 :          0 : perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
     949                 :            : {
     950                 :            :         struct perf_event_context *ctx;
     951                 :            : 
     952                 :            : retry:
     953                 :            :         /*
     954                 :            :          * One of the few rules of preemptible RCU is that one cannot do
     955                 :            :          * rcu_read_unlock() while holding a scheduler (or nested) lock when
     956                 :            :          * part of the read side critical section was preemptible -- see
     957                 :            :          * rcu_read_unlock_special().
     958                 :            :          *
     959                 :            :          * Since ctx->lock nests under rq->lock we must ensure the entire read
     960                 :            :          * side critical section is non-preemptible.
     961                 :            :          */
     962                 :          0 :         preempt_disable();
     963                 :            :         rcu_read_lock();
     964                 :          0 :         ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
     965         [ #  # ]:          0 :         if (ctx) {
     966                 :            :                 /*
     967                 :            :                  * If this context is a clone of another, it might
     968                 :            :                  * get swapped for another underneath us by
     969                 :            :                  * perf_event_task_sched_out, though the
     970                 :            :                  * rcu_read_lock() protects us from any context
     971                 :            :                  * getting freed.  Lock the context and check if it
     972                 :            :                  * got swapped before we could get the lock, and retry
     973                 :            :                  * if so.  If we locked the right context, then it
     974                 :            :                  * can't get swapped on us any more.
     975                 :            :                  */
     976                 :          0 :                 raw_spin_lock_irqsave(&ctx->lock, *flags);
     977         [ #  # ]:          0 :                 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
     978                 :          0 :                         raw_spin_unlock_irqrestore(&ctx->lock, *flags);
     979                 :            :                         rcu_read_unlock();
     980                 :          0 :                         preempt_enable();
     981                 :          0 :                         goto retry;
     982                 :            :                 }
     983                 :            : 
     984         [ #  # ]:          0 :                 if (!atomic_inc_not_zero(&ctx->refcount)) {
     985                 :          0 :                         raw_spin_unlock_irqrestore(&ctx->lock, *flags);
     986                 :            :                         ctx = NULL;
     987                 :            :                 }
     988                 :            :         }
     989                 :            :         rcu_read_unlock();
     990                 :          0 :         preempt_enable();
     991                 :          0 :         return ctx;
     992                 :            : }
     993                 :            : 
     994                 :            : /*
     995                 :            :  * Get the context for a task and increment its pin_count so it
     996                 :            :  * can't get swapped to another task.  This also increments its
     997                 :            :  * reference count so that the context can't get freed.
     998                 :            :  */
     999                 :            : static struct perf_event_context *
    1000                 :          0 : perf_pin_task_context(struct task_struct *task, int ctxn)
    1001                 :            : {
    1002                 :            :         struct perf_event_context *ctx;
    1003                 :            :         unsigned long flags;
    1004                 :            : 
    1005                 :          0 :         ctx = perf_lock_task_context(task, ctxn, &flags);
    1006         [ #  # ]:          0 :         if (ctx) {
    1007                 :          0 :                 ++ctx->pin_count;
    1008                 :          0 :                 raw_spin_unlock_irqrestore(&ctx->lock, flags);
    1009                 :            :         }
    1010                 :          0 :         return ctx;
    1011                 :            : }
    1012                 :            : 
    1013                 :          0 : static void perf_unpin_context(struct perf_event_context *ctx)
    1014                 :            : {
    1015                 :            :         unsigned long flags;
    1016                 :            : 
    1017                 :          0 :         raw_spin_lock_irqsave(&ctx->lock, flags);
    1018                 :          0 :         --ctx->pin_count;
    1019                 :          0 :         raw_spin_unlock_irqrestore(&ctx->lock, flags);
    1020                 :          0 : }
    1021                 :            : 
    1022                 :            : /*
    1023                 :            :  * Update the record of the current time in a context.
    1024                 :            :  */
    1025                 :          0 : static void update_context_time(struct perf_event_context *ctx)
    1026                 :            : {
    1027                 :            :         u64 now = perf_clock();
    1028                 :            : 
    1029                 :          0 :         ctx->time += now - ctx->timestamp;
    1030                 :          0 :         ctx->timestamp = now;
    1031                 :          0 : }
    1032                 :            : 
    1033                 :            : static u64 perf_event_time(struct perf_event *event)
    1034                 :            : {
    1035                 :            :         struct perf_event_context *ctx = event->ctx;
    1036                 :            : 
    1037                 :            :         if (is_cgroup_event(event))
    1038                 :            :                 return perf_cgroup_event_time(event);
    1039                 :            : 
    1040 [ #  # ][ #  # ]:          0 :         return ctx ? ctx->time : 0;
         [ #  # ][ #  # ]
                 [ #  # ]
    1041                 :            : }
    1042                 :            : 
    1043                 :            : /*
    1044                 :            :  * Update the total_time_enabled and total_time_running fields for a event.
    1045                 :            :  * The caller of this function needs to hold the ctx->lock.
    1046                 :            :  */
    1047                 :          0 : static void update_event_times(struct perf_event *event)
    1048                 :            : {
    1049                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1050                 :            :         u64 run_end;
    1051                 :            : 
    1052 [ #  # ][ #  # ]:          0 :         if (event->state < PERF_EVENT_STATE_INACTIVE ||
    1053                 :          0 :             event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
    1054                 :          0 :                 return;
    1055                 :            :         /*
    1056                 :            :          * in cgroup mode, time_enabled represents
    1057                 :            :          * the time the event was enabled AND active
    1058                 :            :          * tasks were in the monitored cgroup. This is
    1059                 :            :          * independent of the activity of the context as
    1060                 :            :          * there may be a mix of cgroup and non-cgroup events.
    1061                 :            :          *
    1062                 :            :          * That is why we treat cgroup events differently
    1063                 :            :          * here.
    1064                 :            :          */
    1065                 :            :         if (is_cgroup_event(event))
    1066                 :            :                 run_end = perf_cgroup_event_time(event);
    1067         [ #  # ]:          0 :         else if (ctx->is_active)
    1068                 :          0 :                 run_end = ctx->time;
    1069                 :            :         else
    1070                 :          0 :                 run_end = event->tstamp_stopped;
    1071                 :            : 
    1072                 :          0 :         event->total_time_enabled = run_end - event->tstamp_enabled;
    1073                 :            : 
    1074         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_INACTIVE)
    1075                 :          0 :                 run_end = event->tstamp_stopped;
    1076                 :            :         else
    1077                 :            :                 run_end = perf_event_time(event);
    1078                 :            : 
    1079                 :          0 :         event->total_time_running = run_end - event->tstamp_running;
    1080                 :            : 
    1081                 :            : }
    1082                 :            : 
    1083                 :            : /*
    1084                 :            :  * Update total_time_enabled and total_time_running for all events in a group.
    1085                 :            :  */
    1086                 :          0 : static void update_group_times(struct perf_event *leader)
    1087                 :            : {
    1088                 :            :         struct perf_event *event;
    1089                 :            : 
    1090                 :          0 :         update_event_times(leader);
    1091         [ #  # ]:          0 :         list_for_each_entry(event, &leader->sibling_list, group_entry)
    1092                 :          0 :                 update_event_times(event);
    1093                 :          0 : }
    1094                 :            : 
    1095                 :            : static struct list_head *
    1096                 :            : ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
    1097                 :            : {
    1098         [ #  # ]:          0 :         if (event->attr.pinned)
    1099                 :          0 :                 return &ctx->pinned_groups;
    1100                 :            :         else
    1101                 :          0 :                 return &ctx->flexible_groups;
    1102                 :            : }
    1103                 :            : 
    1104                 :            : /*
    1105                 :            :  * Add a event from the lists for its context.
    1106                 :            :  * Must be called with ctx->mutex and ctx->lock held.
    1107                 :            :  */
    1108                 :            : static void
    1109                 :          0 : list_add_event(struct perf_event *event, struct perf_event_context *ctx)
    1110                 :            : {
    1111 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
                 [ #  # ]
    1112                 :          0 :         event->attach_state |= PERF_ATTACH_CONTEXT;
    1113                 :            : 
    1114                 :            :         /*
    1115                 :            :          * If we're a stand alone event or group leader, we go to the context
    1116                 :            :          * list, group events are kept attached to the group so that
    1117                 :            :          * perf_group_detach can, at all times, locate all siblings.
    1118                 :            :          */
    1119         [ #  # ]:          0 :         if (event->group_leader == event) {
    1120                 :            :                 struct list_head *list;
    1121                 :            : 
    1122         [ #  # ]:          0 :                 if (is_software_event(event))
    1123                 :          0 :                         event->group_flags |= PERF_GROUP_SOFTWARE;
    1124                 :            : 
    1125                 :            :                 list = ctx_group_list(event, ctx);
    1126                 :          0 :                 list_add_tail(&event->group_entry, list);
    1127                 :            :         }
    1128                 :            : 
    1129                 :            :         if (is_cgroup_event(event))
    1130                 :            :                 ctx->nr_cgroups++;
    1131                 :            : 
    1132         [ #  # ]:          0 :         if (has_branch_stack(event))
    1133                 :          0 :                 ctx->nr_branch_stack++;
    1134                 :            : 
    1135                 :          0 :         list_add_rcu(&event->event_entry, &ctx->event_list);
    1136         [ #  # ]:          0 :         if (!ctx->nr_events)
    1137                 :          0 :                 perf_pmu_rotate_start(ctx->pmu);
    1138                 :          0 :         ctx->nr_events++;
    1139         [ #  # ]:          0 :         if (event->attr.inherit_stat)
    1140                 :          0 :                 ctx->nr_stat++;
    1141                 :            : 
    1142                 :          0 :         ctx->generation++;
    1143                 :          0 : }
    1144                 :            : 
    1145                 :            : /*
    1146                 :            :  * Initialize event state based on the perf_event_attr::disabled.
    1147                 :            :  */
    1148                 :            : static inline void perf_event__state_init(struct perf_event *event)
    1149                 :            : {
    1150 [ #  # ][ #  # ]:          0 :         event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
                 [ #  # ]
    1151                 :            :                                               PERF_EVENT_STATE_INACTIVE;
    1152                 :            : }
    1153                 :            : 
    1154                 :            : /*
    1155                 :            :  * Called at perf_event creation and when events are attached/detached from a
    1156                 :            :  * group.
    1157                 :            :  */
    1158                 :          0 : static void perf_event__read_size(struct perf_event *event)
    1159                 :            : {
    1160                 :            :         int entry = sizeof(u64); /* value */
    1161                 :            :         int size = 0;
    1162                 :            :         int nr = 1;
    1163                 :            : 
    1164         [ #  # ]:          0 :         if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
    1165                 :            :                 size += sizeof(u64);
    1166                 :            : 
    1167         [ #  # ]:          0 :         if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
    1168                 :          0 :                 size += sizeof(u64);
    1169                 :            : 
    1170         [ #  # ]:          0 :         if (event->attr.read_format & PERF_FORMAT_ID)
    1171                 :            :                 entry += sizeof(u64);
    1172                 :            : 
    1173         [ #  # ]:          0 :         if (event->attr.read_format & PERF_FORMAT_GROUP) {
    1174                 :          0 :                 nr += event->group_leader->nr_siblings;
    1175                 :          0 :                 size += sizeof(u64);
    1176                 :            :         }
    1177                 :            : 
    1178                 :          0 :         size += entry * nr;
    1179                 :          0 :         event->read_size = size;
    1180                 :          0 : }
    1181                 :            : 
    1182                 :          0 : static void perf_event__header_size(struct perf_event *event)
    1183                 :            : {
    1184                 :            :         struct perf_sample_data *data;
    1185                 :          0 :         u64 sample_type = event->attr.sample_type;
    1186                 :            :         u16 size = 0;
    1187                 :            : 
    1188                 :          0 :         perf_event__read_size(event);
    1189                 :            : 
    1190         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IP)
    1191                 :            :                 size += sizeof(data->ip);
    1192                 :            : 
    1193         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_ADDR)
    1194                 :          0 :                 size += sizeof(data->addr);
    1195                 :            : 
    1196         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_PERIOD)
    1197                 :          0 :                 size += sizeof(data->period);
    1198                 :            : 
    1199         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_WEIGHT)
    1200                 :          0 :                 size += sizeof(data->weight);
    1201                 :            : 
    1202         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_READ)
    1203                 :          0 :                 size += event->read_size;
    1204                 :            : 
    1205         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_DATA_SRC)
    1206                 :          0 :                 size += sizeof(data->data_src.val);
    1207                 :            : 
    1208         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TRANSACTION)
    1209                 :          0 :                 size += sizeof(data->txn);
    1210                 :            : 
    1211                 :          0 :         event->header_size = size;
    1212                 :          0 : }
    1213                 :            : 
    1214                 :          0 : static void perf_event__id_header_size(struct perf_event *event)
    1215                 :            : {
    1216                 :            :         struct perf_sample_data *data;
    1217                 :          0 :         u64 sample_type = event->attr.sample_type;
    1218                 :            :         u16 size = 0;
    1219                 :            : 
    1220         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TID)
    1221                 :            :                 size += sizeof(data->tid_entry);
    1222                 :            : 
    1223         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TIME)
    1224                 :          0 :                 size += sizeof(data->time);
    1225                 :            : 
    1226         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IDENTIFIER)
    1227                 :          0 :                 size += sizeof(data->id);
    1228                 :            : 
    1229         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_ID)
    1230                 :          0 :                 size += sizeof(data->id);
    1231                 :            : 
    1232         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STREAM_ID)
    1233                 :          0 :                 size += sizeof(data->stream_id);
    1234                 :            : 
    1235         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CPU)
    1236                 :          0 :                 size += sizeof(data->cpu_entry);
    1237                 :            : 
    1238                 :          0 :         event->id_header_size = size;
    1239                 :          0 : }
    1240                 :            : 
    1241                 :          0 : static void perf_group_attach(struct perf_event *event)
    1242                 :            : {
    1243                 :          0 :         struct perf_event *group_leader = event->group_leader, *pos;
    1244                 :            : 
    1245                 :            :         /*
    1246                 :            :          * We can have double attach due to group movement in perf_event_open.
    1247                 :            :          */
    1248         [ #  # ]:          0 :         if (event->attach_state & PERF_ATTACH_GROUP)
    1249                 :            :                 return;
    1250                 :            : 
    1251                 :          0 :         event->attach_state |= PERF_ATTACH_GROUP;
    1252                 :            : 
    1253         [ #  # ]:          0 :         if (group_leader == event)
    1254                 :            :                 return;
    1255                 :            : 
    1256 [ #  # ][ #  # ]:          0 :         if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
    1257                 :            :                         !is_software_event(event))
    1258                 :          0 :                 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
    1259                 :            : 
    1260                 :          0 :         list_add_tail(&event->group_entry, &group_leader->sibling_list);
    1261                 :          0 :         group_leader->nr_siblings++;
    1262                 :            : 
    1263                 :          0 :         perf_event__header_size(group_leader);
    1264                 :            : 
    1265         [ #  # ]:          0 :         list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
    1266                 :          0 :                 perf_event__header_size(pos);
    1267                 :            : }
    1268                 :            : 
    1269                 :            : /*
    1270                 :            :  * Remove a event from the lists for its context.
    1271                 :            :  * Must be called with ctx->mutex and ctx->lock held.
    1272                 :            :  */
    1273                 :            : static void
    1274                 :          0 : list_del_event(struct perf_event *event, struct perf_event_context *ctx)
    1275                 :            : {
    1276                 :            :         struct perf_cpu_context *cpuctx;
    1277                 :            :         /*
    1278                 :            :          * We can have double detach due to exit/hot-unplug + close.
    1279                 :            :          */
    1280         [ #  # ]:          0 :         if (!(event->attach_state & PERF_ATTACH_CONTEXT))
    1281                 :          0 :                 return;
    1282                 :            : 
    1283                 :          0 :         event->attach_state &= ~PERF_ATTACH_CONTEXT;
    1284                 :            : 
    1285                 :            :         if (is_cgroup_event(event)) {
    1286                 :            :                 ctx->nr_cgroups--;
    1287                 :            :                 cpuctx = __get_cpu_context(ctx);
    1288                 :            :                 /*
    1289                 :            :                  * if there are no more cgroup events
    1290                 :            :                  * then cler cgrp to avoid stale pointer
    1291                 :            :                  * in update_cgrp_time_from_cpuctx()
    1292                 :            :                  */
    1293                 :            :                 if (!ctx->nr_cgroups)
    1294                 :            :                         cpuctx->cgrp = NULL;
    1295                 :            :         }
    1296                 :            : 
    1297         [ #  # ]:          0 :         if (has_branch_stack(event))
    1298                 :          0 :                 ctx->nr_branch_stack--;
    1299                 :            : 
    1300                 :          0 :         ctx->nr_events--;
    1301         [ #  # ]:          0 :         if (event->attr.inherit_stat)
    1302                 :          0 :                 ctx->nr_stat--;
    1303                 :            : 
    1304                 :            :         list_del_rcu(&event->event_entry);
    1305                 :            : 
    1306         [ #  # ]:          0 :         if (event->group_leader == event)
    1307                 :          0 :                 list_del_init(&event->group_entry);
    1308                 :            : 
    1309                 :          0 :         update_group_times(event);
    1310                 :            : 
    1311                 :            :         /*
    1312                 :            :          * If event was in error state, then keep it
    1313                 :            :          * that way, otherwise bogus counts will be
    1314                 :            :          * returned on read(). The only way to get out
    1315                 :            :          * of error state is by explicit re-enabling
    1316                 :            :          * of the event
    1317                 :            :          */
    1318         [ #  # ]:          0 :         if (event->state > PERF_EVENT_STATE_OFF)
    1319                 :          0 :                 event->state = PERF_EVENT_STATE_OFF;
    1320                 :            : 
    1321                 :          0 :         ctx->generation++;
    1322                 :            : }
    1323                 :            : 
    1324                 :          0 : static void perf_group_detach(struct perf_event *event)
    1325                 :            : {
    1326                 :            :         struct perf_event *sibling, *tmp;
    1327                 :            :         struct list_head *list = NULL;
    1328                 :            : 
    1329                 :            :         /*
    1330                 :            :          * We can have double detach due to exit/hot-unplug + close.
    1331                 :            :          */
    1332         [ #  # ]:          0 :         if (!(event->attach_state & PERF_ATTACH_GROUP))
    1333                 :          0 :                 return;
    1334                 :            : 
    1335                 :          0 :         event->attach_state &= ~PERF_ATTACH_GROUP;
    1336                 :            : 
    1337                 :            :         /*
    1338                 :            :          * If this is a sibling, remove it from its group.
    1339                 :            :          */
    1340         [ #  # ]:          0 :         if (event->group_leader != event) {
    1341                 :          0 :                 list_del_init(&event->group_entry);
    1342                 :          0 :                 event->group_leader->nr_siblings--;
    1343                 :          0 :                 goto out;
    1344                 :            :         }
    1345                 :            : 
    1346         [ #  # ]:          0 :         if (!list_empty(&event->group_entry))
    1347                 :            :                 list = &event->group_entry;
    1348                 :            : 
    1349                 :            :         /*
    1350                 :            :          * If this was a group event with sibling events then
    1351                 :            :          * upgrade the siblings to singleton events by adding them
    1352                 :            :          * to whatever list we are on.
    1353                 :            :          */
    1354         [ #  # ]:          0 :         list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
    1355         [ #  # ]:          0 :                 if (list)
    1356                 :            :                         list_move_tail(&sibling->group_entry, list);
    1357                 :          0 :                 sibling->group_leader = sibling;
    1358                 :            : 
    1359                 :            :                 /* Inherit group flags from the previous leader */
    1360                 :          0 :                 sibling->group_flags = event->group_flags;
    1361                 :            :         }
    1362                 :            : 
    1363                 :            : out:
    1364                 :          0 :         perf_event__header_size(event->group_leader);
    1365                 :            : 
    1366         [ #  # ]:          0 :         list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
    1367                 :          0 :                 perf_event__header_size(tmp);
    1368                 :            : }
    1369                 :            : 
    1370                 :            : static inline int
    1371                 :            : event_filter_match(struct perf_event *event)
    1372                 :            : {
    1373 [ #  # ][ #  # ]:          0 :         return (event->cpu == -1 || event->cpu == smp_processor_id())
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    1374 [ #  # ][ #  # ]:          0 :             && perf_cgroup_match(event);
         [ #  # ][ #  # ]
           [ #  #  #  # ]
    1375                 :            : }
    1376                 :            : 
    1377                 :            : static void
    1378                 :          0 : event_sched_out(struct perf_event *event,
    1379                 :            :                   struct perf_cpu_context *cpuctx,
    1380                 :            :                   struct perf_event_context *ctx)
    1381                 :            : {
    1382                 :            :         u64 tstamp = perf_event_time(event);
    1383                 :            :         u64 delta;
    1384                 :            :         /*
    1385                 :            :          * An event which could not be activated because of
    1386                 :            :          * filter mismatch still needs to have its timings
    1387                 :            :          * maintained, otherwise bogus information is return
    1388                 :            :          * via read() for time_enabled, time_running:
    1389                 :            :          */
    1390         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_INACTIVE
    1391         [ #  # ]:          0 :             && !event_filter_match(event)) {
    1392                 :          0 :                 delta = tstamp - event->tstamp_stopped;
    1393                 :          0 :                 event->tstamp_running += delta;
    1394                 :          0 :                 event->tstamp_stopped = tstamp;
    1395                 :            :         }
    1396                 :            : 
    1397         [ #  # ]:          0 :         if (event->state != PERF_EVENT_STATE_ACTIVE)
    1398                 :          0 :                 return;
    1399                 :            : 
    1400                 :          0 :         perf_pmu_disable(event->pmu);
    1401                 :            : 
    1402                 :          0 :         event->state = PERF_EVENT_STATE_INACTIVE;
    1403         [ #  # ]:          0 :         if (event->pending_disable) {
    1404                 :          0 :                 event->pending_disable = 0;
    1405                 :          0 :                 event->state = PERF_EVENT_STATE_OFF;
    1406                 :            :         }
    1407                 :          0 :         event->tstamp_stopped = tstamp;
    1408                 :          0 :         event->pmu->del(event, 0);
    1409                 :          0 :         event->oncpu = -1;
    1410                 :            : 
    1411         [ #  # ]:          0 :         if (!is_software_event(event))
    1412                 :          0 :                 cpuctx->active_oncpu--;
    1413                 :          0 :         ctx->nr_active--;
    1414 [ #  # ][ #  # ]:          0 :         if (event->attr.freq && event->attr.sample_freq)
    1415                 :          0 :                 ctx->nr_freq--;
    1416 [ #  # ][ #  # ]:          0 :         if (event->attr.exclusive || !cpuctx->active_oncpu)
    1417                 :          0 :                 cpuctx->exclusive = 0;
    1418                 :            : 
    1419                 :          0 :         perf_pmu_enable(event->pmu);
    1420                 :            : }
    1421                 :            : 
    1422                 :            : static void
    1423                 :          0 : group_sched_out(struct perf_event *group_event,
    1424                 :            :                 struct perf_cpu_context *cpuctx,
    1425                 :            :                 struct perf_event_context *ctx)
    1426                 :            : {
    1427                 :            :         struct perf_event *event;
    1428                 :          0 :         int state = group_event->state;
    1429                 :            : 
    1430                 :          0 :         event_sched_out(group_event, cpuctx, ctx);
    1431                 :            : 
    1432                 :            :         /*
    1433                 :            :          * Schedule out siblings (if any):
    1434                 :            :          */
    1435         [ #  # ]:          0 :         list_for_each_entry(event, &group_event->sibling_list, group_entry)
    1436                 :          0 :                 event_sched_out(event, cpuctx, ctx);
    1437                 :            : 
    1438 [ #  # ][ #  # ]:          0 :         if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
    1439                 :          0 :                 cpuctx->exclusive = 0;
    1440                 :          0 : }
    1441                 :            : 
    1442                 :            : /*
    1443                 :            :  * Cross CPU call to remove a performance event
    1444                 :            :  *
    1445                 :            :  * We disable the event on the hardware level first. After that we
    1446                 :            :  * remove it from the context list.
    1447                 :            :  */
    1448                 :          0 : static int __perf_remove_from_context(void *info)
    1449                 :            : {
    1450                 :            :         struct perf_event *event = info;
    1451                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1452                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    1453                 :            : 
    1454                 :          0 :         raw_spin_lock(&ctx->lock);
    1455                 :          0 :         event_sched_out(event, cpuctx, ctx);
    1456                 :          0 :         list_del_event(event, ctx);
    1457 [ #  # ][ #  # ]:          0 :         if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
    1458                 :          0 :                 ctx->is_active = 0;
    1459                 :          0 :                 cpuctx->task_ctx = NULL;
    1460                 :            :         }
    1461                 :            :         raw_spin_unlock(&ctx->lock);
    1462                 :            : 
    1463                 :          0 :         return 0;
    1464                 :            : }
    1465                 :            : 
    1466                 :            : 
    1467                 :            : /*
    1468                 :            :  * Remove the event from a task's (or a CPU's) list of events.
    1469                 :            :  *
    1470                 :            :  * CPU events are removed with a smp call. For task events we only
    1471                 :            :  * call when the task is on a CPU.
    1472                 :            :  *
    1473                 :            :  * If event->ctx is a cloned context, callers must make sure that
    1474                 :            :  * every task struct that event->ctx->task could possibly point to
    1475                 :            :  * remains valid.  This is OK when called from perf_release since
    1476                 :            :  * that only calls us on the top-level context, which can't be a clone.
    1477                 :            :  * When called from perf_event_exit_task, it's OK because the
    1478                 :            :  * context has been detached from its task.
    1479                 :            :  */
    1480                 :          0 : static void perf_remove_from_context(struct perf_event *event)
    1481                 :            : {
    1482                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1483                 :          0 :         struct task_struct *task = ctx->task;
    1484                 :            : 
    1485                 :            :         lockdep_assert_held(&ctx->mutex);
    1486                 :            : 
    1487         [ #  # ]:          0 :         if (!task) {
    1488                 :            :                 /*
    1489                 :            :                  * Per cpu events are removed via an smp call and
    1490                 :            :                  * the removal is always successful.
    1491                 :            :                  */
    1492                 :          0 :                 cpu_function_call(event->cpu, __perf_remove_from_context, event);
    1493                 :          0 :                 return;
    1494                 :            :         }
    1495                 :            : 
    1496                 :            : retry:
    1497         [ #  # ]:          0 :         if (!task_function_call(task, __perf_remove_from_context, event))
    1498                 :            :                 return;
    1499                 :            : 
    1500                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    1501                 :            :         /*
    1502                 :            :          * If we failed to find a running task, but find the context active now
    1503                 :            :          * that we've acquired the ctx->lock, retry.
    1504                 :            :          */
    1505         [ #  # ]:          0 :         if (ctx->is_active) {
    1506                 :            :                 raw_spin_unlock_irq(&ctx->lock);
    1507                 :            :                 goto retry;
    1508                 :            :         }
    1509                 :            : 
    1510                 :            :         /*
    1511                 :            :          * Since the task isn't running, its safe to remove the event, us
    1512                 :            :          * holding the ctx->lock ensures the task won't get scheduled in.
    1513                 :            :          */
    1514                 :          0 :         list_del_event(event, ctx);
    1515                 :            :         raw_spin_unlock_irq(&ctx->lock);
    1516                 :            : }
    1517                 :            : 
    1518                 :            : /*
    1519                 :            :  * Cross CPU call to disable a performance event
    1520                 :            :  */
    1521                 :          0 : int __perf_event_disable(void *info)
    1522                 :            : {
    1523                 :            :         struct perf_event *event = info;
    1524                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1525                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    1526                 :            : 
    1527                 :            :         /*
    1528                 :            :          * If this is a per-task event, need to check whether this
    1529                 :            :          * event's task is the current task on this cpu.
    1530                 :            :          *
    1531                 :            :          * Can trigger due to concurrent perf_event_context_sched_out()
    1532                 :            :          * flipping contexts around.
    1533                 :            :          */
    1534 [ #  # ][ #  # ]:          0 :         if (ctx->task && cpuctx->task_ctx != ctx)
    1535                 :            :                 return -EINVAL;
    1536                 :            : 
    1537                 :          0 :         raw_spin_lock(&ctx->lock);
    1538                 :            : 
    1539                 :            :         /*
    1540                 :            :          * If the event is on, turn it off.
    1541                 :            :          * If it is in error state, leave it in error state.
    1542                 :            :          */
    1543         [ #  # ]:          0 :         if (event->state >= PERF_EVENT_STATE_INACTIVE) {
    1544                 :          0 :                 update_context_time(ctx);
    1545                 :            :                 update_cgrp_time_from_event(event);
    1546                 :          0 :                 update_group_times(event);
    1547         [ #  # ]:          0 :                 if (event == event->group_leader)
    1548                 :          0 :                         group_sched_out(event, cpuctx, ctx);
    1549                 :            :                 else
    1550                 :          0 :                         event_sched_out(event, cpuctx, ctx);
    1551                 :          0 :                 event->state = PERF_EVENT_STATE_OFF;
    1552                 :            :         }
    1553                 :            : 
    1554                 :            :         raw_spin_unlock(&ctx->lock);
    1555                 :            : 
    1556                 :          0 :         return 0;
    1557                 :            : }
    1558                 :            : 
    1559                 :            : /*
    1560                 :            :  * Disable a event.
    1561                 :            :  *
    1562                 :            :  * If event->ctx is a cloned context, callers must make sure that
    1563                 :            :  * every task struct that event->ctx->task could possibly point to
    1564                 :            :  * remains valid.  This condition is satisifed when called through
    1565                 :            :  * perf_event_for_each_child or perf_event_for_each because they
    1566                 :            :  * hold the top-level event's child_mutex, so any descendant that
    1567                 :            :  * goes to exit will block in sync_child_event.
    1568                 :            :  * When called from perf_pending_event it's OK because event->ctx
    1569                 :            :  * is the current context on this CPU and preemption is disabled,
    1570                 :            :  * hence we can't get into perf_event_task_sched_out for this context.
    1571                 :            :  */
    1572                 :          0 : void perf_event_disable(struct perf_event *event)
    1573                 :            : {
    1574                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1575                 :          0 :         struct task_struct *task = ctx->task;
    1576                 :            : 
    1577         [ #  # ]:          0 :         if (!task) {
    1578                 :            :                 /*
    1579                 :            :                  * Disable the event on the cpu that it's on
    1580                 :            :                  */
    1581                 :          0 :                 cpu_function_call(event->cpu, __perf_event_disable, event);
    1582                 :          0 :                 return;
    1583                 :            :         }
    1584                 :            : 
    1585                 :            : retry:
    1586         [ #  # ]:          0 :         if (!task_function_call(task, __perf_event_disable, event))
    1587                 :            :                 return;
    1588                 :            : 
    1589                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    1590                 :            :         /*
    1591                 :            :          * If the event is still active, we need to retry the cross-call.
    1592                 :            :          */
    1593         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_ACTIVE) {
    1594                 :            :                 raw_spin_unlock_irq(&ctx->lock);
    1595                 :            :                 /*
    1596                 :            :                  * Reload the task pointer, it might have been changed by
    1597                 :            :                  * a concurrent perf_event_context_sched_out().
    1598                 :            :                  */
    1599                 :          0 :                 task = ctx->task;
    1600                 :          0 :                 goto retry;
    1601                 :            :         }
    1602                 :            : 
    1603                 :            :         /*
    1604                 :            :          * Since we have the lock this context can't be scheduled
    1605                 :            :          * in, so we can change the state safely.
    1606                 :            :          */
    1607         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_INACTIVE) {
    1608                 :          0 :                 update_group_times(event);
    1609                 :          0 :                 event->state = PERF_EVENT_STATE_OFF;
    1610                 :            :         }
    1611                 :            :         raw_spin_unlock_irq(&ctx->lock);
    1612                 :            : }
    1613                 :            : EXPORT_SYMBOL_GPL(perf_event_disable);
    1614                 :            : 
    1615                 :            : static void perf_set_shadow_time(struct perf_event *event,
    1616                 :            :                                  struct perf_event_context *ctx,
    1617                 :            :                                  u64 tstamp)
    1618                 :            : {
    1619                 :            :         /*
    1620                 :            :          * use the correct time source for the time snapshot
    1621                 :            :          *
    1622                 :            :          * We could get by without this by leveraging the
    1623                 :            :          * fact that to get to this function, the caller
    1624                 :            :          * has most likely already called update_context_time()
    1625                 :            :          * and update_cgrp_time_xx() and thus both timestamp
    1626                 :            :          * are identical (or very close). Given that tstamp is,
    1627                 :            :          * already adjusted for cgroup, we could say that:
    1628                 :            :          *    tstamp - ctx->timestamp
    1629                 :            :          * is equivalent to
    1630                 :            :          *    tstamp - cgrp->timestamp.
    1631                 :            :          *
    1632                 :            :          * Then, in perf_output_read(), the calculation would
    1633                 :            :          * work with no changes because:
    1634                 :            :          * - event is guaranteed scheduled in
    1635                 :            :          * - no scheduled out in between
    1636                 :            :          * - thus the timestamp would be the same
    1637                 :            :          *
    1638                 :            :          * But this is a bit hairy.
    1639                 :            :          *
    1640                 :            :          * So instead, we have an explicit cgroup call to remain
    1641                 :            :          * within the time time source all along. We believe it
    1642                 :            :          * is cleaner and simpler to understand.
    1643                 :            :          */
    1644                 :            :         if (is_cgroup_event(event))
    1645                 :            :                 perf_cgroup_set_shadow_time(event, tstamp);
    1646                 :            :         else
    1647                 :          0 :                 event->shadow_ctx_time = tstamp - ctx->timestamp;
    1648                 :            : }
    1649                 :            : 
    1650                 :            : #define MAX_INTERRUPTS (~0ULL)
    1651                 :            : 
    1652                 :            : static void perf_log_throttle(struct perf_event *event, int enable);
    1653                 :            : 
    1654                 :            : static int
    1655                 :          0 : event_sched_in(struct perf_event *event,
    1656                 :            :                  struct perf_cpu_context *cpuctx,
    1657                 :          0 :                  struct perf_event_context *ctx)
    1658                 :            : {
    1659                 :            :         u64 tstamp = perf_event_time(event);
    1660                 :            :         int ret = 0;
    1661                 :            : 
    1662         [ #  # ]:          0 :         if (event->state <= PERF_EVENT_STATE_OFF)
    1663                 :            :                 return 0;
    1664                 :            : 
    1665                 :          0 :         event->state = PERF_EVENT_STATE_ACTIVE;
    1666                 :          0 :         event->oncpu = smp_processor_id();
    1667                 :            : 
    1668                 :            :         /*
    1669                 :            :          * Unthrottle events, since we scheduled we might have missed several
    1670                 :            :          * ticks already, also for a heavily scheduling task there is little
    1671                 :            :          * guarantee it'll get a tick in a timely manner.
    1672                 :            :          */
    1673         [ #  # ]:          0 :         if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
    1674                 :          0 :                 perf_log_throttle(event, 1);
    1675                 :          0 :                 event->hw.interrupts = 0;
    1676                 :            :         }
    1677                 :            : 
    1678                 :            :         /*
    1679                 :            :          * The new state must be visible before we turn it on in the hardware:
    1680                 :            :          */
    1681                 :          0 :         smp_wmb();
    1682                 :            : 
    1683                 :          0 :         perf_pmu_disable(event->pmu);
    1684                 :            : 
    1685         [ #  # ]:          0 :         if (event->pmu->add(event, PERF_EF_START)) {
    1686                 :          0 :                 event->state = PERF_EVENT_STATE_INACTIVE;
    1687                 :          0 :                 event->oncpu = -1;
    1688                 :            :                 ret = -EAGAIN;
    1689                 :            :                 goto out;
    1690                 :            :         }
    1691                 :            : 
    1692                 :          0 :         event->tstamp_running += tstamp - event->tstamp_stopped;
    1693                 :            : 
    1694                 :            :         perf_set_shadow_time(event, ctx, tstamp);
    1695                 :            : 
    1696         [ #  # ]:          0 :         if (!is_software_event(event))
    1697                 :          0 :                 cpuctx->active_oncpu++;
    1698                 :          0 :         ctx->nr_active++;
    1699 [ #  # ][ #  # ]:          0 :         if (event->attr.freq && event->attr.sample_freq)
    1700                 :          0 :                 ctx->nr_freq++;
    1701                 :            : 
    1702         [ #  # ]:          0 :         if (event->attr.exclusive)
    1703                 :          0 :                 cpuctx->exclusive = 1;
    1704                 :            : 
    1705                 :            : out:
    1706                 :          0 :         perf_pmu_enable(event->pmu);
    1707                 :            : 
    1708                 :            :         return ret;
    1709                 :            : }
    1710                 :            : 
    1711                 :            : static int
    1712                 :          0 : group_sched_in(struct perf_event *group_event,
    1713                 :            :                struct perf_cpu_context *cpuctx,
    1714                 :            :                struct perf_event_context *ctx)
    1715                 :            : {
    1716                 :            :         struct perf_event *event, *partial_group = NULL;
    1717                 :          0 :         struct pmu *pmu = group_event->pmu;
    1718                 :          0 :         u64 now = ctx->time;
    1719                 :            :         bool simulate = false;
    1720                 :            : 
    1721         [ #  # ]:          0 :         if (group_event->state == PERF_EVENT_STATE_OFF)
    1722                 :            :                 return 0;
    1723                 :            : 
    1724                 :          0 :         pmu->start_txn(pmu);
    1725                 :            : 
    1726         [ #  # ]:          0 :         if (event_sched_in(group_event, cpuctx, ctx)) {
    1727                 :          0 :                 pmu->cancel_txn(pmu);
    1728                 :          0 :                 perf_cpu_hrtimer_restart(cpuctx);
    1729                 :          0 :                 return -EAGAIN;
    1730                 :            :         }
    1731                 :            : 
    1732                 :            :         /*
    1733                 :            :          * Schedule in siblings as one group (if any):
    1734                 :            :          */
    1735         [ #  # ]:          0 :         list_for_each_entry(event, &group_event->sibling_list, group_entry) {
    1736         [ #  # ]:          0 :                 if (event_sched_in(event, cpuctx, ctx)) {
    1737                 :            :                         partial_group = event;
    1738                 :            :                         goto group_error;
    1739                 :            :                 }
    1740                 :            :         }
    1741                 :            : 
    1742         [ #  # ]:          0 :         if (!pmu->commit_txn(pmu))
    1743                 :            :                 return 0;
    1744                 :            : 
    1745                 :            : group_error:
    1746                 :            :         /*
    1747                 :            :          * Groups can be scheduled in as one unit only, so undo any
    1748                 :            :          * partial group before returning:
    1749                 :            :          * The events up to the failed event are scheduled out normally,
    1750                 :            :          * tstamp_stopped will be updated.
    1751                 :            :          *
    1752                 :            :          * The failed events and the remaining siblings need to have
    1753                 :            :          * their timings updated as if they had gone thru event_sched_in()
    1754                 :            :          * and event_sched_out(). This is required to get consistent timings
    1755                 :            :          * across the group. This also takes care of the case where the group
    1756                 :            :          * could never be scheduled by ensuring tstamp_stopped is set to mark
    1757                 :            :          * the time the event was actually stopped, such that time delta
    1758                 :            :          * calculation in update_event_times() is correct.
    1759                 :            :          */
    1760         [ #  # ]:          0 :         list_for_each_entry(event, &group_event->sibling_list, group_entry) {
    1761         [ #  # ]:          0 :                 if (event == partial_group)
    1762                 :            :                         simulate = true;
    1763                 :            : 
    1764         [ #  # ]:          0 :                 if (simulate) {
    1765                 :          0 :                         event->tstamp_running += now - event->tstamp_stopped;
    1766                 :          0 :                         event->tstamp_stopped = now;
    1767                 :            :                 } else {
    1768                 :          0 :                         event_sched_out(event, cpuctx, ctx);
    1769                 :            :                 }
    1770                 :            :         }
    1771                 :          0 :         event_sched_out(group_event, cpuctx, ctx);
    1772                 :            : 
    1773                 :          0 :         pmu->cancel_txn(pmu);
    1774                 :            : 
    1775                 :          0 :         perf_cpu_hrtimer_restart(cpuctx);
    1776                 :            : 
    1777                 :          0 :         return -EAGAIN;
    1778                 :            : }
    1779                 :            : 
    1780                 :            : /*
    1781                 :            :  * Work out whether we can put this event group on the CPU now.
    1782                 :            :  */
    1783                 :            : static int group_can_go_on(struct perf_event *event,
    1784                 :            :                            struct perf_cpu_context *cpuctx,
    1785                 :            :                            int can_add_hw)
    1786                 :            : {
    1787                 :            :         /*
    1788                 :            :          * Groups consisting entirely of software events can always go on.
    1789                 :            :          */
    1790 [ #  # ][ #  # ]:          0 :         if (event->group_flags & PERF_GROUP_SOFTWARE)
                 [ #  # ]
    1791                 :            :                 return 1;
    1792                 :            :         /*
    1793                 :            :          * If an exclusive group is already on, no other hardware
    1794                 :            :          * events can go on.
    1795                 :            :          */
    1796 [ #  # ][ #  # ]:          0 :         if (cpuctx->exclusive)
                 [ #  # ]
    1797                 :            :                 return 0;
    1798                 :            :         /*
    1799                 :            :          * If this group is exclusive and there are already
    1800                 :            :          * events on the CPU, it can't go on.
    1801                 :            :          */
    1802 [ #  # ][ #  # ]:          0 :         if (event->attr.exclusive && cpuctx->active_oncpu)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    1803                 :            :                 return 0;
    1804                 :            :         /*
    1805                 :            :          * Otherwise, try to add it if all previous groups were able
    1806                 :            :          * to go on.
    1807                 :            :          */
    1808                 :            :         return can_add_hw;
    1809                 :            : }
    1810                 :            : 
    1811                 :          0 : static void add_event_to_ctx(struct perf_event *event,
    1812                 :            :                                struct perf_event_context *ctx)
    1813                 :            : {
    1814                 :            :         u64 tstamp = perf_event_time(event);
    1815                 :            : 
    1816                 :          0 :         list_add_event(event, ctx);
    1817                 :          0 :         perf_group_attach(event);
    1818                 :          0 :         event->tstamp_enabled = tstamp;
    1819                 :          0 :         event->tstamp_running = tstamp;
    1820                 :          0 :         event->tstamp_stopped = tstamp;
    1821                 :          0 : }
    1822                 :            : 
    1823                 :            : static void task_ctx_sched_out(struct perf_event_context *ctx);
    1824                 :            : static void
    1825                 :            : ctx_sched_in(struct perf_event_context *ctx,
    1826                 :            :              struct perf_cpu_context *cpuctx,
    1827                 :            :              enum event_type_t event_type,
    1828                 :            :              struct task_struct *task);
    1829                 :            : 
    1830                 :          0 : static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
    1831                 :            :                                 struct perf_event_context *ctx,
    1832                 :            :                                 struct task_struct *task)
    1833                 :            : {
    1834                 :            :         cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
    1835         [ #  # ]:          0 :         if (ctx)
    1836                 :          0 :                 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
    1837                 :            :         cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
    1838         [ #  # ]:          0 :         if (ctx)
    1839                 :          0 :                 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
    1840                 :          0 : }
    1841                 :            : 
    1842                 :            : /*
    1843                 :            :  * Cross CPU call to install and enable a performance event
    1844                 :            :  *
    1845                 :            :  * Must be called with ctx->mutex held
    1846                 :            :  */
    1847                 :          0 : static int  __perf_install_in_context(void *info)
    1848                 :            : {
    1849                 :            :         struct perf_event *event = info;
    1850                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1851                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    1852                 :          0 :         struct perf_event_context *task_ctx = cpuctx->task_ctx;
    1853                 :            :         struct task_struct *task = current;
    1854                 :            : 
    1855                 :            :         perf_ctx_lock(cpuctx, task_ctx);
    1856                 :          0 :         perf_pmu_disable(cpuctx->ctx.pmu);
    1857                 :            : 
    1858                 :            :         /*
    1859                 :            :          * If there was an active task_ctx schedule it out.
    1860                 :            :          */
    1861         [ #  # ]:          0 :         if (task_ctx)
    1862                 :          0 :                 task_ctx_sched_out(task_ctx);
    1863                 :            : 
    1864                 :            :         /*
    1865                 :            :          * If the context we're installing events in is not the
    1866                 :            :          * active task_ctx, flip them.
    1867                 :            :          */
    1868 [ #  # ][ #  # ]:          0 :         if (ctx->task && task_ctx != ctx) {
    1869         [ #  # ]:          0 :                 if (task_ctx)
    1870                 :            :                         raw_spin_unlock(&task_ctx->lock);
    1871                 :          0 :                 raw_spin_lock(&ctx->lock);
    1872                 :            :                 task_ctx = ctx;
    1873                 :            :         }
    1874                 :            : 
    1875         [ #  # ]:          0 :         if (task_ctx) {
    1876                 :          0 :                 cpuctx->task_ctx = task_ctx;
    1877                 :            :                 task = task_ctx->task;
    1878                 :            :         }
    1879                 :            : 
    1880                 :            :         cpu_ctx_sched_out(cpuctx, EVENT_ALL);
    1881                 :            : 
    1882                 :          0 :         update_context_time(ctx);
    1883                 :            :         /*
    1884                 :            :          * update cgrp time only if current cgrp
    1885                 :            :          * matches event->cgrp. Must be done before
    1886                 :            :          * calling add_event_to_ctx()
    1887                 :            :          */
    1888                 :            :         update_cgrp_time_from_event(event);
    1889                 :            : 
    1890                 :          0 :         add_event_to_ctx(event, ctx);
    1891                 :            : 
    1892                 :            :         /*
    1893                 :            :          * Schedule everything back in
    1894                 :            :          */
    1895                 :          0 :         perf_event_sched_in(cpuctx, task_ctx, task);
    1896                 :            : 
    1897                 :          0 :         perf_pmu_enable(cpuctx->ctx.pmu);
    1898                 :          0 :         perf_ctx_unlock(cpuctx, task_ctx);
    1899                 :            : 
    1900                 :          0 :         return 0;
    1901                 :            : }
    1902                 :            : 
    1903                 :            : /*
    1904                 :            :  * Attach a performance event to a context
    1905                 :            :  *
    1906                 :            :  * First we add the event to the list with the hardware enable bit
    1907                 :            :  * in event->hw_config cleared.
    1908                 :            :  *
    1909                 :            :  * If the event is attached to a task which is on a CPU we use a smp
    1910                 :            :  * call to enable it in the task context. The task might have been
    1911                 :            :  * scheduled away, but we check this in the smp call again.
    1912                 :            :  */
    1913                 :            : static void
    1914                 :          0 : perf_install_in_context(struct perf_event_context *ctx,
    1915                 :            :                         struct perf_event *event,
    1916                 :            :                         int cpu)
    1917                 :            : {
    1918                 :          0 :         struct task_struct *task = ctx->task;
    1919                 :            : 
    1920                 :            :         lockdep_assert_held(&ctx->mutex);
    1921                 :            : 
    1922                 :          0 :         event->ctx = ctx;
    1923         [ #  # ]:          0 :         if (event->cpu != -1)
    1924                 :          0 :                 event->cpu = cpu;
    1925                 :            : 
    1926         [ #  # ]:          0 :         if (!task) {
    1927                 :            :                 /*
    1928                 :            :                  * Per cpu events are installed via an smp call and
    1929                 :            :                  * the install is always successful.
    1930                 :            :                  */
    1931                 :          0 :                 cpu_function_call(cpu, __perf_install_in_context, event);
    1932                 :          0 :                 return;
    1933                 :            :         }
    1934                 :            : 
    1935                 :            : retry:
    1936         [ #  # ]:          0 :         if (!task_function_call(task, __perf_install_in_context, event))
    1937                 :            :                 return;
    1938                 :            : 
    1939                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    1940                 :            :         /*
    1941                 :            :          * If we failed to find a running task, but find the context active now
    1942                 :            :          * that we've acquired the ctx->lock, retry.
    1943                 :            :          */
    1944         [ #  # ]:          0 :         if (ctx->is_active) {
    1945                 :            :                 raw_spin_unlock_irq(&ctx->lock);
    1946                 :            :                 goto retry;
    1947                 :            :         }
    1948                 :            : 
    1949                 :            :         /*
    1950                 :            :          * Since the task isn't running, its safe to add the event, us holding
    1951                 :            :          * the ctx->lock ensures the task won't get scheduled in.
    1952                 :            :          */
    1953                 :          0 :         add_event_to_ctx(event, ctx);
    1954                 :            :         raw_spin_unlock_irq(&ctx->lock);
    1955                 :            : }
    1956                 :            : 
    1957                 :            : /*
    1958                 :            :  * Put a event into inactive state and update time fields.
    1959                 :            :  * Enabling the leader of a group effectively enables all
    1960                 :            :  * the group members that aren't explicitly disabled, so we
    1961                 :            :  * have to update their ->tstamp_enabled also.
    1962                 :            :  * Note: this works for group members as well as group leaders
    1963                 :            :  * since the non-leader members' sibling_lists will be empty.
    1964                 :            :  */
    1965                 :          0 : static void __perf_event_mark_enabled(struct perf_event *event)
    1966                 :            : {
    1967                 :            :         struct perf_event *sub;
    1968                 :            :         u64 tstamp = perf_event_time(event);
    1969                 :            : 
    1970                 :          0 :         event->state = PERF_EVENT_STATE_INACTIVE;
    1971                 :          0 :         event->tstamp_enabled = tstamp - event->total_time_enabled;
    1972         [ #  # ]:          0 :         list_for_each_entry(sub, &event->sibling_list, group_entry) {
    1973         [ #  # ]:          0 :                 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
    1974                 :          0 :                         sub->tstamp_enabled = tstamp - sub->total_time_enabled;
    1975                 :            :         }
    1976                 :          0 : }
    1977                 :            : 
    1978                 :            : /*
    1979                 :            :  * Cross CPU call to enable a performance event
    1980                 :            :  */
    1981                 :          0 : static int __perf_event_enable(void *info)
    1982                 :            : {
    1983                 :          0 :         struct perf_event *event = info;
    1984                 :          0 :         struct perf_event_context *ctx = event->ctx;
    1985                 :          0 :         struct perf_event *leader = event->group_leader;
    1986                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    1987                 :            :         int err;
    1988                 :            : 
    1989                 :            :         /*
    1990                 :            :          * There's a time window between 'ctx->is_active' check
    1991                 :            :          * in perf_event_enable function and this place having:
    1992                 :            :          *   - IRQs on
    1993                 :            :          *   - ctx->lock unlocked
    1994                 :            :          *
    1995                 :            :          * where the task could be killed and 'ctx' deactivated
    1996                 :            :          * by perf_event_exit_task.
    1997                 :            :          */
    1998         [ #  # ]:          0 :         if (!ctx->is_active)
    1999                 :            :                 return -EINVAL;
    2000                 :            : 
    2001                 :          0 :         raw_spin_lock(&ctx->lock);
    2002                 :          0 :         update_context_time(ctx);
    2003                 :            : 
    2004         [ #  # ]:          0 :         if (event->state >= PERF_EVENT_STATE_INACTIVE)
    2005                 :            :                 goto unlock;
    2006                 :            : 
    2007                 :            :         /*
    2008                 :            :          * set current task's cgroup time reference point
    2009                 :            :          */
    2010                 :            :         perf_cgroup_set_timestamp(current, ctx);
    2011                 :            : 
    2012                 :          0 :         __perf_event_mark_enabled(event);
    2013                 :            : 
    2014         [ #  # ]:          0 :         if (!event_filter_match(event)) {
    2015                 :            :                 if (is_cgroup_event(event))
    2016                 :            :                         perf_cgroup_defer_enabled(event);
    2017                 :            :                 goto unlock;
    2018                 :            :         }
    2019                 :            : 
    2020                 :            :         /*
    2021                 :            :          * If the event is in a group and isn't the group leader,
    2022                 :            :          * then don't put it on unless the group is on.
    2023                 :            :          */
    2024 [ #  # ][ #  # ]:          0 :         if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
    2025                 :            :                 goto unlock;
    2026                 :            : 
    2027         [ #  # ]:          0 :         if (!group_can_go_on(event, cpuctx, 1)) {
    2028                 :            :                 err = -EEXIST;
    2029                 :            :         } else {
    2030         [ #  # ]:          0 :                 if (event == leader)
    2031                 :          0 :                         err = group_sched_in(event, cpuctx, ctx);
    2032                 :            :                 else
    2033                 :          0 :                         err = event_sched_in(event, cpuctx, ctx);
    2034                 :            :         }
    2035                 :            : 
    2036         [ #  # ]:          0 :         if (err) {
    2037                 :            :                 /*
    2038                 :            :                  * If this event can't go on and it's part of a
    2039                 :            :                  * group, then the whole group has to come off.
    2040                 :            :                  */
    2041         [ #  # ]:          0 :                 if (leader != event) {
    2042                 :          0 :                         group_sched_out(leader, cpuctx, ctx);
    2043                 :          0 :                         perf_cpu_hrtimer_restart(cpuctx);
    2044                 :            :                 }
    2045         [ #  # ]:          0 :                 if (leader->attr.pinned) {
    2046                 :          0 :                         update_group_times(leader);
    2047                 :          0 :                         leader->state = PERF_EVENT_STATE_ERROR;
    2048                 :            :                 }
    2049                 :            :         }
    2050                 :            : 
    2051                 :            : unlock:
    2052                 :            :         raw_spin_unlock(&ctx->lock);
    2053                 :            : 
    2054                 :          0 :         return 0;
    2055                 :            : }
    2056                 :            : 
    2057                 :            : /*
    2058                 :            :  * Enable a event.
    2059                 :            :  *
    2060                 :            :  * If event->ctx is a cloned context, callers must make sure that
    2061                 :            :  * every task struct that event->ctx->task could possibly point to
    2062                 :            :  * remains valid.  This condition is satisfied when called through
    2063                 :            :  * perf_event_for_each_child or perf_event_for_each as described
    2064                 :            :  * for perf_event_disable.
    2065                 :            :  */
    2066                 :          0 : void perf_event_enable(struct perf_event *event)
    2067                 :            : {
    2068                 :          0 :         struct perf_event_context *ctx = event->ctx;
    2069                 :          0 :         struct task_struct *task = ctx->task;
    2070                 :            : 
    2071         [ #  # ]:          0 :         if (!task) {
    2072                 :            :                 /*
    2073                 :            :                  * Enable the event on the cpu that it's on
    2074                 :            :                  */
    2075                 :          0 :                 cpu_function_call(event->cpu, __perf_event_enable, event);
    2076                 :          0 :                 return;
    2077                 :            :         }
    2078                 :            : 
    2079                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    2080         [ #  # ]:          0 :         if (event->state >= PERF_EVENT_STATE_INACTIVE)
    2081                 :            :                 goto out;
    2082                 :            : 
    2083                 :            :         /*
    2084                 :            :          * If the event is in error state, clear that first.
    2085                 :            :          * That way, if we see the event in error state below, we
    2086                 :            :          * know that it has gone back into error state, as distinct
    2087                 :            :          * from the task having been scheduled away before the
    2088                 :            :          * cross-call arrived.
    2089                 :            :          */
    2090         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_ERROR)
    2091                 :          0 :                 event->state = PERF_EVENT_STATE_OFF;
    2092                 :            : 
    2093                 :            : retry:
    2094         [ #  # ]:          0 :         if (!ctx->is_active) {
    2095                 :          0 :                 __perf_event_mark_enabled(event);
    2096                 :          0 :                 goto out;
    2097                 :            :         }
    2098                 :            : 
    2099                 :            :         raw_spin_unlock_irq(&ctx->lock);
    2100                 :            : 
    2101         [ #  # ]:          0 :         if (!task_function_call(task, __perf_event_enable, event))
    2102                 :            :                 return;
    2103                 :            : 
    2104                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    2105                 :            : 
    2106                 :            :         /*
    2107                 :            :          * If the context is active and the event is still off,
    2108                 :            :          * we need to retry the cross-call.
    2109                 :            :          */
    2110 [ #  # ][ #  # ]:          0 :         if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
    2111                 :            :                 /*
    2112                 :            :                  * task could have been flipped by a concurrent
    2113                 :            :                  * perf_event_context_sched_out()
    2114                 :            :                  */
    2115                 :          0 :                 task = ctx->task;
    2116                 :          0 :                 goto retry;
    2117                 :            :         }
    2118                 :            : 
    2119                 :            : out:
    2120                 :            :         raw_spin_unlock_irq(&ctx->lock);
    2121                 :            : }
    2122                 :            : EXPORT_SYMBOL_GPL(perf_event_enable);
    2123                 :            : 
    2124                 :          0 : int perf_event_refresh(struct perf_event *event, int refresh)
    2125                 :            : {
    2126                 :            :         /*
    2127                 :            :          * not supported on inherited events
    2128                 :            :          */
    2129 [ #  # ][ #  # ]:          0 :         if (event->attr.inherit || !is_sampling_event(event))
    2130                 :            :                 return -EINVAL;
    2131                 :            : 
    2132                 :          0 :         atomic_add(refresh, &event->event_limit);
    2133                 :          0 :         perf_event_enable(event);
    2134                 :            : 
    2135                 :          0 :         return 0;
    2136                 :            : }
    2137                 :            : EXPORT_SYMBOL_GPL(perf_event_refresh);
    2138                 :            : 
    2139                 :          0 : static void ctx_sched_out(struct perf_event_context *ctx,
    2140                 :            :                           struct perf_cpu_context *cpuctx,
    2141                 :            :                           enum event_type_t event_type)
    2142                 :            : {
    2143                 :            :         struct perf_event *event;
    2144                 :          0 :         int is_active = ctx->is_active;
    2145                 :            : 
    2146                 :          0 :         ctx->is_active &= ~event_type;
    2147         [ #  # ]:          0 :         if (likely(!ctx->nr_events))
    2148                 :            :                 return;
    2149                 :            : 
    2150                 :          0 :         update_context_time(ctx);
    2151                 :            :         update_cgrp_time_from_cpuctx(cpuctx);
    2152         [ #  # ]:          0 :         if (!ctx->nr_active)
    2153                 :            :                 return;
    2154                 :            : 
    2155                 :          0 :         perf_pmu_disable(ctx->pmu);
    2156 [ #  # ][ #  # ]:          0 :         if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
    2157         [ #  # ]:          0 :                 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
    2158                 :          0 :                         group_sched_out(event, cpuctx, ctx);
    2159                 :            :         }
    2160                 :            : 
    2161 [ #  # ][ #  # ]:          0 :         if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
    2162         [ #  # ]:          0 :                 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
    2163                 :          0 :                         group_sched_out(event, cpuctx, ctx);
    2164                 :            :         }
    2165                 :          0 :         perf_pmu_enable(ctx->pmu);
    2166                 :            : }
    2167                 :            : 
    2168                 :            : /*
    2169                 :            :  * Test whether two contexts are equivalent, i.e. whether they have both been
    2170                 :            :  * cloned from the same version of the same context.
    2171                 :            :  *
    2172                 :            :  * Equivalence is measured using a generation number in the context that is
    2173                 :            :  * incremented on each modification to it; see unclone_ctx(), list_add_event()
    2174                 :            :  * and list_del_event().
    2175                 :            :  */
    2176                 :          0 : static int context_equiv(struct perf_event_context *ctx1,
    2177                 :            :                          struct perf_event_context *ctx2)
    2178                 :            : {
    2179                 :            :         /* Pinning disables the swap optimization */
    2180 [ #  # ][ #  # ]:          0 :         if (ctx1->pin_count || ctx2->pin_count)
    2181                 :            :                 return 0;
    2182                 :            : 
    2183                 :            :         /* If ctx1 is the parent of ctx2 */
    2184 [ #  # ][ #  # ]:          0 :         if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
    2185                 :            :                 return 1;
    2186                 :            : 
    2187                 :            :         /* If ctx2 is the parent of ctx1 */
    2188 [ #  # ][ #  # ]:          0 :         if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
    2189                 :            :                 return 1;
    2190                 :            : 
    2191                 :            :         /*
    2192                 :            :          * If ctx1 and ctx2 have the same parent; we flatten the parent
    2193                 :            :          * hierarchy, see perf_event_init_context().
    2194                 :            :          */
    2195 [ #  # ][ #  # ]:          0 :         if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
                 [ #  # ]
    2196                 :          0 :                         ctx1->parent_gen == ctx2->parent_gen)
    2197                 :            :                 return 1;
    2198                 :            : 
    2199                 :            :         /* Unmatched */
    2200                 :          0 :         return 0;
    2201                 :            : }
    2202                 :            : 
    2203                 :          0 : static void __perf_event_sync_stat(struct perf_event *event,
    2204                 :            :                                      struct perf_event *next_event)
    2205                 :            : {
    2206                 :            :         u64 value;
    2207                 :            : 
    2208         [ #  # ]:          0 :         if (!event->attr.inherit_stat)
    2209                 :          0 :                 return;
    2210                 :            : 
    2211                 :            :         /*
    2212                 :            :          * Update the event value, we cannot use perf_event_read()
    2213                 :            :          * because we're in the middle of a context switch and have IRQs
    2214                 :            :          * disabled, which upsets smp_call_function_single(), however
    2215                 :            :          * we know the event must be on the current CPU, therefore we
    2216                 :            :          * don't need to use it.
    2217                 :            :          */
    2218      [ #  #  # ]:          0 :         switch (event->state) {
    2219                 :            :         case PERF_EVENT_STATE_ACTIVE:
    2220                 :          0 :                 event->pmu->read(event);
    2221                 :            :                 /* fall-through */
    2222                 :            : 
    2223                 :            :         case PERF_EVENT_STATE_INACTIVE:
    2224                 :          0 :                 update_event_times(event);
    2225                 :          0 :                 break;
    2226                 :            : 
    2227                 :            :         default:
    2228                 :            :                 break;
    2229                 :            :         }
    2230                 :            : 
    2231                 :            :         /*
    2232                 :            :          * In order to keep per-task stats reliable we need to flip the event
    2233                 :            :          * values when we flip the contexts.
    2234                 :            :          */
    2235                 :          0 :         value = local64_read(&next_event->count);
    2236                 :          0 :         value = local64_xchg(&event->count, value);
    2237                 :            :         local64_set(&next_event->count, value);
    2238                 :            : 
    2239                 :          0 :         swap(event->total_time_enabled, next_event->total_time_enabled);
    2240                 :          0 :         swap(event->total_time_running, next_event->total_time_running);
    2241                 :            : 
    2242                 :            :         /*
    2243                 :            :          * Since we swizzled the values, update the user visible data too.
    2244                 :            :          */
    2245                 :          0 :         perf_event_update_userpage(event);
    2246                 :          0 :         perf_event_update_userpage(next_event);
    2247                 :            : }
    2248                 :            : 
    2249                 :          0 : static void perf_event_sync_stat(struct perf_event_context *ctx,
    2250                 :            :                                    struct perf_event_context *next_ctx)
    2251                 :            : {
    2252                 :            :         struct perf_event *event, *next_event;
    2253                 :            : 
    2254         [ #  # ]:          0 :         if (!ctx->nr_stat)
    2255                 :          0 :                 return;
    2256                 :            : 
    2257                 :          0 :         update_context_time(ctx);
    2258                 :            : 
    2259                 :          0 :         event = list_first_entry(&ctx->event_list,
    2260                 :            :                                    struct perf_event, event_entry);
    2261                 :            : 
    2262                 :          0 :         next_event = list_first_entry(&next_ctx->event_list,
    2263                 :            :                                         struct perf_event, event_entry);
    2264                 :            : 
    2265 [ #  # ][ #  # ]:          0 :         while (&event->event_entry != &ctx->event_list &&
    2266                 :          0 :                &next_event->event_entry != &next_ctx->event_list) {
    2267                 :            : 
    2268                 :          0 :                 __perf_event_sync_stat(event, next_event);
    2269                 :            : 
    2270                 :          0 :                 event = list_next_entry(event, event_entry);
    2271                 :          0 :                 next_event = list_next_entry(next_event, event_entry);
    2272                 :            :         }
    2273                 :            : }
    2274                 :            : 
    2275                 :          0 : static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
    2276                 :            :                                          struct task_struct *next)
    2277                 :            : {
    2278                 :          0 :         struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
    2279                 :            :         struct perf_event_context *next_ctx;
    2280                 :            :         struct perf_event_context *parent, *next_parent;
    2281                 :            :         struct perf_cpu_context *cpuctx;
    2282                 :            :         int do_switch = 1;
    2283                 :            : 
    2284         [ #  # ]:          0 :         if (likely(!ctx))
    2285                 :            :                 return;
    2286                 :            : 
    2287                 :            :         cpuctx = __get_cpu_context(ctx);
    2288         [ #  # ]:          0 :         if (!cpuctx->task_ctx)
    2289                 :            :                 return;
    2290                 :            : 
    2291                 :            :         rcu_read_lock();
    2292                 :          0 :         next_ctx = next->perf_event_ctxp[ctxn];
    2293         [ #  # ]:          0 :         if (!next_ctx)
    2294                 :            :                 goto unlock;
    2295                 :            : 
    2296                 :          0 :         parent = rcu_dereference(ctx->parent_ctx);
    2297                 :          0 :         next_parent = rcu_dereference(next_ctx->parent_ctx);
    2298                 :            : 
    2299                 :            :         /* If neither context have a parent context; they cannot be clones. */
    2300         [ #  # ]:          0 :         if (!parent && !next_parent)
    2301                 :            :                 goto unlock;
    2302                 :            : 
    2303 [ #  # ][ #  # ]:          0 :         if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
    2304                 :            :                 /*
    2305                 :            :                  * Looks like the two contexts are clones, so we might be
    2306                 :            :                  * able to optimize the context switch.  We lock both
    2307                 :            :                  * contexts and check that they are clones under the
    2308                 :            :                  * lock (including re-checking that neither has been
    2309                 :            :                  * uncloned in the meantime).  It doesn't matter which
    2310                 :            :                  * order we take the locks because no other cpu could
    2311                 :            :                  * be trying to lock both of these tasks.
    2312                 :            :                  */
    2313                 :          0 :                 raw_spin_lock(&ctx->lock);
    2314                 :          0 :                 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
    2315         [ #  # ]:          0 :                 if (context_equiv(ctx, next_ctx)) {
    2316                 :            :                         /*
    2317                 :            :                          * XXX do we need a memory barrier of sorts
    2318                 :            :                          * wrt to rcu_dereference() of perf_event_ctxp
    2319                 :            :                          */
    2320                 :          0 :                         task->perf_event_ctxp[ctxn] = next_ctx;
    2321                 :          0 :                         next->perf_event_ctxp[ctxn] = ctx;
    2322                 :          0 :                         ctx->task = next;
    2323                 :          0 :                         next_ctx->task = task;
    2324                 :            :                         do_switch = 0;
    2325                 :            : 
    2326                 :          0 :                         perf_event_sync_stat(ctx, next_ctx);
    2327                 :            :                 }
    2328                 :            :                 raw_spin_unlock(&next_ctx->lock);
    2329                 :            :                 raw_spin_unlock(&ctx->lock);
    2330                 :            :         }
    2331                 :            : unlock:
    2332                 :            :         rcu_read_unlock();
    2333                 :            : 
    2334         [ #  # ]:          0 :         if (do_switch) {
    2335                 :          0 :                 raw_spin_lock(&ctx->lock);
    2336                 :          0 :                 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
    2337                 :          0 :                 cpuctx->task_ctx = NULL;
    2338                 :            :                 raw_spin_unlock(&ctx->lock);
    2339                 :            :         }
    2340                 :            : }
    2341                 :            : 
    2342                 :            : #define for_each_task_context_nr(ctxn)                                  \
    2343                 :            :         for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
    2344                 :            : 
    2345                 :            : /*
    2346                 :            :  * Called from scheduler to remove the events of the current task,
    2347                 :            :  * with interrupts disabled.
    2348                 :            :  *
    2349                 :            :  * We stop each event and update the event value in event->count.
    2350                 :            :  *
    2351                 :            :  * This does not protect us against NMI, but disable()
    2352                 :            :  * sets the disabled bit in the control field of event _before_
    2353                 :            :  * accessing the event control register. If a NMI hits, then it will
    2354                 :            :  * not restart the event.
    2355                 :            :  */
    2356                 :          0 : void __perf_event_task_sched_out(struct task_struct *task,
    2357                 :            :                                  struct task_struct *next)
    2358                 :            : {
    2359                 :            :         int ctxn;
    2360                 :            : 
    2361         [ #  # ]:          0 :         for_each_task_context_nr(ctxn)
    2362                 :          0 :                 perf_event_context_sched_out(task, ctxn, next);
    2363                 :            : 
    2364                 :            :         /*
    2365                 :            :          * if cgroup events exist on this CPU, then we need
    2366                 :            :          * to check if we have to switch out PMU state.
    2367                 :            :          * cgroup event are system-wide mode only
    2368                 :            :          */
    2369                 :          0 :         if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
    2370                 :            :                 perf_cgroup_sched_out(task, next);
    2371                 :          0 : }
    2372                 :            : 
    2373                 :          0 : static void task_ctx_sched_out(struct perf_event_context *ctx)
    2374                 :            : {
    2375                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    2376                 :            : 
    2377         [ #  # ]:          0 :         if (!cpuctx->task_ctx)
    2378                 :            :                 return;
    2379                 :            : 
    2380 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
         [ #  # ][ #  # ]
    2381                 :            :                 return;
    2382                 :            : 
    2383                 :          0 :         ctx_sched_out(ctx, cpuctx, EVENT_ALL);
    2384                 :          0 :         cpuctx->task_ctx = NULL;
    2385                 :            : }
    2386                 :            : 
    2387                 :            : /*
    2388                 :            :  * Called with IRQs disabled
    2389                 :            :  */
    2390                 :            : static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
    2391                 :            :                               enum event_type_t event_type)
    2392                 :            : {
    2393                 :          0 :         ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
    2394                 :            : }
    2395                 :            : 
    2396                 :            : static void
    2397                 :          0 : ctx_pinned_sched_in(struct perf_event_context *ctx,
    2398                 :            :                     struct perf_cpu_context *cpuctx)
    2399                 :            : {
    2400                 :          0 :         struct perf_event *event;
    2401                 :            : 
    2402         [ #  # ]:          0 :         list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
    2403         [ #  # ]:          0 :                 if (event->state <= PERF_EVENT_STATE_OFF)
    2404                 :          0 :                         continue;
    2405         [ #  # ]:          0 :                 if (!event_filter_match(event))
    2406                 :          0 :                         continue;
    2407                 :            : 
    2408                 :            :                 /* may need to reset tstamp_enabled */
    2409                 :            :                 if (is_cgroup_event(event))
    2410                 :            :                         perf_cgroup_mark_enabled(event, ctx);
    2411                 :            : 
    2412         [ #  # ]:          0 :                 if (group_can_go_on(event, cpuctx, 1))
    2413                 :          0 :                         group_sched_in(event, cpuctx, ctx);
    2414                 :            : 
    2415                 :            :                 /*
    2416                 :            :                  * If this pinned group hasn't been scheduled,
    2417                 :            :                  * put it in error state.
    2418                 :            :                  */
    2419         [ #  # ]:          0 :                 if (event->state == PERF_EVENT_STATE_INACTIVE) {
    2420                 :          0 :                         update_group_times(event);
    2421                 :          0 :                         event->state = PERF_EVENT_STATE_ERROR;
    2422                 :            :                 }
    2423                 :            :         }
    2424                 :          0 : }
    2425                 :            : 
    2426                 :            : static void
    2427                 :          0 : ctx_flexible_sched_in(struct perf_event_context *ctx,
    2428                 :            :                       struct perf_cpu_context *cpuctx)
    2429                 :            : {
    2430                 :          0 :         struct perf_event *event;
    2431                 :            :         int can_add_hw = 1;
    2432                 :            : 
    2433         [ #  # ]:          0 :         list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
    2434                 :            :                 /* Ignore events in OFF or ERROR state */
    2435         [ #  # ]:          0 :                 if (event->state <= PERF_EVENT_STATE_OFF)
    2436                 :          0 :                         continue;
    2437                 :            :                 /*
    2438                 :            :                  * Listen to the 'cpu' scheduling filter constraint
    2439                 :            :                  * of events:
    2440                 :            :                  */
    2441         [ #  # ]:          0 :                 if (!event_filter_match(event))
    2442                 :          0 :                         continue;
    2443                 :            : 
    2444                 :            :                 /* may need to reset tstamp_enabled */
    2445                 :            :                 if (is_cgroup_event(event))
    2446                 :            :                         perf_cgroup_mark_enabled(event, ctx);
    2447                 :            : 
    2448         [ #  # ]:          0 :                 if (group_can_go_on(event, cpuctx, can_add_hw)) {
    2449         [ #  # ]:          0 :                         if (group_sched_in(event, cpuctx, ctx))
    2450                 :            :                                 can_add_hw = 0;
    2451                 :            :                 }
    2452                 :            :         }
    2453                 :          0 : }
    2454                 :            : 
    2455                 :            : static void
    2456                 :          0 : ctx_sched_in(struct perf_event_context *ctx,
    2457                 :            :              struct perf_cpu_context *cpuctx,
    2458                 :            :              enum event_type_t event_type,
    2459                 :            :              struct task_struct *task)
    2460                 :            : {
    2461                 :            :         u64 now;
    2462                 :          0 :         int is_active = ctx->is_active;
    2463                 :            : 
    2464                 :          0 :         ctx->is_active |= event_type;
    2465         [ #  # ]:          0 :         if (likely(!ctx->nr_events))
    2466                 :          0 :                 return;
    2467                 :            : 
    2468                 :            :         now = perf_clock();
    2469                 :          0 :         ctx->timestamp = now;
    2470                 :            :         perf_cgroup_set_timestamp(task, ctx);
    2471                 :            :         /*
    2472                 :            :          * First go through the list and put on any pinned groups
    2473                 :            :          * in order to give them the best chance of going on.
    2474                 :            :          */
    2475 [ #  # ][ #  # ]:          0 :         if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
    2476                 :          0 :                 ctx_pinned_sched_in(ctx, cpuctx);
    2477                 :            : 
    2478                 :            :         /* Then walk through the lower prio flexible groups */
    2479 [ #  # ][ #  # ]:          0 :         if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
    2480                 :          0 :                 ctx_flexible_sched_in(ctx, cpuctx);
    2481                 :            : }
    2482                 :            : 
    2483                 :            : static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
    2484                 :            :                              enum event_type_t event_type,
    2485                 :            :                              struct task_struct *task)
    2486                 :            : {
    2487                 :          0 :         struct perf_event_context *ctx = &cpuctx->ctx;
    2488                 :            : 
    2489                 :          0 :         ctx_sched_in(ctx, cpuctx, event_type, task);
    2490                 :            : }
    2491                 :            : 
    2492                 :          0 : static void perf_event_context_sched_in(struct perf_event_context *ctx,
    2493                 :            :                                         struct task_struct *task)
    2494                 :            : {
    2495                 :            :         struct perf_cpu_context *cpuctx;
    2496                 :            : 
    2497                 :            :         cpuctx = __get_cpu_context(ctx);
    2498         [ #  # ]:          0 :         if (cpuctx->task_ctx == ctx)
    2499                 :          0 :                 return;
    2500                 :            : 
    2501                 :            :         perf_ctx_lock(cpuctx, ctx);
    2502                 :          0 :         perf_pmu_disable(ctx->pmu);
    2503                 :            :         /*
    2504                 :            :          * We want to keep the following priority order:
    2505                 :            :          * cpu pinned (that don't need to move), task pinned,
    2506                 :            :          * cpu flexible, task flexible.
    2507                 :            :          */
    2508                 :            :         cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
    2509                 :            : 
    2510         [ #  # ]:          0 :         if (ctx->nr_events)
    2511                 :          0 :                 cpuctx->task_ctx = ctx;
    2512                 :            : 
    2513                 :          0 :         perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
    2514                 :            : 
    2515                 :          0 :         perf_pmu_enable(ctx->pmu);
    2516                 :          0 :         perf_ctx_unlock(cpuctx, ctx);
    2517                 :            : 
    2518                 :            :         /*
    2519                 :            :          * Since these rotations are per-cpu, we need to ensure the
    2520                 :            :          * cpu-context we got scheduled on is actually rotating.
    2521                 :            :          */
    2522                 :          0 :         perf_pmu_rotate_start(ctx->pmu);
    2523                 :            : }
    2524                 :            : 
    2525                 :            : /*
    2526                 :            :  * When sampling the branck stack in system-wide, it may be necessary
    2527                 :            :  * to flush the stack on context switch. This happens when the branch
    2528                 :            :  * stack does not tag its entries with the pid of the current task.
    2529                 :            :  * Otherwise it becomes impossible to associate a branch entry with a
    2530                 :            :  * task. This ambiguity is more likely to appear when the branch stack
    2531                 :            :  * supports priv level filtering and the user sets it to monitor only
    2532                 :            :  * at the user level (which could be a useful measurement in system-wide
    2533                 :            :  * mode). In that case, the risk is high of having a branch stack with
    2534                 :            :  * branch from multiple tasks. Flushing may mean dropping the existing
    2535                 :            :  * entries or stashing them somewhere in the PMU specific code layer.
    2536                 :            :  *
    2537                 :            :  * This function provides the context switch callback to the lower code
    2538                 :            :  * layer. It is invoked ONLY when there is at least one system-wide context
    2539                 :            :  * with at least one active event using taken branch sampling.
    2540                 :            :  */
    2541                 :          0 : static void perf_branch_stack_sched_in(struct task_struct *prev,
    2542                 :            :                                        struct task_struct *task)
    2543                 :            : {
    2544                 :            :         struct perf_cpu_context *cpuctx;
    2545                 :            :         struct pmu *pmu;
    2546                 :            :         unsigned long flags;
    2547                 :            : 
    2548                 :            :         /* no need to flush branch stack if not changing task */
    2549         [ #  # ]:          0 :         if (prev == task)
    2550                 :          0 :                 return;
    2551                 :            : 
    2552                 :            :         local_irq_save(flags);
    2553                 :            : 
    2554                 :            :         rcu_read_lock();
    2555                 :            : 
    2556         [ #  # ]:          0 :         list_for_each_entry_rcu(pmu, &pmus, entry) {
    2557                 :          0 :                 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
    2558                 :            : 
    2559                 :            :                 /*
    2560                 :            :                  * check if the context has at least one
    2561                 :            :                  * event using PERF_SAMPLE_BRANCH_STACK
    2562                 :            :                  */
    2563         [ #  # ]:          0 :                 if (cpuctx->ctx.nr_branch_stack > 0
    2564         [ #  # ]:          0 :                     && pmu->flush_branch_stack) {
    2565                 :            : 
    2566                 :          0 :                         pmu = cpuctx->ctx.pmu;
    2567                 :            : 
    2568                 :          0 :                         perf_ctx_lock(cpuctx, cpuctx->task_ctx);
    2569                 :            : 
    2570                 :            :                         perf_pmu_disable(pmu);
    2571                 :            : 
    2572                 :          0 :                         pmu->flush_branch_stack();
    2573                 :            : 
    2574                 :            :                         perf_pmu_enable(pmu);
    2575                 :            : 
    2576                 :          0 :                         perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
    2577                 :            :                 }
    2578                 :            :         }
    2579                 :            : 
    2580                 :            :         rcu_read_unlock();
    2581                 :            : 
    2582         [ #  # ]:          0 :         local_irq_restore(flags);
    2583                 :            : }
    2584                 :            : 
    2585                 :            : /*
    2586                 :            :  * Called from scheduler to add the events of the current task
    2587                 :            :  * with interrupts disabled.
    2588                 :            :  *
    2589                 :            :  * We restore the event value and then enable it.
    2590                 :            :  *
    2591                 :            :  * This does not protect us against NMI, but enable()
    2592                 :            :  * sets the enabled bit in the control field of event _before_
    2593                 :            :  * accessing the event control register. If a NMI hits, then it will
    2594                 :            :  * keep the event running.
    2595                 :            :  */
    2596                 :          0 : void __perf_event_task_sched_in(struct task_struct *prev,
    2597                 :            :                                 struct task_struct *task)
    2598                 :            : {
    2599                 :            :         struct perf_event_context *ctx;
    2600                 :            :         int ctxn;
    2601                 :            : 
    2602         [ #  # ]:          0 :         for_each_task_context_nr(ctxn) {
    2603                 :          0 :                 ctx = task->perf_event_ctxp[ctxn];
    2604         [ #  # ]:          0 :                 if (likely(!ctx))
    2605                 :          0 :                         continue;
    2606                 :            : 
    2607                 :          0 :                 perf_event_context_sched_in(ctx, task);
    2608                 :            :         }
    2609                 :            :         /*
    2610                 :            :          * if cgroup events exist on this CPU, then we need
    2611                 :            :          * to check if we have to switch in PMU state.
    2612                 :            :          * cgroup event are system-wide mode only
    2613                 :            :          */
    2614                 :          0 :         if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
    2615                 :            :                 perf_cgroup_sched_in(prev, task);
    2616                 :            : 
    2617                 :            :         /* check for system-wide branch_stack events */
    2618         [ #  # ]:          0 :         if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
    2619                 :          0 :                 perf_branch_stack_sched_in(prev, task);
    2620                 :          0 : }
    2621                 :            : 
    2622                 :          0 : static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
    2623                 :            : {
    2624                 :          0 :         u64 frequency = event->attr.sample_freq;
    2625                 :            :         u64 sec = NSEC_PER_SEC;
    2626                 :            :         u64 divisor, dividend;
    2627                 :            : 
    2628                 :            :         int count_fls, nsec_fls, frequency_fls, sec_fls;
    2629                 :            : 
    2630                 :            :         count_fls = fls64(count);
    2631                 :            :         nsec_fls = fls64(nsec);
    2632                 :            :         frequency_fls = fls64(frequency);
    2633                 :            :         sec_fls = 30;
    2634                 :            : 
    2635                 :            :         /*
    2636                 :            :          * We got @count in @nsec, with a target of sample_freq HZ
    2637                 :            :          * the target period becomes:
    2638                 :            :          *
    2639                 :            :          *             @count * 10^9
    2640                 :            :          * period = -------------------
    2641                 :            :          *          @nsec * sample_freq
    2642                 :            :          *
    2643                 :            :          */
    2644                 :            : 
    2645                 :            :         /*
    2646                 :            :          * Reduce accuracy by one bit such that @a and @b converge
    2647                 :            :          * to a similar magnitude.
    2648                 :            :          */
    2649                 :            : #define REDUCE_FLS(a, b)                \
    2650                 :            : do {                                    \
    2651                 :            :         if (a##_fls > b##_fls) {     \
    2652                 :            :                 a >>= 1;          \
    2653                 :            :                 a##_fls--;              \
    2654                 :            :         } else {                        \
    2655                 :            :                 b >>= 1;          \
    2656                 :            :                 b##_fls--;              \
    2657                 :            :         }                               \
    2658                 :            : } while (0)
    2659                 :            : 
    2660                 :            :         /*
    2661                 :            :          * Reduce accuracy until either term fits in a u64, then proceed with
    2662                 :            :          * the other, so that finally we can do a u64/u64 division.
    2663                 :            :          */
    2664 [ #  # ][ #  # ]:          0 :         while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
    2665         [ #  # ]:          0 :                 REDUCE_FLS(nsec, frequency);
    2666         [ #  # ]:          0 :                 REDUCE_FLS(sec, count);
    2667                 :            :         }
    2668                 :            : 
    2669         [ #  # ]:          0 :         if (count_fls + sec_fls > 64) {
    2670                 :          0 :                 divisor = nsec * frequency;
    2671                 :            : 
    2672         [ #  # ]:          0 :                 while (count_fls + sec_fls > 64) {
    2673         [ #  # ]:          0 :                         REDUCE_FLS(count, sec);
    2674                 :          0 :                         divisor >>= 1;
    2675                 :            :                 }
    2676                 :            : 
    2677                 :          0 :                 dividend = count * sec;
    2678                 :            :         } else {
    2679                 :          0 :                 dividend = count * sec;
    2680                 :            : 
    2681         [ #  # ]:          0 :                 while (nsec_fls + frequency_fls > 64) {
    2682         [ #  # ]:          0 :                         REDUCE_FLS(nsec, frequency);
    2683                 :          0 :                         dividend >>= 1;
    2684                 :            :                 }
    2685                 :            : 
    2686                 :          0 :                 divisor = nsec * frequency;
    2687                 :            :         }
    2688                 :            : 
    2689         [ #  # ]:          0 :         if (!divisor)
    2690                 :            :                 return dividend;
    2691                 :            : 
    2692                 :          0 :         return div64_u64(dividend, divisor);
    2693                 :            : }
    2694                 :            : 
    2695                 :            : static DEFINE_PER_CPU(int, perf_throttled_count);
    2696                 :            : static DEFINE_PER_CPU(u64, perf_throttled_seq);
    2697                 :            : 
    2698                 :          0 : static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
    2699                 :            : {
    2700                 :            :         struct hw_perf_event *hwc = &event->hw;
    2701                 :            :         s64 period, sample_period;
    2702                 :            :         s64 delta;
    2703                 :            : 
    2704                 :          0 :         period = perf_calculate_period(event, nsec, count);
    2705                 :            : 
    2706                 :          0 :         delta = (s64)(period - hwc->sample_period);
    2707                 :          0 :         delta = (delta + 7) / 8; /* low pass filter */
    2708                 :            : 
    2709                 :          0 :         sample_period = hwc->sample_period + delta;
    2710                 :            : 
    2711         [ #  # ]:          0 :         if (!sample_period)
    2712                 :            :                 sample_period = 1;
    2713                 :            : 
    2714                 :          0 :         hwc->sample_period = sample_period;
    2715                 :            : 
    2716         [ #  # ]:          0 :         if (local64_read(&hwc->period_left) > 8*sample_period) {
    2717         [ #  # ]:          0 :                 if (disable)
    2718                 :          0 :                         event->pmu->stop(event, PERF_EF_UPDATE);
    2719                 :            : 
    2720                 :            :                 local64_set(&hwc->period_left, 0);
    2721                 :            : 
    2722         [ #  # ]:          0 :                 if (disable)
    2723                 :          0 :                         event->pmu->start(event, PERF_EF_RELOAD);
    2724                 :            :         }
    2725                 :          0 : }
    2726                 :            : 
    2727                 :            : /*
    2728                 :            :  * combine freq adjustment with unthrottling to avoid two passes over the
    2729                 :            :  * events. At the same time, make sure, having freq events does not change
    2730                 :            :  * the rate of unthrottling as that would introduce bias.
    2731                 :            :  */
    2732                 :          0 : static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
    2733                 :            :                                            int needs_unthr)
    2734                 :            : {
    2735                 :          0 :         struct perf_event *event;
    2736                 :            :         struct hw_perf_event *hwc;
    2737                 :            :         u64 now, period = TICK_NSEC;
    2738                 :            :         s64 delta;
    2739                 :            : 
    2740                 :            :         /*
    2741                 :            :          * only need to iterate over all events iff:
    2742                 :            :          * - context have events in frequency mode (needs freq adjust)
    2743                 :            :          * - there are events to unthrottle on this cpu
    2744                 :            :          */
    2745         [ #  # ]:          0 :         if (!(ctx->nr_freq || needs_unthr))
    2746                 :          0 :                 return;
    2747                 :            : 
    2748                 :          0 :         raw_spin_lock(&ctx->lock);
    2749                 :          0 :         perf_pmu_disable(ctx->pmu);
    2750                 :            : 
    2751         [ #  # ]:          0 :         list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
    2752         [ #  # ]:          0 :                 if (event->state != PERF_EVENT_STATE_ACTIVE)
    2753                 :          0 :                         continue;
    2754                 :            : 
    2755         [ #  # ]:          0 :                 if (!event_filter_match(event))
    2756                 :          0 :                         continue;
    2757                 :            : 
    2758                 :          0 :                 perf_pmu_disable(event->pmu);
    2759                 :            : 
    2760                 :            :                 hwc = &event->hw;
    2761                 :            : 
    2762         [ #  # ]:          0 :                 if (hwc->interrupts == MAX_INTERRUPTS) {
    2763                 :          0 :                         hwc->interrupts = 0;
    2764                 :          0 :                         perf_log_throttle(event, 1);
    2765                 :          0 :                         event->pmu->start(event, 0);
    2766                 :            :                 }
    2767                 :            : 
    2768 [ #  # ][ #  # ]:          0 :                 if (!event->attr.freq || !event->attr.sample_freq)
    2769                 :            :                         goto next;
    2770                 :            : 
    2771                 :            :                 /*
    2772                 :            :                  * stop the event and update event->count
    2773                 :            :                  */
    2774                 :          0 :                 event->pmu->stop(event, PERF_EF_UPDATE);
    2775                 :            : 
    2776                 :          0 :                 now = local64_read(&event->count);
    2777                 :          0 :                 delta = now - hwc->freq_count_stamp;
    2778                 :          0 :                 hwc->freq_count_stamp = now;
    2779                 :            : 
    2780                 :            :                 /*
    2781                 :            :                  * restart the event
    2782                 :            :                  * reload only if value has changed
    2783                 :            :                  * we have stopped the event so tell that
    2784                 :            :                  * to perf_adjust_period() to avoid stopping it
    2785                 :            :                  * twice.
    2786                 :            :                  */
    2787         [ #  # ]:          0 :                 if (delta > 0)
    2788                 :          0 :                         perf_adjust_period(event, period, delta, false);
    2789                 :            : 
    2790         [ #  # ]:          0 :                 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
    2791                 :            :         next:
    2792                 :          0 :                 perf_pmu_enable(event->pmu);
    2793                 :            :         }
    2794                 :            : 
    2795                 :          0 :         perf_pmu_enable(ctx->pmu);
    2796                 :            :         raw_spin_unlock(&ctx->lock);
    2797                 :            : }
    2798                 :            : 
    2799                 :            : /*
    2800                 :            :  * Round-robin a context's events:
    2801                 :            :  */
    2802                 :            : static void rotate_ctx(struct perf_event_context *ctx)
    2803                 :            : {
    2804                 :            :         /*
    2805                 :            :          * Rotate the first entry last of non-pinned groups. Rotation might be
    2806                 :            :          * disabled by the inheritance code.
    2807                 :            :          */
    2808 [ #  # ][ #  # ]:          0 :         if (!ctx->rotate_disable)
    2809                 :          0 :                 list_rotate_left(&ctx->flexible_groups);
    2810                 :            : }
    2811                 :            : 
    2812                 :            : /*
    2813                 :            :  * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
    2814                 :            :  * because they're strictly cpu affine and rotate_start is called with IRQs
    2815                 :            :  * disabled, while rotate_context is called from IRQ context.
    2816                 :            :  */
    2817                 :          0 : static int perf_rotate_context(struct perf_cpu_context *cpuctx)
    2818                 :            : {
    2819                 :            :         struct perf_event_context *ctx = NULL;
    2820                 :            :         int rotate = 0, remove = 1;
    2821                 :            : 
    2822         [ #  # ]:          0 :         if (cpuctx->ctx.nr_events) {
    2823                 :            :                 remove = 0;
    2824         [ #  # ]:          0 :                 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
    2825                 :            :                         rotate = 1;
    2826                 :            :         }
    2827                 :            : 
    2828                 :          0 :         ctx = cpuctx->task_ctx;
    2829 [ #  # ][ #  # ]:          0 :         if (ctx && ctx->nr_events) {
    2830                 :            :                 remove = 0;
    2831         [ #  # ]:          0 :                 if (ctx->nr_events != ctx->nr_active)
    2832                 :            :                         rotate = 1;
    2833                 :            :         }
    2834                 :            : 
    2835         [ #  # ]:          0 :         if (!rotate)
    2836                 :            :                 goto done;
    2837                 :            : 
    2838                 :            :         perf_ctx_lock(cpuctx, cpuctx->task_ctx);
    2839                 :          0 :         perf_pmu_disable(cpuctx->ctx.pmu);
    2840                 :            : 
    2841                 :            :         cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
    2842         [ #  # ]:          0 :         if (ctx)
    2843                 :          0 :                 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
    2844                 :            : 
    2845                 :            :         rotate_ctx(&cpuctx->ctx);
    2846         [ #  # ]:          0 :         if (ctx)
    2847                 :            :                 rotate_ctx(ctx);
    2848                 :            : 
    2849                 :          0 :         perf_event_sched_in(cpuctx, ctx, current);
    2850                 :            : 
    2851                 :          0 :         perf_pmu_enable(cpuctx->ctx.pmu);
    2852                 :          0 :         perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
    2853                 :            : done:
    2854         [ #  # ]:          0 :         if (remove)
    2855                 :          0 :                 list_del_init(&cpuctx->rotation_list);
    2856                 :            : 
    2857                 :          0 :         return rotate;
    2858                 :            : }
    2859                 :            : 
    2860                 :            : #ifdef CONFIG_NO_HZ_FULL
    2861                 :            : bool perf_event_can_stop_tick(void)
    2862                 :            : {
    2863                 :            :         if (atomic_read(&nr_freq_events) ||
    2864                 :            :             __this_cpu_read(perf_throttled_count))
    2865                 :            :                 return false;
    2866                 :            :         else
    2867                 :            :                 return true;
    2868                 :            : }
    2869                 :            : #endif
    2870                 :            : 
    2871                 :          0 : void perf_event_task_tick(void)
    2872                 :            : {
    2873                 :    5192418 :         struct list_head *head = &__get_cpu_var(rotation_list);
    2874                 :            :         struct perf_cpu_context *cpuctx, *tmp;
    2875                 :            :         struct perf_event_context *ctx;
    2876                 :            :         int throttled;
    2877                 :            : 
    2878         [ -  + ]:    2603043 :         WARN_ON(!irqs_disabled());
    2879                 :            : 
    2880                 :    5188244 :         __this_cpu_inc(perf_throttled_seq);
    2881                 :    7782366 :         throttled = __this_cpu_xchg(perf_throttled_count, 0);
    2882                 :            : 
    2883         [ -  + ]:    5190331 :         list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
    2884                 :          0 :                 ctx = &cpuctx->ctx;
    2885                 :          0 :                 perf_adjust_freq_unthr_context(ctx, throttled);
    2886                 :            : 
    2887                 :          0 :                 ctx = cpuctx->task_ctx;
    2888         [ #  # ]:          0 :                 if (ctx)
    2889                 :          0 :                         perf_adjust_freq_unthr_context(ctx, throttled);
    2890                 :            :         }
    2891                 :    2594122 : }
    2892                 :            : 
    2893                 :            : static int event_enable_on_exec(struct perf_event *event,
    2894                 :            :                                 struct perf_event_context *ctx)
    2895                 :            : {
    2896         [ #  # ]:          0 :         if (!event->attr.enable_on_exec)
    2897                 :            :                 return 0;
    2898                 :            : 
    2899                 :          0 :         event->attr.enable_on_exec = 0;
    2900         [ #  # ]:          0 :         if (event->state >= PERF_EVENT_STATE_INACTIVE)
    2901                 :            :                 return 0;
    2902                 :            : 
    2903                 :          0 :         __perf_event_mark_enabled(event);
    2904                 :            : 
    2905                 :            :         return 1;
    2906                 :            : }
    2907                 :            : 
    2908                 :            : /*
    2909                 :            :  * Enable all of a task's events that have been marked enable-on-exec.
    2910                 :            :  * This expects task == current.
    2911                 :            :  */
    2912                 :          0 : static void perf_event_enable_on_exec(struct perf_event_context *ctx)
    2913                 :            : {
    2914                 :            :         struct perf_event *event;
    2915                 :            :         unsigned long flags;
    2916                 :            :         int enabled = 0;
    2917                 :            :         int ret;
    2918                 :            : 
    2919                 :            :         local_irq_save(flags);
    2920 [ #  # ][ #  # ]:          0 :         if (!ctx || !ctx->nr_events)
    2921                 :            :                 goto out;
    2922                 :            : 
    2923                 :            :         /*
    2924                 :            :          * We must ctxsw out cgroup events to avoid conflict
    2925                 :            :          * when invoking perf_task_event_sched_in() later on
    2926                 :            :          * in this function. Otherwise we end up trying to
    2927                 :            :          * ctxswin cgroup events which are already scheduled
    2928                 :            :          * in.
    2929                 :            :          */
    2930                 :            :         perf_cgroup_sched_out(current, NULL);
    2931                 :            : 
    2932                 :          0 :         raw_spin_lock(&ctx->lock);
    2933                 :          0 :         task_ctx_sched_out(ctx);
    2934                 :            : 
    2935         [ #  # ]:          0 :         list_for_each_entry(event, &ctx->event_list, event_entry) {
    2936                 :            :                 ret = event_enable_on_exec(event, ctx);
    2937         [ #  # ]:          0 :                 if (ret)
    2938                 :            :                         enabled = 1;
    2939                 :            :         }
    2940                 :            : 
    2941                 :            :         /*
    2942                 :            :          * Unclone this context if we enabled any event.
    2943                 :            :          */
    2944         [ #  # ]:          0 :         if (enabled)
    2945                 :            :                 unclone_ctx(ctx);
    2946                 :            : 
    2947                 :            :         raw_spin_unlock(&ctx->lock);
    2948                 :            : 
    2949                 :            :         /*
    2950                 :            :          * Also calls ctxswin for cgroup events, if any:
    2951                 :            :          */
    2952                 :          0 :         perf_event_context_sched_in(ctx, ctx->task);
    2953                 :            : out:
    2954         [ #  # ]:          0 :         local_irq_restore(flags);
    2955                 :          0 : }
    2956                 :            : 
    2957                 :            : /*
    2958                 :            :  * Cross CPU call to read the hardware event
    2959                 :            :  */
    2960                 :          0 : static void __perf_event_read(void *info)
    2961                 :            : {
    2962                 :            :         struct perf_event *event = info;
    2963                 :          0 :         struct perf_event_context *ctx = event->ctx;
    2964                 :            :         struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
    2965                 :            : 
    2966                 :            :         /*
    2967                 :            :          * If this is a task context, we need to check whether it is
    2968                 :            :          * the current task context of this cpu.  If not it has been
    2969                 :            :          * scheduled out before the smp call arrived.  In that case
    2970                 :            :          * event->count would have been updated to a recent sample
    2971                 :            :          * when the event was scheduled out.
    2972                 :            :          */
    2973 [ #  # ][ #  # ]:          0 :         if (ctx->task && cpuctx->task_ctx != ctx)
    2974                 :          0 :                 return;
    2975                 :            : 
    2976                 :          0 :         raw_spin_lock(&ctx->lock);
    2977         [ #  # ]:          0 :         if (ctx->is_active) {
    2978                 :          0 :                 update_context_time(ctx);
    2979                 :            :                 update_cgrp_time_from_event(event);
    2980                 :            :         }
    2981                 :          0 :         update_event_times(event);
    2982         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_ACTIVE)
    2983                 :          0 :                 event->pmu->read(event);
    2984                 :            :         raw_spin_unlock(&ctx->lock);
    2985                 :            : }
    2986                 :            : 
    2987                 :            : static inline u64 perf_event_count(struct perf_event *event)
    2988                 :            : {
    2989                 :          0 :         return local64_read(&event->count) + atomic64_read(&event->child_count);
    2990                 :            : }
    2991                 :            : 
    2992                 :          0 : static u64 perf_event_read(struct perf_event *event)
    2993                 :            : {
    2994                 :            :         /*
    2995                 :            :          * If event is enabled and currently active on a CPU, update the
    2996                 :            :          * value in the event structure:
    2997                 :            :          */
    2998         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_ACTIVE) {
    2999                 :          0 :                 smp_call_function_single(event->oncpu,
    3000                 :            :                                          __perf_event_read, event, 1);
    3001         [ #  # ]:          0 :         } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
    3002                 :          0 :                 struct perf_event_context *ctx = event->ctx;
    3003                 :            :                 unsigned long flags;
    3004                 :            : 
    3005                 :          0 :                 raw_spin_lock_irqsave(&ctx->lock, flags);
    3006                 :            :                 /*
    3007                 :            :                  * may read while context is not active
    3008                 :            :                  * (e.g., thread is blocked), in that case
    3009                 :            :                  * we cannot update context time
    3010                 :            :                  */
    3011         [ #  # ]:          0 :                 if (ctx->is_active) {
    3012                 :          0 :                         update_context_time(ctx);
    3013                 :            :                         update_cgrp_time_from_event(event);
    3014                 :            :                 }
    3015                 :          0 :                 update_event_times(event);
    3016                 :          0 :                 raw_spin_unlock_irqrestore(&ctx->lock, flags);
    3017                 :            :         }
    3018                 :            : 
    3019                 :          0 :         return perf_event_count(event);
    3020                 :            : }
    3021                 :            : 
    3022                 :            : /*
    3023                 :            :  * Initialize the perf_event context in a task_struct:
    3024                 :            :  */
    3025                 :          0 : static void __perf_event_init_context(struct perf_event_context *ctx)
    3026                 :            : {
    3027                 :          0 :         raw_spin_lock_init(&ctx->lock);
    3028                 :          0 :         mutex_init(&ctx->mutex);
    3029                 :          0 :         INIT_LIST_HEAD(&ctx->pinned_groups);
    3030                 :          0 :         INIT_LIST_HEAD(&ctx->flexible_groups);
    3031                 :          0 :         INIT_LIST_HEAD(&ctx->event_list);
    3032                 :          0 :         atomic_set(&ctx->refcount, 1);
    3033                 :          0 : }
    3034                 :            : 
    3035                 :            : static struct perf_event_context *
    3036                 :          0 : alloc_perf_context(struct pmu *pmu, struct task_struct *task)
    3037                 :            : {
    3038                 :            :         struct perf_event_context *ctx;
    3039                 :            : 
    3040                 :            :         ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
    3041         [ #  # ]:          0 :         if (!ctx)
    3042                 :            :                 return NULL;
    3043                 :            : 
    3044                 :          0 :         __perf_event_init_context(ctx);
    3045         [ #  # ]:          0 :         if (task) {
    3046                 :          0 :                 ctx->task = task;
    3047                 :          0 :                 get_task_struct(task);
    3048                 :            :         }
    3049                 :          0 :         ctx->pmu = pmu;
    3050                 :            : 
    3051                 :          0 :         return ctx;
    3052                 :            : }
    3053                 :            : 
    3054                 :            : static struct task_struct *
    3055                 :          0 : find_lively_task_by_vpid(pid_t vpid)
    3056                 :            : {
    3057                 :            :         struct task_struct *task;
    3058                 :            :         int err;
    3059                 :            : 
    3060                 :            :         rcu_read_lock();
    3061         [ #  # ]:          0 :         if (!vpid)
    3062                 :          0 :                 task = current;
    3063                 :            :         else
    3064                 :          0 :                 task = find_task_by_vpid(vpid);
    3065         [ #  # ]:          0 :         if (task)
    3066                 :          0 :                 get_task_struct(task);
    3067                 :            :         rcu_read_unlock();
    3068                 :            : 
    3069         [ #  # ]:          0 :         if (!task)
    3070                 :            :                 return ERR_PTR(-ESRCH);
    3071                 :            : 
    3072                 :            :         /* Reuse ptrace permission checks for now. */
    3073                 :            :         err = -EACCES;
    3074         [ #  # ]:          0 :         if (!ptrace_may_access(task, PTRACE_MODE_READ))
    3075                 :            :                 goto errout;
    3076                 :            : 
    3077                 :            :         return task;
    3078                 :            : errout:
    3079                 :            :         put_task_struct(task);
    3080                 :            :         return ERR_PTR(err);
    3081                 :            : 
    3082                 :            : }
    3083                 :            : 
    3084                 :            : /*
    3085                 :            :  * Returns a matching context with refcount and pincount.
    3086                 :            :  */
    3087                 :            : static struct perf_event_context *
    3088                 :          0 : find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
    3089                 :            : {
    3090                 :            :         struct perf_event_context *ctx;
    3091                 :            :         struct perf_cpu_context *cpuctx;
    3092                 :            :         unsigned long flags;
    3093                 :            :         int ctxn, err;
    3094                 :            : 
    3095         [ #  # ]:          0 :         if (!task) {
    3096                 :            :                 /* Must be root to operate on a CPU event: */
    3097 [ #  # ][ #  # ]:          0 :                 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
    3098                 :            :                         return ERR_PTR(-EACCES);
    3099                 :            : 
    3100                 :            :                 /*
    3101                 :            :                  * We could be clever and allow to attach a event to an
    3102                 :            :                  * offline CPU and activate it when the CPU comes up, but
    3103                 :            :                  * that's for later.
    3104                 :            :                  */
    3105         [ #  # ]:          0 :                 if (!cpu_online(cpu))
    3106                 :            :                         return ERR_PTR(-ENODEV);
    3107                 :            : 
    3108                 :          0 :                 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
    3109                 :          0 :                 ctx = &cpuctx->ctx;
    3110                 :          0 :                 get_ctx(ctx);
    3111                 :          0 :                 ++ctx->pin_count;
    3112                 :            : 
    3113                 :          0 :                 return ctx;
    3114                 :            :         }
    3115                 :            : 
    3116                 :            :         err = -EINVAL;
    3117                 :          0 :         ctxn = pmu->task_ctx_nr;
    3118         [ #  # ]:          0 :         if (ctxn < 0)
    3119                 :            :                 goto errout;
    3120                 :            : 
    3121                 :            : retry:
    3122                 :          0 :         ctx = perf_lock_task_context(task, ctxn, &flags);
    3123         [ #  # ]:          0 :         if (ctx) {
    3124                 :            :                 unclone_ctx(ctx);
    3125                 :          0 :                 ++ctx->pin_count;
    3126                 :          0 :                 raw_spin_unlock_irqrestore(&ctx->lock, flags);
    3127                 :            :         } else {
    3128                 :          0 :                 ctx = alloc_perf_context(pmu, task);
    3129                 :            :                 err = -ENOMEM;
    3130         [ #  # ]:          0 :                 if (!ctx)
    3131                 :            :                         goto errout;
    3132                 :            : 
    3133                 :            :                 err = 0;
    3134                 :          0 :                 mutex_lock(&task->perf_event_mutex);
    3135                 :            :                 /*
    3136                 :            :                  * If it has already passed perf_event_exit_task().
    3137                 :            :                  * we must see PF_EXITING, it takes this mutex too.
    3138                 :            :                  */
    3139         [ #  # ]:          0 :                 if (task->flags & PF_EXITING)
    3140                 :            :                         err = -ESRCH;
    3141         [ #  # ]:          0 :                 else if (task->perf_event_ctxp[ctxn])
    3142                 :            :                         err = -EAGAIN;
    3143                 :            :                 else {
    3144                 :          0 :                         get_ctx(ctx);
    3145                 :          0 :                         ++ctx->pin_count;
    3146                 :          0 :                         rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
    3147                 :            :                 }
    3148                 :          0 :                 mutex_unlock(&task->perf_event_mutex);
    3149                 :            : 
    3150         [ #  # ]:          0 :                 if (unlikely(err)) {
    3151                 :          0 :                         put_ctx(ctx);
    3152                 :            : 
    3153         [ #  # ]:          0 :                         if (err == -EAGAIN)
    3154                 :            :                                 goto retry;
    3155                 :            :                         goto errout;
    3156                 :            :                 }
    3157                 :            :         }
    3158                 :            : 
    3159                 :          0 :         return ctx;
    3160                 :            : 
    3161                 :            : errout:
    3162                 :          0 :         return ERR_PTR(err);
    3163                 :            : }
    3164                 :            : 
    3165                 :            : static void perf_event_free_filter(struct perf_event *event);
    3166                 :            : 
    3167                 :          0 : static void free_event_rcu(struct rcu_head *head)
    3168                 :            : {
    3169                 :            :         struct perf_event *event;
    3170                 :            : 
    3171                 :          0 :         event = container_of(head, struct perf_event, rcu_head);
    3172                 :            :         if (event->ns)
    3173                 :            :                 put_pid_ns(event->ns);
    3174                 :            :         perf_event_free_filter(event);
    3175                 :          0 :         kfree(event);
    3176                 :          0 : }
    3177                 :            : 
    3178                 :            : static void ring_buffer_put(struct ring_buffer *rb);
    3179                 :            : static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
    3180                 :            : 
    3181                 :          0 : static void unaccount_event_cpu(struct perf_event *event, int cpu)
    3182                 :            : {
    3183         [ #  # ]:          0 :         if (event->parent)
    3184                 :          0 :                 return;
    3185                 :            : 
    3186         [ #  # ]:          0 :         if (has_branch_stack(event)) {
    3187         [ #  # ]:          0 :                 if (!(event->attach_state & PERF_ATTACH_TASK))
    3188                 :          0 :                         atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
    3189                 :            :         }
    3190                 :            :         if (is_cgroup_event(event))
    3191                 :            :                 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
    3192                 :            : }
    3193                 :            : 
    3194                 :          0 : static void unaccount_event(struct perf_event *event)
    3195                 :            : {
    3196         [ #  # ]:          0 :         if (event->parent)
    3197                 :          0 :                 return;
    3198                 :            : 
    3199         [ #  # ]:          0 :         if (event->attach_state & PERF_ATTACH_TASK)
    3200                 :            :                 static_key_slow_dec_deferred(&perf_sched_events);
    3201         [ #  # ]:          0 :         if (event->attr.mmap || event->attr.mmap_data)
    3202                 :            :                 atomic_dec(&nr_mmap_events);
    3203         [ #  # ]:          0 :         if (event->attr.comm)
    3204                 :            :                 atomic_dec(&nr_comm_events);
    3205         [ #  # ]:          0 :         if (event->attr.task)
    3206                 :            :                 atomic_dec(&nr_task_events);
    3207         [ #  # ]:          0 :         if (event->attr.freq)
    3208                 :            :                 atomic_dec(&nr_freq_events);
    3209                 :            :         if (is_cgroup_event(event))
    3210                 :            :                 static_key_slow_dec_deferred(&perf_sched_events);
    3211         [ #  # ]:          0 :         if (has_branch_stack(event))
    3212                 :            :                 static_key_slow_dec_deferred(&perf_sched_events);
    3213                 :            : 
    3214                 :          0 :         unaccount_event_cpu(event, event->cpu);
    3215                 :            : }
    3216                 :            : 
    3217                 :          0 : static void __free_event(struct perf_event *event)
    3218                 :            : {
    3219         [ #  # ]:          0 :         if (!event->parent) {
    3220         [ #  # ]:          0 :                 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
    3221                 :          0 :                         put_callchain_buffers();
    3222                 :            :         }
    3223                 :            : 
    3224         [ #  # ]:          0 :         if (event->destroy)
    3225                 :          0 :                 event->destroy(event);
    3226                 :            : 
    3227         [ #  # ]:          0 :         if (event->ctx)
    3228                 :          0 :                 put_ctx(event->ctx);
    3229                 :            : 
    3230                 :          0 :         call_rcu(&event->rcu_head, free_event_rcu);
    3231                 :          0 : }
    3232                 :          0 : static void free_event(struct perf_event *event)
    3233                 :            : {
    3234                 :          0 :         irq_work_sync(&event->pending);
    3235                 :            : 
    3236                 :          0 :         unaccount_event(event);
    3237                 :            : 
    3238         [ #  # ]:          0 :         if (event->rb) {
    3239                 :            :                 struct ring_buffer *rb;
    3240                 :            : 
    3241                 :            :                 /*
    3242                 :            :                  * Can happen when we close an event with re-directed output.
    3243                 :            :                  *
    3244                 :            :                  * Since we have a 0 refcount, perf_mmap_close() will skip
    3245                 :            :                  * over us; possibly making our ring_buffer_put() the last.
    3246                 :            :                  */
    3247                 :          0 :                 mutex_lock(&event->mmap_mutex);
    3248                 :          0 :                 rb = event->rb;
    3249         [ #  # ]:          0 :                 if (rb) {
    3250                 :          0 :                         rcu_assign_pointer(event->rb, NULL);
    3251                 :          0 :                         ring_buffer_detach(event, rb);
    3252                 :          0 :                         ring_buffer_put(rb); /* could be last */
    3253                 :            :                 }
    3254                 :          0 :                 mutex_unlock(&event->mmap_mutex);
    3255                 :            :         }
    3256                 :            : 
    3257                 :            :         if (is_cgroup_event(event))
    3258                 :            :                 perf_detach_cgroup(event);
    3259                 :            : 
    3260                 :            : 
    3261                 :          0 :         __free_event(event);
    3262                 :          0 : }
    3263                 :            : 
    3264                 :          0 : int perf_event_release_kernel(struct perf_event *event)
    3265                 :            : {
    3266                 :          0 :         struct perf_event_context *ctx = event->ctx;
    3267                 :            : 
    3268 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(ctx->parent_ctx);
                 [ #  # ]
    3269                 :            :         /*
    3270                 :            :          * There are two ways this annotation is useful:
    3271                 :            :          *
    3272                 :            :          *  1) there is a lock recursion from perf_event_exit_task
    3273                 :            :          *     see the comment there.
    3274                 :            :          *
    3275                 :            :          *  2) there is a lock-inversion with mmap_sem through
    3276                 :            :          *     perf_event_read_group(), which takes faults while
    3277                 :            :          *     holding ctx->mutex, however this is called after
    3278                 :            :          *     the last filedesc died, so there is no possibility
    3279                 :            :          *     to trigger the AB-BA case.
    3280                 :            :          */
    3281                 :          0 :         mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
    3282                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    3283                 :          0 :         perf_group_detach(event);
    3284                 :            :         raw_spin_unlock_irq(&ctx->lock);
    3285                 :          0 :         perf_remove_from_context(event);
    3286                 :          0 :         mutex_unlock(&ctx->mutex);
    3287                 :            : 
    3288                 :          0 :         free_event(event);
    3289                 :            : 
    3290                 :          0 :         return 0;
    3291                 :            : }
    3292                 :            : EXPORT_SYMBOL_GPL(perf_event_release_kernel);
    3293                 :            : 
    3294                 :            : /*
    3295                 :            :  * Called when the last reference to the file is gone.
    3296                 :            :  */
    3297                 :          0 : static void put_event(struct perf_event *event)
    3298                 :            : {
    3299                 :            :         struct task_struct *owner;
    3300                 :            : 
    3301         [ #  # ]:          0 :         if (!atomic_long_dec_and_test(&event->refcount))
    3302                 :          0 :                 return;
    3303                 :            : 
    3304                 :            :         rcu_read_lock();
    3305                 :          0 :         owner = ACCESS_ONCE(event->owner);
    3306                 :            :         /*
    3307                 :            :          * Matches the smp_wmb() in perf_event_exit_task(). If we observe
    3308                 :            :          * !owner it means the list deletion is complete and we can indeed
    3309                 :            :          * free this event, otherwise we need to serialize on
    3310                 :            :          * owner->perf_event_mutex.
    3311                 :            :          */
    3312                 :            :         smp_read_barrier_depends();
    3313         [ #  # ]:          0 :         if (owner) {
    3314                 :            :                 /*
    3315                 :            :                  * Since delayed_put_task_struct() also drops the last
    3316                 :            :                  * task reference we can safely take a new reference
    3317                 :            :                  * while holding the rcu_read_lock().
    3318                 :            :                  */
    3319                 :          0 :                 get_task_struct(owner);
    3320                 :            :         }
    3321                 :            :         rcu_read_unlock();
    3322                 :            : 
    3323         [ #  # ]:          0 :         if (owner) {
    3324                 :          0 :                 mutex_lock(&owner->perf_event_mutex);
    3325                 :            :                 /*
    3326                 :            :                  * We have to re-check the event->owner field, if it is cleared
    3327                 :            :                  * we raced with perf_event_exit_task(), acquiring the mutex
    3328                 :            :                  * ensured they're done, and we can proceed with freeing the
    3329                 :            :                  * event.
    3330                 :            :                  */
    3331         [ #  # ]:          0 :                 if (event->owner)
    3332                 :          0 :                         list_del_init(&event->owner_entry);
    3333                 :          0 :                 mutex_unlock(&owner->perf_event_mutex);
    3334                 :            :                 put_task_struct(owner);
    3335                 :            :         }
    3336                 :            : 
    3337                 :          0 :         perf_event_release_kernel(event);
    3338                 :            : }
    3339                 :            : 
    3340                 :          0 : static int perf_release(struct inode *inode, struct file *file)
    3341                 :            : {
    3342                 :          0 :         put_event(file->private_data);
    3343                 :          0 :         return 0;
    3344                 :            : }
    3345                 :            : 
    3346                 :          0 : u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
    3347                 :            : {
    3348                 :            :         struct perf_event *child;
    3349                 :            :         u64 total = 0;
    3350                 :            : 
    3351                 :          0 :         *enabled = 0;
    3352                 :          0 :         *running = 0;
    3353                 :            : 
    3354                 :          0 :         mutex_lock(&event->child_mutex);
    3355                 :          0 :         total += perf_event_read(event);
    3356                 :          0 :         *enabled += event->total_time_enabled +
    3357                 :          0 :                         atomic64_read(&event->child_total_time_enabled);
    3358                 :          0 :         *running += event->total_time_running +
    3359                 :          0 :                         atomic64_read(&event->child_total_time_running);
    3360                 :            : 
    3361         [ #  # ]:          0 :         list_for_each_entry(child, &event->child_list, child_list) {
    3362                 :          0 :                 total += perf_event_read(child);
    3363                 :          0 :                 *enabled += child->total_time_enabled;
    3364                 :          0 :                 *running += child->total_time_running;
    3365                 :            :         }
    3366                 :          0 :         mutex_unlock(&event->child_mutex);
    3367                 :            : 
    3368                 :          0 :         return total;
    3369                 :            : }
    3370                 :            : EXPORT_SYMBOL_GPL(perf_event_read_value);
    3371                 :            : 
    3372                 :          0 : static int perf_event_read_group(struct perf_event *event,
    3373                 :            :                                    u64 read_format, char __user *buf)
    3374                 :            : {
    3375                 :          0 :         struct perf_event *leader = event->group_leader, *sub;
    3376                 :            :         int n = 0, size = 0, ret = -EFAULT;
    3377                 :          0 :         struct perf_event_context *ctx = leader->ctx;
    3378                 :            :         u64 values[5];
    3379                 :            :         u64 count, enabled, running;
    3380                 :            : 
    3381                 :          0 :         mutex_lock(&ctx->mutex);
    3382                 :          0 :         count = perf_event_read_value(leader, &enabled, &running);
    3383                 :            : 
    3384                 :          0 :         values[n++] = 1 + leader->nr_siblings;
    3385         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
    3386                 :          0 :                 values[n++] = enabled;
    3387         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
    3388                 :          0 :                 values[n++] = running;
    3389                 :          0 :         values[n++] = count;
    3390         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_ID)
    3391                 :          0 :                 values[n++] = primary_event_id(leader);
    3392                 :            : 
    3393                 :          0 :         size = n * sizeof(u64);
    3394                 :            : 
    3395         [ #  # ]:          0 :         if (copy_to_user(buf, values, size))
    3396                 :            :                 goto unlock;
    3397                 :            : 
    3398                 :            :         ret = size;
    3399                 :            : 
    3400         [ #  # ]:          0 :         list_for_each_entry(sub, &leader->sibling_list, group_entry) {
    3401                 :            :                 n = 0;
    3402                 :            : 
    3403                 :          0 :                 values[n++] = perf_event_read_value(sub, &enabled, &running);
    3404         [ #  # ]:          0 :                 if (read_format & PERF_FORMAT_ID)
    3405                 :          0 :                         values[n++] = primary_event_id(sub);
    3406                 :            : 
    3407                 :          0 :                 size = n * sizeof(u64);
    3408                 :            : 
    3409         [ #  # ]:          0 :                 if (copy_to_user(buf + ret, values, size)) {
    3410                 :            :                         ret = -EFAULT;
    3411                 :            :                         goto unlock;
    3412                 :            :                 }
    3413                 :            : 
    3414                 :          0 :                 ret += size;
    3415                 :            :         }
    3416                 :            : unlock:
    3417                 :          0 :         mutex_unlock(&ctx->mutex);
    3418                 :            : 
    3419                 :          0 :         return ret;
    3420                 :            : }
    3421                 :            : 
    3422                 :          0 : static int perf_event_read_one(struct perf_event *event,
    3423                 :            :                                  u64 read_format, char __user *buf)
    3424                 :            : {
    3425                 :            :         u64 enabled, running;
    3426                 :            :         u64 values[4];
    3427                 :            :         int n = 0;
    3428                 :            : 
    3429                 :          0 :         values[n++] = perf_event_read_value(event, &enabled, &running);
    3430         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
    3431                 :          0 :                 values[n++] = enabled;
    3432         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
    3433                 :          0 :                 values[n++] = running;
    3434         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_ID)
    3435                 :          0 :                 values[n++] = primary_event_id(event);
    3436                 :            : 
    3437         [ #  # ]:          0 :         if (copy_to_user(buf, values, n * sizeof(u64)))
    3438                 :            :                 return -EFAULT;
    3439                 :            : 
    3440                 :          0 :         return n * sizeof(u64);
    3441                 :            : }
    3442                 :            : 
    3443                 :            : /*
    3444                 :            :  * Read the performance event - simple non blocking version for now
    3445                 :            :  */
    3446                 :            : static ssize_t
    3447                 :          0 : perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
    3448                 :            : {
    3449                 :          0 :         u64 read_format = event->attr.read_format;
    3450                 :            :         int ret;
    3451                 :            : 
    3452                 :            :         /*
    3453                 :            :          * Return end-of-file for a read on a event that is in
    3454                 :            :          * error state (i.e. because it was pinned but it couldn't be
    3455                 :            :          * scheduled on to the CPU at some point).
    3456                 :            :          */
    3457         [ #  # ]:          0 :         if (event->state == PERF_EVENT_STATE_ERROR)
    3458                 :            :                 return 0;
    3459                 :            : 
    3460         [ #  # ]:          0 :         if (count < event->read_size)
    3461                 :            :                 return -ENOSPC;
    3462                 :            : 
    3463 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(event->ctx->parent_ctx);
                 [ #  # ]
    3464         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_GROUP)
    3465                 :          0 :                 ret = perf_event_read_group(event, read_format, buf);
    3466                 :            :         else
    3467                 :          0 :                 ret = perf_event_read_one(event, read_format, buf);
    3468                 :            : 
    3469                 :          0 :         return ret;
    3470                 :            : }
    3471                 :            : 
    3472                 :            : static ssize_t
    3473                 :          0 : perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
    3474                 :            : {
    3475                 :          0 :         struct perf_event *event = file->private_data;
    3476                 :            : 
    3477                 :          0 :         return perf_read_hw(event, buf, count);
    3478                 :            : }
    3479                 :            : 
    3480                 :          0 : static unsigned int perf_poll(struct file *file, poll_table *wait)
    3481                 :            : {
    3482                 :          0 :         struct perf_event *event = file->private_data;
    3483                 :            :         struct ring_buffer *rb;
    3484                 :            :         unsigned int events = POLL_HUP;
    3485                 :            : 
    3486                 :            :         /*
    3487                 :            :          * Pin the event->rb by taking event->mmap_mutex; otherwise
    3488                 :            :          * perf_event_set_output() can swizzle our rb and make us miss wakeups.
    3489                 :            :          */
    3490                 :          0 :         mutex_lock(&event->mmap_mutex);
    3491                 :          0 :         rb = event->rb;
    3492         [ #  # ]:          0 :         if (rb)
    3493                 :          0 :                 events = atomic_xchg(&rb->poll, 0);
    3494                 :          0 :         mutex_unlock(&event->mmap_mutex);
    3495                 :            : 
    3496                 :          0 :         poll_wait(file, &event->waitq, wait);
    3497                 :            : 
    3498                 :          0 :         return events;
    3499                 :            : }
    3500                 :            : 
    3501                 :          0 : static void perf_event_reset(struct perf_event *event)
    3502                 :            : {
    3503                 :          0 :         (void)perf_event_read(event);
    3504                 :          0 :         local64_set(&event->count, 0);
    3505                 :          0 :         perf_event_update_userpage(event);
    3506                 :          0 : }
    3507                 :            : 
    3508                 :            : /*
    3509                 :            :  * Holding the top-level event's child_mutex means that any
    3510                 :            :  * descendant process that has inherited this event will block
    3511                 :            :  * in sync_child_event if it goes to exit, thus satisfying the
    3512                 :            :  * task existence requirements of perf_event_enable/disable.
    3513                 :            :  */
    3514                 :          0 : static void perf_event_for_each_child(struct perf_event *event,
    3515                 :            :                                         void (*func)(struct perf_event *))
    3516                 :            : {
    3517                 :            :         struct perf_event *child;
    3518                 :            : 
    3519 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(event->ctx->parent_ctx);
                 [ #  # ]
    3520                 :          0 :         mutex_lock(&event->child_mutex);
    3521                 :          0 :         func(event);
    3522         [ #  # ]:          0 :         list_for_each_entry(child, &event->child_list, child_list)
    3523                 :          0 :                 func(child);
    3524                 :          0 :         mutex_unlock(&event->child_mutex);
    3525                 :          0 : }
    3526                 :            : 
    3527                 :          0 : static void perf_event_for_each(struct perf_event *event,
    3528                 :            :                                   void (*func)(struct perf_event *))
    3529                 :            : {
    3530                 :          0 :         struct perf_event_context *ctx = event->ctx;
    3531                 :            :         struct perf_event *sibling;
    3532                 :            : 
    3533 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(ctx->parent_ctx);
                 [ #  # ]
    3534                 :          0 :         mutex_lock(&ctx->mutex);
    3535                 :          0 :         event = event->group_leader;
    3536                 :            : 
    3537                 :          0 :         perf_event_for_each_child(event, func);
    3538         [ #  # ]:          0 :         list_for_each_entry(sibling, &event->sibling_list, group_entry)
    3539                 :          0 :                 perf_event_for_each_child(sibling, func);
    3540                 :          0 :         mutex_unlock(&ctx->mutex);
    3541                 :          0 : }
    3542                 :            : 
    3543                 :          0 : static int perf_event_period(struct perf_event *event, u64 __user *arg)
    3544                 :            : {
    3545                 :          0 :         struct perf_event_context *ctx = event->ctx;
    3546                 :            :         int ret = 0, active;
    3547                 :            :         u64 value;
    3548                 :            : 
    3549         [ #  # ]:          0 :         if (!is_sampling_event(event))
    3550                 :            :                 return -EINVAL;
    3551                 :            : 
    3552         [ #  # ]:          0 :         if (copy_from_user(&value, arg, sizeof(value)))
    3553                 :            :                 return -EFAULT;
    3554                 :            : 
    3555         [ #  # ]:          0 :         if (!value)
    3556                 :            :                 return -EINVAL;
    3557                 :            : 
    3558                 :          0 :         raw_spin_lock_irq(&ctx->lock);
    3559         [ #  # ]:          0 :         if (event->attr.freq) {
    3560         [ #  # ]:          0 :                 if (value > sysctl_perf_event_sample_rate) {
    3561                 :            :                         ret = -EINVAL;
    3562                 :            :                         goto unlock;
    3563                 :            :                 }
    3564                 :            : 
    3565                 :          0 :                 event->attr.sample_freq = value;
    3566                 :            :         } else {
    3567                 :          0 :                 event->attr.sample_period = value;
    3568                 :          0 :                 event->hw.sample_period = value;
    3569                 :            :         }
    3570                 :            : 
    3571                 :          0 :         active = (event->state == PERF_EVENT_STATE_ACTIVE);
    3572         [ #  # ]:          0 :         if (active) {
    3573                 :          0 :                 perf_pmu_disable(ctx->pmu);
    3574                 :          0 :                 event->pmu->stop(event, PERF_EF_UPDATE);
    3575                 :            :         }
    3576                 :            : 
    3577                 :          0 :         local64_set(&event->hw.period_left, 0);
    3578                 :            : 
    3579         [ #  # ]:          0 :         if (active) {
    3580                 :          0 :                 event->pmu->start(event, PERF_EF_RELOAD);
    3581                 :          0 :                 perf_pmu_enable(ctx->pmu);
    3582                 :            :         }
    3583                 :            : 
    3584                 :            : unlock:
    3585                 :            :         raw_spin_unlock_irq(&ctx->lock);
    3586                 :            : 
    3587                 :          0 :         return ret;
    3588                 :            : }
    3589                 :            : 
    3590                 :            : static const struct file_operations perf_fops;
    3591                 :            : 
    3592                 :            : static inline int perf_fget_light(int fd, struct fd *p)
    3593                 :            : {
    3594                 :          0 :         struct fd f = fdget(fd);
    3595   [ #  #  #  # ]:          0 :         if (!f.file)
    3596                 :            :                 return -EBADF;
    3597                 :            : 
    3598 [ #  # ][ #  # ]:          0 :         if (f.file->f_op != &perf_fops) {
    3599                 :            :                 fdput(f);
    3600                 :            :                 return -EBADF;
    3601                 :            :         }
    3602                 :            :         *p = f;
    3603                 :            :         return 0;
    3604                 :            : }
    3605                 :            : 
    3606                 :            : static int perf_event_set_output(struct perf_event *event,
    3607                 :            :                                  struct perf_event *output_event);
    3608                 :            : static int perf_event_set_filter(struct perf_event *event, void __user *arg);
    3609                 :            : 
    3610                 :          0 : static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    3611                 :            : {
    3612                 :          0 :         struct perf_event *event = file->private_data;
    3613                 :            :         void (*func)(struct perf_event *);
    3614                 :            :         u32 flags = arg;
    3615                 :            : 
    3616   [ #  #  #  #  :          0 :         switch (cmd) {
             #  #  #  #  
                      # ]
    3617                 :            :         case PERF_EVENT_IOC_ENABLE:
    3618                 :            :                 func = perf_event_enable;
    3619                 :            :                 break;
    3620                 :            :         case PERF_EVENT_IOC_DISABLE:
    3621                 :            :                 func = perf_event_disable;
    3622                 :          0 :                 break;
    3623                 :            :         case PERF_EVENT_IOC_RESET:
    3624                 :            :                 func = perf_event_reset;
    3625                 :          0 :                 break;
    3626                 :            : 
    3627                 :            :         case PERF_EVENT_IOC_REFRESH:
    3628                 :          0 :                 return perf_event_refresh(event, arg);
    3629                 :            : 
    3630                 :            :         case PERF_EVENT_IOC_PERIOD:
    3631                 :          0 :                 return perf_event_period(event, (u64 __user *)arg);
    3632                 :            : 
    3633                 :            :         case PERF_EVENT_IOC_ID:
    3634                 :            :         {
    3635                 :          0 :                 u64 id = primary_event_id(event);
    3636                 :            : 
    3637         [ #  # ]:          0 :                 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
    3638                 :            :                         return -EFAULT;
    3639                 :          0 :                 return 0;
    3640                 :            :         }
    3641                 :            : 
    3642                 :            :         case PERF_EVENT_IOC_SET_OUTPUT:
    3643                 :            :         {
    3644                 :            :                 int ret;
    3645         [ #  # ]:          0 :                 if (arg != -1) {
    3646                 :            :                         struct perf_event *output_event;
    3647                 :            :                         struct fd output;
    3648                 :            :                         ret = perf_fget_light(arg, &output);
    3649         [ #  # ]:          0 :                         if (ret)
    3650                 :            :                                 return ret;
    3651                 :          0 :                         output_event = output.file->private_data;
    3652                 :          0 :                         ret = perf_event_set_output(event, output_event);
    3653                 :            :                         fdput(output);
    3654                 :            :                 } else {
    3655                 :          0 :                         ret = perf_event_set_output(event, NULL);
    3656                 :            :                 }
    3657                 :          0 :                 return ret;
    3658                 :            :         }
    3659                 :            : 
    3660                 :            :         case PERF_EVENT_IOC_SET_FILTER:
    3661                 :          0 :                 return perf_event_set_filter(event, (void __user *)arg);
    3662                 :            : 
    3663                 :            :         default:
    3664                 :            :                 return -ENOTTY;
    3665                 :            :         }
    3666                 :            : 
    3667         [ #  # ]:          0 :         if (flags & PERF_IOC_FLAG_GROUP)
    3668                 :          0 :                 perf_event_for_each(event, func);
    3669                 :            :         else
    3670                 :          0 :                 perf_event_for_each_child(event, func);
    3671                 :            : 
    3672                 :            :         return 0;
    3673                 :            : }
    3674                 :            : 
    3675                 :          0 : int perf_event_task_enable(void)
    3676                 :            : {
    3677                 :            :         struct perf_event *event;
    3678                 :            : 
    3679                 :          0 :         mutex_lock(&current->perf_event_mutex);
    3680         [ #  # ]:          0 :         list_for_each_entry(event, &current->perf_event_list, owner_entry)
    3681                 :          0 :                 perf_event_for_each_child(event, perf_event_enable);
    3682                 :          0 :         mutex_unlock(&current->perf_event_mutex);
    3683                 :            : 
    3684                 :          0 :         return 0;
    3685                 :            : }
    3686                 :            : 
    3687                 :          0 : int perf_event_task_disable(void)
    3688                 :            : {
    3689                 :            :         struct perf_event *event;
    3690                 :            : 
    3691                 :          0 :         mutex_lock(&current->perf_event_mutex);
    3692         [ #  # ]:          0 :         list_for_each_entry(event, &current->perf_event_list, owner_entry)
    3693                 :          0 :                 perf_event_for_each_child(event, perf_event_disable);
    3694                 :          0 :         mutex_unlock(&current->perf_event_mutex);
    3695                 :            : 
    3696                 :          0 :         return 0;
    3697                 :            : }
    3698                 :            : 
    3699                 :            : static int perf_event_index(struct perf_event *event)
    3700                 :            : {
    3701         [ #  # ]:          0 :         if (event->hw.state & PERF_HES_STOPPED)
    3702                 :            :                 return 0;
    3703                 :            : 
    3704         [ #  # ]:          0 :         if (event->state != PERF_EVENT_STATE_ACTIVE)
    3705                 :            :                 return 0;
    3706                 :            : 
    3707                 :          0 :         return event->pmu->event_idx(event);
    3708                 :            : }
    3709                 :            : 
    3710                 :          0 : static void calc_timer_values(struct perf_event *event,
    3711                 :            :                                 u64 *now,
    3712                 :            :                                 u64 *enabled,
    3713                 :            :                                 u64 *running)
    3714                 :            : {
    3715                 :            :         u64 ctx_time;
    3716                 :            : 
    3717                 :          0 :         *now = perf_clock();
    3718                 :          0 :         ctx_time = event->shadow_ctx_time + *now;
    3719                 :          0 :         *enabled = ctx_time - event->tstamp_enabled;
    3720                 :          0 :         *running = ctx_time - event->tstamp_running;
    3721                 :          0 : }
    3722                 :            : 
    3723                 :            : static void perf_event_init_userpage(struct perf_event *event)
    3724                 :            : {
    3725                 :            :         struct perf_event_mmap_page *userpg;
    3726                 :            :         struct ring_buffer *rb;
    3727                 :            : 
    3728                 :            :         rcu_read_lock();
    3729                 :          0 :         rb = rcu_dereference(event->rb);
    3730         [ #  # ]:          0 :         if (!rb)
    3731                 :            :                 goto unlock;
    3732                 :            : 
    3733                 :          0 :         userpg = rb->user_page;
    3734                 :            : 
    3735                 :            :         /* Allow new userspace to detect that bit 0 is deprecated */
    3736                 :          0 :         userpg->cap_bit0_is_deprecated = 1;
    3737                 :          0 :         userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
    3738                 :            : 
    3739                 :            : unlock:
    3740                 :            :         rcu_read_unlock();
    3741                 :            : }
    3742                 :            : 
    3743                 :          0 : void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
    3744                 :            : {
    3745                 :          0 : }
    3746                 :            : 
    3747                 :            : /*
    3748                 :            :  * Callers need to ensure there can be no nesting of this function, otherwise
    3749                 :            :  * the seqlock logic goes bad. We can not serialize this because the arch
    3750                 :            :  * code calls this from NMI context.
    3751                 :            :  */
    3752                 :          0 : void perf_event_update_userpage(struct perf_event *event)
    3753                 :            : {
    3754                 :            :         struct perf_event_mmap_page *userpg;
    3755                 :            :         struct ring_buffer *rb;
    3756                 :            :         u64 enabled, running, now;
    3757                 :            : 
    3758                 :            :         rcu_read_lock();
    3759                 :          0 :         rb = rcu_dereference(event->rb);
    3760         [ #  # ]:          0 :         if (!rb)
    3761                 :            :                 goto unlock;
    3762                 :            : 
    3763                 :            :         /*
    3764                 :            :          * compute total_time_enabled, total_time_running
    3765                 :            :          * based on snapshot values taken when the event
    3766                 :            :          * was last scheduled in.
    3767                 :            :          *
    3768                 :            :          * we cannot simply called update_context_time()
    3769                 :            :          * because of locking issue as we can be called in
    3770                 :            :          * NMI context
    3771                 :            :          */
    3772                 :          0 :         calc_timer_values(event, &now, &enabled, &running);
    3773                 :            : 
    3774                 :          0 :         userpg = rb->user_page;
    3775                 :            :         /*
    3776                 :            :          * Disable preemption so as to not let the corresponding user-space
    3777                 :            :          * spin too long if we get preempted.
    3778                 :            :          */
    3779                 :          0 :         preempt_disable();
    3780                 :          0 :         ++userpg->lock;
    3781                 :          0 :         barrier();
    3782                 :          0 :         userpg->index = perf_event_index(event);
    3783                 :          0 :         userpg->offset = perf_event_count(event);
    3784         [ #  # ]:          0 :         if (userpg->index)
    3785                 :          0 :                 userpg->offset -= local64_read(&event->hw.prev_count);
    3786                 :            : 
    3787                 :          0 :         userpg->time_enabled = enabled +
    3788                 :          0 :                         atomic64_read(&event->child_total_time_enabled);
    3789                 :            : 
    3790                 :          0 :         userpg->time_running = running +
    3791                 :          0 :                         atomic64_read(&event->child_total_time_running);
    3792                 :            : 
    3793                 :          0 :         arch_perf_update_userpage(userpg, now);
    3794                 :            : 
    3795                 :          0 :         barrier();
    3796                 :          0 :         ++userpg->lock;
    3797                 :          0 :         preempt_enable();
    3798                 :            : unlock:
    3799                 :            :         rcu_read_unlock();
    3800                 :          0 : }
    3801                 :            : 
    3802                 :          0 : static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    3803                 :            : {
    3804                 :          0 :         struct perf_event *event = vma->vm_file->private_data;
    3805                 :            :         struct ring_buffer *rb;
    3806                 :            :         int ret = VM_FAULT_SIGBUS;
    3807                 :            : 
    3808         [ #  # ]:          0 :         if (vmf->flags & FAULT_FLAG_MKWRITE) {
    3809         [ #  # ]:          0 :                 if (vmf->pgoff == 0)
    3810                 :            :                         ret = 0;
    3811                 :          0 :                 return ret;
    3812                 :            :         }
    3813                 :            : 
    3814                 :            :         rcu_read_lock();
    3815                 :          0 :         rb = rcu_dereference(event->rb);
    3816         [ #  # ]:          0 :         if (!rb)
    3817                 :            :                 goto unlock;
    3818                 :            : 
    3819 [ #  # ][ #  # ]:          0 :         if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
    3820                 :            :                 goto unlock;
    3821                 :            : 
    3822                 :          0 :         vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
    3823         [ #  # ]:          0 :         if (!vmf->page)
    3824                 :            :                 goto unlock;
    3825                 :            : 
    3826                 :            :         get_page(vmf->page);
    3827                 :          0 :         vmf->page->mapping = vma->vm_file->f_mapping;
    3828                 :          0 :         vmf->page->index   = vmf->pgoff;
    3829                 :            : 
    3830                 :            :         ret = 0;
    3831                 :            : unlock:
    3832                 :            :         rcu_read_unlock();
    3833                 :            : 
    3834                 :          0 :         return ret;
    3835                 :            : }
    3836                 :            : 
    3837                 :          0 : static void ring_buffer_attach(struct perf_event *event,
    3838                 :            :                                struct ring_buffer *rb)
    3839                 :            : {
    3840                 :            :         unsigned long flags;
    3841                 :            : 
    3842         [ #  # ]:          0 :         if (!list_empty(&event->rb_entry))
    3843                 :          0 :                 return;
    3844                 :            : 
    3845                 :          0 :         spin_lock_irqsave(&rb->event_lock, flags);
    3846         [ #  # ]:          0 :         if (list_empty(&event->rb_entry))
    3847                 :          0 :                 list_add(&event->rb_entry, &rb->event_list);
    3848                 :            :         spin_unlock_irqrestore(&rb->event_lock, flags);
    3849                 :            : }
    3850                 :            : 
    3851                 :          0 : static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
    3852                 :            : {
    3853                 :            :         unsigned long flags;
    3854                 :            : 
    3855         [ #  # ]:          0 :         if (list_empty(&event->rb_entry))
    3856                 :          0 :                 return;
    3857                 :            : 
    3858                 :          0 :         spin_lock_irqsave(&rb->event_lock, flags);
    3859                 :            :         list_del_init(&event->rb_entry);
    3860                 :          0 :         wake_up_all(&event->waitq);
    3861                 :            :         spin_unlock_irqrestore(&rb->event_lock, flags);
    3862                 :            : }
    3863                 :            : 
    3864                 :          0 : static void ring_buffer_wakeup(struct perf_event *event)
    3865                 :            : {
    3866                 :            :         struct ring_buffer *rb;
    3867                 :            : 
    3868                 :            :         rcu_read_lock();
    3869                 :          0 :         rb = rcu_dereference(event->rb);
    3870         [ #  # ]:          0 :         if (rb) {
    3871         [ #  # ]:          0 :                 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
    3872                 :          0 :                         wake_up_all(&event->waitq);
    3873                 :            :         }
    3874                 :            :         rcu_read_unlock();
    3875                 :          0 : }
    3876                 :            : 
    3877                 :          0 : static void rb_free_rcu(struct rcu_head *rcu_head)
    3878                 :            : {
    3879                 :            :         struct ring_buffer *rb;
    3880                 :            : 
    3881                 :          0 :         rb = container_of(rcu_head, struct ring_buffer, rcu_head);
    3882                 :          0 :         rb_free(rb);
    3883                 :          0 : }
    3884                 :            : 
    3885                 :          0 : static struct ring_buffer *ring_buffer_get(struct perf_event *event)
    3886                 :            : {
    3887                 :            :         struct ring_buffer *rb;
    3888                 :            : 
    3889                 :            :         rcu_read_lock();
    3890                 :          0 :         rb = rcu_dereference(event->rb);
    3891         [ #  # ]:          0 :         if (rb) {
    3892         [ #  # ]:          0 :                 if (!atomic_inc_not_zero(&rb->refcount))
    3893                 :            :                         rb = NULL;
    3894                 :            :         }
    3895                 :            :         rcu_read_unlock();
    3896                 :            : 
    3897                 :          0 :         return rb;
    3898                 :            : }
    3899                 :            : 
    3900                 :          0 : static void ring_buffer_put(struct ring_buffer *rb)
    3901                 :            : {
    3902         [ #  # ]:          0 :         if (!atomic_dec_and_test(&rb->refcount))
    3903                 :          0 :                 return;
    3904                 :            : 
    3905 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(!list_empty(&rb->event_list));
                 [ #  # ]
    3906                 :            : 
    3907                 :          0 :         call_rcu(&rb->rcu_head, rb_free_rcu);
    3908                 :            : }
    3909                 :            : 
    3910                 :          0 : static void perf_mmap_open(struct vm_area_struct *vma)
    3911                 :            : {
    3912                 :          0 :         struct perf_event *event = vma->vm_file->private_data;
    3913                 :            : 
    3914                 :          0 :         atomic_inc(&event->mmap_count);
    3915                 :          0 :         atomic_inc(&event->rb->mmap_count);
    3916                 :          0 : }
    3917                 :            : 
    3918                 :            : /*
    3919                 :            :  * A buffer can be mmap()ed multiple times; either directly through the same
    3920                 :            :  * event, or through other events by use of perf_event_set_output().
    3921                 :            :  *
    3922                 :            :  * In order to undo the VM accounting done by perf_mmap() we need to destroy
    3923                 :            :  * the buffer here, where we still have a VM context. This means we need
    3924                 :            :  * to detach all events redirecting to us.
    3925                 :            :  */
    3926                 :          0 : static void perf_mmap_close(struct vm_area_struct *vma)
    3927                 :            : {
    3928                 :          0 :         struct perf_event *event = vma->vm_file->private_data;
    3929                 :            : 
    3930                 :          0 :         struct ring_buffer *rb = event->rb;
    3931                 :          0 :         struct user_struct *mmap_user = rb->mmap_user;
    3932                 :          0 :         int mmap_locked = rb->mmap_locked;
    3933                 :            :         unsigned long size = perf_data_size(rb);
    3934                 :            : 
    3935                 :          0 :         atomic_dec(&rb->mmap_count);
    3936                 :            : 
    3937         [ #  # ]:          0 :         if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
    3938                 :            :                 return;
    3939                 :            : 
    3940                 :            :         /* Detach current event from the buffer. */
    3941                 :          0 :         rcu_assign_pointer(event->rb, NULL);
    3942                 :          0 :         ring_buffer_detach(event, rb);
    3943                 :          0 :         mutex_unlock(&event->mmap_mutex);
    3944                 :            : 
    3945                 :            :         /* If there's still other mmap()s of this buffer, we're done. */
    3946         [ #  # ]:          0 :         if (atomic_read(&rb->mmap_count)) {
    3947                 :          0 :                 ring_buffer_put(rb); /* can't be last */
    3948                 :          0 :                 return;
    3949                 :            :         }
    3950                 :            : 
    3951                 :            :         /*
    3952                 :            :          * No other mmap()s, detach from all other events that might redirect
    3953                 :            :          * into the now unreachable buffer. Somewhat complicated by the
    3954                 :            :          * fact that rb::event_lock otherwise nests inside mmap_mutex.
    3955                 :            :          */
    3956                 :            : again:
    3957                 :            :         rcu_read_lock();
    3958         [ #  # ]:          0 :         list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
    3959         [ #  # ]:          0 :                 if (!atomic_long_inc_not_zero(&event->refcount)) {
    3960                 :            :                         /*
    3961                 :            :                          * This event is en-route to free_event() which will
    3962                 :            :                          * detach it and remove it from the list.
    3963                 :            :                          */
    3964                 :          0 :                         continue;
    3965                 :            :                 }
    3966                 :            :                 rcu_read_unlock();
    3967                 :            : 
    3968                 :          0 :                 mutex_lock(&event->mmap_mutex);
    3969                 :            :                 /*
    3970                 :            :                  * Check we didn't race with perf_event_set_output() which can
    3971                 :            :                  * swizzle the rb from under us while we were waiting to
    3972                 :            :                  * acquire mmap_mutex.
    3973                 :            :                  *
    3974                 :            :                  * If we find a different rb; ignore this event, a next
    3975                 :            :                  * iteration will no longer find it on the list. We have to
    3976                 :            :                  * still restart the iteration to make sure we're not now
    3977                 :            :                  * iterating the wrong list.
    3978                 :            :                  */
    3979         [ #  # ]:          0 :                 if (event->rb == rb) {
    3980                 :          0 :                         rcu_assign_pointer(event->rb, NULL);
    3981                 :          0 :                         ring_buffer_detach(event, rb);
    3982                 :          0 :                         ring_buffer_put(rb); /* can't be last, we still have one */
    3983                 :            :                 }
    3984                 :          0 :                 mutex_unlock(&event->mmap_mutex);
    3985                 :          0 :                 put_event(event);
    3986                 :            : 
    3987                 :            :                 /*
    3988                 :            :                  * Restart the iteration; either we're on the wrong list or
    3989                 :            :                  * destroyed its integrity by doing a deletion.
    3990                 :            :                  */
    3991                 :          0 :                 goto again;
    3992                 :            :         }
    3993                 :            :         rcu_read_unlock();
    3994                 :            : 
    3995                 :            :         /*
    3996                 :            :          * It could be there's still a few 0-ref events on the list; they'll
    3997                 :            :          * get cleaned up by free_event() -- they'll also still have their
    3998                 :            :          * ref on the rb and will free it whenever they are done with it.
    3999                 :            :          *
    4000                 :            :          * Aside from that, this buffer is 'fully' detached and unmapped,
    4001                 :            :          * undo the VM accounting.
    4002                 :            :          */
    4003                 :            : 
    4004                 :          0 :         atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
    4005                 :          0 :         vma->vm_mm->pinned_vm -= mmap_locked;
    4006                 :          0 :         free_uid(mmap_user);
    4007                 :            : 
    4008                 :          0 :         ring_buffer_put(rb); /* could be last */
    4009                 :            : }
    4010                 :            : 
    4011                 :            : static const struct vm_operations_struct perf_mmap_vmops = {
    4012                 :            :         .open           = perf_mmap_open,
    4013                 :            :         .close          = perf_mmap_close,
    4014                 :            :         .fault          = perf_mmap_fault,
    4015                 :            :         .page_mkwrite   = perf_mmap_fault,
    4016                 :            : };
    4017                 :            : 
    4018                 :          0 : static int perf_mmap(struct file *file, struct vm_area_struct *vma)
    4019                 :            : {
    4020                 :          0 :         struct perf_event *event = file->private_data;
    4021                 :            :         unsigned long user_locked, user_lock_limit;
    4022                 :          0 :         struct user_struct *user = current_user();
    4023                 :            :         unsigned long locked, lock_limit;
    4024                 :            :         struct ring_buffer *rb;
    4025                 :            :         unsigned long vma_size;
    4026                 :            :         unsigned long nr_pages;
    4027                 :            :         long user_extra, extra;
    4028                 :            :         int ret = 0, flags = 0;
    4029                 :            : 
    4030                 :            :         /*
    4031                 :            :          * Don't allow mmap() of inherited per-task counters. This would
    4032                 :            :          * create a performance issue due to all children writing to the
    4033                 :            :          * same rb.
    4034                 :            :          */
    4035 [ #  # ][ #  # ]:          0 :         if (event->cpu == -1 && event->attr.inherit)
    4036                 :            :                 return -EINVAL;
    4037                 :            : 
    4038         [ #  # ]:          0 :         if (!(vma->vm_flags & VM_SHARED))
    4039                 :            :                 return -EINVAL;
    4040                 :            : 
    4041                 :          0 :         vma_size = vma->vm_end - vma->vm_start;
    4042                 :          0 :         nr_pages = (vma_size / PAGE_SIZE) - 1;
    4043                 :            : 
    4044                 :            :         /*
    4045                 :            :          * If we have rb pages ensure they're a power-of-two number, so we
    4046                 :            :          * can do bitmasks instead of modulo.
    4047                 :            :          */
    4048 [ #  # ][ #  # ]:          0 :         if (nr_pages != 0 && !is_power_of_2(nr_pages))
    4049                 :            :                 return -EINVAL;
    4050                 :            : 
    4051         [ #  # ]:          0 :         if (vma_size != PAGE_SIZE * (1 + nr_pages))
    4052                 :            :                 return -EINVAL;
    4053                 :            : 
    4054         [ #  # ]:          0 :         if (vma->vm_pgoff != 0)
    4055                 :            :                 return -EINVAL;
    4056                 :            : 
    4057 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(event->ctx->parent_ctx);
                 [ #  # ]
    4058                 :            : again:
    4059                 :          0 :         mutex_lock(&event->mmap_mutex);
    4060         [ #  # ]:          0 :         if (event->rb) {
    4061         [ #  # ]:          0 :                 if (event->rb->nr_pages != nr_pages) {
    4062                 :            :                         ret = -EINVAL;
    4063                 :            :                         goto unlock;
    4064                 :            :                 }
    4065                 :            : 
    4066         [ #  # ]:          0 :                 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
    4067                 :            :                         /*
    4068                 :            :                          * Raced against perf_mmap_close() through
    4069                 :            :                          * perf_event_set_output(). Try again, hope for better
    4070                 :            :                          * luck.
    4071                 :            :                          */
    4072                 :          0 :                         mutex_unlock(&event->mmap_mutex);
    4073                 :          0 :                         goto again;
    4074                 :            :                 }
    4075                 :            : 
    4076                 :            :                 goto unlock;
    4077                 :            :         }
    4078                 :            : 
    4079                 :          0 :         user_extra = nr_pages + 1;
    4080                 :          0 :         user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
    4081                 :            : 
    4082                 :            :         /*
    4083                 :            :          * Increase the limit linearly with more CPUs:
    4084                 :            :          */
    4085                 :          0 :         user_lock_limit *= num_online_cpus();
    4086                 :            : 
    4087                 :          0 :         user_locked = atomic_long_read(&user->locked_vm) + user_extra;
    4088                 :            : 
    4089                 :            :         extra = 0;
    4090         [ #  # ]:          0 :         if (user_locked > user_lock_limit)
    4091                 :          0 :                 extra = user_locked - user_lock_limit;
    4092                 :            : 
    4093                 :            :         lock_limit = rlimit(RLIMIT_MEMLOCK);
    4094                 :          0 :         lock_limit >>= PAGE_SHIFT;
    4095                 :          0 :         locked = vma->vm_mm->pinned_vm + extra;
    4096                 :            : 
    4097         [ #  # ]:          0 :         if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
           [ #  #  #  # ]
    4098                 :          0 :                 !capable(CAP_IPC_LOCK)) {
    4099                 :            :                 ret = -EPERM;
    4100                 :            :                 goto unlock;
    4101                 :            :         }
    4102                 :            : 
    4103         [ #  # ]:          0 :         WARN_ON(event->rb);
    4104                 :            : 
    4105         [ #  # ]:          0 :         if (vma->vm_flags & VM_WRITE)
    4106                 :            :                 flags |= RING_BUFFER_WRITABLE;
    4107                 :            : 
    4108         [ #  # ]:          0 :         rb = rb_alloc(nr_pages, 
    4109                 :          0 :                 event->attr.watermark ? event->attr.wakeup_watermark : 0,
    4110                 :            :                 event->cpu, flags);
    4111                 :            : 
    4112         [ #  # ]:          0 :         if (!rb) {
    4113                 :            :                 ret = -ENOMEM;
    4114                 :            :                 goto unlock;
    4115                 :            :         }
    4116                 :            : 
    4117                 :          0 :         atomic_set(&rb->mmap_count, 1);
    4118                 :          0 :         rb->mmap_locked = extra;
    4119                 :          0 :         rb->mmap_user = get_current_user();
    4120                 :            : 
    4121                 :          0 :         atomic_long_add(user_extra, &user->locked_vm);
    4122                 :          0 :         vma->vm_mm->pinned_vm += extra;
    4123                 :            : 
    4124                 :          0 :         ring_buffer_attach(event, rb);
    4125                 :          0 :         rcu_assign_pointer(event->rb, rb);
    4126                 :            : 
    4127                 :            :         perf_event_init_userpage(event);
    4128                 :          0 :         perf_event_update_userpage(event);
    4129                 :            : 
    4130                 :            : unlock:
    4131         [ #  # ]:          0 :         if (!ret)
    4132                 :          0 :                 atomic_inc(&event->mmap_count);
    4133                 :          0 :         mutex_unlock(&event->mmap_mutex);
    4134                 :            : 
    4135                 :            :         /*
    4136                 :            :          * Since pinned accounting is per vm we cannot allow fork() to copy our
    4137                 :            :          * vma.
    4138                 :            :          */
    4139                 :          0 :         vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
    4140                 :          0 :         vma->vm_ops = &perf_mmap_vmops;
    4141                 :            : 
    4142                 :          0 :         return ret;
    4143                 :            : }
    4144                 :            : 
    4145                 :          0 : static int perf_fasync(int fd, struct file *filp, int on)
    4146                 :            : {
    4147                 :            :         struct inode *inode = file_inode(filp);
    4148                 :          0 :         struct perf_event *event = filp->private_data;
    4149                 :            :         int retval;
    4150                 :            : 
    4151                 :          0 :         mutex_lock(&inode->i_mutex);
    4152                 :          0 :         retval = fasync_helper(fd, filp, on, &event->fasync);
    4153                 :          0 :         mutex_unlock(&inode->i_mutex);
    4154                 :            : 
    4155         [ #  # ]:          0 :         if (retval < 0)
    4156                 :          0 :                 return retval;
    4157                 :            : 
    4158                 :            :         return 0;
    4159                 :            : }
    4160                 :            : 
    4161                 :            : static const struct file_operations perf_fops = {
    4162                 :            :         .llseek                 = no_llseek,
    4163                 :            :         .release                = perf_release,
    4164                 :            :         .read                   = perf_read,
    4165                 :            :         .poll                   = perf_poll,
    4166                 :            :         .unlocked_ioctl         = perf_ioctl,
    4167                 :            :         .compat_ioctl           = perf_ioctl,
    4168                 :            :         .mmap                   = perf_mmap,
    4169                 :            :         .fasync                 = perf_fasync,
    4170                 :            : };
    4171                 :            : 
    4172                 :            : /*
    4173                 :            :  * Perf event wakeup
    4174                 :            :  *
    4175                 :            :  * If there's data, ensure we set the poll() state and publish everything
    4176                 :            :  * to user-space before waking everybody up.
    4177                 :            :  */
    4178                 :            : 
    4179                 :          0 : void perf_event_wakeup(struct perf_event *event)
    4180                 :            : {
    4181                 :          0 :         ring_buffer_wakeup(event);
    4182                 :            : 
    4183         [ #  # ]:          0 :         if (event->pending_kill) {
    4184                 :          0 :                 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
    4185                 :          0 :                 event->pending_kill = 0;
    4186                 :            :         }
    4187                 :          0 : }
    4188                 :            : 
    4189                 :          0 : static void perf_pending_event(struct irq_work *entry)
    4190                 :            : {
    4191                 :          0 :         struct perf_event *event = container_of(entry,
    4192                 :            :                         struct perf_event, pending);
    4193                 :            : 
    4194         [ #  # ]:          0 :         if (event->pending_disable) {
    4195                 :          0 :                 event->pending_disable = 0;
    4196                 :          0 :                 __perf_event_disable(event);
    4197                 :            :         }
    4198                 :            : 
    4199         [ #  # ]:          0 :         if (event->pending_wakeup) {
    4200                 :          0 :                 event->pending_wakeup = 0;
    4201                 :          0 :                 perf_event_wakeup(event);
    4202                 :            :         }
    4203                 :          0 : }
    4204                 :            : 
    4205                 :            : /*
    4206                 :            :  * We assume there is only KVM supporting the callbacks.
    4207                 :            :  * Later on, we might change it to a list if there is
    4208                 :            :  * another virtualization implementation supporting the callbacks.
    4209                 :            :  */
    4210                 :            : struct perf_guest_info_callbacks *perf_guest_cbs;
    4211                 :            : 
    4212                 :          0 : int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
    4213                 :            : {
    4214                 :          0 :         perf_guest_cbs = cbs;
    4215                 :          0 :         return 0;
    4216                 :            : }
    4217                 :            : EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
    4218                 :            : 
    4219                 :          0 : int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
    4220                 :            : {
    4221                 :          0 :         perf_guest_cbs = NULL;
    4222                 :          0 :         return 0;
    4223                 :            : }
    4224                 :            : EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
    4225                 :            : 
    4226                 :            : static void
    4227                 :          0 : perf_output_sample_regs(struct perf_output_handle *handle,
    4228                 :            :                         struct pt_regs *regs, u64 mask)
    4229                 :            : {
    4230                 :            :         int bit;
    4231                 :            : 
    4232         [ #  # ]:          0 :         for_each_set_bit(bit, (const unsigned long *) &mask,
    4233                 :            :                          sizeof(mask) * BITS_PER_BYTE) {
    4234                 :            :                 u64 val;
    4235                 :            : 
    4236                 :          0 :                 val = perf_reg_value(regs, bit);
    4237                 :          0 :                 perf_output_put(handle, val);
    4238                 :            :         }
    4239                 :          0 : }
    4240                 :            : 
    4241                 :          0 : static void perf_sample_regs_user(struct perf_regs_user *regs_user,
    4242                 :            :                                   struct pt_regs *regs)
    4243                 :            : {
    4244         [ #  # ]:          0 :         if (!user_mode(regs)) {
    4245         [ #  # ]:          0 :                 if (current->mm)
    4246                 :          0 :                         regs = task_pt_regs(current);
    4247                 :            :                 else
    4248                 :            :                         regs = NULL;
    4249                 :            :         }
    4250                 :            : 
    4251         [ #  # ]:          0 :         if (regs) {
    4252                 :          0 :                 regs_user->regs = regs;
    4253                 :          0 :                 regs_user->abi  = perf_reg_abi(current);
    4254                 :            :         }
    4255                 :          0 : }
    4256                 :            : 
    4257                 :            : /*
    4258                 :            :  * Get remaining task size from user stack pointer.
    4259                 :            :  *
    4260                 :            :  * It'd be better to take stack vma map and limit this more
    4261                 :            :  * precisly, but there's no way to get it safely under interrupt,
    4262                 :            :  * so using TASK_SIZE as limit.
    4263                 :            :  */
    4264                 :          0 : static u64 perf_ustack_task_size(struct pt_regs *regs)
    4265                 :            : {
    4266                 :            :         unsigned long addr = perf_user_stack_pointer(regs);
    4267                 :            : 
    4268         [ #  # ]:          0 :         if (!addr || addr >= TASK_SIZE)
    4269                 :            :                 return 0;
    4270                 :            : 
    4271                 :          0 :         return TASK_SIZE - addr;
    4272                 :            : }
    4273                 :            : 
    4274                 :            : static u16
    4275                 :          0 : perf_sample_ustack_size(u16 stack_size, u16 header_size,
    4276                 :            :                         struct pt_regs *regs)
    4277                 :            : {
    4278                 :            :         u64 task_size;
    4279                 :            : 
    4280                 :            :         /* No regs, no stack pointer, no dump. */
    4281         [ #  # ]:          0 :         if (!regs)
    4282                 :            :                 return 0;
    4283                 :            : 
    4284                 :            :         /*
    4285                 :            :          * Check if we fit in with the requested stack size into the:
    4286                 :            :          * - TASK_SIZE
    4287                 :            :          *   If we don't, we limit the size to the TASK_SIZE.
    4288                 :            :          *
    4289                 :            :          * - remaining sample size
    4290                 :            :          *   If we don't, we customize the stack size to
    4291                 :            :          *   fit in to the remaining sample size.
    4292                 :            :          */
    4293                 :            : 
    4294                 :          0 :         task_size  = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
    4295         [ #  # ]:          0 :         stack_size = min(stack_size, (u16) task_size);
    4296                 :            : 
    4297                 :            :         /* Current header size plus static size and dynamic size. */
    4298                 :          0 :         header_size += 2 * sizeof(u64);
    4299                 :            : 
    4300                 :            :         /* Do we fit in with the current stack dump size? */
    4301         [ #  # ]:          0 :         if ((u16) (header_size + stack_size) < header_size) {
    4302                 :            :                 /*
    4303                 :            :                  * If we overflow the maximum size for the sample,
    4304                 :            :                  * we customize the stack dump size to fit in.
    4305                 :            :                  */
    4306                 :            :                 stack_size = USHRT_MAX - header_size - sizeof(u64);
    4307                 :          0 :                 stack_size = round_up(stack_size, sizeof(u64));
    4308                 :            :         }
    4309                 :            : 
    4310                 :          0 :         return stack_size;
    4311                 :            : }
    4312                 :            : 
    4313                 :            : static void
    4314                 :          0 : perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
    4315                 :          0 :                           struct pt_regs *regs)
    4316                 :            : {
    4317                 :            :         /* Case of a kernel thread, nothing to dump */
    4318         [ #  # ]:          0 :         if (!regs) {
    4319                 :          0 :                 u64 size = 0;
    4320                 :          0 :                 perf_output_put(handle, size);
    4321                 :            :         } else {
    4322                 :            :                 unsigned long sp;
    4323                 :            :                 unsigned int rem;
    4324                 :            :                 u64 dyn_size;
    4325                 :            : 
    4326                 :            :                 /*
    4327                 :            :                  * We dump:
    4328                 :            :                  * static size
    4329                 :            :                  *   - the size requested by user or the best one we can fit
    4330                 :            :                  *     in to the sample max size
    4331                 :            :                  * data
    4332                 :            :                  *   - user stack dump data
    4333                 :            :                  * dynamic size
    4334                 :            :                  *   - the actual dumped size
    4335                 :            :                  */
    4336                 :            : 
    4337                 :            :                 /* Static size. */
    4338                 :          0 :                 perf_output_put(handle, dump_size);
    4339                 :            : 
    4340                 :            :                 /* Data. */
    4341                 :            :                 sp = perf_user_stack_pointer(regs);
    4342                 :          0 :                 rem = __output_copy_user(handle, (void *) sp, dump_size);
    4343                 :          0 :                 dyn_size = dump_size - rem;
    4344                 :            : 
    4345                 :          0 :                 perf_output_skip(handle, rem);
    4346                 :            : 
    4347                 :            :                 /* Dynamic size. */
    4348                 :          0 :                 perf_output_put(handle, dyn_size);
    4349                 :            :         }
    4350                 :          0 : }
    4351                 :            : 
    4352                 :          0 : static void __perf_event_header__init_id(struct perf_event_header *header,
    4353                 :            :                                          struct perf_sample_data *data,
    4354                 :            :                                          struct perf_event *event)
    4355                 :            : {
    4356                 :          0 :         u64 sample_type = event->attr.sample_type;
    4357                 :            : 
    4358                 :          0 :         data->type = sample_type;
    4359                 :          0 :         header->size += event->id_header_size;
    4360                 :            : 
    4361         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TID) {
    4362                 :            :                 /* namespace issues */
    4363                 :          0 :                 data->tid_entry.pid = perf_event_pid(event, current);
    4364                 :          0 :                 data->tid_entry.tid = perf_event_tid(event, current);
    4365                 :            :         }
    4366                 :            : 
    4367         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TIME)
    4368                 :          0 :                 data->time = perf_clock();
    4369                 :            : 
    4370         [ #  # ]:          0 :         if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
    4371                 :          0 :                 data->id = primary_event_id(event);
    4372                 :            : 
    4373         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STREAM_ID)
    4374                 :          0 :                 data->stream_id = event->id;
    4375                 :            : 
    4376         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CPU) {
    4377                 :          0 :                 data->cpu_entry.cpu   = raw_smp_processor_id();
    4378                 :          0 :                 data->cpu_entry.reserved = 0;
    4379                 :            :         }
    4380                 :          0 : }
    4381                 :            : 
    4382                 :          0 : void perf_event_header__init_id(struct perf_event_header *header,
    4383                 :            :                                 struct perf_sample_data *data,
    4384                 :            :                                 struct perf_event *event)
    4385                 :            : {
    4386 [ #  # ][ #  # ]:          0 :         if (event->attr.sample_id_all)
                 [ #  # ]
           [ #  #  #  # ]
                 [ #  # ]
    4387                 :          0 :                 __perf_event_header__init_id(header, data, event);
    4388                 :          0 : }
    4389                 :            : 
    4390                 :          0 : static void __perf_event__output_id_sample(struct perf_output_handle *handle,
    4391                 :            :                                            struct perf_sample_data *data)
    4392                 :            : {
    4393                 :          0 :         u64 sample_type = data->type;
    4394                 :            : 
    4395         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TID)
    4396                 :          0 :                 perf_output_put(handle, data->tid_entry);
    4397                 :            : 
    4398         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TIME)
    4399                 :          0 :                 perf_output_put(handle, data->time);
    4400                 :            : 
    4401         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_ID)
    4402                 :          0 :                 perf_output_put(handle, data->id);
    4403                 :            : 
    4404         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STREAM_ID)
    4405                 :          0 :                 perf_output_put(handle, data->stream_id);
    4406                 :            : 
    4407         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CPU)
    4408                 :          0 :                 perf_output_put(handle, data->cpu_entry);
    4409                 :            : 
    4410         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IDENTIFIER)
    4411                 :          0 :                 perf_output_put(handle, data->id);
    4412                 :          0 : }
    4413                 :            : 
    4414                 :          0 : void perf_event__output_id_sample(struct perf_event *event,
    4415                 :            :                                   struct perf_output_handle *handle,
    4416                 :            :                                   struct perf_sample_data *sample)
    4417                 :            : {
    4418 [ #  # ][ #  # ]:          0 :         if (event->attr.sample_id_all)
           [ #  #  #  #  
           #  # ][ #  # ]
    4419                 :          0 :                 __perf_event__output_id_sample(handle, sample);
    4420                 :          0 : }
    4421                 :            : 
    4422                 :          0 : static void perf_output_read_one(struct perf_output_handle *handle,
    4423                 :            :                                  struct perf_event *event,
    4424                 :            :                                  u64 enabled, u64 running)
    4425                 :            : {
    4426                 :          0 :         u64 read_format = event->attr.read_format;
    4427                 :            :         u64 values[4];
    4428                 :            :         int n = 0;
    4429                 :            : 
    4430                 :          0 :         values[n++] = perf_event_count(event);
    4431         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
    4432                 :          0 :                 values[n++] = enabled +
    4433                 :          0 :                         atomic64_read(&event->child_total_time_enabled);
    4434                 :            :         }
    4435         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
    4436                 :          0 :                 values[n++] = running +
    4437                 :          0 :                         atomic64_read(&event->child_total_time_running);
    4438                 :            :         }
    4439         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_ID)
    4440                 :          0 :                 values[n++] = primary_event_id(event);
    4441                 :            : 
    4442                 :          0 :         __output_copy(handle, values, n * sizeof(u64));
    4443                 :          0 : }
    4444                 :            : 
    4445                 :            : /*
    4446                 :            :  * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
    4447                 :            :  */
    4448                 :          0 : static void perf_output_read_group(struct perf_output_handle *handle,
    4449                 :            :                             struct perf_event *event,
    4450                 :            :                             u64 enabled, u64 running)
    4451                 :            : {
    4452                 :          0 :         struct perf_event *leader = event->group_leader, *sub;
    4453                 :          0 :         u64 read_format = event->attr.read_format;
    4454                 :            :         u64 values[5];
    4455                 :            :         int n = 0;
    4456                 :            : 
    4457                 :          0 :         values[n++] = 1 + leader->nr_siblings;
    4458                 :            : 
    4459         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
    4460                 :          0 :                 values[n++] = enabled;
    4461                 :            : 
    4462         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
    4463                 :          0 :                 values[n++] = running;
    4464                 :            : 
    4465         [ #  # ]:          0 :         if (leader != event)
    4466                 :          0 :                 leader->pmu->read(leader);
    4467                 :            : 
    4468                 :          0 :         values[n++] = perf_event_count(leader);
    4469         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_ID)
    4470                 :          0 :                 values[n++] = primary_event_id(leader);
    4471                 :            : 
    4472                 :          0 :         __output_copy(handle, values, n * sizeof(u64));
    4473                 :            : 
    4474         [ #  # ]:          0 :         list_for_each_entry(sub, &leader->sibling_list, group_entry) {
    4475                 :            :                 n = 0;
    4476                 :            : 
    4477 [ #  # ][ #  # ]:          0 :                 if ((sub != event) &&
    4478                 :          0 :                     (sub->state == PERF_EVENT_STATE_ACTIVE))
    4479                 :          0 :                         sub->pmu->read(sub);
    4480                 :            : 
    4481                 :          0 :                 values[n++] = perf_event_count(sub);
    4482         [ #  # ]:          0 :                 if (read_format & PERF_FORMAT_ID)
    4483                 :          0 :                         values[n++] = primary_event_id(sub);
    4484                 :            : 
    4485                 :          0 :                 __output_copy(handle, values, n * sizeof(u64));
    4486                 :            :         }
    4487                 :          0 : }
    4488                 :            : 
    4489                 :            : #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
    4490                 :            :                                  PERF_FORMAT_TOTAL_TIME_RUNNING)
    4491                 :            : 
    4492                 :          0 : static void perf_output_read(struct perf_output_handle *handle,
    4493                 :            :                              struct perf_event *event)
    4494                 :            : {
    4495                 :          0 :         u64 enabled = 0, running = 0, now;
    4496                 :          0 :         u64 read_format = event->attr.read_format;
    4497                 :            : 
    4498                 :            :         /*
    4499                 :            :          * compute total_time_enabled, total_time_running
    4500                 :            :          * based on snapshot values taken when the event
    4501                 :            :          * was last scheduled in.
    4502                 :            :          *
    4503                 :            :          * we cannot simply called update_context_time()
    4504                 :            :          * because of locking issue as we are called in
    4505                 :            :          * NMI context
    4506                 :            :          */
    4507         [ #  # ]:          0 :         if (read_format & PERF_FORMAT_TOTAL_TIMES)
    4508                 :          0 :                 calc_timer_values(event, &now, &enabled, &running);
    4509                 :            : 
    4510         [ #  # ]:          0 :         if (event->attr.read_format & PERF_FORMAT_GROUP)
    4511                 :          0 :                 perf_output_read_group(handle, event, enabled, running);
    4512                 :            :         else
    4513                 :          0 :                 perf_output_read_one(handle, event, enabled, running);
    4514                 :          0 : }
    4515                 :            : 
    4516                 :          0 : void perf_output_sample(struct perf_output_handle *handle,
    4517                 :            :                         struct perf_event_header *header,
    4518                 :            :                         struct perf_sample_data *data,
    4519                 :            :                         struct perf_event *event)
    4520                 :            : {
    4521                 :          0 :         u64 sample_type = data->type;
    4522                 :            : 
    4523                 :          0 :         perf_output_put(handle, *header);
    4524                 :            : 
    4525         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IDENTIFIER)
    4526                 :          0 :                 perf_output_put(handle, data->id);
    4527                 :            : 
    4528         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IP)
    4529                 :          0 :                 perf_output_put(handle, data->ip);
    4530                 :            : 
    4531         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TID)
    4532                 :          0 :                 perf_output_put(handle, data->tid_entry);
    4533                 :            : 
    4534         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TIME)
    4535                 :          0 :                 perf_output_put(handle, data->time);
    4536                 :            : 
    4537         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_ADDR)
    4538                 :          0 :                 perf_output_put(handle, data->addr);
    4539                 :            : 
    4540         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_ID)
    4541                 :          0 :                 perf_output_put(handle, data->id);
    4542                 :            : 
    4543         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STREAM_ID)
    4544                 :          0 :                 perf_output_put(handle, data->stream_id);
    4545                 :            : 
    4546         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CPU)
    4547                 :          0 :                 perf_output_put(handle, data->cpu_entry);
    4548                 :            : 
    4549         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_PERIOD)
    4550                 :          0 :                 perf_output_put(handle, data->period);
    4551                 :            : 
    4552         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_READ)
    4553                 :          0 :                 perf_output_read(handle, event);
    4554                 :            : 
    4555         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
    4556         [ #  # ]:          0 :                 if (data->callchain) {
    4557                 :            :                         int size = 1;
    4558                 :            : 
    4559         [ #  # ]:          0 :                         if (data->callchain)
    4560                 :          0 :                                 size += data->callchain->nr;
    4561                 :            : 
    4562                 :          0 :                         size *= sizeof(u64);
    4563                 :            : 
    4564                 :            :                         __output_copy(handle, data->callchain, size);
    4565                 :            :                 } else {
    4566                 :          0 :                         u64 nr = 0;
    4567                 :          0 :                         perf_output_put(handle, nr);
    4568                 :            :                 }
    4569                 :            :         }
    4570                 :            : 
    4571         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_RAW) {
    4572         [ #  # ]:          0 :                 if (data->raw) {
    4573                 :          0 :                         perf_output_put(handle, data->raw->size);
    4574                 :          0 :                         __output_copy(handle, data->raw->data,
    4575                 :          0 :                                            data->raw->size);
    4576                 :            :                 } else {
    4577                 :            :                         struct {
    4578                 :            :                                 u32     size;
    4579                 :            :                                 u32     data;
    4580                 :          0 :                         } raw = {
    4581                 :            :                                 .size = sizeof(u32),
    4582                 :            :                                 .data = 0,
    4583                 :            :                         };
    4584                 :          0 :                         perf_output_put(handle, raw);
    4585                 :            :                 }
    4586                 :            :         }
    4587                 :            : 
    4588         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
    4589         [ #  # ]:          0 :                 if (data->br_stack) {
    4590                 :            :                         size_t size;
    4591                 :            : 
    4592                 :          0 :                         size = data->br_stack->nr
    4593                 :            :                              * sizeof(struct perf_branch_entry);
    4594                 :            : 
    4595                 :          0 :                         perf_output_put(handle, data->br_stack->nr);
    4596                 :          0 :                         perf_output_copy(handle, data->br_stack->entries, size);
    4597                 :            :                 } else {
    4598                 :            :                         /*
    4599                 :            :                          * we always store at least the value of nr
    4600                 :            :                          */
    4601                 :          0 :                         u64 nr = 0;
    4602                 :          0 :                         perf_output_put(handle, nr);
    4603                 :            :                 }
    4604                 :            :         }
    4605                 :            : 
    4606         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_REGS_USER) {
    4607                 :          0 :                 u64 abi = data->regs_user.abi;
    4608                 :            : 
    4609                 :            :                 /*
    4610                 :            :                  * If there are no regs to dump, notice it through
    4611                 :            :                  * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
    4612                 :            :                  */
    4613                 :          0 :                 perf_output_put(handle, abi);
    4614                 :            : 
    4615         [ #  # ]:          0 :                 if (abi) {
    4616                 :          0 :                         u64 mask = event->attr.sample_regs_user;
    4617                 :          0 :                         perf_output_sample_regs(handle,
    4618                 :            :                                                 data->regs_user.regs,
    4619                 :            :                                                 mask);
    4620                 :            :                 }
    4621                 :            :         }
    4622                 :            : 
    4623         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STACK_USER) {
    4624                 :          0 :                 perf_output_sample_ustack(handle,
    4625                 :            :                                           data->stack_user_size,
    4626                 :            :                                           data->regs_user.regs);
    4627                 :            :         }
    4628                 :            : 
    4629         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_WEIGHT)
    4630                 :          0 :                 perf_output_put(handle, data->weight);
    4631                 :            : 
    4632         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_DATA_SRC)
    4633                 :          0 :                 perf_output_put(handle, data->data_src.val);
    4634                 :            : 
    4635         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_TRANSACTION)
    4636                 :          0 :                 perf_output_put(handle, data->txn);
    4637                 :            : 
    4638         [ #  # ]:          0 :         if (!event->attr.watermark) {
    4639                 :          0 :                 int wakeup_events = event->attr.wakeup_events;
    4640                 :            : 
    4641         [ #  # ]:          0 :                 if (wakeup_events) {
    4642                 :          0 :                         struct ring_buffer *rb = handle->rb;
    4643                 :          0 :                         int events = local_inc_return(&rb->events);
    4644                 :            : 
    4645         [ #  # ]:          0 :                         if (events >= wakeup_events) {
    4646                 :            :                                 local_sub(wakeup_events, &rb->events);
    4647                 :          0 :                                 local_inc(&rb->wakeup);
    4648                 :            :                         }
    4649                 :            :                 }
    4650                 :            :         }
    4651                 :          0 : }
    4652                 :            : 
    4653                 :          0 : void perf_prepare_sample(struct perf_event_header *header,
    4654                 :            :                          struct perf_sample_data *data,
    4655                 :            :                          struct perf_event *event,
    4656                 :            :                          struct pt_regs *regs)
    4657                 :            : {
    4658                 :          0 :         u64 sample_type = event->attr.sample_type;
    4659                 :            : 
    4660                 :          0 :         header->type = PERF_RECORD_SAMPLE;
    4661                 :          0 :         header->size = sizeof(*header) + event->header_size;
    4662                 :            : 
    4663                 :          0 :         header->misc = 0;
    4664                 :          0 :         header->misc |= perf_misc_flags(regs);
    4665                 :            : 
    4666                 :          0 :         __perf_event_header__init_id(header, data, event);
    4667                 :            : 
    4668         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_IP)
    4669                 :          0 :                 data->ip = perf_instruction_pointer(regs);
    4670                 :            : 
    4671         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
    4672                 :            :                 int size = 1;
    4673                 :            : 
    4674                 :          0 :                 data->callchain = perf_callchain(event, regs);
    4675                 :            : 
    4676         [ #  # ]:          0 :                 if (data->callchain)
    4677                 :          0 :                         size += data->callchain->nr;
    4678                 :            : 
    4679                 :          0 :                 header->size += size * sizeof(u64);
    4680                 :            :         }
    4681                 :            : 
    4682         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_RAW) {
    4683                 :            :                 int size = sizeof(u32);
    4684                 :            : 
    4685         [ #  # ]:          0 :                 if (data->raw)
    4686                 :          0 :                         size += data->raw->size;
    4687                 :            :                 else
    4688                 :            :                         size += sizeof(u32);
    4689                 :            : 
    4690 [ #  # ][ #  # ]:          0 :                 WARN_ON_ONCE(size & (sizeof(u64)-1));
                 [ #  # ]
    4691                 :          0 :                 header->size += size;
    4692                 :            :         }
    4693                 :            : 
    4694         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
    4695                 :            :                 int size = sizeof(u64); /* nr */
    4696         [ #  # ]:          0 :                 if (data->br_stack) {
    4697                 :          0 :                         size += data->br_stack->nr
    4698                 :            :                               * sizeof(struct perf_branch_entry);
    4699                 :            :                 }
    4700                 :          0 :                 header->size += size;
    4701                 :            :         }
    4702                 :            : 
    4703         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_REGS_USER) {
    4704                 :            :                 /* regs dump ABI info */
    4705                 :            :                 int size = sizeof(u64);
    4706                 :            : 
    4707                 :          0 :                 perf_sample_regs_user(&data->regs_user, regs);
    4708                 :            : 
    4709         [ #  # ]:          0 :                 if (data->regs_user.regs) {
    4710                 :          0 :                         u64 mask = event->attr.sample_regs_user;
    4711         [ #  # ]:          0 :                         size += hweight64(mask) * sizeof(u64);
    4712                 :            :                 }
    4713                 :            : 
    4714                 :          0 :                 header->size += size;
    4715                 :            :         }
    4716                 :            : 
    4717         [ #  # ]:          0 :         if (sample_type & PERF_SAMPLE_STACK_USER) {
    4718                 :            :                 /*
    4719                 :            :                  * Either we need PERF_SAMPLE_STACK_USER bit to be allways
    4720                 :            :                  * processed as the last one or have additional check added
    4721                 :            :                  * in case new sample type is added, because we could eat
    4722                 :            :                  * up the rest of the sample size.
    4723                 :            :                  */
    4724                 :          0 :                 struct perf_regs_user *uregs = &data->regs_user;
    4725                 :          0 :                 u16 stack_size = event->attr.sample_stack_user;
    4726                 :            :                 u16 size = sizeof(u64);
    4727                 :            : 
    4728         [ #  # ]:          0 :                 if (!uregs->abi)
    4729                 :          0 :                         perf_sample_regs_user(uregs, regs);
    4730                 :            : 
    4731                 :          0 :                 stack_size = perf_sample_ustack_size(stack_size, header->size,
    4732                 :            :                                                      uregs->regs);
    4733                 :            : 
    4734                 :            :                 /*
    4735                 :            :                  * If there is something to dump, add space for the dump
    4736                 :            :                  * itself and for the field that tells the dynamic size,
    4737                 :            :                  * which is how many have been actually dumped.
    4738                 :            :                  */
    4739         [ #  # ]:          0 :                 if (stack_size)
    4740                 :          0 :                         size += sizeof(u64) + stack_size;
    4741                 :            : 
    4742                 :          0 :                 data->stack_user_size = stack_size;
    4743                 :          0 :                 header->size += size;
    4744                 :            :         }
    4745                 :          0 : }
    4746                 :            : 
    4747                 :          0 : static void perf_event_output(struct perf_event *event,
    4748                 :            :                                 struct perf_sample_data *data,
    4749                 :            :                                 struct pt_regs *regs)
    4750                 :            : {
    4751                 :            :         struct perf_output_handle handle;
    4752                 :            :         struct perf_event_header header;
    4753                 :            : 
    4754                 :            :         /* protect the callchain buffers */
    4755                 :            :         rcu_read_lock();
    4756                 :            : 
    4757                 :          0 :         perf_prepare_sample(&header, data, event, regs);
    4758                 :            : 
    4759         [ #  # ]:          0 :         if (perf_output_begin(&handle, event, header.size))
    4760                 :            :                 goto exit;
    4761                 :            : 
    4762                 :          0 :         perf_output_sample(&handle, &header, data, event);
    4763                 :            : 
    4764                 :          0 :         perf_output_end(&handle);
    4765                 :            : 
    4766                 :            : exit:
    4767                 :            :         rcu_read_unlock();
    4768                 :          0 : }
    4769                 :            : 
    4770                 :            : /*
    4771                 :            :  * read event_id
    4772                 :            :  */
    4773                 :            : 
    4774                 :            : struct perf_read_event {
    4775                 :            :         struct perf_event_header        header;
    4776                 :            : 
    4777                 :            :         u32                             pid;
    4778                 :            :         u32                             tid;
    4779                 :            : };
    4780                 :            : 
    4781                 :            : static void
    4782                 :          0 : perf_event_read_event(struct perf_event *event,
    4783                 :            :                         struct task_struct *task)
    4784                 :            : {
    4785                 :            :         struct perf_output_handle handle;
    4786                 :            :         struct perf_sample_data sample;
    4787                 :          0 :         struct perf_read_event read_event = {
    4788                 :            :                 .header = {
    4789                 :            :                         .type = PERF_RECORD_READ,
    4790                 :            :                         .misc = 0,
    4791                 :          0 :                         .size = sizeof(read_event) + event->read_size,
    4792                 :            :                 },
    4793                 :            :                 .pid = perf_event_pid(event, task),
    4794                 :            :                 .tid = perf_event_tid(event, task),
    4795                 :            :         };
    4796                 :            :         int ret;
    4797                 :            : 
    4798                 :            :         perf_event_header__init_id(&read_event.header, &sample, event);
    4799                 :          0 :         ret = perf_output_begin(&handle, event, read_event.header.size);
    4800         [ #  # ]:          0 :         if (ret)
    4801                 :          0 :                 return;
    4802                 :            : 
    4803                 :          0 :         perf_output_put(&handle, read_event);
    4804                 :          0 :         perf_output_read(&handle, event);
    4805                 :            :         perf_event__output_id_sample(event, &handle, &sample);
    4806                 :            : 
    4807                 :          0 :         perf_output_end(&handle);
    4808                 :            : }
    4809                 :            : 
    4810                 :            : typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
    4811                 :            : 
    4812                 :            : static void
    4813                 :          0 : perf_event_aux_ctx(struct perf_event_context *ctx,
    4814                 :            :                    perf_event_aux_output_cb output,
    4815                 :            :                    void *data)
    4816                 :            : {
    4817                 :          0 :         struct perf_event *event;
    4818                 :            : 
    4819         [ #  # ]:          0 :         list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
    4820         [ #  # ]:          0 :                 if (event->state < PERF_EVENT_STATE_INACTIVE)
    4821                 :          0 :                         continue;
    4822         [ #  # ]:          0 :                 if (!event_filter_match(event))
    4823                 :          0 :                         continue;
    4824                 :          0 :                 output(event, data);
    4825                 :            :         }
    4826                 :          0 : }
    4827                 :            : 
    4828                 :            : static void
    4829                 :          0 : perf_event_aux(perf_event_aux_output_cb output, void *data,
    4830                 :            :                struct perf_event_context *task_ctx)
    4831                 :            : {
    4832                 :            :         struct perf_cpu_context *cpuctx;
    4833                 :            :         struct perf_event_context *ctx;
    4834                 :            :         struct pmu *pmu;
    4835                 :            :         int ctxn;
    4836                 :            : 
    4837                 :            :         rcu_read_lock();
    4838         [ #  # ]:          0 :         list_for_each_entry_rcu(pmu, &pmus, entry) {
    4839                 :          0 :                 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
    4840         [ #  # ]:          0 :                 if (cpuctx->unique_pmu != pmu)
    4841                 :            :                         goto next;
    4842                 :          0 :                 perf_event_aux_ctx(&cpuctx->ctx, output, data);
    4843         [ #  # ]:          0 :                 if (task_ctx)
    4844                 :            :                         goto next;
    4845                 :          0 :                 ctxn = pmu->task_ctx_nr;
    4846         [ #  # ]:          0 :                 if (ctxn < 0)
    4847                 :            :                         goto next;
    4848                 :          0 :                 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
    4849         [ #  # ]:          0 :                 if (ctx)
    4850                 :          0 :                         perf_event_aux_ctx(ctx, output, data);
    4851                 :            : next:
    4852                 :          0 :                 put_cpu_ptr(pmu->pmu_cpu_context);
    4853                 :            :         }
    4854                 :            : 
    4855         [ #  # ]:          0 :         if (task_ctx) {
    4856                 :          0 :                 preempt_disable();
    4857                 :          0 :                 perf_event_aux_ctx(task_ctx, output, data);
    4858                 :          0 :                 preempt_enable();
    4859                 :            :         }
    4860                 :            :         rcu_read_unlock();
    4861                 :          0 : }
    4862                 :            : 
    4863                 :            : /*
    4864                 :            :  * task tracking -- fork/exit
    4865                 :            :  *
    4866                 :            :  * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
    4867                 :            :  */
    4868                 :            : 
    4869                 :            : struct perf_task_event {
    4870                 :            :         struct task_struct              *task;
    4871                 :            :         struct perf_event_context       *task_ctx;
    4872                 :            : 
    4873                 :            :         struct {
    4874                 :            :                 struct perf_event_header        header;
    4875                 :            : 
    4876                 :            :                 u32                             pid;
    4877                 :            :                 u32                             ppid;
    4878                 :            :                 u32                             tid;
    4879                 :            :                 u32                             ptid;
    4880                 :            :                 u64                             time;
    4881                 :            :         } event_id;
    4882                 :            : };
    4883                 :            : 
    4884                 :            : static int perf_event_task_match(struct perf_event *event)
    4885                 :            : {
    4886                 :            :         return event->attr.comm  || event->attr.mmap ||
    4887                 :          0 :                event->attr.mmap2 || event->attr.mmap_data ||
    4888                 :            :                event->attr.task;
    4889                 :            : }
    4890                 :            : 
    4891                 :          0 : static void perf_event_task_output(struct perf_event *event,
    4892                 :            :                                    void *data)
    4893                 :            : {
    4894                 :            :         struct perf_task_event *task_event = data;
    4895                 :            :         struct perf_output_handle handle;
    4896                 :            :         struct perf_sample_data sample;
    4897                 :          0 :         struct task_struct *task = task_event->task;
    4898                 :          0 :         int ret, size = task_event->event_id.header.size;
    4899                 :            : 
    4900         [ #  # ]:          0 :         if (!perf_event_task_match(event))
    4901                 :          0 :                 return;
    4902                 :            : 
    4903                 :            :         perf_event_header__init_id(&task_event->event_id.header, &sample, event);
    4904                 :            : 
    4905                 :          0 :         ret = perf_output_begin(&handle, event,
    4906                 :          0 :                                 task_event->event_id.header.size);
    4907         [ #  # ]:          0 :         if (ret)
    4908                 :            :                 goto out;
    4909                 :            : 
    4910                 :          0 :         task_event->event_id.pid = perf_event_pid(event, task);
    4911                 :          0 :         task_event->event_id.ppid = perf_event_pid(event, current);
    4912                 :            : 
    4913                 :          0 :         task_event->event_id.tid = perf_event_tid(event, task);
    4914                 :          0 :         task_event->event_id.ptid = perf_event_tid(event, current);
    4915                 :            : 
    4916                 :          0 :         perf_output_put(&handle, task_event->event_id);
    4917                 :            : 
    4918                 :            :         perf_event__output_id_sample(event, &handle, &sample);
    4919                 :            : 
    4920                 :          0 :         perf_output_end(&handle);
    4921                 :            : out:
    4922                 :          0 :         task_event->event_id.header.size = size;
    4923                 :            : }
    4924                 :            : 
    4925                 :          0 : static void perf_event_task(struct task_struct *task,
    4926                 :            :                               struct perf_event_context *task_ctx,
    4927                 :            :                               int new)
    4928                 :            : {
    4929                 :            :         struct perf_task_event task_event;
    4930                 :            : 
    4931    [ + ][ +  + ]:    3312667 :         if (!atomic_read(&nr_comm_events) &&
    4932            [ + ]:    3312663 :             !atomic_read(&nr_mmap_events) &&
    4933                 :    3312663 :             !atomic_read(&nr_task_events))
    4934                 :    3312677 :                 return;
    4935                 :            : 
    4936            [ - ]:          0 :         task_event = (struct perf_task_event){
    4937                 :            :                 .task     = task,
    4938                 :            :                 .task_ctx = task_ctx,
    4939                 :            :                 .event_id    = {
    4940                 :            :                         .header = {
    4941                 :            :                                 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
    4942                 :            :                                 .misc = 0,
    4943                 :            :                                 .size = sizeof(task_event.event_id),
    4944                 :            :                         },
    4945                 :            :                         /* .pid  */
    4946                 :            :                         /* .ppid */
    4947                 :            :                         /* .tid  */
    4948                 :            :                         /* .ptid */
    4949                 :            :                         .time = perf_clock(),
    4950                 :            :                 },
    4951                 :            :         };
    4952                 :            : 
    4953                 :          0 :         perf_event_aux(perf_event_task_output,
    4954                 :            :                        &task_event,
    4955                 :            :                        task_ctx);
    4956                 :            : }
    4957                 :            : 
    4958                 :          0 : void perf_event_fork(struct task_struct *task)
    4959                 :            : {
    4960                 :    1104223 :         perf_event_task(task, NULL, 1);
    4961                 :    1104223 : }
    4962                 :            : 
    4963                 :            : /*
    4964                 :            :  * comm tracking
    4965                 :            :  */
    4966                 :            : 
    4967                 :            : struct perf_comm_event {
    4968                 :            :         struct task_struct      *task;
    4969                 :            :         char                    *comm;
    4970                 :            :         int                     comm_size;
    4971                 :            : 
    4972                 :            :         struct {
    4973                 :            :                 struct perf_event_header        header;
    4974                 :            : 
    4975                 :            :                 u32                             pid;
    4976                 :            :                 u32                             tid;
    4977                 :            :         } event_id;
    4978                 :            : };
    4979                 :            : 
    4980                 :            : static int perf_event_comm_match(struct perf_event *event)
    4981                 :            : {
    4982                 :          0 :         return event->attr.comm;
    4983                 :            : }
    4984                 :            : 
    4985                 :          0 : static void perf_event_comm_output(struct perf_event *event,
    4986                 :            :                                    void *data)
    4987                 :            : {
    4988                 :            :         struct perf_comm_event *comm_event = data;
    4989                 :            :         struct perf_output_handle handle;
    4990                 :            :         struct perf_sample_data sample;
    4991                 :          0 :         int size = comm_event->event_id.header.size;
    4992                 :            :         int ret;
    4993                 :            : 
    4994         [ #  # ]:          0 :         if (!perf_event_comm_match(event))
    4995                 :          0 :                 return;
    4996                 :            : 
    4997                 :            :         perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
    4998                 :          0 :         ret = perf_output_begin(&handle, event,
    4999                 :          0 :                                 comm_event->event_id.header.size);
    5000                 :            : 
    5001         [ #  # ]:          0 :         if (ret)
    5002                 :            :                 goto out;
    5003                 :            : 
    5004                 :          0 :         comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
    5005                 :          0 :         comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
    5006                 :            : 
    5007                 :          0 :         perf_output_put(&handle, comm_event->event_id);
    5008                 :          0 :         __output_copy(&handle, comm_event->comm,
    5009                 :          0 :                                    comm_event->comm_size);
    5010                 :            : 
    5011                 :            :         perf_event__output_id_sample(event, &handle, &sample);
    5012                 :            : 
    5013                 :          0 :         perf_output_end(&handle);
    5014                 :            : out:
    5015                 :          0 :         comm_event->event_id.header.size = size;
    5016                 :            : }
    5017                 :            : 
    5018                 :          0 : static void perf_event_comm_event(struct perf_comm_event *comm_event)
    5019                 :            : {
    5020                 :            :         char comm[TASK_COMM_LEN];
    5021                 :            :         unsigned int size;
    5022                 :            : 
    5023                 :          0 :         memset(comm, 0, sizeof(comm));
    5024                 :          0 :         strlcpy(comm, comm_event->task->comm, sizeof(comm));
    5025                 :          0 :         size = ALIGN(strlen(comm)+1, sizeof(u64));
    5026                 :            : 
    5027                 :          0 :         comm_event->comm = comm;
    5028                 :          0 :         comm_event->comm_size = size;
    5029                 :            : 
    5030                 :          0 :         comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
    5031                 :            : 
    5032                 :          0 :         perf_event_aux(perf_event_comm_output,
    5033                 :            :                        comm_event,
    5034                 :            :                        NULL);
    5035                 :          0 : }
    5036                 :            : 
    5037                 :          0 : void perf_event_comm(struct task_struct *task)
    5038                 :            : {
    5039                 :            :         struct perf_comm_event comm_event;
    5040                 :            :         struct perf_event_context *ctx;
    5041                 :            :         int ctxn;
    5042                 :            : 
    5043                 :            :         rcu_read_lock();
    5044         [ +  + ]:      81815 :         for_each_task_context_nr(ctxn) {
    5045                 :      54541 :                 ctx = task->perf_event_ctxp[ctxn];
    5046         [ +  + ]:      54541 :                 if (!ctx)
    5047                 :      54539 :                         continue;
    5048                 :            : 
    5049                 :          2 :                 perf_event_enable_on_exec(ctx);
    5050                 :            :         }
    5051                 :            :         rcu_read_unlock();
    5052                 :            : 
    5053         [ -  + ]:      54550 :         if (!atomic_read(&nr_comm_events))
    5054                 :      27277 :                 return;
    5055                 :            : 
    5056                 :          0 :         comm_event = (struct perf_comm_event){
    5057                 :            :                 .task   = task,
    5058                 :            :                 /* .comm      */
    5059                 :            :                 /* .comm_size */
    5060                 :            :                 .event_id  = {
    5061                 :            :                         .header = {
    5062                 :            :                                 .type = PERF_RECORD_COMM,
    5063                 :            :                                 .misc = 0,
    5064                 :            :                                 /* .size */
    5065                 :            :                         },
    5066                 :            :                         /* .pid */
    5067                 :            :                         /* .tid */
    5068                 :            :                 },
    5069                 :            :         };
    5070                 :            : 
    5071                 :          0 :         perf_event_comm_event(&comm_event);
    5072                 :            : }
    5073                 :            : 
    5074                 :            : /*
    5075                 :            :  * mmap tracking
    5076                 :            :  */
    5077                 :            : 
    5078                 :            : struct perf_mmap_event {
    5079                 :            :         struct vm_area_struct   *vma;
    5080                 :            : 
    5081                 :            :         const char              *file_name;
    5082                 :            :         int                     file_size;
    5083                 :            :         int                     maj, min;
    5084                 :            :         u64                     ino;
    5085                 :            :         u64                     ino_generation;
    5086                 :            : 
    5087                 :            :         struct {
    5088                 :            :                 struct perf_event_header        header;
    5089                 :            : 
    5090                 :            :                 u32                             pid;
    5091                 :            :                 u32                             tid;
    5092                 :            :                 u64                             start;
    5093                 :            :                 u64                             len;
    5094                 :            :                 u64                             pgoff;
    5095                 :            :         } event_id;
    5096                 :            : };
    5097                 :            : 
    5098                 :            : static int perf_event_mmap_match(struct perf_event *event,
    5099                 :            :                                  void *data)
    5100                 :            : {
    5101                 :            :         struct perf_mmap_event *mmap_event = data;
    5102                 :          0 :         struct vm_area_struct *vma = mmap_event->vma;
    5103                 :          0 :         int executable = vma->vm_flags & VM_EXEC;
    5104                 :            : 
    5105 [ #  # ][ #  # ]:          0 :         return (!executable && event->attr.mmap_data) ||
                 [ #  # ]
    5106         [ #  # ]:          0 :                (executable && (event->attr.mmap || event->attr.mmap2));
    5107                 :            : }
    5108                 :            : 
    5109                 :          0 : static void perf_event_mmap_output(struct perf_event *event,
    5110                 :            :                                    void *data)
    5111                 :            : {
    5112                 :            :         struct perf_mmap_event *mmap_event = data;
    5113                 :            :         struct perf_output_handle handle;
    5114                 :            :         struct perf_sample_data sample;
    5115                 :          0 :         int size = mmap_event->event_id.header.size;
    5116                 :            :         int ret;
    5117                 :            : 
    5118         [ #  # ]:          0 :         if (!perf_event_mmap_match(event, data))
    5119                 :          0 :                 return;
    5120                 :            : 
    5121         [ #  # ]:          0 :         if (event->attr.mmap2) {
    5122                 :          0 :                 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
    5123                 :          0 :                 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
    5124                 :          0 :                 mmap_event->event_id.header.size += sizeof(mmap_event->min);
    5125                 :          0 :                 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
    5126                 :          0 :                 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
    5127                 :            :         }
    5128                 :            : 
    5129                 :            :         perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
    5130                 :          0 :         ret = perf_output_begin(&handle, event,
    5131                 :          0 :                                 mmap_event->event_id.header.size);
    5132         [ #  # ]:          0 :         if (ret)
    5133                 :            :                 goto out;
    5134                 :            : 
    5135                 :          0 :         mmap_event->event_id.pid = perf_event_pid(event, current);
    5136                 :          0 :         mmap_event->event_id.tid = perf_event_tid(event, current);
    5137                 :            : 
    5138                 :          0 :         perf_output_put(&handle, mmap_event->event_id);
    5139                 :            : 
    5140         [ #  # ]:          0 :         if (event->attr.mmap2) {
    5141                 :          0 :                 perf_output_put(&handle, mmap_event->maj);
    5142                 :          0 :                 perf_output_put(&handle, mmap_event->min);
    5143                 :          0 :                 perf_output_put(&handle, mmap_event->ino);
    5144                 :          0 :                 perf_output_put(&handle, mmap_event->ino_generation);
    5145                 :            :         }
    5146                 :            : 
    5147                 :          0 :         __output_copy(&handle, mmap_event->file_name,
    5148                 :          0 :                                    mmap_event->file_size);
    5149                 :            : 
    5150                 :            :         perf_event__output_id_sample(event, &handle, &sample);
    5151                 :            : 
    5152                 :          0 :         perf_output_end(&handle);
    5153                 :            : out:
    5154                 :          0 :         mmap_event->event_id.header.size = size;
    5155                 :            : }
    5156                 :            : 
    5157                 :          0 : static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
    5158                 :            : {
    5159                 :          0 :         struct vm_area_struct *vma = mmap_event->vma;
    5160                 :          0 :         struct file *file = vma->vm_file;
    5161                 :            :         int maj = 0, min = 0;
    5162                 :            :         u64 ino = 0, gen = 0;
    5163                 :            :         unsigned int size;
    5164                 :            :         char tmp[16];
    5165                 :            :         char *buf = NULL;
    5166                 :            :         char *name;
    5167                 :            : 
    5168         [ #  # ]:          0 :         if (file) {
    5169                 :            :                 struct inode *inode;
    5170                 :            :                 dev_t dev;
    5171                 :            : 
    5172                 :            :                 buf = kmalloc(PATH_MAX, GFP_KERNEL);
    5173         [ #  # ]:          0 :                 if (!buf) {
    5174                 :            :                         name = "//enomem";
    5175                 :            :                         goto cpy_name;
    5176                 :            :                 }
    5177                 :            :                 /*
    5178                 :            :                  * d_path() works from the end of the rb backwards, so we
    5179                 :            :                  * need to add enough zero bytes after the string to handle
    5180                 :            :                  * the 64bit alignment we do later.
    5181                 :            :                  */
    5182                 :          0 :                 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
    5183         [ #  # ]:          0 :                 if (IS_ERR(name)) {
    5184                 :            :                         name = "//toolong";
    5185                 :            :                         goto cpy_name;
    5186                 :            :                 }
    5187                 :          0 :                 inode = file_inode(vma->vm_file);
    5188                 :          0 :                 dev = inode->i_sb->s_dev;
    5189                 :          0 :                 ino = inode->i_ino;
    5190                 :          0 :                 gen = inode->i_generation;
    5191                 :          0 :                 maj = MAJOR(dev);
    5192                 :          0 :                 min = MINOR(dev);
    5193                 :          0 :                 goto got_name;
    5194                 :            :         } else {
    5195                 :          0 :                 name = (char *)arch_vma_name(vma);
    5196         [ #  # ]:          0 :                 if (name)
    5197                 :            :                         goto cpy_name;
    5198                 :            : 
    5199 [ #  # ][ #  # ]:          0 :                 if (vma->vm_start <= vma->vm_mm->start_brk &&
    5200                 :          0 :                                 vma->vm_end >= vma->vm_mm->brk) {
    5201                 :            :                         name = "[heap]";
    5202                 :            :                         goto cpy_name;
    5203                 :            :                 }
    5204 [ #  # ][ #  # ]:          0 :                 if (vma->vm_start <= vma->vm_mm->start_stack &&
    5205                 :          0 :                                 vma->vm_end >= vma->vm_mm->start_stack) {
    5206                 :            :                         name = "[stack]";
    5207                 :            :                         goto cpy_name;
    5208                 :            :                 }
    5209                 :            : 
    5210                 :            :                 name = "//anon";
    5211                 :          0 :                 goto cpy_name;
    5212                 :            :         }
    5213                 :            : 
    5214                 :            : cpy_name:
    5215                 :          0 :         strlcpy(tmp, name, sizeof(tmp));
    5216                 :            :         name = tmp;
    5217                 :            : got_name:
    5218                 :            :         /*
    5219                 :            :          * Since our buffer works in 8 byte units we need to align our string
    5220                 :            :          * size to a multiple of 8. However, we must guarantee the tail end is
    5221                 :            :          * zero'd out to avoid leaking random bits to userspace.
    5222                 :            :          */
    5223                 :          0 :         size = strlen(name)+1;
    5224         [ #  # ]:          0 :         while (!IS_ALIGNED(size, sizeof(u64)))
    5225                 :          0 :                 name[size++] = '\0';
    5226                 :            : 
    5227                 :          0 :         mmap_event->file_name = name;
    5228                 :          0 :         mmap_event->file_size = size;
    5229                 :          0 :         mmap_event->maj = maj;
    5230                 :          0 :         mmap_event->min = min;
    5231                 :          0 :         mmap_event->ino = ino;
    5232                 :          0 :         mmap_event->ino_generation = gen;
    5233                 :            : 
    5234         [ #  # ]:          0 :         if (!(vma->vm_flags & VM_EXEC))
    5235                 :          0 :                 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
    5236                 :            : 
    5237                 :          0 :         mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
    5238                 :            : 
    5239                 :          0 :         perf_event_aux(perf_event_mmap_output,
    5240                 :            :                        mmap_event,
    5241                 :            :                        NULL);
    5242                 :            : 
    5243                 :          0 :         kfree(buf);
    5244                 :          0 : }
    5245                 :            : 
    5246                 :          0 : void perf_event_mmap(struct vm_area_struct *vma)
    5247                 :            : {
    5248                 :            :         struct perf_mmap_event mmap_event;
    5249                 :            : 
    5250         [ -  + ]:    4114556 :         if (!atomic_read(&nr_mmap_events))
    5251                 :    4114556 :                 return;
    5252                 :            : 
    5253                 :          0 :         mmap_event = (struct perf_mmap_event){
    5254                 :            :                 .vma    = vma,
    5255                 :            :                 /* .file_name */
    5256                 :            :                 /* .file_size */
    5257                 :            :                 .event_id  = {
    5258                 :            :                         .header = {
    5259                 :            :                                 .type = PERF_RECORD_MMAP,
    5260                 :            :                                 .misc = PERF_RECORD_MISC_USER,
    5261                 :            :                                 /* .size */
    5262                 :            :                         },
    5263                 :            :                         /* .pid */
    5264                 :            :                         /* .tid */
    5265                 :          0 :                         .start  = vma->vm_start,
    5266                 :          0 :                         .len    = vma->vm_end - vma->vm_start,
    5267                 :          0 :                         .pgoff  = (u64)vma->vm_pgoff << PAGE_SHIFT,
    5268                 :            :                 },
    5269                 :            :                 /* .maj (attr_mmap2 only) */
    5270                 :            :                 /* .min (attr_mmap2 only) */
    5271                 :            :                 /* .ino (attr_mmap2 only) */
    5272                 :            :                 /* .ino_generation (attr_mmap2 only) */
    5273                 :            :         };
    5274                 :            : 
    5275                 :          0 :         perf_event_mmap_event(&mmap_event);
    5276                 :            : }
    5277                 :            : 
    5278                 :            : /*
    5279                 :            :  * IRQ throttle logging
    5280                 :            :  */
    5281                 :            : 
    5282                 :          0 : static void perf_log_throttle(struct perf_event *event, int enable)
    5283                 :            : {
    5284                 :            :         struct perf_output_handle handle;
    5285                 :            :         struct perf_sample_data sample;
    5286                 :            :         int ret;
    5287                 :            : 
    5288                 :            :         struct {
    5289                 :            :                 struct perf_event_header        header;
    5290                 :            :                 u64                             time;
    5291                 :            :                 u64                             id;
    5292                 :            :                 u64                             stream_id;
    5293                 :          0 :         } throttle_event = {
    5294                 :            :                 .header = {
    5295                 :            :                         .type = PERF_RECORD_THROTTLE,
    5296                 :            :                         .misc = 0,
    5297                 :            :                         .size = sizeof(throttle_event),
    5298                 :            :                 },
    5299                 :            :                 .time           = perf_clock(),
    5300                 :            :                 .id             = primary_event_id(event),
    5301                 :            :                 .stream_id      = event->id,
    5302                 :            :         };
    5303                 :            : 
    5304         [ #  # ]:          0 :         if (enable)
    5305                 :          0 :                 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
    5306                 :            : 
    5307                 :            :         perf_event_header__init_id(&throttle_event.header, &sample, event);
    5308                 :            : 
    5309                 :          0 :         ret = perf_output_begin(&handle, event,
    5310                 :          0 :                                 throttle_event.header.size);
    5311         [ #  # ]:          0 :         if (ret)
    5312                 :          0 :                 return;
    5313                 :            : 
    5314                 :          0 :         perf_output_put(&handle, throttle_event);
    5315                 :            :         perf_event__output_id_sample(event, &handle, &sample);
    5316                 :          0 :         perf_output_end(&handle);
    5317                 :            : }
    5318                 :            : 
    5319                 :            : /*
    5320                 :            :  * Generic event overflow handling, sampling.
    5321                 :            :  */
    5322                 :            : 
    5323                 :          0 : static int __perf_event_overflow(struct perf_event *event,
    5324                 :            :                                    int throttle, struct perf_sample_data *data,
    5325                 :            :                                    struct pt_regs *regs)
    5326                 :            : {
    5327                 :          0 :         int events = atomic_read(&event->event_limit);
    5328                 :            :         struct hw_perf_event *hwc = &event->hw;
    5329                 :            :         u64 seq;
    5330                 :            :         int ret = 0;
    5331                 :            : 
    5332                 :            :         /*
    5333                 :            :          * Non-sampling counters might still use the PMI to fold short
    5334                 :            :          * hardware counters, ignore those.
    5335                 :            :          */
    5336         [ #  # ]:          0 :         if (unlikely(!is_sampling_event(event)))
    5337                 :            :                 return 0;
    5338                 :            : 
    5339                 :          0 :         seq = __this_cpu_read(perf_throttled_seq);
    5340         [ #  # ]:          0 :         if (seq != hwc->interrupts_seq) {
    5341                 :          0 :                 hwc->interrupts_seq = seq;
    5342                 :          0 :                 hwc->interrupts = 1;
    5343                 :            :         } else {
    5344                 :          0 :                 hwc->interrupts++;
    5345 [ #  # ][ #  # ]:          0 :                 if (unlikely(throttle
    5346                 :            :                              && hwc->interrupts >= max_samples_per_tick)) {
    5347                 :          0 :                         __this_cpu_inc(perf_throttled_count);
    5348                 :          0 :                         hwc->interrupts = MAX_INTERRUPTS;
    5349                 :          0 :                         perf_log_throttle(event, 0);
    5350                 :            :                         tick_nohz_full_kick();
    5351                 :            :                         ret = 1;
    5352                 :            :                 }
    5353                 :            :         }
    5354                 :            : 
    5355         [ #  # ]:          0 :         if (event->attr.freq) {
    5356                 :            :                 u64 now = perf_clock();
    5357                 :          0 :                 s64 delta = now - hwc->freq_time_stamp;
    5358                 :            : 
    5359                 :          0 :                 hwc->freq_time_stamp = now;
    5360                 :            : 
    5361         [ #  # ]:          0 :                 if (delta > 0 && delta < 2*TICK_NSEC)
    5362                 :          0 :                         perf_adjust_period(event, delta, hwc->last_period, true);
    5363                 :            :         }
    5364                 :            : 
    5365                 :            :         /*
    5366                 :            :          * XXX event_limit might not quite work as expected on inherited
    5367                 :            :          * events
    5368                 :            :          */
    5369                 :            : 
    5370                 :          0 :         event->pending_kill = POLL_IN;
    5371   [ #  #  #  # ]:          0 :         if (events && atomic_dec_and_test(&event->event_limit)) {
    5372                 :            :                 ret = 1;
    5373                 :          0 :                 event->pending_kill = POLL_HUP;
    5374                 :          0 :                 event->pending_disable = 1;
    5375                 :          0 :                 irq_work_queue(&event->pending);
    5376                 :            :         }
    5377                 :            : 
    5378         [ #  # ]:          0 :         if (event->overflow_handler)
    5379                 :          0 :                 event->overflow_handler(event, data, regs);
    5380                 :            :         else
    5381                 :          0 :                 perf_event_output(event, data, regs);
    5382                 :            : 
    5383 [ #  # ][ #  # ]:          0 :         if (event->fasync && event->pending_kill) {
    5384                 :          0 :                 event->pending_wakeup = 1;
    5385                 :          0 :                 irq_work_queue(&event->pending);
    5386                 :            :         }
    5387                 :            : 
    5388                 :          0 :         return ret;
    5389                 :            : }
    5390                 :            : 
    5391                 :          0 : int perf_event_overflow(struct perf_event *event,
    5392                 :            :                           struct perf_sample_data *data,
    5393                 :            :                           struct pt_regs *regs)
    5394                 :            : {
    5395                 :          0 :         return __perf_event_overflow(event, 1, data, regs);
    5396                 :            : }
    5397                 :            : 
    5398                 :            : /*
    5399                 :            :  * Generic software event infrastructure
    5400                 :            :  */
    5401                 :            : 
    5402                 :            : struct swevent_htable {
    5403                 :            :         struct swevent_hlist            *swevent_hlist;
    5404                 :            :         struct mutex                    hlist_mutex;
    5405                 :            :         int                             hlist_refcount;
    5406                 :            : 
    5407                 :            :         /* Recursion avoidance in each contexts */
    5408                 :            :         int                             recursion[PERF_NR_CONTEXTS];
    5409                 :            : };
    5410                 :            : 
    5411                 :            : static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
    5412                 :            : 
    5413                 :            : /*
    5414                 :            :  * We directly increment event->count and keep a second value in
    5415                 :            :  * event->hw.period_left to count intervals. This period event
    5416                 :            :  * is kept in the range [-sample_period, 0] so that we can use the
    5417                 :            :  * sign as trigger.
    5418                 :            :  */
    5419                 :            : 
    5420                 :          0 : u64 perf_swevent_set_period(struct perf_event *event)
    5421                 :            : {
    5422                 :            :         struct hw_perf_event *hwc = &event->hw;
    5423                 :          0 :         u64 period = hwc->last_period;
    5424                 :            :         u64 nr, offset;
    5425                 :            :         s64 old, val;
    5426                 :            : 
    5427                 :          0 :         hwc->last_period = hwc->sample_period;
    5428                 :            : 
    5429                 :            : again:
    5430                 :          0 :         old = val = local64_read(&hwc->period_left);
    5431         [ #  # ]:          0 :         if (val < 0)
    5432                 :            :                 return 0;
    5433                 :            : 
    5434                 :          0 :         nr = div64_u64(period + val, period);
    5435                 :          0 :         offset = nr * period;
    5436                 :          0 :         val -= offset;
    5437         [ #  # ]:          0 :         if (local64_cmpxchg(&hwc->period_left, old, val) != old)
    5438                 :            :                 goto again;
    5439                 :            : 
    5440                 :            :         return nr;
    5441                 :            : }
    5442                 :            : 
    5443                 :          0 : static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
    5444                 :            :                                     struct perf_sample_data *data,
    5445                 :            :                                     struct pt_regs *regs)
    5446                 :            : {
    5447                 :            :         struct hw_perf_event *hwc = &event->hw;
    5448                 :            :         int throttle = 0;
    5449                 :            : 
    5450         [ #  # ]:          0 :         if (!overflow)
    5451                 :          0 :                 overflow = perf_swevent_set_period(event);
    5452                 :            : 
    5453         [ #  # ]:          0 :         if (hwc->interrupts == MAX_INTERRUPTS)
    5454                 :          0 :                 return;
    5455                 :            : 
    5456         [ #  # ]:          0 :         for (; overflow; overflow--) {
    5457         [ #  # ]:          0 :                 if (__perf_event_overflow(event, throttle,
    5458                 :            :                                             data, regs)) {
    5459                 :            :                         /*
    5460                 :            :                          * We inhibit the overflow from happening when
    5461                 :            :                          * hwc->interrupts == MAX_INTERRUPTS.
    5462                 :            :                          */
    5463                 :            :                         break;
    5464                 :            :                 }
    5465                 :            :                 throttle = 1;
    5466                 :            :         }
    5467                 :            : }
    5468                 :            : 
    5469                 :          0 : static void perf_swevent_event(struct perf_event *event, u64 nr,
    5470                 :            :                                struct perf_sample_data *data,
    5471                 :            :                                struct pt_regs *regs)
    5472                 :            : {
    5473                 :            :         struct hw_perf_event *hwc = &event->hw;
    5474                 :            : 
    5475                 :          0 :         local64_add(nr, &event->count);
    5476                 :            : 
    5477         [ #  # ]:          0 :         if (!regs)
    5478                 :            :                 return;
    5479                 :            : 
    5480         [ #  # ]:          0 :         if (!is_sampling_event(event))
    5481                 :            :                 return;
    5482                 :            : 
    5483 [ #  # ][ #  # ]:          0 :         if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
    5484                 :          0 :                 data->period = nr;
    5485                 :          0 :                 return perf_swevent_overflow(event, 1, data, regs);
    5486                 :            :         } else
    5487                 :          0 :                 data->period = event->hw.last_period;
    5488                 :            : 
    5489 [ #  # ][ #  # ]:          0 :         if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
                 [ #  # ]
    5490                 :          0 :                 return perf_swevent_overflow(event, 1, data, regs);
    5491                 :            : 
    5492         [ #  # ]:          0 :         if (local64_add_negative(nr, &hwc->period_left))
    5493                 :            :                 return;
    5494                 :            : 
    5495                 :          0 :         perf_swevent_overflow(event, 0, data, regs);
    5496                 :            : }
    5497                 :            : 
    5498                 :          0 : static int perf_exclude_event(struct perf_event *event,
    5499                 :            :                               struct pt_regs *regs)
    5500                 :            : {
    5501         [ #  # ]:          0 :         if (event->hw.state & PERF_HES_STOPPED)
    5502                 :            :                 return 1;
    5503                 :            : 
    5504         [ #  # ]:          0 :         if (regs) {
    5505 [ #  # ][ #  # ]:          0 :                 if (event->attr.exclude_user && user_mode(regs))
    5506                 :            :                         return 1;
    5507                 :            : 
    5508 [ #  # ][ #  # ]:          0 :                 if (event->attr.exclude_kernel && !user_mode(regs))
    5509                 :            :                         return 1;
    5510                 :            :         }
    5511                 :            : 
    5512                 :          0 :         return 0;
    5513                 :            : }
    5514                 :            : 
    5515                 :          0 : static int perf_swevent_match(struct perf_event *event,
    5516                 :            :                                 enum perf_type_id type,
    5517                 :            :                                 u32 event_id,
    5518                 :            :                                 struct perf_sample_data *data,
    5519                 :            :                                 struct pt_regs *regs)
    5520                 :            : {
    5521         [ #  # ]:          0 :         if (event->attr.type != type)
    5522                 :            :                 return 0;
    5523                 :            : 
    5524         [ #  # ]:          0 :         if (event->attr.config != event_id)
    5525                 :            :                 return 0;
    5526                 :            : 
    5527         [ #  # ]:          0 :         if (perf_exclude_event(event, regs))
    5528                 :            :                 return 0;
    5529                 :            : 
    5530                 :            :         return 1;
    5531                 :            : }
    5532                 :            : 
    5533                 :            : static inline u64 swevent_hash(u64 type, u32 event_id)
    5534                 :            : {
    5535                 :          0 :         u64 val = event_id | (type << 32);
    5536                 :            : 
    5537                 :            :         return hash_64(val, SWEVENT_HLIST_BITS);
    5538                 :            : }
    5539                 :            : 
    5540                 :            : static inline struct hlist_head *
    5541                 :            : __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
    5542                 :            : {
    5543                 :            :         u64 hash = swevent_hash(type, event_id);
    5544                 :            : 
    5545                 :            :         return &hlist->heads[hash];
    5546                 :            : }
    5547                 :            : 
    5548                 :            : /* For the read side: events when they trigger */
    5549                 :            : static inline struct hlist_head *
    5550                 :            : find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
    5551                 :            : {
    5552                 :            :         struct swevent_hlist *hlist;
    5553                 :            : 
    5554                 :            :         hlist = rcu_dereference(swhash->swevent_hlist);
    5555         [ #  # ]:          0 :         if (!hlist)
    5556                 :            :                 return NULL;
    5557                 :            : 
    5558                 :          0 :         return __find_swevent_head(hlist, type, event_id);
    5559                 :            : }
    5560                 :            : 
    5561                 :            : /* For the event head insertion and removal in the hlist */
    5562                 :            : static inline struct hlist_head *
    5563                 :            : find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
    5564                 :            : {
    5565                 :            :         struct swevent_hlist *hlist;
    5566                 :          0 :         u32 event_id = event->attr.config;
    5567                 :          0 :         u64 type = event->attr.type;
    5568                 :            : 
    5569                 :            :         /*
    5570                 :            :          * Event scheduling is always serialized against hlist allocation
    5571                 :            :          * and release. Which makes the protected version suitable here.
    5572                 :            :          * The context lock guarantees that.
    5573                 :            :          */
    5574                 :            :         hlist = rcu_dereference_protected(swhash->swevent_hlist,
    5575                 :            :                                           lockdep_is_held(&event->ctx->lock));
    5576         [ #  # ]:          0 :         if (!hlist)
    5577                 :            :                 return NULL;
    5578                 :            : 
    5579                 :          0 :         return __find_swevent_head(hlist, type, event_id);
    5580                 :            : }
    5581                 :            : 
    5582                 :          0 : static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
    5583                 :            :                                     u64 nr,
    5584                 :            :                                     struct perf_sample_data *data,
    5585                 :            :                                     struct pt_regs *regs)
    5586                 :            : {
    5587                 :          0 :         struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
    5588                 :            :         struct perf_event *event;
    5589                 :            :         struct hlist_head *head;
    5590                 :            : 
    5591                 :            :         rcu_read_lock();
    5592                 :          0 :         head = find_swevent_head_rcu(swhash, type, event_id);
    5593         [ #  # ]:          0 :         if (!head)
    5594                 :            :                 goto end;
    5595                 :            : 
    5596 [ #  # ][ #  # ]:          0 :         hlist_for_each_entry_rcu(event, head, hlist_entry) {
                 [ #  # ]
    5597         [ #  # ]:          0 :                 if (perf_swevent_match(event, type, event_id, data, regs))
    5598                 :          0 :                         perf_swevent_event(event, nr, data, regs);
    5599                 :            :         }
    5600                 :            : end:
    5601                 :            :         rcu_read_unlock();
    5602                 :          0 : }
    5603                 :            : 
    5604                 :          0 : int perf_swevent_get_recursion_context(void)
    5605                 :            : {
    5606                 :          0 :         struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
    5607                 :            : 
    5608                 :          0 :         return get_recursion_context(swhash->recursion);
    5609                 :            : }
    5610                 :            : EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
    5611                 :            : 
    5612                 :          0 : inline void perf_swevent_put_recursion_context(int rctx)
    5613                 :            : {
    5614                 :          0 :         struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
    5615                 :            : 
    5616                 :          0 :         put_recursion_context(swhash->recursion, rctx);
    5617                 :          0 : }
    5618                 :            : 
    5619                 :          0 : void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
    5620                 :            : {
    5621                 :            :         struct perf_sample_data data;
    5622                 :            :         int rctx;
    5623                 :            : 
    5624                 :          0 :         preempt_disable_notrace();
    5625                 :          0 :         rctx = perf_swevent_get_recursion_context();
    5626         [ #  # ]:          0 :         if (rctx < 0)
    5627                 :          0 :                 return;
    5628                 :            : 
    5629                 :            :         perf_sample_data_init(&data, addr, 0);
    5630                 :            : 
    5631                 :          0 :         do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
    5632                 :            : 
    5633                 :            :         perf_swevent_put_recursion_context(rctx);
    5634                 :          0 :         preempt_enable_notrace();
    5635                 :            : }
    5636                 :            : 
    5637                 :          0 : static void perf_swevent_read(struct perf_event *event)
    5638                 :            : {
    5639                 :          0 : }
    5640                 :            : 
    5641                 :          0 : static int perf_swevent_add(struct perf_event *event, int flags)
    5642                 :            : {
    5643                 :          0 :         struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
    5644                 :            :         struct hw_perf_event *hwc = &event->hw;
    5645                 :            :         struct hlist_head *head;
    5646                 :            : 
    5647         [ #  # ]:          0 :         if (is_sampling_event(event)) {
    5648                 :          0 :                 hwc->last_period = hwc->sample_period;
    5649                 :          0 :                 perf_swevent_set_period(event);
    5650                 :            :         }
    5651                 :            : 
    5652                 :          0 :         hwc->state = !(flags & PERF_EF_START);
    5653                 :            : 
    5654                 :            :         head = find_swevent_head(swhash, event);
    5655 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(!head))
         [ #  # ][ #  # ]
    5656                 :            :                 return -EINVAL;
    5657                 :            : 
    5658                 :          0 :         hlist_add_head_rcu(&event->hlist_entry, head);
    5659                 :            : 
    5660                 :            :         return 0;
    5661                 :            : }
    5662                 :            : 
    5663                 :          0 : static void perf_swevent_del(struct perf_event *event, int flags)
    5664                 :            : {
    5665                 :            :         hlist_del_rcu(&event->hlist_entry);
    5666                 :          0 : }
    5667                 :            : 
    5668                 :          0 : static void perf_swevent_start(struct perf_event *event, int flags)
    5669                 :            : {
    5670                 :          0 :         event->hw.state = 0;
    5671                 :          0 : }
    5672                 :            : 
    5673                 :          0 : static void perf_swevent_stop(struct perf_event *event, int flags)
    5674                 :            : {
    5675                 :          0 :         event->hw.state = PERF_HES_STOPPED;
    5676                 :          0 : }
    5677                 :            : 
    5678                 :            : /* Deref the hlist from the update side */
    5679                 :            : static inline struct swevent_hlist *
    5680                 :            : swevent_hlist_deref(struct swevent_htable *swhash)
    5681                 :            : {
    5682                 :            :         return rcu_dereference_protected(swhash->swevent_hlist,
    5683                 :            :                                          lockdep_is_held(&swhash->hlist_mutex));
    5684                 :            : }
    5685                 :            : 
    5686                 :         78 : static void swevent_hlist_release(struct swevent_htable *swhash)
    5687                 :            : {
    5688                 :            :         struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
    5689                 :            : 
    5690   [ #  #  -  + ]:         78 :         if (!hlist)
    5691                 :            :                 return;
    5692                 :            : 
    5693                 :          0 :         rcu_assign_pointer(swhash->swevent_hlist, NULL);
    5694                 :          0 :         kfree_rcu(hlist, rcu_head);
    5695                 :            : }
    5696                 :            : 
    5697                 :          0 : static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
    5698                 :            : {
    5699                 :          0 :         struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
    5700                 :            : 
    5701                 :          0 :         mutex_lock(&swhash->hlist_mutex);
    5702                 :            : 
    5703         [ #  # ]:          0 :         if (!--swhash->hlist_refcount)
    5704                 :            :                 swevent_hlist_release(swhash);
    5705                 :            : 
    5706                 :          0 :         mutex_unlock(&swhash->hlist_mutex);
    5707                 :          0 : }
    5708                 :            : 
    5709                 :          0 : static void swevent_hlist_put(struct perf_event *event)
    5710                 :            : {
    5711                 :            :         int cpu;
    5712                 :            : 
    5713         [ #  # ]:          0 :         for_each_possible_cpu(cpu)
    5714                 :          0 :                 swevent_hlist_put_cpu(event, cpu);
    5715                 :          0 : }
    5716                 :            : 
    5717                 :          0 : static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
    5718                 :            : {
    5719                 :          0 :         struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
    5720                 :            :         int err = 0;
    5721                 :            : 
    5722                 :          0 :         mutex_lock(&swhash->hlist_mutex);
    5723                 :            : 
    5724 [ #  # ][ #  # ]:          0 :         if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
    5725                 :            :                 struct swevent_hlist *hlist;
    5726                 :            : 
    5727                 :            :                 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
    5728         [ #  # ]:          0 :                 if (!hlist) {
    5729                 :            :                         err = -ENOMEM;
    5730                 :            :                         goto exit;
    5731                 :            :                 }
    5732                 :          0 :                 rcu_assign_pointer(swhash->swevent_hlist, hlist);
    5733                 :            :         }
    5734                 :          0 :         swhash->hlist_refcount++;
    5735                 :            : exit:
    5736                 :          0 :         mutex_unlock(&swhash->hlist_mutex);
    5737                 :            : 
    5738                 :          0 :         return err;
    5739                 :            : }
    5740                 :            : 
    5741                 :          0 : static int swevent_hlist_get(struct perf_event *event)
    5742                 :            : {
    5743                 :            :         int err;
    5744                 :            :         int cpu, failed_cpu;
    5745                 :            : 
    5746                 :          0 :         get_online_cpus();
    5747         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    5748                 :          0 :                 err = swevent_hlist_get_cpu(event, cpu);
    5749         [ #  # ]:          0 :                 if (err) {
    5750                 :            :                         failed_cpu = cpu;
    5751                 :            :                         goto fail;
    5752                 :            :                 }
    5753                 :            :         }
    5754                 :          0 :         put_online_cpus();
    5755                 :            : 
    5756                 :            :         return 0;
    5757                 :            : fail:
    5758         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    5759         [ #  # ]:          0 :                 if (cpu == failed_cpu)
    5760                 :            :                         break;
    5761                 :          0 :                 swevent_hlist_put_cpu(event, cpu);
    5762                 :            :         }
    5763                 :            : 
    5764                 :          0 :         put_online_cpus();
    5765                 :            :         return err;
    5766                 :            : }
    5767                 :            : 
    5768                 :            : struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
    5769                 :            : 
    5770                 :          0 : static void sw_perf_event_destroy(struct perf_event *event)
    5771                 :            : {
    5772                 :          0 :         u64 event_id = event->attr.config;
    5773                 :            : 
    5774         [ #  # ]:          0 :         WARN_ON(event->parent);
    5775                 :            : 
    5776                 :          0 :         static_key_slow_dec(&perf_swevent_enabled[event_id]);
    5777                 :          0 :         swevent_hlist_put(event);
    5778                 :          0 : }
    5779                 :            : 
    5780                 :          0 : static int perf_swevent_init(struct perf_event *event)
    5781                 :            : {
    5782                 :          0 :         u64 event_id = event->attr.config;
    5783                 :            : 
    5784         [ #  # ]:          0 :         if (event->attr.type != PERF_TYPE_SOFTWARE)
    5785                 :            :                 return -ENOENT;
    5786                 :            : 
    5787                 :            :         /*
    5788                 :            :          * no branch sampling for software events
    5789                 :            :          */
    5790         [ #  # ]:          0 :         if (has_branch_stack(event))
    5791                 :            :                 return -EOPNOTSUPP;
    5792                 :            : 
    5793         [ #  # ]:          0 :         switch (event_id) {
    5794                 :            :         case PERF_COUNT_SW_CPU_CLOCK:
    5795                 :            :         case PERF_COUNT_SW_TASK_CLOCK:
    5796                 :            :                 return -ENOENT;
    5797                 :            : 
    5798                 :            :         default:
    5799                 :            :                 break;
    5800                 :            :         }
    5801                 :            : 
    5802         [ #  # ]:          0 :         if (event_id >= PERF_COUNT_SW_MAX)
    5803                 :            :                 return -ENOENT;
    5804                 :            : 
    5805         [ #  # ]:          0 :         if (!event->parent) {
    5806                 :            :                 int err;
    5807                 :            : 
    5808                 :          0 :                 err = swevent_hlist_get(event);
    5809         [ #  # ]:          0 :                 if (err)
    5810                 :            :                         return err;
    5811                 :            : 
    5812                 :          0 :                 static_key_slow_inc(&perf_swevent_enabled[event_id]);
    5813                 :          0 :                 event->destroy = sw_perf_event_destroy;
    5814                 :            :         }
    5815                 :            : 
    5816                 :            :         return 0;
    5817                 :            : }
    5818                 :            : 
    5819                 :          0 : static int perf_swevent_event_idx(struct perf_event *event)
    5820                 :            : {
    5821                 :          0 :         return 0;
    5822                 :            : }
    5823                 :            : 
    5824                 :            : static struct pmu perf_swevent = {
    5825                 :            :         .task_ctx_nr    = perf_sw_context,
    5826                 :            : 
    5827                 :            :         .event_init     = perf_swevent_init,
    5828                 :            :         .add            = perf_swevent_add,
    5829                 :            :         .del            = perf_swevent_del,
    5830                 :            :         .start          = perf_swevent_start,
    5831                 :            :         .stop           = perf_swevent_stop,
    5832                 :            :         .read           = perf_swevent_read,
    5833                 :            : 
    5834                 :            :         .event_idx      = perf_swevent_event_idx,
    5835                 :            : };
    5836                 :            : 
    5837                 :            : #ifdef CONFIG_EVENT_TRACING
    5838                 :            : 
    5839                 :          0 : static int perf_tp_filter_match(struct perf_event *event,
    5840                 :            :                                 struct perf_sample_data *data)
    5841                 :            : {
    5842                 :          0 :         void *record = data->raw->data;
    5843                 :            : 
    5844 [ #  # ][ #  # ]:          0 :         if (likely(!event->filter) || filter_match_preds(event->filter, record))
    5845                 :            :                 return 1;
    5846                 :            :         return 0;
    5847                 :            : }
    5848                 :            : 
    5849                 :          0 : static int perf_tp_event_match(struct perf_event *event,
    5850                 :          0 :                                 struct perf_sample_data *data,
    5851                 :            :                                 struct pt_regs *regs)
    5852                 :            : {
    5853         [ #  # ]:          0 :         if (event->hw.state & PERF_HES_STOPPED)
    5854                 :            :                 return 0;
    5855                 :            :         /*
    5856                 :            :          * All tracepoints are from kernel-space.
    5857                 :            :          */
    5858         [ #  # ]:          0 :         if (event->attr.exclude_kernel)
    5859                 :            :                 return 0;
    5860                 :            : 
    5861         [ #  # ]:          0 :         if (!perf_tp_filter_match(event, data))
    5862                 :            :                 return 0;
    5863                 :            : 
    5864                 :            :         return 1;
    5865                 :            : }
    5866                 :            : 
    5867                 :          0 : void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
    5868                 :            :                    struct pt_regs *regs, struct hlist_head *head, int rctx,
    5869                 :            :                    struct task_struct *task)
    5870                 :            : {
    5871                 :            :         struct perf_sample_data data;
    5872                 :            :         struct perf_event *event;
    5873                 :            : 
    5874                 :          0 :         struct perf_raw_record raw = {
    5875                 :            :                 .size = entry_size,
    5876                 :            :                 .data = record,
    5877                 :            :         };
    5878                 :            : 
    5879                 :            :         perf_sample_data_init(&data, addr, 0);
    5880                 :          0 :         data.raw = &raw;
    5881                 :            : 
    5882 [ #  # ][ #  # ]:          0 :         hlist_for_each_entry_rcu(event, head, hlist_entry) {
                 [ #  # ]
    5883         [ #  # ]:          0 :                 if (perf_tp_event_match(event, &data, regs))
    5884                 :          0 :                         perf_swevent_event(event, count, &data, regs);
    5885                 :            :         }
    5886                 :            : 
    5887                 :            :         /*
    5888                 :            :          * If we got specified a target task, also iterate its context and
    5889                 :            :          * deliver this event there too.
    5890                 :            :          */
    5891 [ #  # ][ #  # ]:          0 :         if (task && task != current) {
    5892                 :            :                 struct perf_event_context *ctx;
    5893                 :            :                 struct trace_entry *entry = record;
    5894                 :            : 
    5895                 :            :                 rcu_read_lock();
    5896                 :          0 :                 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
    5897         [ #  # ]:          0 :                 if (!ctx)
    5898                 :            :                         goto unlock;
    5899                 :            : 
    5900         [ #  # ]:          0 :                 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
    5901         [ #  # ]:          0 :                         if (event->attr.type != PERF_TYPE_TRACEPOINT)
    5902                 :          0 :                                 continue;
    5903         [ #  # ]:          0 :                         if (event->attr.config != entry->type)
    5904                 :          0 :                                 continue;
    5905         [ #  # ]:          0 :                         if (perf_tp_event_match(event, &data, regs))
    5906                 :          0 :                                 perf_swevent_event(event, count, &data, regs);
    5907                 :            :                 }
    5908                 :            : unlock:
    5909                 :            :                 rcu_read_unlock();
    5910                 :            :         }
    5911                 :            : 
    5912                 :            :         perf_swevent_put_recursion_context(rctx);
    5913                 :          0 : }
    5914                 :            : EXPORT_SYMBOL_GPL(perf_tp_event);
    5915                 :            : 
    5916                 :          0 : static void tp_perf_event_destroy(struct perf_event *event)
    5917                 :            : {
    5918                 :          0 :         perf_trace_destroy(event);
    5919                 :          0 : }
    5920                 :            : 
    5921                 :          0 : static int perf_tp_event_init(struct perf_event *event)
    5922                 :            : {
    5923                 :            :         int err;
    5924                 :            : 
    5925         [ #  # ]:          0 :         if (event->attr.type != PERF_TYPE_TRACEPOINT)
    5926                 :            :                 return -ENOENT;
    5927                 :            : 
    5928                 :            :         /*
    5929                 :            :          * no branch sampling for tracepoint events
    5930                 :            :          */
    5931         [ #  # ]:          0 :         if (has_branch_stack(event))
    5932                 :            :                 return -EOPNOTSUPP;
    5933                 :            : 
    5934                 :          0 :         err = perf_trace_init(event);
    5935         [ #  # ]:          0 :         if (err)
    5936                 :            :                 return err;
    5937                 :            : 
    5938                 :          0 :         event->destroy = tp_perf_event_destroy;
    5939                 :            : 
    5940                 :          0 :         return 0;
    5941                 :            : }
    5942                 :            : 
    5943                 :            : static struct pmu perf_tracepoint = {
    5944                 :            :         .task_ctx_nr    = perf_sw_context,
    5945                 :            : 
    5946                 :            :         .event_init     = perf_tp_event_init,
    5947                 :            :         .add            = perf_trace_add,
    5948                 :            :         .del            = perf_trace_del,
    5949                 :            :         .start          = perf_swevent_start,
    5950                 :            :         .stop           = perf_swevent_stop,
    5951                 :            :         .read           = perf_swevent_read,
    5952                 :            : 
    5953                 :            :         .event_idx      = perf_swevent_event_idx,
    5954                 :            : };
    5955                 :            : 
    5956                 :            : static inline void perf_tp_register(void)
    5957                 :            : {
    5958                 :          0 :         perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
    5959                 :            : }
    5960                 :            : 
    5961                 :          0 : static int perf_event_set_filter(struct perf_event *event, void __user *arg)
    5962                 :            : {
    5963                 :            :         char *filter_str;
    5964                 :            :         int ret;
    5965                 :            : 
    5966         [ #  # ]:          0 :         if (event->attr.type != PERF_TYPE_TRACEPOINT)
    5967                 :            :                 return -EINVAL;
    5968                 :            : 
    5969                 :          0 :         filter_str = strndup_user(arg, PAGE_SIZE);
    5970         [ #  # ]:          0 :         if (IS_ERR(filter_str))
    5971                 :          0 :                 return PTR_ERR(filter_str);
    5972                 :            : 
    5973                 :          0 :         ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
    5974                 :            : 
    5975                 :          0 :         kfree(filter_str);
    5976                 :          0 :         return ret;
    5977                 :            : }
    5978                 :            : 
    5979                 :            : static void perf_event_free_filter(struct perf_event *event)
    5980                 :            : {
    5981                 :          0 :         ftrace_profile_free_filter(event);
    5982                 :            : }
    5983                 :            : 
    5984                 :            : #else
    5985                 :            : 
    5986                 :            : static inline void perf_tp_register(void)
    5987                 :            : {
    5988                 :            : }
    5989                 :            : 
    5990                 :            : static int perf_event_set_filter(struct perf_event *event, void __user *arg)
    5991                 :            : {
    5992                 :            :         return -ENOENT;
    5993                 :            : }
    5994                 :            : 
    5995                 :            : static void perf_event_free_filter(struct perf_event *event)
    5996                 :            : {
    5997                 :            : }
    5998                 :            : 
    5999                 :            : #endif /* CONFIG_EVENT_TRACING */
    6000                 :            : 
    6001                 :            : #ifdef CONFIG_HAVE_HW_BREAKPOINT
    6002                 :          0 : void perf_bp_event(struct perf_event *bp, void *data)
    6003                 :            : {
    6004                 :            :         struct perf_sample_data sample;
    6005                 :            :         struct pt_regs *regs = data;
    6006                 :            : 
    6007                 :          0 :         perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
    6008                 :            : 
    6009 [ #  # ][ #  # ]:          0 :         if (!bp->hw.state && !perf_exclude_event(bp, regs))
    6010                 :          0 :                 perf_swevent_event(bp, 1, &sample, regs);
    6011                 :          0 : }
    6012                 :            : #endif
    6013                 :            : 
    6014                 :            : /*
    6015                 :            :  * hrtimer based swevent callback
    6016                 :            :  */
    6017                 :            : 
    6018                 :          0 : static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
    6019                 :            : {
    6020                 :            :         enum hrtimer_restart ret = HRTIMER_RESTART;
    6021                 :            :         struct perf_sample_data data;
    6022                 :            :         struct pt_regs *regs;
    6023                 :            :         struct perf_event *event;
    6024                 :            :         u64 period;
    6025                 :            : 
    6026                 :          0 :         event = container_of(hrtimer, struct perf_event, hw.hrtimer);
    6027                 :            : 
    6028         [ #  # ]:          0 :         if (event->state != PERF_EVENT_STATE_ACTIVE)
    6029                 :            :                 return HRTIMER_NORESTART;
    6030                 :            : 
    6031                 :          0 :         event->pmu->read(event);
    6032                 :            : 
    6033                 :          0 :         perf_sample_data_init(&data, 0, event->hw.last_period);
    6034                 :            :         regs = get_irq_regs();
    6035                 :            : 
    6036 [ #  # ][ #  # ]:          0 :         if (regs && !perf_exclude_event(event, regs)) {
    6037 [ #  # ][ #  # ]:          0 :                 if (!(event->attr.exclude_idle && is_idle_task(current)))
    6038         [ #  # ]:          0 :                         if (__perf_event_overflow(event, 1, &data, regs))
    6039                 :            :                                 ret = HRTIMER_NORESTART;
    6040                 :            :         }
    6041                 :            : 
    6042                 :          0 :         period = max_t(u64, 10000, event->hw.sample_period);
    6043                 :            :         hrtimer_forward_now(hrtimer, ns_to_ktime(period));
    6044                 :            : 
    6045                 :          0 :         return ret;
    6046                 :            : }
    6047                 :            : 
    6048                 :          0 : static void perf_swevent_start_hrtimer(struct perf_event *event)
    6049                 :            : {
    6050                 :            :         struct hw_perf_event *hwc = &event->hw;
    6051                 :            :         s64 period;
    6052                 :            : 
    6053         [ #  # ]:          0 :         if (!is_sampling_event(event))
    6054                 :          0 :                 return;
    6055                 :            : 
    6056                 :          0 :         period = local64_read(&hwc->period_left);
    6057         [ #  # ]:          0 :         if (period) {
    6058         [ #  # ]:          0 :                 if (period < 0)
    6059                 :            :                         period = 10000;
    6060                 :            : 
    6061                 :            :                 local64_set(&hwc->period_left, 0);
    6062                 :            :         } else {
    6063                 :          0 :                 period = max_t(u64, 10000, hwc->sample_period);
    6064                 :            :         }
    6065                 :          0 :         __hrtimer_start_range_ns(&hwc->hrtimer,
    6066                 :            :                                 ns_to_ktime(period), 0,
    6067                 :            :                                 HRTIMER_MODE_REL_PINNED, 0);
    6068                 :            : }
    6069                 :            : 
    6070                 :          0 : static void perf_swevent_cancel_hrtimer(struct perf_event *event)
    6071                 :            : {
    6072                 :            :         struct hw_perf_event *hwc = &event->hw;
    6073                 :            : 
    6074         [ #  # ]:          0 :         if (is_sampling_event(event)) {
    6075                 :          0 :                 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
    6076                 :          0 :                 local64_set(&hwc->period_left, ktime_to_ns(remaining));
    6077                 :            : 
    6078                 :          0 :                 hrtimer_cancel(&hwc->hrtimer);
    6079                 :            :         }
    6080                 :          0 : }
    6081                 :            : 
    6082                 :          0 : static void perf_swevent_init_hrtimer(struct perf_event *event)
    6083                 :            : {
    6084                 :            :         struct hw_perf_event *hwc = &event->hw;
    6085                 :            : 
    6086         [ #  # ]:          0 :         if (!is_sampling_event(event))
    6087                 :          0 :                 return;
    6088                 :            : 
    6089                 :          0 :         hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    6090                 :          0 :         hwc->hrtimer.function = perf_swevent_hrtimer;
    6091                 :            : 
    6092                 :            :         /*
    6093                 :            :          * Since hrtimers have a fixed rate, we can do a static freq->period
    6094                 :            :          * mapping and avoid the whole period adjust feedback stuff.
    6095                 :            :          */
    6096         [ #  # ]:          0 :         if (event->attr.freq) {
    6097                 :          0 :                 long freq = event->attr.sample_freq;
    6098                 :            : 
    6099                 :          0 :                 event->attr.sample_period = NSEC_PER_SEC / freq;
    6100                 :          0 :                 hwc->sample_period = event->attr.sample_period;
    6101                 :          0 :                 local64_set(&hwc->period_left, hwc->sample_period);
    6102                 :          0 :                 hwc->last_period = hwc->sample_period;
    6103                 :          0 :                 event->attr.freq = 0;
    6104                 :            :         }
    6105                 :            : }
    6106                 :            : 
    6107                 :            : /*
    6108                 :            :  * Software event: cpu wall time clock
    6109                 :            :  */
    6110                 :            : 
    6111                 :          0 : static void cpu_clock_event_update(struct perf_event *event)
    6112                 :            : {
    6113                 :            :         s64 prev;
    6114                 :            :         u64 now;
    6115                 :            : 
    6116                 :          0 :         now = local_clock();
    6117                 :          0 :         prev = local64_xchg(&event->hw.prev_count, now);
    6118                 :          0 :         local64_add(now - prev, &event->count);
    6119                 :          0 : }
    6120                 :            : 
    6121                 :          0 : static void cpu_clock_event_start(struct perf_event *event, int flags)
    6122                 :            : {
    6123                 :          0 :         local64_set(&event->hw.prev_count, local_clock());
    6124                 :          0 :         perf_swevent_start_hrtimer(event);
    6125                 :          0 : }
    6126                 :            : 
    6127                 :          0 : static void cpu_clock_event_stop(struct perf_event *event, int flags)
    6128                 :            : {
    6129                 :          0 :         perf_swevent_cancel_hrtimer(event);
    6130                 :          0 :         cpu_clock_event_update(event);
    6131                 :          0 : }
    6132                 :            : 
    6133                 :          0 : static int cpu_clock_event_add(struct perf_event *event, int flags)
    6134                 :            : {
    6135         [ #  # ]:          0 :         if (flags & PERF_EF_START)
    6136                 :          0 :                 cpu_clock_event_start(event, flags);
    6137                 :            : 
    6138                 :          0 :         return 0;
    6139                 :            : }
    6140                 :            : 
    6141                 :          0 : static void cpu_clock_event_del(struct perf_event *event, int flags)
    6142                 :            : {
    6143                 :            :         cpu_clock_event_stop(event, flags);
    6144                 :          0 : }
    6145                 :            : 
    6146                 :          0 : static void cpu_clock_event_read(struct perf_event *event)
    6147                 :            : {
    6148                 :          0 :         cpu_clock_event_update(event);
    6149                 :          0 : }
    6150                 :            : 
    6151                 :          0 : static int cpu_clock_event_init(struct perf_event *event)
    6152                 :            : {
    6153         [ #  # ]:          0 :         if (event->attr.type != PERF_TYPE_SOFTWARE)
    6154                 :            :                 return -ENOENT;
    6155                 :            : 
    6156         [ #  # ]:          0 :         if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
    6157                 :            :                 return -ENOENT;
    6158                 :            : 
    6159                 :            :         /*
    6160                 :            :          * no branch sampling for software events
    6161                 :            :          */
    6162         [ #  # ]:          0 :         if (has_branch_stack(event))
    6163                 :            :                 return -EOPNOTSUPP;
    6164                 :            : 
    6165                 :          0 :         perf_swevent_init_hrtimer(event);
    6166                 :            : 
    6167                 :          0 :         return 0;
    6168                 :            : }
    6169                 :            : 
    6170                 :            : static struct pmu perf_cpu_clock = {
    6171                 :            :         .task_ctx_nr    = perf_sw_context,
    6172                 :            : 
    6173                 :            :         .event_init     = cpu_clock_event_init,
    6174                 :            :         .add            = cpu_clock_event_add,
    6175                 :            :         .del            = cpu_clock_event_del,
    6176                 :            :         .start          = cpu_clock_event_start,
    6177                 :            :         .stop           = cpu_clock_event_stop,
    6178                 :            :         .read           = cpu_clock_event_read,
    6179                 :            : 
    6180                 :            :         .event_idx      = perf_swevent_event_idx,
    6181                 :            : };
    6182                 :            : 
    6183                 :            : /*
    6184                 :            :  * Software event: task time clock
    6185                 :            :  */
    6186                 :            : 
    6187                 :          0 : static void task_clock_event_update(struct perf_event *event, u64 now)
    6188                 :            : {
    6189                 :            :         u64 prev;
    6190                 :            :         s64 delta;
    6191                 :            : 
    6192                 :          0 :         prev = local64_xchg(&event->hw.prev_count, now);
    6193                 :          0 :         delta = now - prev;
    6194                 :          0 :         local64_add(delta, &event->count);
    6195                 :          0 : }
    6196                 :            : 
    6197                 :          0 : static void task_clock_event_start(struct perf_event *event, int flags)
    6198                 :            : {
    6199                 :          0 :         local64_set(&event->hw.prev_count, event->ctx->time);
    6200                 :          0 :         perf_swevent_start_hrtimer(event);
    6201                 :          0 : }
    6202                 :            : 
    6203                 :          0 : static void task_clock_event_stop(struct perf_event *event, int flags)
    6204                 :            : {
    6205                 :          0 :         perf_swevent_cancel_hrtimer(event);
    6206                 :          0 :         task_clock_event_update(event, event->ctx->time);
    6207                 :          0 : }
    6208                 :            : 
    6209                 :          0 : static int task_clock_event_add(struct perf_event *event, int flags)
    6210                 :            : {
    6211         [ #  # ]:          0 :         if (flags & PERF_EF_START)
    6212                 :          0 :                 task_clock_event_start(event, flags);
    6213                 :            : 
    6214                 :          0 :         return 0;
    6215                 :            : }
    6216                 :            : 
    6217                 :          0 : static void task_clock_event_del(struct perf_event *event, int flags)
    6218                 :            : {
    6219                 :          0 :         task_clock_event_stop(event, PERF_EF_UPDATE);
    6220                 :          0 : }
    6221                 :            : 
    6222                 :          0 : static void task_clock_event_read(struct perf_event *event)
    6223                 :            : {
    6224                 :            :         u64 now = perf_clock();
    6225                 :          0 :         u64 delta = now - event->ctx->timestamp;
    6226                 :          0 :         u64 time = event->ctx->time + delta;
    6227                 :            : 
    6228                 :          0 :         task_clock_event_update(event, time);
    6229                 :          0 : }
    6230                 :            : 
    6231                 :          0 : static int task_clock_event_init(struct perf_event *event)
    6232                 :            : {
    6233         [ #  # ]:          0 :         if (event->attr.type != PERF_TYPE_SOFTWARE)
    6234                 :            :                 return -ENOENT;
    6235                 :            : 
    6236         [ #  # ]:          0 :         if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
    6237                 :            :                 return -ENOENT;
    6238                 :            : 
    6239                 :            :         /*
    6240                 :            :          * no branch sampling for software events
    6241                 :            :          */
    6242         [ #  # ]:          0 :         if (has_branch_stack(event))
    6243                 :            :                 return -EOPNOTSUPP;
    6244                 :            : 
    6245                 :          0 :         perf_swevent_init_hrtimer(event);
    6246                 :            : 
    6247                 :          0 :         return 0;
    6248                 :            : }
    6249                 :            : 
    6250                 :            : static struct pmu perf_task_clock = {
    6251                 :            :         .task_ctx_nr    = perf_sw_context,
    6252                 :            : 
    6253                 :            :         .event_init     = task_clock_event_init,
    6254                 :            :         .add            = task_clock_event_add,
    6255                 :            :         .del            = task_clock_event_del,
    6256                 :            :         .start          = task_clock_event_start,
    6257                 :            :         .stop           = task_clock_event_stop,
    6258                 :            :         .read           = task_clock_event_read,
    6259                 :            : 
    6260                 :            :         .event_idx      = perf_swevent_event_idx,
    6261                 :            : };
    6262                 :            : 
    6263                 :          0 : static void perf_pmu_nop_void(struct pmu *pmu)
    6264                 :            : {
    6265                 :          0 : }
    6266                 :            : 
    6267                 :          0 : static int perf_pmu_nop_int(struct pmu *pmu)
    6268                 :            : {
    6269                 :          0 :         return 0;
    6270                 :            : }
    6271                 :            : 
    6272                 :          0 : static void perf_pmu_start_txn(struct pmu *pmu)
    6273                 :            : {
    6274                 :            :         perf_pmu_disable(pmu);
    6275                 :          0 : }
    6276                 :            : 
    6277                 :          0 : static int perf_pmu_commit_txn(struct pmu *pmu)
    6278                 :            : {
    6279                 :            :         perf_pmu_enable(pmu);
    6280                 :          0 :         return 0;
    6281                 :            : }
    6282                 :            : 
    6283                 :          0 : static void perf_pmu_cancel_txn(struct pmu *pmu)
    6284                 :            : {
    6285                 :            :         perf_pmu_enable(pmu);
    6286                 :          0 : }
    6287                 :            : 
    6288                 :          0 : static int perf_event_idx_default(struct perf_event *event)
    6289                 :            : {
    6290                 :          0 :         return event->hw.idx + 1;
    6291                 :            : }
    6292                 :            : 
    6293                 :            : /*
    6294                 :            :  * Ensures all contexts with the same task_ctx_nr have the same
    6295                 :            :  * pmu_cpu_context too.
    6296                 :            :  */
    6297                 :            : static void *find_pmu_context(int ctxn)
    6298                 :            : {
    6299                 :            :         struct pmu *pmu;
    6300                 :            : 
    6301         [ #  # ]:          0 :         if (ctxn < 0)
    6302                 :            :                 return NULL;
    6303                 :            : 
    6304         [ #  # ]:          0 :         list_for_each_entry(pmu, &pmus, entry) {
    6305         [ #  # ]:          0 :                 if (pmu->task_ctx_nr == ctxn)
    6306                 :          0 :                         return pmu->pmu_cpu_context;
    6307                 :            :         }
    6308                 :            : 
    6309                 :            :         return NULL;
    6310                 :            : }
    6311                 :            : 
    6312                 :          0 : static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
    6313                 :            : {
    6314                 :            :         int cpu;
    6315                 :            : 
    6316         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    6317                 :            :                 struct perf_cpu_context *cpuctx;
    6318                 :            : 
    6319                 :          0 :                 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
    6320                 :            : 
    6321         [ #  # ]:          0 :                 if (cpuctx->unique_pmu == old_pmu)
    6322                 :          0 :                         cpuctx->unique_pmu = pmu;
    6323                 :            :         }
    6324                 :          0 : }
    6325                 :            : 
    6326                 :          0 : static void free_pmu_context(struct pmu *pmu)
    6327                 :            : {
    6328                 :            :         struct pmu *i;
    6329                 :            : 
    6330                 :          0 :         mutex_lock(&pmus_lock);
    6331                 :            :         /*
    6332                 :            :          * Like a real lame refcount.
    6333                 :            :          */
    6334         [ #  # ]:          0 :         list_for_each_entry(i, &pmus, entry) {
    6335         [ #  # ]:          0 :                 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
    6336                 :          0 :                         update_pmu_context(i, pmu);
    6337                 :          0 :                         goto out;
    6338                 :            :                 }
    6339                 :            :         }
    6340                 :            : 
    6341                 :          0 :         free_percpu(pmu->pmu_cpu_context);
    6342                 :            : out:
    6343                 :          0 :         mutex_unlock(&pmus_lock);
    6344                 :          0 : }
    6345                 :            : static struct idr pmu_idr;
    6346                 :            : 
    6347                 :            : static ssize_t
    6348                 :          0 : type_show(struct device *dev, struct device_attribute *attr, char *page)
    6349                 :            : {
    6350                 :          0 :         struct pmu *pmu = dev_get_drvdata(dev);
    6351                 :            : 
    6352                 :          0 :         return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
    6353                 :            : }
    6354                 :            : static DEVICE_ATTR_RO(type);
    6355                 :            : 
    6356                 :            : static ssize_t
    6357                 :          0 : perf_event_mux_interval_ms_show(struct device *dev,
    6358                 :            :                                 struct device_attribute *attr,
    6359                 :            :                                 char *page)
    6360                 :            : {
    6361                 :          0 :         struct pmu *pmu = dev_get_drvdata(dev);
    6362                 :            : 
    6363                 :          0 :         return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
    6364                 :            : }
    6365                 :            : 
    6366                 :            : static ssize_t
    6367                 :          0 : perf_event_mux_interval_ms_store(struct device *dev,
    6368                 :            :                                  struct device_attribute *attr,
    6369                 :            :                                  const char *buf, size_t count)
    6370                 :            : {
    6371                 :          0 :         struct pmu *pmu = dev_get_drvdata(dev);
    6372                 :            :         int timer, cpu, ret;
    6373                 :            : 
    6374                 :          0 :         ret = kstrtoint(buf, 0, &timer);
    6375         [ #  # ]:          0 :         if (ret)
    6376                 :            :                 return ret;
    6377                 :            : 
    6378         [ #  # ]:          0 :         if (timer < 1)
    6379                 :            :                 return -EINVAL;
    6380                 :            : 
    6381                 :            :         /* same value, noting to do */
    6382         [ #  # ]:          0 :         if (timer == pmu->hrtimer_interval_ms)
    6383                 :          0 :                 return count;
    6384                 :            : 
    6385                 :          0 :         pmu->hrtimer_interval_ms = timer;
    6386                 :            : 
    6387                 :            :         /* update all cpuctx for this PMU */
    6388         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    6389                 :            :                 struct perf_cpu_context *cpuctx;
    6390                 :          0 :                 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
    6391                 :          0 :                 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
    6392                 :            : 
    6393         [ #  # ]:          0 :                 if (hrtimer_active(&cpuctx->hrtimer))
    6394                 :          0 :                         hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
    6395                 :            :         }
    6396                 :            : 
    6397                 :          0 :         return count;
    6398                 :            : }
    6399                 :            : static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
    6400                 :            : 
    6401                 :            : static struct attribute *pmu_dev_attrs[] = {
    6402                 :            :         &dev_attr_type.attr,
    6403                 :            :         &dev_attr_perf_event_mux_interval_ms.attr,
    6404                 :            :         NULL,
    6405                 :            : };
    6406                 :            : ATTRIBUTE_GROUPS(pmu_dev);
    6407                 :            : 
    6408                 :            : static int pmu_bus_running;
    6409                 :            : static struct bus_type pmu_bus = {
    6410                 :            :         .name           = "event_source",
    6411                 :            :         .dev_groups     = pmu_dev_groups,
    6412                 :            : };
    6413                 :            : 
    6414                 :          0 : static void pmu_dev_release(struct device *dev)
    6415                 :            : {
    6416                 :          0 :         kfree(dev);
    6417                 :          0 : }
    6418                 :            : 
    6419                 :          0 : static int pmu_dev_alloc(struct pmu *pmu)
    6420                 :            : {
    6421                 :            :         int ret = -ENOMEM;
    6422                 :            : 
    6423                 :          0 :         pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
    6424         [ #  # ]:          0 :         if (!pmu->dev)
    6425                 :            :                 goto out;
    6426                 :            : 
    6427                 :          0 :         pmu->dev->groups = pmu->attr_groups;
    6428                 :          0 :         device_initialize(pmu->dev);
    6429                 :          0 :         ret = dev_set_name(pmu->dev, "%s", pmu->name);
    6430         [ #  # ]:          0 :         if (ret)
    6431                 :            :                 goto free_dev;
    6432                 :            : 
    6433                 :          0 :         dev_set_drvdata(pmu->dev, pmu);
    6434                 :          0 :         pmu->dev->bus = &pmu_bus;
    6435                 :          0 :         pmu->dev->release = pmu_dev_release;
    6436                 :          0 :         ret = device_add(pmu->dev);
    6437         [ #  # ]:          0 :         if (ret)
    6438                 :            :                 goto free_dev;
    6439                 :            : 
    6440                 :            : out:
    6441                 :          0 :         return ret;
    6442                 :            : 
    6443                 :            : free_dev:
    6444                 :          0 :         put_device(pmu->dev);
    6445                 :          0 :         goto out;
    6446                 :            : }
    6447                 :            : 
    6448                 :            : static struct lock_class_key cpuctx_mutex;
    6449                 :            : static struct lock_class_key cpuctx_lock;
    6450                 :            : 
    6451                 :          0 : int perf_pmu_register(struct pmu *pmu, const char *name, int type)
    6452                 :            : {
    6453                 :            :         int cpu, ret;
    6454                 :            : 
    6455                 :          0 :         mutex_lock(&pmus_lock);
    6456                 :            :         ret = -ENOMEM;
    6457                 :          0 :         pmu->pmu_disable_count = alloc_percpu(int);
    6458         [ #  # ]:          0 :         if (!pmu->pmu_disable_count)
    6459                 :            :                 goto unlock;
    6460                 :            : 
    6461                 :          0 :         pmu->type = -1;
    6462         [ #  # ]:          0 :         if (!name)
    6463                 :            :                 goto skip_type;
    6464                 :          0 :         pmu->name = name;
    6465                 :            : 
    6466         [ #  # ]:          0 :         if (type < 0) {
    6467                 :          0 :                 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
    6468         [ #  # ]:          0 :                 if (type < 0) {
    6469                 :            :                         ret = type;
    6470                 :            :                         goto free_pdc;
    6471                 :            :                 }
    6472                 :            :         }
    6473                 :          0 :         pmu->type = type;
    6474                 :            : 
    6475         [ #  # ]:          0 :         if (pmu_bus_running) {
    6476                 :          0 :                 ret = pmu_dev_alloc(pmu);
    6477         [ #  # ]:          0 :                 if (ret)
    6478                 :            :                         goto free_idr;
    6479                 :            :         }
    6480                 :            : 
    6481                 :            : skip_type:
    6482                 :          0 :         pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
    6483         [ #  # ]:          0 :         if (pmu->pmu_cpu_context)
    6484                 :            :                 goto got_cpu_context;
    6485                 :            : 
    6486                 :            :         ret = -ENOMEM;
    6487                 :          0 :         pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
    6488         [ #  # ]:          0 :         if (!pmu->pmu_cpu_context)
    6489                 :            :                 goto free_dev;
    6490                 :            : 
    6491         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    6492                 :            :                 struct perf_cpu_context *cpuctx;
    6493                 :            : 
    6494                 :          0 :                 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
    6495                 :          0 :                 __perf_event_init_context(&cpuctx->ctx);
    6496                 :            :                 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
    6497                 :            :                 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
    6498                 :          0 :                 cpuctx->ctx.type = cpu_context;
    6499                 :          0 :                 cpuctx->ctx.pmu = pmu;
    6500                 :            : 
    6501                 :          0 :                 __perf_cpu_hrtimer_init(cpuctx, cpu);
    6502                 :            : 
    6503                 :          0 :                 INIT_LIST_HEAD(&cpuctx->rotation_list);
    6504                 :          0 :                 cpuctx->unique_pmu = pmu;
    6505                 :            :         }
    6506                 :            : 
    6507                 :            : got_cpu_context:
    6508         [ #  # ]:          0 :         if (!pmu->start_txn) {
    6509         [ #  # ]:          0 :                 if (pmu->pmu_enable) {
    6510                 :            :                         /*
    6511                 :            :                          * If we have pmu_enable/pmu_disable calls, install
    6512                 :            :                          * transaction stubs that use that to try and batch
    6513                 :            :                          * hardware accesses.
    6514                 :            :                          */
    6515                 :          0 :                         pmu->start_txn  = perf_pmu_start_txn;
    6516                 :          0 :                         pmu->commit_txn = perf_pmu_commit_txn;
    6517                 :          0 :                         pmu->cancel_txn = perf_pmu_cancel_txn;
    6518                 :            :                 } else {
    6519                 :          0 :                         pmu->start_txn  = perf_pmu_nop_void;
    6520                 :          0 :                         pmu->commit_txn = perf_pmu_nop_int;
    6521                 :          0 :                         pmu->cancel_txn = perf_pmu_nop_void;
    6522                 :            :                 }
    6523                 :            :         }
    6524                 :            : 
    6525         [ #  # ]:          0 :         if (!pmu->pmu_enable) {
    6526                 :          0 :                 pmu->pmu_enable  = perf_pmu_nop_void;
    6527                 :          0 :                 pmu->pmu_disable = perf_pmu_nop_void;
    6528                 :            :         }
    6529                 :            : 
    6530         [ #  # ]:          0 :         if (!pmu->event_idx)
    6531                 :          0 :                 pmu->event_idx = perf_event_idx_default;
    6532                 :            : 
    6533                 :          0 :         list_add_rcu(&pmu->entry, &pmus);
    6534                 :            :         ret = 0;
    6535                 :            : unlock:
    6536                 :          0 :         mutex_unlock(&pmus_lock);
    6537                 :            : 
    6538                 :          0 :         return ret;
    6539                 :            : 
    6540                 :            : free_dev:
    6541                 :          0 :         device_del(pmu->dev);
    6542                 :          0 :         put_device(pmu->dev);
    6543                 :            : 
    6544                 :            : free_idr:
    6545         [ #  # ]:          0 :         if (pmu->type >= PERF_TYPE_MAX)
    6546                 :          0 :                 idr_remove(&pmu_idr, pmu->type);
    6547                 :            : 
    6548                 :            : free_pdc:
    6549                 :          0 :         free_percpu(pmu->pmu_disable_count);
    6550                 :          0 :         goto unlock;
    6551                 :            : }
    6552                 :            : 
    6553                 :          0 : void perf_pmu_unregister(struct pmu *pmu)
    6554                 :            : {
    6555                 :          0 :         mutex_lock(&pmus_lock);
    6556                 :            :         list_del_rcu(&pmu->entry);
    6557                 :          0 :         mutex_unlock(&pmus_lock);
    6558                 :            : 
    6559                 :            :         /*
    6560                 :            :          * We dereference the pmu list under both SRCU and regular RCU, so
    6561                 :            :          * synchronize against both of those.
    6562                 :            :          */
    6563                 :          0 :         synchronize_srcu(&pmus_srcu);
    6564                 :            :         synchronize_rcu();
    6565                 :            : 
    6566                 :          0 :         free_percpu(pmu->pmu_disable_count);
    6567         [ #  # ]:          0 :         if (pmu->type >= PERF_TYPE_MAX)
    6568                 :          0 :                 idr_remove(&pmu_idr, pmu->type);
    6569                 :          0 :         device_del(pmu->dev);
    6570                 :          0 :         put_device(pmu->dev);
    6571                 :          0 :         free_pmu_context(pmu);
    6572                 :          0 : }
    6573                 :            : 
    6574                 :          0 : struct pmu *perf_init_event(struct perf_event *event)
    6575                 :            : {
    6576                 :            :         struct pmu *pmu = NULL;
    6577                 :            :         int idx;
    6578                 :            :         int ret;
    6579                 :            : 
    6580                 :            :         idx = srcu_read_lock(&pmus_srcu);
    6581                 :            : 
    6582                 :            :         rcu_read_lock();
    6583                 :          0 :         pmu = idr_find(&pmu_idr, event->attr.type);
    6584                 :            :         rcu_read_unlock();
    6585         [ #  # ]:          0 :         if (pmu) {
    6586                 :          0 :                 event->pmu = pmu;
    6587                 :          0 :                 ret = pmu->event_init(event);
    6588         [ #  # ]:          0 :                 if (ret)
    6589                 :            :                         pmu = ERR_PTR(ret);
    6590                 :            :                 goto unlock;
    6591                 :            :         }
    6592                 :            : 
    6593         [ #  # ]:          0 :         list_for_each_entry_rcu(pmu, &pmus, entry) {
    6594                 :          0 :                 event->pmu = pmu;
    6595                 :          0 :                 ret = pmu->event_init(event);
    6596         [ #  # ]:          0 :                 if (!ret)
    6597                 :            :                         goto unlock;
    6598                 :            : 
    6599         [ #  # ]:          0 :                 if (ret != -ENOENT) {
    6600                 :            :                         pmu = ERR_PTR(ret);
    6601                 :          0 :                         goto unlock;
    6602                 :            :                 }
    6603                 :            :         }
    6604                 :            :         pmu = ERR_PTR(-ENOENT);
    6605                 :            : unlock:
    6606                 :            :         srcu_read_unlock(&pmus_srcu, idx);
    6607                 :            : 
    6608                 :          0 :         return pmu;
    6609                 :            : }
    6610                 :            : 
    6611                 :          0 : static void account_event_cpu(struct perf_event *event, int cpu)
    6612                 :            : {
    6613         [ #  # ]:          0 :         if (event->parent)
    6614                 :          0 :                 return;
    6615                 :            : 
    6616         [ #  # ]:          0 :         if (has_branch_stack(event)) {
    6617         [ #  # ]:          0 :                 if (!(event->attach_state & PERF_ATTACH_TASK))
    6618                 :          0 :                         atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
    6619                 :            :         }
    6620                 :            :         if (is_cgroup_event(event))
    6621                 :            :                 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
    6622                 :            : }
    6623                 :            : 
    6624                 :          0 : static void account_event(struct perf_event *event)
    6625                 :            : {
    6626         [ #  # ]:          0 :         if (event->parent)
    6627                 :          0 :                 return;
    6628                 :            : 
    6629         [ #  # ]:          0 :         if (event->attach_state & PERF_ATTACH_TASK)
    6630                 :            :                 static_key_slow_inc(&perf_sched_events.key);
    6631         [ #  # ]:          0 :         if (event->attr.mmap || event->attr.mmap_data)
    6632                 :            :                 atomic_inc(&nr_mmap_events);
    6633         [ #  # ]:          0 :         if (event->attr.comm)
    6634                 :            :                 atomic_inc(&nr_comm_events);
    6635         [ #  # ]:          0 :         if (event->attr.task)
    6636                 :            :                 atomic_inc(&nr_task_events);
    6637         [ #  # ]:          0 :         if (event->attr.freq) {
    6638                 :            :                 if (atomic_inc_return(&nr_freq_events) == 1)
    6639                 :            :                         tick_nohz_full_kick_all();
    6640                 :            :         }
    6641         [ #  # ]:          0 :         if (has_branch_stack(event))
    6642                 :            :                 static_key_slow_inc(&perf_sched_events.key);
    6643                 :            :         if (is_cgroup_event(event))
    6644                 :            :                 static_key_slow_inc(&perf_sched_events.key);
    6645                 :            : 
    6646                 :          0 :         account_event_cpu(event, event->cpu);
    6647                 :            : }
    6648                 :            : 
    6649                 :            : /*
    6650                 :            :  * Allocate and initialize a event structure
    6651                 :            :  */
    6652                 :            : static struct perf_event *
    6653                 :          0 : perf_event_alloc(struct perf_event_attr *attr, int cpu,
    6654                 :            :                  struct task_struct *task,
    6655                 :            :                  struct perf_event *group_leader,
    6656                 :            :                  struct perf_event *parent_event,
    6657                 :            :                  perf_overflow_handler_t overflow_handler,
    6658                 :            :                  void *context)
    6659                 :            : {
    6660                 :            :         struct pmu *pmu;
    6661                 :            :         struct perf_event *event;
    6662                 :            :         struct hw_perf_event *hwc;
    6663                 :            :         long err = -EINVAL;
    6664                 :            : 
    6665         [ #  # ]:          0 :         if ((unsigned)cpu >= nr_cpu_ids) {
    6666         [ #  # ]:          0 :                 if (!task || cpu != -1)
    6667                 :            :                         return ERR_PTR(-EINVAL);
    6668                 :            :         }
    6669                 :            : 
    6670                 :            :         event = kzalloc(sizeof(*event), GFP_KERNEL);
    6671         [ #  # ]:          0 :         if (!event)
    6672                 :            :                 return ERR_PTR(-ENOMEM);
    6673                 :            : 
    6674                 :            :         /*
    6675                 :            :          * Single events are their own group leaders, with an
    6676                 :            :          * empty sibling list:
    6677                 :            :          */
    6678         [ #  # ]:          0 :         if (!group_leader)
    6679                 :            :                 group_leader = event;
    6680                 :            : 
    6681                 :          0 :         mutex_init(&event->child_mutex);
    6682                 :          0 :         INIT_LIST_HEAD(&event->child_list);
    6683                 :            : 
    6684                 :          0 :         INIT_LIST_HEAD(&event->group_entry);
    6685                 :          0 :         INIT_LIST_HEAD(&event->event_entry);
    6686                 :          0 :         INIT_LIST_HEAD(&event->sibling_list);
    6687                 :          0 :         INIT_LIST_HEAD(&event->rb_entry);
    6688                 :          0 :         INIT_LIST_HEAD(&event->active_entry);
    6689                 :            :         INIT_HLIST_NODE(&event->hlist_entry);
    6690                 :            : 
    6691                 :            : 
    6692                 :          0 :         init_waitqueue_head(&event->waitq);
    6693                 :            :         init_irq_work(&event->pending, perf_pending_event);
    6694                 :            : 
    6695                 :          0 :         mutex_init(&event->mmap_mutex);
    6696                 :            : 
    6697                 :            :         atomic_long_set(&event->refcount, 1);
    6698                 :          0 :         event->cpu           = cpu;
    6699                 :          0 :         event->attr          = *attr;
    6700                 :          0 :         event->group_leader  = group_leader;
    6701                 :          0 :         event->pmu           = NULL;
    6702                 :          0 :         event->oncpu         = -1;
    6703                 :            : 
    6704                 :          0 :         event->parent                = parent_event;
    6705                 :            : 
    6706                 :          0 :         event->ns            = get_pid_ns(task_active_pid_ns(current));
    6707                 :          0 :         event->id            = atomic64_inc_return(&perf_event_id);
    6708                 :            : 
    6709                 :          0 :         event->state         = PERF_EVENT_STATE_INACTIVE;
    6710                 :            : 
    6711         [ #  # ]:          0 :         if (task) {
    6712                 :          0 :                 event->attach_state = PERF_ATTACH_TASK;
    6713                 :            : 
    6714         [ #  # ]:          0 :                 if (attr->type == PERF_TYPE_TRACEPOINT)
    6715                 :          0 :                         event->hw.tp_target = task;
    6716                 :            : #ifdef CONFIG_HAVE_HW_BREAKPOINT
    6717                 :            :                 /*
    6718                 :            :                  * hw_breakpoint is a bit difficult here..
    6719                 :            :                  */
    6720         [ #  # ]:          0 :                 else if (attr->type == PERF_TYPE_BREAKPOINT)
    6721                 :          0 :                         event->hw.bp_target = task;
    6722                 :            : #endif
    6723                 :            :         }
    6724                 :            : 
    6725         [ #  # ]:          0 :         if (!overflow_handler && parent_event) {
    6726                 :          0 :                 overflow_handler = parent_event->overflow_handler;
    6727                 :          0 :                 context = parent_event->overflow_handler_context;
    6728                 :            :         }
    6729                 :            : 
    6730                 :          0 :         event->overflow_handler      = overflow_handler;
    6731                 :          0 :         event->overflow_handler_context = context;
    6732                 :            : 
    6733                 :            :         perf_event__state_init(event);
    6734                 :            : 
    6735                 :            :         pmu = NULL;
    6736                 :            : 
    6737                 :            :         hwc = &event->hw;
    6738                 :          0 :         hwc->sample_period = attr->sample_period;
    6739 [ #  # ][ #  # ]:          0 :         if (attr->freq && attr->sample_freq)
    6740                 :          0 :                 hwc->sample_period = 1;
    6741                 :          0 :         hwc->last_period = hwc->sample_period;
    6742                 :            : 
    6743                 :          0 :         local64_set(&hwc->period_left, hwc->sample_period);
    6744                 :            : 
    6745                 :            :         /*
    6746                 :            :          * we currently do not support PERF_FORMAT_GROUP on inherited events
    6747                 :            :          */
    6748 [ #  # ][ #  # ]:          0 :         if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
    6749                 :            :                 goto err_ns;
    6750                 :            : 
    6751                 :          0 :         pmu = perf_init_event(event);
    6752         [ #  # ]:          0 :         if (!pmu)
    6753                 :            :                 goto err_ns;
    6754         [ #  # ]:          0 :         else if (IS_ERR(pmu)) {
    6755                 :            :                 err = PTR_ERR(pmu);
    6756                 :          0 :                 goto err_ns;
    6757                 :            :         }
    6758                 :            : 
    6759         [ #  # ]:          0 :         if (!event->parent) {
    6760         [ #  # ]:          0 :                 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
    6761                 :          0 :                         err = get_callchain_buffers();
    6762         [ #  # ]:          0 :                         if (err)
    6763                 :            :                                 goto err_pmu;
    6764                 :            :                 }
    6765                 :            :         }
    6766                 :            : 
    6767                 :          0 :         return event;
    6768                 :            : 
    6769                 :            : err_pmu:
    6770         [ #  # ]:          0 :         if (event->destroy)
    6771                 :          0 :                 event->destroy(event);
    6772                 :            : err_ns:
    6773                 :            :         if (event->ns)
    6774                 :            :                 put_pid_ns(event->ns);
    6775                 :          0 :         kfree(event);
    6776                 :            : 
    6777                 :          0 :         return ERR_PTR(err);
    6778                 :            : }
    6779                 :            : 
    6780                 :          0 : static int perf_copy_attr(struct perf_event_attr __user *uattr,
    6781                 :            :                           struct perf_event_attr *attr)
    6782                 :            : {
    6783                 :            :         u32 size;
    6784                 :            :         int ret;
    6785                 :            : 
    6786         [ #  # ]:          0 :         if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
    6787                 :            :                 return -EFAULT;
    6788                 :            : 
    6789                 :            :         /*
    6790                 :            :          * zero the full structure, so that a short copy will be nice.
    6791                 :            :          */
    6792                 :          0 :         memset(attr, 0, sizeof(*attr));
    6793                 :            : 
    6794                 :          0 :         ret = get_user(size, &uattr->size);
    6795         [ #  # ]:          0 :         if (ret)
    6796                 :            :                 return ret;
    6797                 :            : 
    6798         [ #  # ]:          0 :         if (size > PAGE_SIZE)        /* silly large */
    6799                 :            :                 goto err_size;
    6800                 :            : 
    6801         [ #  # ]:          0 :         if (!size)              /* abi compat */
    6802                 :            :                 size = PERF_ATTR_SIZE_VER0;
    6803                 :            : 
    6804         [ #  # ]:          0 :         if (size < PERF_ATTR_SIZE_VER0)
    6805                 :            :                 goto err_size;
    6806                 :            : 
    6807                 :            :         /*
    6808                 :            :          * If we're handed a bigger struct than we know of,
    6809                 :            :          * ensure all the unknown bits are 0 - i.e. new
    6810                 :            :          * user-space does not rely on any kernel feature
    6811                 :            :          * extensions we dont know about yet.
    6812                 :            :          */
    6813         [ #  # ]:          0 :         if (size > sizeof(*attr)) {
    6814                 :            :                 unsigned char __user *addr;
    6815                 :            :                 unsigned char __user *end;
    6816                 :            :                 unsigned char val;
    6817                 :            : 
    6818                 :          0 :                 addr = (void __user *)uattr + sizeof(*attr);
    6819                 :          0 :                 end  = (void __user *)uattr + size;
    6820                 :            : 
    6821         [ #  # ]:          0 :                 for (; addr < end; addr++) {
    6822                 :          0 :                         ret = get_user(val, addr);
    6823         [ #  # ]:          0 :                         if (ret)
    6824                 :            :                                 return ret;
    6825         [ #  # ]:          0 :                         if (val)
    6826                 :            :                                 goto err_size;
    6827                 :            :                 }
    6828                 :            :                 size = sizeof(*attr);
    6829                 :            :         }
    6830                 :            : 
    6831                 :          0 :         ret = copy_from_user(attr, uattr, size);
    6832         [ #  # ]:          0 :         if (ret)
    6833                 :            :                 return -EFAULT;
    6834                 :            : 
    6835                 :            :         /* disabled for now */
    6836         [ #  # ]:          0 :         if (attr->mmap2)
    6837                 :            :                 return -EINVAL;
    6838                 :            : 
    6839         [ #  # ]:          0 :         if (attr->__reserved_1)
    6840                 :            :                 return -EINVAL;
    6841                 :            : 
    6842         [ #  # ]:          0 :         if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
    6843                 :            :                 return -EINVAL;
    6844                 :            : 
    6845         [ #  # ]:          0 :         if (attr->read_format & ~(PERF_FORMAT_MAX-1))
    6846                 :            :                 return -EINVAL;
    6847                 :            : 
    6848         [ #  # ]:          0 :         if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
    6849                 :          0 :                 u64 mask = attr->branch_sample_type;
    6850                 :            : 
    6851                 :            :                 /* only using defined bits */
    6852         [ #  # ]:          0 :                 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
    6853                 :            :                         return -EINVAL;
    6854                 :            : 
    6855                 :            :                 /* at least one branch bit must be set */
    6856         [ #  # ]:          0 :                 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
    6857                 :            :                         return -EINVAL;
    6858                 :            : 
    6859                 :            :                 /* propagate priv level, when not set for branch */
    6860         [ #  # ]:          0 :                 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
    6861                 :            : 
    6862                 :            :                         /* exclude_kernel checked on syscall entry */
    6863         [ #  # ]:          0 :                         if (!attr->exclude_kernel)
    6864                 :          0 :                                 mask |= PERF_SAMPLE_BRANCH_KERNEL;
    6865                 :            : 
    6866         [ #  # ]:          0 :                         if (!attr->exclude_user)
    6867                 :          0 :                                 mask |= PERF_SAMPLE_BRANCH_USER;
    6868                 :            : 
    6869         [ #  # ]:          0 :                         if (!attr->exclude_hv)
    6870                 :          0 :                                 mask |= PERF_SAMPLE_BRANCH_HV;
    6871                 :            :                         /*
    6872                 :            :                          * adjust user setting (for HW filter setup)
    6873                 :            :                          */
    6874                 :          0 :                         attr->branch_sample_type = mask;
    6875                 :            :                 }
    6876                 :            :                 /* privileged levels capture (kernel, hv): check permissions */
    6877         [ #  # ]:          0 :                 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
    6878 [ #  # ][ #  # ]:          0 :                     && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
    6879                 :            :                         return -EACCES;
    6880                 :            :         }
    6881                 :            : 
    6882         [ #  # ]:          0 :         if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
    6883                 :          0 :                 ret = perf_reg_validate(attr->sample_regs_user);
    6884         [ #  # ]:          0 :                 if (ret)
    6885                 :            :                         return ret;
    6886                 :            :         }
    6887                 :            : 
    6888         [ #  # ]:          0 :         if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
    6889                 :            :                 if (!arch_perf_have_user_stack_dump())
    6890                 :            :                         return -ENOSYS;
    6891                 :            : 
    6892                 :            :                 /*
    6893                 :            :                  * We have __u32 type for the size, but so far
    6894                 :            :                  * we can only use __u16 as maximum due to the
    6895                 :            :                  * __u16 sample size limit.
    6896                 :            :                  */
    6897         [ #  # ]:          0 :                 if (attr->sample_stack_user >= USHRT_MAX)
    6898                 :            :                         ret = -EINVAL;
    6899         [ #  # ]:          0 :                 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
    6900                 :            :                         ret = -EINVAL;
    6901                 :            :         }
    6902                 :            : 
    6903                 :            : out:
    6904                 :          0 :         return ret;
    6905                 :            : 
    6906                 :            : err_size:
    6907                 :          0 :         put_user(sizeof(*attr), &uattr->size);
    6908                 :            :         ret = -E2BIG;
    6909                 :          0 :         goto out;
    6910                 :            : }
    6911                 :            : 
    6912                 :            : static int
    6913                 :          0 : perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
    6914                 :            : {
    6915                 :            :         struct ring_buffer *rb = NULL, *old_rb = NULL;
    6916                 :            :         int ret = -EINVAL;
    6917                 :            : 
    6918         [ #  # ]:          0 :         if (!output_event)
    6919                 :            :                 goto set;
    6920                 :            : 
    6921                 :            :         /* don't allow circular references */
    6922         [ #  # ]:          0 :         if (event == output_event)
    6923                 :            :                 goto out;
    6924                 :            : 
    6925                 :            :         /*
    6926                 :            :          * Don't allow cross-cpu buffers
    6927                 :            :          */
    6928         [ #  # ]:          0 :         if (output_event->cpu != event->cpu)
    6929                 :            :                 goto out;
    6930                 :            : 
    6931                 :            :         /*
    6932                 :            :          * If its not a per-cpu rb, it must be the same task.
    6933                 :            :          */
    6934 [ #  # ][ #  # ]:          0 :         if (output_event->cpu == -1 && output_event->ctx != event->ctx)
    6935                 :            :                 goto out;
    6936                 :            : 
    6937                 :            : set:
    6938                 :          0 :         mutex_lock(&event->mmap_mutex);
    6939                 :            :         /* Can't redirect output if we've got an active mmap() */
    6940         [ #  # ]:          0 :         if (atomic_read(&event->mmap_count))
    6941                 :            :                 goto unlock;
    6942                 :            : 
    6943                 :          0 :         old_rb = event->rb;
    6944                 :            : 
    6945         [ #  # ]:          0 :         if (output_event) {
    6946                 :            :                 /* get the rb we want to redirect to */
    6947                 :          0 :                 rb = ring_buffer_get(output_event);
    6948         [ #  # ]:          0 :                 if (!rb)
    6949                 :            :                         goto unlock;
    6950                 :            :         }
    6951                 :            : 
    6952         [ #  # ]:          0 :         if (old_rb)
    6953                 :          0 :                 ring_buffer_detach(event, old_rb);
    6954                 :            : 
    6955         [ #  # ]:          0 :         if (rb)
    6956                 :          0 :                 ring_buffer_attach(event, rb);
    6957                 :            : 
    6958                 :          0 :         rcu_assign_pointer(event->rb, rb);
    6959                 :            : 
    6960         [ #  # ]:          0 :         if (old_rb) {
    6961                 :          0 :                 ring_buffer_put(old_rb);
    6962                 :            :                 /*
    6963                 :            :                  * Since we detached before setting the new rb, so that we
    6964                 :            :                  * could attach the new rb, we could have missed a wakeup.
    6965                 :            :                  * Provide it now.
    6966                 :            :                  */
    6967                 :          0 :                 wake_up_all(&event->waitq);
    6968                 :            :         }
    6969                 :            : 
    6970                 :            :         ret = 0;
    6971                 :            : unlock:
    6972                 :          0 :         mutex_unlock(&event->mmap_mutex);
    6973                 :            : 
    6974                 :            : out:
    6975                 :          0 :         return ret;
    6976                 :            : }
    6977                 :            : 
    6978                 :            : /**
    6979                 :            :  * sys_perf_event_open - open a performance event, associate it to a task/cpu
    6980                 :            :  *
    6981                 :            :  * @attr_uptr:  event_id type attributes for monitoring/sampling
    6982                 :            :  * @pid:                target pid
    6983                 :            :  * @cpu:                target cpu
    6984                 :            :  * @group_fd:           group leader event fd
    6985                 :            :  */
    6986                 :          0 : SYSCALL_DEFINE5(perf_event_open,
    6987                 :            :                 struct perf_event_attr __user *, attr_uptr,
    6988                 :            :                 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
    6989                 :            : {
    6990                 :          0 :         struct perf_event *group_leader = NULL, *output_event = NULL;
    6991                 :            :         struct perf_event *event, *sibling;
    6992                 :            :         struct perf_event_attr attr;
    6993                 :            :         struct perf_event_context *ctx;
    6994                 :            :         struct file *event_file = NULL;
    6995                 :            :         struct fd group = {NULL, 0};
    6996                 :            :         struct task_struct *task = NULL;
    6997                 :            :         struct pmu *pmu;
    6998                 :            :         int event_fd;
    6999                 :            :         int move_group = 0;
    7000                 :            :         int err;
    7001                 :            :         int f_flags = O_RDWR;
    7002                 :            : 
    7003                 :            :         /* for future expandability... */
    7004         [ #  # ]:          0 :         if (flags & ~PERF_FLAG_ALL)
    7005                 :            :                 return -EINVAL;
    7006                 :            : 
    7007                 :          0 :         err = perf_copy_attr(attr_uptr, &attr);
    7008         [ #  # ]:          0 :         if (err)
    7009                 :            :                 return err;
    7010                 :            : 
    7011         [ #  # ]:          0 :         if (!attr.exclude_kernel) {
    7012 [ #  # ][ #  # ]:          0 :                 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
    7013                 :            :                         return -EACCES;
    7014                 :            :         }
    7015                 :            : 
    7016         [ #  # ]:          0 :         if (attr.freq) {
    7017         [ #  # ]:          0 :                 if (attr.sample_freq > sysctl_perf_event_sample_rate)
    7018                 :            :                         return -EINVAL;
    7019                 :            :         }
    7020                 :            : 
    7021                 :            :         /*
    7022                 :            :          * In cgroup mode, the pid argument is used to pass the fd
    7023                 :            :          * opened to the cgroup directory in cgroupfs. The cpu argument
    7024                 :            :          * designates the cpu on which to monitor threads from that
    7025                 :            :          * cgroup.
    7026                 :            :          */
    7027 [ #  # ][ #  # ]:          0 :         if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
    7028                 :            :                 return -EINVAL;
    7029                 :            : 
    7030         [ #  # ]:          0 :         if (flags & PERF_FLAG_FD_CLOEXEC)
    7031                 :            :                 f_flags |= O_CLOEXEC;
    7032                 :            : 
    7033                 :          0 :         event_fd = get_unused_fd_flags(f_flags);
    7034         [ #  # ]:          0 :         if (event_fd < 0)
    7035                 :            :                 return event_fd;
    7036                 :            : 
    7037         [ #  # ]:          0 :         if (group_fd != -1) {
    7038                 :            :                 err = perf_fget_light(group_fd, &group);
    7039         [ #  # ]:          0 :                 if (err)
    7040                 :            :                         goto err_fd;
    7041                 :          0 :                 group_leader = group.file->private_data;
    7042         [ #  # ]:          0 :                 if (flags & PERF_FLAG_FD_OUTPUT)
    7043                 :            :                         output_event = group_leader;
    7044         [ #  # ]:          0 :                 if (flags & PERF_FLAG_FD_NO_GROUP)
    7045                 :            :                         group_leader = NULL;
    7046                 :            :         }
    7047                 :            : 
    7048 [ #  # ][ #  # ]:          0 :         if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
    7049                 :          0 :                 task = find_lively_task_by_vpid(pid);
    7050         [ #  # ]:          0 :                 if (IS_ERR(task)) {
    7051                 :            :                         err = PTR_ERR(task);
    7052                 :            :                         goto err_group_fd;
    7053                 :            :                 }
    7054                 :            :         }
    7055                 :            : 
    7056                 :          0 :         get_online_cpus();
    7057                 :            : 
    7058                 :          0 :         event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
    7059                 :            :                                  NULL, NULL);
    7060         [ #  # ]:          0 :         if (IS_ERR(event)) {
    7061                 :            :                 err = PTR_ERR(event);
    7062                 :            :                 goto err_task;
    7063                 :            :         }
    7064                 :            : 
    7065         [ #  # ]:          0 :         if (flags & PERF_FLAG_PID_CGROUP) {
    7066                 :            :                 err = perf_cgroup_connect(pid, event, &attr, group_leader);
    7067                 :            :                 if (err) {
    7068                 :          0 :                         __free_event(event);
    7069                 :            :                         goto err_task;
    7070                 :            :                 }
    7071                 :            :         }
    7072                 :            : 
    7073                 :          0 :         account_event(event);
    7074                 :            : 
    7075                 :            :         /*
    7076                 :            :          * Special case software events and allow them to be part of
    7077                 :            :          * any hardware group.
    7078                 :            :          */
    7079                 :          0 :         pmu = event->pmu;
    7080                 :            : 
    7081 [ #  # ][ #  # ]:          0 :         if (group_leader &&
    7082                 :            :             (is_software_event(event) != is_software_event(group_leader))) {
    7083         [ #  # ]:          0 :                 if (is_software_event(event)) {
    7084                 :            :                         /*
    7085                 :            :                          * If event and group_leader are not both a software
    7086                 :            :                          * event, and event is, then group leader is not.
    7087                 :            :                          *
    7088                 :            :                          * Allow the addition of software events to !software
    7089                 :            :                          * groups, this is safe because software events never
    7090                 :            :                          * fail to schedule.
    7091                 :            :                          */
    7092                 :            :                         pmu = group_leader->pmu;
    7093 [ #  # ][ #  # ]:          0 :                 } else if (is_software_event(group_leader) &&
    7094                 :          0 :                            (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
    7095                 :            :                         /*
    7096                 :            :                          * In case the group is a pure software group, and we
    7097                 :            :                          * try to add a hardware event, move the whole group to
    7098                 :            :                          * the hardware context.
    7099                 :            :                          */
    7100                 :            :                         move_group = 1;
    7101                 :            :                 }
    7102                 :            :         }
    7103                 :            : 
    7104                 :            :         /*
    7105                 :            :          * Get the target context (task or percpu):
    7106                 :            :          */
    7107                 :          0 :         ctx = find_get_context(pmu, task, event->cpu);
    7108         [ #  # ]:          0 :         if (IS_ERR(ctx)) {
    7109                 :            :                 err = PTR_ERR(ctx);
    7110                 :            :                 goto err_alloc;
    7111                 :            :         }
    7112                 :            : 
    7113         [ #  # ]:          0 :         if (task) {
    7114                 :            :                 put_task_struct(task);
    7115                 :            :                 task = NULL;
    7116                 :            :         }
    7117                 :            : 
    7118                 :            :         /*
    7119                 :            :          * Look up the group leader (we will attach this event to it):
    7120                 :            :          */
    7121         [ #  # ]:          0 :         if (group_leader) {
    7122                 :            :                 err = -EINVAL;
    7123                 :            : 
    7124                 :            :                 /*
    7125                 :            :                  * Do not allow a recursive hierarchy (this new sibling
    7126                 :            :                  * becoming part of another group-sibling):
    7127                 :            :                  */
    7128         [ #  # ]:          0 :                 if (group_leader->group_leader != group_leader)
    7129                 :            :                         goto err_context;
    7130                 :            :                 /*
    7131                 :            :                  * Do not allow to attach to a group in a different
    7132                 :            :                  * task or CPU context:
    7133                 :            :                  */
    7134         [ #  # ]:          0 :                 if (move_group) {
    7135         [ #  # ]:          0 :                         if (group_leader->ctx->type != ctx->type)
    7136                 :            :                                 goto err_context;
    7137                 :            :                 } else {
    7138         [ #  # ]:          0 :                         if (group_leader->ctx != ctx)
    7139                 :            :                                 goto err_context;
    7140                 :            :                 }
    7141                 :            : 
    7142                 :            :                 /*
    7143                 :            :                  * Only a group leader can be exclusive or pinned
    7144                 :            :                  */
    7145         [ #  # ]:          0 :                 if (attr.exclusive || attr.pinned)
    7146                 :            :                         goto err_context;
    7147                 :            :         }
    7148                 :            : 
    7149         [ #  # ]:          0 :         if (output_event) {
    7150                 :          0 :                 err = perf_event_set_output(event, output_event);
    7151         [ #  # ]:          0 :                 if (err)
    7152                 :            :                         goto err_context;
    7153                 :            :         }
    7154                 :            : 
    7155                 :          0 :         event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
    7156                 :            :                                         f_flags);
    7157         [ #  # ]:          0 :         if (IS_ERR(event_file)) {
    7158                 :            :                 err = PTR_ERR(event_file);
    7159                 :            :                 goto err_context;
    7160                 :            :         }
    7161                 :            : 
    7162         [ #  # ]:          0 :         if (move_group) {
    7163                 :          0 :                 struct perf_event_context *gctx = group_leader->ctx;
    7164                 :            : 
    7165                 :          0 :                 mutex_lock(&gctx->mutex);
    7166                 :          0 :                 perf_remove_from_context(group_leader);
    7167                 :            : 
    7168                 :            :                 /*
    7169                 :            :                  * Removing from the context ends up with disabled
    7170                 :            :                  * event. What we want here is event in the initial
    7171                 :            :                  * startup state, ready to be add into new context.
    7172                 :            :                  */
    7173                 :            :                 perf_event__state_init(group_leader);
    7174         [ #  # ]:          0 :                 list_for_each_entry(sibling, &group_leader->sibling_list,
    7175                 :            :                                     group_entry) {
    7176                 :          0 :                         perf_remove_from_context(sibling);
    7177                 :            :                         perf_event__state_init(sibling);
    7178                 :          0 :                         put_ctx(gctx);
    7179                 :            :                 }
    7180                 :          0 :                 mutex_unlock(&gctx->mutex);
    7181                 :          0 :                 put_ctx(gctx);
    7182                 :            :         }
    7183                 :            : 
    7184 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(ctx->parent_ctx);
                 [ #  # ]
    7185                 :          0 :         mutex_lock(&ctx->mutex);
    7186                 :            : 
    7187         [ #  # ]:          0 :         if (move_group) {
    7188                 :            :                 synchronize_rcu();
    7189                 :          0 :                 perf_install_in_context(ctx, group_leader, event->cpu);
    7190                 :          0 :                 get_ctx(ctx);
    7191         [ #  # ]:          0 :                 list_for_each_entry(sibling, &group_leader->sibling_list,
    7192                 :            :                                     group_entry) {
    7193                 :          0 :                         perf_install_in_context(ctx, sibling, event->cpu);
    7194                 :          0 :                         get_ctx(ctx);
    7195                 :            :                 }
    7196                 :            :         }
    7197                 :            : 
    7198                 :          0 :         perf_install_in_context(ctx, event, event->cpu);
    7199                 :          0 :         perf_unpin_context(ctx);
    7200                 :          0 :         mutex_unlock(&ctx->mutex);
    7201                 :            : 
    7202                 :          0 :         put_online_cpus();
    7203                 :            : 
    7204                 :          0 :         event->owner = current;
    7205                 :            : 
    7206                 :          0 :         mutex_lock(&current->perf_event_mutex);
    7207                 :          0 :         list_add_tail(&event->owner_entry, &current->perf_event_list);
    7208                 :          0 :         mutex_unlock(&current->perf_event_mutex);
    7209                 :            : 
    7210                 :            :         /*
    7211                 :            :          * Precalculate sample_data sizes
    7212                 :            :          */
    7213                 :          0 :         perf_event__header_size(event);
    7214                 :          0 :         perf_event__id_header_size(event);
    7215                 :            : 
    7216                 :            :         /*
    7217                 :            :          * Drop the reference on the group_event after placing the
    7218                 :            :          * new event on the sibling_list. This ensures destruction
    7219                 :            :          * of the group leader will find the pointer to itself in
    7220                 :            :          * perf_group_detach().
    7221                 :            :          */
    7222                 :            :         fdput(group);
    7223                 :          0 :         fd_install(event_fd, event_file);
    7224                 :            :         return event_fd;
    7225                 :            : 
    7226                 :            : err_context:
    7227                 :          0 :         perf_unpin_context(ctx);
    7228                 :          0 :         put_ctx(ctx);
    7229                 :            : err_alloc:
    7230                 :          0 :         free_event(event);
    7231                 :            : err_task:
    7232                 :          0 :         put_online_cpus();
    7233         [ #  # ]:          0 :         if (task)
    7234                 :            :                 put_task_struct(task);
    7235                 :            : err_group_fd:
    7236                 :            :         fdput(group);
    7237                 :            : err_fd:
    7238                 :          0 :         put_unused_fd(event_fd);
    7239                 :            :         return err;
    7240                 :            : }
    7241                 :            : 
    7242                 :            : /**
    7243                 :            :  * perf_event_create_kernel_counter
    7244                 :            :  *
    7245                 :            :  * @attr: attributes of the counter to create
    7246                 :            :  * @cpu: cpu in which the counter is bound
    7247                 :            :  * @task: task to profile (NULL for percpu)
    7248                 :            :  */
    7249                 :            : struct perf_event *
    7250                 :          0 : perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
    7251                 :            :                                  struct task_struct *task,
    7252                 :            :                                  perf_overflow_handler_t overflow_handler,
    7253                 :            :                                  void *context)
    7254                 :            : {
    7255                 :            :         struct perf_event_context *ctx;
    7256                 :            :         struct perf_event *event;
    7257                 :            :         int err;
    7258                 :            : 
    7259                 :            :         /*
    7260                 :            :          * Get the target context (task or percpu):
    7261                 :            :          */
    7262                 :            : 
    7263                 :          0 :         event = perf_event_alloc(attr, cpu, task, NULL, NULL,
    7264                 :            :                                  overflow_handler, context);
    7265         [ #  # ]:          0 :         if (IS_ERR(event)) {
    7266                 :            :                 err = PTR_ERR(event);
    7267                 :          0 :                 goto err;
    7268                 :            :         }
    7269                 :            : 
    7270                 :          0 :         account_event(event);
    7271                 :            : 
    7272                 :          0 :         ctx = find_get_context(event->pmu, task, cpu);
    7273         [ #  # ]:          0 :         if (IS_ERR(ctx)) {
    7274                 :            :                 err = PTR_ERR(ctx);
    7275                 :            :                 goto err_free;
    7276                 :            :         }
    7277                 :            : 
    7278 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(ctx->parent_ctx);
                 [ #  # ]
    7279                 :          0 :         mutex_lock(&ctx->mutex);
    7280                 :          0 :         perf_install_in_context(ctx, event, cpu);
    7281                 :          0 :         perf_unpin_context(ctx);
    7282                 :          0 :         mutex_unlock(&ctx->mutex);
    7283                 :            : 
    7284                 :          0 :         return event;
    7285                 :            : 
    7286                 :            : err_free:
    7287                 :          0 :         free_event(event);
    7288                 :            : err:
    7289                 :          0 :         return ERR_PTR(err);
    7290                 :            : }
    7291                 :            : EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
    7292                 :            : 
    7293                 :          0 : void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
    7294                 :            : {
    7295                 :            :         struct perf_event_context *src_ctx;
    7296                 :            :         struct perf_event_context *dst_ctx;
    7297                 :            :         struct perf_event *event, *tmp;
    7298                 :          0 :         LIST_HEAD(events);
    7299                 :            : 
    7300                 :          0 :         src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
    7301                 :          0 :         dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
    7302                 :            : 
    7303                 :          0 :         mutex_lock(&src_ctx->mutex);
    7304         [ #  # ]:          0 :         list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
    7305                 :            :                                  event_entry) {
    7306                 :          0 :                 perf_remove_from_context(event);
    7307                 :          0 :                 unaccount_event_cpu(event, src_cpu);
    7308                 :          0 :                 put_ctx(src_ctx);
    7309                 :          0 :                 list_add(&event->migrate_entry, &events);
    7310                 :            :         }
    7311                 :          0 :         mutex_unlock(&src_ctx->mutex);
    7312                 :            : 
    7313                 :            :         synchronize_rcu();
    7314                 :            : 
    7315                 :          0 :         mutex_lock(&dst_ctx->mutex);
    7316         [ #  # ]:          0 :         list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
    7317                 :            :                 list_del(&event->migrate_entry);
    7318         [ #  # ]:          0 :                 if (event->state >= PERF_EVENT_STATE_OFF)
    7319                 :          0 :                         event->state = PERF_EVENT_STATE_INACTIVE;
    7320                 :          0 :                 account_event_cpu(event, dst_cpu);
    7321                 :          0 :                 perf_install_in_context(dst_ctx, event, dst_cpu);
    7322                 :          0 :                 get_ctx(dst_ctx);
    7323                 :            :         }
    7324                 :          0 :         mutex_unlock(&dst_ctx->mutex);
    7325                 :          0 : }
    7326                 :            : EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
    7327                 :            : 
    7328                 :          0 : static void sync_child_event(struct perf_event *child_event,
    7329                 :            :                                struct task_struct *child)
    7330                 :            : {
    7331                 :          0 :         struct perf_event *parent_event = child_event->parent;
    7332                 :            :         u64 child_val;
    7333                 :            : 
    7334         [ #  # ]:          0 :         if (child_event->attr.inherit_stat)
    7335                 :          0 :                 perf_event_read_event(child_event, child);
    7336                 :            : 
    7337                 :            :         child_val = perf_event_count(child_event);
    7338                 :            : 
    7339                 :            :         /*
    7340                 :            :          * Add back the child's count to the parent's count:
    7341                 :            :          */
    7342                 :          0 :         atomic64_add(child_val, &parent_event->child_count);
    7343                 :          0 :         atomic64_add(child_event->total_time_enabled,
    7344                 :            :                      &parent_event->child_total_time_enabled);
    7345                 :          0 :         atomic64_add(child_event->total_time_running,
    7346                 :            :                      &parent_event->child_total_time_running);
    7347                 :            : 
    7348                 :            :         /*
    7349                 :            :          * Remove this event from the parent's list
    7350                 :            :          */
    7351 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(parent_event->ctx->parent_ctx);
                 [ #  # ]
    7352                 :          0 :         mutex_lock(&parent_event->child_mutex);
    7353                 :          0 :         list_del_init(&child_event->child_list);
    7354                 :          0 :         mutex_unlock(&parent_event->child_mutex);
    7355                 :            : 
    7356                 :            :         /*
    7357                 :            :          * Release the parent event, if this was the last
    7358                 :            :          * reference to it.
    7359                 :            :          */
    7360                 :          0 :         put_event(parent_event);
    7361                 :          0 : }
    7362                 :            : 
    7363                 :            : static void
    7364                 :          0 : __perf_event_exit_task(struct perf_event *child_event,
    7365                 :            :                          struct perf_event_context *child_ctx,
    7366                 :            :                          struct task_struct *child)
    7367                 :            : {
    7368         [ #  # ]:          0 :         if (child_event->parent) {
    7369                 :          0 :                 raw_spin_lock_irq(&child_ctx->lock);
    7370                 :          0 :                 perf_group_detach(child_event);
    7371                 :            :                 raw_spin_unlock_irq(&child_ctx->lock);
    7372                 :            :         }
    7373                 :            : 
    7374                 :          0 :         perf_remove_from_context(child_event);
    7375                 :            : 
    7376                 :            :         /*
    7377                 :            :          * It can happen that the parent exits first, and has events
    7378                 :            :          * that are still around due to the child reference. These
    7379                 :            :          * events need to be zapped.
    7380                 :            :          */
    7381         [ #  # ]:          0 :         if (child_event->parent) {
    7382                 :          0 :                 sync_child_event(child_event, child);
    7383                 :          0 :                 free_event(child_event);
    7384                 :            :         }
    7385                 :          0 : }
    7386                 :            : 
    7387                 :          0 : static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
    7388                 :            : {
    7389                 :            :         struct perf_event *child_event, *tmp;
    7390                 :            :         struct perf_event_context *child_ctx;
    7391                 :            :         unsigned long flags;
    7392                 :            : 
    7393         [ +  - ]:    2208438 :         if (likely(!child->perf_event_ctxp[ctxn])) {
    7394                 :    2208438 :                 perf_event_task(child, NULL, 0);
    7395                 :    2208422 :                 return;
    7396                 :            :         }
    7397                 :            : 
    7398                 :            :         local_irq_save(flags);
    7399                 :            :         /*
    7400                 :            :          * We can't reschedule here because interrupts are disabled,
    7401                 :            :          * and either child is current or it is a task that can't be
    7402                 :            :          * scheduled, so we are now safe from rescheduling changing
    7403                 :            :          * our context.
    7404                 :            :          */
    7405                 :          0 :         child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
    7406                 :            : 
    7407                 :            :         /*
    7408                 :            :          * Take the context lock here so that if find_get_context is
    7409                 :            :          * reading child->perf_event_ctxp, we wait until it has
    7410                 :            :          * incremented the context's refcount before we do put_ctx below.
    7411                 :            :          */
    7412                 :          0 :         raw_spin_lock(&child_ctx->lock);
    7413                 :          0 :         task_ctx_sched_out(child_ctx);
    7414                 :          0 :         child->perf_event_ctxp[ctxn] = NULL;
    7415                 :            :         /*
    7416                 :            :          * If this context is a clone; unclone it so it can't get
    7417                 :            :          * swapped to another process while we're removing all
    7418                 :            :          * the events from it.
    7419                 :            :          */
    7420                 :            :         unclone_ctx(child_ctx);
    7421                 :          0 :         update_context_time(child_ctx);
    7422                 :          0 :         raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
    7423                 :            : 
    7424                 :            :         /*
    7425                 :            :          * Report the task dead after unscheduling the events so that we
    7426                 :            :          * won't get any samples after PERF_RECORD_EXIT. We can however still
    7427                 :            :          * get a few PERF_RECORD_READ events.
    7428                 :            :          */
    7429                 :          0 :         perf_event_task(child, child_ctx, 0);
    7430                 :            : 
    7431                 :            :         /*
    7432                 :            :          * We can recurse on the same lock type through:
    7433                 :            :          *
    7434                 :            :          *   __perf_event_exit_task()
    7435                 :            :          *     sync_child_event()
    7436                 :            :          *       put_event()
    7437                 :            :          *         mutex_lock(&ctx->mutex)
    7438                 :            :          *
    7439                 :            :          * But since its the parent context it won't be the same instance.
    7440                 :            :          */
    7441                 :          0 :         mutex_lock(&child_ctx->mutex);
    7442                 :            : 
    7443                 :            : again:
    7444         [ #  # ]:    2208438 :         list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
    7445                 :            :                                  group_entry)
    7446                 :          0 :                 __perf_event_exit_task(child_event, child_ctx, child);
    7447                 :            : 
    7448         [ #  # ]:          0 :         list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
    7449                 :            :                                  group_entry)
    7450                 :          0 :                 __perf_event_exit_task(child_event, child_ctx, child);
    7451                 :            : 
    7452                 :            :         /*
    7453                 :            :          * If the last event was a group event, it will have appended all
    7454                 :            :          * its siblings to the list, but we obtained 'tmp' before that which
    7455                 :            :          * will still point to the list head terminating the iteration.
    7456                 :            :          */
    7457 [ #  # ][ #  # ]:          0 :         if (!list_empty(&child_ctx->pinned_groups) ||
    7458                 :            :             !list_empty(&child_ctx->flexible_groups))
    7459                 :            :                 goto again;
    7460                 :            : 
    7461                 :          0 :         mutex_unlock(&child_ctx->mutex);
    7462                 :            : 
    7463                 :          0 :         put_ctx(child_ctx);
    7464                 :            : }
    7465                 :            : 
    7466                 :            : /*
    7467                 :            :  * When a child task exits, feed back event values to parent events.
    7468                 :            :  */
    7469                 :          0 : void perf_event_exit_task(struct task_struct *child)
    7470                 :            : {
    7471                 :            :         struct perf_event *event, *tmp;
    7472                 :            :         int ctxn;
    7473                 :            : 
    7474                 :    1104240 :         mutex_lock(&child->perf_event_mutex);
    7475         [ -  + ]:    1104236 :         list_for_each_entry_safe(event, tmp, &child->perf_event_list,
    7476                 :            :                                  owner_entry) {
    7477                 :            :                 list_del_init(&event->owner_entry);
    7478                 :            : 
    7479                 :            :                 /*
    7480                 :            :                  * Ensure the list deletion is visible before we clear
    7481                 :            :                  * the owner, closes a race against perf_release() where
    7482                 :            :                  * we need to serialize on the owner->perf_event_mutex.
    7483                 :            :                  */
    7484                 :          0 :                 smp_wmb();
    7485                 :          0 :                 event->owner = NULL;
    7486                 :            :         }
    7487                 :    1104236 :         mutex_unlock(&child->perf_event_mutex);
    7488                 :            : 
    7489         [ +  + ]:    4416922 :         for_each_task_context_nr(ctxn)
    7490                 :    2208436 :                 perf_event_exit_task_context(child, ctxn);
    7491                 :    1104246 : }
    7492                 :            : 
    7493                 :          0 : static void perf_free_event(struct perf_event *event,
    7494                 :            :                             struct perf_event_context *ctx)
    7495                 :            : {
    7496                 :          0 :         struct perf_event *parent = event->parent;
    7497                 :            : 
    7498 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(!parent))
         [ #  # ][ #  # ]
    7499                 :          0 :                 return;
    7500                 :            : 
    7501                 :          0 :         mutex_lock(&parent->child_mutex);
    7502                 :          0 :         list_del_init(&event->child_list);
    7503                 :          0 :         mutex_unlock(&parent->child_mutex);
    7504                 :            : 
    7505                 :          0 :         put_event(parent);
    7506                 :            : 
    7507                 :          0 :         perf_group_detach(event);
    7508                 :          0 :         list_del_event(event, ctx);
    7509                 :          0 :         free_event(event);
    7510                 :            : }
    7511                 :            : 
    7512                 :            : /*
    7513                 :            :  * free an unexposed, unused context as created by inheritance by
    7514                 :            :  * perf_event_init_task below, used by fork() in case of fail.
    7515                 :            :  */
    7516                 :          0 : void perf_event_free_task(struct task_struct *task)
    7517                 :            : {
    7518                 :            :         struct perf_event_context *ctx;
    7519                 :            :         struct perf_event *event, *tmp;
    7520                 :            :         int ctxn;
    7521                 :            : 
    7522         [ +  + ]:         15 :         for_each_task_context_nr(ctxn) {
    7523                 :         10 :                 ctx = task->perf_event_ctxp[ctxn];
    7524         [ +  - ]:         10 :                 if (!ctx)
    7525                 :         10 :                         continue;
    7526                 :            : 
    7527                 :          0 :                 mutex_lock(&ctx->mutex);
    7528                 :            : again:
    7529         [ #  # ]:          5 :                 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
    7530                 :            :                                 group_entry)
    7531                 :          0 :                         perf_free_event(event, ctx);
    7532                 :            : 
    7533         [ #  # ]:          0 :                 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
    7534                 :            :                                 group_entry)
    7535                 :          0 :                         perf_free_event(event, ctx);
    7536                 :            : 
    7537 [ #  # ][ #  # ]:          0 :                 if (!list_empty(&ctx->pinned_groups) ||
    7538                 :            :                                 !list_empty(&ctx->flexible_groups))
    7539                 :            :                         goto again;
    7540                 :            : 
    7541                 :          0 :                 mutex_unlock(&ctx->mutex);
    7542                 :            : 
    7543                 :          0 :                 put_ctx(ctx);
    7544                 :            :         }
    7545                 :          5 : }
    7546                 :            : 
    7547                 :          0 : void perf_event_delayed_put(struct task_struct *task)
    7548                 :            : {
    7549                 :            :         int ctxn;
    7550                 :            : 
    7551         [ +  + ]:    3310082 :         for_each_task_context_nr(ctxn)
    7552 [ -  + ][ #  # ]:    2206474 :                 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
                 [ -  + ]
    7553                 :    1103608 : }
    7554                 :            : 
    7555                 :            : /*
    7556                 :            :  * inherit a event from parent task to child task:
    7557                 :            :  */
    7558                 :            : static struct perf_event *
    7559                 :          0 : inherit_event(struct perf_event *parent_event,
    7560                 :            :               struct task_struct *parent,
    7561                 :            :               struct perf_event_context *parent_ctx,
    7562                 :            :               struct task_struct *child,
    7563                 :            :               struct perf_event *group_leader,
    7564                 :            :               struct perf_event_context *child_ctx)
    7565                 :            : {
    7566                 :            :         struct perf_event *child_event;
    7567                 :            :         unsigned long flags;
    7568                 :            : 
    7569                 :            :         /*
    7570                 :            :          * Instead of creating recursive hierarchies of events,
    7571                 :            :          * we link inherited events back to the original parent,
    7572                 :            :          * which has a filp for sure, which we use as the reference
    7573                 :            :          * count:
    7574                 :            :          */
    7575         [ #  # ]:          0 :         if (parent_event->parent)
    7576                 :            :                 parent_event = parent_event->parent;
    7577                 :            : 
    7578                 :          0 :         child_event = perf_event_alloc(&parent_event->attr,
    7579                 :            :                                            parent_event->cpu,
    7580                 :            :                                            child,
    7581                 :            :                                            group_leader, parent_event,
    7582                 :            :                                            NULL, NULL);
    7583         [ #  # ]:          0 :         if (IS_ERR(child_event))
    7584                 :            :                 return child_event;
    7585                 :            : 
    7586         [ #  # ]:          0 :         if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
    7587                 :          0 :                 free_event(child_event);
    7588                 :            :                 return NULL;
    7589                 :            :         }
    7590                 :            : 
    7591                 :          0 :         get_ctx(child_ctx);
    7592                 :            : 
    7593                 :            :         /*
    7594                 :            :          * Make the child state follow the state of the parent event,
    7595                 :            :          * not its attr.disabled bit.  We hold the parent's mutex,
    7596                 :            :          * so we won't race with perf_event_{en, dis}able_family.
    7597                 :            :          */
    7598         [ #  # ]:          0 :         if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
    7599                 :          0 :                 child_event->state = PERF_EVENT_STATE_INACTIVE;
    7600                 :            :         else
    7601                 :          0 :                 child_event->state = PERF_EVENT_STATE_OFF;
    7602                 :            : 
    7603         [ #  # ]:          0 :         if (parent_event->attr.freq) {
    7604                 :          0 :                 u64 sample_period = parent_event->hw.sample_period;
    7605                 :            :                 struct hw_perf_event *hwc = &child_event->hw;
    7606                 :            : 
    7607                 :          0 :                 hwc->sample_period = sample_period;
    7608                 :          0 :                 hwc->last_period   = sample_period;
    7609                 :            : 
    7610                 :          0 :                 local64_set(&hwc->period_left, sample_period);
    7611                 :            :         }
    7612                 :            : 
    7613                 :          0 :         child_event->ctx = child_ctx;
    7614                 :          0 :         child_event->overflow_handler = parent_event->overflow_handler;
    7615                 :            :         child_event->overflow_handler_context
    7616                 :          0 :                 = parent_event->overflow_handler_context;
    7617                 :            : 
    7618                 :            :         /*
    7619                 :            :          * Precalculate sample_data sizes
    7620                 :            :          */
    7621                 :          0 :         perf_event__header_size(child_event);
    7622                 :          0 :         perf_event__id_header_size(child_event);
    7623                 :            : 
    7624                 :            :         /*
    7625                 :            :          * Link it up in the child's context:
    7626                 :            :          */
    7627                 :          0 :         raw_spin_lock_irqsave(&child_ctx->lock, flags);
    7628                 :          0 :         add_event_to_ctx(child_event, child_ctx);
    7629                 :          0 :         raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
    7630                 :            : 
    7631                 :            :         /*
    7632                 :            :          * Link this into the parent event's child list
    7633                 :            :          */
    7634 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(parent_event->ctx->parent_ctx);
                 [ #  # ]
    7635                 :          0 :         mutex_lock(&parent_event->child_mutex);
    7636                 :          0 :         list_add_tail(&child_event->child_list, &parent_event->child_list);
    7637                 :          0 :         mutex_unlock(&parent_event->child_mutex);
    7638                 :            : 
    7639                 :            :         return child_event;
    7640                 :            : }
    7641                 :            : 
    7642                 :          0 : static int inherit_group(struct perf_event *parent_event,
    7643                 :            :               struct task_struct *parent,
    7644                 :            :               struct perf_event_context *parent_ctx,
    7645                 :            :               struct task_struct *child,
    7646                 :            :               struct perf_event_context *child_ctx)
    7647                 :            : {
    7648                 :            :         struct perf_event *leader;
    7649                 :            :         struct perf_event *sub;
    7650                 :            :         struct perf_event *child_ctr;
    7651                 :            : 
    7652                 :          0 :         leader = inherit_event(parent_event, parent, parent_ctx,
    7653                 :            :                                  child, NULL, child_ctx);
    7654         [ #  # ]:          0 :         if (IS_ERR(leader))
    7655                 :            :                 return PTR_ERR(leader);
    7656         [ #  # ]:          0 :         list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
    7657                 :          0 :                 child_ctr = inherit_event(sub, parent, parent_ctx,
    7658                 :            :                                             child, leader, child_ctx);
    7659         [ #  # ]:          0 :                 if (IS_ERR(child_ctr))
    7660                 :            :                         return PTR_ERR(child_ctr);
    7661                 :            :         }
    7662                 :            :         return 0;
    7663                 :            : }
    7664                 :            : 
    7665                 :            : static int
    7666                 :          0 : inherit_task_group(struct perf_event *event, struct task_struct *parent,
    7667                 :            :                    struct perf_event_context *parent_ctx,
    7668                 :            :                    struct task_struct *child, int ctxn,
    7669                 :            :                    int *inherited_all)
    7670                 :            : {
    7671                 :            :         int ret;
    7672                 :            :         struct perf_event_context *child_ctx;
    7673                 :            : 
    7674         [ #  # ]:          0 :         if (!event->attr.inherit) {
    7675                 :          0 :                 *inherited_all = 0;
    7676                 :            :                 return 0;
    7677                 :            :         }
    7678                 :            : 
    7679                 :          0 :         child_ctx = child->perf_event_ctxp[ctxn];
    7680         [ #  # ]:          0 :         if (!child_ctx) {
    7681                 :            :                 /*
    7682                 :            :                  * This is executed from the parent task context, so
    7683                 :            :                  * inherit events that have been marked for cloning.
    7684                 :            :                  * First allocate and initialize a context for the
    7685                 :            :                  * child.
    7686                 :            :                  */
    7687                 :            : 
    7688                 :          0 :                 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
    7689         [ #  # ]:          0 :                 if (!child_ctx)
    7690                 :            :                         return -ENOMEM;
    7691                 :            : 
    7692                 :          0 :                 child->perf_event_ctxp[ctxn] = child_ctx;
    7693                 :            :         }
    7694                 :            : 
    7695                 :          0 :         ret = inherit_group(event, parent, parent_ctx,
    7696                 :            :                             child, child_ctx);
    7697                 :            : 
    7698         [ #  # ]:          0 :         if (ret)
    7699                 :          0 :                 *inherited_all = 0;
    7700                 :            : 
    7701                 :            :         return ret;
    7702                 :            : }
    7703                 :            : 
    7704                 :            : /*
    7705                 :            :  * Initialize the perf_event context in task_struct
    7706                 :            :  */
    7707                 :          0 : int perf_event_init_context(struct task_struct *child, int ctxn)
    7708                 :            : {
    7709                 :            :         struct perf_event_context *child_ctx, *parent_ctx;
    7710                 :            :         struct perf_event_context *cloned_ctx;
    7711                 :            :         struct perf_event *event;
    7712                 :    2208441 :         struct task_struct *parent = current;
    7713                 :    2208441 :         int inherited_all = 1;
    7714                 :            :         unsigned long flags;
    7715                 :            :         int ret = 0;
    7716                 :            : 
    7717         [ -  + ]:    2208441 :         if (likely(!parent->perf_event_ctxp[ctxn]))
    7718                 :            :                 return 0;
    7719                 :            : 
    7720                 :            :         /*
    7721                 :            :          * If the parent's context is a clone, pin it so it won't get
    7722                 :            :          * swapped under us.
    7723                 :            :          */
    7724                 :          0 :         parent_ctx = perf_pin_task_context(parent, ctxn);
    7725                 :            : 
    7726                 :            :         /*
    7727                 :            :          * No need to check if parent_ctx != NULL here; since we saw
    7728                 :            :          * it non-NULL earlier, the only reason for it to become NULL
    7729                 :            :          * is if we exit, and since we're currently in the middle of
    7730                 :            :          * a fork we can't be exiting at the same time.
    7731                 :            :          */
    7732                 :            : 
    7733                 :            :         /*
    7734                 :            :          * Lock the parent list. No need to lock the child - not PID
    7735                 :            :          * hashed yet and not running, so nobody can access it.
    7736                 :            :          */
    7737                 :          0 :         mutex_lock(&parent_ctx->mutex);
    7738                 :            : 
    7739                 :            :         /*
    7740                 :            :          * We dont have to disable NMIs - we are only looking at
    7741                 :            :          * the list, not manipulating it:
    7742                 :            :          */
    7743         [ #  # ]:          0 :         list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
    7744                 :          0 :                 ret = inherit_task_group(event, parent, parent_ctx,
    7745                 :            :                                          child, ctxn, &inherited_all);
    7746         [ #  # ]:          0 :                 if (ret)
    7747                 :            :                         break;
    7748                 :            :         }
    7749                 :            : 
    7750                 :            :         /*
    7751                 :            :          * We can't hold ctx->lock when iterating the ->flexible_group list due
    7752                 :            :          * to allocations, but we need to prevent rotation because
    7753                 :            :          * rotate_ctx() will change the list from interrupt context.
    7754                 :            :          */
    7755                 :          0 :         raw_spin_lock_irqsave(&parent_ctx->lock, flags);
    7756                 :          0 :         parent_ctx->rotate_disable = 1;
    7757                 :          0 :         raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
    7758                 :            : 
    7759         [ #  # ]:          0 :         list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
    7760                 :          0 :                 ret = inherit_task_group(event, parent, parent_ctx,
    7761                 :            :                                          child, ctxn, &inherited_all);
    7762         [ #  # ]:          0 :                 if (ret)
    7763                 :            :                         break;
    7764                 :            :         }
    7765                 :            : 
    7766                 :          0 :         raw_spin_lock_irqsave(&parent_ctx->lock, flags);
    7767                 :          0 :         parent_ctx->rotate_disable = 0;
    7768                 :            : 
    7769                 :          0 :         child_ctx = child->perf_event_ctxp[ctxn];
    7770                 :            : 
    7771 [ #  # ][ #  # ]:          0 :         if (child_ctx && inherited_all) {
    7772                 :            :                 /*
    7773                 :            :                  * Mark the child context as a clone of the parent
    7774                 :            :                  * context, or of whatever the parent is a clone of.
    7775                 :            :                  *
    7776                 :            :                  * Note that if the parent is a clone, the holding of
    7777                 :            :                  * parent_ctx->lock avoids it from being uncloned.
    7778                 :            :                  */
    7779                 :          0 :                 cloned_ctx = parent_ctx->parent_ctx;
    7780         [ #  # ]:          0 :                 if (cloned_ctx) {
    7781                 :          0 :                         child_ctx->parent_ctx = cloned_ctx;
    7782                 :          0 :                         child_ctx->parent_gen = parent_ctx->parent_gen;
    7783                 :            :                 } else {
    7784                 :          0 :                         child_ctx->parent_ctx = parent_ctx;
    7785                 :          0 :                         child_ctx->parent_gen = parent_ctx->generation;
    7786                 :            :                 }
    7787                 :          0 :                 get_ctx(child_ctx->parent_ctx);
    7788                 :            :         }
    7789                 :            : 
    7790                 :          0 :         raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
    7791                 :          0 :         mutex_unlock(&parent_ctx->mutex);
    7792                 :            : 
    7793                 :          0 :         perf_unpin_context(parent_ctx);
    7794                 :          0 :         put_ctx(parent_ctx);
    7795                 :            : 
    7796                 :          0 :         return ret;
    7797                 :            : }
    7798                 :            : 
    7799                 :            : /*
    7800                 :            :  * Initialize the perf_event context in task_struct
    7801                 :            :  */
    7802                 :          0 : int perf_event_init_task(struct task_struct *child)
    7803                 :            : {
    7804                 :            :         int ctxn, ret;
    7805                 :            : 
    7806                 :    1104225 :         memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
    7807                 :    1104226 :         mutex_init(&child->perf_event_mutex);
    7808                 :    1104213 :         INIT_LIST_HEAD(&child->perf_event_list);
    7809                 :            : 
    7810         [ +  + ]:    3312640 :         for_each_task_context_nr(ctxn) {
    7811                 :    2208436 :                 ret = perf_event_init_context(child, ctxn);
    7812         [ +  + ]:    2208429 :                 if (ret)
    7813                 :            :                         return ret;
    7814                 :            :         }
    7815                 :            : 
    7816                 :            :         return 0;
    7817                 :            : }
    7818                 :            : 
    7819                 :          0 : static void __init perf_event_init_all_cpus(void)
    7820                 :            : {
    7821                 :            :         struct swevent_htable *swhash;
    7822                 :            :         int cpu;
    7823                 :            : 
    7824         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    7825                 :          0 :                 swhash = &per_cpu(swevent_htable, cpu);
    7826                 :          0 :                 mutex_init(&swhash->hlist_mutex);
    7827                 :          0 :                 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
    7828                 :            :         }
    7829                 :          0 : }
    7830                 :            : 
    7831                 :          0 : static void perf_event_init_cpu(int cpu)
    7832                 :            : {
    7833                 :         81 :         struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
    7834                 :            : 
    7835                 :         81 :         mutex_lock(&swhash->hlist_mutex);
    7836         [ -  + ]:         81 :         if (swhash->hlist_refcount > 0) {
    7837                 :            :                 struct swevent_hlist *hlist;
    7838                 :            : 
    7839                 :            :                 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
    7840         [ #  # ]:          0 :                 WARN_ON(!hlist);
    7841                 :          0 :                 rcu_assign_pointer(swhash->swevent_hlist, hlist);
    7842                 :            :         }
    7843                 :         81 :         mutex_unlock(&swhash->hlist_mutex);
    7844                 :         81 : }
    7845                 :            : 
    7846                 :            : #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
    7847                 :          0 : static void perf_pmu_rotate_stop(struct pmu *pmu)
    7848                 :            : {
    7849                 :       1248 :         struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
    7850                 :            : 
    7851         [ -  + ]:        624 :         WARN_ON(!irqs_disabled());
    7852                 :            : 
    7853                 :          0 :         list_del_init(&cpuctx->rotation_list);
    7854                 :          0 : }
    7855                 :            : 
    7856                 :          0 : static void __perf_event_exit_context(void *__info)
    7857                 :            : {
    7858                 :            :         struct perf_event_context *ctx = __info;
    7859                 :            :         struct perf_event *event;
    7860                 :            : 
    7861                 :        624 :         perf_pmu_rotate_stop(ctx->pmu);
    7862                 :            : 
    7863                 :            :         rcu_read_lock();
    7864         [ -  + ]:       1248 :         list_for_each_entry_rcu(event, &ctx->event_list, event_entry)
    7865                 :          0 :                 __perf_remove_from_context(event);
    7866                 :            :         rcu_read_unlock();
    7867                 :        624 : }
    7868                 :            : 
    7869                 :          0 : static void perf_event_exit_cpu_context(int cpu)
    7870                 :            : {
    7871                 :            :         struct perf_event_context *ctx;
    7872                 :            :         struct pmu *pmu;
    7873                 :            :         int idx;
    7874                 :            : 
    7875                 :            :         idx = srcu_read_lock(&pmus_srcu);
    7876         [ +  + ]:        780 :         list_for_each_entry_rcu(pmu, &pmus, entry) {
    7877                 :        624 :                 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
    7878                 :            : 
    7879                 :        624 :                 mutex_lock(&ctx->mutex);
    7880                 :        624 :                 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
    7881                 :        624 :                 mutex_unlock(&ctx->mutex);
    7882                 :            :         }
    7883                 :            :         srcu_read_unlock(&pmus_srcu, idx);
    7884                 :         78 : }
    7885                 :            : 
    7886                 :          0 : static void perf_event_exit_cpu(int cpu)
    7887                 :            : {
    7888                 :         78 :         struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
    7889                 :            : 
    7890                 :         78 :         perf_event_exit_cpu_context(cpu);
    7891                 :            : 
    7892                 :         78 :         mutex_lock(&swhash->hlist_mutex);
    7893                 :            :         swevent_hlist_release(swhash);
    7894                 :         78 :         mutex_unlock(&swhash->hlist_mutex);
    7895                 :         78 : }
    7896                 :            : #else
    7897                 :            : static inline void perf_event_exit_cpu(int cpu) { }
    7898                 :            : #endif
    7899                 :            : 
    7900                 :            : static int
    7901                 :          0 : perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
    7902                 :            : {
    7903                 :            :         int cpu;
    7904                 :            : 
    7905         [ #  # ]:          0 :         for_each_online_cpu(cpu)
    7906                 :          0 :                 perf_event_exit_cpu(cpu);
    7907                 :            : 
    7908                 :          0 :         return NOTIFY_OK;
    7909                 :            : }
    7910                 :            : 
    7911                 :            : /*
    7912                 :            :  * Run the perf reboot notifier at the very last possible moment so that
    7913                 :            :  * the generic watchdog code runs as long as possible.
    7914                 :            :  */
    7915                 :            : static struct notifier_block perf_reboot_notifier = {
    7916                 :            :         .notifier_call = perf_reboot,
    7917                 :            :         .priority = INT_MIN,
    7918                 :            : };
    7919                 :            : 
    7920                 :            : static int
    7921                 :          0 : perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
    7922                 :            : {
    7923                 :            :         unsigned int cpu = (long)hcpu;
    7924                 :            : 
    7925      [ +  +  + ]:        555 :         switch (action & ~CPU_TASKS_FROZEN) {
    7926                 :            : 
    7927                 :            :         case CPU_UP_PREPARE:
    7928                 :            :         case CPU_DOWN_FAILED:
    7929                 :         81 :                 perf_event_init_cpu(cpu);
    7930                 :         81 :                 break;
    7931                 :            : 
    7932                 :            :         case CPU_UP_CANCELED:
    7933                 :            :         case CPU_DOWN_PREPARE:
    7934                 :         78 :                 perf_event_exit_cpu(cpu);
    7935                 :         78 :                 break;
    7936                 :            :         default:
    7937                 :            :                 break;
    7938                 :            :         }
    7939                 :            : 
    7940                 :          0 :         return NOTIFY_OK;
    7941                 :            : }
    7942                 :            : 
    7943                 :          0 : void __init perf_event_init(void)
    7944                 :            : {
    7945                 :            :         int ret;
    7946                 :            : 
    7947                 :          0 :         idr_init(&pmu_idr);
    7948                 :            : 
    7949                 :          0 :         perf_event_init_all_cpus();
    7950                 :          0 :         init_srcu_struct(&pmus_srcu);
    7951                 :          0 :         perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
    7952                 :          0 :         perf_pmu_register(&perf_cpu_clock, NULL, -1);
    7953                 :          0 :         perf_pmu_register(&perf_task_clock, NULL, -1);
    7954                 :            :         perf_tp_register();
    7955         [ #  # ]:          0 :         perf_cpu_notifier(perf_cpu_notify);
    7956                 :          0 :         register_reboot_notifier(&perf_reboot_notifier);
    7957                 :            : 
    7958                 :          0 :         ret = init_hw_breakpoint();
    7959         [ #  # ]:          0 :         WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
    7960                 :            : 
    7961                 :            :         /* do not patch jump label more than once per second */
    7962                 :            :         jump_label_rate_limit(&perf_sched_events, HZ);
    7963                 :            : 
    7964                 :            :         /*
    7965                 :            :          * Build time assertion that we keep the data_head at the intended
    7966                 :            :          * location.  IOW, validation we got the __reserved[] size right.
    7967                 :            :          */
    7968                 :            :         BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
    7969                 :            :                      != 1024);
    7970                 :          0 : }
    7971                 :            : 
    7972                 :          0 : static int __init perf_event_sysfs_init(void)
    7973                 :            : {
    7974                 :            :         struct pmu *pmu;
    7975                 :            :         int ret;
    7976                 :            : 
    7977                 :          0 :         mutex_lock(&pmus_lock);
    7978                 :            : 
    7979                 :          0 :         ret = bus_register(&pmu_bus);
    7980         [ #  # ]:          0 :         if (ret)
    7981                 :            :                 goto unlock;
    7982                 :            : 
    7983         [ #  # ]:          0 :         list_for_each_entry(pmu, &pmus, entry) {
    7984 [ #  # ][ #  # ]:          0 :                 if (!pmu->name || pmu->type < 0)
    7985                 :          0 :                         continue;
    7986                 :            : 
    7987                 :          0 :                 ret = pmu_dev_alloc(pmu);
    7988         [ #  # ]:          0 :                 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
    7989                 :            :         }
    7990                 :          0 :         pmu_bus_running = 1;
    7991                 :            :         ret = 0;
    7992                 :            : 
    7993                 :            : unlock:
    7994                 :          0 :         mutex_unlock(&pmus_lock);
    7995                 :            : 
    7996                 :          0 :         return ret;
    7997                 :            : }
    7998                 :            : device_initcall(perf_event_sysfs_init);
    7999                 :            : 
    8000                 :            : #ifdef CONFIG_CGROUP_PERF
    8001                 :            : static struct cgroup_subsys_state *
    8002                 :            : perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
    8003                 :            : {
    8004                 :            :         struct perf_cgroup *jc;
    8005                 :            : 
    8006                 :            :         jc = kzalloc(sizeof(*jc), GFP_KERNEL);
    8007                 :            :         if (!jc)
    8008                 :            :                 return ERR_PTR(-ENOMEM);
    8009                 :            : 
    8010                 :            :         jc->info = alloc_percpu(struct perf_cgroup_info);
    8011                 :            :         if (!jc->info) {
    8012                 :            :                 kfree(jc);
    8013                 :            :                 return ERR_PTR(-ENOMEM);
    8014                 :            :         }
    8015                 :            : 
    8016                 :            :         return &jc->css;
    8017                 :            : }
    8018                 :            : 
    8019                 :            : static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
    8020                 :            : {
    8021                 :            :         struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
    8022                 :            : 
    8023                 :            :         free_percpu(jc->info);
    8024                 :            :         kfree(jc);
    8025                 :            : }
    8026                 :            : 
    8027                 :            : static int __perf_cgroup_move(void *info)
    8028                 :            : {
    8029                 :            :         struct task_struct *task = info;
    8030                 :            :         perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
    8031                 :            :         return 0;
    8032                 :            : }
    8033                 :            : 
    8034                 :            : static void perf_cgroup_attach(struct cgroup_subsys_state *css,
    8035                 :            :                                struct cgroup_taskset *tset)
    8036                 :            : {
    8037                 :            :         struct task_struct *task;
    8038                 :            : 
    8039                 :            :         cgroup_taskset_for_each(task, css, tset)
    8040                 :            :                 task_function_call(task, __perf_cgroup_move, task);
    8041                 :            : }
    8042                 :            : 
    8043                 :            : static void perf_cgroup_exit(struct cgroup_subsys_state *css,
    8044                 :            :                              struct cgroup_subsys_state *old_css,
    8045                 :            :                              struct task_struct *task)
    8046                 :            : {
    8047                 :            :         /*
    8048                 :            :          * cgroup_exit() is called in the copy_process() failure path.
    8049                 :            :          * Ignore this case since the task hasn't ran yet, this avoids
    8050                 :            :          * trying to poke a half freed task state from generic code.
    8051                 :            :          */
    8052                 :            :         if (!(task->flags & PF_EXITING))
    8053                 :            :                 return;
    8054                 :            : 
    8055                 :            :         task_function_call(task, __perf_cgroup_move, task);
    8056                 :            : }
    8057                 :            : 
    8058                 :            : struct cgroup_subsys perf_subsys = {
    8059                 :            :         .name           = "perf_event",
    8060                 :            :         .subsys_id      = perf_subsys_id,
    8061                 :            :         .css_alloc      = perf_cgroup_css_alloc,
    8062                 :            :         .css_free       = perf_cgroup_css_free,
    8063                 :            :         .exit           = perf_cgroup_exit,
    8064                 :            :         .attach         = perf_cgroup_attach,
    8065                 :            : };
    8066                 :            : #endif /* CONFIG_CGROUP_PERF */

Generated by: LCOV version 1.9