FUSE: implement direct mmap, take#2
Subsystems (4)
PATCH 1/5 mmap: don't assume f_op->mmap() doesn't change vma->vm_file
In reply to Tejun Heommap_region() assumes that vma->vm_file isn't changed by f_op->mmap() and continues to use cache file after f_op->mmap() returns. Don't assume that. This will be used by FUSE to redirect mmap to shmem_file.
--- mm/mmap.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
mm/mmap.c +1 -0
@@ -1194,6 +1194,7 @@ munmap_back: vma->vm_file = file;get_file (file); error = file->f_op->mmap (file, vma); + file = vma->vm_file;if (error) goto unmap_and_free_vma;if (vm_flags &VM_EXECUTABLE )
Signature · 1 line
1.6.0.2
PATCH 4/5 FUSE: implement fuse_req->prep()
In reply to Tejun HeoImplement ->prep() which is the opposite equivalent of ->end(). It's called right before the request is passed to userland server in the kernel context of the server. ->prep() can fail the request without disrupting the whole channel.
This will be used by direct mmap implementation.
--- fs/fuse/dev.c | 29 ++++++++++++++++++++++++++--- fs/fuse/fuse_i.h | 6 ++++++ 2 files changed, 32 insertions(+), 3 deletions(-)
fs/fuse/dev.c +26 -3
@@ -756,6 +756,7 @@ static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov, unsigned long nr_segs, loff_t pos) { int err; +bool restart;struct fuse_req *req;struct fuse_in *in;struct fuse_copy_state cs;
@@ -802,12 +803,32 @@ static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov, goto restart; }spin_unlock (&fc->lock); + + restart = false;fuse_copy_init (&cs, fc,1 , req, iov, nr_segs); + +/* +* Execute prep if available. Failure from prep doesn't +* indicate faulty channel. On failure, fail the current +* request and proceed to the next one. +*/ +if (req->prep) { + err = req->prep (fc, req); +if (err) { + restart = true; + goto finish; + } + } + err =fuse_copy_one (&cs, &in->h,sizeof (in->h)); -if (!err) - err =fuse_copy_args (&cs, in->numargs, in->argpages, - (struct fuse_arg *) in->args,0 ); +if (err) + goto finish; + + err =fuse_copy_args (&cs, in->numargs, in->argpages, + (struct fuse_arg *) in->args,0 ); + finish:fuse_copy_finish (&cs); +spin_lock (&fc->lock); req->locked =0 ;if (req->aborted) {
@@ -817,6 +838,8 @@ static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,if (err) { req->out.h.error = -EIO ;request_end (fc, req); +if (restart) + goto restart;return err; }if (!req->isreply)
fs/fuse/fuse_i.h +6 -0
@@ -291,6 +291,12 @@ struct fuse_req {/** Link on fi->writepages */ struct list_head writepages_entry; +/** Request preparation callback. Called from the kernel +context of the FUSE server before passing the request to +the FUSE server. Non-zero return from this function will +fail the request. */ +int (*prep)(struct fuse_conn *,struct fuse_req *); +/** Request completion callback. This function is called from the kernel context of the FUSE server if the request isn't being aborted. If the request is being aborted, it's
Signature · 1 line
1.6.0.2
PATCH 1/1 FUSE: implement direct mmap, take#2
Hello, Miklos, Nick, Andrew.
This is the second take of fuse-implement-direct-mmap patchset. This patchset implements direct mmap support for FUSE (and CUSE). Each direct mmap area is backed by anonymous mapping (shmem_file) and the FUSE server can decide how they are shared.
mmap request is handled in two steps. MMAP first queries the server whether it wants to share the mapping with an existing one or create a new one, and if so, with which flags. MMAP_COMMIT notifies the server the result of mmap and if successful the fd the server can use to access the mmap region.
Changes from the last take[L] are
* 0003-FUSE-don-t-let-fuse_req-end-put-the-base-referen.patch was merged and thus dropped from this series.
* Updated to the current master + implement-cuse, take#3.
This patchset contains the following patches.
0001-mmap-don-t-assume-f_op-mmap-doesn-t-change-vma.patch 0002-fdtable-export-alloc_fd.patch 0003-FUSE-make-request_wait_answer-wait-for-end-co.patch 0004-FUSE-implement-fuse_req-prep.patch 0005-FUSE-implement-direct-mmap.patch
0001-0002 update mm and fdtable for following FUSE changes. 0003-0004 update fuse_req->end() handling and add ->prep(). 0005 implements direct mmap.
Nick, Andrew, can you guys please review and ack 0001-0002?
This patchset is on top of
linus#master(80a04d3f2f94fb68b5df05e3ac6697130bc3467a)
+ [1] implement-cuse patchset, take#3
and is available in the following git tree.
git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git fuse-mmap
diffstat follows.
fs/file.c | 1 fs/fuse/cuse.c | 1 fs/fuse/dev.c | 70 ++++++-- fs/fuse/file.c | 424 +++++++++++++++++++++++++++++++++++++++++++++++++-- fs/fuse/fuse_i.h | 19 ++ include/linux/fuse.h | 47 +++++ mm/mmap.c | 1 7 files changed, 533 insertions(+), 30 deletions(-)
Thanks.
--
tejun
[L] http://thread.gmane.org/gmane.comp.file-systems.fuse.devel/7212
[1] http://thread.gmane.org/gmane.comp.file-systems.fuse.devel/7705
PATCH 5/5 FUSE: implement direct mmap
This patch implements direct mmap. It allows FUSE server to honor each mmap request with anonymous mapping. FUSE server can make multiple mmap requests share a single anonymous mapping or separate mappings as it sees fit.
mmap request is handled in two steps. MMAP first queries the server whether it wants to share the mapping with an existing one or create a new one, and if so, with which flags. MMAP_COMMIT notifies the server the result of mmap and if successful the fd the server can use to access the mmap region.
Internally, shmem_file is used to back the mmap areas and vma->vm_file is overridden from the FUSE file to the shmem_file.
For details, please read the comment on top offuse_file_direct_mmap().
--- fs/fuse/cuse.c | 1 + fs/fuse/file.c | 424 ++++++++++++++++++++++++++++++++++++++++++++++++-- fs/fuse/fuse_i.h | 8 + include/linux/fuse.h | 47 ++++++ 4 files changed, 470 insertions(+), 10 deletions(-)
fs/fuse/cuse.c +1 -0
@@ -180,6 +180,7 @@ static const struct file_operations cuse_frontend_fops = { .unlocked_ioctl = cuse_file_ioctl, .compat_ioctl = cuse_file_compat_ioctl, .poll = fuse_file_poll, + .mmap = fuse_file_direct_mmap, };
fs/fuse/file.c +414 -10
448 lines not on this page · raw message
fs/fuse/fuse_i.h +8 -0
@@ -271,6 +271,13 @@ struct fuse_req {struct fuse_write_out out; } write;struct fuse_lk_in lk_in; +struct { +/** to move filp for mmap between client and server */ +struct file *file; + } mmap; +struct { +struct fuse_munmap_in in; + } munmap; } misc;/** page vector */
@@ -596,6 +603,7 @@ int fuse_flush(struct file *file, fl_owner_t id);* Send FSYNCDIR or FSYNC request */ int fuse_fsync (struct file *file,struct dentry *de,int datasync); +int fuse_file_direct_mmap (struct file *file,struct vm_area_struct *vma);/** * Send IOCTL request
include/linux/fuse.h +47 -0
65 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH 2/5 fdtable: export alloc_fd()
In reply to Tejun HeoExport alloc_fd(). Will be used by FUSE.
--- fs/file.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
fs/file.c +1 -0
@@ -487,6 +487,7 @@ out:spin_unlock (&files->file_lock);return error; } +EXPORT_SYMBOL_GPL (alloc_fd);int get_unused_fd (void ) {
Signature · 1 line
1.6.0.2
PATCH 3/5 FUSE: make request_wait_answer() wait for ->end() completion
In reply to Tejun HeoPreviously, a request was marked FINISHED before ->end() is executed and thus request_wait_answer() can return before it's done. This patch makes request_wait_answer() wait for ->end() to finish before returning.
Note that no current ->end() user waits for request completion, so this change doesn't cause any behavior difference.
While at it, beef up the comment above ->end() hook and clarify when and where it's called.
--- fs/fuse/dev.c | 41 +++++++++++++++++++++++++---------------- fs/fuse/fuse_i.h | 5 ++++- 2 files changed, 29 insertions(+), 17 deletions(-)
fs/fuse/dev.c +25 -16
@@ -278,7 +278,6 @@ __releases(&fc->lock) req->end =NULL ;list_del (&req->list);list_del (&req->intr_entry); - req->state =FUSE_REQ_FINISHED ;if (req->background) {if (fc->num_background ==FUSE_MAX_BACKGROUND ) { fc->blocked =0 ;
@@ -293,10 +292,21 @@ __releases(&fc->lock) fc->active_background--;flush_bg_queue (fc); } +spin_unlock (&fc->lock); -wake_up (&req->waitq); -if (end) + +if (end) {end (fc, req); +smp_wmb (); + } + +/* +* We own this request and wake_up() has enough memory +* barrier, no need to grab spin lock to set state. +*/ + req->state =FUSE_REQ_FINISHED ; + +wake_up (&req->waitq);fuse_put_request (fc, req); }
@@ -372,17 +382,16 @@ __acquires(&fc->lock)return ; aborted: -BUG_ON (req->state !=FUSE_REQ_FINISHED ); -if (req->locked) { -/* This is uninterruptible sleep, because data is -being copied to/from the buffers of req. During -locked state, there mustn't be any filesystem -operation (e.g. page fault), since that could lead -to deadlock */ -spin_unlock (&fc->lock); -wait_event (req->waitq, !req->locked); -spin_lock (&fc->lock); - } +spin_unlock (&fc->lock); +wait_event (req->waitq, req->state ==FUSE_REQ_FINISHED ); +/* +* This is uninterruptible sleep, because data is being copied +* to/from the buffers of req. During locked state, there +* mustn't be any filesystem operation (e.g. page fault), +* since that could lead to deadlock +*/ +wait_event (req->waitq, !req->locked); +spin_lock (&fc->lock); }void fuse_request_send (struct fuse_conn *fc,struct fuse_req *req)
@@ -1060,9 +1069,7 @@ __acquires(&fc->lock) req->aborted =1 ; req->out.h.error = -ECONNABORTED ; - req->state =FUSE_REQ_FINISHED ;list_del_init (&req->list); -wake_up (&req->waitq);if (end) { req->end =NULL ;__fuse_get_request (req);
@@ -1072,6 +1079,8 @@ __acquires(&fc->lock)fuse_put_request (fc, req);spin_lock (&fc->lock); } + req->state =FUSE_REQ_FINISHED ; +wake_up (&req->waitq); } }
fs/fuse/fuse_i.h +4 -1
@@ -291,7 +291,10 @@ struct fuse_req {/** Link on fi->writepages */ struct list_head writepages_entry; -/** Request completion callback */ +/** Request completion callback. This function is called from +the kernel context of the FUSE server if the request isn't +being aborted. If the request is being aborted, it's +called from the kernel context of the aborting process. */ void (*end)(struct fuse_conn *,struct fuse_req *);/** Request is stolen from fuse_file->reserved_req */
Signature · 1 line
1.6.0.2
Re: [PATCH 1/5] mmap: don't assume f_op->mmap() doesn't change vma->vm_file
In reply to Tejun HeoQuoted 2 lines · Tejun Heo
mmap_region()assumes thatvma->vm_fileisn't changed byf_op->mmap()and continues to use cache file afterf_op->mmap()returns.
It does use "file" again in the unmap_and_free_vma error path (isn't that reasonable? if the ->mmap failed, it shouldn't have mucked with vma; and even if it has, then we'd better not change the current behaviour of which to fput), but I don't see where else.
Further down, covering both vma->vm_file previously set and previously unset cases, there is a "file = vma->vm_file;" before file is used. So I think this patch is not necessary - if it is necessary, it's already a bug, because already we switch from /dev/zero to a shmem file there.
Hugh
Quoted 23 lines
Don't assume that. This will be used by FUSE to redirect mmap to
shmem_file.Signed-off-by Tejun Heo <[email protected]>Cc Nick Piggin <[email protected]>--- mm/mmap.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)mm/mmap.c +1
-0@@ -1194,6 +1194,7 @@ munmap_back: vma->vm_file = file;get_file (file); error = file->f_op->mmap (file, vma); + file = vma->vm_file;if (error) goto unmap_and_free_vma;if (vm_flags &VM_EXECUTABLE )Signature · 1 line
1.6.0.2
Re: [PATCH 1/5] mmap: don't assume f_op->mmap() doesn't change vma->vm_file
Hello,
Quoted 10 lines · Hugh Dickins
Quoted 2 lines · Tejun Heo
mmap_region()assumes thatvma->vm_fileisn't changed byf_op->mmap()and continues to use cache file afterf_op->mmap()returns.It does use "file" again in the
unmap_and_free_vmaerror path (isn't that reasonable? if the ->mmap failed, it shouldn't have mucked with vma; and even if it has, then we'd better not change the current behaviour of which to fput), but I don't see where else.
Quoted 5 lines
Further down, covering both
vma->vm_filepreviously set and previously unset cases, there is a "file =vma->vm_file;" before file is used. So I think this patch is not necessary - if it is necessary, it's already a bug, because already we switch from/dev/zeroto a shmem file there.
Right, ->mmap() shouldn't modify @vma if it has failed. I was trying to clarify that @vma->file may change as the code was a bit confusing regarding whether @vma->vm_file is allowed to be substituted or not. Hmmmm... how about adding a BUG_ON() or WARN_ON() in the failure path to make sure @vma->vm_file hasn't changed?
Anyways, I'm dropping this patch and updating FUSE mmap implementation accordingly. Thanks for the comment.
Signature · 1 line
tejun
Re: [PATCH 1/5] mmap: don't assume f_op->mmap() doesn't change vma->vm_file
In reply to Tejun HeoThis patch is dropped as per Hugh Dickins' comment.
Thanks.
Signature · 1 line
tejun
PATCH 1/1 FUSE: implement direct mmap
In reply to Tejun HeoThis patch implements direct mmap. It allows FUSE server to honor each mmap request with anonymous mapping. FUSE server can make multiple mmap requests share a single anonymous mapping or separate mappings as it sees fit.
mmap request is handled in two steps. MMAP first queries the server whether it wants to share the mapping with an existing one or create a new one, and if so, with which flags. MMAP_COMMIT notifies the server the result of mmap and if successful the fd the server can use to access the mmap region.
Internally, shmem_file is used to back the mmap areas and vma->vm_file is overridden from the FUSE file to the shmem_file.
For details, please read the comment on top offuse_file_direct_mmap().
---
Updated such that vma is not modified on failure return. git tree is updated accordingly.
fs/fuse/cuse.c | 1 + fs/fuse/file.c | 428 ++++++++++++++++++++++++++++++++++++++++++++++++-- fs/fuse/fuse_i.h | 8 + include/linux/fuse.h | 47 ++++++ 4 files changed, 474 insertions(+), 10 deletions(-)
fs/fuse/cuse.c +1 -0
@@ -180,6 +180,7 @@ static const struct file_operations cuse_frontend_fops = { .unlocked_ioctl = cuse_file_ioctl, .compat_ioctl = cuse_file_compat_ioctl, .poll = fuse_file_poll, + .mmap = fuse_file_direct_mmap, };
fs/fuse/file.c +418 -10
452 lines not on this page · raw message
fs/fuse/fuse_i.h +8 -0
@@ -271,6 +271,13 @@ struct fuse_req {struct fuse_write_out out; } write;struct fuse_lk_in lk_in; +struct { +/** to move filp for mmap between client and server */ +struct file *file; + } mmap; +struct { +struct fuse_munmap_in in; + } munmap; } misc;/** page vector */
@@ -596,6 +603,7 @@ int fuse_flush(struct file *file, fl_owner_t id);* Send FSYNCDIR or FSYNC request */ int fuse_fsync (struct file *file,struct dentry *de,int datasync); +int fuse_file_direct_mmap (struct file *file,struct vm_area_struct *vma);/** * Send IOCTL request
include/linux/fuse.h +47 -0
65 lines not on this page · raw message
Signature · 1 line
1.6.0.2