Skip to contentSkip to conversation

9p fid refcount: add a 9p_fid_ref tracepoint

PATCH In review Dominique Martinet 2 participants 2 messages Jul 2, 2022 – Jul 3, 2022

Subsystems (4)
lore
Share & tools
lore

Dominique Martinet

author maintainer
#0 lore raw

PATCH v3 1/1 9p fid refcount: add a 9p_fid_ref tracepoint

This adds a tracepoint event for 9p fid lifecycle tracing: when a fid is created, its reference count increased/decreased, and freed. The new 9p_fid_ref tracepoint should help anyone wishing to debug any fid problem such as missing clunk (destroy) or use-after-free.

---

Just resending this single patch of the series as it's the only one without a review tag.

Steven, is it ok to carry it in my tree as is or do I need blessings from you or Ingo?
(it depends on the previous patch so I'd carry it in my tree anyway, but would be more comfortable with a reviewed-by tag)

v2 -> v3:
- added EXPORT_TRACEPOINT_SYMBOL(9p_fid_ref) to have this work when built as module

v1 -> v2:
- added rationale to commit message
- adjusted to use DECLARE_TRACEPOINT + tracepoint_enable() in header

 include/net/9p/client.h   | 13 +++++++++++
 include/trace/events/9p.h | 48 +++++++++++++++++++++++++++++++++++++++
 net/9p/client.c           | 20 +++++++++++++++-
 3 files changed, 80 insertions(+), 1 deletion(-)
include/net/9p/client.h +13 -0
index eabb53992350..8f629f1df865 100644
@@ -11,6 +11,7 @@
 
 #include <linux/utsname.h>
 #include <linux/idr.h>
+#include <linux/tracepoint-defs.h>
 
 /* Number of requests per row */
 #define P9_ROW_MAXTAG 255
@@ -237,6 +238,12 @@ static inline int p9_req_try_get(struct p9_req_t *r)
 
 int p9_req_put(struct p9_req_t *r);
 
+/* We cannot have the real tracepoints in header files,
+ * use a wrapper function */
+DECLARE_TRACEPOINT(9p_fid_ref);
+void do_trace_9p_fid_get(struct p9_fid *fid);
+void do_trace_9p_fid_put(struct p9_fid *fid);
+
 /* fid reference counting helpers:
  *  - fids used for any length of time should always be referenced through
  *    p9_fid_get(), and released with p9_fid_put()
@@ -249,6 +256,9 @@ int p9_req_put(struct p9_req_t *r);
  */
 static inline struct p9_fid *p9_fid_get(struct p9_fid *fid)
 {
+	if (tracepoint_enabled(9p_fid_ref))
+		do_trace_9p_fid_get(fid);
+
 	refcount_inc(&fid->count);
 
 	return fid;
@@ -259,6 +269,9 @@ static inline int p9_fid_put(struct p9_fid *fid)
 	if (!fid || IS_ERR(fid))
 		return 0;
 
+	if (tracepoint_enabled(9p_fid_ref))
+		do_trace_9p_fid_put(fid);
+
 	if (!refcount_dec_and_test(&fid->count))
 		return 0;
 
include/trace/events/9p.h +48 -0

72 lines not on this page · raw message

net/9p/client.c +19 -1
index f3eb280c7d9d..dfe8beb864fc 100644
@@ -907,8 +907,10 @@ static struct p9_fid *p9_fid_create(struct p9_client *clnt)
 			    GFP_NOWAIT);
 	spin_unlock_irq(&clnt->lock);
 	idr_preload_end();
-	if (!ret)
+	if (!ret) {
+		trace_9p_fid_ref(fid, P9_FID_REF_CREATE);
 		return fid;
+	}
 
 	kfree(fid);
 	return NULL;
@@ -920,6 +922,7 @@ static void p9_fid_destroy(struct p9_fid *fid)
 	unsigned long flags;
 
 	p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
+	trace_9p_fid_ref(fid, P9_FID_REF_DESTROY);
 	clnt = fid->clnt;
 	spin_lock_irqsave(&clnt->lock, flags);
 	idr_remove(&clnt->fids, fid->fid);
@@ -928,6 +931,21 @@ static void p9_fid_destroy(struct p9_fid *fid)
 	kfree(fid);
 }
 
+/* We also need to export tracepoint symbols for tracepoint_enabled() */
+EXPORT_TRACEPOINT_SYMBOL(9p_fid_ref);
+
+void do_trace_9p_fid_get(struct p9_fid *fid)
+{
+	trace_9p_fid_ref(fid, P9_FID_REF_GET);
+}
+EXPORT_SYMBOL(do_trace_9p_fid_get);
+
+void do_trace_9p_fid_put(struct p9_fid *fid)
+{
+	trace_9p_fid_ref(fid, P9_FID_REF_PUT);
+}
+EXPORT_SYMBOL(do_trace_9p_fid_put);
+
 static int p9_client_version(struct p9_client *c)
 {
 	int err = 0;
Signature · 1 line
2.35.1
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 7 lines · On Sat, 2 Jul 2022 19:29:14 +0900 Dominique Martinet <[email protected]>…

This adds a tracepoint event for 9p fid lifecycle tracing: when a fid is created, its reference count increased/decreased, and freed. The new 9p_fid_ref tracepoint should help anyone wishing to debug any fid problem such as missing clunk (destroy) or use-after-free.

Quoted 7 lines

---

Just resending this single patch of the series as it's the only one without a review tag.

Steven, is it ok to carry it in my tree as is or do I need blessings from you or Ingo?

The addition of trace events do belong to the maintainers of where the trace events go.

Quoted 2 lines

(it depends on the previous patch so I'd carry it in my tree anyway, but would be more comfortable with a reviewed-by tag)

Yes, I prefer people Cc me on trace events just so that I can catch mistakes or find better ways to accomplish what is trying to be done.

Especially for something that does changes like this patch, which are not just the trivial TRACE_EVENT() trace_*() procedure. Thanks for Cc'ing me.

Quoted 6 lines

v2 -> v3:
- added EXPORT_TRACEPOINT_SYMBOL(9p_fid_ref) to have this work when built as module

Quoted 9 lines

v1 -> v2:
- added rationale to commit message
- adjusted to use DECLARE_TRACEPOINT + tracepoint_enable() in header

 include/net/9p/client.h   | 13 +++++++++++
 include/trace/events/9p.h | 48 +++++++++++++++++++++++++++++++++++++++
 net/9p/client.c           | 20 +++++++++++++++-
 3 files changed, 80 insertions(+), 1 deletion(-)

The rest looks fine.

For the tracing point of view:

-- Steve