Received: by 2002:a05:7412:b92:b0:f4:e171:4568 with SMTP id v18csp583987rdh; Wed, 29 Nov 2023 17:26:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHi6rbjFybvt7qkghnzXcsQ70b5lAvfFWWNKMTibNbYraQ221B3nUrYaKvTACvy5UBhLhju X-Received: by 2002:a05:6a00:1590:b0:6cc:b448:3652 with SMTP id u16-20020a056a00159000b006ccb4483652mr16150500pfk.19.1701307572554; Wed, 29 Nov 2023 17:26:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701307572; cv=none; d=google.com; s=arc-20160816; b=R2N9pVk53Ey/OaNNXQeKFhnrc8TogwnUMLI6VGG3ekO8sPrfNZgY+rM6R2orbfAMyS EGd4Z/lVmT2BG8AlI5xfmlyexkjfdcSnauUpR/Hogu7WTpTGJ6bk2Oatr+EGOijDJ2kK FS5CbtriD2FJt360ejOnAzuIYSq15LLJc7fwjb23W7NRltDyylagwWkciAwR1SIV9q0C eK8NY5mAGunekr3EfrAq9D+n5ouEinBzkDwF25bMGstJIQ0Qq8BJekq8pEW7EKCk9khI Sy+w7Iaw2gM9VZP1wkGRgqyMX17Db7eqH7JZmAGGA6WnLqCTQ1m6qIf8V+PVHSaipOMr VAlQ== 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; bh=e7enINy8ZlAxrlFxD5DxSiKfr9d6Mj6wTrKwUDm07w8=; fh=aWa9tyepd0GWyBFjkZa5ys7uNwpZ1AjfSj0Mu1iuQbE=; b=OwNxke0TiqMAe7O/nkhJLAx6zf+FC12dy8RYRVsPZnOFWNeIGegiuq8VOphqF0WgML 5fZ70m8c55MNhHL0MQEzTJGImW651mZH9LthVtYgmObM4uPdAY+APhndCPNisEVb7Idr bWNh1P0r4ecGT8KiIWl+Ca0GyQ6QQubeF/oQh5b0oBqBx3dE27pGwiK2lhT0F50QmHR+ GaDHs+OIMHKopuqmZqnGIrzEQ7+PcknPx7SxQMRK5Ouda8VX7TTnfYsvlX/igPY9lxQi JwNk3LULe9QQoleipIYS1jSdLO8XKRIXAax+oGWu2gIvPcOBqO3rXgsogA5qiWxwoEUG 9hbg== 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:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id p23-20020a056a000a1700b006c370186331si70581pfh.218.2023.11.29.17.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 17:26:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CEBAF808E8E7; Wed, 29 Nov 2023 17:26:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231617AbjK3BZy convert rfc822-to-8bit (ORCPT + 99 others); Wed, 29 Nov 2023 20:25:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbjK3BZx (ORCPT ); Wed, 29 Nov 2023 20:25:53 -0500 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74A8510C6; Wed, 29 Nov 2023 17:25:59 -0800 (PST) Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2839b418f7fso503293a91.2; Wed, 29 Nov 2023 17:25:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701307559; x=1701912359; 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=zXtHLSq+EKlXiK3+DIMCv+P/V741jLaA1RjUhoggFmU=; b=Qr4qYWr3yWT6JK5ClVizV3aFkkYOijAunQiCHU1no5jqn7jB+/gXAefkaBE+LEy/Wp MSqwPHnkwFCePuJKNLLHSp+Qu60fsFncQEyPlXwtx1xQCxSiE02eGSRhCQxJ+5vLkkDF hvxPCcW4JT/rC3lxPX2DRskckc1WfYc4D3skZ5i5wByMTN6He42p5DnHqrLWxvQWkNa4 3PXl411tGGIIqr8tJNke/TStoDXkjZIZvz3M5OfAduBhi8fdVJzXyKBv/PR3xYZCtWWU mo6YmFO/P53Xs2KmGEUTmXH6B15FPuP30OguHn9gpXtro2mBEYDn5+WIbeLW4O+65ORE IbsA== X-Gm-Message-State: AOJu0YzIV4X2KauNXALsTITA4OTuMsVY0Jex7n8jrquPxiNDtthhPBl9 F14odfjn5CMkWvN7Fc/ifgzcjMDt4ZzOZ6wHxvw= X-Received: by 2002:a17:90b:3c2:b0:285:772b:91a3 with SMTP id go2-20020a17090b03c200b00285772b91a3mr20174672pjb.27.1701307558816; Wed, 29 Nov 2023 17:25:58 -0800 (PST) MIME-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> <20231127220902.1315692-3-irogers@google.com> In-Reply-To: <20231127220902.1315692-3-irogers@google.com> From: Namhyung Kim Date: Wed, 29 Nov 2023 17:25:47 -0800 Message-ID: Subject: Re: [PATCH v5 02/50] libperf: Lazily allocate/size mmap event copy To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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, Guilherme Amadio Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=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 groat.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 (groat.vger.email [0.0.0.0]); Wed, 29 Nov 2023 17:26:10 -0800 (PST) On Mon, Nov 27, 2023 at 2:09 PM Ian Rogers wrote: > > The event copy in the mmap is used to have storage to read an > event. Not all users of mmaps read the events, such as perf > record. The amount of buffer was also statically set to > PERF_SAMPLE_MAX_SIZE rather than the amount necessary from the > header's event size. Switch to a model where the event_copy is > reallocated if too small to the event's size. This adds the potential > for the event to move, so if a copy of the event pointer were stored > it could be broken. All the current users do: > > while(event = perf_mmap__read_event()) { ... } > > and so they would be broken due to the event being overwritten if they > had stored the pointer. Manual inspection and address sanitizer > testing also shows the event pointer not being stored. > > Signed-off-by: Ian Rogers Acked-by: Namhyung Kim Thanks, Namhyung > --- > tools/lib/perf/include/internal/mmap.h | 3 ++- > tools/lib/perf/mmap.c | 21 ++++++++++++++++++--- > 2 files changed, 20 insertions(+), 4 deletions(-) > > diff --git a/tools/lib/perf/include/internal/mmap.h b/tools/lib/perf/include/internal/mmap.h > index 5a062af8e9d8..5f08cab61ece 100644 > --- a/tools/lib/perf/include/internal/mmap.h > +++ b/tools/lib/perf/include/internal/mmap.h > @@ -33,7 +33,8 @@ 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; > + size_t event_copy_sz; > struct perf_mmap *next; > }; > > diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c > index 2184814b37dd..c829db7bf1fa 100644 > --- a/tools/lib/perf/mmap.c > +++ b/tools/lib/perf/mmap.c > @@ -19,6 +19,7 @@ > void perf_mmap__init(struct perf_mmap *map, struct perf_mmap *prev, > bool overwrite, libperf_unmap_cb_t unmap_cb) > { > + /* Assume fields were zero initialized. */ > map->fd = -1; > map->overwrite = overwrite; > map->unmap_cb = unmap_cb; > @@ -51,13 +52,19 @@ int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp, > > void perf_mmap__munmap(struct perf_mmap *map) > { > - if (map && map->base != NULL) { > + if (!map) > + return; > + > + free(map->event_copy); > + map->event_copy = NULL; > + map->event_copy_sz = 0; > + if (map->base) { > munmap(map->base, perf_mmap__mmap_len(map)); > map->base = NULL; > map->fd = -1; > refcount_set(&map->refcnt, 0); > } > - if (map && map->unmap_cb) > + if (map->unmap_cb) > map->unmap_cb(map); > } > > @@ -223,9 +230,17 @@ static union perf_event *perf_mmap__read(struct perf_mmap *map, > */ > if ((*startp & map->mask) + size != ((*startp + size) & map->mask)) { > unsigned int offset = *startp; > - unsigned int len = min(sizeof(*event), size), cpy; > + unsigned int len = size, cpy; > void *dst = map->event_copy; > > + if (size > map->event_copy_sz) { > + dst = realloc(map->event_copy, size); > + if (!dst) > + return NULL; > + map->event_copy = dst; > + map->event_copy_sz = size; > + } > + > do { > cpy = min(map->mask + 1 - (offset & map->mask), len); > memcpy(dst, &data[offset & map->mask], cpy); > -- > 2.43.0.rc1.413.gea7ed67945-goog >