minix/servers/vm/exit.c
Ben Gras 565f13088f 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
2014-07-28 17:05:14 +02:00

154 lines
3.1 KiB
C

#define _SYSTEM 1
#include <minix/callnr.h>
#include <minix/com.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/ds.h>
#include <minix/endpoint.h>
#include <minix/minlib.h>
#include <minix/type.h>
#include <minix/ipc.h>
#include <minix/sysutil.h>
#include <minix/syslib.h>
#include <minix/bitmap.h>
#include <errno.h>
#include <assert.h>
#include <env.h>
#include "glo.h"
#include "proto.h"
#include "util.h"
#include "sanitycheck.h"
static void reset_vm_rusage(struct vmproc *vmp)
{
vmp->vm_total = 0;
vmp->vm_total_max = 0;
vmp->vm_minor_page_fault = 0;
vmp->vm_major_page_fault = 0;
}
void free_proc(struct vmproc *vmp)
{
map_free_proc(vmp);
pt_free(&vmp->vm_pt);
region_init(&vmp->vm_regions_avl);
#if VMSTATS
vmp->vm_bytecopies = 0;
#endif
vmp->vm_region_top = 0;
reset_vm_rusage(vmp);
}
void clear_proc(struct vmproc *vmp)
{
region_init(&vmp->vm_regions_avl);
acl_clear(vmp);
vmp->vm_flags = 0; /* Clear INUSE, so slot is free. */
#if VMSTATS
vmp->vm_bytecopies = 0;
#endif
vmp->vm_region_top = 0;
reset_vm_rusage(vmp);
}
/*===========================================================================*
* do_exit *
*===========================================================================*/
int do_exit(message *msg)
{
int proc;
struct vmproc *vmp;
SANITYCHECK(SCL_FUNCTIONS);
if(vm_isokendpt(msg->VME_ENDPOINT, &proc) != OK) {
printf("VM: bogus endpoint VM_EXIT %d\n", msg->VME_ENDPOINT);
return EINVAL;
}
vmp = &vmproc[proc];
if(!(vmp->vm_flags & VMF_EXITING)) {
printf("VM: unannounced VM_EXIT %d\n", msg->VME_ENDPOINT);
return EINVAL;
}
{
/* Free pagetable and pages allocated by pt code. */
SANITYCHECK(SCL_DETAIL);
free_proc(vmp);
SANITYCHECK(SCL_DETAIL);
}
SANITYCHECK(SCL_DETAIL);
/* Reset process slot fields. */
clear_proc(vmp);
SANITYCHECK(SCL_FUNCTIONS);
return OK;
}
/*===========================================================================*
* do_willexit *
*===========================================================================*/
int do_willexit(message *msg)
{
int proc;
struct vmproc *vmp;
if(vm_isokendpt(msg->VMWE_ENDPOINT, &proc) != OK) {
printf("VM: bogus endpoint VM_EXITING %d\n",
msg->VMWE_ENDPOINT);
return EINVAL;
}
vmp = &vmproc[proc];
vmp->vm_flags |= VMF_EXITING;
return OK;
}
int do_procctl(message *msg, int transid)
{
endpoint_t proc;
struct vmproc *vmp;
if(vm_isokendpt(msg->VMPCTL_WHO, &proc) != OK) {
printf("VM: bogus endpoint VM_PROCCTL %ld\n",
msg->VMPCTL_WHO);
return EINVAL;
}
vmp = &vmproc[proc];
switch(msg->VMPCTL_PARAM) {
case VMPPARAM_CLEAR:
if(msg->m_source != RS_PROC_NR
&& msg->m_source != VFS_PROC_NR)
return EPERM;
free_proc(vmp);
if(pt_new(&vmp->vm_pt) != OK)
panic("VMPPARAM_CLEAR: pt_new failed");
pt_bind(&vmp->vm_pt, vmp);
return OK;
case VMPPARAM_HANDLEMEM:
{
if(msg->m_source != VFS_PROC_NR)
return EPERM;
handle_memory_start(vmp, msg->VMPCTL_M1,
msg->VMPCTL_LEN, msg->VMPCTL_FLAGS,
VFS_PROC_NR, VFS_PROC_NR, transid, 1);
return SUSPEND;
}
default:
return EINVAL;
}
return OK;
}