Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp45627lqo; Tue, 7 May 2024 11:37:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXMi/zF7qoEJ2tbMyqpt7zJghAR03JmLC5XbgN6sZQIwY3hrA57smwA++oW+SIiLp9v0tvj7DIrKEjNJofAeAOUY2mIcFkAb1MVvpnz/g== X-Google-Smtp-Source: AGHT+IE3bCvnWPH/XlLifJFaOXNnKrfhB6+/FyqDrTF5Ibl/fkcY0dT6pJtrTfjm7gXEqdque/pC X-Received: by 2002:a17:903:2444:b0:1eb:3df4:e63b with SMTP id d9443c01a7336-1eeb01837b1mr6751585ad.7.1715107071534; Tue, 07 May 2024 11:37:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715107071; cv=pass; d=google.com; s=arc-20160816; b=d0bOCXa1nVczov/A/FCP7tHnNFtR1ZLyNx6eXbYprxS4P4wWlJkE+pfYxulrB1nb6m NBmESJROxHCbXbzYBnPKHRg8ZKYO2vfyWSPdFfitZb1cPIMNl3fFckIG8iIW9cEZHBce BKHtEzrKbjLgaCqfxY/GzoU2MQU/2H5qKYGpCLUP4aV7EhQyGetJ3StlTy4wET9hW7Fz fBFiTNA8usoXM73Z0OwtZqKW3jcTqa7qzAw5Ua4SsikrGKyLCXZrhY3FcSH1RAdBvdH1 hTthiXrFqK3uZYmhyZleuukWlSHzI2fWq2sGdhFXqb7E8Zi8FLa6Idw2PRC+tD2YLygY wu0A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=L6D30HL2E3T95rFOwqlYE0nA8a5DhBCmO1MCVWhQ7Mk=; fh=t01uoK/COzi3nGczzYqXIQTnNRSpGRjFRV7soKC/h/U=; b=NSzURkccY7M1SueZq/+l+fzFL/XL3RMoj6fTI78G4xVfKPenP2IocNPAH0osMRyuJ1 w8YU35qsAB5tF0A5kiR0yFso6pEnCJ8Wy7m5m0X2zgazQTGnqsFS7RI70Z1zV3NIzGuo XwZTKP8zGPgUIS+R0KgxQIQmsirW7yJZdoZygatndmHoW7JPmazN9sjLriDKbbCmsnwq Y8ZL5esEwAKnn28SZs86J5M79FaAWpnND/Nb4mov1fsu4Lat1tTxYp5PMu6lPLMj26Z9 TLMmg3mOqJZclGUh7sht8Y09unpyc2NlfU6Quvdmk8eppBtRjC/zsS/NWGD8iiUrOr6l hQog==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ay1tSMhi; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-171991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171991-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j18-20020a170903029200b001e589306d73si11125362plr.26.2024.05.07.11.37.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 11:37:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ay1tSMhi; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-171991-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171991-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 28C83284935 for ; Tue, 7 May 2024 18:37:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9755216F0F9; Tue, 7 May 2024 18:36:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ay1tSMhi" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAD3B16F0DB for ; Tue, 7 May 2024 18:36:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715106986; cv=none; b=eSjD8NFEe0m4tVHgbI8mdbRttgMOvH2VINRQcurTiii8jFzb0vfXYnTA1fbZMzJO8Qw/YHTrw3JF7DQnrytDKnUkzpZcpD0BXdvMy9tegEYOLUQIgGLrYt+PARdvCX603VJ43r0YiFmU5fArMnwLZ/aRmcEVyV/zOnJIZl+anfI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715106986; c=relaxed/simple; bh=CZSkYQ87LqNxKB6+cfjHvCInBypTk1PVj9bAsiWh/bE=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=T6eEDIrjIo9BAajCvtfEHHsGUzxjhcWgnJdZ1VyEcoXycNmo2+DxIqu3+u7pCvT2WyTzXOsl/To++2SVVKJ8NduEGSvJjmyU8khJzbzltlEVx2TouTIqqb5OmHxbbbFrp5WvkD0pWdU8v/Ao724W/t0C3Md+xzAez+c+6x5AMmU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ay1tSMhi; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-61be452c62bso58694127b3.2 for ; Tue, 07 May 2024 11:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715106984; x=1715711784; 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=L6D30HL2E3T95rFOwqlYE0nA8a5DhBCmO1MCVWhQ7Mk=; b=Ay1tSMhi21TTlL3dMkSsNI6cn2NMUTKxF67VxgWYUOACov7XLxuf3wOS6m1UrGCGQY 0oO5gB8kguHk8LUmqkc7AKkb3m78V+o9lWrFObW/xDmIFlwSeKylS0A4MA5X+3VIw/5Z Rc5UEv2Htr6Cmwtwo2x52wH6OfqAUXyXeKeKGbxGqV2vit3eAkvvdL28haC00mhv8gal Vs97MIdUWywFOj+au74U6XfzI81i4l+uDePd+K8neD3M926ab9l9n/aorIwo6IZz9Jt4 0n0iKpPDznKmMZYk+smJH86rmjBPySf3KfrkPTwFRXPxgDGX37xq0usi5jEPxdh5i6IK fu1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715106984; x=1715711784; 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=L6D30HL2E3T95rFOwqlYE0nA8a5DhBCmO1MCVWhQ7Mk=; b=lVltMBfIjP9Jt1koEtJ3V3bgcS8F4TxU7vaGa6HdYuWFeaATF1Yp5DdUqAB4pahj8m m0X0n64fmYbjWKmDoTv/tgi/p0R4KhDImf7GhUvXIpSb5i6Zjeu++/w39s8bn0hM/vxw tiFmYe6pPfw3oBkIeK9YiZplUUUDxA238lphunBwFNUMyjo0HcAftl/sxEKvduS99Ymo QT59sdlerTfjy+Qs2FpkWFTWZANRYfaxwz/2kIltz2yUn1vMWI/FOSnUkFQI6jxw1R6l 72tMygnBI9d8eyUya6Oc01c3r3amyiOKpxd8adL9ZkojWQZkdo++eVICip29p+V6wque /dTw== X-Forwarded-Encrypted: i=1; AJvYcCWXdykpcmTXOGLCG7WnnC5pvsP3v8KVUFmhQVsH441OU7n2W1k+pN24OnE/OGGDTao1RtML7DmOBs7Jx6SQ2Uvui8WZW18ZKwGrmdtg X-Gm-Message-State: AOJu0YwtCK/HL/h18G/sv0LaR/Euj6YPYc5xAt9QbJwRPvhqoFu2Rsly 1ow08aFrrSihhHwsh+mTJrizaoZuEZ3zhexTkE5hBTyMeBhoB2nOwBmWmV/4xHXGrE84530qmE8 LqnrGeg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:8095:fd7:9773:b1df]) (user=irogers job=sendgmr) by 2002:a25:c78d:0:b0:de6:141a:b0de with SMTP id 3f1490d57ef6-debb9d887e9mr46121276.10.1715106983803; Tue, 07 May 2024 11:36:23 -0700 (PDT) Date: Tue, 7 May 2024 11:35:45 -0700 In-Reply-To: <20240507183545.1236093-1-irogers@google.com> Message-Id: <20240507183545.1236093-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240507183545.1236093-1-irogers@google.com> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Subject: [PATCH v1 8/8] perf hist: Avoid hist_entry_iter mem_info memory leak From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , James Clark , Tim Chen , Yicong Yang , K Prateek Nayak , Yanteng Si , Sun Haiyong , Kajol Jain , Ravi Bangoria , Li Dong , Paran Lee , Ben Gainey , Andi Kleen , Athira Rajeev , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" struct mem_info is reference counted while branch_info and he_cache are not. Break apart the priv field in hist_entry_iter so that we can know which values are owned by the iter and do the appropriate free or put. Move hide_unresolved to marginally shrink the size of the now grown struct. Signed-off-by: Ian Rogers --- tools/perf/util/hist.c | 39 ++++++++++++++------------------------- tools/perf/util/hist.h | 8 +++++--- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 00814d42d5f1..2e9e193179dd 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -476,13 +476,6 @@ static int hist_entry__init(struct hist_entry *he, he->branch_info->to.ms.map = map__get(he->branch_info->to.ms.map); } - if (he->mem_info) { - mem_info__iaddr(he->mem_info)->ms.map = - map__get(mem_info__iaddr(he->mem_info)->ms.map); - mem_info__daddr(he->mem_info)->ms.map = - map__get(mem_info__daddr(he->mem_info)->ms.map); - } - if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) callchain_init(he->callchain); @@ -574,7 +567,6 @@ static struct hist_entry *hist_entry__new(struct hist_entry *template, he = NULL; } } - return he; } @@ -747,7 +739,7 @@ __hists__add_entry(struct hists *hists, .filtered = symbol__parent_filter(sym_parent) | al->filtered, .hists = hists, .branch_info = bi, - .mem_info = mi, + .mem_info = mem_info__get(mi), .kvm_info = ki, .block_info = block_info, .transaction = sample->transaction, @@ -836,7 +828,7 @@ iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) if (mi == NULL) return -ENOMEM; - iter->priv = mi; + iter->mi = mi; return 0; } @@ -844,7 +836,7 @@ static int iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) { u64 cost; - struct mem_info *mi = iter->priv; + struct mem_info *mi = iter->mi; struct hists *hists = evsel__hists(iter->evsel); struct perf_sample *sample = iter->sample; struct hist_entry *he; @@ -891,12 +883,7 @@ iter_finish_mem_entry(struct hist_entry_iter *iter, err = hist_entry__append_callchain(he, iter->sample); out: - /* - * We don't need to free iter->priv (mem_info) here since the mem info - * was either already freed in hists__findnew_entry() or passed to a - * new hist entry by hist_entry__new(). - */ - iter->priv = NULL; + mem_info__zput(iter->mi); iter->he = NULL; return err; @@ -915,7 +902,7 @@ iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al iter->curr = 0; iter->total = sample->branch_stack->nr; - iter->priv = bi; + iter->bi = bi; return 0; } @@ -929,7 +916,7 @@ iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused, static int iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) { - struct branch_info *bi = iter->priv; + struct branch_info *bi = iter->bi; int i = iter->curr; if (bi == NULL) @@ -958,7 +945,7 @@ iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *a int i = iter->curr; int err = 0; - bi = iter->priv; + bi = iter->bi; if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym)) goto out; @@ -987,7 +974,7 @@ static int iter_finish_branch_entry(struct hist_entry_iter *iter, struct addr_location *al __maybe_unused) { - zfree(&iter->priv); + zfree(&iter->bi); iter->he = NULL; return iter->curr >= iter->total ? 0 : -1; @@ -1055,7 +1042,7 @@ iter_prepare_cumulative_entry(struct hist_entry_iter *iter, if (he_cache == NULL) return -ENOMEM; - iter->priv = he_cache; + iter->he_cache = he_cache; iter->curr = 0; return 0; @@ -1068,7 +1055,7 @@ iter_add_single_cumulative_entry(struct hist_entry_iter *iter, struct evsel *evsel = iter->evsel; struct hists *hists = evsel__hists(evsel); struct perf_sample *sample = iter->sample; - struct hist_entry **he_cache = iter->priv; + struct hist_entry **he_cache = iter->he_cache; struct hist_entry *he; int err = 0; @@ -1126,7 +1113,7 @@ iter_add_next_cumulative_entry(struct hist_entry_iter *iter, { struct evsel *evsel = iter->evsel; struct perf_sample *sample = iter->sample; - struct hist_entry **he_cache = iter->priv; + struct hist_entry **he_cache = iter->he_cache; struct hist_entry *he; struct hist_entry he_tmp = { .hists = evsel__hists(evsel), @@ -1192,7 +1179,9 @@ static int iter_finish_cumulative_entry(struct hist_entry_iter *iter, struct addr_location *al __maybe_unused) { - zfree(&iter->priv); + mem_info__zput(iter->mi); + zfree(&iter->bi); + zfree(&iter->he_cache); iter->he = NULL; return 0; diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h index 5260822b9773..8fb3bdd29188 100644 --- a/tools/perf/util/hist.h +++ b/tools/perf/util/hist.h @@ -132,18 +132,20 @@ struct hist_entry_iter { int total; int curr; - bool hide_unresolved; - struct evsel *evsel; struct perf_sample *sample; struct hist_entry *he; struct symbol *parent; - void *priv; + + struct mem_info *mi; + struct branch_info *bi; + struct hist_entry **he_cache; const struct hist_iter_ops *ops; /* user-defined callback function (optional) */ int (*add_entry_cb)(struct hist_entry_iter *iter, struct addr_location *al, bool single, void *arg); + bool hide_unresolved; }; extern const struct hist_iter_ops hist_iter_normal; -- 2.45.0.rc1.225.g2a3ae87e7f-goog