All of lore.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: 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>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Milian Wolff <milian.wolff@kdab.com>
Subject: Re: [PATCH v1] perf record: Ensure space for lost samples
Date: Sat, 8 Jun 2024 19:31:06 -0700	[thread overview]
Message-ID: <ZmUT6qNB_ghSCibT@google.com> (raw)
In-Reply-To: <20240607204727.849174-1-irogers@google.com>

On Fri, Jun 07, 2024 at 01:47:27PM -0700, Ian Rogers wrote:
> Previous allocation didn't account for sample ID written after the
> lost samples event. Switch from malloc/free to a stack allocation.
> 
> Reported-by: Milian Wolff <milian.wolff@kdab.com>
> Closes: https://lore.kernel.org/linux-perf-users/23879991.0LEYPuXRzz@milian-workstation/
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/lib/perf/include/perf/event.h |  3 +++
>  tools/perf/builtin-record.c         | 37 ++++++++++-------------------
>  2 files changed, 15 insertions(+), 25 deletions(-)
> 
> diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/perf/event.h
> index ae64090184d3..8f3cefef9069 100644
> --- a/tools/lib/perf/include/perf/event.h
> +++ b/tools/lib/perf/include/perf/event.h
> @@ -77,6 +77,9 @@ struct perf_record_lost_samples {
>  	__u64			 lost;
>  };
>  
> +#define MAX_ID_HDR_ENTRIES  6
> +#define PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE \
> +	(sizeof(struct perf_record_lost_samples) + MAX_ID_HDR_ENTRIES * sizeof(__u64))
>  /*
>   * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_LOST
>   */
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 66a3de8ac661..1615a1723fb9 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -1926,7 +1926,10 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel,
>  static void record__read_lost_samples(struct record *rec)
>  {
>  	struct perf_session *session = rec->session;
> -	struct perf_record_lost_samples *lost = NULL;
> +	union {
> +		struct perf_record_lost_samples lost;
> +		char lost_and_sample_id[PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE];
> +	} lost;

Can we make it struct and keep the sample_id part only?

Other than that, looks good to me.

Thanks,
Namhyung

>  	struct evsel *evsel;
>  
>  	/* there was an error during record__open */
> @@ -1951,20 +1954,13 @@ static void record__read_lost_samples(struct record *rec)
>  
>  				if (perf_evsel__read(&evsel->core, x, y, &count) < 0) {
>  					pr_debug("read LOST count failed\n");
> -					goto out;
> +					return;
>  				}
>  
>  				if (count.lost) {
> -					if (!lost) {
> -						lost = zalloc(sizeof(*lost) +
> -							      session->machines.host.id_hdr_size);
> -						if (!lost) {
> -							pr_debug("Memory allocation failed\n");
> -							return;
> -						}
> -						lost->header.type = PERF_RECORD_LOST_SAMPLES;
> -					}
> -					__record__save_lost_samples(rec, evsel, lost,
> +					memset(&lost, 0, sizeof(lost));
> +					lost.lost.header.type = PERF_RECORD_LOST_SAMPLES;
> +					__record__save_lost_samples(rec, evsel, &lost.lost,
>  								    x, y, count.lost, 0);
>  				}
>  			}
> @@ -1972,21 +1968,12 @@ static void record__read_lost_samples(struct record *rec)
>  
>  		lost_count = perf_bpf_filter__lost_count(evsel);
>  		if (lost_count) {
> -			if (!lost) {
> -				lost = zalloc(sizeof(*lost) +
> -					      session->machines.host.id_hdr_size);
> -				if (!lost) {
> -					pr_debug("Memory allocation failed\n");
> -					return;
> -				}
> -				lost->header.type = PERF_RECORD_LOST_SAMPLES;
> -			}
> -			__record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count,
> +			memset(&lost, 0, sizeof(lost));
> +			lost.lost.header.type = PERF_RECORD_LOST_SAMPLES;
> +			__record__save_lost_samples(rec, evsel, &lost.lost, 0, 0, lost_count,
>  						    PERF_RECORD_MISC_LOST_SAMPLES_BPF);
>  		}
>  	}
> -out:
> -	free(lost);
>  }
>  
>  static volatile sig_atomic_t workload_exec_errno;
> @@ -3198,7 +3185,7 @@ static int switch_output_setup(struct record *rec)
>  	unsigned long val;
>  
>  	/*
> -	 * If we're using --switch-output-events, then we imply its 
> +	 * If we're using --switch-output-events, then we imply its
>  	 * --switch-output=signal, as we'll send a SIGUSR2 from the side band
>  	 *  thread to its parent.
>  	 */
> -- 
> 2.45.2.505.gda0bf45e8d-goog
> 

      reply	other threads:[~2024-06-09  2:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07 20:47 [PATCH v1] perf record: Ensure space for lost samples Ian Rogers
2024-06-09  2:31 ` Namhyung Kim [this message]

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=ZmUT6qNB_ghSCibT@google.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=milian.wolff@kdab.com \
    --cc=mingo@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.