Skip to contentSkip to conversation

relayfs redux, part 3

PATCH In review Tom Zanussi 3 participants 9 messages Feb 4, 2005 – Feb 5, 2005 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 relayfs redux, part 3

Hi,

Here's the latest version of relayfs, against 2.6.10. It includes a bunch of cleanup and restructuring prompted by the previous round of comments, but the major change that people would care about would probably be the changes to the logging functions relay_write(), __relay_write(), and relay_reserve(). They've been rewritten to be more efficient, or so I hope - I'm sure I'll hear about how they should be improved for the next version in any case. ;-) Thanks to everyone who commented on the previous version.

This is what the API currently looks like:

    rchan *relay_open(chanpath, subbuf_size, n_subbufs, flags, callbacks);
    void relay_close(chan);
    unsigned relay_write(chan, data, length);
    unsigned __relay_write(chan, data, length);
    void *relay_reserve(chan, length);
    void relay_subbufs_consumed(chan, subbufs_consumed, cpu);
    extern void relay_reset(chan);
    void relay_commit(buf, subbuf_idx, count);

helper macros:

relay_get_buffer(chan, cpu)
relay_get_padding(buf, subbuf_idx)
relay_get_commit(buf, subbuf_idx)

callbacks:

    int subbuf_start(buf, subbuf, prev_subbuf_idx);
    int deliver(buffer, subbuf, subbuf_idx);
    int fileop_notify(buf, filp, fileop);

As before, I've tested this code on a single proc machine using a hacked version of the kprobes network packet tracing module, which can be found here:

http://prdownloads.sourceforge.net/dprobes/plog.tar.gz?download

Once everyone's more or less happy with the API and implementation, I'll do some SMP testing and write some Documentation.

Thanks,

Tom

 fs/Kconfig                 |   12 
 fs/Makefile                |    1 
 fs/relayfs/Makefile        |    8 
 fs/relayfs/buffers.c       |  124 ++++++++
 fs/relayfs/buffers.h       |   14 
 fs/relayfs/inode.c         |  481 +++++++++++++++++++++++++++++++++
 fs/relayfs/relay.c         |  653 +++++++++++++++++++++++++++++++++++++++++++++
 fs/relayfs/relay.h         |   34 ++
 include/linux/relayfs_fs.h |  239 ++++++++++++++++
 9 files changed, 1566 insertions(+)
fs/Kconfig +12 -0
diff -urpN -X dontdiff linux-2.6.10/fs/Kconfig linux-2.6.10-cur/fs/Kconfig
@@ -972,6 +972,18 @@ config RAMFS
 	  To compile this as a module, choose M here: the module will be called
 	  ramfs.
 
+config RELAYFS_FS
+	tristate "Relayfs file system support"
+	---help---
+	  Relayfs is a high-speed data relay filesystem designed to provide
+	  an efficient mechanism for tools and facilities to relay large
+	  amounts of data from kernel space to user space.
+
+	  To compile this code as a module, choose M here: the module will be
+	  called relayfs.
+
+	  If unsure, say N.
+
 endmenu
 
 menu "Miscellaneous filesystems"
fs/Makefile +1 -0
diff -urpN -X dontdiff linux-2.6.10/fs/Makefile linux-2.6.10-cur/fs/Makefile
@@ -94,3 +94,4 @@ obj-$(CONFIG_AFS_FS)		+= afs/
 obj-$(CONFIG_BEFS_FS)		+= befs/
 obj-$(CONFIG_HOSTFS)		+= hostfs/
 obj-$(CONFIG_HPPFS)		+= hppfs/
+obj-$(CONFIG_RELAYFS_FS)	+= relayfs/
fs/relayfs/Makefile +8 -0
diff -urpN -X dontdiff linux-2.6.10/fs/relayfs/Makefile linux-2.6.10-cur/fs/relayfs/Makefile
@@ -0,0 +1,8 @@
+#
+# relayfs Makefile
+#
+
+obj-$(CONFIG_RELAYFS_FS) += relayfs.o
+
+relayfs-y := relay.o buffers.o inode.o
+
fs/relayfs/buffers.c +124 -0

124 lines not on this page · raw message

