Received: by 2002:ab2:68c1:0:b0:1fd:9a81:d0e4 with SMTP id e1csp319438lqp; Sat, 8 Jun 2024 19:31:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVFrC5wtz0Ljf3B6mbMKNa+/bDdZzXXzHP63i9Oi8uA0xV6qwNsDLWJF6GPw4h4EIKs3XhXhg2CJkrlAuck8B/eHiDYjacLS23iw67olw== X-Google-Smtp-Source: AGHT+IHwSWugwWJBGmGu60TLP89yMk1DrmusIkSZqk8t+ZM+d+fX6A5XXjTPLZ53Wv0igiBc0DU5 X-Received: by 2002:a05:620a:45a4:b0:795:531c:c7fb with SMTP id af79cd13be357-795531cc987mr342594085a.27.1717900277617; Sat, 08 Jun 2024 19:31:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717900277; cv=pass; d=google.com; s=arc-20160816; b=AX7FEOXRDATcDgj/raNU/O0WvXV3UJezjtPYW1oiqOdmhS7COdVAHHz6fGZyKqefUV Hp9jwgNQXXRC0TFxqN1V3Zi3M1ut+EV2WuuWny9EKqGdsbotH04DyIOto4WN4E2kki52 uhjwJlYfezefMDY2J29hdgLcEnSdoR9Nr8vEziHhlWHcv7BWrlwfxpZIXN0pLwJs+u8w HDNql8qUk9PqJcvgLVdcf9tLmrfDBqGqdqbFb/rZsrspgKhA1vFnWGePAECxOoY7F+0a vIDvstDLJv2gh/+YhCMGjjSBynC9Y/80FFKWdkpm6txS6MrAAhK5sCYMMXQfY87/FXIN i29g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ELnrqsacRT9N8F+Di6/3BWdVnH8zYphL8TWNyrTjcWY=; fh=RrLlxbsMz8fUOd0LtV4WXL5GHohy2z73aq3MGrPanq4=; b=eQRewInxk09k1NSB8v0oO0+haRDm6awQFg0BxIOnOq63p6EjVCtzS2gsVINt0h2Y5g QlptTKgJiX+E02WvBB89dDPXiTeR2AC3kxOXcIfoi13iw5dFS7b5j1xk2NyD8moZQhJC OXTVpFkRrlH4ehEBYify/D0AUB4oFcma8+gFtAXmVusybAcBJfZEWLLVZkBAFgpjgpKB lQKGw7vMgsFMlidYFtdFESNIPPr3u2x6DOggOlVER1WWorRwJqXh4/zEQpuhb6fN+r5N rDtV+v1f9DYJMTUhpoEeGXarJqWu+5hnAFcHRdFZJDc9ck9wLUgVjDroMdTlZWQzvxhB tkNg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=p+6B8fRt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-207233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-44038a86297si74730741cf.219.2024.06.08.19.31.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jun 2024 19:31:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-207233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=p+6B8fRt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-207233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5AE791C20CEB for ; Sun, 9 Jun 2024 02:31:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E63816AD7; Sun, 9 Jun 2024 02:31:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="p+6B8fRt" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15C86A3F; Sun, 9 Jun 2024 02:31:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717900270; cv=none; b=q+ekQiccYD3E8AMfHMiZAL6+5d3FbUiUAzQl90wkLzXV3RmeKifC1T20s+f3rWRVDrghmt7ElVQ78CS0ngdl/23Yw+GnY7oENeO+v3jxs7AFhpE59e72t3693lv3Fwi+SKzJNwkZGrgDP7f3MScivPd6lg2xsH9Shs4f/3zs1WY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717900270; c=relaxed/simple; bh=RimMKfclsGPvJiI/BEOm16GPf0mGYGXXaBBnCfkkSms=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PCsC+4G+h/X9b5Fop3E3zVnS7qtmNX6UH0vvvLTSborgvulJNGllRLta16BhhaEHJzxabCc94ArSdEUXLIFG9RKPWlsx/SQsJFfp52eccnu0dfxFXzPhvuZj5vobdygtLhhTzEFEvRTVwFIeWUho/T90OvwvQkjH8TVrowfbi+Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p+6B8fRt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3822EC2BD11; Sun, 9 Jun 2024 02:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717900269; bh=RimMKfclsGPvJiI/BEOm16GPf0mGYGXXaBBnCfkkSms=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=p+6B8fRtaerzTWUY9neUkoQ0lqwulx/SPHrSo8IO+v9uCRn3U90YhXC9vpjCeD80w TWIlSfy565is4Ehq6RJ4CqXpMcgc1Q4LyL7Hn6nB/avqQ8ab9K4uKbxXm2tc9i9o3V bBIhqm2hpOQQtVvqTUous8MUZLD6WaY3AEKPa0JAl+XaII6cotvGiFUQtQ4VR/skfg vdY+N/lOJyTh+to1Rhn5Oc0IopB+7reEWusPQpo8tA/HVuVMY3pGmQqpGnaqiu6aCe 0r1Weg0oHBB3xU0/C3b5MClWNuBZfUdPfiBl4PngCk82guZUMHJpAtBJ+d1GE24aFf zFTdKj5G43ztg== Date: Sat, 8 Jun 2024 19:31:06 -0700 From: Namhyung Kim To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Milian Wolff Subject: Re: [PATCH v1] perf record: Ensure space for lost samples Message-ID: References: <20240607204727.849174-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 > Closes: https://lore.kernel.org/linux-perf-users/23879991.0LEYPuXRzz@milian-workstation/ > Signed-off-by: Ian Rogers > --- > 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 >