Message ID | 20231102175735.2272696-4-irogers@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp533256vqu; Thu, 2 Nov 2023 10:58:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHhx5rFcfF8kydHxnA1sBLLviHQa7mjLszaSymKtduEVYFOaM70eGQWo7cz5oCWwvlGVP/D X-Received: by 2002:a05:6a21:4841:b0:181:8e2:ba3c with SMTP id au1-20020a056a21484100b0018108e2ba3cmr8886364pzc.19.1698947939723; Thu, 02 Nov 2023 10:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698947939; cv=none; d=google.com; s=arc-20160816; b=ip2G4E7WSmJRZsm4GY1iHuYC9PYmubDlkPJ89V1sVEqXjQF1HrdFeYpku5rhdwT/3A XA9tSuHp1opakL0jpG5GgM5Mi3ozR0LwL4TkasTx4DTIeaKa/r1rs6cgP3k7XM01noZM FU6Du16PE0rgZhuawRAhg88LatPIW6v59h/IlHJus4UVvjmCqsmFnQUH5/lsNGuL65ll U8CNER14Xp6LaC4BgVH9vbxhubQkk3FQLt28CuxSPfhZ/y1Cg1OyX9ISU3KvKPKSSJRR J2Mm623FY8hKc06YTRctpnGTLbvAWEDEadKWZjzqm5jE9fZK+y4gxRxx+shYMsw6FBAo 6ufA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=45fiMNEp9HPngAxEFglGXsIL4df8TDdJeTxUUf03U6k=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=iCKBcKrSrCBWjJeiexJCJE77wMGG7gZlZ0alRhr1N+IQfmrKDK1alEP4FlrYXEw2rk hX+j1pd/IC7Y4PFRe7Gk2tNsPgvrYCs9QXp9M5Qlh/CLo+qXSzXvB0qOblwIFEIKLzDR xsX2HI3nCLwocKdebwr41jsY5FiYOLRKs82VN5X1Y6cnnQRoRJMyMRYuBvVIb1Imqs3R VYRZ2qBRfnnM/MBVihYMC6Uel4RxDnoy/R9+pMOfBOpF8bNm5fjnFdgNOXv8sx0253vC f3pVjHwgNqCmFlTWnew3zo2D91H5v93N9oqjsCz8U9J/VCVxQZJKC16Q4A8mWrl2NdVA rwvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZDeAbjg7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id bs64-20020a632843000000b005b8eacb29c1si52414pgb.437.2023.11.02.10.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:58:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZDeAbjg7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 4CFC7837EA4A; Thu, 2 Nov 2023 10:58:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345186AbjKBR6V (ORCPT <rfc822;heyuhang3455@gmail.com> + 35 others); Thu, 2 Nov 2023 13:58:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344990AbjKBR6N (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 2 Nov 2023 13:58:13 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED4341A6 for <linux-kernel@vger.kernel.org>; Thu, 2 Nov 2023 10:58:03 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da0cb98f66cso1414569276.2 for <linux-kernel@vger.kernel.org>; Thu, 02 Nov 2023 10:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947883; x=1699552683; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=45fiMNEp9HPngAxEFglGXsIL4df8TDdJeTxUUf03U6k=; b=ZDeAbjg7DorUI11gf20wny99hf5XbQfWunnSPi5m2R0HqVc8OL5uP07/PGLYd9Y90a f9teeab/1qgusnyL0LPb1etfZ5EWJ5vgf8AukOhCo4nC7hgC2jentTcDzalgx6PvTnEp vGt3ocDD6X2AecPEWf9gFIfcMuFiMPzsRLDkIRpZlO1bgeOi+M9bB8r0fqfALrwzzlIy jHs7MitmAzVeS9YCcFsnURV/w3ezT2H3wbu15KE0uaiZgY0p5kZ1+UPlBdJGQuBOWjOM oBBoK47cgaIC+gvIe9Mub1Q8gaDZIazLl6FnYNBLoxz7OEswjHCYi8xdepODCBzcWmcF hYIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947883; x=1699552683; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=45fiMNEp9HPngAxEFglGXsIL4df8TDdJeTxUUf03U6k=; b=m0bm5QWl3x7GAL0j8InzHUp27xoQty79er1GQqQ0ZMbMS3v6jc4Splc2VHBPMShCrG oCdB+uJFl9ve1yAydZV0n1+EGIAME++SPfhpSn/4BScGk9Z61V0qZZTlLem8kjakYkih pVAjpadCH9X7f6ol5506ZSlIEejR0L+VeKjL2K5k7tQZiSDpJZZgyKxmQcaeZA86dZj8 7e9J+dT3p1IQYkrzEJ2typHRjrHkcE/A40f78EvEHgeN10pik1w2HsWbX9qYxFtQvHmX Di+n9+IygGCxxZ2e0y9jEdoVRn7SpagUyvUXOg3pqs3NyvWprKs91zMvmkOKTgvPAviX 5M4w== X-Gm-Message-State: AOJu0YxpPdSY0hxaKJmhoSQ7/uhTstBvZF1GuzZA2trC7vtEWFmqeTeD Onq66fI7xEZoqnQvJza7q1Ek5xqX+3r0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a05:6902:168c:b0:da0:3e46:8ba5 with SMTP id bx12-20020a056902168c00b00da03e468ba5mr359884ybb.8.1698947882736; Thu, 02 Nov 2023 10:58:02 -0700 (PDT) Date: Thu, 2 Nov 2023 10:56:45 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-4-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 03/53] libperf: Lazily allocate mmap event copy From: Ian Rogers <irogers@google.com> To: 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>, Ian Rogers <irogers@google.com>, Adrian Hunter <adrian.hunter@intel.com>, Nick Terrell <terrelln@fb.com>, Kan Liang <kan.liang@linux.intel.com>, Andi Kleen <ak@linux.intel.com>, Kajol Jain <kjain@linux.ibm.com>, Athira Rajeev <atrajeev@linux.vnet.ibm.com>, Huacai Chen <chenhuacai@kernel.org>, Masami Hiramatsu <mhiramat@kernel.org>, Vincent Whitchurch <vincent.whitchurch@axis.com>, "Steinar H. Gunderson" <sesse@google.com>, Liam Howlett <liam.howlett@oracle.com>, Miguel Ojeda <ojeda@kernel.org>, Colin Ian King <colin.i.king@gmail.com>, Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong <yangjihong1@huawei.com>, Ming Wang <wangming01@loongson.cn>, James Clark <james.clark@arm.com>, K Prateek Nayak <kprateek.nayak@amd.com>, Sean Christopherson <seanjc@google.com>, Leo Yan <leo.yan@linaro.org>, Ravi Bangoria <ravi.bangoria@amd.com>, German Gomez <german.gomez@arm.com>, Changbin Du <changbin.du@huawei.com>, Paolo Bonzini <pbonzini@redhat.com>, Li Dong <lidong@vivo.com>, Sandipan Das <sandipan.das@amd.com>, liuwenyu <liuwenyu7@huawei.com>, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 02 Nov 2023 10:58:47 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781476034950982460 X-GMAIL-MSGID: 1781476034950982460 |
Series |
Improvements to memory use
|
|
Commit Message
Ian Rogers
Nov. 2, 2023, 5:56 p.m. UTC
The event copy in the mmap is used to have storage to a read
event. Not all users of mmaps read the events, such as perf record, so
switch the allocation to being on first read rather than being
embedded within the perf_mmap.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/lib/perf/include/internal/mmap.h | 2 +-
tools/lib/perf/mmap.c | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
Comments
Hi, On Thu, Nov 02, 2023 at 10:56:45AM -0700, Ian Rogers wrote: > The event copy in the mmap is used to have storage to a read > event. Not all users of mmaps read the events, such as perf record, so > switch the allocation to being on first read rather than being > embedded within the perf_mmap. > > Signed-off-by: Ian Rogers <irogers@google.com> > --- > tools/lib/perf/include/internal/mmap.h | 2 +- > tools/lib/perf/mmap.c | 9 +++++++++ > 2 files changed, 10 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h > index 5a062af8e9d8..b11aaf5ed645 100644 > --- a/tools/lib/perf/include/internal/mmap.h > +++ b/tools/lib/perf/include/internal/mmap.h > @@ -33,7 +33,7 @@ struct perf_mmap { > bool overwrite; > u64 flush; > libperf_unmap_cb_t unmap_cb; > - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); > + void *event_copy; > struct perf_mmap *next; > }; > > diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c > index 2184814b37dd..91ae46aac378 100644 > --- a/tools/lib/perf/mmap.c > +++ b/tools/lib/perf/mmap.c > @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, > > void perf_mmap__munmap(struct perf_mmap *map) > { > + free(map->event_copy); > + map->event_copy = NULL; > if (map && map->base != NULL) { If map can be NULL as the if statement above suggests, then there is a potential a null pointer dereference bug here. Suggestion: if (!map) return; free(map->event_copy); map->event_copy = NULL; if (map->base != NULL) { ... Cheers, -Guilherme > munmap(map->base, perf_mmap__mmap_len(map)); > map->base = NULL; > @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, > unsigned int len = min(sizeof(*event), size), cpy; > void *dst = map->event_copy; > > + if (!dst) { > + dst = malloc(PERF_SAMPLE_MAX_SIZE); > + if (!dst) > + return NULL; > + map->event_copy = dst; > + } > + > do { > cpy = min(map->mask + 1 - (offset & map->mask), len); > memcpy(dst, &data[offset & map->mask], cpy); > -- > 2.42.0.869.gea05f2083d-goog > >
On Fri, Nov 3, 2023 at 1:33 AM Guilherme Amadio <amadio@gentoo.org> wrote: > > Hi, > > On Thu, Nov 02, 2023 at 10:56:45AM -0700, Ian Rogers wrote: > > The event copy in the mmap is used to have storage to a read > > event. Not all users of mmaps read the events, such as perf record, so > > switch the allocation to being on first read rather than being > > embedded within the perf_mmap. > > > > Signed-off-by: Ian Rogers <irogers@google.com> > > --- > > tools/lib/perf/include/internal/mmap.h | 2 +- > > tools/lib/perf/mmap.c | 9 +++++++++ > > 2 files changed, 10 insertions(+), 1 deletion(-) > > > > diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h > > index 5a062af8e9d8..b11aaf5ed645 100644 > > --- a/tools/lib/perf/include/internal/mmap.h > > +++ b/tools/lib/perf/include/internal/mmap.h > > @@ -33,7 +33,7 @@ struct perf_mmap { > > bool overwrite; > > u64 flush; > > libperf_unmap_cb_t unmap_cb; > > - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); > > + void *event_copy; > > struct perf_mmap *next; > > }; > > > > diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c > > index 2184814b37dd..91ae46aac378 100644 > > --- a/tools/lib/perf/mmap.c > > +++ b/tools/lib/perf/mmap.c > > @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, > > > > void perf_mmap__munmap(struct perf_mmap *map) > > { > > + free(map->event_copy); > > + map->event_copy = NULL; > > if (map && map->base != NULL) { > > If map can be NULL as the if statement above suggests, then there is a > potential a null pointer dereference bug here. Suggestion: > > if (!map) > return; > > free(map->event_copy); > map->event_copy = NULL; > if (map->base != NULL) { > > ... Makes sense, will fix in v5. Waiting to get additional feedback to avoid too much email. Thanks, Ian > Cheers, > -Guilherme > > > munmap(map->base, perf_mmap__mmap_len(map)); > > map->base = NULL; > > @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, > > unsigned int len = min(sizeof(*event), size), cpy; > > void *dst = map->event_copy; > > > > + if (!dst) { > > + dst = malloc(PERF_SAMPLE_MAX_SIZE); > > + if (!dst) > > + return NULL; > > + map->event_copy = dst; > > + } > > + > > do { > > cpy = min(map->mask + 1 - (offset & map->mask), len); > > memcpy(dst, &data[offset & map->mask], cpy); > > -- > > 2.42.0.869.gea05f2083d-goog > > > >
On Fri, Nov 3, 2023 at 8:49 AM Ian Rogers <irogers@google.com> wrote: > > On Fri, Nov 3, 2023 at 1:33 AM Guilherme Amadio <amadio@gentoo.org> wrote: > > > > Hi, > > > > On Thu, Nov 02, 2023 at 10:56:45AM -0700, Ian Rogers wrote: > > > The event copy in the mmap is used to have storage to a read > > > event. Not all users of mmaps read the events, such as perf record, so > > > switch the allocation to being on first read rather than being > > > embedded within the perf_mmap. > > > > > > Signed-off-by: Ian Rogers <irogers@google.com> > > > --- > > > tools/lib/perf/include/internal/mmap.h | 2 +- > > > tools/lib/perf/mmap.c | 9 +++++++++ > > > 2 files changed, 10 insertions(+), 1 deletion(-) > > > > > > diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h > > > index 5a062af8e9d8..b11aaf5ed645 100644 > > > --- a/tools/lib/perf/include/internal/mmap.h > > > +++ b/tools/lib/perf/include/internal/mmap.h > > > @@ -33,7 +33,7 @@ struct perf_mmap { > > > bool overwrite; > > > u64 flush; > > > libperf_unmap_cb_t unmap_cb; > > > - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); > > > + void *event_copy; > > > struct perf_mmap *next; > > > }; > > > > > > diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c > > > index 2184814b37dd..91ae46aac378 100644 > > > --- a/tools/lib/perf/mmap.c > > > +++ b/tools/lib/perf/mmap.c > > > @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, > > > > > > void perf_mmap__munmap(struct perf_mmap *map) > > > { > > > + free(map->event_copy); > > > + map->event_copy = NULL; > > > if (map && map->base != NULL) { > > > > If map can be NULL as the if statement above suggests, then there is a > > potential a null pointer dereference bug here. Suggestion: > > > > if (!map) > > return; > > > > free(map->event_copy); > > map->event_copy = NULL; > > if (map->base != NULL) { > > > > ... > > Makes sense, will fix in v5. Waiting to get additional feedback to > avoid too much email. Acked-by: Namhyung Kim <namhyung@kernel.org> But I have another concern (not related to this change). > > > > > munmap(map->base, perf_mmap__mmap_len(map)); > > > map->base = NULL; > > > @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, > > > unsigned int len = min(sizeof(*event), size), cpy; I'm not sure if it's ok to read less than the actual size, IOW it seems to assume 'size' is smaller than sizeof(*event). I guess it's true for most cases as union perf_event has perf_record_mmap2 (among others) which contains a filename array of size PATH_MAX. But the SAMPLE record can be larger than that when it has PERF_SAMPLE_AUX IIRC. It'd happen only if it crossed the mmap boundary and I'm afraid it'd corrupt the data. Thanks, Namhyung > > > void *dst = map->event_copy; > > > > > > + if (!dst) { > > > + dst = malloc(PERF_SAMPLE_MAX_SIZE); > > > + if (!dst) > > > + return NULL; > > > + map->event_copy = dst; > > > + } > > > + > > > do { > > > cpy = min(map->mask + 1 - (offset & map->mask), len); > > > memcpy(dst, &data[offset & map->mask], cpy); > > > -- > > > 2.42.0.869.gea05f2083d-goog > > > > > >
On Sun, Nov 5, 2023 at 10:12 AM Namhyung Kim <namhyung@kernel.org> wrote: > > On Fri, Nov 3, 2023 at 8:49 AM Ian Rogers <irogers@google.com> wrote: > > > > On Fri, Nov 3, 2023 at 1:33 AM Guilherme Amadio <amadio@gentoo.org> wrote: > > > > > > Hi, > > > > > > On Thu, Nov 02, 2023 at 10:56:45AM -0700, Ian Rogers wrote: > > > > The event copy in the mmap is used to have storage to a read > > > > event. Not all users of mmaps read the events, such as perf record, so > > > > switch the allocation to being on first read rather than being > > > > embedded within the perf_mmap. > > > > > > > > Signed-off-by: Ian Rogers <irogers@google.com> > > > > --- > > > > tools/lib/perf/include/internal/mmap.h | 2 +- > > > > tools/lib/perf/mmap.c | 9 +++++++++ > > > > 2 files changed, 10 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h > > > > index 5a062af8e9d8..b11aaf5ed645 100644 > > > > --- a/tools/lib/perf/include/internal/mmap.h > > > > +++ b/tools/lib/perf/include/internal/mmap.h > > > > @@ -33,7 +33,7 @@ struct perf_mmap { > > > > bool overwrite; > > > > u64 flush; > > > > libperf_unmap_cb_t unmap_cb; > > > > - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); > > > > + void *event_copy; > > > > struct perf_mmap *next; > > > > }; > > > > > > > > diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c > > > > index 2184814b37dd..91ae46aac378 100644 > > > > --- a/tools/lib/perf/mmap.c > > > > +++ b/tools/lib/perf/mmap.c > > > > @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, > > > > > > > > void perf_mmap__munmap(struct perf_mmap *map) > > > > { > > > > + free(map->event_copy); > > > > + map->event_copy = NULL; > > > > if (map && map->base != NULL) { > > > > > > If map can be NULL as the if statement above suggests, then there is a > > > potential a null pointer dereference bug here. Suggestion: > > > > > > if (!map) > > > return; > > > > > > free(map->event_copy); > > > map->event_copy = NULL; > > > if (map->base != NULL) { > > > > > > ... > > > > Makes sense, will fix in v5. Waiting to get additional feedback to > > avoid too much email. > > Acked-by: Namhyung Kim <namhyung@kernel.org> > > > But I have another concern (not related to this change). > > > > > > > > munmap(map->base, perf_mmap__mmap_len(map)); > > > > map->base = NULL; > > > > @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, > > > > unsigned int len = min(sizeof(*event), size), cpy; > > I'm not sure if it's ok to read less than the actual size, IOW > it seems to assume 'size' is smaller than sizeof(*event). > I guess it's true for most cases as union perf_event has > perf_record_mmap2 (among others) which contains a > filename array of size PATH_MAX. > > But the SAMPLE record can be larger than that when it has > PERF_SAMPLE_AUX IIRC. It'd happen only if it crossed the mmap > boundary and I'm afraid it'd corrupt the data. Thanks, I was thinking this would just be a drop in change but I think given this feedback it would be better to switch from allocating once a PERF_SAMPLE_MAX_SIZE buffer to allocating or reallocating one based on size. This potentially saves memory when size is less than PERF_SAMPLE_MAX_SIZE and by removing the min calculation for the amount copied (len) we can potentially exceed it and fix a potential bug. I'll add this in v5. Thanks, Ian > Thanks, > Namhyung > > > > > > void *dst = map->event_copy; > > > > > > > > + if (!dst) { > > > > + dst = malloc(PERF_SAMPLE_MAX_SIZE); > > > > + if (!dst) > > > > + return NULL; > > > > + map->event_copy = dst; > > > > + } > > > > + > > > > do { > > > > cpy = min(map->mask + 1 - (offset & map->mask), len); > > > > memcpy(dst, &data[offset & map->mask], cpy); > > > > -- > > > > 2.42.0.869.gea05f2083d-goog > > > > > > > >
diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h index 5a062af8e9d8..b11aaf5ed645 100644 --- a/tools/lib/perf/include/internal/mmap.h +++ b/tools/lib/perf/include/internal/mmap.h @@ -33,7 +33,7 @@ struct perf_mmap { bool overwrite; u64 flush; libperf_unmap_cb_t unmap_cb; - char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); + void *event_copy; struct perf_mmap *next; }; diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c index 2184814b37dd..91ae46aac378 100644 --- a/tools/lib/perf/mmap.c +++ b/tools/lib/perf/mmap.c @@ -51,6 +51,8 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, void perf_mmap__munmap(struct perf_mmap *map) { + free(map->event_copy); + map->event_copy = NULL; if (map && map->base != NULL) { munmap(map->base, perf_mmap__mmap_len(map)); map->base = NULL; @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, unsigned int len = min(sizeof(*event), size), cpy; void *dst = map->event_copy; + if (!dst) { + dst = malloc(PERF_SAMPLE_MAX_SIZE); + if (!dst) + return NULL; + map->event_copy = dst; + } + do { cpy = min(map->mask + 1 - (offset & map->mask), len); memcpy(dst, &data[offset & map->mask], cpy);