From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org, Namhyung Kim <namhyung@kernel.org>,
David Ahern <dsahern@gmail.com>, Jiri Olsa <jolsa@redhat.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 12/37] perf tools: Add dso__data_get/put_fd()
Date: Tue, 26 May 2015 13:47:43 -0300 [thread overview]
Message-ID: <1432658888-7993-13-git-send-email-acme@kernel.org> (raw)
In-Reply-To: <1432658888-7993-1-git-send-email-acme@kernel.org>
From: Namhyung Kim <namhyung@kernel.org>
Using dso__data_fd() in multi-thread environment is not safe since
returned fd can be closed and/or reused anytime.
So convert it to the dso__data_get/put_fd() pair to protect the access
with lock.
The original dso__data_fd() is deprecated and kept only for testing.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1432137821-10853-3-git-send-email-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/tests/dso-data.c | 11 +++++++++++
tools/perf/util/dso.c | 31 ++++++++++++++++++++++---------
tools/perf/util/dso.h | 13 +++++++++----
tools/perf/util/unwind-libunwind.c | 11 ++++++++---
4 files changed, 50 insertions(+), 16 deletions(-)
diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c
index 513e5febbe5a..3e41c61bd861 100644
--- a/tools/perf/tests/dso-data.c
+++ b/tools/perf/tests/dso-data.c
@@ -99,6 +99,17 @@ struct test_data_offset offsets[] = {
},
};
+/* move it from util/dso.c for compatibility */
+static int dso__data_fd(struct dso *dso, struct machine *machine)
+{
+ int fd = dso__data_get_fd(dso, machine);
+
+ if (fd >= 0)
+ dso__data_put_fd(dso);
+
+ return fd;
+}
+
int test__dso_data(void)
{
struct machine machine;
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index e95e850dd832..7e11a700303f 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -473,25 +473,35 @@ out:
}
/**
- * dso__data_fd - Get dso's data file descriptor
+ * dso__data_get_fd - Get dso's data file descriptor
* @dso: dso object
* @machine: machine object
*
* External interface to find dso's file, open it and
- * returns file descriptor.
+ * returns file descriptor. It should be paired with
+ * dso__data_put_fd() if it returns non-negative value.
*/
-int dso__data_fd(struct dso *dso, struct machine *machine)
+int dso__data_get_fd(struct dso *dso, struct machine *machine)
{
if (dso->data.status == DSO_DATA_STATUS_ERROR)
return -1;
- pthread_mutex_lock(&dso__data_open_lock);
+ if (pthread_mutex_lock(&dso__data_open_lock) < 0)
+ return -1;
+
try_to_open_dso(dso, machine);
- pthread_mutex_unlock(&dso__data_open_lock);
+
+ if (dso->data.fd < 0)
+ pthread_mutex_unlock(&dso__data_open_lock);
return dso->data.fd;
}
+void dso__data_put_fd(struct dso *dso __maybe_unused)
+{
+ pthread_mutex_unlock(&dso__data_open_lock);
+}
+
bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
{
u32 flag = 1 << by;
@@ -1199,12 +1209,15 @@ size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
enum dso_type dso__type(struct dso *dso, struct machine *machine)
{
int fd;
+ enum dso_type type = DSO__TYPE_UNKNOWN;
- fd = dso__data_fd(dso, machine);
- if (fd < 0)
- return DSO__TYPE_UNKNOWN;
+ fd = dso__data_get_fd(dso, machine);
+ if (fd >= 0) {
+ type = dso__type_fd(fd);
+ dso__data_put_fd(dso);
+ }
- return dso__type_fd(fd);
+ return type;
}
int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index b26ec3ab1336..bcec06ad73a2 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -240,7 +240,8 @@ int __kmod_path__parse(struct kmod_path *m, const char *path,
/*
* The dso__data_* external interface provides following functions:
- * dso__data_fd
+ * dso__data_get_fd
+ * dso__data_put_fd
* dso__data_close
* dso__data_size
* dso__data_read_offset
@@ -257,8 +258,11 @@ int __kmod_path__parse(struct kmod_path *m, const char *path,
* The current usage of the dso__data_* interface is as follows:
*
* Get DSO's fd:
- * int fd = dso__data_fd(dso, machine);
- * USE 'fd' SOMEHOW
+ * int fd = dso__data_get_fd(dso, machine);
+ * if (fd >= 0) {
+ * USE 'fd' SOMEHOW
+ * dso__data_put_fd(dso);
+ * }
*
* Read DSO's data:
* n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
@@ -277,7 +281,8 @@ int __kmod_path__parse(struct kmod_path *m, const char *path,
*
* TODO
*/
-int dso__data_fd(struct dso *dso, struct machine *machine);
+int dso__data_get_fd(struct dso *dso, struct machine *machine);
+void dso__data_put_fd(struct dso *dso __maybe_unused);
void dso__data_close(struct dso *dso);
off_t dso__data_size(struct dso *dso, struct machine *machine);
diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c
index 7b09a443a280..f079b63f0b7f 100644
--- a/tools/perf/util/unwind-libunwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -269,13 +269,14 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
u64 offset = dso->data.eh_frame_hdr_offset;
if (offset == 0) {
- fd = dso__data_fd(dso, machine);
+ fd = dso__data_get_fd(dso, machine);
if (fd < 0)
return -EINVAL;
/* Check the .eh_frame section for unwinding info */
offset = elf_section_offset(fd, ".eh_frame_hdr");
dso->data.eh_frame_hdr_offset = offset;
+ dso__data_put_fd(dso);
}
if (offset)
@@ -294,13 +295,14 @@ static int read_unwind_spec_debug_frame(struct dso *dso,
u64 ofs = dso->data.debug_frame_offset;
if (ofs == 0) {
- fd = dso__data_fd(dso, machine);
+ fd = dso__data_get_fd(dso, machine);
if (fd < 0)
return -EINVAL;
/* Check the .debug_frame section for unwinding info */
ofs = elf_section_offset(fd, ".debug_frame");
dso->data.debug_frame_offset = ofs;
+ dso__data_put_fd(dso);
}
*offset = ofs;
@@ -353,10 +355,13 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
#ifndef NO_LIBUNWIND_DEBUG_FRAME
/* Check the .debug_frame section for unwinding info */
if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
- int fd = dso__data_fd(map->dso, ui->machine);
+ int fd = dso__data_get_fd(map->dso, ui->machine);
int is_exec = elf_is_exec(fd, map->dso->name);
unw_word_t base = is_exec ? 0 : map->start;
+ if (fd >= 0)
+ dso__data_put_fd(dso);
+
memset(&di, 0, sizeof(di));
if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name,
map->start, map->end))
--
2.1.0
next prev parent reply other threads:[~2015-05-26 16:58 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-26 16:47 [GIT PULL 00/37] perf/core improvements and fixes Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 01/37] perf tools: Separate the tests and tools in installation Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 02/37] perf tools: Fix function declarations needed by parse-events.y Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 03/37] perf tools: Fix parse_events_error dereferences Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 04/37] perf build: Fix libunwind feature detection on 32-bit x86 Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 05/37] perf session: Fix perf_session__peek_event() Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 06/37] perf hists: Reducing arguments of hist_entry_iter__add() Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 07/37] perf hists: Rename add_hist_entry to hists__findnew_entry Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 08/37] perf comm: Use atomic.h for refcounting Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 09/37] perf machine: Do not call map_groups__delete(), drop refcnt instead Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 10/37] perf tools: Fix dso__data_read_offset() file opening Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 11/37] perf tools: Get rid of dso__data_fd() from dso__data_size() Arnaldo Carvalho de Melo
2015-05-26 16:47 ` Arnaldo Carvalho de Melo [this message]
2015-05-26 16:47 ` [PATCH 13/37] perf tools: Rename maps__next Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 14/37] perf tools: Remove redundant initialization of thread linkage members Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 15/37] perf tools: Nuke unused map_groups__flush() Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 16/37] perf tools: Import rb_erase_init from block/ in the kernel sources Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 17/37] perf machine: Mark removed threads as such Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 18/37] perf tools: Leave DSO destruction to the map destruction Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 19/37] perf tools: Use maps__first()/map__next() Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 20/37] perf tools: Assign default value for some pointers Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 21/37] perf tools: Improve setting of gcc debug option Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 22/37] perf sched: Add option to merge like comms to lat output Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 23/37] perf tools: Disallow PMU events intel_pt and intel_bts until there is support Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 24/37] perf auxtrace: Add Intel PT as an AUX area tracing type Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 25/37] perf tools: Add Intel PT packet decoder Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 26/37] perf tools: Add Intel PT instruction decoder Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 27/37] perf tools: Add Intel PT log Arnaldo Carvalho de Melo
2015-05-26 16:47 ` [PATCH 28/37] perf tools: Add Intel PT decoder Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 29/37] perf tools: Add Intel PT support Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 30/37] perf tools: Take Intel PT into use Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 31/37] perf tools: Allow auxtrace data alignment Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 32/37] perf tools: Add Intel BTS support Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 33/37] perf tools: Output sample flags and insn_len from intel_pt Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 34/37] perf tools: Output sample flags and insn_len from intel_bts Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 35/37] perf tools: Intel PT to always update thread stack trace number Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 36/37] perf tools: Intel BTS " Arnaldo Carvalho de Melo
2015-05-26 16:48 ` [PATCH 37/37] perf tools: Put itrace options into an asciidoc include Arnaldo Carvalho de Melo
2015-05-27 7:38 ` [GIT PULL 00/37] perf/core improvements and fixes Ingo Molnar
2015-05-27 12:35 ` Arnaldo Carvalho de Melo
2015-05-27 12:40 ` Adrian Hunter
2015-05-27 12:45 ` Ingo Molnar
2015-06-05 13:21 ` Adrian Hunter
2015-06-05 14:08 ` Arnaldo Carvalho de Melo
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=1432658888-7993-13-git-send-email-acme@kernel.org \
--to=acme@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@redhat.com \
--cc=dsahern@gmail.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
/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 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.