Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427161rdh; Mon, 27 Nov 2023 14:09:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5lpMkS1TAqEFQdaEUUo5ve8IsUscXyfheNT3yqUBGZb4QyxjqjSCNDPTavPrZ5FMsFu0e X-Received: by 2002:a05:6808:1152:b0:3b7:673:871b with SMTP id u18-20020a056808115200b003b70673871bmr20704482oiu.0.1701122993996; Mon, 27 Nov 2023 14:09:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701122993; cv=none; d=google.com; s=arc-20160816; b=UaJuuM4CRFMbWX0JPKA3XF0+lB//5Bqep4ZodUdrZx73F4c9Y6uSa/QM5N7/jPaRAG 0uF890bdgxYDMHFGx3/QzedYroVL28RgC1H0reCjkPsSGLgl2j3GLp7dAWCTMg8QO4X3 iyi4vsJu6d4iPuByeEOEEcMmF5oPT92TTr8VwG8pL021BE+NLV3smu67ZqcbaCfUfxtR 0wHz6w6e/DgHXOHeHTnV/r4a5+dFll3oKhNFXOwtysEDTU5T8Xc4bj75quI08O3dQwuG luoXG1e/md1jLAYqqpPvyv6eOTddwSb1KeBbf+LhorpdFiFK0W6jyFacrFZZ7Olcf6aL 7onQ== 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=LW5iu3CW2lKtmsJ3ZW+qTTKgcFmtY4yDP5ns7rJcimg=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=zZ7TgNxogew37ulb++N0HaSmmH71TUbx0nvGZg5fT1j7AFJaic9Ry7ijIeI3+/M4wU vb0ncaTjs3JJ5Zd7nbxnML4YjML77eE3ffLstBpW46hP7KNgj8m2i2vhHYxnI26gPUf6 kQeHWtjTYpmytUzgilTWITBpdg1xqw1+Ekoug+n4fsmAW7ekg2DIvoi8rOxZLKeU4Ds4 JOS6f0j92fha3/9HeN6AJFQv1VkCu8Cchs0sd1T90GKF3hiOh/PYNOSb3AGUY51lA4OY UD0zyBbHf3DkjWI7jH1WDSJ+hF7uVMk3Y0RzH4OZxO0t0lJgk+mNScixGhSbHtKFWz1H XqCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ik6rg0Ri; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id l3-20020a63f303000000b005bdd8dcfe21si10347674pgh.195.2023.11.27.14.09.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:09:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ik6rg0Ri; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id D287C80FFDBC; Mon, 27 Nov 2023 14:09:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233569AbjK0WJX (ORCPT + 99 others); Mon, 27 Nov 2023 17:09:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233553AbjK0WJT (ORCPT ); Mon, 27 Nov 2023 17:09:19 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF53A187 for ; Mon, 27 Nov 2023 14:09:25 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ccc8b7f578so74965077b3.2 for ; Mon, 27 Nov 2023 14:09:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122965; x=1701727765; 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=LW5iu3CW2lKtmsJ3ZW+qTTKgcFmtY4yDP5ns7rJcimg=; b=Ik6rg0RijjlMXBcDelVnY03go2HXnrP5sD7WhESFH5F/0vwbI13yht3ikAZshpcvXg k3aS+LIdf8VG0dGQsLGTpj0DQJwhGh4I0eA9wgkwtJKaGKSu61P+8jcdTYSwEzb9wg0b eoQAJ1Ju3T+JRd7WZFwyaHcFQG7TML7EZcERn1/e6IwuR7fmURTKijxkVal2aeE785x7 Yoxlfxmyeu97NOVIzXO9ZPcZswVSpqQw3VDvGbS9kDZ45gzJBckEswOngkkm6X5+SzdG oGlYlqQ1bYsMp+p9YgOQALga+8AUEpeNJFwqunzxmEDxit1GrBioTTdX4M2RnvwwQbsE A7JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122965; x=1701727765; 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=LW5iu3CW2lKtmsJ3ZW+qTTKgcFmtY4yDP5ns7rJcimg=; b=IauzdJzBAIGnhiglOd6nWLpFsGvO6nQjP+Zwy0bQ7J3H5IM8aiP5GgAspEgCW6zepq PlcPE0qsi7rNEBwbC8VekpMTzUxwO+oQYGJYKb1xWvw0SrRRG0HlJHf6rk3YXyW0R0tl dsfhloOUpApnv0ahsZhjFv8/ZVMd/cahB0TGKkTBNAAXdTTnP/+b+oSdVcBDUSgdm9SU 9zpbDoFGPiv2LEQRXgKNd/zx8uG7tcyknwsJaqtxzyphTsj94E+oclxFMWaSqOs4YEAk Tgr+YksfseFlLkYzVwOLO4Vq5yCv9weMwhKDVIW5iVyAQh2M76wa7UPiAtQ12o/rier3 mCRg== X-Gm-Message-State: AOJu0Yww6Sctqt6Ia4J5kTb84pyuzlqSX6GiqVeUuZe2ZfyPfbRojwEO 63UXqhENICVDaMZF2hen1NfxzsMS0hWL X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:d114:0:b0:dae:b98c:6ffa with SMTP id i20-20020a25d114000000b00daeb98c6ffamr375100ybg.8.1701122964882; Mon, 27 Nov 2023 14:09:24 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:14 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-3-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 02/50] libperf: Lazily allocate/size mmap event copy From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , 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" 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 morse.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 (morse.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:09:50 -0800 (PST) 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 --- 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