Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#142] Write data back to memory mapped file when eviction #143

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/userprog/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ process_exit (void)
{
struct process_mmap *mmap = list_entry (e, struct process_mmap, elem);
e = list_next (e);
unmap_mmap_item (mmap);
mmap_unmap_item (mmap);
}
#endif

Expand Down Expand Up @@ -643,7 +643,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage,

#ifdef VM
if (!suppl_pt_set_file (upage, file, ofs, page_read_bytes,
page_zero_bytes, writable))
page_zero_bytes, writable, false))
return false;
#else
/* Get a page of memory. */
Expand Down
20 changes: 17 additions & 3 deletions src/userprog/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ syscall_mmap (int fd, void *addr)
{
size_t read_bytes = (size_t) ofs + PGSIZE < size ? PGSIZE : size - ofs;
size_t zero_bytes = PGSIZE - read_bytes;
if (!suppl_pt_set_file (addr + ofs, f, ofs, read_bytes, zero_bytes, true))
if (!suppl_pt_set_file (addr + ofs, f, ofs, read_bytes, zero_bytes,
true, true))
{
fault_ofs = ofs;
goto fail;
Expand Down Expand Up @@ -463,14 +464,27 @@ syscall_munmap (mapid_t mapping)
struct process_mmap *mmap = process_get_mmap (mapping);
if (mmap == NULL)
return;
unmap_mmap_item (mmap);
mmap_unmap_item (mmap);
}

/* Wirtes data back to the original file with given offset. */
off_t
mmap_write_back (struct file *file, void *kpage, off_t ofs)
{
lock_acquire (&filesys_lock);
file = file_reopen (file);
if (file == NULL)
return -1;
off_t writes_byte = file_write_at (file, kpage, PGSIZE, ofs);
lock_release (&filesys_lock);
return writes_byte;
}

/* Unmaps the mapping, which must be a mapping ID returned by
a previous call to mmap by the same process that has not yet
been unmapped. */
void
unmap_mmap_item (struct process_mmap *mmap)
mmap_unmap_item (struct process_mmap *mmap)
{
ASSERT (pg_ofs (mmap->addr) == 0);

Expand Down
6 changes: 4 additions & 2 deletions src/userprog/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#define USERPROG_SYSCALL_H

#ifdef VM
#include "process.h"
#include "filesys/off_t.h"
#include "userprog/process.h"
#endif

void syscall_init (void);
void syscall_exit (int);

#ifdef VM
void unmap_mmap_item (struct process_mmap *);
off_t mmap_write_back (struct file *, void *kpage, off_t);
void mmap_unmap_item (struct process_mmap *);
#endif

#endif /* userprog/syscall.h */
7 changes: 7 additions & 0 deletions src/vm/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ frame_evict_and_get (void)
case PAGE_FILE:
if (!pte->writable)
break;
if (pte->mmap)
{
if (!suppl_pt_update_dirty (pte))
break;
if (mmap_write_back (pte->file, pte->kpage, pte->ofs) == -1)
return NULL;
}

case PAGE_ZERO:
if (!suppl_pt_update_dirty (pte))
Expand Down
4 changes: 3 additions & 1 deletion src/vm/page.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ suppl_pt_set_zero (void *upage)
Note that this does not involve actual frame allocation. */
bool
suppl_pt_set_file (void *upage, struct file *file, off_t ofs,
uint32_t read_bytes, uint32_t zero_bytes, bool writable)
uint32_t read_bytes, uint32_t zero_bytes,
bool writable, bool mmap)
{
if (suppl_pt_get_page (upage) != NULL)
return false;
Expand All @@ -81,6 +82,7 @@ suppl_pt_set_file (void *upage, struct file *file, off_t ofs,
pte->read_bytes = read_bytes;
pte->zero_bytes = zero_bytes;
pte->writable = writable;
pte->mmap = mmap;

struct suppl_pt *pt = thread_current ()->suppl_pt;
hash_insert (&pt->hash, &pte->elem);
Expand Down
3 changes: 2 additions & 1 deletion src/vm/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct suppl_pte
uint32_t read_bytes; /* Read bytes. */
uint32_t zero_bytes; /* Zero bytes. */
bool writable; /* Writable flag. */
bool mmap; /* Memory mapped file. */
};
struct /* Only for page type PAGE_SWAP. */
{
Expand All @@ -52,7 +53,7 @@ void suppl_pt_destroy (struct suppl_pt *);

bool suppl_pt_set_zero (void *upage);
bool suppl_pt_set_file (void *upage, struct file *, off_t, uint32_t read_bytes,
uint32_t zero_bytes, bool writable);
uint32_t zero_bytes, bool writable, bool mmap);
bool suppl_pt_load_page (void *upage);
struct suppl_pte *suppl_pt_get_page (void *upage);
void suppl_pt_clear_page (void *upage);
Expand Down