Re: [PATCH v2 1/6] perf: Introduce extended syscall error reporting

From: Andy Shevchenko
Date: Mon Aug 31 2015 - 14:47:24 EST


On Mon, Aug 24, 2015 at 5:32 PM, Alexander Shishkin
<alexander.shishkin@xxxxxxxxxxxxxxx> wrote:

One small comment below.

> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -49,6 +49,79 @@
>
> #include <asm/irq_regs.h>
>
> +static bool extended_reporting_enabled(struct perf_event_attr *attr)
> +{
> + if (attr->size >= PERF_ATTR_SIZE_VER6 &&
> + attr->perf_err_size > 0)
> + return true;
> +
> + return false;
> +}
> +
> +/*
> + * Provide a JSON formatted error report to the user if they asked for it.
> + */
> +static void perf_error_report_site(struct perf_event_attr *attr,
> + const struct perf_err_site *site)
> +{
> + unsigned long len;
> + char *buffer;
> +
> + if (!site || !extended_reporting_enabled(attr))
> + return;
> +
> + /* in case of nested perf_err()s, which you shouldn't really do */
> + while (site->code <= -PERF_ERRNO)
> + site = perf_errno_to_site(site->code);
> +
> + buffer = kasprintf(GFP_KERNEL,
> + "{\n"
> + "\t\"code\": %d,\n"
> + "\t\"module\": \"%s\",\n"
> + "\t\"message\": \"%s\"\n"
> + "}\n",
> + site->code, site->owner, site->message
> + );
> + if (!buffer)
> + return;
> +
> + /* trim the buffer to the supplied boundary */
> + len = strlen(buffer);
> + if (len >= attr->perf_err_size) {
> + len = attr->perf_err_size - 1;
> + buffer[len] = 0;
> + }

len = strnlen(buffer, attr->perf_err_size);
buffer[len] = 0;

And perhaps perf_err_size has to be length (perf_err_len) ?

> +
> + if (copy_to_user((void __user *)attr->perf_err, buffer, len + 1)) {
> + /* if we failed to copy once, don't bother later */
> + attr->perf_err_size = 0;

Kaboom next time on buffer[-1] = 0; since len >= 0?

> + }
> +
> + kfree(buffer);
> +}

--
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/