Skip to content

Commit fcf6340

Browse files
Christopher Yeohtorvalds
authored andcommitted
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.tgz Signed-off-by: Chris 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: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 32ea845 commit fcf6340

17 files changed

Lines changed: 550 additions & 17 deletions

File tree

‎arch/powerpc/include/asm/systbl.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,5 @@ COMPAT_SYS_SPU(clock_adjtime)
354354
SYSCALL_SPU(syncfs)
355355
COMPAT_SYS_SPU(sendmmsg)
356356
SYSCALL_SPU(setns)
357+
COMPAT_SYS(process_vm_readv)
358+
COMPAT_SYS(process_vm_writev)

‎arch/powerpc/include/asm/unistd.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,12 @@
373373
#define __NR_syncfs 348
374374
#define __NR_sendmmsg 349
375375
#define __NR_setns 350
376+
#define __NR_process_vm_readv 351
377+
#define __NR_process_vm_writev 352
376378

377379
#ifdef __KERNEL__
378380

379-
#define __NR_syscalls 351
381+
#define __NR_syscalls 353
380382

381383
#define __NR__exit __NR_exit
382384
#define NR_syscalls __NR_syscalls

‎arch/x86/ia32/ia32entry.S‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,4 +850,6 @@ ia32_sys_call_table:
850850
.quad sys_syncfs
851851
.quad compat_sys_sendmmsg /* 345 */
852852
.quad sys_setns
853+
.quad compat_sys_process_vm_readv
854+
.quad compat_sys_process_vm_writev
853855
ia32_syscall_end:

‎arch/x86/include/asm/unistd_32.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,12 @@
352352
#define __NR_syncfs 344
353353
#define __NR_sendmmsg 345
354354
#define __NR_setns 346
355+
#define __NR_process_vm_readv 347
356+
#define __NR_process_vm_writev 348
355357

356358
#ifdef __KERNEL__
357359

358-
#define NR_syscalls 347
360+
#define NR_syscalls 349
359361

360362
#define __ARCH_WANT_IPC_PARSE_VERSION
361363
#define __ARCH_WANT_OLD_READDIR

‎arch/x86/include/asm/unistd_64.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ __SYSCALL(__NR_sendmmsg, sys_sendmmsg)
682682
__SYSCALL(__NR_setns, sys_setns)
683683
#define __NR_getcpu 309
684684
__SYSCALL(__NR_getcpu, sys_getcpu)
685+
#define __NR_process_vm_readv 310
686+
__SYSCALL(__NR_process_vm_readv, sys_process_vm_readv)
687+
#define __NR_process_vm_writev 311
688+
__SYSCALL(__NR_process_vm_writev, sys_process_vm_writev)
685689

686690
#ifndef __NO_STUBS
687691
#define __ARCH_WANT_OLD_READDIR

‎arch/x86/kernel/syscall_table_32.S‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,5 @@ ENTRY(sys_call_table)
346346
.long sys_syncfs
347347
.long sys_sendmmsg /* 345 */
348348
.long sys_setns
349+
.long sys_process_vm_readv
350+
.long sys_process_vm_writev

‎fs/aio.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,13 +1387,13 @@ static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
13871387
ret = compat_rw_copy_check_uvector(type,
13881388
(struct compat_iovec __user *)kiocb->ki_buf,
13891389
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
1390-
&kiocb->ki_iovec);
1390+
&kiocb->ki_iovec, 1);
13911391
else
13921392
#endif
13931393
ret = rw_copy_check_uvector(type,
13941394
(struct iovec __user *)kiocb->ki_buf,
13951395
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
1396-
&kiocb->ki_iovec);
1396+
&kiocb->ki_iovec, 1);
13971397
if (ret < 0)
13981398
goto out;
13991399

‎fs/compat.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ compat_sys_io_getevents(aio_context_t ctx_id,
546546
ssize_t compat_rw_copy_check_uvector(int type,
547547
const struct compat_iovec __user *uvector, unsigned long nr_segs,
548548
unsigned long fast_segs, struct iovec *fast_pointer,
549-
struct iovec **ret_pointer)
549+
struct iovec **ret_pointer, int check_access)
550550
{
551551
compat_ssize_t tot_len;
552552
struct iovec *iov = *ret_pointer = fast_pointer;
@@ -593,7 +593,8 @@ ssize_t compat_rw_copy_check_uvector(int type,
593593
}
594594
if (len < 0) /* size_t not fitting in compat_ssize_t .. */
595595
goto out;
596-
if (!access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
596+
if (check_access &&
597+
!access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
597598
ret = -EFAULT;
598599
goto out;
599600
}
@@ -1107,7 +1108,7 @@ static ssize_t compat_do_readv_writev(int type, struct file *file,
11071108
goto out;
11081109

11091110
tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1110-
UIO_FASTIOV, iovstack, &iov);
1111+
UIO_FASTIOV, iovstack, &iov, 1);
11111112
if (tot_len == 0) {
11121113
ret = 0;
11131114
goto out;

‎fs/read_write.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
633633
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
634634
unsigned long nr_segs, unsigned long fast_segs,
635635
struct iovec *fast_pointer,
636-
struct iovec **ret_pointer)
636+
struct iovec **ret_pointer,
637+
int check_access)
637638
{
638639
unsigned long seg;
639640
ssize_t ret;
@@ -689,7 +690,8 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
689690
ret = -EINVAL;
690691
goto out;
691692
}
692-
if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
693+
if (check_access
694+
&& unlikely(!access_ok(vrfy_dir(type), buf, len))) {
693695
ret = -EFAULT;
694696
goto out;
695697
}
@@ -721,7 +723,7 @@ static ssize_t do_readv_writev(int type, struct file *file,
721723
}
722724

723725
ret = rw_copy_check_uvector(type, uvector, nr_segs,
724-
ARRAY_SIZE(iovstack), iovstack, &iov);
726+
ARRAY_SIZE(iovstack), iovstack, &iov, 1);
725727
if (ret <= 0)
726728
goto out;
727729

‎include/linux/compat.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ extern ssize_t compat_rw_copy_check_uvector(int type,
547547
const struct compat_iovec __user *uvector,
548548
unsigned long nr_segs,
549549
unsigned long fast_segs, struct iovec *fast_pointer,
550-
struct iovec **ret_pointer);
550+
struct iovec **ret_pointer,
551+
int check_access);
551552

552553
extern void __user *compat_alloc_user_space(unsigned long len);
553554

0 commit comments

Comments
 (0)