make vfs & filesystems use failable copying

Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.

Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.

This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.

Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.

	. kernel: add CPF_TRY flag to safecopies
	. kernel: only request writable ranges to VM for the
	  target buffer when copying fails
	. do copying in VFS TRY-first
	. some fixes in VM to build SANITYCHECK mode
	. add regression test for the cases where
	  - a FS system call needs memory mapped in a process that the
	    FS itself must map.
	  - such a range covers more than one file-mapped region.
	. add 'try' mode to vircopy, physcopy
	. add flags field to copy kernel call messages
	. if CP_FLAG_TRY is set, do not transparently try
	  to fix memory ranges
	. for use by VFS when accessing user buffers to avoid
	  deadlock
	. remove some obsolete backwards compatability assignments
        . VFS: let thread scheduling work for VM requests too
          Allows VFS to make calls to VM while suspending and resuming
          the currently running thread. Does currently not work for the
          main thread.
        . VM: add fix memory range call for use by VFS

Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
This commit is contained in:
Ben Gras
2014-01-16 14:22:13 +01:00
committed by Lionel Sambuc
parent 8c277f99f4
commit 565f13088f
58 changed files with 705 additions and 296 deletions

View File

@@ -398,7 +398,8 @@ size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
* vm_suspend *
*===========================================================================*/
static void vm_suspend(struct proc *caller, const struct proc *target,
const vir_bytes linaddr, const vir_bytes len, const int type)
const vir_bytes linaddr, const vir_bytes len, const int type,
const int writeflag)
{
/* This range is not OK for this process. Set parameters
* of the request and notify VM about the pending request.
@@ -412,7 +413,7 @@ static void vm_suspend(struct proc *caller, const struct proc *target,
caller->p_vmrequest.target = target->p_endpoint;
caller->p_vmrequest.params.check.start = linaddr;
caller->p_vmrequest.params.check.length = len;
caller->p_vmrequest.params.check.writeflag = 1;
caller->p_vmrequest.params.check.writeflag = writeflag;
caller->p_vmrequest.type = type;
/* Connect caller on vmrequest wait queue. */
@@ -426,7 +427,7 @@ static void vm_suspend(struct proc *caller, const struct proc *target,
* vm_check_range *
*===========================================================================*/
int vm_check_range(struct proc *caller, struct proc *target,
vir_bytes vir_addr, size_t bytes)
vir_bytes vir_addr, size_t bytes, int writeflag)
{
/* Public interface to vm_suspend(), for use by kernel calls. On behalf
* of 'caller', call into VM to check linear virtual address range of
@@ -442,7 +443,8 @@ int vm_check_range(struct proc *caller, struct proc *target,
(r = caller->p_vmrequest.vmresult) != OK)
return r;
vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL);
vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
writeflag);
return VMSUSPEND;
}
@@ -520,7 +522,7 @@ int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
/* If a process pagefaults, VM may help out */
if (whoptr) {
vm_suspend(caller, whoptr, ph, count,
VMSTYPE_KERNELCALL);
VMSTYPE_KERNELCALL, 1);
assert(catch_pagefaults);
catch_pagefaults = 0;
return VMSUSPEND;
@@ -589,6 +591,7 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
int writeflag;
struct proc *target = NULL;
phys_bytes lin;
if(r != EFAULT_SRC && r != EFAULT_DST)
@@ -600,9 +603,11 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
if(r == EFAULT_SRC) {
lin = vir_addr[_SRC_]->offset;
target = procs[_SRC_];
writeflag = 0;
} else if(r == EFAULT_DST) {
lin = vir_addr[_DST_]->offset;
target = procs[_DST_];
writeflag = 1;
} else {
panic("r strange: %d", r);
}
@@ -610,7 +615,7 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
assert(caller);
assert(target);
vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL);
vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
return VMSUSPEND;
}

View File

@@ -72,7 +72,7 @@ int do_sdevio(struct proc * caller, message *m_ptr)
count,
req_dir == _DIO_INPUT ? CPF_WRITE : CPF_READ,
(vir_bytes) m_ptr->DIO_OFFSET,
&newoffset, &newep) != OK) {
&newoffset, &newep, NULL) != OK) {
printf("do_sdevio: verify_grant failed\n");
return EPERM;
}

View File

