Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1700097rdf; Sun, 5 Nov 2023 10:13:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkdM9GUrLFQBkghMECV5k1p4vlmVikUnY4JXEdp/RyjwAb8qyeegf91f6ooVL6J7ARLHD+ X-Received: by 2002:a05:6871:8108:b0:1e9:bba3:4902 with SMTP id sn8-20020a056871810800b001e9bba34902mr29551832oab.37.1699207990169; Sun, 05 Nov 2023 10:13:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699207990; cv=none; d=google.com; s=arc-20160816; b=Kk1be7mmhuCF90L3lA/X2wXWG5VIDyEyJ0wi1cYvg6PZWT0SuBYn9sS31KpXygDior K0l6MloFPyLeEuzaq9K9kJI7LDzlfA4+B1MdKfE4Dh/mNBdPPhTWbLyylsltNXpdIHs2 0N1jlygk+dbM5GXlcX8NIetoIJA4aIdgfJbuycQ5z4B7uXPFTWxcW6a6oS8bfJUdtRC7 F5DcKQfALawrECOBePpOpUfI/t2K1czTXwMZBYIu7Xxz2u3SISQfXvkWzw9QnVfQRH5w 0qxtXqt7rBgGVfmJniWA41+EoQ7VzKTd4K2eyG+mKPQA+AOIljqFaZ0cPbGk1JloG54+ 7lPg== 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=hptOT84P0bVaJ6kbN+tDwsYdWGi21pttLP8FnPtU/zs=; fh=73xcvFW+hwxwGpiNqeGXx+z+vU6hBf14MTy6l3R1j4M=; b=TjhbEjXoar4FIz6ApjqA5p/ZrTvLn7uRMbO8/3ZT2ueMsclvxdjUHfFFMhFXu6bURR ssstU53KRAu2klhamt0BjBbnveA5uyHzjhrf9gpBQHR7+GDn/8d/nabzVDFmaR/i/pmD gwRXPPd0E8XCTGVmTB1UGgkeBg91720qBSOB/os8be/phFMd/p/xUZiujrRnO21NFhF0 3Jsbun7JxwZpQqCRLK4J0IIhV2fhN8lGUcQOpuYscayKrLwpvV2wxC8H6V91vxVBu3Yn ywFK2Jiw/0MU9HSoMNT5g8rTQNnLkqdsde0l/BHkNWm529+zPfGpF4HW60rnnGtVcpHk Wb0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id c3-20020a6566c3000000b005824bad8f81si5982357pgw.853.2023.11.05.10.13.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 10:13:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 83509805B9DF; Sun, 5 Nov 2023 10:13:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229496AbjKESM6 convert rfc822-to-8bit (ORCPT + 99 others); Sun, 5 Nov 2023 13:12:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbjKESM6 (ORCPT ); Sun, 5 Nov 2023 13:12:58 -0500 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 987CFBB; Sun, 5 Nov 2023 10:12:55 -0800 (PST) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6be0277c05bso3151117b3a.0; Sun, 05 Nov 2023 10:12:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699207975; x=1699812775; 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=gE6CsEAM2XNQiggqUIgiyV/o4aLtXLLS/EW3cW4NJuE=; b=mKigFjFsKfrEgw/vzLxYwNOAWtnusM9E2V5gKRnELE4qVhY/uY2anE5Qm639Z5QebJ GbCtnnZrG9zqk60+vIFAPKEngU4FxyDZT50deuxlhfX7XZyExkA/6p1eWg5kRTW3a/hl BfM0Awh1e0Fqy+wGQkDqKLaHx3FUzFW6/DKDOBFxGlsSVEzdjrw+7RpQY8QU+CkWYJxJ CnhDjXTGzOZTj9ej6ox+q0nIlitkj6aMWvRlJRGOuyrgbQ8T/vAs0NLd/k2Tc6Flwnc+ oCDmsnVZp5zjhjaUS8hY7QYoARYDee2vnCrI+tqaLJQndaG2ZbxL+LwoBTnJF6X11BfI J1Qw== X-Gm-Message-State: AOJu0Yx4TQFehD+Zl8hOJbT4RLzImZO3CyC7PoOacmRy1N0UtGOs/E0X Gt7/tMdX3ozM53DS5dId2S+PrhNWvHaMotCOig0= X-Received: by 2002:a05:6a21:66c5:b0:180:febc:8ed5 with SMTP id ze5-20020a056a2166c500b00180febc8ed5mr16760379pzb.52.1699207974972; Sun, 05 Nov 2023 10:12:54 -0800 (PST) MIME-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> <20231102175735.2272696-4-irogers@google.com> In-Reply-To: From: Namhyung Kim Date: Sun, 5 Nov 2023 10:12:43 -0800 Message-ID: Subject: Re: [PATCH v4 03/53] libperf: Lazily allocate mmap event copy To: Ian Rogers Cc: Guilherme Amadio , 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 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 agentk.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 (agentk.vger.email [0.0.0.0]); Sun, 05 Nov 2023 10:13:06 -0800 (PST) On Fri, Nov 3, 2023 at 8:49 AM Ian Rogers wrote: > > On Fri, Nov 3, 2023 at 1:33 AM 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/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 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 > > > > > >