Re: [PATCH v1] perf record: Fix event fd races

From: Leo Yan
Date: Mon Oct 24 2022 - 05:18:01 EST


On Sun, Oct 23, 2022 at 10:33:30PM -0700, Ian Rogers wrote:

[...]

> > > +static volatile int done_fd = -1;
> >
> > Here is a bit suspecious for adding volatile qualifier. See the
> > document: process/volatile-considered-harmful.rst.
> >
> > I know the document is mainly for kernel programming, but seems to me
> > it's also valid for C programming in userspace.
> >
> > I not sure what's the purpose for adding volatile for done_fd, if we
> > really have concern for reading any stale value for done_fd, should we
> > use WRITE_ONCE/READ_ONCE?
>
> We could just switch to C11 and stdatomic. The volatile is consistent
> with the code above and more consistent with the expectation of
> writing to a variable that is read in a signal handler.

Thanks for the info for C11 and stdatomic.h. The documentation [1] says
the safe way is for accessing shared data in signal handler is:

static volatile sig_atomic_t done_fd = -1;

It's fine if you want to use another patch to address this issue, this
patch for fixing errno is fine for me:

Reviewed-by: Leo Yan <leo.yan@xxxxxxxxxx>

[1] https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers