linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@linaro.org>
To: Ian Rogers <irogers@google.com>
Cc: Greg Thelen <gthelen@google.com>,
	Anand K Mistry <amistry@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Stephane Eranian <eranian@google.com>
Subject: Re: [PATCH v1] perf record: Fix event fd races
Date: Mon, 24 Oct 2022 10:56:44 +0800	[thread overview]
Message-ID: <Y1X+7FCemionnepj@leoy-huanghe.lan> (raw)
In-Reply-To: <20221024011024.462518-1-irogers@google.com>

Hi Ian,

On Sun, Oct 23, 2022 at 06:10:24PM -0700, Ian Rogers wrote:
> The write call may set errno which is problematic if occurring in a
> function also setting errno. Save and restore errno around the write
> call.
> 
> done_fd may be used after close, clear it as part of the close and
> check its validity in the signal handler.
> 
> Suggested-by: Greg Thelen <gthelen@google.com>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/builtin-record.c | 41 ++++++++++++++++++++++---------------
>  1 file changed, 25 insertions(+), 16 deletions(-)
> 
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 52d254b1530c..e128b855ddde 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -649,7 +649,7 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
>  static volatile int signr = -1;
>  static volatile int child_finished;
>  #ifdef HAVE_EVENTFD_SUPPORT
> -static int done_fd = -1;
> +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?

The rest changes look good to me.

Thanks,
Leo

>  #endif
>  
>  static void sig_handler(int sig)
> @@ -661,19 +661,24 @@ static void sig_handler(int sig)
>  
>  	done = 1;
>  #ifdef HAVE_EVENTFD_SUPPORT
> -{
> -	u64 tmp = 1;
> -	/*
> -	 * It is possible for this signal handler to run after done is checked
> -	 * in the main loop, but before the perf counter fds are polled. If this
> -	 * happens, the poll() will continue to wait even though done is set,
> -	 * and will only break out if either another signal is received, or the
> -	 * counters are ready for read. To ensure the poll() doesn't sleep when
> -	 * done is set, use an eventfd (done_fd) to wake up the poll().
> -	 */
> -	if (write(done_fd, &tmp, sizeof(tmp)) < 0)
> -		pr_err("failed to signal wakeup fd, error: %m\n");
> -}
> +	if (done_fd >= 0) {
> +		u64 tmp = 1;
> +		int orig_errno = errno;
> +
> +		/*
> +		 * It is possible for this signal handler to run after done is
> +		 * checked in the main loop, but before the perf counter fds are
> +		 * polled. If this happens, the poll() will continue to wait
> +		 * even though done is set, and will only break out if either
> +		 * another signal is received, or the counters are ready for
> +		 * read. To ensure the poll() doesn't sleep when done is set,
> +		 * use an eventfd (done_fd) to wake up the poll().
> +		 */
> +		if (write(done_fd, &tmp, sizeof(tmp)) < 0)
> +			pr_err("failed to signal wakeup fd, error: %m\n");
> +
> +		errno = orig_errno;
> +	}
>  #endif // HAVE_EVENTFD_SUPPORT
>  }
>  
> @@ -2834,8 +2839,12 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
>  
>  out_delete_session:
>  #ifdef HAVE_EVENTFD_SUPPORT
> -	if (done_fd >= 0)
> -		close(done_fd);
> +	if (done_fd >= 0) {
> +		fd = done_fd;
> +		done_fd = -1;
> +
> +		close(fd);
> +	}
>  #endif
>  	zstd_fini(&session->zstd_data);
>  	perf_session__delete(session);
> -- 
> 2.38.0.135.g90850a2211-goog
> 

  reply	other threads:[~2022-10-24  2:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24  1:10 [PATCH v1] perf record: Fix event fd races Ian Rogers
2022-10-24  2:56 ` Leo Yan [this message]
2022-10-24  5:33   ` Ian Rogers
2022-10-24  9:16     ` Leo Yan
2022-10-24 11:30       ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y1X+7FCemionnepj@leoy-huanghe.lan \
    --to=leo.yan@linaro.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=amistry@google.com \
    --cc=eranian@google.com \
    --cc=gthelen@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).