[PATCH v15 5/9] ref_tracker: allow pr_ostream() to print directly to a seq_file

Jeff Layton posted 9 patches 3 months, 3 weeks ago
[PATCH v15 5/9] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jeff Layton 3 months, 3 weeks ago
Allow pr_ostream to also output directly to a seq_file without an
intermediate buffer. The first caller of +ref_tracker_dir_seq_print()
will come in a later patch, so mark that __maybe_unused for now. That
designation will be removed once it is used.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 lib/ref_tracker.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c
index 42872f406b2a91b5bc611405cae7ce883fd8ed22..73b606570cce9e551d13e65365a87dc4ce748b13 100644
--- a/lib/ref_tracker.c
+++ b/lib/ref_tracker.c
@@ -8,6 +8,7 @@
 #include <linux/slab.h>
 #include <linux/stacktrace.h>
 #include <linux/stackdepot.h>
+#include <linux/seq_file.h>
 
 #define REF_TRACKER_STACK_ENTRIES 16
 #define STACK_BUF_SIZE 1024
@@ -66,6 +67,7 @@ struct ostream {
 	void __ostream_printf (*func)(struct ostream *stream, char *fmt, ...);
 	char *prefix;
 	char *buf;
+	struct seq_file *seq;
 	int size, used;
 };
 
@@ -301,6 +303,30 @@ EXPORT_SYMBOL_GPL(ref_tracker_free);
 
 static struct dentry *ref_tracker_debug_dir = (struct dentry *)-ENOENT;
 
+static void __ostream_printf pr_ostream_seq(struct ostream *stream, char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	seq_vprintf(stream->seq, fmt, args);
+	va_end(args);
+}
+
+static __maybe_unused int
+ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
+{
+	struct ostream os = { .func = pr_ostream_seq,
+			      .prefix = "",
+			      .seq = seq };
+	unsigned long flags;
+
+	spin_lock_irqsave(&dir->lock, flags);
+	__ref_tracker_dir_pr_ostream(dir, 16, &os);
+	spin_unlock_irqrestore(&dir->lock, flags);
+
+	return os.used;
+}
+
 static int __init ref_tracker_debugfs_init(void)
 {
 	ref_tracker_debug_dir = debugfs_create_dir("ref_tracker", NULL);

-- 
2.49.0
Re: [PATCH v15 5/9] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Krzysztof Karas 3 months, 2 weeks ago
Hi Jeff,

[...]
> +static __maybe_unused int
> +ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
> +{
> +	struct ostream os = { .func = pr_ostream_seq,
> +			      .prefix = "",
This is also a "ref_tracker_*" function, so maybe use the same
prefix as in other functions? I mean .prefix = "ref_tracker:".
Unless, you have a reason for leaving it empty ;)

---
Best Regards,
Krzysztof
Re: [PATCH v15 5/9] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jeff Layton 3 months, 2 weeks ago
On Mon, 2025-06-23 at 14:01 +0000, Krzysztof Karas wrote:
> Hi Jeff,
> 
> [...]
> > +static __maybe_unused int
> > +ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
> > +{
> > +	struct ostream os = { .func = pr_ostream_seq,
> > +			      .prefix = "",
> This is also a "ref_tracker_*" function, so maybe use the same
> prefix as in other functions? I mean .prefix = "ref_tracker:".
> Unless, you have a reason for leaving it empty ;)
> 

I have a reason to leave it empty.

That is the prefix for each line that gets printed by the pr_ostream()
call. This one is for the lines that go to the debugfs files. I think
it will be redundant to prepend every line in these files with
"ref_tracker: ".
-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH v15 5/9] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Krzysztof Karas 3 months, 1 week ago
Hi Jeff,

> On Mon, 2025-06-23 at 14:01 +0000, Krzysztof Karas wrote:
> > Hi Jeff,
> > 
> > [...]
> > > +static __maybe_unused int
> > > +ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
> > > +{
> > > +	struct ostream os = { .func = pr_ostream_seq,
> > > +			      .prefix = "",
> > This is also a "ref_tracker_*" function, so maybe use the same
> > prefix as in other functions? I mean .prefix = "ref_tracker:".
> > Unless, you have a reason for leaving it empty ;)
> > 
> 
> I have a reason to leave it empty.
> 
> That is the prefix for each line that gets printed by the pr_ostream()
> call. This one is for the lines that go to the debugfs files. I think
> it will be redundant to prepend every line in these files with
> "ref_tracker: ".

OK, this makes sense.
Reviewed-by: Krzysztof Karas <krzysztof.karas@intel.com>

---
Best Regards,
Krzysztof