aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/perf_counter.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/perf_counter.c')
-rw-r--r--kernel/perf_counter.c855
1 files changed, 555 insertions, 300 deletions
diff --git a/kernel/perf_counter.c b/kernel/perf_counter.c
index a641eb753b8c..f274e1959885 100644
--- a/kernel/perf_counter.c
+++ b/kernel/perf_counter.c
@@ -42,6 +42,7 @@ static int perf_overcommit __read_mostly = 1;
42static atomic_t nr_counters __read_mostly; 42static atomic_t nr_counters __read_mostly;
43static atomic_t nr_mmap_counters __read_mostly; 43static atomic_t nr_mmap_counters __read_mostly;
44static atomic_t nr_comm_counters __read_mostly; 44static atomic_t nr_comm_counters __read_mostly;
45static atomic_t nr_task_counters __read_mostly;
45 46
46/* 47/*
47 * perf counter paranoia level: 48 * perf counter paranoia level:
@@ -87,6 +88,7 @@ void __weak hw_perf_disable(void) { barrier(); }
87void __weak hw_perf_enable(void) { barrier(); } 88void __weak hw_perf_enable(void) { barrier(); }
88 89
89void __weak hw_perf_counter_setup(int cpu) { barrier(); } 90void __weak hw_perf_counter_setup(int cpu) { barrier(); }
91void __weak hw_perf_counter_setup_online(int cpu) { barrier(); }
90 92
91int __weak 93int __weak
92hw_perf_group_sched_in(struct perf_counter *group_leader, 94hw_perf_group_sched_in(struct perf_counter *group_leader,
@@ -146,6 +148,28 @@ static void put_ctx(struct perf_counter_context *ctx)
146 } 148 }
147} 149}
148 150
151static void unclone_ctx(struct perf_counter_context *ctx)
152{
153 if (ctx->parent_ctx) {
154 put_ctx(ctx->parent_ctx);
155 ctx->parent_ctx = NULL;
156 }
157}
158
159/*
160 * If we inherit counters we want to return the parent counter id
161 * to userspace.
162 */
163static u64 primary_counter_id(struct perf_counter *counter)
164{
165 u64 id = counter->id;
166
167 if (counter->parent)
168 id = counter->parent->id;
169
170 return id;
171}
172
149/* 173/*
150 * Get the perf_counter_context for a task and lock it. 174 * Get the perf_counter_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked, 175 * This has to cope with with the fact that until it is locked,
@@ -283,6 +307,10 @@ counter_sched_out(struct perf_counter *counter,
283 return; 307 return;
284 308
285 counter->state = PERF_COUNTER_STATE_INACTIVE; 309 counter->state = PERF_COUNTER_STATE_INACTIVE;
310 if (counter->pending_disable) {
311 counter->pending_disable = 0;
312 counter->state = PERF_COUNTER_STATE_OFF;
313 }
286 counter->tstamp_stopped = ctx->time; 314 counter->tstamp_stopped = ctx->time;
287 counter->pmu->disable(counter); 315 counter->pmu->disable(counter);
288 counter->oncpu = -1; 316 counter->oncpu = -1;
@@ -1081,7 +1109,7 @@ static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1081 __perf_counter_sync_stat(counter, next_counter); 1109 __perf_counter_sync_stat(counter, next_counter);
1082 1110
1083 counter = list_next_entry(counter, event_entry); 1111 counter = list_next_entry(counter, event_entry);
1084 next_counter = list_next_entry(counter, event_entry); 1112 next_counter = list_next_entry(next_counter, event_entry);
1085 } 1113 }
1086} 1114}
1087 1115
@@ -1288,7 +1316,6 @@ static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1288#define MAX_INTERRUPTS (~0ULL) 1316#define MAX_INTERRUPTS (~0ULL)
1289 1317
1290static void perf_log_throttle(struct perf_counter *counter, int enable); 1318static void perf_log_throttle(struct perf_counter *counter, int enable);
1291static void perf_log_period(struct perf_counter *counter, u64 period);
1292 1319
1293static void perf_adjust_period(struct perf_counter *counter, u64 events) 1320static void perf_adjust_period(struct perf_counter *counter, u64 events)
1294{ 1321{
@@ -1307,8 +1334,6 @@ static void perf_adjust_period(struct perf_counter *counter, u64 events)
1307 if (!sample_period) 1334 if (!sample_period)
1308 sample_period = 1; 1335 sample_period = 1;
1309 1336
1310 perf_log_period(counter, sample_period);
1311
1312 hwc->sample_period = sample_period; 1337 hwc->sample_period = sample_period;
1313} 1338}
1314 1339
@@ -1463,10 +1488,8 @@ static void perf_counter_enable_on_exec(struct task_struct *task)
1463 /* 1488 /*
1464 * Unclone this context if we enabled any counter. 1489 * Unclone this context if we enabled any counter.
1465 */ 1490 */
1466 if (enabled && ctx->parent_ctx) { 1491 if (enabled)
1467 put_ctx(ctx->parent_ctx); 1492 unclone_ctx(ctx);
1468 ctx->parent_ctx = NULL;
1469 }
1470 1493
1471 spin_unlock(&ctx->lock); 1494 spin_unlock(&ctx->lock);
1472 1495
@@ -1480,10 +1503,21 @@ static void perf_counter_enable_on_exec(struct task_struct *task)
1480 */ 1503 */
1481static void __perf_counter_read(void *info) 1504static void __perf_counter_read(void *info)
1482{ 1505{
1506 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1483 struct perf_counter *counter = info; 1507 struct perf_counter *counter = info;
1484 struct perf_counter_context *ctx = counter->ctx; 1508 struct perf_counter_context *ctx = counter->ctx;
1485 unsigned long flags; 1509 unsigned long flags;
1486 1510
1511 /*
1512 * If this is a task context, we need to check whether it is
1513 * the current task context of this cpu. If not it has been
1514 * scheduled out before the smp call arrived. In that case
1515 * counter->count would have been updated to a recent sample
1516 * when the counter was scheduled out.
1517 */
1518 if (ctx->task && cpuctx->task_ctx != ctx)
1519 return;
1520
1487 local_irq_save(flags); 1521 local_irq_save(flags);
1488 if (ctx->is_active) 1522 if (ctx->is_active)
1489 update_context_time(ctx); 1523 update_context_time(ctx);
@@ -1526,7 +1560,6 @@ __perf_counter_init_context(struct perf_counter_context *ctx,
1526 1560
1527static struct perf_counter_context *find_get_context(pid_t pid, int cpu) 1561static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1528{ 1562{
1529 struct perf_counter_context *parent_ctx;
1530 struct perf_counter_context *ctx; 1563 struct perf_counter_context *ctx;
1531 struct perf_cpu_context *cpuctx; 1564 struct perf_cpu_context *cpuctx;
1532 struct task_struct *task; 1565 struct task_struct *task;
@@ -1586,11 +1619,7 @@ static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1586 retry: 1619 retry:
1587 ctx = perf_lock_task_context(task, &flags); 1620 ctx = perf_lock_task_context(task, &flags);
1588 if (ctx) { 1621 if (ctx) {
1589 parent_ctx = ctx->parent_ctx; 1622 unclone_ctx(ctx);
1590 if (parent_ctx) {
1591 put_ctx(parent_ctx);
1592 ctx->parent_ctx = NULL; /* no longer a clone */
1593 }
1594 spin_unlock_irqrestore(&ctx->lock, flags); 1623 spin_unlock_irqrestore(&ctx->lock, flags);
1595 } 1624 }
1596 1625
@@ -1642,6 +1671,8 @@ static void free_counter(struct perf_counter *counter)
1642 atomic_dec(&nr_mmap_counters); 1671 atomic_dec(&nr_mmap_counters);
1643 if (counter->attr.comm) 1672 if (counter->attr.comm)
1644 atomic_dec(&nr_comm_counters); 1673 atomic_dec(&nr_comm_counters);
1674 if (counter->attr.task)
1675 atomic_dec(&nr_task_counters);
1645 } 1676 }
1646 1677
1647 if (counter->destroy) 1678 if (counter->destroy)
@@ -1676,14 +1707,133 @@ static int perf_release(struct inode *inode, struct file *file)
1676 return 0; 1707 return 0;
1677} 1708}
1678 1709
1710static int perf_counter_read_size(struct perf_counter *counter)
1711{
1712 int entry = sizeof(u64); /* value */
1713 int size = 0;
1714 int nr = 1;
1715
1716 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1717 size += sizeof(u64);
1718
1719 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1720 size += sizeof(u64);
1721
1722 if (counter->attr.read_format & PERF_FORMAT_ID)
1723 entry += sizeof(u64);
1724
1725 if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1726 nr += counter->group_leader->nr_siblings;
1727 size += sizeof(u64);
1728 }
1729
1730 size += entry * nr;
1731
1732 return size;
1733}
1734
1735static u64 perf_counter_read_value(struct perf_counter *counter)
1736{
1737 struct perf_counter *child;
1738 u64 total = 0;
1739
1740 total += perf_counter_read(counter);
1741 list_for_each_entry(child, &counter->child_list, child_list)
1742 total += perf_counter_read(child);
1743
1744 return total;
1745}
1746
1747static int perf_counter_read_entry(struct perf_counter *counter,
1748 u64 read_format, char __user *buf)
1749{
1750 int n = 0, count = 0;
1751 u64 values[2];
1752
1753 values[n++] = perf_counter_read_value(counter);
1754 if (read_format & PERF_FORMAT_ID)
1755 values[n++] = primary_counter_id(counter);
1756
1757 count = n * sizeof(u64);
1758
1759 if (copy_to_user(buf, values, count))
1760 return -EFAULT;
1761
1762 return count;
1763}
1764
1765static int perf_counter_read_group(struct perf_counter *counter,
1766 u64 read_format, char __user *buf)
1767{
1768 struct perf_counter *leader = counter->group_leader, *sub;
1769 int n = 0, size = 0, err = -EFAULT;
1770 u64 values[3];
1771
1772 values[n++] = 1 + leader->nr_siblings;
1773 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1774 values[n++] = leader->total_time_enabled +
1775 atomic64_read(&leader->child_total_time_enabled);
1776 }
1777 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1778 values[n++] = leader->total_time_running +
1779 atomic64_read(&leader->child_total_time_running);
1780 }
1781
1782 size = n * sizeof(u64);
1783
1784 if (copy_to_user(buf, values, size))
1785 return -EFAULT;
1786
1787 err = perf_counter_read_entry(leader, read_format, buf + size);
1788 if (err < 0)
1789 return err;
1790
1791 size += err;
1792
1793 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1794 err = perf_counter_read_entry(sub, read_format,
1795 buf + size);
1796 if (err < 0)
1797 return err;
1798
1799 size += err;
1800 }
1801
1802 return size;
1803}
1804
1805static int perf_counter_read_one(struct perf_counter *counter,
1806 u64 read_format, char __user *buf)
1807{
1808 u64 values[4];
1809 int n = 0;
1810
1811 values[n++] = perf_counter_read_value(counter);
1812 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1813 values[n++] = counter->total_time_enabled +
1814 atomic64_read(&counter->child_total_time_enabled);
1815 }
1816 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1817 values[n++] = counter->total_time_running +
1818 atomic64_read(&counter->child_total_time_running);
1819 }
1820 if (read_format & PERF_FORMAT_ID)
1821 values[n++] = primary_counter_id(counter);
1822
1823 if (copy_to_user(buf, values, n * sizeof(u64)))
1824 return -EFAULT;
1825
1826 return n * sizeof(u64);
1827}
1828
1679/* 1829/*
1680 * Read the performance counter - simple non blocking version for now 1830 * Read the performance counter - simple non blocking version for now
1681 */ 1831 */
1682static ssize_t 1832static ssize_t
1683perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) 1833perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1684{ 1834{
1685 u64 values[4]; 1835 u64 read_format = counter->attr.read_format;
1686 int n; 1836 int ret;
1687 1837
1688 /* 1838 /*
1689 * Return end-of-file for a read on a counter that is in 1839 * Return end-of-file for a read on a counter that is in
@@ -1693,28 +1843,18 @@ perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1693 if (counter->state == PERF_COUNTER_STATE_ERROR) 1843 if (counter->state == PERF_COUNTER_STATE_ERROR)
1694 return 0; 1844 return 0;
1695 1845
1846 if (count < perf_counter_read_size(counter))
1847 return -ENOSPC;
1848
1696 WARN_ON_ONCE(counter->ctx->parent_ctx); 1849 WARN_ON_ONCE(counter->ctx->parent_ctx);
1697 mutex_lock(&counter->child_mutex); 1850 mutex_lock(&counter->child_mutex);
1698 values[0] = perf_counter_read(counter); 1851 if (read_format & PERF_FORMAT_GROUP)
1699 n = 1; 1852 ret = perf_counter_read_group(counter, read_format, buf);
1700 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1853 else
1701 values[n++] = counter->total_time_enabled + 1854 ret = perf_counter_read_one(counter, read_format, buf);
1702 atomic64_read(&counter->child_total_time_enabled);
1703 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1704 values[n++] = counter->total_time_running +
1705 atomic64_read(&counter->child_total_time_running);
1706 if (counter->attr.read_format & PERF_FORMAT_ID)
1707 values[n++] = counter->id;
1708 mutex_unlock(&counter->child_mutex); 1855 mutex_unlock(&counter->child_mutex);
1709 1856
1710 if (count < n * sizeof(u64)) 1857 return ret;
1711 return -EINVAL;
1712 count = n * sizeof(u64);
1713
1714 if (copy_to_user(buf, values, count))
1715 return -EFAULT;
1716
1717 return count;
1718} 1858}
1719 1859
1720static ssize_t 1860static ssize_t
@@ -1811,8 +1951,6 @@ static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1811 1951
1812 counter->attr.sample_freq = value; 1952 counter->attr.sample_freq = value;
1813 } else { 1953 } else {
1814 perf_log_period(counter, value);
1815
1816 counter->attr.sample_period = value; 1954 counter->attr.sample_period = value;
1817 counter->hw.sample_period = value; 1955 counter->hw.sample_period = value;
1818 } 1956 }
@@ -1881,6 +2019,10 @@ int perf_counter_task_disable(void)
1881 return 0; 2019 return 0;
1882} 2020}
1883 2021
2022#ifndef PERF_COUNTER_INDEX_OFFSET
2023# define PERF_COUNTER_INDEX_OFFSET 0
2024#endif
2025
1884static int perf_counter_index(struct perf_counter *counter) 2026static int perf_counter_index(struct perf_counter *counter)
1885{ 2027{
1886 if (counter->state != PERF_COUNTER_STATE_ACTIVE) 2028 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
@@ -2220,7 +2362,7 @@ static void perf_pending_counter(struct perf_pending_entry *entry)
2220 2362
2221 if (counter->pending_disable) { 2363 if (counter->pending_disable) {
2222 counter->pending_disable = 0; 2364 counter->pending_disable = 0;
2223 perf_counter_disable(counter); 2365 __perf_counter_disable(counter);
2224 } 2366 }
2225 2367
2226 if (counter->pending_wakeup) { 2368 if (counter->pending_wakeup) {
@@ -2605,7 +2747,80 @@ static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2605 return task_pid_nr_ns(p, counter->ns); 2747 return task_pid_nr_ns(p, counter->ns);
2606} 2748}
2607 2749
2608static void perf_counter_output(struct perf_counter *counter, int nmi, 2750static void perf_output_read_one(struct perf_output_handle *handle,
2751 struct perf_counter *counter)
2752{
2753 u64 read_format = counter->attr.read_format;
2754 u64 values[4];
2755 int n = 0;
2756
2757 values[n++] = atomic64_read(&counter->count);
2758 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2759 values[n++] = counter->total_time_enabled +
2760 atomic64_read(&counter->child_total_time_enabled);
2761 }
2762 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2763 values[n++] = counter->total_time_running +
2764 atomic64_read(&counter->child_total_time_running);
2765 }
2766 if (read_format & PERF_FORMAT_ID)
2767 values[n++] = primary_counter_id(counter);
2768
2769 perf_output_copy(handle, values, n * sizeof(u64));
2770}
2771
2772/*
2773 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2774 */
2775static void perf_output_read_group(struct perf_output_handle *handle,
2776 struct perf_counter *counter)
2777{
2778 struct perf_counter *leader = counter->group_leader, *sub;
2779 u64 read_format = counter->attr.read_format;
2780 u64 values[5];
2781 int n = 0;
2782
2783 values[n++] = 1 + leader->nr_siblings;
2784
2785 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2786 values[n++] = leader->total_time_enabled;
2787
2788 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2789 values[n++] = leader->total_time_running;
2790
2791 if (leader != counter)
2792 leader->pmu->read(leader);
2793
2794 values[n++] = atomic64_read(&leader->count);
2795 if (read_format & PERF_FORMAT_ID)
2796 values[n++] = primary_counter_id(leader);
2797
2798 perf_output_copy(handle, values, n * sizeof(u64));
2799
2800 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2801 n = 0;
2802
2803 if (sub != counter)
2804 sub->pmu->read(sub);
2805
2806 values[n++] = atomic64_read(&sub->count);
2807 if (read_format & PERF_FORMAT_ID)
2808 values[n++] = primary_counter_id(sub);
2809
2810 perf_output_copy(handle, values, n * sizeof(u64));
2811 }
2812}
2813
2814static void perf_output_read(struct perf_output_handle *handle,
2815 struct perf_counter *counter)
2816{
2817 if (counter->attr.read_format & PERF_FORMAT_GROUP)
2818 perf_output_read_group(handle, counter);
2819 else
2820 perf_output_read_one(handle, counter);
2821}
2822
2823void perf_counter_output(struct perf_counter *counter, int nmi,
2609 struct perf_sample_data *data) 2824 struct perf_sample_data *data)
2610{ 2825{
2611 int ret; 2826 int ret;
@@ -2616,10 +2831,6 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2616 struct { 2831 struct {
2617 u32 pid, tid; 2832 u32 pid, tid;
2618 } tid_entry; 2833 } tid_entry;
2619 struct {
2620 u64 id;
2621 u64 counter;
2622 } group_entry;
2623 struct perf_callchain_entry *callchain = NULL; 2834 struct perf_callchain_entry *callchain = NULL;
2624 int callchain_size = 0; 2835 int callchain_size = 0;
2625 u64 time; 2836 u64 time;
@@ -2661,19 +2872,21 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2661 if (sample_type & PERF_SAMPLE_ID) 2872 if (sample_type & PERF_SAMPLE_ID)
2662 header.size += sizeof(u64); 2873 header.size += sizeof(u64);
2663 2874
2875 if (sample_type & PERF_SAMPLE_STREAM_ID)
2876 header.size += sizeof(u64);
2877
2664 if (sample_type & PERF_SAMPLE_CPU) { 2878 if (sample_type & PERF_SAMPLE_CPU) {
2665 header.size += sizeof(cpu_entry); 2879 header.size += sizeof(cpu_entry);
2666 2880
2667 cpu_entry.cpu = raw_smp_processor_id(); 2881 cpu_entry.cpu = raw_smp_processor_id();
2882 cpu_entry.reserved = 0;
2668 } 2883 }
2669 2884
2670 if (sample_type & PERF_SAMPLE_PERIOD) 2885 if (sample_type & PERF_SAMPLE_PERIOD)
2671 header.size += sizeof(u64); 2886 header.size += sizeof(u64);
2672 2887
2673 if (sample_type & PERF_SAMPLE_GROUP) { 2888 if (sample_type & PERF_SAMPLE_READ)
2674 header.size += sizeof(u64) + 2889 header.size += perf_counter_read_size(counter);
2675 counter->nr_siblings * sizeof(group_entry);
2676 }
2677 2890
2678 if (sample_type & PERF_SAMPLE_CALLCHAIN) { 2891 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2679 callchain = perf_callchain(data->regs); 2892 callchain = perf_callchain(data->regs);
@@ -2685,6 +2898,18 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2685 header.size += sizeof(u64); 2898 header.size += sizeof(u64);
2686 } 2899 }
2687 2900
2901 if (sample_type & PERF_SAMPLE_RAW) {
2902 int size = sizeof(u32);
2903
2904 if (data->raw)
2905 size += data->raw->size;
2906 else
2907 size += sizeof(u32);
2908
2909 WARN_ON_ONCE(size & (sizeof(u64)-1));
2910 header.size += size;
2911 }
2912
2688 ret = perf_output_begin(&handle, counter, header.size, nmi, 1); 2913 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2689 if (ret) 2914 if (ret)
2690 return; 2915 return;
@@ -2703,7 +2928,13 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2703 if (sample_type & PERF_SAMPLE_ADDR) 2928 if (sample_type & PERF_SAMPLE_ADDR)
2704 perf_output_put(&handle, data->addr); 2929 perf_output_put(&handle, data->addr);
2705 2930
2706 if (sample_type & PERF_SAMPLE_ID) 2931 if (sample_type & PERF_SAMPLE_ID) {
2932 u64 id = primary_counter_id(counter);
2933
2934 perf_output_put(&handle, id);
2935 }
2936
2937 if (sample_type & PERF_SAMPLE_STREAM_ID)
2707 perf_output_put(&handle, counter->id); 2938 perf_output_put(&handle, counter->id);
2708 2939
2709 if (sample_type & PERF_SAMPLE_CPU) 2940 if (sample_type & PERF_SAMPLE_CPU)
@@ -2712,26 +2943,8 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2712 if (sample_type & PERF_SAMPLE_PERIOD) 2943 if (sample_type & PERF_SAMPLE_PERIOD)
2713 perf_output_put(&handle, data->period); 2944 perf_output_put(&handle, data->period);
2714 2945
2715 /* 2946 if (sample_type & PERF_SAMPLE_READ)
2716 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult. 2947 perf_output_read(&handle, counter);
2717 */
2718 if (sample_type & PERF_SAMPLE_GROUP) {
2719 struct perf_counter *leader, *sub;
2720 u64 nr = counter->nr_siblings;
2721
2722 perf_output_put(&handle, nr);
2723
2724 leader = counter->group_leader;
2725 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2726 if (sub != counter)
2727 sub->pmu->read(sub);
2728
2729 group_entry.id = sub->id;
2730 group_entry.counter = atomic64_read(&sub->count);
2731
2732 perf_output_put(&handle, group_entry);
2733 }
2734 }
2735 2948
2736 if (sample_type & PERF_SAMPLE_CALLCHAIN) { 2949 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2737 if (callchain) 2950 if (callchain)
@@ -2742,6 +2955,22 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
2742 } 2955 }
2743 } 2956 }
2744 2957
2958 if (sample_type & PERF_SAMPLE_RAW) {
2959 if (data->raw) {
2960 perf_output_put(&handle, data->raw->size);
2961 perf_output_copy(&handle, data->raw->data, data->raw->size);
2962 } else {
2963 struct {
2964 u32 size;
2965 u32 data;
2966 } raw = {
2967 .size = sizeof(u32),
2968 .data = 0,
2969 };
2970 perf_output_put(&handle, raw);
2971 }
2972 }
2973
2745 perf_output_end(&handle); 2974 perf_output_end(&handle);
2746} 2975}
2747 2976
@@ -2754,8 +2983,6 @@ struct perf_read_event {
2754 2983
2755 u32 pid; 2984 u32 pid;
2756 u32 tid; 2985 u32 tid;
2757 u64 value;
2758 u64 format[3];
2759}; 2986};
2760 2987
2761static void 2988static void
@@ -2767,87 +2994,74 @@ perf_counter_read_event(struct perf_counter *counter,
2767 .header = { 2994 .header = {
2768 .type = PERF_EVENT_READ, 2995 .type = PERF_EVENT_READ,
2769 .misc = 0, 2996 .misc = 0,
2770 .size = sizeof(event) - sizeof(event.format), 2997 .size = sizeof(event) + perf_counter_read_size(counter),
2771 }, 2998 },
2772 .pid = perf_counter_pid(counter, task), 2999 .pid = perf_counter_pid(counter, task),
2773 .tid = perf_counter_tid(counter, task), 3000 .tid = perf_counter_tid(counter, task),
2774 .value = atomic64_read(&counter->count),
2775 }; 3001 };
2776 int ret, i = 0; 3002 int ret;
2777
2778 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2779 event.header.size += sizeof(u64);
2780 event.format[i++] = counter->total_time_enabled;
2781 }
2782
2783 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2784 event.header.size += sizeof(u64);
2785 event.format[i++] = counter->total_time_running;
2786 }
2787
2788 if (counter->attr.read_format & PERF_FORMAT_ID) {
2789 u64 id;
2790
2791 event.header.size += sizeof(u64);
2792 if (counter->parent)
2793 id = counter->parent->id;
2794 else
2795 id = counter->id;
2796
2797 event.format[i++] = id;
2798 }
2799 3003
2800 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0); 3004 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2801 if (ret) 3005 if (ret)
2802 return; 3006 return;
2803 3007
2804 perf_output_copy(&handle, &event, event.header.size); 3008 perf_output_put(&handle, event);
3009 perf_output_read(&handle, counter);
3010
2805 perf_output_end(&handle); 3011 perf_output_end(&handle);
2806} 3012}
2807 3013
2808/* 3014/*
2809 * fork tracking 3015 * task tracking -- fork/exit
3016 *
3017 * enabled by: attr.comm | attr.mmap | attr.task
2810 */ 3018 */
2811 3019
2812struct perf_fork_event { 3020struct perf_task_event {
2813 struct task_struct *task; 3021 struct task_struct *task;
3022 struct perf_counter_context *task_ctx;
2814 3023
2815 struct { 3024 struct {
2816 struct perf_event_header header; 3025 struct perf_event_header header;
2817 3026
2818 u32 pid; 3027 u32 pid;
2819 u32 ppid; 3028 u32 ppid;
3029 u32 tid;
3030 u32 ptid;
2820 } event; 3031 } event;
2821}; 3032};
2822 3033
2823static void perf_counter_fork_output(struct perf_counter *counter, 3034static void perf_counter_task_output(struct perf_counter *counter,
2824 struct perf_fork_event *fork_event) 3035 struct perf_task_event *task_event)
2825{ 3036{
2826 struct perf_output_handle handle; 3037 struct perf_output_handle handle;
2827 int size = fork_event->event.header.size; 3038 int size = task_event->event.header.size;
2828 struct task_struct *task = fork_event->task; 3039 struct task_struct *task = task_event->task;
2829 int ret = perf_output_begin(&handle, counter, size, 0, 0); 3040 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2830 3041
2831 if (ret) 3042 if (ret)
2832 return; 3043 return;
2833 3044
2834 fork_event->event.pid = perf_counter_pid(counter, task); 3045 task_event->event.pid = perf_counter_pid(counter, task);
2835 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent); 3046 task_event->event.ppid = perf_counter_pid(counter, current);
2836 3047
2837 perf_output_put(&handle, fork_event->event); 3048 task_event->event.tid = perf_counter_tid(counter, task);
3049 task_event->event.ptid = perf_counter_tid(counter, current);
3050
3051 perf_output_put(&handle, task_event->event);
2838 perf_output_end(&handle); 3052 perf_output_end(&handle);
2839} 3053}
2840 3054
2841static int perf_counter_fork_match(struct perf_counter *counter) 3055static int perf_counter_task_match(struct perf_counter *counter)
2842{ 3056{
2843 if (counter->attr.comm || counter->attr.mmap) 3057 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
2844 return 1; 3058 return 1;
2845 3059
2846 return 0; 3060 return 0;
2847} 3061}
2848 3062
2849static void perf_counter_fork_ctx(struct perf_counter_context *ctx, 3063static void perf_counter_task_ctx(struct perf_counter_context *ctx,
2850 struct perf_fork_event *fork_event) 3064 struct perf_task_event *task_event)
2851{ 3065{
2852 struct perf_counter *counter; 3066 struct perf_counter *counter;
2853 3067
@@ -2856,51 +3070,62 @@ static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2856 3070
2857 rcu_read_lock(); 3071 rcu_read_lock();
2858 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { 3072 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2859 if (perf_counter_fork_match(counter)) 3073 if (perf_counter_task_match(counter))
2860 perf_counter_fork_output(counter, fork_event); 3074 perf_counter_task_output(counter, task_event);
2861 } 3075 }
2862 rcu_read_unlock(); 3076 rcu_read_unlock();
2863} 3077}
2864 3078
2865static void perf_counter_fork_event(struct perf_fork_event *fork_event) 3079static void perf_counter_task_event(struct perf_task_event *task_event)
2866{ 3080{
2867 struct perf_cpu_context *cpuctx; 3081 struct perf_cpu_context *cpuctx;
2868 struct perf_counter_context *ctx; 3082 struct perf_counter_context *ctx = task_event->task_ctx;
2869 3083
2870 cpuctx = &get_cpu_var(perf_cpu_context); 3084 cpuctx = &get_cpu_var(perf_cpu_context);
2871 perf_counter_fork_ctx(&cpuctx->ctx, fork_event); 3085 perf_counter_task_ctx(&cpuctx->ctx, task_event);
2872 put_cpu_var(perf_cpu_context); 3086 put_cpu_var(perf_cpu_context);
2873 3087
2874 rcu_read_lock(); 3088 rcu_read_lock();
2875 /* 3089 if (!ctx)
2876 * doesn't really matter which of the child contexts the 3090 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
2877 * events ends up in.
2878 */
2879 ctx = rcu_dereference(current->perf_counter_ctxp);
2880 if (ctx) 3091 if (ctx)
2881 perf_counter_fork_ctx(ctx, fork_event); 3092 perf_counter_task_ctx(ctx, task_event);
2882 rcu_read_unlock(); 3093 rcu_read_unlock();
2883} 3094}
2884 3095
2885void perf_counter_fork(struct task_struct *task) 3096static void perf_counter_task(struct task_struct *task,
3097 struct perf_counter_context *task_ctx,
3098 int new)
2886{ 3099{
2887 struct perf_fork_event fork_event; 3100 struct perf_task_event task_event;
2888 3101
2889 if (!atomic_read(&nr_comm_counters) && 3102 if (!atomic_read(&nr_comm_counters) &&
2890 !atomic_read(&nr_mmap_counters)) 3103 !atomic_read(&nr_mmap_counters) &&
3104 !atomic_read(&nr_task_counters))
2891 return; 3105 return;
2892 3106
2893 fork_event = (struct perf_fork_event){ 3107 task_event = (struct perf_task_event){
2894 .task = task, 3108 .task = task,
2895 .event = { 3109 .task_ctx = task_ctx,
3110 .event = {
2896 .header = { 3111 .header = {
2897 .type = PERF_EVENT_FORK, 3112 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
2898 .size = sizeof(fork_event.event), 3113 .misc = 0,
3114 .size = sizeof(task_event.event),
2899 }, 3115 },
3116 /* .pid */
3117 /* .ppid */
3118 /* .tid */
3119 /* .ptid */
2900 }, 3120 },
2901 }; 3121 };
2902 3122
2903 perf_counter_fork_event(&fork_event); 3123 perf_counter_task_event(&task_event);
3124}
3125
3126void perf_counter_fork(struct task_struct *task)
3127{
3128 perf_counter_task(task, NULL, 1);
2904} 3129}
2905 3130
2906/* 3131/*
@@ -2968,8 +3193,10 @@ static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2968 struct perf_cpu_context *cpuctx; 3193 struct perf_cpu_context *cpuctx;
2969 struct perf_counter_context *ctx; 3194 struct perf_counter_context *ctx;
2970 unsigned int size; 3195 unsigned int size;
2971 char *comm = comm_event->task->comm; 3196 char comm[TASK_COMM_LEN];
2972 3197
3198 memset(comm, 0, sizeof(comm));
3199 strncpy(comm, comm_event->task->comm, sizeof(comm));
2973 size = ALIGN(strlen(comm)+1, sizeof(u64)); 3200 size = ALIGN(strlen(comm)+1, sizeof(u64));
2974 3201
2975 comm_event->comm = comm; 3202 comm_event->comm = comm;
@@ -3004,8 +3231,16 @@ void perf_counter_comm(struct task_struct *task)
3004 3231
3005 comm_event = (struct perf_comm_event){ 3232 comm_event = (struct perf_comm_event){
3006 .task = task, 3233 .task = task,
3234 /* .comm */
3235 /* .comm_size */
3007 .event = { 3236 .event = {
3008 .header = { .type = PERF_EVENT_COMM, }, 3237 .header = {
3238 .type = PERF_EVENT_COMM,
3239 .misc = 0,
3240 /* .size */
3241 },
3242 /* .pid */
3243 /* .tid */
3009 }, 3244 },
3010 }; 3245 };
3011 3246
@@ -3088,8 +3323,15 @@ static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3088 char *buf = NULL; 3323 char *buf = NULL;
3089 const char *name; 3324 const char *name;
3090 3325
3326 memset(tmp, 0, sizeof(tmp));
3327
3091 if (file) { 3328 if (file) {
3092 buf = kzalloc(PATH_MAX, GFP_KERNEL); 3329 /*
3330 * d_path works from the end of the buffer backwards, so we
3331 * need to add enough zero bytes after the string to handle
3332 * the 64bit alignment we do later.
3333 */
3334 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3093 if (!buf) { 3335 if (!buf) {
3094 name = strncpy(tmp, "//enomem", sizeof(tmp)); 3336 name = strncpy(tmp, "//enomem", sizeof(tmp));
3095 goto got_name; 3337 goto got_name;
@@ -3100,9 +3342,11 @@ static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3100 goto got_name; 3342 goto got_name;
3101 } 3343 }
3102 } else { 3344 } else {
3103 name = arch_vma_name(mmap_event->vma); 3345 if (arch_vma_name(mmap_event->vma)) {
3104 if (name) 3346 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3347 sizeof(tmp));
3105 goto got_name; 3348 goto got_name;
3349 }
3106 3350
3107 if (!vma->vm_mm) { 3351 if (!vma->vm_mm) {
3108 name = strncpy(tmp, "[vdso]", sizeof(tmp)); 3352 name = strncpy(tmp, "[vdso]", sizeof(tmp));
@@ -3147,8 +3391,16 @@ void __perf_counter_mmap(struct vm_area_struct *vma)
3147 3391
3148 mmap_event = (struct perf_mmap_event){ 3392 mmap_event = (struct perf_mmap_event){
3149 .vma = vma, 3393 .vma = vma,
3394 /* .file_name */
3395 /* .file_size */
3150 .event = { 3396 .event = {
3151 .header = { .type = PERF_EVENT_MMAP, }, 3397 .header = {
3398 .type = PERF_EVENT_MMAP,
3399 .misc = 0,
3400 /* .size */
3401 },
3402 /* .pid */
3403 /* .tid */
3152 .start = vma->vm_start, 3404 .start = vma->vm_start,
3153 .len = vma->vm_end - vma->vm_start, 3405 .len = vma->vm_end - vma->vm_start,
3154 .pgoff = vma->vm_pgoff, 3406 .pgoff = vma->vm_pgoff,
@@ -3159,49 +3411,6 @@ void __perf_counter_mmap(struct vm_area_struct *vma)
3159} 3411}
3160 3412
3161/* 3413/*
3162 * Log sample_period changes so that analyzing tools can re-normalize the
3163 * event flow.
3164 */
3165
3166struct freq_event {
3167 struct perf_event_header header;
3168 u64 time;
3169 u64 id;
3170 u64 period;
3171};
3172
3173static void perf_log_period(struct perf_counter *counter, u64 period)
3174{
3175 struct perf_output_handle handle;
3176 struct freq_event event;
3177 int ret;
3178
3179 if (counter->hw.sample_period == period)
3180 return;
3181
3182 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
3183 return;
3184
3185 event = (struct freq_event) {
3186 .header = {
3187 .type = PERF_EVENT_PERIOD,
3188 .misc = 0,
3189 .size = sizeof(event),
3190 },
3191 .time = sched_clock(),
3192 .id = counter->id,
3193 .period = period,
3194 };
3195
3196 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
3197 if (ret)
3198 return;
3199
3200 perf_output_put(&handle, event);
3201 perf_output_end(&handle);
3202}
3203
3204/*
3205 * IRQ throttle logging 3414 * IRQ throttle logging
3206 */ 3415 */
3207 3416
@@ -3214,16 +3423,21 @@ static void perf_log_throttle(struct perf_counter *counter, int enable)
3214 struct perf_event_header header; 3423 struct perf_event_header header;
3215 u64 time; 3424 u64 time;
3216 u64 id; 3425 u64 id;
3426 u64 stream_id;
3217 } throttle_event = { 3427 } throttle_event = {
3218 .header = { 3428 .header = {
3219 .type = PERF_EVENT_THROTTLE + 1, 3429 .type = PERF_EVENT_THROTTLE,
3220 .misc = 0, 3430 .misc = 0,
3221 .size = sizeof(throttle_event), 3431 .size = sizeof(throttle_event),
3222 }, 3432 },
3223 .time = sched_clock(), 3433 .time = sched_clock(),
3224 .id = counter->id, 3434 .id = primary_counter_id(counter),
3435 .stream_id = counter->id,
3225 }; 3436 };
3226 3437
3438 if (enable)
3439 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3440
3227 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0); 3441 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
3228 if (ret) 3442 if (ret)
3229 return; 3443 return;
@@ -3300,125 +3514,111 @@ int perf_counter_overflow(struct perf_counter *counter, int nmi,
3300 * Generic software counter infrastructure 3514 * Generic software counter infrastructure
3301 */ 3515 */
3302 3516
3303static void perf_swcounter_update(struct perf_counter *counter) 3517/*
3518 * We directly increment counter->count and keep a second value in
3519 * counter->hw.period_left to count intervals. This period counter
3520 * is kept in the range [-sample_period, 0] so that we can use the
3521 * sign as trigger.
3522 */
3523
3524static u64 perf_swcounter_set_period(struct perf_counter *counter)
3304{ 3525{
3305 struct hw_perf_counter *hwc = &counter->hw; 3526 struct hw_perf_counter *hwc = &counter->hw;
3306 u64 prev, now; 3527 u64 period = hwc->last_period;
3307 s64 delta; 3528 u64 nr, offset;
3529 s64 old, val;
3530
3531 hwc->last_period = hwc->sample_period;
3308 3532
3309again: 3533again:
3310 prev = atomic64_read(&hwc->prev_count); 3534 old = val = atomic64_read(&hwc->period_left);
3311 now = atomic64_read(&hwc->count); 3535 if (val < 0)
3312 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev) 3536 return 0;
3313 goto again;
3314 3537
3315 delta = now - prev; 3538 nr = div64_u64(period + val, period);
3539 offset = nr * period;
3540 val -= offset;
3541 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3542 goto again;
3316 3543
3317 atomic64_add(delta, &counter->count); 3544 return nr;
3318 atomic64_sub(delta, &hwc->period_left);
3319} 3545}
3320 3546
3321static void perf_swcounter_set_period(struct perf_counter *counter) 3547static void perf_swcounter_overflow(struct perf_counter *counter,
3548 int nmi, struct perf_sample_data *data)
3322{ 3549{
3323 struct hw_perf_counter *hwc = &counter->hw; 3550 struct hw_perf_counter *hwc = &counter->hw;
3324 s64 left = atomic64_read(&hwc->period_left); 3551 u64 overflow;
3325 s64 period = hwc->sample_period;
3326 3552
3327 if (unlikely(left <= -period)) { 3553 data->period = counter->hw.last_period;
3328 left = period; 3554 overflow = perf_swcounter_set_period(counter);
3329 atomic64_set(&hwc->period_left, left);
3330 hwc->last_period = period;
3331 }
3332 3555
3333 if (unlikely(left <= 0)) { 3556 if (hwc->interrupts == MAX_INTERRUPTS)
3334 left += period; 3557 return;
3335 atomic64_add(period, &hwc->period_left);
3336 hwc->last_period = period;
3337 }
3338 3558
3339 atomic64_set(&hwc->prev_count, -left); 3559 for (; overflow; overflow--) {
3340 atomic64_set(&hwc->count, -left); 3560 if (perf_counter_overflow(counter, nmi, data)) {
3561 /*
3562 * We inhibit the overflow from happening when
3563 * hwc->interrupts == MAX_INTERRUPTS.
3564 */
3565 break;
3566 }
3567 }
3341} 3568}
3342 3569
3343static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer) 3570static void perf_swcounter_unthrottle(struct perf_counter *counter)
3344{ 3571{
3345 enum hrtimer_restart ret = HRTIMER_RESTART;
3346 struct perf_sample_data data;
3347 struct perf_counter *counter;
3348 u64 period;
3349
3350 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3351 counter->pmu->read(counter);
3352
3353 data.addr = 0;
3354 data.regs = get_irq_regs();
3355 /* 3572 /*
3356 * In case we exclude kernel IPs or are somehow not in interrupt 3573 * Nothing to do, we already reset hwc->interrupts.
3357 * context, provide the next best thing, the user IP.
3358 */ 3574 */
3359 if ((counter->attr.exclude_kernel || !data.regs) && 3575}
3360 !counter->attr.exclude_user)
3361 data.regs = task_pt_regs(current);
3362 3576
3363 if (data.regs) { 3577static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3364 if (perf_counter_overflow(counter, 0, &data)) 3578 int nmi, struct perf_sample_data *data)
3365 ret = HRTIMER_NORESTART; 3579{
3366 } 3580 struct hw_perf_counter *hwc = &counter->hw;
3367 3581
3368 period = max_t(u64, 10000, counter->hw.sample_period); 3582 atomic64_add(nr, &counter->count);
3369 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3370 3583
3371 return ret; 3584 if (!hwc->sample_period)
3372} 3585 return;
3373 3586
3374static void perf_swcounter_overflow(struct perf_counter *counter, 3587 if (!data->regs)
3375 int nmi, struct perf_sample_data *data) 3588 return;
3376{
3377 data->period = counter->hw.last_period;
3378 3589
3379 perf_swcounter_update(counter); 3590 if (!atomic64_add_negative(nr, &hwc->period_left))
3380 perf_swcounter_set_period(counter); 3591 perf_swcounter_overflow(counter, nmi, data);
3381 if (perf_counter_overflow(counter, nmi, data))
3382 /* soft-disable the counter */
3383 ;
3384} 3592}
3385 3593
3386static int perf_swcounter_is_counting(struct perf_counter *counter) 3594static int perf_swcounter_is_counting(struct perf_counter *counter)
3387{ 3595{
3388 struct perf_counter_context *ctx; 3596 /*
3389 unsigned long flags; 3597 * The counter is active, we're good!
3390 int count; 3598 */
3391
3392 if (counter->state == PERF_COUNTER_STATE_ACTIVE) 3599 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3393 return 1; 3600 return 1;
3394 3601
3602 /*
3603 * The counter is off/error, not counting.
3604 */
3395 if (counter->state != PERF_COUNTER_STATE_INACTIVE) 3605 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3396 return 0; 3606 return 0;
3397 3607
3398 /* 3608 /*
3399 * If the counter is inactive, it could be just because 3609 * The counter is inactive, if the context is active
3400 * its task is scheduled out, or because it's in a group 3610 * we're part of a group that didn't make it on the 'pmu',
3401 * which could not go on the PMU. We want to count in 3611 * not counting.
3402 * the first case but not the second. If the context is
3403 * currently active then an inactive software counter must
3404 * be the second case. If it's not currently active then
3405 * we need to know whether the counter was active when the
3406 * context was last active, which we can determine by
3407 * comparing counter->tstamp_stopped with ctx->time.
3408 *
3409 * We are within an RCU read-side critical section,
3410 * which protects the existence of *ctx.
3411 */ 3612 */
3412 ctx = counter->ctx; 3613 if (counter->ctx->is_active)
3413 spin_lock_irqsave(&ctx->lock, flags); 3614 return 0;
3414 count = 1; 3615
3415 /* Re-check state now we have the lock */ 3616 /*
3416 if (counter->state < PERF_COUNTER_STATE_INACTIVE || 3617 * We're inactive and the context is too, this means the
3417 counter->ctx->is_active || 3618 * task is scheduled out, we're counting events that happen
3418 counter->tstamp_stopped < ctx->time) 3619 * to us, like migration events.
3419 count = 0; 3620 */
3420 spin_unlock_irqrestore(&ctx->lock, flags); 3621 return 1;
3421 return count;
3422} 3622}
3423 3623
3424static int perf_swcounter_match(struct perf_counter *counter, 3624static int perf_swcounter_match(struct perf_counter *counter,
@@ -3444,15 +3644,6 @@ static int perf_swcounter_match(struct perf_counter *counter,
3444 return 1; 3644 return 1;
3445} 3645}
3446 3646
3447static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3448 int nmi, struct perf_sample_data *data)
3449{
3450 int neg = atomic64_add_negative(nr, &counter->hw.count);
3451
3452 if (counter->hw.sample_period && !neg && data->regs)
3453 perf_swcounter_overflow(counter, nmi, data);
3454}
3455
3456static void perf_swcounter_ctx_event(struct perf_counter_context *ctx, 3647static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3457 enum perf_type_id type, 3648 enum perf_type_id type,
3458 u32 event, u64 nr, int nmi, 3649 u32 event, u64 nr, int nmi,
@@ -3531,27 +3722,66 @@ void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3531 3722
3532static void perf_swcounter_read(struct perf_counter *counter) 3723static void perf_swcounter_read(struct perf_counter *counter)
3533{ 3724{
3534 perf_swcounter_update(counter);
3535} 3725}
3536 3726
3537static int perf_swcounter_enable(struct perf_counter *counter) 3727static int perf_swcounter_enable(struct perf_counter *counter)
3538{ 3728{
3539 perf_swcounter_set_period(counter); 3729 struct hw_perf_counter *hwc = &counter->hw;
3730
3731 if (hwc->sample_period) {
3732 hwc->last_period = hwc->sample_period;
3733 perf_swcounter_set_period(counter);
3734 }
3540 return 0; 3735 return 0;
3541} 3736}
3542 3737
3543static void perf_swcounter_disable(struct perf_counter *counter) 3738static void perf_swcounter_disable(struct perf_counter *counter)
3544{ 3739{
3545 perf_swcounter_update(counter);
3546} 3740}
3547 3741
3548static const struct pmu perf_ops_generic = { 3742static const struct pmu perf_ops_generic = {
3549 .enable = perf_swcounter_enable, 3743 .enable = perf_swcounter_enable,
3550 .disable = perf_swcounter_disable, 3744 .disable = perf_swcounter_disable,
3551 .read = perf_swcounter_read, 3745 .read = perf_swcounter_read,
3746 .unthrottle = perf_swcounter_unthrottle,
3552}; 3747};
3553 3748
3554/* 3749/*
3750 * hrtimer based swcounter callback
3751 */
3752
3753static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3754{
3755 enum hrtimer_restart ret = HRTIMER_RESTART;
3756 struct perf_sample_data data;
3757 struct perf_counter *counter;
3758 u64 period;
3759
3760 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3761 counter->pmu->read(counter);
3762
3763 data.addr = 0;
3764 data.regs = get_irq_regs();
3765 /*
3766 * In case we exclude kernel IPs or are somehow not in interrupt
3767 * context, provide the next best thing, the user IP.
3768 */
3769 if ((counter->attr.exclude_kernel || !data.regs) &&
3770 !counter->attr.exclude_user)
3771 data.regs = task_pt_regs(current);
3772
3773 if (data.regs) {
3774 if (perf_counter_overflow(counter, 0, &data))
3775 ret = HRTIMER_NORESTART;
3776 }
3777
3778 period = max_t(u64, 10000, counter->hw.sample_period);
3779 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3780
3781 return ret;
3782}
3783
3784/*
3555 * Software counter: cpu wall time clock 3785 * Software counter: cpu wall time clock
3556 */ 3786 */
3557 3787
@@ -3668,17 +3898,24 @@ static const struct pmu perf_ops_task_clock = {
3668}; 3898};
3669 3899
3670#ifdef CONFIG_EVENT_PROFILE 3900#ifdef CONFIG_EVENT_PROFILE
3671void perf_tpcounter_event(int event_id) 3901void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3902 int entry_size)
3672{ 3903{
3904 struct perf_raw_record raw = {
3905 .size = entry_size,
3906 .data = record,
3907 };
3908
3673 struct perf_sample_data data = { 3909 struct perf_sample_data data = {
3674 .regs = get_irq_regs(); 3910 .regs = get_irq_regs(),
3675 .addr = 0, 3911 .addr = addr,
3912 .raw = &raw,
3676 }; 3913 };
3677 3914
3678 if (!data.regs) 3915 if (!data.regs)
3679 data.regs = task_pt_regs(current); 3916 data.regs = task_pt_regs(current);
3680 3917
3681 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, &data); 3918 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
3682} 3919}
3683EXPORT_SYMBOL_GPL(perf_tpcounter_event); 3920EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3684 3921
@@ -3687,16 +3924,20 @@ extern void ftrace_profile_disable(int);
3687 3924
3688static void tp_perf_counter_destroy(struct perf_counter *counter) 3925static void tp_perf_counter_destroy(struct perf_counter *counter)
3689{ 3926{
3690 ftrace_profile_disable(perf_event_id(&counter->attr)); 3927 ftrace_profile_disable(counter->attr.config);
3691} 3928}
3692 3929
3693static const struct pmu *tp_perf_counter_init(struct perf_counter *counter) 3930static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3694{ 3931{
3695 int event_id = perf_event_id(&counter->attr); 3932 /*
3696 int ret; 3933 * Raw tracepoint data is a severe data leak, only allow root to
3934 * have these.
3935 */
3936 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3937 !capable(CAP_SYS_ADMIN))
3938 return ERR_PTR(-EPERM);
3697 3939
3698 ret = ftrace_profile_enable(event_id); 3940 if (ftrace_profile_enable(counter->attr.config))
3699 if (ret)
3700 return NULL; 3941 return NULL;
3701 3942
3702 counter->destroy = tp_perf_counter_destroy; 3943 counter->destroy = tp_perf_counter_destroy;
@@ -3829,9 +4070,9 @@ perf_counter_alloc(struct perf_counter_attr *attr,
3829 atomic64_set(&hwc->period_left, hwc->sample_period); 4070 atomic64_set(&hwc->period_left, hwc->sample_period);
3830 4071
3831 /* 4072 /*
3832 * we currently do not support PERF_SAMPLE_GROUP on inherited counters 4073 * we currently do not support PERF_FORMAT_GROUP on inherited counters
3833 */ 4074 */
3834 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP)) 4075 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
3835 goto done; 4076 goto done;
3836 4077
3837 switch (attr->type) { 4078 switch (attr->type) {
@@ -3874,6 +4115,8 @@ done:
3874 atomic_inc(&nr_mmap_counters); 4115 atomic_inc(&nr_mmap_counters);
3875 if (counter->attr.comm) 4116 if (counter->attr.comm)
3876 atomic_inc(&nr_comm_counters); 4117 atomic_inc(&nr_comm_counters);
4118 if (counter->attr.task)
4119 atomic_inc(&nr_task_counters);
3877 } 4120 }
3878 4121
3879 return counter; 4122 return counter;
@@ -4235,8 +4478,10 @@ void perf_counter_exit_task(struct task_struct *child)
4235 struct perf_counter_context *child_ctx; 4478 struct perf_counter_context *child_ctx;
4236 unsigned long flags; 4479 unsigned long flags;
4237 4480
4238 if (likely(!child->perf_counter_ctxp)) 4481 if (likely(!child->perf_counter_ctxp)) {
4482 perf_counter_task(child, NULL, 0);
4239 return; 4483 return;
4484 }
4240 4485
4241 local_irq_save(flags); 4486 local_irq_save(flags);
4242 /* 4487 /*
@@ -4255,17 +4500,20 @@ void perf_counter_exit_task(struct task_struct *child)
4255 */ 4500 */
4256 spin_lock(&child_ctx->lock); 4501 spin_lock(&child_ctx->lock);
4257 child->perf_counter_ctxp = NULL; 4502 child->perf_counter_ctxp = NULL;
4258 if (child_ctx->parent_ctx) { 4503 /*
4259 /* 4504 * If this context is a clone; unclone it so it can't get
4260 * This context is a clone; unclone it so it can't get 4505 * swapped to another process while we're removing all
4261 * swapped to another process while we're removing all 4506 * the counters from it.
4262 * the counters from it. 4507 */
4263 */ 4508 unclone_ctx(child_ctx);
4264 put_ctx(child_ctx->parent_ctx); 4509 spin_unlock_irqrestore(&child_ctx->lock, flags);
4265 child_ctx->parent_ctx = NULL; 4510
4266 } 4511 /*
4267 spin_unlock(&child_ctx->lock); 4512 * Report the task dead after unscheduling the counters so that we
4268 local_irq_restore(flags); 4513 * won't get any samples after PERF_EVENT_EXIT. We can however still
4514 * get a few PERF_EVENT_READ events.
4515 */
4516 perf_counter_task(child, child_ctx, 0);
4269 4517
4270 /* 4518 /*
4271 * We can recurse on the same lock type through: 4519 * We can recurse on the same lock type through:
@@ -4486,6 +4734,11 @@ perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4486 perf_counter_init_cpu(cpu); 4734 perf_counter_init_cpu(cpu);
4487 break; 4735 break;
4488 4736
4737 case CPU_ONLINE:
4738 case CPU_ONLINE_FROZEN:
4739 hw_perf_counter_setup_online(cpu);
4740 break;
4741
4489 case CPU_DOWN_PREPARE: 4742 case CPU_DOWN_PREPARE:
4490 case CPU_DOWN_PREPARE_FROZEN: 4743 case CPU_DOWN_PREPARE_FROZEN:
4491 perf_counter_exit_cpu(cpu); 4744 perf_counter_exit_cpu(cpu);
@@ -4510,6 +4763,8 @@ void __init perf_counter_init(void)
4510{ 4763{
4511 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, 4764 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4512 (void *)(long)smp_processor_id()); 4765 (void *)(long)smp_processor_id());
4766 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4767 (void *)(long)smp_processor_id());
4513 register_cpu_notifier(&perf_cpu_nb); 4768 register_cpu_notifier(&perf_cpu_nb);
4514} 4769}
4515 4770