Commit fcf63409 authored by Christopher Yeoh's avatar Christopher Yeoh Committed by Linus Torvalds

Cross Memory Attach

The basic idea behind cross memory attach is to allow MPI programs doing
intra-node communication to do a single copy of the message rather than a
double copy of the message via shared memory.

The following patch attempts to achieve this by allowing a destination
process, given an address and size from a source process, to copy memory
directly from the source process into its own address space via a system
call.  There is also a symmetrical ability to copy from the current
process's address space into a destination process's address space.

- Use of /proc/pid/mem has been considered, but there are issues with
  using it:
  - Does not allow for specifying iovecs for both src and dest, assuming
    preadv or pwritev was implemented either the area read from or
  written to would need to be contiguous.
  - Currently mem_read allows only processes who are currently
  ptrace'ing the target and are still able to ptrace the target to read
  from the target. This check could possibly be moved to the open call,
  but its not clear exactly what race this restriction is stopping
  (reason  appears to have been lost)
  - Having to send the fd of /proc/self/mem via SCM_RIGHTS on unix
  domain socket is a bit ugly from a userspace point of view,
  especially when you may have hundreds if not (eventually) thousands
  of processes  that all need to do this with each other
  - Doesn't allow for some future use of the interface we would like to
  consider adding in the future (see below)
  - Interestingly reading from /proc/pid/mem currently actually
  involves two copies! (But this could be fixed pretty easily)

As mentioned previously use of vmsplice instead was considered, but has
problems.  Since you need the reader and writer working co-operatively if
the pipe is not drained then you block.  Which requires some wrapping to
do non blocking on the send side or polling on the receive.  In all to all
communication it requires ordering otherwise you can deadlock.  And in the
example of many MPI tasks writing to one MPI task vmsplice serialises the
copying.

There are some cases of MPI collectives where even a single copy interface
does not get us the performance gain we could.  For example in an
MPI_Reduce rather than copy the data from the source we would like to
instead use it directly in a mathops (say the reduce is doing a sum) as
this would save us doing a copy.  We don't need to keep a copy of the data
from the source.  I haven't implemented this, but I think this interface
could in the future do all this through the use of the flags - eg could
specify the math operation and type and the kernel rather than just
copying the data would apply the specified operation between the source
and destination and store it in the destination.

Although we don't have a "second user" of the interface (though I've had
some nibbles from people who may be interested in using it for intra
process messaging which is not MPI).  This interface is something which
hardware vendors are already doing for their custom drivers to implement
fast local communication.  And so in addition to this being useful for
OpenMPI it would mean the driver maintainers don't have to fix things up
when the mm changes.

There was some discussion about how much faster a true zero copy would
go. Here's a link back to the email with some testing I did on that:

http://marc.info/?l=linux-mm&m=130105930902915&w=2

There is a basic man page for the proposed interface here:

http://ozlabs.org/~cyeoh/cma/process_vm_readv.txt

This has been implemented for x86 and powerpc, other architecture should
mainly (I think) just need to add syscall numbers for the process_vm_readv
and process_vm_writev. There are 32 bit compatibility versions for
64-bit kernels.

For arch maintainers there are some simple tests to be able to quickly
verify that the syscalls are working correctly here:

