Re: [PATCH v2 8/8] perf record: Directly bail out for compat case

From: Adrian Hunter
Date: Wed Jun 02 2021 - 07:18:44 EST


On 2/06/21 1:30 pm, Leo Yan wrote:
> Since the 64-bit atomicity is not promised in 32-bit perf, directly
> report the error and bail out for this case.
>
> Now only applies on x86_64 and Arm64 platforms.
>
> Suggested-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>

Maybe we can do better for the compat case.

We can assume the upper 32-bits change very seldom,
and always increase. So for the 'read' case:

u64 first, second, last;
u64 mask = (u64)((u32)-1) << 32;

do {
first = READ_ONCE(pc->aux_head);
rmb();
second = READ_ONCE(pc->aux_head);
rmb();
last = READ_ONCE(pc->aux_head);
} while ((first & mask) != (last & mask));
return second;

For the write case, we can cause a fatal error only if the new
tail has non-zero upper 32-bits. That gives up to 4GiB of data
before aborting:

if (tail & mask)
return -1;
smp_mb();
WRITE_ONCE(pc->aux_tail, tail);

> Signed-off-by: Leo Yan <leo.yan@xxxxxxxxxx>
> ---
> tools/perf/builtin-record.c | 17 +++++++++++++++++
> 1 file changed, 17 insertions(+)
>
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 3337b5f93336..f47e298281f7 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -74,6 +74,7 @@
> #include <linux/zalloc.h>
> #include <linux/bitmap.h>
> #include <sys/time.h>
> +#include <sys/utsname.h>
>
> struct switch_output {
> bool enabled;
> @@ -848,6 +849,22 @@ static int record__mmap_evlist(struct record *rec,
> opts->auxtrace_sample_mode;
> char msg[512];
>
> +#ifndef __LP64__
> + struct utsname uts;
> + int ret;
> +
> + ret = uname(&uts);
> + if (ret < 0)
> + return ret;
> +
> + if (!strncmp(uts.machine, "x86_64", 6) || !strncmp(uts.machine, "aarch64", 7) ||
> + !strncmp(uts.machine, "arm64", 5)) {
> + pr_err("Error, 32-bit perf cannot record from a 64-bit kernel.\n"
> + "Please use a 64-bit version of perf instead.\n");
> + return -ENOTSUP;
> + }
> +#endif
> +
> if (opts->affinity != PERF_AFFINITY_SYS)
> cpu__setup_cpunode_map();
>
>