diff --git a/fs/inode.c b/fs/inode.c
index ee4e66b..c1f55f4 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -278,6 +278,7 @@ void address_space_init_once(struct address_space *mapping)
 	spin_lock_init(&mapping->private_lock);
 	INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
 	INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
+	INIT_LIST_HEAD(&mapping->unpinned_list);
 }
 EXPORT_SYMBOL(address_space_init_once);
 
diff --git a/include/asm-generic/mman-common.h b/include/asm-generic/mman-common.h
index 787abbb..c633d13 100644
--- a/include/asm-generic/mman-common.h
+++ b/include/asm-generic/mman-common.h
@@ -47,6 +47,9 @@
 
 #define MADV_HUGEPAGE	14		/* Worth backing with hugepages */
 #define MADV_NOHUGEPAGE	15		/* Not worth backing with hugepages */
+#define MADV_VOLATILE   16		/* _can_ toss, but don't toss now */
+#define MADV_ISVOLATILE 17		/* Check if page is marked volatile or not */
+#define MADV_NONVOLATILE 18		/* Remove VOLATILE flag */
 
 /* compatibility flags */
 #define MAP_FILE	0
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 0c4df26..a4e281f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -632,6 +632,33 @@ int pagecache_write_end(struct file *, struct address_space *mapping,
 				loff_t pos, unsigned len, unsigned copied,
 				struct page *page, void *fsdata);
 