http://ozlabs.org/~cyeoh/cma/cma-test-20110718.tgzSigned-off-by: default avatarChris Yeoh <yeohc@au1.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: David Howells <dhowells@redhat.com>
Cc: James Morris <jmorris@namei.org>
Cc: <linux-man@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 32ea845d
...@@ -354,3 +354,5 @@ COMPAT_SYS_SPU(clock_adjtime) ...@@ -354,3 +354,5 @@ COMPAT_SYS_SPU(clock_adjtime)
SYSCALL_SPU(syncfs) SYSCALL_SPU(syncfs)
COMPAT_SYS_SPU(sendmmsg) COMPAT_SYS_SPU(sendmmsg)
SYSCALL_SPU(setns) SYSCALL_SPU(setns)
COMPAT_SYS(process_vm_readv)
COMPAT_SYS(process_vm_writev)
...@@ -373,10 +373,12 @@ ...@@ -373,10 +373,12 @@
#define __NR_syncfs 348 #define __NR_syncfs 348
#define __NR_sendmmsg 349 #define __NR_sendmmsg 349
#define __NR_setns 350 #define __NR_setns 350
#define __NR_process_vm_readv 351
#define __NR_process_vm_writev 352
#ifdef __KERNEL__ #ifdef __KERNEL__
#define __NR_syscalls 351 #define __NR_syscalls 353
#define __NR__exit __NR_exit #define __NR__exit __NR_exit
#define NR_syscalls __NR_syscalls #define NR_syscalls __NR_syscalls
......
...@@ -850,4 +850,6 @@ ia32_sys_call_table: ...@@ -850,4 +850,6 @@ ia32_sys_call_table:
.quad sys_syncfs .quad sys_syncfs
.quad compat_sys_sendmmsg /* 345 */ .quad compat_sys_sendmmsg /* 345 */
.quad sys_setns .quad sys_setns
.quad compat_sys_process_vm_readv
.quad compat_sys_process_vm_writev
ia32_syscall_end: ia32_syscall_end:
...@@ -352,10 +352,12 @@ ...@@ -352,10 +352,12 @@
#define __NR_syncfs 344 #define __NR_syncfs 344
#define __NR_sendmmsg 345 #define __NR_sendmmsg 345
#define __NR_setns 346 #define __NR_setns 346
#define __NR_process_vm_readv 347
#define __NR_process_vm_writev 348
#ifdef __KERNEL__ #ifdef __KERNEL__
#define NR_syscalls 347 #define NR_syscalls 349
#define __ARCH_WANT_IPC_PARSE_VERSION #define __ARCH_WANT_IPC_PARSE_VERSION
#define __ARCH_WANT_OLD_READDIR #define __ARCH_WANT_OLD_READDIR
......
...@@ -682,6 +682,10 @@ __SYSCALL(__NR_sendmmsg, sys_sendmmsg) ...@@ -682,6 +682,10 @@ __SYSCALL(__NR_sendmmsg, sys_sendmmsg)
__SYSCALL(__NR_setns, sys_setns) __SYSCALL(__NR_setns, sys_setns)
#define __NR_getcpu 309 #define __NR_getcpu 309
__SYSCALL(__NR_getcpu, sys_getcpu) __SYSCALL(__NR_getcpu, sys_getcpu)
#define __NR_process_vm_readv 310
__SYSCALL(__NR_process_vm_readv, sys_process_vm_readv)
#define __NR_process_vm_writev 311
__SYSCALL(__NR_process_vm_writev, sys_process_vm_writev)
#ifndef __NO_STUBS #ifndef __NO_STUBS
#define __ARCH_WANT_OLD_READDIR #define __ARCH_WANT_OLD_READDIR
......
...@@ -346,3 +346,5 @@ ENTRY(sys_call_table) ...@@ -346,3 +346,5 @@ ENTRY(sys_call_table)
.long sys_syncfs .long sys_syncfs
.long sys_sendmmsg /* 345 */ .long sys_sendmmsg /* 345 */
.long sys_setns .long sys_setns
.long sys_process_vm_readv
.long sys_process_vm_writev
...@@ -1387,13 +1387,13 @@ static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat) ...@@ -1387,13 +1387,13 @@ static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
ret = compat_rw_copy_check_uvector(type, ret = compat_rw_copy_check_uvector(type,
(struct compat_iovec __user *)kiocb->ki_buf, (struct compat_iovec __user *)kiocb->ki_buf,
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec, kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
&kiocb->ki_iovec); &kiocb->ki_iovec, 1);
else else
#endif #endif
ret = rw_copy_check_uvector(type, ret = rw_copy_check_uvector(type,
(struct iovec __user *)kiocb->ki_buf, (struct iovec __user *)kiocb->ki_buf,
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec, kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
&kiocb->ki_iovec); &kiocb->ki_iovec, 1);
if (ret < 0) if (ret < 0)
goto out; goto out;
......
...@@ -546,7 +546,7 @@ compat_sys_io_getevents(aio_context_t ctx_id, ...@@ -546,7 +546,7 @@ compat_sys_io_getevents(aio_context_t ctx_id,
ssize_t compat_rw_copy_check_uvector(int type, ssize_t compat_rw_copy_check_uvector(int type,
const struct compat_iovec __user *uvector, unsigned long nr_segs, const struct compat_iovec __user *uvector, unsigned long nr_segs,
unsigned long fast_segs, struct iovec *fast_pointer, unsigned long fast_segs, struct iovec *fast_pointer,
struct iovec **ret_pointer) struct iovec **ret_pointer, int check_access)
{ {
compat_ssize_t tot_len; compat_ssize_t tot_len;
struct iovec *iov = *ret_pointer = fast_pointer; struct iovec *iov = *ret_pointer = fast_pointer;
...@@ -593,7 +593,8 @@ ssize_t compat_rw_copy_check_uvector(int type, ...@@ -593,7 +593,8 @@ ssize_t compat_rw_copy_check_uvector(int type,
} }
if (len < 0) /* size_t not fitting in compat_ssize_t .. */ if (len < 0) /* size_t not fitting in compat_ssize_t .. */
goto out; goto out;
if (!access_ok(vrfy_dir(type), compat_ptr(buf), len)) { if (check_access &&
!access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
ret = -EFAULT; ret = -EFAULT;
goto out; goto out;
} }
...@@ -1107,7 +1108,7 @@ static ssize_t compat_do_readv_writev(int type, struct file *file, ...@@ -1107,7 +1108,7 @@ static ssize_t compat_do_readv_writev(int type, struct file *file,
goto out; goto out;
tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs, tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
UIO_FASTIOV, iovstack, &iov); UIO_FASTIOV, iovstack, &iov, 1);
if (tot_len == 0) { if (tot_len == 0) {
ret = 0; ret = 0;
goto out; goto out;
......
...@@ -633,7 +633,8 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov, ...@@ -633,7 +633,8 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
unsigned long nr_segs, unsigned long fast_segs, unsigned long nr_segs, unsigned long fast_segs,
struct iovec *fast_pointer, struct iovec *fast_pointer,
struct iovec **ret_pointer) struct iovec **ret_pointer,
int check_access)
{ {
unsigned long seg; unsigned long seg;
ssize_t ret; ssize_t ret;
...@@ -689,7 +690,8 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, ...@@ -689,7 +690,8 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
if (unlikely(!access_ok(vrfy_dir(type), buf, len))) { if (check_access
&& unlikely(!access_ok(vrfy_dir(type), buf, len))) {
ret = -EFAULT; ret = -EFAULT;
goto out; goto out;
} }
...@@ -721,7 +723,7 @@ static ssize_t do_readv_writev(int type, struct file *file, ...@@ -721,7 +723,7 @@ static ssize_t do_readv_writev(int type, struct file *file,
} }
ret = rw_copy_check_uvector(type, uvector, nr_segs, ret = rw_copy_check_uvector(type, uvector, nr_segs,
ARRAY_SIZE(iovstack), iovstack, &iov); ARRAY_SIZE(iovstack), iovstack, &iov, 1);
if (ret <= 0) if (ret <= 0)
goto out; goto out;
......
...@@ -547,7 +547,8 @@ extern ssize_t compat_rw_copy_check_uvector(int type, ...@@ -547,7 +547,8 @@ extern ssize_t compat_rw_copy_check_uvector(int type,
const struct compat_iovec __user *uvector, const struct compat_iovec __user *uvector,
unsigned long nr_segs, unsigned long nr_segs,
unsigned long fast_segs, struct iovec *fast_pointer, unsigned long fast_segs, struct iovec *fast_pointer,
struct iovec **ret_pointer); struct iovec **ret_pointer,
int check_access);
extern void __user *compat_alloc_user_space(unsigned long len); extern void __user *compat_alloc_user_space(unsigned long len);
......
...@@ -1633,9 +1633,10 @@ struct inode_operations { ...@@ -1633,9 +1633,10 @@ struct inode_operations {
struct seq_file; struct seq_file;
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
unsigned long nr_segs, unsigned long fast_segs, unsigned long nr_segs, unsigned long fast_segs,
struct iovec *fast_pointer, struct iovec *fast_pointer,
struct iovec **ret_pointer); struct iovec **ret_pointer,
int check_access);
extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *); extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *); extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
......
...@@ -844,4 +844,17 @@ asmlinkage long sys_open_by_handle_at(int mountdirfd, ...@@ -844,4 +844,17 @@ asmlinkage long sys_open_by_handle_at(int mountdirfd,
struct file_handle __user *handle, struct file_handle __user *handle,
int flags); int flags);
asmlinkage long sys_setns(int fd, int nstype); asmlinkage long sys_setns(int fd, int nstype);
asmlinkage long sys_process_vm_readv(pid_t pid,
const struct iovec __user *lvec,
unsigned long liovcnt,
const struct iovec __user *rvec,
unsigned long riovcnt,
unsigned long flags);
asmlinkage long sys_process_vm_writev(pid_t pid,
const struct iovec __user *lvec,
unsigned long liovcnt,
const struct iovec __user *rvec,
unsigned long riovcnt,
unsigned long flags);
#endif #endif
...@@ -145,6 +145,10 @@ cond_syscall(sys_io_submit); ...@@ -145,6 +145,10 @@ cond_syscall(sys_io_submit);
cond_syscall(sys_io_cancel); cond_syscall(sys_io_cancel);
cond_syscall(sys_io_getevents); cond_syscall(sys_io_getevents);
cond_syscall(sys_syslog); cond_syscall(sys_syslog);
cond_syscall(sys_process_vm_readv);
cond_syscall(sys_process_vm_writev);
cond_syscall(compat_sys_process_vm_readv);
cond_syscall(compat_sys_process_vm_writev);
/* arch-specific weak syscall entries */ /* arch-specific weak syscall entries */
cond_syscall(sys_pciconfig_read); cond_syscall(sys_pciconfig_read);
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
mmu-y := nommu.o mmu-y := nommu.o
mmu-$(CONFIG_MMU) := fremap.o highmem.o madvise.o memory.o mincore.o \ mmu-$(CONFIG_MMU) := fremap.o highmem.o madvise.o memory.o mincore.o \
mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \ mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \
vmalloc.o pagewalk.o pgtable-generic.o vmalloc.o pagewalk.o pgtable-generic.o \
process_vm_access.o
obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
maccess.o page_alloc.o page-writeback.o \ maccess.o page_alloc.o page-writeback.o \
......
This diff is collapsed.
...@@ -38,7 +38,7 @@ long compat_keyctl_instantiate_key_iov( ...@@ -38,7 +38,7 @@ long compat_keyctl_instantiate_key_iov(
ret = compat_rw_copy_check_uvector(WRITE, _payload_iov, ioc, ret = compat_rw_copy_check_uvector(WRITE, _payload_iov, ioc,
ARRAY_SIZE(iovstack), ARRAY_SIZE(iovstack),
iovstack, &iov); iovstack, &iov, 1);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (ret == 0) if (ret == 0)
......
...@@ -1065,7 +1065,7 @@ long keyctl_instantiate_key_iov(key_serial_t id, ...@@ -1065,7 +1065,7 @@ long keyctl_instantiate_key_iov(key_serial_t id,
goto no_payload; goto no_payload;
ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc, ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
ARRAY_SIZE(iovstack), iovstack, &iov); ARRAY_SIZE(iovstack), iovstack, &iov, 1);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (ret == 0) if (ret == 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment