From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (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 EAE0441360A for ; Fri, 5 Jun 2026 19:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780686927; cv=none; b=po5oPfrnbD/20Y0Nstro3aqMxx1KUiPLKtjTdGyqbbAyaeEbRMz15cY+vLV6cJrdZjf8jiDlaE2rnRlhShVG62fGz6AWcdocbo8Itvfvu9HItvaI/5mSzYFns3cLJKXyTLTPim5/ZI88KHUdMieWZ1jDdtXsPAiPO2LehrYjXT8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780686927; c=relaxed/simple; bh=taSKB9BzdfFFc5okG7SIw+6aW6dC+xrRCx+VI9z577Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bi+NVvp+f+y5kROPjWUKRdMNz62uDjCf4sOcmQHK1V5fsvfNrCchbzXoUUf8BpyQMferLnQrVhX0fPYI7iFtVECdZOacFSFxXIWBVnSbizSg2FY6rJrf5DRS5lnFQKOdsqwDc7VmPNTiAc4fhfeO26F3fxtJpMZ/g79qs/LoI+E= 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=mAO+l4tj; arc=none smtp.client-ip=74.125.82.74 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mAO+l4tj" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-137d4309062so5920808c88.0 for ; Fri, 05 Jun 2026 12:15:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780686919; x=1781291719; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b1KCxLzm5r/4/75FpnIjDCoJZ2/GF5PCnxsr/c+/xOQ=; b=mAO+l4tjFeMpWiEfjhIPK/al/q+XtFj8dXiTwQy7pB1Hl9R8EYGsfFaZ7i0JHcH17X HchyHMyPMz+VscNf2ElI0eoV9lKk6l6TlYDFeNfjfibnIMTiJtWPh55poachXAIfvvGW 4f9wvAUyRpEWmDgw7kIiPve6tc5dDosO07bzEjHeVFrbbB6qfy73r20piKCTugplvIud 31+DeN49YTaH75KDIbtolVG9EJr0c19m5mG6Rj/GlXYQBbLqrFrBLjftphB4g9IQzYZA 2hwe6MVP4xVjoqoXjLjMY3RMFw20L8+6JP8okNu0h41Dv4NeIxmdtCfsFn6IyAbxLeCK g86g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780686919; x=1781291719; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b1KCxLzm5r/4/75FpnIjDCoJZ2/GF5PCnxsr/c+/xOQ=; b=krA2UqsLU1OYsr1hHUpBPsfurNqjRDDhlCGlGkRL4nbi+HYD4N0ZeU8/Ltpzz+WF2I GD/ii6DjCREVoKrWYwxyvCzmxNkvJez2/l9P3pZGzKSfT7zCngSjIn2woXZ8F/c8vn7N Wpi1j2L8uDJgWOXY9x28OVc3qD3Vf3xXyRipFeS9oh+RyvcUVfEAVMV+ZLK6mMfKUKRM Zx3BZzTwuNBlyl5bwrbF/B4aZfiYj/T7qkmmb7MKktqvBPlcVLVXIZZlz+ceXWakbiXj GiDvmKD47qhsu5fRHCq0rXlNopnC6+hk8c3OH6xTVCUxmEmuSxoy4+SaeYRjsY7H5IjU 3dIQ== X-Forwarded-Encrypted: i=1; AFNElJ9fK8GoFNLHMleiXOSfKwC+iAbWgPxOPcDCvR7XhCkUApOFFow1bvX5uMucVwQu+5kqLxfXi7heQ8exz67enPz+@vger.kernel.org X-Gm-Message-State: AOJu0YxOzCEaYYnSsIHD5hGDxaADwhwBEfO7Wpm0DeGp8m/u8rh1MUTr E1MjnOtHUXbmA2J9zq12dPs9o0/yJTNs62skXO2uNpTx7+QUspGDOhQdzIvw+hQXmn1in3getZB Jb9ShvWEqjQ== X-Received: from dlcip7.prod.google.com ([2002:a05:7023:c087:b0:138:d5:834e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:ef18:b0:135:6914:87e1 with SMTP id a92af1059eb24-13806668735mr2485588c88.6.1780686918592; Fri, 05 Jun 2026 12:15:18 -0700 (PDT) Date: Fri, 5 Jun 2026 12:14:45 -0700 In-Reply-To: <20260605191500.2477026-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260605074003.1730715-1-irogers@google.com> <20260605191500.2477026-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260605191500.2477026-5-irogers@google.com> Subject: [PATCH v11 04/19] perf data: Add open flag From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Type: text/plain; charset="UTF-8" Avoid double opens and ensure only open files are closed. This addresses some issues with python integration where the data file wants to be opened before being given to a session. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- Changes in v2: 1. Fixed File Rotation: In perf_data__switch() , I added data->open = false; after the file is closed. This ensures that the subsequent perf_data__open() call will not exit early and will successfully open the new file. 2. Fixed Memory Leak: In open_dir() , I added a call to zfree(&data->file.path) if mkdir() fails, preventing the leak of the path string. --- tools/perf/util/data.c | 27 ++++++++++++++++++++++----- tools/perf/util/data.h | 4 +++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c index 94dc534a7386..33fd1b82019e 100644 --- a/tools/perf/util/data.c +++ b/tools/perf/util/data.c @@ -345,9 +345,10 @@ static int open_dir(struct perf_data *data) if (asprintf(&data->file.path, "%s/data", data->path) < 0) return -1; - if (perf_data__is_write(data) && - mkdir(data->path, S_IRWXU) < 0) + if (perf_data__is_write(data) && mkdir(data->path, 0700) < 0) { + zfree(&data->file.path); return -1; + } ret = open_file(data); @@ -360,9 +361,16 @@ static int open_dir(struct perf_data *data) int perf_data__open(struct perf_data *data) { - if (check_pipe(data)) + int ret; + + if (data->open) return 0; + if (check_pipe(data)) { + data->open = true; + return 0; + } + /* currently it allows stdio for pipe only */ data->file.use_stdio = false; @@ -375,16 +383,24 @@ int perf_data__open(struct perf_data *data) if (perf_data__is_read(data)) data->is_dir = is_dir(data); - return perf_data__is_dir(data) ? - open_dir(data) : open_file_dup(data); + ret = perf_data__is_dir(data) ? open_dir(data) : open_file_dup(data); + + if (!ret) + data->open = true; + + return ret; } void perf_data__close(struct perf_data *data) { + if (!data->open) + return; + if (perf_data__is_dir(data)) perf_data__close_dir(data); perf_data_file__close(&data->file); + data->open = false; } static ssize_t perf_data_file__read(struct perf_data_file *file, void *buf, size_t size) @@ -457,6 +473,7 @@ int perf_data__switch(struct perf_data *data, if (!at_exit) { perf_data_file__close(&data->file); + data->open = false; ret = perf_data__open(data); if (ret < 0) goto out; diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h index 8299fb5fa7da..76f57f60361f 100644 --- a/tools/perf/util/data.h +++ b/tools/perf/util/data.h @@ -50,6 +50,8 @@ struct perf_data { const char *path; /** @file: Underlying file to be used. */ struct perf_data_file file; + /** @open: Has the file or directory been opened. */ + bool open; /** @is_pipe: Underlying file is a pipe. */ bool is_pipe; /** @is_dir: Underlying file is a directory. */ @@ -59,7 +61,7 @@ struct perf_data { /** @in_place_update: A file opened for reading but will be written to. */ bool in_place_update; /** @mode: Read or write mode. */ - enum perf_data_mode mode; + enum perf_data_mode mode:8; struct { /** @version: perf_dir_version. */ -- 2.54.0.1032.g2f8565e1d1-goog