fs/relayfs/buffers.h +14 -0
diff -urpN -X dontdiff linux-2.6.10/fs/relayfs/buffers.h linux-2.6.10-cur/fs/relayfs/buffers.h
@@ -0,0 +1,14 @@
+#ifndef _BUFFERS_H
+#define _BUFFERS_H
+
+/* This inspired by rtai/shmem */
+#define FIX_SIZE(x) (((x) - 1) & PAGE_MASK) + PAGE_SIZE
+
+extern void *relay_alloc_rchan_buf(unsigned long size,
+				   struct page ***page_array,
+				   int *page_count);
+extern void relay_free_rchan_buf(void *buf,
+				 struct page **page_array,
+				 int page_count);
+
+#endif/* _BUFFERS_H */
fs/relayfs/inode.c +481 -0

481 lines not on this page · raw message

fs/relayfs/relay.c +653 -0

653 lines not on this page · raw message

fs/relayfs/relay.h +34 -0
diff -urpN -X dontdiff linux-2.6.10/fs/relayfs/relay.h linux-2.6.10-cur/fs/relayfs/relay.h
@@ -0,0 +1,34 @@
+/*
+ * fs/relayfs/relay.h
+ *
+ * Copyright (C) 1999-2005 - Karim Yaghmour ([email protected]), Opersys inc.
+ * Copyright (C) 2002, 2003 - Tom Zanussi ([email protected]), IBM Corp
+ *
+ * RelayFS internal definitions and declarations
+ */
+
+#ifndef _RELAY_H
+#define _RELAY_H
+
+/*
+ * Internal relayfs kernel API, fs/relayfs/relay.c
+ */
+
+#define relay_mode_continuous(chan) (((chan)->flags & RELAY_MODE_CONTINUOUS) ? 1 : 0)
+
+extern int relayfs_create_file(const char * name,
+			       struct dentry *parent,
+			       struct dentry **dentry,
+			       void * data,
+			       int mode);
+extern int relayfs_remove_file(struct dentry *dentry);
+extern int relayfs_create_dirs(const char *chanpath,
+			       const char **residual,
+			       struct dentry **topdir);
+extern void relayfs_remove_dirs(struct dentry *dir);
+extern int relay_mmap_buffer(struct rchan_buf *buf,
+			     struct vm_area_struct *vma);
+extern void relay_destroy_buf(struct kref *kref);
+extern int relay_buf_empty(struct rchan_buf *buf);
+
+#endif /* _RELAY_H */
include/linux/relayfs_fs.h +239 -0

239 lines not on this page · raw message

Signature · 4 lines
Regards,

Tom Zanussi <[email protected]>
IBM Linux Technology Center/RAS
#1 lore raw

First set of comments. Mostly ignores the actual filesystem sematics bits, that'll come next.

fs/relayfs/Makefile line 3

Quoted 3 lines
fs/relayfs/Makefile +3 -0
+#
+# relayfs Makefile
+#

probably superflous comment ;-)

fs/relayfs/buffers.c line 98

Quoted 1 line
fs/relayfs/buffers.c +1 -0
+	mem = vmap(*page_array, *page_count, GFP_KERNEL, PAGE_KERNEL);

Do you really need to vmap it, and eat up vmallocspace and TLB entries? Maybe some simple arithmetics on a page array could replace it?

Quoted 1 line

+#include <linux/smp_lock.h>

don't think you need this one.

Quoted 4 lines
fs/relayfs/inode.c +4 -0
+/**
+ *	relayfs_open - open file op for relayfs files
+ *	@inode: the inode
+ *	@filp: the file

fs/relayfs/inode.c line 334

Quoted 10 lines
fs/relayfs/inode.c +10 -0
+ *
+ *	Associates the channel buffer with the file, and increments the
+ *	channel buffer refcount.
+ */
+int relayfs_open(struct inode *inode, struct file *filp)
+{
+	struct rchan_buf *buf;
+	int retval = 0;
+
+	if (inode->u.generic_ip) {

Can inode->u.generic_ip ever be non-NULL? What about using ->alloc_inode and ->destroy_inode to have the rchan_buf allocated as part of the inode?

fs/relayfs/inode.c line 339

Quoted 1 line
fs/relayfs/inode.c +1 -0
+		retval = buf->chan->cb->fileop_notify(buf, filp, RELAY_FILE_OPEN);

I think you should split ->fileop_notify into individual operations for each of the different events.

fs/relayfs/inode.c

Quoted 8 lines
fs/relayfs/inode.c +7 -0
+int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct rchan_buf *buf = filp->private_data;
+  
+	return relay_mmap_buffer(buf, vma);
+}
 
+#include <asm/io.h>

What do you need this one for?

Quoted 1 line

+#include <asm/current.h>

should already be there through <linux/sched.h>

Quoted 1 line

+#include <asm/bitops.h>

please use <linux/bitops.h>

Quoted 1 line

+#include <asm/pgtable.h>

what do you need this one for?

Quoted 1 line

+#include <asm/hardirq.h>

always use <linux/hardirq.h> instead

Quoted 1 line

+/* \begin{Code inspired from BTTV driver} */

...

fs/relayfs/relay.c line 116

Quoted 11 lines
fs/relayfs/relay.c +11 -0
+	while (size > 0) {
+		page = kvirt_to_pa(pos);
+		if (remap_pfn_range(vma, start, page >> PAGE_SHIFT, PAGE_SIZE, PAGE_SHARED))
+			return -EAGAIN;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
+		size -= PAGE_SIZE;
+	}
+
+	return 0;
+}