+
+
+/* upinned_mem_range & range macros from Robert Love's Ashmem patch */
+struct unpinned_mem_range {
+	struct address_space *addrsp;	/* associated address_space */
+	struct list_head unpinned;	/* Next unpinned range */
+	size_t start;
+	size_t end;
+	unsigned int purged;
+};
+
+#define range_size(range) \
+	((range)->end - (range)->start +1)
+#define page_range_subsumes_range(range, start_addr, end_addr) \
+	(((range)->start >= (start_addr)) && ((range)->end <= (end_addr)))
+#define page_range_subsumed_by_range(range, start_addr, end_addr) \
+	(((range)->start <= (start_addr)) && ((range)->end >= (end_addr)))
+#define page_in_range(range, page) \
+	(((range)->start <= (page)) && ((range)->end >= (page)))
+#define page_range_in_range(range, start_addr, end_addr) \
+	(page_in_range(range, start_addr) || page_in_range(range, end_addr) || \
+	page_range_subsumes_range(range, start_addr, end_addr))
+#define range_before_page(range, page) \
+	((range)->end < (page))
+
+
+
 struct backing_dev_info;
 struct address_space {
 	struct inode		*host;		/* owner: inode, block_device */
@@ -650,6 +677,7 @@ struct address_space {
 	spinlock_t		private_lock;	/* for use by the address_space */
 	struct list_head	private_list;	/* ditto */
 	struct address_space	*assoc_mapping;	/* ditto */
+	struct list_head	unpinned_list;	/* unpinned area list */
 } __attribute__((aligned(sizeof(long))));
 	/*
 	 * On most architectures that alignment is already the case; but
diff --git a/mm/madvise.c b/mm/madvise.c
index 74bf193..fdc98e5 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -225,6 +225,168 @@ static long madvise_remove(struct vm_area_struct *vma,
 	return error;
 }
 
+
+
+static int unpinned_range_alloc(struct address_space *addrsp,
+				struct unpinned_mem_range *prev_range,
+				unsigned int purged, size_t start, size_t end)
+{
+	struct unpinned_mem_range *range;
+
+	range = kzalloc(sizeof(struct unpinned_mem_range), GFP_KERNEL);
+	if (unlikely(!range))
+		return -ENOMEM;
+
+	range->addrsp = addrsp;
+	range->start = start;
+	range->end = end;
+	range->purged = purged;
+
+	list_add_tail(&range->unpinned, &prev_range->unpinned);
+
+	return 0;
+}
+
+static void unpinned_range_del(struct unpinned_mem_range *range)
+{
+	list_del(&range->unpinned);
+	kfree(range);
+}
+
+static inline void unpinned_range_shrink(struct unpinned_mem_range *range,
+				size_t start, size_t end)
+{
+	range->start = start;
+	range->end = end;
+}
+
+
+static long madvise_volatile(struct vm_area_struct * vma,
+			     unsigned long start, unsigned long end)
+{
+	struct unpinned_mem_range *range, *next;
+	unsigned int purged = 0;
+	int ret;
+	struct address_space *addrsp;
+
+	/* XXX - check start->end is within mmapped shm region */
+	if(!vma->vm_file)
+		return -1;
+
+	addrsp=vma->vm_file->f_mapping;
+
+
+	start -= vma->vm_start;
+	end -= vma->vm_start;
+
+	printk("Madvise_volatile: start: 0x%lx  end: 0x%lx vma start: 0x%lx\n", start, end, vma->vm_start);
+
+restart:
+	list_for_each_entry_safe(range, next, &addrsp->unpinned_list,
+				unpinned) {
+		if (range_before_page(range, start))
+			break;
+
+		if(page_range_subsumed_by_range(range, start, end))
+			return 0;
+		if (page_range_in_range(range, start,end)) {
+			start = min_t(size_t, range->start, start);
+			end = max_t(size_t, range->end, end);
+			purged |= range->purged;
+			unpinned_range_del(range);
+			goto restart;
+		}
+
+	}
+	ret = unpinned_range_alloc(addrsp, range, purged, start, end);
+	return ret;
+}
+
+static long madvise_nonvolatile(struct vm_area_struct * vma,
+				unsigned long start, unsigned long end)
+{
+	struct unpinned_mem_range *range, *next;
+	int ret  = 0;
+	struct address_space *addrsp;
+	if(!vma->vm_file)
+		return -1;
+	addrsp=vma->vm_file->f_mapping;
+
+
+	start -= vma->vm_start;
+	end -= vma->vm_start;
+
+	list_for_each_entry_safe(range, next, &addrsp->unpinned_list,
+				unpinned) {
+		if (range_before_page(range, start))
+			break;
+
+		if (page_range_in_range(range, start, end)) {
+			ret |= range->purged;
+			/* Case #1: Easy. Just nuke the whole thing. */
+			if (page_range_subsumes_range(range, start, end)) {
+				unpinned_range_del(range);
+				continue;
+			}
+
+			/* Case #2: We overlap from the start, so adjust it */
+			if (range->start >= start) {
+				unpinned_range_shrink(range, end + 1,
+							range->end);
+				continue;
+			}
+
+			/* Case #3: We overlap from the rear, so adjust it */
+			if (range->end <= end) {
+				unpinned_range_shrink(range, range->start,
+							start-1);
+				continue;
+			}
+
+			/*
+			 * Case #4: We eat a chunk out of the middle. A bit
+			 * more complicated, we allocate a new range for the
+			 * second half and adjust the first chunk's endpoint.
+			 */
+			unpinned_range_alloc(addrsp, range,
+						range->purged, end + 1,
+						range->end);
+			unpinned_range_shrink(range, range->start, start - 1);
+
+		}
+
+	}
+
+	printk("Madvise_nonvolatile: start: 0x%lx  end: 0x%lx vma start: 0x%lx ret: %i\n", start, end, vma->vm_start, ret);
+	return ret;
+
+
+}
+
+static long madvise_isvolatile(struct vm_area_struct * vma,
+				unsigned long start, unsigned long end)
+{
+	struct unpinned_mem_range *range;
+	long ret = 0;
+	struct address_space *addrsp;
+	if(!vma->vm_file)
+		return -1;
+	addrsp=vma->vm_file->f_mapping;
+
+	start -= vma->vm_start;
+	end -= vma->vm_start;
+
+	list_for_each_entry(range, &addrsp->unpinned_list, unpinned) {
+		if (range_before_page(range, start))
+			break;
+		if (page_range_in_range(range, start, end)) {
+			ret = 1;
+			break;
+		}
+	}
+	return ret;
+}
+
 #ifdef CONFIG_MEMORY_FAILURE
 /*
  * Error injection support for memory error handling.
@@ -268,6 +430,12 @@ madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
 		return madvise_willneed(vma, prev, start, end);
 	case MADV_DONTNEED:
 		return madvise_dontneed(vma, prev, start, end);
+	case MADV_VOLATILE:
+		return madvise_volatile(vma, start, end);
+	case MADV_ISVOLATILE:
+		return madvise_isvolatile(vma, start, end);
+	case MADV_NONVOLATILE:
+		return madvise_nonvolatile(vma, start, end);
 	default:
 		return madvise_behavior(vma, prev, start, end, behavior);
 	}
@@ -293,6 +461,9 @@ madvise_behavior_valid(int behavior)
 	case MADV_HUGEPAGE:
 	case MADV_NOHUGEPAGE:
 #endif
+	case MADV_VOLATILE:
+	case MADV_ISVOLATILE:
+	case MADV_NONVOLATILE:
 		return 1;
 
 	default:
diff --git a/mm/shmem.c b/mm/shmem.c
index d672250..85506e0 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -679,6 +679,26 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	index = page->index;
 	inode = mapping->host;
 	info = SHMEM_I(inode);
+
+	if (!list_empty(&mapping->unpinned_list)) {
+		struct unpinned_mem_range *range, *next;
+		printk("shmem_writepage:\n");
+		list_for_each_entry_safe(range, next, &mapping->unpinned_list,
+					unpinned) {
+			printk("	range: 0x%lx - 0x%lx vs 0x%lx\n",
+					range->start, range->end,
+					(long)page->index << PAGE_SHIFT);
+
+			if (page_in_range(range,
+					(long)page->index << PAGE_SHIFT)) {
+				range->purged = 1;
+				unlock_page(page);
+				printk("	Purged page!\n");
+				return 0;
+			}
+		}
+	}
+
 	if (info->flags & VM_LOCKED)
 		goto redirty;
 	if (!total_swap_pages)