@@ -421,7 +421,8 @@ size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
* vm_suspend *
*===========================================================================*/
static void vm_suspend(struct proc *caller, const struct proc *target,
const vir_bytes linaddr, const vir_bytes len, const int type)
const vir_bytes linaddr, const vir_bytes len, const int type,
const int writeflag)
{
/* This range is not OK for this process. Set parameters
* of the request and notify VM about the pending request.
@@ -437,7 +438,7 @@ static void vm_suspend(struct proc *caller, const struct proc *target,
caller->p_vmrequest.target = target->p_endpoint;
caller->p_vmrequest.params.check.start = linaddr;
caller->p_vmrequest.params.check.length = len;
caller->p_vmrequest.params.check.writeflag = 1;
caller->p_vmrequest.params.check.writeflag = writeflag;
caller->p_vmrequest.type = type;
/* Connect caller on vmrequest wait queue. */
@@ -451,7 +452,7 @@ static void vm_suspend(struct proc *caller, const struct proc *target,
* vm_check_range *
*===========================================================================*/
int vm_check_range(struct proc *caller, struct proc *target,
vir_bytes vir_addr, size_t bytes)
vir_bytes vir_addr, size_t bytes, int writeflag)
{
/* Public interface to vm_suspend(), for use by kernel calls. On behalf
* of 'caller', call into VM to check linear virtual address range of
@@ -467,7 +468,8 @@ int vm_check_range(struct proc *caller, struct proc *target,
(r = caller->p_vmrequest.vmresult) != OK)
return r;
vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL);
vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
writeflag);
return VMSUSPEND;
}
@@ -619,7 +621,7 @@ int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
/* If a process pagefaults, VM may help out */
if (whoptr) {
vm_suspend(caller, whoptr, ph, count,
VMSTYPE_KERNELCALL);
VMSTYPE_KERNELCALL, 1);
assert(catch_pagefaults);
catch_pagefaults = 0;
return VMSUSPEND;
@@ -688,6 +690,7 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
int writeflag;
struct proc *target = NULL;
phys_bytes lin;
if(r != EFAULT_SRC && r != EFAULT_DST)
@@ -699,9 +702,11 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
if(r == EFAULT_SRC) {
lin = vir_addr[_SRC_]->offset;
target = procs[_SRC_];
writeflag = 0;
} else if(r == EFAULT_DST) {
lin = vir_addr[_DST_]->offset;
target = procs[_DST_];
writeflag = 1;
} else {
panic("r strange: %d", r);
}
@@ -709,7 +714,7 @@ int vmcheck; /* if nonzero, can return VMSUSPEND */
assert(caller);
assert(target);
vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL);
vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
return VMSUSPEND;
}

View File

@@ -138,7 +138,7 @@ void hook_ipc_clear(struct proc *proc);
/* system/do_safecopy.c */
int verify_grant(endpoint_t, endpoint_t, cp_grant_id_t, vir_bytes, int,
vir_bytes, vir_bytes *, endpoint_t *);
vir_bytes, vir_bytes *, endpoint_t *, u32_t *);
/* system/do_diagctl.c */
int do_diagctl(struct proc * caller, message *m);
@@ -211,7 +211,7 @@ int arch_phys_map_reply(int index, vir_bytes addr);
reg_t arch_get_sp(struct proc *p);
int arch_enable_paging(struct proc * caller);
int vm_check_range(struct proc *caller,
struct proc *target, vir_bytes vir_addr, size_t bytes);
struct proc *target, vir_bytes vir_addr, size_t bytes, int writable);
int copy_msg_from_user(message * user_mbuf, message * dst);
int copy_msg_to_user(message * src, message * user_mbuf);

View File