Using remap_pfn_range on normal kernel memory and the PageReserved flags, etc.. is not nice. You'd better implement a ->nopage handler that just does a simple array lookup and install the page. And if you really care about the little time this spends in the pagefault path you can implement ->populate and support MAP_POPULATE mmaps :)

fs/relayfs/relay.c line 353

Quoted 8 lines
fs/relayfs/relay.c +8 -0
+static struct rchan_buf *rchan_create_buf(struct rchan *chan)
+{
+	struct page **page_array;
+	int page_count;
+	struct rchan_buf *buf;
+
+	if ((buf = kcalloc(1, sizeof(struct rchan_buf), GFP_KERNEL)) == NULL)
+		return NULL;

this doesn't fir the normal linux style. Whic you use two lines below:

fs/relayfs/relay.c line 363

Quoted 6 lines
fs/relayfs/relay.c +5 -0
+
+	buf->padding = kmalloc(chan->n_subbufs * sizeof(unsigned *), GFP_KERNEL);
+	if (buf->padding == NULL)
+		goto free_buf;
 
+	if ((buf->start = relay_alloc_rchan_buf(chan->alloc_size, &page_array, &page_count)) == NULL) {

but here again not.

fs/relayfs/relay.c line 366

Quoted 3 lines
fs/relayfs/relay.c +3 -0
+		buf->page_array = NULL;
+		buf->page_count = 0;
+		goto free_commit;

also this would be cleaner if placed at another goto label at the end

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((buf = rchan_create_buf(chan)) == NULL)
+		return NULL;

so this style is used in various places. Please try to make it fit normal style everywhere.

Also you're using foo == NULL a lot while we mostly use !foo. This isn't important in anyway, but would make reading a little easier.

include/linux/relayfs_fs.h line 147

Quoted 3 lines
include/linux/relayfs_fs.h +3 -0
+#define relay_get_buf(chan, cpu)		(chan->buf[cpu])
+#define relay_get_padding(buf, subbuf_idx)	(buf->padding[subbuf_idx])
+#define relay_get_commit(buf, subbuf_idx)	(buf->commit[subbuf_idx])

I don't think these macros help following the code.

include/linux/relayfs_fs.h line 166

Quoted 4 lines
include/linux/relayfs_fs.h +4 -0
+	unsigned long flags;
+	struct rchan_buf *buf = relay_get_buf(chan, smp_processor_id());
+
+	local_irq_save(flags);

you need to place the relay_get_buf inside the locked region, so that the return value from smp_processor_id() is stable.

#2 lore raw
Quoted 10 lines · Christoph Hellwig

First set of comments. Mostly ignores the actual filesystem sematics bits, that'll come next.

fs/relayfs/Makefile line 3

Quoted 3 lines
fs/relayfs/Makefile +3 -0
+#
+# relayfs Makefile
+#

probably superflous comment ;-)

Quoted 5 lines

fs/relayfs/buffers.c line 98

Quoted 1 line
fs/relayfs/buffers.c +1 -0
+	mem = vmap(*page_array, *page_count, GFP_KERNEL, PAGE_KERNEL);

Do you really need to vmap it, and eat up vmallocspace and TLB entries? Maybe some simple arithmetics on a page array could replace it?

I would really like to not vmap it, but I don't know how else to create a large kernel virtual address space for the large buffers that might be needed for some applications like ltt. Maybe managing a bunch of large kmalloc'ed buffers instead would work, but there's no guarantee how many of those you'd be able to get at one time. Doing arithmetic on pages seems like it would be too much overhead for the fast logging path, but please let me know if I'm missing something.

Thanks for all the other comments - I'll be sure to make those fixes.

Tom

#3 lore raw
In reply to Tom Zanussi

in the filesystem path especially relayfs_create_entry and the functions called by it seem overly complex, probably because copying from ramfs which allows namespace operations from userland. See the totally untested code below for how it could be done more cleanly.

What I really dislike is the code for automatically creating complex hiearchies. What kinds of hierachies does LTT use? It shouldn't be more than subsystem/{stream1, stream2, ..., streamN}, right? In that case I think we could leave it to the user to take of that himself.

static struct inode *relayfs_get_inode(struct super_block *sb, int mode)

{
	struct inode * inode = new_inode(sb);

	if (!inode)
		return NULL;

	inode->i_mode = mode;
	inode->i_uid = 0;
	inode->i_gid = 0;
	inode->i_blksize = PAGE_CACHE_SIZE;
	inode->i_blocks = 0;
	inode->i_mapping->backing_dev_info = &relayfs_backing_dev_info;
	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;

	switch (mode & S_IFMT) {
	case S_IFREG:
		inode->i_fop = &relayfs_file_operations;
		break;
	case S_IFDIR:
		inode->i_op = &simple_dir_inode_operations;
		inode->i_fop = &simple_dir_operations;
		/*
		 * directory inodes start off with i_nlink == 2 (for "." entry)
		 */
		inode->i_nlink++;
		break;
	default:
		break;
	}

	return inode;
}

/**
* relayfs_create_entry - create a relayfs directory or file
* @name: the name of the file to create
* @parent: parent directory
* @dentry: result dentry
* @mode: mode
* @data: data to associate with the file
*
* Creates a file or directory with the specifed permissions.
*/
static int relayfs_create_entry(const char *name, struct dentry *parent,
struct dentry **dentry, int mode, void *data)

{
	struct qstr qname;
	struct dentry *d;
	struct inode *inode;
	int error = 0;

	BUG_ON(!S_ISREG(mode) || !S_ISDIR(mode));

	error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
	if (error) {
		printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
		return error;
	}

	qname.name = name;
	qname.len = strlen(name);
	qname.hash = full_name_hash(name, qname.len);

	if (parent == NULL)
		if (relayfs_mount && relayfs_mount->mnt_sb)
			parent = relayfs_mount->mnt_sb->s_root;

	if (parent == NULL) {
		simple_release_fs(&relayfs_mount, &relayfs_mount_count);
 		return -EINVAL;
	}

	parent = dget(parent);
	down(&parent->d_inode->i_sem);
	d = lookup_hash(&qname, parent);
	if (IS_ERR(d)) {
		error = PTR_ERR(d);
		goto release_mount;
	}

	if (d->d_inode) {
		error = -EEXIST;
		goto release_mount;
	}

	inode = relayfs_get_inode(parent->d_inode->i_sb, mode);
	if (!inode) {
		error = -ENOSPC;
		goto release_mount;
	}

	d_instantiate(d, inode);
	dget(d);	/* Extra count - pin the dentry in core */

	if (mode & S_IFREG)
		d->d_inode->u.generic_ip = data;
	else
		parent->d_inode->i_nlink++;

	error = 0;
	goto exit;

release_mount:

	simple_release_fs(&relayfs_mount, &relayfs_mount_count);

exit:

	*dentry = d;
	up(&parent->d_inode->i_sem);
	dput(parent);

	return error;
}
#4 lore raw
Quoted 4 lines · Christoph Hellwig

in the filesystem path especially relayfs_create_entry and the functions called by it seem overly complex, probably because copying from ramfs which allows namespace operations from userland. See the totally untested code below for how it could be done more cleanly.

Thanks, I'll do some simplification as you suggest.

Quoted 6 lines

What I really dislike is the code for automatically creating complex hiearchies. What kinds of hierachies does LTT use? It shouldn't be more than subsystem/{stream1, stream2, ..., streamN}, right? In that case I think we could leave it to the user to take of that himself.

Yeah, I was debating whether to keep that code or just export a creat_subdir() function instead, which I think I'll do after all. ltt doesn't really doesn't need much of a hierarchy beyond ltt/ or maybe one more deep e.g. ltt/trace ltt/flight, and I doubt many other applications would either.

Tom

#5 lore raw
In reply to Tom Zanussi

Just a minor comment, not reading it all.

On Fri, Feb 04, 2005 at 02:17:37PM -0600, Tom Zanussi wrote:
_write - write data into the channel

Quoted 7 lines

+ * @chan: relay channel
+ * @data: data to be written
+ * @length: number of bytes to write
+ *
+ * Returns the number of bytes written, 0 if full.
+ *
+ * Writes data into the current cpu's channel buffer. irq safe.

This needs a better comment on the allowed contexts.

include/linux/relayfs_fs.h line 164

Quoted 7 lines

+ */

include/linux/relayfs_fs.h +6 -0
+static inline unsigned relay_write(struct rchan *chan,
+				   const void *data,
+				   unsigned length)
+{
+	unsigned long flags;
+	struct rchan_buf *buf = relay_get_buf(chan, smp_processor_id());

Needs to be inside the local_irq_save of course.

include/linux/relayfs_fs.h line 173

Quoted 9 lines
include/linux/relayfs_fs.h +9 -0
+
+	local_irq_save(flags);
+	if (unlikely(buf->offset + length > chan->subbuf_size))
+		length = relay_switch_subbuf(buf, length);
+	memcpy(buf->data + buf->offset, data, length);
+	buf->offset += length;
+	local_irq_restore(flags);
+ 
+	return length;

Is there any useful user case for returning length here? (e.g. are users likely to handle errors? I doubt it somehow)

If not I would eliminate it.

include/linux/relayfs_fs.h line 184

Quoted 11 lines
include/linux/relayfs_fs.h +11 -0
+}
+
+/**
+ *	__relay_write - write data into the channel
+ *	@chan: relay channel
+ *	@data: data to be written
+ *	@length: number of bytes to write
+ *
+ *	Returns the number of bytes written, 0 if full.
+ *
+ *	Writes data into the current cpu's channel buffer.  Preempt safe.

And this needs more comments on the context too.

-Andi

#6 lore raw
Quoted 14 lines · Andi Kleen

include/linux/relayfs_fs.h line 173

Quoted 10 lines
include/linux/relayfs_fs.h +9 -0
+
+	local_irq_save(flags);
+	if (unlikely(buf->offset + length > chan->subbuf_size))
+		length = relay_switch_subbuf(buf, length);
+	memcpy(buf->data + buf->offset, data, length);
+	buf->offset += length;
+	local_irq_restore(flags);
+ 
+	return length;

Is there any useful user case for returning length here? (e.g. are users likely to handle errors? I doubt it somehow)

Quoted 2 lines

If not I would eliminate it.

The main reason would be that length would be 0 only if the buffers were full, so the caller can suspend writing if it sees that, until e.g. a daemon catches up.

Thanks for your other comments - I'll make those changes in the next version.

Tom

#7 lore raw
Quoted 3 lines

The main reason would be that length would be 0 only if the buffers were full, so the caller can suspend writing if it sees that, until e.g. a daemon catches up.

Would be? Is there actually any caller doing this?

Better eliminate it and if someone really does it add a separate function that checks this condition.

-Andi

#8 lore raw

Re: [PATCH] relayfs redux, part 3 II - another comment

In reply to Tom Zanussi

Another comment on relay_write fast pathing

include/linux/relayfs_fs.h line 169

Quoted 11 lines
include/linux/relayfs_fs.h +11 -0
+static inline unsigned relay_write(struct rchan *chan,
+				   const void *data,
+				   unsigned length)
+{
+	unsigned long flags;
+	struct rchan_buf *buf = relay_get_buf(chan, smp_processor_id());
+
+	local_irq_save(flags);
+	if (unlikely(buf->offset + length > chan->subbuf_size))
+		length = relay_switch_subbuf(buf, length);
+	memcpy(buf->data + buf->offset, data, length);

I said earlier gcc would optimize the memcpy, but thinking about this again I'm not so sure anymore. The problem is that with variable buf->offset gcc cannot prove the alignment of the destination, and with unknown alignment it tends to generate a out of line call to memcpy, which is quite typically slow.

You can take a look at the generated assembly if that's true or not, but I suspect it is.

To avoid this it may be needed to play

if (__builtin_constant_p(length))

	switch (length) { 
	case 1: /* optimized version for 1 byte */ break;
	case 2: ...
	case 4: ...
	case 8:  ...
	}
else
	memcpy(...); 

games like e.g. uaccess.h does. Problem is that sometimes gcc seems to break __builtin_constant_p inside inlines, so it may be needed to move it into a macro (i would double check if that is really needed though for this case, the code is much nicer with a inline)

-Andi