Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp324684rdf; Fri, 3 Nov 2023 01:33:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF9k8mtzoi+NmgGISskFkOryQRYfC9ytV0WH0mqr/MEfIgcULY4Vh+uFidbqCa2/YDkYjEL X-Received: by 2002:a92:cb42:0:b0:359:4223:5735 with SMTP id f2-20020a92cb42000000b0035942235735mr7319601ilq.8.1699000405433; Fri, 03 Nov 2023 01:33:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699000405; cv=none; d=google.com; s=arc-20160816; b=Cyn+JP9CnOD5eepW0D4AeNPl5nlcvPvGdCkRJlA6xmaPI8ux8IC63+PVG9KBK2EzBG kj51QZShceNvWjR336hyxLeo5bolz51tTOf87b8VaqqvdBOLuB1kIDKhQH3nMSfb1weZ X/npCUlYmstxUlYg0JwqdcQwOp+Danjea/Z0wcxprlQ+yJ5Zc0VLVhGIAZCaJfO5co4P LxnhMmjrsDoCOWIsC39o24NKJOFvuZo/KU68ll01P7T5JswHSuwo0weHKe4hioYhvHnQ n6kbG4ygoHWYgqBe/EDm7KOGVshudc1/w5E4yf8ZkhOmzSRSm068h++cqtGEHv+F4p7o vRSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Wp17WuHUdeb51slv3IyJGWu/tgpocAsbkT4jnSaKbEI=; fh=4xc6Ej83TorJaQZCEpCfM4H5kStNKXcKPZv/vnSz5hU=; b=dlecxPbpilf0GyDu1W9mi79DE1a662Fg+a5BkIzNizyFJJ9SRp2ujBRNVYYHLuAnms wzXsG377RIGnRLrYieq/tYlwZg72APe8l8nHHoCJTK3hAx05EYHMBR+6lXcbTm6C/RbP T7QloB24yL4MQZK8tM9lnS8VutLnAlb5/Hi3Gy3gk1J+pcfaRCtAbE0vB/sLK82fmc8W 6wSB1qocys5dKUWe6HcT375Z+5qIIMjkIlINObrIFBx1h/jJtUDu85veEH6WkU76ooBt Cem4x/XbYRbRGj4/F/Q0dumrHjnqFdSaYbf4CelTlFvyU/1eJdhN0YXt2h0H+VxsOiRS YGsg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gentoo.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id x64-20020a638643000000b005b8ebb76177si1140320pgd.561.2023.11.03.01.33.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 01:33:25 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gentoo.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9ADD18020701; Fri, 3 Nov 2023 01:33:22 -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 S1346280AbjKCIdM (ORCPT + 99 others); Fri, 3 Nov 2023 04:33:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235683AbjKCIdL (ORCPT ); Fri, 3 Nov 2023 04:33:11 -0400 Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F45F182; Fri, 3 Nov 2023 01:33:09 -0700 (PDT) Date: Fri, 3 Nov 2023 09:32:58 +0100 From: Guilherme Amadio To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: Re: [PATCH v4 03/53] libperf: Lazily allocate mmap event copy Message-ID: References: <20231102175735.2272696-1-irogers@google.com> <20231102175735.2272696-4-irogers@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20231102175735.2272696-4-irogers@google.com> X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: 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]); Fri, 03 Nov 2023 01:33:22 -0700 (PDT) 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 > --- > 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 > >