Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1512256pxb; Fri, 20 Aug 2021 07:15:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcVE6qh+wFwe71j4YYIUb0j9M3YPM1OMxgS+wldOWOVw8frFKCEYaclvu55HoJTgJcWc13 X-Received: by 2002:a02:3312:: with SMTP id c18mr971314jae.77.1629468934534; Fri, 20 Aug 2021 07:15:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629468934; cv=none; d=google.com; s=arc-20160816; b=YreCI1F+wK8gZezlvaiRjBcYdui7jxd2QGhALgC47zBHT4Df1RDDCzTbsbP5bDlImD dPL+g+ZxRX3eMq+jC0woq7u14DiPIuGnGXS5Iv59p5WqvlbWSV/7EuQTrn8FeAWPtvGt pTC+6ewlN2ETSYj5CB/oNpqWOUdJ1WcwxB56mDfRs2JnLQBswJisNPoHZoswrhCn+8pJ RW56F102a8heJ4aYz7xLTlYaHaZ6FCBdZNUK5wVQEySHl7axeELCT5VABz4GtuIIpnsD G4+wczaL+ZNTcSfnw58X4SYh8M7S9Z2R8muBZrySoR047f6laWnCiwBY6oggH19lbT+A +TPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/bcdothJD3yTUFOUvyqONSRss8Z1rQ7p5jtZhfXhT1A=; b=YTAiCWK6CrJPZ8czUoti+FhBi6cBAahMQVHCnfK1RH5iZGN4JR8Ct21sfHdYUr9fPp 45yyFyxSeSjJTsGdwDS3p7BkJA7/Cye2blFyt2EmN5+gdL4yeDQbpW65NqjMG7it8NrV 4Di+3fo6PdDs0HWAZJ7cQHtG8FFDkrjbNM+kpy8J2CTGUWoNqtTGqGQMyjIcgn5FI0mg UwQf65tgCl+Gy15gyINsS9I0tuA+jU6JIbnqn/kOwNYIqD7MEAXkDSE73oFwtKkUoqkl 1bO8IojXLF0CJxD/wcBkNc6t1HbsxFUllBxchA6xP2vAmhToLCoa1Lm8zW4LPJH7DhRF Z3MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fCrKTzf8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g2si6338769iob.28.2021.08.20.07.15.22; Fri, 20 Aug 2021 07:15:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fCrKTzf8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240814AbhHTOOT (ORCPT + 99 others); Fri, 20 Aug 2021 10:14:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:35832 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235032AbhHTOOS (ORCPT ); Fri, 20 Aug 2021 10:14:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 91E4C610FF; Fri, 20 Aug 2021 14:13:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629468820; bh=nLRJd2BhPGg9x5pL4Sm4XdH/2597fudaoCp7mLsY0TY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fCrKTzf8Uuqw7spFPDmtpk/PXKoqRrk/tfMb0JspaII+WxB4lDECM0OwjfPmh9qty 55W/OmUQs8cZbg/52TdDk8pd9uaJNC+nXT2+iSmMl57QFxZDW2AHDHohR+bGUhBUQj f9yIRokHTymxNeQQJrmEih6CsVpHrtwVH7Ygx1LGy1W+1dvlhvp5uwn8k7N2sO5wTa c+aoxU1pJCA+F5D1WpUZjMpohReAqs9kgH5AhF0/gx9A3j2JfxdwEIf1LmOzn5fWzX KIY566rVzXimtaXQzXFG+UHbSoR/qCkc9AkIM8ORsTDbdUv/rn/caWjWQbunMLR0Aw zgnrEud+bfzHA== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 027754007E; Fri, 20 Aug 2021 11:13:36 -0300 (-03) Date: Fri, 20 Aug 2021 11:13:36 -0300 From: Arnaldo Carvalho de Melo To: Riccardo Mancini Cc: Ian Rogers , Namhyung Kim , Jiri Olsa , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Jin Yao , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig Message-ID: References: <20210820130817.740536-1-rickyman7@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210820130817.740536-1-rickyman7@gmail.com> X-Url: http://acmel.wordpress.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Fri, Aug 20, 2021 at 03:08:17PM +0200, Riccardo Mancini escreveu: > Acaict, perf_home_perfconfig is supposed to cache the result of > home_perfconfig, which returns the default location of perfconfig for > the user, given the HOME environment variable. > However, the current implementation calls home_perfconfig every time > perf_home_perfconfig is called (so no caching is actually performed), > replacing the previous pointer, thus also causing a memory leak. > > This patch adds a check of whether either config or failed is set and, > in that case, directly returns config without calling home_perfconfig at > each invocation. > > Cc: Jiri Olsa > Fixes: f5f03e19ce14fc31 ("perf config: Add perf_home_perfconfig function") > Signed-off-by: Riccardo Mancini > --- > tools/perf/util/config.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c > index 63d472b336de21d4..6ab670cdf512507e 100644 > --- a/tools/perf/util/config.c > +++ b/tools/perf/util/config.c > @@ -581,6 +581,9 @@ const char *perf_home_perfconfig(void) > static const char *config; > static bool failed; > > + if (config || failed) > + return config; > + > config = failed ? NULL : home_perfconfig(); humm, why keep the above failed test then? > if (!config) > failed = true; I.e. please check this: diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index 63d472b336de21d4..4fb5e90d7a57ae48 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -581,7 +581,10 @@ const char *perf_home_perfconfig(void) static const char *config; static bool failed; - config = failed ? NULL : home_perfconfig(); + if (failed || config) + return config; + + config = home_perfconfig(); if (!config) failed = true;