Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp575971rdf; Fri, 3 Nov 2023 08:49:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHWRySU23gal6zmrQVYZe1PGmIyxu/CMOTQSKrELvomOWyhLk35R5eiBRBXFQB3WJ3Uo+Rt X-Received: by 2002:a17:902:e5ce:b0:1cc:5730:ce55 with SMTP id u14-20020a170902e5ce00b001cc5730ce55mr16246843plf.66.1699026565144; Fri, 03 Nov 2023 08:49:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699026565; cv=none; d=google.com; s=arc-20160816; b=gawnD8SptKvHaXdTSc3ddnrA5fHq48vNELhyG8gHMrgX0UfmJacVJRwJfTa/A4BS+I ZQXBeIenWEwtP7j4cv34J+ql8TZoFx8j+i/dhmCLIif8t0TDVi4yzpcqB7U0AAyZ5wDs A5wU9FQBhYVoy6FUdoK3jIknk0pk2CfSwQsJj4+dcyJBEhgJvJnzk8F2sHcxH07mXRRj UV+x2TCH+dB9iO3vkFMc5+MdPwx/X28fZAJ9yOfEMB2vyINaz+OLCp5R3eHGQ+LdTxyM 2+UXTKRzzmbxuxtFyAlPYK5zub/Axd4babykVNb9fVScExCb/q8mc6s5S7aPj4VWq+oF W64g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=392tRVZxGtLzEQKNmN3YbTUmZB8ld747UCZRAgobKm8=; fh=g/iD4f52tDDhzGRx80sY678wqfPGe5qKIL+BlV1OvQg=; b=yIXNljRAbL3WZ73o2Q9Rqz3azJpaFAxF/akEIoJscYMrPpuzzF0iTXsbchJVCNMDMg INvkzMrtBbOoWjYbS161JlGz4BahJNUI2TWxteklkvgj5XThbdRplQkUXy/KwFnWqfjh 2EALnSHvep3kqRn2O1aARQyn8j1LLIpTnVuVoG3j4l6TuHRLofpp9GkzZ82ZcGpGsHZN FD99mdeNW84DutHQ1tjv8OJFdzMPubfRrFbcgTFHlkSgt0mBkxehy0YpPAwM71dutAOG RE8oBQrrJ3PjWJiGuMaHNgTa0a0YiWhosbVcvJ6rVh20miTiZPqyBXHhdE8Ajao2R0gT msPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UZKx1CqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id i5-20020a17090332c500b001c62cfff795si1803012plr.429.2023.11.03.08.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 08:49:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UZKx1CqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 08F7C81C3D5E; Fri, 3 Nov 2023 08:49: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 S1343554AbjKCPtO (ORCPT + 99 others); Fri, 3 Nov 2023 11:49:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230110AbjKCPtN (ORCPT ); Fri, 3 Nov 2023 11:49:13 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D2EBD42 for ; Fri, 3 Nov 2023 08:49:10 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-50931d0bb04so4511e87.0 for ; Fri, 03 Nov 2023 08:49:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699026548; x=1699631348; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=392tRVZxGtLzEQKNmN3YbTUmZB8ld747UCZRAgobKm8=; b=UZKx1CqInR3ZeR64N91T0FSZEPRZo1d1rEecXwYALVpexcC2FJwf91mbbc63ZOfcEY 8X7OlfWW72Ux4QbdmuthKz7KYFyW/zKhWwbnOt6sZhpW7R3XoxPpE5jQuL/CYOzjLArx ColqZoYu3wXV1VN00nzqihQKVnVLWuy4jkSgXiRK2kSiO0do/6/OXb2PtuKPkijEnR2w H+d7ZTK8S2QQ8mPKIurYbjJHX2/YsasFcxl9BVuqQc4Y8ex+tARiBU1bEXsCvGW8b6jy +4l4yywj+D8+o8NO1qbUabZ9hXaydgqwBaSGrhQVaKsZtITueVoF2rwfwkR2Qs3NohX8 8yUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699026548; x=1699631348; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=392tRVZxGtLzEQKNmN3YbTUmZB8ld747UCZRAgobKm8=; b=OAX2JnP4C5saxGEIQd967z5utdejCUYDScDlZvnfXlqQahthRA3wkrB+Yp7Rnkv+9D FysiOxHMyTK3YrNgwp1FdMMA0LwFVvl/zIbMpg9UFITcnUq7cNtFP2zr3iiYpKQnne7n SGJ6lSOZIl17I380wwEjvbCPzIyV5A0IOaA38M3TjZsM/WW2cYa7SFBUVwK0yag5JjeA tbJ9no1tHPiUnm84SzvG+/1kmc7t2hw88A9pJth8GEc2TmvfpTC9mta64QR3mUsIICtL y0aX4vIg5KkRH5ounuPKbBWJAfVrGorbGVITA/OpGbFcQv90G2nUYl4HXn+ypKhzCDEK 9EFw== X-Gm-Message-State: AOJu0YyJq+bKqviR6kGWInTWVfeiwcu3gUUBiot4vp1iXel4boyWKVlE b7G+NZqiONYEpz5M5q3WQauyd73twKjrbxenaAqkog== X-Received: by 2002:a19:9153:0:b0:501:b029:1a47 with SMTP id y19-20020a199153000000b00501b0291a47mr120339lfj.1.1699026548121; Fri, 03 Nov 2023 08:49:08 -0700 (PDT) MIME-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> <20231102175735.2272696-4-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Fri, 3 Nov 2023 08:48:56 -0700 Message-ID: Subject: Re: [PATCH v4 03/53] libperf: Lazily allocate mmap event copy To: Guilherme Amadio 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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: 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 08:49:22 -0700 (PDT) On Fri, Nov 3, 2023 at 1:33=E2=80=AFAM Guilherme Amadio = 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 > > --- > > 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/in= clude/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] __align= ed(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 per= f_mmap_param *mp, > > > > void perf_mmap__munmap(struct perf_mmap *map) > > { > > + free(map->event_copy); > > + map->event_copy =3D NULL; > > if (map && map->base !=3D 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 =3D NULL; > if (map->base !=3D 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 =3D NULL; > > @@ -226,6 +228,13 @@ static union perf_event *perf_mmap__read(struct pe= rf_mmap *map, > > unsigned int len =3D min(sizeof(*event), size), c= py; > > void *dst =3D map->event_copy; > > > > + if (!dst) { > > + dst =3D malloc(PERF_SAMPLE_MAX_SIZE); > > + if (!dst) > > + return NULL; > > + map->event_copy =3D dst; > > + } > > + > > do { > > cpy =3D min(map->mask + 1 - (offset & map= ->mask), len); > > memcpy(dst, &data[offset & map->mask], cp= y); > > -- > > 2.42.0.869.gea05f2083d-goog > > > >