[PATCH v2 3/6] perf: Annotate some of the error codes with perf_err()

Alexander Shishkin
2015-08-24T14:35:38Z

This patch annotates a few semi-random error paths in perf core to
illustrate the extended error reporting facility. Most of them can
be triggered from perf tools.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
kernel/events/core.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 3ff28fc8bd..7beab37ea6 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3382,10 +3382,10 @@ find_lively_task_by_vpid(pid_t vpid)
rcu_read_unlock();

if (!task)
- return ERR_PTR(-ESRCH);
+ return PERF_ERR_PTR(-ESRCH, "task not found");

/* Reuse ptrace permission checks for now. */
- err = -EACCES;
+ err = perf_err(-EACCES, "insufficient permissions for tracing this task");
if (!ptrace_may_access(task, PTRACE_MODE_READ))
goto errout;

@@ -3413,7 +3413,8 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
if (!task) {
/* Must be root to operate on a CPU event: */
if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
- return ERR_PTR(-EACCES);
+ return PERF_ERR_PTR(-EACCES,
+ "must be root to operate on a CPU event");

/*
* We could be clever and allow to attach a event to an
@@ -3421,7 +3422,7 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
* that's for later.
*/
if (!cpu_online(cpu))
- return ERR_PTR(-ENODEV);
+ return PERF_ERR_PTR(-ENODEV, "cpu is offline");

cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
ctx = &cpuctx->ctx;
@@ -8134,15 +8135,16 @@ SYSCALL_DEFINE5(perf_event_open,

if (!attr.exclude_kernel) {
if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
- return -EACCES;
+ return perf_err_sync(&attr, -EACCES,
+ "kernel tracing forbidden for the unprivileged");
}

if (attr.freq) {
if (attr.sample_freq > sysctl_perf_event_sample_rate)
- return -EINVAL;
+ return perf_err_sync(&attr, -EINVAL, "sample_freq too high");
} else {
if (attr.sample_period & (1ULL << 63))
- return -EINVAL;
+ return perf_err_sync(&attr, -EINVAL, "sample_period too high");
}

/*
@@ -8152,14 +8154,14 @@ SYSCALL_DEFINE5(perf_event_open,
* cgroup.
*/
if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
- return -EINVAL;
+ return perf_err_sync(&attr, -EINVAL, "pid and cpu need to be set in cgroup mode");

if (flags & PERF_FLAG_FD_CLOEXEC)
f_flags |= O_CLOEXEC;

event_fd = get_unused_fd_flags(f_flags);
if (event_fd < 0)
- return event_fd;
+ return perf_err_sync(&attr, event_fd, "can't obtain a file descriptor");

if (group_fd != -1) {
err = perf_fget_light(group_fd, &group);
--
2.5.0