All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf record: Add O_EXCL flag for opening perf.data file
@ 2021-12-23  1:58 yaowenbin
  0 siblings, 0 replies; only message in thread
From: yaowenbin @ 2021-12-23  1:58 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, thunder.leizhen, rickyman7, ak, adrian.hunter,
	yaowenbin1, linux-perf-users, linux-kernel
  Cc: hewenliang4, wuxu.wu

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-23  1:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-23  1:58 [PATCH] perf record: Add O_EXCL flag for opening perf.data file yaowenbin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.