diff options
Diffstat (limited to 'kernel/perf_event.c')
-rw-r--r-- | kernel/perf_event.c | 75 |
1 files changed, 47 insertions, 28 deletions
diff --git a/kernel/perf_event.c b/kernel/perf_event.c index 84522c796987..656222fcf767 100644 --- a/kernel/perf_event.c +++ b/kernel/perf_event.c | |||
@@ -782,6 +782,10 @@ retry: | |||
782 | raw_spin_unlock_irq(&ctx->lock); | 782 | raw_spin_unlock_irq(&ctx->lock); |
783 | } | 783 | } |
784 | 784 | ||
785 | #define MAX_INTERRUPTS (~0ULL) | ||
786 | |||
787 | static void perf_log_throttle(struct perf_event *event, int enable); | ||
788 | |||
785 | static int | 789 | static int |
786 | event_sched_in(struct perf_event *event, | 790 | event_sched_in(struct perf_event *event, |
787 | struct perf_cpu_context *cpuctx, | 791 | struct perf_cpu_context *cpuctx, |
@@ -794,6 +798,17 @@ event_sched_in(struct perf_event *event, | |||
794 | 798 | ||
795 | event->state = PERF_EVENT_STATE_ACTIVE; | 799 | event->state = PERF_EVENT_STATE_ACTIVE; |
796 | event->oncpu = smp_processor_id(); | 800 | event->oncpu = smp_processor_id(); |
801 | |||
802 | /* | ||
803 | * Unthrottle events, since we scheduled we might have missed several | ||
804 | * ticks already, also for a heavily scheduling task there is little | ||
805 | * guarantee it'll get a tick in a timely manner. | ||
806 | */ | ||
807 | if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { | ||
808 | perf_log_throttle(event, 1); | ||
809 | event->hw.interrupts = 0; | ||
810 | } | ||
811 | |||
797 | /* | 812 | /* |
798 | * The new state must be visible before we turn it on in the hardware: | 813 | * The new state must be visible before we turn it on in the hardware: |
799 | */ | 814 | */ |
@@ -1596,10 +1611,6 @@ void __perf_event_task_sched_in(struct task_struct *task) | |||
1596 | } | 1611 | } |
1597 | } | 1612 | } |
1598 | 1613 | ||
1599 | #define MAX_INTERRUPTS (~0ULL) | ||
1600 | |||
1601 | static void perf_log_throttle(struct perf_event *event, int enable); | ||
1602 | |||
1603 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) | 1614 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
1604 | { | 1615 | { |
1605 | u64 frequency = event->attr.sample_freq; | 1616 | u64 frequency = event->attr.sample_freq; |
@@ -1901,11 +1912,12 @@ static void __perf_event_read(void *info) | |||
1901 | return; | 1912 | return; |
1902 | 1913 | ||
1903 | raw_spin_lock(&ctx->lock); | 1914 | raw_spin_lock(&ctx->lock); |
1904 | update_context_time(ctx); | 1915 | if (ctx->is_active) |
1916 | update_context_time(ctx); | ||
1905 | update_event_times(event); | 1917 | update_event_times(event); |
1918 | if (event->state == PERF_EVENT_STATE_ACTIVE) | ||
1919 | event->pmu->read(event); | ||
1906 | raw_spin_unlock(&ctx->lock); | 1920 | raw_spin_unlock(&ctx->lock); |
1907 | |||
1908 | event->pmu->read(event); | ||
1909 | } | 1921 | } |
1910 | 1922 | ||
1911 | static inline u64 perf_event_count(struct perf_event *event) | 1923 | static inline u64 perf_event_count(struct perf_event *event) |
@@ -1999,8 +2011,7 @@ static int alloc_callchain_buffers(void) | |||
1999 | * accessed from NMI. Use a temporary manual per cpu allocation | 2011 | * accessed from NMI. Use a temporary manual per cpu allocation |
2000 | * until that gets sorted out. | 2012 | * until that gets sorted out. |
2001 | */ | 2013 | */ |
2002 | size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) * | 2014 | size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]); |
2003 | num_possible_cpus(); | ||
2004 | 2015 | ||
2005 | entries = kzalloc(size, GFP_KERNEL); | 2016 | entries = kzalloc(size, GFP_KERNEL); |
2006 | if (!entries) | 2017 | if (!entries) |
@@ -2201,13 +2212,6 @@ find_lively_task_by_vpid(pid_t vpid) | |||
2201 | if (!task) | 2212 | if (!task) |
2202 | return ERR_PTR(-ESRCH); | 2213 | return ERR_PTR(-ESRCH); |
2203 | 2214 | ||
2204 | /* | ||
2205 | * Can't attach events to a dying task. | ||
2206 | */ | ||
2207 | err = -ESRCH; | ||
2208 | if (task->flags & PF_EXITING) | ||
2209 | goto errout; | ||
2210 | |||
2211 | /* Reuse ptrace permission checks for now. */ | 2215 | /* Reuse ptrace permission checks for now. */ |
2212 | err = -EACCES; | 2216 | err = -EACCES; |
2213 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) | 2217 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
@@ -2268,14 +2272,27 @@ retry: | |||
2268 | 2272 | ||
2269 | get_ctx(ctx); | 2273 | get_ctx(ctx); |
2270 | 2274 | ||
2271 | if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) { | 2275 | err = 0; |
2272 | /* | 2276 | mutex_lock(&task->perf_event_mutex); |
2273 | * We raced with some other task; use | 2277 | /* |
2274 | * the context they set. | 2278 | * If it has already passed perf_event_exit_task(). |
2275 | */ | 2279 | * we must see PF_EXITING, it takes this mutex too. |
2280 | */ | ||
2281 | if (task->flags & PF_EXITING) | ||
2282 | err = -ESRCH; | ||
2283 | else if (task->perf_event_ctxp[ctxn]) | ||
2284 | err = -EAGAIN; | ||
2285 | else | ||
2286 | rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); | ||
2287 | mutex_unlock(&task->perf_event_mutex); | ||
2288 | |||
2289 | if (unlikely(err)) { | ||
2276 | put_task_struct(task); | 2290 | put_task_struct(task); |
2277 | kfree(ctx); | 2291 | kfree(ctx); |
2278 | goto retry; | 2292 | |
2293 | if (err == -EAGAIN) | ||
2294 | goto retry; | ||
2295 | goto errout; | ||
2279 | } | 2296 | } |
2280 | } | 2297 | } |
2281 | 2298 | ||
@@ -5374,6 +5391,8 @@ free_dev: | |||
5374 | goto out; | 5391 | goto out; |
5375 | } | 5392 | } |
5376 | 5393 | ||
5394 | static struct lock_class_key cpuctx_mutex; | ||
5395 | |||
5377 | int perf_pmu_register(struct pmu *pmu, char *name, int type) | 5396 | int perf_pmu_register(struct pmu *pmu, char *name, int type) |
5378 | { | 5397 | { |
5379 | int cpu, ret; | 5398 | int cpu, ret; |
@@ -5422,6 +5441,7 @@ skip_type: | |||
5422 | 5441 | ||
5423 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); | 5442 | cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); |
5424 | __perf_event_init_context(&cpuctx->ctx); | 5443 | __perf_event_init_context(&cpuctx->ctx); |
5444 | lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); | ||
5425 | cpuctx->ctx.type = cpu_context; | 5445 | cpuctx->ctx.type = cpu_context; |
5426 | cpuctx->ctx.pmu = pmu; | 5446 | cpuctx->ctx.pmu = pmu; |
5427 | cpuctx->jiffies_interval = 1; | 5447 | cpuctx->jiffies_interval = 1; |
@@ -6127,7 +6147,7 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn) | |||
6127 | * scheduled, so we are now safe from rescheduling changing | 6147 | * scheduled, so we are now safe from rescheduling changing |
6128 | * our context. | 6148 | * our context. |
6129 | */ | 6149 | */ |
6130 | child_ctx = child->perf_event_ctxp[ctxn]; | 6150 | child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); |
6131 | task_ctx_sched_out(child_ctx, EVENT_ALL); | 6151 | task_ctx_sched_out(child_ctx, EVENT_ALL); |
6132 | 6152 | ||
6133 | /* | 6153 | /* |
@@ -6440,11 +6460,6 @@ int perf_event_init_context(struct task_struct *child, int ctxn) | |||
6440 | unsigned long flags; | 6460 | unsigned long flags; |
6441 | int ret = 0; | 6461 | int ret = 0; |
6442 | 6462 | ||
6443 | child->perf_event_ctxp[ctxn] = NULL; | ||
6444 | |||
6445 | mutex_init(&child->perf_event_mutex); | ||
6446 | INIT_LIST_HEAD(&child->perf_event_list); | ||
6447 | |||
6448 | if (likely(!parent->perf_event_ctxp[ctxn])) | 6463 | if (likely(!parent->perf_event_ctxp[ctxn])) |
6449 | return 0; | 6464 | return 0; |
6450 | 6465 | ||
@@ -6533,6 +6548,10 @@ int perf_event_init_task(struct task_struct *child) | |||
6533 | { | 6548 | { |
6534 | int ctxn, ret; | 6549 | int ctxn, ret; |
6535 | 6550 | ||
6551 | memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); | ||
6552 | mutex_init(&child->perf_event_mutex); | ||
6553 | INIT_LIST_HEAD(&child->perf_event_list); | ||
6554 | |||
6536 | for_each_task_context_nr(ctxn) { | 6555 | for_each_task_context_nr(ctxn) { |
6537 | ret = perf_event_init_context(child, ctxn); | 6556 | ret = perf_event_init_context(child, ctxn); |
6538 | if (ret) | 6557 | if (ret) |