diff --git a/src/tools/kernel/nk900xm-patch/fs/pipe.c b/src/tools/kernel/nk900xm-patch/fs/pipe.c index 7087310e1278..d2c45e14e6d8 100644 --- a/src/tools/kernel/nk900xm-patch/fs/pipe.c +++ b/src/tools/kernel/nk900xm-patch/fs/pipe.c @@ -28,10 +28,6 @@ #include "internal.h" -extern size_t copy_page_to_iter(struct page *page, - size_t offset, - size_t bytes, - struct iov_iter *i); /* * The max size that a non-root user is allowed to grow the pipe. Can * be set by root in /proc/sys/fs/pipe-max-size @@ -146,6 +142,55 @@ pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len, return 0; } +static int +pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len, + int atomic) +{ + unsigned long copy; + + while (len > 0) { + while (!iov->iov_len) + iov++; + copy = min_t(unsigned long, len, iov->iov_len); + + if (atomic) { + if (__copy_to_user_inatomic(iov->iov_base, from, copy)) + return -EFAULT; + } else { + if (copy_to_user(iov->iov_base, from, copy)) + return -EFAULT; + } + from += copy; + len -= copy; + iov->iov_base += copy; + iov->iov_len -= copy; + } + return 0; +} + +/* + * Attempt to pre-fault in the user memory, so we can use atomic copies. + * Returns the number of bytes not faulted in. + */ +static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len) +{ + while (!iov->iov_len) + iov++; + + while (len > 0) { + unsigned long this_len; + + this_len = min_t(unsigned long, len, iov->iov_len); + if (fault_in_pages_writeable(iov->iov_base, this_len)) + break; + + len -= this_len; + iov++; + } + + return len; +} + /* * Pre-fault in the user memory, so we can use atomic copies. */ @@ -334,15 +379,12 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov, ssize_t ret; struct iovec *iov = (struct iovec *)_iov; size_t total_len; - struct iov_iter iter; total_len = iov_length(iov, nr_segs); /* Null read succeeds. */ if (unlikely(total_len == 0)) return 0; - iov_iter_init(&iter, iov, nr_segs, total_len, 0); - do_wakeup = 0; ret = 0; __pipe_lock(pipe); @@ -352,9 +394,9 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov, int curbuf = pipe->curbuf; struct pipe_buffer *buf = pipe->bufs + curbuf; const struct pipe_buf_operations *ops = buf->ops; + void *addr; size_t chars = buf->len; - size_t written; - int error; + int error, atomic; if (chars > total_len) chars = total_len; @@ -366,11 +408,21 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov, break; } - written = copy_page_to_iter(buf->page, buf->offset, - chars, &iter); - if (unlikely(written < chars)) { + atomic = !iov_fault_in_pages_write(iov, chars); +redo: + addr = ops->map(pipe, buf, atomic); + error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic); + ops->unmap(pipe, buf, addr); + if (unlikely(error)) { + /* + * Just retry with the slow path if we failed. + */ + if (atomic) { + atomic = 0; + goto redo; + } if (!ret) - ret = -EFAULT; + ret = error; break; } ret += chars; diff --git a/src/tools/kernel/nk900xm-patch/include/linux/fs.h b/src/tools/kernel/nk900xm-patch/include/linux/fs.h index 40ff2b1eab4f..65c2be22b601 100644 --- a/src/tools/kernel/nk900xm-patch/include/linux/fs.h +++ b/src/tools/kernel/nk900xm-patch/include/linux/fs.h @@ -288,21 +288,11 @@ struct page; struct address_space; struct writeback_control; -/*struct iov_iter { +struct iov_iter { const struct iovec *iov; unsigned long nr_segs; size_t iov_offset; size_t count; -};*/ -struct iov_iter { - int type; - size_t iov_offset; - size_t count; - union { - const struct iovec *iov; - const struct bio_vec *bvec; - }; - unsigned long nr_segs; }; size_t iov_iter_copy_from_user_atomic(struct page *page, diff --git a/src/tools/kernel/nk900xm-patch/include/linux/uio.h b/src/tools/kernel/nk900xm-patch/include/linux/uio.h index 627444f27ef0..c55ce243cc09 100644 --- a/src/tools/kernel/nk900xm-patch/include/linux/uio.h +++ b/src/tools/kernel/nk900xm-patch/include/linux/uio.h @@ -9,7 +9,6 @@ #ifndef __LINUX_UIO_H #define __LINUX_UIO_H -#include #include @@ -18,12 +17,6 @@ struct kvec { size_t iov_len; }; -enum { - ITER_IOVEC = 0, - ITER_KVEC = 2, - ITER_BVEC = 4, -}; - /* * Total number of bytes covered by an iovec. * diff --git a/src/tools/kernel/nk900xm-patch/mm/Makefile b/src/tools/kernel/nk900xm-patch/mm/Makefile index be01e8c7b89c..72c5acb9345f 100644 --- a/src/tools/kernel/nk900xm-patch/mm/Makefile +++ b/src/tools/kernel/nk900xm-patch/mm/Makefile @@ -17,7 +17,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o mmu_context.o percpu.o slab_common.o \ compaction.o balloon_compaction.o \ - iov_iter.o interval_tree.o $(mmu-y) + interval_tree.o $(mmu-y) obj-y += init-mm.o