From: yaowenbin <yaowenbin1@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
<jolsa@redhat.com>, <namhyung@kernel.org>,
<thunder.leizhen@huawei.com>, <rickyman7@gmail.com>,
<ak@linux.intel.com>, <adrian.hunter@intel.com>,
<yaowenbin1@huawei.com>, <linux-perf-users@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Cc: <hewenliang4@huawei.com>, <wuxu.wu@huawei.com>
Subject: [PATCH] perf record: Add O_EXCL flag for opening perf.data file
Date: Thu, 23 Dec 2021 09:58:15 +0800 [thread overview]
Message-ID: <1f7127f4-6f32-8b4f-5bc2-3f35a337bc1a@huawei.com> (raw)
When the following command is executed, a coredump file is generated.
$ perf record -e cpu-clock sleep 3 & perf record -e cpu-clock sleep 3 &
[1] 213456
[2] 213457
[ perf record: Woken up 1 times to write data ]
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data (4 samples) ]
[1]- Bus error (core dumped) perf record -e cpu-clock sleep 3
[2]+ Done perf record -e cpu-clock sleep 3
It is a problem of concurrent access of perf.data file. When several
processes execute "perf record" command in the same directory
concurrently, they may open the same perf.data file, and write
the same file, causing data confusion. It may cause coredump when
reading the file. Then add O_EXCL flag to ensure that there is only
one process operating the perf.data file in the same directory concurrently.
After adding O_EXCL flag, the open operation will fail if there is
a perf.data file existing in the same directory, so also rename the
perf.data file to perf.data.old even if the size of perf.data is zero.
Signed-off-by: yaowenbin <yaowenbin1@huawei.com>
---
tools/perf/util/data.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index f5d260b1df4d..92cf0aa3ed83 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -201,7 +201,7 @@ static int check_backup(struct perf_data *data)
if (perf_data__is_read(data))
return 0;
- if (!stat(data->path, &st) && st.st_size) {
+ if (!stat(data->path, &st)) {
char oldname[PATH_MAX];
int ret;
@@ -285,7 +285,7 @@ static int open_file_write(struct perf_data *data)
int fd;
char sbuf[STRERR_BUFSIZE];
- fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
+ fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC|O_EXCL,
S_IRUSR|S_IWUSR);
if (fd < 0)
--
2.27.0
reply other threads:[~2021-12-23 1:58 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1f7127f4-6f32-8b4f-5bc2-3f35a337bc1a@huawei.com \
--to=yaowenbin1@huawei.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=hewenliang4@huawei.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=rickyman7@gmail.com \
--cc=thunder.leizhen@huawei.com \
--cc=wuxu.wu@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®