@@ -10,7 +10,9 @@
*/
#include "kernel/system.h"
#include "kernel/vm.h"
#include <minix/type.h>
#include <assert.h>
#if (USE_VIRCOPY || USE_PHYSCOPY)
@@ -75,8 +77,16 @@ int do_copy(struct proc * caller, message * m_ptr)
if (bytes != (phys_bytes) (vir_bytes) bytes) return(E2BIG);
/* Now try to make the actual virtual copy. */
return( virtual_copy_vmcheck(caller, &vir_addr[_SRC_],
if(m_ptr->CP_FLAGS & CP_FLAG_TRY) {
int r;
assert(caller->p_endpoint == VFS_PROC_NR);
r = virtual_copy(&vir_addr[_SRC_], &vir_addr[_DST_], bytes);
if(r == EFAULT_SRC || r == EFAULT_DST) return r = EFAULT;
return r;
} else {
return( virtual_copy_vmcheck(caller, &vir_addr[_SRC_],
&vir_addr[_DST_], bytes) );
}
}
#endif /* (USE_VIRCOPY || USE_PHYSCOPY) */

View File

@@ -17,6 +17,7 @@
#include "kernel/system.h"
#include "kernel/kernel.h"
#include "kernel/vm.h"
#define MAX_INDIRECT_DEPTH 5 /* up to how many indirect grants to follow? */
@@ -32,7 +33,7 @@ static int safecopy(struct proc *, endpoint_t, endpoint_t,
* verify_grant *
*===========================================================================*/
int verify_grant(granter, grantee, grant, bytes, access,
offset_in, offset_result, e_granter)
offset_in, offset_result, e_granter, flags)
endpoint_t granter, grantee; /* copyee, copyer */
cp_grant_id_t grant; /* grant id */
vir_bytes bytes; /* copy size */
@@ -40,6 +41,7 @@ int access; /* direction (read/write) */
vir_bytes offset_in; /* copy offset within grant */
vir_bytes *offset_result; /* copy offset within virtual address space */
endpoint_t *e_granter; /* new granter (magic grants) */
u32_t *flags; /* CPF_* */
{
static cp_grant_t g;
static int proc_nr;
@@ -96,6 +98,8 @@ endpoint_t *e_granter; /* new granter (magic grants) */
return EPERM;
}
if(flags) *flags = g.cp_flags;
/* Check validity. */
if((g.cp_flags & (CPF_USED | CPF_VALID)) !=
(CPF_USED | CPF_VALID)) {
@@ -241,6 +245,7 @@ int access; /* CPF_READ for a copy from granter to grantee, CPF_WRITE
endpoint_t new_granter, *src, *dst;
struct proc *granter_p;
int r;
u32_t flags;
#if PERF_USE_COW_SAFECOPY
vir_bytes size;
#endif
@@ -269,7 +274,7 @@ int access; /* CPF_READ for a copy from granter to grantee, CPF_WRITE
/* Verify permission exists. */
if((r=verify_grant(granter, grantee, grantid, bytes, access,
g_offset, &v_offset, &new_granter)) != OK) {
g_offset, &v_offset, &new_granter, &flags)) != OK) {
printf(
"grant %d verify to copy %d->%d by %d failed: err %d\n",
grantid, *src, *dst, grantee, r);
@@ -298,6 +303,13 @@ int access; /* CPF_READ for a copy from granter to grantee, CPF_WRITE
}
/* Do the regular copy. */
if(flags & CPF_TRY) {
int r;
/* Try copy without transparently faulting in pages. */
r = virtual_copy(&v_src, &v_dst, bytes);
if(r == EFAULT_SRC || r == EFAULT_DST) return EFAULT;
return r;
}
return virtual_copy_vmcheck(caller, &v_src, &v_dst, bytes);
}

View File

@@ -47,7 +47,7 @@ int do_safememset(struct proc *caller, message *m_ptr) {
/* Verify permission exists, memset always requires CPF_WRITE */
r = verify_grant(dst_endpt, caller_endpt, grantid, len, CPF_WRITE,
g_offset, &v_offset, &new_granter);
g_offset, &v_offset, &new_granter, NULL);
if (r != OK) {
printf("safememset: grant %d verify failed %d", grantid, r);

View File

@@ -64,7 +64,7 @@ int do_umap_remote(struct proc * caller, message * m_ptr)
cp_grant_id_t grant = (cp_grant_id_t) offset;
if(verify_grant(targetpr->p_endpoint, grantee, grant, count,
0, 0, &newoffset, &newep) != OK) {
0, 0, &newoffset, &newep, NULL) != OK) {
printf("SYSTEM: do_umap: verify_grant in %s, grant %d, bytes 0x%lx, failed, caller %s\n", targetpr->p_name, offset, count, caller->p_name);
proc_stacktrace(caller);
return EFAULT;

View File

@@ -78,7 +78,7 @@ int do_vumap(struct proc *caller, message *m_ptr)
if (source != SELF) {
r = verify_grant(source, endpt, vvec[i].vv_grant, size, access,
offset, &vir_addr, &granter);
offset, &vir_addr, &granter, NULL);
if (r != OK)
return r;
} else {
@@ -103,7 +103,7 @@ int do_vumap(struct proc *caller, message *m_ptr)
/* This call may suspend the current call, or return an
* error for a previous invocation.
*/
return vm_check_range(caller, procp, vir_addr, size);
return vm_check_range(caller, procp, vir_addr, size, 1);
}
pvec[pcount].vp_addr = phys_addr;