[PATCH v5 04/10] ref_tracker: allow pr_ostream() to print directly to a seq_file

Jeff Layton posted 10 patches 9 months, 2 weeks ago
There is a newer version of this series
[PATCH v5 04/10] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jeff Layton 9 months, 2 weeks ago
Allow pr_ostream to also output directly to a seq_file without an
intermediate buffer.

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

diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c
index b6e0a87dd75eddef4d504419c0cf398ea65c19d8..4857bcb6d4bf557a0089f51328e75e8209e959e6 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
@@ -70,6 +71,7 @@ struct ostream {
 	void __ostream_printf (*func)(struct ostream *stream, char *fmt, ...);
 	char *prefix;
 	char *buf;
+	struct seq_file *seq;
 	int size, used;
 };
 
@@ -93,6 +95,15 @@ static void __ostream_printf pr_ostream_buf(struct ostream *stream, char *fmt, .
 	stream->used += min(ret, len);
 }
 
+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);
+}
+
 #define pr_ostream(stream, fmt, args...) \
 ({ \
 	struct ostream *_s = (stream); \
@@ -302,6 +313,20 @@ EXPORT_SYMBOL_GPL(ref_tracker_free);
 #ifdef CONFIG_DEBUG_FS
 #include <linux/debugfs.h>
 
+static 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 v5 04/10] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jakub Kicinski 9 months, 2 weeks ago
On Mon, 28 Apr 2025 11:26:27 -0700 Jeff Layton wrote:
> Allow pr_ostream to also output directly to a seq_file without an
> intermediate buffer.
> 
> Reviewed-by: Andrew Lunn <andrew@lunn.ch>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>

lib/ref_tracker.c:316:12: warning: unused function 'ref_tracker_dir_seq_print' [-Wunused-function]
  316 | static int ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~
Re: [PATCH v5 04/10] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jeff Layton 9 months, 2 weeks ago
On Tue, 2025-04-29 at 16:27 -0700, Jakub Kicinski wrote:
> On Mon, 28 Apr 2025 11:26:27 -0700 Jeff Layton wrote:
> > Allow pr_ostream to also output directly to a seq_file without an
> > intermediate buffer.
> > 
> > Reviewed-by: Andrew Lunn <andrew@lunn.ch>
> > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> 
> lib/ref_tracker.c:316:12: warning: unused function 'ref_tracker_dir_seq_print' [-Wunused-function]
>   316 | static int ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
>       |            ^~~~~~~~~~~~~~~~~~~~~~~~~

The caller ends up being added in patch #6. I think the only thing I
can do here to silence this is to squash this patch into that one.

I kind of don't like doing that here because I think the patches are
conceptually separate, and it'll make for a rather large patch.

Let me know what you prefer.

Thanks,
-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH v5 04/10] ref_tracker: allow pr_ostream() to print directly to a seq_file
Posted by Jakub Kicinski 9 months, 2 weeks ago
On Tue, 29 Apr 2025 19:18:01 -0700 Jeff Layton wrote:
> On Tue, 2025-04-29 at 16:27 -0700, Jakub Kicinski wrote:
> > On Mon, 28 Apr 2025 11:26:27 -0700 Jeff Layton wrote:  
> > > Allow pr_ostream to also output directly to a seq_file without an
> > > intermediate buffer.
> > > 
> > > Reviewed-by: Andrew Lunn <andrew@lunn.ch>
> > > Signed-off-by: Jeff Layton <jlayton@kernel.org>  
> > 
> > lib/ref_tracker.c:316:12: warning: unused function 'ref_tracker_dir_seq_print' [-Wunused-function]
> >   316 | static int ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *seq)
> >       |            ^~~~~~~~~~~~~~~~~~~~~~~~~  
> 
> The caller ends up being added in patch #6. I think the only thing I
> can do here to silence this is to squash this patch into that one.
> 
> I kind of don't like doing that here because I think the patches are
> conceptually separate, and it'll make for a rather large patch.
> 
> Let me know what you prefer.

Would it work to make the fops very dumbed down - return an error 
from open. And then implement the seqfile output as the next patch?